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