Skip to content

Commit

Permalink
FS-Cache: Provide proper invalidation
Browse files Browse the repository at this point in the history
Provide a proper invalidation method rather than relying on the netfs retiring
the cookie it has and getting a new one.  The problem with this is that isn't
easy for the netfs to make sure that it has completed/cancelled all its
outstanding storage and retrieval operations on the cookie it is retiring.

Instead, have the cache provide an invalidation method that will cancel or wait
for all currently outstanding operations before invalidating the cache, and
will cause new operations to queue up behind that.  Whilst invalidation is in
progress, some requests will be rejected until the cache can stack a barrier on
the operation queue to cause new operations to be deferred behind it.

Signed-off-by: David Howells <[email protected]>
  • Loading branch information
dhowells committed Dec 20, 2012
1 parent 9f10523 commit ef778e7
Show file tree
Hide file tree
Showing 11 changed files with 345 additions and 18 deletions.
12 changes: 12 additions & 0 deletions Documentation/filesystems/caching/backend-api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,18 @@ performed on the denizens of the cache. These are held in a structure of type:
obtained by calling object->cookie->def->get_aux()/get_attr().


(*) Invalidate data object [mandatory]:

int (*invalidate_object)(struct fscache_operation *op)

This is called to invalidate a data object (as pointed to by op->object).
All the data stored for this object should be discarded and an
attr_changed operation should be performed. The caller will follow up
with an object update operation.

fscache_op_complete() must be called on op before returning.


(*) Discard object [mandatory]:

void (*drop_object)(struct fscache_object *object)
Expand Down
46 changes: 38 additions & 8 deletions Documentation/filesystems/caching/netfs-api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ This document contains the following sections:
(12) Index and data file update
(13) Miscellaneous cookie operations
(14) Cookie unregistration
(15) Index and data file invalidation
(16) FS-Cache specific page flags.
(15) Index invalidation
(16) Data file invalidation
(17) FS-Cache specific page flags.


=============================
Expand Down Expand Up @@ -767,13 +768,42 @@ the cookies for "child" indices, objects and pages have been relinquished
first.


================================
INDEX AND DATA FILE INVALIDATION
================================
==================
INDEX INVALIDATION
==================

There is no direct way to invalidate an index subtree. To do this, the caller
should relinquish and retire the cookie they have, and then acquire a new one.


======================
DATA FILE INVALIDATION
======================

Sometimes it will be necessary to invalidate an object that contains data.
Typically this will be necessary when the server tells the netfs of a foreign
change - at which point the netfs has to throw away all the state it had for an
inode and reload from the server.

To indicate that a cache object should be invalidated, the following function
can be called:

void fscache_invalidate(struct fscache_cookie *cookie);

This can be called with spinlocks held as it defers the work to a thread pool.
All extant storage, retrieval and attribute change ops at this point are
cancelled and discarded. Some future operations will be rejected until the
cache has had a chance to insert a barrier in the operations queue. After
that, operations will be queued again behind the invalidation operation.

The invalidation operation will perform an attribute change operation and an
auxiliary data update operation as it is very likely these will have changed.

Using the following function, the netfs can wait for the invalidation operation
to have reached a point at which it can start submitting ordinary operations
once again:

There is no direct way to invalidate an index subtree or a data file. To do
this, the caller should relinquish and retire the cookie they have, and then
acquire a new one.
void fscache_wait_on_invalidate(struct fscache_cookie *cookie);


===========================
Expand Down
23 changes: 15 additions & 8 deletions Documentation/filesystems/caching/object.txt
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,14 @@ servicing netfs requests:
The normal running state. In this state, requests the netfs makes will be
passed on to the cache.

(6) State FSCACHE_OBJECT_UPDATING.
(6) State FSCACHE_OBJECT_INVALIDATING.

The object is undergoing invalidation. When the state comes here, it
discards all pending read, write and attribute change operations as it is
going to clear out the cache entirely and reinitialise it. It will then
continue to the FSCACHE_OBJECT_UPDATING state.

(7) State FSCACHE_OBJECT_UPDATING.

