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 if (rdata
->res
.eof
|| rdata
->res
.count
== rdata
->args
.count
) {
208 SetPageUptodate(page
);
209 if (rdata
->res
.eof
&& count
!= 0)
210 memclear_highpage_flush(page
, rdata
->args
.pgbase
, count
);
216 nfs_readdata_free(rdata
);
220 static int nfs_readpage_async(struct nfs_open_context
*ctx
, struct inode
*inode
,
223 LIST_HEAD(one_request
);
224 struct nfs_page
*new;
227 len
= nfs_page_length(inode
, page
);
229 return nfs_return_empty_page(page
);
230 new = nfs_create_request(ctx
, inode
, page
, 0, len
);
235 if (len
< PAGE_CACHE_SIZE
)
236 memclear_highpage_flush(page
, len
, PAGE_CACHE_SIZE
- len
);
238 nfs_list_add_request(new, &one_request
);
239 nfs_pagein_one(&one_request
, inode
);
243 static void nfs_readpage_release(struct nfs_page
*req
)
245 unlock_page(req
->wb_page
);
247 dprintk("NFS: read done (%s/%Ld %d@%Ld)\n",
248 req
->wb_context
->dentry
->d_inode
->i_sb
->s_id
,
249 (long long)NFS_FILEID(req
->wb_context
->dentry
->d_inode
),
251 (long long)req_offset(req
));
252 nfs_clear_request(req
);
253 nfs_release_request(req
);
257 * Set up the NFS read request struct
259 static void nfs_read_rpcsetup(struct nfs_page
*req
, struct nfs_read_data
*data
,
260 const struct rpc_call_ops
*call_ops
,
261 unsigned int count
, unsigned int offset
)
267 data
->inode
= inode
= req
->wb_context
->dentry
->d_inode
;
268 data
->cred
= req
->wb_context
->cred
;
270 data
->args
.fh
= NFS_FH(inode
);
271 data
->args
.offset
= req_offset(req
) + offset
;
272 data
->args
.pgbase
= req
->wb_pgbase
+ offset
;
273 data
->args
.pages
= data
->pagevec
;
274 data
->args
.count
= count
;
275 data
->args
.context
= req
->wb_context
;
277 data
->res
.fattr
= &data
->fattr
;
278 data
->res
.count
= count
;
280 nfs_fattr_init(&data
->fattr
);
282 /* Set up the initial task struct. */
283 flags
= RPC_TASK_ASYNC
| (IS_SWAPFILE(inode
)? NFS_RPC_SWAPFLAGS
: 0);
284 rpc_init_task(&data
->task
, NFS_CLIENT(inode
), flags
, call_ops
, data
);
285 NFS_PROTO(inode
)->read_setup(data
);
287 data
->task
.tk_cookie
= (unsigned long)inode
;
289 dprintk("NFS: %4d initiated read call (req %s/%Ld, %u bytes @ offset %Lu)\n",
292 (long long)NFS_FILEID(inode
),
294 (unsigned long long)data
->args
.offset
);
298 nfs_async_read_error(struct list_head
*head
)
300 struct nfs_page
*req
;
302 while (!list_empty(head
)) {
303 req
= nfs_list_entry(head
->next
);
304 nfs_list_remove_request(req
);
305 SetPageError(req
->wb_page
);
306 nfs_readpage_release(req
);
311 * Start an async read operation
313 static void nfs_execute_read(struct nfs_read_data
*data
)
315 struct rpc_clnt
*clnt
= NFS_CLIENT(data
->inode
);
318 rpc_clnt_sigmask(clnt
, &oldset
);
320 rpc_execute(&data
->task
);
322 rpc_clnt_sigunmask(clnt
, &oldset
);
326 * Generate multiple requests to fill a single page.
328 * We optimize to reduce the number of read operations on the wire. If we
329 * detect that we're reading a page, or an area of a page, that is past the
330 * end of file, we do not generate NFS read operations but just clear the
331 * parts of the page that would have come back zero from the server anyway.
333 * We rely on the cached value of i_size to make this determination; another
334 * client can fill pages on the server past our cached end-of-file, but we
335 * won't see the new data until our attribute cache is updated. This is more
336 * or less conventional NFS client behavior.
338 static int nfs_pagein_multi(struct list_head
*head
, struct inode
*inode
)
340 struct nfs_page
*req
= nfs_list_entry(head
->next
);
341 struct page
*page
= req
->wb_page
;
342 struct nfs_read_data
*data
;
343 size_t rsize
= NFS_SERVER(inode
)->rsize
, nbytes
;
348 nfs_list_remove_request(req
);
350 nbytes
= req
->wb_bytes
;
352 size_t len
= min(nbytes
,rsize
);
354 data
= nfs_readdata_alloc(len
);
357 INIT_LIST_HEAD(&data
->pages
);
358 list_add(&data
->pages
, &list
);
361 } while(nbytes
!= 0);
362 atomic_set(&req
->wb_complete
, requests
);
364 ClearPageError(page
);
366 nbytes
= req
->wb_bytes
;
368 data
= list_entry(list
.next
, struct nfs_read_data
, pages
);
369 list_del_init(&data
->pages
);
371 data
->pagevec
[0] = page
;
373 if (nbytes
> rsize
) {
374 nfs_read_rpcsetup(req
, data
, &nfs_read_partial_ops
,
379 nfs_read_rpcsetup(req
, data
, &nfs_read_partial_ops
,
383 nfs_execute_read(data
);
384 } while (nbytes
!= 0);
389 while (!list_empty(&list
)) {
390 data
= list_entry(list
.next
, struct nfs_read_data
, pages
);
391 list_del(&data
->pages
);
392 nfs_readdata_free(data
);
395 nfs_readpage_release(req
);
399 static int nfs_pagein_one(struct list_head
*head
, struct inode
*inode
)
401 struct nfs_page
*req
;
403 struct nfs_read_data
*data
;
406 if (NFS_SERVER(inode
)->rsize
< PAGE_CACHE_SIZE
)
407 return nfs_pagein_multi(head
, inode
);
409 data
= nfs_readdata_alloc(NFS_SERVER(inode
)->rsize
);
413 INIT_LIST_HEAD(&data
->pages
);
414 pages
= data
->pagevec
;
416 while (!list_empty(head
)) {
417 req
= nfs_list_entry(head
->next
);
418 nfs_list_remove_request(req
);
419 nfs_list_add_request(req
, &data
->pages
);
420 ClearPageError(req
->wb_page
);
421 *pages
++ = req
->wb_page
;
422 count
+= req
->wb_bytes
;
424 req
= nfs_list_entry(data
->pages
.next
);
426 nfs_read_rpcsetup(req
, data
, &nfs_read_full_ops
, count
, 0);
428 nfs_execute_read(data
);
431 nfs_async_read_error(head
);
436 nfs_pagein_list(struct list_head
*head
, int rpages
)
438 LIST_HEAD(one_request
);
439 struct nfs_page
*req
;
441 unsigned int pages
= 0;
443 while (!list_empty(head
)) {
444 pages
+= nfs_coalesce_requests(head
, &one_request
, rpages
);
445 req
= nfs_list_entry(one_request
.next
);
446 error
= nfs_pagein_one(&one_request
, req
->wb_context
->dentry
->d_inode
);
453 nfs_async_read_error(head
);
458 * Handle a read reply that fills part of a page.
460 static void nfs_readpage_result_partial(struct rpc_task
*task
, void *calldata
)
462 struct nfs_read_data
*data
= calldata
;
463 struct nfs_page
*req
= data
->req
;
464 struct page
*page
= req
->wb_page
;
466 if (likely(task
->tk_status
>= 0))
467 nfs_readpage_truncate_uninitialised_page(data
);
470 if (nfs_readpage_result(task
, data
) != 0)
472 if (atomic_dec_and_test(&req
->wb_complete
)) {
473 if (!PageError(page
))
474 SetPageUptodate(page
);
475 nfs_readpage_release(req
);
479 static const struct rpc_call_ops nfs_read_partial_ops
= {
480 .rpc_call_done
= nfs_readpage_result_partial
,
481 .rpc_release
= nfs_readdata_release
,
484 static void nfs_readpage_set_pages_uptodate(struct nfs_read_data
*data
)
486 unsigned int count
= data
->res
.count
;
487 unsigned int base
= data
->args
.pgbase
;
491 count
= data
->args
.count
;
492 if (unlikely(count
== 0))
494 pages
= &data
->args
.pages
[base
>> PAGE_CACHE_SHIFT
];
495 base
&= ~PAGE_CACHE_MASK
;
497 for (;count
>= PAGE_CACHE_SIZE
; count
-= PAGE_CACHE_SIZE
, pages
++)
498 SetPageUptodate(*pages
);
500 SetPageUptodate(*pages
);
503 static void nfs_readpage_set_pages_error(struct nfs_read_data
*data
)
505 unsigned int count
= data
->args
.count
;
506 unsigned int base
= data
->args
.pgbase
;
509 pages
= &data
->args
.pages
[base
>> PAGE_CACHE_SHIFT
];
510 base
&= ~PAGE_CACHE_MASK
;
512 for (;count
>= PAGE_CACHE_SIZE
; count
-= PAGE_CACHE_SIZE
, pages
++)
513 SetPageError(*pages
);
515 SetPageError(*pages
);
519 * This is the callback from RPC telling us whether a reply was
520 * received or some error occurred (timeout or socket shutdown).
522 static void nfs_readpage_result_full(struct rpc_task
*task
, void *calldata
)
524 struct nfs_read_data
*data
= calldata
;
527 * Note: nfs_readpage_result may change the values of
528 * data->args. In the multi-page case, we therefore need
529 * to ensure that we call the next nfs_readpage_set_page_uptodate()
530 * first in the multi-page case.
532 if (likely(task
->tk_status
>= 0)) {
533 nfs_readpage_truncate_uninitialised_page(data
);
534 nfs_readpage_set_pages_uptodate(data
);
536 nfs_readpage_set_pages_error(data
);
537 if (nfs_readpage_result(task
, data
) != 0)
539 while (!list_empty(&data
->pages
)) {
540 struct nfs_page
*req
= nfs_list_entry(data
->pages
.next
);
542 nfs_list_remove_request(req
);
543 nfs_readpage_release(req
);
547 static const struct rpc_call_ops nfs_read_full_ops
= {
548 .rpc_call_done
= nfs_readpage_result_full
,
549 .rpc_release
= nfs_readdata_release
,
553 * This is the callback from RPC telling us whether a reply was
554 * received or some error occurred (timeout or socket shutdown).
556 int nfs_readpage_result(struct rpc_task
*task
, struct nfs_read_data
*data
)
558 struct nfs_readargs
*argp
= &data
->args
;
559 struct nfs_readres
*resp
= &data
->res
;
562 dprintk("NFS: %4d nfs_readpage_result, (status %d)\n",
563 task
->tk_pid
, task
->tk_status
);
565 status
= NFS_PROTO(data
->inode
)->read_done(task
, data
);
569 nfs_add_stats(data
->inode
, NFSIOS_SERVERREADBYTES
, resp
->count
);
571 /* Is this a short read? */
572 if (task
->tk_status
>= 0 && resp
->count
< argp
->count
&& !resp
->eof
) {
573 nfs_inc_stats(data
->inode
, NFSIOS_SHORTREAD
);
574 /* Has the server at least made some progress? */
575 if (resp
->count
!= 0) {
576 /* Yes, so retry the read at the end of the data */
577 argp
->offset
+= resp
->count
;
578 argp
->pgbase
+= resp
->count
;
579 argp
->count
-= resp
->count
;
580 rpc_restart_call(task
);
583 task
->tk_status
= -EIO
;
585 spin_lock(&data
->inode
->i_lock
);
586 NFS_I(data
->inode
)->cache_validity
|= NFS_INO_INVALID_ATIME
;
587 spin_unlock(&data
->inode
->i_lock
);
592 * Read a page over NFS.
593 * We read the page synchronously in the following case:
594 * - The error flag is set for this page. This happens only when a
595 * previous async read operation failed.
597 int nfs_readpage(struct file
*file
, struct page
*page
)
599 struct nfs_open_context
*ctx
;
600 struct inode
*inode
= page
->mapping
->host
;
603 dprintk("NFS: nfs_readpage (%p %ld@%lu)\n",
604 page
, PAGE_CACHE_SIZE
, page
->index
);
605 nfs_inc_stats(inode
, NFSIOS_VFSREADPAGE
);
606 nfs_add_stats(inode
, NFSIOS_READPAGES
, 1);
609 * Try to flush any pending writes to the file..
611 * NOTE! Because we own the page lock, there cannot
612 * be any new pending writes generated at this point
613 * for this page (other pages can be written to).
615 error
= nfs_wb_page(inode
, page
);
620 ctx
= nfs_find_open_context(inode
, NULL
, FMODE_READ
);
624 ctx
= get_nfs_open_context((struct nfs_open_context
*)
626 if (!IS_SYNC(inode
)) {
627 error
= nfs_readpage_async(ctx
, inode
, page
);
631 error
= nfs_readpage_sync(ctx
, inode
, page
);
632 if (error
< 0 && IS_SWAPFILE(inode
))
633 printk("Aiee.. nfs swap-in of page failed!\n");
635 put_nfs_open_context(ctx
);
643 struct nfs_readdesc
{
644 struct list_head
*head
;
645 struct nfs_open_context
*ctx
;
649 readpage_async_filler(void *data
, struct page
*page
)
651 struct nfs_readdesc
*desc
= (struct nfs_readdesc
*)data
;
652 struct inode
*inode
= page
->mapping
->host
;
653 struct nfs_page
*new;
656 nfs_wb_page(inode
, page
);
657 len
= nfs_page_length(inode
, page
);
659 return nfs_return_empty_page(page
);
660 new = nfs_create_request(desc
->ctx
, inode
, page
, 0, len
);
666 if (len
< PAGE_CACHE_SIZE
)
667 memclear_highpage_flush(page
, len
, PAGE_CACHE_SIZE
- len
);
668 nfs_list_add_request(new, desc
->head
);
672 int nfs_readpages(struct file
*filp
, struct address_space
*mapping
,
673 struct list_head
*pages
, unsigned nr_pages
)
676 struct nfs_readdesc desc
= {
679 struct inode
*inode
= mapping
->host
;
680 struct nfs_server
*server
= NFS_SERVER(inode
);
683 dprintk("NFS: nfs_readpages (%s/%Ld %d)\n",
685 (long long)NFS_FILEID(inode
),
687 nfs_inc_stats(inode
, NFSIOS_VFSREADPAGES
);
690 desc
.ctx
= nfs_find_open_context(inode
, NULL
, FMODE_READ
);
691 if (desc
.ctx
== NULL
)
694 desc
.ctx
= get_nfs_open_context((struct nfs_open_context
*)
696 ret
= read_cache_pages(mapping
, pages
, readpage_async_filler
, &desc
);
697 if (!list_empty(&head
)) {
698 int err
= nfs_pagein_list(&head
, server
->rpages
);
700 nfs_add_stats(inode
, NFSIOS_READPAGES
, err
);
703 put_nfs_open_context(desc
.ctx
);
707 int __init
nfs_init_readpagecache(void)
709 nfs_rdata_cachep
= kmem_cache_create("nfs_read_data",
710 sizeof(struct nfs_read_data
),
711 0, SLAB_HWCACHE_ALIGN
,
713 if (nfs_rdata_cachep
== NULL
)
716 nfs_rdata_mempool
= mempool_create_slab_pool(MIN_POOL_READ
,
718 if (nfs_rdata_mempool
== NULL
)
724 void nfs_destroy_readpagecache(void)
726 mempool_destroy(nfs_rdata_mempool
);
727 if (kmem_cache_destroy(nfs_rdata_cachep
))
728 printk(KERN_INFO
"nfs_read_data: not all structures were freed\n");