Commit | Line | Data |
---|---|---|
952efe7b DH |
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> | |
440f0aff | 16 | #include <linux/seq_file.h> |
5a0e3ad6 | 17 | #include <linux/slab.h> |
952efe7b DH |
18 | #include "internal.h" |
19 | ||
20 | atomic_t fscache_op_debug_id; | |
21 | EXPORT_SYMBOL(fscache_op_debug_id); | |
22 | ||
d3b97ca4 DH |
23 | static void fscache_operation_dummy_cancel(struct fscache_operation *op) |
24 | { | |
25 | } | |
26 | ||
1339ec98 DH |
27 | /** |
28 | * fscache_operation_init - Do basic initialisation of an operation | |
29 | * @op: The operation to initialise | |
30 | * @release: The release function to assign | |
31 | * | |
32 | * Do basic initialisation of an operation. The caller must still set flags, | |
33 | * object and processor if needed. | |
34 | */ | |
08c2e3d0 DH |
35 | void fscache_operation_init(struct fscache_cookie *cookie, |
36 | struct fscache_operation *op, | |
1339ec98 | 37 | fscache_operation_processor_t processor, |
d3b97ca4 | 38 | fscache_operation_cancel_t cancel, |
1339ec98 DH |
39 | fscache_operation_release_t release) |
40 | { | |
41 | INIT_WORK(&op->work, fscache_op_work_func); | |
42 | atomic_set(&op->usage, 1); | |
43 | op->state = FSCACHE_OP_ST_INITIALISED; | |
44 | op->debug_id = atomic_inc_return(&fscache_op_debug_id); | |
45 | op->processor = processor; | |
d3b97ca4 | 46 | op->cancel = cancel ?: fscache_operation_dummy_cancel; |
1339ec98 DH |
47 | op->release = release; |
48 | INIT_LIST_HEAD(&op->pend_link); | |
03cdd0e4 | 49 | fscache_stat(&fscache_n_op_initialised); |
08c2e3d0 | 50 | trace_fscache_op(cookie, op, fscache_op_init); |
1339ec98 DH |
51 | } |
52 | EXPORT_SYMBOL(fscache_operation_init); | |
53 | ||
952efe7b DH |
54 | /** |
55 | * fscache_enqueue_operation - Enqueue an operation for processing | |
56 | * @op: The operation to enqueue | |
57 | * | |
58 | * Enqueue an operation for processing by the FS-Cache thread pool. | |
59 | * | |
60 | * This will get its own ref on the object. | |
61 | */ | |
62 | void fscache_enqueue_operation(struct fscache_operation *op) | |
63 | { | |
08c2e3d0 DH |
64 | struct fscache_cookie *cookie = op->object->cookie; |
65 | ||
952efe7b DH |
66 | _enter("{OBJ%x OP%x,%u}", |
67 | op->object->debug_id, op->debug_id, atomic_read(&op->usage)); | |
68 | ||
5753c441 | 69 | ASSERT(list_empty(&op->pend_link)); |
952efe7b | 70 | ASSERT(op->processor != NULL); |
493f7bc1 | 71 | ASSERT(fscache_object_is_available(op->object)); |
952efe7b | 72 | ASSERTCMP(atomic_read(&op->usage), >, 0); |
d0eb06af KKM |
73 | ASSERTIFCMP(op->state != FSCACHE_OP_ST_IN_PROGRESS, |
74 | op->state, ==, FSCACHE_OP_ST_CANCELLED); | |
952efe7b | 75 | |
5753c441 DH |
76 | fscache_stat(&fscache_n_op_enqueue); |
77 | switch (op->flags & FSCACHE_OP_TYPE) { | |
8af7c124 | 78 | case FSCACHE_OP_ASYNC: |
08c2e3d0 | 79 | trace_fscache_op(cookie, op, fscache_op_enqueue_async); |
8af7c124 | 80 | _debug("queue async"); |
5753c441 | 81 | atomic_inc(&op->usage); |
8af7c124 | 82 | if (!queue_work(fscache_op_wq, &op->work)) |
5753c441 DH |
83 | fscache_put_operation(op); |
84 | break; | |
5753c441 | 85 | case FSCACHE_OP_MYTHREAD: |
08c2e3d0 | 86 | trace_fscache_op(cookie, op, fscache_op_enqueue_mythread); |
5753c441 DH |
87 | _debug("queue for caller's attention"); |
88 | break; | |
89 | default: | |
36dfd116 | 90 | pr_err("Unexpected op type %lx", op->flags); |
5753c441 DH |
91 | BUG(); |
92 | break; | |
952efe7b DH |
93 | } |
94 | } | |
95 | EXPORT_SYMBOL(fscache_enqueue_operation); | |
96 | ||
97 | /* | |
98 | * start an op running | |
99 | */ | |
100 | static void fscache_run_op(struct fscache_object *object, | |
101 | struct fscache_operation *op) | |
102 | { | |
9f10523f DH |
103 | ASSERTCMP(op->state, ==, FSCACHE_OP_ST_PENDING); |
104 | ||
105 | op->state = FSCACHE_OP_ST_IN_PROGRESS; | |
952efe7b DH |
106 | object->n_in_progress++; |
107 | if (test_and_clear_bit(FSCACHE_OP_WAITING, &op->flags)) | |
108 | wake_up_bit(&op->flags, FSCACHE_OP_WAITING); | |
109 | if (op->processor) | |
110 | fscache_enqueue_operation(op); | |
08c2e3d0 DH |
111 | else |
112 | trace_fscache_op(object->cookie, op, fscache_op_run); | |
952efe7b DH |
113 | fscache_stat(&fscache_n_op_run); |
114 | } | |
115 | ||
3c305984 DH |
116 | /* |
117 | * report an unexpected submission | |
118 | */ | |
119 | static void fscache_report_unexpected_submission(struct fscache_object *object, | |
120 | struct fscache_operation *op, | |
121 | const struct fscache_state *ostate) | |
122 | { | |
123 | static bool once_only; | |
124 | struct fscache_operation *p; | |
125 | unsigned n; | |
126 | ||
127 | if (once_only) | |
128 | return; | |
129 | once_only = true; | |
130 | ||
131 | kdebug("unexpected submission OP%x [OBJ%x %s]", | |
132 | op->debug_id, object->debug_id, object->state->name); | |
133 | kdebug("objstate=%s [%s]", object->state->name, ostate->name); | |
134 | kdebug("objflags=%lx", object->flags); | |
135 | kdebug("objevent=%lx [%lx]", object->events, object->event_mask); | |
136 | kdebug("ops=%u inp=%u exc=%u", | |
137 | object->n_ops, object->n_in_progress, object->n_exclusive); | |
138 | ||
139 | if (!list_empty(&object->pending_ops)) { | |
140 | n = 0; | |
141 | list_for_each_entry(p, &object->pending_ops, pend_link) { | |
142 | ASSERTCMP(p->object, ==, object); | |
143 | kdebug("%p %p", op->processor, op->release); | |
144 | n++; | |
145 | } | |
146 | ||
147 | kdebug("n=%u", n); | |
148 | } | |
149 | ||
150 | dump_stack(); | |
151 | } | |
152 | ||
952efe7b DH |
153 | /* |
154 | * submit an exclusive operation for an object | |
155 | * - other ops are excluded from running simultaneously with this one | |
156 | * - this gets any extra refs it needs on an op | |
157 | */ | |
158 | int fscache_submit_exclusive_op(struct fscache_object *object, | |
159 | struct fscache_operation *op) | |
160 | { | |
30ceec62 DH |
161 | const struct fscache_state *ostate; |
162 | unsigned long flags; | |
8d76349d DH |
163 | int ret; |
164 | ||
952efe7b DH |
165 | _enter("{OBJ%x OP%x},", object->debug_id, op->debug_id); |
166 | ||
08c2e3d0 DH |
167 | trace_fscache_op(object->cookie, op, fscache_op_submit_ex); |
168 | ||
9f10523f DH |
169 | ASSERTCMP(op->state, ==, FSCACHE_OP_ST_INITIALISED); |
170 | ASSERTCMP(atomic_read(&op->usage), >, 0); | |
171 | ||
952efe7b DH |
172 | spin_lock(&object->lock); |
173 | ASSERTCMP(object->n_ops, >=, object->n_in_progress); | |
174 | ASSERTCMP(object->n_ops, >=, object->n_exclusive); | |
5753c441 | 175 | ASSERT(list_empty(&op->pend_link)); |
952efe7b | 176 | |
30ceec62 DH |
177 | ostate = object->state; |
178 | smp_rmb(); | |
179 | ||
9f10523f | 180 | op->state = FSCACHE_OP_ST_PENDING; |
30ceec62 DH |
181 | flags = READ_ONCE(object->flags); |
182 | if (unlikely(!(flags & BIT(FSCACHE_OBJECT_IS_LIVE)))) { | |
183 | fscache_stat(&fscache_n_op_rejected); | |
d3b97ca4 | 184 | op->cancel(op); |
30ceec62 DH |
185 | op->state = FSCACHE_OP_ST_CANCELLED; |
186 | ret = -ENOBUFS; | |
187 | } else if (unlikely(fscache_cache_is_broken(object))) { | |
d3b97ca4 | 188 | op->cancel(op); |
30ceec62 DH |
189 | op->state = FSCACHE_OP_ST_CANCELLED; |
190 | ret = -EIO; | |
191 | } else if (flags & BIT(FSCACHE_OBJECT_IS_AVAILABLE)) { | |
952efe7b DH |
192 | op->object = object; |
193 | object->n_ops++; | |
194 | object->n_exclusive++; /* reads and writes must wait */ | |
195 | ||
9f10523f | 196 | if (object->n_in_progress > 0) { |
952efe7b DH |
197 | atomic_inc(&op->usage); |
198 | list_add_tail(&op->pend_link, &object->pending_ops); | |
199 | fscache_stat(&fscache_n_op_pend); | |
200 | } else if (!list_empty(&object->pending_ops)) { | |
201 | atomic_inc(&op->usage); | |
202 | list_add_tail(&op->pend_link, &object->pending_ops); | |
203 | fscache_stat(&fscache_n_op_pend); | |
204 | fscache_start_operations(object); | |
205 | } else { | |
206 | ASSERTCMP(object->n_in_progress, ==, 0); | |
207 | fscache_run_op(object, op); | |
208 | } | |
209 | ||
210 | /* need to issue a new write op after this */ | |
211 | clear_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags); | |
8d76349d | 212 | ret = 0; |
30ceec62 | 213 | } else if (flags & BIT(FSCACHE_OBJECT_IS_LOOKED_UP)) { |
952efe7b DH |
214 | op->object = object; |
215 | object->n_ops++; | |
216 | object->n_exclusive++; /* reads and writes must wait */ | |
217 | atomic_inc(&op->usage); | |
218 | list_add_tail(&op->pend_link, &object->pending_ops); | |
219 | fscache_stat(&fscache_n_op_pend); | |
8d76349d | 220 | ret = 0; |
6515d1db | 221 | } else if (flags & BIT(FSCACHE_OBJECT_KILLED_BY_CACHE)) { |
d3b97ca4 | 222 | op->cancel(op); |
6515d1db DH |
223 | op->state = FSCACHE_OP_ST_CANCELLED; |
224 | ret = -ENOBUFS; | |
952efe7b | 225 | } else { |
30ceec62 | 226 | fscache_report_unexpected_submission(object, op, ostate); |
d3b97ca4 | 227 | op->cancel(op); |
30ceec62 DH |
228 | op->state = FSCACHE_OP_ST_CANCELLED; |
229 | ret = -ENOBUFS; | |
952efe7b DH |
230 | } |
231 | ||
232 | spin_unlock(&object->lock); | |
8d76349d | 233 | return ret; |
952efe7b DH |
234 | } |
235 | ||
952efe7b DH |
236 | /* |
237 | * submit an operation for an object | |
238 | * - objects may be submitted only in the following states: | |
239 | * - during object creation (write ops may be submitted) | |
240 | * - whilst the object is active | |
241 | * - after an I/O error incurred in one of the two above states (op rejected) | |
242 | * - this gets any extra refs it needs on an op | |
243 | */ | |
244 | int fscache_submit_op(struct fscache_object *object, | |
245 | struct fscache_operation *op) | |
246 | { | |
caaef690 | 247 | const struct fscache_state *ostate; |
30ceec62 | 248 | unsigned long flags; |
952efe7b DH |
249 | int ret; |
250 | ||
251 | _enter("{OBJ%x OP%x},{%u}", | |
252 | object->debug_id, op->debug_id, atomic_read(&op->usage)); | |
253 | ||
08c2e3d0 DH |
254 | trace_fscache_op(object->cookie, op, fscache_op_submit); |
255 | ||
9f10523f | 256 | ASSERTCMP(op->state, ==, FSCACHE_OP_ST_INITIALISED); |
952efe7b DH |
257 | ASSERTCMP(atomic_read(&op->usage), >, 0); |
258 | ||
259 | spin_lock(&object->lock); | |
260 | ASSERTCMP(object->n_ops, >=, object->n_in_progress); | |
261 | ASSERTCMP(object->n_ops, >=, object->n_exclusive); | |
5753c441 | 262 | ASSERT(list_empty(&op->pend_link)); |
952efe7b DH |
263 | |
264 | ostate = object->state; | |
265 | smp_rmb(); | |
266 | ||
9f10523f | 267 | op->state = FSCACHE_OP_ST_PENDING; |
30ceec62 DH |
268 | flags = READ_ONCE(object->flags); |
269 | if (unlikely(!(flags & BIT(FSCACHE_OBJECT_IS_LIVE)))) { | |
270 | fscache_stat(&fscache_n_op_rejected); | |
d3b97ca4 | 271 | op->cancel(op); |
30ceec62 DH |
272 | op->state = FSCACHE_OP_ST_CANCELLED; |
273 | ret = -ENOBUFS; | |
274 | } else if (unlikely(fscache_cache_is_broken(object))) { | |
d3b97ca4 | 275 | op->cancel(op); |
30ceec62 DH |
276 | op->state = FSCACHE_OP_ST_CANCELLED; |
277 | ret = -EIO; | |
278 | } else if (flags & BIT(FSCACHE_OBJECT_IS_AVAILABLE)) { | |
952efe7b DH |
279 | op->object = object; |
280 | object->n_ops++; | |
281 | ||
282 | if (object->n_exclusive > 0) { | |
283 | atomic_inc(&op->usage); | |
284 | list_add_tail(&op->pend_link, &object->pending_ops); | |
285 | fscache_stat(&fscache_n_op_pend); | |
286 | } else if (!list_empty(&object->pending_ops)) { | |
287 | atomic_inc(&op->usage); | |
288 | list_add_tail(&op->pend_link, &object->pending_ops); | |
289 | fscache_stat(&fscache_n_op_pend); | |
290 | fscache_start_operations(object); | |
291 | } else { | |
292 | ASSERTCMP(object->n_exclusive, ==, 0); | |
293 | fscache_run_op(object, op); | |
294 | } | |
295 | ret = 0; | |
30ceec62 | 296 | } else if (flags & BIT(FSCACHE_OBJECT_IS_LOOKED_UP)) { |
952efe7b DH |
297 | op->object = object; |
298 | object->n_ops++; | |
299 | atomic_inc(&op->usage); | |
300 | list_add_tail(&op->pend_link, &object->pending_ops); | |
301 | fscache_stat(&fscache_n_op_pend); | |
302 | ret = 0; | |
6515d1db | 303 | } else if (flags & BIT(FSCACHE_OBJECT_KILLED_BY_CACHE)) { |
d3b97ca4 | 304 | op->cancel(op); |
6515d1db DH |
305 | op->state = FSCACHE_OP_ST_CANCELLED; |
306 | ret = -ENOBUFS; | |
30ceec62 | 307 | } else { |
952efe7b DH |
308 | fscache_report_unexpected_submission(object, op, ostate); |
309 | ASSERT(!fscache_object_is_active(object)); | |
d3b97ca4 | 310 | op->cancel(op); |
9f10523f | 311 | op->state = FSCACHE_OP_ST_CANCELLED; |
952efe7b | 312 | ret = -ENOBUFS; |
952efe7b DH |
313 | } |
314 | ||
315 | spin_unlock(&object->lock); | |
316 | return ret; | |
317 | } | |
318 | ||
319 | /* | |
320 | * queue an object for withdrawal on error, aborting all following asynchronous | |
321 | * operations | |
322 | */ | |
323 | void fscache_abort_object(struct fscache_object *object) | |
324 | { | |
325 | _enter("{OBJ%x}", object->debug_id); | |
326 | ||
327 | fscache_raise_event(object, FSCACHE_OBJECT_EV_ERROR); | |
328 | } | |
329 | ||
330 | /* | |
dcfae32f DH |
331 | * Jump start the operation processing on an object. The caller must hold |
332 | * object->lock. | |
952efe7b DH |
333 | */ |
334 | void fscache_start_operations(struct fscache_object *object) | |
335 | { | |
336 | struct fscache_operation *op; | |
337 | bool stop = false; | |
338 | ||
339 | while (!list_empty(&object->pending_ops) && !stop) { | |
340 | op = list_entry(object->pending_ops.next, | |
341 | struct fscache_operation, pend_link); | |
342 | ||
343 | if (test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags)) { | |
344 | if (object->n_in_progress > 0) | |
345 | break; | |
346 | stop = true; | |
347 | } | |
348 | list_del_init(&op->pend_link); | |
5753c441 | 349 | fscache_run_op(object, op); |
952efe7b DH |
350 | |
351 | /* the pending queue was holding a ref on the object */ | |
352 | fscache_put_operation(op); | |
353 | } | |
354 | ||
355 | ASSERTCMP(object->n_in_progress, <=, object->n_ops); | |
356 | ||
357 | _debug("woke %d ops on OBJ%x", | |
358 | object->n_in_progress, object->debug_id); | |
359 | } | |
360 | ||
5753c441 DH |
361 | /* |
362 | * cancel an operation that's pending on an object | |
363 | */ | |
91c7fbbf | 364 | int fscache_cancel_op(struct fscache_operation *op, |
418b7eb9 | 365 | bool cancel_in_progress_op) |
5753c441 DH |
366 | { |
367 | struct fscache_object *object = op->object; | |
418b7eb9 | 368 | bool put = false; |
5753c441 DH |
369 | int ret; |
370 | ||
371 | _enter("OBJ%x OP%x}", op->object->debug_id, op->debug_id); | |
372 | ||
08c2e3d0 DH |
373 | trace_fscache_op(object->cookie, op, fscache_op_cancel); |
374 | ||
9f10523f DH |
375 | ASSERTCMP(op->state, >=, FSCACHE_OP_ST_PENDING); |
376 | ASSERTCMP(op->state, !=, FSCACHE_OP_ST_CANCELLED); | |
377 | ASSERTCMP(atomic_read(&op->usage), >, 0); | |
378 | ||
5753c441 DH |
379 | spin_lock(&object->lock); |
380 | ||
381 | ret = -EBUSY; | |
9f10523f DH |
382 | if (op->state == FSCACHE_OP_ST_PENDING) { |
383 | ASSERT(!list_empty(&op->pend_link)); | |
5753c441 | 384 | list_del_init(&op->pend_link); |
418b7eb9 | 385 | put = true; |
d3b97ca4 | 386 | |
418b7eb9 | 387 | fscache_stat(&fscache_n_op_cancelled); |
d3b97ca4 | 388 | op->cancel(op); |
418b7eb9 DH |
389 | op->state = FSCACHE_OP_ST_CANCELLED; |
390 | if (test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags)) | |
391 | object->n_exclusive--; | |
392 | if (test_and_clear_bit(FSCACHE_OP_WAITING, &op->flags)) | |
393 | wake_up_bit(&op->flags, FSCACHE_OP_WAITING); | |
394 | ret = 0; | |
395 | } else if (op->state == FSCACHE_OP_ST_IN_PROGRESS && cancel_in_progress_op) { | |
73c04a47 DH |
396 | ASSERTCMP(object->n_in_progress, >, 0); |
397 | if (test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags)) | |
398 | object->n_exclusive--; | |
399 | object->n_in_progress--; | |
400 | if (object->n_in_progress == 0) | |
401 | fscache_start_operations(object); | |
402 | ||
418b7eb9 | 403 | fscache_stat(&fscache_n_op_cancelled); |
d3b97ca4 | 404 | op->cancel(op); |
9f10523f | 405 | op->state = FSCACHE_OP_ST_CANCELLED; |
5753c441 DH |
406 | if (test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags)) |
407 | object->n_exclusive--; | |
408 | if (test_and_clear_bit(FSCACHE_OP_WAITING, &op->flags)) | |
409 | wake_up_bit(&op->flags, FSCACHE_OP_WAITING); | |
5753c441 DH |
410 | ret = 0; |
411 | } | |
412 | ||
418b7eb9 DH |
413 | if (put) |
414 | fscache_put_operation(op); | |
5753c441 DH |
415 | spin_unlock(&object->lock); |
416 | _leave(" = %d", ret); | |
417 | return ret; | |
418 | } | |
419 | ||
ef778e7a DH |
420 | /* |
421 | * Cancel all pending operations on an object | |
422 | */ | |
423 | void fscache_cancel_all_ops(struct fscache_object *object) | |
424 | { | |
425 | struct fscache_operation *op; | |
426 | ||
427 | _enter("OBJ%x", object->debug_id); | |
428 | ||
429 | spin_lock(&object->lock); | |
430 | ||
431 | while (!list_empty(&object->pending_ops)) { | |
432 | op = list_entry(object->pending_ops.next, | |
433 | struct fscache_operation, pend_link); | |
434 | fscache_stat(&fscache_n_op_cancelled); | |
435 | list_del_init(&op->pend_link); | |
436 | ||
08c2e3d0 DH |
437 | trace_fscache_op(object->cookie, op, fscache_op_cancel_all); |
438 | ||
ef778e7a | 439 | ASSERTCMP(op->state, ==, FSCACHE_OP_ST_PENDING); |
d3b97ca4 | 440 | op->cancel(op); |
ef778e7a DH |
441 | op->state = FSCACHE_OP_ST_CANCELLED; |
442 | ||
443 | if (test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags)) | |
444 | object->n_exclusive--; | |
445 | if (test_and_clear_bit(FSCACHE_OP_WAITING, &op->flags)) | |
446 | wake_up_bit(&op->flags, FSCACHE_OP_WAITING); | |
447 | fscache_put_operation(op); | |
448 | cond_resched_lock(&object->lock); | |
449 | } | |
450 | ||
451 | spin_unlock(&object->lock); | |
452 | _leave(""); | |
453 | } | |
454 | ||
9f10523f | 455 | /* |
1f372dff | 456 | * Record the completion or cancellation of an in-progress operation. |
9f10523f | 457 | */ |
1f372dff | 458 | void fscache_op_complete(struct fscache_operation *op, bool cancelled) |
9f10523f DH |
459 | { |
460 | struct fscache_object *object = op->object; | |
461 | ||
462 | _enter("OBJ%x", object->debug_id); | |
463 | ||
464 | ASSERTCMP(op->state, ==, FSCACHE_OP_ST_IN_PROGRESS); | |
465 | ASSERTCMP(object->n_in_progress, >, 0); | |
466 | ASSERTIFCMP(test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags), | |
467 | object->n_exclusive, >, 0); | |
468 | ASSERTIFCMP(test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags), | |
469 | object->n_in_progress, ==, 1); | |
470 | ||
471 | spin_lock(&object->lock); | |
472 | ||
d3b97ca4 | 473 | if (!cancelled) { |
08c2e3d0 | 474 | trace_fscache_op(object->cookie, op, fscache_op_completed); |
d3b97ca4 DH |
475 | op->state = FSCACHE_OP_ST_COMPLETE; |
476 | } else { | |
477 | op->cancel(op); | |
08c2e3d0 | 478 | trace_fscache_op(object->cookie, op, fscache_op_cancelled); |
d3b97ca4 DH |
479 | op->state = FSCACHE_OP_ST_CANCELLED; |
480 | } | |
9f10523f DH |
481 | |
482 | if (test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags)) | |
483 | object->n_exclusive--; | |
484 | object->n_in_progress--; | |
485 | if (object->n_in_progress == 0) | |
486 | fscache_start_operations(object); | |
487 | ||
488 | spin_unlock(&object->lock); | |
489 | _leave(""); | |
490 | } | |
491 | EXPORT_SYMBOL(fscache_op_complete); | |
492 | ||
952efe7b DH |
493 | /* |
494 | * release an operation | |
495 | * - queues pending ops if this is the last in-progress op | |
496 | */ | |
497 | void fscache_put_operation(struct fscache_operation *op) | |
498 | { | |
499 | struct fscache_object *object; | |
500 | struct fscache_cache *cache; | |
501 | ||
502 | _enter("{OBJ%x OP%x,%d}", | |
d0eb06af KKM |
503 | op->object ? op->object->debug_id : 0, |
504 | op->debug_id, atomic_read(&op->usage)); | |
952efe7b DH |
505 | |
506 | ASSERTCMP(atomic_read(&op->usage), >, 0); | |
507 | ||
508 | if (!atomic_dec_and_test(&op->usage)) | |
509 | return; | |
510 | ||
402cb8dd | 511 | trace_fscache_op(op->object ? op->object->cookie : NULL, op, fscache_op_put); |
08c2e3d0 | 512 | |
952efe7b | 513 | _debug("PUT OP"); |
a39caadf DH |
514 | ASSERTIFCMP(op->state != FSCACHE_OP_ST_INITIALISED && |
515 | op->state != FSCACHE_OP_ST_COMPLETE, | |
9f10523f | 516 | op->state, ==, FSCACHE_OP_ST_CANCELLED); |
952efe7b DH |
517 | |
518 | fscache_stat(&fscache_n_op_release); | |
519 | ||
520 | if (op->release) { | |
521 | op->release(op); | |
522 | op->release = NULL; | |
523 | } | |
4a47132f | 524 | op->state = FSCACHE_OP_ST_DEAD; |
952efe7b DH |
525 | |
526 | object = op->object; | |
a39caadf DH |
527 | if (likely(object)) { |
528 | if (test_bit(FSCACHE_OP_DEC_READ_CNT, &op->flags)) | |
529 | atomic_dec(&object->n_reads); | |
530 | if (test_bit(FSCACHE_OP_UNUSE_COOKIE, &op->flags)) | |
531 | fscache_unuse_cookie(object); | |
532 | ||
533 | /* now... we may get called with the object spinlock held, so we | |
534 | * complete the cleanup here only if we can immediately acquire the | |
535 | * lock, and defer it otherwise */ | |
536 | if (!spin_trylock(&object->lock)) { | |
537 | _debug("defer put"); | |
538 | fscache_stat(&fscache_n_op_deferred_release); | |
539 | ||
540 | cache = object->cache; | |
541 | spin_lock(&cache->op_gc_list_lock); | |
542 | list_add_tail(&op->pend_link, &cache->op_gc_list); | |
543 | spin_unlock(&cache->op_gc_list_lock); | |
544 | schedule_work(&cache->op_gc); | |
545 | _leave(" [defer]"); | |
546 | return; | |
547 | } | |
952efe7b | 548 | |
a39caadf DH |
549 | ASSERTCMP(object->n_ops, >, 0); |
550 | object->n_ops--; | |
551 | if (object->n_ops == 0) | |
552 | fscache_raise_event(object, FSCACHE_OBJECT_EV_CLEARED); | |
952efe7b | 553 | |
a39caadf | 554 | spin_unlock(&object->lock); |
952efe7b DH |
555 | } |
556 | ||
952efe7b DH |
557 | kfree(op); |
558 | _leave(" [done]"); | |
559 | } | |
560 | EXPORT_SYMBOL(fscache_put_operation); | |
561 | ||
562 | /* | |
563 | * garbage collect operations that have had their release deferred | |
564 | */ | |
565 | void fscache_operation_gc(struct work_struct *work) | |
566 | { | |
567 | struct fscache_operation *op; | |
568 | struct fscache_object *object; | |
569 | struct fscache_cache *cache = | |
570 | container_of(work, struct fscache_cache, op_gc); | |
571 | int count = 0; | |
572 | ||
573 | _enter(""); | |
574 | ||
575 | do { | |
576 | spin_lock(&cache->op_gc_list_lock); | |
577 | if (list_empty(&cache->op_gc_list)) { | |
578 | spin_unlock(&cache->op_gc_list_lock); | |
579 | break; | |
580 | } | |
581 | ||
582 | op = list_entry(cache->op_gc_list.next, | |
583 | struct fscache_operation, pend_link); | |
584 | list_del(&op->pend_link); | |
585 | spin_unlock(&cache->op_gc_list_lock); | |
586 | ||
587 | object = op->object; | |
08c2e3d0 DH |
588 | trace_fscache_op(object->cookie, op, fscache_op_gc); |
589 | ||
9f10523f | 590 | spin_lock(&object->lock); |
952efe7b DH |
591 | |
592 | _debug("GC DEFERRED REL OBJ%x OP%x", | |
593 | object->debug_id, op->debug_id); | |
594 | fscache_stat(&fscache_n_op_gc); | |
595 | ||
596 | ASSERTCMP(atomic_read(&op->usage), ==, 0); | |
9f10523f | 597 | ASSERTCMP(op->state, ==, FSCACHE_OP_ST_DEAD); |
952efe7b DH |
598 | |
599 | ASSERTCMP(object->n_ops, >, 0); | |
600 | object->n_ops--; | |
601 | if (object->n_ops == 0) | |
602 | fscache_raise_event(object, FSCACHE_OBJECT_EV_CLEARED); | |
603 | ||
604 | spin_unlock(&object->lock); | |
9f10523f | 605 | kfree(op); |
952efe7b DH |
606 | |
607 | } while (count++ < 20); | |
608 | ||
609 | if (!list_empty(&cache->op_gc_list)) | |
610 | schedule_work(&cache->op_gc); | |
611 | ||
612 | _leave(""); | |
613 | } | |
614 | ||
615 | /* | |
8af7c124 TH |
616 | * execute an operation using fs_op_wq to provide processing context - |
617 | * the caller holds a ref to this object, so we don't need to hold one | |
952efe7b | 618 | */ |
8af7c124 | 619 | void fscache_op_work_func(struct work_struct *work) |
952efe7b DH |
620 | { |
621 | struct fscache_operation *op = | |
8af7c124 | 622 | container_of(work, struct fscache_operation, work); |
952efe7b DH |
623 | unsigned long start; |
624 | ||
625 | _enter("{OBJ%x OP%x,%d}", | |
626 | op->object->debug_id, op->debug_id, atomic_read(&op->usage)); | |
627 | ||
08c2e3d0 DH |
628 | trace_fscache_op(op->object->cookie, op, fscache_op_work); |
629 | ||
952efe7b DH |
630 | ASSERT(op->processor != NULL); |
631 | start = jiffies; | |
632 | op->processor(op); | |
633 | fscache_hist(fscache_ops_histogram, start); | |
8af7c124 | 634 | fscache_put_operation(op); |
952efe7b DH |
635 | |
636 | _leave(""); | |
637 | } |