6 * Partial copy of Linus' read cache modifications to fs/nfs/file.c
7 * modified for async RPC by okir@monad.swb.de
10 #include <linux/time.h>
11 #include <linux/kernel.h>
12 #include <linux/errno.h>
13 #include <linux/fcntl.h>
14 #include <linux/stat.h>
16 #include <linux/slab.h>
17 #include <linux/pagemap.h>
18 #include <linux/sunrpc/clnt.h>
19 #include <linux/nfs_fs.h>
20 #include <linux/nfs_page.h>
21 #include <linux/module.h>
30 #define NFSDBG_FACILITY NFSDBG_PAGECACHE
32 static const struct nfs_pageio_ops nfs_pageio_read_ops
;
33 static const struct rpc_call_ops nfs_read_partial_ops
;
34 static const struct rpc_call_ops nfs_read_full_ops
;
36 static struct kmem_cache
*nfs_rdata_cachep
;
38 struct nfs_read_data
*nfs_readdata_alloc(unsigned int pagecount
)
40 struct nfs_read_data
*p
;
42 p
= kmem_cache_zalloc(nfs_rdata_cachep
, GFP_KERNEL
);
44 INIT_LIST_HEAD(&p
->pages
);
45 p
->npages
= pagecount
;
46 if (pagecount
<= ARRAY_SIZE(p
->page_array
))
47 p
->pagevec
= p
->page_array
;
49 p
->pagevec
= kcalloc(pagecount
, sizeof(struct page
*), GFP_KERNEL
);
51 kmem_cache_free(nfs_rdata_cachep
, p
);
59 void nfs_readdata_free(struct nfs_read_data
*p
)
61 if (p
&& (p
->pagevec
!= &p
->page_array
[0]))
63 kmem_cache_free(nfs_rdata_cachep
, p
);
66 void nfs_readdata_release(struct nfs_read_data
*rdata
)
68 put_nfs_open_context(rdata
->args
.context
);
69 nfs_readdata_free(rdata
);
73 int nfs_return_empty_page(struct page
*page
)
75 zero_user(page
, 0, PAGE_CACHE_SIZE
);
76 SetPageUptodate(page
);
81 static void nfs_readpage_truncate_uninitialised_page(struct nfs_read_data
*data
)
83 unsigned int remainder
= data
->args
.count
- data
->res
.count
;
84 unsigned int base
= data
->args
.pgbase
+ data
->res
.count
;
88 if (data
->res
.eof
== 0 || remainder
== 0)
91 * Note: "remainder" can never be negative, since we check for
92 * this in the XDR code.
94 pages
= &data
->args
.pages
[base
>> PAGE_CACHE_SHIFT
];
95 base
&= ~PAGE_CACHE_MASK
;
96 pglen
= PAGE_CACHE_SIZE
- base
;
98 if (remainder
<= pglen
) {
99 zero_user(*pages
, base
, remainder
);
102 zero_user(*pages
, base
, pglen
);
105 pglen
= PAGE_CACHE_SIZE
;
110 void nfs_pageio_init_read_mds(struct nfs_pageio_descriptor
*pgio
,
113 nfs_pageio_init(pgio
, inode
, &nfs_pageio_read_ops
,
114 NFS_SERVER(inode
)->rsize
, 0);
117 void nfs_pageio_reset_read_mds(struct nfs_pageio_descriptor
*pgio
)
119 pgio
->pg_ops
= &nfs_pageio_read_ops
;
120 pgio
->pg_bsize
= NFS_SERVER(pgio
->pg_inode
)->rsize
;
122 EXPORT_SYMBOL_GPL(nfs_pageio_reset_read_mds
);
124 static void nfs_pageio_init_read(struct nfs_pageio_descriptor
*pgio
,
127 if (!pnfs_pageio_init_read(pgio
, inode
))
128 nfs_pageio_init_read_mds(pgio
, inode
);
131 int nfs_readpage_async(struct nfs_open_context
*ctx
, struct inode
*inode
,
134 struct nfs_page
*new;
136 struct nfs_pageio_descriptor pgio
;
138 len
= nfs_page_length(page
);
140 return nfs_return_empty_page(page
);
141 new = nfs_create_request(ctx
, inode
, page
, 0, len
);
146 if (len
< PAGE_CACHE_SIZE
)
147 zero_user_segment(page
, len
, PAGE_CACHE_SIZE
);
149 nfs_pageio_init_read(&pgio
, inode
);
150 nfs_pageio_add_request(&pgio
, new);
151 nfs_pageio_complete(&pgio
);
155 static void nfs_readpage_release(struct nfs_page
*req
)
157 struct inode
*d_inode
= req
->wb_context
->dentry
->d_inode
;
159 if (PageUptodate(req
->wb_page
))
160 nfs_readpage_to_fscache(d_inode
, req
->wb_page
, 0);
162 unlock_page(req
->wb_page
);
164 dprintk("NFS: read done (%s/%Ld %d@%Ld)\n",
165 req
->wb_context
->dentry
->d_inode
->i_sb
->s_id
,
166 (long long)NFS_FILEID(req
->wb_context
->dentry
->d_inode
),
168 (long long)req_offset(req
));
169 nfs_release_request(req
);
172 int nfs_initiate_read(struct nfs_read_data
*data
, struct rpc_clnt
*clnt
,
173 const struct rpc_call_ops
*call_ops
)
175 struct inode
*inode
= data
->inode
;
176 int swap_flags
= IS_SWAPFILE(inode
) ? NFS_RPC_SWAPFLAGS
: 0;
177 struct rpc_task
*task
;
178 struct rpc_message msg
= {
179 .rpc_argp
= &data
->args
,
180 .rpc_resp
= &data
->res
,
181 .rpc_cred
= data
->cred
,
183 struct rpc_task_setup task_setup_data
= {
187 .callback_ops
= call_ops
,
188 .callback_data
= data
,
189 .workqueue
= nfsiod_workqueue
,
190 .flags
= RPC_TASK_ASYNC
| swap_flags
,
193 /* Set up the initial task struct. */
194 NFS_PROTO(inode
)->read_setup(data
, &msg
);
196 dprintk("NFS: %5u initiated read call (req %s/%lld, %u bytes @ "
200 (long long)NFS_FILEID(inode
),
202 (unsigned long long)data
->args
.offset
);
204 task
= rpc_run_task(&task_setup_data
);
206 return PTR_ERR(task
);
210 EXPORT_SYMBOL_GPL(nfs_initiate_read
);
213 * Set up the NFS read request struct
215 static void nfs_read_rpcsetup(struct nfs_page
*req
, struct nfs_read_data
*data
,
216 unsigned int count
, unsigned int offset
)
218 struct inode
*inode
= req
->wb_context
->dentry
->d_inode
;
222 data
->cred
= req
->wb_context
->cred
;
224 data
->args
.fh
= NFS_FH(inode
);
225 data
->args
.offset
= req_offset(req
) + offset
;
226 data
->args
.pgbase
= req
->wb_pgbase
+ offset
;
227 data
->args
.pages
= data
->pagevec
;
228 data
->args
.count
= count
;
229 data
->args
.context
= get_nfs_open_context(req
->wb_context
);
230 data
->args
.lock_context
= req
->wb_lock_context
;
232 data
->res
.fattr
= &data
->fattr
;
233 data
->res
.count
= count
;
235 nfs_fattr_init(&data
->fattr
);
238 static int nfs_do_read(struct nfs_read_data
*data
,
239 const struct rpc_call_ops
*call_ops
)
241 struct inode
*inode
= data
->args
.context
->dentry
->d_inode
;
243 return nfs_initiate_read(data
, NFS_CLIENT(inode
), call_ops
);
247 nfs_do_multiple_reads(struct list_head
*head
,
248 const struct rpc_call_ops
*call_ops
)
250 struct nfs_read_data
*data
;
253 while (!list_empty(head
)) {
256 data
= list_entry(head
->next
, struct nfs_read_data
, list
);
257 list_del_init(&data
->list
);
259 ret2
= nfs_do_read(data
, call_ops
);
267 nfs_async_read_error(struct list_head
*head
)
269 struct nfs_page
*req
;
271 while (!list_empty(head
)) {
272 req
= nfs_list_entry(head
->next
);
273 nfs_list_remove_request(req
);
274 nfs_readpage_release(req
);
279 * Generate multiple requests to fill a single page.
281 * We optimize to reduce the number of read operations on the wire. If we
282 * detect that we're reading a page, or an area of a page, that is past the
283 * end of file, we do not generate NFS read operations but just clear the
284 * parts of the page that would have come back zero from the server anyway.
286 * We rely on the cached value of i_size to make this determination; another
287 * client can fill pages on the server past our cached end-of-file, but we
288 * won't see the new data until our attribute cache is updated. This is more
289 * or less conventional NFS client behavior.
291 static int nfs_pagein_multi(struct nfs_pageio_descriptor
*desc
, struct list_head
*res
)
293 struct nfs_page
*req
= nfs_list_entry(desc
->pg_list
.next
);
294 struct page
*page
= req
->wb_page
;
295 struct nfs_read_data
*data
;
296 size_t rsize
= desc
->pg_bsize
, nbytes
;
301 nfs_list_remove_request(req
);
304 nbytes
= desc
->pg_count
;
306 size_t len
= min(nbytes
,rsize
);
308 data
= nfs_readdata_alloc(1);
311 data
->pagevec
[0] = page
;
312 nfs_read_rpcsetup(req
, data
, len
, offset
);
313 list_add(&data
->list
, res
);
317 } while(nbytes
!= 0);
318 atomic_set(&req
->wb_complete
, requests
);
319 desc
->pg_rpc_callops
= &nfs_read_partial_ops
;
322 while (!list_empty(res
)) {
323 data
= list_entry(res
->next
, struct nfs_read_data
, list
);
324 list_del(&data
->list
);
325 nfs_readdata_free(data
);
327 nfs_readpage_release(req
);
331 static int nfs_pagein_one(struct nfs_pageio_descriptor
*desc
, struct list_head
*res
)
333 struct nfs_page
*req
;
335 struct nfs_read_data
*data
;
336 struct list_head
*head
= &desc
->pg_list
;
339 data
= nfs_readdata_alloc(nfs_page_array_len(desc
->pg_base
,
342 nfs_async_read_error(head
);
347 pages
= data
->pagevec
;
348 while (!list_empty(head
)) {
349 req
= nfs_list_entry(head
->next
);
350 nfs_list_remove_request(req
);
351 nfs_list_add_request(req
, &data
->pages
);
352 *pages
++ = req
->wb_page
;
354 req
= nfs_list_entry(data
->pages
.next
);
356 nfs_read_rpcsetup(req
, data
, desc
->pg_count
, 0);
357 list_add(&data
->list
, res
);
358 desc
->pg_rpc_callops
= &nfs_read_full_ops
;
363 int nfs_generic_pagein(struct nfs_pageio_descriptor
*desc
, struct list_head
*head
)
365 if (desc
->pg_bsize
< PAGE_CACHE_SIZE
)
366 return nfs_pagein_multi(desc
, head
);
367 return nfs_pagein_one(desc
, head
);
370 static int nfs_generic_pg_readpages(struct nfs_pageio_descriptor
*desc
)
375 ret
= nfs_generic_pagein(desc
, &head
);
377 ret
= nfs_do_multiple_reads(&head
, desc
->pg_rpc_callops
);
381 static const struct nfs_pageio_ops nfs_pageio_read_ops
= {
382 .pg_test
= nfs_generic_pg_test
,
383 .pg_doio
= nfs_generic_pg_readpages
,
387 * This is the callback from RPC telling us whether a reply was
388 * received or some error occurred (timeout or socket shutdown).
390 int nfs_readpage_result(struct rpc_task
*task
, struct nfs_read_data
*data
)
394 dprintk("NFS: %s: %5u, (status %d)\n", __func__
, task
->tk_pid
,
397 status
= NFS_PROTO(data
->inode
)->read_done(task
, data
);
401 nfs_add_stats(data
->inode
, NFSIOS_SERVERREADBYTES
, data
->res
.count
);
403 if (task
->tk_status
== -ESTALE
) {
404 set_bit(NFS_INO_STALE
, &NFS_I(data
->inode
)->flags
);
405 nfs_mark_for_revalidate(data
->inode
);
410 static void nfs_readpage_retry(struct rpc_task
*task
, struct nfs_read_data
*data
)
412 struct nfs_readargs
*argp
= &data
->args
;
413 struct nfs_readres
*resp
= &data
->res
;
415 if (resp
->eof
|| resp
->count
== argp
->count
)
418 /* This is a short read! */
419 nfs_inc_stats(data
->inode
, NFSIOS_SHORTREAD
);
420 /* Has the server at least made some progress? */
421 if (resp
->count
== 0)
424 /* Yes, so retry the read at the end of the data */
425 data
->mds_offset
+= resp
->count
;
426 argp
->offset
+= resp
->count
;
427 argp
->pgbase
+= resp
->count
;
428 argp
->count
-= resp
->count
;
429 rpc_restart_call_prepare(task
);
433 * Handle a read reply that fills part of a page.
435 static void nfs_readpage_result_partial(struct rpc_task
*task
, void *calldata
)
437 struct nfs_read_data
*data
= calldata
;
439 if (nfs_readpage_result(task
, data
) != 0)
441 if (task
->tk_status
< 0)
444 nfs_readpage_truncate_uninitialised_page(data
);
445 nfs_readpage_retry(task
, data
);
448 static void nfs_readpage_release_partial(void *calldata
)
450 struct nfs_read_data
*data
= calldata
;
451 struct nfs_page
*req
= data
->req
;
452 struct page
*page
= req
->wb_page
;
453 int status
= data
->task
.tk_status
;
456 set_bit(PG_PARTIAL_READ_FAILED
, &req
->wb_flags
);
458 if (atomic_dec_and_test(&req
->wb_complete
)) {
459 if (!test_bit(PG_PARTIAL_READ_FAILED
, &req
->wb_flags
))
460 SetPageUptodate(page
);
461 nfs_readpage_release(req
);
463 nfs_readdata_release(calldata
);
466 void nfs_read_prepare(struct rpc_task
*task
, void *calldata
)
468 struct nfs_read_data
*data
= calldata
;
469 NFS_PROTO(data
->inode
)->read_rpc_prepare(task
, data
);
472 static const struct rpc_call_ops nfs_read_partial_ops
= {
473 .rpc_call_prepare
= nfs_read_prepare
,
474 .rpc_call_done
= nfs_readpage_result_partial
,
475 .rpc_release
= nfs_readpage_release_partial
,
478 static void nfs_readpage_set_pages_uptodate(struct nfs_read_data
*data
)
480 unsigned int count
= data
->res
.count
;
481 unsigned int base
= data
->args
.pgbase
;
485 count
= data
->args
.count
;
486 if (unlikely(count
== 0))
488 pages
= &data
->args
.pages
[base
>> PAGE_CACHE_SHIFT
];
489 base
&= ~PAGE_CACHE_MASK
;
491 for (;count
>= PAGE_CACHE_SIZE
; count
-= PAGE_CACHE_SIZE
, pages
++)
492 SetPageUptodate(*pages
);
495 /* Was this a short read? */
496 if (data
->res
.eof
|| data
->res
.count
== data
->args
.count
)
497 SetPageUptodate(*pages
);
501 * This is the callback from RPC telling us whether a reply was
502 * received or some error occurred (timeout or socket shutdown).
504 static void nfs_readpage_result_full(struct rpc_task
*task
, void *calldata
)
506 struct nfs_read_data
*data
= calldata
;
508 if (nfs_readpage_result(task
, data
) != 0)
510 if (task
->tk_status
< 0)
513 * Note: nfs_readpage_retry may change the values of
514 * data->args. In the multi-page case, we therefore need
515 * to ensure that we call nfs_readpage_set_pages_uptodate()
518 nfs_readpage_truncate_uninitialised_page(data
);
519 nfs_readpage_set_pages_uptodate(data
);
520 nfs_readpage_retry(task
, data
);
523 static void nfs_readpage_release_full(void *calldata
)
525 struct nfs_read_data
*data
= calldata
;
527 while (!list_empty(&data
->pages
)) {
528 struct nfs_page
*req
= nfs_list_entry(data
->pages
.next
);
530 nfs_list_remove_request(req
);
531 nfs_readpage_release(req
);
533 nfs_readdata_release(calldata
);
536 static const struct rpc_call_ops nfs_read_full_ops
= {
537 .rpc_call_prepare
= nfs_read_prepare
,
538 .rpc_call_done
= nfs_readpage_result_full
,
539 .rpc_release
= nfs_readpage_release_full
,
543 * Read a page over NFS.
544 * We read the page synchronously in the following case:
545 * - The error flag is set for this page. This happens only when a
546 * previous async read operation failed.
548 int nfs_readpage(struct file
*file
, struct page
*page
)
550 struct nfs_open_context
*ctx
;
551 struct inode
*inode
= page
->mapping
->host
;
554 dprintk("NFS: nfs_readpage (%p %ld@%lu)\n",
555 page
, PAGE_CACHE_SIZE
, page
->index
);
556 nfs_inc_stats(inode
, NFSIOS_VFSREADPAGE
);
557 nfs_add_stats(inode
, NFSIOS_READPAGES
, 1);
560 * Try to flush any pending writes to the file..
562 * NOTE! Because we own the page lock, there cannot
563 * be any new pending writes generated at this point
564 * for this page (other pages can be written to).
566 error
= nfs_wb_page(inode
, page
);
569 if (PageUptodate(page
))
573 if (NFS_STALE(inode
))
578 ctx
= nfs_find_open_context(inode
, NULL
, FMODE_READ
);
582 ctx
= get_nfs_open_context(nfs_file_open_context(file
));
584 if (!IS_SYNC(inode
)) {
585 error
= nfs_readpage_from_fscache(ctx
, inode
, page
);
590 error
= nfs_readpage_async(ctx
, inode
, page
);
593 put_nfs_open_context(ctx
);
600 struct nfs_readdesc
{
601 struct nfs_pageio_descriptor
*pgio
;
602 struct nfs_open_context
*ctx
;
606 readpage_async_filler(void *data
, struct page
*page
)
608 struct nfs_readdesc
*desc
= (struct nfs_readdesc
*)data
;
609 struct inode
*inode
= page
->mapping
->host
;
610 struct nfs_page
*new;
614 len
= nfs_page_length(page
);
616 return nfs_return_empty_page(page
);
618 new = nfs_create_request(desc
->ctx
, inode
, page
, 0, len
);
622 if (len
< PAGE_CACHE_SIZE
)
623 zero_user_segment(page
, len
, PAGE_CACHE_SIZE
);
624 if (!nfs_pageio_add_request(desc
->pgio
, new)) {
625 error
= desc
->pgio
->pg_error
;
630 error
= PTR_ERR(new);
636 int nfs_readpages(struct file
*filp
, struct address_space
*mapping
,
637 struct list_head
*pages
, unsigned nr_pages
)
639 struct nfs_pageio_descriptor pgio
;
640 struct nfs_readdesc desc
= {
643 struct inode
*inode
= mapping
->host
;
644 unsigned long npages
;
647 dprintk("NFS: nfs_readpages (%s/%Ld %d)\n",
649 (long long)NFS_FILEID(inode
),
651 nfs_inc_stats(inode
, NFSIOS_VFSREADPAGES
);
653 if (NFS_STALE(inode
))
657 desc
.ctx
= nfs_find_open_context(inode
, NULL
, FMODE_READ
);
658 if (desc
.ctx
== NULL
)
661 desc
.ctx
= get_nfs_open_context(nfs_file_open_context(filp
));
663 /* attempt to read as many of the pages as possible from the cache
664 * - this returns -ENOBUFS immediately if the cookie is negative
666 ret
= nfs_readpages_from_fscache(desc
.ctx
, inode
, mapping
,
669 goto read_complete
; /* all pages were read */
671 nfs_pageio_init_read(&pgio
, inode
);
673 ret
= read_cache_pages(mapping
, pages
, readpage_async_filler
, &desc
);
675 nfs_pageio_complete(&pgio
);
676 npages
= (pgio
.pg_bytes_written
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
677 nfs_add_stats(inode
, NFSIOS_READPAGES
, npages
);
679 put_nfs_open_context(desc
.ctx
);
684 int __init
nfs_init_readpagecache(void)
686 nfs_rdata_cachep
= kmem_cache_create("nfs_read_data",
687 sizeof(struct nfs_read_data
),
688 0, SLAB_HWCACHE_ALIGN
,
690 if (nfs_rdata_cachep
== NULL
)
696 void nfs_destroy_readpagecache(void)
698 kmem_cache_destroy(nfs_rdata_cachep
);