blob: 09e43b6e822fa4eb891a3c76bfe8aa3fc2dd90d1 [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 Howells952efe72009-04-03 16:42:39 +010037 ASSERT(op->processor != NULL);
38 ASSERTCMP(op->object->state, >=, FSCACHE_OBJECT_AVAILABLE);
39 ASSERTCMP(atomic_read(&op->usage), >, 0);
40
41 if (list_empty(&op->pend_link)) {
42 switch (op->flags & FSCACHE_OP_TYPE) {
43 case FSCACHE_OP_FAST:
44 _debug("queue fast");
45 atomic_inc(&op->usage);
46 if (!schedule_work(&op->fast_work))
47 fscache_put_operation(op);
48 break;
49 case FSCACHE_OP_SLOW:
50 _debug("queue slow");
51 slow_work_enqueue(&op->slow_work);
52 break;
53 case FSCACHE_OP_MYTHREAD:
54 _debug("queue for caller's attention");
55 break;
56 default:
57 printk(KERN_ERR "FS-Cache: Unexpected op type %lx",
58 op->flags);
59 BUG();
60 break;
61 }
62 fscache_stat(&fscache_n_op_enqueue);
63 }
64}
65EXPORT_SYMBOL(fscache_enqueue_operation);
66
67/*
68 * start an op running
69 */
70static void fscache_run_op(struct fscache_object *object,
71 struct fscache_operation *op)
72{
David Howells440f0af2009-11-19 18:11:01 +000073 fscache_set_op_state(op, "Run");
74
David Howells952efe72009-04-03 16:42:39 +010075 object->n_in_progress++;
76 if (test_and_clear_bit(FSCACHE_OP_WAITING, &op->flags))
77 wake_up_bit(&op->flags, FSCACHE_OP_WAITING);
78 if (op->processor)
79 fscache_enqueue_operation(op);
80 fscache_stat(&fscache_n_op_run);
81}
82
83/*
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
87 */
88int fscache_submit_exclusive_op(struct fscache_object *object,
89 struct fscache_operation *op)
90{
91 int ret;
92
93 _enter("{OBJ%x OP%x},", object->debug_id, op->debug_id);
94
David Howells440f0af2009-11-19 18:11:01 +000095 fscache_set_op_state(op, "SubmitX");
96
David Howells952efe72009-04-03 16:42:39 +010097 spin_lock(&object->lock);
98 ASSERTCMP(object->n_ops, >=, object->n_in_progress);
99 ASSERTCMP(object->n_ops, >=, object->n_exclusive);
100
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);
205
206 ostate = object->state;
207 smp_rmb();
208
209 if (fscache_object_is_active(object)) {
210 op->object = object;
211 object->n_ops++;
212
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);
222 } else {
223 ASSERTCMP(object->n_exclusive, ==, 0);
224 fscache_run_op(object, op);
225 }
226 ret = 0;
227 } else if (object->state == FSCACHE_OBJECT_CREATING) {
228 op->object = object;
229 object->n_ops++;
230 atomic_inc(&op->usage);
231 list_add_tail(&op->pend_link, &object->pending_ops);
232 fscache_stat(&fscache_n_op_pend);
233 ret = 0;
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));
237 ret = -ENOBUFS;
238 } else {
239 ret = -ENOBUFS;
240 }
241
242 spin_unlock(&object->lock);
243 return ret;
244}
245
246/*
247 * queue an object for withdrawal on error, aborting all following asynchronous
248 * operations
249 */
250void fscache_abort_object(struct fscache_object *object)
251{
252 _enter("{OBJ%x}", object->debug_id);
253
254 fscache_raise_event(object, FSCACHE_OBJECT_EV_ERROR);
255}
256
257/*
258 * jump start the operation processing on an object
259 * - caller must hold object->lock
260 */
261void fscache_start_operations(struct fscache_object *object)
262{
263 struct fscache_operation *op;
264 bool stop = false;
265
266 while (!list_empty(&object->pending_ops) && !stop) {
267 op = list_entry(object->pending_ops.next,
268 struct fscache_operation, pend_link);
269
270 if (test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags)) {
271 if (object->n_in_progress > 0)
272 break;
273 stop = true;
274 }
275 list_del_init(&op->pend_link);
276 object->n_in_progress++;
277
278 if (test_and_clear_bit(FSCACHE_OP_WAITING, &op->flags))
279 wake_up_bit(&op->flags, FSCACHE_OP_WAITING);
280 if (op->processor)
281 fscache_enqueue_operation(op);
282
283 /* the pending queue was holding a ref on the object */
284 fscache_put_operation(op);
285 }
286
287 ASSERTCMP(object->n_in_progress, <=, object->n_ops);
288
289 _debug("woke %d ops on OBJ%x",
290 object->n_in_progress, object->debug_id);
291}
292
293/*
294 * release an operation
295 * - queues pending ops if this is the last in-progress op
296 */
297void fscache_put_operation(struct fscache_operation *op)
298{
299 struct fscache_object *object;
300 struct fscache_cache *cache;
301
302 _enter("{OBJ%x OP%x,%d}",
303 op->object->debug_id, op->debug_id, atomic_read(&op->usage));
304
305 ASSERTCMP(atomic_read(&op->usage), >, 0);
306
307 if (!atomic_dec_and_test(&op->usage))
308 return;
309
David Howells440f0af2009-11-19 18:11:01 +0000310 fscache_set_op_state(op, "Put");
311
David Howells952efe72009-04-03 16:42:39 +0100312 _debug("PUT OP");
313 if (test_and_set_bit(FSCACHE_OP_DEAD, &op->flags))
314 BUG();
315
316 fscache_stat(&fscache_n_op_release);
317
318 if (op->release) {
319 op->release(op);
320 op->release = NULL;
321 }
322
323 object = op->object;
324
David Howells4fbf4292009-11-19 18:11:04 +0000325 if (test_bit(FSCACHE_OP_DEC_READ_CNT, &op->flags))
326 atomic_dec(&object->n_reads);
327
David Howells952efe72009-04-03 16:42:39 +0100328 /* 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)) {
332 _debug("defer put");
333 fscache_stat(&fscache_n_op_deferred_release);
334
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);
340 _leave(" [defer]");
341 return;
342 }
343
344 if (test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags)) {
345 ASSERTCMP(object->n_exclusive, >, 0);
346 object->n_exclusive--;
347 }
348
349 ASSERTCMP(object->n_in_progress, >, 0);
350 object->n_in_progress--;
351 if (object->n_in_progress == 0)
352 fscache_start_operations(object);
353
354 ASSERTCMP(object->n_ops, >, 0);
355 object->n_ops--;
356 if (object->n_ops == 0)
357 fscache_raise_event(object, FSCACHE_OBJECT_EV_CLEARED);
358
359 spin_unlock(&object->lock);
360
361 kfree(op);
362 _leave(" [done]");
363}
364EXPORT_SYMBOL(fscache_put_operation);
365
366/*
367 * garbage collect operations that have had their release deferred
368 */
369void fscache_operation_gc(struct work_struct *work)
370{
371 struct fscache_operation *op;
372 struct fscache_object *object;
373 struct fscache_cache *cache =
374 container_of(work, struct fscache_cache, op_gc);
375 int count = 0;
376
377 _enter("");
378
379 do {
380 spin_lock(&cache->op_gc_list_lock);
381 if (list_empty(&cache->op_gc_list)) {
382 spin_unlock(&cache->op_gc_list_lock);
383 break;
384 }
385
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);
390
391 object = op->object;
392
393 _debug("GC DEFERRED REL OBJ%x OP%x",
394 object->debug_id, op->debug_id);
395 fscache_stat(&fscache_n_op_gc);
396
397 ASSERTCMP(atomic_read(&op->usage), ==, 0);
398
399 spin_lock(&object->lock);
400 if (test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags)) {
401 ASSERTCMP(object->n_exclusive, >, 0);
402 object->n_exclusive--;
403 }
404
405 ASSERTCMP(object->n_in_progress, >, 0);
406 object->n_in_progress--;
407 if (object->n_in_progress == 0)
408 fscache_start_operations(object);
409
410 ASSERTCMP(object->n_ops, >, 0);
411 object->n_ops--;
412 if (object->n_ops == 0)
413 fscache_raise_event(object, FSCACHE_OBJECT_EV_CLEARED);
414
415 spin_unlock(&object->lock);
416
417 } while (count++ < 20);
418
419 if (!list_empty(&cache->op_gc_list))
420 schedule_work(&cache->op_gc);
421
422 _leave("");
423}
424
425/*
426 * allow the slow work item processor to get a ref on an operation
427 */
428static int fscache_op_get_ref(struct slow_work *work)
429{
430 struct fscache_operation *op =
431 container_of(work, struct fscache_operation, slow_work);
432
433 atomic_inc(&op->usage);
434 return 0;
435}
436
437/*
438 * allow the slow work item processor to discard a ref on an operation
439 */
440static void fscache_op_put_ref(struct slow_work *work)
441{
442 struct fscache_operation *op =
443 container_of(work, struct fscache_operation, slow_work);
444
445 fscache_put_operation(op);
446}
447
448/*
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
451 */
452static void fscache_op_execute(struct slow_work *work)
453{
454 struct fscache_operation *op =
455 container_of(work, struct fscache_operation, slow_work);
456 unsigned long start;
457
458 _enter("{OBJ%x OP%x,%d}",
459 op->object->debug_id, op->debug_id, atomic_read(&op->usage));
460
461 ASSERT(op->processor != NULL);
462 start = jiffies;
463 op->processor(op);
464 fscache_hist(fscache_ops_histogram, start);
465
466 _leave("");
467}
468
David Howells440f0af2009-11-19 18:11:01 +0000469/*
470 * describe an operation for slow-work debugging
471 */
472#ifdef CONFIG_SLOW_WORK_PROC
473static void fscache_op_desc(struct slow_work *work, struct seq_file *m)
474{
475 struct fscache_operation *op =
476 container_of(work, struct fscache_operation, slow_work);
477
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);
481}
482#endif
483
David Howells952efe72009-04-03 16:42:39 +0100484const struct slow_work_ops fscache_op_slow_work_ops = {
David Howells3d7a6412009-11-19 18:10:23 +0000485 .owner = THIS_MODULE,
David Howells952efe72009-04-03 16:42:39 +0100486 .get_ref = fscache_op_get_ref,
487 .put_ref = fscache_op_put_ref,
488 .execute = fscache_op_execute,
David Howells440f0af2009-11-19 18:11:01 +0000489#ifdef CONFIG_SLOW_WORK_PROC
490 .desc = fscache_op_desc,
491#endif
David Howells952efe72009-04-03 16:42:39 +0100492};