Blame view

fs/nfs/pagelist.c 12.6 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
7
8
9
10
  /*
   * linux/fs/nfs/pagelist.c
   *
   * A set of helper functions for managing NFS read and write requests.
   * The main purpose of these routines is to provide support for the
   * coalescing of several requests into a single RPC call.
   *
   * Copyright 2000, 2001 (c) Trond Myklebust <trond.myklebust@fys.uio.no>
   *
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
11
12
  #include <linux/slab.h>
  #include <linux/file.h>
e8edc6e03   Alexey Dobriyan   Detach sched.h fr...
13
  #include <linux/sched.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
14
15
16
17
18
19
  #include <linux/sunrpc/clnt.h>
  #include <linux/nfs3.h>
  #include <linux/nfs4.h>
  #include <linux/nfs_page.h>
  #include <linux/nfs_fs.h>
  #include <linux/nfs_mount.h>
afeacc8c1   Paul Gortmaker   fs: add export.h ...
20
  #include <linux/export.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
21

8d5658c94   Trond Myklebust   NFS: Fix a buffer...
22
  #include "internal.h"
bae724ef9   Fred Isaman   NFSv4.1: shift pn...
23
  #include "pnfs.h"
8d5658c94   Trond Myklebust   NFS: Fix a buffer...
24

e18b890bb   Christoph Lameter   [PATCH] slab: rem...
25
  static struct kmem_cache *nfs_page_cachep;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
26
27
28
29
  
  static inline struct nfs_page *
  nfs_page_alloc(void)
  {
72895b1ac   Jesper Juhl   nfs: Take advanta...
30
31
  	struct nfs_page	*p = kmem_cache_zalloc(nfs_page_cachep, GFP_KERNEL);
  	if (p)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
32
  		INIT_LIST_HEAD(&p->wb_list);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
33
34
35
36
37
38
39
40
41
42
43
  	return p;
  }
  
  static inline void
  nfs_page_free(struct nfs_page *p)
  {
  	kmem_cache_free(nfs_page_cachep, p);
  }
  
  /**
   * nfs_create_request - Create an NFS read/write request.
c02f557dd   Chuck Lever   NFS: Fix document...
44
   * @ctx: open context to use
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
45
46
47
48
49
50
   * @inode: inode to which the request is attached
   * @page: page to write
   * @offset: starting offset within the page for the write
   * @count: number of bytes to read/write
   *
   * The page must be locked by the caller. This makes sure we never
a19b89cad   Jason Uhlenkott   NFS: Clean up nfs...
51
   * create two different requests for the same page.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
52
53
54
55
56
57
58
   * User should ensure it is safe to sleep in this function.
   */
  struct nfs_page *
  nfs_create_request(struct nfs_open_context *ctx, struct inode *inode,
  		   struct page *page,
  		   unsigned int offset, unsigned int count)
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
59
  	struct nfs_page		*req;
18eb88428   Trond Myklebust   NFS: Clean up nfs...
60
61
62
63
  	/* try to allocate the request struct */
  	req = nfs_page_alloc();
  	if (req == NULL)
  		return ERR_PTR(-ENOMEM);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
64

