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/seq_file.h>
20 const char *fscache_object_states
[FSCACHE_OBJECT__NSTATES
] = {
21 [FSCACHE_OBJECT_INIT
] = "OBJECT_INIT",
22 [FSCACHE_OBJECT_LOOKING_UP
] = "OBJECT_LOOKING_UP",
23 [FSCACHE_OBJECT_CREATING
] = "OBJECT_CREATING",
24 [FSCACHE_OBJECT_AVAILABLE
] = "OBJECT_AVAILABLE",
25 [FSCACHE_OBJECT_ACTIVE
] = "OBJECT_ACTIVE",
26 [FSCACHE_OBJECT_UPDATING
] = "OBJECT_UPDATING",
27 [FSCACHE_OBJECT_DYING
] = "OBJECT_DYING",
28 [FSCACHE_OBJECT_LC_DYING
] = "OBJECT_LC_DYING",
29 [FSCACHE_OBJECT_ABORT_INIT
] = "OBJECT_ABORT_INIT",
30 [FSCACHE_OBJECT_RELEASING
] = "OBJECT_RELEASING",
31 [FSCACHE_OBJECT_RECYCLING
] = "OBJECT_RECYCLING",
32 [FSCACHE_OBJECT_WITHDRAWING
] = "OBJECT_WITHDRAWING",
33 [FSCACHE_OBJECT_DEAD
] = "OBJECT_DEAD",
35 EXPORT_SYMBOL(fscache_object_states
);
37 const char fscache_object_states_short
[FSCACHE_OBJECT__NSTATES
][5] = {
38 [FSCACHE_OBJECT_INIT
] = "INIT",
39 [FSCACHE_OBJECT_LOOKING_UP
] = "LOOK",
40 [FSCACHE_OBJECT_CREATING
] = "CRTN",
41 [FSCACHE_OBJECT_AVAILABLE
] = "AVBL",
42 [FSCACHE_OBJECT_ACTIVE
] = "ACTV",
43 [FSCACHE_OBJECT_UPDATING
] = "UPDT",
44 [FSCACHE_OBJECT_DYING
] = "DYNG",
45 [FSCACHE_OBJECT_LC_DYING
] = "LCDY",
46 [FSCACHE_OBJECT_ABORT_INIT
] = "ABTI",
47 [FSCACHE_OBJECT_RELEASING
] = "RELS",
48 [FSCACHE_OBJECT_RECYCLING
] = "RCYC",
49 [FSCACHE_OBJECT_WITHDRAWING
] = "WTHD",
50 [FSCACHE_OBJECT_DEAD
] = "DEAD",
53 static void fscache_object_slow_work_put_ref(struct slow_work
*);
54 static int fscache_object_slow_work_get_ref(struct slow_work
*);
55 static void fscache_object_slow_work_execute(struct slow_work
*);
56 #ifdef CONFIG_SLOW_WORK_PROC
57 static void fscache_object_slow_work_desc(struct slow_work
*, struct seq_file
*);
59 static void fscache_initialise_object(struct fscache_object
*);
60 static void fscache_lookup_object(struct fscache_object
*);
61 static void fscache_object_available(struct fscache_object
*);
62 static void fscache_release_object(struct fscache_object
*);
63 static void fscache_withdraw_object(struct fscache_object
*);
64 static void fscache_enqueue_dependents(struct fscache_object
*);
65 static void fscache_dequeue_object(struct fscache_object
*);
67 const struct slow_work_ops fscache_object_slow_work_ops
= {
69 .get_ref
= fscache_object_slow_work_get_ref
,
70 .put_ref
= fscache_object_slow_work_put_ref
,
71 .execute
= fscache_object_slow_work_execute
,
72 #ifdef CONFIG_SLOW_WORK_PROC
73 .desc
= fscache_object_slow_work_desc
,
76 EXPORT_SYMBOL(fscache_object_slow_work_ops
);
79 * we need to notify the parent when an op completes that we had outstanding
82 static inline void fscache_done_parent_op(struct fscache_object
*object
)
84 struct fscache_object
*parent
= object
->parent
;
86 _enter("OBJ%x {OBJ%x,%x}",
87 object
->debug_id
, parent
->debug_id
, parent
->n_ops
);
89 spin_lock_nested(&parent
->lock
, 1);
92 if (parent
->n_ops
== 0)
93 fscache_raise_event(parent
, FSCACHE_OBJECT_EV_CLEARED
);
94 spin_unlock(&parent
->lock
);
98 * process events that have been sent to an object's state machine
99 * - initiates parent lookup
100 * - does object lookup
101 * - does object creation
102 * - does object recycling and retirement
103 * - does object withdrawal
105 static void fscache_object_state_machine(struct fscache_object
*object
)
107 enum fscache_object_state new_state
;
109 ASSERT(object
!= NULL
);
111 _enter("{OBJ%x,%s,%lx}",
112 object
->debug_id
, fscache_object_states
[object
->state
],
115 switch (object
->state
) {
116 /* wait for the parent object to become ready */
117 case FSCACHE_OBJECT_INIT
:
119 ULONG_MAX
& ~(1 << FSCACHE_OBJECT_EV_CLEARED
);
120 fscache_initialise_object(object
);
123 /* look up the object metadata on disk */
124 case FSCACHE_OBJECT_LOOKING_UP
:
125 fscache_lookup_object(object
);
128 /* create the object metadata on disk */
129 case FSCACHE_OBJECT_CREATING
:
130 fscache_lookup_object(object
);
133 /* handle an object becoming available; start pending
134 * operations and queue dependent operations for processing */
135 case FSCACHE_OBJECT_AVAILABLE
:
136 fscache_object_available(object
);
139 /* normal running state */
140 case FSCACHE_OBJECT_ACTIVE
:
143 /* update the object metadata on disk */
144 case FSCACHE_OBJECT_UPDATING
:
145 clear_bit(FSCACHE_OBJECT_EV_UPDATE
, &object
->events
);
146 fscache_stat(&fscache_n_updates_run
);
147 fscache_stat(&fscache_n_cop_update_object
);
148 object
->cache
->ops
->update_object(object
);
149 fscache_stat_d(&fscache_n_cop_update_object
);
152 /* handle an object dying during lookup or creation */
153 case FSCACHE_OBJECT_LC_DYING
:
154 object
->event_mask
&= ~(1 << FSCACHE_OBJECT_EV_UPDATE
);
155 fscache_stat(&fscache_n_cop_lookup_complete
);
156 object
->cache
->ops
->lookup_complete(object
);
157 fscache_stat_d(&fscache_n_cop_lookup_complete
);
159 spin_lock(&object
->lock
);
160 object
->state
= FSCACHE_OBJECT_DYING
;
161 if (test_and_clear_bit(FSCACHE_COOKIE_CREATING
,
162 &object
->cookie
->flags
))
163 wake_up_bit(&object
->cookie
->flags
,
164 FSCACHE_COOKIE_CREATING
);
165 spin_unlock(&object
->lock
);
167 fscache_done_parent_op(object
);
169 /* wait for completion of all active operations on this object
170 * and the death of all child objects of this object */
171 case FSCACHE_OBJECT_DYING
:
173 clear_bit(FSCACHE_OBJECT_EV_CLEARED
, &object
->events
);
174 spin_lock(&object
->lock
);
175 _debug("dying OBJ%x {%d,%d}",
176 object
->debug_id
, object
->n_ops
, object
->n_children
);
177 if (object
->n_ops
== 0 && object
->n_children
== 0) {
178 object
->event_mask
&=
179 ~(1 << FSCACHE_OBJECT_EV_CLEARED
);
180 object
->event_mask
|=
181 (1 << FSCACHE_OBJECT_EV_WITHDRAW
) |
182 (1 << FSCACHE_OBJECT_EV_RETIRE
) |
183 (1 << FSCACHE_OBJECT_EV_RELEASE
) |
184 (1 << FSCACHE_OBJECT_EV_ERROR
);
186 object
->event_mask
&=
187 ~((1 << FSCACHE_OBJECT_EV_WITHDRAW
) |
188 (1 << FSCACHE_OBJECT_EV_RETIRE
) |
189 (1 << FSCACHE_OBJECT_EV_RELEASE
) |
190 (1 << FSCACHE_OBJECT_EV_ERROR
));
191 object
->event_mask
|=
192 1 << FSCACHE_OBJECT_EV_CLEARED
;
194 spin_unlock(&object
->lock
);
195 fscache_enqueue_dependents(object
);
196 goto terminal_transit
;
198 /* handle an abort during initialisation */
199 case FSCACHE_OBJECT_ABORT_INIT
:
200 _debug("handle abort init %lx", object
->events
);
201 object
->event_mask
&= ~(1 << FSCACHE_OBJECT_EV_UPDATE
);
203 spin_lock(&object
->lock
);
204 fscache_dequeue_object(object
);
206 object
->state
= FSCACHE_OBJECT_DYING
;
207 if (test_and_clear_bit(FSCACHE_COOKIE_CREATING
,
208 &object
->cookie
->flags
))
209 wake_up_bit(&object
->cookie
->flags
,
210 FSCACHE_COOKIE_CREATING
);
211 spin_unlock(&object
->lock
);
214 /* handle the netfs releasing an object and possibly marking it
216 case FSCACHE_OBJECT_RELEASING
:
217 case FSCACHE_OBJECT_RECYCLING
:
218 object
->event_mask
&=
219 ~((1 << FSCACHE_OBJECT_EV_WITHDRAW
) |
220 (1 << FSCACHE_OBJECT_EV_RETIRE
) |
221 (1 << FSCACHE_OBJECT_EV_RELEASE
) |
222 (1 << FSCACHE_OBJECT_EV_ERROR
));
223 fscache_release_object(object
);
224 spin_lock(&object
->lock
);
225 object
->state
= FSCACHE_OBJECT_DEAD
;
226 spin_unlock(&object
->lock
);
227 fscache_stat(&fscache_n_object_dead
);
228 goto terminal_transit
;
230 /* handle the parent cache of this object being withdrawn from
232 case FSCACHE_OBJECT_WITHDRAWING
:
233 object
->event_mask
&=
234 ~((1 << FSCACHE_OBJECT_EV_WITHDRAW
) |
235 (1 << FSCACHE_OBJECT_EV_RETIRE
) |
236 (1 << FSCACHE_OBJECT_EV_RELEASE
) |
237 (1 << FSCACHE_OBJECT_EV_ERROR
));
238 fscache_withdraw_object(object
);
239 spin_lock(&object
->lock
);
240 object
->state
= FSCACHE_OBJECT_DEAD
;
241 spin_unlock(&object
->lock
);
242 fscache_stat(&fscache_n_object_dead
);
243 goto terminal_transit
;
245 /* complain about the object being woken up once it is
247 case FSCACHE_OBJECT_DEAD
:
248 printk(KERN_ERR
"FS-Cache:"
249 " Unexpected event in dead state %lx\n",
250 object
->events
& object
->event_mask
);
254 printk(KERN_ERR
"FS-Cache: Unknown object state %u\n",
259 /* determine the transition from a lookup state */
261 switch (fls(object
->events
& object
->event_mask
) - 1) {
262 case FSCACHE_OBJECT_EV_WITHDRAW
:
263 case FSCACHE_OBJECT_EV_RETIRE
:
264 case FSCACHE_OBJECT_EV_RELEASE
:
265 case FSCACHE_OBJECT_EV_ERROR
:
266 new_state
= FSCACHE_OBJECT_LC_DYING
;
268 case FSCACHE_OBJECT_EV_REQUEUE
:
271 goto done
; /* sleep until event */
273 goto unsupported_event
;
276 /* determine the transition from an active state */
278 switch (fls(object
->events
& object
->event_mask
) - 1) {
279 case FSCACHE_OBJECT_EV_WITHDRAW
:
280 case FSCACHE_OBJECT_EV_RETIRE
:
281 case FSCACHE_OBJECT_EV_RELEASE
:
282 case FSCACHE_OBJECT_EV_ERROR
:
283 new_state
= FSCACHE_OBJECT_DYING
;
285 case FSCACHE_OBJECT_EV_UPDATE
:
286 new_state
= FSCACHE_OBJECT_UPDATING
;
289 new_state
= FSCACHE_OBJECT_ACTIVE
;
290 goto change_state
; /* sleep until event */
292 goto unsupported_event
;
295 /* determine the transition from a terminal state */
297 switch (fls(object
->events
& object
->event_mask
) - 1) {
298 case FSCACHE_OBJECT_EV_WITHDRAW
:
299 new_state
= FSCACHE_OBJECT_WITHDRAWING
;
301 case FSCACHE_OBJECT_EV_RETIRE
:
302 new_state
= FSCACHE_OBJECT_RECYCLING
;
304 case FSCACHE_OBJECT_EV_RELEASE
:
305 new_state
= FSCACHE_OBJECT_RELEASING
;
307 case FSCACHE_OBJECT_EV_ERROR
:
308 new_state
= FSCACHE_OBJECT_WITHDRAWING
;
310 case FSCACHE_OBJECT_EV_CLEARED
:
311 new_state
= FSCACHE_OBJECT_DYING
;
314 goto done
; /* sleep until event */
316 goto unsupported_event
;
320 spin_lock(&object
->lock
);
321 object
->state
= new_state
;
322 spin_unlock(&object
->lock
);
325 _leave(" [->%s]", fscache_object_states
[object
->state
]);
329 printk(KERN_ERR
"FS-Cache:"
330 " Unsupported event %lx [mask %lx] in state %s\n",
331 object
->events
, object
->event_mask
,
332 fscache_object_states
[object
->state
]);
339 static void fscache_object_slow_work_execute(struct slow_work
*work
)
341 struct fscache_object
*object
=
342 container_of(work
, struct fscache_object
, work
);
345 _enter("{OBJ%x}", object
->debug_id
);
347 clear_bit(FSCACHE_OBJECT_EV_REQUEUE
, &object
->events
);
350 fscache_object_state_machine(object
);
351 fscache_hist(fscache_objs_histogram
, start
);
352 if (object
->events
& object
->event_mask
)
353 fscache_enqueue_object(object
);
357 * describe an object for slow-work debugging
359 #ifdef CONFIG_SLOW_WORK_PROC
360 static void fscache_object_slow_work_desc(struct slow_work
*work
,
363 struct fscache_object
*object
=
364 container_of(work
, struct fscache_object
, work
);
366 seq_printf(m
, "FSC: OBJ%x: %s",
368 fscache_object_states_short
[object
->state
]);
373 * initialise an object
374 * - check the specified object's parent to see if we can make use of it
375 * immediately to do a creation
376 * - we may need to start the process of creating a parent and we need to wait
377 * for the parent's lookup and creation to complete if it's not there yet
378 * - an object's cookie is pinned until we clear FSCACHE_COOKIE_CREATING on the
379 * leaf-most cookies of the object and all its children
381 static void fscache_initialise_object(struct fscache_object
*object
)
383 struct fscache_object
*parent
;
386 ASSERT(object
->cookie
!= NULL
);
387 ASSERT(object
->cookie
->parent
!= NULL
);
388 ASSERT(list_empty(&object
->work
.link
));
390 if (object
->events
& ((1 << FSCACHE_OBJECT_EV_ERROR
) |
391 (1 << FSCACHE_OBJECT_EV_RELEASE
) |
392 (1 << FSCACHE_OBJECT_EV_RETIRE
) |
393 (1 << FSCACHE_OBJECT_EV_WITHDRAW
))) {
394 _debug("abort init %lx", object
->events
);
395 spin_lock(&object
->lock
);
396 object
->state
= FSCACHE_OBJECT_ABORT_INIT
;
397 spin_unlock(&object
->lock
);
401 spin_lock(&object
->cookie
->lock
);
402 spin_lock_nested(&object
->cookie
->parent
->lock
, 1);
404 parent
= object
->parent
;
407 set_bit(FSCACHE_OBJECT_EV_WITHDRAW
, &object
->events
);
409 spin_lock(&object
->lock
);
410 spin_lock_nested(&parent
->lock
, 1);
411 _debug("parent %s", fscache_object_states
[parent
->state
]);
413 if (parent
->state
>= FSCACHE_OBJECT_DYING
) {
414 _debug("bad parent");
415 set_bit(FSCACHE_OBJECT_EV_WITHDRAW
, &object
->events
);
416 } else if (parent
->state
< FSCACHE_OBJECT_AVAILABLE
) {
419 /* we may get woken up in this state by child objects
420 * binding on to us, so we need to make sure we don't
421 * add ourself to the list multiple times */
422 if (list_empty(&object
->dep_link
)) {
423 fscache_stat(&fscache_n_cop_grab_object
);
424 object
->cache
->ops
->grab_object(object
);
425 fscache_stat_d(&fscache_n_cop_grab_object
);
426 list_add(&object
->dep_link
,
427 &parent
->dependents
);
429 /* fscache_acquire_non_index_cookie() uses this
430 * to wake the chain up */
431 if (parent
->state
== FSCACHE_OBJECT_INIT
)
432 fscache_enqueue_object(parent
);
438 object
->lookup_jif
= jiffies
;
439 object
->state
= FSCACHE_OBJECT_LOOKING_UP
;
440 set_bit(FSCACHE_OBJECT_EV_REQUEUE
, &object
->events
);
443 spin_unlock(&parent
->lock
);
444 spin_unlock(&object
->lock
);
447 spin_unlock(&object
->cookie
->parent
->lock
);
448 spin_unlock(&object
->cookie
->lock
);
453 * look an object up in the cache from which it was allocated
454 * - we hold an "access lock" on the parent object, so the parent object cannot
455 * be withdrawn by either party till we've finished
456 * - an object's cookie is pinned until we clear FSCACHE_COOKIE_CREATING on the
457 * leaf-most cookies of the object and all its children
459 static void fscache_lookup_object(struct fscache_object
*object
)
461 struct fscache_cookie
*cookie
= object
->cookie
;
462 struct fscache_object
*parent
;
466 parent
= object
->parent
;
467 ASSERT(parent
!= NULL
);
468 ASSERTCMP(parent
->n_ops
, >, 0);
469 ASSERTCMP(parent
->n_obj_ops
, >, 0);
471 /* make sure the parent is still available */
472 ASSERTCMP(parent
->state
, >=, FSCACHE_OBJECT_AVAILABLE
);
474 if (parent
->state
>= FSCACHE_OBJECT_DYING
||
475 test_bit(FSCACHE_IOERROR
, &object
->cache
->flags
)) {
476 _debug("unavailable");
477 set_bit(FSCACHE_OBJECT_EV_WITHDRAW
, &object
->events
);
482 _debug("LOOKUP \"%s/%s\" in \"%s\"",
483 parent
->cookie
->def
->name
, cookie
->def
->name
,
484 object
->cache
->tag
->name
);
486 fscache_stat(&fscache_n_object_lookups
);
487 fscache_stat(&fscache_n_cop_lookup_object
);
488 object
->cache
->ops
->lookup_object(object
);
489 fscache_stat_d(&fscache_n_cop_lookup_object
);
491 if (test_bit(FSCACHE_OBJECT_EV_ERROR
, &object
->events
))
492 set_bit(FSCACHE_COOKIE_UNAVAILABLE
, &cookie
->flags
);
498 * fscache_object_lookup_negative - Note negative cookie lookup
499 * @object: Object pointing to cookie to mark
501 * Note negative lookup, permitting those waiting to read data from an already
502 * existing backing object to continue as there's no data for them to read.
504 void fscache_object_lookup_negative(struct fscache_object
*object
)
506 struct fscache_cookie
*cookie
= object
->cookie
;
509 object
->debug_id
, fscache_object_states
[object
->state
]);
511 spin_lock(&object
->lock
);
512 if (object
->state
== FSCACHE_OBJECT_LOOKING_UP
) {
513 fscache_stat(&fscache_n_object_lookups_negative
);
515 /* transit here to allow write requests to begin stacking up
516 * and read requests to begin returning ENODATA */
517 object
->state
= FSCACHE_OBJECT_CREATING
;
518 spin_unlock(&object
->lock
);
520 set_bit(FSCACHE_COOKIE_PENDING_FILL
, &cookie
->flags
);
521 set_bit(FSCACHE_COOKIE_NO_DATA_YET
, &cookie
->flags
);
523 _debug("wake up lookup %p", &cookie
->flags
);
524 smp_mb__before_clear_bit();
525 clear_bit(FSCACHE_COOKIE_LOOKING_UP
, &cookie
->flags
);
526 smp_mb__after_clear_bit();
527 wake_up_bit(&cookie
->flags
, FSCACHE_COOKIE_LOOKING_UP
);
528 set_bit(FSCACHE_OBJECT_EV_REQUEUE
, &object
->events
);
530 ASSERTCMP(object
->state
, ==, FSCACHE_OBJECT_CREATING
);
531 spin_unlock(&object
->lock
);
536 EXPORT_SYMBOL(fscache_object_lookup_negative
);
539 * fscache_obtained_object - Note successful object lookup or creation
540 * @object: Object pointing to cookie to mark
542 * Note successful lookup and/or creation, permitting those waiting to write
543 * data to a backing object to continue.
545 * Note that after calling this, an object's cookie may be relinquished by the
546 * netfs, and so must be accessed with object lock held.
548 void fscache_obtained_object(struct fscache_object
*object
)
550 struct fscache_cookie
*cookie
= object
->cookie
;
553 object
->debug_id
, fscache_object_states
[object
->state
]);
555 /* if we were still looking up, then we must have a positive lookup
556 * result, in which case there may be data available */
557 spin_lock(&object
->lock
);
558 if (object
->state
== FSCACHE_OBJECT_LOOKING_UP
) {
559 fscache_stat(&fscache_n_object_lookups_positive
);
561 clear_bit(FSCACHE_COOKIE_NO_DATA_YET
, &cookie
->flags
);
563 object
->state
= FSCACHE_OBJECT_AVAILABLE
;
564 spin_unlock(&object
->lock
);
566 smp_mb__before_clear_bit();
567 clear_bit(FSCACHE_COOKIE_LOOKING_UP
, &cookie
->flags
);
568 smp_mb__after_clear_bit();
569 wake_up_bit(&cookie
->flags
, FSCACHE_COOKIE_LOOKING_UP
);
570 set_bit(FSCACHE_OBJECT_EV_REQUEUE
, &object
->events
);
572 ASSERTCMP(object
->state
, ==, FSCACHE_OBJECT_CREATING
);
573 fscache_stat(&fscache_n_object_created
);
575 object
->state
= FSCACHE_OBJECT_AVAILABLE
;
576 spin_unlock(&object
->lock
);
577 set_bit(FSCACHE_OBJECT_EV_REQUEUE
, &object
->events
);
581 if (test_and_clear_bit(FSCACHE_COOKIE_CREATING
, &cookie
->flags
))
582 wake_up_bit(&cookie
->flags
, FSCACHE_COOKIE_CREATING
);
586 EXPORT_SYMBOL(fscache_obtained_object
);
589 * handle an object that has just become available
591 static void fscache_object_available(struct fscache_object
*object
)
593 _enter("{OBJ%x}", object
->debug_id
);
595 spin_lock(&object
->lock
);
597 if (test_and_clear_bit(FSCACHE_COOKIE_CREATING
, &object
->cookie
->flags
))
598 wake_up_bit(&object
->cookie
->flags
, FSCACHE_COOKIE_CREATING
);
600 fscache_done_parent_op(object
);
601 if (object
->n_in_progress
== 0) {
602 if (object
->n_ops
> 0) {
603 ASSERTCMP(object
->n_ops
, >=, object
->n_obj_ops
);
604 ASSERTIF(object
->n_ops
> object
->n_obj_ops
,
605 !list_empty(&object
->pending_ops
));
606 fscache_start_operations(object
);
608 ASSERT(list_empty(&object
->pending_ops
));
611 spin_unlock(&object
->lock
);
613 fscache_stat(&fscache_n_cop_lookup_complete
);
614 object
->cache
->ops
->lookup_complete(object
);
615 fscache_stat_d(&fscache_n_cop_lookup_complete
);
616 fscache_enqueue_dependents(object
);
618 fscache_hist(fscache_obj_instantiate_histogram
, object
->lookup_jif
);
619 fscache_stat(&fscache_n_object_avail
);
625 * drop an object's attachments
627 static void fscache_drop_object(struct fscache_object
*object
)
629 struct fscache_object
*parent
= object
->parent
;
630 struct fscache_cache
*cache
= object
->cache
;
632 _enter("{OBJ%x,%d}", object
->debug_id
, object
->n_children
);
634 spin_lock(&cache
->object_list_lock
);
635 list_del_init(&object
->cache_link
);
636 spin_unlock(&cache
->object_list_lock
);
638 fscache_stat(&fscache_n_cop_drop_object
);
639 cache
->ops
->drop_object(object
);
640 fscache_stat_d(&fscache_n_cop_drop_object
);
643 _debug("release parent OBJ%x {%d}",
644 parent
->debug_id
, parent
->n_children
);
646 spin_lock(&parent
->lock
);
647 parent
->n_children
--;
648 if (parent
->n_children
== 0)
649 fscache_raise_event(parent
, FSCACHE_OBJECT_EV_CLEARED
);
650 spin_unlock(&parent
->lock
);
651 object
->parent
= NULL
;
654 /* this just shifts the object release to the slow work processor */
655 fscache_stat(&fscache_n_cop_put_object
);
656 object
->cache
->ops
->put_object(object
);
657 fscache_stat_d(&fscache_n_cop_put_object
);
663 * release or recycle an object that the netfs has discarded
665 static void fscache_release_object(struct fscache_object
*object
)
669 fscache_drop_object(object
);
673 * withdraw an object from active service
675 static void fscache_withdraw_object(struct fscache_object
*object
)
677 struct fscache_cookie
*cookie
;
682 spin_lock(&object
->lock
);
683 cookie
= object
->cookie
;
685 /* need to get the cookie lock before the object lock, starting
686 * from the object pointer */
687 atomic_inc(&cookie
->usage
);
688 spin_unlock(&object
->lock
);
691 spin_lock(&cookie
->lock
);
692 spin_lock(&object
->lock
);
694 if (object
->cookie
== cookie
) {
695 hlist_del_init(&object
->cookie_link
);
696 object
->cookie
= NULL
;
699 spin_unlock(&cookie
->lock
);
700 fscache_cookie_put(cookie
);
702 fscache_cookie_put(cookie
);
705 spin_unlock(&object
->lock
);
707 fscache_drop_object(object
);
711 * withdraw an object from active service at the behest of the cache
712 * - need break the links to a cached object cookie
713 * - called under two situations:
714 * (1) recycler decides to reclaim an in-use object
715 * (2) a cache is unmounted
716 * - have to take care as the cookie can be being relinquished by the netfs
718 * - the object is pinned by the caller holding a refcount on it
720 void fscache_withdrawing_object(struct fscache_cache
*cache
,
721 struct fscache_object
*object
)
723 bool enqueue
= false;
725 _enter(",OBJ%x", object
->debug_id
);
727 spin_lock(&object
->lock
);
728 if (object
->state
< FSCACHE_OBJECT_WITHDRAWING
) {
729 object
->state
= FSCACHE_OBJECT_WITHDRAWING
;
732 spin_unlock(&object
->lock
);
735 fscache_enqueue_object(object
);
741 * allow the slow work item processor to get a ref on an object
743 static int fscache_object_slow_work_get_ref(struct slow_work
*work
)
745 struct fscache_object
*object
=
746 container_of(work
, struct fscache_object
, work
);
749 fscache_stat(&fscache_n_cop_grab_object
);
750 ret
= object
->cache
->ops
->grab_object(object
) ? 0 : -EAGAIN
;
751 fscache_stat_d(&fscache_n_cop_grab_object
);
756 * allow the slow work item processor to discard a ref on a work item
758 static void fscache_object_slow_work_put_ref(struct slow_work
*work
)
760 struct fscache_object
*object
=
761 container_of(work
, struct fscache_object
, work
);
763 fscache_stat(&fscache_n_cop_put_object
);
764 object
->cache
->ops
->put_object(object
);
765 fscache_stat_d(&fscache_n_cop_put_object
);
769 * enqueue an object for metadata-type processing
771 void fscache_enqueue_object(struct fscache_object
*object
)
773 _enter("{OBJ%x}", object
->debug_id
);
775 slow_work_enqueue(&object
->work
);
779 * enqueue the dependents of an object for metadata-type processing
780 * - the caller must hold the object's lock
781 * - this may cause an already locked object to wind up being processed again
783 static void fscache_enqueue_dependents(struct fscache_object
*object
)
785 struct fscache_object
*dep
;
787 _enter("{OBJ%x}", object
->debug_id
);
789 if (list_empty(&object
->dependents
))
792 spin_lock(&object
->lock
);
794 while (!list_empty(&object
->dependents
)) {
795 dep
= list_entry(object
->dependents
.next
,
796 struct fscache_object
, dep_link
);
797 list_del_init(&dep
->dep_link
);
800 /* sort onto appropriate lists */
801 fscache_enqueue_object(dep
);
802 fscache_stat(&fscache_n_cop_put_object
);
803 dep
->cache
->ops
->put_object(dep
);
804 fscache_stat_d(&fscache_n_cop_put_object
);
806 if (!list_empty(&object
->dependents
))
807 cond_resched_lock(&object
->lock
);
810 spin_unlock(&object
->lock
);
814 * remove an object from whatever queue it's waiting on
815 * - the caller must hold object->lock
817 void fscache_dequeue_object(struct fscache_object
*object
)
819 _enter("{OBJ%x}", object
->debug_id
);
821 if (!list_empty(&object
->dep_link
)) {
822 spin_lock(&object
->parent
->lock
);
823 list_del_init(&object
->dep_link
);
824 spin_unlock(&object
->parent
->lock
);
831 * fscache_check_aux - Ask the netfs whether an object on disk is still valid
832 * @object: The object to ask about
833 * @data: The auxiliary data for the object
834 * @datalen: The size of the auxiliary data
836 * This function consults the netfs about the coherency state of an object
838 enum fscache_checkaux
fscache_check_aux(struct fscache_object
*object
,
839 const void *data
, uint16_t datalen
)
841 enum fscache_checkaux result
;
843 if (!object
->cookie
->def
->check_aux
) {
844 fscache_stat(&fscache_n_checkaux_none
);
845 return FSCACHE_CHECKAUX_OKAY
;
848 result
= object
->cookie
->def
->check_aux(object
->cookie
->netfs_data
,
851 /* entry okay as is */
852 case FSCACHE_CHECKAUX_OKAY
:
853 fscache_stat(&fscache_n_checkaux_okay
);
856 /* entry requires update */
857 case FSCACHE_CHECKAUX_NEEDS_UPDATE
:
858 fscache_stat(&fscache_n_checkaux_update
);
861 /* entry requires deletion */
862 case FSCACHE_CHECKAUX_OBSOLETE
:
863 fscache_stat(&fscache_n_checkaux_obsolete
);
872 EXPORT_SYMBOL(fscache_check_aux
);