FS-Cache: Make cookie relinquishment wait for outstanding reads
[linux-2.6.git] / fs / fscache / operation.c
blobc857ab824d6ec4876b67722ae428cdd3dedacf3c
1 /* FS-Cache worker operation management routines
3 * Copyright (C) 2008 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
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.
11 * See Documentation/filesystems/caching/operations.txt
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"
20 atomic_t fscache_op_debug_id;
21 EXPORT_SYMBOL(fscache_op_debug_id);
23 /**
24 * fscache_enqueue_operation - Enqueue an operation for processing
25 * @op: The operation to enqueue
27 * Enqueue an operation for processing by the FS-Cache thread pool.
29 * This will get its own ref on the object.
31 void fscache_enqueue_operation(struct fscache_operation *op)
33 _enter("{OBJ%x OP%x,%u}",
34 op->object->debug_id, op->debug_id, atomic_read(&op->usage));
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);
41 fscache_stat(&fscache_n_op_enqueue);
42 switch (op->flags & FSCACHE_OP_TYPE) {
43 case FSCACHE_OP_ASYNC:
44 _debug("queue async");
45 atomic_inc(&op->usage);
46 if (!queue_work(fscache_op_wq, &op->work))
47 fscache_put_operation(op);
48 break;
49 case FSCACHE_OP_MYTHREAD:
50 _debug("queue for caller's attention");
51 break;
52 default:
53 printk(KERN_ERR "FS-Cache: Unexpected op type %lx",
54 op->flags);
55 BUG();
56 break;
59 EXPORT_SYMBOL(fscache_enqueue_operation);
62 * start an op running
64 static void fscache_run_op(struct fscache_object *object,
65 struct fscache_operation *op)
67 object->n_in_progress++;
68 if (test_and_clear_bit(FSCACHE_OP_WAITING, &op->flags))
69 wake_up_bit(&op->flags, FSCACHE_OP_WAITING);
70 if (op->processor)
71 fscache_enqueue_operation(op);
72 fscache_stat(&fscache_n_op_run);
76 * submit an exclusive operation for an object
77 * - other ops are excluded from running simultaneously with this one
78 * - this gets any extra refs it needs on an op
80 int fscache_submit_exclusive_op(struct fscache_object *object,
81 struct fscache_operation *op)
83 int ret;
85 _enter("{OBJ%x OP%x},", object->debug_id, op->debug_id);
87 spin_lock(&object->lock);
88 ASSERTCMP(object->n_ops, >=, object->n_in_progress);
89 ASSERTCMP(object->n_ops, >=, object->n_exclusive);
90 ASSERT(list_empty(&op->pend_link));
92 ret = -ENOBUFS;
93 if (fscache_object_is_active(object)) {
94 op->object = object;
95 object->n_ops++;
96 object->n_exclusive++; /* reads and writes must wait */
98 if (object->n_ops > 1) {
99 atomic_inc(&op->usage);
100 list_add_tail(&op->pend_link, &object->pending_ops);
101 fscache_stat(&fscache_n_op_pend);
102 } else if (!list_empty(&object->pending_ops)) {
103 atomic_inc(&op->usage);
104 list_add_tail(&op->pend_link, &object->pending_ops);
105 fscache_stat(&fscache_n_op_pend);
106 fscache_start_operations(object);
107 } else {
108 ASSERTCMP(object->n_in_progress, ==, 0);
109 fscache_run_op(object, op);
112 /* need to issue a new write op after this */
113 clear_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags);
114 ret = 0;
115 } else if (object->state == FSCACHE_OBJECT_CREATING) {
116 op->object = object;
117 object->n_ops++;
118 object->n_exclusive++; /* reads and writes must wait */
119 atomic_inc(&op->usage);
120 list_add_tail(&op->pend_link, &object->pending_ops);
121 fscache_stat(&fscache_n_op_pend);
122 ret = 0;
123 } else {
124 /* not allowed to submit ops in any other state */
125 BUG();
128 spin_unlock(&object->lock);
129 return ret;
133 * report an unexpected submission
135 static void fscache_report_unexpected_submission(struct fscache_object *object,
136 struct fscache_operation *op,
137 unsigned long ostate)
139 static bool once_only;
140 struct fscache_operation *p;
141 unsigned n;
143 if (once_only)
144 return;
145 once_only = true;
147 kdebug("unexpected submission OP%x [OBJ%x %s]",
148 op->debug_id, object->debug_id,
149 fscache_object_states[object->state]);
150 kdebug("objstate=%s [%s]",
151 fscache_object_states[object->state],
152 fscache_object_states[ostate]);
153 kdebug("objflags=%lx", object->flags);
154 kdebug("objevent=%lx [%lx]", object->events, object->event_mask);
155 kdebug("ops=%u inp=%u exc=%u",
156 object->n_ops, object->n_in_progress, object->n_exclusive);
158 if (!list_empty(&object->pending_ops)) {
159 n = 0;
160 list_for_each_entry(p, &object->pending_ops, pend_link) {
161 ASSERTCMP(p->object, ==, object);
162 kdebug("%p %p", op->processor, op->release);
163 n++;
166 kdebug("n=%u", n);
169 dump_stack();
173 * submit an operation for an object
174 * - objects may be submitted only in the following states:
175 * - during object creation (write ops may be submitted)
176 * - whilst the object is active
177 * - after an I/O error incurred in one of the two above states (op rejected)
178 * - this gets any extra refs it needs on an op
180 int fscache_submit_op(struct fscache_object *object,
181 struct fscache_operation *op)
183 unsigned long ostate;
184 int ret;
186 _enter("{OBJ%x OP%x},{%u}",
187 object->debug_id, op->debug_id, atomic_read(&op->usage));
189 ASSERTCMP(atomic_read(&op->usage), >, 0);
191 spin_lock(&object->lock);
192 ASSERTCMP(object->n_ops, >=, object->n_in_progress);
193 ASSERTCMP(object->n_ops, >=, object->n_exclusive);
194 ASSERT(list_empty(&op->pend_link));
196 ostate = object->state;
197 smp_rmb();
199 if (fscache_object_is_active(object)) {
200 op->object = object;
201 object->n_ops++;
203 if (object->n_exclusive > 0) {
204 atomic_inc(&op->usage);
205 list_add_tail(&op->pend_link, &object->pending_ops);
206 fscache_stat(&fscache_n_op_pend);
207 } else if (!list_empty(&object->pending_ops)) {
208 atomic_inc(&op->usage);
209 list_add_tail(&op->pend_link, &object->pending_ops);
210 fscache_stat(&fscache_n_op_pend);
211 fscache_start_operations(object);
212 } else {
213 ASSERTCMP(object->n_exclusive, ==, 0);
214 fscache_run_op(object, op);
216 ret = 0;
217 } else if (object->state == FSCACHE_OBJECT_CREATING) {
218 op->object = object;
219 object->n_ops++;
220 atomic_inc(&op->usage);
221 list_add_tail(&op->pend_link, &object->pending_ops);
222 fscache_stat(&fscache_n_op_pend);
223 ret = 0;
224 } else if (object->state == FSCACHE_OBJECT_DYING ||
225 object->state == FSCACHE_OBJECT_LC_DYING ||
226 object->state == FSCACHE_OBJECT_WITHDRAWING) {
227 fscache_stat(&fscache_n_op_rejected);
228 ret = -ENOBUFS;
229 } else if (!test_bit(FSCACHE_IOERROR, &object->cache->flags)) {
230 fscache_report_unexpected_submission(object, op, ostate);
231 ASSERT(!fscache_object_is_active(object));
232 ret = -ENOBUFS;
233 } else {
234 ret = -ENOBUFS;
237 spin_unlock(&object->lock);
238 return ret;
242 * queue an object for withdrawal on error, aborting all following asynchronous
243 * operations
245 void fscache_abort_object(struct fscache_object *object)
247 _enter("{OBJ%x}", object->debug_id);
249 fscache_raise_event(object, FSCACHE_OBJECT_EV_ERROR);
253 * jump start the operation processing on an object
254 * - caller must hold object->lock
256 void fscache_start_operations(struct fscache_object *object)
258 struct fscache_operation *op;
259 bool stop = false;
261 while (!list_empty(&object->pending_ops) && !stop) {
262 op = list_entry(object->pending_ops.next,
263 struct fscache_operation, pend_link);
265 if (test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags)) {
266 if (object->n_in_progress > 0)
267 break;
268 stop = true;
270 list_del_init(&op->pend_link);
271 fscache_run_op(object, op);
273 /* the pending queue was holding a ref on the object */
274 fscache_put_operation(op);
277 ASSERTCMP(object->n_in_progress, <=, object->n_ops);
279 _debug("woke %d ops on OBJ%x",
280 object->n_in_progress, object->debug_id);
284 * cancel an operation that's pending on an object
286 int fscache_cancel_op(struct fscache_operation *op)
288 struct fscache_object *object = op->object;
289 int ret;
291 _enter("OBJ%x OP%x}", op->object->debug_id, op->debug_id);
293 spin_lock(&object->lock);
295 ret = -EBUSY;
296 if (!list_empty(&op->pend_link)) {
297 fscache_stat(&fscache_n_op_cancelled);
298 list_del_init(&op->pend_link);
299 object->n_ops--;
300 if (test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags))
301 object->n_exclusive--;
302 if (test_and_clear_bit(FSCACHE_OP_WAITING, &op->flags))
303 wake_up_bit(&op->flags, FSCACHE_OP_WAITING);
304 fscache_put_operation(op);
305 ret = 0;
308 spin_unlock(&object->lock);
309 _leave(" = %d", ret);
310 return ret;
314 * release an operation
315 * - queues pending ops if this is the last in-progress op
317 void fscache_put_operation(struct fscache_operation *op)
319 struct fscache_object *object;
320 struct fscache_cache *cache;
322 _enter("{OBJ%x OP%x,%d}",
323 op->object->debug_id, op->debug_id, atomic_read(&op->usage));
325 ASSERTCMP(atomic_read(&op->usage), >, 0);
327 if (!atomic_dec_and_test(&op->usage))
328 return;
330 _debug("PUT OP");
331 if (test_and_set_bit(FSCACHE_OP_DEAD, &op->flags))
332 BUG();
334 fscache_stat(&fscache_n_op_release);
336 if (op->release) {
337 op->release(op);
338 op->release = NULL;
341 object = op->object;
343 if (test_bit(FSCACHE_OP_DEC_READ_CNT, &op->flags)) {
344 if (atomic_dec_and_test(&object->n_reads)) {
345 clear_bit(FSCACHE_COOKIE_WAITING_ON_READS,
346 &object->cookie->flags);
347 wake_up_bit(&object->cookie->flags,
348 FSCACHE_COOKIE_WAITING_ON_READS);
352 /* now... we may get called with the object spinlock held, so we
353 * complete the cleanup here only if we can immediately acquire the
354 * lock, and defer it otherwise */
355 if (!spin_trylock(&object->lock)) {
356 _debug("defer put");
357 fscache_stat(&fscache_n_op_deferred_release);
359 cache = object->cache;
360 spin_lock(&cache->op_gc_list_lock);
361 list_add_tail(&op->pend_link, &cache->op_gc_list);
362 spin_unlock(&cache->op_gc_list_lock);
363 schedule_work(&cache->op_gc);
364 _leave(" [defer]");
365 return;
368 if (test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags)) {
369 ASSERTCMP(object->n_exclusive, >, 0);
370 object->n_exclusive--;
373 ASSERTCMP(object->n_in_progress, >, 0);
374 object->n_in_progress--;
375 if (object->n_in_progress == 0)
376 fscache_start_operations(object);
378 ASSERTCMP(object->n_ops, >, 0);
379 object->n_ops--;
380 if (object->n_ops == 0)
381 fscache_raise_event(object, FSCACHE_OBJECT_EV_CLEARED);
383 spin_unlock(&object->lock);
385 kfree(op);
386 _leave(" [done]");
388 EXPORT_SYMBOL(fscache_put_operation);
391 * garbage collect operations that have had their release deferred
393 void fscache_operation_gc(struct work_struct *work)
395 struct fscache_operation *op;
396 struct fscache_object *object;
397 struct fscache_cache *cache =
398 container_of(work, struct fscache_cache, op_gc);
399 int count = 0;
401 _enter("");
403 do {
404 spin_lock(&cache->op_gc_list_lock);
405 if (list_empty(&cache->op_gc_list)) {
406 spin_unlock(&cache->op_gc_list_lock);
407 break;
410 op = list_entry(cache->op_gc_list.next,
411 struct fscache_operation, pend_link);
412 list_del(&op->pend_link);
413 spin_unlock(&cache->op_gc_list_lock);
415 object = op->object;
417 _debug("GC DEFERRED REL OBJ%x OP%x",
418 object->debug_id, op->debug_id);
419 fscache_stat(&fscache_n_op_gc);
421 ASSERTCMP(atomic_read(&op->usage), ==, 0);
423 spin_lock(&object->lock);
424 if (test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags)) {
425 ASSERTCMP(object->n_exclusive, >, 0);
426 object->n_exclusive--;
429 ASSERTCMP(object->n_in_progress, >, 0);
430 object->n_in_progress--;
431 if (object->n_in_progress == 0)
432 fscache_start_operations(object);
434 ASSERTCMP(object->n_ops, >, 0);
435 object->n_ops--;
436 if (object->n_ops == 0)
437 fscache_raise_event(object, FSCACHE_OBJECT_EV_CLEARED);
439 spin_unlock(&object->lock);
441 } while (count++ < 20);
443 if (!list_empty(&cache->op_gc_list))
444 schedule_work(&cache->op_gc);
446 _leave("");
450 * execute an operation using fs_op_wq to provide processing context -
451 * the caller holds a ref to this object, so we don't need to hold one
453 void fscache_op_work_func(struct work_struct *work)
455 struct fscache_operation *op =
456 container_of(work, struct fscache_operation, work);
457 unsigned long start;
459 _enter("{OBJ%x OP%x,%d}",
460 op->object->debug_id, op->debug_id, atomic_read(&op->usage));
462 ASSERT(op->processor != NULL);
463 start = jiffies;
464 op->processor(op);
465 fscache_hist(fscache_ops_histogram, start);
466 fscache_put_operation(op);
468 _leave("");