]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/nfs/read.c
nfs: add support for multiple nfs reqs per page
[mirror_ubuntu-bionic-kernel.git] / fs / nfs / read.c
CommitLineData
1da177e4
LT
1/*
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
1da177e4
LT
8 */
9
1da177e4
LT
10#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>
64419a9b 21#include <linux/module.h>
1da177e4 22
f11c88af 23#include "nfs4_fs.h"
49a70f27 24#include "internal.h"
91d5b470 25#include "iostat.h"
9a9fc1c0 26#include "fscache.h"
fab5fc25 27#include "pnfs.h"
91d5b470 28
1da177e4
LT
29#define NFSDBG_FACILITY NFSDBG_PAGECACHE
30
061ae2ed 31static const struct nfs_pgio_completion_ops nfs_async_read_completion_ops;
4a0de55c 32static const struct nfs_rw_ops nfs_rw_read_ops;
1da177e4 33
e18b890b 34static struct kmem_cache *nfs_rdata_cachep;
1da177e4 35
4a0de55c 36static struct nfs_rw_header *nfs_readhdr_alloc(void)
3feb2d49 37{
4a0de55c 38 return kmem_cache_zalloc(nfs_rdata_cachep, GFP_KERNEL);
4db6e0b7
FI
39}
40
4a0de55c 41static void nfs_readhdr_free(struct nfs_rw_header *rhdr)
3feb2d49 42{
cd841605 43 kmem_cache_free(nfs_rdata_cachep, rhdr);
3feb2d49
TM
44}
45
1da177e4
LT
46static
47int nfs_return_empty_page(struct page *page)
48{
eebd2aa3 49 zero_user(page, 0, PAGE_CACHE_SIZE);
1da177e4
LT
50 SetPageUptodate(page);
51 unlock_page(page);
52 return 0;
53}
54
1abb5088 55void nfs_pageio_init_read(struct nfs_pageio_descriptor *pgio,
fab5fc25 56 struct inode *inode, bool force_mds,
061ae2ed 57 const struct nfs_pgio_completion_ops *compl_ops)
1751c363 58{
fab5fc25 59 struct nfs_server *server = NFS_SERVER(inode);
41d8d5b7 60 const struct nfs_pageio_ops *pg_ops = &nfs_pgio_rw_ops;
fab5fc25
CH
61
62#ifdef CONFIG_NFS_V4_1
63 if (server->pnfs_curr_ld && !force_mds)
64 pg_ops = server->pnfs_curr_ld->pg_read_ops;
65#endif
4a0de55c
AS
66 nfs_pageio_init(pgio, inode, pg_ops, compl_ops, &nfs_rw_read_ops,
67 server->rsize, 0);
1751c363 68}
ddda8e0a 69EXPORT_SYMBOL_GPL(nfs_pageio_init_read);
1751c363 70
493292dd
TM
71void nfs_pageio_reset_read_mds(struct nfs_pageio_descriptor *pgio)
72{
41d8d5b7 73 pgio->pg_ops = &nfs_pgio_rw_ops;
493292dd
TM
74 pgio->pg_bsize = NFS_SERVER(pgio->pg_inode)->rsize;
75}
1f945357 76EXPORT_SYMBOL_GPL(nfs_pageio_reset_read_mds);
493292dd 77
f42b293d
DH
78int nfs_readpage_async(struct nfs_open_context *ctx, struct inode *inode,
79 struct page *page)
1da177e4 80{
1da177e4
LT
81 struct nfs_page *new;
82 unsigned int len;
c76069bd 83 struct nfs_pageio_descriptor pgio;
1da177e4 84
49a70f27 85 len = nfs_page_length(page);
1da177e4
LT
86 if (len == 0)
87 return nfs_return_empty_page(page);
2bfc6e56 88 new = nfs_create_request(ctx, page, NULL, 0, len);
1da177e4
LT
89 if (IS_ERR(new)) {
90 unlock_page(page);
91 return PTR_ERR(new);
92 }
93 if (len < PAGE_CACHE_SIZE)
eebd2aa3 94 zero_user_segment(page, len, PAGE_CACHE_SIZE);
1da177e4 95
fab5fc25
CH
96 nfs_pageio_init_read(&pgio, inode, false,
97 &nfs_async_read_completion_ops);
d8007d4d 98 nfs_pageio_add_request(&pgio, new);
1751c363 99 nfs_pageio_complete(&pgio);
2701d086 100 NFS_I(inode)->read_io += pgio.pg_bytes_written;
1da177e4
LT
101 return 0;
102}
103
104static void nfs_readpage_release(struct nfs_page *req)
105{
3d4ff43d 106 struct inode *d_inode = req->wb_context->dentry->d_inode;
7f8e05f6
DH
107
108 if (PageUptodate(req->wb_page))
109 nfs_readpage_to_fscache(d_inode, req->wb_page, 0);
110
1da177e4
LT
111 unlock_page(req->wb_page);
112
1e8968c5 113 dprintk("NFS: read done (%s/%Lu %d@%Ld)\n",
3d4ff43d 114 req->wb_context->dentry->d_inode->i_sb->s_id,
1e8968c5 115 (unsigned long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1da177e4
LT
116 req->wb_bytes,
117 (long long)req_offset(req));
10d2c46f 118 nfs_release_request(req);
1da177e4
LT
119}
120
4db6e0b7 121/* Note io was page aligned */
061ae2ed 122static void nfs_read_completion(struct nfs_pgio_header *hdr)
4db6e0b7
FI
123{
124 unsigned long bytes = 0;
125
126 if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
127 goto out;
4bd8b010
TM
128 while (!list_empty(&hdr->pages)) {
129 struct nfs_page *req = nfs_list_entry(hdr->pages.next);
130 struct page *page = req->wb_page;
131
132 if (test_bit(NFS_IOHDR_EOF, &hdr->flags)) {
133 if (bytes > hdr->good_bytes)
134 zero_user(page, 0, PAGE_SIZE);
135 else if (hdr->good_bytes - bytes < PAGE_SIZE)
136 zero_user_segment(page,
137 hdr->good_bytes & ~PAGE_MASK,
138 PAGE_SIZE);
4db6e0b7 139 }
4bd8b010
TM
140 bytes += req->wb_bytes;
141 if (test_bit(NFS_IOHDR_ERROR, &hdr->flags)) {
4db6e0b7 142 if (bytes <= hdr->good_bytes)
4bd8b010
TM
143 SetPageUptodate(page);
144 } else
145 SetPageUptodate(page);
146 nfs_list_remove_request(req);
147 nfs_readpage_release(req);
4db6e0b7
FI
148 }
149out:
150 hdr->release(hdr);
151}
152
1ed26f33
AS
153static void nfs_initiate_read(struct nfs_pgio_data *data, struct rpc_message *msg,
154 struct rpc_task_setup *task_setup_data, int how)
1da177e4 155{
cd841605 156 struct inode *inode = data->header->inode;
84115e1c 157 int swap_flags = IS_SWAPFILE(inode) ? NFS_RPC_SWAPFLAGS : 0;
1da177e4 158
1ed26f33
AS
159 task_setup_data->flags |= swap_flags;
160 NFS_PROTO(inode)->read_setup(data, msg);
64419a9b
AA
161}
162
061ae2ed 163static void
1da177e4
LT
164nfs_async_read_error(struct list_head *head)
165{
166 struct nfs_page *req;
167
168 while (!list_empty(head)) {
169 req = nfs_list_entry(head->next);
170 nfs_list_remove_request(req);
1da177e4
LT
171 nfs_readpage_release(req);
172 }
173}
174
061ae2ed
FI
175static const struct nfs_pgio_completion_ops nfs_async_read_completion_ops = {
176 .error_cleanup = nfs_async_read_error,
177 .completion = nfs_read_completion,
178};
179
0b671301
TM
180/*
181 * This is the callback from RPC telling us whether a reply was
182 * received or some error occurred (timeout or socket shutdown).
183 */
0eecb214
AS
184static int nfs_readpage_done(struct rpc_task *task, struct nfs_pgio_data *data,
185 struct inode *inode)
0b671301 186{
0eecb214 187 int status = NFS_PROTO(inode)->read_done(task, data);
0b671301
TM
188 if (status != 0)
189 return status;
190
cd841605 191 nfs_add_stats(inode, NFSIOS_SERVERREADBYTES, data->res.count);
0b671301
TM
192
193 if (task->tk_status == -ESTALE) {
cd841605
FI
194 set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
195 nfs_mark_for_revalidate(inode);
0b671301 196 }
0b671301
TM
197 return 0;
198}
199
9c7e1b3d 200static void nfs_readpage_retry(struct rpc_task *task, struct nfs_pgio_data *data)
0b671301 201{
3c6b899c 202 struct nfs_pgio_args *argp = &data->args;
9137bdf3 203 struct nfs_pgio_res *resp = &data->res;
0b671301 204
0b671301 205 /* This is a short read! */
cd841605 206 nfs_inc_stats(data->header->inode, NFSIOS_SHORTREAD);
0b671301 207 /* Has the server at least made some progress? */
4db6e0b7
FI
208 if (resp->count == 0) {
209 nfs_set_pgio_error(data->header, -EIO, argp->offset);
d61e612a 210 return;
4db6e0b7 211 }
0b671301 212 /* Yes, so retry the read at the end of the data */
cbdabc7f 213 data->mds_offset += resp->count;
0b671301
TM
214 argp->offset += resp->count;
215 argp->pgbase += resp->count;
216 argp->count -= resp->count;
d00c5d43 217 rpc_restart_call_prepare(task);
0b671301
TM
218}
219
0eecb214 220static void nfs_readpage_result(struct rpc_task *task, struct nfs_pgio_data *data)
1da177e4 221{
4db6e0b7
FI
222 struct nfs_pgio_header *hdr = data->header;
223
0eecb214 224 if (data->res.eof) {
4db6e0b7
FI
225 loff_t bound;
226
227 bound = data->args.offset + data->res.count;
228 spin_lock(&hdr->lock);
229 if (bound < hdr->io_start + hdr->good_bytes) {
230 set_bit(NFS_IOHDR_EOF, &hdr->flags);
231 clear_bit(NFS_IOHDR_ERROR, &hdr->flags);
232 hdr->good_bytes = bound - hdr->io_start;
233 }
234 spin_unlock(&hdr->lock);
235 } else if (data->res.count != data->args.count)
236 nfs_readpage_retry(task, data);
fdd1e74c
TM
237}
238
1da177e4
LT
239/*
240 * Read a page over NFS.
241 * We read the page synchronously in the following case:
242 * - The error flag is set for this page. This happens only when a
243 * previous async read operation failed.
244 */
245int nfs_readpage(struct file *file, struct page *page)
246{
247 struct nfs_open_context *ctx;
d56b4ddf 248 struct inode *inode = page_file_mapping(page)->host;
1da177e4
LT
249 int error;
250
251 dprintk("NFS: nfs_readpage (%p %ld@%lu)\n",
d56b4ddf 252 page, PAGE_CACHE_SIZE, page_file_index(page));
91d5b470
CL
253 nfs_inc_stats(inode, NFSIOS_VFSREADPAGE);
254 nfs_add_stats(inode, NFSIOS_READPAGES, 1);
255
1da177e4
LT
256 /*
257 * Try to flush any pending writes to the file..
258 *
259 * NOTE! Because we own the page lock, there cannot
260 * be any new pending writes generated at this point
261 * for this page (other pages can be written to).
262 */
263 error = nfs_wb_page(inode, page);
264 if (error)
de05a0cc
TM
265 goto out_unlock;
266 if (PageUptodate(page))
267 goto out_unlock;
1da177e4 268
5f004cf2
TM
269 error = -ESTALE;
270 if (NFS_STALE(inode))
de05a0cc 271 goto out_unlock;
5f004cf2 272
1da177e4 273 if (file == NULL) {
cf1308ff 274 error = -EBADF;
d530838b 275 ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
1da177e4 276 if (ctx == NULL)
de05a0cc 277 goto out_unlock;
1da177e4 278 } else
cd3758e3 279 ctx = get_nfs_open_context(nfs_file_open_context(file));
1da177e4 280
9a9fc1c0
DH
281 if (!IS_SYNC(inode)) {
282 error = nfs_readpage_from_fscache(ctx, inode, page);
283 if (error == 0)
284 goto out;
285 }
286
8e0969f0
TM
287 error = nfs_readpage_async(ctx, inode, page);
288
9a9fc1c0 289out:
1da177e4
LT
290 put_nfs_open_context(ctx);
291 return error;
de05a0cc 292out_unlock:
1da177e4
LT
293 unlock_page(page);
294 return error;
295}
296
297struct nfs_readdesc {
8b09bee3 298 struct nfs_pageio_descriptor *pgio;
1da177e4
LT
299 struct nfs_open_context *ctx;
300};
301
302static int
303readpage_async_filler(void *data, struct page *page)
304{
305 struct nfs_readdesc *desc = (struct nfs_readdesc *)data;
1da177e4
LT
306 struct nfs_page *new;
307 unsigned int len;
de05a0cc
TM
308 int error;
309
49a70f27 310 len = nfs_page_length(page);
1da177e4
LT
311 if (len == 0)
312 return nfs_return_empty_page(page);
de05a0cc 313
2bfc6e56 314 new = nfs_create_request(desc->ctx, page, NULL, 0, len);
de05a0cc
TM
315 if (IS_ERR(new))
316 goto out_error;
317
1da177e4 318 if (len < PAGE_CACHE_SIZE)
eebd2aa3 319 zero_user_segment(page, len, PAGE_CACHE_SIZE);
f8512ad0
FI
320 if (!nfs_pageio_add_request(desc->pgio, new)) {
321 error = desc->pgio->pg_error;
322 goto out_unlock;
323 }
1da177e4 324 return 0;
de05a0cc
TM
325out_error:
326 error = PTR_ERR(new);
de05a0cc
TM
327out_unlock:
328 unlock_page(page);
329 return error;
1da177e4
LT
330}
331
332int nfs_readpages(struct file *filp, struct address_space *mapping,
333 struct list_head *pages, unsigned nr_pages)
334{
8b09bee3 335 struct nfs_pageio_descriptor pgio;
1da177e4 336 struct nfs_readdesc desc = {
8b09bee3 337 .pgio = &pgio,
1da177e4
LT
338 };
339 struct inode *inode = mapping->host;
8b09bee3 340 unsigned long npages;
5f004cf2 341 int ret = -ESTALE;
1da177e4 342
1e8968c5 343 dprintk("NFS: nfs_readpages (%s/%Lu %d)\n",
1da177e4 344 inode->i_sb->s_id,
1e8968c5 345 (unsigned long long)NFS_FILEID(inode),
1da177e4 346 nr_pages);
91d5b470 347 nfs_inc_stats(inode, NFSIOS_VFSREADPAGES);
1da177e4 348
5f004cf2
TM
349 if (NFS_STALE(inode))
350 goto out;
351
1da177e4 352 if (filp == NULL) {
d530838b 353 desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
1da177e4
LT
354 if (desc.ctx == NULL)
355 return -EBADF;
356 } else
cd3758e3 357 desc.ctx = get_nfs_open_context(nfs_file_open_context(filp));
9a9fc1c0
DH
358
359 /* attempt to read as many of the pages as possible from the cache
360 * - this returns -ENOBUFS immediately if the cookie is negative
361 */
362 ret = nfs_readpages_from_fscache(desc.ctx, inode, mapping,
363 pages, &nr_pages);
364 if (ret == 0)
365 goto read_complete; /* all pages were read */
366
fab5fc25
CH
367 nfs_pageio_init_read(&pgio, inode, false,
368 &nfs_async_read_completion_ops);
8b09bee3 369
1da177e4 370 ret = read_cache_pages(mapping, pages, readpage_async_filler, &desc);
8b09bee3
TM
371
372 nfs_pageio_complete(&pgio);
2701d086 373 NFS_I(inode)->read_io += pgio.pg_bytes_written;
8b09bee3
TM
374 npages = (pgio.pg_bytes_written + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
375 nfs_add_stats(inode, NFSIOS_READPAGES, npages);
9a9fc1c0 376read_complete:
1da177e4 377 put_nfs_open_context(desc.ctx);
5f004cf2 378out:
1da177e4
LT
379 return ret;
380}
381
f7b422b1 382int __init nfs_init_readpagecache(void)
1da177e4
LT
383{
384 nfs_rdata_cachep = kmem_cache_create("nfs_read_data",
c0752cdf 385 sizeof(struct nfs_rw_header),
1da177e4 386 0, SLAB_HWCACHE_ALIGN,
20c2df83 387 NULL);
1da177e4
LT
388 if (nfs_rdata_cachep == NULL)
389 return -ENOMEM;
390
1da177e4
LT
391 return 0;
392}
393
266bee88 394void nfs_destroy_readpagecache(void)
1da177e4 395{
1a1d92c1 396 kmem_cache_destroy(nfs_rdata_cachep);
1da177e4 397}
4a0de55c
AS
398
399static const struct nfs_rw_ops nfs_rw_read_ops = {
a4cdda59 400 .rw_mode = FMODE_READ,
4a0de55c
AS
401 .rw_alloc_header = nfs_readhdr_alloc,
402 .rw_free_header = nfs_readhdr_free,
0eecb214
AS
403 .rw_done = nfs_readpage_done,
404 .rw_result = nfs_readpage_result,
1ed26f33 405 .rw_initiate = nfs_initiate_read,
4a0de55c 406};