diff options
author | Josef Bacik <jbacik@redhat.com> | 2008-07-24 12:16:36 -0400 |
---|---|---|
committer | Chris Mason <chris.mason@oracle.com> | 2008-09-25 11:04:05 -0400 |
commit | 33268eaf0b3db5e2bd12c0ada81a8e8f87a46d68 (patch) | |
tree | 93b6d00a73402b59c2ea7ae7434138feecefc085 /fs/btrfs/acl.c | |
parent | 6099afe88fe64b2f47c43a8a71c13be3a416bbf7 (diff) |
Btrfs: Add ACL support
Signed-off-by: Chris Mason <chris.mason@oracle.com>
Diffstat (limited to 'fs/btrfs/acl.c')
-rw-r--r-- | fs/btrfs/acl.c | 296 |
1 files changed, 261 insertions, 35 deletions
diff --git a/fs/btrfs/acl.c b/fs/btrfs/acl.c index ff0fcc72848a..b192659b4f5f 100644 --- a/fs/btrfs/acl.c +++ b/fs/btrfs/acl.c | |||
@@ -20,76 +20,302 @@ | |||
20 | #include <linux/string.h> | 20 | #include <linux/string.h> |
21 | #include <linux/xattr.h> | 21 | #include <linux/xattr.h> |
22 | #include <linux/posix_acl_xattr.h> | 22 | #include <linux/posix_acl_xattr.h> |
23 | #include <linux/posix_acl.h> | ||
23 | #include <linux/sched.h> | 24 | #include <linux/sched.h> |
25 | |||
24 | #include "ctree.h" | 26 | #include "ctree.h" |
27 | #include "btrfs_inode.h" | ||
25 | #include "xattr.h" | 28 | #include "xattr.h" |
26 | #ifndef is_owner_or_cap | 29 | |
27 | #define is_owner_or_cap(inode) \ | 30 | static void btrfs_update_cached_acl(struct inode *inode, |
28 | ((current->fsuid == (inode)->i_uid) || capable(CAP_FOWNER)) | 31 | struct posix_acl **p_acl, |
29 | #endif | 32 | struct posix_acl *acl) |
33 | { | ||
34 | spin_lock(&inode->i_lock); | ||
35 | if (*p_acl && *p_acl != BTRFS_ACL_NOT_CACHED) | ||
36 | posix_acl_release(*p_acl); | ||
37 | *p_acl = posix_acl_dup(acl); | ||
38 | spin_unlock(&inode->i_lock); | ||
39 | } | ||
40 | |||
41 | static struct posix_acl *btrfs_get_acl(struct inode *inode, int type) | ||
42 | { | ||
43 | int size, name_index; | ||
44 | char *value = NULL; | ||
45 | struct posix_acl *acl = NULL, **p_acl; | ||
46 | |||
47 | switch (type) { | ||
48 | case ACL_TYPE_ACCESS: | ||
49 | name_index = BTRFS_XATTR_INDEX_POSIX_ACL_ACCESS; | ||
50 | p_acl = &BTRFS_I(inode)->i_acl; | ||
51 | break; | ||
52 | case ACL_TYPE_DEFAULT: | ||
53 | name_index = BTRFS_XATTR_INDEX_POSIX_ACL_DEFAULT; | ||
54 | p_acl = &BTRFS_I(inode)->i_default_acl; | ||
55 | break; | ||
56 | default: | ||
57 | return ERR_PTR(-EINVAL); | ||
58 | } | ||
59 | |||
60 | spin_lock(&inode->i_lock); | ||
61 | if (*p_acl != BTRFS_ACL_NOT_CACHED) | ||
62 | acl = posix_acl_dup(*p_acl); | ||
63 | spin_unlock(&inode->i_lock); | ||
64 | |||
65 | if (acl) | ||
66 | return acl; | ||
67 | |||
68 | |||
69 | size = btrfs_xattr_get(inode, name_index, "", NULL, 0); | ||
70 | if (size > 0) { | ||
71 | value = kzalloc(size, GFP_NOFS); | ||
72 | if (!value) | ||
73 | return ERR_PTR(-ENOMEM); | ||
74 | size = btrfs_xattr_get(inode, name_index, "", value, size); | ||
75 | if (size > 0) { | ||
76 | acl = posix_acl_from_xattr(value, size); | ||
77 | btrfs_update_cached_acl(inode, p_acl, acl); | ||
78 | } | ||
79 | kfree(value); | ||
80 | } else if (size == -ENOENT) { | ||
81 | acl = NULL; | ||
82 | btrfs_update_cached_acl(inode, p_acl, acl); | ||
83 | } | ||
84 | |||
85 | return acl; | ||
86 | } | ||
87 | |||
88 | static int btrfs_xattr_get_acl(struct inode *inode, int type, | ||
89 | void *value, size_t size) | ||
90 | { | ||
91 | struct posix_acl *acl; | ||
92 | int ret = 0; | ||
93 | |||
94 | acl = btrfs_get_acl(inode, type); | ||
95 | |||
96 | if (IS_ERR(acl)) | ||
97 | return PTR_ERR(acl); | ||
98 | if (acl == NULL) | ||
99 | return -ENODATA; | ||
100 | ret = posix_acl_to_xattr(acl, value, size); | ||
101 | posix_acl_release(acl); | ||
102 | |||
103 | return ret; | ||
104 | } | ||
105 | |||
106 | /* | ||
107 | * Needs to be called with fs_mutex held | ||
108 | */ | ||
109 | static int btrfs_set_acl(struct inode *inode, struct posix_acl *acl, int type) | ||
110 | { | ||
111 | int ret, name_index = 0, size = 0; | ||
112 | struct posix_acl **p_acl; | ||
113 | char *value = NULL; | ||
114 | mode_t mode; | ||
115 | |||
116 | if (acl) { | ||
117 | ret = posix_acl_valid(acl); | ||
118 | if (ret < 0) | ||
119 | return ret; | ||
120 | ret = 0; | ||
121 | } | ||
122 | |||
123 | switch (type) { | ||
124 | case ACL_TYPE_ACCESS: | ||
125 | mode = inode->i_mode; | ||
126 | ret = posix_acl_equiv_mode(acl, &mode); | ||
127 | if (ret < 0) | ||
128 | return ret; | ||
129 | ret = 0; | ||
130 | inode->i_mode = mode; | ||
131 | name_index = BTRFS_XATTR_INDEX_POSIX_ACL_ACCESS; | ||
132 | p_acl = &BTRFS_I(inode)->i_acl; | ||
133 | break; | ||
134 | case ACL_TYPE_DEFAULT: | ||
135 | if (!S_ISDIR(inode->i_mode)) | ||
136 | return acl ? -EINVAL : 0; | ||
137 | name_index = BTRFS_XATTR_INDEX_POSIX_ACL_DEFAULT; | ||
138 | p_acl = &BTRFS_I(inode)->i_default_acl; | ||
139 | break; | ||
140 | default: | ||
141 | return -EINVAL; | ||
142 | } | ||
143 | |||
144 | if (acl) { | ||
145 | size = posix_acl_xattr_size(acl->a_count); | ||
146 | value = kmalloc(size, GFP_NOFS); | ||
147 | if (!value) { | ||
148 | ret = -ENOMEM; | ||
149 | goto out; | ||
150 | } | ||
151 | |||
152 | ret = posix_acl_to_xattr(acl, value, size); | ||
153 | if (ret < 0) | ||
154 | goto out; | ||
155 | } | ||
156 | |||
157 | ret = btrfs_xattr_set(inode, name_index, "", value, size, 0); | ||
158 | |||
159 | out: | ||
160 | if (value) | ||
161 | kfree(value); | ||
162 | |||
163 | if (!ret) | ||
164 | btrfs_update_cached_acl(inode, p_acl, acl); | ||
165 | |||
166 | return ret; | ||
167 | } | ||
30 | 168 | ||
31 | static int btrfs_xattr_set_acl(struct inode *inode, int type, | 169 | static int btrfs_xattr_set_acl(struct inode *inode, int type, |
32 | const void *value, size_t size) | 170 | const void *value, size_t size) |
33 | { | 171 | { |
34 | int ret = 0; | 172 | int ret = 0; |
35 | struct posix_acl *acl; | 173 | struct posix_acl *acl = NULL; |
36 | 174 | ||
37 | if (!is_owner_or_cap(inode)) | ||
38 | return -EPERM; | ||
39 | if (value) { | 175 | if (value) { |
40 | acl = posix_acl_from_xattr(value, size); | 176 | acl = posix_acl_from_xattr(value, size); |
41 | if (acl == NULL) { | 177 | if (acl == NULL) { |
42 | value = NULL; | 178 | value = NULL; |
43 | size = 0; | 179 | size = 0; |
44 | } else if (IS_ERR(acl)) { | 180 | } else if (IS_ERR(acl)) { |
45 | ret = PTR_ERR(acl); | 181 | return PTR_ERR(acl); |
46 | } else { | ||
47 | ret = posix_acl_valid(acl); | ||
48 | posix_acl_release(acl); | ||
49 | } | 182 | } |
50 | if (ret) | ||
51 | return ret; | ||
52 | } | 183 | } |
53 | return btrfs_xattr_set(inode, type, "", value, size, 0); | ||
54 | } | ||
55 | 184 | ||
56 | static int btrfs_xattr_get_acl(struct inode *inode, int type, | 185 | ret = btrfs_set_acl(inode, acl, type); |
57 | void *value, size_t size) | 186 | |
58 | { | 187 | posix_acl_release(acl); |
59 | return btrfs_xattr_get(inode, type, "", value, size); | 188 | |
189 | return ret; | ||
60 | } | 190 | } |
191 | |||
192 | |||
61 | static int btrfs_xattr_acl_access_get(struct inode *inode, const char *name, | 193 | static int btrfs_xattr_acl_access_get(struct inode *inode, const char *name, |
62 | void *value, size_t size) | 194 | void *value, size_t size) |
63 | { | 195 | { |
64 | if (*name != '\0') | 196 | return btrfs_xattr_get_acl(inode, ACL_TYPE_ACCESS, value, size); |
65 | return -EINVAL; | ||
66 | return btrfs_xattr_get_acl(inode, BTRFS_XATTR_INDEX_POSIX_ACL_ACCESS, | ||
67 | value, size); | ||
68 | } | 197 | } |
198 | |||
69 | static int btrfs_xattr_acl_access_set(struct inode *inode, const char *name, | 199 | static int btrfs_xattr_acl_access_set(struct inode *inode, const char *name, |
70 | const void *value, size_t size, int flags) | 200 | const void *value, size_t size, int flags) |
71 | { | 201 | { |
72 | if (*name != '\0') | 202 | return btrfs_xattr_set_acl(inode, ACL_TYPE_ACCESS, value, size); |
73 | return -EINVAL; | ||
74 | return btrfs_xattr_set_acl(inode, BTRFS_XATTR_INDEX_POSIX_ACL_ACCESS, | ||
75 | value, size); | ||
76 | } | 203 | } |
204 | |||
77 | static int btrfs_xattr_acl_default_get(struct inode *inode, const char *name, | 205 | static int btrfs_xattr_acl_default_get(struct inode *inode, const char *name, |
78 | void *value, size_t size) | 206 | void *value, size_t size) |
79 | { | 207 | { |
80 | if (*name != '\0') | 208 | return btrfs_xattr_get_acl(inode, ACL_TYPE_DEFAULT, value, size); |
81 | return -EINVAL; | ||
82 | return btrfs_xattr_get_acl(inode, BTRFS_XATTR_INDEX_POSIX_ACL_DEFAULT, | ||
83 | value, size); | ||
84 | } | 209 | } |
210 | |||
85 | static int btrfs_xattr_acl_default_set(struct inode *inode, const char *name, | 211 | static int btrfs_xattr_acl_default_set(struct inode *inode, const char *name, |
86 | const void *value, size_t size, int flags) | 212 | const void *value, size_t size, int flags) |
87 | { | 213 | { |
88 | if (*name != '\0') | 214 | return btrfs_xattr_set_acl(inode, ACL_TYPE_DEFAULT, value, size); |
89 | return -EINVAL; | 215 | } |
90 | return btrfs_xattr_set_acl(inode, BTRFS_XATTR_INDEX_POSIX_ACL_DEFAULT, | 216 | |
91 | value, size); | 217 | int btrfs_check_acl(struct inode *inode, int mask) |
218 | { | ||
219 | struct posix_acl *acl; | ||
220 | int error = -EAGAIN; | ||
221 | |||
222 | acl = btrfs_get_acl(inode, ACL_TYPE_ACCESS); | ||
223 | |||
224 | if (IS_ERR(acl)) | ||
225 | return PTR_ERR(acl); | ||
226 | if (acl) { | ||
227 | error = posix_acl_permission(inode, acl, mask); | ||
228 | posix_acl_release(acl); | ||
229 | } | ||
230 | |||
231 | return error; | ||
232 | } | ||
233 | |||
234 | /* | ||
235 | * btrfs_init_acl is already generally called under fs_mutex, so the locking | ||
236 | * stuff has been fixed to work with that. If the locking stuff changes, we | ||
237 | * need to re-evaluate the acl locking stuff. | ||
238 | */ | ||
239 | int btrfs_init_acl(struct inode *inode, struct inode *dir) | ||
240 | { | ||
241 | struct posix_acl *acl = NULL; | ||
242 | int ret = 0; | ||
243 | |||
244 | /* this happens with subvols */ | ||
245 | if (!dir) | ||
246 | return 0; | ||
247 | |||
248 | if (!S_ISLNK(inode->i_mode)) { | ||
249 | if (IS_POSIXACL(dir)) { | ||
250 | acl = btrfs_get_acl(dir, ACL_TYPE_DEFAULT); | ||
251 | if (IS_ERR(acl)) | ||
252 | return PTR_ERR(acl); | ||
253 | } | ||
254 | |||
255 | if (!acl) | ||
256 | inode->i_mode &= ~current->fs->umask; | ||
257 | } | ||
258 | |||
259 | if (IS_POSIXACL(dir) && acl) { | ||
260 | struct posix_acl *clone; | ||
261 | mode_t mode; | ||
262 | |||
263 | if (S_ISDIR(inode->i_mode)) { | ||
264 | ret = btrfs_set_acl(inode, acl, ACL_TYPE_DEFAULT); | ||
265 | if (ret) | ||
266 | goto failed; | ||
267 | } | ||
268 | clone = posix_acl_clone(acl, GFP_NOFS); | ||
269 | ret = -ENOMEM; | ||
270 | if (!clone) | ||
271 | goto failed; | ||
272 | |||
273 | mode = inode->i_mode; | ||
274 | ret = posix_acl_create_masq(clone, &mode); | ||
275 | if (ret >= 0) { | ||
276 | inode->i_mode = mode; | ||
277 | if (ret > 0) { | ||
278 | /* we need an acl */ | ||
279 | ret = btrfs_set_acl(inode, clone, | ||
280 | ACL_TYPE_ACCESS); | ||
281 | } | ||
282 | } | ||
283 | } | ||
284 | failed: | ||
285 | posix_acl_release(acl); | ||
286 | |||
287 | return ret; | ||
288 | } | ||
289 | |||
290 | int btrfs_acl_chmod(struct inode *inode) | ||
291 | { | ||
292 | struct posix_acl *acl, *clone; | ||
293 | int ret = 0; | ||
294 | |||
295 | if (S_ISLNK(inode->i_mode)) | ||
296 | return -EOPNOTSUPP; | ||
297 | |||
298 | if (!IS_POSIXACL(inode)) | ||
299 | return 0; | ||
300 | |||
301 | acl = btrfs_get_acl(inode, ACL_TYPE_ACCESS); | ||
302 | if (IS_ERR(acl) || !acl) | ||
303 | return PTR_ERR(acl); | ||
304 | |||
305 | clone = posix_acl_clone(acl, GFP_KERNEL); | ||
306 | posix_acl_release(acl); | ||
307 | if (!clone) | ||
308 | return -ENOMEM; | ||
309 | |||
310 | ret = posix_acl_chmod_masq(clone, inode->i_mode); | ||
311 | if (!ret) | ||
312 | ret = btrfs_set_acl(inode, clone, ACL_TYPE_ACCESS); | ||
313 | |||
314 | posix_acl_release(clone); | ||
315 | |||
316 | return ret; | ||
92 | } | 317 | } |
318 | |||
93 | struct xattr_handler btrfs_xattr_acl_default_handler = { | 319 | struct xattr_handler btrfs_xattr_acl_default_handler = { |
94 | .prefix = POSIX_ACL_XATTR_DEFAULT, | 320 | .prefix = POSIX_ACL_XATTR_DEFAULT, |
95 | .list = btrfs_xattr_generic_list, | 321 | .list = btrfs_xattr_generic_list, |