The state machine comes here to update the object in the cache from the
netfs's records. This involves updating the auxiliary data that is used
Expand All @@ -225,13 +232,13 @@ servicing netfs requests:
And there are terminal states in which an object cleans itself up, deallocates
memory and potentially deletes stuff from disk:

(7) State FSCACHE_OBJECT_LC_DYING.
(8) State FSCACHE_OBJECT_LC_DYING.

The object comes here if it is dying because of a lookup or creation
error. This would be due to a disk error or system error of some sort.
Temporary data is cleaned up, and the parent is released.

(8) State FSCACHE_OBJECT_DYING.
(9) State FSCACHE_OBJECT_DYING.

The object comes here if it is dying due to an error, because its parent
cookie has been relinquished by the netfs or because the cache is being
Expand All @@ -241,27 +248,27 @@ memory and potentially deletes stuff from disk:
can destroy themselves. This object waits for all its children to go away
before advancing to the next state.

(9) State FSCACHE_OBJECT_ABORT_INIT.
(10) State FSCACHE_OBJECT_ABORT_INIT.

The object comes to this state if it was waiting on its parent in
FSCACHE_OBJECT_INIT, but its parent died. The object will destroy itself
so that the parent may proceed from the FSCACHE_OBJECT_DYING state.

(10) State FSCACHE_OBJECT_RELEASING.
(11) State FSCACHE_OBJECT_RECYCLING.
(11) State FSCACHE_OBJECT_RELEASING.
(12) State FSCACHE_OBJECT_RECYCLING.

The object comes to one of these two states when dying once it is rid of
all its children, if it is dying because the netfs relinquished its
cookie. In the first state, the cached data is expected to persist, and
in the second it will be deleted.

(12) State FSCACHE_OBJECT_WITHDRAWING.
(13) State FSCACHE_OBJECT_WITHDRAWING.

The object transits to this state if the cache decides it wants to
withdraw the object from service, perhaps to make space, but also due to
error or just because the whole cache is being withdrawn.

(13) State FSCACHE_OBJECT_DEAD.
(14) State FSCACHE_OBJECT_DEAD.

The object transits to this state when the in-memory object record is
ready to be deleted. The object processor shouldn't ever see an object in
Expand Down
60 changes: 60 additions & 0 deletions fs/fscache/cookie.c
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,66 @@ static int fscache_attach_object(struct fscache_cookie *cookie,
return ret;
}

/*
* Invalidate an object. Callable with spinlocks held.
*/
void __fscache_invalidate(struct fscache_cookie *cookie)
{
struct fscache_object *object;

_enter("{%s}", cookie->def->name);

fscache_stat(&fscache_n_invalidates);

/* Only permit invalidation of data files. Invalidating an index will
* require the caller to release all its attachments to the tree rooted
* there, and if it's doing that, it may as well just retire the
* cookie.
*/
ASSERTCMP(cookie->def->type, ==, FSCACHE_COOKIE_TYPE_DATAFILE);

/* We will be updating the cookie too. */
BUG_ON(!cookie->def->get_aux);

/* If there's an object, we tell the object state machine to handle the
* invalidation on our behalf, otherwise there's nothing to do.
*/
if (!hlist_empty(&cookie->backing_objects)) {
spin_lock(&cookie->lock);

if (!hlist_empty(&cookie->backing_objects) &&
!test_and_set_bit(FSCACHE_COOKIE_INVALIDATING,
&cookie->flags)) {
object = hlist_entry(cookie->backing_objects.first,
struct fscache_object,
cookie_link);
if (object->state < FSCACHE_OBJECT_DYING)
fscache_raise_event(
object, FSCACHE_OBJECT_EV_INVALIDATE);
}

spin_unlock(&cookie->lock);
}

_leave("");
}
EXPORT_SYMBOL(__fscache_invalidate);

