13 #include <linux/sched.h>
15 #include <linux/slab.h>
17 #if BITS_PER_LONG >= 64
34 entry->
d_fsdata = (
void *) (
unsigned long) (time >> 32);
37 static u64 fuse_dentry_time(
struct dentry *entry)
53 static u64 time_to_jiffies(
unsigned long sec,
unsigned long nsec)
66 static void fuse_change_entry_timeout(
struct dentry *entry,
69 fuse_dentry_settime(entry,
89 get_fuse_inode(inode)->i_time = 0;
102 fuse_dentry_settime(entry, 0);
109 static void fuse_invalidate_entry(
struct dentry *entry)
121 req->
in.h.nodeid = nodeid;
123 req->
in.args[0].size = name->len + 1;
124 req->
in.args[0].value = name->
name;
125 req->
out.numargs = 1;
130 req->
out.args[0].value = outarg;
141 spin_lock(&fc->
lock);
143 spin_unlock(&fc->
lock);
157 static int fuse_dentry_revalidate(
struct dentry *entry,
unsigned int flags)
180 fc = get_fuse_conn(inode);
194 fuse_lookup_init(fc, req, get_node_id(parent->
d_inode),
198 err = req->
out.h.error;
201 if (!err && !outarg.
nodeid)
204 struct fuse_inode *fi = get_fuse_inode(inode);
205 if (outarg.
nodeid != get_node_id(inode)) {
209 spin_lock(&fc->
lock);
211 spin_unlock(&fc->
lock);
218 entry_attr_timeout(&outarg),
220 fuse_change_entry_timeout(entry, &outarg);
225 static int invalid_nodeid(
u64 nodeid)
231 .d_revalidate = fuse_dentry_revalidate,
244 static struct dentry *fuse_d_add_directory(
struct dentry *entry,
250 fuse_invalidate_entry(alias);
253 return ERR_PTR(-
EBUSY);
263 struct fuse_conn *fc = get_fuse_conn_super(sb);
288 fuse_lookup_init(fc, req, nodeid, name, outarg);
290 err = req->
out.h.error;
293 if (err || !outarg->
nodeid)
303 &outarg->
attr, entry_attr_timeout(outarg),
318 static struct dentry *fuse_lookup(
struct inode *dir,
struct dentry *entry,
325 struct fuse_conn *fc = get_fuse_conn(dir);
326 bool outarg_valid =
true;
331 outarg_valid =
false;
343 newent = fuse_d_add_directory(entry, inode);
345 err = PTR_ERR(newent);
352 entry = newent ? newent :
entry;
354 fuse_change_entry_timeout(entry, &outarg);
372 static int fuse_create_open(
struct inode *dir,
struct dentry *entry,
378 struct fuse_conn *fc = get_fuse_conn(dir);
397 goto out_put_forget_req;
402 goto out_put_request;
408 memset(&inarg, 0,
sizeof(inarg));
409 memset(&outentry, 0,
sizeof(outentry));
414 req->
in.h.nodeid = get_node_id(dir);
421 req->
out.numargs = 2;
435 if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid))
440 ff->nodeid = outentry.nodeid;
443 &outentry.
attr, entry_attr_timeout(&outentry), 0);
453 fuse_change_entry_timeout(entry, &outentry);
475 static int fuse_atomic_open(
struct inode *dir,
struct dentry *entry,
476 struct file *file,
unsigned flags,
480 struct fuse_conn *fc = get_fuse_conn(dir);
483 if (d_unhashed(entry)) {
484 res = fuse_lookup(dir, entry, 0);
501 err = fuse_create_open(dir, entry, file, flags, mode, opened);
511 err = fuse_mknod(dir, entry, mode, 0);
522 struct inode *dir,
struct dentry *entry,
536 memset(&outarg, 0,
sizeof(outarg));
537 req->
in.h.nodeid = get_node_id(dir);
538 req->
out.numargs = 1;
542 req->
out.args[0].size =
sizeof(outarg);
543 req->
out.args[0].value = &outarg;
545 err = req->
out.h.error;
548 goto out_put_forget_req;
551 if (invalid_nodeid(outarg.
nodeid))
552 goto out_put_forget_req;
555 goto out_put_forget_req;
558 &outarg.
attr, entry_attr_timeout(&outarg), 0);
581 fuse_change_entry_timeout(entry, &outarg);
590 static int fuse_mknod(
struct inode *dir,
struct dentry *entry,
umode_t mode,
594 struct fuse_conn *fc = get_fuse_conn(dir);
602 memset(&inarg, 0,
sizeof(inarg));
604 inarg.rdev = new_encode_dev(rdev);
610 req->
in.args[0].value = &inarg;
611 req->
in.args[1].size = entry->
d_name.len + 1;
612 req->
in.args[1].value = entry->
d_name.name;
613 return create_new_entry(fc, req, dir, entry, mode);
616 static int fuse_create(
struct inode *dir,
struct dentry *entry,
umode_t mode,
619 return fuse_mknod(dir, entry, mode, 0);
622 static int fuse_mkdir(
struct inode *dir,
struct dentry *entry,
umode_t mode)
625 struct fuse_conn *fc = get_fuse_conn(dir);
633 memset(&inarg, 0,
sizeof(inarg));
638 req->
in.args[0].size =
sizeof(inarg);
639 req->
in.args[0].value = &inarg;
640 req->
in.args[1].size = entry->
d_name.len + 1;
641 req->
in.args[1].value = entry->
d_name.name;
642 return create_new_entry(fc, req, dir, entry,
S_IFDIR);
645 static int fuse_symlink(
struct inode *dir,
struct dentry *entry,
648 struct fuse_conn *fc = get_fuse_conn(dir);
656 req->
in.args[0].size = entry->
d_name.len + 1;
657 req->
in.args[0].value = entry->
d_name.name;
658 req->
in.args[1].size = len;
659 req->
in.args[1].value =
link;
660 return create_new_entry(fc, req, dir, entry,
S_IFLNK);
663 static int fuse_unlink(
struct inode *dir,
struct dentry *entry)
666 struct fuse_conn *fc = get_fuse_conn(dir);
672 req->
in.h.nodeid = get_node_id(dir);
674 req->
in.args[0].size = entry->
d_name.len + 1;
675 req->
in.args[0].value = entry->
d_name.name;
677 err = req->
out.h.error;
680 struct inode *inode = entry->
d_inode;
681 struct fuse_inode *fi = get_fuse_inode(inode);
683 spin_lock(&fc->
lock);
686 spin_unlock(&fc->
lock);
690 }
else if (err == -
EINTR)
691 fuse_invalidate_entry(entry);
695 static int fuse_rmdir(
struct inode *dir,
struct dentry *entry)
698 struct fuse_conn *fc = get_fuse_conn(dir);
704 req->
in.h.nodeid = get_node_id(dir);
706 req->
in.args[0].size = entry->
d_name.len + 1;
707 req->
in.args[0].value = entry->
d_name.name;
709 err = req->
out.h.error;
715 }
else if (err == -
EINTR)
716 fuse_invalidate_entry(entry);
720 static int fuse_rename(
struct inode *olddir,
struct dentry *oldent,
721 struct inode *newdir,
struct dentry *newent)
725 struct fuse_conn *fc = get_fuse_conn(olddir);
731 memset(&inarg, 0,
sizeof(inarg));
732 inarg.newdir = get_node_id(newdir);
734 req->
in.h.nodeid = get_node_id(olddir);
736 req->
in.args[0].size =
sizeof(inarg);
737 req->
in.args[0].value = &inarg;
738 req->
in.args[1].size = oldent->
d_name.len + 1;
739 req->
in.args[1].value = oldent->
d_name.name;
740 req->
in.args[2].size = newent->
d_name.len + 1;
741 req->
in.args[2].value = newent->
d_name.name;
743 err = req->
out.h.error;
750 if (olddir != newdir)
758 }
else if (err == -
EINTR) {
764 fuse_invalidate_entry(oldent);
766 fuse_invalidate_entry(newent);
772 static int fuse_link(
struct dentry *entry,
struct inode *newdir,
777 struct inode *inode = entry->
d_inode;
778 struct fuse_conn *fc = get_fuse_conn(inode);
783 memset(&inarg, 0,
sizeof(inarg));
784 inarg.oldnodeid = get_node_id(inode);
787 req->
in.args[0].size =
sizeof(inarg);
788 req->
in.args[0].value = &inarg;
789 req->
in.args[1].size = newent->
d_name.len + 1;
790 req->
in.args[1].value = newent->
d_name.name;
791 err = create_new_entry(fc, req, newdir, newent, inode->
i_mode);
799 struct fuse_inode *fi = get_fuse_inode(inode);
801 spin_lock(&fc->
lock);
804 spin_unlock(&fc->
lock);
806 }
else if (err == -
EINTR) {
812 static void fuse_fillattr(
struct inode *inode,
struct fuse_attr *
attr,
815 unsigned int blkbits;
817 stat->
dev = inode->
i_sb->s_dev;
836 blkbits = inode->
i_sb->s_blocksize_bits;
841 static int fuse_do_getattr(
struct inode *inode,
struct kstat *stat,
847 struct fuse_conn *fc = get_fuse_conn(inode);
857 memset(&inarg, 0,
sizeof(inarg));
858 memset(&outarg, 0,
sizeof(outarg));
867 req->
in.h.nodeid = get_node_id(inode);
869 req->
in.args[0].size =
sizeof(inarg);
870 req->
in.args[0].value = &inarg;
871 req->
out.numargs = 1;
875 req->
out.args[0].size =
sizeof(outarg);
876 req->
out.args[0].value = &outarg;
878 err = req->
out.h.error;
881 if ((inode->
i_mode ^ outarg.
attr.mode) & S_IFMT) {
886 attr_timeout(&outarg),
889 fuse_fillattr(inode, &outarg.
attr, stat);
896 struct file *file,
bool *refreshed)
898 struct fuse_inode *fi = get_fuse_inode(inode);
904 err = fuse_do_getattr(inode, stat, file);
915 if (refreshed !=
NULL)
922 u64 child_nodeid,
struct qstr *name)
925 struct inode *parent;
948 fuse_invalidate_entry(entry);
950 if (child_nodeid != 0 && entry->
d_inode) {
952 if (get_node_id(entry->
d_inode) != child_nodeid) {
956 if (d_mountpoint(entry)) {
1022 static int fuse_access(
struct inode *inode,
int mask)
1024 struct fuse_conn *fc = get_fuse_conn(inode);
1034 return PTR_ERR(req);
1036 memset(&inarg, 0,
sizeof(inarg));
1039 req->
in.h.nodeid = get_node_id(inode);
1040 req->
in.numargs = 1;
1041 req->
in.args[0].size =
sizeof(inarg);
1042 req->
in.args[0].value = &inarg;
1044 err = req->
out.h.error;
1053 static int fuse_perm_getattr(
struct inode *inode,
int mask)
1058 return fuse_do_getattr(inode,
NULL,
NULL);
1074 static int fuse_permission(
struct inode *inode,
int mask)
1076 struct fuse_conn *fc = get_fuse_conn(inode);
1077 bool refreshed =
false;
1088 struct fuse_inode *fi = get_fuse_inode(inode);
1093 err = fuse_perm_getattr(inode, mask);
1105 if (err == -
EACCES && !refreshed) {
1106 err = fuse_perm_getattr(inode, mask);
1119 err = fuse_access(inode, mask);
1125 err = fuse_perm_getattr(inode, mask);
1133 static int parse_dirfile(
char *
buf,
size_t nbytes,
struct file *file,
1142 if (reclen > nbytes)
1145 over = filldir(dstbuf, dirent->
name, dirent->
namelen,
1158 static int fuse_readdir(
struct file *file,
void *dstbuf,
filldir_t filldir)
1163 struct inode *inode = file->
f_path.dentry->d_inode;
1164 struct fuse_conn *fc = get_fuse_conn(inode);
1172 return PTR_ERR(req);
1179 req->
out.argpages = 1;
1184 nbytes = req->
out.args[0].size;
1185 err = req->
out.h.error;
1188 err = parse_dirfile(
page_address(page), nbytes, file, dstbuf,
1198 struct inode *inode = dentry->
d_inode;
1199 struct fuse_conn *fc = get_fuse_conn(inode);
1204 return ERR_CAST(req);
1212 req->
in.h.nodeid = get_node_id(inode);
1213 req->
out.argvar = 1;
1214 req->
out.numargs = 1;
1216 req->
out.args[0].value =
link;
1218 if (req->
out.h.error) {
1220 link = ERR_PTR(req->
out.h.error);
1222 link[req->
out.args[0].size] =
'\0';
1229 static void free_link(
char *link)
1235 static void *fuse_follow_link(
struct dentry *dentry,
struct nameidata *nd)
1237 nd_set_link(nd, read_link(dentry));
1241 static void fuse_put_link(
struct dentry *dentry,
struct nameidata *nd,
void *
c)
1243 free_link(nd_get_link(nd));
1246 static int fuse_dir_open(
struct inode *inode,
struct file *file)
1251 static int fuse_dir_release(
struct inode *inode,
struct file *file)
1258 static int fuse_dir_fsync(
struct file *file, loff_t
start, loff_t
end,
1264 static long fuse_dir_ioctl(
struct file *file,
unsigned int cmd,
1276 static long fuse_dir_compat_ioctl(
struct file *file,
unsigned int cmd,
1288 static bool update_mtime(
unsigned ivalid)
1312 if (ivalid & ATTR_SIZE)
1321 if ((ivalid &
ATTR_MTIME) && update_mtime(ivalid)) {
1325 if (!(ivalid & ATTR_MTIME_SET))
1338 struct fuse_conn *fc = get_fuse_conn(inode);
1339 struct fuse_inode *fi = get_fuse_inode(inode);
1343 spin_lock(&fc->
lock);
1346 spin_unlock(&fc->
lock);
1356 static void __fuse_release_nowrite(
struct inode *inode)
1358 struct fuse_inode *fi = get_fuse_inode(inode);
1367 struct fuse_conn *fc = get_fuse_conn(inode);
1369 spin_lock(&fc->
lock);
1370 __fuse_release_nowrite(inode);
1371 spin_unlock(&fc->
lock);
1382 static int fuse_do_setattr(
struct dentry *entry,
struct iattr *attr,
1385 struct inode *inode = entry->
d_inode;
1386 struct fuse_conn *fc = get_fuse_conn(inode);
1390 bool is_truncate =
false;
1415 return PTR_ERR(req);
1420 memset(&inarg, 0,
sizeof(inarg));
1421 memset(&outarg, 0,
sizeof(outarg));
1422 iattr_to_fattr(attr, &inarg);
1434 req->
in.h.nodeid = get_node_id(inode);
1435 req->
in.numargs = 1;
1436 req->
in.args[0].size =
sizeof(inarg);
1437 req->
in.args[0].value = &inarg;
1438 req->
out.numargs = 1;
1442 req->
out.args[0].size =
sizeof(outarg);
1443 req->
out.args[0].value = &outarg;
1445 err = req->
out.h.error;
1453 if ((inode->
i_mode ^ outarg.
attr.mode) & S_IFMT) {
1459 spin_lock(&fc->
lock);
1461 attr_timeout(&outarg));
1463 i_size_write(inode, outarg.
attr.size);
1467 __fuse_release_nowrite(inode);
1469 spin_unlock(&fc->
lock);
1489 static int fuse_setattr(
struct dentry *entry,
struct iattr *attr)
1492 return fuse_do_setattr(entry, attr, attr->
ia_file);
1494 return fuse_do_setattr(entry, attr,
NULL);
1497 static int fuse_getattr(
struct vfsmount *mnt,
struct dentry *entry,
1500 struct inode *inode = entry->
d_inode;
1501 struct fuse_conn *fc = get_fuse_conn(inode);
1509 static int fuse_setxattr(
struct dentry *entry,
const char *name,
1510 const void *
value,
size_t size,
int flags)
1512 struct inode *inode = entry->
d_inode;
1513 struct fuse_conn *fc = get_fuse_conn(inode);
1523 return PTR_ERR(req);
1525 memset(&inarg, 0,
sizeof(inarg));
1527 inarg.flags =
flags;
1529 req->
in.h.nodeid = get_node_id(inode);
1530 req->
in.numargs = 3;
1531 req->
in.args[0].size =
sizeof(inarg);
1532 req->
in.args[0].value = &inarg;
1533 req->
in.args[1].size =
strlen(name) + 1;
1534 req->
in.args[1].value =
name;
1535 req->
in.args[2].size =
size;
1536 req->
in.args[2].value =
value;
1538 err = req->
out.h.error;
1547 static ssize_t fuse_getxattr(
struct dentry *entry,
const char *name,
1548 void *value,
size_t size)
1550 struct inode *inode = entry->
d_inode;
1551 struct fuse_conn *fc = get_fuse_conn(inode);
1562 return PTR_ERR(req);
1564 memset(&inarg, 0,
sizeof(inarg));
1567 req->
in.h.nodeid = get_node_id(inode);
1568 req->
in.numargs = 2;
1569 req->
in.args[0].size =
sizeof(inarg);
1570 req->
in.args[0].value = &inarg;
1571 req->
in.args[1].size =
strlen(name) + 1;
1572 req->
in.args[1].value =
name;
1574 req->
out.numargs = 1;
1576 req->
out.argvar = 1;
1580 req->
out.args[0].size =
sizeof(outarg);
1581 req->
out.args[0].value = &outarg;
1584 ret = req->
out.h.error;
1586 ret = size ? req->
out.args[0].size : outarg.size;
1597 static ssize_t fuse_listxattr(
struct dentry *entry,
char *
list,
size_t size)
1599 struct inode *inode = entry->
d_inode;
1600 struct fuse_conn *fc = get_fuse_conn(inode);
1614 return PTR_ERR(req);
1616 memset(&inarg, 0,
sizeof(inarg));
1619 req->
in.h.nodeid = get_node_id(inode);
1620 req->
in.numargs = 1;
1621 req->
in.args[0].size =
sizeof(inarg);
1622 req->
in.args[0].value = &inarg;
1624 req->
out.numargs = 1;
1626 req->
out.argvar = 1;
1628 req->
out.args[0].value =
list;
1630 req->
out.args[0].size =
sizeof(outarg);
1631 req->
out.args[0].value = &outarg;
1634 ret = req->
out.h.error;
1636 ret = size ? req->
out.args[0].size : outarg.size;
1647 static int fuse_removexattr(
struct dentry *entry,
const char *name)
1649 struct inode *inode = entry->
d_inode;
1650 struct fuse_conn *fc = get_fuse_conn(inode);
1659 return PTR_ERR(req);
1662 req->
in.h.nodeid = get_node_id(inode);
1663 req->
in.numargs = 1;
1664 req->
in.args[0].size =
strlen(name) + 1;
1665 req->
in.args[0].value =
name;
1667 err = req->
out.h.error;
1677 .lookup = fuse_lookup,
1678 .mkdir = fuse_mkdir,
1679 .symlink = fuse_symlink,
1680 .unlink = fuse_unlink,
1681 .rmdir = fuse_rmdir,
1682 .rename = fuse_rename,
1684 .setattr = fuse_setattr,
1685 .create = fuse_create,
1686 .atomic_open = fuse_atomic_open,
1687 .mknod = fuse_mknod,
1688 .permission = fuse_permission,
1689 .getattr = fuse_getattr,
1690 .setxattr = fuse_setxattr,
1691 .getxattr = fuse_getxattr,
1692 .listxattr = fuse_listxattr,
1693 .removexattr = fuse_removexattr,
1699 .readdir = fuse_readdir,
1700 .open = fuse_dir_open,
1701 .release = fuse_dir_release,
1702 .fsync = fuse_dir_fsync,
1703 .unlocked_ioctl = fuse_dir_ioctl,
1704 .compat_ioctl = fuse_dir_compat_ioctl,
1708 .setattr = fuse_setattr,
1709 .permission = fuse_permission,
1710 .getattr = fuse_getattr,
1711 .setxattr = fuse_setxattr,
1712 .getxattr = fuse_getxattr,
1713 .listxattr = fuse_listxattr,
1714 .removexattr = fuse_removexattr,
1718 .setattr = fuse_setattr,
1719 .follow_link = fuse_follow_link,
1720 .put_link = fuse_put_link,
1722 .getattr = fuse_getattr,
1723 .setxattr = fuse_setxattr,
1724 .getxattr = fuse_getxattr,
1725 .listxattr = fuse_listxattr,
1726 .removexattr = fuse_removexattr,
1731 inode->
i_op = &fuse_common_inode_operations;
1736 inode->
i_op = &fuse_dir_inode_operations;
1737 inode->
i_fop = &fuse_dir_operations;
1742 inode->
i_op = &fuse_symlink_inode_operations;