blob: ebd1666ee13cf9a3505a6004d0fd6d747e98ae28 [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
Fred Isaman061ae2e2012-04-20 14:47:48 -0400164static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165nfs_async_read_error(struct list_head *head)
166{
167 struct nfs_page *req;
168
169 while (!list_empty(head)) {
170 req = nfs_list_entry(head->next);
171 nfs_list_remove_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 nfs_readpage_release(req);
173 }
174}
175
Fred Isaman061ae2e2012-04-20 14:47:48 -0400176static const struct nfs_pgio_completion_ops nfs_async_read_completion_ops = {
177 .error_cleanup = nfs_async_read_error,
178 .completion = nfs_read_completion,
179};
180
Trond Myklebust1751c362011-06-10 13:30:23 -0400181static const struct nfs_pageio_ops nfs_pageio_read_ops = {
182 .pg_test = nfs_generic_pg_test,
Anna Schumakercf485fc2014-05-06 09:12:39 -0400183 .pg_doio = nfs_generic_pg_pgios,
Trond Myklebust1751c362011-06-10 13:30:23 -0400184};
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186/*
Trond Myklebust0b671302006-11-14 16:12:23 -0500187 * This is the callback from RPC telling us whether a reply was
188 * received or some error occurred (timeout or socket shutdown).
189 */
Anna Schumaker0eecb212014-05-06 09:12:32 -0400190static int nfs_readpage_done(struct rpc_task *task, struct nfs_pgio_data *data,
191 struct inode *inode)
Trond Myklebust0b671302006-11-14 16:12:23 -0500192{
Anna Schumaker0eecb212014-05-06 09:12:32 -0400193 int status = NFS_PROTO(inode)->read_done(task, data);
Trond Myklebust0b671302006-11-14 16:12:23 -0500194 if (status != 0)
195 return status;
196
Fred Isamancd841602012-04-20 14:47:44 -0400197 nfs_add_stats(inode, NFSIOS_SERVERREADBYTES, data->res.count);
Trond Myklebust0b671302006-11-14 16:12:23 -0500198
199 if (task->tk_status == -ESTALE) {
Fred Isamancd841602012-04-20 14:47:44 -0400200 set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
201 nfs_mark_for_revalidate(inode);
Trond Myklebust0b671302006-11-14 16:12:23 -0500202 }
Trond Myklebust0b671302006-11-14 16:12:23 -0500203 return 0;
204}
205
Anna Schumaker9c7e1b32014-05-06 09:12:26 -0400206static void nfs_readpage_retry(struct rpc_task *task, struct nfs_pgio_data *data)
Trond Myklebust0b671302006-11-14 16:12:23 -0500207{
Anna Schumaker3c6b8992014-05-06 09:12:24 -0400208 struct nfs_pgio_args *argp = &data->args;
Anna Schumaker9137bdf2014-05-06 09:12:25 -0400209 struct nfs_pgio_res *resp = &data->res;
Trond Myklebust0b671302006-11-14 16:12:23 -0500210
Trond Myklebust0b671302006-11-14 16:12:23 -0500211 /* This is a short read! */
Fred Isamancd841602012-04-20 14:47:44 -0400212 nfs_inc_stats(data->header->inode, NFSIOS_SHORTREAD);
Trond Myklebust0b671302006-11-14 16:12:23 -0500213 /* Has the server at least made some progress? */
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400214 if (resp->count == 0) {
215 nfs_set_pgio_error(data->header, -EIO, argp->offset);
Trond Myklebustd61e6122009-12-05 19:32:19 -0500216 return;
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400217 }
Trond Myklebust0b671302006-11-14 16:12:23 -0500218 /* Yes, so retry the read at the end of the data */
Andy Adamsoncbdabc72011-03-01 01:34:20 +0000219 data->mds_offset += resp->count;
Trond Myklebust0b671302006-11-14 16:12:23 -0500220 argp->offset += resp->count;
221 argp->pgbase += resp->count;
222 argp->count -= resp->count;
Trond Myklebustd00c5d42011-10-19 12:17:29 -0700223 rpc_restart_call_prepare(task);
Trond Myklebust0b671302006-11-14 16:12:23 -0500224}
225
Anna Schumaker0eecb212014-05-06 09:12:32 -0400226static void nfs_readpage_result(struct rpc_task *task, struct nfs_pgio_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400228 struct nfs_pgio_header *hdr = data->header;
229
Anna Schumaker0eecb212014-05-06 09:12:32 -0400230 if (data->res.eof) {
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400231 loff_t bound;
Trond Myklebust0b671302006-11-14 16:12:23 -0500232
Fred Isaman4db6e0b2012-04-20 14:47:46 -0400233 bound = data->args.offset + data->res.count;
234 spin_lock(&hdr->lock);
235 if (bound < hdr->io_start + hdr->good_bytes) {
236 set_bit(NFS_IOHDR_EOF, &hdr->flags);
237 clear_bit(NFS_IOHDR_ERROR, &hdr->flags);
238 hdr->good_bytes = bound - hdr->io_start;
239 }
240 spin_unlock(&hdr->lock);
241 } else if (data->res.count != data->args.count)
242 nfs_readpage_retry(task, data);
Trond Myklebustfdd1e742008-04-15 16:33:58 -0400243}
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 * Read a page over NFS.
247 * We read the page synchronously in the following case:
248 * - The error flag is set for this page. This happens only when a
249 * previous async read operation failed.
250 */
251int nfs_readpage(struct file *file, struct page *page)
252{
253 struct nfs_open_context *ctx;
Mel Gormand56b4dd2012-07-31 16:45:06 -0700254 struct inode *inode = page_file_mapping(page)->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 int error;
256
257 dprintk("NFS: nfs_readpage (%p %ld@%lu)\n",
Mel Gormand56b4dd2012-07-31 16:45:06 -0700258 page, PAGE_CACHE_SIZE, page_file_index(page));
Chuck Lever91d5b472006-03-20 13:44:14 -0500259 nfs_inc_stats(inode, NFSIOS_VFSREADPAGE);
260 nfs_add_stats(inode, NFSIOS_READPAGES, 1);
261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 /*
263 * Try to flush any pending writes to the file..
264 *
265 * NOTE! Because we own the page lock, there cannot
266 * be any new pending writes generated at this point
267 * for this page (other pages can be written to).
268 */
269 error = nfs_wb_page(inode, page);
270 if (error)
Trond Myklebustde05a0c2007-05-20 13:05:05 -0400271 goto out_unlock;
272 if (PageUptodate(page))
273 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Trond Myklebust5f004cf2006-09-14 14:03:14 -0400275 error = -ESTALE;
276 if (NFS_STALE(inode))
Trond Myklebustde05a0c2007-05-20 13:05:05 -0400277 goto out_unlock;
Trond Myklebust5f004cf2006-09-14 14:03:14 -0400278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 if (file == NULL) {
Trond Myklebustcf1308f2006-11-19 16:44:52 -0500280 error = -EBADF;
Trond Myklebustd5308382005-11-04 15:33:38 -0500281 ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 if (ctx == NULL)
Trond Myklebustde05a0c2007-05-20 13:05:05 -0400283 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 } else
Trond Myklebustcd3758e2007-08-10 17:44:32 -0400285 ctx = get_nfs_open_context(nfs_file_open_context(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
David Howells9a9fc1c2009-04-03 16:42:44 +0100287 if (!IS_SYNC(inode)) {
288 error = nfs_readpage_from_fscache(ctx, inode, page);
289 if (error == 0)
290 goto out;
291 }
292
Trond Myklebust8e0969f2006-12-13 15:23:44 -0500293 error = nfs_readpage_async(ctx, inode, page);
294
David Howells9a9fc1c2009-04-03 16:42:44 +0100295out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 put_nfs_open_context(ctx);
297 return error;
Trond Myklebustde05a0c2007-05-20 13:05:05 -0400298out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 unlock_page(page);
300 return error;
301}
302
303struct nfs_readdesc {
Trond Myklebust8b09bee2007-04-02 18:48:28 -0400304 struct nfs_pageio_descriptor *pgio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 struct nfs_open_context *ctx;
306};
307
308static int
309readpage_async_filler(void *data, struct page *page)
310{
311 struct nfs_readdesc *desc = (struct nfs_readdesc *)data;
Mel Gormand56b4dd2012-07-31 16:45:06 -0700312 struct inode *inode = page_file_mapping(page)->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 struct nfs_page *new;
314 unsigned int len;
Trond Myklebustde05a0c2007-05-20 13:05:05 -0400315 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Trond Myklebust49a70f22006-12-05 00:35:38 -0500317 len = nfs_page_length(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 if (len == 0)
319 return nfs_return_empty_page(page);
Trond Myklebustde05a0c2007-05-20 13:05:05 -0400320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 new = nfs_create_request(desc->ctx, inode, page, 0, len);
Trond Myklebustde05a0c2007-05-20 13:05:05 -0400322 if (IS_ERR(new))
323 goto out_error;
324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 if (len < PAGE_CACHE_SIZE)
Christoph Lametereebd2aa2008-02-04 22:28:29 -0800326 zero_user_segment(page, len, PAGE_CACHE_SIZE);
Fred Isamanf8512ad2008-03-19 11:24:39 -0400327 if (!nfs_pageio_add_request(desc->pgio, new)) {
328 error = desc->pgio->pg_error;
329 goto out_unlock;
330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 return 0;
Trond Myklebustde05a0c2007-05-20 13:05:05 -0400332out_error:
333 error = PTR_ERR(new);
Trond Myklebustde05a0c2007-05-20 13:05:05 -0400334out_unlock:
335 unlock_page(page);
336 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337}
338
339int nfs_readpages(struct file *filp, struct address_space *mapping,
340 struct list_head *pages, unsigned nr_pages)
341{
Trond Myklebust8b09bee2007-04-02 18:48:28 -0400342 struct nfs_pageio_descriptor pgio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 struct nfs_readdesc desc = {
Trond Myklebust8b09bee2007-04-02 18:48:28 -0400344 .pgio = &pgio,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 };
346 struct inode *inode = mapping->host;
Trond Myklebust8b09bee2007-04-02 18:48:28 -0400347 unsigned long npages;
Trond Myklebust5f004cf2006-09-14 14:03:14 -0400348 int ret = -ESTALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Niels de Vos1e8968c2013-12-17 18:20:16 +0100350 dprintk("NFS: nfs_readpages (%s/%Lu %d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 inode->i_sb->s_id,
Niels de Vos1e8968c2013-12-17 18:20:16 +0100352 (unsigned long long)NFS_FILEID(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 nr_pages);
Chuck Lever91d5b472006-03-20 13:44:14 -0500354 nfs_inc_stats(inode, NFSIOS_VFSREADPAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Trond Myklebust5f004cf2006-09-14 14:03:14 -0400356 if (NFS_STALE(inode))
357 goto out;
358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 if (filp == NULL) {
Trond Myklebustd5308382005-11-04 15:33:38 -0500360 desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 if (desc.ctx == NULL)
362 return -EBADF;
363 } else
Trond Myklebustcd3758e2007-08-10 17:44:32 -0400364 desc.ctx = get_nfs_open_context(nfs_file_open_context(filp));
David Howells9a9fc1c2009-04-03 16:42:44 +0100365
366 /* attempt to read as many of the pages as possible from the cache
367 * - this returns -ENOBUFS immediately if the cookie is negative
368 */
369 ret = nfs_readpages_from_fscache(desc.ctx, inode, mapping,
370 pages, &nr_pages);
371 if (ret == 0)
372 goto read_complete; /* all pages were read */
373
Christoph Hellwigfab5fc22014-04-16 15:07:22 +0200374 nfs_pageio_init_read(&pgio, inode, false,
375 &nfs_async_read_completion_ops);
Trond Myklebust8b09bee2007-04-02 18:48:28 -0400376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 ret = read_cache_pages(mapping, pages, readpage_async_filler, &desc);
Trond Myklebust8b09bee2007-04-02 18:48:28 -0400378
379 nfs_pageio_complete(&pgio);
Andy Adamson2701d082012-05-24 13:13:24 -0400380 NFS_I(inode)->read_io += pgio.pg_bytes_written;
Trond Myklebust8b09bee2007-04-02 18:48:28 -0400381 npages = (pgio.pg_bytes_written + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
382 nfs_add_stats(inode, NFSIOS_READPAGES, npages);
David Howells9a9fc1c2009-04-03 16:42:44 +0100383read_complete:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 put_nfs_open_context(desc.ctx);
Trond Myklebust5f004cf2006-09-14 14:03:14 -0400385out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 return ret;
387}
388
David Howellsf7b422b2006-06-09 09:34:33 -0400389int __init nfs_init_readpagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
391 nfs_rdata_cachep = kmem_cache_create("nfs_read_data",
Anna Schumakerc0752cd2014-05-06 09:12:27 -0400392 sizeof(struct nfs_rw_header),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 0, SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +0900394 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 if (nfs_rdata_cachep == NULL)
396 return -ENOMEM;
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 return 0;
399}
400
David Brownell266bee82006-06-27 12:59:15 -0700401void nfs_destroy_readpagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -0700403 kmem_cache_destroy(nfs_rdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404}
Anna Schumaker4a0de552014-05-06 09:12:30 -0400405
406static const struct nfs_rw_ops nfs_rw_read_ops = {
Anna Schumakera4cdda52014-05-06 09:12:31 -0400407 .rw_mode = FMODE_READ,
Anna Schumaker4a0de552014-05-06 09:12:30 -0400408 .rw_alloc_header = nfs_readhdr_alloc,
409 .rw_free_header = nfs_readhdr_free,
Anna Schumaker0eecb212014-05-06 09:12:32 -0400410 .rw_done = nfs_readpage_done,
411 .rw_result = nfs_readpage_result,
Anna Schumaker1ed26f32014-05-06 09:12:37 -0400412 .rw_initiate = nfs_initiate_read,
Anna Schumaker4a0de552014-05-06 09:12:30 -0400413};