blob: cca9fa259994440b637a7c5993241b17fe43ce20 [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
8 *
9 * We do an ugly hack here in order to return proper error codes to the
10 * user program when a read request failed: since generic_file_read
11 * only checks the return value of inode->i_op->readpage() which is always 0
12 * for async RPC, we set the error bit of the page to 1 when an error occurs,
13 * and make nfs_readpage transmit requests synchronously when encountering this.
14 * This is only a small problem, though, since we now retry all operations
15 * within the RPC code when root squashing is suspected.
16 */
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/time.h>
19#include <linux/kernel.h>
20#include <linux/errno.h>
21#include <linux/fcntl.h>
22#include <linux/stat.h>
23#include <linux/mm.h>
24#include <linux/slab.h>
25#include <linux/pagemap.h>
26#include <linux/sunrpc/clnt.h>
27#include <linux/nfs_fs.h>
28#include <linux/nfs_page.h>
29#include <linux/smp_lock.h>
30
31#include <asm/system.h>
32
Chuck Lever91d5b472006-03-20 13:44:14 -050033#include "iostat.h"
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#define NFSDBG_FACILITY NFSDBG_PAGECACHE
36
37static int nfs_pagein_one(struct list_head *, struct inode *);
Trond Myklebustec06c092006-03-20 13:44:27 -050038static const struct rpc_call_ops nfs_read_partial_ops;
39static const struct rpc_call_ops nfs_read_full_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41static kmem_cache_t *nfs_rdata_cachep;
Trond Myklebust3feb2d42006-03-20 13:44:37 -050042static mempool_t *nfs_rdata_mempool;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44#define MIN_POOL_READ (32)
45
Trond Myklebuste9f7bee2006-09-08 09:48:54 -070046struct nfs_read_data *nfs_readdata_alloc(size_t len)
Trond Myklebust3feb2d42006-03-20 13:44:37 -050047{
Trond Myklebuste9f7bee2006-09-08 09:48:54 -070048 unsigned int pagecount = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
Trond Myklebust3feb2d42006-03-20 13:44:37 -050049 struct nfs_read_data *p = mempool_alloc(nfs_rdata_mempool, SLAB_NOFS);
50
51 if (p) {
52 memset(p, 0, sizeof(*p));
53 INIT_LIST_HEAD(&p->pages);
Trond Myklebuste9f7bee2006-09-08 09:48:54 -070054 p->npages = pagecount;
Chuck Lever0d0b5cb2006-05-25 01:40:53 -040055 if (pagecount <= ARRAY_SIZE(p->page_array))
56 p->pagevec = p->page_array;
Trond Myklebust3feb2d42006-03-20 13:44:37 -050057 else {
Chuck Lever0d0b5cb2006-05-25 01:40:53 -040058 p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
59 if (!p->pagevec) {
Trond Myklebust3feb2d42006-03-20 13:44:37 -050060 mempool_free(p, nfs_rdata_mempool);
61 p = NULL;
62 }
63 }
64 }
65 return p;
66}
67
Trond Myklebust8aca67f2006-11-13 16:23:44 -050068static void nfs_readdata_rcu_free(struct rcu_head *head)
Trond Myklebust3feb2d42006-03-20 13:44:37 -050069{
Trond Myklebust8aca67f2006-11-13 16:23:44 -050070 struct nfs_read_data *p = container_of(head, struct nfs_read_data, task.u.tk_rcu);
Trond Myklebust3feb2d42006-03-20 13:44:37 -050071 if (p && (p->pagevec != &p->page_array[0]))
72 kfree(p->pagevec);
73 mempool_free(p, nfs_rdata_mempool);
74}
75
Trond Myklebust8aca67f2006-11-13 16:23:44 -050076static void nfs_readdata_free(struct nfs_read_data *rdata)
77{
78 call_rcu_bh(&rdata->task.u.tk_rcu, nfs_readdata_rcu_free);
79}
80
Trond Myklebust963d8fe2006-01-03 09:55:04 +010081void nfs_readdata_release(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 nfs_readdata_free(data);
84}
85
86static
87unsigned int nfs_page_length(struct inode *inode, struct page *page)
88{
89 loff_t i_size = i_size_read(inode);
90 unsigned long idx;
91
92 if (i_size <= 0)
93 return 0;
94 idx = (i_size - 1) >> PAGE_CACHE_SHIFT;
95 if (page->index > idx)
96 return 0;
97 if (page->index != idx)
98 return PAGE_CACHE_SIZE;
99 return 1 + ((i_size - 1) & (PAGE_CACHE_SIZE - 1));
100}
101
102static
103int nfs_return_empty_page(struct page *page)
104{
105 memclear_highpage_flush(page, 0, PAGE_CACHE_SIZE);
106 SetPageUptodate(page);
107 unlock_page(page);
108 return 0;
109}
110
Trond Myklebust1de3fc12006-05-25 01:40:44 -0400111static void nfs_readpage_truncate_uninitialised_page(struct nfs_read_data *data)
112{
113 unsigned int remainder = data->args.count - data->res.count;
114 unsigned int base = data->args.pgbase + data->res.count;
115 unsigned int pglen;
116 struct page **pages;
117
118 if (data->res.eof == 0 || remainder == 0)
119 return;
120 /*
121 * Note: "remainder" can never be negative, since we check for
122 * this in the XDR code.
123 */
124 pages = &data->args.pages[base >> PAGE_CACHE_SHIFT];
125 base &= ~PAGE_CACHE_MASK;
126 pglen = PAGE_CACHE_SIZE - base;
Trond Myklebust79558f32006-08-22 13:44:32 -0400127 for (;;) {
128 if (remainder <= pglen) {
129 memclear_highpage_flush(*pages, base, remainder);
130 break;
131 }
Trond Myklebust1de3fc12006-05-25 01:40:44 -0400132 memclear_highpage_flush(*pages, base, pglen);
Trond Myklebust79558f32006-08-22 13:44:32 -0400133 pages++;
134 remainder -= pglen;
135 pglen = PAGE_CACHE_SIZE;
136 base = 0;
137 }
Trond Myklebust1de3fc12006-05-25 01:40:44 -0400138}
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140/*
141 * Read a page synchronously.
142 */
143static int nfs_readpage_sync(struct nfs_open_context *ctx, struct inode *inode,
144 struct page *page)
145{
146 unsigned int rsize = NFS_SERVER(inode)->rsize;
147 unsigned int count = PAGE_CACHE_SIZE;
Trond Myklebustcf1308f2006-11-19 16:44:52 -0500148 int result = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 struct nfs_read_data *rdata;
150
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700151 rdata = nfs_readdata_alloc(count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 if (!rdata)
Trond Myklebustcf1308f2006-11-19 16:44:52 -0500153 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155 memset(rdata, 0, sizeof(*rdata));
156 rdata->flags = (IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0);
157 rdata->cred = ctx->cred;
158 rdata->inode = inode;
159 INIT_LIST_HEAD(&rdata->pages);
160 rdata->args.fh = NFS_FH(inode);
161 rdata->args.context = ctx;
162 rdata->args.pages = &page;
163 rdata->args.pgbase = 0UL;
164 rdata->args.count = rsize;
165 rdata->res.fattr = &rdata->fattr;
166
167 dprintk("NFS: nfs_readpage_sync(%p)\n", page);
168
169 /*
170 * This works now because the socket layer never tries to DMA
171 * into this buffer directly.
172 */
173 do {
174 if (count < rsize)
175 rdata->args.count = count;
176 rdata->res.count = rdata->args.count;
177 rdata->args.offset = page_offset(page) + rdata->args.pgbase;
178
179 dprintk("NFS: nfs_proc_read(%s, (%s/%Ld), %Lu, %u)\n",
David Howells54ceac42006-08-22 20:06:13 -0400180 NFS_SERVER(inode)->nfs_client->cl_hostname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 inode->i_sb->s_id,
182 (long long)NFS_FILEID(inode),
183 (unsigned long long)rdata->args.pgbase,
184 rdata->args.count);
185
186 lock_kernel();
187 result = NFS_PROTO(inode)->read(rdata);
188 unlock_kernel();
189
190 /*
191 * Even if we had a partial success we can't mark the page
192 * cache valid.
193 */
194 if (result < 0) {
195 if (result == -EISDIR)
196 result = -EINVAL;
197 goto io_error;
198 }
199 count -= result;
200 rdata->args.pgbase += result;
Chuck Lever91d5b472006-03-20 13:44:14 -0500201 nfs_add_stats(inode, NFSIOS_SERVERREADBYTES, result);
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 /* Note: result == 0 should only happen if we're caching
204 * a write that extends the file and punches a hole.
205 */
206 if (rdata->res.eof != 0 || result == 0)
207 break;
208 } while (count);
Chuck Leverdc592502005-08-18 11:24:12 -0700209 spin_lock(&inode->i_lock);
Chuck Lever55296802005-08-18 11:24:09 -0700210 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATIME;
Chuck Leverdc592502005-08-18 11:24:12 -0700211 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Trond Myklebust7a524112006-09-15 16:03:45 -0400213 if (rdata->res.eof || rdata->res.count == rdata->args.count) {
Trond Myklebust1de3fc12006-05-25 01:40:44 -0400214 SetPageUptodate(page);
Trond Myklebust7a524112006-09-15 16:03:45 -0400215 if (rdata->res.eof && count != 0)
216 memclear_highpage_flush(page, rdata->args.pgbase, count);
217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 result = 0;
219
220io_error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 nfs_readdata_free(rdata);
Trond Myklebustcf1308f2006-11-19 16:44:52 -0500222out_unlock:
223 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 return result;
225}
226
227static int nfs_readpage_async(struct nfs_open_context *ctx, struct inode *inode,
228 struct page *page)
229{
230 LIST_HEAD(one_request);
231 struct nfs_page *new;
232 unsigned int len;
233
234 len = nfs_page_length(inode, page);
235 if (len == 0)
236 return nfs_return_empty_page(page);
237 new = nfs_create_request(ctx, inode, page, 0, len);
238 if (IS_ERR(new)) {
239 unlock_page(page);
240 return PTR_ERR(new);
241 }
242 if (len < PAGE_CACHE_SIZE)
243 memclear_highpage_flush(page, len, PAGE_CACHE_SIZE - len);
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 nfs_list_add_request(new, &one_request);
246 nfs_pagein_one(&one_request, inode);
247 return 0;
248}
249
250static void nfs_readpage_release(struct nfs_page *req)
251{
252 unlock_page(req->wb_page);
253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 dprintk("NFS: read done (%s/%Ld %d@%Ld)\n",
255 req->wb_context->dentry->d_inode->i_sb->s_id,
256 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
257 req->wb_bytes,
258 (long long)req_offset(req));
Nick Wilson10d2c462005-09-22 21:44:28 -0700259 nfs_clear_request(req);
260 nfs_release_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
262
263/*
264 * Set up the NFS read request struct
265 */
266static void nfs_read_rpcsetup(struct nfs_page *req, struct nfs_read_data *data,
Trond Myklebustec06c092006-03-20 13:44:27 -0500267 const struct rpc_call_ops *call_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 unsigned int count, unsigned int offset)
269{
270 struct inode *inode;
Trond Myklebustec06c092006-03-20 13:44:27 -0500271 int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273 data->req = req;
274 data->inode = inode = req->wb_context->dentry->d_inode;
275 data->cred = req->wb_context->cred;
276
277 data->args.fh = NFS_FH(inode);
278 data->args.offset = req_offset(req) + offset;
279 data->args.pgbase = req->wb_pgbase + offset;
280 data->args.pages = data->pagevec;
281 data->args.count = count;
282 data->args.context = req->wb_context;
283
284 data->res.fattr = &data->fattr;
285 data->res.count = count;
286 data->res.eof = 0;
Trond Myklebust0e574af2005-10-27 22:12:38 -0400287 nfs_fattr_init(&data->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Trond Myklebustec06c092006-03-20 13:44:27 -0500289 /* Set up the initial task struct. */
290 flags = RPC_TASK_ASYNC | (IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0);
291 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, call_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 NFS_PROTO(inode)->read_setup(data);
293
294 data->task.tk_cookie = (unsigned long)inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
296 dprintk("NFS: %4d initiated read call (req %s/%Ld, %u bytes @ offset %Lu)\n",
297 data->task.tk_pid,
298 inode->i_sb->s_id,
299 (long long)NFS_FILEID(inode),
300 count,
301 (unsigned long long)data->args.offset);
302}
303
304static void
305nfs_async_read_error(struct list_head *head)
306{
307 struct nfs_page *req;
308
309 while (!list_empty(head)) {
310 req = nfs_list_entry(head->next);
311 nfs_list_remove_request(req);
312 SetPageError(req->wb_page);
313 nfs_readpage_release(req);
314 }
315}
316
317/*
318 * Start an async read operation
319 */
320static void nfs_execute_read(struct nfs_read_data *data)
321{
322 struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
323 sigset_t oldset;
324
325 rpc_clnt_sigmask(clnt, &oldset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 rpc_execute(&data->task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 rpc_clnt_sigunmask(clnt, &oldset);
328}
329
330/*
331 * Generate multiple requests to fill a single page.
332 *
333 * We optimize to reduce the number of read operations on the wire. If we
334 * detect that we're reading a page, or an area of a page, that is past the
335 * end of file, we do not generate NFS read operations but just clear the
336 * parts of the page that would have come back zero from the server anyway.
337 *
338 * We rely on the cached value of i_size to make this determination; another
339 * client can fill pages on the server past our cached end-of-file, but we
340 * won't see the new data until our attribute cache is updated. This is more
341 * or less conventional NFS client behavior.
342 */
343static int nfs_pagein_multi(struct list_head *head, struct inode *inode)
344{
345 struct nfs_page *req = nfs_list_entry(head->next);
346 struct page *page = req->wb_page;
347 struct nfs_read_data *data;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700348 size_t rsize = NFS_SERVER(inode)->rsize, nbytes;
349 unsigned int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 int requests = 0;
351 LIST_HEAD(list);
352
353 nfs_list_remove_request(req);
354
355 nbytes = req->wb_bytes;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700356 do {
357 size_t len = min(nbytes,rsize);
358
359 data = nfs_readdata_alloc(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 if (!data)
361 goto out_bad;
362 INIT_LIST_HEAD(&data->pages);
363 list_add(&data->pages, &list);
364 requests++;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700365 nbytes -= len;
366 } while(nbytes != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 atomic_set(&req->wb_complete, requests);
368
369 ClearPageError(page);
370 offset = 0;
371 nbytes = req->wb_bytes;
372 do {
373 data = list_entry(list.next, struct nfs_read_data, pages);
374 list_del_init(&data->pages);
375
376 data->pagevec[0] = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
378 if (nbytes > rsize) {
Trond Myklebustec06c092006-03-20 13:44:27 -0500379 nfs_read_rpcsetup(req, data, &nfs_read_partial_ops,
380 rsize, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 offset += rsize;
382 nbytes -= rsize;
383 } else {
Trond Myklebustec06c092006-03-20 13:44:27 -0500384 nfs_read_rpcsetup(req, data, &nfs_read_partial_ops,
385 nbytes, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 nbytes = 0;
387 }
388 nfs_execute_read(data);
389 } while (nbytes != 0);
390
391 return 0;
392
393out_bad:
394 while (!list_empty(&list)) {
395 data = list_entry(list.next, struct nfs_read_data, pages);
396 list_del(&data->pages);
397 nfs_readdata_free(data);
398 }
399 SetPageError(page);
400 nfs_readpage_release(req);
401 return -ENOMEM;
402}
403
404static int nfs_pagein_one(struct list_head *head, struct inode *inode)
405{
406 struct nfs_page *req;
407 struct page **pages;
408 struct nfs_read_data *data;
409 unsigned int count;
410
411 if (NFS_SERVER(inode)->rsize < PAGE_CACHE_SIZE)
412 return nfs_pagein_multi(head, inode);
413
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700414 data = nfs_readdata_alloc(NFS_SERVER(inode)->rsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 if (!data)
416 goto out_bad;
417
418 INIT_LIST_HEAD(&data->pages);
419 pages = data->pagevec;
420 count = 0;
421 while (!list_empty(head)) {
422 req = nfs_list_entry(head->next);
423 nfs_list_remove_request(req);
424 nfs_list_add_request(req, &data->pages);
425 ClearPageError(req->wb_page);
426 *pages++ = req->wb_page;
427 count += req->wb_bytes;
428 }
429 req = nfs_list_entry(data->pages.next);
430
Trond Myklebustec06c092006-03-20 13:44:27 -0500431 nfs_read_rpcsetup(req, data, &nfs_read_full_ops, count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433 nfs_execute_read(data);
434 return 0;
435out_bad:
436 nfs_async_read_error(head);
437 return -ENOMEM;
438}
439
440static int
441nfs_pagein_list(struct list_head *head, int rpages)
442{
443 LIST_HEAD(one_request);
444 struct nfs_page *req;
445 int error = 0;
446 unsigned int pages = 0;
447
448 while (!list_empty(head)) {
449 pages += nfs_coalesce_requests(head, &one_request, rpages);
450 req = nfs_list_entry(one_request.next);
451 error = nfs_pagein_one(&one_request, req->wb_context->dentry->d_inode);
452 if (error < 0)
453 break;
454 }
455 if (error >= 0)
456 return pages;
457
458 nfs_async_read_error(head);
459 return error;
460}
461
462/*
Trond Myklebust0b671302006-11-14 16:12:23 -0500463 * This is the callback from RPC telling us whether a reply was
464 * received or some error occurred (timeout or socket shutdown).
465 */
466int nfs_readpage_result(struct rpc_task *task, struct nfs_read_data *data)
467{
468 int status;
469
470 dprintk("%s: %4d, (status %d)\n", __FUNCTION__, task->tk_pid,
471 task->tk_status);
472
473 status = NFS_PROTO(data->inode)->read_done(task, data);
474 if (status != 0)
475 return status;
476
477 nfs_add_stats(data->inode, NFSIOS_SERVERREADBYTES, data->res.count);
478
479 if (task->tk_status == -ESTALE) {
480 set_bit(NFS_INO_STALE, &NFS_FLAGS(data->inode));
481 nfs_mark_for_revalidate(data->inode);
482 }
483 spin_lock(&data->inode->i_lock);
484 NFS_I(data->inode)->cache_validity |= NFS_INO_INVALID_ATIME;
485 spin_unlock(&data->inode->i_lock);
486 return 0;
487}
488
489static int nfs_readpage_retry(struct rpc_task *task, struct nfs_read_data *data)
490{
491 struct nfs_readargs *argp = &data->args;
492 struct nfs_readres *resp = &data->res;
493
494 if (resp->eof || resp->count == argp->count)
495 return 0;
496
497 /* This is a short read! */
498 nfs_inc_stats(data->inode, NFSIOS_SHORTREAD);
499 /* Has the server at least made some progress? */
500 if (resp->count == 0)
501 return 0;
502
503 /* Yes, so retry the read at the end of the data */
504 argp->offset += resp->count;
505 argp->pgbase += resp->count;
506 argp->count -= resp->count;
507 rpc_restart_call(task);
508 return -EAGAIN;
509}
510
511/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 * Handle a read reply that fills part of a page.
513 */
Trond Myklebustec06c092006-03-20 13:44:27 -0500514static void nfs_readpage_result_partial(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
Trond Myklebustec06c092006-03-20 13:44:27 -0500516 struct nfs_read_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 struct nfs_page *req = data->req;
518 struct page *page = req->wb_page;
519
Trond Myklebustec06c092006-03-20 13:44:27 -0500520 if (nfs_readpage_result(task, data) != 0)
521 return;
Trond Myklebust0b671302006-11-14 16:12:23 -0500522
523 if (likely(task->tk_status >= 0)) {
524 nfs_readpage_truncate_uninitialised_page(data);
525 if (nfs_readpage_retry(task, data) != 0)
526 return;
527 }
528 if (unlikely(task->tk_status < 0))
529 SetPageError(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 if (atomic_dec_and_test(&req->wb_complete)) {
531 if (!PageError(page))
532 SetPageUptodate(page);
533 nfs_readpage_release(req);
534 }
535}
536
Trond Myklebustec06c092006-03-20 13:44:27 -0500537static const struct rpc_call_ops nfs_read_partial_ops = {
538 .rpc_call_done = nfs_readpage_result_partial,
539 .rpc_release = nfs_readdata_release,
540};
541
Trond Myklebust1de3fc12006-05-25 01:40:44 -0400542static void nfs_readpage_set_pages_uptodate(struct nfs_read_data *data)
543{
544 unsigned int count = data->res.count;
545 unsigned int base = data->args.pgbase;
546 struct page **pages;
547
Trond Myklebust79558f32006-08-22 13:44:32 -0400548 if (data->res.eof)
549 count = data->args.count;
Trond Myklebust1de3fc12006-05-25 01:40:44 -0400550 if (unlikely(count == 0))
551 return;
552 pages = &data->args.pages[base >> PAGE_CACHE_SHIFT];
553 base &= ~PAGE_CACHE_MASK;
554 count += base;
555 for (;count >= PAGE_CACHE_SIZE; count -= PAGE_CACHE_SIZE, pages++)
556 SetPageUptodate(*pages);
Trond Myklebust0b671302006-11-14 16:12:23 -0500557 if (count == 0)
558 return;
559 /* Was this a short read? */
560 if (data->res.eof || data->res.count == data->args.count)
Trond Myklebust1de3fc12006-05-25 01:40:44 -0400561 SetPageUptodate(*pages);
562}
563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564/*
565 * This is the callback from RPC telling us whether a reply was
566 * received or some error occurred (timeout or socket shutdown).
567 */
Trond Myklebustec06c092006-03-20 13:44:27 -0500568static void nfs_readpage_result_full(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
Trond Myklebustec06c092006-03-20 13:44:27 -0500570 struct nfs_read_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Trond Myklebust0b671302006-11-14 16:12:23 -0500572 if (nfs_readpage_result(task, data) != 0)
573 return;
Trond Myklebust1de3fc12006-05-25 01:40:44 -0400574 /*
Trond Myklebust0b671302006-11-14 16:12:23 -0500575 * Note: nfs_readpage_retry may change the values of
Trond Myklebust1de3fc12006-05-25 01:40:44 -0400576 * data->args. In the multi-page case, we therefore need
Trond Myklebust0b671302006-11-14 16:12:23 -0500577 * to ensure that we call nfs_readpage_set_pages_uptodate()
578 * first.
Trond Myklebust1de3fc12006-05-25 01:40:44 -0400579 */
580 if (likely(task->tk_status >= 0)) {
581 nfs_readpage_truncate_uninitialised_page(data);
582 nfs_readpage_set_pages_uptodate(data);
Trond Myklebust0b671302006-11-14 16:12:23 -0500583 if (nfs_readpage_retry(task, data) != 0)
584 return;
585 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 while (!list_empty(&data->pages)) {
587 struct nfs_page *req = nfs_list_entry(data->pages.next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Trond Myklebust1de3fc12006-05-25 01:40:44 -0400589 nfs_list_remove_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 nfs_readpage_release(req);
591 }
592}
593
Trond Myklebustec06c092006-03-20 13:44:27 -0500594static const struct rpc_call_ops nfs_read_full_ops = {
595 .rpc_call_done = nfs_readpage_result_full,
596 .rpc_release = nfs_readdata_release,
597};
598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 * Read a page over NFS.
601 * We read the page synchronously in the following case:
602 * - The error flag is set for this page. This happens only when a
603 * previous async read operation failed.
604 */
605int nfs_readpage(struct file *file, struct page *page)
606{
607 struct nfs_open_context *ctx;
608 struct inode *inode = page->mapping->host;
609 int error;
610
611 dprintk("NFS: nfs_readpage (%p %ld@%lu)\n",
612 page, PAGE_CACHE_SIZE, page->index);
Chuck Lever91d5b472006-03-20 13:44:14 -0500613 nfs_inc_stats(inode, NFSIOS_VFSREADPAGE);
614 nfs_add_stats(inode, NFSIOS_READPAGES, 1);
615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 /*
617 * Try to flush any pending writes to the file..
618 *
619 * NOTE! Because we own the page lock, there cannot
620 * be any new pending writes generated at this point
621 * for this page (other pages can be written to).
622 */
623 error = nfs_wb_page(inode, page);
624 if (error)
625 goto out_error;
626
Trond Myklebust5f004cf2006-09-14 14:03:14 -0400627 error = -ESTALE;
628 if (NFS_STALE(inode))
629 goto out_error;
630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 if (file == NULL) {
Trond Myklebustcf1308f2006-11-19 16:44:52 -0500632 error = -EBADF;
Trond Myklebustd5308382005-11-04 15:33:38 -0500633 ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 if (ctx == NULL)
Trond Myklebustcf1308f2006-11-19 16:44:52 -0500635 goto out_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 } else
637 ctx = get_nfs_open_context((struct nfs_open_context *)
638 file->private_data);
639 if (!IS_SYNC(inode)) {
640 error = nfs_readpage_async(ctx, inode, page);
641 goto out;
642 }
643
644 error = nfs_readpage_sync(ctx, inode, page);
645 if (error < 0 && IS_SWAPFILE(inode))
646 printk("Aiee.. nfs swap-in of page failed!\n");
647out:
648 put_nfs_open_context(ctx);
649 return error;
650
651out_error:
652 unlock_page(page);
653 return error;
654}
655
656struct nfs_readdesc {
657 struct list_head *head;
658 struct nfs_open_context *ctx;
659};
660
661static int
662readpage_async_filler(void *data, struct page *page)
663{
664 struct nfs_readdesc *desc = (struct nfs_readdesc *)data;
665 struct inode *inode = page->mapping->host;
666 struct nfs_page *new;
667 unsigned int len;
668
669 nfs_wb_page(inode, page);
670 len = nfs_page_length(inode, page);
671 if (len == 0)
672 return nfs_return_empty_page(page);
673 new = nfs_create_request(desc->ctx, inode, page, 0, len);
674 if (IS_ERR(new)) {
675 SetPageError(page);
676 unlock_page(page);
677 return PTR_ERR(new);
678 }
679 if (len < PAGE_CACHE_SIZE)
680 memclear_highpage_flush(page, len, PAGE_CACHE_SIZE - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 nfs_list_add_request(new, desc->head);
682 return 0;
683}
684
685int nfs_readpages(struct file *filp, struct address_space *mapping,
686 struct list_head *pages, unsigned nr_pages)
687{
688 LIST_HEAD(head);
689 struct nfs_readdesc desc = {
690 .head = &head,
691 };
692 struct inode *inode = mapping->host;
693 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust5f004cf2006-09-14 14:03:14 -0400694 int ret = -ESTALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
696 dprintk("NFS: nfs_readpages (%s/%Ld %d)\n",
697 inode->i_sb->s_id,
698 (long long)NFS_FILEID(inode),
699 nr_pages);
Chuck Lever91d5b472006-03-20 13:44:14 -0500700 nfs_inc_stats(inode, NFSIOS_VFSREADPAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Trond Myklebust5f004cf2006-09-14 14:03:14 -0400702 if (NFS_STALE(inode))
703 goto out;
704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 if (filp == NULL) {
Trond Myklebustd5308382005-11-04 15:33:38 -0500706 desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 if (desc.ctx == NULL)
708 return -EBADF;
709 } else
710 desc.ctx = get_nfs_open_context((struct nfs_open_context *)
711 filp->private_data);
712 ret = read_cache_pages(mapping, pages, readpage_async_filler, &desc);
713 if (!list_empty(&head)) {
714 int err = nfs_pagein_list(&head, server->rpages);
715 if (!ret)
Chuck Lever91d5b472006-03-20 13:44:14 -0500716 nfs_add_stats(inode, NFSIOS_READPAGES, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 ret = err;
718 }
719 put_nfs_open_context(desc.ctx);
Trond Myklebust5f004cf2006-09-14 14:03:14 -0400720out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 return ret;
722}
723
David Howellsf7b422b2006-06-09 09:34:33 -0400724int __init nfs_init_readpagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
726 nfs_rdata_cachep = kmem_cache_create("nfs_read_data",
727 sizeof(struct nfs_read_data),
728 0, SLAB_HWCACHE_ALIGN,
729 NULL, NULL);
730 if (nfs_rdata_cachep == NULL)
731 return -ENOMEM;
732
Matthew Dobson93d23412006-03-26 01:37:50 -0800733 nfs_rdata_mempool = mempool_create_slab_pool(MIN_POOL_READ,
734 nfs_rdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 if (nfs_rdata_mempool == NULL)
736 return -ENOMEM;
737
738 return 0;
739}
740
David Brownell266bee82006-06-27 12:59:15 -0700741void nfs_destroy_readpagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
743 mempool_destroy(nfs_rdata_mempool);
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -0700744 kmem_cache_destroy(nfs_rdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745}