blob: 1a1afa82f7981a73decfb5c72947e31f4943bb7c [file] [log] [blame]
David Howells36c95592009-04-03 16:42:38 +01001/* FS-Cache object state machine handler
2 *
3 * Copyright (C) 2007 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/object.txt for a description of the
12 * object state machine and the in-kernel representations.
13 */
14
15#define FSCACHE_DEBUG_LEVEL COOKIE
16#include <linux/module.h>
David Howells440f0af2009-11-19 18:11:01 +000017#include <linux/seq_file.h>
David Howells36c95592009-04-03 16:42:38 +010018#include "internal.h"
19
David Howells440f0af2009-11-19 18:11:01 +000020const char *fscache_object_states[FSCACHE_OBJECT__NSTATES] = {
David Howells36c95592009-04-03 16:42:38 +010021 [FSCACHE_OBJECT_INIT] = "OBJECT_INIT",
22 [FSCACHE_OBJECT_LOOKING_UP] = "OBJECT_LOOKING_UP",
23 [FSCACHE_OBJECT_CREATING] = "OBJECT_CREATING",
24 [FSCACHE_OBJECT_AVAILABLE] = "OBJECT_AVAILABLE",
25 [FSCACHE_OBJECT_ACTIVE] = "OBJECT_ACTIVE",
26 [FSCACHE_OBJECT_UPDATING] = "OBJECT_UPDATING",
27 [FSCACHE_OBJECT_DYING] = "OBJECT_DYING",
28 [FSCACHE_OBJECT_LC_DYING] = "OBJECT_LC_DYING",
29 [FSCACHE_OBJECT_ABORT_INIT] = "OBJECT_ABORT_INIT",
30 [FSCACHE_OBJECT_RELEASING] = "OBJECT_RELEASING",
31 [FSCACHE_OBJECT_RECYCLING] = "OBJECT_RECYCLING",
32 [FSCACHE_OBJECT_WITHDRAWING] = "OBJECT_WITHDRAWING",
33 [FSCACHE_OBJECT_DEAD] = "OBJECT_DEAD",
34};
35EXPORT_SYMBOL(fscache_object_states);
36
David Howells4fbf4292009-11-19 18:11:04 +000037const char fscache_object_states_short[FSCACHE_OBJECT__NSTATES][5] = {
David Howells440f0af2009-11-19 18:11:01 +000038 [FSCACHE_OBJECT_INIT] = "INIT",
39 [FSCACHE_OBJECT_LOOKING_UP] = "LOOK",
40 [FSCACHE_OBJECT_CREATING] = "CRTN",
41 [FSCACHE_OBJECT_AVAILABLE] = "AVBL",
42 [FSCACHE_OBJECT_ACTIVE] = "ACTV",
43 [FSCACHE_OBJECT_UPDATING] = "UPDT",
44 [FSCACHE_OBJECT_DYING] = "DYNG",
45 [FSCACHE_OBJECT_LC_DYING] = "LCDY",
46 [FSCACHE_OBJECT_ABORT_INIT] = "ABTI",
47 [FSCACHE_OBJECT_RELEASING] = "RELS",
48 [FSCACHE_OBJECT_RECYCLING] = "RCYC",
49 [FSCACHE_OBJECT_WITHDRAWING] = "WTHD",
50 [FSCACHE_OBJECT_DEAD] = "DEAD",
51};
52
David Howells36c95592009-04-03 16:42:38 +010053static void fscache_object_slow_work_put_ref(struct slow_work *);
54static int fscache_object_slow_work_get_ref(struct slow_work *);
55static void fscache_object_slow_work_execute(struct slow_work *);
David Howells440f0af2009-11-19 18:11:01 +000056#ifdef CONFIG_SLOW_WORK_PROC
57static void fscache_object_slow_work_desc(struct slow_work *, struct seq_file *);
58#endif
David Howells36c95592009-04-03 16:42:38 +010059static void fscache_initialise_object(struct fscache_object *);
60static void fscache_lookup_object(struct fscache_object *);
61static void fscache_object_available(struct fscache_object *);
62static void fscache_release_object(struct fscache_object *);
63static void fscache_withdraw_object(struct fscache_object *);
64static void fscache_enqueue_dependents(struct fscache_object *);
65static void fscache_dequeue_object(struct fscache_object *);
66
67const struct slow_work_ops fscache_object_slow_work_ops = {
David Howells3d7a6412009-11-19 18:10:23 +000068 .owner = THIS_MODULE,
David Howells36c95592009-04-03 16:42:38 +010069 .get_ref = fscache_object_slow_work_get_ref,
70 .put_ref = fscache_object_slow_work_put_ref,
71 .execute = fscache_object_slow_work_execute,
David Howells440f0af2009-11-19 18:11:01 +000072#ifdef CONFIG_SLOW_WORK_PROC
73 .desc = fscache_object_slow_work_desc,
74#endif
David Howells36c95592009-04-03 16:42:38 +010075};
76EXPORT_SYMBOL(fscache_object_slow_work_ops);
77
78/*
79 * we need to notify the parent when an op completes that we had outstanding
80 * upon it
81 */
82static inline void fscache_done_parent_op(struct fscache_object *object)
83{
84 struct fscache_object *parent = object->parent;
85
86 _enter("OBJ%x {OBJ%x,%x}",
87 object->debug_id, parent->debug_id, parent->n_ops);
88
89 spin_lock_nested(&parent->lock, 1);
90 parent->n_ops--;
91 parent->n_obj_ops--;
92 if (parent->n_ops == 0)
93 fscache_raise_event(parent, FSCACHE_OBJECT_EV_CLEARED);
94 spin_unlock(&parent->lock);
95}
96
97/*
98 * process events that have been sent to an object's state machine
99 * - initiates parent lookup
100 * - does object lookup
101 * - does object creation
102 * - does object recycling and retirement
103 * - does object withdrawal
104 */
105static void fscache_object_state_machine(struct fscache_object *object)
106{
107 enum fscache_object_state new_state;
108
109 ASSERT(object != NULL);
110
111 _enter("{OBJ%x,%s,%lx}",
112 object->debug_id, fscache_object_states[object->state],
113 object->events);
114
115 switch (object->state) {
116 /* wait for the parent object to become ready */
117 case FSCACHE_OBJECT_INIT:
118 object->event_mask =
119 ULONG_MAX & ~(1 << FSCACHE_OBJECT_EV_CLEARED);
120 fscache_initialise_object(object);
121 goto done;
122
123 /* look up the object metadata on disk */
124 case FSCACHE_OBJECT_LOOKING_UP:
125 fscache_lookup_object(object);
126 goto lookup_transit;
127
128 /* create the object metadata on disk */
129 case FSCACHE_OBJECT_CREATING:
130 fscache_lookup_object(object);
131 goto lookup_transit;
132
133 /* handle an object becoming available; start pending
134 * operations and queue dependent operations for processing */
135 case FSCACHE_OBJECT_AVAILABLE:
136 fscache_object_available(object);
137 goto active_transit;
138
139 /* normal running state */
140 case FSCACHE_OBJECT_ACTIVE:
141 goto active_transit;
142
143 /* update the object metadata on disk */
144 case FSCACHE_OBJECT_UPDATING:
145 clear_bit(FSCACHE_OBJECT_EV_UPDATE, &object->events);
146 fscache_stat(&fscache_n_updates_run);
David Howells52bd75f2009-11-19 18:11:08 +0000147 fscache_stat(&fscache_n_cop_update_object);
David Howells36c95592009-04-03 16:42:38 +0100148 object->cache->ops->update_object(object);
David Howells52bd75f2009-11-19 18:11:08 +0000149 fscache_stat_d(&fscache_n_cop_update_object);
David Howells36c95592009-04-03 16:42:38 +0100150 goto active_transit;
151
152 /* handle an object dying during lookup or creation */
153 case FSCACHE_OBJECT_LC_DYING:
154 object->event_mask &= ~(1 << FSCACHE_OBJECT_EV_UPDATE);
David Howells52bd75f2009-11-19 18:11:08 +0000155 fscache_stat(&fscache_n_cop_lookup_complete);
David Howells36c95592009-04-03 16:42:38 +0100156 object->cache->ops->lookup_complete(object);
David Howells52bd75f2009-11-19 18:11:08 +0000157 fscache_stat_d(&fscache_n_cop_lookup_complete);
David Howells36c95592009-04-03 16:42:38 +0100158
159 spin_lock(&object->lock);
160 object->state = FSCACHE_OBJECT_DYING;
David Howells6897e3d2009-11-19 18:11:22 +0000161 if (object->cookie &&
162 test_and_clear_bit(FSCACHE_COOKIE_CREATING,
David Howells36c95592009-04-03 16:42:38 +0100163 &object->cookie->flags))
164 wake_up_bit(&object->cookie->flags,
165 FSCACHE_COOKIE_CREATING);
166 spin_unlock(&object->lock);
167
168 fscache_done_parent_op(object);
169
170 /* wait for completion of all active operations on this object
171 * and the death of all child objects of this object */
172 case FSCACHE_OBJECT_DYING:
173 dying:
174 clear_bit(FSCACHE_OBJECT_EV_CLEARED, &object->events);
175 spin_lock(&object->lock);
176 _debug("dying OBJ%x {%d,%d}",
177 object->debug_id, object->n_ops, object->n_children);
178 if (object->n_ops == 0 && object->n_children == 0) {
179 object->event_mask &=
180 ~(1 << FSCACHE_OBJECT_EV_CLEARED);
181 object->event_mask |=
182 (1 << FSCACHE_OBJECT_EV_WITHDRAW) |
183 (1 << FSCACHE_OBJECT_EV_RETIRE) |
184 (1 << FSCACHE_OBJECT_EV_RELEASE) |
185 (1 << FSCACHE_OBJECT_EV_ERROR);
186 } else {
187 object->event_mask &=
188 ~((1 << FSCACHE_OBJECT_EV_WITHDRAW) |
189 (1 << FSCACHE_OBJECT_EV_RETIRE) |
190 (1 << FSCACHE_OBJECT_EV_RELEASE) |
191 (1 << FSCACHE_OBJECT_EV_ERROR));
192 object->event_mask |=
193 1 << FSCACHE_OBJECT_EV_CLEARED;
194 }
195 spin_unlock(&object->lock);
196 fscache_enqueue_dependents(object);
197 goto terminal_transit;
198
199 /* handle an abort during initialisation */
200 case FSCACHE_OBJECT_ABORT_INIT:
201 _debug("handle abort init %lx", object->events);
202 object->event_mask &= ~(1 << FSCACHE_OBJECT_EV_UPDATE);
203
204 spin_lock(&object->lock);
205 fscache_dequeue_object(object);
206
207 object->state = FSCACHE_OBJECT_DYING;
208 if (test_and_clear_bit(FSCACHE_COOKIE_CREATING,
209 &object->cookie->flags))
210 wake_up_bit(&object->cookie->flags,
211 FSCACHE_COOKIE_CREATING);
212 spin_unlock(&object->lock);
213 goto dying;
214
215 /* handle the netfs releasing an object and possibly marking it
216 * obsolete too */
217 case FSCACHE_OBJECT_RELEASING:
218 case FSCACHE_OBJECT_RECYCLING:
219 object->event_mask &=
220 ~((1 << FSCACHE_OBJECT_EV_WITHDRAW) |
221 (1 << FSCACHE_OBJECT_EV_RETIRE) |
222 (1 << FSCACHE_OBJECT_EV_RELEASE) |
223 (1 << FSCACHE_OBJECT_EV_ERROR));
224 fscache_release_object(object);
225 spin_lock(&object->lock);
226 object->state = FSCACHE_OBJECT_DEAD;
227 spin_unlock(&object->lock);
228 fscache_stat(&fscache_n_object_dead);
229 goto terminal_transit;
230
231 /* handle the parent cache of this object being withdrawn from
232 * active service */
233 case FSCACHE_OBJECT_WITHDRAWING:
234 object->event_mask &=
235 ~((1 << FSCACHE_OBJECT_EV_WITHDRAW) |
236 (1 << FSCACHE_OBJECT_EV_RETIRE) |
237 (1 << FSCACHE_OBJECT_EV_RELEASE) |
238 (1 << FSCACHE_OBJECT_EV_ERROR));
239 fscache_withdraw_object(object);
240 spin_lock(&object->lock);
241 object->state = FSCACHE_OBJECT_DEAD;
242 spin_unlock(&object->lock);
243 fscache_stat(&fscache_n_object_dead);
244 goto terminal_transit;
245
246 /* complain about the object being woken up once it is
247 * deceased */
248 case FSCACHE_OBJECT_DEAD:
249 printk(KERN_ERR "FS-Cache:"
250 " Unexpected event in dead state %lx\n",
251 object->events & object->event_mask);
252 BUG();
253
254 default:
255 printk(KERN_ERR "FS-Cache: Unknown object state %u\n",
256 object->state);
257 BUG();
258 }
259
260 /* determine the transition from a lookup state */
261lookup_transit:
262 switch (fls(object->events & object->event_mask) - 1) {
263 case FSCACHE_OBJECT_EV_WITHDRAW:
264 case FSCACHE_OBJECT_EV_RETIRE:
265 case FSCACHE_OBJECT_EV_RELEASE:
266 case FSCACHE_OBJECT_EV_ERROR:
267 new_state = FSCACHE_OBJECT_LC_DYING;
268 goto change_state;
269 case FSCACHE_OBJECT_EV_REQUEUE:
270 goto done;
271 case -1:
272 goto done; /* sleep until event */
273 default:
274 goto unsupported_event;
275 }
276
277 /* determine the transition from an active state */
278active_transit:
279 switch (fls(object->events & object->event_mask) - 1) {
280 case FSCACHE_OBJECT_EV_WITHDRAW:
281 case FSCACHE_OBJECT_EV_RETIRE:
282 case FSCACHE_OBJECT_EV_RELEASE:
283 case FSCACHE_OBJECT_EV_ERROR:
284 new_state = FSCACHE_OBJECT_DYING;
285 goto change_state;
286 case FSCACHE_OBJECT_EV_UPDATE:
287 new_state = FSCACHE_OBJECT_UPDATING;
288 goto change_state;
289 case -1:
290 new_state = FSCACHE_OBJECT_ACTIVE;
291 goto change_state; /* sleep until event */
292 default:
293 goto unsupported_event;
294 }
295
296 /* determine the transition from a terminal state */
297terminal_transit:
298 switch (fls(object->events & object->event_mask) - 1) {
299 case FSCACHE_OBJECT_EV_WITHDRAW:
300 new_state = FSCACHE_OBJECT_WITHDRAWING;
301 goto change_state;
302 case FSCACHE_OBJECT_EV_RETIRE:
303 new_state = FSCACHE_OBJECT_RECYCLING;
304 goto change_state;
305 case FSCACHE_OBJECT_EV_RELEASE:
306 new_state = FSCACHE_OBJECT_RELEASING;
307 goto change_state;
308 case FSCACHE_OBJECT_EV_ERROR:
309 new_state = FSCACHE_OBJECT_WITHDRAWING;
310 goto change_state;
311 case FSCACHE_OBJECT_EV_CLEARED:
312 new_state = FSCACHE_OBJECT_DYING;
313 goto change_state;
314 case -1:
315 goto done; /* sleep until event */
316 default:
317 goto unsupported_event;
318 }
319
320change_state:
321 spin_lock(&object->lock);
322 object->state = new_state;
323 spin_unlock(&object->lock);
324
325done:
326 _leave(" [->%s]", fscache_object_states[object->state]);
327 return;
328
329unsupported_event:
330 printk(KERN_ERR "FS-Cache:"
331 " Unsupported event %lx [mask %lx] in state %s\n",
332 object->events, object->event_mask,
333 fscache_object_states[object->state]);
334 BUG();
335}
336
337/*
338 * execute an object
339 */
340static void fscache_object_slow_work_execute(struct slow_work *work)
341{
342 struct fscache_object *object =
343 container_of(work, struct fscache_object, work);
344 unsigned long start;
345
346 _enter("{OBJ%x}", object->debug_id);
347
348 clear_bit(FSCACHE_OBJECT_EV_REQUEUE, &object->events);
349
350 start = jiffies;
351 fscache_object_state_machine(object);
352 fscache_hist(fscache_objs_histogram, start);
353 if (object->events & object->event_mask)
354 fscache_enqueue_object(object);
355}
356
357/*
David Howells440f0af2009-11-19 18:11:01 +0000358 * describe an object for slow-work debugging
359 */
360#ifdef CONFIG_SLOW_WORK_PROC
361static void fscache_object_slow_work_desc(struct slow_work *work,
362 struct seq_file *m)
363{
364 struct fscache_object *object =
365 container_of(work, struct fscache_object, work);
366
367 seq_printf(m, "FSC: OBJ%x: %s",
368 object->debug_id,
369 fscache_object_states_short[object->state]);
370}
371#endif
372
373/*
David Howells36c95592009-04-03 16:42:38 +0100374 * initialise an object
375 * - check the specified object's parent to see if we can make use of it
376 * immediately to do a creation
377 * - we may need to start the process of creating a parent and we need to wait
378 * for the parent's lookup and creation to complete if it's not there yet
379 * - an object's cookie is pinned until we clear FSCACHE_COOKIE_CREATING on the
380 * leaf-most cookies of the object and all its children
381 */
382static void fscache_initialise_object(struct fscache_object *object)
383{
384 struct fscache_object *parent;
385
386 _enter("");
387 ASSERT(object->cookie != NULL);
388 ASSERT(object->cookie->parent != NULL);
389 ASSERT(list_empty(&object->work.link));
390
391 if (object->events & ((1 << FSCACHE_OBJECT_EV_ERROR) |
392 (1 << FSCACHE_OBJECT_EV_RELEASE) |
393 (1 << FSCACHE_OBJECT_EV_RETIRE) |
394 (1 << FSCACHE_OBJECT_EV_WITHDRAW))) {
395 _debug("abort init %lx", object->events);
396 spin_lock(&object->lock);
397 object->state = FSCACHE_OBJECT_ABORT_INIT;
398 spin_unlock(&object->lock);
399 return;
400 }
401
402 spin_lock(&object->cookie->lock);
403 spin_lock_nested(&object->cookie->parent->lock, 1);
404
405 parent = object->parent;
406 if (!parent) {
407 _debug("no parent");
408 set_bit(FSCACHE_OBJECT_EV_WITHDRAW, &object->events);
409 } else {
410 spin_lock(&object->lock);
411 spin_lock_nested(&parent->lock, 1);
412 _debug("parent %s", fscache_object_states[parent->state]);
413
414 if (parent->state >= FSCACHE_OBJECT_DYING) {
415 _debug("bad parent");
416 set_bit(FSCACHE_OBJECT_EV_WITHDRAW, &object->events);
417 } else if (parent->state < FSCACHE_OBJECT_AVAILABLE) {
418 _debug("wait");
419
420 /* we may get woken up in this state by child objects
421 * binding on to us, so we need to make sure we don't
422 * add ourself to the list multiple times */
423 if (list_empty(&object->dep_link)) {
David Howells52bd75f2009-11-19 18:11:08 +0000424 fscache_stat(&fscache_n_cop_grab_object);
David Howells36c95592009-04-03 16:42:38 +0100425 object->cache->ops->grab_object(object);
David Howells52bd75f2009-11-19 18:11:08 +0000426 fscache_stat_d(&fscache_n_cop_grab_object);
David Howells36c95592009-04-03 16:42:38 +0100427 list_add(&object->dep_link,
428 &parent->dependents);
429
430 /* fscache_acquire_non_index_cookie() uses this
431 * to wake the chain up */
432 if (parent->state == FSCACHE_OBJECT_INIT)
433 fscache_enqueue_object(parent);
434 }
435 } else {
436 _debug("go");
437 parent->n_ops++;
438 parent->n_obj_ops++;
439 object->lookup_jif = jiffies;
440 object->state = FSCACHE_OBJECT_LOOKING_UP;
441 set_bit(FSCACHE_OBJECT_EV_REQUEUE, &object->events);
442 }
443
444 spin_unlock(&parent->lock);
445 spin_unlock(&object->lock);
446 }
447
448 spin_unlock(&object->cookie->parent->lock);
449 spin_unlock(&object->cookie->lock);
450 _leave("");
451}
452
453/*
454 * look an object up in the cache from which it was allocated
455 * - we hold an "access lock" on the parent object, so the parent object cannot
456 * be withdrawn by either party till we've finished
457 * - an object's cookie is pinned until we clear FSCACHE_COOKIE_CREATING on the
458 * leaf-most cookies of the object and all its children
459 */
460static void fscache_lookup_object(struct fscache_object *object)
461{
462 struct fscache_cookie *cookie = object->cookie;
463 struct fscache_object *parent;
464
465 _enter("");
466
467 parent = object->parent;
468 ASSERT(parent != NULL);
469 ASSERTCMP(parent->n_ops, >, 0);
470 ASSERTCMP(parent->n_obj_ops, >, 0);
471
472 /* make sure the parent is still available */
473 ASSERTCMP(parent->state, >=, FSCACHE_OBJECT_AVAILABLE);
474
475 if (parent->state >= FSCACHE_OBJECT_DYING ||
476 test_bit(FSCACHE_IOERROR, &object->cache->flags)) {
477 _debug("unavailable");
478 set_bit(FSCACHE_OBJECT_EV_WITHDRAW, &object->events);
479 _leave("");
480 return;
481 }
482
483 _debug("LOOKUP \"%s/%s\" in \"%s\"",
484 parent->cookie->def->name, cookie->def->name,
485 object->cache->tag->name);
486
487 fscache_stat(&fscache_n_object_lookups);
David Howells52bd75f2009-11-19 18:11:08 +0000488 fscache_stat(&fscache_n_cop_lookup_object);
David Howells36c95592009-04-03 16:42:38 +0100489 object->cache->ops->lookup_object(object);
David Howells52bd75f2009-11-19 18:11:08 +0000490 fscache_stat_d(&fscache_n_cop_lookup_object);
David Howells36c95592009-04-03 16:42:38 +0100491
492 if (test_bit(FSCACHE_OBJECT_EV_ERROR, &object->events))
493 set_bit(FSCACHE_COOKIE_UNAVAILABLE, &cookie->flags);
494
495 _leave("");
496}
497
498/**
499 * fscache_object_lookup_negative - Note negative cookie lookup
500 * @object: Object pointing to cookie to mark
501 *
502 * Note negative lookup, permitting those waiting to read data from an already
503 * existing backing object to continue as there's no data for them to read.
504 */
505void fscache_object_lookup_negative(struct fscache_object *object)
506{
507 struct fscache_cookie *cookie = object->cookie;
508
509 _enter("{OBJ%x,%s}",
510 object->debug_id, fscache_object_states[object->state]);
511
512 spin_lock(&object->lock);
513 if (object->state == FSCACHE_OBJECT_LOOKING_UP) {
514 fscache_stat(&fscache_n_object_lookups_negative);
515
516 /* transit here to allow write requests to begin stacking up
517 * and read requests to begin returning ENODATA */
518 object->state = FSCACHE_OBJECT_CREATING;
519 spin_unlock(&object->lock);
520
521 set_bit(FSCACHE_COOKIE_PENDING_FILL, &cookie->flags);
522 set_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
523
524 _debug("wake up lookup %p", &cookie->flags);
525 smp_mb__before_clear_bit();
526 clear_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags);
527 smp_mb__after_clear_bit();
528 wake_up_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP);
529 set_bit(FSCACHE_OBJECT_EV_REQUEUE, &object->events);
530 } else {
531 ASSERTCMP(object->state, ==, FSCACHE_OBJECT_CREATING);
532 spin_unlock(&object->lock);
533 }
534
535 _leave("");
536}
537EXPORT_SYMBOL(fscache_object_lookup_negative);
538
539/**
540 * fscache_obtained_object - Note successful object lookup or creation
541 * @object: Object pointing to cookie to mark
542 *
543 * Note successful lookup and/or creation, permitting those waiting to write
544 * data to a backing object to continue.
545 *
546 * Note that after calling this, an object's cookie may be relinquished by the
547 * netfs, and so must be accessed with object lock held.
548 */
549void fscache_obtained_object(struct fscache_object *object)
550{
551 struct fscache_cookie *cookie = object->cookie;
552
553 _enter("{OBJ%x,%s}",
554 object->debug_id, fscache_object_states[object->state]);
555
556 /* if we were still looking up, then we must have a positive lookup
557 * result, in which case there may be data available */
558 spin_lock(&object->lock);
559 if (object->state == FSCACHE_OBJECT_LOOKING_UP) {
560 fscache_stat(&fscache_n_object_lookups_positive);
561
562 clear_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
563
564 object->state = FSCACHE_OBJECT_AVAILABLE;
565 spin_unlock(&object->lock);
566
567 smp_mb__before_clear_bit();
568 clear_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags);
569 smp_mb__after_clear_bit();
570 wake_up_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP);
571 set_bit(FSCACHE_OBJECT_EV_REQUEUE, &object->events);
572 } else {
573 ASSERTCMP(object->state, ==, FSCACHE_OBJECT_CREATING);
574 fscache_stat(&fscache_n_object_created);
575
576 object->state = FSCACHE_OBJECT_AVAILABLE;
577 spin_unlock(&object->lock);
578 set_bit(FSCACHE_OBJECT_EV_REQUEUE, &object->events);
579 smp_wmb();
580 }
581
582 if (test_and_clear_bit(FSCACHE_COOKIE_CREATING, &cookie->flags))
583 wake_up_bit(&cookie->flags, FSCACHE_COOKIE_CREATING);
584
585 _leave("");
586}
587EXPORT_SYMBOL(fscache_obtained_object);
588
589/*
590 * handle an object that has just become available
591 */
592static void fscache_object_available(struct fscache_object *object)
593{
594 _enter("{OBJ%x}", object->debug_id);
595
596 spin_lock(&object->lock);
597
David Howells6897e3d2009-11-19 18:11:22 +0000598 if (object->cookie &&
599 test_and_clear_bit(FSCACHE_COOKIE_CREATING, &object->cookie->flags))
David Howells36c95592009-04-03 16:42:38 +0100600 wake_up_bit(&object->cookie->flags, FSCACHE_COOKIE_CREATING);
601
602 fscache_done_parent_op(object);
603 if (object->n_in_progress == 0) {
604 if (object->n_ops > 0) {
605 ASSERTCMP(object->n_ops, >=, object->n_obj_ops);
606 ASSERTIF(object->n_ops > object->n_obj_ops,
607 !list_empty(&object->pending_ops));
608 fscache_start_operations(object);
609 } else {
610 ASSERT(list_empty(&object->pending_ops));
611 }
612 }
613 spin_unlock(&object->lock);
614
David Howells52bd75f2009-11-19 18:11:08 +0000615 fscache_stat(&fscache_n_cop_lookup_complete);
David Howells36c95592009-04-03 16:42:38 +0100616 object->cache->ops->lookup_complete(object);
David Howells52bd75f2009-11-19 18:11:08 +0000617 fscache_stat_d(&fscache_n_cop_lookup_complete);
David Howells36c95592009-04-03 16:42:38 +0100618 fscache_enqueue_dependents(object);
619
620 fscache_hist(fscache_obj_instantiate_histogram, object->lookup_jif);
621 fscache_stat(&fscache_n_object_avail);
622
623 _leave("");
624}
625
626/*
627 * drop an object's attachments
628 */
629static void fscache_drop_object(struct fscache_object *object)
630{
631 struct fscache_object *parent = object->parent;
632 struct fscache_cache *cache = object->cache;
633
634 _enter("{OBJ%x,%d}", object->debug_id, object->n_children);
635
David Howells6897e3d2009-11-19 18:11:22 +0000636 ASSERTCMP(object->cookie, ==, NULL);
637 ASSERT(hlist_unhashed(&object->cookie_link));
638
David Howells36c95592009-04-03 16:42:38 +0100639 spin_lock(&cache->object_list_lock);
640 list_del_init(&object->cache_link);
641 spin_unlock(&cache->object_list_lock);
642
David Howells52bd75f2009-11-19 18:11:08 +0000643 fscache_stat(&fscache_n_cop_drop_object);
David Howells36c95592009-04-03 16:42:38 +0100644 cache->ops->drop_object(object);
David Howells52bd75f2009-11-19 18:11:08 +0000645 fscache_stat_d(&fscache_n_cop_drop_object);
David Howells36c95592009-04-03 16:42:38 +0100646
647 if (parent) {
648 _debug("release parent OBJ%x {%d}",
649 parent->debug_id, parent->n_children);
650
651 spin_lock(&parent->lock);
652 parent->n_children--;
653 if (parent->n_children == 0)
654 fscache_raise_event(parent, FSCACHE_OBJECT_EV_CLEARED);
655 spin_unlock(&parent->lock);
656 object->parent = NULL;
657 }
658
659 /* this just shifts the object release to the slow work processor */
David Howells52bd75f2009-11-19 18:11:08 +0000660 fscache_stat(&fscache_n_cop_put_object);
David Howells36c95592009-04-03 16:42:38 +0100661 object->cache->ops->put_object(object);
David Howells52bd75f2009-11-19 18:11:08 +0000662 fscache_stat_d(&fscache_n_cop_put_object);
David Howells36c95592009-04-03 16:42:38 +0100663
664 _leave("");
665}
666
667/*
668 * release or recycle an object that the netfs has discarded
669 */
670static void fscache_release_object(struct fscache_object *object)
671{
672 _enter("");
673
674 fscache_drop_object(object);
675}
676
677/*
678 * withdraw an object from active service
679 */
680static void fscache_withdraw_object(struct fscache_object *object)
681{
682 struct fscache_cookie *cookie;
683 bool detached;
684
685 _enter("");
686
687 spin_lock(&object->lock);
688 cookie = object->cookie;
689 if (cookie) {
690 /* need to get the cookie lock before the object lock, starting
691 * from the object pointer */
692 atomic_inc(&cookie->usage);
693 spin_unlock(&object->lock);
694
695 detached = false;
696 spin_lock(&cookie->lock);
697 spin_lock(&object->lock);
698
699 if (object->cookie == cookie) {
700 hlist_del_init(&object->cookie_link);
701 object->cookie = NULL;
702 detached = true;
703 }
704 spin_unlock(&cookie->lock);
705 fscache_cookie_put(cookie);
706 if (detached)
707 fscache_cookie_put(cookie);
708 }
709
710 spin_unlock(&object->lock);
711
712 fscache_drop_object(object);
713}
714
715/*
716 * withdraw an object from active service at the behest of the cache
717 * - need break the links to a cached object cookie
718 * - called under two situations:
719 * (1) recycler decides to reclaim an in-use object
720 * (2) a cache is unmounted
721 * - have to take care as the cookie can be being relinquished by the netfs
722 * simultaneously
723 * - the object is pinned by the caller holding a refcount on it
724 */
725void fscache_withdrawing_object(struct fscache_cache *cache,
726 struct fscache_object *object)
727{
728 bool enqueue = false;
729
730 _enter(",OBJ%x", object->debug_id);
731
732 spin_lock(&object->lock);
733 if (object->state < FSCACHE_OBJECT_WITHDRAWING) {
734 object->state = FSCACHE_OBJECT_WITHDRAWING;
735 enqueue = true;
736 }
737 spin_unlock(&object->lock);
738
739 if (enqueue)
740 fscache_enqueue_object(object);
741
742 _leave("");
743}
744
745/*
746 * allow the slow work item processor to get a ref on an object
747 */
748static int fscache_object_slow_work_get_ref(struct slow_work *work)
749{
750 struct fscache_object *object =
751 container_of(work, struct fscache_object, work);
David Howells52bd75f2009-11-19 18:11:08 +0000752 int ret;
David Howells36c95592009-04-03 16:42:38 +0100753
David Howells52bd75f2009-11-19 18:11:08 +0000754 fscache_stat(&fscache_n_cop_grab_object);
755 ret = object->cache->ops->grab_object(object) ? 0 : -EAGAIN;
756 fscache_stat_d(&fscache_n_cop_grab_object);
757 return ret;
David Howells36c95592009-04-03 16:42:38 +0100758}
759
760/*
761 * allow the slow work item processor to discard a ref on a work item
762 */
763static void fscache_object_slow_work_put_ref(struct slow_work *work)
764{
765 struct fscache_object *object =
766 container_of(work, struct fscache_object, work);
767
David Howells52bd75f2009-11-19 18:11:08 +0000768 fscache_stat(&fscache_n_cop_put_object);
769 object->cache->ops->put_object(object);
770 fscache_stat_d(&fscache_n_cop_put_object);
David Howells36c95592009-04-03 16:42:38 +0100771}
772
773/*
774 * enqueue an object for metadata-type processing
775 */
776void fscache_enqueue_object(struct fscache_object *object)
777{
778 _enter("{OBJ%x}", object->debug_id);
779
780 slow_work_enqueue(&object->work);
781}
782
783/*
784 * enqueue the dependents of an object for metadata-type processing
785 * - the caller must hold the object's lock
786 * - this may cause an already locked object to wind up being processed again
787 */
788static void fscache_enqueue_dependents(struct fscache_object *object)
789{
790 struct fscache_object *dep;
791
792 _enter("{OBJ%x}", object->debug_id);
793
794 if (list_empty(&object->dependents))
795 return;
796
797 spin_lock(&object->lock);
798
799 while (!list_empty(&object->dependents)) {
800 dep = list_entry(object->dependents.next,
801 struct fscache_object, dep_link);
802 list_del_init(&dep->dep_link);
803
804
805 /* sort onto appropriate lists */
806 fscache_enqueue_object(dep);
David Howells52bd75f2009-11-19 18:11:08 +0000807 fscache_stat(&fscache_n_cop_put_object);
David Howells36c95592009-04-03 16:42:38 +0100808 dep->cache->ops->put_object(dep);
David Howells52bd75f2009-11-19 18:11:08 +0000809 fscache_stat_d(&fscache_n_cop_put_object);
David Howells36c95592009-04-03 16:42:38 +0100810
811 if (!list_empty(&object->dependents))
812 cond_resched_lock(&object->lock);
813 }
814
815 spin_unlock(&object->lock);
816}
817
818/*
819 * remove an object from whatever queue it's waiting on
820 * - the caller must hold object->lock
821 */
822void fscache_dequeue_object(struct fscache_object *object)
823{
824 _enter("{OBJ%x}", object->debug_id);
825
826 if (!list_empty(&object->dep_link)) {
827 spin_lock(&object->parent->lock);
828 list_del_init(&object->dep_link);
829 spin_unlock(&object->parent->lock);
830 }
831
832 _leave("");
833}
834
835/**
836 * fscache_check_aux - Ask the netfs whether an object on disk is still valid
837 * @object: The object to ask about
838 * @data: The auxiliary data for the object
839 * @datalen: The size of the auxiliary data
840 *
841 * This function consults the netfs about the coherency state of an object
842 */
843enum fscache_checkaux fscache_check_aux(struct fscache_object *object,
844 const void *data, uint16_t datalen)
845{
846 enum fscache_checkaux result;
847
848 if (!object->cookie->def->check_aux) {
849 fscache_stat(&fscache_n_checkaux_none);
850 return FSCACHE_CHECKAUX_OKAY;
851 }
852
853 result = object->cookie->def->check_aux(object->cookie->netfs_data,
854 data, datalen);
855 switch (result) {
856 /* entry okay as is */
857 case FSCACHE_CHECKAUX_OKAY:
858 fscache_stat(&fscache_n_checkaux_okay);
859 break;
860
861 /* entry requires update */
862 case FSCACHE_CHECKAUX_NEEDS_UPDATE:
863 fscache_stat(&fscache_n_checkaux_update);
864 break;
865
866 /* entry requires deletion */
867 case FSCACHE_CHECKAUX_OBSOLETE:
868 fscache_stat(&fscache_n_checkaux_obsolete);
869 break;
870
871 default:
872 BUG();
873 }
874
875 return result;
876}
877EXPORT_SYMBOL(fscache_check_aux);