xref: /illumos-gate/usr/src/uts/common/fs/ufs/ufs_acl.c (revision 1f563eb1)
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 /*
2260c8e821SFrank 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
89*1f563eb1SToomas Soome  *		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 ?
23680d34432Sfrankho 	    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 ?
23980d34432Sfrankho 	    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
309*1f563eb1SToomas Soome 	 *		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
407*1f563eb1SToomas Soome  *		N - From errno.h
4087c478bd9Sstevel@tonic-gate  */
4097c478bd9Sstevel@tonic-gate /*
4107c478bd9Sstevel@tonic-gate  *	ip	parent inode in
4117c478bd9Sstevel@tonic-gate  *	cr	credentials in
4127c478bd9Sstevel@tonic-gate  */
413*1f563eb1SToomas Soome int
ufs_si_load(struct inode * ip,cred_t * cr)414*1f563eb1SToomas Soome ufs_si_load(struct inode *ip, cred_t *cr)
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;
420*1f563eb1SToomas Soome 	vsecattr_t	vsecattr = { 0, 0, NULL, 0, NULL};
4217c478bd9Sstevel@tonic-gate 	aclent_t	*aclp;
4227c478bd9Sstevel@tonic-gate 	ufs_acl_t	*ufsaclp;
4237c478bd9Sstevel@tonic-gate 	caddr_t		acldata = NULL;
4247c478bd9Sstevel@tonic-gate 	ino_t		maxino;
4257c478bd9Sstevel@tonic-gate 	int		err;
4267c478bd9Sstevel@tonic-gate 	size_t		acldatalen;
4277c478bd9Sstevel@tonic-gate 	int		numacls;
4287c478bd9Sstevel@tonic-gate 	int		shadow;
4297c478bd9Sstevel@tonic-gate 	int		usecnt;
4307c478bd9Sstevel@tonic-gate 	struct ufsvfs	*ufsvfsp	= ip->i_ufsvfs;
4317c478bd9Sstevel@tonic-gate 	struct fs	*fs		= ufsvfsp->vfs_fs;
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 	ASSERT(ip != NULL);
4347c478bd9Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&ip->i_contents));
4357c478bd9Sstevel@tonic-gate 	ASSERT(ip->i_shadow && ip->i_ufs_acl == NULL);
4367c478bd9Sstevel@tonic-gate 	ASSERT((ip->i_mode & IFMT) != IFSHAD);
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 	if (!CHECK_ACL_ALLOWED(ip->i_mode & IFMT))
4397c478bd9Sstevel@tonic-gate 		return (ENOSYS);
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate 	if (ip->i_shadow == ip->i_number)
4427c478bd9Sstevel@tonic-gate 		return (EIO);
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 	maxino = (ino_t)(ITOF(ip)->fs_ncg * ITOF(ip)->fs_ipg);
4457c478bd9Sstevel@tonic-gate 	if (ip->i_shadow < UFSROOTINO || ip->i_shadow > maxino)
4467c478bd9Sstevel@tonic-gate 		return (EIO);
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	/*
4497c478bd9Sstevel@tonic-gate 	 * XXX Check cache.  If in cache, link to it and increment
4507c478bd9Sstevel@tonic-gate 	 * the reference count, then return.
4517c478bd9Sstevel@tonic-gate 	 */
4527c478bd9Sstevel@tonic-gate 	if (si_cachei_get(ip, &sp) == 0) {
4537c478bd9Sstevel@tonic-gate 		ASSERT(RW_WRITE_HELD(&sp->s_lock));
4547c478bd9Sstevel@tonic-gate 		ip->i_ufs_acl = sp;
4557c478bd9Sstevel@tonic-gate 		sp->s_ref++;
4567c478bd9Sstevel@tonic-gate 		ASSERT(sp->s_ref >= 0 && sp->s_ref <= sp->s_use);
4577c478bd9Sstevel@tonic-gate 		rw_exit(&sp->s_lock);
4587c478bd9Sstevel@tonic-gate 		si_cachehit++;
4597c478bd9Sstevel@tonic-gate 		return (0);
4607c478bd9Sstevel@tonic-gate 	}
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 	/* Get the shadow inode */
4637c478bd9Sstevel@tonic-gate 	vfsp = ITOV(ip)->v_vfsp;
4647c478bd9Sstevel@tonic-gate 	shadow = ip->i_shadow;
4657c478bd9Sstevel@tonic-gate 	if ((err = ufs_iget_alloced(vfsp, shadow, &sip, cr)) != 0) {
4667c478bd9Sstevel@tonic-gate 		return (err);
4677c478bd9Sstevel@tonic-gate 	}
4687c478bd9Sstevel@tonic-gate 	rw_enter(&sip->i_contents, RW_WRITER);
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	if ((sip->i_mode & IFMT) != IFSHAD) {
4717c478bd9Sstevel@tonic-gate 		rw_exit(&sip->i_contents);
4727c478bd9Sstevel@tonic-gate 		err = EINVAL;
4737c478bd9Sstevel@tonic-gate 		goto alldone;
4747c478bd9Sstevel@tonic-gate 	}
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 	ASSERT(sip->i_dquot == 0);
4777c478bd9Sstevel@tonic-gate 	usecnt = sip->i_nlink;
4787c478bd9Sstevel@tonic-gate 	if ((!ULOCKFS_IS_NOIACC(&ufsvfsp->vfs_ulockfs)) &&
4797c478bd9Sstevel@tonic-gate 	    (!(sip)->i_ufsvfs->vfs_noatime)) {
4807c478bd9Sstevel@tonic-gate 		sip->i_flag |= IACC;
4817c478bd9Sstevel@tonic-gate 	}
4827c478bd9Sstevel@tonic-gate 	rw_downgrade(&sip->i_contents);
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 	ASSERT(sip->i_size <= MAXOFF_T);
4857c478bd9Sstevel@tonic-gate 	/* Read the acl's and other stuff from disk */
4867c478bd9Sstevel@tonic-gate 	acldata	 = kmem_zalloc((size_t)sip->i_size, KM_SLEEP);
4877c478bd9Sstevel@tonic-gate 	acldatalen = sip->i_size;
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate 	err = ufs_rdwri(UIO_READ, FREAD, sip, acldata, acldatalen, (offset_t)0,
4907c478bd9Sstevel@tonic-gate 	    UIO_SYSSPACE, (int *)0, cr);
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate 	rw_exit(&sip->i_contents);
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate 	if (err)
4957c478bd9Sstevel@tonic-gate 		goto alldone;
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 	/*
4987c478bd9Sstevel@tonic-gate 	 * Convert from disk format
4997c478bd9Sstevel@tonic-gate 	 * Result is a vsecattr struct which we then convert to the
5007c478bd9Sstevel@tonic-gate 	 * si struct.
5017c478bd9Sstevel@tonic-gate 	 */
5027c478bd9Sstevel@tonic-gate 	bzero((caddr_t)&vsecattr, sizeof (vsecattr_t));
5037c478bd9Sstevel@tonic-gate 	for (fsdp = (ufs_fsd_t *)acldata;
504*1f563eb1SToomas Soome 	    fsdp < (ufs_fsd_t *)(acldata + acldatalen);
505*1f563eb1SToomas Soome 	    fsdp = (ufs_fsd_t *)((caddr_t)fsdp +
506*1f563eb1SToomas Soome 	    FSD_RECSZ(fsdp, fsdp->fsd_size))) {
5077c478bd9Sstevel@tonic-gate 		if (fsdp->fsd_size <= 0)
5087c478bd9Sstevel@tonic-gate 			break;
5097c478bd9Sstevel@tonic-gate 		switch (fsdp->fsd_type) {
5107c478bd9Sstevel@tonic-gate 		case FSD_ACL:
5117c478bd9Sstevel@tonic-gate 			numacls = vsecattr.vsa_aclcnt =
512*1f563eb1SToomas Soome 			    (int)((fsdp->fsd_size -
513*1f563eb1SToomas Soome 			    2 * sizeof (int)) / sizeof (ufs_acl_t));
5147c478bd9Sstevel@tonic-gate 			aclp = vsecattr.vsa_aclentp =
515*1f563eb1SToomas Soome 			    kmem_zalloc(numacls * sizeof (aclent_t), KM_SLEEP);
5167c478bd9Sstevel@tonic-gate 			for (ufsaclp = (ufs_acl_t *)fsdp->fsd_data;
517*1f563eb1SToomas Soome 			    numacls; ufsaclp++) {
5187c478bd9Sstevel@tonic-gate 				aclp->a_type = ufsaclp->acl_tag;
5197c478bd9Sstevel@tonic-gate 				aclp->a_id = ufsaclp->acl_who;
5207c478bd9Sstevel@tonic-gate 				aclp->a_perm = ufsaclp->acl_perm;
5217c478bd9Sstevel@tonic-gate 				aclp++;
5227c478bd9Sstevel@tonic-gate 				numacls--;
5237c478bd9Sstevel@tonic-gate 			}
5247c478bd9Sstevel@tonic-gate 			break;
5257c478bd9Sstevel@tonic-gate 		case FSD_DFACL:
5267c478bd9Sstevel@tonic-gate 			numacls = vsecattr.vsa_dfaclcnt =
527*1f563eb1SToomas Soome 			    (int)((fsdp->fsd_size -
528*1f563eb1SToomas Soome 			    2 * sizeof (int)) / sizeof (ufs_acl_t));
5297c478bd9Sstevel@tonic-gate 			aclp = vsecattr.vsa_dfaclentp =
530*1f563eb1SToomas Soome 			    kmem_zalloc(numacls * sizeof (aclent_t), KM_SLEEP);
5317c478bd9Sstevel@tonic-gate 			for (ufsaclp = (ufs_acl_t *)fsdp->fsd_data;
532*1f563eb1SToomas Soome 			    numacls; ufsaclp++) {
5337c478bd9Sstevel@tonic-gate 				aclp->a_type = ufsaclp->acl_tag;
5347c478bd9Sstevel@tonic-gate 				aclp->a_id = ufsaclp->acl_who;
5357c478bd9Sstevel@tonic-gate 				aclp->a_perm = ufsaclp->acl_perm;
5367c478bd9Sstevel@tonic-gate 				aclp++;
5377c478bd9Sstevel@tonic-gate 				numacls--;
5387c478bd9Sstevel@tonic-gate 			}
5397c478bd9Sstevel@tonic-gate 			break;
5407c478bd9Sstevel@tonic-gate 		}
5417c478bd9Sstevel@tonic-gate 	}
5427c478bd9Sstevel@tonic-gate 	/* Sort the lists */
5437c478bd9Sstevel@tonic-gate 	if (vsecattr.vsa_aclentp) {
5447c478bd9Sstevel@tonic-gate 		ksort((caddr_t)vsecattr.vsa_aclentp, vsecattr.vsa_aclcnt,
545*1f563eb1SToomas Soome 		    sizeof (aclent_t), cmp2acls);
5467c478bd9Sstevel@tonic-gate 		if ((err = acl_validate(vsecattr.vsa_aclentp,
547*1f563eb1SToomas Soome 		    vsecattr.vsa_aclcnt, ACL_CHECK)) != 0) {
5487c478bd9Sstevel@tonic-gate 			goto alldone;
5497c478bd9Sstevel@tonic-gate 		}
5507c478bd9Sstevel@tonic-gate 	}
5517c478bd9Sstevel@tonic-gate 	if (vsecattr.vsa_dfaclentp) {
5527c478bd9Sstevel@tonic-gate 		ksort((caddr_t)vsecattr.vsa_dfaclentp, vsecattr.vsa_dfaclcnt,
553*1f563eb1SToomas Soome 		    sizeof (aclent_t), cmp2acls);
5547c478bd9Sstevel@tonic-gate 		if ((err = acl_validate(vsecattr.vsa_dfaclentp,
555*1f563eb1SToomas Soome 		    vsecattr.vsa_dfaclcnt, DEF_ACL_CHECK)) != 0) {
5567c478bd9Sstevel@tonic-gate 			goto alldone;
5577c478bd9Sstevel@tonic-gate 		}
5587c478bd9Sstevel@tonic-gate 	}
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate 	/* ignore shadow inodes without ACLs */
5617c478bd9Sstevel@tonic-gate 	if (!vsecattr.vsa_aclentp && !vsecattr.vsa_dfaclentp) {
5627c478bd9Sstevel@tonic-gate 		err = 0;
5637c478bd9Sstevel@tonic-gate 		goto alldone;
5647c478bd9Sstevel@tonic-gate 	}
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 	/* Convert from vsecattr struct to ufs_acl_entry struct */
5677c478bd9Sstevel@tonic-gate 	if ((err = vsecattr2aclentry(&vsecattr, &sp)) != 0) {
5687c478bd9Sstevel@tonic-gate 		goto alldone;
5697c478bd9Sstevel@tonic-gate 	}
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 	/* There aren't filled in by vsecattr2aclentry */
5727c478bd9Sstevel@tonic-gate 	sp->s_shadow = ip->i_shadow;
5737c478bd9Sstevel@tonic-gate 	sp->s_dev = ip->i_dev;
5747c478bd9Sstevel@tonic-gate 	sp->s_use = usecnt;
5757c478bd9Sstevel@tonic-gate 	sp->s_ref = 1;
5767c478bd9Sstevel@tonic-gate 	ASSERT(sp->s_ref >= 0 && sp->s_ref <= sp->s_use);
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate 	/* XXX Might make a duplicate */
5797c478bd9Sstevel@tonic-gate 	si_cache_put(sp);
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate 	/* Signal anyone waiting on this shadow to be loaded */
5827c478bd9Sstevel@tonic-gate 	ip->i_ufs_acl = sp;
5837c478bd9Sstevel@tonic-gate 	err = 0;
5847c478bd9Sstevel@tonic-gate 	si_cachemiss++;
5857c478bd9Sstevel@tonic-gate 	if ((acldatalen + fs->fs_bsize) > ufsvfsp->vfs_maxacl)
5867c478bd9Sstevel@tonic-gate 		ufsvfsp->vfs_maxacl = acldatalen + fs->fs_bsize;
5877c478bd9Sstevel@tonic-gate alldone:
5887c478bd9Sstevel@tonic-gate 	/*
5897c478bd9Sstevel@tonic-gate 	 * Common exit point. Mark shadow inode as ISTALE
5907c478bd9Sstevel@tonic-gate 	 * if we detect an internal inconsistency, to
5917c478bd9Sstevel@tonic-gate 	 * prevent stray inodes appearing in the cache.
5927c478bd9Sstevel@tonic-gate 	 */
5937c478bd9Sstevel@tonic-gate 	if (err) {
5947c478bd9Sstevel@tonic-gate 		rw_enter(&sip->i_contents, RW_READER);
5957c478bd9Sstevel@tonic-gate 		mutex_enter(&sip->i_tlock);
5967c478bd9Sstevel@tonic-gate 		sip->i_flag |= ISTALE;
5977c478bd9Sstevel@tonic-gate 		mutex_exit(&sip->i_tlock);
5987c478bd9Sstevel@tonic-gate 		rw_exit(&sip->i_contents);
5997c478bd9Sstevel@tonic-gate 	}
6007c478bd9Sstevel@tonic-gate 	VN_RELE(ITOV(sip));
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate 	/*
6037c478bd9Sstevel@tonic-gate 	 * Cleanup of data structures allocated
6047c478bd9Sstevel@tonic-gate 	 * on the fly.
6057c478bd9Sstevel@tonic-gate 	 */
6067c478bd9Sstevel@tonic-gate 	if (acldata)
6077c478bd9Sstevel@tonic-gate 		kmem_free(acldata, acldatalen);
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate 	if (vsecattr.vsa_aclentp)
6107c478bd9Sstevel@tonic-gate 		kmem_free(vsecattr.vsa_aclentp,
611*1f563eb1SToomas Soome 		    vsecattr.vsa_aclcnt * sizeof (aclent_t));
6127c478bd9Sstevel@tonic-gate 	if (vsecattr.vsa_dfaclentp)
6137c478bd9Sstevel@tonic-gate 		kmem_free(vsecattr.vsa_dfaclentp,
614*1f563eb1SToomas Soome 		    vsecattr.vsa_dfaclcnt * sizeof (aclent_t));
6157c478bd9Sstevel@tonic-gate 	return (err);
6167c478bd9Sstevel@tonic-gate }
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate /*
6197c478bd9Sstevel@tonic-gate  * Check the inode's ACL's to see if this mode of access is
6207c478bd9Sstevel@tonic-gate  * allowed; return 0 if allowed, EACCES if not.
6217c478bd9Sstevel@tonic-gate  *
6227c478bd9Sstevel@tonic-gate  * We follow the procedure defined in Sec. 3.3.5, ACL Access
6237c478bd9Sstevel@tonic-gate  * Check Algorithm, of the POSIX 1003.6 Draft Standard.
6247c478bd9Sstevel@tonic-gate  */
6257c478bd9Sstevel@tonic-gate /*
626*1f563eb1SToomas Soome  *	ip	parent inode
627*1f563eb1SToomas Soome  *	mode	mode of access read, write, execute/examine
6287c478bd9Sstevel@tonic-gate  *	cr	credentials
6297c478bd9Sstevel@tonic-gate  */
630*1f563eb1SToomas Soome int
ufs_acl_access(struct inode * ip,int mode,cred_t * cr)631*1f563eb1SToomas Soome ufs_acl_access(struct inode *ip, int mode, cred_t *cr)
6327c478bd9Sstevel@tonic-gate {
6337c478bd9Sstevel@tonic-gate 	ufs_ic_acl_t *acl;
6347c478bd9Sstevel@tonic-gate 	int ismask, mask = 0;
6357c478bd9Sstevel@tonic-gate 	int gperm = 0;
6367c478bd9Sstevel@tonic-gate 	int ngroup = 0;
6377c478bd9Sstevel@tonic-gate 	si_t	*sp = NULL;
6387c478bd9Sstevel@tonic-gate 	uid_t uid = crgetuid(cr);
6397c478bd9Sstevel@tonic-gate 	uid_t owner;
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 	ASSERT(ip->i_ufs_acl != NULL);
64260c8e821SFrank Batschulat 	ASSERT(RW_LOCK_HELD(&ip->i_contents));
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate 	sp = ip->i_ufs_acl;
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate 	ismask = sp->aclass.acl_ismask ?
647*1f563eb1SToomas Soome 	    sp->aclass.acl_ismask : 0;
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate 	if (ismask)
6507c478bd9Sstevel@tonic-gate 		mask = sp->aclass.acl_maskbits;
6517c478bd9Sstevel@tonic-gate 	else
6527c478bd9Sstevel@tonic-gate 		mask = -1;
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate 	/*
6557c478bd9Sstevel@tonic-gate 	 * (1) If user owns the file, obey user mode bits
6567c478bd9Sstevel@tonic-gate 	 */
6577c478bd9Sstevel@tonic-gate 	owner = sp->aowner->acl_ic_who;
6587c478bd9Sstevel@tonic-gate 	if (uid == owner) {
6597c478bd9Sstevel@tonic-gate 		return (MODE_CHECK(owner, mode, (sp->aowner->acl_ic_perm << 6),
660*1f563eb1SToomas Soome 		    cr, ip));
6617c478bd9Sstevel@tonic-gate 	}
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate 	/*
6647c478bd9Sstevel@tonic-gate 	 * (2) Obey any matching ACL_USER entry
6657c478bd9Sstevel@tonic-gate 	 */
6667c478bd9Sstevel@tonic-gate 	if (sp->ausers)
6677c478bd9Sstevel@tonic-gate 		for (acl = sp->ausers; acl != NULL; acl = acl->acl_ic_next) {
6687c478bd9Sstevel@tonic-gate 			if (acl->acl_ic_who == uid) {
6697c478bd9Sstevel@tonic-gate 				return (MODE_CHECK(owner, mode,
6707c478bd9Sstevel@tonic-gate 				    (mask & acl->acl_ic_perm) << 6, cr, ip));
6717c478bd9Sstevel@tonic-gate 			}
6727c478bd9Sstevel@tonic-gate 		}
6737c478bd9Sstevel@tonic-gate 
6747c478bd9Sstevel@tonic-gate 	/*
6757c478bd9Sstevel@tonic-gate 	 * (3) If user belongs to file's group, obey group mode bits
6767c478bd9Sstevel@tonic-gate 	 * if no ACL mask is defined; if there is an ACL mask, we look
6777c478bd9Sstevel@tonic-gate 	 * at both the group mode bits and any ACL_GROUP entries.
6787c478bd9Sstevel@tonic-gate 	 */
6797c478bd9Sstevel@tonic-gate 	if (groupmember((uid_t)sp->agroup->acl_ic_who, cr)) {
6807c478bd9Sstevel@tonic-gate 		ngroup++;
6817c478bd9Sstevel@tonic-gate 		gperm = (sp->agroup->acl_ic_perm);
6827c478bd9Sstevel@tonic-gate 		if (!ismask)
6837c478bd9Sstevel@tonic-gate 			return (MODE_CHECK(owner, mode, (gperm << 6), cr, ip));
6847c478bd9Sstevel@tonic-gate 	}
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate 	/*
6877c478bd9Sstevel@tonic-gate 	 * (4) Accumulate the permissions in matching ACL_GROUP entries
6887c478bd9Sstevel@tonic-gate 	 */
6897c478bd9Sstevel@tonic-gate 	if (sp->agroups)
690*1f563eb1SToomas Soome 		for (acl = sp->agroups; acl != NULL; acl = acl->acl_ic_next) {
6917c478bd9Sstevel@tonic-gate 			if (groupmember(acl->acl_ic_who, cr)) {
6927c478bd9Sstevel@tonic-gate 				ngroup++;
6937c478bd9Sstevel@tonic-gate 				gperm |= acl->acl_ic_perm;
6947c478bd9Sstevel@tonic-gate 			}
6957c478bd9Sstevel@tonic-gate 		}
6967c478bd9Sstevel@tonic-gate 
6977c478bd9Sstevel@tonic-gate 	if (ngroup != 0)
6987c478bd9Sstevel@tonic-gate 		return (MODE_CHECK(owner, mode, ((gperm & mask) << 6), cr, ip));
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate 	/*
7017c478bd9Sstevel@tonic-gate 	 * (5) Finally, use the "other" mode bits
7027c478bd9Sstevel@tonic-gate 	 */
7037c478bd9Sstevel@tonic-gate 	return (MODE_CHECK(owner, mode, sp->aother->acl_ic_perm << 6, cr, ip));
7047c478bd9Sstevel@tonic-gate }
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate /*ARGSUSED2*/
7077c478bd9Sstevel@tonic-gate int
ufs_acl_get(struct inode * ip,vsecattr_t * vsap,int flag,cred_t * cr)7087c478bd9Sstevel@tonic-gate ufs_acl_get(struct inode *ip, vsecattr_t *vsap, int flag, cred_t *cr)
7097c478bd9Sstevel@tonic-gate {
7107c478bd9Sstevel@tonic-gate 	aclent_t	*aclentp;
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate 	ASSERT(RW_LOCK_HELD(&ip->i_contents));
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate 	/* XXX Range check, sanity check, shadow check */
7157c478bd9Sstevel@tonic-gate 	/* If an ACL is present, get the data from the shadow inode info */
7167c478bd9Sstevel@tonic-gate 	if (ip->i_ufs_acl)
7177c478bd9Sstevel@tonic-gate 		return (aclentry2vsecattr(ip->i_ufs_acl, vsap));
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate 	/*
7207c478bd9Sstevel@tonic-gate 	 * If no ACLs are present, fabricate one from the mode bits.
7217c478bd9Sstevel@tonic-gate 	 * This code is almost identical to fs_fab_acl(), but we
7227c478bd9Sstevel@tonic-gate 	 * already have the mode bits handy, so we'll avoid going
7237c478bd9Sstevel@tonic-gate 	 * through VOP_GETATTR() again.
7247c478bd9Sstevel@tonic-gate 	 */
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate 	vsap->vsa_aclcnt    = 0;
7277c478bd9Sstevel@tonic-gate 	vsap->vsa_aclentp   = NULL;
7287c478bd9Sstevel@tonic-gate 	vsap->vsa_dfaclcnt  = 0;	/* Default ACLs are not fabricated */
7297c478bd9Sstevel@tonic-gate 	vsap->vsa_dfaclentp = NULL;
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate 	if (vsap->vsa_mask & (VSA_ACLCNT | VSA_ACL))
7327c478bd9Sstevel@tonic-gate 		vsap->vsa_aclcnt    = 4;  /* USER, GROUP, OTHER, and CLASS */
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate 	if (vsap->vsa_mask & VSA_ACL) {
7357c478bd9Sstevel@tonic-gate 		vsap->vsa_aclentp = kmem_zalloc(4 * sizeof (aclent_t),
7367c478bd9Sstevel@tonic-gate 		    KM_SLEEP);
7377c478bd9Sstevel@tonic-gate 		if (vsap->vsa_aclentp == NULL)
7387c478bd9Sstevel@tonic-gate 			return (ENOMEM);
7397c478bd9Sstevel@tonic-gate 		aclentp = vsap->vsa_aclentp;
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate 		/* Owner */
7427c478bd9Sstevel@tonic-gate 		aclentp->a_type = USER_OBJ;
7437c478bd9Sstevel@tonic-gate 		aclentp->a_perm = ((ushort_t)(ip->i_mode & 0700)) >> 6;
7447c478bd9Sstevel@tonic-gate 		aclentp->a_id = ip->i_uid;	/* Really undefined */
7457c478bd9Sstevel@tonic-gate 		aclentp++;
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate 		/* Group */
7487c478bd9Sstevel@tonic-gate 		aclentp->a_type = GROUP_OBJ;
7497c478bd9Sstevel@tonic-gate 		aclentp->a_perm = ((ushort_t)(ip->i_mode & 0070)) >> 3;
750*1f563eb1SToomas Soome 		aclentp->a_id = ip->i_gid;	/* Really undefined */
7517c478bd9Sstevel@tonic-gate 		aclentp++;
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate 		/* Other */
7547c478bd9Sstevel@tonic-gate 		aclentp->a_type = OTHER_OBJ;
7557c478bd9Sstevel@tonic-gate 		aclentp->a_perm = ip->i_mode & 0007;
7567c478bd9Sstevel@tonic-gate 		aclentp->a_id = 0;		/* Really undefined */
7577c478bd9Sstevel@tonic-gate 		aclentp++;
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate 		/* Class */
7607c478bd9Sstevel@tonic-gate 		aclentp->a_type = CLASS_OBJ;
7617c478bd9Sstevel@tonic-gate 		aclentp->a_perm = ((ushort_t)(ip->i_mode & 0070)) >> 3;
7627c478bd9Sstevel@tonic-gate 		aclentp->a_id = 0;		/* Really undefined */
7637c478bd9Sstevel@tonic-gate 		ksort((caddr_t)vsap->vsa_aclentp, vsap->vsa_aclcnt,
76480d34432Sfrankho 		    sizeof (aclent_t), cmp2acls);
7657c478bd9Sstevel@tonic-gate 	}
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate 	return (0);
7687c478bd9Sstevel@tonic-gate }
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate /*ARGSUSED2*/
7717c478bd9Sstevel@tonic-gate int
ufs_acl_set(struct inode * ip,vsecattr_t * vsap,int flag,cred_t * cr)7727c478bd9Sstevel@tonic-gate ufs_acl_set(struct inode *ip, vsecattr_t *vsap, int flag, cred_t *cr)
7737c478bd9Sstevel@tonic-gate {
7747c478bd9Sstevel@tonic-gate 	si_t	*sp;
7757c478bd9Sstevel@tonic-gate 	int	err;
7767c478bd9Sstevel@tonic-gate 
7777c478bd9Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&ip->i_contents));
7787c478bd9Sstevel@tonic-gate 
7797c478bd9Sstevel@tonic-gate 	if (!CHECK_ACL_ALLOWED(ip->i_mode & IFMT))
7807c478bd9Sstevel@tonic-gate 		return (ENOSYS);
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate 	/*
7837c478bd9Sstevel@tonic-gate 	 * only the owner of the file or privileged users can change the ACLs
7847c478bd9Sstevel@tonic-gate 	 */
7857c478bd9Sstevel@tonic-gate 	if (secpolicy_vnode_setdac(cr, ip->i_uid) != 0)
7867c478bd9Sstevel@tonic-gate 		return (EPERM);
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate 	/* Convert from vsecattr struct to ufs_acl_entry struct */
7897c478bd9Sstevel@tonic-gate 	if ((err = vsecattr2aclentry(vsap, &sp)) != 0)
7907c478bd9Sstevel@tonic-gate 		return (err);
7917c478bd9Sstevel@tonic-gate 	sp->s_dev = ip->i_dev;
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate 	/*
7947c478bd9Sstevel@tonic-gate 	 * Make the user & group objs in the acl list follow what's
7957c478bd9Sstevel@tonic-gate 	 * in the inode.
7967c478bd9Sstevel@tonic-gate 	 */
7977c478bd9Sstevel@tonic-gate #ifdef DEBUG
7987c478bd9Sstevel@tonic-gate 	if (vsap->vsa_mask == VSA_ACL) {
7997c478bd9Sstevel@tonic-gate 		ASSERT(sp->aowner);
8007c478bd9Sstevel@tonic-gate 		ASSERT(sp->agroup);
8017c478bd9Sstevel@tonic-gate 		ASSERT(sp->aother);
8027c478bd9Sstevel@tonic-gate 	}
8037c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate 	if (sp->aowner)
8067c478bd9Sstevel@tonic-gate 		sp->aowner->acl_ic_who = ip->i_uid;
8077c478bd9Sstevel@tonic-gate 	if (sp->agroup)
8087c478bd9Sstevel@tonic-gate 		sp->agroup->acl_ic_who = ip->i_gid;
8097c478bd9Sstevel@tonic-gate 
8107c478bd9Sstevel@tonic-gate 	/*
8117c478bd9Sstevel@tonic-gate 	 * Write and cache the new acl list
8127c478bd9Sstevel@tonic-gate 	 */
8137c478bd9Sstevel@tonic-gate 	err = ufs_si_store(ip, sp, 1, cr);
8147c478bd9Sstevel@tonic-gate 
8157c478bd9Sstevel@tonic-gate 	return (err);
8167c478bd9Sstevel@tonic-gate }
8177c478bd9Sstevel@tonic-gate 
8187c478bd9Sstevel@tonic-gate /*
8197c478bd9Sstevel@tonic-gate  * XXX Scan sorted array of acl's, checking for:
8207c478bd9Sstevel@tonic-gate  * 1) Any duplicate/conflicting entries (same type and id)
8217c478bd9Sstevel@tonic-gate  * 2) More than 1 of USER_OBJ, GROUP_OBJ, OTHER_OBJ, CLASS_OBJ
8227c478bd9Sstevel@tonic-gate  * 3) More than 1 of DEF_USER_OBJ, DEF_GROUP_OBJ, DEF_OTHER_OBJ, DEF_CLASS_OBJ
8237c478bd9Sstevel@tonic-gate  *
8247c478bd9Sstevel@tonic-gate  * Parameters:
8257c478bd9Sstevel@tonic-gate  * aclentp - ptr to sorted list of acl entries.
8267c478bd9Sstevel@tonic-gate  * nentries - # acl entries on the list
8277c478bd9Sstevel@tonic-gate  * flag - Bitmap (ACL_CHECK and/or DEF_ACL_CHECK) indicating whether the
8287c478bd9Sstevel@tonic-gate  * list contains regular acls, default acls, or both.
8297c478bd9Sstevel@tonic-gate  *
8307c478bd9Sstevel@tonic-gate  * Returns:	0 - Success
8317c478bd9Sstevel@tonic-gate  * EINVAL - Invalid list (dups or multiple entries of type USER_OBJ, etc)
8327c478bd9Sstevel@tonic-gate  */
8337c478bd9Sstevel@tonic-gate static int
acl_validate(aclent_t * aclentp,int nentries,int flag)8347c478bd9Sstevel@tonic-gate acl_validate(aclent_t *aclentp, int nentries, int flag)
8357c478bd9Sstevel@tonic-gate {
8367c478bd9Sstevel@tonic-gate 	int	i;
8377c478bd9Sstevel@tonic-gate 	int	nuser_objs = 0;
8387c478bd9Sstevel@tonic-gate 	int	ngroup_objs = 0;
8397c478bd9Sstevel@tonic-gate 	int	nother_objs = 0;
8407c478bd9Sstevel@tonic-gate 	int	nclass_objs = 0;
8417c478bd9Sstevel@tonic-gate 	int	ndef_user_objs = 0;
8427c478bd9Sstevel@tonic-gate 	int	ndef_group_objs = 0;
8437c478bd9Sstevel@tonic-gate 	int	ndef_other_objs = 0;
8447c478bd9Sstevel@tonic-gate 	int	ndef_class_objs = 0;
8457c478bd9Sstevel@tonic-gate 	int	nusers = 0;
8467c478bd9Sstevel@tonic-gate 	int	ngroups = 0;
8477c478bd9Sstevel@tonic-gate 	int	ndef_users = 0;
8487c478bd9Sstevel@tonic-gate 	int	ndef_groups = 0;
8497c478bd9Sstevel@tonic-gate 	int	numdefs = 0;
8507c478bd9Sstevel@tonic-gate 
8517c478bd9Sstevel@tonic-gate 	/* Null list or list of one */
8527c478bd9Sstevel@tonic-gate 	if (aclentp == NULL)
8537c478bd9Sstevel@tonic-gate 		return (0);
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate 	if (nentries <= 0)
8567c478bd9Sstevel@tonic-gate 		return (EINVAL);
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate 	for (i = 1; i < nentries; i++) {
8597c478bd9Sstevel@tonic-gate 		if (((aclentp[i - 1].a_type == aclentp[i].a_type) &&
8607c478bd9Sstevel@tonic-gate 		    (aclentp[i - 1].a_id   == aclentp[i].a_id)) ||
8617c478bd9Sstevel@tonic-gate 		    (aclentp[i - 1].a_perm > 07)) {
8627c478bd9Sstevel@tonic-gate 			return (EINVAL);
8637c478bd9Sstevel@tonic-gate 		}
8647c478bd9Sstevel@tonic-gate 	}
8657c478bd9Sstevel@tonic-gate 
8667c478bd9Sstevel@tonic-gate 	if (flag == 0 || (flag != ACL_CHECK && flag != DEF_ACL_CHECK))
8677c478bd9Sstevel@tonic-gate 		return (EINVAL);
8687c478bd9Sstevel@tonic-gate 
8697c478bd9Sstevel@tonic-gate 	/* Count types */
8707c478bd9Sstevel@tonic-gate 	for (i = 0; i < nentries; i++) {
8717c478bd9Sstevel@tonic-gate 		switch (aclentp[i].a_type) {
8727c478bd9Sstevel@tonic-gate 		case USER_OBJ:		/* Owner */
8737c478bd9Sstevel@tonic-gate 			nuser_objs++;
8747c478bd9Sstevel@tonic-gate 			break;
8757c478bd9Sstevel@tonic-gate 		case GROUP_OBJ:		/* Group */
8767c478bd9Sstevel@tonic-gate 			ngroup_objs++;
8777c478bd9Sstevel@tonic-gate 			break;
8787c478bd9Sstevel@tonic-gate 		case OTHER_OBJ:		/* Other */
8797c478bd9Sstevel@tonic-gate 			nother_objs++;
8807c478bd9Sstevel@tonic-gate 			break;
8817c478bd9Sstevel@tonic-gate 		case CLASS_OBJ:		/* Mask */
8827c478bd9Sstevel@tonic-gate 			nclass_objs++;
8837c478bd9Sstevel@tonic-gate 			break;
8847c478bd9Sstevel@tonic-gate 		case DEF_USER_OBJ:	/* Default Owner */
8857c478bd9Sstevel@tonic-gate 			ndef_user_objs++;
8867c478bd9Sstevel@tonic-gate 			break;
8877c478bd9Sstevel@tonic-gate 		case DEF_GROUP_OBJ:	/* Default Group */
8887c478bd9Sstevel@tonic-gate 			ndef_group_objs++;
8897c478bd9Sstevel@tonic-gate 			break;
8907c478bd9Sstevel@tonic-gate 		case DEF_OTHER_OBJ:	/* Default Other */
8917c478bd9Sstevel@tonic-gate 			ndef_other_objs++;
8927c478bd9Sstevel@tonic-gate 			break;
8937c478bd9Sstevel@tonic-gate 		case DEF_CLASS_OBJ:	/* Default Mask */
8947c478bd9Sstevel@tonic-gate 			ndef_class_objs++;
8957c478bd9Sstevel@tonic-gate 			break;
8967c478bd9Sstevel@tonic-gate 		case USER:		/* Users */
8977c478bd9Sstevel@tonic-gate 			nusers++;
8987c478bd9Sstevel@tonic-gate 			break;
8997c478bd9Sstevel@tonic-gate 		case GROUP:		/* Groups */
9007c478bd9Sstevel@tonic-gate 			ngroups++;
9017c478bd9Sstevel@tonic-gate 			break;
9027c478bd9Sstevel@tonic-gate 		case DEF_USER:		/* Default Users */
9037c478bd9Sstevel@tonic-gate 			ndef_users++;
9047c478bd9Sstevel@tonic-gate 			break;
9057c478bd9Sstevel@tonic-gate 		case DEF_GROUP:		/* Default Groups */
9067c478bd9Sstevel@tonic-gate 			ndef_groups++;
9077c478bd9Sstevel@tonic-gate 			break;
9087c478bd9Sstevel@tonic-gate 		default:		/* Unknown type */
9097c478bd9Sstevel@tonic-gate 			return (EINVAL);
9107c478bd9Sstevel@tonic-gate 		}
9117c478bd9Sstevel@tonic-gate 	}
9127c478bd9Sstevel@tonic-gate 
9137c478bd9Sstevel@tonic-gate 	/*
9147c478bd9Sstevel@tonic-gate 	 * For normal acl's, we require there be one (and only one)
9157c478bd9Sstevel@tonic-gate 	 * USER_OBJ, GROUP_OBJ and OTHER_OBJ.  There is either zero
9167c478bd9Sstevel@tonic-gate 	 * or one CLASS_OBJ.
9177c478bd9Sstevel@tonic-gate 	 */
9187c478bd9Sstevel@tonic-gate 	if (flag & ACL_CHECK) {
9197c478bd9Sstevel@tonic-gate 		if (nuser_objs != 1 || ngroup_objs != 1 ||
9207c478bd9Sstevel@tonic-gate 		    nother_objs != 1 || nclass_objs > 1) {
9217c478bd9Sstevel@tonic-gate 			return (EINVAL);
9227c478bd9Sstevel@tonic-gate 		}
9237c478bd9Sstevel@tonic-gate 		/*
9247c478bd9Sstevel@tonic-gate 		 * If there are ANY group acls, there MUST be a
9257c478bd9Sstevel@tonic-gate 		 * class_obj(mask) acl (1003.6/D12 p. 29 lines 75-80).
9267c478bd9Sstevel@tonic-gate 		 */
9277c478bd9Sstevel@tonic-gate 		if (ngroups && !nclass_objs) {
9287c478bd9Sstevel@tonic-gate 			return (EINVAL);
9297c478bd9Sstevel@tonic-gate 		}
9307c478bd9Sstevel@tonic-gate 		if (nuser_objs + ngroup_objs + nother_objs + nclass_objs +
9317c478bd9Sstevel@tonic-gate 		    ngroups + nusers > MAX_ACL_ENTRIES)
9327c478bd9Sstevel@tonic-gate 			return (EINVAL);
9337c478bd9Sstevel@tonic-gate 	}
9347c478bd9Sstevel@tonic-gate 
9357c478bd9Sstevel@tonic-gate 	/*
9367c478bd9Sstevel@tonic-gate 	 * For default acl's, we require that there be either one (and only one)
9377c478bd9Sstevel@tonic-gate 	 * DEF_USER_OBJ, DEF_GROUP_OBJ and DEF_OTHER_OBJ
9387c478bd9Sstevel@tonic-gate 	 * or  there be none of them.
9397c478bd9Sstevel@tonic-gate 	 */
9407c478bd9Sstevel@tonic-gate 	if (flag & DEF_ACL_CHECK) {
9417c478bd9Sstevel@tonic-gate 		if (ndef_other_objs > 1 || ndef_user_objs > 1 ||
9427c478bd9Sstevel@tonic-gate 		    ndef_group_objs > 1 || ndef_class_objs > 1) {
9437c478bd9Sstevel@tonic-gate 			return (EINVAL);
9447c478bd9Sstevel@tonic-gate 		}
9457c478bd9Sstevel@tonic-gate 
9467c478bd9Sstevel@tonic-gate 		numdefs = ndef_other_objs + ndef_user_objs + ndef_group_objs;
9477c478bd9Sstevel@tonic-gate 
9487c478bd9Sstevel@tonic-gate 		if (numdefs != 0 && numdefs != 3) {
9497c478bd9Sstevel@tonic-gate 			return (EINVAL);
9507c478bd9Sstevel@tonic-gate 		}
9517c478bd9Sstevel@tonic-gate 		/*
9527c478bd9Sstevel@tonic-gate 		 * If there are ANY def_group acls, there MUST be a
9537c478bd9Sstevel@tonic-gate 		 * def_class_obj(mask) acl (1003.6/D12 P. 29 lines 75-80).
9547c478bd9Sstevel@tonic-gate 		 * XXX(jimh) This is inferred.
9557c478bd9Sstevel@tonic-gate 		 */
9567c478bd9Sstevel@tonic-gate 		if (ndef_groups && !ndef_class_objs) {
9577c478bd9Sstevel@tonic-gate 			return (EINVAL);
9587c478bd9Sstevel@tonic-gate 		}
9597c478bd9Sstevel@tonic-gate 		if ((ndef_users || ndef_groups) &&
9607c478bd9Sstevel@tonic-gate 		    ((numdefs != 3) && !ndef_class_objs)) {
9617c478bd9Sstevel@tonic-gate 			return (EINVAL);
9627c478bd9Sstevel@tonic-gate 		}
9637c478bd9Sstevel@tonic-gate 		if (ndef_user_objs + ndef_group_objs + ndef_other_objs +
9647c478bd9Sstevel@tonic-gate 		    ndef_class_objs + ndef_users + ndef_groups >
9657c478bd9Sstevel@tonic-gate 		    MAX_ACL_ENTRIES)
9667c478bd9Sstevel@tonic-gate 			return (EINVAL);
9677c478bd9Sstevel@tonic-gate 	}
9687c478bd9Sstevel@tonic-gate 	return (0);
9697c478bd9Sstevel@tonic-gate }
9707c478bd9Sstevel@tonic-gate 
9717c478bd9Sstevel@tonic-gate static int
formacl(ufs_ic_acl_t ** aclpp,aclent_t * aclentp)9727c478bd9Sstevel@tonic-gate formacl(ufs_ic_acl_t **aclpp, aclent_t *aclentp)
9737c478bd9Sstevel@tonic-gate {
9747c478bd9Sstevel@tonic-gate 	ufs_ic_acl_t *uaclp;
9757c478bd9Sstevel@tonic-gate 
9767c478bd9Sstevel@tonic-gate 	uaclp = kmem_alloc(sizeof (ufs_ic_acl_t), KM_SLEEP);
9777c478bd9Sstevel@tonic-gate 	uaclp->acl_ic_perm = aclentp->a_perm;
9787c478bd9Sstevel@tonic-gate 	uaclp->acl_ic_who = aclentp->a_id;
9797c478bd9Sstevel@tonic-gate 	uaclp->acl_ic_next = *aclpp;
9807c478bd9Sstevel@tonic-gate 	*aclpp = uaclp;
9817c478bd9Sstevel@tonic-gate 	return (0);
9827c478bd9Sstevel@tonic-gate }
9837c478bd9Sstevel@tonic-gate 
9847c478bd9Sstevel@tonic-gate /*
9857c478bd9Sstevel@tonic-gate  * XXX - Make more efficient
9867c478bd9Sstevel@tonic-gate  * Convert from the vsecattr struct, used by the VOP interface, to
9877c478bd9Sstevel@tonic-gate  * the ufs_acl_entry struct used for in-core storage of acl's.
9887c478bd9Sstevel@tonic-gate  *
9897c478bd9Sstevel@tonic-gate  * Parameters:
9907c478bd9Sstevel@tonic-gate  * vsap - Ptr to array of security attributes.
9917c478bd9Sstevel@tonic-gate  * spp - Ptr to ptr to si struct for the results
9927c478bd9Sstevel@tonic-gate  *
9937c478bd9Sstevel@tonic-gate  * Returns:	0 - Success
994*1f563eb1SToomas Soome  *		N - From errno.h
9957c478bd9Sstevel@tonic-gate  */
9967c478bd9Sstevel@tonic-gate static int
vsecattr2aclentry(vsecattr_t * vsap,si_t ** spp)9977c478bd9Sstevel@tonic-gate vsecattr2aclentry(vsecattr_t *vsap, si_t **spp)
9987c478bd9Sstevel@tonic-gate {
9997c478bd9Sstevel@tonic-gate 	aclent_t	*aclentp, *aclp;
10007c478bd9Sstevel@tonic-gate 	si_t		*sp;
10017c478bd9Sstevel@tonic-gate 	int		err;
10027c478bd9Sstevel@tonic-gate 	int		i;
10037c478bd9Sstevel@tonic-gate 
10047c478bd9Sstevel@tonic-gate 	/* Sort & validate the lists on the vsap */
10057c478bd9Sstevel@tonic-gate 	ksort((caddr_t)vsap->vsa_aclentp, vsap->vsa_aclcnt,
100680d34432Sfrankho 	    sizeof (aclent_t), cmp2acls);
10077c478bd9Sstevel@tonic-gate 	ksort((caddr_t)vsap->vsa_dfaclentp, vsap->vsa_dfaclcnt,
100880d34432Sfrankho 	    sizeof (aclent_t), cmp2acls);
10097c478bd9Sstevel@tonic-gate 	if ((err = acl_validate(vsap->vsa_aclentp,
101080d34432Sfrankho 	    vsap->vsa_aclcnt, ACL_CHECK)) != 0)
10117c478bd9Sstevel@tonic-gate 		return (err);
10127c478bd9Sstevel@tonic-gate 	if ((err = acl_validate(vsap->vsa_dfaclentp,
101380d34432Sfrankho 	    vsap->vsa_dfaclcnt, DEF_ACL_CHECK)) != 0)
10147c478bd9Sstevel@tonic-gate 		return (err);
10157c478bd9Sstevel@tonic-gate 
10167c478bd9Sstevel@tonic-gate 	/* Create new si struct and hang acl's off it */
10177c478bd9Sstevel@tonic-gate 	sp = kmem_zalloc(sizeof (si_t), KM_SLEEP);
10187c478bd9Sstevel@tonic-gate 	rw_init(&sp->s_lock, NULL, RW_DEFAULT, NULL);
10197c478bd9Sstevel@tonic-gate 
10207c478bd9Sstevel@tonic-gate 	/* Process acl list */
10217c478bd9Sstevel@tonic-gate 	aclp = (aclent_t *)vsap->vsa_aclentp;
10227c478bd9Sstevel@tonic-gate 	aclentp = aclp + vsap->vsa_aclcnt - 1;
10237c478bd9Sstevel@tonic-gate 	for (i = 0; i < vsap->vsa_aclcnt; i++) {
10247c478bd9Sstevel@tonic-gate 		switch (aclentp->a_type) {
10257c478bd9Sstevel@tonic-gate 		case USER_OBJ:		/* Owner */
10267c478bd9Sstevel@tonic-gate 			if (err = formacl(&sp->aowner, aclentp))
10277c478bd9Sstevel@tonic-gate 				goto error;
10287c478bd9Sstevel@tonic-gate 			break;
10297c478bd9Sstevel@tonic-gate 		case GROUP_OBJ:		/* Group */
10307c478bd9Sstevel@tonic-gate 			if (err = formacl(&sp->agroup, aclentp))
10317c478bd9Sstevel@tonic-gate 				goto error;
10327c478bd9Sstevel@tonic-gate 			break;
10337c478bd9Sstevel@tonic-gate 		case OTHER_OBJ:		/* Other */
10347c478bd9Sstevel@tonic-gate 			if (err = formacl(&sp->aother, aclentp))
10357c478bd9Sstevel@tonic-gate 				goto error;
10367c478bd9Sstevel@tonic-gate 			break;
10377c478bd9Sstevel@tonic-gate 		case USER:
10387c478bd9Sstevel@tonic-gate 			if (err = formacl(&sp->ausers, aclentp))
10397c478bd9Sstevel@tonic-gate 				goto error;
10407c478bd9Sstevel@tonic-gate 			break;
10417c478bd9Sstevel@tonic-gate 		case CLASS_OBJ:		/* Mask */
10427c478bd9Sstevel@tonic-gate 			sp->aclass.acl_ismask = 1;
10437c478bd9Sstevel@tonic-gate 			sp->aclass.acl_maskbits = aclentp->a_perm;
10447c478bd9Sstevel@tonic-gate 			break;
10457c478bd9Sstevel@tonic-gate 		case GROUP:
10467c478bd9Sstevel@tonic-gate 			if (err = formacl(&sp->agroups, aclentp))
10477c478bd9Sstevel@tonic-gate 				goto error;
10487c478bd9Sstevel@tonic-gate 			break;
10497c478bd9Sstevel@tonic-gate 		default:
10507c478bd9Sstevel@tonic-gate 			break;
10517c478bd9Sstevel@tonic-gate 		}
10527c478bd9Sstevel@tonic-gate 		aclentp--;
10537c478bd9Sstevel@tonic-gate 	}
10547c478bd9Sstevel@tonic-gate 
10557c478bd9Sstevel@tonic-gate 	/* Process default acl list */
10567c478bd9Sstevel@tonic-gate 	aclp = (aclent_t *)vsap->vsa_dfaclentp;
10577c478bd9Sstevel@tonic-gate 	aclentp = aclp + vsap->vsa_dfaclcnt - 1;
10587c478bd9Sstevel@tonic-gate 	for (i = 0; i < vsap->vsa_dfaclcnt; i++) {
10597c478bd9Sstevel@tonic-gate 		switch (aclentp->a_type) {
10607c478bd9Sstevel@tonic-gate 		case DEF_USER_OBJ:	/* Default Owner */
10617c478bd9Sstevel@tonic-gate 			if (err = formacl(&sp->downer, aclentp))
10627c478bd9Sstevel@tonic-gate 				goto error;
10637c478bd9Sstevel@tonic-gate 			break;
10647c478bd9Sstevel@tonic-gate 		case DEF_GROUP_OBJ:	/* Default Group */
10657c478bd9Sstevel@tonic-gate 			if (err = formacl(&sp->dgroup, aclentp))
10667c478bd9Sstevel@tonic-gate 				goto error;
10677c478bd9Sstevel@tonic-gate 			break;
10687c478bd9Sstevel@tonic-gate 		case DEF_OTHER_OBJ:	/* Default Other */
10697c478bd9Sstevel@tonic-gate 			if (err = formacl(&sp->dother, aclentp))
10707c478bd9Sstevel@tonic-gate 				goto error;
10717c478bd9Sstevel@tonic-gate 			break;
10727c478bd9Sstevel@tonic-gate 		case DEF_USER:
10737c478bd9Sstevel@tonic-gate 			if (err = formacl(&sp->dusers, aclentp))
10747c478bd9Sstevel@tonic-gate 				goto error;
10757c478bd9Sstevel@tonic-gate 			break;
10767c478bd9Sstevel@tonic-gate 		case DEF_CLASS_OBJ:	/* Default Mask */
10777c478bd9Sstevel@tonic-gate 			sp->dclass.acl_ismask = 1;
10787c478bd9Sstevel@tonic-gate 			sp->dclass.acl_maskbits = aclentp->a_perm;
10797c478bd9Sstevel@tonic-gate 			break;
10807c478bd9Sstevel@tonic-gate 		case DEF_GROUP:
10817c478bd9Sstevel@tonic-gate 			if (err = formacl(&sp->dgroups, aclentp))
10827c478bd9Sstevel@tonic-gate 				goto error;
10837c478bd9Sstevel@tonic-gate 			break;
10847c478bd9Sstevel@tonic-gate 		default:
10857c478bd9Sstevel@tonic-gate 			break;
10867c478bd9Sstevel@tonic-gate 		}
10877c478bd9Sstevel@tonic-gate 		aclentp--;
10887c478bd9Sstevel@tonic-gate 	}
10897c478bd9Sstevel@tonic-gate 	*spp = sp;
10907c478bd9Sstevel@tonic-gate 	return (0);
10917c478bd9Sstevel@tonic-gate 
10927c478bd9Sstevel@tonic-gate error:
10937c478bd9Sstevel@tonic-gate 	ufs_si_free_mem(sp);
10947c478bd9Sstevel@tonic-gate 	return (err);
10957c478bd9Sstevel@tonic-gate }
10967c478bd9Sstevel@tonic-gate 
10977c478bd9Sstevel@tonic-gate void
formvsec(int obj_type,ufs_ic_acl_t * aclp,aclent_t ** aclentpp)10987c478bd9Sstevel@tonic-gate formvsec(int obj_type, ufs_ic_acl_t *aclp, aclent_t **aclentpp)
10997c478bd9Sstevel@tonic-gate {
11007c478bd9Sstevel@tonic-gate 	for (; aclp; aclp = aclp->acl_ic_next) {
11017c478bd9Sstevel@tonic-gate 		(*aclentpp)->a_type = obj_type;
11027c478bd9Sstevel@tonic-gate 		(*aclentpp)->a_perm = aclp->acl_ic_perm;
11037c478bd9Sstevel@tonic-gate 		(*aclentpp)->a_id = aclp->acl_ic_who;
11047c478bd9Sstevel@tonic-gate 		(*aclentpp)++;
11057c478bd9Sstevel@tonic-gate 	}
11067c478bd9Sstevel@tonic-gate }
11077c478bd9Sstevel@tonic-gate 
11087c478bd9Sstevel@tonic-gate /*
11097c478bd9Sstevel@tonic-gate  * XXX - Make more efficient
11107c478bd9Sstevel@tonic-gate  * Convert from the ufs_acl_entry struct used for in-core storage of acl's
11117c478bd9Sstevel@tonic-gate  * to the vsecattr struct,  used by the VOP interface.
11127c478bd9Sstevel@tonic-gate  *
11137c478bd9Sstevel@tonic-gate  * Parameters:
11147c478bd9Sstevel@tonic-gate  * sp - Ptr to si struct with the acls
11157c478bd9Sstevel@tonic-gate  * vsap - Ptr to a vsecattr struct which will take the results.
11167c478bd9Sstevel@tonic-gate  *
11177c478bd9Sstevel@tonic-gate  * Returns:	0 - Success
11187c478bd9Sstevel@tonic-gate  *		N - From errno table
11197c478bd9Sstevel@tonic-gate  */
11207c478bd9Sstevel@tonic-gate static int
aclentry2vsecattr(si_t * sp,vsecattr_t * vsap)11217c478bd9Sstevel@tonic-gate aclentry2vsecattr(si_t *sp, vsecattr_t *vsap)
11227c478bd9Sstevel@tonic-gate {
11237c478bd9Sstevel@tonic-gate 	aclent_t	*aclentp;
11247c478bd9Sstevel@tonic-gate 	int		numacls = 0;
11257c478bd9Sstevel@tonic-gate 	int		err;
11267c478bd9Sstevel@tonic-gate 
11277c478bd9Sstevel@tonic-gate 	vsap->vsa_aclentp = vsap->vsa_dfaclentp = NULL;
11287c478bd9Sstevel@tonic-gate 
11297c478bd9Sstevel@tonic-gate 	numacls = acl_count(sp->aowner) +
11307c478bd9Sstevel@tonic-gate 	    acl_count(sp->agroup) +
11317c478bd9Sstevel@tonic-gate 	    acl_count(sp->aother) +
11327c478bd9Sstevel@tonic-gate 	    acl_count(sp->ausers) +
11337c478bd9Sstevel@tonic-gate 	    acl_count(sp->agroups);
11347c478bd9Sstevel@tonic-gate 	if (sp->aclass.acl_ismask)
11357c478bd9Sstevel@tonic-gate 		numacls++;
11367c478bd9Sstevel@tonic-gate 
11377c478bd9Sstevel@tonic-gate 	if (vsap->vsa_mask & (VSA_ACLCNT | VSA_ACL))
11387c478bd9Sstevel@tonic-gate 		vsap->vsa_aclcnt = numacls;
11397c478bd9Sstevel@tonic-gate 
114006c5a6e8Sjr 	if (numacls == 0)
114106c5a6e8Sjr 		goto do_defaults;
114206c5a6e8Sjr 
11437c478bd9Sstevel@tonic-gate 	if (vsap->vsa_mask & VSA_ACL) {
11447c478bd9Sstevel@tonic-gate 		vsap->vsa_aclentp = kmem_zalloc(numacls * sizeof (aclent_t),
11457c478bd9Sstevel@tonic-gate 		    KM_SLEEP);
11467c478bd9Sstevel@tonic-gate 		aclentp = vsap->vsa_aclentp;
11477c478bd9Sstevel@tonic-gate 
11487c478bd9Sstevel@tonic-gate 		formvsec(USER_OBJ, sp->aowner, &aclentp);
11497c478bd9Sstevel@tonic-gate 		formvsec(USER, sp->ausers, &aclentp);
11507c478bd9Sstevel@tonic-gate 		formvsec(GROUP_OBJ, sp->agroup, &aclentp);
11517c478bd9Sstevel@tonic-gate 		formvsec(GROUP, sp->agroups, &aclentp);
11527c478bd9Sstevel@tonic-gate 		formvsec(OTHER_OBJ, sp->aother, &aclentp);
11537c478bd9Sstevel@tonic-gate 
11547c478bd9Sstevel@tonic-gate 		if (sp->aclass.acl_ismask) {
11557c478bd9Sstevel@tonic-gate 			aclentp->a_type = CLASS_OBJ;		/* Mask */
11567c478bd9Sstevel@tonic-gate 			aclentp->a_perm = sp->aclass.acl_maskbits;
11577c478bd9Sstevel@tonic-gate 			aclentp->a_id = 0;
11587c478bd9Sstevel@tonic-gate 			aclentp++;
11597c478bd9Sstevel@tonic-gate 		}
11607c478bd9Sstevel@tonic-gate 
11617c478bd9Sstevel@tonic-gate 		/* Sort the acl list */
11627c478bd9Sstevel@tonic-gate 		ksort((caddr_t)vsap->vsa_aclentp, vsap->vsa_aclcnt,
116380d34432Sfrankho 		    sizeof (aclent_t), cmp2acls);
11647c478bd9Sstevel@tonic-gate 		/* Check the acl list */
11657c478bd9Sstevel@tonic-gate 		if ((err = acl_validate(vsap->vsa_aclentp,
116680d34432Sfrankho 		    vsap->vsa_aclcnt, ACL_CHECK)) != 0) {
116780d34432Sfrankho 			kmem_free(vsap->vsa_aclentp,
116880d34432Sfrankho 			    numacls * sizeof (aclent_t));
11697c478bd9Sstevel@tonic-gate 			vsap->vsa_aclentp = NULL;
11707c478bd9Sstevel@tonic-gate 			return (err);
11717c478bd9Sstevel@tonic-gate 		}
11727c478bd9Sstevel@tonic-gate 
11737c478bd9Sstevel@tonic-gate 	}
11747c478bd9Sstevel@tonic-gate do_defaults:
11757c478bd9Sstevel@tonic-gate 	/* Process Defaults */
11767c478bd9Sstevel@tonic-gate 
11777c478bd9Sstevel@tonic-gate 	numacls = acl_count(sp->downer) +
11787c478bd9Sstevel@tonic-gate 	    acl_count(sp->dgroup) +
11797c478bd9Sstevel@tonic-gate 	    acl_count(sp->dother) +
11807c478bd9Sstevel@tonic-gate 	    acl_count(sp->dusers) +
11817c478bd9Sstevel@tonic-gate 	    acl_count(sp->dgroups);
11827c478bd9Sstevel@tonic-gate 	if (sp->dclass.acl_ismask)
11837c478bd9Sstevel@tonic-gate 		numacls++;
11847c478bd9Sstevel@tonic-gate 
11857c478bd9Sstevel@tonic-gate 	if (vsap->vsa_mask & (VSA_DFACLCNT | VSA_DFACL))
11867c478bd9Sstevel@tonic-gate 		vsap->vsa_dfaclcnt = numacls;
11877c478bd9Sstevel@tonic-gate 
118806c5a6e8Sjr 	if (numacls == 0)
118906c5a6e8Sjr 		goto do_others;
119006c5a6e8Sjr 
11917c478bd9Sstevel@tonic-gate 	if (vsap->vsa_mask & VSA_DFACL) {
119280d34432Sfrankho 		vsap->vsa_dfaclentp =
119380d34432Sfrankho 		    kmem_zalloc(numacls * sizeof (aclent_t), KM_SLEEP);
11947c478bd9Sstevel@tonic-gate 		aclentp = vsap->vsa_dfaclentp;
11957c478bd9Sstevel@tonic-gate 		formvsec(DEF_USER_OBJ, sp->downer, &aclentp);
11967c478bd9Sstevel@tonic-gate 		formvsec(DEF_USER, sp->dusers, &aclentp);
11977c478bd9Sstevel@tonic-gate 		formvsec(DEF_GROUP_OBJ, sp->dgroup, &aclentp);
11987c478bd9Sstevel@tonic-gate 		formvsec(DEF_GROUP, sp->dgroups, &aclentp);
11997c478bd9Sstevel@tonic-gate 		formvsec(DEF_OTHER_OBJ, sp->dother, &aclentp);
12007c478bd9Sstevel@tonic-gate 
12017c478bd9Sstevel@tonic-gate 		if (sp->dclass.acl_ismask) {
12027c478bd9Sstevel@tonic-gate 			aclentp->a_type = DEF_CLASS_OBJ;	/* Mask */
12037c478bd9Sstevel@tonic-gate 			aclentp->a_perm = sp->dclass.acl_maskbits;
12047c478bd9Sstevel@tonic-gate 			aclentp->a_id = 0;
12057c478bd9Sstevel@tonic-gate 			aclentp++;
12067c478bd9Sstevel@tonic-gate 		}
12077c478bd9Sstevel@tonic-gate 
12087c478bd9Sstevel@tonic-gate 		/* Sort the default acl list */
12097c478bd9Sstevel@tonic-gate 		ksort((caddr_t)vsap->vsa_dfaclentp, vsap->vsa_dfaclcnt,
121080d34432Sfrankho 		    sizeof (aclent_t), cmp2acls);
12117c478bd9Sstevel@tonic-gate 		if ((err = acl_validate(vsap->vsa_dfaclentp,
12127c478bd9Sstevel@tonic-gate 		    vsap->vsa_dfaclcnt, DEF_ACL_CHECK)) != 0) {
12137c478bd9Sstevel@tonic-gate 			if (vsap->vsa_aclentp != NULL)
12147c478bd9Sstevel@tonic-gate 				kmem_free(vsap->vsa_aclentp,
12157c478bd9Sstevel@tonic-gate 				    vsap->vsa_aclcnt * sizeof (aclent_t));
12167c478bd9Sstevel@tonic-gate 			kmem_free(vsap->vsa_dfaclentp,
12177c478bd9Sstevel@tonic-gate 			    vsap->vsa_dfaclcnt * sizeof (aclent_t));
12187c478bd9Sstevel@tonic-gate 			vsap->vsa_aclentp = vsap->vsa_dfaclentp = NULL;
12197c478bd9Sstevel@tonic-gate 			return (err);
12207c478bd9Sstevel@tonic-gate 		}
12217c478bd9Sstevel@tonic-gate 	}
12227c478bd9Sstevel@tonic-gate 
12237c478bd9Sstevel@tonic-gate do_others:
12247c478bd9Sstevel@tonic-gate 	return (0);
12257c478bd9Sstevel@tonic-gate }
12267c478bd9Sstevel@tonic-gate 
12277c478bd9Sstevel@tonic-gate static void
acl_free(ufs_ic_acl_t * aclp)12287c478bd9Sstevel@tonic-gate acl_free(ufs_ic_acl_t *aclp)
12297c478bd9Sstevel@tonic-gate {
12307c478bd9Sstevel@tonic-gate 	while (aclp != NULL) {
12317c478bd9Sstevel@tonic-gate 		ufs_ic_acl_t *nextaclp = aclp->acl_ic_next;
12327c478bd9Sstevel@tonic-gate 		kmem_free(aclp, sizeof (ufs_ic_acl_t));
12337c478bd9Sstevel@tonic-gate 		aclp = nextaclp;
12347c478bd9Sstevel@tonic-gate 	}
12357c478bd9Sstevel@tonic-gate }
12367c478bd9Sstevel@tonic-gate 
12377c478bd9Sstevel@tonic-gate /*
12387c478bd9Sstevel@tonic-gate  * ufs_si_free_mem will discard the sp, and the acl hanging off of the
12397c478bd9Sstevel@tonic-gate  * sp.  It is required that the sp not be locked, and not be in the
12407c478bd9Sstevel@tonic-gate  * cache.
12417c478bd9Sstevel@tonic-gate  *
12427c478bd9Sstevel@tonic-gate  * input: pointer to sp to discard.
12437c478bd9Sstevel@tonic-gate  *
12447c478bd9Sstevel@tonic-gate  * return - nothing.
12457c478bd9Sstevel@tonic-gate  *
12467c478bd9Sstevel@tonic-gate  */
12477c478bd9Sstevel@tonic-gate static void
ufs_si_free_mem(si_t * sp)12487c478bd9Sstevel@tonic-gate ufs_si_free_mem(si_t *sp)
12497c478bd9Sstevel@tonic-gate {
12507c478bd9Sstevel@tonic-gate 	ASSERT(!(sp->s_flags & SI_CACHED));
12517c478bd9Sstevel@tonic-gate 	ASSERT(!RW_LOCK_HELD(&sp->s_lock));
12527c478bd9Sstevel@tonic-gate 	/*
12537c478bd9Sstevel@tonic-gate 	 *	remove from the cache
12547c478bd9Sstevel@tonic-gate 	 *	free the acl entries
12557c478bd9Sstevel@tonic-gate 	 */
12567c478bd9Sstevel@tonic-gate 	acl_free(sp->aowner);
12577c478bd9Sstevel@tonic-gate 	acl_free(sp->agroup);
12587c478bd9Sstevel@tonic-gate 	acl_free(sp->aother);
12597c478bd9Sstevel@tonic-gate 	acl_free(sp->ausers);
12607c478bd9Sstevel@tonic-gate 	acl_free(sp->agroups);
12617c478bd9Sstevel@tonic-gate 
12627c478bd9Sstevel@tonic-gate 	acl_free(sp->downer);
12637c478bd9Sstevel@tonic-gate 	acl_free(sp->dgroup);
12647c478bd9Sstevel@tonic-gate 	acl_free(sp->dother);
12657c478bd9Sstevel@tonic-gate 	acl_free(sp->dusers);
12667c478bd9Sstevel@tonic-gate 	acl_free(sp->dgroups);
12677c478bd9Sstevel@tonic-gate 
12687c478bd9Sstevel@tonic-gate 	rw_destroy(&sp->s_lock);
12697c478bd9Sstevel@tonic-gate 	kmem_free(sp, sizeof (si_t));
12707c478bd9Sstevel@tonic-gate }
12717c478bd9Sstevel@tonic-gate 
12727c478bd9Sstevel@tonic-gate void
acl_cpy(ufs_ic_acl_t * saclp,ufs_ic_acl_t * daclp)12737c478bd9Sstevel@tonic-gate acl_cpy(ufs_ic_acl_t *saclp, ufs_ic_acl_t *daclp)
12747c478bd9Sstevel@tonic-gate {
12757c478bd9Sstevel@tonic-gate 	ufs_ic_acl_t  *aclp, *prev_aclp = NULL, *aclp1;
12767c478bd9Sstevel@tonic-gate 
12777c478bd9Sstevel@tonic-gate 	if (saclp == NULL) {
12787c478bd9Sstevel@tonic-gate 		daclp = NULL;
12797c478bd9Sstevel@tonic-gate 		return;
12807c478bd9Sstevel@tonic-gate 	}
12817c478bd9Sstevel@tonic-gate 	prev_aclp = daclp;
12827c478bd9Sstevel@tonic-gate 
12837c478bd9Sstevel@tonic-gate 	for (aclp = saclp; aclp != NULL; aclp = aclp->acl_ic_next) {
12847c478bd9Sstevel@tonic-gate 		aclp1 = kmem_alloc(sizeof (ufs_ic_acl_t), KM_SLEEP);
12857c478bd9Sstevel@tonic-gate 		aclp1->acl_ic_next = NULL;
12867c478bd9Sstevel@tonic-gate 		aclp1->acl_ic_who = aclp->acl_ic_who;
12877c478bd9Sstevel@tonic-gate 		aclp1->acl_ic_perm = aclp->acl_ic_perm;
12887c478bd9Sstevel@tonic-gate 		prev_aclp->acl_ic_next = aclp1;
12897c478bd9Sstevel@tonic-gate 		prev_aclp = (ufs_ic_acl_t *)&aclp1->acl_ic_next;
12907c478bd9Sstevel@tonic-gate 	}
12917c478bd9Sstevel@tonic-gate }
12927c478bd9Sstevel@tonic-gate 
12937c478bd9Sstevel@tonic-gate /*
12947c478bd9Sstevel@tonic-gate  *	ufs_si_inherit takes a parent acl structure (saclp) and the inode
12957c478bd9Sstevel@tonic-gate  *	of the object that is inheriting an acl and returns the inode
12967c478bd9Sstevel@tonic-gate  *	with the acl linked to it.  It also writes the acl to disk if
12977c478bd9Sstevel@tonic-gate  *	it is a unique inode.
12987c478bd9Sstevel@tonic-gate  *
12997c478bd9Sstevel@tonic-gate  *	ip - pointer to inode of object inheriting the acl (contents lock)
13007c478bd9Sstevel@tonic-gate  *	tdp - parent inode (rw_lock and contents lock)
13017c478bd9Sstevel@tonic-gate  *	mode - creation modes
13027c478bd9Sstevel@tonic-gate  *	cr - credentials pointer
13037c478bd9Sstevel@tonic-gate  */
13047c478bd9Sstevel@tonic-gate int
ufs_si_inherit(struct inode * ip,struct inode * tdp,o_mode_t mode,cred_t * cr)13057c478bd9Sstevel@tonic-gate ufs_si_inherit(struct inode *ip, struct inode *tdp, o_mode_t mode, cred_t *cr)
13067c478bd9Sstevel@tonic-gate {
13077c478bd9Sstevel@tonic-gate 	si_t *tsp, *sp = tdp->i_ufs_acl;
13087c478bd9Sstevel@tonic-gate 	int error;
13097c478bd9Sstevel@tonic-gate 	o_mode_t old_modes, old_uid, old_gid;
13107c478bd9Sstevel@tonic-gate 	int mask;
13117c478bd9Sstevel@tonic-gate 
13127c478bd9Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&ip->i_contents));
13137c478bd9Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&tdp->i_rwlock));
13147c478bd9Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&tdp->i_contents));
13157c478bd9Sstevel@tonic-gate 
13167c478bd9Sstevel@tonic-gate 	/*
13177c478bd9Sstevel@tonic-gate 	 * if links/symbolic links, or other invalid acl objects are copied
13187c478bd9Sstevel@tonic-gate 	 * or moved to a directory with a default acl do not allow inheritance
13197c478bd9Sstevel@tonic-gate 	 * just return.
13207c478bd9Sstevel@tonic-gate 	 */
13217c478bd9Sstevel@tonic-gate 	if (!CHECK_ACL_ALLOWED(ip->i_mode & IFMT))
13227c478bd9Sstevel@tonic-gate 		return (0);
13237c478bd9Sstevel@tonic-gate 
13247c478bd9Sstevel@tonic-gate 	/* lock the parent security information */
13257c478bd9Sstevel@tonic-gate 	rw_enter(&sp->s_lock, RW_READER);
13267c478bd9Sstevel@tonic-gate 
13277c478bd9Sstevel@tonic-gate 	ASSERT(((tdp->i_mode & IFMT) == IFDIR) ||
132880d34432Sfrankho 	    ((tdp->i_mode & IFMT) == IFATTRDIR));
13297c478bd9Sstevel@tonic-gate 
13307c478bd9Sstevel@tonic-gate 	mask = ((sp->downer != NULL) ? 1 : 0) |
13317c478bd9Sstevel@tonic-gate 	    ((sp->dgroup != NULL) ? 2 : 0) |
13327c478bd9Sstevel@tonic-gate 	    ((sp->dother != NULL) ? 4 : 0);
13337c478bd9Sstevel@tonic-gate 
13347c478bd9Sstevel@tonic-gate 	if (mask == 0) {
13357c478bd9Sstevel@tonic-gate 		rw_exit(&sp->s_lock);
13367c478bd9Sstevel@tonic-gate 		return (0);
13377c478bd9Sstevel@tonic-gate 	}
13387c478bd9Sstevel@tonic-gate 
13397c478bd9Sstevel@tonic-gate 	if (mask != 7) {
13407c478bd9Sstevel@tonic-gate 		rw_exit(&sp->s_lock);
13417c478bd9Sstevel@tonic-gate 		return (EINVAL);
13427c478bd9Sstevel@tonic-gate 	}
13437c478bd9Sstevel@tonic-gate 
13447c478bd9Sstevel@tonic-gate 	tsp = kmem_zalloc(sizeof (si_t), KM_SLEEP);
13457c478bd9Sstevel@tonic-gate 	rw_init(&tsp->s_lock, NULL, RW_DEFAULT, NULL);
13467c478bd9Sstevel@tonic-gate 
13477c478bd9Sstevel@tonic-gate 	/* copy the default acls */
13487c478bd9Sstevel@tonic-gate 
13497c478bd9Sstevel@tonic-gate 	ASSERT(RW_READ_HELD(&sp->s_lock));
13507c478bd9Sstevel@tonic-gate 	acl_cpy(sp->downer, (ufs_ic_acl_t *)&tsp->aowner);
13517c478bd9Sstevel@tonic-gate 	acl_cpy(sp->dgroup, (ufs_ic_acl_t *)&tsp->agroup);
13527c478bd9Sstevel@tonic-gate 	acl_cpy(sp->dother, (ufs_ic_acl_t *)&tsp->aother);
13537c478bd9Sstevel@tonic-gate 	acl_cpy(sp->dusers, (ufs_ic_acl_t *)&tsp->ausers);
13547c478bd9Sstevel@tonic-gate 	acl_cpy(sp->dgroups, (ufs_ic_acl_t *)&tsp->agroups);
13557c478bd9Sstevel@tonic-gate 	tsp->aclass.acl_ismask = sp->dclass.acl_ismask;
13567c478bd9Sstevel@tonic-gate 	tsp->aclass.acl_maskbits = sp->dclass.acl_maskbits;
13577c478bd9Sstevel@tonic-gate 
13587c478bd9Sstevel@tonic-gate 	/*
13597c478bd9Sstevel@tonic-gate 	 * set the owner, group, and other values from the master
13607c478bd9Sstevel@tonic-gate 	 * inode.
13617c478bd9Sstevel@tonic-gate 	 */
13627c478bd9Sstevel@tonic-gate 
13637c478bd9Sstevel@tonic-gate 	MODE2ACL(tsp->aowner, (mode >> 6), ip->i_uid);
13647c478bd9Sstevel@tonic-gate 	MODE2ACL(tsp->agroup, (mode >> 3), ip->i_gid);
13657c478bd9Sstevel@tonic-gate 	MODE2ACL(tsp->aother, (mode), 0);
13667c478bd9Sstevel@tonic-gate 
13677c478bd9Sstevel@tonic-gate 	if (tsp->aclass.acl_ismask) {
13687c478bd9Sstevel@tonic-gate 		tsp->aclass.acl_maskbits &= mode >> 3;
13697c478bd9Sstevel@tonic-gate 	}
13707c478bd9Sstevel@tonic-gate 
13717c478bd9Sstevel@tonic-gate 
13727c478bd9Sstevel@tonic-gate 	/* copy default acl if necessary */
13737c478bd9Sstevel@tonic-gate 
13747c478bd9Sstevel@tonic-gate 	if (((ip->i_mode & IFMT) == IFDIR) ||
137580d34432Sfrankho 	    ((ip->i_mode & IFMT) == IFATTRDIR)) {
13767c478bd9Sstevel@tonic-gate 		acl_cpy(sp->downer, (ufs_ic_acl_t *)&tsp->downer);
13777c478bd9Sstevel@tonic-gate 		acl_cpy(sp->dgroup, (ufs_ic_acl_t *)&tsp->dgroup);
13787c478bd9Sstevel@tonic-gate 		acl_cpy(sp->dother, (ufs_ic_acl_t *)&tsp->dother);
13797c478bd9Sstevel@tonic-gate 		acl_cpy(sp->dusers, (ufs_ic_acl_t *)&tsp->dusers);
13807c478bd9Sstevel@tonic-gate 		acl_cpy(sp->dgroups, (ufs_ic_acl_t *)&tsp->dgroups);
13817c478bd9Sstevel@tonic-gate 		tsp->dclass.acl_ismask = sp->dclass.acl_ismask;
13827c478bd9Sstevel@tonic-gate 		tsp->dclass.acl_maskbits = sp->dclass.acl_maskbits;
13837c478bd9Sstevel@tonic-gate 	}
13847c478bd9Sstevel@tonic-gate 	/*
13857c478bd9Sstevel@tonic-gate 	 * save the new 9 mode bits in the inode (ip->ic_smode) for
13867c478bd9Sstevel@tonic-gate 	 * ufs_getattr.  Be sure the mode can be recovered if the store
13877c478bd9Sstevel@tonic-gate 	 * fails.
13887c478bd9Sstevel@tonic-gate 	 */
13897c478bd9Sstevel@tonic-gate 	old_modes = ip->i_mode;
13907c478bd9Sstevel@tonic-gate 	old_uid = ip->i_uid;
13917c478bd9Sstevel@tonic-gate 	old_gid = ip->i_gid;
13927c478bd9Sstevel@tonic-gate 	/*
13937c478bd9Sstevel@tonic-gate 	 * store the acl, and get back a new security anchor if
13947c478bd9Sstevel@tonic-gate 	 * it is a duplicate.
13957c478bd9Sstevel@tonic-gate 	 */
13967c478bd9Sstevel@tonic-gate 	rw_exit(&sp->s_lock);
13977c478bd9Sstevel@tonic-gate 	rw_enter(&ip->i_rwlock, RW_WRITER);
13987c478bd9Sstevel@tonic-gate 
13997c478bd9Sstevel@tonic-gate 	/*
14007c478bd9Sstevel@tonic-gate 	 * Suppress out of inodes messages if instructed in the
14017c478bd9Sstevel@tonic-gate 	 * tdp inode.
14027c478bd9Sstevel@tonic-gate 	 */
14037c478bd9Sstevel@tonic-gate 	ip->i_flag |= tdp->i_flag & IQUIET;
14047c478bd9Sstevel@tonic-gate 
14057c478bd9Sstevel@tonic-gate 	if ((error = ufs_si_store(ip, tsp, 0, cr)) != 0) {
14067c478bd9Sstevel@tonic-gate 		ip->i_mode = old_modes;
14077c478bd9Sstevel@tonic-gate 		ip->i_uid = old_uid;
14087c478bd9Sstevel@tonic-gate 		ip->i_gid = old_gid;
14097c478bd9Sstevel@tonic-gate 	}
14107c478bd9Sstevel@tonic-gate 	ip->i_flag &= ~IQUIET;
14117c478bd9Sstevel@tonic-gate 	rw_exit(&ip->i_rwlock);
14127c478bd9Sstevel@tonic-gate 	return (error);
14137c478bd9Sstevel@tonic-gate }
14147c478bd9Sstevel@tonic-gate 
14157c478bd9Sstevel@tonic-gate si_t *
ufs_acl_cp(si_t * sp)14167c478bd9Sstevel@tonic-gate ufs_acl_cp(si_t *sp)
14177c478bd9Sstevel@tonic-gate {
14187c478bd9Sstevel@tonic-gate 
14197c478bd9Sstevel@tonic-gate 	si_t *dsp;
14207c478bd9Sstevel@tonic-gate 
14217c478bd9Sstevel@tonic-gate 	ASSERT(RW_READ_HELD(&sp->s_lock));
14227c478bd9Sstevel@tonic-gate 	ASSERT(sp->s_ref && sp->s_use);
14237c478bd9Sstevel@tonic-gate 
14247c478bd9Sstevel@tonic-gate 	dsp = kmem_zalloc(sizeof (si_t), KM_SLEEP);
14257c478bd9Sstevel@tonic-gate 	rw_init(&dsp->s_lock, NULL, RW_DEFAULT, NULL);
14267c478bd9Sstevel@tonic-gate 
14277c478bd9Sstevel@tonic-gate 	acl_cpy(sp->aowner, (ufs_ic_acl_t *)&dsp->aowner);
14287c478bd9Sstevel@tonic-gate 	acl_cpy(sp->agroup, (ufs_ic_acl_t *)&dsp->agroup);
14297c478bd9Sstevel@tonic-gate 	acl_cpy(sp->aother, (ufs_ic_acl_t *)&dsp->aother);
14307c478bd9Sstevel@tonic-gate 	acl_cpy(sp->ausers, (ufs_ic_acl_t *)&dsp->ausers);
14317c478bd9Sstevel@tonic-gate 	acl_cpy(sp->agroups, (ufs_ic_acl_t *)&dsp->agroups);
14327c478bd9Sstevel@tonic-gate 
14337c478bd9Sstevel@tonic-gate 	dsp->aclass.acl_ismask = sp->aclass.acl_ismask;
14347c478bd9Sstevel@tonic-gate 	dsp->aclass.acl_maskbits = sp->aclass.acl_maskbits;
14357c478bd9Sstevel@tonic-gate 
14367c478bd9Sstevel@tonic-gate 	acl_cpy(sp->downer, (ufs_ic_acl_t *)&dsp->downer);
14377c478bd9Sstevel@tonic-gate 	acl_cpy(sp->dgroup, (ufs_ic_acl_t *)&dsp->dgroup);
14387c478bd9Sstevel@tonic-gate 	acl_cpy(sp->dother, (ufs_ic_acl_t *)&dsp->dother);
14397c478bd9Sstevel@tonic-gate 	acl_cpy(sp->dusers, (ufs_ic_acl_t *)&dsp->dusers);
14407c478bd9Sstevel@tonic-gate 	acl_cpy(sp->dgroups, (ufs_ic_acl_t *)&dsp->dgroups);
14417c478bd9Sstevel@tonic-gate 
14427c478bd9Sstevel@tonic-gate 	dsp->dclass.acl_ismask = sp->dclass.acl_ismask;
14437c478bd9Sstevel@tonic-gate 	dsp->dclass.acl_maskbits = sp->dclass.acl_maskbits;
14447c478bd9Sstevel@tonic-gate 
14457c478bd9Sstevel@tonic-gate 	return (dsp);
14467c478bd9Sstevel@tonic-gate 
14477c478bd9Sstevel@tonic-gate }
14487c478bd9Sstevel@tonic-gate 
14497c478bd9Sstevel@tonic-gate int
ufs_acl_setattr(struct inode * ip,struct vattr * vap,cred_t * cr)14507c478bd9Sstevel@tonic-gate ufs_acl_setattr(struct inode *ip, struct vattr *vap, cred_t *cr)
14517c478bd9Sstevel@tonic-gate {
14527c478bd9Sstevel@tonic-gate 
14537c478bd9Sstevel@tonic-gate 	si_t *sp;
14547c478bd9Sstevel@tonic-gate 	int mask = vap->va_mask;
14557c478bd9Sstevel@tonic-gate 	int error = 0;
14567c478bd9Sstevel@tonic-gate 
14577c478bd9Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&ip->i_contents));
14587c478bd9Sstevel@tonic-gate 
14597c478bd9Sstevel@tonic-gate 	if (!(mask & (AT_MODE|AT_UID|AT_GID)))
14607c478bd9Sstevel@tonic-gate 		return (0);
14617c478bd9Sstevel@tonic-gate 
14627c478bd9Sstevel@tonic-gate 	/*
14637c478bd9Sstevel@tonic-gate 	 * if no regular acl's, nothing to do, so let's get out
14647c478bd9Sstevel@tonic-gate 	 */
14657c478bd9Sstevel@tonic-gate 	if (!(ip->i_ufs_acl) || !(ip->i_ufs_acl->aowner))
14667c478bd9Sstevel@tonic-gate 		return (0);
14677c478bd9Sstevel@tonic-gate 
14687c478bd9Sstevel@tonic-gate 	rw_enter(&ip->i_ufs_acl->s_lock, RW_READER);
14697c478bd9Sstevel@tonic-gate 	sp = ufs_acl_cp(ip->i_ufs_acl);
14707c478bd9Sstevel@tonic-gate 	ASSERT(sp != ip->i_ufs_acl);
14717c478bd9Sstevel@tonic-gate 
14727c478bd9Sstevel@tonic-gate 	/*
14737c478bd9Sstevel@tonic-gate 	 * set the mask to the group permissions if a mask entry
14747c478bd9Sstevel@tonic-gate 	 * exists.  Otherwise, set the group obj bits to the group
14757c478bd9Sstevel@tonic-gate 	 * permissions.  Since non-trivial ACLs always have a mask,
14767c478bd9Sstevel@tonic-gate 	 * and the mask is the final arbiter of group permissions,
14777c478bd9Sstevel@tonic-gate 	 * setting the mask has the effect of changing the effective
14787c478bd9Sstevel@tonic-gate 	 * group permissions, even if the group_obj permissions in
14797c478bd9Sstevel@tonic-gate 	 * the ACL aren't changed.  Posix P1003.1e states that when
14807c478bd9Sstevel@tonic-gate 	 * an ACL mask exists, chmod(2) must set the acl mask (NOT the
14817c478bd9Sstevel@tonic-gate 	 * group_obj permissions) to the requested group permissions.
14827c478bd9Sstevel@tonic-gate 	 */
14837c478bd9Sstevel@tonic-gate 	if (mask & AT_MODE) {
14847c478bd9Sstevel@tonic-gate 		sp->aowner->acl_ic_perm = (o_mode_t)(ip->i_mode & 0700) >> 6;
14857c478bd9Sstevel@tonic-gate 		if (sp->aclass.acl_ismask)
14867c478bd9Sstevel@tonic-gate 			sp->aclass.acl_maskbits =
14877c478bd9Sstevel@tonic-gate 			    (o_mode_t)(ip->i_mode & 070) >> 3;
14887c478bd9Sstevel@tonic-gate 		else
14897c478bd9Sstevel@tonic-gate 			sp->agroup->acl_ic_perm =
14907c478bd9Sstevel@tonic-gate 			    (o_mode_t)(ip->i_mode & 070) >> 3;
14917c478bd9Sstevel@tonic-gate 		sp->aother->acl_ic_perm = (o_mode_t)(ip->i_mode & 07);
14927c478bd9Sstevel@tonic-gate 	}
14937c478bd9Sstevel@tonic-gate 
14947c478bd9Sstevel@tonic-gate 	if (mask & AT_UID) {
14957c478bd9Sstevel@tonic-gate 		/* Caller has verified our privileges */
14967c478bd9Sstevel@tonic-gate 		sp->aowner->acl_ic_who = ip->i_uid;
14977c478bd9Sstevel@tonic-gate 	}
14987c478bd9Sstevel@tonic-gate 
14997c478bd9Sstevel@tonic-gate 	if (mask & AT_GID) {
15007c478bd9Sstevel@tonic-gate 		sp->agroup->acl_ic_who = ip->i_gid;
15017c478bd9Sstevel@tonic-gate 	}
15027c478bd9Sstevel@tonic-gate 
15037c478bd9Sstevel@tonic-gate 	rw_exit(&ip->i_ufs_acl->s_lock);
15047c478bd9Sstevel@tonic-gate 	error = ufs_si_store(ip, sp, 0, cr);
15057c478bd9Sstevel@tonic-gate 	return (error);
15067c478bd9Sstevel@tonic-gate }
15077c478bd9Sstevel@tonic-gate 
15087c478bd9Sstevel@tonic-gate static int
acl_count(ufs_ic_acl_t * p)15097c478bd9Sstevel@tonic-gate acl_count(ufs_ic_acl_t *p)
15107c478bd9Sstevel@tonic-gate {
15117c478bd9Sstevel@tonic-gate 	ufs_ic_acl_t	*acl;
15127c478bd9Sstevel@tonic-gate 	int		count;
15137c478bd9Sstevel@tonic-gate 
15147c478bd9Sstevel@tonic-gate 	for (count = 0, acl = p; acl; acl = acl->acl_ic_next, count++)
15157c478bd9Sstevel@tonic-gate 		;
15167c478bd9Sstevel@tonic-gate 	return (count);
15177c478bd9Sstevel@tonic-gate }
15187c478bd9Sstevel@tonic-gate 
15197c478bd9Sstevel@tonic-gate /*
15207c478bd9Sstevel@tonic-gate  *	Takes as input a security structure and generates a buffer
15217c478bd9Sstevel@tonic-gate  *	with fsd's in a form which be written to the shadow inode.
15227c478bd9Sstevel@tonic-gate  */
15237c478bd9Sstevel@tonic-gate static int
ufs_sectobuf(si_t * sp,caddr_t * buf,size_t * len)15247c478bd9Sstevel@tonic-gate ufs_sectobuf(si_t *sp, caddr_t *buf, size_t *len)
15257c478bd9Sstevel@tonic-gate {
15267c478bd9Sstevel@tonic-gate 	size_t		acl_size;
15277c478bd9Sstevel@tonic-gate 	size_t		def_acl_size;
15287c478bd9Sstevel@tonic-gate 	caddr_t		buffer;
15297c478bd9Sstevel@tonic-gate 	struct ufs_fsd	*fsdp;
15307c478bd9Sstevel@tonic-gate 	ufs_acl_t	*bufaclp;
15317c478bd9Sstevel@tonic-gate 
15327c478bd9Sstevel@tonic-gate 	/*
15337c478bd9Sstevel@tonic-gate 	 * Calc size of buffer to hold all the acls
15347c478bd9Sstevel@tonic-gate 	 */
15357c478bd9Sstevel@tonic-gate 	acl_size = acl_count(sp->aowner) +		/* owner */
15367c478bd9Sstevel@tonic-gate 	    acl_count(sp->agroup) +			/* owner group */
15377c478bd9Sstevel@tonic-gate 	    acl_count(sp->aother) +			/* owner other */
15387c478bd9Sstevel@tonic-gate 	    acl_count(sp->ausers) +			/* acl list */
15397c478bd9Sstevel@tonic-gate 	    acl_count(sp->agroups);			/* group alcs */
15407c478bd9Sstevel@tonic-gate 	if (sp->aclass.acl_ismask)
15417c478bd9Sstevel@tonic-gate 		acl_size++;
15427c478bd9Sstevel@tonic-gate 
15437c478bd9Sstevel@tonic-gate 	/* Convert to bytes */
15447c478bd9Sstevel@tonic-gate 	acl_size *= sizeof (ufs_acl_t);
15457c478bd9Sstevel@tonic-gate 
15467c478bd9Sstevel@tonic-gate 	/* Add fsd header */
15477c478bd9Sstevel@tonic-gate 	if (acl_size)
15487c478bd9Sstevel@tonic-gate 		acl_size += 2 * sizeof (int);
15497c478bd9Sstevel@tonic-gate 
15507c478bd9Sstevel@tonic-gate 	/*
15517c478bd9Sstevel@tonic-gate 	 * Calc size of buffer to hold all the default acls
15527c478bd9Sstevel@tonic-gate 	 */
15537c478bd9Sstevel@tonic-gate 	def_acl_size =
15547c478bd9Sstevel@tonic-gate 	    acl_count(sp->downer) +	/* def owner */
15557c478bd9Sstevel@tonic-gate 	    acl_count(sp->dgroup) +	/* def owner group */
15567c478bd9Sstevel@tonic-gate 	    acl_count(sp->dother) +	/* def owner other */
15577c478bd9Sstevel@tonic-gate 	    acl_count(sp->dusers) +	/* def users  */
15587c478bd9Sstevel@tonic-gate 	    acl_count(sp->dgroups);	/* def group acls */
15597c478bd9Sstevel@tonic-gate 	if (sp->dclass.acl_ismask)
15607c478bd9Sstevel@tonic-gate 		def_acl_size++;
15617c478bd9Sstevel@tonic-gate 
15627c478bd9Sstevel@tonic-gate 	/*
15637c478bd9Sstevel@tonic-gate 	 * Convert to bytes
15647c478bd9Sstevel@tonic-gate 	 */
15657c478bd9Sstevel@tonic-gate 	def_acl_size *= sizeof (ufs_acl_t);
15667c478bd9Sstevel@tonic-gate 
15677c478bd9Sstevel@tonic-gate 	/*
15687c478bd9Sstevel@tonic-gate 	 * Add fsd header
15697c478bd9Sstevel@tonic-gate 	 */
15707c478bd9Sstevel@tonic-gate 	if (def_acl_size)
15717c478bd9Sstevel@tonic-gate 		def_acl_size += 2 * sizeof (int);
15727c478bd9Sstevel@tonic-gate 
15737c478bd9Sstevel@tonic-gate 	if (acl_size + def_acl_size == 0)
15747c478bd9Sstevel@tonic-gate 		return (0);
15757c478bd9Sstevel@tonic-gate 
15767c478bd9Sstevel@tonic-gate 	buffer = kmem_zalloc((acl_size + def_acl_size), KM_SLEEP);
15777c478bd9Sstevel@tonic-gate 	bufaclp = (ufs_acl_t *)buffer;
15787c478bd9Sstevel@tonic-gate 
15797c478bd9Sstevel@tonic-gate 	if (acl_size == 0)
15807c478bd9Sstevel@tonic-gate 		goto wrtdefs;
15817c478bd9Sstevel@tonic-gate 
15827c478bd9Sstevel@tonic-gate 	/* create fsd and copy acls */
15837c478bd9Sstevel@tonic-gate 	fsdp = (struct ufs_fsd *)bufaclp;
15847c478bd9Sstevel@tonic-gate 	fsdp->fsd_type = FSD_ACL;
15857c478bd9Sstevel@tonic-gate 	bufaclp = (ufs_acl_t *)&fsdp->fsd_data[0];
15867c478bd9Sstevel@tonic-gate 
15877c478bd9Sstevel@tonic-gate 	ACL_MOVE(sp->aowner, USER_OBJ, bufaclp);
15887c478bd9Sstevel@tonic-gate 	ACL_MOVE(sp->agroup, GROUP_OBJ, bufaclp);
15897c478bd9Sstevel@tonic-gate 	ACL_MOVE(sp->aother, OTHER_OBJ, bufaclp);
15907c478bd9Sstevel@tonic-gate 	ACL_MOVE(sp->ausers, USER, bufaclp);
15917c478bd9Sstevel@tonic-gate 	ACL_MOVE(sp->agroups, GROUP, bufaclp);
15927c478bd9Sstevel@tonic-gate 
15937c478bd9Sstevel@tonic-gate 	if (sp->aclass.acl_ismask) {
15947c478bd9Sstevel@tonic-gate 		bufaclp->acl_tag = CLASS_OBJ;
15957c478bd9Sstevel@tonic-gate 		bufaclp->acl_who = (uid_t)sp->aclass.acl_ismask;
15967c478bd9Sstevel@tonic-gate 		bufaclp->acl_perm = (o_mode_t)sp->aclass.acl_maskbits;
15977c478bd9Sstevel@tonic-gate 		bufaclp++;
15987c478bd9Sstevel@tonic-gate 	}
15997c478bd9Sstevel@tonic-gate 	ASSERT(acl_size <= INT_MAX);
16007c478bd9Sstevel@tonic-gate 	fsdp->fsd_size = (int)acl_size;
16017c478bd9Sstevel@tonic-gate 
16027c478bd9Sstevel@tonic-gate wrtdefs:
16037c478bd9Sstevel@tonic-gate 	if (def_acl_size == 0)
16047c478bd9Sstevel@tonic-gate 		goto alldone;
16057c478bd9Sstevel@tonic-gate 
16067c478bd9Sstevel@tonic-gate 	/* if defaults exist then create fsd and copy default acls */
16077c478bd9Sstevel@tonic-gate 	fsdp = (struct ufs_fsd *)bufaclp;
16087c478bd9Sstevel@tonic-gate 	fsdp->fsd_type = FSD_DFACL;
16097c478bd9Sstevel@tonic-gate 	bufaclp = (ufs_acl_t *)&fsdp->fsd_data[0];
16107c478bd9Sstevel@tonic-gate 
16117c478bd9Sstevel@tonic-gate 	ACL_MOVE(sp->downer, DEF_USER_OBJ, bufaclp);
16127c478bd9Sstevel@tonic-gate 	ACL_MOVE(sp->dgroup, DEF_GROUP_OBJ, bufaclp);
16137c478bd9Sstevel@tonic-gate 	ACL_MOVE(sp->dother, DEF_OTHER_OBJ, bufaclp);
16147c478bd9Sstevel@tonic-gate 	ACL_MOVE(sp->dusers, DEF_USER, bufaclp);
16157c478bd9Sstevel@tonic-gate 	ACL_MOVE(sp->dgroups, DEF_GROUP, bufaclp);
16167c478bd9Sstevel@tonic-gate 	if (sp->dclass.acl_ismask) {
16177c478bd9Sstevel@tonic-gate 		bufaclp->acl_tag = DEF_CLASS_OBJ;
16187c478bd9Sstevel@tonic-gate 		bufaclp->acl_who = (uid_t)sp->dclass.acl_ismask;
16197c478bd9Sstevel@tonic-gate 		bufaclp->acl_perm = (o_mode_t)sp->dclass.acl_maskbits;
16207c478bd9Sstevel@tonic-gate 		bufaclp++;
16217c478bd9Sstevel@tonic-gate 	}
16227c478bd9Sstevel@tonic-gate 	ASSERT(def_acl_size <= INT_MAX);
16237c478bd9Sstevel@tonic-gate 	fsdp->fsd_size = (int)def_acl_size;
16247c478bd9Sstevel@tonic-gate 
16257c478bd9Sstevel@tonic-gate alldone:
16267c478bd9Sstevel@tonic-gate 	*buf = buffer;
16277c478bd9Sstevel@tonic-gate 	*len = acl_size + def_acl_size;
16287c478bd9Sstevel@tonic-gate 
16297c478bd9Sstevel@tonic-gate 	return (0);
16307c478bd9Sstevel@tonic-gate }
16317c478bd9Sstevel@tonic-gate 
16327c478bd9Sstevel@tonic-gate /*
16337c478bd9Sstevel@tonic-gate  *  free a shadow inode  on disk and in memory
16347c478bd9Sstevel@tonic-gate  */
16357c478bd9Sstevel@tonic-gate int
ufs_si_free(si_t * sp,struct vfs * vfsp,cred_t * cr)16367c478bd9Sstevel@tonic-gate ufs_si_free(si_t *sp, struct vfs *vfsp, cred_t *cr)
16377c478bd9Sstevel@tonic-gate {
1638*1f563eb1SToomas Soome 	struct inode	*sip;
1639*1f563eb1SToomas Soome 	int		shadow;
1640*1f563eb1SToomas Soome 	int		err = 0;
16417c478bd9Sstevel@tonic-gate 	int		refcnt;
16427c478bd9Sstevel@tonic-gate 	int		signature;
16437c478bd9Sstevel@tonic-gate 
16447c478bd9Sstevel@tonic-gate 	ASSERT(vfsp);
16457c478bd9Sstevel@tonic-gate 	ASSERT(sp);
16467c478bd9Sstevel@tonic-gate 
16477c478bd9Sstevel@tonic-gate 	rw_enter(&sp->s_lock, RW_READER);
16487c478bd9Sstevel@tonic-gate 	ASSERT(sp->s_shadow <= INT_MAX);
16497c478bd9Sstevel@tonic-gate 	shadow = (int)sp->s_shadow;
16507c478bd9Sstevel@tonic-gate 	ASSERT(sp->s_ref);
16517c478bd9Sstevel@tonic-gate 	rw_exit(&sp->s_lock);
16527c478bd9Sstevel@tonic-gate 
16537c478bd9Sstevel@tonic-gate 	/*
16547c478bd9Sstevel@tonic-gate 	 * Decrement link count on the shadow inode,
16557c478bd9Sstevel@tonic-gate 	 * and decrement reference count on the sip.
16567c478bd9Sstevel@tonic-gate 	 */
16577c478bd9Sstevel@tonic-gate 	if ((err = ufs_iget_alloced(vfsp, shadow, &sip, cr)) == 0) {
16587c478bd9Sstevel@tonic-gate 		rw_enter(&sip->i_contents, RW_WRITER);
16597c478bd9Sstevel@tonic-gate 		rw_enter(&sp->s_lock, RW_WRITER);
16607c478bd9Sstevel@tonic-gate 		ASSERT(sp->s_shadow == shadow);
16617c478bd9Sstevel@tonic-gate 		ASSERT(sip->i_dquot == 0);
16627c478bd9Sstevel@tonic-gate 		/* Decrement link count */
16637c478bd9Sstevel@tonic-gate 		ASSERT(sip->i_nlink > 0);
16647c478bd9Sstevel@tonic-gate 		/*
16657c478bd9Sstevel@tonic-gate 		 * bug #1264710 assertion failure below
16667c478bd9Sstevel@tonic-gate 		 */
16677c478bd9Sstevel@tonic-gate 		sp->s_use = --sip->i_nlink;
16687c478bd9Sstevel@tonic-gate 		ufs_setreclaim(sip);
16697c478bd9Sstevel@tonic-gate 		TRANS_INODE(sip->i_ufsvfs, sip);
16707c478bd9Sstevel@tonic-gate 		sip->i_flag |= ICHG | IMOD;
16717c478bd9Sstevel@tonic-gate 		sip->i_seq++;
16727c478bd9Sstevel@tonic-gate 		ITIMES_NOLOCK(sip);
16737c478bd9Sstevel@tonic-gate 		/* Dec ref counts on si referenced by this ip */
16747c478bd9Sstevel@tonic-gate 		refcnt = --sp->s_ref;
16757c478bd9Sstevel@tonic-gate 		signature = sp->s_signature;
16767c478bd9Sstevel@tonic-gate 		ASSERT(sp->s_ref >= 0 && sp->s_ref <= sp->s_use);
16777c478bd9Sstevel@tonic-gate 		/*
16787c478bd9Sstevel@tonic-gate 		 * Release s_lock before calling VN_RELE
16797c478bd9Sstevel@tonic-gate 		 * (which may want to acquire i_contents).
16807c478bd9Sstevel@tonic-gate 		 */
16817c478bd9Sstevel@tonic-gate 		rw_exit(&sp->s_lock);
16827c478bd9Sstevel@tonic-gate 		rw_exit(&sip->i_contents);
16837c478bd9Sstevel@tonic-gate 		VN_RELE(ITOV(sip));
16847c478bd9Sstevel@tonic-gate 	} else {
16857c478bd9Sstevel@tonic-gate 		rw_enter(&sp->s_lock, RW_WRITER);
16867c478bd9Sstevel@tonic-gate 		/* Dec ref counts on si referenced by this ip */
16877c478bd9Sstevel@tonic-gate 		refcnt = --sp->s_ref;
16887c478bd9Sstevel@tonic-gate 		signature = sp->s_signature;
16897c478bd9Sstevel@tonic-gate 		ASSERT(sp->s_ref >= 0 && sp->s_ref <= sp->s_use);
16907c478bd9Sstevel@tonic-gate 		rw_exit(&sp->s_lock);
16917c478bd9Sstevel@tonic-gate 	}
16927c478bd9Sstevel@tonic-gate 
16937c478bd9Sstevel@tonic-gate 	if (refcnt == 0)
16947c478bd9Sstevel@tonic-gate 		si_cache_del(sp, signature);
16957c478bd9Sstevel@tonic-gate 	return (err);
16967c478bd9Sstevel@tonic-gate }
16977c478bd9Sstevel@tonic-gate 
16987c478bd9Sstevel@tonic-gate /*
16997c478bd9Sstevel@tonic-gate  * Seach the si cache for an si structure by inode #.
17007c478bd9Sstevel@tonic-gate  * Returns a locked si structure.
17017c478bd9Sstevel@tonic-gate  *
17027c478bd9Sstevel@tonic-gate  * Parameters:
17037c478bd9Sstevel@tonic-gate  * ip - Ptr to an inode on this fs
17047c478bd9Sstevel@tonic-gate  * spp - Ptr to ptr to si struct for the results, if found.
17057c478bd9Sstevel@tonic-gate  *
17067c478bd9Sstevel@tonic-gate  * Returns:	0 - Success (results in spp)
17077c478bd9Sstevel@tonic-gate  *		1 - Failure (spp undefined)
17087c478bd9Sstevel@tonic-gate  */
17097c478bd9Sstevel@tonic-gate static int
si_cachei_get(struct inode * ip,si_t ** spp)17107c478bd9Sstevel@tonic-gate si_cachei_get(struct inode *ip, si_t **spp)
17117c478bd9Sstevel@tonic-gate {
17127c478bd9Sstevel@tonic-gate 	si_t	*sp;
17137c478bd9Sstevel@tonic-gate 
17147c478bd9Sstevel@tonic-gate 	rw_enter(&si_cache_lock, RW_READER);
17157c478bd9Sstevel@tonic-gate loop:
17167c478bd9Sstevel@tonic-gate 	for (sp = si_cachei[SI_HASH(ip->i_shadow)]; sp; sp = sp->s_forw)
17177c478bd9Sstevel@tonic-gate 		if (sp->s_shadow == ip->i_shadow && sp->s_dev == ip->i_dev)
17187c478bd9Sstevel@tonic-gate 			break;
17197c478bd9Sstevel@tonic-gate 
17207c478bd9Sstevel@tonic-gate 	if (sp == NULL) {
17217c478bd9Sstevel@tonic-gate 		/* Not in cache */
17227c478bd9Sstevel@tonic-gate 		rw_exit(&si_cache_lock);
17237c478bd9Sstevel@tonic-gate 		return (1);
17247c478bd9Sstevel@tonic-gate 	}
17257c478bd9Sstevel@tonic-gate 	/* Found it */
17267c478bd9Sstevel@tonic-gate 	rw_enter(&sp->s_lock, RW_WRITER);
17277c478bd9Sstevel@tonic-gate alldone:
17287c478bd9Sstevel@tonic-gate 	rw_exit(&si_cache_lock);
17297c478bd9Sstevel@tonic-gate 	*spp = sp;
17307c478bd9Sstevel@tonic-gate 	return (0);
17317c478bd9Sstevel@tonic-gate }
17327c478bd9Sstevel@tonic-gate 
17337c478bd9Sstevel@tonic-gate /*
17347c478bd9Sstevel@tonic-gate  * Seach the si cache by si structure (ie duplicate of the one passed in).
17357c478bd9Sstevel@tonic-gate  * In order for a match the signatures must be the same and
17367c478bd9Sstevel@tonic-gate  * the devices must be the same, the acls must match and
17377c478bd9Sstevel@tonic-gate  * link count of the cached shadow must be less than the
17387c478bd9Sstevel@tonic-gate  * size of ic_nlink - 1.  MAXLINK - 1 is used to allow the count
17397c478bd9Sstevel@tonic-gate  * to be incremented one more time by the caller.
17407c478bd9Sstevel@tonic-gate  * Returns a locked si structure.
17417c478bd9Sstevel@tonic-gate  *
17427c478bd9Sstevel@tonic-gate  * Parameters:
17437c478bd9Sstevel@tonic-gate  * ip - Ptr to an inode on this fs
17447c478bd9Sstevel@tonic-gate  * spi - Ptr to si the struct we're searching the cache for.
17457c478bd9Sstevel@tonic-gate  * spp - Ptr to ptr to si struct for the results, if found.
17467c478bd9Sstevel@tonic-gate  *
17477c478bd9Sstevel@tonic-gate  * Returns:	0 - Success (results in spp)
17487c478bd9Sstevel@tonic-gate  *		1 - Failure (spp undefined)
17497c478bd9Sstevel@tonic-gate  */
17507c478bd9Sstevel@tonic-gate static int
si_cachea_get(struct inode * ip,si_t * spi,si_t ** spp)17517c478bd9Sstevel@tonic-gate si_cachea_get(struct inode *ip, si_t *spi, si_t **spp)
17527c478bd9Sstevel@tonic-gate {
17537c478bd9Sstevel@tonic-gate 	si_t	*sp;
17547c478bd9Sstevel@tonic-gate 
17557c478bd9Sstevel@tonic-gate 	spi->s_dev = ip->i_dev;
17567c478bd9Sstevel@tonic-gate 	spi->s_signature = si_signature(spi);
17577c478bd9Sstevel@tonic-gate 	rw_enter(&si_cache_lock, RW_READER);
17587c478bd9Sstevel@tonic-gate loop:
17597c478bd9Sstevel@tonic-gate 	for (sp = si_cachea[SI_HASH(spi->s_signature)]; sp; sp = sp->s_next) {
17607c478bd9Sstevel@tonic-gate 		if (sp->s_signature == spi->s_signature &&
17617c478bd9Sstevel@tonic-gate 		    sp->s_dev == spi->s_dev &&
17627c478bd9Sstevel@tonic-gate 		    sp->s_use > 0 &&			/* deleting */
17637c478bd9Sstevel@tonic-gate 		    sp->s_use <= (MAXLINK - 1) &&	/* Too many links */
17647c478bd9Sstevel@tonic-gate 		    !si_cmp(sp, spi))
17657c478bd9Sstevel@tonic-gate 			break;
17667c478bd9Sstevel@tonic-gate 	}
17677c478bd9Sstevel@tonic-gate 
17687c478bd9Sstevel@tonic-gate 	if (sp == NULL) {
17697c478bd9Sstevel@tonic-gate 		/* Cache miss */
17707c478bd9Sstevel@tonic-gate 		rw_exit(&si_cache_lock);
17717c478bd9Sstevel@tonic-gate 		return (1);
17727c478bd9Sstevel@tonic-gate 	}
17737c478bd9Sstevel@tonic-gate 	/* Found it */
17747c478bd9Sstevel@tonic-gate 	rw_enter(&sp->s_lock, RW_WRITER);
17757c478bd9Sstevel@tonic-gate alldone:
17767c478bd9Sstevel@tonic-gate 	spi->s_shadow = sp->s_shadow; /* XXX For debugging */
17777c478bd9Sstevel@tonic-gate 	rw_exit(&si_cache_lock);
17787c478bd9Sstevel@tonic-gate 	*spp = sp;
17797c478bd9Sstevel@tonic-gate 	return (0);
17807c478bd9Sstevel@tonic-gate }
17817c478bd9Sstevel@tonic-gate 
17827c478bd9Sstevel@tonic-gate /*
17837c478bd9Sstevel@tonic-gate  * Place an si structure in the si cache.  May cause duplicates.
17847c478bd9Sstevel@tonic-gate  *
17857c478bd9Sstevel@tonic-gate  * Parameters:
17867c478bd9Sstevel@tonic-gate  * sp - Ptr to the si struct to add to the cache.
17877c478bd9Sstevel@tonic-gate  *
17887c478bd9Sstevel@tonic-gate  * Returns: Nothing (void)
17897c478bd9Sstevel@tonic-gate  */
17907c478bd9Sstevel@tonic-gate static void
si_cache_put(si_t * sp)17917c478bd9Sstevel@tonic-gate si_cache_put(si_t *sp)
17927c478bd9Sstevel@tonic-gate {
17937c478bd9Sstevel@tonic-gate 	si_t	**tspp;
17947c478bd9Sstevel@tonic-gate 
17957c478bd9Sstevel@tonic-gate 	ASSERT(sp->s_fore == NULL);
17967c478bd9Sstevel@tonic-gate 	rw_enter(&si_cache_lock, RW_WRITER);
17977c478bd9Sstevel@tonic-gate 	if (!sp->s_signature)
17987c478bd9Sstevel@tonic-gate 		sp->s_signature = si_signature(sp);
17997c478bd9Sstevel@tonic-gate 	sp->s_flags |= SI_CACHED;
18007c478bd9Sstevel@tonic-gate 	sp->s_fore = NULL;
18017c478bd9Sstevel@tonic-gate 
18027c478bd9Sstevel@tonic-gate 	/* The 'by acl' chains */
18037c478bd9Sstevel@tonic-gate 	tspp = &si_cachea[SI_HASH(sp->s_signature)];
18047c478bd9Sstevel@tonic-gate 	sp->s_next = *tspp;
18057c478bd9Sstevel@tonic-gate 	*tspp = sp;
18067c478bd9Sstevel@tonic-gate 
18077c478bd9Sstevel@tonic-gate 	/* The 'by inode' chains */
18087c478bd9Sstevel@tonic-gate 	tspp = &si_cachei[SI_HASH(sp->s_shadow)];
18097c478bd9Sstevel@tonic-gate 	sp->s_forw = *tspp;
18107c478bd9Sstevel@tonic-gate 	*tspp = sp;
18117c478bd9Sstevel@tonic-gate 
18127c478bd9Sstevel@tonic-gate 	rw_exit(&si_cache_lock);
18137c478bd9Sstevel@tonic-gate }
18147c478bd9Sstevel@tonic-gate 
18157c478bd9Sstevel@tonic-gate /*
18167c478bd9Sstevel@tonic-gate  * The sp passed in is a candidate for deletion from the cache.  We acquire
18177c478bd9Sstevel@tonic-gate  * the cache lock first, so no cache searches can be done.  Then we search
18187c478bd9Sstevel@tonic-gate  * for the acl in the cache, and if we find it we can lock it and check that
18197c478bd9Sstevel@tonic-gate  * nobody else attached to it while we were acquiring the locks.  If the acl
18207c478bd9Sstevel@tonic-gate  * is in the cache and still has a zero reference count, then we remove it
18217c478bd9Sstevel@tonic-gate  * from the cache and deallocate it.  If the reference count is non-zero or
18227c478bd9Sstevel@tonic-gate  * it is not found in the cache, then someone else attached to it or has
18237c478bd9Sstevel@tonic-gate  * already freed it, so we just return.
18247c478bd9Sstevel@tonic-gate  *
18257c478bd9Sstevel@tonic-gate  * Parameters:
18267c478bd9Sstevel@tonic-gate  * sp - Ptr to the sp struct which is the candicate for deletion.
18277c478bd9Sstevel@tonic-gate  * signature - the signature for the acl for lookup in the hash table
18287c478bd9Sstevel@tonic-gate  *
18297c478bd9Sstevel@tonic-gate  * Returns: Nothing (void)
18307c478bd9Sstevel@tonic-gate  */
18317c478bd9Sstevel@tonic-gate void
si_cache_del(si_t * sp,int signature)18327c478bd9Sstevel@tonic-gate si_cache_del(si_t *sp, int signature)
18337c478bd9Sstevel@tonic-gate {
18347c478bd9Sstevel@tonic-gate 	si_t	**tspp;
18357c478bd9Sstevel@tonic-gate 	int	hash;
18367c478bd9Sstevel@tonic-gate 	int	foundacl = 0;
18377c478bd9Sstevel@tonic-gate 
18387c478bd9Sstevel@tonic-gate 	/*
18397c478bd9Sstevel@tonic-gate 	 * Unlink & free the sp from the other queues, then destroy it.
18407c478bd9Sstevel@tonic-gate 	 * Search the 'by acl' chain first, then the 'by inode' chain
18417c478bd9Sstevel@tonic-gate 	 * after the acl is locked.
18427c478bd9Sstevel@tonic-gate 	 */
18437c478bd9Sstevel@tonic-gate 	rw_enter(&si_cache_lock, RW_WRITER);
18447c478bd9Sstevel@tonic-gate 	hash = SI_HASH(signature);
18457c478bd9Sstevel@tonic-gate 	for (tspp = &si_cachea[hash]; *tspp; tspp = &(*tspp)->s_next) {
18467c478bd9Sstevel@tonic-gate 		if (*tspp == sp) {
18477c478bd9Sstevel@tonic-gate 			/*
18487c478bd9Sstevel@tonic-gate 			 * Wait to grab the acl lock until after the acl has
18497c478bd9Sstevel@tonic-gate 			 * been found in the cache.  Otherwise it might try to
18507c478bd9Sstevel@tonic-gate 			 * grab a lock that has already been destroyed, or
18517c478bd9Sstevel@tonic-gate 			 * delete an acl that has already been freed.
18527c478bd9Sstevel@tonic-gate 			 */
18537c478bd9Sstevel@tonic-gate 			rw_enter(&sp->s_lock, RW_WRITER);
18547c478bd9Sstevel@tonic-gate 			/* See if someone else attached to it */
18557c478bd9Sstevel@tonic-gate 			if (sp->s_ref) {
18567c478bd9Sstevel@tonic-gate 				rw_exit(&sp->s_lock);
18577c478bd9Sstevel@tonic-gate 				rw_exit(&si_cache_lock);
18587c478bd9Sstevel@tonic-gate 				return;
18597c478bd9Sstevel@tonic-gate 			}
18607c478bd9Sstevel@tonic-gate 			ASSERT(sp->s_fore == NULL);
18617c478bd9Sstevel@tonic-gate 			ASSERT(sp->s_flags & SI_CACHED);
18627c478bd9Sstevel@tonic-gate 			foundacl = 1;
18637c478bd9Sstevel@tonic-gate 			*tspp = sp->s_next;
18647c478bd9Sstevel@tonic-gate 			break;
18657c478bd9Sstevel@tonic-gate 		}
18667c478bd9Sstevel@tonic-gate 	}
18677c478bd9Sstevel@tonic-gate 
18687c478bd9Sstevel@tonic-gate 	/*
18697c478bd9Sstevel@tonic-gate 	 * If the acl was not in the cache, we assume another thread has
18707c478bd9Sstevel@tonic-gate 	 * deleted it already. This could happen if another thread attaches to
18717c478bd9Sstevel@tonic-gate 	 * the acl and then releases it after this thread has already found the
18727c478bd9Sstevel@tonic-gate 	 * reference count to be zero but has not yet taken the cache lock.
18737c478bd9Sstevel@tonic-gate 	 * Both threads end up seeing a reference count of zero, and call into
18747c478bd9Sstevel@tonic-gate 	 * si_cache_del.  See bug 4244827 for details on the race condition.
18757c478bd9Sstevel@tonic-gate 	 */
18767c478bd9Sstevel@tonic-gate 	if (foundacl == 0) {
18777c478bd9Sstevel@tonic-gate 		rw_exit(&si_cache_lock);
18787c478bd9Sstevel@tonic-gate 		return;
18797c478bd9Sstevel@tonic-gate 	}
18807c478bd9Sstevel@tonic-gate 
18817c478bd9Sstevel@tonic-gate 	/* Now check the 'by inode' chain */
18827c478bd9Sstevel@tonic-gate 	hash = SI_HASH(sp->s_shadow);
18837c478bd9Sstevel@tonic-gate 	for (tspp = &si_cachei[hash]; *tspp; tspp = &(*tspp)->s_forw) {
18847c478bd9Sstevel@tonic-gate 		if (*tspp == sp) {
18857c478bd9Sstevel@tonic-gate 			*tspp = sp->s_forw;
18867c478bd9Sstevel@tonic-gate 			break;
18877c478bd9Sstevel@tonic-gate 		}
18887c478bd9Sstevel@tonic-gate 	}
18897c478bd9Sstevel@tonic-gate 
18907c478bd9Sstevel@tonic-gate 	/*
18917c478bd9Sstevel@tonic-gate 	 * At this point, we can unlock everything because this si
18927c478bd9Sstevel@tonic-gate 	 * is no longer in the cache, thus cannot be attached to.
18937c478bd9Sstevel@tonic-gate 	 */
18947c478bd9Sstevel@tonic-gate 	rw_exit(&sp->s_lock);
18957c478bd9Sstevel@tonic-gate 	rw_exit(&si_cache_lock);
18967c478bd9Sstevel@tonic-gate 	sp->s_flags &= ~SI_CACHED;
18977c478bd9Sstevel@tonic-gate 	(void) ufs_si_free_mem(sp);
18987c478bd9Sstevel@tonic-gate }
18997c478bd9Sstevel@tonic-gate 
19007c478bd9Sstevel@tonic-gate /*
19017c478bd9Sstevel@tonic-gate  * Alloc the hash buckets for the si cache & initialize
19027c478bd9Sstevel@tonic-gate  * the unreferenced anchor and the cache lock.
19037c478bd9Sstevel@tonic-gate  */
19047c478bd9Sstevel@tonic-gate void
si_cache_init(void)19057c478bd9Sstevel@tonic-gate si_cache_init(void)
19067c478bd9Sstevel@tonic-gate {
19077c478bd9Sstevel@tonic-gate 	rw_init(&si_cache_lock, NULL, RW_DEFAULT, NULL);
19087c478bd9Sstevel@tonic-gate 
19097c478bd9Sstevel@tonic-gate 	/* The 'by acl' headers */
19107c478bd9Sstevel@tonic-gate 	si_cachea = kmem_zalloc(si_cachecnt * sizeof (si_t *), KM_SLEEP);
19117c478bd9Sstevel@tonic-gate 	/* The 'by inode' headers */
19127c478bd9Sstevel@tonic-gate 	si_cachei = kmem_zalloc(si_cachecnt * sizeof (si_t *), KM_SLEEP);
19137c478bd9Sstevel@tonic-gate }
19147c478bd9Sstevel@tonic-gate 
19157c478bd9Sstevel@tonic-gate /*
19167c478bd9Sstevel@tonic-gate  *  aclcksum takes an acl and generates a checksum.  It takes as input
19177c478bd9Sstevel@tonic-gate  *  the acl to start at.
19187c478bd9Sstevel@tonic-gate  *
19197c478bd9Sstevel@tonic-gate  *  s_aclp - pointer to starting acl
19207c478bd9Sstevel@tonic-gate  *
19217c478bd9Sstevel@tonic-gate  *  returns checksum
19227c478bd9Sstevel@tonic-gate  */
19237c478bd9Sstevel@tonic-gate static int
aclcksum(ufs_ic_acl_t * s_aclp)19247c478bd9Sstevel@tonic-gate aclcksum(ufs_ic_acl_t *s_aclp)
19257c478bd9Sstevel@tonic-gate {
19267c478bd9Sstevel@tonic-gate 	ufs_ic_acl_t *aclp;
19277c478bd9Sstevel@tonic-gate 	int signature = 0;
19287c478bd9Sstevel@tonic-gate 	for (aclp = s_aclp; aclp; aclp = aclp->acl_ic_next) {
19297c478bd9Sstevel@tonic-gate 		signature += aclp->acl_ic_perm;
19307c478bd9Sstevel@tonic-gate 		signature += aclp->acl_ic_who;
19317c478bd9Sstevel@tonic-gate 	}
19327c478bd9Sstevel@tonic-gate 	return (signature);
19337c478bd9Sstevel@tonic-gate }
19347c478bd9Sstevel@tonic-gate 
19357c478bd9Sstevel@tonic-gate /*
19367c478bd9Sstevel@tonic-gate  * Generate a unique signature for an si structure.  Used by the
19377c478bd9Sstevel@tonic-gate  * search routine si_cachea_get() to quickly identify candidates
19387c478bd9Sstevel@tonic-gate  * prior to calling si_cmp().
19397c478bd9Sstevel@tonic-gate  * Parameters:
19407c478bd9Sstevel@tonic-gate  * sp - Ptr to the si struct to generate the signature for.
19417c478bd9Sstevel@tonic-gate  *
19427c478bd9Sstevel@tonic-gate  * Returns:  A signature for the si struct (really a checksum)
19437c478bd9Sstevel@tonic-gate  */
19447c478bd9Sstevel@tonic-gate static int
si_signature(si_t * sp)19457c478bd9Sstevel@tonic-gate si_signature(si_t *sp)
19467c478bd9Sstevel@tonic-gate {
19477c478bd9Sstevel@tonic-gate 	int signature = sp->s_dev;
19487c478bd9Sstevel@tonic-gate 
19497c478bd9Sstevel@tonic-gate 	signature += aclcksum(sp->aowner) + aclcksum(sp->agroup) +
19507c478bd9Sstevel@tonic-gate 	    aclcksum(sp->aother) + aclcksum(sp->ausers) +
19517c478bd9Sstevel@tonic-gate 	    aclcksum(sp->agroups) + aclcksum(sp->downer) +
19527c478bd9Sstevel@tonic-gate 	    aclcksum(sp->dgroup) + aclcksum(sp->dother) +
19537c478bd9Sstevel@tonic-gate 	    aclcksum(sp->dusers) + aclcksum(sp->dgroups);
19547c478bd9Sstevel@tonic-gate 	if (sp->aclass.acl_ismask)
19557c478bd9Sstevel@tonic-gate 		signature += sp->aclass.acl_maskbits;
19567c478bd9Sstevel@tonic-gate 	if (sp->dclass.acl_ismask)
19577c478bd9Sstevel@tonic-gate 		signature += sp->dclass.acl_maskbits;
19587c478bd9Sstevel@tonic-gate 
19597c478bd9Sstevel@tonic-gate 	return (signature);
19607c478bd9Sstevel@tonic-gate }
19617c478bd9Sstevel@tonic-gate 
19627c478bd9Sstevel@tonic-gate /*
19637c478bd9Sstevel@tonic-gate  * aclcmp compares to acls to see if they are identical.
19647c478bd9Sstevel@tonic-gate  *
19657c478bd9Sstevel@tonic-gate  * sp1 is source
19667c478bd9Sstevel@tonic-gate  * sp2 is sourceb
19677c478bd9Sstevel@tonic-gate  *
19687c478bd9Sstevel@tonic-gate  * returns 0 if equal and 1 if not equal
19697c478bd9Sstevel@tonic-gate  */
19707c478bd9Sstevel@tonic-gate static int
aclcmp(ufs_ic_acl_t * aclin1p,ufs_ic_acl_t * aclin2p)19717c478bd9Sstevel@tonic-gate aclcmp(ufs_ic_acl_t *aclin1p, ufs_ic_acl_t *aclin2p)
19727c478bd9Sstevel@tonic-gate {
19737c478bd9Sstevel@tonic-gate 	ufs_ic_acl_t *aclp1;
19747c478bd9Sstevel@tonic-gate 	ufs_ic_acl_t *aclp2;
19757c478bd9Sstevel@tonic-gate 
19767c478bd9Sstevel@tonic-gate 	/*
19777c478bd9Sstevel@tonic-gate 	 * if the starting pointers are equal then they are equal so
19787c478bd9Sstevel@tonic-gate 	 * just return.
19797c478bd9Sstevel@tonic-gate 	 */
19807c478bd9Sstevel@tonic-gate 	if (aclin1p == aclin2p)
19817c478bd9Sstevel@tonic-gate 		return (0);
19827c478bd9Sstevel@tonic-gate 	/*
19837c478bd9Sstevel@tonic-gate 	 * check element by element
19847c478bd9Sstevel@tonic-gate 	 */
19857c478bd9Sstevel@tonic-gate 	for (aclp1 = aclin1p, aclp2 = aclin2p; aclp1 && aclp2;
19867c478bd9Sstevel@tonic-gate 	    aclp1 = aclp1->acl_ic_next, aclp2 = aclp2->acl_ic_next) {
19877c478bd9Sstevel@tonic-gate 		if (aclp1->acl_ic_perm != aclp2->acl_ic_perm ||
19887c478bd9Sstevel@tonic-gate 		    aclp1->acl_ic_who != aclp2->acl_ic_who)
19897c478bd9Sstevel@tonic-gate 			return (1);
19907c478bd9Sstevel@tonic-gate 	}
19917c478bd9Sstevel@tonic-gate 	/*
19927c478bd9Sstevel@tonic-gate 	 * both must be zero (at the end of the acl)
19937c478bd9Sstevel@tonic-gate 	 */
19947c478bd9Sstevel@tonic-gate 	if (aclp1 || aclp2)
19957c478bd9Sstevel@tonic-gate 		return (1);
19967c478bd9Sstevel@tonic-gate 
19977c478bd9Sstevel@tonic-gate 	return (0);
19987c478bd9Sstevel@tonic-gate }
19997c478bd9Sstevel@tonic-gate 
20007c478bd9Sstevel@tonic-gate /*
20017c478bd9Sstevel@tonic-gate  * Do extensive, field-by-field compare of two si structures.  Returns
20027c478bd9Sstevel@tonic-gate  * 0 if they are exactly identical, 1 otherwise.
20037c478bd9Sstevel@tonic-gate  *
20047c478bd9Sstevel@tonic-gate  * Paramters:
20057c478bd9Sstevel@tonic-gate  * sp1 - Ptr to 1st si struct
20067c478bd9Sstevel@tonic-gate  * sp2 - Ptr to 2nd si struct
20077c478bd9Sstevel@tonic-gate  *
20087c478bd9Sstevel@tonic-gate  * Returns:
20097c478bd9Sstevel@tonic-gate  *		0 - Not identical
2010*1f563eb1SToomas Soome  *		1 - Identical
20117c478bd9Sstevel@tonic-gate  */
20127c478bd9Sstevel@tonic-gate static int
si_cmp(si_t * sp1,si_t * sp2)20137c478bd9Sstevel@tonic-gate si_cmp(si_t *sp1, si_t *sp2)
20147c478bd9Sstevel@tonic-gate {
20157c478bd9Sstevel@tonic-gate 	if (sp1->s_dev != sp2->s_dev)
20167c478bd9Sstevel@tonic-gate 		return (1);
20177c478bd9Sstevel@tonic-gate 	if (aclcmp(sp1->aowner, sp2->aowner) ||
20187c478bd9Sstevel@tonic-gate 	    aclcmp(sp1->agroup, sp2->agroup) ||
20197c478bd9Sstevel@tonic-gate 	    aclcmp(sp1->aother, sp2->aother) ||
20207c478bd9Sstevel@tonic-gate 	    aclcmp(sp1->ausers, sp2->ausers) ||
20217c478bd9Sstevel@tonic-gate 	    aclcmp(sp1->agroups, sp2->agroups) ||
20227c478bd9Sstevel@tonic-gate 	    aclcmp(sp1->downer, sp2->downer) ||
20237c478bd9Sstevel@tonic-gate 	    aclcmp(sp1->dgroup, sp2->dgroup) ||
20247c478bd9Sstevel@tonic-gate 	    aclcmp(sp1->dother, sp2->dother) ||
20257c478bd9Sstevel@tonic-gate 	    aclcmp(sp1->dusers, sp2->dusers) ||
20267c478bd9Sstevel@tonic-gate 	    aclcmp(sp1->dgroups, sp2->dgroups))
20277c478bd9Sstevel@tonic-gate 		return (1);
20287c478bd9Sstevel@tonic-gate 	if (sp1->aclass.acl_ismask != sp2->aclass.acl_ismask)
20297c478bd9Sstevel@tonic-gate 		return (1);
20307c478bd9Sstevel@tonic-gate 	if (sp1->dclass.acl_ismask != sp2->dclass.acl_ismask)
20317c478bd9Sstevel@tonic-gate 		return (1);
20327c478bd9Sstevel@tonic-gate 	if (sp1->aclass.acl_ismask &&
203380d34432Sfrankho 	    sp1->aclass.acl_maskbits != sp2->aclass.acl_maskbits)
20347c478bd9Sstevel@tonic-gate 		return (1);
20357c478bd9Sstevel@tonic-gate 	if (sp1->dclass.acl_ismask &&
203680d34432Sfrankho 	    sp1->dclass.acl_maskbits != sp2->dclass.acl_maskbits)
20377c478bd9Sstevel@tonic-gate 		return (1);
20387c478bd9Sstevel@tonic-gate 
20397c478bd9Sstevel@tonic-gate 	return (0);
20407c478bd9Sstevel@tonic-gate }
20417c478bd9Sstevel@tonic-gate 
20427c478bd9Sstevel@tonic-gate /*
20437c478bd9Sstevel@tonic-gate  * Remove all acls associated with a device.  All acls must have
20447c478bd9Sstevel@tonic-gate  * a reference count of zero.
20457c478bd9Sstevel@tonic-gate  *
20467c478bd9Sstevel@tonic-gate  * inputs:
20477c478bd9Sstevel@tonic-gate  *	device - device to remove from the cache
20487c478bd9Sstevel@tonic-gate  *
20497c478bd9Sstevel@tonic-gate  * outputs:
20507c478bd9Sstevel@tonic-gate  *	none
20517c478bd9Sstevel@tonic-gate  */
20527c478bd9Sstevel@tonic-gate void
ufs_si_cache_flush(dev_t dev)20537c478bd9Sstevel@tonic-gate ufs_si_cache_flush(dev_t dev)
20547c478bd9Sstevel@tonic-gate {
20557c478bd9Sstevel@tonic-gate 	si_t *tsp, **tspp;
20567c478bd9Sstevel@tonic-gate 	int i;
20577c478bd9Sstevel@tonic-gate 
20587c478bd9Sstevel@tonic-gate 	rw_enter(&si_cache_lock, RW_WRITER);
20597c478bd9Sstevel@tonic-gate 	for (i = 0; i < si_cachecnt; i++) {
20607c478bd9Sstevel@tonic-gate 		tspp = &si_cachea[i];
20617c478bd9Sstevel@tonic-gate 		while (*tspp) {
20627c478bd9Sstevel@tonic-gate 			if ((*tspp)->s_dev == dev) {
20637c478bd9Sstevel@tonic-gate 				*tspp = (*tspp)->s_next;
20647c478bd9Sstevel@tonic-gate 			} else {
20657c478bd9Sstevel@tonic-gate 				tspp = &(*tspp)->s_next;
20667c478bd9Sstevel@tonic-gate 			}
20677c478bd9Sstevel@tonic-gate 		}
20687c478bd9Sstevel@tonic-gate 	}
20697c478bd9Sstevel@tonic-gate 	for (i = 0; i < si_cachecnt; i++) {
20707c478bd9Sstevel@tonic-gate 		tspp = &si_cachei[i];
20717c478bd9Sstevel@tonic-gate 		while (*tspp) {
20727c478bd9Sstevel@tonic-gate 			if ((*tspp)->s_dev == dev) {
20737c478bd9Sstevel@tonic-gate 				tsp = *tspp;
20747c478bd9Sstevel@tonic-gate 				*tspp = (*tspp)->s_forw;
20757c478bd9Sstevel@tonic-gate 				tsp->s_flags &= ~SI_CACHED;
20767c478bd9Sstevel@tonic-gate 				ufs_si_free_mem(tsp);
20777c478bd9Sstevel@tonic-gate 			} else {
20787c478bd9Sstevel@tonic-gate 				tspp = &(*tspp)->s_forw;
20797c478bd9Sstevel@tonic-gate 			}
20807c478bd9Sstevel@tonic-gate 		}
20817c478bd9Sstevel@tonic-gate 	}
20827c478bd9Sstevel@tonic-gate 	rw_exit(&si_cache_lock);
20837c478bd9Sstevel@tonic-gate }
20847c478bd9Sstevel@tonic-gate 
20857c478bd9Sstevel@tonic-gate /*
20867c478bd9Sstevel@tonic-gate  * ufs_si_del is used to unhook a sp from a inode in memory
20877c478bd9Sstevel@tonic-gate  *
20887c478bd9Sstevel@tonic-gate  * ip is the inode to remove the sp from.
20897c478bd9Sstevel@tonic-gate  */
20907c478bd9Sstevel@tonic-gate void
ufs_si_del(struct inode * ip)20917c478bd9Sstevel@tonic-gate ufs_si_del(struct inode *ip)
20927c478bd9Sstevel@tonic-gate {
20937c478bd9Sstevel@tonic-gate 	si_t    *sp = ip->i_ufs_acl;
20947c478bd9Sstevel@tonic-gate 	int	refcnt;
20957c478bd9Sstevel@tonic-gate 	int	signature;
20967c478bd9Sstevel@tonic-gate 
20977c478bd9Sstevel@tonic-gate 	if (sp) {
20987c478bd9Sstevel@tonic-gate 		rw_enter(&sp->s_lock, RW_WRITER);
20997c478bd9Sstevel@tonic-gate 		refcnt = --sp->s_ref;
21007c478bd9Sstevel@tonic-gate 		signature = sp->s_signature;
21017c478bd9Sstevel@tonic-gate 		ASSERT(sp->s_ref >= 0 && sp->s_ref <= sp->s_use);
21027c478bd9Sstevel@tonic-gate 		rw_exit(&sp->s_lock);
21037c478bd9Sstevel@tonic-gate 		if (refcnt == 0)
21047c478bd9Sstevel@tonic-gate 			si_cache_del(sp, signature);
21057c478bd9Sstevel@tonic-gate 		ip->i_ufs_acl = NULL;
21067c478bd9Sstevel@tonic-gate 	}
21077c478bd9Sstevel@tonic-gate }
2108