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_object_dead(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(OBJECT_DEAD, "DEAD", fscache_object_dead);
97 static WAIT_STATE(WAIT_FOR_INIT, "?INI",
98 TRANSIT_TO(INIT_OBJECT, 1 << FSCACHE_OBJECT_EV_NEW_CHILD));
100 static WAIT_STATE(WAIT_FOR_PARENT, "?PRN",
101 TRANSIT_TO(PARENT_READY, 1 << FSCACHE_OBJECT_EV_PARENT_READY));
103 static WAIT_STATE(WAIT_FOR_CMD, "?CMD",
104 TRANSIT_TO(INVALIDATE_OBJECT, 1 << FSCACHE_OBJECT_EV_INVALIDATE),
105 TRANSIT_TO(UPDATE_OBJECT, 1 << FSCACHE_OBJECT_EV_UPDATE),
106 TRANSIT_TO(JUMPSTART_DEPS, 1 << FSCACHE_OBJECT_EV_NEW_CHILD));
108 static WAIT_STATE(WAIT_FOR_CLEARANCE, "?CLR",
109 TRANSIT_TO(KILL_OBJECT, 1 << FSCACHE_OBJECT_EV_CLEARED));
112 * Out-of-band event transition tables. These are for handling unexpected
113 * events, such as an I/O error. If an OOB event occurs, the state machine
114 * clears and disables the event and forces a transition to the nominated work
115 * state (acurrently executing work states will complete first).
117 * In such a situation, object->state remembers the state the machine should
118 * have been in/gone to and returning NO_TRANSIT returns to that.
120 static const struct fscache_transition fscache_osm_init_oob[] = {
121 TRANSIT_TO(ABORT_INIT,
122 (1 << FSCACHE_OBJECT_EV_ERROR) |
123 (1 << FSCACHE_OBJECT_EV_KILL)),
127 static const struct fscache_transition fscache_osm_lookup_oob[] = {
128 TRANSIT_TO(LOOKUP_FAILURE,
129 (1 << FSCACHE_OBJECT_EV_ERROR) |
130 (1 << FSCACHE_OBJECT_EV_KILL)),
134 static const struct fscache_transition fscache_osm_run_oob[] = {
135 TRANSIT_TO(KILL_OBJECT,
136 (1 << FSCACHE_OBJECT_EV_ERROR) |
137 (1 << FSCACHE_OBJECT_EV_KILL)),
141 static int fscache_get_object(struct fscache_object *,
142 enum fscache_obj_ref_trace);
143 static void fscache_put_object(struct fscache_object *,
144 enum fscache_obj_ref_trace);
145 static bool fscache_enqueue_dependents(struct fscache_object *, int);
146 static void fscache_dequeue_object(struct fscache_object *);
147 static void fscache_update_aux_data(struct fscache_object *);
150 * we need to notify the parent when an op completes that we had outstanding
153 static inline void fscache_done_parent_op(struct fscache_object *object)
155 struct fscache_object *parent = object->parent;
157 _enter("OBJ%x {OBJ%x,%x}",
158 object->debug_id, parent->debug_id, parent->n_ops);
160 spin_lock_nested(&parent->lock, 1);
163 if (parent->n_ops == 0)
164 fscache_raise_event(parent, FSCACHE_OBJECT_EV_CLEARED);
165 spin_unlock(&parent->lock);
169 * Object state machine dispatcher.
171 static void fscache_object_sm_dispatcher(struct fscache_object *object)
173 const struct fscache_transition *t;
174 const struct fscache_state *state, *new_state;
175 unsigned long events, event_mask;
179 ASSERT(object != NULL);
181 _enter("{OBJ%x,%s,%lx}",
182 object->debug_id, object->state->name, object->events);
184 event_mask = object->event_mask;
186 object->event_mask = 0; /* Mask normal event handling */
187 state = object->state;
189 events = object->events;
191 /* Handle any out-of-band events (typically an error) */
192 if (events & object->oob_event_mask) {
193 _debug("{OBJ%x} oob %lx",
194 object->debug_id, events & object->oob_event_mask);
196 for (t = object->oob_table; t->events; t++) {
197 if (events & t->events) {
198 state = t->transit_to;
199 ASSERT(state->work != NULL);
200 event = fls(events & t->events) - 1;
201 __clear_bit(event, &object->oob_event_mask);
202 clear_bit(event, &object->events);
203 goto execute_work_state;
209 /* Wait states are just transition tables */
211 if (events & event_mask) {
212 for (t = state->transitions; t->events; t++) {
213 if (events & t->events) {
214 new_state = t->transit_to;
215 event = fls(events & t->events) - 1;
216 trace_fscache_osm(object, state,
218 clear_bit(event, &object->events);
219 _debug("{OBJ%x} ev %d: %s -> %s",
220 object->debug_id, event,
221 state->name, new_state->name);
222 object->state = state = new_state;
223 goto execute_work_state;
227 /* The event mask didn't include all the tabled bits */
230 /* Randomly woke up */
235 _debug("{OBJ%x} exec %s", object->debug_id, state->name);
237 trace_fscache_osm(object, state, false, oob, event);
238 new_state = state->work(object, event);
240 if (new_state == NO_TRANSIT) {
241 _debug("{OBJ%x} %s notrans", object->debug_id, state->name);
242 if (unlikely(state == STATE(OBJECT_DEAD))) {
246 fscache_enqueue_object(object);
247 event_mask = object->oob_event_mask;
251 _debug("{OBJ%x} %s -> %s",
252 object->debug_id, state->name, new_state->name);
253 object->state = state = new_state;
256 if (unlikely(state == STATE(OBJECT_DEAD))) {
263 /* Transited to wait state */
264 event_mask = object->oob_event_mask;
265 for (t = state->transitions; t->events; t++)
266 event_mask |= t->events;
269 object->event_mask = event_mask;
271 events = object->events;
272 if (events & event_mask)
274 _leave(" [msk %lx]", event_mask);
280 static void fscache_object_work_func(struct work_struct *work)
282 struct fscache_object *object =
283 container_of(work, struct fscache_object, work);
286 _enter("{OBJ%x}", object->debug_id);
289 fscache_object_sm_dispatcher(object);
290 fscache_hist(fscache_objs_histogram, start);
291 fscache_put_object(object, fscache_obj_put_work);
295 * fscache_object_init - Initialise a cache object description
296 * @object: Object description
297 * @cookie: Cookie object will be attached to
298 * @cache: Cache in which backing object will be found
300 * Initialise a cache object description to its basic values.
302 * See Documentation/filesystems/caching/backend-api.txt for a complete
305 void fscache_object_init(struct fscache_object *object,
306 struct fscache_cookie *cookie,
307 struct fscache_cache *cache)
309 const struct fscache_transition *t;
311 atomic_inc(&cache->object_count);
313 object->state = STATE(WAIT_FOR_INIT);
314 object->oob_table = fscache_osm_init_oob;
315 object->flags = 1 << FSCACHE_OBJECT_IS_LIVE;
316 spin_lock_init(&object->lock);
317 INIT_LIST_HEAD(&object->cache_link);
318 INIT_HLIST_NODE(&object->cookie_link);
319 INIT_WORK(&object->work, fscache_object_work_func);
320 INIT_LIST_HEAD(&object->dependents);
321 INIT_LIST_HEAD(&object->dep_link);
322 INIT_LIST_HEAD(&object->pending_ops);
323 object->n_children = 0;
324 object->n_ops = object->n_in_progress = object->n_exclusive = 0;
326 object->store_limit = 0;
327 object->store_limit_l = 0;
328 object->cache = cache;
329 object->cookie = cookie;
330 fscache_cookie_get(cookie, fscache_cookie_get_attach_object);
331 object->parent = NULL;
332 #ifdef CONFIG_FSCACHE_OBJECT_LIST
333 RB_CLEAR_NODE(&object->objlist_link);
336 object->oob_event_mask = 0;
337 for (t = object->oob_table; t->events; t++)
338 object->oob_event_mask |= t->events;
339 object->event_mask = object->oob_event_mask;
340 for (t = object->state->transitions; t->events; t++)
341 object->event_mask |= t->events;
343 EXPORT_SYMBOL(fscache_object_init);
346 * Mark the object as no longer being live, making sure that we synchronise
347 * against op submission.
349 static inline void fscache_mark_object_dead(struct fscache_object *object)
351 spin_lock(&object->lock);
352 clear_bit(FSCACHE_OBJECT_IS_LIVE, &object->flags);
353 spin_unlock(&object->lock);
357 * Abort object initialisation before we start it.
359 static const struct fscache_state *fscache_abort_initialisation(struct fscache_object *object,
362 _enter("{OBJ%x},%d", object->debug_id, event);
364 object->oob_event_mask = 0;
365 fscache_dequeue_object(object);
366 return transit_to(KILL_OBJECT);
370 * initialise an object
371 * - check the specified object's parent to see if we can make use of it
372 * immediately to do a creation
373 * - we may need to start the process of creating a parent and we need to wait
374 * for the parent's lookup and creation to complete if it's not there yet
376 static const struct fscache_state *fscache_initialise_object(struct fscache_object *object,
379 struct fscache_object *parent;
382 _enter("{OBJ%x},%d", object->debug_id, event);
384 ASSERT(list_empty(&object->dep_link));
386 parent = object->parent;
388 _leave(" [no parent]");
389 return transit_to(DROP_OBJECT);
392 _debug("parent: %s of:%lx", parent->state->name, parent->flags);
394 if (fscache_object_is_dying(parent)) {
395 _leave(" [bad parent]");
396 return transit_to(DROP_OBJECT);
399 if (fscache_object_is_available(parent)) {
401 return transit_to(PARENT_READY);
406 spin_lock(&parent->lock);
407 fscache_stat(&fscache_n_cop_grab_object);
409 if (fscache_object_is_live(parent) &&
410 object->cache->ops->grab_object(object, fscache_obj_get_add_to_deps)) {
411 list_add(&object->dep_link, &parent->dependents);
414 fscache_stat_d(&fscache_n_cop_grab_object);
415 spin_unlock(&parent->lock);
417 _leave(" [grab failed]");
418 return transit_to(DROP_OBJECT);
421 /* fscache_acquire_non_index_cookie() uses this
422 * to wake the chain up */
423 fscache_raise_event(parent, FSCACHE_OBJECT_EV_NEW_CHILD);
425 return transit_to(WAIT_FOR_PARENT);
429 * Once the parent object is ready, we should kick off our lookup op.
431 static const struct fscache_state *fscache_parent_ready(struct fscache_object *object,
434 struct fscache_object *parent = object->parent;
436 _enter("{OBJ%x},%d", object->debug_id, event);
438 ASSERT(parent != NULL);
440 spin_lock(&parent->lock);
443 object->lookup_jif = jiffies;
444 spin_unlock(&parent->lock);
447 return transit_to(LOOK_UP_OBJECT);
451 * look an object up in the cache from which it was allocated
452 * - we hold an "access lock" on the parent object, so the parent object cannot
453 * be withdrawn by either party till we've finished
455 static const struct fscache_state *fscache_look_up_object(struct fscache_object *object,
458 struct fscache_cookie *cookie = object->cookie;
459 struct fscache_object *parent = object->parent;
462 _enter("{OBJ%x},%d", object->debug_id, event);
464 object->oob_table = fscache_osm_lookup_oob;
466 ASSERT(parent != NULL);
467 ASSERTCMP(parent->n_ops, >, 0);
468 ASSERTCMP(parent->n_obj_ops, >, 0);
470 /* make sure the parent is still available */
471 ASSERT(fscache_object_is_available(parent));
473 if (fscache_object_is_dying(parent) ||
474 test_bit(FSCACHE_IOERROR, &object->cache->flags) ||
475 !fscache_use_cookie(object)) {
476 _leave(" [unavailable]");
477 return transit_to(LOOKUP_FAILURE);
480 _debug("LOOKUP \"%s\" in \"%s\"",
481 cookie->def->name, object->cache->tag->name);
483 fscache_stat(&fscache_n_object_lookups);
484 fscache_stat(&fscache_n_cop_lookup_object);
485 ret = object->cache->ops->lookup_object(object);
486 fscache_stat_d(&fscache_n_cop_lookup_object);
488 fscache_unuse_cookie(object);
490 if (ret == -ETIMEDOUT) {
491 /* probably stuck behind another object, so move this one to
492 * the back of the queue */
493 fscache_stat(&fscache_n_object_lookups_timed_out);
494 _leave(" [timeout]");
500 return transit_to(LOOKUP_FAILURE);
504 return transit_to(OBJECT_AVAILABLE);
508 * fscache_object_lookup_negative - Note negative cookie lookup
509 * @object: Object pointing to cookie to mark
511 * Note negative lookup, permitting those waiting to read data from an already
512 * existing backing object to continue as there's no data for them to read.
514 void fscache_object_lookup_negative(struct fscache_object *object)
516 struct fscache_cookie *cookie = object->cookie;
518 _enter("{OBJ%x,%s}", object->debug_id, object->state->name);
520 if (!test_and_set_bit(FSCACHE_OBJECT_IS_LOOKED_UP, &object->flags)) {
521 fscache_stat(&fscache_n_object_lookups_negative);
523 /* Allow write requests to begin stacking up and read requests to begin
526 set_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
527 clear_bit(FSCACHE_COOKIE_UNAVAILABLE, &cookie->flags);
529 _debug("wake up lookup %p", &cookie->flags);
530 clear_bit_unlock(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags);
531 wake_up_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP);
535 EXPORT_SYMBOL(fscache_object_lookup_negative);
538 * fscache_obtained_object - Note successful object lookup or creation
539 * @object: Object pointing to cookie to mark
541 * Note successful lookup and/or creation, permitting those waiting to write
542 * data to a backing object to continue.
544 * Note that after calling this, an object's cookie may be relinquished by the
545 * netfs, and so must be accessed with object lock held.
547 void fscache_obtained_object(struct fscache_object *object)
549 struct fscache_cookie *cookie = object->cookie;
551 _enter("{OBJ%x,%s}", object->debug_id, object->state->name);
553 /* if we were still looking up, then we must have a positive lookup
554 * result, in which case there may be data available */
555 if (!test_and_set_bit(FSCACHE_OBJECT_IS_LOOKED_UP, &object->flags)) {
556 fscache_stat(&fscache_n_object_lookups_positive);
558 /* We do (presumably) have data */
559 clear_bit_unlock(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
560 clear_bit(FSCACHE_COOKIE_UNAVAILABLE, &cookie->flags);
562 /* Allow write requests to begin stacking up and read requests
563 * to begin shovelling data.
565 clear_bit_unlock(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags);
566 wake_up_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP);
568 fscache_stat(&fscache_n_object_created);
571 set_bit(FSCACHE_OBJECT_IS_AVAILABLE, &object->flags);
574 EXPORT_SYMBOL(fscache_obtained_object);
577 * handle an object that has just become available
579 static const struct fscache_state *fscache_object_available(struct fscache_object *object,
582 _enter("{OBJ%x},%d", object->debug_id, event);
584 object->oob_table = fscache_osm_run_oob;
586 spin_lock(&object->lock);
588 fscache_done_parent_op(object);
589 if (object->n_in_progress == 0) {
590 if (object->n_ops > 0) {
591 ASSERTCMP(object->n_ops, >=, object->n_obj_ops);
592 fscache_start_operations(object);
594 ASSERT(list_empty(&object->pending_ops));
597 spin_unlock(&object->lock);
599 fscache_stat(&fscache_n_cop_lookup_complete);
600 object->cache->ops->lookup_complete(object);
601 fscache_stat_d(&fscache_n_cop_lookup_complete);
603 fscache_hist(fscache_obj_instantiate_histogram, object->lookup_jif);
604 fscache_stat(&fscache_n_object_avail);
607 return transit_to(JUMPSTART_DEPS);
611 * Wake up this object's dependent objects now that we've become available.
613 static const struct fscache_state *fscache_jumpstart_dependents(struct fscache_object *object,
616 _enter("{OBJ%x},%d", object->debug_id, event);
618 if (!fscache_enqueue_dependents(object, FSCACHE_OBJECT_EV_PARENT_READY))
619 return NO_TRANSIT; /* Not finished; requeue */
620 return transit_to(WAIT_FOR_CMD);
624 * Handle lookup or creation failute.
626 static const struct fscache_state *fscache_lookup_failure(struct fscache_object *object,
629 struct fscache_cookie *cookie;
631 _enter("{OBJ%x},%d", object->debug_id, event);
633 object->oob_event_mask = 0;
635 fscache_stat(&fscache_n_cop_lookup_complete);
636 object->cache->ops->lookup_complete(object);
637 fscache_stat_d(&fscache_n_cop_lookup_complete);
639 set_bit(FSCACHE_OBJECT_KILLED_BY_CACHE, &object->flags);
641 cookie = object->cookie;
642 set_bit(FSCACHE_COOKIE_UNAVAILABLE, &cookie->flags);
643 if (test_and_clear_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags))
644 wake_up_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP);
646 fscache_done_parent_op(object);
647 return transit_to(KILL_OBJECT);
651 * Wait for completion of all active operations on this object and the death of
652 * all child objects of this object.
654 static const struct fscache_state *fscache_kill_object(struct fscache_object *object,
657 _enter("{OBJ%x,%d,%d},%d",
658 object->debug_id, object->n_ops, object->n_children, event);
660 fscache_mark_object_dead(object);
661 object->oob_event_mask = 0;
663 if (test_bit(FSCACHE_OBJECT_RETIRED, &object->flags)) {
664 /* Reject any new read/write ops and abort any that are pending. */
665 clear_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags);
666 fscache_cancel_all_ops(object);
669 if (list_empty(&object->dependents) &&
670 object->n_ops == 0 &&
671 object->n_children == 0)
672 return transit_to(DROP_OBJECT);
674 if (object->n_in_progress == 0) {
675 spin_lock(&object->lock);
676 if (object->n_ops > 0 && object->n_in_progress == 0)
677 fscache_start_operations(object);
678 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 * Drop an object's attachments
703 static const struct fscache_state *fscache_drop_object(struct fscache_object *object,
706 struct fscache_object *parent = object->parent;
707 struct fscache_cookie *cookie = object->cookie;
708 struct fscache_cache *cache = object->cache;
711 _enter("{OBJ%x,%d},%d", object->debug_id, object->n_children, event);
713 ASSERT(cookie != NULL);
714 ASSERT(!hlist_unhashed(&object->cookie_link));
716 if (test_bit(FSCACHE_COOKIE_AUX_UPDATED, &cookie->flags)) {
717 _debug("final update");
718 fscache_update_aux_data(object);
721 /* Make sure the cookie no longer points here and that the netfs isn't
724 spin_lock(&cookie->lock);
725 hlist_del_init(&object->cookie_link);
726 if (hlist_empty(&cookie->backing_objects) &&
727 test_and_clear_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags))
729 spin_unlock(&cookie->lock);
732 wake_up_bit(&cookie->flags, FSCACHE_COOKIE_INVALIDATING);
733 if (test_and_clear_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags))
734 wake_up_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP);
737 /* Prevent a race with our last child, which has to signal EV_CLEARED
738 * before dropping our spinlock.
740 spin_lock(&object->lock);
741 spin_unlock(&object->lock);
743 /* Discard from the cache's collection of objects */
744 spin_lock(&cache->object_list_lock);
745 list_del_init(&object->cache_link);
746 spin_unlock(&cache->object_list_lock);
748 fscache_stat(&fscache_n_cop_drop_object);
749 cache->ops->drop_object(object);
750 fscache_stat_d(&fscache_n_cop_drop_object);
752 /* The parent object wants to know when all it dependents have gone */
754 _debug("release parent OBJ%x {%d}",
755 parent->debug_id, parent->n_children);
757 spin_lock(&parent->lock);
758 parent->n_children--;
759 if (parent->n_children == 0)
760 fscache_raise_event(parent, FSCACHE_OBJECT_EV_CLEARED);
761 spin_unlock(&parent->lock);
762 object->parent = NULL;
765 /* this just shifts the object release to the work processor */
766 fscache_put_object(object, fscache_obj_put_drop_obj);
767 fscache_stat(&fscache_n_object_dead);
770 return transit_to(OBJECT_DEAD);
774 * get a ref on an object
776 static int fscache_get_object(struct fscache_object *object,
777 enum fscache_obj_ref_trace why)
781 fscache_stat(&fscache_n_cop_grab_object);
782 ret = object->cache->ops->grab_object(object, why) ? 0 : -EAGAIN;
783 fscache_stat_d(&fscache_n_cop_grab_object);
788 * Discard a ref on an object
790 static void fscache_put_object(struct fscache_object *object,
791 enum fscache_obj_ref_trace why)
793 fscache_stat(&fscache_n_cop_put_object);
794 object->cache->ops->put_object(object, why);
795 fscache_stat_d(&fscache_n_cop_put_object);
799 * fscache_object_destroy - Note that a cache object is about to be destroyed
800 * @object: The object to be destroyed
802 * Note the imminent destruction and deallocation of a cache object record.
804 void fscache_object_destroy(struct fscache_object *object)
806 fscache_objlist_remove(object);
808 /* We can get rid of the cookie now */
809 fscache_cookie_put(object->cookie, fscache_cookie_put_object);
810 object->cookie = NULL;
812 EXPORT_SYMBOL(fscache_object_destroy);
815 * enqueue an object for metadata-type processing
817 void fscache_enqueue_object(struct fscache_object *object)
819 _enter("{OBJ%x}", object->debug_id);
821 if (fscache_get_object(object, fscache_obj_get_queue) >= 0) {
822 wait_queue_head_t *cong_wq =
823 &get_cpu_var(fscache_object_cong_wait);
825 if (queue_work(fscache_object_wq, &object->work)) {
826 if (fscache_object_congested())
829 fscache_put_object(object, fscache_obj_put_queue);
831 put_cpu_var(fscache_object_cong_wait);
836 * fscache_object_sleep_till_congested - Sleep until object wq is congested
837 * @timeoutp: Scheduler sleep timeout
839 * Allow an object handler to sleep until the object workqueue is congested.
841 * The caller must set up a wake up event before calling this and must have set
842 * the appropriate sleep mode (such as TASK_UNINTERRUPTIBLE) and tested its own
843 * condition before calling this function as no test is made here.
845 * %true is returned if the object wq is congested, %false otherwise.
847 bool fscache_object_sleep_till_congested(signed long *timeoutp)
849 wait_queue_head_t *cong_wq = this_cpu_ptr(&fscache_object_cong_wait);
852 if (fscache_object_congested())
855 add_wait_queue_exclusive(cong_wq, &wait);
856 if (!fscache_object_congested())
857 *timeoutp = schedule_timeout(*timeoutp);
858 finish_wait(cong_wq, &wait);
860 return fscache_object_congested();
862 EXPORT_SYMBOL_GPL(fscache_object_sleep_till_congested);
865 * Enqueue the dependents of an object for metadata-type processing.
867 * If we don't manage to finish the list before the scheduler wants to run
868 * again then return false immediately. We return true if the list was
871 static bool fscache_enqueue_dependents(struct fscache_object *object, int event)
873 struct fscache_object *dep;
876 _enter("{OBJ%x}", object->debug_id);
878 if (list_empty(&object->dependents))
881 spin_lock(&object->lock);
883 while (!list_empty(&object->dependents)) {
884 dep = list_entry(object->dependents.next,
885 struct fscache_object, dep_link);
886 list_del_init(&dep->dep_link);
888 fscache_raise_event(dep, event);
889 fscache_put_object(dep, fscache_obj_put_enq_dep);
891 if (!list_empty(&object->dependents) && need_resched()) {
897 spin_unlock(&object->lock);
902 * remove an object from whatever queue it's waiting on
904 static void fscache_dequeue_object(struct fscache_object *object)
906 _enter("{OBJ%x}", object->debug_id);
908 if (!list_empty(&object->dep_link)) {
909 spin_lock(&object->parent->lock);
910 list_del_init(&object->dep_link);
911 spin_unlock(&object->parent->lock);
918 * fscache_check_aux - Ask the netfs whether an object on disk is still valid
919 * @object: The object to ask about
920 * @data: The auxiliary data for the object
921 * @datalen: The size of the auxiliary data
923 * This function consults the netfs about the coherency state of an object.
924 * The caller must be holding a ref on cookie->n_active (held by
925 * fscache_look_up_object() on behalf of the cache backend during object lookup
928 enum fscache_checkaux fscache_check_aux(struct fscache_object *object,
929 const void *data, uint16_t datalen,
932 enum fscache_checkaux result;
934 if (!object->cookie->def->check_aux) {
935 fscache_stat(&fscache_n_checkaux_none);
936 return FSCACHE_CHECKAUX_OKAY;
939 result = object->cookie->def->check_aux(object->cookie->netfs_data,
940 data, datalen, object_size);
942 /* entry okay as is */
943 case FSCACHE_CHECKAUX_OKAY:
944 fscache_stat(&fscache_n_checkaux_okay);
947 /* entry requires update */
948 case FSCACHE_CHECKAUX_NEEDS_UPDATE:
949 fscache_stat(&fscache_n_checkaux_update);
952 /* entry requires deletion */
953 case FSCACHE_CHECKAUX_OBSOLETE:
954 fscache_stat(&fscache_n_checkaux_obsolete);
963 EXPORT_SYMBOL(fscache_check_aux);
966 * Asynchronously invalidate an object.
968 static const struct fscache_state *_fscache_invalidate_object(struct fscache_object *object,
971 struct fscache_operation *op;
972 struct fscache_cookie *cookie = object->cookie;
974 _enter("{OBJ%x},%d", object->debug_id, event);
976 /* We're going to need the cookie. If the cookie is not available then
977 * retire the object instead.
979 if (!fscache_use_cookie(object)) {
980 ASSERT(radix_tree_empty(&object->cookie->stores));
981 set_bit(FSCACHE_OBJECT_RETIRED, &object->flags);
982 _leave(" [no cookie]");
983 return transit_to(KILL_OBJECT);
986 /* Reject any new read/write ops and abort any that are pending. */
987 fscache_invalidate_writes(cookie);
988 clear_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags);
989 fscache_cancel_all_ops(object);
991 /* Now we have to wait for in-progress reads and writes */
992 op = kzalloc(sizeof(*op), GFP_KERNEL);
996 fscache_operation_init(cookie, op, object->cache->ops->invalidate_object,
998 op->flags = FSCACHE_OP_ASYNC |
999 (1 << FSCACHE_OP_EXCLUSIVE) |
1000 (1 << FSCACHE_OP_UNUSE_COOKIE);
1001 trace_fscache_page_op(cookie, NULL, op, fscache_page_op_invalidate);
1003 spin_lock(&cookie->lock);
1004 if (fscache_submit_exclusive_op(object, op) < 0)
1005 goto submit_op_failed;
1006 spin_unlock(&cookie->lock);
1007 fscache_put_operation(op);
1009 /* Once we've completed the invalidation, we know there will be no data
1010 * stored in the cache and thus we can reinstate the data-check-skip
1013 set_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
1015 /* We can allow read and write requests to come in once again. They'll
1016 * queue up behind our exclusive invalidation operation.
1018 if (test_and_clear_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags))
1019 wake_up_bit(&cookie->flags, FSCACHE_COOKIE_INVALIDATING);
1021 return transit_to(UPDATE_OBJECT);
1024 fscache_mark_object_dead(object);
1025 fscache_unuse_cookie(object);
1026 _leave(" [ENOMEM]");
1027 return transit_to(KILL_OBJECT);
1030 fscache_mark_object_dead(object);
1031 spin_unlock(&cookie->lock);
1032 fscache_unuse_cookie(object);
1035 return transit_to(KILL_OBJECT);
1038 static const struct fscache_state *fscache_invalidate_object(struct fscache_object *object,
1041 const struct fscache_state *s;
1043 fscache_stat(&fscache_n_invalidates_run);
1044 fscache_stat(&fscache_n_cop_invalidate_object);
1045 s = _fscache_invalidate_object(object, event);
1046 fscache_stat_d(&fscache_n_cop_invalidate_object);
1051 * Update auxiliary data.
1053 static void fscache_update_aux_data(struct fscache_object *object)
1055 fscache_stat(&fscache_n_updates_run);
1056 fscache_stat(&fscache_n_cop_update_object);
1057 object->cache->ops->update_object(object);
1058 fscache_stat_d(&fscache_n_cop_update_object);
1062 * Asynchronously update an object.
1064 static const struct fscache_state *fscache_update_object(struct fscache_object *object,
1067 _enter("{OBJ%x},%d", object->debug_id, event);
1069 fscache_update_aux_data(object);
1072 return transit_to(WAIT_FOR_CMD);
1076 * fscache_object_retrying_stale - Note retrying stale object
1077 * @object: The object that will be retried
1079 * Note that an object lookup found an on-disk object that was adjudged to be
1080 * stale and has been deleted. The lookup will be retried.
1082 void fscache_object_retrying_stale(struct fscache_object *object)
1084 fscache_stat(&fscache_n_cache_no_space_reject);
1086 EXPORT_SYMBOL(fscache_object_retrying_stale);
1089 * fscache_object_mark_killed - Note that an object was killed
1090 * @object: The object that was culled
1091 * @why: The reason the object was killed.
1093 * Note that an object was killed. Returns true if the object was
1094 * already marked killed, false if it wasn't.
1096 void fscache_object_mark_killed(struct fscache_object *object,
1097 enum fscache_why_object_killed why)
1099 if (test_and_set_bit(FSCACHE_OBJECT_KILLED_BY_CACHE, &object->flags)) {
1100 pr_err("Error: Object already killed by cache [%s]\n",
1101 object->cache->identifier);
1106 case FSCACHE_OBJECT_NO_SPACE:
1107 fscache_stat(&fscache_n_cache_no_space_reject);
1109 case FSCACHE_OBJECT_IS_STALE:
1110 fscache_stat(&fscache_n_cache_stale_objects);
1112 case FSCACHE_OBJECT_WAS_RETIRED:
1113 fscache_stat(&fscache_n_cache_retired_objects);
1115 case FSCACHE_OBJECT_WAS_CULLED:
1116 fscache_stat(&fscache_n_cache_culled_objects);
1120 EXPORT_SYMBOL(fscache_object_mark_killed);
1123 * The object is dead. We can get here if an object gets queued by an event
1124 * that would lead to its death (such as EV_KILL) when the dispatcher is
1125 * already running (and so can be requeued) but hasn't yet cleared the event
1128 static const struct fscache_state *fscache_object_dead(struct fscache_object *object,
1131 if (!test_and_set_bit(FSCACHE_OBJECT_RUN_AFTER_DEAD,
1135 WARN(true, "FS-Cache object redispatched after death");