FS-Cache: Fix object state machine to have separate work and wait states
[platform/adaptation/renesas_rcar/renesas_kernel.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/slab.h>
18 #include <linux/prefetch.h>
19 #include "internal.h"
20
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_detach_from_cookie(struct fscache_object *, int);
34
35 #define __STATE_NAME(n) fscache_osm_##n
36 #define STATE(n) (&__STATE_NAME(n))
37
38 /*
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.
44  */
45 #define WORK_STATE(n, sn, f) \
46         const struct fscache_state __STATE_NAME(n) = {                  \
47                 .name = #n,                                             \
48                 .short_name = sn,                                       \
49                 .work = f                                               \
50         }
51
52 /*
53  * Returns from work states.
54  */
55 #define transit_to(state) ({ prefetch(&STATE(state)->work); STATE(state); })
56
57 #define NO_TRANSIT ((struct fscache_state *)NULL)
58
59 /*
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
64  * currently pending.
65  */
66 #define WAIT_STATE(n, sn, ...) \
67         const struct fscache_state __STATE_NAME(n) = {                  \
68                 .name = #n,                                             \
69                 .short_name = sn,                                       \
70                 .work = NULL,                                           \
71                 .transitions = { __VA_ARGS__, { 0, NULL } }             \
72         }
73
74 #define TRANSIT_TO(state, emask) \
75         { .events = (emask), .transit_to = STATE(state) }
76
77 /*
78  * The object state machine.
79  */
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);
87
88 static WORK_STATE(INVALIDATE_OBJECT,    "INVL", fscache_invalidate_object);
89 static WORK_STATE(UPDATE_OBJECT,        "UPDT", fscache_update_object);
90
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(DETACH_FROM_COOKIE,   "DTCH", fscache_detach_from_cookie);
96 static WORK_STATE(OBJECT_DEAD,          "DEAD", (void*)2UL);
97
98 static WAIT_STATE(WAIT_FOR_INIT,        "?INI",
99                   TRANSIT_TO(INIT_OBJECT,       1 << FSCACHE_OBJECT_EV_NEW_CHILD));
100
101 static WAIT_STATE(WAIT_FOR_PARENT,      "?PRN",
102                   TRANSIT_TO(PARENT_READY,      1 << FSCACHE_OBJECT_EV_PARENT_READY));
103
104 static WAIT_STATE(WAIT_FOR_CMD,         "?CMD",
105                   TRANSIT_TO(INVALIDATE_OBJECT, 1 << FSCACHE_OBJECT_EV_INVALIDATE),
106                   TRANSIT_TO(UPDATE_OBJECT,     1 << FSCACHE_OBJECT_EV_UPDATE),
107                   TRANSIT_TO(JUMPSTART_DEPS,    1 << FSCACHE_OBJECT_EV_NEW_CHILD));
108
109 static WAIT_STATE(WAIT_FOR_CLEARANCE,   "?CLR",
110                   TRANSIT_TO(KILL_OBJECT,       1 << FSCACHE_OBJECT_EV_CLEARED));
111
112 /*
113  * Out-of-band event transition tables.  These are for handling unexpected
114  * events, such as an I/O error.  If an OOB event occurs, the state machine
115  * clears and disables the event and forces a transition to the nominated work
116  * state (acurrently executing work states will complete first).
117  *
118  * In such a situation, object->state remembers the state the machine should
119  * have been in/gone to and returning NO_TRANSIT returns to that.
120  */
121 static const struct fscache_transition fscache_osm_init_oob[] = {
122            TRANSIT_TO(ABORT_INIT,
123                       (1 << FSCACHE_OBJECT_EV_ERROR) |
124                       (1 << FSCACHE_OBJECT_EV_KILL)),
125            { 0, NULL }
126 };
127
128 static const struct fscache_transition fscache_osm_lookup_oob[] = {
129            TRANSIT_TO(LOOKUP_FAILURE,
130                       (1 << FSCACHE_OBJECT_EV_ERROR) |
131                       (1 << FSCACHE_OBJECT_EV_KILL)),
132            { 0, NULL }
133 };
134
135 static const struct fscache_transition fscache_osm_run_oob[] = {
136            TRANSIT_TO(KILL_OBJECT,
137                       (1 << FSCACHE_OBJECT_EV_ERROR) |
138                       (1 << FSCACHE_OBJECT_EV_KILL)),
139            { 0, NULL }
140 };
141
142 static int  fscache_get_object(struct fscache_object *);
143 static void fscache_put_object(struct fscache_object *);
144 static bool fscache_enqueue_dependents(struct fscache_object *, int);
145 static void fscache_dequeue_object(struct fscache_object *);
146
147 /*
148  * we need to notify the parent when an op completes that we had outstanding
149  * upon it
150  */
151 static inline void fscache_done_parent_op(struct fscache_object *object)
152 {
153         struct fscache_object *parent = object->parent;
154
155         _enter("OBJ%x {OBJ%x,%x}",
156                object->debug_id, parent->debug_id, parent->n_ops);
157
158         spin_lock_nested(&parent->lock, 1);
159         parent->n_ops--;
160         parent->n_obj_ops--;
161         if (parent->n_ops == 0)
162                 fscache_raise_event(parent, FSCACHE_OBJECT_EV_CLEARED);
163         spin_unlock(&parent->lock);
164 }
165
166 /*
167  * Object state machine dispatcher.
168  */
169 static void fscache_object_sm_dispatcher(struct fscache_object *object)
170 {
171         const struct fscache_transition *t;
172         const struct fscache_state *state, *new_state;
173         unsigned long events, event_mask;
174         int event = -1;
175
176         ASSERT(object != NULL);
177
178         _enter("{OBJ%x,%s,%lx}",
179                object->debug_id, object->state->name, object->events);
180
181         event_mask = object->event_mask;
182 restart:
183         object->event_mask = 0; /* Mask normal event handling */
184         state = object->state;
185 restart_masked:
186         events = object->events;
187
188         /* Handle any out-of-band events (typically an error) */
189         if (events & object->oob_event_mask) {
190                 _debug("{OBJ%x} oob %lx",
191                        object->debug_id, events & object->oob_event_mask);
192                 for (t = object->oob_table; t->events; t++) {
193                         if (events & t->events) {
194                                 state = t->transit_to;
195                                 ASSERT(state->work != NULL);
196                                 event = fls(events & t->events) - 1;
197                                 __clear_bit(event, &object->oob_event_mask);
198                                 clear_bit(event, &object->events);
199                                 goto execute_work_state;
200                         }
201                 }
202         }
203
204         /* Wait states are just transition tables */
205         if (!state->work) {
206                 if (events & event_mask) {
207                         for (t = state->transitions; t->events; t++) {
208                                 if (events & t->events) {
209                                         new_state = t->transit_to;
210                                         event = fls(events & t->events) - 1;
211                                         clear_bit(event, &object->events);
212                                         _debug("{OBJ%x} ev %d: %s -> %s",
213                                                object->debug_id, event,
214                                                state->name, new_state->name);
215                                         object->state = state = new_state;
216                                         goto execute_work_state;
217                                 }
218                         }
219
220                         /* The event mask didn't include all the tabled bits */
221                         BUG();
222                 }
223                 /* Randomly woke up */
224                 goto unmask_events;
225         }
226
227 execute_work_state:
228         _debug("{OBJ%x} exec %s", object->debug_id, state->name);
229
230         new_state = state->work(object, event);
231         event = -1;
232         if (new_state == NO_TRANSIT) {
233                 _debug("{OBJ%x} %s notrans", object->debug_id, state->name);
234                 fscache_enqueue_object(object);
235                 event_mask = object->oob_event_mask;
236                 goto unmask_events;
237         }
238
239         _debug("{OBJ%x} %s -> %s",
240                object->debug_id, state->name, new_state->name);
241         object->state = state = new_state;
242
243         if (state->work) {
244                 if (unlikely(state->work == ((void *)2UL))) {
245                         _leave(" [dead]");
246                         return;
247                 }
248                 goto restart_masked;
249         }
250
251         /* Transited to wait state */
252         event_mask = object->oob_event_mask;
253         for (t = state->transitions; t->events; t++)
254                 event_mask |= t->events;
255
256 unmask_events:
257         object->event_mask = event_mask;
258         smp_mb();
259         events = object->events;
260         if (events & event_mask)
261                 goto restart;
262         _leave(" [msk %lx]", event_mask);
263 }
264
265 /*
266  * execute an object
267  */
268 static void fscache_object_work_func(struct work_struct *work)
269 {
270         struct fscache_object *object =
271                 container_of(work, struct fscache_object, work);
272         unsigned long start;
273
274         _enter("{OBJ%x}", object->debug_id);
275
276         start = jiffies;
277         fscache_object_sm_dispatcher(object);
278         fscache_hist(fscache_objs_histogram, start);
279         fscache_put_object(object);
280 }
281
282 /**
283  * fscache_object_init - Initialise a cache object description
284  * @object: Object description
285  * @cookie: Cookie object will be attached to
286  * @cache: Cache in which backing object will be found
287  *
288  * Initialise a cache object description to its basic values.
289  *
290  * See Documentation/filesystems/caching/backend-api.txt for a complete
291  * description.
292  */
293 void fscache_object_init(struct fscache_object *object,
294                          struct fscache_cookie *cookie,
295                          struct fscache_cache *cache)
296 {
297         const struct fscache_transition *t;
298
299         atomic_inc(&cache->object_count);
300
301         object->state = STATE(WAIT_FOR_INIT);
302         object->oob_table = fscache_osm_init_oob;
303         object->flags = 1 << FSCACHE_OBJECT_IS_LIVE;
304         spin_lock_init(&object->lock);
305         INIT_LIST_HEAD(&object->cache_link);
306         INIT_HLIST_NODE(&object->cookie_link);
307         INIT_WORK(&object->work, fscache_object_work_func);
308         INIT_LIST_HEAD(&object->dependents);
309         INIT_LIST_HEAD(&object->dep_link);
310         INIT_LIST_HEAD(&object->pending_ops);
311         object->n_children = 0;
312         object->n_ops = object->n_in_progress = object->n_exclusive = 0;
313         object->events = 0;
314         object->store_limit = 0;
315         object->store_limit_l = 0;
316         object->cache = cache;
317         object->cookie = cookie;
318         object->parent = NULL;
319
320         object->oob_event_mask = 0;
321         for (t = object->oob_table; t->events; t++)
322                 object->oob_event_mask |= t->events;
323         object->event_mask = object->oob_event_mask;
324         for (t = object->state->transitions; t->events; t++)
325                 object->event_mask |= t->events;
326 }
327 EXPORT_SYMBOL(fscache_object_init);
328
329 /*
330  * Abort object initialisation before we start it.
331  */
332 static const struct fscache_state *fscache_abort_initialisation(struct fscache_object *object,
333                                                                 int event)
334 {
335         struct fscache_cookie *cookie;
336
337         _enter("{OBJ%x},%d", object->debug_id, event);
338
339         object->oob_event_mask = 0;
340         clear_bit(FSCACHE_OBJECT_IS_LIVE, &object->flags);
341
342         fscache_dequeue_object(object);
343
344         spin_lock(&object->lock);
345         cookie = object->cookie;
346         clear_bit_unlock(FSCACHE_COOKIE_CREATING, &cookie->flags);
347         spin_unlock(&object->lock);
348
349         wake_up_bit(&cookie->flags, FSCACHE_COOKIE_CREATING);
350
351         return transit_to(KILL_OBJECT);
352 }
353
354 /*
355  * initialise an object
356  * - check the specified object's parent to see if we can make use of it
357  *   immediately to do a creation
358  * - we may need to start the process of creating a parent and we need to wait
359  *   for the parent's lookup and creation to complete if it's not there yet
360  * - an object's cookie is pinned until we clear FSCACHE_COOKIE_CREATING on the
361  *   leaf-most cookies of the object and all its children
362  */
363 static const struct fscache_state *fscache_initialise_object(struct fscache_object *object,
364                                                              int event)
365 {
366         struct fscache_object *parent;
367         bool success;
368
369         _enter("{OBJ%x},%d", object->debug_id, event);
370
371         ASSERT(list_empty(&object->dep_link));
372
373         parent = object->parent;
374         if (!parent) {
375                 _leave(" [no parent]");
376                 return transit_to(DETACH_FROM_COOKIE);
377         }
378
379         _debug("parent %s", parent->state->name);
380
381         if (fscache_object_is_dying(parent)) {
382                 _leave(" [bad parent]");
383                 return transit_to(DETACH_FROM_COOKIE);
384         }
385
386         if (fscache_object_is_available(parent)) {
387                 _leave(" [ready]");
388                 return transit_to(PARENT_READY);
389         }
390
391         _debug("wait");
392
393         spin_lock(&parent->lock);
394         fscache_stat(&fscache_n_cop_grab_object);
395         success = false;
396         if (fscache_object_is_live(parent) &&
397             object->cache->ops->grab_object(object)) {
398                 list_add(&object->dep_link, &parent->dependents);
399                 success = true;
400         }
401         fscache_stat_d(&fscache_n_cop_grab_object);
402         spin_unlock(&parent->lock);
403         if (!success) {
404                 _leave(" [grab failed]");
405                 return transit_to(DETACH_FROM_COOKIE);
406         }
407
408         /* fscache_acquire_non_index_cookie() uses this
409          * to wake the chain up */
410         fscache_raise_event(parent, FSCACHE_OBJECT_EV_NEW_CHILD);
411         _leave(" [wait]");
412         return transit_to(WAIT_FOR_PARENT);
413 }
414
415 /*
416  * Once the parent object is ready, we should kick off our lookup op.
417  */
418 static const struct fscache_state *fscache_parent_ready(struct fscache_object *object,
419                                                         int event)
420 {
421         struct fscache_object *parent = object->parent;
422
423         _enter("{OBJ%x},%d", object->debug_id, event);
424
425         ASSERT(parent != NULL);
426
427         spin_lock(&parent->lock);
428         parent->n_ops++;
429         parent->n_obj_ops++;
430         object->lookup_jif = jiffies;
431         spin_unlock(&parent->lock);
432
433         _leave("");
434         return transit_to(LOOK_UP_OBJECT);
435 }
436
437 /*
438  * look an object up in the cache from which it was allocated
439  * - we hold an "access lock" on the parent object, so the parent object cannot
440  *   be withdrawn by either party till we've finished
441  * - an object's cookie is pinned until we clear FSCACHE_COOKIE_CREATING on the
442  *   leaf-most cookies of the object and all its children
443  */
444 static const struct fscache_state *fscache_look_up_object(struct fscache_object *object,
445                                                           int event)
446 {
447         struct fscache_cookie *cookie = object->cookie;
448         struct fscache_object *parent = object->parent;
449         int ret;
450
451         _enter("{OBJ%x},%d", object->debug_id, event);
452
453         object->oob_table = fscache_osm_lookup_oob;
454
455         ASSERT(parent != NULL);
456         ASSERTCMP(parent->n_ops, >, 0);
457         ASSERTCMP(parent->n_obj_ops, >, 0);
458
459         /* make sure the parent is still available */
460         ASSERT(fscache_object_is_available(parent));
461
462         if (fscache_object_is_dying(parent) ||
463             test_bit(FSCACHE_IOERROR, &object->cache->flags)) {
464                 _leave(" [unavailable]");
465                 return transit_to(LOOKUP_FAILURE);
466         }
467
468         _debug("LOOKUP \"%s/%s\" in \"%s\"",
469                parent->cookie->def->name, cookie->def->name,
470                object->cache->tag->name);
471
472         fscache_stat(&fscache_n_object_lookups);
473         fscache_stat(&fscache_n_cop_lookup_object);
474         ret = object->cache->ops->lookup_object(object);
475         fscache_stat_d(&fscache_n_cop_lookup_object);
476
477         if (test_bit(FSCACHE_OBJECT_EV_ERROR, &object->events))
478                 set_bit(FSCACHE_COOKIE_UNAVAILABLE, &cookie->flags);
479
480         if (ret == -ETIMEDOUT) {
481                 /* probably stuck behind another object, so move this one to
482                  * the back of the queue */
483                 fscache_stat(&fscache_n_object_lookups_timed_out);
484                 _leave(" [timeout]");
485                 return NO_TRANSIT;
486         }
487
488         if (ret < 0) {
489                 _leave(" [error]");
490                 return transit_to(LOOKUP_FAILURE);
491         }
492
493         _leave(" [ok]");
494         return transit_to(OBJECT_AVAILABLE);
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}", object->debug_id, object->state->name);
509
510         if (!test_and_set_bit(FSCACHE_OBJECT_IS_LOOKED_UP, &object->flags)) {
511                 fscache_stat(&fscache_n_object_lookups_negative);
512
513                 /* Allow write requests to begin stacking up and read requests to begin
514                  * returning ENODATA.
515                  */
516                 set_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
517
518                 _debug("wake up lookup %p", &cookie->flags);
519                 clear_bit_unlock(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags);
520                 wake_up_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP);
521         }
522         _leave("");
523 }
524 EXPORT_SYMBOL(fscache_object_lookup_negative);
525
526 /**
527  * fscache_obtained_object - Note successful object lookup or creation
528  * @object: Object pointing to cookie to mark
529  *
530  * Note successful lookup and/or creation, permitting those waiting to write
531  * data to a backing object to continue.
532  *
533  * Note that after calling this, an object's cookie may be relinquished by the
534  * netfs, and so must be accessed with object lock held.
535  */
536 void fscache_obtained_object(struct fscache_object *object)
537 {
538         struct fscache_cookie *cookie = object->cookie;
539
540         _enter("{OBJ%x,%s}", object->debug_id, object->state->name);
541
542         /* if we were still looking up, then we must have a positive lookup
543          * result, in which case there may be data available */
544         if (!test_and_set_bit(FSCACHE_OBJECT_IS_LOOKED_UP, &object->flags)) {
545                 fscache_stat(&fscache_n_object_lookups_positive);
546
547                 /* We do (presumably) have data */
548                 clear_bit_unlock(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
549
550                 /* Allow write requests to begin stacking up and read requests
551                  * to begin shovelling data.
552                  */
553                 clear_bit_unlock(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags);
554                 wake_up_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP);
555         } else {
556                 fscache_stat(&fscache_n_object_created);
557         }
558
559         set_bit(FSCACHE_OBJECT_IS_AVAILABLE, &object->flags);
560
561         /* Permit __fscache_relinquish_cookie() to proceed */
562         clear_bit_unlock(FSCACHE_COOKIE_CREATING, &cookie->flags);
563         wake_up_bit(&cookie->flags, FSCACHE_COOKIE_CREATING);
564
565         _leave("");
566 }
567 EXPORT_SYMBOL(fscache_obtained_object);
568
569 /*
570  * handle an object that has just become available
571  */
572 static const struct fscache_state *fscache_object_available(struct fscache_object *object,
573                                                             int event)
574 {
575         struct fscache_cookie *cookie = object->cookie;
576
577         _enter("{OBJ%x},%d", object->debug_id, event);
578
579         object->oob_table = fscache_osm_run_oob;
580
581         spin_lock(&object->lock);
582
583         ASSERTIF(cookie, !test_bit(FSCACHE_COOKIE_CREATING, &object->cookie->flags));
584
585         fscache_done_parent_op(object);
586         if (object->n_in_progress == 0) {
587                 if (object->n_ops > 0) {
588                         ASSERTCMP(object->n_ops, >=, object->n_obj_ops);
589                         fscache_start_operations(object);
590                 } else {
591                         ASSERT(list_empty(&object->pending_ops));
592                 }
593         }
594         spin_unlock(&object->lock);
595
596         fscache_stat(&fscache_n_cop_lookup_complete);
597         object->cache->ops->lookup_complete(object);
598         fscache_stat_d(&fscache_n_cop_lookup_complete);
599
600         fscache_hist(fscache_obj_instantiate_histogram, object->lookup_jif);
601         fscache_stat(&fscache_n_object_avail);
602
603         _leave("");
604         return transit_to(JUMPSTART_DEPS);
605 }
606
607 /*
608  * Wake up this object's dependent objects now that we've become available.
609  */
610 static const struct fscache_state *fscache_jumpstart_dependents(struct fscache_object *object,
611                                                                 int event)
612 {
613         _enter("{OBJ%x},%d", object->debug_id, event);
614
615         if (!fscache_enqueue_dependents(object, FSCACHE_OBJECT_EV_PARENT_READY))
616                 return NO_TRANSIT; /* Not finished; requeue */
617         return transit_to(WAIT_FOR_CMD);
618 }
619
620 /*
621  * Handle lookup or creation failute.
622  */
623 static const struct fscache_state *fscache_lookup_failure(struct fscache_object *object,
624                                                           int event)
625 {
626         struct fscache_cookie *cookie;
627         bool wake_looking_up = false;
628
629         _enter("{OBJ%x},%d", object->debug_id, event);
630
631         object->oob_event_mask = 0;
632
633         fscache_stat(&fscache_n_cop_lookup_complete);
634         object->cache->ops->lookup_complete(object);
635         fscache_stat_d(&fscache_n_cop_lookup_complete);
636
637         spin_lock(&object->lock);
638         cookie = object->cookie;
639         set_bit(FSCACHE_COOKIE_UNAVAILABLE, &cookie->flags);
640         if (cookie) {
641                 if (test_and_clear_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags))
642                         wake_looking_up = true;
643                 clear_bit_unlock(FSCACHE_COOKIE_CREATING, &cookie->flags);
644         }
645         spin_unlock(&object->lock);
646
647         if (wake_looking_up)
648                 wake_up_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP);
649         wake_up_bit(&cookie->flags, FSCACHE_COOKIE_CREATING);
650
651         fscache_done_parent_op(object);
652         return transit_to(KILL_OBJECT);
653 }
654
655 /*
656  * Wait for completion of all active operations on this object and the death of
657  * all child objects of this object.
658  */
659 static const struct fscache_state *fscache_kill_object(struct fscache_object *object,
660                                                        int event)
661 {
662         _enter("{OBJ%x,%d,%d},%d",
663                object->debug_id, object->n_ops, object->n_children, event);
664
665         object->oob_event_mask = 0;
666
667         spin_lock(&object->lock);
668         clear_bit(FSCACHE_OBJECT_IS_LIVE, &object->flags);
669         spin_unlock(&object->lock);
670
671         if (list_empty(&object->dependents) &&
672             object->n_ops == 0 &&
673             object->n_children == 0)
674                 return object->cookie ?
675                         transit_to(DETACH_FROM_COOKIE) : transit_to(DROP_OBJECT);
676
677         spin_lock(&object->lock);
678         fscache_start_operations(object);
679         spin_unlock(&object->lock);
680
681         if (!list_empty(&object->dependents))
682                 return transit_to(KILL_DEPENDENTS);
683
684         return transit_to(WAIT_FOR_CLEARANCE);
685 }
686
687 /*
688  * Kill dependent objects.
689  */
690 static const struct fscache_state *fscache_kill_dependents(struct fscache_object *object,
691                                                            int event)
692 {
693         _enter("{OBJ%x},%d", object->debug_id, event);
694
695         if (!fscache_enqueue_dependents(object, FSCACHE_OBJECT_EV_KILL))
696                 return NO_TRANSIT; /* Not finished */
697         return transit_to(WAIT_FOR_CLEARANCE);
698 }
699
700 /*
701  * withdraw an object from active service
702  */
703 static const struct fscache_state *fscache_detach_from_cookie(struct fscache_object *object,
704                                                               int event)
705 {
706         struct fscache_cookie *cookie;
707         bool detached = false, awaken = false;
708
709         _enter("{OBJ%x},%d", object->debug_id, event);
710
711         spin_lock(&object->lock);
712         cookie = object->cookie;
713         if (cookie) {
714                 /* need to get the cookie lock before the object lock, starting
715                  * from the object pointer */
716                 atomic_inc(&cookie->usage);
717                 spin_unlock(&object->lock);
718
719                 spin_lock(&cookie->lock);
720                 spin_lock(&object->lock);
721
722                 if (object->cookie == cookie) {
723                         hlist_del_init(&object->cookie_link);
724                         object->cookie = NULL;
725                         if (test_and_clear_bit(FSCACHE_COOKIE_INVALIDATING,
726                                                &cookie->flags))
727                                 awaken = true;
728                         detached = true;
729                 }
730                 spin_unlock(&cookie->lock);
731                 fscache_cookie_put(cookie);
732                 if (detached)
733                         fscache_cookie_put(cookie);
734         }
735
736         spin_unlock(&object->lock);
737
738         if (awaken)
739                 wake_up_bit(&cookie->flags, FSCACHE_COOKIE_INVALIDATING);
740
741         fscache_stat(&fscache_n_object_dead);
742         _leave("");
743         return transit_to(DROP_OBJECT);
744 }
745
746 /*
747  * Drop an object's attachments
748  */
749 static const struct fscache_state *fscache_drop_object(struct fscache_object *object,
750                                                        int event)
751 {
752         struct fscache_object *parent = object->parent;
753         struct fscache_cache *cache = object->cache;
754
755         _enter("{OBJ%x,%d},%d", object->debug_id, object->n_children, event);
756
757         ASSERTCMP(object->cookie, ==, NULL);
758         ASSERT(hlist_unhashed(&object->cookie_link));
759
760         /* Prevent a race with our last child, which has to signal EV_CLEARED
761          * before dropping our spinlock.
762          */
763         spin_lock(&object->lock);
764         spin_unlock(&object->lock);
765
766         /* Discard from the cache's collection of objects */
767         spin_lock(&cache->object_list_lock);
768         list_del_init(&object->cache_link);
769         spin_unlock(&cache->object_list_lock);
770
771         fscache_stat(&fscache_n_cop_drop_object);
772         cache->ops->drop_object(object);
773         fscache_stat_d(&fscache_n_cop_drop_object);
774
775         /* The parent object wants to know when all it dependents have gone */
776         if (parent) {
777                 _debug("release parent OBJ%x {%d}",
778                        parent->debug_id, parent->n_children);
779
780                 spin_lock(&parent->lock);
781                 parent->n_children--;
782                 if (parent->n_children == 0)
783                         fscache_raise_event(parent, FSCACHE_OBJECT_EV_CLEARED);
784                 spin_unlock(&parent->lock);
785                 object->parent = NULL;
786         }
787
788         /* this just shifts the object release to the work processor */
789         fscache_put_object(object);
790         fscache_stat(&fscache_n_object_dead);
791
792         _leave("");
793         return transit_to(OBJECT_DEAD);
794 }
795
796 /*
797  * get a ref on an object
798  */
799 static int fscache_get_object(struct fscache_object *object)
800 {
801         int ret;
802
803         fscache_stat(&fscache_n_cop_grab_object);
804         ret = object->cache->ops->grab_object(object) ? 0 : -EAGAIN;
805         fscache_stat_d(&fscache_n_cop_grab_object);
806         return ret;
807 }
808
809 /*
810  * Discard a ref on an object
811  */
812 static void fscache_put_object(struct fscache_object *object)
813 {
814         fscache_stat(&fscache_n_cop_put_object);
815         object->cache->ops->put_object(object);
816         fscache_stat_d(&fscache_n_cop_put_object);
817 }
818
819 /*
820  * enqueue an object for metadata-type processing
821  */
822 void fscache_enqueue_object(struct fscache_object *object)
823 {
824         _enter("{OBJ%x}", object->debug_id);
825
826         if (fscache_get_object(object) >= 0) {
827                 wait_queue_head_t *cong_wq =
828                         &get_cpu_var(fscache_object_cong_wait);
829
830                 if (queue_work(fscache_object_wq, &object->work)) {
831                         if (fscache_object_congested())
832                                 wake_up(cong_wq);
833                 } else
834                         fscache_put_object(object);
835
836                 put_cpu_var(fscache_object_cong_wait);
837         }
838 }
839
840 /**
841  * fscache_object_sleep_till_congested - Sleep until object wq is congested
842  * @timeoutp: Scheduler sleep timeout
843  *
844  * Allow an object handler to sleep until the object workqueue is congested.
845  *
846  * The caller must set up a wake up event before calling this and must have set
847  * the appropriate sleep mode (such as TASK_UNINTERRUPTIBLE) and tested its own
848  * condition before calling this function as no test is made here.
849  *
850  * %true is returned if the object wq is congested, %false otherwise.
851  */
852 bool fscache_object_sleep_till_congested(signed long *timeoutp)
853 {
854         wait_queue_head_t *cong_wq = &__get_cpu_var(fscache_object_cong_wait);
855         DEFINE_WAIT(wait);
856
857         if (fscache_object_congested())
858                 return true;
859
860         add_wait_queue_exclusive(cong_wq, &wait);
861         if (!fscache_object_congested())
862                 *timeoutp = schedule_timeout(*timeoutp);
863         finish_wait(cong_wq, &wait);
864
865         return fscache_object_congested();
866 }
867 EXPORT_SYMBOL_GPL(fscache_object_sleep_till_congested);
868
869 /*
870  * Enqueue the dependents of an object for metadata-type processing.
871  *
872  * If we don't manage to finish the list before the scheduler wants to run
873  * again then return false immediately.  We return true if the list was
874  * cleared.
875  */
876 static bool fscache_enqueue_dependents(struct fscache_object *object, int event)
877 {
878         struct fscache_object *dep;
879         bool ret = true;
880
881         _enter("{OBJ%x}", object->debug_id);
882
883         if (list_empty(&object->dependents))
884                 return true;
885
886         spin_lock(&object->lock);
887
888         while (!list_empty(&object->dependents)) {
889                 dep = list_entry(object->dependents.next,
890                                  struct fscache_object, dep_link);
891                 list_del_init(&dep->dep_link);
892
893                 fscache_raise_event(dep, event);
894                 fscache_put_object(dep);
895
896                 if (!list_empty(&object->dependents) && need_resched()) {
897                         ret = false;
898                         break;
899                 }
900         }
901
902         spin_unlock(&object->lock);
903         return ret;
904 }
905
906 /*
907  * remove an object from whatever queue it's waiting on
908  */
909 static void fscache_dequeue_object(struct fscache_object *object)
910 {
911         _enter("{OBJ%x}", object->debug_id);
912
913         if (!list_empty(&object->dep_link)) {
914                 spin_lock(&object->parent->lock);
915                 list_del_init(&object->dep_link);
916                 spin_unlock(&object->parent->lock);
917         }
918
919         _leave("");
920 }
921
922 /**
923  * fscache_check_aux - Ask the netfs whether an object on disk is still valid
924  * @object: The object to ask about
925  * @data: The auxiliary data for the object
926  * @datalen: The size of the auxiliary data
927  *
928  * This function consults the netfs about the coherency state of an object
929  */
930 enum fscache_checkaux fscache_check_aux(struct fscache_object *object,
931                                         const void *data, uint16_t datalen)
932 {
933         enum fscache_checkaux result;
934
935         if (!object->cookie->def->check_aux) {
936                 fscache_stat(&fscache_n_checkaux_none);
937                 return FSCACHE_CHECKAUX_OKAY;
938         }
939
940         result = object->cookie->def->check_aux(object->cookie->netfs_data,
941                                                 data, datalen);
942         switch (result) {
943                 /* entry okay as is */
944         case FSCACHE_CHECKAUX_OKAY:
945                 fscache_stat(&fscache_n_checkaux_okay);
946                 break;
947
948                 /* entry requires update */
949         case FSCACHE_CHECKAUX_NEEDS_UPDATE:
950                 fscache_stat(&fscache_n_checkaux_update);
951                 break;
952
953                 /* entry requires deletion */
954         case FSCACHE_CHECKAUX_OBSOLETE:
955                 fscache_stat(&fscache_n_checkaux_obsolete);
956                 break;
957
958         default:
959                 BUG();
960         }
961
962         return result;
963 }
964 EXPORT_SYMBOL(fscache_check_aux);
965
966 /*
967  * Asynchronously invalidate an object.
968  */
969 static const struct fscache_state *_fscache_invalidate_object(struct fscache_object *object,
970                                                               int event)
971 {
972         struct fscache_operation *op;
973         struct fscache_cookie *cookie = object->cookie;
974
975         _enter("{OBJ%x},%d", object->debug_id, event);
976
977
978         /* Reject any new read/write ops and abort any that are pending. */
979         fscache_invalidate_writes(cookie);
980         clear_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags);
981         fscache_cancel_all_ops(object);
982
983         /* Now we have to wait for in-progress reads and writes */
984         op = kzalloc(sizeof(*op), GFP_KERNEL);
985         if (!op) {
986                 clear_bit(FSCACHE_OBJECT_IS_LIVE, &object->flags);
987                 _leave(" [ENOMEM]");
988                 return transit_to(KILL_OBJECT);
989         }
990
991         fscache_operation_init(op, object->cache->ops->invalidate_object, NULL);
992         op->flags = FSCACHE_OP_ASYNC | (1 << FSCACHE_OP_EXCLUSIVE);
993
994         spin_lock(&cookie->lock);
995         if (fscache_submit_exclusive_op(object, op) < 0)
996                 goto submit_op_failed;
997         spin_unlock(&cookie->lock);
998         fscache_put_operation(op);
999
1000         /* Once we've completed the invalidation, we know there will be no data
1001          * stored in the cache and thus we can reinstate the data-check-skip
1002          * optimisation.
1003          */
1004         set_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
1005
1006         /* We can allow read and write requests to come in once again.  They'll
1007          * queue up behind our exclusive invalidation operation.
1008          */
1009         if (test_and_clear_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags))
1010                 wake_up_bit(&cookie->flags, FSCACHE_COOKIE_INVALIDATING);
1011         _leave(" [ok]");
1012         return transit_to(UPDATE_OBJECT);
1013
1014 submit_op_failed:
1015         clear_bit(FSCACHE_OBJECT_IS_LIVE, &object->flags);
1016         spin_unlock(&cookie->lock);
1017         kfree(op);
1018         _leave(" [EIO]");
1019         return transit_to(KILL_OBJECT);
1020 }
1021
1022 static const struct fscache_state *fscache_invalidate_object(struct fscache_object *object,
1023                                                              int event)
1024 {
1025         const struct fscache_state *s;
1026
1027         fscache_stat(&fscache_n_invalidates_run);
1028         fscache_stat(&fscache_n_cop_invalidate_object);
1029         s = _fscache_invalidate_object(object, event);
1030         fscache_stat_d(&fscache_n_cop_invalidate_object);
1031         return s;
1032 }
1033
1034 /*
1035  * Asynchronously update an object.
1036  */
1037 static const struct fscache_state *fscache_update_object(struct fscache_object *object,
1038                                                          int event)
1039 {
1040         _enter("{OBJ%x},%d", object->debug_id, event);
1041
1042         fscache_stat(&fscache_n_updates_run);
1043         fscache_stat(&fscache_n_cop_update_object);
1044         object->cache->ops->update_object(object);
1045         fscache_stat_d(&fscache_n_cop_update_object);
1046
1047         _leave("");
1048         return transit_to(WAIT_FOR_CMD);
1049 }