[IPVS]: Move remaining sysctl handlers over to CTL_UNNUMBERED
[linux-2.6/mini2440.git] / fs / fuse / file.c
blob535b37399009128df2413d3b7632160390113b9c
1 /*
2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2006 Miklos Szeredi <miklos@szeredi.hu>
5 This program can be distributed under the terms of the GNU GPL.
6 See the file COPYING.
7 */
9 #include "fuse_i.h"
11 #include <linux/pagemap.h>
12 #include <linux/slab.h>
13 #include <linux/kernel.h>
14 #include <linux/sched.h>
16 static const struct file_operations fuse_direct_io_file_operations;
18 static int fuse_send_open(struct inode *inode, struct file *file, int isdir,
19 struct fuse_open_out *outargp)
21 struct fuse_conn *fc = get_fuse_conn(inode);
22 struct fuse_open_in inarg;
23 struct fuse_req *req;
24 int err;
26 req = fuse_get_req(fc);
27 if (IS_ERR(req))
28 return PTR_ERR(req);
30 memset(&inarg, 0, sizeof(inarg));
31 inarg.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY);
32 if (!fc->atomic_o_trunc)
33 inarg.flags &= ~O_TRUNC;
34 req->in.h.opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN;
35 req->in.h.nodeid = get_node_id(inode);
36 req->in.numargs = 1;
37 req->in.args[0].size = sizeof(inarg);
38 req->in.args[0].value = &inarg;
39 req->out.numargs = 1;
40 req->out.args[0].size = sizeof(*outargp);
41 req->out.args[0].value = outargp;
42 request_send(fc, req);
43 err = req->out.h.error;
44 fuse_put_request(fc, req);
46 return err;
49 struct fuse_file *fuse_file_alloc(void)
51 struct fuse_file *ff;
52 ff = kmalloc(sizeof(struct fuse_file), GFP_KERNEL);
53 if (ff) {
54 ff->reserved_req = fuse_request_alloc();
55 if (!ff->reserved_req) {
56 kfree(ff);
57 ff = NULL;
58 } else {
59 INIT_LIST_HEAD(&ff->write_entry);
60 atomic_set(&ff->count, 0);
63 return ff;
66 void fuse_file_free(struct fuse_file *ff)
68 fuse_request_free(ff->reserved_req);
69 kfree(ff);
72 static struct fuse_file *fuse_file_get(struct fuse_file *ff)
74 atomic_inc(&ff->count);
75 return ff;
78 static void fuse_release_end(struct fuse_conn *fc, struct fuse_req *req)
80 dput(req->dentry);
81 mntput(req->vfsmount);
82 fuse_put_request(fc, req);
85 static void fuse_file_put(struct fuse_file *ff)
87 if (atomic_dec_and_test(&ff->count)) {
88 struct fuse_req *req = ff->reserved_req;
89 struct fuse_conn *fc = get_fuse_conn(req->dentry->d_inode);
90 req->end = fuse_release_end;
91 request_send_background(fc, req);
92 kfree(ff);
96 void fuse_finish_open(struct inode *inode, struct file *file,
97 struct fuse_file *ff, struct fuse_open_out *outarg)
99 if (outarg->open_flags & FOPEN_DIRECT_IO)
100 file->f_op = &fuse_direct_io_file_operations;
101 if (!(outarg->open_flags & FOPEN_KEEP_CACHE))
102 invalidate_inode_pages2(inode->i_mapping);
103 ff->fh = outarg->fh;
104 file->private_data = fuse_file_get(ff);
107 int fuse_open_common(struct inode *inode, struct file *file, int isdir)
109 struct fuse_open_out outarg;
110 struct fuse_file *ff;
111 int err;
113 /* VFS checks this, but only _after_ ->open() */
114 if (file->f_flags & O_DIRECT)
115 return -EINVAL;
117 err = generic_file_open(inode, file);
118 if (err)
119 return err;
121 ff = fuse_file_alloc();
122 if (!ff)
123 return -ENOMEM;
125 err = fuse_send_open(inode, file, isdir, &outarg);
126 if (err)
127 fuse_file_free(ff);
128 else {
129 if (isdir)
130 outarg.open_flags &= ~FOPEN_DIRECT_IO;
131 fuse_finish_open(inode, file, ff, &outarg);
134 return err;
137 void fuse_release_fill(struct fuse_file *ff, u64 nodeid, int flags, int opcode)
139 struct fuse_req *req = ff->reserved_req;
140 struct fuse_release_in *inarg = &req->misc.release_in;
142 inarg->fh = ff->fh;
143 inarg->flags = flags;
144 req->in.h.opcode = opcode;
145 req->in.h.nodeid = nodeid;
146 req->in.numargs = 1;
147 req->in.args[0].size = sizeof(struct fuse_release_in);
148 req->in.args[0].value = inarg;
151 int fuse_release_common(struct inode *inode, struct file *file, int isdir)
153 struct fuse_file *ff = file->private_data;
154 if (ff) {
155 struct fuse_conn *fc = get_fuse_conn(inode);
157 fuse_release_fill(ff, get_node_id(inode), file->f_flags,
158 isdir ? FUSE_RELEASEDIR : FUSE_RELEASE);
160 /* Hold vfsmount and dentry until release is finished */
161 ff->reserved_req->vfsmount = mntget(file->f_path.mnt);
162 ff->reserved_req->dentry = dget(file->f_path.dentry);
164 spin_lock(&fc->lock);
165 list_del(&ff->write_entry);
166 spin_unlock(&fc->lock);
168 * Normally this will send the RELEASE request,
169 * however if some asynchronous READ or WRITE requests
170 * are outstanding, the sending will be delayed
172 fuse_file_put(ff);
175 /* Return value is ignored by VFS */
176 return 0;
179 static int fuse_open(struct inode *inode, struct file *file)
181 return fuse_open_common(inode, file, 0);
184 static int fuse_release(struct inode *inode, struct file *file)
186 return fuse_release_common(inode, file, 0);
190 * Scramble the ID space with XTEA, so that the value of the files_struct
191 * pointer is not exposed to userspace.
193 u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
195 u32 *k = fc->scramble_key;
196 u64 v = (unsigned long) id;
197 u32 v0 = v;
198 u32 v1 = v >> 32;
199 u32 sum = 0;
200 int i;
202 for (i = 0; i < 32; i++) {
203 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
204 sum += 0x9E3779B9;
205 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
208 return (u64) v0 + ((u64) v1 << 32);
211 static int fuse_flush(struct file *file, fl_owner_t id)
213 struct inode *inode = file->f_path.dentry->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_flush_in inarg;
218 int err;
220 if (is_bad_inode(inode))
221 return -EIO;
223 if (fc->no_flush)
224 return 0;
226 req = fuse_get_req_nofail(fc, file);
227 memset(&inarg, 0, sizeof(inarg));
228 inarg.fh = ff->fh;
229 inarg.lock_owner = fuse_lock_owner_id(fc, id);
230 req->in.h.opcode = FUSE_FLUSH;
231 req->in.h.nodeid = get_node_id(inode);
232 req->in.numargs = 1;
233 req->in.args[0].size = sizeof(inarg);
234 req->in.args[0].value = &inarg;
235 req->force = 1;
236 request_send(fc, req);
237 err = req->out.h.error;
238 fuse_put_request(fc, req);
239 if (err == -ENOSYS) {
240 fc->no_flush = 1;
241 err = 0;
243 return err;
246 int fuse_fsync_common(struct file *file, struct dentry *de, int datasync,
247 int isdir)
249 struct inode *inode = de->d_inode;
250 struct fuse_conn *fc = get_fuse_conn(inode);
251 struct fuse_file *ff = file->private_data;
252 struct fuse_req *req;
253 struct fuse_fsync_in inarg;
254 int err;
256 if (is_bad_inode(inode))
257 return -EIO;
259 if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir))
260 return 0;
262 req = fuse_get_req(fc);
263 if (IS_ERR(req))
264 return PTR_ERR(req);
266 memset(&inarg, 0, sizeof(inarg));
267 inarg.fh = ff->fh;
268 inarg.fsync_flags = datasync ? 1 : 0;
269 req->in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC;
270 req->in.h.nodeid = get_node_id(inode);
271 req->in.numargs = 1;
272 req->in.args[0].size = sizeof(inarg);
273 req->in.args[0].value = &inarg;
274 request_send(fc, req);
275 err = req->out.h.error;
276 fuse_put_request(fc, req);
277 if (err == -ENOSYS) {
278 if (isdir)
279 fc->no_fsyncdir = 1;
280 else
281 fc->no_fsync = 1;
282 err = 0;
284 return err;
287 static int fuse_fsync(struct file *file, struct dentry *de, int datasync)
289 return fuse_fsync_common(file, de, datasync, 0);
292 void fuse_read_fill(struct fuse_req *req, struct fuse_file *ff,
293 struct inode *inode, loff_t pos, size_t count, int opcode)
295 struct fuse_read_in *inarg = &req->misc.read_in;
297 inarg->fh = ff->fh;
298 inarg->offset = pos;
299 inarg->size = count;
300 req->in.h.opcode = opcode;
301 req->in.h.nodeid = get_node_id(inode);
302 req->in.numargs = 1;
303 req->in.args[0].size = sizeof(struct fuse_read_in);
304 req->in.args[0].value = inarg;
305 req->out.argpages = 1;
306 req->out.argvar = 1;
307 req->out.numargs = 1;
308 req->out.args[0].size = count;
311 static size_t fuse_send_read(struct fuse_req *req, struct file *file,
312 struct inode *inode, loff_t pos, size_t count,
313 fl_owner_t owner)
315 struct fuse_conn *fc = get_fuse_conn(inode);
316 struct fuse_file *ff = file->private_data;
318 fuse_read_fill(req, ff, inode, pos, count, FUSE_READ);
319 if (owner != NULL) {
320 struct fuse_read_in *inarg = &req->misc.read_in;
322 inarg->read_flags |= FUSE_READ_LOCKOWNER;
323 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
325 request_send(fc, req);
326 return req->out.args[0].size;
329 static int fuse_readpage(struct file *file, struct page *page)
331 struct inode *inode = page->mapping->host;
332 struct fuse_conn *fc = get_fuse_conn(inode);
333 struct fuse_req *req;
334 int err;
336 err = -EIO;
337 if (is_bad_inode(inode))
338 goto out;
340 req = fuse_get_req(fc);
341 err = PTR_ERR(req);
342 if (IS_ERR(req))
343 goto out;
345 req->out.page_zeroing = 1;
346 req->num_pages = 1;
347 req->pages[0] = page;
348 fuse_send_read(req, file, inode, page_offset(page), PAGE_CACHE_SIZE,
349 NULL);
350 err = req->out.h.error;
351 fuse_put_request(fc, req);
352 if (!err)
353 SetPageUptodate(page);
354 fuse_invalidate_attr(inode); /* atime changed */
355 out:
356 unlock_page(page);
357 return err;
360 static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
362 int i;
364 fuse_invalidate_attr(req->pages[0]->mapping->host); /* atime changed */
366 for (i = 0; i < req->num_pages; i++) {
367 struct page *page = req->pages[i];
368 if (!req->out.h.error)
369 SetPageUptodate(page);
370 else
371 SetPageError(page);
372 unlock_page(page);
374 if (req->ff)
375 fuse_file_put(req->ff);
376 fuse_put_request(fc, req);
379 static void fuse_send_readpages(struct fuse_req *req, struct fuse_file *ff,
380 struct inode *inode)
382 struct fuse_conn *fc = get_fuse_conn(inode);
383 loff_t pos = page_offset(req->pages[0]);
384 size_t count = req->num_pages << PAGE_CACHE_SHIFT;
385 req->out.page_zeroing = 1;
386 fuse_read_fill(req, ff, inode, pos, count, FUSE_READ);
387 if (fc->async_read) {
388 req->ff = fuse_file_get(ff);
389 req->end = fuse_readpages_end;
390 request_send_background(fc, req);
391 } else {
392 request_send(fc, req);
393 fuse_readpages_end(fc, req);
397 struct fuse_fill_data {
398 struct fuse_req *req;
399 struct fuse_file *ff;
400 struct inode *inode;
403 static int fuse_readpages_fill(void *_data, struct page *page)
405 struct fuse_fill_data *data = _data;
406 struct fuse_req *req = data->req;
407 struct inode *inode = data->inode;
408 struct fuse_conn *fc = get_fuse_conn(inode);
410 if (req->num_pages &&
411 (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
412 (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read ||
413 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
414 fuse_send_readpages(req, data->ff, inode);
415 data->req = req = fuse_get_req(fc);
416 if (IS_ERR(req)) {
417 unlock_page(page);
418 return PTR_ERR(req);
421 req->pages[req->num_pages] = page;
422 req->num_pages ++;
423 return 0;
426 static int fuse_readpages(struct file *file, struct address_space *mapping,
427 struct list_head *pages, unsigned nr_pages)
429 struct inode *inode = mapping->host;
430 struct fuse_conn *fc = get_fuse_conn(inode);
431 struct fuse_fill_data data;
432 int err;
434 err = -EIO;
435 if (is_bad_inode(inode))
436 goto out;
438 data.ff = file->private_data;
439 data.inode = inode;
440 data.req = fuse_get_req(fc);
441 err = PTR_ERR(data.req);
442 if (IS_ERR(data.req))
443 goto out;
445 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
446 if (!err) {
447 if (data.req->num_pages)
448 fuse_send_readpages(data.req, data.ff, inode);
449 else
450 fuse_put_request(fc, data.req);
452 out:
453 return err;
456 static void fuse_write_fill(struct fuse_req *req, struct fuse_file *ff,
457 struct inode *inode, loff_t pos, size_t count,
458 int writepage)
460 struct fuse_conn *fc = get_fuse_conn(inode);
461 struct fuse_write_in *inarg = &req->misc.write.in;
462 struct fuse_write_out *outarg = &req->misc.write.out;
464 memset(inarg, 0, sizeof(struct fuse_write_in));
465 inarg->fh = ff->fh;
466 inarg->offset = pos;
467 inarg->size = count;
468 inarg->write_flags = writepage ? FUSE_WRITE_CACHE : 0;
469 req->in.h.opcode = FUSE_WRITE;
470 req->in.h.nodeid = get_node_id(inode);
471 req->in.argpages = 1;
472 req->in.numargs = 2;
473 if (fc->minor < 9)
474 req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
475 else
476 req->in.args[0].size = sizeof(struct fuse_write_in);
477 req->in.args[0].value = inarg;
478 req->in.args[1].size = count;
479 req->out.numargs = 1;
480 req->out.args[0].size = sizeof(struct fuse_write_out);
481 req->out.args[0].value = outarg;
484 static size_t fuse_send_write(struct fuse_req *req, struct file *file,
485 struct inode *inode, loff_t pos, size_t count,
486 fl_owner_t owner)
488 struct fuse_conn *fc = get_fuse_conn(inode);
489 fuse_write_fill(req, file->private_data, inode, pos, count, 0);
490 if (owner != NULL) {
491 struct fuse_write_in *inarg = &req->misc.write.in;
492 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
493 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
495 request_send(fc, req);
496 return req->misc.write.out.size;
499 static int fuse_write_begin(struct file *file, struct address_space *mapping,
500 loff_t pos, unsigned len, unsigned flags,
501 struct page **pagep, void **fsdata)
503 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
505 *pagep = __grab_cache_page(mapping, index);
506 if (!*pagep)
507 return -ENOMEM;
508 return 0;
511 static int fuse_buffered_write(struct file *file, struct inode *inode,
512 loff_t pos, unsigned count, struct page *page)
514 int err;
515 size_t nres;
516 struct fuse_conn *fc = get_fuse_conn(inode);
517 struct fuse_inode *fi = get_fuse_inode(inode);
518 unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
519 struct fuse_req *req;
521 if (is_bad_inode(inode))
522 return -EIO;
524 req = fuse_get_req(fc);
525 if (IS_ERR(req))
526 return PTR_ERR(req);
528 req->num_pages = 1;
529 req->pages[0] = page;
530 req->page_offset = offset;
531 nres = fuse_send_write(req, file, inode, pos, count, NULL);
532 err = req->out.h.error;
533 fuse_put_request(fc, req);
534 if (!err && !nres)
535 err = -EIO;
536 if (!err) {
537 pos += nres;
538 spin_lock(&fc->lock);
539 fi->attr_version = ++fc->attr_version;
540 if (pos > inode->i_size)
541 i_size_write(inode, pos);
542 spin_unlock(&fc->lock);
544 if (count == PAGE_CACHE_SIZE)
545 SetPageUptodate(page);
547 fuse_invalidate_attr(inode);
548 return err ? err : nres;
551 static int fuse_write_end(struct file *file, struct address_space *mapping,
552 loff_t pos, unsigned len, unsigned copied,
553 struct page *page, void *fsdata)
555 struct inode *inode = mapping->host;
556 int res = 0;
558 if (copied)
559 res = fuse_buffered_write(file, inode, pos, copied, page);
561 unlock_page(page);
562 page_cache_release(page);
563 return res;
566 static void fuse_release_user_pages(struct fuse_req *req, int write)
568 unsigned i;
570 for (i = 0; i < req->num_pages; i++) {
571 struct page *page = req->pages[i];
572 if (write)
573 set_page_dirty_lock(page);
574 put_page(page);
578 static int fuse_get_user_pages(struct fuse_req *req, const char __user *buf,
579 unsigned nbytes, int write)
581 unsigned long user_addr = (unsigned long) buf;
582 unsigned offset = user_addr & ~PAGE_MASK;
583 int npages;
585 /* This doesn't work with nfsd */
586 if (!current->mm)
587 return -EPERM;
589 nbytes = min(nbytes, (unsigned) FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT);
590 npages = (nbytes + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
591 npages = min(max(npages, 1), FUSE_MAX_PAGES_PER_REQ);
592 down_read(&current->mm->mmap_sem);
593 npages = get_user_pages(current, current->mm, user_addr, npages, write,
594 0, req->pages, NULL);
595 up_read(&current->mm->mmap_sem);
596 if (npages < 0)
597 return npages;
599 req->num_pages = npages;
600 req->page_offset = offset;
601 return 0;
604 static ssize_t fuse_direct_io(struct file *file, const char __user *buf,
605 size_t count, loff_t *ppos, int write)
607 struct inode *inode = file->f_path.dentry->d_inode;
608 struct fuse_conn *fc = get_fuse_conn(inode);
609 size_t nmax = write ? fc->max_write : fc->max_read;
610 loff_t pos = *ppos;
611 ssize_t res = 0;
612 struct fuse_req *req;
614 if (is_bad_inode(inode))
615 return -EIO;
617 req = fuse_get_req(fc);
618 if (IS_ERR(req))
619 return PTR_ERR(req);
621 while (count) {
622 size_t nres;
623 size_t nbytes = min(count, nmax);
624 int err = fuse_get_user_pages(req, buf, nbytes, !write);
625 if (err) {
626 res = err;
627 break;
629 nbytes = (req->num_pages << PAGE_SHIFT) - req->page_offset;
630 nbytes = min(count, nbytes);
631 if (write)
632 nres = fuse_send_write(req, file, inode, pos, nbytes,
633 current->files);
634 else
635 nres = fuse_send_read(req, file, inode, pos, nbytes,
636 current->files);
637 fuse_release_user_pages(req, !write);
638 if (req->out.h.error) {
639 if (!res)
640 res = req->out.h.error;
641 break;
642 } else if (nres > nbytes) {
643 res = -EIO;
644 break;
646 count -= nres;
647 res += nres;
648 pos += nres;
649 buf += nres;
650 if (nres != nbytes)
651 break;
652 if (count) {
653 fuse_put_request(fc, req);
654 req = fuse_get_req(fc);
655 if (IS_ERR(req))
656 break;
659 fuse_put_request(fc, req);
660 if (res > 0) {
661 if (write) {
662 spin_lock(&fc->lock);
663 if (pos > inode->i_size)
664 i_size_write(inode, pos);
665 spin_unlock(&fc->lock);
667 *ppos = pos;
669 fuse_invalidate_attr(inode);
671 return res;
674 static ssize_t fuse_direct_read(struct file *file, char __user *buf,
675 size_t count, loff_t *ppos)
677 return fuse_direct_io(file, buf, count, ppos, 0);
680 static ssize_t fuse_direct_write(struct file *file, const char __user *buf,
681 size_t count, loff_t *ppos)
683 struct inode *inode = file->f_path.dentry->d_inode;
684 ssize_t res;
685 /* Don't allow parallel writes to the same file */
686 mutex_lock(&inode->i_mutex);
687 res = generic_write_checks(file, ppos, &count, 0);
688 if (!res)
689 res = fuse_direct_io(file, buf, count, ppos, 1);
690 mutex_unlock(&inode->i_mutex);
691 return res;
694 static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
696 if ((vma->vm_flags & VM_SHARED)) {
697 if ((vma->vm_flags & VM_WRITE))
698 return -ENODEV;
699 else
700 vma->vm_flags &= ~VM_MAYWRITE;
702 return generic_file_mmap(file, vma);
705 static int fuse_set_page_dirty(struct page *page)
707 printk("fuse_set_page_dirty: should not happen\n");
708 dump_stack();
709 return 0;
712 static int convert_fuse_file_lock(const struct fuse_file_lock *ffl,
713 struct file_lock *fl)
715 switch (ffl->type) {
716 case F_UNLCK:
717 break;
719 case F_RDLCK:
720 case F_WRLCK:
721 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
722 ffl->end < ffl->start)
723 return -EIO;
725 fl->fl_start = ffl->start;
726 fl->fl_end = ffl->end;
727 fl->fl_pid = ffl->pid;
728 break;
730 default:
731 return -EIO;
733 fl->fl_type = ffl->type;
734 return 0;
737 static void fuse_lk_fill(struct fuse_req *req, struct file *file,
738 const struct file_lock *fl, int opcode, pid_t pid,
739 int flock)
741 struct inode *inode = file->f_path.dentry->d_inode;
742 struct fuse_conn *fc = get_fuse_conn(inode);
743 struct fuse_file *ff = file->private_data;
744 struct fuse_lk_in *arg = &req->misc.lk_in;
746 arg->fh = ff->fh;
747 arg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
748 arg->lk.start = fl->fl_start;
749 arg->lk.end = fl->fl_end;
750 arg->lk.type = fl->fl_type;
751 arg->lk.pid = pid;
752 if (flock)
753 arg->lk_flags |= FUSE_LK_FLOCK;
754 req->in.h.opcode = opcode;
755 req->in.h.nodeid = get_node_id(inode);
756 req->in.numargs = 1;
757 req->in.args[0].size = sizeof(*arg);
758 req->in.args[0].value = arg;
761 static int fuse_getlk(struct file *file, struct file_lock *fl)
763 struct inode *inode = file->f_path.dentry->d_inode;
764 struct fuse_conn *fc = get_fuse_conn(inode);
765 struct fuse_req *req;
766 struct fuse_lk_out outarg;
767 int err;
769 req = fuse_get_req(fc);
770 if (IS_ERR(req))
771 return PTR_ERR(req);
773 fuse_lk_fill(req, file, fl, FUSE_GETLK, 0, 0);
774 req->out.numargs = 1;
775 req->out.args[0].size = sizeof(outarg);
776 req->out.args[0].value = &outarg;
777 request_send(fc, req);
778 err = req->out.h.error;
779 fuse_put_request(fc, req);
780 if (!err)
781 err = convert_fuse_file_lock(&outarg.lk, fl);
783 return err;
786 static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
788 struct inode *inode = file->f_path.dentry->d_inode;
789 struct fuse_conn *fc = get_fuse_conn(inode);
790 struct fuse_req *req;
791 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
792 pid_t pid = fl->fl_type != F_UNLCK ? current->tgid : 0;
793 int err;
795 /* Unlock on close is handled by the flush method */
796 if (fl->fl_flags & FL_CLOSE)
797 return 0;
799 req = fuse_get_req(fc);
800 if (IS_ERR(req))
801 return PTR_ERR(req);
803 fuse_lk_fill(req, file, fl, opcode, pid, flock);
804 request_send(fc, req);
805 err = req->out.h.error;
806 /* locking is restartable */
807 if (err == -EINTR)
808 err = -ERESTARTSYS;
809 fuse_put_request(fc, req);
810 return err;
813 static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
815 struct inode *inode = file->f_path.dentry->d_inode;
816 struct fuse_conn *fc = get_fuse_conn(inode);
817 int err;
819 if (cmd == F_GETLK) {
820 if (fc->no_lock) {
821 posix_test_lock(file, fl);
822 err = 0;
823 } else
824 err = fuse_getlk(file, fl);
825 } else {
826 if (fc->no_lock)
827 err = posix_lock_file_wait(file, fl);
828 else
829 err = fuse_setlk(file, fl, 0);
831 return err;
834 static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
836 struct inode *inode = file->f_path.dentry->d_inode;
837 struct fuse_conn *fc = get_fuse_conn(inode);
838 int err;
840 if (fc->no_lock) {
841 err = flock_lock_file_wait(file, fl);
842 } else {
843 /* emulate flock with POSIX locks */
844 fl->fl_owner = (fl_owner_t) file;
845 err = fuse_setlk(file, fl, 1);
848 return err;
851 static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
853 struct inode *inode = mapping->host;
854 struct fuse_conn *fc = get_fuse_conn(inode);
855 struct fuse_req *req;
856 struct fuse_bmap_in inarg;
857 struct fuse_bmap_out outarg;
858 int err;
860 if (!inode->i_sb->s_bdev || fc->no_bmap)
861 return 0;
863 req = fuse_get_req(fc);
864 if (IS_ERR(req))
865 return 0;
867 memset(&inarg, 0, sizeof(inarg));
868 inarg.block = block;
869 inarg.blocksize = inode->i_sb->s_blocksize;
870 req->in.h.opcode = FUSE_BMAP;
871 req->in.h.nodeid = get_node_id(inode);
872 req->in.numargs = 1;
873 req->in.args[0].size = sizeof(inarg);
874 req->in.args[0].value = &inarg;
875 req->out.numargs = 1;
876 req->out.args[0].size = sizeof(outarg);
877 req->out.args[0].value = &outarg;
878 request_send(fc, req);
879 err = req->out.h.error;
880 fuse_put_request(fc, req);
881 if (err == -ENOSYS)
882 fc->no_bmap = 1;
884 return err ? 0 : outarg.block;
887 static const struct file_operations fuse_file_operations = {
888 .llseek = generic_file_llseek,
889 .read = do_sync_read,
890 .aio_read = generic_file_aio_read,
891 .write = do_sync_write,
892 .aio_write = generic_file_aio_write,
893 .mmap = fuse_file_mmap,
894 .open = fuse_open,
895 .flush = fuse_flush,
896 .release = fuse_release,
897 .fsync = fuse_fsync,
898 .lock = fuse_file_lock,
899 .flock = fuse_file_flock,
900 .splice_read = generic_file_splice_read,
903 static const struct file_operations fuse_direct_io_file_operations = {
904 .llseek = generic_file_llseek,
905 .read = fuse_direct_read,
906 .write = fuse_direct_write,
907 .open = fuse_open,
908 .flush = fuse_flush,
909 .release = fuse_release,
910 .fsync = fuse_fsync,
911 .lock = fuse_file_lock,
912 .flock = fuse_file_flock,
913 /* no mmap and splice_read */
916 static const struct address_space_operations fuse_file_aops = {
917 .readpage = fuse_readpage,
918 .write_begin = fuse_write_begin,
919 .write_end = fuse_write_end,
920 .readpages = fuse_readpages,
921 .set_page_dirty = fuse_set_page_dirty,
922 .bmap = fuse_bmap,
925 void fuse_init_file_inode(struct inode *inode)
927 inode->i_fop = &fuse_file_operations;
928 inode->i_data.a_ops = &fuse_file_aops;