Merge master.kernel.org:/pub/scm/linux/kernel/git/dtor/input
[linux-2.6] / fs / fuse / file.c
1 /*
2   FUSE: Filesystem in Userspace
3   Copyright (C) 2001-2005  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 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_request(fc);
26         if (!req)
27                 return -EINTR;
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 void fuse_send_release(struct fuse_conn *fc, struct fuse_file *ff,
117                        u64 nodeid, struct inode *inode, int flags, int isdir)
118 {
119         struct fuse_req * req = ff->release_req;
120         struct fuse_release_in *inarg = &req->misc.release_in;
121
122         inarg->fh = ff->fh;
123         inarg->flags = flags;
124         req->in.h.opcode = isdir ? FUSE_RELEASEDIR : FUSE_RELEASE;
125         req->in.h.nodeid = nodeid;
126         req->inode = inode;
127         req->in.numargs = 1;
128         req->in.args[0].size = sizeof(struct fuse_release_in);
129         req->in.args[0].value = inarg;
130         request_send_background(fc, req);
131         kfree(ff);
132 }
133
134 int fuse_release_common(struct inode *inode, struct file *file, int isdir)
135 {
136         struct fuse_file *ff = file->private_data;
137         if (ff) {
138                 struct fuse_conn *fc = get_fuse_conn(inode);
139                 u64 nodeid = get_node_id(inode);
140                 fuse_send_release(fc, ff, nodeid, inode, file->f_flags, isdir);
141         }
142
143         /* Return value is ignored by VFS */
144         return 0;
145 }
146
147 static int fuse_open(struct inode *inode, struct file *file)
148 {
149         return fuse_open_common(inode, file, 0);
150 }
151
152 static int fuse_release(struct inode *inode, struct file *file)
153 {
154         return fuse_release_common(inode, file, 0);
155 }
156
157 static int fuse_flush(struct file *file)
158 {
159         struct inode *inode = file->f_dentry->d_inode;
160         struct fuse_conn *fc = get_fuse_conn(inode);
161         struct fuse_file *ff = file->private_data;
162         struct fuse_req *req;
163         struct fuse_flush_in inarg;
164         int err;
165
166         if (is_bad_inode(inode))
167                 return -EIO;
168
169         if (fc->no_flush)
170                 return 0;
171
172         req = fuse_get_request(fc);
173         if (!req)
174                 return -EINTR;
175
176         memset(&inarg, 0, sizeof(inarg));
177         inarg.fh = ff->fh;
178         req->in.h.opcode = FUSE_FLUSH;
179         req->in.h.nodeid = get_node_id(inode);
180         req->inode = inode;
181         req->file = file;
182         req->in.numargs = 1;
183         req->in.args[0].size = sizeof(inarg);
184         req->in.args[0].value = &inarg;
185         request_send(fc, req);
186         err = req->out.h.error;
187         fuse_put_request(fc, req);
188         if (err == -ENOSYS) {
189                 fc->no_flush = 1;
190                 err = 0;
191         }
192         return err;
193 }
194
195 int fuse_fsync_common(struct file *file, struct dentry *de, int datasync,
196                       int isdir)
197 {
198         struct inode *inode = de->d_inode;
199         struct fuse_conn *fc = get_fuse_conn(inode);
200         struct fuse_file *ff = file->private_data;
201         struct fuse_req *req;
202         struct fuse_fsync_in inarg;
203         int err;
204
205         if (is_bad_inode(inode))
206                 return -EIO;
207
208         if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir))
209                 return 0;
210
211         req = fuse_get_request(fc);
212         if (!req)
213                 return -EINTR;
214
215         memset(&inarg, 0, sizeof(inarg));
216         inarg.fh = ff->fh;
217         inarg.fsync_flags = datasync ? 1 : 0;
218         req->in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC;
219         req->in.h.nodeid = get_node_id(inode);
220         req->inode = inode;
221         req->file = file;
222         req->in.numargs = 1;
223         req->in.args[0].size = sizeof(inarg);
224         req->in.args[0].value = &inarg;
225         request_send(fc, req);
226         err = req->out.h.error;
227         fuse_put_request(fc, req);
228         if (err == -ENOSYS) {
229                 if (isdir)
230                         fc->no_fsyncdir = 1;
231                 else
232                         fc->no_fsync = 1;
233                 err = 0;
234         }
235         return err;
236 }
237
238 static int fuse_fsync(struct file *file, struct dentry *de, int datasync)
239 {
240         return fuse_fsync_common(file, de, datasync, 0);
241 }
242
243 size_t fuse_send_read_common(struct fuse_req *req, struct file *file,
244                              struct inode *inode, loff_t pos, size_t count,
245                              int isdir)
246 {
247         struct fuse_conn *fc = get_fuse_conn(inode);
248         struct fuse_file *ff = file->private_data;
249         struct fuse_read_in inarg;
250
251         memset(&inarg, 0, sizeof(struct fuse_read_in));
252         inarg.fh = ff->fh;
253         inarg.offset = pos;
254         inarg.size = count;
255         req->in.h.opcode = isdir ? FUSE_READDIR : FUSE_READ;
256         req->in.h.nodeid = get_node_id(inode);
257         req->inode = inode;
258         req->file = file;
259         req->in.numargs = 1;
260         req->in.args[0].size = sizeof(struct fuse_read_in);
261         req->in.args[0].value = &inarg;
262         req->out.argpages = 1;
263         req->out.argvar = 1;
264         req->out.numargs = 1;
265         req->out.args[0].size = count;
266         request_send(fc, req);
267         return req->out.args[0].size;
268 }
269
270 static inline size_t fuse_send_read(struct fuse_req *req, struct file *file,
271                                     struct inode *inode, loff_t pos,
272                                     size_t count)
273 {
274         return fuse_send_read_common(req, file, inode, pos, count, 0);
275 }
276
277 static int fuse_readpage(struct file *file, struct page *page)
278 {
279         struct inode *inode = page->mapping->host;
280         struct fuse_conn *fc = get_fuse_conn(inode);
281         struct fuse_req *req;
282         int err;
283
284         err = -EIO;
285         if (is_bad_inode(inode))
286                 goto out;
287
288         err = -EINTR;
289         req = fuse_get_request(fc);
290         if (!req)
291                 goto out;
292
293         req->out.page_zeroing = 1;
294         req->num_pages = 1;
295         req->pages[0] = page;
296         fuse_send_read(req, file, inode, page_offset(page), PAGE_CACHE_SIZE);
297         err = req->out.h.error;
298         fuse_put_request(fc, req);
299         if (!err)
300                 SetPageUptodate(page);
301         fuse_invalidate_attr(inode); /* atime changed */
302  out:
303         unlock_page(page);
304         return err;
305 }
306
307 static int fuse_send_readpages(struct fuse_req *req, struct file *file,
308                                struct inode *inode)
309 {
310         loff_t pos = page_offset(req->pages[0]);
311         size_t count = req->num_pages << PAGE_CACHE_SHIFT;
312         unsigned i;
313         req->out.page_zeroing = 1;
314         fuse_send_read(req, file, inode, pos, count);
315         for (i = 0; i < req->num_pages; i++) {
316                 struct page *page = req->pages[i];
317                 if (!req->out.h.error)
318                         SetPageUptodate(page);
319                 unlock_page(page);
320         }
321         return req->out.h.error;
322 }
323
324 struct fuse_readpages_data {
325         struct fuse_req *req;
326         struct file *file;
327         struct inode *inode;
328 };
329
330 static int fuse_readpages_fill(void *_data, struct page *page)
331 {
332         struct fuse_readpages_data *data = _data;
333         struct fuse_req *req = data->req;
334         struct inode *inode = data->inode;
335         struct fuse_conn *fc = get_fuse_conn(inode);
336
337         if (req->num_pages &&
338             (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
339              (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read ||
340              req->pages[req->num_pages - 1]->index + 1 != page->index)) {
341                 int err = fuse_send_readpages(req, data->file, inode);
342                 if (err) {
343                         unlock_page(page);
344                         return err;
345                 }
346                 fuse_reset_request(req);
347         }
348         req->pages[req->num_pages] = page;
349         req->num_pages ++;
350         return 0;
351 }
352
353 static int fuse_readpages(struct file *file, struct address_space *mapping,
354                           struct list_head *pages, unsigned nr_pages)
355 {
356         struct inode *inode = mapping->host;
357         struct fuse_conn *fc = get_fuse_conn(inode);
358         struct fuse_readpages_data data;
359         int err;
360
361         if (is_bad_inode(inode))
362                 return -EIO;
363
364         data.file = file;
365         data.inode = inode;
366         data.req = fuse_get_request(fc);
367         if (!data.req)
368                 return -EINTR;
369
370         err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
371         if (!err && data.req->num_pages)
372                 err = fuse_send_readpages(data.req, file, inode);
373         fuse_put_request(fc, data.req);
374         fuse_invalidate_attr(inode); /* atime changed */
375         return err;
376 }
377
378 static size_t fuse_send_write(struct fuse_req *req, struct file *file,
379                               struct inode *inode, loff_t pos, size_t count)
380 {
381         struct fuse_conn *fc = get_fuse_conn(inode);
382         struct fuse_file *ff = file->private_data;
383         struct fuse_write_in inarg;
384         struct fuse_write_out outarg;
385
386         memset(&inarg, 0, sizeof(struct fuse_write_in));
387         inarg.fh = ff->fh;
388         inarg.offset = pos;
389         inarg.size = count;
390         req->in.h.opcode = FUSE_WRITE;
391         req->in.h.nodeid = get_node_id(inode);
392         req->inode = inode;
393         req->file = file;
394         req->in.argpages = 1;
395         req->in.numargs = 2;
396         req->in.args[0].size = sizeof(struct fuse_write_in);
397         req->in.args[0].value = &inarg;
398         req->in.args[1].size = count;
399         req->out.numargs = 1;
400         req->out.args[0].size = sizeof(struct fuse_write_out);
401         req->out.args[0].value = &outarg;
402         request_send(fc, req);
403         return outarg.size;
404 }
405
406 static int fuse_prepare_write(struct file *file, struct page *page,
407                               unsigned offset, unsigned to)
408 {
409         /* No op */
410         return 0;
411 }
412
413 static int fuse_commit_write(struct file *file, struct page *page,
414                              unsigned offset, unsigned to)
415 {
416         int err;
417         size_t nres;
418         unsigned count = to - offset;
419         struct inode *inode = page->mapping->host;
420         struct fuse_conn *fc = get_fuse_conn(inode);
421         loff_t pos = page_offset(page) + offset;
422         struct fuse_req *req;
423
424         if (is_bad_inode(inode))
425                 return -EIO;
426
427         req = fuse_get_request(fc);
428         if (!req)
429                 return -EINTR;
430
431         req->num_pages = 1;
432         req->pages[0] = page;
433         req->page_offset = offset;
434         nres = fuse_send_write(req, file, inode, pos, count);
435         err = req->out.h.error;
436         fuse_put_request(fc, req);
437         if (!err && nres != count)
438                 err = -EIO;
439         if (!err) {
440                 pos += count;
441                 if (pos > i_size_read(inode))
442                         i_size_write(inode, pos);
443
444                 if (offset == 0 && to == PAGE_CACHE_SIZE) {
445                         clear_page_dirty(page);
446                         SetPageUptodate(page);
447                 }
448         }
449         fuse_invalidate_attr(inode);
450         return err;
451 }
452
453 static void fuse_release_user_pages(struct fuse_req *req, int write)
454 {
455         unsigned i;
456
457         for (i = 0; i < req->num_pages; i++) {
458                 struct page *page = req->pages[i];
459                 if (write)
460                         set_page_dirty_lock(page);
461                 put_page(page);
462         }
463 }
464
465 static int fuse_get_user_pages(struct fuse_req *req, const char __user *buf,
466                                unsigned nbytes, int write)
467 {
468         unsigned long user_addr = (unsigned long) buf;
469         unsigned offset = user_addr & ~PAGE_MASK;
470         int npages;
471
472         /* This doesn't work with nfsd */
473         if (!current->mm)
474                 return -EPERM;
475
476         nbytes = min(nbytes, (unsigned) FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT);
477         npages = (nbytes + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
478         npages = min(max(npages, 1), FUSE_MAX_PAGES_PER_REQ);
479         down_read(&current->mm->mmap_sem);
480         npages = get_user_pages(current, current->mm, user_addr, npages, write,
481                                 0, req->pages, NULL);
482         up_read(&current->mm->mmap_sem);
483         if (npages < 0)
484                 return npages;
485
486         req->num_pages = npages;
487         req->page_offset = offset;
488         return 0;
489 }
490
491 static ssize_t fuse_direct_io(struct file *file, const char __user *buf,
492                               size_t count, loff_t *ppos, int write)
493 {
494         struct inode *inode = file->f_dentry->d_inode;
495         struct fuse_conn *fc = get_fuse_conn(inode);
496         size_t nmax = write ? fc->max_write : fc->max_read;
497         loff_t pos = *ppos;
498         ssize_t res = 0;
499         struct fuse_req *req;
500
501         if (is_bad_inode(inode))
502                 return -EIO;
503
504         req = fuse_get_request(fc);
505         if (!req)
506                 return -EINTR;
507
508         while (count) {
509                 size_t nres;
510                 size_t nbytes = min(count, nmax);
511                 int err = fuse_get_user_pages(req, buf, nbytes, !write);
512                 if (err) {
513                         res = err;
514                         break;
515                 }
516                 nbytes = (req->num_pages << PAGE_SHIFT) - req->page_offset;
517                 nbytes = min(count, nbytes);
518                 if (write)
519                         nres = fuse_send_write(req, file, inode, pos, nbytes);
520                 else
521                         nres = fuse_send_read(req, file, inode, pos, nbytes);
522                 fuse_release_user_pages(req, !write);
523                 if (req->out.h.error) {
524                         if (!res)
525                                 res = req->out.h.error;
526                         break;
527                 } else if (nres > nbytes) {
528                         res = -EIO;
529                         break;
530                 }
531                 count -= nres;
532                 res += nres;
533                 pos += nres;
534                 buf += nres;
535                 if (nres != nbytes)
536                         break;
537                 if (count)
538                         fuse_reset_request(req);
539         }
540         fuse_put_request(fc, req);
541         if (res > 0) {
542                 if (write && pos > i_size_read(inode))
543                         i_size_write(inode, pos);
544                 *ppos = pos;
545         }
546         fuse_invalidate_attr(inode);
547
548         return res;
549 }
550
551 static ssize_t fuse_direct_read(struct file *file, char __user *buf,
552                                      size_t count, loff_t *ppos)
553 {
554         return fuse_direct_io(file, buf, count, ppos, 0);
555 }
556
557 static ssize_t fuse_direct_write(struct file *file, const char __user *buf,
558                                  size_t count, loff_t *ppos)
559 {
560         struct inode *inode = file->f_dentry->d_inode;
561         ssize_t res;
562         /* Don't allow parallel writes to the same file */
563         mutex_lock(&inode->i_mutex);
564         res = fuse_direct_io(file, buf, count, ppos, 1);
565         mutex_unlock(&inode->i_mutex);
566         return res;
567 }
568
569 static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
570 {
571         if ((vma->vm_flags & VM_SHARED)) {
572                 if ((vma->vm_flags & VM_WRITE))
573                         return -ENODEV;
574                 else
575                         vma->vm_flags &= ~VM_MAYWRITE;
576         }
577         return generic_file_mmap(file, vma);
578 }
579
580 static int fuse_set_page_dirty(struct page *page)
581 {
582         printk("fuse_set_page_dirty: should not happen\n");
583         dump_stack();
584         return 0;
585 }
586
587 static struct file_operations fuse_file_operations = {
588         .llseek         = generic_file_llseek,
589         .read           = generic_file_read,
590         .write          = generic_file_write,
591         .mmap           = fuse_file_mmap,
592         .open           = fuse_open,
593         .flush          = fuse_flush,
594         .release        = fuse_release,
595         .fsync          = fuse_fsync,
596         .sendfile       = generic_file_sendfile,
597 };
598
599 static struct file_operations fuse_direct_io_file_operations = {
600         .llseek         = generic_file_llseek,
601         .read           = fuse_direct_read,
602         .write          = fuse_direct_write,
603         .open           = fuse_open,
604         .flush          = fuse_flush,
605         .release        = fuse_release,
606         .fsync          = fuse_fsync,
607         /* no mmap and sendfile */
608 };
609
610 static struct address_space_operations fuse_file_aops  = {
611         .readpage       = fuse_readpage,
612         .prepare_write  = fuse_prepare_write,
613         .commit_write   = fuse_commit_write,
614         .readpages      = fuse_readpages,
615         .set_page_dirty = fuse_set_page_dirty,
616 };
617
618 void fuse_init_file_inode(struct inode *inode)
619 {
620         inode->i_fop = &fuse_file_operations;
621         inode->i_data.a_ops = &fuse_file_aops;
622 }