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