Commit bcd7351e authored by Linus Torvalds's avatar Linus Torvalds

Merge tag 'fscache-20130702' of git://git.kernel.org/pub/scm/linux/kernel/git/dhowells/linux-fs

Pull FS-Cache updates from David Howells:
 "This contains a number of fixes for various FS-Cache issues plus some
  cleanups.  The commits are, in order:

   1) Provide a system wait_on_atomic_t() and wake_up_atomic_t() sharing
      the bit-wait table (enhancement for #8).

   2) Don't put spin_lock() in a while-condition as spin_lock() may have
      a do {} while(0) wrapper (cleanup).

   3) Symbolically name i_mutex lock classes rather than using numbers
      in CacheFiles (cleanup).

   4) Don't sleep in page release if __GFP_FS is not set (deadlock vs
      ext4).

   5) Uninline fscache_object_init() (cleanup for #7).

   6) Wrap checks on object state (cleanup for #7).

   7) Simplify the object state machine by separating work states from
      wait states.

   8) Simplify cookie retention by objects (NULL pointer deref fix).

   9) Remove unused list_to_page() macro (cleanup).

  10) Make the remaining-pages counter in the retrieval op atomic
      (assertion failure fix).

  11) Don't use spin_is_locked() in assertions (assertion failure fix)"

* tag 'fscache-20130702' of git://git.kernel.org/pub/scm/linux/kernel/git/dhowells/linux-fs:
  FS-Cache: Don't use spin_is_locked() in assertions
  FS-Cache: The retrieval remaining-pages counter needs to be atomic_t
  cachefiles: remove unused macro list_to_page()
  FS-Cache: Simplify cookie retention for fscache_objects, fixing oops
  FS-Cache: Fix object state machine to have separate work and wait states
  FS-Cache: Wrap checks on object state
  FS-Cache: Uninline fscache_object_init()
  FS-Cache: Don't sleep in page release if __GFP_FS is not set
  CacheFiles: name i_mutex lock class explicitly
  fs/fscache: remove spin_lock() from the condition in while()
  Add wait_on_atomic_t() and wake_up_atomic_t()
