2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2005 Miklos Szeredi <miklos@szeredi.hu>
5 This program can be distributed under the terms of the GNU GPL.
11 #include <linux/pagemap.h>
12 #include <linux/file.h>
13 #include <linux/gfp.h>
14 #include <linux/sched.h>
15 #include <linux/namei.h>
18 * FUSE caches dentries and attributes with separate timeout. The
19 * time in jiffies until the dentry/attributes are valid is stored in
20 * dentry->d_time and fuse_inode->i_time respectively.
24 * Calculate the time in jiffies until a dentry/attributes are valid
26 static inline unsigned long time_to_jiffies(unsigned long sec,
29 struct timespec ts = {sec, nsec};
30 return jiffies + timespec_to_jiffies(&ts);
34 * Set dentry and possibly attribute timeouts from the lookup/mk*
37 static void fuse_change_timeout(struct dentry *entry, struct fuse_entry_out *o)
39 entry->d_time = time_to_jiffies(o->entry_valid, o->entry_valid_nsec);
41 get_fuse_inode(entry->d_inode)->i_time =
42 time_to_jiffies(o->attr_valid, o->attr_valid_nsec);
46 * Mark the attributes as stale, so that at the next call to
47 * ->getattr() they will be fetched from userspace
49 void fuse_invalidate_attr(struct inode *inode)
51 get_fuse_inode(inode)->i_time = jiffies - 1;
55 * Just mark the entry as stale, so that a next attempt to look it up
56 * will result in a new lookup call to userspace
58 * This is called when a dentry is about to become negative and the
59 * timeout is unknown (unlink, rmdir, rename and in some cases
62 static void fuse_invalidate_entry_cache(struct dentry *entry)
64 entry->d_time = jiffies - 1;
68 * Same as fuse_invalidate_entry_cache(), but also try to remove the
69 * dentry from the hash
71 static void fuse_invalidate_entry(struct dentry *entry)
74 fuse_invalidate_entry_cache(entry);
77 static void fuse_lookup_init(struct fuse_req *req, struct inode *dir,
79 struct fuse_entry_out *outarg)
81 req->in.h.opcode = FUSE_LOOKUP;
82 req->in.h.nodeid = get_node_id(dir);
85 req->in.args[0].size = entry->d_name.len + 1;
86 req->in.args[0].value = entry->d_name.name;
88 req->out.args[0].size = sizeof(struct fuse_entry_out);
89 req->out.args[0].value = outarg;
93 * Check whether the dentry is still valid
95 * If the entry validity timeout has expired and the dentry is
96 * positive, try to redo the lookup. If the lookup results in a
97 * different inode, then let the VFS invalidate the dentry and redo
98 * the lookup once more. If the lookup results in the same inode,
99 * then refresh the attributes, timeouts and mark the dentry valid.
101 static int fuse_dentry_revalidate(struct dentry *entry, struct nameidata *nd)
103 struct inode *inode = entry->d_inode;
105 if (inode && is_bad_inode(inode))
107 else if (time_after(jiffies, entry->d_time)) {
109 struct fuse_entry_out outarg;
110 struct fuse_conn *fc;
111 struct fuse_req *req;
113 /* Doesn't hurt to "reset" the validity timeout */
114 fuse_invalidate_entry_cache(entry);
118 fc = get_fuse_conn(inode);
119 req = fuse_get_request(fc);
123 fuse_lookup_init(req, entry->d_parent->d_inode, entry, &outarg);
124 request_send(fc, req);
125 err = req->out.h.error;
127 struct fuse_inode *fi = get_fuse_inode(inode);
128 if (outarg.nodeid != get_node_id(inode)) {
129 fuse_send_forget(fc, req, outarg.nodeid, 1);
134 fuse_put_request(fc, req);
135 if (err || (outarg.attr.mode ^ inode->i_mode) & S_IFMT)
138 fuse_change_attributes(inode, &outarg.attr);
139 fuse_change_timeout(entry, &outarg);
145 * Check if there's already a hashed alias of this directory inode.
146 * If yes, then lookup and mkdir must not create a new alias.
148 static int dir_alias(struct inode *inode)
150 if (S_ISDIR(inode->i_mode)) {
151 struct dentry *alias = d_find_alias(inode);
160 static inline int invalid_nodeid(u64 nodeid)
162 return !nodeid || nodeid == FUSE_ROOT_ID;
165 static struct dentry_operations fuse_dentry_operations = {
166 .d_revalidate = fuse_dentry_revalidate,
169 static inline int valid_mode(int m)
171 return S_ISREG(m) || S_ISDIR(m) || S_ISLNK(m) || S_ISCHR(m) ||
172 S_ISBLK(m) || S_ISFIFO(m) || S_ISSOCK(m);
175 static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry,
176 struct nameidata *nd)
179 struct fuse_entry_out outarg;
180 struct inode *inode = NULL;
181 struct fuse_conn *fc = get_fuse_conn(dir);
182 struct fuse_req *req;
184 if (entry->d_name.len > FUSE_NAME_MAX)
185 return ERR_PTR(-ENAMETOOLONG);
187 req = fuse_get_request(fc);
189 return ERR_PTR(-EINTR);
191 fuse_lookup_init(req, dir, entry, &outarg);
192 request_send(fc, req);
193 err = req->out.h.error;
194 if (!err && ((outarg.nodeid && invalid_nodeid(outarg.nodeid)) ||
195 !valid_mode(outarg.attr.mode)))
197 if (!err && outarg.nodeid) {
198 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
201 fuse_send_forget(fc, req, outarg.nodeid, 1);
202 return ERR_PTR(-ENOMEM);
205 fuse_put_request(fc, req);
206 if (err && err != -ENOENT)
209 if (inode && dir_alias(inode)) {
211 return ERR_PTR(-EIO);
214 entry->d_op = &fuse_dentry_operations;
216 fuse_change_timeout(entry, &outarg);
218 fuse_invalidate_entry_cache(entry);
223 * Atomic create+open operation
225 * If the filesystem doesn't support this, then fall back to separate
226 * 'mknod' + 'open' requests.
228 static int fuse_create_open(struct inode *dir, struct dentry *entry, int mode,
229 struct nameidata *nd)
233 struct fuse_conn *fc = get_fuse_conn(dir);
234 struct fuse_req *req;
235 struct fuse_open_in inarg;
236 struct fuse_open_out outopen;
237 struct fuse_entry_out outentry;
238 struct fuse_file *ff;
240 int flags = nd->intent.open.flags - 1;
247 req = fuse_get_request(fc);
251 ff = fuse_file_alloc();
253 goto out_put_request;
256 memset(&inarg, 0, sizeof(inarg));
259 req->in.h.opcode = FUSE_CREATE;
260 req->in.h.nodeid = get_node_id(dir);
263 req->in.args[0].size = sizeof(inarg);
264 req->in.args[0].value = &inarg;
265 req->in.args[1].size = entry->d_name.len + 1;
266 req->in.args[1].value = entry->d_name.name;
267 req->out.numargs = 2;
268 req->out.args[0].size = sizeof(outentry);
269 req->out.args[0].value = &outentry;
270 req->out.args[1].size = sizeof(outopen);
271 req->out.args[1].value = &outopen;
272 request_send(fc, req);
273 err = req->out.h.error;
281 if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid))
284 inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation,
288 flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
290 /* Special release, with inode = NULL, this will
291 trigger a 'forget' request when the release is
293 fuse_send_release(fc, ff, outentry.nodeid, NULL, flags, 0);
294 goto out_put_request;
296 fuse_put_request(fc, req);
297 d_instantiate(entry, inode);
298 fuse_change_timeout(entry, &outentry);
299 file = lookup_instantiate_filp(nd, entry, generic_file_open);
302 fuse_send_release(fc, ff, outentry.nodeid, inode, flags, 0);
303 return PTR_ERR(file);
305 fuse_finish_open(inode, file, ff, &outopen);
311 fuse_put_request(fc, req);
317 * Code shared between mknod, mkdir, symlink and link
319 static int create_new_entry(struct fuse_conn *fc, struct fuse_req *req,
320 struct inode *dir, struct dentry *entry,
323 struct fuse_entry_out outarg;
327 req->in.h.nodeid = get_node_id(dir);
329 req->out.numargs = 1;
330 req->out.args[0].size = sizeof(outarg);
331 req->out.args[0].value = &outarg;
332 request_send(fc, req);
333 err = req->out.h.error;
335 fuse_put_request(fc, req);
339 if (invalid_nodeid(outarg.nodeid))
340 goto out_put_request;
342 if ((outarg.attr.mode ^ mode) & S_IFMT)
343 goto out_put_request;
345 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
348 fuse_send_forget(fc, req, outarg.nodeid, 1);
351 fuse_put_request(fc, req);
353 if (dir_alias(inode)) {
358 d_instantiate(entry, inode);
359 fuse_change_timeout(entry, &outarg);
360 fuse_invalidate_attr(dir);
364 fuse_put_request(fc, req);
368 static int fuse_mknod(struct inode *dir, struct dentry *entry, int mode,
371 struct fuse_mknod_in inarg;
372 struct fuse_conn *fc = get_fuse_conn(dir);
373 struct fuse_req *req = fuse_get_request(fc);
377 memset(&inarg, 0, sizeof(inarg));
379 inarg.rdev = new_encode_dev(rdev);
380 req->in.h.opcode = FUSE_MKNOD;
382 req->in.args[0].size = sizeof(inarg);
383 req->in.args[0].value = &inarg;
384 req->in.args[1].size = entry->d_name.len + 1;
385 req->in.args[1].value = entry->d_name.name;
386 return create_new_entry(fc, req, dir, entry, mode);
389 static int fuse_create(struct inode *dir, struct dentry *entry, int mode,
390 struct nameidata *nd)
392 if (nd && (nd->flags & LOOKUP_CREATE)) {
393 int err = fuse_create_open(dir, entry, mode, nd);
396 /* Fall back on mknod */
398 return fuse_mknod(dir, entry, mode, 0);
401 static int fuse_mkdir(struct inode *dir, struct dentry *entry, int mode)
403 struct fuse_mkdir_in inarg;
404 struct fuse_conn *fc = get_fuse_conn(dir);
405 struct fuse_req *req = fuse_get_request(fc);
409 memset(&inarg, 0, sizeof(inarg));
411 req->in.h.opcode = FUSE_MKDIR;
413 req->in.args[0].size = sizeof(inarg);
414 req->in.args[0].value = &inarg;
415 req->in.args[1].size = entry->d_name.len + 1;
416 req->in.args[1].value = entry->d_name.name;
417 return create_new_entry(fc, req, dir, entry, S_IFDIR);
420 static int fuse_symlink(struct inode *dir, struct dentry *entry,
423 struct fuse_conn *fc = get_fuse_conn(dir);
424 unsigned len = strlen(link) + 1;
425 struct fuse_req *req = fuse_get_request(fc);
429 req->in.h.opcode = FUSE_SYMLINK;
431 req->in.args[0].size = entry->d_name.len + 1;
432 req->in.args[0].value = entry->d_name.name;
433 req->in.args[1].size = len;
434 req->in.args[1].value = link;
435 return create_new_entry(fc, req, dir, entry, S_IFLNK);
438 static int fuse_unlink(struct inode *dir, struct dentry *entry)
441 struct fuse_conn *fc = get_fuse_conn(dir);
442 struct fuse_req *req = fuse_get_request(fc);
446 req->in.h.opcode = FUSE_UNLINK;
447 req->in.h.nodeid = get_node_id(dir);
450 req->in.args[0].size = entry->d_name.len + 1;
451 req->in.args[0].value = entry->d_name.name;
452 request_send(fc, req);
453 err = req->out.h.error;
454 fuse_put_request(fc, req);
456 struct inode *inode = entry->d_inode;
458 /* Set nlink to zero so the inode can be cleared, if
459 the inode does have more links this will be
460 discovered at the next lookup/getattr */
462 fuse_invalidate_attr(inode);
463 fuse_invalidate_attr(dir);
464 fuse_invalidate_entry_cache(entry);
465 } else if (err == -EINTR)
466 fuse_invalidate_entry(entry);
470 static int fuse_rmdir(struct inode *dir, struct dentry *entry)
473 struct fuse_conn *fc = get_fuse_conn(dir);
474 struct fuse_req *req = fuse_get_request(fc);
478 req->in.h.opcode = FUSE_RMDIR;
479 req->in.h.nodeid = get_node_id(dir);
482 req->in.args[0].size = entry->d_name.len + 1;
483 req->in.args[0].value = entry->d_name.name;
484 request_send(fc, req);
485 err = req->out.h.error;
486 fuse_put_request(fc, req);
488 entry->d_inode->i_nlink = 0;
489 fuse_invalidate_attr(dir);
490 fuse_invalidate_entry_cache(entry);
491 } else if (err == -EINTR)
492 fuse_invalidate_entry(entry);
496 static int fuse_rename(struct inode *olddir, struct dentry *oldent,
497 struct inode *newdir, struct dentry *newent)
500 struct fuse_rename_in inarg;
501 struct fuse_conn *fc = get_fuse_conn(olddir);
502 struct fuse_req *req = fuse_get_request(fc);
506 memset(&inarg, 0, sizeof(inarg));
507 inarg.newdir = get_node_id(newdir);
508 req->in.h.opcode = FUSE_RENAME;
509 req->in.h.nodeid = get_node_id(olddir);
511 req->inode2 = newdir;
513 req->in.args[0].size = sizeof(inarg);
514 req->in.args[0].value = &inarg;
515 req->in.args[1].size = oldent->d_name.len + 1;
516 req->in.args[1].value = oldent->d_name.name;
517 req->in.args[2].size = newent->d_name.len + 1;
518 req->in.args[2].value = newent->d_name.name;
519 request_send(fc, req);
520 err = req->out.h.error;
521 fuse_put_request(fc, req);
523 fuse_invalidate_attr(olddir);
524 if (olddir != newdir)
525 fuse_invalidate_attr(newdir);
527 /* newent will end up negative */
529 fuse_invalidate_entry_cache(newent);
530 } else if (err == -EINTR) {
531 /* If request was interrupted, DEITY only knows if the
532 rename actually took place. If the invalidation
533 fails (e.g. some process has CWD under the renamed
534 directory), then there can be inconsistency between
535 the dcache and the real filesystem. Tough luck. */
536 fuse_invalidate_entry(oldent);
538 fuse_invalidate_entry(newent);
544 static int fuse_link(struct dentry *entry, struct inode *newdir,
545 struct dentry *newent)
548 struct fuse_link_in inarg;
549 struct inode *inode = entry->d_inode;
550 struct fuse_conn *fc = get_fuse_conn(inode);
551 struct fuse_req *req = fuse_get_request(fc);
555 memset(&inarg, 0, sizeof(inarg));
556 inarg.oldnodeid = get_node_id(inode);
557 req->in.h.opcode = FUSE_LINK;
560 req->in.args[0].size = sizeof(inarg);
561 req->in.args[0].value = &inarg;
562 req->in.args[1].size = newent->d_name.len + 1;
563 req->in.args[1].value = newent->d_name.name;
564 err = create_new_entry(fc, req, newdir, newent, inode->i_mode);
565 /* Contrary to "normal" filesystems it can happen that link
566 makes two "logical" inodes point to the same "physical"
567 inode. We invalidate the attributes of the old one, so it
568 will reflect changes in the backing inode (link count,
571 if (!err || err == -EINTR)
572 fuse_invalidate_attr(inode);
576 int fuse_do_getattr(struct inode *inode)
579 struct fuse_attr_out arg;
580 struct fuse_conn *fc = get_fuse_conn(inode);
581 struct fuse_req *req = fuse_get_request(fc);
585 req->in.h.opcode = FUSE_GETATTR;
586 req->in.h.nodeid = get_node_id(inode);
588 req->out.numargs = 1;
589 req->out.args[0].size = sizeof(arg);
590 req->out.args[0].value = &arg;
591 request_send(fc, req);
592 err = req->out.h.error;
593 fuse_put_request(fc, req);
595 if ((inode->i_mode ^ arg.attr.mode) & S_IFMT) {
596 make_bad_inode(inode);
599 struct fuse_inode *fi = get_fuse_inode(inode);
600 fuse_change_attributes(inode, &arg.attr);
601 fi->i_time = time_to_jiffies(arg.attr_valid,
602 arg.attr_valid_nsec);
609 * Calling into a user-controlled filesystem gives the filesystem
610 * daemon ptrace-like capabilities over the requester process. This
611 * means, that the filesystem daemon is able to record the exact
612 * filesystem operations performed, and can also control the behavior
613 * of the requester process in otherwise impossible ways. For example
614 * it can delay the operation for arbitrary length of time allowing
615 * DoS against the requester.
617 * For this reason only those processes can call into the filesystem,
618 * for which the owner of the mount has ptrace privilege. This
619 * excludes processes started by other users, suid or sgid processes.
621 static int fuse_allow_task(struct fuse_conn *fc, struct task_struct *task)
623 if (fc->flags & FUSE_ALLOW_OTHER)
626 if (task->euid == fc->user_id &&
627 task->suid == fc->user_id &&
628 task->uid == fc->user_id &&
629 task->egid == fc->group_id &&
630 task->sgid == fc->group_id &&
631 task->gid == fc->group_id)
638 * Check whether the inode attributes are still valid
640 * If the attribute validity timeout has expired, then fetch the fresh
641 * attributes with a 'getattr' request
643 * I'm not sure why cached attributes are never returned for the root
644 * inode, this is probably being too cautious.
646 static int fuse_revalidate(struct dentry *entry)
648 struct inode *inode = entry->d_inode;
649 struct fuse_inode *fi = get_fuse_inode(inode);
650 struct fuse_conn *fc = get_fuse_conn(inode);
652 if (!fuse_allow_task(fc, current))
654 if (get_node_id(inode) != FUSE_ROOT_ID &&
655 time_before_eq(jiffies, fi->i_time))
658 return fuse_do_getattr(inode);
661 static int fuse_access(struct inode *inode, int mask)
663 struct fuse_conn *fc = get_fuse_conn(inode);
664 struct fuse_req *req;
665 struct fuse_access_in inarg;
671 req = fuse_get_request(fc);
675 memset(&inarg, 0, sizeof(inarg));
677 req->in.h.opcode = FUSE_ACCESS;
678 req->in.h.nodeid = get_node_id(inode);
681 req->in.args[0].size = sizeof(inarg);
682 req->in.args[0].value = &inarg;
683 request_send(fc, req);
684 err = req->out.h.error;
685 fuse_put_request(fc, req);
686 if (err == -ENOSYS) {
694 * Check permission. The two basic access models of FUSE are:
696 * 1) Local access checking ('default_permissions' mount option) based
697 * on file mode. This is the plain old disk filesystem permission
700 * 2) "Remote" access checking, where server is responsible for
701 * checking permission in each inode operation. An exception to this
702 * is if ->permission() was invoked from sys_access() in which case an
703 * access request is sent. Execute permission is still checked
704 * locally based on file mode.
706 static int fuse_permission(struct inode *inode, int mask, struct nameidata *nd)
708 struct fuse_conn *fc = get_fuse_conn(inode);
710 if (!fuse_allow_task(fc, current))
712 else if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
713 int err = generic_permission(inode, mask, NULL);
715 /* If permission is denied, try to refresh file
716 attributes. This is also needed, because the root
717 node will at first have no permissions */
718 if (err == -EACCES) {
719 err = fuse_do_getattr(inode);
721 err = generic_permission(inode, mask, NULL);
724 /* Note: the opposite of the above test does not
725 exist. So if permissions are revoked this won't be
726 noticed immediately, only after the attribute
727 timeout has expired */
731 int mode = inode->i_mode;
732 if ((mask & MAY_EXEC) && !S_ISDIR(mode) && !(mode & S_IXUGO))
735 if (nd && (nd->flags & LOOKUP_ACCESS))
736 return fuse_access(inode, mask);
741 static int parse_dirfile(char *buf, size_t nbytes, struct file *file,
742 void *dstbuf, filldir_t filldir)
744 while (nbytes >= FUSE_NAME_OFFSET) {
745 struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
746 size_t reclen = FUSE_DIRENT_SIZE(dirent);
748 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
753 over = filldir(dstbuf, dirent->name, dirent->namelen,
754 file->f_pos, dirent->ino, dirent->type);
760 file->f_pos = dirent->off;
766 static inline size_t fuse_send_readdir(struct fuse_req *req, struct file *file,
767 struct inode *inode, loff_t pos,
770 return fuse_send_read_common(req, file, inode, pos, count, 1);
773 static int fuse_readdir(struct file *file, void *dstbuf, filldir_t filldir)
778 struct inode *inode = file->f_dentry->d_inode;
779 struct fuse_conn *fc = get_fuse_conn(inode);
780 struct fuse_req *req;
782 if (is_bad_inode(inode))
785 req = fuse_get_request(fc);
789 page = alloc_page(GFP_KERNEL);
791 fuse_put_request(fc, req);
795 req->pages[0] = page;
796 nbytes = fuse_send_readdir(req, file, inode, file->f_pos, PAGE_SIZE);
797 err = req->out.h.error;
798 fuse_put_request(fc, req);
800 err = parse_dirfile(page_address(page), nbytes, file, dstbuf,
804 fuse_invalidate_attr(inode); /* atime changed */
808 static char *read_link(struct dentry *dentry)
810 struct inode *inode = dentry->d_inode;
811 struct fuse_conn *fc = get_fuse_conn(inode);
812 struct fuse_req *req = fuse_get_request(fc);
816 return ERR_PTR(-EINTR);
818 link = (char *) __get_free_page(GFP_KERNEL);
820 link = ERR_PTR(-ENOMEM);
823 req->in.h.opcode = FUSE_READLINK;
824 req->in.h.nodeid = get_node_id(inode);
827 req->out.numargs = 1;
828 req->out.args[0].size = PAGE_SIZE - 1;
829 req->out.args[0].value = link;
830 request_send(fc, req);
831 if (req->out.h.error) {
832 free_page((unsigned long) link);
833 link = ERR_PTR(req->out.h.error);
835 link[req->out.args[0].size] = '\0';
837 fuse_put_request(fc, req);
838 fuse_invalidate_attr(inode); /* atime changed */
842 static void free_link(char *link)
845 free_page((unsigned long) link);
848 static void *fuse_follow_link(struct dentry *dentry, struct nameidata *nd)
850 nd_set_link(nd, read_link(dentry));
854 static void fuse_put_link(struct dentry *dentry, struct nameidata *nd, void *c)
856 free_link(nd_get_link(nd));
859 static int fuse_dir_open(struct inode *inode, struct file *file)
861 return fuse_open_common(inode, file, 1);
864 static int fuse_dir_release(struct inode *inode, struct file *file)
866 return fuse_release_common(inode, file, 1);
869 static int fuse_dir_fsync(struct file *file, struct dentry *de, int datasync)
871 /* nfsd can call this with no file */
872 return file ? fuse_fsync_common(file, de, datasync, 1) : 0;
875 static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg)
877 unsigned ivalid = iattr->ia_valid;
879 if (ivalid & ATTR_MODE)
880 arg->valid |= FATTR_MODE, arg->mode = iattr->ia_mode;
881 if (ivalid & ATTR_UID)
882 arg->valid |= FATTR_UID, arg->uid = iattr->ia_uid;
883 if (ivalid & ATTR_GID)
884 arg->valid |= FATTR_GID, arg->gid = iattr->ia_gid;
885 if (ivalid & ATTR_SIZE)
886 arg->valid |= FATTR_SIZE, arg->size = iattr->ia_size;
887 /* You can only _set_ these together (they may change by themselves) */
888 if ((ivalid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME)) {
889 arg->valid |= FATTR_ATIME | FATTR_MTIME;
890 arg->atime = iattr->ia_atime.tv_sec;
891 arg->mtime = iattr->ia_mtime.tv_sec;
893 if (ivalid & ATTR_FILE) {
894 struct fuse_file *ff = iattr->ia_file->private_data;
895 arg->valid |= FATTR_FH;
901 * Set attributes, and at the same time refresh them.
903 * Truncation is slightly complicated, because the 'truncate' request
904 * may fail, in which case we don't want to touch the mapping.
905 * vmtruncate() doesn't allow for this case. So do the rlimit
906 * checking by hand and call vmtruncate() only after the file has
907 * actually been truncated.
909 static int fuse_setattr(struct dentry *entry, struct iattr *attr)
911 struct inode *inode = entry->d_inode;
912 struct fuse_conn *fc = get_fuse_conn(inode);
913 struct fuse_inode *fi = get_fuse_inode(inode);
914 struct fuse_req *req;
915 struct fuse_setattr_in inarg;
916 struct fuse_attr_out outarg;
920 if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
921 err = inode_change_ok(inode, attr);
926 if (attr->ia_valid & ATTR_SIZE) {
929 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
930 if (limit != RLIM_INFINITY && attr->ia_size > (loff_t) limit) {
931 send_sig(SIGXFSZ, current, 0);
936 req = fuse_get_request(fc);
940 memset(&inarg, 0, sizeof(inarg));
941 iattr_to_fattr(attr, &inarg);
942 req->in.h.opcode = FUSE_SETATTR;
943 req->in.h.nodeid = get_node_id(inode);
946 req->in.args[0].size = sizeof(inarg);
947 req->in.args[0].value = &inarg;
948 req->out.numargs = 1;
949 req->out.args[0].size = sizeof(outarg);
950 req->out.args[0].value = &outarg;
951 request_send(fc, req);
952 err = req->out.h.error;
953 fuse_put_request(fc, req);
955 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
956 make_bad_inode(inode);
960 loff_t origsize = i_size_read(inode);
961 i_size_write(inode, outarg.attr.size);
962 if (origsize > outarg.attr.size)
963 vmtruncate(inode, outarg.attr.size);
965 fuse_change_attributes(inode, &outarg.attr);
966 fi->i_time = time_to_jiffies(outarg.attr_valid,
967 outarg.attr_valid_nsec);
969 } else if (err == -EINTR)
970 fuse_invalidate_attr(inode);
975 static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry,
978 struct inode *inode = entry->d_inode;
979 int err = fuse_revalidate(entry);
981 generic_fillattr(inode, stat);
986 static int fuse_setxattr(struct dentry *entry, const char *name,
987 const void *value, size_t size, int flags)
989 struct inode *inode = entry->d_inode;
990 struct fuse_conn *fc = get_fuse_conn(inode);
991 struct fuse_req *req;
992 struct fuse_setxattr_in inarg;
998 req = fuse_get_request(fc);
1002 memset(&inarg, 0, sizeof(inarg));
1004 inarg.flags = flags;
1005 req->in.h.opcode = FUSE_SETXATTR;
1006 req->in.h.nodeid = get_node_id(inode);
1008 req->in.numargs = 3;
1009 req->in.args[0].size = sizeof(inarg);
1010 req->in.args[0].value = &inarg;
1011 req->in.args[1].size = strlen(name) + 1;
1012 req->in.args[1].value = name;
1013 req->in.args[2].size = size;
1014 req->in.args[2].value = value;
1015 request_send(fc, req);
1016 err = req->out.h.error;
1017 fuse_put_request(fc, req);
1018 if (err == -ENOSYS) {
1019 fc->no_setxattr = 1;
1025 static ssize_t fuse_getxattr(struct dentry *entry, const char *name,
1026 void *value, size_t size)
1028 struct inode *inode = entry->d_inode;
1029 struct fuse_conn *fc = get_fuse_conn(inode);
1030 struct fuse_req *req;
1031 struct fuse_getxattr_in inarg;
1032 struct fuse_getxattr_out outarg;
1035 if (fc->no_getxattr)
1038 req = fuse_get_request(fc);
1042 memset(&inarg, 0, sizeof(inarg));
1044 req->in.h.opcode = FUSE_GETXATTR;
1045 req->in.h.nodeid = get_node_id(inode);
1047 req->in.numargs = 2;
1048 req->in.args[0].size = sizeof(inarg);
1049 req->in.args[0].value = &inarg;
1050 req->in.args[1].size = strlen(name) + 1;
1051 req->in.args[1].value = name;
1052 /* This is really two different operations rolled into one */
1053 req->out.numargs = 1;
1055 req->out.argvar = 1;
1056 req->out.args[0].size = size;
1057 req->out.args[0].value = value;
1059 req->out.args[0].size = sizeof(outarg);
1060 req->out.args[0].value = &outarg;
1062 request_send(fc, req);
1063 ret = req->out.h.error;
1065 ret = size ? req->out.args[0].size : outarg.size;
1067 if (ret == -ENOSYS) {
1068 fc->no_getxattr = 1;
1072 fuse_put_request(fc, req);
1076 static ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size)
1078 struct inode *inode = entry->d_inode;
1079 struct fuse_conn *fc = get_fuse_conn(inode);
1080 struct fuse_req *req;
1081 struct fuse_getxattr_in inarg;
1082 struct fuse_getxattr_out outarg;
1085 if (fc->no_listxattr)
1088 req = fuse_get_request(fc);
1092 memset(&inarg, 0, sizeof(inarg));
1094 req->in.h.opcode = FUSE_LISTXATTR;
1095 req->in.h.nodeid = get_node_id(inode);
1097 req->in.numargs = 1;
1098 req->in.args[0].size = sizeof(inarg);
1099 req->in.args[0].value = &inarg;
1100 /* This is really two different operations rolled into one */
1101 req->out.numargs = 1;
1103 req->out.argvar = 1;
1104 req->out.args[0].size = size;
1105 req->out.args[0].value = list;
1107 req->out.args[0].size = sizeof(outarg);
1108 req->out.args[0].value = &outarg;
1110 request_send(fc, req);
1111 ret = req->out.h.error;
1113 ret = size ? req->out.args[0].size : outarg.size;
1115 if (ret == -ENOSYS) {
1116 fc->no_listxattr = 1;
1120 fuse_put_request(fc, req);
1124 static int fuse_removexattr(struct dentry *entry, const char *name)
1126 struct inode *inode = entry->d_inode;
1127 struct fuse_conn *fc = get_fuse_conn(inode);
1128 struct fuse_req *req;
1131 if (fc->no_removexattr)
1134 req = fuse_get_request(fc);
1138 req->in.h.opcode = FUSE_REMOVEXATTR;
1139 req->in.h.nodeid = get_node_id(inode);
1141 req->in.numargs = 1;
1142 req->in.args[0].size = strlen(name) + 1;
1143 req->in.args[0].value = name;
1144 request_send(fc, req);
1145 err = req->out.h.error;
1146 fuse_put_request(fc, req);
1147 if (err == -ENOSYS) {
1148 fc->no_removexattr = 1;
1154 static struct inode_operations fuse_dir_inode_operations = {
1155 .lookup = fuse_lookup,
1156 .mkdir = fuse_mkdir,
1157 .symlink = fuse_symlink,
1158 .unlink = fuse_unlink,
1159 .rmdir = fuse_rmdir,
1160 .rename = fuse_rename,
1162 .setattr = fuse_setattr,
1163 .create = fuse_create,
1164 .mknod = fuse_mknod,
1165 .permission = fuse_permission,
1166 .getattr = fuse_getattr,
1167 .setxattr = fuse_setxattr,
1168 .getxattr = fuse_getxattr,
1169 .listxattr = fuse_listxattr,
1170 .removexattr = fuse_removexattr,
1173 static struct file_operations fuse_dir_operations = {
1174 .llseek = generic_file_llseek,
1175 .read = generic_read_dir,
1176 .readdir = fuse_readdir,
1177 .open = fuse_dir_open,
1178 .release = fuse_dir_release,
1179 .fsync = fuse_dir_fsync,
1182 static struct inode_operations fuse_common_inode_operations = {
1183 .setattr = fuse_setattr,
1184 .permission = fuse_permission,
1185 .getattr = fuse_getattr,
1186 .setxattr = fuse_setxattr,
1187 .getxattr = fuse_getxattr,
1188 .listxattr = fuse_listxattr,
1189 .removexattr = fuse_removexattr,
1192 static struct inode_operations fuse_symlink_inode_operations = {
1193 .setattr = fuse_setattr,
1194 .follow_link = fuse_follow_link,
1195 .put_link = fuse_put_link,
1196 .readlink = generic_readlink,
1197 .getattr = fuse_getattr,
1198 .setxattr = fuse_setxattr,
1199 .getxattr = fuse_getxattr,
1200 .listxattr = fuse_listxattr,
1201 .removexattr = fuse_removexattr,
1204 void fuse_init_common(struct inode *inode)
1206 inode->i_op = &fuse_common_inode_operations;
1209 void fuse_init_dir(struct inode *inode)
1211 inode->i_op = &fuse_dir_inode_operations;
1212 inode->i_fop = &fuse_dir_operations;
1215 void fuse_init_symlink(struct inode *inode)
1217 inode->i_op = &fuse_symlink_inode_operations;