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 struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry,
170 struct nameidata *nd)
173 struct fuse_entry_out outarg;
174 struct inode *inode = NULL;
175 struct fuse_conn *fc = get_fuse_conn(dir);
176 struct fuse_req *req;
178 if (entry->d_name.len > FUSE_NAME_MAX)
179 return ERR_PTR(-ENAMETOOLONG);
181 req = fuse_get_request(fc);
183 return ERR_PTR(-EINTR);
185 fuse_lookup_init(req, dir, entry, &outarg);
186 request_send(fc, req);
187 err = req->out.h.error;
188 if (!err && outarg.nodeid && invalid_nodeid(outarg.nodeid))
190 if (!err && outarg.nodeid) {
191 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
194 fuse_send_forget(fc, req, outarg.nodeid, 1);
195 return ERR_PTR(-ENOMEM);
198 fuse_put_request(fc, req);
199 if (err && err != -ENOENT)
202 if (inode && dir_alias(inode)) {
204 return ERR_PTR(-EIO);
207 entry->d_op = &fuse_dentry_operations;
209 fuse_change_timeout(entry, &outarg);
211 fuse_invalidate_entry_cache(entry);
216 * Atomic create+open operation
218 * If the filesystem doesn't support this, then fall back to separate
219 * 'mknod' + 'open' requests.
221 static int fuse_create_open(struct inode *dir, struct dentry *entry, int mode,
222 struct nameidata *nd)
226 struct fuse_conn *fc = get_fuse_conn(dir);
227 struct fuse_req *req;
228 struct fuse_open_in inarg;
229 struct fuse_open_out outopen;
230 struct fuse_entry_out outentry;
231 struct fuse_file *ff;
233 int flags = nd->intent.open.flags - 1;
240 req = fuse_get_request(fc);
244 ff = fuse_file_alloc();
246 goto out_put_request;
249 memset(&inarg, 0, sizeof(inarg));
252 req->in.h.opcode = FUSE_CREATE;
253 req->in.h.nodeid = get_node_id(dir);
256 req->in.args[0].size = sizeof(inarg);
257 req->in.args[0].value = &inarg;
258 req->in.args[1].size = entry->d_name.len + 1;
259 req->in.args[1].value = entry->d_name.name;
260 req->out.numargs = 2;
261 req->out.args[0].size = sizeof(outentry);
262 req->out.args[0].value = &outentry;
263 req->out.args[1].size = sizeof(outopen);
264 req->out.args[1].value = &outopen;
265 request_send(fc, req);
266 err = req->out.h.error;
274 if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid))
277 inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation,
281 flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
283 /* Special release, with inode = NULL, this will
284 trigger a 'forget' request when the release is
286 fuse_send_release(fc, ff, outentry.nodeid, NULL, flags, 0);
287 goto out_put_request;
289 fuse_put_request(fc, req);
290 d_instantiate(entry, inode);
291 fuse_change_timeout(entry, &outentry);
292 file = lookup_instantiate_filp(nd, entry, generic_file_open);
295 fuse_send_release(fc, ff, outentry.nodeid, inode, flags, 0);
296 return PTR_ERR(file);
298 fuse_finish_open(inode, file, ff, &outopen);
304 fuse_put_request(fc, req);
310 * Code shared between mknod, mkdir, symlink and link
312 static int create_new_entry(struct fuse_conn *fc, struct fuse_req *req,
313 struct inode *dir, struct dentry *entry,
316 struct fuse_entry_out outarg;
320 req->in.h.nodeid = get_node_id(dir);
322 req->out.numargs = 1;
323 req->out.args[0].size = sizeof(outarg);
324 req->out.args[0].value = &outarg;
325 request_send(fc, req);
326 err = req->out.h.error;
328 fuse_put_request(fc, req);
331 if (invalid_nodeid(outarg.nodeid)) {
332 fuse_put_request(fc, req);
335 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
338 fuse_send_forget(fc, req, outarg.nodeid, 1);
341 fuse_put_request(fc, req);
343 /* Don't allow userspace to do really stupid things... */
344 if (((inode->i_mode ^ mode) & S_IFMT) || dir_alias(inode)) {
349 d_instantiate(entry, inode);
350 fuse_change_timeout(entry, &outarg);
351 fuse_invalidate_attr(dir);
355 static int fuse_mknod(struct inode *dir, struct dentry *entry, int mode,
358 struct fuse_mknod_in inarg;
359 struct fuse_conn *fc = get_fuse_conn(dir);
360 struct fuse_req *req = fuse_get_request(fc);
364 memset(&inarg, 0, sizeof(inarg));
366 inarg.rdev = new_encode_dev(rdev);
367 req->in.h.opcode = FUSE_MKNOD;
369 req->in.args[0].size = sizeof(inarg);
370 req->in.args[0].value = &inarg;
371 req->in.args[1].size = entry->d_name.len + 1;
372 req->in.args[1].value = entry->d_name.name;
373 return create_new_entry(fc, req, dir, entry, mode);
376 static int fuse_create(struct inode *dir, struct dentry *entry, int mode,
377 struct nameidata *nd)
379 if (nd && (nd->flags & LOOKUP_CREATE)) {
380 int err = fuse_create_open(dir, entry, mode, nd);
383 /* Fall back on mknod */
385 return fuse_mknod(dir, entry, mode, 0);
388 static int fuse_mkdir(struct inode *dir, struct dentry *entry, int mode)
390 struct fuse_mkdir_in inarg;
391 struct fuse_conn *fc = get_fuse_conn(dir);
392 struct fuse_req *req = fuse_get_request(fc);
396 memset(&inarg, 0, sizeof(inarg));
398 req->in.h.opcode = FUSE_MKDIR;
400 req->in.args[0].size = sizeof(inarg);
401 req->in.args[0].value = &inarg;
402 req->in.args[1].size = entry->d_name.len + 1;
403 req->in.args[1].value = entry->d_name.name;
404 return create_new_entry(fc, req, dir, entry, S_IFDIR);
407 static int fuse_symlink(struct inode *dir, struct dentry *entry,
410 struct fuse_conn *fc = get_fuse_conn(dir);
411 unsigned len = strlen(link) + 1;
412 struct fuse_req *req = fuse_get_request(fc);
416 req->in.h.opcode = FUSE_SYMLINK;
418 req->in.args[0].size = entry->d_name.len + 1;
419 req->in.args[0].value = entry->d_name.name;
420 req->in.args[1].size = len;
421 req->in.args[1].value = link;
422 return create_new_entry(fc, req, dir, entry, S_IFLNK);
425 static int fuse_unlink(struct inode *dir, struct dentry *entry)
428 struct fuse_conn *fc = get_fuse_conn(dir);
429 struct fuse_req *req = fuse_get_request(fc);
433 req->in.h.opcode = FUSE_UNLINK;
434 req->in.h.nodeid = get_node_id(dir);
437 req->in.args[0].size = entry->d_name.len + 1;
438 req->in.args[0].value = entry->d_name.name;
439 request_send(fc, req);
440 err = req->out.h.error;
441 fuse_put_request(fc, req);
443 struct inode *inode = entry->d_inode;
445 /* Set nlink to zero so the inode can be cleared, if
446 the inode does have more links this will be
447 discovered at the next lookup/getattr */
449 fuse_invalidate_attr(inode);
450 fuse_invalidate_attr(dir);
451 fuse_invalidate_entry_cache(entry);
452 } else if (err == -EINTR)
453 fuse_invalidate_entry(entry);
457 static int fuse_rmdir(struct inode *dir, struct dentry *entry)
460 struct fuse_conn *fc = get_fuse_conn(dir);
461 struct fuse_req *req = fuse_get_request(fc);
465 req->in.h.opcode = FUSE_RMDIR;
466 req->in.h.nodeid = get_node_id(dir);
469 req->in.args[0].size = entry->d_name.len + 1;
470 req->in.args[0].value = entry->d_name.name;
471 request_send(fc, req);
472 err = req->out.h.error;
473 fuse_put_request(fc, req);
475 entry->d_inode->i_nlink = 0;
476 fuse_invalidate_attr(dir);
477 fuse_invalidate_entry_cache(entry);
478 } else if (err == -EINTR)
479 fuse_invalidate_entry(entry);
483 static int fuse_rename(struct inode *olddir, struct dentry *oldent,
484 struct inode *newdir, struct dentry *newent)
487 struct fuse_rename_in inarg;
488 struct fuse_conn *fc = get_fuse_conn(olddir);
489 struct fuse_req *req = fuse_get_request(fc);
493 memset(&inarg, 0, sizeof(inarg));
494 inarg.newdir = get_node_id(newdir);
495 req->in.h.opcode = FUSE_RENAME;
496 req->in.h.nodeid = get_node_id(olddir);
498 req->inode2 = newdir;
500 req->in.args[0].size = sizeof(inarg);
501 req->in.args[0].value = &inarg;
502 req->in.args[1].size = oldent->d_name.len + 1;
503 req->in.args[1].value = oldent->d_name.name;
504 req->in.args[2].size = newent->d_name.len + 1;
505 req->in.args[2].value = newent->d_name.name;
506 request_send(fc, req);
507 err = req->out.h.error;
508 fuse_put_request(fc, req);
510 fuse_invalidate_attr(olddir);
511 if (olddir != newdir)
512 fuse_invalidate_attr(newdir);
514 /* newent will end up negative */
516 fuse_invalidate_entry_cache(newent);
517 } else if (err == -EINTR) {
518 /* If request was interrupted, DEITY only knows if the
519 rename actually took place. If the invalidation
520 fails (e.g. some process has CWD under the renamed
521 directory), then there can be inconsistency between
522 the dcache and the real filesystem. Tough luck. */
523 fuse_invalidate_entry(oldent);
525 fuse_invalidate_entry(newent);
531 static int fuse_link(struct dentry *entry, struct inode *newdir,
532 struct dentry *newent)
535 struct fuse_link_in inarg;
536 struct inode *inode = entry->d_inode;
537 struct fuse_conn *fc = get_fuse_conn(inode);
538 struct fuse_req *req = fuse_get_request(fc);
542 memset(&inarg, 0, sizeof(inarg));
543 inarg.oldnodeid = get_node_id(inode);
544 req->in.h.opcode = FUSE_LINK;
547 req->in.args[0].size = sizeof(inarg);
548 req->in.args[0].value = &inarg;
549 req->in.args[1].size = newent->d_name.len + 1;
550 req->in.args[1].value = newent->d_name.name;
551 err = create_new_entry(fc, req, newdir, newent, inode->i_mode);
552 /* Contrary to "normal" filesystems it can happen that link
553 makes two "logical" inodes point to the same "physical"
554 inode. We invalidate the attributes of the old one, so it
555 will reflect changes in the backing inode (link count,
558 if (!err || err == -EINTR)
559 fuse_invalidate_attr(inode);
563 int fuse_do_getattr(struct inode *inode)
566 struct fuse_attr_out arg;
567 struct fuse_conn *fc = get_fuse_conn(inode);
568 struct fuse_req *req = fuse_get_request(fc);
572 req->in.h.opcode = FUSE_GETATTR;
573 req->in.h.nodeid = get_node_id(inode);
575 req->out.numargs = 1;
576 req->out.args[0].size = sizeof(arg);
577 req->out.args[0].value = &arg;
578 request_send(fc, req);
579 err = req->out.h.error;
580 fuse_put_request(fc, req);
582 if ((inode->i_mode ^ arg.attr.mode) & S_IFMT) {
583 make_bad_inode(inode);
586 struct fuse_inode *fi = get_fuse_inode(inode);
587 fuse_change_attributes(inode, &arg.attr);
588 fi->i_time = time_to_jiffies(arg.attr_valid,
589 arg.attr_valid_nsec);
596 * Calling into a user-controlled filesystem gives the filesystem
597 * daemon ptrace-like capabilities over the requester process. This
598 * means, that the filesystem daemon is able to record the exact
599 * filesystem operations performed, and can also control the behavior
600 * of the requester process in otherwise impossible ways. For example
601 * it can delay the operation for arbitrary length of time allowing
602 * DoS against the requester.
604 * For this reason only those processes can call into the filesystem,
605 * for which the owner of the mount has ptrace privilege. This
606 * excludes processes started by other users, suid or sgid processes.
608 static int fuse_allow_task(struct fuse_conn *fc, struct task_struct *task)
610 if (fc->flags & FUSE_ALLOW_OTHER)
613 if (task->euid == fc->user_id &&
614 task->suid == fc->user_id &&
615 task->uid == fc->user_id &&
616 task->egid == fc->group_id &&
617 task->sgid == fc->group_id &&
618 task->gid == fc->group_id)
625 * Check whether the inode attributes are still valid
627 * If the attribute validity timeout has expired, then fetch the fresh
628 * attributes with a 'getattr' request
630 * I'm not sure why cached attributes are never returned for the root
631 * inode, this is probably being too cautious.
633 static int fuse_revalidate(struct dentry *entry)
635 struct inode *inode = entry->d_inode;
636 struct fuse_inode *fi = get_fuse_inode(inode);
637 struct fuse_conn *fc = get_fuse_conn(inode);
639 if (!fuse_allow_task(fc, current))
641 if (get_node_id(inode) != FUSE_ROOT_ID &&
642 time_before_eq(jiffies, fi->i_time))
645 return fuse_do_getattr(inode);
648 static int fuse_access(struct inode *inode, int mask)
650 struct fuse_conn *fc = get_fuse_conn(inode);
651 struct fuse_req *req;
652 struct fuse_access_in inarg;
658 req = fuse_get_request(fc);
662 memset(&inarg, 0, sizeof(inarg));
664 req->in.h.opcode = FUSE_ACCESS;
665 req->in.h.nodeid = get_node_id(inode);
668 req->in.args[0].size = sizeof(inarg);
669 req->in.args[0].value = &inarg;
670 request_send(fc, req);
671 err = req->out.h.error;
672 fuse_put_request(fc, req);
673 if (err == -ENOSYS) {
681 * Check permission. The two basic access models of FUSE are:
683 * 1) Local access checking ('default_permissions' mount option) based
684 * on file mode. This is the plain old disk filesystem permission
687 * 2) "Remote" access checking, where server is responsible for
688 * checking permission in each inode operation. An exception to this
689 * is if ->permission() was invoked from sys_access() in which case an
690 * access request is sent. Execute permission is still checked
691 * locally based on file mode.
693 static int fuse_permission(struct inode *inode, int mask, struct nameidata *nd)
695 struct fuse_conn *fc = get_fuse_conn(inode);
697 if (!fuse_allow_task(fc, current))
699 else if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
700 int err = generic_permission(inode, mask, NULL);
702 /* If permission is denied, try to refresh file
703 attributes. This is also needed, because the root
704 node will at first have no permissions */
705 if (err == -EACCES) {
706 err = fuse_do_getattr(inode);
708 err = generic_permission(inode, mask, NULL);
711 /* Note: the opposite of the above test does not
712 exist. So if permissions are revoked this won't be
713 noticed immediately, only after the attribute
714 timeout has expired */
718 int mode = inode->i_mode;
719 if ((mask & MAY_EXEC) && !S_ISDIR(mode) && !(mode & S_IXUGO))
722 if (nd && (nd->flags & LOOKUP_ACCESS))
723 return fuse_access(inode, mask);
728 static int parse_dirfile(char *buf, size_t nbytes, struct file *file,
729 void *dstbuf, filldir_t filldir)
731 while (nbytes >= FUSE_NAME_OFFSET) {
732 struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
733 size_t reclen = FUSE_DIRENT_SIZE(dirent);
735 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
740 over = filldir(dstbuf, dirent->name, dirent->namelen,
741 file->f_pos, dirent->ino, dirent->type);
747 file->f_pos = dirent->off;
753 static inline size_t fuse_send_readdir(struct fuse_req *req, struct file *file,
754 struct inode *inode, loff_t pos,
757 return fuse_send_read_common(req, file, inode, pos, count, 1);
760 static int fuse_readdir(struct file *file, void *dstbuf, filldir_t filldir)
765 struct inode *inode = file->f_dentry->d_inode;
766 struct fuse_conn *fc = get_fuse_conn(inode);
767 struct fuse_req *req;
769 if (is_bad_inode(inode))
772 req = fuse_get_request(fc);
776 page = alloc_page(GFP_KERNEL);
778 fuse_put_request(fc, req);
782 req->pages[0] = page;
783 nbytes = fuse_send_readdir(req, file, inode, file->f_pos, PAGE_SIZE);
784 err = req->out.h.error;
785 fuse_put_request(fc, req);
787 err = parse_dirfile(page_address(page), nbytes, file, dstbuf,
791 fuse_invalidate_attr(inode); /* atime changed */
795 static char *read_link(struct dentry *dentry)
797 struct inode *inode = dentry->d_inode;
798 struct fuse_conn *fc = get_fuse_conn(inode);
799 struct fuse_req *req = fuse_get_request(fc);
803 return ERR_PTR(-EINTR);
805 link = (char *) __get_free_page(GFP_KERNEL);
807 link = ERR_PTR(-ENOMEM);
810 req->in.h.opcode = FUSE_READLINK;
811 req->in.h.nodeid = get_node_id(inode);
814 req->out.numargs = 1;
815 req->out.args[0].size = PAGE_SIZE - 1;
816 req->out.args[0].value = link;
817 request_send(fc, req);
818 if (req->out.h.error) {
819 free_page((unsigned long) link);
820 link = ERR_PTR(req->out.h.error);
822 link[req->out.args[0].size] = '\0';
824 fuse_put_request(fc, req);
825 fuse_invalidate_attr(inode); /* atime changed */
829 static void free_link(char *link)
832 free_page((unsigned long) link);
835 static void *fuse_follow_link(struct dentry *dentry, struct nameidata *nd)
837 nd_set_link(nd, read_link(dentry));
841 static void fuse_put_link(struct dentry *dentry, struct nameidata *nd, void *c)
843 free_link(nd_get_link(nd));
846 static int fuse_dir_open(struct inode *inode, struct file *file)
848 return fuse_open_common(inode, file, 1);
851 static int fuse_dir_release(struct inode *inode, struct file *file)
853 return fuse_release_common(inode, file, 1);
856 static int fuse_dir_fsync(struct file *file, struct dentry *de, int datasync)
858 /* nfsd can call this with no file */
859 return file ? fuse_fsync_common(file, de, datasync, 1) : 0;
862 static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg)
864 unsigned ivalid = iattr->ia_valid;
866 if (ivalid & ATTR_MODE)
867 arg->valid |= FATTR_MODE, arg->mode = iattr->ia_mode;
868 if (ivalid & ATTR_UID)
869 arg->valid |= FATTR_UID, arg->uid = iattr->ia_uid;
870 if (ivalid & ATTR_GID)
871 arg->valid |= FATTR_GID, arg->gid = iattr->ia_gid;
872 if (ivalid & ATTR_SIZE)
873 arg->valid |= FATTR_SIZE, arg->size = iattr->ia_size;
874 /* You can only _set_ these together (they may change by themselves) */
875 if ((ivalid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME)) {
876 arg->valid |= FATTR_ATIME | FATTR_MTIME;
877 arg->atime = iattr->ia_atime.tv_sec;
878 arg->mtime = iattr->ia_mtime.tv_sec;
880 if (ivalid & ATTR_FILE) {
881 struct fuse_file *ff = iattr->ia_file->private_data;
882 arg->valid |= FATTR_FH;
888 * Set attributes, and at the same time refresh them.
890 * Truncation is slightly complicated, because the 'truncate' request
891 * may fail, in which case we don't want to touch the mapping.
892 * vmtruncate() doesn't allow for this case. So do the rlimit
893 * checking by hand and call vmtruncate() only after the file has
894 * actually been truncated.
896 static int fuse_setattr(struct dentry *entry, struct iattr *attr)
898 struct inode *inode = entry->d_inode;
899 struct fuse_conn *fc = get_fuse_conn(inode);
900 struct fuse_inode *fi = get_fuse_inode(inode);
901 struct fuse_req *req;
902 struct fuse_setattr_in inarg;
903 struct fuse_attr_out outarg;
907 if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
908 err = inode_change_ok(inode, attr);
913 if (attr->ia_valid & ATTR_SIZE) {
916 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
917 if (limit != RLIM_INFINITY && attr->ia_size > (loff_t) limit) {
918 send_sig(SIGXFSZ, current, 0);
923 req = fuse_get_request(fc);
927 memset(&inarg, 0, sizeof(inarg));
928 iattr_to_fattr(attr, &inarg);
929 req->in.h.opcode = FUSE_SETATTR;
930 req->in.h.nodeid = get_node_id(inode);
933 req->in.args[0].size = sizeof(inarg);
934 req->in.args[0].value = &inarg;
935 req->out.numargs = 1;
936 req->out.args[0].size = sizeof(outarg);
937 req->out.args[0].value = &outarg;
938 request_send(fc, req);
939 err = req->out.h.error;
940 fuse_put_request(fc, req);
942 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
943 make_bad_inode(inode);
947 loff_t origsize = i_size_read(inode);
948 i_size_write(inode, outarg.attr.size);
949 if (origsize > outarg.attr.size)
950 vmtruncate(inode, outarg.attr.size);
952 fuse_change_attributes(inode, &outarg.attr);
953 fi->i_time = time_to_jiffies(outarg.attr_valid,
954 outarg.attr_valid_nsec);
956 } else if (err == -EINTR)
957 fuse_invalidate_attr(inode);
962 static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry,
965 struct inode *inode = entry->d_inode;
966 int err = fuse_revalidate(entry);
968 generic_fillattr(inode, stat);
973 static int fuse_setxattr(struct dentry *entry, const char *name,
974 const void *value, size_t size, int flags)
976 struct inode *inode = entry->d_inode;
977 struct fuse_conn *fc = get_fuse_conn(inode);
978 struct fuse_req *req;
979 struct fuse_setxattr_in inarg;
985 req = fuse_get_request(fc);
989 memset(&inarg, 0, sizeof(inarg));
992 req->in.h.opcode = FUSE_SETXATTR;
993 req->in.h.nodeid = get_node_id(inode);
996 req->in.args[0].size = sizeof(inarg);
997 req->in.args[0].value = &inarg;
998 req->in.args[1].size = strlen(name) + 1;
999 req->in.args[1].value = name;
1000 req->in.args[2].size = size;
1001 req->in.args[2].value = value;
1002 request_send(fc, req);
1003 err = req->out.h.error;
1004 fuse_put_request(fc, req);
1005 if (err == -ENOSYS) {
1006 fc->no_setxattr = 1;
1012 static ssize_t fuse_getxattr(struct dentry *entry, const char *name,
1013 void *value, size_t size)
1015 struct inode *inode = entry->d_inode;
1016 struct fuse_conn *fc = get_fuse_conn(inode);
1017 struct fuse_req *req;
1018 struct fuse_getxattr_in inarg;
1019 struct fuse_getxattr_out outarg;
1022 if (fc->no_getxattr)
1025 req = fuse_get_request(fc);
1029 memset(&inarg, 0, sizeof(inarg));
1031 req->in.h.opcode = FUSE_GETXATTR;
1032 req->in.h.nodeid = get_node_id(inode);
1034 req->in.numargs = 2;
1035 req->in.args[0].size = sizeof(inarg);
1036 req->in.args[0].value = &inarg;
1037 req->in.args[1].size = strlen(name) + 1;
1038 req->in.args[1].value = name;
1039 /* This is really two different operations rolled into one */
1040 req->out.numargs = 1;
1042 req->out.argvar = 1;
1043 req->out.args[0].size = size;
1044 req->out.args[0].value = value;
1046 req->out.args[0].size = sizeof(outarg);
1047 req->out.args[0].value = &outarg;
1049 request_send(fc, req);
1050 ret = req->out.h.error;
1052 ret = size ? req->out.args[0].size : outarg.size;
1054 if (ret == -ENOSYS) {
1055 fc->no_getxattr = 1;
1059 fuse_put_request(fc, req);
1063 static ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size)
1065 struct inode *inode = entry->d_inode;
1066 struct fuse_conn *fc = get_fuse_conn(inode);
1067 struct fuse_req *req;
1068 struct fuse_getxattr_in inarg;
1069 struct fuse_getxattr_out outarg;
1072 if (fc->no_listxattr)
1075 req = fuse_get_request(fc);
1079 memset(&inarg, 0, sizeof(inarg));
1081 req->in.h.opcode = FUSE_LISTXATTR;
1082 req->in.h.nodeid = get_node_id(inode);
1084 req->in.numargs = 1;
1085 req->in.args[0].size = sizeof(inarg);
1086 req->in.args[0].value = &inarg;
1087 /* This is really two different operations rolled into one */
1088 req->out.numargs = 1;
1090 req->out.argvar = 1;
1091 req->out.args[0].size = size;
1092 req->out.args[0].value = list;
1094 req->out.args[0].size = sizeof(outarg);
1095 req->out.args[0].value = &outarg;
1097 request_send(fc, req);
1098 ret = req->out.h.error;
1100 ret = size ? req->out.args[0].size : outarg.size;
1102 if (ret == -ENOSYS) {
1103 fc->no_listxattr = 1;
1107 fuse_put_request(fc, req);
1111 static int fuse_removexattr(struct dentry *entry, const char *name)
1113 struct inode *inode = entry->d_inode;
1114 struct fuse_conn *fc = get_fuse_conn(inode);
1115 struct fuse_req *req;
1118 if (fc->no_removexattr)
1121 req = fuse_get_request(fc);
1125 req->in.h.opcode = FUSE_REMOVEXATTR;
1126 req->in.h.nodeid = get_node_id(inode);
1128 req->in.numargs = 1;
1129 req->in.args[0].size = strlen(name) + 1;
1130 req->in.args[0].value = name;
1131 request_send(fc, req);
1132 err = req->out.h.error;
1133 fuse_put_request(fc, req);
1134 if (err == -ENOSYS) {
1135 fc->no_removexattr = 1;
1141 static struct inode_operations fuse_dir_inode_operations = {
1142 .lookup = fuse_lookup,
1143 .mkdir = fuse_mkdir,
1144 .symlink = fuse_symlink,
1145 .unlink = fuse_unlink,
1146 .rmdir = fuse_rmdir,
1147 .rename = fuse_rename,
1149 .setattr = fuse_setattr,
1150 .create = fuse_create,
1151 .mknod = fuse_mknod,
1152 .permission = fuse_permission,
1153 .getattr = fuse_getattr,
1154 .setxattr = fuse_setxattr,
1155 .getxattr = fuse_getxattr,
1156 .listxattr = fuse_listxattr,
1157 .removexattr = fuse_removexattr,
1160 static struct file_operations fuse_dir_operations = {
1161 .llseek = generic_file_llseek,
1162 .read = generic_read_dir,
1163 .readdir = fuse_readdir,
1164 .open = fuse_dir_open,
1165 .release = fuse_dir_release,
1166 .fsync = fuse_dir_fsync,
1169 static struct inode_operations fuse_common_inode_operations = {
1170 .setattr = fuse_setattr,
1171 .permission = fuse_permission,
1172 .getattr = fuse_getattr,
1173 .setxattr = fuse_setxattr,
1174 .getxattr = fuse_getxattr,
1175 .listxattr = fuse_listxattr,
1176 .removexattr = fuse_removexattr,
1179 static struct inode_operations fuse_symlink_inode_operations = {
1180 .setattr = fuse_setattr,
1181 .follow_link = fuse_follow_link,
1182 .put_link = fuse_put_link,
1183 .readlink = generic_readlink,
1184 .getattr = fuse_getattr,
1185 .setxattr = fuse_setxattr,
1186 .getxattr = fuse_getxattr,
1187 .listxattr = fuse_listxattr,
1188 .removexattr = fuse_removexattr,
1191 void fuse_init_common(struct inode *inode)
1193 inode->i_op = &fuse_common_inode_operations;
1196 void fuse_init_dir(struct inode *inode)
1198 inode->i_op = &fuse_dir_inode_operations;
1199 inode->i_fop = &fuse_dir_operations;
1202 void fuse_init_symlink(struct inode *inode)
1204 inode->i_op = &fuse_symlink_inode_operations;