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/mempool.h>
28 #include <linux/sunrpc/clnt.h>
29 #include <linux/nfs_fs.h>
30 #include <linux/nfs_page.h>
31 #include <linux/smp_lock.h>
33 #include <asm/system.h>
35 #define NFSDBG_FACILITY NFSDBG_PAGECACHE
37 static int nfs_pagein_one(struct list_head *, struct inode *);
38 static void nfs_readpage_result_partial(struct nfs_read_data *, int);
39 static void nfs_readpage_result_full(struct nfs_read_data *, int);
41 static kmem_cache_t *nfs_rdata_cachep;
42 static mempool_t *nfs_rdata_mempool;
44 #define MIN_POOL_READ (32)
46 static __inline__ struct nfs_read_data *nfs_readdata_alloc(void)
48 struct nfs_read_data *p;
49 p = (struct nfs_read_data *)mempool_alloc(nfs_rdata_mempool, SLAB_NOFS);
51 memset(p, 0, sizeof(*p));
52 INIT_LIST_HEAD(&p->pages);
57 static __inline__ void nfs_readdata_free(struct nfs_read_data *p)
59 mempool_free(p, nfs_rdata_mempool);
62 static void nfs_readdata_release(struct rpc_task *task)
64 struct nfs_read_data *data = (struct nfs_read_data *)task->tk_calldata;
65 nfs_readdata_free(data);
69 unsigned int nfs_page_length(struct inode *inode, struct page *page)
71 loff_t i_size = i_size_read(inode);
76 idx = (i_size - 1) >> PAGE_CACHE_SHIFT;
77 if (page->index > idx)
79 if (page->index != idx)
80 return PAGE_CACHE_SIZE;
81 return 1 + ((i_size - 1) & (PAGE_CACHE_SIZE - 1));
85 int nfs_return_empty_page(struct page *page)
87 memclear_highpage_flush(page, 0, PAGE_CACHE_SIZE);
88 SetPageUptodate(page);
94 * Read a page synchronously.
97 nfs_readpage_sync(struct file *file, struct inode *inode, struct page *page)
99 unsigned int rsize = NFS_SERVER(inode)->rsize;
100 unsigned int count = PAGE_CACHE_SIZE;
102 struct nfs_read_data rdata = {
103 .flags = (IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0),
108 .lockowner = current->files,
114 .fattr = &rdata.fattr,
118 dprintk("NFS: nfs_readpage_sync(%p)\n", page);
121 * This works now because the socket layer never tries to DMA
122 * into this buffer directly.
126 rdata.args.count = count;
127 rdata.res.count = rdata.args.count;
128 rdata.args.offset = page_offset(page) + rdata.args.pgbase;
130 dprintk("NFS: nfs_proc_read(%s, (%s/%Ld), %Lu, %u)\n",
131 NFS_SERVER(inode)->hostname,
133 (long long)NFS_FILEID(inode),
134 (unsigned long long)rdata.args.pgbase,
138 result = NFS_PROTO(inode)->read(&rdata, file);
142 * Even if we had a partial success we can't mark the page
146 if (result == -EISDIR)
151 rdata.args.pgbase += result;
152 /* Note: result == 0 should only happen if we're caching
153 * a write that extends the file and punches a hole.
155 if (rdata.res.eof != 0 || result == 0)
158 NFS_FLAGS(inode) |= NFS_INO_INVALID_ATIME;
161 memclear_highpage_flush(page, rdata.args.pgbase, count);
162 SetPageUptodate(page);
164 ClearPageError(page);
173 nfs_readpage_async(struct file *file, struct inode *inode, struct page *page)
175 LIST_HEAD(one_request);
176 struct nfs_page *new;
179 len = nfs_page_length(inode, page);
181 return nfs_return_empty_page(page);
182 new = nfs_create_request(file, inode, page, 0, len);
187 if (len < PAGE_CACHE_SIZE)
188 memclear_highpage_flush(page, len, PAGE_CACHE_SIZE - len);
190 nfs_lock_request(new);
191 nfs_list_add_request(new, &one_request);
192 nfs_pagein_one(&one_request, inode);
196 static void nfs_readpage_release(struct nfs_page *req)
198 unlock_page(req->wb_page);
200 nfs_clear_request(req);
201 nfs_release_request(req);
202 nfs_unlock_request(req);
204 dprintk("NFS: read done (%s/%Ld %d@%Ld)\n",
205 req->wb_inode->i_sb->s_id,
206 (long long)NFS_FILEID(req->wb_inode),
208 (long long)req_offset(req));
212 * Set up the NFS read request struct
214 static void nfs_read_rpcsetup(struct nfs_page *req, struct nfs_read_data *data,
215 unsigned int count, unsigned int offset)
220 data->inode = inode = req->wb_inode;
221 data->cred = req->wb_cred;
223 data->args.fh = NFS_FH(inode);
224 data->args.offset = req_offset(req) + offset;
225 data->args.pgbase = req->wb_pgbase + offset;
226 data->args.pages = data->pagevec;
227 data->args.count = count;
228 data->args.lockowner = req->wb_lockowner;
229 data->args.state = req->wb_state;
231 data->res.fattr = &data->fattr;
232 data->res.count = count;
235 NFS_PROTO(inode)->read_setup(data);
237 data->task.tk_cookie = (unsigned long)inode;
238 data->task.tk_calldata = data;
239 /* Release requests */
240 data->task.tk_release = nfs_readdata_release;
242 dprintk("NFS: %4d initiated read call (req %s/%Ld, %u bytes @ offset %Lu)\n",
245 (long long)NFS_FILEID(inode),
247 (unsigned long long)data->args.offset);
251 nfs_async_read_error(struct list_head *head)
253 struct nfs_page *req;
255 while (!list_empty(head)) {
256 req = nfs_list_entry(head->next);
257 nfs_list_remove_request(req);
258 SetPageError(req->wb_page);
259 nfs_readpage_release(req);
264 * Start an async read operation
266 static void nfs_execute_read(struct nfs_read_data *data)
268 struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
271 rpc_clnt_sigmask(clnt, &oldset);
273 rpc_execute(&data->task);
275 rpc_clnt_sigunmask(clnt, &oldset);
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 list_head *head, struct inode *inode)
293 struct nfs_page *req = nfs_list_entry(head->next);
294 struct page *page = req->wb_page;
295 struct nfs_read_data *data;
296 unsigned int rsize = NFS_SERVER(inode)->rsize;
297 unsigned int nbytes, offset;
301 nfs_list_remove_request(req);
303 nbytes = req->wb_bytes;
305 data = nfs_readdata_alloc();
308 list_add(&data->pages, &list);
314 atomic_set(&req->wb_complete, requests);
316 ClearPageError(page);
318 nbytes = req->wb_bytes;
320 data = list_entry(list.next, struct nfs_read_data, pages);
321 list_del_init(&data->pages);
323 data->pagevec[0] = page;
324 data->complete = nfs_readpage_result_partial;
326 if (nbytes > rsize) {
327 nfs_read_rpcsetup(req, data, rsize, offset);
331 nfs_read_rpcsetup(req, data, nbytes, offset);
334 nfs_execute_read(data);
335 } while (nbytes != 0);
340 while (!list_empty(&list)) {
341 data = list_entry(list.next, struct nfs_read_data, pages);
342 list_del(&data->pages);
343 nfs_readdata_free(data);
346 nfs_readpage_release(req);
350 static int nfs_pagein_one(struct list_head *head, struct inode *inode)
352 struct nfs_page *req;
354 struct nfs_read_data *data;
357 if (NFS_SERVER(inode)->rsize < PAGE_CACHE_SIZE)
358 return nfs_pagein_multi(head, inode);
360 data = nfs_readdata_alloc();
364 pages = data->pagevec;
366 while (!list_empty(head)) {
367 req = nfs_list_entry(head->next);
368 nfs_list_remove_request(req);
369 nfs_list_add_request(req, &data->pages);
370 ClearPageError(req->wb_page);
371 *pages++ = req->wb_page;
372 count += req->wb_bytes;
374 req = nfs_list_entry(data->pages.next);
376 data->complete = nfs_readpage_result_full;
377 nfs_read_rpcsetup(req, data, count, 0);
379 nfs_execute_read(data);
382 nfs_async_read_error(head);
387 nfs_pagein_list(struct list_head *head, int rpages)
389 LIST_HEAD(one_request);
390 struct nfs_page *req;
392 unsigned int pages = 0;
394 while (!list_empty(head)) {
395 pages += nfs_coalesce_requests(head, &one_request, rpages);
396 req = nfs_list_entry(one_request.next);
397 error = nfs_pagein_one(&one_request, req->wb_inode);
404 nfs_async_read_error(head);
409 * Handle a read reply that fills part of a page.
411 static void nfs_readpage_result_partial(struct nfs_read_data *data, int status)
413 struct nfs_page *req = data->req;
414 struct page *page = req->wb_page;
417 unsigned int request = data->args.count;
418 unsigned int result = data->res.count;
420 if (result < request) {
421 memclear_highpage_flush(page,
422 data->args.pgbase + result,
428 if (atomic_dec_and_test(&req->wb_complete)) {
429 if (!PageError(page))
430 SetPageUptodate(page);
431 nfs_readpage_release(req);
436 * This is the callback from RPC telling us whether a reply was
437 * received or some error occurred (timeout or socket shutdown).
439 static void nfs_readpage_result_full(struct nfs_read_data *data, int status)
441 unsigned int count = data->res.count;
443 while (!list_empty(&data->pages)) {
444 struct nfs_page *req = nfs_list_entry(data->pages.next);
445 struct page *page = req->wb_page;
446 nfs_list_remove_request(req);
449 if (count < PAGE_CACHE_SIZE) {
450 if (count < req->wb_bytes)
451 memclear_highpage_flush(page,
452 req->wb_pgbase + count,
453 req->wb_bytes - count);
456 count -= PAGE_CACHE_SIZE;
457 SetPageUptodate(page);
460 nfs_readpage_release(req);
465 * This is the callback from RPC telling us whether a reply was
466 * received or some error occurred (timeout or socket shutdown).
468 void nfs_readpage_result(struct rpc_task *task)
470 struct nfs_read_data *data = (struct nfs_read_data *)task->tk_calldata;
471 struct nfs_readargs *argp = &data->args;
472 struct nfs_readres *resp = &data->res;
473 int status = task->tk_status;
475 dprintk("NFS: %4d nfs_readpage_result, (status %d)\n",
476 task->tk_pid, status);
478 /* Is this a short read? */
479 if (task->tk_status >= 0 && resp->count < argp->count && !resp->eof) {
480 /* Has the server at least made some progress? */
481 if (resp->count != 0) {
482 /* Yes, so retry the read at the end of the data */
483 argp->offset += resp->count;
484 argp->pgbase += resp->count;
485 argp->count -= resp->count;
486 rpc_restart_call(task);
489 task->tk_status = -EIO;
491 NFS_FLAGS(data->inode) |= NFS_INO_INVALID_ATIME;
492 data->complete(data, status);
496 * Read a page over NFS.
497 * We read the page synchronously in the following case:
498 * - The error flag is set for this page. This happens only when a
499 * previous async read operation failed.
502 nfs_readpage(struct file *file, struct page *page)
504 struct inode *inode = page->mapping->host;
507 dprintk("NFS: nfs_readpage (%p %ld@%lu)\n",
508 page, PAGE_CACHE_SIZE, page->index);
510 * Try to flush any pending writes to the file..
512 * NOTE! Because we own the page lock, there cannot
513 * be any new pending writes generated at this point
514 * for this page (other pages can be written to).
516 error = nfs_wb_page(inode, page);
520 if (!IS_SYNC(inode)) {
521 error = nfs_readpage_async(file, inode, page);
525 error = nfs_readpage_sync(file, inode, page);
526 if (error < 0 && IS_SWAPFILE(inode))
527 printk("Aiee.. nfs swap-in of page failed!\n");
536 struct nfs_readdesc {
537 struct list_head *head;
542 readpage_async_filler(void *data, struct page *page)
544 struct nfs_readdesc *desc = (struct nfs_readdesc *)data;
545 struct inode *inode = page->mapping->host;
546 struct nfs_page *new;
549 nfs_wb_page(inode, page);
550 len = nfs_page_length(inode, page);
552 return nfs_return_empty_page(page);
553 new = nfs_create_request(desc->filp, inode, page, 0, len);
559 if (len < PAGE_CACHE_SIZE)
560 memclear_highpage_flush(page, len, PAGE_CACHE_SIZE - len);
561 nfs_lock_request(new);
562 nfs_list_add_request(new, desc->head);
567 nfs_readpages(struct file *filp, struct address_space *mapping,
568 struct list_head *pages, unsigned nr_pages)
571 struct nfs_readdesc desc = {
575 struct inode *inode = mapping->host;
576 struct nfs_server *server = NFS_SERVER(inode);
579 dprintk("NFS: nfs_readpages (%s/%Ld %d)\n",
581 (long long)NFS_FILEID(inode),
584 ret = read_cache_pages(mapping, pages, readpage_async_filler, &desc);
585 if (!list_empty(&head)) {
586 int err = nfs_pagein_list(&head, server->rpages);
593 int nfs_init_readpagecache(void)
595 nfs_rdata_cachep = kmem_cache_create("nfs_read_data",
596 sizeof(struct nfs_read_data),
597 0, SLAB_HWCACHE_ALIGN,
599 if (nfs_rdata_cachep == NULL)
602 nfs_rdata_mempool = mempool_create(MIN_POOL_READ,
606 if (nfs_rdata_mempool == NULL)
612 void nfs_destroy_readpagecache(void)
614 mempool_destroy(nfs_rdata_mempool);
615 if (kmem_cache_destroy(nfs_rdata_cachep))
616 printk(KERN_INFO "nfs_read_data: not all structures were freed\n");