Blame view

fs/fscache/page.c 25.7 KB
b51088228   David Howells   FS-Cache: Impleme...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  /* Cache page management and data I/O routines
   *
   * Copyright (C) 2004-2008 Red Hat, Inc. All Rights Reserved.
   * Written by David Howells (dhowells@redhat.com)
   *
   * This program is free software; you can redistribute it and/or
   * modify it under the terms of the GNU General Public License
   * as published by the Free Software Foundation; either version
   * 2 of the License, or (at your option) any later version.
   */
  
  #define FSCACHE_DEBUG_LEVEL PAGE
  #include <linux/module.h>
  #include <linux/fscache-cache.h>
  #include <linux/buffer_head.h>
  #include <linux/pagevec.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
17
  #include <linux/slab.h>
b51088228   David Howells   FS-Cache: Impleme...
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
  #include "internal.h"
  
  /*
   * check to see if a page is being written to the cache
   */
  bool __fscache_check_page_write(struct fscache_cookie *cookie, struct page *page)
  {
  	void *val;
  
  	rcu_read_lock();
  	val = radix_tree_lookup(&cookie->stores, page->index);
  	rcu_read_unlock();
  
  	return val != NULL;
  }
  EXPORT_SYMBOL(__fscache_check_page_write);
  
  /*
   * wait for a page to finish being written to the cache
   */
  void __fscache_wait_on_page_write(struct fscache_cookie *cookie, struct page *page)
  {
  	wait_queue_head_t *wq = bit_waitqueue(&cookie->flags, 0);
  
  	wait_event(*wq, !__fscache_check_page_write(cookie, page));
  }
  EXPORT_SYMBOL(__fscache_wait_on_page_write);
  
  /*
201a15428   David Howells   FS-Cache: Handle ...
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
   * decide whether a page can be released, possibly by cancelling a store to it
   * - we're allowed to sleep if __GFP_WAIT is flagged
   */
  bool __fscache_maybe_release_page(struct fscache_cookie *cookie,
  				  struct page *page,
  				  gfp_t gfp)
  {
  	struct page *xpage;
  	void *val;
  
  	_enter("%p,%p,%x", cookie, page, gfp);
  
  	rcu_read_lock();
  	val = radix_tree_lookup(&cookie->stores, page->index);
  	if (!val) {
  		rcu_read_unlock();
  		fscache_stat(&fscache_n_store_vmscan_not_storing);
  		__fscache_uncache_page(cookie, page);
  		return true;
  	}
  
  	/* see if the page is actually undergoing storage - if so we can't get
  	 * rid of it till the cache has finished with it */
  	if (radix_tree_tag_get(&cookie->stores, page->index,
  			       FSCACHE_COOKIE_STORING_TAG)) {
  		rcu_read_unlock();
  		goto page_busy;
  	}
  
  	/* the page is pending storage, so we attempt to cancel the store and
  	 * discard the store request so that the page can be reclaimed */
  	spin_lock(&cookie->stores_lock);
  	rcu_read_unlock();
  
  	if (radix_tree_tag_get(&cookie->stores, page->index,
  			       FSCACHE_COOKIE_STORING_TAG)) {
  		/* the page started to undergo storage whilst we were looking,
  		 * so now we can only wait or return */
  		spin_unlock(&cookie->stores_lock);
  		goto page_busy;
  	}
  
  	xpage = radix_tree_delete(&cookie->stores, page->index);
  	spin_unlock(&cookie->stores_lock);
  
  	if (xpage) {
  		fscache_stat(&fscache_n_store_vmscan_cancelled);
  		fscache_stat(&fscache_n_store_radix_deletes);
  		ASSERTCMP(xpage, ==, page);
  	} else {
  		fscache_stat(&fscache_n_store_vmscan_gone);
  	}
  
  	wake_up_bit(&cookie->flags, 0);
  	if (xpage)
  		page_cache_release(xpage);
  	__fscache_uncache_page(cookie, page);
  	return true;
  
  page_busy:
  	/* we might want to wait here, but that could deadlock the allocator as
8af7c1243   Tejun Heo   fscache: convert ...
108
  	 * the work threads writing to the cache may all end up sleeping
201a15428   David Howells   FS-Cache: Handle ...
109
110
111
112
113
114
115
  	 * on memory allocation */
  	fscache_stat(&fscache_n_store_vmscan_busy);
  	return false;
  }
  EXPORT_SYMBOL(__fscache_maybe_release_page);
  
  /*
b51088228   David Howells   FS-Cache: Impleme...
116
117
   * note that a page has finished being written to the cache
   */
1bccf513a   David Howells   FS-Cache: Fix loc...
118
119
  static void fscache_end_page_write(struct fscache_object *object,
  				   struct page *page)
