[POWERPC] iseries: eliminate a couple of warnings
[linux-2.6/kvm.git] / fs / nfs / read.c
blob7a9ee00e0c613e9b279d2a86eb35fee9f6dab9cb
1 /*
2 * linux/fs/nfs/read.c
4 * Block I/O for NFS
6 * Partial copy of Linus' read cache modifications to fs/nfs/file.c
7 * modified for async RPC by okir@monad.swb.de
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.
18 #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>
31 #include <asm/system.h>
33 #include "iostat.h"
35 #define NFSDBG_FACILITY NFSDBG_PAGECACHE
37 static int nfs_pagein_one(struct list_head *, struct inode *);
38 static const struct rpc_call_ops nfs_read_partial_ops;
39 static const struct rpc_call_ops nfs_read_full_ops;
41 static kmem_cache_t *nfs_rdata_cachep;
42 static mempool_t *nfs_rdata_mempool;
44 #define MIN_POOL_READ (32)
46 struct nfs_read_data *nfs_readdata_alloc(size_t len)
48 unsigned int pagecount = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
49 struct nfs_read_data *p = mempool_alloc(nfs_rdata_mempool, SLAB_NOFS);
51 if (p) {
52 memset(p, 0, sizeof(*p));
53 INIT_LIST_HEAD(&p->pages);
54 p->npages = pagecount;
55 if (pagecount <= ARRAY_SIZE(p->page_array))
56 p->pagevec = p->page_array;
57 else {
58 p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
59 if (!p->pagevec) {
60 mempool_free(p, nfs_rdata_mempool);
61 p = NULL;
65 return p;
68 static void nfs_readdata_free(struct nfs_read_data *p)
70 if (p && (p->pagevec != &p->page_array[0]))
71 kfree(p->pagevec);
72 mempool_free(p, nfs_rdata_mempool);
75 void nfs_readdata_release(void *data)
77 nfs_readdata_free(data);
80 static
81 unsigned int nfs_page_length(struct inode *inode, struct page *page)
83 loff_t i_size = i_size_read(inode);
84 unsigned long idx;
86 if (i_size <= 0)
87 return 0;
88 idx = (i_size - 1) >> PAGE_CACHE_SHIFT;
89 if (page->index > idx)
90 return 0;
91 if (page->index != idx)
92 return PAGE_CACHE_SIZE;
93 return 1 + ((i_size - 1) & (PAGE_CACHE_SIZE - 1));
96 static
97 int nfs_return_empty_page(struct page *page)
99 memclear_highpage_flush(page, 0, PAGE_CACHE_SIZE);
100 SetPageUptodate(page);
101 unlock_page(page);
102 return 0;
105 static void nfs_readpage_truncate_uninitialised_page(struct nfs_read_data *data)
107 unsigned int remainder = data->args.count - data->res.count;
108 unsigned int base = data->args.pgbase + data->res.count;
109 unsigned int pglen;
110 struct page **pages;
112 if (data->res.eof == 0 || remainder == 0)
113 return;
115 * Note: "remainder" can never be negative, since we check for
116 * this in the XDR code.
118 pages = &data->args.pages[base >> PAGE_CACHE_SHIFT];
119 base &= ~PAGE_CACHE_MASK;
120 pglen = PAGE_CACHE_SIZE - base;
121 for (;;) {
122 if (remainder <= pglen) {
123 memclear_highpage_flush(*pages, base, remainder);
124 break;
126 memclear_highpage_flush(*pages, base, pglen);
127 pages++;
128 remainder -= pglen;
129 pglen = PAGE_CACHE_SIZE;
130 base = 0;
135 * Read a page synchronously.
137 static int nfs_readpage_sync(struct nfs_open_context *ctx, struct inode *inode,
138 struct page *page)
140 unsigned int rsize = NFS_SERVER(inode)->rsize;
141 unsigned int count = PAGE_CACHE_SIZE;
142 int result;
143 struct nfs_read_data *rdata;
145 rdata = nfs_readdata_alloc(count);
146 if (!rdata)
147 return -ENOMEM;
149 memset(rdata, 0, sizeof(*rdata));
150 rdata->flags = (IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0);
151 rdata->cred = ctx->cred;
152 rdata->inode = inode;
153 INIT_LIST_HEAD(&rdata->pages);
154 rdata->args.fh = NFS_FH(inode);
155 rdata->args.context = ctx;
156 rdata->args.pages = &page;
157 rdata->args.pgbase = 0UL;
158 rdata->args.count = rsize;
159 rdata->res.fattr = &rdata->fattr;
161 dprintk("NFS: nfs_readpage_sync(%p)\n", page);
164 * This works now because the socket layer never tries to DMA
165 * into this buffer directly.
167 do {
168 if (count < rsize)
169 rdata->args.count = count;
170 rdata->res.count = rdata->args.count;
171 rdata->args.offset = page_offset(page) + rdata->args.pgbase;
173 dprintk("NFS: nfs_proc_read(%s, (%s/%Ld), %Lu, %u)\n",
174 NFS_SERVER(inode)->hostname,
175 inode->i_sb->s_id,
176 (long long)NFS_FILEID(inode),
177 (unsigned long long)rdata->args.pgbase,
178 rdata->args.count);
180 lock_kernel();
181 result = NFS_PROTO(inode)->read(rdata);
182 unlock_kernel();
185 * Even if we had a partial success we can't mark the page
186 * cache valid.
188 if (result < 0) {
189 if (result == -EISDIR)
190 result = -EINVAL;
191 goto io_error;
193 count -= result;
194 rdata->args.pgbase += result;
195 nfs_add_stats(inode, NFSIOS_SERVERREADBYTES, result);
197 /* Note: result == 0 should only happen if we're caching
198 * a write that extends the file and punches a hole.
200 if (rdata->res.eof != 0 || result == 0)
201 break;
202 } while (count);
203 spin_lock(&inode->i_lock);
204 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATIME;
205 spin_unlock(&inode->i_lock);
207 nfs_readpage_truncate_uninitialised_page(rdata);
208 if (rdata->res.eof || rdata->res.count == rdata->args.count)
209 SetPageUptodate(page);
210 result = 0;
212 io_error:
213 unlock_page(page);
214 nfs_readdata_free(rdata);
215 return result;
218 static int nfs_readpage_async(struct nfs_open_context *ctx, struct inode *inode,
219 struct page *page)
221 LIST_HEAD(one_request);
222 struct nfs_page *new;
223 unsigned int len;
225 len = nfs_page_length(inode, page);
226 if (len == 0)
227 return nfs_return_empty_page(page);
228 new = nfs_create_request(ctx, inode, page, 0, len);
229 if (IS_ERR(new)) {
230 unlock_page(page);
231 return PTR_ERR(new);
233 if (len < PAGE_CACHE_SIZE)
234 memclear_highpage_flush(page, len, PAGE_CACHE_SIZE - len);
236 nfs_list_add_request(new, &one_request);
237 nfs_pagein_one(&one_request, inode);
238 return 0;
241 static void nfs_readpage_release(struct nfs_page *req)
243 unlock_page(req->wb_page);
245 dprintk("NFS: read done (%s/%Ld %d@%Ld)\n",
246 req->wb_context->dentry->d_inode->i_sb->s_id,
247 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
248 req->wb_bytes,
249 (long long)req_offset(req));
250 nfs_clear_request(req);
251 nfs_release_request(req);
255 * Set up the NFS read request struct
257 static void nfs_read_rpcsetup(struct nfs_page *req, struct nfs_read_data *data,
258 const struct rpc_call_ops *call_ops,
259 unsigned int count, unsigned int offset)
261 struct inode *inode;
262 int flags;
264 data->req = req;
265 data->inode = inode = req->wb_context->dentry->d_inode;
266 data->cred = req->wb_context->cred;
268 data->args.fh = NFS_FH(inode);
269 data->args.offset = req_offset(req) + offset;
270 data->args.pgbase = req->wb_pgbase + offset;
271 data->args.pages = data->pagevec;
272 data->args.count = count;
273 data->args.context = req->wb_context;
275 data->res.fattr = &data->fattr;
276 data->res.count = count;
277 data->res.eof = 0;
278 nfs_fattr_init(&data->fattr);
280 /* Set up the initial task struct. */
281 flags = RPC_TASK_ASYNC | (IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0);
282 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, call_ops, data);
283 NFS_PROTO(inode)->read_setup(data);
285 data->task.tk_cookie = (unsigned long)inode;
287 dprintk("NFS: %4d initiated read call (req %s/%Ld, %u bytes @ offset %Lu)\n",
288 data->task.tk_pid,
289 inode->i_sb->s_id,
290 (long long)NFS_FILEID(inode),
291 count,
292 (unsigned long long)data->args.offset);
295 static void
296 nfs_async_read_error(struct list_head *head)
298 struct nfs_page *req;
300 while (!list_empty(head)) {
301 req = nfs_list_entry(head->next);
302 nfs_list_remove_request(req);
303 SetPageError(req->wb_page);
304 nfs_readpage_release(req);
309 * Start an async read operation
311 static void nfs_execute_read(struct nfs_read_data *data)
313 struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
314 sigset_t oldset;
316 rpc_clnt_sigmask(clnt, &oldset);
317 lock_kernel();
318 rpc_execute(&data->task);
319 unlock_kernel();
320 rpc_clnt_sigunmask(clnt, &oldset);
324 * Generate multiple requests to fill a single page.
326 * We optimize to reduce the number of read operations on the wire. If we
327 * detect that we're reading a page, or an area of a page, that is past the
328 * end of file, we do not generate NFS read operations but just clear the
329 * parts of the page that would have come back zero from the server anyway.
331 * We rely on the cached value of i_size to make this determination; another
332 * client can fill pages on the server past our cached end-of-file, but we
333 * won't see the new data until our attribute cache is updated. This is more
334 * or less conventional NFS client behavior.
336 static int nfs_pagein_multi(struct list_head *head, struct inode *inode)
338 struct nfs_page *req = nfs_list_entry(head->next);
339 struct page *page = req->wb_page;
340 struct nfs_read_data *data;
341 size_t rsize = NFS_SERVER(inode)->rsize, nbytes;
342 unsigned int offset;
343 int requests = 0;
344 LIST_HEAD(list);
346 nfs_list_remove_request(req);
348 nbytes = req->wb_bytes;
349 do {
350 size_t len = min(nbytes,rsize);
352 data = nfs_readdata_alloc(len);
353 if (!data)
354 goto out_bad;
355 INIT_LIST_HEAD(&data->pages);
356 list_add(&data->pages, &list);
357 requests++;
358 nbytes -= len;
359 } while(nbytes != 0);
360 atomic_set(&req->wb_complete, requests);
362 ClearPageError(page);
363 offset = 0;
364 nbytes = req->wb_bytes;
365 do {
366 data = list_entry(list.next, struct nfs_read_data, pages);
367 list_del_init(&data->pages);
369 data->pagevec[0] = page;
371 if (nbytes > rsize) {
372 nfs_read_rpcsetup(req, data, &nfs_read_partial_ops,
373 rsize, offset);
374 offset += rsize;
375 nbytes -= rsize;
376 } else {
377 nfs_read_rpcsetup(req, data, &nfs_read_partial_ops,
378 nbytes, offset);
379 nbytes = 0;
381 nfs_execute_read(data);
382 } while (nbytes != 0);
384 return 0;
386 out_bad:
387 while (!list_empty(&list)) {
388 data = list_entry(list.next, struct nfs_read_data, pages);
389 list_del(&data->pages);
390 nfs_readdata_free(data);
392 SetPageError(page);
393 nfs_readpage_release(req);
394 return -ENOMEM;
397 static int nfs_pagein_one(struct list_head *head, struct inode *inode)
399 struct nfs_page *req;
400 struct page **pages;
401 struct nfs_read_data *data;
402 unsigned int count;
404 if (NFS_SERVER(inode)->rsize < PAGE_CACHE_SIZE)
405 return nfs_pagein_multi(head, inode);
407 data = nfs_readdata_alloc(NFS_SERVER(inode)->rsize);
408 if (!data)
409 goto out_bad;
411 INIT_LIST_HEAD(&data->pages);
412 pages = data->pagevec;
413 count = 0;
414 while (!list_empty(head)) {
415 req = nfs_list_entry(head->next);
416 nfs_list_remove_request(req);
417 nfs_list_add_request(req, &data->pages);
418 ClearPageError(req->wb_page);
419 *pages++ = req->wb_page;
420 count += req->wb_bytes;
422 req = nfs_list_entry(data->pages.next);
424 nfs_read_rpcsetup(req, data, &nfs_read_full_ops, count, 0);
426 nfs_execute_read(data);
427 return 0;
428 out_bad:
429 nfs_async_read_error(head);
430 return -ENOMEM;
433 static int
434 nfs_pagein_list(struct list_head *head, int rpages)
436 LIST_HEAD(one_request);
437 struct nfs_page *req;
438 int error = 0;
439 unsigned int pages = 0;
441 while (!list_empty(head)) {
442 pages += nfs_coalesce_requests(head, &one_request, rpages);
443 req = nfs_list_entry(one_request.next);
444 error = nfs_pagein_one(&one_request, req->wb_context->dentry->d_inode);
445 if (error < 0)
446 break;
448 if (error >= 0)
449 return pages;
451 nfs_async_read_error(head);
452 return error;
456 * Handle a read reply that fills part of a page.
458 static void nfs_readpage_result_partial(struct rpc_task *task, void *calldata)
460 struct nfs_read_data *data = calldata;
461 struct nfs_page *req = data->req;
462 struct page *page = req->wb_page;
464 if (likely(task->tk_status >= 0))
465 nfs_readpage_truncate_uninitialised_page(data);
466 else
467 SetPageError(page);
468 if (nfs_readpage_result(task, data) != 0)
469 return;
470 if (atomic_dec_and_test(&req->wb_complete)) {
471 if (!PageError(page))
472 SetPageUptodate(page);
473 nfs_readpage_release(req);
477 static const struct rpc_call_ops nfs_read_partial_ops = {
478 .rpc_call_done = nfs_readpage_result_partial,
479 .rpc_release = nfs_readdata_release,
482 static void nfs_readpage_set_pages_uptodate(struct nfs_read_data *data)
484 unsigned int count = data->res.count;
485 unsigned int base = data->args.pgbase;
486 struct page **pages;
488 if (data->res.eof)
489 count = data->args.count;
490 if (unlikely(count == 0))
491 return;
492 pages = &data->args.pages[base >> PAGE_CACHE_SHIFT];
493 base &= ~PAGE_CACHE_MASK;
494 count += base;
495 for (;count >= PAGE_CACHE_SIZE; count -= PAGE_CACHE_SIZE, pages++)
496 SetPageUptodate(*pages);
497 if (count != 0)
498 SetPageUptodate(*pages);
501 static void nfs_readpage_set_pages_error(struct nfs_read_data *data)
503 unsigned int count = data->args.count;
504 unsigned int base = data->args.pgbase;
505 struct page **pages;
507 pages = &data->args.pages[base >> PAGE_CACHE_SHIFT];
508 base &= ~PAGE_CACHE_MASK;
509 count += base;
510 for (;count >= PAGE_CACHE_SIZE; count -= PAGE_CACHE_SIZE, pages++)
511 SetPageError(*pages);
512 if (count != 0)
513 SetPageError(*pages);
517 * This is the callback from RPC telling us whether a reply was
518 * received or some error occurred (timeout or socket shutdown).
520 static void nfs_readpage_result_full(struct rpc_task *task, void *calldata)
522 struct nfs_read_data *data = calldata;
525 * Note: nfs_readpage_result may change the values of
526 * data->args. In the multi-page case, we therefore need
527 * to ensure that we call the next nfs_readpage_set_page_uptodate()
528 * first in the multi-page case.
530 if (likely(task->tk_status >= 0)) {
531 nfs_readpage_truncate_uninitialised_page(data);
532 nfs_readpage_set_pages_uptodate(data);
533 } else
534 nfs_readpage_set_pages_error(data);
535 if (nfs_readpage_result(task, data) != 0)
536 return;
537 while (!list_empty(&data->pages)) {
538 struct nfs_page *req = nfs_list_entry(data->pages.next);
540 nfs_list_remove_request(req);
541 nfs_readpage_release(req);
545 static const struct rpc_call_ops nfs_read_full_ops = {
546 .rpc_call_done = nfs_readpage_result_full,
547 .rpc_release = nfs_readdata_release,
551 * This is the callback from RPC telling us whether a reply was
552 * received or some error occurred (timeout or socket shutdown).
554 int nfs_readpage_result(struct rpc_task *task, struct nfs_read_data *data)
556 struct nfs_readargs *argp = &data->args;
557 struct nfs_readres *resp = &data->res;
558 int status;
560 dprintk("NFS: %4d nfs_readpage_result, (status %d)\n",
561 task->tk_pid, task->tk_status);
563 status = NFS_PROTO(data->inode)->read_done(task, data);
564 if (status != 0)
565 return status;
567 nfs_add_stats(data->inode, NFSIOS_SERVERREADBYTES, resp->count);
569 /* Is this a short read? */
570 if (task->tk_status >= 0 && resp->count < argp->count && !resp->eof) {
571 nfs_inc_stats(data->inode, NFSIOS_SHORTREAD);
572 /* Has the server at least made some progress? */
573 if (resp->count != 0) {
574 /* Yes, so retry the read at the end of the data */
575 argp->offset += resp->count;
576 argp->pgbase += resp->count;
577 argp->count -= resp->count;
578 rpc_restart_call(task);
579 return -EAGAIN;
581 task->tk_status = -EIO;
583 spin_lock(&data->inode->i_lock);
584 NFS_I(data->inode)->cache_validity |= NFS_INO_INVALID_ATIME;
585 spin_unlock(&data->inode->i_lock);
586 return 0;
590 * Read a page over NFS.
591 * We read the page synchronously in the following case:
592 * - The error flag is set for this page. This happens only when a
593 * previous async read operation failed.
595 int nfs_readpage(struct file *file, struct page *page)
597 struct nfs_open_context *ctx;
598 struct inode *inode = page->mapping->host;
599 int error;
601 dprintk("NFS: nfs_readpage (%p %ld@%lu)\n",
602 page, PAGE_CACHE_SIZE, page->index);
603 nfs_inc_stats(inode, NFSIOS_VFSREADPAGE);
604 nfs_add_stats(inode, NFSIOS_READPAGES, 1);
607 * Try to flush any pending writes to the file..
609 * NOTE! Because we own the page lock, there cannot
610 * be any new pending writes generated at this point
611 * for this page (other pages can be written to).
613 error = nfs_wb_page(inode, page);
614 if (error)
615 goto out_error;
617 if (file == NULL) {
618 ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
619 if (ctx == NULL)
620 return -EBADF;
621 } else
622 ctx = get_nfs_open_context((struct nfs_open_context *)
623 file->private_data);
624 if (!IS_SYNC(inode)) {
625 error = nfs_readpage_async(ctx, inode, page);
626 goto out;
629 error = nfs_readpage_sync(ctx, inode, page);
630 if (error < 0 && IS_SWAPFILE(inode))
631 printk("Aiee.. nfs swap-in of page failed!\n");
632 out:
633 put_nfs_open_context(ctx);
634 return error;
636 out_error:
637 unlock_page(page);
638 return error;
641 struct nfs_readdesc {
642 struct list_head *head;
643 struct nfs_open_context *ctx;
646 static int
647 readpage_async_filler(void *data, struct page *page)
649 struct nfs_readdesc *desc = (struct nfs_readdesc *)data;
650 struct inode *inode = page->mapping->host;
651 struct nfs_page *new;
652 unsigned int len;
654 nfs_wb_page(inode, page);
655 len = nfs_page_length(inode, page);
656 if (len == 0)
657 return nfs_return_empty_page(page);
658 new = nfs_create_request(desc->ctx, inode, page, 0, len);
659 if (IS_ERR(new)) {
660 SetPageError(page);
661 unlock_page(page);
662 return PTR_ERR(new);
664 if (len < PAGE_CACHE_SIZE)
665 memclear_highpage_flush(page, len, PAGE_CACHE_SIZE - len);
666 nfs_list_add_request(new, desc->head);
667 return 0;
670 int nfs_readpages(struct file *filp, struct address_space *mapping,
671 struct list_head *pages, unsigned nr_pages)
673 LIST_HEAD(head);
674 struct nfs_readdesc desc = {
675 .head = &head,
677 struct inode *inode = mapping->host;
678 struct nfs_server *server = NFS_SERVER(inode);
679 int ret;
681 dprintk("NFS: nfs_readpages (%s/%Ld %d)\n",
682 inode->i_sb->s_id,
683 (long long)NFS_FILEID(inode),
684 nr_pages);
685 nfs_inc_stats(inode, NFSIOS_VFSREADPAGES);
687 if (filp == NULL) {
688 desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
689 if (desc.ctx == NULL)
690 return -EBADF;
691 } else
692 desc.ctx = get_nfs_open_context((struct nfs_open_context *)
693 filp->private_data);
694 ret = read_cache_pages(mapping, pages, readpage_async_filler, &desc);
695 if (!list_empty(&head)) {
696 int err = nfs_pagein_list(&head, server->rpages);
697 if (!ret)
698 nfs_add_stats(inode, NFSIOS_READPAGES, err);
699 ret = err;
701 put_nfs_open_context(desc.ctx);
702 return ret;
705 int __init nfs_init_readpagecache(void)
707 nfs_rdata_cachep = kmem_cache_create("nfs_read_data",
708 sizeof(struct nfs_read_data),
709 0, SLAB_HWCACHE_ALIGN,
710 NULL, NULL);
711 if (nfs_rdata_cachep == NULL)
712 return -ENOMEM;
714 nfs_rdata_mempool = mempool_create_slab_pool(MIN_POOL_READ,
715 nfs_rdata_cachep);
716 if (nfs_rdata_mempool == NULL)
717 return -ENOMEM;
719 return 0;
722 void nfs_destroy_readpagecache(void)
724 mempool_destroy(nfs_rdata_mempool);
725 if (kmem_cache_destroy(nfs_rdata_cachep))
726 printk(KERN_INFO "nfs_read_data: not all structures were freed\n");