ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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 __inline__ 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                 INIT_LIST_HEAD(&p->pages);
53         }
54         return p;
55 }
56
57 static __inline__ void nfs_readdata_free(struct nfs_read_data *p)
58 {
59         mempool_free(p, nfs_rdata_mempool);
60 }
61
62 static void nfs_readdata_release(struct rpc_task *task)
63 {
64         struct nfs_read_data   *data = (struct nfs_read_data *)task->tk_calldata;
65         nfs_readdata_free(data);
66 }
67
68 static
69 unsigned int nfs_page_length(struct inode *inode, struct page *page)
70 {
71         loff_t i_size = i_size_read(inode);
72         unsigned long idx;
73
74         if (i_size <= 0)
75                 return 0;
76         idx = (i_size - 1) >> PAGE_CACHE_SHIFT;
77         if (page->index > idx)
78                 return 0;
79         if (page->index != idx)
80                 return PAGE_CACHE_SIZE;
81         return 1 + ((i_size - 1) & (PAGE_CACHE_SIZE - 1));
82 }
83
84 static
85 int nfs_return_empty_page(struct page *page)
86 {
87         memclear_highpage_flush(page, 0, PAGE_CACHE_SIZE);
88         SetPageUptodate(page);
89         unlock_page(page);
90         return 0;
91 }
92
93 /*
94  * Read a page synchronously.
95  */
96 static int
97 nfs_readpage_sync(struct file *file, struct inode *inode, struct page *page)
98 {
99         unsigned int    rsize = NFS_SERVER(inode)->rsize;
100         unsigned int    count = PAGE_CACHE_SIZE;
101         int             result;
102         struct nfs_read_data    rdata = {
103                 .flags          = (IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0),
104                 .cred           = NULL,
105                 .inode          = inode,
106                 .args           = {
107                         .fh             = NFS_FH(inode),
108                         .lockowner      = current->files,
109                         .pages          = &page,
110                         .pgbase         = 0UL,
111                         .count          = rsize,
112                 },
113                 .res            = {
114                         .fattr          = &rdata.fattr,
115                 }
116         };
117
118         dprintk("NFS: nfs_readpage_sync(%p)\n", page);
119
120         /*
121          * This works now because the socket layer never tries to DMA
122          * into this buffer directly.
123          */
124         do {
125                 if (count < rsize)
126                         rdata.args.count = count;
127                 rdata.res.count = rdata.args.count;
128                 rdata.args.offset = page_offset(page) + rdata.args.pgbase;
129
130                 dprintk("NFS: nfs_proc_read(%s, (%s/%Ld), %Lu, %u)\n",
131                         NFS_SERVER(inode)->hostname,
132                         inode->i_sb->s_id,
133                         (long long)NFS_FILEID(inode),
134                         (unsigned long long)rdata.args.pgbase,
135                         rdata.args.count);
136
137                 lock_kernel();
138                 result = NFS_PROTO(inode)->read(&rdata, file);
139                 unlock_kernel();
140
141                 /*
142                  * Even if we had a partial success we can't mark the page
143                  * cache valid.
144                  */
145                 if (result < 0) {
146                         if (result == -EISDIR)
147                                 result = -EINVAL;
148                         goto io_error;
149                 }
150                 count -= result;
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.
154                  */
155                 if (rdata.res.eof != 0 || result == 0)
156                         break;
157         } while (count);
158         NFS_FLAGS(inode) |= NFS_INO_INVALID_ATIME;
159
160         if (count)
161                 memclear_highpage_flush(page, rdata.args.pgbase, count);
162         SetPageUptodate(page);
163         if (PageError(page))
164                 ClearPageError(page);
165         result = 0;
166
167 io_error:
168         unlock_page(page);
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                 list_add(&data->pages, &list);
309                 requests++;
310                 if (nbytes <= rsize)
311                         break;
312                 nbytes -= rsize;
313         }
314         atomic_set(&req->wb_complete, requests);
315
316         ClearPageError(page);
317         offset = 0;
318         nbytes = req->wb_bytes;
319         do {
320                 data = list_entry(list.next, struct nfs_read_data, pages);
321                 list_del_init(&data->pages);
322
323                 data->pagevec[0] = page;
324                 data->complete = nfs_readpage_result_partial;
325
326                 if (nbytes > rsize) {
327                         nfs_read_rpcsetup(req, data, rsize, offset);
328                         offset += rsize;
329                         nbytes -= rsize;
330                 } else {
331                         nfs_read_rpcsetup(req, data, nbytes, offset);
332                         nbytes = 0;
333                 }
334                 nfs_execute_read(data);
335         } while (nbytes != 0);
336
337         return 0;
338
339 out_bad:
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);
344         }
345         SetPageError(page);
346         nfs_readpage_release(req);
347         return -ENOMEM;
348 }
349
350 static int nfs_pagein_one(struct list_head *head, struct inode *inode)
351 {
352         struct nfs_page         *req;
353         struct page             **pages;
354         struct nfs_read_data    *data;
355         unsigned int            count;
356
357         if (NFS_SERVER(inode)->rsize < PAGE_CACHE_SIZE)
358                 return nfs_pagein_multi(head, inode);
359
360         data = nfs_readdata_alloc();
361         if (!data)
362                 goto out_bad;
363
364         pages = data->pagevec;
365         count = 0;
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;
373         }
374         req = nfs_list_entry(data->pages.next);
375
376         data->complete = nfs_readpage_result_full;
377         nfs_read_rpcsetup(req, data, count, 0);
378
379         nfs_execute_read(data);
380         return 0;
381 out_bad:
382         nfs_async_read_error(head);
383         return -ENOMEM;
384 }
385
386 int
387 nfs_pagein_list(struct list_head *head, int rpages)
388 {
389         LIST_HEAD(one_request);
390         struct nfs_page         *req;
391         int                     error = 0;
392         unsigned int            pages = 0;
393
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);
398                 if (error < 0)
399                         break;
400         }
401         if (error >= 0)
402                 return pages;
403
404         nfs_async_read_error(head);
405         return error;
406 }
407
408 /*
409  * Handle a read reply that fills part of a page.
410  */
411 static void nfs_readpage_result_partial(struct nfs_read_data *data, int status)
412 {
413         struct nfs_page *req = data->req;
414         struct page *page = req->wb_page;
415  
416         if (status >= 0) {
417                 unsigned int request = data->args.count;
418                 unsigned int result = data->res.count;
419
420                 if (result < request) {
421                         memclear_highpage_flush(page,
422                                                 data->args.pgbase + result,
423                                                 request - result);
424                 }
425         } else
426                 SetPageError(page);
427
428         if (atomic_dec_and_test(&req->wb_complete)) {
429                 if (!PageError(page))
430                         SetPageUptodate(page);
431                 nfs_readpage_release(req);
432         }
433 }
434
435 /*
436  * This is the callback from RPC telling us whether a reply was
437  * received or some error occurred (timeout or socket shutdown).
438  */
439 static void nfs_readpage_result_full(struct nfs_read_data *data, int status)
440 {
441         unsigned int count = data->res.count;
442
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);
447
448                 if (status >= 0) {
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);
454                                 count = 0;
455                         } else
456                                 count -= PAGE_CACHE_SIZE;
457                         SetPageUptodate(page);
458                 } else
459                         SetPageError(page);
460                 nfs_readpage_release(req);
461         }
462 }
463
464 /*
465  * This is the callback from RPC telling us whether a reply was
466  * received or some error occurred (timeout or socket shutdown).
467  */
468 void nfs_readpage_result(struct rpc_task *task)
469 {
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;
474
475         dprintk("NFS: %4d nfs_readpage_result, (status %d)\n",
476                 task->tk_pid, status);
477
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);
487                         return;
488                 }
489                 task->tk_status = -EIO;
490         }
491         NFS_FLAGS(data->inode) |= NFS_INO_INVALID_ATIME;
492         data->complete(data, status);
493 }
494
495 /*
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.
500  */
501 int
502 nfs_readpage(struct file *file, struct page *page)
503 {
504         struct inode *inode = page->mapping->host;
505         int             error;
506
507         dprintk("NFS: nfs_readpage (%p %ld@%lu)\n",
508                 page, PAGE_CACHE_SIZE, page->index);
509         /*
510          * Try to flush any pending writes to the file..
511          *
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).
515          */
516         error = nfs_wb_page(inode, page);
517         if (error)
518                 goto out_error;
519
520         if (!IS_SYNC(inode)) {
521                 error = nfs_readpage_async(file, inode, page);
522                 goto out;
523         }
524
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");
528 out:
529         return error;
530
531 out_error:
532         unlock_page(page);
533         goto out;
534 }
535
536 struct nfs_readdesc {
537         struct list_head *head;
538         struct file *filp;
539 };
540
541 static int
542 readpage_async_filler(void *data, struct page *page)
543 {
544         struct nfs_readdesc *desc = (struct nfs_readdesc *)data;
545         struct inode *inode = page->mapping->host;
546         struct nfs_page *new;
547         unsigned int len;
548
549         nfs_wb_page(inode, page);
550         len = nfs_page_length(inode, page);
551         if (len == 0)
552                 return nfs_return_empty_page(page);
553         new = nfs_create_request(desc->filp, inode, page, 0, len);
554         if (IS_ERR(new)) {
555                         SetPageError(page);
556                         unlock_page(page);
557                         return PTR_ERR(new);
558         }
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);
563         return 0;
564 }
565
566 int
567 nfs_readpages(struct file *filp, struct address_space *mapping,
568                 struct list_head *pages, unsigned nr_pages)
569 {
570         LIST_HEAD(head);
571         struct nfs_readdesc desc = {
572                 .filp           = filp,
573                 .head           = &head,
574         };
575         struct inode *inode = mapping->host;
576         struct nfs_server *server = NFS_SERVER(inode);
577         int ret;
578
579         dprintk("NFS: nfs_readpages (%s/%Ld %d)\n",
580                         inode->i_sb->s_id,
581                         (long long)NFS_FILEID(inode),
582                         nr_pages);
583
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);
587                 if (!ret)
588                         ret = err;
589         }
590         return ret;
591 }
592
593 int nfs_init_readpagecache(void)
594 {
595         nfs_rdata_cachep = kmem_cache_create("nfs_read_data",
596                                              sizeof(struct nfs_read_data),
597                                              0, SLAB_HWCACHE_ALIGN,
598                                              NULL, NULL);
599         if (nfs_rdata_cachep == NULL)
600                 return -ENOMEM;
601
602         nfs_rdata_mempool = mempool_create(MIN_POOL_READ,
603                                            mempool_alloc_slab,
604                                            mempool_free_slab,
605                                            nfs_rdata_cachep);
606         if (nfs_rdata_mempool == NULL)
607                 return -ENOMEM;
608
609         return 0;
610 }
611
612 void nfs_destroy_readpagecache(void)
613 {
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");
617 }