Merge to Fedora kernel-2.6.18-1.2224_FC5 patched with stable patch-2.6.18.1-vs2.0...
[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/time.h>
19 #include <linux/kernel.h>
20 #include <linux/errno.h>
21 #include <linux/fcntl.h>
22 #include <linux/stat.h>
23 #include <linux/mm.h>
24 #include <linux/slab.h>
25 #include <linux/pagemap.h>
26 #include <linux/sunrpc/clnt.h>
27 #include <linux/nfs_fs.h>
28 #include <linux/nfs_page.h>
29 #include <linux/nfs_mount.h>
30 #include <linux/smp_lock.h>
31
32 #include <asm/system.h>
33
34 #include "iostat.h"
35 #include "internal.h"
36
37 #define NFSDBG_FACILITY         NFSDBG_PAGECACHE
38
39 static int nfs_pagein_one(struct list_head *, struct inode *);
40 static const struct rpc_call_ops nfs_read_partial_ops;
41 static const struct rpc_call_ops nfs_read_full_ops;
42
43 static kmem_cache_t *nfs_rdata_cachep;
44 static mempool_t *nfs_rdata_mempool;
45
46 #define MIN_POOL_READ   (32)
47
48 struct nfs_read_data *nfs_readdata_alloc(size_t len)
49 {
50         unsigned int pagecount = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
51         struct nfs_read_data *p = mempool_alloc(nfs_rdata_mempool, SLAB_NOFS);
52
53         if (p) {
54                 memset(p, 0, sizeof(*p));
55                 INIT_LIST_HEAD(&p->pages);
56                 p->npages = pagecount;
57                 if (pagecount <= ARRAY_SIZE(p->page_array))
58                         p->pagevec = p->page_array;
59                 else {
60                         p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
61                         if (!p->pagevec) {
62                                 mempool_free(p, nfs_rdata_mempool);
63                                 p = NULL;
64                         }
65                 }
66         }
67         return p;
68 }
69
70 static void nfs_readdata_free(struct nfs_read_data *p)
71 {
72         if (p && (p->pagevec != &p->page_array[0]))
73                 kfree(p->pagevec);
74         mempool_free(p, nfs_rdata_mempool);
75 }
76
77 void nfs_readdata_release(void *data)
78 {
79         nfs_readdata_free(data);
80 }
81
82 static
83 unsigned int nfs_page_length(struct inode *inode, struct page *page)
84 {
85         loff_t i_size = i_size_read(inode);
86         unsigned long idx;
87
88         if (i_size <= 0)
89                 return 0;
90         idx = (i_size - 1) >> PAGE_CACHE_SHIFT;
91         if (page->index > idx)
92                 return 0;
93         if (page->index != idx)
94                 return PAGE_CACHE_SIZE;
95         return 1 + ((i_size - 1) & (PAGE_CACHE_SIZE - 1));
96 }
97
98 static
99 int nfs_return_empty_page(struct page *page)
100 {
101         memclear_highpage_flush(page, 0, PAGE_CACHE_SIZE);
102         SetPageUptodate(page);
103         unlock_page(page);
104         return 0;
105 }
106
107 static void nfs_readpage_truncate_uninitialised_page(struct nfs_read_data *data)
108 {
109         unsigned int remainder = data->args.count - data->res.count;
110         unsigned int base = data->args.pgbase + data->res.count;
111         unsigned int pglen;
112         struct page **pages;
113
114         if (data->res.eof == 0 || remainder == 0)
115                 return;
116         /*
117          * Note: "remainder" can never be negative, since we check for
118          *      this in the XDR code.
119          */
120         pages = &data->args.pages[base >> PAGE_CACHE_SHIFT];
121         base &= ~PAGE_CACHE_MASK;
122         pglen = PAGE_CACHE_SIZE - base;
123         for (;;) {
124                 if (remainder <= pglen) {
125                         memclear_highpage_flush(*pages, base, remainder);
126                         break;
127                 }
128                 memclear_highpage_flush(*pages, base, pglen);
129                 pages++;
130                 remainder -= pglen;
131                 pglen = PAGE_CACHE_SIZE;
132                 base = 0;
133         }
134 }
135
136 /*
137  * Read a page synchronously.
138  */
139 static int nfs_readpage_sync(struct nfs_open_context *ctx, struct inode *inode,
140                 struct page *page)
141 {
142         unsigned int    rsize = NFS_SERVER(inode)->rsize;
143         unsigned int    count = PAGE_CACHE_SIZE;
144         int             result;
145         struct nfs_read_data *rdata;
146
147         rdata = nfs_readdata_alloc(count);
148         if (!rdata)
149                 return -ENOMEM;
150
151         memset(rdata, 0, sizeof(*rdata));
152         rdata->flags = (IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0);
153         rdata->cred = ctx->cred;
154         rdata->inode = inode;
155         INIT_LIST_HEAD(&rdata->pages);
156         rdata->args.fh = NFS_FH(inode);
157         rdata->args.context = ctx;
158         rdata->args.pages = &page;
159         rdata->args.pgbase = 0UL;
160         rdata->args.count = rsize;
161         rdata->res.fattr = &rdata->fattr;
162
163         dprintk("NFS: nfs_readpage_sync(%p)\n", page);
164
165         /*
166          * This works now because the socket layer never tries to DMA
167          * into this buffer directly.
168          */
169         do {
170                 if (count < rsize)
171                         rdata->args.count = count;
172                 rdata->res.count = rdata->args.count;
173                 rdata->args.offset = page_offset(page) + rdata->args.pgbase;
174
175                 dprintk("NFS: nfs_proc_read(%s, (%s/%Ld), %Lu, %u)\n",
176                         NFS_SERVER(inode)->nfs_client->cl_hostname,
177                         inode->i_sb->s_id,
178                         (long long)NFS_FILEID(inode),
179                         (unsigned long long)rdata->args.pgbase,
180                         rdata->args.count);
181
182                 lock_kernel();
183                 result = NFS_PROTO(inode)->read(rdata);
184                 unlock_kernel();
185
186                 /*
187                  * Even if we had a partial success we can't mark the page
188                  * cache valid.
189                  */
190                 if (result < 0) {
191                         if (result == -EISDIR)
192                                 result = -EINVAL;
193                         goto io_error;
194                 }
195                 count -= result;
196                 rdata->args.pgbase += result;
197                 nfs_add_stats(inode, NFSIOS_SERVERREADBYTES, result);
198
199                 /* Note: result == 0 should only happen if we're caching
200                  * a write that extends the file and punches a hole.
201                  */
202                 if (rdata->res.eof != 0 || result == 0)
203                         break;
204         } while (count);
205         spin_lock(&inode->i_lock);
206         NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATIME;
207         spin_unlock(&inode->i_lock);
208
209         if (rdata->res.eof || rdata->res.count == rdata->args.count) {
210                 SetPageUptodate(page);
211                 if (rdata->res.eof && count != 0)
212                         memclear_highpage_flush(page, rdata->args.pgbase, count);
213         }
214         result = 0;
215
216         nfs_readpage_to_fscache(inode, page, 1);
217         unlock_page(page);
218
219         return result;
220
221 io_error:
222         unlock_page(page);
223         nfs_readdata_free(rdata);
224         return result;
225 }
226
227 int nfs_readpage_async(struct nfs_open_context *ctx, struct inode *inode,
228                 struct page *page)
229 {
230         LIST_HEAD(one_request);
231         struct nfs_page *new;
232         unsigned int len;
233
234         len = nfs_page_length(inode, page);
235         if (len == 0)
236                 return nfs_return_empty_page(page);
237         new = nfs_create_request(ctx, inode, page, 0, len);
238         if (IS_ERR(new)) {
239                 unlock_page(page);
240                 return PTR_ERR(new);
241         }
242         if (len < PAGE_CACHE_SIZE)
243                 memclear_highpage_flush(page, len, PAGE_CACHE_SIZE - len);
244
245         nfs_list_add_request(new, &one_request);
246         nfs_pagein_one(&one_request, inode);
247         return 0;
248 }
249
250 static void nfs_readpage_release(struct nfs_page *req)
251 {
252         struct inode *d_inode = req->wb_context->dentry->d_inode;
253
254         if (PageUptodate(req->wb_page))
255                 nfs_readpage_to_fscache(d_inode, req->wb_page, 0);
256
257         unlock_page(req->wb_page);
258
259         dprintk("NFS: read done (%s/%Ld %d@%Ld)\n",
260                         req->wb_context->dentry->d_inode->i_sb->s_id,
261                         (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
262                         req->wb_bytes,
263                         (long long)req_offset(req));
264         nfs_clear_request(req);
265         nfs_release_request(req);
266 }
267
268 /*
269  * Set up the NFS read request struct
270  */
271 static void nfs_read_rpcsetup(struct nfs_page *req, struct nfs_read_data *data,
272                 const struct rpc_call_ops *call_ops,
273                 unsigned int count, unsigned int offset)
274 {
275         struct inode            *inode;
276         int flags;
277
278         data->req         = req;
279         data->inode       = inode = req->wb_context->dentry->d_inode;
280         data->cred        = req->wb_context->cred;
281
282         data->args.fh     = NFS_FH(inode);
283         data->args.offset = req_offset(req) + offset;
284         data->args.pgbase = req->wb_pgbase + offset;
285         data->args.pages  = data->pagevec;
286         data->args.count  = count;
287         data->args.context = req->wb_context;
288
289         data->res.fattr   = &data->fattr;
290         data->res.count   = count;
291         data->res.eof     = 0;
292         nfs_fattr_init(&data->fattr);
293
294         /* Set up the initial task struct. */
295         flags = RPC_TASK_ASYNC | (IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0);
296         rpc_init_task(&data->task, NFS_CLIENT(inode), flags, call_ops, data);
297         NFS_PROTO(inode)->read_setup(data);
298
299         data->task.tk_cookie = (unsigned long)inode;
300
301         dprintk("NFS: %4d initiated read call (req %s/%Ld, %u bytes @ offset %Lu)\n",
302                         data->task.tk_pid,
303                         inode->i_sb->s_id,
304                         (long long)NFS_FILEID(inode),
305                         count,
306                         (unsigned long long)data->args.offset);
307 }
308
309 static void
310 nfs_async_read_error(struct list_head *head)
311 {
312         struct nfs_page *req;
313
314         while (!list_empty(head)) {
315                 req = nfs_list_entry(head->next);
316                 nfs_list_remove_request(req);
317                 SetPageError(req->wb_page);
318                 nfs_readpage_release(req);
319         }
320 }
321
322 /*
323  * Start an async read operation
324  */
325 static void nfs_execute_read(struct nfs_read_data *data)
326 {
327         struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
328         sigset_t oldset;
329
330         rpc_clnt_sigmask(clnt, &oldset);
331         lock_kernel();
332         rpc_execute(&data->task);
333         unlock_kernel();
334         rpc_clnt_sigunmask(clnt, &oldset);
335 }
336
337 /*
338  * Generate multiple requests to fill a single page.
339  *
340  * We optimize to reduce the number of read operations on the wire.  If we
341  * detect that we're reading a page, or an area of a page, that is past the
342  * end of file, we do not generate NFS read operations but just clear the
343  * parts of the page that would have come back zero from the server anyway.
344  *
345  * We rely on the cached value of i_size to make this determination; another
346  * client can fill pages on the server past our cached end-of-file, but we
347  * won't see the new data until our attribute cache is updated.  This is more
348  * or less conventional NFS client behavior.
349  */
350 static int nfs_pagein_multi(struct list_head *head, struct inode *inode)
351 {
352         struct nfs_page *req = nfs_list_entry(head->next);
353         struct page *page = req->wb_page;
354         struct nfs_read_data *data;
355         size_t rsize = NFS_SERVER(inode)->rsize, nbytes;
356         unsigned int offset;
357         int requests = 0;
358         LIST_HEAD(list);
359
360         nfs_list_remove_request(req);
361
362         nbytes = req->wb_bytes;
363         do {
364                 size_t len = min(nbytes,rsize);
365
366                 data = nfs_readdata_alloc(len);
367                 if (!data)
368                         goto out_bad;
369                 INIT_LIST_HEAD(&data->pages);
370                 list_add(&data->pages, &list);
371                 requests++;
372                 nbytes -= len;
373         } while(nbytes != 0);
374         atomic_set(&req->wb_complete, requests);
375
376         ClearPageError(page);
377         offset = 0;
378         nbytes = req->wb_bytes;
379         do {
380                 data = list_entry(list.next, struct nfs_read_data, pages);
381                 list_del_init(&data->pages);
382
383                 data->pagevec[0] = page;
384
385                 if (nbytes > rsize) {
386                         nfs_read_rpcsetup(req, data, &nfs_read_partial_ops,
387                                         rsize, offset);
388                         offset += rsize;
389                         nbytes -= rsize;
390                 } else {
391                         nfs_read_rpcsetup(req, data, &nfs_read_partial_ops,
392                                         nbytes, offset);
393                         nbytes = 0;
394                 }
395                 nfs_execute_read(data);
396         } while (nbytes != 0);
397
398         return 0;
399
400 out_bad:
401         while (!list_empty(&list)) {
402                 data = list_entry(list.next, struct nfs_read_data, pages);
403                 list_del(&data->pages);
404                 nfs_readdata_free(data);
405         }
406         SetPageError(page);
407         nfs_readpage_release(req);
408         return -ENOMEM;
409 }
410
411 static int nfs_pagein_one(struct list_head *head, struct inode *inode)
412 {
413         struct nfs_page         *req;
414         struct page             **pages;
415         struct nfs_read_data    *data;
416         unsigned int            count;
417
418         if (NFS_SERVER(inode)->rsize < PAGE_CACHE_SIZE)
419                 return nfs_pagein_multi(head, inode);
420
421         data = nfs_readdata_alloc(NFS_SERVER(inode)->rsize);
422         if (!data)
423                 goto out_bad;
424
425         INIT_LIST_HEAD(&data->pages);
426         pages = data->pagevec;
427         count = 0;
428         while (!list_empty(head)) {
429                 req = nfs_list_entry(head->next);
430                 nfs_list_remove_request(req);
431                 nfs_list_add_request(req, &data->pages);
432                 ClearPageError(req->wb_page);
433                 *pages++ = req->wb_page;
434                 count += req->wb_bytes;
435         }
436         req = nfs_list_entry(data->pages.next);
437
438         nfs_read_rpcsetup(req, data, &nfs_read_full_ops, count, 0);
439
440         nfs_execute_read(data);
441         return 0;
442 out_bad:
443         nfs_async_read_error(head);
444         return -ENOMEM;
445 }
446
447 static int
448 nfs_pagein_list(struct list_head *head, int rpages)
449 {
450         LIST_HEAD(one_request);
451         struct nfs_page         *req;
452         int                     error = 0;
453         unsigned int            pages = 0;
454
455         while (!list_empty(head)) {
456                 pages += nfs_coalesce_requests(head, &one_request, rpages);
457                 req = nfs_list_entry(one_request.next);
458                 error = nfs_pagein_one(&one_request, req->wb_context->dentry->d_inode);
459                 if (error < 0)
460                         break;
461         }
462         if (error >= 0)
463                 return pages;
464
465         nfs_async_read_error(head);
466         return error;
467 }
468
469 /*
470  * Handle a read reply that fills part of a page.
471  */
472 static void nfs_readpage_result_partial(struct rpc_task *task, void *calldata)
473 {
474         struct nfs_read_data *data = calldata;
475         struct nfs_page *req = data->req;
476         struct page *page = req->wb_page;
477  
478         if (likely(task->tk_status >= 0))
479                 nfs_readpage_truncate_uninitialised_page(data);
480         else
481                 SetPageError(page);
482         if (nfs_readpage_result(task, data) != 0)
483                 return;
484         if (atomic_dec_and_test(&req->wb_complete)) {
485                 if (!PageError(page))
486                         SetPageUptodate(page);
487                 nfs_readpage_release(req);
488         }
489 }
490
491 static const struct rpc_call_ops nfs_read_partial_ops = {
492         .rpc_call_done = nfs_readpage_result_partial,
493         .rpc_release = nfs_readdata_release,
494 };
495
496 static void nfs_readpage_set_pages_uptodate(struct nfs_read_data *data)
497 {
498         unsigned int count = data->res.count;
499         unsigned int base = data->args.pgbase;
500         struct page **pages;
501
502         if (data->res.eof)
503                 count = data->args.count;
504         if (unlikely(count == 0))
505                 return;
506         pages = &data->args.pages[base >> PAGE_CACHE_SHIFT];
507         base &= ~PAGE_CACHE_MASK;
508         count += base;
509         for (;count >= PAGE_CACHE_SIZE; count -= PAGE_CACHE_SIZE, pages++)
510                 SetPageUptodate(*pages);
511         if (count != 0)
512                 SetPageUptodate(*pages);
513 }
514
515 static void nfs_readpage_set_pages_error(struct nfs_read_data *data)
516 {
517         unsigned int count = data->args.count;
518         unsigned int base = data->args.pgbase;
519         struct page **pages;
520
521         pages = &data->args.pages[base >> PAGE_CACHE_SHIFT];
522         base &= ~PAGE_CACHE_MASK;
523         count += base;
524         for (;count >= PAGE_CACHE_SIZE; count -= PAGE_CACHE_SIZE, pages++)
525                 SetPageError(*pages);
526         if (count != 0)
527                 SetPageError(*pages);
528 }
529
530 /*
531  * This is the callback from RPC telling us whether a reply was
532  * received or some error occurred (timeout or socket shutdown).
533  */
534 static void nfs_readpage_result_full(struct rpc_task *task, void *calldata)
535 {
536         struct nfs_read_data *data = calldata;
537
538         /*
539          * Note: nfs_readpage_result may change the values of
540          * data->args. In the multi-page case, we therefore need
541          * to ensure that we call the next nfs_readpage_set_page_uptodate()
542          * first in the multi-page case.
543          */
544         if (likely(task->tk_status >= 0)) {
545                 nfs_readpage_truncate_uninitialised_page(data);
546                 nfs_readpage_set_pages_uptodate(data);
547         } else
548                 nfs_readpage_set_pages_error(data);
549         if (nfs_readpage_result(task, data) != 0)
550                 return;
551         while (!list_empty(&data->pages)) {
552                 struct nfs_page *req = nfs_list_entry(data->pages.next);
553
554                 nfs_list_remove_request(req);
555                 nfs_readpage_release(req);
556         }
557 }
558
559 static const struct rpc_call_ops nfs_read_full_ops = {
560         .rpc_call_done = nfs_readpage_result_full,
561         .rpc_release = nfs_readdata_release,
562 };
563
564 /*
565  * This is the callback from RPC telling us whether a reply was
566  * received or some error occurred (timeout or socket shutdown).
567  */
568 int nfs_readpage_result(struct rpc_task *task, struct nfs_read_data *data)
569 {
570         struct nfs_readargs *argp = &data->args;
571         struct nfs_readres *resp = &data->res;
572         int status;
573
574         dprintk("NFS: %4d nfs_readpage_result, (status %d)\n",
575                 task->tk_pid, task->tk_status);
576
577         status = NFS_PROTO(data->inode)->read_done(task, data);
578         if (status != 0)
579                 return status;
580
581         nfs_add_stats(data->inode, NFSIOS_SERVERREADBYTES, resp->count);
582
583         /* Is this a short read? */
584         if (task->tk_status >= 0 && resp->count < argp->count && !resp->eof) {
585                 nfs_inc_stats(data->inode, NFSIOS_SHORTREAD);
586                 /* Has the server at least made some progress? */
587                 if (resp->count != 0) {
588                         /* Yes, so retry the read at the end of the data */
589                         argp->offset += resp->count;
590                         argp->pgbase += resp->count;
591                         argp->count -= resp->count;
592                         rpc_restart_call(task);
593                         return -EAGAIN;
594                 }
595                 task->tk_status = -EIO;
596         }
597         spin_lock(&data->inode->i_lock);
598         NFS_I(data->inode)->cache_validity |= NFS_INO_INVALID_ATIME;
599         spin_unlock(&data->inode->i_lock);
600         return 0;
601 }
602
603 /*
604  * Read a page over NFS.
605  * We read the page synchronously in the following case:
606  *  -   The error flag is set for this page. This happens only when a
607  *      previous async read operation failed.
608  */
609 int nfs_readpage(struct file *file, struct page *page)
610 {
611         struct nfs_open_context *ctx;
612         struct inode *inode = page->mapping->host;
613         int             error;
614
615         dprintk("NFS: nfs_readpage (%p %ld@%lu)\n",
616                 page, PAGE_CACHE_SIZE, page->index);
617         nfs_inc_stats(inode, NFSIOS_VFSREADPAGE);
618         nfs_add_stats(inode, NFSIOS_READPAGES, 1);
619
620         /*
621          * Try to flush any pending writes to the file..
622          *
623          * NOTE! Because we own the page lock, there cannot
624          * be any new pending writes generated at this point
625          * for this page (other pages can be written to).
626          */
627         error = nfs_wb_page(inode, page);
628         if (error)
629                 goto out_error;
630
631         if (file == NULL) {
632                 ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
633                 if (ctx == NULL)
634                         return -EBADF;
635         } else
636                 ctx = get_nfs_open_context((struct nfs_open_context *)
637                                 file->private_data);
638         if (!IS_SYNC(inode)) {
639                 error = nfs_readpage_from_fscache(ctx, inode, page);
640                 if (error == 0)
641                         goto out;
642
643                 error = nfs_readpage_async(ctx, inode, page);
644                 goto out;
645         }
646
647         error = nfs_readpage_sync(ctx, inode, page);
648         if (error < 0 && IS_SWAPFILE(inode))
649                 printk("Aiee.. nfs swap-in of page failed!\n");
650 out:
651         put_nfs_open_context(ctx);
652         return error;
653
654 out_error:
655         unlock_page(page);
656         return error;
657 }
658
659 struct nfs_readdesc {
660         struct list_head *head;
661         struct nfs_open_context *ctx;
662 };
663
664 static int
665 readpage_async_filler(void *data, struct page *page)
666 {
667         struct nfs_readdesc *desc = (struct nfs_readdesc *)data;
668         struct inode *inode = page->mapping->host;
669         struct nfs_page *new;
670         unsigned int len;
671
672         nfs_wb_page(inode, page);
673
674         len = nfs_page_length(inode, page);
675         if (len == 0)
676                 return nfs_return_empty_page(page);
677         new = nfs_create_request(desc->ctx, inode, page, 0, len);
678         if (IS_ERR(new)) {
679                         SetPageError(page);
680                         unlock_page(page);
681                         return PTR_ERR(new);
682         }
683         if (len < PAGE_CACHE_SIZE)
684                 memclear_highpage_flush(page, len, PAGE_CACHE_SIZE - len);
685         nfs_list_add_request(new, desc->head);
686         return 0;
687 }
688
689 int nfs_readpages(struct file *filp, struct address_space *mapping,
690                 struct list_head *pages, unsigned nr_pages)
691 {
692         LIST_HEAD(head);
693         struct nfs_readdesc desc = {
694                 .head           = &head,
695         };
696         struct inode *inode = mapping->host;
697         struct nfs_server *server = NFS_SERVER(inode);
698         int ret;
699
700         dprintk("NFS: nfs_readpages (%s/%Ld %d)\n",
701                         inode->i_sb->s_id,
702                         (long long)NFS_FILEID(inode),
703                         nr_pages);
704         nfs_inc_stats(inode, NFSIOS_VFSREADPAGES);
705
706         if (filp == NULL) {
707                 desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
708                 if (desc.ctx == NULL)
709                         return -EBADF;
710         } else
711                 desc.ctx = get_nfs_open_context((struct nfs_open_context *)
712                                 filp->private_data);
713
714         /* attempt to read as many of the pages as possible from the cache
715          * - this returns -ENOBUFS immediately if the cookie is negative
716          */
717         ret = nfs_readpages_from_fscache(desc.ctx, inode, mapping,
718                                          pages, &nr_pages);
719         if (ret == 0) {
720                 put_nfs_open_context(desc.ctx);
721                 return ret; /* all read */
722         }
723
724         ret = read_cache_pages(mapping, pages, readpage_async_filler, &desc);
725         if (!list_empty(&head)) {
726                 int err = nfs_pagein_list(&head, server->rpages);
727                 if (!ret)
728                         nfs_add_stats(inode, NFSIOS_READPAGES, err);
729                         ret = err;
730         }
731         put_nfs_open_context(desc.ctx);
732         return ret;
733 }
734
735 int __init nfs_init_readpagecache(void)
736 {
737         nfs_rdata_cachep = kmem_cache_create("nfs_read_data",
738                                              sizeof(struct nfs_read_data),
739                                              0, SLAB_HWCACHE_ALIGN,
740                                              NULL, NULL);
741         if (nfs_rdata_cachep == NULL)
742                 return -ENOMEM;
743
744         nfs_rdata_mempool = mempool_create_slab_pool(MIN_POOL_READ,
745                                                      nfs_rdata_cachep);
746         if (nfs_rdata_mempool == NULL)
747                 return -ENOMEM;
748
749         return 0;
750 }
751
752 void nfs_destroy_readpagecache(void)
753 {
754         mempool_destroy(nfs_rdata_mempool);
755         if (kmem_cache_destroy(nfs_rdata_cachep))
756                 printk(KERN_INFO "nfs_read_data: not all structures were freed\n");
757 }