Merge /spare/repo/linux-2.6/
[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 int fuse_open_common(struct inode *inode, struct file *file, int isdir)
18 {
19         struct fuse_conn *fc = get_fuse_conn(inode);
20         struct fuse_req *req;
21         struct fuse_open_in inarg;
22         struct fuse_open_out outarg;
23         struct fuse_file *ff;
24         int err;
25
26         err = generic_file_open(inode, file);
27         if (err)
28                 return err;
29
30         /* If opening the root node, no lookup has been performed on
31            it, so the attributes must be refreshed */
32         if (get_node_id(inode) == FUSE_ROOT_ID) {
33                 int err = fuse_do_getattr(inode);
34                 if (err)
35                         return err;
36         }
37
38         req = fuse_get_request(fc);
39         if (!req)
40                 return -EINTR;
41
42         err = -ENOMEM;
43         ff = kmalloc(sizeof(struct fuse_file), GFP_KERNEL);
44         if (!ff)
45                 goto out_put_request;
46
47         ff->release_req = fuse_request_alloc();
48         if (!ff->release_req) {
49                 kfree(ff);
50                 goto out_put_request;
51         }
52
53         memset(&inarg, 0, sizeof(inarg));
54         inarg.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
55         req->in.h.opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN;
56         req->in.h.nodeid = get_node_id(inode);
57         req->inode = inode;
58         req->in.numargs = 1;
59         req->in.args[0].size = sizeof(inarg);
60         req->in.args[0].value = &inarg;
61         req->out.numargs = 1;
62         req->out.args[0].size = sizeof(outarg);
63         req->out.args[0].value = &outarg;
64         request_send(fc, req);
65         err = req->out.h.error;
66         if (err) {
67                 fuse_request_free(ff->release_req);
68                 kfree(ff);
69         } else {
70                 if (!isdir && (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  out_put_request:
79         fuse_put_request(fc, req);
80         return err;
81 }
82
83 int fuse_release_common(struct inode *inode, struct file *file, int isdir)
84 {
85         struct fuse_conn *fc = get_fuse_conn(inode);
86         struct fuse_file *ff = file->private_data;
87         struct fuse_req *req = ff->release_req;
88         struct fuse_release_in *inarg = &req->misc.release_in;
89
90         inarg->fh = ff->fh;
91         inarg->flags = file->f_flags & ~O_EXCL;
92         req->in.h.opcode = isdir ? FUSE_RELEASEDIR : FUSE_RELEASE;
93         req->in.h.nodeid = get_node_id(inode);
94         req->inode = inode;
95         req->in.numargs = 1;
96         req->in.args[0].size = sizeof(struct fuse_release_in);
97         req->in.args[0].value = inarg;
98         request_send_background(fc, req);
99         kfree(ff);
100
101         /* Return value is ignored by VFS */
102         return 0;
103 }
104
105 static int fuse_open(struct inode *inode, struct file *file)
106 {
107         return fuse_open_common(inode, file, 0);
108 }
109
110 static int fuse_release(struct inode *inode, struct file *file)
111 {
112         return fuse_release_common(inode, file, 0);
113 }
114
115 static int fuse_flush(struct file *file)
116 {
117         struct inode *inode = file->f_dentry->d_inode;
118         struct fuse_conn *fc = get_fuse_conn(inode);
119         struct fuse_file *ff = file->private_data;
120         struct fuse_req *req;
121         struct fuse_flush_in inarg;
122         int err;
123
124         if (fc->no_flush)
125                 return 0;
126
127         req = fuse_get_request(fc);
128         if (!req)
129                 return -EINTR;
130
131         memset(&inarg, 0, sizeof(inarg));
132         inarg.fh = ff->fh;
133         req->in.h.opcode = FUSE_FLUSH;
134         req->in.h.nodeid = get_node_id(inode);
135         req->inode = inode;
136         req->file = file;
137         req->in.numargs = 1;
138         req->in.args[0].size = sizeof(inarg);
139         req->in.args[0].value = &inarg;
140         request_send(fc, req);
141         err = req->out.h.error;
142         fuse_put_request(fc, req);
143         if (err == -ENOSYS) {
144                 fc->no_flush = 1;
145                 err = 0;
146         }
147         return err;
148 }
149
150 int fuse_fsync_common(struct file *file, struct dentry *de, int datasync,
151                       int isdir)
152 {
153         struct inode *inode = de->d_inode;
154         struct fuse_conn *fc = get_fuse_conn(inode);
155         struct fuse_file *ff = file->private_data;
156         struct fuse_req *req;
157         struct fuse_fsync_in inarg;
158         int err;
159
160         if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir))
161                 return 0;
162
163         req = fuse_get_request(fc);
164         if (!req)
165                 return -EINTR;
166
167         memset(&inarg, 0, sizeof(inarg));
168         inarg.fh = ff->fh;
169         inarg.fsync_flags = datasync ? 1 : 0;
170         req->in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC;
171         req->in.h.nodeid = get_node_id(inode);
172         req->inode = inode;
173         req->file = file;
174         req->in.numargs = 1;
175         req->in.args[0].size = sizeof(inarg);
176         req->in.args[0].value = &inarg;
177         request_send(fc, req);
178         err = req->out.h.error;
179         fuse_put_request(fc, req);
180         if (err == -ENOSYS) {
181                 if (isdir)
182                         fc->no_fsyncdir = 1;
183                 else
184                         fc->no_fsync = 1;
185                 err = 0;
186         }
187         return err;
188 }
189
190 static int fuse_fsync(struct file *file, struct dentry *de, int datasync)
191 {
192         return fuse_fsync_common(file, de, datasync, 0);
193 }
194
195 size_t fuse_send_read_common(struct fuse_req *req, struct file *file,
196                              struct inode *inode, loff_t pos, size_t count,
197                              int isdir)
198 {
199         struct fuse_conn *fc = get_fuse_conn(inode);
200         struct fuse_file *ff = file->private_data;
201         struct fuse_read_in inarg;
202
203         memset(&inarg, 0, sizeof(struct fuse_read_in));
204         inarg.fh = ff->fh;
205         inarg.offset = pos;
206         inarg.size = count;
207         req->in.h.opcode = isdir ? FUSE_READDIR : FUSE_READ;
208         req->in.h.nodeid = get_node_id(inode);
209         req->inode = inode;
210         req->file = file;
211         req->in.numargs = 1;
212         req->in.args[0].size = sizeof(struct fuse_read_in);
213         req->in.args[0].value = &inarg;
214         req->out.argpages = 1;
215         req->out.argvar = 1;
216         req->out.numargs = 1;
217         req->out.args[0].size = count;
218         request_send(fc, req);
219         return req->out.args[0].size;
220 }
221
222 static inline size_t fuse_send_read(struct fuse_req *req, struct file *file,
223                                     struct inode *inode, loff_t pos,
224                                     size_t count)
225 {
226         return fuse_send_read_common(req, file, inode, pos, count, 0);
227 }
228
229 static int fuse_readpage(struct file *file, struct page *page)
230 {
231         struct inode *inode = page->mapping->host;
232         struct fuse_conn *fc = get_fuse_conn(inode);
233         loff_t pos = (loff_t) page->index << PAGE_CACHE_SHIFT;
234         struct fuse_req *req = fuse_get_request(fc);
235         int err = -EINTR;
236         if (!req)
237                 goto out;
238
239         req->out.page_zeroing = 1;
240         req->num_pages = 1;
241         req->pages[0] = page;
242         fuse_send_read(req, file, inode, pos, PAGE_CACHE_SIZE);
243         err = req->out.h.error;
244         fuse_put_request(fc, req);
245         if (!err)
246                 SetPageUptodate(page);
247         fuse_invalidate_attr(inode); /* atime changed */
248  out:
249         unlock_page(page);
250         return err;
251 }
252
253 static int fuse_send_readpages(struct fuse_req *req, struct file *file,
254                                struct inode *inode)
255 {
256         loff_t pos = (loff_t) req->pages[0]->index << PAGE_CACHE_SHIFT;
257         size_t count = req->num_pages << PAGE_CACHE_SHIFT;
258         unsigned i;
259         req->out.page_zeroing = 1;
260         fuse_send_read(req, file, inode, pos, count);
261         for (i = 0; i < req->num_pages; i++) {
262                 struct page *page = req->pages[i];
263                 if (!req->out.h.error)
264                         SetPageUptodate(page);
265                 unlock_page(page);
266         }
267         return req->out.h.error;
268 }
269
270 struct fuse_readpages_data {
271         struct fuse_req *req;
272         struct file *file;
273         struct inode *inode;
274 };
275
276 static int fuse_readpages_fill(void *_data, struct page *page)
277 {
278         struct fuse_readpages_data *data = _data;
279         struct fuse_req *req = data->req;
280         struct inode *inode = data->inode;
281         struct fuse_conn *fc = get_fuse_conn(inode);
282
283         if (req->num_pages &&
284             (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
285              (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read ||
286              req->pages[req->num_pages - 1]->index + 1 != page->index)) {
287                 int err = fuse_send_readpages(req, data->file, inode);
288                 if (err) {
289                         unlock_page(page);
290                         return err;
291                 }
292                 fuse_reset_request(req);
293         }
294         req->pages[req->num_pages] = page;
295         req->num_pages ++;
296         return 0;
297 }
298
299 static int fuse_readpages(struct file *file, struct address_space *mapping,
300                           struct list_head *pages, unsigned nr_pages)
301 {
302         struct inode *inode = mapping->host;
303         struct fuse_conn *fc = get_fuse_conn(inode);
304         struct fuse_readpages_data data;
305         int err;
306         data.file = file;
307         data.inode = inode;
308         data.req = fuse_get_request(fc);
309         if (!data.req)
310                 return -EINTR;
311
312         err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
313         if (!err && data.req->num_pages)
314                 err = fuse_send_readpages(data.req, file, inode);
315         fuse_put_request(fc, data.req);
316         fuse_invalidate_attr(inode); /* atime changed */
317         return err;
318 }
319
320 static size_t fuse_send_write(struct fuse_req *req, struct file *file,
321                               struct inode *inode, loff_t pos, size_t count)
322 {
323         struct fuse_conn *fc = get_fuse_conn(inode);
324         struct fuse_file *ff = file->private_data;
325         struct fuse_write_in inarg;
326         struct fuse_write_out outarg;
327
328         memset(&inarg, 0, sizeof(struct fuse_write_in));
329         inarg.fh = ff->fh;
330         inarg.offset = pos;
331         inarg.size = count;
332         req->in.h.opcode = FUSE_WRITE;
333         req->in.h.nodeid = get_node_id(inode);
334         req->inode = inode;
335         req->file = file;
336         req->in.argpages = 1;
337         req->in.numargs = 2;
338         req->in.args[0].size = sizeof(struct fuse_write_in);
339         req->in.args[0].value = &inarg;
340         req->in.args[1].size = count;
341         req->out.numargs = 1;
342         req->out.args[0].size = sizeof(struct fuse_write_out);
343         req->out.args[0].value = &outarg;
344         request_send(fc, req);
345         return outarg.size;
346 }
347
348 static int fuse_prepare_write(struct file *file, struct page *page,
349                               unsigned offset, unsigned to)
350 {
351         /* No op */
352         return 0;
353 }
354
355 static int fuse_commit_write(struct file *file, struct page *page,
356                              unsigned offset, unsigned to)
357 {
358         int err;
359         size_t nres;
360         unsigned count = to - offset;
361         struct inode *inode = page->mapping->host;
362         struct fuse_conn *fc = get_fuse_conn(inode);
363         loff_t pos = ((loff_t) page->index << PAGE_CACHE_SHIFT) + offset;
364         struct fuse_req *req = fuse_get_request(fc);
365         if (!req)
366                 return -EINTR;
367
368         req->num_pages = 1;
369         req->pages[0] = page;
370         req->page_offset = offset;
371         nres = fuse_send_write(req, file, inode, pos, count);
372         err = req->out.h.error;
373         fuse_put_request(fc, req);
374         if (!err && nres != count)
375                 err = -EIO;
376         if (!err) {
377                 pos += count;
378                 if (pos > i_size_read(inode))
379                         i_size_write(inode, pos);
380
381                 if (offset == 0 && to == PAGE_CACHE_SIZE) {
382                         clear_page_dirty(page);
383                         SetPageUptodate(page);
384                 }
385         }
386         fuse_invalidate_attr(inode);
387         return err;
388 }
389
390 static void fuse_release_user_pages(struct fuse_req *req, int write)
391 {
392         unsigned i;
393
394         for (i = 0; i < req->num_pages; i++) {
395                 struct page *page = req->pages[i];
396                 if (write)
397                         set_page_dirty_lock(page);
398                 put_page(page);
399         }
400 }
401
402 static int fuse_get_user_pages(struct fuse_req *req, const char __user *buf,
403                                unsigned nbytes, int write)
404 {
405         unsigned long user_addr = (unsigned long) buf;
406         unsigned offset = user_addr & ~PAGE_MASK;
407         int npages;
408
409         /* This doesn't work with nfsd */
410         if (!current->mm)
411                 return -EPERM;
412
413         nbytes = min(nbytes, (unsigned) FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT);
414         npages = (nbytes + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
415         npages = min(npages, FUSE_MAX_PAGES_PER_REQ);
416         down_read(&current->mm->mmap_sem);
417         npages = get_user_pages(current, current->mm, user_addr, npages, write,
418                                 0, req->pages, NULL);
419         up_read(&current->mm->mmap_sem);
420         if (npages < 0)
421                 return npages;
422
423         req->num_pages = npages;
424         req->page_offset = offset;
425         return 0;
426 }
427
428 static ssize_t fuse_direct_io(struct file *file, const char __user *buf,
429                               size_t count, loff_t *ppos, int write)
430 {
431         struct inode *inode = file->f_dentry->d_inode;
432         struct fuse_conn *fc = get_fuse_conn(inode);
433         size_t nmax = write ? fc->max_write : fc->max_read;
434         loff_t pos = *ppos;
435         ssize_t res = 0;
436         struct fuse_req *req = fuse_get_request(fc);
437         if (!req)
438                 return -EINTR;
439
440         while (count) {
441                 size_t tmp;
442                 size_t nres;
443                 size_t nbytes = min(count, nmax);
444                 int err = fuse_get_user_pages(req, buf, nbytes, !write);
445                 if (err) {
446                         res = err;
447                         break;
448                 }
449                 tmp = (req->num_pages << PAGE_SHIFT) - req->page_offset;
450                 nbytes = min(nbytes, tmp);
451                 if (write)
452                         nres = fuse_send_write(req, file, inode, pos, nbytes);
453                 else
454                         nres = fuse_send_read(req, file, inode, pos, nbytes);
455                 fuse_release_user_pages(req, !write);
456                 if (req->out.h.error) {
457                         if (!res)
458                                 res = req->out.h.error;
459                         break;
460                 } else if (nres > nbytes) {
461                         res = -EIO;
462                         break;
463                 }
464                 count -= nres;
465                 res += nres;
466                 pos += nres;
467                 buf += nres;
468                 if (nres != nbytes)
469                         break;
470                 if (count)
471                         fuse_reset_request(req);
472         }
473         fuse_put_request(fc, req);
474         if (res > 0) {
475                 if (write && pos > i_size_read(inode))
476                         i_size_write(inode, pos);
477                 *ppos = pos;
478         }
479         fuse_invalidate_attr(inode);
480
481         return res;
482 }
483
484 static ssize_t fuse_direct_read(struct file *file, char __user *buf,
485                                      size_t count, loff_t *ppos)
486 {
487         return fuse_direct_io(file, buf, count, ppos, 0);
488 }
489
490 static ssize_t fuse_direct_write(struct file *file, const char __user *buf,
491                                  size_t count, loff_t *ppos)
492 {
493         struct inode *inode = file->f_dentry->d_inode;
494         ssize_t res;
495         /* Don't allow parallel writes to the same file */
496         down(&inode->i_sem);
497         res = fuse_direct_io(file, buf, count, ppos, 1);
498         up(&inode->i_sem);
499         return res;
500 }
501
502 static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
503 {
504         if ((vma->vm_flags & VM_SHARED)) {
505                 if ((vma->vm_flags & VM_WRITE))
506                         return -ENODEV;
507                 else
508                         vma->vm_flags &= ~VM_MAYWRITE;
509         }
510         return generic_file_mmap(file, vma);
511 }
512
513 static int fuse_set_page_dirty(struct page *page)
514 {
515         printk("fuse_set_page_dirty: should not happen\n");
516         dump_stack();
517         return 0;
518 }
519
520 static struct file_operations fuse_file_operations = {
521         .llseek         = generic_file_llseek,
522         .read           = generic_file_read,
523         .write          = generic_file_write,
524         .mmap           = fuse_file_mmap,
525         .open           = fuse_open,
526         .flush          = fuse_flush,
527         .release        = fuse_release,
528         .fsync          = fuse_fsync,
529         .sendfile       = generic_file_sendfile,
530 };
531
532 static struct file_operations fuse_direct_io_file_operations = {
533         .llseek         = generic_file_llseek,
534         .read           = fuse_direct_read,
535         .write          = fuse_direct_write,
536         .open           = fuse_open,
537         .flush          = fuse_flush,
538         .release        = fuse_release,
539         .fsync          = fuse_fsync,
540         /* no mmap and sendfile */
541 };
542
543 static struct address_space_operations fuse_file_aops  = {
544         .readpage       = fuse_readpage,
545         .prepare_write  = fuse_prepare_write,
546         .commit_write   = fuse_commit_write,
547         .readpages      = fuse_readpages,
548         .set_page_dirty = fuse_set_page_dirty,
549 };
550
551 void fuse_init_file_inode(struct inode *inode)
552 {
553         inode->i_fop = &fuse_file_operations;
554         inode->i_data.a_ops = &fuse_file_aops;
555 }