ARM: 7409/1: Do not call flush_cache_user_range with mmap_sem held
[linux/fpc-iii.git] / fs / 9p / acl.c
blob4a866cd06287b240e4c9d7b79c183e0822fc83a1
1 /*
2 * Copyright IBM Corporation, 2010
3 * Author Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
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.
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.
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>
20 #include <linux/sched.h>
21 #include <linux/posix_acl_xattr.h>
22 #include "xattr.h"
23 #include "acl.h"
24 #include "v9fs.h"
25 #include "v9fs_vfs.h"
27 static struct posix_acl *__v9fs_get_acl(struct p9_fid *fid, char *name)
29 ssize_t size;
30 void *value = NULL;
31 struct posix_acl *acl = NULL;
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;
44 } else if (size == -ENODATA || size == 0 ||
45 size == -ENOSYS || size == -EOPNOTSUPP) {
46 acl = NULL;
47 } else
48 acl = ERR_PTR(-EIO);
50 err_out:
51 kfree(value);
52 return acl;
55 int v9fs_get_acl(struct inode *inode, struct p9_fid *fid)
57 int retval = 0;
58 struct posix_acl *pacl, *dacl;
59 struct v9fs_session_info *v9ses;
61 v9ses = v9fs_inode2v9ses(inode);
62 if (((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT) ||
63 ((v9ses->flags & V9FS_ACL_MASK) != V9FS_POSIX_ACL)) {
64 set_cached_acl(inode, ACL_TYPE_DEFAULT, NULL);
65 set_cached_acl(inode, ACL_TYPE_ACCESS, NULL);
66 return 0;
68 /* get the default/access acl values and cache them */
69 dacl = __v9fs_get_acl(fid, POSIX_ACL_XATTR_DEFAULT);
70 pacl = __v9fs_get_acl(fid, POSIX_ACL_XATTR_ACCESS);
72 if (!IS_ERR(dacl) && !IS_ERR(pacl)) {
73 set_cached_acl(inode, ACL_TYPE_DEFAULT, dacl);
74 set_cached_acl(inode, ACL_TYPE_ACCESS, pacl);
75 } else
76 retval = -EIO;
78 if (!IS_ERR(dacl))
79 posix_acl_release(dacl);
81 if (!IS_ERR(pacl))
82 posix_acl_release(pacl);
84 return retval;
87 static struct posix_acl *v9fs_get_cached_acl(struct inode *inode, int type)
89 struct posix_acl *acl;
91 * 9p Always cache the acl value when
92 * instantiating the inode (v9fs_inode_from_fid)
94 acl = get_cached_acl(inode, type);
95 BUG_ON(acl == ACL_NOT_CACHED);
96 return acl;
99 int v9fs_check_acl(struct inode *inode, int mask, unsigned int flags)
101 struct posix_acl *acl;
102 struct v9fs_session_info *v9ses;
104 if (flags & IPERM_FLAG_RCU)
105 return -ECHILD;
107 v9ses = v9fs_inode2v9ses(inode);
108 if (((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT) ||
109 ((v9ses->flags & V9FS_ACL_MASK) != V9FS_POSIX_ACL)) {
111 * On access = client and acl = on mode get the acl
112 * values from the server
114 return 0;
116 acl = v9fs_get_cached_acl(inode, ACL_TYPE_ACCESS);
118 if (IS_ERR(acl))
119 return PTR_ERR(acl);
120 if (acl) {
121 int error = posix_acl_permission(inode, acl, mask);
122 posix_acl_release(acl);
123 return error;
125 return -EAGAIN;
128 static int v9fs_set_acl(struct dentry *dentry, int type, struct posix_acl *acl)
130 int retval;
131 char *name;
132 size_t size;
133 void *buffer;
134 struct inode *inode = dentry->d_inode;
136 set_cached_acl(inode, type, acl);
138 if (!acl)
139 return 0;
141 /* Set a setxattr request to server */
142 size = posix_acl_xattr_size(acl->a_count);
143 buffer = kmalloc(size, GFP_KERNEL);
144 if (!buffer)
145 return -ENOMEM;
146 retval = posix_acl_to_xattr(acl, buffer, size);
147 if (retval < 0)
148 goto err_free_out;
149 switch (type) {
150 case ACL_TYPE_ACCESS:
151 name = POSIX_ACL_XATTR_ACCESS;
152 break;
153 case ACL_TYPE_DEFAULT:
154 name = POSIX_ACL_XATTR_DEFAULT;
155 break;
156 default:
157 BUG();
159 retval = v9fs_xattr_set(dentry, name, buffer, size, 0);
160 err_free_out:
161 kfree(buffer);
162 return retval;
165 int v9fs_acl_chmod(struct dentry *dentry)
167 int retval = 0;
168 struct posix_acl *acl, *clone;
169 struct inode *inode = dentry->d_inode;
171 if (S_ISLNK(inode->i_mode))
172 return -EOPNOTSUPP;
173 acl = v9fs_get_cached_acl(inode, ACL_TYPE_ACCESS);
174 if (acl) {
175 clone = posix_acl_clone(acl, GFP_KERNEL);
176 posix_acl_release(acl);
177 if (!clone)
178 return -ENOMEM;
179 retval = posix_acl_chmod_masq(clone, inode->i_mode);
180 if (!retval)
181 retval = v9fs_set_acl(dentry, ACL_TYPE_ACCESS, clone);
182 posix_acl_release(clone);
184 return retval;
187 int v9fs_set_create_acl(struct dentry *dentry,
188 struct posix_acl **dpacl, struct posix_acl **pacl)
190 if (dentry) {
191 v9fs_set_acl(dentry, ACL_TYPE_DEFAULT, *dpacl);
192 v9fs_set_acl(dentry, ACL_TYPE_ACCESS, *pacl);
194 posix_acl_release(*dpacl);
195 posix_acl_release(*pacl);
196 *dpacl = *pacl = NULL;
197 return 0;
200 int v9fs_acl_mode(struct inode *dir, mode_t *modep,
201 struct posix_acl **dpacl, struct posix_acl **pacl)
203 int retval = 0;
204 mode_t mode = *modep;
205 struct posix_acl *acl = NULL;
207 if (!S_ISLNK(mode)) {
208 acl = v9fs_get_cached_acl(dir, ACL_TYPE_DEFAULT);
209 if (IS_ERR(acl))
210 return PTR_ERR(acl);
211 if (!acl)
212 mode &= ~current_umask();
214 if (acl) {
215 struct posix_acl *clone;
217 if (S_ISDIR(mode))
218 *dpacl = posix_acl_dup(acl);
219 clone = posix_acl_clone(acl, GFP_NOFS);
220 posix_acl_release(acl);
221 if (!clone)
222 return -ENOMEM;
224 retval = posix_acl_create_masq(clone, &mode);
225 if (retval < 0) {
226 posix_acl_release(clone);
227 goto cleanup;
229 if (retval > 0)
230 *pacl = clone;
231 else
232 posix_acl_release(clone);
234 *modep = mode;
235 return 0;
236 cleanup:
237 return retval;
241 static int v9fs_remote_get_acl(struct dentry *dentry, const char *name,
242 void *buffer, size_t size, int type)
244 char *full_name;
246 switch (type) {
247 case ACL_TYPE_ACCESS:
248 full_name = POSIX_ACL_XATTR_ACCESS;
249 break;
250 case ACL_TYPE_DEFAULT:
251 full_name = POSIX_ACL_XATTR_DEFAULT;
252 break;
253 default:
254 BUG();
256 return v9fs_xattr_get(dentry, full_name, buffer, size);
259 static int v9fs_xattr_get_acl(struct dentry *dentry, const char *name,
260 void *buffer, size_t size, int type)
262 struct v9fs_session_info *v9ses;
263 struct posix_acl *acl;
264 int error;
266 if (strcmp(name, "") != 0)
267 return -EINVAL;
269 v9ses = v9fs_dentry2v9ses(dentry);
271 * We allow set/get/list of acl when access=client is not specified
273 if ((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT)
274 return v9fs_remote_get_acl(dentry, name, buffer, size, type);
276 acl = v9fs_get_cached_acl(dentry->d_inode, type);
277 if (IS_ERR(acl))
278 return PTR_ERR(acl);
279 if (acl == NULL)
280 return -ENODATA;
281 error = posix_acl_to_xattr(acl, buffer, size);
282 posix_acl_release(acl);
284 return error;
287 static int v9fs_remote_set_acl(struct dentry *dentry, const char *name,
288 const void *value, size_t size,
289 int flags, int type)
291 char *full_name;
293 switch (type) {
294 case ACL_TYPE_ACCESS:
295 full_name = POSIX_ACL_XATTR_ACCESS;
296 break;
297 case ACL_TYPE_DEFAULT:
298 full_name = POSIX_ACL_XATTR_DEFAULT;
299 break;
300 default:
301 BUG();
303 return v9fs_xattr_set(dentry, full_name, value, size, flags);
307 static int v9fs_xattr_set_acl(struct dentry *dentry, const char *name,
308 const void *value, size_t size,
309 int flags, int type)
311 int retval;
312 struct posix_acl *acl;
313 struct v9fs_session_info *v9ses;
314 struct inode *inode = dentry->d_inode;
316 if (strcmp(name, "") != 0)
317 return -EINVAL;
319 v9ses = v9fs_dentry2v9ses(dentry);
321 * set the attribute on the remote. Without even looking at the
322 * xattr value. We leave it to the server to validate
324 if ((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT)
325 return v9fs_remote_set_acl(dentry, name,
326 value, size, flags, type);
328 if (S_ISLNK(inode->i_mode))
329 return -EOPNOTSUPP;
330 if (!inode_owner_or_capable(inode))
331 return -EPERM;
332 if (value) {
333 /* update the cached acl value */
334 acl = posix_acl_from_xattr(value, size);
335 if (IS_ERR(acl))
336 return PTR_ERR(acl);
337 else if (acl) {
338 retval = posix_acl_valid(acl);
339 if (retval)
340 goto err_out;
342 } else
343 acl = NULL;
345 switch (type) {
346 case ACL_TYPE_ACCESS:
347 name = POSIX_ACL_XATTR_ACCESS;
348 if (acl) {
349 mode_t mode = inode->i_mode;
350 retval = posix_acl_equiv_mode(acl, &mode);
351 if (retval < 0)
352 goto err_out;
353 else {
354 struct iattr iattr;
355 if (retval == 0) {
357 * ACL can be represented
358 * by the mode bits. So don't
359 * update ACL.
361 acl = NULL;
362 value = NULL;
363 size = 0;
365 /* Updte the mode bits */
366 iattr.ia_mode = ((mode & S_IALLUGO) |
367 (inode->i_mode & ~S_IALLUGO));
368 iattr.ia_valid = ATTR_MODE;
369 /* FIXME should we update ctime ?
370 * What is the following setxattr update the
371 * mode ?
373 v9fs_vfs_setattr_dotl(dentry, &iattr);
376 break;
377 case ACL_TYPE_DEFAULT:
378 name = POSIX_ACL_XATTR_DEFAULT;
379 if (!S_ISDIR(inode->i_mode)) {
380 retval = acl ? -EINVAL : 0;
381 goto err_out;
383 break;
384 default:
385 BUG();
387 retval = v9fs_xattr_set(dentry, name, value, size, flags);
388 if (!retval)
389 set_cached_acl(inode, type, acl);
390 err_out:
391 posix_acl_release(acl);
392 return retval;
395 const struct xattr_handler v9fs_xattr_acl_access_handler = {
396 .prefix = POSIX_ACL_XATTR_ACCESS,
397 .flags = ACL_TYPE_ACCESS,
398 .get = v9fs_xattr_get_acl,
399 .set = v9fs_xattr_set_acl,
402 const struct xattr_handler v9fs_xattr_acl_default_handler = {
403 .prefix = POSIX_ACL_XATTR_DEFAULT,
404 .flags = ACL_TYPE_DEFAULT,
405 .get = v9fs_xattr_get_acl,
406 .set = v9fs_xattr_set_acl,