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>
20 atomic_t fscache_op_debug_id;
21 EXPORT_SYMBOL(fscache_op_debug_id);
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);
40 ASSERTCMP(op->state, ==, FSCACHE_OP_ST_IN_PROGRESS);
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);
50 case FSCACHE_OP_MYTHREAD:
51 _debug("queue for caller's attention");
54 printk(KERN_ERR "FS-Cache: Unexpected op type %lx",
60 EXPORT_SYMBOL(fscache_enqueue_operation);
65 static void fscache_run_op(struct fscache_object *object,
66 struct fscache_operation *op)
68 ASSERTCMP(op->state, ==, FSCACHE_OP_ST_PENDING);
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);
75 fscache_enqueue_operation(op);
76 fscache_stat(&fscache_n_op_run);
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
84 int fscache_submit_exclusive_op(struct fscache_object *object,
85 struct fscache_operation *op)
87 _enter("{OBJ%x OP%x},", object->debug_id, op->debug_id);
89 ASSERTCMP(op->state, ==, FSCACHE_OP_ST_INITIALISED);
90 ASSERTCMP(atomic_read(&op->usage), >, 0);
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));
97 op->state = FSCACHE_OP_ST_PENDING;
98 if (fscache_object_is_active(object)) {
101 object->n_exclusive++; /* reads and writes must wait */
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);
113 ASSERTCMP(object->n_in_progress, ==, 0);
114 fscache_run_op(object, op);
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) {
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);
127 /* not allowed to submit ops in any other state */
131 spin_unlock(&object->lock);
136 * report an unexpected submission
138 static void fscache_report_unexpected_submission(struct fscache_object *object,
139 struct fscache_operation *op,
140 unsigned long ostate)
142 static bool once_only;
143 struct fscache_operation *p;
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);
161 if (!list_empty(&object->pending_ops)) {
163 list_for_each_entry(p, &object->pending_ops, pend_link) {
164 ASSERTCMP(p->object, ==, object);
165 kdebug("%p %p", op->processor, op->release);
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
183 int fscache_submit_op(struct fscache_object *object,
184 struct fscache_operation *op)
186 unsigned long ostate;
189 _enter("{OBJ%x OP%x},{%u}",
190 object->debug_id, op->debug_id, atomic_read(&op->usage));
192 ASSERTCMP(op->state, ==, FSCACHE_OP_ST_INITIALISED);
193 ASSERTCMP(atomic_read(&op->usage), >, 0);
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));
200 ostate = object->state;
203 op->state = FSCACHE_OP_ST_PENDING;
204 if (fscache_object_is_active(object)) {
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);
218 ASSERTCMP(object->n_exclusive, ==, 0);
219 fscache_run_op(object, op);
222 } else if (object->state == FSCACHE_OBJECT_CREATING) {
225 atomic_inc(&op->usage);
226 list_add_tail(&op->pend_link, &object->pending_ops);
227 fscache_stat(&fscache_n_op_pend);
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;
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;
241 op->state = FSCACHE_OP_ST_CANCELLED;
245 spin_unlock(&object->lock);
250 * queue an object for withdrawal on error, aborting all following asynchronous
253 void fscache_abort_object(struct fscache_object *object)
255 _enter("{OBJ%x}", object->debug_id);
257 fscache_raise_event(object, FSCACHE_OBJECT_EV_ERROR);
261 * jump start the operation processing on an object
262 * - caller must hold object->lock
264 void fscache_start_operations(struct fscache_object *object)
266 struct fscache_operation *op;
269 while (!list_empty(&object->pending_ops) && !stop) {
270 op = list_entry(object->pending_ops.next,
271 struct fscache_operation, pend_link);
273 if (test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags)) {
274 if (object->n_in_progress > 0)
278 list_del_init(&op->pend_link);
279 fscache_run_op(object, op);
281 /* the pending queue was holding a ref on the object */
282 fscache_put_operation(op);
285 ASSERTCMP(object->n_in_progress, <=, object->n_ops);
287 _debug("woke %d ops on OBJ%x",
288 object->n_in_progress, object->debug_id);
292 * cancel an operation that's pending on an object
294 int fscache_cancel_op(struct fscache_operation *op)
296 struct fscache_object *object = op->object;
299 _enter("OBJ%x OP%x}", op->object->debug_id, op->debug_id);
301 ASSERTCMP(op->state, >=, FSCACHE_OP_ST_PENDING);
302 ASSERTCMP(op->state, !=, FSCACHE_OP_ST_CANCELLED);
303 ASSERTCMP(atomic_read(&op->usage), >, 0);
305 spin_lock(&object->lock);
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);
321 spin_unlock(&object->lock);
322 _leave(" = %d", ret);
327 * Record the completion of an in-progress operation.
329 void fscache_op_complete(struct fscache_operation *op)
331 struct fscache_object *object = op->object;
333 _enter("OBJ%x", object->debug_id);
335 ASSERTCMP(op->state, ==, FSCACHE_OP_ST_IN_PROGRESS);
336 ASSERTCMP(object->n_in_progress, >, 0);
337 ASSERTIFCMP(test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags),
338 object->n_exclusive, >, 0);
339 ASSERTIFCMP(test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags),
340 object->n_in_progress, ==, 1);
342 spin_lock(&object->lock);
344 op->state = FSCACHE_OP_ST_COMPLETE;
346 if (test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags))
347 object->n_exclusive--;
348 object->n_in_progress--;
349 if (object->n_in_progress == 0)
350 fscache_start_operations(object);
352 spin_unlock(&object->lock);
355 EXPORT_SYMBOL(fscache_op_complete);
358 * release an operation
359 * - queues pending ops if this is the last in-progress op
361 void fscache_put_operation(struct fscache_operation *op)
363 struct fscache_object *object;
364 struct fscache_cache *cache;
366 _enter("{OBJ%x OP%x,%d}",
367 op->object->debug_id, op->debug_id, atomic_read(&op->usage));
369 ASSERTCMP(atomic_read(&op->usage), >, 0);
371 if (!atomic_dec_and_test(&op->usage))
375 ASSERTIFCMP(op->state != FSCACHE_OP_ST_COMPLETE,
376 op->state, ==, FSCACHE_OP_ST_CANCELLED);
377 op->state = FSCACHE_OP_ST_DEAD;
379 fscache_stat(&fscache_n_op_release);
388 if (test_bit(FSCACHE_OP_DEC_READ_CNT, &op->flags)) {
389 if (atomic_dec_and_test(&object->n_reads)) {
390 clear_bit(FSCACHE_COOKIE_WAITING_ON_READS,
391 &object->cookie->flags);
392 wake_up_bit(&object->cookie->flags,
393 FSCACHE_COOKIE_WAITING_ON_READS);
397 /* now... we may get called with the object spinlock held, so we
398 * complete the cleanup here only if we can immediately acquire the
399 * lock, and defer it otherwise */
400 if (!spin_trylock(&object->lock)) {
402 fscache_stat(&fscache_n_op_deferred_release);
404 cache = object->cache;
405 spin_lock(&cache->op_gc_list_lock);
406 list_add_tail(&op->pend_link, &cache->op_gc_list);
407 spin_unlock(&cache->op_gc_list_lock);
408 schedule_work(&cache->op_gc);
413 ASSERTCMP(object->n_ops, >, 0);
415 if (object->n_ops == 0)
416 fscache_raise_event(object, FSCACHE_OBJECT_EV_CLEARED);
418 spin_unlock(&object->lock);
423 EXPORT_SYMBOL(fscache_put_operation);
426 * garbage collect operations that have had their release deferred
428 void fscache_operation_gc(struct work_struct *work)
430 struct fscache_operation *op;
431 struct fscache_object *object;
432 struct fscache_cache *cache =
433 container_of(work, struct fscache_cache, op_gc);
439 spin_lock(&cache->op_gc_list_lock);
440 if (list_empty(&cache->op_gc_list)) {
441 spin_unlock(&cache->op_gc_list_lock);
445 op = list_entry(cache->op_gc_list.next,
446 struct fscache_operation, pend_link);
447 list_del(&op->pend_link);
448 spin_unlock(&cache->op_gc_list_lock);
451 spin_lock(&object->lock);
453 _debug("GC DEFERRED REL OBJ%x OP%x",
454 object->debug_id, op->debug_id);
455 fscache_stat(&fscache_n_op_gc);
457 ASSERTCMP(atomic_read(&op->usage), ==, 0);
458 ASSERTCMP(op->state, ==, FSCACHE_OP_ST_DEAD);
460 ASSERTCMP(object->n_ops, >, 0);
462 if (object->n_ops == 0)
463 fscache_raise_event(object, FSCACHE_OBJECT_EV_CLEARED);
465 spin_unlock(&object->lock);
468 } while (count++ < 20);
470 if (!list_empty(&cache->op_gc_list))
471 schedule_work(&cache->op_gc);
477 * execute an operation using fs_op_wq to provide processing context -
478 * the caller holds a ref to this object, so we don't need to hold one
480 void fscache_op_work_func(struct work_struct *work)
482 struct fscache_operation *op =
483 container_of(work, struct fscache_operation, work);
486 _enter("{OBJ%x OP%x,%d}",
487 op->object->debug_id, op->debug_id, atomic_read(&op->usage));
489 ASSERT(op->processor != NULL);
492 fscache_hist(fscache_ops_histogram, start);
493 fscache_put_operation(op);