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.
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>
17 #if BITS_PER_LONG >= 64
18 static inline void fuse_dentry_settime(struct dentry *entry, u64 time)
23 static inline u64 fuse_dentry_time(struct dentry *entry)
29 * On 32 bit archs store the high 32 bits of time in d_fsdata
31 static void fuse_dentry_settime(struct dentry *entry, u64 time)
34 entry->d_fsdata = (void *) (unsigned long) (time >> 32);
37 static u64 fuse_dentry_time(struct dentry *entry)
39 return (u64) entry->d_time +
40 ((u64) (unsigned long) entry->d_fsdata << 32);
45 * FUSE caches dentries and attributes with separate timeout. The
46 * time in jiffies until the dentry/attributes are valid is stored in
47 * dentry->d_time and fuse_inode->i_time respectively.
51 * Calculate the time in jiffies until a dentry/attributes are valid
53 static u64 time_to_jiffies(unsigned long sec, unsigned long nsec)
56 struct timespec ts = {sec, nsec};
57 return get_jiffies_64() + timespec_to_jiffies(&ts);
63 * Set dentry and possibly attribute timeouts from the lookup/mk*
66 static void fuse_change_entry_timeout(struct dentry *entry,
67 struct fuse_entry_out *o)
69 fuse_dentry_settime(entry,
70 time_to_jiffies(o->entry_valid, o->entry_valid_nsec));
73 static u64 attr_timeout(struct fuse_attr_out *o)
75 return time_to_jiffies(o->attr_valid, o->attr_valid_nsec);
78 static u64 entry_attr_timeout(struct fuse_entry_out *o)
80 return time_to_jiffies(o->attr_valid, o->attr_valid_nsec);
84 * Mark the attributes as stale, so that at the next call to
85 * ->getattr() they will be fetched from userspace
87 void fuse_invalidate_attr(struct inode *inode)
89 get_fuse_inode(inode)->i_time = 0;
93 * Just mark the entry as stale, so that a next attempt to look it up
94 * will result in a new lookup call to userspace
96 * This is called when a dentry is about to become negative and the
97 * timeout is unknown (unlink, rmdir, rename and in some cases
100 static void fuse_invalidate_entry_cache(struct dentry *entry)
102 fuse_dentry_settime(entry, 0);
106 * Same as fuse_invalidate_entry_cache(), but also try to remove the
107 * dentry from the hash
109 static void fuse_invalidate_entry(struct dentry *entry)
112 fuse_invalidate_entry_cache(entry);
115 static void fuse_lookup_init(struct fuse_req *req, struct inode *dir,
116 struct dentry *entry,
117 struct fuse_entry_out *outarg)
119 struct fuse_conn *fc = get_fuse_conn(dir);
121 memset(outarg, 0, sizeof(struct fuse_entry_out));
122 req->in.h.opcode = FUSE_LOOKUP;
123 req->in.h.nodeid = get_node_id(dir);
125 req->in.args[0].size = entry->d_name.len + 1;
126 req->in.args[0].value = entry->d_name.name;
127 req->out.numargs = 1;
129 req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
131 req->out.args[0].size = sizeof(struct fuse_entry_out);
132 req->out.args[0].value = outarg;
136 * Check whether the dentry is still valid
138 * If the entry validity timeout has expired and the dentry is
139 * positive, try to redo the lookup. If the lookup results in a
140 * different inode, then let the VFS invalidate the dentry and redo
141 * the lookup once more. If the lookup results in the same inode,
142 * then refresh the attributes, timeouts and mark the dentry valid.
144 static int fuse_dentry_revalidate(struct dentry *entry, struct nameidata *nd)
146 struct inode *inode = entry->d_inode;
148 if (inode && is_bad_inode(inode))
150 else if (fuse_dentry_time(entry) < get_jiffies_64()) {
152 struct fuse_entry_out outarg;
153 struct fuse_conn *fc;
154 struct fuse_req *req;
155 struct fuse_req *forget_req;
156 struct dentry *parent;
159 /* For negative dentries, always do a fresh lookup */
163 fc = get_fuse_conn(inode);
164 req = fuse_get_req(fc);
168 forget_req = fuse_get_req(fc);
169 if (IS_ERR(forget_req)) {
170 fuse_put_request(fc, req);
174 spin_lock(&fc->lock);
175 attr_version = fc->attr_version;
176 spin_unlock(&fc->lock);
178 parent = dget_parent(entry);
179 fuse_lookup_init(req, parent->d_inode, entry, &outarg);
180 request_send(fc, req);
182 err = req->out.h.error;
183 fuse_put_request(fc, req);
184 /* Zero nodeid is same as -ENOENT */
185 if (!err && !outarg.nodeid)
188 struct fuse_inode *fi = get_fuse_inode(inode);
189 if (outarg.nodeid != get_node_id(inode)) {
190 fuse_send_forget(fc, forget_req,
194 spin_lock(&fc->lock);
196 spin_unlock(&fc->lock);
198 fuse_put_request(fc, forget_req);
199 if (err || (outarg.attr.mode ^ inode->i_mode) & S_IFMT)
202 fuse_change_attributes(inode, &outarg.attr,
203 entry_attr_timeout(&outarg),
205 fuse_change_entry_timeout(entry, &outarg);
210 static int invalid_nodeid(u64 nodeid)
212 return !nodeid || nodeid == FUSE_ROOT_ID;
215 static struct dentry_operations fuse_dentry_operations = {
216 .d_revalidate = fuse_dentry_revalidate,
219 int fuse_valid_type(int m)
221 return S_ISREG(m) || S_ISDIR(m) || S_ISLNK(m) || S_ISCHR(m) ||
222 S_ISBLK(m) || S_ISFIFO(m) || S_ISSOCK(m);
226 * Add a directory inode to a dentry, ensuring that no other dentry
227 * refers to this inode. Called with fc->inst_mutex.
229 static int fuse_d_add_directory(struct dentry *entry, struct inode *inode)
231 struct dentry *alias = d_find_alias(inode);
233 /* This tries to shrink the subtree below alias */
234 fuse_invalidate_entry(alias);
236 if (!list_empty(&inode->i_dentry))
243 static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry,
244 struct nameidata *nd)
247 struct fuse_entry_out outarg;
248 struct inode *inode = NULL;
249 struct fuse_conn *fc = get_fuse_conn(dir);
250 struct fuse_req *req;
251 struct fuse_req *forget_req;
254 if (entry->d_name.len > FUSE_NAME_MAX)
255 return ERR_PTR(-ENAMETOOLONG);
257 req = fuse_get_req(fc);
259 return ERR_PTR(PTR_ERR(req));
261 forget_req = fuse_get_req(fc);
262 if (IS_ERR(forget_req)) {
263 fuse_put_request(fc, req);
264 return ERR_PTR(PTR_ERR(forget_req));
267 spin_lock(&fc->lock);
268 attr_version = fc->attr_version;
269 spin_unlock(&fc->lock);
271 fuse_lookup_init(req, dir, entry, &outarg);
272 request_send(fc, req);
273 err = req->out.h.error;
274 fuse_put_request(fc, req);
275 /* Zero nodeid is same as -ENOENT, but with valid timeout */
276 if (!err && outarg.nodeid &&
277 (invalid_nodeid(outarg.nodeid) ||
278 !fuse_valid_type(outarg.attr.mode)))
280 if (!err && outarg.nodeid) {
281 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
282 &outarg.attr, entry_attr_timeout(&outarg),
285 fuse_send_forget(fc, forget_req, outarg.nodeid, 1);
286 return ERR_PTR(-ENOMEM);
289 fuse_put_request(fc, forget_req);
290 if (err && err != -ENOENT)
293 if (inode && S_ISDIR(inode->i_mode)) {
294 mutex_lock(&fc->inst_mutex);
295 err = fuse_d_add_directory(entry, inode);
296 mutex_unlock(&fc->inst_mutex);
304 entry->d_op = &fuse_dentry_operations;
306 fuse_change_entry_timeout(entry, &outarg);
308 fuse_invalidate_entry_cache(entry);
313 * Synchronous release for the case when something goes wrong in CREATE_OPEN
315 static void fuse_sync_release(struct fuse_conn *fc, struct fuse_file *ff,
316 u64 nodeid, int flags)
318 fuse_release_fill(ff, nodeid, flags, FUSE_RELEASE);
319 ff->reserved_req->force = 1;
320 request_send(fc, ff->reserved_req);
321 fuse_put_request(fc, ff->reserved_req);
326 * Atomic create+open operation
328 * If the filesystem doesn't support this, then fall back to separate
329 * 'mknod' + 'open' requests.
331 static int fuse_create_open(struct inode *dir, struct dentry *entry, int mode,
332 struct nameidata *nd)
336 struct fuse_conn *fc = get_fuse_conn(dir);
337 struct fuse_req *req;
338 struct fuse_req *forget_req;
339 struct fuse_open_in inarg;
340 struct fuse_open_out outopen;
341 struct fuse_entry_out outentry;
342 struct fuse_file *ff;
344 int flags = nd->intent.open.flags - 1;
349 forget_req = fuse_get_req(fc);
350 if (IS_ERR(forget_req))
351 return PTR_ERR(forget_req);
353 req = fuse_get_req(fc);
356 goto out_put_forget_req;
359 ff = fuse_file_alloc();
361 goto out_put_request;
364 memset(&inarg, 0, sizeof(inarg));
365 memset(&outentry, 0, sizeof(outentry));
368 req->in.h.opcode = FUSE_CREATE;
369 req->in.h.nodeid = get_node_id(dir);
371 req->in.args[0].size = sizeof(inarg);
372 req->in.args[0].value = &inarg;
373 req->in.args[1].size = entry->d_name.len + 1;
374 req->in.args[1].value = entry->d_name.name;
375 req->out.numargs = 2;
377 req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
379 req->out.args[0].size = sizeof(outentry);
380 req->out.args[0].value = &outentry;
381 req->out.args[1].size = sizeof(outopen);
382 req->out.args[1].value = &outopen;
383 request_send(fc, req);
384 err = req->out.h.error;
392 if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid))
395 fuse_put_request(fc, req);
396 inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation,
397 &outentry.attr, entry_attr_timeout(&outentry), 0);
399 flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
401 fuse_sync_release(fc, ff, outentry.nodeid, flags);
402 fuse_send_forget(fc, forget_req, outentry.nodeid, 1);
405 fuse_put_request(fc, forget_req);
406 d_instantiate(entry, inode);
407 fuse_change_entry_timeout(entry, &outentry);
408 file = lookup_instantiate_filp(nd, entry, generic_file_open);
411 fuse_sync_release(fc, ff, outentry.nodeid, flags);
412 return PTR_ERR(file);
414 fuse_finish_open(inode, file, ff, &outopen);
420 fuse_put_request(fc, req);
422 fuse_put_request(fc, forget_req);
427 * Code shared between mknod, mkdir, symlink and link
429 static int create_new_entry(struct fuse_conn *fc, struct fuse_req *req,
430 struct inode *dir, struct dentry *entry,
433 struct fuse_entry_out outarg;
436 struct fuse_req *forget_req;
438 forget_req = fuse_get_req(fc);
439 if (IS_ERR(forget_req)) {
440 fuse_put_request(fc, req);
441 return PTR_ERR(forget_req);
444 memset(&outarg, 0, sizeof(outarg));
445 req->in.h.nodeid = get_node_id(dir);
446 req->out.numargs = 1;
448 req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
450 req->out.args[0].size = sizeof(outarg);
451 req->out.args[0].value = &outarg;
452 request_send(fc, req);
453 err = req->out.h.error;
454 fuse_put_request(fc, req);
456 goto out_put_forget_req;
459 if (invalid_nodeid(outarg.nodeid))
460 goto out_put_forget_req;
462 if ((outarg.attr.mode ^ mode) & S_IFMT)
463 goto out_put_forget_req;
465 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
466 &outarg.attr, entry_attr_timeout(&outarg), 0);
468 fuse_send_forget(fc, forget_req, outarg.nodeid, 1);
471 fuse_put_request(fc, forget_req);
473 if (S_ISDIR(inode->i_mode)) {
474 struct dentry *alias;
475 mutex_lock(&fc->inst_mutex);
476 alias = d_find_alias(inode);
478 /* New directory must have moved since mkdir */
479 mutex_unlock(&fc->inst_mutex);
484 d_instantiate(entry, inode);
485 mutex_unlock(&fc->inst_mutex);
487 d_instantiate(entry, inode);
489 fuse_change_entry_timeout(entry, &outarg);
490 fuse_invalidate_attr(dir);
494 fuse_put_request(fc, forget_req);
498 static int fuse_mknod(struct inode *dir, struct dentry *entry, int mode,
501 struct fuse_mknod_in inarg;
502 struct fuse_conn *fc = get_fuse_conn(dir);
503 struct fuse_req *req = fuse_get_req(fc);
507 memset(&inarg, 0, sizeof(inarg));
509 inarg.rdev = new_encode_dev(rdev);
510 req->in.h.opcode = FUSE_MKNOD;
512 req->in.args[0].size = sizeof(inarg);
513 req->in.args[0].value = &inarg;
514 req->in.args[1].size = entry->d_name.len + 1;
515 req->in.args[1].value = entry->d_name.name;
516 return create_new_entry(fc, req, dir, entry, mode);
519 static int fuse_create(struct inode *dir, struct dentry *entry, int mode,
520 struct nameidata *nd)
522 if (nd && (nd->flags & LOOKUP_OPEN)) {
523 int err = fuse_create_open(dir, entry, mode, nd);
526 /* Fall back on mknod */
528 return fuse_mknod(dir, entry, mode, 0);
531 static int fuse_mkdir(struct inode *dir, struct dentry *entry, int mode)
533 struct fuse_mkdir_in inarg;
534 struct fuse_conn *fc = get_fuse_conn(dir);
535 struct fuse_req *req = fuse_get_req(fc);
539 memset(&inarg, 0, sizeof(inarg));
541 req->in.h.opcode = FUSE_MKDIR;
543 req->in.args[0].size = sizeof(inarg);
544 req->in.args[0].value = &inarg;
545 req->in.args[1].size = entry->d_name.len + 1;
546 req->in.args[1].value = entry->d_name.name;
547 return create_new_entry(fc, req, dir, entry, S_IFDIR);
550 static int fuse_symlink(struct inode *dir, struct dentry *entry,
553 struct fuse_conn *fc = get_fuse_conn(dir);
554 unsigned len = strlen(link) + 1;
555 struct fuse_req *req = fuse_get_req(fc);
559 req->in.h.opcode = FUSE_SYMLINK;
561 req->in.args[0].size = entry->d_name.len + 1;
562 req->in.args[0].value = entry->d_name.name;
563 req->in.args[1].size = len;
564 req->in.args[1].value = link;
565 return create_new_entry(fc, req, dir, entry, S_IFLNK);
568 static int fuse_unlink(struct inode *dir, struct dentry *entry)
571 struct fuse_conn *fc = get_fuse_conn(dir);
572 struct fuse_req *req = fuse_get_req(fc);
576 req->in.h.opcode = FUSE_UNLINK;
577 req->in.h.nodeid = get_node_id(dir);
579 req->in.args[0].size = entry->d_name.len + 1;
580 req->in.args[0].value = entry->d_name.name;
581 request_send(fc, req);
582 err = req->out.h.error;
583 fuse_put_request(fc, req);
585 struct inode *inode = entry->d_inode;
587 /* Set nlink to zero so the inode can be cleared, if
588 the inode does have more links this will be
589 discovered at the next lookup/getattr */
591 fuse_invalidate_attr(inode);
592 fuse_invalidate_attr(dir);
593 fuse_invalidate_entry_cache(entry);
594 } else if (err == -EINTR)
595 fuse_invalidate_entry(entry);
599 static int fuse_rmdir(struct inode *dir, struct dentry *entry)
602 struct fuse_conn *fc = get_fuse_conn(dir);
603 struct fuse_req *req = fuse_get_req(fc);
607 req->in.h.opcode = FUSE_RMDIR;
608 req->in.h.nodeid = get_node_id(dir);
610 req->in.args[0].size = entry->d_name.len + 1;
611 req->in.args[0].value = entry->d_name.name;
612 request_send(fc, req);
613 err = req->out.h.error;
614 fuse_put_request(fc, req);
616 clear_nlink(entry->d_inode);
617 fuse_invalidate_attr(dir);
618 fuse_invalidate_entry_cache(entry);
619 } else if (err == -EINTR)
620 fuse_invalidate_entry(entry);
624 static int fuse_rename(struct inode *olddir, struct dentry *oldent,
625 struct inode *newdir, struct dentry *newent)
628 struct fuse_rename_in inarg;
629 struct fuse_conn *fc = get_fuse_conn(olddir);
630 struct fuse_req *req = fuse_get_req(fc);
634 memset(&inarg, 0, sizeof(inarg));
635 inarg.newdir = get_node_id(newdir);
636 req->in.h.opcode = FUSE_RENAME;
637 req->in.h.nodeid = get_node_id(olddir);
639 req->in.args[0].size = sizeof(inarg);
640 req->in.args[0].value = &inarg;
641 req->in.args[1].size = oldent->d_name.len + 1;
642 req->in.args[1].value = oldent->d_name.name;
643 req->in.args[2].size = newent->d_name.len + 1;
644 req->in.args[2].value = newent->d_name.name;
645 request_send(fc, req);
646 err = req->out.h.error;
647 fuse_put_request(fc, req);
649 fuse_invalidate_attr(olddir);
650 if (olddir != newdir)
651 fuse_invalidate_attr(newdir);
653 /* newent will end up negative */
655 fuse_invalidate_entry_cache(newent);
656 } else if (err == -EINTR) {
657 /* If request was interrupted, DEITY only knows if the
658 rename actually took place. If the invalidation
659 fails (e.g. some process has CWD under the renamed
660 directory), then there can be inconsistency between
661 the dcache and the real filesystem. Tough luck. */
662 fuse_invalidate_entry(oldent);
664 fuse_invalidate_entry(newent);
670 static int fuse_link(struct dentry *entry, struct inode *newdir,
671 struct dentry *newent)
674 struct fuse_link_in inarg;
675 struct inode *inode = entry->d_inode;
676 struct fuse_conn *fc = get_fuse_conn(inode);
677 struct fuse_req *req = fuse_get_req(fc);
681 memset(&inarg, 0, sizeof(inarg));
682 inarg.oldnodeid = get_node_id(inode);
683 req->in.h.opcode = FUSE_LINK;
685 req->in.args[0].size = sizeof(inarg);
686 req->in.args[0].value = &inarg;
687 req->in.args[1].size = newent->d_name.len + 1;
688 req->in.args[1].value = newent->d_name.name;
689 err = create_new_entry(fc, req, newdir, newent, inode->i_mode);
690 /* Contrary to "normal" filesystems it can happen that link
691 makes two "logical" inodes point to the same "physical"
692 inode. We invalidate the attributes of the old one, so it
693 will reflect changes in the backing inode (link count,
696 if (!err || err == -EINTR)
697 fuse_invalidate_attr(inode);
701 static void fuse_fillattr(struct inode *inode, struct fuse_attr *attr,
704 stat->dev = inode->i_sb->s_dev;
705 stat->ino = attr->ino;
706 stat->mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777);
707 stat->nlink = attr->nlink;
708 stat->uid = attr->uid;
709 stat->gid = attr->gid;
710 stat->rdev = inode->i_rdev;
711 stat->atime.tv_sec = attr->atime;
712 stat->atime.tv_nsec = attr->atimensec;
713 stat->mtime.tv_sec = attr->mtime;
714 stat->mtime.tv_nsec = attr->mtimensec;
715 stat->ctime.tv_sec = attr->ctime;
716 stat->ctime.tv_nsec = attr->ctimensec;
717 stat->size = attr->size;
718 stat->blocks = attr->blocks;
719 stat->blksize = (1 << inode->i_blkbits);
722 static int fuse_do_getattr(struct inode *inode, struct kstat *stat,
726 struct fuse_getattr_in inarg;
727 struct fuse_attr_out outarg;
728 struct fuse_conn *fc = get_fuse_conn(inode);
729 struct fuse_req *req;
732 req = fuse_get_req(fc);
736 spin_lock(&fc->lock);
737 attr_version = fc->attr_version;
738 spin_unlock(&fc->lock);
740 memset(&inarg, 0, sizeof(inarg));
741 memset(&outarg, 0, sizeof(outarg));
742 /* Directories have separate file-handle space */
743 if (file && S_ISREG(inode->i_mode)) {
744 struct fuse_file *ff = file->private_data;
746 inarg.getattr_flags |= FUSE_GETATTR_FH;
749 req->in.h.opcode = FUSE_GETATTR;
750 req->in.h.nodeid = get_node_id(inode);
752 req->in.args[0].size = sizeof(inarg);
753 req->in.args[0].value = &inarg;
754 req->out.numargs = 1;
756 req->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
758 req->out.args[0].size = sizeof(outarg);
759 req->out.args[0].value = &outarg;
760 request_send(fc, req);
761 err = req->out.h.error;
762 fuse_put_request(fc, req);
764 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
765 make_bad_inode(inode);
768 fuse_change_attributes(inode, &outarg.attr,
769 attr_timeout(&outarg),
772 fuse_fillattr(inode, &outarg.attr, stat);
778 int fuse_update_attributes(struct inode *inode, struct kstat *stat,
779 struct file *file, bool *refreshed)
781 struct fuse_inode *fi = get_fuse_inode(inode);
785 if (fi->i_time < get_jiffies_64()) {
787 err = fuse_do_getattr(inode, stat, file);
792 generic_fillattr(inode, stat);
793 stat->mode = fi->orig_i_mode;
797 if (refreshed != NULL)
804 * Calling into a user-controlled filesystem gives the filesystem
805 * daemon ptrace-like capabilities over the requester process. This
806 * means, that the filesystem daemon is able to record the exact
807 * filesystem operations performed, and can also control the behavior
808 * of the requester process in otherwise impossible ways. For example
809 * it can delay the operation for arbitrary length of time allowing
810 * DoS against the requester.
812 * For this reason only those processes can call into the filesystem,
813 * for which the owner of the mount has ptrace privilege. This
814 * excludes processes started by other users, suid or sgid processes.
816 int fuse_allow_task(struct fuse_conn *fc, struct task_struct *task)
818 if (fc->flags & FUSE_ALLOW_OTHER)
821 if (task->euid == fc->user_id &&
822 task->suid == fc->user_id &&
823 task->uid == fc->user_id &&
824 task->egid == fc->group_id &&
825 task->sgid == fc->group_id &&
826 task->gid == fc->group_id)
832 static int fuse_access(struct inode *inode, int mask)
834 struct fuse_conn *fc = get_fuse_conn(inode);
835 struct fuse_req *req;
836 struct fuse_access_in inarg;
842 req = fuse_get_req(fc);
846 memset(&inarg, 0, sizeof(inarg));
848 req->in.h.opcode = FUSE_ACCESS;
849 req->in.h.nodeid = get_node_id(inode);
851 req->in.args[0].size = sizeof(inarg);
852 req->in.args[0].value = &inarg;
853 request_send(fc, req);
854 err = req->out.h.error;
855 fuse_put_request(fc, req);
856 if (err == -ENOSYS) {
864 * Check permission. The two basic access models of FUSE are:
866 * 1) Local access checking ('default_permissions' mount option) based
867 * on file mode. This is the plain old disk filesystem permission
870 * 2) "Remote" access checking, where server is responsible for
871 * checking permission in each inode operation. An exception to this
872 * is if ->permission() was invoked from sys_access() in which case an
873 * access request is sent. Execute permission is still checked
874 * locally based on file mode.
876 static int fuse_permission(struct inode *inode, int mask, struct nameidata *nd)
878 struct fuse_conn *fc = get_fuse_conn(inode);
879 bool refreshed = false;
882 if (!fuse_allow_task(fc, current))
886 * If attributes are needed, refresh them before proceeding
888 if ((fc->flags & FUSE_DEFAULT_PERMISSIONS) ||
889 ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))) {
890 err = fuse_update_attributes(inode, NULL, NULL, &refreshed);
895 if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
896 int err = generic_permission(inode, mask, NULL);
898 /* If permission is denied, try to refresh file
899 attributes. This is also needed, because the root
900 node will at first have no permissions */
901 if (err == -EACCES && !refreshed) {
902 err = fuse_do_getattr(inode, NULL, NULL);
904 err = generic_permission(inode, mask, NULL);
907 /* Note: the opposite of the above test does not
908 exist. So if permissions are revoked this won't be
909 noticed immediately, only after the attribute
910 timeout has expired */
911 } else if (nd && (nd->flags & (LOOKUP_ACCESS | LOOKUP_CHDIR))) {
912 err = fuse_access(inode, mask);
913 } else if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) {
914 if (!(inode->i_mode & S_IXUGO)) {
918 err = fuse_do_getattr(inode, NULL, NULL);
919 if (!err && !(inode->i_mode & S_IXUGO))
926 static int parse_dirfile(char *buf, size_t nbytes, struct file *file,
927 void *dstbuf, filldir_t filldir)
929 while (nbytes >= FUSE_NAME_OFFSET) {
930 struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
931 size_t reclen = FUSE_DIRENT_SIZE(dirent);
933 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
938 over = filldir(dstbuf, dirent->name, dirent->namelen,
939 file->f_pos, dirent->ino, dirent->type);
945 file->f_pos = dirent->off;
951 static int fuse_readdir(struct file *file, void *dstbuf, filldir_t filldir)
956 struct inode *inode = file->f_path.dentry->d_inode;
957 struct fuse_conn *fc = get_fuse_conn(inode);
958 struct fuse_file *ff = file->private_data;
959 struct fuse_req *req;
961 if (is_bad_inode(inode))
964 req = fuse_get_req(fc);
968 page = alloc_page(GFP_KERNEL);
970 fuse_put_request(fc, req);
974 req->pages[0] = page;
975 fuse_read_fill(req, ff, inode, file->f_pos, PAGE_SIZE, FUSE_READDIR);
976 request_send(fc, req);
977 nbytes = req->out.args[0].size;
978 err = req->out.h.error;
979 fuse_put_request(fc, req);
981 err = parse_dirfile(page_address(page), nbytes, file, dstbuf,
985 fuse_invalidate_attr(inode); /* atime changed */
989 static char *read_link(struct dentry *dentry)
991 struct inode *inode = dentry->d_inode;
992 struct fuse_conn *fc = get_fuse_conn(inode);
993 struct fuse_req *req = fuse_get_req(fc);
997 return ERR_PTR(PTR_ERR(req));
999 link = (char *) __get_free_page(GFP_KERNEL);
1001 link = ERR_PTR(-ENOMEM);
1004 req->in.h.opcode = FUSE_READLINK;
1005 req->in.h.nodeid = get_node_id(inode);
1006 req->out.argvar = 1;
1007 req->out.numargs = 1;
1008 req->out.args[0].size = PAGE_SIZE - 1;
1009 req->out.args[0].value = link;
1010 request_send(fc, req);
1011 if (req->out.h.error) {
1012 free_page((unsigned long) link);
1013 link = ERR_PTR(req->out.h.error);
1015 link[req->out.args[0].size] = '\0';
1017 fuse_put_request(fc, req);
1018 fuse_invalidate_attr(inode); /* atime changed */
1022 static void free_link(char *link)
1025 free_page((unsigned long) link);
1028 static void *fuse_follow_link(struct dentry *dentry, struct nameidata *nd)
1030 nd_set_link(nd, read_link(dentry));
1034 static void fuse_put_link(struct dentry *dentry, struct nameidata *nd, void *c)
1036 free_link(nd_get_link(nd));
1039 static int fuse_dir_open(struct inode *inode, struct file *file)
1041 return fuse_open_common(inode, file, 1);
1044 static int fuse_dir_release(struct inode *inode, struct file *file)
1046 return fuse_release_common(inode, file, 1);
1049 static int fuse_dir_fsync(struct file *file, struct dentry *de, int datasync)
1051 /* nfsd can call this with no file */
1052 return file ? fuse_fsync_common(file, de, datasync, 1) : 0;
1055 static bool update_mtime(unsigned ivalid)
1057 /* Always update if mtime is explicitly set */
1058 if (ivalid & ATTR_MTIME_SET)
1061 /* If it's an open(O_TRUNC) or an ftruncate(), don't update */
1062 if ((ivalid & ATTR_SIZE) && (ivalid & (ATTR_OPEN | ATTR_FILE)))
1065 /* In all other cases update */
1069 static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg)
1071 unsigned ivalid = iattr->ia_valid;
1073 if (ivalid & ATTR_MODE)
1074 arg->valid |= FATTR_MODE, arg->mode = iattr->ia_mode;
1075 if (ivalid & ATTR_UID)
1076 arg->valid |= FATTR_UID, arg->uid = iattr->ia_uid;
1077 if (ivalid & ATTR_GID)
1078 arg->valid |= FATTR_GID, arg->gid = iattr->ia_gid;
1079 if (ivalid & ATTR_SIZE)
1080 arg->valid |= FATTR_SIZE, arg->size = iattr->ia_size;
1081 if (ivalid & ATTR_ATIME) {
1082 arg->valid |= FATTR_ATIME;
1083 arg->atime = iattr->ia_atime.tv_sec;
1084 arg->atimensec = iattr->ia_atime.tv_nsec;
1085 if (!(ivalid & ATTR_ATIME_SET))
1086 arg->valid |= FATTR_ATIME_NOW;
1088 if ((ivalid & ATTR_MTIME) && update_mtime(ivalid)) {
1089 arg->valid |= FATTR_MTIME;
1090 arg->mtime = iattr->ia_mtime.tv_sec;
1091 arg->mtimensec = iattr->ia_mtime.tv_nsec;
1092 if (!(ivalid & ATTR_MTIME_SET))
1093 arg->valid |= FATTR_MTIME_NOW;
1098 * Set attributes, and at the same time refresh them.
1100 * Truncation is slightly complicated, because the 'truncate' request
1101 * may fail, in which case we don't want to touch the mapping.
1102 * vmtruncate() doesn't allow for this case, so do the rlimit checking
1103 * and the actual truncation by hand.
1105 static int fuse_do_setattr(struct dentry *entry, struct iattr *attr,
1108 struct inode *inode = entry->d_inode;
1109 struct fuse_conn *fc = get_fuse_conn(inode);
1110 struct fuse_req *req;
1111 struct fuse_setattr_in inarg;
1112 struct fuse_attr_out outarg;
1115 if (!fuse_allow_task(fc, current))
1118 if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
1119 err = inode_change_ok(inode, attr);
1124 if ((attr->ia_valid & ATTR_OPEN) && fc->atomic_o_trunc)
1127 if (attr->ia_valid & ATTR_SIZE) {
1128 unsigned long limit;
1129 if (IS_SWAPFILE(inode))
1131 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
1132 if (limit != RLIM_INFINITY && attr->ia_size > (loff_t) limit) {
1133 send_sig(SIGXFSZ, current, 0);
1138 req = fuse_get_req(fc);
1140 return PTR_ERR(req);
1142 memset(&inarg, 0, sizeof(inarg));
1143 memset(&outarg, 0, sizeof(outarg));
1144 iattr_to_fattr(attr, &inarg);
1146 struct fuse_file *ff = file->private_data;
1147 inarg.valid |= FATTR_FH;
1150 if (attr->ia_valid & ATTR_SIZE) {
1151 /* For mandatory locking in truncate */
1152 inarg.valid |= FATTR_LOCKOWNER;
1153 inarg.lock_owner = fuse_lock_owner_id(fc, current->files);
1155 req->in.h.opcode = FUSE_SETATTR;
1156 req->in.h.nodeid = get_node_id(inode);
1157 req->in.numargs = 1;
1158 req->in.args[0].size = sizeof(inarg);
1159 req->in.args[0].value = &inarg;
1160 req->out.numargs = 1;
1162 req->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
1164 req->out.args[0].size = sizeof(outarg);
1165 req->out.args[0].value = &outarg;
1166 request_send(fc, req);
1167 err = req->out.h.error;
1168 fuse_put_request(fc, req);
1171 fuse_invalidate_attr(inode);
1175 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
1176 make_bad_inode(inode);
1180 fuse_change_attributes(inode, &outarg.attr, attr_timeout(&outarg), 0);
1184 static int fuse_setattr(struct dentry *entry, struct iattr *attr)
1186 if (attr->ia_valid & ATTR_FILE)
1187 return fuse_do_setattr(entry, attr, attr->ia_file);
1189 return fuse_do_setattr(entry, attr, NULL);
1192 static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry,
1195 struct inode *inode = entry->d_inode;
1196 struct fuse_conn *fc = get_fuse_conn(inode);
1198 if (!fuse_allow_task(fc, current))
1201 return fuse_update_attributes(inode, stat, NULL, NULL);
1204 static int fuse_setxattr(struct dentry *entry, const char *name,
1205 const void *value, size_t size, int flags)
1207 struct inode *inode = entry->d_inode;
1208 struct fuse_conn *fc = get_fuse_conn(inode);
1209 struct fuse_req *req;
1210 struct fuse_setxattr_in inarg;
1213 if (fc->no_setxattr)
1216 req = fuse_get_req(fc);
1218 return PTR_ERR(req);
1220 memset(&inarg, 0, sizeof(inarg));
1222 inarg.flags = flags;
1223 req->in.h.opcode = FUSE_SETXATTR;
1224 req->in.h.nodeid = get_node_id(inode);
1225 req->in.numargs = 3;
1226 req->in.args[0].size = sizeof(inarg);
1227 req->in.args[0].value = &inarg;
1228 req->in.args[1].size = strlen(name) + 1;
1229 req->in.args[1].value = name;
1230 req->in.args[2].size = size;
1231 req->in.args[2].value = value;
1232 request_send(fc, req);
1233 err = req->out.h.error;
1234 fuse_put_request(fc, req);
1235 if (err == -ENOSYS) {
1236 fc->no_setxattr = 1;
1242 static ssize_t fuse_getxattr(struct dentry *entry, const char *name,
1243 void *value, size_t size)
1245 struct inode *inode = entry->d_inode;
1246 struct fuse_conn *fc = get_fuse_conn(inode);
1247 struct fuse_req *req;
1248 struct fuse_getxattr_in inarg;
1249 struct fuse_getxattr_out outarg;
1252 if (fc->no_getxattr)
1255 req = fuse_get_req(fc);
1257 return PTR_ERR(req);
1259 memset(&inarg, 0, sizeof(inarg));
1261 req->in.h.opcode = FUSE_GETXATTR;
1262 req->in.h.nodeid = get_node_id(inode);
1263 req->in.numargs = 2;
1264 req->in.args[0].size = sizeof(inarg);
1265 req->in.args[0].value = &inarg;
1266 req->in.args[1].size = strlen(name) + 1;
1267 req->in.args[1].value = name;
1268 /* This is really two different operations rolled into one */
1269 req->out.numargs = 1;
1271 req->out.argvar = 1;
1272 req->out.args[0].size = size;
1273 req->out.args[0].value = value;
1275 req->out.args[0].size = sizeof(outarg);
1276 req->out.args[0].value = &outarg;
1278 request_send(fc, req);
1279 ret = req->out.h.error;
1281 ret = size ? req->out.args[0].size : outarg.size;
1283 if (ret == -ENOSYS) {
1284 fc->no_getxattr = 1;
1288 fuse_put_request(fc, req);
1292 static ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size)
1294 struct inode *inode = entry->d_inode;
1295 struct fuse_conn *fc = get_fuse_conn(inode);
1296 struct fuse_req *req;
1297 struct fuse_getxattr_in inarg;
1298 struct fuse_getxattr_out outarg;
1301 if (!fuse_allow_task(fc, current))
1304 if (fc->no_listxattr)
1307 req = fuse_get_req(fc);
1309 return PTR_ERR(req);
1311 memset(&inarg, 0, sizeof(inarg));
1313 req->in.h.opcode = FUSE_LISTXATTR;
1314 req->in.h.nodeid = get_node_id(inode);
1315 req->in.numargs = 1;
1316 req->in.args[0].size = sizeof(inarg);
1317 req->in.args[0].value = &inarg;
1318 /* This is really two different operations rolled into one */
1319 req->out.numargs = 1;
1321 req->out.argvar = 1;
1322 req->out.args[0].size = size;
1323 req->out.args[0].value = list;
1325 req->out.args[0].size = sizeof(outarg);
1326 req->out.args[0].value = &outarg;
1328 request_send(fc, req);
1329 ret = req->out.h.error;
1331 ret = size ? req->out.args[0].size : outarg.size;
1333 if (ret == -ENOSYS) {
1334 fc->no_listxattr = 1;
1338 fuse_put_request(fc, req);
1342 static int fuse_removexattr(struct dentry *entry, const char *name)
1344 struct inode *inode = entry->d_inode;
1345 struct fuse_conn *fc = get_fuse_conn(inode);
1346 struct fuse_req *req;
1349 if (fc->no_removexattr)
1352 req = fuse_get_req(fc);
1354 return PTR_ERR(req);
1356 req->in.h.opcode = FUSE_REMOVEXATTR;
1357 req->in.h.nodeid = get_node_id(inode);
1358 req->in.numargs = 1;
1359 req->in.args[0].size = strlen(name) + 1;
1360 req->in.args[0].value = name;
1361 request_send(fc, req);
1362 err = req->out.h.error;
1363 fuse_put_request(fc, req);
1364 if (err == -ENOSYS) {
1365 fc->no_removexattr = 1;
1371 static const struct inode_operations fuse_dir_inode_operations = {
1372 .lookup = fuse_lookup,
1373 .mkdir = fuse_mkdir,
1374 .symlink = fuse_symlink,
1375 .unlink = fuse_unlink,
1376 .rmdir = fuse_rmdir,
1377 .rename = fuse_rename,
1379 .setattr = fuse_setattr,
1380 .create = fuse_create,
1381 .mknod = fuse_mknod,
1382 .permission = fuse_permission,
1383 .getattr = fuse_getattr,
1384 .setxattr = fuse_setxattr,
1385 .getxattr = fuse_getxattr,
1386 .listxattr = fuse_listxattr,
1387 .removexattr = fuse_removexattr,
1390 static const struct file_operations fuse_dir_operations = {
1391 .llseek = generic_file_llseek,
1392 .read = generic_read_dir,
1393 .readdir = fuse_readdir,
1394 .open = fuse_dir_open,
1395 .release = fuse_dir_release,
1396 .fsync = fuse_dir_fsync,
1399 static const struct inode_operations fuse_common_inode_operations = {
1400 .setattr = fuse_setattr,
1401 .permission = fuse_permission,
1402 .getattr = fuse_getattr,
1403 .setxattr = fuse_setxattr,
1404 .getxattr = fuse_getxattr,
1405 .listxattr = fuse_listxattr,
1406 .removexattr = fuse_removexattr,
1409 static const struct inode_operations fuse_symlink_inode_operations = {
1410 .setattr = fuse_setattr,
1411 .follow_link = fuse_follow_link,
1412 .put_link = fuse_put_link,
1413 .readlink = generic_readlink,
1414 .getattr = fuse_getattr,
1415 .setxattr = fuse_setxattr,
1416 .getxattr = fuse_getxattr,
1417 .listxattr = fuse_listxattr,
1418 .removexattr = fuse_removexattr,
1421 void fuse_init_common(struct inode *inode)
1423 inode->i_op = &fuse_common_inode_operations;
1426 void fuse_init_dir(struct inode *inode)
1428 inode->i_op = &fuse_dir_inode_operations;
1429 inode->i_fop = &fuse_dir_operations;
1432 void fuse_init_symlink(struct inode *inode)
1434 inode->i_op = &fuse_symlink_inode_operations;