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
);
108 * Read a page synchronously.
110 static int nfs_readpage_sync(struct nfs_open_context
*ctx
, struct inode
*inode
,
113 unsigned int rsize
= NFS_SERVER(inode
)->rsize
;
114 unsigned int count
= PAGE_CACHE_SIZE
;
116 struct nfs_read_data
*rdata
;
118 rdata
= nfs_readdata_alloc(1);
122 memset(rdata
, 0, sizeof(*rdata
));
123 rdata
->flags
= (IS_SWAPFILE(inode
)? NFS_RPC_SWAPFLAGS
: 0);
124 rdata
->cred
= ctx
->cred
;
125 rdata
->inode
= inode
;
126 INIT_LIST_HEAD(&rdata
->pages
);
127 rdata
->args
.fh
= NFS_FH(inode
);
128 rdata
->args
.context
= ctx
;
129 rdata
->args
.pages
= &page
;
130 rdata
->args
.pgbase
= 0UL;
131 rdata
->args
.count
= rsize
;
132 rdata
->res
.fattr
= &rdata
->fattr
;
134 dprintk("NFS: nfs_readpage_sync(%p)\n", page
);
137 * This works now because the socket layer never tries to DMA
138 * into this buffer directly.
142 rdata
->args
.count
= count
;
143 rdata
->res
.count
= rdata
->args
.count
;
144 rdata
->args
.offset
= page_offset(page
) + rdata
->args
.pgbase
;
146 dprintk("NFS: nfs_proc_read(%s, (%s/%Ld), %Lu, %u)\n",
147 NFS_SERVER(inode
)->hostname
,
149 (long long)NFS_FILEID(inode
),
150 (unsigned long long)rdata
->args
.pgbase
,
154 result
= NFS_PROTO(inode
)->read(rdata
);
158 * Even if we had a partial success we can't mark the page
162 if (result
== -EISDIR
)
167 rdata
->args
.pgbase
+= result
;
168 nfs_add_stats(inode
, NFSIOS_SERVERREADBYTES
, result
);
170 /* Note: result == 0 should only happen if we're caching
171 * a write that extends the file and punches a hole.
173 if (rdata
->res
.eof
!= 0 || result
== 0)
176 spin_lock(&inode
->i_lock
);
177 NFS_I(inode
)->cache_validity
|= NFS_INO_INVALID_ATIME
;
178 spin_unlock(&inode
->i_lock
);
181 memclear_highpage_flush(page
, rdata
->args
.pgbase
, count
);
182 SetPageUptodate(page
);
184 ClearPageError(page
);
189 nfs_readdata_free(rdata
);
193 static int nfs_readpage_async(struct nfs_open_context
*ctx
, struct inode
*inode
,
196 LIST_HEAD(one_request
);
197 struct nfs_page
*new;
200 len
= nfs_page_length(inode
, page
);
202 return nfs_return_empty_page(page
);
203 new = nfs_create_request(ctx
, inode
, page
, 0, len
);
208 if (len
< PAGE_CACHE_SIZE
)
209 memclear_highpage_flush(page
, len
, PAGE_CACHE_SIZE
- len
);
211 nfs_list_add_request(new, &one_request
);
212 nfs_pagein_one(&one_request
, inode
);
216 static void nfs_readpage_release(struct nfs_page
*req
)
218 unlock_page(req
->wb_page
);
220 dprintk("NFS: read done (%s/%Ld %d@%Ld)\n",
221 req
->wb_context
->dentry
->d_inode
->i_sb
->s_id
,
222 (long long)NFS_FILEID(req
->wb_context
->dentry
->d_inode
),
224 (long long)req_offset(req
));
225 nfs_clear_request(req
);
226 nfs_release_request(req
);
230 * Set up the NFS read request struct
232 static void nfs_read_rpcsetup(struct nfs_page
*req
, struct nfs_read_data
*data
,
233 const struct rpc_call_ops
*call_ops
,
234 unsigned int count
, unsigned int offset
)
240 data
->inode
= inode
= req
->wb_context
->dentry
->d_inode
;
241 data
->cred
= req
->wb_context
->cred
;
243 data
->args
.fh
= NFS_FH(inode
);
244 data
->args
.offset
= req_offset(req
) + offset
;
245 data
->args
.pgbase
= req
->wb_pgbase
+ offset
;
246 data
->args
.pages
= data
->pagevec
;
247 data
->args
.count
= count
;
248 data
->args
.context
= req
->wb_context
;
250 data
->res
.fattr
= &data
->fattr
;
251 data
->res
.count
= count
;
253 nfs_fattr_init(&data
->fattr
);
255 /* Set up the initial task struct. */
256 flags
= RPC_TASK_ASYNC
| (IS_SWAPFILE(inode
)? NFS_RPC_SWAPFLAGS
: 0);
257 rpc_init_task(&data
->task
, NFS_CLIENT(inode
), flags
, call_ops
, data
);
258 NFS_PROTO(inode
)->read_setup(data
);
260 data
->task
.tk_cookie
= (unsigned long)inode
;
262 dprintk("NFS: %4d initiated read call (req %s/%Ld, %u bytes @ offset %Lu)\n",
265 (long long)NFS_FILEID(inode
),
267 (unsigned long long)data
->args
.offset
);
271 nfs_async_read_error(struct list_head
*head
)
273 struct nfs_page
*req
;
275 while (!list_empty(head
)) {
276 req
= nfs_list_entry(head
->next
);
277 nfs_list_remove_request(req
);
278 SetPageError(req
->wb_page
);
279 nfs_readpage_release(req
);
284 * Start an async read operation
286 static void nfs_execute_read(struct nfs_read_data
*data
)
288 struct rpc_clnt
*clnt
= NFS_CLIENT(data
->inode
);
291 rpc_clnt_sigmask(clnt
, &oldset
);
293 rpc_execute(&data
->task
);
295 rpc_clnt_sigunmask(clnt
, &oldset
);
299 * Generate multiple requests to fill a single page.
301 * We optimize to reduce the number of read operations on the wire. If we
302 * detect that we're reading a page, or an area of a page, that is past the
303 * end of file, we do not generate NFS read operations but just clear the
304 * parts of the page that would have come back zero from the server anyway.
306 * We rely on the cached value of i_size to make this determination; another
307 * client can fill pages on the server past our cached end-of-file, but we
308 * won't see the new data until our attribute cache is updated. This is more
309 * or less conventional NFS client behavior.
311 static int nfs_pagein_multi(struct list_head
*head
, struct inode
*inode
)
313 struct nfs_page
*req
= nfs_list_entry(head
->next
);
314 struct page
*page
= req
->wb_page
;
315 struct nfs_read_data
*data
;
316 unsigned int rsize
= NFS_SERVER(inode
)->rsize
;
317 unsigned int nbytes
, offset
;
321 nfs_list_remove_request(req
);
323 nbytes
= req
->wb_bytes
;
325 data
= nfs_readdata_alloc(1);
328 INIT_LIST_HEAD(&data
->pages
);
329 list_add(&data
->pages
, &list
);
335 atomic_set(&req
->wb_complete
, requests
);
337 ClearPageError(page
);
339 nbytes
= req
->wb_bytes
;
341 data
= list_entry(list
.next
, struct nfs_read_data
, pages
);
342 list_del_init(&data
->pages
);
344 data
->pagevec
[0] = page
;
346 if (nbytes
> rsize
) {
347 nfs_read_rpcsetup(req
, data
, &nfs_read_partial_ops
,
352 nfs_read_rpcsetup(req
, data
, &nfs_read_partial_ops
,
356 nfs_execute_read(data
);
357 } while (nbytes
!= 0);
362 while (!list_empty(&list
)) {
363 data
= list_entry(list
.next
, struct nfs_read_data
, pages
);
364 list_del(&data
->pages
);
365 nfs_readdata_free(data
);
368 nfs_readpage_release(req
);
372 static int nfs_pagein_one(struct list_head
*head
, struct inode
*inode
)
374 struct nfs_page
*req
;
376 struct nfs_read_data
*data
;
379 if (NFS_SERVER(inode
)->rsize
< PAGE_CACHE_SIZE
)
380 return nfs_pagein_multi(head
, inode
);
382 data
= nfs_readdata_alloc(NFS_SERVER(inode
)->rpages
);
386 INIT_LIST_HEAD(&data
->pages
);
387 pages
= data
->pagevec
;
389 while (!list_empty(head
)) {
390 req
= nfs_list_entry(head
->next
);
391 nfs_list_remove_request(req
);
392 nfs_list_add_request(req
, &data
->pages
);
393 ClearPageError(req
->wb_page
);
394 *pages
++ = req
->wb_page
;
395 count
+= req
->wb_bytes
;
397 req
= nfs_list_entry(data
->pages
.next
);
399 nfs_read_rpcsetup(req
, data
, &nfs_read_full_ops
, count
, 0);
401 nfs_execute_read(data
);
404 nfs_async_read_error(head
);
409 nfs_pagein_list(struct list_head
*head
, int rpages
)
411 LIST_HEAD(one_request
);
412 struct nfs_page
*req
;
414 unsigned int pages
= 0;
416 while (!list_empty(head
)) {
417 pages
+= nfs_coalesce_requests(head
, &one_request
, rpages
);
418 req
= nfs_list_entry(one_request
.next
);
419 error
= nfs_pagein_one(&one_request
, req
->wb_context
->dentry
->d_inode
);
426 nfs_async_read_error(head
);
431 * Handle a read reply that fills part of a page.
433 static void nfs_readpage_result_partial(struct rpc_task
*task
, void *calldata
)
435 struct nfs_read_data
*data
= calldata
;
436 struct nfs_page
*req
= data
->req
;
437 struct page
*page
= req
->wb_page
;
439 if (nfs_readpage_result(task
, data
) != 0)
441 if (task
->tk_status
>= 0) {
442 unsigned int request
= data
->args
.count
;
443 unsigned int result
= data
->res
.count
;
445 if (result
< request
) {
446 memclear_highpage_flush(page
,
447 data
->args
.pgbase
+ result
,
453 if (atomic_dec_and_test(&req
->wb_complete
)) {
454 if (!PageError(page
))
455 SetPageUptodate(page
);
456 nfs_readpage_release(req
);
460 static const struct rpc_call_ops nfs_read_partial_ops
= {
461 .rpc_call_done
= nfs_readpage_result_partial
,
462 .rpc_release
= nfs_readdata_release
,
466 * This is the callback from RPC telling us whether a reply was
467 * received or some error occurred (timeout or socket shutdown).
469 static void nfs_readpage_result_full(struct rpc_task
*task
, void *calldata
)
471 struct nfs_read_data
*data
= calldata
;
472 unsigned int count
= data
->res
.count
;
474 if (nfs_readpage_result(task
, data
) != 0)
476 while (!list_empty(&data
->pages
)) {
477 struct nfs_page
*req
= nfs_list_entry(data
->pages
.next
);
478 struct page
*page
= req
->wb_page
;
479 nfs_list_remove_request(req
);
481 if (task
->tk_status
>= 0) {
482 if (count
< PAGE_CACHE_SIZE
) {
483 if (count
< req
->wb_bytes
)
484 memclear_highpage_flush(page
,
485 req
->wb_pgbase
+ count
,
486 req
->wb_bytes
- count
);
489 count
-= PAGE_CACHE_SIZE
;
490 SetPageUptodate(page
);
493 nfs_readpage_release(req
);
497 static const struct rpc_call_ops nfs_read_full_ops
= {
498 .rpc_call_done
= nfs_readpage_result_full
,
499 .rpc_release
= nfs_readdata_release
,
503 * This is the callback from RPC telling us whether a reply was
504 * received or some error occurred (timeout or socket shutdown).
506 int nfs_readpage_result(struct rpc_task
*task
, struct nfs_read_data
*data
)
508 struct nfs_readargs
*argp
= &data
->args
;
509 struct nfs_readres
*resp
= &data
->res
;
512 dprintk("NFS: %4d nfs_readpage_result, (status %d)\n",
513 task
->tk_pid
, task
->tk_status
);
515 status
= NFS_PROTO(data
->inode
)->read_done(task
, data
);
519 nfs_add_stats(data
->inode
, NFSIOS_SERVERREADBYTES
, resp
->count
);
521 /* Is this a short read? */
522 if (task
->tk_status
>= 0 && resp
->count
< argp
->count
&& !resp
->eof
) {
523 nfs_inc_stats(data
->inode
, NFSIOS_SHORTREAD
);
524 /* Has the server at least made some progress? */
525 if (resp
->count
!= 0) {
526 /* Yes, so retry the read at the end of the data */
527 argp
->offset
+= resp
->count
;
528 argp
->pgbase
+= resp
->count
;
529 argp
->count
-= resp
->count
;
530 rpc_restart_call(task
);
533 task
->tk_status
= -EIO
;
535 spin_lock(&data
->inode
->i_lock
);
536 NFS_I(data
->inode
)->cache_validity
|= NFS_INO_INVALID_ATIME
;
537 spin_unlock(&data
->inode
->i_lock
);
542 * Read a page over NFS.
543 * We read the page synchronously in the following case:
544 * - The error flag is set for this page. This happens only when a
545 * previous async read operation failed.
547 int nfs_readpage(struct file
*file
, struct page
*page
)
549 struct nfs_open_context
*ctx
;
550 struct inode
*inode
= page
->mapping
->host
;
553 dprintk("NFS: nfs_readpage (%p %ld@%lu)\n",
554 page
, PAGE_CACHE_SIZE
, page
->index
);
555 nfs_inc_stats(inode
, NFSIOS_VFSREADPAGE
);
556 nfs_add_stats(inode
, NFSIOS_READPAGES
, 1);
559 * Try to flush any pending writes to the file..
561 * NOTE! Because we own the page lock, there cannot
562 * be any new pending writes generated at this point
563 * for this page (other pages can be written to).
565 error
= nfs_wb_page(inode
, page
);
570 ctx
= nfs_find_open_context(inode
, NULL
, FMODE_READ
);
574 ctx
= get_nfs_open_context((struct nfs_open_context
*)
576 if (!IS_SYNC(inode
)) {
577 error
= nfs_readpage_async(ctx
, inode
, page
);
581 error
= nfs_readpage_sync(ctx
, inode
, page
);
582 if (error
< 0 && IS_SWAPFILE(inode
))
583 printk("Aiee.. nfs swap-in of page failed!\n");
585 put_nfs_open_context(ctx
);
593 struct nfs_readdesc
{
594 struct list_head
*head
;
595 struct nfs_open_context
*ctx
;
599 readpage_async_filler(void *data
, struct page
*page
)
601 struct nfs_readdesc
*desc
= (struct nfs_readdesc
*)data
;
602 struct inode
*inode
= page
->mapping
->host
;
603 struct nfs_page
*new;
606 nfs_wb_page(inode
, page
);
607 len
= nfs_page_length(inode
, page
);
609 return nfs_return_empty_page(page
);
610 new = nfs_create_request(desc
->ctx
, inode
, page
, 0, len
);
616 if (len
< PAGE_CACHE_SIZE
)
617 memclear_highpage_flush(page
, len
, PAGE_CACHE_SIZE
- len
);
618 nfs_list_add_request(new, desc
->head
);
622 int nfs_readpages(struct file
*filp
, struct address_space
*mapping
,
623 struct list_head
*pages
, unsigned nr_pages
)
626 struct nfs_readdesc desc
= {
629 struct inode
*inode
= mapping
->host
;
630 struct nfs_server
*server
= NFS_SERVER(inode
);
633 dprintk("NFS: nfs_readpages (%s/%Ld %d)\n",
635 (long long)NFS_FILEID(inode
),
637 nfs_inc_stats(inode
, NFSIOS_VFSREADPAGES
);
640 desc
.ctx
= nfs_find_open_context(inode
, NULL
, FMODE_READ
);
641 if (desc
.ctx
== NULL
)
644 desc
.ctx
= get_nfs_open_context((struct nfs_open_context
*)
646 ret
= read_cache_pages(mapping
, pages
, readpage_async_filler
, &desc
);
647 if (!list_empty(&head
)) {
648 int err
= nfs_pagein_list(&head
, server
->rpages
);
650 nfs_add_stats(inode
, NFSIOS_READPAGES
, err
);
653 put_nfs_open_context(desc
.ctx
);
657 int nfs_init_readpagecache(void)
659 nfs_rdata_cachep
= kmem_cache_create("nfs_read_data",
660 sizeof(struct nfs_read_data
),
661 0, SLAB_HWCACHE_ALIGN
,
663 if (nfs_rdata_cachep
== NULL
)
666 nfs_rdata_mempool
= mempool_create_slab_pool(MIN_POOL_READ
,
668 if (nfs_rdata_mempool
== NULL
)
674 void nfs_destroy_readpagecache(void)
676 mempool_destroy(nfs_rdata_mempool
);
677 if (kmem_cache_destroy(nfs_rdata_cachep
))
678 printk(KERN_INFO
"nfs_read_data: not all structures were freed\n");