17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
580d34432Sfrankho * Common Development and Distribution License (the "License").
680d34432Sfrankho * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate /*
22*60c8e821SFrank Batschulat * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
237c478bd9Sstevel@tonic-gate * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate #include <sys/types.h>
277c478bd9Sstevel@tonic-gate #include <sys/stat.h>
287c478bd9Sstevel@tonic-gate #include <sys/errno.h>
297c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
307c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
317c478bd9Sstevel@tonic-gate #include <sys/ksynch.h>
327c478bd9Sstevel@tonic-gate #include <sys/buf.h>
337c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
347c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
357c478bd9Sstevel@tonic-gate #include <sys/mode.h>
367c478bd9Sstevel@tonic-gate #include <sys/systm.h>
377c478bd9Sstevel@tonic-gate #include <vm/seg.h>
387c478bd9Sstevel@tonic-gate #include <sys/file.h>
397c478bd9Sstevel@tonic-gate #include <sys/acl.h>
407c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_inode.h>
417c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_acl.h>
427c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_quota.h>
437c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
447c478bd9Sstevel@tonic-gate #include <sys/debug.h>
457c478bd9Sstevel@tonic-gate #include <sys/policy.h>
467c478bd9Sstevel@tonic-gate
477c478bd9Sstevel@tonic-gate /* Cache routines */
487c478bd9Sstevel@tonic-gate static int si_signature(si_t *);
497c478bd9Sstevel@tonic-gate static int si_cachei_get(struct inode *, si_t **);
507c478bd9Sstevel@tonic-gate static int si_cachea_get(struct inode *, si_t *, si_t **);
517c478bd9Sstevel@tonic-gate static int si_cmp(si_t *, si_t *);
527c478bd9Sstevel@tonic-gate static void si_cache_put(si_t *);
537c478bd9Sstevel@tonic-gate void si_cache_del(si_t *, int);
547c478bd9Sstevel@tonic-gate void si_cache_init(void);
557c478bd9Sstevel@tonic-gate
567c478bd9Sstevel@tonic-gate static void ufs_si_free_mem(si_t *);
577c478bd9Sstevel@tonic-gate static int ufs_si_store(struct inode *, si_t *, int, cred_t *);
587c478bd9Sstevel@tonic-gate static si_t *ufs_acl_cp(si_t *);
597c478bd9Sstevel@tonic-gate static int ufs_sectobuf(si_t *, caddr_t *, size_t *);
607c478bd9Sstevel@tonic-gate static int acl_count(ufs_ic_acl_t *);
617c478bd9Sstevel@tonic-gate static int acl_validate(aclent_t *, int, int);
627c478bd9Sstevel@tonic-gate static int vsecattr2aclentry(vsecattr_t *, si_t **);
637c478bd9Sstevel@tonic-gate static int aclentry2vsecattr(si_t *, vsecattr_t *);
647c478bd9Sstevel@tonic-gate
657c478bd9Sstevel@tonic-gate krwlock_t si_cache_lock; /* Protects si_cache */
667c478bd9Sstevel@tonic-gate int si_cachecnt = 64; /* # buckets in si_cache[a|i] */
677c478bd9Sstevel@tonic-gate si_t **si_cachea; /* The 'by acl' cache chains */
687c478bd9Sstevel@tonic-gate si_t **si_cachei; /* The 'by inode' cache chains */
697c478bd9Sstevel@tonic-gate long si_cachehit = 0;
707c478bd9Sstevel@tonic-gate long si_cachemiss = 0;
717c478bd9Sstevel@tonic-gate
727c478bd9Sstevel@tonic-gate #define SI_HASH(S) ((int)(S) & (si_cachecnt - 1))
737c478bd9Sstevel@tonic-gate
747c478bd9Sstevel@tonic-gate /*
757c478bd9Sstevel@tonic-gate * Store the new acls in aclp. Attempts to make things atomic.
767c478bd9Sstevel@tonic-gate * Search the acl cache for an identical sp and, if found, attach
777c478bd9Sstevel@tonic-gate * the cache'd acl to ip. If the acl is new (not in the cache),
787c478bd9Sstevel@tonic-gate * add it to the cache, then attach it to ip. Last, remove and
797c478bd9Sstevel@tonic-gate * decrement the reference count of any prior acl list attached
807c478bd9Sstevel@tonic-gate * to the ip.
817c478bd9Sstevel@tonic-gate *
827c478bd9Sstevel@tonic-gate * Parameters:
837c478bd9Sstevel@tonic-gate * ip - Ptr to inode to receive the acl list
847c478bd9Sstevel@tonic-gate * sp - Ptr to in-core acl structure to attach to the inode.
857c478bd9Sstevel@tonic-gate * puship - 0 do not push the object inode(ip) 1 push the ip
867c478bd9Sstevel@tonic-gate * cr - Ptr to credentials
877c478bd9Sstevel@tonic-gate *
887c478bd9Sstevel@tonic-gate * Returns: 0 - Success
897c478bd9Sstevel@tonic-gate * N - From errno.h
907c478bd9Sstevel@tonic-gate */
917c478bd9Sstevel@tonic-gate static int
ufs_si_store(struct inode * ip,si_t * sp,int puship,cred_t * cr)927c478bd9Sstevel@tonic-gate ufs_si_store(struct inode *ip, si_t *sp, int puship, cred_t *cr)
937c478bd9Sstevel@tonic-gate {
947c478bd9Sstevel@tonic-gate struct vfs *vfsp;
957c478bd9Sstevel@tonic-gate struct inode *sip;
967c478bd9Sstevel@tonic-gate si_t *oldsp;
977c478bd9Sstevel@tonic-gate si_t *csp;
987c478bd9Sstevel@tonic-gate caddr_t acldata;
997c478bd9Sstevel@tonic-gate ino_t oldshadow;
1007c478bd9Sstevel@tonic-gate size_t acldatalen;
1017c478bd9Sstevel@tonic-gate off_t offset;
1027c478bd9Sstevel@tonic-gate int shadow;
1037c478bd9Sstevel@tonic-gate int err;
1047c478bd9Sstevel@tonic-gate int refcnt;
1057c478bd9Sstevel@tonic-gate int usecnt;
1067c478bd9Sstevel@tonic-gate int signature;
1077c478bd9Sstevel@tonic-gate int resid;
1087c478bd9Sstevel@tonic-gate struct ufsvfs *ufsvfsp = ip->i_ufsvfs;
1097c478bd9Sstevel@tonic-gate struct fs *fs = ufsvfsp->vfs_fs;
1107c478bd9Sstevel@tonic-gate
1117c478bd9Sstevel@tonic-gate ASSERT(RW_WRITE_HELD(&ip->i_contents));
1127c478bd9Sstevel@tonic-gate ASSERT(ip->i_ufs_acl != sp);
1137c478bd9Sstevel@tonic-gate
1147c478bd9Sstevel@tonic-gate if (!CHECK_ACL_ALLOWED(ip->i_mode & IFMT))
1157c478bd9Sstevel@tonic-gate return (ENOSYS);
1167c478bd9Sstevel@tonic-gate
1177c478bd9Sstevel@tonic-gate /*
1187c478bd9Sstevel@tonic-gate * if there are only the three owner/group/other then do not
1197c478bd9Sstevel@tonic-gate * create a shadow inode. If there is already a shadow with
1207c478bd9Sstevel@tonic-gate * the file, remove it.
1217c478bd9Sstevel@tonic-gate *
1227c478bd9Sstevel@tonic-gate */
1237c478bd9Sstevel@tonic-gate if (!sp->ausers &&
1247c478bd9Sstevel@tonic-gate !sp->agroups &&
1257c478bd9Sstevel@tonic-gate !sp->downer &&
1267c478bd9Sstevel@tonic-gate !sp->dgroup &&
1277c478bd9Sstevel@tonic-gate !sp->dother &&
1287c478bd9Sstevel@tonic-gate sp->dclass.acl_ismask == 0 &&
1297c478bd9Sstevel@tonic-gate !sp->dusers &&
1307c478bd9Sstevel@tonic-gate !sp->dgroups) {
1317c478bd9Sstevel@tonic-gate if (ip->i_ufs_acl)
1327c478bd9Sstevel@tonic-gate err = ufs_si_free(ip->i_ufs_acl, ITOV(ip)->v_vfsp, cr);
1337c478bd9Sstevel@tonic-gate ip->i_ufs_acl = NULL;
1347c478bd9Sstevel@tonic-gate ip->i_shadow = 0;
1357c478bd9Sstevel@tonic-gate ip->i_flag |= IMOD | IACC;
1367c478bd9Sstevel@tonic-gate ip->i_mode = (ip->i_smode & ~0777) |
1377c478bd9Sstevel@tonic-gate ((sp->aowner->acl_ic_perm & 07) << 6) |
1382879e8a4Sprabahar (MASK2MODE(sp)) |
1397c478bd9Sstevel@tonic-gate (sp->aother->acl_ic_perm & 07);
1407c478bd9Sstevel@tonic-gate TRANS_INODE(ip->i_ufsvfs, ip);
1417c478bd9Sstevel@tonic-gate ufs_iupdat(ip, 1);
1427c478bd9Sstevel@tonic-gate ufs_si_free_mem(sp);
1437c478bd9Sstevel@tonic-gate return (0);
1447c478bd9Sstevel@tonic-gate }
1457c478bd9Sstevel@tonic-gate
1467c478bd9Sstevel@tonic-gate loop:
1477c478bd9Sstevel@tonic-gate
1487c478bd9Sstevel@tonic-gate /*
1497c478bd9Sstevel@tonic-gate * Check cache. If in cache, use existing shadow inode.
1507c478bd9Sstevel@tonic-gate * Increment the shadow link count, then attach to the
1517c478bd9Sstevel@tonic-gate * cached ufs_acl_entry struct, and increment it's reference
1527c478bd9Sstevel@tonic-gate * count. Then discard the passed-in ufs_acl_entry and
1537c478bd9Sstevel@tonic-gate * return.
1547c478bd9Sstevel@tonic-gate */
1557c478bd9Sstevel@tonic-gate if (si_cachea_get(ip, sp, &csp) == 0) {
1567c478bd9Sstevel@tonic-gate ASSERT(RW_WRITE_HELD(&csp->s_lock));
1577c478bd9Sstevel@tonic-gate if (ip->i_ufs_acl == csp) {
1587c478bd9Sstevel@tonic-gate rw_exit(&csp->s_lock);
1597c478bd9Sstevel@tonic-gate (void) ufs_si_free_mem(sp);
1607c478bd9Sstevel@tonic-gate return (0);
1617c478bd9Sstevel@tonic-gate }
1627c478bd9Sstevel@tonic-gate vfsp = ITOV(ip)->v_vfsp;
1637c478bd9Sstevel@tonic-gate ASSERT(csp->s_shadow <= INT_MAX);
1647c478bd9Sstevel@tonic-gate shadow = (int)csp->s_shadow;
1657c478bd9Sstevel@tonic-gate /*
1667c478bd9Sstevel@tonic-gate * We can't call ufs_iget while holding the csp locked,
1677c478bd9Sstevel@tonic-gate * because we might deadlock. So we drop the
1687c478bd9Sstevel@tonic-gate * lock on csp, then go search the si_cache again
1697c478bd9Sstevel@tonic-gate * to see if the csp is still there.
1707c478bd9Sstevel@tonic-gate */
1717c478bd9Sstevel@tonic-gate rw_exit(&csp->s_lock);
1727c478bd9Sstevel@tonic-gate if ((err = ufs_iget(vfsp, shadow, &sip, cr)) != 0) {
1737c478bd9Sstevel@tonic-gate (void) ufs_si_free_mem(sp);
1747c478bd9Sstevel@tonic-gate return (EIO);
1757c478bd9Sstevel@tonic-gate }
1767c478bd9Sstevel@tonic-gate rw_enter(&sip->i_contents, RW_WRITER);
1777c478bd9Sstevel@tonic-gate if ((sip->i_mode & IFMT) != IFSHAD || sip->i_nlink <= 0) {
1787c478bd9Sstevel@tonic-gate rw_exit(&sip->i_contents);
1797c478bd9Sstevel@tonic-gate VN_RELE(ITOV(sip));
1807c478bd9Sstevel@tonic-gate goto loop;
1817c478bd9Sstevel@tonic-gate }
1827c478bd9Sstevel@tonic-gate /* Get the csp again */
1837c478bd9Sstevel@tonic-gate if (si_cachea_get(ip, sp, &csp) != 0) {
1847c478bd9Sstevel@tonic-gate rw_exit(&sip->i_contents);
1857c478bd9Sstevel@tonic-gate VN_RELE(ITOV(sip));
1867c478bd9Sstevel@tonic-gate goto loop;
1877c478bd9Sstevel@tonic-gate }
1887c478bd9Sstevel@tonic-gate ASSERT(RW_WRITE_HELD(&csp->s_lock));
1897c478bd9Sstevel@tonic-gate /* See if we got the right shadow */
1907c478bd9Sstevel@tonic-gate if (csp->s_shadow != shadow) {
1917c478bd9Sstevel@tonic-gate rw_exit(&csp->s_lock);
1927c478bd9Sstevel@tonic-gate rw_exit(&sip->i_contents);
1937c478bd9Sstevel@tonic-gate VN_RELE(ITOV(sip));
1947c478bd9Sstevel@tonic-gate goto loop;
1957c478bd9Sstevel@tonic-gate }
1967c478bd9Sstevel@tonic-gate ASSERT(RW_WRITE_HELD(&sip->i_contents));
1977c478bd9Sstevel@tonic-gate ASSERT(sip->i_dquot == 0);
1987c478bd9Sstevel@tonic-gate /* Increment link count */
1997c478bd9Sstevel@tonic-gate ASSERT(sip->i_nlink > 0);
2007c478bd9Sstevel@tonic-gate sip->i_nlink++;
2017c478bd9Sstevel@tonic-gate TRANS_INODE(ufsvfsp, sip);
2027c478bd9Sstevel@tonic-gate csp->s_use = sip->i_nlink;
2037c478bd9Sstevel@tonic-gate csp->s_ref++;
2047c478bd9Sstevel@tonic-gate ASSERT(sp->s_ref >= 0 && sp->s_ref <= sp->s_use);
2057c478bd9Sstevel@tonic-gate sip->i_flag |= ICHG | IMOD;
2067c478bd9Sstevel@tonic-gate sip->i_seq++;
2077c478bd9Sstevel@tonic-gate ITIMES_NOLOCK(sip);
2087c478bd9Sstevel@tonic-gate /*
2097c478bd9Sstevel@tonic-gate * Always release s_lock before both releasing i_contents
2107c478bd9Sstevel@tonic-gate * and calling VN_RELE.
2117c478bd9Sstevel@tonic-gate */
2127c478bd9Sstevel@tonic-gate rw_exit(&csp->s_lock);
2137c478bd9Sstevel@tonic-gate rw_exit(&sip->i_contents);
2147c478bd9Sstevel@tonic-gate VN_RELE(ITOV(sip));
2157c478bd9Sstevel@tonic-gate (void) ufs_si_free_mem(sp);
2167c478bd9Sstevel@tonic-gate sp = csp;
2177c478bd9Sstevel@tonic-gate si_cachehit++;
2187c478bd9Sstevel@tonic-gate goto switchshadows;
2197c478bd9Sstevel@tonic-gate }
2207c478bd9Sstevel@tonic-gate
2217c478bd9Sstevel@tonic-gate /* Alloc a shadow inode and fill it in */
2227c478bd9Sstevel@tonic-gate err = ufs_ialloc(ip, ip->i_number, (mode_t)IFSHAD, &sip, cr);
2237c478bd9Sstevel@tonic-gate if (err) {
2247c478bd9Sstevel@tonic-gate (void) ufs_si_free_mem(sp);
2257c478bd9Sstevel@tonic-gate return (err);
2267c478bd9Sstevel@tonic-gate }
2277c478bd9Sstevel@tonic-gate rw_enter(&sip->i_contents, RW_WRITER);
2287c478bd9Sstevel@tonic-gate sip->i_flag |= IACC | IUPD | ICHG;
2297c478bd9Sstevel@tonic-gate sip->i_seq++;
2307c478bd9Sstevel@tonic-gate sip->i_mode = (o_mode_t)IFSHAD;
2317c478bd9Sstevel@tonic-gate ITOV(sip)->v_type = VREG;
23252d54943Sbatschul ufs_reset_vnode(ITOV(sip));
2337c478bd9Sstevel@tonic-gate sip->i_nlink = 1;
2347c478bd9Sstevel@tonic-gate sip->i_uid = crgetuid(cr);
2357c478bd9Sstevel@tonic-gate sip->i_suid = (ulong_t)sip->i_uid > (ulong_t)USHRT_MAX ?
2367c478bd9Sstevel@tonic-gate UID_LONG : sip->i_uid;
2377c478bd9Sstevel@tonic-gate sip->i_gid = crgetgid(cr);
2387c478bd9Sstevel@tonic-gate sip->i_sgid = (ulong_t)sip->i_gid > (ulong_t)USHRT_MAX ?
2397c478bd9Sstevel@tonic-gate GID_LONG : sip->i_gid;
2407c478bd9Sstevel@tonic-gate sip->i_shadow = 0;
2417c478bd9Sstevel@tonic-gate TRANS_INODE(ufsvfsp, sip);
2427c478bd9Sstevel@tonic-gate sip->i_ufs_acl = NULL;
2437c478bd9Sstevel@tonic-gate ASSERT(sip->i_size == 0);
2447c478bd9Sstevel@tonic-gate
2457c478bd9Sstevel@tonic-gate sp->s_shadow = sip->i_number;
2467c478bd9Sstevel@tonic-gate
2477c478bd9Sstevel@tonic-gate if ((err = ufs_sectobuf(sp, &acldata, &acldatalen)) != 0)
2487c478bd9Sstevel@tonic-gate goto errout;
2497c478bd9Sstevel@tonic-gate offset = 0;
2507c478bd9Sstevel@tonic-gate
2517c478bd9Sstevel@tonic-gate /*
2527c478bd9Sstevel@tonic-gate * We don't actually care about the residual count upon failure,
2537c478bd9Sstevel@tonic-gate * but giving ufs_rdwri() the pointer means it won't translate
2547c478bd9Sstevel@tonic-gate * all failures to EIO. Our caller needs to know when ENOSPC
2557c478bd9Sstevel@tonic-gate * gets hit.
2567c478bd9Sstevel@tonic-gate */
2577c478bd9Sstevel@tonic-gate resid = 0;
2587c478bd9Sstevel@tonic-gate if (((err = ufs_rdwri(UIO_WRITE, FWRITE|FSYNC, sip, acldata,
2597c478bd9Sstevel@tonic-gate acldatalen, (offset_t)0, UIO_SYSSPACE, &resid, cr)) != 0) ||
2607c478bd9Sstevel@tonic-gate (resid != 0)) {
2617c478bd9Sstevel@tonic-gate kmem_free(acldata, acldatalen);
2627c478bd9Sstevel@tonic-gate if ((resid != 0) && (err == 0))
2637c478bd9Sstevel@tonic-gate err = ENOSPC;
2647c478bd9Sstevel@tonic-gate goto errout;
2657c478bd9Sstevel@tonic-gate }
2667c478bd9Sstevel@tonic-gate
2677c478bd9Sstevel@tonic-gate offset += acldatalen;
2687c478bd9Sstevel@tonic-gate if ((acldatalen + fs->fs_bsize) > ufsvfsp->vfs_maxacl)
2697c478bd9Sstevel@tonic-gate ufsvfsp->vfs_maxacl = acldatalen + fs->fs_bsize;
2707c478bd9Sstevel@tonic-gate
2717c478bd9Sstevel@tonic-gate kmem_free(acldata, acldatalen);
2727c478bd9Sstevel@tonic-gate /* Sync & free the shadow inode */
2737c478bd9Sstevel@tonic-gate ufs_iupdat(sip, 1);
2747c478bd9Sstevel@tonic-gate rw_exit(&sip->i_contents);
2757c478bd9Sstevel@tonic-gate VN_RELE(ITOV(sip));
2767c478bd9Sstevel@tonic-gate
2777c478bd9Sstevel@tonic-gate /* We're committed to using this sp */
2787c478bd9Sstevel@tonic-gate sp->s_use = 1;
2797c478bd9Sstevel@tonic-gate sp->s_ref = 1;
2807c478bd9Sstevel@tonic-gate
2817c478bd9Sstevel@tonic-gate /* Now put the new acl stuff in the cache */
2827c478bd9Sstevel@tonic-gate /* XXX Might make a duplicate */
2837c478bd9Sstevel@tonic-gate si_cache_put(sp);
2847c478bd9Sstevel@tonic-gate si_cachemiss++;
2857c478bd9Sstevel@tonic-gate
2867c478bd9Sstevel@tonic-gate switchshadows:
2877c478bd9Sstevel@tonic-gate /* Now switch the parent inode to use the new shadow inode */
2887c478bd9Sstevel@tonic-gate ASSERT(RW_WRITE_HELD(&ip->i_contents));
2897c478bd9Sstevel@tonic-gate rw_enter(&sp->s_lock, RW_READER);
2907c478bd9Sstevel@tonic-gate oldsp = ip->i_ufs_acl;
2917c478bd9Sstevel@tonic-gate oldshadow = ip->i_shadow;
2927c478bd9Sstevel@tonic-gate ip->i_ufs_acl = sp;
2937c478bd9Sstevel@tonic-gate ASSERT(sp->s_shadow <= INT_MAX);
2947c478bd9Sstevel@tonic-gate ip->i_shadow = (int32_t)sp->s_shadow;
2957c478bd9Sstevel@tonic-gate ASSERT(oldsp != sp);
2967c478bd9Sstevel@tonic-gate ASSERT(oldshadow != ip->i_number);
2977c478bd9Sstevel@tonic-gate ASSERT(ip->i_number != ip->i_shadow);
2987c478bd9Sstevel@tonic-gate /*
2997c478bd9Sstevel@tonic-gate * Change the mode bits to follow the acl list
3007c478bd9Sstevel@tonic-gate *
3017c478bd9Sstevel@tonic-gate * NOTE: a directory is not required to have a "regular" acl
3027c478bd9Sstevel@tonic-gate * bug id's 1238908, 1257173, 1263171 and 1263188
3037c478bd9Sstevel@tonic-gate *
3047c478bd9Sstevel@tonic-gate * but if a "regular" acl is present, it must contain
3057c478bd9Sstevel@tonic-gate * an "owner", "group", and "other" acl
3067c478bd9Sstevel@tonic-gate *
3077c478bd9Sstevel@tonic-gate * If an ACL mask exists, the effective group rights are
3087c478bd9Sstevel@tonic-gate * set to the mask. Otherwise, the effective group rights
3097c478bd9Sstevel@tonic-gate * are set to the object group bits.
3107c478bd9Sstevel@tonic-gate */
3117c478bd9Sstevel@tonic-gate if (sp->aowner) { /* Owner */
3127c478bd9Sstevel@tonic-gate ip->i_mode &= ~0700; /* clear Owner */
3137c478bd9Sstevel@tonic-gate ip->i_mode |= (sp->aowner->acl_ic_perm & 07) << 6;
3147c478bd9Sstevel@tonic-gate ip->i_uid = sp->aowner->acl_ic_who;
3157c478bd9Sstevel@tonic-gate }
3167c478bd9Sstevel@tonic-gate
3177c478bd9Sstevel@tonic-gate if (sp->agroup) { /* Group */
3187c478bd9Sstevel@tonic-gate ip->i_mode &= ~0070; /* clear Group */
3192879e8a4Sprabahar ip->i_mode |= MASK2MODE(sp); /* apply mask */
3207c478bd9Sstevel@tonic-gate ip->i_gid = sp->agroup->acl_ic_who;
3217c478bd9Sstevel@tonic-gate }
3227c478bd9Sstevel@tonic-gate
3237c478bd9Sstevel@tonic-gate if (sp->aother) { /* Other */
3247c478bd9Sstevel@tonic-gate ip->i_mode &= ~0007; /* clear Other */
3257c478bd9Sstevel@tonic-gate ip->i_mode |= (sp->aother->acl_ic_perm & 07);
3267c478bd9Sstevel@tonic-gate }
3277c478bd9Sstevel@tonic-gate
3287c478bd9Sstevel@tonic-gate if (sp->aclass.acl_ismask)
3297c478bd9Sstevel@tonic-gate ip->i_mode = (ip->i_mode & ~070) |
3307c478bd9Sstevel@tonic-gate (((sp->aclass.acl_maskbits & 07) << 3) &
3317c478bd9Sstevel@tonic-gate ip->i_mode);
3327c478bd9Sstevel@tonic-gate
3337c478bd9Sstevel@tonic-gate TRANS_INODE(ufsvfsp, ip);
3347c478bd9Sstevel@tonic-gate rw_exit(&sp->s_lock);
3357c478bd9Sstevel@tonic-gate ip->i_flag |= ICHG;
3367c478bd9Sstevel@tonic-gate ip->i_seq++;
3377c478bd9Sstevel@tonic-gate /*
3387c478bd9Sstevel@tonic-gate * when creating a file there is no need to push the inode, it
3397c478bd9Sstevel@tonic-gate * is pushed later
3407c478bd9Sstevel@tonic-gate */
3417c478bd9Sstevel@tonic-gate if (puship == 1)
3427c478bd9Sstevel@tonic-gate ufs_iupdat(ip, 1);
3437c478bd9Sstevel@tonic-gate
3447c478bd9Sstevel@tonic-gate /*
3457c478bd9Sstevel@tonic-gate * Decrement link count on the old shadow inode,
3467c478bd9Sstevel@tonic-gate * and decrement reference count on the old aclp,
3477c478bd9Sstevel@tonic-gate */
3487c478bd9Sstevel@tonic-gate if (oldshadow) {
3497c478bd9Sstevel@tonic-gate /* Get the shadow inode */
3507c478bd9Sstevel@tonic-gate ASSERT(RW_WRITE_HELD(&ip->i_contents));
3517c478bd9Sstevel@tonic-gate vfsp = ITOV(ip)->v_vfsp;
3527c478bd9Sstevel@tonic-gate if ((err = ufs_iget_alloced(vfsp, oldshadow, &sip, cr)) != 0) {
3537c478bd9Sstevel@tonic-gate return (EIO);
3547c478bd9Sstevel@tonic-gate }
3557c478bd9Sstevel@tonic-gate /* Decrement link count */
3567c478bd9Sstevel@tonic-gate rw_enter(&sip->i_contents, RW_WRITER);
3577c478bd9Sstevel@tonic-gate if (oldsp)
3587c478bd9Sstevel@tonic-gate rw_enter(&oldsp->s_lock, RW_WRITER);
3597c478bd9Sstevel@tonic-gate ASSERT(sip->i_dquot == 0);
3607c478bd9Sstevel@tonic-gate ASSERT(sip->i_nlink > 0);
3617c478bd9Sstevel@tonic-gate usecnt = --sip->i_nlink;
3627c478bd9Sstevel@tonic-gate ufs_setreclaim(sip);
3637c478bd9Sstevel@tonic-gate TRANS_INODE(ufsvfsp, sip);
3647c478bd9Sstevel@tonic-gate sip->i_flag |= ICHG | IMOD;
3657c478bd9Sstevel@tonic-gate sip->i_seq++;
3667c478bd9Sstevel@tonic-gate ITIMES_NOLOCK(sip);
3677c478bd9Sstevel@tonic-gate if (oldsp) {
3687c478bd9Sstevel@tonic-gate oldsp->s_use = usecnt;
3697c478bd9Sstevel@tonic-gate refcnt = --oldsp->s_ref;
3707c478bd9Sstevel@tonic-gate signature = oldsp->s_signature;
3717c478bd9Sstevel@tonic-gate /*
3727c478bd9Sstevel@tonic-gate * Always release s_lock before both releasing
3737c478bd9Sstevel@tonic-gate * i_contents and calling VN_RELE.
3747c478bd9Sstevel@tonic-gate */
3757c478bd9Sstevel@tonic-gate rw_exit(&oldsp->s_lock);
3767c478bd9Sstevel@tonic-gate }
3777c478bd9Sstevel@tonic-gate rw_exit(&sip->i_contents);
3787c478bd9Sstevel@tonic-gate VN_RELE(ITOV(sip));
3797c478bd9Sstevel@tonic-gate if (oldsp && (refcnt == 0))
3807c478bd9Sstevel@tonic-gate si_cache_del(oldsp, signature);
3817c478bd9Sstevel@tonic-gate }
3827c478bd9Sstevel@tonic-gate return (0);
3837c478bd9Sstevel@tonic-gate
3847c478bd9Sstevel@tonic-gate errout:
3857c478bd9Sstevel@tonic-gate /* Throw the newly alloc'd inode away */
3867c478bd9Sstevel@tonic-gate sip->i_nlink = 0;
3877c478bd9Sstevel@tonic-gate ufs_setreclaim(sip);
3887c478bd9Sstevel@tonic-gate TRANS_INODE(ufsvfsp, sip);
3897c478bd9Sstevel@tonic-gate ITIMES_NOLOCK(sip);
3907c478bd9Sstevel@tonic-gate rw_exit(&sip->i_contents);
3917c478bd9Sstevel@tonic-gate VN_RELE(ITOV(sip));
3927c478bd9Sstevel@tonic-gate ASSERT(!sp->s_use && !sp->s_ref && !(sp->s_flags & SI_CACHED));
3937c478bd9Sstevel@tonic-gate (void) ufs_si_free_mem(sp);
3947c478bd9Sstevel@tonic-gate return (err);
3957c478bd9Sstevel@tonic-gate }
3967c478bd9Sstevel@tonic-gate
3977c478bd9Sstevel@tonic-gate /*
3987c478bd9Sstevel@tonic-gate * Load the acls for inode ip either from disk (adding to the cache),
3997c478bd9Sstevel@tonic-gate * or search the cache and attach the cache'd acl list to the ip.
4007c478bd9Sstevel@tonic-gate * In either case, maintain the proper reference count on the cached entry.
4017c478bd9Sstevel@tonic-gate *
4027c478bd9Sstevel@tonic-gate * Parameters:
4037c478bd9Sstevel@tonic-gate * ip - Ptr to the inode which needs the acl list loaded
4047c478bd9Sstevel@tonic-gate * cr - Ptr to credentials
4057c478bd9Sstevel@tonic-gate *
4067c478bd9Sstevel@tonic-gate * Returns: 0 - Success
4077c478bd9Sstevel@tonic-gate * N - From errno.h
4087c478bd9Sstevel@tonic-gate */
4097c478bd9Sstevel@tonic-gate int
ufs_si_load(struct inode * ip,cred_t * cr)4107c478bd9Sstevel@tonic-gate ufs_si_load(struct inode *ip, cred_t *cr)
4117c478bd9Sstevel@tonic-gate /*
4127c478bd9Sstevel@tonic-gate * ip parent inode in
4137c478bd9Sstevel@tonic-gate * cr credentials in
4147c478bd9Sstevel@tonic-gate */
4157c478bd9Sstevel@tonic-gate {
4167c478bd9Sstevel@tonic-gate struct vfs *vfsp;
4177c478bd9Sstevel@tonic-gate struct inode *sip;
4187c478bd9Sstevel@tonic-gate ufs_fsd_t *fsdp;
4197c478bd9Sstevel@tonic-gate si_t *sp;
4207c478bd9Sstevel@tonic-gate vsecattr_t vsecattr = {
4217c478bd9Sstevel@tonic-gate (uint_t)0,
4227c478bd9Sstevel@tonic-gate (int)0,
4237c478bd9Sstevel@tonic-gate (void *)NULL,
4247c478bd9Sstevel@tonic-gate (int)0,
4257c478bd9Sstevel@tonic-gate (void *)NULL};
4267c478bd9Sstevel@tonic-gate aclent_t *aclp;
4277c478bd9Sstevel@tonic-gate ufs_acl_t *ufsaclp;
4287c478bd9Sstevel@tonic-gate caddr_t acldata = NULL;
4297c478bd9Sstevel@tonic-gate ino_t maxino;
4307c478bd9Sstevel@tonic-gate int err;
4317c478bd9Sstevel@tonic-gate size_t acldatalen;
4327c478bd9Sstevel@tonic-gate int numacls;
4337c478bd9Sstevel@tonic-gate int shadow;
4347c478bd9Sstevel@tonic-gate int usecnt;
4357c478bd9Sstevel@tonic-gate struct ufsvfs *ufsvfsp = ip->i_ufsvfs;
4367c478bd9Sstevel@tonic-gate struct fs *fs = ufsvfsp->vfs_fs;
4377c478bd9Sstevel@tonic-gate
4387c478bd9Sstevel@tonic-gate ASSERT(ip != NULL);
4397c478bd9Sstevel@tonic-gate ASSERT(RW_WRITE_HELD(&ip->i_contents));
4407c478bd9Sstevel@tonic-gate ASSERT(ip->i_shadow && ip->i_ufs_acl == NULL);
4417c478bd9Sstevel@tonic-gate ASSERT((ip->i_mode & IFMT) != IFSHAD);
4427c478bd9Sstevel@tonic-gate
4437c478bd9Sstevel@tonic-gate if (!CHECK_ACL_ALLOWED(ip->i_mode & IFMT))
4447c478bd9Sstevel@tonic-gate return (ENOSYS);
4457c478bd9Sstevel@tonic-gate
4467c478bd9Sstevel@tonic-gate if (ip->i_shadow == ip->i_number)
4477c478bd9Sstevel@tonic-gate return (EIO);
4487c478bd9Sstevel@tonic-gate
4497c478bd9Sstevel@tonic-gate maxino = (ino_t)(ITOF(ip)->fs_ncg * ITOF(ip)->fs_ipg);
4507c478bd9Sstevel@tonic-gate if (ip->i_shadow < UFSROOTINO || ip->i_shadow > maxino)
4517c478bd9Sstevel@tonic-gate return (EIO);
4527c478bd9Sstevel@tonic-gate
4537c478bd9Sstevel@tonic-gate /*
4547c478bd9Sstevel@tonic-gate * XXX Check cache. If in cache, link to it and increment
4557c478bd9Sstevel@tonic-gate * the reference count, then return.
4567c478bd9Sstevel@tonic-gate */
4577c478bd9Sstevel@tonic-gate if (si_cachei_get(ip, &sp) == 0) {
4587c478bd9Sstevel@tonic-gate ASSERT(RW_WRITE_HELD(&sp->s_lock));
4597c478bd9Sstevel@tonic-gate ip->i_ufs_acl = sp;
4607c478bd9Sstevel@tonic-gate sp->s_ref++;
4617c478bd9Sstevel@tonic-gate ASSERT(sp->s_ref >= 0 && sp->s_ref <= sp->s_use);
4627c478bd9Sstevel@tonic-gate rw_exit(&sp->s_lock);
4637c478bd9Sstevel@tonic-gate si_cachehit++;
4647c478bd9Sstevel@tonic-gate return (0);
4657c478bd9Sstevel@tonic-gate }
4667c478bd9Sstevel@tonic-gate
4677c478bd9Sstevel@tonic-gate /* Get the shadow inode */
4687c478bd9Sstevel@tonic-gate vfsp = ITOV(ip)->v_vfsp;
4697c478bd9Sstevel@tonic-gate shadow = ip->i_shadow;
4707c478bd9Sstevel@tonic-gate if ((err = ufs_iget_alloced(vfsp, shadow, &sip, cr)) != 0) {
4717c478bd9Sstevel@tonic-gate return (err);
4727c478bd9Sstevel@tonic-gate }
4737c478bd9Sstevel@tonic-gate rw_enter(&sip->i_contents, RW_WRITER);
4747c478bd9Sstevel@tonic-gate
4757c478bd9Sstevel@tonic-gate if ((sip->i_mode & IFMT) != IFSHAD) {
4767c478bd9Sstevel@tonic-gate rw_exit(&sip->i_contents);
4777c478bd9Sstevel@tonic-gate err = EINVAL;
4787c478bd9Sstevel@tonic-gate goto alldone;
4797c478bd9Sstevel@tonic-gate }
4807c478bd9Sstevel@tonic-gate
4817c478bd9Sstevel@tonic-gate ASSERT(sip->i_dquot == 0);
4827c478bd9Sstevel@tonic-gate usecnt = sip->i_nlink;
4837c478bd9Sstevel@tonic-gate if ((!ULOCKFS_IS_NOIACC(&ufsvfsp->vfs_ulockfs)) &&
4847c478bd9Sstevel@tonic-gate (!(sip)->i_ufsvfs->vfs_noatime)) {
4857c478bd9Sstevel@tonic-gate sip->i_flag |= IACC;
4867c478bd9Sstevel@tonic-gate }
4877c478bd9Sstevel@tonic-gate rw_downgrade(&sip->i_contents);
4887c478bd9Sstevel@tonic-gate
4897c478bd9Sstevel@tonic-gate ASSERT(sip->i_size <= MAXOFF_T);
4907c478bd9Sstevel@tonic-gate /* Read the acl's and other stuff from disk */
4917c478bd9Sstevel@tonic-gate acldata = kmem_zalloc((size_t)sip->i_size, KM_SLEEP);
4927c478bd9Sstevel@tonic-gate acldatalen = sip->i_size;
4937c478bd9Sstevel@tonic-gate
4947c478bd9Sstevel@tonic-gate err = ufs_rdwri(UIO_READ, FREAD, sip, acldata, acldatalen, (offset_t)0,
4957c478bd9Sstevel@tonic-gate UIO_SYSSPACE, (int *)0, cr);
4967c478bd9Sstevel@tonic-gate
4977c478bd9Sstevel@tonic-gate rw_exit(&sip->i_contents);
4987c478bd9Sstevel@tonic-gate
4997c478bd9Sstevel@tonic-gate if (err)
5007c478bd9Sstevel@tonic-gate goto alldone;
5017c478bd9Sstevel@tonic-gate
5027c478bd9Sstevel@tonic-gate /*
5037c478bd9Sstevel@tonic-gate * Convert from disk format
5047c478bd9Sstevel@tonic-gate * Result is a vsecattr struct which we then convert to the
5057c478bd9Sstevel@tonic-gate * si struct.
5067c478bd9Sstevel@tonic-gate */
5077c478bd9Sstevel@tonic-gate bzero((caddr_t)&vsecattr, sizeof (vsecattr_t));
5087c478bd9Sstevel@tonic-gate for (fsdp = (ufs_fsd_t *)acldata;
5097c478bd9Sstevel@tonic-gate fsdp < (ufs_fsd_t *)(acldata + acldatalen);
5107c478bd9Sstevel@tonic-gate fsdp = (ufs_fsd_t *)((caddr_t)fsdp +
5117c478bd9Sstevel@tonic-gate FSD_RECSZ(fsdp, fsdp->fsd_size))) {
5127c478bd9Sstevel@tonic-gate if (fsdp->fsd_size <= 0)
5137c478bd9Sstevel@tonic-gate break;
5147c478bd9Sstevel@tonic-gate switch (fsdp->fsd_type) {
5157c478bd9Sstevel@tonic-gate case FSD_ACL:
5167c478bd9Sstevel@tonic-gate numacls = vsecattr.vsa_aclcnt =
5177c478bd9Sstevel@tonic-gate (int)((fsdp->fsd_size - 2 * sizeof (int)) /
5187c478bd9Sstevel@tonic-gate sizeof (ufs_acl_t));
5197c478bd9Sstevel@tonic-gate aclp = vsecattr.vsa_aclentp =
5207c478bd9Sstevel@tonic-gate kmem_zalloc(numacls * sizeof (aclent_t), KM_SLEEP);
5217c478bd9Sstevel@tonic-gate for (ufsaclp = (ufs_acl_t *)fsdp->fsd_data;
5227c478bd9Sstevel@tonic-gate numacls; ufsaclp++) {
5237c478bd9Sstevel@tonic-gate aclp->a_type = ufsaclp->acl_tag;
5247c478bd9Sstevel@tonic-gate aclp->a_id = ufsaclp->acl_who;
5257c478bd9Sstevel@tonic-gate aclp->a_perm = ufsaclp->acl_perm;
5267c478bd9Sstevel@tonic-gate aclp++;
5277c478bd9Sstevel@tonic-gate numacls--;
5287c478bd9Sstevel@tonic-gate }
5297c478bd9Sstevel@tonic-gate break;
5307c478bd9Sstevel@tonic-gate case FSD_DFACL:
5317c478bd9Sstevel@tonic-gate numacls = vsecattr.vsa_dfaclcnt =
5327c478bd9Sstevel@tonic-gate (int)((fsdp->fsd_size - 2 * sizeof (int)) /
5337c478bd9Sstevel@tonic-gate sizeof (ufs_acl_t));
5347c478bd9Sstevel@tonic-gate aclp = vsecattr.vsa_dfaclentp =
5357c478bd9Sstevel@tonic-gate kmem_zalloc(numacls * sizeof (aclent_t), KM_SLEEP);
5367c478bd9Sstevel@tonic-gate for (ufsaclp = (ufs_acl_t *)fsdp->fsd_data;
5377c478bd9Sstevel@tonic-gate numacls; ufsaclp++) {
5387c478bd9Sstevel@tonic-gate aclp->a_type = ufsaclp->acl_tag;
5397c478bd9Sstevel@tonic-gate aclp->a_id = ufsaclp->acl_who;
5407c478bd9Sstevel@tonic-gate aclp->a_perm = ufsaclp->acl_perm;
5417c478bd9Sstevel@tonic-gate aclp++;
5427c478bd9Sstevel@tonic-gate numacls--;
5437c478bd9Sstevel@tonic-gate }
5447c478bd9Sstevel@tonic-gate break;
5457c478bd9Sstevel@tonic-gate }
5467c478bd9Sstevel@tonic-gate }
5477c478bd9Sstevel@tonic-gate /* Sort the lists */
5487c478bd9Sstevel@tonic-gate if (vsecattr.vsa_aclentp) {
5497c478bd9Sstevel@tonic-gate ksort((caddr_t)vsecattr.vsa_aclentp, vsecattr.vsa_aclcnt,
5507c478bd9Sstevel@tonic-gate sizeof (aclent_t), cmp2acls);
5517c478bd9Sstevel@tonic-gate if ((err = acl_validate(vsecattr.vsa_aclentp,
5527c478bd9Sstevel@tonic-gate vsecattr.vsa_aclcnt, ACL_CHECK)) != 0) {
5537c478bd9Sstevel@tonic-gate goto alldone;
5547c478bd9Sstevel@tonic-gate }
5557c478bd9Sstevel@tonic-gate }
5567c478bd9Sstevel@tonic-gate if (vsecattr.vsa_dfaclentp) {
5577c478bd9Sstevel@tonic-gate ksort((caddr_t)vsecattr.vsa_dfaclentp, vsecattr.vsa_dfaclcnt,
5587c478bd9Sstevel@tonic-gate sizeof (aclent_t), cmp2acls);
5597c478bd9Sstevel@tonic-gate if ((err = acl_validate(vsecattr.vsa_dfaclentp,
5607c478bd9Sstevel@tonic-gate vsecattr.vsa_dfaclcnt, DEF_ACL_CHECK)) != 0) {
5617c478bd9Sstevel@tonic-gate goto alldone;
5627c478bd9Sstevel@tonic-gate }
5637c478bd9Sstevel@tonic-gate }
5647c478bd9Sstevel@tonic-gate
5657c478bd9Sstevel@tonic-gate /* ignore shadow inodes without ACLs */
5667c478bd9Sstevel@tonic-gate if (!vsecattr.vsa_aclentp && !vsecattr.vsa_dfaclentp) {
5677c478bd9Sstevel@tonic-gate err = 0;
5687c478bd9Sstevel@tonic-gate goto alldone;
5697c478bd9Sstevel@tonic-gate }
5707c478bd9Sstevel@tonic-gate
5717c478bd9Sstevel@tonic-gate /* Convert from vsecattr struct to ufs_acl_entry struct */
5727c478bd9Sstevel@tonic-gate if ((err = vsecattr2aclentry(&vsecattr, &sp)) != 0) {
5737c478bd9Sstevel@tonic-gate goto alldone;
5747c478bd9Sstevel@tonic-gate }
5757c478bd9Sstevel@tonic-gate
5767c478bd9Sstevel@tonic-gate /* There aren't filled in by vsecattr2aclentry */
5777c478bd9Sstevel@tonic-gate sp->s_shadow = ip->i_shadow;
5787c478bd9Sstevel@tonic-gate sp->s_dev = ip->i_dev;
5797c478bd9Sstevel@tonic-gate sp->s_use = usecnt;
5807c478bd9Sstevel@tonic-gate sp->s_ref = 1;
5817c478bd9Sstevel@tonic-gate ASSERT(sp->s_ref >= 0 && sp->s_ref <= sp->s_use);
5827c478bd9Sstevel@tonic-gate
5837c478bd9Sstevel@tonic-gate /* XXX Might make a duplicate */
5847c478bd9Sstevel@tonic-gate si_cache_put(sp);
5857c478bd9Sstevel@tonic-gate
5867c478bd9Sstevel@tonic-gate /* Signal anyone waiting on this shadow to be loaded */
5877c478bd9Sstevel@tonic-gate ip->i_ufs_acl = sp;
5887c478bd9Sstevel@tonic-gate err = 0;
5897c478bd9Sstevel@tonic-gate si_cachemiss++;
5907c478bd9Sstevel@tonic-gate if ((acldatalen + fs->fs_bsize) > ufsvfsp->vfs_maxacl)
5917c478bd9Sstevel@tonic-gate ufsvfsp->vfs_maxacl = acldatalen + fs->fs_bsize;
5927c478bd9Sstevel@tonic-gate alldone:
5937c478bd9Sstevel@tonic-gate /*
5947c478bd9Sstevel@tonic-gate * Common exit point. Mark shadow inode as ISTALE
5957c478bd9Sstevel@tonic-gate * if we detect an internal inconsistency, to
5967c478bd9Sstevel@tonic-gate * prevent stray inodes appearing in the cache.
5977c478bd9Sstevel@tonic-gate */
5987c478bd9Sstevel@tonic-gate if (err) {
5997c478bd9Sstevel@tonic-gate rw_enter(&sip->i_contents, RW_READER);
6007c478bd9Sstevel@tonic-gate mutex_enter(&sip->i_tlock);
6017c478bd9Sstevel@tonic-gate sip->i_flag |= ISTALE;
6027c478bd9Sstevel@tonic-gate mutex_exit(&sip->i_tlock);
6037c478bd9Sstevel@tonic-gate rw_exit(&sip->i_contents);
6047c478bd9Sstevel@tonic-gate }
6057c478bd9Sstevel@tonic-gate VN_RELE(ITOV(sip));
6067c478bd9Sstevel@tonic-gate
6077c478bd9Sstevel@tonic-gate /*
6087c478bd9Sstevel@tonic-gate * Cleanup of data structures allocated
6097c478bd9Sstevel@tonic-gate * on the fly.
6107c478bd9Sstevel@tonic-gate */
6117c478bd9Sstevel@tonic-gate if (acldata)
6127c478bd9Sstevel@tonic-gate kmem_free(acldata, acldatalen);
6137c478bd9Sstevel@tonic-gate
6147c478bd9Sstevel@tonic-gate if (vsecattr.vsa_aclentp)
6157c478bd9Sstevel@tonic-gate kmem_free(vsecattr.vsa_aclentp,
6167c478bd9Sstevel@tonic-gate vsecattr.vsa_aclcnt * sizeof (aclent_t));
6177c478bd9Sstevel@tonic-gate if (vsecattr.vsa_dfaclentp)
6187c478bd9Sstevel@tonic-gate kmem_free(vsecattr.vsa_dfaclentp,
6197c478bd9Sstevel@tonic-gate vsecattr.vsa_dfaclcnt * sizeof (aclent_t));
6207c478bd9Sstevel@tonic-gate return (err);
6217c478bd9Sstevel@tonic-gate }
6227c478bd9Sstevel@tonic-gate
6237c478bd9Sstevel@tonic-gate /*
6247c478bd9Sstevel@tonic-gate * Check the inode's ACL's to see if this mode of access is
6257c478bd9Sstevel@tonic-gate * allowed; return 0 if allowed, EACCES if not.
6267c478bd9Sstevel@tonic-gate *
6277c478bd9Sstevel@tonic-gate * We follow the procedure defined in Sec. 3.3.5, ACL Access
6287c478bd9Sstevel@tonic-gate * Check Algorithm, of the POSIX 1003.6 Draft Standard.
6297c478bd9Sstevel@tonic-gate */
6307c478bd9Sstevel@tonic-gate int
ufs_acl_access(struct inode * ip,int mode,cred_t * cr)6317c478bd9Sstevel@tonic-gate ufs_acl_access(struct inode *ip, int mode, cred_t *cr)
6327c478bd9Sstevel@tonic-gate /*
6337c478bd9Sstevel@tonic-gate * ip parent inode
6347c478bd9Sstevel@tonic-gate * mode mode of access read, write, execute/examine
6357c478bd9Sstevel@tonic-gate * cr credentials
6367c478bd9Sstevel@tonic-gate */
6377c478bd9Sstevel@tonic-gate {
6387c478bd9Sstevel@tonic-gate ufs_ic_acl_t *acl;
6397c478bd9Sstevel@tonic-gate int ismask, mask = 0;
6407c478bd9Sstevel@tonic-gate int gperm = 0;
6417c478bd9Sstevel@tonic-gate int ngroup = 0;
6427c478bd9Sstevel@tonic-gate si_t *sp = NULL;
6437c478bd9Sstevel@tonic-gate uid_t uid = crgetuid(cr);
6447c478bd9Sstevel@tonic-gate uid_t owner;
6457c478bd9Sstevel@tonic-gate
6467c478bd9Sstevel@tonic-gate ASSERT(ip->i_ufs_acl != NULL);
647*60c8e821SFrank Batschulat ASSERT(RW_LOCK_HELD(&ip->i_contents));
6487c478bd9Sstevel@tonic-gate
6497c478bd9Sstevel@tonic-gate sp = ip->i_ufs_acl;
6507c478bd9Sstevel@tonic-gate
6517c478bd9Sstevel@tonic-gate ismask = sp->aclass.acl_ismask ?
6527c478bd9Sstevel@tonic-gate sp->aclass.acl_ismask : NULL;
6537c478bd9Sstevel@tonic-gate
6547c478bd9Sstevel@tonic-gate if (ismask)
6557c478bd9Sstevel@tonic-gate mask = sp->aclass.acl_maskbits;
6567c478bd9Sstevel@tonic-gate else
6577c478bd9Sstevel@tonic-gate mask = -1;
6587c478bd9Sstevel@tonic-gate
6597c478bd9Sstevel@tonic-gate /*
6607c478bd9Sstevel@tonic-gate * (1) If user owns the file, obey user mode bits
6617c478bd9Sstevel@tonic-gate */
6627c478bd9Sstevel@tonic-gate owner = sp->aowner->acl_ic_who;
6637c478bd9Sstevel@tonic-gate if (uid == owner) {
6647c478bd9Sstevel@tonic-gate return (MODE_CHECK(owner, mode, (sp->aowner->acl_ic_perm << 6),
6657c478bd9Sstevel@tonic-gate cr, ip));
6667c478bd9Sstevel@tonic-gate }
6677c478bd9Sstevel@tonic-gate
6687c478bd9Sstevel@tonic-gate /*
6697c478bd9Sstevel@tonic-gate * (2) Obey any matching ACL_USER entry
6707c478bd9Sstevel@tonic-gate */
6717c478bd9Sstevel@tonic-gate if (sp->ausers)
6727c478bd9Sstevel@tonic-gate for (acl = sp->ausers; acl != NULL; acl = acl->acl_ic_next) {
6737c478bd9Sstevel@tonic-gate if (acl->acl_ic_who == uid) {
6747c478bd9Sstevel@tonic-gate return (MODE_CHECK(owner, mode,
6757c478bd9Sstevel@tonic-gate (mask & acl->acl_ic_perm) << 6, cr, ip));
6767c478bd9Sstevel@tonic-gate }
6777c478bd9Sstevel@tonic-gate }
6787c478bd9Sstevel@tonic-gate
6797c478bd9Sstevel@tonic-gate /*
6807c478bd9Sstevel@tonic-gate * (3) If user belongs to file's group, obey group mode bits
6817c478bd9Sstevel@tonic-gate * if no ACL mask is defined; if there is an ACL mask, we look
6827c478bd9Sstevel@tonic-gate * at both the group mode bits and any ACL_GROUP entries.
6837c478bd9Sstevel@tonic-gate */
6847c478bd9Sstevel@tonic-gate if (groupmember((uid_t)sp->agroup->acl_ic_who, cr)) {
6857c478bd9Sstevel@tonic-gate ngroup++;
6867c478bd9Sstevel@tonic-gate gperm = (sp->agroup->acl_ic_perm);
6877c478bd9Sstevel@tonic-gate if (!ismask)
6887c478bd9Sstevel@tonic-gate return (MODE_CHECK(owner, mode, (gperm << 6), cr, ip));
6897c478bd9Sstevel@tonic-gate }
6907c478bd9Sstevel@tonic-gate
6917c478bd9Sstevel@tonic-gate /*
6927c478bd9Sstevel@tonic-gate * (4) Accumulate the permissions in matching ACL_GROUP entries
6937c478bd9Sstevel@tonic-gate */
6947c478bd9Sstevel@tonic-gate if (sp->agroups)
6957c478bd9Sstevel@tonic-gate for (acl = sp->agroups; acl != NULL; acl = acl->acl_ic_next)
6967c478bd9Sstevel@tonic-gate {
6977c478bd9Sstevel@tonic-gate if (groupmember(acl->acl_ic_who, cr)) {
6987c478bd9Sstevel@tonic-gate ngroup++;
6997c478bd9Sstevel@tonic-gate gperm |= acl->acl_ic_perm;
7007c478bd9Sstevel@tonic-gate }
7017c478bd9Sstevel@tonic-gate }
7027c478bd9Sstevel@tonic-gate
7037c478bd9Sstevel@tonic-gate if (ngroup != 0)
7047c478bd9Sstevel@tonic-gate return (MODE_CHECK(owner, mode, ((gperm & mask) << 6), cr, ip));
7057c478bd9Sstevel@tonic-gate
7067c478bd9Sstevel@tonic-gate /*
7077c478bd9Sstevel@tonic-gate * (5) Finally, use the "other" mode bits
7087c478bd9Sstevel@tonic-gate */
7097c478bd9Sstevel@tonic-gate return (MODE_CHECK(owner, mode, sp->aother->acl_ic_perm << 6, cr, ip));
7107c478bd9Sstevel@tonic-gate }
7117c478bd9Sstevel@tonic-gate
7127c478bd9Sstevel@tonic-gate /*ARGSUSED2*/
7137c478bd9Sstevel@tonic-gate int
ufs_acl_get(struct inode * ip,vsecattr_t * vsap,int flag,cred_t * cr)7147c478bd9Sstevel@tonic-gate ufs_acl_get(struct inode *ip, vsecattr_t *vsap, int flag, cred_t *cr)
7157c478bd9Sstevel@tonic-gate {
7167c478bd9Sstevel@tonic-gate aclent_t *aclentp;
7177c478bd9Sstevel@tonic-gate
7187c478bd9Sstevel@tonic-gate ASSERT(RW_LOCK_HELD(&ip->i_contents));
7197c478bd9Sstevel@tonic-gate
7207c478bd9Sstevel@tonic-gate /* XXX Range check, sanity check, shadow check */
7217c478bd9Sstevel@tonic-gate /* If an ACL is present, get the data from the shadow inode info */
7227c478bd9Sstevel@tonic-gate if (ip->i_ufs_acl)
7237c478bd9Sstevel@tonic-gate return (aclentry2vsecattr(ip->i_ufs_acl, vsap));
7247c478bd9Sstevel@tonic-gate
7257c478bd9Sstevel@tonic-gate /*
7267c478bd9Sstevel@tonic-gate * If no ACLs are present, fabricate one from the mode bits.
7277c478bd9Sstevel@tonic-gate * This code is almost identical to fs_fab_acl(), but we
7287c478bd9Sstevel@tonic-gate * already have the mode bits handy, so we'll avoid going
7297c478bd9Sstevel@tonic-gate * through VOP_GETATTR() again.
7307c478bd9Sstevel@tonic-gate */
7317c478bd9Sstevel@tonic-gate
7327c478bd9Sstevel@tonic-gate vsap->vsa_aclcnt = 0;
7337c478bd9Sstevel@tonic-gate vsap->vsa_aclentp = NULL;
7347c478bd9Sstevel@tonic-gate vsap->vsa_dfaclcnt = 0; /* Default ACLs are not fabricated */
7357c478bd9Sstevel@tonic-gate vsap->vsa_dfaclentp = NULL;
7367c478bd9Sstevel@tonic-gate
7377c478bd9Sstevel@tonic-gate if (vsap->vsa_mask & (VSA_ACLCNT | VSA_ACL))
7387c478bd9Sstevel@tonic-gate vsap->vsa_aclcnt = 4; /* USER, GROUP, OTHER, and CLASS */
7397c478bd9Sstevel@tonic-gate
7407c478bd9Sstevel@tonic-gate if (vsap->vsa_mask & VSA_ACL) {
7417c478bd9Sstevel@tonic-gate vsap->vsa_aclentp = kmem_zalloc(4 * sizeof (aclent_t),
7427c478bd9Sstevel@tonic-gate KM_SLEEP);
7437c478bd9Sstevel@tonic-gate if (vsap->vsa_aclentp == NULL)
7447c478bd9Sstevel@tonic-gate return (ENOMEM);
7457c478bd9Sstevel@tonic-gate aclentp = vsap->vsa_aclentp;
7467c478bd9Sstevel@tonic-gate
7477c478bd9Sstevel@tonic-gate /* Owner */
7487c478bd9Sstevel@tonic-gate aclentp->a_type = USER_OBJ;
7497c478bd9Sstevel@tonic-gate aclentp->a_perm = ((ushort_t)(ip->i_mode & 0700)) >> 6;
7507c478bd9Sstevel@tonic-gate aclentp->a_id = ip->i_uid; /* Really undefined */
7517c478bd9Sstevel@tonic-gate aclentp++;
7527c478bd9Sstevel@tonic-gate
7537c478bd9Sstevel@tonic-gate /* Group */
7547c478bd9Sstevel@tonic-gate aclentp->a_type = GROUP_OBJ;
7557c478bd9Sstevel@tonic-gate aclentp->a_perm = ((ushort_t)(ip->i_mode & 0070)) >> 3;
7567c478bd9Sstevel@tonic-gate aclentp->a_id = ip->i_gid; /* Really undefined */
7577c478bd9Sstevel@tonic-gate aclentp++;
7587c478bd9Sstevel@tonic-gate
7597c478bd9Sstevel@tonic-gate /* Other */
7607c478bd9Sstevel@tonic-gate aclentp->a_type = OTHER_OBJ;
7617c478bd9Sstevel@tonic-gate aclentp->a_perm = ip->i_mode & 0007;
7627c478bd9Sstevel@tonic-gate aclentp->a_id = 0; /* Really undefined */
7637c478bd9Sstevel@tonic-gate aclentp++;
7647c478bd9Sstevel@tonic-gate
7657c478bd9Sstevel@tonic-gate /* Class */
7667c478bd9Sstevel@tonic-gate aclentp->a_type = CLASS_OBJ;
7677c478bd9Sstevel@tonic-gate aclentp->a_perm = ((ushort_t)(ip->i_mode & 0070)) >> 3;
7687c478bd9Sstevel@tonic-gate aclentp->a_id = 0; /* Really undefined */
7697c478bd9Sstevel@tonic-gate ksort((caddr_t)vsap->vsa_aclentp, vsap->vsa_aclcnt,
7707c478bd9Sstevel@tonic-gate sizeof (aclent_t), cmp2acls);
7717c478bd9Sstevel@tonic-gate }
7727c478bd9Sstevel@tonic-gate
7737c478bd9Sstevel@tonic-gate return (0);
7747c478bd9Sstevel@tonic-gate }
7757c478bd9Sstevel@tonic-gate
7767c478bd9Sstevel@tonic-gate /*ARGSUSED2*/
7777c478bd9Sstevel@tonic-gate int
ufs_acl_set(struct inode * ip,vsecattr_t * vsap,int flag,cred_t * cr)7787c478bd9Sstevel@tonic-gate ufs_acl_set(struct inode *ip, vsecattr_t *vsap, int flag, cred_t *cr)
7797c478bd9Sstevel@tonic-gate {
7807c478bd9Sstevel@tonic-gate si_t *sp;
7817c478bd9Sstevel@tonic-gate int err;
7827c478bd9Sstevel@tonic-gate
7837c478bd9Sstevel@tonic-gate ASSERT(RW_WRITE_HELD(&ip->i_contents));
7847c478bd9Sstevel@tonic-gate
7857c478bd9Sstevel@tonic-gate if (!CHECK_ACL_ALLOWED(ip->i_mode & IFMT))
7867c478bd9Sstevel@tonic-gate return (ENOSYS);
7877c478bd9Sstevel@tonic-gate
7887c478bd9Sstevel@tonic-gate /*
7897c478bd9Sstevel@tonic-gate * only the owner of the file or privileged users can change the ACLs
7907c478bd9Sstevel@tonic-gate */
7917c478bd9Sstevel@tonic-gate if (secpolicy_vnode_setdac(cr, ip->i_uid) != 0)
7927c478bd9Sstevel@tonic-gate return (EPERM);
7937c478bd9Sstevel@tonic-gate
7947c478bd9Sstevel@tonic-gate /* Convert from vsecattr struct to ufs_acl_entry struct */
7957c478bd9Sstevel@tonic-gate if ((err = vsecattr2aclentry(vsap, &sp)) != 0)
7967c478bd9Sstevel@tonic-gate return (err);
7977c478bd9Sstevel@tonic-gate sp->s_dev = ip->i_dev;
7987c478bd9Sstevel@tonic-gate
7997c478bd9Sstevel@tonic-gate /*
8007c478bd9Sstevel@tonic-gate * Make the user & group objs in the acl list follow what's
8017c478bd9Sstevel@tonic-gate * in the inode.
8027c478bd9Sstevel@tonic-gate */
8037c478bd9Sstevel@tonic-gate #ifdef DEBUG
8047c478bd9Sstevel@tonic-gate if (vsap->vsa_mask == VSA_ACL) {
8057c478bd9Sstevel@tonic-gate ASSERT(sp->aowner);
8067c478bd9Sstevel@tonic-gate ASSERT(sp->agroup);
8077c478bd9Sstevel@tonic-gate ASSERT(sp->aother);
8087c478bd9Sstevel@tonic-gate }
8097c478bd9Sstevel@tonic-gate #endif /* DEBUG */
8107c478bd9Sstevel@tonic-gate
8117c478bd9Sstevel@tonic-gate if (sp->aowner)
8127c478bd9Sstevel@tonic-gate sp->aowner->acl_ic_who = ip->i_uid;
8137c478bd9Sstevel@tonic-gate if (sp->agroup)
8147c478bd9Sstevel@tonic-gate sp->agroup->acl_ic_who = ip->i_gid;
8157c478bd9Sstevel@tonic-gate
8167c478bd9Sstevel@tonic-gate /*
8177c478bd9Sstevel@tonic-gate * Write and cache the new acl list
8187c478bd9Sstevel@tonic-gate */
8197c478bd9Sstevel@tonic-gate err = ufs_si_store(ip, sp, 1, cr);
8207c478bd9Sstevel@tonic-gate
8217c478bd9Sstevel@tonic-gate return (err);
8227c478bd9Sstevel@tonic-gate }
8237c478bd9Sstevel@tonic-gate
8247c478bd9Sstevel@tonic-gate /*
8257c478bd9Sstevel@tonic-gate * XXX Scan sorted array of acl's, checking for:
8267c478bd9Sstevel@tonic-gate * 1) Any duplicate/conflicting entries (same type and id)
8277c478bd9Sstevel@tonic-gate * 2) More than 1 of USER_OBJ, GROUP_OBJ, OTHER_OBJ, CLASS_OBJ
8287c478bd9Sstevel@tonic-gate * 3) More than 1 of DEF_USER_OBJ, DEF_GROUP_OBJ, DEF_OTHER_OBJ, DEF_CLASS_OBJ
8297c478bd9Sstevel@tonic-gate *
8307c478bd9Sstevel@tonic-gate * Parameters:
8317c478bd9Sstevel@tonic-gate * aclentp - ptr to sorted list of acl entries.
8327c478bd9Sstevel@tonic-gate * nentries - # acl entries on the list
8337c478bd9Sstevel@tonic-gate * flag - Bitmap (ACL_CHECK and/or DEF_ACL_CHECK) indicating whether the
8347c478bd9Sstevel@tonic-gate * list contains regular acls, default acls, or both.
8357c478bd9Sstevel@tonic-gate *
8367c478bd9Sstevel@tonic-gate * Returns: 0 - Success
8377c478bd9Sstevel@tonic-gate * EINVAL - Invalid list (dups or multiple entries of type USER_OBJ, etc)
8387c478bd9Sstevel@tonic-gate */
8397c478bd9Sstevel@tonic-gate static int
acl_validate(aclent_t * aclentp,int nentries,int flag)8407c478bd9Sstevel@tonic-gate acl_validate(aclent_t *aclentp, int nentries, int flag)
8417c478bd9Sstevel@tonic-gate {
8427c478bd9Sstevel@tonic-gate int i;
8437c478bd9Sstevel@tonic-gate int nuser_objs = 0;
8447c478bd9Sstevel@tonic-gate int ngroup_objs = 0;
8457c478bd9Sstevel@tonic-gate int nother_objs = 0;
8467c478bd9Sstevel@tonic-gate int nclass_objs = 0;
8477c478bd9Sstevel@tonic-gate int ndef_user_objs = 0;
8487c478bd9Sstevel@tonic-gate int ndef_group_objs = 0;
8497c478bd9Sstevel@tonic-gate int ndef_other_objs = 0;
8507c478bd9Sstevel@tonic-gate int ndef_class_objs = 0;
8517c478bd9Sstevel@tonic-gate int nusers = 0;
8527c478bd9Sstevel@tonic-gate int ngroups = 0;
8537c478bd9Sstevel@tonic-gate int ndef_users = 0;
8547c478bd9Sstevel@tonic-gate int ndef_groups = 0;
8557c478bd9Sstevel@tonic-gate int numdefs = 0;
8567c478bd9Sstevel@tonic-gate
8577c478bd9Sstevel@tonic-gate /* Null list or list of one */
8587c478bd9Sstevel@tonic-gate if (aclentp == NULL)
8597c478bd9Sstevel@tonic-gate return (0);
8607c478bd9Sstevel@tonic-gate
8617c478bd9Sstevel@tonic-gate if (nentries <= 0)
8627c478bd9Sstevel@tonic-gate return (EINVAL);
8637c478bd9Sstevel@tonic-gate
8647c478bd9Sstevel@tonic-gate for (i = 1; i < nentries; i++) {
8657c478bd9Sstevel@tonic-gate if (((aclentp[i - 1].a_type == aclentp[i].a_type) &&
8667c478bd9Sstevel@tonic-gate (aclentp[i - 1].a_id == aclentp[i].a_id)) ||
8677c478bd9Sstevel@tonic-gate (aclentp[i - 1].a_perm > 07)) {
8687c478bd9Sstevel@tonic-gate return (EINVAL);
8697c478bd9Sstevel@tonic-gate }
8707c478bd9Sstevel@tonic-gate }
8717c478bd9Sstevel@tonic-gate
8727c478bd9Sstevel@tonic-gate if (flag == 0 || (flag != ACL_CHECK && flag != DEF_ACL_CHECK))
8737c478bd9Sstevel@tonic-gate return (EINVAL);
8747c478bd9Sstevel@tonic-gate
8757c478bd9Sstevel@tonic-gate /* Count types */
8767c478bd9Sstevel@tonic-gate for (i = 0; i < nentries; i++) {
8777c478bd9Sstevel@tonic-gate switch (aclentp[i].a_type) {
8787c478bd9Sstevel@tonic-gate case USER_OBJ: /* Owner */
8797c478bd9Sstevel@tonic-gate nuser_objs++;
8807c478bd9Sstevel@tonic-gate break;
8817c478bd9Sstevel@tonic-gate case GROUP_OBJ: /* Group */
8827c478bd9Sstevel@tonic-gate ngroup_objs++;
8837c478bd9Sstevel@tonic-gate break;
8847c478bd9Sstevel@tonic-gate case OTHER_OBJ: /* Other */
8857c478bd9Sstevel@tonic-gate nother_objs++;
8867c478bd9Sstevel@tonic-gate break;
8877c478bd9Sstevel@tonic-gate case CLASS_OBJ: /* Mask */
8887c478bd9Sstevel@tonic-gate nclass_objs++;
8897c478bd9Sstevel@tonic-gate break;
8907c478bd9Sstevel@tonic-gate case DEF_USER_OBJ: /* Default Owner */
8917c478bd9Sstevel@tonic-gate ndef_user_objs++;
8927c478bd9Sstevel@tonic-gate break;
8937c478bd9Sstevel@tonic-gate case DEF_GROUP_OBJ: /* Default Group */
8947c478bd9Sstevel@tonic-gate ndef_group_objs++;
8957c478bd9Sstevel@tonic-gate break;
8967c478bd9Sstevel@tonic-gate case DEF_OTHER_OBJ: /* Default Other */
8977c478bd9Sstevel@tonic-gate ndef_other_objs++;
8987c478bd9Sstevel@tonic-gate break;
8997c478bd9Sstevel@tonic-gate case DEF_CLASS_OBJ: /* Default Mask */
9007c478bd9Sstevel@tonic-gate ndef_class_objs++;
9017c478bd9Sstevel@tonic-gate break;
9027c478bd9Sstevel@tonic-gate case USER: /* Users */
9037c478bd9Sstevel@tonic-gate nusers++;
9047c478bd9Sstevel@tonic-gate break;
9057c478bd9Sstevel@tonic-gate case GROUP: /* Groups */
9067c478bd9Sstevel@tonic-gate ngroups++;
9077c478bd9Sstevel@tonic-gate break;
9087c478bd9Sstevel@tonic-gate case DEF_USER: /* Default Users */
9097c478bd9Sstevel@tonic-gate ndef_users++;
9107c478bd9Sstevel@tonic-gate break;
9117c478bd9Sstevel@tonic-gate case DEF_GROUP: /* Default Groups */
9127c478bd9Sstevel@tonic-gate ndef_groups++;
9137c478bd9Sstevel@tonic-gate break;
9147c478bd9Sstevel@tonic-gate default: /* Unknown type */
9157c478bd9Sstevel@tonic-gate return (EINVAL);
9167c478bd9Sstevel@tonic-gate }
9177c478bd9Sstevel@tonic-gate }
9187c478bd9Sstevel@tonic-gate
9197c478bd9Sstevel@tonic-gate /*
9207c478bd9Sstevel@tonic-gate * For normal acl's, we require there be one (and only one)
9217c478bd9Sstevel@tonic-gate * USER_OBJ, GROUP_OBJ and OTHER_OBJ. There is either zero
9227c478bd9Sstevel@tonic-gate * or one CLASS_OBJ.
9237c478bd9Sstevel@tonic-gate */
9247c478bd9Sstevel@tonic-gate if (flag & ACL_CHECK) {
9257c478bd9Sstevel@tonic-gate if (nuser_objs != 1 || ngroup_objs != 1 ||
9267c478bd9Sstevel@tonic-gate nother_objs != 1 || nclass_objs > 1) {
9277c478bd9Sstevel@tonic-gate return (EINVAL);
9287c478bd9Sstevel@tonic-gate }
9297c478bd9Sstevel@tonic-gate /*
9307c478bd9Sstevel@tonic-gate * If there are ANY group acls, there MUST be a
9317c478bd9Sstevel@tonic-gate * class_obj(mask) acl (1003.6/D12 p. 29 lines 75-80).
9327c478bd9Sstevel@tonic-gate */
9337c478bd9Sstevel@tonic-gate if (ngroups && !nclass_objs) {
9347c478bd9Sstevel@tonic-gate return (EINVAL);
9357c478bd9Sstevel@tonic-gate }
9367c478bd9Sstevel@tonic-gate if (nuser_objs + ngroup_objs + nother_objs + nclass_objs +
9377c478bd9Sstevel@tonic-gate ngroups + nusers > MAX_ACL_ENTRIES)
9387c478bd9Sstevel@tonic-gate return (EINVAL);
9397c478bd9Sstevel@tonic-gate }
9407c478bd9Sstevel@tonic-gate
9417c478bd9Sstevel@tonic-gate /*
9427c478bd9Sstevel@tonic-gate * For default acl's, we require that there be either one (and only one)
9437c478bd9Sstevel@tonic-gate * DEF_USER_OBJ, DEF_GROUP_OBJ and DEF_OTHER_OBJ
9447c478bd9Sstevel@tonic-gate * or there be none of them.
9457c478bd9Sstevel@tonic-gate */
9467c478bd9Sstevel@tonic-gate if (flag & DEF_ACL_CHECK) {
9477c478bd9Sstevel@tonic-gate if (ndef_other_objs > 1 || ndef_user_objs > 1 ||
9487c478bd9Sstevel@tonic-gate ndef_group_objs > 1 || ndef_class_objs > 1) {
9497c478bd9Sstevel@tonic-gate return (EINVAL);
9507c478bd9Sstevel@tonic-gate }
9517c478bd9Sstevel@tonic-gate
9527c478bd9Sstevel@tonic-gate numdefs = ndef_other_objs + ndef_user_objs + ndef_group_objs;
9537c478bd9Sstevel@tonic-gate
9547c478bd9Sstevel@tonic-gate if (numdefs != 0 && numdefs != 3) {
9557c478bd9Sstevel@tonic-gate return (EINVAL);
9567c478bd9Sstevel@tonic-gate }
9577c478bd9Sstevel@tonic-gate /*
9587c478bd9Sstevel@tonic-gate * If there are ANY def_group acls, there MUST be a
9597c478bd9Sstevel@tonic-gate * def_class_obj(mask) acl (1003.6/D12 P. 29 lines 75-80).
9607c478bd9Sstevel@tonic-gate * XXX(jimh) This is inferred.
9617c478bd9Sstevel@tonic-gate */
9627c478bd9Sstevel@tonic-gate if (ndef_groups && !ndef_class_objs) {
9637c478bd9Sstevel@tonic-gate return (EINVAL);
9647c478bd9Sstevel@tonic-gate }
9657c478bd9Sstevel@tonic-gate if ((ndef_users || ndef_groups) &&
9667c478bd9Sstevel@tonic-gate ((numdefs != 3) && !ndef_class_objs)) {
9677c478bd9Sstevel@tonic-gate return (EINVAL);
9687c478bd9Sstevel@tonic-gate }
9697c478bd9Sstevel@tonic-gate if (ndef_user_objs + ndef_group_objs + ndef_other_objs +
9707c478bd9Sstevel@tonic-gate ndef_class_objs + ndef_users + ndef_groups >
9717c478bd9Sstevel@tonic-gate MAX_ACL_ENTRIES)
9727c478bd9Sstevel@tonic-gate return (EINVAL);
9737c478bd9Sstevel@tonic-gate }
9747c478bd9Sstevel@tonic-gate return (0);
9757c478bd9Sstevel@tonic-gate }
9767c478bd9Sstevel@tonic-gate
9777c478bd9Sstevel@tonic-gate static int
formacl(ufs_ic_acl_t ** aclpp,aclent_t * aclentp)9787c478bd9Sstevel@tonic-gate formacl(ufs_ic_acl_t **aclpp, aclent_t *aclentp)
9797c478bd9Sstevel@tonic-gate {
9807c478bd9Sstevel@tonic-gate ufs_ic_acl_t *uaclp;
9817c478bd9Sstevel@tonic-gate
9827c478bd9Sstevel@tonic-gate uaclp = kmem_alloc(sizeof (ufs_ic_acl_t), KM_SLEEP);
9837c478bd9Sstevel@tonic-gate uaclp->acl_ic_perm = aclentp->a_perm;
9847c478bd9Sstevel@tonic-gate uaclp->acl_ic_who = aclentp->a_id;
9857c478bd9Sstevel@tonic-gate uaclp->acl_ic_next = *aclpp;
9867c478bd9Sstevel@tonic-gate *aclpp = uaclp;
9877c478bd9Sstevel@tonic-gate return (0);
9887c478bd9Sstevel@tonic-gate }
9897c478bd9Sstevel@tonic-gate
9907c478bd9Sstevel@tonic-gate /*
9917c478bd9Sstevel@tonic-gate * XXX - Make more efficient
9927c478bd9Sstevel@tonic-gate * Convert from the vsecattr struct, used by the VOP interface, to
9937c478bd9Sstevel@tonic-gate * the ufs_acl_entry struct used for in-core storage of acl's.
9947c478bd9Sstevel@tonic-gate *
9957c478bd9Sstevel@tonic-gate * Parameters:
9967c478bd9Sstevel@tonic-gate * vsap - Ptr to array of security attributes.
9977c478bd9Sstevel@tonic-gate * spp - Ptr to ptr to si struct for the results
9987c478bd9Sstevel@tonic-gate *
9997c478bd9Sstevel@tonic-gate * Returns: 0 - Success
10007c478bd9Sstevel@tonic-gate * N - From errno.h
10017c478bd9Sstevel@tonic-gate */
10027c478bd9Sstevel@tonic-gate static int
vsecattr2aclentry(vsecattr_t * vsap,si_t ** spp)10037c478bd9Sstevel@tonic-gate vsecattr2aclentry(vsecattr_t *vsap, si_t **spp)
10047c478bd9Sstevel@tonic-gate {
10057c478bd9Sstevel@tonic-gate aclent_t *aclentp, *aclp;
10067c478bd9Sstevel@tonic-gate si_t *sp;
10077c478bd9Sstevel@tonic-gate int err;
10087c478bd9Sstevel@tonic-gate int i;
10097c478bd9Sstevel@tonic-gate
10107c478bd9Sstevel@tonic-gate /* Sort & validate the lists on the vsap */
10117c478bd9Sstevel@tonic-gate ksort((caddr_t)vsap->vsa_aclentp, vsap->vsa_aclcnt,
10127c478bd9Sstevel@tonic-gate sizeof (aclent_t), cmp2acls);
10137c478bd9Sstevel@tonic-gate ksort((caddr_t)vsap->vsa_dfaclentp, vsap->vsa_dfaclcnt,
10147c478bd9Sstevel@tonic-gate sizeof (aclent_t), cmp2acls);
10157c478bd9Sstevel@tonic-gate if ((err = acl_validate(vsap->vsa_aclentp,
10167c478bd9Sstevel@tonic-gate vsap->vsa_aclcnt, ACL_CHECK)) != 0)
10177c478bd9Sstevel@tonic-gate return (err);
10187c478bd9Sstevel@tonic-gate if ((err = acl_validate(vsap->vsa_dfaclentp,
10197c478bd9Sstevel@tonic-gate vsap->vsa_dfaclcnt, DEF_ACL_CHECK)) != 0)
10207c478bd9Sstevel@tonic-gate return (err);
10217c478bd9Sstevel@tonic-gate
10227c478bd9Sstevel@tonic-gate /* Create new si struct and hang acl's off it */
10237c478bd9Sstevel@tonic-gate sp = kmem_zalloc(sizeof (si_t), KM_SLEEP);
10247c478bd9Sstevel@tonic-gate rw_init(&sp->s_lock, NULL, RW_DEFAULT, NULL);
10257c478bd9Sstevel@tonic-gate
10267c478bd9Sstevel@tonic-gate /* Process acl list */
10277c478bd9Sstevel@tonic-gate aclp = (aclent_t *)vsap->vsa_aclentp;
10287c478bd9Sstevel@tonic-gate aclentp = aclp + vsap->vsa_aclcnt - 1;
10297c478bd9Sstevel@tonic-gate for (i = 0; i < vsap->vsa_aclcnt; i++) {
10307c478bd9Sstevel@tonic-gate switch (aclentp->a_type) {
10317c478bd9Sstevel@tonic-gate case USER_OBJ: /* Owner */
10327c478bd9Sstevel@tonic-gate if (err = formacl(&sp->aowner, aclentp))
10337c478bd9Sstevel@tonic-gate goto error;
10347c478bd9Sstevel@tonic-gate break;
10357c478bd9Sstevel@tonic-gate case GROUP_OBJ: /* Group */
10367c478bd9Sstevel@tonic-gate if (err = formacl(&sp->agroup, aclentp))
10377c478bd9Sstevel@tonic-gate goto error;
10387c478bd9Sstevel@tonic-gate break;
10397c478bd9Sstevel@tonic-gate case OTHER_OBJ: /* Other */
10407c478bd9Sstevel@tonic-gate if (err = formacl(&sp->aother, aclentp))
10417c478bd9Sstevel@tonic-gate goto error;
10427c478bd9Sstevel@tonic-gate break;
10437c478bd9Sstevel@tonic-gate case USER:
10447c478bd9Sstevel@tonic-gate if (err = formacl(&sp->ausers, aclentp))
10457c478bd9Sstevel@tonic-gate goto error;
10467c478bd9Sstevel@tonic-gate break;
10477c478bd9Sstevel@tonic-gate case CLASS_OBJ: /* Mask */
10487c478bd9Sstevel@tonic-gate sp->aclass.acl_ismask = 1;
10497c478bd9Sstevel@tonic-gate sp->aclass.acl_maskbits = aclentp->a_perm;
10507c478bd9Sstevel@tonic-gate break;
10517c478bd9Sstevel@tonic-gate case GROUP:
10527c478bd9Sstevel@tonic-gate if (err = formacl(&sp->agroups, aclentp))
10537c478bd9Sstevel@tonic-gate goto error;
10547c478bd9Sstevel@tonic-gate break;
10557c478bd9Sstevel@tonic-gate default:
10567c478bd9Sstevel@tonic-gate break;
10577c478bd9Sstevel@tonic-gate }
10587c478bd9Sstevel@tonic-gate aclentp--;
10597c478bd9Sstevel@tonic-gate }
10607c478bd9Sstevel@tonic-gate
10617c478bd9Sstevel@tonic-gate /* Process default acl list */
10627c478bd9Sstevel@tonic-gate aclp = (aclent_t *)vsap->vsa_dfaclentp;
10637c478bd9Sstevel@tonic-gate aclentp = aclp + vsap->vsa_dfaclcnt - 1;
10647c478bd9Sstevel@tonic-gate for (i = 0; i < vsap->vsa_dfaclcnt; i++) {
10657c478bd9Sstevel@tonic-gate switch (aclentp->a_type) {
10667c478bd9Sstevel@tonic-gate case DEF_USER_OBJ: /* Default Owner */
10677c478bd9Sstevel@tonic-gate if (err = formacl(&sp->downer, aclentp))
10687c478bd9Sstevel@tonic-gate goto error;
10697c478bd9Sstevel@tonic-gate break;
10707c478bd9Sstevel@tonic-gate case DEF_GROUP_OBJ: /* Default Group */
10717c478bd9Sstevel@tonic-gate if (err = formacl(&sp->dgroup, aclentp))
10727c478bd9Sstevel@tonic-gate goto error;
10737c478bd9Sstevel@tonic-gate break;
10747c478bd9Sstevel@tonic-gate case DEF_OTHER_OBJ: /* Default Other */
10757c478bd9Sstevel@tonic-gate if (err = formacl(&sp->dother, aclentp))
10767c478bd9Sstevel@tonic-gate goto error;
10777c478bd9Sstevel@tonic-gate break;
10787c478bd9Sstevel@tonic-gate case DEF_USER:
10797c478bd9Sstevel@tonic-gate if (err = formacl(&sp->dusers, aclentp))
10807c478bd9Sstevel@tonic-gate goto error;
10817c478bd9Sstevel@tonic-gate break;
10827c478bd9Sstevel@tonic-gate case DEF_CLASS_OBJ: /* Default Mask */
10837c478bd9Sstevel@tonic-gate sp->dclass.acl_ismask = 1;
10847c478bd9Sstevel@tonic-gate sp->dclass.acl_maskbits = aclentp->a_perm;
10857c478bd9Sstevel@tonic-gate break;
10867c478bd9Sstevel@tonic-gate case DEF_GROUP:
10877c478bd9Sstevel@tonic-gate if (err = formacl(&sp->dgroups, aclentp))
10887c478bd9Sstevel@tonic-gate goto error;
10897c478bd9Sstevel@tonic-gate break;
10907c478bd9Sstevel@tonic-gate default:
10917c478bd9Sstevel@tonic-gate break;
10927c478bd9Sstevel@tonic-gate }
10937c478bd9Sstevel@tonic-gate aclentp--;
10947c478bd9Sstevel@tonic-gate }
10957c478bd9Sstevel@tonic-gate *spp = sp;
10967c478bd9Sstevel@tonic-gate return (0);
10977c478bd9Sstevel@tonic-gate
10987c478bd9Sstevel@tonic-gate error:
10997c478bd9Sstevel@tonic-gate ufs_si_free_mem(sp);
11007c478bd9Sstevel@tonic-gate return (err);
11017c478bd9Sstevel@tonic-gate }
11027c478bd9Sstevel@tonic-gate
11037c478bd9Sstevel@tonic-gate void
formvsec(int obj_type,ufs_ic_acl_t * aclp,aclent_t ** aclentpp)11047c478bd9Sstevel@tonic-gate formvsec(int obj_type, ufs_ic_acl_t *aclp, aclent_t **aclentpp)
11057c478bd9Sstevel@tonic-gate {
11067c478bd9Sstevel@tonic-gate for (; aclp; aclp = aclp->acl_ic_next) {
11077c478bd9Sstevel@tonic-gate (*aclentpp)->a_type = obj_type;
11087c478bd9Sstevel@tonic-gate (*aclentpp)->a_perm = aclp->acl_ic_perm;
11097c478bd9Sstevel@tonic-gate (*aclentpp)->a_id = aclp->acl_ic_who;
11107c478bd9Sstevel@tonic-gate (*aclentpp)++;
11117c478bd9Sstevel@tonic-gate }
11127c478bd9Sstevel@tonic-gate }
11137c478bd9Sstevel@tonic-gate
11147c478bd9Sstevel@tonic-gate /*
11157c478bd9Sstevel@tonic-gate * XXX - Make more efficient
11167c478bd9Sstevel@tonic-gate * Convert from the ufs_acl_entry struct used for in-core storage of acl's
11177c478bd9Sstevel@tonic-gate * to the vsecattr struct, used by the VOP interface.
11187c478bd9Sstevel@tonic-gate *
11197c478bd9Sstevel@tonic-gate * Parameters:
11207c478bd9Sstevel@tonic-gate * sp - Ptr to si struct with the acls
11217c478bd9Sstevel@tonic-gate * vsap - Ptr to a vsecattr struct which will take the results.
11227c478bd9Sstevel@tonic-gate *
11237c478bd9Sstevel@tonic-gate * Returns: 0 - Success
11247c478bd9Sstevel@tonic-gate * N - From errno table
11257c478bd9Sstevel@tonic-gate */
11267c478bd9Sstevel@tonic-gate static int
aclentry2vsecattr(si_t * sp,vsecattr_t * vsap)11277c478bd9Sstevel@tonic-gate aclentry2vsecattr(si_t *sp, vsecattr_t *vsap)
11287c478bd9Sstevel@tonic-gate {
11297c478bd9Sstevel@tonic-gate aclent_t *aclentp;
11307c478bd9Sstevel@tonic-gate int numacls = 0;
11317c478bd9Sstevel@tonic-gate int err;
11327c478bd9Sstevel@tonic-gate
11337c478bd9Sstevel@tonic-gate vsap->vsa_aclentp = vsap->vsa_dfaclentp = NULL;
11347c478bd9Sstevel@tonic-gate
11357c478bd9Sstevel@tonic-gate numacls = acl_count(sp->aowner) +
11367c478bd9Sstevel@tonic-gate acl_count(sp->agroup) +
11377c478bd9Sstevel@tonic-gate acl_count(sp->aother) +
11387c478bd9Sstevel@tonic-gate acl_count(sp->ausers) +
11397c478bd9Sstevel@tonic-gate acl_count(sp->agroups);
11407c478bd9Sstevel@tonic-gate if (sp->aclass.acl_ismask)
11417c478bd9Sstevel@tonic-gate numacls++;
11427c478bd9Sstevel@tonic-gate
11437c478bd9Sstevel@tonic-gate if (vsap->vsa_mask & (VSA_ACLCNT | VSA_ACL))
11447c478bd9Sstevel@tonic-gate vsap->vsa_aclcnt = numacls;
11457c478bd9Sstevel@tonic-gate
114606c5a6e8Sjr26306 if (numacls == 0)
114706c5a6e8Sjr26306 goto do_defaults;
114806c5a6e8Sjr26306
11497c478bd9Sstevel@tonic-gate if (vsap->vsa_mask & VSA_ACL) {
11507c478bd9Sstevel@tonic-gate vsap->vsa_aclentp = kmem_zalloc(numacls * sizeof (aclent_t),
11517c478bd9Sstevel@tonic-gate KM_SLEEP);
11527c478bd9Sstevel@tonic-gate aclentp = vsap->vsa_aclentp;
11537c478bd9Sstevel@tonic-gate
11547c478bd9Sstevel@tonic-gate formvsec(USER_OBJ, sp->aowner, &aclentp);
11557c478bd9Sstevel@tonic-gate formvsec(USER, sp->ausers, &aclentp);
11567c478bd9Sstevel@tonic-gate formvsec(GROUP_OBJ, sp->agroup, &aclentp);
11577c478bd9Sstevel@tonic-gate formvsec(GROUP, sp->agroups, &aclentp);
11587c478bd9Sstevel@tonic-gate formvsec(OTHER_OBJ, sp->aother, &aclentp);
11597c478bd9Sstevel@tonic-gate
11607c478bd9Sstevel@tonic-gate if (sp->aclass.acl_ismask) {
11617c478bd9Sstevel@tonic-gate aclentp->a_type = CLASS_OBJ; /* Mask */
11627c478bd9Sstevel@tonic-gate aclentp->a_perm = sp->aclass.acl_maskbits;
11637c478bd9Sstevel@tonic-gate aclentp->a_id = 0;
11647c478bd9Sstevel@tonic-gate aclentp++;
11657c478bd9Sstevel@tonic-gate }
11667c478bd9Sstevel@tonic-gate
11677c478bd9Sstevel@tonic-gate /* Sort the acl list */
11687c478bd9Sstevel@tonic-gate ksort((caddr_t)vsap->vsa_aclentp, vsap->vsa_aclcnt,
11697c478bd9Sstevel@tonic-gate sizeof (aclent_t), cmp2acls);
11707c478bd9Sstevel@tonic-gate /* Check the acl list */
11717c478bd9Sstevel@tonic-gate if ((err = acl_validate(vsap->vsa_aclentp,
11727c478bd9Sstevel@tonic-gate vsap->vsa_aclcnt, ACL_CHECK)) != 0) {
117380d34432Sfrankho kmem_free(vsap->vsa_aclentp,
117480d34432Sfrankho numacls * sizeof (aclent_t));
11757c478bd9Sstevel@tonic-gate vsap->vsa_aclentp = NULL;
11767c478bd9Sstevel@tonic-gate return (err);
11777c478bd9Sstevel@tonic-gate }
11787c478bd9Sstevel@tonic-gate
11797c478bd9Sstevel@tonic-gate }
11807c478bd9Sstevel@tonic-gate do_defaults:
11817c478bd9Sstevel@tonic-gate /* Process Defaults */
11827c478bd9Sstevel@tonic-gate
11837c478bd9Sstevel@tonic-gate numacls = acl_count(sp->downer) +
11847c478bd9Sstevel@tonic-gate acl_count(sp->dgroup) +
11857c478bd9Sstevel@tonic-gate acl_count(sp->dother) +
11867c478bd9Sstevel@tonic-gate acl_count(sp->dusers) +
11877c478bd9Sstevel@tonic-gate acl_count(sp->dgroups);
11887c478bd9Sstevel@tonic-gate if (sp->dclass.acl_ismask)
11897c478bd9Sstevel@tonic-gate numacls++;
11907c478bd9Sstevel@tonic-gate
11917c478bd9Sstevel@tonic-gate if (vsap->vsa_mask & (VSA_DFACLCNT | VSA_DFACL))
11927c478bd9Sstevel@tonic-gate vsap->vsa_dfaclcnt = numacls;
11937c478bd9Sstevel@tonic-gate
119406c5a6e8Sjr26306 if (numacls == 0)
119506c5a6e8Sjr26306 goto do_others;
119606c5a6e8Sjr26306
11977c478bd9Sstevel@tonic-gate if (vsap->vsa_mask & VSA_DFACL) {
119880d34432Sfrankho vsap->vsa_dfaclentp =
119980d34432Sfrankho kmem_zalloc(numacls * sizeof (aclent_t), KM_SLEEP);
12007c478bd9Sstevel@tonic-gate aclentp = vsap->vsa_dfaclentp;
12017c478bd9Sstevel@tonic-gate formvsec(DEF_USER_OBJ, sp->downer, &aclentp);
12027c478bd9Sstevel@tonic-gate formvsec(DEF_USER, sp->dusers, &aclentp);
12037c478bd9Sstevel@tonic-gate formvsec(DEF_GROUP_OBJ, sp->dgroup, &aclentp);
12047c478bd9Sstevel@tonic-gate formvsec(DEF_GROUP, sp->dgroups, &aclentp);
12057c478bd9Sstevel@tonic-gate formvsec(DEF_OTHER_OBJ, sp->dother, &aclentp);
12067c478bd9Sstevel@tonic-gate
12077c478bd9Sstevel@tonic-gate if (sp->dclass.acl_ismask) {
12087c478bd9Sstevel@tonic-gate aclentp->a_type = DEF_CLASS_OBJ; /* Mask */
12097c478bd9Sstevel@tonic-gate aclentp->a_perm = sp->dclass.acl_maskbits;
12107c478bd9Sstevel@tonic-gate aclentp->a_id = 0;
12117c478bd9Sstevel@tonic-gate aclentp++;
12127c478bd9Sstevel@tonic-gate }
12137c478bd9Sstevel@tonic-gate
12147c478bd9Sstevel@tonic-gate /* Sort the default acl list */
12157c478bd9Sstevel@tonic-gate ksort((caddr_t)vsap->vsa_dfaclentp, vsap->vsa_dfaclcnt,
12167c478bd9Sstevel@tonic-gate sizeof (aclent_t), cmp2acls);
12177c478bd9Sstevel@tonic-gate if ((err = acl_validate(vsap->vsa_dfaclentp,
12187c478bd9Sstevel@tonic-gate vsap->vsa_dfaclcnt, DEF_ACL_CHECK)) != 0) {
12197c478bd9Sstevel@tonic-gate if (vsap->vsa_aclentp != NULL)
12207c478bd9Sstevel@tonic-gate kmem_free(vsap->vsa_aclentp,
12217c478bd9Sstevel@tonic-gate vsap->vsa_aclcnt * sizeof (aclent_t));
12227c478bd9Sstevel@tonic-gate kmem_free(vsap->vsa_dfaclentp,
12237c478bd9Sstevel@tonic-gate vsap->vsa_dfaclcnt * sizeof (aclent_t));
12247c478bd9Sstevel@tonic-gate vsap->vsa_aclentp = vsap->vsa_dfaclentp = NULL;
12257c478bd9Sstevel@tonic-gate return (err);
12267c478bd9Sstevel@tonic-gate }
12277c478bd9Sstevel@tonic-gate }
12287c478bd9Sstevel@tonic-gate
12297c478bd9Sstevel@tonic-gate do_others:
12307c478bd9Sstevel@tonic-gate return (0);
12317c478bd9Sstevel@tonic-gate }
12327c478bd9Sstevel@tonic-gate
12337c478bd9Sstevel@tonic-gate static void
acl_free(ufs_ic_acl_t * aclp)12347c478bd9Sstevel@tonic-gate acl_free(ufs_ic_acl_t *aclp)
12357c478bd9Sstevel@tonic-gate {
12367c478bd9Sstevel@tonic-gate while (aclp != NULL) {
12377c478bd9Sstevel@tonic-gate ufs_ic_acl_t *nextaclp = aclp->acl_ic_next;
12387c478bd9Sstevel@tonic-gate kmem_free(aclp, sizeof (ufs_ic_acl_t));
12397c478bd9Sstevel@tonic-gate aclp = nextaclp;
12407c478bd9Sstevel@tonic-gate }
12417c478bd9Sstevel@tonic-gate }
12427c478bd9Sstevel@tonic-gate
12437c478bd9Sstevel@tonic-gate /*
12447c478bd9Sstevel@tonic-gate * ufs_si_free_mem will discard the sp, and the acl hanging off of the
12457c478bd9Sstevel@tonic-gate * sp. It is required that the sp not be locked, and not be in the
12467c478bd9Sstevel@tonic-gate * cache.
12477c478bd9Sstevel@tonic-gate *
12487c478bd9Sstevel@tonic-gate * input: pointer to sp to discard.
12497c478bd9Sstevel@tonic-gate *
12507c478bd9Sstevel@tonic-gate * return - nothing.
12517c478bd9Sstevel@tonic-gate *
12527c478bd9Sstevel@tonic-gate */
12537c478bd9Sstevel@tonic-gate static void
ufs_si_free_mem(si_t * sp)12547c478bd9Sstevel@tonic-gate ufs_si_free_mem(si_t *sp)
12557c478bd9Sstevel@tonic-gate {
12567c478bd9Sstevel@tonic-gate ASSERT(!(sp->s_flags & SI_CACHED));
12577c478bd9Sstevel@tonic-gate ASSERT(!RW_LOCK_HELD(&sp->s_lock));
12587c478bd9Sstevel@tonic-gate /*
12597c478bd9Sstevel@tonic-gate * remove from the cache
12607c478bd9Sstevel@tonic-gate * free the acl entries
12617c478bd9Sstevel@tonic-gate */
12627c478bd9Sstevel@tonic-gate acl_free(sp->aowner);
12637c478bd9Sstevel@tonic-gate acl_free(sp->agroup);
12647c478bd9Sstevel@tonic-gate acl_free(sp->aother);
12657c478bd9Sstevel@tonic-gate acl_free(sp->ausers);
12667c478bd9Sstevel@tonic-gate acl_free(sp->agroups);
12677c478bd9Sstevel@tonic-gate
12687c478bd9Sstevel@tonic-gate acl_free(sp->downer);
12697c478bd9Sstevel@tonic-gate acl_free(sp->dgroup);
12707c478bd9Sstevel@tonic-gate acl_free(sp->dother);
12717c478bd9Sstevel@tonic-gate acl_free(sp->dusers);
12727c478bd9Sstevel@tonic-gate acl_free(sp->dgroups);
12737c478bd9Sstevel@tonic-gate
12747c478bd9Sstevel@tonic-gate rw_destroy(&sp->s_lock);
12757c478bd9Sstevel@tonic-gate kmem_free(sp, sizeof (si_t));
12767c478bd9Sstevel@tonic-gate }
12777c478bd9Sstevel@tonic-gate
12787c478bd9Sstevel@tonic-gate void
acl_cpy(ufs_ic_acl_t * saclp,ufs_ic_acl_t * daclp)12797c478bd9Sstevel@tonic-gate acl_cpy(ufs_ic_acl_t *saclp, ufs_ic_acl_t *daclp)
12807c478bd9Sstevel@tonic-gate {
12817c478bd9Sstevel@tonic-gate ufs_ic_acl_t *aclp, *prev_aclp = NULL, *aclp1;
12827c478bd9Sstevel@tonic-gate
12837c478bd9Sstevel@tonic-gate if (saclp == NULL) {
12847c478bd9Sstevel@tonic-gate daclp = NULL;
12857c478bd9Sstevel@tonic-gate return;
12867c478bd9Sstevel@tonic-gate }
12877c478bd9Sstevel@tonic-gate prev_aclp = daclp;
12887c478bd9Sstevel@tonic-gate
12897c478bd9Sstevel@tonic-gate for (aclp = saclp; aclp != NULL; aclp = aclp->acl_ic_next) {
12907c478bd9Sstevel@tonic-gate aclp1 = kmem_alloc(sizeof (ufs_ic_acl_t), KM_SLEEP);
12917c478bd9Sstevel@tonic-gate aclp1->acl_ic_next = NULL;
12927c478bd9Sstevel@tonic-gate aclp1->acl_ic_who = aclp->acl_ic_who;
12937c478bd9Sstevel@tonic-gate aclp1->acl_ic_perm = aclp->acl_ic_perm;
12947c478bd9Sstevel@tonic-gate prev_aclp->acl_ic_next = aclp1;
12957c478bd9Sstevel@tonic-gate prev_aclp = (ufs_ic_acl_t *)&aclp1->acl_ic_next;
12967c478bd9Sstevel@tonic-gate }
12977c478bd9Sstevel@tonic-gate }
12987c478bd9Sstevel@tonic-gate
12997c478bd9Sstevel@tonic-gate /*
13007c478bd9Sstevel@tonic-gate * ufs_si_inherit takes a parent acl structure (saclp) and the inode
13017c478bd9Sstevel@tonic-gate * of the object that is inheriting an acl and returns the inode
13027c478bd9Sstevel@tonic-gate * with the acl linked to it. It also writes the acl to disk if
13037c478bd9Sstevel@tonic-gate * it is a unique inode.
13047c478bd9Sstevel@tonic-gate *
13057c478bd9Sstevel@tonic-gate * ip - pointer to inode of object inheriting the acl (contents lock)
13067c478bd9Sstevel@tonic-gate * tdp - parent inode (rw_lock and contents lock)
13077c478bd9Sstevel@tonic-gate * mode - creation modes
13087c478bd9Sstevel@tonic-gate * cr - credentials pointer
13097c478bd9Sstevel@tonic-gate */
13107c478bd9Sstevel@tonic-gate int
ufs_si_inherit(struct inode * ip,struct inode * tdp,o_mode_t mode,cred_t * cr)13117c478bd9Sstevel@tonic-gate ufs_si_inherit(struct inode *ip, struct inode *tdp, o_mode_t mode, cred_t *cr)
13127c478bd9Sstevel@tonic-gate {
13137c478bd9Sstevel@tonic-gate si_t *tsp, *sp = tdp->i_ufs_acl;
13147c478bd9Sstevel@tonic-gate int error;
13157c478bd9Sstevel@tonic-gate o_mode_t old_modes, old_uid, old_gid;
13167c478bd9Sstevel@tonic-gate int mask;
13177c478bd9Sstevel@tonic-gate
13187c478bd9Sstevel@tonic-gate ASSERT(RW_WRITE_HELD(&ip->i_contents));
13197c478bd9Sstevel@tonic-gate ASSERT(RW_WRITE_HELD(&tdp->i_rwlock));
13207c478bd9Sstevel@tonic-gate ASSERT(RW_WRITE_HELD(&tdp->i_contents));
13217c478bd9Sstevel@tonic-gate
13227c478bd9Sstevel@tonic-gate /*
13237c478bd9Sstevel@tonic-gate * if links/symbolic links, or other invalid acl objects are copied
13247c478bd9Sstevel@tonic-gate * or moved to a directory with a default acl do not allow inheritance
13257c478bd9Sstevel@tonic-gate * just return.
13267c478bd9Sstevel@tonic-gate */
13277c478bd9Sstevel@tonic-gate if (!CHECK_ACL_ALLOWED(ip->i_mode & IFMT))
13287c478bd9Sstevel@tonic-gate return (0);
13297c478bd9Sstevel@tonic-gate
13307c478bd9Sstevel@tonic-gate /* lock the parent security information */
13317c478bd9Sstevel@tonic-gate rw_enter(&sp->s_lock, RW_READER);
13327c478bd9Sstevel@tonic-gate
13337c478bd9Sstevel@tonic-gate ASSERT(((tdp->i_mode & IFMT) == IFDIR) ||
13347c478bd9Sstevel@tonic-gate ((tdp->i_mode & IFMT) == IFATTRDIR));
13357c478bd9Sstevel@tonic-gate
13367c478bd9Sstevel@tonic-gate mask = ((sp->downer != NULL) ? 1 : 0) |
13377c478bd9Sstevel@tonic-gate ((sp->dgroup != NULL) ? 2 : 0) |
13387c478bd9Sstevel@tonic-gate ((sp->dother != NULL) ? 4 : 0);
13397c478bd9Sstevel@tonic-gate
13407c478bd9Sstevel@tonic-gate if (mask == 0) {
13417c478bd9Sstevel@tonic-gate rw_exit(&sp->s_lock);
13427c478bd9Sstevel@tonic-gate return (0);
13437c478bd9Sstevel@tonic-gate }
13447c478bd9Sstevel@tonic-gate
13457c478bd9Sstevel@tonic-gate if (mask != 7) {
13467c478bd9Sstevel@tonic-gate rw_exit(&sp->s_lock);
13477c478bd9Sstevel@tonic-gate return (EINVAL);
13487c478bd9Sstevel@tonic-gate }
13497c478bd9Sstevel@tonic-gate
13507c478bd9Sstevel@tonic-gate tsp = kmem_zalloc(sizeof (si_t), KM_SLEEP);
13517c478bd9Sstevel@tonic-gate rw_init(&tsp->s_lock, NULL, RW_DEFAULT, NULL);
13527c478bd9Sstevel@tonic-gate
13537c478bd9Sstevel@tonic-gate /* copy the default acls */
13547c478bd9Sstevel@tonic-gate
13557c478bd9Sstevel@tonic-gate ASSERT(RW_READ_HELD(&sp->s_lock));
13567c478bd9Sstevel@tonic-gate acl_cpy(sp->downer, (ufs_ic_acl_t *)&tsp->aowner);
13577c478bd9Sstevel@tonic-gate acl_cpy(sp->dgroup, (ufs_ic_acl_t *)&tsp->agroup);
13587c478bd9Sstevel@tonic-gate acl_cpy(sp->dother, (ufs_ic_acl_t *)&tsp->aother);
13597c478bd9Sstevel@tonic-gate acl_cpy(sp->dusers, (ufs_ic_acl_t *)&tsp->ausers);
13607c478bd9Sstevel@tonic-gate acl_cpy(sp->dgroups, (ufs_ic_acl_t *)&tsp->agroups);
13617c478bd9Sstevel@tonic-gate tsp->aclass.acl_ismask = sp->dclass.acl_ismask;
13627c478bd9Sstevel@tonic-gate tsp->aclass.acl_maskbits = sp->dclass.acl_maskbits;
13637c478bd9Sstevel@tonic-gate
13647c478bd9Sstevel@tonic-gate /*
13657c478bd9Sstevel@tonic-gate * set the owner, group, and other values from the master
13667c478bd9Sstevel@tonic-gate * inode.
13677c478bd9Sstevel@tonic-gate */
13687c478bd9Sstevel@tonic-gate
13697c478bd9Sstevel@tonic-gate MODE2ACL(tsp->aowner, (mode >> 6), ip->i_uid);
13707c478bd9Sstevel@tonic-gate MODE2ACL(tsp->agroup, (mode >> 3), ip->i_gid);
13717c478bd9Sstevel@tonic-gate MODE2ACL(tsp->aother, (mode), 0);
13727c478bd9Sstevel@tonic-gate
13737c478bd9Sstevel@tonic-gate if (tsp->aclass.acl_ismask) {
13747c478bd9Sstevel@tonic-gate tsp->aclass.acl_maskbits &= mode >> 3;
13757c478bd9Sstevel@tonic-gate }
13767c478bd9Sstevel@tonic-gate
13777c478bd9Sstevel@tonic-gate
13787c478bd9Sstevel@tonic-gate /* copy default acl if necessary */
13797c478bd9Sstevel@tonic-gate
13807c478bd9Sstevel@tonic-gate if (((ip->i_mode & IFMT) == IFDIR) ||
13817c478bd9Sstevel@tonic-gate ((ip->i_mode & IFMT) == IFATTRDIR)) {
13827c478bd9Sstevel@tonic-gate acl_cpy(sp->downer, (ufs_ic_acl_t *)&tsp->downer);
13837c478bd9Sstevel@tonic-gate acl_cpy(sp->dgroup, (ufs_ic_acl_t *)&tsp->dgroup);
13847c478bd9Sstevel@tonic-gate acl_cpy(sp->dother, (ufs_ic_acl_t *)&tsp->dother);
13857c478bd9Sstevel@tonic-gate acl_cpy(sp->dusers, (ufs_ic_acl_t *)&tsp->dusers);
13867c478bd9Sstevel@tonic-gate acl_cpy(sp->dgroups, (ufs_ic_acl_t *)&tsp->dgroups);
13877c478bd9Sstevel@tonic-gate tsp->dclass.acl_ismask = sp->dclass.acl_ismask;
13887c478bd9Sstevel@tonic-gate tsp->dclass.acl_maskbits = sp->dclass.acl_maskbits;
13897c478bd9Sstevel@tonic-gate }
13907c478bd9Sstevel@tonic-gate /*
13917c478bd9Sstevel@tonic-gate * save the new 9 mode bits in the inode (ip->ic_smode) for
13927c478bd9Sstevel@tonic-gate * ufs_getattr. Be sure the mode can be recovered if the store
13937c478bd9Sstevel@tonic-gate * fails.
13947c478bd9Sstevel@tonic-gate */
13957c478bd9Sstevel@tonic-gate old_modes = ip->i_mode;
13967c478bd9Sstevel@tonic-gate old_uid = ip->i_uid;
13977c478bd9Sstevel@tonic-gate old_gid = ip->i_gid;
13987c478bd9Sstevel@tonic-gate /*
13997c478bd9Sstevel@tonic-gate * store the acl, and get back a new security anchor if
14007c478bd9Sstevel@tonic-gate * it is a duplicate.
14017c478bd9Sstevel@tonic-gate */
14027c478bd9Sstevel@tonic-gate rw_exit(&sp->s_lock);
14037c478bd9Sstevel@tonic-gate rw_enter(&ip->i_rwlock, RW_WRITER);
14047c478bd9Sstevel@tonic-gate
14057c478bd9Sstevel@tonic-gate /*
14067c478bd9Sstevel@tonic-gate * Suppress out of inodes messages if instructed in the
14077c478bd9Sstevel@tonic-gate * tdp inode.
14087c478bd9Sstevel@tonic-gate */
14097c478bd9Sstevel@tonic-gate ip->i_flag |= tdp->i_flag & IQUIET;
14107c478bd9Sstevel@tonic-gate
14117c478bd9Sstevel@tonic-gate if ((error = ufs_si_store(ip, tsp, 0, cr)) != 0) {
14127c478bd9Sstevel@tonic-gate ip->i_mode = old_modes;
14137c478bd9Sstevel@tonic-gate ip->i_uid = old_uid;
14147c478bd9Sstevel@tonic-gate ip->i_gid = old_gid;
14157c478bd9Sstevel@tonic-gate }
14167c478bd9Sstevel@tonic-gate ip->i_flag &= ~IQUIET;
14177c478bd9Sstevel@tonic-gate rw_exit(&ip->i_rwlock);
14187c478bd9Sstevel@tonic-gate return (error);
14197c478bd9Sstevel@tonic-gate }
14207c478bd9Sstevel@tonic-gate
14217c478bd9Sstevel@tonic-gate si_t *
ufs_acl_cp(si_t * sp)14227c478bd9Sstevel@tonic-gate ufs_acl_cp(si_t *sp)
14237c478bd9Sstevel@tonic-gate {
14247c478bd9Sstevel@tonic-gate
14257c478bd9Sstevel@tonic-gate si_t *dsp;
14267c478bd9Sstevel@tonic-gate
14277c478bd9Sstevel@tonic-gate ASSERT(RW_READ_HELD(&sp->s_lock));
14287c478bd9Sstevel@tonic-gate ASSERT(sp->s_ref && sp->s_use);
14297c478bd9Sstevel@tonic-gate
14307c478bd9Sstevel@tonic-gate dsp = kmem_zalloc(sizeof (si_t), KM_SLEEP);
14317c478bd9Sstevel@tonic-gate rw_init(&dsp->s_lock, NULL, RW_DEFAULT, NULL);
14327c478bd9Sstevel@tonic-gate
14337c478bd9Sstevel@tonic-gate acl_cpy(sp->aowner, (ufs_ic_acl_t *)&dsp->aowner);
14347c478bd9Sstevel@tonic-gate acl_cpy(sp->agroup, (ufs_ic_acl_t *)&dsp->agroup);
14357c478bd9Sstevel@tonic-gate acl_cpy(sp->aother, (ufs_ic_acl_t *)&dsp->aother);
14367c478bd9Sstevel@tonic-gate acl_cpy(sp->ausers, (ufs_ic_acl_t *)&dsp->ausers);
14377c478bd9Sstevel@tonic-gate acl_cpy(sp->agroups, (ufs_ic_acl_t *)&dsp->agroups);
14387c478bd9Sstevel@tonic-gate
14397c478bd9Sstevel@tonic-gate dsp->aclass.acl_ismask = sp->aclass.acl_ismask;
14407c478bd9Sstevel@tonic-gate dsp->aclass.acl_maskbits = sp->aclass.acl_maskbits;
14417c478bd9Sstevel@tonic-gate
14427c478bd9Sstevel@tonic-gate acl_cpy(sp->downer, (ufs_ic_acl_t *)&dsp->downer);
14437c478bd9Sstevel@tonic-gate acl_cpy(sp->dgroup, (ufs_ic_acl_t *)&dsp->dgroup);
14447c478bd9Sstevel@tonic-gate acl_cpy(sp->dother, (ufs_ic_acl_t *)&dsp->dother);
14457c478bd9Sstevel@tonic-gate acl_cpy(sp->dusers, (ufs_ic_acl_t *)&dsp->dusers);
14467c478bd9Sstevel@tonic-gate acl_cpy(sp->dgroups, (ufs_ic_acl_t *)&dsp->dgroups);
14477c478bd9Sstevel@tonic-gate
14487c478bd9Sstevel@tonic-gate dsp->dclass.acl_ismask = sp->dclass.acl_ismask;
14497c478bd9Sstevel@tonic-gate dsp->dclass.acl_maskbits = sp->dclass.acl_maskbits;
14507c478bd9Sstevel@tonic-gate
14517c478bd9Sstevel@tonic-gate return (dsp);
14527c478bd9Sstevel@tonic-gate
14537c478bd9Sstevel@tonic-gate }
14547c478bd9Sstevel@tonic-gate
14557c478bd9Sstevel@tonic-gate int
ufs_acl_setattr(struct inode * ip,struct vattr * vap,cred_t * cr)14567c478bd9Sstevel@tonic-gate ufs_acl_setattr(struct inode *ip, struct vattr *vap, cred_t *cr)
14577c478bd9Sstevel@tonic-gate {
14587c478bd9Sstevel@tonic-gate
14597c478bd9Sstevel@tonic-gate si_t *sp;
14607c478bd9Sstevel@tonic-gate int mask = vap->va_mask;
14617c478bd9Sstevel@tonic-gate int error = 0;
14627c478bd9Sstevel@tonic-gate
14637c478bd9Sstevel@tonic-gate ASSERT(RW_WRITE_HELD(&ip->i_contents));
14647c478bd9Sstevel@tonic-gate
14657c478bd9Sstevel@tonic-gate if (!(mask & (AT_MODE|AT_UID|AT_GID)))
14667c478bd9Sstevel@tonic-gate return (0);
14677c478bd9Sstevel@tonic-gate
14687c478bd9Sstevel@tonic-gate /*
14697c478bd9Sstevel@tonic-gate * if no regular acl's, nothing to do, so let's get out
14707c478bd9Sstevel@tonic-gate */
14717c478bd9Sstevel@tonic-gate if (!(ip->i_ufs_acl) || !(ip->i_ufs_acl->aowner))
14727c478bd9Sstevel@tonic-gate return (0);
14737c478bd9Sstevel@tonic-gate
14747c478bd9Sstevel@tonic-gate rw_enter(&ip->i_ufs_acl->s_lock, RW_READER);
14757c478bd9Sstevel@tonic-gate sp = ufs_acl_cp(ip->i_ufs_acl);
14767c478bd9Sstevel@tonic-gate ASSERT(sp != ip->i_ufs_acl);
14777c478bd9Sstevel@tonic-gate
14787c478bd9Sstevel@tonic-gate /*
14797c478bd9Sstevel@tonic-gate * set the mask to the group permissions if a mask entry
14807c478bd9Sstevel@tonic-gate * exists. Otherwise, set the group obj bits to the group
14817c478bd9Sstevel@tonic-gate * permissions. Since non-trivial ACLs always have a mask,
14827c478bd9Sstevel@tonic-gate * and the mask is the final arbiter of group permissions,
14837c478bd9Sstevel@tonic-gate * setting the mask has the effect of changing the effective
14847c478bd9Sstevel@tonic-gate * group permissions, even if the group_obj permissions in
14857c478bd9Sstevel@tonic-gate * the ACL aren't changed. Posix P1003.1e states that when
14867c478bd9Sstevel@tonic-gate * an ACL mask exists, chmod(2) must set the acl mask (NOT the
14877c478bd9Sstevel@tonic-gate * group_obj permissions) to the requested group permissions.
14887c478bd9Sstevel@tonic-gate */
14897c478bd9Sstevel@tonic-gate if (mask & AT_MODE) {
14907c478bd9Sstevel@tonic-gate sp->aowner->acl_ic_perm = (o_mode_t)(ip->i_mode & 0700) >> 6;
14917c478bd9Sstevel@tonic-gate if (sp->aclass.acl_ismask)
14927c478bd9Sstevel@tonic-gate sp->aclass.acl_maskbits =
14937c478bd9Sstevel@tonic-gate (o_mode_t)(ip->i_mode & 070) >> 3;
14947c478bd9Sstevel@tonic-gate else
14957c478bd9Sstevel@tonic-gate sp->agroup->acl_ic_perm =
14967c478bd9Sstevel@tonic-gate (o_mode_t)(ip->i_mode & 070) >> 3;
14977c478bd9Sstevel@tonic-gate sp->aother->acl_ic_perm = (o_mode_t)(ip->i_mode & 07);
14987c478bd9Sstevel@tonic-gate }
14997c478bd9Sstevel@tonic-gate
15007c478bd9Sstevel@tonic-gate if (mask & AT_UID) {
15017c478bd9Sstevel@tonic-gate /* Caller has verified our privileges */
15027c478bd9Sstevel@tonic-gate sp->aowner->acl_ic_who = ip->i_uid;
15037c478bd9Sstevel@tonic-gate }
15047c478bd9Sstevel@tonic-gate
15057c478bd9Sstevel@tonic-gate if (mask & AT_GID) {
15067c478bd9Sstevel@tonic-gate sp->agroup->acl_ic_who = ip->i_gid;
15077c478bd9Sstevel@tonic-gate }
15087c478bd9Sstevel@tonic-gate
15097c478bd9Sstevel@tonic-gate rw_exit(&ip->i_ufs_acl->s_lock);
15107c478bd9Sstevel@tonic-gate error = ufs_si_store(ip, sp, 0, cr);
15117c478bd9Sstevel@tonic-gate return (error);
15127c478bd9Sstevel@tonic-gate }
15137c478bd9Sstevel@tonic-gate
15147c478bd9Sstevel@tonic-gate static int
acl_count(ufs_ic_acl_t * p)15157c478bd9Sstevel@tonic-gate acl_count(ufs_ic_acl_t *p)
15167c478bd9Sstevel@tonic-gate {
15177c478bd9Sstevel@tonic-gate ufs_ic_acl_t *acl;
15187c478bd9Sstevel@tonic-gate int count;
15197c478bd9Sstevel@tonic-gate
15207c478bd9Sstevel@tonic-gate for (count = 0, acl = p; acl; acl = acl->acl_ic_next, count++)
15217c478bd9Sstevel@tonic-gate ;
15227c478bd9Sstevel@tonic-gate return (count);
15237c478bd9Sstevel@tonic-gate }
15247c478bd9Sstevel@tonic-gate
15257c478bd9Sstevel@tonic-gate /*
15267c478bd9Sstevel@tonic-gate * Takes as input a security structure and generates a buffer
15277c478bd9Sstevel@tonic-gate * with fsd's in a form which be written to the shadow inode.
15287c478bd9Sstevel@tonic-gate */
15297c478bd9Sstevel@tonic-gate static int
ufs_sectobuf(si_t * sp,caddr_t * buf,size_t * len)15307c478bd9Sstevel@tonic-gate ufs_sectobuf(si_t *sp, caddr_t *buf, size_t *len)
15317c478bd9Sstevel@tonic-gate {
15327c478bd9Sstevel@tonic-gate size_t acl_size;
15337c478bd9Sstevel@tonic-gate size_t def_acl_size;
15347c478bd9Sstevel@tonic-gate caddr_t buffer;
15357c478bd9Sstevel@tonic-gate struct ufs_fsd *fsdp;
15367c478bd9Sstevel@tonic-gate ufs_acl_t *bufaclp;
15377c478bd9Sstevel@tonic-gate
15387c478bd9Sstevel@tonic-gate /*
15397c478bd9Sstevel@tonic-gate * Calc size of buffer to hold all the acls
15407c478bd9Sstevel@tonic-gate */
15417c478bd9Sstevel@tonic-gate acl_size = acl_count(sp->aowner) + /* owner */
15427c478bd9Sstevel@tonic-gate acl_count(sp->agroup) + /* owner group */
15437c478bd9Sstevel@tonic-gate acl_count(sp->aother) + /* owner other */
15447c478bd9Sstevel@tonic-gate acl_count(sp->ausers) + /* acl list */
15457c478bd9Sstevel@tonic-gate acl_count(sp->agroups); /* group alcs */
15467c478bd9Sstevel@tonic-gate if (sp->aclass.acl_ismask)
15477c478bd9Sstevel@tonic-gate acl_size++;
15487c478bd9Sstevel@tonic-gate
15497c478bd9Sstevel@tonic-gate /* Convert to bytes */
15507c478bd9Sstevel@tonic-gate acl_size *= sizeof (ufs_acl_t);
15517c478bd9Sstevel@tonic-gate
15527c478bd9Sstevel@tonic-gate /* Add fsd header */
15537c478bd9Sstevel@tonic-gate if (acl_size)
15547c478bd9Sstevel@tonic-gate acl_size += 2 * sizeof (int);
15557c478bd9Sstevel@tonic-gate
15567c478bd9Sstevel@tonic-gate /*
15577c478bd9Sstevel@tonic-gate * Calc size of buffer to hold all the default acls
15587c478bd9Sstevel@tonic-gate */
15597c478bd9Sstevel@tonic-gate def_acl_size =
15607c478bd9Sstevel@tonic-gate acl_count(sp->downer) + /* def owner */
15617c478bd9Sstevel@tonic-gate acl_count(sp->dgroup) + /* def owner group */
15627c478bd9Sstevel@tonic-gate acl_count(sp->dother) + /* def owner other */
15637c478bd9Sstevel@tonic-gate acl_count(sp->dusers) + /* def users */
15647c478bd9Sstevel@tonic-gate acl_count(sp->dgroups); /* def group acls */
15657c478bd9Sstevel@tonic-gate if (sp->dclass.acl_ismask)
15667c478bd9Sstevel@tonic-gate def_acl_size++;
15677c478bd9Sstevel@tonic-gate
15687c478bd9Sstevel@tonic-gate /*
15697c478bd9Sstevel@tonic-gate * Convert to bytes
15707c478bd9Sstevel@tonic-gate */
15717c478bd9Sstevel@tonic-gate def_acl_size *= sizeof (ufs_acl_t);
15727c478bd9Sstevel@tonic-gate
15737c478bd9Sstevel@tonic-gate /*
15747c478bd9Sstevel@tonic-gate * Add fsd header
15757c478bd9Sstevel@tonic-gate */
15767c478bd9Sstevel@tonic-gate if (def_acl_size)
15777c478bd9Sstevel@tonic-gate def_acl_size += 2 * sizeof (int);
15787c478bd9Sstevel@tonic-gate
15797c478bd9Sstevel@tonic-gate if (acl_size + def_acl_size == 0)
15807c478bd9Sstevel@tonic-gate return (0);
15817c478bd9Sstevel@tonic-gate
15827c478bd9Sstevel@tonic-gate buffer = kmem_zalloc((acl_size + def_acl_size), KM_SLEEP);
15837c478bd9Sstevel@tonic-gate bufaclp = (ufs_acl_t *)buffer;
15847c478bd9Sstevel@tonic-gate
15857c478bd9Sstevel@tonic-gate if (acl_size == 0)
15867c478bd9Sstevel@tonic-gate goto wrtdefs;
15877c478bd9Sstevel@tonic-gate
15887c478bd9Sstevel@tonic-gate /* create fsd and copy acls */
15897c478bd9Sstevel@tonic-gate fsdp = (struct ufs_fsd *)bufaclp;
15907c478bd9Sstevel@tonic-gate fsdp->fsd_type = FSD_ACL;
15917c478bd9Sstevel@tonic-gate bufaclp = (ufs_acl_t *)&fsdp->fsd_data[0];
15927c478bd9Sstevel@tonic-gate
15937c478bd9Sstevel@tonic-gate ACL_MOVE(sp->aowner, USER_OBJ, bufaclp);
15947c478bd9Sstevel@tonic-gate ACL_MOVE(sp->agroup, GROUP_OBJ, bufaclp);
15957c478bd9Sstevel@tonic-gate ACL_MOVE(sp->aother, OTHER_OBJ, bufaclp);
15967c478bd9Sstevel@tonic-gate ACL_MOVE(sp->ausers, USER, bufaclp);
15977c478bd9Sstevel@tonic-gate ACL_MOVE(sp->agroups, GROUP, bufaclp);
15987c478bd9Sstevel@tonic-gate
15997c478bd9Sstevel@tonic-gate if (sp->aclass.acl_ismask) {
16007c478bd9Sstevel@tonic-gate bufaclp->acl_tag = CLASS_OBJ;
16017c478bd9Sstevel@tonic-gate bufaclp->acl_who = (uid_t)sp->aclass.acl_ismask;
16027c478bd9Sstevel@tonic-gate bufaclp->acl_perm = (o_mode_t)sp->aclass.acl_maskbits;
16037c478bd9Sstevel@tonic-gate bufaclp++;
16047c478bd9Sstevel@tonic-gate }
16057c478bd9Sstevel@tonic-gate ASSERT(acl_size <= INT_MAX);
16067c478bd9Sstevel@tonic-gate fsdp->fsd_size = (int)acl_size;
16077c478bd9Sstevel@tonic-gate
16087c478bd9Sstevel@tonic-gate wrtdefs:
16097c478bd9Sstevel@tonic-gate if (def_acl_size == 0)
16107c478bd9Sstevel@tonic-gate goto alldone;
16117c478bd9Sstevel@tonic-gate
16127c478bd9Sstevel@tonic-gate /* if defaults exist then create fsd and copy default acls */
16137c478bd9Sstevel@tonic-gate fsdp = (struct ufs_fsd *)bufaclp;
16147c478bd9Sstevel@tonic-gate fsdp->fsd_type = FSD_DFACL;
16157c478bd9Sstevel@tonic-gate bufaclp = (ufs_acl_t *)&fsdp->fsd_data[0];
16167c478bd9Sstevel@tonic-gate
16177c478bd9Sstevel@tonic-gate ACL_MOVE(sp->downer, DEF_USER_OBJ, bufaclp);
16187c478bd9Sstevel@tonic-gate ACL_MOVE(sp->dgroup, DEF_GROUP_OBJ, bufaclp);
16197c478bd9Sstevel@tonic-gate ACL_MOVE(sp->dother, DEF_OTHER_OBJ, bufaclp);
16207c478bd9Sstevel@tonic-gate ACL_MOVE(sp->dusers, DEF_USER, bufaclp);
16217c478bd9Sstevel@tonic-gate ACL_MOVE(sp->dgroups, DEF_GROUP, bufaclp);
16227c478bd9Sstevel@tonic-gate if (sp->dclass.acl_ismask) {
16237c478bd9Sstevel@tonic-gate bufaclp->acl_tag = DEF_CLASS_OBJ;
16247c478bd9Sstevel@tonic-gate bufaclp->acl_who = (uid_t)sp->dclass.acl_ismask;
16257c478bd9Sstevel@tonic-gate bufaclp->acl_perm = (o_mode_t)sp->dclass.acl_maskbits;
16267c478bd9Sstevel@tonic-gate bufaclp++;
16277c478bd9Sstevel@tonic-gate }
16287c478bd9Sstevel@tonic-gate ASSERT(def_acl_size <= INT_MAX);
16297c478bd9Sstevel@tonic-gate fsdp->fsd_size = (int)def_acl_size;
16307c478bd9Sstevel@tonic-gate
16317c478bd9Sstevel@tonic-gate alldone:
16327c478bd9Sstevel@tonic-gate *buf = buffer;
16337c478bd9Sstevel@tonic-gate *len = acl_size + def_acl_size;
16347c478bd9Sstevel@tonic-gate
16357c478bd9Sstevel@tonic-gate return (0);
16367c478bd9Sstevel@tonic-gate }
16377c478bd9Sstevel@tonic-gate
16387c478bd9Sstevel@tonic-gate /*
16397c478bd9Sstevel@tonic-gate * free a shadow inode on disk and in memory
16407c478bd9Sstevel@tonic-gate */
16417c478bd9Sstevel@tonic-gate int
ufs_si_free(si_t * sp,struct vfs * vfsp,cred_t * cr)16427c478bd9Sstevel@tonic-gate ufs_si_free(si_t *sp, struct vfs *vfsp, cred_t *cr)
16437c478bd9Sstevel@tonic-gate {
16447c478bd9Sstevel@tonic-gate struct inode *sip;
16457c478bd9Sstevel@tonic-gate int shadow;
16467c478bd9Sstevel@tonic-gate int err = 0;
16477c478bd9Sstevel@tonic-gate int refcnt;
16487c478bd9Sstevel@tonic-gate int signature;
16497c478bd9Sstevel@tonic-gate
16507c478bd9Sstevel@tonic-gate ASSERT(vfsp);
16517c478bd9Sstevel@tonic-gate ASSERT(sp);
16527c478bd9Sstevel@tonic-gate
16537c478bd9Sstevel@tonic-gate rw_enter(&sp->s_lock, RW_READER);
16547c478bd9Sstevel@tonic-gate ASSERT(sp->s_shadow <= INT_MAX);
16557c478bd9Sstevel@tonic-gate shadow = (int)sp->s_shadow;
16567c478bd9Sstevel@tonic-gate ASSERT(sp->s_ref);
16577c478bd9Sstevel@tonic-gate rw_exit(&sp->s_lock);
16587c478bd9Sstevel@tonic-gate
16597c478bd9Sstevel@tonic-gate /*
16607c478bd9Sstevel@tonic-gate * Decrement link count on the shadow inode,
16617c478bd9Sstevel@tonic-gate * and decrement reference count on the sip.
16627c478bd9Sstevel@tonic-gate */
16637c478bd9Sstevel@tonic-gate if ((err = ufs_iget_alloced(vfsp, shadow, &sip, cr)) == 0) {
16647c478bd9Sstevel@tonic-gate rw_enter(&sip->i_contents, RW_WRITER);
16657c478bd9Sstevel@tonic-gate rw_enter(&sp->s_lock, RW_WRITER);
16667c478bd9Sstevel@tonic-gate ASSERT(sp->s_shadow == shadow);
16677c478bd9Sstevel@tonic-gate ASSERT(sip->i_dquot == 0);
16687c478bd9Sstevel@tonic-gate /* Decrement link count */
16697c478bd9Sstevel@tonic-gate ASSERT(sip->i_nlink > 0);
16707c478bd9Sstevel@tonic-gate /*
16717c478bd9Sstevel@tonic-gate * bug #1264710 assertion failure below
16727c478bd9Sstevel@tonic-gate */
16737c478bd9Sstevel@tonic-gate sp->s_use = --sip->i_nlink;
16747c478bd9Sstevel@tonic-gate ufs_setreclaim(sip);
16757c478bd9Sstevel@tonic-gate TRANS_INODE(sip->i_ufsvfs, sip);
16767c478bd9Sstevel@tonic-gate sip->i_flag |= ICHG | IMOD;
16777c478bd9Sstevel@tonic-gate sip->i_seq++;
16787c478bd9Sstevel@tonic-gate ITIMES_NOLOCK(sip);
16797c478bd9Sstevel@tonic-gate /* Dec ref counts on si referenced by this ip */
16807c478bd9Sstevel@tonic-gate refcnt = --sp->s_ref;
16817c478bd9Sstevel@tonic-gate signature = sp->s_signature;
16827c478bd9Sstevel@tonic-gate ASSERT(sp->s_ref >= 0 && sp->s_ref <= sp->s_use);
16837c478bd9Sstevel@tonic-gate /*
16847c478bd9Sstevel@tonic-gate * Release s_lock before calling VN_RELE
16857c478bd9Sstevel@tonic-gate * (which may want to acquire i_contents).
16867c478bd9Sstevel@tonic-gate */
16877c478bd9Sstevel@tonic-gate rw_exit(&sp->s_lock);
16887c478bd9Sstevel@tonic-gate rw_exit(&sip->i_contents);
16897c478bd9Sstevel@tonic-gate VN_RELE(ITOV(sip));
16907c478bd9Sstevel@tonic-gate } else {
16917c478bd9Sstevel@tonic-gate rw_enter(&sp->s_lock, RW_WRITER);
16927c478bd9Sstevel@tonic-gate /* Dec ref counts on si referenced by this ip */
16937c478bd9Sstevel@tonic-gate refcnt = --sp->s_ref;
16947c478bd9Sstevel@tonic-gate signature = sp->s_signature;
16957c478bd9Sstevel@tonic-gate ASSERT(sp->s_ref >= 0 && sp->s_ref <= sp->s_use);
16967c478bd9Sstevel@tonic-gate rw_exit(&sp->s_lock);
16977c478bd9Sstevel@tonic-gate }
16987c478bd9Sstevel@tonic-gate
16997c478bd9Sstevel@tonic-gate if (refcnt == 0)
17007c478bd9Sstevel@tonic-gate si_cache_del(sp, signature);
17017c478bd9Sstevel@tonic-gate return (err);
17027c478bd9Sstevel@tonic-gate }
17037c478bd9Sstevel@tonic-gate
17047c478bd9Sstevel@tonic-gate /*
17057c478bd9Sstevel@tonic-gate * Seach the si cache for an si structure by inode #.
17067c478bd9Sstevel@tonic-gate * Returns a locked si structure.
17077c478bd9Sstevel@tonic-gate *
17087c478bd9Sstevel@tonic-gate * Parameters:
17097c478bd9Sstevel@tonic-gate * ip - Ptr to an inode on this fs
17107c478bd9Sstevel@tonic-gate * spp - Ptr to ptr to si struct for the results, if found.
17117c478bd9Sstevel@tonic-gate *
17127c478bd9Sstevel@tonic-gate * Returns: 0 - Success (results in spp)
17137c478bd9Sstevel@tonic-gate * 1 - Failure (spp undefined)
17147c478bd9Sstevel@tonic-gate */
17157c478bd9Sstevel@tonic-gate static int
si_cachei_get(struct inode * ip,si_t ** spp)17167c478bd9Sstevel@tonic-gate si_cachei_get(struct inode *ip, si_t **spp)
17177c478bd9Sstevel@tonic-gate {
17187c478bd9Sstevel@tonic-gate si_t *sp;
17197c478bd9Sstevel@tonic-gate
17207c478bd9Sstevel@tonic-gate rw_enter(&si_cache_lock, RW_READER);
17217c478bd9Sstevel@tonic-gate loop:
17227c478bd9Sstevel@tonic-gate for (sp = si_cachei[SI_HASH(ip->i_shadow)]; sp; sp = sp->s_forw)
17237c478bd9Sstevel@tonic-gate if (sp->s_shadow == ip->i_shadow && sp->s_dev == ip->i_dev)
17247c478bd9Sstevel@tonic-gate break;
17257c478bd9Sstevel@tonic-gate
17267c478bd9Sstevel@tonic-gate if (sp == NULL) {
17277c478bd9Sstevel@tonic-gate /* Not in cache */
17287c478bd9Sstevel@tonic-gate rw_exit(&si_cache_lock);
17297c478bd9Sstevel@tonic-gate return (1);
17307c478bd9Sstevel@tonic-gate }
17317c478bd9Sstevel@tonic-gate /* Found it */
17327c478bd9Sstevel@tonic-gate rw_enter(&sp->s_lock, RW_WRITER);
17337c478bd9Sstevel@tonic-gate alldone:
17347c478bd9Sstevel@tonic-gate rw_exit(&si_cache_lock);
17357c478bd9Sstevel@tonic-gate *spp = sp;
17367c478bd9Sstevel@tonic-gate return (0);
17377c478bd9Sstevel@tonic-gate }
17387c478bd9Sstevel@tonic-gate
17397c478bd9Sstevel@tonic-gate /*
17407c478bd9Sstevel@tonic-gate * Seach the si cache by si structure (ie duplicate of the one passed in).
17417c478bd9Sstevel@tonic-gate * In order for a match the signatures must be the same and
17427c478bd9Sstevel@tonic-gate * the devices must be the same, the acls must match and
17437c478bd9Sstevel@tonic-gate * link count of the cached shadow must be less than the
17447c478bd9Sstevel@tonic-gate * size of ic_nlink - 1. MAXLINK - 1 is used to allow the count
17457c478bd9Sstevel@tonic-gate * to be incremented one more time by the caller.
17467c478bd9Sstevel@tonic-gate * Returns a locked si structure.
17477c478bd9Sstevel@tonic-gate *
17487c478bd9Sstevel@tonic-gate * Parameters:
17497c478bd9Sstevel@tonic-gate * ip - Ptr to an inode on this fs
17507c478bd9Sstevel@tonic-gate * spi - Ptr to si the struct we're searching the cache for.
17517c478bd9Sstevel@tonic-gate * spp - Ptr to ptr to si struct for the results, if found.
17527c478bd9Sstevel@tonic-gate *
17537c478bd9Sstevel@tonic-gate * Returns: 0 - Success (results in spp)
17547c478bd9Sstevel@tonic-gate * 1 - Failure (spp undefined)
17557c478bd9Sstevel@tonic-gate */
17567c478bd9Sstevel@tonic-gate static int
si_cachea_get(struct inode * ip,si_t * spi,si_t ** spp)17577c478bd9Sstevel@tonic-gate si_cachea_get(struct inode *ip, si_t *spi, si_t **spp)
17587c478bd9Sstevel@tonic-gate {
17597c478bd9Sstevel@tonic-gate si_t *sp;
17607c478bd9Sstevel@tonic-gate
17617c478bd9Sstevel@tonic-gate spi->s_dev = ip->i_dev;
17627c478bd9Sstevel@tonic-gate spi->s_signature = si_signature(spi);
17637c478bd9Sstevel@tonic-gate rw_enter(&si_cache_lock, RW_READER);
17647c478bd9Sstevel@tonic-gate loop:
17657c478bd9Sstevel@tonic-gate for (sp = si_cachea[SI_HASH(spi->s_signature)]; sp; sp = sp->s_next) {
17667c478bd9Sstevel@tonic-gate if (sp->s_signature == spi->s_signature &&
17677c478bd9Sstevel@tonic-gate sp->s_dev == spi->s_dev &&
17687c478bd9Sstevel@tonic-gate sp->s_use > 0 && /* deleting */
17697c478bd9Sstevel@tonic-gate sp->s_use <= (MAXLINK - 1) && /* Too many links */
17707c478bd9Sstevel@tonic-gate !si_cmp(sp, spi))
17717c478bd9Sstevel@tonic-gate break;
17727c478bd9Sstevel@tonic-gate }
17737c478bd9Sstevel@tonic-gate
17747c478bd9Sstevel@tonic-gate if (sp == NULL) {
17757c478bd9Sstevel@tonic-gate /* Cache miss */
17767c478bd9Sstevel@tonic-gate rw_exit(&si_cache_lock);
17777c478bd9Sstevel@tonic-gate return (1);
17787c478bd9Sstevel@tonic-gate }
17797c478bd9Sstevel@tonic-gate /* Found it */
17807c478bd9Sstevel@tonic-gate rw_enter(&sp->s_lock, RW_WRITER);
17817c478bd9Sstevel@tonic-gate alldone:
17827c478bd9Sstevel@tonic-gate spi->s_shadow = sp->s_shadow; /* XXX For debugging */
17837c478bd9Sstevel@tonic-gate rw_exit(&si_cache_lock);
17847c478bd9Sstevel@tonic-gate *spp = sp;
17857c478bd9Sstevel@tonic-gate return (0);
17867c478bd9Sstevel@tonic-gate }
17877c478bd9Sstevel@tonic-gate
17887c478bd9Sstevel@tonic-gate /*
17897c478bd9Sstevel@tonic-gate * Place an si structure in the si cache. May cause duplicates.
17907c478bd9Sstevel@tonic-gate *
17917c478bd9Sstevel@tonic-gate * Parameters:
17927c478bd9Sstevel@tonic-gate * sp - Ptr to the si struct to add to the cache.
17937c478bd9Sstevel@tonic-gate *
17947c478bd9Sstevel@tonic-gate * Returns: Nothing (void)
17957c478bd9Sstevel@tonic-gate */
17967c478bd9Sstevel@tonic-gate static void
si_cache_put(si_t * sp)17977c478bd9Sstevel@tonic-gate si_cache_put(si_t *sp)
17987c478bd9Sstevel@tonic-gate {
17997c478bd9Sstevel@tonic-gate si_t **tspp;
18007c478bd9Sstevel@tonic-gate
18017c478bd9Sstevel@tonic-gate ASSERT(sp->s_fore == NULL);
18027c478bd9Sstevel@tonic-gate rw_enter(&si_cache_lock, RW_WRITER);
18037c478bd9Sstevel@tonic-gate if (!sp->s_signature)
18047c478bd9Sstevel@tonic-gate sp->s_signature = si_signature(sp);
18057c478bd9Sstevel@tonic-gate sp->s_flags |= SI_CACHED;
18067c478bd9Sstevel@tonic-gate sp->s_fore = NULL;
18077c478bd9Sstevel@tonic-gate
18087c478bd9Sstevel@tonic-gate /* The 'by acl' chains */
18097c478bd9Sstevel@tonic-gate tspp = &si_cachea[SI_HASH(sp->s_signature)];
18107c478bd9Sstevel@tonic-gate sp->s_next = *tspp;
18117c478bd9Sstevel@tonic-gate *tspp = sp;
18127c478bd9Sstevel@tonic-gate
18137c478bd9Sstevel@tonic-gate /* The 'by inode' chains */
18147c478bd9Sstevel@tonic-gate tspp = &si_cachei[SI_HASH(sp->s_shadow)];
18157c478bd9Sstevel@tonic-gate sp->s_forw = *tspp;
18167c478bd9Sstevel@tonic-gate *tspp = sp;
18177c478bd9Sstevel@tonic-gate
18187c478bd9Sstevel@tonic-gate rw_exit(&si_cache_lock);
18197c478bd9Sstevel@tonic-gate }
18207c478bd9Sstevel@tonic-gate
18217c478bd9Sstevel@tonic-gate /*
18227c478bd9Sstevel@tonic-gate * The sp passed in is a candidate for deletion from the cache. We acquire
18237c478bd9Sstevel@tonic-gate * the cache lock first, so no cache searches can be done. Then we search
18247c478bd9Sstevel@tonic-gate * for the acl in the cache, and if we find it we can lock it and check that
18257c478bd9Sstevel@tonic-gate * nobody else attached to it while we were acquiring the locks. If the acl
18267c478bd9Sstevel@tonic-gate * is in the cache and still has a zero reference count, then we remove it
18277c478bd9Sstevel@tonic-gate * from the cache and deallocate it. If the reference count is non-zero or
18287c478bd9Sstevel@tonic-gate * it is not found in the cache, then someone else attached to it or has
18297c478bd9Sstevel@tonic-gate * already freed it, so we just return.
18307c478bd9Sstevel@tonic-gate *
18317c478bd9Sstevel@tonic-gate * Parameters:
18327c478bd9Sstevel@tonic-gate * sp - Ptr to the sp struct which is the candicate for deletion.
18337c478bd9Sstevel@tonic-gate * signature - the signature for the acl for lookup in the hash table
18347c478bd9Sstevel@tonic-gate *
18357c478bd9Sstevel@tonic-gate * Returns: Nothing (void)
18367c478bd9Sstevel@tonic-gate */
18377c478bd9Sstevel@tonic-gate void
si_cache_del(si_t * sp,int signature)18387c478bd9Sstevel@tonic-gate si_cache_del(si_t *sp, int signature)
18397c478bd9Sstevel@tonic-gate {
18407c478bd9Sstevel@tonic-gate si_t **tspp;
18417c478bd9Sstevel@tonic-gate int hash;
18427c478bd9Sstevel@tonic-gate int foundacl = 0;
18437c478bd9Sstevel@tonic-gate
18447c478bd9Sstevel@tonic-gate /*
18457c478bd9Sstevel@tonic-gate * Unlink & free the sp from the other queues, then destroy it.
18467c478bd9Sstevel@tonic-gate * Search the 'by acl' chain first, then the 'by inode' chain
18477c478bd9Sstevel@tonic-gate * after the acl is locked.
18487c478bd9Sstevel@tonic-gate */
18497c478bd9Sstevel@tonic-gate rw_enter(&si_cache_lock, RW_WRITER);
18507c478bd9Sstevel@tonic-gate hash = SI_HASH(signature);
18517c478bd9Sstevel@tonic-gate for (tspp = &si_cachea[hash]; *tspp; tspp = &(*tspp)->s_next) {
18527c478bd9Sstevel@tonic-gate if (*tspp == sp) {
18537c478bd9Sstevel@tonic-gate /*
18547c478bd9Sstevel@tonic-gate * Wait to grab the acl lock until after the acl has
18557c478bd9Sstevel@tonic-gate * been found in the cache. Otherwise it might try to
18567c478bd9Sstevel@tonic-gate * grab a lock that has already been destroyed, or
18577c478bd9Sstevel@tonic-gate * delete an acl that has already been freed.
18587c478bd9Sstevel@tonic-gate */
18597c478bd9Sstevel@tonic-gate rw_enter(&sp->s_lock, RW_WRITER);
18607c478bd9Sstevel@tonic-gate /* See if someone else attached to it */
18617c478bd9Sstevel@tonic-gate if (sp->s_ref) {
18627c478bd9Sstevel@tonic-gate rw_exit(&sp->s_lock);
18637c478bd9Sstevel@tonic-gate rw_exit(&si_cache_lock);
18647c478bd9Sstevel@tonic-gate return;
18657c478bd9Sstevel@tonic-gate }
18667c478bd9Sstevel@tonic-gate ASSERT(sp->s_fore == NULL);
18677c478bd9Sstevel@tonic-gate ASSERT(sp->s_flags & SI_CACHED);
18687c478bd9Sstevel@tonic-gate foundacl = 1;
18697c478bd9Sstevel@tonic-gate *tspp = sp->s_next;
18707c478bd9Sstevel@tonic-gate break;
18717c478bd9Sstevel@tonic-gate }
18727c478bd9Sstevel@tonic-gate }
18737c478bd9Sstevel@tonic-gate
18747c478bd9Sstevel@tonic-gate /*
18757c478bd9Sstevel@tonic-gate * If the acl was not in the cache, we assume another thread has
18767c478bd9Sstevel@tonic-gate * deleted it already. This could happen if another thread attaches to
18777c478bd9Sstevel@tonic-gate * the acl and then releases it after this thread has already found the
18787c478bd9Sstevel@tonic-gate * reference count to be zero but has not yet taken the cache lock.
18797c478bd9Sstevel@tonic-gate * Both threads end up seeing a reference count of zero, and call into
18807c478bd9Sstevel@tonic-gate * si_cache_del. See bug 4244827 for details on the race condition.
18817c478bd9Sstevel@tonic-gate */
18827c478bd9Sstevel@tonic-gate if (foundacl == 0) {
18837c478bd9Sstevel@tonic-gate rw_exit(&si_cache_lock);
18847c478bd9Sstevel@tonic-gate return;
18857c478bd9Sstevel@tonic-gate }
18867c478bd9Sstevel@tonic-gate
18877c478bd9Sstevel@tonic-gate /* Now check the 'by inode' chain */
18887c478bd9Sstevel@tonic-gate hash = SI_HASH(sp->s_shadow);
18897c478bd9Sstevel@tonic-gate for (tspp = &si_cachei[hash]; *tspp; tspp = &(*tspp)->s_forw) {
18907c478bd9Sstevel@tonic-gate if (*tspp == sp) {
18917c478bd9Sstevel@tonic-gate *tspp = sp->s_forw;
18927c478bd9Sstevel@tonic-gate break;
18937c478bd9Sstevel@tonic-gate }
18947c478bd9Sstevel@tonic-gate }
18957c478bd9Sstevel@tonic-gate
18967c478bd9Sstevel@tonic-gate /*
18977c478bd9Sstevel@tonic-gate * At this point, we can unlock everything because this si
18987c478bd9Sstevel@tonic-gate * is no longer in the cache, thus cannot be attached to.
18997c478bd9Sstevel@tonic-gate */
19007c478bd9Sstevel@tonic-gate rw_exit(&sp->s_lock);
19017c478bd9Sstevel@tonic-gate rw_exit(&si_cache_lock);
19027c478bd9Sstevel@tonic-gate sp->s_flags &= ~SI_CACHED;
19037c478bd9Sstevel@tonic-gate (void) ufs_si_free_mem(sp);
19047c478bd9Sstevel@tonic-gate }
19057c478bd9Sstevel@tonic-gate
19067c478bd9Sstevel@tonic-gate /*
19077c478bd9Sstevel@tonic-gate * Alloc the hash buckets for the si cache & initialize
19087c478bd9Sstevel@tonic-gate * the unreferenced anchor and the cache lock.
19097c478bd9Sstevel@tonic-gate */
19107c478bd9Sstevel@tonic-gate void
si_cache_init(void)19117c478bd9Sstevel@tonic-gate si_cache_init(void)
19127c478bd9Sstevel@tonic-gate {
19137c478bd9Sstevel@tonic-gate rw_init(&si_cache_lock, NULL, RW_DEFAULT, NULL);
19147c478bd9Sstevel@tonic-gate
19157c478bd9Sstevel@tonic-gate /* The 'by acl' headers */
19167c478bd9Sstevel@tonic-gate si_cachea = kmem_zalloc(si_cachecnt * sizeof (si_t *), KM_SLEEP);
19177c478bd9Sstevel@tonic-gate /* The 'by inode' headers */
19187c478bd9Sstevel@tonic-gate si_cachei = kmem_zalloc(si_cachecnt * sizeof (si_t *), KM_SLEEP);
19197c478bd9Sstevel@tonic-gate }
19207c478bd9Sstevel@tonic-gate
19217c478bd9Sstevel@tonic-gate /*
19227c478bd9Sstevel@tonic-gate * aclcksum takes an acl and generates a checksum. It takes as input
19237c478bd9Sstevel@tonic-gate * the acl to start at.
19247c478bd9Sstevel@tonic-gate *
19257c478bd9Sstevel@tonic-gate * s_aclp - pointer to starting acl
19267c478bd9Sstevel@tonic-gate *
19277c478bd9Sstevel@tonic-gate * returns checksum
19287c478bd9Sstevel@tonic-gate */
19297c478bd9Sstevel@tonic-gate static int
aclcksum(ufs_ic_acl_t * s_aclp)19307c478bd9Sstevel@tonic-gate aclcksum(ufs_ic_acl_t *s_aclp)
19317c478bd9Sstevel@tonic-gate {
19327c478bd9Sstevel@tonic-gate ufs_ic_acl_t *aclp;
19337c478bd9Sstevel@tonic-gate int signature = 0;
19347c478bd9Sstevel@tonic-gate for (aclp = s_aclp; aclp; aclp = aclp->acl_ic_next) {
19357c478bd9Sstevel@tonic-gate signature += aclp->acl_ic_perm;
19367c478bd9Sstevel@tonic-gate signature += aclp->acl_ic_who;
19377c478bd9Sstevel@tonic-gate }
19387c478bd9Sstevel@tonic-gate return (signature);
19397c478bd9Sstevel@tonic-gate }
19407c478bd9Sstevel@tonic-gate
19417c478bd9Sstevel@tonic-gate /*
19427c478bd9Sstevel@tonic-gate * Generate a unique signature for an si structure. Used by the
19437c478bd9Sstevel@tonic-gate * search routine si_cachea_get() to quickly identify candidates
19447c478bd9Sstevel@tonic-gate * prior to calling si_cmp().
19457c478bd9Sstevel@tonic-gate * Parameters:
19467c478bd9Sstevel@tonic-gate * sp - Ptr to the si struct to generate the signature for.
19477c478bd9Sstevel@tonic-gate *
19487c478bd9Sstevel@tonic-gate * Returns: A signature for the si struct (really a checksum)
19497c478bd9Sstevel@tonic-gate */
19507c478bd9Sstevel@tonic-gate static int
si_signature(si_t * sp)19517c478bd9Sstevel@tonic-gate si_signature(si_t *sp)
19527c478bd9Sstevel@tonic-gate {
19537c478bd9Sstevel@tonic-gate int signature = sp->s_dev;
19547c478bd9Sstevel@tonic-gate
19557c478bd9Sstevel@tonic-gate signature += aclcksum(sp->aowner) + aclcksum(sp->agroup) +
19567c478bd9Sstevel@tonic-gate aclcksum(sp->aother) + aclcksum(sp->ausers) +
19577c478bd9Sstevel@tonic-gate aclcksum(sp->agroups) + aclcksum(sp->downer) +
19587c478bd9Sstevel@tonic-gate aclcksum(sp->dgroup) + aclcksum(sp->dother) +
19597c478bd9Sstevel@tonic-gate aclcksum(sp->dusers) + aclcksum(sp->dgroups);
19607c478bd9Sstevel@tonic-gate if (sp->aclass.acl_ismask)
19617c478bd9Sstevel@tonic-gate signature += sp->aclass.acl_maskbits;
19627c478bd9Sstevel@tonic-gate if (sp->dclass.acl_ismask)
19637c478bd9Sstevel@tonic-gate signature += sp->dclass.acl_maskbits;
19647c478bd9Sstevel@tonic-gate
19657c478bd9Sstevel@tonic-gate return (signature);
19667c478bd9Sstevel@tonic-gate }
19677c478bd9Sstevel@tonic-gate
19687c478bd9Sstevel@tonic-gate /*
19697c478bd9Sstevel@tonic-gate * aclcmp compares to acls to see if they are identical.
19707c478bd9Sstevel@tonic-gate *
19717c478bd9Sstevel@tonic-gate * sp1 is source
19727c478bd9Sstevel@tonic-gate * sp2 is sourceb
19737c478bd9Sstevel@tonic-gate *
19747c478bd9Sstevel@tonic-gate * returns 0 if equal and 1 if not equal
19757c478bd9Sstevel@tonic-gate */
19767c478bd9Sstevel@tonic-gate static int
aclcmp(ufs_ic_acl_t * aclin1p,ufs_ic_acl_t * aclin2p)19777c478bd9Sstevel@tonic-gate aclcmp(ufs_ic_acl_t *aclin1p, ufs_ic_acl_t *aclin2p)
19787c478bd9Sstevel@tonic-gate {
19797c478bd9Sstevel@tonic-gate ufs_ic_acl_t *aclp1;
19807c478bd9Sstevel@tonic-gate ufs_ic_acl_t *aclp2;
19817c478bd9Sstevel@tonic-gate
19827c478bd9Sstevel@tonic-gate /*
19837c478bd9Sstevel@tonic-gate * if the starting pointers are equal then they are equal so
19847c478bd9Sstevel@tonic-gate * just return.
19857c478bd9Sstevel@tonic-gate */
19867c478bd9Sstevel@tonic-gate if (aclin1p == aclin2p)
19877c478bd9Sstevel@tonic-gate return (0);
19887c478bd9Sstevel@tonic-gate /*
19897c478bd9Sstevel@tonic-gate * check element by element
19907c478bd9Sstevel@tonic-gate */
19917c478bd9Sstevel@tonic-gate for (aclp1 = aclin1p, aclp2 = aclin2p; aclp1 && aclp2;
19927c478bd9Sstevel@tonic-gate aclp1 = aclp1->acl_ic_next, aclp2 = aclp2->acl_ic_next) {
19937c478bd9Sstevel@tonic-gate if (aclp1->acl_ic_perm != aclp2->acl_ic_perm ||
19947c478bd9Sstevel@tonic-gate aclp1->acl_ic_who != aclp2->acl_ic_who)
19957c478bd9Sstevel@tonic-gate return (1);
19967c478bd9Sstevel@tonic-gate }
19977c478bd9Sstevel@tonic-gate /*
19987c478bd9Sstevel@tonic-gate * both must be zero (at the end of the acl)
19997c478bd9Sstevel@tonic-gate */
20007c478bd9Sstevel@tonic-gate if (aclp1 || aclp2)
20017c478bd9Sstevel@tonic-gate return (1);
20027c478bd9Sstevel@tonic-gate
20037c478bd9Sstevel@tonic-gate return (0);
20047c478bd9Sstevel@tonic-gate }
20057c478bd9Sstevel@tonic-gate
20067c478bd9Sstevel@tonic-gate /*
20077c478bd9Sstevel@tonic-gate * Do extensive, field-by-field compare of two si structures. Returns
20087c478bd9Sstevel@tonic-gate * 0 if they are exactly identical, 1 otherwise.
20097c478bd9Sstevel@tonic-gate *
20107c478bd9Sstevel@tonic-gate * Paramters:
20117c478bd9Sstevel@tonic-gate * sp1 - Ptr to 1st si struct
20127c478bd9Sstevel@tonic-gate * sp2 - Ptr to 2nd si struct
20137c478bd9Sstevel@tonic-gate *
20147c478bd9Sstevel@tonic-gate * Returns:
20157c478bd9Sstevel@tonic-gate * 0 - Not identical
20167c478bd9Sstevel@tonic-gate * 1 - Identical
20177c478bd9Sstevel@tonic-gate */
20187c478bd9Sstevel@tonic-gate static int
si_cmp(si_t * sp1,si_t * sp2)20197c478bd9Sstevel@tonic-gate si_cmp(si_t *sp1, si_t *sp2)
20207c478bd9Sstevel@tonic-gate {
20217c478bd9Sstevel@tonic-gate if (sp1->s_dev != sp2->s_dev)
20227c478bd9Sstevel@tonic-gate return (1);
20237c478bd9Sstevel@tonic-gate if (aclcmp(sp1->aowner, sp2->aowner) ||
20247c478bd9Sstevel@tonic-gate aclcmp(sp1->agroup, sp2->agroup) ||
20257c478bd9Sstevel@tonic-gate aclcmp(sp1->aother, sp2->aother) ||
20267c478bd9Sstevel@tonic-gate aclcmp(sp1->ausers, sp2->ausers) ||
20277c478bd9Sstevel@tonic-gate aclcmp(sp1->agroups, sp2->agroups) ||
20287c478bd9Sstevel@tonic-gate aclcmp(sp1->downer, sp2->downer) ||
20297c478bd9Sstevel@tonic-gate aclcmp(sp1->dgroup, sp2->dgroup) ||
20307c478bd9Sstevel@tonic-gate aclcmp(sp1->dother, sp2->dother) ||
20317c478bd9Sstevel@tonic-gate aclcmp(sp1->dusers, sp2->dusers) ||
20327c478bd9Sstevel@tonic-gate aclcmp(sp1->dgroups, sp2->dgroups))
20337c478bd9Sstevel@tonic-gate return (1);
20347c478bd9Sstevel@tonic-gate if (sp1->aclass.acl_ismask != sp2->aclass.acl_ismask)
20357c478bd9Sstevel@tonic-gate return (1);
20367c478bd9Sstevel@tonic-gate if (sp1->dclass.acl_ismask != sp2->dclass.acl_ismask)
20377c478bd9Sstevel@tonic-gate return (1);
20387c478bd9Sstevel@tonic-gate if (sp1->aclass.acl_ismask &&
20397c478bd9Sstevel@tonic-gate sp1->aclass.acl_maskbits != sp2->aclass.acl_maskbits)
20407c478bd9Sstevel@tonic-gate return (1);
20417c478bd9Sstevel@tonic-gate if (sp1->dclass.acl_ismask &&
20427c478bd9Sstevel@tonic-gate sp1->dclass.acl_maskbits != sp2->dclass.acl_maskbits)
20437c478bd9Sstevel@tonic-gate return (1);
20447c478bd9Sstevel@tonic-gate
20457c478bd9Sstevel@tonic-gate return (0);
20467c478bd9Sstevel@tonic-gate }
20477c478bd9Sstevel@tonic-gate
20487c478bd9Sstevel@tonic-gate /*
20497c478bd9Sstevel@tonic-gate * Remove all acls associated with a device. All acls must have
20507c478bd9Sstevel@tonic-gate * a reference count of zero.
20517c478bd9Sstevel@tonic-gate *
20527c478bd9Sstevel@tonic-gate * inputs:
20537c478bd9Sstevel@tonic-gate * device - device to remove from the cache
20547c478bd9Sstevel@tonic-gate *
20557c478bd9Sstevel@tonic-gate * outputs:
20567c478bd9Sstevel@tonic-gate * none
20577c478bd9Sstevel@tonic-gate */
20587c478bd9Sstevel@tonic-gate void
ufs_si_cache_flush(dev_t dev)20597c478bd9Sstevel@tonic-gate ufs_si_cache_flush(dev_t dev)
20607c478bd9Sstevel@tonic-gate {
20617c478bd9Sstevel@tonic-gate si_t *tsp, **tspp;
20627c478bd9Sstevel@tonic-gate int i;
20637c478bd9Sstevel@tonic-gate
20647c478bd9Sstevel@tonic-gate rw_enter(&si_cache_lock, RW_WRITER);
20657c478bd9Sstevel@tonic-gate for (i = 0; i < si_cachecnt; i++) {
20667c478bd9Sstevel@tonic-gate tspp = &si_cachea[i];
20677c478bd9Sstevel@tonic-gate while (*tspp) {
20687c478bd9Sstevel@tonic-gate if ((*tspp)->s_dev == dev) {
20697c478bd9Sstevel@tonic-gate *tspp = (*tspp)->s_next;
20707c478bd9Sstevel@tonic-gate } else {
20717c478bd9Sstevel@tonic-gate tspp = &(*tspp)->s_next;
20727c478bd9Sstevel@tonic-gate }
20737c478bd9Sstevel@tonic-gate }
20747c478bd9Sstevel@tonic-gate }
20757c478bd9Sstevel@tonic-gate for (i = 0; i < si_cachecnt; i++) {
20767c478bd9Sstevel@tonic-gate tspp = &si_cachei[i];
20777c478bd9Sstevel@tonic-gate while (*tspp) {
20787c478bd9Sstevel@tonic-gate if ((*tspp)->s_dev == dev) {
20797c478bd9Sstevel@tonic-gate tsp = *tspp;
20807c478bd9Sstevel@tonic-gate *tspp = (*tspp)->s_forw;
20817c478bd9Sstevel@tonic-gate tsp->s_flags &= ~SI_CACHED;
20827c478bd9Sstevel@tonic-gate ufs_si_free_mem(tsp);
20837c478bd9Sstevel@tonic-gate } else {
20847c478bd9Sstevel@tonic-gate tspp = &(*tspp)->s_forw;
20857c478bd9Sstevel@tonic-gate }
20867c478bd9Sstevel@tonic-gate }
20877c478bd9Sstevel@tonic-gate }
20887c478bd9Sstevel@tonic-gate rw_exit(&si_cache_lock);
20897c478bd9Sstevel@tonic-gate }
20907c478bd9Sstevel@tonic-gate
20917c478bd9Sstevel@tonic-gate /*
20927c478bd9Sstevel@tonic-gate * ufs_si_del is used to unhook a sp from a inode in memory
20937c478bd9Sstevel@tonic-gate *
20947c478bd9Sstevel@tonic-gate * ip is the inode to remove the sp from.
20957c478bd9Sstevel@tonic-gate */
20967c478bd9Sstevel@tonic-gate void
ufs_si_del(struct inode * ip)20977c478bd9Sstevel@tonic-gate ufs_si_del(struct inode *ip)
20987c478bd9Sstevel@tonic-gate {
20997c478bd9Sstevel@tonic-gate si_t *sp = ip->i_ufs_acl;
21007c478bd9Sstevel@tonic-gate int refcnt;
21017c478bd9Sstevel@tonic-gate int signature;
21027c478bd9Sstevel@tonic-gate
21037c478bd9Sstevel@tonic-gate if (sp) {
21047c478bd9Sstevel@tonic-gate rw_enter(&sp->s_lock, RW_WRITER);
21057c478bd9Sstevel@tonic-gate refcnt = --sp->s_ref;
21067c478bd9Sstevel@tonic-gate signature = sp->s_signature;
21077c478bd9Sstevel@tonic-gate ASSERT(sp->s_ref >= 0 && sp->s_ref <= sp->s_use);
21087c478bd9Sstevel@tonic-gate rw_exit(&sp->s_lock);
21097c478bd9Sstevel@tonic-gate if (refcnt == 0)
21107c478bd9Sstevel@tonic-gate si_cache_del(sp, signature);
21117c478bd9Sstevel@tonic-gate ip->i_ufs_acl = NULL;
21127c478bd9Sstevel@tonic-gate }
21137c478bd9Sstevel@tonic-gate }
2114