]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/nfs/read.c
NFS: Don't use DATA_SYNC writes
[mirror_ubuntu-artful-kernel.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
1da177e4
LT
8 */
9
1da177e4
LT
10#include <linux/time.h>
11#include <linux/kernel.h>
12#include <linux/errno.h>
13#include <linux/fcntl.h>
14#include <linux/stat.h>
15#include <linux/mm.h>
16#include <linux/slab.h>
17#include <linux/pagemap.h>
18#include <linux/sunrpc/clnt.h>
19#include <linux/nfs_fs.h>
20#include <linux/nfs_page.h>
64419a9b 21#include <linux/module.h>
1da177e4
LT
22
23#include <asm/system.h>
bae724ef 24#include "pnfs.h"
1da177e4 25
f11c88af 26#include "nfs4_fs.h"
49a70f27 27#include "internal.h"
91d5b470 28#include "iostat.h"
9a9fc1c0 29#include "fscache.h"
91d5b470 30
1da177e4
LT
31#define NFSDBG_FACILITY NFSDBG_PAGECACHE
32
c76069bd
FI
33static int nfs_pagein_multi(struct nfs_pageio_descriptor *desc);
34static int nfs_pagein_one(struct nfs_pageio_descriptor *desc);
1751c363 35static const struct nfs_pageio_ops nfs_pageio_read_ops;
ec06c096
TM
36static const struct rpc_call_ops nfs_read_partial_ops;
37static const struct rpc_call_ops nfs_read_full_ops;
1da177e4 38
e18b890b 39static struct kmem_cache *nfs_rdata_cachep;
3feb2d49 40static mempool_t *nfs_rdata_mempool;
1da177e4
LT
41
42#define MIN_POOL_READ (32)
43
8d5658c9 44struct nfs_read_data *nfs_readdata_alloc(unsigned int pagecount)
3feb2d49 45{
93870d76 46 struct nfs_read_data *p = mempool_alloc(nfs_rdata_mempool, GFP_KERNEL);
3feb2d49
TM
47
48 if (p) {
49 memset(p, 0, sizeof(*p));
50 INIT_LIST_HEAD(&p->pages);
e9f7bee1 51 p->npages = pagecount;
0d0b5cb3
CL
52 if (pagecount <= ARRAY_SIZE(p->page_array))
53 p->pagevec = p->page_array;
3feb2d49 54 else {
93870d76 55 p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_KERNEL);
0d0b5cb3 56 if (!p->pagevec) {
3feb2d49
TM
57 mempool_free(p, nfs_rdata_mempool);
58 p = NULL;
59 }
60 }
61 }
62 return p;
63}
64
1ae88b2e 65void nfs_readdata_free(struct nfs_read_data *p)
3feb2d49
TM
66{
67 if (p && (p->pagevec != &p->page_array[0]))
68 kfree(p->pagevec);
69 mempool_free(p, nfs_rdata_mempool);
70}
71
1ae88b2e 72static void nfs_readdata_release(struct nfs_read_data *rdata)
1da177e4 73{
bae724ef 74 put_lseg(rdata->lseg);
383ba719
TM
75 put_nfs_open_context(rdata->args.context);
76 nfs_readdata_free(rdata);
1da177e4
LT
77}
78
1da177e4
LT
79static
80int nfs_return_empty_page(struct page *page)
81{
eebd2aa3 82 zero_user(page, 0, PAGE_CACHE_SIZE);
1da177e4
LT
83 SetPageUptodate(page);
84 unlock_page(page);
85 return 0;
86}
87
1de3fc12
TM
88static void nfs_readpage_truncate_uninitialised_page(struct nfs_read_data *data)
89{
90 unsigned int remainder = data->args.count - data->res.count;
91 unsigned int base = data->args.pgbase + data->res.count;
92 unsigned int pglen;
93 struct page **pages;
94
95 if (data->res.eof == 0 || remainder == 0)
96 return;
97 /*
98 * Note: "remainder" can never be negative, since we check for
99 * this in the XDR code.
100 */
101 pages = &data->args.pages[base >> PAGE_CACHE_SHIFT];
102 base &= ~PAGE_CACHE_MASK;
103 pglen = PAGE_CACHE_SIZE - base;
79558f36
TM
104 for (;;) {
105 if (remainder <= pglen) {
eebd2aa3 106 zero_user(*pages, base, remainder);
79558f36
TM
107 break;
108 }
eebd2aa3 109 zero_user(*pages, base, pglen);
79558f36
TM
110 pages++;
111 remainder -= pglen;
112 pglen = PAGE_CACHE_SIZE;
113 base = 0;
114 }
1de3fc12
TM
115}
116
e885de1a 117void nfs_pageio_init_read_mds(struct nfs_pageio_descriptor *pgio,
1751c363
TM
118 struct inode *inode)
119{
120 nfs_pageio_init(pgio, inode, &nfs_pageio_read_ops,
121 NFS_SERVER(inode)->rsize, 0);
122}
7c24d948 123EXPORT_SYMBOL_GPL(nfs_pageio_init_read_mds);
1751c363
TM
124
125static void nfs_pageio_init_read(struct nfs_pageio_descriptor *pgio,
126 struct inode *inode)
127{
128 if (!pnfs_pageio_init_read(pgio, inode))
129 nfs_pageio_init_read_mds(pgio, inode);
130}
131
f42b293d
DH
132int nfs_readpage_async(struct nfs_open_context *ctx, struct inode *inode,
133 struct page *page)
1da177e4 134{
1da177e4
LT
135 struct nfs_page *new;
136 unsigned int len;
c76069bd 137 struct nfs_pageio_descriptor pgio;
1da177e4 138
49a70f27 139 len = nfs_page_length(page);
1da177e4
LT
140 if (len == 0)
141 return nfs_return_empty_page(page);
142 new = nfs_create_request(ctx, inode, page, 0, len);
143 if (IS_ERR(new)) {
144 unlock_page(page);
145 return PTR_ERR(new);
146 }
147 if (len < PAGE_CACHE_SIZE)
eebd2aa3 148 zero_user_segment(page, len, PAGE_CACHE_SIZE);
1da177e4 149
1751c363 150 nfs_pageio_init_read(&pgio, inode);
d8007d4d 151 nfs_pageio_add_request(&pgio, new);
1751c363 152 nfs_pageio_complete(&pgio);
1da177e4
LT
153 return 0;
154}
155
156static void nfs_readpage_release(struct nfs_page *req)
157{
7f8e05f6
DH
158 struct inode *d_inode = req->wb_context->path.dentry->d_inode;
159
160 if (PageUptodate(req->wb_page))
161 nfs_readpage_to_fscache(d_inode, req->wb_page, 0);
162
1da177e4
LT
163 unlock_page(req->wb_page);
164
1da177e4 165 dprintk("NFS: read done (%s/%Ld %d@%Ld)\n",
88be9f99
TM
166 req->wb_context->path.dentry->d_inode->i_sb->s_id,
167 (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
1da177e4
LT
168 req->wb_bytes,
169 (long long)req_offset(req));
10d2c46f 170 nfs_release_request(req);
1da177e4
LT
171}
172
dc70d7b3 173int nfs_initiate_read(struct nfs_read_data *data, struct rpc_clnt *clnt,
64419a9b 174 const struct rpc_call_ops *call_ops)
1da177e4 175{
64419a9b 176 struct inode *inode = data->inode;
84115e1c 177 int swap_flags = IS_SWAPFILE(inode) ? NFS_RPC_SWAPFLAGS : 0;
07737691 178 struct rpc_task *task;
bdc7f021
TM
179 struct rpc_message msg = {
180 .rpc_argp = &data->args,
181 .rpc_resp = &data->res,
64419a9b 182 .rpc_cred = data->cred,
bdc7f021 183 };
84115e1c 184 struct rpc_task_setup task_setup_data = {
07737691 185 .task = &data->task,
64419a9b 186 .rpc_client = clnt,
bdc7f021 187 .rpc_message = &msg,
84115e1c
TM
188 .callback_ops = call_ops,
189 .callback_data = data,
101070ca 190 .workqueue = nfsiod_workqueue,
84115e1c
TM
191 .flags = RPC_TASK_ASYNC | swap_flags,
192 };
1da177e4 193
64419a9b
AA
194 /* Set up the initial task struct. */
195 NFS_PROTO(inode)->read_setup(data, &msg);
196
197 dprintk("NFS: %5u initiated read call (req %s/%lld, %u bytes @ "
198 "offset %llu)\n",
199 data->task.tk_pid,
200 inode->i_sb->s_id,
201 (long long)NFS_FILEID(inode),
202 data->args.count,
203 (unsigned long long)data->args.offset);
204
205 task = rpc_run_task(&task_setup_data);
206 if (IS_ERR(task))
207 return PTR_ERR(task);
208 rpc_put_task(task);
209 return 0;
210}
dc70d7b3 211EXPORT_SYMBOL_GPL(nfs_initiate_read);
64419a9b
AA
212
213/*
214 * Set up the NFS read request struct
215 */
216static int nfs_read_rpcsetup(struct nfs_page *req, struct nfs_read_data *data,
217 const struct rpc_call_ops *call_ops,
218 unsigned int count, unsigned int offset,
219 struct pnfs_layout_segment *lseg)
220{
221 struct inode *inode = req->wb_context->path.dentry->d_inode;
222
1da177e4 223 data->req = req;
84115e1c 224 data->inode = inode;
64419a9b 225 data->cred = req->wb_context->cred;
bae724ef 226 data->lseg = get_lseg(lseg);
1da177e4
LT
227
228 data->args.fh = NFS_FH(inode);
229 data->args.offset = req_offset(req) + offset;
230 data->args.pgbase = req->wb_pgbase + offset;
231 data->args.pages = data->pagevec;
232 data->args.count = count;
383ba719 233 data->args.context = get_nfs_open_context(req->wb_context);
f11ac8db 234 data->args.lock_context = req->wb_lock_context;
1da177e4
LT
235
236 data->res.fattr = &data->fattr;
237 data->res.count = count;
238 data->res.eof = 0;
0e574af1 239 nfs_fattr_init(&data->fattr);
1da177e4 240
64419a9b
AA
241 if (data->lseg &&
242 (pnfs_try_to_read_data(data, call_ops) == PNFS_ATTEMPTED))
243 return 0;
bdc7f021 244
64419a9b 245 return nfs_initiate_read(data, NFS_CLIENT(inode), call_ops);
1da177e4
LT
246}
247
248static void
249nfs_async_read_error(struct list_head *head)
250{
251 struct nfs_page *req;
252
253 while (!list_empty(head)) {
254 req = nfs_list_entry(head->next);
255 nfs_list_remove_request(req);
256 SetPageError(req->wb_page);
257 nfs_readpage_release(req);
258 }
259}
260
1da177e4
LT
261/*
262 * Generate multiple requests to fill a single page.
263 *
264 * We optimize to reduce the number of read operations on the wire. If we
265 * detect that we're reading a page, or an area of a page, that is past the
266 * end of file, we do not generate NFS read operations but just clear the
267 * parts of the page that would have come back zero from the server anyway.
268 *
269 * We rely on the cached value of i_size to make this determination; another
270 * client can fill pages on the server past our cached end-of-file, but we
271 * won't see the new data until our attribute cache is updated. This is more
272 * or less conventional NFS client behavior.
273 */
c76069bd 274static int nfs_pagein_multi(struct nfs_pageio_descriptor *desc)
1da177e4 275{
c76069bd 276 struct nfs_page *req = nfs_list_entry(desc->pg_list.next);
1da177e4
LT
277 struct page *page = req->wb_page;
278 struct nfs_read_data *data;
c76069bd 279 size_t rsize = NFS_SERVER(desc->pg_inode)->rsize, nbytes;
e9f7bee1 280 unsigned int offset;
1da177e4 281 int requests = 0;
dbae4c73 282 int ret = 0;
d8007d4d 283 struct pnfs_layout_segment *lseg = desc->pg_lseg;
1da177e4
LT
284 LIST_HEAD(list);
285
286 nfs_list_remove_request(req);
287
c76069bd 288 nbytes = desc->pg_count;
e9f7bee1
TM
289 do {
290 size_t len = min(nbytes,rsize);
291
8d5658c9 292 data = nfs_readdata_alloc(1);
1da177e4
LT
293 if (!data)
294 goto out_bad;
1da177e4
LT
295 list_add(&data->pages, &list);
296 requests++;
e9f7bee1
TM
297 nbytes -= len;
298 } while(nbytes != 0);
1da177e4
LT
299 atomic_set(&req->wb_complete, requests);
300
301 ClearPageError(page);
302 offset = 0;
c76069bd 303 nbytes = desc->pg_count;
1da177e4 304 do {
dbae4c73
TM
305 int ret2;
306
1da177e4
LT
307 data = list_entry(list.next, struct nfs_read_data, pages);
308 list_del_init(&data->pages);
309
310 data->pagevec[0] = page;
1da177e4 311
bcb71bba
TM
312 if (nbytes < rsize)
313 rsize = nbytes;
dbae4c73 314 ret2 = nfs_read_rpcsetup(req, data, &nfs_read_partial_ops,
bae724ef 315 rsize, offset, lseg);
dbae4c73
TM
316 if (ret == 0)
317 ret = ret2;
bcb71bba
TM
318 offset += rsize;
319 nbytes -= rsize;
1da177e4 320 } while (nbytes != 0);
bae724ef 321 put_lseg(lseg);
36fe432d 322 desc->pg_lseg = NULL;
1da177e4 323
dbae4c73 324 return ret;
1da177e4
LT
325
326out_bad:
327 while (!list_empty(&list)) {
328 data = list_entry(list.next, struct nfs_read_data, pages);
329 list_del(&data->pages);
330 nfs_readdata_free(data);
331 }
332 SetPageError(page);
333 nfs_readpage_release(req);
d8007d4d
TM
334 put_lseg(lseg);
335 desc->pg_lseg = NULL;
1da177e4
LT
336 return -ENOMEM;
337}
338
c76069bd 339static int nfs_pagein_one(struct nfs_pageio_descriptor *desc)
1da177e4
LT
340{
341 struct nfs_page *req;
342 struct page **pages;
343 struct nfs_read_data *data;
c76069bd
FI
344 struct list_head *head = &desc->pg_list;
345 struct pnfs_layout_segment *lseg = desc->pg_lseg;
dbae4c73 346 int ret = -ENOMEM;
1da177e4 347
c76069bd
FI
348 data = nfs_readdata_alloc(nfs_page_array_len(desc->pg_base,
349 desc->pg_count));
bae724ef
FI
350 if (!data) {
351 nfs_async_read_error(head);
352 goto out;
353 }
1da177e4 354
1da177e4 355 pages = data->pagevec;
1da177e4
LT
356 while (!list_empty(head)) {
357 req = nfs_list_entry(head->next);
358 nfs_list_remove_request(req);
359 nfs_list_add_request(req, &data->pages);
360 ClearPageError(req->wb_page);
361 *pages++ = req->wb_page;
1da177e4
LT
362 }
363 req = nfs_list_entry(data->pages.next);
364
c76069bd
FI
365 ret = nfs_read_rpcsetup(req, data, &nfs_read_full_ops, desc->pg_count,
366 0, lseg);
bae724ef
FI
367out:
368 put_lseg(lseg);
36fe432d 369 desc->pg_lseg = NULL;
dbae4c73 370 return ret;
1da177e4
LT
371}
372
1751c363
TM
373int nfs_generic_pg_readpages(struct nfs_pageio_descriptor *desc)
374{
375 if (desc->pg_bsize < PAGE_CACHE_SIZE)
376 return nfs_pagein_multi(desc);
377 return nfs_pagein_one(desc);
378}
379EXPORT_SYMBOL_GPL(nfs_generic_pg_readpages);
380
381
382static const struct nfs_pageio_ops nfs_pageio_read_ops = {
383 .pg_test = nfs_generic_pg_test,
384 .pg_doio = nfs_generic_pg_readpages,
385};
386
0b671301
TM
387/*
388 * This is the callback from RPC telling us whether a reply was
389 * received or some error occurred (timeout or socket shutdown).
390 */
391int nfs_readpage_result(struct rpc_task *task, struct nfs_read_data *data)
392{
393 int status;
394
3110ff80 395 dprintk("NFS: %s: %5u, (status %d)\n", __func__, task->tk_pid,
0b671301
TM
396 task->tk_status);
397
398 status = NFS_PROTO(data->inode)->read_done(task, data);
399 if (status != 0)
400 return status;
401
402 nfs_add_stats(data->inode, NFSIOS_SERVERREADBYTES, data->res.count);
403
404 if (task->tk_status == -ESTALE) {
3a10c30a 405 set_bit(NFS_INO_STALE, &NFS_I(data->inode)->flags);
0b671301
TM
406 nfs_mark_for_revalidate(data->inode);
407 }
0b671301
TM
408 return 0;
409}
410
fdd1e74c 411static void nfs_readpage_retry(struct rpc_task *task, struct nfs_read_data *data)
0b671301
TM
412{
413 struct nfs_readargs *argp = &data->args;
414 struct nfs_readres *resp = &data->res;
415
416 if (resp->eof || resp->count == argp->count)
d61e612a 417 return;
0b671301
TM
418
419 /* This is a short read! */
420 nfs_inc_stats(data->inode, NFSIOS_SHORTREAD);
421 /* Has the server at least made some progress? */
422 if (resp->count == 0)
d61e612a 423 return;
0b671301
TM
424
425 /* Yes, so retry the read at the end of the data */
cbdabc7f 426 data->mds_offset += resp->count;
0b671301
TM
427 argp->offset += resp->count;
428 argp->pgbase += resp->count;
429 argp->count -= resp->count;
0110ee15 430 nfs_restart_rpc(task, NFS_SERVER(data->inode)->nfs_client);
0b671301
TM
431}
432
1da177e4
LT
433/*
434 * Handle a read reply that fills part of a page.
435 */
ec06c096 436static void nfs_readpage_result_partial(struct rpc_task *task, void *calldata)
1da177e4 437{
ec06c096 438 struct nfs_read_data *data = calldata;
1da177e4 439
ec06c096
TM
440 if (nfs_readpage_result(task, data) != 0)
441 return;
fdd1e74c
TM
442 if (task->tk_status < 0)
443 return;
0b671301 444
fdd1e74c
TM
445 nfs_readpage_truncate_uninitialised_page(data);
446 nfs_readpage_retry(task, data);
447}
448
449static void nfs_readpage_release_partial(void *calldata)
450{
451 struct nfs_read_data *data = calldata;
452 struct nfs_page *req = data->req;
453 struct page *page = req->wb_page;
454 int status = data->task.tk_status;
455
456 if (status < 0)
0b671301 457 SetPageError(page);
fdd1e74c 458
1da177e4
LT
459 if (atomic_dec_and_test(&req->wb_complete)) {
460 if (!PageError(page))
461 SetPageUptodate(page);
462 nfs_readpage_release(req);
463 }
fdd1e74c 464 nfs_readdata_release(calldata);
1da177e4
LT
465}
466
f11c88af
AA
467#if defined(CONFIG_NFS_V4_1)
468void nfs_read_prepare(struct rpc_task *task, void *calldata)
469{
470 struct nfs_read_data *data = calldata;
471
035168ab 472 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
f11c88af
AA
473 &data->args.seq_args, &data->res.seq_res,
474 0, task))
475 return;
476 rpc_call_start(task);
477}
478#endif /* CONFIG_NFS_V4_1 */
479
ec06c096 480static const struct rpc_call_ops nfs_read_partial_ops = {
f11c88af
AA
481#if defined(CONFIG_NFS_V4_1)
482 .rpc_call_prepare = nfs_read_prepare,
483#endif /* CONFIG_NFS_V4_1 */
ec06c096 484 .rpc_call_done = nfs_readpage_result_partial,
fdd1e74c 485 .rpc_release = nfs_readpage_release_partial,
ec06c096
TM
486};
487
1de3fc12
TM
488static void nfs_readpage_set_pages_uptodate(struct nfs_read_data *data)
489{
490 unsigned int count = data->res.count;
491 unsigned int base = data->args.pgbase;
492 struct page **pages;
493
79558f36
TM
494 if (data->res.eof)
495 count = data->args.count;
1de3fc12
TM
496 if (unlikely(count == 0))
497 return;
498 pages = &data->args.pages[base >> PAGE_CACHE_SHIFT];
499 base &= ~PAGE_CACHE_MASK;
500 count += base;
501 for (;count >= PAGE_CACHE_SIZE; count -= PAGE_CACHE_SIZE, pages++)
502 SetPageUptodate(*pages);
0b671301
TM
503 if (count == 0)
504 return;
505 /* Was this a short read? */
506 if (data->res.eof || data->res.count == data->args.count)
1de3fc12
TM
507 SetPageUptodate(*pages);
508}
509
1da177e4
LT
510/*
511 * This is the callback from RPC telling us whether a reply was
512 * received or some error occurred (timeout or socket shutdown).
513 */
ec06c096 514static void nfs_readpage_result_full(struct rpc_task *task, void *calldata)
1da177e4 515{
ec06c096 516 struct nfs_read_data *data = calldata;
1da177e4 517
0b671301
TM
518 if (nfs_readpage_result(task, data) != 0)
519 return;
fdd1e74c
TM
520 if (task->tk_status < 0)
521 return;
1de3fc12 522 /*
0b671301 523 * Note: nfs_readpage_retry may change the values of
1de3fc12 524 * data->args. In the multi-page case, we therefore need
0b671301
TM
525 * to ensure that we call nfs_readpage_set_pages_uptodate()
526 * first.
1de3fc12 527 */
fdd1e74c
TM
528 nfs_readpage_truncate_uninitialised_page(data);
529 nfs_readpage_set_pages_uptodate(data);
530 nfs_readpage_retry(task, data);
531}
532
533static void nfs_readpage_release_full(void *calldata)
534{
535 struct nfs_read_data *data = calldata;
536
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 }
fdd1e74c 543 nfs_readdata_release(calldata);
1da177e4
LT
544}
545
ec06c096 546static const struct rpc_call_ops nfs_read_full_ops = {
f11c88af
AA
547#if defined(CONFIG_NFS_V4_1)
548 .rpc_call_prepare = nfs_read_prepare,
549#endif /* CONFIG_NFS_V4_1 */
ec06c096 550 .rpc_call_done = nfs_readpage_result_full,
fdd1e74c 551 .rpc_release = nfs_readpage_release_full,
ec06c096
TM
552};
553
1da177e4
LT
554/*
555 * Read a page over NFS.
556 * We read the page synchronously in the following case:
557 * - The error flag is set for this page. This happens only when a
558 * previous async read operation failed.
559 */
560int nfs_readpage(struct file *file, struct page *page)
561{
562 struct nfs_open_context *ctx;
563 struct inode *inode = page->mapping->host;
564 int error;
565
566 dprintk("NFS: nfs_readpage (%p %ld@%lu)\n",
567 page, PAGE_CACHE_SIZE, page->index);
91d5b470
CL
568 nfs_inc_stats(inode, NFSIOS_VFSREADPAGE);
569 nfs_add_stats(inode, NFSIOS_READPAGES, 1);
570
1da177e4
LT
571 /*
572 * Try to flush any pending writes to the file..
573 *
574 * NOTE! Because we own the page lock, there cannot
575 * be any new pending writes generated at this point
576 * for this page (other pages can be written to).
577 */
578 error = nfs_wb_page(inode, page);
579 if (error)
de05a0cc
TM
580 goto out_unlock;
581 if (PageUptodate(page))
582 goto out_unlock;
1da177e4 583
5f004cf2
TM
584 error = -ESTALE;
585 if (NFS_STALE(inode))
de05a0cc 586 goto out_unlock;
5f004cf2 587
1da177e4 588 if (file == NULL) {
cf1308ff 589 error = -EBADF;
d530838b 590 ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
1da177e4 591 if (ctx == NULL)
de05a0cc 592 goto out_unlock;
1da177e4 593 } else
cd3758e3 594 ctx = get_nfs_open_context(nfs_file_open_context(file));
1da177e4 595
9a9fc1c0
DH
596 if (!IS_SYNC(inode)) {
597 error = nfs_readpage_from_fscache(ctx, inode, page);
598 if (error == 0)
599 goto out;
600 }
601
8e0969f0
TM
602 error = nfs_readpage_async(ctx, inode, page);
603
9a9fc1c0 604out:
1da177e4
LT
605 put_nfs_open_context(ctx);
606 return error;
de05a0cc 607out_unlock:
1da177e4
LT
608 unlock_page(page);
609 return error;
610}
611
612struct nfs_readdesc {
8b09bee3 613 struct nfs_pageio_descriptor *pgio;
1da177e4
LT
614 struct nfs_open_context *ctx;
615};
616
617static int
618readpage_async_filler(void *data, struct page *page)
619{
620 struct nfs_readdesc *desc = (struct nfs_readdesc *)data;
621 struct inode *inode = page->mapping->host;
622 struct nfs_page *new;
623 unsigned int len;
de05a0cc
TM
624 int error;
625
49a70f27 626 len = nfs_page_length(page);
1da177e4
LT
627 if (len == 0)
628 return nfs_return_empty_page(page);
de05a0cc 629
1da177e4 630 new = nfs_create_request(desc->ctx, inode, page, 0, len);
de05a0cc
TM
631 if (IS_ERR(new))
632 goto out_error;
633
1da177e4 634 if (len < PAGE_CACHE_SIZE)
eebd2aa3 635 zero_user_segment(page, len, PAGE_CACHE_SIZE);
f8512ad0
FI
636 if (!nfs_pageio_add_request(desc->pgio, new)) {
637 error = desc->pgio->pg_error;
638 goto out_unlock;
639 }
1da177e4 640 return 0;
de05a0cc
TM
641out_error:
642 error = PTR_ERR(new);
643 SetPageError(page);
644out_unlock:
645 unlock_page(page);
646 return error;
1da177e4
LT
647}
648
649int nfs_readpages(struct file *filp, struct address_space *mapping,
650 struct list_head *pages, unsigned nr_pages)
651{
8b09bee3 652 struct nfs_pageio_descriptor pgio;
1da177e4 653 struct nfs_readdesc desc = {
8b09bee3 654 .pgio = &pgio,
1da177e4
LT
655 };
656 struct inode *inode = mapping->host;
8b09bee3 657 unsigned long npages;
5f004cf2 658 int ret = -ESTALE;
1da177e4
LT
659
660 dprintk("NFS: nfs_readpages (%s/%Ld %d)\n",
661 inode->i_sb->s_id,
662 (long long)NFS_FILEID(inode),
663 nr_pages);
91d5b470 664 nfs_inc_stats(inode, NFSIOS_VFSREADPAGES);
1da177e4 665
5f004cf2
TM
666 if (NFS_STALE(inode))
667 goto out;
668
1da177e4 669 if (filp == NULL) {
d530838b 670 desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
1da177e4
LT
671 if (desc.ctx == NULL)
672 return -EBADF;
673 } else
cd3758e3 674 desc.ctx = get_nfs_open_context(nfs_file_open_context(filp));
9a9fc1c0
DH
675
676 /* attempt to read as many of the pages as possible from the cache
677 * - this returns -ENOBUFS immediately if the cookie is negative
678 */
679 ret = nfs_readpages_from_fscache(desc.ctx, inode, mapping,
680 pages, &nr_pages);
681 if (ret == 0)
682 goto read_complete; /* all pages were read */
683
1751c363 684 nfs_pageio_init_read(&pgio, inode);
8b09bee3 685
1da177e4 686 ret = read_cache_pages(mapping, pages, readpage_async_filler, &desc);
8b09bee3
TM
687
688 nfs_pageio_complete(&pgio);
689 npages = (pgio.pg_bytes_written + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
690 nfs_add_stats(inode, NFSIOS_READPAGES, npages);
9a9fc1c0 691read_complete:
1da177e4 692 put_nfs_open_context(desc.ctx);
5f004cf2 693out:
1da177e4
LT
694 return ret;
695}
696
f7b422b1 697int __init nfs_init_readpagecache(void)
1da177e4
LT
698{
699 nfs_rdata_cachep = kmem_cache_create("nfs_read_data",
700 sizeof(struct nfs_read_data),
701 0, SLAB_HWCACHE_ALIGN,
20c2df83 702 NULL);
1da177e4
LT
703 if (nfs_rdata_cachep == NULL)
704 return -ENOMEM;
705
93d2341c
MD
706 nfs_rdata_mempool = mempool_create_slab_pool(MIN_POOL_READ,
707 nfs_rdata_cachep);
1da177e4
LT
708 if (nfs_rdata_mempool == NULL)
709 return -ENOMEM;
710
711 return 0;
712}
713
266bee88 714void nfs_destroy_readpagecache(void)
1da177e4
LT
715{
716 mempool_destroy(nfs_rdata_mempool);
1a1d92c1 717 kmem_cache_destroy(nfs_rdata_cachep);
1da177e4 718}