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