FS-Cache: Add counters for entry/exit to/from cache operation functions
[linux-2.6.git] / fs / fscache / object.c
1 /* FS-Cache object state machine handler
2  *
3  * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4  * Written by David Howells (dhowells@redhat.com)
5  *
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.
10  *
11  * See Documentation/filesystems/caching/object.txt for a description of the
12  * object state machine and the in-kernel representations.
13  */
14
15 #define FSCACHE_DEBUG_LEVEL COOKIE
16 #include <linux/module.h>
17 #include <linux/seq_file.h>
18 #include "internal.h"
19
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",
34 };
35 EXPORT_SYMBOL(fscache_object_states);
36
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",
51 };
52
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 *);
58 #endif
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 *);
66
67 const struct slow_work_ops fscache_object_slow_work_ops = {
68         .owner          = THIS_MODULE,
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,
74 #endif
75 };
76 EXPORT_SYMBOL(fscache_object_slow_work_ops);
77
78 /*
79  * we need to notify the parent when an op completes that we had outstanding
80  * upon it
81  */
82 static inline void fscache_done_parent_op(struct fscache_object *object)
83 {
84         struct fscache_object *parent = object->parent;
85
86         _enter("OBJ%x {OBJ%x,%x}",
87                object->debug_id, parent->debug_id, parent->n_ops);
88
89         spin_lock_nested(&parent->lock, 1);
90         parent->n_ops--;
91         parent->n_obj_ops--;
92         if (parent->n_ops == 0)
93                 fscache_raise_event(parent, FSCACHE_OBJECT_EV_CLEARED);
94         spin_unlock(&parent->lock);
95 }
96
97 /*
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
104  */
105 static void fscache_object_state_machine(struct fscache_object *object)
106 {
107         enum fscache_object_state new_state;
108
109         ASSERT(object != NULL);
110
111         _enter("{OBJ%x,%s,%lx}",
112                object->debug_id, fscache_object_states[object->state],
113                object->events);
114
115         switch (object->state) {
116                 /* wait for the parent object to become ready */
117         case FSCACHE_OBJECT_INIT:
118                 object->event_mask =
119                         ULONG_MAX & ~(1 << FSCACHE_OBJECT_EV_CLEARED);
120                 fscache_initialise_object(object);
121                 goto done;
122
123                 /* look up the object metadata on disk */
124         case FSCACHE_OBJECT_LOOKING_UP:
125                 fscache_lookup_object(object);
126                 goto lookup_transit;
127
128                 /* create the object metadata on disk */
129         case FSCACHE_OBJECT_CREATING:
130                 fscache_lookup_object(object);
131                 goto lookup_transit;
132
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);
137                 goto active_transit;
138
139                 /* normal running state */
140         case FSCACHE_OBJECT_ACTIVE:
141                 goto active_transit;
142
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);
150                 goto active_transit;
151
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);
158
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);
166
167                 fscache_done_parent_op(object);
168
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:
172         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);
185                 } else {
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;
193                 }
194                 spin_unlock(&object->lock);
195                 fscache_enqueue_dependents(object);
196                 goto terminal_transit;
197
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);
202
203                 spin_lock(&object->lock);
204                 fscache_dequeue_object(object);
205
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);
212                 goto dying;
213
214                 /* handle the netfs releasing an object and possibly marking it
215                  * obsolete too */
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;
229
230                 /* handle the parent cache of this object being withdrawn from
231                  * active service */
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;
244
245                 /* complain about the object being woken up once it is
246                  * deceased */
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);
251                 BUG();
252
253         default:
254                 printk(KERN_ERR "FS-Cache: Unknown object state %u\n",
255                        object->state);
256                 BUG();
257         }
258
259         /* determine the transition from a lookup state */
260 lookup_transit:
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;
267                 goto change_state;
268         case FSCACHE_OBJECT_EV_REQUEUE:
269                 goto done;
270         case -1:
271                 goto done; /* sleep until event */
272         default:
273                 goto unsupported_event;
274         }
275
276         /* determine the transition from an active state */
277 active_transit:
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;
284                 goto change_state;
285         case FSCACHE_OBJECT_EV_UPDATE:
286                 new_state = FSCACHE_OBJECT_UPDATING;
287                 goto change_state;
288         case -1:
289                 new_state = FSCACHE_OBJECT_ACTIVE;
290                 goto change_state; /* sleep until event */
291         default:
292                 goto unsupported_event;
293         }
294
295         /* determine the transition from a terminal state */
296 terminal_transit:
297         switch (fls(object->events & object->event_mask) - 1) {
298         case FSCACHE_OBJECT_EV_WITHDRAW:
299                 new_state = FSCACHE_OBJECT_WITHDRAWING;
300                 goto change_state;
301         case FSCACHE_OBJECT_EV_RETIRE:
302                 new_state = FSCACHE_OBJECT_RECYCLING;
303                 goto change_state;
304         case FSCACHE_OBJECT_EV_RELEASE:
305                 new_state = FSCACHE_OBJECT_RELEASING;
306                 goto change_state;
307         case FSCACHE_OBJECT_EV_ERROR:
308                 new_state = FSCACHE_OBJECT_WITHDRAWING;
309                 goto change_state;
310         case FSCACHE_OBJECT_EV_CLEARED:
311                 new_state = FSCACHE_OBJECT_DYING;
312                 goto change_state;
313         case -1:
314                 goto done; /* sleep until event */
315         default:
316                 goto unsupported_event;
317         }
318
319 change_state:
320         spin_lock(&object->lock);
321         object->state = new_state;
322         spin_unlock(&object->lock);
323
324 done:
325         _leave(" [->%s]", fscache_object_states[object->state]);
326         return;
327
328 unsupported_event:
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]);
333         BUG();
334 }
335
336 /*
337  * execute an object
338  */
339 static void fscache_object_slow_work_execute(struct slow_work *work)
340 {
341         struct fscache_object *object =
342                 container_of(work, struct fscache_object, work);
343         unsigned long start;
344
345         _enter("{OBJ%x}", object->debug_id);
346
347         clear_bit(FSCACHE_OBJECT_EV_REQUEUE, &object->events);
348
349         start = jiffies;
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);
354 }
355
356 /*
357  * describe an object for slow-work debugging
358  */
359 #ifdef CONFIG_SLOW_WORK_PROC
360 static void fscache_object_slow_work_desc(struct slow_work *work,
361                                           struct seq_file *m)
362 {
363         struct fscache_object *object =
364                 container_of(work, struct fscache_object, work);
365
366         seq_printf(m, "FSC: OBJ%x: %s",
367                    object->debug_id,
368                    fscache_object_states_short[object->state]);
369 }
370 #endif
371
372 /*
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
380  */
381 static void fscache_initialise_object(struct fscache_object *object)
382 {
383         struct fscache_object *parent;
384
385         _enter("");
386         ASSERT(object->cookie != NULL);
387         ASSERT(object->cookie->parent != NULL);
388         ASSERT(list_empty(&object->work.link));
389
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);
398                 return;
399         }
400
401         spin_lock(&object->cookie->lock);
402         spin_lock_nested(&object->cookie->parent->lock, 1);
403
404         parent = object->parent;
405         if (!parent) {
406                 _debug("no parent");
407                 set_bit(FSCACHE_OBJECT_EV_WITHDRAW, &object->events);
408         } else {
409                 spin_lock(&object->lock);
410                 spin_lock_nested(&parent->lock, 1);
411                 _debug("parent %s", fscache_object_states[parent->state]);
412
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) {
417                         _debug("wait");
418
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);
428
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);
433                         }
434                 } else {
435                         _debug("go");
436                         parent->n_ops++;
437                         parent->n_obj_ops++;
438                         object->lookup_jif = jiffies;
439                         object->state = FSCACHE_OBJECT_LOOKING_UP;
440                         set_bit(FSCACHE_OBJECT_EV_REQUEUE, &object->events);
441                 }
442
443                 spin_unlock(&parent->lock);
444                 spin_unlock(&object->lock);
445         }
446
447         spin_unlock(&object->cookie->parent->lock);
448         spin_unlock(&object->cookie->lock);
449         _leave("");
450 }
451
452 /*
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
458  */
459 static void fscache_lookup_object(struct fscache_object *object)
460 {
461         struct fscache_cookie *cookie = object->cookie;
462         struct fscache_object *parent;
463
464         _enter("");
465
466         parent = object->parent;
467         ASSERT(parent != NULL);
468         ASSERTCMP(parent->n_ops, >, 0);
469         ASSERTCMP(parent->n_obj_ops, >, 0);
470
471         /* make sure the parent is still available */
472         ASSERTCMP(parent->state, >=, FSCACHE_OBJECT_AVAILABLE);
473
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);
478                 _leave("");
479                 return;
480         }
481
482         _debug("LOOKUP \"%s/%s\" in \"%s\"",
483                parent->cookie->def->name, cookie->def->name,
484                object->cache->tag->name);
485
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);
490
491         if (test_bit(FSCACHE_OBJECT_EV_ERROR, &object->events))
492                 set_bit(FSCACHE_COOKIE_UNAVAILABLE, &cookie->flags);
493
494         _leave("");
495 }
496
497 /**
498  * fscache_object_lookup_negative - Note negative cookie lookup
499  * @object: Object pointing to cookie to mark
500  *
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.
503  */
504 void fscache_object_lookup_negative(struct fscache_object *object)
505 {
506         struct fscache_cookie *cookie = object->cookie;
507
508         _enter("{OBJ%x,%s}",
509                object->debug_id, fscache_object_states[object->state]);
510
511         spin_lock(&object->lock);
512         if (object->state == FSCACHE_OBJECT_LOOKING_UP) {
513                 fscache_stat(&fscache_n_object_lookups_negative);
514
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);
519
520                 set_bit(FSCACHE_COOKIE_PENDING_FILL, &cookie->flags);
521                 set_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
522
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);
529         } else {
530                 ASSERTCMP(object->state, ==, FSCACHE_OBJECT_CREATING);
531                 spin_unlock(&object->lock);
532         }
533
534         _leave("");
535 }
536 EXPORT_SYMBOL(fscache_object_lookup_negative);
537
538 /**
539  * fscache_obtained_object - Note successful object lookup or creation
540  * @object: Object pointing to cookie to mark
541  *
542  * Note successful lookup and/or creation, permitting those waiting to write
543  * data to a backing object to continue.
544  *
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.
547  */
548 void fscache_obtained_object(struct fscache_object *object)
549 {
550         struct fscache_cookie *cookie = object->cookie;
551
552         _enter("{OBJ%x,%s}",
553                object->debug_id, fscache_object_states[object->state]);
554
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);
560
561                 clear_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
562
563                 object->state = FSCACHE_OBJECT_AVAILABLE;
564                 spin_unlock(&object->lock);
565
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);
571         } else {
572                 ASSERTCMP(object->state, ==, FSCACHE_OBJECT_CREATING);
573                 fscache_stat(&fscache_n_object_created);
574
575                 object->state = FSCACHE_OBJECT_AVAILABLE;
576                 spin_unlock(&object->lock);
577                 set_bit(FSCACHE_OBJECT_EV_REQUEUE, &object->events);
578                 smp_wmb();
579         }
580
581         if (test_and_clear_bit(FSCACHE_COOKIE_CREATING, &cookie->flags))
582                 wake_up_bit(&cookie->flags, FSCACHE_COOKIE_CREATING);
583
584         _leave("");
585 }
586 EXPORT_SYMBOL(fscache_obtained_object);
587
588 /*
589  * handle an object that has just become available
590  */
591 static void fscache_object_available(struct fscache_object *object)
592 {
593         _enter("{OBJ%x}", object->debug_id);
594
595         spin_lock(&object->lock);
596
597         if (test_and_clear_bit(FSCACHE_COOKIE_CREATING, &object->cookie->flags))
598                 wake_up_bit(&object->cookie->flags, FSCACHE_COOKIE_CREATING);
599
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);
607                 } else {
608                         ASSERT(list_empty(&object->pending_ops));
609                 }
610         }
611         spin_unlock(&object->lock);
612
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);
617
618         fscache_hist(fscache_obj_instantiate_histogram, object->lookup_jif);
619         fscache_stat(&fscache_n_object_avail);
620
621         _leave("");
622 }
623
624 /*
625  * drop an object's attachments
626  */
627 static void fscache_drop_object(struct fscache_object *object)
628 {
629         struct fscache_object *parent = object->parent;
630         struct fscache_cache *cache = object->cache;
631
632         _enter("{OBJ%x,%d}", object->debug_id, object->n_children);
633
634         spin_lock(&cache->object_list_lock);
635         list_del_init(&object->cache_link);
636         spin_unlock(&cache->object_list_lock);
637
638         fscache_stat(&fscache_n_cop_drop_object);
639         cache->ops->drop_object(object);
640         fscache_stat_d(&fscache_n_cop_drop_object);
641
642         if (parent) {
643                 _debug("release parent OBJ%x {%d}",
644                        parent->debug_id, parent->n_children);
645
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;
652         }
653
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);
658
659         _leave("");
660 }
661
662 /*
663  * release or recycle an object that the netfs has discarded
664  */
665 static void fscache_release_object(struct fscache_object *object)
666 {
667         _enter("");
668
669         fscache_drop_object(object);
670 }
671
672 /*
673  * withdraw an object from active service
674  */
675 static void fscache_withdraw_object(struct fscache_object *object)
676 {
677         struct fscache_cookie *cookie;
678         bool detached;
679
680         _enter("");
681
682         spin_lock(&object->lock);
683         cookie = object->cookie;
684         if (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);
689
690                 detached = false;
691                 spin_lock(&cookie->lock);
692                 spin_lock(&object->lock);
693
694                 if (object->cookie == cookie) {
695                         hlist_del_init(&object->cookie_link);
696                         object->cookie = NULL;
697                         detached = true;
698                 }
699                 spin_unlock(&cookie->lock);
700                 fscache_cookie_put(cookie);
701                 if (detached)
702                         fscache_cookie_put(cookie);
703         }
704
705         spin_unlock(&object->lock);
706
707         fscache_drop_object(object);
708 }
709
710 /*
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
717  *   simultaneously
718  * - the object is pinned by the caller holding a refcount on it
719  */
720 void fscache_withdrawing_object(struct fscache_cache *cache,
721                                 struct fscache_object *object)
722 {
723         bool enqueue = false;
724
725         _enter(",OBJ%x", object->debug_id);
726
727         spin_lock(&object->lock);
728         if (object->state < FSCACHE_OBJECT_WITHDRAWING) {
729                 object->state = FSCACHE_OBJECT_WITHDRAWING;
730                 enqueue = true;
731         }
732         spin_unlock(&object->lock);
733
734         if (enqueue)
735                 fscache_enqueue_object(object);
736
737         _leave("");
738 }
739
740 /*
741  * allow the slow work item processor to get a ref on an object
742  */
743 static int fscache_object_slow_work_get_ref(struct slow_work *work)
744 {
745         struct fscache_object *object =
746                 container_of(work, struct fscache_object, work);
747         int ret;
748
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);
752         return ret;
753 }
754
755 /*
756  * allow the slow work item processor to discard a ref on a work item
757  */
758 static void fscache_object_slow_work_put_ref(struct slow_work *work)
759 {
760         struct fscache_object *object =
761                 container_of(work, struct fscache_object, work);
762
763         fscache_stat(&fscache_n_cop_put_object);
764         object->cache->ops->put_object(object);
765         fscache_stat_d(&fscache_n_cop_put_object);
766 }
767
768 /*
769  * enqueue an object for metadata-type processing
770  */
771 void fscache_enqueue_object(struct fscache_object *object)
772 {
773         _enter("{OBJ%x}", object->debug_id);
774
775         slow_work_enqueue(&object->work);
776 }
777
778 /*
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
782  */
783 static void fscache_enqueue_dependents(struct fscache_object *object)
784 {
785         struct fscache_object *dep;
786
787         _enter("{OBJ%x}", object->debug_id);
788
789         if (list_empty(&object->dependents))
790                 return;
791
792         spin_lock(&object->lock);
793
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);
798
799
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);
805
806                 if (!list_empty(&object->dependents))
807                         cond_resched_lock(&object->lock);
808         }
809
810         spin_unlock(&object->lock);
811 }
812
813 /*
814  * remove an object from whatever queue it's waiting on
815  * - the caller must hold object->lock
816  */
817 void fscache_dequeue_object(struct fscache_object *object)
818 {
819         _enter("{OBJ%x}", object->debug_id);
820
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);
825         }
826
827         _leave("");
828 }
829
830 /**
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
835  *
836  * This function consults the netfs about the coherency state of an object
837  */
838 enum fscache_checkaux fscache_check_aux(struct fscache_object *object,
839                                         const void *data, uint16_t datalen)
840 {
841         enum fscache_checkaux result;
842
843         if (!object->cookie->def->check_aux) {
844                 fscache_stat(&fscache_n_checkaux_none);
845                 return FSCACHE_CHECKAUX_OKAY;
846         }
847
848         result = object->cookie->def->check_aux(object->cookie->netfs_data,
849                                                 data, datalen);
850         switch (result) {
851                 /* entry okay as is */
852         case FSCACHE_CHECKAUX_OKAY:
853                 fscache_stat(&fscache_n_checkaux_okay);
854                 break;
855
856                 /* entry requires update */
857         case FSCACHE_CHECKAUX_NEEDS_UPDATE:
858                 fscache_stat(&fscache_n_checkaux_update);
859                 break;
860
861                 /* entry requires deletion */
862         case FSCACHE_CHECKAUX_OBSOLETE:
863                 fscache_stat(&fscache_n_checkaux_obsolete);
864                 break;
865
866         default:
867                 BUG();
868         }
869
870         return result;
871 }
872 EXPORT_SYMBOL(fscache_check_aux);