20bc3341f11311d83b468be21fa5baf46cc8c888
[linux-2.6-block.git] / fs / fscache / cookie.c
1 /* netfs cookie management
2  *
3  * Copyright (C) 2004-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/netfs-api.txt for more information on
12  * the netfs API.
13  */
14
15 #define FSCACHE_DEBUG_LEVEL COOKIE
16 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include "internal.h"
19
20 struct kmem_cache *fscache_cookie_jar;
21
22 static atomic_t fscache_object_debug_id = ATOMIC_INIT(0);
23
24 static int fscache_acquire_non_index_cookie(struct fscache_cookie *cookie);
25 static int fscache_alloc_object(struct fscache_cache *cache,
26                                 struct fscache_cookie *cookie);
27 static int fscache_attach_object(struct fscache_cookie *cookie,
28                                  struct fscache_object *object);
29
30 /*
31  * initialise an cookie jar slab element prior to any use
32  */
33 void fscache_cookie_init_once(void *_cookie)
34 {
35         struct fscache_cookie *cookie = _cookie;
36
37         memset(cookie, 0, sizeof(*cookie));
38         spin_lock_init(&cookie->lock);
39         spin_lock_init(&cookie->stores_lock);
40         INIT_HLIST_HEAD(&cookie->backing_objects);
41 }
42
43 /*
44  * request a cookie to represent an object (index, datafile, xattr, etc)
45  * - parent specifies the parent object
46  *   - the top level index cookie for each netfs is stored in the fscache_netfs
47  *     struct upon registration
48  * - def points to the definition
49  * - the netfs_data will be passed to the functions pointed to in *def
50  * - all attached caches will be searched to see if they contain this object
51  * - index objects aren't stored on disk until there's a dependent file that
52  *   needs storing
53  * - other objects are stored in a selected cache immediately, and all the
54  *   indices forming the path to it are instantiated if necessary
55  * - we never let on to the netfs about errors
56  *   - we may set a negative cookie pointer, but that's okay
57  */
58 struct fscache_cookie *__fscache_acquire_cookie(
59         struct fscache_cookie *parent,
60         const struct fscache_cookie_def *def,
61         void *netfs_data,
62         bool enable)
63 {
64         struct fscache_cookie *cookie;
65
66         BUG_ON(!def);
67
68         _enter("{%s},{%s},%p,%u",
69                parent ? (char *) parent->def->name : "<no-parent>",
70                def->name, netfs_data, enable);
71
72         fscache_stat(&fscache_n_acquires);
73
74         /* if there's no parent cookie, then we don't create one here either */
75         if (!parent) {
76                 fscache_stat(&fscache_n_acquires_null);
77                 _leave(" [no parent]");
78                 return NULL;
79         }
80
81         /* validate the definition */
82         BUG_ON(!def->get_key);
83         BUG_ON(!def->name[0]);
84
85         BUG_ON(def->type == FSCACHE_COOKIE_TYPE_INDEX &&
86                parent->def->type != FSCACHE_COOKIE_TYPE_INDEX);
87
88         /* allocate and initialise a cookie */
89         cookie = kmem_cache_alloc(fscache_cookie_jar, GFP_KERNEL);
90         if (!cookie) {
91                 fscache_stat(&fscache_n_acquires_oom);
92                 _leave(" [ENOMEM]");
93                 return NULL;
94         }
95
96         atomic_set(&cookie->usage, 1);
97         atomic_set(&cookie->n_children, 0);
98
99         /* We keep the active count elevated until relinquishment to prevent an
100          * attempt to wake up every time the object operations queue quiesces.
101          */
102         atomic_set(&cookie->n_active, 1);
103
104         fscache_cookie_get(parent, fscache_cookie_get_acquire_parent);
105         atomic_inc(&parent->n_children);
106
107         cookie->def             = def;
108         cookie->parent          = parent;
109         cookie->netfs_data      = netfs_data;
110         cookie->flags           = (1 << FSCACHE_COOKIE_NO_DATA_YET);
111
112         /* radix tree insertion won't use the preallocation pool unless it's
113          * told it may not wait */
114         INIT_RADIX_TREE(&cookie->stores, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
115
116         switch (cookie->def->type) {
117         case FSCACHE_COOKIE_TYPE_INDEX:
118                 fscache_stat(&fscache_n_cookie_index);
119                 break;
120         case FSCACHE_COOKIE_TYPE_DATAFILE:
121                 fscache_stat(&fscache_n_cookie_data);
122                 break;
123         default:
124                 fscache_stat(&fscache_n_cookie_special);
125                 break;
126         }
127
128         trace_fscache_acquire(cookie);
129
130         if (enable) {
131                 /* if the object is an index then we need do nothing more here
132                  * - we create indices on disk when we need them as an index
133                  * may exist in multiple caches */
134                 if (cookie->def->type != FSCACHE_COOKIE_TYPE_INDEX) {
135                         if (fscache_acquire_non_index_cookie(cookie) == 0) {
136                                 set_bit(FSCACHE_COOKIE_ENABLED, &cookie->flags);
137                         } else {
138                                 atomic_dec(&parent->n_children);
139                                 fscache_cookie_put(cookie,
140                                                    fscache_cookie_put_acquire_nobufs);
141                                 fscache_stat(&fscache_n_acquires_nobufs);
142                                 _leave(" = NULL");
143                                 return NULL;
144                         }
145                 } else {
146                         set_bit(FSCACHE_COOKIE_ENABLED, &cookie->flags);
147                 }
148         }
149
150         fscache_stat(&fscache_n_acquires_ok);
151         _leave(" = %p", cookie);
152         return cookie;
153 }
154 EXPORT_SYMBOL(__fscache_acquire_cookie);
155
156 /*
157  * Enable a cookie to permit it to accept new operations.
158  */
159 void __fscache_enable_cookie(struct fscache_cookie *cookie,
160                              bool (*can_enable)(void *data),
161                              void *data)
162 {
163         _enter("%p", cookie);
164
165         trace_fscache_enable(cookie);
166
167         wait_on_bit_lock(&cookie->flags, FSCACHE_COOKIE_ENABLEMENT_LOCK,
168                          TASK_UNINTERRUPTIBLE);
169
170         if (test_bit(FSCACHE_COOKIE_ENABLED, &cookie->flags))
171                 goto out_unlock;
172
173         if (can_enable && !can_enable(data)) {
174                 /* The netfs decided it didn't want to enable after all */
175         } else if (cookie->def->type != FSCACHE_COOKIE_TYPE_INDEX) {
176                 /* Wait for outstanding disablement to complete */
177                 __fscache_wait_on_invalidate(cookie);
178
179                 if (fscache_acquire_non_index_cookie(cookie) == 0)
180                         set_bit(FSCACHE_COOKIE_ENABLED, &cookie->flags);
181         } else {
182                 set_bit(FSCACHE_COOKIE_ENABLED, &cookie->flags);
183         }
184
185 out_unlock:
186         clear_bit_unlock(FSCACHE_COOKIE_ENABLEMENT_LOCK, &cookie->flags);
187         wake_up_bit(&cookie->flags, FSCACHE_COOKIE_ENABLEMENT_LOCK);
188 }
189 EXPORT_SYMBOL(__fscache_enable_cookie);
190
191 /*
192  * acquire a non-index cookie
193  * - this must make sure the index chain is instantiated and instantiate the
194  *   object representation too
195  */
196 static int fscache_acquire_non_index_cookie(struct fscache_cookie *cookie)
197 {
198         struct fscache_object *object;
199         struct fscache_cache *cache;
200         uint64_t i_size;
201         int ret;
202
203         _enter("");
204
205         set_bit(FSCACHE_COOKIE_UNAVAILABLE, &cookie->flags);
206
207         /* now we need to see whether the backing objects for this cookie yet
208          * exist, if not there'll be nothing to search */
209         down_read(&fscache_addremove_sem);
210
211         if (list_empty(&fscache_cache_list)) {
212                 up_read(&fscache_addremove_sem);
213                 _leave(" = 0 [no caches]");
214                 return 0;
215         }
216
217         /* select a cache in which to store the object */
218         cache = fscache_select_cache_for_object(cookie->parent);
219         if (!cache) {
220                 up_read(&fscache_addremove_sem);
221                 fscache_stat(&fscache_n_acquires_no_cache);
222                 _leave(" = -ENOMEDIUM [no cache]");
223                 return -ENOMEDIUM;
224         }
225
226         _debug("cache %s", cache->tag->name);
227
228         set_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags);
229
230         /* ask the cache to allocate objects for this cookie and its parent
231          * chain */
232         ret = fscache_alloc_object(cache, cookie);
233         if (ret < 0) {
234                 up_read(&fscache_addremove_sem);
235                 _leave(" = %d", ret);
236                 return ret;
237         }
238
239         /* pass on how big the object we're caching is supposed to be */
240         cookie->def->get_attr(cookie->netfs_data, &i_size);
241
242         spin_lock(&cookie->lock);
243         if (hlist_empty(&cookie->backing_objects)) {
244                 spin_unlock(&cookie->lock);
245                 goto unavailable;
246         }
247
248         object = hlist_entry(cookie->backing_objects.first,
249                              struct fscache_object, cookie_link);
250
251         fscache_set_store_limit(object, i_size);
252
253         /* initiate the process of looking up all the objects in the chain
254          * (done by fscache_initialise_object()) */
255         fscache_raise_event(object, FSCACHE_OBJECT_EV_NEW_CHILD);
256
257         spin_unlock(&cookie->lock);
258
259         /* we may be required to wait for lookup to complete at this point */
260         if (!fscache_defer_lookup) {
261                 _debug("non-deferred lookup %p", &cookie->flags);
262                 wait_on_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP,
263                             TASK_UNINTERRUPTIBLE);
264                 _debug("complete");
265                 if (test_bit(FSCACHE_COOKIE_UNAVAILABLE, &cookie->flags))
266                         goto unavailable;
267         }
268
269         up_read(&fscache_addremove_sem);
270         _leave(" = 0 [deferred]");
271         return 0;
272
273 unavailable:
274         up_read(&fscache_addremove_sem);
275         _leave(" = -ENOBUFS");
276         return -ENOBUFS;
277 }
278
279 /*
280  * recursively allocate cache object records for a cookie/cache combination
281  * - caller must be holding the addremove sem
282  */
283 static int fscache_alloc_object(struct fscache_cache *cache,
284                                 struct fscache_cookie *cookie)
285 {
286         struct fscache_object *object;
287         int ret;
288
289         _enter("%p,%p{%s}", cache, cookie, cookie->def->name);
290
291         spin_lock(&cookie->lock);
292         hlist_for_each_entry(object, &cookie->backing_objects,
293                              cookie_link) {
294                 if (object->cache == cache)
295                         goto object_already_extant;
296         }
297         spin_unlock(&cookie->lock);
298
299         /* ask the cache to allocate an object (we may end up with duplicate
300          * objects at this stage, but we sort that out later) */
301         fscache_stat(&fscache_n_cop_alloc_object);
302         object = cache->ops->alloc_object(cache, cookie);
303         fscache_stat_d(&fscache_n_cop_alloc_object);
304         if (IS_ERR(object)) {
305                 fscache_stat(&fscache_n_object_no_alloc);
306                 ret = PTR_ERR(object);
307                 goto error;
308         }
309
310         fscache_stat(&fscache_n_object_alloc);
311
312         object->debug_id = atomic_inc_return(&fscache_object_debug_id);
313
314         _debug("ALLOC OBJ%x: %s {%lx}",
315                object->debug_id, cookie->def->name, object->events);
316
317         ret = fscache_alloc_object(cache, cookie->parent);
318         if (ret < 0)
319                 goto error_put;
320
321         /* only attach if we managed to allocate all we needed, otherwise
322          * discard the object we just allocated and instead use the one
323          * attached to the cookie */
324         if (fscache_attach_object(cookie, object) < 0) {
325                 fscache_stat(&fscache_n_cop_put_object);
326                 cache->ops->put_object(object, fscache_obj_put_attach_fail);
327                 fscache_stat_d(&fscache_n_cop_put_object);
328         }
329
330         _leave(" = 0");
331         return 0;
332
333 object_already_extant:
334         ret = -ENOBUFS;
335         if (fscache_object_is_dying(object) ||
336             fscache_cache_is_broken(object)) {
337                 spin_unlock(&cookie->lock);
338                 goto error;
339         }
340         spin_unlock(&cookie->lock);
341         _leave(" = 0 [found]");
342         return 0;
343
344 error_put:
345         fscache_stat(&fscache_n_cop_put_object);
346         cache->ops->put_object(object, fscache_obj_put_alloc_fail);
347         fscache_stat_d(&fscache_n_cop_put_object);
348 error:
349         _leave(" = %d", ret);
350         return ret;
351 }
352
353 /*
354  * attach a cache object to a cookie
355  */
356 static int fscache_attach_object(struct fscache_cookie *cookie,
357                                  struct fscache_object *object)
358 {
359         struct fscache_object *p;
360         struct fscache_cache *cache = object->cache;
361         int ret;
362
363         _enter("{%s},{OBJ%x}", cookie->def->name, object->debug_id);
364
365         spin_lock(&cookie->lock);
366
367         /* there may be multiple initial creations of this object, but we only
368          * want one */
369         ret = -EEXIST;
370         hlist_for_each_entry(p, &cookie->backing_objects, cookie_link) {
371                 if (p->cache == object->cache) {
372                         if (fscache_object_is_dying(p))
373                                 ret = -ENOBUFS;
374                         goto cant_attach_object;
375                 }
376         }
377
378         /* pin the parent object */
379         spin_lock_nested(&cookie->parent->lock, 1);
380         hlist_for_each_entry(p, &cookie->parent->backing_objects,
381                              cookie_link) {
382                 if (p->cache == object->cache) {
383                         if (fscache_object_is_dying(p)) {
384                                 ret = -ENOBUFS;
385                                 spin_unlock(&cookie->parent->lock);
386                                 goto cant_attach_object;
387                         }
388                         object->parent = p;
389                         spin_lock(&p->lock);
390                         p->n_children++;
391                         spin_unlock(&p->lock);
392                         break;
393                 }
394         }
395         spin_unlock(&cookie->parent->lock);
396
397         /* attach to the cache's object list */
398         if (list_empty(&object->cache_link)) {
399                 spin_lock(&cache->object_list_lock);
400                 list_add(&object->cache_link, &cache->object_list);
401                 spin_unlock(&cache->object_list_lock);
402         }
403
404         /* attach to the cookie */
405         object->cookie = cookie;
406         fscache_cookie_get(cookie, fscache_cookie_get_attach_object);
407         hlist_add_head(&object->cookie_link, &cookie->backing_objects);
408
409         fscache_objlist_add(object);
410         ret = 0;
411
412 cant_attach_object:
413         spin_unlock(&cookie->lock);
414         _leave(" = %d", ret);
415         return ret;
416 }
417
418 /*
419  * Invalidate an object.  Callable with spinlocks held.
420  */
421 void __fscache_invalidate(struct fscache_cookie *cookie)
422 {
423         struct fscache_object *object;
424
425         _enter("{%s}", cookie->def->name);
426
427         fscache_stat(&fscache_n_invalidates);
428
429         /* Only permit invalidation of data files.  Invalidating an index will
430          * require the caller to release all its attachments to the tree rooted
431          * there, and if it's doing that, it may as well just retire the
432          * cookie.
433          */
434         ASSERTCMP(cookie->def->type, ==, FSCACHE_COOKIE_TYPE_DATAFILE);
435
436         /* We will be updating the cookie too. */
437         BUG_ON(!cookie->def->get_aux);
438
439         /* If there's an object, we tell the object state machine to handle the
440          * invalidation on our behalf, otherwise there's nothing to do.
441          */
442         if (!hlist_empty(&cookie->backing_objects)) {
443                 spin_lock(&cookie->lock);
444
445                 if (fscache_cookie_enabled(cookie) &&
446                     !hlist_empty(&cookie->backing_objects) &&
447                     !test_and_set_bit(FSCACHE_COOKIE_INVALIDATING,
448                                       &cookie->flags)) {
449                         object = hlist_entry(cookie->backing_objects.first,
450                                              struct fscache_object,
451                                              cookie_link);
452                         if (fscache_object_is_live(object))
453                                 fscache_raise_event(
454                                         object, FSCACHE_OBJECT_EV_INVALIDATE);
455                 }
456
457                 spin_unlock(&cookie->lock);
458         }
459
460         _leave("");
461 }
462 EXPORT_SYMBOL(__fscache_invalidate);
463
464 /*
465  * Wait for object invalidation to complete.
466  */
467 void __fscache_wait_on_invalidate(struct fscache_cookie *cookie)
468 {
469         _enter("%p", cookie);
470
471         wait_on_bit(&cookie->flags, FSCACHE_COOKIE_INVALIDATING,
472                     TASK_UNINTERRUPTIBLE);
473
474         _leave("");
475 }
476 EXPORT_SYMBOL(__fscache_wait_on_invalidate);
477
478 /*
479  * update the index entries backing a cookie
480  */
481 void __fscache_update_cookie(struct fscache_cookie *cookie)
482 {
483         struct fscache_object *object;
484
485         fscache_stat(&fscache_n_updates);
486
487         if (!cookie) {
488                 fscache_stat(&fscache_n_updates_null);
489                 _leave(" [no cookie]");
490                 return;
491         }
492
493         _enter("{%s}", cookie->def->name);
494
495         BUG_ON(!cookie->def->get_aux);
496
497         spin_lock(&cookie->lock);
498
499         if (fscache_cookie_enabled(cookie)) {
500                 /* update the index entry on disk in each cache backing this
501                  * cookie.
502                  */
503                 hlist_for_each_entry(object,
504                                      &cookie->backing_objects, cookie_link) {
505                         fscache_raise_event(object, FSCACHE_OBJECT_EV_UPDATE);
506                 }
507         }
508
509         spin_unlock(&cookie->lock);
510         _leave("");
511 }
512 EXPORT_SYMBOL(__fscache_update_cookie);
513
514 /*
515  * Disable a cookie to stop it from accepting new requests from the netfs.
516  */
517 void __fscache_disable_cookie(struct fscache_cookie *cookie, bool invalidate)
518 {
519         struct fscache_object *object;
520         bool awaken = false;
521
522         _enter("%p,%u", cookie, invalidate);
523
524         trace_fscache_disable(cookie);
525
526         ASSERTCMP(atomic_read(&cookie->n_active), >, 0);
527
528         if (atomic_read(&cookie->n_children) != 0) {
529                 pr_err("Cookie '%s' still has children\n",
530                        cookie->def->name);
531                 BUG();
532         }
533
534         wait_on_bit_lock(&cookie->flags, FSCACHE_COOKIE_ENABLEMENT_LOCK,
535                          TASK_UNINTERRUPTIBLE);
536         if (!test_and_clear_bit(FSCACHE_COOKIE_ENABLED, &cookie->flags))
537                 goto out_unlock_enable;
538
539         /* If the cookie is being invalidated, wait for that to complete first
540          * so that we can reuse the flag.
541          */
542         __fscache_wait_on_invalidate(cookie);
543
544         /* Dispose of the backing objects */
545         set_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags);
546
547         spin_lock(&cookie->lock);
548         if (!hlist_empty(&cookie->backing_objects)) {
549                 hlist_for_each_entry(object, &cookie->backing_objects, cookie_link) {
550                         if (invalidate)
551                                 set_bit(FSCACHE_OBJECT_RETIRED, &object->flags);
552                         clear_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags);
553                         fscache_raise_event(object, FSCACHE_OBJECT_EV_KILL);
554                 }
555         } else {
556                 if (test_and_clear_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags))
557                         awaken = true;
558         }
559         spin_unlock(&cookie->lock);
560         if (awaken)
561                 wake_up_bit(&cookie->flags, FSCACHE_COOKIE_INVALIDATING);
562
563         /* Wait for cessation of activity requiring access to the netfs (when
564          * n_active reaches 0).  This makes sure outstanding reads and writes
565          * have completed.
566          */
567         if (!atomic_dec_and_test(&cookie->n_active)) {
568                 wait_var_event(&cookie->n_active,
569                                !atomic_read(&cookie->n_active));
570         }
571
572         /* Make sure any pending writes are cancelled. */
573         if (cookie->def->type != FSCACHE_COOKIE_TYPE_INDEX)
574                 fscache_invalidate_writes(cookie);
575
576         /* Reset the cookie state if it wasn't relinquished */
577         if (!test_bit(FSCACHE_COOKIE_RELINQUISHED, &cookie->flags)) {
578                 atomic_inc(&cookie->n_active);
579                 set_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
580         }
581
582 out_unlock_enable:
583         clear_bit_unlock(FSCACHE_COOKIE_ENABLEMENT_LOCK, &cookie->flags);
584         wake_up_bit(&cookie->flags, FSCACHE_COOKIE_ENABLEMENT_LOCK);
585         _leave("");
586 }
587 EXPORT_SYMBOL(__fscache_disable_cookie);
588
589 /*
590  * release a cookie back to the cache
591  * - the object will be marked as recyclable on disk if retire is true
592  * - all dependents of this cookie must have already been unregistered
593  *   (indices/files/pages)
594  */
595 void __fscache_relinquish_cookie(struct fscache_cookie *cookie, bool retire)
596 {
597         fscache_stat(&fscache_n_relinquishes);
598         if (retire)
599                 fscache_stat(&fscache_n_relinquishes_retire);
600
601         if (!cookie) {
602                 fscache_stat(&fscache_n_relinquishes_null);
603                 _leave(" [no cookie]");
604                 return;
605         }
606
607         _enter("%p{%s,%p,%d},%d",
608                cookie, cookie->def->name, cookie->netfs_data,
609                atomic_read(&cookie->n_active), retire);
610
611         trace_fscache_relinquish(cookie, retire);
612
613         /* No further netfs-accessing operations on this cookie permitted */
614         if (test_and_set_bit(FSCACHE_COOKIE_RELINQUISHED, &cookie->flags))
615                 BUG();
616
617         __fscache_disable_cookie(cookie, retire);
618
619         /* Clear pointers back to the netfs */
620         cookie->netfs_data      = NULL;
621         cookie->def             = NULL;
622         BUG_ON(cookie->stores.rnode);
623
624         if (cookie->parent) {
625                 ASSERTCMP(atomic_read(&cookie->parent->usage), >, 0);
626                 ASSERTCMP(atomic_read(&cookie->parent->n_children), >, 0);
627                 atomic_dec(&cookie->parent->n_children);
628         }
629
630         /* Dispose of the netfs's link to the cookie */
631         ASSERTCMP(atomic_read(&cookie->usage), >, 0);
632         fscache_cookie_put(cookie, fscache_cookie_put_relinquish);
633
634         _leave("");
635 }
636 EXPORT_SYMBOL(__fscache_relinquish_cookie);
637
638 /*
639  * Drop a reference to a cookie.
640  */
641 void fscache_cookie_put(struct fscache_cookie *cookie,
642                         enum fscache_cookie_trace where)
643 {
644         struct fscache_cookie *parent;
645         int usage;
646
647         _enter("%p", cookie);
648
649         do {
650                 usage = atomic_dec_return(&cookie->usage);
651                 trace_fscache_cookie(cookie, where, usage);
652
653                 if (usage > 0)
654                         return;
655                 BUG_ON(usage < 0);
656
657                 parent = cookie->parent;
658                 BUG_ON(!hlist_empty(&cookie->backing_objects));
659                 kmem_cache_free(fscache_cookie_jar, cookie);
660
661                 cookie = parent;
662                 where = fscache_cookie_put_parent;
663         } while (cookie);
664
665         _leave("");
666 }
667
668 /*
669  * check the consistency between the netfs inode and the backing cache
670  *
671  * NOTE: it only serves no-index type
672  */
673 int __fscache_check_consistency(struct fscache_cookie *cookie)
674 {
675         struct fscache_operation *op;
676         struct fscache_object *object;
677         bool wake_cookie = false;
678         int ret;
679
680         _enter("%p,", cookie);
681
682         ASSERTCMP(cookie->def->type, ==, FSCACHE_COOKIE_TYPE_DATAFILE);
683
684         if (fscache_wait_for_deferred_lookup(cookie) < 0)
685                 return -ERESTARTSYS;
686
687         if (hlist_empty(&cookie->backing_objects))
688                 return 0;
689
690         op = kzalloc(sizeof(*op), GFP_NOIO | __GFP_NOMEMALLOC | __GFP_NORETRY);
691         if (!op)
692                 return -ENOMEM;
693
694         fscache_operation_init(op, NULL, NULL, NULL);
695         op->flags = FSCACHE_OP_MYTHREAD |
696                 (1 << FSCACHE_OP_WAITING) |
697                 (1 << FSCACHE_OP_UNUSE_COOKIE);
698
699         spin_lock(&cookie->lock);
700
701         if (!fscache_cookie_enabled(cookie) ||
702             hlist_empty(&cookie->backing_objects))
703                 goto inconsistent;
704         object = hlist_entry(cookie->backing_objects.first,
705                              struct fscache_object, cookie_link);
706         if (test_bit(FSCACHE_IOERROR, &object->cache->flags))
707                 goto inconsistent;
708
709         op->debug_id = atomic_inc_return(&fscache_op_debug_id);
710
711         __fscache_use_cookie(cookie);
712         if (fscache_submit_op(object, op) < 0)
713                 goto submit_failed;
714
715         /* the work queue now carries its own ref on the object */
716         spin_unlock(&cookie->lock);
717
718         ret = fscache_wait_for_operation_activation(object, op, NULL, NULL);
719         if (ret == 0) {
720                 /* ask the cache to honour the operation */
721                 ret = object->cache->ops->check_consistency(op);
722                 fscache_op_complete(op, false);
723         } else if (ret == -ENOBUFS) {
724                 ret = 0;
725         }
726
727         fscache_put_operation(op);
728         _leave(" = %d", ret);
729         return ret;
730
731 submit_failed:
732         wake_cookie = __fscache_unuse_cookie(cookie);
733 inconsistent:
734         spin_unlock(&cookie->lock);
735         if (wake_cookie)
736                 __fscache_wake_unused_cookie(cookie);
737         kfree(op);
738         _leave(" = -ESTALE");
739         return -ESTALE;
740 }
741 EXPORT_SYMBOL(__fscache_check_consistency);