/*
* Wait for object invalidation to complete.
*/
void __fscache_wait_on_invalidate(struct fscache_cookie *cookie)
{
_enter("%p", cookie);

wait_on_bit(&cookie->flags, FSCACHE_COOKIE_INVALIDATING,
fscache_wait_bit_interruptible,
TASK_UNINTERRUPTIBLE);

_leave("");
}
EXPORT_SYMBOL(__fscache_wait_on_invalidate);

/*
* update the index entries backing a cookie
*/
Expand Down
10 changes: 10 additions & 0 deletions fs/fscache/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,16 @@ extern int fscache_submit_exclusive_op(struct fscache_object *,
extern int fscache_submit_op(struct fscache_object *,
struct fscache_operation *);
extern int fscache_cancel_op(struct fscache_operation *);
extern void fscache_cancel_all_ops(struct fscache_object *);
extern void fscache_abort_object(struct fscache_object *);
extern void fscache_start_operations(struct fscache_object *);
extern void fscache_operation_gc(struct work_struct *);

/*
* page.c
*/
extern void fscache_invalidate_writes(struct fscache_cookie *);

/*
* proc.c
*/
Expand Down Expand Up @@ -205,6 +211,9 @@ extern atomic_t fscache_n_acquires_ok;
extern atomic_t fscache_n_acquires_nobufs;
extern atomic_t fscache_n_acquires_oom;

extern atomic_t fscache_n_invalidates;
extern atomic_t fscache_n_invalidates_run;

extern atomic_t fscache_n_updates;
extern atomic_t fscache_n_updates_null;
extern atomic_t fscache_n_updates_run;
Expand Down Expand Up @@ -237,6 +246,7 @@ extern atomic_t fscache_n_cop_alloc_object;
extern atomic_t fscache_n_cop_lookup_object;
extern atomic_t fscache_n_cop_lookup_complete;
extern atomic_t fscache_n_cop_grab_object;
extern atomic_t fscache_n_cop_invalidate_object;
extern atomic_t fscache_n_cop_update_object;
extern atomic_t fscache_n_cop_drop_object;
extern atomic_t fscache_n_cop_put_object;
Expand Down
72 changes: 72 additions & 0 deletions fs/fscache/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#define FSCACHE_DEBUG_LEVEL COOKIE
#include <linux/module.h>
#include <linux/slab.h>
#include "internal.h"

const char *fscache_object_states[FSCACHE_OBJECT__NSTATES] = {
Expand All @@ -22,6 +23,7 @@ const char *fscache_object_states[FSCACHE_OBJECT__NSTATES] = {
[FSCACHE_OBJECT_CREATING] = "OBJECT_CREATING",
[FSCACHE_OBJECT_AVAILABLE] = "OBJECT_AVAILABLE",
[FSCACHE_OBJECT_ACTIVE] = "OBJECT_ACTIVE",
[FSCACHE_OBJECT_INVALIDATING] = "OBJECT_INVALIDATING",
[FSCACHE_OBJECT_UPDATING] = "OBJECT_UPDATING",
[FSCACHE_OBJECT_DYING] = "OBJECT_DYING",
[FSCACHE_OBJECT_LC_DYING] = "OBJECT_LC_DYING",
Expand All @@ -39,6 +41,7 @@ const char fscache_object_states_short[FSCACHE_OBJECT__NSTATES][5] = {
[FSCACHE_OBJECT_CREATING] = "CRTN",
[FSCACHE_OBJECT_AVAILABLE] = "AVBL",
[FSCACHE_OBJECT_ACTIVE] = "ACTV",
[FSCACHE_OBJECT_INVALIDATING] = "INVL",
[FSCACHE_OBJECT_UPDATING] = "UPDT",
[FSCACHE_OBJECT_DYING] = "DYNG",
[FSCACHE_OBJECT_LC_DYING] = "LCDY",
Expand All @@ -54,6 +57,7 @@ static void fscache_put_object(struct fscache_object *);
static void fscache_initialise_object(struct fscache_object *);
static void fscache_lookup_object(struct fscache_object *);
static void fscache_object_available(struct fscache_object *);
static void fscache_invalidate_object(struct fscache_object *);
static void fscache_release_object(struct fscache_object *);
static void fscache_withdraw_object(struct fscache_object *);
static void fscache_enqueue_dependents(struct fscache_object *);
Expand All @@ -78,6 +82,15 @@ static inline void fscache_done_parent_op(struct fscache_object *object)
spin_unlock(&parent->lock);
}

/*
* Notify netfs of invalidation completion.
*/
static inline void fscache_invalidation_complete(struct fscache_cookie *cookie)
{
if (test_and_clear_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags))
wake_up_bit(&cookie->flags, FSCACHE_COOKIE_INVALIDATING);
}

/*
* process events that have been sent to an object's state machine
* - initiates parent lookup
Expand Down Expand Up @@ -125,6 +138,16 @@ static void fscache_object_state_machine(struct fscache_object *object)
case FSCACHE_OBJECT_ACTIVE:
goto active_transit;

/* Invalidate an object on disk */
case FSCACHE_OBJECT_INVALIDATING:
clear_bit(FSCACHE_OBJECT_EV_INVALIDATE, &object->events);
fscache_stat(&fscache_n_invalidates_run);
fscache_stat(&fscache_n_cop_invalidate_object);
fscache_invalidate_object(object);
fscache_stat_d(&fscache_n_cop_invalidate_object);
fscache_raise_event(object, FSCACHE_OBJECT_EV_UPDATE);
goto active_transit;

/* update the object metadata on disk */
case FSCACHE_OBJECT_UPDATING:
clear_bit(FSCACHE_OBJECT_EV_UPDATE, &object->events);
Expand Down Expand Up @@ -275,6 +298,9 @@ static void fscache_object_state_machine(struct fscache_object *object)
case FSCACHE_OBJECT_EV_ERROR:
new_state = FSCACHE_OBJECT_DYING;
goto change_state;
case FSCACHE_OBJECT_EV_INVALIDATE:
new_state = FSCACHE_OBJECT_INVALIDATING;
goto change_state;
case FSCACHE_OBJECT_EV_UPDATE:
new_state = FSCACHE_OBJECT_UPDATING;
goto change_state;
Expand Down Expand Up @@ -679,6 +705,7 @@ static void fscache_withdraw_object(struct fscache_object *object)
if (object->cookie == cookie) {
hlist_del_init(&object->cookie_link);
object->cookie = NULL;
fscache_invalidation_complete(cookie);
detached = true;
}
spin_unlock(&cookie->lock);
Expand Down Expand Up @@ -888,3 +915,48 @@ enum fscache_checkaux fscache_check_aux(struct fscache_object *object,
return result;
}
EXPORT_SYMBOL(fscache_check_aux);

/*
* Asynchronously invalidate an object.
*/
static void fscache_invalidate_object(struct fscache_object *object)
{
struct fscache_operation *op;
struct fscache_cookie *cookie = object->cookie;

_enter("{OBJ%x}", object->debug_id);

/* Reject any new read/write ops and abort any that are pending. */
fscache_invalidate_writes(cookie);
clear_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags);
fscache_cancel_all_ops(object);

/* Now we have to wait for in-progress reads and writes */
op = kzalloc(sizeof(*op), GFP_KERNEL);
if (!op) {
fscache_raise_event(object, FSCACHE_OBJECT_EV_ERROR);
_leave(" [ENOMEM]");
return;
}

fscache_operation_init(op, object->cache->ops->invalidate_object, NULL);
op->flags = FSCACHE_OP_ASYNC | (1 << FSCACHE_OP_EXCLUSIVE);

spin_lock(&cookie->lock);
if (fscache_submit_exclusive_op(object, op) < 0)
BUG();
spin_unlock(&cookie->lock);
fscache_put_operation(op);

/* Once we've completed the invalidation, we know there will be no data
* stored in the cache and thus we can reinstate the data-check-skip
* optimisation.
*/
set_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);

/* We can allow read and write requests to come in once again. They'll
* queue up behind our exclusive invalidation operation.
*/
fscache_invalidation_complete(cookie);
_leave("");
}
Loading

0 comments on commit ef778e7

Please sign in to comment.