blob: f6ab30b5a462831167cdaa25067ff5f2662a4812 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#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>
Andy Adamson64419a92011-03-01 01:34:16 +000021#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Fred Isamanbae724e2011-03-01 01:34:15 +000023#include "pnfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Andy Adamsonf11c88a2009-04-01 09:22:25 -040025#include "nfs4_fs.h"
Trond Myklebust49a70f22006-12-05 00:35:38 -050026#include "internal.h"
Chuck Lever91d5b472006-03-20 13:44:14 -050027#include "iostat.h"
David Howells9a9fc1c2009-04-03 16:42:44 +010028#include "fscache.h"
Chuck Lever91d5b472006-03-20 13:44:14 -050029
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#define NFSDBG_FACILITY NFSDBG_PAGECACHE
31
Trond Myklebust1751c362011-06-10 13:30:23 -040032static const struct nfs_pageio_ops nfs_pageio_read_ops;
Trond Myklebustec06c092006-03-20 13:44:27 -050033static const struct rpc_call_ops nfs_read_partial_ops;
34static const struct rpc_call_ops nfs_read_full_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Christoph Lametere18b8902006-12-06 20:33:20 -080036static struct kmem_cache *nfs_rdata_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Fred Isamancd841602012-04-20 14:47:44 -040038struct nfs_read_header *nfs_readhdr_alloc(unsigned int pagecount)
Trond Myklebust3feb2d42006-03-20 13:44:37 -050039{
Fred Isamancd841602012-04-20 14:47:44 -040040 struct nfs_read_header *p;
Trond Myklebust3feb2d42006-03-20 13:44:37 -050041
Trond Myklebustb6ee8cd2011-10-19 12:17:29 -070042 p = kmem_cache_zalloc(nfs_rdata_cachep, GFP_KERNEL);
Trond Myklebust3feb2d42006-03-20 13:44:37 -050043 if (p) {
Fred Isamancd841602012-04-20 14:47:44 -040044 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);
Fred Isamancd841602012-04-20 14:47:44 -040049 data->header = hdr;
Fred Isaman30dd3742012-04-20 14:47:45 -040050 if (!nfs_pgarray_set(&data->pages, pagecount)) {
51 kmem_cache_free(nfs_rdata_cachep, p);
52 p = NULL;
Trond Myklebust3feb2d42006-03-20 13:44:37 -050053 }
54 }
55 return p;
56}
57
Fred Isamancd841602012-04-20 14:47:44 -040058void nfs_readhdr_free(struct nfs_pgio_header *hdr)
Trond Myklebust3feb2d42006-03-20 13:44:37 -050059{
Fred Isamancd841602012-04-20 14:47:44 -040060 struct nfs_read_header *rhdr = container_of(hdr, struct nfs_read_header, header);
61
62 kmem_cache_free(nfs_rdata_cachep, rhdr);
Trond Myklebust3feb2d42006-03-20 13:44:37 -050063}
64
Trond Myklebust493292d2011-07-13 15:58:28 -040065void nfs_readdata_release(struct nfs_read_data *rdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
Trond Myklebust383ba712008-02-19 20:04:20 -050067 put_nfs_open_context(rdata->args.context);
Fred Isaman30dd3742012-04-20 14:47:45 -040068 if (rdata->pages.pagevec != rdata->pages.page_array)
69 kfree(rdata->pages.pagevec);
Fred Isamancd841602012-04-20 14:47:44 -040070 nfs_readhdr_free(rdata->header);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071}
72
73static
Linus Torvalds1da177e2005-04-16 15:20:36 -070074int nfs_return_empty_page(struct page *page)
75{
Christoph Lametereebd2aa2008-02-04 22:28:29 -080076 zero_user(page, 0, PAGE_CACHE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 SetPageUptodate(page);
78 unlock_page(page);
79 return 0;
80}
81
Trond Myklebust1de3fc12006-05-25 01:40:44 -040082static void nfs_readpage_truncate_uninitialised_page(struct nfs_read_data *data)
83{
84 unsigned int remainder = data->args.count - data->res.count;
85 unsigned int base = data->args.pgbase + data->res.count;
86 unsigned int pglen;
87 struct page **pages;
88
89 if (data->res.eof == 0 || remainder == 0)
90 return;
91 /*
92 * Note: "remainder" can never be negative, since we check for
93 * this in the XDR code.
94 */
95 pages = &data->args.pages[base >> PAGE_CACHE_SHIFT];
96 base &= ~PAGE_CACHE_MASK;
97 pglen = PAGE_CACHE_SIZE - base;
Trond Myklebust79558f32006-08-22 13:44:32 -040098 for (;;) {
99 if (remainder <= pglen) {
Christoph Lametereebd2aa2008-02-04 22:28:29 -0800100 zero_user(*pages, base, remainder);
Trond Myklebust79558f32006-08-22 13:44:32 -0400101 break;
102 }
Christoph Lametereebd2aa2008-02-04 22:28:29 -0800103 zero_user(*pages, base, pglen);
Trond Myklebust79558f32006-08-22 13:44:32 -0400104 pages++;
105 remainder -= pglen;
106 pglen = PAGE_CACHE_SIZE;
107 base = 0;
108 }
Trond Myklebust1de3fc12006-05-25 01:40:44 -0400109}
110
Trond Myklebust62e4a762011-11-10 14:30:37 -0500111void nfs_pageio_init_read_mds(struct nfs_pageio_descriptor *pgio,
Trond Myklebust1751c362011-06-10 13:30:23 -0400112 struct inode *inode)
113{
114 nfs_pageio_init(pgio, inode, &nfs_pageio_read_ops,
115 NFS_SERVER(inode)->rsize, 0);
116}
117
Trond Myklebust493292d2011-07-13 15:58:28 -0400118void nfs_pageio_reset_read_mds(struct nfs_pageio_descriptor *pgio)
119{
120 pgio->pg_ops = &nfs_pageio_read_ops;
121 pgio->pg_bsize = NFS_SERVER(pgio->pg_inode)->rsize;
122}
Trond Myklebust1f945352011-07-13 15:59:57 -0400123EXPORT_SYMBOL_GPL(nfs_pageio_reset_read_mds);
Trond Myklebust493292d2011-07-13 15:58:28 -0400124
Trond Myklebust1751c362011-06-10 13:30:23 -0400125static 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
David Howellsf42b2932009-04-03 16:42:44 +0100132int nfs_readpage_async(struct nfs_open_context *ctx, struct inode *inode,
133 struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 struct nfs_page *new;
136 unsigned int len;
Fred Isamanc76069b2011-03-03 15:13:48 +0000137 struct nfs_pageio_descriptor pgio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Trond Myklebust49a70f22006-12-05 00:35:38 -0500139 len = nfs_page_length(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 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)
Christoph Lametereebd2aa2008-02-04 22:28:29 -0800148 zero_user_segment(page, len, PAGE_CACHE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Trond Myklebust1751c362011-06-10 13:30:23 -0400150 nfs_pageio_init_read(&pgio, inode);
Trond Myklebustd8007d42011-06-10 13:30:23 -0400151 nfs_pageio_add_request(&pgio, new);
Trond Myklebust1751c362011-06-10 13:30:23 -0400152 nfs_pageio_complete(&pgio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 return 0;
154}
155
156static void nfs_readpage_release(struct nfs_page *req)
157{
Al Viro3d4ff432011-06-22 18:40:12 -0400158 struct inode *d_inode = req->wb_context->dentry->d_inode;
David Howells7f8e05f2009-04-03 16:42:45 +0100159
160 if (PageUptodate(req->wb_page))
161 nfs_readpage_to_fscache(d_inode, req->wb_page, 0);
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 unlock_page(req->wb_page);
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 dprintk("NFS: read done (%s/%Ld %d@%Ld)\n",
Al Viro3d4ff432011-06-22 18:40:12 -0400166 req->wb_context->dentry->d_inode->i_sb->s_id,
167 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 req->wb_bytes,
169 (long long)req_offset(req));
Nick Wilson10d2c46f2005-09-22 21:44:28 -0700170 nfs_release_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
Fred Isamanc5996c42012-04-20 14:47:41 -0400173int nfs_initiate_read(struct rpc_clnt *clnt,
174 struct nfs_read_data *data,
Andy Adamson64419a92011-03-01 01:34:16 +0000175 const struct rpc_call_ops *call_ops)
176{
Fred Isamancd841602012-04-20 14:47:44 -0400177 struct inode *inode = data->header->inode;
Andy Adamson64419a92011-03-01 01:34:16 +0000178 int swap_flags = IS_SWAPFILE(inode) ? NFS_RPC_SWAPFLAGS : 0;
179 struct rpc_task *task;
180 struct rpc_message msg = {
181 .rpc_argp = &data->args,
182 .rpc_resp = &data->res,
Fred Isamancd841602012-04-20 14:47:44 -0400183 .rpc_cred = data->header->cred,
Andy Adamson64419a92011-03-01 01:34:16 +0000184 };
185 struct rpc_task_setup task_setup_data = {
186 .task = &data->task,
187 .rpc_client = clnt,
188 .rpc_message = &msg,
189 .callback_ops = call_ops,
190 .callback_data = data,
191 .workqueue = nfsiod_workqueue,
192 .flags = RPC_TASK_ASYNC | swap_flags,
193 };
194
195 /* Set up the initial task struct. */
196 NFS_PROTO(inode)->read_setup(data, &msg);
197
198 dprintk("NFS: %5u initiated read call (req %s/%lld, %u bytes @ "
199 "offset %llu)\n",
200 data->task.tk_pid,
201 inode->i_sb->s_id,
202 (long long)NFS_FILEID(inode),
203 data->args.count,
204 (unsigned long long)data->args.offset);
205
206 task = rpc_run_task(&task_setup_data);
207 if (IS_ERR(task))
208 return PTR_ERR(task);
209 rpc_put_task(task);
210 return 0;
211}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000212EXPORT_SYMBOL_GPL(nfs_initiate_read);
Andy Adamson64419a92011-03-01 01:34:16 +0000213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214/*
215 * Set up the NFS read request struct
216 */
Trond Myklebust6e4efd52011-07-12 13:42:02 -0400217static void nfs_read_rpcsetup(struct nfs_page *req, struct nfs_read_data *data,
218 unsigned int count, unsigned int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
Fred Isamancd841602012-04-20 14:47:44 -0400220 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Fred Isamancd841602012-04-20 14:47:44 -0400222 data->header->req = req;
223 data->header->inode = inode;
224 data->header->cred = req->wb_context->cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 data->args.fh = NFS_FH(inode);
227 data->args.offset = req_offset(req) + offset;
228 data->args.pgbase = req->wb_pgbase + offset;
Fred Isaman30dd3742012-04-20 14:47:45 -0400229 data->args.pages = data->pages.pagevec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 data->args.count = count;
Trond Myklebust383ba712008-02-19 20:04:20 -0500231 data->args.context = get_nfs_open_context(req->wb_context);
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400232 data->args.lock_context = req->wb_lock_context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 data->res.fattr = &data->fattr;
235 data->res.count = count;
236 data->res.eof = 0;
Trond Myklebust0e574af2005-10-27 22:12:38 -0400237 nfs_fattr_init(&data->fattr);
Trond Myklebust6e4efd52011-07-12 13:42:02 -0400238}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Trond Myklebust6e4efd52011-07-12 13:42:02 -0400240static int nfs_do_read(struct nfs_read_data *data,
Trond Myklebust493292d2011-07-13 15:58:28 -0400241 const struct rpc_call_ops *call_ops)
Trond Myklebust6e4efd52011-07-12 13:42:02 -0400242{
Fred Isamancd841602012-04-20 14:47:44 -0400243 struct inode *inode = data->header->inode;
Trond Myklebust6e4efd52011-07-12 13:42:02 -0400244
Fred Isamanc5996c42012-04-20 14:47:41 -0400245 return nfs_initiate_read(NFS_CLIENT(inode), data, call_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246}
247
Trond Myklebust275acaa2011-07-12 13:42:02 -0400248static int
249nfs_do_multiple_reads(struct list_head *head,
Trond Myklebust493292d2011-07-13 15:58:28 -0400250 const struct rpc_call_ops *call_ops)
Trond Myklebust275acaa2011-07-12 13:42:02 -0400251{
252 struct nfs_read_data *data;
253 int ret = 0;
254
255 while (!list_empty(head)) {
256 int ret2;
257
258 data = list_entry(head->next, struct nfs_read_data, list);
259 list_del_init(&data->list);
260
Trond Myklebust493292d2011-07-13 15:58:28 -0400261 ret2 = nfs_do_read(data, call_ops);
Trond Myklebust275acaa2011-07-12 13:42:02 -0400262 if (ret == 0)
263 ret = ret2;
264 }
265 return ret;
266}
267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268static void
269nfs_async_read_error(struct list_head *head)
270{
271 struct nfs_page *req;
272
273 while (!list_empty(head)) {
274 req = nfs_list_entry(head->next);
275 nfs_list_remove_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 nfs_readpage_release(req);
277 }
278}
279
280/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 * Generate multiple requests to fill a single page.
282 *
283 * We optimize to reduce the number of read operations on the wire. If we
284 * detect that we're reading a page, or an area of a page, that is past the
285 * end of file, we do not generate NFS read operations but just clear the
286 * parts of the page that would have come back zero from the server anyway.
287 *
288 * We rely on the cached value of i_size to make this determination; another
289 * client can fill pages on the server past our cached end-of-file, but we
290 * won't see the new data until our attribute cache is updated. This is more
291 * or less conventional NFS client behavior.
292 */
Trond Myklebust275acaa2011-07-12 13:42:02 -0400293static int nfs_pagein_multi(struct nfs_pageio_descriptor *desc, struct list_head *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
Fred Isamanc76069b2011-03-03 15:13:48 +0000295 struct nfs_page *req = nfs_list_entry(desc->pg_list.next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 struct page *page = req->wb_page;
Fred Isamancd841602012-04-20 14:47:44 -0400297 struct nfs_read_header *rhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 struct nfs_read_data *data;
Trond Myklebustd0979712011-07-12 13:42:02 -0400299 size_t rsize = desc->pg_bsize, nbytes;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700300 unsigned int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 int requests = 0;
Trond Myklebustdbae4c72008-04-14 14:54:53 -0400302 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304 nfs_list_remove_request(req);
305
Trond Myklebust275acaa2011-07-12 13:42:02 -0400306 offset = 0;
Fred Isamanc76069b2011-03-03 15:13:48 +0000307 nbytes = desc->pg_count;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700308 do {
309 size_t len = min(nbytes,rsize);
310
Fred Isamancd841602012-04-20 14:47:44 -0400311 rhdr = nfs_readhdr_alloc(1);
312 if (!rhdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 goto out_bad;
Fred Isamancd841602012-04-20 14:47:44 -0400314 data = &rhdr->rpc_data;
Fred Isaman30dd3742012-04-20 14:47:45 -0400315 data->pages.pagevec[0] = page;
Trond Myklebust275acaa2011-07-12 13:42:02 -0400316 nfs_read_rpcsetup(req, data, len, offset);
317 list_add(&data->list, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 requests++;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700319 nbytes -= len;
Trond Myklebust275acaa2011-07-12 13:42:02 -0400320 offset += len;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700321 } while(nbytes != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 atomic_set(&req->wb_complete, requests);
Trond Myklebust50828d72011-07-12 13:42:02 -0400323 desc->pg_rpc_callops = &nfs_read_partial_ops;
Trond Myklebustdbae4c72008-04-14 14:54:53 -0400324 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325out_bad:
Trond Myklebust275acaa2011-07-12 13:42:02 -0400326 while (!list_empty(res)) {
327 data = list_entry(res->next, struct nfs_read_data, list);
Trond Myklebust6e4efd52011-07-12 13:42:02 -0400328 list_del(&data->list);
Fred Isaman73fb7bc2012-04-20 14:47:34 -0400329 nfs_readdata_release(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 nfs_readpage_release(req);
332 return -ENOMEM;
333}
334
Trond Myklebust275acaa2011-07-12 13:42:02 -0400335static int nfs_pagein_one(struct nfs_pageio_descriptor *desc, struct list_head *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
337 struct nfs_page *req;
338 struct page **pages;
Fred Isamancd841602012-04-20 14:47:44 -0400339 struct nfs_read_header *rhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 struct nfs_read_data *data;
Fred Isamanc76069b2011-03-03 15:13:48 +0000341 struct list_head *head = &desc->pg_list;
Peng Tao3b609182011-07-15 03:33:42 -0400342 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Fred Isamancd841602012-04-20 14:47:44 -0400344 rhdr = nfs_readhdr_alloc(nfs_page_array_len(desc->pg_base,
345 desc->pg_count));
346 if (!rhdr) {
Fred Isamanbae724e2011-03-01 01:34:15 +0000347 nfs_async_read_error(head);
Peng Tao3b609182011-07-15 03:33:42 -0400348 ret = -ENOMEM;
Fred Isamanbae724e2011-03-01 01:34:15 +0000349 goto out;
350 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Fred Isamancd841602012-04-20 14:47:44 -0400352 data = &rhdr->rpc_data;
Fred Isaman30dd3742012-04-20 14:47:45 -0400353 pages = data->pages.pagevec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 while (!list_empty(head)) {
355 req = nfs_list_entry(head->next);
356 nfs_list_remove_request(req);
Fred Isamancd841602012-04-20 14:47:44 -0400357 nfs_list_add_request(req, &rhdr->header.pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 *pages++ = req->wb_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 }
Fred Isamancd841602012-04-20 14:47:44 -0400360 req = nfs_list_entry(rhdr->header.pages.next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Trond Myklebust6e4efd52011-07-12 13:42:02 -0400362 nfs_read_rpcsetup(req, data, desc->pg_count, 0);
Trond Myklebust275acaa2011-07-12 13:42:02 -0400363 list_add(&data->list, res);
Trond Myklebust50828d72011-07-12 13:42:02 -0400364 desc->pg_rpc_callops = &nfs_read_full_ops;
Fred Isamanbae724e2011-03-01 01:34:15 +0000365out:
Trond Myklebustdbae4c72008-04-14 14:54:53 -0400366 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367}
368
Trond Myklebust493292d2011-07-13 15:58:28 -0400369int nfs_generic_pagein(struct nfs_pageio_descriptor *desc, struct list_head *head)
370{
371 if (desc->pg_bsize < PAGE_CACHE_SIZE)
372 return nfs_pagein_multi(desc, head);
373 return nfs_pagein_one(desc, head);
374}
375
376static int nfs_generic_pg_readpages(struct nfs_pageio_descriptor *desc)
Trond Myklebust1751c362011-06-10 13:30:23 -0400377{
Trond Myklebust275acaa2011-07-12 13:42:02 -0400378 LIST_HEAD(head);
379 int ret;
380
Trond Myklebust493292d2011-07-13 15:58:28 -0400381 ret = nfs_generic_pagein(desc, &head);
Trond Myklebust50828d72011-07-12 13:42:02 -0400382 if (ret == 0)
Trond Myklebust493292d2011-07-13 15:58:28 -0400383 ret = nfs_do_multiple_reads(&head, desc->pg_rpc_callops);
Trond Myklebust275acaa2011-07-12 13:42:02 -0400384 return ret;
Trond Myklebust1751c362011-06-10 13:30:23 -0400385}
Trond Myklebust1751c362011-06-10 13:30:23 -0400386
387static const struct nfs_pageio_ops nfs_pageio_read_ops = {
388 .pg_test = nfs_generic_pg_test,
389 .pg_doio = nfs_generic_pg_readpages,
390};
391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392/*
Trond Myklebust0b671302006-11-14 16:12:23 -0500393 * This is the callback from RPC telling us whether a reply was
394 * received or some error occurred (timeout or socket shutdown).
395 */
396int nfs_readpage_result(struct rpc_task *task, struct nfs_read_data *data)
397{
Fred Isamancd841602012-04-20 14:47:44 -0400398 struct inode *inode = data->header->inode;
Trond Myklebust0b671302006-11-14 16:12:23 -0500399 int status;
400
Harvey Harrison3110ff82008-05-02 13:42:44 -0700401 dprintk("NFS: %s: %5u, (status %d)\n", __func__, task->tk_pid,
Trond Myklebust0b671302006-11-14 16:12:23 -0500402 task->tk_status);
403
Fred Isamancd841602012-04-20 14:47:44 -0400404 status = NFS_PROTO(inode)->read_done(task, data);
Trond Myklebust0b671302006-11-14 16:12:23 -0500405 if (status != 0)
406 return status;
407
Fred Isamancd841602012-04-20 14:47:44 -0400408 nfs_add_stats(inode, NFSIOS_SERVERREADBYTES, data->res.count);
Trond Myklebust0b671302006-11-14 16:12:23 -0500409
410 if (task->tk_status == -ESTALE) {
Fred Isamancd841602012-04-20 14:47:44 -0400411 set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
412 nfs_mark_for_revalidate(inode);
Trond Myklebust0b671302006-11-14 16:12:23 -0500413 }
Trond Myklebust0b671302006-11-14 16:12:23 -0500414 return 0;
415}
416
Trond Myklebustfdd1e742008-04-15 16:33:58 -0400417static void nfs_readpage_retry(struct rpc_task *task, struct nfs_read_data *data)
Trond Myklebust0b671302006-11-14 16:12:23 -0500418{
419 struct nfs_readargs *argp = &data->args;
420 struct nfs_readres *resp = &data->res;
421
422 if (resp->eof || resp->count == argp->count)
Trond Myklebustd61e6122009-12-05 19:32:19 -0500423 return;
Trond Myklebust0b671302006-11-14 16:12:23 -0500424
425 /* This is a short read! */
Fred Isamancd841602012-04-20 14:47:44 -0400426 nfs_inc_stats(data->header->inode, NFSIOS_SHORTREAD);
Trond Myklebust0b671302006-11-14 16:12:23 -0500427 /* Has the server at least made some progress? */
428 if (resp->count == 0)
Trond Myklebustd61e6122009-12-05 19:32:19 -0500429 return;
Trond Myklebust0b671302006-11-14 16:12:23 -0500430
431 /* Yes, so retry the read at the end of the data */
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +0000432 data->mds_offset += resp->count;
Trond Myklebust0b671302006-11-14 16:12:23 -0500433 argp->offset += resp->count;
434 argp->pgbase += resp->count;
435 argp->count -= resp->count;
Trond Myklebustd00c5d42011-10-19 12:17:29 -0700436 rpc_restart_call_prepare(task);
Trond Myklebust0b671302006-11-14 16:12:23 -0500437}
438
439/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 * Handle a read reply that fills part of a page.
441 */
Trond Myklebustec06c092006-03-20 13:44:27 -0500442static void nfs_readpage_result_partial(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
Trond Myklebustec06c092006-03-20 13:44:27 -0500444 struct nfs_read_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Trond Myklebustec06c092006-03-20 13:44:27 -0500446 if (nfs_readpage_result(task, data) != 0)
447 return;
Trond Myklebustfdd1e742008-04-15 16:33:58 -0400448 if (task->tk_status < 0)
449 return;
Trond Myklebust0b671302006-11-14 16:12:23 -0500450
Trond Myklebustfdd1e742008-04-15 16:33:58 -0400451 nfs_readpage_truncate_uninitialised_page(data);
452 nfs_readpage_retry(task, data);
453}
454
455static void nfs_readpage_release_partial(void *calldata)
456{
457 struct nfs_read_data *data = calldata;
Fred Isamancd841602012-04-20 14:47:44 -0400458 struct nfs_page *req = data->header->req;
Trond Myklebustfdd1e742008-04-15 16:33:58 -0400459 struct page *page = req->wb_page;
460 int status = data->task.tk_status;
461
462 if (status < 0)
Trond Myklebustfba73002011-10-19 12:17:29 -0700463 set_bit(PG_PARTIAL_READ_FAILED, &req->wb_flags);
Trond Myklebustfdd1e742008-04-15 16:33:58 -0400464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 if (atomic_dec_and_test(&req->wb_complete)) {
Trond Myklebustfba73002011-10-19 12:17:29 -0700466 if (!test_bit(PG_PARTIAL_READ_FAILED, &req->wb_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 SetPageUptodate(page);
468 nfs_readpage_release(req);
469 }
Fred Isamancd841602012-04-20 14:47:44 -0400470 nfs_readdata_release(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471}
472
Andy Adamsonf11c88a2009-04-01 09:22:25 -0400473void nfs_read_prepare(struct rpc_task *task, void *calldata)
474{
475 struct nfs_read_data *data = calldata;
Fred Isamancd841602012-04-20 14:47:44 -0400476 NFS_PROTO(data->header->inode)->read_rpc_prepare(task, data);
Andy Adamsonf11c88a2009-04-01 09:22:25 -0400477}
Andy Adamsonf11c88a2009-04-01 09:22:25 -0400478
Trond Myklebustec06c092006-03-20 13:44:27 -0500479static const struct rpc_call_ops nfs_read_partial_ops = {
Andy Adamsonf11c88a2009-04-01 09:22:25 -0400480 .rpc_call_prepare = nfs_read_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -0500481 .rpc_call_done = nfs_readpage_result_partial,
Trond Myklebustfdd1e742008-04-15 16:33:58 -0400482 .rpc_release = nfs_readpage_release_partial,
Trond Myklebustec06c092006-03-20 13:44:27 -0500483};
484
Trond Myklebust1de3fc12006-05-25 01:40:44 -0400485static void nfs_readpage_set_pages_uptodate(struct nfs_read_data *data)
486{
487 unsigned int count = data->res.count;
488 unsigned int base = data->args.pgbase;
489 struct page **pages;
490
Trond Myklebust79558f32006-08-22 13:44:32 -0400491 if (data->res.eof)
492 count = data->args.count;
Trond Myklebust1de3fc12006-05-25 01:40:44 -0400493 if (unlikely(count == 0))
494 return;
495 pages = &data->args.pages[base >> PAGE_CACHE_SHIFT];
496 base &= ~PAGE_CACHE_MASK;
497 count += base;
498 for (;count >= PAGE_CACHE_SIZE; count -= PAGE_CACHE_SIZE, pages++)
499 SetPageUptodate(*pages);
Trond Myklebust0b671302006-11-14 16:12:23 -0500500 if (count == 0)
501 return;
502 /* Was this a short read? */
503 if (data->res.eof || data->res.count == data->args.count)
Trond Myklebust1de3fc12006-05-25 01:40:44 -0400504 SetPageUptodate(*pages);
505}
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507/*
508 * This is the callback from RPC telling us whether a reply was
509 * received or some error occurred (timeout or socket shutdown).
510 */
Trond Myklebustec06c092006-03-20 13:44:27 -0500511static void nfs_readpage_result_full(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
Trond Myklebustec06c092006-03-20 13:44:27 -0500513 struct nfs_read_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Trond Myklebust0b671302006-11-14 16:12:23 -0500515 if (nfs_readpage_result(task, data) != 0)
516 return;
Trond Myklebustfdd1e742008-04-15 16:33:58 -0400517 if (task->tk_status < 0)
518 return;
Trond Myklebust1de3fc12006-05-25 01:40:44 -0400519 /*
Trond Myklebust0b671302006-11-14 16:12:23 -0500520 * Note: nfs_readpage_retry may change the values of
Trond Myklebust1de3fc12006-05-25 01:40:44 -0400521 * data->args. In the multi-page case, we therefore need
Trond Myklebust0b671302006-11-14 16:12:23 -0500522 * to ensure that we call nfs_readpage_set_pages_uptodate()
523 * first.
Trond Myklebust1de3fc12006-05-25 01:40:44 -0400524 */
Trond Myklebustfdd1e742008-04-15 16:33:58 -0400525 nfs_readpage_truncate_uninitialised_page(data);
526 nfs_readpage_set_pages_uptodate(data);
527 nfs_readpage_retry(task, data);
528}
529
530static void nfs_readpage_release_full(void *calldata)
531{
532 struct nfs_read_data *data = calldata;
Fred Isamancd841602012-04-20 14:47:44 -0400533 struct nfs_pgio_header *hdr = data->header;
Trond Myklebustfdd1e742008-04-15 16:33:58 -0400534
Fred Isamancd841602012-04-20 14:47:44 -0400535 while (!list_empty(&hdr->pages)) {
536 struct nfs_page *req = nfs_list_entry(hdr->pages.next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Trond Myklebust1de3fc12006-05-25 01:40:44 -0400538 nfs_list_remove_request(req);
Trond Myklebust62e4a762011-11-10 14:30:37 -0500539 nfs_readpage_release(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 }
Trond Myklebustfdd1e742008-04-15 16:33:58 -0400541 nfs_readdata_release(calldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542}
543
Trond Myklebustec06c092006-03-20 13:44:27 -0500544static const struct rpc_call_ops nfs_read_full_ops = {
Andy Adamsonf11c88a2009-04-01 09:22:25 -0400545 .rpc_call_prepare = nfs_read_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -0500546 .rpc_call_done = nfs_readpage_result_full,
Trond Myklebustfdd1e742008-04-15 16:33:58 -0400547 .rpc_release = nfs_readpage_release_full,
Trond Myklebustec06c092006-03-20 13:44:27 -0500548};
549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 * Read a page over NFS.
552 * We read the page synchronously in the following case:
553 * - The error flag is set for this page. This happens only when a
554 * previous async read operation failed.
555 */
556int nfs_readpage(struct file *file, struct page *page)
557{
558 struct nfs_open_context *ctx;
559 struct inode *inode = page->mapping->host;
560 int error;
561
562 dprintk("NFS: nfs_readpage (%p %ld@%lu)\n",
563 page, PAGE_CACHE_SIZE, page->index);
Chuck Lever91d5b472006-03-20 13:44:14 -0500564 nfs_inc_stats(inode, NFSIOS_VFSREADPAGE);
565 nfs_add_stats(inode, NFSIOS_READPAGES, 1);
566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 /*
568 * Try to flush any pending writes to the file..
569 *
570 * NOTE! Because we own the page lock, there cannot
571 * be any new pending writes generated at this point
572 * for this page (other pages can be written to).
573 */
574 error = nfs_wb_page(inode, page);
575 if (error)
Trond Myklebustde05a0c2007-05-20 13:05:05 -0400576 goto out_unlock;
577 if (PageUptodate(page))
578 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Trond Myklebust5f004cf2006-09-14 14:03:14 -0400580 error = -ESTALE;
581 if (NFS_STALE(inode))
Trond Myklebustde05a0c2007-05-20 13:05:05 -0400582 goto out_unlock;
Trond Myklebust5f004cf2006-09-14 14:03:14 -0400583
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 if (file == NULL) {
Trond Myklebustcf1308f2006-11-19 16:44:52 -0500585 error = -EBADF;
Trond Myklebustd5308382005-11-04 15:33:38 -0500586 ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 if (ctx == NULL)
Trond Myklebustde05a0c2007-05-20 13:05:05 -0400588 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 } else
Trond Myklebustcd3758e2007-08-10 17:44:32 -0400590 ctx = get_nfs_open_context(nfs_file_open_context(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
David Howells9a9fc1c2009-04-03 16:42:44 +0100592 if (!IS_SYNC(inode)) {
593 error = nfs_readpage_from_fscache(ctx, inode, page);
594 if (error == 0)
595 goto out;
596 }
597
Trond Myklebust8e0969f2006-12-13 15:23:44 -0500598 error = nfs_readpage_async(ctx, inode, page);
599
David Howells9a9fc1c2009-04-03 16:42:44 +0100600out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 put_nfs_open_context(ctx);
602 return error;
Trond Myklebustde05a0c2007-05-20 13:05:05 -0400603out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 unlock_page(page);
605 return error;
606}
607
608struct nfs_readdesc {
Trond Myklebust8b09bee2007-04-02 18:48:28 -0400609 struct nfs_pageio_descriptor *pgio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 struct nfs_open_context *ctx;
611};
612
613static int
614readpage_async_filler(void *data, struct page *page)
615{
616 struct nfs_readdesc *desc = (struct nfs_readdesc *)data;
617 struct inode *inode = page->mapping->host;
618 struct nfs_page *new;
619 unsigned int len;
Trond Myklebustde05a0c2007-05-20 13:05:05 -0400620 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Trond Myklebust49a70f22006-12-05 00:35:38 -0500622 len = nfs_page_length(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 if (len == 0)
624 return nfs_return_empty_page(page);
Trond Myklebustde05a0c2007-05-20 13:05:05 -0400625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 new = nfs_create_request(desc->ctx, inode, page, 0, len);
Trond Myklebustde05a0c2007-05-20 13:05:05 -0400627 if (IS_ERR(new))
628 goto out_error;
629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 if (len < PAGE_CACHE_SIZE)
Christoph Lametereebd2aa2008-02-04 22:28:29 -0800631 zero_user_segment(page, len, PAGE_CACHE_SIZE);
Fred Isamanf8512ad2008-03-19 11:24:39 -0400632 if (!nfs_pageio_add_request(desc->pgio, new)) {
633 error = desc->pgio->pg_error;
634 goto out_unlock;
635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 return 0;
Trond Myklebustde05a0c2007-05-20 13:05:05 -0400637out_error:
638 error = PTR_ERR(new);
Trond Myklebustde05a0c2007-05-20 13:05:05 -0400639out_unlock:
640 unlock_page(page);
641 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642}
643
644int nfs_readpages(struct file *filp, struct address_space *mapping,
645 struct list_head *pages, unsigned nr_pages)
646{
Trond Myklebust8b09bee2007-04-02 18:48:28 -0400647 struct nfs_pageio_descriptor pgio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 struct nfs_readdesc desc = {
Trond Myklebust8b09bee2007-04-02 18:48:28 -0400649 .pgio = &pgio,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 };
651 struct inode *inode = mapping->host;
Trond Myklebust8b09bee2007-04-02 18:48:28 -0400652 unsigned long npages;
Trond Myklebust5f004cf2006-09-14 14:03:14 -0400653 int ret = -ESTALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655 dprintk("NFS: nfs_readpages (%s/%Ld %d)\n",
656 inode->i_sb->s_id,
657 (long long)NFS_FILEID(inode),
658 nr_pages);
Chuck Lever91d5b472006-03-20 13:44:14 -0500659 nfs_inc_stats(inode, NFSIOS_VFSREADPAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Trond Myklebust5f004cf2006-09-14 14:03:14 -0400661 if (NFS_STALE(inode))
662 goto out;
663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 if (filp == NULL) {
Trond Myklebustd5308382005-11-04 15:33:38 -0500665 desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 if (desc.ctx == NULL)
667 return -EBADF;
668 } else
Trond Myklebustcd3758e2007-08-10 17:44:32 -0400669 desc.ctx = get_nfs_open_context(nfs_file_open_context(filp));
David Howells9a9fc1c2009-04-03 16:42:44 +0100670
671 /* attempt to read as many of the pages as possible from the cache
672 * - this returns -ENOBUFS immediately if the cookie is negative
673 */
674 ret = nfs_readpages_from_fscache(desc.ctx, inode, mapping,
675 pages, &nr_pages);
676 if (ret == 0)
677 goto read_complete; /* all pages were read */
678
Trond Myklebust1751c362011-06-10 13:30:23 -0400679 nfs_pageio_init_read(&pgio, inode);
Trond Myklebust8b09bee2007-04-02 18:48:28 -0400680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 ret = read_cache_pages(mapping, pages, readpage_async_filler, &desc);
Trond Myklebust8b09bee2007-04-02 18:48:28 -0400682
683 nfs_pageio_complete(&pgio);
684 npages = (pgio.pg_bytes_written + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
685 nfs_add_stats(inode, NFSIOS_READPAGES, npages);
David Howells9a9fc1c2009-04-03 16:42:44 +0100686read_complete:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 put_nfs_open_context(desc.ctx);
Trond Myklebust5f004cf2006-09-14 14:03:14 -0400688out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 return ret;
690}
691
David Howellsf7b422b2006-06-09 09:34:33 -0400692int __init nfs_init_readpagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
694 nfs_rdata_cachep = kmem_cache_create("nfs_read_data",
Fred Isamancd841602012-04-20 14:47:44 -0400695 sizeof(struct nfs_read_header),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 0, SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +0900697 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 if (nfs_rdata_cachep == NULL)
699 return -ENOMEM;
700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 return 0;
702}
703
David Brownell266bee82006-06-27 12:59:15 -0700704void nfs_destroy_readpagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -0700706 kmem_cache_destroy(nfs_rdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707}