blob: e7b87a0e5185b53ac304d50b46daf14d8149356c [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
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 */
31void 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 Howells5753c442009-11-19 18:11:19 +000036 ASSERT(list_empty(&op->pend_link));
David Howells952efe72009-04-03 16:42:39 +010037 ASSERT(op->processor != NULL);
David Howells493f7bc2013-05-10 19:50:26 +010038 ASSERT(fscache_object_is_available(op->object));
David Howells952efe72009-04-03 16:42:39 +010039 ASSERTCMP(atomic_read(&op->usage), >, 0);
David Howells9f105232012-12-20 21:52:35 +000040 ASSERTCMP(op->state, ==, FSCACHE_OP_ST_IN_PROGRESS);
David Howells952efe72009-04-03 16:42:39 +010041
David Howells5753c442009-11-19 18:11:19 +000042 fscache_stat(&fscache_n_op_enqueue);
43 switch (op->flags & FSCACHE_OP_TYPE) {
Tejun Heo8af7c122010-07-20 22:09:01 +020044 case FSCACHE_OP_ASYNC:
45 _debug("queue async");
David Howells5753c442009-11-19 18:11:19 +000046 atomic_inc(&op->usage);
Tejun Heo8af7c122010-07-20 22:09:01 +020047 if (!queue_work(fscache_op_wq, &op->work))
David Howells5753c442009-11-19 18:11:19 +000048 fscache_put_operation(op);
49 break;
David Howells5753c442009-11-19 18:11:19 +000050 case FSCACHE_OP_MYTHREAD:
51 _debug("queue for caller's attention");
52 break;
53 default:
Fabian Frederick36dfd112014-06-04 16:05:38 -070054 pr_err("Unexpected op type %lx", op->flags);
David Howells5753c442009-11-19 18:11:19 +000055 BUG();
56 break;
David Howells952efe72009-04-03 16:42:39 +010057 }
58}
59EXPORT_SYMBOL(fscache_enqueue_operation);
60
61/*
62 * start an op running
63 */
64static void fscache_run_op(struct fscache_object *object,
65 struct fscache_operation *op)
66{
David Howells9f105232012-12-20 21:52:35 +000067 ASSERTCMP(op->state, ==, FSCACHE_OP_ST_PENDING);
68
69 op->state = FSCACHE_OP_ST_IN_PROGRESS;
David Howells952efe72009-04-03 16:42:39 +010070 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/*
79 * submit an exclusive operation for an object
80 * - other ops are excluded from running simultaneously with this one
81 * - this gets any extra refs it needs on an op
82 */
83int fscache_submit_exclusive_op(struct fscache_object *object,
84 struct fscache_operation *op)
85{
David Howells8d763492012-12-05 13:34:48 +000086 int ret;
87
David Howells952efe72009-04-03 16:42:39 +010088 _enter("{OBJ%x OP%x},", object->debug_id, op->debug_id);
89
David Howells9f105232012-12-20 21:52:35 +000090 ASSERTCMP(op->state, ==, FSCACHE_OP_ST_INITIALISED);
91 ASSERTCMP(atomic_read(&op->usage), >, 0);
92
David Howells952efe72009-04-03 16:42:39 +010093 spin_lock(&object->lock);
94 ASSERTCMP(object->n_ops, >=, object->n_in_progress);
95 ASSERTCMP(object->n_ops, >=, object->n_exclusive);
David Howells5753c442009-11-19 18:11:19 +000096 ASSERT(list_empty(&op->pend_link));
David Howells952efe72009-04-03 16:42:39 +010097
David Howells9f105232012-12-20 21:52:35 +000098 op->state = FSCACHE_OP_ST_PENDING;
David Howells952efe72009-04-03 16:42:39 +010099 if (fscache_object_is_active(object)) {
100 op->object = object;
101 object->n_ops++;
102 object->n_exclusive++; /* reads and writes must wait */
103
David Howells9f105232012-12-20 21:52:35 +0000104 if (object->n_in_progress > 0) {
David Howells952efe72009-04-03 16:42:39 +0100105 atomic_inc(&op->usage);
106 list_add_tail(&op->pend_link, &object->pending_ops);
107 fscache_stat(&fscache_n_op_pend);
108 } else if (!list_empty(&object->pending_ops)) {
109 atomic_inc(&op->usage);
110 list_add_tail(&op->pend_link, &object->pending_ops);
111 fscache_stat(&fscache_n_op_pend);
112 fscache_start_operations(object);
113 } else {
114 ASSERTCMP(object->n_in_progress, ==, 0);
115 fscache_run_op(object, op);
116 }
117
118 /* need to issue a new write op after this */
119 clear_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags);
David Howells8d763492012-12-05 13:34:48 +0000120 ret = 0;
David Howellscaaef692013-05-10 19:50:26 +0100121 } else if (test_bit(FSCACHE_OBJECT_IS_LOOKED_UP, &object->flags)) {
David Howells952efe72009-04-03 16:42:39 +0100122 op->object = object;
123 object->n_ops++;
124 object->n_exclusive++; /* reads and writes must wait */
125 atomic_inc(&op->usage);
126 list_add_tail(&op->pend_link, &object->pending_ops);
127 fscache_stat(&fscache_n_op_pend);
David Howells8d763492012-12-05 13:34:48 +0000128 ret = 0;
David Howells952efe72009-04-03 16:42:39 +0100129 } else {
David Howells8d763492012-12-05 13:34:48 +0000130 /* If we're in any other state, there must have been an I/O
131 * error of some nature.
132 */
133 ASSERT(test_bit(FSCACHE_IOERROR, &object->cache->flags));
134 ret = -EIO;
David Howells952efe72009-04-03 16:42:39 +0100135 }
136
137 spin_unlock(&object->lock);
David Howells8d763492012-12-05 13:34:48 +0000138 return ret;
David Howells952efe72009-04-03 16:42:39 +0100139}
140
141/*
142 * report an unexpected submission
143 */
144static void fscache_report_unexpected_submission(struct fscache_object *object,
145 struct fscache_operation *op,
David Howellscaaef692013-05-10 19:50:26 +0100146 const struct fscache_state *ostate)
David Howells952efe72009-04-03 16:42:39 +0100147{
148 static bool once_only;
149 struct fscache_operation *p;
150 unsigned n;
151
152 if (once_only)
153 return;
154 once_only = true;
155
156 kdebug("unexpected submission OP%x [OBJ%x %s]",
David Howellscaaef692013-05-10 19:50:26 +0100157 op->debug_id, object->debug_id, object->state->name);
158 kdebug("objstate=%s [%s]", object->state->name, ostate->name);
David Howells952efe72009-04-03 16:42:39 +0100159 kdebug("objflags=%lx", object->flags);
160 kdebug("objevent=%lx [%lx]", object->events, object->event_mask);
161 kdebug("ops=%u inp=%u exc=%u",
162 object->n_ops, object->n_in_progress, object->n_exclusive);
163
164 if (!list_empty(&object->pending_ops)) {
165 n = 0;
166 list_for_each_entry(p, &object->pending_ops, pend_link) {
167 ASSERTCMP(p->object, ==, object);
168 kdebug("%p %p", op->processor, op->release);
169 n++;
170 }
171
172 kdebug("n=%u", n);
173 }
174
175 dump_stack();
176}
177
178/*
179 * submit an operation for an object
180 * - objects may be submitted only in the following states:
181 * - during object creation (write ops may be submitted)
182 * - whilst the object is active
183 * - after an I/O error incurred in one of the two above states (op rejected)
184 * - this gets any extra refs it needs on an op
185 */
186int fscache_submit_op(struct fscache_object *object,
187 struct fscache_operation *op)
188{
David Howellscaaef692013-05-10 19:50:26 +0100189 const struct fscache_state *ostate;
David Howells952efe72009-04-03 16:42:39 +0100190 int ret;
191
192 _enter("{OBJ%x OP%x},{%u}",
193 object->debug_id, op->debug_id, atomic_read(&op->usage));
194
David Howells9f105232012-12-20 21:52:35 +0000195 ASSERTCMP(op->state, ==, FSCACHE_OP_ST_INITIALISED);
David Howells952efe72009-04-03 16:42:39 +0100196 ASSERTCMP(atomic_read(&op->usage), >, 0);
197
198 spin_lock(&object->lock);
199 ASSERTCMP(object->n_ops, >=, object->n_in_progress);
200 ASSERTCMP(object->n_ops, >=, object->n_exclusive);
David Howells5753c442009-11-19 18:11:19 +0000201 ASSERT(list_empty(&op->pend_link));
David Howells952efe72009-04-03 16:42:39 +0100202
203 ostate = object->state;
204 smp_rmb();
205
David Howells9f105232012-12-20 21:52:35 +0000206 op->state = FSCACHE_OP_ST_PENDING;
David Howells952efe72009-04-03 16:42:39 +0100207 if (fscache_object_is_active(object)) {
208 op->object = object;
209 object->n_ops++;
210
211 if (object->n_exclusive > 0) {
212 atomic_inc(&op->usage);
213 list_add_tail(&op->pend_link, &object->pending_ops);
214 fscache_stat(&fscache_n_op_pend);
215 } else if (!list_empty(&object->pending_ops)) {
216 atomic_inc(&op->usage);
217 list_add_tail(&op->pend_link, &object->pending_ops);
218 fscache_stat(&fscache_n_op_pend);
219 fscache_start_operations(object);
220 } else {
221 ASSERTCMP(object->n_exclusive, ==, 0);
222 fscache_run_op(object, op);
223 }
224 ret = 0;
David Howellscaaef692013-05-10 19:50:26 +0100225 } else if (test_bit(FSCACHE_OBJECT_IS_LOOKED_UP, &object->flags)) {
David Howells952efe72009-04-03 16:42:39 +0100226 op->object = object;
227 object->n_ops++;
228 atomic_inc(&op->usage);
229 list_add_tail(&op->pend_link, &object->pending_ops);
230 fscache_stat(&fscache_n_op_pend);
231 ret = 0;
David Howellscaaef692013-05-10 19:50:26 +0100232 } else if (fscache_object_is_dying(object)) {
David Howellse3d4d282009-11-19 18:11:32 +0000233 fscache_stat(&fscache_n_op_rejected);
David Howells9f105232012-12-20 21:52:35 +0000234 op->state = FSCACHE_OP_ST_CANCELLED;
David Howellse3d4d282009-11-19 18:11:32 +0000235 ret = -ENOBUFS;
David Howells952efe72009-04-03 16:42:39 +0100236 } else if (!test_bit(FSCACHE_IOERROR, &object->cache->flags)) {
237 fscache_report_unexpected_submission(object, op, ostate);
238 ASSERT(!fscache_object_is_active(object));
David Howells9f105232012-12-20 21:52:35 +0000239 op->state = FSCACHE_OP_ST_CANCELLED;
David Howells952efe72009-04-03 16:42:39 +0100240 ret = -ENOBUFS;
241 } else {
David Howells9f105232012-12-20 21:52:35 +0000242 op->state = FSCACHE_OP_ST_CANCELLED;
David Howells952efe72009-04-03 16:42:39 +0100243 ret = -ENOBUFS;
244 }
245
246 spin_unlock(&object->lock);
247 return ret;
248}
249
250/*
251 * queue an object for withdrawal on error, aborting all following asynchronous
252 * operations
253 */
254void fscache_abort_object(struct fscache_object *object)
255{
256 _enter("{OBJ%x}", object->debug_id);
257
258 fscache_raise_event(object, FSCACHE_OBJECT_EV_ERROR);
259}
260
261/*
David Howellsdcfae322013-05-24 12:45:31 +0100262 * Jump start the operation processing on an object. The caller must hold
263 * object->lock.
David Howells952efe72009-04-03 16:42:39 +0100264 */
265void fscache_start_operations(struct fscache_object *object)
266{
267 struct fscache_operation *op;
268 bool stop = false;
269
270 while (!list_empty(&object->pending_ops) && !stop) {
271 op = list_entry(object->pending_ops.next,
272 struct fscache_operation, pend_link);
273
274 if (test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags)) {
275 if (object->n_in_progress > 0)
276 break;
277 stop = true;
278 }
279 list_del_init(&op->pend_link);
David Howells5753c442009-11-19 18:11:19 +0000280 fscache_run_op(object, op);
David Howells952efe72009-04-03 16:42:39 +0100281
282 /* the pending queue was holding a ref on the object */
283 fscache_put_operation(op);
284 }
285
286 ASSERTCMP(object->n_in_progress, <=, object->n_ops);
287
288 _debug("woke %d ops on OBJ%x",
289 object->n_in_progress, object->debug_id);
290}
291
292/*
David Howells5753c442009-11-19 18:11:19 +0000293 * cancel an operation that's pending on an object
294 */
David Howells91c7fbb2012-12-14 11:02:22 +0000295int fscache_cancel_op(struct fscache_operation *op,
296 void (*do_cancel)(struct fscache_operation *))
David Howells5753c442009-11-19 18:11:19 +0000297{
298 struct fscache_object *object = op->object;
299 int ret;
300
301 _enter("OBJ%x OP%x}", op->object->debug_id, op->debug_id);
302
David Howells9f105232012-12-20 21:52:35 +0000303 ASSERTCMP(op->state, >=, FSCACHE_OP_ST_PENDING);
304 ASSERTCMP(op->state, !=, FSCACHE_OP_ST_CANCELLED);
305 ASSERTCMP(atomic_read(&op->usage), >, 0);
306
David Howells5753c442009-11-19 18:11:19 +0000307 spin_lock(&object->lock);
308
309 ret = -EBUSY;
David Howells9f105232012-12-20 21:52:35 +0000310 if (op->state == FSCACHE_OP_ST_PENDING) {
311 ASSERT(!list_empty(&op->pend_link));
David Howells5753c442009-11-19 18:11:19 +0000312 fscache_stat(&fscache_n_op_cancelled);
313 list_del_init(&op->pend_link);
David Howells91c7fbb2012-12-14 11:02:22 +0000314 if (do_cancel)
315 do_cancel(op);
David Howells9f105232012-12-20 21:52:35 +0000316 op->state = FSCACHE_OP_ST_CANCELLED;
David Howells5753c442009-11-19 18:11:19 +0000317 if (test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags))
318 object->n_exclusive--;
319 if (test_and_clear_bit(FSCACHE_OP_WAITING, &op->flags))
320 wake_up_bit(&op->flags, FSCACHE_OP_WAITING);
321 fscache_put_operation(op);
322 ret = 0;
323 }
324
325 spin_unlock(&object->lock);
326 _leave(" = %d", ret);
327 return ret;
328}
329
330/*
David Howellsef778e72012-12-20 21:52:36 +0000331 * Cancel all pending operations on an object
332 */
333void fscache_cancel_all_ops(struct fscache_object *object)
334{
335 struct fscache_operation *op;
336
337 _enter("OBJ%x", object->debug_id);
338
339 spin_lock(&object->lock);
340
341 while (!list_empty(&object->pending_ops)) {
342 op = list_entry(object->pending_ops.next,
343 struct fscache_operation, pend_link);
344 fscache_stat(&fscache_n_op_cancelled);
345 list_del_init(&op->pend_link);
346
347 ASSERTCMP(op->state, ==, FSCACHE_OP_ST_PENDING);
348 op->state = FSCACHE_OP_ST_CANCELLED;
349
350 if (test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags))
351 object->n_exclusive--;
352 if (test_and_clear_bit(FSCACHE_OP_WAITING, &op->flags))
353 wake_up_bit(&op->flags, FSCACHE_OP_WAITING);
354 fscache_put_operation(op);
355 cond_resched_lock(&object->lock);
356 }
357
358 spin_unlock(&object->lock);
359 _leave("");
360}
361
362/*
David Howells1f372df2012-12-13 20:03:13 +0000363 * Record the completion or cancellation of an in-progress operation.
David Howells9f105232012-12-20 21:52:35 +0000364 */
David Howells1f372df2012-12-13 20:03:13 +0000365void fscache_op_complete(struct fscache_operation *op, bool cancelled)
David Howells9f105232012-12-20 21:52:35 +0000366{
367 struct fscache_object *object = op->object;
368
369 _enter("OBJ%x", object->debug_id);
370
371 ASSERTCMP(op->state, ==, FSCACHE_OP_ST_IN_PROGRESS);
372 ASSERTCMP(object->n_in_progress, >, 0);
373 ASSERTIFCMP(test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags),
374 object->n_exclusive, >, 0);
375 ASSERTIFCMP(test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags),
376 object->n_in_progress, ==, 1);
377
378 spin_lock(&object->lock);
379
David Howells1f372df2012-12-13 20:03:13 +0000380 op->state = cancelled ?
381 FSCACHE_OP_ST_CANCELLED : FSCACHE_OP_ST_COMPLETE;
David Howells9f105232012-12-20 21:52:35 +0000382
383 if (test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags))
384 object->n_exclusive--;
385 object->n_in_progress--;
386 if (object->n_in_progress == 0)
387 fscache_start_operations(object);
388
389 spin_unlock(&object->lock);
390 _leave("");
391}
392EXPORT_SYMBOL(fscache_op_complete);
393
394/*
David Howells952efe72009-04-03 16:42:39 +0100395 * release an operation
396 * - queues pending ops if this is the last in-progress op
397 */
398void fscache_put_operation(struct fscache_operation *op)
399{
400 struct fscache_object *object;
401 struct fscache_cache *cache;
402
403 _enter("{OBJ%x OP%x,%d}",
404 op->object->debug_id, op->debug_id, atomic_read(&op->usage));
405
406 ASSERTCMP(atomic_read(&op->usage), >, 0);
407
408 if (!atomic_dec_and_test(&op->usage))
409 return;
410
411 _debug("PUT OP");
David Howells9f105232012-12-20 21:52:35 +0000412 ASSERTIFCMP(op->state != FSCACHE_OP_ST_COMPLETE,
413 op->state, ==, FSCACHE_OP_ST_CANCELLED);
414 op->state = FSCACHE_OP_ST_DEAD;
David Howells952efe72009-04-03 16:42:39 +0100415
416 fscache_stat(&fscache_n_op_release);
417
418 if (op->release) {
419 op->release(op);
420 op->release = NULL;
421 }
422
423 object = op->object;
424
David Howells13627292013-05-10 19:50:26 +0100425 if (test_bit(FSCACHE_OP_DEC_READ_CNT, &op->flags))
426 atomic_dec(&object->n_reads);
427 if (test_bit(FSCACHE_OP_UNUSE_COOKIE, &op->flags))
428 fscache_unuse_cookie(object);
David Howells4fbf4292009-11-19 18:11:04 +0000429
David Howells952efe72009-04-03 16:42:39 +0100430 /* now... we may get called with the object spinlock held, so we
431 * complete the cleanup here only if we can immediately acquire the
432 * lock, and defer it otherwise */
433 if (!spin_trylock(&object->lock)) {
434 _debug("defer put");
435 fscache_stat(&fscache_n_op_deferred_release);
436
437 cache = object->cache;
438 spin_lock(&cache->op_gc_list_lock);
439 list_add_tail(&op->pend_link, &cache->op_gc_list);
440 spin_unlock(&cache->op_gc_list_lock);
441 schedule_work(&cache->op_gc);
442 _leave(" [defer]");
443 return;
444 }
445
David Howells952efe72009-04-03 16:42:39 +0100446 ASSERTCMP(object->n_ops, >, 0);
447 object->n_ops--;
448 if (object->n_ops == 0)
449 fscache_raise_event(object, FSCACHE_OBJECT_EV_CLEARED);
450
451 spin_unlock(&object->lock);
452
453 kfree(op);
454 _leave(" [done]");
455}
456EXPORT_SYMBOL(fscache_put_operation);
457
458/*
459 * garbage collect operations that have had their release deferred
460 */
461void fscache_operation_gc(struct work_struct *work)
462{
463 struct fscache_operation *op;
464 struct fscache_object *object;
465 struct fscache_cache *cache =
466 container_of(work, struct fscache_cache, op_gc);
467 int count = 0;
468
469 _enter("");
470
471 do {
472 spin_lock(&cache->op_gc_list_lock);
473 if (list_empty(&cache->op_gc_list)) {
474 spin_unlock(&cache->op_gc_list_lock);
475 break;
476 }
477
478 op = list_entry(cache->op_gc_list.next,
479 struct fscache_operation, pend_link);
480 list_del(&op->pend_link);
481 spin_unlock(&cache->op_gc_list_lock);
482
483 object = op->object;
David Howells9f105232012-12-20 21:52:35 +0000484 spin_lock(&object->lock);
David Howells952efe72009-04-03 16:42:39 +0100485
486 _debug("GC DEFERRED REL OBJ%x OP%x",
487 object->debug_id, op->debug_id);
488 fscache_stat(&fscache_n_op_gc);
489
490 ASSERTCMP(atomic_read(&op->usage), ==, 0);
David Howells9f105232012-12-20 21:52:35 +0000491 ASSERTCMP(op->state, ==, FSCACHE_OP_ST_DEAD);
David Howells952efe72009-04-03 16:42:39 +0100492
493 ASSERTCMP(object->n_ops, >, 0);
494 object->n_ops--;
495 if (object->n_ops == 0)
496 fscache_raise_event(object, FSCACHE_OBJECT_EV_CLEARED);
497
498 spin_unlock(&object->lock);
David Howells9f105232012-12-20 21:52:35 +0000499 kfree(op);
David Howells952efe72009-04-03 16:42:39 +0100500
501 } while (count++ < 20);
502
503 if (!list_empty(&cache->op_gc_list))
504 schedule_work(&cache->op_gc);
505
506 _leave("");
507}
508
509/*
Tejun Heo8af7c122010-07-20 22:09:01 +0200510 * execute an operation using fs_op_wq to provide processing context -
511 * the caller holds a ref to this object, so we don't need to hold one
David Howells952efe72009-04-03 16:42:39 +0100512 */
Tejun Heo8af7c122010-07-20 22:09:01 +0200513void fscache_op_work_func(struct work_struct *work)
David Howells952efe72009-04-03 16:42:39 +0100514{
515 struct fscache_operation *op =
Tejun Heo8af7c122010-07-20 22:09:01 +0200516 container_of(work, struct fscache_operation, work);
David Howells952efe72009-04-03 16:42:39 +0100517 unsigned long start;
518
519 _enter("{OBJ%x OP%x,%d}",
520 op->object->debug_id, op->debug_id, atomic_read(&op->usage));
521
522 ASSERT(op->processor != NULL);
523 start = jiffies;
524 op->processor(op);
525 fscache_hist(fscache_ops_histogram, start);
Tejun Heo8af7c122010-07-20 22:09:01 +0200526 fscache_put_operation(op);
David Howells952efe72009-04-03 16:42:39 +0100527
528 _leave("");
529}