1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* FS-Cache object state machine handler
4 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
7 * See Documentation/filesystems/caching/object.rst for a description of the
8 * object state machine and the in-kernel representations.
11 #define FSCACHE_DEBUG_LEVEL COOKIE
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/prefetch.h>
17 static const struct fscache_state *fscache_abort_initialisation(struct fscache_object *, int);
18 static const struct fscache_state *fscache_kill_dependents(struct fscache_object *, int);
19 static const struct fscache_state *fscache_drop_object(struct fscache_object *, int);
20 static const struct fscache_state *fscache_initialise_object(struct fscache_object *, int);
21 static const struct fscache_state *fscache_invalidate_object(struct fscache_object *, int);
22 static const struct fscache_state *fscache_jumpstart_dependents(struct fscache_object *, int);
23 static const struct fscache_state *fscache_kill_object(struct fscache_object *, int);
24 static const struct fscache_state *fscache_lookup_failure(struct fscache_object *, int);
25 static const struct fscache_state *fscache_look_up_object(struct fscache_object *, int);
26 static const struct fscache_state *fscache_object_available(struct fscache_object *, int);
27 static const struct fscache_state *fscache_parent_ready(struct fscache_object *, int);
28 static const struct fscache_state *fscache_update_object(struct fscache_object *, int);
29 static const struct fscache_state *fscache_object_dead(struct fscache_object *, int);
31 #define __STATE_NAME(n) fscache_osm_##n
32 #define STATE(n) (&__STATE_NAME(n))
35 * Define a work state. Work states are execution states. No event processing
36 * is performed by them. The function attached to a work state returns a
37 * pointer indicating the next state to which the state machine should
38 * transition. Returning NO_TRANSIT repeats the current state, but goes back
39 * to the scheduler first.
41 #define WORK_STATE(n, sn, f) \
42 const struct fscache_state __STATE_NAME(n) = { \
49 * Returns from work states.
51 #define transit_to(state) ({ prefetch(&STATE(state)->work); STATE(state); })
53 #define NO_TRANSIT ((struct fscache_state *)NULL)
56 * Define a wait state. Wait states are event processing states. No execution
57 * is performed by them. Wait states are just tables of "if event X occurs,
58 * clear it and transition to state Y". The dispatcher returns to the
59 * scheduler if none of the events in which the wait state has an interest are
62 #define WAIT_STATE(n, sn, ...) \
63 const struct fscache_state __STATE_NAME(n) = { \
67 .transitions = { __VA_ARGS__, { 0, NULL } } \
70 #define TRANSIT_TO(state, emask) \
71 { .events = (emask), .transit_to = STATE(state) }
74 * The object state machine.
76 static WORK_STATE(INIT_OBJECT, "INIT", fscache_initialise_object);
77 static WORK_STATE(PARENT_READY, "PRDY", fscache_parent_ready);
78 static WORK_STATE(ABORT_INIT, "ABRT", fscache_abort_initialisation);
79 static WORK_STATE(LOOK_UP_OBJECT, "LOOK", fscache_look_up_object);
80 static WORK_STATE(CREATE_OBJECT, "CRTO", fscache_look_up_object);
81 static WORK_STATE(OBJECT_AVAILABLE, "AVBL", fscache_object_available);
82 static WORK_STATE(JUMPSTART_DEPS, "JUMP", fscache_jumpstart_dependents);
84 static WORK_STATE(INVALIDATE_OBJECT, "INVL", fscache_invalidate_object);
85 static WORK_STATE(UPDATE_OBJECT, "UPDT", fscache_update_object);
87 static WORK_STATE(LOOKUP_FAILURE, "LCFL", fscache_lookup_failure);
88 static WORK_STATE(KILL_OBJECT, "KILL", fscache_kill_object);
89 static WORK_STATE(KILL_DEPENDENTS, "KDEP", fscache_kill_dependents);
90 static WORK_STATE(DROP_OBJECT, "DROP", fscache_drop_object);
91 static WORK_STATE(OBJECT_DEAD, "DEAD", fscache_object_dead);
93 static WAIT_STATE(WAIT_FOR_INIT, "?INI",
94 TRANSIT_TO(INIT_OBJECT, 1 << FSCACHE_OBJECT_EV_NEW_CHILD));
96 static WAIT_STATE(WAIT_FOR_PARENT, "?PRN",
97 TRANSIT_TO(PARENT_READY, 1 << FSCACHE_OBJECT_EV_PARENT_READY));
99 static WAIT_STATE(WAIT_FOR_CMD, "?CMD",
100 TRANSIT_TO(INVALIDATE_OBJECT, 1 << FSCACHE_OBJECT_EV_INVALIDATE),
101 TRANSIT_TO(UPDATE_OBJECT, 1 << FSCACHE_OBJECT_EV_UPDATE),
102 TRANSIT_TO(JUMPSTART_DEPS, 1 << FSCACHE_OBJECT_EV_NEW_CHILD));
104 static WAIT_STATE(WAIT_FOR_CLEARANCE, "?CLR",
105 TRANSIT_TO(KILL_OBJECT, 1 << FSCACHE_OBJECT_EV_CLEARED));
108 * Out-of-band event transition tables. These are for handling unexpected
109 * events, such as an I/O error. If an OOB event occurs, the state machine
110 * clears and disables the event and forces a transition to the nominated work
111 * state (acurrently executing work states will complete first).
113 * In such a situation, object->state remembers the state the machine should
114 * have been in/gone to and returning NO_TRANSIT returns to that.
116 static const struct fscache_transition fscache_osm_init_oob[] = {
117 TRANSIT_TO(ABORT_INIT,
118 (1 << FSCACHE_OBJECT_EV_ERROR) |
119 (1 << FSCACHE_OBJECT_EV_KILL)),
123 static const struct fscache_transition fscache_osm_lookup_oob[] = {
124 TRANSIT_TO(LOOKUP_FAILURE,
125 (1 << FSCACHE_OBJECT_EV_ERROR) |
126 (1 << FSCACHE_OBJECT_EV_KILL)),
130 static const struct fscache_transition fscache_osm_run_oob[] = {
131 TRANSIT_TO(KILL_OBJECT,
132 (1 << FSCACHE_OBJECT_EV_ERROR) |
133 (1 << FSCACHE_OBJECT_EV_KILL)),
137 static int fscache_get_object(struct fscache_object *,
138 enum fscache_obj_ref_trace);
139 static void fscache_put_object(struct fscache_object *,
140 enum fscache_obj_ref_trace);
141 static bool fscache_enqueue_dependents(struct fscache_object *, int);
142 static void fscache_dequeue_object(struct fscache_object *);
143 static void fscache_update_aux_data(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;
175 ASSERT(object != NULL);
177 _enter("{OBJ%x,%s,%lx}",
178 object->debug_id, object->state->name, object->events);
180 event_mask = object->event_mask;
182 object->event_mask = 0; /* Mask normal event handling */
183 state = object->state;
185 events = object->events;
187 /* Handle any out-of-band events (typically an error) */
188 if (events & object->oob_event_mask) {
189 _debug("{OBJ%x} oob %lx",
190 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;
205 /* Wait states are just transition tables */
207 if (events & event_mask) {
208 for (t = state->transitions; t->events; t++) {
209 if (events & t->events) {
210 new_state = t->transit_to;
211 event = fls(events & t->events) - 1;
212 trace_fscache_osm(object, state,
214 clear_bit(event, &object->events);
215 _debug("{OBJ%x} ev %d: %s -> %s",
216 object->debug_id, event,
217 state->name, new_state->name);
218 object->state = state = new_state;
219 goto execute_work_state;
223 /* The event mask didn't include all the tabled bits */
226 /* Randomly woke up */
231 _debug("{OBJ%x} exec %s", object->debug_id, state->name);
233 trace_fscache_osm(object, state, false, oob, event);
234 new_state = state->work(object, event);
236 if (new_state == NO_TRANSIT) {
237 _debug("{OBJ%x} %s notrans", object->debug_id, state->name);
238 if (unlikely(state == STATE(OBJECT_DEAD))) {
242 fscache_enqueue_object(object);
243 event_mask = object->oob_event_mask;
247 _debug("{OBJ%x} %s -> %s",
248 object->debug_id, state->name, new_state->name);
249 object->state = state = new_state;
252 if (unlikely(state == STATE(OBJECT_DEAD))) {
259 /* Transited to wait state */
260 event_mask = object->oob_event_mask;
261 for (t = state->transitions; t->events; t++)
262 event_mask |= t->events;
265 object->event_mask = event_mask;
267 events = object->events;
268 if (events & event_mask)
270 _leave(" [msk %lx]", event_mask);
276 static void fscache_object_work_func(struct work_struct *work)
278 struct fscache_object *object =
279 container_of(work, struct fscache_object, work);
281 _enter("{OBJ%x}", object->debug_id);
283 fscache_object_sm_dispatcher(object);
284 fscache_put_object(object, fscache_obj_put_work);
288 * fscache_object_init - Initialise a cache object description
289 * @object: Object description
290 * @cookie: Cookie object will be attached to
291 * @cache: Cache in which backing object will be found
293 * Initialise a cache object description to its basic values.
295 * See Documentation/filesystems/caching/backend-api.rst for a complete
298 void fscache_object_init(struct fscache_object *object,
299 struct fscache_cookie *cookie,
300 struct fscache_cache *cache)
302 const struct fscache_transition *t;
304 atomic_inc(&cache->object_count);
306 object->state = STATE(WAIT_FOR_INIT);
307 object->oob_table = fscache_osm_init_oob;
308 object->flags = 1 << FSCACHE_OBJECT_IS_LIVE;
309 spin_lock_init(&object->lock);
310 INIT_LIST_HEAD(&object->cache_link);
311 INIT_HLIST_NODE(&object->cookie_link);
312 INIT_WORK(&object->work, fscache_object_work_func);
313 INIT_LIST_HEAD(&object->dependents);
314 INIT_LIST_HEAD(&object->dep_link);
315 INIT_LIST_HEAD(&object->pending_ops);
316 object->n_children = 0;
317 object->n_ops = object->n_in_progress = object->n_exclusive = 0;
319 object->store_limit = 0;
320 object->store_limit_l = 0;
321 object->cache = cache;
322 object->cookie = cookie;
323 fscache_cookie_get(cookie, fscache_cookie_get_attach_object);
324 object->parent = NULL;
325 #ifdef CONFIG_FSCACHE_OBJECT_LIST
326 RB_CLEAR_NODE(&object->objlist_link);
329 object->oob_event_mask = 0;
330 for (t = object->oob_table; t->events; t++)
331 object->oob_event_mask |= t->events;
332 object->event_mask = object->oob_event_mask;
333 for (t = object->state->transitions; t->events; t++)
334 object->event_mask |= t->events;
336 EXPORT_SYMBOL(fscache_object_init);
339 * Mark the object as no longer being live, making sure that we synchronise
340 * against op submission.
342 static inline void fscache_mark_object_dead(struct fscache_object *object)
344 spin_lock(&object->lock);
345 clear_bit(FSCACHE_OBJECT_IS_LIVE, &object->flags);
346 spin_unlock(&object->lock);
350 * Abort object initialisation before we start it.
352 static const struct fscache_state *fscache_abort_initialisation(struct fscache_object *object,
355 _enter("{OBJ%x},%d", object->debug_id, event);
357 object->oob_event_mask = 0;
358 fscache_dequeue_object(object);
359 return transit_to(KILL_OBJECT);
363 * initialise an object
364 * - check the specified object's parent to see if we can make use of it
365 * immediately to do a creation
366 * - we may need to start the process of creating a parent and we need to wait
367 * for the parent's lookup and creation to complete if it's not there yet
369 static const struct fscache_state *fscache_initialise_object(struct fscache_object *object,
372 struct fscache_object *parent;
375 _enter("{OBJ%x},%d", object->debug_id, event);
377 ASSERT(list_empty(&object->dep_link));
379 parent = object->parent;
381 _leave(" [no parent]");
382 return transit_to(DROP_OBJECT);
385 _debug("parent: %s of:%lx", parent->state->name, parent->flags);
387 if (fscache_object_is_dying(parent)) {
388 _leave(" [bad parent]");
389 return transit_to(DROP_OBJECT);
392 if (fscache_object_is_available(parent)) {
394 return transit_to(PARENT_READY);
399 spin_lock(&parent->lock);
400 fscache_stat(&fscache_n_cop_grab_object);
402 if (fscache_object_is_live(parent) &&
403 object->cache->ops->grab_object(object, fscache_obj_get_add_to_deps)) {
404 list_add(&object->dep_link, &parent->dependents);
407 fscache_stat_d(&fscache_n_cop_grab_object);
408 spin_unlock(&parent->lock);
410 _leave(" [grab failed]");
411 return transit_to(DROP_OBJECT);
414 /* fscache_acquire_non_index_cookie() uses this
415 * to wake the chain up */
416 fscache_raise_event(parent, FSCACHE_OBJECT_EV_NEW_CHILD);
418 return transit_to(WAIT_FOR_PARENT);
422 * Once the parent object is ready, we should kick off our lookup op.
424 static const struct fscache_state *fscache_parent_ready(struct fscache_object *object,
427 struct fscache_object *parent = object->parent;
429 _enter("{OBJ%x},%d", object->debug_id, event);
431 ASSERT(parent != NULL);
433 spin_lock(&parent->lock);
436 spin_unlock(&parent->lock);
439 return transit_to(LOOK_UP_OBJECT);
443 * look an object up in the cache from which it was allocated
444 * - we hold an "access lock" on the parent object, so the parent object cannot
445 * be withdrawn by either party till we've finished
447 static const struct fscache_state *fscache_look_up_object(struct fscache_object *object,
450 struct fscache_cookie *cookie = object->cookie;
451 struct fscache_object *parent = object->parent;
454 _enter("{OBJ%x},%d", object->debug_id, event);
456 object->oob_table = fscache_osm_lookup_oob;
458 ASSERT(parent != NULL);
459 ASSERTCMP(parent->n_ops, >, 0);
460 ASSERTCMP(parent->n_obj_ops, >, 0);
462 /* make sure the parent is still available */
463 ASSERT(fscache_object_is_available(parent));
465 if (fscache_object_is_dying(parent) ||
466 test_bit(FSCACHE_IOERROR, &object->cache->flags) ||
467 !fscache_use_cookie(object)) {
468 _leave(" [unavailable]");
469 return transit_to(LOOKUP_FAILURE);
472 _debug("LOOKUP \"%s\" in \"%s\"",
473 cookie->def->name, object->cache->tag->name);
475 fscache_stat(&fscache_n_object_lookups);
476 fscache_stat(&fscache_n_cop_lookup_object);
477 ret = object->cache->ops->lookup_object(object);
478 fscache_stat_d(&fscache_n_cop_lookup_object);
480 fscache_unuse_cookie(object);
482 if (ret == -ETIMEDOUT) {
483 /* probably stuck behind another object, so move this one to
484 * the back of the queue */
485 fscache_stat(&fscache_n_object_lookups_timed_out);
486 _leave(" [timeout]");
492 return transit_to(LOOKUP_FAILURE);
496 return transit_to(OBJECT_AVAILABLE);
500 * fscache_object_lookup_negative - Note negative cookie lookup
501 * @object: Object pointing to cookie to mark
503 * Note negative lookup, permitting those waiting to read data from an already
504 * existing backing object to continue as there's no data for them to read.
506 void fscache_object_lookup_negative(struct fscache_object *object)
508 struct fscache_cookie *cookie = object->cookie;
510 _enter("{OBJ%x,%s}", object->debug_id, object->state->name);
512 if (!test_and_set_bit(FSCACHE_OBJECT_IS_LOOKED_UP, &object->flags)) {
513 fscache_stat(&fscache_n_object_lookups_negative);
515 /* Allow write requests to begin stacking up and read requests to begin
518 set_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
519 clear_bit(FSCACHE_COOKIE_UNAVAILABLE, &cookie->flags);
521 _debug("wake up lookup %p", &cookie->flags);
522 clear_bit_unlock(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags);
523 wake_up_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP);
527 EXPORT_SYMBOL(fscache_object_lookup_negative);
530 * fscache_obtained_object - Note successful object lookup or creation
531 * @object: Object pointing to cookie to mark
533 * Note successful lookup and/or creation, permitting those waiting to write
534 * data to a backing object to continue.
536 * Note that after calling this, an object's cookie may be relinquished by the
537 * netfs, and so must be accessed with object lock held.
539 void fscache_obtained_object(struct fscache_object *object)
541 struct fscache_cookie *cookie = object->cookie;
543 _enter("{OBJ%x,%s}", object->debug_id, object->state->name);
545 /* if we were still looking up, then we must have a positive lookup
546 * result, in which case there may be data available */
547 if (!test_and_set_bit(FSCACHE_OBJECT_IS_LOOKED_UP, &object->flags)) {
548 fscache_stat(&fscache_n_object_lookups_positive);
550 /* We do (presumably) have data */
551 clear_bit_unlock(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
552 clear_bit(FSCACHE_COOKIE_UNAVAILABLE, &cookie->flags);
554 /* Allow write requests to begin stacking up and read requests
555 * to begin shovelling data.
557 clear_bit_unlock(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags);
558 wake_up_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP);
560 fscache_stat(&fscache_n_object_created);
563 set_bit(FSCACHE_OBJECT_IS_AVAILABLE, &object->flags);
566 EXPORT_SYMBOL(fscache_obtained_object);
569 * handle an object that has just become available
571 static const struct fscache_state *fscache_object_available(struct fscache_object *object,
574 _enter("{OBJ%x},%d", object->debug_id, event);
576 object->oob_table = fscache_osm_run_oob;
578 spin_lock(&object->lock);
580 fscache_done_parent_op(object);
581 if (object->n_in_progress == 0) {
582 if (object->n_ops > 0) {
583 ASSERTCMP(object->n_ops, >=, object->n_obj_ops);
584 fscache_start_operations(object);
586 ASSERT(list_empty(&object->pending_ops));
589 spin_unlock(&object->lock);
591 fscache_stat(&fscache_n_cop_lookup_complete);
592 object->cache->ops->lookup_complete(object);
593 fscache_stat_d(&fscache_n_cop_lookup_complete);
595 fscache_stat(&fscache_n_object_avail);
598 return transit_to(JUMPSTART_DEPS);
602 * Wake up this object's dependent objects now that we've become available.
604 static const struct fscache_state *fscache_jumpstart_dependents(struct fscache_object *object,
607 _enter("{OBJ%x},%d", object->debug_id, event);
609 if (!fscache_enqueue_dependents(object, FSCACHE_OBJECT_EV_PARENT_READY))
610 return NO_TRANSIT; /* Not finished; requeue */
611 return transit_to(WAIT_FOR_CMD);
615 * Handle lookup or creation failute.
617 static const struct fscache_state *fscache_lookup_failure(struct fscache_object *object,
620 struct fscache_cookie *cookie;
622 _enter("{OBJ%x},%d", object->debug_id, event);
624 object->oob_event_mask = 0;
626 fscache_stat(&fscache_n_cop_lookup_complete);
627 object->cache->ops->lookup_complete(object);
628 fscache_stat_d(&fscache_n_cop_lookup_complete);
630 set_bit(FSCACHE_OBJECT_KILLED_BY_CACHE, &object->flags);
632 cookie = object->cookie;
633 set_bit(FSCACHE_COOKIE_UNAVAILABLE, &cookie->flags);
634 if (test_and_clear_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags))
635 wake_up_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP);
637 fscache_done_parent_op(object);
638 return transit_to(KILL_OBJECT);
642 * Wait for completion of all active operations on this object and the death of
643 * all child objects of this object.
645 static const struct fscache_state *fscache_kill_object(struct fscache_object *object,
648 _enter("{OBJ%x,%d,%d},%d",
649 object->debug_id, object->n_ops, object->n_children, event);
651 fscache_mark_object_dead(object);
652 object->oob_event_mask = 0;
654 if (test_bit(FSCACHE_OBJECT_RETIRED, &object->flags)) {
655 /* Reject any new read/write ops and abort any that are pending. */
656 clear_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags);
657 fscache_cancel_all_ops(object);
660 if (list_empty(&object->dependents) &&
661 object->n_ops == 0 &&
662 object->n_children == 0)
663 return transit_to(DROP_OBJECT);
665 if (object->n_in_progress == 0) {
666 spin_lock(&object->lock);
667 if (object->n_ops > 0 && object->n_in_progress == 0)
668 fscache_start_operations(object);
669 spin_unlock(&object->lock);
672 if (!list_empty(&object->dependents))
673 return transit_to(KILL_DEPENDENTS);
675 return transit_to(WAIT_FOR_CLEARANCE);
679 * Kill dependent objects.
681 static const struct fscache_state *fscache_kill_dependents(struct fscache_object *object,
684 _enter("{OBJ%x},%d", object->debug_id, event);
686 if (!fscache_enqueue_dependents(object, FSCACHE_OBJECT_EV_KILL))
687 return NO_TRANSIT; /* Not finished */
688 return transit_to(WAIT_FOR_CLEARANCE);
692 * Drop an object's attachments
694 static const struct fscache_state *fscache_drop_object(struct fscache_object *object,
697 struct fscache_object *parent = object->parent;
698 struct fscache_cookie *cookie = object->cookie;
699 struct fscache_cache *cache = object->cache;
702 _enter("{OBJ%x,%d},%d", object->debug_id, object->n_children, event);
704 ASSERT(cookie != NULL);
705 ASSERT(!hlist_unhashed(&object->cookie_link));
707 if (test_bit(FSCACHE_COOKIE_AUX_UPDATED, &cookie->flags)) {
708 _debug("final update");
709 fscache_update_aux_data(object);
712 /* Make sure the cookie no longer points here and that the netfs isn't
715 spin_lock(&cookie->lock);
716 hlist_del_init(&object->cookie_link);
717 if (hlist_empty(&cookie->backing_objects) &&
718 test_and_clear_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags))
720 spin_unlock(&cookie->lock);
723 wake_up_bit(&cookie->flags, FSCACHE_COOKIE_INVALIDATING);
724 if (test_and_clear_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags))
725 wake_up_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP);
728 /* Prevent a race with our last child, which has to signal EV_CLEARED
729 * before dropping our spinlock.
731 spin_lock(&object->lock);
732 spin_unlock(&object->lock);
734 /* Discard from the cache's collection of objects */
735 spin_lock(&cache->object_list_lock);
736 list_del_init(&object->cache_link);
737 spin_unlock(&cache->object_list_lock);
739 fscache_stat(&fscache_n_cop_drop_object);
740 cache->ops->drop_object(object);
741 fscache_stat_d(&fscache_n_cop_drop_object);
743 /* The parent object wants to know when all it dependents have gone */
745 _debug("release parent OBJ%x {%d}",
746 parent->debug_id, parent->n_children);
748 spin_lock(&parent->lock);
749 parent->n_children--;
750 if (parent->n_children == 0)
751 fscache_raise_event(parent, FSCACHE_OBJECT_EV_CLEARED);
752 spin_unlock(&parent->lock);
753 object->parent = NULL;
756 /* this just shifts the object release to the work processor */
757 fscache_put_object(object, fscache_obj_put_drop_obj);
758 fscache_stat(&fscache_n_object_dead);
761 return transit_to(OBJECT_DEAD);
765 * get a ref on an object
767 static int fscache_get_object(struct fscache_object *object,
768 enum fscache_obj_ref_trace why)
772 fscache_stat(&fscache_n_cop_grab_object);
773 ret = object->cache->ops->grab_object(object, why) ? 0 : -EAGAIN;
774 fscache_stat_d(&fscache_n_cop_grab_object);
779 * Discard a ref on an object
781 static void fscache_put_object(struct fscache_object *object,
782 enum fscache_obj_ref_trace why)
784 fscache_stat(&fscache_n_cop_put_object);
785 object->cache->ops->put_object(object, why);
786 fscache_stat_d(&fscache_n_cop_put_object);
790 * fscache_object_destroy - Note that a cache object is about to be destroyed
791 * @object: The object to be destroyed
793 * Note the imminent destruction and deallocation of a cache object record.
795 void fscache_object_destroy(struct fscache_object *object)
797 fscache_objlist_remove(object);
799 /* We can get rid of the cookie now */
800 fscache_cookie_put(object->cookie, fscache_cookie_put_object);
801 object->cookie = NULL;
803 EXPORT_SYMBOL(fscache_object_destroy);
806 * enqueue an object for metadata-type processing
808 void fscache_enqueue_object(struct fscache_object *object)
810 _enter("{OBJ%x}", object->debug_id);
812 if (fscache_get_object(object, fscache_obj_get_queue) >= 0) {
813 wait_queue_head_t *cong_wq =
814 &get_cpu_var(fscache_object_cong_wait);
816 if (queue_work(fscache_object_wq, &object->work)) {
817 if (fscache_object_congested())
820 fscache_put_object(object, fscache_obj_put_queue);
822 put_cpu_var(fscache_object_cong_wait);
827 * fscache_object_sleep_till_congested - Sleep until object wq is congested
828 * @timeoutp: Scheduler sleep timeout
830 * Allow an object handler to sleep until the object workqueue is congested.
832 * The caller must set up a wake up event before calling this and must have set
833 * the appropriate sleep mode (such as TASK_UNINTERRUPTIBLE) and tested its own
834 * condition before calling this function as no test is made here.
836 * %true is returned if the object wq is congested, %false otherwise.
838 bool fscache_object_sleep_till_congested(signed long *timeoutp)
840 wait_queue_head_t *cong_wq = this_cpu_ptr(&fscache_object_cong_wait);
843 if (fscache_object_congested())
846 add_wait_queue_exclusive(cong_wq, &wait);
847 if (!fscache_object_congested())
848 *timeoutp = schedule_timeout(*timeoutp);
849 finish_wait(cong_wq, &wait);
851 return fscache_object_congested();
853 EXPORT_SYMBOL_GPL(fscache_object_sleep_till_congested);
856 * Enqueue the dependents of an object for metadata-type processing.
858 * If we don't manage to finish the list before the scheduler wants to run
859 * again then return false immediately. We return true if the list was
862 static bool fscache_enqueue_dependents(struct fscache_object *object, int event)
864 struct fscache_object *dep;
867 _enter("{OBJ%x}", object->debug_id);
869 if (list_empty(&object->dependents))
872 spin_lock(&object->lock);
874 while (!list_empty(&object->dependents)) {
875 dep = list_entry(object->dependents.next,
876 struct fscache_object, dep_link);
877 list_del_init(&dep->dep_link);
879 fscache_raise_event(dep, event);
880 fscache_put_object(dep, fscache_obj_put_enq_dep);
882 if (!list_empty(&object->dependents) && need_resched()) {
888 spin_unlock(&object->lock);
893 * remove an object from whatever queue it's waiting on
895 static void fscache_dequeue_object(struct fscache_object *object)
897 _enter("{OBJ%x}", object->debug_id);
899 if (!list_empty(&object->dep_link)) {
900 spin_lock(&object->parent->lock);
901 list_del_init(&object->dep_link);
902 spin_unlock(&object->parent->lock);
909 * fscache_check_aux - Ask the netfs whether an object on disk is still valid
910 * @object: The object to ask about
911 * @data: The auxiliary data for the object
912 * @datalen: The size of the auxiliary data
914 * This function consults the netfs about the coherency state of an object.
915 * The caller must be holding a ref on cookie->n_active (held by
916 * fscache_look_up_object() on behalf of the cache backend during object lookup
919 enum fscache_checkaux fscache_check_aux(struct fscache_object *object,
920 const void *data, uint16_t datalen,
923 enum fscache_checkaux result;
925 if (!object->cookie->def->check_aux) {
926 fscache_stat(&fscache_n_checkaux_none);
927 return FSCACHE_CHECKAUX_OKAY;
930 result = object->cookie->def->check_aux(object->cookie->netfs_data,
931 data, datalen, object_size);
933 /* entry okay as is */
934 case FSCACHE_CHECKAUX_OKAY:
935 fscache_stat(&fscache_n_checkaux_okay);
938 /* entry requires update */
939 case FSCACHE_CHECKAUX_NEEDS_UPDATE:
940 fscache_stat(&fscache_n_checkaux_update);
943 /* entry requires deletion */
944 case FSCACHE_CHECKAUX_OBSOLETE:
945 fscache_stat(&fscache_n_checkaux_obsolete);
954 EXPORT_SYMBOL(fscache_check_aux);
957 * Asynchronously invalidate an object.
959 static const struct fscache_state *_fscache_invalidate_object(struct fscache_object *object,
962 struct fscache_operation *op;
963 struct fscache_cookie *cookie = object->cookie;
965 _enter("{OBJ%x},%d", object->debug_id, event);
967 /* We're going to need the cookie. If the cookie is not available then
968 * retire the object instead.
970 if (!fscache_use_cookie(object)) {
971 ASSERT(radix_tree_empty(&object->cookie->stores));
972 set_bit(FSCACHE_OBJECT_RETIRED, &object->flags);
973 _leave(" [no cookie]");
974 return transit_to(KILL_OBJECT);
977 /* Reject any new read/write ops and abort any that are pending. */
978 fscache_invalidate_writes(cookie);
979 clear_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags);
980 fscache_cancel_all_ops(object);
982 /* Now we have to wait for in-progress reads and writes */
983 op = kzalloc(sizeof(*op), GFP_KERNEL);
987 fscache_operation_init(cookie, op, object->cache->ops->invalidate_object,
989 op->flags = FSCACHE_OP_ASYNC |
990 (1 << FSCACHE_OP_EXCLUSIVE) |
991 (1 << FSCACHE_OP_UNUSE_COOKIE);
992 trace_fscache_page_op(cookie, NULL, op, fscache_page_op_invalidate);
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 fscache_mark_object_dead(object);
1016 fscache_unuse_cookie(object);
1017 _leave(" [ENOMEM]");
1018 return transit_to(KILL_OBJECT);
1021 fscache_mark_object_dead(object);
1022 spin_unlock(&cookie->lock);
1023 fscache_unuse_cookie(object);
1026 return transit_to(KILL_OBJECT);
1029 static const struct fscache_state *fscache_invalidate_object(struct fscache_object *object,
1032 const struct fscache_state *s;
1034 fscache_stat(&fscache_n_invalidates_run);
1035 fscache_stat(&fscache_n_cop_invalidate_object);
1036 s = _fscache_invalidate_object(object, event);
1037 fscache_stat_d(&fscache_n_cop_invalidate_object);
1042 * Update auxiliary data.
1044 static void fscache_update_aux_data(struct fscache_object *object)
1046 fscache_stat(&fscache_n_updates_run);
1047 fscache_stat(&fscache_n_cop_update_object);
1048 object->cache->ops->update_object(object);
1049 fscache_stat_d(&fscache_n_cop_update_object);
1053 * Asynchronously update an object.
1055 static const struct fscache_state *fscache_update_object(struct fscache_object *object,
1058 _enter("{OBJ%x},%d", object->debug_id, event);
1060 fscache_update_aux_data(object);
1063 return transit_to(WAIT_FOR_CMD);
1067 * fscache_object_retrying_stale - Note retrying stale object
1068 * @object: The object that will be retried
1070 * Note that an object lookup found an on-disk object that was adjudged to be
1071 * stale and has been deleted. The lookup will be retried.
1073 void fscache_object_retrying_stale(struct fscache_object *object)
1075 fscache_stat(&fscache_n_cache_no_space_reject);
1077 EXPORT_SYMBOL(fscache_object_retrying_stale);
1080 * fscache_object_mark_killed - Note that an object was killed
1081 * @object: The object that was culled
1082 * @why: The reason the object was killed.
1084 * Note that an object was killed. Returns true if the object was
1085 * already marked killed, false if it wasn't.
1087 void fscache_object_mark_killed(struct fscache_object *object,
1088 enum fscache_why_object_killed why)
1090 if (test_and_set_bit(FSCACHE_OBJECT_KILLED_BY_CACHE, &object->flags)) {
1091 pr_err("Error: Object already killed by cache [%s]\n",
1092 object->cache->identifier);
1097 case FSCACHE_OBJECT_NO_SPACE:
1098 fscache_stat(&fscache_n_cache_no_space_reject);
1100 case FSCACHE_OBJECT_IS_STALE:
1101 fscache_stat(&fscache_n_cache_stale_objects);
1103 case FSCACHE_OBJECT_WAS_RETIRED:
1104 fscache_stat(&fscache_n_cache_retired_objects);
1106 case FSCACHE_OBJECT_WAS_CULLED:
1107 fscache_stat(&fscache_n_cache_culled_objects);
1111 EXPORT_SYMBOL(fscache_object_mark_killed);
1114 * The object is dead. We can get here if an object gets queued by an event
1115 * that would lead to its death (such as EV_KILL) when the dispatcher is
1116 * already running (and so can be requeued) but hasn't yet cleared the event
1119 static const struct fscache_state *fscache_object_dead(struct fscache_object *object,
1122 if (!test_and_set_bit(FSCACHE_OBJECT_RUN_AFTER_DEAD,
1126 WARN(true, "FS-Cache object redispatched after death");