blob: 296492efb81b74ede679a326b7ccff4d1b86a29f [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>
David Howells952efe72009-04-03 16:42:39 +010017#include "internal.h"
18
19atomic_t fscache_op_debug_id;
20EXPORT_SYMBOL(fscache_op_debug_id);
21
22/**
23 * fscache_enqueue_operation - Enqueue an operation for processing
24 * @op: The operation to enqueue
25 *
26 * Enqueue an operation for processing by the FS-Cache thread pool.
27 *
28 * This will get its own ref on the object.
29 */
30void fscache_enqueue_operation(struct fscache_operation *op)
31{
32 _enter("{OBJ%x OP%x,%u}",
33 op->object->debug_id, op->debug_id, atomic_read(&op->usage));
34
David Howells440f0af2009-11-19 18:11:01 +000035 fscache_set_op_state(op, "EnQ");
36
David Howells5753c442009-11-19 18:11:19 +000037 ASSERT(list_empty(&op->pend_link));
David Howells952efe72009-04-03 16:42:39 +010038 ASSERT(op->processor != NULL);
39 ASSERTCMP(op->object->state, >=, FSCACHE_OBJECT_AVAILABLE);
40 ASSERTCMP(atomic_read(&op->usage), >, 0);
41
David Howells5753c442009-11-19 18:11:19 +000042 fscache_stat(&fscache_n_op_enqueue);
43 switch (op->flags & FSCACHE_OP_TYPE) {
44 case FSCACHE_OP_FAST:
45 _debug("queue fast");
46 atomic_inc(&op->usage);
47 if (!schedule_work(&op->fast_work))
48 fscache_put_operation(op);
49 break;
50 case FSCACHE_OP_SLOW:
51 _debug("queue slow");
52 slow_work_enqueue(&op->slow_work);
53 break;
54 case FSCACHE_OP_MYTHREAD:
55 _debug("queue for caller's attention");
56 break;
57 default:
58 printk(KERN_ERR "FS-Cache: Unexpected op type %lx",
59 op->flags);
60 BUG();
61 break;
David Howells952efe72009-04-03 16:42:39 +010062 }
63}
64EXPORT_SYMBOL(fscache_enqueue_operation);
65
66/*
67 * start an op running
68 */
69static void fscache_run_op(struct fscache_object *object,
70 struct fscache_operation *op)
71{
David Howells440f0af2009-11-19 18:11:01 +000072 fscache_set_op_state(op, "Run");
73
David Howells952efe72009-04-03 16:42:39 +010074 object->n_in_progress++;
75 if (test_and_clear_bit(FSCACHE_OP_WAITING, &op->flags))
76 wake_up_bit(&op->flags, FSCACHE_OP_WAITING);
77 if (op->processor)
78 fscache_enqueue_operation(op);
79 fscache_stat(&fscache_n_op_run);
80}
81
82/*
83 * submit an exclusive operation for an object
84 * - other ops are excluded from running simultaneously with this one
85 * - this gets any extra refs it needs on an op
86 */
87int fscache_submit_exclusive_op(struct fscache_object *object,
88 struct fscache_operation *op)
89{
90 int ret;
91
92 _enter("{OBJ%x OP%x},", object->debug_id, op->debug_id);
93
David Howells440f0af2009-11-19 18:11:01 +000094 fscache_set_op_state(op, "SubmitX");
95
David Howells952efe72009-04-03 16:42:39 +010096 spin_lock(&object->lock);
97 ASSERTCMP(object->n_ops, >=, object->n_in_progress);
98 ASSERTCMP(object->n_ops, >=, object->n_exclusive);
David Howells5753c442009-11-19 18:11:19 +000099 ASSERT(list_empty(&op->pend_link));
David Howells952efe72009-04-03 16:42:39 +0100100
101 ret = -ENOBUFS;
102 if (fscache_object_is_active(object)) {
103 op->object = object;
104 object->n_ops++;
105 object->n_exclusive++; /* reads and writes must wait */
106
107 if (object->n_ops > 0) {
108 atomic_inc(&op->usage);
109 list_add_tail(&op->pend_link, &object->pending_ops);
110 fscache_stat(&fscache_n_op_pend);
111 } else if (!list_empty(&object->pending_ops)) {
112 atomic_inc(&op->usage);
113 list_add_tail(&op->pend_link, &object->pending_ops);
114 fscache_stat(&fscache_n_op_pend);
115 fscache_start_operations(object);
116 } else {
117 ASSERTCMP(object->n_in_progress, ==, 0);
118 fscache_run_op(object, op);
119 }
120
121 /* need to issue a new write op after this */
122 clear_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags);
123 ret = 0;
124 } else if (object->state == FSCACHE_OBJECT_CREATING) {
125 op->object = object;
126 object->n_ops++;
127 object->n_exclusive++; /* reads and writes must wait */
128 atomic_inc(&op->usage);
129 list_add_tail(&op->pend_link, &object->pending_ops);
130 fscache_stat(&fscache_n_op_pend);
131 ret = 0;
132 } else {
133 /* not allowed to submit ops in any other state */
134 BUG();
135 }
136
137 spin_unlock(&object->lock);
138 return ret;
139}
140
141/*
142 * report an unexpected submission
143 */
144static void fscache_report_unexpected_submission(struct fscache_object *object,
145 struct fscache_operation *op,
146 unsigned long ostate)
147{
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]",
157 op->debug_id, object->debug_id,
158 fscache_object_states[object->state]);
159 kdebug("objstate=%s [%s]",
160 fscache_object_states[object->state],
161 fscache_object_states[ostate]);
162 kdebug("objflags=%lx", object->flags);
163 kdebug("objevent=%lx [%lx]", object->events, object->event_mask);
164 kdebug("ops=%u inp=%u exc=%u",
165 object->n_ops, object->n_in_progress, object->n_exclusive);
166
167 if (!list_empty(&object->pending_ops)) {
168 n = 0;
169 list_for_each_entry(p, &object->pending_ops, pend_link) {
170 ASSERTCMP(p->object, ==, object);
171 kdebug("%p %p", op->processor, op->release);
172 n++;
173 }
174
175 kdebug("n=%u", n);
176 }
177
178 dump_stack();
179}
180
181/*
182 * submit an operation for an object
183 * - objects may be submitted only in the following states:
184 * - during object creation (write ops may be submitted)
185 * - whilst the object is active
186 * - after an I/O error incurred in one of the two above states (op rejected)
187 * - this gets any extra refs it needs on an op
188 */
189int fscache_submit_op(struct fscache_object *object,
190 struct fscache_operation *op)
191{
192 unsigned long ostate;
193 int ret;
194
195 _enter("{OBJ%x OP%x},{%u}",
196 object->debug_id, op->debug_id, atomic_read(&op->usage));
197
198 ASSERTCMP(atomic_read(&op->usage), >, 0);
199
David Howells440f0af2009-11-19 18:11:01 +0000200 fscache_set_op_state(op, "Submit");
201
David Howells952efe72009-04-03 16:42:39 +0100202 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
210 if (fscache_object_is_active(object)) {
211 op->object = object;
212 object->n_ops++;
213
214 if (object->n_exclusive > 0) {
215 atomic_inc(&op->usage);
216 list_add_tail(&op->pend_link, &object->pending_ops);
217 fscache_stat(&fscache_n_op_pend);
218 } else if (!list_empty(&object->pending_ops)) {
219 atomic_inc(&op->usage);
220 list_add_tail(&op->pend_link, &object->pending_ops);
221 fscache_stat(&fscache_n_op_pend);
222 fscache_start_operations(object);
223 } else {
224 ASSERTCMP(object->n_exclusive, ==, 0);
225 fscache_run_op(object, op);
226 }
227 ret = 0;
228 } else if (object->state == FSCACHE_OBJECT_CREATING) {
229 op->object = object;
230 object->n_ops++;
231 atomic_inc(&op->usage);
232 list_add_tail(&op->pend_link, &object->pending_ops);
233 fscache_stat(&fscache_n_op_pend);
234 ret = 0;
235 } else if (!test_bit(FSCACHE_IOERROR, &object->cache->flags)) {
236 fscache_report_unexpected_submission(object, op, ostate);
237 ASSERT(!fscache_object_is_active(object));
238 ret = -ENOBUFS;
239 } else {
240 ret = -ENOBUFS;
241 }
242
243 spin_unlock(&object->lock);
244 return ret;
245}
246
247/*
248 * queue an object for withdrawal on error, aborting all following asynchronous
249 * operations
250 */
251void fscache_abort_object(struct fscache_object *object)
252{
253 _enter("{OBJ%x}", object->debug_id);
254
255 fscache_raise_event(object, FSCACHE_OBJECT_EV_ERROR);
256}
257
258/*
259 * jump start the operation processing on an object
260 * - caller must hold object->lock
261 */
262void fscache_start_operations(struct fscache_object *object)
263{
264 struct fscache_operation *op;
265 bool stop = false;
266
267 while (!list_empty(&object->pending_ops) && !stop) {
268 op = list_entry(object->pending_ops.next,
269 struct fscache_operation, pend_link);
270
271 if (test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags)) {
272 if (object->n_in_progress > 0)
273 break;
274 stop = true;
275 }
276 list_del_init(&op->pend_link);
David Howells5753c442009-11-19 18:11:19 +0000277 fscache_run_op(object, op);
David Howells952efe72009-04-03 16:42:39 +0100278
279 /* the pending queue was holding a ref on the object */
280 fscache_put_operation(op);
281 }
282
283 ASSERTCMP(object->n_in_progress, <=, object->n_ops);
284
285 _debug("woke %d ops on OBJ%x",
286 object->n_in_progress, object->debug_id);
287}
288
289/*
David Howells5753c442009-11-19 18:11:19 +0000290 * cancel an operation that's pending on an object
291 */
292int fscache_cancel_op(struct fscache_operation *op)
293{
294 struct fscache_object *object = op->object;
295 int ret;
296
297 _enter("OBJ%x OP%x}", op->object->debug_id, op->debug_id);
298
299 spin_lock(&object->lock);
300
301 ret = -EBUSY;
302 if (!list_empty(&op->pend_link)) {
303 fscache_stat(&fscache_n_op_cancelled);
304 list_del_init(&op->pend_link);
305 object->n_ops--;
306 if (test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags))
307 object->n_exclusive--;
308 if (test_and_clear_bit(FSCACHE_OP_WAITING, &op->flags))
309 wake_up_bit(&op->flags, FSCACHE_OP_WAITING);
310 fscache_put_operation(op);
311 ret = 0;
312 }
313
314 spin_unlock(&object->lock);
315 _leave(" = %d", ret);
316 return ret;
317}
318
319/*
David Howells952efe72009-04-03 16:42:39 +0100320 * release an operation
321 * - queues pending ops if this is the last in-progress op
322 */
323void fscache_put_operation(struct fscache_operation *op)
324{
325 struct fscache_object *object;
326 struct fscache_cache *cache;
327
328 _enter("{OBJ%x OP%x,%d}",
329 op->object->debug_id, op->debug_id, atomic_read(&op->usage));
330
331 ASSERTCMP(atomic_read(&op->usage), >, 0);
332
333 if (!atomic_dec_and_test(&op->usage))
334 return;
335
David Howells440f0af2009-11-19 18:11:01 +0000336 fscache_set_op_state(op, "Put");
337
David Howells952efe72009-04-03 16:42:39 +0100338 _debug("PUT OP");
339 if (test_and_set_bit(FSCACHE_OP_DEAD, &op->flags))
340 BUG();
341
342 fscache_stat(&fscache_n_op_release);
343
344 if (op->release) {
345 op->release(op);
346 op->release = NULL;
347 }
348
349 object = op->object;
350
David Howells4fbf4292009-11-19 18:11:04 +0000351 if (test_bit(FSCACHE_OP_DEC_READ_CNT, &op->flags))
352 atomic_dec(&object->n_reads);
353
David Howells952efe72009-04-03 16:42:39 +0100354 /* now... we may get called with the object spinlock held, so we
355 * complete the cleanup here only if we can immediately acquire the
356 * lock, and defer it otherwise */
357 if (!spin_trylock(&object->lock)) {
358 _debug("defer put");
359 fscache_stat(&fscache_n_op_deferred_release);
360
361 cache = object->cache;
362 spin_lock(&cache->op_gc_list_lock);
363 list_add_tail(&op->pend_link, &cache->op_gc_list);
364 spin_unlock(&cache->op_gc_list_lock);
365 schedule_work(&cache->op_gc);
366 _leave(" [defer]");
367 return;
368 }
369
370 if (test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags)) {
371 ASSERTCMP(object->n_exclusive, >, 0);
372 object->n_exclusive--;
373 }
374
375 ASSERTCMP(object->n_in_progress, >, 0);
376 object->n_in_progress--;
377 if (object->n_in_progress == 0)
378 fscache_start_operations(object);
379
380 ASSERTCMP(object->n_ops, >, 0);
381 object->n_ops--;
382 if (object->n_ops == 0)
383 fscache_raise_event(object, FSCACHE_OBJECT_EV_CLEARED);
384
385 spin_unlock(&object->lock);
386
387 kfree(op);
388 _leave(" [done]");
389}
390EXPORT_SYMBOL(fscache_put_operation);
391
392/*
393 * garbage collect operations that have had their release deferred
394 */
395void fscache_operation_gc(struct work_struct *work)
396{
397 struct fscache_operation *op;
398 struct fscache_object *object;
399 struct fscache_cache *cache =
400 container_of(work, struct fscache_cache, op_gc);
401 int count = 0;
402
403 _enter("");
404
405 do {
406 spin_lock(&cache->op_gc_list_lock);
407 if (list_empty(&cache->op_gc_list)) {
408 spin_unlock(&cache->op_gc_list_lock);
409 break;
410 }
411
412 op = list_entry(cache->op_gc_list.next,
413 struct fscache_operation, pend_link);
414 list_del(&op->pend_link);
415 spin_unlock(&cache->op_gc_list_lock);
416
417 object = op->object;
418
419 _debug("GC DEFERRED REL OBJ%x OP%x",
420 object->debug_id, op->debug_id);
421 fscache_stat(&fscache_n_op_gc);
422
423 ASSERTCMP(atomic_read(&op->usage), ==, 0);
424
425 spin_lock(&object->lock);
426 if (test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags)) {
427 ASSERTCMP(object->n_exclusive, >, 0);
428 object->n_exclusive--;
429 }
430
431 ASSERTCMP(object->n_in_progress, >, 0);
432 object->n_in_progress--;
433 if (object->n_in_progress == 0)
434 fscache_start_operations(object);
435
436 ASSERTCMP(object->n_ops, >, 0);
437 object->n_ops--;
438 if (object->n_ops == 0)
439 fscache_raise_event(object, FSCACHE_OBJECT_EV_CLEARED);
440
441 spin_unlock(&object->lock);
442
443 } while (count++ < 20);
444
445 if (!list_empty(&cache->op_gc_list))
446 schedule_work(&cache->op_gc);
447
448 _leave("");
449}
450
451/*
452 * allow the slow work item processor to get a ref on an operation
453 */
454static int fscache_op_get_ref(struct slow_work *work)
455{
456 struct fscache_operation *op =
457 container_of(work, struct fscache_operation, slow_work);
458
459 atomic_inc(&op->usage);
460 return 0;
461}
462
463/*
464 * allow the slow work item processor to discard a ref on an operation
465 */
466static void fscache_op_put_ref(struct slow_work *work)
467{
468 struct fscache_operation *op =
469 container_of(work, struct fscache_operation, slow_work);
470
471 fscache_put_operation(op);
472}
473
474/*
475 * execute an operation using the slow thread pool to provide processing context
476 * - the caller holds a ref to this object, so we don't need to hold one
477 */
478static void fscache_op_execute(struct slow_work *work)
479{
480 struct fscache_operation *op =
481 container_of(work, struct fscache_operation, slow_work);
482 unsigned long start;
483
484 _enter("{OBJ%x OP%x,%d}",
485 op->object->debug_id, op->debug_id, atomic_read(&op->usage));
486
487 ASSERT(op->processor != NULL);
488 start = jiffies;
489 op->processor(op);
490 fscache_hist(fscache_ops_histogram, start);
491
492 _leave("");
493}
494
David Howells440f0af2009-11-19 18:11:01 +0000495/*
496 * describe an operation for slow-work debugging
497 */
498#ifdef CONFIG_SLOW_WORK_PROC
499static void fscache_op_desc(struct slow_work *work, struct seq_file *m)
500{
501 struct fscache_operation *op =
502 container_of(work, struct fscache_operation, slow_work);
503
504 seq_printf(m, "FSC: OBJ%x OP%x: %s/%s fl=%lx",
505 op->object->debug_id, op->debug_id,
506 op->name, op->state, op->flags);
507}
508#endif
509
David Howells952efe72009-04-03 16:42:39 +0100510const struct slow_work_ops fscache_op_slow_work_ops = {
David Howells3d7a6412009-11-19 18:10:23 +0000511 .owner = THIS_MODULE,
David Howells952efe72009-04-03 16:42:39 +0100512 .get_ref = fscache_op_get_ref,
513 .put_ref = fscache_op_put_ref,
514 .execute = fscache_op_execute,
David Howells440f0af2009-11-19 18:11:01 +0000515#ifdef CONFIG_SLOW_WORK_PROC
516 .desc = fscache_op_desc,
517#endif
David Howells952efe72009-04-03 16:42:39 +0100518};