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> | |
ef778e7a | 17 | #include <linux/slab.h> |
36c95590 DH |
18 | #include "internal.h" |
19 | ||
440f0aff | 20 | const char *fscache_object_states[FSCACHE_OBJECT__NSTATES] = { |
36c95590 DH |
21 | [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", | |
ef778e7a | 26 | [FSCACHE_OBJECT_INVALIDATING] = "OBJECT_INVALIDATING", |
36c95590 DH |
27 | [FSCACHE_OBJECT_UPDATING] = "OBJECT_UPDATING", |
28 | [FSCACHE_OBJECT_DYING] = "OBJECT_DYING", | |
29 | [FSCACHE_OBJECT_LC_DYING] = "OBJECT_LC_DYING", | |
30 | [FSCACHE_OBJECT_ABORT_INIT] = "OBJECT_ABORT_INIT", | |
31 | [FSCACHE_OBJECT_RELEASING] = "OBJECT_RELEASING", | |
32 | [FSCACHE_OBJECT_RECYCLING] = "OBJECT_RECYCLING", | |
33 | [FSCACHE_OBJECT_WITHDRAWING] = "OBJECT_WITHDRAWING", | |
34 | [FSCACHE_OBJECT_DEAD] = "OBJECT_DEAD", | |
35 | }; | |
36 | EXPORT_SYMBOL(fscache_object_states); | |
37 | ||
4fbf4291 | 38 | const char fscache_object_states_short[FSCACHE_OBJECT__NSTATES][5] = { |
440f0aff DH |
39 | [FSCACHE_OBJECT_INIT] = "INIT", |
40 | [FSCACHE_OBJECT_LOOKING_UP] = "LOOK", | |
41 | [FSCACHE_OBJECT_CREATING] = "CRTN", | |
42 | [FSCACHE_OBJECT_AVAILABLE] = "AVBL", | |
43 | [FSCACHE_OBJECT_ACTIVE] = "ACTV", | |
ef778e7a | 44 | [FSCACHE_OBJECT_INVALIDATING] = "INVL", |
440f0aff DH |
45 | [FSCACHE_OBJECT_UPDATING] = "UPDT", |
46 | [FSCACHE_OBJECT_DYING] = "DYNG", | |
47 | [FSCACHE_OBJECT_LC_DYING] = "LCDY", | |
48 | [FSCACHE_OBJECT_ABORT_INIT] = "ABTI", | |
49 | [FSCACHE_OBJECT_RELEASING] = "RELS", | |
50 | [FSCACHE_OBJECT_RECYCLING] = "RCYC", | |
51 | [FSCACHE_OBJECT_WITHDRAWING] = "WTHD", | |
52 | [FSCACHE_OBJECT_DEAD] = "DEAD", | |
53 | }; | |
54 | ||
8b8edefa TH |
55 | static int fscache_get_object(struct fscache_object *); |
56 | static void fscache_put_object(struct fscache_object *); | |
36c95590 DH |
57 | static void fscache_initialise_object(struct fscache_object *); |
58 | static void fscache_lookup_object(struct fscache_object *); | |
59 | static void fscache_object_available(struct fscache_object *); | |
ef778e7a | 60 | static void fscache_invalidate_object(struct fscache_object *); |
36c95590 DH |
61 | static void fscache_release_object(struct fscache_object *); |
62 | static void fscache_withdraw_object(struct fscache_object *); | |
63 | static void fscache_enqueue_dependents(struct fscache_object *); | |
64 | static void fscache_dequeue_object(struct fscache_object *); | |
65 | ||
36c95590 DH |
66 | /* |
67 | * we need to notify the parent when an op completes that we had outstanding | |
68 | * upon it | |
69 | */ | |
70 | static inline void fscache_done_parent_op(struct fscache_object *object) | |
71 | { | |
72 | struct fscache_object *parent = object->parent; | |
73 | ||
74 | _enter("OBJ%x {OBJ%x,%x}", | |
75 | object->debug_id, parent->debug_id, parent->n_ops); | |
76 | ||
77 | spin_lock_nested(&parent->lock, 1); | |
78 | parent->n_ops--; | |
79 | parent->n_obj_ops--; | |
80 | if (parent->n_ops == 0) | |
81 | fscache_raise_event(parent, FSCACHE_OBJECT_EV_CLEARED); | |
82 | spin_unlock(&parent->lock); | |
83 | } | |
84 | ||
ef778e7a DH |
85 | /* |
86 | * Notify netfs of invalidation completion. | |
87 | */ | |
88 | static inline void fscache_invalidation_complete(struct fscache_cookie *cookie) | |
89 | { | |
90 | if (test_and_clear_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags)) | |
91 | wake_up_bit(&cookie->flags, FSCACHE_COOKIE_INVALIDATING); | |
92 | } | |
93 | ||
36c95590 DH |
94 | /* |
95 | * process events that have been sent to an object's state machine | |
96 | * - initiates parent lookup | |
97 | * - does object lookup | |
98 | * - does object creation | |
99 | * - does object recycling and retirement | |
100 | * - does object withdrawal | |
101 | */ | |
102 | static void fscache_object_state_machine(struct fscache_object *object) | |
103 | { | |
104 | enum fscache_object_state new_state; | |
d461d26d | 105 | struct fscache_cookie *cookie; |
36c95590 DH |
106 | |
107 | ASSERT(object != NULL); | |
108 | ||
109 | _enter("{OBJ%x,%s,%lx}", | |
110 | object->debug_id, fscache_object_states[object->state], | |
111 | object->events); | |
112 | ||
113 | switch (object->state) { | |
114 | /* wait for the parent object to become ready */ | |
115 | case FSCACHE_OBJECT_INIT: | |
116 | object->event_mask = | |
03acc4be DH |
117 | FSCACHE_OBJECT_EVENTS_MASK & |
118 | ~(1 << FSCACHE_OBJECT_EV_CLEARED); | |
36c95590 DH |
119 | fscache_initialise_object(object); |
120 | goto done; | |
121 | ||
122 | /* look up the object metadata on disk */ | |
123 | case FSCACHE_OBJECT_LOOKING_UP: | |
124 | fscache_lookup_object(object); | |
125 | goto lookup_transit; | |
126 | ||
127 | /* create the object metadata on disk */ | |
128 | case FSCACHE_OBJECT_CREATING: | |
129 | fscache_lookup_object(object); | |
130 | goto lookup_transit; | |
131 | ||
132 | /* handle an object becoming available; start pending | |
133 | * operations and queue dependent operations for processing */ | |
134 | case FSCACHE_OBJECT_AVAILABLE: | |
135 | fscache_object_available(object); | |
136 | goto active_transit; | |
137 | ||
138 | /* normal running state */ | |
139 | case FSCACHE_OBJECT_ACTIVE: | |
140 | goto active_transit; | |
141 | ||
ef778e7a DH |
142 | /* Invalidate an object on disk */ |
143 | case FSCACHE_OBJECT_INVALIDATING: | |
144 | clear_bit(FSCACHE_OBJECT_EV_INVALIDATE, &object->events); | |
145 | fscache_stat(&fscache_n_invalidates_run); | |
146 | fscache_stat(&fscache_n_cop_invalidate_object); | |
147 | fscache_invalidate_object(object); | |
148 | fscache_stat_d(&fscache_n_cop_invalidate_object); | |
149 | fscache_raise_event(object, FSCACHE_OBJECT_EV_UPDATE); | |
150 | goto active_transit; | |
151 | ||
36c95590 DH |
152 | /* update the object metadata on disk */ |
153 | case FSCACHE_OBJECT_UPDATING: | |
154 | clear_bit(FSCACHE_OBJECT_EV_UPDATE, &object->events); | |
155 | fscache_stat(&fscache_n_updates_run); | |
52bd75fd | 156 | fscache_stat(&fscache_n_cop_update_object); |
36c95590 | 157 | object->cache->ops->update_object(object); |
52bd75fd | 158 | fscache_stat_d(&fscache_n_cop_update_object); |
36c95590 DH |
159 | goto active_transit; |
160 | ||
161 | /* handle an object dying during lookup or creation */ | |
162 | case FSCACHE_OBJECT_LC_DYING: | |
163 | object->event_mask &= ~(1 << FSCACHE_OBJECT_EV_UPDATE); | |
52bd75fd | 164 | fscache_stat(&fscache_n_cop_lookup_complete); |
36c95590 | 165 | object->cache->ops->lookup_complete(object); |
52bd75fd | 166 | fscache_stat_d(&fscache_n_cop_lookup_complete); |
36c95590 DH |
167 | |
168 | spin_lock(&object->lock); | |
169 | object->state = FSCACHE_OBJECT_DYING; | |
d461d26d DH |
170 | cookie = object->cookie; |
171 | if (cookie) { | |
172 | if (test_and_clear_bit(FSCACHE_COOKIE_LOOKING_UP, | |
173 | &cookie->flags)) | |
174 | wake_up_bit(&cookie->flags, | |
175 | FSCACHE_COOKIE_LOOKING_UP); | |
176 | if (test_and_clear_bit(FSCACHE_COOKIE_CREATING, | |
177 | &cookie->flags)) | |
178 | wake_up_bit(&cookie->flags, | |
179 | FSCACHE_COOKIE_CREATING); | |
180 | } | |
36c95590 DH |
181 | spin_unlock(&object->lock); |
182 | ||
183 | fscache_done_parent_op(object); | |
184 | ||
185 | /* wait for completion of all active operations on this object | |
186 | * and the death of all child objects of this object */ | |
187 | case FSCACHE_OBJECT_DYING: | |
188 | dying: | |
189 | clear_bit(FSCACHE_OBJECT_EV_CLEARED, &object->events); | |
190 | spin_lock(&object->lock); | |
191 | _debug("dying OBJ%x {%d,%d}", | |
192 | object->debug_id, object->n_ops, object->n_children); | |
193 | if (object->n_ops == 0 && object->n_children == 0) { | |
194 | object->event_mask &= | |
195 | ~(1 << FSCACHE_OBJECT_EV_CLEARED); | |
196 | object->event_mask |= | |
197 | (1 << FSCACHE_OBJECT_EV_WITHDRAW) | | |
198 | (1 << FSCACHE_OBJECT_EV_RETIRE) | | |
199 | (1 << FSCACHE_OBJECT_EV_RELEASE) | | |
200 | (1 << FSCACHE_OBJECT_EV_ERROR); | |
201 | } else { | |
202 | object->event_mask &= | |
203 | ~((1 << FSCACHE_OBJECT_EV_WITHDRAW) | | |
204 | (1 << FSCACHE_OBJECT_EV_RETIRE) | | |
205 | (1 << FSCACHE_OBJECT_EV_RELEASE) | | |
206 | (1 << FSCACHE_OBJECT_EV_ERROR)); | |
207 | object->event_mask |= | |
208 | 1 << FSCACHE_OBJECT_EV_CLEARED; | |
209 | } | |
210 | spin_unlock(&object->lock); | |
211 | fscache_enqueue_dependents(object); | |
60d543ca | 212 | fscache_start_operations(object); |
36c95590 DH |
213 | goto terminal_transit; |
214 | ||
215 | /* handle an abort during initialisation */ | |
216 | case FSCACHE_OBJECT_ABORT_INIT: | |
217 | _debug("handle abort init %lx", object->events); | |
218 | object->event_mask &= ~(1 << FSCACHE_OBJECT_EV_UPDATE); | |
219 | ||
220 | spin_lock(&object->lock); | |
221 | fscache_dequeue_object(object); | |
222 | ||
223 | object->state = FSCACHE_OBJECT_DYING; | |
224 | if (test_and_clear_bit(FSCACHE_COOKIE_CREATING, | |
225 | &object->cookie->flags)) | |
226 | wake_up_bit(&object->cookie->flags, | |
227 | FSCACHE_COOKIE_CREATING); | |
228 | spin_unlock(&object->lock); | |
229 | goto dying; | |
230 | ||
231 | /* handle the netfs releasing an object and possibly marking it | |
232 | * obsolete too */ | |
233 | case FSCACHE_OBJECT_RELEASING: | |
234 | case FSCACHE_OBJECT_RECYCLING: | |
235 | object->event_mask &= | |
236 | ~((1 << FSCACHE_OBJECT_EV_WITHDRAW) | | |
237 | (1 << FSCACHE_OBJECT_EV_RETIRE) | | |
238 | (1 << FSCACHE_OBJECT_EV_RELEASE) | | |
239 | (1 << FSCACHE_OBJECT_EV_ERROR)); | |
240 | fscache_release_object(object); | |
241 | spin_lock(&object->lock); | |
242 | object->state = FSCACHE_OBJECT_DEAD; | |
243 | spin_unlock(&object->lock); | |
244 | fscache_stat(&fscache_n_object_dead); | |
245 | goto terminal_transit; | |
246 | ||
247 | /* handle the parent cache of this object being withdrawn from | |
248 | * active service */ | |
249 | case FSCACHE_OBJECT_WITHDRAWING: | |
250 | object->event_mask &= | |
251 | ~((1 << FSCACHE_OBJECT_EV_WITHDRAW) | | |
252 | (1 << FSCACHE_OBJECT_EV_RETIRE) | | |
253 | (1 << FSCACHE_OBJECT_EV_RELEASE) | | |
254 | (1 << FSCACHE_OBJECT_EV_ERROR)); | |
255 | fscache_withdraw_object(object); | |
256 | spin_lock(&object->lock); | |
257 | object->state = FSCACHE_OBJECT_DEAD; | |
258 | spin_unlock(&object->lock); | |
259 | fscache_stat(&fscache_n_object_dead); | |
260 | goto terminal_transit; | |
261 | ||
262 | /* complain about the object being woken up once it is | |
263 | * deceased */ | |
264 | case FSCACHE_OBJECT_DEAD: | |
265 | printk(KERN_ERR "FS-Cache:" | |
266 | " Unexpected event in dead state %lx\n", | |
267 | object->events & object->event_mask); | |
268 | BUG(); | |
269 | ||
270 | default: | |
271 | printk(KERN_ERR "FS-Cache: Unknown object state %u\n", | |
272 | object->state); | |
273 | BUG(); | |
274 | } | |
275 | ||
276 | /* determine the transition from a lookup state */ | |
277 | lookup_transit: | |
278 | switch (fls(object->events & object->event_mask) - 1) { | |
279 | case FSCACHE_OBJECT_EV_WITHDRAW: | |
280 | case FSCACHE_OBJECT_EV_RETIRE: | |
281 | case FSCACHE_OBJECT_EV_RELEASE: | |
282 | case FSCACHE_OBJECT_EV_ERROR: | |
283 | new_state = FSCACHE_OBJECT_LC_DYING; | |
284 | goto change_state; | |
285 | case FSCACHE_OBJECT_EV_REQUEUE: | |
286 | goto done; | |
287 | case -1: | |
288 | goto done; /* sleep until event */ | |
289 | default: | |
290 | goto unsupported_event; | |
291 | } | |
292 | ||
293 | /* determine the transition from an active state */ | |
294 | active_transit: | |
295 | switch (fls(object->events & object->event_mask) - 1) { | |
296 | case FSCACHE_OBJECT_EV_WITHDRAW: | |
297 | case FSCACHE_OBJECT_EV_RETIRE: | |
298 | case FSCACHE_OBJECT_EV_RELEASE: | |
299 | case FSCACHE_OBJECT_EV_ERROR: | |
300 | new_state = FSCACHE_OBJECT_DYING; | |
301 | goto change_state; | |
ef778e7a DH |
302 | case FSCACHE_OBJECT_EV_INVALIDATE: |
303 | new_state = FSCACHE_OBJECT_INVALIDATING; | |
304 | goto change_state; | |
36c95590 DH |
305 | case FSCACHE_OBJECT_EV_UPDATE: |
306 | new_state = FSCACHE_OBJECT_UPDATING; | |
307 | goto change_state; | |
308 | case -1: | |
309 | new_state = FSCACHE_OBJECT_ACTIVE; | |
310 | goto change_state; /* sleep until event */ | |
311 | default: | |
312 | goto unsupported_event; | |
313 | } | |
314 | ||
315 | /* determine the transition from a terminal state */ | |
316 | terminal_transit: | |
317 | switch (fls(object->events & object->event_mask) - 1) { | |
318 | case FSCACHE_OBJECT_EV_WITHDRAW: | |
319 | new_state = FSCACHE_OBJECT_WITHDRAWING; | |
320 | goto change_state; | |
321 | case FSCACHE_OBJECT_EV_RETIRE: | |
322 | new_state = FSCACHE_OBJECT_RECYCLING; | |
323 | goto change_state; | |
324 | case FSCACHE_OBJECT_EV_RELEASE: | |
325 | new_state = FSCACHE_OBJECT_RELEASING; | |
326 | goto change_state; | |
327 | case FSCACHE_OBJECT_EV_ERROR: | |
328 | new_state = FSCACHE_OBJECT_WITHDRAWING; | |
329 | goto change_state; | |
330 | case FSCACHE_OBJECT_EV_CLEARED: | |
331 | new_state = FSCACHE_OBJECT_DYING; | |
332 | goto change_state; | |
333 | case -1: | |
334 | goto done; /* sleep until event */ | |
335 | default: | |
336 | goto unsupported_event; | |
337 | } | |
338 | ||
339 | change_state: | |
340 | spin_lock(&object->lock); | |
341 | object->state = new_state; | |
342 | spin_unlock(&object->lock); | |
343 | ||
344 | done: | |
345 | _leave(" [->%s]", fscache_object_states[object->state]); | |
346 | return; | |
347 | ||
348 | unsupported_event: | |
349 | printk(KERN_ERR "FS-Cache:" | |
350 | " Unsupported event %lx [mask %lx] in state %s\n", | |
351 | object->events, object->event_mask, | |
352 | fscache_object_states[object->state]); | |
353 | BUG(); | |
354 | } | |
355 | ||
356 | /* | |
357 | * execute an object | |
358 | */ | |
8b8edefa | 359 | void fscache_object_work_func(struct work_struct *work) |
36c95590 DH |
360 | { |
361 | struct fscache_object *object = | |
362 | container_of(work, struct fscache_object, work); | |
363 | unsigned long start; | |
364 | ||
365 | _enter("{OBJ%x}", object->debug_id); | |
366 | ||
36c95590 DH |
367 | start = jiffies; |
368 | fscache_object_state_machine(object); | |
369 | fscache_hist(fscache_objs_histogram, start); | |
370 | if (object->events & object->event_mask) | |
371 | fscache_enqueue_object(object); | |
868411be | 372 | clear_bit(FSCACHE_OBJECT_EV_REQUEUE, &object->events); |
8b8edefa | 373 | fscache_put_object(object); |
36c95590 | 374 | } |
8b8edefa | 375 | EXPORT_SYMBOL(fscache_object_work_func); |
440f0aff | 376 | |
36c95590 DH |
377 | /* |
378 | * initialise an object | |
379 | * - check the specified object's parent to see if we can make use of it | |
380 | * immediately to do a creation | |
381 | * - we may need to start the process of creating a parent and we need to wait | |
382 | * for the parent's lookup and creation to complete if it's not there yet | |
383 | * - an object's cookie is pinned until we clear FSCACHE_COOKIE_CREATING on the | |
384 | * leaf-most cookies of the object and all its children | |
385 | */ | |
386 | static void fscache_initialise_object(struct fscache_object *object) | |
387 | { | |
388 | struct fscache_object *parent; | |
389 | ||
390 | _enter(""); | |
391 | ASSERT(object->cookie != NULL); | |
392 | ASSERT(object->cookie->parent != NULL); | |
36c95590 DH |
393 | |
394 | if (object->events & ((1 << FSCACHE_OBJECT_EV_ERROR) | | |
395 | (1 << FSCACHE_OBJECT_EV_RELEASE) | | |
396 | (1 << FSCACHE_OBJECT_EV_RETIRE) | | |
397 | (1 << FSCACHE_OBJECT_EV_WITHDRAW))) { | |
398 | _debug("abort init %lx", object->events); | |
399 | spin_lock(&object->lock); | |
400 | object->state = FSCACHE_OBJECT_ABORT_INIT; | |
401 | spin_unlock(&object->lock); | |
402 | return; | |
403 | } | |
404 | ||
405 | spin_lock(&object->cookie->lock); | |
406 | spin_lock_nested(&object->cookie->parent->lock, 1); | |
407 | ||
408 | parent = object->parent; | |
409 | if (!parent) { | |
410 | _debug("no parent"); | |
411 | set_bit(FSCACHE_OBJECT_EV_WITHDRAW, &object->events); | |
412 | } else { | |
413 | spin_lock(&object->lock); | |
414 | spin_lock_nested(&parent->lock, 1); | |
415 | _debug("parent %s", fscache_object_states[parent->state]); | |
416 | ||
417 | if (parent->state >= FSCACHE_OBJECT_DYING) { | |
418 | _debug("bad parent"); | |
419 | set_bit(FSCACHE_OBJECT_EV_WITHDRAW, &object->events); | |
420 | } else if (parent->state < FSCACHE_OBJECT_AVAILABLE) { | |
421 | _debug("wait"); | |
422 | ||
423 | /* we may get woken up in this state by child objects | |
424 | * binding on to us, so we need to make sure we don't | |
425 | * add ourself to the list multiple times */ | |
426 | if (list_empty(&object->dep_link)) { | |
52bd75fd | 427 | fscache_stat(&fscache_n_cop_grab_object); |
36c95590 | 428 | object->cache->ops->grab_object(object); |
52bd75fd | 429 | fscache_stat_d(&fscache_n_cop_grab_object); |
36c95590 DH |
430 | list_add(&object->dep_link, |
431 | &parent->dependents); | |
432 | ||
433 | /* fscache_acquire_non_index_cookie() uses this | |
434 | * to wake the chain up */ | |
435 | if (parent->state == FSCACHE_OBJECT_INIT) | |
436 | fscache_enqueue_object(parent); | |
437 | } | |
438 | } else { | |
439 | _debug("go"); | |
440 | parent->n_ops++; | |
441 | parent->n_obj_ops++; | |
442 | object->lookup_jif = jiffies; | |
443 | object->state = FSCACHE_OBJECT_LOOKING_UP; | |
444 | set_bit(FSCACHE_OBJECT_EV_REQUEUE, &object->events); | |
445 | } | |
446 | ||
447 | spin_unlock(&parent->lock); | |
448 | spin_unlock(&object->lock); | |
449 | } | |
450 | ||
451 | spin_unlock(&object->cookie->parent->lock); | |
452 | spin_unlock(&object->cookie->lock); | |
453 | _leave(""); | |
454 | } | |
455 | ||
456 | /* | |
457 | * look an object up in the cache from which it was allocated | |
458 | * - we hold an "access lock" on the parent object, so the parent object cannot | |
459 | * be withdrawn by either party till we've finished | |
460 | * - an object's cookie is pinned until we clear FSCACHE_COOKIE_CREATING on the | |
461 | * leaf-most cookies of the object and all its children | |
462 | */ | |
463 | static void fscache_lookup_object(struct fscache_object *object) | |
464 | { | |
465 | struct fscache_cookie *cookie = object->cookie; | |
466 | struct fscache_object *parent; | |
fee096de | 467 | int ret; |
36c95590 DH |
468 | |
469 | _enter(""); | |
470 | ||
471 | parent = object->parent; | |
472 | ASSERT(parent != NULL); | |
473 | ASSERTCMP(parent->n_ops, >, 0); | |
474 | ASSERTCMP(parent->n_obj_ops, >, 0); | |
475 | ||
476 | /* make sure the parent is still available */ | |
477 | ASSERTCMP(parent->state, >=, FSCACHE_OBJECT_AVAILABLE); | |
478 | ||
479 | if (parent->state >= FSCACHE_OBJECT_DYING || | |
480 | test_bit(FSCACHE_IOERROR, &object->cache->flags)) { | |
481 | _debug("unavailable"); | |
482 | set_bit(FSCACHE_OBJECT_EV_WITHDRAW, &object->events); | |
483 | _leave(""); | |
484 | return; | |
485 | } | |
486 | ||
487 | _debug("LOOKUP \"%s/%s\" in \"%s\"", | |
488 | parent->cookie->def->name, cookie->def->name, | |
489 | object->cache->tag->name); | |
490 | ||
491 | fscache_stat(&fscache_n_object_lookups); | |
52bd75fd | 492 | fscache_stat(&fscache_n_cop_lookup_object); |
fee096de | 493 | ret = object->cache->ops->lookup_object(object); |
52bd75fd | 494 | fscache_stat_d(&fscache_n_cop_lookup_object); |
36c95590 DH |
495 | |
496 | if (test_bit(FSCACHE_OBJECT_EV_ERROR, &object->events)) | |
497 | set_bit(FSCACHE_COOKIE_UNAVAILABLE, &cookie->flags); | |
498 | ||
fee096de DH |
499 | if (ret == -ETIMEDOUT) { |
500 | /* probably stuck behind another object, so move this one to | |
501 | * the back of the queue */ | |
502 | fscache_stat(&fscache_n_object_lookups_timed_out); | |
503 | set_bit(FSCACHE_OBJECT_EV_REQUEUE, &object->events); | |
504 | } | |
505 | ||
36c95590 DH |
506 | _leave(""); |
507 | } | |
508 | ||
509 | /** | |
510 | * fscache_object_lookup_negative - Note negative cookie lookup | |
511 | * @object: Object pointing to cookie to mark | |
512 | * | |
513 | * Note negative lookup, permitting those waiting to read data from an already | |
514 | * existing backing object to continue as there's no data for them to read. | |
515 | */ | |
516 | void fscache_object_lookup_negative(struct fscache_object *object) | |
517 | { | |
518 | struct fscache_cookie *cookie = object->cookie; | |
519 | ||
520 | _enter("{OBJ%x,%s}", | |
521 | object->debug_id, fscache_object_states[object->state]); | |
522 | ||
523 | spin_lock(&object->lock); | |
524 | if (object->state == FSCACHE_OBJECT_LOOKING_UP) { | |
525 | fscache_stat(&fscache_n_object_lookups_negative); | |
526 | ||
527 | /* transit here to allow write requests to begin stacking up | |
528 | * and read requests to begin returning ENODATA */ | |
529 | object->state = FSCACHE_OBJECT_CREATING; | |
530 | spin_unlock(&object->lock); | |
531 | ||
532 | set_bit(FSCACHE_COOKIE_PENDING_FILL, &cookie->flags); | |
533 | set_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags); | |
534 | ||
535 | _debug("wake up lookup %p", &cookie->flags); | |
536 | smp_mb__before_clear_bit(); | |
537 | clear_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags); | |
538 | smp_mb__after_clear_bit(); | |
539 | wake_up_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP); | |
540 | set_bit(FSCACHE_OBJECT_EV_REQUEUE, &object->events); | |
541 | } else { | |
542 | ASSERTCMP(object->state, ==, FSCACHE_OBJECT_CREATING); | |
543 | spin_unlock(&object->lock); | |
544 | } | |
545 | ||
546 | _leave(""); | |
547 | } | |
548 | EXPORT_SYMBOL(fscache_object_lookup_negative); | |
549 | ||
550 | /** | |
551 | * fscache_obtained_object - Note successful object lookup or creation | |
552 | * @object: Object pointing to cookie to mark | |
553 | * | |
554 | * Note successful lookup and/or creation, permitting those waiting to write | |
555 | * data to a backing object to continue. | |
556 | * | |
557 | * Note that after calling this, an object's cookie may be relinquished by the | |
558 | * netfs, and so must be accessed with object lock held. | |
559 | */ | |
560 | void fscache_obtained_object(struct fscache_object *object) | |
561 | { | |
562 | struct fscache_cookie *cookie = object->cookie; | |
563 | ||
564 | _enter("{OBJ%x,%s}", | |
565 | object->debug_id, fscache_object_states[object->state]); | |
566 | ||
567 | /* if we were still looking up, then we must have a positive lookup | |
568 | * result, in which case there may be data available */ | |
569 | spin_lock(&object->lock); | |
570 | if (object->state == FSCACHE_OBJECT_LOOKING_UP) { | |
571 | fscache_stat(&fscache_n_object_lookups_positive); | |
572 | ||
573 | clear_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags); | |
574 | ||
575 | object->state = FSCACHE_OBJECT_AVAILABLE; | |
576 | spin_unlock(&object->lock); | |
577 | ||
578 | smp_mb__before_clear_bit(); | |
579 | clear_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags); | |
580 | smp_mb__after_clear_bit(); | |
581 | wake_up_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP); | |
582 | set_bit(FSCACHE_OBJECT_EV_REQUEUE, &object->events); | |
583 | } else { | |
584 | ASSERTCMP(object->state, ==, FSCACHE_OBJECT_CREATING); | |
585 | fscache_stat(&fscache_n_object_created); | |
586 | ||
587 | object->state = FSCACHE_OBJECT_AVAILABLE; | |
588 | spin_unlock(&object->lock); | |
589 | set_bit(FSCACHE_OBJECT_EV_REQUEUE, &object->events); | |
590 | smp_wmb(); | |
591 | } | |
592 | ||
593 | if (test_and_clear_bit(FSCACHE_COOKIE_CREATING, &cookie->flags)) | |
594 | wake_up_bit(&cookie->flags, FSCACHE_COOKIE_CREATING); | |
595 | ||
596 | _leave(""); | |
597 | } | |
598 | EXPORT_SYMBOL(fscache_obtained_object); | |
599 | ||
600 | /* | |
601 | * handle an object that has just become available | |
602 | */ | |
603 | static void fscache_object_available(struct fscache_object *object) | |
604 | { | |
605 | _enter("{OBJ%x}", object->debug_id); | |
606 | ||
607 | spin_lock(&object->lock); | |
608 | ||
6897e3df DH |
609 | if (object->cookie && |
610 | test_and_clear_bit(FSCACHE_COOKIE_CREATING, &object->cookie->flags)) | |
36c95590 DH |
611 | wake_up_bit(&object->cookie->flags, FSCACHE_COOKIE_CREATING); |
612 | ||
613 | fscache_done_parent_op(object); | |
614 | if (object->n_in_progress == 0) { | |
615 | if (object->n_ops > 0) { | |
616 | ASSERTCMP(object->n_ops, >=, object->n_obj_ops); | |
36c95590 DH |
617 | fscache_start_operations(object); |
618 | } else { | |
619 | ASSERT(list_empty(&object->pending_ops)); | |
620 | } | |
621 | } | |
622 | spin_unlock(&object->lock); | |
623 | ||
52bd75fd | 624 | fscache_stat(&fscache_n_cop_lookup_complete); |
36c95590 | 625 | object->cache->ops->lookup_complete(object); |
52bd75fd | 626 | fscache_stat_d(&fscache_n_cop_lookup_complete); |
36c95590 DH |
627 | fscache_enqueue_dependents(object); |
628 | ||
629 | fscache_hist(fscache_obj_instantiate_histogram, object->lookup_jif); | |
630 | fscache_stat(&fscache_n_object_avail); | |
631 | ||
632 | _leave(""); | |
633 | } | |
634 | ||
635 | /* | |
636 | * drop an object's attachments | |
637 | */ | |
638 | static void fscache_drop_object(struct fscache_object *object) | |
639 | { | |
640 | struct fscache_object *parent = object->parent; | |
641 | struct fscache_cache *cache = object->cache; | |
642 | ||
643 | _enter("{OBJ%x,%d}", object->debug_id, object->n_children); | |
644 | ||
6897e3df DH |
645 | ASSERTCMP(object->cookie, ==, NULL); |
646 | ASSERT(hlist_unhashed(&object->cookie_link)); | |
647 | ||
36c95590 DH |
648 | spin_lock(&cache->object_list_lock); |
649 | list_del_init(&object->cache_link); | |
650 | spin_unlock(&cache->object_list_lock); | |
651 | ||
52bd75fd | 652 | fscache_stat(&fscache_n_cop_drop_object); |
36c95590 | 653 | cache->ops->drop_object(object); |
52bd75fd | 654 | fscache_stat_d(&fscache_n_cop_drop_object); |
36c95590 DH |
655 | |
656 | if (parent) { | |
657 | _debug("release parent OBJ%x {%d}", | |
658 | parent->debug_id, parent->n_children); | |
659 | ||
660 | spin_lock(&parent->lock); | |
661 | parent->n_children--; | |
662 | if (parent->n_children == 0) | |
663 | fscache_raise_event(parent, FSCACHE_OBJECT_EV_CLEARED); | |
664 | spin_unlock(&parent->lock); | |
665 | object->parent = NULL; | |
666 | } | |
667 | ||
8b8edefa TH |
668 | /* this just shifts the object release to the work processor */ |
669 | fscache_put_object(object); | |
36c95590 DH |
670 | |
671 | _leave(""); | |
672 | } | |
673 | ||
674 | /* | |
675 | * release or recycle an object that the netfs has discarded | |
676 | */ | |
677 | static void fscache_release_object(struct fscache_object *object) | |
678 | { | |
679 | _enter(""); | |
680 | ||
681 | fscache_drop_object(object); | |
682 | } | |
683 | ||
684 | /* | |
685 | * withdraw an object from active service | |
686 | */ | |
687 | static void fscache_withdraw_object(struct fscache_object *object) | |
688 | { | |
689 | struct fscache_cookie *cookie; | |
690 | bool detached; | |
691 | ||
692 | _enter(""); | |
693 | ||
694 | spin_lock(&object->lock); | |
695 | cookie = object->cookie; | |
696 | if (cookie) { | |
697 | /* need to get the cookie lock before the object lock, starting | |
698 | * from the object pointer */ | |
699 | atomic_inc(&cookie->usage); | |
700 | spin_unlock(&object->lock); | |
701 | ||
702 | detached = false; | |
703 | spin_lock(&cookie->lock); | |
704 | spin_lock(&object->lock); | |
705 | ||
706 | if (object->cookie == cookie) { | |
707 | hlist_del_init(&object->cookie_link); | |
708 | object->cookie = NULL; | |
ef778e7a | 709 | fscache_invalidation_complete(cookie); |
36c95590 DH |
710 | detached = true; |
711 | } | |
712 | spin_unlock(&cookie->lock); | |
713 | fscache_cookie_put(cookie); | |
714 | if (detached) | |
715 | fscache_cookie_put(cookie); | |
716 | } | |
717 | ||
718 | spin_unlock(&object->lock); | |
719 | ||
720 | fscache_drop_object(object); | |
721 | } | |
722 | ||
723 | /* | |
724 | * withdraw an object from active service at the behest of the cache | |
725 | * - need break the links to a cached object cookie | |
726 | * - called under two situations: | |
727 | * (1) recycler decides to reclaim an in-use object | |
728 | * (2) a cache is unmounted | |
729 | * - have to take care as the cookie can be being relinquished by the netfs | |
730 | * simultaneously | |
731 | * - the object is pinned by the caller holding a refcount on it | |
732 | */ | |
733 | void fscache_withdrawing_object(struct fscache_cache *cache, | |
734 | struct fscache_object *object) | |
735 | { | |
736 | bool enqueue = false; | |
737 | ||
738 | _enter(",OBJ%x", object->debug_id); | |
739 | ||
740 | spin_lock(&object->lock); | |
741 | if (object->state < FSCACHE_OBJECT_WITHDRAWING) { | |
742 | object->state = FSCACHE_OBJECT_WITHDRAWING; | |
743 | enqueue = true; | |
744 | } | |
745 | spin_unlock(&object->lock); | |
746 | ||
747 | if (enqueue) | |
748 | fscache_enqueue_object(object); | |
749 | ||
750 | _leave(""); | |
751 | } | |
752 | ||
753 | /* | |
8b8edefa | 754 | * get a ref on an object |
36c95590 | 755 | */ |
8b8edefa | 756 | static int fscache_get_object(struct fscache_object *object) |
36c95590 | 757 | { |
52bd75fd | 758 | int ret; |
36c95590 | 759 | |
52bd75fd DH |
760 | fscache_stat(&fscache_n_cop_grab_object); |
761 | ret = object->cache->ops->grab_object(object) ? 0 : -EAGAIN; | |
762 | fscache_stat_d(&fscache_n_cop_grab_object); | |
763 | return ret; | |
36c95590 DH |
764 | } |
765 | ||
766 | /* | |
8b8edefa | 767 | * discard a ref on a work item |
36c95590 | 768 | */ |
8b8edefa | 769 | static void fscache_put_object(struct fscache_object *object) |
36c95590 | 770 | { |
52bd75fd DH |
771 | fscache_stat(&fscache_n_cop_put_object); |
772 | object->cache->ops->put_object(object); | |
773 | fscache_stat_d(&fscache_n_cop_put_object); | |
36c95590 DH |
774 | } |
775 | ||
776 | /* | |
777 | * enqueue an object for metadata-type processing | |
778 | */ | |
779 | void fscache_enqueue_object(struct fscache_object *object) | |
780 | { | |
781 | _enter("{OBJ%x}", object->debug_id); | |
782 | ||
8b8edefa TH |
783 | if (fscache_get_object(object) >= 0) { |
784 | wait_queue_head_t *cong_wq = | |
785 | &get_cpu_var(fscache_object_cong_wait); | |
786 | ||
787 | if (queue_work(fscache_object_wq, &object->work)) { | |
788 | if (fscache_object_congested()) | |
789 | wake_up(cong_wq); | |
790 | } else | |
791 | fscache_put_object(object); | |
792 | ||
793 | put_cpu_var(fscache_object_cong_wait); | |
794 | } | |
795 | } | |
796 | ||
797 | /** | |
798 | * fscache_object_sleep_till_congested - Sleep until object wq is congested | |
799 | * @timoutp: Scheduler sleep timeout | |
800 | * | |
801 | * Allow an object handler to sleep until the object workqueue is congested. | |
802 | * | |
803 | * The caller must set up a wake up event before calling this and must have set | |
804 | * the appropriate sleep mode (such as TASK_UNINTERRUPTIBLE) and tested its own | |
805 | * condition before calling this function as no test is made here. | |
806 | * | |
807 | * %true is returned if the object wq is congested, %false otherwise. | |
808 | */ | |
809 | bool fscache_object_sleep_till_congested(signed long *timeoutp) | |
810 | { | |
811 | wait_queue_head_t *cong_wq = &__get_cpu_var(fscache_object_cong_wait); | |
812 | DEFINE_WAIT(wait); | |
813 | ||
814 | if (fscache_object_congested()) | |
815 | return true; | |
816 | ||
817 | add_wait_queue_exclusive(cong_wq, &wait); | |
818 | if (!fscache_object_congested()) | |
819 | *timeoutp = schedule_timeout(*timeoutp); | |
820 | finish_wait(cong_wq, &wait); | |
821 | ||
822 | return fscache_object_congested(); | |
36c95590 | 823 | } |
8b8edefa | 824 | EXPORT_SYMBOL_GPL(fscache_object_sleep_till_congested); |
36c95590 DH |
825 | |
826 | /* | |
827 | * enqueue the dependents of an object for metadata-type processing | |
828 | * - the caller must hold the object's lock | |
829 | * - this may cause an already locked object to wind up being processed again | |
830 | */ | |
831 | static void fscache_enqueue_dependents(struct fscache_object *object) | |
832 | { | |
833 | struct fscache_object *dep; | |
834 | ||
835 | _enter("{OBJ%x}", object->debug_id); | |
836 | ||
837 | if (list_empty(&object->dependents)) | |
838 | return; | |
839 | ||
840 | spin_lock(&object->lock); | |
841 | ||
842 | while (!list_empty(&object->dependents)) { | |
843 | dep = list_entry(object->dependents.next, | |
844 | struct fscache_object, dep_link); | |
845 | list_del_init(&dep->dep_link); | |
846 | ||
847 | ||
848 | /* sort onto appropriate lists */ | |
849 | fscache_enqueue_object(dep); | |
8b8edefa | 850 | fscache_put_object(dep); |
36c95590 DH |
851 | |
852 | if (!list_empty(&object->dependents)) | |
853 | cond_resched_lock(&object->lock); | |
854 | } | |
855 | ||
856 | spin_unlock(&object->lock); | |
857 | } | |
858 | ||
859 | /* | |
860 | * remove an object from whatever queue it's waiting on | |
861 | * - the caller must hold object->lock | |
862 | */ | |
863 | void fscache_dequeue_object(struct fscache_object *object) | |
864 | { | |
865 | _enter("{OBJ%x}", object->debug_id); | |
866 | ||
867 | if (!list_empty(&object->dep_link)) { | |
868 | spin_lock(&object->parent->lock); | |
869 | list_del_init(&object->dep_link); | |
870 | spin_unlock(&object->parent->lock); | |
871 | } | |
872 | ||
873 | _leave(""); | |
874 | } | |
875 | ||
876 | /** | |
877 | * fscache_check_aux - Ask the netfs whether an object on disk is still valid | |
878 | * @object: The object to ask about | |
879 | * @data: The auxiliary data for the object | |
880 | * @datalen: The size of the auxiliary data | |
881 | * | |
882 | * This function consults the netfs about the coherency state of an object | |
883 | */ | |
884 | enum fscache_checkaux fscache_check_aux(struct fscache_object *object, | |
885 | const void *data, uint16_t datalen) | |
886 | { | |
887 | enum fscache_checkaux result; | |
888 | ||
889 | if (!object->cookie->def->check_aux) { | |
890 | fscache_stat(&fscache_n_checkaux_none); | |
891 | return FSCACHE_CHECKAUX_OKAY; | |
892 | } | |
893 | ||
894 | result = object->cookie->def->check_aux(object->cookie->netfs_data, | |
895 | data, datalen); | |
896 | switch (result) { | |
897 | /* entry okay as is */ | |
898 | case FSCACHE_CHECKAUX_OKAY: | |
899 | fscache_stat(&fscache_n_checkaux_okay); | |
900 | break; | |
901 | ||
902 | /* entry requires update */ | |
903 | case FSCACHE_CHECKAUX_NEEDS_UPDATE: | |
904 | fscache_stat(&fscache_n_checkaux_update); | |
905 | break; | |
906 | ||
907 | /* entry requires deletion */ | |
908 | case FSCACHE_CHECKAUX_OBSOLETE: | |
909 | fscache_stat(&fscache_n_checkaux_obsolete); | |
910 | break; | |
911 | ||
912 | default: | |
913 | BUG(); | |
914 | } | |
915 | ||
916 | return result; | |
917 | } | |
918 | EXPORT_SYMBOL(fscache_check_aux); | |
ef778e7a DH |
919 | |
920 | /* | |
921 | * Asynchronously invalidate an object. | |
922 | */ | |
923 | static void fscache_invalidate_object(struct fscache_object *object) | |
924 | { | |
925 | struct fscache_operation *op; | |
926 | struct fscache_cookie *cookie = object->cookie; | |
927 | ||
928 | _enter("{OBJ%x}", object->debug_id); | |
929 | ||
930 | /* Reject any new read/write ops and abort any that are pending. */ | |
931 | fscache_invalidate_writes(cookie); | |
932 | clear_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags); | |
933 | fscache_cancel_all_ops(object); | |
934 | ||
935 | /* Now we have to wait for in-progress reads and writes */ | |
936 | op = kzalloc(sizeof(*op), GFP_KERNEL); | |
937 | if (!op) { | |
938 | fscache_raise_event(object, FSCACHE_OBJECT_EV_ERROR); | |
939 | _leave(" [ENOMEM]"); | |
940 | return; | |
941 | } | |
942 | ||
943 | fscache_operation_init(op, object->cache->ops->invalidate_object, NULL); | |
944 | op->flags = FSCACHE_OP_ASYNC | (1 << FSCACHE_OP_EXCLUSIVE); | |
945 | ||
946 | spin_lock(&cookie->lock); | |
947 | if (fscache_submit_exclusive_op(object, op) < 0) | |
948 | BUG(); | |
949 | spin_unlock(&cookie->lock); | |
950 | fscache_put_operation(op); | |
951 | ||
952 | /* Once we've completed the invalidation, we know there will be no data | |
953 | * stored in the cache and thus we can reinstate the data-check-skip | |
954 | * optimisation. | |
955 | */ | |
956 | set_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags); | |
957 | ||
958 | /* We can allow read and write requests to come in once again. They'll | |
959 | * queue up behind our exclusive invalidation operation. | |
960 | */ | |
961 | fscache_invalidation_complete(cookie); | |
962 | _leave(""); | |
963 | } |