blob: 36c59604130dc0b5906b67a1855da5f3c988584a [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);
38 ASSERTCMP(op->object->state, >=, FSCACHE_OBJECT_AVAILABLE);
39 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:
54 printk(KERN_ERR "FS-Cache: Unexpected op type %lx",
55 op->flags);
56 BUG();
57 break;
David Howells952efe72009-04-03 16:42:39 +010058 }
59}
60EXPORT_SYMBOL(fscache_enqueue_operation);
61
62/*
63 * start an op running
64 */
65static void fscache_run_op(struct fscache_object *object,
66 struct fscache_operation *op)
67{
David Howells9f105232012-12-20 21:52:35 +000068 ASSERTCMP(op->state, ==, FSCACHE_OP_ST_PENDING);
69
70 op->state = FSCACHE_OP_ST_IN_PROGRESS;
David Howells952efe72009-04-03 16:42:39 +010071 object->n_in_progress++;
72 if (test_and_clear_bit(FSCACHE_OP_WAITING, &op->flags))
73 wake_up_bit(&op->flags, FSCACHE_OP_WAITING);
74 if (op->processor)
75 fscache_enqueue_operation(op);
76 fscache_stat(&fscache_n_op_run);
77}
78
79/*
80 * submit an exclusive operation for an object
81 * - other ops are excluded from running simultaneously with this one
82 * - this gets any extra refs it needs on an op
83 */
84int fscache_submit_exclusive_op(struct fscache_object *object,
85 struct fscache_operation *op)
86{
David Howells8d763492012-12-05 13:34:48 +000087 int ret;
88
David Howells952efe72009-04-03 16:42:39 +010089 _enter("{OBJ%x OP%x},", object->debug_id, op->debug_id);
90
David Howells9f105232012-12-20 21:52:35 +000091 ASSERTCMP(op->state, ==, FSCACHE_OP_ST_INITIALISED);
92 ASSERTCMP(atomic_read(&op->usage), >, 0);
93
David Howells952efe72009-04-03 16:42:39 +010094 spin_lock(&object->lock);
95 ASSERTCMP(object->n_ops, >=, object->n_in_progress);
96 ASSERTCMP(object->n_ops, >=, object->n_exclusive);
David Howells5753c442009-11-19 18:11:19 +000097 ASSERT(list_empty(&op->pend_link));
David Howells952efe72009-04-03 16:42:39 +010098
David Howells9f105232012-12-20 21:52:35 +000099 op->state = FSCACHE_OP_ST_PENDING;
David Howells952efe72009-04-03 16:42:39 +0100100 if (fscache_object_is_active(object)) {
101 op->object = object;
102 object->n_ops++;
103 object->n_exclusive++; /* reads and writes must wait */
104
David Howells9f105232012-12-20 21:52:35 +0000105 if (object->n_in_progress > 0) {
David Howells952efe72009-04-03 16:42:39 +0100106 atomic_inc(&op->usage);
107 list_add_tail(&op->pend_link, &object->pending_ops);
108 fscache_stat(&fscache_n_op_pend);
109 } else if (!list_empty(&object->pending_ops)) {
110 atomic_inc(&op->usage);
111 list_add_tail(&op->pend_link, &object->pending_ops);
112 fscache_stat(&fscache_n_op_pend);
113 fscache_start_operations(object);
114 } else {
115 ASSERTCMP(object->n_in_progress, ==, 0);
116 fscache_run_op(object, op);
117 }
118
119 /* need to issue a new write op after this */
120 clear_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags);
David Howells8d763492012-12-05 13:34:48 +0000121 ret = 0;
David Howells952efe72009-04-03 16:42:39 +0100122 } else if (object->state == FSCACHE_OBJECT_CREATING) {
123 op->object = object;
124 object->n_ops++;
125 object->n_exclusive++; /* reads and writes must wait */
126 atomic_inc(&op->usage);
127 list_add_tail(&op->pend_link, &object->pending_ops);
128 fscache_stat(&fscache_n_op_pend);
David Howells8d763492012-12-05 13:34:48 +0000129 ret = 0;
David Howells952efe72009-04-03 16:42:39 +0100130 } else {
David Howells8d763492012-12-05 13:34:48 +0000131 /* If we're in any other state, there must have been an I/O
132 * error of some nature.
133 */
134 ASSERT(test_bit(FSCACHE_IOERROR, &object->cache->flags));
135 ret = -EIO;
David Howells952efe72009-04-03 16:42:39 +0100136 }
137
138 spin_unlock(&object->lock);
David Howells8d763492012-12-05 13:34:48 +0000139 return ret;
David Howells952efe72009-04-03 16:42:39 +0100140}
141
142/*
143 * report an unexpected submission
144 */
145static void fscache_report_unexpected_submission(struct fscache_object *object,
146 struct fscache_operation *op,
147 unsigned long ostate)
148{
149 static bool once_only;
150 struct fscache_operation *p;
151 unsigned n;
152
153 if (once_only)
154 return;
155 once_only = true;
156
157 kdebug("unexpected submission OP%x [OBJ%x %s]",
158 op->debug_id, object->debug_id,
159 fscache_object_states[object->state]);
160 kdebug("objstate=%s [%s]",
161 fscache_object_states[object->state],
162 fscache_object_states[ostate]);
163 kdebug("objflags=%lx", object->flags);
164 kdebug("objevent=%lx [%lx]", object->events, object->event_mask);
165 kdebug("ops=%u inp=%u exc=%u",
166 object->n_ops, object->n_in_progress, object->n_exclusive);
167
168 if (!list_empty(&object->pending_ops)) {
169 n = 0;
170 list_for_each_entry(p, &object->pending_ops, pend_link) {
171 ASSERTCMP(p->object, ==, object);
172 kdebug("%p %p", op->processor, op->release);
173 n++;
174 }
175
176 kdebug("n=%u", n);
177 }
178
179 dump_stack();
180}
181
182/*
183 * submit an operation for an object
184 * - objects may be submitted only in the following states:
185 * - during object creation (write ops may be submitted)
186 * - whilst the object is active
187 * - after an I/O error incurred in one of the two above states (op rejected)
188 * - this gets any extra refs it needs on an op
189 */
190int fscache_submit_op(struct fscache_object *object,
191 struct fscache_operation *op)
192{
193 unsigned long ostate;
194 int ret;
195
196 _enter("{OBJ%x OP%x},{%u}",
197 object->debug_id, op->debug_id, atomic_read(&op->usage));
198
David Howells9f105232012-12-20 21:52:35 +0000199 ASSERTCMP(op->state, ==, FSCACHE_OP_ST_INITIALISED);
David Howells952efe72009-04-03 16:42:39 +0100200 ASSERTCMP(atomic_read(&op->usage), >, 0);
201
202 spin_lock(&object->lock);
203 ASSERTCMP(object->n_ops, >=, object->n_in_progress);
204 ASSERTCMP(object->n_ops, >=, object->n_exclusive);
David Howells5753c442009-11-19 18:11:19 +0000205 ASSERT(list_empty(&op->pend_link));
David Howells952efe72009-04-03 16:42:39 +0100206
207 ostate = object->state;
208 smp_rmb();
209
David Howells9f105232012-12-20 21:52:35 +0000210 op->state = FSCACHE_OP_ST_PENDING;
David Howells952efe72009-04-03 16:42:39 +0100211 if (fscache_object_is_active(object)) {
212 op->object = object;
213 object->n_ops++;
214
215 if (object->n_exclusive > 0) {
216 atomic_inc(&op->usage);
217 list_add_tail(&op->pend_link, &object->pending_ops);
218 fscache_stat(&fscache_n_op_pend);
219 } else if (!list_empty(&object->pending_ops)) {
220 atomic_inc(&op->usage);
221 list_add_tail(&op->pend_link, &object->pending_ops);
222 fscache_stat(&fscache_n_op_pend);
223 fscache_start_operations(object);
224 } else {
225 ASSERTCMP(object->n_exclusive, ==, 0);
226 fscache_run_op(object, op);
227 }
228 ret = 0;
229 } else if (object->state == FSCACHE_OBJECT_CREATING) {
230 op->object = object;
231 object->n_ops++;
232 atomic_inc(&op->usage);
233 list_add_tail(&op->pend_link, &object->pending_ops);
234 fscache_stat(&fscache_n_op_pend);
235 ret = 0;
David Howellse3d4d282009-11-19 18:11:32 +0000236 } else if (object->state == FSCACHE_OBJECT_DYING ||
237 object->state == FSCACHE_OBJECT_LC_DYING ||
238 object->state == FSCACHE_OBJECT_WITHDRAWING) {
239 fscache_stat(&fscache_n_op_rejected);
David Howells9f105232012-12-20 21:52:35 +0000240 op->state = FSCACHE_OP_ST_CANCELLED;
David Howellse3d4d282009-11-19 18:11:32 +0000241 ret = -ENOBUFS;
David Howells952efe72009-04-03 16:42:39 +0100242 } else if (!test_bit(FSCACHE_IOERROR, &object->cache->flags)) {
243 fscache_report_unexpected_submission(object, op, ostate);
244 ASSERT(!fscache_object_is_active(object));
David Howells9f105232012-12-20 21:52:35 +0000245 op->state = FSCACHE_OP_ST_CANCELLED;
David Howells952efe72009-04-03 16:42:39 +0100246 ret = -ENOBUFS;
247 } else {
David Howells9f105232012-12-20 21:52:35 +0000248 op->state = FSCACHE_OP_ST_CANCELLED;
David Howells952efe72009-04-03 16:42:39 +0100249 ret = -ENOBUFS;
250 }
251
252 spin_unlock(&object->lock);
253 return ret;
254}
255
256/*
257 * queue an object for withdrawal on error, aborting all following asynchronous
258 * operations
259 */
260void fscache_abort_object(struct fscache_object *object)
261{
262 _enter("{OBJ%x}", object->debug_id);
263
264 fscache_raise_event(object, FSCACHE_OBJECT_EV_ERROR);
265}
266
267/*
268 * jump start the operation processing on an object
269 * - caller must hold object->lock
270 */
271void fscache_start_operations(struct fscache_object *object)
272{
273 struct fscache_operation *op;
274 bool stop = false;
275
276 while (!list_empty(&object->pending_ops) && !stop) {
277 op = list_entry(object->pending_ops.next,
278 struct fscache_operation, pend_link);
279
280 if (test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags)) {
281 if (object->n_in_progress > 0)
282 break;
283 stop = true;
284 }
285 list_del_init(&op->pend_link);
David Howells5753c442009-11-19 18:11:19 +0000286 fscache_run_op(object, op);
David Howells952efe72009-04-03 16:42:39 +0100287
288 /* the pending queue was holding a ref on the object */
289 fscache_put_operation(op);
290 }
291
292 ASSERTCMP(object->n_in_progress, <=, object->n_ops);
293
294 _debug("woke %d ops on OBJ%x",
295 object->n_in_progress, object->debug_id);
296}
297
298/*
David Howells5753c442009-11-19 18:11:19 +0000299 * cancel an operation that's pending on an object
300 */
301int fscache_cancel_op(struct fscache_operation *op)
302{
303 struct fscache_object *object = op->object;
304 int ret;
305
306 _enter("OBJ%x OP%x}", op->object->debug_id, op->debug_id);
307
David Howells9f105232012-12-20 21:52:35 +0000308 ASSERTCMP(op->state, >=, FSCACHE_OP_ST_PENDING);
309 ASSERTCMP(op->state, !=, FSCACHE_OP_ST_CANCELLED);
310 ASSERTCMP(atomic_read(&op->usage), >, 0);
311
David Howells5753c442009-11-19 18:11:19 +0000312 spin_lock(&object->lock);
313
314 ret = -EBUSY;
David Howells9f105232012-12-20 21:52:35 +0000315 if (op->state == FSCACHE_OP_ST_PENDING) {
316 ASSERT(!list_empty(&op->pend_link));
David Howells5753c442009-11-19 18:11:19 +0000317 fscache_stat(&fscache_n_op_cancelled);
318 list_del_init(&op->pend_link);
David Howells9f105232012-12-20 21:52:35 +0000319 op->state = FSCACHE_OP_ST_CANCELLED;
David Howells5753c442009-11-19 18:11:19 +0000320 if (test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags))
321 object->n_exclusive--;
322 if (test_and_clear_bit(FSCACHE_OP_WAITING, &op->flags))
323 wake_up_bit(&op->flags, FSCACHE_OP_WAITING);
324 fscache_put_operation(op);
325 ret = 0;
326 }
327
328 spin_unlock(&object->lock);
329 _leave(" = %d", ret);
330 return ret;
331}
332
333/*
David Howellsef778e72012-12-20 21:52:36 +0000334 * Cancel all pending operations on an object
335 */
336void fscache_cancel_all_ops(struct fscache_object *object)
337{
338 struct fscache_operation *op;
339
340 _enter("OBJ%x", object->debug_id);
341
342 spin_lock(&object->lock);
343
344 while (!list_empty(&object->pending_ops)) {
345 op = list_entry(object->pending_ops.next,
346 struct fscache_operation, pend_link);
347 fscache_stat(&fscache_n_op_cancelled);
348 list_del_init(&op->pend_link);
349
350 ASSERTCMP(op->state, ==, FSCACHE_OP_ST_PENDING);
351 op->state = FSCACHE_OP_ST_CANCELLED;
352
353 if (test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags))
354 object->n_exclusive--;
355 if (test_and_clear_bit(FSCACHE_OP_WAITING, &op->flags))
356 wake_up_bit(&op->flags, FSCACHE_OP_WAITING);
357 fscache_put_operation(op);
358 cond_resched_lock(&object->lock);
359 }
360
361 spin_unlock(&object->lock);
362 _leave("");
363}
364
365/*
David Howells1f372df2012-12-13 20:03:13 +0000366 * Record the completion or cancellation of an in-progress operation.
David Howells9f105232012-12-20 21:52:35 +0000367 */
David Howells1f372df2012-12-13 20:03:13 +0000368void fscache_op_complete(struct fscache_operation *op, bool cancelled)
David Howells9f105232012-12-20 21:52:35 +0000369{
370 struct fscache_object *object = op->object;
371
372 _enter("OBJ%x", object->debug_id);
373
374 ASSERTCMP(op->state, ==, FSCACHE_OP_ST_IN_PROGRESS);
375 ASSERTCMP(object->n_in_progress, >, 0);
376 ASSERTIFCMP(test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags),
377 object->n_exclusive, >, 0);
378 ASSERTIFCMP(test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags),
379 object->n_in_progress, ==, 1);
380
381 spin_lock(&object->lock);
382
David Howells1f372df2012-12-13 20:03:13 +0000383 op->state = cancelled ?
384 FSCACHE_OP_ST_CANCELLED : FSCACHE_OP_ST_COMPLETE;
David Howells9f105232012-12-20 21:52:35 +0000385
386 if (test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags))
387 object->n_exclusive--;
388 object->n_in_progress--;
389 if (object->n_in_progress == 0)
390 fscache_start_operations(object);
391
392 spin_unlock(&object->lock);
393 _leave("");
394}
395EXPORT_SYMBOL(fscache_op_complete);
396
397/*
David Howells952efe72009-04-03 16:42:39 +0100398 * release an operation
399 * - queues pending ops if this is the last in-progress op
400 */
401void fscache_put_operation(struct fscache_operation *op)
402{
403 struct fscache_object *object;
404 struct fscache_cache *cache;
405
406 _enter("{OBJ%x OP%x,%d}",
407 op->object->debug_id, op->debug_id, atomic_read(&op->usage));
408
409 ASSERTCMP(atomic_read(&op->usage), >, 0);
410
411 if (!atomic_dec_and_test(&op->usage))
412 return;
413
414 _debug("PUT OP");
David Howells9f105232012-12-20 21:52:35 +0000415 ASSERTIFCMP(op->state != FSCACHE_OP_ST_COMPLETE,
416 op->state, ==, FSCACHE_OP_ST_CANCELLED);
417 op->state = FSCACHE_OP_ST_DEAD;
David Howells952efe72009-04-03 16:42:39 +0100418
419 fscache_stat(&fscache_n_op_release);
420
421 if (op->release) {
422 op->release(op);
423 op->release = NULL;
424 }
425
426 object = op->object;
427
David Howellsef46ed82012-12-20 21:52:35 +0000428 if (test_bit(FSCACHE_OP_DEC_READ_CNT, &op->flags)) {
429 if (atomic_dec_and_test(&object->n_reads)) {
430 clear_bit(FSCACHE_COOKIE_WAITING_ON_READS,
431 &object->cookie->flags);
432 wake_up_bit(&object->cookie->flags,
433 FSCACHE_COOKIE_WAITING_ON_READS);
434 }
435 }
David Howells4fbf4292009-11-19 18:11:04 +0000436
David Howells952efe72009-04-03 16:42:39 +0100437 /* now... we may get called with the object spinlock held, so we
438 * complete the cleanup here only if we can immediately acquire the
439 * lock, and defer it otherwise */
440 if (!spin_trylock(&object->lock)) {
441 _debug("defer put");
442 fscache_stat(&fscache_n_op_deferred_release);
443
444 cache = object->cache;
445 spin_lock(&cache->op_gc_list_lock);
446 list_add_tail(&op->pend_link, &cache->op_gc_list);
447 spin_unlock(&cache->op_gc_list_lock);
448 schedule_work(&cache->op_gc);
449 _leave(" [defer]");
450 return;
451 }
452
David Howells952efe72009-04-03 16:42:39 +0100453 ASSERTCMP(object->n_ops, >, 0);
454 object->n_ops--;
455 if (object->n_ops == 0)
456 fscache_raise_event(object, FSCACHE_OBJECT_EV_CLEARED);
457
458 spin_unlock(&object->lock);
459
460 kfree(op);
461 _leave(" [done]");
462}
463EXPORT_SYMBOL(fscache_put_operation);
464
465/*
466 * garbage collect operations that have had their release deferred
467 */
468void fscache_operation_gc(struct work_struct *work)
469{
470 struct fscache_operation *op;
471 struct fscache_object *object;
472 struct fscache_cache *cache =
473 container_of(work, struct fscache_cache, op_gc);
474 int count = 0;
475
476 _enter("");
477
478 do {
479 spin_lock(&cache->op_gc_list_lock);
480 if (list_empty(&cache->op_gc_list)) {
481 spin_unlock(&cache->op_gc_list_lock);
482 break;
483 }
484
485 op = list_entry(cache->op_gc_list.next,
486 struct fscache_operation, pend_link);
487 list_del(&op->pend_link);
488 spin_unlock(&cache->op_gc_list_lock);
489
490 object = op->object;
David Howells9f105232012-12-20 21:52:35 +0000491 spin_lock(&object->lock);
David Howells952efe72009-04-03 16:42:39 +0100492
493 _debug("GC DEFERRED REL OBJ%x OP%x",
494 object->debug_id, op->debug_id);
495 fscache_stat(&fscache_n_op_gc);
496
497 ASSERTCMP(atomic_read(&op->usage), ==, 0);
David Howells9f105232012-12-20 21:52:35 +0000498 ASSERTCMP(op->state, ==, FSCACHE_OP_ST_DEAD);
David Howells952efe72009-04-03 16:42:39 +0100499
500 ASSERTCMP(object->n_ops, >, 0);
501 object->n_ops--;
502 if (object->n_ops == 0)
503 fscache_raise_event(object, FSCACHE_OBJECT_EV_CLEARED);
504
505 spin_unlock(&object->lock);
David Howells9f105232012-12-20 21:52:35 +0000506 kfree(op);
David Howells952efe72009-04-03 16:42:39 +0100507
508 } while (count++ < 20);
509
510 if (!list_empty(&cache->op_gc_list))
511 schedule_work(&cache->op_gc);
512
513 _leave("");
514}
515
516/*
Tejun Heo8af7c122010-07-20 22:09:01 +0200517 * execute an operation using fs_op_wq to provide processing context -
518 * the caller holds a ref to this object, so we don't need to hold one
David Howells952efe72009-04-03 16:42:39 +0100519 */
Tejun Heo8af7c122010-07-20 22:09:01 +0200520void fscache_op_work_func(struct work_struct *work)
David Howells952efe72009-04-03 16:42:39 +0100521{
522 struct fscache_operation *op =
Tejun Heo8af7c122010-07-20 22:09:01 +0200523 container_of(work, struct fscache_operation, work);
David Howells952efe72009-04-03 16:42:39 +0100524 unsigned long start;
525
526 _enter("{OBJ%x OP%x,%d}",
527 op->object->debug_id, op->debug_id, atomic_read(&op->usage));
528
529 ASSERT(op->processor != NULL);
530 start = jiffies;
531 op->processor(op);
532 fscache_hist(fscache_ops_histogram, start);
Tejun Heo8af7c122010-07-20 22:09:01 +0200533 fscache_put_operation(op);
David Howells952efe72009-04-03 16:42:39 +0100534
535 _leave("");
536}