diff options
Diffstat (limited to 'fs/ceph/acl.c')
-rw-r--r-- | fs/ceph/acl.c | 332 |
1 files changed, 332 insertions, 0 deletions
diff --git a/fs/ceph/acl.c b/fs/ceph/acl.c new file mode 100644 index 000000000000..64fddbc1d17b --- /dev/null +++ b/fs/ceph/acl.c | |||
@@ -0,0 +1,332 @@ | |||
1 | /* | ||
2 | * linux/fs/ceph/acl.c | ||
3 | * | ||
4 | * Copyright (C) 2013 Guangliang Zhao, <lucienchao@gmail.com> | ||
5 | * | ||
6 | * This program is free software; you can redistribute it and/or | ||
7 | * modify it under the terms of the GNU General Public | ||
8 | * License v2 as published by the Free Software Foundation. | ||
9 | * | ||
10 | * This program is distributed in the hope that it will be useful, | ||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | * General Public License for more details. | ||
14 | * | ||
15 | * You should have received a copy of the GNU General Public | ||
16 | * License along with this program; if not, write to the | ||
17 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | * Boston, MA 021110-1307, USA. | ||
19 | */ | ||
20 | |||
21 | #include <linux/ceph/ceph_debug.h> | ||
22 | #include <linux/fs.h> | ||
23 | #include <linux/string.h> | ||
24 | #include <linux/xattr.h> | ||
25 | #include <linux/posix_acl_xattr.h> | ||
26 | #include <linux/posix_acl.h> | ||
27 | #include <linux/sched.h> | ||
28 | #include <linux/slab.h> | ||
29 | |||
30 | #include "super.h" | ||
31 | |||
32 | static inline void ceph_set_cached_acl(struct inode *inode, | ||
33 | int type, struct posix_acl *acl) | ||
34 | { | ||
35 | struct ceph_inode_info *ci = ceph_inode(inode); | ||
36 | |||
37 | spin_lock(&ci->i_ceph_lock); | ||
38 | if (__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 0)) | ||
39 | set_cached_acl(inode, type, acl); | ||
40 | spin_unlock(&ci->i_ceph_lock); | ||
41 | } | ||
42 | |||
43 | static inline struct posix_acl *ceph_get_cached_acl(struct inode *inode, | ||
44 | int type) | ||
45 | { | ||
46 | struct ceph_inode_info *ci = ceph_inode(inode); | ||
47 | struct posix_acl *acl = ACL_NOT_CACHED; | ||
48 | |||
49 | spin_lock(&ci->i_ceph_lock); | ||
50 | if (__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 0)) | ||
51 | acl = get_cached_acl(inode, type); | ||
52 | spin_unlock(&ci->i_ceph_lock); | ||
53 | |||
54 | return acl; | ||
55 | } | ||
56 | |||
57 | void ceph_forget_all_cached_acls(struct inode *inode) | ||
58 | { | ||
59 | forget_all_cached_acls(inode); | ||
60 | } | ||
61 | |||
62 | struct posix_acl *ceph_get_acl(struct inode *inode, int type) | ||
63 | { | ||
64 | int size; | ||
65 | const char *name; | ||
66 | char *value = NULL; | ||
67 | struct posix_acl *acl; | ||
68 | |||
69 | if (!IS_POSIXACL(inode)) | ||
70 | return NULL; | ||
71 | |||
72 | acl = ceph_get_cached_acl(inode, type); | ||
73 | if (acl != ACL_NOT_CACHED) | ||
74 | return acl; | ||
75 | |||
76 | switch (type) { | ||
77 | case ACL_TYPE_ACCESS: | ||
78 | name = POSIX_ACL_XATTR_ACCESS; | ||
79 | break; | ||
80 | case ACL_TYPE_DEFAULT: | ||
81 | name = POSIX_ACL_XATTR_DEFAULT; | ||
82 | break; | ||
83 | default: | ||
84 | BUG(); | ||
85 | } | ||
86 | |||
87 | size = __ceph_getxattr(inode, name, "", 0); | ||
88 | if (size > 0) { | ||
89 | value = kzalloc(size, GFP_NOFS); | ||
90 | if (!value) | ||
91 | return ERR_PTR(-ENOMEM); | ||
92 | size = __ceph_getxattr(inode, name, value, size); | ||
93 | } | ||
94 | |||
95 | if (size > 0) | ||
96 | acl = posix_acl_from_xattr(&init_user_ns, value, size); | ||
97 | else if (size == -ERANGE || size == -ENODATA || size == 0) | ||
98 | acl = NULL; | ||
99 | else | ||
100 | acl = ERR_PTR(-EIO); | ||
101 | |||
102 | kfree(value); | ||
103 | |||
104 | if (!IS_ERR(acl)) | ||
105 | ceph_set_cached_acl(inode, type, acl); | ||
106 | |||
107 | return acl; | ||
108 | } | ||
109 | |||
110 | static int ceph_set_acl(struct dentry *dentry, struct inode *inode, | ||
111 | struct posix_acl *acl, int type) | ||
112 | { | ||
113 | int ret = 0, size = 0; | ||
114 | const char *name = NULL; | ||
115 | char *value = NULL; | ||
116 | struct iattr newattrs; | ||
117 | umode_t new_mode = inode->i_mode, old_mode = inode->i_mode; | ||
118 | |||
119 | if (acl) { | ||
120 | ret = posix_acl_valid(acl); | ||
121 | if (ret < 0) | ||
122 | goto out; | ||
123 | } | ||
124 | |||
125 | switch (type) { | ||
126 | case ACL_TYPE_ACCESS: | ||
127 | name = POSIX_ACL_XATTR_ACCESS; | ||
128 | if (acl) { | ||
129 | ret = posix_acl_equiv_mode(acl, &new_mode); | ||
130 | if (ret < 0) | ||
131 | goto out; | ||
132 | if (ret == 0) | ||
133 | acl = NULL; | ||
134 | } | ||
135 | break; | ||
136 | case ACL_TYPE_DEFAULT: | ||
137 | if (!S_ISDIR(inode->i_mode)) { | ||
138 | ret = acl ? -EINVAL : 0; | ||
139 | goto out; | ||
140 | } | ||
141 | name = POSIX_ACL_XATTR_DEFAULT; | ||
142 | break; | ||
143 | default: | ||
144 | ret = -EINVAL; | ||
145 | goto out; | ||
146 | } | ||
147 | |||
148 | if (acl) { | ||
149 | size = posix_acl_xattr_size(acl->a_count); | ||
150 | value = kmalloc(size, GFP_NOFS); | ||
151 | if (!value) { | ||
152 | ret = -ENOMEM; | ||
153 | goto out; | ||
154 | } | ||
155 | |||
156 | ret = posix_acl_to_xattr(&init_user_ns, acl, value, size); | ||
157 | if (ret < 0) | ||
158 | goto out_free; | ||
159 | } | ||
160 | |||
161 | if (new_mode != old_mode) { | ||
162 | newattrs.ia_mode = new_mode; | ||
163 | newattrs.ia_valid = ATTR_MODE; | ||
164 | ret = ceph_setattr(dentry, &newattrs); | ||
165 | if (ret) | ||
166 | goto out_free; | ||
167 | } | ||
168 | |||
169 | if (value) | ||
170 | ret = __ceph_setxattr(dentry, name, value, size, 0); | ||
171 | else | ||
172 | ret = __ceph_removexattr(dentry, name); | ||
173 | |||
174 | if (ret) { | ||
175 | if (new_mode != old_mode) { | ||
176 | newattrs.ia_mode = old_mode; | ||
177 | newattrs.ia_valid = ATTR_MODE; | ||
178 | ceph_setattr(dentry, &newattrs); | ||
179 | } | ||
180 | goto out_free; | ||
181 | } | ||
182 | |||
183 | ceph_set_cached_acl(inode, type, acl); | ||
184 | |||
185 | out_free: | ||
186 | kfree(value); | ||
187 | out: | ||
188 | return ret; | ||
189 | } | ||
190 | |||
191 | int ceph_init_acl(struct dentry *dentry, struct inode *inode, struct inode *dir) | ||
192 | { | ||
193 | struct posix_acl *acl = NULL; | ||
194 | int ret = 0; | ||
195 | |||
196 | if (!S_ISLNK(inode->i_mode)) { | ||
197 | if (IS_POSIXACL(dir)) { | ||
198 | acl = ceph_get_acl(dir, ACL_TYPE_DEFAULT); | ||
199 | if (IS_ERR(acl)) { | ||
200 | ret = PTR_ERR(acl); | ||
201 | goto out; | ||
202 | } | ||
203 | } | ||
204 | |||
205 | if (!acl) | ||
206 | inode->i_mode &= ~current_umask(); | ||
207 | } | ||
208 | |||
209 | if (IS_POSIXACL(dir) && acl) { | ||
210 | if (S_ISDIR(inode->i_mode)) { | ||
211 | ret = ceph_set_acl(dentry, inode, acl, | ||
212 | ACL_TYPE_DEFAULT); | ||
213 | if (ret) | ||
214 | goto out_release; | ||
215 | } | ||
216 | ret = posix_acl_create(&acl, GFP_NOFS, &inode->i_mode); | ||
217 | if (ret < 0) | ||
218 | goto out; | ||
219 | else if (ret > 0) | ||
220 | ret = ceph_set_acl(dentry, inode, acl, ACL_TYPE_ACCESS); | ||
221 | else | ||
222 | cache_no_acl(inode); | ||
223 | } else { | ||
224 | cache_no_acl(inode); | ||
225 | } | ||
226 | |||
227 | out_release: | ||
228 | posix_acl_release(acl); | ||
229 | out: | ||
230 | return ret; | ||
231 | } | ||
232 | |||
233 | int ceph_acl_chmod(struct dentry *dentry, struct inode *inode) | ||
234 | { | ||
235 | struct posix_acl *acl; | ||
236 | int ret = 0; | ||
237 | |||
238 | if (S_ISLNK(inode->i_mode)) { | ||
239 | ret = -EOPNOTSUPP; | ||
240 | goto out; | ||
241 | } | ||
242 | |||
243 | if (!IS_POSIXACL(inode)) | ||
244 | goto out; | ||
245 | |||
246 | acl = ceph_get_acl(inode, ACL_TYPE_ACCESS); | ||
247 | if (IS_ERR_OR_NULL(acl)) { | ||
248 | ret = PTR_ERR(acl); | ||
249 | goto out; | ||
250 | } | ||
251 | |||
252 | ret = posix_acl_chmod(&acl, GFP_KERNEL, inode->i_mode); | ||
253 | if (ret) | ||
254 | goto out; | ||
255 | ret = ceph_set_acl(dentry, inode, acl, ACL_TYPE_ACCESS); | ||
256 | posix_acl_release(acl); | ||
257 | out: | ||
258 | return ret; | ||
259 | } | ||
260 | |||
261 | static int ceph_xattr_acl_get(struct dentry *dentry, const char *name, | ||
262 | void *value, size_t size, int type) | ||
263 | { | ||
264 | struct posix_acl *acl; | ||
265 | int ret = 0; | ||
266 | |||
267 | if (!IS_POSIXACL(dentry->d_inode)) | ||
268 | return -EOPNOTSUPP; | ||
269 | |||
270 | acl = ceph_get_acl(dentry->d_inode, type); | ||
271 | if (IS_ERR(acl)) | ||
272 | return PTR_ERR(acl); | ||
273 | if (acl == NULL) | ||
274 | return -ENODATA; | ||
275 | |||
276 | ret = posix_acl_to_xattr(&init_user_ns, acl, value, size); | ||
277 | posix_acl_release(acl); | ||
278 | |||
279 | return ret; | ||
280 | } | ||
281 | |||
282 | static int ceph_xattr_acl_set(struct dentry *dentry, const char *name, | ||
283 | const void *value, size_t size, int flags, int type) | ||
284 | { | ||
285 | int ret = 0; | ||
286 | struct posix_acl *acl = NULL; | ||
287 | |||
288 | if (!inode_owner_or_capable(dentry->d_inode)) { | ||
289 | ret = -EPERM; | ||
290 | goto out; | ||
291 | } | ||
292 | |||
293 | if (!IS_POSIXACL(dentry->d_inode)) { | ||
294 | ret = -EOPNOTSUPP; | ||
295 | goto out; | ||
296 | } | ||
297 | |||
298 | if (value) { | ||
299 | acl = posix_acl_from_xattr(&init_user_ns, value, size); | ||
300 | if (IS_ERR(acl)) { | ||
301 | ret = PTR_ERR(acl); | ||
302 | goto out; | ||
303 | } | ||
304 | |||
305 | if (acl) { | ||
306 | ret = posix_acl_valid(acl); | ||
307 | if (ret) | ||
308 | goto out_release; | ||
309 | } | ||
310 | } | ||
311 | |||
312 | ret = ceph_set_acl(dentry, dentry->d_inode, acl, type); | ||
313 | |||
314 | out_release: | ||
315 | posix_acl_release(acl); | ||
316 | out: | ||
317 | return ret; | ||
318 | } | ||
319 | |||
320 | const struct xattr_handler ceph_xattr_acl_default_handler = { | ||
321 | .prefix = POSIX_ACL_XATTR_DEFAULT, | ||
322 | .flags = ACL_TYPE_DEFAULT, | ||
323 | .get = ceph_xattr_acl_get, | ||
324 | .set = ceph_xattr_acl_set, | ||
325 | }; | ||
326 | |||
327 | const struct xattr_handler ceph_xattr_acl_access_handler = { | ||
328 | .prefix = POSIX_ACL_XATTR_ACCESS, | ||
329 | .flags = ACL_TYPE_ACCESS, | ||
330 | .get = ceph_xattr_acl_get, | ||
331 | .set = ceph_xattr_acl_set, | ||
332 | }; | ||