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