1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#define FSCACHE_DEBUG_LEVEL COOKIE
16#include <linux/module.h>
17#include <linux/slab.h>
18#include <linux/prefetch.h>
19#include "internal.h"
20
21static const struct fscache_state *fscache_abort_initialisation(struct fscache_object *, int);
22static const struct fscache_state *fscache_kill_dependents(struct fscache_object *, int);
23static const struct fscache_state *fscache_drop_object(struct fscache_object *, int);
24static const struct fscache_state *fscache_initialise_object(struct fscache_object *, int);
25static const struct fscache_state *fscache_invalidate_object(struct fscache_object *, int);
26static const struct fscache_state *fscache_jumpstart_dependents(struct fscache_object *, int);
27static const struct fscache_state *fscache_kill_object(struct fscache_object *, int);
28static const struct fscache_state *fscache_lookup_failure(struct fscache_object *, int);
29static const struct fscache_state *fscache_look_up_object(struct fscache_object *, int);
30static const struct fscache_state *fscache_object_available(struct fscache_object *, int);
31static const struct fscache_state *fscache_parent_ready(struct fscache_object *, int);
32static const struct fscache_state *fscache_update_object(struct fscache_object *, int);
33static const struct fscache_state *fscache_object_dead(struct fscache_object *, int);
34
35#define __STATE_NAME(n) fscache_osm_##n
36#define STATE(n) (&__STATE_NAME(n))
37
38
39
40
41
42
43
44
45#define WORK_STATE(n, sn, f) \
46 const struct fscache_state __STATE_NAME(n) = { \
47 .name = #n, \
48 .short_name = sn, \
49 .work = f \
50 }
51
52
53
54
55#define transit_to(state) ({ prefetch(&STATE(state)->work); STATE(state); })
56
57#define NO_TRANSIT ((struct fscache_state *)NULL)
58
59
60
61
62
63
64
65
66#define WAIT_STATE(n, sn, ...) \
67 const struct fscache_state __STATE_NAME(n) = { \
68 .name = #n, \
69 .short_name = sn, \
70 .work = NULL, \
71 .transitions = { __VA_ARGS__, { 0, NULL } } \
72 }
73
74#define TRANSIT_TO(state, emask) \
75 { .events = (emask), .transit_to = STATE(state) }
76
77
78
79
80static WORK_STATE(INIT_OBJECT, "INIT", fscache_initialise_object);
81static WORK_STATE(PARENT_READY, "PRDY", fscache_parent_ready);
82static WORK_STATE(ABORT_INIT, "ABRT", fscache_abort_initialisation);
83static WORK_STATE(LOOK_UP_OBJECT, "LOOK", fscache_look_up_object);
84static WORK_STATE(CREATE_OBJECT, "CRTO", fscache_look_up_object);
85static WORK_STATE(OBJECT_AVAILABLE, "AVBL", fscache_object_available);
86static WORK_STATE(JUMPSTART_DEPS, "JUMP", fscache_jumpstart_dependents);
87
88static WORK_STATE(INVALIDATE_OBJECT, "INVL", fscache_invalidate_object);
89static WORK_STATE(UPDATE_OBJECT, "UPDT", fscache_update_object);
90
91static WORK_STATE(LOOKUP_FAILURE, "LCFL", fscache_lookup_failure);
92static WORK_STATE(KILL_OBJECT, "KILL", fscache_kill_object);
93static WORK_STATE(KILL_DEPENDENTS, "KDEP", fscache_kill_dependents);
94static WORK_STATE(DROP_OBJECT, "DROP", fscache_drop_object);
95static WORK_STATE(OBJECT_DEAD, "DEAD", fscache_object_dead);
96
97static WAIT_STATE(WAIT_FOR_INIT, "?INI",
98 TRANSIT_TO(INIT_OBJECT, 1 << FSCACHE_OBJECT_EV_NEW_CHILD));
99
100static WAIT_STATE(WAIT_FOR_PARENT, "?PRN",
101 TRANSIT_TO(PARENT_READY, 1 << FSCACHE_OBJECT_EV_PARENT_READY));
102
103static WAIT_STATE(WAIT_FOR_CMD, "?CMD",
104 TRANSIT_TO(INVALIDATE_OBJECT, 1 << FSCACHE_OBJECT_EV_INVALIDATE),
105 TRANSIT_TO(UPDATE_OBJECT, 1 << FSCACHE_OBJECT_EV_UPDATE),
106 TRANSIT_TO(JUMPSTART_DEPS, 1 << FSCACHE_OBJECT_EV_NEW_CHILD));
107
108static WAIT_STATE(WAIT_FOR_CLEARANCE, "?CLR",
109 TRANSIT_TO(KILL_OBJECT, 1 << FSCACHE_OBJECT_EV_CLEARED));
110
111
112
113
114
115
116
117
118
119
120static const struct fscache_transition fscache_osm_init_oob[] = {
121 TRANSIT_TO(ABORT_INIT,
122 (1 << FSCACHE_OBJECT_EV_ERROR) |
123 (1 << FSCACHE_OBJECT_EV_KILL)),
124 { 0, NULL }
125};
126
127static const struct fscache_transition fscache_osm_lookup_oob[] = {
128 TRANSIT_TO(LOOKUP_FAILURE,
129 (1 << FSCACHE_OBJECT_EV_ERROR) |
130 (1 << FSCACHE_OBJECT_EV_KILL)),
131 { 0, NULL }
132};
133
134static const struct fscache_transition fscache_osm_run_oob[] = {
135 TRANSIT_TO(KILL_OBJECT,
136 (1 << FSCACHE_OBJECT_EV_ERROR) |
137 (1 << FSCACHE_OBJECT_EV_KILL)),
138 { 0, NULL }
139};
140
141static int fscache_get_object(struct fscache_object *,
142 enum fscache_obj_ref_trace);
143static void fscache_put_object(struct fscache_object *,
144 enum fscache_obj_ref_trace);
145static bool fscache_enqueue_dependents(struct fscache_object *, int);
146static void fscache_dequeue_object(struct fscache_object *);
147static void fscache_update_aux_data(struct fscache_object *);
148
149
150
151
152
153static inline void fscache_done_parent_op(struct fscache_object *object)
154{
155 struct fscache_object *parent = object->parent;
156
157 _enter("OBJ%x {OBJ%x,%x}",
158 object->debug_id, parent->debug_id, parent->n_ops);
159
160 spin_lock_nested(&parent->lock, 1);
161 parent->n_obj_ops--;
162 parent->n_ops--;
163 if (parent->n_ops == 0)
164 fscache_raise_event(parent, FSCACHE_OBJECT_EV_CLEARED);
165 spin_unlock(&parent->lock);
166}
167
168
169
170
171static void fscache_object_sm_dispatcher(struct fscache_object *object)
172{
173 const struct fscache_transition *t;
174 const struct fscache_state *state, *new_state;
175 unsigned long events, event_mask;
176 bool oob;
177 int event = -1;
178
179 ASSERT(object != NULL);
180
181 _enter("{OBJ%x,%s,%lx}",
182 object->debug_id, object->state->name, object->events);
183
184 event_mask = object->event_mask;
185restart:
186 object->event_mask = 0;
187 state = object->state;
188restart_masked:
189 events = object->events;
190
191
192 if (events & object->oob_event_mask) {
193 _debug("{OBJ%x} oob %lx",
194 object->debug_id, events & object->oob_event_mask);
195 oob = true;
196 for (t = object->oob_table; t->events; t++) {
197 if (events & t->events) {
198 state = t->transit_to;
199 ASSERT(state->work != NULL);
200 event = fls(events & t->events) - 1;
201 __clear_bit(event, &object->oob_event_mask);
202 clear_bit(event, &object->events);
203 goto execute_work_state;
204 }
205 }
206 }
207 oob = false;
208
209
210 if (!state->work) {
211 if (events & event_mask) {
212 for (t = state->transitions; t->events; t++) {
213 if (events & t->events) {
214 new_state = t->transit_to;
215 event = fls(events & t->events) - 1;
216 trace_fscache_osm(object, state,
217 true, false, event);
218 clear_bit(event, &object->events);
219 _debug("{OBJ%x} ev %d: %s -> %s",
220 object->debug_id, event,
221 state->name, new_state->name);
222 object->state = state = new_state;
223 goto execute_work_state;
224 }
225 }
226
227
228 BUG();
229 }
230
231 goto unmask_events;
232 }
233
234execute_work_state:
235 _debug("{OBJ%x} exec %s", object->debug_id, state->name);
236
237 trace_fscache_osm(object, state, false, oob, event);
238 new_state = state->work(object, event);
239 event = -1;
240 if (new_state == NO_TRANSIT) {
241 _debug("{OBJ%x} %s notrans", object->debug_id, state->name);
242 if (unlikely(state == STATE(OBJECT_DEAD))) {
243 _leave(" [dead]");
244 return;
245 }
246 fscache_enqueue_object(object);
247 event_mask = object->oob_event_mask;
248 goto unmask_events;
249 }
250
251 _debug("{OBJ%x} %s -> %s",
252 object->debug_id, state->name, new_state->name);
253 object->state = state = new_state;
254
255 if (state->work) {
256 if (unlikely(state == STATE(OBJECT_DEAD))) {
257 _leave(" [dead]");
258 return;
259 }
260 goto restart_masked;
261 }
262
263
264 event_mask = object->oob_event_mask;
265 for (t = state->transitions; t->events; t++)
266 event_mask |= t->events;
267
268unmask_events:
269 object->event_mask = event_mask;
270 smp_mb();
271 events = object->events;
272 if (events & event_mask)
273 goto restart;
274 _leave(" [msk %lx]", event_mask);
275}
276
277
278
279
280static void fscache_object_work_func(struct work_struct *work)
281{
282 struct fscache_object *object =
283 container_of(work, struct fscache_object, work);
284 unsigned long start;
285
286 _enter("{OBJ%x}", object->debug_id);
287
288 start = jiffies;
289 fscache_object_sm_dispatcher(object);
290 fscache_hist(fscache_objs_histogram, start);
291 fscache_put_object(object, fscache_obj_put_work);
292}
293
294
295
296
297
298
299
300
301
302
303
304
305void fscache_object_init(struct fscache_object *object,
306 struct fscache_cookie *cookie,
307 struct fscache_cache *cache)
308{
309 const struct fscache_transition *t;
310
311 atomic_inc(&cache->object_count);
312
313 object->state = STATE(WAIT_FOR_INIT);
314 object->oob_table = fscache_osm_init_oob;
315 object->flags = 1 << FSCACHE_OBJECT_IS_LIVE;
316 spin_lock_init(&object->lock);
317 INIT_LIST_HEAD(&object->cache_link);
318 INIT_HLIST_NODE(&object->cookie_link);
319 INIT_WORK(&object->work, fscache_object_work_func);
320 INIT_LIST_HEAD(&object->dependents);
321 INIT_LIST_HEAD(&object->dep_link);
322 INIT_LIST_HEAD(&object->pending_ops);
323 object->n_children = 0;
324 object->n_ops = object->n_in_progress = object->n_exclusive = 0;
325 object->events = 0;
326 object->store_limit = 0;
327 object->store_limit_l = 0;
328 object->cache = cache;
329 object->cookie = cookie;
330 fscache_cookie_get(cookie, fscache_cookie_get_attach_object);
331 object->parent = NULL;
332#ifdef CONFIG_FSCACHE_OBJECT_LIST
333 RB_CLEAR_NODE(&object->objlist_link);
334#endif
335
336 object->oob_event_mask = 0;
337 for (t = object->oob_table; t->events; t++)
338 object->oob_event_mask |= t->events;
339 object->event_mask = object->oob_event_mask;
340 for (t = object->state->transitions; t->events; t++)
341 object->event_mask |= t->events;
342}
343EXPORT_SYMBOL(fscache_object_init);
344
345
346
347
348
349static inline void fscache_mark_object_dead(struct fscache_object *object)
350{
351 spin_lock(&object->lock);
352 clear_bit(FSCACHE_OBJECT_IS_LIVE, &object->flags);
353 spin_unlock(&object->lock);
354}
355
356
357
358
359static const struct fscache_state *fscache_abort_initialisation(struct fscache_object *object,
360 int event)
361{
362 _enter("{OBJ%x},%d", object->debug_id, event);
363
364 object->oob_event_mask = 0;
365 fscache_dequeue_object(object);
366 return transit_to(KILL_OBJECT);
367}
368
369
370
371
372
373
374
375
376static const struct fscache_state *fscache_initialise_object(struct fscache_object *object,
377 int event)
378{
379 struct fscache_object *parent;
380 bool success;
381
382 _enter("{OBJ%x},%d", object->debug_id, event);
383
384 ASSERT(list_empty(&object->dep_link));
385
386 parent = object->parent;
387 if (!parent) {
388 _leave(" [no parent]");
389 return transit_to(DROP_OBJECT);
390 }
391
392 _debug("parent: %s of:%lx", parent->state->name, parent->flags);
393
394 if (fscache_object_is_dying(parent)) {
395 _leave(" [bad parent]");
396 return transit_to(DROP_OBJECT);
397 }
398
399 if (fscache_object_is_available(parent)) {
400 _leave(" [ready]");
401 return transit_to(PARENT_READY);
402 }
403
404 _debug("wait");
405
406 spin_lock(&parent->lock);
407 fscache_stat(&fscache_n_cop_grab_object);
408 success = false;
409 if (fscache_object_is_live(parent) &&
410 object->cache->ops->grab_object(object, fscache_obj_get_add_to_deps)) {
411 list_add(&object->dep_link, &parent->dependents);
412 success = true;
413 }
414 fscache_stat_d(&fscache_n_cop_grab_object);
415 spin_unlock(&parent->lock);
416 if (!success) {
417 _leave(" [grab failed]");
418 return transit_to(DROP_OBJECT);
419 }
420
421
422
423 fscache_raise_event(parent, FSCACHE_OBJECT_EV_NEW_CHILD);
424 _leave(" [wait]");
425 return transit_to(WAIT_FOR_PARENT);
426}
427
428
429
430
431static const struct fscache_state *fscache_parent_ready(struct fscache_object *object,
432 int event)
433{
434 struct fscache_object *parent = object->parent;
435
436 _enter("{OBJ%x},%d", object->debug_id, event);
437
438 ASSERT(parent != NULL);
439
440 spin_lock(&parent->lock);
441 parent->n_ops++;
442 parent->n_obj_ops++;
443 object->lookup_jif = jiffies;
444 spin_unlock(&parent->lock);
445
446 _leave("");
447 return transit_to(LOOK_UP_OBJECT);
448}
449
450
451
452
453
454
455static const struct fscache_state *fscache_look_up_object(struct fscache_object *object,
456 int event)
457{
458 struct fscache_cookie *cookie = object->cookie;
459 struct fscache_object *parent = object->parent;
460 int ret;
461
462 _enter("{OBJ%x},%d", object->debug_id, event);
463
464 object->oob_table = fscache_osm_lookup_oob;
465
466 ASSERT(parent != NULL);
467 ASSERTCMP(parent->n_ops, >, 0);
468 ASSERTCMP(parent->n_obj_ops, >, 0);
469
470
471 ASSERT(fscache_object_is_available(parent));
472
473 if (fscache_object_is_dying(parent) ||
474 test_bit(FSCACHE_IOERROR, &object->cache->flags) ||
475 !fscache_use_cookie(object)) {
476 _leave(" [unavailable]");
477 return transit_to(LOOKUP_FAILURE);
478 }
479
480 _debug("LOOKUP \"%s\" in \"%s\"",
481 cookie->def->name, object->cache->tag->name);
482
483 fscache_stat(&fscache_n_object_lookups);
484 fscache_stat(&fscache_n_cop_lookup_object);
485 ret = object->cache->ops->lookup_object(object);
486 fscache_stat_d(&fscache_n_cop_lookup_object);
487
488 fscache_unuse_cookie(object);
489
490 if (ret == -ETIMEDOUT) {
491
492
493 fscache_stat(&fscache_n_object_lookups_timed_out);
494 _leave(" [timeout]");
495 return NO_TRANSIT;
496 }
497
498 if (ret < 0) {
499 _leave(" [error]");
500 return transit_to(LOOKUP_FAILURE);
501 }
502
503 _leave(" [ok]");
504 return transit_to(OBJECT_AVAILABLE);
505}
506
507
508
509
510
511
512
513
514void fscache_object_lookup_negative(struct fscache_object *object)
515{
516 struct fscache_cookie *cookie = object->cookie;
517
518 _enter("{OBJ%x,%s}", object->debug_id, object->state->name);
519
520 if (!test_and_set_bit(FSCACHE_OBJECT_IS_LOOKED_UP, &object->flags)) {
521 fscache_stat(&fscache_n_object_lookups_negative);
522
523
524
525
526 set_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
527 clear_bit(FSCACHE_COOKIE_UNAVAILABLE, &cookie->flags);
528
529 _debug("wake up lookup %p", &cookie->flags);
530 clear_bit_unlock(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags);
531 wake_up_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP);
532 }
533 _leave("");
534}
535EXPORT_SYMBOL(fscache_object_lookup_negative);
536
537
538
539
540
541
542
543
544
545
546
547void fscache_obtained_object(struct fscache_object *object)
548{
549 struct fscache_cookie *cookie = object->cookie;
550
551 _enter("{OBJ%x,%s}", object->debug_id, object->state->name);
552
553
554
555 if (!test_and_set_bit(FSCACHE_OBJECT_IS_LOOKED_UP, &object->flags)) {
556 fscache_stat(&fscache_n_object_lookups_positive);
557
558
559 clear_bit_unlock(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
560 clear_bit(FSCACHE_COOKIE_UNAVAILABLE, &cookie->flags);
561
562
563
564
565 clear_bit_unlock(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags);
566 wake_up_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP);
567 } else {
568 fscache_stat(&fscache_n_object_created);
569 }
570
571 set_bit(FSCACHE_OBJECT_IS_AVAILABLE, &object->flags);
572 _leave("");
573}
574EXPORT_SYMBOL(fscache_obtained_object);
575
576
577
578
579static const struct fscache_state *fscache_object_available(struct fscache_object *object,
580 int event)
581{
582 _enter("{OBJ%x},%d", object->debug_id, event);
583
584 object->oob_table = fscache_osm_run_oob;
585
586 spin_lock(&object->lock);
587
588 fscache_done_parent_op(object);
589 if (object->n_in_progress == 0) {
590 if (object->n_ops > 0) {
591 ASSERTCMP(object->n_ops, >=, object->n_obj_ops);
592 fscache_start_operations(object);
593 } else {
594 ASSERT(list_empty(&object->pending_ops));
595 }
596 }
597 spin_unlock(&object->lock);
598
599 fscache_stat(&fscache_n_cop_lookup_complete);
600 object->cache->ops->lookup_complete(object);
601 fscache_stat_d(&fscache_n_cop_lookup_complete);
602
603 fscache_hist(fscache_obj_instantiate_histogram, object->lookup_jif);
604 fscache_stat(&fscache_n_object_avail);
605
606 _leave("");
607 return transit_to(JUMPSTART_DEPS);
608}
609
610
611
612
613static const struct fscache_state *fscache_jumpstart_dependents(struct fscache_object *object,
614 int event)
615{
616 _enter("{OBJ%x},%d", object->debug_id, event);
617
618 if (!fscache_enqueue_dependents(object, FSCACHE_OBJECT_EV_PARENT_READY))
619 return NO_TRANSIT;
620 return transit_to(WAIT_FOR_CMD);
621}
622
623
624
625
626static const struct fscache_state *fscache_lookup_failure(struct fscache_object *object,
627 int event)
628{
629 struct fscache_cookie *cookie;
630
631 _enter("{OBJ%x},%d", object->debug_id, event);
632
633 object->oob_event_mask = 0;
634
635 fscache_stat(&fscache_n_cop_lookup_complete);
636 object->cache->ops->lookup_complete(object);
637 fscache_stat_d(&fscache_n_cop_lookup_complete);
638
639 set_bit(FSCACHE_OBJECT_KILLED_BY_CACHE, &object->flags);
640
641 cookie = object->cookie;
642 set_bit(FSCACHE_COOKIE_UNAVAILABLE, &cookie->flags);
643 if (test_and_clear_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags))
644 wake_up_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP);
645
646 fscache_done_parent_op(object);
647 return transit_to(KILL_OBJECT);
648}
649
650
651
652
653
654static const struct fscache_state *fscache_kill_object(struct fscache_object *object,
655 int event)
656{
657 _enter("{OBJ%x,%d,%d},%d",
658 object->debug_id, object->n_ops, object->n_children, event);
659
660 fscache_mark_object_dead(object);
661 object->oob_event_mask = 0;
662
663 if (test_bit(FSCACHE_OBJECT_RETIRED, &object->flags)) {
664
665 clear_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags);
666 fscache_cancel_all_ops(object);
667 }
668
669 if (list_empty(&object->dependents) &&
670 object->n_ops == 0 &&
671 object->n_children == 0)
672 return transit_to(DROP_OBJECT);
673
674 if (object->n_in_progress == 0) {
675 spin_lock(&object->lock);
676 if (object->n_ops > 0 && object->n_in_progress == 0)
677 fscache_start_operations(object);
678 spin_unlock(&object->lock);
679 }
680
681 if (!list_empty(&object->dependents))
682 return transit_to(KILL_DEPENDENTS);
683
684 return transit_to(WAIT_FOR_CLEARANCE);
685}
686
687
688
689
690static const struct fscache_state *fscache_kill_dependents(struct fscache_object *object,
691 int event)
692{
693 _enter("{OBJ%x},%d", object->debug_id, event);
694
695 if (!fscache_enqueue_dependents(object, FSCACHE_OBJECT_EV_KILL))
696 return NO_TRANSIT;
697 return transit_to(WAIT_FOR_CLEARANCE);
698}
699
700
701
702
703static const struct fscache_state *fscache_drop_object(struct fscache_object *object,
704 int event)
705{
706 struct fscache_object *parent = object->parent;
707 struct fscache_cookie *cookie = object->cookie;
708 struct fscache_cache *cache = object->cache;
709 bool awaken = false;
710
711 _enter("{OBJ%x,%d},%d", object->debug_id, object->n_children, event);
712
713 ASSERT(cookie != NULL);
714 ASSERT(!hlist_unhashed(&object->cookie_link));
715
716 if (test_bit(FSCACHE_COOKIE_AUX_UPDATED, &cookie->flags)) {
717 _debug("final update");
718 fscache_update_aux_data(object);
719 }
720
721
722
723
724 spin_lock(&cookie->lock);
725 hlist_del_init(&object->cookie_link);
726 if (hlist_empty(&cookie->backing_objects) &&
727 test_and_clear_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags))
728 awaken = true;
729 spin_unlock(&cookie->lock);
730
731 if (awaken)
732 wake_up_bit(&cookie->flags, FSCACHE_COOKIE_INVALIDATING);
733 if (test_and_clear_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags))
734 wake_up_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP);
735
736
737
738
739
740 spin_lock(&object->lock);
741 spin_unlock(&object->lock);
742
743
744 spin_lock(&cache->object_list_lock);
745 list_del_init(&object->cache_link);
746 spin_unlock(&cache->object_list_lock);
747
748 fscache_stat(&fscache_n_cop_drop_object);
749 cache->ops->drop_object(object);
750 fscache_stat_d(&fscache_n_cop_drop_object);
751
752
753 if (parent) {
754 _debug("release parent OBJ%x {%d}",
755 parent->debug_id, parent->n_children);
756
757 spin_lock(&parent->lock);
758 parent->n_children--;
759 if (parent->n_children == 0)
760 fscache_raise_event(parent, FSCACHE_OBJECT_EV_CLEARED);
761 spin_unlock(&parent->lock);
762 object->parent = NULL;
763 }
764
765
766 fscache_put_object(object, fscache_obj_put_drop_obj);
767 fscache_stat(&fscache_n_object_dead);
768
769 _leave("");
770 return transit_to(OBJECT_DEAD);
771}
772
773
774
775
776static int fscache_get_object(struct fscache_object *object,
777 enum fscache_obj_ref_trace why)
778{
779 int ret;
780
781 fscache_stat(&fscache_n_cop_grab_object);
782 ret = object->cache->ops->grab_object(object, why) ? 0 : -EAGAIN;
783 fscache_stat_d(&fscache_n_cop_grab_object);
784 return ret;
785}
786
787
788
789
790static void fscache_put_object(struct fscache_object *object,
791 enum fscache_obj_ref_trace why)
792{
793 fscache_stat(&fscache_n_cop_put_object);
794 object->cache->ops->put_object(object, why);
795 fscache_stat_d(&fscache_n_cop_put_object);
796}
797
798
799
800
801
802
803
804void fscache_object_destroy(struct fscache_object *object)
805{
806 fscache_objlist_remove(object);
807
808
809 fscache_cookie_put(object->cookie, fscache_cookie_put_object);
810 object->cookie = NULL;
811}
812EXPORT_SYMBOL(fscache_object_destroy);
813
814
815
816
817void fscache_enqueue_object(struct fscache_object *object)
818{
819 _enter("{OBJ%x}", object->debug_id);
820
821 if (fscache_get_object(object, fscache_obj_get_queue) >= 0) {
822 wait_queue_head_t *cong_wq =
823 &get_cpu_var(fscache_object_cong_wait);
824
825 if (queue_work(fscache_object_wq, &object->work)) {
826 if (fscache_object_congested())
827 wake_up(cong_wq);
828 } else
829 fscache_put_object(object, fscache_obj_put_queue);
830
831 put_cpu_var(fscache_object_cong_wait);
832 }
833}
834
835
836
837
838
839
840
841
842
843
844
845
846
847bool fscache_object_sleep_till_congested(signed long *timeoutp)
848{
849 wait_queue_head_t *cong_wq = this_cpu_ptr(&fscache_object_cong_wait);
850 DEFINE_WAIT(wait);
851
852 if (fscache_object_congested())
853 return true;
854
855 add_wait_queue_exclusive(cong_wq, &wait);
856 if (!fscache_object_congested())
857 *timeoutp = schedule_timeout(*timeoutp);
858 finish_wait(cong_wq, &wait);
859
860 return fscache_object_congested();
861}
862EXPORT_SYMBOL_GPL(fscache_object_sleep_till_congested);
863
864
865
866
867
868
869
870
871static bool fscache_enqueue_dependents(struct fscache_object *object, int event)
872{
873 struct fscache_object *dep;
874 bool ret = true;
875
876 _enter("{OBJ%x}", object->debug_id);
877
878 if (list_empty(&object->dependents))
879 return true;
880
881 spin_lock(&object->lock);
882
883 while (!list_empty(&object->dependents)) {
884 dep = list_entry(object->dependents.next,
885 struct fscache_object, dep_link);
886 list_del_init(&dep->dep_link);
887
888 fscache_raise_event(dep, event);
889 fscache_put_object(dep, fscache_obj_put_enq_dep);
890
891 if (!list_empty(&object->dependents) && need_resched()) {
892 ret = false;
893 break;
894 }
895 }
896
897 spin_unlock(&object->lock);
898 return ret;
899}
900
901
902
903
904static void fscache_dequeue_object(struct fscache_object *object)
905{
906 _enter("{OBJ%x}", object->debug_id);
907
908 if (!list_empty(&object->dep_link)) {
909 spin_lock(&object->parent->lock);
910 list_del_init(&object->dep_link);
911 spin_unlock(&object->parent->lock);
912 }
913
914 _leave("");
915}
916
917
918
919
920
921
922
923
924
925
926
927
928enum fscache_checkaux fscache_check_aux(struct fscache_object *object,
929 const void *data, uint16_t datalen,
930 loff_t object_size)
931{
932 enum fscache_checkaux result;
933
934 if (!object->cookie->def->check_aux) {
935 fscache_stat(&fscache_n_checkaux_none);
936 return FSCACHE_CHECKAUX_OKAY;
937 }
938
939 result = object->cookie->def->check_aux(object->cookie->netfs_data,
940 data, datalen, object_size);
941 switch (result) {
942
943 case FSCACHE_CHECKAUX_OKAY:
944 fscache_stat(&fscache_n_checkaux_okay);
945 break;
946
947
948 case FSCACHE_CHECKAUX_NEEDS_UPDATE:
949 fscache_stat(&fscache_n_checkaux_update);
950 break;
951
952
953 case FSCACHE_CHECKAUX_OBSOLETE:
954 fscache_stat(&fscache_n_checkaux_obsolete);
955 break;
956
957 default:
958 BUG();
959 }
960
961 return result;
962}
963EXPORT_SYMBOL(fscache_check_aux);
964
965
966
967
968static const struct fscache_state *_fscache_invalidate_object(struct fscache_object *object,
969 int event)
970{
971 struct fscache_operation *op;
972 struct fscache_cookie *cookie = object->cookie;
973
974 _enter("{OBJ%x},%d", object->debug_id, event);
975
976
977
978
979 if (!fscache_use_cookie(object)) {
980 ASSERT(radix_tree_empty(&object->cookie->stores));
981 set_bit(FSCACHE_OBJECT_RETIRED, &object->flags);
982 _leave(" [no cookie]");
983 return transit_to(KILL_OBJECT);
984 }
985
986
987 fscache_invalidate_writes(cookie);
988 clear_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags);
989 fscache_cancel_all_ops(object);
990
991
992 op = kzalloc(sizeof(*op), GFP_KERNEL);
993 if (!op)
994 goto nomem;
995
996 fscache_operation_init(cookie, op, object->cache->ops->invalidate_object,
997 NULL, NULL);
998 op->flags = FSCACHE_OP_ASYNC |
999 (1 << FSCACHE_OP_EXCLUSIVE) |
1000 (1 << FSCACHE_OP_UNUSE_COOKIE);
1001 trace_fscache_page_op(cookie, NULL, op, fscache_page_op_invalidate);
1002
1003 spin_lock(&cookie->lock);
1004 if (fscache_submit_exclusive_op(object, op) < 0)
1005 goto submit_op_failed;
1006 spin_unlock(&cookie->lock);
1007 fscache_put_operation(op);
1008
1009
1010
1011
1012
1013 set_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
1014
1015
1016
1017
1018 if (test_and_clear_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags))
1019 wake_up_bit(&cookie->flags, FSCACHE_COOKIE_INVALIDATING);
1020 _leave(" [ok]");
1021 return transit_to(UPDATE_OBJECT);
1022
1023nomem:
1024 fscache_mark_object_dead(object);
1025 fscache_unuse_cookie(object);
1026 _leave(" [ENOMEM]");
1027 return transit_to(KILL_OBJECT);
1028
1029submit_op_failed:
1030 fscache_mark_object_dead(object);
1031 spin_unlock(&cookie->lock);
1032 fscache_unuse_cookie(object);
1033 kfree(op);
1034 _leave(" [EIO]");
1035 return transit_to(KILL_OBJECT);
1036}
1037
1038static const struct fscache_state *fscache_invalidate_object(struct fscache_object *object,
1039 int event)
1040{
1041 const struct fscache_state *s;
1042
1043 fscache_stat(&fscache_n_invalidates_run);
1044 fscache_stat(&fscache_n_cop_invalidate_object);
1045 s = _fscache_invalidate_object(object, event);
1046 fscache_stat_d(&fscache_n_cop_invalidate_object);
1047 return s;
1048}
1049
1050
1051
1052
1053static void fscache_update_aux_data(struct fscache_object *object)
1054{
1055 fscache_stat(&fscache_n_updates_run);
1056 fscache_stat(&fscache_n_cop_update_object);
1057 object->cache->ops->update_object(object);
1058 fscache_stat_d(&fscache_n_cop_update_object);
1059}
1060
1061
1062
1063
1064static const struct fscache_state *fscache_update_object(struct fscache_object *object,
1065 int event)
1066{
1067 _enter("{OBJ%x},%d", object->debug_id, event);
1068
1069 fscache_update_aux_data(object);
1070
1071 _leave("");
1072 return transit_to(WAIT_FOR_CMD);
1073}
1074
1075
1076
1077
1078
1079
1080
1081
1082void fscache_object_retrying_stale(struct fscache_object *object)
1083{
1084 fscache_stat(&fscache_n_cache_no_space_reject);
1085}
1086EXPORT_SYMBOL(fscache_object_retrying_stale);
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096void fscache_object_mark_killed(struct fscache_object *object,
1097 enum fscache_why_object_killed why)
1098{
1099 if (test_and_set_bit(FSCACHE_OBJECT_KILLED_BY_CACHE, &object->flags)) {
1100 pr_err("Error: Object already killed by cache [%s]\n",
1101 object->cache->identifier);
1102 return;
1103 }
1104
1105 switch (why) {
1106 case FSCACHE_OBJECT_NO_SPACE:
1107 fscache_stat(&fscache_n_cache_no_space_reject);
1108 break;
1109 case FSCACHE_OBJECT_IS_STALE:
1110 fscache_stat(&fscache_n_cache_stale_objects);
1111 break;
1112 case FSCACHE_OBJECT_WAS_RETIRED:
1113 fscache_stat(&fscache_n_cache_retired_objects);
1114 break;
1115 case FSCACHE_OBJECT_WAS_CULLED:
1116 fscache_stat(&fscache_n_cache_culled_objects);
1117 break;
1118 }
1119}
1120EXPORT_SYMBOL(fscache_object_mark_killed);
1121
1122
1123
1124
1125
1126
1127
1128static const struct fscache_state *fscache_object_dead(struct fscache_object *object,
1129 int event)
1130{
1131 if (!test_and_set_bit(FSCACHE_OBJECT_RUN_AFTER_DEAD,
1132 &object->flags))
1133 return NO_TRANSIT;
1134
1135 WARN(true, "FS-Cache object redispatched after death");
1136 return NO_TRANSIT;
1137}
1138