blob: b6bdb18e892c8ea5bf04869e296b1cdafca4d4a7 [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
Andy Adamsonf11c88a2009-04-01 09:22:25 -040023#include "nfs4_fs.h"
Trond Myklebust49a70f22006-12-05 00:35:38 -050024#include "internal.h"
Chuck Lever91d5b472006-03-20 13:44:14 -050025#include "iostat.h"
David Howells9a9fc1c2009-04-03 16:42:44 +010026#include "fscache.h"
Chuck Lever91d5b472006-03-20 13:44:14 -050027
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#define NFSDBG_FACILITY NFSDBG_PAGECACHE
29
Trond Myklebust1751c362011-06-10 13:30:23 -040030static const struct nfs_pageio_ops nfs_pageio_read_ops;
Fred Isaman4db6e0b2012-04-20 14:47:46 -040031static const struct rpc_call_ops nfs_read_common_ops;
Fred Isaman061ae2e2012-04-20 14:47:48 -040032static const struct nfs_pgio_completion_ops nfs_async_read_completion_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Christoph Lametere18b8902006-12-06 20:33:20 -080034static struct kmem_cache *nfs_rdata_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Trond Myklebust1385b812012-05-04 13:54:24 -040036struct nfs_read_header *nfs_readhdr_alloc(void)
Trond Myklebust3feb2d42006-03-20 13:44:37 -050037{
Fred Isaman4db6e0b2012-04-20 14:47:46 -040038 struct nfs_read_header *rhdr;
Trond Myklebust3feb2d42006-03-20 13:44:37 -050039
Fred Isaman4db6e0b2012-04-20 14:47:46 -040040 rhdr = kmem_cache_zalloc(nfs_rdata_cachep, GFP_KERNEL);
41 if (rhdr) {
42 struct nfs_pgio_header *hdr = &rhdr->header;
Fred Isamancd841602012-04-20 14:47:44 -040043
44 INIT_LIST_HEAD(&hdr->pages);
Fred Isaman4db6e0b2012-04-20 14:47:46 -040045 INIT_LIST_HEAD(&hdr->rpc_list);
46 spin_lock_init(&hdr->lock);
47 atomic_set(&hdr->refcnt, 0);
Trond Myklebust3feb2d42006-03-20 13:44:37 -050048 }
Fred Isaman4db6e0b2012-04-20 14:47:46 -040049 return rhdr;
50}
Bryan Schumaker89d77c82012-07-30 16:05:25 -040051EXPORT_SYMBOL_GPL(nfs_readhdr_alloc);
Fred Isaman4db6e0b2012-04-20 14:47:46 -040052
Fred Isaman584aa812012-04-20 14:47:51 -040053static struct nfs_read_data *nfs_readdata_alloc(struct nfs_pgio_header *hdr,
54 unsigned int pagecount)
Fred Isaman4db6e0b2012-04-20 14:47:46 -040055{
56 struct nfs_read_data *data, *prealloc;
57
58 prealloc = &container_of(hdr, struct nfs_read_header, header)->rpc_data;
59 if (prealloc->header == NULL)
60 data = prealloc;
61 else
62 data = kzalloc(sizeof(*data), GFP_KERNEL);
63 if (!data)
64 goto out;
65
66 if (nfs_pgarray_set(&data->pages, pagecount)) {
67 data->header = hdr;
68 atomic_inc(&hdr->refcnt);
69 } else {
70 if (data != prealloc)
71 kfree(data);
72 data = NULL;
73 }
74out:
75 return data;
Trond Myklebust3feb2d42006-03-20 13:44:37 -050076}
77
Fred Isamancd841602012-04-20 14:47:44 -040078void nfs_readhdr_free(struct nfs_pgio_header *hdr)
Trond Myklebust3feb2d42006-03-20 13:44:37 -050079{
Fred Isamancd841602012-04-20 14:47:44 -040080 struct nfs_read_header *rhdr = container_of(hdr, struct nfs_read_header, header);
81
82 kmem_cache_free(nfs_rdata_cachep, rhdr);
Trond Myklebust3feb2d42006-03-20 13:44:37 -050083}
Bryan Schumaker89d77c82012-07-30 16:05:25 -040084EXPORT_SYMBOL_GPL(nfs_readhdr_free);
Trond Myklebust3feb2d42006-03-20 13:44:37 -050085
Trond Myklebust493292d2011-07-13 15:58:28 -040086void nfs_readdata_release(struct nfs_read_data *rdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
Fred Isaman4db6e0b2012-04-20 14:47:46 -040088 struct nfs_pgio_header *hdr = rdata->header;
89 struct nfs_read_header *read_header = container_of(hdr, struct nfs_read_header, header);
90
Trond Myklebust383ba712008-02-19 20:04:20 -050091 put_nfs_open_context(rdata->args.context);
Fred Isaman30dd3742012-04-20 14:47:45 -040092 if (rdata->pages.pagevec != rdata->pages.page_array)
93 kfree(rdata->pages.pagevec);
Fred Isaman4db6e0b2012-04-20 14:47:46 -040094 if (rdata != &read_header->rpc_data)
95 kfree(rdata);
96 else
97 rdata->header = NULL;
98 if (atomic_dec_and_test(&hdr->refcnt))
Fred Isaman061ae2e2012-04-20 14:47:48 -040099 hdr->completion_ops->completion(hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
Bryan Schumaker89d77c82012-07-30 16:05:25 -0400101EXPORT_SYMBOL_GPL(nfs_readdata_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103static
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104int nfs_return_empty_page(struct page *page)
105{
Christoph Lametereebd2aa2008-02-04 22:28:29 -0800106 zero_user(page, 0, PAGE_CACHE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 SetPageUptodate(page);
108 unlock_page(page);
109 return 0;
110}
111
Bryan Schumaker1abb50882012-06-20 15:53:47 -0400112void nfs_pageio_init_read(struct nfs_pageio_descriptor *pgio,
Fred Isaman061ae2e2012-04-20 14:47:48 -0400113 struct inode *inode,
114 const struct nfs_pgio_completion_ops *compl_ops)
Trond Myklebust1751c362011-06-10 13:30:23 -0400115{
Fred Isaman061ae2e2012-04-20 14:47:48 -0400116 nfs_pageio_init(pgio, inode, &nfs_pageio_read_ops, compl_ops,
Trond Myklebust1751c362011-06-10 13:30:23 -0400117 NFS_SERVER(inode)->rsize, 0);
118}
Bryan Schumakerddda8e02012-07-30 16:05:23 -0400119EXPORT_SYMBOL_GPL(nfs_pageio_init_read);
Trond Myklebust1751c362011-06-10 13:30:23 -0400120
Trond Myklebust493292d2011-07-13 15:58:28 -0400121void nfs_pageio_reset_read_mds(struct nfs_pageio_descriptor *pgio)
122{
123 pgio->pg_ops = &nfs_pageio_read_ops;
124 pgio->pg_bsize = NFS_SERVER(pgio->pg_inode)->rsize;
125}
Trond Myklebust1f945352011-07-13 15:59:57 -0400126EXPORT_SYMBOL_GPL(nfs_pageio_reset_read_mds);
Trond Myklebust493292d2011-07-13 15:58:28 -0400127
David Howellsf42b2932009-04-03 16:42:44 +0100128int nfs_readpage_async(struct nfs_open_context *ctx, struct inode *inode,
129 struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 struct nfs_page *new;
132 unsigned int len;
Fred Isamanc76069b2011-03-03 15:13:48 +0000133 struct nfs_pageio_descriptor pgio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Trond Myklebust49a70f22006-12-05 00:35:38 -0500135 len = nfs_page_length(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 if (len == 0)
137 return nfs_return_empty_page(page);
138 new = nfs_create_request(ctx, inode, page, 0, len);
139 if (IS_ERR(new)) {
140 unlock_page(page);
141 return PTR_ERR(new);
142 }
143 if (len < PAGE_CACHE_SIZE)
Christoph Lametereebd2aa2008-02-04 22:28:29 -0800144 zero_user_segment(page, len, PAGE_CACHE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Bryan Schumaker1abb50882012-06-20 15:53:47 -0400146 NFS_PROTO(inode)->read_pageio_init(&pgio, inode, &nfs_async_read_completion_ops);
Trond Myklebustd8007d42011-06-10 13:30:23 -0400147 nfs_pageio_add_request(&pgio, new);
Trond Myklebust1751c362011-06-10 13:30:23 -0400148 nfs_pageio_complete(&pgio);
Andy Adamson2701d082012-05-24 13:13:24 -0400149 NFS_I(inode)->read_io += pgio.pg_bytes_written;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 return 0;
151}
152
153static void nfs_readpage_release(struct nfs_page *req)
154{
Al Viro3d4ff432011-06-22 18:40:12 -0400155 struct inode *d_inode = req->wb_context->dentry->d_inode;
David Howells7f8e05f2009-04-03 16:42:45 +0100156
157 if (PageUptodate(req->wb_page))
158 nfs_readpage_to_fscache(d_inode, req->wb_page, 0);
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 unlock_page(req->wb_page);
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 dprintk("NFS: read done (%s/%Ld %d@%Ld)\n",
Al Viro3d4ff432011-06-22 18:40:12 -0400163 req->wb_context->dentry->d_inode->i_sb->s_id,
164 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 req->wb_bytes,
166 (long long)req_offset(req));
Nick Wilson10d2c462005-09-22 21:44:28 -0700167 nfs_release_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168}
169
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400170/* Note io was page aligned */
Fred Isaman061ae2e2012-04-20 14:47:48 -0400171static void nfs_read_completion(struct nfs_pgio_header *hdr)
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400172{
173 unsigned long bytes = 0;
174
175 if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
176 goto out;
Trond Myklebust4bd8b012012-05-01 12:49:58 -0400177 while (!list_empty(&hdr->pages)) {
178 struct nfs_page *req = nfs_list_entry(hdr->pages.next);
179 struct page *page = req->wb_page;
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400180
Trond Myklebust4bd8b012012-05-01 12:49:58 -0400181 if (test_bit(NFS_IOHDR_EOF, &hdr->flags)) {
182 if (bytes > hdr->good_bytes)
183 zero_user(page, 0, PAGE_SIZE);
184 else if (hdr->good_bytes - bytes < PAGE_SIZE)
185 zero_user_segment(page,
186 hdr->good_bytes & ~PAGE_MASK,
187 PAGE_SIZE);
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400188 }
Trond Myklebust4bd8b012012-05-01 12:49:58 -0400189 bytes += req->wb_bytes;
190 if (test_bit(NFS_IOHDR_ERROR, &hdr->flags)) {
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400191 if (bytes <= hdr->good_bytes)
Trond Myklebust4bd8b012012-05-01 12:49:58 -0400192 SetPageUptodate(page);
193 } else
194 SetPageUptodate(page);
195 nfs_list_remove_request(req);
196 nfs_readpage_release(req);
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400197 }
198out:
199 hdr->release(hdr);
200}
201
Fred Isamanc5996c42012-04-20 14:47:41 -0400202int nfs_initiate_read(struct rpc_clnt *clnt,
203 struct nfs_read_data *data,
Andy Adamson9f0ec1762012-04-27 17:53:44 -0400204 const struct rpc_call_ops *call_ops, int flags)
Andy Adamson64419a92011-03-01 01:34:16 +0000205{
Fred Isamancd841602012-04-20 14:47:44 -0400206 struct inode *inode = data->header->inode;
Andy Adamson64419a92011-03-01 01:34:16 +0000207 int swap_flags = IS_SWAPFILE(inode) ? NFS_RPC_SWAPFLAGS : 0;
208 struct rpc_task *task;
209 struct rpc_message msg = {
210 .rpc_argp = &data->args,
211 .rpc_resp = &data->res,
Fred Isamancd841602012-04-20 14:47:44 -0400212 .rpc_cred = data->header->cred,
Andy Adamson64419a92011-03-01 01:34:16 +0000213 };
214 struct rpc_task_setup task_setup_data = {
215 .task = &data->task,
216 .rpc_client = clnt,
217 .rpc_message = &msg,
218 .callback_ops = call_ops,
219 .callback_data = data,
220 .workqueue = nfsiod_workqueue,
Andy Adamson9f0ec1762012-04-27 17:53:44 -0400221 .flags = RPC_TASK_ASYNC | swap_flags | flags,
Andy Adamson64419a92011-03-01 01:34:16 +0000222 };
223
224 /* Set up the initial task struct. */
225 NFS_PROTO(inode)->read_setup(data, &msg);
226
227 dprintk("NFS: %5u initiated read call (req %s/%lld, %u bytes @ "
228 "offset %llu)\n",
229 data->task.tk_pid,
230 inode->i_sb->s_id,
231 (long long)NFS_FILEID(inode),
232 data->args.count,
233 (unsigned long long)data->args.offset);
234
235 task = rpc_run_task(&task_setup_data);
236 if (IS_ERR(task))
237 return PTR_ERR(task);
238 rpc_put_task(task);
239 return 0;
240}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000241EXPORT_SYMBOL_GPL(nfs_initiate_read);
Andy Adamson64419a92011-03-01 01:34:16 +0000242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243/*
244 * Set up the NFS read request struct
245 */
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400246static void nfs_read_rpcsetup(struct nfs_read_data *data,
Trond Myklebust6e4efd52011-07-12 13:42:02 -0400247 unsigned int count, unsigned int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400249 struct nfs_page *req = data->header->req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400251 data->args.fh = NFS_FH(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 data->args.offset = req_offset(req) + offset;
253 data->args.pgbase = req->wb_pgbase + offset;
Fred Isaman30dd3742012-04-20 14:47:45 -0400254 data->args.pages = data->pages.pagevec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 data->args.count = count;
Trond Myklebust383ba712008-02-19 20:04:20 -0500256 data->args.context = get_nfs_open_context(req->wb_context);
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400257 data->args.lock_context = req->wb_lock_context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 data->res.fattr = &data->fattr;
260 data->res.count = count;
261 data->res.eof = 0;
Trond Myklebust0e574af2005-10-27 22:12:38 -0400262 nfs_fattr_init(&data->fattr);
Trond Myklebust6e4efd52011-07-12 13:42:02 -0400263}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Trond Myklebust6e4efd52011-07-12 13:42:02 -0400265static int nfs_do_read(struct nfs_read_data *data,
Trond Myklebust493292d2011-07-13 15:58:28 -0400266 const struct rpc_call_ops *call_ops)
Trond Myklebust6e4efd52011-07-12 13:42:02 -0400267{
Fred Isamancd841602012-04-20 14:47:44 -0400268 struct inode *inode = data->header->inode;
Trond Myklebust6e4efd52011-07-12 13:42:02 -0400269
Andy Adamson9f0ec1762012-04-27 17:53:44 -0400270 return nfs_initiate_read(NFS_CLIENT(inode), data, call_ops, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271}
272
Trond Myklebust275acaa2011-07-12 13:42:02 -0400273static int
274nfs_do_multiple_reads(struct list_head *head,
Trond Myklebust493292d2011-07-13 15:58:28 -0400275 const struct rpc_call_ops *call_ops)
Trond Myklebust275acaa2011-07-12 13:42:02 -0400276{
277 struct nfs_read_data *data;
278 int ret = 0;
279
280 while (!list_empty(head)) {
281 int ret2;
282
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400283 data = list_first_entry(head, struct nfs_read_data, list);
Trond Myklebust275acaa2011-07-12 13:42:02 -0400284 list_del_init(&data->list);
285
Trond Myklebust493292d2011-07-13 15:58:28 -0400286 ret2 = nfs_do_read(data, call_ops);
Trond Myklebust275acaa2011-07-12 13:42:02 -0400287 if (ret == 0)
288 ret = ret2;
289 }
290 return ret;
291}
292
Fred Isaman061ae2e2012-04-20 14:47:48 -0400293static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294nfs_async_read_error(struct list_head *head)
295{
296 struct nfs_page *req;
297
298 while (!list_empty(head)) {
299 req = nfs_list_entry(head->next);
300 nfs_list_remove_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 nfs_readpage_release(req);
302 }
303}
304
Fred Isaman061ae2e2012-04-20 14:47:48 -0400305static const struct nfs_pgio_completion_ops nfs_async_read_completion_ops = {
306 .error_cleanup = nfs_async_read_error,
307 .completion = nfs_read_completion,
308};
309
Trond Myklebust25b11dc2012-05-01 12:07:22 -0400310static void nfs_pagein_error(struct nfs_pageio_descriptor *desc,
311 struct nfs_pgio_header *hdr)
312{
313 set_bit(NFS_IOHDR_REDO, &hdr->flags);
314 while (!list_empty(&hdr->rpc_list)) {
315 struct nfs_read_data *data = list_first_entry(&hdr->rpc_list,
316 struct nfs_read_data, list);
317 list_del(&data->list);
318 nfs_readdata_release(data);
319 }
320 desc->pg_completion_ops->error_cleanup(&desc->pg_list);
321}
322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 * Generate multiple requests to fill a single page.
325 *
326 * We optimize to reduce the number of read operations on the wire. If we
327 * detect that we're reading a page, or an area of a page, that is past the
328 * end of file, we do not generate NFS read operations but just clear the
329 * parts of the page that would have come back zero from the server anyway.
330 *
331 * We rely on the cached value of i_size to make this determination; another
332 * client can fill pages on the server past our cached end-of-file, but we
333 * won't see the new data until our attribute cache is updated. This is more
334 * or less conventional NFS client behavior.
335 */
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400336static int nfs_pagein_multi(struct nfs_pageio_descriptor *desc,
337 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400339 struct nfs_page *req = hdr->req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 struct page *page = req->wb_page;
341 struct nfs_read_data *data;
Trond Myklebustd0979712011-07-12 13:42:02 -0400342 size_t rsize = desc->pg_bsize, nbytes;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700343 unsigned int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Trond Myklebust275acaa2011-07-12 13:42:02 -0400345 offset = 0;
Fred Isamanc76069b2011-03-03 15:13:48 +0000346 nbytes = desc->pg_count;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700347 do {
348 size_t len = min(nbytes,rsize);
349
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400350 data = nfs_readdata_alloc(hdr, 1);
Trond Myklebust25b11dc2012-05-01 12:07:22 -0400351 if (!data) {
352 nfs_pagein_error(desc, hdr);
353 return -ENOMEM;
354 }
Fred Isaman30dd3742012-04-20 14:47:45 -0400355 data->pages.pagevec[0] = page;
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400356 nfs_read_rpcsetup(data, len, offset);
357 list_add(&data->list, &hdr->rpc_list);
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700358 nbytes -= len;
Trond Myklebust275acaa2011-07-12 13:42:02 -0400359 offset += len;
Trond Myklebust9146ab52012-05-01 11:21:43 -0400360 } while (nbytes != 0);
Trond Myklebust25b11dc2012-05-01 12:07:22 -0400361
362 nfs_list_remove_request(req);
363 nfs_list_add_request(req, &hdr->pages);
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400364 desc->pg_rpc_callops = &nfs_read_common_ops;
Trond Myklebust9146ab52012-05-01 11:21:43 -0400365 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366}
367
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400368static int nfs_pagein_one(struct nfs_pageio_descriptor *desc,
369 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
371 struct nfs_page *req;
372 struct page **pages;
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400373 struct nfs_read_data *data;
Fred Isamanc76069b2011-03-03 15:13:48 +0000374 struct list_head *head = &desc->pg_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400376 data = nfs_readdata_alloc(hdr, nfs_page_array_len(desc->pg_base,
377 desc->pg_count));
378 if (!data) {
Trond Myklebust25b11dc2012-05-01 12:07:22 -0400379 nfs_pagein_error(desc, hdr);
Trond Myklebust9146ab52012-05-01 11:21:43 -0400380 return -ENOMEM;
Fred Isamanbae724e2011-03-01 01:34:15 +0000381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Fred Isaman30dd3742012-04-20 14:47:45 -0400383 pages = data->pages.pagevec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 while (!list_empty(head)) {
385 req = nfs_list_entry(head->next);
386 nfs_list_remove_request(req);
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400387 nfs_list_add_request(req, &hdr->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 *pages++ = req->wb_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400391 nfs_read_rpcsetup(data, desc->pg_count, 0);
392 list_add(&data->list, &hdr->rpc_list);
393 desc->pg_rpc_callops = &nfs_read_common_ops;
Trond Myklebust9146ab52012-05-01 11:21:43 -0400394 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395}
396
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400397int nfs_generic_pagein(struct nfs_pageio_descriptor *desc,
398 struct nfs_pgio_header *hdr)
Trond Myklebust493292d2011-07-13 15:58:28 -0400399{
400 if (desc->pg_bsize < PAGE_CACHE_SIZE)
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400401 return nfs_pagein_multi(desc, hdr);
402 return nfs_pagein_one(desc, hdr);
Trond Myklebust493292d2011-07-13 15:58:28 -0400403}
Bryan Schumaker89d77c82012-07-30 16:05:25 -0400404EXPORT_SYMBOL_GPL(nfs_generic_pagein);
Trond Myklebust493292d2011-07-13 15:58:28 -0400405
406static int nfs_generic_pg_readpages(struct nfs_pageio_descriptor *desc)
Trond Myklebust1751c362011-06-10 13:30:23 -0400407{
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400408 struct nfs_read_header *rhdr;
409 struct nfs_pgio_header *hdr;
Trond Myklebust275acaa2011-07-12 13:42:02 -0400410 int ret;
411
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400412 rhdr = nfs_readhdr_alloc();
413 if (!rhdr) {
Fred Isaman061ae2e2012-04-20 14:47:48 -0400414 desc->pg_completion_ops->error_cleanup(&desc->pg_list);
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400415 return -ENOMEM;
416 }
417 hdr = &rhdr->header;
418 nfs_pgheader_init(desc, hdr, nfs_readhdr_free);
419 atomic_inc(&hdr->refcnt);
420 ret = nfs_generic_pagein(desc, hdr);
Trond Myklebust50828d72011-07-12 13:42:02 -0400421 if (ret == 0)
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400422 ret = nfs_do_multiple_reads(&hdr->rpc_list,
423 desc->pg_rpc_callops);
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400424 if (atomic_dec_and_test(&hdr->refcnt))
Fred Isaman061ae2e2012-04-20 14:47:48 -0400425 hdr->completion_ops->completion(hdr);
Trond Myklebust275acaa2011-07-12 13:42:02 -0400426 return ret;
Trond Myklebust1751c362011-06-10 13:30:23 -0400427}
Trond Myklebust1751c362011-06-10 13:30:23 -0400428
429static const struct nfs_pageio_ops nfs_pageio_read_ops = {
430 .pg_test = nfs_generic_pg_test,
431 .pg_doio = nfs_generic_pg_readpages,
432};
433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434/*
Trond Myklebust0b671302006-11-14 16:12:23 -0500435 * This is the callback from RPC telling us whether a reply was
436 * received or some error occurred (timeout or socket shutdown).
437 */
438int nfs_readpage_result(struct rpc_task *task, struct nfs_read_data *data)
439{
Fred Isamancd841602012-04-20 14:47:44 -0400440 struct inode *inode = data->header->inode;
Trond Myklebust0b671302006-11-14 16:12:23 -0500441 int status;
442
Harvey Harrison3110ff82008-05-02 13:42:44 -0700443 dprintk("NFS: %s: %5u, (status %d)\n", __func__, task->tk_pid,
Trond Myklebust0b671302006-11-14 16:12:23 -0500444 task->tk_status);
445
Fred Isamancd841602012-04-20 14:47:44 -0400446 status = NFS_PROTO(inode)->read_done(task, data);
Trond Myklebust0b671302006-11-14 16:12:23 -0500447 if (status != 0)
448 return status;
449
Fred Isamancd841602012-04-20 14:47:44 -0400450 nfs_add_stats(inode, NFSIOS_SERVERREADBYTES, data->res.count);
Trond Myklebust0b671302006-11-14 16:12:23 -0500451
452 if (task->tk_status == -ESTALE) {
Fred Isamancd841602012-04-20 14:47:44 -0400453 set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
454 nfs_mark_for_revalidate(inode);
Trond Myklebust0b671302006-11-14 16:12:23 -0500455 }
Trond Myklebust0b671302006-11-14 16:12:23 -0500456 return 0;
457}
458
Trond Myklebustfdd1e742008-04-15 16:33:58 -0400459static void nfs_readpage_retry(struct rpc_task *task, struct nfs_read_data *data)
Trond Myklebust0b671302006-11-14 16:12:23 -0500460{
461 struct nfs_readargs *argp = &data->args;
462 struct nfs_readres *resp = &data->res;
463
Trond Myklebust0b671302006-11-14 16:12:23 -0500464 /* This is a short read! */
Fred Isamancd841602012-04-20 14:47:44 -0400465 nfs_inc_stats(data->header->inode, NFSIOS_SHORTREAD);
Trond Myklebust0b671302006-11-14 16:12:23 -0500466 /* Has the server at least made some progress? */
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400467 if (resp->count == 0) {
468 nfs_set_pgio_error(data->header, -EIO, argp->offset);
Trond Myklebustd61e6122009-12-05 19:32:19 -0500469 return;
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400470 }
Trond Myklebust0b671302006-11-14 16:12:23 -0500471 /* Yes, so retry the read at the end of the data */
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +0000472 data->mds_offset += resp->count;
Trond Myklebust0b671302006-11-14 16:12:23 -0500473 argp->offset += resp->count;
474 argp->pgbase += resp->count;
475 argp->count -= resp->count;
Trond Myklebustd00c5d42011-10-19 12:17:29 -0700476 rpc_restart_call_prepare(task);
Trond Myklebust0b671302006-11-14 16:12:23 -0500477}
478
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400479static void nfs_readpage_result_common(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
Trond Myklebustec06c092006-03-20 13:44:27 -0500481 struct nfs_read_data *data = calldata;
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400482 struct nfs_pgio_header *hdr = data->header;
483
484 /* Note the only returns of nfs_readpage_result are 0 and -EAGAIN */
Trond Myklebustec06c092006-03-20 13:44:27 -0500485 if (nfs_readpage_result(task, data) != 0)
486 return;
Trond Myklebustfdd1e742008-04-15 16:33:58 -0400487 if (task->tk_status < 0)
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400488 nfs_set_pgio_error(hdr, task->tk_status, data->args.offset);
489 else if (data->res.eof) {
490 loff_t bound;
Trond Myklebust0b671302006-11-14 16:12:23 -0500491
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400492 bound = data->args.offset + data->res.count;
493 spin_lock(&hdr->lock);
494 if (bound < hdr->io_start + hdr->good_bytes) {
495 set_bit(NFS_IOHDR_EOF, &hdr->flags);
496 clear_bit(NFS_IOHDR_ERROR, &hdr->flags);
497 hdr->good_bytes = bound - hdr->io_start;
498 }
499 spin_unlock(&hdr->lock);
500 } else if (data->res.count != data->args.count)
501 nfs_readpage_retry(task, data);
Trond Myklebustfdd1e742008-04-15 16:33:58 -0400502}
503
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400504static void nfs_readpage_release_common(void *calldata)
Trond Myklebustfdd1e742008-04-15 16:33:58 -0400505{
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400506 nfs_readdata_release(calldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507}
508
Andy Adamsonf11c88a2009-04-01 09:22:25 -0400509void nfs_read_prepare(struct rpc_task *task, void *calldata)
510{
511 struct nfs_read_data *data = calldata;
Fred Isamancd841602012-04-20 14:47:44 -0400512 NFS_PROTO(data->header->inode)->read_rpc_prepare(task, data);
Andy Adamsonf11c88a2009-04-01 09:22:25 -0400513}
Andy Adamsonf11c88a2009-04-01 09:22:25 -0400514
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400515static const struct rpc_call_ops nfs_read_common_ops = {
Andy Adamsonf11c88a2009-04-01 09:22:25 -0400516 .rpc_call_prepare = nfs_read_prepare,
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400517 .rpc_call_done = nfs_readpage_result_common,
518 .rpc_release = nfs_readpage_release_common,
Trond Myklebustec06c092006-03-20 13:44:27 -0500519};
520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 * Read a page over NFS.
523 * We read the page synchronously in the following case:
524 * - The error flag is set for this page. This happens only when a
525 * previous async read operation failed.
526 */
527int nfs_readpage(struct file *file, struct page *page)
528{
529 struct nfs_open_context *ctx;
Mel Gormand56b4dd2012-07-31 16:45:06 -0700530 struct inode *inode = page_file_mapping(page)->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 int error;
532
533 dprintk("NFS: nfs_readpage (%p %ld@%lu)\n",
Mel Gormand56b4dd2012-07-31 16:45:06 -0700534 page, PAGE_CACHE_SIZE, page_file_index(page));
Chuck Lever91d5b472006-03-20 13:44:14 -0500535 nfs_inc_stats(inode, NFSIOS_VFSREADPAGE);
536 nfs_add_stats(inode, NFSIOS_READPAGES, 1);
537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 /*
539 * Try to flush any pending writes to the file..
540 *
541 * NOTE! Because we own the page lock, there cannot
542 * be any new pending writes generated at this point
543 * for this page (other pages can be written to).
544 */
545 error = nfs_wb_page(inode, page);
546 if (error)
Trond Myklebustde05a0c2007-05-20 13:05:05 -0400547 goto out_unlock;
548 if (PageUptodate(page))
549 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Trond Myklebust5f004cf2006-09-14 14:03:14 -0400551 error = -ESTALE;
552 if (NFS_STALE(inode))
Trond Myklebustde05a0c2007-05-20 13:05:05 -0400553 goto out_unlock;
Trond Myklebust5f004cf2006-09-14 14:03:14 -0400554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 if (file == NULL) {
Trond Myklebustcf1308f2006-11-19 16:44:52 -0500556 error = -EBADF;
Trond Myklebustd5308382005-11-04 15:33:38 -0500557 ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 if (ctx == NULL)
Trond Myklebustde05a0c2007-05-20 13:05:05 -0400559 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 } else
Trond Myklebustcd3758e2007-08-10 17:44:32 -0400561 ctx = get_nfs_open_context(nfs_file_open_context(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
David Howells9a9fc1c2009-04-03 16:42:44 +0100563 if (!IS_SYNC(inode)) {
564 error = nfs_readpage_from_fscache(ctx, inode, page);
565 if (error == 0)
566 goto out;
567 }
568
Trond Myklebust8e0969f2006-12-13 15:23:44 -0500569 error = nfs_readpage_async(ctx, inode, page);
570
David Howells9a9fc1c2009-04-03 16:42:44 +0100571out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 put_nfs_open_context(ctx);
573 return error;
Trond Myklebustde05a0c2007-05-20 13:05:05 -0400574out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 unlock_page(page);
576 return error;
577}
578
579struct nfs_readdesc {
Trond Myklebust8b09bee2007-04-02 18:48:28 -0400580 struct nfs_pageio_descriptor *pgio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 struct nfs_open_context *ctx;
582};
583
584static int
585readpage_async_filler(void *data, struct page *page)
586{
587 struct nfs_readdesc *desc = (struct nfs_readdesc *)data;
Mel Gormand56b4dd2012-07-31 16:45:06 -0700588 struct inode *inode = page_file_mapping(page)->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 struct nfs_page *new;
590 unsigned int len;
Trond Myklebustde05a0c2007-05-20 13:05:05 -0400591 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Trond Myklebust49a70f22006-12-05 00:35:38 -0500593 len = nfs_page_length(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 if (len == 0)
595 return nfs_return_empty_page(page);
Trond Myklebustde05a0c2007-05-20 13:05:05 -0400596
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 new = nfs_create_request(desc->ctx, inode, page, 0, len);
Trond Myklebustde05a0c2007-05-20 13:05:05 -0400598 if (IS_ERR(new))
599 goto out_error;
600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 if (len < PAGE_CACHE_SIZE)
Christoph Lametereebd2aa2008-02-04 22:28:29 -0800602 zero_user_segment(page, len, PAGE_CACHE_SIZE);
Fred Isamanf8512ad2008-03-19 11:24:39 -0400603 if (!nfs_pageio_add_request(desc->pgio, new)) {
604 error = desc->pgio->pg_error;
605 goto out_unlock;
606 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 return 0;
Trond Myklebustde05a0c2007-05-20 13:05:05 -0400608out_error:
609 error = PTR_ERR(new);
Trond Myklebustde05a0c2007-05-20 13:05:05 -0400610out_unlock:
611 unlock_page(page);
612 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613}
614
615int nfs_readpages(struct file *filp, struct address_space *mapping,
616 struct list_head *pages, unsigned nr_pages)
617{
Trond Myklebust8b09bee2007-04-02 18:48:28 -0400618 struct nfs_pageio_descriptor pgio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 struct nfs_readdesc desc = {
Trond Myklebust8b09bee2007-04-02 18:48:28 -0400620 .pgio = &pgio,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 };
622 struct inode *inode = mapping->host;
Trond Myklebust8b09bee2007-04-02 18:48:28 -0400623 unsigned long npages;
Trond Myklebust5f004cf2006-09-14 14:03:14 -0400624 int ret = -ESTALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
626 dprintk("NFS: nfs_readpages (%s/%Ld %d)\n",
627 inode->i_sb->s_id,
628 (long long)NFS_FILEID(inode),
629 nr_pages);
Chuck Lever91d5b472006-03-20 13:44:14 -0500630 nfs_inc_stats(inode, NFSIOS_VFSREADPAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Trond Myklebust5f004cf2006-09-14 14:03:14 -0400632 if (NFS_STALE(inode))
633 goto out;
634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 if (filp == NULL) {
Trond Myklebustd5308382005-11-04 15:33:38 -0500636 desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 if (desc.ctx == NULL)
638 return -EBADF;
639 } else
Trond Myklebustcd3758e2007-08-10 17:44:32 -0400640 desc.ctx = get_nfs_open_context(nfs_file_open_context(filp));
David Howells9a9fc1c2009-04-03 16:42:44 +0100641
642 /* attempt to read as many of the pages as possible from the cache
643 * - this returns -ENOBUFS immediately if the cookie is negative
644 */
645 ret = nfs_readpages_from_fscache(desc.ctx, inode, mapping,
646 pages, &nr_pages);
647 if (ret == 0)
648 goto read_complete; /* all pages were read */
649
Bryan Schumaker1abb50882012-06-20 15:53:47 -0400650 NFS_PROTO(inode)->read_pageio_init(&pgio, inode, &nfs_async_read_completion_ops);
Trond Myklebust8b09bee2007-04-02 18:48:28 -0400651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 ret = read_cache_pages(mapping, pages, readpage_async_filler, &desc);
Trond Myklebust8b09bee2007-04-02 18:48:28 -0400653
654 nfs_pageio_complete(&pgio);
Andy Adamson2701d082012-05-24 13:13:24 -0400655 NFS_I(inode)->read_io += pgio.pg_bytes_written;
Trond Myklebust8b09bee2007-04-02 18:48:28 -0400656 npages = (pgio.pg_bytes_written + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
657 nfs_add_stats(inode, NFSIOS_READPAGES, npages);
David Howells9a9fc1c2009-04-03 16:42:44 +0100658read_complete:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 put_nfs_open_context(desc.ctx);
Trond Myklebust5f004cf2006-09-14 14:03:14 -0400660out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 return ret;
662}
663
David Howellsf7b422b2006-06-09 09:34:33 -0400664int __init nfs_init_readpagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
666 nfs_rdata_cachep = kmem_cache_create("nfs_read_data",
Fred Isamancd841602012-04-20 14:47:44 -0400667 sizeof(struct nfs_read_header),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 0, SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +0900669 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 if (nfs_rdata_cachep == NULL)
671 return -ENOMEM;
672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 return 0;
674}
675
David Brownell266bee82006-06-27 12:59:15 -0700676void nfs_destroy_readpagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -0700678 kmem_cache_destroy(nfs_rdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679}