b51088228   David Howells   FS-Cache: Impleme...
120
  {
1bccf513a   David Howells   FS-Cache: Fix loc...
121
122
  	struct fscache_cookie *cookie;
  	struct page *xpage = NULL;
b51088228   David Howells   FS-Cache: Impleme...
123

1bccf513a   David Howells   FS-Cache: Fix loc...
124
125
126
127
128
129
  	spin_lock(&object->lock);
  	cookie = object->cookie;
  	if (cookie) {
  		/* delete the page from the tree if it is now no longer
  		 * pending */
  		spin_lock(&cookie->stores_lock);
201a15428   David Howells   FS-Cache: Handle ...
130
131
  		radix_tree_tag_clear(&cookie->stores, page->index,
  				     FSCACHE_COOKIE_STORING_TAG);
285e728b0   David Howells   FS-Cache: Don't d...
132
133
134
135
136
  		if (!radix_tree_tag_get(&cookie->stores, page->index,
  					FSCACHE_COOKIE_PENDING_TAG)) {
  			fscache_stat(&fscache_n_store_radix_deletes);
  			xpage = radix_tree_delete(&cookie->stores, page->index);
  		}
1bccf513a   David Howells   FS-Cache: Fix loc...
137
138
139
140
141
142
  		spin_unlock(&cookie->stores_lock);
  		wake_up_bit(&cookie->flags, 0);
  	}
  	spin_unlock(&object->lock);
  	if (xpage)
  		page_cache_release(xpage);
b51088228   David Howells   FS-Cache: Impleme...
143
144
145
146
147
148
149
150
  }
  
  /*
   * actually apply the changed attributes to a cache object
   */
  static void fscache_attr_changed_op(struct fscache_operation *op)
  {
  	struct fscache_object *object = op->object;
440f0affe   David Howells   FS-Cache: Annotat...
151
  	int ret;
b51088228   David Howells   FS-Cache: Impleme...
152
153
154
155
  
  	_enter("{OBJ%x OP%x}", object->debug_id, op->debug_id);
  
  	fscache_stat(&fscache_n_attr_changed_calls);
440f0affe   David Howells   FS-Cache: Annotat...
156
  	if (fscache_object_is_active(object)) {
52bd75fdb   David Howells   FS-Cache: Add cou...
157
  		fscache_stat(&fscache_n_cop_attr_changed);
440f0affe   David Howells   FS-Cache: Annotat...
158
  		ret = object->cache->ops->attr_changed(object);
52bd75fdb   David Howells   FS-Cache: Add cou...
159
  		fscache_stat_d(&fscache_n_cop_attr_changed);
440f0affe   David Howells   FS-Cache: Annotat...
160
161
162
  		if (ret < 0)
  			fscache_abort_object(object);
  	}
b51088228   David Howells   FS-Cache: Impleme...
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
  
  	_leave("");
  }
  
  /*
   * notification that the attributes on an object have changed
   */
  int __fscache_attr_changed(struct fscache_cookie *cookie)
  {
  	struct fscache_operation *op;
  	struct fscache_object *object;
  
  	_enter("%p", cookie);
  
  	ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
  
  	fscache_stat(&fscache_n_attr_changed);
  
  	op = kzalloc(sizeof(*op), GFP_KERNEL);
  	if (!op) {
  		fscache_stat(&fscache_n_attr_changed_nomem);
  		_leave(" = -ENOMEM");
  		return -ENOMEM;
  	}
8af7c1243   Tejun Heo   fscache: convert ...
187
188
  	fscache_operation_init(op, fscache_attr_changed_op, NULL);
  	op->flags = FSCACHE_OP_ASYNC | (1 << FSCACHE_OP_EXCLUSIVE);
b51088228   David Howells   FS-Cache: Impleme...
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
  
  	spin_lock(&cookie->lock);
  
  	if (hlist_empty(&cookie->backing_objects))
  		goto nobufs;
  	object = hlist_entry(cookie->backing_objects.first,
  			     struct fscache_object, cookie_link);
  
  	if (fscache_submit_exclusive_op(object, op) < 0)
  		goto nobufs;
  	spin_unlock(&cookie->lock);
  	fscache_stat(&fscache_n_attr_changed_ok);
  	fscache_put_operation(op);
  	_leave(" = 0");
  	return 0;
  
  nobufs:
  	spin_unlock(&cookie->lock);
  	kfree(op);
  	fscache_stat(&fscache_n_attr_changed_nobufs);
  	_leave(" = %d", -ENOBUFS);
  	return -ENOBUFS;
  }
  EXPORT_SYMBOL(__fscache_attr_changed);
  
  /*
b51088228   David Howells   FS-Cache: Impleme...
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
   * release a retrieval op reference
   */
  static void fscache_release_retrieval_op(struct fscache_operation *_op)
  {
  	struct fscache_retrieval *op =
  		container_of(_op, struct fscache_retrieval, op);
  
  	_enter("{OP%x}", op->op.debug_id);
  
  	fscache_hist(fscache_retrieval_histogram, op->start_time);
  	if (op->context)
  		fscache_put_context(op->op.object->cookie, op->context);
  
  	_leave("");
  }
  
  /*
   * allocate a retrieval op
   */
  static struct fscache_retrieval *fscache_alloc_retrieval(
  	struct address_space *mapping,
  	fscache_rw_complete_t end_io_func,
  	void *context)
  {
  	struct fscache_retrieval *op;
  
  	/* allocate a retrieval operation and attempt to submit it */
  	op = kzalloc(sizeof(*op), GFP_NOIO);
  	if (!op) {
  		fscache_stat(&fscache_n_retrievals_nomem);
  		return NULL;
  	}
8af7c1243   Tejun Heo   fscache: convert ...
247
  	fscache_operation_init(&op->op, NULL, fscache_release_retrieval_op);
b51088228   David Howells   FS-Cache: Impleme...
248
249
250
251
252
  	op->op.flags	= FSCACHE_OP_MYTHREAD | (1 << FSCACHE_OP_WAITING);
  	op->mapping	= mapping;
  	op->end_io_func	= end_io_func;
  	op->context	= context;
  	op->start_time	= jiffies;
b51088228   David Howells   FS-Cache: Impleme...
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
  	INIT_LIST_HEAD(&op->to_do);
  	return op;
  }
  
  /*
   * wait for a deferred lookup to complete
   */
  static int fscache_wait_for_deferred_lookup(struct fscache_cookie *cookie)
  {
  	unsigned long jif;
  
  	_enter("");
  
  	if (!test_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags)) {
  		_leave(" = 0 [imm]");
  		return 0;
  	}
  
  	fscache_stat(&fscache_n_retrievals_wait);
  
  	jif = jiffies;
  	if (wait_on_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP,
  			fscache_wait_bit_interruptible,
  			TASK_INTERRUPTIBLE) != 0) {
  		fscache_stat(&fscache_n_retrievals_intr);
  		_leave(" = -ERESTARTSYS");
  		return -ERESTARTSYS;
  	}
  
  	ASSERT(!test_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags));
  
  	smp_rmb();
  	fscache_hist(fscache_retrieval_delay_histogram, jif);
  	_leave(" = 0 [dly]");
  	return 0;
  }
  
  /*
60d543ca7   David Howells   FS-Cache: Start p...
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
   * wait for an object to become active (or dead)
   */
  static int fscache_wait_for_retrieval_activation(struct fscache_object *object,
  						 struct fscache_retrieval *op,
  						 atomic_t *stat_op_waits,
  						 atomic_t *stat_object_dead)
  {
  	int ret;
  
  	if (!test_bit(FSCACHE_OP_WAITING, &op->op.flags))
  		goto check_if_dead;
  
  	_debug(">>> WT");
  	fscache_stat(stat_op_waits);
  	if (wait_on_bit(&op->op.flags, FSCACHE_OP_WAITING,
  			fscache_wait_bit_interruptible,
  			TASK_INTERRUPTIBLE) < 0) {
  		ret = fscache_cancel_op(&op->op);
  		if (ret == 0)
  			return -ERESTARTSYS;
  
  		/* it's been removed from the pending queue by another party,
  		 * so we should get to run shortly */
  		wait_on_bit(&op->op.flags, FSCACHE_OP_WAITING,
  			    fscache_wait_bit, TASK_UNINTERRUPTIBLE);
  	}
  	_debug("<<< GO");
  
  check_if_dead:
  	if (unlikely(fscache_object_is_dead(object))) {
  		fscache_stat(stat_object_dead);
  		return -ENOBUFS;
  	}
  	return 0;
  }
  
  /*
b51088228   David Howells   FS-Cache: Impleme...
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
   * read a page from the cache or allocate a block in which to store it
   * - we return:
   *   -ENOMEM	- out of memory, nothing done
   *   -ERESTARTSYS - interrupted
   *   -ENOBUFS	- no backing object available in which to cache the block
   *   -ENODATA	- no data available in the backing object for this block
   *   0		- dispatched a read - it'll call end_io_func() when finished
   */
  int __fscache_read_or_alloc_page(struct fscache_cookie *cookie,
  				 struct page *page,
  				 fscache_rw_complete_t end_io_func,
  				 void *context,
  				 gfp_t gfp)
  {
  	struct fscache_retrieval *op;
  	struct fscache_object *object;
  	int ret;
  
  	_enter("%p,%p,,,", cookie, page);
  
  	fscache_stat(&fscache_n_retrievals);
  
  	if (hlist_empty(&cookie->backing_objects))
  		goto nobufs;
  
  	ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
  	ASSERTCMP(page, !=, NULL);
  
  	if (fscache_wait_for_deferred_lookup(cookie) < 0)
  		return -ERESTARTSYS;
  
  	op = fscache_alloc_retrieval(page->mapping, end_io_func, context);
  	if (!op) {
  		_leave(" = -ENOMEM");
  		return -ENOMEM;
  	}
  
  	spin_lock(&cookie->lock);
  
  	if (hlist_empty(&cookie->backing_objects))
  		goto nobufs_unlock;
  	object = hlist_entry(cookie->backing_objects.first,
  			     struct fscache_object, cookie_link);
  
  	ASSERTCMP(object->state, >, FSCACHE_OBJECT_LOOKING_UP);
4fbf4291a   David Howells   FS-Cache: Allow t...
373
374
  	atomic_inc(&object->n_reads);
  	set_bit(FSCACHE_OP_DEC_READ_CNT, &op->op.flags);
b51088228   David Howells   FS-Cache: Impleme...
375
376
377
378
379
380
381
382
383
384
385
386
  	if (fscache_submit_op(object, &op->op) < 0)
  		goto nobufs_unlock;
  	spin_unlock(&cookie->lock);
  
  	fscache_stat(&fscache_n_retrieval_ops);
  
  	/* pin the netfs read context in case we need to do the actual netfs
  	 * read because we've encountered a cache read failure */
  	fscache_get_context(object->cookie, op->context);
  
  	/* we wait for the operation to become active, and then process it
  	 * *here*, in this thread, and not in the thread pool */
60d543ca7   David Howells   FS-Cache: Start p...
387
388
389
390
391
392
  	ret = fscache_wait_for_retrieval_activation(
  		object, op,
  		__fscache_stat(&fscache_n_retrieval_op_waits),
  		__fscache_stat(&fscache_n_retrievals_object_dead));
  	if (ret < 0)
  		goto error;
b51088228   David Howells   FS-Cache: Impleme...
393
394
395
  
  	/* ask the cache to honour the operation */
  	if (test_bit(FSCACHE_COOKIE_NO_DATA_YET, &object->cookie->flags)) {
52bd75fdb   David Howells   FS-Cache: Add cou...
396
  		fscache_stat(&fscache_n_cop_allocate_page);
b51088228   David Howells   FS-Cache: Impleme...
397
  		ret = object->cache->ops->allocate_page(op, page, gfp);
52bd75fdb   David Howells   FS-Cache: Add cou...
398
  		fscache_stat_d(&fscache_n_cop_allocate_page);
b51088228   David Howells   FS-Cache: Impleme...
399
400
401
  		if (ret == 0)
  			ret = -ENODATA;
  	} else {
52bd75fdb   David Howells   FS-Cache: Add cou...
402
  		fscache_stat(&fscache_n_cop_read_or_alloc_page);
b51088228   David Howells   FS-Cache: Impleme...
403
  		ret = object->cache->ops->read_or_alloc_page(op, page, gfp);
52bd75fdb   David Howells   FS-Cache: Add cou...
404
  		fscache_stat_d(&fscache_n_cop_read_or_alloc_page);
b51088228   David Howells   FS-Cache: Impleme...
405
  	}
5753c4418   David Howells   FS-Cache: Permit ...
406
  error:
b51088228   David Howells   FS-Cache: Impleme...
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
  	if (ret == -ENOMEM)
  		fscache_stat(&fscache_n_retrievals_nomem);
  	else if (ret == -ERESTARTSYS)
  		fscache_stat(&fscache_n_retrievals_intr);
  	else if (ret == -ENODATA)
  		fscache_stat(&fscache_n_retrievals_nodata);
  	else if (ret < 0)
  		fscache_stat(&fscache_n_retrievals_nobufs);
  	else
  		fscache_stat(&fscache_n_retrievals_ok);
  
  	fscache_put_retrieval(op);
  	_leave(" = %d", ret);
  	return ret;
  
  nobufs_unlock:
  	spin_unlock(&cookie->lock);
  	kfree(op);
  nobufs:
  	fscache_stat(&fscache_n_retrievals_nobufs);
  	_leave(" = -ENOBUFS");
  	return -ENOBUFS;
  }
  EXPORT_SYMBOL(__fscache_read_or_alloc_page);
  
  /*
   * read a list of page from the cache or allocate a block in which to store
   * them
   * - we return:
   *   -ENOMEM	- out of memory, some pages may be being read
   *   -ERESTARTSYS - interrupted, some pages may be being read
   *   -ENOBUFS	- no backing object or space available in which to cache any
   *                pages not being read
   *   -ENODATA	- no data available in the backing object for some or all of
   *                the pages
   *   0		- dispatched a read on all pages
   *
   * end_io_func() will be called for each page read from the cache as it is
   * finishes being read
   *
   * any pages for which a read is dispatched will be removed from pages and
   * nr_pages
   */
  int __fscache_read_or_alloc_pages(struct fscache_cookie *cookie,
  				  struct address_space *mapping,
  				  struct list_head *pages,
  				  unsigned *nr_pages,
  				  fscache_rw_complete_t end_io_func,
  				  void *context,
  				  gfp_t gfp)
  {
b51088228   David Howells   FS-Cache: Impleme...
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
  	struct fscache_retrieval *op;
  	struct fscache_object *object;
  	int ret;
  
  	_enter("%p,,%d,,,", cookie, *nr_pages);
  
  	fscache_stat(&fscache_n_retrievals);
  
  	if (hlist_empty(&cookie->backing_objects))
  		goto nobufs;
  
  	ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
  	ASSERTCMP(*nr_pages, >, 0);
  	ASSERT(!list_empty(pages));
  
  	if (fscache_wait_for_deferred_lookup(cookie) < 0)
  		return -ERESTARTSYS;
  
  	op = fscache_alloc_retrieval(mapping, end_io_func, context);
  	if (!op)
  		return -ENOMEM;
  
  	spin_lock(&cookie->lock);
  
  	if (hlist_empty(&cookie->backing_objects))
  		goto nobufs_unlock;
  	object = hlist_entry(cookie->backing_objects.first,
  			     struct fscache_object, cookie_link);
4fbf4291a   David Howells   FS-Cache: Allow t...
486
487
  	atomic_inc(&object->n_reads);
  	set_bit(FSCACHE_OP_DEC_READ_CNT, &op->op.flags);
b51088228   David Howells   FS-Cache: Impleme...
488
489
490
491
492
493
494
495
496
497
498
499
  	if (fscache_submit_op(object, &op->op) < 0)
  		goto nobufs_unlock;
  	spin_unlock(&cookie->lock);
  
  	fscache_stat(&fscache_n_retrieval_ops);
  
  	/* pin the netfs read context in case we need to do the actual netfs
  	 * read because we've encountered a cache read failure */
  	fscache_get_context(object->cookie, op->context);
  
  	/* we wait for the operation to become active, and then process it
  	 * *here*, in this thread, and not in the thread pool */
60d543ca7   David Howells   FS-Cache: Start p...
500
501
502
503
504
505
  	ret = fscache_wait_for_retrieval_activation(
  		object, op,
  		__fscache_stat(&fscache_n_retrieval_op_waits),
  		__fscache_stat(&fscache_n_retrievals_object_dead));
  	if (ret < 0)
  		goto error;
b51088228   David Howells   FS-Cache: Impleme...
506
507
  
  	/* ask the cache to honour the operation */
52bd75fdb   David Howells   FS-Cache: Add cou...
508
509
510
511
512
513
514
515
516
517
518
  	if (test_bit(FSCACHE_COOKIE_NO_DATA_YET, &object->cookie->flags)) {
  		fscache_stat(&fscache_n_cop_allocate_pages);
  		ret = object->cache->ops->allocate_pages(
  			op, pages, nr_pages, gfp);
  		fscache_stat_d(&fscache_n_cop_allocate_pages);
  	} else {
  		fscache_stat(&fscache_n_cop_read_or_alloc_pages);
  		ret = object->cache->ops->read_or_alloc_pages(
  			op, pages, nr_pages, gfp);
  		fscache_stat_d(&fscache_n_cop_read_or_alloc_pages);
  	}
b51088228   David Howells   FS-Cache: Impleme...
519

5753c4418   David Howells   FS-Cache: Permit ...
520
  error:
b51088228   David Howells   FS-Cache: Impleme...
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
  	if (ret == -ENOMEM)
  		fscache_stat(&fscache_n_retrievals_nomem);
  	else if (ret == -ERESTARTSYS)
  		fscache_stat(&fscache_n_retrievals_intr);
  	else if (ret == -ENODATA)
  		fscache_stat(&fscache_n_retrievals_nodata);
  	else if (ret < 0)
  		fscache_stat(&fscache_n_retrievals_nobufs);
  	else
  		fscache_stat(&fscache_n_retrievals_ok);
  
  	fscache_put_retrieval(op);
  	_leave(" = %d", ret);
  	return ret;
  
  nobufs_unlock:
  	spin_unlock(&cookie->lock);
  	kfree(op);
  nobufs:
  	fscache_stat(&fscache_n_retrievals_nobufs);
  	_leave(" = -ENOBUFS");
  	return -ENOBUFS;
  }
  EXPORT_SYMBOL(__fscache_read_or_alloc_pages);
  
  /*
   * allocate a block in the cache on which to store a page
   * - we return:
   *   -ENOMEM	- out of memory, nothing done
   *   -ERESTARTSYS - interrupted
   *   -ENOBUFS	- no backing object available in which to cache the block
   *   0		- block allocated
   */
  int __fscache_alloc_page(struct fscache_cookie *cookie,
  			 struct page *page,
  			 gfp_t gfp)
  {
  	struct fscache_retrieval *op;
  	struct fscache_object *object;
  	int ret;
  
  	_enter("%p,%p,,,", cookie, page);
  
  	fscache_stat(&fscache_n_allocs);
  
  	if (hlist_empty(&cookie->backing_objects))
  		goto nobufs;
  
  	ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
  	ASSERTCMP(page, !=, NULL);
  
  	if (fscache_wait_for_deferred_lookup(cookie) < 0)
  		return -ERESTARTSYS;
  
  	op = fscache_alloc_retrieval(page->mapping, NULL, NULL);
  	if (!op)
  		return -ENOMEM;
  
  	spin_lock(&cookie->lock);
  
  	if (hlist_empty(&cookie->backing_objects))
  		goto nobufs_unlock;
  	object = hlist_entry(cookie->backing_objects.first,
  			     struct fscache_object, cookie_link);
  
  	if (fscache_submit_op(object, &op->op) < 0)
  		goto nobufs_unlock;
  	spin_unlock(&cookie->lock);
  
  	fscache_stat(&fscache_n_alloc_ops);
60d543ca7   David Howells   FS-Cache: Start p...
591
592
593
594
595
596
  	ret = fscache_wait_for_retrieval_activation(
  		object, op,
  		__fscache_stat(&fscache_n_alloc_op_waits),
  		__fscache_stat(&fscache_n_allocs_object_dead));
  	if (ret < 0)
  		goto error;
b51088228   David Howells   FS-Cache: Impleme...
597
598
  
  	/* ask the cache to honour the operation */
52bd75fdb   David Howells   FS-Cache: Add cou...
599
  	fscache_stat(&fscache_n_cop_allocate_page);
b51088228   David Howells   FS-Cache: Impleme...
600
  	ret = object->cache->ops->allocate_page(op, page, gfp);
52bd75fdb   David Howells   FS-Cache: Add cou...
601
  	fscache_stat_d(&fscache_n_cop_allocate_page);
b51088228   David Howells   FS-Cache: Impleme...
602

5753c4418   David Howells   FS-Cache: Permit ...
603
604
605
606
  error:
  	if (ret == -ERESTARTSYS)
  		fscache_stat(&fscache_n_allocs_intr);
  	else if (ret < 0)
b51088228   David Howells   FS-Cache: Impleme...
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
  		fscache_stat(&fscache_n_allocs_nobufs);
  	else
  		fscache_stat(&fscache_n_allocs_ok);
  
  	fscache_put_retrieval(op);
  	_leave(" = %d", ret);
  	return ret;
  
  nobufs_unlock:
  	spin_unlock(&cookie->lock);
  	kfree(op);
  nobufs:
  	fscache_stat(&fscache_n_allocs_nobufs);
  	_leave(" = -ENOBUFS");
  	return -ENOBUFS;
  }
  EXPORT_SYMBOL(__fscache_alloc_page);
  
  /*
   * release a write op reference
   */
  static void fscache_release_write_op(struct fscache_operation *_op)
  {
  	_enter("{OP%x}", _op->debug_id);
  }
  
  /*
   * perform the background storage of a page into the cache
   */
  static void fscache_write_op(struct fscache_operation *_op)
  {
  	struct fscache_storage *op =
  		container_of(_op, struct fscache_storage, op);
  	struct fscache_object *object = op->op.object;
1bccf513a   David Howells   FS-Cache: Fix loc...
641
  	struct fscache_cookie *cookie;
b51088228   David Howells   FS-Cache: Impleme...
642
643
644
645
646
647
  	struct page *page;
  	unsigned n;
  	void *results[1];
  	int ret;
  
  	_enter("{OP%x,%d}", op->op.debug_id, atomic_read(&op->op.usage));
b51088228   David Howells   FS-Cache: Impleme...
648
  	spin_lock(&object->lock);
1bccf513a   David Howells   FS-Cache: Fix loc...
649
  	cookie = object->cookie;
b51088228   David Howells   FS-Cache: Impleme...
650

1bccf513a   David Howells   FS-Cache: Fix loc...
651
  	if (!fscache_object_is_active(object) || !cookie) {
b51088228   David Howells   FS-Cache: Impleme...
652
  		spin_unlock(&object->lock);
b51088228   David Howells   FS-Cache: Impleme...
653
654
655
  		_leave("");
  		return;
  	}
1bccf513a   David Howells   FS-Cache: Fix loc...
656
  	spin_lock(&cookie->stores_lock);
b51088228   David Howells   FS-Cache: Impleme...
657
658
659
660
661
662
663
664
665
666
  	fscache_stat(&fscache_n_store_calls);
  
  	/* find a page to store */
  	page = NULL;
  	n = radix_tree_gang_lookup_tag(&cookie->stores, results, 0, 1,
  				       FSCACHE_COOKIE_PENDING_TAG);
  	if (n != 1)
  		goto superseded;
  	page = results[0];
  	_debug("gang %d [%lx]", n, page->index);
1bccf513a   David Howells   FS-Cache: Fix loc...
667
668
  	if (page->index > op->store_limit) {
  		fscache_stat(&fscache_n_store_pages_over_limit);
b51088228   David Howells   FS-Cache: Impleme...
669
  		goto superseded;
1bccf513a   David Howells   FS-Cache: Fix loc...
670
  	}
b51088228   David Howells   FS-Cache: Impleme...
671

08a66859e   Dan Carpenter   FS-Cache: Remove ...
672
673
674
675
  	radix_tree_tag_set(&cookie->stores, page->index,
  			   FSCACHE_COOKIE_STORING_TAG);
  	radix_tree_tag_clear(&cookie->stores, page->index,
  			     FSCACHE_COOKIE_PENDING_TAG);
b51088228   David Howells   FS-Cache: Impleme...
676

1bccf513a   David Howells   FS-Cache: Fix loc...
677
  	spin_unlock(&cookie->stores_lock);
b51088228   David Howells   FS-Cache: Impleme...
678
  	spin_unlock(&object->lock);
b51088228   David Howells   FS-Cache: Impleme...
679

08a66859e   Dan Carpenter   FS-Cache: Remove ...
680
681
682
683
  	fscache_stat(&fscache_n_store_pages);
  	fscache_stat(&fscache_n_cop_write_page);
  	ret = object->cache->ops->write_page(op, page);
  	fscache_stat_d(&fscache_n_cop_write_page);
08a66859e   Dan Carpenter   FS-Cache: Remove ...
684
685
  	fscache_end_page_write(object, page);
  	if (ret < 0) {
08a66859e   Dan Carpenter   FS-Cache: Remove ...
686
687
688
  		fscache_abort_object(object);
  	} else {
  		fscache_enqueue_operation(&op->op);
b51088228   David Howells   FS-Cache: Impleme...
689
690
691
692
693
694
695
696
697
  	}
  
  	_leave("");
  	return;
  
  superseded:
  	/* this writer is going away and there aren't any more things to
  	 * write */
  	_debug("cease");
1bccf513a   David Howells   FS-Cache: Fix loc...
698
  	spin_unlock(&cookie->stores_lock);
b51088228   David Howells   FS-Cache: Impleme...
699
700
  	clear_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags);
  	spin_unlock(&object->lock);
b51088228   David Howells   FS-Cache: Impleme...
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
  	_leave("");
  }
  
  /*
   * request a page be stored in the cache
   * - returns:
   *   -ENOMEM	- out of memory, nothing done
   *   -ENOBUFS	- no backing object available in which to cache the page
   *   0		- dispatched a write - it'll call end_io_func() when finished
   *
   * if the cookie still has a backing object at this point, that object can be
   * in one of a few states with respect to storage processing:
   *
   *  (1) negative lookup, object not yet created (FSCACHE_COOKIE_CREATING is
   *      set)
   *
   *	(a) no writes yet (set FSCACHE_COOKIE_PENDING_FILL and queue deferred
   *	    fill op)
   *
   *	(b) writes deferred till post-creation (mark page for writing and
   *	    return immediately)
   *
   *  (2) negative lookup, object created, initial fill being made from netfs
   *      (FSCACHE_COOKIE_INITIAL_FILL is set)
   *
   *	(a) fill point not yet reached this page (mark page for writing and
   *          return)
   *
   *	(b) fill point passed this page (queue op to store this page)
   *
   *  (3) object extant (queue op to store this page)
   *
   * any other state is invalid
   */
  int __fscache_write_page(struct fscache_cookie *cookie,
  			 struct page *page,
  			 gfp_t gfp)
  {
  	struct fscache_storage *op;
  	struct fscache_object *object;
  	int ret;
  
  	_enter("%p,%x,", cookie, (u32) page->flags);
  
  	ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
  	ASSERT(PageFsCache(page));
  
  	fscache_stat(&fscache_n_stores);
  
  	op = kzalloc(sizeof(*op), GFP_NOIO);
  	if (!op)
  		goto nomem;
8af7c1243   Tejun Heo   fscache: convert ...
753
754
755
  	fscache_operation_init(&op->op, fscache_write_op,
  			       fscache_release_write_op);
  	op->op.flags = FSCACHE_OP_ASYNC | (1 << FSCACHE_OP_WAITING);
b51088228   David Howells   FS-Cache: Impleme...
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
  
  	ret = radix_tree_preload(gfp & ~__GFP_HIGHMEM);
  	if (ret < 0)
  		goto nomem_free;
  
  	ret = -ENOBUFS;
  	spin_lock(&cookie->lock);
  
  	if (hlist_empty(&cookie->backing_objects))
  		goto nobufs;
  	object = hlist_entry(cookie->backing_objects.first,
  			     struct fscache_object, cookie_link);
  	if (test_bit(FSCACHE_IOERROR, &object->cache->flags))
  		goto nobufs;
  
  	/* add the page to the pending-storage radix tree on the backing
  	 * object */
  	spin_lock(&object->lock);
1bccf513a   David Howells   FS-Cache: Fix loc...
774
  	spin_lock(&cookie->stores_lock);
b51088228   David Howells   FS-Cache: Impleme...
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
  
  	_debug("store limit %llx", (unsigned long long) object->store_limit);
  
  	ret = radix_tree_insert(&cookie->stores, page->index, page);
  	if (ret < 0) {
  		if (ret == -EEXIST)
  			goto already_queued;
  		_debug("insert failed %d", ret);
  		goto nobufs_unlock_obj;
  	}
  
  	radix_tree_tag_set(&cookie->stores, page->index,
  			   FSCACHE_COOKIE_PENDING_TAG);
  	page_cache_get(page);
  
  	/* we only want one writer at a time, but we do need to queue new
  	 * writers after exclusive ops */
  	if (test_and_set_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags))
  		goto already_pending;
