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 static void fscache_put_object(struct fscache_object
*);
143 static bool fscache_enqueue_dependents(struct fscache_object
*, int);
144 static void fscache_dequeue_object(struct fscache_object
*);
147 * we need to notify the parent when an op completes that we had outstanding
150 static inline void fscache_done_parent_op(struct fscache_object
*object
)
152 struct fscache_object
*parent
= object
->parent
;
154 _enter("OBJ%x {OBJ%x,%x}",
155 object
->debug_id
, parent
->debug_id
, parent
->n_ops
);
157 spin_lock_nested(&parent
->lock
, 1);
160 if (parent
->n_ops
== 0)
161 fscache_raise_event(parent
, FSCACHE_OBJECT_EV_CLEARED
);
162 spin_unlock(&parent
->lock
);
166 * Object state machine dispatcher.
168 static void fscache_object_sm_dispatcher(struct fscache_object
*object
)
170 const struct fscache_transition
*t
;
171 const struct fscache_state
*state
, *new_state
;
172 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
);
191 for (t
= object
->oob_table
; t
->events
; t
++) {
192 if (events
& t
->events
) {
193 state
= t
->transit_to
;
194 ASSERT(state
->work
!= NULL
);
195 event
= fls(events
& t
->events
) - 1;
196 __clear_bit(event
, &object
->oob_event_mask
);
197 clear_bit(event
, &object
->events
);
198 goto execute_work_state
;
203 /* Wait states are just transition tables */
205 if (events
& event_mask
) {
206 for (t
= state
->transitions
; t
->events
; t
++) {
207 if (events
& t
->events
) {
208 new_state
= t
->transit_to
;
209 event
= fls(events
& t
->events
) - 1;
210 clear_bit(event
, &object
->events
);
211 _debug("{OBJ%x} ev %d: %s -> %s",
212 object
->debug_id
, event
,
213 state
->name
, new_state
->name
);
214 object
->state
= state
= new_state
;
215 goto execute_work_state
;
219 /* The event mask didn't include all the tabled bits */
222 /* Randomly woke up */
227 _debug("{OBJ%x} exec %s", object
->debug_id
, state
->name
);
229 new_state
= state
->work(object
, event
);
231 if (new_state
== NO_TRANSIT
) {
232 _debug("{OBJ%x} %s notrans", object
->debug_id
, state
->name
);
233 if (unlikely(state
== STATE(OBJECT_DEAD
))) {
237 fscache_enqueue_object(object
);
238 event_mask
= object
->oob_event_mask
;
242 _debug("{OBJ%x} %s -> %s",
243 object
->debug_id
, state
->name
, new_state
->name
);
244 object
->state
= state
= new_state
;
247 if (unlikely(state
== STATE(OBJECT_DEAD
))) {
254 /* Transited to wait state */
255 event_mask
= object
->oob_event_mask
;
256 for (t
= state
->transitions
; t
->events
; t
++)
257 event_mask
|= t
->events
;
260 object
->event_mask
= event_mask
;
262 events
= object
->events
;
263 if (events
& event_mask
)
265 _leave(" [msk %lx]", event_mask
);
271 static void fscache_object_work_func(struct work_struct
*work
)
273 struct fscache_object
*object
=
274 container_of(work
, struct fscache_object
, work
);
277 _enter("{OBJ%x}", object
->debug_id
);
280 fscache_object_sm_dispatcher(object
);
281 fscache_hist(fscache_objs_histogram
, start
);
282 fscache_put_object(object
);
286 * fscache_object_init - Initialise a cache object description
287 * @object: Object description
288 * @cookie: Cookie object will be attached to
289 * @cache: Cache in which backing object will be found
291 * Initialise a cache object description to its basic values.
293 * See Documentation/filesystems/caching/backend-api.txt for a complete
296 void fscache_object_init(struct fscache_object
*object
,
297 struct fscache_cookie
*cookie
,
298 struct fscache_cache
*cache
)
300 const struct fscache_transition
*t
;
302 atomic_inc(&cache
->object_count
);
304 object
->state
= STATE(WAIT_FOR_INIT
);
305 object
->oob_table
= fscache_osm_init_oob
;
306 object
->flags
= 1 << FSCACHE_OBJECT_IS_LIVE
;
307 spin_lock_init(&object
->lock
);
308 INIT_LIST_HEAD(&object
->cache_link
);
309 INIT_HLIST_NODE(&object
->cookie_link
);
310 INIT_WORK(&object
->work
, fscache_object_work_func
);
311 INIT_LIST_HEAD(&object
->dependents
);
312 INIT_LIST_HEAD(&object
->dep_link
);
313 INIT_LIST_HEAD(&object
->pending_ops
);
314 object
->n_children
= 0;
315 object
->n_ops
= object
->n_in_progress
= object
->n_exclusive
= 0;
317 object
->store_limit
= 0;
318 object
->store_limit_l
= 0;
319 object
->cache
= cache
;
320 object
->cookie
= cookie
;
321 atomic_inc(&cookie
->usage
);
322 object
->parent
= NULL
;
323 #ifdef CONFIG_FSCACHE_OBJECT_LIST
324 RB_CLEAR_NODE(&object
->objlist_link
);
327 object
->oob_event_mask
= 0;
328 for (t
= object
->oob_table
; t
->events
; t
++)
329 object
->oob_event_mask
|= t
->events
;
330 object
->event_mask
= object
->oob_event_mask
;
331 for (t
= object
->state
->transitions
; t
->events
; t
++)
332 object
->event_mask
|= t
->events
;
334 EXPORT_SYMBOL(fscache_object_init
);
337 * Mark the object as no longer being live, making sure that we synchronise
338 * against op submission.
340 static inline void fscache_mark_object_dead(struct fscache_object
*object
)
342 spin_lock(&object
->lock
);
343 clear_bit(FSCACHE_OBJECT_IS_LIVE
, &object
->flags
);
344 spin_unlock(&object
->lock
);
348 * Abort object initialisation before we start it.
350 static const struct fscache_state
*fscache_abort_initialisation(struct fscache_object
*object
,
353 _enter("{OBJ%x},%d", object
->debug_id
, event
);
355 object
->oob_event_mask
= 0;
356 fscache_dequeue_object(object
);
357 return transit_to(KILL_OBJECT
);
361 * initialise an object
362 * - check the specified object's parent to see if we can make use of it
363 * immediately to do a creation
364 * - we may need to start the process of creating a parent and we need to wait
365 * for the parent's lookup and creation to complete if it's not there yet
367 static const struct fscache_state
*fscache_initialise_object(struct fscache_object
*object
,
370 struct fscache_object
*parent
;
373 _enter("{OBJ%x},%d", object
->debug_id
, event
);
375 ASSERT(list_empty(&object
->dep_link
));
377 parent
= object
->parent
;
379 _leave(" [no parent]");
380 return transit_to(DROP_OBJECT
);
383 _debug("parent: %s of:%lx", parent
->state
->name
, parent
->flags
);
385 if (fscache_object_is_dying(parent
)) {
386 _leave(" [bad parent]");
387 return transit_to(DROP_OBJECT
);
390 if (fscache_object_is_available(parent
)) {
392 return transit_to(PARENT_READY
);
397 spin_lock(&parent
->lock
);
398 fscache_stat(&fscache_n_cop_grab_object
);
400 if (fscache_object_is_live(parent
) &&
401 object
->cache
->ops
->grab_object(object
)) {
402 list_add(&object
->dep_link
, &parent
->dependents
);
405 fscache_stat_d(&fscache_n_cop_grab_object
);
406 spin_unlock(&parent
->lock
);
408 _leave(" [grab failed]");
409 return transit_to(DROP_OBJECT
);
412 /* fscache_acquire_non_index_cookie() uses this
413 * to wake the chain up */
414 fscache_raise_event(parent
, FSCACHE_OBJECT_EV_NEW_CHILD
);
416 return transit_to(WAIT_FOR_PARENT
);
420 * Once the parent object is ready, we should kick off our lookup op.
422 static const struct fscache_state
*fscache_parent_ready(struct fscache_object
*object
,
425 struct fscache_object
*parent
= object
->parent
;
427 _enter("{OBJ%x},%d", object
->debug_id
, event
);
429 ASSERT(parent
!= NULL
);
431 spin_lock(&parent
->lock
);
434 object
->lookup_jif
= jiffies
;
435 spin_unlock(&parent
->lock
);
438 return transit_to(LOOK_UP_OBJECT
);
442 * look an object up in the cache from which it was allocated
443 * - we hold an "access lock" on the parent object, so the parent object cannot
444 * be withdrawn by either party till we've finished
446 static const struct fscache_state
*fscache_look_up_object(struct fscache_object
*object
,
449 struct fscache_cookie
*cookie
= object
->cookie
;
450 struct fscache_object
*parent
= object
->parent
;
453 _enter("{OBJ%x},%d", object
->debug_id
, event
);
455 object
->oob_table
= fscache_osm_lookup_oob
;
457 ASSERT(parent
!= NULL
);
458 ASSERTCMP(parent
->n_ops
, >, 0);
459 ASSERTCMP(parent
->n_obj_ops
, >, 0);
461 /* make sure the parent is still available */
462 ASSERT(fscache_object_is_available(parent
));
464 if (fscache_object_is_dying(parent
) ||
465 test_bit(FSCACHE_IOERROR
, &object
->cache
->flags
) ||
466 !fscache_use_cookie(object
)) {
467 _leave(" [unavailable]");
468 return transit_to(LOOKUP_FAILURE
);
471 _debug("LOOKUP \"%s\" in \"%s\"",
472 cookie
->def
->name
, object
->cache
->tag
->name
);
474 fscache_stat(&fscache_n_object_lookups
);
475 fscache_stat(&fscache_n_cop_lookup_object
);
476 ret
= object
->cache
->ops
->lookup_object(object
);
477 fscache_stat_d(&fscache_n_cop_lookup_object
);
479 fscache_unuse_cookie(object
);
481 if (ret
== -ETIMEDOUT
) {
482 /* probably stuck behind another object, so move this one to
483 * the back of the queue */
484 fscache_stat(&fscache_n_object_lookups_timed_out
);
485 _leave(" [timeout]");
491 return transit_to(LOOKUP_FAILURE
);
495 return transit_to(OBJECT_AVAILABLE
);
499 * fscache_object_lookup_negative - Note negative cookie lookup
500 * @object: Object pointing to cookie to mark
502 * Note negative lookup, permitting those waiting to read data from an already
503 * existing backing object to continue as there's no data for them to read.
505 void fscache_object_lookup_negative(struct fscache_object
*object
)
507 struct fscache_cookie
*cookie
= object
->cookie
;
509 _enter("{OBJ%x,%s}", object
->debug_id
, object
->state
->name
);
511 if (!test_and_set_bit(FSCACHE_OBJECT_IS_LOOKED_UP
, &object
->flags
)) {
512 fscache_stat(&fscache_n_object_lookups_negative
);
514 /* Allow write requests to begin stacking up and read requests to begin
517 set_bit(FSCACHE_COOKIE_NO_DATA_YET
, &cookie
->flags
);
518 clear_bit(FSCACHE_COOKIE_UNAVAILABLE
, &cookie
->flags
);
520 _debug("wake up lookup %p", &cookie
->flags
);
521 clear_bit_unlock(FSCACHE_COOKIE_LOOKING_UP
, &cookie
->flags
);
522 wake_up_bit(&cookie
->flags
, FSCACHE_COOKIE_LOOKING_UP
);
526 EXPORT_SYMBOL(fscache_object_lookup_negative
);
529 * fscache_obtained_object - Note successful object lookup or creation
530 * @object: Object pointing to cookie to mark
532 * Note successful lookup and/or creation, permitting those waiting to write
533 * data to a backing object to continue.
535 * Note that after calling this, an object's cookie may be relinquished by the
536 * netfs, and so must be accessed with object lock held.
538 void fscache_obtained_object(struct fscache_object
*object
)
540 struct fscache_cookie
*cookie
= object
->cookie
;
542 _enter("{OBJ%x,%s}", object
->debug_id
, object
->state
->name
);
544 /* if we were still looking up, then we must have a positive lookup
545 * result, in which case there may be data available */
546 if (!test_and_set_bit(FSCACHE_OBJECT_IS_LOOKED_UP
, &object
->flags
)) {
547 fscache_stat(&fscache_n_object_lookups_positive
);
549 /* We do (presumably) have data */
550 clear_bit_unlock(FSCACHE_COOKIE_NO_DATA_YET
, &cookie
->flags
);
551 clear_bit(FSCACHE_COOKIE_UNAVAILABLE
, &cookie
->flags
);
553 /* Allow write requests to begin stacking up and read requests
554 * to begin shovelling data.
556 clear_bit_unlock(FSCACHE_COOKIE_LOOKING_UP
, &cookie
->flags
);
557 wake_up_bit(&cookie
->flags
, FSCACHE_COOKIE_LOOKING_UP
);
559 fscache_stat(&fscache_n_object_created
);
562 set_bit(FSCACHE_OBJECT_IS_AVAILABLE
, &object
->flags
);
565 EXPORT_SYMBOL(fscache_obtained_object
);
568 * handle an object that has just become available
570 static const struct fscache_state
*fscache_object_available(struct fscache_object
*object
,
573 _enter("{OBJ%x},%d", object
->debug_id
, event
);
575 object
->oob_table
= fscache_osm_run_oob
;
577 spin_lock(&object
->lock
);
579 fscache_done_parent_op(object
);
580 if (object
->n_in_progress
== 0) {
581 if (object
->n_ops
> 0) {
582 ASSERTCMP(object
->n_ops
, >=, object
->n_obj_ops
);
583 fscache_start_operations(object
);
585 ASSERT(list_empty(&object
->pending_ops
));
588 spin_unlock(&object
->lock
);
590 fscache_stat(&fscache_n_cop_lookup_complete
);
591 object
->cache
->ops
->lookup_complete(object
);
592 fscache_stat_d(&fscache_n_cop_lookup_complete
);
594 fscache_hist(fscache_obj_instantiate_histogram
, object
->lookup_jif
);
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 /* Make sure the cookie no longer points here and that the netfs isn't
710 spin_lock(&cookie
->lock
);
711 hlist_del_init(&object
->cookie_link
);
712 if (hlist_empty(&cookie
->backing_objects
) &&
713 test_and_clear_bit(FSCACHE_COOKIE_INVALIDATING
, &cookie
->flags
))
715 spin_unlock(&cookie
->lock
);
718 wake_up_bit(&cookie
->flags
, FSCACHE_COOKIE_INVALIDATING
);
720 /* Prevent a race with our last child, which has to signal EV_CLEARED
721 * before dropping our spinlock.
723 spin_lock(&object
->lock
);
724 spin_unlock(&object
->lock
);
726 /* Discard from the cache's collection of objects */
727 spin_lock(&cache
->object_list_lock
);
728 list_del_init(&object
->cache_link
);
729 spin_unlock(&cache
->object_list_lock
);
731 fscache_stat(&fscache_n_cop_drop_object
);
732 cache
->ops
->drop_object(object
);
733 fscache_stat_d(&fscache_n_cop_drop_object
);
735 /* The parent object wants to know when all it dependents have gone */
737 _debug("release parent OBJ%x {%d}",
738 parent
->debug_id
, parent
->n_children
);
740 spin_lock(&parent
->lock
);
741 parent
->n_children
--;
742 if (parent
->n_children
== 0)
743 fscache_raise_event(parent
, FSCACHE_OBJECT_EV_CLEARED
);
744 spin_unlock(&parent
->lock
);
745 object
->parent
= NULL
;
748 /* this just shifts the object release to the work processor */
749 fscache_put_object(object
);
750 fscache_stat(&fscache_n_object_dead
);
753 return transit_to(OBJECT_DEAD
);
757 * get a ref on an object
759 static int fscache_get_object(struct fscache_object
*object
)
763 fscache_stat(&fscache_n_cop_grab_object
);
764 ret
= object
->cache
->ops
->grab_object(object
) ? 0 : -EAGAIN
;
765 fscache_stat_d(&fscache_n_cop_grab_object
);
770 * Discard a ref on an object
772 static void fscache_put_object(struct fscache_object
*object
)
774 fscache_stat(&fscache_n_cop_put_object
);
775 object
->cache
->ops
->put_object(object
);
776 fscache_stat_d(&fscache_n_cop_put_object
);
780 * fscache_object_destroy - Note that a cache object is about to be destroyed
781 * @object: The object to be destroyed
783 * Note the imminent destruction and deallocation of a cache object record.
785 void fscache_object_destroy(struct fscache_object
*object
)
787 fscache_objlist_remove(object
);
789 /* We can get rid of the cookie now */
790 fscache_cookie_put(object
->cookie
);
791 object
->cookie
= NULL
;
793 EXPORT_SYMBOL(fscache_object_destroy
);
796 * enqueue an object for metadata-type processing
798 void fscache_enqueue_object(struct fscache_object
*object
)
800 _enter("{OBJ%x}", object
->debug_id
);
802 if (fscache_get_object(object
) >= 0) {
803 wait_queue_head_t
*cong_wq
=
804 &get_cpu_var(fscache_object_cong_wait
);
806 if (queue_work(fscache_object_wq
, &object
->work
)) {
807 if (fscache_object_congested())
810 fscache_put_object(object
);
812 put_cpu_var(fscache_object_cong_wait
);
817 * fscache_object_sleep_till_congested - Sleep until object wq is congested
818 * @timeoutp: Scheduler sleep timeout
820 * Allow an object handler to sleep until the object workqueue is congested.
822 * The caller must set up a wake up event before calling this and must have set
823 * the appropriate sleep mode (such as TASK_UNINTERRUPTIBLE) and tested its own
824 * condition before calling this function as no test is made here.
826 * %true is returned if the object wq is congested, %false otherwise.
828 bool fscache_object_sleep_till_congested(signed long *timeoutp
)
830 wait_queue_head_t
*cong_wq
= this_cpu_ptr(&fscache_object_cong_wait
);
833 if (fscache_object_congested())
836 add_wait_queue_exclusive(cong_wq
, &wait
);
837 if (!fscache_object_congested())
838 *timeoutp
= schedule_timeout(*timeoutp
);
839 finish_wait(cong_wq
, &wait
);
841 return fscache_object_congested();
843 EXPORT_SYMBOL_GPL(fscache_object_sleep_till_congested
);
846 * Enqueue the dependents of an object for metadata-type processing.
848 * If we don't manage to finish the list before the scheduler wants to run
849 * again then return false immediately. We return true if the list was
852 static bool fscache_enqueue_dependents(struct fscache_object
*object
, int event
)
854 struct fscache_object
*dep
;
857 _enter("{OBJ%x}", object
->debug_id
);
859 if (list_empty(&object
->dependents
))
862 spin_lock(&object
->lock
);
864 while (!list_empty(&object
->dependents
)) {
865 dep
= list_entry(object
->dependents
.next
,
866 struct fscache_object
, dep_link
);
867 list_del_init(&dep
->dep_link
);
869 fscache_raise_event(dep
, event
);
870 fscache_put_object(dep
);
872 if (!list_empty(&object
->dependents
) && need_resched()) {
878 spin_unlock(&object
->lock
);
883 * remove an object from whatever queue it's waiting on
885 static void fscache_dequeue_object(struct fscache_object
*object
)
887 _enter("{OBJ%x}", object
->debug_id
);
889 if (!list_empty(&object
->dep_link
)) {
890 spin_lock(&object
->parent
->lock
);
891 list_del_init(&object
->dep_link
);
892 spin_unlock(&object
->parent
->lock
);
899 * fscache_check_aux - Ask the netfs whether an object on disk is still valid
900 * @object: The object to ask about
901 * @data: The auxiliary data for the object
902 * @datalen: The size of the auxiliary data
904 * This function consults the netfs about the coherency state of an object.
905 * The caller must be holding a ref on cookie->n_active (held by
906 * fscache_look_up_object() on behalf of the cache backend during object lookup
909 enum fscache_checkaux
fscache_check_aux(struct fscache_object
*object
,
910 const void *data
, uint16_t datalen
)
912 enum fscache_checkaux result
;
914 if (!object
->cookie
->def
->check_aux
) {
915 fscache_stat(&fscache_n_checkaux_none
);
916 return FSCACHE_CHECKAUX_OKAY
;
919 result
= object
->cookie
->def
->check_aux(object
->cookie
->netfs_data
,
922 /* entry okay as is */
923 case FSCACHE_CHECKAUX_OKAY
:
924 fscache_stat(&fscache_n_checkaux_okay
);
927 /* entry requires update */
928 case FSCACHE_CHECKAUX_NEEDS_UPDATE
:
929 fscache_stat(&fscache_n_checkaux_update
);
932 /* entry requires deletion */
933 case FSCACHE_CHECKAUX_OBSOLETE
:
934 fscache_stat(&fscache_n_checkaux_obsolete
);
943 EXPORT_SYMBOL(fscache_check_aux
);
946 * Asynchronously invalidate an object.
948 static const struct fscache_state
*_fscache_invalidate_object(struct fscache_object
*object
,
951 struct fscache_operation
*op
;
952 struct fscache_cookie
*cookie
= object
->cookie
;
954 _enter("{OBJ%x},%d", object
->debug_id
, event
);
956 /* We're going to need the cookie. If the cookie is not available then
957 * retire the object instead.
959 if (!fscache_use_cookie(object
)) {
960 ASSERT(object
->cookie
->stores
.rnode
== NULL
);
961 set_bit(FSCACHE_OBJECT_RETIRED
, &object
->flags
);
962 _leave(" [no cookie]");
963 return transit_to(KILL_OBJECT
);
966 /* Reject any new read/write ops and abort any that are pending. */
967 fscache_invalidate_writes(cookie
);
968 clear_bit(FSCACHE_OBJECT_PENDING_WRITE
, &object
->flags
);
969 fscache_cancel_all_ops(object
);
971 /* Now we have to wait for in-progress reads and writes */
972 op
= kzalloc(sizeof(*op
), GFP_KERNEL
);
976 fscache_operation_init(op
, object
->cache
->ops
->invalidate_object
,
978 op
->flags
= FSCACHE_OP_ASYNC
|
979 (1 << FSCACHE_OP_EXCLUSIVE
) |
980 (1 << FSCACHE_OP_UNUSE_COOKIE
);
982 spin_lock(&cookie
->lock
);
983 if (fscache_submit_exclusive_op(object
, op
) < 0)
984 goto submit_op_failed
;
985 spin_unlock(&cookie
->lock
);
986 fscache_put_operation(op
);
988 /* Once we've completed the invalidation, we know there will be no data
989 * stored in the cache and thus we can reinstate the data-check-skip
992 set_bit(FSCACHE_COOKIE_NO_DATA_YET
, &cookie
->flags
);
994 /* We can allow read and write requests to come in once again. They'll
995 * queue up behind our exclusive invalidation operation.
997 if (test_and_clear_bit(FSCACHE_COOKIE_INVALIDATING
, &cookie
->flags
))
998 wake_up_bit(&cookie
->flags
, FSCACHE_COOKIE_INVALIDATING
);
1000 return transit_to(UPDATE_OBJECT
);
1003 fscache_mark_object_dead(object
);
1004 fscache_unuse_cookie(object
);
1005 _leave(" [ENOMEM]");
1006 return transit_to(KILL_OBJECT
);
1009 fscache_mark_object_dead(object
);
1010 spin_unlock(&cookie
->lock
);
1011 fscache_unuse_cookie(object
);
1014 return transit_to(KILL_OBJECT
);
1017 static const struct fscache_state
*fscache_invalidate_object(struct fscache_object
*object
,
1020 const struct fscache_state
*s
;
1022 fscache_stat(&fscache_n_invalidates_run
);
1023 fscache_stat(&fscache_n_cop_invalidate_object
);
1024 s
= _fscache_invalidate_object(object
, event
);
1025 fscache_stat_d(&fscache_n_cop_invalidate_object
);
1030 * Asynchronously update an object.
1032 static const struct fscache_state
*fscache_update_object(struct fscache_object
*object
,
1035 _enter("{OBJ%x},%d", object
->debug_id
, event
);
1037 fscache_stat(&fscache_n_updates_run
);
1038 fscache_stat(&fscache_n_cop_update_object
);
1039 object
->cache
->ops
->update_object(object
);
1040 fscache_stat_d(&fscache_n_cop_update_object
);
1043 return transit_to(WAIT_FOR_CMD
);
1047 * fscache_object_retrying_stale - Note retrying stale object
1048 * @object: The object that will be retried
1050 * Note that an object lookup found an on-disk object that was adjudged to be
1051 * stale and has been deleted. The lookup will be retried.
1053 void fscache_object_retrying_stale(struct fscache_object
*object
)
1055 fscache_stat(&fscache_n_cache_no_space_reject
);
1057 EXPORT_SYMBOL(fscache_object_retrying_stale
);
1060 * fscache_object_mark_killed - Note that an object was killed
1061 * @object: The object that was culled
1062 * @why: The reason the object was killed.
1064 * Note that an object was killed. Returns true if the object was
1065 * already marked killed, false if it wasn't.
1067 void fscache_object_mark_killed(struct fscache_object
*object
,
1068 enum fscache_why_object_killed why
)
1070 if (test_and_set_bit(FSCACHE_OBJECT_KILLED_BY_CACHE
, &object
->flags
)) {
1071 pr_err("Error: Object already killed by cache [%s]\n",
1072 object
->cache
->identifier
);
1077 case FSCACHE_OBJECT_NO_SPACE
:
1078 fscache_stat(&fscache_n_cache_no_space_reject
);
1080 case FSCACHE_OBJECT_IS_STALE
:
1081 fscache_stat(&fscache_n_cache_stale_objects
);
1083 case FSCACHE_OBJECT_WAS_RETIRED
:
1084 fscache_stat(&fscache_n_cache_retired_objects
);
1086 case FSCACHE_OBJECT_WAS_CULLED
:
1087 fscache_stat(&fscache_n_cache_culled_objects
);
1091 EXPORT_SYMBOL(fscache_object_mark_killed
);
1094 * The object is dead. We can get here if an object gets queued by an event
1095 * that would lead to its death (such as EV_KILL) when the dispatcher is
1096 * already running (and so can be requeued) but hasn't yet cleared the event
1099 static const struct fscache_state
*fscache_object_dead(struct fscache_object
*object
,
1102 if (!test_and_set_bit(FSCACHE_OBJECT_RUN_AFTER_DEAD
,
1106 WARN(true, "FS-Cache object redispatched after death");