SUNRPC: kernel PipeFS mount point creation routines removed
[linux-2.6-block.git] / fs / nfs / pagelist.c
CommitLineData
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
LT
15#include <linux/sunrpc/clnt.h>
16#include <linux/nfs3.h>
17#include <linux/nfs4.h>
18#include <linux/nfs_page.h>
19#include <linux/nfs_fs.h>
20#include <linux/nfs_mount.h>
afeacc8c 21#include <linux/export.h>
1da177e4 22
8d5658c9 23#include "internal.h"
bae724ef 24#include "pnfs.h"
8d5658c9 25
e18b890b 26static struct kmem_cache *nfs_page_cachep;
1da177e4
LT
27
28static inline struct nfs_page *
29nfs_page_alloc(void)
30{
72895b1a
JJ
31 struct nfs_page *p = kmem_cache_zalloc(nfs_page_cachep, GFP_KERNEL);
32 if (p)
1da177e4 33 INIT_LIST_HEAD(&p->wb_list);
1da177e4
LT
34 return p;
35}
36
37static inline void
38nfs_page_free(struct nfs_page *p)
39{
40 kmem_cache_free(nfs_page_cachep, p);
41}
42
43/**
44 * nfs_create_request - Create an NFS read/write request.
c02f557d 45 * @ctx: open context to use
1da177e4
LT
46 * @inode: inode to which the request is attached
47 * @page: page to write
48 * @offset: starting offset within the page for the write
49 * @count: number of bytes to read/write
50 *
51 * The page must be locked by the caller. This makes sure we never
a19b89ca 52 * create two different requests for the same page.
1da177e4
LT
53 * User should ensure it is safe to sleep in this function.
54 */
55struct nfs_page *
56nfs_create_request(struct nfs_open_context *ctx, struct inode *inode,
57 struct page *page,
58 unsigned int offset, unsigned int count)
59{
1da177e4
LT
60 struct nfs_page *req;
61
18eb8842
TM
62 /* try to allocate the request struct */
63 req = nfs_page_alloc();
64 if (req == NULL)
65 return ERR_PTR(-ENOMEM);
1da177e4 66
015f0212
JL
67 /* get lock context early so we can deal with alloc failures */
68 req->wb_lock_context = nfs_get_lock_context(ctx);
69 if (req->wb_lock_context == NULL) {
70 nfs_page_free(req);
71 return ERR_PTR(-ENOMEM);
72 }
73
1da177e4
LT
74 /* Initialize the request struct. Initially, we assume a
75 * long write-back delay. This will be adjusted in
76 * update_nfs_request below if the region is not locked. */
77 req->wb_page = page;
78 atomic_set(&req->wb_complete, 0);
79 req->wb_index = page->index;
80 page_cache_get(page);
cd52ed35
TM
81 BUG_ON(PagePrivate(page));
82 BUG_ON(!PageLocked(page));
83 BUG_ON(page->mapping->host != inode);
1da177e4
LT
84 req->wb_offset = offset;
85 req->wb_pgbase = offset;
86 req->wb_bytes = count;
1da177e4 87 req->wb_context = get_nfs_open_context(ctx);
c03b4024 88 kref_init(&req->wb_kref);
1da177e4
LT
89 return req;
90}
91
92/**
93 * nfs_unlock_request - Unlock request and wake up sleepers.
94 * @req:
95 */
96void nfs_unlock_request(struct nfs_page *req)
97{
98 if (!NFS_WBACK_BUSY(req)) {
99 printk(KERN_ERR "NFS: Invalid unlock attempted\n");
100 BUG();
101 }
102 smp_mb__before_clear_bit();
103 clear_bit(PG_BUSY, &req->wb_flags);
104 smp_mb__after_clear_bit();
464a98bd 105 wake_up_bit(&req->wb_flags, PG_BUSY);
1da177e4
LT
106 nfs_release_request(req);
107}
108
c6a556b8 109/**
9fd367f0 110 * nfs_set_page_tag_locked - Tag a request as locked
c6a556b8
TM
111 * @req:
112 */
acee478a 113int nfs_set_page_tag_locked(struct nfs_page *req)
c6a556b8 114{
acee478a 115 if (!nfs_lock_request_dontget(req))
c6a556b8 116 return 0;
2df485a7 117 if (test_bit(PG_MAPPED, &req->wb_flags))
3d4ff43d 118 radix_tree_tag_set(&NFS_I(req->wb_context->dentry->d_inode)->nfs_page_tree, req->wb_index, NFS_PAGE_TAG_LOCKED);
c6a556b8
TM
119 return 1;
120}
121
122/**
9fd367f0 123 * nfs_clear_page_tag_locked - Clear request tag and wake up sleepers
c6a556b8 124 */
9fd367f0 125void nfs_clear_page_tag_locked(struct nfs_page *req)
c6a556b8 126{
2df485a7 127 if (test_bit(PG_MAPPED, &req->wb_flags)) {
3d4ff43d 128 struct inode *inode = req->wb_context->dentry->d_inode;
bb6fbc45
TM
129 struct nfs_inode *nfsi = NFS_I(inode);
130
587142f8 131 spin_lock(&inode->i_lock);
9fd367f0 132 radix_tree_tag_clear(&nfsi->nfs_page_tree, req->wb_index, NFS_PAGE_TAG_LOCKED);
acee478a 133 nfs_unlock_request(req);
587142f8 134 spin_unlock(&inode->i_lock);
acee478a
TM
135 } else
136 nfs_unlock_request(req);
c6a556b8
TM
137}
138
4d65c520 139/*
1da177e4
LT
140 * nfs_clear_request - Free up all resources allocated to the request
141 * @req:
142 *
bb6fbc45
TM
143 * Release page and open context resources associated with a read/write
144 * request after it has completed.
1da177e4 145 */
4d65c520 146static void nfs_clear_request(struct nfs_page *req)
1da177e4 147{
cd52ed35 148 struct page *page = req->wb_page;
bb6fbc45 149 struct nfs_open_context *ctx = req->wb_context;
f11ac8db 150 struct nfs_lock_context *l_ctx = req->wb_lock_context;
bb6fbc45 151
cd52ed35 152 if (page != NULL) {
cd52ed35 153 page_cache_release(page);
1da177e4
LT
154 req->wb_page = NULL;
155 }
f11ac8db
TM
156 if (l_ctx != NULL) {
157 nfs_put_lock_context(l_ctx);
158 req->wb_lock_context = NULL;
159 }
bb6fbc45
TM
160 if (ctx != NULL) {
161 put_nfs_open_context(ctx);
162 req->wb_context = NULL;
163 }
1da177e4
LT
164}
165
166
167/**
168 * nfs_release_request - Release the count on an NFS read/write request
169 * @req: request to release
170 *
171 * Note: Should never be called with the spinlock held!
172 */
c03b4024 173static void nfs_free_request(struct kref *kref)
1da177e4 174{
c03b4024 175 struct nfs_page *req = container_of(kref, struct nfs_page, wb_kref);
1da177e4 176
bb6fbc45 177 /* Release struct file and open context */
1da177e4 178 nfs_clear_request(req);
1da177e4
LT
179 nfs_page_free(req);
180}
181
c03b4024
TM
182void nfs_release_request(struct nfs_page *req)
183{
184 kref_put(&req->wb_kref, nfs_free_request);
185}
186
9f557cd8
TM
187static int nfs_wait_bit_uninterruptible(void *word)
188{
189 io_schedule();
190 return 0;
191}
192
1da177e4
LT
193/**
194 * nfs_wait_on_request - Wait for a request to complete.
195 * @req: request to wait upon.
196 *
150030b7 197 * Interruptible by fatal signals only.
1da177e4
LT
198 * The user is responsible for holding a count on the request.
199 */
200int
201nfs_wait_on_request(struct nfs_page *req)
202{
9f557cd8
TM
203 return wait_on_bit(&req->wb_flags, PG_BUSY,
204 nfs_wait_bit_uninterruptible,
205 TASK_UNINTERRUPTIBLE);
1da177e4
LT
206}
207
19345cb2 208bool nfs_generic_pg_test(struct nfs_pageio_descriptor *desc, struct nfs_page *prev, struct nfs_page *req)
5b36c7dc
BH
209{
210 /*
211 * FIXME: ideally we should be able to coalesce all requests
212 * that are not block boundary aligned, but currently this
213 * is problematic for the case of bsize < PAGE_CACHE_SIZE,
214 * since nfs_flush_multi and nfs_pagein_multi assume you
215 * can have only one struct nfs_page.
216 */
217 if (desc->pg_bsize < PAGE_SIZE)
218 return 0;
219
220 return desc->pg_count + req->wb_bytes <= desc->pg_bsize;
221}
19345cb2 222EXPORT_SYMBOL_GPL(nfs_generic_pg_test);
5b36c7dc 223
1da177e4 224/**
d8a5ad75
TM
225 * nfs_pageio_init - initialise a page io descriptor
226 * @desc: pointer to descriptor
bcb71bba
TM
227 * @inode: pointer to inode
228 * @doio: pointer to io function
229 * @bsize: io block size
230 * @io_flags: extra parameters for the io function
d8a5ad75 231 */
bcb71bba
TM
232void nfs_pageio_init(struct nfs_pageio_descriptor *desc,
233 struct inode *inode,
1751c363 234 const struct nfs_pageio_ops *pg_ops,
84dde76c 235 size_t bsize,
bcb71bba 236 int io_flags)
d8a5ad75
TM
237{
238 INIT_LIST_HEAD(&desc->pg_list);
bcb71bba 239 desc->pg_bytes_written = 0;
d8a5ad75
TM
240 desc->pg_count = 0;
241 desc->pg_bsize = bsize;
242 desc->pg_base = 0;
b31268ac 243 desc->pg_moreio = 0;
d9156f9f 244 desc->pg_recoalesce = 0;
bcb71bba 245 desc->pg_inode = inode;
1751c363 246 desc->pg_ops = pg_ops;
bcb71bba
TM
247 desc->pg_ioflags = io_flags;
248 desc->pg_error = 0;
94ad1c80 249 desc->pg_lseg = NULL;
d8a5ad75
TM
250}
251
252/**
253 * nfs_can_coalesce_requests - test two requests for compatibility
254 * @prev: pointer to nfs_page
255 * @req: pointer to nfs_page
256 *
257 * The nfs_page structures 'prev' and 'req' are compared to ensure that the
258 * page data area they describe is contiguous, and that their RPC
259 * credentials, NFSv4 open state, and lockowners are the same.
260 *
261 * Return 'true' if this is the case, else return 'false'.
262 */
18ad0a9f
BH
263static bool nfs_can_coalesce_requests(struct nfs_page *prev,
264 struct nfs_page *req,
265 struct nfs_pageio_descriptor *pgio)
d8a5ad75
TM
266{
267 if (req->wb_context->cred != prev->wb_context->cred)
18ad0a9f 268 return false;
f11ac8db 269 if (req->wb_lock_context->lockowner != prev->wb_lock_context->lockowner)
18ad0a9f 270 return false;
d8a5ad75 271 if (req->wb_context->state != prev->wb_context->state)
18ad0a9f 272 return false;
d8a5ad75 273 if (req->wb_index != (prev->wb_index + 1))
18ad0a9f 274 return false;
d8a5ad75 275 if (req->wb_pgbase != 0)
18ad0a9f 276 return false;
d8a5ad75 277 if (prev->wb_pgbase + prev->wb_bytes != PAGE_CACHE_SIZE)
18ad0a9f 278 return false;
1751c363 279 return pgio->pg_ops->pg_test(pgio, prev, req);
d8a5ad75
TM
280}
281
282/**
bcb71bba 283 * nfs_pageio_do_add_request - Attempt to coalesce a request into a page list.
d8a5ad75
TM
284 * @desc: destination io descriptor
285 * @req: request
286 *
287 * Returns true if the request 'req' was successfully coalesced into the
288 * existing list of pages 'desc'.
289 */
bcb71bba
TM
290static int nfs_pageio_do_add_request(struct nfs_pageio_descriptor *desc,
291 struct nfs_page *req)
d8a5ad75 292{
d8a5ad75
TM
293 if (desc->pg_count != 0) {
294 struct nfs_page *prev;
295
d8a5ad75 296 prev = nfs_list_entry(desc->pg_list.prev);
94ad1c80 297 if (!nfs_can_coalesce_requests(prev, req, desc))
d8a5ad75 298 return 0;
5b36c7dc 299 } else {
d8007d4d
TM
300 if (desc->pg_ops->pg_init)
301 desc->pg_ops->pg_init(desc, req);
d8a5ad75 302 desc->pg_base = req->wb_pgbase;
5b36c7dc 303 }
d8a5ad75
TM
304 nfs_list_remove_request(req);
305 nfs_list_add_request(req, &desc->pg_list);
5b36c7dc 306 desc->pg_count += req->wb_bytes;
d8a5ad75
TM
307 return 1;
308}
309
bcb71bba
TM
310/*
311 * Helper for nfs_pageio_add_request and nfs_pageio_complete
312 */
313static void nfs_pageio_doio(struct nfs_pageio_descriptor *desc)
314{
315 if (!list_empty(&desc->pg_list)) {
1751c363 316 int error = desc->pg_ops->pg_doio(desc);
bcb71bba
TM
317 if (error < 0)
318 desc->pg_error = error;
319 else
320 desc->pg_bytes_written += desc->pg_count;
321 }
322 if (list_empty(&desc->pg_list)) {
323 desc->pg_count = 0;
324 desc->pg_base = 0;
325 }
326}
327
328/**
329 * nfs_pageio_add_request - Attempt to coalesce a request into a page list.
330 * @desc: destination io descriptor
331 * @req: request
332 *
333 * Returns true if the request 'req' was successfully coalesced into the
334 * existing list of pages 'desc'.
335 */
d9156f9f 336static int __nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
8b09bee3 337 struct nfs_page *req)
bcb71bba
TM
338{
339 while (!nfs_pageio_do_add_request(desc, req)) {
b31268ac 340 desc->pg_moreio = 1;
bcb71bba
TM
341 nfs_pageio_doio(desc);
342 if (desc->pg_error < 0)
343 return 0;
b31268ac 344 desc->pg_moreio = 0;
d9156f9f
TM
345 if (desc->pg_recoalesce)
346 return 0;
bcb71bba
TM
347 }
348 return 1;
349}
350
d9156f9f
TM
351static int nfs_do_recoalesce(struct nfs_pageio_descriptor *desc)
352{
353 LIST_HEAD(head);
354
355 do {
356 list_splice_init(&desc->pg_list, &head);
357 desc->pg_bytes_written -= desc->pg_count;
358 desc->pg_count = 0;
359 desc->pg_base = 0;
360 desc->pg_recoalesce = 0;
361
362 while (!list_empty(&head)) {
363 struct nfs_page *req;
364
365 req = list_first_entry(&head, struct nfs_page, wb_list);
366 nfs_list_remove_request(req);
367 if (__nfs_pageio_add_request(desc, req))
368 continue;
369 if (desc->pg_error < 0)
370 return 0;
371 break;
372 }
373 } while (desc->pg_recoalesce);
374 return 1;
375}
376
377int nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
378 struct nfs_page *req)
379{
380 int ret;
381
382 do {
383 ret = __nfs_pageio_add_request(desc, req);
384 if (ret)
385 break;
386 if (desc->pg_error < 0)
387 break;
388 ret = nfs_do_recoalesce(desc);
389 } while (ret);
390 return ret;
391}
392
bcb71bba
TM
393/**
394 * nfs_pageio_complete - Complete I/O on an nfs_pageio_descriptor
395 * @desc: pointer to io descriptor
396 */
397void nfs_pageio_complete(struct nfs_pageio_descriptor *desc)
398{
d9156f9f
TM
399 for (;;) {
400 nfs_pageio_doio(desc);
401 if (!desc->pg_recoalesce)
402 break;
403 if (!nfs_do_recoalesce(desc))
404 break;
405 }
bcb71bba
TM
406}
407
7fe7f848
TM
408/**
409 * nfs_pageio_cond_complete - Conditional I/O completion
410 * @desc: pointer to io descriptor
411 * @index: page index
412 *
413 * It is important to ensure that processes don't try to take locks
414 * on non-contiguous ranges of pages as that might deadlock. This
415 * function should be called before attempting to wait on a locked
416 * nfs_page. It will complete the I/O if the page index 'index'
417 * is not contiguous with the existing list of pages in 'desc'.
418 */
419void nfs_pageio_cond_complete(struct nfs_pageio_descriptor *desc, pgoff_t index)
420{
421 if (!list_empty(&desc->pg_list)) {
422 struct nfs_page *prev = nfs_list_entry(desc->pg_list.prev);
423 if (index != prev->wb_index + 1)
d9156f9f 424 nfs_pageio_complete(desc);
7fe7f848
TM
425 }
426}
427
3da28eb1 428#define NFS_SCAN_MAXENTRIES 16
1da177e4
LT
429/**
430 * nfs_scan_list - Scan a list for matching requests
d2ccddf0 431 * @nfsi: NFS inode
1da177e4
LT
432 * @dst: Destination list
433 * @idx_start: lower bound of page->index to scan
434 * @npages: idx_start + npages sets the upper bound to scan.
5c369683 435 * @tag: tag to scan for
1da177e4
LT
436 *
437 * Moves elements from one of the inode request lists.
438 * If the number of requests is set to 0, the entire address_space
439 * starting at index idx_start, is scanned.
440 * The requests are *not* checked to ensure that they form a contiguous set.
587142f8 441 * You must be holding the inode's i_lock when calling this function
1da177e4 442 */
5c369683 443int nfs_scan_list(struct nfs_inode *nfsi,
ca52fec1 444 struct list_head *dst, pgoff_t idx_start,
5c369683 445 unsigned int npages, int tag)
1da177e4 446{
d2ccddf0
TM
447 struct nfs_page *pgvec[NFS_SCAN_MAXENTRIES];
448 struct nfs_page *req;
ca52fec1 449 pgoff_t idx_end;
d2ccddf0
TM
450 int found, i;
451 int res;
a861a1e1 452 struct list_head *list;
1da177e4
LT
453
454 res = 0;
455 if (npages == 0)
456 idx_end = ~0;
457 else
458 idx_end = idx_start + npages - 1;
459
d2ccddf0 460 for (;;) {
5c369683 461 found = radix_tree_gang_lookup_tag(&nfsi->nfs_page_tree,
d2ccddf0 462 (void **)&pgvec[0], idx_start,
5c369683 463 NFS_SCAN_MAXENTRIES, tag);
d2ccddf0 464 if (found <= 0)
1da177e4 465 break;
d2ccddf0
TM
466 for (i = 0; i < found; i++) {
467 req = pgvec[i];
468 if (req->wb_index > idx_end)
469 goto out;
470 idx_start = req->wb_index + 1;
9fd367f0 471 if (nfs_set_page_tag_locked(req)) {
acee478a 472 kref_get(&req->wb_kref);
5c369683
TM
473 radix_tree_tag_clear(&nfsi->nfs_page_tree,
474 req->wb_index, tag);
a861a1e1
FI
475 list = pnfs_choose_commit_list(req, dst);
476 nfs_list_add_request(req, list);
d2ccddf0 477 res++;
dce34ce2
TM
478 if (res == INT_MAX)
479 goto out;
d2ccddf0
TM
480 }
481 }
edc05fc1 482 /* for latency reduction */
587142f8 483 cond_resched_lock(&nfsi->vfs_inode.i_lock);
1da177e4 484 }
d2ccddf0 485out:
1da177e4
LT
486 return res;
487}
488
f7b422b1 489int __init nfs_init_nfspagecache(void)
1da177e4
LT
490{
491 nfs_page_cachep = kmem_cache_create("nfs_page",
492 sizeof(struct nfs_page),
493 0, SLAB_HWCACHE_ALIGN,
20c2df83 494 NULL);
1da177e4
LT
495 if (nfs_page_cachep == NULL)
496 return -ENOMEM;
497
498 return 0;
499}
500
266bee88 501void nfs_destroy_nfspagecache(void)
1da177e4 502{
1a1d92c1 503 kmem_cache_destroy(nfs_page_cachep);
1da177e4
LT
504}
505