FS-Cache: Limit the number of I/O error reports for a cache
[linux-block.git] / fs / fscache / operation.c
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>
16 #include <linux/seq_file.h>
17 #include <linux/slab.h>
18 #include "internal.h"
19
20 atomic_t fscache_op_debug_id;
21 EXPORT_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  */
31 void 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
36         ASSERT(list_empty(&op->pend_link));
37         ASSERT(op->processor != NULL);
38         ASSERTCMP(op->object->state, >=, FSCACHE_OBJECT_AVAILABLE);
39         ASSERTCMP(atomic_read(&op->usage), >, 0);
40         ASSERTCMP(op->state, ==, FSCACHE_OP_ST_IN_PROGRESS);
41
42         fscache_stat(&fscache_n_op_enqueue);
43         switch (op->flags & FSCACHE_OP_TYPE) {
44         case FSCACHE_OP_ASYNC:
45                 _debug("queue async");
46                 atomic_inc(&op->usage);
47                 if (!queue_work(fscache_op_wq, &op->work))
48                         fscache_put_operation(op);
49                 break;
50         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;
58         }
59 }
60 EXPORT_SYMBOL(fscache_enqueue_operation);
61
62 /*
63  * start an op running
64  */
65 static void fscache_run_op(struct fscache_object *object,
66                            struct fscache_operation *op)
67 {
68         ASSERTCMP(op->state, ==, FSCACHE_OP_ST_PENDING);
69
70         op->state = FSCACHE_OP_ST_IN_PROGRESS;
71         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  */
84 int fscache_submit_exclusive_op(struct fscache_object *object,
85                                 struct fscache_operation *op)
86 {
87         _enter("{OBJ%x OP%x},", object->debug_id, op->debug_id);
88
89         ASSERTCMP(op->state, ==, FSCACHE_OP_ST_INITIALISED);
90         ASSERTCMP(atomic_read(&op->usage), >, 0);
91
92         spin_lock(&object->lock);
93         ASSERTCMP(object->n_ops, >=, object->n_in_progress);
94         ASSERTCMP(object->n_ops, >=, object->n_exclusive);
95         ASSERT(list_empty(&op->pend_link));
96
97         op->state = FSCACHE_OP_ST_PENDING;
98         if (fscache_object_is_active(object)) {
99                 op->object = object;
100                 object->n_ops++;
101                 object->n_exclusive++;  /* reads and writes must wait */
102
103                 if (object->n_in_progress > 0) {
104                         atomic_inc(&op->usage);
105                         list_add_tail(&op->pend_link, &object->pending_ops);
106                         fscache_stat(&fscache_n_op_pend);
107                 } else if (!list_empty(&object->pending_ops)) {
108                         atomic_inc(&op->usage);
109                         list_add_tail(&op->pend_link, &object->pending_ops);
110                         fscache_stat(&fscache_n_op_pend);
111                         fscache_start_operations(object);
112                 } else {
113                         ASSERTCMP(object->n_in_progress, ==, 0);
114                         fscache_run_op(object, op);
115                 }
116
117                 /* need to issue a new write op after this */
118                 clear_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags);
119         } else if (object->state == FSCACHE_OBJECT_CREATING) {
120                 op->object = object;
121                 object->n_ops++;
122                 object->n_exclusive++;  /* reads and writes must wait */
123                 atomic_inc(&op->usage);
124                 list_add_tail(&op->pend_link, &object->pending_ops);
125                 fscache_stat(&fscache_n_op_pend);
126         } else {
127                 /* not allowed to submit ops in any other state */
128                 BUG();
129         }
130
131         spin_unlock(&object->lock);
132         return 0;
133 }
134
135 /*
136  * report an unexpected submission
137  */
138 static void fscache_report_unexpected_submission(struct fscache_object *object,
139                                                  struct fscache_operation *op,
140                                                  unsigned long ostate)
141 {
142         static bool once_only;
143         struct fscache_operation *p;
144         unsigned n;
145
146         if (once_only)
147                 return;
148         once_only = true;
149
150         kdebug("unexpected submission OP%x [OBJ%x %s]",
151                op->debug_id, object->debug_id,
152                fscache_object_states[object->state]);
153         kdebug("objstate=%s [%s]",
154                fscache_object_states[object->state],
155                fscache_object_states[ostate]);
156         kdebug("objflags=%lx", object->flags);
157         kdebug("objevent=%lx [%lx]", object->events, object->event_mask);
158         kdebug("ops=%u inp=%u exc=%u",
159                object->n_ops, object->n_in_progress, object->n_exclusive);
160
161         if (!list_empty(&object->pending_ops)) {
162                 n = 0;
163                 list_for_each_entry(p, &object->pending_ops, pend_link) {
164                         ASSERTCMP(p->object, ==, object);
165                         kdebug("%p %p", op->processor, op->release);
166                         n++;
167                 }
168
169                 kdebug("n=%u", n);
170         }
171
172         dump_stack();
173 }
174
175 /*
176  * submit an operation for an object
177  * - objects may be submitted only in the following states:
178  *   - during object creation (write ops may be submitted)
179  *   - whilst the object is active
180  *   - after an I/O error incurred in one of the two above states (op rejected)
181  * - this gets any extra refs it needs on an op
182  */
183 int fscache_submit_op(struct fscache_object *object,
184                       struct fscache_operation *op)
185 {
186         unsigned long ostate;
187         int ret;
188
189         _enter("{OBJ%x OP%x},{%u}",
190                object->debug_id, op->debug_id, atomic_read(&op->usage));
191
192         ASSERTCMP(op->state, ==, FSCACHE_OP_ST_INITIALISED);
193         ASSERTCMP(atomic_read(&op->usage), >, 0);
194
195         spin_lock(&object->lock);
196         ASSERTCMP(object->n_ops, >=, object->n_in_progress);
197         ASSERTCMP(object->n_ops, >=, object->n_exclusive);
198         ASSERT(list_empty(&op->pend_link));
199
200         ostate = object->state;
201         smp_rmb();
202
203         op->state = FSCACHE_OP_ST_PENDING;
204         if (fscache_object_is_active(object)) {
205                 op->object = object;
206                 object->n_ops++;
207
208                 if (object->n_exclusive > 0) {
209                         atomic_inc(&op->usage);
210                         list_add_tail(&op->pend_link, &object->pending_ops);
211                         fscache_stat(&fscache_n_op_pend);
212                 } else if (!list_empty(&object->pending_ops)) {
213                         atomic_inc(&op->usage);
214                         list_add_tail(&op->pend_link, &object->pending_ops);
215                         fscache_stat(&fscache_n_op_pend);
216                         fscache_start_operations(object);
217                 } else {
218                         ASSERTCMP(object->n_exclusive, ==, 0);
219                         fscache_run_op(object, op);
220                 }
221                 ret = 0;
222         } else if (object->state == FSCACHE_OBJECT_CREATING) {
223                 op->object = object;
224                 object->n_ops++;
225                 atomic_inc(&op->usage);
226                 list_add_tail(&op->pend_link, &object->pending_ops);
227                 fscache_stat(&fscache_n_op_pend);
228                 ret = 0;
229         } else if (object->state == FSCACHE_OBJECT_DYING ||
230                    object->state == FSCACHE_OBJECT_LC_DYING ||
231                    object->state == FSCACHE_OBJECT_WITHDRAWING) {
232                 fscache_stat(&fscache_n_op_rejected);
233                 op->state = FSCACHE_OP_ST_CANCELLED;
234                 ret = -ENOBUFS;
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                 op->state = FSCACHE_OP_ST_CANCELLED;
239                 ret = -ENOBUFS;
240         } else {
241                 op->state = FSCACHE_OP_ST_CANCELLED;
242                 ret = -ENOBUFS;
243         }
244
245         spin_unlock(&object->lock);
246         return ret;
247 }
248
249 /*
250  * queue an object for withdrawal on error, aborting all following asynchronous
251  * operations
252  */
253 void fscache_abort_object(struct fscache_object *object)
254 {
255         _enter("{OBJ%x}", object->debug_id);
256
257         fscache_raise_event(object, FSCACHE_OBJECT_EV_ERROR);
258 }
259
260 /*
261  * jump start the operation processing on an object
262  * - caller must hold object->lock
263  */
264 void fscache_start_operations(struct fscache_object *object)
265 {
266         struct fscache_operation *op;
267         bool stop = false;
268
269         while (!list_empty(&object->pending_ops) && !stop) {
270                 op = list_entry(object->pending_ops.next,
271                                 struct fscache_operation, pend_link);
272
273                 if (test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags)) {
274                         if (object->n_in_progress > 0)
275                                 break;
276                         stop = true;
277                 }
278                 list_del_init(&op->pend_link);
279                 fscache_run_op(object, op);
280
281                 /* the pending queue was holding a ref on the object */
282                 fscache_put_operation(op);
283         }
284
285         ASSERTCMP(object->n_in_progress, <=, object->n_ops);
286
287         _debug("woke %d ops on OBJ%x",
288                object->n_in_progress, object->debug_id);
289 }
290
291 /*
292  * cancel an operation that's pending on an object
293  */
294 int fscache_cancel_op(struct fscache_operation *op)
295 {
296         struct fscache_object *object = op->object;
297         int ret;
298
299         _enter("OBJ%x OP%x}", op->object->debug_id, op->debug_id);
300
301         ASSERTCMP(op->state, >=, FSCACHE_OP_ST_PENDING);
302         ASSERTCMP(op->state, !=, FSCACHE_OP_ST_CANCELLED);
303         ASSERTCMP(atomic_read(&op->usage), >, 0);
304
305         spin_lock(&object->lock);
306
307         ret = -EBUSY;
308         if (op->state == FSCACHE_OP_ST_PENDING) {
309                 ASSERT(!list_empty(&op->pend_link));
310                 fscache_stat(&fscache_n_op_cancelled);
311                 list_del_init(&op->pend_link);
312                 op->state = FSCACHE_OP_ST_CANCELLED;
313                 if (test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags))
314                         object->n_exclusive--;
315                 if (test_and_clear_bit(FSCACHE_OP_WAITING, &op->flags))
316                         wake_up_bit(&op->flags, FSCACHE_OP_WAITING);
317                 fscache_put_operation(op);
318                 ret = 0;
319         }
320
321         spin_unlock(&object->lock);
322         _leave(" = %d", ret);
323         return ret;
324 }
325
326 /*
327  * Cancel all pending operations on an object
328  */
329 void fscache_cancel_all_ops(struct fscache_object *object)
330 {
331         struct fscache_operation *op;
332
333         _enter("OBJ%x", object->debug_id);
334
335         spin_lock(&object->lock);
336
337         while (!list_empty(&object->pending_ops)) {
338                 op = list_entry(object->pending_ops.next,
339                                 struct fscache_operation, pend_link);
340                 fscache_stat(&fscache_n_op_cancelled);
341                 list_del_init(&op->pend_link);
342
343                 ASSERTCMP(op->state, ==, FSCACHE_OP_ST_PENDING);
344                 op->state = FSCACHE_OP_ST_CANCELLED;
345
346                 if (test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags))
347                         object->n_exclusive--;
348                 if (test_and_clear_bit(FSCACHE_OP_WAITING, &op->flags))
349                         wake_up_bit(&op->flags, FSCACHE_OP_WAITING);
350                 fscache_put_operation(op);
351                 cond_resched_lock(&object->lock);
352         }
353
354         spin_unlock(&object->lock);
355         _leave("");
356 }
357
358 /*
359  * Record the completion of an in-progress operation.
360  */
361 void fscache_op_complete(struct fscache_operation *op)
362 {
363         struct fscache_object *object = op->object;
364
365         _enter("OBJ%x", object->debug_id);
366
367         ASSERTCMP(op->state, ==, FSCACHE_OP_ST_IN_PROGRESS);
368         ASSERTCMP(object->n_in_progress, >, 0);
369         ASSERTIFCMP(test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags),
370                     object->n_exclusive, >, 0);
371         ASSERTIFCMP(test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags),
372                     object->n_in_progress, ==, 1);
373
374         spin_lock(&object->lock);
375
376         op->state = FSCACHE_OP_ST_COMPLETE;
377
378         if (test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags))
379                 object->n_exclusive--;
380         object->n_in_progress--;
381         if (object->n_in_progress == 0)
382                 fscache_start_operations(object);
383
384         spin_unlock(&object->lock);
385         _leave("");
386 }
387 EXPORT_SYMBOL(fscache_op_complete);
388
389 /*
390  * release an operation
391  * - queues pending ops if this is the last in-progress op
392  */
393 void fscache_put_operation(struct fscache_operation *op)
394 {
395         struct fscache_object *object;
396         struct fscache_cache *cache;
397
398         _enter("{OBJ%x OP%x,%d}",
399                op->object->debug_id, op->debug_id, atomic_read(&op->usage));
400
401         ASSERTCMP(atomic_read(&op->usage), >, 0);
402
403         if (!atomic_dec_and_test(&op->usage))
404                 return;
405
406         _debug("PUT OP");
407         ASSERTIFCMP(op->state != FSCACHE_OP_ST_COMPLETE,
408                     op->state, ==, FSCACHE_OP_ST_CANCELLED);
409         op->state = FSCACHE_OP_ST_DEAD;
410
411         fscache_stat(&fscache_n_op_release);
412
413         if (op->release) {
414                 op->release(op);
415                 op->release = NULL;
416         }
417
418         object = op->object;
419
420         if (test_bit(FSCACHE_OP_DEC_READ_CNT, &op->flags)) {
421                 if (atomic_dec_and_test(&object->n_reads)) {
422                         clear_bit(FSCACHE_COOKIE_WAITING_ON_READS,
423                                   &object->cookie->flags);
424                         wake_up_bit(&object->cookie->flags,
425                                     FSCACHE_COOKIE_WAITING_ON_READS);
426                 }
427         }
428
429         /* now... we may get called with the object spinlock held, so we
430          * complete the cleanup here only if we can immediately acquire the
431          * lock, and defer it otherwise */
432         if (!spin_trylock(&object->lock)) {
433                 _debug("defer put");
434                 fscache_stat(&fscache_n_op_deferred_release);
435
436                 cache = object->cache;
437                 spin_lock(&cache->op_gc_list_lock);
438                 list_add_tail(&op->pend_link, &cache->op_gc_list);
439                 spin_unlock(&cache->op_gc_list_lock);
440                 schedule_work(&cache->op_gc);
441                 _leave(" [defer]");
442                 return;
443         }
444
445         ASSERTCMP(object->n_ops, >, 0);
446         object->n_ops--;
447         if (object->n_ops == 0)
448                 fscache_raise_event(object, FSCACHE_OBJECT_EV_CLEARED);
449
450         spin_unlock(&object->lock);
451
452         kfree(op);
453         _leave(" [done]");
454 }
455 EXPORT_SYMBOL(fscache_put_operation);
456
457 /*
458  * garbage collect operations that have had their release deferred
459  */
460 void fscache_operation_gc(struct work_struct *work)
461 {
462         struct fscache_operation *op;
463         struct fscache_object *object;
464         struct fscache_cache *cache =
465                 container_of(work, struct fscache_cache, op_gc);
466         int count = 0;
467
468         _enter("");
469
470         do {
471                 spin_lock(&cache->op_gc_list_lock);
472                 if (list_empty(&cache->op_gc_list)) {
473                         spin_unlock(&cache->op_gc_list_lock);
474                         break;
475                 }
476
477                 op = list_entry(cache->op_gc_list.next,
478                                 struct fscache_operation, pend_link);
479                 list_del(&op->pend_link);
480                 spin_unlock(&cache->op_gc_list_lock);
481
482                 object = op->object;
483                 spin_lock(&object->lock);
484
485                 _debug("GC DEFERRED REL OBJ%x OP%x",
486                        object->debug_id, op->debug_id);
487                 fscache_stat(&fscache_n_op_gc);
488
489                 ASSERTCMP(atomic_read(&op->usage), ==, 0);
490                 ASSERTCMP(op->state, ==, FSCACHE_OP_ST_DEAD);
491
492                 ASSERTCMP(object->n_ops, >, 0);
493                 object->n_ops--;
494                 if (object->n_ops == 0)
495                         fscache_raise_event(object, FSCACHE_OBJECT_EV_CLEARED);
496
497                 spin_unlock(&object->lock);
498                 kfree(op);
499
500         } while (count++ < 20);
501
502         if (!list_empty(&cache->op_gc_list))
503                 schedule_work(&cache->op_gc);
504
505         _leave("");
506 }
507
508 /*
509  * execute an operation using fs_op_wq to provide processing context -
510  * the caller holds a ref to this object, so we don't need to hold one
511  */
512 void fscache_op_work_func(struct work_struct *work)
513 {
514         struct fscache_operation *op =
515                 container_of(work, struct fscache_operation, work);
516         unsigned long start;
517
518         _enter("{OBJ%x OP%x,%d}",
519                op->object->debug_id, op->debug_id, atomic_read(&op->usage));
520
521         ASSERT(op->processor != NULL);
522         start = jiffies;
523         op->processor(op);
524         fscache_hist(fscache_ops_histogram, start);
525         fscache_put_operation(op);
526
527         _leave("");
528 }