blob: 0359b0d76ef67f9232791d37e6264195e5a8bf3d [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 Isaman061ae2e2012-04-20 14:47:48 -040032static const struct nfs_pgio_completion_ops nfs_async_read_completion_ops;
Anna Schumaker4a0de552014-05-06 09:12:30 -040033static const struct nfs_rw_ops nfs_rw_read_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 Schumaker4a0de552014-05-06 09:12:30 -040037static struct nfs_rw_header *nfs_readhdr_alloc(void)
Trond Myklebust3feb2d42006-03-20 13:44:37 -050038{
Anna Schumaker4a0de552014-05-06 09:12:30 -040039 return kmem_cache_zalloc(nfs_rdata_cachep, GFP_KERNEL);
Fred Isaman4db6e0b2012-04-20 14:47:46 -040040}
41
Anna Schumaker4a0de552014-05-06 09:12:30 -040042static void nfs_readhdr_free(struct nfs_rw_header *rhdr)
Trond Myklebust3feb2d42006-03-20 13:44:37 -050043{
Fred Isamancd841602012-04-20 14:47:44 -040044 kmem_cache_free(nfs_rdata_cachep, rhdr);
Trond Myklebust3feb2d42006-03-20 13:44:37 -050045}
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047static
Linus Torvalds1da177e2005-04-16 15:20:36 -070048int nfs_return_empty_page(struct page *page)
49{
Christoph Lametereebd2aa2008-02-04 22:28:29 -080050 zero_user(page, 0, PAGE_CACHE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 SetPageUptodate(page);
52 unlock_page(page);
53 return 0;
54}
55
Bryan Schumaker1abb50882012-06-20 15:53:47 -040056void nfs_pageio_init_read(struct nfs_pageio_descriptor *pgio,
Christoph Hellwigfab5fc22014-04-16 15:07:22 +020057 struct inode *inode, bool force_mds,
Fred Isaman061ae2e2012-04-20 14:47:48 -040058 const struct nfs_pgio_completion_ops *compl_ops)
Trond Myklebust1751c362011-06-10 13:30:23 -040059{
Christoph Hellwigfab5fc22014-04-16 15:07:22 +020060 struct nfs_server *server = NFS_SERVER(inode);
61 const struct nfs_pageio_ops *pg_ops = &nfs_pageio_read_ops;
62
63#ifdef CONFIG_NFS_V4_1
64 if (server->pnfs_curr_ld && !force_mds)
65 pg_ops = server->pnfs_curr_ld->pg_read_ops;
66#endif
Anna Schumaker4a0de552014-05-06 09:12:30 -040067 nfs_pageio_init(pgio, inode, pg_ops, compl_ops, &nfs_rw_read_ops,
68 server->rsize, 0);
Trond Myklebust1751c362011-06-10 13:30:23 -040069}
Bryan Schumakerddda8e02012-07-30 16:05:23 -040070EXPORT_SYMBOL_GPL(nfs_pageio_init_read);
Trond Myklebust1751c362011-06-10 13:30:23 -040071
Trond Myklebust493292d2011-07-13 15:58:28 -040072void nfs_pageio_reset_read_mds(struct nfs_pageio_descriptor *pgio)
73{
74 pgio->pg_ops = &nfs_pageio_read_ops;
75 pgio->pg_bsize = NFS_SERVER(pgio->pg_inode)->rsize;
76}
Trond Myklebust1f945352011-07-13 15:59:57 -040077EXPORT_SYMBOL_GPL(nfs_pageio_reset_read_mds);
Trond Myklebust493292d2011-07-13 15:58:28 -040078
David Howellsf42b2932009-04-03 16:42:44 +010079int nfs_readpage_async(struct nfs_open_context *ctx, struct inode *inode,
80 struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 struct nfs_page *new;
83 unsigned int len;
Fred Isamanc76069b2011-03-03 15:13:48 +000084 struct nfs_pageio_descriptor pgio;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Trond Myklebust49a70f22006-12-05 00:35:38 -050086 len = nfs_page_length(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 if (len == 0)
88 return nfs_return_empty_page(page);
89 new = nfs_create_request(ctx, inode, page, 0, len);
90 if (IS_ERR(new)) {
91 unlock_page(page);
92 return PTR_ERR(new);
93 }
94 if (len < PAGE_CACHE_SIZE)
Christoph Lametereebd2aa2008-02-04 22:28:29 -080095 zero_user_segment(page, len, PAGE_CACHE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Christoph Hellwigfab5fc22014-04-16 15:07:22 +020097 nfs_pageio_init_read(&pgio, inode, false,
98 &nfs_async_read_completion_ops);
Trond Myklebustd8007d42011-06-10 13:30:23 -040099 nfs_pageio_add_request(&pgio, new);
Trond Myklebust1751c362011-06-10 13:30:23 -0400100 nfs_pageio_complete(&pgio);
Andy Adamson2701d082012-05-24 13:13:24 -0400101 NFS_I(inode)->read_io += pgio.pg_bytes_written;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 return 0;
103}
104
105static void nfs_readpage_release(struct nfs_page *req)
106{
Al Viro3d4ff432011-06-22 18:40:12 -0400107 struct inode *d_inode = req->wb_context->dentry->d_inode;
David Howells7f8e05f2009-04-03 16:42:45 +0100108
109 if (PageUptodate(req->wb_page))
110 nfs_readpage_to_fscache(d_inode, req->wb_page, 0);
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 unlock_page(req->wb_page);
113
Niels de Vos1e8968c2013-12-17 18:20:16 +0100114 dprintk("NFS: read done (%s/%Lu %d@%Ld)\n",
Al Viro3d4ff432011-06-22 18:40:12 -0400115 req->wb_context->dentry->d_inode->i_sb->s_id,
Niels de Vos1e8968c2013-12-17 18:20:16 +0100116 (unsigned long long)NFS_FILEID(req->wb_context->dentry->d_inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 req->wb_bytes,
118 (long long)req_offset(req));
Nick Wilson10d2c462005-09-22 21:44:28 -0700119 nfs_release_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
121
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400122/* Note io was page aligned */
Fred Isaman061ae2e2012-04-20 14:47:48 -0400123static void nfs_read_completion(struct nfs_pgio_header *hdr)
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400124{
125 unsigned long bytes = 0;
126
127 if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
128 goto out;
Trond Myklebust4bd8b012012-05-01 12:49:58 -0400129 while (!list_empty(&hdr->pages)) {
130 struct nfs_page *req = nfs_list_entry(hdr->pages.next);
131 struct page *page = req->wb_page;
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400132
Trond Myklebust4bd8b012012-05-01 12:49:58 -0400133 if (test_bit(NFS_IOHDR_EOF, &hdr->flags)) {
134 if (bytes > hdr->good_bytes)
135 zero_user(page, 0, PAGE_SIZE);
136 else if (hdr->good_bytes - bytes < PAGE_SIZE)
137 zero_user_segment(page,
138 hdr->good_bytes & ~PAGE_MASK,
139 PAGE_SIZE);
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400140 }
Trond Myklebust4bd8b012012-05-01 12:49:58 -0400141 bytes += req->wb_bytes;
142 if (test_bit(NFS_IOHDR_ERROR, &hdr->flags)) {
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400143 if (bytes <= hdr->good_bytes)
Trond Myklebust4bd8b012012-05-01 12:49:58 -0400144 SetPageUptodate(page);
145 } else
146 SetPageUptodate(page);
147 nfs_list_remove_request(req);
148 nfs_readpage_release(req);
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400149 }
150out:
151 hdr->release(hdr);
152}
153
Anna Schumaker1ed26f32014-05-06 09:12:37 -0400154static void nfs_initiate_read(struct nfs_pgio_data *data, struct rpc_message *msg,
155 struct rpc_task_setup *task_setup_data, int how)
Andy Adamson64419a92011-03-01 01:34:16 +0000156{
Fred Isamancd841602012-04-20 14:47:44 -0400157 struct inode *inode = data->header->inode;
Andy Adamson64419a92011-03-01 01:34:16 +0000158 int swap_flags = IS_SWAPFILE(inode) ? NFS_RPC_SWAPFLAGS : 0;
Andy Adamson64419a92011-03-01 01:34:16 +0000159
Anna Schumaker1ed26f32014-05-06 09:12:37 -0400160 task_setup_data->flags |= swap_flags;
161 NFS_PROTO(inode)->read_setup(data, msg);
Andy Adamson64419a92011-03-01 01:34:16 +0000162}
163
Anna Schumaker9c7e1b32014-05-06 09:12:26 -0400164static int nfs_do_read(struct nfs_pgio_data *data,
Trond Myklebust493292d2011-07-13 15:58:28 -0400165 const struct rpc_call_ops *call_ops)
Trond Myklebust6e4efd52011-07-12 13:42:02 -0400166{
Fred Isamancd841602012-04-20 14:47:44 -0400167 struct inode *inode = data->header->inode;
Trond Myklebust6e4efd52011-07-12 13:42:02 -0400168
Anna Schumaker1ed26f32014-05-06 09:12:37 -0400169 return nfs_initiate_pgio(NFS_CLIENT(inode), data, call_ops, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171
Trond Myklebust275acaa2011-07-12 13:42:02 -0400172static int
173nfs_do_multiple_reads(struct list_head *head,
Trond Myklebust493292d2011-07-13 15:58:28 -0400174 const struct rpc_call_ops *call_ops)
Trond Myklebust275acaa2011-07-12 13:42:02 -0400175{
Anna Schumaker9c7e1b32014-05-06 09:12:26 -0400176 struct nfs_pgio_data *data;
Trond Myklebust275acaa2011-07-12 13:42:02 -0400177 int ret = 0;
178
179 while (!list_empty(head)) {
180 int ret2;
181
Anna Schumaker9c7e1b32014-05-06 09:12:26 -0400182 data = list_first_entry(head, struct nfs_pgio_data, list);
Trond Myklebust275acaa2011-07-12 13:42:02 -0400183 list_del_init(&data->list);
184
Trond Myklebust493292d2011-07-13 15:58:28 -0400185 ret2 = nfs_do_read(data, call_ops);
Trond Myklebust275acaa2011-07-12 13:42:02 -0400186 if (ret == 0)
187 ret = ret2;
188 }
189 return ret;
190}
191
Fred Isaman061ae2e2012-04-20 14:47:48 -0400192static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193nfs_async_read_error(struct list_head *head)
194{
195 struct nfs_page *req;
196
197 while (!list_empty(head)) {
198 req = nfs_list_entry(head->next);
199 nfs_list_remove_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 nfs_readpage_release(req);
201 }
202}
203
Fred Isaman061ae2e2012-04-20 14:47:48 -0400204static const struct nfs_pgio_completion_ops nfs_async_read_completion_ops = {
205 .error_cleanup = nfs_async_read_error,
206 .completion = nfs_read_completion,
207};
208
Trond Myklebust493292d2011-07-13 15:58:28 -0400209static int nfs_generic_pg_readpages(struct nfs_pageio_descriptor *desc)
Trond Myklebust1751c362011-06-10 13:30:23 -0400210{
Anna Schumakerc0752cd2014-05-06 09:12:27 -0400211 struct nfs_rw_header *rhdr;
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400212 struct nfs_pgio_header *hdr;
Trond Myklebust275acaa2011-07-12 13:42:02 -0400213 int ret;
214
Anna Schumaker4a0de552014-05-06 09:12:30 -0400215 rhdr = nfs_rw_header_alloc(desc->pg_rw_ops);
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400216 if (!rhdr) {
Fred Isaman061ae2e2012-04-20 14:47:48 -0400217 desc->pg_completion_ops->error_cleanup(&desc->pg_list);
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400218 return -ENOMEM;
219 }
220 hdr = &rhdr->header;
Anna Schumaker4a0de552014-05-06 09:12:30 -0400221 nfs_pgheader_init(desc, hdr, nfs_rw_header_free);
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400222 atomic_inc(&hdr->refcnt);
Anna Schumakeref2c4882014-05-06 09:12:36 -0400223 ret = nfs_generic_pgio(desc, hdr);
Trond Myklebust50828d72011-07-12 13:42:02 -0400224 if (ret == 0)
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400225 ret = nfs_do_multiple_reads(&hdr->rpc_list,
226 desc->pg_rpc_callops);
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400227 if (atomic_dec_and_test(&hdr->refcnt))
Fred Isaman061ae2e2012-04-20 14:47:48 -0400228 hdr->completion_ops->completion(hdr);
Trond Myklebust275acaa2011-07-12 13:42:02 -0400229 return ret;
Trond Myklebust1751c362011-06-10 13:30:23 -0400230}
Trond Myklebust1751c362011-06-10 13:30:23 -0400231
232static const struct nfs_pageio_ops nfs_pageio_read_ops = {
233 .pg_test = nfs_generic_pg_test,
234 .pg_doio = nfs_generic_pg_readpages,
235};
236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237/*
Trond Myklebust0b671302006-11-14 16:12:23 -0500238 * This is the callback from RPC telling us whether a reply was
239 * received or some error occurred (timeout or socket shutdown).
240 */
Anna Schumaker0eecb212014-05-06 09:12:32 -0400241static int nfs_readpage_done(struct rpc_task *task, struct nfs_pgio_data *data,
242 struct inode *inode)
Trond Myklebust0b671302006-11-14 16:12:23 -0500243{
Anna Schumaker0eecb212014-05-06 09:12:32 -0400244 int status = NFS_PROTO(inode)->read_done(task, data);
Trond Myklebust0b671302006-11-14 16:12:23 -0500245 if (status != 0)
246 return status;
247
Fred Isamancd841602012-04-20 14:47:44 -0400248 nfs_add_stats(inode, NFSIOS_SERVERREADBYTES, data->res.count);
Trond Myklebust0b671302006-11-14 16:12:23 -0500249
250 if (task->tk_status == -ESTALE) {
Fred Isamancd841602012-04-20 14:47:44 -0400251 set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
252 nfs_mark_for_revalidate(inode);
Trond Myklebust0b671302006-11-14 16:12:23 -0500253 }
Trond Myklebust0b671302006-11-14 16:12:23 -0500254 return 0;
255}
256
Anna Schumaker9c7e1b32014-05-06 09:12:26 -0400257static void nfs_readpage_retry(struct rpc_task *task, struct nfs_pgio_data *data)
Trond Myklebust0b671302006-11-14 16:12:23 -0500258{
Anna Schumaker3c6b8992014-05-06 09:12:24 -0400259 struct nfs_pgio_args *argp = &data->args;
Anna Schumaker9137bdf2014-05-06 09:12:25 -0400260 struct nfs_pgio_res *resp = &data->res;
Trond Myklebust0b671302006-11-14 16:12:23 -0500261
Trond Myklebust0b671302006-11-14 16:12:23 -0500262 /* This is a short read! */
Fred Isamancd841602012-04-20 14:47:44 -0400263 nfs_inc_stats(data->header->inode, NFSIOS_SHORTREAD);
Trond Myklebust0b671302006-11-14 16:12:23 -0500264 /* Has the server at least made some progress? */
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400265 if (resp->count == 0) {
266 nfs_set_pgio_error(data->header, -EIO, argp->offset);
Trond Myklebustd61e6122009-12-05 19:32:19 -0500267 return;
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400268 }
Trond Myklebust0b671302006-11-14 16:12:23 -0500269 /* Yes, so retry the read at the end of the data */
Andy Adamsoncbdabc72011-03-01 01:34:20 +0000270 data->mds_offset += resp->count;
Trond Myklebust0b671302006-11-14 16:12:23 -0500271 argp->offset += resp->count;
272 argp->pgbase += resp->count;
273 argp->count -= resp->count;
Trond Myklebustd00c5d42011-10-19 12:17:29 -0700274 rpc_restart_call_prepare(task);
Trond Myklebust0b671302006-11-14 16:12:23 -0500275}
276
Anna Schumaker0eecb212014-05-06 09:12:32 -0400277static void nfs_readpage_result(struct rpc_task *task, struct nfs_pgio_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400279 struct nfs_pgio_header *hdr = data->header;
280
Anna Schumaker0eecb212014-05-06 09:12:32 -0400281 if (data->res.eof) {
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400282 loff_t bound;
Trond Myklebust0b671302006-11-14 16:12:23 -0500283
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400284 bound = data->args.offset + data->res.count;
285 spin_lock(&hdr->lock);
286 if (bound < hdr->io_start + hdr->good_bytes) {
287 set_bit(NFS_IOHDR_EOF, &hdr->flags);
288 clear_bit(NFS_IOHDR_ERROR, &hdr->flags);
289 hdr->good_bytes = bound - hdr->io_start;
290 }
291 spin_unlock(&hdr->lock);
292 } else if (data->res.count != data->args.count)
293 nfs_readpage_retry(task, data);
Trond Myklebustfdd1e742008-04-15 16:33:58 -0400294}
295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 * Read a page over NFS.
298 * We read the page synchronously in the following case:
299 * - The error flag is set for this page. This happens only when a
300 * previous async read operation failed.
301 */
302int nfs_readpage(struct file *file, struct page *page)
303{
304 struct nfs_open_context *ctx;
Mel Gormand56b4dd2012-07-31 16:45:06 -0700305 struct inode *inode = page_file_mapping(page)->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 int error;
307
308 dprintk("NFS: nfs_readpage (%p %ld@%lu)\n",
Mel Gormand56b4dd2012-07-31 16:45:06 -0700309 page, PAGE_CACHE_SIZE, page_file_index(page));
Chuck Lever91d5b472006-03-20 13:44:14 -0500310 nfs_inc_stats(inode, NFSIOS_VFSREADPAGE);
311 nfs_add_stats(inode, NFSIOS_READPAGES, 1);
312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 /*
314 * Try to flush any pending writes to the file..
315 *
316 * NOTE! Because we own the page lock, there cannot
317 * be any new pending writes generated at this point
318 * for this page (other pages can be written to).
319 */
320 error = nfs_wb_page(inode, page);
321 if (error)
Trond Myklebustde05a0c2007-05-20 13:05:05 -0400322 goto out_unlock;
323 if (PageUptodate(page))
324 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Trond Myklebust5f004cf2006-09-14 14:03:14 -0400326 error = -ESTALE;
327 if (NFS_STALE(inode))
Trond Myklebustde05a0c2007-05-20 13:05:05 -0400328 goto out_unlock;
Trond Myklebust5f004cf2006-09-14 14:03:14 -0400329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 if (file == NULL) {
Trond Myklebustcf1308f2006-11-19 16:44:52 -0500331 error = -EBADF;
Trond Myklebustd5308382005-11-04 15:33:38 -0500332 ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 if (ctx == NULL)
Trond Myklebustde05a0c2007-05-20 13:05:05 -0400334 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 } else
Trond Myklebustcd3758e2007-08-10 17:44:32 -0400336 ctx = get_nfs_open_context(nfs_file_open_context(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
David Howells9a9fc1c2009-04-03 16:42:44 +0100338 if (!IS_SYNC(inode)) {
339 error = nfs_readpage_from_fscache(ctx, inode, page);
340 if (error == 0)
341 goto out;
342 }
343
Trond Myklebust8e0969f2006-12-13 15:23:44 -0500344 error = nfs_readpage_async(ctx, inode, page);
345
David Howells9a9fc1c2009-04-03 16:42:44 +0100346out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 put_nfs_open_context(ctx);
348 return error;
Trond Myklebustde05a0c2007-05-20 13:05:05 -0400349out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 unlock_page(page);
351 return error;
352}
353
354struct nfs_readdesc {
Trond Myklebust8b09bee2007-04-02 18:48:28 -0400355 struct nfs_pageio_descriptor *pgio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 struct nfs_open_context *ctx;
357};
358
359static int
360readpage_async_filler(void *data, struct page *page)
361{
362 struct nfs_readdesc *desc = (struct nfs_readdesc *)data;
Mel Gormand56b4dd2012-07-31 16:45:06 -0700363 struct inode *inode = page_file_mapping(page)->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 struct nfs_page *new;
365 unsigned int len;
Trond Myklebustde05a0c2007-05-20 13:05:05 -0400366 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Trond Myklebust49a70f22006-12-05 00:35:38 -0500368 len = nfs_page_length(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 if (len == 0)
370 return nfs_return_empty_page(page);
Trond Myklebustde05a0c2007-05-20 13:05:05 -0400371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 new = nfs_create_request(desc->ctx, inode, page, 0, len);
Trond Myklebustde05a0c2007-05-20 13:05:05 -0400373 if (IS_ERR(new))
374 goto out_error;
375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 if (len < PAGE_CACHE_SIZE)
Christoph Lametereebd2aa2008-02-04 22:28:29 -0800377 zero_user_segment(page, len, PAGE_CACHE_SIZE);
Fred Isamanf8512ad2008-03-19 11:24:39 -0400378 if (!nfs_pageio_add_request(desc->pgio, new)) {
379 error = desc->pgio->pg_error;
380 goto out_unlock;
381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 return 0;
Trond Myklebustde05a0c2007-05-20 13:05:05 -0400383out_error:
384 error = PTR_ERR(new);
Trond Myklebustde05a0c2007-05-20 13:05:05 -0400385out_unlock:
386 unlock_page(page);
387 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388}
389
390int nfs_readpages(struct file *filp, struct address_space *mapping,
391 struct list_head *pages, unsigned nr_pages)
392{
Trond Myklebust8b09bee2007-04-02 18:48:28 -0400393 struct nfs_pageio_descriptor pgio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 struct nfs_readdesc desc = {
Trond Myklebust8b09bee2007-04-02 18:48:28 -0400395 .pgio = &pgio,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 };
397 struct inode *inode = mapping->host;
Trond Myklebust8b09bee2007-04-02 18:48:28 -0400398 unsigned long npages;
Trond Myklebust5f004cf2006-09-14 14:03:14 -0400399 int ret = -ESTALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Niels de Vos1e8968c2013-12-17 18:20:16 +0100401 dprintk("NFS: nfs_readpages (%s/%Lu %d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 inode->i_sb->s_id,
Niels de Vos1e8968c2013-12-17 18:20:16 +0100403 (unsigned long long)NFS_FILEID(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 nr_pages);
Chuck Lever91d5b472006-03-20 13:44:14 -0500405 nfs_inc_stats(inode, NFSIOS_VFSREADPAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Trond Myklebust5f004cf2006-09-14 14:03:14 -0400407 if (NFS_STALE(inode))
408 goto out;
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 if (filp == NULL) {
Trond Myklebustd5308382005-11-04 15:33:38 -0500411 desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 if (desc.ctx == NULL)
413 return -EBADF;
414 } else
Trond Myklebustcd3758e2007-08-10 17:44:32 -0400415 desc.ctx = get_nfs_open_context(nfs_file_open_context(filp));
David Howells9a9fc1c2009-04-03 16:42:44 +0100416
417 /* attempt to read as many of the pages as possible from the cache
418 * - this returns -ENOBUFS immediately if the cookie is negative
419 */
420 ret = nfs_readpages_from_fscache(desc.ctx, inode, mapping,
421 pages, &nr_pages);
422 if (ret == 0)
423 goto read_complete; /* all pages were read */
424
Christoph Hellwigfab5fc22014-04-16 15:07:22 +0200425 nfs_pageio_init_read(&pgio, inode, false,
426 &nfs_async_read_completion_ops);
Trond Myklebust8b09bee2007-04-02 18:48:28 -0400427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 ret = read_cache_pages(mapping, pages, readpage_async_filler, &desc);
Trond Myklebust8b09bee2007-04-02 18:48:28 -0400429
430 nfs_pageio_complete(&pgio);
Andy Adamson2701d082012-05-24 13:13:24 -0400431 NFS_I(inode)->read_io += pgio.pg_bytes_written;
Trond Myklebust8b09bee2007-04-02 18:48:28 -0400432 npages = (pgio.pg_bytes_written + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
433 nfs_add_stats(inode, NFSIOS_READPAGES, npages);
David Howells9a9fc1c2009-04-03 16:42:44 +0100434read_complete:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 put_nfs_open_context(desc.ctx);
Trond Myklebust5f004cf2006-09-14 14:03:14 -0400436out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 return ret;
438}
439
David Howellsf7b422b2006-06-09 09:34:33 -0400440int __init nfs_init_readpagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
442 nfs_rdata_cachep = kmem_cache_create("nfs_read_data",
Anna Schumakerc0752cd2014-05-06 09:12:27 -0400443 sizeof(struct nfs_rw_header),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 0, SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +0900445 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 if (nfs_rdata_cachep == NULL)
447 return -ENOMEM;
448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 return 0;
450}
451
David Brownell266bee82006-06-27 12:59:15 -0700452void nfs_destroy_readpagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -0700454 kmem_cache_destroy(nfs_rdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
Anna Schumaker4a0de552014-05-06 09:12:30 -0400456
457static const struct nfs_rw_ops nfs_rw_read_ops = {
Anna Schumakera4cdda52014-05-06 09:12:31 -0400458 .rw_mode = FMODE_READ,
Anna Schumaker4a0de552014-05-06 09:12:30 -0400459 .rw_alloc_header = nfs_readhdr_alloc,
460 .rw_free_header = nfs_readhdr_free,
Anna Schumaker0eecb212014-05-06 09:12:32 -0400461 .rw_done = nfs_readpage_done,
462 .rw_result = nfs_readpage_result,
Anna Schumaker1ed26f32014-05-06 09:12:37 -0400463 .rw_initiate = nfs_initiate_read,
Anna Schumaker4a0de552014-05-06 09:12:30 -0400464};