blob: 7c0ddb7ae29ab0745b703a453f81a774edd41bc8 [file] [log] [blame]
David Howells36c955902009-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 Howells36c955902009-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 Howellse26bfeb2017-01-31 09:45:28 +000033static const struct fscache_state *fscache_object_dead(struct fscache_object *, int);
David Howells36c955902009-04-03 16:42:38 +010034
David Howellscaaef692013-05-10 19:50:26 +010035#define __STATE_NAME(n) fscache_osm_##n
36#define STATE(n) (&__STATE_NAME(n))
37
38/*
39 * Define a work state. Work states are execution states. No event processing
40 * is performed by them. The function attached to a work state returns a
41 * pointer indicating the next state to which the state machine should
42 * transition. Returning NO_TRANSIT repeats the current state, but goes back
43 * to the scheduler first.
44 */
45#define WORK_STATE(n, sn, f) \
46 const struct fscache_state __STATE_NAME(n) = { \
47 .name = #n, \
48 .short_name = sn, \
49 .work = f \
50 }
51
52/*
53 * Returns from work states.
54 */
55#define transit_to(state) ({ prefetch(&STATE(state)->work); STATE(state); })
56
57#define NO_TRANSIT ((struct fscache_state *)NULL)
58
59/*
60 * Define a wait state. Wait states are event processing states. No execution
61 * is performed by them. Wait states are just tables of "if event X occurs,
62 * clear it and transition to state Y". The dispatcher returns to the
63 * scheduler if none of the events in which the wait state has an interest are
64 * currently pending.
65 */
66#define WAIT_STATE(n, sn, ...) \
67 const struct fscache_state __STATE_NAME(n) = { \
68 .name = #n, \
69 .short_name = sn, \
70 .work = NULL, \
71 .transitions = { __VA_ARGS__, { 0, NULL } } \
72 }
73
74#define TRANSIT_TO(state, emask) \
75 { .events = (emask), .transit_to = STATE(state) }
76
77/*
78 * The object state machine.
79 */
80static WORK_STATE(INIT_OBJECT, "INIT", fscache_initialise_object);
81static WORK_STATE(PARENT_READY, "PRDY", fscache_parent_ready);
82static WORK_STATE(ABORT_INIT, "ABRT", fscache_abort_initialisation);
83static WORK_STATE(LOOK_UP_OBJECT, "LOOK", fscache_look_up_object);
84static WORK_STATE(CREATE_OBJECT, "CRTO", fscache_look_up_object);
85static WORK_STATE(OBJECT_AVAILABLE, "AVBL", fscache_object_available);
86static WORK_STATE(JUMPSTART_DEPS, "JUMP", fscache_jumpstart_dependents);
87
88static WORK_STATE(INVALIDATE_OBJECT, "INVL", fscache_invalidate_object);
89static WORK_STATE(UPDATE_OBJECT, "UPDT", fscache_update_object);
90
91static WORK_STATE(LOOKUP_FAILURE, "LCFL", fscache_lookup_failure);
92static WORK_STATE(KILL_OBJECT, "KILL", fscache_kill_object);
93static WORK_STATE(KILL_DEPENDENTS, "KDEP", fscache_kill_dependents);
94static WORK_STATE(DROP_OBJECT, "DROP", fscache_drop_object);
David Howellse26bfeb2017-01-31 09:45:28 +000095static WORK_STATE(OBJECT_DEAD, "DEAD", fscache_object_dead);
David Howellscaaef692013-05-10 19:50:26 +010096
97static WAIT_STATE(WAIT_FOR_INIT, "?INI",
98 TRANSIT_TO(INIT_OBJECT, 1 << FSCACHE_OBJECT_EV_NEW_CHILD));
99
100static WAIT_STATE(WAIT_FOR_PARENT, "?PRN",
101 TRANSIT_TO(PARENT_READY, 1 << FSCACHE_OBJECT_EV_PARENT_READY));
102
103static WAIT_STATE(WAIT_FOR_CMD, "?CMD",
104 TRANSIT_TO(INVALIDATE_OBJECT, 1 << FSCACHE_OBJECT_EV_INVALIDATE),
105 TRANSIT_TO(UPDATE_OBJECT, 1 << FSCACHE_OBJECT_EV_UPDATE),
106 TRANSIT_TO(JUMPSTART_DEPS, 1 << FSCACHE_OBJECT_EV_NEW_CHILD));
107
108static WAIT_STATE(WAIT_FOR_CLEARANCE, "?CLR",
109 TRANSIT_TO(KILL_OBJECT, 1 << FSCACHE_OBJECT_EV_CLEARED));
110
111/*
112 * Out-of-band event transition tables. These are for handling unexpected
113 * events, such as an I/O error. If an OOB event occurs, the state machine
114 * clears and disables the event and forces a transition to the nominated work
115 * state (acurrently executing work states will complete first).
116 *
117 * In such a situation, object->state remembers the state the machine should
118 * have been in/gone to and returning NO_TRANSIT returns to that.
119 */
120static const struct fscache_transition fscache_osm_init_oob[] = {
121 TRANSIT_TO(ABORT_INIT,
122 (1 << FSCACHE_OBJECT_EV_ERROR) |
123 (1 << FSCACHE_OBJECT_EV_KILL)),
124 { 0, NULL }
125};
126
127static const struct fscache_transition fscache_osm_lookup_oob[] = {
128 TRANSIT_TO(LOOKUP_FAILURE,
129 (1 << FSCACHE_OBJECT_EV_ERROR) |
130 (1 << FSCACHE_OBJECT_EV_KILL)),
131 { 0, NULL }
132};
133
134static const struct fscache_transition fscache_osm_run_oob[] = {
135 TRANSIT_TO(KILL_OBJECT,
136 (1 << FSCACHE_OBJECT_EV_ERROR) |
137 (1 << FSCACHE_OBJECT_EV_KILL)),
138 { 0, NULL }
David Howells440f0af2009-11-19 18:11:01 +0000139};
140
David Howellsa18feb52018-04-04 13:41:27 +0100141static int fscache_get_object(struct fscache_object *,
142 enum fscache_obj_ref_trace);
143static void fscache_put_object(struct fscache_object *,
144 enum fscache_obj_ref_trace);
David Howellscaaef692013-05-10 19:50:26 +0100145static bool fscache_enqueue_dependents(struct fscache_object *, int);
David Howells36c955902009-04-03 16:42:38 +0100146static void fscache_dequeue_object(struct fscache_object *);
147
David Howells36c955902009-04-03 16:42:38 +0100148/*
149 * we need to notify the parent when an op completes that we had outstanding
150 * upon it
151 */
152static inline void fscache_done_parent_op(struct fscache_object *object)
153{
154 struct fscache_object *parent = object->parent;
155
156 _enter("OBJ%x {OBJ%x,%x}",
157 object->debug_id, parent->debug_id, parent->n_ops);
158
159 spin_lock_nested(&parent->lock, 1);
David Howells36c955902009-04-03 16:42:38 +0100160 parent->n_obj_ops--;
David Howells13627292013-05-10 19:50:26 +0100161 parent->n_ops--;
David Howells36c955902009-04-03 16:42:38 +0100162 if (parent->n_ops == 0)
163 fscache_raise_event(parent, FSCACHE_OBJECT_EV_CLEARED);
164 spin_unlock(&parent->lock);
165}
166
167/*
David Howellscaaef692013-05-10 19:50:26 +0100168 * Object state machine dispatcher.
David Howellsef778e72012-12-20 21:52:36 +0000169 */
David Howellscaaef692013-05-10 19:50:26 +0100170static void fscache_object_sm_dispatcher(struct fscache_object *object)
David Howellsef778e72012-12-20 21:52:36 +0000171{
David Howellscaaef692013-05-10 19:50:26 +0100172 const struct fscache_transition *t;
173 const struct fscache_state *state, *new_state;
174 unsigned long events, event_mask;
David Howellsa18feb52018-04-04 13:41:27 +0100175 bool oob;
David Howellscaaef692013-05-10 19:50:26 +0100176 int event = -1;
David Howells36c955902009-04-03 16:42:38 +0100177
178 ASSERT(object != NULL);
179
180 _enter("{OBJ%x,%s,%lx}",
David Howellscaaef692013-05-10 19:50:26 +0100181 object->debug_id, object->state->name, object->events);
David Howells36c955902009-04-03 16:42:38 +0100182
David Howellscaaef692013-05-10 19:50:26 +0100183 event_mask = object->event_mask;
184restart:
185 object->event_mask = 0; /* Mask normal event handling */
186 state = object->state;
187restart_masked:
188 events = object->events;
David Howells36c955902009-04-03 16:42:38 +0100189
David Howellscaaef692013-05-10 19:50:26 +0100190 /* Handle any out-of-band events (typically an error) */
191 if (events & object->oob_event_mask) {
192 _debug("{OBJ%x} oob %lx",
193 object->debug_id, events & object->oob_event_mask);
David Howellsa18feb52018-04-04 13:41:27 +0100194 oob = true;
David Howellscaaef692013-05-10 19:50:26 +0100195 for (t = object->oob_table; t->events; t++) {
196 if (events & t->events) {
197 state = t->transit_to;
198 ASSERT(state->work != NULL);
199 event = fls(events & t->events) - 1;
200 __clear_bit(event, &object->oob_event_mask);
201 clear_bit(event, &object->events);
202 goto execute_work_state;
203 }
David Howellsd461d262009-11-19 18:11:41 +0000204 }
David Howellscaaef692013-05-10 19:50:26 +0100205 }
David Howellsa18feb52018-04-04 13:41:27 +0100206 oob = false;
David Howells36c955902009-04-03 16:42:38 +0100207
David Howellscaaef692013-05-10 19:50:26 +0100208 /* Wait states are just transition tables */
209 if (!state->work) {
210 if (events & event_mask) {
211 for (t = state->transitions; t->events; t++) {
212 if (events & t->events) {
213 new_state = t->transit_to;
214 event = fls(events & t->events) - 1;
David Howellsa18feb52018-04-04 13:41:27 +0100215 trace_fscache_osm(object, state,
216 true, false, event);
David Howellscaaef692013-05-10 19:50:26 +0100217 clear_bit(event, &object->events);
218 _debug("{OBJ%x} ev %d: %s -> %s",
219 object->debug_id, event,
220 state->name, new_state->name);
221 object->state = state = new_state;
222 goto execute_work_state;
223 }
224 }
David Howells36c955902009-04-03 16:42:38 +0100225
David Howellscaaef692013-05-10 19:50:26 +0100226 /* The event mask didn't include all the tabled bits */
227 BUG();
David Howells36c955902009-04-03 16:42:38 +0100228 }
David Howellscaaef692013-05-10 19:50:26 +0100229 /* Randomly woke up */
230 goto unmask_events;
David Howells36c955902009-04-03 16:42:38 +0100231 }
232
David Howellscaaef692013-05-10 19:50:26 +0100233execute_work_state:
234 _debug("{OBJ%x} exec %s", object->debug_id, state->name);
235
David Howellsa18feb52018-04-04 13:41:27 +0100236 trace_fscache_osm(object, state, false, oob, event);
David Howellscaaef692013-05-10 19:50:26 +0100237 new_state = state->work(object, event);
238 event = -1;
239 if (new_state == NO_TRANSIT) {
240 _debug("{OBJ%x} %s notrans", object->debug_id, state->name);
David Howellse26bfeb2017-01-31 09:45:28 +0000241 if (unlikely(state == STATE(OBJECT_DEAD))) {
242 _leave(" [dead]");
243 return;
244 }
David Howellscaaef692013-05-10 19:50:26 +0100245 fscache_enqueue_object(object);
246 event_mask = object->oob_event_mask;
247 goto unmask_events;
David Howells36c955902009-04-03 16:42:38 +0100248 }
249
David Howellscaaef692013-05-10 19:50:26 +0100250 _debug("{OBJ%x} %s -> %s",
251 object->debug_id, state->name, new_state->name);
252 object->state = state = new_state;
253
254 if (state->work) {
David Howellse26bfeb2017-01-31 09:45:28 +0000255 if (unlikely(state == STATE(OBJECT_DEAD))) {
David Howellscaaef692013-05-10 19:50:26 +0100256 _leave(" [dead]");
257 return;
258 }
259 goto restart_masked;
David Howells36c955902009-04-03 16:42:38 +0100260 }
261
David Howellscaaef692013-05-10 19:50:26 +0100262 /* Transited to wait state */
263 event_mask = object->oob_event_mask;
264 for (t = state->transitions; t->events; t++)
265 event_mask |= t->events;
David Howells36c955902009-04-03 16:42:38 +0100266
David Howellscaaef692013-05-10 19:50:26 +0100267unmask_events:
268 object->event_mask = event_mask;
269 smp_mb();
270 events = object->events;
271 if (events & event_mask)
272 goto restart;
273 _leave(" [msk %lx]", event_mask);
David Howells36c955902009-04-03 16:42:38 +0100274}
275
276/*
277 * execute an object
278 */
David Howells610be242013-05-10 19:50:25 +0100279static void fscache_object_work_func(struct work_struct *work)
David Howells36c955902009-04-03 16:42:38 +0100280{
281 struct fscache_object *object =
282 container_of(work, struct fscache_object, work);
283 unsigned long start;
284
285 _enter("{OBJ%x}", object->debug_id);
286
David Howells36c955902009-04-03 16:42:38 +0100287 start = jiffies;
David Howellscaaef692013-05-10 19:50:26 +0100288 fscache_object_sm_dispatcher(object);
David Howells36c955902009-04-03 16:42:38 +0100289 fscache_hist(fscache_objs_histogram, start);
David Howellsa18feb52018-04-04 13:41:27 +0100290 fscache_put_object(object, fscache_obj_put_work);
David Howells36c955902009-04-03 16:42:38 +0100291}
David Howells610be242013-05-10 19:50:25 +0100292
293/**
294 * fscache_object_init - Initialise a cache object description
295 * @object: Object description
296 * @cookie: Cookie object will be attached to
297 * @cache: Cache in which backing object will be found
298 *
299 * Initialise a cache object description to its basic values.
300 *
301 * See Documentation/filesystems/caching/backend-api.txt for a complete
302 * description.
303 */
304void fscache_object_init(struct fscache_object *object,
305 struct fscache_cookie *cookie,
306 struct fscache_cache *cache)
307{
David Howellscaaef692013-05-10 19:50:26 +0100308 const struct fscache_transition *t;
309
David Howells610be242013-05-10 19:50:25 +0100310 atomic_inc(&cache->object_count);
311
David Howellscaaef692013-05-10 19:50:26 +0100312 object->state = STATE(WAIT_FOR_INIT);
313 object->oob_table = fscache_osm_init_oob;
314 object->flags = 1 << FSCACHE_OBJECT_IS_LIVE;
David Howells610be242013-05-10 19:50:25 +0100315 spin_lock_init(&object->lock);
316 INIT_LIST_HEAD(&object->cache_link);
317 INIT_HLIST_NODE(&object->cookie_link);
318 INIT_WORK(&object->work, fscache_object_work_func);
319 INIT_LIST_HEAD(&object->dependents);
320 INIT_LIST_HEAD(&object->dep_link);
321 INIT_LIST_HEAD(&object->pending_ops);
322 object->n_children = 0;
323 object->n_ops = object->n_in_progress = object->n_exclusive = 0;
David Howellscaaef692013-05-10 19:50:26 +0100324 object->events = 0;
David Howells610be242013-05-10 19:50:25 +0100325 object->store_limit = 0;
326 object->store_limit_l = 0;
327 object->cache = cache;
328 object->cookie = cookie;
329 object->parent = NULL;
David Howells7026f192014-02-17 15:01:47 +0000330#ifdef CONFIG_FSCACHE_OBJECT_LIST
331 RB_CLEAR_NODE(&object->objlist_link);
332#endif
David Howellscaaef692013-05-10 19:50:26 +0100333
334 object->oob_event_mask = 0;
335 for (t = object->oob_table; t->events; t++)
336 object->oob_event_mask |= t->events;
337 object->event_mask = object->oob_event_mask;
338 for (t = object->state->transitions; t->events; t++)
339 object->event_mask |= t->events;
David Howells610be242013-05-10 19:50:25 +0100340}
341EXPORT_SYMBOL(fscache_object_init);
David Howells440f0af2009-11-19 18:11:01 +0000342
343/*
David Howellsf09b4432015-02-24 10:05:28 +0000344 * Mark the object as no longer being live, making sure that we synchronise
345 * against op submission.
346 */
347static inline void fscache_mark_object_dead(struct fscache_object *object)
348{
349 spin_lock(&object->lock);
350 clear_bit(FSCACHE_OBJECT_IS_LIVE, &object->flags);
351 spin_unlock(&object->lock);
352}
353
354/*
David Howellscaaef692013-05-10 19:50:26 +0100355 * Abort object initialisation before we start it.
356 */
357static const struct fscache_state *fscache_abort_initialisation(struct fscache_object *object,
358 int event)
359{
David Howellscaaef692013-05-10 19:50:26 +0100360 _enter("{OBJ%x},%d", object->debug_id, event);
361
362 object->oob_event_mask = 0;
David Howellscaaef692013-05-10 19:50:26 +0100363 fscache_dequeue_object(object);
David Howellscaaef692013-05-10 19:50:26 +0100364 return transit_to(KILL_OBJECT);
365}
366
367/*
David Howells36c955902009-04-03 16:42:38 +0100368 * initialise an object
369 * - check the specified object's parent to see if we can make use of it
370 * immediately to do a creation
371 * - we may need to start the process of creating a parent and we need to wait
372 * for the parent's lookup and creation to complete if it's not there yet
David Howells36c955902009-04-03 16:42:38 +0100373 */
David Howellscaaef692013-05-10 19:50:26 +0100374static const struct fscache_state *fscache_initialise_object(struct fscache_object *object,
375 int event)
David Howells36c955902009-04-03 16:42:38 +0100376{
377 struct fscache_object *parent;
David Howellscaaef692013-05-10 19:50:26 +0100378 bool success;
David Howells36c955902009-04-03 16:42:38 +0100379
David Howellscaaef692013-05-10 19:50:26 +0100380 _enter("{OBJ%x},%d", object->debug_id, event);
David Howells36c955902009-04-03 16:42:38 +0100381
David Howellscaaef692013-05-10 19:50:26 +0100382 ASSERT(list_empty(&object->dep_link));
David Howells36c955902009-04-03 16:42:38 +0100383
384 parent = object->parent;
385 if (!parent) {
David Howellscaaef692013-05-10 19:50:26 +0100386 _leave(" [no parent]");
David Howells13627292013-05-10 19:50:26 +0100387 return transit_to(DROP_OBJECT);
David Howells36c955902009-04-03 16:42:38 +0100388 }
389
David Howells13627292013-05-10 19:50:26 +0100390 _debug("parent: %s of:%lx", parent->state->name, parent->flags);
David Howellscaaef692013-05-10 19:50:26 +0100391
392 if (fscache_object_is_dying(parent)) {
393 _leave(" [bad parent]");
David Howells13627292013-05-10 19:50:26 +0100394 return transit_to(DROP_OBJECT);
David Howellscaaef692013-05-10 19:50:26 +0100395 }
396
397 if (fscache_object_is_available(parent)) {
398 _leave(" [ready]");
399 return transit_to(PARENT_READY);
400 }
401
402 _debug("wait");
403
404 spin_lock(&parent->lock);
405 fscache_stat(&fscache_n_cop_grab_object);
406 success = false;
407 if (fscache_object_is_live(parent) &&
David Howellsa18feb52018-04-04 13:41:27 +0100408 object->cache->ops->grab_object(object, fscache_obj_get_add_to_deps)) {
David Howellscaaef692013-05-10 19:50:26 +0100409 list_add(&object->dep_link, &parent->dependents);
410 success = true;
411 }
412 fscache_stat_d(&fscache_n_cop_grab_object);
413 spin_unlock(&parent->lock);
414 if (!success) {
415 _leave(" [grab failed]");
David Howells13627292013-05-10 19:50:26 +0100416 return transit_to(DROP_OBJECT);
David Howellscaaef692013-05-10 19:50:26 +0100417 }
418
419 /* fscache_acquire_non_index_cookie() uses this
420 * to wake the chain up */
421 fscache_raise_event(parent, FSCACHE_OBJECT_EV_NEW_CHILD);
422 _leave(" [wait]");
423 return transit_to(WAIT_FOR_PARENT);
424}
425
426/*
427 * Once the parent object is ready, we should kick off our lookup op.
428 */
429static const struct fscache_state *fscache_parent_ready(struct fscache_object *object,
430 int event)
431{
432 struct fscache_object *parent = object->parent;
433
434 _enter("{OBJ%x},%d", object->debug_id, event);
435
436 ASSERT(parent != NULL);
437
438 spin_lock(&parent->lock);
439 parent->n_ops++;
440 parent->n_obj_ops++;
441 object->lookup_jif = jiffies;
442 spin_unlock(&parent->lock);
443
David Howells36c955902009-04-03 16:42:38 +0100444 _leave("");
David Howellscaaef692013-05-10 19:50:26 +0100445 return transit_to(LOOK_UP_OBJECT);
David Howells36c955902009-04-03 16:42:38 +0100446}
447
448/*
449 * look an object up in the cache from which it was allocated
450 * - we hold an "access lock" on the parent object, so the parent object cannot
451 * be withdrawn by either party till we've finished
David Howells36c955902009-04-03 16:42:38 +0100452 */
David Howellscaaef692013-05-10 19:50:26 +0100453static const struct fscache_state *fscache_look_up_object(struct fscache_object *object,
454 int event)
David Howells36c955902009-04-03 16:42:38 +0100455{
456 struct fscache_cookie *cookie = object->cookie;
David Howellscaaef692013-05-10 19:50:26 +0100457 struct fscache_object *parent = object->parent;
David Howellsfee096d2009-11-19 18:12:05 +0000458 int ret;
David Howells36c955902009-04-03 16:42:38 +0100459
David Howellscaaef692013-05-10 19:50:26 +0100460 _enter("{OBJ%x},%d", object->debug_id, event);
David Howells36c955902009-04-03 16:42:38 +0100461
David Howellscaaef692013-05-10 19:50:26 +0100462 object->oob_table = fscache_osm_lookup_oob;
463
David Howells36c955902009-04-03 16:42:38 +0100464 ASSERT(parent != NULL);
465 ASSERTCMP(parent->n_ops, >, 0);
466 ASSERTCMP(parent->n_obj_ops, >, 0);
467
468 /* make sure the parent is still available */
David Howells493f7bc2013-05-10 19:50:26 +0100469 ASSERT(fscache_object_is_available(parent));
David Howells36c955902009-04-03 16:42:38 +0100470
David Howells493f7bc2013-05-10 19:50:26 +0100471 if (fscache_object_is_dying(parent) ||
David Howells13627292013-05-10 19:50:26 +0100472 test_bit(FSCACHE_IOERROR, &object->cache->flags) ||
473 !fscache_use_cookie(object)) {
David Howellscaaef692013-05-10 19:50:26 +0100474 _leave(" [unavailable]");
475 return transit_to(LOOKUP_FAILURE);
David Howells36c955902009-04-03 16:42:38 +0100476 }
477
David Howells13627292013-05-10 19:50:26 +0100478 _debug("LOOKUP \"%s\" in \"%s\"",
479 cookie->def->name, object->cache->tag->name);
David Howells36c955902009-04-03 16:42:38 +0100480
481 fscache_stat(&fscache_n_object_lookups);
David Howells52bd75f2009-11-19 18:11:08 +0000482 fscache_stat(&fscache_n_cop_lookup_object);
David Howellsfee096d2009-11-19 18:12:05 +0000483 ret = object->cache->ops->lookup_object(object);
David Howells52bd75f2009-11-19 18:11:08 +0000484 fscache_stat_d(&fscache_n_cop_lookup_object);
David Howells36c955902009-04-03 16:42:38 +0100485
David Howells13627292013-05-10 19:50:26 +0100486 fscache_unuse_cookie(object);
David Howells36c955902009-04-03 16:42:38 +0100487
David Howellsfee096d2009-11-19 18:12:05 +0000488 if (ret == -ETIMEDOUT) {
489 /* probably stuck behind another object, so move this one to
490 * the back of the queue */
491 fscache_stat(&fscache_n_object_lookups_timed_out);
David Howellscaaef692013-05-10 19:50:26 +0100492 _leave(" [timeout]");
493 return NO_TRANSIT;
David Howellsfee096d2009-11-19 18:12:05 +0000494 }
495
David Howellscaaef692013-05-10 19:50:26 +0100496 if (ret < 0) {
497 _leave(" [error]");
498 return transit_to(LOOKUP_FAILURE);
499 }
500
501 _leave(" [ok]");
502 return transit_to(OBJECT_AVAILABLE);
David Howells36c955902009-04-03 16:42:38 +0100503}
504
505/**
506 * fscache_object_lookup_negative - Note negative cookie lookup
507 * @object: Object pointing to cookie to mark
508 *
509 * Note negative lookup, permitting those waiting to read data from an already
510 * existing backing object to continue as there's no data for them to read.
511 */
512void fscache_object_lookup_negative(struct fscache_object *object)
513{
514 struct fscache_cookie *cookie = object->cookie;
515
David Howellscaaef692013-05-10 19:50:26 +0100516 _enter("{OBJ%x,%s}", object->debug_id, object->state->name);
David Howells36c955902009-04-03 16:42:38 +0100517
David Howellscaaef692013-05-10 19:50:26 +0100518 if (!test_and_set_bit(FSCACHE_OBJECT_IS_LOOKED_UP, &object->flags)) {
David Howells36c955902009-04-03 16:42:38 +0100519 fscache_stat(&fscache_n_object_lookups_negative);
520
David Howellscaaef692013-05-10 19:50:26 +0100521 /* Allow write requests to begin stacking up and read requests to begin
522 * returning ENODATA.
523 */
David Howells36c955902009-04-03 16:42:38 +0100524 set_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
David Howells94d30ae2013-09-21 00:09:31 +0100525 clear_bit(FSCACHE_COOKIE_UNAVAILABLE, &cookie->flags);
David Howells36c955902009-04-03 16:42:38 +0100526
527 _debug("wake up lookup %p", &cookie->flags);
David Howellscaaef692013-05-10 19:50:26 +0100528 clear_bit_unlock(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags);
David Howells36c955902009-04-03 16:42:38 +0100529 wake_up_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP);
David Howells36c955902009-04-03 16:42:38 +0100530 }
David Howells36c955902009-04-03 16:42:38 +0100531 _leave("");
532}
533EXPORT_SYMBOL(fscache_object_lookup_negative);
534
535/**
536 * fscache_obtained_object - Note successful object lookup or creation
537 * @object: Object pointing to cookie to mark
538 *
539 * Note successful lookup and/or creation, permitting those waiting to write
540 * data to a backing object to continue.
541 *
542 * Note that after calling this, an object's cookie may be relinquished by the
543 * netfs, and so must be accessed with object lock held.
544 */
545void fscache_obtained_object(struct fscache_object *object)
546{
547 struct fscache_cookie *cookie = object->cookie;
548
David Howellscaaef692013-05-10 19:50:26 +0100549 _enter("{OBJ%x,%s}", object->debug_id, object->state->name);
David Howells36c955902009-04-03 16:42:38 +0100550
551 /* if we were still looking up, then we must have a positive lookup
552 * result, in which case there may be data available */
David Howellscaaef692013-05-10 19:50:26 +0100553 if (!test_and_set_bit(FSCACHE_OBJECT_IS_LOOKED_UP, &object->flags)) {
David Howells36c955902009-04-03 16:42:38 +0100554 fscache_stat(&fscache_n_object_lookups_positive);
555
David Howellscaaef692013-05-10 19:50:26 +0100556 /* We do (presumably) have data */
557 clear_bit_unlock(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
David Howells94d30ae2013-09-21 00:09:31 +0100558 clear_bit(FSCACHE_COOKIE_UNAVAILABLE, &cookie->flags);
David Howells36c955902009-04-03 16:42:38 +0100559
David Howellscaaef692013-05-10 19:50:26 +0100560 /* Allow write requests to begin stacking up and read requests
561 * to begin shovelling data.
562 */
563 clear_bit_unlock(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags);
David Howells36c955902009-04-03 16:42:38 +0100564 wake_up_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP);
David Howells36c955902009-04-03 16:42:38 +0100565 } else {
David Howells36c955902009-04-03 16:42:38 +0100566 fscache_stat(&fscache_n_object_created);
David Howells36c955902009-04-03 16:42:38 +0100567 }
568
David Howellscaaef692013-05-10 19:50:26 +0100569 set_bit(FSCACHE_OBJECT_IS_AVAILABLE, &object->flags);
David Howells36c955902009-04-03 16:42:38 +0100570 _leave("");
571}
572EXPORT_SYMBOL(fscache_obtained_object);
573
574/*
575 * handle an object that has just become available
576 */
David Howellscaaef692013-05-10 19:50:26 +0100577static const struct fscache_state *fscache_object_available(struct fscache_object *object,
578 int event)
David Howells36c955902009-04-03 16:42:38 +0100579{
David Howellscaaef692013-05-10 19:50:26 +0100580 _enter("{OBJ%x},%d", object->debug_id, event);
581
582 object->oob_table = fscache_osm_run_oob;
David Howells36c955902009-04-03 16:42:38 +0100583
584 spin_lock(&object->lock);
585
David Howells36c955902009-04-03 16:42:38 +0100586 fscache_done_parent_op(object);
587 if (object->n_in_progress == 0) {
588 if (object->n_ops > 0) {
589 ASSERTCMP(object->n_ops, >=, object->n_obj_ops);
David Howells36c955902009-04-03 16:42:38 +0100590 fscache_start_operations(object);
591 } else {
592 ASSERT(list_empty(&object->pending_ops));
593 }
594 }
595 spin_unlock(&object->lock);
596
David Howells52bd75f2009-11-19 18:11:08 +0000597 fscache_stat(&fscache_n_cop_lookup_complete);
David Howells36c955902009-04-03 16:42:38 +0100598 object->cache->ops->lookup_complete(object);
David Howells52bd75f2009-11-19 18:11:08 +0000599 fscache_stat_d(&fscache_n_cop_lookup_complete);
David Howells36c955902009-04-03 16:42:38 +0100600
601 fscache_hist(fscache_obj_instantiate_histogram, object->lookup_jif);
602 fscache_stat(&fscache_n_object_avail);
603
604 _leave("");
David Howellscaaef692013-05-10 19:50:26 +0100605 return transit_to(JUMPSTART_DEPS);
David Howells36c955902009-04-03 16:42:38 +0100606}
607
608/*
David Howellscaaef692013-05-10 19:50:26 +0100609 * Wake up this object's dependent objects now that we've become available.
David Howells36c955902009-04-03 16:42:38 +0100610 */
David Howellscaaef692013-05-10 19:50:26 +0100611static const struct fscache_state *fscache_jumpstart_dependents(struct fscache_object *object,
612 int event)
613{
614 _enter("{OBJ%x},%d", object->debug_id, event);
615
616 if (!fscache_enqueue_dependents(object, FSCACHE_OBJECT_EV_PARENT_READY))
617 return NO_TRANSIT; /* Not finished; requeue */
618 return transit_to(WAIT_FOR_CMD);
619}
620
621/*
622 * Handle lookup or creation failute.
623 */
624static const struct fscache_state *fscache_lookup_failure(struct fscache_object *object,
625 int event)
626{
627 struct fscache_cookie *cookie;
David Howellscaaef692013-05-10 19:50:26 +0100628
629 _enter("{OBJ%x},%d", object->debug_id, event);
630
631 object->oob_event_mask = 0;
632
633 fscache_stat(&fscache_n_cop_lookup_complete);
634 object->cache->ops->lookup_complete(object);
635 fscache_stat_d(&fscache_n_cop_lookup_complete);
636
David Howells6515d1d2015-02-25 11:53:57 +0000637 set_bit(FSCACHE_OBJECT_KILLED_BY_CACHE, &object->flags);
638
David Howellscaaef692013-05-10 19:50:26 +0100639 cookie = object->cookie;
640 set_bit(FSCACHE_COOKIE_UNAVAILABLE, &cookie->flags);
David Howells13627292013-05-10 19:50:26 +0100641 if (test_and_clear_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags))
David Howellscaaef692013-05-10 19:50:26 +0100642 wake_up_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP);
David Howellscaaef692013-05-10 19:50:26 +0100643
644 fscache_done_parent_op(object);
645 return transit_to(KILL_OBJECT);
646}
647
648/*
649 * Wait for completion of all active operations on this object and the death of
650 * all child objects of this object.
651 */
652static const struct fscache_state *fscache_kill_object(struct fscache_object *object,
653 int event)
654{
655 _enter("{OBJ%x,%d,%d},%d",
656 object->debug_id, object->n_ops, object->n_children, event);
657
David Howellsf09b4432015-02-24 10:05:28 +0000658 fscache_mark_object_dead(object);
David Howells13627292013-05-10 19:50:26 +0100659 object->oob_event_mask = 0;
David Howellscaaef692013-05-10 19:50:26 +0100660
David Howells6bdded52017-01-18 14:29:25 +0000661 if (test_bit(FSCACHE_OBJECT_RETIRED, &object->flags)) {
662 /* Reject any new read/write ops and abort any that are pending. */
663 clear_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags);
664 fscache_cancel_all_ops(object);
665 }
666
David Howellscaaef692013-05-10 19:50:26 +0100667 if (list_empty(&object->dependents) &&
668 object->n_ops == 0 &&
669 object->n_children == 0)
David Howells13627292013-05-10 19:50:26 +0100670 return transit_to(DROP_OBJECT);
David Howellscaaef692013-05-10 19:50:26 +0100671
David Howells13627292013-05-10 19:50:26 +0100672 if (object->n_in_progress == 0) {
673 spin_lock(&object->lock);
674 if (object->n_ops > 0 && object->n_in_progress == 0)
675 fscache_start_operations(object);
676 spin_unlock(&object->lock);
677 }
David Howellscaaef692013-05-10 19:50:26 +0100678
679 if (!list_empty(&object->dependents))
680 return transit_to(KILL_DEPENDENTS);
681
682 return transit_to(WAIT_FOR_CLEARANCE);
683}
684
685/*
686 * Kill dependent objects.
687 */
688static const struct fscache_state *fscache_kill_dependents(struct fscache_object *object,
689 int event)
690{
691 _enter("{OBJ%x},%d", object->debug_id, event);
692
693 if (!fscache_enqueue_dependents(object, FSCACHE_OBJECT_EV_KILL))
694 return NO_TRANSIT; /* Not finished */
695 return transit_to(WAIT_FOR_CLEARANCE);
696}
697
698/*
David Howellscaaef692013-05-10 19:50:26 +0100699 * Drop an object's attachments
700 */
701static const struct fscache_state *fscache_drop_object(struct fscache_object *object,
702 int event)
David Howells36c955902009-04-03 16:42:38 +0100703{
704 struct fscache_object *parent = object->parent;
David Howells13627292013-05-10 19:50:26 +0100705 struct fscache_cookie *cookie = object->cookie;
David Howells36c955902009-04-03 16:42:38 +0100706 struct fscache_cache *cache = object->cache;
David Howells13627292013-05-10 19:50:26 +0100707 bool awaken = false;
David Howells36c955902009-04-03 16:42:38 +0100708
David Howellscaaef692013-05-10 19:50:26 +0100709 _enter("{OBJ%x,%d},%d", object->debug_id, object->n_children, event);
David Howells36c955902009-04-03 16:42:38 +0100710
David Howells13627292013-05-10 19:50:26 +0100711 ASSERT(cookie != NULL);
712 ASSERT(!hlist_unhashed(&object->cookie_link));
713
714 /* Make sure the cookie no longer points here and that the netfs isn't
715 * waiting for us.
716 */
717 spin_lock(&cookie->lock);
718 hlist_del_init(&object->cookie_link);
David Howells94d30ae2013-09-21 00:09:31 +0100719 if (hlist_empty(&cookie->backing_objects) &&
720 test_and_clear_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags))
David Howells13627292013-05-10 19:50:26 +0100721 awaken = true;
722 spin_unlock(&cookie->lock);
723
724 if (awaken)
725 wake_up_bit(&cookie->flags, FSCACHE_COOKIE_INVALIDATING);
David Howells6897e3d2009-11-19 18:11:22 +0000726
David Howellscaaef692013-05-10 19:50:26 +0100727 /* 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 */
David Howells36c955902009-04-03 16:42:38 +0100734 spin_lock(&cache->object_list_lock);
735 list_del_init(&object->cache_link);
736 spin_unlock(&cache->object_list_lock);
737
David Howells52bd75f2009-11-19 18:11:08 +0000738 fscache_stat(&fscache_n_cop_drop_object);
David Howells36c955902009-04-03 16:42:38 +0100739 cache->ops->drop_object(object);
David Howells52bd75f2009-11-19 18:11:08 +0000740 fscache_stat_d(&fscache_n_cop_drop_object);
David Howells36c955902009-04-03 16:42:38 +0100741
David Howellscaaef692013-05-10 19:50:26 +0100742 /* The parent object wants to know when all it dependents have gone */
David Howells36c955902009-04-03 16:42:38 +0100743 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
Tejun Heo8b8edef2010-07-20 22:09:01 +0200755 /* this just shifts the object release to the work processor */
David Howellsa18feb52018-04-04 13:41:27 +0100756 fscache_put_object(object, fscache_obj_put_drop_obj);
David Howellscaaef692013-05-10 19:50:26 +0100757 fscache_stat(&fscache_n_object_dead);
David Howells36c955902009-04-03 16:42:38 +0100758
759 _leave("");
David Howellscaaef692013-05-10 19:50:26 +0100760 return transit_to(OBJECT_DEAD);
David Howells36c955902009-04-03 16:42:38 +0100761}
762
763/*
Tejun Heo8b8edef2010-07-20 22:09:01 +0200764 * get a ref on an object
David Howells36c955902009-04-03 16:42:38 +0100765 */
David Howellsa18feb52018-04-04 13:41:27 +0100766static int fscache_get_object(struct fscache_object *object,
767 enum fscache_obj_ref_trace why)
David Howells36c955902009-04-03 16:42:38 +0100768{
David Howells52bd75f2009-11-19 18:11:08 +0000769 int ret;
David Howells36c955902009-04-03 16:42:38 +0100770
David Howells52bd75f2009-11-19 18:11:08 +0000771 fscache_stat(&fscache_n_cop_grab_object);
David Howellsa18feb52018-04-04 13:41:27 +0100772 ret = object->cache->ops->grab_object(object, why) ? 0 : -EAGAIN;
David Howells52bd75f2009-11-19 18:11:08 +0000773 fscache_stat_d(&fscache_n_cop_grab_object);
774 return ret;
David Howells36c955902009-04-03 16:42:38 +0100775}
776
777/*
David Howellscaaef692013-05-10 19:50:26 +0100778 * Discard a ref on an object
David Howells36c955902009-04-03 16:42:38 +0100779 */
David Howellsa18feb52018-04-04 13:41:27 +0100780static void fscache_put_object(struct fscache_object *object,
781 enum fscache_obj_ref_trace why)
David Howells36c955902009-04-03 16:42:38 +0100782{
David Howells52bd75f2009-11-19 18:11:08 +0000783 fscache_stat(&fscache_n_cop_put_object);
David Howellsa18feb52018-04-04 13:41:27 +0100784 object->cache->ops->put_object(object, why);
David Howells52bd75f2009-11-19 18:11:08 +0000785 fscache_stat_d(&fscache_n_cop_put_object);
David Howells36c955902009-04-03 16:42:38 +0100786}
787
David Howells13627292013-05-10 19:50:26 +0100788/**
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 */
794void fscache_object_destroy(struct fscache_object *object)
795{
796 fscache_objlist_remove(object);
797
798 /* We can get rid of the cookie now */
David Howellsa18feb52018-04-04 13:41:27 +0100799 fscache_cookie_put(object->cookie, fscache_cookie_put_object);
David Howells13627292013-05-10 19:50:26 +0100800 object->cookie = NULL;
801}
802EXPORT_SYMBOL(fscache_object_destroy);
803
David Howells36c955902009-04-03 16:42:38 +0100804/*
805 * enqueue an object for metadata-type processing
806 */
807void fscache_enqueue_object(struct fscache_object *object)
808{
809 _enter("{OBJ%x}", object->debug_id);
810
David Howellsa18feb52018-04-04 13:41:27 +0100811 if (fscache_get_object(object, fscache_obj_get_queue) >= 0) {
Tejun Heo8b8edef2010-07-20 22:09:01 +0200812 wait_queue_head_t *cong_wq =
813 &get_cpu_var(fscache_object_cong_wait);
814
815 if (queue_work(fscache_object_wq, &object->work)) {
816 if (fscache_object_congested())
817 wake_up(cong_wq);
818 } else
David Howellsa18feb52018-04-04 13:41:27 +0100819 fscache_put_object(object, fscache_obj_put_queue);
Tejun Heo8b8edef2010-07-20 22:09:01 +0200820
821 put_cpu_var(fscache_object_cong_wait);
822 }
David Howells36c955902009-04-03 16:42:38 +0100823}
824
Tejun Heo8b8edef2010-07-20 22:09:01 +0200825/**
826 * fscache_object_sleep_till_congested - Sleep until object wq is congested
David Howellscaaef692013-05-10 19:50:26 +0100827 * @timeoutp: Scheduler sleep timeout
Tejun Heo8b8edef2010-07-20 22:09:01 +0200828 *
829 * Allow an object handler to sleep until the object workqueue is congested.
830 *
831 * The caller must set up a wake up event before calling this and must have set
832 * the appropriate sleep mode (such as TASK_UNINTERRUPTIBLE) and tested its own
833 * condition before calling this function as no test is made here.
834 *
835 * %true is returned if the object wq is congested, %false otherwise.
836 */
837bool fscache_object_sleep_till_congested(signed long *timeoutp)
838{
Christoph Lameter170d8002013-10-15 12:22:29 -0600839 wait_queue_head_t *cong_wq = this_cpu_ptr(&fscache_object_cong_wait);
Tejun Heo8b8edef2010-07-20 22:09:01 +0200840 DEFINE_WAIT(wait);
841
842 if (fscache_object_congested())
843 return true;
844
845 add_wait_queue_exclusive(cong_wq, &wait);
846 if (!fscache_object_congested())
847 *timeoutp = schedule_timeout(*timeoutp);
848 finish_wait(cong_wq, &wait);
849
850 return fscache_object_congested();
851}
852EXPORT_SYMBOL_GPL(fscache_object_sleep_till_congested);
853
David Howells36c955902009-04-03 16:42:38 +0100854/*
David Howellscaaef692013-05-10 19:50:26 +0100855 * Enqueue the dependents of an object for metadata-type processing.
856 *
857 * If we don't manage to finish the list before the scheduler wants to run
858 * again then return false immediately. We return true if the list was
859 * cleared.
David Howells36c955902009-04-03 16:42:38 +0100860 */
David Howellscaaef692013-05-10 19:50:26 +0100861static bool fscache_enqueue_dependents(struct fscache_object *object, int event)
David Howells36c955902009-04-03 16:42:38 +0100862{
863 struct fscache_object *dep;
David Howellscaaef692013-05-10 19:50:26 +0100864 bool ret = true;
David Howells36c955902009-04-03 16:42:38 +0100865
866 _enter("{OBJ%x}", object->debug_id);
867
868 if (list_empty(&object->dependents))
David Howellscaaef692013-05-10 19:50:26 +0100869 return true;
David Howells36c955902009-04-03 16:42:38 +0100870
871 spin_lock(&object->lock);
872
873 while (!list_empty(&object->dependents)) {
874 dep = list_entry(object->dependents.next,
875 struct fscache_object, dep_link);
876 list_del_init(&dep->dep_link);
877
David Howellscaaef692013-05-10 19:50:26 +0100878 fscache_raise_event(dep, event);
David Howellsa18feb52018-04-04 13:41:27 +0100879 fscache_put_object(dep, fscache_obj_put_enq_dep);
David Howells36c955902009-04-03 16:42:38 +0100880
David Howellscaaef692013-05-10 19:50:26 +0100881 if (!list_empty(&object->dependents) && need_resched()) {
882 ret = false;
883 break;
884 }
David Howells36c955902009-04-03 16:42:38 +0100885 }
886
887 spin_unlock(&object->lock);
David Howellscaaef692013-05-10 19:50:26 +0100888 return ret;
David Howells36c955902009-04-03 16:42:38 +0100889}
890
891/*
892 * remove an object from whatever queue it's waiting on
David Howells36c955902009-04-03 16:42:38 +0100893 */
David Howellscaaef692013-05-10 19:50:26 +0100894static void fscache_dequeue_object(struct fscache_object *object)
David Howells36c955902009-04-03 16:42:38 +0100895{
896 _enter("{OBJ%x}", object->debug_id);
897
898 if (!list_empty(&object->dep_link)) {
899 spin_lock(&object->parent->lock);
900 list_del_init(&object->dep_link);
901 spin_unlock(&object->parent->lock);
902 }
903
904 _leave("");
905}
906
907/**
908 * fscache_check_aux - Ask the netfs whether an object on disk is still valid
909 * @object: The object to ask about
910 * @data: The auxiliary data for the object
911 * @datalen: The size of the auxiliary data
912 *
David Howells13627292013-05-10 19:50:26 +0100913 * This function consults the netfs about the coherency state of an object.
914 * The caller must be holding a ref on cookie->n_active (held by
915 * fscache_look_up_object() on behalf of the cache backend during object lookup
916 * and creation).
David Howells36c955902009-04-03 16:42:38 +0100917 */
918enum fscache_checkaux fscache_check_aux(struct fscache_object *object,
919 const void *data, uint16_t datalen)
920{
921 enum fscache_checkaux result;
922
923 if (!object->cookie->def->check_aux) {
924 fscache_stat(&fscache_n_checkaux_none);
925 return FSCACHE_CHECKAUX_OKAY;
926 }
927
928 result = object->cookie->def->check_aux(object->cookie->netfs_data,
929 data, datalen);
930 switch (result) {
931 /* entry okay as is */
932 case FSCACHE_CHECKAUX_OKAY:
933 fscache_stat(&fscache_n_checkaux_okay);
934 break;
935
936 /* entry requires update */
937 case FSCACHE_CHECKAUX_NEEDS_UPDATE:
938 fscache_stat(&fscache_n_checkaux_update);
939 break;
940
941 /* entry requires deletion */
942 case FSCACHE_CHECKAUX_OBSOLETE:
943 fscache_stat(&fscache_n_checkaux_obsolete);
944 break;
945
946 default:
947 BUG();
948 }
949
950 return result;
951}
952EXPORT_SYMBOL(fscache_check_aux);
David Howellsef778e72012-12-20 21:52:36 +0000953
954/*
955 * Asynchronously invalidate an object.
956 */
David Howellscaaef692013-05-10 19:50:26 +0100957static const struct fscache_state *_fscache_invalidate_object(struct fscache_object *object,
958 int event)
David Howellsef778e72012-12-20 21:52:36 +0000959{
960 struct fscache_operation *op;
961 struct fscache_cookie *cookie = object->cookie;
962
David Howellscaaef692013-05-10 19:50:26 +0100963 _enter("{OBJ%x},%d", object->debug_id, event);
964
David Howells13627292013-05-10 19:50:26 +0100965 /* We're going to need the cookie. If the cookie is not available then
966 * retire the object instead.
967 */
968 if (!fscache_use_cookie(object)) {
969 ASSERT(object->cookie->stores.rnode == NULL);
David Howells94d30ae2013-09-21 00:09:31 +0100970 set_bit(FSCACHE_OBJECT_RETIRED, &object->flags);
David Howells13627292013-05-10 19:50:26 +0100971 _leave(" [no cookie]");
972 return transit_to(KILL_OBJECT);
973 }
David Howellsef778e72012-12-20 21:52:36 +0000974
975 /* Reject any new read/write ops and abort any that are pending. */
976 fscache_invalidate_writes(cookie);
977 clear_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags);
978 fscache_cancel_all_ops(object);
979
980 /* Now we have to wait for in-progress reads and writes */
981 op = kzalloc(sizeof(*op), GFP_KERNEL);
David Howells13627292013-05-10 19:50:26 +0100982 if (!op)
983 goto nomem;
David Howellsef778e72012-12-20 21:52:36 +0000984
David Howells08c2e3d2018-04-04 13:41:27 +0100985 fscache_operation_init(cookie, op, object->cache->ops->invalidate_object,
David Howellsd3b97ca2015-02-24 10:05:29 +0000986 NULL, NULL);
David Howells13627292013-05-10 19:50:26 +0100987 op->flags = FSCACHE_OP_ASYNC |
988 (1 << FSCACHE_OP_EXCLUSIVE) |
989 (1 << FSCACHE_OP_UNUSE_COOKIE);
David Howells08c2e3d2018-04-04 13:41:27 +0100990 trace_fscache_page_op(cookie, NULL, op, fscache_page_op_invalidate);
David Howellsef778e72012-12-20 21:52:36 +0000991
992 spin_lock(&cookie->lock);
993 if (fscache_submit_exclusive_op(object, op) < 0)
David Howells8d763492012-12-05 13:34:48 +0000994 goto submit_op_failed;
David Howellsef778e72012-12-20 21:52:36 +0000995 spin_unlock(&cookie->lock);
996 fscache_put_operation(op);
997
998 /* Once we've completed the invalidation, we know there will be no data
999 * stored in the cache and thus we can reinstate the data-check-skip
1000 * optimisation.
1001 */
1002 set_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
1003
1004 /* We can allow read and write requests to come in once again. They'll
1005 * queue up behind our exclusive invalidation operation.
1006 */
David Howellscaaef692013-05-10 19:50:26 +01001007 if (test_and_clear_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags))
1008 wake_up_bit(&cookie->flags, FSCACHE_COOKIE_INVALIDATING);
1009 _leave(" [ok]");
1010 return transit_to(UPDATE_OBJECT);
David Howells8d763492012-12-05 13:34:48 +00001011
David Howells13627292013-05-10 19:50:26 +01001012nomem:
David Howellsf09b4432015-02-24 10:05:28 +00001013 fscache_mark_object_dead(object);
David Howells13627292013-05-10 19:50:26 +01001014 fscache_unuse_cookie(object);
1015 _leave(" [ENOMEM]");
1016 return transit_to(KILL_OBJECT);
1017
David Howells8d763492012-12-05 13:34:48 +00001018submit_op_failed:
David Howellsf09b4432015-02-24 10:05:28 +00001019 fscache_mark_object_dead(object);
David Howells8d763492012-12-05 13:34:48 +00001020 spin_unlock(&cookie->lock);
Milosz Tanski920bce22014-08-13 12:58:21 -04001021 fscache_unuse_cookie(object);
David Howells8d763492012-12-05 13:34:48 +00001022 kfree(op);
David Howells8d763492012-12-05 13:34:48 +00001023 _leave(" [EIO]");
David Howellscaaef692013-05-10 19:50:26 +01001024 return transit_to(KILL_OBJECT);
1025}
1026
1027static const struct fscache_state *fscache_invalidate_object(struct fscache_object *object,
1028 int event)
1029{
1030 const struct fscache_state *s;
1031
1032 fscache_stat(&fscache_n_invalidates_run);
1033 fscache_stat(&fscache_n_cop_invalidate_object);
1034 s = _fscache_invalidate_object(object, event);
1035 fscache_stat_d(&fscache_n_cop_invalidate_object);
1036 return s;
1037}
1038
1039/*
1040 * Asynchronously update an object.
1041 */
1042static const struct fscache_state *fscache_update_object(struct fscache_object *object,
1043 int event)
1044{
1045 _enter("{OBJ%x},%d", object->debug_id, event);
1046
1047 fscache_stat(&fscache_n_updates_run);
1048 fscache_stat(&fscache_n_cop_update_object);
1049 object->cache->ops->update_object(object);
1050 fscache_stat_d(&fscache_n_cop_update_object);
1051
1052 _leave("");
1053 return transit_to(WAIT_FOR_CMD);
David Howellsef778e72012-12-20 21:52:36 +00001054}
David Howells182d9192015-02-19 23:47:31 +00001055
1056/**
1057 * fscache_object_retrying_stale - Note retrying stale object
1058 * @object: The object that will be retried
1059 *
1060 * Note that an object lookup found an on-disk object that was adjudged to be
1061 * stale and has been deleted. The lookup will be retried.
1062 */
1063void fscache_object_retrying_stale(struct fscache_object *object)
1064{
1065 fscache_stat(&fscache_n_cache_no_space_reject);
1066}
1067EXPORT_SYMBOL(fscache_object_retrying_stale);
1068
1069/**
1070 * fscache_object_mark_killed - Note that an object was killed
1071 * @object: The object that was culled
1072 * @why: The reason the object was killed.
1073 *
1074 * Note that an object was killed. Returns true if the object was
1075 * already marked killed, false if it wasn't.
1076 */
1077void fscache_object_mark_killed(struct fscache_object *object,
1078 enum fscache_why_object_killed why)
1079{
1080 if (test_and_set_bit(FSCACHE_OBJECT_KILLED_BY_CACHE, &object->flags)) {
1081 pr_err("Error: Object already killed by cache [%s]\n",
1082 object->cache->identifier);
1083 return;
1084 }
1085
1086 switch (why) {
1087 case FSCACHE_OBJECT_NO_SPACE:
1088 fscache_stat(&fscache_n_cache_no_space_reject);
1089 break;
1090 case FSCACHE_OBJECT_IS_STALE:
1091 fscache_stat(&fscache_n_cache_stale_objects);
1092 break;
1093 case FSCACHE_OBJECT_WAS_RETIRED:
1094 fscache_stat(&fscache_n_cache_retired_objects);
1095 break;
1096 case FSCACHE_OBJECT_WAS_CULLED:
1097 fscache_stat(&fscache_n_cache_culled_objects);
1098 break;
1099 }
1100}
1101EXPORT_SYMBOL(fscache_object_mark_killed);
David Howellse26bfeb2017-01-31 09:45:28 +00001102
1103/*
1104 * The object is dead. We can get here if an object gets queued by an event
1105 * that would lead to its death (such as EV_KILL) when the dispatcher is
1106 * already running (and so can be requeued) but hasn't yet cleared the event
1107 * mask.
1108 */
1109static const struct fscache_state *fscache_object_dead(struct fscache_object *object,
1110 int event)
1111{
1112 if (!test_and_set_bit(FSCACHE_OBJECT_RUN_AFTER_DEAD,
1113 &object->flags))
1114 return NO_TRANSIT;
1115
1116 WARN(true, "FS-Cache object redispatched after death");
1117 return NO_TRANSIT;
1118}