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/config.h>
19 #include <linux/time.h>
20 #include <linux/kernel.h>
21 #include <linux/errno.h>
22 #include <linux/fcntl.h>
23 #include <linux/stat.h>
25 #include <linux/slab.h>
26 #include <linux/pagemap.h>
27 #include <linux/sunrpc/clnt.h>
28 #include <linux/nfs_fs.h>
29 #include <linux/nfs_page.h>
30 #include <linux/smp_lock.h>
32 #include <asm/system.h>
36 #define NFSDBG_FACILITY NFSDBG_PAGECACHE
38 static int nfs_pagein_one(struct list_head
*, struct inode
*);
39 static const struct rpc_call_ops nfs_read_partial_ops
;
40 static const struct rpc_call_ops nfs_read_full_ops
;
42 static kmem_cache_t
*nfs_rdata_cachep
;
43 static mempool_t
*nfs_rdata_mempool
;
45 #define MIN_POOL_READ (32)
47 struct nfs_read_data
*nfs_readdata_alloc(unsigned int pagecount
)
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 if (pagecount
< NFS_PAGEVEC_SIZE
)
55 p
->pagevec
= &p
->page_array
[0];
57 size_t size
= ++pagecount
* sizeof(struct page
*);
58 p
->pagevec
= kmalloc(size
, GFP_NOFS
);
60 memset(p
->pagevec
, 0, size
);
62 mempool_free(p
, nfs_rdata_mempool
);
70 void nfs_readdata_free(struct nfs_read_data
*p
)
72 if (p
&& (p
->pagevec
!= &p
->page_array
[0]))
74 mempool_free(p
, nfs_rdata_mempool
);
77 void nfs_readdata_release(void *data
)
79 nfs_readdata_free(data
);
83 unsigned int nfs_page_length(struct inode
*inode
, struct page
*page
)
85 loff_t i_size
= i_size_read(inode
);
90 idx
= (i_size
- 1) >> PAGE_CACHE_SHIFT
;
91 if (page
->index
> idx
)
93 if (page
->index
!= idx
)
94 return PAGE_CACHE_SIZE
;
95 return 1 + ((i_size
- 1) & (PAGE_CACHE_SIZE
- 1));
99 int nfs_return_empty_page(struct page
*page
)
101 memclear_highpage_flush(page
, 0, PAGE_CACHE_SIZE
);
102 SetPageUptodate(page
);
107 static void nfs_readpage_truncate_uninitialised_page(struct nfs_read_data
*data
)
109 unsigned int remainder
= data
->args
.count
- data
->res
.count
;
110 unsigned int base
= data
->args
.pgbase
+ data
->res
.count
;
114 if (data
->res
.eof
== 0 || remainder
== 0)
117 * Note: "remainder" can never be negative, since we check for
118 * this in the XDR code.
120 pages
= &data
->args
.pages
[base
>> PAGE_CACHE_SHIFT
];
121 base
&= ~PAGE_CACHE_MASK
;
122 pglen
= PAGE_CACHE_SIZE
- base
;
123 if (pglen
< remainder
)
124 memclear_highpage_flush(*pages
, base
, pglen
);
126 memclear_highpage_flush(*pages
, base
, remainder
);
130 * Read a page synchronously.
132 static int nfs_readpage_sync(struct nfs_open_context
*ctx
, struct inode
*inode
,
135 unsigned int rsize
= NFS_SERVER(inode
)->rsize
;
136 unsigned int count
= PAGE_CACHE_SIZE
;
138 struct nfs_read_data
*rdata
;
140 rdata
= nfs_readdata_alloc(1);
144 memset(rdata
, 0, sizeof(*rdata
));
145 rdata
->flags
= (IS_SWAPFILE(inode
)? NFS_RPC_SWAPFLAGS
: 0);
146 rdata
->cred
= ctx
->cred
;
147 rdata
->inode
= inode
;
148 INIT_LIST_HEAD(&rdata
->pages
);
149 rdata
->args
.fh
= NFS_FH(inode
);
150 rdata
->args
.context
= ctx
;
151 rdata
->args
.pages
= &page
;
152 rdata
->args
.pgbase
= 0UL;
153 rdata
->args
.count
= rsize
;
154 rdata
->res
.fattr
= &rdata
->fattr
;
156 dprintk("NFS: nfs_readpage_sync(%p)\n", page
);
159 * This works now because the socket layer never tries to DMA
160 * into this buffer directly.
164 rdata
->args
.count
= count
;
165 rdata
->res
.count
= rdata
->args
.count
;
166 rdata
->args
.offset
= page_offset(page
) + rdata
->args
.pgbase
;
168 dprintk("NFS: nfs_proc_read(%s, (%s/%Ld), %Lu, %u)\n",
169 NFS_SERVER(inode
)->hostname
,
171 (long long)NFS_FILEID(inode
),
172 (unsigned long long)rdata
->args
.pgbase
,
176 result
= NFS_PROTO(inode
)->read(rdata
);
180 * Even if we had a partial success we can't mark the page
184 if (result
== -EISDIR
)
189 rdata
->args
.pgbase
+= result
;
190 nfs_add_stats(inode
, NFSIOS_SERVERREADBYTES
, result
);
192 /* Note: result == 0 should only happen if we're caching
193 * a write that extends the file and punches a hole.
195 if (rdata
->res
.eof
!= 0 || result
== 0)
198 spin_lock(&inode
->i_lock
);
199 NFS_I(inode
)->cache_validity
|= NFS_INO_INVALID_ATIME
;
200 spin_unlock(&inode
->i_lock
);
202 nfs_readpage_truncate_uninitialised_page(rdata
);
203 if (rdata
->res
.eof
|| rdata
->res
.count
== rdata
->args
.count
)
204 SetPageUptodate(page
);
209 nfs_readdata_free(rdata
);
213 static int nfs_readpage_async(struct nfs_open_context
*ctx
, struct inode
*inode
,
216 LIST_HEAD(one_request
);
217 struct nfs_page
*new;
220 len
= nfs_page_length(inode
, page
);
222 return nfs_return_empty_page(page
);
223 new = nfs_create_request(ctx
, inode
, page
, 0, len
);
228 if (len
< PAGE_CACHE_SIZE
)
229 memclear_highpage_flush(page
, len
, PAGE_CACHE_SIZE
- len
);
231 nfs_list_add_request(new, &one_request
);
232 nfs_pagein_one(&one_request
, inode
);
236 static void nfs_readpage_release(struct nfs_page
*req
)
238 unlock_page(req
->wb_page
);
240 dprintk("NFS: read done (%s/%Ld %d@%Ld)\n",
241 req
->wb_context
->dentry
->d_inode
->i_sb
->s_id
,
242 (long long)NFS_FILEID(req
->wb_context
->dentry
->d_inode
),
244 (long long)req_offset(req
));
245 nfs_clear_request(req
);
246 nfs_release_request(req
);
250 * Set up the NFS read request struct
252 static void nfs_read_rpcsetup(struct nfs_page
*req
, struct nfs_read_data
*data
,
253 const struct rpc_call_ops
*call_ops
,
254 unsigned int count
, unsigned int offset
)
260 data
->inode
= inode
= req
->wb_context
->dentry
->d_inode
;
261 data
->cred
= req
->wb_context
->cred
;
263 data
->args
.fh
= NFS_FH(inode
);
264 data
->args
.offset
= req_offset(req
) + offset
;
265 data
->args
.pgbase
= req
->wb_pgbase
+ offset
;
266 data
->args
.pages
= data
->pagevec
;
267 data
->args
.count
= count
;
268 data
->args
.context
= req
->wb_context
;
270 data
->res
.fattr
= &data
->fattr
;
271 data
->res
.count
= count
;
273 nfs_fattr_init(&data
->fattr
);
275 /* Set up the initial task struct. */
276 flags
= RPC_TASK_ASYNC
| (IS_SWAPFILE(inode
)? NFS_RPC_SWAPFLAGS
: 0);
277 rpc_init_task(&data
->task
, NFS_CLIENT(inode
), flags
, call_ops
, data
);
278 NFS_PROTO(inode
)->read_setup(data
);
280 data
->task
.tk_cookie
= (unsigned long)inode
;
282 dprintk("NFS: %4d initiated read call (req %s/%Ld, %u bytes @ offset %Lu)\n",
285 (long long)NFS_FILEID(inode
),
287 (unsigned long long)data
->args
.offset
);
291 nfs_async_read_error(struct list_head
*head
)
293 struct nfs_page
*req
;
295 while (!list_empty(head
)) {
296 req
= nfs_list_entry(head
->next
);
297 nfs_list_remove_request(req
);
298 SetPageError(req
->wb_page
);
299 nfs_readpage_release(req
);
304 * Start an async read operation
306 static void nfs_execute_read(struct nfs_read_data
*data
)
308 struct rpc_clnt
*clnt
= NFS_CLIENT(data
->inode
);
311 rpc_clnt_sigmask(clnt
, &oldset
);
313 rpc_execute(&data
->task
);
315 rpc_clnt_sigunmask(clnt
, &oldset
);
319 * Generate multiple requests to fill a single page.
321 * We optimize to reduce the number of read operations on the wire. If we
322 * detect that we're reading a page, or an area of a page, that is past the
323 * end of file, we do not generate NFS read operations but just clear the
324 * parts of the page that would have come back zero from the server anyway.
326 * We rely on the cached value of i_size to make this determination; another
327 * client can fill pages on the server past our cached end-of-file, but we
328 * won't see the new data until our attribute cache is updated. This is more
329 * or less conventional NFS client behavior.
331 static int nfs_pagein_multi(struct list_head
*head
, struct inode
*inode
)
333 struct nfs_page
*req
= nfs_list_entry(head
->next
);
334 struct page
*page
= req
->wb_page
;
335 struct nfs_read_data
*data
;
336 unsigned int rsize
= NFS_SERVER(inode
)->rsize
;
337 unsigned int nbytes
, offset
;
341 nfs_list_remove_request(req
);
343 nbytes
= req
->wb_bytes
;
345 data
= nfs_readdata_alloc(1);
348 INIT_LIST_HEAD(&data
->pages
);
349 list_add(&data
->pages
, &list
);
355 atomic_set(&req
->wb_complete
, requests
);
357 ClearPageError(page
);
359 nbytes
= req
->wb_bytes
;
361 data
= list_entry(list
.next
, struct nfs_read_data
, pages
);
362 list_del_init(&data
->pages
);
364 data
->pagevec
[0] = page
;
366 if (nbytes
> rsize
) {
367 nfs_read_rpcsetup(req
, data
, &nfs_read_partial_ops
,
372 nfs_read_rpcsetup(req
, data
, &nfs_read_partial_ops
,
376 nfs_execute_read(data
);
377 } while (nbytes
!= 0);
382 while (!list_empty(&list
)) {
383 data
= list_entry(list
.next
, struct nfs_read_data
, pages
);
384 list_del(&data
->pages
);
385 nfs_readdata_free(data
);
388 nfs_readpage_release(req
);
392 static int nfs_pagein_one(struct list_head
*head
, struct inode
*inode
)
394 struct nfs_page
*req
;
396 struct nfs_read_data
*data
;
399 if (NFS_SERVER(inode
)->rsize
< PAGE_CACHE_SIZE
)
400 return nfs_pagein_multi(head
, inode
);
402 data
= nfs_readdata_alloc(NFS_SERVER(inode
)->rpages
);
406 INIT_LIST_HEAD(&data
->pages
);
407 pages
= data
->pagevec
;
409 while (!list_empty(head
)) {
410 req
= nfs_list_entry(head
->next
);
411 nfs_list_remove_request(req
);
412 nfs_list_add_request(req
, &data
->pages
);
413 ClearPageError(req
->wb_page
);
414 *pages
++ = req
->wb_page
;
415 count
+= req
->wb_bytes
;
417 req
= nfs_list_entry(data
->pages
.next
);
419 nfs_read_rpcsetup(req
, data
, &nfs_read_full_ops
, count
, 0);
421 nfs_execute_read(data
);
424 nfs_async_read_error(head
);
429 nfs_pagein_list(struct list_head
*head
, int rpages
)
431 LIST_HEAD(one_request
);
432 struct nfs_page
*req
;
434 unsigned int pages
= 0;
436 while (!list_empty(head
)) {
437 pages
+= nfs_coalesce_requests(head
, &one_request
, rpages
);
438 req
= nfs_list_entry(one_request
.next
);
439 error
= nfs_pagein_one(&one_request
, req
->wb_context
->dentry
->d_inode
);
446 nfs_async_read_error(head
);
451 * Handle a read reply that fills part of a page.
453 static void nfs_readpage_result_partial(struct rpc_task
*task
, void *calldata
)
455 struct nfs_read_data
*data
= calldata
;
456 struct nfs_page
*req
= data
->req
;
457 struct page
*page
= req
->wb_page
;
459 if (likely(task
->tk_status
>= 0))
460 nfs_readpage_truncate_uninitialised_page(data
);
463 if (nfs_readpage_result(task
, data
) != 0)
465 if (atomic_dec_and_test(&req
->wb_complete
)) {
466 if (!PageError(page
))
467 SetPageUptodate(page
);
468 nfs_readpage_release(req
);
472 static const struct rpc_call_ops nfs_read_partial_ops
= {
473 .rpc_call_done
= nfs_readpage_result_partial
,
474 .rpc_release
= nfs_readdata_release
,
477 static void nfs_readpage_set_pages_uptodate(struct nfs_read_data
*data
)
479 unsigned int count
= data
->res
.count
;
480 unsigned int base
= data
->args
.pgbase
;
483 if (unlikely(count
== 0))
485 pages
= &data
->args
.pages
[base
>> PAGE_CACHE_SHIFT
];
486 base
&= ~PAGE_CACHE_MASK
;
488 for (;count
>= PAGE_CACHE_SIZE
; count
-= PAGE_CACHE_SIZE
, pages
++)
489 SetPageUptodate(*pages
);
491 * Was this an eof or a short read? If the latter, don't mark the page
494 if (count
> 0 && (data
->res
.eof
|| data
->args
.count
== data
->res
.count
))
495 SetPageUptodate(*pages
);
498 static void nfs_readpage_set_pages_error(struct nfs_read_data
*data
)
500 unsigned int count
= data
->args
.count
;
501 unsigned int base
= data
->args
.pgbase
;
504 pages
= &data
->args
.pages
[base
>> PAGE_CACHE_SHIFT
];
505 base
&= ~PAGE_CACHE_MASK
;
507 for (;count
>= PAGE_CACHE_SIZE
; count
-= PAGE_CACHE_SIZE
, pages
++)
508 SetPageError(*pages
);
512 * This is the callback from RPC telling us whether a reply was
513 * received or some error occurred (timeout or socket shutdown).
515 static void nfs_readpage_result_full(struct rpc_task
*task
, void *calldata
)
517 struct nfs_read_data
*data
= calldata
;
520 * Note: nfs_readpage_result may change the values of
521 * data->args. In the multi-page case, we therefore need
522 * to ensure that we call the next nfs_readpage_set_page_uptodate()
523 * first in the multi-page case.
525 if (likely(task
->tk_status
>= 0)) {
526 nfs_readpage_truncate_uninitialised_page(data
);
527 nfs_readpage_set_pages_uptodate(data
);
529 nfs_readpage_set_pages_error(data
);
530 if (nfs_readpage_result(task
, data
) != 0)
532 while (!list_empty(&data
->pages
)) {
533 struct nfs_page
*req
= nfs_list_entry(data
->pages
.next
);
535 nfs_list_remove_request(req
);
536 nfs_readpage_release(req
);
540 static const struct rpc_call_ops nfs_read_full_ops
= {
541 .rpc_call_done
= nfs_readpage_result_full
,
542 .rpc_release
= nfs_readdata_release
,
546 * This is the callback from RPC telling us whether a reply was
547 * received or some error occurred (timeout or socket shutdown).
549 int nfs_readpage_result(struct rpc_task
*task
, struct nfs_read_data
*data
)
551 struct nfs_readargs
*argp
= &data
->args
;
552 struct nfs_readres
*resp
= &data
->res
;
555 dprintk("NFS: %4d nfs_readpage_result, (status %d)\n",
556 task
->tk_pid
, task
->tk_status
);
558 status
= NFS_PROTO(data
->inode
)->read_done(task
, data
);
562 nfs_add_stats(data
->inode
, NFSIOS_SERVERREADBYTES
, resp
->count
);
564 /* Is this a short read? */
565 if (task
->tk_status
>= 0 && resp
->count
< argp
->count
&& !resp
->eof
) {
566 nfs_inc_stats(data
->inode
, NFSIOS_SHORTREAD
);
567 /* Has the server at least made some progress? */
568 if (resp
->count
!= 0) {
569 /* Yes, so retry the read at the end of the data */
570 argp
->offset
+= resp
->count
;
571 argp
->pgbase
+= resp
->count
;
572 argp
->count
-= resp
->count
;
573 rpc_restart_call(task
);
576 task
->tk_status
= -EIO
;
578 spin_lock(&data
->inode
->i_lock
);
579 NFS_I(data
->inode
)->cache_validity
|= NFS_INO_INVALID_ATIME
;
580 spin_unlock(&data
->inode
->i_lock
);
585 * Read a page over NFS.
586 * We read the page synchronously in the following case:
587 * - The error flag is set for this page. This happens only when a
588 * previous async read operation failed.
590 int nfs_readpage(struct file
*file
, struct page
*page
)
592 struct nfs_open_context
*ctx
;
593 struct inode
*inode
= page
->mapping
->host
;
596 dprintk("NFS: nfs_readpage (%p %ld@%lu)\n",
597 page
, PAGE_CACHE_SIZE
, page
->index
);
598 nfs_inc_stats(inode
, NFSIOS_VFSREADPAGE
);
599 nfs_add_stats(inode
, NFSIOS_READPAGES
, 1);
602 * Try to flush any pending writes to the file..
604 * NOTE! Because we own the page lock, there cannot
605 * be any new pending writes generated at this point
606 * for this page (other pages can be written to).
608 error
= nfs_wb_page(inode
, page
);
613 ctx
= nfs_find_open_context(inode
, NULL
, FMODE_READ
);
617 ctx
= get_nfs_open_context((struct nfs_open_context
*)
619 if (!IS_SYNC(inode
)) {
620 error
= nfs_readpage_async(ctx
, inode
, page
);
624 error
= nfs_readpage_sync(ctx
, inode
, page
);
625 if (error
< 0 && IS_SWAPFILE(inode
))
626 printk("Aiee.. nfs swap-in of page failed!\n");
628 put_nfs_open_context(ctx
);
636 struct nfs_readdesc
{
637 struct list_head
*head
;
638 struct nfs_open_context
*ctx
;
642 readpage_async_filler(void *data
, struct page
*page
)
644 struct nfs_readdesc
*desc
= (struct nfs_readdesc
*)data
;
645 struct inode
*inode
= page
->mapping
->host
;
646 struct nfs_page
*new;
649 nfs_wb_page(inode
, page
);
650 len
= nfs_page_length(inode
, page
);
652 return nfs_return_empty_page(page
);
653 new = nfs_create_request(desc
->ctx
, inode
, page
, 0, len
);
659 if (len
< PAGE_CACHE_SIZE
)
660 memclear_highpage_flush(page
, len
, PAGE_CACHE_SIZE
- len
);
661 nfs_list_add_request(new, desc
->head
);
665 int nfs_readpages(struct file
*filp
, struct address_space
*mapping
,
666 struct list_head
*pages
, unsigned nr_pages
)
669 struct nfs_readdesc desc
= {
672 struct inode
*inode
= mapping
->host
;
673 struct nfs_server
*server
= NFS_SERVER(inode
);
676 dprintk("NFS: nfs_readpages (%s/%Ld %d)\n",
678 (long long)NFS_FILEID(inode
),
680 nfs_inc_stats(inode
, NFSIOS_VFSREADPAGES
);
683 desc
.ctx
= nfs_find_open_context(inode
, NULL
, FMODE_READ
);
684 if (desc
.ctx
== NULL
)
687 desc
.ctx
= get_nfs_open_context((struct nfs_open_context
*)
689 ret
= read_cache_pages(mapping
, pages
, readpage_async_filler
, &desc
);
690 if (!list_empty(&head
)) {
691 int err
= nfs_pagein_list(&head
, server
->rpages
);
693 nfs_add_stats(inode
, NFSIOS_READPAGES
, err
);
696 put_nfs_open_context(desc
.ctx
);
700 int nfs_init_readpagecache(void)
702 nfs_rdata_cachep
= kmem_cache_create("nfs_read_data",
703 sizeof(struct nfs_read_data
),
704 0, SLAB_HWCACHE_ALIGN
,
706 if (nfs_rdata_cachep
== NULL
)
709 nfs_rdata_mempool
= mempool_create_slab_pool(MIN_POOL_READ
,
711 if (nfs_rdata_mempool
== NULL
)
717 void nfs_destroy_readpagecache(void)
719 mempool_destroy(nfs_rdata_mempool
);
720 if (kmem_cache_destroy(nfs_rdata_cachep
))
721 printk(KERN_INFO
"nfs_read_data: not all structures were freed\n");