added 2.6.29.6 aldebaran kernel
[nao-ulib.git] / kernel / 2.6.29.6-aldebaran-rt / fs / xfs / xfs_acl.c
blob0baf318d43be21eaedc5ae33eb62201e3a5d93db
1 /*
2 * Copyright (c) 2001-2002,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 #include "xfs.h"
19 #include "xfs_fs.h"
20 #include "xfs_types.h"
21 #include "xfs_bit.h"
22 #include "xfs_inum.h"
23 #include "xfs_ag.h"
24 #include "xfs_dir2.h"
25 #include "xfs_bmap_btree.h"
26 #include "xfs_alloc_btree.h"
27 #include "xfs_ialloc_btree.h"
28 #include "xfs_dir2_sf.h"
29 #include "xfs_attr_sf.h"
30 #include "xfs_dinode.h"
31 #include "xfs_inode.h"
32 #include "xfs_btree.h"
33 #include "xfs_acl.h"
34 #include "xfs_attr.h"
35 #include "xfs_vnodeops.h"
37 #include <linux/capability.h>
38 #include <linux/posix_acl_xattr.h>
40 STATIC int xfs_acl_setmode(struct inode *, xfs_acl_t *, int *);
41 STATIC void xfs_acl_filter_mode(mode_t, xfs_acl_t *);
42 STATIC void xfs_acl_get_endian(xfs_acl_t *);
43 STATIC int xfs_acl_access(uid_t, gid_t, xfs_acl_t *, mode_t, cred_t *);
44 STATIC int xfs_acl_invalid(xfs_acl_t *);
45 STATIC void xfs_acl_sync_mode(mode_t, xfs_acl_t *);
46 STATIC void xfs_acl_get_attr(struct inode *, xfs_acl_t *, int, int, int *);
47 STATIC void xfs_acl_set_attr(struct inode *, xfs_acl_t *, int, int *);
48 STATIC int xfs_acl_allow_set(struct inode *, int);
50 kmem_zone_t *xfs_acl_zone;
54 * ACL handling.
56 int
57 xfs_decode_acl(const char *name)
59 if (strcmp(name, "posix_acl_access") == 0)
60 return _ACL_TYPE_ACCESS;
61 else if (strcmp(name, "posix_acl_default") == 0)
62 return _ACL_TYPE_DEFAULT;
63 return -EINVAL;
67 * Test for existence of access ACL attribute as efficiently as possible.
69 int
70 xfs_acl_vhasacl_access(
71 struct inode *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 struct inode *vp)
86 int error;
88 if (!S_ISDIR(vp->i_mode))
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 EOPNOTSUPP;
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 xfs_sort().
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 xfs_sort(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 struct inode *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 if(size) {
234 if (!(_ACL_ALLOC(xfs_acl))) {
235 error = ENOMEM;
236 goto out;
238 memset(xfs_acl, 0, sizeof(xfs_acl_t));
239 } else
240 flags = ATTR_KERNOVAL;
242 xfs_acl_get_attr(vp, xfs_acl, kind, flags, &error);
243 if (error)
244 goto out;
246 if (!size) {
247 error = -posix_acl_xattr_size(XFS_ACL_MAX_ENTRIES);
248 } else {
249 if (xfs_acl_invalid(xfs_acl)) {
250 error = EINVAL;
251 goto out;
253 if (kind == _ACL_TYPE_ACCESS)
254 xfs_acl_sync_mode(XFS_I(vp)->i_d.di_mode, xfs_acl);
255 error = -posix_acl_xfs_to_xattr(xfs_acl, ext_acl, size);
257 out:
258 if(xfs_acl)
259 _ACL_FREE(xfs_acl);
260 return -error;
264 xfs_acl_vremove(
265 struct inode *vp,
266 int kind)
268 int error;
270 error = xfs_acl_allow_set(vp, kind);
271 if (!error) {
272 error = xfs_attr_remove(XFS_I(vp),
273 kind == _ACL_TYPE_DEFAULT?
274 SGI_ACL_DEFAULT: SGI_ACL_FILE,
275 ATTR_ROOT);
276 if (error == ENOATTR)
277 error = 0; /* 'scool */
279 return -error;
283 xfs_acl_vset(
284 struct inode *vp,
285 void *acl,
286 size_t size,
287 int kind)
289 posix_acl_xattr_header *ext_acl = acl;
290 xfs_acl_t *xfs_acl;
291 int error;
292 int basicperms = 0; /* more than std unix perms? */
294 if (!acl)
295 return -EINVAL;
297 if (!(_ACL_ALLOC(xfs_acl)))
298 return -ENOMEM;
300 error = posix_acl_xattr_to_xfs(ext_acl, size, xfs_acl);
301 if (error) {
302 _ACL_FREE(xfs_acl);
303 return -error;
305 if (!xfs_acl->acl_cnt) {
306 _ACL_FREE(xfs_acl);
307 return 0;
310 error = xfs_acl_allow_set(vp, kind);
312 /* Incoming ACL exists, set file mode based on its value */
313 if (!error && kind == _ACL_TYPE_ACCESS)
314 error = xfs_acl_setmode(vp, xfs_acl, &basicperms);
316 if (error)
317 goto out;
320 * If we have more than std unix permissions, set up the actual attr.
321 * Otherwise, delete any existing attr. This prevents us from
322 * having actual attrs for permissions that can be stored in the
323 * standard permission bits.
325 if (!basicperms) {
326 xfs_acl_set_attr(vp, xfs_acl, kind, &error);
327 } else {
328 error = -xfs_acl_vremove(vp, _ACL_TYPE_ACCESS);
331 out:
332 _ACL_FREE(xfs_acl);
333 return -error;
337 xfs_acl_iaccess(
338 xfs_inode_t *ip,
339 mode_t mode,
340 cred_t *cr)
342 xfs_acl_t *acl;
343 int rval;
344 struct xfs_name acl_name = {SGI_ACL_FILE, SGI_ACL_FILE_SIZE};
346 if (!(_ACL_ALLOC(acl)))
347 return -1;
349 /* If the file has no ACL return -1. */
350 rval = sizeof(xfs_acl_t);
351 if (xfs_attr_fetch(ip, &acl_name, (char *)acl, &rval, ATTR_ROOT)) {
352 _ACL_FREE(acl);
353 return -1;
355 xfs_acl_get_endian(acl);
357 /* If the file has an empty ACL return -1. */
358 if (acl->acl_cnt == XFS_ACL_NOT_PRESENT) {
359 _ACL_FREE(acl);
360 return -1;
363 /* Synchronize ACL with mode bits */
364 xfs_acl_sync_mode(ip->i_d.di_mode, acl);
366 rval = xfs_acl_access(ip->i_d.di_uid, ip->i_d.di_gid, acl, mode, cr);
367 _ACL_FREE(acl);
368 return rval;
371 STATIC int
372 xfs_acl_allow_set(
373 struct inode *vp,
374 int kind)
376 if (vp->i_flags & (S_IMMUTABLE|S_APPEND))
377 return EPERM;
378 if (kind == _ACL_TYPE_DEFAULT && !S_ISDIR(vp->i_mode))
379 return ENOTDIR;
380 if (vp->i_sb->s_flags & MS_RDONLY)
381 return EROFS;
382 if (XFS_I(vp)->i_d.di_uid != current_fsuid() && !capable(CAP_FOWNER))
383 return EPERM;
384 return 0;
388 * Note: cr is only used here for the capability check if the ACL test fails.
389 * It is not used to find out the credentials uid or groups etc, as was
390 * done in IRIX. It is assumed that the uid and groups for the current
391 * thread are taken from "current" instead of the cr parameter.
393 STATIC int
394 xfs_acl_access(
395 uid_t fuid,
396 gid_t fgid,
397 xfs_acl_t *fap,
398 mode_t md,
399 cred_t *cr)
401 xfs_acl_entry_t matched;
402 int i, allows;
403 int maskallows = -1; /* true, but not 1, either */
404 int seen_userobj = 0;
406 matched.ae_tag = 0; /* Invalid type */
407 matched.ae_perm = 0;
409 for (i = 0; i < fap->acl_cnt; i++) {
411 * Break out if we've got a user_obj entry or
412 * a user entry and the mask (and have processed USER_OBJ)
414 if (matched.ae_tag == ACL_USER_OBJ)
415 break;
416 if (matched.ae_tag == ACL_USER) {
417 if (maskallows != -1 && seen_userobj)
418 break;
419 if (fap->acl_entry[i].ae_tag != ACL_MASK &&
420 fap->acl_entry[i].ae_tag != ACL_USER_OBJ)
421 continue;
423 /* True if this entry allows the requested access */
424 allows = ((fap->acl_entry[i].ae_perm & md) == md);
426 switch (fap->acl_entry[i].ae_tag) {
427 case ACL_USER_OBJ:
428 seen_userobj = 1;
429 if (fuid != current_fsuid())
430 continue;
431 matched.ae_tag = ACL_USER_OBJ;
432 matched.ae_perm = allows;
433 break;
434 case ACL_USER:
435 if (fap->acl_entry[i].ae_id != current_fsuid())
436 continue;
437 matched.ae_tag = ACL_USER;
438 matched.ae_perm = allows;
439 break;
440 case ACL_GROUP_OBJ:
441 if ((matched.ae_tag == ACL_GROUP_OBJ ||
442 matched.ae_tag == ACL_GROUP) && !allows)
443 continue;
444 if (!in_group_p(fgid))
445 continue;
446 matched.ae_tag = ACL_GROUP_OBJ;
447 matched.ae_perm = allows;
448 break;
449 case ACL_GROUP:
450 if ((matched.ae_tag == ACL_GROUP_OBJ ||
451 matched.ae_tag == ACL_GROUP) && !allows)
452 continue;
453 if (!in_group_p(fap->acl_entry[i].ae_id))
454 continue;
455 matched.ae_tag = ACL_GROUP;
456 matched.ae_perm = allows;
457 break;
458 case ACL_MASK:
459 maskallows = allows;
460 break;
461 case ACL_OTHER:
462 if (matched.ae_tag != 0)
463 continue;
464 matched.ae_tag = ACL_OTHER;
465 matched.ae_perm = allows;
466 break;
470 * First possibility is that no matched entry allows access.
471 * The capability to override DAC may exist, so check for it.
473 switch (matched.ae_tag) {
474 case ACL_OTHER:
475 case ACL_USER_OBJ:
476 if (matched.ae_perm)
477 return 0;
478 break;
479 case ACL_USER:
480 case ACL_GROUP_OBJ:
481 case ACL_GROUP:
482 if (maskallows && matched.ae_perm)
483 return 0;
484 break;
485 case 0:
486 break;
489 /* EACCES tells generic_permission to check for capability overrides */
490 return EACCES;
494 * ACL validity checker.
495 * This acl validation routine checks each ACL entry read in makes sense.
497 STATIC int
498 xfs_acl_invalid(
499 xfs_acl_t *aclp)
501 xfs_acl_entry_t *entry, *e;
502 int user = 0, group = 0, other = 0, mask = 0;
503 int mask_required = 0;
504 int i, j;
506 if (!aclp)
507 goto acl_invalid;
509 if (aclp->acl_cnt > XFS_ACL_MAX_ENTRIES)
510 goto acl_invalid;
512 for (i = 0; i < aclp->acl_cnt; i++) {
513 entry = &aclp->acl_entry[i];
514 switch (entry->ae_tag) {
515 case ACL_USER_OBJ:
516 if (user++)
517 goto acl_invalid;
518 break;
519 case ACL_GROUP_OBJ:
520 if (group++)
521 goto acl_invalid;
522 break;
523 case ACL_OTHER:
524 if (other++)
525 goto acl_invalid;
526 break;
527 case ACL_USER:
528 case ACL_GROUP:
529 for (j = i + 1; j < aclp->acl_cnt; j++) {
530 e = &aclp->acl_entry[j];
531 if (e->ae_id == entry->ae_id &&
532 e->ae_tag == entry->ae_tag)
533 goto acl_invalid;
535 mask_required++;
536 break;
537 case ACL_MASK:
538 if (mask++)
539 goto acl_invalid;
540 break;
541 default:
542 goto acl_invalid;
545 if (!user || !group || !other || (mask_required && !mask))
546 goto acl_invalid;
547 else
548 return 0;
549 acl_invalid:
550 return EINVAL;
554 * Do ACL endian conversion.
556 STATIC void
557 xfs_acl_get_endian(
558 xfs_acl_t *aclp)
560 xfs_acl_entry_t *ace, *end;
562 INT_SET(aclp->acl_cnt, ARCH_CONVERT, aclp->acl_cnt);
563 end = &aclp->acl_entry[0]+aclp->acl_cnt;
564 for (ace = &aclp->acl_entry[0]; ace < end; ace++) {
565 INT_SET(ace->ae_tag, ARCH_CONVERT, ace->ae_tag);
566 INT_SET(ace->ae_id, ARCH_CONVERT, ace->ae_id);
567 INT_SET(ace->ae_perm, ARCH_CONVERT, ace->ae_perm);
572 * Get the ACL from the EA and do endian conversion.
574 STATIC void
575 xfs_acl_get_attr(
576 struct inode *vp,
577 xfs_acl_t *aclp,
578 int kind,
579 int flags,
580 int *error)
582 int len = sizeof(xfs_acl_t);
584 ASSERT((flags & ATTR_KERNOVAL) ? (aclp == NULL) : 1);
585 flags |= ATTR_ROOT;
586 *error = xfs_attr_get(XFS_I(vp),
587 kind == _ACL_TYPE_ACCESS ?
588 SGI_ACL_FILE : SGI_ACL_DEFAULT,
589 (char *)aclp, &len, flags);
590 if (*error || (flags & ATTR_KERNOVAL))
591 return;
592 xfs_acl_get_endian(aclp);
596 * Set the EA with the ACL and do endian conversion.
598 STATIC void
599 xfs_acl_set_attr(
600 struct inode *vp,
601 xfs_acl_t *aclp,
602 int kind,
603 int *error)
605 xfs_acl_entry_t *ace, *newace, *end;
606 xfs_acl_t *newacl;
607 int len;
609 if (!(_ACL_ALLOC(newacl))) {
610 *error = ENOMEM;
611 return;
614 len = sizeof(xfs_acl_t) -
615 (sizeof(xfs_acl_entry_t) * (XFS_ACL_MAX_ENTRIES - aclp->acl_cnt));
616 end = &aclp->acl_entry[0]+aclp->acl_cnt;
617 for (ace = &aclp->acl_entry[0], newace = &newacl->acl_entry[0];
618 ace < end;
619 ace++, newace++) {
620 INT_SET(newace->ae_tag, ARCH_CONVERT, ace->ae_tag);
621 INT_SET(newace->ae_id, ARCH_CONVERT, ace->ae_id);
622 INT_SET(newace->ae_perm, ARCH_CONVERT, ace->ae_perm);
624 INT_SET(newacl->acl_cnt, ARCH_CONVERT, aclp->acl_cnt);
625 *error = xfs_attr_set(XFS_I(vp),
626 kind == _ACL_TYPE_ACCESS ?
627 SGI_ACL_FILE: SGI_ACL_DEFAULT,
628 (char *)newacl, len, ATTR_ROOT);
629 _ACL_FREE(newacl);
633 xfs_acl_vtoacl(
634 struct inode *vp,
635 xfs_acl_t *access_acl,
636 xfs_acl_t *default_acl)
638 int error = 0;
640 if (access_acl) {
642 * Get the Access ACL and the mode. If either cannot
643 * be obtained for some reason, invalidate the access ACL.
645 xfs_acl_get_attr(vp, access_acl, _ACL_TYPE_ACCESS, 0, &error);
646 if (error)
647 access_acl->acl_cnt = XFS_ACL_NOT_PRESENT;
648 else /* We have a good ACL and the file mode, synchronize. */
649 xfs_acl_sync_mode(XFS_I(vp)->i_d.di_mode, access_acl);
652 if (default_acl) {
653 xfs_acl_get_attr(vp, default_acl, _ACL_TYPE_DEFAULT, 0, &error);
654 if (error)
655 default_acl->acl_cnt = XFS_ACL_NOT_PRESENT;
657 return error;
661 * This function retrieves the parent directory's acl, processes it
662 * and lets the child inherit the acl(s) that it should.
665 xfs_acl_inherit(
666 struct inode *vp,
667 mode_t mode,
668 xfs_acl_t *pdaclp)
670 xfs_acl_t *cacl;
671 int error = 0;
672 int basicperms = 0;
675 * If the parent does not have a default ACL, or it's an
676 * invalid ACL, we're done.
678 if (!vp)
679 return 0;
680 if (!pdaclp || xfs_acl_invalid(pdaclp))
681 return 0;
684 * Copy the default ACL of the containing directory to
685 * the access ACL of the new file and use the mode that
686 * was passed in to set up the correct initial values for
687 * the u::,g::[m::], and o:: entries. This is what makes
688 * umask() "work" with ACL's.
691 if (!(_ACL_ALLOC(cacl)))
692 return ENOMEM;
694 memcpy(cacl, pdaclp, sizeof(xfs_acl_t));
695 xfs_acl_filter_mode(mode, cacl);
696 error = xfs_acl_setmode(vp, cacl, &basicperms);
697 if (error)
698 goto out_error;
701 * Set the Default and Access ACL on the file. The mode is already
702 * set on the file, so we don't need to worry about that.
704 * If the new file is a directory, its default ACL is a copy of
705 * the containing directory's default ACL.
707 if (S_ISDIR(vp->i_mode))
708 xfs_acl_set_attr(vp, pdaclp, _ACL_TYPE_DEFAULT, &error);
709 if (!error && !basicperms)
710 xfs_acl_set_attr(vp, cacl, _ACL_TYPE_ACCESS, &error);
711 out_error:
712 _ACL_FREE(cacl);
713 return error;
717 * Set up the correct mode on the file based on the supplied ACL. This
718 * makes sure that the mode on the file reflects the state of the
719 * u::,g::[m::], and o:: entries in the ACL. Since the mode is where
720 * the ACL is going to get the permissions for these entries, we must
721 * synchronize the mode whenever we set the ACL on a file.
723 STATIC int
724 xfs_acl_setmode(
725 struct inode *vp,
726 xfs_acl_t *acl,
727 int *basicperms)
729 struct iattr iattr;
730 xfs_acl_entry_t *ap;
731 xfs_acl_entry_t *gap = NULL;
732 int i, nomask = 1;
734 *basicperms = 1;
736 if (acl->acl_cnt == XFS_ACL_NOT_PRESENT)
737 return 0;
740 * Copy the u::, g::, o::, and m:: bits from the ACL into the
741 * mode. The m:: bits take precedence over the g:: bits.
743 iattr.ia_valid = ATTR_MODE;
744 iattr.ia_mode = XFS_I(vp)->i_d.di_mode;
745 iattr.ia_mode &= ~(S_IRWXU|S_IRWXG|S_IRWXO);
746 ap = acl->acl_entry;
747 for (i = 0; i < acl->acl_cnt; ++i) {
748 switch (ap->ae_tag) {
749 case ACL_USER_OBJ:
750 iattr.ia_mode |= ap->ae_perm << 6;
751 break;
752 case ACL_GROUP_OBJ:
753 gap = ap;
754 break;
755 case ACL_MASK: /* more than just standard modes */
756 nomask = 0;
757 iattr.ia_mode |= ap->ae_perm << 3;
758 *basicperms = 0;
759 break;
760 case ACL_OTHER:
761 iattr.ia_mode |= ap->ae_perm;
762 break;
763 default: /* more than just standard modes */
764 *basicperms = 0;
765 break;
767 ap++;
770 /* Set the group bits from ACL_GROUP_OBJ if there's no ACL_MASK */
771 if (gap && nomask)
772 iattr.ia_mode |= gap->ae_perm << 3;
774 return xfs_setattr(XFS_I(vp), &iattr, 0);
778 * The permissions for the special ACL entries (u::, g::[m::], o::) are
779 * actually stored in the file mode (if there is both a group and a mask,
780 * the group is stored in the ACL entry and the mask is stored on the file).
781 * This allows the mode to remain automatically in sync with the ACL without
782 * the need for a call-back to the ACL system at every point where the mode
783 * could change. This function takes the permissions from the specified mode
784 * and places it in the supplied ACL.
786 * This implementation draws its validity from the fact that, when the ACL
787 * was assigned, the mode was copied from the ACL.
788 * If the mode did not change, therefore, the mode remains exactly what was
789 * taken from the special ACL entries at assignment.
790 * If a subsequent chmod() was done, the POSIX spec says that the change in
791 * mode must cause an update to the ACL seen at user level and used for
792 * access checks. Before and after a mode change, therefore, the file mode
793 * most accurately reflects what the special ACL entries should permit/deny.
795 * CAVEAT: If someone sets the SGI_ACL_FILE attribute directly,
796 * the existing mode bits will override whatever is in the
797 * ACL. Similarly, if there is a pre-existing ACL that was
798 * never in sync with its mode (owing to a bug in 6.5 and
799 * before), it will now magically (or mystically) be
800 * synchronized. This could cause slight astonishment, but
801 * it is better than inconsistent permissions.
803 * The supplied ACL is a template that may contain any combination
804 * of special entries. These are treated as place holders when we fill
805 * out the ACL. This routine does not add or remove special entries, it
806 * simply unites each special entry with its associated set of permissions.
808 STATIC void
809 xfs_acl_sync_mode(
810 mode_t mode,
811 xfs_acl_t *acl)
813 int i, nomask = 1;
814 xfs_acl_entry_t *ap;
815 xfs_acl_entry_t *gap = NULL;
818 * Set ACL entries. POSIX1003.1eD16 requires that the MASK
819 * be set instead of the GROUP entry, if there is a MASK.
821 for (ap = acl->acl_entry, i = 0; i < acl->acl_cnt; ap++, i++) {
822 switch (ap->ae_tag) {
823 case ACL_USER_OBJ:
824 ap->ae_perm = (mode >> 6) & 0x7;
825 break;
826 case ACL_GROUP_OBJ:
827 gap = ap;
828 break;
829 case ACL_MASK:
830 nomask = 0;
831 ap->ae_perm = (mode >> 3) & 0x7;
832 break;
833 case ACL_OTHER:
834 ap->ae_perm = mode & 0x7;
835 break;
836 default:
837 break;
840 /* Set the ACL_GROUP_OBJ if there's no ACL_MASK */
841 if (gap && nomask)
842 gap->ae_perm = (mode >> 3) & 0x7;
846 * When inheriting an Access ACL from a directory Default ACL,
847 * the ACL bits are set to the intersection of the ACL default
848 * permission bits and the file permission bits in mode. If there
849 * are no permission bits on the file then we must not give them
850 * the ACL. This is what what makes umask() work with ACLs.
852 STATIC void
853 xfs_acl_filter_mode(
854 mode_t mode,
855 xfs_acl_t *acl)
857 int i, nomask = 1;
858 xfs_acl_entry_t *ap;
859 xfs_acl_entry_t *gap = NULL;
862 * Set ACL entries. POSIX1003.1eD16 requires that the MASK
863 * be merged with GROUP entry, if there is a MASK.
865 for (ap = acl->acl_entry, i = 0; i < acl->acl_cnt; ap++, i++) {
866 switch (ap->ae_tag) {
867 case ACL_USER_OBJ:
868 ap->ae_perm &= (mode >> 6) & 0x7;
869 break;
870 case ACL_GROUP_OBJ:
871 gap = ap;
872 break;
873 case ACL_MASK:
874 nomask = 0;
875 ap->ae_perm &= (mode >> 3) & 0x7;
876 break;
877 case ACL_OTHER:
878 ap->ae_perm &= mode & 0x7;
879 break;
880 default:
881 break;
884 /* Set the ACL_GROUP_OBJ if there's no ACL_MASK */
885 if (gap && nomask)
886 gap->ae_perm &= (mode >> 3) & 0x7;