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