Merge with Linux 2.5.74.
[linux-2.6/linux-mips.git] / fs / xfs / xfs_acl.c
blob950b50b9b6cf49939062fafd1bf5dccc5fc335ce
1 /*
2 * Copyright (c) 2001-2002 Silicon Graphics, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
26 * http://www.sgi.com
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
33 #include "xfs.h"
35 #include "xfs_inum.h"
36 #include "xfs_dir.h"
37 #include "xfs_dir2.h"
38 #include "xfs_alloc_btree.h"
39 #include "xfs_bmap_btree.h"
40 #include "xfs_ialloc_btree.h"
41 #include "xfs_btree.h"
42 #include "xfs_attr_sf.h"
43 #include "xfs_dir_sf.h"
44 #include "xfs_dir2_sf.h"
45 #include "xfs_dinode.h"
46 #include "xfs_inode.h"
47 #include "xfs_acl.h"
48 #include "xfs_mac.h"
49 #include "xfs_attr.h"
51 #include <linux/posix_acl_xattr.h>
53 STATIC int xfs_acl_setmode(vnode_t *, xfs_acl_t *, int *);
54 STATIC void xfs_acl_filter_mode(mode_t, xfs_acl_t *);
55 STATIC void xfs_acl_get_endian(xfs_acl_t *);
56 STATIC int xfs_acl_access(uid_t, gid_t, xfs_acl_t *, mode_t, cred_t *);
57 STATIC int xfs_acl_invalid(xfs_acl_t *);
58 STATIC void xfs_acl_sync_mode(mode_t, xfs_acl_t *);
59 STATIC void xfs_acl_get_attr(vnode_t *, xfs_acl_t *, int, int, int *);
60 STATIC void xfs_acl_set_attr(vnode_t *, xfs_acl_t *, int, int *);
61 STATIC int xfs_acl_allow_set(vnode_t *, int);
63 kmem_zone_t *xfs_acl_zone;
67 * Test for existence of access ACL attribute as efficiently as possible.
69 int
70 xfs_acl_vhasacl_access(
71 vnode_t *vp)
73 int error;
75 xfs_acl_get_attr(vp, NULL, _ACL_TYPE_ACCESS, ATTR_KERNOVAL, &error);
76 return (error == 0);
80 * Test for existence of default ACL attribute as efficiently as possible.
82 int
83 xfs_acl_vhasacl_default(
84 vnode_t *vp)
86 int error;
88 if (vp->v_type != VDIR)
89 return 0;
90 xfs_acl_get_attr(vp, NULL, _ACL_TYPE_DEFAULT, ATTR_KERNOVAL, &error);
91 return (error == 0);
95 * Convert from extended attribute representation to in-memory for XFS.
97 STATIC int
98 posix_acl_xattr_to_xfs(
99 posix_acl_xattr_header *src,
100 size_t size,
101 xfs_acl_t *dest)
103 posix_acl_xattr_entry *src_entry;
104 xfs_acl_entry_t *dest_entry;
105 int n;
107 if (!src || !dest)
108 return EINVAL;
110 if (size < sizeof(posix_acl_xattr_header))
111 return EINVAL;
113 if (src->a_version != cpu_to_le32(POSIX_ACL_XATTR_VERSION))
114 return EINVAL;
116 memset(dest, 0, sizeof(xfs_acl_t));
117 dest->acl_cnt = posix_acl_xattr_count(size);
118 if (dest->acl_cnt < 0 || dest->acl_cnt > XFS_ACL_MAX_ENTRIES)
119 return EINVAL;
122 * acl_set_file(3) may request that we set default ACLs with
123 * zero length -- defend (gracefully) against that here.
125 if (!dest->acl_cnt)
126 return 0;
128 src_entry = (posix_acl_xattr_entry *)((char *)src + sizeof(*src));
129 dest_entry = &dest->acl_entry[0];
131 for (n = 0; n < dest->acl_cnt; n++, src_entry++, dest_entry++) {
132 dest_entry->ae_perm = le16_to_cpu(src_entry->e_perm);
133 if (_ACL_PERM_INVALID(dest_entry->ae_perm))
134 return EINVAL;
135 dest_entry->ae_tag = le16_to_cpu(src_entry->e_tag);
136 switch(dest_entry->ae_tag) {
137 case ACL_USER:
138 case ACL_GROUP:
139 dest_entry->ae_id = le32_to_cpu(src_entry->e_id);
140 break;
141 case ACL_USER_OBJ:
142 case ACL_GROUP_OBJ:
143 case ACL_MASK:
144 case ACL_OTHER:
145 dest_entry->ae_id = ACL_UNDEFINED_ID;
146 break;
147 default:
148 return EINVAL;
151 if (xfs_acl_invalid(dest))
152 return EINVAL;
154 return 0;
158 * Comparison function called from qsort().
159 * Primary key is ae_tag, secondary key is ae_id.
161 STATIC int
162 xfs_acl_entry_compare(
163 const void *va,
164 const void *vb)
166 xfs_acl_entry_t *a = (xfs_acl_entry_t *)va,
167 *b = (xfs_acl_entry_t *)vb;
169 if (a->ae_tag == b->ae_tag)
170 return (a->ae_id - b->ae_id);
171 return (a->ae_tag - b->ae_tag);
175 * Convert from in-memory XFS to extended attribute representation.
177 STATIC int
178 posix_acl_xfs_to_xattr(
179 xfs_acl_t *src,
180 posix_acl_xattr_header *dest,
181 size_t size)
183 int n;
184 size_t new_size = posix_acl_xattr_size(src->acl_cnt);
185 posix_acl_xattr_entry *dest_entry;
186 xfs_acl_entry_t *src_entry;
188 if (size < new_size)
189 return -ERANGE;
191 /* Need to sort src XFS ACL by <ae_tag,ae_id> */
192 qsort(src->acl_entry, src->acl_cnt, sizeof(src->acl_entry[0]),
193 xfs_acl_entry_compare);
195 dest->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
196 dest_entry = &dest->a_entries[0];
197 src_entry = &src->acl_entry[0];
198 for (n = 0; n < src->acl_cnt; n++, dest_entry++, src_entry++) {
199 dest_entry->e_perm = cpu_to_le16(src_entry->ae_perm);
200 if (_ACL_PERM_INVALID(src_entry->ae_perm))
201 return -EINVAL;
202 dest_entry->e_tag = cpu_to_le16(src_entry->ae_tag);
203 switch (src_entry->ae_tag) {
204 case ACL_USER:
205 case ACL_GROUP:
206 dest_entry->e_id = cpu_to_le32(src_entry->ae_id);
207 break;
208 case ACL_USER_OBJ:
209 case ACL_GROUP_OBJ:
210 case ACL_MASK:
211 case ACL_OTHER:
212 dest_entry->e_id = cpu_to_le32(ACL_UNDEFINED_ID);
213 break;
214 default:
215 return -EINVAL;
218 return new_size;
222 xfs_acl_vget(
223 vnode_t *vp,
224 void *acl,
225 size_t size,
226 int kind)
228 int error;
229 xfs_acl_t *xfs_acl = NULL;
230 posix_acl_xattr_header *ext_acl = acl;
231 int flags = 0;
233 VN_HOLD(vp);
234 if ((error = _MAC_VACCESS(vp, NULL, VREAD)))
235 goto out;
236 if(size) {
237 if (!(_ACL_ALLOC(xfs_acl))) {
238 error = ENOMEM;
239 goto out;
241 memset(xfs_acl, 0, sizeof(xfs_acl_t));
242 } else
243 flags = ATTR_KERNOVAL;
245 xfs_acl_get_attr(vp, xfs_acl, kind, flags, &error);
246 if (error)
247 goto out;
249 if (!size) {
250 error = -posix_acl_xattr_size(XFS_ACL_MAX_ENTRIES);
251 } else {
252 if (xfs_acl_invalid(xfs_acl)) {
253 error = EINVAL;
254 goto out;
256 if (kind == _ACL_TYPE_ACCESS) {
257 vattr_t va;
259 va.va_mask = XFS_AT_MODE;
260 VOP_GETATTR(vp, &va, 0, sys_cred, error);
261 if (error)
262 goto out;
263 xfs_acl_sync_mode(va.va_mode, xfs_acl);
265 error = -posix_acl_xfs_to_xattr(xfs_acl, ext_acl, size);
267 out:
268 VN_RELE(vp);
269 if(xfs_acl)
270 _ACL_FREE(xfs_acl);
271 return -error;
275 xfs_acl_vremove(
276 vnode_t *vp,
277 int kind)
279 int error;
281 VN_HOLD(vp);
282 error = xfs_acl_allow_set(vp, kind);
283 if (!error) {
284 VOP_ATTR_REMOVE(vp, kind == _ACL_TYPE_DEFAULT?
285 SGI_ACL_DEFAULT: SGI_ACL_FILE,
286 ATTR_ROOT, sys_cred, error);
287 if (error == ENOATTR)
288 error = 0; /* 'scool */
290 VN_RELE(vp);
291 return -error;
295 xfs_acl_vset(
296 vnode_t *vp,
297 void *acl,
298 size_t size,
299 int kind)
301 posix_acl_xattr_header *ext_acl = acl;
302 xfs_acl_t *xfs_acl;
303 int error;
304 int basicperms = 0; /* more than std unix perms? */
306 if (!acl)
307 return -EINVAL;
309 if (!(_ACL_ALLOC(xfs_acl)))
310 return -ENOMEM;
312 error = posix_acl_xattr_to_xfs(ext_acl, size, xfs_acl);
313 if (error) {
314 _ACL_FREE(xfs_acl);
315 return -error;
317 if (!xfs_acl->acl_cnt) {
318 _ACL_FREE(xfs_acl);
319 return 0;
322 VN_HOLD(vp);
323 error = xfs_acl_allow_set(vp, kind);
324 if (error)
325 goto out;
327 /* Incoming ACL exists, set file mode based on its value */
328 if (kind == _ACL_TYPE_ACCESS)
329 xfs_acl_setmode(vp, xfs_acl, &basicperms);
332 * If we have more than std unix permissions, set up the actual attr.
333 * Otherwise, delete any existing attr. This prevents us from
334 * having actual attrs for permissions that can be stored in the
335 * standard permission bits.
337 if (!basicperms) {
338 xfs_acl_set_attr(vp, xfs_acl, kind, &error);
339 } else {
340 xfs_acl_vremove(vp, _ACL_TYPE_ACCESS);
344 out:
345 VN_RELE(vp);
346 _ACL_FREE(xfs_acl);
347 return -error;
351 xfs_acl_iaccess(
352 xfs_inode_t *ip,
353 mode_t mode,
354 cred_t *cr)
356 xfs_acl_t *acl;
357 int error;
359 if (!(_ACL_ALLOC(acl)))
360 return -1;
362 /* If the file has no ACL return -1. */
363 if (xfs_attr_fetch(ip, SGI_ACL_FILE, (char *)acl, sizeof(xfs_acl_t))) {
364 _ACL_FREE(acl);
365 return -1;
367 xfs_acl_get_endian(acl);
369 /* If the file has an empty ACL return -1. */
370 if (acl->acl_cnt == XFS_ACL_NOT_PRESENT) {
371 _ACL_FREE(acl);
372 return -1;
375 /* Synchronize ACL with mode bits */
376 xfs_acl_sync_mode(ip->i_d.di_mode, acl);
378 error = xfs_acl_access(ip->i_d.di_uid, ip->i_d.di_gid, acl, mode, cr);
379 _ACL_FREE(acl);
380 return error;
383 STATIC int
384 xfs_acl_allow_set(
385 vnode_t *vp,
386 int kind)
388 vattr_t va;
389 int error;
391 if (kind == _ACL_TYPE_DEFAULT && vp->v_type != VDIR)
392 return ENOTDIR;
393 if (vp->v_vfsp->vfs_flag & VFS_RDONLY)
394 return EROFS;
395 if ((error = _MAC_VACCESS(vp, NULL, VWRITE)))
396 return error;
397 va.va_mask = XFS_AT_UID;
398 VOP_GETATTR(vp, &va, 0, NULL, error);
399 if (error)
400 return error;
401 if (va.va_uid != current->fsuid && !capable(CAP_FOWNER))
402 return EPERM;
403 return error;
407 * Look for any effective exec access, to allow CAP_DAC_OVERRIDE for exec.
408 * Ignore checking for exec in USER_OBJ when there is no mask, because
409 * in this "minimal acl" case we don't have any actual acls, and we
410 * won't even be here.
412 STATIC int
413 xfs_acl_find_any_exec(
414 xfs_acl_t *fap)
416 int i;
417 int masked_aces = 0;
418 int mask = 0;
420 for (i = 0; i < fap->acl_cnt; i++) {
421 if (fap->acl_entry[i].ae_perm & ACL_EXECUTE) {
422 if (fap->acl_entry[i].ae_tag & (ACL_USER_OBJ|ACL_OTHER))
423 return 1;
425 if (fap->acl_entry[i].ae_tag == ACL_MASK)
426 mask = fap->acl_entry[i].ae_perm;
427 else
428 masked_aces |= fap->acl_entry[i].ae_perm;
430 if ((mask & masked_aces) & ACL_EXECUTE)
431 return 1;
435 return 0;
439 * The access control process to determine the access permission:
440 * if uid == file owner id, use the file owner bits.
441 * if gid == file owner group id, use the file group bits.
442 * scan ACL for a maching user or group, and use matched entry
443 * permission. Use total permissions of all matching group entries,
444 * until all acl entries are exhausted. The final permission produced
445 * by matching acl entry or entries needs to be & with group permission.
446 * if not owner, owning group, or matching entry in ACL, use file
447 * other bits. Don't allow CAP_DAC_OVERRIDE on exec access unless
448 * there is some effective exec access somewhere.
450 STATIC int
451 xfs_acl_capability_check(
452 mode_t mode,
453 cred_t *cr,
454 xfs_acl_t *fap)
456 if ((mode & ACL_READ) && !capable_cred(cr, CAP_DAC_READ_SEARCH))
457 return EACCES;
458 if ((mode & ACL_WRITE) && !capable_cred(cr, CAP_DAC_OVERRIDE))
459 return EACCES;
460 if ((mode & ACL_EXECUTE) &&
461 (!capable_cred(cr, CAP_DAC_OVERRIDE) ||
462 !xfs_acl_find_any_exec(fap))) {
463 return EACCES;
466 return 0;
470 * Note: cr is only used here for the capability check if the ACL test fails.
471 * It is not used to find out the credentials uid or groups etc, as was
472 * done in IRIX. It is assumed that the uid and groups for the current
473 * thread are taken from "current" instead of the cr parameter.
475 STATIC int
476 xfs_acl_access(
477 uid_t fuid,
478 gid_t fgid,
479 xfs_acl_t *fap,
480 mode_t md,
481 cred_t *cr)
483 xfs_acl_entry_t matched;
484 int i, allows;
485 int maskallows = -1; /* true, but not 1, either */
486 int seen_userobj = 0;
488 matched.ae_tag = 0; /* Invalid type */
489 md >>= 6; /* Normalize the bits for comparison */
491 for (i = 0; i < fap->acl_cnt; i++) {
493 * Break out if we've got a user_obj entry or
494 * a user entry and the mask (and have processed USER_OBJ)
496 if (matched.ae_tag == ACL_USER_OBJ)
497 break;
498 if (matched.ae_tag == ACL_USER) {
499 if (maskallows != -1 && seen_userobj)
500 break;
501 if (fap->acl_entry[i].ae_tag != ACL_MASK &&
502 fap->acl_entry[i].ae_tag != ACL_USER_OBJ)
503 continue;
505 /* True if this entry allows the requested access */
506 allows = ((fap->acl_entry[i].ae_perm & md) == md);
508 switch (fap->acl_entry[i].ae_tag) {
509 case ACL_USER_OBJ:
510 seen_userobj = 1;
511 if (fuid != current->fsuid)
512 continue;
513 matched.ae_tag = ACL_USER_OBJ;
514 matched.ae_perm = allows;
515 break;
516 case ACL_USER:
517 if (fap->acl_entry[i].ae_id != current->fsuid)
518 continue;
519 matched.ae_tag = ACL_USER;
520 matched.ae_perm = allows;
521 break;
522 case ACL_GROUP_OBJ:
523 if ((matched.ae_tag == ACL_GROUP_OBJ ||
524 matched.ae_tag == ACL_GROUP) && !allows)
525 continue;
526 if (!in_group_p(fgid))
527 continue;
528 matched.ae_tag = ACL_GROUP_OBJ;
529 matched.ae_perm = allows;
530 break;
531 case ACL_GROUP:
532 if ((matched.ae_tag == ACL_GROUP_OBJ ||
533 matched.ae_tag == ACL_GROUP) && !allows)
534 continue;
535 if (!in_group_p(fap->acl_entry[i].ae_id))
536 continue;
537 matched.ae_tag = ACL_GROUP;
538 matched.ae_perm = allows;
539 break;
540 case ACL_MASK:
541 maskallows = allows;
542 break;
543 case ACL_OTHER:
544 if (matched.ae_tag != 0)
545 continue;
546 matched.ae_tag = ACL_OTHER;
547 matched.ae_perm = allows;
548 break;
552 * First possibility is that no matched entry allows access.
553 * The capability to override DAC may exist, so check for it.
555 switch (matched.ae_tag) {
556 case ACL_OTHER:
557 case ACL_USER_OBJ:
558 if (matched.ae_perm)
559 return 0;
560 break;
561 case ACL_USER:
562 case ACL_GROUP_OBJ:
563 case ACL_GROUP:
564 if (maskallows && matched.ae_perm)
565 return 0;
566 break;
567 case 0:
568 break;
571 return xfs_acl_capability_check(md, cr, fap);
575 * ACL validity checker.
576 * This acl validation routine checks each ACL entry read in makes sense.
578 STATIC int
579 xfs_acl_invalid(
580 xfs_acl_t *aclp)
582 xfs_acl_entry_t *entry, *e;
583 int user = 0, group = 0, other = 0, mask = 0;
584 int mask_required = 0;
585 int i, j;
587 if (!aclp)
588 goto acl_invalid;
590 if (aclp->acl_cnt > XFS_ACL_MAX_ENTRIES)
591 goto acl_invalid;
593 for (i = 0; i < aclp->acl_cnt; i++) {
594 entry = &aclp->acl_entry[i];
595 switch (entry->ae_tag) {
596 case ACL_USER_OBJ:
597 if (user++)
598 goto acl_invalid;
599 break;
600 case ACL_GROUP_OBJ:
601 if (group++)
602 goto acl_invalid;
603 break;
604 case ACL_OTHER:
605 if (other++)
606 goto acl_invalid;
607 break;
608 case ACL_USER:
609 case ACL_GROUP:
610 for (j = i + 1; j < aclp->acl_cnt; j++) {
611 e = &aclp->acl_entry[j];
612 if (e->ae_id == entry->ae_id &&
613 e->ae_tag == entry->ae_tag)
614 goto acl_invalid;
616 mask_required++;
617 break;
618 case ACL_MASK:
619 if (mask++)
620 goto acl_invalid;
621 break;
622 default:
623 goto acl_invalid;
626 if (!user || !group || !other || (mask_required && !mask))
627 goto acl_invalid;
628 else
629 return 0;
630 acl_invalid:
631 return EINVAL;
635 * Do ACL endian conversion.
637 STATIC void
638 xfs_acl_get_endian(
639 xfs_acl_t *aclp)
641 xfs_acl_entry_t *ace, *end;
643 INT_SET(aclp->acl_cnt, ARCH_CONVERT, aclp->acl_cnt);
644 end = &aclp->acl_entry[0]+aclp->acl_cnt;
645 for (ace = &aclp->acl_entry[0]; ace < end; ace++) {
646 INT_SET(ace->ae_tag, ARCH_CONVERT, ace->ae_tag);
647 INT_SET(ace->ae_id, ARCH_CONVERT, ace->ae_id);
648 INT_SET(ace->ae_perm, ARCH_CONVERT, ace->ae_perm);
653 * Get the ACL from the EA and do endian conversion.
655 STATIC void
656 xfs_acl_get_attr(
657 vnode_t *vp,
658 xfs_acl_t *aclp,
659 int kind,
660 int flags,
661 int *error)
663 int len = sizeof(xfs_acl_t);
665 ASSERT((flags & ATTR_KERNOVAL) ? (aclp == NULL) : 1);
666 flags |= ATTR_ROOT;
667 VOP_ATTR_GET(vp,
668 kind == _ACL_TYPE_ACCESS ? SGI_ACL_FILE : SGI_ACL_DEFAULT,
669 (char *)aclp, &len, flags, sys_cred, *error);
670 if (*error || (flags & ATTR_KERNOVAL))
671 return;
672 xfs_acl_get_endian(aclp);
676 * Set the EA with the ACL and do endian conversion.
678 STATIC void
679 xfs_acl_set_attr(
680 vnode_t *vp,
681 xfs_acl_t *aclp,
682 int kind,
683 int *error)
685 xfs_acl_entry_t *ace, *newace, *end;
686 xfs_acl_t *newacl;
687 int len;
689 if (!(_ACL_ALLOC(newacl))) {
690 *error = ENOMEM;
691 return;
694 len = sizeof(xfs_acl_t) -
695 (sizeof(xfs_acl_entry_t) * (XFS_ACL_MAX_ENTRIES - aclp->acl_cnt));
696 end = &aclp->acl_entry[0]+aclp->acl_cnt;
697 for (ace = &aclp->acl_entry[0], newace = &newacl->acl_entry[0];
698 ace < end;
699 ace++, newace++) {
700 INT_SET(newace->ae_tag, ARCH_CONVERT, ace->ae_tag);
701 INT_SET(newace->ae_id, ARCH_CONVERT, ace->ae_id);
702 INT_SET(newace->ae_perm, ARCH_CONVERT, ace->ae_perm);
704 INT_SET(newacl->acl_cnt, ARCH_CONVERT, aclp->acl_cnt);
705 VOP_ATTR_SET(vp,
706 kind == _ACL_TYPE_ACCESS ? SGI_ACL_FILE: SGI_ACL_DEFAULT,
707 (char *)newacl, len, ATTR_ROOT, sys_cred, *error);
708 _ACL_FREE(newacl);
712 xfs_acl_vtoacl(
713 vnode_t *vp,
714 xfs_acl_t *access_acl,
715 xfs_acl_t *default_acl)
717 vattr_t va;
718 int error = 0;
720 if (access_acl) {
722 * Get the Access ACL and the mode. If either cannot
723 * be obtained for some reason, invalidate the access ACL.
725 xfs_acl_get_attr(vp, access_acl, _ACL_TYPE_ACCESS, 0, &error);
726 if (!error) {
727 /* Got the ACL, need the mode... */
728 va.va_mask = XFS_AT_MODE;
729 VOP_GETATTR(vp, &va, 0, sys_cred, error);
732 if (error)
733 access_acl->acl_cnt = XFS_ACL_NOT_PRESENT;
734 else /* We have a good ACL and the file mode, synchronize. */
735 xfs_acl_sync_mode(va.va_mode, access_acl);
738 if (default_acl) {
739 xfs_acl_get_attr(vp, default_acl, _ACL_TYPE_DEFAULT, 0, &error);
740 if (error)
741 default_acl->acl_cnt = XFS_ACL_NOT_PRESENT;
743 return error;
747 * This function retrieves the parent directory's acl, processes it
748 * and lets the child inherit the acl(s) that it should.
751 xfs_acl_inherit(
752 vnode_t *vp,
753 vattr_t *vap,
754 xfs_acl_t *pdaclp)
756 xfs_acl_t *cacl;
757 int error = 0;
758 int basicperms = 0;
761 * If the parent does not have a default ACL, or it's an
762 * invalid ACL, we're done.
764 if (!vp)
765 return 0;
766 if (!pdaclp || xfs_acl_invalid(pdaclp))
767 return 0;
770 * Copy the default ACL of the containing directory to
771 * the access ACL of the new file and use the mode that
772 * was passed in to set up the correct initial values for
773 * the u::,g::[m::], and o:: entries. This is what makes
774 * umask() "work" with ACL's.
777 if (!(_ACL_ALLOC(cacl)))
778 return ENOMEM;
780 memcpy(cacl, pdaclp, sizeof(xfs_acl_t));
781 xfs_acl_filter_mode(vap->va_mode, cacl);
782 xfs_acl_setmode(vp, cacl, &basicperms);
785 * Set the Default and Access ACL on the file. The mode is already
786 * set on the file, so we don't need to worry about that.
788 * If the new file is a directory, its default ACL is a copy of
789 * the containing directory's default ACL.
791 if (vp->v_type == VDIR)
792 xfs_acl_set_attr(vp, pdaclp, _ACL_TYPE_DEFAULT, &error);
793 if (!error && !basicperms)
794 xfs_acl_set_attr(vp, cacl, _ACL_TYPE_ACCESS, &error);
795 _ACL_FREE(cacl);
796 return error;
800 * Set up the correct mode on the file based on the supplied ACL. This
801 * makes sure that the mode on the file reflects the state of the
802 * u::,g::[m::], and o:: entries in the ACL. Since the mode is where
803 * the ACL is going to get the permissions for these entries, we must
804 * synchronize the mode whenever we set the ACL on a file.
806 STATIC int
807 xfs_acl_setmode(
808 vnode_t *vp,
809 xfs_acl_t *acl,
810 int *basicperms)
812 vattr_t va;
813 xfs_acl_entry_t *ap;
814 xfs_acl_entry_t *gap = NULL;
815 int i, error, nomask = 1;
817 *basicperms = 1;
819 if (acl->acl_cnt == XFS_ACL_NOT_PRESENT)
820 return 0;
823 * Copy the u::, g::, o::, and m:: bits from the ACL into the
824 * mode. The m:: bits take precedence over the g:: bits.
826 va.va_mask = XFS_AT_MODE;
827 VOP_GETATTR(vp, &va, 0, sys_cred, error);
828 if (error)
829 return error;
831 va.va_mask = XFS_AT_MODE;
832 va.va_mode &= ~(S_IRWXU|S_IRWXG|S_IRWXO);
833 ap = acl->acl_entry;
834 for (i = 0; i < acl->acl_cnt; ++i) {
835 switch (ap->ae_tag) {
836 case ACL_USER_OBJ:
837 va.va_mode |= ap->ae_perm << 6;
838 break;
839 case ACL_GROUP_OBJ:
840 gap = ap;
841 break;
842 case ACL_MASK: /* more than just standard modes */
843 nomask = 0;
844 va.va_mode |= ap->ae_perm << 3;
845 *basicperms = 0;
846 break;
847 case ACL_OTHER:
848 va.va_mode |= ap->ae_perm;
849 break;
850 default: /* more than just standard modes */
851 *basicperms = 0;
852 break;
854 ap++;
857 /* Set the group bits from ACL_GROUP_OBJ if there's no ACL_MASK */
858 if (gap && nomask)
859 va.va_mode |= gap->ae_perm << 3;
861 VOP_SETATTR(vp, &va, 0, sys_cred, error);
862 return error;
866 * The permissions for the special ACL entries (u::, g::[m::], o::) are
867 * actually stored in the file mode (if there is both a group and a mask,
868 * the group is stored in the ACL entry and the mask is stored on the file).
869 * This allows the mode to remain automatically in sync with the ACL without
870 * the need for a call-back to the ACL system at every point where the mode
871 * could change. This function takes the permissions from the specified mode
872 * and places it in the supplied ACL.
874 * This implementation draws its validity from the fact that, when the ACL
875 * was assigned, the mode was copied from the ACL.
876 * If the mode did not change, therefore, the mode remains exactly what was
877 * taken from the special ACL entries at assignment.
878 * If a subsequent chmod() was done, the POSIX spec says that the change in
879 * mode must cause an update to the ACL seen at user level and used for
880 * access checks. Before and after a mode change, therefore, the file mode
881 * most accurately reflects what the special ACL entries should permit/deny.
883 * CAVEAT: If someone sets the SGI_ACL_FILE attribute directly,
884 * the existing mode bits will override whatever is in the
885 * ACL. Similarly, if there is a pre-existing ACL that was
886 * never in sync with its mode (owing to a bug in 6.5 and
887 * before), it will now magically (or mystically) be
888 * synchronized. This could cause slight astonishment, but
889 * it is better than inconsistent permissions.
891 * The supplied ACL is a template that may contain any combination
892 * of special entries. These are treated as place holders when we fill
893 * out the ACL. This routine does not add or remove special entries, it
894 * simply unites each special entry with its associated set of permissions.
896 STATIC void
897 xfs_acl_sync_mode(
898 mode_t mode,
899 xfs_acl_t *acl)
901 int i, nomask = 1;
902 xfs_acl_entry_t *ap;
903 xfs_acl_entry_t *gap = NULL;
906 * Set ACL entries. POSIX1003.1eD16 requires that the MASK
907 * be set instead of the GROUP entry, if there is a MASK.
909 for (ap = acl->acl_entry, i = 0; i < acl->acl_cnt; ap++, i++) {
910 switch (ap->ae_tag) {
911 case ACL_USER_OBJ:
912 ap->ae_perm = (mode >> 6) & 0x7;
913 break;
914 case ACL_GROUP_OBJ:
915 gap = ap;
916 break;
917 case ACL_MASK:
918 nomask = 0;
919 ap->ae_perm = (mode >> 3) & 0x7;
920 break;
921 case ACL_OTHER:
922 ap->ae_perm = mode & 0x7;
923 break;
924 default:
925 break;
928 /* Set the ACL_GROUP_OBJ if there's no ACL_MASK */
929 if (gap && nomask)
930 gap->ae_perm = (mode >> 3) & 0x7;
934 * When inheriting an Access ACL from a directory Default ACL,
935 * the ACL bits are set to the intersection of the ACL default
936 * permission bits and the file permission bits in mode. If there
937 * are no permission bits on the file then we must not give them
938 * the ACL. This is what what makes umask() work with ACLs.
940 STATIC void
941 xfs_acl_filter_mode(
942 mode_t mode,
943 xfs_acl_t *acl)
945 int i, nomask = 1;
946 xfs_acl_entry_t *ap;
947 xfs_acl_entry_t *gap = NULL;
950 * Set ACL entries. POSIX1003.1eD16 requires that the MASK
951 * be merged with GROUP entry, if there is a MASK.
953 for (ap = acl->acl_entry, i = 0; i < acl->acl_cnt; ap++, i++) {
954 switch (ap->ae_tag) {
955 case ACL_USER_OBJ:
956 ap->ae_perm &= (mode >> 6) & 0x7;
957 break;
958 case ACL_GROUP_OBJ:
959 gap = ap;
960 break;
961 case ACL_MASK:
962 nomask = 0;
963 ap->ae_perm &= (mode >> 3) & 0x7;
964 break;
965 case ACL_OTHER:
966 ap->ae_perm &= mode & 0x7;
967 break;
968 default:
969 break;
972 /* Set the ACL_GROUP_OBJ if there's no ACL_MASK */
973 if (gap && nomask)
974 gap->ae_perm &= (mode >> 3) & 0x7;