blob: da9cf11c326fbcf1eaa816f25c4d3b7de70a4d2c [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 Myklebust3feb2d42006-03-20 13:44:37 -050046struct nfs_read_data *nfs_readdata_alloc(unsigned int pagecount)
47{
48 struct nfs_read_data *p = mempool_alloc(nfs_rdata_mempool, SLAB_NOFS);
49
50 if (p) {
51 memset(p, 0, sizeof(*p));
52 INIT_LIST_HEAD(&p->pages);
Chuck Lever0d0b5cb2006-05-25 01:40:53 -040053 if (pagecount <= ARRAY_SIZE(p->page_array))
54 p->pagevec = p->page_array;
Trond Myklebust3feb2d42006-03-20 13:44:37 -050055 else {
Chuck Lever0d0b5cb2006-05-25 01:40:53 -040056 p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
57 if (!p->pagevec) {
Trond Myklebust3feb2d42006-03-20 13:44:37 -050058 mempool_free(p, nfs_rdata_mempool);
59 p = NULL;
60 }
61 }
62 }
63 return p;
64}
65
Adrian Bunke4e20512006-08-03 15:07:47 -040066static void nfs_readdata_free(struct nfs_read_data *p)
Trond Myklebust3feb2d42006-03-20 13:44:37 -050067{
68 if (p && (p->pagevec != &p->page_array[0]))
69 kfree(p->pagevec);
70 mempool_free(p, nfs_rdata_mempool);
71}
72
Trond Myklebust963d8fe2006-01-03 09:55:04 +010073void nfs_readdata_release(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 nfs_readdata_free(data);
76}
77
78static
79unsigned int nfs_page_length(struct inode *inode, struct page *page)
80{
81 loff_t i_size = i_size_read(inode);
82 unsigned long idx;
83
84 if (i_size <= 0)
85 return 0;
86 idx = (i_size - 1) >> PAGE_CACHE_SHIFT;
87 if (page->index > idx)
88 return 0;
89 if (page->index != idx)
90 return PAGE_CACHE_SIZE;
91 return 1 + ((i_size - 1) & (PAGE_CACHE_SIZE - 1));
92}
93
94static
95int nfs_return_empty_page(struct page *page)
96{
97 memclear_highpage_flush(page, 0, PAGE_CACHE_SIZE);
98 SetPageUptodate(page);
99 unlock_page(page);
100 return 0;
101}
102
Trond Myklebust1de3fc12006-05-25 01:40:44 -0400103static void nfs_readpage_truncate_uninitialised_page(struct nfs_read_data *data)
104{
105 unsigned int remainder = data->args.count - data->res.count;
106 unsigned int base = data->args.pgbase + data->res.count;
107 unsigned int pglen;
108 struct page **pages;
109
110 if (data->res.eof == 0 || remainder == 0)
111 return;
112 /*
113 * Note: "remainder" can never be negative, since we check for
114 * this in the XDR code.
115 */
116 pages = &data->args.pages[base >> PAGE_CACHE_SHIFT];
117 base &= ~PAGE_CACHE_MASK;
118 pglen = PAGE_CACHE_SIZE - base;
Trond Myklebust79558f32006-08-22 13:44:32 -0400119 for (;;) {
120 if (remainder <= pglen) {
121 memclear_highpage_flush(*pages, base, remainder);
122 break;
123 }
Trond Myklebust1de3fc12006-05-25 01:40:44 -0400124 memclear_highpage_flush(*pages, base, pglen);
Trond Myklebust79558f32006-08-22 13:44:32 -0400125 pages++;
126 remainder -= pglen;
127 pglen = PAGE_CACHE_SIZE;
128 base = 0;
129 }
Trond Myklebust1de3fc12006-05-25 01:40:44 -0400130}
131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132/*
133 * Read a page synchronously.
134 */
135static int nfs_readpage_sync(struct nfs_open_context *ctx, struct inode *inode,
136 struct page *page)
137{
138 unsigned int rsize = NFS_SERVER(inode)->rsize;
139 unsigned int count = PAGE_CACHE_SIZE;
140 int result;
141 struct nfs_read_data *rdata;
142
Chuck Lever40859d72005-11-30 18:09:02 -0500143 rdata = nfs_readdata_alloc(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 if (!rdata)
145 return -ENOMEM;
146
147 memset(rdata, 0, sizeof(*rdata));
148 rdata->flags = (IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0);
149 rdata->cred = ctx->cred;
150 rdata->inode = inode;
151 INIT_LIST_HEAD(&rdata->pages);
152 rdata->args.fh = NFS_FH(inode);
153 rdata->args.context = ctx;
154 rdata->args.pages = &page;
155 rdata->args.pgbase = 0UL;
156 rdata->args.count = rsize;
157 rdata->res.fattr = &rdata->fattr;
158
159 dprintk("NFS: nfs_readpage_sync(%p)\n", page);
160
161 /*
162 * This works now because the socket layer never tries to DMA
163 * into this buffer directly.
164 */
165 do {
166 if (count < rsize)
167 rdata->args.count = count;
168 rdata->res.count = rdata->args.count;
169 rdata->args.offset = page_offset(page) + rdata->args.pgbase;
170
171 dprintk("NFS: nfs_proc_read(%s, (%s/%Ld), %Lu, %u)\n",
172 NFS_SERVER(inode)->hostname,
173 inode->i_sb->s_id,
174 (long long)NFS_FILEID(inode),
175 (unsigned long long)rdata->args.pgbase,
176 rdata->args.count);
177
178 lock_kernel();
179 result = NFS_PROTO(inode)->read(rdata);
180 unlock_kernel();
181
182 /*
183 * Even if we had a partial success we can't mark the page
184 * cache valid.
185 */
186 if (result < 0) {
187 if (result == -EISDIR)
188 result = -EINVAL;
189 goto io_error;
190 }
191 count -= result;
192 rdata->args.pgbase += result;
Chuck Lever91d5b472006-03-20 13:44:14 -0500193 nfs_add_stats(inode, NFSIOS_SERVERREADBYTES, result);
194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 /* Note: result == 0 should only happen if we're caching
196 * a write that extends the file and punches a hole.
197 */
198 if (rdata->res.eof != 0 || result == 0)
199 break;
200 } while (count);
Chuck Leverdc592502005-08-18 11:24:12 -0700201 spin_lock(&inode->i_lock);
Chuck Lever55296802005-08-18 11:24:09 -0700202 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATIME;
Chuck Leverdc592502005-08-18 11:24:12 -0700203 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Trond Myklebust1de3fc12006-05-25 01:40:44 -0400205 nfs_readpage_truncate_uninitialised_page(rdata);
206 if (rdata->res.eof || rdata->res.count == rdata->args.count)
207 SetPageUptodate(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 result = 0;
209
210io_error:
211 unlock_page(page);
212 nfs_readdata_free(rdata);
213 return result;
214}
215
216static int nfs_readpage_async(struct nfs_open_context *ctx, struct inode *inode,
217 struct page *page)
218{
219 LIST_HEAD(one_request);
220 struct nfs_page *new;
221 unsigned int len;
222
223 len = nfs_page_length(inode, page);
224 if (len == 0)
225 return nfs_return_empty_page(page);
226 new = nfs_create_request(ctx, inode, page, 0, len);
227 if (IS_ERR(new)) {
228 unlock_page(page);
229 return PTR_ERR(new);
230 }
231 if (len < PAGE_CACHE_SIZE)
232 memclear_highpage_flush(page, len, PAGE_CACHE_SIZE - len);
233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 nfs_list_add_request(new, &one_request);
235 nfs_pagein_one(&one_request, inode);
236 return 0;
237}
238
239static void nfs_readpage_release(struct nfs_page *req)
240{
241 unlock_page(req->wb_page);
242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 dprintk("NFS: read done (%s/%Ld %d@%Ld)\n",
244 req->wb_context->dentry->d_inode->i_sb->s_id,
245 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
246 req->wb_bytes,
247 (long long)req_offset(req));
Nick Wilson10d2c462005-09-22 21:44:28 -0700248 nfs_clear_request(req);
249 nfs_release_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250}
251
252/*
253 * Set up the NFS read request struct
254 */
255static void nfs_read_rpcsetup(struct nfs_page *req, struct nfs_read_data *data,
Trond Myklebustec06c092006-03-20 13:44:27 -0500256 const struct rpc_call_ops *call_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 unsigned int count, unsigned int offset)
258{
259 struct inode *inode;
Trond Myklebustec06c092006-03-20 13:44:27 -0500260 int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262 data->req = req;
263 data->inode = inode = req->wb_context->dentry->d_inode;
264 data->cred = req->wb_context->cred;
265
266 data->args.fh = NFS_FH(inode);
267 data->args.offset = req_offset(req) + offset;
268 data->args.pgbase = req->wb_pgbase + offset;
269 data->args.pages = data->pagevec;
270 data->args.count = count;
271 data->args.context = req->wb_context;
272
273 data->res.fattr = &data->fattr;
274 data->res.count = count;
275 data->res.eof = 0;
Trond Myklebust0e574af2005-10-27 22:12:38 -0400276 nfs_fattr_init(&data->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Trond Myklebustec06c092006-03-20 13:44:27 -0500278 /* Set up the initial task struct. */
279 flags = RPC_TASK_ASYNC | (IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0);
280 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, call_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 NFS_PROTO(inode)->read_setup(data);
282
283 data->task.tk_cookie = (unsigned long)inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 dprintk("NFS: %4d initiated read call (req %s/%Ld, %u bytes @ offset %Lu)\n",
286 data->task.tk_pid,
287 inode->i_sb->s_id,
288 (long long)NFS_FILEID(inode),
289 count,
290 (unsigned long long)data->args.offset);
291}
292
293static void
294nfs_async_read_error(struct list_head *head)
295{
296 struct nfs_page *req;
297
298 while (!list_empty(head)) {
299 req = nfs_list_entry(head->next);
300 nfs_list_remove_request(req);
301 SetPageError(req->wb_page);
302 nfs_readpage_release(req);
303 }
304}
305
306/*
307 * Start an async read operation
308 */
309static void nfs_execute_read(struct nfs_read_data *data)
310{
311 struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
312 sigset_t oldset;
313
314 rpc_clnt_sigmask(clnt, &oldset);
315 lock_kernel();
316 rpc_execute(&data->task);
317 unlock_kernel();
318 rpc_clnt_sigunmask(clnt, &oldset);
319}
320
321/*
322 * Generate multiple requests to fill a single page.
323 *
324 * We optimize to reduce the number of read operations on the wire. If we
325 * detect that we're reading a page, or an area of a page, that is past the
326 * end of file, we do not generate NFS read operations but just clear the
327 * parts of the page that would have come back zero from the server anyway.
328 *
329 * We rely on the cached value of i_size to make this determination; another
330 * client can fill pages on the server past our cached end-of-file, but we
331 * won't see the new data until our attribute cache is updated. This is more
332 * or less conventional NFS client behavior.
333 */
334static int nfs_pagein_multi(struct list_head *head, struct inode *inode)
335{
336 struct nfs_page *req = nfs_list_entry(head->next);
337 struct page *page = req->wb_page;
338 struct nfs_read_data *data;
339 unsigned int rsize = NFS_SERVER(inode)->rsize;
340 unsigned int nbytes, offset;
341 int requests = 0;
342 LIST_HEAD(list);
343
344 nfs_list_remove_request(req);
345
346 nbytes = req->wb_bytes;
347 for(;;) {
Chuck Lever40859d72005-11-30 18:09:02 -0500348 data = nfs_readdata_alloc(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 if (!data)
350 goto out_bad;
351 INIT_LIST_HEAD(&data->pages);
352 list_add(&data->pages, &list);
353 requests++;
354 if (nbytes <= rsize)
355 break;
356 nbytes -= rsize;
357 }
358 atomic_set(&req->wb_complete, requests);
359
360 ClearPageError(page);
361 offset = 0;
362 nbytes = req->wb_bytes;
363 do {
364 data = list_entry(list.next, struct nfs_read_data, pages);
365 list_del_init(&data->pages);
366
367 data->pagevec[0] = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 if (nbytes > rsize) {
Trond Myklebustec06c092006-03-20 13:44:27 -0500370 nfs_read_rpcsetup(req, data, &nfs_read_partial_ops,
371 rsize, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 offset += rsize;
373 nbytes -= rsize;
374 } else {
Trond Myklebustec06c092006-03-20 13:44:27 -0500375 nfs_read_rpcsetup(req, data, &nfs_read_partial_ops,
376 nbytes, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 nbytes = 0;
378 }
379 nfs_execute_read(data);
380 } while (nbytes != 0);
381
382 return 0;
383
384out_bad:
385 while (!list_empty(&list)) {
386 data = list_entry(list.next, struct nfs_read_data, pages);
387 list_del(&data->pages);
388 nfs_readdata_free(data);
389 }
390 SetPageError(page);
391 nfs_readpage_release(req);
392 return -ENOMEM;
393}
394
395static int nfs_pagein_one(struct list_head *head, struct inode *inode)
396{
397 struct nfs_page *req;
398 struct page **pages;
399 struct nfs_read_data *data;
400 unsigned int count;
401
402 if (NFS_SERVER(inode)->rsize < PAGE_CACHE_SIZE)
403 return nfs_pagein_multi(head, inode);
404
Chuck Lever40859d72005-11-30 18:09:02 -0500405 data = nfs_readdata_alloc(NFS_SERVER(inode)->rpages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 if (!data)
407 goto out_bad;
408
409 INIT_LIST_HEAD(&data->pages);
410 pages = data->pagevec;
411 count = 0;
412 while (!list_empty(head)) {
413 req = nfs_list_entry(head->next);
414 nfs_list_remove_request(req);
415 nfs_list_add_request(req, &data->pages);
416 ClearPageError(req->wb_page);
417 *pages++ = req->wb_page;
418 count += req->wb_bytes;
419 }
420 req = nfs_list_entry(data->pages.next);
421
Trond Myklebustec06c092006-03-20 13:44:27 -0500422 nfs_read_rpcsetup(req, data, &nfs_read_full_ops, count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
424 nfs_execute_read(data);
425 return 0;
426out_bad:
427 nfs_async_read_error(head);
428 return -ENOMEM;
429}
430
431static int
432nfs_pagein_list(struct list_head *head, int rpages)
433{
434 LIST_HEAD(one_request);
435 struct nfs_page *req;
436 int error = 0;
437 unsigned int pages = 0;
438
439 while (!list_empty(head)) {
440 pages += nfs_coalesce_requests(head, &one_request, rpages);
441 req = nfs_list_entry(one_request.next);
442 error = nfs_pagein_one(&one_request, req->wb_context->dentry->d_inode);
443 if (error < 0)
444 break;
445 }
446 if (error >= 0)
447 return pages;
448
449 nfs_async_read_error(head);
450 return error;
451}
452
453/*
454 * Handle a read reply that fills part of a page.
455 */
Trond Myklebustec06c092006-03-20 13:44:27 -0500456static void nfs_readpage_result_partial(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
Trond Myklebustec06c092006-03-20 13:44:27 -0500458 struct nfs_read_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 struct nfs_page *req = data->req;
460 struct page *page = req->wb_page;
461
Trond Myklebust1de3fc12006-05-25 01:40:44 -0400462 if (likely(task->tk_status >= 0))
463 nfs_readpage_truncate_uninitialised_page(data);
464 else
465 SetPageError(page);
Trond Myklebustec06c092006-03-20 13:44:27 -0500466 if (nfs_readpage_result(task, data) != 0)
467 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 if (atomic_dec_and_test(&req->wb_complete)) {
469 if (!PageError(page))
470 SetPageUptodate(page);
471 nfs_readpage_release(req);
472 }
473}
474
Trond Myklebustec06c092006-03-20 13:44:27 -0500475static const struct rpc_call_ops nfs_read_partial_ops = {
476 .rpc_call_done = nfs_readpage_result_partial,
477 .rpc_release = nfs_readdata_release,
478};
479
Trond Myklebust1de3fc12006-05-25 01:40:44 -0400480static void nfs_readpage_set_pages_uptodate(struct nfs_read_data *data)
481{
482 unsigned int count = data->res.count;
483 unsigned int base = data->args.pgbase;
484 struct page **pages;
485
Trond Myklebust79558f32006-08-22 13:44:32 -0400486 if (data->res.eof)
487 count = data->args.count;
Trond Myklebust1de3fc12006-05-25 01:40:44 -0400488 if (unlikely(count == 0))
489 return;
490 pages = &data->args.pages[base >> PAGE_CACHE_SHIFT];
491 base &= ~PAGE_CACHE_MASK;
492 count += base;
493 for (;count >= PAGE_CACHE_SIZE; count -= PAGE_CACHE_SIZE, pages++)
494 SetPageUptodate(*pages);
Trond Myklebust79558f32006-08-22 13:44:32 -0400495 if (count != 0)
Trond Myklebust1de3fc12006-05-25 01:40:44 -0400496 SetPageUptodate(*pages);
497}
498
499static void nfs_readpage_set_pages_error(struct nfs_read_data *data)
500{
501 unsigned int count = data->args.count;
502 unsigned int base = data->args.pgbase;
503 struct page **pages;
504
505 pages = &data->args.pages[base >> PAGE_CACHE_SHIFT];
506 base &= ~PAGE_CACHE_MASK;
507 count += base;
508 for (;count >= PAGE_CACHE_SIZE; count -= PAGE_CACHE_SIZE, pages++)
509 SetPageError(*pages);
Trond Myklebust79558f32006-08-22 13:44:32 -0400510 if (count != 0)
511 SetPageError(*pages);
Trond Myklebust1de3fc12006-05-25 01:40:44 -0400512}
513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514/*
515 * This is the callback from RPC telling us whether a reply was
516 * received or some error occurred (timeout or socket shutdown).
517 */
Trond Myklebustec06c092006-03-20 13:44:27 -0500518static void nfs_readpage_result_full(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
Trond Myklebustec06c092006-03-20 13:44:27 -0500520 struct nfs_read_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Trond Myklebust1de3fc12006-05-25 01:40:44 -0400522 /*
523 * Note: nfs_readpage_result may change the values of
524 * data->args. In the multi-page case, we therefore need
525 * to ensure that we call the next nfs_readpage_set_page_uptodate()
526 * first in the multi-page case.
527 */
528 if (likely(task->tk_status >= 0)) {
529 nfs_readpage_truncate_uninitialised_page(data);
530 nfs_readpage_set_pages_uptodate(data);
531 } else
532 nfs_readpage_set_pages_error(data);
Trond Myklebustec06c092006-03-20 13:44:27 -0500533 if (nfs_readpage_result(task, data) != 0)
534 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 while (!list_empty(&data->pages)) {
536 struct nfs_page *req = nfs_list_entry(data->pages.next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Trond Myklebust1de3fc12006-05-25 01:40:44 -0400538 nfs_list_remove_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 nfs_readpage_release(req);
540 }
541}
542
Trond Myklebustec06c092006-03-20 13:44:27 -0500543static const struct rpc_call_ops nfs_read_full_ops = {
544 .rpc_call_done = nfs_readpage_result_full,
545 .rpc_release = nfs_readdata_release,
546};
547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548/*
549 * This is the callback from RPC telling us whether a reply was
550 * received or some error occurred (timeout or socket shutdown).
551 */
Trond Myklebustec06c092006-03-20 13:44:27 -0500552int nfs_readpage_result(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 struct nfs_readargs *argp = &data->args;
555 struct nfs_readres *resp = &data->res;
Trond Myklebustec06c092006-03-20 13:44:27 -0500556 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558 dprintk("NFS: %4d nfs_readpage_result, (status %d)\n",
Trond Myklebustec06c092006-03-20 13:44:27 -0500559 task->tk_pid, task->tk_status);
560
561 status = NFS_PROTO(data->inode)->read_done(task, data);
562 if (status != 0)
563 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Chuck Lever91d5b472006-03-20 13:44:14 -0500565 nfs_add_stats(data->inode, NFSIOS_SERVERREADBYTES, resp->count);
566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 /* Is this a short read? */
568 if (task->tk_status >= 0 && resp->count < argp->count && !resp->eof) {
Chuck Lever91d5b472006-03-20 13:44:14 -0500569 nfs_inc_stats(data->inode, NFSIOS_SHORTREAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 /* Has the server at least made some progress? */
571 if (resp->count != 0) {
572 /* Yes, so retry the read at the end of the data */
573 argp->offset += resp->count;
574 argp->pgbase += resp->count;
575 argp->count -= resp->count;
576 rpc_restart_call(task);
Trond Myklebustec06c092006-03-20 13:44:27 -0500577 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 }
579 task->tk_status = -EIO;
580 }
Chuck Leverdc592502005-08-18 11:24:12 -0700581 spin_lock(&data->inode->i_lock);
Chuck Lever55296802005-08-18 11:24:09 -0700582 NFS_I(data->inode)->cache_validity |= NFS_INO_INVALID_ATIME;
Chuck Leverdc592502005-08-18 11:24:12 -0700583 spin_unlock(&data->inode->i_lock);
Trond Myklebustec06c092006-03-20 13:44:27 -0500584 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585}
586
587/*
588 * Read a page over NFS.
589 * We read the page synchronously in the following case:
590 * - The error flag is set for this page. This happens only when a
591 * previous async read operation failed.
592 */
593int nfs_readpage(struct file *file, struct page *page)
594{
595 struct nfs_open_context *ctx;
596 struct inode *inode = page->mapping->host;
597 int error;
598
599 dprintk("NFS: nfs_readpage (%p %ld@%lu)\n",
600 page, PAGE_CACHE_SIZE, page->index);
Chuck Lever91d5b472006-03-20 13:44:14 -0500601 nfs_inc_stats(inode, NFSIOS_VFSREADPAGE);
602 nfs_add_stats(inode, NFSIOS_READPAGES, 1);
603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 /*
605 * Try to flush any pending writes to the file..
606 *
607 * NOTE! Because we own the page lock, there cannot
608 * be any new pending writes generated at this point
609 * for this page (other pages can be written to).
610 */
611 error = nfs_wb_page(inode, page);
612 if (error)
613 goto out_error;
614
615 if (file == NULL) {
Trond Myklebustd5308382005-11-04 15:33:38 -0500616 ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 if (ctx == NULL)
618 return -EBADF;
619 } else
620 ctx = get_nfs_open_context((struct nfs_open_context *)
621 file->private_data);
622 if (!IS_SYNC(inode)) {
623 error = nfs_readpage_async(ctx, inode, page);
624 goto out;
625 }
626
627 error = nfs_readpage_sync(ctx, inode, page);
628 if (error < 0 && IS_SWAPFILE(inode))
629 printk("Aiee.. nfs swap-in of page failed!\n");
630out:
631 put_nfs_open_context(ctx);
632 return error;
633
634out_error:
635 unlock_page(page);
636 return error;
637}
638
639struct nfs_readdesc {
640 struct list_head *head;
641 struct nfs_open_context *ctx;
642};
643
644static int
645readpage_async_filler(void *data, struct page *page)
646{
647 struct nfs_readdesc *desc = (struct nfs_readdesc *)data;
648 struct inode *inode = page->mapping->host;
649 struct nfs_page *new;
650 unsigned int len;
651
652 nfs_wb_page(inode, page);
653 len = nfs_page_length(inode, page);
654 if (len == 0)
655 return nfs_return_empty_page(page);
656 new = nfs_create_request(desc->ctx, inode, page, 0, len);
657 if (IS_ERR(new)) {
658 SetPageError(page);
659 unlock_page(page);
660 return PTR_ERR(new);
661 }
662 if (len < PAGE_CACHE_SIZE)
663 memclear_highpage_flush(page, len, PAGE_CACHE_SIZE - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 nfs_list_add_request(new, desc->head);
665 return 0;
666}
667
668int nfs_readpages(struct file *filp, struct address_space *mapping,
669 struct list_head *pages, unsigned nr_pages)
670{
671 LIST_HEAD(head);
672 struct nfs_readdesc desc = {
673 .head = &head,
674 };
675 struct inode *inode = mapping->host;
676 struct nfs_server *server = NFS_SERVER(inode);
677 int ret;
678
679 dprintk("NFS: nfs_readpages (%s/%Ld %d)\n",
680 inode->i_sb->s_id,
681 (long long)NFS_FILEID(inode),
682 nr_pages);
Chuck Lever91d5b472006-03-20 13:44:14 -0500683 nfs_inc_stats(inode, NFSIOS_VFSREADPAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
685 if (filp == NULL) {
Trond Myklebustd5308382005-11-04 15:33:38 -0500686 desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 if (desc.ctx == NULL)
688 return -EBADF;
689 } else
690 desc.ctx = get_nfs_open_context((struct nfs_open_context *)
691 filp->private_data);
692 ret = read_cache_pages(mapping, pages, readpage_async_filler, &desc);
693 if (!list_empty(&head)) {
694 int err = nfs_pagein_list(&head, server->rpages);
695 if (!ret)
Chuck Lever91d5b472006-03-20 13:44:14 -0500696 nfs_add_stats(inode, NFSIOS_READPAGES, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 ret = err;
698 }
699 put_nfs_open_context(desc.ctx);
700 return ret;
701}
702
David Howellsf7b422b2006-06-09 09:34:33 -0400703int __init nfs_init_readpagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
705 nfs_rdata_cachep = kmem_cache_create("nfs_read_data",
706 sizeof(struct nfs_read_data),
707 0, SLAB_HWCACHE_ALIGN,
708 NULL, NULL);
709 if (nfs_rdata_cachep == NULL)
710 return -ENOMEM;
711
Matthew Dobson93d23412006-03-26 01:37:50 -0800712 nfs_rdata_mempool = mempool_create_slab_pool(MIN_POOL_READ,
713 nfs_rdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 if (nfs_rdata_mempool == NULL)
715 return -ENOMEM;
716
717 return 0;
718}
719
David Brownell266bee82006-06-27 12:59:15 -0700720void nfs_destroy_readpagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
722 mempool_destroy(nfs_rdata_mempool);
723 if (kmem_cache_destroy(nfs_rdata_cachep))
724 printk(KERN_INFO "nfs_read_data: not all structures were freed\n");
725}