FS-Cache: Allow the current state of all objects to be dumped
[linux-2.6.git] / fs / fscache / page.c
1 /* Cache page management and data I/O routines
2  *
3  * Copyright (C) 2004-2008 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
12 #define FSCACHE_DEBUG_LEVEL PAGE
13 #include <linux/module.h>
14 #include <linux/fscache-cache.h>
15 #include <linux/buffer_head.h>
16 #include <linux/pagevec.h>
17 #include "internal.h"
18
19 /*
20  * check to see if a page is being written to the cache
21  */
22 bool __fscache_check_page_write(struct fscache_cookie *cookie, struct page *page)
23 {
24         void *val;
25
26         rcu_read_lock();
27         val = radix_tree_lookup(&cookie->stores, page->index);
28         rcu_read_unlock();
29
30         return val != NULL;
31 }
32 EXPORT_SYMBOL(__fscache_check_page_write);
33
34 /*
35  * wait for a page to finish being written to the cache
36  */
37 void __fscache_wait_on_page_write(struct fscache_cookie *cookie, struct page *page)
38 {
39         wait_queue_head_t *wq = bit_waitqueue(&cookie->flags, 0);
40
41         wait_event(*wq, !__fscache_check_page_write(cookie, page));
42 }
43 EXPORT_SYMBOL(__fscache_wait_on_page_write);
44
45 /*
46  * note that a page has finished being written to the cache
47  */
48 static void fscache_end_page_write(struct fscache_cookie *cookie, struct page *page)
49 {
50         struct page *xpage;
51
52         spin_lock(&cookie->lock);
53         xpage = radix_tree_delete(&cookie->stores, page->index);
54         spin_unlock(&cookie->lock);
55         ASSERT(xpage != NULL);
56
57         wake_up_bit(&cookie->flags, 0);
58 }
59
60 /*
61  * actually apply the changed attributes to a cache object
62  */
63 static void fscache_attr_changed_op(struct fscache_operation *op)
64 {
65         struct fscache_object *object = op->object;
66         int ret;
67
68         _enter("{OBJ%x OP%x}", object->debug_id, op->debug_id);
69
70         fscache_stat(&fscache_n_attr_changed_calls);
71
72         if (fscache_object_is_active(object)) {
73                 fscache_set_op_state(op, "CallFS");
74                 ret = object->cache->ops->attr_changed(object);
75                 fscache_set_op_state(op, "Done");
76                 if (ret < 0)
77                         fscache_abort_object(object);
78         }
79
80         _leave("");
81 }
82
83 /*
84  * notification that the attributes on an object have changed
85  */
86 int __fscache_attr_changed(struct fscache_cookie *cookie)
87 {
88         struct fscache_operation *op;
89         struct fscache_object *object;
90
91         _enter("%p", cookie);
92
93         ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
94
95         fscache_stat(&fscache_n_attr_changed);
96
97         op = kzalloc(sizeof(*op), GFP_KERNEL);
98         if (!op) {
99                 fscache_stat(&fscache_n_attr_changed_nomem);
100                 _leave(" = -ENOMEM");
101                 return -ENOMEM;
102         }
103
104         fscache_operation_init(op, NULL);
105         fscache_operation_init_slow(op, fscache_attr_changed_op);
106         op->flags = FSCACHE_OP_SLOW | (1 << FSCACHE_OP_EXCLUSIVE);
107         fscache_set_op_name(op, "Attr");
108
109         spin_lock(&cookie->lock);
110
111         if (hlist_empty(&cookie->backing_objects))
112                 goto nobufs;
113         object = hlist_entry(cookie->backing_objects.first,
114                              struct fscache_object, cookie_link);
115
116         if (fscache_submit_exclusive_op(object, op) < 0)
117                 goto nobufs;
118         spin_unlock(&cookie->lock);
119         fscache_stat(&fscache_n_attr_changed_ok);
120         fscache_put_operation(op);
121         _leave(" = 0");
122         return 0;
123
124 nobufs:
125         spin_unlock(&cookie->lock);
126         kfree(op);
127         fscache_stat(&fscache_n_attr_changed_nobufs);
128         _leave(" = %d", -ENOBUFS);
129         return -ENOBUFS;
130 }
131 EXPORT_SYMBOL(__fscache_attr_changed);
132
133 /*
134  * handle secondary execution given to a retrieval op on behalf of the
135  * cache
136  */
137 static void fscache_retrieval_work(struct work_struct *work)
138 {
139         struct fscache_retrieval *op =
140                 container_of(work, struct fscache_retrieval, op.fast_work);
141         unsigned long start;
142
143         _enter("{OP%x}", op->op.debug_id);
144
145         start = jiffies;
146         op->op.processor(&op->op);
147         fscache_hist(fscache_ops_histogram, start);
148         fscache_put_operation(&op->op);
149 }
150
151 /*
152  * release a retrieval op reference
153  */
154 static void fscache_release_retrieval_op(struct fscache_operation *_op)
155 {
156         struct fscache_retrieval *op =
157                 container_of(_op, struct fscache_retrieval, op);
158
159         _enter("{OP%x}", op->op.debug_id);
160
161         fscache_hist(fscache_retrieval_histogram, op->start_time);
162         if (op->context)
163                 fscache_put_context(op->op.object->cookie, op->context);
164
165         _leave("");
166 }
167
168 /*
169  * allocate a retrieval op
170  */
171 static struct fscache_retrieval *fscache_alloc_retrieval(
172         struct address_space *mapping,
173         fscache_rw_complete_t end_io_func,
174         void *context)
175 {
176         struct fscache_retrieval *op;
177
178         /* allocate a retrieval operation and attempt to submit it */
179         op = kzalloc(sizeof(*op), GFP_NOIO);
180         if (!op) {
181                 fscache_stat(&fscache_n_retrievals_nomem);
182                 return NULL;
183         }
184
185         fscache_operation_init(&op->op, fscache_release_retrieval_op);
186         op->op.flags    = FSCACHE_OP_MYTHREAD | (1 << FSCACHE_OP_WAITING);
187         op->mapping     = mapping;
188         op->end_io_func = end_io_func;
189         op->context     = context;
190         op->start_time  = jiffies;
191         INIT_WORK(&op->op.fast_work, fscache_retrieval_work);
192         INIT_LIST_HEAD(&op->to_do);
193         fscache_set_op_name(&op->op, "Retr");
194         return op;
195 }
196
197 /*
198  * wait for a deferred lookup to complete
199  */
200 static int fscache_wait_for_deferred_lookup(struct fscache_cookie *cookie)
201 {
202         unsigned long jif;
203
204         _enter("");
205
206         if (!test_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags)) {
207                 _leave(" = 0 [imm]");
208                 return 0;
209         }
210
211         fscache_stat(&fscache_n_retrievals_wait);
212
213         jif = jiffies;
214         if (wait_on_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP,
215                         fscache_wait_bit_interruptible,
216                         TASK_INTERRUPTIBLE) != 0) {
217                 fscache_stat(&fscache_n_retrievals_intr);
218                 _leave(" = -ERESTARTSYS");
219                 return -ERESTARTSYS;
220         }
221
222         ASSERT(!test_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags));
223
224         smp_rmb();
225         fscache_hist(fscache_retrieval_delay_histogram, jif);
226         _leave(" = 0 [dly]");
227         return 0;
228 }
229
230 /*
231  * read a page from the cache or allocate a block in which to store it
232  * - we return:
233  *   -ENOMEM    - out of memory, nothing done
234  *   -ERESTARTSYS - interrupted
235  *   -ENOBUFS   - no backing object available in which to cache the block
236  *   -ENODATA   - no data available in the backing object for this block
237  *   0          - dispatched a read - it'll call end_io_func() when finished
238  */
239 int __fscache_read_or_alloc_page(struct fscache_cookie *cookie,
240                                  struct page *page,
241                                  fscache_rw_complete_t end_io_func,
242                                  void *context,
243                                  gfp_t gfp)
244 {
245         struct fscache_retrieval *op;
246         struct fscache_object *object;
247         int ret;
248
249         _enter("%p,%p,,,", cookie, page);
250
251         fscache_stat(&fscache_n_retrievals);
252
253         if (hlist_empty(&cookie->backing_objects))
254                 goto nobufs;
255
256         ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
257         ASSERTCMP(page, !=, NULL);
258
259         if (fscache_wait_for_deferred_lookup(cookie) < 0)
260                 return -ERESTARTSYS;
261
262         op = fscache_alloc_retrieval(page->mapping, end_io_func, context);
263         if (!op) {
264                 _leave(" = -ENOMEM");
265                 return -ENOMEM;
266         }
267         fscache_set_op_name(&op->op, "RetrRA1");
268
269         spin_lock(&cookie->lock);
270
271         if (hlist_empty(&cookie->backing_objects))
272                 goto nobufs_unlock;
273         object = hlist_entry(cookie->backing_objects.first,
274                              struct fscache_object, cookie_link);
275
276         ASSERTCMP(object->state, >, FSCACHE_OBJECT_LOOKING_UP);
277
278         atomic_inc(&object->n_reads);
279         set_bit(FSCACHE_OP_DEC_READ_CNT, &op->op.flags);
280
281         if (fscache_submit_op(object, &op->op) < 0)
282                 goto nobufs_unlock;
283         spin_unlock(&cookie->lock);
284
285         fscache_stat(&fscache_n_retrieval_ops);
286
287         /* pin the netfs read context in case we need to do the actual netfs
288          * read because we've encountered a cache read failure */
289         fscache_get_context(object->cookie, op->context);
290
291         /* we wait for the operation to become active, and then process it
292          * *here*, in this thread, and not in the thread pool */
293         if (test_bit(FSCACHE_OP_WAITING, &op->op.flags)) {
294                 _debug(">>> WT");
295                 fscache_stat(&fscache_n_retrieval_op_waits);
296                 wait_on_bit(&op->op.flags, FSCACHE_OP_WAITING,
297                             fscache_wait_bit, TASK_UNINTERRUPTIBLE);
298                 _debug("<<< GO");
299         }
300
301         /* ask the cache to honour the operation */
302         if (test_bit(FSCACHE_COOKIE_NO_DATA_YET, &object->cookie->flags)) {
303                 ret = object->cache->ops->allocate_page(op, page, gfp);
304                 if (ret == 0)
305                         ret = -ENODATA;
306         } else {
307                 ret = object->cache->ops->read_or_alloc_page(op, page, gfp);
308         }
309
310         if (ret == -ENOMEM)
311                 fscache_stat(&fscache_n_retrievals_nomem);
312         else if (ret == -ERESTARTSYS)
313                 fscache_stat(&fscache_n_retrievals_intr);
314         else if (ret == -ENODATA)
315                 fscache_stat(&fscache_n_retrievals_nodata);
316         else if (ret < 0)
317                 fscache_stat(&fscache_n_retrievals_nobufs);
318         else
319                 fscache_stat(&fscache_n_retrievals_ok);
320
321         fscache_put_retrieval(op);
322         _leave(" = %d", ret);
323         return ret;
324
325 nobufs_unlock:
326         spin_unlock(&cookie->lock);
327         kfree(op);
328 nobufs:
329         fscache_stat(&fscache_n_retrievals_nobufs);
330         _leave(" = -ENOBUFS");
331         return -ENOBUFS;
332 }
333 EXPORT_SYMBOL(__fscache_read_or_alloc_page);
334
335 /*
336  * read a list of page from the cache or allocate a block in which to store
337  * them
338  * - we return:
339  *   -ENOMEM    - out of memory, some pages may be being read
340  *   -ERESTARTSYS - interrupted, some pages may be being read
341  *   -ENOBUFS   - no backing object or space available in which to cache any
342  *                pages not being read
343  *   -ENODATA   - no data available in the backing object for some or all of
344  *                the pages
345  *   0          - dispatched a read on all pages
346  *
347  * end_io_func() will be called for each page read from the cache as it is
348  * finishes being read
349  *
350  * any pages for which a read is dispatched will be removed from pages and
351  * nr_pages
352  */
353 int __fscache_read_or_alloc_pages(struct fscache_cookie *cookie,
354                                   struct address_space *mapping,
355                                   struct list_head *pages,
356                                   unsigned *nr_pages,
357                                   fscache_rw_complete_t end_io_func,
358                                   void *context,
359                                   gfp_t gfp)
360 {
361         fscache_pages_retrieval_func_t func;
362         struct fscache_retrieval *op;
363         struct fscache_object *object;
364         int ret;
365
366         _enter("%p,,%d,,,", cookie, *nr_pages);
367
368         fscache_stat(&fscache_n_retrievals);
369
370         if (hlist_empty(&cookie->backing_objects))
371                 goto nobufs;
372
373         ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
374         ASSERTCMP(*nr_pages, >, 0);
375         ASSERT(!list_empty(pages));
376
377         if (fscache_wait_for_deferred_lookup(cookie) < 0)
378                 return -ERESTARTSYS;
379
380         op = fscache_alloc_retrieval(mapping, end_io_func, context);
381         if (!op)
382                 return -ENOMEM;
383         fscache_set_op_name(&op->op, "RetrRAN");
384
385         spin_lock(&cookie->lock);
386
387         if (hlist_empty(&cookie->backing_objects))
388                 goto nobufs_unlock;
389         object = hlist_entry(cookie->backing_objects.first,
390                              struct fscache_object, cookie_link);
391
392         atomic_inc(&object->n_reads);
393         set_bit(FSCACHE_OP_DEC_READ_CNT, &op->op.flags);
394
395         if (fscache_submit_op(object, &op->op) < 0)
396                 goto nobufs_unlock;
397         spin_unlock(&cookie->lock);
398
399         fscache_stat(&fscache_n_retrieval_ops);
400
401         /* pin the netfs read context in case we need to do the actual netfs
402          * read because we've encountered a cache read failure */
403         fscache_get_context(object->cookie, op->context);
404
405         /* we wait for the operation to become active, and then process it
406          * *here*, in this thread, and not in the thread pool */
407         if (test_bit(FSCACHE_OP_WAITING, &op->op.flags)) {
408                 _debug(">>> WT");
409                 fscache_stat(&fscache_n_retrieval_op_waits);
410                 wait_on_bit(&op->op.flags, FSCACHE_OP_WAITING,
411                             fscache_wait_bit, TASK_UNINTERRUPTIBLE);
412                 _debug("<<< GO");
413         }
414
415         /* ask the cache to honour the operation */
416         if (test_bit(FSCACHE_COOKIE_NO_DATA_YET, &object->cookie->flags))
417                 func = object->cache->ops->allocate_pages;
418         else
419                 func = object->cache->ops->read_or_alloc_pages;
420         ret = func(op, pages, nr_pages, gfp);
421
422         if (ret == -ENOMEM)
423                 fscache_stat(&fscache_n_retrievals_nomem);
424         else if (ret == -ERESTARTSYS)
425                 fscache_stat(&fscache_n_retrievals_intr);
426         else if (ret == -ENODATA)
427                 fscache_stat(&fscache_n_retrievals_nodata);
428         else if (ret < 0)
429                 fscache_stat(&fscache_n_retrievals_nobufs);
430         else
431                 fscache_stat(&fscache_n_retrievals_ok);
432
433         fscache_put_retrieval(op);
434         _leave(" = %d", ret);
435         return ret;
436
437 nobufs_unlock:
438         spin_unlock(&cookie->lock);
439         kfree(op);
440 nobufs:
441         fscache_stat(&fscache_n_retrievals_nobufs);
442         _leave(" = -ENOBUFS");
443         return -ENOBUFS;
444 }
445 EXPORT_SYMBOL(__fscache_read_or_alloc_pages);
446
447 /*
448  * allocate a block in the cache on which to store a page
449  * - we return:
450  *   -ENOMEM    - out of memory, nothing done
451  *   -ERESTARTSYS - interrupted
452  *   -ENOBUFS   - no backing object available in which to cache the block
453  *   0          - block allocated
454  */
455 int __fscache_alloc_page(struct fscache_cookie *cookie,
456                          struct page *page,
457                          gfp_t gfp)
458 {
459         struct fscache_retrieval *op;
460         struct fscache_object *object;
461         int ret;
462
463         _enter("%p,%p,,,", cookie, page);
464
465         fscache_stat(&fscache_n_allocs);
466
467         if (hlist_empty(&cookie->backing_objects))
468                 goto nobufs;
469
470         ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
471         ASSERTCMP(page, !=, NULL);
472
473         if (fscache_wait_for_deferred_lookup(cookie) < 0)
474                 return -ERESTARTSYS;
475
476         op = fscache_alloc_retrieval(page->mapping, NULL, NULL);
477         if (!op)
478                 return -ENOMEM;
479         fscache_set_op_name(&op->op, "RetrAL1");
480
481         spin_lock(&cookie->lock);
482
483         if (hlist_empty(&cookie->backing_objects))
484                 goto nobufs_unlock;
485         object = hlist_entry(cookie->backing_objects.first,
486                              struct fscache_object, cookie_link);
487
488         if (fscache_submit_op(object, &op->op) < 0)
489                 goto nobufs_unlock;
490         spin_unlock(&cookie->lock);
491
492         fscache_stat(&fscache_n_alloc_ops);
493
494         if (test_bit(FSCACHE_OP_WAITING, &op->op.flags)) {
495                 _debug(">>> WT");
496                 fscache_stat(&fscache_n_alloc_op_waits);
497                 wait_on_bit(&op->op.flags, FSCACHE_OP_WAITING,
498                             fscache_wait_bit, TASK_UNINTERRUPTIBLE);
499                 _debug("<<< GO");
500         }
501
502         /* ask the cache to honour the operation */
503         ret = object->cache->ops->allocate_page(op, page, gfp);
504
505         if (ret < 0)
506                 fscache_stat(&fscache_n_allocs_nobufs);
507         else
508                 fscache_stat(&fscache_n_allocs_ok);
509
510         fscache_put_retrieval(op);
511         _leave(" = %d", ret);
512         return ret;
513
514 nobufs_unlock:
515         spin_unlock(&cookie->lock);
516         kfree(op);
517 nobufs:
518         fscache_stat(&fscache_n_allocs_nobufs);
519         _leave(" = -ENOBUFS");
520         return -ENOBUFS;
521 }
522 EXPORT_SYMBOL(__fscache_alloc_page);
523
524 /*
525  * release a write op reference
526  */
527 static void fscache_release_write_op(struct fscache_operation *_op)
528 {
529         _enter("{OP%x}", _op->debug_id);
530 }
531
532 /*
533  * perform the background storage of a page into the cache
534  */
535 static void fscache_write_op(struct fscache_operation *_op)
536 {
537         struct fscache_storage *op =
538                 container_of(_op, struct fscache_storage, op);
539         struct fscache_object *object = op->op.object;
540         struct fscache_cookie *cookie = object->cookie;
541         struct page *page;
542         unsigned n;
543         void *results[1];
544         int ret;
545
546         _enter("{OP%x,%d}", op->op.debug_id, atomic_read(&op->op.usage));
547
548         fscache_set_op_state(&op->op, "GetPage");
549
550         spin_lock(&cookie->lock);
551         spin_lock(&object->lock);
552
553         if (!fscache_object_is_active(object)) {
554                 spin_unlock(&object->lock);
555                 spin_unlock(&cookie->lock);
556                 _leave("");
557                 return;
558         }
559
560         fscache_stat(&fscache_n_store_calls);
561
562         /* find a page to store */
563         page = NULL;
564         n = radix_tree_gang_lookup_tag(&cookie->stores, results, 0, 1,
565                                        FSCACHE_COOKIE_PENDING_TAG);
566         if (n != 1)
567                 goto superseded;
568         page = results[0];
569         _debug("gang %d [%lx]", n, page->index);
570         if (page->index > op->store_limit)
571                 goto superseded;
572
573         radix_tree_tag_clear(&cookie->stores, page->index,
574                              FSCACHE_COOKIE_PENDING_TAG);
575
576         spin_unlock(&object->lock);
577         spin_unlock(&cookie->lock);
578
579         if (page) {
580                 fscache_set_op_state(&op->op, "Store");
581                 ret = object->cache->ops->write_page(op, page);
582                 fscache_set_op_state(&op->op, "EndWrite");
583                 fscache_end_page_write(cookie, page);
584                 page_cache_release(page);
585                 if (ret < 0) {
586                         fscache_set_op_state(&op->op, "Abort");
587                         fscache_abort_object(object);
588                 } else {
589                         fscache_enqueue_operation(&op->op);
590                 }
591         }
592
593         _leave("");
594         return;
595
596 superseded:
597         /* this writer is going away and there aren't any more things to
598          * write */
599         _debug("cease");
600         clear_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags);
601         spin_unlock(&object->lock);
602         spin_unlock(&cookie->lock);
603         _leave("");
604 }
605
606 /*
607  * request a page be stored in the cache
608  * - returns:
609  *   -ENOMEM    - out of memory, nothing done
610  *   -ENOBUFS   - no backing object available in which to cache the page
611  *   0          - dispatched a write - it'll call end_io_func() when finished
612  *
613  * if the cookie still has a backing object at this point, that object can be
614  * in one of a few states with respect to storage processing:
615  *
616  *  (1) negative lookup, object not yet created (FSCACHE_COOKIE_CREATING is
617  *      set)
618  *
619  *      (a) no writes yet (set FSCACHE_COOKIE_PENDING_FILL and queue deferred
620  *          fill op)
621  *
622  *      (b) writes deferred till post-creation (mark page for writing and
623  *          return immediately)
624  *
625  *  (2) negative lookup, object created, initial fill being made from netfs
626  *      (FSCACHE_COOKIE_INITIAL_FILL is set)
627  *
628  *      (a) fill point not yet reached this page (mark page for writing and
629  *          return)
630  *
631  *      (b) fill point passed this page (queue op to store this page)
632  *
633  *  (3) object extant (queue op to store this page)
634  *
635  * any other state is invalid
636  */
637 int __fscache_write_page(struct fscache_cookie *cookie,
638                          struct page *page,
639                          gfp_t gfp)
640 {
641         struct fscache_storage *op;
642         struct fscache_object *object;
643         int ret;
644
645         _enter("%p,%x,", cookie, (u32) page->flags);
646
647         ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
648         ASSERT(PageFsCache(page));
649
650         fscache_stat(&fscache_n_stores);
651
652         op = kzalloc(sizeof(*op), GFP_NOIO);
653         if (!op)
654                 goto nomem;
655
656         fscache_operation_init(&op->op, fscache_release_write_op);
657         fscache_operation_init_slow(&op->op, fscache_write_op);
658         op->op.flags = FSCACHE_OP_SLOW | (1 << FSCACHE_OP_WAITING);
659         fscache_set_op_name(&op->op, "Write1");
660
661         ret = radix_tree_preload(gfp & ~__GFP_HIGHMEM);
662         if (ret < 0)
663                 goto nomem_free;
664
665         ret = -ENOBUFS;
666         spin_lock(&cookie->lock);
667
668         if (hlist_empty(&cookie->backing_objects))
669                 goto nobufs;
670         object = hlist_entry(cookie->backing_objects.first,
671                              struct fscache_object, cookie_link);
672         if (test_bit(FSCACHE_IOERROR, &object->cache->flags))
673                 goto nobufs;
674
675         /* add the page to the pending-storage radix tree on the backing
676          * object */
677         spin_lock(&object->lock);
678
679         _debug("store limit %llx", (unsigned long long) object->store_limit);
680
681         ret = radix_tree_insert(&cookie->stores, page->index, page);
682         if (ret < 0) {
683                 if (ret == -EEXIST)
684                         goto already_queued;
685                 _debug("insert failed %d", ret);
686                 goto nobufs_unlock_obj;
687         }
688
689         radix_tree_tag_set(&cookie->stores, page->index,
690                            FSCACHE_COOKIE_PENDING_TAG);
691         page_cache_get(page);
692
693         /* we only want one writer at a time, but we do need to queue new
694          * writers after exclusive ops */
695         if (test_and_set_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags))
696                 goto already_pending;
697
698         spin_unlock(&object->lock);
699
700         op->op.debug_id = atomic_inc_return(&fscache_op_debug_id);
701         op->store_limit = object->store_limit;
702
703         if (fscache_submit_op(object, &op->op) < 0)
704                 goto submit_failed;
705
706         spin_unlock(&cookie->lock);
707         radix_tree_preload_end();
708         fscache_stat(&fscache_n_store_ops);
709         fscache_stat(&fscache_n_stores_ok);
710
711         /* the slow work queue now carries its own ref on the object */
712         fscache_put_operation(&op->op);
713         _leave(" = 0");
714         return 0;
715
716 already_queued:
717         fscache_stat(&fscache_n_stores_again);
718 already_pending:
719         spin_unlock(&object->lock);
720         spin_unlock(&cookie->lock);
721         radix_tree_preload_end();
722         kfree(op);
723         fscache_stat(&fscache_n_stores_ok);
724         _leave(" = 0");
725         return 0;
726
727 submit_failed:
728         radix_tree_delete(&cookie->stores, page->index);
729         page_cache_release(page);
730         ret = -ENOBUFS;
731         goto nobufs;
732
733 nobufs_unlock_obj:
734         spin_unlock(&object->lock);
735 nobufs:
736         spin_unlock(&cookie->lock);
737         radix_tree_preload_end();
738         kfree(op);
739         fscache_stat(&fscache_n_stores_nobufs);
740         _leave(" = -ENOBUFS");
741         return -ENOBUFS;
742
743 nomem_free:
744         kfree(op);
745 nomem:
746         fscache_stat(&fscache_n_stores_oom);
747         _leave(" = -ENOMEM");
748         return -ENOMEM;
749 }
750 EXPORT_SYMBOL(__fscache_write_page);
751
752 /*
753  * remove a page from the cache
754  */
755 void __fscache_uncache_page(struct fscache_cookie *cookie, struct page *page)
756 {
757         struct fscache_object *object;
758
759         _enter(",%p", page);
760
761         ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
762         ASSERTCMP(page, !=, NULL);
763
764         fscache_stat(&fscache_n_uncaches);
765
766         /* cache withdrawal may beat us to it */
767         if (!PageFsCache(page))
768                 goto done;
769
770         /* get the object */
771         spin_lock(&cookie->lock);
772
773         if (hlist_empty(&cookie->backing_objects)) {
774                 ClearPageFsCache(page);
775                 goto done_unlock;
776         }
777
778         object = hlist_entry(cookie->backing_objects.first,
779                              struct fscache_object, cookie_link);
780
781         /* there might now be stuff on disk we could read */
782         clear_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
783
784         /* only invoke the cache backend if we managed to mark the page
785          * uncached here; this deals with synchronisation vs withdrawal */
786         if (TestClearPageFsCache(page) &&
787             object->cache->ops->uncache_page) {
788                 /* the cache backend releases the cookie lock */
789                 object->cache->ops->uncache_page(object, page);
790                 goto done;
791         }
792
793 done_unlock:
794         spin_unlock(&cookie->lock);
795 done:
796         _leave("");
797 }
798 EXPORT_SYMBOL(__fscache_uncache_page);
799
800 /**
801  * fscache_mark_pages_cached - Mark pages as being cached
802  * @op: The retrieval op pages are being marked for
803  * @pagevec: The pages to be marked
804  *
805  * Mark a bunch of netfs pages as being cached.  After this is called,
806  * the netfs must call fscache_uncache_page() to remove the mark.
807  */
808 void fscache_mark_pages_cached(struct fscache_retrieval *op,
809                                struct pagevec *pagevec)
810 {
811         struct fscache_cookie *cookie = op->op.object->cookie;
812         unsigned long loop;
813
814 #ifdef CONFIG_FSCACHE_STATS
815         atomic_add(pagevec->nr, &fscache_n_marks);
816 #endif
817
818         for (loop = 0; loop < pagevec->nr; loop++) {
819                 struct page *page = pagevec->pages[loop];
820
821                 _debug("- mark %p{%lx}", page, page->index);
822                 if (TestSetPageFsCache(page)) {
823                         static bool once_only;
824                         if (!once_only) {
825                                 once_only = true;
826                                 printk(KERN_WARNING "FS-Cache:"
827                                        " Cookie type %s marked page %lx"
828                                        " multiple times\n",
829                                        cookie->def->name, page->index);
830                         }
831                 }
832         }
833
834         if (cookie->def->mark_pages_cached)
835                 cookie->def->mark_pages_cached(cookie->netfs_data,
836                                                op->mapping, pagevec);
837         pagevec_reinit(pagevec);
838 }
839 EXPORT_SYMBOL(fscache_mark_pages_cached);