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);
34 #define __STATE_NAME(n) fscache_osm_##n
35 #define STATE(n) (&__STATE_NAME(n))
38 * Define a work state. Work states are execution states. No event processing
39 * is performed by them. The function attached to a work state returns a
40 * pointer indicating the next state to which the state machine should
41 * transition. Returning NO_TRANSIT repeats the current state, but goes back
42 * to the scheduler first.
44 #define WORK_STATE(n, sn, f) \
45 const struct fscache_state __STATE_NAME(n) = { \
52 * Returns from work states.
54 #define transit_to(state) ({ prefetch(&STATE(state)->work); STATE(state); })
56 #define NO_TRANSIT ((struct fscache_state *)NULL)
59 * Define a wait state. Wait states are event processing states. No execution
60 * is performed by them. Wait states are just tables of "if event X occurs,
61 * clear it and transition to state Y". The dispatcher returns to the
62 * scheduler if none of the events in which the wait state has an interest are
65 #define WAIT_STATE(n, sn, ...) \
66 const struct fscache_state __STATE_NAME(n) = { \
70 .transitions = { __VA_ARGS__, { 0, NULL } } \
73 #define TRANSIT_TO(state, emask) \
74 { .events = (emask), .transit_to = STATE(state) }
77 * The object state machine.
79 static WORK_STATE(INIT_OBJECT, "INIT", fscache_initialise_object);
80 static WORK_STATE(PARENT_READY, "PRDY", fscache_parent_ready);
81 static WORK_STATE(ABORT_INIT, "ABRT", fscache_abort_initialisation);
82 static WORK_STATE(LOOK_UP_OBJECT, "LOOK", fscache_look_up_object);
83 static WORK_STATE(CREATE_OBJECT, "CRTO", fscache_look_up_object);
84 static WORK_STATE(OBJECT_AVAILABLE, "AVBL", fscache_object_available);
85 static WORK_STATE(JUMPSTART_DEPS, "JUMP", fscache_jumpstart_dependents);
87 static WORK_STATE(INVALIDATE_OBJECT, "INVL", fscache_invalidate_object);
88 static WORK_STATE(UPDATE_OBJECT, "UPDT", fscache_update_object);
90 static WORK_STATE(LOOKUP_FAILURE, "LCFL", fscache_lookup_failure);
91 static WORK_STATE(KILL_OBJECT, "KILL", fscache_kill_object);
92 static WORK_STATE(KILL_DEPENDENTS, "KDEP", fscache_kill_dependents);
93 static WORK_STATE(DROP_OBJECT, "DROP", fscache_drop_object);
94 static WORK_STATE(OBJECT_DEAD, "DEAD", (void*)2UL);
96 static WAIT_STATE(WAIT_FOR_INIT, "?INI",
97 TRANSIT_TO(INIT_OBJECT, 1 << FSCACHE_OBJECT_EV_NEW_CHILD));
99 static WAIT_STATE(WAIT_FOR_PARENT, "?PRN",
100 TRANSIT_TO(PARENT_READY, 1 << FSCACHE_OBJECT_EV_PARENT_READY));
102 static WAIT_STATE(WAIT_FOR_CMD, "?CMD",
103 TRANSIT_TO(INVALIDATE_OBJECT, 1 << FSCACHE_OBJECT_EV_INVALIDATE),
104 TRANSIT_TO(UPDATE_OBJECT, 1 << FSCACHE_OBJECT_EV_UPDATE),
105 TRANSIT_TO(JUMPSTART_DEPS, 1 << FSCACHE_OBJECT_EV_NEW_CHILD));
107 static WAIT_STATE(WAIT_FOR_CLEARANCE, "?CLR",
108 TRANSIT_TO(KILL_OBJECT, 1 << FSCACHE_OBJECT_EV_CLEARED));
111 * Out-of-band event transition tables. These are for handling unexpected
112 * events, such as an I/O error. If an OOB event occurs, the state machine
113 * clears and disables the event and forces a transition to the nominated work
114 * state (acurrently executing work states will complete first).
116 * In such a situation, object->state remembers the state the machine should
117 * have been in/gone to and returning NO_TRANSIT returns to that.
119 static const struct fscache_transition fscache_osm_init_oob[] = {
120 TRANSIT_TO(ABORT_INIT,
121 (1 << FSCACHE_OBJECT_EV_ERROR) |
122 (1 << FSCACHE_OBJECT_EV_KILL)),
126 static const struct fscache_transition fscache_osm_lookup_oob[] = {
127 TRANSIT_TO(LOOKUP_FAILURE,
128 (1 << FSCACHE_OBJECT_EV_ERROR) |
129 (1 << FSCACHE_OBJECT_EV_KILL)),
133 static const struct fscache_transition fscache_osm_run_oob[] = {
134 TRANSIT_TO(KILL_OBJECT,
135 (1 << FSCACHE_OBJECT_EV_ERROR) |
136 (1 << FSCACHE_OBJECT_EV_KILL)),
140 static int fscache_get_object(struct fscache_object *);
141 static void fscache_put_object(struct fscache_object *);
142 static bool fscache_enqueue_dependents(struct fscache_object *, int);
143 static void fscache_dequeue_object(struct fscache_object *);
146 * we need to notify the parent when an op completes that we had outstanding
149 static inline void fscache_done_parent_op(struct fscache_object *object)
151 struct fscache_object *parent = object->parent;
153 _enter("OBJ%x {OBJ%x,%x}",
154 object->debug_id, parent->debug_id, parent->n_ops);
156 spin_lock_nested(&parent->lock, 1);
159 if (parent->n_ops == 0)
160 fscache_raise_event(parent, FSCACHE_OBJECT_EV_CLEARED);
161 spin_unlock(&parent->lock);
165 * Object state machine dispatcher.
167 static void fscache_object_sm_dispatcher(struct fscache_object *object)
169 const struct fscache_transition *t;
170 const struct fscache_state *state, *new_state;
171 unsigned long events, event_mask;
174 ASSERT(object != NULL);
176 _enter("{OBJ%x,%s,%lx}",
177 object->debug_id, object->state->name, object->events);
179 event_mask = object->event_mask;
181 object->event_mask = 0; /* Mask normal event handling */
182 state = object->state;
184 events = object->events;
186 /* Handle any out-of-band events (typically an error) */
187 if (events & object->oob_event_mask) {
188 _debug("{OBJ%x} oob %lx",
189 object->debug_id, events & object->oob_event_mask);
190 for (t = object->oob_table; t->events; t++) {
191 if (events & t->events) {
192 state = t->transit_to;
193 ASSERT(state->work != NULL);
194 event = fls(events & t->events) - 1;
195 __clear_bit(event, &object->oob_event_mask);
196 clear_bit(event, &object->events);
197 goto execute_work_state;
202 /* Wait states are just transition tables */
204 if (events & event_mask) {
205 for (t = state->transitions; t->events; t++) {
206 if (events & t->events) {
207 new_state = t->transit_to;
208 event = fls(events & t->events) - 1;
209 clear_bit(event, &object->events);
210 _debug("{OBJ%x} ev %d: %s -> %s",
211 object->debug_id, event,
212 state->name, new_state->name);
213 object->state = state = new_state;
214 goto execute_work_state;
218 /* The event mask didn't include all the tabled bits */
221 /* Randomly woke up */
226 _debug("{OBJ%x} exec %s", object->debug_id, state->name);
228 new_state = state->work(object, event);
230 if (new_state == NO_TRANSIT) {
231 _debug("{OBJ%x} %s notrans", object->debug_id, state->name);
232 fscache_enqueue_object(object);
233 event_mask = object->oob_event_mask;
237 _debug("{OBJ%x} %s -> %s",
238 object->debug_id, state->name, new_state->name);
239 object->state = state = new_state;
242 if (unlikely(state->work == ((void *)2UL))) {
249 /* Transited to wait state */
250 event_mask = object->oob_event_mask;
251 for (t = state->transitions; t->events; t++)
252 event_mask |= t->events;
255 object->event_mask = event_mask;
257 events = object->events;
258 if (events & event_mask)
260 _leave(" [msk %lx]", event_mask);
266 static void fscache_object_work_func(struct work_struct *work)
268 struct fscache_object *object =
269 container_of(work, struct fscache_object, work);
272 _enter("{OBJ%x}", object->debug_id);
275 fscache_object_sm_dispatcher(object);
276 fscache_hist(fscache_objs_histogram, start);
277 fscache_put_object(object);
281 * fscache_object_init - Initialise a cache object description
282 * @object: Object description
283 * @cookie: Cookie object will be attached to
284 * @cache: Cache in which backing object will be found
286 * Initialise a cache object description to its basic values.
288 * See Documentation/filesystems/caching/backend-api.txt for a complete
291 void fscache_object_init(struct fscache_object *object,
292 struct fscache_cookie *cookie,
293 struct fscache_cache *cache)
295 const struct fscache_transition *t;
297 atomic_inc(&cache->object_count);
299 object->state = STATE(WAIT_FOR_INIT);
300 object->oob_table = fscache_osm_init_oob;
301 object->flags = 1 << FSCACHE_OBJECT_IS_LIVE;
302 spin_lock_init(&object->lock);
303 INIT_LIST_HEAD(&object->cache_link);
304 INIT_HLIST_NODE(&object->cookie_link);
305 INIT_WORK(&object->work, fscache_object_work_func);
306 INIT_LIST_HEAD(&object->dependents);
307 INIT_LIST_HEAD(&object->dep_link);
308 INIT_LIST_HEAD(&object->pending_ops);
309 object->n_children = 0;
310 object->n_ops = object->n_in_progress = object->n_exclusive = 0;
312 object->store_limit = 0;
313 object->store_limit_l = 0;
314 object->cache = cache;
315 object->cookie = cookie;
316 atomic_inc(&cookie->usage);
317 object->parent = NULL;
318 #ifdef CONFIG_FSCACHE_OBJECT_LIST
319 RB_CLEAR_NODE(&object->objlist_link);
322 object->oob_event_mask = 0;
323 for (t = object->oob_table; t->events; t++)
324 object->oob_event_mask |= t->events;
325 object->event_mask = object->oob_event_mask;
326 for (t = object->state->transitions; t->events; t++)
327 object->event_mask |= t->events;
329 EXPORT_SYMBOL(fscache_object_init);
332 * Abort object initialisation before we start it.
334 static const struct fscache_state *fscache_abort_initialisation(struct fscache_object *object,
337 _enter("{OBJ%x},%d", object->debug_id, event);
339 object->oob_event_mask = 0;
340 fscache_dequeue_object(object);
341 return transit_to(KILL_OBJECT);
345 * initialise an object
346 * - check the specified object's parent to see if we can make use of it
347 * immediately to do a creation
348 * - we may need to start the process of creating a parent and we need to wait
349 * for the parent's lookup and creation to complete if it's not there yet
351 static const struct fscache_state *fscache_initialise_object(struct fscache_object *object,
354 struct fscache_object *parent;
357 _enter("{OBJ%x},%d", object->debug_id, event);
359 ASSERT(list_empty(&object->dep_link));
361 parent = object->parent;
363 _leave(" [no parent]");
364 return transit_to(DROP_OBJECT);
367 _debug("parent: %s of:%lx", parent->state->name, parent->flags);
369 if (fscache_object_is_dying(parent)) {
370 _leave(" [bad parent]");
371 return transit_to(DROP_OBJECT);
374 if (fscache_object_is_available(parent)) {
376 return transit_to(PARENT_READY);
381 spin_lock(&parent->lock);
382 fscache_stat(&fscache_n_cop_grab_object);
384 if (fscache_object_is_live(parent) &&
385 object->cache->ops->grab_object(object)) {
386 list_add(&object->dep_link, &parent->dependents);
389 fscache_stat_d(&fscache_n_cop_grab_object);
390 spin_unlock(&parent->lock);
392 _leave(" [grab failed]");
393 return transit_to(DROP_OBJECT);
396 /* fscache_acquire_non_index_cookie() uses this
397 * to wake the chain up */
398 fscache_raise_event(parent, FSCACHE_OBJECT_EV_NEW_CHILD);
400 return transit_to(WAIT_FOR_PARENT);
404 * Once the parent object is ready, we should kick off our lookup op.
406 static const struct fscache_state *fscache_parent_ready(struct fscache_object *object,
409 struct fscache_object *parent = object->parent;
411 _enter("{OBJ%x},%d", object->debug_id, event);
413 ASSERT(parent != NULL);
415 spin_lock(&parent->lock);
418 object->lookup_jif = jiffies;
419 spin_unlock(&parent->lock);
422 return transit_to(LOOK_UP_OBJECT);
426 * look an object up in the cache from which it was allocated
427 * - we hold an "access lock" on the parent object, so the parent object cannot
428 * be withdrawn by either party till we've finished
430 static const struct fscache_state *fscache_look_up_object(struct fscache_object *object,
433 struct fscache_cookie *cookie = object->cookie;
434 struct fscache_object *parent = object->parent;
437 _enter("{OBJ%x},%d", object->debug_id, event);
439 object->oob_table = fscache_osm_lookup_oob;
441 ASSERT(parent != NULL);
442 ASSERTCMP(parent->n_ops, >, 0);
443 ASSERTCMP(parent->n_obj_ops, >, 0);
445 /* make sure the parent is still available */
446 ASSERT(fscache_object_is_available(parent));
448 if (fscache_object_is_dying(parent) ||
449 test_bit(FSCACHE_IOERROR, &object->cache->flags) ||
450 !fscache_use_cookie(object)) {
451 _leave(" [unavailable]");
452 return transit_to(LOOKUP_FAILURE);
455 _debug("LOOKUP \"%s\" in \"%s\"",
456 cookie->def->name, object->cache->tag->name);
458 fscache_stat(&fscache_n_object_lookups);
459 fscache_stat(&fscache_n_cop_lookup_object);
460 ret = object->cache->ops->lookup_object(object);
461 fscache_stat_d(&fscache_n_cop_lookup_object);
463 fscache_unuse_cookie(object);
465 if (ret == -ETIMEDOUT) {
466 /* probably stuck behind another object, so move this one to
467 * the back of the queue */
468 fscache_stat(&fscache_n_object_lookups_timed_out);
469 _leave(" [timeout]");
475 return transit_to(LOOKUP_FAILURE);
479 return transit_to(OBJECT_AVAILABLE);
483 * fscache_object_lookup_negative - Note negative cookie lookup
484 * @object: Object pointing to cookie to mark
486 * Note negative lookup, permitting those waiting to read data from an already
487 * existing backing object to continue as there's no data for them to read.
489 void fscache_object_lookup_negative(struct fscache_object *object)
491 struct fscache_cookie *cookie = object->cookie;
493 _enter("{OBJ%x,%s}", object->debug_id, object->state->name);
495 if (!test_and_set_bit(FSCACHE_OBJECT_IS_LOOKED_UP, &object->flags)) {
496 fscache_stat(&fscache_n_object_lookups_negative);
498 /* Allow write requests to begin stacking up and read requests to begin
501 set_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
502 clear_bit(FSCACHE_COOKIE_UNAVAILABLE, &cookie->flags);
504 _debug("wake up lookup %p", &cookie->flags);
505 clear_bit_unlock(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags);
506 wake_up_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP);
510 EXPORT_SYMBOL(fscache_object_lookup_negative);
513 * fscache_obtained_object - Note successful object lookup or creation
514 * @object: Object pointing to cookie to mark
516 * Note successful lookup and/or creation, permitting those waiting to write
517 * data to a backing object to continue.
519 * Note that after calling this, an object's cookie may be relinquished by the
520 * netfs, and so must be accessed with object lock held.
522 void fscache_obtained_object(struct fscache_object *object)
524 struct fscache_cookie *cookie = object->cookie;
526 _enter("{OBJ%x,%s}", object->debug_id, object->state->name);
528 /* if we were still looking up, then we must have a positive lookup
529 * result, in which case there may be data available */
530 if (!test_and_set_bit(FSCACHE_OBJECT_IS_LOOKED_UP, &object->flags)) {
531 fscache_stat(&fscache_n_object_lookups_positive);
533 /* We do (presumably) have data */
534 clear_bit_unlock(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
535 clear_bit(FSCACHE_COOKIE_UNAVAILABLE, &cookie->flags);
537 /* Allow write requests to begin stacking up and read requests
538 * to begin shovelling data.
540 clear_bit_unlock(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags);
541 wake_up_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP);
543 fscache_stat(&fscache_n_object_created);
546 set_bit(FSCACHE_OBJECT_IS_AVAILABLE, &object->flags);
549 EXPORT_SYMBOL(fscache_obtained_object);
552 * handle an object that has just become available
554 static const struct fscache_state *fscache_object_available(struct fscache_object *object,
557 _enter("{OBJ%x},%d", object->debug_id, event);
559 object->oob_table = fscache_osm_run_oob;
561 spin_lock(&object->lock);
563 fscache_done_parent_op(object);
564 if (object->n_in_progress == 0) {
565 if (object->n_ops > 0) {
566 ASSERTCMP(object->n_ops, >=, object->n_obj_ops);
567 fscache_start_operations(object);
569 ASSERT(list_empty(&object->pending_ops));
572 spin_unlock(&object->lock);
574 fscache_stat(&fscache_n_cop_lookup_complete);
575 object->cache->ops->lookup_complete(object);
576 fscache_stat_d(&fscache_n_cop_lookup_complete);
578 fscache_hist(fscache_obj_instantiate_histogram, object->lookup_jif);
579 fscache_stat(&fscache_n_object_avail);
582 return transit_to(JUMPSTART_DEPS);
586 * Wake up this object's dependent objects now that we've become available.
588 static const struct fscache_state *fscache_jumpstart_dependents(struct fscache_object *object,
591 _enter("{OBJ%x},%d", object->debug_id, event);
593 if (!fscache_enqueue_dependents(object, FSCACHE_OBJECT_EV_PARENT_READY))
594 return NO_TRANSIT; /* Not finished; requeue */
595 return transit_to(WAIT_FOR_CMD);
599 * Handle lookup or creation failute.
601 static const struct fscache_state *fscache_lookup_failure(struct fscache_object *object,
604 struct fscache_cookie *cookie;
606 _enter("{OBJ%x},%d", object->debug_id, event);
608 object->oob_event_mask = 0;
610 fscache_stat(&fscache_n_cop_lookup_complete);
611 object->cache->ops->lookup_complete(object);
612 fscache_stat_d(&fscache_n_cop_lookup_complete);
614 cookie = object->cookie;
615 set_bit(FSCACHE_COOKIE_UNAVAILABLE, &cookie->flags);
616 if (test_and_clear_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags))
617 wake_up_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP);
619 fscache_done_parent_op(object);
620 return transit_to(KILL_OBJECT);
624 * Wait for completion of all active operations on this object and the death of
625 * all child objects of this object.
627 static const struct fscache_state *fscache_kill_object(struct fscache_object *object,
630 _enter("{OBJ%x,%d,%d},%d",
631 object->debug_id, object->n_ops, object->n_children, event);
633 clear_bit(FSCACHE_OBJECT_IS_LIVE, &object->flags);
634 object->oob_event_mask = 0;
636 if (list_empty(&object->dependents) &&
637 object->n_ops == 0 &&
638 object->n_children == 0)
639 return transit_to(DROP_OBJECT);
641 if (object->n_in_progress == 0) {
642 spin_lock(&object->lock);
643 if (object->n_ops > 0 && object->n_in_progress == 0)
644 fscache_start_operations(object);
645 spin_unlock(&object->lock);
648 if (!list_empty(&object->dependents))
649 return transit_to(KILL_DEPENDENTS);
651 return transit_to(WAIT_FOR_CLEARANCE);
655 * Kill dependent objects.
657 static const struct fscache_state *fscache_kill_dependents(struct fscache_object *object,
660 _enter("{OBJ%x},%d", object->debug_id, event);
662 if (!fscache_enqueue_dependents(object, FSCACHE_OBJECT_EV_KILL))
663 return NO_TRANSIT; /* Not finished */
664 return transit_to(WAIT_FOR_CLEARANCE);
668 * Drop an object's attachments
670 static const struct fscache_state *fscache_drop_object(struct fscache_object *object,
673 struct fscache_object *parent = object->parent;
674 struct fscache_cookie *cookie = object->cookie;
675 struct fscache_cache *cache = object->cache;
678 _enter("{OBJ%x,%d},%d", object->debug_id, object->n_children, event);
680 ASSERT(cookie != NULL);
681 ASSERT(!hlist_unhashed(&object->cookie_link));
683 /* Make sure the cookie no longer points here and that the netfs isn't
686 spin_lock(&cookie->lock);
687 hlist_del_init(&object->cookie_link);
688 if (hlist_empty(&cookie->backing_objects) &&
689 test_and_clear_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags))
691 spin_unlock(&cookie->lock);
694 wake_up_bit(&cookie->flags, FSCACHE_COOKIE_INVALIDATING);
696 /* Prevent a race with our last child, which has to signal EV_CLEARED
697 * before dropping our spinlock.
699 spin_lock(&object->lock);
700 spin_unlock(&object->lock);
702 /* Discard from the cache's collection of objects */
703 spin_lock(&cache->object_list_lock);
704 list_del_init(&object->cache_link);
705 spin_unlock(&cache->object_list_lock);
707 fscache_stat(&fscache_n_cop_drop_object);
708 cache->ops->drop_object(object);
709 fscache_stat_d(&fscache_n_cop_drop_object);
711 /* The parent object wants to know when all it dependents have gone */
713 _debug("release parent OBJ%x {%d}",
714 parent->debug_id, parent->n_children);
716 spin_lock(&parent->lock);
717 parent->n_children--;
718 if (parent->n_children == 0)
719 fscache_raise_event(parent, FSCACHE_OBJECT_EV_CLEARED);
720 spin_unlock(&parent->lock);
721 object->parent = NULL;
724 /* this just shifts the object release to the work processor */
725 fscache_put_object(object);
726 fscache_stat(&fscache_n_object_dead);
729 return transit_to(OBJECT_DEAD);
733 * get a ref on an object
735 static int fscache_get_object(struct fscache_object *object)
739 fscache_stat(&fscache_n_cop_grab_object);
740 ret = object->cache->ops->grab_object(object) ? 0 : -EAGAIN;
741 fscache_stat_d(&fscache_n_cop_grab_object);
746 * Discard a ref on an object
748 static void fscache_put_object(struct fscache_object *object)
750 fscache_stat(&fscache_n_cop_put_object);
751 object->cache->ops->put_object(object);
752 fscache_stat_d(&fscache_n_cop_put_object);
756 * fscache_object_destroy - Note that a cache object is about to be destroyed
757 * @object: The object to be destroyed
759 * Note the imminent destruction and deallocation of a cache object record.
761 void fscache_object_destroy(struct fscache_object *object)
763 fscache_objlist_remove(object);
765 /* We can get rid of the cookie now */
766 fscache_cookie_put(object->cookie);
767 object->cookie = NULL;
769 EXPORT_SYMBOL(fscache_object_destroy);
772 * enqueue an object for metadata-type processing
774 void fscache_enqueue_object(struct fscache_object *object)
776 _enter("{OBJ%x}", object->debug_id);
778 if (fscache_get_object(object) >= 0) {
779 wait_queue_head_t *cong_wq =
780 &get_cpu_var(fscache_object_cong_wait);
782 if (queue_work(fscache_object_wq, &object->work)) {
783 if (fscache_object_congested())
786 fscache_put_object(object);
788 put_cpu_var(fscache_object_cong_wait);
793 * fscache_object_sleep_till_congested - Sleep until object wq is congested
794 * @timeoutp: Scheduler sleep timeout
796 * Allow an object handler to sleep until the object workqueue is congested.
798 * The caller must set up a wake up event before calling this and must have set
799 * the appropriate sleep mode (such as TASK_UNINTERRUPTIBLE) and tested its own
800 * condition before calling this function as no test is made here.
802 * %true is returned if the object wq is congested, %false otherwise.
804 bool fscache_object_sleep_till_congested(signed long *timeoutp)
806 wait_queue_head_t *cong_wq = this_cpu_ptr(&fscache_object_cong_wait);
809 if (fscache_object_congested())
812 add_wait_queue_exclusive(cong_wq, &wait);
813 if (!fscache_object_congested())
814 *timeoutp = schedule_timeout(*timeoutp);
815 finish_wait(cong_wq, &wait);
817 return fscache_object_congested();
819 EXPORT_SYMBOL_GPL(fscache_object_sleep_till_congested);
822 * Enqueue the dependents of an object for metadata-type processing.
824 * If we don't manage to finish the list before the scheduler wants to run
825 * again then return false immediately. We return true if the list was
828 static bool fscache_enqueue_dependents(struct fscache_object *object, int event)
830 struct fscache_object *dep;
833 _enter("{OBJ%x}", object->debug_id);
835 if (list_empty(&object->dependents))
838 spin_lock(&object->lock);
840 while (!list_empty(&object->dependents)) {
841 dep = list_entry(object->dependents.next,
842 struct fscache_object, dep_link);
843 list_del_init(&dep->dep_link);
845 fscache_raise_event(dep, event);
846 fscache_put_object(dep);
848 if (!list_empty(&object->dependents) && need_resched()) {
854 spin_unlock(&object->lock);
859 * remove an object from whatever queue it's waiting on
861 static void fscache_dequeue_object(struct fscache_object *object)
863 _enter("{OBJ%x}", object->debug_id);
865 if (!list_empty(&object->dep_link)) {
866 spin_lock(&object->parent->lock);
867 list_del_init(&object->dep_link);
868 spin_unlock(&object->parent->lock);
875 * fscache_check_aux - Ask the netfs whether an object on disk is still valid
876 * @object: The object to ask about
877 * @data: The auxiliary data for the object
878 * @datalen: The size of the auxiliary data
880 * This function consults the netfs about the coherency state of an object.
881 * The caller must be holding a ref on cookie->n_active (held by
882 * fscache_look_up_object() on behalf of the cache backend during object lookup
885 enum fscache_checkaux fscache_check_aux(struct fscache_object *object,
886 const void *data, uint16_t datalen)
888 enum fscache_checkaux result;
890 if (!object->cookie->def->check_aux) {
891 fscache_stat(&fscache_n_checkaux_none);
892 return FSCACHE_CHECKAUX_OKAY;
895 result = object->cookie->def->check_aux(object->cookie->netfs_data,
898 /* entry okay as is */
899 case FSCACHE_CHECKAUX_OKAY:
900 fscache_stat(&fscache_n_checkaux_okay);
903 /* entry requires update */
904 case FSCACHE_CHECKAUX_NEEDS_UPDATE:
905 fscache_stat(&fscache_n_checkaux_update);
908 /* entry requires deletion */
909 case FSCACHE_CHECKAUX_OBSOLETE:
910 fscache_stat(&fscache_n_checkaux_obsolete);
919 EXPORT_SYMBOL(fscache_check_aux);
922 * Asynchronously invalidate an object.
924 static const struct fscache_state *_fscache_invalidate_object(struct fscache_object *object,
927 struct fscache_operation *op;
928 struct fscache_cookie *cookie = object->cookie;
930 _enter("{OBJ%x},%d", object->debug_id, event);
932 /* We're going to need the cookie. If the cookie is not available then
933 * retire the object instead.
935 if (!fscache_use_cookie(object)) {
936 ASSERT(object->cookie->stores.rnode == NULL);
937 set_bit(FSCACHE_OBJECT_RETIRED, &object->flags);
938 _leave(" [no cookie]");
939 return transit_to(KILL_OBJECT);
942 /* Reject any new read/write ops and abort any that are pending. */
943 fscache_invalidate_writes(cookie);
944 clear_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags);
945 fscache_cancel_all_ops(object);
947 /* Now we have to wait for in-progress reads and writes */
948 op = kzalloc(sizeof(*op), GFP_KERNEL);
952 fscache_operation_init(op, object->cache->ops->invalidate_object, NULL);
953 op->flags = FSCACHE_OP_ASYNC |
954 (1 << FSCACHE_OP_EXCLUSIVE) |
955 (1 << FSCACHE_OP_UNUSE_COOKIE);
957 spin_lock(&cookie->lock);
958 if (fscache_submit_exclusive_op(object, op) < 0)
959 goto submit_op_failed;
960 spin_unlock(&cookie->lock);
961 fscache_put_operation(op);
963 /* Once we've completed the invalidation, we know there will be no data
964 * stored in the cache and thus we can reinstate the data-check-skip
967 set_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
969 /* We can allow read and write requests to come in once again. They'll
970 * queue up behind our exclusive invalidation operation.
972 if (test_and_clear_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags))
973 wake_up_bit(&cookie->flags, FSCACHE_COOKIE_INVALIDATING);
975 return transit_to(UPDATE_OBJECT);
978 clear_bit(FSCACHE_OBJECT_IS_LIVE, &object->flags);
979 fscache_unuse_cookie(object);
981 return transit_to(KILL_OBJECT);
984 clear_bit(FSCACHE_OBJECT_IS_LIVE, &object->flags);
985 spin_unlock(&cookie->lock);
988 return transit_to(KILL_OBJECT);
991 static const struct fscache_state *fscache_invalidate_object(struct fscache_object *object,
994 const struct fscache_state *s;
996 fscache_stat(&fscache_n_invalidates_run);
997 fscache_stat(&fscache_n_cop_invalidate_object);
998 s = _fscache_invalidate_object(object, event);
999 fscache_stat_d(&fscache_n_cop_invalidate_object);
1004 * Asynchronously update an object.
1006 static const struct fscache_state *fscache_update_object(struct fscache_object *object,
1009 _enter("{OBJ%x},%d", object->debug_id, event);
1011 fscache_stat(&fscache_n_updates_run);
1012 fscache_stat(&fscache_n_cop_update_object);
1013 object->cache->ops->update_object(object);
1014 fscache_stat_d(&fscache_n_cop_update_object);
1017 return transit_to(WAIT_FOR_CMD);