015f0212d   Jeff Layton   nfs: handle lock ...
65
66
67
68
69
70
  	/* get lock context early so we can deal with alloc failures */
  	req->wb_lock_context = nfs_get_lock_context(ctx);
  	if (req->wb_lock_context == NULL) {
  		nfs_page_free(req);
  		return ERR_PTR(-ENOMEM);
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
71
72
73
74
75
76
77
  	/* Initialize the request struct. Initially, we assume a
  	 * long write-back delay. This will be adjusted in
  	 * update_nfs_request below if the region is not locked. */
  	req->wb_page    = page;
  	atomic_set(&req->wb_complete, 0);
  	req->wb_index	= page->index;
  	page_cache_get(page);
cd52ed355   Trond Myklebust   NFS: Avoid races ...
78
79
80
  	BUG_ON(PagePrivate(page));
  	BUG_ON(!PageLocked(page));
  	BUG_ON(page->mapping->host != inode);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
81
82
83
  	req->wb_offset  = offset;
  	req->wb_pgbase	= offset;
  	req->wb_bytes   = count;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
84
  	req->wb_context = get_nfs_open_context(ctx);
c03b40246   Trond Myklebust   NFS: Convert stru...
85
  	kref_init(&req->wb_kref);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
  	return req;
  }
  
  /**
   * nfs_unlock_request - Unlock request and wake up sleepers.
   * @req:
   */
  void nfs_unlock_request(struct nfs_page *req)
  {
  	if (!NFS_WBACK_BUSY(req)) {
  		printk(KERN_ERR "NFS: Invalid unlock attempted
  ");
  		BUG();
  	}
  	smp_mb__before_clear_bit();
  	clear_bit(PG_BUSY, &req->wb_flags);
  	smp_mb__after_clear_bit();
464a98bd7   Trond Myklebust   [PATCH] NFS: clea...
103
  	wake_up_bit(&req->wb_flags, PG_BUSY);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
104
105
106
107
  	nfs_release_request(req);
  }
  
  /**
9fd367f0f   Trond Myklebust   NFS cleanup: Rena...
108
   * nfs_set_page_tag_locked - Tag a request as locked
c6a556b88   Trond Myklebust   [PATCH] NFS: Make...
109
110
   * @req:
   */
acee478af   Trond Myklebust   NFS: Clean up the...
111
  int nfs_set_page_tag_locked(struct nfs_page *req)
c6a556b88   Trond Myklebust   [PATCH] NFS: Make...
112
  {
acee478af   Trond Myklebust   NFS: Clean up the...
113
  	if (!nfs_lock_request_dontget(req))
c6a556b88   Trond Myklebust   [PATCH] NFS: Make...
114
  		return 0;
2df485a77   Trond Myklebust   nfs: remove extra...
115
  	if (test_bit(PG_MAPPED, &req->wb_flags))
3d4ff43d8   Al Viro   nfs_open_context ...
116
  		radix_tree_tag_set(&NFS_I(req->wb_context->dentry->d_inode)->nfs_page_tree, req->wb_index, NFS_PAGE_TAG_LOCKED);
c6a556b88   Trond Myklebust   [PATCH] NFS: Make...
117
118
119
120
  	return 1;
  }
  
  /**
9fd367f0f   Trond Myklebust   NFS cleanup: Rena...
121
   * nfs_clear_page_tag_locked - Clear request tag and wake up sleepers
c6a556b88   Trond Myklebust   [PATCH] NFS: Make...
122
   */
9fd367f0f   Trond Myklebust   NFS cleanup: Rena...
123
  void nfs_clear_page_tag_locked(struct nfs_page *req)
c6a556b88   Trond Myklebust   [PATCH] NFS: Make...
124
  {
2df485a77   Trond Myklebust   nfs: remove extra...
125
  	if (test_bit(PG_MAPPED, &req->wb_flags)) {
3d4ff43d8   Al Viro   nfs_open_context ...
126
  		struct inode *inode = req->wb_context->dentry->d_inode;
bb6fbc454   Trond Myklebust   NFS: Avoid a dead...
127
  		struct nfs_inode *nfsi = NFS_I(inode);
587142f85   Trond Myklebust   NFS: Replace NFS_...
128
  		spin_lock(&inode->i_lock);
9fd367f0f   Trond Myklebust   NFS cleanup: Rena...
129
  		radix_tree_tag_clear(&nfsi->nfs_page_tree, req->wb_index, NFS_PAGE_TAG_LOCKED);
acee478af   Trond Myklebust   NFS: Clean up the...
130
  		nfs_unlock_request(req);
587142f85   Trond Myklebust   NFS: Replace NFS_...
131
  		spin_unlock(&inode->i_lock);
acee478af   Trond Myklebust   NFS: Clean up the...
132
133
  	} else
  		nfs_unlock_request(req);
c6a556b88   Trond Myklebust   [PATCH] NFS: Make...
134
  }
4d65c520f   Trond Myklebust   NFS: Fix a hang i...
135
  /*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
136
137
138
   * nfs_clear_request - Free up all resources allocated to the request
   * @req:
   *
bb6fbc454   Trond Myklebust   NFS: Avoid a dead...
139
140
   * Release page and open context resources associated with a read/write
   * request after it has completed.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
141
   */
4d65c520f   Trond Myklebust   NFS: Fix a hang i...
142
  static void nfs_clear_request(struct nfs_page *req)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
143
  {
cd52ed355   Trond Myklebust   NFS: Avoid races ...
144
  	struct page *page = req->wb_page;
bb6fbc454   Trond Myklebust   NFS: Avoid a dead...
145
  	struct nfs_open_context *ctx = req->wb_context;
f11ac8db5   Trond Myklebust   NFSv4: Ensure tha...
146
  	struct nfs_lock_context *l_ctx = req->wb_lock_context;
bb6fbc454   Trond Myklebust   NFS: Avoid a dead...
147

cd52ed355   Trond Myklebust   NFS: Avoid races ...
148
  	if (page != NULL) {
cd52ed355   Trond Myklebust   NFS: Avoid races ...
149
  		page_cache_release(page);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
150
151
  		req->wb_page = NULL;
  	}
f11ac8db5   Trond Myklebust   NFSv4: Ensure tha...
152
153
154
155
  	if (l_ctx != NULL) {
  		nfs_put_lock_context(l_ctx);
  		req->wb_lock_context = NULL;
  	}
bb6fbc454   Trond Myklebust   NFS: Avoid a dead...
156
157
158
159
  	if (ctx != NULL) {
  		put_nfs_open_context(ctx);
  		req->wb_context = NULL;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
160
161
162
163
164
165
166
167
168
  }
  
  
  /**
   * nfs_release_request - Release the count on an NFS read/write request
   * @req: request to release
   *
   * Note: Should never be called with the spinlock held!
   */
c03b40246   Trond Myklebust   NFS: Convert stru...
169
  static void nfs_free_request(struct kref *kref)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
170
  {
c03b40246   Trond Myklebust   NFS: Convert stru...
171
  	struct nfs_page *req = container_of(kref, struct nfs_page, wb_kref);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
172

bb6fbc454   Trond Myklebust   NFS: Avoid a dead...
173
  	/* Release struct file and open context */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
174
  	nfs_clear_request(req);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
175
176
  	nfs_page_free(req);
  }
c03b40246   Trond Myklebust   NFS: Convert stru...
177
178
179
180
  void nfs_release_request(struct nfs_page *req)
  {
  	kref_put(&req->wb_kref, nfs_free_request);
  }
9f557cd80   Trond Myklebust   NFS: Fix an Oops ...
181
182
183
184
185
  static int nfs_wait_bit_uninterruptible(void *word)
  {
  	io_schedule();
  	return 0;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
186
187
188
189
  /**
   * nfs_wait_on_request - Wait for a request to complete.
   * @req: request to wait upon.
   *
150030b78   Matthew Wilcox   NFS: Switch from ...
190
   * Interruptible by fatal signals only.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
191
192
193
194
195
   * The user is responsible for holding a count on the request.
   */
  int
  nfs_wait_on_request(struct nfs_page *req)
  {
9f557cd80   Trond Myklebust   NFS: Fix an Oops ...
196
197
198
  	return wait_on_bit(&req->wb_flags, PG_BUSY,
  			nfs_wait_bit_uninterruptible,
  			TASK_UNINTERRUPTIBLE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
199
  }
19345cb29   Benny Halevy   NFSv4.1: file lay...
200
  bool nfs_generic_pg_test(struct nfs_pageio_descriptor *desc, struct nfs_page *prev, struct nfs_page *req)
5b36c7dc4   Boaz Harrosh   NFSv4.1: define n...
201
202
203
204
205
206
207
208
209
210
211
212
213
  {
  	/*
  	 * FIXME: ideally we should be able to coalesce all requests
  	 * that are not block boundary aligned, but currently this
  	 * is problematic for the case of bsize < PAGE_CACHE_SIZE,
  	 * since nfs_flush_multi and nfs_pagein_multi assume you
  	 * can have only one struct nfs_page.
  	 */
  	if (desc->pg_bsize < PAGE_SIZE)
  		return 0;
  
  	return desc->pg_count + req->wb_bytes <= desc->pg_bsize;
  }
19345cb29   Benny Halevy   NFSv4.1: file lay...
214
  EXPORT_SYMBOL_GPL(nfs_generic_pg_test);
5b36c7dc4   Boaz Harrosh   NFSv4.1: define n...
215

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
216
  /**
d8a5ad75c   Trond Myklebust   NFS: Cleanup the ...
217
218
   * nfs_pageio_init - initialise a page io descriptor
   * @desc: pointer to descriptor
bcb71bba7   Trond Myklebust   NFS: Another clea...
219
220
221
222
   * @inode: pointer to inode
   * @doio: pointer to io function
   * @bsize: io block size
   * @io_flags: extra parameters for the io function
d8a5ad75c   Trond Myklebust   NFS: Cleanup the ...
223
   */
bcb71bba7   Trond Myklebust   NFS: Another clea...
224
225
  void nfs_pageio_init(struct nfs_pageio_descriptor *desc,
  		     struct inode *inode,
1751c3638   Trond Myklebust   NFS: Cleanup of t...
226
  		     const struct nfs_pageio_ops *pg_ops,
84dde76c4   Trond Myklebust   NFS: Fix a compil...
227
  		     size_t bsize,
bcb71bba7   Trond Myklebust   NFS: Another clea...
228
  		     int io_flags)
d8a5ad75c   Trond Myklebust   NFS: Cleanup the ...
229
230
  {
  	INIT_LIST_HEAD(&desc->pg_list);
bcb71bba7   Trond Myklebust   NFS: Another clea...
231
  	desc->pg_bytes_written = 0;
d8a5ad75c   Trond Myklebust   NFS: Cleanup the ...
232
233
234
  	desc->pg_count = 0;
  	desc->pg_bsize = bsize;
  	desc->pg_base = 0;
b31268ac7   Trond Myklebust   FS: Use stable wr...
235
  	desc->pg_moreio = 0;
d9156f9f3   Trond Myklebust   NFS: Allow the nf...
236
  	desc->pg_recoalesce = 0;
bcb71bba7   Trond Myklebust   NFS: Another clea...
237
  	desc->pg_inode = inode;
1751c3638   Trond Myklebust   NFS: Cleanup of t...
238
  	desc->pg_ops = pg_ops;
bcb71bba7   Trond Myklebust   NFS: Another clea...
239
240
  	desc->pg_ioflags = io_flags;
  	desc->pg_error = 0;
94ad1c80e   Fred Isaman   NFSv4.1: coelesce...
241
  	desc->pg_lseg = NULL;
d8a5ad75c   Trond Myklebust   NFS: Cleanup the ...
242
243
244
245
246
247
248
249
250
251
252
253
254
  }
  
  /**
   * nfs_can_coalesce_requests - test two requests for compatibility
   * @prev: pointer to nfs_page
   * @req: pointer to nfs_page
   *
   * The nfs_page structures 'prev' and 'req' are compared to ensure that the
   * page data area they describe is contiguous, and that their RPC
   * credentials, NFSv4 open state, and lockowners are the same.
   *
   * Return 'true' if this is the case, else return 'false'.
   */
18ad0a9f2   Benny Halevy   NFSv4.1: change p...
255
256
257
  static bool nfs_can_coalesce_requests(struct nfs_page *prev,
  				      struct nfs_page *req,
  				      struct nfs_pageio_descriptor *pgio)
d8a5ad75c   Trond Myklebust   NFS: Cleanup the ...
258
259
  {
  	if (req->wb_context->cred != prev->wb_context->cred)
18ad0a9f2   Benny Halevy   NFSv4.1: change p...
260
  		return false;
f11ac8db5   Trond Myklebust   NFSv4: Ensure tha...
261
  	if (req->wb_lock_context->lockowner != prev->wb_lock_context->lockowner)
18ad0a9f2   Benny Halevy   NFSv4.1: change p...
262
  		return false;
d8a5ad75c   Trond Myklebust   NFS: Cleanup the ...
263
  	if (req->wb_context->state != prev->wb_context->state)
18ad0a9f2   Benny Halevy   NFSv4.1: change p...
264
  		return false;
d8a5ad75c   Trond Myklebust   NFS: Cleanup the ...
265
  	if (req->wb_index != (prev->wb_index + 1))
18ad0a9f2   Benny Halevy   NFSv4.1: change p...
266
  		return false;
d8a5ad75c   Trond Myklebust   NFS: Cleanup the ...
267
  	if (req->wb_pgbase != 0)
18ad0a9f2   Benny Halevy   NFSv4.1: change p...
268
  		return false;
d8a5ad75c   Trond Myklebust   NFS: Cleanup the ...
269
  	if (prev->wb_pgbase + prev->wb_bytes != PAGE_CACHE_SIZE)
18ad0a9f2   Benny Halevy   NFSv4.1: change p...
270
  		return false;
1751c3638   Trond Myklebust   NFS: Cleanup of t...
271
  	return pgio->pg_ops->pg_test(pgio, prev, req);
d8a5ad75c   Trond Myklebust   NFS: Cleanup the ...
272
273
274
  }
  
  /**
bcb71bba7   Trond Myklebust   NFS: Another clea...
275
   * nfs_pageio_do_add_request - Attempt to coalesce a request into a page list.
d8a5ad75c   Trond Myklebust   NFS: Cleanup the ...
276
277
278
279
280
281
   * @desc: destination io descriptor
   * @req: request
   *
   * Returns true if the request 'req' was successfully coalesced into the
   * existing list of pages 'desc'.
   */
bcb71bba7   Trond Myklebust   NFS: Another clea...
282
283
  static int nfs_pageio_do_add_request(struct nfs_pageio_descriptor *desc,
  				     struct nfs_page *req)
d8a5ad75c   Trond Myklebust   NFS: Cleanup the ...
284
  {
d8a5ad75c   Trond Myklebust   NFS: Cleanup the ...
285
286
  	if (desc->pg_count != 0) {
  		struct nfs_page *prev;
d8a5ad75c   Trond Myklebust   NFS: Cleanup the ...
287
  		prev = nfs_list_entry(desc->pg_list.prev);
94ad1c80e   Fred Isaman   NFSv4.1: coelesce...
288
  		if (!nfs_can_coalesce_requests(prev, req, desc))
d8a5ad75c   Trond Myklebust   NFS: Cleanup the ...
289
  			return 0;
5b36c7dc4   Boaz Harrosh   NFSv4.1: define n...
290
  	} else {
d8007d4dd   Trond Myklebust   NFSv4.1: Add an i...
291
292
  		if (desc->pg_ops->pg_init)
  			desc->pg_ops->pg_init(desc, req);
d8a5ad75c   Trond Myklebust   NFS: Cleanup the ...
293
  		desc->pg_base = req->wb_pgbase;
5b36c7dc4   Boaz Harrosh   NFSv4.1: define n...
294
  	}
d8a5ad75c   Trond Myklebust   NFS: Cleanup the ...
295
296
  	nfs_list_remove_request(req);
  	nfs_list_add_request(req, &desc->pg_list);
5b36c7dc4   Boaz Harrosh   NFSv4.1: define n...
297
  	desc->pg_count += req->wb_bytes;
d8a5ad75c   Trond Myklebust   NFS: Cleanup the ...
298
299
  	return 1;
  }
bcb71bba7   Trond Myklebust   NFS: Another clea...
300
301
302
303
304
305
  /*
   * Helper for nfs_pageio_add_request and nfs_pageio_complete
   */
  static void nfs_pageio_doio(struct nfs_pageio_descriptor *desc)
  {
  	if (!list_empty(&desc->pg_list)) {
1751c3638   Trond Myklebust   NFS: Cleanup of t...
306
  		int error = desc->pg_ops->pg_doio(desc);
bcb71bba7   Trond Myklebust   NFS: Another clea...
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
  		if (error < 0)
  			desc->pg_error = error;
  		else
  			desc->pg_bytes_written += desc->pg_count;
  	}
  	if (list_empty(&desc->pg_list)) {
  		desc->pg_count = 0;
  		desc->pg_base = 0;
  	}
  }
  
  /**
   * nfs_pageio_add_request - Attempt to coalesce a request into a page list.
   * @desc: destination io descriptor
   * @req: request
   *
   * Returns true if the request 'req' was successfully coalesced into the
   * existing list of pages 'desc'.
   */
d9156f9f3   Trond Myklebust   NFS: Allow the nf...
326
  static int __nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
8b09bee30   Trond Myklebust   NFS: Cleanup for ...
327
  			   struct nfs_page *req)
bcb71bba7   Trond Myklebust   NFS: Another clea...
328
329
  {
  	while (!nfs_pageio_do_add_request(desc, req)) {
b31268ac7   Trond Myklebust   FS: Use stable wr...
330
  		desc->pg_moreio = 1;
bcb71bba7   Trond Myklebust   NFS: Another clea...
331
332
333
  		nfs_pageio_doio(desc);
  		if (desc->pg_error < 0)
  			return 0;
b31268ac7   Trond Myklebust   FS: Use stable wr...
334
  		desc->pg_moreio = 0;
d9156f9f3   Trond Myklebust   NFS: Allow the nf...
335
336
  		if (desc->pg_recoalesce)
  			return 0;
bcb71bba7   Trond Myklebust   NFS: Another clea...
337
338
339
  	}
  	return 1;
  }
d9156f9f3   Trond Myklebust   NFS: Allow the nf...
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
373
374
375
376
377
378
379
380
  static int nfs_do_recoalesce(struct nfs_pageio_descriptor *desc)
  {
  	LIST_HEAD(head);
  
  	do {
  		list_splice_init(&desc->pg_list, &head);
  		desc->pg_bytes_written -= desc->pg_count;
  		desc->pg_count = 0;
  		desc->pg_base = 0;
  		desc->pg_recoalesce = 0;
  
  		while (!list_empty(&head)) {
  			struct nfs_page *req;
  
  			req = list_first_entry(&head, struct nfs_page, wb_list);
  			nfs_list_remove_request(req);
  			if (__nfs_pageio_add_request(desc, req))
  				continue;
  			if (desc->pg_error < 0)
  				return 0;
  			break;
  		}
  	} while (desc->pg_recoalesce);
  	return 1;
  }
  
  int nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
  		struct nfs_page *req)
  {
  	int ret;
  
  	do {
  		ret = __nfs_pageio_add_request(desc, req);
  		if (ret)
  			break;
  		if (desc->pg_error < 0)
  			break;
  		ret = nfs_do_recoalesce(desc);
  	} while (ret);
  	return ret;
  }
d8a5ad75c   Trond Myklebust   NFS: Cleanup the ...
381
  /**
bcb71bba7   Trond Myklebust   NFS: Another clea...
382
383
384
385
386
   * nfs_pageio_complete - Complete I/O on an nfs_pageio_descriptor
   * @desc: pointer to io descriptor
   */
  void nfs_pageio_complete(struct nfs_pageio_descriptor *desc)
  {
d9156f9f3   Trond Myklebust   NFS: Allow the nf...
387
388
389
390
391
392
393
  	for (;;) {
  		nfs_pageio_doio(desc);
  		if (!desc->pg_recoalesce)
  			break;
  		if (!nfs_do_recoalesce(desc))
  			break;
  	}
bcb71bba7   Trond Myklebust   NFS: Another clea...
394
  }
7fe7f8487   Trond Myklebust   NFS: Avoid a dead...
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
  /**
   * nfs_pageio_cond_complete - Conditional I/O completion
   * @desc: pointer to io descriptor
   * @index: page index
   *
   * It is important to ensure that processes don't try to take locks
   * on non-contiguous ranges of pages as that might deadlock. This
   * function should be called before attempting to wait on a locked
   * nfs_page. It will complete the I/O if the page index 'index'
   * is not contiguous with the existing list of pages in 'desc'.
   */
  void nfs_pageio_cond_complete(struct nfs_pageio_descriptor *desc, pgoff_t index)
  {
  	if (!list_empty(&desc->pg_list)) {
  		struct nfs_page *prev = nfs_list_entry(desc->pg_list.prev);
  		if (index != prev->wb_index + 1)
d9156f9f3   Trond Myklebust   NFS: Allow the nf...
411
  			nfs_pageio_complete(desc);
7fe7f8487   Trond Myklebust   NFS: Avoid a dead...
412
413
  	}
  }
3da28eb1c   Trond Myklebust   [PATCH] NFS: Repl...
414
415
  #define NFS_SCAN_MAXENTRIES 16
  /**
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
416
   * nfs_scan_list - Scan a list for matching requests
d2ccddf04   Trond Myklebust   NFS: Flesh out nf...
417
   * @nfsi: NFS inode
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
418
419
420
   * @dst: Destination list
   * @idx_start: lower bound of page->index to scan
   * @npages: idx_start + npages sets the upper bound to scan.
5c3696834   Trond Myklebust   NFS cleanup: spee...
421
   * @tag: tag to scan for
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
422
423
424
425
426
   *
   * Moves elements from one of the inode request lists.
   * If the number of requests is set to 0, the entire address_space
   * starting at index idx_start, is scanned.
   * The requests are *not* checked to ensure that they form a contiguous set.
587142f85   Trond Myklebust   NFS: Replace NFS_...
427
   * You must be holding the inode's i_lock when calling this function
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
428
   */
5c3696834   Trond Myklebust   NFS cleanup: spee...
429
  int nfs_scan_list(struct nfs_inode *nfsi,
ca52fec15   Trond Myklebust   NFS: Use pgoff_t ...
430
  		struct list_head *dst, pgoff_t idx_start,
5c3696834   Trond Myklebust   NFS cleanup: spee...
431
  		unsigned int npages, int tag)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
432
  {
d2ccddf04   Trond Myklebust   NFS: Flesh out nf...
433
434
  	struct nfs_page *pgvec[NFS_SCAN_MAXENTRIES];
  	struct nfs_page *req;
ca52fec15   Trond Myklebust   NFS: Use pgoff_t ...
435
  	pgoff_t idx_end;
d2ccddf04   Trond Myklebust   NFS: Flesh out nf...
436
437
  	int found, i;
  	int res;
a861a1e1c   Fred Isaman   NFSv4.1: add gene...
438
  	struct list_head *list;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
439
440
441
442
443
444
  
  	res = 0;
  	if (npages == 0)
  		idx_end = ~0;
  	else
  		idx_end = idx_start + npages - 1;
d2ccddf04   Trond Myklebust   NFS: Flesh out nf...
445
  	for (;;) {
5c3696834   Trond Myklebust   NFS cleanup: spee...
446
  		found = radix_tree_gang_lookup_tag(&nfsi->nfs_page_tree,
d2ccddf04   Trond Myklebust   NFS: Flesh out nf...
447
  				(void **)&pgvec[0], idx_start,
5c3696834   Trond Myklebust   NFS cleanup: spee...
448
  				NFS_SCAN_MAXENTRIES, tag);
d2ccddf04   Trond Myklebust   NFS: Flesh out nf...
449
  		if (found <= 0)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
450
  			break;
d2ccddf04   Trond Myklebust   NFS: Flesh out nf...
451
452
453
454
455
  		for (i = 0; i < found; i++) {
  			req = pgvec[i];
  			if (req->wb_index > idx_end)
  				goto out;
  			idx_start = req->wb_index + 1;
9fd367f0f   Trond Myklebust   NFS cleanup: Rena...
456
  			if (nfs_set_page_tag_locked(req)) {
acee478af   Trond Myklebust   NFS: Clean up the...
457
  				kref_get(&req->wb_kref);
5c3696834   Trond Myklebust   NFS cleanup: spee...
458
459
  				radix_tree_tag_clear(&nfsi->nfs_page_tree,
  						req->wb_index, tag);
a861a1e1c   Fred Isaman   NFSv4.1: add gene...
460
461
  				list = pnfs_choose_commit_list(req, dst);
  				nfs_list_add_request(req, list);
d2ccddf04   Trond Myklebust   NFS: Flesh out nf...
462
  				res++;
dce34ce29   Trond Myklebust   NFS: Prevent inte...
463
464
  				if (res == INT_MAX)
  					goto out;
d2ccddf04   Trond Myklebust   NFS: Flesh out nf...
465
466
  			}
  		}
edc05fc1c   Trond Myklebust   NFS: reduce laten...
467
  		/* for latency reduction */
587142f85   Trond Myklebust   NFS: Replace NFS_...
468
  		cond_resched_lock(&nfsi->vfs_inode.i_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
469
  	}
d2ccddf04   Trond Myklebust   NFS: Flesh out nf...
470
  out:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
471
472
  	return res;
  }
f7b422b17   David Howells   NFS: Split fs/nfs...
473
  int __init nfs_init_nfspagecache(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
474
475
476
477
  {
  	nfs_page_cachep = kmem_cache_create("nfs_page",
  					    sizeof(struct nfs_page),
  					    0, SLAB_HWCACHE_ALIGN,
20c2df83d   Paul Mundt   mm: Remove slab d...
478
  					    NULL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
479
480
481
482
483
  	if (nfs_page_cachep == NULL)
  		return -ENOMEM;
  
  	return 0;
  }
266bee886   David Brownell   [PATCH] fix stati...
484
  void nfs_destroy_nfspagecache(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
485
  {
1a1d92c10   Alexey Dobriyan   [PATCH] Really ig...
486
  	kmem_cache_destroy(nfs_page_cachep);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
487
  }