Linux-2.3.7.. Let's be careful out there..
[davej-history.git] / fs / nfs / read.c
blob843f6b23eae8bbab223ab1e851ad9e6df6b8e151
1 /*
2 * linux/fs/nfs/read.c
4 * Block I/O for NFS
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 #define NFS_NEED_XDR_TYPES
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>
24 #include <linux/mm.h>
25 #include <linux/malloc.h>
26 #include <linux/pagemap.h>
27 #include <linux/sunrpc/clnt.h>
28 #include <linux/nfs_fs.h>
29 #include <linux/smp_lock.h>
31 #include <asm/segment.h>
32 #include <asm/system.h>
34 #define NFSDBG_FACILITY NFSDBG_PAGECACHE
36 struct nfs_rreq {
37 struct inode * ra_inode; /* inode from which to read */
38 struct page * ra_page; /* page to be read */
39 struct nfs_readargs ra_args; /* XDR argument struct */
40 struct nfs_readres ra_res; /* ... and result struct */
41 struct nfs_fattr ra_fattr; /* fattr storage */
44 /* Hack for future NFS swap support */
45 #ifndef IS_SWAPFILE
46 # define IS_SWAPFILE(inode) (0)
47 #endif
51 * Set up the NFS read request struct
53 static inline void
54 nfs_readreq_setup(struct nfs_rreq *req, struct nfs_fh *fh,
55 unsigned long offset, void *buffer, unsigned int rsize)
57 req->ra_args.fh = fh;
58 req->ra_args.offset = offset;
59 req->ra_args.count = rsize;
60 req->ra_args.buffer = buffer;
61 req->ra_res.fattr = &req->ra_fattr;
62 req->ra_res.count = rsize;
67 * Read a page synchronously.
69 static int
70 nfs_readpage_sync(struct dentry *dentry, struct inode *inode, struct page *page)
72 struct nfs_rreq rqst;
73 unsigned long offset = page->offset;
74 char *buffer = (char *) page_address(page);
75 int rsize = NFS_SERVER(inode)->rsize;
76 int result, refresh = 0;
77 int count = PAGE_SIZE;
78 int flags = IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0;
80 dprintk("NFS: nfs_readpage_sync(%p)\n", page);
82 do {
83 if (count < rsize)
84 rsize = count;
86 dprintk("NFS: nfs_proc_read(%s, (%s/%s), %ld, %d, %p)\n",
87 NFS_SERVER(inode)->hostname,
88 dentry->d_parent->d_name.name, dentry->d_name.name,
89 offset, rsize, buffer);
91 /* Set up arguments and perform rpc call */
92 nfs_readreq_setup(&rqst, NFS_FH(dentry), offset, buffer, rsize);
93 result = rpc_call(NFS_CLIENT(inode), NFSPROC_READ,
94 &rqst.ra_args, &rqst.ra_res, flags);
97 * Even if we had a partial success we can't mark the page
98 * cache valid.
100 if (result < 0) {
101 if (result == -EISDIR)
102 result = -EINVAL;
103 goto io_error;
105 refresh = 1;
106 count -= result;
107 offset += result;
108 buffer += result;
109 if (result < rsize) /* NFSv2ism */
110 break;
111 } while (count);
113 memset(buffer, 0, count);
114 SetPageUptodate(page);
115 result = 0;
117 io_error:
118 UnlockPage(page);
119 /* Note: we don't refresh if the call returned error */
120 if (refresh && result >= 0)
121 nfs_refresh_inode(inode, &rqst.ra_fattr);
122 return result;
126 * This is the callback from RPC telling us whether a reply was
127 * received or some error occurred (timeout or socket shutdown).
129 static void
130 nfs_readpage_result(struct rpc_task *task)
132 struct nfs_rreq *req = (struct nfs_rreq *) task->tk_calldata;
133 struct page *page = req->ra_page;
134 unsigned long address = page_address(page);
135 int result = task->tk_status;
136 static int succ = 0, fail = 0;
138 dprintk("NFS: %4d received callback for page %lx, result %d\n",
139 task->tk_pid, address, result);
141 if (result >= 0) {
142 result = req->ra_res.count;
143 if (result < PAGE_SIZE) {
144 memset((char *) address + result, 0, PAGE_SIZE - result);
146 nfs_refresh_inode(req->ra_inode, &req->ra_fattr);
147 SetPageUptodate(page);
148 succ++;
149 } else {
150 SetPageError(page);
151 fail++;
152 dprintk("NFS: %d successful reads, %d failures\n", succ, fail);
154 page->owner = (int)current; // HACK, FIXME, will go away.
155 UnlockPage(page);
156 free_page(address);
158 rpc_release_task(task);
159 kfree(req);
162 static inline int
163 nfs_readpage_async(struct dentry *dentry, struct inode *inode,
164 struct page *page)
166 unsigned long address = page_address(page);
167 struct nfs_rreq *req;
168 int result = -1, flags;
170 dprintk("NFS: nfs_readpage_async(%p)\n", page);
171 if (NFS_CONGESTED(inode))
172 goto out_defer;
174 /* N.B. Do we need to test? Never called for swapfile inode */
175 flags = RPC_TASK_ASYNC | (IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0);
176 req = (struct nfs_rreq *) rpc_allocate(flags, sizeof(*req));
177 if (!req)
178 goto out_defer;
180 /* Initialize request */
181 /* N.B. Will the dentry remain valid for life of request? */
182 nfs_readreq_setup(req, NFS_FH(dentry), page->offset,
183 (void *) address, PAGE_SIZE);
184 req->ra_inode = inode;
185 req->ra_page = page; /* count has been incremented by caller */
187 /* Start the async call */
188 dprintk("NFS: executing async READ request.\n");
189 result = rpc_do_call(NFS_CLIENT(inode), NFSPROC_READ,
190 &req->ra_args, &req->ra_res, flags,
191 nfs_readpage_result, req);
192 if (result < 0)
193 goto out_free;
194 result = 0;
195 out:
196 return result;
198 out_defer:
199 dprintk("NFS: deferring async READ request.\n");
200 goto out;
201 out_free:
202 dprintk("NFS: failed to enqueue async READ request.\n");
203 kfree(req);
204 goto out;
208 * Read a page over NFS.
209 * We read the page synchronously in the following cases:
210 * - The file is a swap file. Swap-ins are always sync operations,
211 * so there's no need bothering to make async reads 100% fail-safe.
212 * - The NFS rsize is smaller than PAGE_SIZE. We could kludge our way
213 * around this by creating several consecutive read requests, but
214 * that's hardly worth it.
215 * - The error flag is set for this page. This happens only when a
216 * previous async read operation failed.
217 * - The server is congested.
220 nfs_readpage(struct file *file, struct page *page)
222 struct dentry *dentry = file->f_dentry;
223 struct inode *inode = dentry->d_inode;
224 int error;
226 lock_kernel();
227 dprintk("NFS: nfs_readpage (%p %ld@%ld)\n",
228 page, PAGE_SIZE, page->offset);
229 get_page(page);
232 * Try to flush any pending writes to the file..
234 * NOTE! Because we own the page lock, there cannot
235 * be any new pending writes generated at this point
236 * for this page (other pages can be written to).
238 error = nfs_wb_page(inode, page);
239 if (error)
240 goto out_error;
242 error = -1;
243 if (!IS_SWAPFILE(inode) && !PageError(page) &&
244 NFS_SERVER(inode)->rsize >= PAGE_SIZE)
245 error = nfs_readpage_async(dentry, inode, page);
246 if (error >= 0)
247 goto out;
249 error = nfs_readpage_sync(dentry, inode, page);
250 if (error < 0 && IS_SWAPFILE(inode))
251 printk("Aiee.. nfs swap-in of page failed!\n");
252 goto out_free;
254 out_error:
255 UnlockPage(page);
256 out_free:
257 free_page(page_address(page));
258 out:
259 unlock_kernel();
260 return error;