parents 6072a93b dcfae32f
...@@ -13,8 +13,6 @@ ...@@ -13,8 +13,6 @@
#include <linux/mount.h> #include <linux/mount.h>
#include "internal.h" #include "internal.h"
#define list_to_page(head) (list_entry((head)->prev, struct page, lru))
struct cachefiles_lookup_data { struct cachefiles_lookup_data {
struct cachefiles_xattr *auxdata; /* auxiliary data */ struct cachefiles_xattr *auxdata; /* auxiliary data */
char *key; /* key path */ char *key; /* key path */
...@@ -212,20 +210,29 @@ static void cachefiles_update_object(struct fscache_object *_object) ...@@ -212,20 +210,29 @@ static void cachefiles_update_object(struct fscache_object *_object)
object = container_of(_object, struct cachefiles_object, fscache); object = container_of(_object, struct cachefiles_object, fscache);
cache = container_of(object->fscache.cache, struct cachefiles_cache, cache = container_of(object->fscache.cache, struct cachefiles_cache,
cache); cache);
if (!fscache_use_cookie(_object)) {
_leave(" [relinq]");
return;
}
cookie = object->fscache.cookie; cookie = object->fscache.cookie;
if (!cookie->def->get_aux) { if (!cookie->def->get_aux) {
fscache_unuse_cookie(_object);
_leave(" [no aux]"); _leave(" [no aux]");
return; return;
} }
auxdata = kmalloc(2 + 512 + 3, cachefiles_gfp); auxdata = kmalloc(2 + 512 + 3, cachefiles_gfp);
if (!auxdata) { if (!auxdata) {
fscache_unuse_cookie(_object);
_leave(" [nomem]"); _leave(" [nomem]");
return; return;
} }
auxlen = cookie->def->get_aux(cookie->netfs_data, auxdata->data, 511); auxlen = cookie->def->get_aux(cookie->netfs_data, auxdata->data, 511);
fscache_unuse_cookie(_object);
ASSERTCMP(auxlen, <, 511); ASSERTCMP(auxlen, <, 511);
auxdata->len = auxlen + 1; auxdata->len = auxlen + 1;
...@@ -263,7 +270,7 @@ static void cachefiles_drop_object(struct fscache_object *_object) ...@@ -263,7 +270,7 @@ static void cachefiles_drop_object(struct fscache_object *_object)
#endif #endif
/* delete retired objects */ /* delete retired objects */
if (object->fscache.state == FSCACHE_OBJECT_RECYCLING && if (test_bit(FSCACHE_COOKIE_RETIRED, &object->fscache.cookie->flags) &&
_object != cache->cache.fsdef _object != cache->cache.fsdef
) { ) {
_debug("- retire object OBJ%x", object->fscache.debug_id); _debug("- retire object OBJ%x", object->fscache.debug_id);
......
...@@ -38,7 +38,7 @@ void __cachefiles_printk_object(struct cachefiles_object *object, ...@@ -38,7 +38,7 @@ void __cachefiles_printk_object(struct cachefiles_object *object,
printk(KERN_ERR "%sobject: OBJ%x\n", printk(KERN_ERR "%sobject: OBJ%x\n",
prefix, object->fscache.debug_id); prefix, object->fscache.debug_id);
printk(KERN_ERR "%sobjstate=%s fl=%lx wbusy=%x ev=%lx[%lx]\n", printk(KERN_ERR "%sobjstate=%s fl=%lx wbusy=%x ev=%lx[%lx]\n",
prefix, fscache_object_states[object->fscache.state], prefix, object->fscache.state->name,
object->fscache.flags, work_busy(&object->fscache.work), object->fscache.flags, work_busy(&object->fscache.work),
object->fscache.events, object->fscache.event_mask); object->fscache.events, object->fscache.event_mask);
printk(KERN_ERR "%sops=%u inp=%u exc=%u\n", printk(KERN_ERR "%sops=%u inp=%u exc=%u\n",
...@@ -127,10 +127,10 @@ static void cachefiles_mark_object_buried(struct cachefiles_cache *cache, ...@@ -127,10 +127,10 @@ static void cachefiles_mark_object_buried(struct cachefiles_cache *cache,
found_dentry: found_dentry:
kdebug("preemptive burial: OBJ%x [%s] %p", kdebug("preemptive burial: OBJ%x [%s] %p",
object->fscache.debug_id, object->fscache.debug_id,
fscache_object_states[object->fscache.state], object->fscache.state->name,
dentry); dentry);
if (object->fscache.state < FSCACHE_OBJECT_DYING) { if (fscache_object_is_live(&object->fscache)) {
printk(KERN_ERR "\n"); printk(KERN_ERR "\n");
printk(KERN_ERR "CacheFiles: Error:" printk(KERN_ERR "CacheFiles: Error:"
" Can't preemptively bury live object\n"); " Can't preemptively bury live object\n");
...@@ -192,7 +192,7 @@ static int cachefiles_mark_object_active(struct cachefiles_cache *cache, ...@@ -192,7 +192,7 @@ static int cachefiles_mark_object_active(struct cachefiles_cache *cache,
/* an old object from a previous incarnation is hogging the slot - we /* an old object from a previous incarnation is hogging the slot - we
* need to wait for it to be destroyed */ * need to wait for it to be destroyed */
wait_for_old_object: wait_for_old_object:
if (xobject->fscache.state < FSCACHE_OBJECT_DYING) { if (fscache_object_is_live(&object->fscache)) {
printk(KERN_ERR "\n"); printk(KERN_ERR "\n");
printk(KERN_ERR "CacheFiles: Error:" printk(KERN_ERR "CacheFiles: Error:"
" Unexpected object collision\n"); " Unexpected object collision\n");
...@@ -836,7 +836,7 @@ static struct dentry *cachefiles_check_active(struct cachefiles_cache *cache, ...@@ -836,7 +836,7 @@ static struct dentry *cachefiles_check_active(struct cachefiles_cache *cache,
// dir->d_name.len, dir->d_name.len, dir->d_name.name, filename); // dir->d_name.len, dir->d_name.len, dir->d_name.name, filename);
/* look up the victim */ /* look up the victim */
mutex_lock_nested(&dir->d_inode->i_mutex, 1); mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
start = jiffies; start = jiffies;
victim = lookup_one_len(filename, dir, strlen(filename)); victim = lookup_one_len(filename, dir, strlen(filename));
......
...@@ -109,13 +109,12 @@ int cachefiles_set_object_xattr(struct cachefiles_object *object, ...@@ -109,13 +109,12 @@ int cachefiles_set_object_xattr(struct cachefiles_object *object,
struct dentry *dentry = object->dentry; struct dentry *dentry = object->dentry;
int ret; int ret;
ASSERT(object->fscache.cookie);
ASSERT(dentry); ASSERT(dentry);
_enter("%p,#%d", object, auxdata->len); _enter("%p,#%d", object, auxdata->len);
/* attempt to install the cache metadata directly */ /* attempt to install the cache metadata directly */
_debug("SET %s #%u", object->fscache.cookie->def->name, auxdata->len); _debug("SET #%u", auxdata->len);
ret = vfs_setxattr(dentry, cachefiles_xattr_cache, ret = vfs_setxattr(dentry, cachefiles_xattr_cache,
&auxdata->type, auxdata->len, &auxdata->type, auxdata->len,
...@@ -138,13 +137,12 @@ int cachefiles_update_object_xattr(struct cachefiles_object *object, ...@@ -138,13 +137,12 @@ int cachefiles_update_object_xattr(struct cachefiles_object *object,
struct dentry *dentry = object->dentry; struct dentry *dentry = object->dentry;
int ret; int ret;
ASSERT(object->fscache.cookie);
ASSERT(dentry); ASSERT(dentry);
_enter("%p,#%d", object, auxdata->len); _enter("%p,#%d", object, auxdata->len);
/* attempt to install the cache metadata directly */ /* attempt to install the cache metadata directly */
_debug("SET %s #%u", object->fscache.cookie->def->name, auxdata->len); _debug("SET #%u", auxdata->len);
ret = vfs_setxattr(dentry, cachefiles_xattr_cache, ret = vfs_setxattr(dentry, cachefiles_xattr_cache,
&auxdata->type, auxdata->len, &auxdata->type, auxdata->len,
......
...@@ -115,7 +115,7 @@ struct fscache_cache *fscache_select_cache_for_object( ...@@ -115,7 +115,7 @@ struct fscache_cache *fscache_select_cache_for_object(
struct fscache_object, cookie_link); struct fscache_object, cookie_link);
cache = object->cache; cache = object->cache;
if (object->state >= FSCACHE_OBJECT_DYING || if (fscache_object_is_dying(object) ||
test_bit(FSCACHE_IOERROR, &cache->flags)) test_bit(FSCACHE_IOERROR, &cache->flags))
cache = NULL; cache = NULL;
...@@ -224,8 +224,10 @@ int fscache_add_cache(struct fscache_cache *cache, ...@@ -224,8 +224,10 @@ int fscache_add_cache(struct fscache_cache *cache,
BUG_ON(!ifsdef); BUG_ON(!ifsdef);
cache->flags = 0; cache->flags = 0;
ifsdef->event_mask = ULONG_MAX & ~(1 << FSCACHE_OBJECT_EV_CLEARED); ifsdef->event_mask =
ifsdef->state = FSCACHE_OBJECT_ACTIVE; ((1 << NR_FSCACHE_OBJECT_EVENTS) - 1) &
~(1 << FSCACHE_OBJECT_EV_CLEARED);
__set_bit(FSCACHE_OBJECT_IS_AVAILABLE, &ifsdef->flags);
if (!tagname) if (!tagname)
tagname = cache->identifier; tagname = cache->identifier;
...@@ -330,25 +332,25 @@ static void fscache_withdraw_all_objects(struct fscache_cache *cache, ...@@ -330,25 +332,25 @@ static void fscache_withdraw_all_objects(struct fscache_cache *cache,
{ {
struct fscache_object *object; struct fscache_object *object;
spin_lock(&cache->object_list_lock);
while (!list_empty(&cache->object_list)) { while (!list_empty(&cache->object_list)) {
object = list_entry(cache->object_list.next, spin_lock(&cache->object_list_lock);
struct fscache_object, cache_link);
list_move_tail(&object->cache_link, dying_objects);
_debug("withdraw %p", object->cookie); if (!list_empty(&cache->object_list)) {
object = list_entry(cache->object_list.next,
struct fscache_object, cache_link);
list_move_tail(&object->cache_link, dying_objects);
spin_lock(&object->lock); _debug("withdraw %p", object->cookie);
spin_unlock(&cache->object_list_lock);
fscache_raise_event(object, FSCACHE_OBJECT_EV_WITHDRAW); /* This must be done under object_list_lock to prevent
spin_unlock(&object->lock); * a race with fscache_drop_object().
*/
fscache_raise_event(object, FSCACHE_OBJECT_EV_KILL);
}
spin_unlock(&cache->object_list_lock);
cond_resched(); cond_resched();
spin_lock(&cache->object_list_lock);
} }
spin_unlock(&cache->object_list_lock);
} }
/** /**
......
...@@ -95,6 +95,11 @@ struct fscache_cookie *__fscache_acquire_cookie( ...@@ -95,6 +95,11 @@ struct fscache_cookie *__fscache_acquire_cookie(
atomic_set(&cookie->usage, 1); atomic_set(&cookie->usage, 1);
atomic_set(&cookie->n_children, 0); atomic_set(&cookie->n_children, 0);
/* We keep the active count elevated until relinquishment to prevent an
* attempt to wake up every time the object operations queue quiesces.
*/
atomic_set(&cookie->n_active, 1);
atomic_inc(&parent->usage); atomic_inc(&parent->usage);
atomic_inc(&parent->n_children); atomic_inc(&parent->n_children);
...@@ -177,7 +182,6 @@ static int fscache_acquire_non_index_cookie(struct fscache_cookie *cookie) ...@@ -177,7 +182,6 @@ static int fscache_acquire_non_index_cookie(struct fscache_cookie *cookie)
cookie->flags = cookie->flags =
(1 << FSCACHE_COOKIE_LOOKING_UP) | (1 << FSCACHE_COOKIE_LOOKING_UP) |
(1 << FSCACHE_COOKIE_CREATING) |
(1 << FSCACHE_COOKIE_NO_DATA_YET); (1 << FSCACHE_COOKIE_NO_DATA_YET);
/* ask the cache to allocate objects for this cookie and its parent /* ask the cache to allocate objects for this cookie and its parent
...@@ -205,7 +209,7 @@ static int fscache_acquire_non_index_cookie(struct fscache_cookie *cookie) ...@@ -205,7 +209,7 @@ static int fscache_acquire_non_index_cookie(struct fscache_cookie *cookie)
/* initiate the process of looking up all the objects in the chain /* initiate the process of looking up all the objects in the chain
* (done by fscache_initialise_object()) */ * (done by fscache_initialise_object()) */
fscache_enqueue_object(object); fscache_raise_event(object, FSCACHE_OBJECT_EV_NEW_CHILD);
spin_unlock(&cookie->lock); spin_unlock(&cookie->lock);
...@@ -285,7 +289,7 @@ static int fscache_alloc_object(struct fscache_cache *cache, ...@@ -285,7 +289,7 @@ static int fscache_alloc_object(struct fscache_cache *cache,
object_already_extant: object_already_extant:
ret = -ENOBUFS; ret = -ENOBUFS;
if (object->state >= FSCACHE_OBJECT_DYING) { if (fscache_object_is_dead(object)) {
spin_unlock(&cookie->lock); spin_unlock(&cookie->lock);
goto error; goto error;
} }
...@@ -321,7 +325,7 @@ static int fscache_attach_object(struct fscache_cookie *cookie, ...@@ -321,7 +325,7 @@ static int fscache_attach_object(struct fscache_cookie *cookie,
ret = -EEXIST; ret = -EEXIST;
hlist_for_each_entry(p, &cookie->backing_objects, cookie_link) { hlist_for_each_entry(p, &cookie->backing_objects, cookie_link) {
if (p->cache == object->cache) { if (p->cache == object->cache) {
if (p->state >= FSCACHE_OBJECT_DYING) if (fscache_object_is_dying(p))
ret = -ENOBUFS; ret = -ENOBUFS;
goto cant_attach_object; goto cant_attach_object;
} }
...@@ -332,7 +336,7 @@ static int fscache_attach_object(struct fscache_cookie *cookie, ...@@ -332,7 +336,7 @@ static int fscache_attach_object(struct fscache_cookie *cookie,
hlist_for_each_entry(p, &cookie->parent->backing_objects, hlist_for_each_entry(p, &cookie->parent->backing_objects,
cookie_link) { cookie_link) {
if (p->cache == object->cache) { if (p->cache == object->cache) {
if (p->state >= FSCACHE_OBJECT_DYING) { if (fscache_object_is_dying(p)) {
ret = -ENOBUFS; ret = -ENOBUFS;
spin_unlock(&cookie->parent->lock); spin_unlock(&cookie->parent->lock);
goto cant_attach_object; goto cant_attach_object;
...@@ -400,7 +404,7 @@ void __fscache_invalidate(struct fscache_cookie *cookie) ...@@ -400,7 +404,7 @@ void __fscache_invalidate(struct fscache_cookie *cookie)
object = hlist_entry(cookie->backing_objects.first, object = hlist_entry(cookie->backing_objects.first,
struct fscache_object, struct fscache_object,
cookie_link); cookie_link);
if (object->state < FSCACHE_OBJECT_DYING) if (fscache_object_is_live(object))
fscache_raise_event( fscache_raise_event(
object, FSCACHE_OBJECT_EV_INVALIDATE); object, FSCACHE_OBJECT_EV_INVALIDATE);
} }
...@@ -467,9 +471,7 @@ EXPORT_SYMBOL(__fscache_update_cookie); ...@@ -467,9 +471,7 @@ EXPORT_SYMBOL(__fscache_update_cookie);
*/ */
void __fscache_relinquish_cookie(struct fscache_cookie *cookie, int retire) void __fscache_relinquish_cookie(struct fscache_cookie *cookie, int retire)
{ {
struct fscache_cache *cache;
struct fscache_object *object; struct fscache_object *object;
unsigned long event;
fscache_stat(&fscache_n_relinquishes); fscache_stat(&fscache_n_relinquishes);
if (retire) if (retire)
...@@ -481,8 +483,11 @@ void __fscache_relinquish_cookie(struct fscache_cookie *cookie, int retire) ...@@ -481,8 +483,11 @@ void __fscache_relinquish_cookie(struct fscache_cookie *cookie, int retire)
return; return;
} }
_enter("%p{%s,%p},%d", _enter("%p{%s,%p,%d},%d",
cookie, cookie->def->name, cookie->netfs_data, retire); cookie, cookie->def->name, cookie->netfs_data,
atomic_read(&cookie->n_active), retire);
ASSERTCMP(atomic_read(&cookie->n_active), >, 0);
if (atomic_read(&cookie->n_children) != 0) { if (atomic_read(&cookie->n_children) != 0) {
printk(KERN_ERR "FS-Cache: Cookie '%s' still has children\n", printk(KERN_ERR "FS-Cache: Cookie '%s' still has children\n",
...@@ -490,62 +495,28 @@ void __fscache_relinquish_cookie(struct fscache_cookie *cookie, int retire) ...@@ -490,62 +495,28 @@ void __fscache_relinquish_cookie(struct fscache_cookie *cookie, int retire)
BUG(); BUG();
} }
/* wait for the cookie to finish being instantiated (or to fail) */ /* No further netfs-accessing operations on this cookie permitted */
if (test_bit(FSCACHE_COOKIE_CREATING, &cookie->flags)) { set_bit(FSCACHE_COOKIE_RELINQUISHED, &cookie->flags);
fscache_stat(&fscache_n_relinquishes_waitcrt); if (retire)
wait_on_bit(&cookie->flags, FSCACHE_COOKIE_CREATING, set_bit(FSCACHE_COOKIE_RETIRED, &cookie->flags);
fscache_wait_bit, TASK_UNINTERRUPTIBLE);
}
event = retire ? FSCACHE_OBJECT_EV_RETIRE : FSCACHE_OBJECT_EV_RELEASE;
try_again:
spin_lock(&cookie->lock); spin_lock(&cookie->lock);
hlist_for_each_entry(object, &cookie->backing_objects, cookie_link) {
/* break links with all the active objects */ fscache_raise_event(object, FSCACHE_OBJECT_EV_KILL);
while (!hlist_empty(&cookie->backing_objects)) {
int n_reads;
object = hlist_entry(cookie->backing_objects.first,
struct fscache_object,
cookie_link);
_debug("RELEASE OBJ%x", object->debug_id);
set_bit(FSCACHE_COOKIE_WAITING_ON_READS, &cookie->flags);
n_reads = atomic_read(&object->n_reads);
if (n_reads) {
int n_ops = object->n_ops;
int n_in_progress = object->n_in_progress;
spin_unlock(&cookie->lock);
printk(KERN_ERR "FS-Cache:"
" Cookie '%s' still has %d outstanding reads (%d,%d)\n",
cookie->def->name,
n_reads, n_ops, n_in_progress);
wait_on_bit(&cookie->flags, FSCACHE_COOKIE_WAITING_ON_READS,
fscache_wait_bit, TASK_UNINTERRUPTIBLE);
printk("Wait finished\n");
goto try_again;
}
/* detach each cache object from the object cookie */
spin_lock(&object->lock);
hlist_del_init(&object->cookie_link);
cache = object->cache;
object->cookie = NULL;
fscache_raise_event(object, event);
spin_unlock(&object->lock);
if (atomic_dec_and_test(&cookie->usage))
/* the cookie refcount shouldn't be reduced to 0 yet */
BUG();
} }
spin_unlock(&cookie->lock);
/* detach pointers back to the netfs */ /* Wait for cessation of activity requiring access to the netfs (when
* n_active reaches 0).
*/
if (!atomic_dec_and_test(&cookie->n_active))
wait_on_atomic_t(&cookie->n_active, fscache_wait_atomic_t,
TASK_UNINTERRUPTIBLE);
/* Clear pointers back to the netfs */
cookie->netfs_data = NULL; cookie->netfs_data = NULL;
cookie->def = NULL; cookie->def = NULL;
BUG_ON(cookie->stores.rnode);
spin_unlock(&cookie->lock);
if (cookie->parent) { if (cookie->parent) {
ASSERTCMP(atomic_read(&cookie->parent->usage), >, 0); ASSERTCMP(atomic_read(&cookie->parent->usage), >, 0);
...@@ -553,7 +524,7 @@ void __fscache_relinquish_cookie(struct fscache_cookie *cookie, int retire) ...@@ -553,7 +524,7 @@ void __fscache_relinquish_cookie(struct fscache_cookie *cookie, int retire)
atomic_dec(&cookie->parent->n_children); atomic_dec(&cookie->parent->n_children);
} }
/* finally dispose of the cookie */ /* Dispose of the netfs's link to the cookie */
ASSERTCMP(atomic_read(&cookie->usage), >, 0); ASSERTCMP(atomic_read(&cookie->usage), >, 0);
fscache_cookie_put(cookie); fscache_cookie_put(cookie);
......
...@@ -55,6 +55,7 @@ static struct fscache_cookie_def fscache_fsdef_index_def = { ...@@ -55,6 +55,7 @@ static struct fscache_cookie_def fscache_fsdef_index_def = {
struct fscache_cookie fscache_fsdef_index = { struct fscache_cookie fscache_fsdef_index = {
.usage = ATOMIC_INIT(1), .usage = ATOMIC_INIT(1),
.n_active = ATOMIC_INIT(1),
.lock = __SPIN_LOCK_UNLOCKED(fscache_fsdef_index.lock), .lock = __SPIN_LOCK_UNLOCKED(fscache_fsdef_index.lock),
.backing_objects = HLIST_HEAD_INIT, .backing_objects = HLIST_HEAD_INIT,
.def = &fscache_fsdef_index_def, .def = &fscache_fsdef_index_def,
......
...@@ -93,14 +93,11 @@ static inline bool fscache_object_congested(void) ...@@ -93,14 +93,11 @@ static inline bool fscache_object_congested(void)
extern int fscache_wait_bit(void *); extern int fscache_wait_bit(void *);
extern int fscache_wait_bit_interruptible(void *); extern int fscache_wait_bit_interruptible(void *);
extern int fscache_wait_atomic_t(atomic_t *);
/* /*
* object.c * object.c
*/ */
extern const char fscache_object_states_short[FSCACHE_OBJECT__NSTATES][5];
extern void fscache_withdrawing_object(struct fscache_cache *,
struct fscache_object *);
extern void fscache_enqueue_object(struct fscache_object *); extern void fscache_enqueue_object(struct fscache_object *);
/* /*
...@@ -110,8 +107,10 @@ extern void fscache_enqueue_object(struct fscache_object *); ...@@ -110,8 +107,10 @@ extern void fscache_enqueue_object(struct fscache_object *);
extern const struct file_operations fscache_objlist_fops; extern const struct file_operations fscache_objlist_fops;
extern void fscache_objlist_add(struct fscache_object *); extern void fscache_objlist_add(struct fscache_object *);
extern void fscache_objlist_remove(struct fscache_object *);
#else #else
#define fscache_objlist_add(object) do {} while(0) #define fscache_objlist_add(object) do {} while(0)
#define fscache_objlist_remove(object) do {} while(0)
#endif #endif
/* /*
...@@ -291,6 +290,10 @@ static inline void fscache_raise_event(struct fscache_object *object, ...@@ -291,6 +290,10 @@ static inline void fscache_raise_event(struct fscache_object *object,
unsigned event) unsigned event)
{ {
BUG_ON(event >= NR_FSCACHE_OBJECT_EVENTS); BUG_ON(event >= NR_FSCACHE_OBJECT_EVENTS);
#if 0
printk("*** fscache_raise_event(OBJ%d{%lx},%x)\n",
object->debug_id, object->event_mask, (1 << event));
#endif
if (!test_and_set_bit(event, &object->events) && if (!test_and_set_bit(event, &object->events) &&
test_bit(event, &object->event_mask)) test_bit(event, &object->event_mask))
fscache_enqueue_object(object); fscache_enqueue_object(object);
......
...@@ -205,7 +205,6 @@ int fscache_wait_bit(void *flags) ...@@ -205,7 +205,6 @@ int fscache_wait_bit(void *flags)
schedule(); schedule();
return 0; return 0;
} }
EXPORT_SYMBOL(fscache_wait_bit);
/* /*
* wait_on_bit() sleep function for interruptible waiting * wait_on_bit() sleep function for interruptible waiting
...@@ -215,4 +214,12 @@ int fscache_wait_bit_interruptible(void *flags) ...@@ -215,4 +214,12 @@ int fscache_wait_bit_interruptible(void *flags)
schedule(); schedule();
return signal_pending(current); return signal_pending(current);
} }
EXPORT_SYMBOL(fscache_wait_bit_interruptible);
/*
* wait_on_atomic_t() sleep function for uninterruptible waiting
*/
int fscache_wait_atomic_t(atomic_t *p)
{
schedule();
return 0;
}
...@@ -40,6 +40,7 @@ int __fscache_register_netfs(struct fscache_netfs *netfs) ...@@ -40,6 +40,7 @@ int __fscache_register_netfs(struct fscache_netfs *netfs)
/* initialise the primary index cookie */ /* initialise the primary index cookie */
atomic_set(&netfs->primary_index->usage, 1); atomic_set(&netfs->primary_index->usage, 1);
atomic_set(&netfs->primary_index->n_children, 0); atomic_set(&netfs->primary_index->n_children, 0);
atomic_set(&netfs->primary_index->n_active, 1);
netfs->primary_index->def = &fscache_fsdef_netfs_def; netfs->primary_index->def = &fscache_fsdef_netfs_def;
netfs->primary_index->parent = &fscache_fsdef_index; netfs->primary_index->parent = &fscache_fsdef_index;
......
...@@ -70,13 +70,10 @@ void fscache_objlist_add(struct fscache_object *obj) ...@@ -70,13 +70,10 @@ void fscache_objlist_add(struct fscache_object *obj)
write_unlock(&fscache_object_list_lock); write_unlock(&fscache_object_list_lock);
} }
/** /*
* fscache_object_destroy - Note that a cache object is about to be destroyed * Remove an object from the object list.
* @object: The object to be destroyed
*
* Note the imminent destruction and deallocation of a cache object record.
*/ */
void fscache_object_destroy(struct fscache_object *obj) void fscache_objlist_remove(struct fscache_object *obj)
{ {
write_lock(&fscache_object_list_lock); write_lock(&fscache_object_list_lock);
...@@ -85,7 +82,6 @@ void fscache_object_destroy(struct fscache_object *obj) ...@@ -85,7 +82,6 @@ void fscache_object_destroy(struct fscache_object *obj)
write_unlock(&fscache_object_list_lock); write_unlock(&fscache_object_list_lock);
} }
EXPORT_SYMBOL(fscache_object_destroy);
/* /*
* find the object in the tree on or after the specified index * find the object in the tree on or after the specified index
...@@ -166,15 +162,14 @@ static int fscache_objlist_show(struct seq_file *m, void *v) ...@@ -166,15 +162,14 @@ static int fscache_objlist_show(struct seq_file *m, void *v)
{ {
struct fscache_objlist_data *data = m->private; struct fscache_objlist_data *data = m->private;
struct fscache_object *obj = v; struct fscache_object *obj = v;
struct fscache_cookie *cookie;
unsigned long config = data->config; unsigned long config = data->config;
uint16_t keylen, auxlen;
char _type[3], *type; char _type[3], *type;
bool no_cookie;
u8 *buf = data->buf, *p; u8 *buf = data->buf, *p;
if ((unsigned long) v == 1) { if ((unsigned long) v == 1) {
seq_puts(m, "OBJECT PARENT STAT CHLDN OPS OOP IPR EX READS" seq_puts(m, "OBJECT PARENT STAT CHLDN OPS OOP IPR EX READS"
" EM EV F S" " EM EV FL S"
" | NETFS_COOKIE_DEF TY FL NETFS_DATA"); " | NETFS_COOKIE_DEF TY FL NETFS_DATA");
if (config & (FSCACHE_OBJLIST_CONFIG_KEY | if (config & (FSCACHE_OBJLIST_CONFIG_KEY |
FSCACHE_OBJLIST_CONFIG_AUX)) FSCACHE_OBJLIST_CONFIG_AUX))
...@@ -193,7 +188,7 @@ static int fscache_objlist_show(struct seq_file *m, void *v) ...@@ -193,7 +188,7 @@ static int fscache_objlist_show(struct seq_file *m, void *v)
if ((unsigned long) v == 2) { if ((unsigned long) v == 2) {
seq_puts(m, "======== ======== ==== ===== === === === == =====" seq_puts(m, "======== ======== ==== ===== === === === == ====="
" == == = =" " == == == ="
" | ================ == == ================"); " | ================ == == ================");
if (config & (FSCACHE_OBJLIST_CONFIG_KEY | if (config & (FSCACHE_OBJLIST_CONFIG_KEY |
FSCACHE_OBJLIST_CONFIG_AUX)) FSCACHE_OBJLIST_CONFIG_AUX))
...@@ -216,10 +211,11 @@ static int fscache_objlist_show(struct seq_file *m, void *v) ...@@ -216,10 +211,11 @@ static int fscache_objlist_show(struct seq_file *m, void *v)
} \ } \
} while(0) } while(0)
cookie = obj->cookie;
if (~config) { if (~config) {
FILTER(obj->cookie, FILTER(cookie->def,
COOKIE, NOCOOKIE); COOKIE, NOCOOKIE);
FILTER(obj->state != FSCACHE_OBJECT_ACTIVE || FILTER(fscache_object_is_active(obj) ||
obj->n_ops != 0 || obj->n_ops != 0 ||
obj->n_obj_ops != 0 || obj->n_obj_ops != 0 ||
obj->flags || obj->flags ||
...@@ -235,10 +231,10 @@ static int fscache_objlist_show(struct seq_file *m, void *v) ...@@ -235,10 +231,10 @@ static int fscache_objlist_show(struct seq_file *m, void *v)
} }
seq_printf(m, seq_printf(m,
"%8x %8x %s %5u %3u %3u %3u %2u %5u %2lx %2lx %1lx %1x | ", "%8x %8x %s %5u %3u %3u %3u %2u %5u %2lx %2lx %2lx %1x | ",
obj->debug_id, obj->debug_id,
obj->parent ? obj->parent->debug_id : -1, obj->parent ? obj->parent->debug_id : -1,
fscache_object_states_short[obj->state], obj->state->short_name,
obj->n_children, obj->n_children,
obj->n_ops, obj->n_ops,
obj->n_obj_ops, obj->n_obj_ops,
...@@ -250,48 +246,40 @@ static int fscache_objlist_show(struct seq_file *m, void *v) ...@@ -250,48 +246,40 @@ static int fscache_objlist_show(struct seq_file *m, void *v)
obj->flags, obj->flags,
work_busy(&obj->work)); work_busy(&obj->work));
no_cookie = true; if (fscache_use_cookie(obj)) {
keylen = auxlen = 0; uint16_t keylen = 0, auxlen = 0;
if (obj->cookie) {
spin_lock(&obj->lock);
if (obj->cookie) {
switch (obj->cookie->def->type) {
case 0:
type = "IX";
break;
case 1:
type = "DT";
break;
default:
sprintf(_type, "%02u",
obj->cookie->def->type);
type = _type;
break;
}
seq_printf(m, "%-16s %s %2lx %16p", switch (cookie->def->type) {
obj->cookie->def->name, case 0:
type, type = "IX";
obj->cookie->flags, break;
obj->cookie->netfs_data); case 1:
type = "DT";
if (obj->cookie->def->get_key && break;
config & FSCACHE_OBJLIST_CONFIG_KEY) default:
keylen = obj->cookie->def->get_key( sprintf(_type, "%02u", cookie->def->type);
obj->cookie->netfs_data, type = _type;
buf, 400); break;
if (obj->cookie->def->get_aux &&
config & FSCACHE_OBJLIST_CONFIG_AUX)
auxlen = obj->cookie->def->get_aux(
obj->cookie->netfs_data,
buf + keylen, 512 - keylen);
no_cookie = false;
} }
spin_unlock(&obj->lock);
if (!no_cookie && (keylen > 0 || auxlen > 0)) { seq_printf(m, "%-16s %s %2lx %16p",
cookie->def->name,
type,
cookie->flags,
cookie->netfs_data);
if (cookie->def->get_key &&
config & FSCACHE_OBJLIST_CONFIG_KEY)
keylen = cookie->def->get_key(cookie->netfs_data,
buf, 400);
if (cookie->def->get_aux &&
config & FSCACHE_OBJLIST_CONFIG_AUX)
auxlen = cookie->def->get_aux(cookie->netfs_data,
buf + keylen, 512 - keylen);
fscache_unuse_cookie(obj);
if (keylen > 0 || auxlen > 0) {
seq_printf(m, " "); seq_printf(m, " ");
for (p = buf; keylen > 0; keylen--) for (p = buf; keylen > 0; keylen--)
seq_printf(m, "%02x", *p++); seq_printf(m, "%02x", *p++);
...@@ -302,12 +290,11 @@ static int fscache_objlist_show(struct seq_file *m, void *v) ...@@ -302,12 +290,11 @@ static int fscache_objlist_show(struct seq_file *m, void *v)
seq_printf(m, "%02x", *p++); seq_printf(m, "%02x", *p++);
} }
} }
}
if (no_cookie)
seq_printf(m, "<no_cookie>\n");
else
seq_printf(m, "\n"); seq_printf(m, "\n");
} else {
seq_printf(m, "<no_netfs>\n");
}
return 0; return 0;
} }
......
...@@ -15,52 +15,131 @@ ...@@ -15,52 +15,131 @@
#define FSCACHE_DEBUG_LEVEL COOKIE #define FSCACHE_DEBUG_LEVEL COOKIE
#include <linux/module.h> #include <linux/module.h>
#include <linux/slab.h> #include <linux/slab.h>
#include <linux/prefetch.h>
#include "internal.h" #include "internal.h"
const char *fscache_object_states[FSCACHE_OBJECT__NSTATES] = { static const struct fscache_state *fscache_abort_initialisation(struct fscache_object *, int);
[FSCACHE_OBJECT_INIT] = "OBJECT_INIT", static const struct fscache_state *fscache_kill_dependents(struct fscache_object *, int);
[FSCACHE_OBJECT_LOOKING_UP] = "OBJECT_LOOKING_UP", static const struct fscache_state *fscache_drop_object(struct fscache_object *, int);
[FSCACHE_OBJECT_CREATING] = "OBJECT_CREATING", static const struct fscache_state *fscache_initialise_object(struct fscache_object *, int);
[FSCACHE_OBJECT_AVAILABLE] = "OBJECT_AVAILABLE", static const struct fscache_state *fscache_invalidate_object(struct fscache_object *, int);
[FSCACHE_OBJECT_ACTIVE] = "OBJECT_ACTIVE", static const struct fscache_state *fscache_jumpstart_dependents(struct fscache_object *, int);
[FSCACHE_OBJECT_INVALIDATING] = "OBJECT_INVALIDATING", static const struct fscache_state *fscache_kill_object(struct fscache_object *, int);
[FSCACHE_OBJECT_UPDATING] = "OBJECT_UPDATING", static const struct fscache_state *fscache_lookup_failure(struct fscache_object *, int);
[FSCACHE_OBJECT_DYING] = "OBJECT_DYING", static const struct fscache_state *fscache_look_up_object(struct fscache_object *, int);
[FSCACHE_OBJECT_LC_DYING] = "OBJECT_LC_DYING", static const struct fscache_state *fscache_object_available(struct fscache_object *, int);
[FSCACHE_OBJECT_ABORT_INIT] = "OBJECT_ABORT_INIT", static const struct fscache_state *fscache_parent_ready(struct fscache_object *, int);
[FSCACHE_OBJECT_RELEASING] = "OBJECT_RELEASING", static const struct fscache_state *fscache_update_object(struct fscache_object *, int);
[FSCACHE_OBJECT_RECYCLING] = "OBJECT_RECYCLING",
[FSCACHE_OBJECT_WITHDRAWING] = "OBJECT_WITHDRAWING", #define __STATE_NAME(n) fscache_osm_##n
[FSCACHE_OBJECT_DEAD] = "OBJECT_DEAD", #define STATE(n) (&__STATE_NAME(n))
/*
* Define a work state. Work states are execution states. No event processing
* is performed by them. The function attached to a work state returns a
* pointer indicating the next state to which the state machine should
* transition. Returning NO_TRANSIT repeats the current state, but goes back
* to the scheduler first.
*/
#define WORK_STATE(n, sn, f) \
const struct fscache_state __STATE_NAME(n) = { \
.name = #n, \
.short_name = sn, \
.work = f \
}
/*
* Returns from work states.
*/
#define transit_to(state) ({ prefetch(&STATE(state)->work); STATE(state); })
#define NO_TRANSIT ((struct fscache_state *)NULL)
/*
* Define a wait state. Wait states are event processing states. No execution
* is performed by them. Wait states are just tables of "if event X occurs,
* clear it and transition to state Y". The dispatcher returns to the
* scheduler if none of the events in which the wait state has an interest are
* currently pending.
*/
#define WAIT_STATE(n, sn, ...) \
const struct fscache_state __STATE_NAME(n) = { \
.name = #n, \
.short_name = sn, \
.work = NULL, \
.transitions = { __VA_ARGS__, { 0, NULL } } \
}
#define TRANSIT_TO(state, emask) \
{ .events = (emask), .transit_to = STATE(state) }
/*
* The object state machine.
*/
static WORK_STATE(INIT_OBJECT, "INIT", fscache_initialise_object);
static WORK_STATE(PARENT_READY, "PRDY", fscache_parent_ready);
static WORK_STATE(ABORT_INIT, "ABRT", fscache_abort_initialisation);
static WORK_STATE(LOOK_UP_OBJECT, "LOOK", fscache_look_up_object);
static WORK_STATE(CREATE_OBJECT, "CRTO", fscache_look_up_object);
static WORK_STATE(OBJECT_AVAILABLE, "AVBL", fscache_object_available);
static WORK_STATE(JUMPSTART_DEPS, "JUMP", fscache_jumpstart_dependents);
static WORK_STATE(INVALIDATE_OBJECT, "INVL", fscache_invalidate_object);
static WORK_STATE(UPDATE_OBJECT, "UPDT", fscache_update_object);
static WORK_STATE(LOOKUP_FAILURE, "LCFL", fscache_lookup_failure);
static WORK_STATE(KILL_OBJECT, "KILL", fscache_kill_object);
static WORK_STATE(KILL_DEPENDENTS, "KDEP", fscache_kill_dependents);
static WORK_STATE(DROP_OBJECT, "DROP", fscache_drop_object);
static WORK_STATE(OBJECT_DEAD, "DEAD", (void*)2UL);
static WAIT_STATE(WAIT_FOR_INIT, "?INI",
TRANSIT_TO(INIT_OBJECT, 1 << FSCACHE_OBJECT_EV_NEW_CHILD));
static WAIT_STATE(WAIT_FOR_PARENT, "?PRN",
TRANSIT_TO(PARENT_READY, 1 << FSCACHE_OBJECT_EV_PARENT_READY));
static WAIT_STATE(WAIT_FOR_CMD, "?CMD",
TRANSIT_TO(INVALIDATE_OBJECT, 1 << FSCACHE_OBJECT_EV_INVALIDATE),
TRANSIT_TO(UPDATE_OBJECT, 1 << FSCACHE_OBJECT_EV_UPDATE),
TRANSIT_TO(JUMPSTART_DEPS, 1 << FSCACHE_OBJECT_EV_NEW_CHILD));
static WAIT_STATE(WAIT_FOR_CLEARANCE, "?CLR",
TRANSIT_TO(KILL_OBJECT, 1 << FSCACHE_OBJECT_EV_CLEARED));
/*
* Out-of-band event transition tables. These are for handling unexpected
* events, such as an I/O error. If an OOB event occurs, the state machine
* clears and disables the event and forces a transition to the nominated work
* state (acurrently executing work states will complete first).
*
* In such a situation, object->state remembers the state the machine should
* have been in/gone to and returning NO_TRANSIT returns to that.
*/
static const struct fscache_transition fscache_osm_init_oob[] = {
TRANSIT_TO(ABORT_INIT,
(1 << FSCACHE_OBJECT_EV_ERROR) |
(1 << FSCACHE_OBJECT_EV_KILL)),
{ 0, NULL }
};
static const struct fscache_transition fscache_osm_lookup_oob[] = {
TRANSIT_TO(LOOKUP_FAILURE,
(1 << FSCACHE_OBJECT_EV_ERROR) |
(1 << FSCACHE_OBJECT_EV_KILL)),
{ 0, NULL }
}; };
EXPORT_SYMBOL(fscache_object_states);
static const struct fscache_transition fscache_osm_run_oob[] = {
const char fscache_object_states_short[FSCACHE_OBJECT__NSTATES][5] = { TRANSIT_TO(KILL_OBJECT,
[FSCACHE_OBJECT_INIT] = "INIT", (1 << FSCACHE_OBJECT_EV_ERROR) |
[FSCACHE_OBJECT_LOOKING_UP] = "LOOK", (1 << FSCACHE_OBJECT_EV_KILL)),
[FSCACHE_OBJECT_CREATING] = "CRTN", { 0, NULL }
[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",
[FSCACHE_OBJECT_ABORT_INIT] = "ABTI",
[FSCACHE_OBJECT_RELEASING] = "RELS",
[FSCACHE_OBJECT_RECYCLING] = "RCYC",
[FSCACHE_OBJECT_WITHDRAWING] = "WTHD",
[FSCACHE_OBJECT_DEAD] = "DEAD",
}; };
static int fscache_get_object(struct fscache_object *); static int fscache_get_object(struct fscache_object *);
static void fscache_put_object(struct fscache_object *); static void fscache_put_object(struct fscache_object *);
static void fscache_initialise_object(struct fscache_object *); static bool fscache_enqueue_dependents(struct fscache_object *, int);
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 *);
static void fscache_dequeue_object(struct fscache_object *); static void fscache_dequeue_object(struct fscache_object *);
/* /*
...@@ -75,295 +154,116 @@ static inline void fscache_done_parent_op(struct fscache_object *object) ...@@ -75,295 +154,116 @@ static inline void fscache_done_parent_op(struct fscache_object *object)
object->debug_id, parent->debug_id, parent->n_ops); object->debug_id, parent->debug_id, parent->n_ops);
spin_lock_nested(&parent->lock, 1); spin_lock_nested(&parent->lock, 1);
parent->n_ops--;
parent->n_obj_ops--; parent->n_obj_ops--;
parent->n_ops--;
if (parent->n_ops == 0) if (parent->n_ops == 0)
fscache_raise_event(parent, FSCACHE_OBJECT_EV_CLEARED); fscache_raise_event(parent, FSCACHE_OBJECT_EV_CLEARED);
spin_unlock(&parent->lock); spin_unlock(&parent->lock);
} }
/* /*
* Notify netfs of invalidation completion. * Object state machine dispatcher.
*/ */
static inline void fscache_invalidation_complete(struct fscache_cookie *cookie) static void fscache_object_sm_dispatcher(struct fscache_object *object)
{ {
if (test_and_clear_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags)) const struct fscache_transition *t;
wake_up_bit(&cookie->flags, FSCACHE_COOKIE_INVALIDATING); const struct fscache_state *state, *new_state;
} unsigned long events, event_mask;
int event = -1;
/*
* process events that have been sent to an object's state machine
* - initiates parent lookup
* - does object lookup
* - does object creation
* - does object recycling and retirement
* - does object withdrawal
*/
static void fscache_object_state_machine(struct fscache_object *object)
{
enum fscache_object_state new_state;
struct fscache_cookie *cookie;
int event;
ASSERT(object != NULL); ASSERT(object != NULL);
_enter("{OBJ%x,%s,%lx}", _enter("{OBJ%x,%s,%lx}",
object->debug_id, fscache_object_states[object->state], object->debug_id, object->state->name, object->events);
object->events);
event_mask = object->event_mask;
switch (object->state) { restart:
/* wait for the parent object to become ready */ object->event_mask = 0; /* Mask normal event handling */
case FSCACHE_OBJECT_INIT: state = object->state;
object->event_mask = restart_masked:
FSCACHE_OBJECT_EVENTS_MASK & events = object->events;
~(1 << FSCACHE_OBJECT_EV_CLEARED);
fscache_initialise_object(object); /* Handle any out-of-band events (typically an error) */
goto done; if (events & object->oob_event_mask) {
_debug("{OBJ%x} oob %lx",
/* look up the object metadata on disk */ object->debug_id, events & object->oob_event_mask);
case FSCACHE_OBJECT_LOOKING_UP: for (t = object->oob_table; t->events; t++) {
fscache_lookup_object(object); if (events & t->events) {
goto lookup_transit; state = t->transit_to;
ASSERT(state->work != NULL);
/* create the object metadata on disk */ event = fls(events & t->events) - 1;
case FSCACHE_OBJECT_CREATING: __clear_bit(event, &object->oob_event_mask);
fscache_lookup_object(object); clear_bit(event, &object->events);
goto lookup_transit; goto execute_work_state;
}
/* handle an object becoming available; start pending
* operations and queue dependent operations for processing */
case FSCACHE_OBJECT_AVAILABLE:
fscache_object_available(object);
goto active_transit;
/* normal running state */
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);
fscache_stat(&fscache_n_updates_run);
fscache_stat(&fscache_n_cop_update_object);
object->cache->ops->update_object(object);
fscache_stat_d(&fscache_n_cop_update_object);
goto active_transit;
/* handle an object dying during lookup or creation */
case FSCACHE_OBJECT_LC_DYING:
object->event_mask &= ~(1 << FSCACHE_OBJECT_EV_UPDATE);
fscache_stat(&fscache_n_cop_lookup_complete);
object->cache->ops->lookup_complete(object);
fscache_stat_d(&fscache_n_cop_lookup_complete);
spin_lock(&object->lock);
object->state = FSCACHE_OBJECT_DYING;
cookie = object->cookie;
if (cookie) {
if (test_and_clear_bit(FSCACHE_COOKIE_LOOKING_UP,
&cookie->flags))
wake_up_bit(&cookie->flags,
FSCACHE_COOKIE_LOOKING_UP);
if (test_and_clear_bit(FSCACHE_COOKIE_CREATING,
&cookie->flags))
wake_up_bit(&cookie->flags,
FSCACHE_COOKIE_CREATING);
} }
spin_unlock(&object->lock); }
fscache_done_parent_op(object); /* Wait states are just transition tables */
if (!state->work) {
if (events & event_mask) {
for (t = state->transitions; t->events; t++) {
if (events & t->events) {
new_state = t->transit_to;
event = fls(events & t->events) - 1;
clear_bit(event, &object->events);
_debug("{OBJ%x} ev %d: %s -> %s",
object->debug_id, event,
state->name, new_state->name);
object->state = state = new_state;
goto execute_work_state;
}
}
/* wait for completion of all active operations on this object /* The event mask didn't include all the tabled bits */
* and the death of all child objects of this object */ BUG();
case FSCACHE_OBJECT_DYING:
dying:
clear_bit(FSCACHE_OBJECT_EV_CLEARED, &object->events);
spin_lock(&object->lock);
_debug("dying OBJ%x {%d,%d}",
object->debug_id, object->n_ops, object->n_children);
if (object->n_ops == 0 && object->n_children == 0) {
object->event_mask &=
~(1 << FSCACHE_OBJECT_EV_CLEARED);
object->event_mask |=
(1 << FSCACHE_OBJECT_EV_WITHDRAW) |
(1 << FSCACHE_OBJECT_EV_RETIRE) |
(1 << FSCACHE_OBJECT_EV_RELEASE) |
(1 << FSCACHE_OBJECT_EV_ERROR);
} else {
object->event_mask &=
~((1 << FSCACHE_OBJECT_EV_WITHDRAW) |
(1 << FSCACHE_OBJECT_EV_RETIRE) |
(1 << FSCACHE_OBJECT_EV_RELEASE) |
(1 << FSCACHE_OBJECT_EV_ERROR));
object->event_mask |=
1 << FSCACHE_OBJECT_EV_CLEARED;
} }
spin_unlock(&object->lock); /* Randomly woke up */
fscache_enqueue_dependents(object); goto unmask_events;
fscache_start_operations(object);
goto terminal_transit;
/* handle an abort during initialisation */
case FSCACHE_OBJECT_ABORT_INIT:
_debug("handle abort init %lx", object->events);
object->event_mask &= ~(1 << FSCACHE_OBJECT_EV_UPDATE);
spin_lock(&object->lock);
fscache_dequeue_object(object);
object->state = FSCACHE_OBJECT_DYING;
if (test_and_clear_bit(FSCACHE_COOKIE_CREATING,
&object->cookie->flags))
wake_up_bit(&object->cookie->flags,
FSCACHE_COOKIE_CREATING);
spin_unlock(&object->lock);
goto dying;
/* handle the netfs releasing an object and possibly marking it
* obsolete too */
case FSCACHE_OBJECT_RELEASING:
case FSCACHE_OBJECT_RECYCLING:
object->event_mask &=
~((1 << FSCACHE_OBJECT_EV_WITHDRAW) |
(1 << FSCACHE_OBJECT_EV_RETIRE) |
(1 << FSCACHE_OBJECT_EV_RELEASE) |
(1 << FSCACHE_OBJECT_EV_ERROR));
fscache_release_object(object);
spin_lock(&object->lock);
object->state = FSCACHE_OBJECT_DEAD;
spin_unlock(&object->lock);
fscache_stat(&fscache_n_object_dead);
goto terminal_transit;
/* handle the parent cache of this object being withdrawn from
* active service */
case FSCACHE_OBJECT_WITHDRAWING:
object->event_mask &=
~((1 << FSCACHE_OBJECT_EV_WITHDRAW) |
(1 << FSCACHE_OBJECT_EV_RETIRE) |
(1 << FSCACHE_OBJECT_EV_RELEASE) |
(1 << FSCACHE_OBJECT_EV_ERROR));
fscache_withdraw_object(object);
spin_lock(&object->lock);
object->state = FSCACHE_OBJECT_DEAD;
spin_unlock(&object->lock);
fscache_stat(&fscache_n_object_dead);
goto terminal_transit;
/* complain about the object being woken up once it is
* deceased */
case FSCACHE_OBJECT_DEAD:
printk(KERN_ERR "FS-Cache:"
" Unexpected event in dead state %lx\n",
object->events & object->event_mask);
BUG();
default:
printk(KERN_ERR "FS-Cache: Unknown object state %u\n",
object->state);
BUG();
}
/* determine the transition from a lookup state */
lookup_transit:
event = fls(object->events & object->event_mask) - 1;
switch (event) {
case FSCACHE_OBJECT_EV_WITHDRAW:
case FSCACHE_OBJECT_EV_RETIRE:
case FSCACHE_OBJECT_EV_RELEASE:
case FSCACHE_OBJECT_EV_ERROR:
new_state = FSCACHE_OBJECT_LC_DYING;
goto change_state;
case FSCACHE_OBJECT_EV_INVALIDATE:
new_state = FSCACHE_OBJECT_INVALIDATING;
goto change_state;
case FSCACHE_OBJECT_EV_REQUEUE:
goto done;
case -1:
goto done; /* sleep until event */
default:
goto unsupported_event;
} }
/* determine the transition from an active state */ execute_work_state:
active_transit: _debug("{OBJ%x} exec %s", object->debug_id, state->name);
event = fls(object->events & object->event_mask) - 1;
switch (event) {
case FSCACHE_OBJECT_EV_WITHDRAW:
case FSCACHE_OBJECT_EV_RETIRE:
case FSCACHE_OBJECT_EV_RELEASE:
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;
case -1:
new_state = FSCACHE_OBJECT_ACTIVE;
goto change_state; /* sleep until event */
default:
goto unsupported_event;
}
/* determine the transition from a terminal state */ new_state = state->work(object, event);
terminal_transit: event = -1;
event = fls(object->events & object->event_mask) - 1; if (new_state == NO_TRANSIT) {
switch (event) { _debug("{OBJ%x} %s notrans", object->debug_id, state->name);
case FSCACHE_OBJECT_EV_WITHDRAW: fscache_enqueue_object(object);
new_state = FSCACHE_OBJECT_WITHDRAWING; event_mask = object->oob_event_mask;
goto change_state; goto unmask_events;
case FSCACHE_OBJECT_EV_RETIRE:
new_state = FSCACHE_OBJECT_RECYCLING;
goto change_state;
case FSCACHE_OBJECT_EV_RELEASE:
new_state = FSCACHE_OBJECT_RELEASING;
goto change_state;
case FSCACHE_OBJECT_EV_ERROR:
new_state = FSCACHE_OBJECT_WITHDRAWING;
goto change_state;
case FSCACHE_OBJECT_EV_CLEARED:
new_state = FSCACHE_OBJECT_DYING;
goto change_state;
case -1:
goto done; /* sleep until event */
default:
goto unsupported_event;
} }
change_state: _debug("{OBJ%x} %s -> %s",
spin_lock(&object->lock); object->debug_id, state->name, new_state->name);
object->state = new_state; object->state = state = new_state;
spin_unlock(&object->lock);
done: if (state->work) {
_leave(" [->%s]", fscache_object_states[object->state]); if (unlikely(state->work == ((void *)2UL))) {
return; _leave(" [dead]");
return;
}
goto restart_masked;
}
unsupported_event: /* Transited to wait state */
printk(KERN_ERR "FS-Cache:" event_mask = object->oob_event_mask;
" Unsupported event %d [%lx/%lx] in state %s\n", for (t = state->transitions; t->events; t++)
event, object->events, object->event_mask, event_mask |= t->events;
fscache_object_states[object->state]);
BUG(); unmask_events:
object->event_mask = event_mask;
smp_mb();
events = object->events;
if (events & event_mask)
goto restart;
_leave(" [msk %lx]", event_mask);
} }
/* /*
* execute an object * execute an object
*/ */
void fscache_object_work_func(struct work_struct *work) static void fscache_object_work_func(struct work_struct *work)
{ {
struct fscache_object *object = struct fscache_object *object =
container_of(work, struct fscache_object, work); container_of(work, struct fscache_object, work);
...@@ -372,14 +272,70 @@ void fscache_object_work_func(struct work_struct *work) ...@@ -372,14 +272,70 @@ void fscache_object_work_func(struct work_struct *work)
_enter("{OBJ%x}", object->debug_id); _enter("{OBJ%x}", object->debug_id);
start = jiffies; start = jiffies;
fscache_object_state_machine(object); fscache_object_sm_dispatcher(object);
fscache_hist(fscache_objs_histogram, start); fscache_hist(fscache_objs_histogram, start);
if (object->events & object->event_mask)
fscache_enqueue_object(object);
clear_bit(FSCACHE_OBJECT_EV_REQUEUE, &object->events);
fscache_put_object(object); fscache_put_object(object);
} }
EXPORT_SYMBOL(fscache_object_work_func);
/**
* fscache_object_init - Initialise a cache object description
* @object: Object description
* @cookie: Cookie object will be attached to
* @cache: Cache in which backing object will be found
*
* Initialise a cache object description to its basic values.
*
* See Documentation/filesystems/caching/backend-api.txt for a complete
* description.
*/
void fscache_object_init(struct fscache_object *object,
struct fscache_cookie *cookie,
struct fscache_cache *cache)
{
const struct fscache_transition *t;
atomic_inc(&cache->object_count);
object->state = STATE(WAIT_FOR_INIT);
object->oob_table = fscache_osm_init_oob;
object->flags = 1 << FSCACHE_OBJECT_IS_LIVE;
spin_lock_init(&object->lock);
INIT_LIST_HEAD(&object->cache_link);
INIT_HLIST_NODE(&object->cookie_link);
INIT_WORK(&object->work, fscache_object_work_func);
INIT_LIST_HEAD(&object->dependents);
INIT_LIST_HEAD(&object->dep_link);
INIT_LIST_HEAD(&object->pending_ops);
object->n_children = 0;
object->n_ops = object->n_in_progress = object->n_exclusive = 0;
object->events = 0;
object->store_limit = 0;
object->store_limit_l = 0;
object->cache = cache;
object->cookie = cookie;
object->parent = NULL;
object->oob_event_mask = 0;
for (t = object->oob_table; t->events; t++)
object->oob_event_mask |= t->events;
object->event_mask = object->oob_event_mask;
for (t = object->state->transitions; t->events; t++)
object->event_mask |= t->events;
}
EXPORT_SYMBOL(fscache_object_init);
/*
* Abort object initialisation before we start it.
*/
static const struct fscache_state *fscache_abort_initialisation(struct fscache_object *object,
int event)
{
_enter("{OBJ%x},%d", object->debug_id, event);
object->oob_event_mask = 0;
fscache_dequeue_object(object);
return transit_to(KILL_OBJECT);
}
/* /*
* initialise an object * initialise an object
...@@ -387,130 +343,136 @@ EXPORT_SYMBOL(fscache_object_work_func); ...@@ -387,130 +343,136 @@ EXPORT_SYMBOL(fscache_object_work_func);
* immediately to do a creation * immediately to do a creation
* - we may need to start the process of creating a parent and we need to wait * - we may need to start the process of creating a parent and we need to wait
* for the parent's lookup and creation to complete if it's not there yet * for the parent's lookup and creation to complete if it's not there yet
* - an object's cookie is pinned until we clear FSCACHE_COOKIE_CREATING on the
* leaf-most cookies of the object and all its children
*/ */
static void fscache_initialise_object(struct fscache_object *object) static const struct fscache_state *fscache_initialise_object(struct fscache_object *object,
int event)
{ {
struct fscache_object *parent; struct fscache_object *parent;
bool success;
_enter(""); _enter("{OBJ%x},%d", object->debug_id, event);
ASSERT(object->cookie != NULL);
ASSERT(object->cookie->parent != NULL);
if (object->events & ((1 << FSCACHE_OBJECT_EV_ERROR) |
(1 << FSCACHE_OBJECT_EV_RELEASE) |
(1 << FSCACHE_OBJECT_EV_RETIRE) |
(1 << FSCACHE_OBJECT_EV_WITHDRAW))) {
_debug("abort init %lx", object->events);
spin_lock(&object->lock);
object->state = FSCACHE_OBJECT_ABORT_INIT;
spin_unlock(&object->lock);
return;
}
spin_lock(&object->cookie->lock); ASSERT(list_empty(&object->dep_link));
spin_lock_nested(&object->cookie->parent->lock, 1);
parent = object->parent; parent = object->parent;
if (!parent) { if (!parent) {
_debug("no parent"); _leave(" [no parent]");
set_bit(FSCACHE_OBJECT_EV_WITHDRAW, &object->events); return transit_to(DROP_OBJECT);
} else { }
spin_lock(&object->lock);
spin_lock_nested(&parent->lock, 1);
_debug("parent %s", fscache_object_states[parent->state]);
if (parent->state >= FSCACHE_OBJECT_DYING) {
_debug("bad parent");
set_bit(FSCACHE_OBJECT_EV_WITHDRAW, &object->events);
} else if (parent->state < FSCACHE_OBJECT_AVAILABLE) {
_debug("wait");
/* we may get woken up in this state by child objects
* binding on to us, so we need to make sure we don't
* add ourself to the list multiple times */
if (list_empty(&object->dep_link)) {
fscache_stat(&fscache_n_cop_grab_object);
object->cache->ops->grab_object(object);
fscache_stat_d(&fscache_n_cop_grab_object);
list_add(&object->dep_link,
&parent->dependents);
/* fscache_acquire_non_index_cookie() uses this
* to wake the chain up */
if (parent->state == FSCACHE_OBJECT_INIT)
fscache_enqueue_object(parent);
}
} else {
_debug("go");
parent->n_ops++;
parent->n_obj_ops++;
object->lookup_jif = jiffies;
object->state = FSCACHE_OBJECT_LOOKING_UP;
set_bit(FSCACHE_OBJECT_EV_REQUEUE, &object->events);
}
spin_unlock(&parent->lock); _debug("parent: %s of:%lx", parent->state->name, parent->flags);
spin_unlock(&object->lock);
if (fscache_object_is_dying(parent)) {
_leave(" [bad parent]");
return transit_to(DROP_OBJECT);
} }
spin_unlock(&object->cookie->parent->lock); if (fscache_object_is_available(parent)) {
spin_unlock(&object->cookie->lock); _leave(" [ready]");
return transit_to(PARENT_READY);
}
_debug("wait");
spin_lock(&parent->lock);
fscache_stat(&fscache_n_cop_grab_object);
success = false;
if (fscache_object_is_live(parent) &&
object->cache->ops->grab_object(object)) {
list_add(&object->dep_link, &parent->dependents);
success = true;
}
fscache_stat_d(&fscache_n_cop_grab_object);
spin_unlock(&parent->lock);
if (!success) {
_leave(" [grab failed]");
return transit_to(DROP_OBJECT);
}
/* fscache_acquire_non_index_cookie() uses this
* to wake the chain up */
fscache_raise_event(parent, FSCACHE_OBJECT_EV_NEW_CHILD);
_leave(" [wait]");
return transit_to(WAIT_FOR_PARENT);
}
/*
* Once the parent object is ready, we should kick off our lookup op.
*/
static const struct fscache_state *fscache_parent_ready(struct fscache_object *object,
int event)
{
struct fscache_object *parent = object->parent;
_enter("{OBJ%x},%d", object->debug_id, event);
ASSERT(parent != NULL);
spin_lock(&parent->lock);
parent->n_ops++;
parent->n_obj_ops++;
object->lookup_jif = jiffies;
spin_unlock(&parent->lock);
_leave(""); _leave("");
return transit_to(LOOK_UP_OBJECT);
} }
/* /*
* look an object up in the cache from which it was allocated * look an object up in the cache from which it was allocated
* - we hold an "access lock" on the parent object, so the parent object cannot * - we hold an "access lock" on the parent object, so the parent object cannot
* be withdrawn by either party till we've finished * be withdrawn by either party till we've finished
* - an object's cookie is pinned until we clear FSCACHE_COOKIE_CREATING on the
* leaf-most cookies of the object and all its children
*/ */
static void fscache_lookup_object(struct fscache_object *object) static const struct fscache_state *fscache_look_up_object(struct fscache_object *object,
int event)
{ {
struct fscache_cookie *cookie = object->cookie; struct fscache_cookie *cookie = object->cookie;
struct fscache_object *parent; struct fscache_object *parent = object->parent;
int ret; int ret;
_enter(""); _enter("{OBJ%x},%d", object->debug_id, event);
object->oob_table = fscache_osm_lookup_oob;
parent = object->parent;
ASSERT(parent != NULL); ASSERT(parent != NULL);
ASSERTCMP(parent->n_ops, >, 0); ASSERTCMP(parent->n_ops, >, 0);
ASSERTCMP(parent->n_obj_ops, >, 0); ASSERTCMP(parent->n_obj_ops, >, 0);
/* make sure the parent is still available */ /* make sure the parent is still available */
ASSERTCMP(parent->state, >=, FSCACHE_OBJECT_AVAILABLE); ASSERT(fscache_object_is_available(parent));
if (parent->state >= FSCACHE_OBJECT_DYING || if (fscache_object_is_dying(parent) ||
test_bit(FSCACHE_IOERROR, &object->cache->flags)) { test_bit(FSCACHE_IOERROR, &object->cache->flags) ||
_debug("unavailable"); !fscache_use_cookie(object)) {
set_bit(FSCACHE_OBJECT_EV_WITHDRAW, &object->events); _leave(" [unavailable]");
_leave(""); return transit_to(LOOKUP_FAILURE);
return;
} }
_debug("LOOKUP \"%s/%s\" in \"%s\"", _debug("LOOKUP \"%s\" in \"%s\"",
parent->cookie->def->name, cookie->def->name, cookie->def->name, object->cache->tag->name);
object->cache->tag->name);
fscache_stat(&fscache_n_object_lookups); fscache_stat(&fscache_n_object_lookups);
fscache_stat(&fscache_n_cop_lookup_object); fscache_stat(&fscache_n_cop_lookup_object);
ret = object->cache->ops->lookup_object(object); ret = object->cache->ops->lookup_object(object);
fscache_stat_d(&fscache_n_cop_lookup_object); fscache_stat_d(&fscache_n_cop_lookup_object);
if (test_bit(FSCACHE_OBJECT_EV_ERROR, &object->events)) fscache_unuse_cookie(object);
set_bit(FSCACHE_COOKIE_UNAVAILABLE, &cookie->flags);
if (ret == -ETIMEDOUT) { if (ret == -ETIMEDOUT) {
/* probably stuck behind another object, so move this one to /* probably stuck behind another object, so move this one to
* the back of the queue */ * the back of the queue */
fscache_stat(&fscache_n_object_lookups_timed_out); fscache_stat(&fscache_n_object_lookups_timed_out);
set_bit(FSCACHE_OBJECT_EV_REQUEUE, &object->events); _leave(" [timeout]");
return NO_TRANSIT;
} }
_leave(""); if (ret < 0) {
_leave(" [error]");
return transit_to(LOOKUP_FAILURE);
}
_leave(" [ok]");
return transit_to(OBJECT_AVAILABLE);
} }
/** /**
...@@ -524,32 +486,20 @@ void fscache_object_lookup_negative(struct fscache_object *object) ...@@ -524,32 +486,20 @@ void fscache_object_lookup_negative(struct fscache_object *object)
{ {
struct fscache_cookie *cookie = object->cookie; struct fscache_cookie *cookie = object->cookie;
_enter("{OBJ%x,%s}", _enter("{OBJ%x,%s}", object->debug_id, object->state->name);
object->debug_id, fscache_object_states[object->state]);
spin_lock(&object->lock); if (!test_and_set_bit(FSCACHE_OBJECT_IS_LOOKED_UP, &object->flags)) {
if (object->state == FSCACHE_OBJECT_LOOKING_UP) {
fscache_stat(&fscache_n_object_lookups_negative); fscache_stat(&fscache_n_object_lookups_negative);
/* transit here to allow write requests to begin stacking up /* Allow write requests to begin stacking up and read requests to begin
* and read requests to begin returning ENODATA */ * returning ENODATA.
object->state = FSCACHE_OBJECT_CREATING; */
spin_unlock(&object->lock);
set_bit(FSCACHE_COOKIE_PENDING_FILL, &cookie->flags);
set_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags); set_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
_debug("wake up lookup %p", &cookie->flags); _debug("wake up lookup %p", &cookie->flags);
smp_mb__before_clear_bit(); clear_bit_unlock(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags);
clear_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags);
smp_mb__after_clear_bit();
wake_up_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP); wake_up_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP);
set_bit(FSCACHE_OBJECT_EV_REQUEUE, &object->events);
} else {
ASSERTCMP(object->state, ==, FSCACHE_OBJECT_CREATING);
spin_unlock(&object->lock);
} }
_leave(""); _leave("");
} }
EXPORT_SYMBOL(fscache_object_lookup_negative); EXPORT_SYMBOL(fscache_object_lookup_negative);
...@@ -568,38 +518,26 @@ void fscache_obtained_object(struct fscache_object *object) ...@@ -568,38 +518,26 @@ void fscache_obtained_object(struct fscache_object *object)
{ {
struct fscache_cookie *cookie = object->cookie; struct fscache_cookie *cookie = object->cookie;
_enter("{OBJ%x,%s}", _enter("{OBJ%x,%s}", object->debug_id, object->state->name);
object->debug_id, fscache_object_states[object->state]);
/* if we were still looking up, then we must have a positive lookup /* if we were still looking up, then we must have a positive lookup
* result, in which case there may be data available */ * result, in which case there may be data available */
spin_lock(&object->lock); if (!test_and_set_bit(FSCACHE_OBJECT_IS_LOOKED_UP, &object->flags)) {
if (object->state == FSCACHE_OBJECT_LOOKING_UP) {
fscache_stat(&fscache_n_object_lookups_positive); fscache_stat(&fscache_n_object_lookups_positive);
clear_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags); /* We do (presumably) have data */
clear_bit_unlock(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
object->state = FSCACHE_OBJECT_AVAILABLE; /* Allow write requests to begin stacking up and read requests
spin_unlock(&object->lock); * to begin shovelling data.
*/
smp_mb__before_clear_bit(); clear_bit_unlock(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags);
clear_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags);
smp_mb__after_clear_bit();
wake_up_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP); wake_up_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP);
set_bit(FSCACHE_OBJECT_EV_REQUEUE, &object->events);
} else { } else {
ASSERTCMP(object->state, ==, FSCACHE_OBJECT_CREATING);
fscache_stat(&fscache_n_object_created); fscache_stat(&fscache_n_object_created);
object->state = FSCACHE_OBJECT_AVAILABLE;
spin_unlock(&object->lock);
set_bit(FSCACHE_OBJECT_EV_REQUEUE, &object->events);
smp_wmb();
} }
if (test_and_clear_bit(FSCACHE_COOKIE_CREATING, &cookie->flags)) set_bit(FSCACHE_OBJECT_IS_AVAILABLE, &object->flags);
wake_up_bit(&cookie->flags, FSCACHE_COOKIE_CREATING);
_leave(""); _leave("");
} }
EXPORT_SYMBOL(fscache_obtained_object); EXPORT_SYMBOL(fscache_obtained_object);
...@@ -607,15 +545,14 @@ EXPORT_SYMBOL(fscache_obtained_object); ...@@ -607,15 +545,14 @@ EXPORT_SYMBOL(fscache_obtained_object);
/* /*
* handle an object that has just become available * handle an object that has just become available
*/ */
static void fscache_object_available(struct fscache_object *object) static const struct fscache_state *fscache_object_available(struct fscache_object *object,
int event)
{ {
_enter("{OBJ%x}", object->debug_id); _enter("{OBJ%x},%d", object->debug_id, event);
spin_lock(&object->lock); object->oob_table = fscache_osm_run_oob;
if (object->cookie && spin_lock(&object->lock);
test_and_clear_bit(FSCACHE_COOKIE_CREATING, &object->cookie->flags))
wake_up_bit(&object->cookie->flags, FSCACHE_COOKIE_CREATING);
fscache_done_parent_op(object); fscache_done_parent_op(object);
if (object->n_in_progress == 0) { if (object->n_in_progress == 0) {
...@@ -631,130 +568,158 @@ static void fscache_object_available(struct fscache_object *object) ...@@ -631,130 +568,158 @@ static void fscache_object_available(struct fscache_object *object)
fscache_stat(&fscache_n_cop_lookup_complete); fscache_stat(&fscache_n_cop_lookup_complete);
object->cache->ops->lookup_complete(object); object->cache->ops->lookup_complete(object);
fscache_stat_d(&fscache_n_cop_lookup_complete); fscache_stat_d(&fscache_n_cop_lookup_complete);
fscache_enqueue_dependents(object);
fscache_hist(fscache_obj_instantiate_histogram, object->lookup_jif); fscache_hist(fscache_obj_instantiate_histogram, object->lookup_jif);
fscache_stat(&fscache_n_object_avail); fscache_stat(&fscache_n_object_avail);
_leave(""); _leave("");
return transit_to(JUMPSTART_DEPS);
} }
/* /*
* drop an object's attachments * Wake up this object's dependent objects now that we've become available.
*/ */
static void fscache_drop_object(struct fscache_object *object) static const struct fscache_state *fscache_jumpstart_dependents(struct fscache_object *object,
int event)
{ {
struct fscache_object *parent = object->parent; _enter("{OBJ%x},%d", object->debug_id, event);
struct fscache_cache *cache = object->cache;
_enter("{OBJ%x,%d}", object->debug_id, object->n_children); if (!fscache_enqueue_dependents(object, FSCACHE_OBJECT_EV_PARENT_READY))
return NO_TRANSIT; /* Not finished; requeue */
return transit_to(WAIT_FOR_CMD);
}
ASSERTCMP(object->cookie, ==, NULL); /*
ASSERT(hlist_unhashed(&object->cookie_link)); * Handle lookup or creation failute.
*/
static const struct fscache_state *fscache_lookup_failure(struct fscache_object *object,
int event)
{
struct fscache_cookie *cookie;
spin_lock(&cache->object_list_lock); _enter("{OBJ%x},%d", object->debug_id, event);
list_del_init(&object->cache_link);
spin_unlock(&cache->object_list_lock);
fscache_stat(&fscache_n_cop_drop_object); object->oob_event_mask = 0;
cache->ops->drop_object(object);
fscache_stat_d(&fscache_n_cop_drop_object);
if (parent) { fscache_stat(&fscache_n_cop_lookup_complete);
_debug("release parent OBJ%x {%d}", object->cache->ops->lookup_complete(object);
parent->debug_id, parent->n_children); fscache_stat_d(&fscache_n_cop_lookup_complete);
spin_lock(&parent->lock); cookie = object->cookie;
parent->n_children--; set_bit(FSCACHE_COOKIE_UNAVAILABLE, &cookie->flags);
if (parent->n_children == 0) if (test_and_clear_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags))
fscache_raise_event(parent, FSCACHE_OBJECT_EV_CLEARED); wake_up_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP);
spin_unlock(&parent->lock);
object->parent = NULL; fscache_done_parent_op(object);
return transit_to(KILL_OBJECT);
}
/*
* Wait for completion of all active operations on this object and the death of
* all child objects of this object.
*/
static const struct fscache_state *fscache_kill_object(struct fscache_object *object,
int event)
{
_enter("{OBJ%x,%d,%d},%d",
object->debug_id, object->n_ops, object->n_children, event);
clear_bit(FSCACHE_OBJECT_IS_LIVE, &object->flags);
object->oob_event_mask = 0;
if (list_empty(&object->dependents) &&
object->n_ops == 0 &&
object->n_children == 0)
return transit_to(DROP_OBJECT);
if (object->n_in_progress == 0) {
spin_lock(&object->lock);
if (object->n_ops > 0 && object->n_in_progress == 0)
fscache_start_operations(object);
spin_unlock(&object->lock);
} }
/* this just shifts the object release to the work processor */ if (!list_empty(&object->dependents))
fscache_put_object(object); return transit_to(KILL_DEPENDENTS);
_leave(""); return transit_to(WAIT_FOR_CLEARANCE);
} }
/* /*
* release or recycle an object that the netfs has discarded * Kill dependent objects.
*/ */
static void fscache_release_object(struct fscache_object *object) static const struct fscache_state *fscache_kill_dependents(struct fscache_object *object,
int event)
{ {
_enter(""); _enter("{OBJ%x},%d", object->debug_id, event);
fscache_drop_object(object); if (!fscache_enqueue_dependents(object, FSCACHE_OBJECT_EV_KILL))
return NO_TRANSIT; /* Not finished */
return transit_to(WAIT_FOR_CLEARANCE);
} }
/* /*
* withdraw an object from active service * Drop an object's attachments
*/ */
static void fscache_withdraw_object(struct fscache_object *object) static const struct fscache_state *fscache_drop_object(struct fscache_object *object,
int event)
{ {
struct fscache_cookie *cookie; struct fscache_object *parent = object->parent;
bool detached; struct fscache_cookie *cookie = object->cookie;
struct fscache_cache *cache = object->cache;
bool awaken = false;
_enter(""); _enter("{OBJ%x,%d},%d", object->debug_id, object->n_children, event);
spin_lock(&object->lock); ASSERT(cookie != NULL);
cookie = object->cookie; ASSERT(!hlist_unhashed(&object->cookie_link));
if (cookie) {
/* need to get the cookie lock before the object lock, starting
* from the object pointer */
atomic_inc(&cookie->usage);
spin_unlock(&object->lock);
detached = false; /* Make sure the cookie no longer points here and that the netfs isn't
spin_lock(&cookie->lock); * waiting for us.
spin_lock(&object->lock); */
spin_lock(&cookie->lock);
hlist_del_init(&object->cookie_link);
if (test_and_clear_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags))
awaken = true;
spin_unlock(&cookie->lock);
if (object->cookie == cookie) { if (awaken)
hlist_del_init(&object->cookie_link); wake_up_bit(&cookie->flags, FSCACHE_COOKIE_INVALIDATING);
object->cookie = NULL;
fscache_invalidation_complete(cookie);
detached = true;
}
spin_unlock(&cookie->lock);
fscache_cookie_put(cookie);
if (detached)
fscache_cookie_put(cookie);
}
/* Prevent a race with our last child, which has to signal EV_CLEARED
* before dropping our spinlock.
*/
spin_lock(&object->lock);
spin_unlock(&object->lock); spin_unlock(&object->lock);
fscache_drop_object(object); /* Discard from the cache's collection of objects */
} spin_lock(&cache->object_list_lock);
list_del_init(&object->cache_link);
spin_unlock(&cache->object_list_lock);
/* fscache_stat(&fscache_n_cop_drop_object);
* withdraw an object from active service at the behest of the cache cache->ops->drop_object(object);
* - need break the links to a cached object cookie fscache_stat_d(&fscache_n_cop_drop_object);
* - called under two situations:
* (1) recycler decides to reclaim an in-use object
* (2) a cache is unmounted
* - have to take care as the cookie can be being relinquished by the netfs
* simultaneously
* - the object is pinned by the caller holding a refcount on it
*/
void fscache_withdrawing_object(struct fscache_cache *cache,
struct fscache_object *object)
{
bool enqueue = false;
_enter(",OBJ%x", object->debug_id); /* The parent object wants to know when all it dependents have gone */
if (parent) {
_debug("release parent OBJ%x {%d}",
parent->debug_id, parent->n_children);
spin_lock(&object->lock); spin_lock(&parent->lock);
if (object->state < FSCACHE_OBJECT_WITHDRAWING) { parent->n_children--;
object->state = FSCACHE_OBJECT_WITHDRAWING; if (parent->n_children == 0)
enqueue = true; fscache_raise_event(parent, FSCACHE_OBJECT_EV_CLEARED);
spin_unlock(&parent->lock);
object->parent = NULL;
} }
spin_unlock(&object->lock);
if (enqueue) /* this just shifts the object release to the work processor */
fscache_enqueue_object(object); fscache_put_object(object);
fscache_stat(&fscache_n_object_dead);
_leave(""); _leave("");
return transit_to(OBJECT_DEAD);
} }
/* /*
...@@ -771,7 +736,7 @@ static int fscache_get_object(struct fscache_object *object) ...@@ -771,7 +736,7 @@ static int fscache_get_object(struct fscache_object *object)
} }
/* /*
* discard a ref on a work item * Discard a ref on an object
*/ */
static void fscache_put_object(struct fscache_object *object) static void fscache_put_object(struct fscache_object *object)
{ {
...@@ -780,6 +745,22 @@ static void fscache_put_object(struct fscache_object *object) ...@@ -780,6 +745,22 @@ static void fscache_put_object(struct fscache_object *object)
fscache_stat_d(&fscache_n_cop_put_object); fscache_stat_d(&fscache_n_cop_put_object);
} }
/**
* fscache_object_destroy - Note that a cache object is about to be destroyed
* @object: The object to be destroyed
*
* Note the imminent destruction and deallocation of a cache object record.
*/
void fscache_object_destroy(struct fscache_object *object)
{
fscache_objlist_remove(object);
/* We can get rid of the cookie now */
fscache_cookie_put(object->cookie);
object->cookie = NULL;
}
EXPORT_SYMBOL(fscache_object_destroy);
/* /*
* enqueue an object for metadata-type processing * enqueue an object for metadata-type processing
*/ */
...@@ -803,7 +784,7 @@ void fscache_enqueue_object(struct fscache_object *object) ...@@ -803,7 +784,7 @@ void fscache_enqueue_object(struct fscache_object *object)
/** /**
* fscache_object_sleep_till_congested - Sleep until object wq is congested * fscache_object_sleep_till_congested - Sleep until object wq is congested
* @timoutp: Scheduler sleep timeout * @timeoutp: Scheduler sleep timeout
* *
* Allow an object handler to sleep until the object workqueue is congested. * Allow an object handler to sleep until the object workqueue is congested.
* *
...@@ -831,18 +812,21 @@ bool fscache_object_sleep_till_congested(signed long *timeoutp) ...@@ -831,18 +812,21 @@ bool fscache_object_sleep_till_congested(signed long *timeoutp)
EXPORT_SYMBOL_GPL(fscache_object_sleep_till_congested); EXPORT_SYMBOL_GPL(fscache_object_sleep_till_congested);
/* /*
* enqueue the dependents of an object for metadata-type processing * Enqueue the dependents of an object for metadata-type processing.
* - the caller must hold the object's lock *
* - this may cause an already locked object to wind up being processed again * If we don't manage to finish the list before the scheduler wants to run
* again then return false immediately. We return true if the list was
* cleared.
*/ */
static void fscache_enqueue_dependents(struct fscache_object *object) static bool fscache_enqueue_dependents(struct fscache_object *object, int event)
{ {
struct fscache_object *dep; struct fscache_object *dep;
bool ret = true;
_enter("{OBJ%x}", object->debug_id); _enter("{OBJ%x}", object->debug_id);
if (list_empty(&object->dependents)) if (list_empty(&object->dependents))
return; return true;
spin_lock(&object->lock); spin_lock(&object->lock);
...@@ -851,23 +835,23 @@ static void fscache_enqueue_dependents(struct fscache_object *object) ...@@ -851,23 +835,23 @@ static void fscache_enqueue_dependents(struct fscache_object *object)
struct fscache_object, dep_link); struct fscache_object, dep_link);
list_del_init(&dep->dep_link); list_del_init(&dep->dep_link);
fscache_raise_event(dep, event);
/* sort onto appropriate lists */
fscache_enqueue_object(dep);
fscache_put_object(dep); fscache_put_object(dep);
if (!list_empty(&object->dependents)) if (!list_empty(&object->dependents) && need_resched()) {
cond_resched_lock(&object->lock); ret = false;
break;
}
} }
spin_unlock(&object->lock); spin_unlock(&object->lock);
return ret;
} }
/* /*
* remove an object from whatever queue it's waiting on * remove an object from whatever queue it's waiting on
* - the caller must hold object->lock
*/ */
void fscache_dequeue_object(struct fscache_object *object) static void fscache_dequeue_object(struct fscache_object *object)
{ {
_enter("{OBJ%x}", object->debug_id); _enter("{OBJ%x}", object->debug_id);
...@@ -886,7 +870,10 @@ void fscache_dequeue_object(struct fscache_object *object) ...@@ -886,7 +870,10 @@ void fscache_dequeue_object(struct fscache_object *object)
* @data: The auxiliary data for the object * @data: The auxiliary data for the object
* @datalen: The size of the auxiliary data * @datalen: The size of the auxiliary data
* *
* This function consults the netfs about the coherency state of an object * This function consults the netfs about the coherency state of an object.
* The caller must be holding a ref on cookie->n_active (held by
* fscache_look_up_object() on behalf of the cache backend during object lookup
* and creation).
*/ */
enum fscache_checkaux fscache_check_aux(struct fscache_object *object, enum fscache_checkaux fscache_check_aux(struct fscache_object *object,
const void *data, uint16_t datalen) const void *data, uint16_t datalen)
...@@ -927,12 +914,23 @@ EXPORT_SYMBOL(fscache_check_aux); ...@@ -927,12 +914,23 @@ EXPORT_SYMBOL(fscache_check_aux);
/* /*
* Asynchronously invalidate an object. * Asynchronously invalidate an object.
*/ */
static void fscache_invalidate_object(struct fscache_object *object) static const struct fscache_state *_fscache_invalidate_object(struct fscache_object *object,
int event)
{ {
struct fscache_operation *op; struct fscache_operation *op;
struct fscache_cookie *cookie = object->cookie; struct fscache_cookie *cookie = object->cookie;
_enter("{OBJ%x}", object->debug_id); _enter("{OBJ%x},%d", object->debug_id, event);
/* We're going to need the cookie. If the cookie is not available then
* retire the object instead.
*/
if (!fscache_use_cookie(object)) {
ASSERT(object->cookie->stores.rnode == NULL);
set_bit(FSCACHE_COOKIE_RETIRED, &cookie->flags);
_leave(" [no cookie]");
return transit_to(KILL_OBJECT);
}
/* Reject any new read/write ops and abort any that are pending. */ /* Reject any new read/write ops and abort any that are pending. */
fscache_invalidate_writes(cookie); fscache_invalidate_writes(cookie);
...@@ -941,14 +939,13 @@ static void fscache_invalidate_object(struct fscache_object *object) ...@@ -941,14 +939,13 @@ static void fscache_invalidate_object(struct fscache_object *object)
/* Now we have to wait for in-progress reads and writes */ /* Now we have to wait for in-progress reads and writes */
op = kzalloc(sizeof(*op), GFP_KERNEL); op = kzalloc(sizeof(*op), GFP_KERNEL);
if (!op) { if (!op)
fscache_raise_event(object, FSCACHE_OBJECT_EV_ERROR); goto nomem;
_leave(" [ENOMEM]");
return;
}
fscache_operation_init(op, object->cache->ops->invalidate_object, NULL); fscache_operation_init(op, object->cache->ops->invalidate_object, NULL);
op->flags = FSCACHE_OP_ASYNC | (1 << FSCACHE_OP_EXCLUSIVE); op->flags = FSCACHE_OP_ASYNC |
(1 << FSCACHE_OP_EXCLUSIVE) |
(1 << FSCACHE_OP_UNUSE_COOKIE);
spin_lock(&cookie->lock); spin_lock(&cookie->lock);
if (fscache_submit_exclusive_op(object, op) < 0) if (fscache_submit_exclusive_op(object, op) < 0)
...@@ -965,13 +962,50 @@ static void fscache_invalidate_object(struct fscache_object *object) ...@@ -965,13 +962,50 @@ static void fscache_invalidate_object(struct fscache_object *object)
/* We can allow read and write requests to come in once again. They'll /* We can allow read and write requests to come in once again. They'll
* queue up behind our exclusive invalidation operation. * queue up behind our exclusive invalidation operation.
*/ */
fscache_invalidation_complete(cookie); if (test_and_clear_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags))
_leave(""); wake_up_bit(&cookie->flags, FSCACHE_COOKIE_INVALIDATING);
return; _leave(" [ok]");
return transit_to(UPDATE_OBJECT);
nomem:
clear_bit(FSCACHE_OBJECT_IS_LIVE, &object->flags);
fscache_unuse_cookie(object);
_leave(" [ENOMEM]");
return transit_to(KILL_OBJECT);
submit_op_failed: submit_op_failed:
clear_bit(FSCACHE_OBJECT_IS_LIVE, &object->flags);
spin_unlock(&cookie->lock); spin_unlock(&cookie->lock);
kfree(op); kfree(op);
fscache_raise_event(object, FSCACHE_OBJECT_EV_ERROR);
_leave(" [EIO]"); _leave(" [EIO]");
return transit_to(KILL_OBJECT);
}
static const struct fscache_state *fscache_invalidate_object(struct fscache_object *object,
int event)
{
const struct fscache_state *s;
fscache_stat(&fscache_n_invalidates_run);
fscache_stat(&fscache_n_cop_invalidate_object);
s = _fscache_invalidate_object(object, event);
fscache_stat_d(&fscache_n_cop_invalidate_object);
return s;
}
/*
* Asynchronously update an object.
*/
static const struct fscache_state *fscache_update_object(struct fscache_object *object,
int event)
{
_enter("{OBJ%x},%d", object->debug_id, event);
fscache_stat(&fscache_n_updates_run);
fscache_stat(&fscache_n_cop_update_object);
object->cache->ops->update_object(object);
fscache_stat_d(&fscache_n_cop_update_object);
_leave("");
return transit_to(WAIT_FOR_CMD);
} }
...@@ -35,7 +35,7 @@ void fscache_enqueue_operation(struct fscache_operation *op) ...@@ -35,7 +35,7 @@ void fscache_enqueue_operation(struct fscache_operation *op)
ASSERT(list_empty(&op->pend_link)); ASSERT(list_empty(&op->pend_link));
ASSERT(op->processor != NULL); ASSERT(op->processor != NULL);
ASSERTCMP(op->object->state, >=, FSCACHE_OBJECT_AVAILABLE); ASSERT(fscache_object_is_available(op->object));
ASSERTCMP(atomic_read(&op->usage), >, 0); ASSERTCMP(atomic_read(&op->usage), >, 0);
ASSERTCMP(op->state, ==, FSCACHE_OP_ST_IN_PROGRESS); ASSERTCMP(op->state, ==, FSCACHE_OP_ST_IN_PROGRESS);
...@@ -119,7 +119,7 @@ int fscache_submit_exclusive_op(struct fscache_object *object, ...@@ -119,7 +119,7 @@ int fscache_submit_exclusive_op(struct fscache_object *object,
/* need to issue a new write op after this */ /* need to issue a new write op after this */
clear_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags); clear_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags);
ret = 0; ret = 0;
} else if (object->state == FSCACHE_OBJECT_CREATING) { } else if (test_bit(FSCACHE_OBJECT_IS_LOOKED_UP, &object->flags)) {
op->object = object; op->object = object;
object->n_ops++; object->n_ops++;
object->n_exclusive++; /* reads and writes must wait */ object->n_exclusive++; /* reads and writes must wait */
...@@ -144,7 +144,7 @@ int fscache_submit_exclusive_op(struct fscache_object *object, ...@@ -144,7 +144,7 @@ int fscache_submit_exclusive_op(struct fscache_object *object,
*/ */
static void fscache_report_unexpected_submission(struct fscache_object *object, static void fscache_report_unexpected_submission(struct fscache_object *object,
struct fscache_operation *op, struct fscache_operation *op,
unsigned long ostate) const struct fscache_state *ostate)
{ {
static bool once_only; static bool once_only;
struct fscache_operation *p; struct fscache_operation *p;
...@@ -155,11 +155,8 @@ static void fscache_report_unexpected_submission(struct fscache_object *object, ...@@ -155,11 +155,8 @@ static void fscache_report_unexpected_submission(struct fscache_object *object,
once_only = true; once_only = true;
kdebug("unexpected submission OP%x [OBJ%x %s]", kdebug("unexpected submission OP%x [OBJ%x %s]",
op->debug_id, object->debug_id, op->debug_id, object->debug_id, object->state->name);
fscache_object_states[object->state]); kdebug("objstate=%s [%s]", object->state->name, ostate->name);
kdebug("objstate=%s [%s]",
fscache_object_states[object->state],
fscache_object_states[ostate]);
kdebug("objflags=%lx", object->flags); kdebug("objflags=%lx", object->flags);
kdebug("objevent=%lx [%lx]", object->events, object->event_mask); kdebug("objevent=%lx [%lx]", object->events, object->event_mask);
kdebug("ops=%u inp=%u exc=%u", kdebug("ops=%u inp=%u exc=%u",
...@@ -190,7 +187,7 @@ static void fscache_report_unexpected_submission(struct fscache_object *object, ...@@ -190,7 +187,7 @@ static void fscache_report_unexpected_submission(struct fscache_object *object,
int fscache_submit_op(struct fscache_object *object, int fscache_submit_op(struct fscache_object *object,
struct fscache_operation *op) struct fscache_operation *op)
{ {
unsigned long ostate; const struct fscache_state *ostate;
int ret; int ret;
_enter("{OBJ%x OP%x},{%u}", _enter("{OBJ%x OP%x},{%u}",
...@@ -226,16 +223,14 @@ int fscache_submit_op(struct fscache_object *object, ...@@ -226,16 +223,14 @@ int fscache_submit_op(struct fscache_object *object,
fscache_run_op(object, op); fscache_run_op(object, op);
} }
ret = 0; ret = 0;
} else if (object->state == FSCACHE_OBJECT_CREATING) { } else if (test_bit(FSCACHE_OBJECT_IS_LOOKED_UP, &object->flags)) {
op->object = object; op->object = object;
object->n_ops++; object->n_ops++;
atomic_inc(&op->usage); atomic_inc(&op->usage);
list_add_tail(&op->pend_link, &object->pending_ops); list_add_tail(&op->pend_link, &object->pending_ops);
fscache_stat(&fscache_n_op_pend); fscache_stat(&fscache_n_op_pend);
ret = 0; ret = 0;
} else if (object->state == FSCACHE_OBJECT_DYING || } else if (fscache_object_is_dying(object)) {
object->state == FSCACHE_OBJECT_LC_DYING ||
object->state == FSCACHE_OBJECT_WITHDRAWING) {
fscache_stat(&fscache_n_op_rejected); fscache_stat(&fscache_n_op_rejected);
op->state = FSCACHE_OP_ST_CANCELLED; op->state = FSCACHE_OP_ST_CANCELLED;
ret = -ENOBUFS; ret = -ENOBUFS;
...@@ -265,8 +260,8 @@ void fscache_abort_object(struct fscache_object *object) ...@@ -265,8 +260,8 @@ void fscache_abort_object(struct fscache_object *object)
} }
/* /*
* jump start the operation processing on an object * Jump start the operation processing on an object. The caller must hold
* - caller must hold object->lock * object->lock.
*/ */
void fscache_start_operations(struct fscache_object *object) void fscache_start_operations(struct fscache_object *object)
{ {
...@@ -428,14 +423,10 @@ void fscache_put_operation(struct fscache_operation *op) ...@@ -428,14 +423,10 @@ void fscache_put_operation(struct fscache_operation *op)
object = op->object; object = op->object;
if (test_bit(FSCACHE_OP_DEC_READ_CNT, &op->flags)) { if (test_bit(FSCACHE_OP_DEC_READ_CNT, &op->flags))
if (atomic_dec_and_test(&object->n_reads)) { atomic_dec(&object->n_reads);
clear_bit(FSCACHE_COOKIE_WAITING_ON_READS, if (test_bit(FSCACHE_OP_UNUSE_COOKIE, &op->flags))
&object->cookie->flags); fscache_unuse_cookie(object);
wake_up_bit(&object->cookie->flags,
FSCACHE_COOKIE_WAITING_ON_READS);
}
}
/* now... we may get called with the object spinlock held, so we /* now... we may get called with the object spinlock held, so we
* complete the cleanup here only if we can immediately acquire the * complete the cleanup here only if we can immediately acquire the
......
...@@ -109,7 +109,7 @@ bool __fscache_maybe_release_page(struct fscache_cookie *cookie, ...@@ -109,7 +109,7 @@ bool __fscache_maybe_release_page(struct fscache_cookie *cookie,
* allocator as the work threads writing to the cache may all end up * allocator as the work threads writing to the cache may all end up
* sleeping on memory allocation, so we may need to impose a timeout * sleeping on memory allocation, so we may need to impose a timeout
* too. */ * too. */
if (!(gfp & __GFP_WAIT)) { if (!(gfp & __GFP_WAIT) || !(gfp & __GFP_FS)) {
fscache_stat(&fscache_n_store_vmscan_busy); fscache_stat(&fscache_n_store_vmscan_busy);
return false; return false;
} }
...@@ -163,10 +163,12 @@ static void fscache_attr_changed_op(struct fscache_operation *op) ...@@ -163,10 +163,12 @@ static void fscache_attr_changed_op(struct fscache_operation *op)
fscache_stat(&fscache_n_attr_changed_calls); fscache_stat(&fscache_n_attr_changed_calls);
if (fscache_object_is_active(object)) { if (fscache_object_is_active(object) &&
fscache_use_cookie(object)) {
fscache_stat(&fscache_n_cop_attr_changed); fscache_stat(&fscache_n_cop_attr_changed);
ret = object->cache->ops->attr_changed(object); ret = object->cache->ops->attr_changed(object);
fscache_stat_d(&fscache_n_cop_attr_changed); fscache_stat_d(&fscache_n_cop_attr_changed);
fscache_unuse_cookie(object);
if (ret < 0) if (ret < 0)
fscache_abort_object(object); fscache_abort_object(object);
} }
...@@ -233,7 +235,7 @@ static void fscache_release_retrieval_op(struct fscache_operation *_op) ...@@ -233,7 +235,7 @@ static void fscache_release_retrieval_op(struct fscache_operation *_op)
_enter("{OP%x}", op->op.debug_id); _enter("{OP%x}", op->op.debug_id);
ASSERTCMP(op->n_pages, ==, 0); ASSERTCMP(atomic_read(&op->n_pages), ==, 0);
fscache_hist(fscache_retrieval_histogram, op->start_time); fscache_hist(fscache_retrieval_histogram, op->start_time);
if (op->context) if (op->context)
...@@ -246,6 +248,7 @@ static void fscache_release_retrieval_op(struct fscache_operation *_op) ...@@ -246,6 +248,7 @@ static void fscache_release_retrieval_op(struct fscache_operation *_op)
* allocate a retrieval op * allocate a retrieval op
*/ */
static struct fscache_retrieval *fscache_alloc_retrieval( static struct fscache_retrieval *fscache_alloc_retrieval(
struct fscache_cookie *cookie,
struct address_space *mapping, struct address_space *mapping,
fscache_rw_complete_t end_io_func, fscache_rw_complete_t end_io_func,
void *context) void *context)
...@@ -260,7 +263,10 @@ static struct fscache_retrieval *fscache_alloc_retrieval( ...@@ -260,7 +263,10 @@ static struct fscache_retrieval *fscache_alloc_retrieval(
} }
fscache_operation_init(&op->op, NULL, fscache_release_retrieval_op); fscache_operation_init(&op->op, NULL, fscache_release_retrieval_op);
op->op.flags = FSCACHE_OP_MYTHREAD | (1 << FSCACHE_OP_WAITING); atomic_inc(&cookie->n_active);
op->op.flags = FSCACHE_OP_MYTHREAD |
(1UL << FSCACHE_OP_WAITING) |
(1UL << FSCACHE_OP_UNUSE_COOKIE);
op->mapping = mapping; op->mapping = mapping;
op->end_io_func = end_io_func; op->end_io_func = end_io_func;
op->context = context; op->context = context;
...@@ -310,7 +316,7 @@ static void fscache_do_cancel_retrieval(struct fscache_operation *_op) ...@@ -310,7 +316,7 @@ static void fscache_do_cancel_retrieval(struct fscache_operation *_op)
struct fscache_retrieval *op = struct fscache_retrieval *op =
container_of(_op, struct fscache_retrieval, op); container_of(_op, struct fscache_retrieval, op);
op->n_pages = 0; atomic_set(&op->n_pages, 0);
} }
/* /*
...@@ -394,12 +400,13 @@ int __fscache_read_or_alloc_page(struct fscache_cookie *cookie, ...@@ -394,12 +400,13 @@ int __fscache_read_or_alloc_page(struct fscache_cookie *cookie,
if (fscache_wait_for_deferred_lookup(cookie) < 0) if (fscache_wait_for_deferred_lookup(cookie) < 0)
return -ERESTARTSYS; return -ERESTARTSYS;
op = fscache_alloc_retrieval(page->mapping, end_io_func, context); op = fscache_alloc_retrieval(cookie, page->mapping,
end_io_func,context);
if (!op) { if (!op) {
_leave(" = -ENOMEM"); _leave(" = -ENOMEM");
return -ENOMEM; return -ENOMEM;
} }
op->n_pages = 1; atomic_set(&op->n_pages, 1);
spin_lock(&cookie->lock); spin_lock(&cookie->lock);
...@@ -408,7 +415,7 @@ int __fscache_read_or_alloc_page(struct fscache_cookie *cookie, ...@@ -408,7 +415,7 @@ int __fscache_read_or_alloc_page(struct fscache_cookie *cookie,
object = hlist_entry(cookie->backing_objects.first, object = hlist_entry(cookie->backing_objects.first,
struct fscache_object, cookie_link); struct fscache_object, cookie_link);
ASSERTCMP(object->state, >, FSCACHE_OBJECT_LOOKING_UP); ASSERT(test_bit(FSCACHE_OBJECT_IS_LOOKED_UP, &object->flags));
atomic_inc(&object->n_reads); atomic_inc(&object->n_reads);
__set_bit(FSCACHE_OP_DEC_READ_CNT, &op->op.flags); __set_bit(FSCACHE_OP_DEC_READ_CNT, &op->op.flags);
...@@ -465,6 +472,7 @@ int __fscache_read_or_alloc_page(struct fscache_cookie *cookie, ...@@ -465,6 +472,7 @@ int __fscache_read_or_alloc_page(struct fscache_cookie *cookie,
atomic_dec(&object->n_reads); atomic_dec(&object->n_reads);
nobufs_unlock: nobufs_unlock:
spin_unlock(&cookie->lock); spin_unlock(&cookie->lock);
atomic_dec(&cookie->n_active);
kfree(op); kfree(op);
nobufs: nobufs:
fscache_stat(&fscache_n_retrievals_nobufs); fscache_stat(&fscache_n_retrievals_nobufs);
...@@ -522,10 +530,10 @@ int __fscache_read_or_alloc_pages(struct fscache_cookie *cookie, ...@@ -522,10 +530,10 @@ int __fscache_read_or_alloc_pages(struct fscache_cookie *cookie,
if (fscache_wait_for_deferred_lookup(cookie) < 0) if (fscache_wait_for_deferred_lookup(cookie) < 0)
return -ERESTARTSYS; return -ERESTARTSYS;
op = fscache_alloc_retrieval(mapping, end_io_func, context); op = fscache_alloc_retrieval(cookie, mapping, end_io_func, context);
if (!op) if (!op)
return -ENOMEM; return -ENOMEM;
op->n_pages = *nr_pages; atomic_set(&op->n_pages, *nr_pages);
spin_lock(&cookie->lock); spin_lock(&cookie->lock);
...@@ -589,6 +597,7 @@ int __fscache_read_or_alloc_pages(struct fscache_cookie *cookie, ...@@ -589,6 +597,7 @@ int __fscache_read_or_alloc_pages(struct fscache_cookie *cookie,
atomic_dec(&object->n_reads); atomic_dec(&object->n_reads);
nobufs_unlock: nobufs_unlock:
spin_unlock(&cookie->lock); spin_unlock(&cookie->lock);
atomic_dec(&cookie->n_active);
kfree(op); kfree(op);
nobufs: nobufs:
fscache_stat(&fscache_n_retrievals_nobufs); fscache_stat(&fscache_n_retrievals_nobufs);
...@@ -631,10 +640,10 @@ int __fscache_alloc_page(struct fscache_cookie *cookie, ...@@ -631,10 +640,10 @@ int __fscache_alloc_page(struct fscache_cookie *cookie,
if (fscache_wait_for_deferred_lookup(cookie) < 0) if (fscache_wait_for_deferred_lookup(cookie) < 0)
return -ERESTARTSYS; return -ERESTARTSYS;
op = fscache_alloc_retrieval(page->mapping, NULL, NULL); op = fscache_alloc_retrieval(cookie, page->mapping, NULL, NULL);
if (!op) if (!op)
return -ENOMEM; return -ENOMEM;
op->n_pages = 1; atomic_set(&op->n_pages, 1);
spin_lock(&cookie->lock); spin_lock(&cookie->lock);
...@@ -675,6 +684,7 @@ int __fscache_alloc_page(struct fscache_cookie *cookie, ...@@ -675,6 +684,7 @@ int __fscache_alloc_page(struct fscache_cookie *cookie,
nobufs_unlock: nobufs_unlock:
spin_unlock(&cookie->lock); spin_unlock(&cookie->lock);
atomic_dec(&cookie->n_active);
kfree(op); kfree(op);
nobufs: nobufs:
fscache_stat(&fscache_n_allocs_nobufs); fscache_stat(&fscache_n_allocs_nobufs);
...@@ -729,8 +739,9 @@ static void fscache_write_op(struct fscache_operation *_op) ...@@ -729,8 +739,9 @@ static void fscache_write_op(struct fscache_operation *_op)
*/ */
spin_unlock(&object->lock); spin_unlock(&object->lock);
fscache_op_complete(&op->op, false); fscache_op_complete(&op->op, false);
_leave(" [cancel] op{f=%lx s=%u} obj{s=%u f=%lx}", _leave(" [cancel] op{f=%lx s=%u} obj{s=%s f=%lx}",
_op->flags, _op->state, object->state, object->flags); _op->flags, _op->state, object->state->short_name,
object->flags);
return; return;
} }
...@@ -796,11 +807,16 @@ void fscache_invalidate_writes(struct fscache_cookie *cookie) ...@@ -796,11 +807,16 @@ void fscache_invalidate_writes(struct fscache_cookie *cookie)
_enter(""); _enter("");
while (spin_lock(&cookie->stores_lock), for (;;) {
n = radix_tree_gang_lookup_tag(&cookie->stores, results, 0, spin_lock(&cookie->stores_lock);
ARRAY_SIZE(results), n = radix_tree_gang_lookup_tag(&cookie->stores, results, 0,
FSCACHE_COOKIE_PENDING_TAG), ARRAY_SIZE(results),
n > 0) { FSCACHE_COOKIE_PENDING_TAG);
if (n == 0) {
spin_unlock(&cookie->stores_lock);
break;
}
for (i = n - 1; i >= 0; i--) { for (i = n - 1; i >= 0; i--) {
page = results[i]; page = results[i];
radix_tree_delete(&cookie->stores, page->index); radix_tree_delete(&cookie->stores, page->index);
...@@ -812,7 +828,6 @@ void fscache_invalidate_writes(struct fscache_cookie *cookie) ...@@ -812,7 +828,6 @@ void fscache_invalidate_writes(struct fscache_cookie *cookie)
page_cache_release(results[i]); page_cache_release(results[i]);
} }
spin_unlock(&cookie->stores_lock);
_leave(""); _leave("");
} }
...@@ -829,14 +844,12 @@ void fscache_invalidate_writes(struct fscache_cookie *cookie) ...@@ -829,14 +844,12 @@ void fscache_invalidate_writes(struct fscache_cookie *cookie)
* (1) negative lookup, object not yet created (FSCACHE_COOKIE_CREATING is * (1) negative lookup, object not yet created (FSCACHE_COOKIE_CREATING is
* set) * set)
* *
* (a) no writes yet (set FSCACHE_COOKIE_PENDING_FILL and queue deferred * (a) no writes yet
* fill op)
* *
* (b) writes deferred till post-creation (mark page for writing and * (b) writes deferred till post-creation (mark page for writing and
* return immediately) * return immediately)
* *
* (2) negative lookup, object created, initial fill being made from netfs * (2) negative lookup, object created, initial fill being made from netfs
* (FSCACHE_COOKIE_INITIAL_FILL is set)
* *
* (a) fill point not yet reached this page (mark page for writing and * (a) fill point not yet reached this page (mark page for writing and
* return) * return)
...@@ -873,7 +886,9 @@ int __fscache_write_page(struct fscache_cookie *cookie, ...@@ -873,7 +886,9 @@ int __fscache_write_page(struct fscache_cookie *cookie,
fscache_operation_init(&op->op, fscache_write_op, fscache_operation_init(&op->op, fscache_write_op,
fscache_release_write_op); fscache_release_write_op);
op->op.flags = FSCACHE_OP_ASYNC | (1 << FSCACHE_OP_WAITING); op->op.flags = FSCACHE_OP_ASYNC |
(1 << FSCACHE_OP_WAITING) |
(1 << FSCACHE_OP_UNUSE_COOKIE);
ret = radix_tree_preload(gfp & ~__GFP_HIGHMEM); ret = radix_tree_preload(gfp & ~__GFP_HIGHMEM);
if (ret < 0) if (ret < 0)
...@@ -919,6 +934,7 @@ int __fscache_write_page(struct fscache_cookie *cookie, ...@@ -919,6 +934,7 @@ int __fscache_write_page(struct fscache_cookie *cookie,
op->op.debug_id = atomic_inc_return(&fscache_op_debug_id); op->op.debug_id = atomic_inc_return(&fscache_op_debug_id);
op->store_limit = object->store_limit; op->store_limit = object->store_limit;
atomic_inc(&cookie->n_active);
if (fscache_submit_op(object, &op->op) < 0) if (fscache_submit_op(object, &op->op) < 0)
goto submit_failed; goto submit_failed;
...@@ -945,6 +961,7 @@ int __fscache_write_page(struct fscache_cookie *cookie, ...@@ -945,6 +961,7 @@ int __fscache_write_page(struct fscache_cookie *cookie,
return 0; return 0;
submit_failed: submit_failed:
atomic_dec(&cookie->n_active);
spin_lock(&cookie->stores_lock); spin_lock(&cookie->stores_lock);
radix_tree_delete(&cookie->stores, page->index); radix_tree_delete(&cookie->stores, page->index);
spin_unlock(&cookie->stores_lock); spin_unlock(&cookie->stores_lock);
......
...@@ -97,7 +97,8 @@ struct fscache_operation { ...@@ -97,7 +97,8 @@ struct fscache_operation {
#define FSCACHE_OP_WAITING 4 /* cleared when op is woken */ #define FSCACHE_OP_WAITING 4 /* cleared when op is woken */
#define FSCACHE_OP_EXCLUSIVE 5 /* exclusive op, other ops must wait */ #define FSCACHE_OP_EXCLUSIVE 5 /* exclusive op, other ops must wait */
#define FSCACHE_OP_DEC_READ_CNT 6 /* decrement object->n_reads on destruction */ #define FSCACHE_OP_DEC_READ_CNT 6 /* decrement object->n_reads on destruction */
#define FSCACHE_OP_KEEP_FLAGS 0x0070 /* flags to keep when repurposing an op */ #define FSCACHE_OP_UNUSE_COOKIE 7 /* call fscache_unuse_cookie() on completion */
#define FSCACHE_OP_KEEP_FLAGS 0x00f0 /* flags to keep when repurposing an op */
enum fscache_operation_state state; enum fscache_operation_state state;
atomic_t usage; atomic_t usage;
...@@ -150,7 +151,7 @@ struct fscache_retrieval { ...@@ -150,7 +151,7 @@ struct fscache_retrieval {
void *context; /* netfs read context (pinned) */ void *context; /* netfs read context (pinned) */
struct list_head to_do; /* list of things to be done by the backend */ struct list_head to_do; /* list of things to be done by the backend */
unsigned long start_time; /* time at which retrieval started */ unsigned long start_time; /* time at which retrieval started */
unsigned n_pages; /* number of pages to be retrieved */ atomic_t n_pages; /* number of pages to be retrieved */
}; };
typedef int (*fscache_page_retrieval_func_t)(struct fscache_retrieval *op, typedef int (*fscache_page_retrieval_func_t)(struct fscache_retrieval *op,
...@@ -194,15 +195,14 @@ static inline void fscache_enqueue_retrieval(struct fscache_retrieval *op) ...@@ -194,15 +195,14 @@ static inline void fscache_enqueue_retrieval(struct fscache_retrieval *op)
static inline void fscache_retrieval_complete(struct fscache_retrieval *op, static inline void fscache_retrieval_complete(struct fscache_retrieval *op,
int n_pages) int n_pages)
{ {
op->n_pages -= n_pages; atomic_sub(n_pages, &op->n_pages);
if (op->n_pages <= 0) if (atomic_read(&op->n_pages) <= 0)
fscache_op_complete(&op->op, true); fscache_op_complete(&op->op, true);
} }
/** /**
* fscache_put_retrieval - Drop a reference to a retrieval operation * fscache_put_retrieval - Drop a reference to a retrieval operation
* @op: The retrieval operation affected * @op: The retrieval operation affected
* @n_pages: The number of pages to account for
* *
* Drop a reference to a retrieval operation. * Drop a reference to a retrieval operation.
*/ */
...@@ -314,6 +314,7 @@ struct fscache_cache_ops { ...@@ -314,6 +314,7 @@ struct fscache_cache_ops {
struct fscache_cookie { struct fscache_cookie {
atomic_t usage; /* number of users of this cookie */ atomic_t usage; /* number of users of this cookie */
atomic_t n_children; /* number of children of this cookie */ atomic_t n_children; /* number of children of this cookie */
atomic_t n_active; /* number of active users of netfs ptrs */
spinlock_t lock; spinlock_t lock;
spinlock_t stores_lock; /* lock on page store tree */ spinlock_t stores_lock; /* lock on page store tree */
struct hlist_head backing_objects; /* object(s) backing this file/index */ struct hlist_head backing_objects; /* object(s) backing this file/index */
...@@ -326,13 +327,11 @@ struct fscache_cookie { ...@@ -326,13 +327,11 @@ struct fscache_cookie {
unsigned long flags; unsigned long flags;
#define FSCACHE_COOKIE_LOOKING_UP 0 /* T if non-index cookie being looked up still */ #define FSCACHE_COOKIE_LOOKING_UP 0 /* T if non-index cookie being looked up still */
#define FSCACHE_COOKIE_CREATING 1 /* T if non-index object being created still */ #define FSCACHE_COOKIE_NO_DATA_YET 1 /* T if new object with no cached data yet */
#define FSCACHE_COOKIE_NO_DATA_YET 2 /* T if new object with no cached data yet */ #define FSCACHE_COOKIE_UNAVAILABLE 2 /* T if cookie is unavailable (error, etc) */
#define FSCACHE_COOKIE_PENDING_FILL 3 /* T if pending initial fill on object */ #define FSCACHE_COOKIE_INVALIDATING 3 /* T if cookie is being invalidated */
#define FSCACHE_COOKIE_FILLING 4 /* T if filling object incrementally */ #define FSCACHE_COOKIE_RELINQUISHED 4 /* T if cookie has been relinquished */
#define FSCACHE_COOKIE_UNAVAILABLE 5 /* T if cookie is unavailable (error, etc) */ #define FSCACHE_COOKIE_RETIRED 5 /* T if cookie was retired */
#define FSCACHE_COOKIE_WAITING_ON_READS 6 /* T if cookie is waiting on reads */
#define FSCACHE_COOKIE_INVALIDATING 7 /* T if cookie is being invalidated */
}; };
extern struct fscache_cookie fscache_fsdef_index; extern struct fscache_cookie fscache_fsdef_index;
...@@ -341,45 +340,40 @@ extern struct fscache_cookie fscache_fsdef_index; ...@@ -341,45 +340,40 @@ extern struct fscache_cookie fscache_fsdef_index;
* Event list for fscache_object::{event_mask,events} * Event list for fscache_object::{event_mask,events}
*/ */
enum { enum {
FSCACHE_OBJECT_EV_REQUEUE, /* T if object should be requeued */ FSCACHE_OBJECT_EV_NEW_CHILD, /* T if object has a new child */
FSCACHE_OBJECT_EV_PARENT_READY, /* T if object's parent is ready */
FSCACHE_OBJECT_EV_UPDATE, /* T if object should be updated */ FSCACHE_OBJECT_EV_UPDATE, /* T if object should be updated */
FSCACHE_OBJECT_EV_INVALIDATE, /* T if cache requested object invalidation */ FSCACHE_OBJECT_EV_INVALIDATE, /* T if cache requested object invalidation */
FSCACHE_OBJECT_EV_CLEARED, /* T if accessors all gone */ FSCACHE_OBJECT_EV_CLEARED, /* T if accessors all gone */
FSCACHE_OBJECT_EV_ERROR, /* T if fatal error occurred during processing */ FSCACHE_OBJECT_EV_ERROR, /* T if fatal error occurred during processing */
FSCACHE_OBJECT_EV_RELEASE, /* T if netfs requested object release */ FSCACHE_OBJECT_EV_KILL, /* T if netfs relinquished or cache withdrew object */
FSCACHE_OBJECT_EV_RETIRE, /* T if netfs requested object retirement */
FSCACHE_OBJECT_EV_WITHDRAW, /* T if cache requested object withdrawal */
NR_FSCACHE_OBJECT_EVENTS NR_FSCACHE_OBJECT_EVENTS
}; };
#define FSCACHE_OBJECT_EVENTS_MASK ((1UL << NR_FSCACHE_OBJECT_EVENTS) - 1) #define FSCACHE_OBJECT_EVENTS_MASK ((1UL << NR_FSCACHE_OBJECT_EVENTS) - 1)
/*
* States for object state machine.
*/
struct fscache_transition {
unsigned long events;
const struct fscache_state *transit_to;
};
struct fscache_state {
char name[24];
char short_name[8];
const struct fscache_state *(*work)(struct fscache_object *object,
int event);
const struct fscache_transition transitions[];
};
/* /*
* on-disk cache file or index handle * on-disk cache file or index handle
*/ */
struct fscache_object { struct fscache_object {
enum fscache_object_state { const struct fscache_state *state; /* Object state machine state */
FSCACHE_OBJECT_INIT, /* object in initial unbound state */ const struct fscache_transition *oob_table; /* OOB state transition table */
FSCACHE_OBJECT_LOOKING_UP, /* looking up object */
FSCACHE_OBJECT_CREATING, /* creating object */
/* active states */
FSCACHE_OBJECT_AVAILABLE, /* cleaning up object after creation */
FSCACHE_OBJECT_ACTIVE, /* object is usable */
FSCACHE_OBJECT_INVALIDATING, /* object is invalidating */
FSCACHE_OBJECT_UPDATING, /* object is updating */
/* terminal states */
FSCACHE_OBJECT_DYING, /* object waiting for accessors to finish */
FSCACHE_OBJECT_LC_DYING, /* object cleaning up after lookup/create */
FSCACHE_OBJECT_ABORT_INIT, /* abort the init state */
FSCACHE_OBJECT_RELEASING, /* releasing object */
FSCACHE_OBJECT_RECYCLING, /* retiring object */
FSCACHE_OBJECT_WITHDRAWING, /* withdrawing object */
FSCACHE_OBJECT_DEAD, /* object is now dead */
FSCACHE_OBJECT__NSTATES
} state;
int debug_id; /* debugging ID */ int debug_id; /* debugging ID */
int n_children; /* number of child objects */ int n_children; /* number of child objects */
int n_ops; /* number of extant ops on object */ int n_ops; /* number of extant ops on object */
...@@ -390,6 +384,7 @@ struct fscache_object { ...@@ -390,6 +384,7 @@ struct fscache_object {
spinlock_t lock; /* state and operations lock */ spinlock_t lock; /* state and operations lock */
unsigned long lookup_jif; /* time at which lookup started */ unsigned long lookup_jif; /* time at which lookup started */
unsigned long oob_event_mask; /* OOB events this object is interested in */
unsigned long event_mask; /* events this object is interested in */ unsigned long event_mask; /* events this object is interested in */
unsigned long events; /* events to be processed by this object unsigned long events; /* events to be processed by this object
* (order is important - using fls) */ * (order is important - using fls) */
...@@ -398,6 +393,9 @@ struct fscache_object { ...@@ -398,6 +393,9 @@ struct fscache_object {
#define FSCACHE_OBJECT_LOCK 0 /* T if object is busy being processed */ #define FSCACHE_OBJECT_LOCK 0 /* T if object is busy being processed */
#define FSCACHE_OBJECT_PENDING_WRITE 1 /* T if object has pending write */ #define FSCACHE_OBJECT_PENDING_WRITE 1 /* T if object has pending write */
#define FSCACHE_OBJECT_WAITING 2 /* T if object is waiting on its parent */ #define FSCACHE_OBJECT_WAITING 2 /* T if object is waiting on its parent */
#define FSCACHE_OBJECT_IS_LIVE 3 /* T if object is not withdrawn or relinquished */
#define FSCACHE_OBJECT_IS_LOOKED_UP 4 /* T if object has been looked up */
#define FSCACHE_OBJECT_IS_AVAILABLE 5 /* T if object has become active */
struct list_head cache_link; /* link in cache->object_list */ struct list_head cache_link; /* link in cache->object_list */
struct hlist_node cookie_link; /* link in cookie->backing_objects */ struct hlist_node cookie_link; /* link in cookie->backing_objects */
...@@ -415,62 +413,40 @@ struct fscache_object { ...@@ -415,62 +413,40 @@ struct fscache_object {
loff_t store_limit_l; /* current storage limit */ loff_t store_limit_l; /* current storage limit */
}; };
extern const char *fscache_object_states[]; extern void fscache_object_init(struct fscache_object *, struct fscache_cookie *,
struct fscache_cache *);
extern void fscache_object_destroy(struct fscache_object *);
#define fscache_object_is_active(obj) \ extern void fscache_object_lookup_negative(struct fscache_object *object);
(!test_bit(FSCACHE_IOERROR, &(obj)->cache->flags) && \ extern void fscache_obtained_object(struct fscache_object *object);
(obj)->state >= FSCACHE_OBJECT_AVAILABLE && \
(obj)->state < FSCACHE_OBJECT_DYING)
#define fscache_object_is_dead(obj) \ static inline bool fscache_object_is_live(struct fscache_object *object)
(test_bit(FSCACHE_IOERROR, &(obj)->cache->flags) && \ {
(obj)->state >= FSCACHE_OBJECT_DYING) return test_bit(FSCACHE_OBJECT_IS_LIVE, &object->flags);
}
extern void fscache_object_work_func(struct work_struct *work); static inline bool fscache_object_is_dying(struct fscache_object *object)
{
return !fscache_object_is_live(object);
}
/** static inline bool fscache_object_is_available(struct fscache_object *object)
* fscache_object_init - Initialise a cache object description
* @object: Object description
*
* Initialise a cache object description to its basic values.
*
* See Documentation/filesystems/caching/backend-api.txt for a complete
* description.
*/
static inline
void fscache_object_init(struct fscache_object *object,
struct fscache_cookie *cookie,
struct fscache_cache *cache)
{ {
atomic_inc(&cache->object_count); return test_bit(FSCACHE_OBJECT_IS_AVAILABLE, &object->flags);
object->state = FSCACHE_OBJECT_INIT;
spin_lock_init(&object->lock);
INIT_LIST_HEAD(&object->cache_link);
INIT_HLIST_NODE(&object->cookie_link);
INIT_WORK(&object->work, fscache_object_work_func);
INIT_LIST_HEAD(&object->dependents);
INIT_LIST_HEAD(&object->dep_link);
INIT_LIST_HEAD(&object->pending_ops);
object->n_children = 0;
object->n_ops = object->n_in_progress = object->n_exclusive = 0;
object->events = object->event_mask = 0;
object->flags = 0;
object->store_limit = 0;
object->store_limit_l = 0;
object->cache = cache;
object->cookie = cookie;
object->parent = NULL;
} }
extern void fscache_object_lookup_negative(struct fscache_object *object); static inline bool fscache_object_is_active(struct fscache_object *object)
extern void fscache_obtained_object(struct fscache_object *object); {
return fscache_object_is_available(object) &&
fscache_object_is_live(object) &&
!test_bit(FSCACHE_IOERROR, &object->cache->flags);
}
#ifdef CONFIG_FSCACHE_OBJECT_LIST static inline bool fscache_object_is_dead(struct fscache_object *object)
extern void fscache_object_destroy(struct fscache_object *object); {
#else return fscache_object_is_dying(object) &&
#define fscache_object_destroy(object) do {} while(0) test_bit(FSCACHE_IOERROR, &object->cache->flags);
#endif }
/** /**
* fscache_object_destroyed - Note destruction of an object in a cache * fscache_object_destroyed - Note destruction of an object in a cache
...@@ -531,6 +507,33 @@ static inline void fscache_end_io(struct fscache_retrieval *op, ...@@ -531,6 +507,33 @@ static inline void fscache_end_io(struct fscache_retrieval *op,
op->end_io_func(page, op->context, error); op->end_io_func(page, op->context, error);
} }
/**
* fscache_use_cookie - Request usage of cookie attached to an object
* @object: Object description
*
* Request usage of the cookie attached to an object. NULL is returned if the
* relinquishment had reduced the cookie usage count to 0.
*/
static inline bool fscache_use_cookie(struct fscache_object *object)
{
struct fscache_cookie *cookie = object->cookie;
return atomic_inc_not_zero(&cookie->n_active) != 0;
}
/**
* fscache_unuse_cookie - Cease usage of cookie attached to an object
* @object: Object description
*
* Cease usage of the cookie attached to an object. When the users count
* reaches zero then the cookie relinquishment will be permitted to proceed.
*/
static inline void fscache_unuse_cookie(struct fscache_object *object)
{
struct fscache_cookie *cookie = object->cookie;
if (atomic_dec_and_test(&cookie->n_active))
wake_up_atomic_t(&cookie->n_active);
}
/* /*
* out-of-line cache backend functions * out-of-line cache backend functions
*/ */
......
...@@ -23,6 +23,7 @@ struct __wait_queue { ...@@ -23,6 +23,7 @@ struct __wait_queue {
struct wait_bit_key { struct wait_bit_key {
void *flags; void *flags;
int bit_nr; int bit_nr;
#define WAIT_ATOMIC_T_BIT_NR -1
}; };
struct wait_bit_queue { struct wait_bit_queue {
...@@ -60,6 +61,9 @@ struct task_struct; ...@@ -60,6 +61,9 @@ struct task_struct;
#define __WAIT_BIT_KEY_INITIALIZER(word, bit) \ #define __WAIT_BIT_KEY_INITIALIZER(word, bit) \
{ .flags = word, .bit_nr = bit, } { .flags = word, .bit_nr = bit, }
#define __WAIT_ATOMIC_T_KEY_INITIALIZER(p) \
{ .flags = p, .bit_nr = WAIT_ATOMIC_T_BIT_NR, }
extern void __init_waitqueue_head(wait_queue_head_t *q, const char *name, struct lock_class_key *); extern void __init_waitqueue_head(wait_queue_head_t *q, const char *name, struct lock_class_key *);
#define init_waitqueue_head(q) \ #define init_waitqueue_head(q) \
...@@ -146,8 +150,10 @@ void __wake_up_bit(wait_queue_head_t *, void *, int); ...@@ -146,8 +150,10 @@ void __wake_up_bit(wait_queue_head_t *, void *, int);
int __wait_on_bit(wait_queue_head_t *, struct wait_bit_queue *, int (*)(void *), unsigned); int __wait_on_bit(wait_queue_head_t *, struct wait_bit_queue *, int (*)(void *), unsigned);
int __wait_on_bit_lock(wait_queue_head_t *, struct wait_bit_queue *, int (*)(void *), unsigned); int __wait_on_bit_lock(wait_queue_head_t *, struct wait_bit_queue *, int (*)(void *), unsigned);
void wake_up_bit(void *, int); void wake_up_bit(void *, int);
void wake_up_atomic_t(atomic_t *);
int out_of_line_wait_on_bit(void *, int, int (*)(void *), unsigned); int out_of_line_wait_on_bit(void *, int, int (*)(void *), unsigned);
int out_of_line_wait_on_bit_lock(void *, int, int (*)(void *), unsigned); int out_of_line_wait_on_bit_lock(void *, int, int (*)(void *), unsigned);
int out_of_line_wait_on_atomic_t(atomic_t *, int (*)(atomic_t *), unsigned);
wait_queue_head_t *bit_waitqueue(void *, int); wait_queue_head_t *bit_waitqueue(void *, int);
#define wake_up(x) __wake_up(x, TASK_NORMAL, 1, NULL) #define wake_up(x) __wake_up(x, TASK_NORMAL, 1, NULL)
...@@ -902,5 +908,23 @@ static inline int wait_on_bit_lock(void *word, int bit, ...@@ -902,5 +908,23 @@ static inline int wait_on_bit_lock(void *word, int bit,
return 0; return 0;
return out_of_line_wait_on_bit_lock(word, bit, action, mode); return out_of_line_wait_on_bit_lock(word, bit, action, mode);
} }
/**
* wait_on_atomic_t - Wait for an atomic_t to become 0
* @val: The atomic value being waited on, a kernel virtual address
* @action: the function used to sleep, which may take special actions
* @mode: the task state to sleep in
*
* Wait for an atomic_t to become 0. We abuse the bit-wait waitqueue table for
* the purpose of getting a waitqueue, but we set the key to a bit number
* outside of the target 'word'.
*/
static inline
int wait_on_atomic_t(atomic_t *val, int (*action)(atomic_t *), unsigned mode)
{
if (atomic_read(val) == 0)
return 0;
return out_of_line_wait_on_atomic_t(val, action, mode);
}
#endif #endif
...@@ -287,3 +287,91 @@ wait_queue_head_t *bit_waitqueue(void *word, int bit) ...@@ -287,3 +287,91 @@ wait_queue_head_t *bit_waitqueue(void *word, int bit)
return &zone->wait_table[hash_long(val, zone->wait_table_bits)]; return &zone->wait_table[hash_long(val, zone->wait_table_bits)];
} }
EXPORT_SYMBOL(bit_waitqueue); EXPORT_SYMBOL(bit_waitqueue);
/*
* Manipulate the atomic_t address to produce a better bit waitqueue table hash
* index (we're keying off bit -1, but that would produce a horrible hash
* value).
*/
static inline wait_queue_head_t *atomic_t_waitqueue(atomic_t *p)
{
if (BITS_PER_LONG == 64) {
unsigned long q = (unsigned long)p;
return bit_waitqueue((void *)(q & ~1), q & 1);
}
return bit_waitqueue(p, 0);
}
static int wake_atomic_t_function(wait_queue_t *wait, unsigned mode, int sync,
void *arg)
{
struct wait_bit_key *key = arg;
struct wait_bit_queue *wait_bit
= container_of(wait, struct wait_bit_queue, wait);
atomic_t *val = key->flags;
if (wait_bit->key.flags != key->flags ||
wait_bit->key.bit_nr != key->bit_nr ||
atomic_read(val) != 0)
return 0;
return autoremove_wake_function(wait, mode, sync, key);
}
/*
* To allow interruptible waiting and asynchronous (i.e. nonblocking) waiting,
* the actions of __wait_on_atomic_t() are permitted return codes. Nonzero
* return codes halt waiting and return.
*/
static __sched
int __wait_on_atomic_t(wait_queue_head_t *wq, struct wait_bit_queue *q,
int (*action)(atomic_t *), unsigned mode)
{
atomic_t *val;
int ret = 0;
do {
prepare_to_wait(wq, &q->wait, mode);
val = q->key.flags;
if (atomic_read(val) == 0)
ret = (*action)(val);
} while (!ret && atomic_read(val) != 0);
finish_wait(wq, &q->wait);
return ret;
}
#define DEFINE_WAIT_ATOMIC_T(name, p) \
struct wait_bit_queue name = { \
.key = __WAIT_ATOMIC_T_KEY_INITIALIZER(p), \
.wait = { \
.private = current, \
.func = wake_atomic_t_function, \
.task_list = \
LIST_HEAD_INIT((name).wait.task_list), \
}, \
}
__sched int out_of_line_wait_on_atomic_t(atomic_t *p, int (*action)(atomic_t *),
unsigned mode)
{
wait_queue_head_t *wq = atomic_t_waitqueue(p);
DEFINE_WAIT_ATOMIC_T(wait, p);
return __wait_on_atomic_t(wq, &wait, action, mode);
}
EXPORT_SYMBOL(out_of_line_wait_on_atomic_t);
/**
* wake_up_atomic_t - Wake up a waiter on a atomic_t
* @word: The word being waited on, a kernel virtual address
* @bit: The bit of the word being waited on
*
* Wake up anyone waiting for the atomic_t to go to zero.
*
* Abuse the bit-waker function and its waitqueue hash table set (the atomic_t
* check is done by the waiter's wake function, not the by the waker itself).
*/
void wake_up_atomic_t(atomic_t *p)
{
__wake_up_bit(atomic_t_waitqueue(p), p, WAIT_ATOMIC_T_BIT_NR);
}
EXPORT_SYMBOL(wake_up_atomic_t);
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment