Blame view

fs/fscache/page.c 31 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);
  
  /*
9776de96e   Milosz Tanski   FS-Cache: Timeout...
47
48
49
50
51
52
53
54
55
56
57
58
59
   * wait for a page to finish being written to the cache. Put a timeout here
   * since we might be called recursively via parent fs.
   */
  static
  bool release_page_wait_timeout(struct fscache_cookie *cookie, struct page *page)
  {
  	wait_queue_head_t *wq = bit_waitqueue(&cookie->flags, 0);
  
  	return wait_event_timeout(*wq, !__fscache_check_page_write(cookie, page),
  				  HZ);
  }
  
  /*
201a15428   David Howells   FS-Cache: Handle ...
60
   * decide whether a page can be released, possibly by cancelling a store to it
d0164adc8   Mel Gorman   mm, page_alloc: d...
61
   * - we're allowed to sleep if __GFP_DIRECT_RECLAIM is flagged
201a15428   David Howells   FS-Cache: Handle ...
62
63
64
65
66
67
68
69
70
   */
  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);
8c209ce72   David Howells   NFS: nfs_migrate_...
71
  try_again:
201a15428   David Howells   FS-Cache: Handle ...
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
108
109
110
111
112
113
114
  	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)
09cbfeaf1   Kirill A. Shutemov   mm, fs: get rid o...
115
  		put_page(xpage);
201a15428   David Howells   FS-Cache: Handle ...
116
117
118
119
  	__fscache_uncache_page(cookie, page);
  	return true;
  
  page_busy:
8c209ce72   David Howells   NFS: nfs_migrate_...
120
121
122
123
  	/* We will wait here if we're allowed to, but that could deadlock the
  	 * allocator as the work threads writing to the cache may all end up
  	 * sleeping on memory allocation, so we may need to impose a timeout
  	 * too. */
d0164adc8   Mel Gorman   mm, page_alloc: d...
124
  	if (!(gfp & __GFP_DIRECT_RECLAIM) || !(gfp & __GFP_FS)) {
8c209ce72   David Howells   NFS: nfs_migrate_...
125
126
127
128
129
  		fscache_stat(&fscache_n_store_vmscan_busy);
  		return false;
  	}
  
  	fscache_stat(&fscache_n_store_vmscan_wait);
9776de96e   Milosz Tanski   FS-Cache: Timeout...
130
131
132
  	if (!release_page_wait_timeout(cookie, page))
  		_debug("fscache writeout timeout page: %p{%lx}",
  			page, page->index);
d0164adc8   Mel Gorman   mm, page_alloc: d...
133
  	gfp &= ~__GFP_DIRECT_RECLAIM;
8c209ce72   David Howells   NFS: nfs_migrate_...
134
  	goto try_again;
201a15428   David Howells   FS-Cache: Handle ...
135
136
137
138
  }
  EXPORT_SYMBOL(__fscache_maybe_release_page);
  
  /*
b51088228   David Howells   FS-Cache: Impleme...
139
140
   * note that a page has finished being written to the cache
   */
1bccf513a   David Howells   FS-Cache: Fix loc...
141
142
  static void fscache_end_page_write(struct fscache_object *object,
  				   struct page *page)
b51088228   David Howells   FS-Cache: Impleme...
143
  {
1bccf513a   David Howells   FS-Cache: Fix loc...
144
145
  	struct fscache_cookie *cookie;
  	struct page *xpage = NULL;
b51088228   David Howells   FS-Cache: Impleme...
146

1bccf513a   David Howells   FS-Cache: Fix loc...
147
148
149
150
151
152
  	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 ...
153
154
  		radix_tree_tag_clear(&cookie->stores, page->index,
  				     FSCACHE_COOKIE_STORING_TAG);
285e728b0   David Howells   FS-Cache: Don't d...
155
156
157
158
159
  		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...
160
161
162
163
164
  		spin_unlock(&cookie->stores_lock);
  		wake_up_bit(&cookie->flags, 0);
  	}
  	spin_unlock(&object->lock);
  	if (xpage)
09cbfeaf1   Kirill A. Shutemov   mm, fs: get rid o...
165
  		put_page(xpage);
b51088228   David Howells   FS-Cache: Impleme...
166
167
168
169
170
171
172
173
  }
  
  /*
   * 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...
174
  	int ret;
b51088228   David Howells   FS-Cache: Impleme...
175
176
177
178
  
  	_enter("{OBJ%x OP%x}", object->debug_id, op->debug_id);
  
  	fscache_stat(&fscache_n_attr_changed_calls);
8fb883f3e   David Howells   FS-Cache: Add use...
179
  	if (fscache_object_is_active(object)) {
52bd75fdb   David Howells   FS-Cache: Add cou...
180
  		fscache_stat(&fscache_n_cop_attr_changed);
440f0affe   David Howells   FS-Cache: Annotat...
181
  		ret = object->cache->ops->attr_changed(object);
52bd75fdb   David Howells   FS-Cache: Add cou...
182
  		fscache_stat_d(&fscache_n_cop_attr_changed);
440f0affe   David Howells   FS-Cache: Annotat...
183
184
185
  		if (ret < 0)
  			fscache_abort_object(object);
  	}
b51088228   David Howells   FS-Cache: Impleme...
186

1f372dff1   David Howells   FS-Cache: Mark ca...
187
  	fscache_op_complete(op, true);
b51088228   David Howells   FS-Cache: Impleme...
188
189
190
191
192
193
194
195
196
197
  	_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;
3e1199dca   Milosz Tanski   FS-Cache: refcoun...
198
  	bool wake_cookie = false;
b51088228   David Howells   FS-Cache: Impleme...
199
200
201
202
203
204
205
206
207
208
209
210
211
  
  	_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;
  	}
d3b97ca4a   David Howells   FS-Cache: The ope...
212
  	fscache_operation_init(op, fscache_attr_changed_op, NULL, NULL);
8fb883f3e   David Howells   FS-Cache: Add use...
213
214
215
  	op->flags = FSCACHE_OP_ASYNC |
  		(1 << FSCACHE_OP_EXCLUSIVE) |
  		(1 << FSCACHE_OP_UNUSE_COOKIE);
b51088228   David Howells   FS-Cache: Impleme...
216
217
  
  	spin_lock(&cookie->lock);
94d30ae90   David Howells   FS-Cache: Provide...
218
219
  	if (!fscache_cookie_enabled(cookie) ||
  	    hlist_empty(&cookie->backing_objects))
b51088228   David Howells   FS-Cache: Impleme...
220
221
222
  		goto nobufs;
  	object = hlist_entry(cookie->backing_objects.first,
  			     struct fscache_object, cookie_link);
8fb883f3e   David Howells   FS-Cache: Add use...
223
  	__fscache_use_cookie(cookie);
b51088228   David Howells   FS-Cache: Impleme...
224
  	if (fscache_submit_exclusive_op(object, op) < 0)
3e1199dca   Milosz Tanski   FS-Cache: refcoun...
225
  		goto nobufs_dec;
b51088228   David Howells   FS-Cache: Impleme...
226
227
228
229
230
  	spin_unlock(&cookie->lock);
  	fscache_stat(&fscache_n_attr_changed_ok);
  	fscache_put_operation(op);
  	_leave(" = 0");
  	return 0;
3e1199dca   Milosz Tanski   FS-Cache: refcoun...
231
  nobufs_dec:
8fb883f3e   David Howells   FS-Cache: Add use...
232
  	wake_cookie = __fscache_unuse_cookie(cookie);
3e1199dca   Milosz Tanski   FS-Cache: refcoun...
233
  nobufs:
b51088228   David Howells   FS-Cache: Impleme...
234
  	spin_unlock(&cookie->lock);
a39caadf0   David Howells   FS-Cache: Put an ...
235
  	fscache_put_operation(op);
8fb883f3e   David Howells   FS-Cache: Add use...
236
237
  	if (wake_cookie)
  		__fscache_wake_unused_cookie(cookie);
b51088228   David Howells   FS-Cache: Impleme...
238
239
240
241
242
243
244
  	fscache_stat(&fscache_n_attr_changed_nobufs);
  	_leave(" = %d", -ENOBUFS);
  	return -ENOBUFS;
  }
  EXPORT_SYMBOL(__fscache_attr_changed);
  
  /*
d3b97ca4a   David Howells   FS-Cache: The ope...
245
246
247
248
249
250
251
252
253
254
255
   * Handle cancellation of a pending retrieval op
   */
  static void fscache_do_cancel_retrieval(struct fscache_operation *_op)
  {
  	struct fscache_retrieval *op =
  		container_of(_op, struct fscache_retrieval, op);
  
  	atomic_set(&op->n_pages, 0);
  }
  
  /*
b51088228   David Howells   FS-Cache: Impleme...
256
257
258
259
260
261
262
263
   * 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);
4a47132ff   David Howells   FS-Cache: Retain ...
264
265
  	ASSERTIFCMP(op->op.state != FSCACHE_OP_ST_INITIALISED,
  		    atomic_read(&op->n_pages), ==, 0);
9f10523f8   David Howells   FS-Cache: Fix ope...
266

b51088228   David Howells   FS-Cache: Impleme...
267
268
  	fscache_hist(fscache_retrieval_histogram, op->start_time);
  	if (op->context)
4a47132ff   David Howells   FS-Cache: Retain ...
269
  		fscache_put_context(op->cookie, op->context);
b51088228   David Howells   FS-Cache: Impleme...
270
271
272
273
274
275
276
277
  
  	_leave("");
  }
  
  /*
   * allocate a retrieval op
   */
  static struct fscache_retrieval *fscache_alloc_retrieval(
1362729b1   David Howells   FS-Cache: Simplif...
278
  	struct fscache_cookie *cookie,
b51088228   David Howells   FS-Cache: Impleme...
279
280
281
282
283
284
285
286
287
288
289
290
  	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;
  	}
d3b97ca4a   David Howells   FS-Cache: The ope...
291
292
293
  	fscache_operation_init(&op->op, NULL,
  			       fscache_do_cancel_retrieval,
  			       fscache_release_retrieval_op);
1362729b1   David Howells   FS-Cache: Simplif...
294
295
296
  	op->op.flags	= FSCACHE_OP_MYTHREAD |
  		(1UL << FSCACHE_OP_WAITING) |
  		(1UL << FSCACHE_OP_UNUSE_COOKIE);
4a47132ff   David Howells   FS-Cache: Retain ...
297
  	op->cookie	= cookie;
b51088228   David Howells   FS-Cache: Impleme...
298
299
300
301
  	op->mapping	= mapping;
  	op->end_io_func	= end_io_func;
  	op->context	= context;
  	op->start_time	= jiffies;
b51088228   David Howells   FS-Cache: Impleme...
302
  	INIT_LIST_HEAD(&op->to_do);
4a47132ff   David Howells   FS-Cache: Retain ...
303
304
305
306
307
308
  
  	/* Pin the netfs read context in case we need to do the actual netfs
  	 * read because we've encountered a cache read failure.
  	 */
  	if (context)
  		fscache_get_context(op->cookie, context);
b51088228   David Howells   FS-Cache: Impleme...
309
310
311
312
313
314
  	return op;
  }
  
  /*
   * wait for a deferred lookup to complete
   */
da9803bc8   David Howells   FS-Cache: Add int...
315
  int fscache_wait_for_deferred_lookup(struct fscache_cookie *cookie)
b51088228   David Howells   FS-Cache: Impleme...
316
317
318
319
320
321
322
323
324
325
326
327
328
329
  {
  	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,
b51088228   David Howells   FS-Cache: Impleme...
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
  			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...
345
346
   * wait for an object to become active (or dead)
   */
da9803bc8   David Howells   FS-Cache: Add int...
347
348
349
  int fscache_wait_for_operation_activation(struct fscache_object *object,
  					  struct fscache_operation *op,
  					  atomic_t *stat_op_waits,
d3b97ca4a   David Howells   FS-Cache: The ope...
350
  					  atomic_t *stat_object_dead)
60d543ca7   David Howells   FS-Cache: Start p...
351
352
  {
  	int ret;
da9803bc8   David Howells   FS-Cache: Add int...
353
  	if (!test_bit(FSCACHE_OP_WAITING, &op->flags))
60d543ca7   David Howells   FS-Cache: Start p...
354
355
356
  		goto check_if_dead;
  
  	_debug(">>> WT");
da9803bc8   David Howells   FS-Cache: Add int...
357
358
359
  	if (stat_op_waits)
  		fscache_stat(stat_op_waits);
  	if (wait_on_bit(&op->flags, FSCACHE_OP_WAITING,
9c04caa81   David Howells   FS-Cache: Fix sig...
360
  			TASK_INTERRUPTIBLE) != 0) {
d3b97ca4a   David Howells   FS-Cache: The ope...
361
  		ret = fscache_cancel_op(op, false);
60d543ca7   David Howells   FS-Cache: Start p...
362
363
364
365
366
  		if (ret == 0)
  			return -ERESTARTSYS;
  
  		/* it's been removed from the pending queue by another party,
  		 * so we should get to run shortly */
da9803bc8   David Howells   FS-Cache: Add int...
367
  		wait_on_bit(&op->flags, FSCACHE_OP_WAITING,
743162013   NeilBrown   sched: Remove pro...
368
  			    TASK_UNINTERRUPTIBLE);
60d543ca7   David Howells   FS-Cache: Start p...
369
370
371
372
  	}
  	_debug("<<< GO");
  
  check_if_dead:
da9803bc8   David Howells   FS-Cache: Add int...
373
374
375
  	if (op->state == FSCACHE_OP_ST_CANCELLED) {
  		if (stat_object_dead)
  			fscache_stat(stat_object_dead);
9f10523f8   David Howells   FS-Cache: Fix ope...
376
377
378
  		_leave(" = -ENOBUFS [cancelled]");
  		return -ENOBUFS;
  	}
870215263   David Howells   FS-Cache: fscache...
379
380
381
  	if (unlikely(fscache_object_is_dying(object) ||
  		     fscache_cache_is_broken(object))) {
  		enum fscache_operation_state state = op->state;
d3b97ca4a   David Howells   FS-Cache: The ope...
382
  		fscache_cancel_op(op, true);
da9803bc8   David Howells   FS-Cache: Add int...
383
384
  		if (stat_object_dead)
  			fscache_stat(stat_object_dead);
870215263   David Howells   FS-Cache: fscache...
385
  		_leave(" = -ENOBUFS [obj dead %d]", state);
60d543ca7   David Howells   FS-Cache: Start p...
386
387
388
389
390
391
  		return -ENOBUFS;
  	}
  	return 0;
  }
  
  /*
b51088228   David Howells   FS-Cache: Impleme...
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
   * 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;
8fb883f3e   David Howells   FS-Cache: Add use...
408
  	bool wake_cookie = false;
b51088228   David Howells   FS-Cache: Impleme...
409
410
411
412
413
414
415
416
  	int ret;
  
  	_enter("%p,%p,,,", cookie, page);
  
  	fscache_stat(&fscache_n_retrievals);
  
  	if (hlist_empty(&cookie->backing_objects))
  		goto nobufs;
ef778e7ae   David Howells   FS-Cache: Provide...
417
418
419
420
  	if (test_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags)) {
  		_leave(" = -ENOBUFS [invalidating]");
  		return -ENOBUFS;
  	}
b51088228   David Howells   FS-Cache: Impleme...
421
422
423
424
425
  	ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
  	ASSERTCMP(page, !=, NULL);
  
  	if (fscache_wait_for_deferred_lookup(cookie) < 0)
  		return -ERESTARTSYS;
1362729b1   David Howells   FS-Cache: Simplif...
426
  	op = fscache_alloc_retrieval(cookie, page->mapping,
94d30ae90   David Howells   FS-Cache: Provide...
427
  				     end_io_func, context);
b51088228   David Howells   FS-Cache: Impleme...
428
429
430
431
  	if (!op) {
  		_leave(" = -ENOMEM");
  		return -ENOMEM;
  	}
1bb4b7f98   David Howells   FS-Cache: The ret...
432
  	atomic_set(&op->n_pages, 1);
b51088228   David Howells   FS-Cache: Impleme...
433
434
  
  	spin_lock(&cookie->lock);
94d30ae90   David Howells   FS-Cache: Provide...
435
436
  	if (!fscache_cookie_enabled(cookie) ||
  	    hlist_empty(&cookie->backing_objects))
b51088228   David Howells   FS-Cache: Impleme...
437
438
439
  		goto nobufs_unlock;
  	object = hlist_entry(cookie->backing_objects.first,
  			     struct fscache_object, cookie_link);
caaef6900   David Howells   FS-Cache: Fix obj...
440
  	ASSERT(test_bit(FSCACHE_OBJECT_IS_LOOKED_UP, &object->flags));
b51088228   David Howells   FS-Cache: Impleme...
441

8fb883f3e   David Howells   FS-Cache: Add use...
442
  	__fscache_use_cookie(cookie);
4fbf4291a   David Howells   FS-Cache: Allow t...
443
  	atomic_inc(&object->n_reads);
9f10523f8   David Howells   FS-Cache: Fix ope...
444
  	__set_bit(FSCACHE_OP_DEC_READ_CNT, &op->op.flags);
4fbf4291a   David Howells   FS-Cache: Allow t...
445

b51088228   David Howells   FS-Cache: Impleme...
446
  	if (fscache_submit_op(object, &op->op) < 0)
9f10523f8   David Howells   FS-Cache: Fix ope...
447
  		goto nobufs_unlock_dec;
b51088228   David Howells   FS-Cache: Impleme...
448
449
450
  	spin_unlock(&cookie->lock);
  
  	fscache_stat(&fscache_n_retrieval_ops);
b51088228   David Howells   FS-Cache: Impleme...
451
452
  	/* we wait for the operation to become active, and then process it
  	 * *here*, in this thread, and not in the thread pool */
da9803bc8   David Howells   FS-Cache: Add int...
453
454
  	ret = fscache_wait_for_operation_activation(
  		object, &op->op,
60d543ca7   David Howells   FS-Cache: Start p...
455
  		__fscache_stat(&fscache_n_retrieval_op_waits),
d3b97ca4a   David Howells   FS-Cache: The ope...
456
  		__fscache_stat(&fscache_n_retrievals_object_dead));
60d543ca7   David Howells   FS-Cache: Start p...
457
458
  	if (ret < 0)
  		goto error;
b51088228   David Howells   FS-Cache: Impleme...
459
460
461
  
  	/* 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...
462
  		fscache_stat(&fscache_n_cop_allocate_page);
b51088228   David Howells   FS-Cache: Impleme...
463
  		ret = object->cache->ops->allocate_page(op, page, gfp);
52bd75fdb   David Howells   FS-Cache: Add cou...
464
  		fscache_stat_d(&fscache_n_cop_allocate_page);
b51088228   David Howells   FS-Cache: Impleme...
465
466
467
  		if (ret == 0)
  			ret = -ENODATA;
  	} else {
52bd75fdb   David Howells   FS-Cache: Add cou...
468
  		fscache_stat(&fscache_n_cop_read_or_alloc_page);
b51088228   David Howells   FS-Cache: Impleme...
469
  		ret = object->cache->ops->read_or_alloc_page(op, page, gfp);
52bd75fdb   David Howells   FS-Cache: Add cou...
470
  		fscache_stat_d(&fscache_n_cop_read_or_alloc_page);
b51088228   David Howells   FS-Cache: Impleme...
471
  	}
5753c4418   David Howells   FS-Cache: Permit ...
472
  error:
b51088228   David Howells   FS-Cache: Impleme...
473
474
475
476
477
478
479
480
481
482
483
484
485
486
  	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;
9f10523f8   David Howells   FS-Cache: Fix ope...
487
488
  nobufs_unlock_dec:
  	atomic_dec(&object->n_reads);
8fb883f3e   David Howells   FS-Cache: Add use...
489
  	wake_cookie = __fscache_unuse_cookie(cookie);
b51088228   David Howells   FS-Cache: Impleme...
490
491
  nobufs_unlock:
  	spin_unlock(&cookie->lock);
8fb883f3e   David Howells   FS-Cache: Add use...
492
493
  	if (wake_cookie)
  		__fscache_wake_unused_cookie(cookie);
a39caadf0   David Howells   FS-Cache: Put an ...
494
  	fscache_put_retrieval(op);
b51088228   David Howells   FS-Cache: Impleme...
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
  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...
528
529
  	struct fscache_retrieval *op;
  	struct fscache_object *object;
8fb883f3e   David Howells   FS-Cache: Add use...
530
  	bool wake_cookie = false;
b51088228   David Howells   FS-Cache: Impleme...
531
532
533
534
535
536
537
538
  	int ret;
  
  	_enter("%p,,%d,,,", cookie, *nr_pages);
  
  	fscache_stat(&fscache_n_retrievals);
  
  	if (hlist_empty(&cookie->backing_objects))
  		goto nobufs;
ef778e7ae   David Howells   FS-Cache: Provide...
539
540
541
542
  	if (test_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags)) {
  		_leave(" = -ENOBUFS [invalidating]");
  		return -ENOBUFS;
  	}
b51088228   David Howells   FS-Cache: Impleme...
543
544
545
546
547
548
  	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;
1362729b1   David Howells   FS-Cache: Simplif...
549
  	op = fscache_alloc_retrieval(cookie, mapping, end_io_func, context);
b51088228   David Howells   FS-Cache: Impleme...
550
551
  	if (!op)
  		return -ENOMEM;
1bb4b7f98   David Howells   FS-Cache: The ret...
552
  	atomic_set(&op->n_pages, *nr_pages);
b51088228   David Howells   FS-Cache: Impleme...
553
554
  
  	spin_lock(&cookie->lock);
94d30ae90   David Howells   FS-Cache: Provide...
555
556
  	if (!fscache_cookie_enabled(cookie) ||
  	    hlist_empty(&cookie->backing_objects))
b51088228   David Howells   FS-Cache: Impleme...
557
558
559
  		goto nobufs_unlock;
  	object = hlist_entry(cookie->backing_objects.first,
  			     struct fscache_object, cookie_link);
8fb883f3e   David Howells   FS-Cache: Add use...
560
  	__fscache_use_cookie(cookie);
4fbf4291a   David Howells   FS-Cache: Allow t...
561
  	atomic_inc(&object->n_reads);
9f10523f8   David Howells   FS-Cache: Fix ope...
562
  	__set_bit(FSCACHE_OP_DEC_READ_CNT, &op->op.flags);
4fbf4291a   David Howells   FS-Cache: Allow t...
563

b51088228   David Howells   FS-Cache: Impleme...
564
  	if (fscache_submit_op(object, &op->op) < 0)
9f10523f8   David Howells   FS-Cache: Fix ope...
565
  		goto nobufs_unlock_dec;
b51088228   David Howells   FS-Cache: Impleme...
566
567
568
  	spin_unlock(&cookie->lock);
  
  	fscache_stat(&fscache_n_retrieval_ops);
b51088228   David Howells   FS-Cache: Impleme...
569
570
  	/* we wait for the operation to become active, and then process it
  	 * *here*, in this thread, and not in the thread pool */
da9803bc8   David Howells   FS-Cache: Add int...
571
572
  	ret = fscache_wait_for_operation_activation(
  		object, &op->op,
60d543ca7   David Howells   FS-Cache: Start p...
573
  		__fscache_stat(&fscache_n_retrieval_op_waits),
d3b97ca4a   David Howells   FS-Cache: The ope...
574
  		__fscache_stat(&fscache_n_retrievals_object_dead));
60d543ca7   David Howells   FS-Cache: Start p...
575
576
  	if (ret < 0)
  		goto error;
b51088228   David Howells   FS-Cache: Impleme...
577
578
  
  	/* ask the cache to honour the operation */
52bd75fdb   David Howells   FS-Cache: Add cou...
579
580
581
582
583
584
585
586
587
588
589
  	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...
590

5753c4418   David Howells   FS-Cache: Permit ...
591
  error:
b51088228   David Howells   FS-Cache: Impleme...
592
593
594
595
596
597
598
599
600
601
602
603
604
605
  	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;
9f10523f8   David Howells   FS-Cache: Fix ope...
606
607
  nobufs_unlock_dec:
  	atomic_dec(&object->n_reads);
8fb883f3e   David Howells   FS-Cache: Add use...
608
  	wake_cookie = __fscache_unuse_cookie(cookie);
b51088228   David Howells   FS-Cache: Impleme...
609
610
  nobufs_unlock:
  	spin_unlock(&cookie->lock);
a39caadf0   David Howells   FS-Cache: Put an ...
611
  	fscache_put_retrieval(op);
8fb883f3e   David Howells   FS-Cache: Add use...
612
613
  	if (wake_cookie)
  		__fscache_wake_unused_cookie(cookie);
b51088228   David Howells   FS-Cache: Impleme...
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
  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;
8fb883f3e   David Howells   FS-Cache: Add use...
635
  	bool wake_cookie = false;
b51088228   David Howells   FS-Cache: Impleme...
636
637
638
639
640
641
642
643
644
645
646
  	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);
ef778e7ae   David Howells   FS-Cache: Provide...
647
648
649
650
  	if (test_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags)) {
  		_leave(" = -ENOBUFS [invalidating]");
  		return -ENOBUFS;
  	}
b51088228   David Howells   FS-Cache: Impleme...
651
652
  	if (fscache_wait_for_deferred_lookup(cookie) < 0)
  		return -ERESTARTSYS;
1362729b1   David Howells   FS-Cache: Simplif...
653
  	op = fscache_alloc_retrieval(cookie, page->mapping, NULL, NULL);
b51088228   David Howells   FS-Cache: Impleme...
654
655
  	if (!op)
  		return -ENOMEM;
1bb4b7f98   David Howells   FS-Cache: The ret...
656
  	atomic_set(&op->n_pages, 1);
b51088228   David Howells   FS-Cache: Impleme...
657
658
  
  	spin_lock(&cookie->lock);
94d30ae90   David Howells   FS-Cache: Provide...
659
660
  	if (!fscache_cookie_enabled(cookie) ||
  	    hlist_empty(&cookie->backing_objects))
b51088228   David Howells   FS-Cache: Impleme...
661
662
663
  		goto nobufs_unlock;
  	object = hlist_entry(cookie->backing_objects.first,
  			     struct fscache_object, cookie_link);
8fb883f3e   David Howells   FS-Cache: Add use...
664
  	__fscache_use_cookie(cookie);
b51088228   David Howells   FS-Cache: Impleme...
665
  	if (fscache_submit_op(object, &op->op) < 0)
8fb883f3e   David Howells   FS-Cache: Add use...
666
  		goto nobufs_unlock_dec;
b51088228   David Howells   FS-Cache: Impleme...
667
668
669
  	spin_unlock(&cookie->lock);
  
  	fscache_stat(&fscache_n_alloc_ops);
da9803bc8   David Howells   FS-Cache: Add int...
670
671
  	ret = fscache_wait_for_operation_activation(
  		object, &op->op,
60d543ca7   David Howells   FS-Cache: Start p...
672
  		__fscache_stat(&fscache_n_alloc_op_waits),
d3b97ca4a   David Howells   FS-Cache: The ope...
673
  		__fscache_stat(&fscache_n_allocs_object_dead));
60d543ca7   David Howells   FS-Cache: Start p...
674
675
  	if (ret < 0)
  		goto error;
b51088228   David Howells   FS-Cache: Impleme...
676
677
  
  	/* ask the cache to honour the operation */
52bd75fdb   David Howells   FS-Cache: Add cou...
678
  	fscache_stat(&fscache_n_cop_allocate_page);
b51088228   David Howells   FS-Cache: Impleme...
679
  	ret = object->cache->ops->allocate_page(op, page, gfp);
52bd75fdb   David Howells   FS-Cache: Add cou...
680
  	fscache_stat_d(&fscache_n_cop_allocate_page);
b51088228   David Howells   FS-Cache: Impleme...
681

5753c4418   David Howells   FS-Cache: Permit ...
682
683
684
685
  error:
  	if (ret == -ERESTARTSYS)
  		fscache_stat(&fscache_n_allocs_intr);
  	else if (ret < 0)
b51088228   David Howells   FS-Cache: Impleme...
686
687
688
689
690
691
692
  		fscache_stat(&fscache_n_allocs_nobufs);
  	else
  		fscache_stat(&fscache_n_allocs_ok);
  
  	fscache_put_retrieval(op);
  	_leave(" = %d", ret);
  	return ret;
8fb883f3e   David Howells   FS-Cache: Add use...
693
694
  nobufs_unlock_dec:
  	wake_cookie = __fscache_unuse_cookie(cookie);
b51088228   David Howells   FS-Cache: Impleme...
695
696
  nobufs_unlock:
  	spin_unlock(&cookie->lock);
a39caadf0   David Howells   FS-Cache: Put an ...
697
  	fscache_put_retrieval(op);
8fb883f3e   David Howells   FS-Cache: Add use...
698
699
  	if (wake_cookie)
  		__fscache_wake_unused_cookie(cookie);
b51088228   David Howells   FS-Cache: Impleme...
700
701
702
703
704
705
706
707
  nobufs:
  	fscache_stat(&fscache_n_allocs_nobufs);
  	_leave(" = -ENOBUFS");
  	return -ENOBUFS;
  }
  EXPORT_SYMBOL(__fscache_alloc_page);
  
  /*
5a6f282a2   Milosz Tanski   fscache: Netfs fu...
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
   * Unmark pages allocate in the readahead code path (via:
   * fscache_readpages_or_alloc) after delegating to the base filesystem
   */
  void __fscache_readpages_cancel(struct fscache_cookie *cookie,
  				struct list_head *pages)
  {
  	struct page *page;
  
  	list_for_each_entry(page, pages, lru) {
  		if (PageFsCache(page))
  			__fscache_uncache_page(cookie, page);
  	}
  }
  EXPORT_SYMBOL(__fscache_readpages_cancel);
  
  /*
b51088228   David Howells   FS-Cache: Impleme...
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
   * 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...
739
  	struct fscache_cookie *cookie;
b51088228   David Howells   FS-Cache: Impleme...
740
741
742
743
744
745
  	struct page *page;
  	unsigned n;
  	void *results[1];
  	int ret;
  
  	_enter("{OP%x,%d}", op->op.debug_id, atomic_read(&op->op.usage));
22f1bde5d   David Howells   fscache: Fix hang...
746
  again:
b51088228   David Howells   FS-Cache: Impleme...
747
  	spin_lock(&object->lock);
1bccf513a   David Howells   FS-Cache: Fix loc...
748
  	cookie = object->cookie;
b51088228   David Howells   FS-Cache: Impleme...
749

7ef001e93   David Howells   FS-Cache: One of ...
750
751
752
753
  	if (!fscache_object_is_active(object)) {
  		/* If we get here, then the on-disk cache object likely longer
  		 * exists, so we should just cancel this write operation.
  		 */
b51088228   David Howells   FS-Cache: Impleme...
754
  		spin_unlock(&object->lock);
1f372dff1   David Howells   FS-Cache: Mark ca...
755
  		fscache_op_complete(&op->op, false);
7ef001e93   David Howells   FS-Cache: One of ...
756
757
758
759
760
761
762
763
764
765
766
767
  		_leave(" [inactive]");
  		return;
  	}
  
  	if (!cookie) {
  		/* If we get here, then the cookie belonging to the object was
  		 * detached, probably by the cookie being withdrawn due to
  		 * memory pressure, which means that the pages we might write
  		 * to the cache from no longer exist - therefore, we can just
  		 * cancel this write operation.
  		 */
  		spin_unlock(&object->lock);
1f372dff1   David Howells   FS-Cache: Mark ca...
768
  		fscache_op_complete(&op->op, false);
caaef6900   David Howells   FS-Cache: Fix obj...
769
770
771
  		_leave(" [cancel] op{f=%lx s=%u} obj{s=%s f=%lx}",
  		       _op->flags, _op->state, object->state->short_name,
  		       object->flags);
b51088228   David Howells   FS-Cache: Impleme...
772
773
  		return;
  	}
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
  	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);
b51088228   David Howells   FS-Cache: Impleme...
785

08a66859e   Dan Carpenter   FS-Cache: Remove ...
786
787
788
789
  	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...
790

1bccf513a   David Howells   FS-Cache: Fix loc...
791
  	spin_unlock(&cookie->stores_lock);
b51088228   David Howells   FS-Cache: Impleme...
792
  	spin_unlock(&object->lock);
b51088228   David Howells   FS-Cache: Impleme...
793

22f1bde5d   David Howells   fscache: Fix hang...
794
795
  	if (page->index >= op->store_limit)
  		goto discard_page;
08a66859e   Dan Carpenter   FS-Cache: Remove ...
796
797
798
799
  	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 ...
800
801
  	fscache_end_page_write(object, page);
  	if (ret < 0) {
08a66859e   Dan Carpenter   FS-Cache: Remove ...
802
  		fscache_abort_object(object);
1f372dff1   David Howells   FS-Cache: Mark ca...
803
  		fscache_op_complete(&op->op, true);
08a66859e   Dan Carpenter   FS-Cache: Remove ...
804
805
  	} else {
  		fscache_enqueue_operation(&op->op);
b51088228   David Howells   FS-Cache: Impleme...
806
807
808
809
  	}
  
  	_leave("");
  	return;
22f1bde5d   David Howells   fscache: Fix hang...
810
811
812
813
  discard_page:
  	fscache_stat(&fscache_n_store_pages_over_limit);
  	fscache_end_page_write(object, page);
  	goto again;
b51088228   David Howells   FS-Cache: Impleme...
814
815
816
817
  superseded:
  	/* this writer is going away and there aren't any more things to
  	 * write */
  	_debug("cease");
1bccf513a   David Howells   FS-Cache: Fix loc...
818
  	spin_unlock(&cookie->stores_lock);
b51088228   David Howells   FS-Cache: Impleme...
819
820
  	clear_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags);
  	spin_unlock(&object->lock);
1f372dff1   David Howells   FS-Cache: Mark ca...
821
  	fscache_op_complete(&op->op, true);
b51088228   David Howells   FS-Cache: Impleme...
822
823
824
825
  	_leave("");
  }
  
  /*
ef778e7ae   David Howells   FS-Cache: Provide...
826
827
828
829
830
831
832
833
834
   * Clear the pages pending writing for invalidation
   */
  void fscache_invalidate_writes(struct fscache_cookie *cookie)
  {
  	struct page *page;
  	void *results[16];
  	int n, i;
  
  	_enter("");
ee8be57bc   Sebastian Andrzej Siewior   fs/fscache: remov...
835
836
837
838
839
840
841
842
843
  	for (;;) {
  		spin_lock(&cookie->stores_lock);
  		n = radix_tree_gang_lookup_tag(&cookie->stores, results, 0,
  					       ARRAY_SIZE(results),
  					       FSCACHE_COOKIE_PENDING_TAG);
  		if (n == 0) {
  			spin_unlock(&cookie->stores_lock);
  			break;
  		}
ef778e7ae   David Howells   FS-Cache: Provide...
844
845
846
847
848
849
850
851
  		for (i = n - 1; i >= 0; i--) {
  			page = results[i];
  			radix_tree_delete(&cookie->stores, page->index);
  		}
  
  		spin_unlock(&cookie->stores_lock);
  
  		for (i = n - 1; i >= 0; i--)
09cbfeaf1   Kirill A. Shutemov   mm, fs: get rid o...
852
  			put_page(results[i]);
ef778e7ae   David Howells   FS-Cache: Provide...
853
  	}
d21384552   Yan, Zheng   FS-Cache: wake wr...
854
  	wake_up_bit(&cookie->flags, 0);
ef778e7ae   David Howells   FS-Cache: Provide...
855
856
857
858
  	_leave("");
  }
  
  /*
b51088228   David Howells   FS-Cache: Impleme...
859
860
861
862
863
864
865
866
867
868
869
870
   * 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)
   *
caaef6900   David Howells   FS-Cache: Fix obj...
871
   *	(a) no writes yet
b51088228   David Howells   FS-Cache: Impleme...
872
873
874
875
876
   *
   *	(b) writes deferred till post-creation (mark page for writing and
   *	    return immediately)
   *
   *  (2) negative lookup, object created, initial fill being made from netfs
b51088228   David Howells   FS-Cache: Impleme...
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
   *
   *	(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;
8fb883f3e   David Howells   FS-Cache: Add use...
893
  	bool wake_cookie = false;
b51088228   David Howells   FS-Cache: Impleme...
894
895
896
897
898
899
900
901
  	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);
ef778e7ae   David Howells   FS-Cache: Provide...
902
903
904
905
  	if (test_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags)) {
  		_leave(" = -ENOBUFS [invalidating]");
  		return -ENOBUFS;
  	}
5f4f9f4af   David Howells   CacheFiles: Downg...
906
  	op = kzalloc(sizeof(*op), GFP_NOIO | __GFP_NOMEMALLOC | __GFP_NORETRY);
b51088228   David Howells   FS-Cache: Impleme...
907
908
  	if (!op)
  		goto nomem;
d3b97ca4a   David Howells   FS-Cache: The ope...
909
  	fscache_operation_init(&op->op, fscache_write_op, NULL,
8af7c1243   Tejun Heo   fscache: convert ...
910
  			       fscache_release_write_op);
1362729b1   David Howells   FS-Cache: Simplif...
911
912
913
  	op->op.flags = FSCACHE_OP_ASYNC |
  		(1 << FSCACHE_OP_WAITING) |
  		(1 << FSCACHE_OP_UNUSE_COOKIE);
b51088228   David Howells   FS-Cache: Impleme...
914

5e4c0d974   Jan Kara   lib/radix-tree.c:...
915
  	ret = radix_tree_maybe_preload(gfp & ~__GFP_HIGHMEM);
b51088228   David Howells   FS-Cache: Impleme...
916
917
918
919
920
  	if (ret < 0)
  		goto nomem_free;
  
  	ret = -ENOBUFS;
  	spin_lock(&cookie->lock);
94d30ae90   David Howells   FS-Cache: Provide...
921
922
  	if (!fscache_cookie_enabled(cookie) ||
  	    hlist_empty(&cookie->backing_objects))
b51088228   David Howells   FS-Cache: Impleme...
923
924
925
926
927
928
929
930
931
  		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...
932
  	spin_lock(&cookie->stores_lock);
b51088228   David Howells   FS-Cache: Impleme...
933
934
935
936
937
938
939
940
941
942
943
944
945
  
  	_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);
09cbfeaf1   Kirill A. Shutemov   mm, fs: get rid o...
946
  	get_page(page);
b51088228   David Howells   FS-Cache: Impleme...
947
948
949
950
951
  
  	/* 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...
952
  	spin_unlock(&cookie->stores_lock);
b51088228   David Howells   FS-Cache: Impleme...
953
954
955
956
  	spin_unlock(&object->lock);
  
  	op->op.debug_id	= atomic_inc_return(&fscache_op_debug_id);
  	op->store_limit = object->store_limit;
8fb883f3e   David Howells   FS-Cache: Add use...
957
  	__fscache_use_cookie(cookie);
b51088228   David Howells   FS-Cache: Impleme...
958
959
960
961
962
963
964
  	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 ...
965
  	/* the work queue now carries its own ref on the object */
b51088228   David Howells   FS-Cache: Impleme...
966
967
968
969
970
971
972
  	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...
973
  	spin_unlock(&cookie->stores_lock);
b51088228   David Howells   FS-Cache: Impleme...
974
975
976
  	spin_unlock(&object->lock);
  	spin_unlock(&cookie->lock);
  	radix_tree_preload_end();
a39caadf0   David Howells   FS-Cache: Put an ...
977
  	fscache_put_operation(&op->op);
b51088228   David Howells   FS-Cache: Impleme...
978
979
980
981
982
  	fscache_stat(&fscache_n_stores_ok);
  	_leave(" = 0");
  	return 0;
  
  submit_failed:
1bccf513a   David Howells   FS-Cache: Fix loc...
983
  	spin_lock(&cookie->stores_lock);
b51088228   David Howells   FS-Cache: Impleme...
984
  	radix_tree_delete(&cookie->stores, page->index);
1bccf513a   David Howells   FS-Cache: Fix loc...
985
  	spin_unlock(&cookie->stores_lock);
8fb883f3e   David Howells   FS-Cache: Add use...
986
  	wake_cookie = __fscache_unuse_cookie(cookie);
09cbfeaf1   Kirill A. Shutemov   mm, fs: get rid o...
987
  	put_page(page);
b51088228   David Howells   FS-Cache: Impleme...
988
989
990
991
  	ret = -ENOBUFS;
  	goto nobufs;
  
  nobufs_unlock_obj:
1147d0f91   Dan Carpenter   fscache: add miss...
992
  	spin_unlock(&cookie->stores_lock);
b51088228   David Howells   FS-Cache: Impleme...
993
994
995
996
  	spin_unlock(&object->lock);
  nobufs:
  	spin_unlock(&cookie->lock);
  	radix_tree_preload_end();
a39caadf0   David Howells   FS-Cache: Put an ...
997
  	fscache_put_operation(&op->op);
8fb883f3e   David Howells   FS-Cache: Add use...
998
999
  	if (wake_cookie)
  		__fscache_wake_unused_cookie(cookie);
b51088228   David Howells   FS-Cache: Impleme...
1000
1001
1002
1003
1004
  	fscache_stat(&fscache_n_stores_nobufs);
  	_leave(" = -ENOBUFS");
  	return -ENOBUFS;
  
  nomem_free:
a39caadf0   David Howells   FS-Cache: Put an ...
1005
  	fscache_put_operation(&op->op);
b51088228   David Howells   FS-Cache: Impleme...
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
  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...
1050
  		fscache_stat(&fscache_n_cop_uncache_page);
b51088228   David Howells   FS-Cache: Impleme...
1051
  		object->cache->ops->uncache_page(object, page);
52bd75fdb   David Howells   FS-Cache: Add cou...
1052
  		fscache_stat_d(&fscache_n_cop_uncache_page);
b51088228   David Howells   FS-Cache: Impleme...
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
  		goto done;
  	}
  
  done_unlock:
  	spin_unlock(&cookie->lock);
  done:
  	_leave("");
  }
  EXPORT_SYMBOL(__fscache_uncache_page);
  
  /**
c4d6d8dbf   David Howells   CacheFiles: Fix t...
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
   * fscache_mark_page_cached - Mark a page as being cached
   * @op: The retrieval op pages are being marked for
   * @page: The page to be marked
   *
   * Mark a netfs page as being cached.  After this is called, the netfs
   * must call fscache_uncache_page() to remove the mark.
   */
  void fscache_mark_page_cached(struct fscache_retrieval *op, struct page *page)
  {
  	struct fscache_cookie *cookie = op->op.object->cookie;
  
  #ifdef CONFIG_FSCACHE_STATS
  	atomic_inc(&fscache_n_marks);
  #endif
  
  	_debug("- mark %p{%lx}", page, page->index);
  	if (TestSetPageFsCache(page)) {
  		static bool once_only;
  		if (!once_only) {
  			once_only = true;
36dfd116e   Fabian Frederick   fs/fscache: conve...
1084
1085
1086
  			pr_warn("Cookie type %s marked page %lx multiple times
  ",
  				cookie->def->name, page->index);
c4d6d8dbf   David Howells   CacheFiles: Fix t...
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
  		}
  	}
  
  	if (cookie->def->mark_page_cached)
  		cookie->def->mark_page_cached(cookie->netfs_data,
  					      op->mapping, page);
  }
  EXPORT_SYMBOL(fscache_mark_page_cached);
  
  /**
b51088228   David Howells   FS-Cache: Impleme...
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
   * 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)
  {
b51088228   David Howells   FS-Cache: Impleme...
1107
  	unsigned long loop;
c4d6d8dbf   David Howells   CacheFiles: Fix t...
1108
1109
  	for (loop = 0; loop < pagevec->nr; loop++)
  		fscache_mark_page_cached(op, pagevec->pages[loop]);
b51088228   David Howells   FS-Cache: Impleme...
1110

b51088228   David Howells   FS-Cache: Impleme...
1111
1112
1113
  	pagevec_reinit(pagevec);
  }
  EXPORT_SYMBOL(fscache_mark_pages_cached);
c902ce1bf   David Howells   FS-Cache: Add a h...
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
  
  /*
   * 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...
1136
  	do {
397162ffa   Jan Kara   mm: remove nr_pag...
1137
  		if (!pagevec_lookup(&pvec, mapping, &next))
b307d4655   Jan Beulich   FS-Cache: Fix __f...
1138
  			break;
c902ce1bf   David Howells   FS-Cache: Add a h...
1139
1140
  		for (i = 0; i < pagevec_count(&pvec); i++) {
  			struct page *page = pvec.pages[i];
c902ce1bf   David Howells   FS-Cache: Add a h...
1141
1142
1143
1144
1145
1146
1147
  			if (PageFsCache(page)) {
  				__fscache_wait_on_page_write(cookie, page);
  				__fscache_uncache_page(cookie, page);
  			}
  		}
  		pagevec_release(&pvec);
  		cond_resched();
d72dc8a25   Jan Kara   mm: make pagevec_...
1148
  	} while (next);
c902ce1bf   David Howells   FS-Cache: Add a h...
1149
1150
1151
1152
  
  	_leave("");
  }
  EXPORT_SYMBOL(__fscache_uncache_all_inode_pages);