1bccf513a   David Howells   FS-Cache: Fix loc...
794
  	spin_unlock(&cookie->stores_lock);
b51088228   David Howells   FS-Cache: Impleme...
795
796
797
798
799
800
801
802
803
804
805
806
  	spin_unlock(&object->lock);
  
  	op->op.debug_id	= atomic_inc_return(&fscache_op_debug_id);
  	op->store_limit = object->store_limit;
  
  	if (fscache_submit_op(object, &op->op) < 0)
  		goto submit_failed;
  
  	spin_unlock(&cookie->lock);
  	radix_tree_preload_end();
  	fscache_stat(&fscache_n_store_ops);
  	fscache_stat(&fscache_n_stores_ok);
8af7c1243   Tejun Heo   fscache: convert ...
807
  	/* the work queue now carries its own ref on the object */
b51088228   David Howells   FS-Cache: Impleme...
808
809
810
811
812
813
814
  	fscache_put_operation(&op->op);
  	_leave(" = 0");
  	return 0;
  
  already_queued:
  	fscache_stat(&fscache_n_stores_again);
  already_pending:
1bccf513a   David Howells   FS-Cache: Fix loc...
815
  	spin_unlock(&cookie->stores_lock);
b51088228   David Howells   FS-Cache: Impleme...
816
817
818
819
820
821
822
823
824
  	spin_unlock(&object->lock);
  	spin_unlock(&cookie->lock);
  	radix_tree_preload_end();
  	kfree(op);
  	fscache_stat(&fscache_n_stores_ok);
  	_leave(" = 0");
  	return 0;
  
  submit_failed:
