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