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>
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>
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
);
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
;
58 p
->pagevec
= kcalloc(pagecount
, sizeof(struct page
*), GFP_NOFS
);
60 mempool_free(p
, nfs_rdata_mempool
);
68 static void nfs_readdata_free(struct nfs_read_data
*p
)
70 if (p
&& (p
->pagevec
!= &p
->page_array
[0]))
72 mempool_free(p
, nfs_rdata_mempool
);
75 void nfs_readdata_release(void *data
)
77 nfs_readdata_free(data
);
81 unsigned int nfs_page_length(struct inode
*inode
, struct page
*page
)
83 loff_t i_size
= i_size_read(inode
);
88 idx
= (i_size
- 1) >> PAGE_CACHE_SHIFT
;
89 if (page
->index
> idx
)
91 if (page
->index
!= idx
)
92 return PAGE_CACHE_SIZE
;
93 return 1 + ((i_size
- 1) & (PAGE_CACHE_SIZE
- 1));
97 int nfs_return_empty_page(struct page
*page
)
99 memclear_highpage_flush(page
, 0, PAGE_CACHE_SIZE
);
100 SetPageUptodate(page
);
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
;
112 if (data
->res
.eof
== 0 || remainder
== 0)
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
;
122 if (remainder
<= pglen
) {
123 memclear_highpage_flush(*pages
, base
, remainder
);
126 memclear_highpage_flush(*pages
, base
, pglen
);
129 pglen
= PAGE_CACHE_SIZE
;
135 * Read a page synchronously.
137 static int nfs_readpage_sync(struct nfs_open_context
*ctx
, struct inode
*inode
,
140 unsigned int rsize
= NFS_SERVER(inode
)->rsize
;
141 unsigned int count
= PAGE_CACHE_SIZE
;
143 struct nfs_read_data
*rdata
;
145 rdata
= nfs_readdata_alloc(count
);
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.
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
,
176 (long long)NFS_FILEID(inode
),
177 (unsigned long long)rdata
->args
.pgbase
,
181 result
= NFS_PROTO(inode
)->read(rdata
);
185 * Even if we had a partial success we can't mark the page
189 if (result
== -EISDIR
)
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)
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
);
214 nfs_readdata_free(rdata
);
218 static int nfs_readpage_async(struct nfs_open_context
*ctx
, struct inode
*inode
,
221 LIST_HEAD(one_request
);
222 struct nfs_page
*new;
225 len
= nfs_page_length(inode
, page
);
227 return nfs_return_empty_page(page
);
228 new = nfs_create_request(ctx
, inode
, page
, 0, len
);
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
);
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
),
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
)
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
;
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",
290 (long long)NFS_FILEID(inode
),
292 (unsigned long long)data
->args
.offset
);
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
);
316 rpc_clnt_sigmask(clnt
, &oldset
);
318 rpc_execute(&data
->task
);
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
;
346 nfs_list_remove_request(req
);
348 nbytes
= req
->wb_bytes
;
350 size_t len
= min(nbytes
,rsize
);
352 data
= nfs_readdata_alloc(len
);
355 INIT_LIST_HEAD(&data
->pages
);
356 list_add(&data
->pages
, &list
);
359 } while(nbytes
!= 0);
360 atomic_set(&req
->wb_complete
, requests
);
362 ClearPageError(page
);
364 nbytes
= req
->wb_bytes
;
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
,
377 nfs_read_rpcsetup(req
, data
, &nfs_read_partial_ops
,
381 nfs_execute_read(data
);
382 } while (nbytes
!= 0);
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
);
393 nfs_readpage_release(req
);
397 static int nfs_pagein_one(struct list_head
*head
, struct inode
*inode
)
399 struct nfs_page
*req
;
401 struct nfs_read_data
*data
;
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
);
411 INIT_LIST_HEAD(&data
->pages
);
412 pages
= data
->pagevec
;
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
);
429 nfs_async_read_error(head
);
434 nfs_pagein_list(struct list_head
*head
, int rpages
)
436 LIST_HEAD(one_request
);
437 struct nfs_page
*req
;
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
);
451 nfs_async_read_error(head
);
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
);
468 if (nfs_readpage_result(task
, data
) != 0)
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
;
489 count
= data
->args
.count
;
490 if (unlikely(count
== 0))
492 pages
= &data
->args
.pages
[base
>> PAGE_CACHE_SHIFT
];
493 base
&= ~PAGE_CACHE_MASK
;
495 for (;count
>= PAGE_CACHE_SIZE
; count
-= PAGE_CACHE_SIZE
, pages
++)
496 SetPageUptodate(*pages
);
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
;
507 pages
= &data
->args
.pages
[base
>> PAGE_CACHE_SHIFT
];
508 base
&= ~PAGE_CACHE_MASK
;
510 for (;count
>= PAGE_CACHE_SIZE
; count
-= PAGE_CACHE_SIZE
, pages
++)
511 SetPageError(*pages
);
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
);
534 nfs_readpage_set_pages_error(data
);
535 if (nfs_readpage_result(task
, data
) != 0)
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
;
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
);
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
);
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
);
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
;
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
);
618 ctx
= nfs_find_open_context(inode
, NULL
, FMODE_READ
);
622 ctx
= get_nfs_open_context((struct nfs_open_context
*)
624 if (!IS_SYNC(inode
)) {
625 error
= nfs_readpage_async(ctx
, inode
, page
);
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");
633 put_nfs_open_context(ctx
);
641 struct nfs_readdesc
{
642 struct list_head
*head
;
643 struct nfs_open_context
*ctx
;
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;
654 nfs_wb_page(inode
, page
);
655 len
= nfs_page_length(inode
, page
);
657 return nfs_return_empty_page(page
);
658 new = nfs_create_request(desc
->ctx
, inode
, page
, 0, len
);
664 if (len
< PAGE_CACHE_SIZE
)
665 memclear_highpage_flush(page
, len
, PAGE_CACHE_SIZE
- len
);
666 nfs_list_add_request(new, desc
->head
);
670 int nfs_readpages(struct file
*filp
, struct address_space
*mapping
,
671 struct list_head
*pages
, unsigned nr_pages
)
674 struct nfs_readdesc desc
= {
677 struct inode
*inode
= mapping
->host
;
678 struct nfs_server
*server
= NFS_SERVER(inode
);
681 dprintk("NFS: nfs_readpages (%s/%Ld %d)\n",
683 (long long)NFS_FILEID(inode
),
685 nfs_inc_stats(inode
, NFSIOS_VFSREADPAGES
);
688 desc
.ctx
= nfs_find_open_context(inode
, NULL
, FMODE_READ
);
689 if (desc
.ctx
== NULL
)
692 desc
.ctx
= get_nfs_open_context((struct nfs_open_context
*)
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
);
698 nfs_add_stats(inode
, NFSIOS_READPAGES
, err
);
701 put_nfs_open_context(desc
.ctx
);
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
,
711 if (nfs_rdata_cachep
== NULL
)
714 nfs_rdata_mempool
= mempool_create_slab_pool(MIN_POOL_READ
,
716 if (nfs_rdata_mempool
== NULL
)
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");