patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / fs / nfs / read.c
1 /*
2  * linux/fs/nfs/read.c
3  *
4  * Block I/O for NFS
5  *
6  * Partial copy of Linus' read cache modifications to fs/nfs/file.c
7  * modified for async RPC by okir@monad.swb.de
8  *
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.
16  */
17
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>
32
33 #include <asm/system.h>
34
35 #define NFSDBG_FACILITY         NFSDBG_PAGECACHE
36
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);
40
41 static kmem_cache_t *nfs_rdata_cachep;
42 static mempool_t *nfs_rdata_mempool;
43
44 #define MIN_POOL_READ   (32)
45
46 static struct nfs_read_data *nfs_readdata_alloc(void)
47 {
48         struct nfs_read_data   *p;
49         p = (struct nfs_read_data *)mempool_alloc(nfs_rdata_mempool, SLAB_NOFS);
50         if (p)
51                 memset(p, 0, sizeof(*p));
52         return p;
53 }
54
55 static __inline__ void nfs_readdata_free(struct nfs_read_data *p)
56 {
57         mempool_free(p, nfs_rdata_mempool);
58 }
59
60 static void nfs_readdata_release(struct rpc_task *task)
61 {
62         struct nfs_read_data   *data = (struct nfs_read_data *)task->tk_calldata;
63         nfs_readdata_free(data);
64 }
65
66 static
67 unsigned int nfs_page_length(struct inode *inode, struct page *page)
68 {
69         loff_t i_size = i_size_read(inode);
70         unsigned long idx;
71
72         if (i_size <= 0)
73                 return 0;
74         idx = (i_size - 1) >> PAGE_CACHE_SHIFT;
75         if (page->index > idx)
76                 return 0;
77         if (page->index != idx)
78                 return PAGE_CACHE_SIZE;
79         return 1 + ((i_size - 1) & (PAGE_CACHE_SIZE - 1));
80 }
81
82 static
83 int nfs_return_empty_page(struct page *page)
84 {
85         memclear_highpage_flush(page, 0, PAGE_CACHE_SIZE);
86         SetPageUptodate(page);
87         unlock_page(page);
88         return 0;
89 }
90
91 /*
92  * Read a page synchronously.
93  */
94 static int
95 nfs_readpage_sync(struct file *file, struct inode *inode, struct page *page)
96 {
97         unsigned int    rsize = NFS_SERVER(inode)->rsize;
98         unsigned int    count = PAGE_CACHE_SIZE;
99         int             result;
100         struct nfs_read_data *rdata;
101
102         rdata = nfs_readdata_alloc();
103         if (!rdata)
104                 return -ENOMEM;
105
106         memset(rdata, 0, sizeof(*rdata));
107         rdata->flags = (IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0);
108         rdata->inode = inode;
109         INIT_LIST_HEAD(&rdata->pages);
110         rdata->args.fh = NFS_FH(inode);
111         rdata->args.lockowner = current->files;
112         rdata->args.pages = &page;
113         rdata->args.pgbase = 0UL;
114         rdata->args.count = rsize;
115         rdata->res.fattr = &rdata->fattr;
116
117         dprintk("NFS: nfs_readpage_sync(%p)\n", page);
118
119         /*
120          * This works now because the socket layer never tries to DMA
121          * into this buffer directly.
122          */
123         do {
124                 if (count < rsize)
125                         rdata->args.count = count;
126                 rdata->res.count = rdata->args.count;
127                 rdata->args.offset = page_offset(page) + rdata->args.pgbase;
128
129                 dprintk("NFS: nfs_proc_read(%s, (%s/%Ld), %Lu, %u)\n",
130                         NFS_SERVER(inode)->hostname,
131                         inode->i_sb->s_id,
132                         (long long)NFS_FILEID(inode),
133                         (unsigned long long)rdata->args.pgbase,
134                         rdata->args.count);
135
136                 lock_kernel();
137                 result = NFS_PROTO(inode)->read(rdata, file);
138                 unlock_kernel();
139
140                 /*
141                  * Even if we had a partial success we can't mark the page
142                  * cache valid.
143                  */
144                 if (result < 0) {
145                         if (result == -EISDIR)
146                                 result = -EINVAL;
147                         goto io_error;
148                 }
149                 count -= result;
150                 rdata->args.pgbase += result;
151                 /* Note: result == 0 should only happen if we're caching
152                  * a write that extends the file and punches a hole.
153                  */
154                 if (rdata->res.eof != 0 || result == 0)
155                         break;
156         } while (count);
157         NFS_FLAGS(inode) |= NFS_INO_INVALID_ATIME;
158
159         if (count)
160                 memclear_highpage_flush(page, rdata->args.pgbase, count);
161         SetPageUptodate(page);
162         if (PageError(page))
163                 ClearPageError(page);
164         result = 0;
165
166 io_error:
167         unlock_page(page);
168         nfs_readdata_free(rdata);
169         return result;
170 }
171
172 static int
173 nfs_readpage_async(struct file *file, struct inode *inode, struct page *page)
174 {
175         LIST_HEAD(one_request);
176         struct nfs_page *new;
177         unsigned int len;
178
179         len = nfs_page_length(inode, page);
180         if (len == 0)
181                 return nfs_return_empty_page(page);
182         new = nfs_create_request(file, inode, page, 0, len);
183         if (IS_ERR(new)) {
184                 unlock_page(page);
185                 return PTR_ERR(new);
186         }
187         if (len < PAGE_CACHE_SIZE)
188                 memclear_highpage_flush(page, len, PAGE_CACHE_SIZE - len);
189
190         nfs_lock_request(new);
191         nfs_list_add_request(new, &one_request);
192         nfs_pagein_one(&one_request, inode);
193         return 0;
194 }
195
196 static void nfs_readpage_release(struct nfs_page *req)
197 {
198         unlock_page(req->wb_page);
199
200         nfs_clear_request(req);
201         nfs_release_request(req);
202         nfs_unlock_request(req);
203
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),
207                         req->wb_bytes,
208                         (long long)req_offset(req));
209 }
210
211 /*
212  * Set up the NFS read request struct
213  */
214 static void nfs_read_rpcsetup(struct nfs_page *req, struct nfs_read_data *data,
215                 unsigned int count, unsigned int offset)
216 {
217         struct inode            *inode;
218
219         data->req         = req;
220         data->inode       = inode = req->wb_inode;
221         data->cred        = req->wb_cred;
222
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;
230
231         data->res.fattr   = &data->fattr;
232         data->res.count   = count;
233         data->res.eof     = 0;
234
235         NFS_PROTO(inode)->read_setup(data);
236
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;
241
242         dprintk("NFS: %4d initiated read call (req %s/%Ld, %u bytes @ offset %Lu)\n",
243                         data->task.tk_pid,
244                         inode->i_sb->s_id,
245                         (long long)NFS_FILEID(inode),
246                         count,
247                         (unsigned long long)data->args.offset);
248 }
249
250 static void
251 nfs_async_read_error(struct list_head *head)
252 {
253         struct nfs_page *req;
254
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);
260         }
261 }
262
263 /*
264  * Start an async read operation
265  */
266 static void nfs_execute_read(struct nfs_read_data *data)
267 {
268         struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
269         sigset_t oldset;
270
271         rpc_clnt_sigmask(clnt, &oldset);
272         lock_kernel();
273         rpc_execute(&data->task);
274         unlock_kernel();
275         rpc_clnt_sigunmask(clnt, &oldset);
276 }
277
278 /*
279  * Generate multiple requests to fill a single page.
280  *
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.
285  *
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.
290  */
291 static int nfs_pagein_multi(struct list_head *head, struct inode *inode)
292 {
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;
298         int requests = 0;
299         LIST_HEAD(list);
300
301         nfs_list_remove_request(req);
302
303         nbytes = req->wb_bytes;
304         for(;;) {
305                 data = nfs_readdata_alloc();
306                 if (!data)
307                         goto out_bad;
308                 INIT_LIST_HEAD(&data->pages);
309                 list_add(&data->pages, &list);
310                 requests++;
311                 if (nbytes <= rsize)
312                         break;
313                 nbytes -= rsize;
314         }
315         atomic_set(&req->wb_complete, requests);
316
317         ClearPageError(page);
318         offset = 0;
319         nbytes = req->wb_bytes;
320         do {
321                 data = list_entry(list.next, struct nfs_read_data, pages);
322                 list_del_init(&data->pages);
323
324                 data->pagevec[0] = page;
325                 data->complete = nfs_readpage_result_partial;
326
327                 if (nbytes > rsize) {
328                         nfs_read_rpcsetup(req, data, rsize, offset);
329                         offset += rsize;
330                         nbytes -= rsize;
331                 } else {
332                         nfs_read_rpcsetup(req, data, nbytes, offset);
333                         nbytes = 0;
334                 }
335                 nfs_execute_read(data);
336         } while (nbytes != 0);
337
338         return 0;
339
340 out_bad:
341         while (!list_empty(&list)) {
342                 data = list_entry(list.next, struct nfs_read_data, pages);
343                 list_del(&data->pages);
344                 nfs_readdata_free(data);
345         }
346         SetPageError(page);
347         nfs_readpage_release(req);
348         return -ENOMEM;
349 }
350
351 static int nfs_pagein_one(struct list_head *head, struct inode *inode)
352 {
353         struct nfs_page         *req;
354         struct page             **pages;
355         struct nfs_read_data    *data;
356         unsigned int            count;
357
358         if (NFS_SERVER(inode)->rsize < PAGE_CACHE_SIZE)
359                 return nfs_pagein_multi(head, inode);
360
361         data = nfs_readdata_alloc();
362         if (!data)
363                 goto out_bad;
364
365         INIT_LIST_HEAD(&data->pages);
366         pages = data->pagevec;
367         count = 0;
368         while (!list_empty(head)) {
369                 req = nfs_list_entry(head->next);
370                 nfs_list_remove_request(req);
371                 nfs_list_add_request(req, &data->pages);
372                 ClearPageError(req->wb_page);
373                 *pages++ = req->wb_page;
374                 count += req->wb_bytes;
375         }
376         req = nfs_list_entry(data->pages.next);
377
378         data->complete = nfs_readpage_result_full;
379         nfs_read_rpcsetup(req, data, count, 0);
380
381         nfs_execute_read(data);
382         return 0;
383 out_bad:
384         nfs_async_read_error(head);
385         return -ENOMEM;
386 }
387
388 int
389 nfs_pagein_list(struct list_head *head, int rpages)
390 {
391         LIST_HEAD(one_request);
392         struct nfs_page         *req;
393         int                     error = 0;
394         unsigned int            pages = 0;
395
396         while (!list_empty(head)) {
397                 pages += nfs_coalesce_requests(head, &one_request, rpages);
398                 req = nfs_list_entry(one_request.next);
399                 error = nfs_pagein_one(&one_request, req->wb_inode);
400                 if (error < 0)
401                         break;
402         }
403         if (error >= 0)
404                 return pages;
405
406         nfs_async_read_error(head);
407         return error;
408 }
409
410 /*
411  * Handle a read reply that fills part of a page.
412  */
413 static void nfs_readpage_result_partial(struct nfs_read_data *data, int status)
414 {
415         struct nfs_page *req = data->req;
416         struct page *page = req->wb_page;
417  
418         if (status >= 0) {
419                 unsigned int request = data->args.count;
420                 unsigned int result = data->res.count;
421
422                 if (result < request) {
423                         memclear_highpage_flush(page,
424                                                 data->args.pgbase + result,
425                                                 request - result);
426                 }
427         } else
428                 SetPageError(page);
429
430         if (atomic_dec_and_test(&req->wb_complete)) {
431                 if (!PageError(page))
432                         SetPageUptodate(page);
433                 nfs_readpage_release(req);
434         }
435 }
436
437 /*
438  * This is the callback from RPC telling us whether a reply was
439  * received or some error occurred (timeout or socket shutdown).
440  */
441 static void nfs_readpage_result_full(struct nfs_read_data *data, int status)
442 {
443         unsigned int count = data->res.count;
444
445         while (!list_empty(&data->pages)) {
446                 struct nfs_page *req = nfs_list_entry(data->pages.next);
447                 struct page *page = req->wb_page;
448                 nfs_list_remove_request(req);
449
450                 if (status >= 0) {
451                         if (count < PAGE_CACHE_SIZE) {
452                                 if (count < req->wb_bytes)
453                                         memclear_highpage_flush(page,
454                                                         req->wb_pgbase + count,
455                                                         req->wb_bytes - count);
456                                 count = 0;
457                         } else
458                                 count -= PAGE_CACHE_SIZE;
459                         SetPageUptodate(page);
460                 } else
461                         SetPageError(page);
462                 nfs_readpage_release(req);
463         }
464 }
465
466 /*
467  * This is the callback from RPC telling us whether a reply was
468  * received or some error occurred (timeout or socket shutdown).
469  */
470 void nfs_readpage_result(struct rpc_task *task)
471 {
472         struct nfs_read_data *data = (struct nfs_read_data *)task->tk_calldata;
473         struct nfs_readargs *argp = &data->args;
474         struct nfs_readres *resp = &data->res;
475         int status = task->tk_status;
476
477         dprintk("NFS: %4d nfs_readpage_result, (status %d)\n",
478                 task->tk_pid, status);
479
480         /* Is this a short read? */
481         if (task->tk_status >= 0 && resp->count < argp->count && !resp->eof) {
482                 /* Has the server at least made some progress? */
483                 if (resp->count != 0) {
484                         /* Yes, so retry the read at the end of the data */
485                         argp->offset += resp->count;
486                         argp->pgbase += resp->count;
487                         argp->count -= resp->count;
488                         rpc_restart_call(task);
489                         return;
490                 }
491                 task->tk_status = -EIO;
492         }
493         NFS_FLAGS(data->inode) |= NFS_INO_INVALID_ATIME;
494         data->complete(data, status);
495 }
496
497 /*
498  * Read a page over NFS.
499  * We read the page synchronously in the following case:
500  *  -   The error flag is set for this page. This happens only when a
501  *      previous async read operation failed.
502  */
503 int
504 nfs_readpage(struct file *file, struct page *page)
505 {
506         struct inode *inode = page->mapping->host;
507         int             error;
508
509         dprintk("NFS: nfs_readpage (%p %ld@%lu)\n",
510                 page, PAGE_CACHE_SIZE, page->index);
511         /*
512          * Try to flush any pending writes to the file..
513          *
514          * NOTE! Because we own the page lock, there cannot
515          * be any new pending writes generated at this point
516          * for this page (other pages can be written to).
517          */
518         error = nfs_wb_page(inode, page);
519         if (error)
520                 goto out_error;
521
522         if (!IS_SYNC(inode)) {
523                 error = nfs_readpage_async(file, inode, page);
524                 goto out;
525         }
526
527         error = nfs_readpage_sync(file, inode, page);
528         if (error < 0 && IS_SWAPFILE(inode))
529                 printk("Aiee.. nfs swap-in of page failed!\n");
530 out:
531         return error;
532
533 out_error:
534         unlock_page(page);
535         goto out;
536 }
537
538 struct nfs_readdesc {
539         struct list_head *head;
540         struct file *filp;
541 };
542
543 static int
544 readpage_async_filler(void *data, struct page *page)
545 {
546         struct nfs_readdesc *desc = (struct nfs_readdesc *)data;
547         struct inode *inode = page->mapping->host;
548         struct nfs_page *new;
549         unsigned int len;
550
551         nfs_wb_page(inode, page);
552         len = nfs_page_length(inode, page);
553         if (len == 0)
554                 return nfs_return_empty_page(page);
555         new = nfs_create_request(desc->filp, inode, page, 0, len);
556         if (IS_ERR(new)) {
557                         SetPageError(page);
558                         unlock_page(page);
559                         return PTR_ERR(new);
560         }
561         if (len < PAGE_CACHE_SIZE)
562                 memclear_highpage_flush(page, len, PAGE_CACHE_SIZE - len);
563         nfs_lock_request(new);
564         nfs_list_add_request(new, desc->head);
565         return 0;
566 }
567
568 int
569 nfs_readpages(struct file *filp, struct address_space *mapping,
570                 struct list_head *pages, unsigned nr_pages)
571 {
572         LIST_HEAD(head);
573         struct nfs_readdesc desc = {
574                 .filp           = filp,
575                 .head           = &head,
576         };
577         struct inode *inode = mapping->host;
578         struct nfs_server *server = NFS_SERVER(inode);
579         int ret;
580
581         dprintk("NFS: nfs_readpages (%s/%Ld %d)\n",
582                         inode->i_sb->s_id,
583                         (long long)NFS_FILEID(inode),
584                         nr_pages);
585
586         ret = read_cache_pages(mapping, pages, readpage_async_filler, &desc);
587         if (!list_empty(&head)) {
588                 int err = nfs_pagein_list(&head, server->rpages);
589                 if (!ret)
590                         ret = err;
591         }
592         return ret;
593 }
594
595 int nfs_init_readpagecache(void)
596 {
597         nfs_rdata_cachep = kmem_cache_create("nfs_read_data",
598                                              sizeof(struct nfs_read_data),
599                                              0, SLAB_HWCACHE_ALIGN,
600                                              NULL, NULL);
601         if (nfs_rdata_cachep == NULL)
602                 return -ENOMEM;
603
604         nfs_rdata_mempool = mempool_create(MIN_POOL_READ,
605                                            mempool_alloc_slab,
606                                            mempool_free_slab,
607                                            nfs_rdata_cachep);
608         if (nfs_rdata_mempool == NULL)
609                 return -ENOMEM;
610
611         return 0;
612 }
613
614 void nfs_destroy_readpagecache(void)
615 {
616         mempool_destroy(nfs_rdata_mempool);
617         if (kmem_cache_destroy(nfs_rdata_cachep))
618                 printk(KERN_INFO "nfs_read_data: not all structures were freed\n");
619 }