1bccf513a   David Howells   FS-Cache: Fix loc...
825
  	spin_lock(&cookie->stores_lock);
b51088228   David Howells   FS-Cache: Impleme...
826
  	radix_tree_delete(&cookie->stores, page->index);
1bccf513a   David Howells   FS-Cache: Fix loc...
827
  	spin_unlock(&cookie->stores_lock);
b51088228   David Howells   FS-Cache: Impleme...
828
829
830
831
832
  	page_cache_release(page);
  	ret = -ENOBUFS;
  	goto nobufs;
  
  nobufs_unlock_obj:
1147d0f91   Dan Carpenter   fscache: add miss...
833
  	spin_unlock(&cookie->stores_lock);
b51088228   David Howells   FS-Cache: Impleme...
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
  	spin_unlock(&object->lock);
  nobufs:
  	spin_unlock(&cookie->lock);
  	radix_tree_preload_end();
  	kfree(op);
  	fscache_stat(&fscache_n_stores_nobufs);
  	_leave(" = -ENOBUFS");
  	return -ENOBUFS;
  
  nomem_free:
  	kfree(op);
  nomem:
  	fscache_stat(&fscache_n_stores_oom);
  	_leave(" = -ENOMEM");
  	return -ENOMEM;
  }
  EXPORT_SYMBOL(__fscache_write_page);
  
  /*
   * remove a page from the cache
   */
  void __fscache_uncache_page(struct fscache_cookie *cookie, struct page *page)
  {
  	struct fscache_object *object;
  
  	_enter(",%p", page);
  
  	ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
  	ASSERTCMP(page, !=, NULL);
  
  	fscache_stat(&fscache_n_uncaches);
  
  	/* cache withdrawal may beat us to it */
  	if (!PageFsCache(page))
  		goto done;
  
  	/* get the object */
  	spin_lock(&cookie->lock);
  
  	if (hlist_empty(&cookie->backing_objects)) {
  		ClearPageFsCache(page);
  		goto done_unlock;
  	}
  
  	object = hlist_entry(cookie->backing_objects.first,
  			     struct fscache_object, cookie_link);
  
  	/* there might now be stuff on disk we could read */
  	clear_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
  
  	/* only invoke the cache backend if we managed to mark the page
  	 * uncached here; this deals with synchronisation vs withdrawal */
  	if (TestClearPageFsCache(page) &&
  	    object->cache->ops->uncache_page) {
  		/* the cache backend releases the cookie lock */
52bd75fdb   David Howells   FS-Cache: Add cou...
889
  		fscache_stat(&fscache_n_cop_uncache_page);
b51088228   David Howells   FS-Cache: Impleme...
890
  		object->cache->ops->uncache_page(object, page);
52bd75fdb   David Howells   FS-Cache: Add cou...
891
  		fscache_stat_d(&fscache_n_cop_uncache_page);
b51088228   David Howells   FS-Cache: Impleme...
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
  		goto done;
  	}
  
  done_unlock:
  	spin_unlock(&cookie->lock);
  done:
  	_leave("");
  }
  EXPORT_SYMBOL(__fscache_uncache_page);
  
  /**
   * fscache_mark_pages_cached - Mark pages as being cached
   * @op: The retrieval op pages are being marked for
   * @pagevec: The pages to be marked
   *
   * Mark a bunch of netfs pages as being cached.  After this is called,
   * the netfs must call fscache_uncache_page() to remove the mark.
   */
  void fscache_mark_pages_cached(struct fscache_retrieval *op,
  			       struct pagevec *pagevec)
  {
  	struct fscache_cookie *cookie = op->op.object->cookie;
  	unsigned long loop;
  
  #ifdef CONFIG_FSCACHE_STATS
  	atomic_add(pagevec->nr, &fscache_n_marks);
  #endif
  
  	for (loop = 0; loop < pagevec->nr; loop++) {
  		struct page *page = pagevec->pages[loop];
  
  		_debug("- mark %p{%lx}", page, page->index);
  		if (TestSetPageFsCache(page)) {
  			static bool once_only;
  			if (!once_only) {
  				once_only = true;
  				printk(KERN_WARNING "FS-Cache:"
  				       " Cookie type %s marked page %lx"
  				       " multiple times
  ",
  				       cookie->def->name, page->index);
  			}
  		}
  	}
  
  	if (cookie->def->mark_pages_cached)
  		cookie->def->mark_pages_cached(cookie->netfs_data,
  					       op->mapping, pagevec);
  	pagevec_reinit(pagevec);
  }
  EXPORT_SYMBOL(fscache_mark_pages_cached);
