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