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