David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 1 | /* FS-Cache object state machine handler |
| 2 | * |
| 3 | * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved. |
| 4 | * Written by David Howells (dhowells@redhat.com) |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * See Documentation/filesystems/caching/object.txt for a description of the |
| 12 | * object state machine and the in-kernel representations. |
| 13 | */ |
| 14 | |
| 15 | #define FSCACHE_DEBUG_LEVEL COOKIE |
| 16 | #include <linux/module.h> |
David Howells | ef778e7 | 2012-12-20 21:52:36 +0000 | [diff] [blame] | 17 | #include <linux/slab.h> |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 18 | #include <linux/prefetch.h> |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 19 | #include "internal.h" |
| 20 | |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 21 | static const struct fscache_state *fscache_abort_initialisation(struct fscache_object *, int); |
| 22 | static const struct fscache_state *fscache_kill_dependents(struct fscache_object *, int); |
| 23 | static const struct fscache_state *fscache_drop_object(struct fscache_object *, int); |
| 24 | static const struct fscache_state *fscache_initialise_object(struct fscache_object *, int); |
| 25 | static const struct fscache_state *fscache_invalidate_object(struct fscache_object *, int); |
| 26 | static const struct fscache_state *fscache_jumpstart_dependents(struct fscache_object *, int); |
| 27 | static const struct fscache_state *fscache_kill_object(struct fscache_object *, int); |
| 28 | static const struct fscache_state *fscache_lookup_failure(struct fscache_object *, int); |
| 29 | static const struct fscache_state *fscache_look_up_object(struct fscache_object *, int); |
| 30 | static const struct fscache_state *fscache_object_available(struct fscache_object *, int); |
| 31 | static const struct fscache_state *fscache_parent_ready(struct fscache_object *, int); |
| 32 | static const struct fscache_state *fscache_update_object(struct fscache_object *, int); |
David Howells | e26bfeb | 2017-01-31 09:45:28 +0000 | [diff] [blame] | 33 | static const struct fscache_state *fscache_object_dead(struct fscache_object *, int); |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 34 | |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 35 | #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 | */ |
| 80 | static WORK_STATE(INIT_OBJECT, "INIT", fscache_initialise_object); |
| 81 | static WORK_STATE(PARENT_READY, "PRDY", fscache_parent_ready); |
| 82 | static WORK_STATE(ABORT_INIT, "ABRT", fscache_abort_initialisation); |
| 83 | static WORK_STATE(LOOK_UP_OBJECT, "LOOK", fscache_look_up_object); |
| 84 | static WORK_STATE(CREATE_OBJECT, "CRTO", fscache_look_up_object); |
| 85 | static WORK_STATE(OBJECT_AVAILABLE, "AVBL", fscache_object_available); |
| 86 | static WORK_STATE(JUMPSTART_DEPS, "JUMP", fscache_jumpstart_dependents); |
| 87 | |
| 88 | static WORK_STATE(INVALIDATE_OBJECT, "INVL", fscache_invalidate_object); |
| 89 | static WORK_STATE(UPDATE_OBJECT, "UPDT", fscache_update_object); |
| 90 | |
| 91 | static WORK_STATE(LOOKUP_FAILURE, "LCFL", fscache_lookup_failure); |
| 92 | static WORK_STATE(KILL_OBJECT, "KILL", fscache_kill_object); |
| 93 | static WORK_STATE(KILL_DEPENDENTS, "KDEP", fscache_kill_dependents); |
| 94 | static WORK_STATE(DROP_OBJECT, "DROP", fscache_drop_object); |
David Howells | e26bfeb | 2017-01-31 09:45:28 +0000 | [diff] [blame] | 95 | static WORK_STATE(OBJECT_DEAD, "DEAD", fscache_object_dead); |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 96 | |
| 97 | static WAIT_STATE(WAIT_FOR_INIT, "?INI", |
| 98 | TRANSIT_TO(INIT_OBJECT, 1 << FSCACHE_OBJECT_EV_NEW_CHILD)); |
| 99 | |
| 100 | static WAIT_STATE(WAIT_FOR_PARENT, "?PRN", |
| 101 | TRANSIT_TO(PARENT_READY, 1 << FSCACHE_OBJECT_EV_PARENT_READY)); |
| 102 | |
| 103 | static 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 | |
| 108 | static 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 | */ |
| 120 | static 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 | |
| 127 | static 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 | |
| 134 | static 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 Howells | 440f0af | 2009-11-19 18:11:01 +0000 | [diff] [blame] | 139 | }; |
| 140 | |
David Howells | a18feb5 | 2018-04-04 13:41:27 +0100 | [diff] [blame] | 141 | static int fscache_get_object(struct fscache_object *, |
| 142 | enum fscache_obj_ref_trace); |
| 143 | static void fscache_put_object(struct fscache_object *, |
| 144 | enum fscache_obj_ref_trace); |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 145 | static bool fscache_enqueue_dependents(struct fscache_object *, int); |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 146 | static void fscache_dequeue_object(struct fscache_object *); |
| 147 | |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 148 | /* |
| 149 | * we need to notify the parent when an op completes that we had outstanding |
| 150 | * upon it |
| 151 | */ |
| 152 | static 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 Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 160 | parent->n_obj_ops--; |
David Howells | 1362729 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 161 | parent->n_ops--; |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 162 | if (parent->n_ops == 0) |
| 163 | fscache_raise_event(parent, FSCACHE_OBJECT_EV_CLEARED); |
| 164 | spin_unlock(&parent->lock); |
| 165 | } |
| 166 | |
| 167 | /* |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 168 | * Object state machine dispatcher. |
David Howells | ef778e7 | 2012-12-20 21:52:36 +0000 | [diff] [blame] | 169 | */ |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 170 | static void fscache_object_sm_dispatcher(struct fscache_object *object) |
David Howells | ef778e7 | 2012-12-20 21:52:36 +0000 | [diff] [blame] | 171 | { |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 172 | const struct fscache_transition *t; |
| 173 | const struct fscache_state *state, *new_state; |
| 174 | unsigned long events, event_mask; |
David Howells | a18feb5 | 2018-04-04 13:41:27 +0100 | [diff] [blame] | 175 | bool oob; |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 176 | int event = -1; |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 177 | |
| 178 | ASSERT(object != NULL); |
| 179 | |
| 180 | _enter("{OBJ%x,%s,%lx}", |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 181 | object->debug_id, object->state->name, object->events); |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 182 | |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 183 | event_mask = object->event_mask; |
| 184 | restart: |
| 185 | object->event_mask = 0; /* Mask normal event handling */ |
| 186 | state = object->state; |
| 187 | restart_masked: |
| 188 | events = object->events; |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 189 | |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 190 | /* 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 Howells | a18feb5 | 2018-04-04 13:41:27 +0100 | [diff] [blame] | 194 | oob = true; |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 195 | 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 Howells | d461d26 | 2009-11-19 18:11:41 +0000 | [diff] [blame] | 204 | } |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 205 | } |
David Howells | a18feb5 | 2018-04-04 13:41:27 +0100 | [diff] [blame] | 206 | oob = false; |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 207 | |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 208 | /* 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 Howells | a18feb5 | 2018-04-04 13:41:27 +0100 | [diff] [blame] | 215 | trace_fscache_osm(object, state, |
| 216 | true, false, event); |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 217 | 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 Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 225 | |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 226 | /* The event mask didn't include all the tabled bits */ |
| 227 | BUG(); |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 228 | } |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 229 | /* Randomly woke up */ |
| 230 | goto unmask_events; |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 231 | } |
| 232 | |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 233 | execute_work_state: |
| 234 | _debug("{OBJ%x} exec %s", object->debug_id, state->name); |
| 235 | |
David Howells | a18feb5 | 2018-04-04 13:41:27 +0100 | [diff] [blame] | 236 | trace_fscache_osm(object, state, false, oob, event); |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 237 | 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 Howells | e26bfeb | 2017-01-31 09:45:28 +0000 | [diff] [blame] | 241 | if (unlikely(state == STATE(OBJECT_DEAD))) { |
| 242 | _leave(" [dead]"); |
| 243 | return; |
| 244 | } |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 245 | fscache_enqueue_object(object); |
| 246 | event_mask = object->oob_event_mask; |
| 247 | goto unmask_events; |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 248 | } |
| 249 | |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 250 | _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 Howells | e26bfeb | 2017-01-31 09:45:28 +0000 | [diff] [blame] | 255 | if (unlikely(state == STATE(OBJECT_DEAD))) { |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 256 | _leave(" [dead]"); |
| 257 | return; |
| 258 | } |
| 259 | goto restart_masked; |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 260 | } |
| 261 | |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 262 | /* 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 Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 266 | |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 267 | unmask_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 Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | /* |
| 277 | * execute an object |
| 278 | */ |
David Howells | 610be24 | 2013-05-10 19:50:25 +0100 | [diff] [blame] | 279 | static void fscache_object_work_func(struct work_struct *work) |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 280 | { |
| 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 Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 287 | start = jiffies; |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 288 | fscache_object_sm_dispatcher(object); |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 289 | fscache_hist(fscache_objs_histogram, start); |
David Howells | a18feb5 | 2018-04-04 13:41:27 +0100 | [diff] [blame] | 290 | fscache_put_object(object, fscache_obj_put_work); |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 291 | } |
David Howells | 610be24 | 2013-05-10 19:50:25 +0100 | [diff] [blame] | 292 | |
| 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 | */ |
| 304 | void fscache_object_init(struct fscache_object *object, |
| 305 | struct fscache_cookie *cookie, |
| 306 | struct fscache_cache *cache) |
| 307 | { |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 308 | const struct fscache_transition *t; |
| 309 | |
David Howells | 610be24 | 2013-05-10 19:50:25 +0100 | [diff] [blame] | 310 | atomic_inc(&cache->object_count); |
| 311 | |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 312 | object->state = STATE(WAIT_FOR_INIT); |
| 313 | object->oob_table = fscache_osm_init_oob; |
| 314 | object->flags = 1 << FSCACHE_OBJECT_IS_LIVE; |
David Howells | 610be24 | 2013-05-10 19:50:25 +0100 | [diff] [blame] | 315 | 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 Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 324 | object->events = 0; |
David Howells | 610be24 | 2013-05-10 19:50:25 +0100 | [diff] [blame] | 325 | object->store_limit = 0; |
| 326 | object->store_limit_l = 0; |
| 327 | object->cache = cache; |
| 328 | object->cookie = cookie; |
| 329 | object->parent = NULL; |
David Howells | 7026f19 | 2014-02-17 15:01:47 +0000 | [diff] [blame] | 330 | #ifdef CONFIG_FSCACHE_OBJECT_LIST |
| 331 | RB_CLEAR_NODE(&object->objlist_link); |
| 332 | #endif |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 333 | |
| 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 Howells | 610be24 | 2013-05-10 19:50:25 +0100 | [diff] [blame] | 340 | } |
| 341 | EXPORT_SYMBOL(fscache_object_init); |
David Howells | 440f0af | 2009-11-19 18:11:01 +0000 | [diff] [blame] | 342 | |
| 343 | /* |
David Howells | f09b443 | 2015-02-24 10:05:28 +0000 | [diff] [blame] | 344 | * Mark the object as no longer being live, making sure that we synchronise |
| 345 | * against op submission. |
| 346 | */ |
| 347 | static 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 Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 355 | * Abort object initialisation before we start it. |
| 356 | */ |
| 357 | static const struct fscache_state *fscache_abort_initialisation(struct fscache_object *object, |
| 358 | int event) |
| 359 | { |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 360 | _enter("{OBJ%x},%d", object->debug_id, event); |
| 361 | |
| 362 | object->oob_event_mask = 0; |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 363 | fscache_dequeue_object(object); |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 364 | return transit_to(KILL_OBJECT); |
| 365 | } |
| 366 | |
| 367 | /* |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 368 | * 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 Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 373 | */ |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 374 | static const struct fscache_state *fscache_initialise_object(struct fscache_object *object, |
| 375 | int event) |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 376 | { |
| 377 | struct fscache_object *parent; |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 378 | bool success; |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 379 | |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 380 | _enter("{OBJ%x},%d", object->debug_id, event); |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 381 | |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 382 | ASSERT(list_empty(&object->dep_link)); |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 383 | |
| 384 | parent = object->parent; |
| 385 | if (!parent) { |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 386 | _leave(" [no parent]"); |
David Howells | 1362729 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 387 | return transit_to(DROP_OBJECT); |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 388 | } |
| 389 | |
David Howells | 1362729 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 390 | _debug("parent: %s of:%lx", parent->state->name, parent->flags); |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 391 | |
| 392 | if (fscache_object_is_dying(parent)) { |
| 393 | _leave(" [bad parent]"); |
David Howells | 1362729 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 394 | return transit_to(DROP_OBJECT); |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 395 | } |
| 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 Howells | a18feb5 | 2018-04-04 13:41:27 +0100 | [diff] [blame] | 408 | object->cache->ops->grab_object(object, fscache_obj_get_add_to_deps)) { |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 409 | 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 Howells | 1362729 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 416 | return transit_to(DROP_OBJECT); |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 417 | } |
| 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 | */ |
| 429 | static 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 Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 444 | _leave(""); |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 445 | return transit_to(LOOK_UP_OBJECT); |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 446 | } |
| 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 Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 452 | */ |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 453 | static const struct fscache_state *fscache_look_up_object(struct fscache_object *object, |
| 454 | int event) |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 455 | { |
| 456 | struct fscache_cookie *cookie = object->cookie; |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 457 | struct fscache_object *parent = object->parent; |
David Howells | fee096d | 2009-11-19 18:12:05 +0000 | [diff] [blame] | 458 | int ret; |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 459 | |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 460 | _enter("{OBJ%x},%d", object->debug_id, event); |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 461 | |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 462 | object->oob_table = fscache_osm_lookup_oob; |
| 463 | |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 464 | 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 Howells | 493f7bc | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 469 | ASSERT(fscache_object_is_available(parent)); |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 470 | |
David Howells | 493f7bc | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 471 | if (fscache_object_is_dying(parent) || |
David Howells | 1362729 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 472 | test_bit(FSCACHE_IOERROR, &object->cache->flags) || |
| 473 | !fscache_use_cookie(object)) { |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 474 | _leave(" [unavailable]"); |
| 475 | return transit_to(LOOKUP_FAILURE); |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 476 | } |
| 477 | |
David Howells | 1362729 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 478 | _debug("LOOKUP \"%s\" in \"%s\"", |
| 479 | cookie->def->name, object->cache->tag->name); |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 480 | |
| 481 | fscache_stat(&fscache_n_object_lookups); |
David Howells | 52bd75f | 2009-11-19 18:11:08 +0000 | [diff] [blame] | 482 | fscache_stat(&fscache_n_cop_lookup_object); |
David Howells | fee096d | 2009-11-19 18:12:05 +0000 | [diff] [blame] | 483 | ret = object->cache->ops->lookup_object(object); |
David Howells | 52bd75f | 2009-11-19 18:11:08 +0000 | [diff] [blame] | 484 | fscache_stat_d(&fscache_n_cop_lookup_object); |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 485 | |
David Howells | 1362729 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 486 | fscache_unuse_cookie(object); |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 487 | |
David Howells | fee096d | 2009-11-19 18:12:05 +0000 | [diff] [blame] | 488 | 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 Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 492 | _leave(" [timeout]"); |
| 493 | return NO_TRANSIT; |
David Howells | fee096d | 2009-11-19 18:12:05 +0000 | [diff] [blame] | 494 | } |
| 495 | |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 496 | if (ret < 0) { |
| 497 | _leave(" [error]"); |
| 498 | return transit_to(LOOKUP_FAILURE); |
| 499 | } |
| 500 | |
| 501 | _leave(" [ok]"); |
| 502 | return transit_to(OBJECT_AVAILABLE); |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 503 | } |
| 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 | */ |
| 512 | void fscache_object_lookup_negative(struct fscache_object *object) |
| 513 | { |
| 514 | struct fscache_cookie *cookie = object->cookie; |
| 515 | |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 516 | _enter("{OBJ%x,%s}", object->debug_id, object->state->name); |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 517 | |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 518 | if (!test_and_set_bit(FSCACHE_OBJECT_IS_LOOKED_UP, &object->flags)) { |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 519 | fscache_stat(&fscache_n_object_lookups_negative); |
| 520 | |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 521 | /* Allow write requests to begin stacking up and read requests to begin |
| 522 | * returning ENODATA. |
| 523 | */ |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 524 | set_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags); |
David Howells | 94d30ae | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 525 | clear_bit(FSCACHE_COOKIE_UNAVAILABLE, &cookie->flags); |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 526 | |
| 527 | _debug("wake up lookup %p", &cookie->flags); |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 528 | clear_bit_unlock(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags); |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 529 | wake_up_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP); |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 530 | } |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 531 | _leave(""); |
| 532 | } |
| 533 | EXPORT_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 | */ |
| 545 | void fscache_obtained_object(struct fscache_object *object) |
| 546 | { |
| 547 | struct fscache_cookie *cookie = object->cookie; |
| 548 | |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 549 | _enter("{OBJ%x,%s}", object->debug_id, object->state->name); |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 550 | |
| 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 Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 553 | if (!test_and_set_bit(FSCACHE_OBJECT_IS_LOOKED_UP, &object->flags)) { |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 554 | fscache_stat(&fscache_n_object_lookups_positive); |
| 555 | |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 556 | /* We do (presumably) have data */ |
| 557 | clear_bit_unlock(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags); |
David Howells | 94d30ae | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 558 | clear_bit(FSCACHE_COOKIE_UNAVAILABLE, &cookie->flags); |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 559 | |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 560 | /* 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 Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 564 | wake_up_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP); |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 565 | } else { |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 566 | fscache_stat(&fscache_n_object_created); |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 567 | } |
| 568 | |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 569 | set_bit(FSCACHE_OBJECT_IS_AVAILABLE, &object->flags); |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 570 | _leave(""); |
| 571 | } |
| 572 | EXPORT_SYMBOL(fscache_obtained_object); |
| 573 | |
| 574 | /* |
| 575 | * handle an object that has just become available |
| 576 | */ |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 577 | static const struct fscache_state *fscache_object_available(struct fscache_object *object, |
| 578 | int event) |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 579 | { |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 580 | _enter("{OBJ%x},%d", object->debug_id, event); |
| 581 | |
| 582 | object->oob_table = fscache_osm_run_oob; |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 583 | |
| 584 | spin_lock(&object->lock); |
| 585 | |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 586 | 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 Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 590 | fscache_start_operations(object); |
| 591 | } else { |
| 592 | ASSERT(list_empty(&object->pending_ops)); |
| 593 | } |
| 594 | } |
| 595 | spin_unlock(&object->lock); |
| 596 | |
David Howells | 52bd75f | 2009-11-19 18:11:08 +0000 | [diff] [blame] | 597 | fscache_stat(&fscache_n_cop_lookup_complete); |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 598 | object->cache->ops->lookup_complete(object); |
David Howells | 52bd75f | 2009-11-19 18:11:08 +0000 | [diff] [blame] | 599 | fscache_stat_d(&fscache_n_cop_lookup_complete); |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 600 | |
| 601 | fscache_hist(fscache_obj_instantiate_histogram, object->lookup_jif); |
| 602 | fscache_stat(&fscache_n_object_avail); |
| 603 | |
| 604 | _leave(""); |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 605 | return transit_to(JUMPSTART_DEPS); |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | /* |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 609 | * Wake up this object's dependent objects now that we've become available. |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 610 | */ |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 611 | static 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 | */ |
| 624 | static const struct fscache_state *fscache_lookup_failure(struct fscache_object *object, |
| 625 | int event) |
| 626 | { |
| 627 | struct fscache_cookie *cookie; |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 628 | |
| 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 Howells | 6515d1d | 2015-02-25 11:53:57 +0000 | [diff] [blame] | 637 | set_bit(FSCACHE_OBJECT_KILLED_BY_CACHE, &object->flags); |
| 638 | |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 639 | cookie = object->cookie; |
| 640 | set_bit(FSCACHE_COOKIE_UNAVAILABLE, &cookie->flags); |
David Howells | 1362729 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 641 | if (test_and_clear_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags)) |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 642 | wake_up_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP); |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 643 | |
| 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 | */ |
| 652 | static 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 Howells | f09b443 | 2015-02-24 10:05:28 +0000 | [diff] [blame] | 658 | fscache_mark_object_dead(object); |
David Howells | 1362729 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 659 | object->oob_event_mask = 0; |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 660 | |
David Howells | 6bdded5 | 2017-01-18 14:29:25 +0000 | [diff] [blame] | 661 | 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 Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 667 | if (list_empty(&object->dependents) && |
| 668 | object->n_ops == 0 && |
| 669 | object->n_children == 0) |
David Howells | 1362729 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 670 | return transit_to(DROP_OBJECT); |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 671 | |
David Howells | 1362729 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 672 | 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 Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 678 | |
| 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 | */ |
| 688 | static 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 Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 699 | * Drop an object's attachments |
| 700 | */ |
| 701 | static const struct fscache_state *fscache_drop_object(struct fscache_object *object, |
| 702 | int event) |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 703 | { |
| 704 | struct fscache_object *parent = object->parent; |
David Howells | 1362729 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 705 | struct fscache_cookie *cookie = object->cookie; |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 706 | struct fscache_cache *cache = object->cache; |
David Howells | 1362729 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 707 | bool awaken = false; |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 708 | |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 709 | _enter("{OBJ%x,%d},%d", object->debug_id, object->n_children, event); |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 710 | |
David Howells | 1362729 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 711 | 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 Howells | 94d30ae | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 719 | if (hlist_empty(&cookie->backing_objects) && |
| 720 | test_and_clear_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags)) |
David Howells | 1362729 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 721 | awaken = true; |
| 722 | spin_unlock(&cookie->lock); |
| 723 | |
| 724 | if (awaken) |
| 725 | wake_up_bit(&cookie->flags, FSCACHE_COOKIE_INVALIDATING); |
David Howells | 6897e3d | 2009-11-19 18:11:22 +0000 | [diff] [blame] | 726 | |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 727 | /* Prevent a race with our last child, which has to signal EV_CLEARED |
| 728 | * before dropping our spinlock. |
| 729 | */ |
| 730 | spin_lock(&object->lock); |
| 731 | spin_unlock(&object->lock); |
| 732 | |
| 733 | /* Discard from the cache's collection of objects */ |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 734 | spin_lock(&cache->object_list_lock); |
| 735 | list_del_init(&object->cache_link); |
| 736 | spin_unlock(&cache->object_list_lock); |
| 737 | |
David Howells | 52bd75f | 2009-11-19 18:11:08 +0000 | [diff] [blame] | 738 | fscache_stat(&fscache_n_cop_drop_object); |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 739 | cache->ops->drop_object(object); |
David Howells | 52bd75f | 2009-11-19 18:11:08 +0000 | [diff] [blame] | 740 | fscache_stat_d(&fscache_n_cop_drop_object); |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 741 | |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 742 | /* The parent object wants to know when all it dependents have gone */ |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 743 | if (parent) { |
| 744 | _debug("release parent OBJ%x {%d}", |
| 745 | parent->debug_id, parent->n_children); |
| 746 | |
| 747 | spin_lock(&parent->lock); |
| 748 | parent->n_children--; |
| 749 | if (parent->n_children == 0) |
| 750 | fscache_raise_event(parent, FSCACHE_OBJECT_EV_CLEARED); |
| 751 | spin_unlock(&parent->lock); |
| 752 | object->parent = NULL; |
| 753 | } |
| 754 | |
Tejun Heo | 8b8edef | 2010-07-20 22:09:01 +0200 | [diff] [blame] | 755 | /* this just shifts the object release to the work processor */ |
David Howells | a18feb5 | 2018-04-04 13:41:27 +0100 | [diff] [blame] | 756 | fscache_put_object(object, fscache_obj_put_drop_obj); |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 757 | fscache_stat(&fscache_n_object_dead); |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 758 | |
| 759 | _leave(""); |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 760 | return transit_to(OBJECT_DEAD); |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 761 | } |
| 762 | |
| 763 | /* |
Tejun Heo | 8b8edef | 2010-07-20 22:09:01 +0200 | [diff] [blame] | 764 | * get a ref on an object |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 765 | */ |
David Howells | a18feb5 | 2018-04-04 13:41:27 +0100 | [diff] [blame] | 766 | static int fscache_get_object(struct fscache_object *object, |
| 767 | enum fscache_obj_ref_trace why) |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 768 | { |
David Howells | 52bd75f | 2009-11-19 18:11:08 +0000 | [diff] [blame] | 769 | int ret; |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 770 | |
David Howells | 52bd75f | 2009-11-19 18:11:08 +0000 | [diff] [blame] | 771 | fscache_stat(&fscache_n_cop_grab_object); |
David Howells | a18feb5 | 2018-04-04 13:41:27 +0100 | [diff] [blame] | 772 | ret = object->cache->ops->grab_object(object, why) ? 0 : -EAGAIN; |
David Howells | 52bd75f | 2009-11-19 18:11:08 +0000 | [diff] [blame] | 773 | fscache_stat_d(&fscache_n_cop_grab_object); |
| 774 | return ret; |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | /* |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 778 | * Discard a ref on an object |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 779 | */ |
David Howells | a18feb5 | 2018-04-04 13:41:27 +0100 | [diff] [blame] | 780 | static void fscache_put_object(struct fscache_object *object, |
| 781 | enum fscache_obj_ref_trace why) |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 782 | { |
David Howells | 52bd75f | 2009-11-19 18:11:08 +0000 | [diff] [blame] | 783 | fscache_stat(&fscache_n_cop_put_object); |
David Howells | a18feb5 | 2018-04-04 13:41:27 +0100 | [diff] [blame] | 784 | object->cache->ops->put_object(object, why); |
David Howells | 52bd75f | 2009-11-19 18:11:08 +0000 | [diff] [blame] | 785 | fscache_stat_d(&fscache_n_cop_put_object); |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 786 | } |
| 787 | |
David Howells | 1362729 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 788 | /** |
| 789 | * fscache_object_destroy - Note that a cache object is about to be destroyed |
| 790 | * @object: The object to be destroyed |
| 791 | * |
| 792 | * Note the imminent destruction and deallocation of a cache object record. |
| 793 | */ |
| 794 | void fscache_object_destroy(struct fscache_object *object) |
| 795 | { |
| 796 | fscache_objlist_remove(object); |
| 797 | |
| 798 | /* We can get rid of the cookie now */ |
David Howells | a18feb5 | 2018-04-04 13:41:27 +0100 | [diff] [blame] | 799 | fscache_cookie_put(object->cookie, fscache_cookie_put_object); |
David Howells | 1362729 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 800 | object->cookie = NULL; |
| 801 | } |
| 802 | EXPORT_SYMBOL(fscache_object_destroy); |
| 803 | |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 804 | /* |
| 805 | * enqueue an object for metadata-type processing |
| 806 | */ |
| 807 | void fscache_enqueue_object(struct fscache_object *object) |
| 808 | { |
| 809 | _enter("{OBJ%x}", object->debug_id); |
| 810 | |
David Howells | a18feb5 | 2018-04-04 13:41:27 +0100 | [diff] [blame] | 811 | if (fscache_get_object(object, fscache_obj_get_queue) >= 0) { |
Tejun Heo | 8b8edef | 2010-07-20 22:09:01 +0200 | [diff] [blame] | 812 | 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 Howells | a18feb5 | 2018-04-04 13:41:27 +0100 | [diff] [blame] | 819 | fscache_put_object(object, fscache_obj_put_queue); |
Tejun Heo | 8b8edef | 2010-07-20 22:09:01 +0200 | [diff] [blame] | 820 | |
| 821 | put_cpu_var(fscache_object_cong_wait); |
| 822 | } |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 823 | } |
| 824 | |
Tejun Heo | 8b8edef | 2010-07-20 22:09:01 +0200 | [diff] [blame] | 825 | /** |
| 826 | * fscache_object_sleep_till_congested - Sleep until object wq is congested |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 827 | * @timeoutp: Scheduler sleep timeout |
Tejun Heo | 8b8edef | 2010-07-20 22:09:01 +0200 | [diff] [blame] | 828 | * |
| 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 | */ |
| 837 | bool fscache_object_sleep_till_congested(signed long *timeoutp) |
| 838 | { |
Christoph Lameter | 170d800 | 2013-10-15 12:22:29 -0600 | [diff] [blame] | 839 | wait_queue_head_t *cong_wq = this_cpu_ptr(&fscache_object_cong_wait); |
Tejun Heo | 8b8edef | 2010-07-20 22:09:01 +0200 | [diff] [blame] | 840 | 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 | } |
| 852 | EXPORT_SYMBOL_GPL(fscache_object_sleep_till_congested); |
| 853 | |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 854 | /* |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 855 | * 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 Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 860 | */ |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 861 | static bool fscache_enqueue_dependents(struct fscache_object *object, int event) |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 862 | { |
| 863 | struct fscache_object *dep; |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 864 | bool ret = true; |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 865 | |
| 866 | _enter("{OBJ%x}", object->debug_id); |
| 867 | |
| 868 | if (list_empty(&object->dependents)) |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 869 | return true; |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 870 | |
| 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 Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 878 | fscache_raise_event(dep, event); |
David Howells | a18feb5 | 2018-04-04 13:41:27 +0100 | [diff] [blame] | 879 | fscache_put_object(dep, fscache_obj_put_enq_dep); |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 880 | |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 881 | if (!list_empty(&object->dependents) && need_resched()) { |
| 882 | ret = false; |
| 883 | break; |
| 884 | } |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | spin_unlock(&object->lock); |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 888 | return ret; |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 889 | } |
| 890 | |
| 891 | /* |
| 892 | * remove an object from whatever queue it's waiting on |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 893 | */ |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 894 | static void fscache_dequeue_object(struct fscache_object *object) |
David Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 895 | { |
| 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 Howells | 1362729 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 913 | * 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 Howells | 36c95590 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 917 | */ |
| 918 | enum 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 | } |
| 952 | EXPORT_SYMBOL(fscache_check_aux); |
David Howells | ef778e7 | 2012-12-20 21:52:36 +0000 | [diff] [blame] | 953 | |
| 954 | /* |
| 955 | * Asynchronously invalidate an object. |
| 956 | */ |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 957 | static const struct fscache_state *_fscache_invalidate_object(struct fscache_object *object, |
| 958 | int event) |
David Howells | ef778e7 | 2012-12-20 21:52:36 +0000 | [diff] [blame] | 959 | { |
| 960 | struct fscache_operation *op; |
| 961 | struct fscache_cookie *cookie = object->cookie; |
| 962 | |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 963 | _enter("{OBJ%x},%d", object->debug_id, event); |
| 964 | |
David Howells | 1362729 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 965 | /* 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 Howells | 94d30ae | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 970 | set_bit(FSCACHE_OBJECT_RETIRED, &object->flags); |
David Howells | 1362729 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 971 | _leave(" [no cookie]"); |
| 972 | return transit_to(KILL_OBJECT); |
| 973 | } |
David Howells | ef778e7 | 2012-12-20 21:52:36 +0000 | [diff] [blame] | 974 | |
| 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 Howells | 1362729 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 982 | if (!op) |
| 983 | goto nomem; |
David Howells | ef778e7 | 2012-12-20 21:52:36 +0000 | [diff] [blame] | 984 | |
David Howells | 08c2e3d | 2018-04-04 13:41:27 +0100 | [diff] [blame^] | 985 | fscache_operation_init(cookie, op, object->cache->ops->invalidate_object, |
David Howells | d3b97ca | 2015-02-24 10:05:29 +0000 | [diff] [blame] | 986 | NULL, NULL); |
David Howells | 1362729 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 987 | op->flags = FSCACHE_OP_ASYNC | |
| 988 | (1 << FSCACHE_OP_EXCLUSIVE) | |
| 989 | (1 << FSCACHE_OP_UNUSE_COOKIE); |
David Howells | 08c2e3d | 2018-04-04 13:41:27 +0100 | [diff] [blame^] | 990 | trace_fscache_page_op(cookie, NULL, op, fscache_page_op_invalidate); |
David Howells | ef778e7 | 2012-12-20 21:52:36 +0000 | [diff] [blame] | 991 | |
| 992 | spin_lock(&cookie->lock); |
| 993 | if (fscache_submit_exclusive_op(object, op) < 0) |
David Howells | 8d76349 | 2012-12-05 13:34:48 +0000 | [diff] [blame] | 994 | goto submit_op_failed; |
David Howells | ef778e7 | 2012-12-20 21:52:36 +0000 | [diff] [blame] | 995 | 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 Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 1007 | 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 Howells | 8d76349 | 2012-12-05 13:34:48 +0000 | [diff] [blame] | 1011 | |
David Howells | 1362729 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 1012 | nomem: |
David Howells | f09b443 | 2015-02-24 10:05:28 +0000 | [diff] [blame] | 1013 | fscache_mark_object_dead(object); |
David Howells | 1362729 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 1014 | fscache_unuse_cookie(object); |
| 1015 | _leave(" [ENOMEM]"); |
| 1016 | return transit_to(KILL_OBJECT); |
| 1017 | |
David Howells | 8d76349 | 2012-12-05 13:34:48 +0000 | [diff] [blame] | 1018 | submit_op_failed: |
David Howells | f09b443 | 2015-02-24 10:05:28 +0000 | [diff] [blame] | 1019 | fscache_mark_object_dead(object); |
David Howells | 8d76349 | 2012-12-05 13:34:48 +0000 | [diff] [blame] | 1020 | spin_unlock(&cookie->lock); |
Milosz Tanski | 920bce2 | 2014-08-13 12:58:21 -0400 | [diff] [blame] | 1021 | fscache_unuse_cookie(object); |
David Howells | 8d76349 | 2012-12-05 13:34:48 +0000 | [diff] [blame] | 1022 | kfree(op); |
David Howells | 8d76349 | 2012-12-05 13:34:48 +0000 | [diff] [blame] | 1023 | _leave(" [EIO]"); |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 1024 | return transit_to(KILL_OBJECT); |
| 1025 | } |
| 1026 | |
| 1027 | static 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 | */ |
| 1042 | static 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 Howells | ef778e7 | 2012-12-20 21:52:36 +0000 | [diff] [blame] | 1054 | } |
David Howells | 182d919 | 2015-02-19 23:47:31 +0000 | [diff] [blame] | 1055 | |
| 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 | */ |
| 1063 | void fscache_object_retrying_stale(struct fscache_object *object) |
| 1064 | { |
| 1065 | fscache_stat(&fscache_n_cache_no_space_reject); |
| 1066 | } |
| 1067 | EXPORT_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 | */ |
| 1077 | void 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 | } |
| 1101 | EXPORT_SYMBOL(fscache_object_mark_killed); |
David Howells | e26bfeb | 2017-01-31 09:45:28 +0000 | [diff] [blame] | 1102 | |
| 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 | */ |
| 1109 | static 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 | } |