087f3b734f407a5d072b72cc375eb722d351a9ae
[platform/adaptation/renesas_rcar/renesas_kernel.git] / fs / fuse / file.c
1 /*
2   FUSE: Filesystem in Userspace
3   Copyright (C) 2001-2006  Miklos Szeredi <miklos@szeredi.hu>
4
5   This program can be distributed under the terms of the GNU GPL.
6   See the file COPYING.
7 */
8
9 #include "fuse_i.h"
10
11 #include <linux/pagemap.h>
12 #include <linux/slab.h>
13 #include <linux/kernel.h>
14
15 static const struct file_operations fuse_direct_io_file_operations;
16
17 static int fuse_send_open(struct inode *inode, struct file *file, int isdir,
18                           struct fuse_open_out *outargp)
19 {
20         struct fuse_conn *fc = get_fuse_conn(inode);
21         struct fuse_open_in inarg;
22         struct fuse_req *req;
23         int err;
24
25         req = fuse_get_req(fc);
26         if (IS_ERR(req))
27                 return PTR_ERR(req);
28
29         memset(&inarg, 0, sizeof(inarg));
30         inarg.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
31         req->in.h.opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN;
32         req->in.h.nodeid = get_node_id(inode);
33         req->inode = inode;
34         req->in.numargs = 1;
35         req->in.args[0].size = sizeof(inarg);
36         req->in.args[0].value = &inarg;
37         req->out.numargs = 1;
38         req->out.args[0].size = sizeof(*outargp);
39         req->out.args[0].value = outargp;
40         request_send(fc, req);
41         err = req->out.h.error;
42         fuse_put_request(fc, req);
43
44         return err;
45 }
46
47 struct fuse_file *fuse_file_alloc(void)
48 {
49         struct fuse_file *ff;
50         ff = kmalloc(sizeof(struct fuse_file), GFP_KERNEL);
51         if (ff) {
52                 ff->release_req = fuse_request_alloc();
53                 if (!ff->release_req) {
54                         kfree(ff);
55                         ff = NULL;
56                 }
57         }
58         return ff;
59 }
60
61 void fuse_file_free(struct fuse_file *ff)
62 {
63         fuse_request_free(ff->release_req);
64         kfree(ff);
65 }
66
67 void fuse_finish_open(struct inode *inode, struct file *file,
68                       struct fuse_file *ff, struct fuse_open_out *outarg)
69 {
70         if (outarg->open_flags & FOPEN_DIRECT_IO)
71                 file->f_op = &fuse_direct_io_file_operations;
72         if (!(outarg->open_flags & FOPEN_KEEP_CACHE))
73                 invalidate_inode_pages(inode->i_mapping);
74         ff->fh = outarg->fh;
75         file->private_data = ff;
76 }
77
78 int fuse_open_common(struct inode *inode, struct file *file, int isdir)
79 {
80         struct fuse_open_out outarg;
81         struct fuse_file *ff;
82         int err;
83
84         /* VFS checks this, but only _after_ ->open() */
85         if (file->f_flags & O_DIRECT)
86                 return -EINVAL;
87
88         err = generic_file_open(inode, file);
89         if (err)
90                 return err;
91
92         /* If opening the root node, no lookup has been performed on
93            it, so the attributes must be refreshed */
94         if (get_node_id(inode) == FUSE_ROOT_ID) {
95                 err = fuse_do_getattr(inode);
96                 if (err)
97                         return err;
98         }
99
100         ff = fuse_file_alloc();
101         if (!ff)
102                 return -ENOMEM;
103
104         err = fuse_send_open(inode, file, isdir, &outarg);
105         if (err)
106                 fuse_file_free(ff);
107         else {
108                 if (isdir)
109                         outarg.open_flags &= ~FOPEN_DIRECT_IO;
110                 fuse_finish_open(inode, file, ff, &outarg);
111         }
112
113         return err;
114 }
115
116 /* Special case for failed iget in CREATE */
117 static void fuse_release_end(struct fuse_conn *fc, struct fuse_req *req)
118 {
119         /* If called from end_io_requests(), req has more than one
120            reference and fuse_reset_request() cannot work */
121         if (fc->connected) {
122                 u64 nodeid = req->in.h.nodeid;
123                 fuse_reset_request(req);
124                 fuse_send_forget(fc, req, nodeid, 1);
125         } else
126                 fuse_put_request(fc, req);
127 }
128
129 void fuse_send_release(struct fuse_conn *fc, struct fuse_file *ff,
130                        u64 nodeid, struct inode *inode, int flags, int isdir)
131 {
132         struct fuse_req * req = ff->release_req;
133         struct fuse_release_in *inarg = &req->misc.release_in;
134
135         inarg->fh = ff->fh;
136         inarg->flags = flags;
137         req->in.h.opcode = isdir ? FUSE_RELEASEDIR : FUSE_RELEASE;
138         req->in.h.nodeid = nodeid;
139         req->inode = inode;
140         req->in.numargs = 1;
141         req->in.args[0].size = sizeof(struct fuse_release_in);
142         req->in.args[0].value = inarg;
143         request_send_background(fc, req);
144         if (!inode)
145                 req->end = fuse_release_end;
146         kfree(ff);
147 }
148
149 int fuse_release_common(struct inode *inode, struct file *file, int isdir)
150 {
151         struct fuse_file *ff = file->private_data;
152         if (ff) {
153                 struct fuse_conn *fc = get_fuse_conn(inode);
154                 u64 nodeid = get_node_id(inode);
155                 fuse_send_release(fc, ff, nodeid, inode, file->f_flags, isdir);
156         }
157
158         /* Return value is ignored by VFS */
159         return 0;
160 }
161
162 static int fuse_open(struct inode *inode, struct file *file)
163 {
164         return fuse_open_common(inode, file, 0);
165 }
166
167 static int fuse_release(struct inode *inode, struct file *file)
168 {
169         return fuse_release_common(inode, file, 0);
170 }
171
172 static int fuse_flush(struct file *file, fl_owner_t id)
173 {
174         struct inode *inode = file->f_dentry->d_inode;
175         struct fuse_conn *fc = get_fuse_conn(inode);
176         struct fuse_file *ff = file->private_data;
177         struct fuse_req *req;
178         struct fuse_flush_in inarg;
179         int err;
180
181         if (is_bad_inode(inode))
182                 return -EIO;
183
184         if (fc->no_flush)
185                 return 0;
186
187         req = fuse_get_req(fc);
188         if (IS_ERR(req))
189                 return PTR_ERR(req);
190
191         memset(&inarg, 0, sizeof(inarg));
192         inarg.fh = ff->fh;
193         req->in.h.opcode = FUSE_FLUSH;
194         req->in.h.nodeid = get_node_id(inode);
195         req->inode = inode;
196         req->file = file;
197         req->in.numargs = 1;
198         req->in.args[0].size = sizeof(inarg);
199         req->in.args[0].value = &inarg;
200         request_send(fc, req);
201         err = req->out.h.error;
202         fuse_put_request(fc, req);
203         if (err == -ENOSYS) {
204                 fc->no_flush = 1;
205                 err = 0;
206         }
207         return err;
208 }
209
210 int fuse_fsync_common(struct file *file, struct dentry *de, int datasync,
211                       int isdir)
212 {
213         struct inode *inode = de->d_inode;
214         struct fuse_conn *fc = get_fuse_conn(inode);
215         struct fuse_file *ff = file->private_data;
216         struct fuse_req *req;
217         struct fuse_fsync_in inarg;
218         int err;
219
220         if (is_bad_inode(inode))
221                 return -EIO;
222
223         if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir))
224                 return 0;
225
226         req = fuse_get_req(fc);
227         if (IS_ERR(req))
228                 return PTR_ERR(req);
229
230         memset(&inarg, 0, sizeof(inarg));
231         inarg.fh = ff->fh;
232         inarg.fsync_flags = datasync ? 1 : 0;
233         req->in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC;
234         req->in.h.nodeid = get_node_id(inode);
235         req->inode = inode;
236         req->file = file;
237         req->in.numargs = 1;
238         req->in.args[0].size = sizeof(inarg);
239         req->in.args[0].value = &inarg;
240         request_send(fc, req);
241         err = req->out.h.error;
242         fuse_put_request(fc, req);
243         if (err == -ENOSYS) {
244                 if (isdir)
245                         fc->no_fsyncdir = 1;
246                 else
247                         fc->no_fsync = 1;
248                 err = 0;
249         }
250         return err;
251 }
252
253 static int fuse_fsync(struct file *file, struct dentry *de, int datasync)
254 {
255         return fuse_fsync_common(file, de, datasync, 0);
256 }
257
258 void fuse_read_fill(struct fuse_req *req, struct file *file,
259                     struct inode *inode, loff_t pos, size_t count, int opcode)
260 {
261         struct fuse_file *ff = file->private_data;
262         struct fuse_read_in *inarg = &req->misc.read_in;
263
264         inarg->fh = ff->fh;
265         inarg->offset = pos;
266         inarg->size = count;
267         req->in.h.opcode = opcode;
268         req->in.h.nodeid = get_node_id(inode);
269         req->inode = inode;
270         req->file = file;
271         req->in.numargs = 1;
272         req->in.args[0].size = sizeof(struct fuse_read_in);
273         req->in.args[0].value = inarg;
274         req->out.argpages = 1;
275         req->out.argvar = 1;
276         req->out.numargs = 1;
277         req->out.args[0].size = count;
278 }
279
280 static size_t fuse_send_read(struct fuse_req *req, struct file *file,
281                              struct inode *inode, loff_t pos, size_t count)
282 {
283         struct fuse_conn *fc = get_fuse_conn(inode);
284         fuse_read_fill(req, file, inode, pos, count, FUSE_READ);
285         request_send(fc, req);
286         return req->out.args[0].size;
287 }
288
289 static int fuse_readpage(struct file *file, struct page *page)
290 {
291         struct inode *inode = page->mapping->host;
292         struct fuse_conn *fc = get_fuse_conn(inode);
293         struct fuse_req *req;
294         int err;
295
296         err = -EIO;
297         if (is_bad_inode(inode))
298                 goto out;
299
300         req = fuse_get_req(fc);
301         err = PTR_ERR(req);
302         if (IS_ERR(req))
303                 goto out;
304
305         req->out.page_zeroing = 1;
306         req->num_pages = 1;
307         req->pages[0] = page;
308         fuse_send_read(req, file, inode, page_offset(page), PAGE_CACHE_SIZE);
309         err = req->out.h.error;
310         fuse_put_request(fc, req);
311         if (!err)
312                 SetPageUptodate(page);
313         fuse_invalidate_attr(inode); /* atime changed */
314  out:
315         unlock_page(page);
316         return err;
317 }
318
319 static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
320 {
321         int i;
322
323         fuse_invalidate_attr(req->pages[0]->mapping->host); /* atime changed */
324
325         for (i = 0; i < req->num_pages; i++) {
326                 struct page *page = req->pages[i];
327                 if (!req->out.h.error)
328                         SetPageUptodate(page);
329                 else
330                         SetPageError(page);
331                 unlock_page(page);
332         }
333         fuse_put_request(fc, req);
334 }
335
336 static void fuse_send_readpages(struct fuse_req *req, struct file *file,
337                                 struct inode *inode)
338 {
339         struct fuse_conn *fc = get_fuse_conn(inode);
340         loff_t pos = page_offset(req->pages[0]);
341         size_t count = req->num_pages << PAGE_CACHE_SHIFT;
342         req->out.page_zeroing = 1;
343         fuse_read_fill(req, file, inode, pos, count, FUSE_READ);
344         if (fc->async_read) {
345                 req->end = fuse_readpages_end;
346                 request_send_background(fc, req);
347         } else {
348                 request_send(fc, req);
349                 fuse_readpages_end(fc, req);
350         }
351 }
352
353 struct fuse_readpages_data {
354         struct fuse_req *req;
355         struct file *file;
356         struct inode *inode;
357 };
358
359 static int fuse_readpages_fill(void *_data, struct page *page)
360 {
361         struct fuse_readpages_data *data = _data;
362         struct fuse_req *req = data->req;
363         struct inode *inode = data->inode;
364         struct fuse_conn *fc = get_fuse_conn(inode);
365
366         if (req->num_pages &&
367             (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
368              (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read ||
369              req->pages[req->num_pages - 1]->index + 1 != page->index)) {
370                 fuse_send_readpages(req, data->file, inode);
371                 data->req = req = fuse_get_req(fc);
372                 if (IS_ERR(req)) {
373                         unlock_page(page);
374                         return PTR_ERR(req);
375                 }
376         }
377         req->pages[req->num_pages] = page;
378         req->num_pages ++;
379         return 0;
380 }
381
382 static int fuse_readpages(struct file *file, struct address_space *mapping,
383                           struct list_head *pages, unsigned nr_pages)
384 {
385         struct inode *inode = mapping->host;
386         struct fuse_conn *fc = get_fuse_conn(inode);
387         struct fuse_readpages_data data;
388         int err;
389
390         if (is_bad_inode(inode))
391                 return -EIO;
392
393         data.file = file;
394         data.inode = inode;
395         data.req = fuse_get_req(fc);
396         if (IS_ERR(data.req))
397                 return PTR_ERR(data.req);
398
399         err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
400         if (!err) {
401                 if (data.req->num_pages)
402                         fuse_send_readpages(data.req, file, inode);
403                 else
404                         fuse_put_request(fc, data.req);
405         }
406         return err;
407 }
408
409 static size_t fuse_send_write(struct fuse_req *req, struct file *file,
410                               struct inode *inode, loff_t pos, size_t count)
411 {
412         struct fuse_conn *fc = get_fuse_conn(inode);
413         struct fuse_file *ff = file->private_data;
414         struct fuse_write_in inarg;
415         struct fuse_write_out outarg;
416
417         memset(&inarg, 0, sizeof(struct fuse_write_in));
418         inarg.fh = ff->fh;
419         inarg.offset = pos;
420         inarg.size = count;
421         req->in.h.opcode = FUSE_WRITE;
422         req->in.h.nodeid = get_node_id(inode);
423         req->inode = inode;
424         req->file = file;
425         req->in.argpages = 1;
426         req->in.numargs = 2;
427         req->in.args[0].size = sizeof(struct fuse_write_in);
428         req->in.args[0].value = &inarg;
429         req->in.args[1].size = count;
430         req->out.numargs = 1;
431         req->out.args[0].size = sizeof(struct fuse_write_out);
432         req->out.args[0].value = &outarg;
433         request_send(fc, req);
434         return outarg.size;
435 }
436
437 static int fuse_prepare_write(struct file *file, struct page *page,
438                               unsigned offset, unsigned to)
439 {
440         /* No op */
441         return 0;
442 }
443
444 static int fuse_commit_write(struct file *file, struct page *page,
445                              unsigned offset, unsigned to)
446 {
447         int err;
448         size_t nres;
449         unsigned count = to - offset;
450         struct inode *inode = page->mapping->host;
451         struct fuse_conn *fc = get_fuse_conn(inode);
452         loff_t pos = page_offset(page) + offset;
453         struct fuse_req *req;
454
455         if (is_bad_inode(inode))
456                 return -EIO;
457
458         req = fuse_get_req(fc);
459         if (IS_ERR(req))
460                 return PTR_ERR(req);
461
462         req->num_pages = 1;
463         req->pages[0] = page;
464         req->page_offset = offset;
465         nres = fuse_send_write(req, file, inode, pos, count);
466         err = req->out.h.error;
467         fuse_put_request(fc, req);
468         if (!err && nres != count)
469                 err = -EIO;
470         if (!err) {
471                 pos += count;
472                 if (pos > i_size_read(inode))
473                         i_size_write(inode, pos);
474
475                 if (offset == 0 && to == PAGE_CACHE_SIZE) {
476                         clear_page_dirty(page);
477                         SetPageUptodate(page);
478                 }
479         }
480         fuse_invalidate_attr(inode);
481         return err;
482 }
483
484 static void fuse_release_user_pages(struct fuse_req *req, int write)
485 {
486         unsigned i;
487
488         for (i = 0; i < req->num_pages; i++) {
489                 struct page *page = req->pages[i];
490                 if (write)
491                         set_page_dirty_lock(page);
492                 put_page(page);
493         }
494 }
495
496 static int fuse_get_user_pages(struct fuse_req *req, const char __user *buf,
497                                unsigned nbytes, int write)
498 {
499         unsigned long user_addr = (unsigned long) buf;
500         unsigned offset = user_addr & ~PAGE_MASK;
501         int npages;
502
503         /* This doesn't work with nfsd */
504         if (!current->mm)
505                 return -EPERM;
506
507         nbytes = min(nbytes, (unsigned) FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT);
508         npages = (nbytes + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
509         npages = min(max(npages, 1), FUSE_MAX_PAGES_PER_REQ);
510         down_read(&current->mm->mmap_sem);
511         npages = get_user_pages(current, current->mm, user_addr, npages, write,
512                                 0, req->pages, NULL);
513         up_read(&current->mm->mmap_sem);
514         if (npages < 0)
515                 return npages;
516
517         req->num_pages = npages;
518         req->page_offset = offset;
519         return 0;
520 }
521
522 static ssize_t fuse_direct_io(struct file *file, const char __user *buf,
523                               size_t count, loff_t *ppos, int write)
524 {
525         struct inode *inode = file->f_dentry->d_inode;
526         struct fuse_conn *fc = get_fuse_conn(inode);
527         size_t nmax = write ? fc->max_write : fc->max_read;
528         loff_t pos = *ppos;
529         ssize_t res = 0;
530         struct fuse_req *req;
531
532         if (is_bad_inode(inode))
533                 return -EIO;
534
535         req = fuse_get_req(fc);
536         if (IS_ERR(req))
537                 return PTR_ERR(req);
538
539         while (count) {
540                 size_t nres;
541                 size_t nbytes = min(count, nmax);
542                 int err = fuse_get_user_pages(req, buf, nbytes, !write);
543                 if (err) {
544                         res = err;
545                         break;
546                 }
547                 nbytes = (req->num_pages << PAGE_SHIFT) - req->page_offset;
548                 nbytes = min(count, nbytes);
549                 if (write)
550                         nres = fuse_send_write(req, file, inode, pos, nbytes);
551                 else
552                         nres = fuse_send_read(req, file, inode, pos, nbytes);
553                 fuse_release_user_pages(req, !write);
554                 if (req->out.h.error) {
555                         if (!res)
556                                 res = req->out.h.error;
557                         break;
558                 } else if (nres > nbytes) {
559                         res = -EIO;
560                         break;
561                 }
562                 count -= nres;
563                 res += nres;
564                 pos += nres;
565                 buf += nres;
566                 if (nres != nbytes)
567                         break;
568                 if (count) {
569                         fuse_put_request(fc, req);
570                         req = fuse_get_req(fc);
571                         if (IS_ERR(req))
572                                 break;
573                 }
574         }
575         fuse_put_request(fc, req);
576         if (res > 0) {
577                 if (write && pos > i_size_read(inode))
578                         i_size_write(inode, pos);
579                 *ppos = pos;
580         }
581         fuse_invalidate_attr(inode);
582
583         return res;
584 }
585
586 static ssize_t fuse_direct_read(struct file *file, char __user *buf,
587                                      size_t count, loff_t *ppos)
588 {
589         return fuse_direct_io(file, buf, count, ppos, 0);
590 }
591
592 static ssize_t fuse_direct_write(struct file *file, const char __user *buf,
593                                  size_t count, loff_t *ppos)
594 {
595         struct inode *inode = file->f_dentry->d_inode;
596         ssize_t res;
597         /* Don't allow parallel writes to the same file */
598         mutex_lock(&inode->i_mutex);
599         res = fuse_direct_io(file, buf, count, ppos, 1);
600         mutex_unlock(&inode->i_mutex);
601         return res;
602 }
603
604 static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
605 {
606         if ((vma->vm_flags & VM_SHARED)) {
607                 if ((vma->vm_flags & VM_WRITE))
608                         return -ENODEV;
609                 else
610                         vma->vm_flags &= ~VM_MAYWRITE;
611         }
612         return generic_file_mmap(file, vma);
613 }
614
615 static int fuse_set_page_dirty(struct page *page)
616 {
617         printk("fuse_set_page_dirty: should not happen\n");
618         dump_stack();
619         return 0;
620 }
621
622 static const struct file_operations fuse_file_operations = {
623         .llseek         = generic_file_llseek,
624         .read           = generic_file_read,
625         .write          = generic_file_write,
626         .mmap           = fuse_file_mmap,
627         .open           = fuse_open,
628         .flush          = fuse_flush,
629         .release        = fuse_release,
630         .fsync          = fuse_fsync,
631         .sendfile       = generic_file_sendfile,
632 };
633
634 static const struct file_operations fuse_direct_io_file_operations = {
635         .llseek         = generic_file_llseek,
636         .read           = fuse_direct_read,
637         .write          = fuse_direct_write,
638         .open           = fuse_open,
639         .flush          = fuse_flush,
640         .release        = fuse_release,
641         .fsync          = fuse_fsync,
642         /* no mmap and sendfile */
643 };
644
645 static struct address_space_operations fuse_file_aops  = {
646         .readpage       = fuse_readpage,
647         .prepare_write  = fuse_prepare_write,
648         .commit_write   = fuse_commit_write,
649         .readpages      = fuse_readpages,
650         .set_page_dirty = fuse_set_page_dirty,
651 };
652
653 void fuse_init_file_inode(struct inode *inode)
654 {
655         inode->i_fop = &fuse_file_operations;
656         inode->i_data.a_ops = &fuse_file_aops;
657 }