[PATCH] NFS: large non-page-aligned direct I/O clobbers memory
[deliverable/linux.git] / fs / nfs / read.c
CommitLineData
1da177e4
LT
1/*
2 * linux/fs/nfs/read.c
3 *
4 * Block I/O for NFS
5 *
6 * Partial copy of Linus' read cache modifications to fs/nfs/file.c
7 * modified for async RPC by okir@monad.swb.de
8 *
9 * We do an ugly hack here in order to return proper error codes to the
10 * user program when a read request failed: since generic_file_read
11 * only checks the return value of inode->i_op->readpage() which is always 0
12 * for async RPC, we set the error bit of the page to 1 when an error occurs,
13 * and make nfs_readpage transmit requests synchronously when encountering this.
14 * This is only a small problem, though, since we now retry all operations
15 * within the RPC code when root squashing is suspected.
16 */
17
1da177e4
LT
18#include <linux/time.h>
19#include <linux/kernel.h>
20#include <linux/errno.h>
21#include <linux/fcntl.h>
22#include <linux/stat.h>
23#include <linux/mm.h>
24#include <linux/slab.h>
25#include <linux/pagemap.h>
26#include <linux/sunrpc/clnt.h>
27#include <linux/nfs_fs.h>
28#include <linux/nfs_page.h>
29#include <linux/smp_lock.h>
30
31#include <asm/system.h>
32
91d5b470
CL
33#include "iostat.h"
34
1da177e4
LT
35#define NFSDBG_FACILITY NFSDBG_PAGECACHE
36
37static int nfs_pagein_one(struct list_head *, struct inode *);
ec06c096
TM
38static const struct rpc_call_ops nfs_read_partial_ops;
39static const struct rpc_call_ops nfs_read_full_ops;
1da177e4
LT
40
41static kmem_cache_t *nfs_rdata_cachep;
3feb2d49 42static mempool_t *nfs_rdata_mempool;
1da177e4
LT
43
44#define MIN_POOL_READ (32)
45
e9f7bee1 46struct nfs_read_data *nfs_readdata_alloc(size_t len)
3feb2d49 47{
e9f7bee1 48 unsigned int pagecount = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
3feb2d49
TM
49 struct nfs_read_data *p = mempool_alloc(nfs_rdata_mempool, SLAB_NOFS);
50
51 if (p) {
52 memset(p, 0, sizeof(*p));
53 INIT_LIST_HEAD(&p->pages);
e9f7bee1 54 p->npages = pagecount;
0d0b5cb3
CL
55 if (pagecount <= ARRAY_SIZE(p->page_array))
56 p->pagevec = p->page_array;
3feb2d49 57 else {
0d0b5cb3
CL
58 p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
59 if (!p->pagevec) {
3feb2d49
TM
60 mempool_free(p, nfs_rdata_mempool);
61 p = NULL;
62 }
63 }
64 }
65 return p;
66}
67
e4e20512 68static void nfs_readdata_free(struct nfs_read_data *p)
3feb2d49
TM
69{
70 if (p && (p->pagevec != &p->page_array[0]))
71 kfree(p->pagevec);
72 mempool_free(p, nfs_rdata_mempool);
73}
74
963d8fe5 75void nfs_readdata_release(void *data)
1da177e4 76{
1da177e4
LT
77 nfs_readdata_free(data);
78}
79
80static
81unsigned int nfs_page_length(struct inode *inode, struct page *page)
82{
83 loff_t i_size = i_size_read(inode);
84 unsigned long idx;
85
86 if (i_size <= 0)
87 return 0;
88 idx = (i_size - 1) >> PAGE_CACHE_SHIFT;
89 if (page->index > idx)
90 return 0;
91 if (page->index != idx)
92 return PAGE_CACHE_SIZE;
93 return 1 + ((i_size - 1) & (PAGE_CACHE_SIZE - 1));
94}
95
96static
97int nfs_return_empty_page(struct page *page)
98{
99 memclear_highpage_flush(page, 0, PAGE_CACHE_SIZE);
100 SetPageUptodate(page);
101 unlock_page(page);
102 return 0;
103}
104
1de3fc12
TM
105static void nfs_readpage_truncate_uninitialised_page(struct nfs_read_data *data)
106{
107 unsigned int remainder = data->args.count - data->res.count;
108 unsigned int base = data->args.pgbase + data->res.count;
109 unsigned int pglen;
110 struct page **pages;
111
112 if (data->res.eof == 0 || remainder == 0)
113 return;
114 /*
115 * Note: "remainder" can never be negative, since we check for
116 * this in the XDR code.
117 */
118 pages = &data->args.pages[base >> PAGE_CACHE_SHIFT];
119 base &= ~PAGE_CACHE_MASK;
120 pglen = PAGE_CACHE_SIZE - base;
79558f36
TM
121 for (;;) {
122 if (remainder <= pglen) {
123 memclear_highpage_flush(*pages, base, remainder);
124 break;
125 }
1de3fc12 126 memclear_highpage_flush(*pages, base, pglen);
79558f36
TM
127 pages++;
128 remainder -= pglen;
129 pglen = PAGE_CACHE_SIZE;
130 base = 0;
131 }
1de3fc12
TM
132}
133
1da177e4
LT
134/*
135 * Read a page synchronously.
136 */
137static int nfs_readpage_sync(struct nfs_open_context *ctx, struct inode *inode,
138 struct page *page)
139{
140 unsigned int rsize = NFS_SERVER(inode)->rsize;
141 unsigned int count = PAGE_CACHE_SIZE;
142 int result;
143 struct nfs_read_data *rdata;
144
e9f7bee1 145 rdata = nfs_readdata_alloc(count);
1da177e4
LT
146 if (!rdata)
147 return -ENOMEM;
148
149 memset(rdata, 0, sizeof(*rdata));
150 rdata->flags = (IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0);
151 rdata->cred = ctx->cred;
152 rdata->inode = inode;
153 INIT_LIST_HEAD(&rdata->pages);
154 rdata->args.fh = NFS_FH(inode);
155 rdata->args.context = ctx;
156 rdata->args.pages = &page;
157 rdata->args.pgbase = 0UL;
158 rdata->args.count = rsize;
159 rdata->res.fattr = &rdata->fattr;
160
161 dprintk("NFS: nfs_readpage_sync(%p)\n", page);
162
163 /*
164 * This works now because the socket layer never tries to DMA
165 * into this buffer directly.
166 */
167 do {
168 if (count < rsize)
169 rdata->args.count = count;
170 rdata->res.count = rdata->args.count;
171 rdata->args.offset = page_offset(page) + rdata->args.pgbase;
172
173 dprintk("NFS: nfs_proc_read(%s, (%s/%Ld), %Lu, %u)\n",
174 NFS_SERVER(inode)->hostname,
175 inode->i_sb->s_id,
176 (long long)NFS_FILEID(inode),
177 (unsigned long long)rdata->args.pgbase,
178 rdata->args.count);
179
180 lock_kernel();
181 result = NFS_PROTO(inode)->read(rdata);
182 unlock_kernel();
183
184 /*
185 * Even if we had a partial success we can't mark the page
186 * cache valid.
187 */
188 if (result < 0) {
189 if (result == -EISDIR)
190 result = -EINVAL;
191 goto io_error;
192 }
193 count -= result;
194 rdata->args.pgbase += result;
91d5b470
CL
195 nfs_add_stats(inode, NFSIOS_SERVERREADBYTES, result);
196
1da177e4
LT
197 /* Note: result == 0 should only happen if we're caching
198 * a write that extends the file and punches a hole.
199 */
200 if (rdata->res.eof != 0 || result == 0)
201 break;
202 } while (count);
dc59250c 203 spin_lock(&inode->i_lock);
55296809 204 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATIME;
dc59250c 205 spin_unlock(&inode->i_lock);
1da177e4 206
1de3fc12
TM
207 nfs_readpage_truncate_uninitialised_page(rdata);
208 if (rdata->res.eof || rdata->res.count == rdata->args.count)
209 SetPageUptodate(page);
1da177e4
LT
210 result = 0;
211
212io_error:
213 unlock_page(page);
214 nfs_readdata_free(rdata);
215 return result;
216}
217
218static int nfs_readpage_async(struct nfs_open_context *ctx, struct inode *inode,
219 struct page *page)
220{
221 LIST_HEAD(one_request);
222 struct nfs_page *new;
223 unsigned int len;
224
225 len = nfs_page_length(inode, page);
226 if (len == 0)
227 return nfs_return_empty_page(page);
228 new = nfs_create_request(ctx, inode, page, 0, len);
229 if (IS_ERR(new)) {
230 unlock_page(page);
231 return PTR_ERR(new);
232 }
233 if (len < PAGE_CACHE_SIZE)
234 memclear_highpage_flush(page, len, PAGE_CACHE_SIZE - len);
235
1da177e4
LT
236 nfs_list_add_request(new, &one_request);
237 nfs_pagein_one(&one_request, inode);
238 return 0;
239}
240
241static void nfs_readpage_release(struct nfs_page *req)
242{
243 unlock_page(req->wb_page);
244
1da177e4
LT
245 dprintk("NFS: read done (%s/%Ld %d@%Ld)\n",
246 req->wb_context->dentry->d_inode->i_sb->s_id,
247 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
248 req->wb_bytes,
249 (long long)req_offset(req));
10d2c46f
NW
250 nfs_clear_request(req);
251 nfs_release_request(req);
1da177e4
LT
252}
253
254/*
255 * Set up the NFS read request struct
256 */
257static void nfs_read_rpcsetup(struct nfs_page *req, struct nfs_read_data *data,
ec06c096 258 const struct rpc_call_ops *call_ops,
1da177e4
LT
259 unsigned int count, unsigned int offset)
260{
261 struct inode *inode;
ec06c096 262 int flags;
1da177e4
LT
263
264 data->req = req;
265 data->inode = inode = req->wb_context->dentry->d_inode;
266 data->cred = req->wb_context->cred;
267
268 data->args.fh = NFS_FH(inode);
269 data->args.offset = req_offset(req) + offset;
270 data->args.pgbase = req->wb_pgbase + offset;
271 data->args.pages = data->pagevec;
272 data->args.count = count;
273 data->args.context = req->wb_context;
274
275 data->res.fattr = &data->fattr;
276 data->res.count = count;
277 data->res.eof = 0;
0e574af1 278 nfs_fattr_init(&data->fattr);
1da177e4 279
ec06c096
TM
280 /* Set up the initial task struct. */
281 flags = RPC_TASK_ASYNC | (IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0);
282 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, call_ops, data);
1da177e4
LT
283 NFS_PROTO(inode)->read_setup(data);
284
285 data->task.tk_cookie = (unsigned long)inode;
1da177e4
LT
286
287 dprintk("NFS: %4d initiated read call (req %s/%Ld, %u bytes @ offset %Lu)\n",
288 data->task.tk_pid,
289 inode->i_sb->s_id,
290 (long long)NFS_FILEID(inode),
291 count,
292 (unsigned long long)data->args.offset);
293}
294
295static void
296nfs_async_read_error(struct list_head *head)
297{
298 struct nfs_page *req;
299
300 while (!list_empty(head)) {
301 req = nfs_list_entry(head->next);
302 nfs_list_remove_request(req);
303 SetPageError(req->wb_page);
304 nfs_readpage_release(req);
305 }
306}
307
308/*
309 * Start an async read operation
310 */
311static void nfs_execute_read(struct nfs_read_data *data)
312{
313 struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
314 sigset_t oldset;
315
316 rpc_clnt_sigmask(clnt, &oldset);
317 lock_kernel();
318 rpc_execute(&data->task);
319 unlock_kernel();
320 rpc_clnt_sigunmask(clnt, &oldset);
321}
322
323/*
324 * Generate multiple requests to fill a single page.
325 *
326 * We optimize to reduce the number of read operations on the wire. If we
327 * detect that we're reading a page, or an area of a page, that is past the
328 * end of file, we do not generate NFS read operations but just clear the
329 * parts of the page that would have come back zero from the server anyway.
330 *
331 * We rely on the cached value of i_size to make this determination; another
332 * client can fill pages on the server past our cached end-of-file, but we
333 * won't see the new data until our attribute cache is updated. This is more
334 * or less conventional NFS client behavior.
335 */
336static int nfs_pagein_multi(struct list_head *head, struct inode *inode)
337{
338 struct nfs_page *req = nfs_list_entry(head->next);
339 struct page *page = req->wb_page;
340 struct nfs_read_data *data;
e9f7bee1
TM
341 size_t rsize = NFS_SERVER(inode)->rsize, nbytes;
342 unsigned int offset;
1da177e4
LT
343 int requests = 0;
344 LIST_HEAD(list);
345
346 nfs_list_remove_request(req);
347
348 nbytes = req->wb_bytes;
e9f7bee1
TM
349 do {
350 size_t len = min(nbytes,rsize);
351
352 data = nfs_readdata_alloc(len);
1da177e4
LT
353 if (!data)
354 goto out_bad;
355 INIT_LIST_HEAD(&data->pages);
356 list_add(&data->pages, &list);
357 requests++;
e9f7bee1
TM
358 nbytes -= len;
359 } while(nbytes != 0);
1da177e4
LT
360 atomic_set(&req->wb_complete, requests);
361
362 ClearPageError(page);
363 offset = 0;
364 nbytes = req->wb_bytes;
365 do {
366 data = list_entry(list.next, struct nfs_read_data, pages);
367 list_del_init(&data->pages);
368
369 data->pagevec[0] = page;
1da177e4
LT
370
371 if (nbytes > rsize) {
ec06c096
TM
372 nfs_read_rpcsetup(req, data, &nfs_read_partial_ops,
373 rsize, offset);
1da177e4
LT
374 offset += rsize;
375 nbytes -= rsize;
376 } else {
ec06c096
TM
377 nfs_read_rpcsetup(req, data, &nfs_read_partial_ops,
378 nbytes, offset);
1da177e4
LT
379 nbytes = 0;
380 }
381 nfs_execute_read(data);
382 } while (nbytes != 0);
383
384 return 0;
385
386out_bad:
387 while (!list_empty(&list)) {
388 data = list_entry(list.next, struct nfs_read_data, pages);
389 list_del(&data->pages);
390 nfs_readdata_free(data);
391 }
392 SetPageError(page);
393 nfs_readpage_release(req);
394 return -ENOMEM;
395}
396
397static int nfs_pagein_one(struct list_head *head, struct inode *inode)
398{
399 struct nfs_page *req;
400 struct page **pages;
401 struct nfs_read_data *data;
402 unsigned int count;
403
404 if (NFS_SERVER(inode)->rsize < PAGE_CACHE_SIZE)
405 return nfs_pagein_multi(head, inode);
406
e9f7bee1 407 data = nfs_readdata_alloc(NFS_SERVER(inode)->rsize);
1da177e4
LT
408 if (!data)
409 goto out_bad;
410
411 INIT_LIST_HEAD(&data->pages);
412 pages = data->pagevec;
413 count = 0;
414 while (!list_empty(head)) {
415 req = nfs_list_entry(head->next);
416 nfs_list_remove_request(req);
417 nfs_list_add_request(req, &data->pages);
418 ClearPageError(req->wb_page);
419 *pages++ = req->wb_page;
420 count += req->wb_bytes;
421 }
422 req = nfs_list_entry(data->pages.next);
423
ec06c096 424 nfs_read_rpcsetup(req, data, &nfs_read_full_ops, count, 0);
1da177e4
LT
425
426 nfs_execute_read(data);
427 return 0;
428out_bad:
429 nfs_async_read_error(head);
430 return -ENOMEM;
431}
432
433static int
434nfs_pagein_list(struct list_head *head, int rpages)
435{
436 LIST_HEAD(one_request);
437 struct nfs_page *req;
438 int error = 0;
439 unsigned int pages = 0;
440
441 while (!list_empty(head)) {
442 pages += nfs_coalesce_requests(head, &one_request, rpages);
443 req = nfs_list_entry(one_request.next);
444 error = nfs_pagein_one(&one_request, req->wb_context->dentry->d_inode);
445 if (error < 0)
446 break;
447 }
448 if (error >= 0)
449 return pages;
450
451 nfs_async_read_error(head);
452 return error;
453}
454
455/*
456 * Handle a read reply that fills part of a page.
457 */
ec06c096 458static void nfs_readpage_result_partial(struct rpc_task *task, void *calldata)
1da177e4 459{
ec06c096 460 struct nfs_read_data *data = calldata;
1da177e4
LT
461 struct nfs_page *req = data->req;
462 struct page *page = req->wb_page;
463
1de3fc12
TM
464 if (likely(task->tk_status >= 0))
465 nfs_readpage_truncate_uninitialised_page(data);
466 else
467 SetPageError(page);
ec06c096
TM
468 if (nfs_readpage_result(task, data) != 0)
469 return;
1da177e4
LT
470 if (atomic_dec_and_test(&req->wb_complete)) {
471 if (!PageError(page))
472 SetPageUptodate(page);
473 nfs_readpage_release(req);
474 }
475}
476
ec06c096
TM
477static const struct rpc_call_ops nfs_read_partial_ops = {
478 .rpc_call_done = nfs_readpage_result_partial,
479 .rpc_release = nfs_readdata_release,
480};
481
1de3fc12
TM
482static void nfs_readpage_set_pages_uptodate(struct nfs_read_data *data)
483{
484 unsigned int count = data->res.count;
485 unsigned int base = data->args.pgbase;
486 struct page **pages;
487
79558f36
TM
488 if (data->res.eof)
489 count = data->args.count;
1de3fc12
TM
490 if (unlikely(count == 0))
491 return;
492 pages = &data->args.pages[base >> PAGE_CACHE_SHIFT];
493 base &= ~PAGE_CACHE_MASK;
494 count += base;
495 for (;count >= PAGE_CACHE_SIZE; count -= PAGE_CACHE_SIZE, pages++)
496 SetPageUptodate(*pages);
79558f36 497 if (count != 0)
1de3fc12
TM
498 SetPageUptodate(*pages);
499}
500
501static void nfs_readpage_set_pages_error(struct nfs_read_data *data)
502{
503 unsigned int count = data->args.count;
504 unsigned int base = data->args.pgbase;
505 struct page **pages;
506
507 pages = &data->args.pages[base >> PAGE_CACHE_SHIFT];
508 base &= ~PAGE_CACHE_MASK;
509 count += base;
510 for (;count >= PAGE_CACHE_SIZE; count -= PAGE_CACHE_SIZE, pages++)
511 SetPageError(*pages);
79558f36
TM
512 if (count != 0)
513 SetPageError(*pages);
1de3fc12
TM
514}
515
1da177e4
LT
516/*
517 * This is the callback from RPC telling us whether a reply was
518 * received or some error occurred (timeout or socket shutdown).
519 */
ec06c096 520static void nfs_readpage_result_full(struct rpc_task *task, void *calldata)
1da177e4 521{
ec06c096 522 struct nfs_read_data *data = calldata;
1da177e4 523
1de3fc12
TM
524 /*
525 * Note: nfs_readpage_result may change the values of
526 * data->args. In the multi-page case, we therefore need
527 * to ensure that we call the next nfs_readpage_set_page_uptodate()
528 * first in the multi-page case.
529 */
530 if (likely(task->tk_status >= 0)) {
531 nfs_readpage_truncate_uninitialised_page(data);
532 nfs_readpage_set_pages_uptodate(data);
533 } else
534 nfs_readpage_set_pages_error(data);
ec06c096
TM
535 if (nfs_readpage_result(task, data) != 0)
536 return;
1da177e4
LT
537 while (!list_empty(&data->pages)) {
538 struct nfs_page *req = nfs_list_entry(data->pages.next);
1da177e4 539
1de3fc12 540 nfs_list_remove_request(req);
1da177e4
LT
541 nfs_readpage_release(req);
542 }
543}
544
ec06c096
TM
545static const struct rpc_call_ops nfs_read_full_ops = {
546 .rpc_call_done = nfs_readpage_result_full,
547 .rpc_release = nfs_readdata_release,
548};
549
1da177e4
LT
550/*
551 * This is the callback from RPC telling us whether a reply was
552 * received or some error occurred (timeout or socket shutdown).
553 */
ec06c096 554int nfs_readpage_result(struct rpc_task *task, struct nfs_read_data *data)
1da177e4 555{
1da177e4
LT
556 struct nfs_readargs *argp = &data->args;
557 struct nfs_readres *resp = &data->res;
ec06c096 558 int status;
1da177e4
LT
559
560 dprintk("NFS: %4d nfs_readpage_result, (status %d)\n",
ec06c096
TM
561 task->tk_pid, task->tk_status);
562
563 status = NFS_PROTO(data->inode)->read_done(task, data);
564 if (status != 0)
565 return status;
1da177e4 566
91d5b470
CL
567 nfs_add_stats(data->inode, NFSIOS_SERVERREADBYTES, resp->count);
568
1da177e4
LT
569 /* Is this a short read? */
570 if (task->tk_status >= 0 && resp->count < argp->count && !resp->eof) {
91d5b470 571 nfs_inc_stats(data->inode, NFSIOS_SHORTREAD);
1da177e4
LT
572 /* Has the server at least made some progress? */
573 if (resp->count != 0) {
574 /* Yes, so retry the read at the end of the data */
575 argp->offset += resp->count;
576 argp->pgbase += resp->count;
577 argp->count -= resp->count;
578 rpc_restart_call(task);
ec06c096 579 return -EAGAIN;
1da177e4
LT
580 }
581 task->tk_status = -EIO;
582 }
dc59250c 583 spin_lock(&data->inode->i_lock);
55296809 584 NFS_I(data->inode)->cache_validity |= NFS_INO_INVALID_ATIME;
dc59250c 585 spin_unlock(&data->inode->i_lock);
ec06c096 586 return 0;
1da177e4
LT
587}
588
589/*
590 * Read a page over NFS.
591 * We read the page synchronously in the following case:
592 * - The error flag is set for this page. This happens only when a
593 * previous async read operation failed.
594 */
595int nfs_readpage(struct file *file, struct page *page)
596{
597 struct nfs_open_context *ctx;
598 struct inode *inode = page->mapping->host;
599 int error;
600
601 dprintk("NFS: nfs_readpage (%p %ld@%lu)\n",
602 page, PAGE_CACHE_SIZE, page->index);
91d5b470
CL
603 nfs_inc_stats(inode, NFSIOS_VFSREADPAGE);
604 nfs_add_stats(inode, NFSIOS_READPAGES, 1);
605
1da177e4
LT
606 /*
607 * Try to flush any pending writes to the file..
608 *
609 * NOTE! Because we own the page lock, there cannot
610 * be any new pending writes generated at this point
611 * for this page (other pages can be written to).
612 */
613 error = nfs_wb_page(inode, page);
614 if (error)
615 goto out_error;
616
617 if (file == NULL) {
d530838b 618 ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
1da177e4
LT
619 if (ctx == NULL)
620 return -EBADF;
621 } else
622 ctx = get_nfs_open_context((struct nfs_open_context *)
623 file->private_data);
624 if (!IS_SYNC(inode)) {
625 error = nfs_readpage_async(ctx, inode, page);
626 goto out;
627 }
628
629 error = nfs_readpage_sync(ctx, inode, page);
630 if (error < 0 && IS_SWAPFILE(inode))
631 printk("Aiee.. nfs swap-in of page failed!\n");
632out:
633 put_nfs_open_context(ctx);
634 return error;
635
636out_error:
637 unlock_page(page);
638 return error;
639}
640
641struct nfs_readdesc {
642 struct list_head *head;
643 struct nfs_open_context *ctx;
644};
645
646static int
647readpage_async_filler(void *data, struct page *page)
648{
649 struct nfs_readdesc *desc = (struct nfs_readdesc *)data;
650 struct inode *inode = page->mapping->host;
651 struct nfs_page *new;
652 unsigned int len;
653
654 nfs_wb_page(inode, page);
655 len = nfs_page_length(inode, page);
656 if (len == 0)
657 return nfs_return_empty_page(page);
658 new = nfs_create_request(desc->ctx, inode, page, 0, len);
659 if (IS_ERR(new)) {
660 SetPageError(page);
661 unlock_page(page);
662 return PTR_ERR(new);
663 }
664 if (len < PAGE_CACHE_SIZE)
665 memclear_highpage_flush(page, len, PAGE_CACHE_SIZE - len);
1da177e4
LT
666 nfs_list_add_request(new, desc->head);
667 return 0;
668}
669
670int nfs_readpages(struct file *filp, struct address_space *mapping,
671 struct list_head *pages, unsigned nr_pages)
672{
673 LIST_HEAD(head);
674 struct nfs_readdesc desc = {
675 .head = &head,
676 };
677 struct inode *inode = mapping->host;
678 struct nfs_server *server = NFS_SERVER(inode);
679 int ret;
680
681 dprintk("NFS: nfs_readpages (%s/%Ld %d)\n",
682 inode->i_sb->s_id,
683 (long long)NFS_FILEID(inode),
684 nr_pages);
91d5b470 685 nfs_inc_stats(inode, NFSIOS_VFSREADPAGES);
1da177e4
LT
686
687 if (filp == NULL) {
d530838b 688 desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
1da177e4
LT
689 if (desc.ctx == NULL)
690 return -EBADF;
691 } else
692 desc.ctx = get_nfs_open_context((struct nfs_open_context *)
693 filp->private_data);
694 ret = read_cache_pages(mapping, pages, readpage_async_filler, &desc);
695 if (!list_empty(&head)) {
696 int err = nfs_pagein_list(&head, server->rpages);
697 if (!ret)
91d5b470 698 nfs_add_stats(inode, NFSIOS_READPAGES, err);
1da177e4
LT
699 ret = err;
700 }
701 put_nfs_open_context(desc.ctx);
702 return ret;
703}
704
f7b422b1 705int __init nfs_init_readpagecache(void)
1da177e4
LT
706{
707 nfs_rdata_cachep = kmem_cache_create("nfs_read_data",
708 sizeof(struct nfs_read_data),
709 0, SLAB_HWCACHE_ALIGN,
710 NULL, NULL);
711 if (nfs_rdata_cachep == NULL)
712 return -ENOMEM;
713
93d2341c
MD
714 nfs_rdata_mempool = mempool_create_slab_pool(MIN_POOL_READ,
715 nfs_rdata_cachep);
1da177e4
LT
716 if (nfs_rdata_mempool == NULL)
717 return -ENOMEM;
718
719 return 0;
720}
721
266bee88 722void nfs_destroy_readpagecache(void)
1da177e4
LT
723{
724 mempool_destroy(nfs_rdata_mempool);
725 if (kmem_cache_destroy(nfs_rdata_cachep))
726 printk(KERN_INFO "nfs_read_data: not all structures were freed\n");
727}
This page took 0.187216 seconds and 5 git commands to generate.