c902ce1bf   David Howells   FS-Cache: Add a h...
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
  
  /*
   * Uncache all the pages in an inode that are marked PG_fscache, assuming them
   * to be associated with the given cookie.
   */
  void __fscache_uncache_all_inode_pages(struct fscache_cookie *cookie,
  				       struct inode *inode)
  {
  	struct address_space *mapping = inode->i_mapping;
  	struct pagevec pvec;
  	pgoff_t next;
  	int i;
  
  	_enter("%p,%p", cookie, inode);
  
  	if (!mapping || mapping->nrpages == 0) {
  		_leave(" [no pages]");
  		return;
  	}
  
  	pagevec_init(&pvec, 0);
  	next = 0;
b307d4655   Jan Beulich   FS-Cache: Fix __f...
965
966
967
  	do {
  		if (!pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE))
  			break;
c902ce1bf   David Howells   FS-Cache: Add a h...
968
969
  		for (i = 0; i < pagevec_count(&pvec); i++) {
  			struct page *page = pvec.pages[i];
b307d4655   Jan Beulich   FS-Cache: Fix __f...
970
  			next = page->index;
c902ce1bf   David Howells   FS-Cache: Add a h...
971
972
973
974
975
976
977
  			if (PageFsCache(page)) {
  				__fscache_wait_on_page_write(cookie, page);
  				__fscache_uncache_page(cookie, page);
  			}
  		}
  		pagevec_release(&pvec);
  		cond_resched();
b307d4655   Jan Beulich   FS-Cache: Fix __f...
978
  	} while (++next);
c902ce1bf   David Howells   FS-Cache: Add a h...
979
980
981
982
  
  	_leave("");
  }
  EXPORT_SYMBOL(__fscache_uncache_all_inode_pages);