Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/fs/nfs/pagelist.c | |
3 | * | |
4 | * A set of helper functions for managing NFS read and write requests. | |
5 | * The main purpose of these routines is to provide support for the | |
6 | * coalescing of several requests into a single RPC call. | |
7 | * | |
8 | * Copyright 2000, 2001 (c) Trond Myklebust <trond.myklebust@fys.uio.no> | |
9 | * | |
10 | */ | |
11 | ||
1da177e4 LT |
12 | #include <linux/slab.h> |
13 | #include <linux/file.h> | |
e8edc6e0 | 14 | #include <linux/sched.h> |
1da177e4 | 15 | #include <linux/sunrpc/clnt.h> |
1313e603 | 16 | #include <linux/nfs.h> |
1da177e4 LT |
17 | #include <linux/nfs3.h> |
18 | #include <linux/nfs4.h> | |
19 | #include <linux/nfs_page.h> | |
20 | #include <linux/nfs_fs.h> | |
21 | #include <linux/nfs_mount.h> | |
afeacc8c | 22 | #include <linux/export.h> |
1da177e4 | 23 | |
8d5658c9 | 24 | #include "internal.h" |
bae724ef | 25 | #include "pnfs.h" |
8d5658c9 | 26 | |
e18b890b | 27 | static struct kmem_cache *nfs_page_cachep; |
1da177e4 | 28 | |
30dd374f FI |
29 | bool nfs_pgarray_set(struct nfs_page_array *p, unsigned int pagecount) |
30 | { | |
31 | p->npages = pagecount; | |
32 | if (pagecount <= ARRAY_SIZE(p->page_array)) | |
33 | p->pagevec = p->page_array; | |
34 | else { | |
35 | p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_KERNEL); | |
36 | if (!p->pagevec) | |
37 | p->npages = 0; | |
38 | } | |
39 | return p->pagevec != NULL; | |
40 | } | |
41 | ||
1da177e4 LT |
42 | static inline struct nfs_page * |
43 | nfs_page_alloc(void) | |
44 | { | |
72895b1a JJ |
45 | struct nfs_page *p = kmem_cache_zalloc(nfs_page_cachep, GFP_KERNEL); |
46 | if (p) | |
1da177e4 | 47 | INIT_LIST_HEAD(&p->wb_list); |
1da177e4 LT |
48 | return p; |
49 | } | |
50 | ||
51 | static inline void | |
52 | nfs_page_free(struct nfs_page *p) | |
53 | { | |
54 | kmem_cache_free(nfs_page_cachep, p); | |
55 | } | |
56 | ||
57 | /** | |
58 | * nfs_create_request - Create an NFS read/write request. | |
c02f557d | 59 | * @ctx: open context to use |
1da177e4 LT |
60 | * @inode: inode to which the request is attached |
61 | * @page: page to write | |
62 | * @offset: starting offset within the page for the write | |
63 | * @count: number of bytes to read/write | |
64 | * | |
65 | * The page must be locked by the caller. This makes sure we never | |
a19b89ca | 66 | * create two different requests for the same page. |
1da177e4 LT |
67 | * User should ensure it is safe to sleep in this function. |
68 | */ | |
69 | struct nfs_page * | |
70 | nfs_create_request(struct nfs_open_context *ctx, struct inode *inode, | |
71 | struct page *page, | |
72 | unsigned int offset, unsigned int count) | |
73 | { | |
1da177e4 LT |
74 | struct nfs_page *req; |
75 | ||
18eb8842 TM |
76 | /* try to allocate the request struct */ |
77 | req = nfs_page_alloc(); | |
78 | if (req == NULL) | |
79 | return ERR_PTR(-ENOMEM); | |
1da177e4 | 80 | |
015f0212 JL |
81 | /* get lock context early so we can deal with alloc failures */ |
82 | req->wb_lock_context = nfs_get_lock_context(ctx); | |
83 | if (req->wb_lock_context == NULL) { | |
84 | nfs_page_free(req); | |
85 | return ERR_PTR(-ENOMEM); | |
86 | } | |
87 | ||
1da177e4 LT |
88 | /* Initialize the request struct. Initially, we assume a |
89 | * long write-back delay. This will be adjusted in | |
90 | * update_nfs_request below if the region is not locked. */ | |
91 | req->wb_page = page; | |
92 | atomic_set(&req->wb_complete, 0); | |
93 | req->wb_index = page->index; | |
94 | page_cache_get(page); | |
cd52ed35 TM |
95 | BUG_ON(PagePrivate(page)); |
96 | BUG_ON(!PageLocked(page)); | |
97 | BUG_ON(page->mapping->host != inode); | |
1da177e4 LT |
98 | req->wb_offset = offset; |
99 | req->wb_pgbase = offset; | |
100 | req->wb_bytes = count; | |
1da177e4 | 101 | req->wb_context = get_nfs_open_context(ctx); |
c03b4024 | 102 | kref_init(&req->wb_kref); |
1da177e4 LT |
103 | return req; |
104 | } | |
105 | ||
106 | /** | |
107 | * nfs_unlock_request - Unlock request and wake up sleepers. | |
108 | * @req: | |
109 | */ | |
110 | void nfs_unlock_request(struct nfs_page *req) | |
111 | { | |
112 | if (!NFS_WBACK_BUSY(req)) { | |
113 | printk(KERN_ERR "NFS: Invalid unlock attempted\n"); | |
114 | BUG(); | |
115 | } | |
116 | smp_mb__before_clear_bit(); | |
117 | clear_bit(PG_BUSY, &req->wb_flags); | |
118 | smp_mb__after_clear_bit(); | |
464a98bd | 119 | wake_up_bit(&req->wb_flags, PG_BUSY); |
1da177e4 LT |
120 | nfs_release_request(req); |
121 | } | |
122 | ||
4d65c520 | 123 | /* |
1da177e4 LT |
124 | * nfs_clear_request - Free up all resources allocated to the request |
125 | * @req: | |
126 | * | |
bb6fbc45 TM |
127 | * Release page and open context resources associated with a read/write |
128 | * request after it has completed. | |
1da177e4 | 129 | */ |
4d65c520 | 130 | static void nfs_clear_request(struct nfs_page *req) |
1da177e4 | 131 | { |
cd52ed35 | 132 | struct page *page = req->wb_page; |
bb6fbc45 | 133 | struct nfs_open_context *ctx = req->wb_context; |
f11ac8db | 134 | struct nfs_lock_context *l_ctx = req->wb_lock_context; |
bb6fbc45 | 135 | |
cd52ed35 | 136 | if (page != NULL) { |
cd52ed35 | 137 | page_cache_release(page); |
1da177e4 LT |
138 | req->wb_page = NULL; |
139 | } | |
f11ac8db TM |
140 | if (l_ctx != NULL) { |
141 | nfs_put_lock_context(l_ctx); | |
142 | req->wb_lock_context = NULL; | |
143 | } | |
bb6fbc45 TM |
144 | if (ctx != NULL) { |
145 | put_nfs_open_context(ctx); | |
146 | req->wb_context = NULL; | |
147 | } | |
1da177e4 LT |
148 | } |
149 | ||
150 | ||
151 | /** | |
152 | * nfs_release_request - Release the count on an NFS read/write request | |
153 | * @req: request to release | |
154 | * | |
155 | * Note: Should never be called with the spinlock held! | |
156 | */ | |
c03b4024 | 157 | static void nfs_free_request(struct kref *kref) |
1da177e4 | 158 | { |
c03b4024 | 159 | struct nfs_page *req = container_of(kref, struct nfs_page, wb_kref); |
1da177e4 | 160 | |
bb6fbc45 | 161 | /* Release struct file and open context */ |
1da177e4 | 162 | nfs_clear_request(req); |
1da177e4 LT |
163 | nfs_page_free(req); |
164 | } | |
165 | ||
c03b4024 TM |
166 | void nfs_release_request(struct nfs_page *req) |
167 | { | |
168 | kref_put(&req->wb_kref, nfs_free_request); | |
169 | } | |
170 | ||
9f557cd8 TM |
171 | static int nfs_wait_bit_uninterruptible(void *word) |
172 | { | |
173 | io_schedule(); | |
174 | return 0; | |
175 | } | |
176 | ||
1da177e4 LT |
177 | /** |
178 | * nfs_wait_on_request - Wait for a request to complete. | |
179 | * @req: request to wait upon. | |
180 | * | |
150030b7 | 181 | * Interruptible by fatal signals only. |
1da177e4 LT |
182 | * The user is responsible for holding a count on the request. |
183 | */ | |
184 | int | |
185 | nfs_wait_on_request(struct nfs_page *req) | |
186 | { | |
9f557cd8 TM |
187 | return wait_on_bit(&req->wb_flags, PG_BUSY, |
188 | nfs_wait_bit_uninterruptible, | |
189 | TASK_UNINTERRUPTIBLE); | |
1da177e4 LT |
190 | } |
191 | ||
19345cb2 | 192 | bool nfs_generic_pg_test(struct nfs_pageio_descriptor *desc, struct nfs_page *prev, struct nfs_page *req) |
5b36c7dc BH |
193 | { |
194 | /* | |
195 | * FIXME: ideally we should be able to coalesce all requests | |
196 | * that are not block boundary aligned, but currently this | |
197 | * is problematic for the case of bsize < PAGE_CACHE_SIZE, | |
198 | * since nfs_flush_multi and nfs_pagein_multi assume you | |
199 | * can have only one struct nfs_page. | |
200 | */ | |
201 | if (desc->pg_bsize < PAGE_SIZE) | |
202 | return 0; | |
203 | ||
204 | return desc->pg_count + req->wb_bytes <= desc->pg_bsize; | |
205 | } | |
19345cb2 | 206 | EXPORT_SYMBOL_GPL(nfs_generic_pg_test); |
5b36c7dc | 207 | |
1da177e4 | 208 | /** |
d8a5ad75 TM |
209 | * nfs_pageio_init - initialise a page io descriptor |
210 | * @desc: pointer to descriptor | |
bcb71bba TM |
211 | * @inode: pointer to inode |
212 | * @doio: pointer to io function | |
213 | * @bsize: io block size | |
214 | * @io_flags: extra parameters for the io function | |
d8a5ad75 | 215 | */ |
bcb71bba TM |
216 | void nfs_pageio_init(struct nfs_pageio_descriptor *desc, |
217 | struct inode *inode, | |
1751c363 | 218 | const struct nfs_pageio_ops *pg_ops, |
84dde76c | 219 | size_t bsize, |
bcb71bba | 220 | int io_flags) |
d8a5ad75 TM |
221 | { |
222 | INIT_LIST_HEAD(&desc->pg_list); | |
bcb71bba | 223 | desc->pg_bytes_written = 0; |
d8a5ad75 TM |
224 | desc->pg_count = 0; |
225 | desc->pg_bsize = bsize; | |
226 | desc->pg_base = 0; | |
b31268ac | 227 | desc->pg_moreio = 0; |
d9156f9f | 228 | desc->pg_recoalesce = 0; |
bcb71bba | 229 | desc->pg_inode = inode; |
1751c363 | 230 | desc->pg_ops = pg_ops; |
bcb71bba TM |
231 | desc->pg_ioflags = io_flags; |
232 | desc->pg_error = 0; | |
94ad1c80 | 233 | desc->pg_lseg = NULL; |
d8a5ad75 TM |
234 | } |
235 | ||
236 | /** | |
237 | * nfs_can_coalesce_requests - test two requests for compatibility | |
238 | * @prev: pointer to nfs_page | |
239 | * @req: pointer to nfs_page | |
240 | * | |
241 | * The nfs_page structures 'prev' and 'req' are compared to ensure that the | |
242 | * page data area they describe is contiguous, and that their RPC | |
243 | * credentials, NFSv4 open state, and lockowners are the same. | |
244 | * | |
245 | * Return 'true' if this is the case, else return 'false'. | |
246 | */ | |
18ad0a9f BH |
247 | static bool nfs_can_coalesce_requests(struct nfs_page *prev, |
248 | struct nfs_page *req, | |
249 | struct nfs_pageio_descriptor *pgio) | |
d8a5ad75 TM |
250 | { |
251 | if (req->wb_context->cred != prev->wb_context->cred) | |
18ad0a9f | 252 | return false; |
f11ac8db | 253 | if (req->wb_lock_context->lockowner != prev->wb_lock_context->lockowner) |
18ad0a9f | 254 | return false; |
d8a5ad75 | 255 | if (req->wb_context->state != prev->wb_context->state) |
18ad0a9f | 256 | return false; |
d8a5ad75 | 257 | if (req->wb_index != (prev->wb_index + 1)) |
18ad0a9f | 258 | return false; |
d8a5ad75 | 259 | if (req->wb_pgbase != 0) |
18ad0a9f | 260 | return false; |
d8a5ad75 | 261 | if (prev->wb_pgbase + prev->wb_bytes != PAGE_CACHE_SIZE) |
18ad0a9f | 262 | return false; |
1751c363 | 263 | return pgio->pg_ops->pg_test(pgio, prev, req); |
d8a5ad75 TM |
264 | } |
265 | ||
266 | /** | |
bcb71bba | 267 | * nfs_pageio_do_add_request - Attempt to coalesce a request into a page list. |
d8a5ad75 TM |
268 | * @desc: destination io descriptor |
269 | * @req: request | |
270 | * | |
271 | * Returns true if the request 'req' was successfully coalesced into the | |
272 | * existing list of pages 'desc'. | |
273 | */ | |
bcb71bba TM |
274 | static int nfs_pageio_do_add_request(struct nfs_pageio_descriptor *desc, |
275 | struct nfs_page *req) | |
d8a5ad75 | 276 | { |
d8a5ad75 TM |
277 | if (desc->pg_count != 0) { |
278 | struct nfs_page *prev; | |
279 | ||
d8a5ad75 | 280 | prev = nfs_list_entry(desc->pg_list.prev); |
94ad1c80 | 281 | if (!nfs_can_coalesce_requests(prev, req, desc)) |
d8a5ad75 | 282 | return 0; |
5b36c7dc | 283 | } else { |
d8007d4d TM |
284 | if (desc->pg_ops->pg_init) |
285 | desc->pg_ops->pg_init(desc, req); | |
d8a5ad75 | 286 | desc->pg_base = req->wb_pgbase; |
5b36c7dc | 287 | } |
d8a5ad75 TM |
288 | nfs_list_remove_request(req); |
289 | nfs_list_add_request(req, &desc->pg_list); | |
5b36c7dc | 290 | desc->pg_count += req->wb_bytes; |
d8a5ad75 TM |
291 | return 1; |
292 | } | |
293 | ||
bcb71bba TM |
294 | /* |
295 | * Helper for nfs_pageio_add_request and nfs_pageio_complete | |
296 | */ | |
297 | static void nfs_pageio_doio(struct nfs_pageio_descriptor *desc) | |
298 | { | |
299 | if (!list_empty(&desc->pg_list)) { | |
1751c363 | 300 | int error = desc->pg_ops->pg_doio(desc); |
bcb71bba TM |
301 | if (error < 0) |
302 | desc->pg_error = error; | |
303 | else | |
304 | desc->pg_bytes_written += desc->pg_count; | |
305 | } | |
306 | if (list_empty(&desc->pg_list)) { | |
307 | desc->pg_count = 0; | |
308 | desc->pg_base = 0; | |
309 | } | |
310 | } | |
311 | ||
312 | /** | |
313 | * nfs_pageio_add_request - Attempt to coalesce a request into a page list. | |
314 | * @desc: destination io descriptor | |
315 | * @req: request | |
316 | * | |
317 | * Returns true if the request 'req' was successfully coalesced into the | |
318 | * existing list of pages 'desc'. | |
319 | */ | |
d9156f9f | 320 | static int __nfs_pageio_add_request(struct nfs_pageio_descriptor *desc, |
8b09bee3 | 321 | struct nfs_page *req) |
bcb71bba TM |
322 | { |
323 | while (!nfs_pageio_do_add_request(desc, req)) { | |
b31268ac | 324 | desc->pg_moreio = 1; |
bcb71bba TM |
325 | nfs_pageio_doio(desc); |
326 | if (desc->pg_error < 0) | |
327 | return 0; | |
b31268ac | 328 | desc->pg_moreio = 0; |
d9156f9f TM |
329 | if (desc->pg_recoalesce) |
330 | return 0; | |
bcb71bba TM |
331 | } |
332 | return 1; | |
333 | } | |
334 | ||
d9156f9f TM |
335 | static int nfs_do_recoalesce(struct nfs_pageio_descriptor *desc) |
336 | { | |
337 | LIST_HEAD(head); | |
338 | ||
339 | do { | |
340 | list_splice_init(&desc->pg_list, &head); | |
341 | desc->pg_bytes_written -= desc->pg_count; | |
342 | desc->pg_count = 0; | |
343 | desc->pg_base = 0; | |
344 | desc->pg_recoalesce = 0; | |
345 | ||
346 | while (!list_empty(&head)) { | |
347 | struct nfs_page *req; | |
348 | ||
349 | req = list_first_entry(&head, struct nfs_page, wb_list); | |
350 | nfs_list_remove_request(req); | |
351 | if (__nfs_pageio_add_request(desc, req)) | |
352 | continue; | |
353 | if (desc->pg_error < 0) | |
354 | return 0; | |
355 | break; | |
356 | } | |
357 | } while (desc->pg_recoalesce); | |
358 | return 1; | |
359 | } | |
360 | ||
361 | int nfs_pageio_add_request(struct nfs_pageio_descriptor *desc, | |
362 | struct nfs_page *req) | |
363 | { | |
364 | int ret; | |
365 | ||
366 | do { | |
367 | ret = __nfs_pageio_add_request(desc, req); | |
368 | if (ret) | |
369 | break; | |
370 | if (desc->pg_error < 0) | |
371 | break; | |
372 | ret = nfs_do_recoalesce(desc); | |
373 | } while (ret); | |
374 | return ret; | |
375 | } | |
376 | ||
bcb71bba TM |
377 | /** |
378 | * nfs_pageio_complete - Complete I/O on an nfs_pageio_descriptor | |
379 | * @desc: pointer to io descriptor | |
380 | */ | |
381 | void nfs_pageio_complete(struct nfs_pageio_descriptor *desc) | |
382 | { | |
d9156f9f TM |
383 | for (;;) { |
384 | nfs_pageio_doio(desc); | |
385 | if (!desc->pg_recoalesce) | |
386 | break; | |
387 | if (!nfs_do_recoalesce(desc)) | |
388 | break; | |
389 | } | |
bcb71bba TM |
390 | } |
391 | ||
7fe7f848 TM |
392 | /** |
393 | * nfs_pageio_cond_complete - Conditional I/O completion | |
394 | * @desc: pointer to io descriptor | |
395 | * @index: page index | |
396 | * | |
397 | * It is important to ensure that processes don't try to take locks | |
398 | * on non-contiguous ranges of pages as that might deadlock. This | |
399 | * function should be called before attempting to wait on a locked | |
400 | * nfs_page. It will complete the I/O if the page index 'index' | |
401 | * is not contiguous with the existing list of pages in 'desc'. | |
402 | */ | |
403 | void nfs_pageio_cond_complete(struct nfs_pageio_descriptor *desc, pgoff_t index) | |
404 | { | |
405 | if (!list_empty(&desc->pg_list)) { | |
406 | struct nfs_page *prev = nfs_list_entry(desc->pg_list.prev); | |
407 | if (index != prev->wb_index + 1) | |
d9156f9f | 408 | nfs_pageio_complete(desc); |
7fe7f848 TM |
409 | } |
410 | } | |
411 | ||
f7b422b1 | 412 | int __init nfs_init_nfspagecache(void) |
1da177e4 LT |
413 | { |
414 | nfs_page_cachep = kmem_cache_create("nfs_page", | |
415 | sizeof(struct nfs_page), | |
416 | 0, SLAB_HWCACHE_ALIGN, | |
20c2df83 | 417 | NULL); |
1da177e4 LT |
418 | if (nfs_page_cachep == NULL) |
419 | return -ENOMEM; | |
420 | ||
421 | return 0; | |
422 | } | |
423 | ||
266bee88 | 424 | void nfs_destroy_nfspagecache(void) |
1da177e4 | 425 | { |
1a1d92c1 | 426 | kmem_cache_destroy(nfs_page_cachep); |
1da177e4 LT |
427 | } |
428 |