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 | 6515d1d | 2015-02-25 11:53:57 +0000 | [diff] [blame] | 179 | } else if (flags & BIT(FSCACHE_OBJECT_KILLED_BY_CACHE)) { |
| 180 | op->state = FSCACHE_OP_ST_CANCELLED; |
| 181 | ret = -ENOBUFS; |
David Howells | 952efe7 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 182 | } else { |
David Howells | 30ceec6 | 2015-02-24 10:05:27 +0000 | [diff] [blame] | 183 | fscache_report_unexpected_submission(object, op, ostate); |
| 184 | op->state = FSCACHE_OP_ST_CANCELLED; |
| 185 | ret = -ENOBUFS; |
David Howells | 952efe7 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | spin_unlock(&object->lock); |
David Howells | 8d76349 | 2012-12-05 13:34:48 +0000 | [diff] [blame] | 189 | return ret; |
David Howells | 952efe7 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | /* |
David Howells | 952efe7 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 193 | * submit an operation for an object |
| 194 | * - objects may be submitted only in the following states: |
| 195 | * - during object creation (write ops may be submitted) |
| 196 | * - whilst the object is active |
| 197 | * - after an I/O error incurred in one of the two above states (op rejected) |
| 198 | * - this gets any extra refs it needs on an op |
| 199 | */ |
| 200 | int fscache_submit_op(struct fscache_object *object, |
| 201 | struct fscache_operation *op) |
| 202 | { |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 203 | const struct fscache_state *ostate; |
David Howells | 30ceec6 | 2015-02-24 10:05:27 +0000 | [diff] [blame] | 204 | unsigned long flags; |
David Howells | 952efe7 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 205 | int ret; |
| 206 | |
| 207 | _enter("{OBJ%x OP%x},{%u}", |
| 208 | object->debug_id, op->debug_id, atomic_read(&op->usage)); |
| 209 | |
David Howells | 9f10523 | 2012-12-20 21:52:35 +0000 | [diff] [blame] | 210 | ASSERTCMP(op->state, ==, FSCACHE_OP_ST_INITIALISED); |
David Howells | 952efe7 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 211 | ASSERTCMP(atomic_read(&op->usage), >, 0); |
| 212 | |
| 213 | spin_lock(&object->lock); |
| 214 | ASSERTCMP(object->n_ops, >=, object->n_in_progress); |
| 215 | ASSERTCMP(object->n_ops, >=, object->n_exclusive); |
David Howells | 5753c44 | 2009-11-19 18:11:19 +0000 | [diff] [blame] | 216 | ASSERT(list_empty(&op->pend_link)); |
David Howells | 952efe7 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 217 | |
| 218 | ostate = object->state; |
| 219 | smp_rmb(); |
| 220 | |
David Howells | 9f10523 | 2012-12-20 21:52:35 +0000 | [diff] [blame] | 221 | op->state = FSCACHE_OP_ST_PENDING; |
David Howells | 30ceec6 | 2015-02-24 10:05:27 +0000 | [diff] [blame] | 222 | flags = READ_ONCE(object->flags); |
| 223 | if (unlikely(!(flags & BIT(FSCACHE_OBJECT_IS_LIVE)))) { |
| 224 | fscache_stat(&fscache_n_op_rejected); |
| 225 | op->state = FSCACHE_OP_ST_CANCELLED; |
| 226 | ret = -ENOBUFS; |
| 227 | } else if (unlikely(fscache_cache_is_broken(object))) { |
| 228 | op->state = FSCACHE_OP_ST_CANCELLED; |
| 229 | ret = -EIO; |
| 230 | } else if (flags & BIT(FSCACHE_OBJECT_IS_AVAILABLE)) { |
David Howells | 952efe7 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 231 | op->object = object; |
| 232 | object->n_ops++; |
| 233 | |
| 234 | if (object->n_exclusive > 0) { |
| 235 | atomic_inc(&op->usage); |
| 236 | list_add_tail(&op->pend_link, &object->pending_ops); |
| 237 | fscache_stat(&fscache_n_op_pend); |
| 238 | } else if (!list_empty(&object->pending_ops)) { |
| 239 | atomic_inc(&op->usage); |
| 240 | list_add_tail(&op->pend_link, &object->pending_ops); |
| 241 | fscache_stat(&fscache_n_op_pend); |
| 242 | fscache_start_operations(object); |
| 243 | } else { |
| 244 | ASSERTCMP(object->n_exclusive, ==, 0); |
| 245 | fscache_run_op(object, op); |
| 246 | } |
| 247 | ret = 0; |
David Howells | 30ceec6 | 2015-02-24 10:05:27 +0000 | [diff] [blame] | 248 | } else if (flags & BIT(FSCACHE_OBJECT_IS_LOOKED_UP)) { |
David Howells | 952efe7 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 249 | op->object = object; |
| 250 | object->n_ops++; |
| 251 | atomic_inc(&op->usage); |
| 252 | list_add_tail(&op->pend_link, &object->pending_ops); |
| 253 | fscache_stat(&fscache_n_op_pend); |
| 254 | ret = 0; |
David Howells | 6515d1d | 2015-02-25 11:53:57 +0000 | [diff] [blame] | 255 | } else if (flags & BIT(FSCACHE_OBJECT_KILLED_BY_CACHE)) { |
| 256 | op->state = FSCACHE_OP_ST_CANCELLED; |
| 257 | ret = -ENOBUFS; |
David Howells | 30ceec6 | 2015-02-24 10:05:27 +0000 | [diff] [blame] | 258 | } else { |
David Howells | 952efe7 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 259 | fscache_report_unexpected_submission(object, op, ostate); |
| 260 | ASSERT(!fscache_object_is_active(object)); |
David Howells | 9f10523 | 2012-12-20 21:52:35 +0000 | [diff] [blame] | 261 | op->state = FSCACHE_OP_ST_CANCELLED; |
David Howells | 952efe7 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 262 | ret = -ENOBUFS; |
David Howells | 952efe7 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | spin_unlock(&object->lock); |
| 266 | return ret; |
| 267 | } |
| 268 | |
| 269 | /* |
| 270 | * queue an object for withdrawal on error, aborting all following asynchronous |
| 271 | * operations |
| 272 | */ |
| 273 | void fscache_abort_object(struct fscache_object *object) |
| 274 | { |
| 275 | _enter("{OBJ%x}", object->debug_id); |
| 276 | |
| 277 | fscache_raise_event(object, FSCACHE_OBJECT_EV_ERROR); |
| 278 | } |
| 279 | |
| 280 | /* |
David Howells | dcfae32 | 2013-05-24 12:45:31 +0100 | [diff] [blame] | 281 | * Jump start the operation processing on an object. The caller must hold |
| 282 | * object->lock. |
David Howells | 952efe7 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 283 | */ |
| 284 | void fscache_start_operations(struct fscache_object *object) |
| 285 | { |
| 286 | struct fscache_operation *op; |
| 287 | bool stop = false; |
| 288 | |
| 289 | while (!list_empty(&object->pending_ops) && !stop) { |
| 290 | op = list_entry(object->pending_ops.next, |
| 291 | struct fscache_operation, pend_link); |
| 292 | |
| 293 | if (test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags)) { |
| 294 | if (object->n_in_progress > 0) |
| 295 | break; |
| 296 | stop = true; |
| 297 | } |
| 298 | list_del_init(&op->pend_link); |
David Howells | 5753c44 | 2009-11-19 18:11:19 +0000 | [diff] [blame] | 299 | fscache_run_op(object, op); |
David Howells | 952efe7 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 300 | |
| 301 | /* the pending queue was holding a ref on the object */ |
| 302 | fscache_put_operation(op); |
| 303 | } |
| 304 | |
| 305 | ASSERTCMP(object->n_in_progress, <=, object->n_ops); |
| 306 | |
| 307 | _debug("woke %d ops on OBJ%x", |
| 308 | object->n_in_progress, object->debug_id); |
| 309 | } |
| 310 | |
| 311 | /* |
David Howells | 5753c44 | 2009-11-19 18:11:19 +0000 | [diff] [blame] | 312 | * cancel an operation that's pending on an object |
| 313 | */ |
David Howells | 91c7fbb | 2012-12-14 11:02:22 +0000 | [diff] [blame] | 314 | int fscache_cancel_op(struct fscache_operation *op, |
David Howells | 418b7eb | 2015-02-24 10:05:28 +0000 | [diff] [blame^] | 315 | void (*do_cancel)(struct fscache_operation *), |
| 316 | bool cancel_in_progress_op) |
David Howells | 5753c44 | 2009-11-19 18:11:19 +0000 | [diff] [blame] | 317 | { |
| 318 | struct fscache_object *object = op->object; |
David Howells | 418b7eb | 2015-02-24 10:05:28 +0000 | [diff] [blame^] | 319 | bool put = false; |
David Howells | 5753c44 | 2009-11-19 18:11:19 +0000 | [diff] [blame] | 320 | int ret; |
| 321 | |
| 322 | _enter("OBJ%x OP%x}", op->object->debug_id, op->debug_id); |
| 323 | |
David Howells | 9f10523 | 2012-12-20 21:52:35 +0000 | [diff] [blame] | 324 | ASSERTCMP(op->state, >=, FSCACHE_OP_ST_PENDING); |
| 325 | ASSERTCMP(op->state, !=, FSCACHE_OP_ST_CANCELLED); |
| 326 | ASSERTCMP(atomic_read(&op->usage), >, 0); |
| 327 | |
David Howells | 5753c44 | 2009-11-19 18:11:19 +0000 | [diff] [blame] | 328 | spin_lock(&object->lock); |
| 329 | |
| 330 | ret = -EBUSY; |
David Howells | 9f10523 | 2012-12-20 21:52:35 +0000 | [diff] [blame] | 331 | if (op->state == FSCACHE_OP_ST_PENDING) { |
| 332 | ASSERT(!list_empty(&op->pend_link)); |
David Howells | 5753c44 | 2009-11-19 18:11:19 +0000 | [diff] [blame] | 333 | list_del_init(&op->pend_link); |
David Howells | 418b7eb | 2015-02-24 10:05:28 +0000 | [diff] [blame^] | 334 | put = true; |
| 335 | fscache_stat(&fscache_n_op_cancelled); |
David Howells | 91c7fbb | 2012-12-14 11:02:22 +0000 | [diff] [blame] | 336 | if (do_cancel) |
| 337 | do_cancel(op); |
David Howells | 9f10523 | 2012-12-20 21:52:35 +0000 | [diff] [blame] | 338 | op->state = FSCACHE_OP_ST_CANCELLED; |
David Howells | 5753c44 | 2009-11-19 18:11:19 +0000 | [diff] [blame] | 339 | if (test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags)) |
| 340 | object->n_exclusive--; |
| 341 | if (test_and_clear_bit(FSCACHE_OP_WAITING, &op->flags)) |
| 342 | wake_up_bit(&op->flags, FSCACHE_OP_WAITING); |
David Howells | 418b7eb | 2015-02-24 10:05:28 +0000 | [diff] [blame^] | 343 | ret = 0; |
| 344 | } else if (op->state == FSCACHE_OP_ST_IN_PROGRESS && cancel_in_progress_op) { |
| 345 | fscache_stat(&fscache_n_op_cancelled); |
| 346 | if (do_cancel) |
| 347 | do_cancel(op); |
| 348 | op->state = FSCACHE_OP_ST_CANCELLED; |
| 349 | if (test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags)) |
| 350 | object->n_exclusive--; |
| 351 | if (test_and_clear_bit(FSCACHE_OP_WAITING, &op->flags)) |
| 352 | wake_up_bit(&op->flags, FSCACHE_OP_WAITING); |
David Howells | 5753c44 | 2009-11-19 18:11:19 +0000 | [diff] [blame] | 353 | ret = 0; |
| 354 | } |
| 355 | |
David Howells | 418b7eb | 2015-02-24 10:05:28 +0000 | [diff] [blame^] | 356 | if (put) |
| 357 | fscache_put_operation(op); |
David Howells | 5753c44 | 2009-11-19 18:11:19 +0000 | [diff] [blame] | 358 | spin_unlock(&object->lock); |
| 359 | _leave(" = %d", ret); |
| 360 | return ret; |
| 361 | } |
| 362 | |
| 363 | /* |
David Howells | ef778e7 | 2012-12-20 21:52:36 +0000 | [diff] [blame] | 364 | * Cancel all pending operations on an object |
| 365 | */ |
| 366 | void fscache_cancel_all_ops(struct fscache_object *object) |
| 367 | { |
| 368 | struct fscache_operation *op; |
| 369 | |
| 370 | _enter("OBJ%x", object->debug_id); |
| 371 | |
| 372 | spin_lock(&object->lock); |
| 373 | |
| 374 | while (!list_empty(&object->pending_ops)) { |
| 375 | op = list_entry(object->pending_ops.next, |
| 376 | struct fscache_operation, pend_link); |
| 377 | fscache_stat(&fscache_n_op_cancelled); |
| 378 | list_del_init(&op->pend_link); |
| 379 | |
| 380 | ASSERTCMP(op->state, ==, FSCACHE_OP_ST_PENDING); |
| 381 | op->state = FSCACHE_OP_ST_CANCELLED; |
| 382 | |
| 383 | if (test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags)) |
| 384 | object->n_exclusive--; |
| 385 | if (test_and_clear_bit(FSCACHE_OP_WAITING, &op->flags)) |
| 386 | wake_up_bit(&op->flags, FSCACHE_OP_WAITING); |
| 387 | fscache_put_operation(op); |
| 388 | cond_resched_lock(&object->lock); |
| 389 | } |
| 390 | |
| 391 | spin_unlock(&object->lock); |
| 392 | _leave(""); |
| 393 | } |
| 394 | |
| 395 | /* |
David Howells | 1f372df | 2012-12-13 20:03:13 +0000 | [diff] [blame] | 396 | * Record the completion or cancellation of an in-progress operation. |
David Howells | 9f10523 | 2012-12-20 21:52:35 +0000 | [diff] [blame] | 397 | */ |
David Howells | 1f372df | 2012-12-13 20:03:13 +0000 | [diff] [blame] | 398 | void fscache_op_complete(struct fscache_operation *op, bool cancelled) |
David Howells | 9f10523 | 2012-12-20 21:52:35 +0000 | [diff] [blame] | 399 | { |
| 400 | struct fscache_object *object = op->object; |
| 401 | |
| 402 | _enter("OBJ%x", object->debug_id); |
| 403 | |
| 404 | ASSERTCMP(op->state, ==, FSCACHE_OP_ST_IN_PROGRESS); |
| 405 | ASSERTCMP(object->n_in_progress, >, 0); |
| 406 | ASSERTIFCMP(test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags), |
| 407 | object->n_exclusive, >, 0); |
| 408 | ASSERTIFCMP(test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags), |
| 409 | object->n_in_progress, ==, 1); |
| 410 | |
| 411 | spin_lock(&object->lock); |
| 412 | |
David Howells | 1f372df | 2012-12-13 20:03:13 +0000 | [diff] [blame] | 413 | op->state = cancelled ? |
| 414 | FSCACHE_OP_ST_CANCELLED : FSCACHE_OP_ST_COMPLETE; |
David Howells | 9f10523 | 2012-12-20 21:52:35 +0000 | [diff] [blame] | 415 | |
| 416 | if (test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags)) |
| 417 | object->n_exclusive--; |
| 418 | object->n_in_progress--; |
| 419 | if (object->n_in_progress == 0) |
| 420 | fscache_start_operations(object); |
| 421 | |
| 422 | spin_unlock(&object->lock); |
| 423 | _leave(""); |
| 424 | } |
| 425 | EXPORT_SYMBOL(fscache_op_complete); |
| 426 | |
| 427 | /* |
David Howells | 952efe7 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 428 | * release an operation |
| 429 | * - queues pending ops if this is the last in-progress op |
| 430 | */ |
| 431 | void fscache_put_operation(struct fscache_operation *op) |
| 432 | { |
| 433 | struct fscache_object *object; |
| 434 | struct fscache_cache *cache; |
| 435 | |
| 436 | _enter("{OBJ%x OP%x,%d}", |
| 437 | op->object->debug_id, op->debug_id, atomic_read(&op->usage)); |
| 438 | |
| 439 | ASSERTCMP(atomic_read(&op->usage), >, 0); |
| 440 | |
| 441 | if (!atomic_dec_and_test(&op->usage)) |
| 442 | return; |
| 443 | |
| 444 | _debug("PUT OP"); |
David Howells | 9f10523 | 2012-12-20 21:52:35 +0000 | [diff] [blame] | 445 | ASSERTIFCMP(op->state != FSCACHE_OP_ST_COMPLETE, |
| 446 | op->state, ==, FSCACHE_OP_ST_CANCELLED); |
| 447 | op->state = FSCACHE_OP_ST_DEAD; |
David Howells | 952efe7 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 448 | |
| 449 | fscache_stat(&fscache_n_op_release); |
| 450 | |
| 451 | if (op->release) { |
| 452 | op->release(op); |
| 453 | op->release = NULL; |
| 454 | } |
| 455 | |
| 456 | object = op->object; |
| 457 | |
David Howells | 1362729 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 458 | if (test_bit(FSCACHE_OP_DEC_READ_CNT, &op->flags)) |
| 459 | atomic_dec(&object->n_reads); |
| 460 | if (test_bit(FSCACHE_OP_UNUSE_COOKIE, &op->flags)) |
| 461 | fscache_unuse_cookie(object); |
David Howells | 4fbf429 | 2009-11-19 18:11:04 +0000 | [diff] [blame] | 462 | |
David Howells | 952efe7 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 463 | /* now... we may get called with the object spinlock held, so we |
| 464 | * complete the cleanup here only if we can immediately acquire the |
| 465 | * lock, and defer it otherwise */ |
| 466 | if (!spin_trylock(&object->lock)) { |
| 467 | _debug("defer put"); |
| 468 | fscache_stat(&fscache_n_op_deferred_release); |
| 469 | |
| 470 | cache = object->cache; |
| 471 | spin_lock(&cache->op_gc_list_lock); |
| 472 | list_add_tail(&op->pend_link, &cache->op_gc_list); |
| 473 | spin_unlock(&cache->op_gc_list_lock); |
| 474 | schedule_work(&cache->op_gc); |
| 475 | _leave(" [defer]"); |
| 476 | return; |
| 477 | } |
| 478 | |
David Howells | 952efe7 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 479 | ASSERTCMP(object->n_ops, >, 0); |
| 480 | object->n_ops--; |
| 481 | if (object->n_ops == 0) |
| 482 | fscache_raise_event(object, FSCACHE_OBJECT_EV_CLEARED); |
| 483 | |
| 484 | spin_unlock(&object->lock); |
| 485 | |
| 486 | kfree(op); |
| 487 | _leave(" [done]"); |
| 488 | } |
| 489 | EXPORT_SYMBOL(fscache_put_operation); |
| 490 | |
| 491 | /* |
| 492 | * garbage collect operations that have had their release deferred |
| 493 | */ |
| 494 | void fscache_operation_gc(struct work_struct *work) |
| 495 | { |
| 496 | struct fscache_operation *op; |
| 497 | struct fscache_object *object; |
| 498 | struct fscache_cache *cache = |
| 499 | container_of(work, struct fscache_cache, op_gc); |
| 500 | int count = 0; |
| 501 | |
| 502 | _enter(""); |
| 503 | |
| 504 | do { |
| 505 | spin_lock(&cache->op_gc_list_lock); |
| 506 | if (list_empty(&cache->op_gc_list)) { |
| 507 | spin_unlock(&cache->op_gc_list_lock); |
| 508 | break; |
| 509 | } |
| 510 | |
| 511 | op = list_entry(cache->op_gc_list.next, |
| 512 | struct fscache_operation, pend_link); |
| 513 | list_del(&op->pend_link); |
| 514 | spin_unlock(&cache->op_gc_list_lock); |
| 515 | |
| 516 | object = op->object; |
David Howells | 9f10523 | 2012-12-20 21:52:35 +0000 | [diff] [blame] | 517 | spin_lock(&object->lock); |
David Howells | 952efe7 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 518 | |
| 519 | _debug("GC DEFERRED REL OBJ%x OP%x", |
| 520 | object->debug_id, op->debug_id); |
| 521 | fscache_stat(&fscache_n_op_gc); |
| 522 | |
| 523 | ASSERTCMP(atomic_read(&op->usage), ==, 0); |
David Howells | 9f10523 | 2012-12-20 21:52:35 +0000 | [diff] [blame] | 524 | ASSERTCMP(op->state, ==, FSCACHE_OP_ST_DEAD); |
David Howells | 952efe7 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 525 | |
| 526 | ASSERTCMP(object->n_ops, >, 0); |
| 527 | object->n_ops--; |
| 528 | if (object->n_ops == 0) |
| 529 | fscache_raise_event(object, FSCACHE_OBJECT_EV_CLEARED); |
| 530 | |
| 531 | spin_unlock(&object->lock); |
David Howells | 9f10523 | 2012-12-20 21:52:35 +0000 | [diff] [blame] | 532 | kfree(op); |
David Howells | 952efe7 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 533 | |
| 534 | } while (count++ < 20); |
| 535 | |
| 536 | if (!list_empty(&cache->op_gc_list)) |
| 537 | schedule_work(&cache->op_gc); |
| 538 | |
| 539 | _leave(""); |
| 540 | } |
| 541 | |
| 542 | /* |
Tejun Heo | 8af7c12 | 2010-07-20 22:09:01 +0200 | [diff] [blame] | 543 | * execute an operation using fs_op_wq to provide processing context - |
| 544 | * 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] | 545 | */ |
Tejun Heo | 8af7c12 | 2010-07-20 22:09:01 +0200 | [diff] [blame] | 546 | void fscache_op_work_func(struct work_struct *work) |
David Howells | 952efe7 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 547 | { |
| 548 | struct fscache_operation *op = |
Tejun Heo | 8af7c12 | 2010-07-20 22:09:01 +0200 | [diff] [blame] | 549 | container_of(work, struct fscache_operation, work); |
David Howells | 952efe7 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 550 | unsigned long start; |
| 551 | |
| 552 | _enter("{OBJ%x OP%x,%d}", |
| 553 | op->object->debug_id, op->debug_id, atomic_read(&op->usage)); |
| 554 | |
| 555 | ASSERT(op->processor != NULL); |
| 556 | start = jiffies; |
| 557 | op->processor(op); |
| 558 | fscache_hist(fscache_ops_histogram, start); |
Tejun Heo | 8af7c12 | 2010-07-20 22:09:01 +0200 | [diff] [blame] | 559 | fscache_put_operation(op); |
David Howells | 952efe7 | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 560 | |
| 561 | _leave(""); |
| 562 | } |