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 unsigned long time_to_jiffies(unsigned long sec, unsigned long nsec)
28 struct timespec ts = {sec, nsec};
29 return jiffies + timespec_to_jiffies(&ts);
33 * Set dentry and possibly attribute timeouts from the lookup/mk*
36 static void fuse_change_timeout(struct dentry *entry, struct fuse_entry_out *o)
38 entry->d_time = time_to_jiffies(o->entry_valid, o->entry_valid_nsec);
40 get_fuse_inode(entry->d_inode)->i_time =
41 time_to_jiffies(o->attr_valid, o->attr_valid_nsec);
45 * Mark the attributes as stale, so that at the next call to
46 * ->getattr() they will be fetched from userspace
48 void fuse_invalidate_attr(struct inode *inode)
50 get_fuse_inode(inode)->i_time = jiffies - 1;
54 * Just mark the entry as stale, so that a next attempt to look it up
55 * will result in a new lookup call to userspace
57 * This is called when a dentry is about to become negative and the
58 * timeout is unknown (unlink, rmdir, rename and in some cases
61 static void fuse_invalidate_entry_cache(struct dentry *entry)
63 entry->d_time = jiffies - 1;
67 * Same as fuse_invalidate_entry_cache(), but also try to remove the
68 * dentry from the hash
70 static void fuse_invalidate_entry(struct dentry *entry)
73 fuse_invalidate_entry_cache(entry);
76 static void fuse_lookup_init(struct fuse_req *req, struct inode *dir,
78 struct fuse_entry_out *outarg)
80 req->in.h.opcode = FUSE_LOOKUP;
81 req->in.h.nodeid = get_node_id(dir);
84 req->in.args[0].size = entry->d_name.len + 1;
85 req->in.args[0].value = entry->d_name.name;
87 req->out.args[0].size = sizeof(struct fuse_entry_out);
88 req->out.args[0].value = outarg;
92 * Check whether the dentry is still valid
94 * If the entry validity timeout has expired and the dentry is
95 * positive, try to redo the lookup. If the lookup results in a
96 * different inode, then let the VFS invalidate the dentry and redo
97 * the lookup once more. If the lookup results in the same inode,
98 * then refresh the attributes, timeouts and mark the dentry valid.
100 static int fuse_dentry_revalidate(struct dentry *entry, struct nameidata *nd)
102 struct inode *inode = entry->d_inode;
104 if (inode && is_bad_inode(inode))
106 else if (time_after(jiffies, entry->d_time)) {
108 struct fuse_entry_out outarg;
109 struct fuse_conn *fc;
110 struct fuse_req *req;
112 /* Doesn't hurt to "reset" the validity timeout */
113 fuse_invalidate_entry_cache(entry);
115 /* For negative dentries, always do a fresh lookup */
119 fc = get_fuse_conn(inode);
120 req = fuse_get_req(fc);
124 fuse_lookup_init(req, entry->d_parent->d_inode, entry, &outarg);
125 request_send(fc, req);
126 err = req->out.h.error;
127 /* Zero nodeid is same as -ENOENT */
128 if (!err && !outarg.nodeid)
131 struct fuse_inode *fi = get_fuse_inode(inode);
132 if (outarg.nodeid != get_node_id(inode)) {
133 fuse_send_forget(fc, req, outarg.nodeid, 1);
138 fuse_put_request(fc, req);
139 if (err || (outarg.attr.mode ^ inode->i_mode) & S_IFMT)
142 fuse_change_attributes(inode, &outarg.attr);
143 fuse_change_timeout(entry, &outarg);
149 * Check if there's already a hashed alias of this directory inode.
150 * If yes, then lookup and mkdir must not create a new alias.
152 static int dir_alias(struct inode *inode)
154 if (S_ISDIR(inode->i_mode)) {
155 struct dentry *alias = d_find_alias(inode);
164 static int invalid_nodeid(u64 nodeid)
166 return !nodeid || nodeid == FUSE_ROOT_ID;
169 static struct dentry_operations fuse_dentry_operations = {
170 .d_revalidate = fuse_dentry_revalidate,
173 static int valid_mode(int m)
175 return S_ISREG(m) || S_ISDIR(m) || S_ISLNK(m) || S_ISCHR(m) ||
176 S_ISBLK(m) || S_ISFIFO(m) || S_ISSOCK(m);
179 static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry,
180 struct nameidata *nd)
183 struct fuse_entry_out outarg;
184 struct inode *inode = NULL;
185 struct fuse_conn *fc = get_fuse_conn(dir);
186 struct fuse_req *req;
188 if (entry->d_name.len > FUSE_NAME_MAX)
189 return ERR_PTR(-ENAMETOOLONG);
191 req = fuse_get_req(fc);
193 return ERR_PTR(PTR_ERR(req));
195 fuse_lookup_init(req, dir, entry, &outarg);
196 request_send(fc, req);
197 err = req->out.h.error;
198 /* Zero nodeid is same as -ENOENT, but with valid timeout */
199 if (!err && outarg.nodeid &&
200 (invalid_nodeid(outarg.nodeid) || !valid_mode(outarg.attr.mode)))
202 if (!err && outarg.nodeid) {
203 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
206 fuse_send_forget(fc, req, outarg.nodeid, 1);
207 return ERR_PTR(-ENOMEM);
210 fuse_put_request(fc, req);
211 if (err && err != -ENOENT)
214 if (inode && dir_alias(inode)) {
216 return ERR_PTR(-EIO);
219 entry->d_op = &fuse_dentry_operations;
221 fuse_change_timeout(entry, &outarg);
223 fuse_invalidate_entry_cache(entry);
228 * Atomic create+open operation
230 * If the filesystem doesn't support this, then fall back to separate
231 * 'mknod' + 'open' requests.
233 static int fuse_create_open(struct inode *dir, struct dentry *entry, int mode,
234 struct nameidata *nd)
238 struct fuse_conn *fc = get_fuse_conn(dir);
239 struct fuse_req *req;
240 struct fuse_open_in inarg;
241 struct fuse_open_out outopen;
242 struct fuse_entry_out outentry;
243 struct fuse_file *ff;
245 int flags = nd->intent.open.flags - 1;
250 req = fuse_get_req(fc);
255 ff = fuse_file_alloc();
257 goto out_put_request;
260 memset(&inarg, 0, sizeof(inarg));
263 req->in.h.opcode = FUSE_CREATE;
264 req->in.h.nodeid = get_node_id(dir);
267 req->in.args[0].size = sizeof(inarg);
268 req->in.args[0].value = &inarg;
269 req->in.args[1].size = entry->d_name.len + 1;
270 req->in.args[1].value = entry->d_name.name;
271 req->out.numargs = 2;
272 req->out.args[0].size = sizeof(outentry);
273 req->out.args[0].value = &outentry;
274 req->out.args[1].size = sizeof(outopen);
275 req->out.args[1].value = &outopen;
276 request_send(fc, req);
277 err = req->out.h.error;
285 if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid))
288 inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation,
292 flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
294 /* Special release, with inode = NULL, this will
295 trigger a 'forget' request when the release is
297 fuse_send_release(fc, ff, outentry.nodeid, NULL, flags, 0);
298 goto out_put_request;
300 fuse_put_request(fc, req);
301 d_instantiate(entry, inode);
302 fuse_change_timeout(entry, &outentry);
303 file = lookup_instantiate_filp(nd, entry, generic_file_open);
306 fuse_send_release(fc, ff, outentry.nodeid, inode, flags, 0);
307 return PTR_ERR(file);
309 fuse_finish_open(inode, file, ff, &outopen);
315 fuse_put_request(fc, req);
320 * Code shared between mknod, mkdir, symlink and link
322 static int create_new_entry(struct fuse_conn *fc, struct fuse_req *req,
323 struct inode *dir, struct dentry *entry,
326 struct fuse_entry_out outarg;
330 req->in.h.nodeid = get_node_id(dir);
332 req->out.numargs = 1;
333 req->out.args[0].size = sizeof(outarg);
334 req->out.args[0].value = &outarg;
335 request_send(fc, req);
336 err = req->out.h.error;
338 fuse_put_request(fc, req);
342 if (invalid_nodeid(outarg.nodeid))
343 goto out_put_request;
345 if ((outarg.attr.mode ^ mode) & S_IFMT)
346 goto out_put_request;
348 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
351 fuse_send_forget(fc, req, outarg.nodeid, 1);
354 fuse_put_request(fc, req);
356 if (dir_alias(inode)) {
361 d_instantiate(entry, inode);
362 fuse_change_timeout(entry, &outarg);
363 fuse_invalidate_attr(dir);
367 fuse_put_request(fc, req);
371 static int fuse_mknod(struct inode *dir, struct dentry *entry, int mode,
374 struct fuse_mknod_in inarg;
375 struct fuse_conn *fc = get_fuse_conn(dir);
376 struct fuse_req *req = fuse_get_req(fc);
380 memset(&inarg, 0, sizeof(inarg));
382 inarg.rdev = new_encode_dev(rdev);
383 req->in.h.opcode = FUSE_MKNOD;
385 req->in.args[0].size = sizeof(inarg);
386 req->in.args[0].value = &inarg;
387 req->in.args[1].size = entry->d_name.len + 1;
388 req->in.args[1].value = entry->d_name.name;
389 return create_new_entry(fc, req, dir, entry, mode);
392 static int fuse_create(struct inode *dir, struct dentry *entry, int mode,
393 struct nameidata *nd)
395 if (nd && (nd->flags & LOOKUP_CREATE)) {
396 int err = fuse_create_open(dir, entry, mode, nd);
399 /* Fall back on mknod */
401 return fuse_mknod(dir, entry, mode, 0);
404 static int fuse_mkdir(struct inode *dir, struct dentry *entry, int mode)
406 struct fuse_mkdir_in inarg;
407 struct fuse_conn *fc = get_fuse_conn(dir);
408 struct fuse_req *req = fuse_get_req(fc);
412 memset(&inarg, 0, sizeof(inarg));
414 req->in.h.opcode = FUSE_MKDIR;
416 req->in.args[0].size = sizeof(inarg);
417 req->in.args[0].value = &inarg;
418 req->in.args[1].size = entry->d_name.len + 1;
419 req->in.args[1].value = entry->d_name.name;
420 return create_new_entry(fc, req, dir, entry, S_IFDIR);
423 static int fuse_symlink(struct inode *dir, struct dentry *entry,
426 struct fuse_conn *fc = get_fuse_conn(dir);
427 unsigned len = strlen(link) + 1;
428 struct fuse_req *req = fuse_get_req(fc);
432 req->in.h.opcode = FUSE_SYMLINK;
434 req->in.args[0].size = entry->d_name.len + 1;
435 req->in.args[0].value = entry->d_name.name;
436 req->in.args[1].size = len;
437 req->in.args[1].value = link;
438 return create_new_entry(fc, req, dir, entry, S_IFLNK);
441 static int fuse_unlink(struct inode *dir, struct dentry *entry)
444 struct fuse_conn *fc = get_fuse_conn(dir);
445 struct fuse_req *req = fuse_get_req(fc);
449 req->in.h.opcode = FUSE_UNLINK;
450 req->in.h.nodeid = get_node_id(dir);
453 req->in.args[0].size = entry->d_name.len + 1;
454 req->in.args[0].value = entry->d_name.name;
455 request_send(fc, req);
456 err = req->out.h.error;
457 fuse_put_request(fc, req);
459 struct inode *inode = entry->d_inode;
461 /* Set nlink to zero so the inode can be cleared, if
462 the inode does have more links this will be
463 discovered at the next lookup/getattr */
465 fuse_invalidate_attr(inode);
466 fuse_invalidate_attr(dir);
467 fuse_invalidate_entry_cache(entry);
468 } else if (err == -EINTR)
469 fuse_invalidate_entry(entry);
473 static int fuse_rmdir(struct inode *dir, struct dentry *entry)
476 struct fuse_conn *fc = get_fuse_conn(dir);
477 struct fuse_req *req = fuse_get_req(fc);
481 req->in.h.opcode = FUSE_RMDIR;
482 req->in.h.nodeid = get_node_id(dir);
485 req->in.args[0].size = entry->d_name.len + 1;
486 req->in.args[0].value = entry->d_name.name;
487 request_send(fc, req);
488 err = req->out.h.error;
489 fuse_put_request(fc, req);
491 entry->d_inode->i_nlink = 0;
492 fuse_invalidate_attr(dir);
493 fuse_invalidate_entry_cache(entry);
494 } else if (err == -EINTR)
495 fuse_invalidate_entry(entry);
499 static int fuse_rename(struct inode *olddir, struct dentry *oldent,
500 struct inode *newdir, struct dentry *newent)
503 struct fuse_rename_in inarg;
504 struct fuse_conn *fc = get_fuse_conn(olddir);
505 struct fuse_req *req = fuse_get_req(fc);
509 memset(&inarg, 0, sizeof(inarg));
510 inarg.newdir = get_node_id(newdir);
511 req->in.h.opcode = FUSE_RENAME;
512 req->in.h.nodeid = get_node_id(olddir);
514 req->inode2 = newdir;
516 req->in.args[0].size = sizeof(inarg);
517 req->in.args[0].value = &inarg;
518 req->in.args[1].size = oldent->d_name.len + 1;
519 req->in.args[1].value = oldent->d_name.name;
520 req->in.args[2].size = newent->d_name.len + 1;
521 req->in.args[2].value = newent->d_name.name;
522 request_send(fc, req);
523 err = req->out.h.error;
524 fuse_put_request(fc, req);
526 fuse_invalidate_attr(olddir);
527 if (olddir != newdir)
528 fuse_invalidate_attr(newdir);
530 /* newent will end up negative */
532 fuse_invalidate_entry_cache(newent);
533 } else if (err == -EINTR) {
534 /* If request was interrupted, DEITY only knows if the
535 rename actually took place. If the invalidation
536 fails (e.g. some process has CWD under the renamed
537 directory), then there can be inconsistency between
538 the dcache and the real filesystem. Tough luck. */
539 fuse_invalidate_entry(oldent);
541 fuse_invalidate_entry(newent);
547 static int fuse_link(struct dentry *entry, struct inode *newdir,
548 struct dentry *newent)
551 struct fuse_link_in inarg;
552 struct inode *inode = entry->d_inode;
553 struct fuse_conn *fc = get_fuse_conn(inode);
554 struct fuse_req *req = fuse_get_req(fc);
558 memset(&inarg, 0, sizeof(inarg));
559 inarg.oldnodeid = get_node_id(inode);
560 req->in.h.opcode = FUSE_LINK;
563 req->in.args[0].size = sizeof(inarg);
564 req->in.args[0].value = &inarg;
565 req->in.args[1].size = newent->d_name.len + 1;
566 req->in.args[1].value = newent->d_name.name;
567 err = create_new_entry(fc, req, newdir, newent, inode->i_mode);
568 /* Contrary to "normal" filesystems it can happen that link
569 makes two "logical" inodes point to the same "physical"
570 inode. We invalidate the attributes of the old one, so it
571 will reflect changes in the backing inode (link count,
574 if (!err || err == -EINTR)
575 fuse_invalidate_attr(inode);
579 int fuse_do_getattr(struct inode *inode)
582 struct fuse_attr_out arg;
583 struct fuse_conn *fc = get_fuse_conn(inode);
584 struct fuse_req *req = fuse_get_req(fc);
588 req->in.h.opcode = FUSE_GETATTR;
589 req->in.h.nodeid = get_node_id(inode);
591 req->out.numargs = 1;
592 req->out.args[0].size = sizeof(arg);
593 req->out.args[0].value = &arg;
594 request_send(fc, req);
595 err = req->out.h.error;
596 fuse_put_request(fc, req);
598 if ((inode->i_mode ^ arg.attr.mode) & S_IFMT) {
599 make_bad_inode(inode);
602 struct fuse_inode *fi = get_fuse_inode(inode);
603 fuse_change_attributes(inode, &arg.attr);
604 fi->i_time = time_to_jiffies(arg.attr_valid,
605 arg.attr_valid_nsec);
612 * Calling into a user-controlled filesystem gives the filesystem
613 * daemon ptrace-like capabilities over the requester process. This
614 * means, that the filesystem daemon is able to record the exact
615 * filesystem operations performed, and can also control the behavior
616 * of the requester process in otherwise impossible ways. For example
617 * it can delay the operation for arbitrary length of time allowing
618 * DoS against the requester.
620 * For this reason only those processes can call into the filesystem,
621 * for which the owner of the mount has ptrace privilege. This
622 * excludes processes started by other users, suid or sgid processes.
624 static int fuse_allow_task(struct fuse_conn *fc, struct task_struct *task)
626 if (fc->flags & FUSE_ALLOW_OTHER)
629 if (task->euid == fc->user_id &&
630 task->suid == fc->user_id &&
631 task->uid == fc->user_id &&
632 task->egid == fc->group_id &&
633 task->sgid == fc->group_id &&
634 task->gid == fc->group_id)
641 * Check whether the inode attributes are still valid
643 * If the attribute validity timeout has expired, then fetch the fresh
644 * attributes with a 'getattr' request
646 * I'm not sure why cached attributes are never returned for the root
647 * inode, this is probably being too cautious.
649 static int fuse_revalidate(struct dentry *entry)
651 struct inode *inode = entry->d_inode;
652 struct fuse_inode *fi = get_fuse_inode(inode);
653 struct fuse_conn *fc = get_fuse_conn(inode);
655 if (!fuse_allow_task(fc, current))
657 if (get_node_id(inode) != FUSE_ROOT_ID &&
658 time_before_eq(jiffies, fi->i_time))
661 return fuse_do_getattr(inode);
664 static int fuse_access(struct inode *inode, int mask)
666 struct fuse_conn *fc = get_fuse_conn(inode);
667 struct fuse_req *req;
668 struct fuse_access_in inarg;
674 req = fuse_get_req(fc);
678 memset(&inarg, 0, sizeof(inarg));
680 req->in.h.opcode = FUSE_ACCESS;
681 req->in.h.nodeid = get_node_id(inode);
684 req->in.args[0].size = sizeof(inarg);
685 req->in.args[0].value = &inarg;
686 request_send(fc, req);
687 err = req->out.h.error;
688 fuse_put_request(fc, req);
689 if (err == -ENOSYS) {
697 * Check permission. The two basic access models of FUSE are:
699 * 1) Local access checking ('default_permissions' mount option) based
700 * on file mode. This is the plain old disk filesystem permission
703 * 2) "Remote" access checking, where server is responsible for
704 * checking permission in each inode operation. An exception to this
705 * is if ->permission() was invoked from sys_access() in which case an
706 * access request is sent. Execute permission is still checked
707 * locally based on file mode.
709 static int fuse_permission(struct inode *inode, int mask, struct nameidata *nd)
711 struct fuse_conn *fc = get_fuse_conn(inode);
713 if (!fuse_allow_task(fc, current))
715 else if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
716 int err = generic_permission(inode, mask, NULL);
718 /* If permission is denied, try to refresh file
719 attributes. This is also needed, because the root
720 node will at first have no permissions */
721 if (err == -EACCES) {
722 err = fuse_do_getattr(inode);
724 err = generic_permission(inode, mask, NULL);
727 /* Note: the opposite of the above test does not
728 exist. So if permissions are revoked this won't be
729 noticed immediately, only after the attribute
730 timeout has expired */
734 int mode = inode->i_mode;
735 if ((mask & MAY_EXEC) && !S_ISDIR(mode) && !(mode & S_IXUGO))
738 if (nd && (nd->flags & LOOKUP_ACCESS))
739 return fuse_access(inode, mask);
744 static int parse_dirfile(char *buf, size_t nbytes, struct file *file,
745 void *dstbuf, filldir_t filldir)
747 while (nbytes >= FUSE_NAME_OFFSET) {
748 struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
749 size_t reclen = FUSE_DIRENT_SIZE(dirent);
751 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
756 over = filldir(dstbuf, dirent->name, dirent->namelen,
757 file->f_pos, dirent->ino, dirent->type);
763 file->f_pos = dirent->off;
769 static int fuse_readdir(struct file *file, void *dstbuf, filldir_t filldir)
774 struct inode *inode = file->f_dentry->d_inode;
775 struct fuse_conn *fc = get_fuse_conn(inode);
776 struct fuse_req *req;
778 if (is_bad_inode(inode))
781 req = fuse_get_req(fc);
785 page = alloc_page(GFP_KERNEL);
787 fuse_put_request(fc, req);
791 req->pages[0] = page;
792 fuse_read_fill(req, file, inode, file->f_pos, PAGE_SIZE, FUSE_READDIR);
793 request_send(fc, req);
794 nbytes = req->out.args[0].size;
795 err = req->out.h.error;
796 fuse_put_request(fc, req);
798 err = parse_dirfile(page_address(page), nbytes, file, dstbuf,
802 fuse_invalidate_attr(inode); /* atime changed */
806 static char *read_link(struct dentry *dentry)
808 struct inode *inode = dentry->d_inode;
809 struct fuse_conn *fc = get_fuse_conn(inode);
810 struct fuse_req *req = fuse_get_req(fc);
814 return ERR_PTR(PTR_ERR(req));
816 link = (char *) __get_free_page(GFP_KERNEL);
818 link = ERR_PTR(-ENOMEM);
821 req->in.h.opcode = FUSE_READLINK;
822 req->in.h.nodeid = get_node_id(inode);
825 req->out.numargs = 1;
826 req->out.args[0].size = PAGE_SIZE - 1;
827 req->out.args[0].value = link;
828 request_send(fc, req);
829 if (req->out.h.error) {
830 free_page((unsigned long) link);
831 link = ERR_PTR(req->out.h.error);
833 link[req->out.args[0].size] = '\0';
835 fuse_put_request(fc, req);
836 fuse_invalidate_attr(inode); /* atime changed */
840 static void free_link(char *link)
843 free_page((unsigned long) link);
846 static void *fuse_follow_link(struct dentry *dentry, struct nameidata *nd)
848 nd_set_link(nd, read_link(dentry));
852 static void fuse_put_link(struct dentry *dentry, struct nameidata *nd, void *c)
854 free_link(nd_get_link(nd));
857 static int fuse_dir_open(struct inode *inode, struct file *file)
859 return fuse_open_common(inode, file, 1);
862 static int fuse_dir_release(struct inode *inode, struct file *file)
864 return fuse_release_common(inode, file, 1);
867 static int fuse_dir_fsync(struct file *file, struct dentry *de, int datasync)
869 /* nfsd can call this with no file */
870 return file ? fuse_fsync_common(file, de, datasync, 1) : 0;
873 static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg)
875 unsigned ivalid = iattr->ia_valid;
877 if (ivalid & ATTR_MODE)
878 arg->valid |= FATTR_MODE, arg->mode = iattr->ia_mode;
879 if (ivalid & ATTR_UID)
880 arg->valid |= FATTR_UID, arg->uid = iattr->ia_uid;
881 if (ivalid & ATTR_GID)
882 arg->valid |= FATTR_GID, arg->gid = iattr->ia_gid;
883 if (ivalid & ATTR_SIZE)
884 arg->valid |= FATTR_SIZE, arg->size = iattr->ia_size;
885 /* You can only _set_ these together (they may change by themselves) */
886 if ((ivalid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME)) {
887 arg->valid |= FATTR_ATIME | FATTR_MTIME;
888 arg->atime = iattr->ia_atime.tv_sec;
889 arg->mtime = iattr->ia_mtime.tv_sec;
891 if (ivalid & ATTR_FILE) {
892 struct fuse_file *ff = iattr->ia_file->private_data;
893 arg->valid |= FATTR_FH;
899 * Set attributes, and at the same time refresh them.
901 * Truncation is slightly complicated, because the 'truncate' request
902 * may fail, in which case we don't want to touch the mapping.
903 * vmtruncate() doesn't allow for this case. So do the rlimit
904 * checking by hand and call vmtruncate() only after the file has
905 * actually been truncated.
907 static int fuse_setattr(struct dentry *entry, struct iattr *attr)
909 struct inode *inode = entry->d_inode;
910 struct fuse_conn *fc = get_fuse_conn(inode);
911 struct fuse_inode *fi = get_fuse_inode(inode);
912 struct fuse_req *req;
913 struct fuse_setattr_in inarg;
914 struct fuse_attr_out outarg;
918 if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
919 err = inode_change_ok(inode, attr);
924 if (attr->ia_valid & ATTR_SIZE) {
927 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
928 if (limit != RLIM_INFINITY && attr->ia_size > (loff_t) limit) {
929 send_sig(SIGXFSZ, current, 0);
934 req = fuse_get_req(fc);
938 memset(&inarg, 0, sizeof(inarg));
939 iattr_to_fattr(attr, &inarg);
940 req->in.h.opcode = FUSE_SETATTR;
941 req->in.h.nodeid = get_node_id(inode);
944 req->in.args[0].size = sizeof(inarg);
945 req->in.args[0].value = &inarg;
946 req->out.numargs = 1;
947 req->out.args[0].size = sizeof(outarg);
948 req->out.args[0].value = &outarg;
949 request_send(fc, req);
950 err = req->out.h.error;
951 fuse_put_request(fc, req);
953 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
954 make_bad_inode(inode);
958 loff_t origsize = i_size_read(inode);
959 i_size_write(inode, outarg.attr.size);
960 if (origsize > outarg.attr.size)
961 vmtruncate(inode, outarg.attr.size);
963 fuse_change_attributes(inode, &outarg.attr);
964 fi->i_time = time_to_jiffies(outarg.attr_valid,
965 outarg.attr_valid_nsec);
967 } else if (err == -EINTR)
968 fuse_invalidate_attr(inode);
973 static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry,
976 struct inode *inode = entry->d_inode;
977 int err = fuse_revalidate(entry);
979 generic_fillattr(inode, stat);
984 static int fuse_setxattr(struct dentry *entry, const char *name,
985 const void *value, size_t size, int flags)
987 struct inode *inode = entry->d_inode;
988 struct fuse_conn *fc = get_fuse_conn(inode);
989 struct fuse_req *req;
990 struct fuse_setxattr_in inarg;
996 req = fuse_get_req(fc);
1000 memset(&inarg, 0, sizeof(inarg));
1002 inarg.flags = flags;
1003 req->in.h.opcode = FUSE_SETXATTR;
1004 req->in.h.nodeid = get_node_id(inode);
1006 req->in.numargs = 3;
1007 req->in.args[0].size = sizeof(inarg);
1008 req->in.args[0].value = &inarg;
1009 req->in.args[1].size = strlen(name) + 1;
1010 req->in.args[1].value = name;
1011 req->in.args[2].size = size;
1012 req->in.args[2].value = value;
1013 request_send(fc, req);
1014 err = req->out.h.error;
1015 fuse_put_request(fc, req);
1016 if (err == -ENOSYS) {
1017 fc->no_setxattr = 1;
1023 static ssize_t fuse_getxattr(struct dentry *entry, const char *name,
1024 void *value, size_t size)
1026 struct inode *inode = entry->d_inode;
1027 struct fuse_conn *fc = get_fuse_conn(inode);
1028 struct fuse_req *req;
1029 struct fuse_getxattr_in inarg;
1030 struct fuse_getxattr_out outarg;
1033 if (fc->no_getxattr)
1036 req = fuse_get_req(fc);
1038 return PTR_ERR(req);
1040 memset(&inarg, 0, sizeof(inarg));
1042 req->in.h.opcode = FUSE_GETXATTR;
1043 req->in.h.nodeid = get_node_id(inode);
1045 req->in.numargs = 2;
1046 req->in.args[0].size = sizeof(inarg);
1047 req->in.args[0].value = &inarg;
1048 req->in.args[1].size = strlen(name) + 1;
1049 req->in.args[1].value = name;
1050 /* This is really two different operations rolled into one */
1051 req->out.numargs = 1;
1053 req->out.argvar = 1;
1054 req->out.args[0].size = size;
1055 req->out.args[0].value = value;
1057 req->out.args[0].size = sizeof(outarg);
1058 req->out.args[0].value = &outarg;
1060 request_send(fc, req);
1061 ret = req->out.h.error;
1063 ret = size ? req->out.args[0].size : outarg.size;
1065 if (ret == -ENOSYS) {
1066 fc->no_getxattr = 1;
1070 fuse_put_request(fc, req);
1074 static ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size)
1076 struct inode *inode = entry->d_inode;
1077 struct fuse_conn *fc = get_fuse_conn(inode);
1078 struct fuse_req *req;
1079 struct fuse_getxattr_in inarg;
1080 struct fuse_getxattr_out outarg;
1083 if (fc->no_listxattr)
1086 req = fuse_get_req(fc);
1088 return PTR_ERR(req);
1090 memset(&inarg, 0, sizeof(inarg));
1092 req->in.h.opcode = FUSE_LISTXATTR;
1093 req->in.h.nodeid = get_node_id(inode);
1095 req->in.numargs = 1;
1096 req->in.args[0].size = sizeof(inarg);
1097 req->in.args[0].value = &inarg;
1098 /* This is really two different operations rolled into one */
1099 req->out.numargs = 1;
1101 req->out.argvar = 1;
1102 req->out.args[0].size = size;
1103 req->out.args[0].value = list;
1105 req->out.args[0].size = sizeof(outarg);
1106 req->out.args[0].value = &outarg;
1108 request_send(fc, req);
1109 ret = req->out.h.error;
1111 ret = size ? req->out.args[0].size : outarg.size;
1113 if (ret == -ENOSYS) {
1114 fc->no_listxattr = 1;
1118 fuse_put_request(fc, req);
1122 static int fuse_removexattr(struct dentry *entry, const char *name)
1124 struct inode *inode = entry->d_inode;
1125 struct fuse_conn *fc = get_fuse_conn(inode);
1126 struct fuse_req *req;
1129 if (fc->no_removexattr)
1132 req = fuse_get_req(fc);
1134 return PTR_ERR(req);
1136 req->in.h.opcode = FUSE_REMOVEXATTR;
1137 req->in.h.nodeid = get_node_id(inode);
1139 req->in.numargs = 1;
1140 req->in.args[0].size = strlen(name) + 1;
1141 req->in.args[0].value = name;
1142 request_send(fc, req);
1143 err = req->out.h.error;
1144 fuse_put_request(fc, req);
1145 if (err == -ENOSYS) {
1146 fc->no_removexattr = 1;
1152 static struct inode_operations fuse_dir_inode_operations = {
1153 .lookup = fuse_lookup,
1154 .mkdir = fuse_mkdir,
1155 .symlink = fuse_symlink,
1156 .unlink = fuse_unlink,
1157 .rmdir = fuse_rmdir,
1158 .rename = fuse_rename,
1160 .setattr = fuse_setattr,
1161 .create = fuse_create,
1162 .mknod = fuse_mknod,
1163 .permission = fuse_permission,
1164 .getattr = fuse_getattr,
1165 .setxattr = fuse_setxattr,
1166 .getxattr = fuse_getxattr,
1167 .listxattr = fuse_listxattr,
1168 .removexattr = fuse_removexattr,
1171 static const struct file_operations fuse_dir_operations = {
1172 .llseek = generic_file_llseek,
1173 .read = generic_read_dir,
1174 .readdir = fuse_readdir,
1175 .open = fuse_dir_open,
1176 .release = fuse_dir_release,
1177 .fsync = fuse_dir_fsync,
1180 static struct inode_operations fuse_common_inode_operations = {
1181 .setattr = fuse_setattr,
1182 .permission = fuse_permission,
1183 .getattr = fuse_getattr,
1184 .setxattr = fuse_setxattr,
1185 .getxattr = fuse_getxattr,
1186 .listxattr = fuse_listxattr,
1187 .removexattr = fuse_removexattr,
1190 static struct inode_operations fuse_symlink_inode_operations = {
1191 .setattr = fuse_setattr,
1192 .follow_link = fuse_follow_link,
1193 .put_link = fuse_put_link,
1194 .readlink = generic_readlink,
1195 .getattr = fuse_getattr,
1196 .setxattr = fuse_setxattr,
1197 .getxattr = fuse_getxattr,
1198 .listxattr = fuse_listxattr,
1199 .removexattr = fuse_removexattr,
1202 void fuse_init_common(struct inode *inode)
1204 inode->i_op = &fuse_common_inode_operations;
1207 void fuse_init_dir(struct inode *inode)
1209 inode->i_op = &fuse_dir_inode_operations;
1210 inode->i_fop = &fuse_dir_operations;
1213 void fuse_init_symlink(struct inode *inode)
1215 inode->i_op = &fuse_symlink_inode_operations;