]> git.karo-electronics.de Git - karo-tx-linux.git/blob - fs/nfs/read.c
NFS: Create a common generic_pg_pgios()
[karo-tx-linux.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
10 #include <linux/time.h>
11 #include <linux/kernel.h>
12 #include <linux/errno.h>
13 #include <linux/fcntl.h>
14 #include <linux/stat.h>
15 #include <linux/mm.h>
16 #include <linux/slab.h>
17 #include <linux/pagemap.h>
18 #include <linux/sunrpc/clnt.h>
19 #include <linux/nfs_fs.h>
20 #include <linux/nfs_page.h>
21 #include <linux/module.h>
22
23 #include "nfs4_fs.h"
24 #include "internal.h"
25 #include "iostat.h"
26 #include "fscache.h"
27 #include "pnfs.h"
28
29 #define NFSDBG_FACILITY         NFSDBG_PAGECACHE
30
31 static const struct nfs_pageio_ops nfs_pageio_read_ops;
32 static const struct nfs_pgio_completion_ops nfs_async_read_completion_ops;
33 static const struct nfs_rw_ops nfs_rw_read_ops;
34
35 static struct kmem_cache *nfs_rdata_cachep;
36
37 static struct nfs_rw_header *nfs_readhdr_alloc(void)
38 {
39         return kmem_cache_zalloc(nfs_rdata_cachep, GFP_KERNEL);
40 }
41
42 static void nfs_readhdr_free(struct nfs_rw_header *rhdr)
43 {
44         kmem_cache_free(nfs_rdata_cachep, rhdr);
45 }
46
47 static
48 int nfs_return_empty_page(struct page *page)
49 {
50         zero_user(page, 0, PAGE_CACHE_SIZE);
51         SetPageUptodate(page);
52         unlock_page(page);
53         return 0;
54 }
55
56 void nfs_pageio_init_read(struct nfs_pageio_descriptor *pgio,
57                               struct inode *inode, bool force_mds,
58                               const struct nfs_pgio_completion_ops *compl_ops)
59 {
60         struct nfs_server *server = NFS_SERVER(inode);
61         const struct nfs_pageio_ops *pg_ops = &nfs_pageio_read_ops;
62
63 #ifdef CONFIG_NFS_V4_1
64         if (server->pnfs_curr_ld && !force_mds)
65                 pg_ops = server->pnfs_curr_ld->pg_read_ops;
66 #endif
67         nfs_pageio_init(pgio, inode, pg_ops, compl_ops, &nfs_rw_read_ops,
68                         server->rsize, 0);
69 }
70 EXPORT_SYMBOL_GPL(nfs_pageio_init_read);
71
72 void nfs_pageio_reset_read_mds(struct nfs_pageio_descriptor *pgio)
73 {
74         pgio->pg_ops = &nfs_pageio_read_ops;
75         pgio->pg_bsize = NFS_SERVER(pgio->pg_inode)->rsize;
76 }
77 EXPORT_SYMBOL_GPL(nfs_pageio_reset_read_mds);
78
79 int nfs_readpage_async(struct nfs_open_context *ctx, struct inode *inode,
80                        struct page *page)
81 {
82         struct nfs_page *new;
83         unsigned int len;
84         struct nfs_pageio_descriptor pgio;
85
86         len = nfs_page_length(page);
87         if (len == 0)
88                 return nfs_return_empty_page(page);
89         new = nfs_create_request(ctx, inode, page, 0, len);
90         if (IS_ERR(new)) {
91                 unlock_page(page);
92                 return PTR_ERR(new);
93         }
94         if (len < PAGE_CACHE_SIZE)
95                 zero_user_segment(page, len, PAGE_CACHE_SIZE);
96
97         nfs_pageio_init_read(&pgio, inode, false,
98                              &nfs_async_read_completion_ops);
99         nfs_pageio_add_request(&pgio, new);
100         nfs_pageio_complete(&pgio);
101         NFS_I(inode)->read_io += pgio.pg_bytes_written;
102         return 0;
103 }
104
105 static void nfs_readpage_release(struct nfs_page *req)
106 {
107         struct inode *d_inode = req->wb_context->dentry->d_inode;
108
109         if (PageUptodate(req->wb_page))
110                 nfs_readpage_to_fscache(d_inode, req->wb_page, 0);
111
112         unlock_page(req->wb_page);
113
114         dprintk("NFS: read done (%s/%Lu %d@%Ld)\n",
115                         req->wb_context->dentry->d_inode->i_sb->s_id,
116                         (unsigned long long)NFS_FILEID(req->wb_context->dentry->d_inode),
117                         req->wb_bytes,
118                         (long long)req_offset(req));
119         nfs_release_request(req);
120 }
121
122 /* Note io was page aligned */
123 static void nfs_read_completion(struct nfs_pgio_header *hdr)
124 {
125         unsigned long bytes = 0;
126
127         if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
128                 goto out;
129         while (!list_empty(&hdr->pages)) {
130                 struct nfs_page *req = nfs_list_entry(hdr->pages.next);
131                 struct page *page = req->wb_page;
132
133                 if (test_bit(NFS_IOHDR_EOF, &hdr->flags)) {
134                         if (bytes > hdr->good_bytes)
135                                 zero_user(page, 0, PAGE_SIZE);
136                         else if (hdr->good_bytes - bytes < PAGE_SIZE)
137                                 zero_user_segment(page,
138                                         hdr->good_bytes & ~PAGE_MASK,
139                                         PAGE_SIZE);
140                 }
141                 bytes += req->wb_bytes;
142                 if (test_bit(NFS_IOHDR_ERROR, &hdr->flags)) {
143                         if (bytes <= hdr->good_bytes)
144                                 SetPageUptodate(page);
145                 } else
146                         SetPageUptodate(page);
147                 nfs_list_remove_request(req);
148                 nfs_readpage_release(req);
149         }
150 out:
151         hdr->release(hdr);
152 }
153
154 static void nfs_initiate_read(struct nfs_pgio_data *data, struct rpc_message *msg,
155                               struct rpc_task_setup *task_setup_data, int how)
156 {
157         struct inode *inode = data->header->inode;
158         int swap_flags = IS_SWAPFILE(inode) ? NFS_RPC_SWAPFLAGS : 0;
159
160         task_setup_data->flags |= swap_flags;
161         NFS_PROTO(inode)->read_setup(data, msg);
162 }
163
164 static void
165 nfs_async_read_error(struct list_head *head)
166 {
167         struct nfs_page *req;
168
169         while (!list_empty(head)) {
170                 req = nfs_list_entry(head->next);
171                 nfs_list_remove_request(req);
172                 nfs_readpage_release(req);
173         }
174 }
175
176 static const struct nfs_pgio_completion_ops nfs_async_read_completion_ops = {
177         .error_cleanup = nfs_async_read_error,
178         .completion = nfs_read_completion,
179 };
180
181 static const struct nfs_pageio_ops nfs_pageio_read_ops = {
182         .pg_test = nfs_generic_pg_test,
183         .pg_doio = nfs_generic_pg_pgios,
184 };
185
186 /*
187  * This is the callback from RPC telling us whether a reply was
188  * received or some error occurred (timeout or socket shutdown).
189  */
190 static int nfs_readpage_done(struct rpc_task *task, struct nfs_pgio_data *data,
191                              struct inode *inode)
192 {
193         int status = NFS_PROTO(inode)->read_done(task, data);
194         if (status != 0)
195                 return status;
196
197         nfs_add_stats(inode, NFSIOS_SERVERREADBYTES, data->res.count);
198
199         if (task->tk_status == -ESTALE) {
200                 set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
201                 nfs_mark_for_revalidate(inode);
202         }
203         return 0;
204 }
205
206 static void nfs_readpage_retry(struct rpc_task *task, struct nfs_pgio_data *data)
207 {
208         struct nfs_pgio_args *argp = &data->args;
209         struct nfs_pgio_res  *resp = &data->res;
210
211         /* This is a short read! */
212         nfs_inc_stats(data->header->inode, NFSIOS_SHORTREAD);
213         /* Has the server at least made some progress? */
214         if (resp->count == 0) {
215                 nfs_set_pgio_error(data->header, -EIO, argp->offset);
216                 return;
217         }
218         /* Yes, so retry the read at the end of the data */
219         data->mds_offset += resp->count;
220         argp->offset += resp->count;
221         argp->pgbase += resp->count;
222         argp->count -= resp->count;
223         rpc_restart_call_prepare(task);
224 }
225
226 static void nfs_readpage_result(struct rpc_task *task, struct nfs_pgio_data *data)
227 {
228         struct nfs_pgio_header *hdr = data->header;
229
230         if (data->res.eof) {
231                 loff_t bound;
232
233                 bound = data->args.offset + data->res.count;
234                 spin_lock(&hdr->lock);
235                 if (bound < hdr->io_start + hdr->good_bytes) {
236                         set_bit(NFS_IOHDR_EOF, &hdr->flags);
237                         clear_bit(NFS_IOHDR_ERROR, &hdr->flags);
238                         hdr->good_bytes = bound - hdr->io_start;
239                 }
240                 spin_unlock(&hdr->lock);
241         } else if (data->res.count != data->args.count)
242                 nfs_readpage_retry(task, data);
243 }
244
245 /*
246  * Read a page over NFS.
247  * We read the page synchronously in the following case:
248  *  -   The error flag is set for this page. This happens only when a
249  *      previous async read operation failed.
250  */
251 int nfs_readpage(struct file *file, struct page *page)
252 {
253         struct nfs_open_context *ctx;
254         struct inode *inode = page_file_mapping(page)->host;
255         int             error;
256
257         dprintk("NFS: nfs_readpage (%p %ld@%lu)\n",
258                 page, PAGE_CACHE_SIZE, page_file_index(page));
259         nfs_inc_stats(inode, NFSIOS_VFSREADPAGE);
260         nfs_add_stats(inode, NFSIOS_READPAGES, 1);
261
262         /*
263          * Try to flush any pending writes to the file..
264          *
265          * NOTE! Because we own the page lock, there cannot
266          * be any new pending writes generated at this point
267          * for this page (other pages can be written to).
268          */
269         error = nfs_wb_page(inode, page);
270         if (error)
271                 goto out_unlock;
272         if (PageUptodate(page))
273                 goto out_unlock;
274
275         error = -ESTALE;
276         if (NFS_STALE(inode))
277                 goto out_unlock;
278
279         if (file == NULL) {
280                 error = -EBADF;
281                 ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
282                 if (ctx == NULL)
283                         goto out_unlock;
284         } else
285                 ctx = get_nfs_open_context(nfs_file_open_context(file));
286
287         if (!IS_SYNC(inode)) {
288                 error = nfs_readpage_from_fscache(ctx, inode, page);
289                 if (error == 0)
290                         goto out;
291         }
292
293         error = nfs_readpage_async(ctx, inode, page);
294
295 out:
296         put_nfs_open_context(ctx);
297         return error;
298 out_unlock:
299         unlock_page(page);
300         return error;
301 }
302
303 struct nfs_readdesc {
304         struct nfs_pageio_descriptor *pgio;
305         struct nfs_open_context *ctx;
306 };
307
308 static int
309 readpage_async_filler(void *data, struct page *page)
310 {
311         struct nfs_readdesc *desc = (struct nfs_readdesc *)data;
312         struct inode *inode = page_file_mapping(page)->host;
313         struct nfs_page *new;
314         unsigned int len;
315         int error;
316
317         len = nfs_page_length(page);
318         if (len == 0)
319                 return nfs_return_empty_page(page);
320
321         new = nfs_create_request(desc->ctx, inode, page, 0, len);
322         if (IS_ERR(new))
323                 goto out_error;
324
325         if (len < PAGE_CACHE_SIZE)
326                 zero_user_segment(page, len, PAGE_CACHE_SIZE);
327         if (!nfs_pageio_add_request(desc->pgio, new)) {
328                 error = desc->pgio->pg_error;
329                 goto out_unlock;
330         }
331         return 0;
332 out_error:
333         error = PTR_ERR(new);
334 out_unlock:
335         unlock_page(page);
336         return error;
337 }
338
339 int nfs_readpages(struct file *filp, struct address_space *mapping,
340                 struct list_head *pages, unsigned nr_pages)
341 {
342         struct nfs_pageio_descriptor pgio;
343         struct nfs_readdesc desc = {
344                 .pgio = &pgio,
345         };
346         struct inode *inode = mapping->host;
347         unsigned long npages;
348         int ret = -ESTALE;
349
350         dprintk("NFS: nfs_readpages (%s/%Lu %d)\n",
351                         inode->i_sb->s_id,
352                         (unsigned long long)NFS_FILEID(inode),
353                         nr_pages);
354         nfs_inc_stats(inode, NFSIOS_VFSREADPAGES);
355
356         if (NFS_STALE(inode))
357                 goto out;
358
359         if (filp == NULL) {
360                 desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
361                 if (desc.ctx == NULL)
362                         return -EBADF;
363         } else
364                 desc.ctx = get_nfs_open_context(nfs_file_open_context(filp));
365
366         /* attempt to read as many of the pages as possible from the cache
367          * - this returns -ENOBUFS immediately if the cookie is negative
368          */
369         ret = nfs_readpages_from_fscache(desc.ctx, inode, mapping,
370                                          pages, &nr_pages);
371         if (ret == 0)
372                 goto read_complete; /* all pages were read */
373
374         nfs_pageio_init_read(&pgio, inode, false,
375                              &nfs_async_read_completion_ops);
376
377         ret = read_cache_pages(mapping, pages, readpage_async_filler, &desc);
378
379         nfs_pageio_complete(&pgio);
380         NFS_I(inode)->read_io += pgio.pg_bytes_written;
381         npages = (pgio.pg_bytes_written + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
382         nfs_add_stats(inode, NFSIOS_READPAGES, npages);
383 read_complete:
384         put_nfs_open_context(desc.ctx);
385 out:
386         return ret;
387 }
388
389 int __init nfs_init_readpagecache(void)
390 {
391         nfs_rdata_cachep = kmem_cache_create("nfs_read_data",
392                                              sizeof(struct nfs_rw_header),
393                                              0, SLAB_HWCACHE_ALIGN,
394                                              NULL);
395         if (nfs_rdata_cachep == NULL)
396                 return -ENOMEM;
397
398         return 0;
399 }
400
401 void nfs_destroy_readpagecache(void)
402 {
403         kmem_cache_destroy(nfs_rdata_cachep);
404 }
405
406 static const struct nfs_rw_ops nfs_rw_read_ops = {
407         .rw_mode                = FMODE_READ,
408         .rw_alloc_header        = nfs_readhdr_alloc,
409         .rw_free_header         = nfs_readhdr_free,
410         .rw_done                = nfs_readpage_done,
411         .rw_result              = nfs_readpage_result,
412         .rw_initiate            = nfs_initiate_read,
413 };