1 /* FS-Cache object state machine handler
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
11 * See Documentation/filesystems/caching/object.txt for a description of the
12 * object state machine and the in-kernel representations.
15 #define FSCACHE_DEBUG_LEVEL COOKIE
16 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <linux/prefetch.h>
21 static const struct fscache_state *fscache_abort_initialisation(struct fscache_object *, int);
22 static const struct fscache_state *fscache_kill_dependents(struct fscache_object *, int);
23 static const struct fscache_state *fscache_drop_object(struct fscache_object *, int);
24 static const struct fscache_state *fscache_initialise_object(struct fscache_object *, int);
25 static const struct fscache_state *fscache_invalidate_object(struct fscache_object *, int);
26 static const struct fscache_state *fscache_jumpstart_dependents(struct fscache_object *, int);
27 static const struct fscache_state *fscache_kill_object(struct fscache_object *, int);
28 static const struct fscache_state *fscache_lookup_failure(struct fscache_object *, int);
29 static const struct fscache_state *fscache_look_up_object(struct fscache_object *, int);
30 static const struct fscache_state *fscache_object_available(struct fscache_object *, int);
31 static const struct fscache_state *fscache_parent_ready(struct fscache_object *, int);
32 static const struct fscache_state *fscache_update_object(struct fscache_object *, int);
33 static const struct fscache_state *fscache_detach_from_cookie(struct fscache_object *, int);
35 #define __STATE_NAME(n) fscache_osm_##n
36 #define STATE(n) (&__STATE_NAME(n))
39 * Define a work state. Work states are execution states. No event processing
40 * is performed by them. The function attached to a work state returns a
41 * pointer indicating the next state to which the state machine should
42 * transition. Returning NO_TRANSIT repeats the current state, but goes back
43 * to the scheduler first.
45 #define WORK_STATE(n, sn, f) \
46 const struct fscache_state __STATE_NAME(n) = { \
53 * Returns from work states.
55 #define transit_to(state) ({ prefetch(&STATE(state)->work); STATE(state); })
57 #define NO_TRANSIT ((struct fscache_state *)NULL)
60 * Define a wait state. Wait states are event processing states. No execution
61 * is performed by them. Wait states are just tables of "if event X occurs,
62 * clear it and transition to state Y". The dispatcher returns to the
63 * scheduler if none of the events in which the wait state has an interest are
66 #define WAIT_STATE(n, sn, ...) \
67 const struct fscache_state __STATE_NAME(n) = { \
71 .transitions = { __VA_ARGS__, { 0, NULL } } \
74 #define TRANSIT_TO(state, emask) \
75 { .events = (emask), .transit_to = STATE(state) }
78 * The object state machine.
80 static WORK_STATE(INIT_OBJECT, "INIT", fscache_initialise_object);
81 static WORK_STATE(PARENT_READY, "PRDY", fscache_parent_ready);
82 static WORK_STATE(ABORT_INIT, "ABRT", fscache_abort_initialisation);
83 static WORK_STATE(LOOK_UP_OBJECT, "LOOK", fscache_look_up_object);
84 static WORK_STATE(CREATE_OBJECT, "CRTO", fscache_look_up_object);
85 static WORK_STATE(OBJECT_AVAILABLE, "AVBL", fscache_object_available);
86 static WORK_STATE(JUMPSTART_DEPS, "JUMP", fscache_jumpstart_dependents);
88 static WORK_STATE(INVALIDATE_OBJECT, "INVL", fscache_invalidate_object);
89 static WORK_STATE(UPDATE_OBJECT, "UPDT", fscache_update_object);
91 static WORK_STATE(LOOKUP_FAILURE, "LCFL", fscache_lookup_failure);
92 static WORK_STATE(KILL_OBJECT, "KILL", fscache_kill_object);
93 static WORK_STATE(KILL_DEPENDENTS, "KDEP", fscache_kill_dependents);
94 static WORK_STATE(DROP_OBJECT, "DROP", fscache_drop_object);
95 static WORK_STATE(DETACH_FROM_COOKIE, "DTCH", fscache_detach_from_cookie);
96 static WORK_STATE(OBJECT_DEAD, "DEAD", (void*)2UL);
98 static WAIT_STATE(WAIT_FOR_INIT, "?INI",
99 TRANSIT_TO(INIT_OBJECT, 1 << FSCACHE_OBJECT_EV_NEW_CHILD));
101 static WAIT_STATE(WAIT_FOR_PARENT, "?PRN",
102 TRANSIT_TO(PARENT_READY, 1 << FSCACHE_OBJECT_EV_PARENT_READY));
104 static WAIT_STATE(WAIT_FOR_CMD, "?CMD",
105 TRANSIT_TO(INVALIDATE_OBJECT, 1 << FSCACHE_OBJECT_EV_INVALIDATE),
106 TRANSIT_TO(UPDATE_OBJECT, 1 << FSCACHE_OBJECT_EV_UPDATE),
107 TRANSIT_TO(JUMPSTART_DEPS, 1 << FSCACHE_OBJECT_EV_NEW_CHILD));
109 static WAIT_STATE(WAIT_FOR_CLEARANCE, "?CLR",
110 TRANSIT_TO(KILL_OBJECT, 1 << FSCACHE_OBJECT_EV_CLEARED));
113 * Out-of-band event transition tables. These are for handling unexpected
114 * events, such as an I/O error. If an OOB event occurs, the state machine
115 * clears and disables the event and forces a transition to the nominated work
116 * state (acurrently executing work states will complete first).
118 * In such a situation, object->state remembers the state the machine should
119 * have been in/gone to and returning NO_TRANSIT returns to that.
121 static const struct fscache_transition fscache_osm_init_oob[] = {
122 TRANSIT_TO(ABORT_INIT,
123 (1 << FSCACHE_OBJECT_EV_ERROR) |
124 (1 << FSCACHE_OBJECT_EV_KILL)),
128 static const struct fscache_transition fscache_osm_lookup_oob[] = {
129 TRANSIT_TO(LOOKUP_FAILURE,
130 (1 << FSCACHE_OBJECT_EV_ERROR) |
131 (1 << FSCACHE_OBJECT_EV_KILL)),
135 static const struct fscache_transition fscache_osm_run_oob[] = {
136 TRANSIT_TO(KILL_OBJECT,
137 (1 << FSCACHE_OBJECT_EV_ERROR) |
138 (1 << FSCACHE_OBJECT_EV_KILL)),
142 static int fscache_get_object(struct fscache_object *);
143 static void fscache_put_object(struct fscache_object *);
144 static bool fscache_enqueue_dependents(struct fscache_object *, int);
145 static void fscache_dequeue_object(struct fscache_object *);
148 * we need to notify the parent when an op completes that we had outstanding
151 static inline void fscache_done_parent_op(struct fscache_object *object)
153 struct fscache_object *parent = object->parent;
155 _enter("OBJ%x {OBJ%x,%x}",
156 object->debug_id, parent->debug_id, parent->n_ops);
158 spin_lock_nested(&parent->lock, 1);
161 if (parent->n_ops == 0)
162 fscache_raise_event(parent, FSCACHE_OBJECT_EV_CLEARED);
163 spin_unlock(&parent->lock);
167 * Object state machine dispatcher.
169 static void fscache_object_sm_dispatcher(struct fscache_object *object)
171 const struct fscache_transition *t;
172 const struct fscache_state *state, *new_state;
173 unsigned long events, event_mask;
176 ASSERT(object != NULL);
178 _enter("{OBJ%x,%s,%lx}",
179 object->debug_id, object->state->name, object->events);
181 event_mask = object->event_mask;
183 object->event_mask = 0; /* Mask normal event handling */
184 state = object->state;
186 events = object->events;
188 /* Handle any out-of-band events (typically an error) */
189 if (events & object->oob_event_mask) {
190 _debug("{OBJ%x} oob %lx",
191 object->debug_id, events & object->oob_event_mask);
192 for (t = object->oob_table; t->events; t++) {
193 if (events & t->events) {
194 state = t->transit_to;
195 ASSERT(state->work != NULL);
196 event = fls(events & t->events) - 1;
197 __clear_bit(event, &object->oob_event_mask);
198 clear_bit(event, &object->events);
199 goto execute_work_state;
204 /* Wait states are just transition tables */
206 if (events & event_mask) {
207 for (t = state->transitions; t->events; t++) {
208 if (events & t->events) {
209 new_state = t->transit_to;
210 event = fls(events & t->events) - 1;
211 clear_bit(event, &object->events);
212 _debug("{OBJ%x} ev %d: %s -> %s",
213 object->debug_id, event,
214 state->name, new_state->name);
215 object->state = state = new_state;
216 goto execute_work_state;
220 /* The event mask didn't include all the tabled bits */
223 /* Randomly woke up */
228 _debug("{OBJ%x} exec %s", object->debug_id, state->name);
230 new_state = state->work(object, event);
232 if (new_state == NO_TRANSIT) {
233 _debug("{OBJ%x} %s notrans", object->debug_id, state->name);
234 fscache_enqueue_object(object);
235 event_mask = object->oob_event_mask;
239 _debug("{OBJ%x} %s -> %s",
240 object->debug_id, state->name, new_state->name);
241 object->state = state = new_state;
244 if (unlikely(state->work == ((void *)2UL))) {
251 /* Transited to wait state */
252 event_mask = object->oob_event_mask;
253 for (t = state->transitions; t->events; t++)
254 event_mask |= t->events;
257 object->event_mask = event_mask;
259 events = object->events;
260 if (events & event_mask)
262 _leave(" [msk %lx]", event_mask);
268 static void fscache_object_work_func(struct work_struct *work)
270 struct fscache_object *object =
271 container_of(work, struct fscache_object, work);
274 _enter("{OBJ%x}", object->debug_id);
277 fscache_object_sm_dispatcher(object);
278 fscache_hist(fscache_objs_histogram, start);
279 fscache_put_object(object);
283 * fscache_object_init - Initialise a cache object description
284 * @object: Object description
285 * @cookie: Cookie object will be attached to
286 * @cache: Cache in which backing object will be found
288 * Initialise a cache object description to its basic values.
290 * See Documentation/filesystems/caching/backend-api.txt for a complete
293 void fscache_object_init(struct fscache_object *object,
294 struct fscache_cookie *cookie,
295 struct fscache_cache *cache)
297 const struct fscache_transition *t;
299 atomic_inc(&cache->object_count);
301 object->state = STATE(WAIT_FOR_INIT);
302 object->oob_table = fscache_osm_init_oob;
303 object->flags = 1 << FSCACHE_OBJECT_IS_LIVE;
304 spin_lock_init(&object->lock);
305 INIT_LIST_HEAD(&object->cache_link);
306 INIT_HLIST_NODE(&object->cookie_link);
307 INIT_WORK(&object->work, fscache_object_work_func);
308 INIT_LIST_HEAD(&object->dependents);
309 INIT_LIST_HEAD(&object->dep_link);
310 INIT_LIST_HEAD(&object->pending_ops);
311 object->n_children = 0;
312 object->n_ops = object->n_in_progress = object->n_exclusive = 0;
314 object->store_limit = 0;
315 object->store_limit_l = 0;
316 object->cache = cache;
317 object->cookie = cookie;
318 object->parent = NULL;
320 object->oob_event_mask = 0;
321 for (t = object->oob_table; t->events; t++)
322 object->oob_event_mask |= t->events;
323 object->event_mask = object->oob_event_mask;
324 for (t = object->state->transitions; t->events; t++)
325 object->event_mask |= t->events;
327 EXPORT_SYMBOL(fscache_object_init);
330 * Abort object initialisation before we start it.
332 static const struct fscache_state *fscache_abort_initialisation(struct fscache_object *object,
335 struct fscache_cookie *cookie;
337 _enter("{OBJ%x},%d", object->debug_id, event);
339 object->oob_event_mask = 0;
340 clear_bit(FSCACHE_OBJECT_IS_LIVE, &object->flags);
342 fscache_dequeue_object(object);
344 spin_lock(&object->lock);
345 cookie = object->cookie;
346 clear_bit_unlock(FSCACHE_COOKIE_CREATING, &cookie->flags);
347 spin_unlock(&object->lock);
349 wake_up_bit(&cookie->flags, FSCACHE_COOKIE_CREATING);
351 return transit_to(KILL_OBJECT);
355 * initialise an object
356 * - check the specified object's parent to see if we can make use of it
357 * immediately to do a creation
358 * - we may need to start the process of creating a parent and we need to wait
359 * for the parent's lookup and creation to complete if it's not there yet
360 * - an object's cookie is pinned until we clear FSCACHE_COOKIE_CREATING on the
361 * leaf-most cookies of the object and all its children
363 static const struct fscache_state *fscache_initialise_object(struct fscache_object *object,
366 struct fscache_object *parent;
369 _enter("{OBJ%x},%d", object->debug_id, event);
371 ASSERT(list_empty(&object->dep_link));
373 parent = object->parent;
375 _leave(" [no parent]");
376 return transit_to(DETACH_FROM_COOKIE);
379 _debug("parent %s", parent->state->name);
381 if (fscache_object_is_dying(parent)) {
382 _leave(" [bad parent]");
383 return transit_to(DETACH_FROM_COOKIE);
386 if (fscache_object_is_available(parent)) {
388 return transit_to(PARENT_READY);
393 spin_lock(&parent->lock);
394 fscache_stat(&fscache_n_cop_grab_object);
396 if (fscache_object_is_live(parent) &&
397 object->cache->ops->grab_object(object)) {
398 list_add(&object->dep_link, &parent->dependents);
401 fscache_stat_d(&fscache_n_cop_grab_object);
402 spin_unlock(&parent->lock);
404 _leave(" [grab failed]");
405 return transit_to(DETACH_FROM_COOKIE);
408 /* fscache_acquire_non_index_cookie() uses this
409 * to wake the chain up */
410 fscache_raise_event(parent, FSCACHE_OBJECT_EV_NEW_CHILD);
412 return transit_to(WAIT_FOR_PARENT);
416 * Once the parent object is ready, we should kick off our lookup op.
418 static const struct fscache_state *fscache_parent_ready(struct fscache_object *object,
421 struct fscache_object *parent = object->parent;
423 _enter("{OBJ%x},%d", object->debug_id, event);
425 ASSERT(parent != NULL);
427 spin_lock(&parent->lock);
430 object->lookup_jif = jiffies;
431 spin_unlock(&parent->lock);
434 return transit_to(LOOK_UP_OBJECT);
438 * look an object up in the cache from which it was allocated
439 * - we hold an "access lock" on the parent object, so the parent object cannot
440 * be withdrawn by either party till we've finished
441 * - an object's cookie is pinned until we clear FSCACHE_COOKIE_CREATING on the
442 * leaf-most cookies of the object and all its children
444 static const struct fscache_state *fscache_look_up_object(struct fscache_object *object,
447 struct fscache_cookie *cookie = object->cookie;
448 struct fscache_object *parent = object->parent;
451 _enter("{OBJ%x},%d", object->debug_id, event);
453 object->oob_table = fscache_osm_lookup_oob;
455 ASSERT(parent != NULL);
456 ASSERTCMP(parent->n_ops, >, 0);
457 ASSERTCMP(parent->n_obj_ops, >, 0);
459 /* make sure the parent is still available */
460 ASSERT(fscache_object_is_available(parent));
462 if (fscache_object_is_dying(parent) ||
463 test_bit(FSCACHE_IOERROR, &object->cache->flags)) {
464 _leave(" [unavailable]");
465 return transit_to(LOOKUP_FAILURE);
468 _debug("LOOKUP \"%s/%s\" in \"%s\"",
469 parent->cookie->def->name, cookie->def->name,
470 object->cache->tag->name);
472 fscache_stat(&fscache_n_object_lookups);
473 fscache_stat(&fscache_n_cop_lookup_object);
474 ret = object->cache->ops->lookup_object(object);
475 fscache_stat_d(&fscache_n_cop_lookup_object);
477 if (test_bit(FSCACHE_OBJECT_EV_ERROR, &object->events))
478 set_bit(FSCACHE_COOKIE_UNAVAILABLE, &cookie->flags);
480 if (ret == -ETIMEDOUT) {
481 /* probably stuck behind another object, so move this one to
482 * the back of the queue */
483 fscache_stat(&fscache_n_object_lookups_timed_out);
484 _leave(" [timeout]");
490 return transit_to(LOOKUP_FAILURE);
494 return transit_to(OBJECT_AVAILABLE);
498 * fscache_object_lookup_negative - Note negative cookie lookup
499 * @object: Object pointing to cookie to mark
501 * Note negative lookup, permitting those waiting to read data from an already
502 * existing backing object to continue as there's no data for them to read.
504 void fscache_object_lookup_negative(struct fscache_object *object)
506 struct fscache_cookie *cookie = object->cookie;
508 _enter("{OBJ%x,%s}", object->debug_id, object->state->name);
510 if (!test_and_set_bit(FSCACHE_OBJECT_IS_LOOKED_UP, &object->flags)) {
511 fscache_stat(&fscache_n_object_lookups_negative);
513 /* Allow write requests to begin stacking up and read requests to begin
516 set_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
518 _debug("wake up lookup %p", &cookie->flags);
519 clear_bit_unlock(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags);
520 wake_up_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP);
524 EXPORT_SYMBOL(fscache_object_lookup_negative);
527 * fscache_obtained_object - Note successful object lookup or creation
528 * @object: Object pointing to cookie to mark
530 * Note successful lookup and/or creation, permitting those waiting to write
531 * data to a backing object to continue.
533 * Note that after calling this, an object's cookie may be relinquished by the
534 * netfs, and so must be accessed with object lock held.
536 void fscache_obtained_object(struct fscache_object *object)
538 struct fscache_cookie *cookie = object->cookie;
540 _enter("{OBJ%x,%s}", object->debug_id, object->state->name);
542 /* if we were still looking up, then we must have a positive lookup
543 * result, in which case there may be data available */
544 if (!test_and_set_bit(FSCACHE_OBJECT_IS_LOOKED_UP, &object->flags)) {
545 fscache_stat(&fscache_n_object_lookups_positive);
547 /* We do (presumably) have data */
548 clear_bit_unlock(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
550 /* Allow write requests to begin stacking up and read requests
551 * to begin shovelling data.
553 clear_bit_unlock(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags);
554 wake_up_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP);
556 fscache_stat(&fscache_n_object_created);
559 set_bit(FSCACHE_OBJECT_IS_AVAILABLE, &object->flags);
561 /* Permit __fscache_relinquish_cookie() to proceed */
562 clear_bit_unlock(FSCACHE_COOKIE_CREATING, &cookie->flags);
563 wake_up_bit(&cookie->flags, FSCACHE_COOKIE_CREATING);
567 EXPORT_SYMBOL(fscache_obtained_object);
570 * handle an object that has just become available
572 static const struct fscache_state *fscache_object_available(struct fscache_object *object,
575 struct fscache_cookie *cookie = object->cookie;
577 _enter("{OBJ%x},%d", object->debug_id, event);
579 object->oob_table = fscache_osm_run_oob;
581 spin_lock(&object->lock);
583 ASSERTIF(cookie, !test_bit(FSCACHE_COOKIE_CREATING, &object->cookie->flags));
585 fscache_done_parent_op(object);
586 if (object->n_in_progress == 0) {
587 if (object->n_ops > 0) {
588 ASSERTCMP(object->n_ops, >=, object->n_obj_ops);
589 fscache_start_operations(object);
591 ASSERT(list_empty(&object->pending_ops));
594 spin_unlock(&object->lock);
596 fscache_stat(&fscache_n_cop_lookup_complete);
597 object->cache->ops->lookup_complete(object);
598 fscache_stat_d(&fscache_n_cop_lookup_complete);
600 fscache_hist(fscache_obj_instantiate_histogram, object->lookup_jif);
601 fscache_stat(&fscache_n_object_avail);
604 return transit_to(JUMPSTART_DEPS);
608 * Wake up this object's dependent objects now that we've become available.
610 static const struct fscache_state *fscache_jumpstart_dependents(struct fscache_object *object,
613 _enter("{OBJ%x},%d", object->debug_id, event);
615 if (!fscache_enqueue_dependents(object, FSCACHE_OBJECT_EV_PARENT_READY))
616 return NO_TRANSIT; /* Not finished; requeue */
617 return transit_to(WAIT_FOR_CMD);
621 * Handle lookup or creation failute.
623 static const struct fscache_state *fscache_lookup_failure(struct fscache_object *object,
626 struct fscache_cookie *cookie;
627 bool wake_looking_up = false;
629 _enter("{OBJ%x},%d", object->debug_id, event);
631 object->oob_event_mask = 0;
633 fscache_stat(&fscache_n_cop_lookup_complete);
634 object->cache->ops->lookup_complete(object);
635 fscache_stat_d(&fscache_n_cop_lookup_complete);
637 spin_lock(&object->lock);
638 cookie = object->cookie;
639 set_bit(FSCACHE_COOKIE_UNAVAILABLE, &cookie->flags);
641 if (test_and_clear_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags))
642 wake_looking_up = true;
643 clear_bit_unlock(FSCACHE_COOKIE_CREATING, &cookie->flags);
645 spin_unlock(&object->lock);
648 wake_up_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP);
649 wake_up_bit(&cookie->flags, FSCACHE_COOKIE_CREATING);
651 fscache_done_parent_op(object);
652 return transit_to(KILL_OBJECT);
656 * Wait for completion of all active operations on this object and the death of
657 * all child objects of this object.
659 static const struct fscache_state *fscache_kill_object(struct fscache_object *object,
662 _enter("{OBJ%x,%d,%d},%d",
663 object->debug_id, object->n_ops, object->n_children, event);
665 object->oob_event_mask = 0;
667 spin_lock(&object->lock);
668 clear_bit(FSCACHE_OBJECT_IS_LIVE, &object->flags);
669 spin_unlock(&object->lock);
671 if (list_empty(&object->dependents) &&
672 object->n_ops == 0 &&
673 object->n_children == 0)
674 return object->cookie ?
675 transit_to(DETACH_FROM_COOKIE) : transit_to(DROP_OBJECT);
677 spin_lock(&object->lock);
678 fscache_start_operations(object);
679 spin_unlock(&object->lock);
681 if (!list_empty(&object->dependents))
682 return transit_to(KILL_DEPENDENTS);
684 return transit_to(WAIT_FOR_CLEARANCE);
688 * Kill dependent objects.
690 static const struct fscache_state *fscache_kill_dependents(struct fscache_object *object,
693 _enter("{OBJ%x},%d", object->debug_id, event);
695 if (!fscache_enqueue_dependents(object, FSCACHE_OBJECT_EV_KILL))
696 return NO_TRANSIT; /* Not finished */
697 return transit_to(WAIT_FOR_CLEARANCE);
701 * withdraw an object from active service
703 static const struct fscache_state *fscache_detach_from_cookie(struct fscache_object *object,
706 struct fscache_cookie *cookie;
707 bool detached = false, awaken = false;
709 _enter("{OBJ%x},%d", object->debug_id, event);
711 spin_lock(&object->lock);
712 cookie = object->cookie;
714 /* need to get the cookie lock before the object lock, starting
715 * from the object pointer */
716 atomic_inc(&cookie->usage);
717 spin_unlock(&object->lock);
719 spin_lock(&cookie->lock);
720 spin_lock(&object->lock);
722 if (object->cookie == cookie) {
723 hlist_del_init(&object->cookie_link);
724 object->cookie = NULL;
725 if (test_and_clear_bit(FSCACHE_COOKIE_INVALIDATING,
730 spin_unlock(&cookie->lock);
731 fscache_cookie_put(cookie);
733 fscache_cookie_put(cookie);
736 spin_unlock(&object->lock);
739 wake_up_bit(&cookie->flags, FSCACHE_COOKIE_INVALIDATING);
741 fscache_stat(&fscache_n_object_dead);
743 return transit_to(DROP_OBJECT);
747 * Drop an object's attachments
749 static const struct fscache_state *fscache_drop_object(struct fscache_object *object,
752 struct fscache_object *parent = object->parent;
753 struct fscache_cache *cache = object->cache;
755 _enter("{OBJ%x,%d},%d", object->debug_id, object->n_children, event);
757 ASSERTCMP(object->cookie, ==, NULL);
758 ASSERT(hlist_unhashed(&object->cookie_link));
760 /* Prevent a race with our last child, which has to signal EV_CLEARED
761 * before dropping our spinlock.
763 spin_lock(&object->lock);
764 spin_unlock(&object->lock);
766 /* Discard from the cache's collection of objects */
767 spin_lock(&cache->object_list_lock);
768 list_del_init(&object->cache_link);
769 spin_unlock(&cache->object_list_lock);
771 fscache_stat(&fscache_n_cop_drop_object);
772 cache->ops->drop_object(object);
773 fscache_stat_d(&fscache_n_cop_drop_object);
775 /* The parent object wants to know when all it dependents have gone */
777 _debug("release parent OBJ%x {%d}",
778 parent->debug_id, parent->n_children);
780 spin_lock(&parent->lock);
781 parent->n_children--;
782 if (parent->n_children == 0)
783 fscache_raise_event(parent, FSCACHE_OBJECT_EV_CLEARED);
784 spin_unlock(&parent->lock);
785 object->parent = NULL;
788 /* this just shifts the object release to the work processor */
789 fscache_put_object(object);
790 fscache_stat(&fscache_n_object_dead);
793 return transit_to(OBJECT_DEAD);
797 * get a ref on an object
799 static int fscache_get_object(struct fscache_object *object)
803 fscache_stat(&fscache_n_cop_grab_object);
804 ret = object->cache->ops->grab_object(object) ? 0 : -EAGAIN;
805 fscache_stat_d(&fscache_n_cop_grab_object);
810 * Discard a ref on an object
812 static void fscache_put_object(struct fscache_object *object)
814 fscache_stat(&fscache_n_cop_put_object);
815 object->cache->ops->put_object(object);
816 fscache_stat_d(&fscache_n_cop_put_object);
820 * enqueue an object for metadata-type processing
822 void fscache_enqueue_object(struct fscache_object *object)
824 _enter("{OBJ%x}", object->debug_id);
826 if (fscache_get_object(object) >= 0) {
827 wait_queue_head_t *cong_wq =
828 &get_cpu_var(fscache_object_cong_wait);
830 if (queue_work(fscache_object_wq, &object->work)) {
831 if (fscache_object_congested())
834 fscache_put_object(object);
836 put_cpu_var(fscache_object_cong_wait);
841 * fscache_object_sleep_till_congested - Sleep until object wq is congested
842 * @timeoutp: Scheduler sleep timeout
844 * Allow an object handler to sleep until the object workqueue is congested.
846 * The caller must set up a wake up event before calling this and must have set
847 * the appropriate sleep mode (such as TASK_UNINTERRUPTIBLE) and tested its own
848 * condition before calling this function as no test is made here.
850 * %true is returned if the object wq is congested, %false otherwise.
852 bool fscache_object_sleep_till_congested(signed long *timeoutp)
854 wait_queue_head_t *cong_wq = &__get_cpu_var(fscache_object_cong_wait);
857 if (fscache_object_congested())
860 add_wait_queue_exclusive(cong_wq, &wait);
861 if (!fscache_object_congested())
862 *timeoutp = schedule_timeout(*timeoutp);
863 finish_wait(cong_wq, &wait);
865 return fscache_object_congested();
867 EXPORT_SYMBOL_GPL(fscache_object_sleep_till_congested);
870 * Enqueue the dependents of an object for metadata-type processing.
872 * If we don't manage to finish the list before the scheduler wants to run
873 * again then return false immediately. We return true if the list was
876 static bool fscache_enqueue_dependents(struct fscache_object *object, int event)
878 struct fscache_object *dep;
881 _enter("{OBJ%x}", object->debug_id);
883 if (list_empty(&object->dependents))
886 spin_lock(&object->lock);
888 while (!list_empty(&object->dependents)) {
889 dep = list_entry(object->dependents.next,
890 struct fscache_object, dep_link);
891 list_del_init(&dep->dep_link);
893 fscache_raise_event(dep, event);
894 fscache_put_object(dep);
896 if (!list_empty(&object->dependents) && need_resched()) {
902 spin_unlock(&object->lock);
907 * remove an object from whatever queue it's waiting on
909 static void fscache_dequeue_object(struct fscache_object *object)
911 _enter("{OBJ%x}", object->debug_id);
913 if (!list_empty(&object->dep_link)) {
914 spin_lock(&object->parent->lock);
915 list_del_init(&object->dep_link);
916 spin_unlock(&object->parent->lock);
923 * fscache_check_aux - Ask the netfs whether an object on disk is still valid
924 * @object: The object to ask about
925 * @data: The auxiliary data for the object
926 * @datalen: The size of the auxiliary data
928 * This function consults the netfs about the coherency state of an object
930 enum fscache_checkaux fscache_check_aux(struct fscache_object *object,
931 const void *data, uint16_t datalen)
933 enum fscache_checkaux result;
935 if (!object->cookie->def->check_aux) {
936 fscache_stat(&fscache_n_checkaux_none);
937 return FSCACHE_CHECKAUX_OKAY;
940 result = object->cookie->def->check_aux(object->cookie->netfs_data,
943 /* entry okay as is */
944 case FSCACHE_CHECKAUX_OKAY:
945 fscache_stat(&fscache_n_checkaux_okay);
948 /* entry requires update */
949 case FSCACHE_CHECKAUX_NEEDS_UPDATE:
950 fscache_stat(&fscache_n_checkaux_update);
953 /* entry requires deletion */
954 case FSCACHE_CHECKAUX_OBSOLETE:
955 fscache_stat(&fscache_n_checkaux_obsolete);
964 EXPORT_SYMBOL(fscache_check_aux);
967 * Asynchronously invalidate an object.
969 static const struct fscache_state *_fscache_invalidate_object(struct fscache_object *object,
972 struct fscache_operation *op;
973 struct fscache_cookie *cookie = object->cookie;
975 _enter("{OBJ%x},%d", object->debug_id, event);
978 /* Reject any new read/write ops and abort any that are pending. */
979 fscache_invalidate_writes(cookie);
980 clear_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags);
981 fscache_cancel_all_ops(object);
983 /* Now we have to wait for in-progress reads and writes */
984 op = kzalloc(sizeof(*op), GFP_KERNEL);
986 clear_bit(FSCACHE_OBJECT_IS_LIVE, &object->flags);
988 return transit_to(KILL_OBJECT);
991 fscache_operation_init(op, object->cache->ops->invalidate_object, NULL);
992 op->flags = FSCACHE_OP_ASYNC | (1 << FSCACHE_OP_EXCLUSIVE);
994 spin_lock(&cookie->lock);
995 if (fscache_submit_exclusive_op(object, op) < 0)
996 goto submit_op_failed;
997 spin_unlock(&cookie->lock);
998 fscache_put_operation(op);
1000 /* Once we've completed the invalidation, we know there will be no data
1001 * stored in the cache and thus we can reinstate the data-check-skip
1004 set_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
1006 /* We can allow read and write requests to come in once again. They'll
1007 * queue up behind our exclusive invalidation operation.
1009 if (test_and_clear_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags))
1010 wake_up_bit(&cookie->flags, FSCACHE_COOKIE_INVALIDATING);
1012 return transit_to(UPDATE_OBJECT);
1015 clear_bit(FSCACHE_OBJECT_IS_LIVE, &object->flags);
1016 spin_unlock(&cookie->lock);
1019 return transit_to(KILL_OBJECT);
1022 static const struct fscache_state *fscache_invalidate_object(struct fscache_object *object,
1025 const struct fscache_state *s;
1027 fscache_stat(&fscache_n_invalidates_run);
1028 fscache_stat(&fscache_n_cop_invalidate_object);
1029 s = _fscache_invalidate_object(object, event);
1030 fscache_stat_d(&fscache_n_cop_invalidate_object);
1035 * Asynchronously update an object.
1037 static const struct fscache_state *fscache_update_object(struct fscache_object *object,
1040 _enter("{OBJ%x},%d", object->debug_id, event);
1042 fscache_stat(&fscache_n_updates_run);
1043 fscache_stat(&fscache_n_cop_update_object);
1044 object->cache->ops->update_object(object);
1045 fscache_stat_d(&fscache_n_cop_update_object);
1048 return transit_to(WAIT_FOR_CMD);