blob: 9e792e30f4db47b38c6db644487c440a2e12febb [file] [log] [blame]
David Howells36c95592009-04-03 16:42:38 +01001/* 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>
David Howellsef778e72012-12-20 21:52:36 +000017#include <linux/slab.h>
David Howellscaaef692013-05-10 19:50:26 +010018#include <linux/prefetch.h>
David Howells36c95592009-04-03 16:42:38 +010019#include "internal.h"
20
David Howellscaaef692013-05-10 19:50:26 +010021static 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);
David Howells36c95592009-04-03 16:42:38 +010033
David Howellscaaef692013-05-10 19:50:26 +010034#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);
David Howellscaaef692013-05-10 19:50:26 +010094static 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 }
124};
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 }
David Howells440f0af2009-11-19 18:11:01 +0000138};
139
Tejun Heo8b8edef2010-07-20 22:09:01 +0200140static int fscache_get_object(struct fscache_object *);
141static void fscache_put_object(struct fscache_object *);
David Howellscaaef692013-05-10 19:50:26 +0100142static bool fscache_enqueue_dependents(struct fscache_object *, int);
David Howells36c95592009-04-03 16:42:38 +0100143static void fscache_dequeue_object(struct fscache_object *);
144
David Howells36c95592009-04-03 16:42:38 +0100145/*
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);
David Howells36c95592009-04-03 16:42:38 +0100157 parent->n_obj_ops--;
David Howells13627292013-05-10 19:50:26 +0100158 parent->n_ops--;
David Howells36c95592009-04-03 16:42:38 +0100159 if (parent->n_ops == 0)
160 fscache_raise_event(parent, FSCACHE_OBJECT_EV_CLEARED);
161 spin_unlock(&parent->lock);
162}
163
164/*
David Howellscaaef692013-05-10 19:50:26 +0100165 * Object state machine dispatcher.
David Howellsef778e72012-12-20 21:52:36 +0000166 */
David Howellscaaef692013-05-10 19:50:26 +0100167static void fscache_object_sm_dispatcher(struct fscache_object *object)
David Howellsef778e72012-12-20 21:52:36 +0000168{
David Howellscaaef692013-05-10 19:50:26 +0100169 const struct fscache_transition *t;
170 const struct fscache_state *state, *new_state;
171 unsigned long events, event_mask;
172 int event = -1;
David Howells36c95592009-04-03 16:42:38 +0100173
174 ASSERT(object != NULL);
175
176 _enter("{OBJ%x,%s,%lx}",
David Howellscaaef692013-05-10 19:50:26 +0100177 object->debug_id, object->state->name, object->events);
David Howells36c95592009-04-03 16:42:38 +0100178
David Howellscaaef692013-05-10 19:50:26 +0100179 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;
David Howells36c95592009-04-03 16:42:38 +0100185
David Howellscaaef692013-05-10 19:50:26 +0100186 /* 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 }
David Howellsd461d262009-11-19 18:11:41 +0000199 }
David Howellscaaef692013-05-10 19:50:26 +0100200 }
David Howells36c95592009-04-03 16:42:38 +0100201
David Howellscaaef692013-05-10 19:50:26 +0100202 /* 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 }
David Howells36c95592009-04-03 16:42:38 +0100217
David Howellscaaef692013-05-10 19:50:26 +0100218 /* The event mask didn't include all the tabled bits */
219 BUG();
David Howells36c95592009-04-03 16:42:38 +0100220 }
David Howellscaaef692013-05-10 19:50:26 +0100221 /* Randomly woke up */
222 goto unmask_events;
David Howells36c95592009-04-03 16:42:38 +0100223 }
224
David Howellscaaef692013-05-10 19:50:26 +0100225execute_work_state:
226 _debug("{OBJ%x} exec %s", object->debug_id, state->name);
227
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;
David Howells36c95592009-04-03 16:42:38 +0100235 }
236
David Howellscaaef692013-05-10 19:50:26 +0100237 _debug("{OBJ%x} %s -> %s",
238 object->debug_id, state->name, new_state->name);
239 object->state = state = new_state;
240
241 if (state->work) {
242 if (unlikely(state->work == ((void *)2UL))) {
243 _leave(" [dead]");
244 return;
245 }
246 goto restart_masked;
David Howells36c95592009-04-03 16:42:38 +0100247 }
248
David Howellscaaef692013-05-10 19:50:26 +0100249 /* Transited to wait state */
250 event_mask = object->oob_event_mask;
251 for (t = state->transitions; t->events; t++)
252 event_mask |= t->events;
David Howells36c95592009-04-03 16:42:38 +0100253
David Howellscaaef692013-05-10 19:50:26 +0100254unmask_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);
David Howells36c95592009-04-03 16:42:38 +0100261}
262
263/*
264 * execute an object
265 */
David Howells610be242013-05-10 19:50:25 +0100266static void fscache_object_work_func(struct work_struct *work)
David Howells36c95592009-04-03 16:42:38 +0100267{
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
David Howells36c95592009-04-03 16:42:38 +0100274 start = jiffies;
David Howellscaaef692013-05-10 19:50:26 +0100275 fscache_object_sm_dispatcher(object);
David Howells36c95592009-04-03 16:42:38 +0100276 fscache_hist(fscache_objs_histogram, start);
Tejun Heo8b8edef2010-07-20 22:09:01 +0200277 fscache_put_object(object);
David Howells36c95592009-04-03 16:42:38 +0100278}
David Howells610be242013-05-10 19:50:25 +0100279
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{
David Howellscaaef692013-05-10 19:50:26 +0100295 const struct fscache_transition *t;
296
David Howells610be242013-05-10 19:50:25 +0100297 atomic_inc(&cache->object_count);
298
David Howellscaaef692013-05-10 19:50:26 +0100299 object->state = STATE(WAIT_FOR_INIT);
300 object->oob_table = fscache_osm_init_oob;
301 object->flags = 1 << FSCACHE_OBJECT_IS_LIVE;
David Howells610be242013-05-10 19:50:25 +0100302 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;
David Howellscaaef692013-05-10 19:50:26 +0100311 object->events = 0;
David Howells610be242013-05-10 19:50:25 +0100312 object->store_limit = 0;
313 object->store_limit_l = 0;
314 object->cache = cache;
315 object->cookie = cookie;
316 object->parent = NULL;
David Howells7026f192014-02-17 15:01:47 +0000317#ifdef CONFIG_FSCACHE_OBJECT_LIST
318 RB_CLEAR_NODE(&object->objlist_link);
319#endif
David Howellscaaef692013-05-10 19:50:26 +0100320
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;
David Howells610be242013-05-10 19:50:25 +0100327}
328EXPORT_SYMBOL(fscache_object_init);
David Howells440f0af2009-11-19 18:11:01 +0000329
330/*
David Howellsf09b4432015-02-24 10:05:28 +0000331 * Mark the object as no longer being live, making sure that we synchronise
332 * against op submission.
333 */
334static inline void fscache_mark_object_dead(struct fscache_object *object)
335{
336 spin_lock(&object->lock);
337 clear_bit(FSCACHE_OBJECT_IS_LIVE, &object->flags);
338 spin_unlock(&object->lock);
339}
340
341/*
David Howellscaaef692013-05-10 19:50:26 +0100342 * Abort object initialisation before we start it.
343 */
344static const struct fscache_state *fscache_abort_initialisation(struct fscache_object *object,
345 int event)
346{
David Howellscaaef692013-05-10 19:50:26 +0100347 _enter("{OBJ%x},%d", object->debug_id, event);
348
349 object->oob_event_mask = 0;
David Howellscaaef692013-05-10 19:50:26 +0100350 fscache_dequeue_object(object);
David Howellscaaef692013-05-10 19:50:26 +0100351 return transit_to(KILL_OBJECT);
352}
353
354/*
David Howells36c95592009-04-03 16:42:38 +0100355 * 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
David Howells36c95592009-04-03 16:42:38 +0100360 */
David Howellscaaef692013-05-10 19:50:26 +0100361static const struct fscache_state *fscache_initialise_object(struct fscache_object *object,
362 int event)
David Howells36c95592009-04-03 16:42:38 +0100363{
364 struct fscache_object *parent;
David Howellscaaef692013-05-10 19:50:26 +0100365 bool success;
David Howells36c95592009-04-03 16:42:38 +0100366
David Howellscaaef692013-05-10 19:50:26 +0100367 _enter("{OBJ%x},%d", object->debug_id, event);
David Howells36c95592009-04-03 16:42:38 +0100368
David Howellscaaef692013-05-10 19:50:26 +0100369 ASSERT(list_empty(&object->dep_link));
David Howells36c95592009-04-03 16:42:38 +0100370
371 parent = object->parent;
372 if (!parent) {
David Howellscaaef692013-05-10 19:50:26 +0100373 _leave(" [no parent]");
David Howells13627292013-05-10 19:50:26 +0100374 return transit_to(DROP_OBJECT);
David Howells36c95592009-04-03 16:42:38 +0100375 }
376
David Howells13627292013-05-10 19:50:26 +0100377 _debug("parent: %s of:%lx", parent->state->name, parent->flags);
David Howellscaaef692013-05-10 19:50:26 +0100378
379 if (fscache_object_is_dying(parent)) {
380 _leave(" [bad parent]");
David Howells13627292013-05-10 19:50:26 +0100381 return transit_to(DROP_OBJECT);
David Howellscaaef692013-05-10 19:50:26 +0100382 }
383
384 if (fscache_object_is_available(parent)) {
385 _leave(" [ready]");
386 return transit_to(PARENT_READY);
387 }
388
389 _debug("wait");
390
391 spin_lock(&parent->lock);
392 fscache_stat(&fscache_n_cop_grab_object);
393 success = false;
394 if (fscache_object_is_live(parent) &&
395 object->cache->ops->grab_object(object)) {
396 list_add(&object->dep_link, &parent->dependents);
397 success = true;
398 }
399 fscache_stat_d(&fscache_n_cop_grab_object);
400 spin_unlock(&parent->lock);
401 if (!success) {
402 _leave(" [grab failed]");
David Howells13627292013-05-10 19:50:26 +0100403 return transit_to(DROP_OBJECT);
David Howellscaaef692013-05-10 19:50:26 +0100404 }
405
406 /* fscache_acquire_non_index_cookie() uses this
407 * to wake the chain up */
408 fscache_raise_event(parent, FSCACHE_OBJECT_EV_NEW_CHILD);
409 _leave(" [wait]");
410 return transit_to(WAIT_FOR_PARENT);
411}
412
413/*
414 * Once the parent object is ready, we should kick off our lookup op.
415 */
416static const struct fscache_state *fscache_parent_ready(struct fscache_object *object,
417 int event)
418{
419 struct fscache_object *parent = object->parent;
420
421 _enter("{OBJ%x},%d", object->debug_id, event);
422
423 ASSERT(parent != NULL);
424
425 spin_lock(&parent->lock);
426 parent->n_ops++;
427 parent->n_obj_ops++;
428 object->lookup_jif = jiffies;
429 spin_unlock(&parent->lock);
430
David Howells36c95592009-04-03 16:42:38 +0100431 _leave("");
David Howellscaaef692013-05-10 19:50:26 +0100432 return transit_to(LOOK_UP_OBJECT);
David Howells36c95592009-04-03 16:42:38 +0100433}
434
435/*
436 * look an object up in the cache from which it was allocated
437 * - we hold an "access lock" on the parent object, so the parent object cannot
438 * be withdrawn by either party till we've finished
David Howells36c95592009-04-03 16:42:38 +0100439 */
David Howellscaaef692013-05-10 19:50:26 +0100440static const struct fscache_state *fscache_look_up_object(struct fscache_object *object,
441 int event)
David Howells36c95592009-04-03 16:42:38 +0100442{
443 struct fscache_cookie *cookie = object->cookie;
David Howellscaaef692013-05-10 19:50:26 +0100444 struct fscache_object *parent = object->parent;
David Howellsfee096d2009-11-19 18:12:05 +0000445 int ret;
David Howells36c95592009-04-03 16:42:38 +0100446
David Howellscaaef692013-05-10 19:50:26 +0100447 _enter("{OBJ%x},%d", object->debug_id, event);
David Howells36c95592009-04-03 16:42:38 +0100448
David Howellscaaef692013-05-10 19:50:26 +0100449 object->oob_table = fscache_osm_lookup_oob;
450
David Howells36c95592009-04-03 16:42:38 +0100451 ASSERT(parent != NULL);
452 ASSERTCMP(parent->n_ops, >, 0);
453 ASSERTCMP(parent->n_obj_ops, >, 0);
454
455 /* make sure the parent is still available */
David Howells493f7bc2013-05-10 19:50:26 +0100456 ASSERT(fscache_object_is_available(parent));
David Howells36c95592009-04-03 16:42:38 +0100457
David Howells493f7bc2013-05-10 19:50:26 +0100458 if (fscache_object_is_dying(parent) ||
David Howells13627292013-05-10 19:50:26 +0100459 test_bit(FSCACHE_IOERROR, &object->cache->flags) ||
460 !fscache_use_cookie(object)) {
David Howellscaaef692013-05-10 19:50:26 +0100461 _leave(" [unavailable]");
462 return transit_to(LOOKUP_FAILURE);
David Howells36c95592009-04-03 16:42:38 +0100463 }
464
David Howells13627292013-05-10 19:50:26 +0100465 _debug("LOOKUP \"%s\" in \"%s\"",
466 cookie->def->name, object->cache->tag->name);
David Howells36c95592009-04-03 16:42:38 +0100467
468 fscache_stat(&fscache_n_object_lookups);
David Howells52bd75f2009-11-19 18:11:08 +0000469 fscache_stat(&fscache_n_cop_lookup_object);
David Howellsfee096d2009-11-19 18:12:05 +0000470 ret = object->cache->ops->lookup_object(object);
David Howells52bd75f2009-11-19 18:11:08 +0000471 fscache_stat_d(&fscache_n_cop_lookup_object);
David Howells36c95592009-04-03 16:42:38 +0100472
David Howells13627292013-05-10 19:50:26 +0100473 fscache_unuse_cookie(object);
David Howells36c95592009-04-03 16:42:38 +0100474
David Howellsfee096d2009-11-19 18:12:05 +0000475 if (ret == -ETIMEDOUT) {
476 /* probably stuck behind another object, so move this one to
477 * the back of the queue */
478 fscache_stat(&fscache_n_object_lookups_timed_out);
David Howellscaaef692013-05-10 19:50:26 +0100479 _leave(" [timeout]");
480 return NO_TRANSIT;
David Howellsfee096d2009-11-19 18:12:05 +0000481 }
482
David Howellscaaef692013-05-10 19:50:26 +0100483 if (ret < 0) {
484 _leave(" [error]");
485 return transit_to(LOOKUP_FAILURE);
486 }
487
488 _leave(" [ok]");
489 return transit_to(OBJECT_AVAILABLE);
David Howells36c95592009-04-03 16:42:38 +0100490}
491
492/**
493 * fscache_object_lookup_negative - Note negative cookie lookup
494 * @object: Object pointing to cookie to mark
495 *
496 * Note negative lookup, permitting those waiting to read data from an already
497 * existing backing object to continue as there's no data for them to read.
498 */
499void fscache_object_lookup_negative(struct fscache_object *object)
500{
501 struct fscache_cookie *cookie = object->cookie;
502
David Howellscaaef692013-05-10 19:50:26 +0100503 _enter("{OBJ%x,%s}", object->debug_id, object->state->name);
David Howells36c95592009-04-03 16:42:38 +0100504
David Howellscaaef692013-05-10 19:50:26 +0100505 if (!test_and_set_bit(FSCACHE_OBJECT_IS_LOOKED_UP, &object->flags)) {
David Howells36c95592009-04-03 16:42:38 +0100506 fscache_stat(&fscache_n_object_lookups_negative);
507
David Howellscaaef692013-05-10 19:50:26 +0100508 /* Allow write requests to begin stacking up and read requests to begin
509 * returning ENODATA.
510 */
David Howells36c95592009-04-03 16:42:38 +0100511 set_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
David Howells94d30ae2013-09-21 00:09:31 +0100512 clear_bit(FSCACHE_COOKIE_UNAVAILABLE, &cookie->flags);
David Howells36c95592009-04-03 16:42:38 +0100513
514 _debug("wake up lookup %p", &cookie->flags);
David Howellscaaef692013-05-10 19:50:26 +0100515 clear_bit_unlock(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags);
David Howells36c95592009-04-03 16:42:38 +0100516 wake_up_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP);
David Howells36c95592009-04-03 16:42:38 +0100517 }
David Howells36c95592009-04-03 16:42:38 +0100518 _leave("");
519}
520EXPORT_SYMBOL(fscache_object_lookup_negative);
521
522/**
523 * fscache_obtained_object - Note successful object lookup or creation
524 * @object: Object pointing to cookie to mark
525 *
526 * Note successful lookup and/or creation, permitting those waiting to write
527 * data to a backing object to continue.
528 *
529 * Note that after calling this, an object's cookie may be relinquished by the
530 * netfs, and so must be accessed with object lock held.
531 */
532void fscache_obtained_object(struct fscache_object *object)
533{
534 struct fscache_cookie *cookie = object->cookie;
535
David Howellscaaef692013-05-10 19:50:26 +0100536 _enter("{OBJ%x,%s}", object->debug_id, object->state->name);
David Howells36c95592009-04-03 16:42:38 +0100537
538 /* if we were still looking up, then we must have a positive lookup
539 * result, in which case there may be data available */
David Howellscaaef692013-05-10 19:50:26 +0100540 if (!test_and_set_bit(FSCACHE_OBJECT_IS_LOOKED_UP, &object->flags)) {
David Howells36c95592009-04-03 16:42:38 +0100541 fscache_stat(&fscache_n_object_lookups_positive);
542
David Howellscaaef692013-05-10 19:50:26 +0100543 /* We do (presumably) have data */
544 clear_bit_unlock(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
David Howells94d30ae2013-09-21 00:09:31 +0100545 clear_bit(FSCACHE_COOKIE_UNAVAILABLE, &cookie->flags);
David Howells36c95592009-04-03 16:42:38 +0100546
David Howellscaaef692013-05-10 19:50:26 +0100547 /* Allow write requests to begin stacking up and read requests
548 * to begin shovelling data.
549 */
550 clear_bit_unlock(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags);
David Howells36c95592009-04-03 16:42:38 +0100551 wake_up_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP);
David Howells36c95592009-04-03 16:42:38 +0100552 } else {
David Howells36c95592009-04-03 16:42:38 +0100553 fscache_stat(&fscache_n_object_created);
David Howells36c95592009-04-03 16:42:38 +0100554 }
555
David Howellscaaef692013-05-10 19:50:26 +0100556 set_bit(FSCACHE_OBJECT_IS_AVAILABLE, &object->flags);
David Howells36c95592009-04-03 16:42:38 +0100557 _leave("");
558}
559EXPORT_SYMBOL(fscache_obtained_object);
560
561/*
562 * handle an object that has just become available
563 */
David Howellscaaef692013-05-10 19:50:26 +0100564static const struct fscache_state *fscache_object_available(struct fscache_object *object,
565 int event)
David Howells36c95592009-04-03 16:42:38 +0100566{
David Howellscaaef692013-05-10 19:50:26 +0100567 _enter("{OBJ%x},%d", object->debug_id, event);
568
569 object->oob_table = fscache_osm_run_oob;
David Howells36c95592009-04-03 16:42:38 +0100570
571 spin_lock(&object->lock);
572
David Howells36c95592009-04-03 16:42:38 +0100573 fscache_done_parent_op(object);
574 if (object->n_in_progress == 0) {
575 if (object->n_ops > 0) {
576 ASSERTCMP(object->n_ops, >=, object->n_obj_ops);
David Howells36c95592009-04-03 16:42:38 +0100577 fscache_start_operations(object);
578 } else {
579 ASSERT(list_empty(&object->pending_ops));
580 }
581 }
582 spin_unlock(&object->lock);
583
David Howells52bd75f2009-11-19 18:11:08 +0000584 fscache_stat(&fscache_n_cop_lookup_complete);
David Howells36c95592009-04-03 16:42:38 +0100585 object->cache->ops->lookup_complete(object);
David Howells52bd75f2009-11-19 18:11:08 +0000586 fscache_stat_d(&fscache_n_cop_lookup_complete);
David Howells36c95592009-04-03 16:42:38 +0100587
588 fscache_hist(fscache_obj_instantiate_histogram, object->lookup_jif);
589 fscache_stat(&fscache_n_object_avail);
590
591 _leave("");
David Howellscaaef692013-05-10 19:50:26 +0100592 return transit_to(JUMPSTART_DEPS);
David Howells36c95592009-04-03 16:42:38 +0100593}
594
595/*
David Howellscaaef692013-05-10 19:50:26 +0100596 * Wake up this object's dependent objects now that we've become available.
David Howells36c95592009-04-03 16:42:38 +0100597 */
David Howellscaaef692013-05-10 19:50:26 +0100598static const struct fscache_state *fscache_jumpstart_dependents(struct fscache_object *object,
599 int event)
600{
601 _enter("{OBJ%x},%d", object->debug_id, event);
602
603 if (!fscache_enqueue_dependents(object, FSCACHE_OBJECT_EV_PARENT_READY))
604 return NO_TRANSIT; /* Not finished; requeue */
605 return transit_to(WAIT_FOR_CMD);
606}
607
608/*
609 * Handle lookup or creation failute.
610 */
611static const struct fscache_state *fscache_lookup_failure(struct fscache_object *object,
612 int event)
613{
614 struct fscache_cookie *cookie;
David Howellscaaef692013-05-10 19:50:26 +0100615
616 _enter("{OBJ%x},%d", object->debug_id, event);
617
618 object->oob_event_mask = 0;
619
620 fscache_stat(&fscache_n_cop_lookup_complete);
621 object->cache->ops->lookup_complete(object);
622 fscache_stat_d(&fscache_n_cop_lookup_complete);
623
David Howells6515d1d2015-02-25 11:53:57 +0000624 set_bit(FSCACHE_OBJECT_KILLED_BY_CACHE, &object->flags);
625
David Howellscaaef692013-05-10 19:50:26 +0100626 cookie = object->cookie;
627 set_bit(FSCACHE_COOKIE_UNAVAILABLE, &cookie->flags);
David Howells13627292013-05-10 19:50:26 +0100628 if (test_and_clear_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags))
David Howellscaaef692013-05-10 19:50:26 +0100629 wake_up_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP);
David Howellscaaef692013-05-10 19:50:26 +0100630
631 fscache_done_parent_op(object);
632 return transit_to(KILL_OBJECT);
633}
634
635/*
636 * Wait for completion of all active operations on this object and the death of
637 * all child objects of this object.
638 */
639static const struct fscache_state *fscache_kill_object(struct fscache_object *object,
640 int event)
641{
642 _enter("{OBJ%x,%d,%d},%d",
643 object->debug_id, object->n_ops, object->n_children, event);
644
David Howellsf09b4432015-02-24 10:05:28 +0000645 fscache_mark_object_dead(object);
David Howells13627292013-05-10 19:50:26 +0100646 object->oob_event_mask = 0;
David Howellscaaef692013-05-10 19:50:26 +0100647
648 if (list_empty(&object->dependents) &&
649 object->n_ops == 0 &&
650 object->n_children == 0)
David Howells13627292013-05-10 19:50:26 +0100651 return transit_to(DROP_OBJECT);
David Howellscaaef692013-05-10 19:50:26 +0100652
David Howells13627292013-05-10 19:50:26 +0100653 if (object->n_in_progress == 0) {
654 spin_lock(&object->lock);
655 if (object->n_ops > 0 && object->n_in_progress == 0)
656 fscache_start_operations(object);
657 spin_unlock(&object->lock);
658 }
David Howellscaaef692013-05-10 19:50:26 +0100659
660 if (!list_empty(&object->dependents))
661 return transit_to(KILL_DEPENDENTS);
662
663 return transit_to(WAIT_FOR_CLEARANCE);
664}
665
666/*
667 * Kill dependent objects.
668 */
669static const struct fscache_state *fscache_kill_dependents(struct fscache_object *object,
670 int event)
671{
672 _enter("{OBJ%x},%d", object->debug_id, event);
673
674 if (!fscache_enqueue_dependents(object, FSCACHE_OBJECT_EV_KILL))
675 return NO_TRANSIT; /* Not finished */
676 return transit_to(WAIT_FOR_CLEARANCE);
677}
678
679/*
David Howellscaaef692013-05-10 19:50:26 +0100680 * Drop an object's attachments
681 */
682static const struct fscache_state *fscache_drop_object(struct fscache_object *object,
683 int event)
David Howells36c95592009-04-03 16:42:38 +0100684{
685 struct fscache_object *parent = object->parent;
David Howells13627292013-05-10 19:50:26 +0100686 struct fscache_cookie *cookie = object->cookie;
David Howells36c95592009-04-03 16:42:38 +0100687 struct fscache_cache *cache = object->cache;
David Howells13627292013-05-10 19:50:26 +0100688 bool awaken = false;
David Howells36c95592009-04-03 16:42:38 +0100689
David Howellscaaef692013-05-10 19:50:26 +0100690 _enter("{OBJ%x,%d},%d", object->debug_id, object->n_children, event);
David Howells36c95592009-04-03 16:42:38 +0100691
David Howells13627292013-05-10 19:50:26 +0100692 ASSERT(cookie != NULL);
693 ASSERT(!hlist_unhashed(&object->cookie_link));
694
695 /* Make sure the cookie no longer points here and that the netfs isn't
696 * waiting for us.
697 */
698 spin_lock(&cookie->lock);
699 hlist_del_init(&object->cookie_link);
David Howells94d30ae2013-09-21 00:09:31 +0100700 if (hlist_empty(&cookie->backing_objects) &&
701 test_and_clear_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags))
David Howells13627292013-05-10 19:50:26 +0100702 awaken = true;
703 spin_unlock(&cookie->lock);
704
705 if (awaken)
706 wake_up_bit(&cookie->flags, FSCACHE_COOKIE_INVALIDATING);
David Howells6897e3d2009-11-19 18:11:22 +0000707
David Howellscaaef692013-05-10 19:50:26 +0100708 /* Prevent a race with our last child, which has to signal EV_CLEARED
709 * before dropping our spinlock.
710 */
711 spin_lock(&object->lock);
712 spin_unlock(&object->lock);
713
714 /* Discard from the cache's collection of objects */
David Howells36c95592009-04-03 16:42:38 +0100715 spin_lock(&cache->object_list_lock);
716 list_del_init(&object->cache_link);
717 spin_unlock(&cache->object_list_lock);
718
David Howells52bd75f2009-11-19 18:11:08 +0000719 fscache_stat(&fscache_n_cop_drop_object);
David Howells36c95592009-04-03 16:42:38 +0100720 cache->ops->drop_object(object);
David Howells52bd75f2009-11-19 18:11:08 +0000721 fscache_stat_d(&fscache_n_cop_drop_object);
David Howells36c95592009-04-03 16:42:38 +0100722
David Howellscaaef692013-05-10 19:50:26 +0100723 /* The parent object wants to know when all it dependents have gone */
David Howells36c95592009-04-03 16:42:38 +0100724 if (parent) {
725 _debug("release parent OBJ%x {%d}",
726 parent->debug_id, parent->n_children);
727
728 spin_lock(&parent->lock);
729 parent->n_children--;
730 if (parent->n_children == 0)
731 fscache_raise_event(parent, FSCACHE_OBJECT_EV_CLEARED);
732 spin_unlock(&parent->lock);
733 object->parent = NULL;
734 }
735
Tejun Heo8b8edef2010-07-20 22:09:01 +0200736 /* this just shifts the object release to the work processor */
737 fscache_put_object(object);
David Howellscaaef692013-05-10 19:50:26 +0100738 fscache_stat(&fscache_n_object_dead);
David Howells36c95592009-04-03 16:42:38 +0100739
740 _leave("");
David Howellscaaef692013-05-10 19:50:26 +0100741 return transit_to(OBJECT_DEAD);
David Howells36c95592009-04-03 16:42:38 +0100742}
743
744/*
Tejun Heo8b8edef2010-07-20 22:09:01 +0200745 * get a ref on an object
David Howells36c95592009-04-03 16:42:38 +0100746 */
Tejun Heo8b8edef2010-07-20 22:09:01 +0200747static int fscache_get_object(struct fscache_object *object)
David Howells36c95592009-04-03 16:42:38 +0100748{
David Howells52bd75f2009-11-19 18:11:08 +0000749 int ret;
David Howells36c95592009-04-03 16:42:38 +0100750
David Howells52bd75f2009-11-19 18:11:08 +0000751 fscache_stat(&fscache_n_cop_grab_object);
752 ret = object->cache->ops->grab_object(object) ? 0 : -EAGAIN;
753 fscache_stat_d(&fscache_n_cop_grab_object);
754 return ret;
David Howells36c95592009-04-03 16:42:38 +0100755}
756
757/*
David Howellscaaef692013-05-10 19:50:26 +0100758 * Discard a ref on an object
David Howells36c95592009-04-03 16:42:38 +0100759 */
Tejun Heo8b8edef2010-07-20 22:09:01 +0200760static void fscache_put_object(struct fscache_object *object)
David Howells36c95592009-04-03 16:42:38 +0100761{
David Howells52bd75f2009-11-19 18:11:08 +0000762 fscache_stat(&fscache_n_cop_put_object);
763 object->cache->ops->put_object(object);
764 fscache_stat_d(&fscache_n_cop_put_object);
David Howells36c95592009-04-03 16:42:38 +0100765}
766
David Howells13627292013-05-10 19:50:26 +0100767/**
768 * fscache_object_destroy - Note that a cache object is about to be destroyed
769 * @object: The object to be destroyed
770 *
771 * Note the imminent destruction and deallocation of a cache object record.
772 */
773void fscache_object_destroy(struct fscache_object *object)
774{
775 fscache_objlist_remove(object);
776
777 /* We can get rid of the cookie now */
778 fscache_cookie_put(object->cookie);
779 object->cookie = NULL;
780}
781EXPORT_SYMBOL(fscache_object_destroy);
782
David Howells36c95592009-04-03 16:42:38 +0100783/*
784 * enqueue an object for metadata-type processing
785 */
786void fscache_enqueue_object(struct fscache_object *object)
787{
788 _enter("{OBJ%x}", object->debug_id);
789
Tejun Heo8b8edef2010-07-20 22:09:01 +0200790 if (fscache_get_object(object) >= 0) {
791 wait_queue_head_t *cong_wq =
792 &get_cpu_var(fscache_object_cong_wait);
793
794 if (queue_work(fscache_object_wq, &object->work)) {
795 if (fscache_object_congested())
796 wake_up(cong_wq);
797 } else
798 fscache_put_object(object);
799
800 put_cpu_var(fscache_object_cong_wait);
801 }
David Howells36c95592009-04-03 16:42:38 +0100802}
803
Tejun Heo8b8edef2010-07-20 22:09:01 +0200804/**
805 * fscache_object_sleep_till_congested - Sleep until object wq is congested
David Howellscaaef692013-05-10 19:50:26 +0100806 * @timeoutp: Scheduler sleep timeout
Tejun Heo8b8edef2010-07-20 22:09:01 +0200807 *
808 * Allow an object handler to sleep until the object workqueue is congested.
809 *
810 * The caller must set up a wake up event before calling this and must have set
811 * the appropriate sleep mode (such as TASK_UNINTERRUPTIBLE) and tested its own
812 * condition before calling this function as no test is made here.
813 *
814 * %true is returned if the object wq is congested, %false otherwise.
815 */
816bool fscache_object_sleep_till_congested(signed long *timeoutp)
817{
Christoph Lameter170d8002013-10-15 12:22:29 -0600818 wait_queue_head_t *cong_wq = this_cpu_ptr(&fscache_object_cong_wait);
Tejun Heo8b8edef2010-07-20 22:09:01 +0200819 DEFINE_WAIT(wait);
820
821 if (fscache_object_congested())
822 return true;
823
824 add_wait_queue_exclusive(cong_wq, &wait);
825 if (!fscache_object_congested())
826 *timeoutp = schedule_timeout(*timeoutp);
827 finish_wait(cong_wq, &wait);
828
829 return fscache_object_congested();
830}
831EXPORT_SYMBOL_GPL(fscache_object_sleep_till_congested);
832
David Howells36c95592009-04-03 16:42:38 +0100833/*
David Howellscaaef692013-05-10 19:50:26 +0100834 * Enqueue the dependents of an object for metadata-type processing.
835 *
836 * If we don't manage to finish the list before the scheduler wants to run
837 * again then return false immediately. We return true if the list was
838 * cleared.
David Howells36c95592009-04-03 16:42:38 +0100839 */
David Howellscaaef692013-05-10 19:50:26 +0100840static bool fscache_enqueue_dependents(struct fscache_object *object, int event)
David Howells36c95592009-04-03 16:42:38 +0100841{
842 struct fscache_object *dep;
David Howellscaaef692013-05-10 19:50:26 +0100843 bool ret = true;
David Howells36c95592009-04-03 16:42:38 +0100844
845 _enter("{OBJ%x}", object->debug_id);
846
847 if (list_empty(&object->dependents))
David Howellscaaef692013-05-10 19:50:26 +0100848 return true;
David Howells36c95592009-04-03 16:42:38 +0100849
850 spin_lock(&object->lock);
851
852 while (!list_empty(&object->dependents)) {
853 dep = list_entry(object->dependents.next,
854 struct fscache_object, dep_link);
855 list_del_init(&dep->dep_link);
856
David Howellscaaef692013-05-10 19:50:26 +0100857 fscache_raise_event(dep, event);
Tejun Heo8b8edef2010-07-20 22:09:01 +0200858 fscache_put_object(dep);
David Howells36c95592009-04-03 16:42:38 +0100859
David Howellscaaef692013-05-10 19:50:26 +0100860 if (!list_empty(&object->dependents) && need_resched()) {
861 ret = false;
862 break;
863 }
David Howells36c95592009-04-03 16:42:38 +0100864 }
865
866 spin_unlock(&object->lock);
David Howellscaaef692013-05-10 19:50:26 +0100867 return ret;
David Howells36c95592009-04-03 16:42:38 +0100868}
869
870/*
871 * remove an object from whatever queue it's waiting on
David Howells36c95592009-04-03 16:42:38 +0100872 */
David Howellscaaef692013-05-10 19:50:26 +0100873static void fscache_dequeue_object(struct fscache_object *object)
David Howells36c95592009-04-03 16:42:38 +0100874{
875 _enter("{OBJ%x}", object->debug_id);
876
877 if (!list_empty(&object->dep_link)) {
878 spin_lock(&object->parent->lock);
879 list_del_init(&object->dep_link);
880 spin_unlock(&object->parent->lock);
881 }
882
883 _leave("");
884}
885
886/**
887 * fscache_check_aux - Ask the netfs whether an object on disk is still valid
888 * @object: The object to ask about
889 * @data: The auxiliary data for the object
890 * @datalen: The size of the auxiliary data
891 *
David Howells13627292013-05-10 19:50:26 +0100892 * This function consults the netfs about the coherency state of an object.
893 * The caller must be holding a ref on cookie->n_active (held by
894 * fscache_look_up_object() on behalf of the cache backend during object lookup
895 * and creation).
David Howells36c95592009-04-03 16:42:38 +0100896 */
897enum fscache_checkaux fscache_check_aux(struct fscache_object *object,
898 const void *data, uint16_t datalen)
899{
900 enum fscache_checkaux result;
901
902 if (!object->cookie->def->check_aux) {
903 fscache_stat(&fscache_n_checkaux_none);
904 return FSCACHE_CHECKAUX_OKAY;
905 }
906
907 result = object->cookie->def->check_aux(object->cookie->netfs_data,
908 data, datalen);
909 switch (result) {
910 /* entry okay as is */
911 case FSCACHE_CHECKAUX_OKAY:
912 fscache_stat(&fscache_n_checkaux_okay);
913 break;
914
915 /* entry requires update */
916 case FSCACHE_CHECKAUX_NEEDS_UPDATE:
917 fscache_stat(&fscache_n_checkaux_update);
918 break;
919
920 /* entry requires deletion */
921 case FSCACHE_CHECKAUX_OBSOLETE:
922 fscache_stat(&fscache_n_checkaux_obsolete);
923 break;
924
925 default:
926 BUG();
927 }
928
929 return result;
930}
931EXPORT_SYMBOL(fscache_check_aux);
David Howellsef778e72012-12-20 21:52:36 +0000932
933/*
934 * Asynchronously invalidate an object.
935 */
David Howellscaaef692013-05-10 19:50:26 +0100936static const struct fscache_state *_fscache_invalidate_object(struct fscache_object *object,
937 int event)
David Howellsef778e72012-12-20 21:52:36 +0000938{
939 struct fscache_operation *op;
940 struct fscache_cookie *cookie = object->cookie;
941
David Howellscaaef692013-05-10 19:50:26 +0100942 _enter("{OBJ%x},%d", object->debug_id, event);
943
David Howells13627292013-05-10 19:50:26 +0100944 /* We're going to need the cookie. If the cookie is not available then
945 * retire the object instead.
946 */
947 if (!fscache_use_cookie(object)) {
948 ASSERT(object->cookie->stores.rnode == NULL);
David Howells94d30ae2013-09-21 00:09:31 +0100949 set_bit(FSCACHE_OBJECT_RETIRED, &object->flags);
David Howells13627292013-05-10 19:50:26 +0100950 _leave(" [no cookie]");
951 return transit_to(KILL_OBJECT);
952 }
David Howellsef778e72012-12-20 21:52:36 +0000953
954 /* Reject any new read/write ops and abort any that are pending. */
955 fscache_invalidate_writes(cookie);
956 clear_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags);
957 fscache_cancel_all_ops(object);
958
959 /* Now we have to wait for in-progress reads and writes */
960 op = kzalloc(sizeof(*op), GFP_KERNEL);
David Howells13627292013-05-10 19:50:26 +0100961 if (!op)
962 goto nomem;
David Howellsef778e72012-12-20 21:52:36 +0000963
David Howellsd3b97ca2015-02-24 10:05:29 +0000964 fscache_operation_init(op, object->cache->ops->invalidate_object,
965 NULL, NULL);
David Howells13627292013-05-10 19:50:26 +0100966 op->flags = FSCACHE_OP_ASYNC |
967 (1 << FSCACHE_OP_EXCLUSIVE) |
968 (1 << FSCACHE_OP_UNUSE_COOKIE);
David Howellsef778e72012-12-20 21:52:36 +0000969
970 spin_lock(&cookie->lock);
971 if (fscache_submit_exclusive_op(object, op) < 0)
David Howells8d763492012-12-05 13:34:48 +0000972 goto submit_op_failed;
David Howellsef778e72012-12-20 21:52:36 +0000973 spin_unlock(&cookie->lock);
974 fscache_put_operation(op);
975
976 /* Once we've completed the invalidation, we know there will be no data
977 * stored in the cache and thus we can reinstate the data-check-skip
978 * optimisation.
979 */
980 set_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
981
982 /* We can allow read and write requests to come in once again. They'll
983 * queue up behind our exclusive invalidation operation.
984 */
David Howellscaaef692013-05-10 19:50:26 +0100985 if (test_and_clear_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags))
986 wake_up_bit(&cookie->flags, FSCACHE_COOKIE_INVALIDATING);
987 _leave(" [ok]");
988 return transit_to(UPDATE_OBJECT);
David Howells8d763492012-12-05 13:34:48 +0000989
David Howells13627292013-05-10 19:50:26 +0100990nomem:
David Howellsf09b4432015-02-24 10:05:28 +0000991 fscache_mark_object_dead(object);
David Howells13627292013-05-10 19:50:26 +0100992 fscache_unuse_cookie(object);
993 _leave(" [ENOMEM]");
994 return transit_to(KILL_OBJECT);
995
David Howells8d763492012-12-05 13:34:48 +0000996submit_op_failed:
David Howellsf09b4432015-02-24 10:05:28 +0000997 fscache_mark_object_dead(object);
David Howells8d763492012-12-05 13:34:48 +0000998 spin_unlock(&cookie->lock);
Milosz Tanski920bce22014-08-13 12:58:21 -0400999 fscache_unuse_cookie(object);
David Howells8d763492012-12-05 13:34:48 +00001000 kfree(op);
David Howells8d763492012-12-05 13:34:48 +00001001 _leave(" [EIO]");
David Howellscaaef692013-05-10 19:50:26 +01001002 return transit_to(KILL_OBJECT);
1003}
1004
1005static const struct fscache_state *fscache_invalidate_object(struct fscache_object *object,
1006 int event)
1007{
1008 const struct fscache_state *s;
1009
1010 fscache_stat(&fscache_n_invalidates_run);
1011 fscache_stat(&fscache_n_cop_invalidate_object);
1012 s = _fscache_invalidate_object(object, event);
1013 fscache_stat_d(&fscache_n_cop_invalidate_object);
1014 return s;
1015}
1016
1017/*
1018 * Asynchronously update an object.
1019 */
1020static const struct fscache_state *fscache_update_object(struct fscache_object *object,
1021 int event)
1022{
1023 _enter("{OBJ%x},%d", object->debug_id, event);
1024
1025 fscache_stat(&fscache_n_updates_run);
1026 fscache_stat(&fscache_n_cop_update_object);
1027 object->cache->ops->update_object(object);
1028 fscache_stat_d(&fscache_n_cop_update_object);
1029
1030 _leave("");
1031 return transit_to(WAIT_FOR_CMD);
David Howellsef778e72012-12-20 21:52:36 +00001032}
David Howells182d9192015-02-19 23:47:31 +00001033
1034/**
1035 * fscache_object_retrying_stale - Note retrying stale object
1036 * @object: The object that will be retried
1037 *
1038 * Note that an object lookup found an on-disk object that was adjudged to be
1039 * stale and has been deleted. The lookup will be retried.
1040 */
1041void fscache_object_retrying_stale(struct fscache_object *object)
1042{
1043 fscache_stat(&fscache_n_cache_no_space_reject);
1044}
1045EXPORT_SYMBOL(fscache_object_retrying_stale);
1046
1047/**
1048 * fscache_object_mark_killed - Note that an object was killed
1049 * @object: The object that was culled
1050 * @why: The reason the object was killed.
1051 *
1052 * Note that an object was killed. Returns true if the object was
1053 * already marked killed, false if it wasn't.
1054 */
1055void fscache_object_mark_killed(struct fscache_object *object,
1056 enum fscache_why_object_killed why)
1057{
1058 if (test_and_set_bit(FSCACHE_OBJECT_KILLED_BY_CACHE, &object->flags)) {
1059 pr_err("Error: Object already killed by cache [%s]\n",
1060 object->cache->identifier);
1061 return;
1062 }
1063
1064 switch (why) {
1065 case FSCACHE_OBJECT_NO_SPACE:
1066 fscache_stat(&fscache_n_cache_no_space_reject);
1067 break;
1068 case FSCACHE_OBJECT_IS_STALE:
1069 fscache_stat(&fscache_n_cache_stale_objects);
1070 break;
1071 case FSCACHE_OBJECT_WAS_RETIRED:
1072 fscache_stat(&fscache_n_cache_retired_objects);
1073 break;
1074 case FSCACHE_OBJECT_WAS_CULLED:
1075 fscache_stat(&fscache_n_cache_culled_objects);
1076 break;
1077 }
1078}
1079EXPORT_SYMBOL(fscache_object_mark_killed);