blob: 2c512cbac38059840002b24d2435b102e6c705c1 [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 Howellsef778e72012-12-20 21:52:36 +000017#include <linux/slab.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",
David Howellsef778e72012-12-20 21:52:36 +000026 [FSCACHE_OBJECT_INVALIDATING] = "OBJECT_INVALIDATING",
David Howells36c95592009-04-03 16:42:38 +010027 [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};
36EXPORT_SYMBOL(fscache_object_states);
37
David Howells4fbf4292009-11-19 18:11:04 +000038const char fscache_object_states_short[FSCACHE_OBJECT__NSTATES][5] = {
David Howells440f0af2009-11-19 18:11:01 +000039 [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",
David Howellsef778e72012-12-20 21:52:36 +000044 [FSCACHE_OBJECT_INVALIDATING] = "INVL",
David Howells440f0af2009-11-19 18:11:01 +000045 [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
Tejun Heo8b8edef2010-07-20 22:09:01 +020055static int fscache_get_object(struct fscache_object *);
56static void fscache_put_object(struct fscache_object *);
David Howells36c95592009-04-03 16:42:38 +010057static void fscache_initialise_object(struct fscache_object *);
58static void fscache_lookup_object(struct fscache_object *);
59static void fscache_object_available(struct fscache_object *);
David Howellsef778e72012-12-20 21:52:36 +000060static void fscache_invalidate_object(struct fscache_object *);
David Howells36c95592009-04-03 16:42:38 +010061static void fscache_release_object(struct fscache_object *);
62static void fscache_withdraw_object(struct fscache_object *);
63static void fscache_enqueue_dependents(struct fscache_object *);
64static void fscache_dequeue_object(struct fscache_object *);
65
David Howells36c95592009-04-03 16:42:38 +010066/*
67 * we need to notify the parent when an op completes that we had outstanding
68 * upon it
69 */
70static 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
85/*
David Howellsef778e72012-12-20 21:52:36 +000086 * Notify netfs of invalidation completion.
87 */
88static 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
94/*
David Howells36c95592009-04-03 16:42:38 +010095 * 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 */
102static void fscache_object_state_machine(struct fscache_object *object)
103{
104 enum fscache_object_state new_state;
David Howellsd461d262009-11-19 18:11:41 +0000105 struct fscache_cookie *cookie;
David Howells8d763492012-12-05 13:34:48 +0000106 int event;
David Howells36c95592009-04-03 16:42:38 +0100107
108 ASSERT(object != NULL);
109
110 _enter("{OBJ%x,%s,%lx}",
111 object->debug_id, fscache_object_states[object->state],
112 object->events);
113
114 switch (object->state) {
115 /* wait for the parent object to become ready */
116 case FSCACHE_OBJECT_INIT:
117 object->event_mask =
David Howells03acc4b2012-12-05 13:34:46 +0000118 FSCACHE_OBJECT_EVENTS_MASK &
119 ~(1 << FSCACHE_OBJECT_EV_CLEARED);
David Howells36c95592009-04-03 16:42:38 +0100120 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
David Howellsef778e72012-12-20 21:52:36 +0000143 /* Invalidate an object on disk */
144 case FSCACHE_OBJECT_INVALIDATING:
145 clear_bit(FSCACHE_OBJECT_EV_INVALIDATE, &object->events);
146 fscache_stat(&fscache_n_invalidates_run);
147 fscache_stat(&fscache_n_cop_invalidate_object);
148 fscache_invalidate_object(object);
149 fscache_stat_d(&fscache_n_cop_invalidate_object);
150 fscache_raise_event(object, FSCACHE_OBJECT_EV_UPDATE);
151 goto active_transit;
152
David Howells36c95592009-04-03 16:42:38 +0100153 /* update the object metadata on disk */
154 case FSCACHE_OBJECT_UPDATING:
155 clear_bit(FSCACHE_OBJECT_EV_UPDATE, &object->events);
156 fscache_stat(&fscache_n_updates_run);
David Howells52bd75f2009-11-19 18:11:08 +0000157 fscache_stat(&fscache_n_cop_update_object);
David Howells36c95592009-04-03 16:42:38 +0100158 object->cache->ops->update_object(object);
David Howells52bd75f2009-11-19 18:11:08 +0000159 fscache_stat_d(&fscache_n_cop_update_object);
David Howells36c95592009-04-03 16:42:38 +0100160 goto active_transit;
161
162 /* handle an object dying during lookup or creation */
163 case FSCACHE_OBJECT_LC_DYING:
164 object->event_mask &= ~(1 << FSCACHE_OBJECT_EV_UPDATE);
David Howells52bd75f2009-11-19 18:11:08 +0000165 fscache_stat(&fscache_n_cop_lookup_complete);
David Howells36c95592009-04-03 16:42:38 +0100166 object->cache->ops->lookup_complete(object);
David Howells52bd75f2009-11-19 18:11:08 +0000167 fscache_stat_d(&fscache_n_cop_lookup_complete);
David Howells36c95592009-04-03 16:42:38 +0100168
169 spin_lock(&object->lock);
170 object->state = FSCACHE_OBJECT_DYING;
David Howellsd461d262009-11-19 18:11:41 +0000171 cookie = object->cookie;
172 if (cookie) {
173 if (test_and_clear_bit(FSCACHE_COOKIE_LOOKING_UP,
174 &cookie->flags))
175 wake_up_bit(&cookie->flags,
176 FSCACHE_COOKIE_LOOKING_UP);
177 if (test_and_clear_bit(FSCACHE_COOKIE_CREATING,
178 &cookie->flags))
179 wake_up_bit(&cookie->flags,
180 FSCACHE_COOKIE_CREATING);
181 }
David Howells36c95592009-04-03 16:42:38 +0100182 spin_unlock(&object->lock);
183
184 fscache_done_parent_op(object);
185
186 /* wait for completion of all active operations on this object
187 * and the death of all child objects of this object */
188 case FSCACHE_OBJECT_DYING:
189 dying:
190 clear_bit(FSCACHE_OBJECT_EV_CLEARED, &object->events);
191 spin_lock(&object->lock);
192 _debug("dying OBJ%x {%d,%d}",
193 object->debug_id, object->n_ops, object->n_children);
194 if (object->n_ops == 0 && object->n_children == 0) {
195 object->event_mask &=
196 ~(1 << FSCACHE_OBJECT_EV_CLEARED);
197 object->event_mask |=
198 (1 << FSCACHE_OBJECT_EV_WITHDRAW) |
199 (1 << FSCACHE_OBJECT_EV_RETIRE) |
200 (1 << FSCACHE_OBJECT_EV_RELEASE) |
201 (1 << FSCACHE_OBJECT_EV_ERROR);
202 } else {
203 object->event_mask &=
204 ~((1 << FSCACHE_OBJECT_EV_WITHDRAW) |
205 (1 << FSCACHE_OBJECT_EV_RETIRE) |
206 (1 << FSCACHE_OBJECT_EV_RELEASE) |
207 (1 << FSCACHE_OBJECT_EV_ERROR));
208 object->event_mask |=
209 1 << FSCACHE_OBJECT_EV_CLEARED;
210 }
211 spin_unlock(&object->lock);
212 fscache_enqueue_dependents(object);
David Howells60d543c2009-11-19 18:11:45 +0000213 fscache_start_operations(object);
David Howells36c95592009-04-03 16:42:38 +0100214 goto terminal_transit;
215
216 /* handle an abort during initialisation */
217 case FSCACHE_OBJECT_ABORT_INIT:
218 _debug("handle abort init %lx", object->events);
219 object->event_mask &= ~(1 << FSCACHE_OBJECT_EV_UPDATE);
220
221 spin_lock(&object->lock);
222 fscache_dequeue_object(object);
223
224 object->state = FSCACHE_OBJECT_DYING;
225 if (test_and_clear_bit(FSCACHE_COOKIE_CREATING,
226 &object->cookie->flags))
227 wake_up_bit(&object->cookie->flags,
228 FSCACHE_COOKIE_CREATING);
229 spin_unlock(&object->lock);
230 goto dying;
231
232 /* handle the netfs releasing an object and possibly marking it
233 * obsolete too */
234 case FSCACHE_OBJECT_RELEASING:
235 case FSCACHE_OBJECT_RECYCLING:
236 object->event_mask &=
237 ~((1 << FSCACHE_OBJECT_EV_WITHDRAW) |
238 (1 << FSCACHE_OBJECT_EV_RETIRE) |
239 (1 << FSCACHE_OBJECT_EV_RELEASE) |
240 (1 << FSCACHE_OBJECT_EV_ERROR));
241 fscache_release_object(object);
242 spin_lock(&object->lock);
243 object->state = FSCACHE_OBJECT_DEAD;
244 spin_unlock(&object->lock);
245 fscache_stat(&fscache_n_object_dead);
246 goto terminal_transit;
247
248 /* handle the parent cache of this object being withdrawn from
249 * active service */
250 case FSCACHE_OBJECT_WITHDRAWING:
251 object->event_mask &=
252 ~((1 << FSCACHE_OBJECT_EV_WITHDRAW) |
253 (1 << FSCACHE_OBJECT_EV_RETIRE) |
254 (1 << FSCACHE_OBJECT_EV_RELEASE) |
255 (1 << FSCACHE_OBJECT_EV_ERROR));
256 fscache_withdraw_object(object);
257 spin_lock(&object->lock);
258 object->state = FSCACHE_OBJECT_DEAD;
259 spin_unlock(&object->lock);
260 fscache_stat(&fscache_n_object_dead);
261 goto terminal_transit;
262
263 /* complain about the object being woken up once it is
264 * deceased */
265 case FSCACHE_OBJECT_DEAD:
266 printk(KERN_ERR "FS-Cache:"
267 " Unexpected event in dead state %lx\n",
268 object->events & object->event_mask);
269 BUG();
270
271 default:
272 printk(KERN_ERR "FS-Cache: Unknown object state %u\n",
273 object->state);
274 BUG();
275 }
276
277 /* determine the transition from a lookup state */
278lookup_transit:
David Howells8d763492012-12-05 13:34:48 +0000279 event = fls(object->events & object->event_mask) - 1;
280 switch (event) {
David Howells36c95592009-04-03 16:42:38 +0100281 case FSCACHE_OBJECT_EV_WITHDRAW:
282 case FSCACHE_OBJECT_EV_RETIRE:
283 case FSCACHE_OBJECT_EV_RELEASE:
284 case FSCACHE_OBJECT_EV_ERROR:
285 new_state = FSCACHE_OBJECT_LC_DYING;
286 goto change_state;
287 case FSCACHE_OBJECT_EV_REQUEUE:
288 goto done;
289 case -1:
290 goto done; /* sleep until event */
291 default:
292 goto unsupported_event;
293 }
294
295 /* determine the transition from an active state */
296active_transit:
David Howells8d763492012-12-05 13:34:48 +0000297 event = fls(object->events & object->event_mask) - 1;
298 switch (event) {
David Howells36c95592009-04-03 16:42:38 +0100299 case FSCACHE_OBJECT_EV_WITHDRAW:
300 case FSCACHE_OBJECT_EV_RETIRE:
301 case FSCACHE_OBJECT_EV_RELEASE:
302 case FSCACHE_OBJECT_EV_ERROR:
303 new_state = FSCACHE_OBJECT_DYING;
304 goto change_state;
David Howellsef778e72012-12-20 21:52:36 +0000305 case FSCACHE_OBJECT_EV_INVALIDATE:
306 new_state = FSCACHE_OBJECT_INVALIDATING;
307 goto change_state;
David Howells36c95592009-04-03 16:42:38 +0100308 case FSCACHE_OBJECT_EV_UPDATE:
309 new_state = FSCACHE_OBJECT_UPDATING;
310 goto change_state;
311 case -1:
312 new_state = FSCACHE_OBJECT_ACTIVE;
313 goto change_state; /* sleep until event */
314 default:
315 goto unsupported_event;
316 }
317
318 /* determine the transition from a terminal state */
319terminal_transit:
David Howells8d763492012-12-05 13:34:48 +0000320 event = fls(object->events & object->event_mask) - 1;
321 switch (event) {
David Howells36c95592009-04-03 16:42:38 +0100322 case FSCACHE_OBJECT_EV_WITHDRAW:
323 new_state = FSCACHE_OBJECT_WITHDRAWING;
324 goto change_state;
325 case FSCACHE_OBJECT_EV_RETIRE:
326 new_state = FSCACHE_OBJECT_RECYCLING;
327 goto change_state;
328 case FSCACHE_OBJECT_EV_RELEASE:
329 new_state = FSCACHE_OBJECT_RELEASING;
330 goto change_state;
331 case FSCACHE_OBJECT_EV_ERROR:
332 new_state = FSCACHE_OBJECT_WITHDRAWING;
333 goto change_state;
334 case FSCACHE_OBJECT_EV_CLEARED:
335 new_state = FSCACHE_OBJECT_DYING;
336 goto change_state;
337 case -1:
338 goto done; /* sleep until event */
339 default:
340 goto unsupported_event;
341 }
342
343change_state:
344 spin_lock(&object->lock);
345 object->state = new_state;
346 spin_unlock(&object->lock);
347
348done:
349 _leave(" [->%s]", fscache_object_states[object->state]);
350 return;
351
352unsupported_event:
353 printk(KERN_ERR "FS-Cache:"
David Howells8d763492012-12-05 13:34:48 +0000354 " Unsupported event %d [%lx/%lx] in state %s\n",
355 event, object->events, object->event_mask,
David Howells36c95592009-04-03 16:42:38 +0100356 fscache_object_states[object->state]);
357 BUG();
358}
359
360/*
361 * execute an object
362 */
Tejun Heo8b8edef2010-07-20 22:09:01 +0200363void fscache_object_work_func(struct work_struct *work)
David Howells36c95592009-04-03 16:42:38 +0100364{
365 struct fscache_object *object =
366 container_of(work, struct fscache_object, work);
367 unsigned long start;
368
369 _enter("{OBJ%x}", object->debug_id);
370
David Howells36c95592009-04-03 16:42:38 +0100371 start = jiffies;
372 fscache_object_state_machine(object);
373 fscache_hist(fscache_objs_histogram, start);
374 if (object->events & object->event_mask)
375 fscache_enqueue_object(object);
David Howells868411b2009-11-19 18:11:48 +0000376 clear_bit(FSCACHE_OBJECT_EV_REQUEUE, &object->events);
Tejun Heo8b8edef2010-07-20 22:09:01 +0200377 fscache_put_object(object);
David Howells36c95592009-04-03 16:42:38 +0100378}
Tejun Heo8b8edef2010-07-20 22:09:01 +0200379EXPORT_SYMBOL(fscache_object_work_func);
David Howells440f0af2009-11-19 18:11:01 +0000380
381/*
David Howells36c95592009-04-03 16:42:38 +0100382 * initialise an object
383 * - check the specified object's parent to see if we can make use of it
384 * immediately to do a creation
385 * - we may need to start the process of creating a parent and we need to wait
386 * for the parent's lookup and creation to complete if it's not there yet
387 * - an object's cookie is pinned until we clear FSCACHE_COOKIE_CREATING on the
388 * leaf-most cookies of the object and all its children
389 */
390static void fscache_initialise_object(struct fscache_object *object)
391{
392 struct fscache_object *parent;
393
394 _enter("");
395 ASSERT(object->cookie != NULL);
396 ASSERT(object->cookie->parent != NULL);
David Howells36c95592009-04-03 16:42:38 +0100397
398 if (object->events & ((1 << FSCACHE_OBJECT_EV_ERROR) |
399 (1 << FSCACHE_OBJECT_EV_RELEASE) |
400 (1 << FSCACHE_OBJECT_EV_RETIRE) |
401 (1 << FSCACHE_OBJECT_EV_WITHDRAW))) {
402 _debug("abort init %lx", object->events);
403 spin_lock(&object->lock);
404 object->state = FSCACHE_OBJECT_ABORT_INIT;
405 spin_unlock(&object->lock);
406 return;
407 }
408
409 spin_lock(&object->cookie->lock);
410 spin_lock_nested(&object->cookie->parent->lock, 1);
411
412 parent = object->parent;
413 if (!parent) {
414 _debug("no parent");
415 set_bit(FSCACHE_OBJECT_EV_WITHDRAW, &object->events);
416 } else {
417 spin_lock(&object->lock);
418 spin_lock_nested(&parent->lock, 1);
419 _debug("parent %s", fscache_object_states[parent->state]);
420
421 if (parent->state >= FSCACHE_OBJECT_DYING) {
422 _debug("bad parent");
423 set_bit(FSCACHE_OBJECT_EV_WITHDRAW, &object->events);
424 } else if (parent->state < FSCACHE_OBJECT_AVAILABLE) {
425 _debug("wait");
426
427 /* we may get woken up in this state by child objects
428 * binding on to us, so we need to make sure we don't
429 * add ourself to the list multiple times */
430 if (list_empty(&object->dep_link)) {
David Howells52bd75f2009-11-19 18:11:08 +0000431 fscache_stat(&fscache_n_cop_grab_object);
David Howells36c95592009-04-03 16:42:38 +0100432 object->cache->ops->grab_object(object);
David Howells52bd75f2009-11-19 18:11:08 +0000433 fscache_stat_d(&fscache_n_cop_grab_object);
David Howells36c95592009-04-03 16:42:38 +0100434 list_add(&object->dep_link,
435 &parent->dependents);
436
437 /* fscache_acquire_non_index_cookie() uses this
438 * to wake the chain up */
439 if (parent->state == FSCACHE_OBJECT_INIT)
440 fscache_enqueue_object(parent);
441 }
442 } else {
443 _debug("go");
444 parent->n_ops++;
445 parent->n_obj_ops++;
446 object->lookup_jif = jiffies;
447 object->state = FSCACHE_OBJECT_LOOKING_UP;
448 set_bit(FSCACHE_OBJECT_EV_REQUEUE, &object->events);
449 }
450
451 spin_unlock(&parent->lock);
452 spin_unlock(&object->lock);
453 }
454
455 spin_unlock(&object->cookie->parent->lock);
456 spin_unlock(&object->cookie->lock);
457 _leave("");
458}
459
460/*
461 * look an object up in the cache from which it was allocated
462 * - we hold an "access lock" on the parent object, so the parent object cannot
463 * be withdrawn by either party till we've finished
464 * - an object's cookie is pinned until we clear FSCACHE_COOKIE_CREATING on the
465 * leaf-most cookies of the object and all its children
466 */
467static void fscache_lookup_object(struct fscache_object *object)
468{
469 struct fscache_cookie *cookie = object->cookie;
470 struct fscache_object *parent;
David Howellsfee096d2009-11-19 18:12:05 +0000471 int ret;
David Howells36c95592009-04-03 16:42:38 +0100472
473 _enter("");
474
475 parent = object->parent;
476 ASSERT(parent != NULL);
477 ASSERTCMP(parent->n_ops, >, 0);
478 ASSERTCMP(parent->n_obj_ops, >, 0);
479
480 /* make sure the parent is still available */
481 ASSERTCMP(parent->state, >=, FSCACHE_OBJECT_AVAILABLE);
482
483 if (parent->state >= FSCACHE_OBJECT_DYING ||
484 test_bit(FSCACHE_IOERROR, &object->cache->flags)) {
485 _debug("unavailable");
486 set_bit(FSCACHE_OBJECT_EV_WITHDRAW, &object->events);
487 _leave("");
488 return;
489 }
490
491 _debug("LOOKUP \"%s/%s\" in \"%s\"",
492 parent->cookie->def->name, cookie->def->name,
493 object->cache->tag->name);
494
495 fscache_stat(&fscache_n_object_lookups);
David Howells52bd75f2009-11-19 18:11:08 +0000496 fscache_stat(&fscache_n_cop_lookup_object);
David Howellsfee096d2009-11-19 18:12:05 +0000497 ret = object->cache->ops->lookup_object(object);
David Howells52bd75f2009-11-19 18:11:08 +0000498 fscache_stat_d(&fscache_n_cop_lookup_object);
David Howells36c95592009-04-03 16:42:38 +0100499
500 if (test_bit(FSCACHE_OBJECT_EV_ERROR, &object->events))
501 set_bit(FSCACHE_COOKIE_UNAVAILABLE, &cookie->flags);
502
David Howellsfee096d2009-11-19 18:12:05 +0000503 if (ret == -ETIMEDOUT) {
504 /* probably stuck behind another object, so move this one to
505 * the back of the queue */
506 fscache_stat(&fscache_n_object_lookups_timed_out);
507 set_bit(FSCACHE_OBJECT_EV_REQUEUE, &object->events);
508 }
509
David Howells36c95592009-04-03 16:42:38 +0100510 _leave("");
511}
512
513/**
514 * fscache_object_lookup_negative - Note negative cookie lookup
515 * @object: Object pointing to cookie to mark
516 *
517 * Note negative lookup, permitting those waiting to read data from an already
518 * existing backing object to continue as there's no data for them to read.
519 */
520void fscache_object_lookup_negative(struct fscache_object *object)
521{
522 struct fscache_cookie *cookie = object->cookie;
523
524 _enter("{OBJ%x,%s}",
525 object->debug_id, fscache_object_states[object->state]);
526
527 spin_lock(&object->lock);
528 if (object->state == FSCACHE_OBJECT_LOOKING_UP) {
529 fscache_stat(&fscache_n_object_lookups_negative);
530
531 /* transit here to allow write requests to begin stacking up
532 * and read requests to begin returning ENODATA */
533 object->state = FSCACHE_OBJECT_CREATING;
534 spin_unlock(&object->lock);
535
536 set_bit(FSCACHE_COOKIE_PENDING_FILL, &cookie->flags);
537 set_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
538
539 _debug("wake up lookup %p", &cookie->flags);
540 smp_mb__before_clear_bit();
541 clear_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags);
542 smp_mb__after_clear_bit();
543 wake_up_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP);
544 set_bit(FSCACHE_OBJECT_EV_REQUEUE, &object->events);
545 } else {
546 ASSERTCMP(object->state, ==, FSCACHE_OBJECT_CREATING);
547 spin_unlock(&object->lock);
548 }
549
550 _leave("");
551}
552EXPORT_SYMBOL(fscache_object_lookup_negative);
553
554/**
555 * fscache_obtained_object - Note successful object lookup or creation
556 * @object: Object pointing to cookie to mark
557 *
558 * Note successful lookup and/or creation, permitting those waiting to write
559 * data to a backing object to continue.
560 *
561 * Note that after calling this, an object's cookie may be relinquished by the
562 * netfs, and so must be accessed with object lock held.
563 */
564void fscache_obtained_object(struct fscache_object *object)
565{
566 struct fscache_cookie *cookie = object->cookie;
567
568 _enter("{OBJ%x,%s}",
569 object->debug_id, fscache_object_states[object->state]);
570
571 /* if we were still looking up, then we must have a positive lookup
572 * result, in which case there may be data available */
573 spin_lock(&object->lock);
574 if (object->state == FSCACHE_OBJECT_LOOKING_UP) {
575 fscache_stat(&fscache_n_object_lookups_positive);
576
577 clear_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
578
579 object->state = FSCACHE_OBJECT_AVAILABLE;
580 spin_unlock(&object->lock);
581
582 smp_mb__before_clear_bit();
583 clear_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags);
584 smp_mb__after_clear_bit();
585 wake_up_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP);
586 set_bit(FSCACHE_OBJECT_EV_REQUEUE, &object->events);
587 } else {
588 ASSERTCMP(object->state, ==, FSCACHE_OBJECT_CREATING);
589 fscache_stat(&fscache_n_object_created);
590
591 object->state = FSCACHE_OBJECT_AVAILABLE;
592 spin_unlock(&object->lock);
593 set_bit(FSCACHE_OBJECT_EV_REQUEUE, &object->events);
594 smp_wmb();
595 }
596
597 if (test_and_clear_bit(FSCACHE_COOKIE_CREATING, &cookie->flags))
598 wake_up_bit(&cookie->flags, FSCACHE_COOKIE_CREATING);
599
600 _leave("");
601}
602EXPORT_SYMBOL(fscache_obtained_object);
603
604/*
605 * handle an object that has just become available
606 */
607static void fscache_object_available(struct fscache_object *object)
608{
609 _enter("{OBJ%x}", object->debug_id);
610
611 spin_lock(&object->lock);
612
David Howells6897e3d2009-11-19 18:11:22 +0000613 if (object->cookie &&
614 test_and_clear_bit(FSCACHE_COOKIE_CREATING, &object->cookie->flags))
David Howells36c95592009-04-03 16:42:38 +0100615 wake_up_bit(&object->cookie->flags, FSCACHE_COOKIE_CREATING);
616
617 fscache_done_parent_op(object);
618 if (object->n_in_progress == 0) {
619 if (object->n_ops > 0) {
620 ASSERTCMP(object->n_ops, >=, object->n_obj_ops);
David Howells36c95592009-04-03 16:42:38 +0100621 fscache_start_operations(object);
622 } else {
623 ASSERT(list_empty(&object->pending_ops));
624 }
625 }
626 spin_unlock(&object->lock);
627
David Howells52bd75f2009-11-19 18:11:08 +0000628 fscache_stat(&fscache_n_cop_lookup_complete);
David Howells36c95592009-04-03 16:42:38 +0100629 object->cache->ops->lookup_complete(object);
David Howells52bd75f2009-11-19 18:11:08 +0000630 fscache_stat_d(&fscache_n_cop_lookup_complete);
David Howells36c95592009-04-03 16:42:38 +0100631 fscache_enqueue_dependents(object);
632
633 fscache_hist(fscache_obj_instantiate_histogram, object->lookup_jif);
634 fscache_stat(&fscache_n_object_avail);
635
636 _leave("");
637}
638
639/*
640 * drop an object's attachments
641 */
642static void fscache_drop_object(struct fscache_object *object)
643{
644 struct fscache_object *parent = object->parent;
645 struct fscache_cache *cache = object->cache;
646
647 _enter("{OBJ%x,%d}", object->debug_id, object->n_children);
648
David Howells6897e3d2009-11-19 18:11:22 +0000649 ASSERTCMP(object->cookie, ==, NULL);
650 ASSERT(hlist_unhashed(&object->cookie_link));
651
David Howells36c95592009-04-03 16:42:38 +0100652 spin_lock(&cache->object_list_lock);
653 list_del_init(&object->cache_link);
654 spin_unlock(&cache->object_list_lock);
655
David Howells52bd75f2009-11-19 18:11:08 +0000656 fscache_stat(&fscache_n_cop_drop_object);
David Howells36c95592009-04-03 16:42:38 +0100657 cache->ops->drop_object(object);
David Howells52bd75f2009-11-19 18:11:08 +0000658 fscache_stat_d(&fscache_n_cop_drop_object);
David Howells36c95592009-04-03 16:42:38 +0100659
660 if (parent) {
661 _debug("release parent OBJ%x {%d}",
662 parent->debug_id, parent->n_children);
663
664 spin_lock(&parent->lock);
665 parent->n_children--;
666 if (parent->n_children == 0)
667 fscache_raise_event(parent, FSCACHE_OBJECT_EV_CLEARED);
668 spin_unlock(&parent->lock);
669 object->parent = NULL;
670 }
671
Tejun Heo8b8edef2010-07-20 22:09:01 +0200672 /* this just shifts the object release to the work processor */
673 fscache_put_object(object);
David Howells36c95592009-04-03 16:42:38 +0100674
675 _leave("");
676}
677
678/*
679 * release or recycle an object that the netfs has discarded
680 */
681static void fscache_release_object(struct fscache_object *object)
682{
683 _enter("");
684
685 fscache_drop_object(object);
686}
687
688/*
689 * withdraw an object from active service
690 */
691static void fscache_withdraw_object(struct fscache_object *object)
692{
693 struct fscache_cookie *cookie;
694 bool detached;
695
696 _enter("");
697
698 spin_lock(&object->lock);
699 cookie = object->cookie;
700 if (cookie) {
701 /* need to get the cookie lock before the object lock, starting
702 * from the object pointer */
703 atomic_inc(&cookie->usage);
704 spin_unlock(&object->lock);
705
706 detached = false;
707 spin_lock(&cookie->lock);
708 spin_lock(&object->lock);
709
710 if (object->cookie == cookie) {
711 hlist_del_init(&object->cookie_link);
712 object->cookie = NULL;
David Howellsef778e72012-12-20 21:52:36 +0000713 fscache_invalidation_complete(cookie);
David Howells36c95592009-04-03 16:42:38 +0100714 detached = true;
715 }
716 spin_unlock(&cookie->lock);
717 fscache_cookie_put(cookie);
718 if (detached)
719 fscache_cookie_put(cookie);
720 }
721
722 spin_unlock(&object->lock);
723
724 fscache_drop_object(object);
725}
726
727/*
728 * withdraw an object from active service at the behest of the cache
729 * - need break the links to a cached object cookie
730 * - called under two situations:
731 * (1) recycler decides to reclaim an in-use object
732 * (2) a cache is unmounted
733 * - have to take care as the cookie can be being relinquished by the netfs
734 * simultaneously
735 * - the object is pinned by the caller holding a refcount on it
736 */
737void fscache_withdrawing_object(struct fscache_cache *cache,
738 struct fscache_object *object)
739{
740 bool enqueue = false;
741
742 _enter(",OBJ%x", object->debug_id);
743
744 spin_lock(&object->lock);
745 if (object->state < FSCACHE_OBJECT_WITHDRAWING) {
746 object->state = FSCACHE_OBJECT_WITHDRAWING;
747 enqueue = true;
748 }
749 spin_unlock(&object->lock);
750
751 if (enqueue)
752 fscache_enqueue_object(object);
753
754 _leave("");
755}
756
757/*
Tejun Heo8b8edef2010-07-20 22:09:01 +0200758 * get a ref on an object
David Howells36c95592009-04-03 16:42:38 +0100759 */
Tejun Heo8b8edef2010-07-20 22:09:01 +0200760static int fscache_get_object(struct fscache_object *object)
David Howells36c95592009-04-03 16:42:38 +0100761{
David Howells52bd75f2009-11-19 18:11:08 +0000762 int ret;
David Howells36c95592009-04-03 16:42:38 +0100763
David Howells52bd75f2009-11-19 18:11:08 +0000764 fscache_stat(&fscache_n_cop_grab_object);
765 ret = object->cache->ops->grab_object(object) ? 0 : -EAGAIN;
766 fscache_stat_d(&fscache_n_cop_grab_object);
767 return ret;
David Howells36c95592009-04-03 16:42:38 +0100768}
769
770/*
Tejun Heo8b8edef2010-07-20 22:09:01 +0200771 * discard a ref on a work item
David Howells36c95592009-04-03 16:42:38 +0100772 */
Tejun Heo8b8edef2010-07-20 22:09:01 +0200773static void fscache_put_object(struct fscache_object *object)
David Howells36c95592009-04-03 16:42:38 +0100774{
David Howells52bd75f2009-11-19 18:11:08 +0000775 fscache_stat(&fscache_n_cop_put_object);
776 object->cache->ops->put_object(object);
777 fscache_stat_d(&fscache_n_cop_put_object);
David Howells36c95592009-04-03 16:42:38 +0100778}
779
780/*
781 * enqueue an object for metadata-type processing
782 */
783void fscache_enqueue_object(struct fscache_object *object)
784{
785 _enter("{OBJ%x}", object->debug_id);
786
Tejun Heo8b8edef2010-07-20 22:09:01 +0200787 if (fscache_get_object(object) >= 0) {
788 wait_queue_head_t *cong_wq =
789 &get_cpu_var(fscache_object_cong_wait);
790
791 if (queue_work(fscache_object_wq, &object->work)) {
792 if (fscache_object_congested())
793 wake_up(cong_wq);
794 } else
795 fscache_put_object(object);
796
797 put_cpu_var(fscache_object_cong_wait);
798 }
David Howells36c95592009-04-03 16:42:38 +0100799}
800
Tejun Heo8b8edef2010-07-20 22:09:01 +0200801/**
802 * fscache_object_sleep_till_congested - Sleep until object wq is congested
803 * @timoutp: Scheduler sleep timeout
804 *
805 * Allow an object handler to sleep until the object workqueue is congested.
806 *
807 * The caller must set up a wake up event before calling this and must have set
808 * the appropriate sleep mode (such as TASK_UNINTERRUPTIBLE) and tested its own
809 * condition before calling this function as no test is made here.
810 *
811 * %true is returned if the object wq is congested, %false otherwise.
812 */
813bool fscache_object_sleep_till_congested(signed long *timeoutp)
814{
815 wait_queue_head_t *cong_wq = &__get_cpu_var(fscache_object_cong_wait);
816 DEFINE_WAIT(wait);
817
818 if (fscache_object_congested())
819 return true;
820
821 add_wait_queue_exclusive(cong_wq, &wait);
822 if (!fscache_object_congested())
823 *timeoutp = schedule_timeout(*timeoutp);
824 finish_wait(cong_wq, &wait);
825
826 return fscache_object_congested();
827}
828EXPORT_SYMBOL_GPL(fscache_object_sleep_till_congested);
829
David Howells36c95592009-04-03 16:42:38 +0100830/*
831 * enqueue the dependents of an object for metadata-type processing
832 * - the caller must hold the object's lock
833 * - this may cause an already locked object to wind up being processed again
834 */
835static void fscache_enqueue_dependents(struct fscache_object *object)
836{
837 struct fscache_object *dep;
838
839 _enter("{OBJ%x}", object->debug_id);
840
841 if (list_empty(&object->dependents))
842 return;
843
844 spin_lock(&object->lock);
845
846 while (!list_empty(&object->dependents)) {
847 dep = list_entry(object->dependents.next,
848 struct fscache_object, dep_link);
849 list_del_init(&dep->dep_link);
850
851
852 /* sort onto appropriate lists */
853 fscache_enqueue_object(dep);
Tejun Heo8b8edef2010-07-20 22:09:01 +0200854 fscache_put_object(dep);
David Howells36c95592009-04-03 16:42:38 +0100855
856 if (!list_empty(&object->dependents))
857 cond_resched_lock(&object->lock);
858 }
859
860 spin_unlock(&object->lock);
861}
862
863/*
864 * remove an object from whatever queue it's waiting on
865 * - the caller must hold object->lock
866 */
867void fscache_dequeue_object(struct fscache_object *object)
868{
869 _enter("{OBJ%x}", object->debug_id);
870
871 if (!list_empty(&object->dep_link)) {
872 spin_lock(&object->parent->lock);
873 list_del_init(&object->dep_link);
874 spin_unlock(&object->parent->lock);
875 }
876
877 _leave("");
878}
879
880/**
881 * fscache_check_aux - Ask the netfs whether an object on disk is still valid
882 * @object: The object to ask about
883 * @data: The auxiliary data for the object
884 * @datalen: The size of the auxiliary data
885 *
886 * This function consults the netfs about the coherency state of an object
887 */
888enum fscache_checkaux fscache_check_aux(struct fscache_object *object,
889 const void *data, uint16_t datalen)
890{
891 enum fscache_checkaux result;
892
893 if (!object->cookie->def->check_aux) {
894 fscache_stat(&fscache_n_checkaux_none);
895 return FSCACHE_CHECKAUX_OKAY;
896 }
897
898 result = object->cookie->def->check_aux(object->cookie->netfs_data,
899 data, datalen);
900 switch (result) {
901 /* entry okay as is */
902 case FSCACHE_CHECKAUX_OKAY:
903 fscache_stat(&fscache_n_checkaux_okay);
904 break;
905
906 /* entry requires update */
907 case FSCACHE_CHECKAUX_NEEDS_UPDATE:
908 fscache_stat(&fscache_n_checkaux_update);
909 break;
910
911 /* entry requires deletion */
912 case FSCACHE_CHECKAUX_OBSOLETE:
913 fscache_stat(&fscache_n_checkaux_obsolete);
914 break;
915
916 default:
917 BUG();
918 }
919
920 return result;
921}
922EXPORT_SYMBOL(fscache_check_aux);
David Howellsef778e72012-12-20 21:52:36 +0000923
924/*
925 * Asynchronously invalidate an object.
926 */
927static void fscache_invalidate_object(struct fscache_object *object)
928{
929 struct fscache_operation *op;
930 struct fscache_cookie *cookie = object->cookie;
931
932 _enter("{OBJ%x}", object->debug_id);
933
934 /* Reject any new read/write ops and abort any that are pending. */
935 fscache_invalidate_writes(cookie);
936 clear_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags);
937 fscache_cancel_all_ops(object);
938
939 /* Now we have to wait for in-progress reads and writes */
940 op = kzalloc(sizeof(*op), GFP_KERNEL);
941 if (!op) {
942 fscache_raise_event(object, FSCACHE_OBJECT_EV_ERROR);
943 _leave(" [ENOMEM]");
944 return;
945 }
946
947 fscache_operation_init(op, object->cache->ops->invalidate_object, NULL);
948 op->flags = FSCACHE_OP_ASYNC | (1 << FSCACHE_OP_EXCLUSIVE);
949
950 spin_lock(&cookie->lock);
951 if (fscache_submit_exclusive_op(object, op) < 0)
David Howells8d763492012-12-05 13:34:48 +0000952 goto submit_op_failed;
David Howellsef778e72012-12-20 21:52:36 +0000953 spin_unlock(&cookie->lock);
954 fscache_put_operation(op);
955
956 /* Once we've completed the invalidation, we know there will be no data
957 * stored in the cache and thus we can reinstate the data-check-skip
958 * optimisation.
959 */
960 set_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
961
962 /* We can allow read and write requests to come in once again. They'll
963 * queue up behind our exclusive invalidation operation.
964 */
965 fscache_invalidation_complete(cookie);
966 _leave("");
David Howells8d763492012-12-05 13:34:48 +0000967 return;
968
969submit_op_failed:
970 spin_unlock(&cookie->lock);
971 kfree(op);
972 fscache_raise_event(object, FSCACHE_OBJECT_EV_ERROR);
973 _leave(" [EIO]");
David Howellsef778e72012-12-20 21:52:36 +0000974}