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