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>
19 atomic_t fscache_op_debug_id;
20 EXPORT_SYMBOL(fscache_op_debug_id);
23 * fscache_enqueue_operation - Enqueue an operation for processing
24 * @op: The operation to enqueue
26 * Enqueue an operation for processing by the FS-Cache thread pool.
28 * This will get its own ref on the object.
30 void fscache_enqueue_operation(struct fscache_operation *op)
32 _enter("{OBJ%x OP%x,%u}",
33 op->object->debug_id, op->debug_id, atomic_read(&op->usage));
35 fscache_set_op_state(op, "EnQ");
37 ASSERT(op->processor != NULL);
38 ASSERTCMP(op->object->state, >=, FSCACHE_OBJECT_AVAILABLE);
39 ASSERTCMP(atomic_read(&op->usage), >, 0);
41 if (list_empty(&op->pend_link)) {
42 switch (op->flags & FSCACHE_OP_TYPE) {
45 atomic_inc(&op->usage);
46 if (!schedule_work(&op->fast_work))
47 fscache_put_operation(op);
51 slow_work_enqueue(&op->slow_work);
53 case FSCACHE_OP_MYTHREAD:
54 _debug("queue for caller's attention");
57 printk(KERN_ERR "FS-Cache: Unexpected op type %lx",
62 fscache_stat(&fscache_n_op_enqueue);
65 EXPORT_SYMBOL(fscache_enqueue_operation);
70 static void fscache_run_op(struct fscache_object *object,
71 struct fscache_operation *op)
73 fscache_set_op_state(op, "Run");
75 object->n_in_progress++;
76 if (test_and_clear_bit(FSCACHE_OP_WAITING, &op->flags))
77 wake_up_bit(&op->flags, FSCACHE_OP_WAITING);
79 fscache_enqueue_operation(op);
80 fscache_stat(&fscache_n_op_run);
84 * submit an exclusive operation for an object
85 * - other ops are excluded from running simultaneously with this one
86 * - this gets any extra refs it needs on an op
88 int fscache_submit_exclusive_op(struct fscache_object *object,
89 struct fscache_operation *op)
93 _enter("{OBJ%x OP%x},", object->debug_id, op->debug_id);
95 fscache_set_op_state(op, "SubmitX");
97 spin_lock(&object->lock);
98 ASSERTCMP(object->n_ops, >=, object->n_in_progress);
99 ASSERTCMP(object->n_ops, >=, object->n_exclusive);
102 if (fscache_object_is_active(object)) {
105 object->n_exclusive++; /* reads and writes must wait */
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);
117 ASSERTCMP(object->n_in_progress, ==, 0);
118 fscache_run_op(object, op);
121 /* need to issue a new write op after this */
122 clear_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags);
124 } else if (object->state == FSCACHE_OBJECT_CREATING) {
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);
133 /* not allowed to submit ops in any other state */
137 spin_unlock(&object->lock);
142 * report an unexpected submission
144 static void fscache_report_unexpected_submission(struct fscache_object *object,
145 struct fscache_operation *op,
146 unsigned long ostate)
148 static bool once_only;
149 struct fscache_operation *p;
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);
167 if (!list_empty(&object->pending_ops)) {
169 list_for_each_entry(p, &object->pending_ops, pend_link) {
170 ASSERTCMP(p->object, ==, object);
171 kdebug("%p %p", op->processor, op->release);
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
189 int fscache_submit_op(struct fscache_object *object,
190 struct fscache_operation *op)
192 unsigned long ostate;
195 _enter("{OBJ%x OP%x},{%u}",
196 object->debug_id, op->debug_id, atomic_read(&op->usage));
198 ASSERTCMP(atomic_read(&op->usage), >, 0);
200 fscache_set_op_state(op, "Submit");
202 spin_lock(&object->lock);
203 ASSERTCMP(object->n_ops, >=, object->n_in_progress);
204 ASSERTCMP(object->n_ops, >=, object->n_exclusive);
206 ostate = object->state;
209 if (fscache_object_is_active(object)) {
213 if (object->n_exclusive > 0) {
214 atomic_inc(&op->usage);
215 list_add_tail(&op->pend_link, &object->pending_ops);
216 fscache_stat(&fscache_n_op_pend);
217 } else if (!list_empty(&object->pending_ops)) {
218 atomic_inc(&op->usage);
219 list_add_tail(&op->pend_link, &object->pending_ops);
220 fscache_stat(&fscache_n_op_pend);
221 fscache_start_operations(object);
223 ASSERTCMP(object->n_exclusive, ==, 0);
224 fscache_run_op(object, op);
227 } else if (object->state == FSCACHE_OBJECT_CREATING) {
230 atomic_inc(&op->usage);
231 list_add_tail(&op->pend_link, &object->pending_ops);
232 fscache_stat(&fscache_n_op_pend);
234 } else if (!test_bit(FSCACHE_IOERROR, &object->cache->flags)) {
235 fscache_report_unexpected_submission(object, op, ostate);
236 ASSERT(!fscache_object_is_active(object));
242 spin_unlock(&object->lock);
247 * queue an object for withdrawal on error, aborting all following asynchronous
250 void fscache_abort_object(struct fscache_object *object)
252 _enter("{OBJ%x}", object->debug_id);
254 fscache_raise_event(object, FSCACHE_OBJECT_EV_ERROR);
258 * jump start the operation processing on an object
259 * - caller must hold object->lock
261 void fscache_start_operations(struct fscache_object *object)
263 struct fscache_operation *op;
266 while (!list_empty(&object->pending_ops) && !stop) {
267 op = list_entry(object->pending_ops.next,
268 struct fscache_operation, pend_link);
270 if (test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags)) {
271 if (object->n_in_progress > 0)
275 list_del_init(&op->pend_link);
276 object->n_in_progress++;
278 if (test_and_clear_bit(FSCACHE_OP_WAITING, &op->flags))
279 wake_up_bit(&op->flags, FSCACHE_OP_WAITING);
281 fscache_enqueue_operation(op);
283 /* the pending queue was holding a ref on the object */
284 fscache_put_operation(op);
287 ASSERTCMP(object->n_in_progress, <=, object->n_ops);
289 _debug("woke %d ops on OBJ%x",
290 object->n_in_progress, object->debug_id);
294 * release an operation
295 * - queues pending ops if this is the last in-progress op
297 void fscache_put_operation(struct fscache_operation *op)
299 struct fscache_object *object;
300 struct fscache_cache *cache;
302 _enter("{OBJ%x OP%x,%d}",
303 op->object->debug_id, op->debug_id, atomic_read(&op->usage));
305 ASSERTCMP(atomic_read(&op->usage), >, 0);
307 if (!atomic_dec_and_test(&op->usage))
310 fscache_set_op_state(op, "Put");
313 if (test_and_set_bit(FSCACHE_OP_DEAD, &op->flags))
316 fscache_stat(&fscache_n_op_release);
325 if (test_bit(FSCACHE_OP_DEC_READ_CNT, &op->flags))
326 atomic_dec(&object->n_reads);
328 /* now... we may get called with the object spinlock held, so we
329 * complete the cleanup here only if we can immediately acquire the
330 * lock, and defer it otherwise */
331 if (!spin_trylock(&object->lock)) {
333 fscache_stat(&fscache_n_op_deferred_release);
335 cache = object->cache;
336 spin_lock(&cache->op_gc_list_lock);
337 list_add_tail(&op->pend_link, &cache->op_gc_list);
338 spin_unlock(&cache->op_gc_list_lock);
339 schedule_work(&cache->op_gc);
344 if (test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags)) {
345 ASSERTCMP(object->n_exclusive, >, 0);
346 object->n_exclusive--;
349 ASSERTCMP(object->n_in_progress, >, 0);
350 object->n_in_progress--;
351 if (object->n_in_progress == 0)
352 fscache_start_operations(object);
354 ASSERTCMP(object->n_ops, >, 0);
356 if (object->n_ops == 0)
357 fscache_raise_event(object, FSCACHE_OBJECT_EV_CLEARED);
359 spin_unlock(&object->lock);
364 EXPORT_SYMBOL(fscache_put_operation);
367 * garbage collect operations that have had their release deferred
369 void fscache_operation_gc(struct work_struct *work)
371 struct fscache_operation *op;
372 struct fscache_object *object;
373 struct fscache_cache *cache =
374 container_of(work, struct fscache_cache, op_gc);
380 spin_lock(&cache->op_gc_list_lock);
381 if (list_empty(&cache->op_gc_list)) {
382 spin_unlock(&cache->op_gc_list_lock);
386 op = list_entry(cache->op_gc_list.next,
387 struct fscache_operation, pend_link);
388 list_del(&op->pend_link);
389 spin_unlock(&cache->op_gc_list_lock);
393 _debug("GC DEFERRED REL OBJ%x OP%x",
394 object->debug_id, op->debug_id);
395 fscache_stat(&fscache_n_op_gc);
397 ASSERTCMP(atomic_read(&op->usage), ==, 0);
399 spin_lock(&object->lock);
400 if (test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags)) {
401 ASSERTCMP(object->n_exclusive, >, 0);
402 object->n_exclusive--;
405 ASSERTCMP(object->n_in_progress, >, 0);
406 object->n_in_progress--;
407 if (object->n_in_progress == 0)
408 fscache_start_operations(object);
410 ASSERTCMP(object->n_ops, >, 0);
412 if (object->n_ops == 0)
413 fscache_raise_event(object, FSCACHE_OBJECT_EV_CLEARED);
415 spin_unlock(&object->lock);
417 } while (count++ < 20);
419 if (!list_empty(&cache->op_gc_list))
420 schedule_work(&cache->op_gc);
426 * allow the slow work item processor to get a ref on an operation
428 static int fscache_op_get_ref(struct slow_work *work)
430 struct fscache_operation *op =
431 container_of(work, struct fscache_operation, slow_work);
433 atomic_inc(&op->usage);
438 * allow the slow work item processor to discard a ref on an operation
440 static void fscache_op_put_ref(struct slow_work *work)
442 struct fscache_operation *op =
443 container_of(work, struct fscache_operation, slow_work);
445 fscache_put_operation(op);
449 * execute an operation using the slow thread pool to provide processing context
450 * - the caller holds a ref to this object, so we don't need to hold one
452 static void fscache_op_execute(struct slow_work *work)
454 struct fscache_operation *op =
455 container_of(work, struct fscache_operation, slow_work);
458 _enter("{OBJ%x OP%x,%d}",
459 op->object->debug_id, op->debug_id, atomic_read(&op->usage));
461 ASSERT(op->processor != NULL);
464 fscache_hist(fscache_ops_histogram, start);
470 * describe an operation for slow-work debugging
472 #ifdef CONFIG_SLOW_WORK_PROC
473 static void fscache_op_desc(struct slow_work *work, struct seq_file *m)
475 struct fscache_operation *op =
476 container_of(work, struct fscache_operation, slow_work);
478 seq_printf(m, "FSC: OBJ%x OP%x: %s/%s fl=%lx",
479 op->object->debug_id, op->debug_id,
480 op->name, op->state, op->flags);
484 const struct slow_work_ops fscache_op_slow_work_ops = {
485 .owner = THIS_MODULE,
486 .get_ref = fscache_op_get_ref,
487 .put_ref = fscache_op_put_ref,
488 .execute = fscache_op_execute,
489 #ifdef CONFIG_SLOW_WORK_PROC
490 .desc = fscache_op_desc,