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/smp_lock.h>
23 #include <asm/system.h>
28 #define NFSDBG_FACILITY NFSDBG_PAGECACHE
30 static int nfs_pagein_multi(struct inode *, struct list_head *, size_t, int);
31 static int nfs_pagein_one(struct inode *, struct list_head *, size_t, int);
32 static const struct rpc_call_ops nfs_read_partial_ops;
33 static const struct rpc_call_ops nfs_read_full_ops;
35 static struct kmem_cache *nfs_rdata_cachep;
36 static mempool_t *nfs_rdata_mempool;
38 #define MIN_POOL_READ (32)
40 struct nfs_read_data *nfs_readdata_alloc(size_t len)
42 unsigned int pagecount = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
43 struct nfs_read_data *p = mempool_alloc(nfs_rdata_mempool, GFP_NOFS);
46 memset(p, 0, sizeof(*p));
47 INIT_LIST_HEAD(&p->pages);
48 p->npages = pagecount;
49 if (pagecount <= ARRAY_SIZE(p->page_array))
50 p->pagevec = p->page_array;
52 p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
54 mempool_free(p, nfs_rdata_mempool);
62 static void nfs_readdata_rcu_free(struct rcu_head *head)
64 struct nfs_read_data *p = container_of(head, struct nfs_read_data, task.u.tk_rcu);
65 if (p && (p->pagevec != &p->page_array[0]))
67 mempool_free(p, nfs_rdata_mempool);
70 static void nfs_readdata_free(struct nfs_read_data *rdata)
72 call_rcu_bh(&rdata->task.u.tk_rcu, nfs_readdata_rcu_free);
75 void nfs_readdata_release(void *data)
77 nfs_readdata_free(data);
81 int nfs_return_empty_page(struct page *page)
83 memclear_highpage_flush(page, 0, PAGE_CACHE_SIZE);
84 SetPageUptodate(page);
89 static void nfs_readpage_truncate_uninitialised_page(struct nfs_read_data *data)
91 unsigned int remainder = data->args.count - data->res.count;
92 unsigned int base = data->args.pgbase + data->res.count;
96 if (data->res.eof == 0 || remainder == 0)
99 * Note: "remainder" can never be negative, since we check for
100 * this in the XDR code.
102 pages = &data->args.pages[base >> PAGE_CACHE_SHIFT];
103 base &= ~PAGE_CACHE_MASK;
104 pglen = PAGE_CACHE_SIZE - base;
106 if (remainder <= pglen) {
107 memclear_highpage_flush(*pages, base, remainder);
110 memclear_highpage_flush(*pages, base, pglen);
113 pglen = PAGE_CACHE_SIZE;
118 static int nfs_readpage_async(struct nfs_open_context *ctx, struct inode *inode,
121 LIST_HEAD(one_request);
122 struct nfs_page *new;
125 len = nfs_page_length(page);
127 return nfs_return_empty_page(page);
128 new = nfs_create_request(ctx, inode, page, 0, len);
133 if (len < PAGE_CACHE_SIZE)
134 memclear_highpage_flush(page, len, PAGE_CACHE_SIZE - len);
136 nfs_list_add_request(new, &one_request);
137 if (NFS_SERVER(inode)->rsize < PAGE_CACHE_SIZE)
138 nfs_pagein_multi(inode, &one_request, len, 0);
140 nfs_pagein_one(inode, &one_request, len, 0);
144 static void nfs_readpage_release(struct nfs_page *req)
146 unlock_page(req->wb_page);
148 dprintk("NFS: read done (%s/%Ld %d@%Ld)\n",
149 req->wb_context->dentry->d_inode->i_sb->s_id,
150 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
152 (long long)req_offset(req));
153 nfs_clear_request(req);
154 nfs_release_request(req);
158 * Set up the NFS read request struct
160 static void nfs_read_rpcsetup(struct nfs_page *req, struct nfs_read_data *data,
161 const struct rpc_call_ops *call_ops,
162 unsigned int count, unsigned int offset)
168 data->inode = inode = req->wb_context->dentry->d_inode;
169 data->cred = req->wb_context->cred;
171 data->args.fh = NFS_FH(inode);
172 data->args.offset = req_offset(req) + offset;
173 data->args.pgbase = req->wb_pgbase + offset;
174 data->args.pages = data->pagevec;
175 data->args.count = count;
176 data->args.context = req->wb_context;
178 data->res.fattr = &data->fattr;
179 data->res.count = count;
181 nfs_fattr_init(&data->fattr);
183 /* Set up the initial task struct. */
184 flags = RPC_TASK_ASYNC | (IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0);
185 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, call_ops, data);
186 NFS_PROTO(inode)->read_setup(data);
188 data->task.tk_cookie = (unsigned long)inode;
190 dprintk("NFS: %5u initiated read call (req %s/%Ld, %u bytes @ offset %Lu)\n",
193 (long long)NFS_FILEID(inode),
195 (unsigned long long)data->args.offset);
199 nfs_async_read_error(struct list_head *head)
201 struct nfs_page *req;
203 while (!list_empty(head)) {
204 req = nfs_list_entry(head->next);
205 nfs_list_remove_request(req);
206 SetPageError(req->wb_page);
207 nfs_readpage_release(req);
212 * Start an async read operation
214 static void nfs_execute_read(struct nfs_read_data *data)
216 struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
219 rpc_clnt_sigmask(clnt, &oldset);
220 rpc_execute(&data->task);
221 rpc_clnt_sigunmask(clnt, &oldset);
225 * Generate multiple requests to fill a single page.
227 * We optimize to reduce the number of read operations on the wire. If we
228 * detect that we're reading a page, or an area of a page, that is past the
229 * end of file, we do not generate NFS read operations but just clear the
230 * parts of the page that would have come back zero from the server anyway.
232 * We rely on the cached value of i_size to make this determination; another
233 * client can fill pages on the server past our cached end-of-file, but we
234 * won't see the new data until our attribute cache is updated. This is more
235 * or less conventional NFS client behavior.
237 static int nfs_pagein_multi(struct inode *inode, struct list_head *head, size_t count, int flags)
239 struct nfs_page *req = nfs_list_entry(head->next);
240 struct page *page = req->wb_page;
241 struct nfs_read_data *data;
242 size_t rsize = NFS_SERVER(inode)->rsize, nbytes;
247 nfs_list_remove_request(req);
251 size_t len = min(nbytes,rsize);
253 data = nfs_readdata_alloc(len);
256 INIT_LIST_HEAD(&data->pages);
257 list_add(&data->pages, &list);
260 } while(nbytes != 0);
261 atomic_set(&req->wb_complete, requests);
263 ClearPageError(page);
267 data = list_entry(list.next, struct nfs_read_data, pages);
268 list_del_init(&data->pages);
270 data->pagevec[0] = page;
274 nfs_read_rpcsetup(req, data, &nfs_read_partial_ops,
278 nfs_execute_read(data);
279 } while (nbytes != 0);
284 while (!list_empty(&list)) {
285 data = list_entry(list.next, struct nfs_read_data, pages);
286 list_del(&data->pages);
287 nfs_readdata_free(data);
290 nfs_readpage_release(req);
294 static int nfs_pagein_one(struct inode *inode, struct list_head *head, size_t count, int flags)
296 struct nfs_page *req;
298 struct nfs_read_data *data;
300 data = nfs_readdata_alloc(count);
304 INIT_LIST_HEAD(&data->pages);
305 pages = data->pagevec;
306 while (!list_empty(head)) {
307 req = nfs_list_entry(head->next);
308 nfs_list_remove_request(req);
309 nfs_list_add_request(req, &data->pages);
310 ClearPageError(req->wb_page);
311 *pages++ = req->wb_page;
313 req = nfs_list_entry(data->pages.next);
315 nfs_read_rpcsetup(req, data, &nfs_read_full_ops, count, 0);
317 nfs_execute_read(data);
320 nfs_async_read_error(head);
325 * This is the callback from RPC telling us whether a reply was
326 * received or some error occurred (timeout or socket shutdown).
328 int nfs_readpage_result(struct rpc_task *task, struct nfs_read_data *data)
332 dprintk("NFS: %s: %5u, (status %d)\n", __FUNCTION__, task->tk_pid,
335 status = NFS_PROTO(data->inode)->read_done(task, data);
339 nfs_add_stats(data->inode, NFSIOS_SERVERREADBYTES, data->res.count);
341 if (task->tk_status == -ESTALE) {
342 set_bit(NFS_INO_STALE, &NFS_FLAGS(data->inode));
343 nfs_mark_for_revalidate(data->inode);
345 spin_lock(&data->inode->i_lock);
346 NFS_I(data->inode)->cache_validity |= NFS_INO_INVALID_ATIME;
347 spin_unlock(&data->inode->i_lock);
351 static int nfs_readpage_retry(struct rpc_task *task, struct nfs_read_data *data)
353 struct nfs_readargs *argp = &data->args;
354 struct nfs_readres *resp = &data->res;
356 if (resp->eof || resp->count == argp->count)
359 /* This is a short read! */
360 nfs_inc_stats(data->inode, NFSIOS_SHORTREAD);
361 /* Has the server at least made some progress? */
362 if (resp->count == 0)
365 /* Yes, so retry the read at the end of the data */
366 argp->offset += resp->count;
367 argp->pgbase += resp->count;
368 argp->count -= resp->count;
369 rpc_restart_call(task);
374 * Handle a read reply that fills part of a page.
376 static void nfs_readpage_result_partial(struct rpc_task *task, void *calldata)
378 struct nfs_read_data *data = calldata;
379 struct nfs_page *req = data->req;
380 struct page *page = req->wb_page;
382 if (nfs_readpage_result(task, data) != 0)
385 if (likely(task->tk_status >= 0)) {
386 nfs_readpage_truncate_uninitialised_page(data);
387 if (nfs_readpage_retry(task, data) != 0)
390 if (unlikely(task->tk_status < 0))
392 if (atomic_dec_and_test(&req->wb_complete)) {
393 if (!PageError(page))
394 SetPageUptodate(page);
395 nfs_readpage_release(req);
399 static const struct rpc_call_ops nfs_read_partial_ops = {
400 .rpc_call_done = nfs_readpage_result_partial,
401 .rpc_release = nfs_readdata_release,
404 static void nfs_readpage_set_pages_uptodate(struct nfs_read_data *data)
406 unsigned int count = data->res.count;
407 unsigned int base = data->args.pgbase;
411 count = data->args.count;
412 if (unlikely(count == 0))
414 pages = &data->args.pages[base >> PAGE_CACHE_SHIFT];
415 base &= ~PAGE_CACHE_MASK;
417 for (;count >= PAGE_CACHE_SIZE; count -= PAGE_CACHE_SIZE, pages++)
418 SetPageUptodate(*pages);
421 /* Was this a short read? */
422 if (data->res.eof || data->res.count == data->args.count)
423 SetPageUptodate(*pages);
427 * This is the callback from RPC telling us whether a reply was
428 * received or some error occurred (timeout or socket shutdown).
430 static void nfs_readpage_result_full(struct rpc_task *task, void *calldata)
432 struct nfs_read_data *data = calldata;
434 if (nfs_readpage_result(task, data) != 0)
437 * Note: nfs_readpage_retry may change the values of
438 * data->args. In the multi-page case, we therefore need
439 * to ensure that we call nfs_readpage_set_pages_uptodate()
442 if (likely(task->tk_status >= 0)) {
443 nfs_readpage_truncate_uninitialised_page(data);
444 nfs_readpage_set_pages_uptodate(data);
445 if (nfs_readpage_retry(task, data) != 0)
448 while (!list_empty(&data->pages)) {
449 struct nfs_page *req = nfs_list_entry(data->pages.next);
451 nfs_list_remove_request(req);
452 nfs_readpage_release(req);
456 static const struct rpc_call_ops nfs_read_full_ops = {
457 .rpc_call_done = nfs_readpage_result_full,
458 .rpc_release = nfs_readdata_release,
462 * Read a page over NFS.
463 * We read the page synchronously in the following case:
464 * - The error flag is set for this page. This happens only when a
465 * previous async read operation failed.
467 int nfs_readpage(struct file *file, struct page *page)
469 struct nfs_open_context *ctx;
470 struct inode *inode = page->mapping->host;
473 dprintk("NFS: nfs_readpage (%p %ld@%lu)\n",
474 page, PAGE_CACHE_SIZE, page->index);
475 nfs_inc_stats(inode, NFSIOS_VFSREADPAGE);
476 nfs_add_stats(inode, NFSIOS_READPAGES, 1);
479 * Try to flush any pending writes to the file..
481 * NOTE! Because we own the page lock, there cannot
482 * be any new pending writes generated at this point
483 * for this page (other pages can be written to).
485 error = nfs_wb_page(inode, page);
490 if (NFS_STALE(inode))
495 ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
499 ctx = get_nfs_open_context((struct nfs_open_context *)
502 error = nfs_readpage_async(ctx, inode, page);
504 put_nfs_open_context(ctx);
512 struct nfs_readdesc {
513 struct nfs_pageio_descriptor *pgio;
514 struct nfs_open_context *ctx;
518 readpage_async_filler(void *data, struct page *page)
520 struct nfs_readdesc *desc = (struct nfs_readdesc *)data;
521 struct inode *inode = page->mapping->host;
522 struct nfs_page *new;
525 nfs_wb_page(inode, page);
526 len = nfs_page_length(page);
528 return nfs_return_empty_page(page);
529 new = nfs_create_request(desc->ctx, inode, page, 0, len);
535 if (len < PAGE_CACHE_SIZE)
536 memclear_highpage_flush(page, len, PAGE_CACHE_SIZE - len);
537 nfs_pageio_add_request(desc->pgio, new);
541 int nfs_readpages(struct file *filp, struct address_space *mapping,
542 struct list_head *pages, unsigned nr_pages)
544 struct nfs_pageio_descriptor pgio;
545 struct nfs_readdesc desc = {
548 struct inode *inode = mapping->host;
549 struct nfs_server *server = NFS_SERVER(inode);
550 size_t rsize = server->rsize;
551 unsigned long npages;
554 dprintk("NFS: nfs_readpages (%s/%Ld %d)\n",
556 (long long)NFS_FILEID(inode),
558 nfs_inc_stats(inode, NFSIOS_VFSREADPAGES);
560 if (NFS_STALE(inode))
564 desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
565 if (desc.ctx == NULL)
568 desc.ctx = get_nfs_open_context((struct nfs_open_context *)
570 if (rsize < PAGE_CACHE_SIZE)
571 nfs_pageio_init(&pgio, inode, nfs_pagein_multi, rsize, 0);
573 nfs_pageio_init(&pgio, inode, nfs_pagein_one, rsize, 0);
575 ret = read_cache_pages(mapping, pages, readpage_async_filler, &desc);
577 nfs_pageio_complete(&pgio);
578 npages = (pgio.pg_bytes_written + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
579 nfs_add_stats(inode, NFSIOS_READPAGES, npages);
580 put_nfs_open_context(desc.ctx);
585 int __init nfs_init_readpagecache(void)
587 nfs_rdata_cachep = kmem_cache_create("nfs_read_data",
588 sizeof(struct nfs_read_data),
589 0, SLAB_HWCACHE_ALIGN,
591 if (nfs_rdata_cachep == NULL)
594 nfs_rdata_mempool = mempool_create_slab_pool(MIN_POOL_READ,
596 if (nfs_rdata_mempool == NULL)
602 void nfs_destroy_readpagecache(void)
604 mempool_destroy(nfs_rdata_mempool);
605 kmem_cache_destroy(nfs_rdata_cachep);