procfs: use simple_read_from_buffer()
[deliverable/linux.git] / fs / nfs / pagelist.c
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
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
21 #include "internal.h"
22
23 #define NFS_PARANOIA 1
24
25 static struct kmem_cache *nfs_page_cachep;
26
27 static inline struct nfs_page *
28 nfs_page_alloc(void)
29 {
30 struct nfs_page *p;
31 p = kmem_cache_alloc(nfs_page_cachep, GFP_KERNEL);
32 if (p) {
33 memset(p, 0, sizeof(*p));
34 INIT_LIST_HEAD(&p->wb_list);
35 }
36 return p;
37 }
38
39 static inline void
40 nfs_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
54 * create two different requests for the same page.
55 * User should ensure it is safe to sleep in this function.
56 */
57 struct nfs_page *
58 nfs_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
65 for (;;) {
66 /* try to allocate the request struct */
67 req = nfs_page_alloc();
68 if (req != NULL)
69 break;
70
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);
83 BUG_ON(PagePrivate(page));
84 BUG_ON(!PageLocked(page));
85 BUG_ON(page->mapping->host != inode);
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 */
99 void 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();
108 wake_up_bit(&req->wb_flags, PG_BUSY);
109 nfs_release_request(req);
110 }
111
112 /**
113 * nfs_set_page_writeback_locked - Lock a request for writeback
114 * @req:
115 */
116 int 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 */
129 void nfs_clear_page_writeback(struct nfs_page *req)
130 {
131 struct nfs_inode *nfsi = NFS_I(req->wb_context->dentry->d_inode);
132
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 }
138 nfs_unlock_request(req);
139 }
140
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 */
148 void nfs_clear_request(struct nfs_page *req)
149 {
150 struct page *page = req->wb_page;
151 if (page != NULL) {
152 page_cache_release(page);
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 */
164 void
165 nfs_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
181 static 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
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 */
199 int
200 nfs_wait_on_request(struct nfs_page *req)
201 {
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);
216 out:
217 return ret;
218 }
219
220 /**
221 * nfs_pageio_init - initialise a page io descriptor
222 * @desc: pointer to descriptor
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
227 */
228 void nfs_pageio_init(struct nfs_pageio_descriptor *desc,
229 struct inode *inode,
230 int (*doio)(struct inode *, struct list_head *, unsigned int, size_t, int),
231 size_t bsize,
232 int io_flags)
233 {
234 INIT_LIST_HEAD(&desc->pg_list);
235 desc->pg_bytes_written = 0;
236 desc->pg_count = 0;
237 desc->pg_bsize = bsize;
238 desc->pg_base = 0;
239 desc->pg_inode = inode;
240 desc->pg_doio = doio;
241 desc->pg_ioflags = io_flags;
242 desc->pg_error = 0;
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 */
256 static 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 /**
275 * nfs_pageio_do_add_request - Attempt to coalesce a request into a page list.
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 */
282 static int nfs_pageio_do_add_request(struct nfs_pageio_descriptor *desc,
283 struct nfs_page *req)
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 */
297 if (desc->pg_bsize < PAGE_SIZE)
298 return 0;
299 newlen += desc->pg_count;
300 if (newlen > desc->pg_bsize)
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
313 /*
314 * Helper for nfs_pageio_add_request and nfs_pageio_complete
315 */
316 static 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,
321 nfs_page_array_len(desc->pg_base,
322 desc->pg_count),
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 */
344 int nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
345 struct nfs_page *req)
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
355 /**
356 * nfs_pageio_complete - Complete I/O on an nfs_pageio_descriptor
357 * @desc: pointer to io descriptor
358 */
359 void nfs_pageio_complete(struct nfs_pageio_descriptor *desc)
360 {
361 nfs_pageio_doio(desc);
362 }
363
364 #define NFS_SCAN_MAXENTRIES 16
365 /**
366 * nfs_scan_list - Scan a list for matching requests
367 * @nfsi: NFS inode
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 */
379 int nfs_scan_list(struct nfs_inode *nfsi, struct list_head *head,
380 struct list_head *dst, pgoff_t idx_start,
381 unsigned int npages)
382 {
383 struct nfs_page *pgvec[NFS_SCAN_MAXENTRIES];
384 struct nfs_page *req;
385 pgoff_t idx_end;
386 int found, i;
387 int res;
388
389 res = 0;
390 if (npages == 0)
391 idx_end = ~0;
392 else
393 idx_end = idx_start + npages - 1;
394
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)
400 break;
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 }
414
415 }
416 out:
417 return res;
418 }
419
420 int __init nfs_init_nfspagecache(void)
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
432 void nfs_destroy_nfspagecache(void)
433 {
434 kmem_cache_destroy(nfs_page_cachep);
435 }
436
This page took 0.038795 seconds and 5 git commands to generate.