NFS: Clean up nfs_create_request comments
[deliverable/linux.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>
14#include <linux/sunrpc/clnt.h>
15#include <linux/nfs3.h>
16#include <linux/nfs4.h>
17#include <linux/nfs_page.h>
18#include <linux/nfs_fs.h>
19#include <linux/nfs_mount.h>
20
8d5658c9
TM
21#include "internal.h"
22
1da177e4
LT
23#define NFS_PARANOIA 1
24
e18b890b 25static struct kmem_cache *nfs_page_cachep;
1da177e4
LT
26
27static inline struct nfs_page *
28nfs_page_alloc(void)
29{
30 struct nfs_page *p;
e94b1766 31 p = kmem_cache_alloc(nfs_page_cachep, GFP_KERNEL);
1da177e4
LT
32 if (p) {
33 memset(p, 0, sizeof(*p));
34 INIT_LIST_HEAD(&p->wb_list);
35 }
36 return p;
37}
38
39static inline void
40nfs_page_free(struct nfs_page *p)
41{
42 kmem_cache_free(nfs_page_cachep, p);
43}
44
45/**
46 * nfs_create_request - Create an NFS read/write request.
47 * @file: file descriptor to use
48 * @inode: inode to which the request is attached
49 * @page: page to write
50 * @offset: starting offset within the page for the write
51 * @count: number of bytes to read/write
52 *
53 * The page must be locked by the caller. This makes sure we never
a19b89ca 54 * create two different requests for the same page.
1da177e4
LT
55 * User should ensure it is safe to sleep in this function.
56 */
57struct nfs_page *
58nfs_create_request(struct nfs_open_context *ctx, struct inode *inode,
59 struct page *page,
60 unsigned int offset, unsigned int count)
61{
62 struct nfs_server *server = NFS_SERVER(inode);
63 struct nfs_page *req;
64
1da177e4
LT
65 for (;;) {
66 /* try to allocate the request struct */
67 req = nfs_page_alloc();
68 if (req != NULL)
69 break;
70
1da177e4
LT
71 if (signalled() && (server->flags & NFS_MOUNT_INTR))
72 return ERR_PTR(-ERESTARTSYS);
73 yield();
74 }
75
76 /* Initialize the request struct. Initially, we assume a
77 * long write-back delay. This will be adjusted in
78 * update_nfs_request below if the region is not locked. */
79 req->wb_page = page;
80 atomic_set(&req->wb_complete, 0);
81 req->wb_index = page->index;
82 page_cache_get(page);
cd52ed35
TM
83 BUG_ON(PagePrivate(page));
84 BUG_ON(!PageLocked(page));
85 BUG_ON(page->mapping->host != inode);
1da177e4
LT
86 req->wb_offset = offset;
87 req->wb_pgbase = offset;
88 req->wb_bytes = count;
89 atomic_set(&req->wb_count, 1);
90 req->wb_context = get_nfs_open_context(ctx);
91
92 return req;
93}
94
95/**
96 * nfs_unlock_request - Unlock request and wake up sleepers.
97 * @req:
98 */
99void nfs_unlock_request(struct nfs_page *req)
100{
101 if (!NFS_WBACK_BUSY(req)) {
102 printk(KERN_ERR "NFS: Invalid unlock attempted\n");
103 BUG();
104 }
105 smp_mb__before_clear_bit();
106 clear_bit(PG_BUSY, &req->wb_flags);
107 smp_mb__after_clear_bit();
464a98bd 108 wake_up_bit(&req->wb_flags, PG_BUSY);
1da177e4
LT
109 nfs_release_request(req);
110}
111
c6a556b8
TM
112/**
113 * nfs_set_page_writeback_locked - Lock a request for writeback
114 * @req:
115 */
116int nfs_set_page_writeback_locked(struct nfs_page *req)
117{
118 struct nfs_inode *nfsi = NFS_I(req->wb_context->dentry->d_inode);
119
120 if (!nfs_lock_request(req))
121 return 0;
122 radix_tree_tag_set(&nfsi->nfs_page_tree, req->wb_index, NFS_PAGE_TAG_WRITEBACK);
123 return 1;
124}
125
126/**
127 * nfs_clear_page_writeback - Unlock request and wake up sleepers
128 */
129void nfs_clear_page_writeback(struct nfs_page *req)
130{
131 struct nfs_inode *nfsi = NFS_I(req->wb_context->dentry->d_inode);
132
deb7d638
TM
133 if (req->wb_page != NULL) {
134 spin_lock(&nfsi->req_lock);
135 radix_tree_tag_clear(&nfsi->nfs_page_tree, req->wb_index, NFS_PAGE_TAG_WRITEBACK);
136 spin_unlock(&nfsi->req_lock);
137 }
c6a556b8
TM
138 nfs_unlock_request(req);
139}
140
1da177e4
LT
141/**
142 * nfs_clear_request - Free up all resources allocated to the request
143 * @req:
144 *
145 * Release page resources associated with a write request after it
146 * has completed.
147 */
148void nfs_clear_request(struct nfs_page *req)
149{
cd52ed35
TM
150 struct page *page = req->wb_page;
151 if (page != NULL) {
cd52ed35 152 page_cache_release(page);
1da177e4
LT
153 req->wb_page = NULL;
154 }
155}
156
157
158/**
159 * nfs_release_request - Release the count on an NFS read/write request
160 * @req: request to release
161 *
162 * Note: Should never be called with the spinlock held!
163 */
164void
165nfs_release_request(struct nfs_page *req)
166{
167 if (!atomic_dec_and_test(&req->wb_count))
168 return;
169
170#ifdef NFS_PARANOIA
171 BUG_ON (!list_empty(&req->wb_list));
172 BUG_ON (NFS_WBACK_BUSY(req));
173#endif
174
175 /* Release struct file or cached credential */
176 nfs_clear_request(req);
177 put_nfs_open_context(req->wb_context);
178 nfs_page_free(req);
179}
180
464a98bd
TM
181static int nfs_wait_bit_interruptible(void *word)
182{
183 int ret = 0;
184
185 if (signal_pending(current))
186 ret = -ERESTARTSYS;
187 else
188 schedule();
189 return ret;
190}
191
1da177e4
LT
192/**
193 * nfs_wait_on_request - Wait for a request to complete.
194 * @req: request to wait upon.
195 *
196 * Interruptible by signals only if mounted with intr flag.
197 * The user is responsible for holding a count on the request.
198 */
199int
200nfs_wait_on_request(struct nfs_page *req)
201{
464a98bd
TM
202 struct rpc_clnt *clnt = NFS_CLIENT(req->wb_context->dentry->d_inode);
203 sigset_t oldmask;
204 int ret = 0;
205
206 if (!test_bit(PG_BUSY, &req->wb_flags))
207 goto out;
208 /*
209 * Note: the call to rpc_clnt_sigmask() suffices to ensure that we
210 * are not interrupted if intr flag is not set
211 */
212 rpc_clnt_sigmask(clnt, &oldmask);
213 ret = out_of_line_wait_on_bit(&req->wb_flags, PG_BUSY,
214 nfs_wait_bit_interruptible, TASK_INTERRUPTIBLE);
215 rpc_clnt_sigunmask(clnt, &oldmask);
216out:
217 return ret;
1da177e4
LT
218}
219
220/**
d8a5ad75
TM
221 * nfs_pageio_init - initialise a page io descriptor
222 * @desc: pointer to descriptor
bcb71bba
TM
223 * @inode: pointer to inode
224 * @doio: pointer to io function
225 * @bsize: io block size
226 * @io_flags: extra parameters for the io function
d8a5ad75 227 */
bcb71bba
TM
228void nfs_pageio_init(struct nfs_pageio_descriptor *desc,
229 struct inode *inode,
8d5658c9 230 int (*doio)(struct inode *, struct list_head *, unsigned int, size_t, int),
bcb71bba
TM
231 unsigned int bsize,
232 int io_flags)
d8a5ad75
TM
233{
234 INIT_LIST_HEAD(&desc->pg_list);
bcb71bba 235 desc->pg_bytes_written = 0;
d8a5ad75
TM
236 desc->pg_count = 0;
237 desc->pg_bsize = bsize;
238 desc->pg_base = 0;
bcb71bba
TM
239 desc->pg_inode = inode;
240 desc->pg_doio = doio;
241 desc->pg_ioflags = io_flags;
242 desc->pg_error = 0;
d8a5ad75
TM
243}
244
245/**
246 * nfs_can_coalesce_requests - test two requests for compatibility
247 * @prev: pointer to nfs_page
248 * @req: pointer to nfs_page
249 *
250 * The nfs_page structures 'prev' and 'req' are compared to ensure that the
251 * page data area they describe is contiguous, and that their RPC
252 * credentials, NFSv4 open state, and lockowners are the same.
253 *
254 * Return 'true' if this is the case, else return 'false'.
255 */
256static int nfs_can_coalesce_requests(struct nfs_page *prev,
257 struct nfs_page *req)
258{
259 if (req->wb_context->cred != prev->wb_context->cred)
260 return 0;
261 if (req->wb_context->lockowner != prev->wb_context->lockowner)
262 return 0;
263 if (req->wb_context->state != prev->wb_context->state)
264 return 0;
265 if (req->wb_index != (prev->wb_index + 1))
266 return 0;
267 if (req->wb_pgbase != 0)
268 return 0;
269 if (prev->wb_pgbase + prev->wb_bytes != PAGE_CACHE_SIZE)
270 return 0;
271 return 1;
272}
273
274/**
bcb71bba 275 * nfs_pageio_do_add_request - Attempt to coalesce a request into a page list.
d8a5ad75
TM
276 * @desc: destination io descriptor
277 * @req: request
278 *
279 * Returns true if the request 'req' was successfully coalesced into the
280 * existing list of pages 'desc'.
281 */
bcb71bba
TM
282static int nfs_pageio_do_add_request(struct nfs_pageio_descriptor *desc,
283 struct nfs_page *req)
d8a5ad75
TM
284{
285 size_t newlen = req->wb_bytes;
286
287 if (desc->pg_count != 0) {
288 struct nfs_page *prev;
289
290 /*
291 * FIXME: ideally we should be able to coalesce all requests
292 * that are not block boundary aligned, but currently this
293 * is problematic for the case of bsize < PAGE_CACHE_SIZE,
294 * since nfs_flush_multi and nfs_pagein_multi assume you
295 * can have only one struct nfs_page.
296 */
8d5658c9
TM
297 if (desc->pg_bsize < PAGE_SIZE)
298 return 0;
d8a5ad75 299 newlen += desc->pg_count;
8d5658c9 300 if (newlen > desc->pg_bsize)
d8a5ad75
TM
301 return 0;
302 prev = nfs_list_entry(desc->pg_list.prev);
303 if (!nfs_can_coalesce_requests(prev, req))
304 return 0;
305 } else
306 desc->pg_base = req->wb_pgbase;
307 nfs_list_remove_request(req);
308 nfs_list_add_request(req, &desc->pg_list);
309 desc->pg_count = newlen;
310 return 1;
311}
312
bcb71bba
TM
313/*
314 * Helper for nfs_pageio_add_request and nfs_pageio_complete
315 */
316static void nfs_pageio_doio(struct nfs_pageio_descriptor *desc)
317{
318 if (!list_empty(&desc->pg_list)) {
319 int error = desc->pg_doio(desc->pg_inode,
320 &desc->pg_list,
8d5658c9
TM
321 nfs_page_array_len(desc->pg_base,
322 desc->pg_count),
bcb71bba
TM
323 desc->pg_count,
324 desc->pg_ioflags);
325 if (error < 0)
326 desc->pg_error = error;
327 else
328 desc->pg_bytes_written += desc->pg_count;
329 }
330 if (list_empty(&desc->pg_list)) {
331 desc->pg_count = 0;
332 desc->pg_base = 0;
333 }
334}
335
336/**
337 * nfs_pageio_add_request - Attempt to coalesce a request into a page list.
338 * @desc: destination io descriptor
339 * @req: request
340 *
341 * Returns true if the request 'req' was successfully coalesced into the
342 * existing list of pages 'desc'.
343 */
8b09bee3
TM
344int nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
345 struct nfs_page *req)
bcb71bba
TM
346{
347 while (!nfs_pageio_do_add_request(desc, req)) {
348 nfs_pageio_doio(desc);
349 if (desc->pg_error < 0)
350 return 0;
351 }
352 return 1;
353}
354
bcb71bba
TM
355/**
356 * nfs_pageio_complete - Complete I/O on an nfs_pageio_descriptor
357 * @desc: pointer to io descriptor
358 */
359void nfs_pageio_complete(struct nfs_pageio_descriptor *desc)
360{
361 nfs_pageio_doio(desc);
362}
363
3da28eb1 364#define NFS_SCAN_MAXENTRIES 16
1da177e4
LT
365/**
366 * nfs_scan_list - Scan a list for matching requests
d2ccddf0 367 * @nfsi: NFS inode
1da177e4
LT
368 * @head: One of the NFS inode request lists
369 * @dst: Destination list
370 * @idx_start: lower bound of page->index to scan
371 * @npages: idx_start + npages sets the upper bound to scan.
372 *
373 * Moves elements from one of the inode request lists.
374 * If the number of requests is set to 0, the entire address_space
375 * starting at index idx_start, is scanned.
376 * The requests are *not* checked to ensure that they form a contiguous set.
377 * You must be holding the inode's req_lock when calling this function
378 */
d2ccddf0 379int nfs_scan_list(struct nfs_inode *nfsi, struct list_head *head,
ca52fec1 380 struct list_head *dst, pgoff_t idx_start,
d2ccddf0 381 unsigned int npages)
1da177e4 382{
d2ccddf0
TM
383 struct nfs_page *pgvec[NFS_SCAN_MAXENTRIES];
384 struct nfs_page *req;
ca52fec1 385 pgoff_t idx_end;
d2ccddf0
TM
386 int found, i;
387 int res;
1da177e4
LT
388
389 res = 0;
390 if (npages == 0)
391 idx_end = ~0;
392 else
393 idx_end = idx_start + npages - 1;
394
d2ccddf0
TM
395 for (;;) {
396 found = radix_tree_gang_lookup(&nfsi->nfs_page_tree,
397 (void **)&pgvec[0], idx_start,
398 NFS_SCAN_MAXENTRIES);
399 if (found <= 0)
1da177e4 400 break;
d2ccddf0
TM
401 for (i = 0; i < found; i++) {
402 req = pgvec[i];
403 if (req->wb_index > idx_end)
404 goto out;
405 idx_start = req->wb_index + 1;
406 if (req->wb_list_head != head)
407 continue;
408 if (nfs_set_page_writeback_locked(req)) {
409 nfs_list_remove_request(req);
410 nfs_list_add_request(req, dst);
411 res++;
412 }
413 }
1da177e4 414
1da177e4 415 }
d2ccddf0 416out:
1da177e4
LT
417 return res;
418}
419
f7b422b1 420int __init nfs_init_nfspagecache(void)
1da177e4
LT
421{
422 nfs_page_cachep = kmem_cache_create("nfs_page",
423 sizeof(struct nfs_page),
424 0, SLAB_HWCACHE_ALIGN,
425 NULL, NULL);
426 if (nfs_page_cachep == NULL)
427 return -ENOMEM;
428
429 return 0;
430}
431
266bee88 432void nfs_destroy_nfspagecache(void)
1da177e4 433{
1a1d92c1 434 kmem_cache_destroy(nfs_page_cachep);
1da177e4
LT
435}
436
This page took 0.242457 seconds and 5 git commands to generate.