]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/9p/acl.c
generic_acl: no need to clone acl just to push it to set_cached_acl()
[mirror_ubuntu-bionic-kernel.git] / fs / 9p / acl.c
CommitLineData
85ff872d
AK
1/*
2 * Copyright IBM Corporation, 2010
3 * Author Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
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"
76381a42 24#include "v9fs.h"
5ffc0cb3 25#include "v9fs_vfs.h"
85ff872d
AK
26
27static 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
50err_out:
51 kfree(value);
52 return acl;
53}
54
55int 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 61 v9ses = v9fs_inode2v9ses(inode);
e782ef71
VJJ
62 if (((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT) ||
63 ((v9ses->flags & V9FS_ACL_MASK) != V9FS_POSIX_ACL)) {
76381a42
AK
64 set_cached_acl(inode, ACL_TYPE_DEFAULT, NULL);
65 set_cached_acl(inode, ACL_TYPE_ACCESS, NULL);
66 return 0;
67 }
85ff872d
AK
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);
71
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);
85ff872d
AK
75 } else
76 retval = -EIO;
77
c61fa0d6
VJJ
78 if (!IS_ERR(dacl))
79 posix_acl_release(dacl);
80
81 if (!IS_ERR(pacl))
82 posix_acl_release(pacl);
83
85ff872d
AK
84 return retval;
85}
86
87static struct posix_acl *v9fs_get_cached_acl(struct inode *inode, int type)
88{
89 struct posix_acl *acl;
90 /*
91 * 9p Always cache the acl value when
92 * instantiating the inode (v9fs_inode_from_fid)
93 */
94 acl = get_cached_acl(inode, type);
95 BUG_ON(acl == ACL_NOT_CACHED);
96 return acl;
97}
98
7e40145e 99int v9fs_check_acl(struct inode *inode, int mask)
85ff872d 100{
76381a42
AK
101 struct posix_acl *acl;
102 struct v9fs_session_info *v9ses;
103
104 v9ses = v9fs_inode2v9ses(inode);
e782ef71
VJJ
105 if (((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT) ||
106 ((v9ses->flags & V9FS_ACL_MASK) != V9FS_POSIX_ACL)) {
76381a42 107 /*
e782ef71 108 * On access = client and acl = on mode get the acl
76381a42
AK
109 * values from the server
110 */
ebbb0ef2 111 return -EAGAIN;
76381a42
AK
112 }
113 acl = v9fs_get_cached_acl(inode, ACL_TYPE_ACCESS);
85ff872d
AK
114
115 if (IS_ERR(acl))
116 return PTR_ERR(acl);
117 if (acl) {
118 int error = posix_acl_permission(inode, acl, mask);
119 posix_acl_release(acl);
120 return error;
121 }
122 return -EAGAIN;
123}
7a4566b0 124
6e8dc555
AK
125static int v9fs_set_acl(struct dentry *dentry, int type, struct posix_acl *acl)
126{
127 int retval;
128 char *name;
129 size_t size;
130 void *buffer;
131 struct inode *inode = dentry->d_inode;
132
133 set_cached_acl(inode, type, acl);
d344b0fb
VJJ
134
135 if (!acl)
136 return 0;
137
6e8dc555
AK
138 /* Set a setxattr request to server */
139 size = posix_acl_xattr_size(acl->a_count);
140 buffer = kmalloc(size, GFP_KERNEL);
141 if (!buffer)
142 return -ENOMEM;
143 retval = posix_acl_to_xattr(acl, buffer, size);
144 if (retval < 0)
145 goto err_free_out;
146 switch (type) {
147 case ACL_TYPE_ACCESS:
148 name = POSIX_ACL_XATTR_ACCESS;
149 break;
150 case ACL_TYPE_DEFAULT:
151 name = POSIX_ACL_XATTR_DEFAULT;
152 break;
153 default:
154 BUG();
155 }
156 retval = v9fs_xattr_set(dentry, name, buffer, size, 0);
157err_free_out:
158 kfree(buffer);
159 return retval;
160}
161
162int v9fs_acl_chmod(struct dentry *dentry)
163{
164 int retval = 0;
bc26ab5f 165 struct posix_acl *acl;
6e8dc555
AK
166 struct inode *inode = dentry->d_inode;
167
168 if (S_ISLNK(inode->i_mode))
169 return -EOPNOTSUPP;
170 acl = v9fs_get_cached_acl(inode, ACL_TYPE_ACCESS);
171 if (acl) {
bc26ab5f
AV
172 retval = posix_acl_chmod(&acl, GFP_KERNEL, inode->i_mode);
173 if (retval)
174 return retval;
175 retval = v9fs_set_acl(dentry, ACL_TYPE_ACCESS, acl);
6e8dc555 176 posix_acl_release(acl);
6e8dc555
AK
177 }
178 return retval;
179}
180
ad77dbce 181int v9fs_set_create_acl(struct dentry *dentry,
1ec95bf3 182 struct posix_acl **dpacl, struct posix_acl **pacl)
ad77dbce 183{
1ec95bf3
AV
184 if (dentry) {
185 v9fs_set_acl(dentry, ACL_TYPE_DEFAULT, *dpacl);
186 v9fs_set_acl(dentry, ACL_TYPE_ACCESS, *pacl);
187 }
188 posix_acl_release(*dpacl);
189 posix_acl_release(*pacl);
190 *dpacl = *pacl = NULL;
ad77dbce
AK
191 return 0;
192}
193
194int v9fs_acl_mode(struct inode *dir, mode_t *modep,
195 struct posix_acl **dpacl, struct posix_acl **pacl)
196{
197 int retval = 0;
198 mode_t mode = *modep;
199 struct posix_acl *acl = NULL;
200
201 if (!S_ISLNK(mode)) {
202 acl = v9fs_get_cached_acl(dir, ACL_TYPE_DEFAULT);
203 if (IS_ERR(acl))
204 return PTR_ERR(acl);
205 if (!acl)
206 mode &= ~current_umask();
207 }
208 if (acl) {
209 struct posix_acl *clone;
210
211 if (S_ISDIR(mode))
1ec95bf3 212 *dpacl = posix_acl_dup(acl);
ad77dbce 213 clone = posix_acl_clone(acl, GFP_NOFS);
1ec95bf3 214 posix_acl_release(acl);
ad77dbce 215 if (!clone)
1ec95bf3 216 return -ENOMEM;
ad77dbce
AK
217
218 retval = posix_acl_create_masq(clone, &mode);
219 if (retval < 0) {
220 posix_acl_release(clone);
221 goto cleanup;
222 }
223 if (retval > 0)
224 *pacl = clone;
1ec95bf3
AV
225 else
226 posix_acl_release(clone);
ad77dbce
AK
227 }
228 *modep = mode;
229 return 0;
230cleanup:
ad77dbce
AK
231 return retval;
232
233}
234
76381a42
AK
235static int v9fs_remote_get_acl(struct dentry *dentry, const char *name,
236 void *buffer, size_t size, int type)
237{
238 char *full_name;
239
240 switch (type) {
241 case ACL_TYPE_ACCESS:
242 full_name = POSIX_ACL_XATTR_ACCESS;
243 break;
244 case ACL_TYPE_DEFAULT:
245 full_name = POSIX_ACL_XATTR_DEFAULT;
246 break;
247 default:
248 BUG();
249 }
250 return v9fs_xattr_get(dentry, full_name, buffer, size);
251}
252
7a4566b0
AK
253static int v9fs_xattr_get_acl(struct dentry *dentry, const char *name,
254 void *buffer, size_t size, int type)
255{
76381a42 256 struct v9fs_session_info *v9ses;
7a4566b0
AK
257 struct posix_acl *acl;
258 int error;
259
260 if (strcmp(name, "") != 0)
261 return -EINVAL;
262
42869c8a 263 v9ses = v9fs_dentry2v9ses(dentry);
76381a42
AK
264 /*
265 * We allow set/get/list of acl when access=client is not specified
266 */
267 if ((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT)
268 return v9fs_remote_get_acl(dentry, name, buffer, size, type);
269
7a4566b0
AK
270 acl = v9fs_get_cached_acl(dentry->d_inode, type);
271 if (IS_ERR(acl))
272 return PTR_ERR(acl);
273 if (acl == NULL)
274 return -ENODATA;
275 error = posix_acl_to_xattr(acl, buffer, size);
276 posix_acl_release(acl);
277
278 return error;
279}
280
76381a42
AK
281static int v9fs_remote_set_acl(struct dentry *dentry, const char *name,
282 const void *value, size_t size,
283 int flags, int type)
284{
285 char *full_name;
286
287 switch (type) {
288 case ACL_TYPE_ACCESS:
289 full_name = POSIX_ACL_XATTR_ACCESS;
290 break;
291 case ACL_TYPE_DEFAULT:
292 full_name = POSIX_ACL_XATTR_DEFAULT;
293 break;
294 default:
295 BUG();
296 }
297 return v9fs_xattr_set(dentry, full_name, value, size, flags);
298}
299
300
7a4566b0
AK
301static int v9fs_xattr_set_acl(struct dentry *dentry, const char *name,
302 const void *value, size_t size,
303 int flags, int type)
304{
22d8dcdf
AK
305 int retval;
306 struct posix_acl *acl;
76381a42 307 struct v9fs_session_info *v9ses;
22d8dcdf
AK
308 struct inode *inode = dentry->d_inode;
309
310 if (strcmp(name, "") != 0)
311 return -EINVAL;
76381a42 312
42869c8a 313 v9ses = v9fs_dentry2v9ses(dentry);
76381a42
AK
314 /*
315 * set the attribute on the remote. Without even looking at the
316 * xattr value. We leave it to the server to validate
317 */
318 if ((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT)
319 return v9fs_remote_set_acl(dentry, name,
320 value, size, flags, type);
321
22d8dcdf
AK
322 if (S_ISLNK(inode->i_mode))
323 return -EOPNOTSUPP;
2e149670 324 if (!inode_owner_or_capable(inode))
22d8dcdf
AK
325 return -EPERM;
326 if (value) {
327 /* update the cached acl value */
328 acl = posix_acl_from_xattr(value, size);
329 if (IS_ERR(acl))
330 return PTR_ERR(acl);
331 else if (acl) {
332 retval = posix_acl_valid(acl);
333 if (retval)
334 goto err_out;
335 }
336 } else
337 acl = NULL;
338
339 switch (type) {
340 case ACL_TYPE_ACCESS:
341 name = POSIX_ACL_XATTR_ACCESS;
342 if (acl) {
343 mode_t mode = inode->i_mode;
344 retval = posix_acl_equiv_mode(acl, &mode);
345 if (retval < 0)
346 goto err_out;
347 else {
348 struct iattr iattr;
349 if (retval == 0) {
350 /*
351 * ACL can be represented
352 * by the mode bits. So don't
353 * update ACL.
354 */
355 acl = NULL;
356 value = NULL;
357 size = 0;
358 }
359 /* Updte the mode bits */
360 iattr.ia_mode = ((mode & S_IALLUGO) |
361 (inode->i_mode & ~S_IALLUGO));
362 iattr.ia_valid = ATTR_MODE;
363 /* FIXME should we update ctime ?
364 * What is the following setxattr update the
365 * mode ?
366 */
367 v9fs_vfs_setattr_dotl(dentry, &iattr);
368 }
369 }
370 break;
371 case ACL_TYPE_DEFAULT:
372 name = POSIX_ACL_XATTR_DEFAULT;
373 if (!S_ISDIR(inode->i_mode)) {
6f81c115 374 retval = acl ? -EINVAL : 0;
22d8dcdf
AK
375 goto err_out;
376 }
377 break;
378 default:
379 BUG();
380 }
381 retval = v9fs_xattr_set(dentry, name, value, size, flags);
382 if (!retval)
383 set_cached_acl(inode, type, acl);
384err_out:
385 posix_acl_release(acl);
386 return retval;
7a4566b0
AK
387}
388
389const struct xattr_handler v9fs_xattr_acl_access_handler = {
390 .prefix = POSIX_ACL_XATTR_ACCESS,
391 .flags = ACL_TYPE_ACCESS,
392 .get = v9fs_xattr_get_acl,
393 .set = v9fs_xattr_set_acl,
394};
395
396const struct xattr_handler v9fs_xattr_acl_default_handler = {
397 .prefix = POSIX_ACL_XATTR_DEFAULT,
398 .flags = ACL_TYPE_DEFAULT,
399 .get = v9fs_xattr_get_acl,
400 .set = v9fs_xattr_set_acl,
401};