David Howells | 952efe7 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 1 | /* FS-Cache worker operation management routines |
| 2 | * |
| 3 | * Copyright (C) 2008 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/operations.txt |
| 12 | */ |
| 13 | |
| 14 | #define FSCACHE_DEBUG_LEVEL OPERATION |
| 15 | #include <linux/module.h> |
David Howells | 440f0af | 2009-11-19 18:11:01 +0000 | [diff] [blame] | 16 | #include <linux/seq_file.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 17 | #include <linux/slab.h> |
David Howells | 952efe7 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 18 | #include "internal.h" |
| 19 | |
| 20 | atomic_t fscache_op_debug_id; |
| 21 | EXPORT_SYMBOL(fscache_op_debug_id); |
| 22 | |
| 23 | /** |
| 24 | * fscache_enqueue_operation - Enqueue an operation for processing |
| 25 | * @op: The operation to enqueue |
| 26 | * |
| 27 | * Enqueue an operation for processing by the FS-Cache thread pool. |
| 28 | * |
| 29 | * This will get its own ref on the object. |
| 30 | */ |
| 31 | void fscache_enqueue_operation(struct fscache_operation *op) |
| 32 | { |
| 33 | _enter("{OBJ%x OP%x,%u}", |
| 34 | op->object->debug_id, op->debug_id, atomic_read(&op->usage)); |
| 35 | |
David Howells | 5753c44 | 2009-11-19 18:11:19 +0000 | [diff] [blame] | 36 | ASSERT(list_empty(&op->pend_link)); |
David Howells | 952efe7 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 37 | ASSERT(op->processor != NULL); |
David Howells | 493f7bc | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 38 | ASSERT(fscache_object_is_available(op->object)); |
David Howells | 952efe7 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 39 | ASSERTCMP(atomic_read(&op->usage), >, 0); |
David Howells | 9f10523 | 2012-12-20 21:52:35 +0000 | [diff] [blame] | 40 | ASSERTCMP(op->state, ==, FSCACHE_OP_ST_IN_PROGRESS); |
David Howells | 952efe7 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 41 | |
David Howells | 5753c44 | 2009-11-19 18:11:19 +0000 | [diff] [blame] | 42 | fscache_stat(&fscache_n_op_enqueue); |
| 43 | switch (op->flags & FSCACHE_OP_TYPE) { |
Tejun Heo | 8af7c12 | 2010-07-20 22:09:01 +0200 | [diff] [blame] | 44 | case FSCACHE_OP_ASYNC: |
| 45 | _debug("queue async"); |
David Howells | 5753c44 | 2009-11-19 18:11:19 +0000 | [diff] [blame] | 46 | atomic_inc(&op->usage); |
Tejun Heo | 8af7c12 | 2010-07-20 22:09:01 +0200 | [diff] [blame] | 47 | if (!queue_work(fscache_op_wq, &op->work)) |
David Howells | 5753c44 | 2009-11-19 18:11:19 +0000 | [diff] [blame] | 48 | fscache_put_operation(op); |
| 49 | break; |
David Howells | 5753c44 | 2009-11-19 18:11:19 +0000 | [diff] [blame] | 50 | case FSCACHE_OP_MYTHREAD: |
| 51 | _debug("queue for caller's attention"); |
| 52 | break; |
| 53 | default: |
Fabian Frederick | 36dfd11 | 2014-06-04 16:05:38 -0700 | [diff] [blame] | 54 | pr_err("Unexpected op type %lx", op->flags); |
David Howells | 5753c44 | 2009-11-19 18:11:19 +0000 | [diff] [blame] | 55 | BUG(); |
| 56 | break; |
David Howells | 952efe7 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | EXPORT_SYMBOL(fscache_enqueue_operation); |
| 60 | |
| 61 | /* |
| 62 | * start an op running |
| 63 | */ |
| 64 | static void fscache_run_op(struct fscache_object *object, |
| 65 | struct fscache_operation *op) |
| 66 | { |
David Howells | 9f10523 | 2012-12-20 21:52:35 +0000 | [diff] [blame] | 67 | ASSERTCMP(op->state, ==, FSCACHE_OP_ST_PENDING); |
| 68 | |
| 69 | op->state = FSCACHE_OP_ST_IN_PROGRESS; |
David Howells | 952efe7 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 70 | object->n_in_progress++; |
| 71 | if (test_and_clear_bit(FSCACHE_OP_WAITING, &op->flags)) |
| 72 | wake_up_bit(&op->flags, FSCACHE_OP_WAITING); |
| 73 | if (op->processor) |
| 74 | fscache_enqueue_operation(op); |
| 75 | fscache_stat(&fscache_n_op_run); |
| 76 | } |
| 77 | |
| 78 | /* |
David Howells | 3c30598 | 2015-02-24 10:39:28 +0000 | [diff] [blame] | 79 | * report an unexpected submission |
| 80 | */ |
| 81 | static void fscache_report_unexpected_submission(struct fscache_object *object, |
| 82 | struct fscache_operation *op, |
| 83 | const struct fscache_state *ostate) |
| 84 | { |
| 85 | static bool once_only; |
| 86 | struct fscache_operation *p; |
| 87 | unsigned n; |
| 88 | |
| 89 | if (once_only) |
| 90 | return; |
| 91 | once_only = true; |
| 92 | |
| 93 | kdebug("unexpected submission OP%x [OBJ%x %s]", |
| 94 | op->debug_id, object->debug_id, object->state->name); |
| 95 | kdebug("objstate=%s [%s]", object->state->name, ostate->name); |
| 96 | kdebug("objflags=%lx", object->flags); |
| 97 | kdebug("objevent=%lx [%lx]", object->events, object->event_mask); |
| 98 | kdebug("ops=%u inp=%u exc=%u", |
| 99 | object->n_ops, object->n_in_progress, object->n_exclusive); |
| 100 | |
| 101 | if (!list_empty(&object->pending_ops)) { |
| 102 | n = 0; |
| 103 | list_for_each_entry(p, &object->pending_ops, pend_link) { |
| 104 | ASSERTCMP(p->object, ==, object); |
| 105 | kdebug("%p %p", op->processor, op->release); |
| 106 | n++; |
| 107 | } |
| 108 | |
| 109 | kdebug("n=%u", n); |
| 110 | } |
| 111 | |
| 112 | dump_stack(); |
| 113 | } |
| 114 | |
| 115 | /* |
David Howells | 952efe7 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 116 | * submit an exclusive operation for an object |
| 117 | * - other ops are excluded from running simultaneously with this one |
| 118 | * - this gets any extra refs it needs on an op |
| 119 | */ |
| 120 | int fscache_submit_exclusive_op(struct fscache_object *object, |
| 121 | struct fscache_operation *op) |
| 122 | { |
David Howells | 30ceec6 | 2015-02-24 10:05:27 +0000 | [diff] [blame^] | 123 | const struct fscache_state *ostate; |
| 124 | unsigned long flags; |
David Howells | 8d76349 | 2012-12-05 13:34:48 +0000 | [diff] [blame] | 125 | int ret; |
| 126 | |
David Howells | 952efe7 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 127 | _enter("{OBJ%x OP%x},", object->debug_id, op->debug_id); |
| 128 | |
David Howells | 9f10523 | 2012-12-20 21:52:35 +0000 | [diff] [blame] | 129 | ASSERTCMP(op->state, ==, FSCACHE_OP_ST_INITIALISED); |
| 130 | ASSERTCMP(atomic_read(&op->usage), >, 0); |
| 131 | |
David Howells | 952efe7 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 132 | spin_lock(&object->lock); |
| 133 | ASSERTCMP(object->n_ops, >=, object->n_in_progress); |
| 134 | ASSERTCMP(object->n_ops, >=, object->n_exclusive); |
David Howells | 5753c44 | 2009-11-19 18:11:19 +0000 | [diff] [blame] | 135 | ASSERT(list_empty(&op->pend_link)); |
David Howells | 952efe7 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 136 | |
David Howells | 30ceec6 | 2015-02-24 10:05:27 +0000 | [diff] [blame^] | 137 | ostate = object->state; |
| 138 | smp_rmb(); |
| 139 | |
David Howells | 9f10523 | 2012-12-20 21:52:35 +0000 | [diff] [blame] | 140 | op->state = FSCACHE_OP_ST_PENDING; |
David Howells | 30ceec6 | 2015-02-24 10:05:27 +0000 | [diff] [blame^] | 141 | flags = READ_ONCE(object->flags); |
| 142 | if (unlikely(!(flags & BIT(FSCACHE_OBJECT_IS_LIVE)))) { |
| 143 | fscache_stat(&fscache_n_op_rejected); |
| 144 | op->state = FSCACHE_OP_ST_CANCELLED; |
| 145 | ret = -ENOBUFS; |
| 146 | } else if (unlikely(fscache_cache_is_broken(object))) { |
| 147 | op->state = FSCACHE_OP_ST_CANCELLED; |
| 148 | ret = -EIO; |
| 149 | } else if (flags & BIT(FSCACHE_OBJECT_IS_AVAILABLE)) { |
David Howells | 952efe7 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 150 | op->object = object; |
| 151 | object->n_ops++; |
| 152 | object->n_exclusive++; /* reads and writes must wait */ |
| 153 | |
David Howells | 9f10523 | 2012-12-20 21:52:35 +0000 | [diff] [blame] | 154 | if (object->n_in_progress > 0) { |
David Howells | 952efe7 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 155 | atomic_inc(&op->usage); |
| 156 | list_add_tail(&op->pend_link, &object->pending_ops); |
| 157 | fscache_stat(&fscache_n_op_pend); |
| 158 | } else if (!list_empty(&object->pending_ops)) { |
| 159 | atomic_inc(&op->usage); |
| 160 | list_add_tail(&op->pend_link, &object->pending_ops); |
| 161 | fscache_stat(&fscache_n_op_pend); |
| 162 | fscache_start_operations(object); |
| 163 | } else { |
| 164 | ASSERTCMP(object->n_in_progress, ==, 0); |
| 165 | fscache_run_op(object, op); |
| 166 | } |
| 167 | |
| 168 | /* need to issue a new write op after this */ |
| 169 | clear_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags); |
David Howells | 8d76349 | 2012-12-05 13:34:48 +0000 | [diff] [blame] | 170 | ret = 0; |
David Howells | 30ceec6 | 2015-02-24 10:05:27 +0000 | [diff] [blame^] | 171 | } else if (flags & BIT(FSCACHE_OBJECT_IS_LOOKED_UP)) { |
David Howells | 952efe7 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 172 | op->object = object; |
| 173 | object->n_ops++; |
| 174 | object->n_exclusive++; /* reads and writes must wait */ |
| 175 | atomic_inc(&op->usage); |
| 176 | list_add_tail(&op->pend_link, &object->pending_ops); |
| 177 | fscache_stat(&fscache_n_op_pend); |
David Howells | 8d76349 | 2012-12-05 13:34:48 +0000 | [diff] [blame] | 178 | ret = 0; |
David Howells | 952efe7 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 179 | } else { |
David Howells | 30ceec6 | 2015-02-24 10:05:27 +0000 | [diff] [blame^] | 180 | fscache_report_unexpected_submission(object, op, ostate); |
| 181 | op->state = FSCACHE_OP_ST_CANCELLED; |
| 182 | ret = -ENOBUFS; |
David Howells | 952efe7 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | spin_unlock(&object->lock); |
David Howells | 8d76349 | 2012-12-05 13:34:48 +0000 | [diff] [blame] | 186 | return ret; |
David Howells | 952efe7 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | /* |
David Howells | 952efe7 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 190 | * submit an operation for an object |
| 191 | * - objects may be submitted only in the following states: |
| 192 | * - during object creation (write ops may be submitted) |
| 193 | * - whilst the object is active |
| 194 | * - after an I/O error incurred in one of the two above states (op rejected) |
| 195 | * - this gets any extra refs it needs on an op |
| 196 | */ |
| 197 | int fscache_submit_op(struct fscache_object *object, |
| 198 | struct fscache_operation *op) |
| 199 | { |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 200 | const struct fscache_state *ostate; |
David Howells | 30ceec6 | 2015-02-24 10:05:27 +0000 | [diff] [blame^] | 201 | unsigned long flags; |
David Howells | 952efe7 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 202 | int ret; |
| 203 | |
| 204 | _enter("{OBJ%x OP%x},{%u}", |
| 205 | object->debug_id, op->debug_id, atomic_read(&op->usage)); |
| 206 | |
David Howells | 9f10523 | 2012-12-20 21:52:35 +0000 | [diff] [blame] | 207 | ASSERTCMP(op->state, ==, FSCACHE_OP_ST_INITIALISED); |
David Howells | 952efe7 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 208 | ASSERTCMP(atomic_read(&op->usage), >, 0); |
| 209 | |
| 210 | spin_lock(&object->lock); |
| 211 | ASSERTCMP(object->n_ops, >=, object->n_in_progress); |
| 212 | ASSERTCMP(object->n_ops, >=, object->n_exclusive); |
David Howells | 5753c44 | 2009-11-19 18:11:19 +0000 | [diff] [blame] | 213 | ASSERT(list_empty(&op->pend_link)); |
David Howells | 952efe7 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 214 | |
| 215 | ostate = object->state; |
| 216 | smp_rmb(); |
| 217 | |
David Howells | 9f10523 | 2012-12-20 21:52:35 +0000 | [diff] [blame] | 218 | op->state = FSCACHE_OP_ST_PENDING; |
David Howells | 30ceec6 | 2015-02-24 10:05:27 +0000 | [diff] [blame^] | 219 | flags = READ_ONCE(object->flags); |
| 220 | if (unlikely(!(flags & BIT(FSCACHE_OBJECT_IS_LIVE)))) { |
| 221 | fscache_stat(&fscache_n_op_rejected); |
| 222 | op->state = FSCACHE_OP_ST_CANCELLED; |
| 223 | ret = -ENOBUFS; |
| 224 | } else if (unlikely(fscache_cache_is_broken(object))) { |
| 225 | op->state = FSCACHE_OP_ST_CANCELLED; |
| 226 | ret = -EIO; |
| 227 | } else if (flags & BIT(FSCACHE_OBJECT_IS_AVAILABLE)) { |
David Howells | 952efe7 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 228 | op->object = object; |
| 229 | object->n_ops++; |
| 230 | |
| 231 | if (object->n_exclusive > 0) { |
| 232 | atomic_inc(&op->usage); |
| 233 | list_add_tail(&op->pend_link, &object->pending_ops); |
| 234 | fscache_stat(&fscache_n_op_pend); |
| 235 | } else if (!list_empty(&object->pending_ops)) { |
| 236 | atomic_inc(&op->usage); |
| 237 | list_add_tail(&op->pend_link, &object->pending_ops); |
| 238 | fscache_stat(&fscache_n_op_pend); |
| 239 | fscache_start_operations(object); |
| 240 | } else { |
| 241 | ASSERTCMP(object->n_exclusive, ==, 0); |
| 242 | fscache_run_op(object, op); |
| 243 | } |
| 244 | ret = 0; |
David Howells | 30ceec6 | 2015-02-24 10:05:27 +0000 | [diff] [blame^] | 245 | } else if (flags & BIT(FSCACHE_OBJECT_IS_LOOKED_UP)) { |
David Howells | 952efe7 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 246 | op->object = object; |
| 247 | object->n_ops++; |
| 248 | atomic_inc(&op->usage); |
| 249 | list_add_tail(&op->pend_link, &object->pending_ops); |
| 250 | fscache_stat(&fscache_n_op_pend); |
| 251 | ret = 0; |
David Howells | 30ceec6 | 2015-02-24 10:05:27 +0000 | [diff] [blame^] | 252 | } else { |
David Howells | 952efe7 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 253 | fscache_report_unexpected_submission(object, op, ostate); |
| 254 | ASSERT(!fscache_object_is_active(object)); |
David Howells | 9f10523 | 2012-12-20 21:52:35 +0000 | [diff] [blame] | 255 | op->state = FSCACHE_OP_ST_CANCELLED; |
David Howells | 952efe7 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 256 | ret = -ENOBUFS; |
David Howells | 952efe7 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | spin_unlock(&object->lock); |
| 260 | return ret; |
| 261 | } |
| 262 | |
| 263 | /* |
| 264 | * queue an object for withdrawal on error, aborting all following asynchronous |
| 265 | * operations |
| 266 | */ |
| 267 | void fscache_abort_object(struct fscache_object *object) |
| 268 | { |
| 269 | _enter("{OBJ%x}", object->debug_id); |
| 270 | |
| 271 | fscache_raise_event(object, FSCACHE_OBJECT_EV_ERROR); |
| 272 | } |
| 273 | |
| 274 | /* |
David Howells | dcfae32 | 2013-05-24 12:45:31 +0100 | [diff] [blame] | 275 | * Jump start the operation processing on an object. The caller must hold |
| 276 | * object->lock. |
David Howells | 952efe7 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 277 | */ |
| 278 | void fscache_start_operations(struct fscache_object *object) |
| 279 | { |
| 280 | struct fscache_operation *op; |
| 281 | bool stop = false; |
| 282 | |
| 283 | while (!list_empty(&object->pending_ops) && !stop) { |
| 284 | op = list_entry(object->pending_ops.next, |
| 285 | struct fscache_operation, pend_link); |
| 286 | |
| 287 | if (test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags)) { |
| 288 | if (object->n_in_progress > 0) |
| 289 | break; |
| 290 | stop = true; |
| 291 | } |
| 292 | list_del_init(&op->pend_link); |
David Howells | 5753c44 | 2009-11-19 18:11:19 +0000 | [diff] [blame] | 293 | fscache_run_op(object, op); |
David Howells | 952efe7 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 294 | |
| 295 | /* the pending queue was holding a ref on the object */ |
| 296 | fscache_put_operation(op); |
| 297 | } |
| 298 | |
| 299 | ASSERTCMP(object->n_in_progress, <=, object->n_ops); |
| 300 | |
| 301 | _debug("woke %d ops on OBJ%x", |
| 302 | object->n_in_progress, object->debug_id); |
| 303 | } |
| 304 | |
| 305 | /* |
David Howells | 5753c44 | 2009-11-19 18:11:19 +0000 | [diff] [blame] | 306 | * cancel an operation that's pending on an object |
| 307 | */ |
David Howells | 91c7fbb | 2012-12-14 11:02:22 +0000 | [diff] [blame] | 308 | int fscache_cancel_op(struct fscache_operation *op, |
| 309 | void (*do_cancel)(struct fscache_operation *)) |
David Howells | 5753c44 | 2009-11-19 18:11:19 +0000 | [diff] [blame] | 310 | { |
| 311 | struct fscache_object *object = op->object; |
| 312 | int ret; |
| 313 | |
| 314 | _enter("OBJ%x OP%x}", op->object->debug_id, op->debug_id); |
| 315 | |
David Howells | 9f10523 | 2012-12-20 21:52:35 +0000 | [diff] [blame] | 316 | ASSERTCMP(op->state, >=, FSCACHE_OP_ST_PENDING); |
| 317 | ASSERTCMP(op->state, !=, FSCACHE_OP_ST_CANCELLED); |
| 318 | ASSERTCMP(atomic_read(&op->usage), >, 0); |
| 319 | |
David Howells | 5753c44 | 2009-11-19 18:11:19 +0000 | [diff] [blame] | 320 | spin_lock(&object->lock); |
| 321 | |
| 322 | ret = -EBUSY; |
David Howells | 9f10523 | 2012-12-20 21:52:35 +0000 | [diff] [blame] | 323 | if (op->state == FSCACHE_OP_ST_PENDING) { |
| 324 | ASSERT(!list_empty(&op->pend_link)); |
David Howells | 5753c44 | 2009-11-19 18:11:19 +0000 | [diff] [blame] | 325 | fscache_stat(&fscache_n_op_cancelled); |
| 326 | list_del_init(&op->pend_link); |
David Howells | 91c7fbb | 2012-12-14 11:02:22 +0000 | [diff] [blame] | 327 | if (do_cancel) |
| 328 | do_cancel(op); |
David Howells | 9f10523 | 2012-12-20 21:52:35 +0000 | [diff] [blame] | 329 | op->state = FSCACHE_OP_ST_CANCELLED; |
David Howells | 5753c44 | 2009-11-19 18:11:19 +0000 | [diff] [blame] | 330 | if (test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags)) |
| 331 | object->n_exclusive--; |
| 332 | if (test_and_clear_bit(FSCACHE_OP_WAITING, &op->flags)) |
| 333 | wake_up_bit(&op->flags, FSCACHE_OP_WAITING); |
| 334 | fscache_put_operation(op); |
| 335 | ret = 0; |
| 336 | } |
| 337 | |
| 338 | spin_unlock(&object->lock); |
| 339 | _leave(" = %d", ret); |
| 340 | return ret; |
| 341 | } |
| 342 | |
| 343 | /* |
David Howells | ef778e7 | 2012-12-20 21:52:36 +0000 | [diff] [blame] | 344 | * Cancel all pending operations on an object |
| 345 | */ |
| 346 | void fscache_cancel_all_ops(struct fscache_object *object) |
| 347 | { |
| 348 | struct fscache_operation *op; |
| 349 | |
| 350 | _enter("OBJ%x", object->debug_id); |
| 351 | |
| 352 | spin_lock(&object->lock); |
| 353 | |
| 354 | while (!list_empty(&object->pending_ops)) { |
| 355 | op = list_entry(object->pending_ops.next, |
| 356 | struct fscache_operation, pend_link); |
| 357 | fscache_stat(&fscache_n_op_cancelled); |
| 358 | list_del_init(&op->pend_link); |
| 359 | |
| 360 | ASSERTCMP(op->state, ==, FSCACHE_OP_ST_PENDING); |
| 361 | op->state = FSCACHE_OP_ST_CANCELLED; |
| 362 | |
| 363 | if (test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags)) |
| 364 | object->n_exclusive--; |
| 365 | if (test_and_clear_bit(FSCACHE_OP_WAITING, &op->flags)) |
| 366 | wake_up_bit(&op->flags, FSCACHE_OP_WAITING); |
| 367 | fscache_put_operation(op); |
| 368 | cond_resched_lock(&object->lock); |
| 369 | } |
| 370 | |
| 371 | spin_unlock(&object->lock); |
| 372 | _leave(""); |
| 373 | } |
| 374 | |
| 375 | /* |
David Howells | 1f372df | 2012-12-13 20:03:13 +0000 | [diff] [blame] | 376 | * Record the completion or cancellation of an in-progress operation. |
David Howells | 9f10523 | 2012-12-20 21:52:35 +0000 | [diff] [blame] | 377 | */ |
David Howells | 1f372df | 2012-12-13 20:03:13 +0000 | [diff] [blame] | 378 | void fscache_op_complete(struct fscache_operation *op, bool cancelled) |
David Howells | 9f10523 | 2012-12-20 21:52:35 +0000 | [diff] [blame] | 379 | { |
| 380 | struct fscache_object *object = op->object; |
| 381 | |
| 382 | _enter("OBJ%x", object->debug_id); |
| 383 | |
| 384 | ASSERTCMP(op->state, ==, FSCACHE_OP_ST_IN_PROGRESS); |
| 385 | ASSERTCMP(object->n_in_progress, >, 0); |
| 386 | ASSERTIFCMP(test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags), |
| 387 | object->n_exclusive, >, 0); |
| 388 | ASSERTIFCMP(test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags), |
| 389 | object->n_in_progress, ==, 1); |
| 390 | |
| 391 | spin_lock(&object->lock); |
| 392 | |
David Howells | 1f372df | 2012-12-13 20:03:13 +0000 | [diff] [blame] | 393 | op->state = cancelled ? |
| 394 | FSCACHE_OP_ST_CANCELLED : FSCACHE_OP_ST_COMPLETE; |
David Howells | 9f10523 | 2012-12-20 21:52:35 +0000 | [diff] [blame] | 395 | |
| 396 | if (test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags)) |
| 397 | object->n_exclusive--; |
| 398 | object->n_in_progress--; |
| 399 | if (object->n_in_progress == 0) |
| 400 | fscache_start_operations(object); |
| 401 | |
| 402 | spin_unlock(&object->lock); |
| 403 | _leave(""); |
| 404 | } |
| 405 | EXPORT_SYMBOL(fscache_op_complete); |
| 406 | |
| 407 | /* |
David Howells | 952efe7 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 408 | * release an operation |
| 409 | * - queues pending ops if this is the last in-progress op |
| 410 | */ |
| 411 | void fscache_put_operation(struct fscache_operation *op) |
| 412 | { |
| 413 | struct fscache_object *object; |
| 414 | struct fscache_cache *cache; |
| 415 | |
| 416 | _enter("{OBJ%x OP%x,%d}", |
| 417 | op->object->debug_id, op->debug_id, atomic_read(&op->usage)); |
| 418 | |
| 419 | ASSERTCMP(atomic_read(&op->usage), >, 0); |
| 420 | |
| 421 | if (!atomic_dec_and_test(&op->usage)) |
| 422 | return; |
| 423 | |
| 424 | _debug("PUT OP"); |
David Howells | 9f10523 | 2012-12-20 21:52:35 +0000 | [diff] [blame] | 425 | ASSERTIFCMP(op->state != FSCACHE_OP_ST_COMPLETE, |
| 426 | op->state, ==, FSCACHE_OP_ST_CANCELLED); |
| 427 | op->state = FSCACHE_OP_ST_DEAD; |
David Howells | 952efe7 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 428 | |
| 429 | fscache_stat(&fscache_n_op_release); |
| 430 | |
| 431 | if (op->release) { |
| 432 | op->release(op); |
| 433 | op->release = NULL; |
| 434 | } |
| 435 | |
| 436 | object = op->object; |
| 437 | |
David Howells | 1362729 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 438 | if (test_bit(FSCACHE_OP_DEC_READ_CNT, &op->flags)) |
| 439 | atomic_dec(&object->n_reads); |
| 440 | if (test_bit(FSCACHE_OP_UNUSE_COOKIE, &op->flags)) |
| 441 | fscache_unuse_cookie(object); |
David Howells | 4fbf429 | 2009-11-19 18:11:04 +0000 | [diff] [blame] | 442 | |
David Howells | 952efe7 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 443 | /* now... we may get called with the object spinlock held, so we |
| 444 | * complete the cleanup here only if we can immediately acquire the |
| 445 | * lock, and defer it otherwise */ |
| 446 | if (!spin_trylock(&object->lock)) { |
| 447 | _debug("defer put"); |
| 448 | fscache_stat(&fscache_n_op_deferred_release); |
| 449 | |
| 450 | cache = object->cache; |
| 451 | spin_lock(&cache->op_gc_list_lock); |
| 452 | list_add_tail(&op->pend_link, &cache->op_gc_list); |
| 453 | spin_unlock(&cache->op_gc_list_lock); |
| 454 | schedule_work(&cache->op_gc); |
| 455 | _leave(" [defer]"); |
| 456 | return; |
| 457 | } |
| 458 | |
David Howells | 952efe7 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 459 | ASSERTCMP(object->n_ops, >, 0); |
| 460 | object->n_ops--; |
| 461 | if (object->n_ops == 0) |
| 462 | fscache_raise_event(object, FSCACHE_OBJECT_EV_CLEARED); |
| 463 | |
| 464 | spin_unlock(&object->lock); |
| 465 | |
| 466 | kfree(op); |
| 467 | _leave(" [done]"); |
| 468 | } |
| 469 | EXPORT_SYMBOL(fscache_put_operation); |
| 470 | |
| 471 | /* |
| 472 | * garbage collect operations that have had their release deferred |
| 473 | */ |
| 474 | void fscache_operation_gc(struct work_struct *work) |
| 475 | { |
| 476 | struct fscache_operation *op; |
| 477 | struct fscache_object *object; |
| 478 | struct fscache_cache *cache = |
| 479 | container_of(work, struct fscache_cache, op_gc); |
| 480 | int count = 0; |
| 481 | |
| 482 | _enter(""); |
| 483 | |
| 484 | do { |
| 485 | spin_lock(&cache->op_gc_list_lock); |
| 486 | if (list_empty(&cache->op_gc_list)) { |
| 487 | spin_unlock(&cache->op_gc_list_lock); |
| 488 | break; |
| 489 | } |
| 490 | |
| 491 | op = list_entry(cache->op_gc_list.next, |
| 492 | struct fscache_operation, pend_link); |
| 493 | list_del(&op->pend_link); |
| 494 | spin_unlock(&cache->op_gc_list_lock); |
| 495 | |
| 496 | object = op->object; |
David Howells | 9f10523 | 2012-12-20 21:52:35 +0000 | [diff] [blame] | 497 | spin_lock(&object->lock); |
David Howells | 952efe7 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 498 | |
| 499 | _debug("GC DEFERRED REL OBJ%x OP%x", |
| 500 | object->debug_id, op->debug_id); |
| 501 | fscache_stat(&fscache_n_op_gc); |
| 502 | |
| 503 | ASSERTCMP(atomic_read(&op->usage), ==, 0); |
David Howells | 9f10523 | 2012-12-20 21:52:35 +0000 | [diff] [blame] | 504 | ASSERTCMP(op->state, ==, FSCACHE_OP_ST_DEAD); |
David Howells | 952efe7 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 505 | |
| 506 | ASSERTCMP(object->n_ops, >, 0); |
| 507 | object->n_ops--; |
| 508 | if (object->n_ops == 0) |
| 509 | fscache_raise_event(object, FSCACHE_OBJECT_EV_CLEARED); |
| 510 | |
| 511 | spin_unlock(&object->lock); |
David Howells | 9f10523 | 2012-12-20 21:52:35 +0000 | [diff] [blame] | 512 | kfree(op); |
David Howells | 952efe7 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 513 | |
| 514 | } while (count++ < 20); |
| 515 | |
| 516 | if (!list_empty(&cache->op_gc_list)) |
| 517 | schedule_work(&cache->op_gc); |
| 518 | |
| 519 | _leave(""); |
| 520 | } |
| 521 | |
| 522 | /* |
Tejun Heo | 8af7c12 | 2010-07-20 22:09:01 +0200 | [diff] [blame] | 523 | * execute an operation using fs_op_wq to provide processing context - |
| 524 | * the caller holds a ref to this object, so we don't need to hold one |
David Howells | 952efe7 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 525 | */ |
Tejun Heo | 8af7c12 | 2010-07-20 22:09:01 +0200 | [diff] [blame] | 526 | void fscache_op_work_func(struct work_struct *work) |
David Howells | 952efe7 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 527 | { |
| 528 | struct fscache_operation *op = |
Tejun Heo | 8af7c12 | 2010-07-20 22:09:01 +0200 | [diff] [blame] | 529 | container_of(work, struct fscache_operation, work); |
David Howells | 952efe7 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 530 | unsigned long start; |
| 531 | |
| 532 | _enter("{OBJ%x OP%x,%d}", |
| 533 | op->object->debug_id, op->debug_id, atomic_read(&op->usage)); |
| 534 | |
| 535 | ASSERT(op->processor != NULL); |
| 536 | start = jiffies; |
| 537 | op->processor(op); |
| 538 | fscache_hist(fscache_ops_histogram, start); |
Tejun Heo | 8af7c12 | 2010-07-20 22:09:01 +0200 | [diff] [blame] | 539 | fscache_put_operation(op); |
David Howells | 952efe7 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 540 | |
| 541 | _leave(""); |
| 542 | } |