fs: pass dentry to set acl method
[linux-block.git] / fs / posix_acl.c
CommitLineData
457c8996 1// SPDX-License-Identifier: GPL-2.0-only
1da177e4 2/*
5c8ebd57 3 * Copyright (C) 2002,2003 by Andreas Gruenbacher <a.gruenbacher@computer.org>
1da177e4 4 *
5c8ebd57
CH
5 * Fixes from William Schumacher incorporated on 15 March 2001.
6 * (Reported by Charles Bertsch, <CBertsch@microtest.com>).
1da177e4
LT
7 */
8
9/*
10 * This file contains generic functions for manipulating
11 * POSIX 1003.1e draft standard 17 ACLs.
12 */
13
14#include <linux/kernel.h>
15#include <linux/slab.h>
60063497 16#include <linux/atomic.h>
1da177e4
LT
17#include <linux/fs.h>
18#include <linux/sched.h>
5b825c3a 19#include <linux/cred.h>
1da177e4 20#include <linux/posix_acl.h>
5c8ebd57 21#include <linux/posix_acl_xattr.h>
2aeccbe9 22#include <linux/xattr.h>
630d9c47 23#include <linux/export.h>
5c8ebd57 24#include <linux/user_namespace.h>
332f606b 25#include <linux/namei.h>
a793d79e 26#include <linux/mnt_idmapping.h>
36f05cab 27#include <linux/iversion.h>
1da177e4 28
04c57f45 29static struct posix_acl **acl_by_type(struct inode *inode, int type)
0afaa120
AM
30{
31 switch (type) {
32 case ACL_TYPE_ACCESS:
33 return &inode->i_acl;
34 case ACL_TYPE_DEFAULT:
35 return &inode->i_default_acl;
36 default:
37 BUG();
38 }
39}
0afaa120
AM
40
41struct posix_acl *get_cached_acl(struct inode *inode, int type)
42{
43 struct posix_acl **p = acl_by_type(inode, type);
b8a7a3a6
AG
44 struct posix_acl *acl;
45
46 for (;;) {
47 rcu_read_lock();
48 acl = rcu_dereference(*p);
49 if (!acl || is_uncached_acl(acl) ||
66717260 50 refcount_inc_not_zero(&acl->a_refcount))
b8a7a3a6
AG
51 break;
52 rcu_read_unlock();
53 cpu_relax();
0afaa120 54 }
b8a7a3a6 55 rcu_read_unlock();
0afaa120
AM
56 return acl;
57}
58EXPORT_SYMBOL(get_cached_acl);
59
60struct posix_acl *get_cached_acl_rcu(struct inode *inode, int type)
61{
332f606b
MS
62 struct posix_acl *acl = rcu_dereference(*acl_by_type(inode, type));
63
64 if (acl == ACL_DONT_CACHE) {
65 struct posix_acl *ret;
66
67 ret = inode->i_op->get_acl(inode, type, LOOKUP_RCU);
68 if (!IS_ERR(ret))
69 acl = ret;
70 }
71
72 return acl;
0afaa120
AM
73}
74EXPORT_SYMBOL(get_cached_acl_rcu);
75
76void set_cached_acl(struct inode *inode, int type, struct posix_acl *acl)
77{
78 struct posix_acl **p = acl_by_type(inode, type);
79 struct posix_acl *old;
b8a7a3a6
AG
80
81 old = xchg(p, posix_acl_dup(acl));
82 if (!is_uncached_acl(old))
0afaa120
AM
83 posix_acl_release(old);
84}
85EXPORT_SYMBOL(set_cached_acl);
86
b8a7a3a6 87static void __forget_cached_acl(struct posix_acl **p)
0afaa120 88{
0afaa120 89 struct posix_acl *old;
b8a7a3a6
AG
90
91 old = xchg(p, ACL_NOT_CACHED);
92 if (!is_uncached_acl(old))
0afaa120
AM
93 posix_acl_release(old);
94}
b8a7a3a6
AG
95
96void forget_cached_acl(struct inode *inode, int type)
97{
98 __forget_cached_acl(acl_by_type(inode, type));
99}
0afaa120
AM
100EXPORT_SYMBOL(forget_cached_acl);
101
102void forget_all_cached_acls(struct inode *inode)
103{
b8a7a3a6
AG
104 __forget_cached_acl(&inode->i_acl);
105 __forget_cached_acl(&inode->i_default_acl);
0afaa120
AM
106}
107EXPORT_SYMBOL(forget_all_cached_acls);
1da177e4 108
2982baa2
CH
109struct posix_acl *get_acl(struct inode *inode, int type)
110{
b8a7a3a6
AG
111 void *sentinel;
112 struct posix_acl **p;
2982baa2
CH
113 struct posix_acl *acl;
114
b8a7a3a6
AG
115 /*
116 * The sentinel is used to detect when another operation like
117 * set_cached_acl() or forget_cached_acl() races with get_acl().
118 * It is guaranteed that is_uncached_acl(sentinel) is true.
119 */
120
2982baa2 121 acl = get_cached_acl(inode, type);
b8a7a3a6 122 if (!is_uncached_acl(acl))
2982baa2
CH
123 return acl;
124
125 if (!IS_POSIXACL(inode))
126 return NULL;
127
b8a7a3a6
AG
128 sentinel = uncached_acl_sentinel(current);
129 p = acl_by_type(inode, type);
130
131 /*
132 * If the ACL isn't being read yet, set our sentinel. Otherwise, the
133 * current value of the ACL will not be ACL_NOT_CACHED and so our own
134 * sentinel will not be set; another task will update the cache. We
135 * could wait for that other task to complete its job, but it's easier
136 * to just call ->get_acl to fetch the ACL ourself. (This is going to
137 * be an unlikely race.)
138 */
d1cef29a 139 cmpxchg(p, ACL_NOT_CACHED, sentinel);
b8a7a3a6 140
2982baa2 141 /*
b8a7a3a6
AG
142 * Normally, the ACL returned by ->get_acl will be cached.
143 * A filesystem can prevent that by calling
144 * forget_cached_acl(inode, type) in ->get_acl.
2982baa2
CH
145 *
146 * If the filesystem doesn't have a get_acl() function at all, we'll
147 * just create the negative cache entry.
148 */
149 if (!inode->i_op->get_acl) {
150 set_cached_acl(inode, type, NULL);
151 return NULL;
152 }
0cad6246 153 acl = inode->i_op->get_acl(inode, type, false);
b8a7a3a6
AG
154
155 if (IS_ERR(acl)) {
156 /*
157 * Remove our sentinel so that we don't block future attempts
158 * to cache the ACL.
159 */
160 cmpxchg(p, sentinel, ACL_NOT_CACHED);
161 return acl;
162 }
163
164 /*
165 * Cache the result, but only if our sentinel is still in place.
166 */
167 posix_acl_dup(acl);
168 if (unlikely(cmpxchg(p, sentinel, acl) != sentinel))
169 posix_acl_release(acl);
170 return acl;
2982baa2
CH
171}
172EXPORT_SYMBOL(get_acl);
173
f61f6da0
CL
174/*
175 * Init a fresh posix_acl
176 */
177void
178posix_acl_init(struct posix_acl *acl, int count)
179{
66717260 180 refcount_set(&acl->a_refcount, 1);
f61f6da0
CL
181 acl->a_count = count;
182}
0afaa120 183EXPORT_SYMBOL(posix_acl_init);
f61f6da0 184
1da177e4
LT
185/*
186 * Allocate a new ACL with the specified number of entries.
187 */
188struct posix_acl *
dd0fc66f 189posix_acl_alloc(int count, gfp_t flags)
1da177e4
LT
190{
191 const size_t size = sizeof(struct posix_acl) +
192 count * sizeof(struct posix_acl_entry);
193 struct posix_acl *acl = kmalloc(size, flags);
f61f6da0
CL
194 if (acl)
195 posix_acl_init(acl, count);
1da177e4
LT
196 return acl;
197}
0afaa120 198EXPORT_SYMBOL(posix_acl_alloc);
1da177e4
LT
199
200/*
201 * Clone an ACL.
202 */
8043bffd 203struct posix_acl *
dd0fc66f 204posix_acl_clone(const struct posix_acl *acl, gfp_t flags)
1da177e4
LT
205{
206 struct posix_acl *clone = NULL;
207
208 if (acl) {
209 int size = sizeof(struct posix_acl) + acl->a_count *
210 sizeof(struct posix_acl_entry);
52978be6
AD
211 clone = kmemdup(acl, size, flags);
212 if (clone)
66717260 213 refcount_set(&clone->a_refcount, 1);
1da177e4
LT
214 }
215 return clone;
216}
8043bffd 217EXPORT_SYMBOL_GPL(posix_acl_clone);
1da177e4
LT
218
219/*
220 * Check if an acl is valid. Returns 0 if it is, or -E... otherwise.
221 */
222int
0d4d717f 223posix_acl_valid(struct user_namespace *user_ns, const struct posix_acl *acl)
1da177e4
LT
224{
225 const struct posix_acl_entry *pa, *pe;
226 int state = ACL_USER_OBJ;
1da177e4
LT
227 int needs_mask = 0;
228
229 FOREACH_ACL_ENTRY(pa, acl, pe) {
230 if (pa->e_perm & ~(ACL_READ|ACL_WRITE|ACL_EXECUTE))
231 return -EINVAL;
232 switch (pa->e_tag) {
233 case ACL_USER_OBJ:
234 if (state == ACL_USER_OBJ) {
1da177e4
LT
235 state = ACL_USER;
236 break;
237 }
238 return -EINVAL;
239
240 case ACL_USER:
241 if (state != ACL_USER)
242 return -EINVAL;
0d4d717f 243 if (!kuid_has_mapping(user_ns, pa->e_uid))
1da177e4 244 return -EINVAL;
1da177e4
LT
245 needs_mask = 1;
246 break;
247
248 case ACL_GROUP_OBJ:
249 if (state == ACL_USER) {
1da177e4
LT
250 state = ACL_GROUP;
251 break;
252 }
253 return -EINVAL;
254
255 case ACL_GROUP:
256 if (state != ACL_GROUP)
257 return -EINVAL;
0d4d717f 258 if (!kgid_has_mapping(user_ns, pa->e_gid))
2f6f0654 259 return -EINVAL;
1da177e4
LT
260 needs_mask = 1;
261 break;
262
263 case ACL_MASK:
264 if (state != ACL_GROUP)
265 return -EINVAL;
266 state = ACL_OTHER;
267 break;
268
269 case ACL_OTHER:
270 if (state == ACL_OTHER ||
271 (state == ACL_GROUP && !needs_mask)) {
272 state = 0;
273 break;
274 }
275 return -EINVAL;
276
277 default:
278 return -EINVAL;
279 }
280 }
281 if (state == 0)
282 return 0;
283 return -EINVAL;
284}
0afaa120 285EXPORT_SYMBOL(posix_acl_valid);
1da177e4
LT
286
287/*
288 * Returns 0 if the acl can be exactly represented in the traditional
289 * file mode permission bits, or else 1. Returns -E... on error.
290 */
291int
d6952123 292posix_acl_equiv_mode(const struct posix_acl *acl, umode_t *mode_p)
1da177e4
LT
293{
294 const struct posix_acl_entry *pa, *pe;
d6952123 295 umode_t mode = 0;
1da177e4
LT
296 int not_equiv = 0;
297
50c6e282
CH
298 /*
299 * A null ACL can always be presented as mode bits.
300 */
301 if (!acl)
302 return 0;
303
1da177e4
LT
304 FOREACH_ACL_ENTRY(pa, acl, pe) {
305 switch (pa->e_tag) {
306 case ACL_USER_OBJ:
307 mode |= (pa->e_perm & S_IRWXO) << 6;
308 break;
309 case ACL_GROUP_OBJ:
310 mode |= (pa->e_perm & S_IRWXO) << 3;
311 break;
312 case ACL_OTHER:
313 mode |= pa->e_perm & S_IRWXO;
314 break;
315 case ACL_MASK:
316 mode = (mode & ~S_IRWXG) |
317 ((pa->e_perm & S_IRWXO) << 3);
318 not_equiv = 1;
319 break;
320 case ACL_USER:
321 case ACL_GROUP:
322 not_equiv = 1;
323 break;
324 default:
325 return -EINVAL;
326 }
327 }
328 if (mode_p)
329 *mode_p = (*mode_p & ~S_IRWXUGO) | mode;
330 return not_equiv;
331}
0afaa120 332EXPORT_SYMBOL(posix_acl_equiv_mode);
1da177e4
LT
333
334/*
335 * Create an ACL representing the file mode permission bits of an inode.
336 */
337struct posix_acl *
3a5fba19 338posix_acl_from_mode(umode_t mode, gfp_t flags)
1da177e4
LT
339{
340 struct posix_acl *acl = posix_acl_alloc(3, flags);
341 if (!acl)
342 return ERR_PTR(-ENOMEM);
343
344 acl->a_entries[0].e_tag = ACL_USER_OBJ;
1da177e4
LT
345 acl->a_entries[0].e_perm = (mode & S_IRWXU) >> 6;
346
347 acl->a_entries[1].e_tag = ACL_GROUP_OBJ;
1da177e4
LT
348 acl->a_entries[1].e_perm = (mode & S_IRWXG) >> 3;
349
350 acl->a_entries[2].e_tag = ACL_OTHER;
1da177e4
LT
351 acl->a_entries[2].e_perm = (mode & S_IRWXO);
352 return acl;
353}
0afaa120 354EXPORT_SYMBOL(posix_acl_from_mode);
1da177e4
LT
355
356/*
357 * Return 0 if current is granted want access to the inode
358 * by the acl. Returns -E... otherwise.
359 */
360int
47291baa
CB
361posix_acl_permission(struct user_namespace *mnt_userns, struct inode *inode,
362 const struct posix_acl *acl, int want)
1da177e4
LT
363{
364 const struct posix_acl_entry *pa, *pe, *mask_obj;
abfcf55d 365 struct user_namespace *fs_userns = i_user_ns(inode);
1da177e4 366 int found = 0;
e933c15f
CB
367 vfsuid_t vfsuid;
368 vfsgid_t vfsgid;
1da177e4 369
63d72b93 370 want &= MAY_READ | MAY_WRITE | MAY_EXEC;
d124b60a 371
1da177e4
LT
372 FOREACH_ACL_ENTRY(pa, acl, pe) {
373 switch(pa->e_tag) {
374 case ACL_USER_OBJ:
375 /* (May have been checked already) */
e933c15f
CB
376 vfsuid = i_uid_into_vfsuid(mnt_userns, inode);
377 if (vfsuid_eq_kuid(vfsuid, current_fsuid()))
1da177e4
LT
378 goto check_perm;
379 break;
380 case ACL_USER:
abfcf55d 381 vfsuid = make_vfsuid(mnt_userns, fs_userns,
bd303368 382 pa->e_uid);
e933c15f 383 if (vfsuid_eq_kuid(vfsuid, current_fsuid()))
1da177e4
LT
384 goto mask;
385 break;
386 case ACL_GROUP_OBJ:
e933c15f
CB
387 vfsgid = i_gid_into_vfsgid(mnt_userns, inode);
388 if (vfsgid_in_group_p(vfsgid)) {
1da177e4
LT
389 found = 1;
390 if ((pa->e_perm & want) == want)
391 goto mask;
392 }
393 break;
394 case ACL_GROUP:
abfcf55d 395 vfsgid = make_vfsgid(mnt_userns, fs_userns,
bd303368 396 pa->e_gid);
e933c15f 397 if (vfsgid_in_group_p(vfsgid)) {
1da177e4
LT
398 found = 1;
399 if ((pa->e_perm & want) == want)
400 goto mask;
401 }
402 break;
403 case ACL_MASK:
404 break;
405 case ACL_OTHER:
406 if (found)
407 return -EACCES;
408 else
409 goto check_perm;
410 default:
411 return -EIO;
412 }
413 }
414 return -EIO;
415
416mask:
417 for (mask_obj = pa+1; mask_obj != pe; mask_obj++) {
418 if (mask_obj->e_tag == ACL_MASK) {
419 if ((pa->e_perm & mask_obj->e_perm & want) == want)
420 return 0;
421 return -EACCES;
422 }
423 }
424
425check_perm:
426 if ((pa->e_perm & want) == want)
427 return 0;
428 return -EACCES;
429}
430
431/*
432 * Modify acl when creating a new inode. The caller must ensure the acl is
433 * only referenced once.
434 *
435 * mode_p initially must contain the mode parameter to the open() / creat()
436 * system calls. All permissions that are not granted by the acl are removed.
437 * The permissions in the acl are changed to reflect the mode_p parameter.
438 */
d3fb6120 439static int posix_acl_create_masq(struct posix_acl *acl, umode_t *mode_p)
1da177e4
LT
440{
441 struct posix_acl_entry *pa, *pe;
442 struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
d3fb6120 443 umode_t mode = *mode_p;
1da177e4
LT
444 int not_equiv = 0;
445
446 /* assert(atomic_read(acl->a_refcount) == 1); */
447
448 FOREACH_ACL_ENTRY(pa, acl, pe) {
449 switch(pa->e_tag) {
450 case ACL_USER_OBJ:
451 pa->e_perm &= (mode >> 6) | ~S_IRWXO;
452 mode &= (pa->e_perm << 6) | ~S_IRWXU;
453 break;
454
455 case ACL_USER:
456 case ACL_GROUP:
457 not_equiv = 1;
458 break;
459
460 case ACL_GROUP_OBJ:
461 group_obj = pa;
462 break;
463
464 case ACL_OTHER:
465 pa->e_perm &= mode | ~S_IRWXO;
466 mode &= pa->e_perm | ~S_IRWXO;
467 break;
468
469 case ACL_MASK:
470 mask_obj = pa;
471 not_equiv = 1;
472 break;
473
474 default:
475 return -EIO;
476 }
477 }
478
479 if (mask_obj) {
480 mask_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
481 mode &= (mask_obj->e_perm << 3) | ~S_IRWXG;
482 } else {
483 if (!group_obj)
484 return -EIO;
485 group_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
486 mode &= (group_obj->e_perm << 3) | ~S_IRWXG;
487 }
488
489 *mode_p = (*mode_p & ~S_IRWXUGO) | mode;
490 return not_equiv;
491}
492
493/*
494 * Modify the ACL for the chmod syscall.
495 */
5bf3258f 496static int __posix_acl_chmod_masq(struct posix_acl *acl, umode_t mode)
1da177e4
LT
497{
498 struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
499 struct posix_acl_entry *pa, *pe;
500
501 /* assert(atomic_read(acl->a_refcount) == 1); */
502
503 FOREACH_ACL_ENTRY(pa, acl, pe) {
504 switch(pa->e_tag) {
505 case ACL_USER_OBJ:
506 pa->e_perm = (mode & S_IRWXU) >> 6;
507 break;
508
509 case ACL_USER:
510 case ACL_GROUP:
511 break;
512
513 case ACL_GROUP_OBJ:
514 group_obj = pa;
515 break;
516
517 case ACL_MASK:
518 mask_obj = pa;
519 break;
520
521 case ACL_OTHER:
522 pa->e_perm = (mode & S_IRWXO);
523 break;
524
525 default:
526 return -EIO;
527 }
528 }
529
530 if (mask_obj) {
531 mask_obj->e_perm = (mode & S_IRWXG) >> 3;
532 } else {
533 if (!group_obj)
534 return -EIO;
535 group_obj->e_perm = (mode & S_IRWXG) >> 3;
536 }
537
538 return 0;
539}
bc26ab5f 540
826cae2f 541int
37bc1539 542__posix_acl_create(struct posix_acl **acl, gfp_t gfp, umode_t *mode_p)
826cae2f
AV
543{
544 struct posix_acl *clone = posix_acl_clone(*acl, gfp);
545 int err = -ENOMEM;
546 if (clone) {
547 err = posix_acl_create_masq(clone, mode_p);
548 if (err < 0) {
549 posix_acl_release(clone);
550 clone = NULL;
551 }
552 }
553 posix_acl_release(*acl);
554 *acl = clone;
555 return err;
556}
37bc1539 557EXPORT_SYMBOL(__posix_acl_create);
826cae2f 558
bc26ab5f 559int
5bf3258f 560__posix_acl_chmod(struct posix_acl **acl, gfp_t gfp, umode_t mode)
bc26ab5f
AV
561{
562 struct posix_acl *clone = posix_acl_clone(*acl, gfp);
563 int err = -ENOMEM;
564 if (clone) {
5bf3258f 565 err = __posix_acl_chmod_masq(clone, mode);
bc26ab5f
AV
566 if (err) {
567 posix_acl_release(clone);
568 clone = NULL;
569 }
570 }
571 posix_acl_release(*acl);
572 *acl = clone;
573 return err;
574}
5bf3258f
CH
575EXPORT_SYMBOL(__posix_acl_chmod);
576
e65ce2a5
CB
577/**
578 * posix_acl_chmod - chmod a posix acl
579 *
580 * @mnt_userns: user namespace of the mount @inode was found from
138060ba 581 * @dentry: dentry to check permissions on
e65ce2a5
CB
582 * @mode: the new mode of @inode
583 *
138060ba 584 * If the dentry has been found through an idmapped mount the user namespace of
e65ce2a5
CB
585 * the vfsmount must be passed through @mnt_userns. This function will then
586 * take care to map the inode according to @mnt_userns before checking
587 * permissions. On non-idmapped mounts or if permission checking is to be
588 * performed on the raw inode simply passs init_user_ns.
589 */
5bf3258f 590int
138060ba 591 posix_acl_chmod(struct user_namespace *mnt_userns, struct dentry *dentry,
e65ce2a5 592 umode_t mode)
5bf3258f 593{
138060ba 594 struct inode *inode = d_inode(dentry);
5bf3258f
CH
595 struct posix_acl *acl;
596 int ret = 0;
597
598 if (!IS_POSIXACL(inode))
599 return 0;
600 if (!inode->i_op->set_acl)
601 return -EOPNOTSUPP;
602
603 acl = get_acl(inode, ACL_TYPE_ACCESS);
789b663a
TM
604 if (IS_ERR_OR_NULL(acl)) {
605 if (acl == ERR_PTR(-EOPNOTSUPP))
606 return 0;
5bf3258f 607 return PTR_ERR(acl);
789b663a 608 }
5bf3258f 609
37bc1539 610 ret = __posix_acl_chmod(&acl, GFP_KERNEL, mode);
5bf3258f
CH
611 if (ret)
612 return ret;
138060ba 613 ret = inode->i_op->set_acl(mnt_userns, dentry, acl, ACL_TYPE_ACCESS);
5bf3258f
CH
614 posix_acl_release(acl);
615 return ret;
616}
bc26ab5f 617EXPORT_SYMBOL(posix_acl_chmod);
5c8ebd57 618
37bc1539
CH
619int
620posix_acl_create(struct inode *dir, umode_t *mode,
621 struct posix_acl **default_acl, struct posix_acl **acl)
622{
623 struct posix_acl *p;
c0c3a718 624 struct posix_acl *clone;
37bc1539
CH
625 int ret;
626
c0c3a718
DC
627 *acl = NULL;
628 *default_acl = NULL;
629
37bc1539 630 if (S_ISLNK(*mode) || !IS_POSIXACL(dir))
c0c3a718 631 return 0;
37bc1539
CH
632
633 p = get_acl(dir, ACL_TYPE_DEFAULT);
c0c3a718
DC
634 if (!p || p == ERR_PTR(-EOPNOTSUPP)) {
635 *mode &= ~current_umask();
636 return 0;
37bc1539 637 }
c0c3a718
DC
638 if (IS_ERR(p))
639 return PTR_ERR(p);
37bc1539 640
beaf226b 641 ret = -ENOMEM;
c0c3a718
DC
642 clone = posix_acl_clone(p, GFP_NOFS);
643 if (!clone)
beaf226b 644 goto err_release;
37bc1539 645
c0c3a718 646 ret = posix_acl_create_masq(clone, mode);
fed0b588 647 if (ret < 0)
beaf226b 648 goto err_release_clone;
37bc1539 649
c0c3a718
DC
650 if (ret == 0)
651 posix_acl_release(clone);
652 else
653 *acl = clone;
37bc1539 654
c0c3a718 655 if (!S_ISDIR(*mode))
37bc1539 656 posix_acl_release(p);
c0c3a718 657 else
37bc1539 658 *default_acl = p;
37bc1539 659
37bc1539 660 return 0;
fed0b588 661
beaf226b 662err_release_clone:
c0c3a718 663 posix_acl_release(clone);
beaf226b 664err_release:
fed0b588 665 posix_acl_release(p);
beaf226b 666 return ret;
37bc1539
CH
667}
668EXPORT_SYMBOL_GPL(posix_acl_create);
669
07393101
JK
670/**
671 * posix_acl_update_mode - update mode in set_acl
e65ce2a5
CB
672 * @mnt_userns: user namespace of the mount @inode was found from
673 * @inode: target inode
674 * @mode_p: mode (pointer) for update
675 * @acl: acl pointer
07393101
JK
676 *
677 * Update the file mode when setting an ACL: compute the new file permission
678 * bits based on the ACL. In addition, if the ACL is equivalent to the new
e39e773a 679 * file mode, set *@acl to NULL to indicate that no ACL should be set.
07393101 680 *
e39e773a 681 * As with chmod, clear the setgid bit if the caller is not in the owning group
07393101
JK
682 * or capable of CAP_FSETID (see inode_change_ok).
683 *
e65ce2a5
CB
684 * If the inode has been found through an idmapped mount the user namespace of
685 * the vfsmount must be passed through @mnt_userns. This function will then
686 * take care to map the inode according to @mnt_userns before checking
687 * permissions. On non-idmapped mounts or if permission checking is to be
688 * performed on the raw inode simply passs init_user_ns.
689 *
07393101
JK
690 * Called from set_acl inode operations.
691 */
e65ce2a5
CB
692int posix_acl_update_mode(struct user_namespace *mnt_userns,
693 struct inode *inode, umode_t *mode_p,
07393101
JK
694 struct posix_acl **acl)
695{
696 umode_t mode = inode->i_mode;
697 int error;
698
699 error = posix_acl_equiv_mode(*acl, &mode);
700 if (error < 0)
701 return error;
702 if (error == 0)
703 *acl = NULL;
e933c15f 704 if (!vfsgid_in_group_p(i_gid_into_vfsgid(mnt_userns, inode)) &&
e65ce2a5 705 !capable_wrt_inode_uidgid(mnt_userns, inode, CAP_FSETID))
07393101
JK
706 mode &= ~S_ISGID;
707 *mode_p = mode;
708 return 0;
709}
710EXPORT_SYMBOL(posix_acl_update_mode);
711
5c8ebd57
CH
712/*
713 * Fix up the uids and gids in posix acl extended attributes in place.
714 */
985a6d0b 715static int posix_acl_fix_xattr_common(const void *value, size_t size)
0c5fd887 716{
985a6d0b 717 const struct posix_acl_xattr_header *header = value;
0c5fd887
CB
718 int count;
719
720 if (!header)
721 return -EINVAL;
722 if (size < sizeof(struct posix_acl_xattr_header))
723 return -EINVAL;
724 if (header->a_version != cpu_to_le32(POSIX_ACL_XATTR_VERSION))
985a6d0b 725 return -EOPNOTSUPP;
0c5fd887
CB
726
727 count = posix_acl_xattr_count(size);
728 if (count < 0)
729 return -EINVAL;
730 if (count == 0)
985a6d0b 731 return 0;
0c5fd887
CB
732
733 return count;
734}
735
736void posix_acl_getxattr_idmapped_mnt(struct user_namespace *mnt_userns,
737 const struct inode *inode,
738 void *value, size_t size)
5c8ebd57 739{
2211d5ba
AG
740 struct posix_acl_xattr_header *header = value;
741 struct posix_acl_xattr_entry *entry = (void *)(header + 1), *end;
abfcf55d 742 struct user_namespace *fs_userns = i_user_ns(inode);
5c8ebd57 743 int count;
0c5fd887
CB
744 vfsuid_t vfsuid;
745 vfsgid_t vfsgid;
5c8ebd57
CH
746 kuid_t uid;
747 kgid_t gid;
748
0c5fd887 749 if (no_idmapping(mnt_userns, i_user_ns(inode)))
5c8ebd57 750 return;
0c5fd887
CB
751
752 count = posix_acl_fix_xattr_common(value, size);
985a6d0b 753 if (count <= 0)
5c8ebd57 754 return;
0c5fd887
CB
755
756 for (end = entry + count; entry != end; entry++) {
757 switch (le16_to_cpu(entry->e_tag)) {
758 case ACL_USER:
759 uid = make_kuid(&init_user_ns, le32_to_cpu(entry->e_id));
abfcf55d 760 vfsuid = make_vfsuid(mnt_userns, fs_userns, uid);
0c5fd887
CB
761 entry->e_id = cpu_to_le32(from_kuid(&init_user_ns,
762 vfsuid_into_kuid(vfsuid)));
763 break;
764 case ACL_GROUP:
765 gid = make_kgid(&init_user_ns, le32_to_cpu(entry->e_id));
abfcf55d 766 vfsgid = make_vfsgid(mnt_userns, fs_userns, gid);
0c5fd887
CB
767 entry->e_id = cpu_to_le32(from_kgid(&init_user_ns,
768 vfsgid_into_kgid(vfsgid)));
769 break;
770 default:
771 break;
772 }
773 }
774}
775
0c5fd887
CB
776static void posix_acl_fix_xattr_userns(
777 struct user_namespace *to, struct user_namespace *from,
778 void *value, size_t size)
779{
780 struct posix_acl_xattr_header *header = value;
781 struct posix_acl_xattr_entry *entry = (void *)(header + 1), *end;
782 int count;
783 kuid_t uid;
784 kgid_t gid;
785
786 count = posix_acl_fix_xattr_common(value, size);
985a6d0b 787 if (count <= 0)
5c8ebd57
CH
788 return;
789
790 for (end = entry + count; entry != end; entry++) {
791 switch(le16_to_cpu(entry->e_tag)) {
792 case ACL_USER:
793 uid = make_kuid(from, le32_to_cpu(entry->e_id));
794 entry->e_id = cpu_to_le32(from_kuid(to, uid));
795 break;
796 case ACL_GROUP:
797 gid = make_kgid(from, le32_to_cpu(entry->e_id));
798 entry->e_id = cpu_to_le32(from_kgid(to, gid));
799 break;
800 default:
801 break;
802 }
803 }
804}
805
0c5fd887 806void posix_acl_fix_xattr_from_user(void *value, size_t size)
5c8ebd57
CH
807{
808 struct user_namespace *user_ns = current_user_ns();
0c5fd887 809 if (user_ns == &init_user_ns)
5c8ebd57 810 return;
0c5fd887 811 posix_acl_fix_xattr_userns(&init_user_ns, user_ns, value, size);
5c8ebd57
CH
812}
813
0c5fd887 814void posix_acl_fix_xattr_to_user(void *value, size_t size)
5c8ebd57
CH
815{
816 struct user_namespace *user_ns = current_user_ns();
0c5fd887 817 if (user_ns == &init_user_ns)
5c8ebd57 818 return;
0c5fd887 819 posix_acl_fix_xattr_userns(user_ns, &init_user_ns, value, size);
5c8ebd57
CH
820}
821
6b70fe06
CB
822/**
823 * make_posix_acl - convert POSIX ACLs from uapi to VFS format using the
824 * provided callbacks to map ACL_{GROUP,USER} entries into the
825 * appropriate format
826 * @mnt_userns: the mount's idmapping
827 * @fs_userns: the filesystem's idmapping
828 * @value: the uapi representation of POSIX ACLs
829 * @size: the size of @void
830 * @uid_cb: callback to use for mapping the uid stored in ACL_USER entries
831 * @gid_cb: callback to use for mapping the gid stored in ACL_GROUP entries
832 *
833 * The make_posix_acl() helper is an abstraction to translate from uapi format
834 * into the VFS format allowing the caller to specific callbacks to map
835 * ACL_{GROUP,USER} entries into the expected format. This is used in
836 * posix_acl_from_xattr() and vfs_set_acl_prepare() and avoids pointless code
837 * duplication.
838 *
839 * Return: Allocated struct posix_acl on success, NULL for a valid header but
840 * without actual POSIX ACL entries, or ERR_PTR() encoded error code.
5c8ebd57 841 */
6b70fe06
CB
842static struct posix_acl *make_posix_acl(struct user_namespace *mnt_userns,
843 struct user_namespace *fs_userns, const void *value, size_t size,
844 kuid_t (*uid_cb)(struct user_namespace *, struct user_namespace *,
845 const struct posix_acl_xattr_entry *),
846 kgid_t (*gid_cb)(struct user_namespace *, struct user_namespace *,
847 const struct posix_acl_xattr_entry *))
5c8ebd57 848{
2211d5ba
AG
849 const struct posix_acl_xattr_header *header = value;
850 const struct posix_acl_xattr_entry *entry = (const void *)(header + 1), *end;
5c8ebd57
CH
851 int count;
852 struct posix_acl *acl;
853 struct posix_acl_entry *acl_e;
854
985a6d0b 855 count = posix_acl_fix_xattr_common(value, size);
5c8ebd57 856 if (count < 0)
985a6d0b 857 return ERR_PTR(count);
5c8ebd57
CH
858 if (count == 0)
859 return NULL;
860
861 acl = posix_acl_alloc(count, GFP_NOFS);
862 if (!acl)
863 return ERR_PTR(-ENOMEM);
864 acl_e = acl->a_entries;
865
866 for (end = entry + count; entry != end; acl_e++, entry++) {
867 acl_e->e_tag = le16_to_cpu(entry->e_tag);
868 acl_e->e_perm = le16_to_cpu(entry->e_perm);
869
870 switch(acl_e->e_tag) {
871 case ACL_USER_OBJ:
872 case ACL_GROUP_OBJ:
873 case ACL_MASK:
874 case ACL_OTHER:
875 break;
876
877 case ACL_USER:
6b70fe06 878 acl_e->e_uid = uid_cb(mnt_userns, fs_userns, entry);
5c8ebd57
CH
879 if (!uid_valid(acl_e->e_uid))
880 goto fail;
881 break;
882 case ACL_GROUP:
6b70fe06 883 acl_e->e_gid = gid_cb(mnt_userns, fs_userns, entry);
5c8ebd57
CH
884 if (!gid_valid(acl_e->e_gid))
885 goto fail;
886 break;
887
888 default:
889 goto fail;
890 }
891 }
892 return acl;
893
894fail:
895 posix_acl_release(acl);
896 return ERR_PTR(-EINVAL);
897}
6b70fe06
CB
898
899/**
900 * vfs_set_acl_prepare_kuid - map ACL_USER uid according to mount- and
901 * filesystem idmapping
902 * @mnt_userns: the mount's idmapping
903 * @fs_userns: the filesystem's idmapping
904 * @e: a ACL_USER entry in POSIX ACL uapi format
905 *
906 * The uid stored as ACL_USER entry in @e is a kuid_t stored as a raw {g,u}id
907 * value. The vfs_set_acl_prepare_kuid() will recover the kuid_t through
908 * KUIDT_INIT() and then map it according to the idmapped mount. The resulting
909 * kuid_t is the value which the filesystem can map up into a raw backing store
910 * id in the filesystem's idmapping.
911 *
912 * This is used in vfs_set_acl_prepare() to generate the proper VFS
913 * representation of POSIX ACLs with ACL_USER entries during setxattr().
914 *
915 * Return: A kuid in @fs_userns for the uid stored in @e.
916 */
917static inline kuid_t
918vfs_set_acl_prepare_kuid(struct user_namespace *mnt_userns,
919 struct user_namespace *fs_userns,
920 const struct posix_acl_xattr_entry *e)
921{
922 kuid_t kuid = KUIDT_INIT(le32_to_cpu(e->e_id));
923 return from_vfsuid(mnt_userns, fs_userns, VFSUIDT_INIT(kuid));
924}
925
926/**
927 * vfs_set_acl_prepare_kgid - map ACL_GROUP gid according to mount- and
928 * filesystem idmapping
929 * @mnt_userns: the mount's idmapping
930 * @fs_userns: the filesystem's idmapping
931 * @e: a ACL_GROUP entry in POSIX ACL uapi format
932 *
933 * The gid stored as ACL_GROUP entry in @e is a kgid_t stored as a raw {g,u}id
934 * value. The vfs_set_acl_prepare_kgid() will recover the kgid_t through
935 * KGIDT_INIT() and then map it according to the idmapped mount. The resulting
936 * kgid_t is the value which the filesystem can map up into a raw backing store
937 * id in the filesystem's idmapping.
938 *
939 * This is used in vfs_set_acl_prepare() to generate the proper VFS
940 * representation of POSIX ACLs with ACL_GROUP entries during setxattr().
941 *
942 * Return: A kgid in @fs_userns for the gid stored in @e.
943 */
944static inline kgid_t
945vfs_set_acl_prepare_kgid(struct user_namespace *mnt_userns,
946 struct user_namespace *fs_userns,
947 const struct posix_acl_xattr_entry *e)
948{
949 kgid_t kgid = KGIDT_INIT(le32_to_cpu(e->e_id));
950 return from_vfsgid(mnt_userns, fs_userns, VFSGIDT_INIT(kgid));
951}
952
953/**
954 * vfs_set_acl_prepare - convert POSIX ACLs from uapi to VFS format taking
955 * mount and filesystem idmappings into account
956 * @mnt_userns: the mount's idmapping
957 * @fs_userns: the filesystem's idmapping
958 * @value: the uapi representation of POSIX ACLs
959 * @size: the size of @void
960 *
961 * When setting POSIX ACLs with ACL_{GROUP,USER} entries they need to be
962 * mapped according to the relevant mount- and filesystem idmapping. It is
963 * important that the ACL_{GROUP,USER} entries in struct posix_acl will be
964 * mapped into k{g,u}id_t that are supposed to be mapped up in the filesystem
965 * idmapping. This is crucial since the resulting struct posix_acl might be
966 * cached filesystem wide. The vfs_set_acl_prepare() function will take care to
967 * perform all necessary idmappings.
968 *
969 * Note, that since basically forever the {g,u}id values encoded as
970 * ACL_{GROUP,USER} entries in the uapi POSIX ACLs passed via @value contain
971 * values that have been mapped according to the caller's idmapping. In other
972 * words, POSIX ACLs passed in uapi format as @value during setxattr() contain
973 * {g,u}id values in their ACL_{GROUP,USER} entries that should actually have
974 * been stored as k{g,u}id_t.
975 *
976 * This means, vfs_set_acl_prepare() needs to first recover the k{g,u}id_t by
977 * calling K{G,U}IDT_INIT(). Afterwards they can be interpreted as vfs{g,u}id_t
978 * through from_vfs{g,u}id() to account for any idmapped mounts. The
979 * vfs_set_acl_prepare_k{g,u}id() helpers will take care to generate the
980 * correct k{g,u}id_t.
981 *
982 * The filesystem will then receive the POSIX ACLs ready to be cached
983 * filesystem wide and ready to be written to the backing store taking the
984 * filesystem's idmapping into account.
985 *
986 * Return: Allocated struct posix_acl on success, NULL for a valid header but
987 * without actual POSIX ACL entries, or ERR_PTR() encoded error code.
988 */
989struct posix_acl *vfs_set_acl_prepare(struct user_namespace *mnt_userns,
990 struct user_namespace *fs_userns,
991 const void *value, size_t size)
992{
993 return make_posix_acl(mnt_userns, fs_userns, value, size,
994 vfs_set_acl_prepare_kuid,
995 vfs_set_acl_prepare_kgid);
996}
997EXPORT_SYMBOL(vfs_set_acl_prepare);
998
999/**
1000 * posix_acl_from_xattr_kuid - map ACL_USER uid into filesystem idmapping
1001 * @mnt_userns: unused
1002 * @fs_userns: the filesystem's idmapping
1003 * @e: a ACL_USER entry in POSIX ACL uapi format
1004 *
1005 * Map the uid stored as ACL_USER entry in @e into the filesystem's idmapping.
1006 * This is used in posix_acl_from_xattr() to generate the proper VFS
1007 * representation of POSIX ACLs with ACL_USER entries.
1008 *
1009 * Return: A kuid in @fs_userns for the uid stored in @e.
1010 */
1011static inline kuid_t
1012posix_acl_from_xattr_kuid(struct user_namespace *mnt_userns,
1013 struct user_namespace *fs_userns,
1014 const struct posix_acl_xattr_entry *e)
1015{
1016 return make_kuid(fs_userns, le32_to_cpu(e->e_id));
1017}
1018
1019/**
1020 * posix_acl_from_xattr_kgid - map ACL_GROUP gid into filesystem idmapping
1021 * @mnt_userns: unused
1022 * @fs_userns: the filesystem's idmapping
1023 * @e: a ACL_GROUP entry in POSIX ACL uapi format
1024 *
1025 * Map the gid stored as ACL_GROUP entry in @e into the filesystem's idmapping.
1026 * This is used in posix_acl_from_xattr() to generate the proper VFS
1027 * representation of POSIX ACLs with ACL_GROUP entries.
1028 *
1029 * Return: A kgid in @fs_userns for the gid stored in @e.
1030 */
1031static inline kgid_t
1032posix_acl_from_xattr_kgid(struct user_namespace *mnt_userns,
1033 struct user_namespace *fs_userns,
1034 const struct posix_acl_xattr_entry *e)
1035{
1036 return make_kgid(fs_userns, le32_to_cpu(e->e_id));
1037}
1038
1039/**
1040 * posix_acl_from_xattr - convert POSIX ACLs from backing store to VFS format
1041 * @fs_userns: the filesystem's idmapping
1042 * @value: the uapi representation of POSIX ACLs
1043 * @size: the size of @void
1044 *
1045 * Filesystems that store POSIX ACLs in the unaltered uapi format should use
1046 * posix_acl_from_xattr() when reading them from the backing store and
1047 * converting them into the struct posix_acl VFS format. The helper is
1048 * specifically intended to be called from the ->get_acl() inode operation.
1049 *
1050 * The posix_acl_from_xattr() function will map the raw {g,u}id values stored
1051 * in ACL_{GROUP,USER} entries into the filesystem idmapping in @fs_userns. The
1052 * posix_acl_from_xattr_k{g,u}id() helpers will take care to generate the
1053 * correct k{g,u}id_t. The returned struct posix_acl can be cached.
1054 *
1055 * Note that posix_acl_from_xattr() does not take idmapped mounts into account.
1056 * If it did it calling is from the ->get_acl() inode operation would return
1057 * POSIX ACLs mapped according to an idmapped mount which would mean that the
1058 * value couldn't be cached for the filesystem. Idmapped mounts are taken into
1059 * account on the fly during permission checking or right at the VFS -
1060 * userspace boundary before reporting them to the user.
1061 *
1062 * Return: Allocated struct posix_acl on success, NULL for a valid header but
1063 * without actual POSIX ACL entries, or ERR_PTR() encoded error code.
1064 */
1065struct posix_acl *
1066posix_acl_from_xattr(struct user_namespace *fs_userns,
1067 const void *value, size_t size)
1068{
1069 return make_posix_acl(&init_user_ns, fs_userns, value, size,
1070 posix_acl_from_xattr_kuid,
1071 posix_acl_from_xattr_kgid);
1072}
5c8ebd57
CH
1073EXPORT_SYMBOL (posix_acl_from_xattr);
1074
1075/*
1076 * Convert from in-memory to extended attribute representation.
1077 */
1078int
1079posix_acl_to_xattr(struct user_namespace *user_ns, const struct posix_acl *acl,
1080 void *buffer, size_t size)
1081{
2211d5ba
AG
1082 struct posix_acl_xattr_header *ext_acl = buffer;
1083 struct posix_acl_xattr_entry *ext_entry;
5c8ebd57
CH
1084 int real_size, n;
1085
1086 real_size = posix_acl_xattr_size(acl->a_count);
1087 if (!buffer)
1088 return real_size;
1089 if (real_size > size)
1090 return -ERANGE;
47ba9734 1091
2211d5ba 1092 ext_entry = (void *)(ext_acl + 1);
5c8ebd57
CH
1093 ext_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
1094
1095 for (n=0; n < acl->a_count; n++, ext_entry++) {
1096 const struct posix_acl_entry *acl_e = &acl->a_entries[n];
1097 ext_entry->e_tag = cpu_to_le16(acl_e->e_tag);
1098 ext_entry->e_perm = cpu_to_le16(acl_e->e_perm);
1099 switch(acl_e->e_tag) {
1100 case ACL_USER:
1101 ext_entry->e_id =
1102 cpu_to_le32(from_kuid(user_ns, acl_e->e_uid));
1103 break;
1104 case ACL_GROUP:
1105 ext_entry->e_id =
1106 cpu_to_le32(from_kgid(user_ns, acl_e->e_gid));
1107 break;
1108 default:
1109 ext_entry->e_id = cpu_to_le32(ACL_UNDEFINED_ID);
1110 break;
1111 }
1112 }
1113 return real_size;
1114}
1115EXPORT_SYMBOL (posix_acl_to_xattr);
2aeccbe9
CH
1116
1117static int
d9a82a04 1118posix_acl_xattr_get(const struct xattr_handler *handler,
b296821a
AV
1119 struct dentry *unused, struct inode *inode,
1120 const char *name, void *value, size_t size)
2aeccbe9
CH
1121{
1122 struct posix_acl *acl;
1123 int error;
1124
b296821a 1125 if (!IS_POSIXACL(inode))
2aeccbe9 1126 return -EOPNOTSUPP;
b296821a 1127 if (S_ISLNK(inode->i_mode))
2aeccbe9
CH
1128 return -EOPNOTSUPP;
1129
b296821a 1130 acl = get_acl(inode, handler->flags);
2aeccbe9
CH
1131 if (IS_ERR(acl))
1132 return PTR_ERR(acl);
1133 if (acl == NULL)
1134 return -ENODATA;
1135
1136 error = posix_acl_to_xattr(&init_user_ns, acl, value, size);
1137 posix_acl_release(acl);
1138
1139 return error;
1140}
1141
485e71e8 1142int
138060ba 1143set_posix_acl(struct user_namespace *mnt_userns, struct dentry *dentry,
e65ce2a5 1144 int type, struct posix_acl *acl)
2aeccbe9 1145{
138060ba
CB
1146 struct inode *inode = d_inode(dentry);
1147
2aeccbe9
CH
1148 if (!IS_POSIXACL(inode))
1149 return -EOPNOTSUPP;
1150 if (!inode->i_op->set_acl)
1151 return -EOPNOTSUPP;
1152
485e71e8
AG
1153 if (type == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode))
1154 return acl ? -EACCES : 0;
e65ce2a5 1155 if (!inode_owner_or_capable(mnt_userns, inode))
2aeccbe9
CH
1156 return -EPERM;
1157
485e71e8 1158 if (acl) {
a867d734 1159 int ret = posix_acl_valid(inode->i_sb->s_user_ns, acl);
485e71e8
AG
1160 if (ret)
1161 return ret;
1162 }
138060ba 1163 return inode->i_op->set_acl(mnt_userns, dentry, acl, type);
485e71e8
AG
1164}
1165EXPORT_SYMBOL(set_posix_acl);
1166
1167static int
1168posix_acl_xattr_set(const struct xattr_handler *handler,
e65ce2a5 1169 struct user_namespace *mnt_userns,
138060ba 1170 struct dentry *dentry, struct inode *inode,
e65ce2a5
CB
1171 const char *name, const void *value, size_t size,
1172 int flags)
485e71e8
AG
1173{
1174 struct posix_acl *acl = NULL;
1175 int ret;
1176
2aeccbe9 1177 if (value) {
52edb408
CB
1178 /*
1179 * By the time we end up here the {g,u}ids stored in
1180 * ACL_{GROUP,USER} have already been mapped according to the
1181 * caller's idmapping. The vfs_set_acl_prepare() helper will
1182 * recover them and take idmapped mounts into account. The
0978c7c4 1183 * filesystem will receive the POSIX ACLs in the correct
52edb408
CB
1184 * format ready to be cached or written to the backing store
1185 * taking the filesystem idmapping into account.
1186 */
1187 acl = vfs_set_acl_prepare(mnt_userns, i_user_ns(inode),
1188 value, size);
2aeccbe9
CH
1189 if (IS_ERR(acl))
1190 return PTR_ERR(acl);
2aeccbe9 1191 }
138060ba 1192 ret = set_posix_acl(mnt_userns, dentry, handler->flags, acl);
2aeccbe9
CH
1193 posix_acl_release(acl);
1194 return ret;
1195}
1196
764a5c6b
AG
1197static bool
1198posix_acl_xattr_list(struct dentry *dentry)
2aeccbe9 1199{
764a5c6b 1200 return IS_POSIXACL(d_backing_inode(dentry));
2aeccbe9
CH
1201}
1202
1203const struct xattr_handler posix_acl_access_xattr_handler = {
98e9cb57 1204 .name = XATTR_NAME_POSIX_ACL_ACCESS,
2aeccbe9
CH
1205 .flags = ACL_TYPE_ACCESS,
1206 .list = posix_acl_xattr_list,
1207 .get = posix_acl_xattr_get,
1208 .set = posix_acl_xattr_set,
1209};
1210EXPORT_SYMBOL_GPL(posix_acl_access_xattr_handler);
1211
1212const struct xattr_handler posix_acl_default_xattr_handler = {
98e9cb57 1213 .name = XATTR_NAME_POSIX_ACL_DEFAULT,
2aeccbe9
CH
1214 .flags = ACL_TYPE_DEFAULT,
1215 .list = posix_acl_xattr_list,
1216 .get = posix_acl_xattr_get,
1217 .set = posix_acl_xattr_set,
1218};
1219EXPORT_SYMBOL_GPL(posix_acl_default_xattr_handler);
feda821e 1220
138060ba 1221int simple_set_acl(struct user_namespace *mnt_userns, struct dentry *dentry,
549c7297 1222 struct posix_acl *acl, int type)
feda821e
CH
1223{
1224 int error;
138060ba 1225 struct inode *inode = d_inode(dentry);
feda821e
CH
1226
1227 if (type == ACL_TYPE_ACCESS) {
549c7297 1228 error = posix_acl_update_mode(mnt_userns, inode,
497de07d
GZ
1229 &inode->i_mode, &acl);
1230 if (error)
1231 return error;
feda821e
CH
1232 }
1233
078cd827 1234 inode->i_ctime = current_time(inode);
36f05cab
JL
1235 if (IS_I_VERSION(inode))
1236 inode_inc_iversion(inode);
feda821e
CH
1237 set_cached_acl(inode, type, acl);
1238 return 0;
1239}
1240
1241int simple_acl_create(struct inode *dir, struct inode *inode)
1242{
1243 struct posix_acl *default_acl, *acl;
1244 int error;
1245
1246 error = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
1247 if (error)
1248 return error;
1249
1250 set_cached_acl(inode, ACL_TYPE_DEFAULT, default_acl);
1251 set_cached_acl(inode, ACL_TYPE_ACCESS, acl);
1252
1253 if (default_acl)
1254 posix_acl_release(default_acl);
1255 if (acl)
1256 posix_acl_release(acl);
1257 return 0;
1258}