[PATCH] DVB: Update documentation and credits
[linux-2.6/history.git] / fs / nfs / read.c
blobeaa650d24d91be9d5eba6288ad2b680f32b06498
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 #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>
24 #include <linux/mm.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 *);
39 static kmem_cache_t *nfs_rdata_cachep;
40 static mempool_t *nfs_rdata_mempool;
42 #define MIN_POOL_READ (32)
44 static __inline__ struct nfs_read_data *nfs_readdata_alloc(void)
46 struct nfs_read_data *p;
47 p = (struct nfs_read_data *)mempool_alloc(nfs_rdata_mempool, SLAB_NOFS);
48 if (p) {
49 memset(p, 0, sizeof(*p));
50 INIT_LIST_HEAD(&p->pages);
52 return p;
55 static __inline__ void nfs_readdata_free(struct nfs_read_data *p)
57 mempool_free(p, nfs_rdata_mempool);
60 void nfs_readdata_release(struct rpc_task *task)
62 struct nfs_read_data *data = (struct nfs_read_data *)task->tk_calldata;
63 nfs_readdata_free(data);
67 * Read a page synchronously.
69 static int
70 nfs_readpage_sync(struct file *file, struct inode *inode, struct page *page)
72 unsigned int rsize = NFS_SERVER(inode)->rsize;
73 unsigned int count = PAGE_CACHE_SIZE;
74 int result;
75 struct nfs_read_data rdata = {
76 .flags = (IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0),
77 .cred = NULL,
78 .inode = inode,
79 .args = {
80 .fh = NFS_FH(inode),
81 .pages = &page,
82 .pgbase = 0UL,
83 .count = rsize,
85 .res = {
86 .fattr = &rdata.fattr,
90 dprintk("NFS: nfs_readpage_sync(%p)\n", page);
93 * This works now because the socket layer never tries to DMA
94 * into this buffer directly.
96 do {
97 if (count < rsize)
98 rdata.args.count = count;
99 rdata.res.count = rdata.args.count;
100 rdata.args.offset = page_offset(page) + rdata.args.pgbase;
102 dprintk("NFS: nfs_proc_read(%s, (%s/%Ld), %Lu, %u)\n",
103 NFS_SERVER(inode)->hostname,
104 inode->i_sb->s_id,
105 (long long)NFS_FILEID(inode),
106 (unsigned long long)rdata.args.pgbase,
107 rdata.args.count);
109 lock_kernel();
110 result = NFS_PROTO(inode)->read(&rdata, file);
111 unlock_kernel();
114 * Even if we had a partial success we can't mark the page
115 * cache valid.
117 if (result < 0) {
118 if (result == -EISDIR)
119 result = -EINVAL;
120 goto io_error;
122 count -= result;
123 rdata.args.pgbase += result;
124 if (result < rdata.args.count) /* NFSv2ism */
125 break;
126 } while (count);
128 if (count)
129 memclear_highpage_flush(page, rdata.args.pgbase, count);
130 SetPageUptodate(page);
131 if (PageError(page))
132 ClearPageError(page);
133 result = 0;
135 io_error:
136 unlock_page(page);
137 return result;
140 static int
141 nfs_readpage_async(struct file *file, struct inode *inode, struct page *page)
143 LIST_HEAD(one_request);
144 struct nfs_page *new;
146 new = nfs_create_request(file, inode, page, 0, PAGE_CACHE_SIZE);
147 if (IS_ERR(new)) {
148 unlock_page(page);
149 return PTR_ERR(new);
151 nfs_lock_request(new);
152 nfs_list_add_request(new, &one_request);
153 nfs_pagein_one(&one_request, inode);
154 return 0;
158 * Set up the NFS read request struct
160 static void
161 nfs_read_rpcsetup(struct list_head *head, struct nfs_read_data *data)
163 struct inode *inode;
164 struct nfs_page *req;
165 struct page **pages;
166 unsigned int count;
168 pages = data->pagevec;
169 count = 0;
170 while (!list_empty(head)) {
171 req = nfs_list_entry(head->next);
172 nfs_list_remove_request(req);
173 nfs_list_add_request(req, &data->pages);
174 *pages++ = req->wb_page;
175 count += req->wb_bytes;
177 req = nfs_list_entry(data->pages.next);
178 data->inode = inode = req->wb_inode;
179 data->cred = req->wb_cred;
181 NFS_PROTO(inode)->read_setup(data, count);
183 dprintk("NFS: %4d initiated read call (req %s/%Ld, %u bytes @ offset %Lu.\n",
184 data->task.tk_pid,
185 inode->i_sb->s_id,
186 (long long)NFS_FILEID(inode),
187 count,
188 (unsigned long long)req_offset(req));
191 static void
192 nfs_async_read_error(struct list_head *head)
194 struct nfs_page *req;
195 struct page *page;
197 while (!list_empty(head)) {
198 req = nfs_list_entry(head->next);
199 page = req->wb_page;
200 nfs_list_remove_request(req);
201 SetPageError(page);
202 unlock_page(page);
203 nfs_clear_request(req);
204 nfs_release_request(req);
205 nfs_unlock_request(req);
209 static int
210 nfs_pagein_one(struct list_head *head, struct inode *inode)
212 struct rpc_clnt *clnt = NFS_CLIENT(inode);
213 struct nfs_read_data *data;
214 sigset_t oldset;
216 data = nfs_readdata_alloc();
217 if (!data)
218 goto out_bad;
220 nfs_read_rpcsetup(head, data);
222 /* Start the async call */
223 rpc_clnt_sigmask(clnt, &oldset);
224 lock_kernel();
225 rpc_execute(&data->task);
226 unlock_kernel();
227 rpc_clnt_sigunmask(clnt, &oldset);
228 return 0;
229 out_bad:
230 nfs_async_read_error(head);
231 return -ENOMEM;
235 nfs_pagein_list(struct list_head *head, int rpages)
237 LIST_HEAD(one_request);
238 struct nfs_page *req;
239 int error = 0;
240 unsigned int pages = 0;
242 while (!list_empty(head)) {
243 pages += nfs_coalesce_requests(head, &one_request, rpages);
244 req = nfs_list_entry(one_request.next);
245 error = nfs_pagein_one(&one_request, req->wb_inode);
246 if (error < 0)
247 break;
249 if (error >= 0)
250 return pages;
252 nfs_async_read_error(head);
253 return error;
257 * This is the callback from RPC telling us whether a reply was
258 * received or some error occurred (timeout or socket shutdown).
260 void
261 nfs_readpage_result(struct rpc_task *task)
263 struct nfs_read_data *data = (struct nfs_read_data *)task->tk_calldata;
264 unsigned int count = data->res.count;
266 dprintk("NFS: %4d nfs_readpage_result, (status %d)\n",
267 task->tk_pid, task->tk_status);
269 while (!list_empty(&data->pages)) {
270 struct nfs_page *req = nfs_list_entry(data->pages.next);
271 struct page *page = req->wb_page;
272 nfs_list_remove_request(req);
274 if (task->tk_status >= 0) {
275 if (count < PAGE_CACHE_SIZE) {
276 memclear_highpage_flush(page,
277 req->wb_pgbase + count,
278 req->wb_bytes - count);
280 count = 0;
281 } else
282 count -= PAGE_CACHE_SIZE;
283 SetPageUptodate(page);
284 } else
285 SetPageError(page);
286 unlock_page(page);
288 dprintk("NFS: read (%s/%Ld %d@%Ld)\n",
289 req->wb_inode->i_sb->s_id,
290 (long long)NFS_FILEID(req->wb_inode),
291 req->wb_bytes,
292 (long long)req_offset(req));
293 nfs_clear_request(req);
294 nfs_release_request(req);
295 nfs_unlock_request(req);
300 * Read a page over NFS.
301 * We read the page synchronously in the following cases:
302 * - The NFS rsize is smaller than PAGE_CACHE_SIZE. We could kludge our way
303 * around this by creating several consecutive read requests, but
304 * that's hardly worth it.
305 * - The error flag is set for this page. This happens only when a
306 * previous async read operation failed.
309 nfs_readpage(struct file *file, struct page *page)
311 struct inode *inode = page->mapping->host;
312 int error;
314 dprintk("NFS: nfs_readpage (%p %ld@%lu)\n",
315 page, PAGE_CACHE_SIZE, page->index);
317 * Try to flush any pending writes to the file..
319 * NOTE! Because we own the page lock, there cannot
320 * be any new pending writes generated at this point
321 * for this page (other pages can be written to).
323 error = nfs_wb_page(inode, page);
324 if (error)
325 goto out_error;
327 if (!PageError(page) && NFS_SERVER(inode)->rsize >= PAGE_CACHE_SIZE) {
328 error = nfs_readpage_async(file, inode, page);
329 goto out;
332 error = nfs_readpage_sync(file, inode, page);
333 if (error < 0 && IS_SWAPFILE(inode))
334 printk("Aiee.. nfs swap-in of page failed!\n");
335 out:
336 return error;
338 out_error:
339 unlock_page(page);
340 goto out;
343 struct nfs_readdesc {
344 struct list_head *head;
345 struct file *filp;
348 static int
349 readpage_sync_filler(void *data, struct page *page)
351 struct nfs_readdesc *desc = (struct nfs_readdesc *)data;
352 return nfs_readpage_sync(desc->filp, page->mapping->host, page);
355 static int
356 readpage_async_filler(void *data, struct page *page)
358 struct nfs_readdesc *desc = (struct nfs_readdesc *)data;
359 struct inode *inode = page->mapping->host;
360 struct nfs_page *new;
362 nfs_wb_page(inode, page);
363 new = nfs_create_request(desc->filp, inode, page, 0, PAGE_CACHE_SIZE);
364 if (IS_ERR(new)) {
365 SetPageError(page);
366 unlock_page(page);
367 return PTR_ERR(new);
369 nfs_lock_request(new);
370 nfs_list_add_request(new, desc->head);
371 return 0;
375 nfs_readpages(struct file *filp, struct address_space *mapping,
376 struct list_head *pages, unsigned nr_pages)
378 LIST_HEAD(head);
379 struct nfs_readdesc desc = {
380 .filp = filp,
381 .head = &head,
383 struct nfs_server *server = NFS_SERVER(mapping->host);
384 int is_sync = server->rsize < PAGE_CACHE_SIZE;
385 int ret;
387 ret = read_cache_pages(mapping, pages,
388 is_sync ? readpage_sync_filler :
389 readpage_async_filler,
390 &desc);
391 if (!list_empty(&head)) {
392 int err = nfs_pagein_list(&head, server->rpages);
393 if (!ret)
394 ret = err;
396 return ret;
399 int nfs_init_readpagecache(void)
401 nfs_rdata_cachep = kmem_cache_create("nfs_read_data",
402 sizeof(struct nfs_read_data),
403 0, SLAB_HWCACHE_ALIGN,
404 NULL, NULL);
405 if (nfs_rdata_cachep == NULL)
406 return -ENOMEM;
408 nfs_rdata_mempool = mempool_create(MIN_POOL_READ,
409 mempool_alloc_slab,
410 mempool_free_slab,
411 nfs_rdata_cachep);
412 if (nfs_rdata_mempool == NULL)
413 return -ENOMEM;
415 return 0;
418 void nfs_destroy_readpagecache(void)
420 mempool_destroy(nfs_rdata_mempool);
421 if (kmem_cache_destroy(nfs_rdata_cachep))
422 printk(KERN_INFO "nfs_read_data: not all structures were freed\n");