]>
Commit | Line | Data |
---|---|---|
85ff872d AK |
1 | /* |
2 | * Copyright IBM Corporation, 2010 | |
3 | * Author Aneesh Kumar K.V <[email protected]> | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify it | |
6 | * under the terms of version 2.1 of the GNU Lesser General Public License | |
7 | * as published by the Free Software Foundation. | |
8 | * | |
9 | * This program is distributed in the hope that it would be useful, but | |
10 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
12 | * | |
13 | */ | |
14 | ||
15 | #include <linux/module.h> | |
16 | #include <linux/fs.h> | |
17 | #include <net/9p/9p.h> | |
18 | #include <net/9p/client.h> | |
19 | #include <linux/slab.h> | |
22d8dcdf | 20 | #include <linux/sched.h> |
85ff872d AK |
21 | #include <linux/posix_acl_xattr.h> |
22 | #include "xattr.h" | |
23 | #include "acl.h" | |
22d8dcdf | 24 | #include "v9fs_vfs.h" |
76381a42 | 25 | #include "v9fs.h" |
85ff872d AK |
26 | |
27 | static struct posix_acl *__v9fs_get_acl(struct p9_fid *fid, char *name) | |
28 | { | |
29 | ssize_t size; | |
30 | void *value = NULL; | |
009ca389 | 31 | struct posix_acl *acl = NULL; |
85ff872d AK |
32 | |
33 | size = v9fs_fid_xattr_get(fid, name, NULL, 0); | |
34 | if (size > 0) { | |
35 | value = kzalloc(size, GFP_NOFS); | |
36 | if (!value) | |
37 | return ERR_PTR(-ENOMEM); | |
38 | size = v9fs_fid_xattr_get(fid, name, value, size); | |
39 | if (size > 0) { | |
40 | acl = posix_acl_from_xattr(value, size); | |
41 | if (IS_ERR(acl)) | |
42 | goto err_out; | |
43 | } | |
44 | } else if (size == -ENODATA || size == 0 || | |
45 | size == -ENOSYS || size == -EOPNOTSUPP) { | |
46 | acl = NULL; | |
47 | } else | |
48 | acl = ERR_PTR(-EIO); | |
49 | ||
50 | err_out: | |
51 | kfree(value); | |
52 | return acl; | |
53 | } | |
54 | ||
55 | int v9fs_get_acl(struct inode *inode, struct p9_fid *fid) | |
56 | { | |
57 | int retval = 0; | |
58 | struct posix_acl *pacl, *dacl; | |
76381a42 | 59 | struct v9fs_session_info *v9ses; |
85ff872d | 60 | |
76381a42 AK |
61 | v9ses = v9fs_inode2v9ses(inode); |
62 | if ((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT) { | |
63 | set_cached_acl(inode, ACL_TYPE_DEFAULT, NULL); | |
64 | set_cached_acl(inode, ACL_TYPE_ACCESS, NULL); | |
65 | return 0; | |
66 | } | |
85ff872d AK |
67 | /* get the default/access acl values and cache them */ |
68 | dacl = __v9fs_get_acl(fid, POSIX_ACL_XATTR_DEFAULT); | |
69 | pacl = __v9fs_get_acl(fid, POSIX_ACL_XATTR_ACCESS); | |
70 | ||
71 | if (!IS_ERR(dacl) && !IS_ERR(pacl)) { | |
72 | set_cached_acl(inode, ACL_TYPE_DEFAULT, dacl); | |
73 | set_cached_acl(inode, ACL_TYPE_ACCESS, pacl); | |
74 | posix_acl_release(dacl); | |
75 | posix_acl_release(pacl); | |
76 | } else | |
77 | retval = -EIO; | |
78 | ||
79 | return retval; | |
80 | } | |
81 | ||
82 | static struct posix_acl *v9fs_get_cached_acl(struct inode *inode, int type) | |
83 | { | |
84 | struct posix_acl *acl; | |
85 | /* | |
86 | * 9p Always cache the acl value when | |
87 | * instantiating the inode (v9fs_inode_from_fid) | |
88 | */ | |
89 | acl = get_cached_acl(inode, type); | |
90 | BUG_ON(acl == ACL_NOT_CACHED); | |
91 | return acl; | |
92 | } | |
93 | ||
b74c79e9 | 94 | int v9fs_check_acl(struct inode *inode, int mask, unsigned int flags) |
85ff872d | 95 | { |
76381a42 AK |
96 | struct posix_acl *acl; |
97 | struct v9fs_session_info *v9ses; | |
98 | ||
b74c79e9 NP |
99 | if (flags & IPERM_FLAG_RCU) |
100 | return -ECHILD; | |
101 | ||
76381a42 AK |
102 | v9ses = v9fs_inode2v9ses(inode); |
103 | if ((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT) { | |
104 | /* | |
105 | * On access = client mode get the acl | |
106 | * values from the server | |
107 | */ | |
108 | return 0; | |
109 | } | |
110 | acl = v9fs_get_cached_acl(inode, ACL_TYPE_ACCESS); | |
85ff872d AK |
111 | |
112 | if (IS_ERR(acl)) | |
113 | return PTR_ERR(acl); | |
114 | if (acl) { | |
115 | int error = posix_acl_permission(inode, acl, mask); | |
116 | posix_acl_release(acl); | |
117 | return error; | |
118 | } | |
119 | return -EAGAIN; | |
120 | } | |
7a4566b0 | 121 | |
6e8dc555 AK |
122 | static int v9fs_set_acl(struct dentry *dentry, int type, struct posix_acl *acl) |
123 | { | |
124 | int retval; | |
125 | char *name; | |
126 | size_t size; | |
127 | void *buffer; | |
128 | struct inode *inode = dentry->d_inode; | |
129 | ||
130 | set_cached_acl(inode, type, acl); | |
131 | /* Set a setxattr request to server */ | |
132 | size = posix_acl_xattr_size(acl->a_count); | |
133 | buffer = kmalloc(size, GFP_KERNEL); | |
134 | if (!buffer) | |
135 | return -ENOMEM; | |
136 | retval = posix_acl_to_xattr(acl, buffer, size); | |
137 | if (retval < 0) | |
138 | goto err_free_out; | |
139 | switch (type) { | |
140 | case ACL_TYPE_ACCESS: | |
141 | name = POSIX_ACL_XATTR_ACCESS; | |
142 | break; | |
143 | case ACL_TYPE_DEFAULT: | |
144 | name = POSIX_ACL_XATTR_DEFAULT; | |
145 | break; | |
146 | default: | |
147 | BUG(); | |
148 | } | |
149 | retval = v9fs_xattr_set(dentry, name, buffer, size, 0); | |
150 | err_free_out: | |
151 | kfree(buffer); | |
152 | return retval; | |
153 | } | |
154 | ||
155 | int v9fs_acl_chmod(struct dentry *dentry) | |
156 | { | |
157 | int retval = 0; | |
158 | struct posix_acl *acl, *clone; | |
159 | struct inode *inode = dentry->d_inode; | |
160 | ||
161 | if (S_ISLNK(inode->i_mode)) | |
162 | return -EOPNOTSUPP; | |
163 | acl = v9fs_get_cached_acl(inode, ACL_TYPE_ACCESS); | |
164 | if (acl) { | |
165 | clone = posix_acl_clone(acl, GFP_KERNEL); | |
166 | posix_acl_release(acl); | |
167 | if (!clone) | |
168 | return -ENOMEM; | |
169 | retval = posix_acl_chmod_masq(clone, inode->i_mode); | |
170 | if (!retval) | |
171 | retval = v9fs_set_acl(dentry, ACL_TYPE_ACCESS, clone); | |
172 | posix_acl_release(clone); | |
173 | } | |
174 | return retval; | |
175 | } | |
176 | ||
ad77dbce AK |
177 | int v9fs_set_create_acl(struct dentry *dentry, |
178 | struct posix_acl *dpacl, struct posix_acl *pacl) | |
179 | { | |
180 | if (dpacl) | |
181 | v9fs_set_acl(dentry, ACL_TYPE_DEFAULT, dpacl); | |
182 | if (pacl) | |
183 | v9fs_set_acl(dentry, ACL_TYPE_ACCESS, pacl); | |
184 | posix_acl_release(dpacl); | |
185 | posix_acl_release(pacl); | |
186 | return 0; | |
187 | } | |
188 | ||
189 | int v9fs_acl_mode(struct inode *dir, mode_t *modep, | |
190 | struct posix_acl **dpacl, struct posix_acl **pacl) | |
191 | { | |
192 | int retval = 0; | |
193 | mode_t mode = *modep; | |
194 | struct posix_acl *acl = NULL; | |
195 | ||
196 | if (!S_ISLNK(mode)) { | |
197 | acl = v9fs_get_cached_acl(dir, ACL_TYPE_DEFAULT); | |
198 | if (IS_ERR(acl)) | |
199 | return PTR_ERR(acl); | |
200 | if (!acl) | |
201 | mode &= ~current_umask(); | |
202 | } | |
203 | if (acl) { | |
204 | struct posix_acl *clone; | |
205 | ||
206 | if (S_ISDIR(mode)) | |
207 | *dpacl = acl; | |
208 | clone = posix_acl_clone(acl, GFP_NOFS); | |
209 | retval = -ENOMEM; | |
210 | if (!clone) | |
211 | goto cleanup; | |
212 | ||
213 | retval = posix_acl_create_masq(clone, &mode); | |
214 | if (retval < 0) { | |
215 | posix_acl_release(clone); | |
216 | goto cleanup; | |
217 | } | |
218 | if (retval > 0) | |
219 | *pacl = clone; | |
220 | } | |
221 | *modep = mode; | |
222 | return 0; | |
223 | cleanup: | |
224 | posix_acl_release(acl); | |
225 | return retval; | |
226 | ||
227 | } | |
228 | ||
76381a42 AK |
229 | static int v9fs_remote_get_acl(struct dentry *dentry, const char *name, |
230 | void *buffer, size_t size, int type) | |
231 | { | |
232 | char *full_name; | |
233 | ||
234 | switch (type) { | |
235 | case ACL_TYPE_ACCESS: | |
236 | full_name = POSIX_ACL_XATTR_ACCESS; | |
237 | break; | |
238 | case ACL_TYPE_DEFAULT: | |
239 | full_name = POSIX_ACL_XATTR_DEFAULT; | |
240 | break; | |
241 | default: | |
242 | BUG(); | |
243 | } | |
244 | return v9fs_xattr_get(dentry, full_name, buffer, size); | |
245 | } | |
246 | ||
7a4566b0 AK |
247 | static int v9fs_xattr_get_acl(struct dentry *dentry, const char *name, |
248 | void *buffer, size_t size, int type) | |
249 | { | |
76381a42 | 250 | struct v9fs_session_info *v9ses; |
7a4566b0 AK |
251 | struct posix_acl *acl; |
252 | int error; | |
253 | ||
254 | if (strcmp(name, "") != 0) | |
255 | return -EINVAL; | |
256 | ||
76381a42 AK |
257 | v9ses = v9fs_inode2v9ses(dentry->d_inode); |
258 | /* | |
259 | * We allow set/get/list of acl when access=client is not specified | |
260 | */ | |
261 | if ((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT) | |
262 | return v9fs_remote_get_acl(dentry, name, buffer, size, type); | |
263 | ||
7a4566b0 AK |
264 | acl = v9fs_get_cached_acl(dentry->d_inode, type); |
265 | if (IS_ERR(acl)) | |
266 | return PTR_ERR(acl); | |
267 | if (acl == NULL) | |
268 | return -ENODATA; | |
269 | error = posix_acl_to_xattr(acl, buffer, size); | |
270 | posix_acl_release(acl); | |
271 | ||
272 | return error; | |
273 | } | |
274 | ||
76381a42 AK |
275 | static int v9fs_remote_set_acl(struct dentry *dentry, const char *name, |
276 | const void *value, size_t size, | |
277 | int flags, int type) | |
278 | { | |
279 | char *full_name; | |
280 | ||
281 | switch (type) { | |
282 | case ACL_TYPE_ACCESS: | |
283 | full_name = POSIX_ACL_XATTR_ACCESS; | |
284 | break; | |
285 | case ACL_TYPE_DEFAULT: | |
286 | full_name = POSIX_ACL_XATTR_DEFAULT; | |
287 | break; | |
288 | default: | |
289 | BUG(); | |
290 | } | |
291 | return v9fs_xattr_set(dentry, full_name, value, size, flags); | |
292 | } | |
293 | ||
294 | ||
7a4566b0 AK |
295 | static int v9fs_xattr_set_acl(struct dentry *dentry, const char *name, |
296 | const void *value, size_t size, | |
297 | int flags, int type) | |
298 | { | |
22d8dcdf AK |
299 | int retval; |
300 | struct posix_acl *acl; | |
76381a42 | 301 | struct v9fs_session_info *v9ses; |
22d8dcdf AK |
302 | struct inode *inode = dentry->d_inode; |
303 | ||
304 | if (strcmp(name, "") != 0) | |
305 | return -EINVAL; | |
76381a42 AK |
306 | |
307 | v9ses = v9fs_inode2v9ses(dentry->d_inode); | |
308 | /* | |
309 | * set the attribute on the remote. Without even looking at the | |
310 | * xattr value. We leave it to the server to validate | |
311 | */ | |
312 | if ((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT) | |
313 | return v9fs_remote_set_acl(dentry, name, | |
314 | value, size, flags, type); | |
315 | ||
22d8dcdf AK |
316 | if (S_ISLNK(inode->i_mode)) |
317 | return -EOPNOTSUPP; | |
318 | if (!is_owner_or_cap(inode)) | |
319 | return -EPERM; | |
320 | if (value) { | |
321 | /* update the cached acl value */ | |
322 | acl = posix_acl_from_xattr(value, size); | |
323 | if (IS_ERR(acl)) | |
324 | return PTR_ERR(acl); | |
325 | else if (acl) { | |
326 | retval = posix_acl_valid(acl); | |
327 | if (retval) | |
328 | goto err_out; | |
329 | } | |
330 | } else | |
331 | acl = NULL; | |
332 | ||
333 | switch (type) { | |
334 | case ACL_TYPE_ACCESS: | |
335 | name = POSIX_ACL_XATTR_ACCESS; | |
336 | if (acl) { | |
337 | mode_t mode = inode->i_mode; | |
338 | retval = posix_acl_equiv_mode(acl, &mode); | |
339 | if (retval < 0) | |
340 | goto err_out; | |
341 | else { | |
342 | struct iattr iattr; | |
343 | if (retval == 0) { | |
344 | /* | |
345 | * ACL can be represented | |
346 | * by the mode bits. So don't | |
347 | * update ACL. | |
348 | */ | |
349 | acl = NULL; | |
350 | value = NULL; | |
351 | size = 0; | |
352 | } | |
353 | /* Updte the mode bits */ | |
354 | iattr.ia_mode = ((mode & S_IALLUGO) | | |
355 | (inode->i_mode & ~S_IALLUGO)); | |
356 | iattr.ia_valid = ATTR_MODE; | |
357 | /* FIXME should we update ctime ? | |
358 | * What is the following setxattr update the | |
359 | * mode ? | |
360 | */ | |
361 | v9fs_vfs_setattr_dotl(dentry, &iattr); | |
362 | } | |
363 | } | |
364 | break; | |
365 | case ACL_TYPE_DEFAULT: | |
366 | name = POSIX_ACL_XATTR_DEFAULT; | |
367 | if (!S_ISDIR(inode->i_mode)) { | |
6f81c115 | 368 | retval = acl ? -EINVAL : 0; |
22d8dcdf AK |
369 | goto err_out; |
370 | } | |
371 | break; | |
372 | default: | |
373 | BUG(); | |
374 | } | |
375 | retval = v9fs_xattr_set(dentry, name, value, size, flags); | |
376 | if (!retval) | |
377 | set_cached_acl(inode, type, acl); | |
378 | err_out: | |
379 | posix_acl_release(acl); | |
380 | return retval; | |
7a4566b0 AK |
381 | } |
382 | ||
383 | const struct xattr_handler v9fs_xattr_acl_access_handler = { | |
384 | .prefix = POSIX_ACL_XATTR_ACCESS, | |
385 | .flags = ACL_TYPE_ACCESS, | |
386 | .get = v9fs_xattr_get_acl, | |
387 | .set = v9fs_xattr_set_acl, | |
388 | }; | |
389 | ||
390 | const struct xattr_handler v9fs_xattr_acl_default_handler = { | |
391 | .prefix = POSIX_ACL_XATTR_DEFAULT, | |
392 | .flags = ACL_TYPE_DEFAULT, | |
393 | .get = v9fs_xattr_get_acl, | |
394 | .set = v9fs_xattr_set_acl, | |
395 | }; |