blob: ab4c1a5b5fbd238dc0b3715605d88e639e88ca28 [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"
Christoph Hellwigfab5fc22014-04-16 15:07:22 +020027#include "pnfs.h"
Chuck Lever91d5b472006-03-20 13:44:14 -050028
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#define NFSDBG_FACILITY NFSDBG_PAGECACHE
30
Trond Myklebust1751c362011-06-10 13:30:23 -040031static const struct nfs_pageio_ops nfs_pageio_read_ops;
Fred Isaman4db6e0b2012-04-20 14:47:46 -040032static const struct rpc_call_ops nfs_read_common_ops;
Fred Isaman061ae2e2012-04-20 14:47:48 -040033static const struct nfs_pgio_completion_ops nfs_async_read_completion_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Christoph Lametere18b8902006-12-06 20:33:20 -080035static struct kmem_cache *nfs_rdata_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Anna Schumakerc0752cd2014-05-06 09:12:27 -040037struct nfs_rw_header *nfs_readhdr_alloc(void)
Trond Myklebust3feb2d42006-03-20 13:44:37 -050038{
Anna Schumakerc0752cd2014-05-06 09:12:27 -040039 struct nfs_rw_header *rhdr;
Trond Myklebust3feb2d42006-03-20 13:44:37 -050040
Fred Isaman4db6e0b2012-04-20 14:47:46 -040041 rhdr = kmem_cache_zalloc(nfs_rdata_cachep, GFP_KERNEL);
42 if (rhdr) {
43 struct nfs_pgio_header *hdr = &rhdr->header;
Fred Isamancd841602012-04-20 14:47:44 -040044
45 INIT_LIST_HEAD(&hdr->pages);
Fred Isaman4db6e0b2012-04-20 14:47:46 -040046 INIT_LIST_HEAD(&hdr->rpc_list);
47 spin_lock_init(&hdr->lock);
48 atomic_set(&hdr->refcnt, 0);
Trond Myklebust3feb2d42006-03-20 13:44:37 -050049 }
Fred Isaman4db6e0b2012-04-20 14:47:46 -040050 return rhdr;
51}
Bryan Schumaker89d77c82012-07-30 16:05:25 -040052EXPORT_SYMBOL_GPL(nfs_readhdr_alloc);
Fred Isaman4db6e0b2012-04-20 14:47:46 -040053
Fred Isamancd841602012-04-20 14:47:44 -040054void nfs_readhdr_free(struct nfs_pgio_header *hdr)
Trond Myklebust3feb2d42006-03-20 13:44:37 -050055{
Anna Schumakerc0752cd2014-05-06 09:12:27 -040056 struct nfs_rw_header *rhdr = container_of(hdr, struct nfs_rw_header, header);
Fred Isamancd841602012-04-20 14:47:44 -040057
58 kmem_cache_free(nfs_rdata_cachep, rhdr);
Trond Myklebust3feb2d42006-03-20 13:44:37 -050059}
Bryan Schumaker89d77c82012-07-30 16:05:25 -040060EXPORT_SYMBOL_GPL(nfs_readhdr_free);
Trond Myklebust3feb2d42006-03-20 13:44:37 -050061
Linus Torvalds1da177e2005-04-16 15:20:36 -070062static
Linus Torvalds1da177e2005-04-16 15:20:36 -070063int nfs_return_empty_page(struct page *page)
64{
Christoph Lametereebd2aa2008-02-04 22:28:29 -080065 zero_user(page, 0, PAGE_CACHE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 SetPageUptodate(page);
67 unlock_page(page);
68 return 0;
69}
70
Bryan Schumaker1abb50882012-06-20 15:53:47 -040071void nfs_pageio_init_read(struct nfs_pageio_descriptor *pgio,
Christoph Hellwigfab5fc22014-04-16 15:07:22 +020072 struct inode *inode, bool force_mds,
Fred Isaman061ae2e2012-04-20 14:47:48 -040073 const struct nfs_pgio_completion_ops *compl_ops)
Trond Myklebust1751c362011-06-10 13:30:23 -040074{
Christoph Hellwigfab5fc22014-04-16 15:07:22 +020075 struct nfs_server *server = NFS_SERVER(inode);
76 const struct nfs_pageio_ops *pg_ops = &nfs_pageio_read_ops;
77
78#ifdef CONFIG_NFS_V4_1
79 if (server->pnfs_curr_ld && !force_mds)
80 pg_ops = server->pnfs_curr_ld->pg_read_ops;
81#endif
82 nfs_pageio_init(pgio, inode, pg_ops, compl_ops, server->rsize, 0);
Trond Myklebust1751c362011-06-10 13:30:23 -040083}
Bryan Schumakerddda8e02012-07-30 16:05:23 -040084EXPORT_SYMBOL_GPL(nfs_pageio_init_read);
Trond Myklebust1751c362011-06-10 13:30:23 -040085
Trond Myklebust493292d2011-07-13 15:58:28 -040086void nfs_pageio_reset_read_mds(struct nfs_pageio_descriptor *pgio)
87{
88 pgio->pg_ops = &nfs_pageio_read_ops;
89 pgio->pg_bsize = NFS_SERVER(pgio->pg_inode)->rsize;
90}
Trond Myklebust1f945352011-07-13 15:59:57 -040091EXPORT_SYMBOL_GPL(nfs_pageio_reset_read_mds);
Trond Myklebust493292d2011-07-13 15:58:28 -040092
David Howellsf42b2932009-04-03 16:42:44 +010093int nfs_readpage_async(struct nfs_open_context *ctx, struct inode *inode,
94 struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 struct nfs_page *new;
97 unsigned int len;
Fred Isamanc76069b2011-03-03 15:13:48 +000098 struct nfs_pageio_descriptor pgio;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Trond Myklebust49a70f22006-12-05 00:35:38 -0500100 len = nfs_page_length(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 if (len == 0)
102 return nfs_return_empty_page(page);
103 new = nfs_create_request(ctx, inode, page, 0, len);
104 if (IS_ERR(new)) {
105 unlock_page(page);
106 return PTR_ERR(new);
107 }
108 if (len < PAGE_CACHE_SIZE)
Christoph Lametereebd2aa2008-02-04 22:28:29 -0800109 zero_user_segment(page, len, PAGE_CACHE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Christoph Hellwigfab5fc22014-04-16 15:07:22 +0200111 nfs_pageio_init_read(&pgio, inode, false,
112 &nfs_async_read_completion_ops);
Trond Myklebustd8007d42011-06-10 13:30:23 -0400113 nfs_pageio_add_request(&pgio, new);
Trond Myklebust1751c362011-06-10 13:30:23 -0400114 nfs_pageio_complete(&pgio);
Andy Adamson2701d082012-05-24 13:13:24 -0400115 NFS_I(inode)->read_io += pgio.pg_bytes_written;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 return 0;
117}
118
119static void nfs_readpage_release(struct nfs_page *req)
120{
Al Viro3d4ff432011-06-22 18:40:12 -0400121 struct inode *d_inode = req->wb_context->dentry->d_inode;
David Howells7f8e05f2009-04-03 16:42:45 +0100122
123 if (PageUptodate(req->wb_page))
124 nfs_readpage_to_fscache(d_inode, req->wb_page, 0);
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 unlock_page(req->wb_page);
127
Niels de Vos1e8968c2013-12-17 18:20:16 +0100128 dprintk("NFS: read done (%s/%Lu %d@%Ld)\n",
Al Viro3d4ff432011-06-22 18:40:12 -0400129 req->wb_context->dentry->d_inode->i_sb->s_id,
Niels de Vos1e8968c2013-12-17 18:20:16 +0100130 (unsigned long long)NFS_FILEID(req->wb_context->dentry->d_inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 req->wb_bytes,
132 (long long)req_offset(req));
Nick Wilson10d2c462005-09-22 21:44:28 -0700133 nfs_release_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134}
135
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400136/* Note io was page aligned */
Fred Isaman061ae2e2012-04-20 14:47:48 -0400137static void nfs_read_completion(struct nfs_pgio_header *hdr)
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400138{
139 unsigned long bytes = 0;
140
141 if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
142 goto out;
Trond Myklebust4bd8b012012-05-01 12:49:58 -0400143 while (!list_empty(&hdr->pages)) {
144 struct nfs_page *req = nfs_list_entry(hdr->pages.next);
145 struct page *page = req->wb_page;
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400146
Trond Myklebust4bd8b012012-05-01 12:49:58 -0400147 if (test_bit(NFS_IOHDR_EOF, &hdr->flags)) {
148 if (bytes > hdr->good_bytes)
149 zero_user(page, 0, PAGE_SIZE);
150 else if (hdr->good_bytes - bytes < PAGE_SIZE)
151 zero_user_segment(page,
152 hdr->good_bytes & ~PAGE_MASK,
153 PAGE_SIZE);
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400154 }
Trond Myklebust4bd8b012012-05-01 12:49:58 -0400155 bytes += req->wb_bytes;
156 if (test_bit(NFS_IOHDR_ERROR, &hdr->flags)) {
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400157 if (bytes <= hdr->good_bytes)
Trond Myklebust4bd8b012012-05-01 12:49:58 -0400158 SetPageUptodate(page);
159 } else
160 SetPageUptodate(page);
161 nfs_list_remove_request(req);
162 nfs_readpage_release(req);
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400163 }
164out:
165 hdr->release(hdr);
166}
167
Fred Isamanc5996c42012-04-20 14:47:41 -0400168int nfs_initiate_read(struct rpc_clnt *clnt,
Anna Schumaker9c7e1b32014-05-06 09:12:26 -0400169 struct nfs_pgio_data *data,
Andy Adamson9f0ec1762012-04-27 17:53:44 -0400170 const struct rpc_call_ops *call_ops, int flags)
Andy Adamson64419a92011-03-01 01:34:16 +0000171{
Fred Isamancd841602012-04-20 14:47:44 -0400172 struct inode *inode = data->header->inode;
Andy Adamson64419a92011-03-01 01:34:16 +0000173 int swap_flags = IS_SWAPFILE(inode) ? NFS_RPC_SWAPFLAGS : 0;
174 struct rpc_task *task;
175 struct rpc_message msg = {
176 .rpc_argp = &data->args,
177 .rpc_resp = &data->res,
Fred Isamancd841602012-04-20 14:47:44 -0400178 .rpc_cred = data->header->cred,
Andy Adamson64419a92011-03-01 01:34:16 +0000179 };
180 struct rpc_task_setup task_setup_data = {
181 .task = &data->task,
182 .rpc_client = clnt,
183 .rpc_message = &msg,
184 .callback_ops = call_ops,
185 .callback_data = data,
186 .workqueue = nfsiod_workqueue,
Andy Adamson9f0ec1762012-04-27 17:53:44 -0400187 .flags = RPC_TASK_ASYNC | swap_flags | flags,
Andy Adamson64419a92011-03-01 01:34:16 +0000188 };
189
190 /* Set up the initial task struct. */
191 NFS_PROTO(inode)->read_setup(data, &msg);
192
Niels de Vos1e8968c2013-12-17 18:20:16 +0100193 dprintk("NFS: %5u initiated read call (req %s/%llu, %u bytes @ "
Andy Adamson64419a92011-03-01 01:34:16 +0000194 "offset %llu)\n",
195 data->task.tk_pid,
196 inode->i_sb->s_id,
Niels de Vos1e8968c2013-12-17 18:20:16 +0100197 (unsigned long long)NFS_FILEID(inode),
Andy Adamson64419a92011-03-01 01:34:16 +0000198 data->args.count,
199 (unsigned long long)data->args.offset);
200
201 task = rpc_run_task(&task_setup_data);
202 if (IS_ERR(task))
203 return PTR_ERR(task);
204 rpc_put_task(task);
205 return 0;
206}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000207EXPORT_SYMBOL_GPL(nfs_initiate_read);
Andy Adamson64419a92011-03-01 01:34:16 +0000208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209/*
210 * Set up the NFS read request struct
211 */
Anna Schumaker9c7e1b32014-05-06 09:12:26 -0400212static void nfs_read_rpcsetup(struct nfs_pgio_data *data,
Trond Myklebust6e4efd52011-07-12 13:42:02 -0400213 unsigned int count, unsigned int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400215 struct nfs_page *req = data->header->req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400217 data->args.fh = NFS_FH(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 data->args.offset = req_offset(req) + offset;
219 data->args.pgbase = req->wb_pgbase + offset;
Fred Isaman30dd3742012-04-20 14:47:45 -0400220 data->args.pages = data->pages.pagevec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 data->args.count = count;
Trond Myklebust383ba712008-02-19 20:04:20 -0500222 data->args.context = get_nfs_open_context(req->wb_context);
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400223 data->args.lock_context = req->wb_lock_context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 data->res.fattr = &data->fattr;
226 data->res.count = count;
227 data->res.eof = 0;
Trond Myklebust0e574af2005-10-27 22:12:38 -0400228 nfs_fattr_init(&data->fattr);
Trond Myklebust6e4efd52011-07-12 13:42:02 -0400229}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Anna Schumaker9c7e1b32014-05-06 09:12:26 -0400231static int nfs_do_read(struct nfs_pgio_data *data,
Trond Myklebust493292d2011-07-13 15:58:28 -0400232 const struct rpc_call_ops *call_ops)
Trond Myklebust6e4efd52011-07-12 13:42:02 -0400233{
Fred Isamancd841602012-04-20 14:47:44 -0400234 struct inode *inode = data->header->inode;
Trond Myklebust6e4efd52011-07-12 13:42:02 -0400235
Andy Adamson9f0ec1762012-04-27 17:53:44 -0400236 return nfs_initiate_read(NFS_CLIENT(inode), data, call_ops, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237}
238
Trond Myklebust275acaa2011-07-12 13:42:02 -0400239static int
240nfs_do_multiple_reads(struct list_head *head,
Trond Myklebust493292d2011-07-13 15:58:28 -0400241 const struct rpc_call_ops *call_ops)
Trond Myklebust275acaa2011-07-12 13:42:02 -0400242{
Anna Schumaker9c7e1b32014-05-06 09:12:26 -0400243 struct nfs_pgio_data *data;
Trond Myklebust275acaa2011-07-12 13:42:02 -0400244 int ret = 0;
245
246 while (!list_empty(head)) {
247 int ret2;
248
Anna Schumaker9c7e1b32014-05-06 09:12:26 -0400249 data = list_first_entry(head, struct nfs_pgio_data, list);
Trond Myklebust275acaa2011-07-12 13:42:02 -0400250 list_del_init(&data->list);
251
Trond Myklebust493292d2011-07-13 15:58:28 -0400252 ret2 = nfs_do_read(data, call_ops);
Trond Myklebust275acaa2011-07-12 13:42:02 -0400253 if (ret == 0)
254 ret = ret2;
255 }
256 return ret;
257}
258
Fred Isaman061ae2e2012-04-20 14:47:48 -0400259static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260nfs_async_read_error(struct list_head *head)
261{
262 struct nfs_page *req;
263
264 while (!list_empty(head)) {
265 req = nfs_list_entry(head->next);
266 nfs_list_remove_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 nfs_readpage_release(req);
268 }
269}
270
Fred Isaman061ae2e2012-04-20 14:47:48 -0400271static const struct nfs_pgio_completion_ops nfs_async_read_completion_ops = {
272 .error_cleanup = nfs_async_read_error,
273 .completion = nfs_read_completion,
274};
275
Trond Myklebust25b11dc2012-05-01 12:07:22 -0400276static void nfs_pagein_error(struct nfs_pageio_descriptor *desc,
277 struct nfs_pgio_header *hdr)
278{
279 set_bit(NFS_IOHDR_REDO, &hdr->flags);
280 while (!list_empty(&hdr->rpc_list)) {
Anna Schumaker9c7e1b32014-05-06 09:12:26 -0400281 struct nfs_pgio_data *data = list_first_entry(&hdr->rpc_list,
282 struct nfs_pgio_data, list);
Trond Myklebust25b11dc2012-05-01 12:07:22 -0400283 list_del(&data->list);
Anna Schumaker00bfa302014-05-06 09:12:29 -0400284 nfs_pgio_data_release(data);
Trond Myklebust25b11dc2012-05-01 12:07:22 -0400285 }
286 desc->pg_completion_ops->error_cleanup(&desc->pg_list);
287}
288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 * Generate multiple requests to fill a single page.
291 *
292 * We optimize to reduce the number of read operations on the wire. If we
293 * detect that we're reading a page, or an area of a page, that is past the
294 * end of file, we do not generate NFS read operations but just clear the
295 * parts of the page that would have come back zero from the server anyway.
296 *
297 * We rely on the cached value of i_size to make this determination; another
298 * client can fill pages on the server past our cached end-of-file, but we
299 * won't see the new data until our attribute cache is updated. This is more
300 * or less conventional NFS client behavior.
301 */
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400302static int nfs_pagein_multi(struct nfs_pageio_descriptor *desc,
303 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400305 struct nfs_page *req = hdr->req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 struct page *page = req->wb_page;
Anna Schumaker9c7e1b32014-05-06 09:12:26 -0400307 struct nfs_pgio_data *data;
Trond Myklebustd0979712011-07-12 13:42:02 -0400308 size_t rsize = desc->pg_bsize, nbytes;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700309 unsigned int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Trond Myklebust275acaa2011-07-12 13:42:02 -0400311 offset = 0;
Fred Isamanc76069b2011-03-03 15:13:48 +0000312 nbytes = desc->pg_count;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700313 do {
314 size_t len = min(nbytes,rsize);
315
Anna Schumaker00bfa302014-05-06 09:12:29 -0400316 data = nfs_pgio_data_alloc(hdr, 1);
Trond Myklebust25b11dc2012-05-01 12:07:22 -0400317 if (!data) {
318 nfs_pagein_error(desc, hdr);
319 return -ENOMEM;
320 }
Fred Isaman30dd3742012-04-20 14:47:45 -0400321 data->pages.pagevec[0] = page;
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400322 nfs_read_rpcsetup(data, len, offset);
323 list_add(&data->list, &hdr->rpc_list);
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700324 nbytes -= len;
Trond Myklebust275acaa2011-07-12 13:42:02 -0400325 offset += len;
Trond Myklebust9146ab52012-05-01 11:21:43 -0400326 } while (nbytes != 0);
Trond Myklebust25b11dc2012-05-01 12:07:22 -0400327
328 nfs_list_remove_request(req);
329 nfs_list_add_request(req, &hdr->pages);
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400330 desc->pg_rpc_callops = &nfs_read_common_ops;
Trond Myklebust9146ab52012-05-01 11:21:43 -0400331 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332}
333
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400334static int nfs_pagein_one(struct nfs_pageio_descriptor *desc,
335 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
337 struct nfs_page *req;
338 struct page **pages;
Anna Schumaker9c7e1b32014-05-06 09:12:26 -0400339 struct nfs_pgio_data *data;
Fred Isamanc76069b2011-03-03 15:13:48 +0000340 struct list_head *head = &desc->pg_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Anna Schumaker00bfa302014-05-06 09:12:29 -0400342 data = nfs_pgio_data_alloc(hdr, nfs_page_array_len(desc->pg_base,
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400343 desc->pg_count));
344 if (!data) {
Trond Myklebust25b11dc2012-05-01 12:07:22 -0400345 nfs_pagein_error(desc, hdr);
Trond Myklebust9146ab52012-05-01 11:21:43 -0400346 return -ENOMEM;
Fred Isamanbae724e2011-03-01 01:34:15 +0000347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Fred Isaman30dd3742012-04-20 14:47:45 -0400349 pages = data->pages.pagevec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 while (!list_empty(head)) {
351 req = nfs_list_entry(head->next);
352 nfs_list_remove_request(req);
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400353 nfs_list_add_request(req, &hdr->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 *pages++ = req->wb_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400357 nfs_read_rpcsetup(data, desc->pg_count, 0);
358 list_add(&data->list, &hdr->rpc_list);
359 desc->pg_rpc_callops = &nfs_read_common_ops;
Trond Myklebust9146ab52012-05-01 11:21:43 -0400360 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
362
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400363int nfs_generic_pagein(struct nfs_pageio_descriptor *desc,
364 struct nfs_pgio_header *hdr)
Trond Myklebust493292d2011-07-13 15:58:28 -0400365{
366 if (desc->pg_bsize < PAGE_CACHE_SIZE)
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400367 return nfs_pagein_multi(desc, hdr);
368 return nfs_pagein_one(desc, hdr);
Trond Myklebust493292d2011-07-13 15:58:28 -0400369}
Bryan Schumaker89d77c82012-07-30 16:05:25 -0400370EXPORT_SYMBOL_GPL(nfs_generic_pagein);
Trond Myklebust493292d2011-07-13 15:58:28 -0400371
372static int nfs_generic_pg_readpages(struct nfs_pageio_descriptor *desc)
Trond Myklebust1751c362011-06-10 13:30:23 -0400373{
Anna Schumakerc0752cd2014-05-06 09:12:27 -0400374 struct nfs_rw_header *rhdr;
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400375 struct nfs_pgio_header *hdr;
Trond Myklebust275acaa2011-07-12 13:42:02 -0400376 int ret;
377
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400378 rhdr = nfs_readhdr_alloc();
379 if (!rhdr) {
Fred Isaman061ae2e2012-04-20 14:47:48 -0400380 desc->pg_completion_ops->error_cleanup(&desc->pg_list);
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400381 return -ENOMEM;
382 }
383 hdr = &rhdr->header;
384 nfs_pgheader_init(desc, hdr, nfs_readhdr_free);
385 atomic_inc(&hdr->refcnt);
386 ret = nfs_generic_pagein(desc, hdr);
Trond Myklebust50828d72011-07-12 13:42:02 -0400387 if (ret == 0)
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400388 ret = nfs_do_multiple_reads(&hdr->rpc_list,
389 desc->pg_rpc_callops);
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400390 if (atomic_dec_and_test(&hdr->refcnt))
Fred Isaman061ae2e2012-04-20 14:47:48 -0400391 hdr->completion_ops->completion(hdr);
Trond Myklebust275acaa2011-07-12 13:42:02 -0400392 return ret;
Trond Myklebust1751c362011-06-10 13:30:23 -0400393}
Trond Myklebust1751c362011-06-10 13:30:23 -0400394
395static const struct nfs_pageio_ops nfs_pageio_read_ops = {
396 .pg_test = nfs_generic_pg_test,
397 .pg_doio = nfs_generic_pg_readpages,
398};
399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400/*
Trond Myklebust0b671302006-11-14 16:12:23 -0500401 * This is the callback from RPC telling us whether a reply was
402 * received or some error occurred (timeout or socket shutdown).
403 */
Anna Schumaker9c7e1b32014-05-06 09:12:26 -0400404int nfs_readpage_result(struct rpc_task *task, struct nfs_pgio_data *data)
Trond Myklebust0b671302006-11-14 16:12:23 -0500405{
Fred Isamancd841602012-04-20 14:47:44 -0400406 struct inode *inode = data->header->inode;
Trond Myklebust0b671302006-11-14 16:12:23 -0500407 int status;
408
Harvey Harrison3110ff82008-05-02 13:42:44 -0700409 dprintk("NFS: %s: %5u, (status %d)\n", __func__, task->tk_pid,
Trond Myklebust0b671302006-11-14 16:12:23 -0500410 task->tk_status);
411
Fred Isamancd841602012-04-20 14:47:44 -0400412 status = NFS_PROTO(inode)->read_done(task, data);
Trond Myklebust0b671302006-11-14 16:12:23 -0500413 if (status != 0)
414 return status;
415
Fred Isamancd841602012-04-20 14:47:44 -0400416 nfs_add_stats(inode, NFSIOS_SERVERREADBYTES, data->res.count);
Trond Myklebust0b671302006-11-14 16:12:23 -0500417
418 if (task->tk_status == -ESTALE) {
Fred Isamancd841602012-04-20 14:47:44 -0400419 set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
420 nfs_mark_for_revalidate(inode);
Trond Myklebust0b671302006-11-14 16:12:23 -0500421 }
Trond Myklebust0b671302006-11-14 16:12:23 -0500422 return 0;
423}
424
Anna Schumaker9c7e1b32014-05-06 09:12:26 -0400425static void nfs_readpage_retry(struct rpc_task *task, struct nfs_pgio_data *data)
Trond Myklebust0b671302006-11-14 16:12:23 -0500426{
Anna Schumaker3c6b8992014-05-06 09:12:24 -0400427 struct nfs_pgio_args *argp = &data->args;
Anna Schumaker9137bdf2014-05-06 09:12:25 -0400428 struct nfs_pgio_res *resp = &data->res;
Trond Myklebust0b671302006-11-14 16:12:23 -0500429
Trond Myklebust0b671302006-11-14 16:12:23 -0500430 /* This is a short read! */
Fred Isamancd841602012-04-20 14:47:44 -0400431 nfs_inc_stats(data->header->inode, NFSIOS_SHORTREAD);
Trond Myklebust0b671302006-11-14 16:12:23 -0500432 /* Has the server at least made some progress? */
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400433 if (resp->count == 0) {
434 nfs_set_pgio_error(data->header, -EIO, argp->offset);
Trond Myklebustd61e6122009-12-05 19:32:19 -0500435 return;
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400436 }
Trond Myklebust0b671302006-11-14 16:12:23 -0500437 /* Yes, so retry the read at the end of the data */
Andy Adamsoncbdabc72011-03-01 01:34:20 +0000438 data->mds_offset += resp->count;
Trond Myklebust0b671302006-11-14 16:12:23 -0500439 argp->offset += resp->count;
440 argp->pgbase += resp->count;
441 argp->count -= resp->count;
Trond Myklebustd00c5d42011-10-19 12:17:29 -0700442 rpc_restart_call_prepare(task);
Trond Myklebust0b671302006-11-14 16:12:23 -0500443}
444
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400445static void nfs_readpage_result_common(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
Anna Schumaker9c7e1b32014-05-06 09:12:26 -0400447 struct nfs_pgio_data *data = calldata;
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400448 struct nfs_pgio_header *hdr = data->header;
449
450 /* Note the only returns of nfs_readpage_result are 0 and -EAGAIN */
Trond Myklebustec06c092006-03-20 13:44:27 -0500451 if (nfs_readpage_result(task, data) != 0)
452 return;
Trond Myklebustfdd1e742008-04-15 16:33:58 -0400453 if (task->tk_status < 0)
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400454 nfs_set_pgio_error(hdr, task->tk_status, data->args.offset);
455 else if (data->res.eof) {
456 loff_t bound;
Trond Myklebust0b671302006-11-14 16:12:23 -0500457
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400458 bound = data->args.offset + data->res.count;
459 spin_lock(&hdr->lock);
460 if (bound < hdr->io_start + hdr->good_bytes) {
461 set_bit(NFS_IOHDR_EOF, &hdr->flags);
462 clear_bit(NFS_IOHDR_ERROR, &hdr->flags);
463 hdr->good_bytes = bound - hdr->io_start;
464 }
465 spin_unlock(&hdr->lock);
466 } else if (data->res.count != data->args.count)
467 nfs_readpage_retry(task, data);
Trond Myklebustfdd1e742008-04-15 16:33:58 -0400468}
469
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400470static void nfs_readpage_release_common(void *calldata)
Trond Myklebustfdd1e742008-04-15 16:33:58 -0400471{
Anna Schumaker00bfa302014-05-06 09:12:29 -0400472 nfs_pgio_data_release(calldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473}
474
Andy Adamsonf11c88a2009-04-01 09:22:25 -0400475void nfs_read_prepare(struct rpc_task *task, void *calldata)
476{
Anna Schumaker9c7e1b32014-05-06 09:12:26 -0400477 struct nfs_pgio_data *data = calldata;
NeilBrownef1820f2013-09-04 17:04:49 +1000478 int err;
479 err = NFS_PROTO(data->header->inode)->read_rpc_prepare(task, data);
480 if (err)
481 rpc_exit(task, err);
Andy Adamsonf11c88a2009-04-01 09:22:25 -0400482}
Andy Adamsonf11c88a2009-04-01 09:22:25 -0400483
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400484static const struct rpc_call_ops nfs_read_common_ops = {
Andy Adamsonf11c88a2009-04-01 09:22:25 -0400485 .rpc_call_prepare = nfs_read_prepare,
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400486 .rpc_call_done = nfs_readpage_result_common,
487 .rpc_release = nfs_readpage_release_common,
Trond Myklebustec06c092006-03-20 13:44:27 -0500488};
489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 * Read a page over NFS.
492 * We read the page synchronously in the following case:
493 * - The error flag is set for this page. This happens only when a
494 * previous async read operation failed.
495 */
496int nfs_readpage(struct file *file, struct page *page)
497{
498 struct nfs_open_context *ctx;
Mel Gormand56b4dd2012-07-31 16:45:06 -0700499 struct inode *inode = page_file_mapping(page)->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 int error;
501
502 dprintk("NFS: nfs_readpage (%p %ld@%lu)\n",
Mel Gormand56b4dd2012-07-31 16:45:06 -0700503 page, PAGE_CACHE_SIZE, page_file_index(page));
Chuck Lever91d5b472006-03-20 13:44:14 -0500504 nfs_inc_stats(inode, NFSIOS_VFSREADPAGE);
505 nfs_add_stats(inode, NFSIOS_READPAGES, 1);
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 /*
508 * Try to flush any pending writes to the file..
509 *
510 * NOTE! Because we own the page lock, there cannot
511 * be any new pending writes generated at this point
512 * for this page (other pages can be written to).
513 */
514 error = nfs_wb_page(inode, page);
515 if (error)
Trond Myklebustde05a0c2007-05-20 13:05:05 -0400516 goto out_unlock;
517 if (PageUptodate(page))
518 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Trond Myklebust5f004cf2006-09-14 14:03:14 -0400520 error = -ESTALE;
521 if (NFS_STALE(inode))
Trond Myklebustde05a0c2007-05-20 13:05:05 -0400522 goto out_unlock;
Trond Myklebust5f004cf2006-09-14 14:03:14 -0400523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 if (file == NULL) {
Trond Myklebustcf1308f2006-11-19 16:44:52 -0500525 error = -EBADF;
Trond Myklebustd5308382005-11-04 15:33:38 -0500526 ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 if (ctx == NULL)
Trond Myklebustde05a0c2007-05-20 13:05:05 -0400528 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 } else
Trond Myklebustcd3758e2007-08-10 17:44:32 -0400530 ctx = get_nfs_open_context(nfs_file_open_context(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
David Howells9a9fc1c2009-04-03 16:42:44 +0100532 if (!IS_SYNC(inode)) {
533 error = nfs_readpage_from_fscache(ctx, inode, page);
534 if (error == 0)
535 goto out;
536 }
537
Trond Myklebust8e0969f2006-12-13 15:23:44 -0500538 error = nfs_readpage_async(ctx, inode, page);
539
David Howells9a9fc1c2009-04-03 16:42:44 +0100540out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 put_nfs_open_context(ctx);
542 return error;
Trond Myklebustde05a0c2007-05-20 13:05:05 -0400543out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 unlock_page(page);
545 return error;
546}
547
548struct nfs_readdesc {
Trond Myklebust8b09bee2007-04-02 18:48:28 -0400549 struct nfs_pageio_descriptor *pgio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 struct nfs_open_context *ctx;
551};
552
553static int
554readpage_async_filler(void *data, struct page *page)
555{
556 struct nfs_readdesc *desc = (struct nfs_readdesc *)data;
Mel Gormand56b4dd2012-07-31 16:45:06 -0700557 struct inode *inode = page_file_mapping(page)->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 struct nfs_page *new;
559 unsigned int len;
Trond Myklebustde05a0c2007-05-20 13:05:05 -0400560 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Trond Myklebust49a70f22006-12-05 00:35:38 -0500562 len = nfs_page_length(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 if (len == 0)
564 return nfs_return_empty_page(page);
Trond Myklebustde05a0c2007-05-20 13:05:05 -0400565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 new = nfs_create_request(desc->ctx, inode, page, 0, len);
Trond Myklebustde05a0c2007-05-20 13:05:05 -0400567 if (IS_ERR(new))
568 goto out_error;
569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 if (len < PAGE_CACHE_SIZE)
Christoph Lametereebd2aa2008-02-04 22:28:29 -0800571 zero_user_segment(page, len, PAGE_CACHE_SIZE);
Fred Isamanf8512ad2008-03-19 11:24:39 -0400572 if (!nfs_pageio_add_request(desc->pgio, new)) {
573 error = desc->pgio->pg_error;
574 goto out_unlock;
575 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 return 0;
Trond Myklebustde05a0c2007-05-20 13:05:05 -0400577out_error:
578 error = PTR_ERR(new);
Trond Myklebustde05a0c2007-05-20 13:05:05 -0400579out_unlock:
580 unlock_page(page);
581 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582}
583
584int nfs_readpages(struct file *filp, struct address_space *mapping,
585 struct list_head *pages, unsigned nr_pages)
586{
Trond Myklebust8b09bee2007-04-02 18:48:28 -0400587 struct nfs_pageio_descriptor pgio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 struct nfs_readdesc desc = {
Trond Myklebust8b09bee2007-04-02 18:48:28 -0400589 .pgio = &pgio,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 };
591 struct inode *inode = mapping->host;
Trond Myklebust8b09bee2007-04-02 18:48:28 -0400592 unsigned long npages;
Trond Myklebust5f004cf2006-09-14 14:03:14 -0400593 int ret = -ESTALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Niels de Vos1e8968c2013-12-17 18:20:16 +0100595 dprintk("NFS: nfs_readpages (%s/%Lu %d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 inode->i_sb->s_id,
Niels de Vos1e8968c2013-12-17 18:20:16 +0100597 (unsigned long long)NFS_FILEID(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 nr_pages);
Chuck Lever91d5b472006-03-20 13:44:14 -0500599 nfs_inc_stats(inode, NFSIOS_VFSREADPAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Trond Myklebust5f004cf2006-09-14 14:03:14 -0400601 if (NFS_STALE(inode))
602 goto out;
603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 if (filp == NULL) {
Trond Myklebustd5308382005-11-04 15:33:38 -0500605 desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 if (desc.ctx == NULL)
607 return -EBADF;
608 } else
Trond Myklebustcd3758e2007-08-10 17:44:32 -0400609 desc.ctx = get_nfs_open_context(nfs_file_open_context(filp));
David Howells9a9fc1c2009-04-03 16:42:44 +0100610
611 /* attempt to read as many of the pages as possible from the cache
612 * - this returns -ENOBUFS immediately if the cookie is negative
613 */
614 ret = nfs_readpages_from_fscache(desc.ctx, inode, mapping,
615 pages, &nr_pages);
616 if (ret == 0)
617 goto read_complete; /* all pages were read */
618
Christoph Hellwigfab5fc22014-04-16 15:07:22 +0200619 nfs_pageio_init_read(&pgio, inode, false,
620 &nfs_async_read_completion_ops);
Trond Myklebust8b09bee2007-04-02 18:48:28 -0400621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 ret = read_cache_pages(mapping, pages, readpage_async_filler, &desc);
Trond Myklebust8b09bee2007-04-02 18:48:28 -0400623
624 nfs_pageio_complete(&pgio);
Andy Adamson2701d082012-05-24 13:13:24 -0400625 NFS_I(inode)->read_io += pgio.pg_bytes_written;
Trond Myklebust8b09bee2007-04-02 18:48:28 -0400626 npages = (pgio.pg_bytes_written + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
627 nfs_add_stats(inode, NFSIOS_READPAGES, npages);
David Howells9a9fc1c2009-04-03 16:42:44 +0100628read_complete:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 put_nfs_open_context(desc.ctx);
Trond Myklebust5f004cf2006-09-14 14:03:14 -0400630out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 return ret;
632}
633
David Howellsf7b422b2006-06-09 09:34:33 -0400634int __init nfs_init_readpagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
636 nfs_rdata_cachep = kmem_cache_create("nfs_read_data",
Anna Schumakerc0752cd2014-05-06 09:12:27 -0400637 sizeof(struct nfs_rw_header),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 0, SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +0900639 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 if (nfs_rdata_cachep == NULL)
641 return -ENOMEM;
642
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 return 0;
644}
645
David Brownell266bee82006-06-27 12:59:15 -0700646void nfs_destroy_readpagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -0700648 kmem_cache_destroy(nfs_rdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649}