blob: e30c5975ea585e73dd70dcba892c295e3d78e68a [file] [log] [blame]
David Howells952efe72009-04-03 16:42:39 +01001/* 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 Howells440f0af2009-11-19 18:11:01 +000016#include <linux/seq_file.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
David Howells952efe72009-04-03 16:42:39 +010018#include "internal.h"
19
20atomic_t fscache_op_debug_id;
21EXPORT_SYMBOL(fscache_op_debug_id);
22
David Howellsd3b97ca2015-02-24 10:05:29 +000023static void fscache_operation_dummy_cancel(struct fscache_operation *op)
24{
25}
26
David Howells952efe72009-04-03 16:42:39 +010027/**
David Howells1339ec92015-02-25 13:26:39 +000028 * 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 Howells08c2e3d2018-04-04 13:41:27 +010035void fscache_operation_init(struct fscache_cookie *cookie,
36 struct fscache_operation *op,
David Howells1339ec92015-02-25 13:26:39 +000037 fscache_operation_processor_t processor,
David Howellsd3b97ca2015-02-24 10:05:29 +000038 fscache_operation_cancel_t cancel,
David Howells1339ec92015-02-25 13:26:39 +000039 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 Howellsd3b97ca2015-02-24 10:05:29 +000046 op->cancel = cancel ?: fscache_operation_dummy_cancel;
David Howells1339ec92015-02-25 13:26:39 +000047 op->release = release;
48 INIT_LIST_HEAD(&op->pend_link);
David Howells03cdd0e2015-02-25 13:21:15 +000049 fscache_stat(&fscache_n_op_initialised);
David Howells08c2e3d2018-04-04 13:41:27 +010050 trace_fscache_op(cookie, op, fscache_op_init);
David Howells1339ec92015-02-25 13:26:39 +000051}
52EXPORT_SYMBOL(fscache_operation_init);
53
54/**
David Howells952efe72009-04-03 16:42:39 +010055 * 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 */
62void fscache_enqueue_operation(struct fscache_operation *op)
63{
David Howells08c2e3d2018-04-04 13:41:27 +010064 struct fscache_cookie *cookie = op->object->cookie;
65
David Howells952efe72009-04-03 16:42:39 +010066 _enter("{OBJ%x OP%x,%u}",
67 op->object->debug_id, op->debug_id, atomic_read(&op->usage));
68
David Howells5753c442009-11-19 18:11:19 +000069 ASSERT(list_empty(&op->pend_link));
David Howells952efe72009-04-03 16:42:39 +010070 ASSERT(op->processor != NULL);
David Howells493f7bc2013-05-10 19:50:26 +010071 ASSERT(fscache_object_is_available(op->object));
David Howells952efe72009-04-03 16:42:39 +010072 ASSERTCMP(atomic_read(&op->usage), >, 0);
David Howells9f105232012-12-20 21:52:35 +000073 ASSERTCMP(op->state, ==, FSCACHE_OP_ST_IN_PROGRESS);
David Howells952efe72009-04-03 16:42:39 +010074
David Howells5753c442009-11-19 18:11:19 +000075 fscache_stat(&fscache_n_op_enqueue);
76 switch (op->flags & FSCACHE_OP_TYPE) {
Tejun Heo8af7c122010-07-20 22:09:01 +020077 case FSCACHE_OP_ASYNC:
David Howells08c2e3d2018-04-04 13:41:27 +010078 trace_fscache_op(cookie, op, fscache_op_enqueue_async);
Tejun Heo8af7c122010-07-20 22:09:01 +020079 _debug("queue async");
David Howells5753c442009-11-19 18:11:19 +000080 atomic_inc(&op->usage);
Tejun Heo8af7c122010-07-20 22:09:01 +020081 if (!queue_work(fscache_op_wq, &op->work))
David Howells5753c442009-11-19 18:11:19 +000082 fscache_put_operation(op);
83 break;
David Howells5753c442009-11-19 18:11:19 +000084 case FSCACHE_OP_MYTHREAD:
David Howells08c2e3d2018-04-04 13:41:27 +010085 trace_fscache_op(cookie, op, fscache_op_enqueue_mythread);
David Howells5753c442009-11-19 18:11:19 +000086 _debug("queue for caller's attention");
87 break;
88 default:
Fabian Frederick36dfd112014-06-04 16:05:38 -070089 pr_err("Unexpected op type %lx", op->flags);
David Howells5753c442009-11-19 18:11:19 +000090 BUG();
91 break;
David Howells952efe72009-04-03 16:42:39 +010092 }
93}
94EXPORT_SYMBOL(fscache_enqueue_operation);
95
96/*
97 * start an op running
98 */
99static void fscache_run_op(struct fscache_object *object,
100 struct fscache_operation *op)
101{
David Howells9f105232012-12-20 21:52:35 +0000102 ASSERTCMP(op->state, ==, FSCACHE_OP_ST_PENDING);
103
104 op->state = FSCACHE_OP_ST_IN_PROGRESS;
David Howells952efe72009-04-03 16:42:39 +0100105 object->n_in_progress++;
106 if (test_and_clear_bit(FSCACHE_OP_WAITING, &op->flags))
107 wake_up_bit(&op->flags, FSCACHE_OP_WAITING);
108 if (op->processor)
109 fscache_enqueue_operation(op);
David Howells08c2e3d2018-04-04 13:41:27 +0100110 else
111 trace_fscache_op(object->cookie, op, fscache_op_run);
David Howells952efe72009-04-03 16:42:39 +0100112 fscache_stat(&fscache_n_op_run);
113}
114
115/*
David Howells3c305982015-02-24 10:39:28 +0000116 * report an unexpected submission
117 */
118static void fscache_report_unexpected_submission(struct fscache_object *object,
119 struct fscache_operation *op,
120 const struct fscache_state *ostate)
121{
122 static bool once_only;
123 struct fscache_operation *p;
124 unsigned n;
125
126 if (once_only)
127 return;
128 once_only = true;
129
130 kdebug("unexpected submission OP%x [OBJ%x %s]",
131 op->debug_id, object->debug_id, object->state->name);
132 kdebug("objstate=%s [%s]", object->state->name, ostate->name);
133 kdebug("objflags=%lx", object->flags);
134 kdebug("objevent=%lx [%lx]", object->events, object->event_mask);
135 kdebug("ops=%u inp=%u exc=%u",
136 object->n_ops, object->n_in_progress, object->n_exclusive);
137
138 if (!list_empty(&object->pending_ops)) {
139 n = 0;
140 list_for_each_entry(p, &object->pending_ops, pend_link) {
141 ASSERTCMP(p->object, ==, object);
142 kdebug("%p %p", op->processor, op->release);
143 n++;
144 }
145
146 kdebug("n=%u", n);
147 }
148
149 dump_stack();
150}
151
152/*
David Howells952efe72009-04-03 16:42:39 +0100153 * submit an exclusive operation for an object
154 * - other ops are excluded from running simultaneously with this one
155 * - this gets any extra refs it needs on an op
156 */
157int fscache_submit_exclusive_op(struct fscache_object *object,
158 struct fscache_operation *op)
159{
David Howells30ceec62015-02-24 10:05:27 +0000160 const struct fscache_state *ostate;
161 unsigned long flags;
David Howells8d763492012-12-05 13:34:48 +0000162 int ret;
163
David Howells952efe72009-04-03 16:42:39 +0100164 _enter("{OBJ%x OP%x},", object->debug_id, op->debug_id);
165
David Howells08c2e3d2018-04-04 13:41:27 +0100166 trace_fscache_op(object->cookie, op, fscache_op_submit_ex);
167
David Howells9f105232012-12-20 21:52:35 +0000168 ASSERTCMP(op->state, ==, FSCACHE_OP_ST_INITIALISED);
169 ASSERTCMP(atomic_read(&op->usage), >, 0);
170
David Howells952efe72009-04-03 16:42:39 +0100171 spin_lock(&object->lock);
172 ASSERTCMP(object->n_ops, >=, object->n_in_progress);
173 ASSERTCMP(object->n_ops, >=, object->n_exclusive);
David Howells5753c442009-11-19 18:11:19 +0000174 ASSERT(list_empty(&op->pend_link));
David Howells952efe72009-04-03 16:42:39 +0100175
David Howells30ceec62015-02-24 10:05:27 +0000176 ostate = object->state;
177 smp_rmb();
178
David Howells9f105232012-12-20 21:52:35 +0000179 op->state = FSCACHE_OP_ST_PENDING;
David Howells30ceec62015-02-24 10:05:27 +0000180 flags = READ_ONCE(object->flags);
181 if (unlikely(!(flags & BIT(FSCACHE_OBJECT_IS_LIVE)))) {
182 fscache_stat(&fscache_n_op_rejected);
David Howellsd3b97ca2015-02-24 10:05:29 +0000183 op->cancel(op);
David Howells30ceec62015-02-24 10:05:27 +0000184 op->state = FSCACHE_OP_ST_CANCELLED;
185 ret = -ENOBUFS;
186 } else if (unlikely(fscache_cache_is_broken(object))) {
David Howellsd3b97ca2015-02-24 10:05:29 +0000187 op->cancel(op);
David Howells30ceec62015-02-24 10:05:27 +0000188 op->state = FSCACHE_OP_ST_CANCELLED;
189 ret = -EIO;
190 } else if (flags & BIT(FSCACHE_OBJECT_IS_AVAILABLE)) {
David Howells952efe72009-04-03 16:42:39 +0100191 op->object = object;
192 object->n_ops++;
193 object->n_exclusive++; /* reads and writes must wait */
194
David Howells9f105232012-12-20 21:52:35 +0000195 if (object->n_in_progress > 0) {
David Howells952efe72009-04-03 16:42:39 +0100196 atomic_inc(&op->usage);
197 list_add_tail(&op->pend_link, &object->pending_ops);
198 fscache_stat(&fscache_n_op_pend);
199 } else if (!list_empty(&object->pending_ops)) {
200 atomic_inc(&op->usage);
201 list_add_tail(&op->pend_link, &object->pending_ops);
202 fscache_stat(&fscache_n_op_pend);
203 fscache_start_operations(object);
204 } else {
205 ASSERTCMP(object->n_in_progress, ==, 0);
206 fscache_run_op(object, op);
207 }
208
209 /* need to issue a new write op after this */
210 clear_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags);
David Howells8d763492012-12-05 13:34:48 +0000211 ret = 0;
David Howells30ceec62015-02-24 10:05:27 +0000212 } else if (flags & BIT(FSCACHE_OBJECT_IS_LOOKED_UP)) {
David Howells952efe72009-04-03 16:42:39 +0100213 op->object = object;
214 object->n_ops++;
215 object->n_exclusive++; /* reads and writes must wait */
216 atomic_inc(&op->usage);
217 list_add_tail(&op->pend_link, &object->pending_ops);
218 fscache_stat(&fscache_n_op_pend);
David Howells8d763492012-12-05 13:34:48 +0000219 ret = 0;
David Howells6515d1d2015-02-25 11:53:57 +0000220 } else if (flags & BIT(FSCACHE_OBJECT_KILLED_BY_CACHE)) {
David Howellsd3b97ca2015-02-24 10:05:29 +0000221 op->cancel(op);
David Howells6515d1d2015-02-25 11:53:57 +0000222 op->state = FSCACHE_OP_ST_CANCELLED;
223 ret = -ENOBUFS;
David Howells952efe72009-04-03 16:42:39 +0100224 } else {
David Howells30ceec62015-02-24 10:05:27 +0000225 fscache_report_unexpected_submission(object, op, ostate);
David Howellsd3b97ca2015-02-24 10:05:29 +0000226 op->cancel(op);
David Howells30ceec62015-02-24 10:05:27 +0000227 op->state = FSCACHE_OP_ST_CANCELLED;
228 ret = -ENOBUFS;
David Howells952efe72009-04-03 16:42:39 +0100229 }
230
231 spin_unlock(&object->lock);
David Howells8d763492012-12-05 13:34:48 +0000232 return ret;
David Howells952efe72009-04-03 16:42:39 +0100233}
234
235/*
David Howells952efe72009-04-03 16:42:39 +0100236 * submit an operation for an object
237 * - objects may be submitted only in the following states:
238 * - during object creation (write ops may be submitted)
239 * - whilst the object is active
240 * - after an I/O error incurred in one of the two above states (op rejected)
241 * - this gets any extra refs it needs on an op
242 */
243int fscache_submit_op(struct fscache_object *object,
244 struct fscache_operation *op)
245{
David Howellscaaef692013-05-10 19:50:26 +0100246 const struct fscache_state *ostate;
David Howells30ceec62015-02-24 10:05:27 +0000247 unsigned long flags;
David Howells952efe72009-04-03 16:42:39 +0100248 int ret;
249
250 _enter("{OBJ%x OP%x},{%u}",
251 object->debug_id, op->debug_id, atomic_read(&op->usage));
252
David Howells08c2e3d2018-04-04 13:41:27 +0100253 trace_fscache_op(object->cookie, op, fscache_op_submit);
254
David Howells9f105232012-12-20 21:52:35 +0000255 ASSERTCMP(op->state, ==, FSCACHE_OP_ST_INITIALISED);
David Howells952efe72009-04-03 16:42:39 +0100256 ASSERTCMP(atomic_read(&op->usage), >, 0);
257
258 spin_lock(&object->lock);
259 ASSERTCMP(object->n_ops, >=, object->n_in_progress);
260 ASSERTCMP(object->n_ops, >=, object->n_exclusive);
David Howells5753c442009-11-19 18:11:19 +0000261 ASSERT(list_empty(&op->pend_link));
David Howells952efe72009-04-03 16:42:39 +0100262
263 ostate = object->state;
264 smp_rmb();
265
David Howells9f105232012-12-20 21:52:35 +0000266 op->state = FSCACHE_OP_ST_PENDING;
David Howells30ceec62015-02-24 10:05:27 +0000267 flags = READ_ONCE(object->flags);
268 if (unlikely(!(flags & BIT(FSCACHE_OBJECT_IS_LIVE)))) {
269 fscache_stat(&fscache_n_op_rejected);
David Howellsd3b97ca2015-02-24 10:05:29 +0000270 op->cancel(op);
David Howells30ceec62015-02-24 10:05:27 +0000271 op->state = FSCACHE_OP_ST_CANCELLED;
272 ret = -ENOBUFS;
273 } else if (unlikely(fscache_cache_is_broken(object))) {
David Howellsd3b97ca2015-02-24 10:05:29 +0000274 op->cancel(op);
David Howells30ceec62015-02-24 10:05:27 +0000275 op->state = FSCACHE_OP_ST_CANCELLED;
276 ret = -EIO;
277 } else if (flags & BIT(FSCACHE_OBJECT_IS_AVAILABLE)) {
David Howells952efe72009-04-03 16:42:39 +0100278 op->object = object;
279 object->n_ops++;
280
281 if (object->n_exclusive > 0) {
282 atomic_inc(&op->usage);
283 list_add_tail(&op->pend_link, &object->pending_ops);
284 fscache_stat(&fscache_n_op_pend);
285 } else if (!list_empty(&object->pending_ops)) {
286 atomic_inc(&op->usage);
287 list_add_tail(&op->pend_link, &object->pending_ops);
288 fscache_stat(&fscache_n_op_pend);
289 fscache_start_operations(object);
290 } else {
291 ASSERTCMP(object->n_exclusive, ==, 0);
292 fscache_run_op(object, op);
293 }
294 ret = 0;
David Howells30ceec62015-02-24 10:05:27 +0000295 } else if (flags & BIT(FSCACHE_OBJECT_IS_LOOKED_UP)) {
David Howells952efe72009-04-03 16:42:39 +0100296 op->object = object;
297 object->n_ops++;
298 atomic_inc(&op->usage);
299 list_add_tail(&op->pend_link, &object->pending_ops);
300 fscache_stat(&fscache_n_op_pend);
301 ret = 0;
David Howells6515d1d2015-02-25 11:53:57 +0000302 } else if (flags & BIT(FSCACHE_OBJECT_KILLED_BY_CACHE)) {
David Howellsd3b97ca2015-02-24 10:05:29 +0000303 op->cancel(op);
David Howells6515d1d2015-02-25 11:53:57 +0000304 op->state = FSCACHE_OP_ST_CANCELLED;
305 ret = -ENOBUFS;
David Howells30ceec62015-02-24 10:05:27 +0000306 } else {
David Howells952efe72009-04-03 16:42:39 +0100307 fscache_report_unexpected_submission(object, op, ostate);
308 ASSERT(!fscache_object_is_active(object));
David Howellsd3b97ca2015-02-24 10:05:29 +0000309 op->cancel(op);
David Howells9f105232012-12-20 21:52:35 +0000310 op->state = FSCACHE_OP_ST_CANCELLED;
David Howells952efe72009-04-03 16:42:39 +0100311 ret = -ENOBUFS;
David Howells952efe72009-04-03 16:42:39 +0100312 }
313
314 spin_unlock(&object->lock);
315 return ret;
316}
317
318/*
319 * queue an object for withdrawal on error, aborting all following asynchronous
320 * operations
321 */
322void fscache_abort_object(struct fscache_object *object)
323{
324 _enter("{OBJ%x}", object->debug_id);
325
326 fscache_raise_event(object, FSCACHE_OBJECT_EV_ERROR);
327}
328
329/*
David Howellsdcfae322013-05-24 12:45:31 +0100330 * Jump start the operation processing on an object. The caller must hold
331 * object->lock.
David Howells952efe72009-04-03 16:42:39 +0100332 */
333void fscache_start_operations(struct fscache_object *object)
334{
335 struct fscache_operation *op;
336 bool stop = false;
337
338 while (!list_empty(&object->pending_ops) && !stop) {
339 op = list_entry(object->pending_ops.next,
340 struct fscache_operation, pend_link);
341
342 if (test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags)) {
343 if (object->n_in_progress > 0)
344 break;
345 stop = true;
346 }
347 list_del_init(&op->pend_link);
David Howells5753c442009-11-19 18:11:19 +0000348 fscache_run_op(object, op);
David Howells952efe72009-04-03 16:42:39 +0100349
350 /* the pending queue was holding a ref on the object */
351 fscache_put_operation(op);
352 }
353
354 ASSERTCMP(object->n_in_progress, <=, object->n_ops);
355
356 _debug("woke %d ops on OBJ%x",
357 object->n_in_progress, object->debug_id);
358}
359
360/*
David Howells5753c442009-11-19 18:11:19 +0000361 * cancel an operation that's pending on an object
362 */
David Howells91c7fbb2012-12-14 11:02:22 +0000363int fscache_cancel_op(struct fscache_operation *op,
David Howells418b7eb2015-02-24 10:05:28 +0000364 bool cancel_in_progress_op)
David Howells5753c442009-11-19 18:11:19 +0000365{
366 struct fscache_object *object = op->object;
David Howells418b7eb2015-02-24 10:05:28 +0000367 bool put = false;
David Howells5753c442009-11-19 18:11:19 +0000368 int ret;
369
370 _enter("OBJ%x OP%x}", op->object->debug_id, op->debug_id);
371
David Howells08c2e3d2018-04-04 13:41:27 +0100372 trace_fscache_op(object->cookie, op, fscache_op_cancel);
373
David Howells9f105232012-12-20 21:52:35 +0000374 ASSERTCMP(op->state, >=, FSCACHE_OP_ST_PENDING);
375 ASSERTCMP(op->state, !=, FSCACHE_OP_ST_CANCELLED);
376 ASSERTCMP(atomic_read(&op->usage), >, 0);
377
David Howells5753c442009-11-19 18:11:19 +0000378 spin_lock(&object->lock);
379
380 ret = -EBUSY;
David Howells9f105232012-12-20 21:52:35 +0000381 if (op->state == FSCACHE_OP_ST_PENDING) {
382 ASSERT(!list_empty(&op->pend_link));
David Howells5753c442009-11-19 18:11:19 +0000383 list_del_init(&op->pend_link);
David Howells418b7eb2015-02-24 10:05:28 +0000384 put = true;
David Howellsd3b97ca2015-02-24 10:05:29 +0000385
David Howells418b7eb2015-02-24 10:05:28 +0000386 fscache_stat(&fscache_n_op_cancelled);
David Howellsd3b97ca2015-02-24 10:05:29 +0000387 op->cancel(op);
David Howells9f105232012-12-20 21:52:35 +0000388 op->state = FSCACHE_OP_ST_CANCELLED;
David Howells5753c442009-11-19 18:11:19 +0000389 if (test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags))
390 object->n_exclusive--;
391 if (test_and_clear_bit(FSCACHE_OP_WAITING, &op->flags))
392 wake_up_bit(&op->flags, FSCACHE_OP_WAITING);
David Howells418b7eb2015-02-24 10:05:28 +0000393 ret = 0;
394 } else if (op->state == FSCACHE_OP_ST_IN_PROGRESS && cancel_in_progress_op) {
David Howells73c04a42015-02-25 14:07:25 +0000395 ASSERTCMP(object->n_in_progress, >, 0);
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
David Howells418b7eb2015-02-24 10:05:28 +0000402 fscache_stat(&fscache_n_op_cancelled);
David Howellsd3b97ca2015-02-24 10:05:29 +0000403 op->cancel(op);
David Howells418b7eb2015-02-24 10:05:28 +0000404 op->state = FSCACHE_OP_ST_CANCELLED;
405 if (test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags))
406 object->n_exclusive--;
407 if (test_and_clear_bit(FSCACHE_OP_WAITING, &op->flags))
408 wake_up_bit(&op->flags, FSCACHE_OP_WAITING);
David Howells5753c442009-11-19 18:11:19 +0000409 ret = 0;
410 }
411
David Howells418b7eb2015-02-24 10:05:28 +0000412 if (put)
413 fscache_put_operation(op);
David Howells5753c442009-11-19 18:11:19 +0000414 spin_unlock(&object->lock);
415 _leave(" = %d", ret);
416 return ret;
417}
418
419/*
David Howellsef778e72012-12-20 21:52:36 +0000420 * Cancel all pending operations on an object
421 */
422void fscache_cancel_all_ops(struct fscache_object *object)
423{
424 struct fscache_operation *op;
425
426 _enter("OBJ%x", object->debug_id);
427
428 spin_lock(&object->lock);
429
430 while (!list_empty(&object->pending_ops)) {
431 op = list_entry(object->pending_ops.next,
432 struct fscache_operation, pend_link);
433 fscache_stat(&fscache_n_op_cancelled);
434 list_del_init(&op->pend_link);
435
David Howells08c2e3d2018-04-04 13:41:27 +0100436 trace_fscache_op(object->cookie, op, fscache_op_cancel_all);
437
David Howellsef778e72012-12-20 21:52:36 +0000438 ASSERTCMP(op->state, ==, FSCACHE_OP_ST_PENDING);
David Howellsd3b97ca2015-02-24 10:05:29 +0000439 op->cancel(op);
David Howellsef778e72012-12-20 21:52:36 +0000440 op->state = FSCACHE_OP_ST_CANCELLED;
441
442 if (test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags))
443 object->n_exclusive--;
444 if (test_and_clear_bit(FSCACHE_OP_WAITING, &op->flags))
445 wake_up_bit(&op->flags, FSCACHE_OP_WAITING);
446 fscache_put_operation(op);
447 cond_resched_lock(&object->lock);
448 }
449
450 spin_unlock(&object->lock);
451 _leave("");
452}
453
454/*
David Howells1f372df2012-12-13 20:03:13 +0000455 * Record the completion or cancellation of an in-progress operation.
David Howells9f105232012-12-20 21:52:35 +0000456 */
David Howells1f372df2012-12-13 20:03:13 +0000457void fscache_op_complete(struct fscache_operation *op, bool cancelled)
David Howells9f105232012-12-20 21:52:35 +0000458{
459 struct fscache_object *object = op->object;
460
461 _enter("OBJ%x", object->debug_id);
462
463 ASSERTCMP(op->state, ==, FSCACHE_OP_ST_IN_PROGRESS);
464 ASSERTCMP(object->n_in_progress, >, 0);
465 ASSERTIFCMP(test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags),
466 object->n_exclusive, >, 0);
467 ASSERTIFCMP(test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags),
468 object->n_in_progress, ==, 1);
469
470 spin_lock(&object->lock);
471
David Howellsd3b97ca2015-02-24 10:05:29 +0000472 if (!cancelled) {
David Howells08c2e3d2018-04-04 13:41:27 +0100473 trace_fscache_op(object->cookie, op, fscache_op_completed);
David Howellsd3b97ca2015-02-24 10:05:29 +0000474 op->state = FSCACHE_OP_ST_COMPLETE;
475 } else {
476 op->cancel(op);
David Howells08c2e3d2018-04-04 13:41:27 +0100477 trace_fscache_op(object->cookie, op, fscache_op_cancelled);
David Howellsd3b97ca2015-02-24 10:05:29 +0000478 op->state = FSCACHE_OP_ST_CANCELLED;
479 }
David Howells9f105232012-12-20 21:52:35 +0000480
481 if (test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags))
482 object->n_exclusive--;
483 object->n_in_progress--;
484 if (object->n_in_progress == 0)
485 fscache_start_operations(object);
486
487 spin_unlock(&object->lock);
488 _leave("");
489}
490EXPORT_SYMBOL(fscache_op_complete);
491
492/*
David Howells952efe72009-04-03 16:42:39 +0100493 * release an operation
494 * - queues pending ops if this is the last in-progress op
495 */
496void fscache_put_operation(struct fscache_operation *op)
497{
498 struct fscache_object *object;
499 struct fscache_cache *cache;
500
501 _enter("{OBJ%x OP%x,%d}",
502 op->object->debug_id, op->debug_id, atomic_read(&op->usage));
503
504 ASSERTCMP(atomic_read(&op->usage), >, 0);
505
506 if (!atomic_dec_and_test(&op->usage))
507 return;
508
David Howells402cb8d2018-04-04 13:41:28 +0100509 trace_fscache_op(op->object ? op->object->cookie : NULL, op, fscache_op_put);
David Howells08c2e3d2018-04-04 13:41:27 +0100510
David Howells952efe72009-04-03 16:42:39 +0100511 _debug("PUT OP");
David Howellsa39caad2015-02-25 14:22:40 +0000512 ASSERTIFCMP(op->state != FSCACHE_OP_ST_INITIALISED &&
513 op->state != FSCACHE_OP_ST_COMPLETE,
David Howells9f105232012-12-20 21:52:35 +0000514 op->state, ==, FSCACHE_OP_ST_CANCELLED);
David Howells952efe72009-04-03 16:42:39 +0100515
516 fscache_stat(&fscache_n_op_release);
517
518 if (op->release) {
519 op->release(op);
520 op->release = NULL;
521 }
David Howells4a471322015-02-24 10:05:29 +0000522 op->state = FSCACHE_OP_ST_DEAD;
David Howells952efe72009-04-03 16:42:39 +0100523
524 object = op->object;
David Howellsa39caad2015-02-25 14:22:40 +0000525 if (likely(object)) {
526 if (test_bit(FSCACHE_OP_DEC_READ_CNT, &op->flags))
527 atomic_dec(&object->n_reads);
528 if (test_bit(FSCACHE_OP_UNUSE_COOKIE, &op->flags))
529 fscache_unuse_cookie(object);
David Howells952efe72009-04-03 16:42:39 +0100530
David Howellsa39caad2015-02-25 14:22:40 +0000531 /* now... we may get called with the object spinlock held, so we
532 * complete the cleanup here only if we can immediately acquire the
533 * lock, and defer it otherwise */
534 if (!spin_trylock(&object->lock)) {
535 _debug("defer put");
536 fscache_stat(&fscache_n_op_deferred_release);
David Howells4fbf4292009-11-19 18:11:04 +0000537
David Howellsa39caad2015-02-25 14:22:40 +0000538 cache = object->cache;
539 spin_lock(&cache->op_gc_list_lock);
540 list_add_tail(&op->pend_link, &cache->op_gc_list);
541 spin_unlock(&cache->op_gc_list_lock);
542 schedule_work(&cache->op_gc);
543 _leave(" [defer]");
544 return;
545 }
David Howells952efe72009-04-03 16:42:39 +0100546
David Howellsa39caad2015-02-25 14:22:40 +0000547 ASSERTCMP(object->n_ops, >, 0);
548 object->n_ops--;
549 if (object->n_ops == 0)
550 fscache_raise_event(object, FSCACHE_OBJECT_EV_CLEARED);
551
552 spin_unlock(&object->lock);
David Howells952efe72009-04-03 16:42:39 +0100553 }
554
David Howells952efe72009-04-03 16:42:39 +0100555 kfree(op);
556 _leave(" [done]");
557}
558EXPORT_SYMBOL(fscache_put_operation);
559
560/*
561 * garbage collect operations that have had their release deferred
562 */
563void fscache_operation_gc(struct work_struct *work)
564{
565 struct fscache_operation *op;
566 struct fscache_object *object;
567 struct fscache_cache *cache =
568 container_of(work, struct fscache_cache, op_gc);
569 int count = 0;
570
571 _enter("");
572
573 do {
574 spin_lock(&cache->op_gc_list_lock);
575 if (list_empty(&cache->op_gc_list)) {
576 spin_unlock(&cache->op_gc_list_lock);
577 break;
578 }
579
580 op = list_entry(cache->op_gc_list.next,
581 struct fscache_operation, pend_link);
582 list_del(&op->pend_link);
583 spin_unlock(&cache->op_gc_list_lock);
584
585 object = op->object;
David Howells08c2e3d2018-04-04 13:41:27 +0100586 trace_fscache_op(object->cookie, op, fscache_op_gc);
587
David Howells9f105232012-12-20 21:52:35 +0000588 spin_lock(&object->lock);
David Howells952efe72009-04-03 16:42:39 +0100589
590 _debug("GC DEFERRED REL OBJ%x OP%x",
591 object->debug_id, op->debug_id);
592 fscache_stat(&fscache_n_op_gc);
593
594 ASSERTCMP(atomic_read(&op->usage), ==, 0);
David Howells9f105232012-12-20 21:52:35 +0000595 ASSERTCMP(op->state, ==, FSCACHE_OP_ST_DEAD);
David Howells952efe72009-04-03 16:42:39 +0100596
597 ASSERTCMP(object->n_ops, >, 0);
598 object->n_ops--;
599 if (object->n_ops == 0)
600 fscache_raise_event(object, FSCACHE_OBJECT_EV_CLEARED);
601
602 spin_unlock(&object->lock);
David Howells9f105232012-12-20 21:52:35 +0000603 kfree(op);
David Howells952efe72009-04-03 16:42:39 +0100604
605 } while (count++ < 20);
606
607 if (!list_empty(&cache->op_gc_list))
608 schedule_work(&cache->op_gc);
609
610 _leave("");
611}
612
613/*
Tejun Heo8af7c122010-07-20 22:09:01 +0200614 * execute an operation using fs_op_wq to provide processing context -
615 * the caller holds a ref to this object, so we don't need to hold one
David Howells952efe72009-04-03 16:42:39 +0100616 */
Tejun Heo8af7c122010-07-20 22:09:01 +0200617void fscache_op_work_func(struct work_struct *work)
David Howells952efe72009-04-03 16:42:39 +0100618{
619 struct fscache_operation *op =
Tejun Heo8af7c122010-07-20 22:09:01 +0200620 container_of(work, struct fscache_operation, work);
David Howells952efe72009-04-03 16:42:39 +0100621 unsigned long start;
622
623 _enter("{OBJ%x OP%x,%d}",
624 op->object->debug_id, op->debug_id, atomic_read(&op->usage));
625
David Howells08c2e3d2018-04-04 13:41:27 +0100626 trace_fscache_op(op->object->cookie, op, fscache_op_work);
627
David Howells952efe72009-04-03 16:42:39 +0100628 ASSERT(op->processor != NULL);
629 start = jiffies;
630 op->processor(op);
631 fscache_hist(fscache_ops_histogram, start);
Tejun Heo8af7c122010-07-20 22:09:01 +0200632 fscache_put_operation(op);
David Howells952efe72009-04-03 16:42:39 +0100633
634 _leave("");
635}