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