History

fuse: Add posix ACL support

Add a new INIT flag, FUSE_POSIX_ACL, for negotiating ACL support with userspace. When it is set in the INIT response, ACL support will be enabled. ACL support also implies "default_permissions". When ACL support is enabled, the kernel will cache and have responsibility for enforcing ACLs. ACL xattrs will be passed to userspace, which is responsible for updating the ACLs in the filesystem, keeping the file mode in sync, and inheritance of default ACLs when new filesystem nodes are created.

Seth Forshee committed 10 years ago

Commit 60bcc881 parent 5e940c18 files · +155 −7Browse files

Unified · Split

8 files
  1. fs/fuse/Kconfig +1 −0
  2. fs/fuse/Makefile +1 −1
  3. fs/fuse/acl.c +99 −0
  4. fs/fuse/dir.c +16 −0
  5. fs/fuse/fuse_i.h +14 −0
  6. fs/fuse/inode.c +8 −1
  7. fs/fuse/xattr.c +13 −5
  8. include/uapi/linux/fuse.h +3 −0
fs/fuse/Kconfig+1−0
@@ -1,5 +1,6 @@
11 config FUSE_FS
22 tristate "FUSE (Filesystem in Userspace) support"
3+ select FS_POSIX_ACL
34 help
45 With FUSE it is possible to implement a fully functional filesystem
56 in a userspace program.
fs/fuse/Makefile+1−1
@@ -5,4 +5,4 @@ · 4 unmodified lines
55 obj-$(CONFIG_FUSE_FS) += fuse.o
66 obj-$(CONFIG_CUSE) += cuse.o
77
8−fuse-objs := dev.o dir.o file.o inode.o control.o xattr.o
8+fuse-objs := dev.o dir.o file.o inode.o control.o xattr.o acl.o
fs/fuse/acl.c+99−0
@@ -0,0 +1,99 @@
1+/*
2+ * FUSE: Filesystem in Userspace
3+ * Copyright (C) 2016 Canonical Ltd. <seth.forshee@canonical.com>
4+ *
5+ * This program can be distributed under the terms of the GNU GPL.
6+ * See the file COPYING.
7+ */
8+
9+#include "fuse_i.h"
10+
11+#include <linux/posix_acl.h>
12+#include <linux/posix_acl_xattr.h>
13+
14+struct posix_acl *fuse_get_acl(struct inode *inode, int type)
15+{
16+ struct fuse_conn *fc = get_fuse_conn(inode);
17+ int size;
18+ const char *name;
19+ void *value = NULL;
20+ struct posix_acl *acl;
21+
22+ if (!fc->posix_acl || fc->no_getxattr)
23+ return NULL;
24+
25+ if (type == ACL_TYPE_ACCESS)
26+ name = XATTR_NAME_POSIX_ACL_ACCESS;
27+ else if (type == ACL_TYPE_DEFAULT)
28+ name = XATTR_NAME_POSIX_ACL_DEFAULT;
29+ else
30+ return ERR_PTR(-EOPNOTSUPP);
31+
32+ value = kmalloc(PAGE_SIZE, GFP_KERNEL);
33+ if (!value)
34+ return ERR_PTR(-ENOMEM);
35+ size = fuse_getxattr(inode, name, value, PAGE_SIZE);
36+ if (size > 0)
37+ acl = posix_acl_from_xattr(&init_user_ns, value, size);
38+ else if ((size == 0) || (size == -ENODATA) ||
39+ (size == -EOPNOTSUPP && fc->no_getxattr))
40+ acl = NULL;
41+ else if (size == -ERANGE)
42+ acl = ERR_PTR(-E2BIG);
43+ else
44+ acl = ERR_PTR(size);
45+
46+ kfree(value);
47+ return acl;
48+}
49+
50+int fuse_set_acl(struct inode *inode, struct posix_acl *acl, int type)
51+{
52+ struct fuse_conn *fc = get_fuse_conn(inode);
53+ const char *name;
54+ int ret;
55+
56+ if (!fc->posix_acl || fc->no_setxattr)
57+ return -EOPNOTSUPP;
58+
59+ if (type == ACL_TYPE_ACCESS)
60+ name = XATTR_NAME_POSIX_ACL_ACCESS;
61+ else if (type == ACL_TYPE_DEFAULT)
62+ name = XATTR_NAME_POSIX_ACL_DEFAULT;
63+ else
64+ return -EINVAL;
65+
66+ if (acl) {
67+ /*
68+ * Fuse userspace is responsible for updating access
69+ * permissions in the inode, if needed. fuse_setxattr
70+ * invalidates the inode attributes, which will force
71+ * them to be refreshed the next time they are used,
72+ * and it also updates i_ctime.
73+ */
74+ size_t size = posix_acl_xattr_size(acl->a_count);
75+ void *value;
76+
77+ if (size > PAGE_SIZE)
78+ return -E2BIG;
79+
80+ value = kmalloc(size, GFP_KERNEL);
81+ if (!value)
82+ return -ENOMEM;
83+
84+ ret = posix_acl_to_xattr(&init_user_ns, acl, value, size);
85+ if (ret < 0) {
86+ kfree(value);
87+ return ret;
88+ }
89+
90+ ret = fuse_setxattr(inode, name, value, size, 0);
91+ kfree(value);
92+ } else {
93+ ret = fuse_removexattr(inode, name);
94+ }
95+ forget_all_cached_acls(inode);
96+ fuse_invalidate_attr(inode);
97+
98+ return ret;
99+}
fs/fuse/dir.c+16−0
@@ -14,6 +14,7 @@ · 13 unmodified lines
1414 #include <linux/namei.h>
1515 #include <linux/slab.h>
1616 #include <linux/xattr.h>
17+#include <linux/posix_acl.h>
1718
1819 static bool fuse_use_readdirplus(struct inode *dir, struct dir_context *ctx)
1920 {
@@ -244,6 +245,7 @@ static int fuse_dentry_revalidate(struct dentry *entry, unsigned int flags) · 224 unmodified lines
244245 if (ret || (outarg.attr.mode ^ inode->i_mode) & S_IFMT)
245246 goto invalid;
246247
248+ forget_all_cached_acls(inode);
247249 fuse_change_attributes(inode, &outarg.attr,
248250 entry_attr_timeout(&outarg),
249251 attr_version);
@@ -918,6 +920,7 @@ int fuse_update_attributes(struct inode *inode, struct kstat *stat, · 668 unmodified lines
918920
919921 if (time_before64(fi->i_time, get_jiffies_64())) {
920922 r = true;
923+ forget_all_cached_acls(inode);
921924 err = fuse_do_getattr(inode, stat, file);
922925 } else {
923926 r = false;
@@ -1065,6 +1068,7 @@ static int fuse_perm_getattr(struct inode *inode, int mask) · 141 unmodified lines
10651068 if (mask & MAY_NOT_BLOCK)
10661069 return -ECHILD;
10671070
1071+ forget_all_cached_acls(inode);
10681072 return fuse_do_getattr(inode, NULL, NULL);
10691073 }
10701074
@@ -1234,6 +1238,7 @@ retry: · 163 unmodified lines
12341238 fi->nlookup++;
12351239 spin_unlock(&fc->lock);
12361240
1241+ forget_all_cached_acls(inode);
12371242 fuse_change_attributes(inode, &o->attr,
12381243 entry_attr_timeout(o),
12391244 attr_version);
@@ -1748,6 +1753,13 @@ static int fuse_setattr(struct dentry *entry, struct iattr *attr) · 508 unmodified lines
17481753
17491754 ret = fuse_do_setattr(inode, attr, file);
17501755 if (!ret) {
1756+ /*
1757+ * If filesystem supports acls it may have updated acl xattrs in
1758+ * the filesystem, so forget cached acls for the inode.
1759+ */
1760+ if (fc->posix_acl)
1761+ forget_all_cached_acls(inode);
1762+
17511763 /* Directory mode changed, may need to revalidate access */
17521764 if (d_is_dir(entry) && (attr->ia_valid & ATTR_MODE))
17531765 fuse_invalidate_entry_cache(entry);
@@ -1785,6 +1797,8 @@ static const struct inode_operations fuse_dir_inode_operations = { · 31 unmodified lines
17851797 .getxattr = generic_getxattr,
17861798 .listxattr = fuse_listxattr,
17871799 .removexattr = generic_removexattr,
1800+ .get_acl = fuse_get_acl,
1801+ .set_acl = fuse_set_acl,
17881802 };
17891803
17901804 static const struct file_operations fuse_dir_operations = {
@@ -1806,6 +1820,8 @@ static const struct inode_operations fuse_common_inode_operations = { · 15 unmodified lines
18061820 .getxattr = generic_getxattr,
18071821 .listxattr = fuse_listxattr,
18081822 .removexattr = generic_removexattr,
1823+ .get_acl = fuse_get_acl,
1824+ .set_acl = fuse_set_acl,
18091825 };
18101826
18111827 static const struct inode_operations fuse_symlink_inode_operations = {
fs/fuse/fuse_i.h+14−0
@@ -23,6 +23,7 @@ · 22 unmodified lines
2323 #include <linux/poll.h>
2424 #include <linux/workqueue.h>
2525 #include <linux/kref.h>
26+#include <linux/xattr.h>
2627
2728 /** Max number of pages that can be used in a single read request */
2829 #define FUSE_MAX_PAGES_PER_REQ 32
@@ -627,6 +628,9 @@ struct fuse_conn { · 598 unmodified lines
627628 /** Is lseek not implemented by fs? */
628629 unsigned no_lseek:1;
629630
631+ /** Does the filesystem support posix acls? */
632+ unsigned posix_acl:1;
633+
630634 /** The number of requests waiting for completion */
631635 atomic_t num_waiting;
632636
@@ -971,7 +975,17 @@ void fuse_set_initialized(struct fuse_conn *fc); · 338 unmodified lines
971975 void fuse_unlock_inode(struct inode *inode);
972976 void fuse_lock_inode(struct inode *inode);
973977
978+int fuse_setxattr(struct inode *inode, const char *name, const void *value,
979+ size_t size, int flags);
980+ssize_t fuse_getxattr(struct inode *inode, const char *name, void *value,
981+ size_t size);
974982 ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size);
983+int fuse_removexattr(struct inode *inode, const char *name);
975984 extern const struct xattr_handler *fuse_xattr_handlers[];
985+extern const struct xattr_handler *fuse_acl_xattr_handlers[];
986+
987+struct posix_acl;
988+struct posix_acl *fuse_get_acl(struct inode *inode, int type);
989+int fuse_set_acl(struct inode *inode, struct posix_acl *acl, int type);
976990
977991 #endif /* _FS_FUSE_I_H */
fs/fuse/inode.c+8−1
@@ -20,6 +20,7 @@ · 19 unmodified lines
2020 #include <linux/random.h>
2121 #include <linux/sched.h>
2222 #include <linux/exportfs.h>
23+#include <linux/posix_acl.h>
2324
2425 MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
2526 MODULE_DESCRIPTION("Filesystem in Userspace");
@@ -340,6 +341,7 @@ int fuse_reverse_inval_inode(struct super_block *sb, u64 nodeid, · 314 unmodified lines
340341 return -ENOENT;
341342
342343 fuse_invalidate_attr(inode);
344+ forget_all_cached_acls(inode);
343345 if (offset >= 0) {
344346 pg_start = offset >> PAGE_SHIFT;
345347 if (len <= 0)
@@ -914,6 +916,11 @@ static void process_init_reply(struct fuse_conn *fc, struct fuse_req *req) · 568 unmodified lines
914916 fc->handle_killpriv = 1;
915917 if (arg->time_gran && arg->time_gran <= 1000000000)
916918 fc->sb->s_time_gran = arg->time_gran;
919+ if ((arg->flags & FUSE_POSIX_ACL)) {
920+ fc->flags |= FUSE_DEFAULT_PERMISSIONS;
921+ fc->posix_acl = 1;
922+ fc->sb->s_xattr = fuse_acl_xattr_handlers;
923+ }
917924 } else {
918925 ra_pages = fc->max_read / PAGE_SIZE;
919926 fc->no_lock = 1;
@@ -943,7 +950,7 @@ static void fuse_send_init(struct fuse_conn *fc, struct fuse_req *req) · 23 unmodified lines
943950 FUSE_FLOCK_LOCKS | FUSE_HAS_IOCTL_DIR | FUSE_AUTO_INVAL_DATA |
944951 FUSE_DO_READDIRPLUS | FUSE_READDIRPLUS_AUTO | FUSE_ASYNC_DIO |
945952 FUSE_WRITEBACK_CACHE | FUSE_NO_OPEN_SUPPORT |
946− FUSE_PARALLEL_DIROPS | FUSE_HANDLE_KILLPRIV;
953+ FUSE_PARALLEL_DIROPS | FUSE_HANDLE_KILLPRIV | FUSE_POSIX_ACL;
947954 req->in.h.opcode = FUSE_INIT;
948955 req->in.numargs = 1;
949956 req->in.args[0].size = sizeof(*arg);
fs/fuse/xattr.c+13−5
@@ -9,9 +9,10 @@ · 8 unmodified lines
99 #include "fuse_i.h"
1010
1111 #include <linux/xattr.h>
12+#include <linux/posix_acl_xattr.h>
1213
13−static int fuse_setxattr(struct inode *inode, const char *name,
14− const void *value, size_t size, int flags)
14+int fuse_setxattr(struct inode *inode, const char *name, const void *value,
15+ size_t size, int flags)
1516 {
1617 struct fuse_conn *fc = get_fuse_conn(inode);
1718 FUSE_ARGS(args);
@@ -45,8 +46,8 @@ static int fuse_setxattr(struct inode *inode, const char *name, · 27 unmodified lines
4546 return err;
4647 }
4748
48−static ssize_t fuse_getxattr(struct inode *inode, const char *name,
49− void *value, size_t size)
49+ssize_t fuse_getxattr(struct inode *inode, const char *name, void *value,
50+ size_t size)
5051 {
5152 struct fuse_conn *fc = get_fuse_conn(inode);
5253 FUSE_ARGS(args);
@@ -147,7 +148,7 @@ ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size) · 94 unmodified lines
147148 return ret;
148149 }
149150
150−static int fuse_removexattr(struct inode *inode, const char *name)
151+int fuse_removexattr(struct inode *inode, const char *name)
151152 {
152153 struct fuse_conn *fc = get_fuse_conn(inode);
153154 FUSE_ARGS(args);
@@ -201,3 +202,10 @@ const struct xattr_handler *fuse_xattr_handlers[] = { · 47 unmodified lines
201202 &fuse_xattr_handler,
202203 NULL
203204 };
205+
206+const struct xattr_handler *fuse_acl_xattr_handlers[] = {
207+ &posix_acl_access_xattr_handler,
208+ &posix_acl_default_xattr_handler,
209+ &fuse_xattr_handler,
210+ NULL
211+};
include/uapi/linux/fuse.h+3−0
@@ -111,6 +111,7 @@ · 110 unmodified lines
111111 *
112112 * 7.26
113113 * - add FUSE_HANDLE_KILLPRIV
114+ * - add FUSE_POSIX_ACL
114115 */
115116
116117 #ifndef _LINUX_FUSE_H
@@ -242,6 +243,7 @@ struct fuse_file_lock { · 125 unmodified lines
242243 * FUSE_NO_OPEN_SUPPORT: kernel supports zero-message opens
243244 * FUSE_PARALLEL_DIROPS: allow parallel lookups and readdir
244245 * FUSE_HANDLE_KILLPRIV: fs handles killing suid/sgid/cap on write/chown/trunc
246+ * FUSE_POSIX_ACL: filesystem supports posix acls
245247 */
246248 #define FUSE_ASYNC_READ (1 << 0)
247249 #define FUSE_POSIX_LOCKS (1 << 1)
@@ -263,6 +265,7 @@ struct fuse_file_lock { · 15 unmodified lines
263265 #define FUSE_NO_OPEN_SUPPORT (1 << 17)
264266 #define FUSE_PARALLEL_DIROPS (1 << 18)
265267 #define FUSE_HANDLE_KILLPRIV (1 << 19)
268+#define FUSE_POSIX_ACL (1 << 20)
266269
267270 /**
268271 * CUSE INIT request/reply flags

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t