blob: 20e0d0a4dc8cba917ef354e93f1aafe8bfee46a8 [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 Howellse26bfeb2017-01-31 09:45:28 +000033static const struct fscache_state *fscache_object_dead(struct fscache_object *, int);
David Howells36c95592009-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 Howells36c95592009-04-03 16:42:38 +0100146static void fscache_dequeue_object(struct fscache_object *);
David Howells402cb8d2018-04-04 13:41:28 +0100147static void fscache_update_aux_data(struct fscache_object *);
David Howells36c95592009-04-03 16:42:38 +0100148
David Howells36c95592009-04-03 16:42:38 +0100149/*
150 * we need to notify the parent when an op completes that we had outstanding
151 * upon it
152 */
153static inline void fscache_done_parent_op(struct fscache_object *object)
154{
155 struct fscache_object *parent = object->parent;
156
157 _enter("OBJ%x {OBJ%x,%x}",
158 object->debug_id, parent->debug_id, parent->n_ops);
159
160 spin_lock_nested(&parent->lock, 1);
David Howells36c95592009-04-03 16:42:38 +0100161 parent->n_obj_ops--;
David Howells13627292013-05-10 19:50:26 +0100162 parent->n_ops--;
David Howells36c95592009-04-03 16:42:38 +0100163 if (parent->n_ops == 0)
164 fscache_raise_event(parent, FSCACHE_OBJECT_EV_CLEARED);
165 spin_unlock(&parent->lock);
166}
167
168/*
David Howellscaaef692013-05-10 19:50:26 +0100169 * Object state machine dispatcher.
David Howellsef778e72012-12-20 21:52:36 +0000170 */
David Howellscaaef692013-05-10 19:50:26 +0100171static void fscache_object_sm_dispatcher(struct fscache_object *object)
David Howellsef778e72012-12-20 21:52:36 +0000172{
David Howellscaaef692013-05-10 19:50:26 +0100173 const struct fscache_transition *t;
174 const struct fscache_state *state, *new_state;
175 unsigned long events, event_mask;
David Howellsa18feb52018-04-04 13:41:27 +0100176 bool oob;
David Howellscaaef692013-05-10 19:50:26 +0100177 int event = -1;
David Howells36c95592009-04-03 16:42:38 +0100178
179 ASSERT(object != NULL);
180
181 _enter("{OBJ%x,%s,%lx}",
David Howellscaaef692013-05-10 19:50:26 +0100182 object->debug_id, object->state->name, object->events);
David Howells36c95592009-04-03 16:42:38 +0100183
David Howellscaaef692013-05-10 19:50:26 +0100184 event_mask = object->event_mask;
185restart:
186 object->event_mask = 0; /* Mask normal event handling */
187 state = object->state;
188restart_masked:
189 events = object->events;
David Howells36c95592009-04-03 16:42:38 +0100190
David Howellscaaef692013-05-10 19:50:26 +0100191 /* Handle any out-of-band events (typically an error) */
192 if (events & object->oob_event_mask) {
193 _debug("{OBJ%x} oob %lx",
194 object->debug_id, events & object->oob_event_mask);
David Howellsa18feb52018-04-04 13:41:27 +0100195 oob = true;
David Howellscaaef692013-05-10 19:50:26 +0100196 for (t = object->oob_table; t->events; t++) {
197 if (events & t->events) {
198 state = t->transit_to;
199 ASSERT(state->work != NULL);
200 event = fls(events & t->events) - 1;
201 __clear_bit(event, &object->oob_event_mask);
202 clear_bit(event, &object->events);
203 goto execute_work_state;
204 }
David Howellsd461d262009-11-19 18:11:41 +0000205 }
David Howellscaaef692013-05-10 19:50:26 +0100206 }
David Howellsa18feb52018-04-04 13:41:27 +0100207 oob = false;
David Howells36c95592009-04-03 16:42:38 +0100208
David Howellscaaef692013-05-10 19:50:26 +0100209 /* Wait states are just transition tables */
210 if (!state->work) {
211 if (events & event_mask) {
212 for (t = state->transitions; t->events; t++) {
213 if (events & t->events) {
214 new_state = t->transit_to;
215 event = fls(events & t->events) - 1;
David Howellsa18feb52018-04-04 13:41:27 +0100216 trace_fscache_osm(object, state,
217 true, false, event);
David Howellscaaef692013-05-10 19:50:26 +0100218 clear_bit(event, &object->events);
219 _debug("{OBJ%x} ev %d: %s -> %s",
220 object->debug_id, event,
221 state->name, new_state->name);
222 object->state = state = new_state;
223 goto execute_work_state;
224 }
225 }
David Howells36c95592009-04-03 16:42:38 +0100226
David Howellscaaef692013-05-10 19:50:26 +0100227 /* The event mask didn't include all the tabled bits */
228 BUG();
David Howells36c95592009-04-03 16:42:38 +0100229 }
David Howellscaaef692013-05-10 19:50:26 +0100230 /* Randomly woke up */
231 goto unmask_events;
David Howells36c95592009-04-03 16:42:38 +0100232 }
233
David Howellscaaef692013-05-10 19:50:26 +0100234execute_work_state:
235 _debug("{OBJ%x} exec %s", object->debug_id, state->name);
236
David Howellsa18feb52018-04-04 13:41:27 +0100237 trace_fscache_osm(object, state, false, oob, event);
David Howellscaaef692013-05-10 19:50:26 +0100238 new_state = state->work(object, event);
239 event = -1;
240 if (new_state == NO_TRANSIT) {
241 _debug("{OBJ%x} %s notrans", object->debug_id, state->name);
David Howellse26bfeb2017-01-31 09:45:28 +0000242 if (unlikely(state == STATE(OBJECT_DEAD))) {
243 _leave(" [dead]");
244 return;
245 }
David Howellscaaef692013-05-10 19:50:26 +0100246 fscache_enqueue_object(object);
247 event_mask = object->oob_event_mask;
248 goto unmask_events;
David Howells36c95592009-04-03 16:42:38 +0100249 }
250
David Howellscaaef692013-05-10 19:50:26 +0100251 _debug("{OBJ%x} %s -> %s",
252 object->debug_id, state->name, new_state->name);
253 object->state = state = new_state;
254
255 if (state->work) {
David Howellse26bfeb2017-01-31 09:45:28 +0000256 if (unlikely(state == STATE(OBJECT_DEAD))) {
David Howellscaaef692013-05-10 19:50:26 +0100257 _leave(" [dead]");
258 return;
259 }
260 goto restart_masked;
David Howells36c95592009-04-03 16:42:38 +0100261 }
262
David Howellscaaef692013-05-10 19:50:26 +0100263 /* Transited to wait state */
264 event_mask = object->oob_event_mask;
265 for (t = state->transitions; t->events; t++)
266 event_mask |= t->events;
David Howells36c95592009-04-03 16:42:38 +0100267
David Howellscaaef692013-05-10 19:50:26 +0100268unmask_events:
269 object->event_mask = event_mask;
270 smp_mb();
271 events = object->events;
272 if (events & event_mask)
273 goto restart;
274 _leave(" [msk %lx]", event_mask);
David Howells36c95592009-04-03 16:42:38 +0100275}
276
277/*
278 * execute an object
279 */
David Howells610be242013-05-10 19:50:25 +0100280static void fscache_object_work_func(struct work_struct *work)
David Howells36c95592009-04-03 16:42:38 +0100281{
282 struct fscache_object *object =
283 container_of(work, struct fscache_object, work);
284 unsigned long start;
285
286 _enter("{OBJ%x}", object->debug_id);
287
David Howells36c95592009-04-03 16:42:38 +0100288 start = jiffies;
David Howellscaaef692013-05-10 19:50:26 +0100289 fscache_object_sm_dispatcher(object);
David Howells36c95592009-04-03 16:42:38 +0100290 fscache_hist(fscache_objs_histogram, start);
David Howellsa18feb52018-04-04 13:41:27 +0100291 fscache_put_object(object, fscache_obj_put_work);
David Howells36c95592009-04-03 16:42:38 +0100292}
David Howells610be242013-05-10 19:50:25 +0100293
294/**
295 * fscache_object_init - Initialise a cache object description
296 * @object: Object description
297 * @cookie: Cookie object will be attached to
298 * @cache: Cache in which backing object will be found
299 *
300 * Initialise a cache object description to its basic values.
301 *
302 * See Documentation/filesystems/caching/backend-api.txt for a complete
303 * description.
304 */
305void fscache_object_init(struct fscache_object *object,
306 struct fscache_cookie *cookie,
307 struct fscache_cache *cache)
308{
David Howellscaaef692013-05-10 19:50:26 +0100309 const struct fscache_transition *t;
310
David Howells610be242013-05-10 19:50:25 +0100311 atomic_inc(&cache->object_count);
312
David Howellscaaef692013-05-10 19:50:26 +0100313 object->state = STATE(WAIT_FOR_INIT);
314 object->oob_table = fscache_osm_init_oob;
315 object->flags = 1 << FSCACHE_OBJECT_IS_LIVE;
David Howells610be242013-05-10 19:50:25 +0100316 spin_lock_init(&object->lock);
317 INIT_LIST_HEAD(&object->cache_link);
318 INIT_HLIST_NODE(&object->cookie_link);
319 INIT_WORK(&object->work, fscache_object_work_func);
320 INIT_LIST_HEAD(&object->dependents);
321 INIT_LIST_HEAD(&object->dep_link);
322 INIT_LIST_HEAD(&object->pending_ops);
323 object->n_children = 0;
324 object->n_ops = object->n_in_progress = object->n_exclusive = 0;
David Howellscaaef692013-05-10 19:50:26 +0100325 object->events = 0;
David Howells610be242013-05-10 19:50:25 +0100326 object->store_limit = 0;
327 object->store_limit_l = 0;
328 object->cache = cache;
329 object->cookie = cookie;
330 object->parent = NULL;
David Howells7026f192014-02-17 15:01:47 +0000331#ifdef CONFIG_FSCACHE_OBJECT_LIST
332 RB_CLEAR_NODE(&object->objlist_link);
333#endif
David Howellscaaef692013-05-10 19:50:26 +0100334
335 object->oob_event_mask = 0;
336 for (t = object->oob_table; t->events; t++)
337 object->oob_event_mask |= t->events;
338 object->event_mask = object->oob_event_mask;
339 for (t = object->state->transitions; t->events; t++)
340 object->event_mask |= t->events;
David Howells610be242013-05-10 19:50:25 +0100341}
342EXPORT_SYMBOL(fscache_object_init);
David Howells440f0af2009-11-19 18:11:01 +0000343
344/*
David Howellsf09b4432015-02-24 10:05:28 +0000345 * Mark the object as no longer being live, making sure that we synchronise
346 * against op submission.
347 */
348static inline void fscache_mark_object_dead(struct fscache_object *object)
349{
350 spin_lock(&object->lock);
351 clear_bit(FSCACHE_OBJECT_IS_LIVE, &object->flags);
352 spin_unlock(&object->lock);
353}
354
355/*
David Howellscaaef692013-05-10 19:50:26 +0100356 * Abort object initialisation before we start it.
357 */
358static const struct fscache_state *fscache_abort_initialisation(struct fscache_object *object,
359 int event)
360{
David Howellscaaef692013-05-10 19:50:26 +0100361 _enter("{OBJ%x},%d", object->debug_id, event);
362
363 object->oob_event_mask = 0;
David Howellscaaef692013-05-10 19:50:26 +0100364 fscache_dequeue_object(object);
David Howellscaaef692013-05-10 19:50:26 +0100365 return transit_to(KILL_OBJECT);
366}
367
368/*
David Howells36c95592009-04-03 16:42:38 +0100369 * initialise an object
370 * - check the specified object's parent to see if we can make use of it
371 * immediately to do a creation
372 * - we may need to start the process of creating a parent and we need to wait
373 * for the parent's lookup and creation to complete if it's not there yet
David Howells36c95592009-04-03 16:42:38 +0100374 */
David Howellscaaef692013-05-10 19:50:26 +0100375static const struct fscache_state *fscache_initialise_object(struct fscache_object *object,
376 int event)
David Howells36c95592009-04-03 16:42:38 +0100377{
378 struct fscache_object *parent;
David Howellscaaef692013-05-10 19:50:26 +0100379 bool success;
David Howells36c95592009-04-03 16:42:38 +0100380
David Howellscaaef692013-05-10 19:50:26 +0100381 _enter("{OBJ%x},%d", object->debug_id, event);
David Howells36c95592009-04-03 16:42:38 +0100382
David Howellscaaef692013-05-10 19:50:26 +0100383 ASSERT(list_empty(&object->dep_link));
David Howells36c95592009-04-03 16:42:38 +0100384
385 parent = object->parent;
386 if (!parent) {
David Howellscaaef692013-05-10 19:50:26 +0100387 _leave(" [no parent]");
David Howells13627292013-05-10 19:50:26 +0100388 return transit_to(DROP_OBJECT);
David Howells36c95592009-04-03 16:42:38 +0100389 }
390
David Howells13627292013-05-10 19:50:26 +0100391 _debug("parent: %s of:%lx", parent->state->name, parent->flags);
David Howellscaaef692013-05-10 19:50:26 +0100392
393 if (fscache_object_is_dying(parent)) {
394 _leave(" [bad parent]");
David Howells13627292013-05-10 19:50:26 +0100395 return transit_to(DROP_OBJECT);
David Howellscaaef692013-05-10 19:50:26 +0100396 }
397
398 if (fscache_object_is_available(parent)) {
399 _leave(" [ready]");
400 return transit_to(PARENT_READY);
401 }
402
403 _debug("wait");
404
405 spin_lock(&parent->lock);
406 fscache_stat(&fscache_n_cop_grab_object);
407 success = false;
408 if (fscache_object_is_live(parent) &&
David Howellsa18feb52018-04-04 13:41:27 +0100409 object->cache->ops->grab_object(object, fscache_obj_get_add_to_deps)) {
David Howellscaaef692013-05-10 19:50:26 +0100410 list_add(&object->dep_link, &parent->dependents);
411 success = true;
412 }
413 fscache_stat_d(&fscache_n_cop_grab_object);
414 spin_unlock(&parent->lock);
415 if (!success) {
416 _leave(" [grab failed]");
David Howells13627292013-05-10 19:50:26 +0100417 return transit_to(DROP_OBJECT);
David Howellscaaef692013-05-10 19:50:26 +0100418 }
419
420 /* fscache_acquire_non_index_cookie() uses this
421 * to wake the chain up */
422 fscache_raise_event(parent, FSCACHE_OBJECT_EV_NEW_CHILD);
423 _leave(" [wait]");
424 return transit_to(WAIT_FOR_PARENT);
425}
426
427/*
428 * Once the parent object is ready, we should kick off our lookup op.
429 */
430static const struct fscache_state *fscache_parent_ready(struct fscache_object *object,
431 int event)
432{
433 struct fscache_object *parent = object->parent;
434
435 _enter("{OBJ%x},%d", object->debug_id, event);
436
437 ASSERT(parent != NULL);
438
439 spin_lock(&parent->lock);
440 parent->n_ops++;
441 parent->n_obj_ops++;
442 object->lookup_jif = jiffies;
443 spin_unlock(&parent->lock);
444
David Howells36c95592009-04-03 16:42:38 +0100445 _leave("");
David Howellscaaef692013-05-10 19:50:26 +0100446 return transit_to(LOOK_UP_OBJECT);
David Howells36c95592009-04-03 16:42:38 +0100447}
448
449/*
450 * look an object up in the cache from which it was allocated
451 * - we hold an "access lock" on the parent object, so the parent object cannot
452 * be withdrawn by either party till we've finished
David Howells36c95592009-04-03 16:42:38 +0100453 */
David Howellscaaef692013-05-10 19:50:26 +0100454static const struct fscache_state *fscache_look_up_object(struct fscache_object *object,
455 int event)
David Howells36c95592009-04-03 16:42:38 +0100456{
457 struct fscache_cookie *cookie = object->cookie;
David Howellscaaef692013-05-10 19:50:26 +0100458 struct fscache_object *parent = object->parent;
David Howellsfee096d2009-11-19 18:12:05 +0000459 int ret;
David Howells36c95592009-04-03 16:42:38 +0100460
David Howellscaaef692013-05-10 19:50:26 +0100461 _enter("{OBJ%x},%d", object->debug_id, event);
David Howells36c95592009-04-03 16:42:38 +0100462
David Howellscaaef692013-05-10 19:50:26 +0100463 object->oob_table = fscache_osm_lookup_oob;
464
David Howells36c95592009-04-03 16:42:38 +0100465 ASSERT(parent != NULL);
466 ASSERTCMP(parent->n_ops, >, 0);
467 ASSERTCMP(parent->n_obj_ops, >, 0);
468
469 /* make sure the parent is still available */
David Howells493f7bc2013-05-10 19:50:26 +0100470 ASSERT(fscache_object_is_available(parent));
David Howells36c95592009-04-03 16:42:38 +0100471
David Howells493f7bc2013-05-10 19:50:26 +0100472 if (fscache_object_is_dying(parent) ||
David Howells13627292013-05-10 19:50:26 +0100473 test_bit(FSCACHE_IOERROR, &object->cache->flags) ||
474 !fscache_use_cookie(object)) {
David Howellscaaef692013-05-10 19:50:26 +0100475 _leave(" [unavailable]");
476 return transit_to(LOOKUP_FAILURE);
David Howells36c95592009-04-03 16:42:38 +0100477 }
478
David Howells13627292013-05-10 19:50:26 +0100479 _debug("LOOKUP \"%s\" in \"%s\"",
480 cookie->def->name, object->cache->tag->name);
David Howells36c95592009-04-03 16:42:38 +0100481
482 fscache_stat(&fscache_n_object_lookups);
David Howells52bd75f2009-11-19 18:11:08 +0000483 fscache_stat(&fscache_n_cop_lookup_object);
David Howellsfee096d2009-11-19 18:12:05 +0000484 ret = object->cache->ops->lookup_object(object);
David Howells52bd75f2009-11-19 18:11:08 +0000485 fscache_stat_d(&fscache_n_cop_lookup_object);
David Howells36c95592009-04-03 16:42:38 +0100486
David Howells13627292013-05-10 19:50:26 +0100487 fscache_unuse_cookie(object);
David Howells36c95592009-04-03 16:42:38 +0100488
David Howellsfee096d2009-11-19 18:12:05 +0000489 if (ret == -ETIMEDOUT) {
490 /* probably stuck behind another object, so move this one to
491 * the back of the queue */
492 fscache_stat(&fscache_n_object_lookups_timed_out);
David Howellscaaef692013-05-10 19:50:26 +0100493 _leave(" [timeout]");
494 return NO_TRANSIT;
David Howellsfee096d2009-11-19 18:12:05 +0000495 }
496
David Howellscaaef692013-05-10 19:50:26 +0100497 if (ret < 0) {
498 _leave(" [error]");
499 return transit_to(LOOKUP_FAILURE);
500 }
501
502 _leave(" [ok]");
503 return transit_to(OBJECT_AVAILABLE);
David Howells36c95592009-04-03 16:42:38 +0100504}
505
506/**
507 * fscache_object_lookup_negative - Note negative cookie lookup
508 * @object: Object pointing to cookie to mark
509 *
510 * Note negative lookup, permitting those waiting to read data from an already
511 * existing backing object to continue as there's no data for them to read.
512 */
513void fscache_object_lookup_negative(struct fscache_object *object)
514{
515 struct fscache_cookie *cookie = object->cookie;
516
David Howellscaaef692013-05-10 19:50:26 +0100517 _enter("{OBJ%x,%s}", object->debug_id, object->state->name);
David Howells36c95592009-04-03 16:42:38 +0100518
David Howellscaaef692013-05-10 19:50:26 +0100519 if (!test_and_set_bit(FSCACHE_OBJECT_IS_LOOKED_UP, &object->flags)) {
David Howells36c95592009-04-03 16:42:38 +0100520 fscache_stat(&fscache_n_object_lookups_negative);
521
David Howellscaaef692013-05-10 19:50:26 +0100522 /* Allow write requests to begin stacking up and read requests to begin
523 * returning ENODATA.
524 */
David Howells36c95592009-04-03 16:42:38 +0100525 set_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
David Howells94d30ae2013-09-21 00:09:31 +0100526 clear_bit(FSCACHE_COOKIE_UNAVAILABLE, &cookie->flags);
David Howells36c95592009-04-03 16:42:38 +0100527
528 _debug("wake up lookup %p", &cookie->flags);
David Howellscaaef692013-05-10 19:50:26 +0100529 clear_bit_unlock(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags);
David Howells36c95592009-04-03 16:42:38 +0100530 wake_up_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP);
David Howells36c95592009-04-03 16:42:38 +0100531 }
David Howells36c95592009-04-03 16:42:38 +0100532 _leave("");
533}
534EXPORT_SYMBOL(fscache_object_lookup_negative);
535
536/**
537 * fscache_obtained_object - Note successful object lookup or creation
538 * @object: Object pointing to cookie to mark
539 *
540 * Note successful lookup and/or creation, permitting those waiting to write
541 * data to a backing object to continue.
542 *
543 * Note that after calling this, an object's cookie may be relinquished by the
544 * netfs, and so must be accessed with object lock held.
545 */
546void fscache_obtained_object(struct fscache_object *object)
547{
548 struct fscache_cookie *cookie = object->cookie;
549
David Howellscaaef692013-05-10 19:50:26 +0100550 _enter("{OBJ%x,%s}", object->debug_id, object->state->name);
David Howells36c95592009-04-03 16:42:38 +0100551
552 /* if we were still looking up, then we must have a positive lookup
553 * result, in which case there may be data available */
David Howellscaaef692013-05-10 19:50:26 +0100554 if (!test_and_set_bit(FSCACHE_OBJECT_IS_LOOKED_UP, &object->flags)) {
David Howells36c95592009-04-03 16:42:38 +0100555 fscache_stat(&fscache_n_object_lookups_positive);
556
David Howellscaaef692013-05-10 19:50:26 +0100557 /* We do (presumably) have data */
558 clear_bit_unlock(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
David Howells94d30ae2013-09-21 00:09:31 +0100559 clear_bit(FSCACHE_COOKIE_UNAVAILABLE, &cookie->flags);
David Howells36c95592009-04-03 16:42:38 +0100560
David Howellscaaef692013-05-10 19:50:26 +0100561 /* Allow write requests to begin stacking up and read requests
562 * to begin shovelling data.
563 */
564 clear_bit_unlock(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags);
David Howells36c95592009-04-03 16:42:38 +0100565 wake_up_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP);
David Howells36c95592009-04-03 16:42:38 +0100566 } else {
David Howells36c95592009-04-03 16:42:38 +0100567 fscache_stat(&fscache_n_object_created);
David Howells36c95592009-04-03 16:42:38 +0100568 }
569
David Howellscaaef692013-05-10 19:50:26 +0100570 set_bit(FSCACHE_OBJECT_IS_AVAILABLE, &object->flags);
David Howells36c95592009-04-03 16:42:38 +0100571 _leave("");
572}
573EXPORT_SYMBOL(fscache_obtained_object);
574
575/*
576 * handle an object that has just become available
577 */
David Howellscaaef692013-05-10 19:50:26 +0100578static const struct fscache_state *fscache_object_available(struct fscache_object *object,
579 int event)
David Howells36c95592009-04-03 16:42:38 +0100580{
David Howellscaaef692013-05-10 19:50:26 +0100581 _enter("{OBJ%x},%d", object->debug_id, event);
582
583 object->oob_table = fscache_osm_run_oob;
David Howells36c95592009-04-03 16:42:38 +0100584
585 spin_lock(&object->lock);
586
David Howells36c95592009-04-03 16:42:38 +0100587 fscache_done_parent_op(object);
588 if (object->n_in_progress == 0) {
589 if (object->n_ops > 0) {
590 ASSERTCMP(object->n_ops, >=, object->n_obj_ops);
David Howells36c95592009-04-03 16:42:38 +0100591 fscache_start_operations(object);
592 } else {
593 ASSERT(list_empty(&object->pending_ops));
594 }
595 }
596 spin_unlock(&object->lock);
597
David Howells52bd75f2009-11-19 18:11:08 +0000598 fscache_stat(&fscache_n_cop_lookup_complete);
David Howells36c95592009-04-03 16:42:38 +0100599 object->cache->ops->lookup_complete(object);
David Howells52bd75f2009-11-19 18:11:08 +0000600 fscache_stat_d(&fscache_n_cop_lookup_complete);
David Howells36c95592009-04-03 16:42:38 +0100601
602 fscache_hist(fscache_obj_instantiate_histogram, object->lookup_jif);
603 fscache_stat(&fscache_n_object_avail);
604
605 _leave("");
David Howellscaaef692013-05-10 19:50:26 +0100606 return transit_to(JUMPSTART_DEPS);
David Howells36c95592009-04-03 16:42:38 +0100607}
608
609/*
David Howellscaaef692013-05-10 19:50:26 +0100610 * Wake up this object's dependent objects now that we've become available.
David Howells36c95592009-04-03 16:42:38 +0100611 */
David Howellscaaef692013-05-10 19:50:26 +0100612static const struct fscache_state *fscache_jumpstart_dependents(struct fscache_object *object,
613 int event)
614{
615 _enter("{OBJ%x},%d", object->debug_id, event);
616
617 if (!fscache_enqueue_dependents(object, FSCACHE_OBJECT_EV_PARENT_READY))
618 return NO_TRANSIT; /* Not finished; requeue */
619 return transit_to(WAIT_FOR_CMD);
620}
621
622/*
623 * Handle lookup or creation failute.
624 */
625static const struct fscache_state *fscache_lookup_failure(struct fscache_object *object,
626 int event)
627{
628 struct fscache_cookie *cookie;
David Howellscaaef692013-05-10 19:50:26 +0100629
630 _enter("{OBJ%x},%d", object->debug_id, event);
631
632 object->oob_event_mask = 0;
633
634 fscache_stat(&fscache_n_cop_lookup_complete);
635 object->cache->ops->lookup_complete(object);
636 fscache_stat_d(&fscache_n_cop_lookup_complete);
637
David Howells6515d1d2015-02-25 11:53:57 +0000638 set_bit(FSCACHE_OBJECT_KILLED_BY_CACHE, &object->flags);
639
David Howellscaaef692013-05-10 19:50:26 +0100640 cookie = object->cookie;
641 set_bit(FSCACHE_COOKIE_UNAVAILABLE, &cookie->flags);
David Howells13627292013-05-10 19:50:26 +0100642 if (test_and_clear_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags))
David Howellscaaef692013-05-10 19:50:26 +0100643 wake_up_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP);
David Howellscaaef692013-05-10 19:50:26 +0100644
645 fscache_done_parent_op(object);
646 return transit_to(KILL_OBJECT);
647}
648
649/*
650 * Wait for completion of all active operations on this object and the death of
651 * all child objects of this object.
652 */
653static const struct fscache_state *fscache_kill_object(struct fscache_object *object,
654 int event)
655{
656 _enter("{OBJ%x,%d,%d},%d",
657 object->debug_id, object->n_ops, object->n_children, event);
658
David Howellsf09b4432015-02-24 10:05:28 +0000659 fscache_mark_object_dead(object);
David Howells13627292013-05-10 19:50:26 +0100660 object->oob_event_mask = 0;
David Howellscaaef692013-05-10 19:50:26 +0100661
David Howells6bdded52017-01-18 14:29:25 +0000662 if (test_bit(FSCACHE_OBJECT_RETIRED, &object->flags)) {
663 /* Reject any new read/write ops and abort any that are pending. */
664 clear_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags);
665 fscache_cancel_all_ops(object);
666 }
667
David Howellscaaef692013-05-10 19:50:26 +0100668 if (list_empty(&object->dependents) &&
669 object->n_ops == 0 &&
670 object->n_children == 0)
David Howells13627292013-05-10 19:50:26 +0100671 return transit_to(DROP_OBJECT);
David Howellscaaef692013-05-10 19:50:26 +0100672
David Howells13627292013-05-10 19:50:26 +0100673 if (object->n_in_progress == 0) {
674 spin_lock(&object->lock);
675 if (object->n_ops > 0 && object->n_in_progress == 0)
676 fscache_start_operations(object);
677 spin_unlock(&object->lock);
678 }
David Howellscaaef692013-05-10 19:50:26 +0100679
680 if (!list_empty(&object->dependents))
681 return transit_to(KILL_DEPENDENTS);
682
683 return transit_to(WAIT_FOR_CLEARANCE);
684}
685
686/*
687 * Kill dependent objects.
688 */
689static const struct fscache_state *fscache_kill_dependents(struct fscache_object *object,
690 int event)
691{
692 _enter("{OBJ%x},%d", object->debug_id, event);
693
694 if (!fscache_enqueue_dependents(object, FSCACHE_OBJECT_EV_KILL))
695 return NO_TRANSIT; /* Not finished */
696 return transit_to(WAIT_FOR_CLEARANCE);
697}
698
699/*
David Howellscaaef692013-05-10 19:50:26 +0100700 * Drop an object's attachments
701 */
702static const struct fscache_state *fscache_drop_object(struct fscache_object *object,
703 int event)
David Howells36c95592009-04-03 16:42:38 +0100704{
705 struct fscache_object *parent = object->parent;
David Howells13627292013-05-10 19:50:26 +0100706 struct fscache_cookie *cookie = object->cookie;
David Howells36c95592009-04-03 16:42:38 +0100707 struct fscache_cache *cache = object->cache;
David Howells13627292013-05-10 19:50:26 +0100708 bool awaken = false;
David Howells36c95592009-04-03 16:42:38 +0100709
David Howellscaaef692013-05-10 19:50:26 +0100710 _enter("{OBJ%x,%d},%d", object->debug_id, object->n_children, event);
David Howells36c95592009-04-03 16:42:38 +0100711
David Howells13627292013-05-10 19:50:26 +0100712 ASSERT(cookie != NULL);
713 ASSERT(!hlist_unhashed(&object->cookie_link));
714
David Howells402cb8d2018-04-04 13:41:28 +0100715 if (test_bit(FSCACHE_COOKIE_AUX_UPDATED, &cookie->flags)) {
716 _debug("final update");
717 fscache_update_aux_data(object);
718 }
719
David Howells13627292013-05-10 19:50:26 +0100720 /* Make sure the cookie no longer points here and that the netfs isn't
721 * waiting for us.
722 */
723 spin_lock(&cookie->lock);
724 hlist_del_init(&object->cookie_link);
David Howells94d30ae2013-09-21 00:09:31 +0100725 if (hlist_empty(&cookie->backing_objects) &&
726 test_and_clear_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags))
David Howells13627292013-05-10 19:50:26 +0100727 awaken = true;
728 spin_unlock(&cookie->lock);
729
730 if (awaken)
731 wake_up_bit(&cookie->flags, FSCACHE_COOKIE_INVALIDATING);
David Howells6897e3d2009-11-19 18:11:22 +0000732
David Howellscaaef692013-05-10 19:50:26 +0100733 /* Prevent a race with our last child, which has to signal EV_CLEARED
734 * before dropping our spinlock.
735 */
736 spin_lock(&object->lock);
737 spin_unlock(&object->lock);
738
739 /* Discard from the cache's collection of objects */
David Howells36c95592009-04-03 16:42:38 +0100740 spin_lock(&cache->object_list_lock);
741 list_del_init(&object->cache_link);
742 spin_unlock(&cache->object_list_lock);
743
David Howells52bd75f2009-11-19 18:11:08 +0000744 fscache_stat(&fscache_n_cop_drop_object);
David Howells36c95592009-04-03 16:42:38 +0100745 cache->ops->drop_object(object);
David Howells52bd75f2009-11-19 18:11:08 +0000746 fscache_stat_d(&fscache_n_cop_drop_object);
David Howells36c95592009-04-03 16:42:38 +0100747
David Howellscaaef692013-05-10 19:50:26 +0100748 /* The parent object wants to know when all it dependents have gone */
David Howells36c95592009-04-03 16:42:38 +0100749 if (parent) {
750 _debug("release parent OBJ%x {%d}",
751 parent->debug_id, parent->n_children);
752
753 spin_lock(&parent->lock);
754 parent->n_children--;
755 if (parent->n_children == 0)
756 fscache_raise_event(parent, FSCACHE_OBJECT_EV_CLEARED);
757 spin_unlock(&parent->lock);
758 object->parent = NULL;
759 }
760
Tejun Heo8b8edef2010-07-20 22:09:01 +0200761 /* this just shifts the object release to the work processor */
David Howellsa18feb52018-04-04 13:41:27 +0100762 fscache_put_object(object, fscache_obj_put_drop_obj);
David Howellscaaef692013-05-10 19:50:26 +0100763 fscache_stat(&fscache_n_object_dead);
David Howells36c95592009-04-03 16:42:38 +0100764
765 _leave("");
David Howellscaaef692013-05-10 19:50:26 +0100766 return transit_to(OBJECT_DEAD);
David Howells36c95592009-04-03 16:42:38 +0100767}
768
769/*
Tejun Heo8b8edef2010-07-20 22:09:01 +0200770 * get a ref on an object
David Howells36c95592009-04-03 16:42:38 +0100771 */
David Howellsa18feb52018-04-04 13:41:27 +0100772static int fscache_get_object(struct fscache_object *object,
773 enum fscache_obj_ref_trace why)
David Howells36c95592009-04-03 16:42:38 +0100774{
David Howells52bd75f2009-11-19 18:11:08 +0000775 int ret;
David Howells36c95592009-04-03 16:42:38 +0100776
David Howells52bd75f2009-11-19 18:11:08 +0000777 fscache_stat(&fscache_n_cop_grab_object);
David Howellsa18feb52018-04-04 13:41:27 +0100778 ret = object->cache->ops->grab_object(object, why) ? 0 : -EAGAIN;
David Howells52bd75f2009-11-19 18:11:08 +0000779 fscache_stat_d(&fscache_n_cop_grab_object);
780 return ret;
David Howells36c95592009-04-03 16:42:38 +0100781}
782
783/*
David Howellscaaef692013-05-10 19:50:26 +0100784 * Discard a ref on an object
David Howells36c95592009-04-03 16:42:38 +0100785 */
David Howellsa18feb52018-04-04 13:41:27 +0100786static void fscache_put_object(struct fscache_object *object,
787 enum fscache_obj_ref_trace why)
David Howells36c95592009-04-03 16:42:38 +0100788{
David Howells52bd75f2009-11-19 18:11:08 +0000789 fscache_stat(&fscache_n_cop_put_object);
David Howellsa18feb52018-04-04 13:41:27 +0100790 object->cache->ops->put_object(object, why);
David Howells52bd75f2009-11-19 18:11:08 +0000791 fscache_stat_d(&fscache_n_cop_put_object);
David Howells36c95592009-04-03 16:42:38 +0100792}
793
David Howells13627292013-05-10 19:50:26 +0100794/**
795 * fscache_object_destroy - Note that a cache object is about to be destroyed
796 * @object: The object to be destroyed
797 *
798 * Note the imminent destruction and deallocation of a cache object record.
799 */
800void fscache_object_destroy(struct fscache_object *object)
801{
802 fscache_objlist_remove(object);
803
804 /* We can get rid of the cookie now */
David Howellsa18feb52018-04-04 13:41:27 +0100805 fscache_cookie_put(object->cookie, fscache_cookie_put_object);
David Howells13627292013-05-10 19:50:26 +0100806 object->cookie = NULL;
807}
808EXPORT_SYMBOL(fscache_object_destroy);
809
David Howells36c95592009-04-03 16:42:38 +0100810/*
811 * enqueue an object for metadata-type processing
812 */
813void fscache_enqueue_object(struct fscache_object *object)
814{
815 _enter("{OBJ%x}", object->debug_id);
816
David Howellsa18feb52018-04-04 13:41:27 +0100817 if (fscache_get_object(object, fscache_obj_get_queue) >= 0) {
Tejun Heo8b8edef2010-07-20 22:09:01 +0200818 wait_queue_head_t *cong_wq =
819 &get_cpu_var(fscache_object_cong_wait);
820
821 if (queue_work(fscache_object_wq, &object->work)) {
822 if (fscache_object_congested())
823 wake_up(cong_wq);
824 } else
David Howellsa18feb52018-04-04 13:41:27 +0100825 fscache_put_object(object, fscache_obj_put_queue);
Tejun Heo8b8edef2010-07-20 22:09:01 +0200826
827 put_cpu_var(fscache_object_cong_wait);
828 }
David Howells36c95592009-04-03 16:42:38 +0100829}
830
Tejun Heo8b8edef2010-07-20 22:09:01 +0200831/**
832 * fscache_object_sleep_till_congested - Sleep until object wq is congested
David Howellscaaef692013-05-10 19:50:26 +0100833 * @timeoutp: Scheduler sleep timeout
Tejun Heo8b8edef2010-07-20 22:09:01 +0200834 *
835 * Allow an object handler to sleep until the object workqueue is congested.
836 *
837 * The caller must set up a wake up event before calling this and must have set
838 * the appropriate sleep mode (such as TASK_UNINTERRUPTIBLE) and tested its own
839 * condition before calling this function as no test is made here.
840 *
841 * %true is returned if the object wq is congested, %false otherwise.
842 */
843bool fscache_object_sleep_till_congested(signed long *timeoutp)
844{
Christoph Lameter170d8002013-10-15 12:22:29 -0600845 wait_queue_head_t *cong_wq = this_cpu_ptr(&fscache_object_cong_wait);
Tejun Heo8b8edef2010-07-20 22:09:01 +0200846 DEFINE_WAIT(wait);
847
848 if (fscache_object_congested())
849 return true;
850
851 add_wait_queue_exclusive(cong_wq, &wait);
852 if (!fscache_object_congested())
853 *timeoutp = schedule_timeout(*timeoutp);
854 finish_wait(cong_wq, &wait);
855
856 return fscache_object_congested();
857}
858EXPORT_SYMBOL_GPL(fscache_object_sleep_till_congested);
859
David Howells36c95592009-04-03 16:42:38 +0100860/*
David Howellscaaef692013-05-10 19:50:26 +0100861 * Enqueue the dependents of an object for metadata-type processing.
862 *
863 * If we don't manage to finish the list before the scheduler wants to run
864 * again then return false immediately. We return true if the list was
865 * cleared.
David Howells36c95592009-04-03 16:42:38 +0100866 */
David Howellscaaef692013-05-10 19:50:26 +0100867static bool fscache_enqueue_dependents(struct fscache_object *object, int event)
David Howells36c95592009-04-03 16:42:38 +0100868{
869 struct fscache_object *dep;
David Howellscaaef692013-05-10 19:50:26 +0100870 bool ret = true;
David Howells36c95592009-04-03 16:42:38 +0100871
872 _enter("{OBJ%x}", object->debug_id);
873
874 if (list_empty(&object->dependents))
David Howellscaaef692013-05-10 19:50:26 +0100875 return true;
David Howells36c95592009-04-03 16:42:38 +0100876
877 spin_lock(&object->lock);
878
879 while (!list_empty(&object->dependents)) {
880 dep = list_entry(object->dependents.next,
881 struct fscache_object, dep_link);
882 list_del_init(&dep->dep_link);
883
David Howellscaaef692013-05-10 19:50:26 +0100884 fscache_raise_event(dep, event);
David Howellsa18feb52018-04-04 13:41:27 +0100885 fscache_put_object(dep, fscache_obj_put_enq_dep);
David Howells36c95592009-04-03 16:42:38 +0100886
David Howellscaaef692013-05-10 19:50:26 +0100887 if (!list_empty(&object->dependents) && need_resched()) {
888 ret = false;
889 break;
890 }
David Howells36c95592009-04-03 16:42:38 +0100891 }
892
893 spin_unlock(&object->lock);
David Howellscaaef692013-05-10 19:50:26 +0100894 return ret;
David Howells36c95592009-04-03 16:42:38 +0100895}
896
897/*
898 * remove an object from whatever queue it's waiting on
David Howells36c95592009-04-03 16:42:38 +0100899 */
David Howellscaaef692013-05-10 19:50:26 +0100900static void fscache_dequeue_object(struct fscache_object *object)
David Howells36c95592009-04-03 16:42:38 +0100901{
902 _enter("{OBJ%x}", object->debug_id);
903
904 if (!list_empty(&object->dep_link)) {
905 spin_lock(&object->parent->lock);
906 list_del_init(&object->dep_link);
907 spin_unlock(&object->parent->lock);
908 }
909
910 _leave("");
911}
912
913/**
914 * fscache_check_aux - Ask the netfs whether an object on disk is still valid
915 * @object: The object to ask about
916 * @data: The auxiliary data for the object
917 * @datalen: The size of the auxiliary data
918 *
David Howells13627292013-05-10 19:50:26 +0100919 * This function consults the netfs about the coherency state of an object.
920 * The caller must be holding a ref on cookie->n_active (held by
921 * fscache_look_up_object() on behalf of the cache backend during object lookup
922 * and creation).
David Howells36c95592009-04-03 16:42:38 +0100923 */
924enum fscache_checkaux fscache_check_aux(struct fscache_object *object,
David Howellsee1235a2018-04-04 13:41:28 +0100925 const void *data, uint16_t datalen,
926 loff_t object_size)
David Howells36c95592009-04-03 16:42:38 +0100927{
928 enum fscache_checkaux result;
929
930 if (!object->cookie->def->check_aux) {
931 fscache_stat(&fscache_n_checkaux_none);
932 return FSCACHE_CHECKAUX_OKAY;
933 }
934
935 result = object->cookie->def->check_aux(object->cookie->netfs_data,
David Howellsee1235a2018-04-04 13:41:28 +0100936 data, datalen, object_size);
David Howells36c95592009-04-03 16:42:38 +0100937 switch (result) {
938 /* entry okay as is */
939 case FSCACHE_CHECKAUX_OKAY:
940 fscache_stat(&fscache_n_checkaux_okay);
941 break;
942
943 /* entry requires update */
944 case FSCACHE_CHECKAUX_NEEDS_UPDATE:
945 fscache_stat(&fscache_n_checkaux_update);
946 break;
947
948 /* entry requires deletion */
949 case FSCACHE_CHECKAUX_OBSOLETE:
950 fscache_stat(&fscache_n_checkaux_obsolete);
951 break;
952
953 default:
954 BUG();
955 }
956
957 return result;
958}
959EXPORT_SYMBOL(fscache_check_aux);
David Howellsef778e72012-12-20 21:52:36 +0000960
961/*
962 * Asynchronously invalidate an object.
963 */
David Howellscaaef692013-05-10 19:50:26 +0100964static const struct fscache_state *_fscache_invalidate_object(struct fscache_object *object,
965 int event)
David Howellsef778e72012-12-20 21:52:36 +0000966{
967 struct fscache_operation *op;
968 struct fscache_cookie *cookie = object->cookie;
969
David Howellscaaef692013-05-10 19:50:26 +0100970 _enter("{OBJ%x},%d", object->debug_id, event);
971
David Howells13627292013-05-10 19:50:26 +0100972 /* We're going to need the cookie. If the cookie is not available then
973 * retire the object instead.
974 */
975 if (!fscache_use_cookie(object)) {
Matthew Wilcoxe5a95542018-04-10 16:36:48 -0700976 ASSERT(radix_tree_empty(&object->cookie->stores));
David Howells94d30ae2013-09-21 00:09:31 +0100977 set_bit(FSCACHE_OBJECT_RETIRED, &object->flags);
David Howells13627292013-05-10 19:50:26 +0100978 _leave(" [no cookie]");
979 return transit_to(KILL_OBJECT);
980 }
David Howellsef778e72012-12-20 21:52:36 +0000981
982 /* Reject any new read/write ops and abort any that are pending. */
983 fscache_invalidate_writes(cookie);
984 clear_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags);
985 fscache_cancel_all_ops(object);
986
987 /* Now we have to wait for in-progress reads and writes */
988 op = kzalloc(sizeof(*op), GFP_KERNEL);
David Howells13627292013-05-10 19:50:26 +0100989 if (!op)
990 goto nomem;
David Howellsef778e72012-12-20 21:52:36 +0000991
David Howells08c2e3d2018-04-04 13:41:27 +0100992 fscache_operation_init(cookie, op, object->cache->ops->invalidate_object,
David Howellsd3b97ca2015-02-24 10:05:29 +0000993 NULL, NULL);
David Howells13627292013-05-10 19:50:26 +0100994 op->flags = FSCACHE_OP_ASYNC |
995 (1 << FSCACHE_OP_EXCLUSIVE) |
996 (1 << FSCACHE_OP_UNUSE_COOKIE);
David Howells08c2e3d2018-04-04 13:41:27 +0100997 trace_fscache_page_op(cookie, NULL, op, fscache_page_op_invalidate);
David Howellsef778e72012-12-20 21:52:36 +0000998
999 spin_lock(&cookie->lock);
1000 if (fscache_submit_exclusive_op(object, op) < 0)
David Howells8d763492012-12-05 13:34:48 +00001001 goto submit_op_failed;
David Howellsef778e72012-12-20 21:52:36 +00001002 spin_unlock(&cookie->lock);
1003 fscache_put_operation(op);
1004
1005 /* Once we've completed the invalidation, we know there will be no data
1006 * stored in the cache and thus we can reinstate the data-check-skip
1007 * optimisation.
1008 */
1009 set_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
1010
1011 /* We can allow read and write requests to come in once again. They'll
1012 * queue up behind our exclusive invalidation operation.
1013 */
David Howellscaaef692013-05-10 19:50:26 +01001014 if (test_and_clear_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags))
1015 wake_up_bit(&cookie->flags, FSCACHE_COOKIE_INVALIDATING);
1016 _leave(" [ok]");
1017 return transit_to(UPDATE_OBJECT);
David Howells8d763492012-12-05 13:34:48 +00001018
David Howells13627292013-05-10 19:50:26 +01001019nomem:
David Howellsf09b4432015-02-24 10:05:28 +00001020 fscache_mark_object_dead(object);
David Howells13627292013-05-10 19:50:26 +01001021 fscache_unuse_cookie(object);
1022 _leave(" [ENOMEM]");
1023 return transit_to(KILL_OBJECT);
1024
David Howells8d763492012-12-05 13:34:48 +00001025submit_op_failed:
David Howellsf09b4432015-02-24 10:05:28 +00001026 fscache_mark_object_dead(object);
David Howells8d763492012-12-05 13:34:48 +00001027 spin_unlock(&cookie->lock);
Milosz Tanski920bce22014-08-13 12:58:21 -04001028 fscache_unuse_cookie(object);
David Howells8d763492012-12-05 13:34:48 +00001029 kfree(op);
David Howells8d763492012-12-05 13:34:48 +00001030 _leave(" [EIO]");
David Howellscaaef692013-05-10 19:50:26 +01001031 return transit_to(KILL_OBJECT);
1032}
1033
1034static const struct fscache_state *fscache_invalidate_object(struct fscache_object *object,
1035 int event)
1036{
1037 const struct fscache_state *s;
1038
1039 fscache_stat(&fscache_n_invalidates_run);
1040 fscache_stat(&fscache_n_cop_invalidate_object);
1041 s = _fscache_invalidate_object(object, event);
1042 fscache_stat_d(&fscache_n_cop_invalidate_object);
1043 return s;
1044}
1045
1046/*
David Howells402cb8d2018-04-04 13:41:28 +01001047 * Update auxiliary data.
1048 */
1049static void fscache_update_aux_data(struct fscache_object *object)
1050{
1051 fscache_stat(&fscache_n_updates_run);
1052 fscache_stat(&fscache_n_cop_update_object);
1053 object->cache->ops->update_object(object);
1054 fscache_stat_d(&fscache_n_cop_update_object);
1055}
1056
1057/*
David Howellscaaef692013-05-10 19:50:26 +01001058 * Asynchronously update an object.
1059 */
1060static const struct fscache_state *fscache_update_object(struct fscache_object *object,
1061 int event)
1062{
1063 _enter("{OBJ%x},%d", object->debug_id, event);
1064
David Howells402cb8d2018-04-04 13:41:28 +01001065 fscache_update_aux_data(object);
David Howellscaaef692013-05-10 19:50:26 +01001066
1067 _leave("");
1068 return transit_to(WAIT_FOR_CMD);
David Howellsef778e72012-12-20 21:52:36 +00001069}
David Howells182d9192015-02-19 23:47:31 +00001070
1071/**
1072 * fscache_object_retrying_stale - Note retrying stale object
1073 * @object: The object that will be retried
1074 *
1075 * Note that an object lookup found an on-disk object that was adjudged to be
1076 * stale and has been deleted. The lookup will be retried.
1077 */
1078void fscache_object_retrying_stale(struct fscache_object *object)
1079{
1080 fscache_stat(&fscache_n_cache_no_space_reject);
1081}
1082EXPORT_SYMBOL(fscache_object_retrying_stale);
1083
1084/**
1085 * fscache_object_mark_killed - Note that an object was killed
1086 * @object: The object that was culled
1087 * @why: The reason the object was killed.
1088 *
1089 * Note that an object was killed. Returns true if the object was
1090 * already marked killed, false if it wasn't.
1091 */
1092void fscache_object_mark_killed(struct fscache_object *object,
1093 enum fscache_why_object_killed why)
1094{
1095 if (test_and_set_bit(FSCACHE_OBJECT_KILLED_BY_CACHE, &object->flags)) {
1096 pr_err("Error: Object already killed by cache [%s]\n",
1097 object->cache->identifier);
1098 return;
1099 }
1100
1101 switch (why) {
1102 case FSCACHE_OBJECT_NO_SPACE:
1103 fscache_stat(&fscache_n_cache_no_space_reject);
1104 break;
1105 case FSCACHE_OBJECT_IS_STALE:
1106 fscache_stat(&fscache_n_cache_stale_objects);
1107 break;
1108 case FSCACHE_OBJECT_WAS_RETIRED:
1109 fscache_stat(&fscache_n_cache_retired_objects);
1110 break;
1111 case FSCACHE_OBJECT_WAS_CULLED:
1112 fscache_stat(&fscache_n_cache_culled_objects);
1113 break;
1114 }
1115}
1116EXPORT_SYMBOL(fscache_object_mark_killed);
David Howellse26bfeb2017-01-31 09:45:28 +00001117
1118/*
1119 * The object is dead. We can get here if an object gets queued by an event
1120 * that would lead to its death (such as EV_KILL) when the dispatcher is
1121 * already running (and so can be requeued) but hasn't yet cleared the event
1122 * mask.
1123 */
1124static const struct fscache_state *fscache_object_dead(struct fscache_object *object,
1125 int event)
1126{
1127 if (!test_and_set_bit(FSCACHE_OBJECT_RUN_AFTER_DEAD,
1128 &object->flags))
1129 return NO_TRANSIT;
1130
1131 WARN(true, "FS-Cache object redispatched after death");
1132 return NO_TRANSIT;
1133}