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/sched.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/nfs_flushd.h>
31 #include <linux/smp_lock.h>
33 #include <asm/system.h>
35 #define NFSDBG_FACILITY NFSDBG_PAGECACHE
37 struct nfs_read_data {
40 struct rpc_cred *cred;
41 struct nfs_readargs args; /* XDR argument struct */
42 struct nfs_readres res; /* ... and result struct */
43 struct nfs_fattr fattr; /* fattr storage */
44 struct list_head pages; /* Coalesced read requests */
45 struct page *pagevec[NFS_READ_MAXIOV];
49 * Local function declarations
51 static void nfs_readpage_result(struct rpc_task *task);
53 /* Hack for future NFS swap support */
55 # define IS_SWAPFILE(inode) (0)
58 static kmem_cache_t *nfs_rdata_cachep;
60 static __inline__ struct nfs_read_data *nfs_readdata_alloc(void)
62 struct nfs_read_data *p;
63 p = kmem_cache_alloc(nfs_rdata_cachep, SLAB_NOFS);
65 memset(p, 0, sizeof(*p));
66 INIT_LIST_HEAD(&p->pages);
67 p->args.pages = p->pagevec;
72 static __inline__ void nfs_readdata_free(struct nfs_read_data *p)
74 kmem_cache_free(nfs_rdata_cachep, p);
77 static void nfs_readdata_release(struct rpc_task *task)
79 struct nfs_read_data *data = (struct nfs_read_data *)task->tk_calldata;
80 nfs_readdata_free(data);
84 * Read a page synchronously.
87 nfs_readpage_sync(struct file *file, struct inode *inode, struct page *page)
89 struct rpc_cred *cred = NULL;
90 struct nfs_fattr fattr;
91 unsigned int offset = 0;
92 int rsize = NFS_SERVER(inode)->rsize;
94 int count = PAGE_CACHE_SIZE;
95 int flags = IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0;
98 dprintk("NFS: nfs_readpage_sync(%p)\n", page);
101 cred = nfs_file_cred(file);
104 * This works now because the socket layer never tries to DMA
105 * into this buffer directly.
111 dprintk("NFS: nfs_proc_read(%s, (%x/%Ld), %u, %u, %p)\n",
112 NFS_SERVER(inode)->hostname,
113 inode->i_dev, (long long)NFS_FILEID(inode),
114 offset, rsize, page);
117 result = NFS_PROTO(inode)->read(inode, cred, &fattr, flags,
118 offset, rsize, page, &eof);
119 nfs_refresh_inode(inode, &fattr);
123 * Even if we had a partial success we can't mark the page
127 if (result == -EISDIR)
133 if (result < rsize) /* NFSv2ism */
138 char *kaddr = kmap(page);
139 memset(kaddr + offset, 0, count);
142 flush_dcache_page(page);
143 SetPageUptodate(page);
145 ClearPageError(page);
154 * Add a request to the inode's asynchronous read list.
157 nfs_mark_request_read(struct nfs_page *req)
159 struct inode *inode = req->wb_inode;
161 spin_lock(&nfs_wreq_lock);
162 nfs_list_add_request(req, &inode->u.nfs_i.read);
163 inode->u.nfs_i.nread++;
164 __nfs_add_lru(&NFS_SERVER(inode)->lru_read, req);
165 spin_unlock(&nfs_wreq_lock);
169 nfs_readpage_async(struct file *file, struct inode *inode, struct page *page)
171 struct nfs_page *new;
173 new = nfs_create_request(nfs_file_cred(file), inode, page, 0, PAGE_CACHE_SIZE);
176 NFS_ClearPageSync(page);
180 nfs_mark_request_read(new);
182 if (NFS_TestClearPageSync(page) ||
183 inode->u.nfs_i.nread >= NFS_SERVER(inode)->rpages ||
184 page_index(page) == (inode->i_size + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT)
185 nfs_pagein_inode(inode, 0, 0);
190 * Set up the NFS read request struct
193 nfs_read_rpcsetup(struct list_head *head, struct nfs_read_data *data)
195 struct nfs_page *req;
199 pages = data->args.pages;
201 while (!list_empty(head)) {
202 struct nfs_page *req = nfs_list_entry(head->next);
203 nfs_list_remove_request(req);
204 nfs_list_add_request(req, &data->pages);
205 *pages++ = req->wb_page;
206 count += req->wb_bytes;
208 req = nfs_list_entry(data->pages.next);
209 data->inode = req->wb_inode;
210 data->cred = req->wb_cred;
211 data->args.fh = NFS_FH(req->wb_inode);
212 data->args.offset = page_offset(req->wb_page) + req->wb_offset;
213 data->args.pgbase = req->wb_offset;
214 data->args.count = count;
215 data->res.fattr = &data->fattr;
216 data->res.count = count;
221 nfs_async_read_error(struct list_head *head)
223 struct nfs_page *req;
226 while (!list_empty(head)) {
227 req = nfs_list_entry(head->next);
229 nfs_list_remove_request(req);
230 NFS_ClearPageSync(page);
233 nfs_clear_request(req);
234 nfs_release_request(req);
235 nfs_unlock_request(req);
240 nfs_pagein_one(struct list_head *head, struct inode *inode)
242 struct rpc_task *task;
243 struct rpc_clnt *clnt = NFS_CLIENT(inode);
244 struct nfs_read_data *data;
245 struct rpc_message msg;
249 data = nfs_readdata_alloc();
254 /* N.B. Do we need to test? Never called for swapfile inode */
255 flags = RPC_TASK_ASYNC | (IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0);
257 nfs_read_rpcsetup(head, data);
259 /* Finalize the task. */
260 rpc_init_task(task, clnt, nfs_readpage_result, flags);
261 task->tk_calldata = data;
262 /* Release requests */
263 task->tk_release = nfs_readdata_release;
266 msg.rpc_proc = (NFS_PROTO(inode)->version == 3) ? NFS3PROC_READ : NFSPROC_READ;
268 msg.rpc_proc = NFSPROC_READ;
270 msg.rpc_argp = &data->args;
271 msg.rpc_resp = &data->res;
272 msg.rpc_cred = data->cred;
274 /* Start the async call */
275 dprintk("NFS: %4d initiated read call (req %x/%Ld count %u.\n",
277 inode->i_dev, (long long)NFS_FILEID(inode),
280 rpc_clnt_sigmask(clnt, &oldset);
281 rpc_call_setup(task, &msg, 0);
285 rpc_clnt_sigunmask(clnt, &oldset);
288 nfs_async_read_error(head);
293 nfs_pagein_list(struct list_head *head, int rpages)
295 LIST_HEAD(one_request);
296 struct nfs_page *req;
298 unsigned int pages = 0;
300 while (!list_empty(head)) {
301 pages += nfs_coalesce_requests(head, &one_request, rpages);
302 req = nfs_list_entry(one_request.next);
303 error = nfs_pagein_one(&one_request, req->wb_inode);
310 nfs_async_read_error(head);
315 * nfs_scan_lru_read_timeout - Scan LRU list for timed out read requests
316 * @server: NFS superblock data
317 * @dst: destination list
319 * Moves a maximum of 'rpages' timed out requests from the NFS read LRU list.
320 * The elements are checked to ensure that they form a contiguous set
321 * of pages, and that they originated from the same file.
324 nfs_scan_lru_read_timeout(struct nfs_server *server, struct list_head *dst)
329 npages = nfs_scan_lru_timeout(&server->lru_read, dst, server->rpages);
331 inode = nfs_list_entry(dst->next)->wb_inode;
332 inode->u.nfs_i.nread -= npages;
338 * nfs_scan_lru_read - Scan LRU list for read requests
339 * @server: NFS superblock data
340 * @dst: destination list
342 * Moves a maximum of 'rpages' requests from the NFS read LRU list.
343 * The elements are checked to ensure that they form a contiguous set
344 * of pages, and that they originated from the same file.
347 nfs_scan_lru_read(struct nfs_server *server, struct list_head *dst)
352 npages = nfs_scan_lru(&server->lru_read, dst, server->rpages);
354 inode = nfs_list_entry(dst->next)->wb_inode;
355 inode->u.nfs_i.nread -= npages;
361 * nfs_scan_read - Scan an inode for read requests
362 * @inode: NFS inode to scan
363 * @dst: destination list
364 * @idx_start: lower bound of page->index to scan
365 * @npages: idx_start + npages sets the upper bound to scan
367 * Moves requests from the inode's read list.
368 * The requests are *not* checked to ensure that they form a contiguous set.
371 nfs_scan_read(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
374 res = nfs_scan_list(&inode->u.nfs_i.read, dst, NULL, idx_start, npages);
375 inode->u.nfs_i.nread -= res;
376 if ((inode->u.nfs_i.nread == 0) != list_empty(&inode->u.nfs_i.read))
377 printk(KERN_ERR "NFS: desynchronized value of nfs_i.nread.\n");
381 int nfs_pagein_inode(struct inode *inode, unsigned long idx_start,
388 spin_lock(&nfs_wreq_lock);
389 res = nfs_scan_read(inode, &head, idx_start, npages);
390 spin_unlock(&nfs_wreq_lock);
392 error = nfs_pagein_list(&head, NFS_SERVER(inode)->rpages);
399 * This is the callback from RPC telling us whether a reply was
400 * received or some error occurred (timeout or socket shutdown).
403 nfs_readpage_result(struct rpc_task *task)
405 struct nfs_read_data *data = (struct nfs_read_data *) task->tk_calldata;
406 struct inode *inode = data->inode;
407 unsigned int count = data->res.count;
409 dprintk("NFS: %4d nfs_readpage_result, (status %d)\n",
410 task->tk_pid, task->tk_status);
412 if (nfs_async_handle_jukebox(task))
415 nfs_refresh_inode(inode, &data->fattr);
416 while (!list_empty(&data->pages)) {
417 struct nfs_page *req = nfs_list_entry(data->pages.next);
418 struct page *page = req->wb_page;
419 nfs_list_remove_request(req);
421 if (task->tk_status >= 0) {
422 if (count < PAGE_CACHE_SIZE) {
423 char *p = kmap(page);
424 memset(p + count, 0, PAGE_CACHE_SIZE - count);
428 SetPageUptodate(page);
432 count -= PAGE_CACHE_SIZE;
433 SetPageUptodate(page);
437 flush_dcache_page(page);
438 NFS_ClearPageSync(page);
441 dprintk("NFS: read (%x/%Ld %d@%Ld)\n",
442 req->wb_inode->i_dev,
443 (long long)NFS_FILEID(req->wb_inode),
445 (long long)(page_offset(page) + req->wb_offset));
446 nfs_clear_request(req);
447 nfs_release_request(req);
448 nfs_unlock_request(req);
453 * Read a page over NFS.
454 * We read the page synchronously in the following cases:
455 * - The NFS rsize is smaller than PAGE_CACHE_SIZE. We could kludge our way
456 * around this by creating several consecutive read requests, but
457 * that's hardly worth it.
458 * - The error flag is set for this page. This happens only when a
459 * previous async read operation failed.
462 nfs_readpage(struct file *file, struct page *page)
464 struct inode *inode = page->mapping->host;
467 dprintk("NFS: nfs_readpage (%p %ld@%lu)\n",
468 page, PAGE_CACHE_SIZE, page->index);
470 * Try to flush any pending writes to the file..
472 * NOTE! Because we own the page lock, there cannot
473 * be any new pending writes generated at this point
474 * for this page (other pages can be written to).
476 error = nfs_wb_page(inode, page);
480 if (!PageError(page) && NFS_SERVER(inode)->rsize >= PAGE_CACHE_SIZE) {
481 error = nfs_readpage_async(file, inode, page);
485 error = nfs_readpage_sync(file, inode, page);
486 if (error < 0 && IS_SWAPFILE(inode))
487 printk("Aiee.. nfs swap-in of page failed!\n");
492 NFS_ClearPageSync(page);
497 int nfs_init_readpagecache(void)
499 nfs_rdata_cachep = kmem_cache_create("nfs_read_data",
500 sizeof(struct nfs_read_data),
501 0, SLAB_HWCACHE_ALIGN,
503 if (nfs_rdata_cachep == NULL)
509 void nfs_destroy_readpagecache(void)
511 if (kmem_cache_destroy(nfs_rdata_cachep))
512 printk(KERN_INFO "nfs_read_data: not all structures were freed\n");