]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - fs/nfs/read.c
NFS: Create a common rw_header_alloc and rw_header_free function
[mirror_ubuntu-eoan-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
f11c88af 23#include "nfs4_fs.h"
49a70f27 24#include "internal.h"
91d5b470 25#include "iostat.h"
9a9fc1c0 26#include "fscache.h"
fab5fc25 27#include "pnfs.h"
91d5b470 28
1da177e4
LT
29#define NFSDBG_FACILITY NFSDBG_PAGECACHE
30
1751c363 31static const struct nfs_pageio_ops nfs_pageio_read_ops;
4db6e0b7 32static const struct rpc_call_ops nfs_read_common_ops;
061ae2ed 33static const struct nfs_pgio_completion_ops nfs_async_read_completion_ops;
4a0de55c 34static const struct nfs_rw_ops nfs_rw_read_ops;
1da177e4 35
e18b890b 36static struct kmem_cache *nfs_rdata_cachep;
1da177e4 37
4a0de55c 38static struct nfs_rw_header *nfs_readhdr_alloc(void)
3feb2d49 39{
4a0de55c 40 return kmem_cache_zalloc(nfs_rdata_cachep, GFP_KERNEL);
4db6e0b7
FI
41}
42
4a0de55c 43static void nfs_readhdr_free(struct nfs_rw_header *rhdr)
3feb2d49 44{
cd841605 45 kmem_cache_free(nfs_rdata_cachep, rhdr);
3feb2d49
TM
46}
47
1da177e4
LT
48static
49int nfs_return_empty_page(struct page *page)
50{
eebd2aa3 51 zero_user(page, 0, PAGE_CACHE_SIZE);
1da177e4
LT
52 SetPageUptodate(page);
53 unlock_page(page);
54 return 0;
55}
56
1abb5088 57void nfs_pageio_init_read(struct nfs_pageio_descriptor *pgio,
fab5fc25 58 struct inode *inode, bool force_mds,
061ae2ed 59 const struct nfs_pgio_completion_ops *compl_ops)
1751c363 60{
fab5fc25
CH
61 struct nfs_server *server = NFS_SERVER(inode);
62 const struct nfs_pageio_ops *pg_ops = &nfs_pageio_read_ops;
63
64#ifdef CONFIG_NFS_V4_1
65 if (server->pnfs_curr_ld && !force_mds)
66 pg_ops = server->pnfs_curr_ld->pg_read_ops;
67#endif
4a0de55c
AS
68 nfs_pageio_init(pgio, inode, pg_ops, compl_ops, &nfs_rw_read_ops,
69 server->rsize, 0);
1751c363 70}
ddda8e0a 71EXPORT_SYMBOL_GPL(nfs_pageio_init_read);
1751c363 72
493292dd
TM
73void nfs_pageio_reset_read_mds(struct nfs_pageio_descriptor *pgio)
74{
75 pgio->pg_ops = &nfs_pageio_read_ops;
76 pgio->pg_bsize = NFS_SERVER(pgio->pg_inode)->rsize;
77}
1f945357 78EXPORT_SYMBOL_GPL(nfs_pageio_reset_read_mds);
493292dd 79
f42b293d
DH
80int nfs_readpage_async(struct nfs_open_context *ctx, struct inode *inode,
81 struct page *page)
1da177e4 82{
1da177e4
LT
83 struct nfs_page *new;
84 unsigned int len;
c76069bd 85 struct nfs_pageio_descriptor pgio;
1da177e4 86
49a70f27 87 len = nfs_page_length(page);
1da177e4
LT
88 if (len == 0)
89 return nfs_return_empty_page(page);
90 new = nfs_create_request(ctx, inode, page, 0, len);
91 if (IS_ERR(new)) {
92 unlock_page(page);
93 return PTR_ERR(new);
94 }
95 if (len < PAGE_CACHE_SIZE)
eebd2aa3 96 zero_user_segment(page, len, PAGE_CACHE_SIZE);
1da177e4 97
fab5fc25
CH
98 nfs_pageio_init_read(&pgio, inode, false,
99 &nfs_async_read_completion_ops);
d8007d4d 100 nfs_pageio_add_request(&pgio, new);
1751c363 101 nfs_pageio_complete(&pgio);
2701d086 102 NFS_I(inode)->read_io += pgio.pg_bytes_written;
1da177e4
LT
103 return 0;
104}
105
106static void nfs_readpage_release(struct nfs_page *req)
107{
3d4ff43d 108 struct inode *d_inode = req->wb_context->dentry->d_inode;
7f8e05f6
DH
109
110 if (PageUptodate(req->wb_page))
111 nfs_readpage_to_fscache(d_inode, req->wb_page, 0);
112
1da177e4
LT
113 unlock_page(req->wb_page);
114
1e8968c5 115 dprintk("NFS: read done (%s/%Lu %d@%Ld)\n",
3d4ff43d 116 req->wb_context->dentry->d_inode->i_sb->s_id,
1e8968c5 117 (unsigned long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1da177e4
LT
118 req->wb_bytes,
119 (long long)req_offset(req));
10d2c46f 120 nfs_release_request(req);
1da177e4
LT
121}
122
4db6e0b7 123/* Note io was page aligned */
061ae2ed 124static void nfs_read_completion(struct nfs_pgio_header *hdr)
4db6e0b7
FI
125{
126 unsigned long bytes = 0;
127
128 if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
129 goto out;
4bd8b010
TM
130 while (!list_empty(&hdr->pages)) {
131 struct nfs_page *req = nfs_list_entry(hdr->pages.next);
132 struct page *page = req->wb_page;
133
134 if (test_bit(NFS_IOHDR_EOF, &hdr->flags)) {
135 if (bytes > hdr->good_bytes)
136 zero_user(page, 0, PAGE_SIZE);
137 else if (hdr->good_bytes - bytes < PAGE_SIZE)
138 zero_user_segment(page,
139 hdr->good_bytes & ~PAGE_MASK,
140 PAGE_SIZE);
4db6e0b7 141 }
4bd8b010
TM
142 bytes += req->wb_bytes;
143 if (test_bit(NFS_IOHDR_ERROR, &hdr->flags)) {
4db6e0b7 144 if (bytes <= hdr->good_bytes)
4bd8b010
TM
145 SetPageUptodate(page);
146 } else
147 SetPageUptodate(page);
148 nfs_list_remove_request(req);
149 nfs_readpage_release(req);
4db6e0b7
FI
150 }
151out:
152 hdr->release(hdr);
153}
154
c5996c4e 155int nfs_initiate_read(struct rpc_clnt *clnt,
9c7e1b3d 156 struct nfs_pgio_data *data,
9f0ec176 157 const struct rpc_call_ops *call_ops, int flags)
1da177e4 158{
cd841605 159 struct inode *inode = data->header->inode;
84115e1c 160 int swap_flags = IS_SWAPFILE(inode) ? NFS_RPC_SWAPFLAGS : 0;
07737691 161 struct rpc_task *task;
bdc7f021
TM
162 struct rpc_message msg = {
163 .rpc_argp = &data->args,
164 .rpc_resp = &data->res,
cd841605 165 .rpc_cred = data->header->cred,
bdc7f021 166 };
84115e1c 167 struct rpc_task_setup task_setup_data = {
07737691 168 .task = &data->task,
64419a9b 169 .rpc_client = clnt,
bdc7f021 170 .rpc_message = &msg,
84115e1c
TM
171 .callback_ops = call_ops,
172 .callback_data = data,
101070ca 173 .workqueue = nfsiod_workqueue,
9f0ec176 174 .flags = RPC_TASK_ASYNC | swap_flags | flags,
84115e1c 175 };
1da177e4 176
64419a9b
AA
177 /* Set up the initial task struct. */
178 NFS_PROTO(inode)->read_setup(data, &msg);
179
1e8968c5 180 dprintk("NFS: %5u initiated read call (req %s/%llu, %u bytes @ "
64419a9b
AA
181 "offset %llu)\n",
182 data->task.tk_pid,
183 inode->i_sb->s_id,
1e8968c5 184 (unsigned long long)NFS_FILEID(inode),
64419a9b
AA
185 data->args.count,
186 (unsigned long long)data->args.offset);
187
188 task = rpc_run_task(&task_setup_data);
189 if (IS_ERR(task))
190 return PTR_ERR(task);
191 rpc_put_task(task);
192 return 0;
193}
dc70d7b3 194EXPORT_SYMBOL_GPL(nfs_initiate_read);
64419a9b
AA
195
196/*
197 * Set up the NFS read request struct
198 */
9c7e1b3d 199static void nfs_read_rpcsetup(struct nfs_pgio_data *data,
6e4efd56 200 unsigned int count, unsigned int offset)
64419a9b 201{
4db6e0b7 202 struct nfs_page *req = data->header->req;
1da177e4 203
4db6e0b7 204 data->args.fh = NFS_FH(data->header->inode);
1da177e4
LT
205 data->args.offset = req_offset(req) + offset;
206 data->args.pgbase = req->wb_pgbase + offset;
30dd374f 207 data->args.pages = data->pages.pagevec;
1da177e4 208 data->args.count = count;
383ba719 209 data->args.context = get_nfs_open_context(req->wb_context);
f11ac8db 210 data->args.lock_context = req->wb_lock_context;
1da177e4
LT
211
212 data->res.fattr = &data->fattr;
213 data->res.count = count;
214 data->res.eof = 0;
0e574af1 215 nfs_fattr_init(&data->fattr);
6e4efd56 216}
1da177e4 217
9c7e1b3d 218static int nfs_do_read(struct nfs_pgio_data *data,
493292dd 219 const struct rpc_call_ops *call_ops)
6e4efd56 220{
cd841605 221 struct inode *inode = data->header->inode;
6e4efd56 222
9f0ec176 223 return nfs_initiate_read(NFS_CLIENT(inode), data, call_ops, 0);
1da177e4
LT
224}
225
275acaaf
TM
226static int
227nfs_do_multiple_reads(struct list_head *head,
493292dd 228 const struct rpc_call_ops *call_ops)
275acaaf 229{
9c7e1b3d 230 struct nfs_pgio_data *data;
275acaaf
TM
231 int ret = 0;
232
233 while (!list_empty(head)) {
234 int ret2;
235
9c7e1b3d 236 data = list_first_entry(head, struct nfs_pgio_data, list);
275acaaf
TM
237 list_del_init(&data->list);
238
493292dd 239 ret2 = nfs_do_read(data, call_ops);
275acaaf
TM
240 if (ret == 0)
241 ret = ret2;
242 }
243 return ret;
244}
245
061ae2ed 246static void
1da177e4
LT
247nfs_async_read_error(struct list_head *head)
248{
249 struct nfs_page *req;
250
251 while (!list_empty(head)) {
252 req = nfs_list_entry(head->next);
253 nfs_list_remove_request(req);
1da177e4
LT
254 nfs_readpage_release(req);
255 }
256}
257
061ae2ed
FI
258static const struct nfs_pgio_completion_ops nfs_async_read_completion_ops = {
259 .error_cleanup = nfs_async_read_error,
260 .completion = nfs_read_completion,
261};
262
25b11dcd
TM
263static void nfs_pagein_error(struct nfs_pageio_descriptor *desc,
264 struct nfs_pgio_header *hdr)
265{
266 set_bit(NFS_IOHDR_REDO, &hdr->flags);
267 while (!list_empty(&hdr->rpc_list)) {
9c7e1b3d
AS
268 struct nfs_pgio_data *data = list_first_entry(&hdr->rpc_list,
269 struct nfs_pgio_data, list);
25b11dcd 270 list_del(&data->list);
00bfa30a 271 nfs_pgio_data_release(data);
25b11dcd
TM
272 }
273 desc->pg_completion_ops->error_cleanup(&desc->pg_list);
274}
275
1da177e4
LT
276/*
277 * Generate multiple requests to fill a single page.
278 *
279 * We optimize to reduce the number of read operations on the wire. If we
280 * detect that we're reading a page, or an area of a page, that is past the
281 * end of file, we do not generate NFS read operations but just clear the
282 * parts of the page that would have come back zero from the server anyway.
283 *
284 * We rely on the cached value of i_size to make this determination; another
285 * client can fill pages on the server past our cached end-of-file, but we
286 * won't see the new data until our attribute cache is updated. This is more
287 * or less conventional NFS client behavior.
288 */
4db6e0b7
FI
289static int nfs_pagein_multi(struct nfs_pageio_descriptor *desc,
290 struct nfs_pgio_header *hdr)
1da177e4 291{
4db6e0b7 292 struct nfs_page *req = hdr->req;
1da177e4 293 struct page *page = req->wb_page;
9c7e1b3d 294 struct nfs_pgio_data *data;
d097971d 295 size_t rsize = desc->pg_bsize, nbytes;
e9f7bee1 296 unsigned int offset;
1da177e4 297
275acaaf 298 offset = 0;
c76069bd 299 nbytes = desc->pg_count;
e9f7bee1
TM
300 do {
301 size_t len = min(nbytes,rsize);
302
00bfa30a 303 data = nfs_pgio_data_alloc(hdr, 1);
25b11dcd
TM
304 if (!data) {
305 nfs_pagein_error(desc, hdr);
306 return -ENOMEM;
307 }
30dd374f 308 data->pages.pagevec[0] = page;
4db6e0b7
FI
309 nfs_read_rpcsetup(data, len, offset);
310 list_add(&data->list, &hdr->rpc_list);
e9f7bee1 311 nbytes -= len;
275acaaf 312 offset += len;
9146ab50 313 } while (nbytes != 0);
25b11dcd
TM
314
315 nfs_list_remove_request(req);
316 nfs_list_add_request(req, &hdr->pages);
4db6e0b7 317 desc->pg_rpc_callops = &nfs_read_common_ops;
9146ab50 318 return 0;
1da177e4
LT
319}
320
4db6e0b7
FI
321static int nfs_pagein_one(struct nfs_pageio_descriptor *desc,
322 struct nfs_pgio_header *hdr)
1da177e4
LT
323{
324 struct nfs_page *req;
325 struct page **pages;
9c7e1b3d 326 struct nfs_pgio_data *data;
c76069bd 327 struct list_head *head = &desc->pg_list;
1da177e4 328
00bfa30a 329 data = nfs_pgio_data_alloc(hdr, nfs_page_array_len(desc->pg_base,
4db6e0b7
FI
330 desc->pg_count));
331 if (!data) {
25b11dcd 332 nfs_pagein_error(desc, hdr);
9146ab50 333 return -ENOMEM;
bae724ef 334 }
1da177e4 335
30dd374f 336 pages = data->pages.pagevec;
1da177e4
LT
337 while (!list_empty(head)) {
338 req = nfs_list_entry(head->next);
339 nfs_list_remove_request(req);
4db6e0b7 340 nfs_list_add_request(req, &hdr->pages);
1da177e4 341 *pages++ = req->wb_page;
1da177e4 342 }
1da177e4 343
4db6e0b7
FI
344 nfs_read_rpcsetup(data, desc->pg_count, 0);
345 list_add(&data->list, &hdr->rpc_list);
346 desc->pg_rpc_callops = &nfs_read_common_ops;
9146ab50 347 return 0;
1da177e4
LT
348}
349
4db6e0b7
FI
350int nfs_generic_pagein(struct nfs_pageio_descriptor *desc,
351 struct nfs_pgio_header *hdr)
493292dd
TM
352{
353 if (desc->pg_bsize < PAGE_CACHE_SIZE)
4db6e0b7
FI
354 return nfs_pagein_multi(desc, hdr);
355 return nfs_pagein_one(desc, hdr);
493292dd 356}
89d77c8f 357EXPORT_SYMBOL_GPL(nfs_generic_pagein);
493292dd
TM
358
359static int nfs_generic_pg_readpages(struct nfs_pageio_descriptor *desc)
1751c363 360{
c0752cdf 361 struct nfs_rw_header *rhdr;
4db6e0b7 362 struct nfs_pgio_header *hdr;
275acaaf
TM
363 int ret;
364
4a0de55c 365 rhdr = nfs_rw_header_alloc(desc->pg_rw_ops);
4db6e0b7 366 if (!rhdr) {
061ae2ed 367 desc->pg_completion_ops->error_cleanup(&desc->pg_list);
4db6e0b7
FI
368 return -ENOMEM;
369 }
370 hdr = &rhdr->header;
4a0de55c 371 nfs_pgheader_init(desc, hdr, nfs_rw_header_free);
4db6e0b7
FI
372 atomic_inc(&hdr->refcnt);
373 ret = nfs_generic_pagein(desc, hdr);
50828d7e 374 if (ret == 0)
4db6e0b7
FI
375 ret = nfs_do_multiple_reads(&hdr->rpc_list,
376 desc->pg_rpc_callops);
4db6e0b7 377 if (atomic_dec_and_test(&hdr->refcnt))
061ae2ed 378 hdr->completion_ops->completion(hdr);
275acaaf 379 return ret;
1751c363 380}
1751c363
TM
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 */
9c7e1b3d 391int nfs_readpage_result(struct rpc_task *task, struct nfs_pgio_data *data)
0b671301 392{
cd841605 393 struct inode *inode = data->header->inode;
0b671301
TM
394 int status;
395
3110ff80 396 dprintk("NFS: %s: %5u, (status %d)\n", __func__, task->tk_pid,
0b671301
TM
397 task->tk_status);
398
cd841605 399 status = NFS_PROTO(inode)->read_done(task, data);
0b671301
TM
400 if (status != 0)
401 return status;
402
cd841605 403 nfs_add_stats(inode, NFSIOS_SERVERREADBYTES, data->res.count);
0b671301
TM
404
405 if (task->tk_status == -ESTALE) {
cd841605
FI
406 set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
407 nfs_mark_for_revalidate(inode);
0b671301 408 }
0b671301
TM
409 return 0;
410}
411
9c7e1b3d 412static void nfs_readpage_retry(struct rpc_task *task, struct nfs_pgio_data *data)
0b671301 413{
3c6b899c 414 struct nfs_pgio_args *argp = &data->args;
9137bdf3 415 struct nfs_pgio_res *resp = &data->res;
0b671301 416
0b671301 417 /* This is a short read! */
cd841605 418 nfs_inc_stats(data->header->inode, NFSIOS_SHORTREAD);
0b671301 419 /* Has the server at least made some progress? */
4db6e0b7
FI
420 if (resp->count == 0) {
421 nfs_set_pgio_error(data->header, -EIO, argp->offset);
d61e612a 422 return;
4db6e0b7 423 }
0b671301 424 /* Yes, so retry the read at the end of the data */
cbdabc7f 425 data->mds_offset += resp->count;
0b671301
TM
426 argp->offset += resp->count;
427 argp->pgbase += resp->count;
428 argp->count -= resp->count;
d00c5d43 429 rpc_restart_call_prepare(task);
0b671301
TM
430}
431
4db6e0b7 432static void nfs_readpage_result_common(struct rpc_task *task, void *calldata)
1da177e4 433{
9c7e1b3d 434 struct nfs_pgio_data *data = calldata;
4db6e0b7
FI
435 struct nfs_pgio_header *hdr = data->header;
436
437 /* Note the only returns of nfs_readpage_result are 0 and -EAGAIN */
ec06c096
TM
438 if (nfs_readpage_result(task, data) != 0)
439 return;
fdd1e74c 440 if (task->tk_status < 0)
4db6e0b7
FI
441 nfs_set_pgio_error(hdr, task->tk_status, data->args.offset);
442 else if (data->res.eof) {
443 loff_t bound;
444
445 bound = data->args.offset + data->res.count;
446 spin_lock(&hdr->lock);
447 if (bound < hdr->io_start + hdr->good_bytes) {
448 set_bit(NFS_IOHDR_EOF, &hdr->flags);
449 clear_bit(NFS_IOHDR_ERROR, &hdr->flags);
450 hdr->good_bytes = bound - hdr->io_start;
451 }
452 spin_unlock(&hdr->lock);
453 } else if (data->res.count != data->args.count)
454 nfs_readpage_retry(task, data);
fdd1e74c
TM
455}
456
4db6e0b7 457static void nfs_readpage_release_common(void *calldata)
fdd1e74c 458{
00bfa30a 459 nfs_pgio_data_release(calldata);
1da177e4
LT
460}
461
f11c88af
AA
462void nfs_read_prepare(struct rpc_task *task, void *calldata)
463{
9c7e1b3d 464 struct nfs_pgio_data *data = calldata;
ef1820f9
N
465 int err;
466 err = NFS_PROTO(data->header->inode)->read_rpc_prepare(task, data);
467 if (err)
468 rpc_exit(task, err);
f11c88af 469}
f11c88af 470
4db6e0b7 471static const struct rpc_call_ops nfs_read_common_ops = {
f11c88af 472 .rpc_call_prepare = nfs_read_prepare,
4db6e0b7
FI
473 .rpc_call_done = nfs_readpage_result_common,
474 .rpc_release = nfs_readpage_release_common,
ec06c096
TM
475};
476
1da177e4
LT
477/*
478 * Read a page over NFS.
479 * We read the page synchronously in the following case:
480 * - The error flag is set for this page. This happens only when a
481 * previous async read operation failed.
482 */
483int nfs_readpage(struct file *file, struct page *page)
484{
485 struct nfs_open_context *ctx;
d56b4ddf 486 struct inode *inode = page_file_mapping(page)->host;
1da177e4
LT
487 int error;
488
489 dprintk("NFS: nfs_readpage (%p %ld@%lu)\n",
d56b4ddf 490 page, PAGE_CACHE_SIZE, page_file_index(page));
91d5b470
CL
491 nfs_inc_stats(inode, NFSIOS_VFSREADPAGE);
492 nfs_add_stats(inode, NFSIOS_READPAGES, 1);
493
1da177e4
LT
494 /*
495 * Try to flush any pending writes to the file..
496 *
497 * NOTE! Because we own the page lock, there cannot
498 * be any new pending writes generated at this point
499 * for this page (other pages can be written to).
500 */
501 error = nfs_wb_page(inode, page);
502 if (error)
de05a0cc
TM
503 goto out_unlock;
504 if (PageUptodate(page))
505 goto out_unlock;
1da177e4 506
5f004cf2
TM
507 error = -ESTALE;
508 if (NFS_STALE(inode))
de05a0cc 509 goto out_unlock;
5f004cf2 510
1da177e4 511 if (file == NULL) {
cf1308ff 512 error = -EBADF;
d530838b 513 ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
1da177e4 514 if (ctx == NULL)
de05a0cc 515 goto out_unlock;
1da177e4 516 } else
cd3758e3 517 ctx = get_nfs_open_context(nfs_file_open_context(file));
1da177e4 518
9a9fc1c0
DH
519 if (!IS_SYNC(inode)) {
520 error = nfs_readpage_from_fscache(ctx, inode, page);
521 if (error == 0)
522 goto out;
523 }
524
8e0969f0
TM
525 error = nfs_readpage_async(ctx, inode, page);
526
9a9fc1c0 527out:
1da177e4
LT
528 put_nfs_open_context(ctx);
529 return error;
de05a0cc 530out_unlock:
1da177e4
LT
531 unlock_page(page);
532 return error;
533}
534
535struct nfs_readdesc {
8b09bee3 536 struct nfs_pageio_descriptor *pgio;
1da177e4
LT
537 struct nfs_open_context *ctx;
538};
539
540static int
541readpage_async_filler(void *data, struct page *page)
542{
543 struct nfs_readdesc *desc = (struct nfs_readdesc *)data;
d56b4ddf 544 struct inode *inode = page_file_mapping(page)->host;
1da177e4
LT
545 struct nfs_page *new;
546 unsigned int len;
de05a0cc
TM
547 int error;
548
49a70f27 549 len = nfs_page_length(page);
1da177e4
LT
550 if (len == 0)
551 return nfs_return_empty_page(page);
de05a0cc 552
1da177e4 553 new = nfs_create_request(desc->ctx, inode, page, 0, len);
de05a0cc
TM
554 if (IS_ERR(new))
555 goto out_error;
556
1da177e4 557 if (len < PAGE_CACHE_SIZE)
eebd2aa3 558 zero_user_segment(page, len, PAGE_CACHE_SIZE);
f8512ad0
FI
559 if (!nfs_pageio_add_request(desc->pgio, new)) {
560 error = desc->pgio->pg_error;
561 goto out_unlock;
562 }
1da177e4 563 return 0;
de05a0cc
TM
564out_error:
565 error = PTR_ERR(new);
de05a0cc
TM
566out_unlock:
567 unlock_page(page);
568 return error;
1da177e4
LT
569}
570
571int nfs_readpages(struct file *filp, struct address_space *mapping,
572 struct list_head *pages, unsigned nr_pages)
573{
8b09bee3 574 struct nfs_pageio_descriptor pgio;
1da177e4 575 struct nfs_readdesc desc = {
8b09bee3 576 .pgio = &pgio,
1da177e4
LT
577 };
578 struct inode *inode = mapping->host;
8b09bee3 579 unsigned long npages;
5f004cf2 580 int ret = -ESTALE;
1da177e4 581
1e8968c5 582 dprintk("NFS: nfs_readpages (%s/%Lu %d)\n",
1da177e4 583 inode->i_sb->s_id,
1e8968c5 584 (unsigned long long)NFS_FILEID(inode),
1da177e4 585 nr_pages);
91d5b470 586 nfs_inc_stats(inode, NFSIOS_VFSREADPAGES);
1da177e4 587
5f004cf2
TM
588 if (NFS_STALE(inode))
589 goto out;
590
1da177e4 591 if (filp == NULL) {
d530838b 592 desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
1da177e4
LT
593 if (desc.ctx == NULL)
594 return -EBADF;
595 } else
cd3758e3 596 desc.ctx = get_nfs_open_context(nfs_file_open_context(filp));
9a9fc1c0
DH
597
598 /* attempt to read as many of the pages as possible from the cache
599 * - this returns -ENOBUFS immediately if the cookie is negative
600 */
601 ret = nfs_readpages_from_fscache(desc.ctx, inode, mapping,
602 pages, &nr_pages);
603 if (ret == 0)
604 goto read_complete; /* all pages were read */
605
fab5fc25
CH
606 nfs_pageio_init_read(&pgio, inode, false,
607 &nfs_async_read_completion_ops);
8b09bee3 608
1da177e4 609 ret = read_cache_pages(mapping, pages, readpage_async_filler, &desc);
8b09bee3
TM
610
611 nfs_pageio_complete(&pgio);
2701d086 612 NFS_I(inode)->read_io += pgio.pg_bytes_written;
8b09bee3
TM
613 npages = (pgio.pg_bytes_written + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
614 nfs_add_stats(inode, NFSIOS_READPAGES, npages);
9a9fc1c0 615read_complete:
1da177e4 616 put_nfs_open_context(desc.ctx);
5f004cf2 617out:
1da177e4
LT
618 return ret;
619}
620
f7b422b1 621int __init nfs_init_readpagecache(void)
1da177e4
LT
622{
623 nfs_rdata_cachep = kmem_cache_create("nfs_read_data",
c0752cdf 624 sizeof(struct nfs_rw_header),
1da177e4 625 0, SLAB_HWCACHE_ALIGN,
20c2df83 626 NULL);
1da177e4
LT
627 if (nfs_rdata_cachep == NULL)
628 return -ENOMEM;
629
1da177e4
LT
630 return 0;
631}
632
266bee88 633void nfs_destroy_readpagecache(void)
1da177e4 634{
1a1d92c1 635 kmem_cache_destroy(nfs_rdata_cachep);
1da177e4 636}
4a0de55c
AS
637
638static const struct nfs_rw_ops nfs_rw_read_ops = {
639 .rw_alloc_header = nfs_readhdr_alloc,
640 .rw_free_header = nfs_readhdr_free,
641};