]> git.karo-electronics.de Git - karo-tx-linux.git/blob - fs/nfs/read.c
NFS: Create a common initiate_pgio() function
[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 int nfs_do_read(struct nfs_pgio_data *data,
165                 const struct rpc_call_ops *call_ops)
166 {
167         struct inode *inode = data->header->inode;
168
169         return nfs_initiate_pgio(NFS_CLIENT(inode), data, call_ops, 0, 0);
170 }
171
172 static int
173 nfs_do_multiple_reads(struct list_head *head,
174                 const struct rpc_call_ops *call_ops)
175 {
176         struct nfs_pgio_data *data;
177         int ret = 0;
178
179         while (!list_empty(head)) {
180                 int ret2;
181
182                 data = list_first_entry(head, struct nfs_pgio_data, list);
183                 list_del_init(&data->list);
184
185                 ret2 = nfs_do_read(data, call_ops);
186                 if (ret == 0)
187                         ret = ret2;
188         }
189         return ret;
190 }
191
192 static void
193 nfs_async_read_error(struct list_head *head)
194 {
195         struct nfs_page *req;
196
197         while (!list_empty(head)) {
198                 req = nfs_list_entry(head->next);
199                 nfs_list_remove_request(req);
200                 nfs_readpage_release(req);
201         }
202 }
203
204 static const struct nfs_pgio_completion_ops nfs_async_read_completion_ops = {
205         .error_cleanup = nfs_async_read_error,
206         .completion = nfs_read_completion,
207 };
208
209 static int nfs_generic_pg_readpages(struct nfs_pageio_descriptor *desc)
210 {
211         struct nfs_rw_header *rhdr;
212         struct nfs_pgio_header *hdr;
213         int ret;
214
215         rhdr = nfs_rw_header_alloc(desc->pg_rw_ops);
216         if (!rhdr) {
217                 desc->pg_completion_ops->error_cleanup(&desc->pg_list);
218                 return -ENOMEM;
219         }
220         hdr = &rhdr->header;
221         nfs_pgheader_init(desc, hdr, nfs_rw_header_free);
222         atomic_inc(&hdr->refcnt);
223         ret = nfs_generic_pgio(desc, hdr);
224         if (ret == 0)
225                 ret = nfs_do_multiple_reads(&hdr->rpc_list,
226                                             desc->pg_rpc_callops);
227         if (atomic_dec_and_test(&hdr->refcnt))
228                 hdr->completion_ops->completion(hdr);
229         return ret;
230 }
231
232 static const struct nfs_pageio_ops nfs_pageio_read_ops = {
233         .pg_test = nfs_generic_pg_test,
234         .pg_doio = nfs_generic_pg_readpages,
235 };
236
237 /*
238  * This is the callback from RPC telling us whether a reply was
239  * received or some error occurred (timeout or socket shutdown).
240  */
241 static int nfs_readpage_done(struct rpc_task *task, struct nfs_pgio_data *data,
242                              struct inode *inode)
243 {
244         int status = NFS_PROTO(inode)->read_done(task, data);
245         if (status != 0)
246                 return status;
247
248         nfs_add_stats(inode, NFSIOS_SERVERREADBYTES, data->res.count);
249
250         if (task->tk_status == -ESTALE) {
251                 set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
252                 nfs_mark_for_revalidate(inode);
253         }
254         return 0;
255 }
256
257 static void nfs_readpage_retry(struct rpc_task *task, struct nfs_pgio_data *data)
258 {
259         struct nfs_pgio_args *argp = &data->args;
260         struct nfs_pgio_res  *resp = &data->res;
261
262         /* This is a short read! */
263         nfs_inc_stats(data->header->inode, NFSIOS_SHORTREAD);
264         /* Has the server at least made some progress? */
265         if (resp->count == 0) {
266                 nfs_set_pgio_error(data->header, -EIO, argp->offset);
267                 return;
268         }
269         /* Yes, so retry the read at the end of the data */
270         data->mds_offset += resp->count;
271         argp->offset += resp->count;
272         argp->pgbase += resp->count;
273         argp->count -= resp->count;
274         rpc_restart_call_prepare(task);
275 }
276
277 static void nfs_readpage_result(struct rpc_task *task, struct nfs_pgio_data *data)
278 {
279         struct nfs_pgio_header *hdr = data->header;
280
281         if (data->res.eof) {
282                 loff_t bound;
283
284                 bound = data->args.offset + data->res.count;
285                 spin_lock(&hdr->lock);
286                 if (bound < hdr->io_start + hdr->good_bytes) {
287                         set_bit(NFS_IOHDR_EOF, &hdr->flags);
288                         clear_bit(NFS_IOHDR_ERROR, &hdr->flags);
289                         hdr->good_bytes = bound - hdr->io_start;
290                 }
291                 spin_unlock(&hdr->lock);
292         } else if (data->res.count != data->args.count)
293                 nfs_readpage_retry(task, data);
294 }
295
296 /*
297  * Read a page over NFS.
298  * We read the page synchronously in the following case:
299  *  -   The error flag is set for this page. This happens only when a
300  *      previous async read operation failed.
301  */
302 int nfs_readpage(struct file *file, struct page *page)
303 {
304         struct nfs_open_context *ctx;
305         struct inode *inode = page_file_mapping(page)->host;
306         int             error;
307
308         dprintk("NFS: nfs_readpage (%p %ld@%lu)\n",
309                 page, PAGE_CACHE_SIZE, page_file_index(page));
310         nfs_inc_stats(inode, NFSIOS_VFSREADPAGE);
311         nfs_add_stats(inode, NFSIOS_READPAGES, 1);
312
313         /*
314          * Try to flush any pending writes to the file..
315          *
316          * NOTE! Because we own the page lock, there cannot
317          * be any new pending writes generated at this point
318          * for this page (other pages can be written to).
319          */
320         error = nfs_wb_page(inode, page);
321         if (error)
322                 goto out_unlock;
323         if (PageUptodate(page))
324                 goto out_unlock;
325
326         error = -ESTALE;
327         if (NFS_STALE(inode))
328                 goto out_unlock;
329
330         if (file == NULL) {
331                 error = -EBADF;
332                 ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
333                 if (ctx == NULL)
334                         goto out_unlock;
335         } else
336                 ctx = get_nfs_open_context(nfs_file_open_context(file));
337
338         if (!IS_SYNC(inode)) {
339                 error = nfs_readpage_from_fscache(ctx, inode, page);
340                 if (error == 0)
341                         goto out;
342         }
343
344         error = nfs_readpage_async(ctx, inode, page);
345
346 out:
347         put_nfs_open_context(ctx);
348         return error;
349 out_unlock:
350         unlock_page(page);
351         return error;
352 }
353
354 struct nfs_readdesc {
355         struct nfs_pageio_descriptor *pgio;
356         struct nfs_open_context *ctx;
357 };
358
359 static int
360 readpage_async_filler(void *data, struct page *page)
361 {
362         struct nfs_readdesc *desc = (struct nfs_readdesc *)data;
363         struct inode *inode = page_file_mapping(page)->host;
364         struct nfs_page *new;
365         unsigned int len;
366         int error;
367
368         len = nfs_page_length(page);
369         if (len == 0)
370                 return nfs_return_empty_page(page);
371
372         new = nfs_create_request(desc->ctx, inode, page, 0, len);
373         if (IS_ERR(new))
374                 goto out_error;
375
376         if (len < PAGE_CACHE_SIZE)
377                 zero_user_segment(page, len, PAGE_CACHE_SIZE);
378         if (!nfs_pageio_add_request(desc->pgio, new)) {
379                 error = desc->pgio->pg_error;
380                 goto out_unlock;
381         }
382         return 0;
383 out_error:
384         error = PTR_ERR(new);
385 out_unlock:
386         unlock_page(page);
387         return error;
388 }
389
390 int nfs_readpages(struct file *filp, struct address_space *mapping,
391                 struct list_head *pages, unsigned nr_pages)
392 {
393         struct nfs_pageio_descriptor pgio;
394         struct nfs_readdesc desc = {
395                 .pgio = &pgio,
396         };
397         struct inode *inode = mapping->host;
398         unsigned long npages;
399         int ret = -ESTALE;
400
401         dprintk("NFS: nfs_readpages (%s/%Lu %d)\n",
402                         inode->i_sb->s_id,
403                         (unsigned long long)NFS_FILEID(inode),
404                         nr_pages);
405         nfs_inc_stats(inode, NFSIOS_VFSREADPAGES);
406
407         if (NFS_STALE(inode))
408                 goto out;
409
410         if (filp == NULL) {
411                 desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
412                 if (desc.ctx == NULL)
413                         return -EBADF;
414         } else
415                 desc.ctx = get_nfs_open_context(nfs_file_open_context(filp));
416
417         /* attempt to read as many of the pages as possible from the cache
418          * - this returns -ENOBUFS immediately if the cookie is negative
419          */
420         ret = nfs_readpages_from_fscache(desc.ctx, inode, mapping,
421                                          pages, &nr_pages);
422         if (ret == 0)
423                 goto read_complete; /* all pages were read */
424
425         nfs_pageio_init_read(&pgio, inode, false,
426                              &nfs_async_read_completion_ops);
427
428         ret = read_cache_pages(mapping, pages, readpage_async_filler, &desc);
429
430         nfs_pageio_complete(&pgio);
431         NFS_I(inode)->read_io += pgio.pg_bytes_written;
432         npages = (pgio.pg_bytes_written + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
433         nfs_add_stats(inode, NFSIOS_READPAGES, npages);
434 read_complete:
435         put_nfs_open_context(desc.ctx);
436 out:
437         return ret;
438 }
439
440 int __init nfs_init_readpagecache(void)
441 {
442         nfs_rdata_cachep = kmem_cache_create("nfs_read_data",
443                                              sizeof(struct nfs_rw_header),
444                                              0, SLAB_HWCACHE_ALIGN,
445                                              NULL);
446         if (nfs_rdata_cachep == NULL)
447                 return -ENOMEM;
448
449         return 0;
450 }
451
452 void nfs_destroy_readpagecache(void)
453 {
454         kmem_cache_destroy(nfs_rdata_cachep);
455 }
456
457 static const struct nfs_rw_ops nfs_rw_read_ops = {
458         .rw_mode                = FMODE_READ,
459         .rw_alloc_header        = nfs_readhdr_alloc,
460         .rw_free_header         = nfs_readhdr_free,
461         .rw_done                = nfs_readpage_done,
462         .rw_result              = nfs_readpage_result,
463         .rw_initiate            = nfs_initiate_read,
464 };