xref: /onnv-gate/usr/src/uts/common/fs/ufs/ufs_acl.c (revision 7737:005e73e5d5a1)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
54662Sfrankho  * Common Development and Distribution License (the "License").
64662Sfrankho  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*7737SFrank.Batschulat@Sun.COM  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <sys/types.h>
270Sstevel@tonic-gate #include <sys/stat.h>
280Sstevel@tonic-gate #include <sys/errno.h>
290Sstevel@tonic-gate #include <sys/kmem.h>
300Sstevel@tonic-gate #include <sys/t_lock.h>
310Sstevel@tonic-gate #include <sys/ksynch.h>
320Sstevel@tonic-gate #include <sys/buf.h>
330Sstevel@tonic-gate #include <sys/vfs.h>
340Sstevel@tonic-gate #include <sys/vnode.h>
350Sstevel@tonic-gate #include <sys/mode.h>
360Sstevel@tonic-gate #include <sys/systm.h>
370Sstevel@tonic-gate #include <vm/seg.h>
380Sstevel@tonic-gate #include <sys/file.h>
390Sstevel@tonic-gate #include <sys/acl.h>
400Sstevel@tonic-gate #include <sys/fs/ufs_inode.h>
410Sstevel@tonic-gate #include <sys/fs/ufs_acl.h>
420Sstevel@tonic-gate #include <sys/fs/ufs_quota.h>
430Sstevel@tonic-gate #include <sys/sysmacros.h>
440Sstevel@tonic-gate #include <sys/debug.h>
450Sstevel@tonic-gate #include <sys/policy.h>
460Sstevel@tonic-gate 
470Sstevel@tonic-gate /* Cache routines */
480Sstevel@tonic-gate static int si_signature(si_t *);
490Sstevel@tonic-gate static int si_cachei_get(struct inode *, si_t **);
500Sstevel@tonic-gate static int si_cachea_get(struct inode *, si_t *, si_t **);
510Sstevel@tonic-gate static int si_cmp(si_t *, si_t *);
520Sstevel@tonic-gate static void si_cache_put(si_t *);
530Sstevel@tonic-gate void si_cache_del(si_t *, int);
540Sstevel@tonic-gate void si_cache_init(void);
550Sstevel@tonic-gate 
560Sstevel@tonic-gate static void ufs_si_free_mem(si_t *);
570Sstevel@tonic-gate static int ufs_si_store(struct inode *, si_t *, int, cred_t *);
580Sstevel@tonic-gate static si_t *ufs_acl_cp(si_t *);
590Sstevel@tonic-gate static int ufs_sectobuf(si_t *, caddr_t *, size_t *);
600Sstevel@tonic-gate static int acl_count(ufs_ic_acl_t *);
610Sstevel@tonic-gate static int acl_validate(aclent_t *, int, int);
620Sstevel@tonic-gate static int vsecattr2aclentry(vsecattr_t *, si_t **);
630Sstevel@tonic-gate static int aclentry2vsecattr(si_t *, vsecattr_t *);
640Sstevel@tonic-gate 
650Sstevel@tonic-gate krwlock_t si_cache_lock;		/* Protects si_cache */
660Sstevel@tonic-gate int	si_cachecnt = 64;		/* # buckets in si_cache[a|i] */
670Sstevel@tonic-gate si_t	**si_cachea;			/* The 'by acl' cache chains */
680Sstevel@tonic-gate si_t	**si_cachei;			/* The 'by inode' cache chains */
690Sstevel@tonic-gate long	si_cachehit = 0;
700Sstevel@tonic-gate long	si_cachemiss = 0;
710Sstevel@tonic-gate 
720Sstevel@tonic-gate #define	SI_HASH(S)	((int)(S) & (si_cachecnt - 1))
730Sstevel@tonic-gate 
740Sstevel@tonic-gate /*
750Sstevel@tonic-gate  * Store the new acls in aclp.  Attempts to make things atomic.
760Sstevel@tonic-gate  * Search the acl cache for an identical sp and, if found, attach
770Sstevel@tonic-gate  * the cache'd acl to ip. If the acl is new (not in the cache),
780Sstevel@tonic-gate  * add it to the cache, then attach it to ip.  Last, remove and
790Sstevel@tonic-gate  * decrement the reference count of any prior acl list attached
800Sstevel@tonic-gate  * to the ip.
810Sstevel@tonic-gate  *
820Sstevel@tonic-gate  * Parameters:
830Sstevel@tonic-gate  * ip - Ptr to inode to receive the acl list
840Sstevel@tonic-gate  * sp - Ptr to in-core acl structure to attach to the inode.
850Sstevel@tonic-gate  * puship - 0 do not push the object inode(ip) 1 push the ip
860Sstevel@tonic-gate  * cr - Ptr to credentials
870Sstevel@tonic-gate  *
880Sstevel@tonic-gate  * Returns:	0 - Success
890Sstevel@tonic-gate  * 		N - From errno.h
900Sstevel@tonic-gate  */
910Sstevel@tonic-gate static int
ufs_si_store(struct inode * ip,si_t * sp,int puship,cred_t * cr)920Sstevel@tonic-gate ufs_si_store(struct inode *ip, si_t *sp, int puship, cred_t *cr)
930Sstevel@tonic-gate {
940Sstevel@tonic-gate 	struct vfs	*vfsp;
950Sstevel@tonic-gate 	struct inode	*sip;
960Sstevel@tonic-gate 	si_t		*oldsp;
970Sstevel@tonic-gate 	si_t		*csp;
980Sstevel@tonic-gate 	caddr_t		acldata;
990Sstevel@tonic-gate 	ino_t		oldshadow;
1000Sstevel@tonic-gate 	size_t		acldatalen;
1010Sstevel@tonic-gate 	off_t		offset;
1020Sstevel@tonic-gate 	int		shadow;
1030Sstevel@tonic-gate 	int		err;
1040Sstevel@tonic-gate 	int		refcnt;
1050Sstevel@tonic-gate 	int		usecnt;
1060Sstevel@tonic-gate 	int		signature;
1070Sstevel@tonic-gate 	int		resid;
1080Sstevel@tonic-gate 	struct ufsvfs	*ufsvfsp	= ip->i_ufsvfs;
1090Sstevel@tonic-gate 	struct fs	*fs		= ufsvfsp->vfs_fs;
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&ip->i_contents));
1120Sstevel@tonic-gate 	ASSERT(ip->i_ufs_acl != sp);
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 	if (!CHECK_ACL_ALLOWED(ip->i_mode & IFMT))
1150Sstevel@tonic-gate 		return (ENOSYS);
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 	/*
1180Sstevel@tonic-gate 	 * if there are only the three owner/group/other then do not
1190Sstevel@tonic-gate 	 * create a shadow inode.  If there is already a shadow with
1200Sstevel@tonic-gate 	 * the file, remove it.
1210Sstevel@tonic-gate 	 *
1220Sstevel@tonic-gate 	 */
1230Sstevel@tonic-gate 	if (!sp->ausers &&
1240Sstevel@tonic-gate 	    !sp->agroups &&
1250Sstevel@tonic-gate 	    !sp->downer &&
1260Sstevel@tonic-gate 	    !sp->dgroup &&
1270Sstevel@tonic-gate 	    !sp->dother &&
1280Sstevel@tonic-gate 	    sp->dclass.acl_ismask == 0 &&
1290Sstevel@tonic-gate 	    !sp->dusers &&
1300Sstevel@tonic-gate 	    !sp->dgroups) {
1310Sstevel@tonic-gate 		if (ip->i_ufs_acl)
1320Sstevel@tonic-gate 			err = ufs_si_free(ip->i_ufs_acl, ITOV(ip)->v_vfsp, cr);
1330Sstevel@tonic-gate 		ip->i_ufs_acl = NULL;
1340Sstevel@tonic-gate 		ip->i_shadow = 0;
1350Sstevel@tonic-gate 		ip->i_flag |= IMOD | IACC;
1360Sstevel@tonic-gate 		ip->i_mode = (ip->i_smode & ~0777) |
1370Sstevel@tonic-gate 		    ((sp->aowner->acl_ic_perm & 07) << 6) |
1385058Sprabahar 		    (MASK2MODE(sp)) |
1390Sstevel@tonic-gate 		    (sp->aother->acl_ic_perm & 07);
1400Sstevel@tonic-gate 		TRANS_INODE(ip->i_ufsvfs, ip);
1410Sstevel@tonic-gate 		ufs_iupdat(ip, 1);
1420Sstevel@tonic-gate 		ufs_si_free_mem(sp);
1430Sstevel@tonic-gate 		return (0);
1440Sstevel@tonic-gate 	}
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate loop:
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	/*
1490Sstevel@tonic-gate 	 * Check cache. If in cache, use existing shadow inode.
1500Sstevel@tonic-gate 	 * Increment the shadow link count, then attach to the
1510Sstevel@tonic-gate 	 * cached ufs_acl_entry struct, and increment it's reference
1520Sstevel@tonic-gate 	 * count.  Then discard the passed-in ufs_acl_entry and
1530Sstevel@tonic-gate 	 * return.
1540Sstevel@tonic-gate 	 */
1550Sstevel@tonic-gate 	if (si_cachea_get(ip, sp, &csp) == 0) {
1560Sstevel@tonic-gate 		ASSERT(RW_WRITE_HELD(&csp->s_lock));
1570Sstevel@tonic-gate 		if (ip->i_ufs_acl == csp) {
1580Sstevel@tonic-gate 			rw_exit(&csp->s_lock);
1590Sstevel@tonic-gate 			(void) ufs_si_free_mem(sp);
1600Sstevel@tonic-gate 			return (0);
1610Sstevel@tonic-gate 		}
1620Sstevel@tonic-gate 		vfsp = ITOV(ip)->v_vfsp;
1630Sstevel@tonic-gate 		ASSERT(csp->s_shadow <= INT_MAX);
1640Sstevel@tonic-gate 		shadow = (int)csp->s_shadow;
1650Sstevel@tonic-gate 		/*
1660Sstevel@tonic-gate 		 * We can't call ufs_iget while holding the csp locked,
1670Sstevel@tonic-gate 		 * because we might deadlock.  So we drop the
1680Sstevel@tonic-gate 		 * lock on csp, then go search the si_cache again
1690Sstevel@tonic-gate 		 * to see if the csp is still there.
1700Sstevel@tonic-gate 		 */
1710Sstevel@tonic-gate 		rw_exit(&csp->s_lock);
1720Sstevel@tonic-gate 		if ((err = ufs_iget(vfsp, shadow, &sip, cr)) != 0) {
1730Sstevel@tonic-gate 			(void) ufs_si_free_mem(sp);
1740Sstevel@tonic-gate 			return (EIO);
1750Sstevel@tonic-gate 		}
1760Sstevel@tonic-gate 		rw_enter(&sip->i_contents, RW_WRITER);
1770Sstevel@tonic-gate 		if ((sip->i_mode & IFMT) != IFSHAD || sip->i_nlink <= 0) {
1780Sstevel@tonic-gate 			rw_exit(&sip->i_contents);
1790Sstevel@tonic-gate 			VN_RELE(ITOV(sip));
1800Sstevel@tonic-gate 			goto loop;
1810Sstevel@tonic-gate 		}
1820Sstevel@tonic-gate 		/* Get the csp again */
1830Sstevel@tonic-gate 		if (si_cachea_get(ip, sp, &csp) != 0) {
1840Sstevel@tonic-gate 			rw_exit(&sip->i_contents);
1850Sstevel@tonic-gate 			VN_RELE(ITOV(sip));
1860Sstevel@tonic-gate 			goto loop;
1870Sstevel@tonic-gate 		}
1880Sstevel@tonic-gate 		ASSERT(RW_WRITE_HELD(&csp->s_lock));
1890Sstevel@tonic-gate 		/* See if we got the right shadow */
1900Sstevel@tonic-gate 		if (csp->s_shadow != shadow) {
1910Sstevel@tonic-gate 			rw_exit(&csp->s_lock);
1920Sstevel@tonic-gate 			rw_exit(&sip->i_contents);
1930Sstevel@tonic-gate 			VN_RELE(ITOV(sip));
1940Sstevel@tonic-gate 			goto loop;
1950Sstevel@tonic-gate 		}
1960Sstevel@tonic-gate 		ASSERT(RW_WRITE_HELD(&sip->i_contents));
1970Sstevel@tonic-gate 		ASSERT(sip->i_dquot == 0);
1980Sstevel@tonic-gate 		/* Increment link count */
1990Sstevel@tonic-gate 		ASSERT(sip->i_nlink > 0);
2000Sstevel@tonic-gate 		sip->i_nlink++;
2010Sstevel@tonic-gate 		TRANS_INODE(ufsvfsp, sip);
2020Sstevel@tonic-gate 		csp->s_use = sip->i_nlink;
2030Sstevel@tonic-gate 		csp->s_ref++;
2040Sstevel@tonic-gate 		ASSERT(sp->s_ref >= 0 && sp->s_ref <= sp->s_use);
2050Sstevel@tonic-gate 		sip->i_flag |= ICHG | IMOD;
2060Sstevel@tonic-gate 		sip->i_seq++;
2070Sstevel@tonic-gate 		ITIMES_NOLOCK(sip);
2080Sstevel@tonic-gate 		/*
2090Sstevel@tonic-gate 		 * Always release s_lock before both releasing i_contents
2100Sstevel@tonic-gate 		 * and calling VN_RELE.
2110Sstevel@tonic-gate 		 */
2120Sstevel@tonic-gate 		rw_exit(&csp->s_lock);
2130Sstevel@tonic-gate 		rw_exit(&sip->i_contents);
2140Sstevel@tonic-gate 		VN_RELE(ITOV(sip));
2150Sstevel@tonic-gate 		(void) ufs_si_free_mem(sp);
2160Sstevel@tonic-gate 		sp = csp;
2170Sstevel@tonic-gate 		si_cachehit++;
2180Sstevel@tonic-gate 		goto switchshadows;
2190Sstevel@tonic-gate 	}
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 	/* Alloc a shadow inode and fill it in */
2220Sstevel@tonic-gate 	err = ufs_ialloc(ip, ip->i_number, (mode_t)IFSHAD, &sip, cr);
2230Sstevel@tonic-gate 	if (err) {
2240Sstevel@tonic-gate 		(void) ufs_si_free_mem(sp);
2250Sstevel@tonic-gate 		return (err);
2260Sstevel@tonic-gate 	}
2270Sstevel@tonic-gate 	rw_enter(&sip->i_contents, RW_WRITER);
2280Sstevel@tonic-gate 	sip->i_flag |= IACC | IUPD | ICHG;
2290Sstevel@tonic-gate 	sip->i_seq++;
2300Sstevel@tonic-gate 	sip->i_mode = (o_mode_t)IFSHAD;
2310Sstevel@tonic-gate 	ITOV(sip)->v_type = VREG;
2325244Sbatschul 	ufs_reset_vnode(ITOV(sip));
2330Sstevel@tonic-gate 	sip->i_nlink = 1;
2340Sstevel@tonic-gate 	sip->i_uid = crgetuid(cr);
2350Sstevel@tonic-gate 	sip->i_suid = (ulong_t)sip->i_uid > (ulong_t)USHRT_MAX ?
2364662Sfrankho 	    UID_LONG : sip->i_uid;
2370Sstevel@tonic-gate 	sip->i_gid = crgetgid(cr);
2380Sstevel@tonic-gate 	sip->i_sgid = (ulong_t)sip->i_gid > (ulong_t)USHRT_MAX ?
2394662Sfrankho 	    GID_LONG : sip->i_gid;
2400Sstevel@tonic-gate 	sip->i_shadow = 0;
2410Sstevel@tonic-gate 	TRANS_INODE(ufsvfsp, sip);
2420Sstevel@tonic-gate 	sip->i_ufs_acl = NULL;
2430Sstevel@tonic-gate 	ASSERT(sip->i_size == 0);
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 	sp->s_shadow = sip->i_number;
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate 	if ((err = ufs_sectobuf(sp, &acldata, &acldatalen)) != 0)
2480Sstevel@tonic-gate 		goto errout;
2490Sstevel@tonic-gate 	offset = 0;
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate 	/*
2520Sstevel@tonic-gate 	 * We don't actually care about the residual count upon failure,
2530Sstevel@tonic-gate 	 * but giving ufs_rdwri() the pointer means it won't translate
2540Sstevel@tonic-gate 	 * all failures to EIO.  Our caller needs to know when ENOSPC
2550Sstevel@tonic-gate 	 * gets hit.
2560Sstevel@tonic-gate 	 */
2570Sstevel@tonic-gate 	resid = 0;
2580Sstevel@tonic-gate 	if (((err = ufs_rdwri(UIO_WRITE, FWRITE|FSYNC, sip, acldata,
2590Sstevel@tonic-gate 	    acldatalen, (offset_t)0, UIO_SYSSPACE, &resid, cr)) != 0) ||
2600Sstevel@tonic-gate 	    (resid != 0)) {
2610Sstevel@tonic-gate 		kmem_free(acldata, acldatalen);
2620Sstevel@tonic-gate 		if ((resid != 0) && (err == 0))
2630Sstevel@tonic-gate 			err = ENOSPC;
2640Sstevel@tonic-gate 		goto errout;
2650Sstevel@tonic-gate 	}
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate 	offset += acldatalen;
2680Sstevel@tonic-gate 	if ((acldatalen + fs->fs_bsize) > ufsvfsp->vfs_maxacl)
2690Sstevel@tonic-gate 		ufsvfsp->vfs_maxacl = acldatalen + fs->fs_bsize;
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate 	kmem_free(acldata, acldatalen);
2720Sstevel@tonic-gate 	/* Sync & free the shadow inode */
2730Sstevel@tonic-gate 	ufs_iupdat(sip, 1);
2740Sstevel@tonic-gate 	rw_exit(&sip->i_contents);
2750Sstevel@tonic-gate 	VN_RELE(ITOV(sip));
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 	/* We're committed to using this sp */
2780Sstevel@tonic-gate 	sp->s_use = 1;
2790Sstevel@tonic-gate 	sp->s_ref = 1;
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate 	/* Now put the new acl stuff in the cache */
2820Sstevel@tonic-gate 	/* XXX Might make a duplicate */
2830Sstevel@tonic-gate 	si_cache_put(sp);
2840Sstevel@tonic-gate 	si_cachemiss++;
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate switchshadows:
2870Sstevel@tonic-gate 	/* Now switch the parent inode to use the new shadow inode */
2880Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&ip->i_contents));
2890Sstevel@tonic-gate 	rw_enter(&sp->s_lock, RW_READER);
2900Sstevel@tonic-gate 	oldsp = ip->i_ufs_acl;
2910Sstevel@tonic-gate 	oldshadow = ip->i_shadow;
2920Sstevel@tonic-gate 	ip->i_ufs_acl = sp;
2930Sstevel@tonic-gate 	ASSERT(sp->s_shadow <= INT_MAX);
2940Sstevel@tonic-gate 	ip->i_shadow = (int32_t)sp->s_shadow;
2950Sstevel@tonic-gate 	ASSERT(oldsp != sp);
2960Sstevel@tonic-gate 	ASSERT(oldshadow != ip->i_number);
2970Sstevel@tonic-gate 	ASSERT(ip->i_number != ip->i_shadow);
2980Sstevel@tonic-gate 	/*
2990Sstevel@tonic-gate 	 * Change the mode bits to follow the acl list
3000Sstevel@tonic-gate 	 *
3010Sstevel@tonic-gate 	 * NOTE:	a directory is not required to have a "regular" acl
3020Sstevel@tonic-gate 	 *		bug id's 1238908,  1257173, 1263171 and 1263188
3030Sstevel@tonic-gate 	 *
3040Sstevel@tonic-gate 	 *		but if a "regular" acl is present, it must contain
3050Sstevel@tonic-gate 	 *		an "owner", "group", and "other" acl
3060Sstevel@tonic-gate 	 *
3070Sstevel@tonic-gate 	 *		If an ACL mask exists, the effective group rights are
3080Sstevel@tonic-gate 	 *		set to the mask.  Otherwise, the effective group rights
3090Sstevel@tonic-gate 	 * 		are set to the object group bits.
3100Sstevel@tonic-gate 	 */
3110Sstevel@tonic-gate 	if (sp->aowner) {				/* Owner */
3120Sstevel@tonic-gate 		ip->i_mode &= ~0700;			/* clear Owner */
3130Sstevel@tonic-gate 		ip->i_mode |= (sp->aowner->acl_ic_perm & 07) << 6;
3140Sstevel@tonic-gate 		ip->i_uid = sp->aowner->acl_ic_who;
3150Sstevel@tonic-gate 	}
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 	if (sp->agroup) {				/* Group */
3180Sstevel@tonic-gate 		ip->i_mode &= ~0070;			/* clear Group */
3195058Sprabahar 		ip->i_mode |= MASK2MODE(sp);		/* apply mask */
3200Sstevel@tonic-gate 		ip->i_gid = sp->agroup->acl_ic_who;
3210Sstevel@tonic-gate 	}
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 	if (sp->aother) {				/* Other */
3240Sstevel@tonic-gate 		ip->i_mode &= ~0007;			/* clear Other */
3250Sstevel@tonic-gate 		ip->i_mode |= (sp->aother->acl_ic_perm & 07);
3260Sstevel@tonic-gate 	}
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate 	if (sp->aclass.acl_ismask)
3290Sstevel@tonic-gate 		ip->i_mode = (ip->i_mode & ~070) |
3300Sstevel@tonic-gate 		    (((sp->aclass.acl_maskbits & 07) << 3) &
3310Sstevel@tonic-gate 		    ip->i_mode);
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate 	TRANS_INODE(ufsvfsp, ip);
3340Sstevel@tonic-gate 	rw_exit(&sp->s_lock);
3350Sstevel@tonic-gate 	ip->i_flag |= ICHG;
3360Sstevel@tonic-gate 	ip->i_seq++;
3370Sstevel@tonic-gate 	/*
3380Sstevel@tonic-gate 	 * when creating a file there is no need to push the inode, it
3390Sstevel@tonic-gate 	 * is pushed later
3400Sstevel@tonic-gate 	 */
3410Sstevel@tonic-gate 	if (puship == 1)
3420Sstevel@tonic-gate 		ufs_iupdat(ip, 1);
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 	/*
3450Sstevel@tonic-gate 	 * Decrement link count on the old shadow inode,
3460Sstevel@tonic-gate 	 * and decrement reference count on the old aclp,
3470Sstevel@tonic-gate 	 */
3480Sstevel@tonic-gate 	if (oldshadow) {
3490Sstevel@tonic-gate 		/* Get the shadow inode */
3500Sstevel@tonic-gate 		ASSERT(RW_WRITE_HELD(&ip->i_contents));
3510Sstevel@tonic-gate 		vfsp = ITOV(ip)->v_vfsp;
3520Sstevel@tonic-gate 		if ((err = ufs_iget_alloced(vfsp, oldshadow, &sip, cr)) != 0) {
3530Sstevel@tonic-gate 			return (EIO);
3540Sstevel@tonic-gate 		}
3550Sstevel@tonic-gate 		/* Decrement link count */
3560Sstevel@tonic-gate 		rw_enter(&sip->i_contents, RW_WRITER);
3570Sstevel@tonic-gate 		if (oldsp)
3580Sstevel@tonic-gate 			rw_enter(&oldsp->s_lock, RW_WRITER);
3590Sstevel@tonic-gate 		ASSERT(sip->i_dquot == 0);
3600Sstevel@tonic-gate 		ASSERT(sip->i_nlink > 0);
3610Sstevel@tonic-gate 		usecnt = --sip->i_nlink;
3620Sstevel@tonic-gate 		ufs_setreclaim(sip);
3630Sstevel@tonic-gate 		TRANS_INODE(ufsvfsp, sip);
3640Sstevel@tonic-gate 		sip->i_flag |= ICHG | IMOD;
3650Sstevel@tonic-gate 		sip->i_seq++;
3660Sstevel@tonic-gate 		ITIMES_NOLOCK(sip);
3670Sstevel@tonic-gate 		if (oldsp) {
3680Sstevel@tonic-gate 			oldsp->s_use = usecnt;
3690Sstevel@tonic-gate 			refcnt = --oldsp->s_ref;
3700Sstevel@tonic-gate 			signature = oldsp->s_signature;
3710Sstevel@tonic-gate 			/*
3720Sstevel@tonic-gate 			 * Always release s_lock before both releasing
3730Sstevel@tonic-gate 			 * i_contents and calling VN_RELE.
3740Sstevel@tonic-gate 			 */
3750Sstevel@tonic-gate 			rw_exit(&oldsp->s_lock);
3760Sstevel@tonic-gate 		}
3770Sstevel@tonic-gate 		rw_exit(&sip->i_contents);
3780Sstevel@tonic-gate 		VN_RELE(ITOV(sip));
3790Sstevel@tonic-gate 		if (oldsp && (refcnt == 0))
3800Sstevel@tonic-gate 			si_cache_del(oldsp, signature);
3810Sstevel@tonic-gate 	}
3820Sstevel@tonic-gate 	return (0);
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate errout:
3850Sstevel@tonic-gate 	/* Throw the newly alloc'd inode away */
3860Sstevel@tonic-gate 	sip->i_nlink = 0;
3870Sstevel@tonic-gate 	ufs_setreclaim(sip);
3880Sstevel@tonic-gate 	TRANS_INODE(ufsvfsp, sip);
3890Sstevel@tonic-gate 	ITIMES_NOLOCK(sip);
3900Sstevel@tonic-gate 	rw_exit(&sip->i_contents);
3910Sstevel@tonic-gate 	VN_RELE(ITOV(sip));
3920Sstevel@tonic-gate 	ASSERT(!sp->s_use && !sp->s_ref && !(sp->s_flags & SI_CACHED));
3930Sstevel@tonic-gate 	(void) ufs_si_free_mem(sp);
3940Sstevel@tonic-gate 	return (err);
3950Sstevel@tonic-gate }
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate /*
3980Sstevel@tonic-gate  * Load the acls for inode ip either from disk (adding to the cache),
3990Sstevel@tonic-gate  * or search the cache and attach the cache'd acl list to the ip.
4000Sstevel@tonic-gate  * In either case, maintain the proper reference count on the cached entry.
4010Sstevel@tonic-gate  *
4020Sstevel@tonic-gate  * Parameters:
4030Sstevel@tonic-gate  * ip - Ptr to the inode which needs the acl list loaded
4040Sstevel@tonic-gate  * cr - Ptr to credentials
4050Sstevel@tonic-gate  *
4060Sstevel@tonic-gate  * Returns:	0 - Success
4070Sstevel@tonic-gate  * 		N - From errno.h
4080Sstevel@tonic-gate  */
4090Sstevel@tonic-gate int
ufs_si_load(struct inode * ip,cred_t * cr)4100Sstevel@tonic-gate ufs_si_load(struct inode *ip, cred_t *cr)
4110Sstevel@tonic-gate /*
4120Sstevel@tonic-gate  *	ip	parent inode in
4130Sstevel@tonic-gate  *	cr	credentials in
4140Sstevel@tonic-gate  */
4150Sstevel@tonic-gate {
4160Sstevel@tonic-gate 	struct vfs	*vfsp;
4170Sstevel@tonic-gate 	struct inode	*sip;
4180Sstevel@tonic-gate 	ufs_fsd_t	*fsdp;
4190Sstevel@tonic-gate 	si_t		*sp;
4200Sstevel@tonic-gate 	vsecattr_t	vsecattr = {
4210Sstevel@tonic-gate 				(uint_t)0,
4220Sstevel@tonic-gate 				(int)0,
4230Sstevel@tonic-gate 				(void *)NULL,
4240Sstevel@tonic-gate 				(int)0,
4250Sstevel@tonic-gate 				(void *)NULL};
4260Sstevel@tonic-gate 	aclent_t	*aclp;
4270Sstevel@tonic-gate 	ufs_acl_t	*ufsaclp;
4280Sstevel@tonic-gate 	caddr_t		acldata = NULL;
4290Sstevel@tonic-gate 	ino_t		maxino;
4300Sstevel@tonic-gate 	int		err;
4310Sstevel@tonic-gate 	size_t		acldatalen;
4320Sstevel@tonic-gate 	int		numacls;
4330Sstevel@tonic-gate 	int		shadow;
4340Sstevel@tonic-gate 	int		usecnt;
4350Sstevel@tonic-gate 	struct ufsvfs	*ufsvfsp	= ip->i_ufsvfs;
4360Sstevel@tonic-gate 	struct fs	*fs		= ufsvfsp->vfs_fs;
4370Sstevel@tonic-gate 
4380Sstevel@tonic-gate 	ASSERT(ip != NULL);
4390Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&ip->i_contents));
4400Sstevel@tonic-gate 	ASSERT(ip->i_shadow && ip->i_ufs_acl == NULL);
4410Sstevel@tonic-gate 	ASSERT((ip->i_mode & IFMT) != IFSHAD);
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate 	if (!CHECK_ACL_ALLOWED(ip->i_mode & IFMT))
4440Sstevel@tonic-gate 		return (ENOSYS);
4450Sstevel@tonic-gate 
4460Sstevel@tonic-gate 	if (ip->i_shadow == ip->i_number)
4470Sstevel@tonic-gate 		return (EIO);
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate 	maxino = (ino_t)(ITOF(ip)->fs_ncg * ITOF(ip)->fs_ipg);
4500Sstevel@tonic-gate 	if (ip->i_shadow < UFSROOTINO || ip->i_shadow > maxino)
4510Sstevel@tonic-gate 		return (EIO);
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate 	/*
4540Sstevel@tonic-gate 	 * XXX Check cache.  If in cache, link to it and increment
4550Sstevel@tonic-gate 	 * the reference count, then return.
4560Sstevel@tonic-gate 	 */
4570Sstevel@tonic-gate 	if (si_cachei_get(ip, &sp) == 0) {
4580Sstevel@tonic-gate 		ASSERT(RW_WRITE_HELD(&sp->s_lock));
4590Sstevel@tonic-gate 		ip->i_ufs_acl = sp;
4600Sstevel@tonic-gate 		sp->s_ref++;
4610Sstevel@tonic-gate 		ASSERT(sp->s_ref >= 0 && sp->s_ref <= sp->s_use);
4620Sstevel@tonic-gate 		rw_exit(&sp->s_lock);
4630Sstevel@tonic-gate 		si_cachehit++;
4640Sstevel@tonic-gate 		return (0);
4650Sstevel@tonic-gate 	}
4660Sstevel@tonic-gate 
4670Sstevel@tonic-gate 	/* Get the shadow inode */
4680Sstevel@tonic-gate 	vfsp = ITOV(ip)->v_vfsp;
4690Sstevel@tonic-gate 	shadow = ip->i_shadow;
4700Sstevel@tonic-gate 	if ((err = ufs_iget_alloced(vfsp, shadow, &sip, cr)) != 0) {
4710Sstevel@tonic-gate 		return (err);
4720Sstevel@tonic-gate 	}
4730Sstevel@tonic-gate 	rw_enter(&sip->i_contents, RW_WRITER);
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate 	if ((sip->i_mode & IFMT) != IFSHAD) {
4760Sstevel@tonic-gate 		rw_exit(&sip->i_contents);
4770Sstevel@tonic-gate 		err = EINVAL;
4780Sstevel@tonic-gate 		goto alldone;
4790Sstevel@tonic-gate 	}
4800Sstevel@tonic-gate 
4810Sstevel@tonic-gate 	ASSERT(sip->i_dquot == 0);
4820Sstevel@tonic-gate 	usecnt = sip->i_nlink;
4830Sstevel@tonic-gate 	if ((!ULOCKFS_IS_NOIACC(&ufsvfsp->vfs_ulockfs)) &&
4840Sstevel@tonic-gate 	    (!(sip)->i_ufsvfs->vfs_noatime)) {
4850Sstevel@tonic-gate 		sip->i_flag |= IACC;
4860Sstevel@tonic-gate 	}
4870Sstevel@tonic-gate 	rw_downgrade(&sip->i_contents);
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate 	ASSERT(sip->i_size <= MAXOFF_T);
4900Sstevel@tonic-gate 	/* Read the acl's and other stuff from disk */
4910Sstevel@tonic-gate 	acldata	 = kmem_zalloc((size_t)sip->i_size, KM_SLEEP);
4920Sstevel@tonic-gate 	acldatalen = sip->i_size;
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate 	err = ufs_rdwri(UIO_READ, FREAD, sip, acldata, acldatalen, (offset_t)0,
4950Sstevel@tonic-gate 	    UIO_SYSSPACE, (int *)0, cr);
4960Sstevel@tonic-gate 
4970Sstevel@tonic-gate 	rw_exit(&sip->i_contents);
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate 	if (err)
5000Sstevel@tonic-gate 		goto alldone;
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate 	/*
5030Sstevel@tonic-gate 	 * Convert from disk format
5040Sstevel@tonic-gate 	 * Result is a vsecattr struct which we then convert to the
5050Sstevel@tonic-gate 	 * si struct.
5060Sstevel@tonic-gate 	 */
5070Sstevel@tonic-gate 	bzero((caddr_t)&vsecattr, sizeof (vsecattr_t));
5080Sstevel@tonic-gate 	for (fsdp = (ufs_fsd_t *)acldata;
5090Sstevel@tonic-gate 			fsdp < (ufs_fsd_t *)(acldata + acldatalen);
5100Sstevel@tonic-gate 			fsdp = (ufs_fsd_t *)((caddr_t)fsdp +
5110Sstevel@tonic-gate 				FSD_RECSZ(fsdp, fsdp->fsd_size))) {
5120Sstevel@tonic-gate 		if (fsdp->fsd_size <= 0)
5130Sstevel@tonic-gate 			break;
5140Sstevel@tonic-gate 		switch (fsdp->fsd_type) {
5150Sstevel@tonic-gate 		case FSD_ACL:
5160Sstevel@tonic-gate 			numacls = vsecattr.vsa_aclcnt =
5170Sstevel@tonic-gate 				(int)((fsdp->fsd_size - 2 * sizeof (int)) /
5180Sstevel@tonic-gate 							sizeof (ufs_acl_t));
5190Sstevel@tonic-gate 			aclp = vsecattr.vsa_aclentp =
5200Sstevel@tonic-gate 			kmem_zalloc(numacls * sizeof (aclent_t), KM_SLEEP);
5210Sstevel@tonic-gate 			for (ufsaclp = (ufs_acl_t *)fsdp->fsd_data;
5220Sstevel@tonic-gate 							numacls; ufsaclp++) {
5230Sstevel@tonic-gate 				aclp->a_type = ufsaclp->acl_tag;
5240Sstevel@tonic-gate 				aclp->a_id = ufsaclp->acl_who;
5250Sstevel@tonic-gate 				aclp->a_perm = ufsaclp->acl_perm;
5260Sstevel@tonic-gate 				aclp++;
5270Sstevel@tonic-gate 				numacls--;
5280Sstevel@tonic-gate 			}
5290Sstevel@tonic-gate 			break;
5300Sstevel@tonic-gate 		case FSD_DFACL:
5310Sstevel@tonic-gate 			numacls = vsecattr.vsa_dfaclcnt =
5320Sstevel@tonic-gate 				(int)((fsdp->fsd_size - 2 * sizeof (int)) /
5330Sstevel@tonic-gate 							sizeof (ufs_acl_t));
5340Sstevel@tonic-gate 			aclp = vsecattr.vsa_dfaclentp =
5350Sstevel@tonic-gate 			kmem_zalloc(numacls * sizeof (aclent_t), KM_SLEEP);
5360Sstevel@tonic-gate 			for (ufsaclp = (ufs_acl_t *)fsdp->fsd_data;
5370Sstevel@tonic-gate 							numacls; ufsaclp++) {
5380Sstevel@tonic-gate 				aclp->a_type = ufsaclp->acl_tag;
5390Sstevel@tonic-gate 				aclp->a_id = ufsaclp->acl_who;
5400Sstevel@tonic-gate 				aclp->a_perm = ufsaclp->acl_perm;
5410Sstevel@tonic-gate 				aclp++;
5420Sstevel@tonic-gate 				numacls--;
5430Sstevel@tonic-gate 			}
5440Sstevel@tonic-gate 			break;
5450Sstevel@tonic-gate 		}
5460Sstevel@tonic-gate 	}
5470Sstevel@tonic-gate 	/* Sort the lists */
5480Sstevel@tonic-gate 	if (vsecattr.vsa_aclentp) {
5490Sstevel@tonic-gate 		ksort((caddr_t)vsecattr.vsa_aclentp, vsecattr.vsa_aclcnt,
5500Sstevel@tonic-gate 				sizeof (aclent_t), cmp2acls);
5510Sstevel@tonic-gate 		if ((err = acl_validate(vsecattr.vsa_aclentp,
5520Sstevel@tonic-gate 				vsecattr.vsa_aclcnt, ACL_CHECK)) != 0) {
5530Sstevel@tonic-gate 			goto alldone;
5540Sstevel@tonic-gate 		}
5550Sstevel@tonic-gate 	}
5560Sstevel@tonic-gate 	if (vsecattr.vsa_dfaclentp) {
5570Sstevel@tonic-gate 		ksort((caddr_t)vsecattr.vsa_dfaclentp, vsecattr.vsa_dfaclcnt,
5580Sstevel@tonic-gate 				sizeof (aclent_t), cmp2acls);
5590Sstevel@tonic-gate 		if ((err = acl_validate(vsecattr.vsa_dfaclentp,
5600Sstevel@tonic-gate 				vsecattr.vsa_dfaclcnt, DEF_ACL_CHECK)) != 0) {
5610Sstevel@tonic-gate 			goto alldone;
5620Sstevel@tonic-gate 		}
5630Sstevel@tonic-gate 	}
5640Sstevel@tonic-gate 
5650Sstevel@tonic-gate 	/* ignore shadow inodes without ACLs */
5660Sstevel@tonic-gate 	if (!vsecattr.vsa_aclentp && !vsecattr.vsa_dfaclentp) {
5670Sstevel@tonic-gate 		err = 0;
5680Sstevel@tonic-gate 		goto alldone;
5690Sstevel@tonic-gate 	}
5700Sstevel@tonic-gate 
5710Sstevel@tonic-gate 	/* Convert from vsecattr struct to ufs_acl_entry struct */
5720Sstevel@tonic-gate 	if ((err = vsecattr2aclentry(&vsecattr, &sp)) != 0) {
5730Sstevel@tonic-gate 		goto alldone;
5740Sstevel@tonic-gate 	}
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate 	/* There aren't filled in by vsecattr2aclentry */
5770Sstevel@tonic-gate 	sp->s_shadow = ip->i_shadow;
5780Sstevel@tonic-gate 	sp->s_dev = ip->i_dev;
5790Sstevel@tonic-gate 	sp->s_use = usecnt;
5800Sstevel@tonic-gate 	sp->s_ref = 1;
5810Sstevel@tonic-gate 	ASSERT(sp->s_ref >= 0 && sp->s_ref <= sp->s_use);
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate 	/* XXX Might make a duplicate */
5840Sstevel@tonic-gate 	si_cache_put(sp);
5850Sstevel@tonic-gate 
5860Sstevel@tonic-gate 	/* Signal anyone waiting on this shadow to be loaded */
5870Sstevel@tonic-gate 	ip->i_ufs_acl = sp;
5880Sstevel@tonic-gate 	err = 0;
5890Sstevel@tonic-gate 	si_cachemiss++;
5900Sstevel@tonic-gate 	if ((acldatalen + fs->fs_bsize) > ufsvfsp->vfs_maxacl)
5910Sstevel@tonic-gate 		ufsvfsp->vfs_maxacl = acldatalen + fs->fs_bsize;
5920Sstevel@tonic-gate alldone:
5930Sstevel@tonic-gate 	/*
5940Sstevel@tonic-gate 	 * Common exit point. Mark shadow inode as ISTALE
5950Sstevel@tonic-gate 	 * if we detect an internal inconsistency, to
5960Sstevel@tonic-gate 	 * prevent stray inodes appearing in the cache.
5970Sstevel@tonic-gate 	 */
5980Sstevel@tonic-gate 	if (err) {
5990Sstevel@tonic-gate 		rw_enter(&sip->i_contents, RW_READER);
6000Sstevel@tonic-gate 		mutex_enter(&sip->i_tlock);
6010Sstevel@tonic-gate 		sip->i_flag |= ISTALE;
6020Sstevel@tonic-gate 		mutex_exit(&sip->i_tlock);
6030Sstevel@tonic-gate 		rw_exit(&sip->i_contents);
6040Sstevel@tonic-gate 	}
6050Sstevel@tonic-gate 	VN_RELE(ITOV(sip));
6060Sstevel@tonic-gate 
6070Sstevel@tonic-gate 	/*
6080Sstevel@tonic-gate 	 * Cleanup of data structures allocated
6090Sstevel@tonic-gate 	 * on the fly.
6100Sstevel@tonic-gate 	 */
6110Sstevel@tonic-gate 	if (acldata)
6120Sstevel@tonic-gate 		kmem_free(acldata, acldatalen);
6130Sstevel@tonic-gate 
6140Sstevel@tonic-gate 	if (vsecattr.vsa_aclentp)
6150Sstevel@tonic-gate 		kmem_free(vsecattr.vsa_aclentp,
6160Sstevel@tonic-gate 			vsecattr.vsa_aclcnt * sizeof (aclent_t));
6170Sstevel@tonic-gate 	if (vsecattr.vsa_dfaclentp)
6180Sstevel@tonic-gate 		kmem_free(vsecattr.vsa_dfaclentp,
6190Sstevel@tonic-gate 			vsecattr.vsa_dfaclcnt * sizeof (aclent_t));
6200Sstevel@tonic-gate 	return (err);
6210Sstevel@tonic-gate }
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate /*
6240Sstevel@tonic-gate  * Check the inode's ACL's to see if this mode of access is
6250Sstevel@tonic-gate  * allowed; return 0 if allowed, EACCES if not.
6260Sstevel@tonic-gate  *
6270Sstevel@tonic-gate  * We follow the procedure defined in Sec. 3.3.5, ACL Access
6280Sstevel@tonic-gate  * Check Algorithm, of the POSIX 1003.6 Draft Standard.
6290Sstevel@tonic-gate  */
6300Sstevel@tonic-gate int
ufs_acl_access(struct inode * ip,int mode,cred_t * cr)6310Sstevel@tonic-gate ufs_acl_access(struct inode *ip, int mode, cred_t *cr)
6320Sstevel@tonic-gate /*
6330Sstevel@tonic-gate  *	ip 	parent inode
6340Sstevel@tonic-gate  *	mode 	mode of access read, write, execute/examine
6350Sstevel@tonic-gate  *	cr	credentials
6360Sstevel@tonic-gate  */
6370Sstevel@tonic-gate {
6380Sstevel@tonic-gate 	ufs_ic_acl_t *acl;
6390Sstevel@tonic-gate 	int ismask, mask = 0;
6400Sstevel@tonic-gate 	int gperm = 0;
6410Sstevel@tonic-gate 	int ngroup = 0;
6420Sstevel@tonic-gate 	si_t	*sp = NULL;
6430Sstevel@tonic-gate 	uid_t uid = crgetuid(cr);
6440Sstevel@tonic-gate 	uid_t owner;
6450Sstevel@tonic-gate 
6460Sstevel@tonic-gate 	ASSERT(ip->i_ufs_acl != NULL);
647*7737SFrank.Batschulat@Sun.COM 	ASSERT(RW_LOCK_HELD(&ip->i_contents));
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate 	sp = ip->i_ufs_acl;
6500Sstevel@tonic-gate 
6510Sstevel@tonic-gate 	ismask = sp->aclass.acl_ismask ?
6520Sstevel@tonic-gate 	    sp->aclass.acl_ismask : NULL;
6530Sstevel@tonic-gate 
6540Sstevel@tonic-gate 	if (ismask)
6550Sstevel@tonic-gate 		mask = sp->aclass.acl_maskbits;
6560Sstevel@tonic-gate 	else
6570Sstevel@tonic-gate 		mask = -1;
6580Sstevel@tonic-gate 
6590Sstevel@tonic-gate 	/*
6600Sstevel@tonic-gate 	 * (1) If user owns the file, obey user mode bits
6610Sstevel@tonic-gate 	 */
6620Sstevel@tonic-gate 	owner = sp->aowner->acl_ic_who;
6630Sstevel@tonic-gate 	if (uid == owner) {
6640Sstevel@tonic-gate 		return (MODE_CHECK(owner, mode, (sp->aowner->acl_ic_perm << 6),
6650Sstevel@tonic-gate 							    cr, ip));
6660Sstevel@tonic-gate 	}
6670Sstevel@tonic-gate 
6680Sstevel@tonic-gate 	/*
6690Sstevel@tonic-gate 	 * (2) Obey any matching ACL_USER entry
6700Sstevel@tonic-gate 	 */
6710Sstevel@tonic-gate 	if (sp->ausers)
6720Sstevel@tonic-gate 		for (acl = sp->ausers; acl != NULL; acl = acl->acl_ic_next) {
6730Sstevel@tonic-gate 			if (acl->acl_ic_who == uid) {
6740Sstevel@tonic-gate 				return (MODE_CHECK(owner, mode,
6750Sstevel@tonic-gate 				    (mask & acl->acl_ic_perm) << 6, cr, ip));
6760Sstevel@tonic-gate 			}
6770Sstevel@tonic-gate 		}
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate 	/*
6800Sstevel@tonic-gate 	 * (3) If user belongs to file's group, obey group mode bits
6810Sstevel@tonic-gate 	 * if no ACL mask is defined; if there is an ACL mask, we look
6820Sstevel@tonic-gate 	 * at both the group mode bits and any ACL_GROUP entries.
6830Sstevel@tonic-gate 	 */
6840Sstevel@tonic-gate 	if (groupmember((uid_t)sp->agroup->acl_ic_who, cr)) {
6850Sstevel@tonic-gate 		ngroup++;
6860Sstevel@tonic-gate 		gperm = (sp->agroup->acl_ic_perm);
6870Sstevel@tonic-gate 		if (!ismask)
6880Sstevel@tonic-gate 			return (MODE_CHECK(owner, mode, (gperm << 6), cr, ip));
6890Sstevel@tonic-gate 	}
6900Sstevel@tonic-gate 
6910Sstevel@tonic-gate 	/*
6920Sstevel@tonic-gate 	 * (4) Accumulate the permissions in matching ACL_GROUP entries
6930Sstevel@tonic-gate 	 */
6940Sstevel@tonic-gate 	if (sp->agroups)
6950Sstevel@tonic-gate 		for (acl = sp->agroups; acl != NULL; acl = acl->acl_ic_next)
6960Sstevel@tonic-gate 		{
6970Sstevel@tonic-gate 			if (groupmember(acl->acl_ic_who, cr)) {
6980Sstevel@tonic-gate 				ngroup++;
6990Sstevel@tonic-gate 				gperm |= acl->acl_ic_perm;
7000Sstevel@tonic-gate 			}
7010Sstevel@tonic-gate 		}
7020Sstevel@tonic-gate 
7030Sstevel@tonic-gate 	if (ngroup != 0)
7040Sstevel@tonic-gate 		return (MODE_CHECK(owner, mode, ((gperm & mask) << 6), cr, ip));
7050Sstevel@tonic-gate 
7060Sstevel@tonic-gate 	/*
7070Sstevel@tonic-gate 	 * (5) Finally, use the "other" mode bits
7080Sstevel@tonic-gate 	 */
7090Sstevel@tonic-gate 	return (MODE_CHECK(owner, mode, sp->aother->acl_ic_perm << 6, cr, ip));
7100Sstevel@tonic-gate }
7110Sstevel@tonic-gate 
7120Sstevel@tonic-gate /*ARGSUSED2*/
7130Sstevel@tonic-gate int
ufs_acl_get(struct inode * ip,vsecattr_t * vsap,int flag,cred_t * cr)7140Sstevel@tonic-gate ufs_acl_get(struct inode *ip, vsecattr_t *vsap, int flag, cred_t *cr)
7150Sstevel@tonic-gate {
7160Sstevel@tonic-gate 	aclent_t	*aclentp;
7170Sstevel@tonic-gate 
7180Sstevel@tonic-gate 	ASSERT(RW_LOCK_HELD(&ip->i_contents));
7190Sstevel@tonic-gate 
7200Sstevel@tonic-gate 	/* XXX Range check, sanity check, shadow check */
7210Sstevel@tonic-gate 	/* If an ACL is present, get the data from the shadow inode info */
7220Sstevel@tonic-gate 	if (ip->i_ufs_acl)
7230Sstevel@tonic-gate 		return (aclentry2vsecattr(ip->i_ufs_acl, vsap));
7240Sstevel@tonic-gate 
7250Sstevel@tonic-gate 	/*
7260Sstevel@tonic-gate 	 * If no ACLs are present, fabricate one from the mode bits.
7270Sstevel@tonic-gate 	 * This code is almost identical to fs_fab_acl(), but we
7280Sstevel@tonic-gate 	 * already have the mode bits handy, so we'll avoid going
7290Sstevel@tonic-gate 	 * through VOP_GETATTR() again.
7300Sstevel@tonic-gate 	 */
7310Sstevel@tonic-gate 
7320Sstevel@tonic-gate 	vsap->vsa_aclcnt    = 0;
7330Sstevel@tonic-gate 	vsap->vsa_aclentp   = NULL;
7340Sstevel@tonic-gate 	vsap->vsa_dfaclcnt  = 0;	/* Default ACLs are not fabricated */
7350Sstevel@tonic-gate 	vsap->vsa_dfaclentp = NULL;
7360Sstevel@tonic-gate 
7370Sstevel@tonic-gate 	if (vsap->vsa_mask & (VSA_ACLCNT | VSA_ACL))
7380Sstevel@tonic-gate 		vsap->vsa_aclcnt    = 4;  /* USER, GROUP, OTHER, and CLASS */
7390Sstevel@tonic-gate 
7400Sstevel@tonic-gate 	if (vsap->vsa_mask & VSA_ACL) {
7410Sstevel@tonic-gate 		vsap->vsa_aclentp = kmem_zalloc(4 * sizeof (aclent_t),
7420Sstevel@tonic-gate 		    KM_SLEEP);
7430Sstevel@tonic-gate 		if (vsap->vsa_aclentp == NULL)
7440Sstevel@tonic-gate 			return (ENOMEM);
7450Sstevel@tonic-gate 		aclentp = vsap->vsa_aclentp;
7460Sstevel@tonic-gate 
7470Sstevel@tonic-gate 		/* Owner */
7480Sstevel@tonic-gate 		aclentp->a_type = USER_OBJ;
7490Sstevel@tonic-gate 		aclentp->a_perm = ((ushort_t)(ip->i_mode & 0700)) >> 6;
7500Sstevel@tonic-gate 		aclentp->a_id = ip->i_uid;	/* Really undefined */
7510Sstevel@tonic-gate 		aclentp++;
7520Sstevel@tonic-gate 
7530Sstevel@tonic-gate 		/* Group */
7540Sstevel@tonic-gate 		aclentp->a_type = GROUP_OBJ;
7550Sstevel@tonic-gate 		aclentp->a_perm = ((ushort_t)(ip->i_mode & 0070)) >> 3;
7560Sstevel@tonic-gate 		aclentp->a_id = ip->i_gid; 	/* Really undefined */
7570Sstevel@tonic-gate 		aclentp++;
7580Sstevel@tonic-gate 
7590Sstevel@tonic-gate 		/* Other */
7600Sstevel@tonic-gate 		aclentp->a_type = OTHER_OBJ;
7610Sstevel@tonic-gate 		aclentp->a_perm = ip->i_mode & 0007;
7620Sstevel@tonic-gate 		aclentp->a_id = 0;		/* Really undefined */
7630Sstevel@tonic-gate 		aclentp++;
7640Sstevel@tonic-gate 
7650Sstevel@tonic-gate 		/* Class */
7660Sstevel@tonic-gate 		aclentp->a_type = CLASS_OBJ;
7670Sstevel@tonic-gate 		aclentp->a_perm = ((ushort_t)(ip->i_mode & 0070)) >> 3;
7680Sstevel@tonic-gate 		aclentp->a_id = 0;		/* Really undefined */
7690Sstevel@tonic-gate 		ksort((caddr_t)vsap->vsa_aclentp, vsap->vsa_aclcnt,
7704662Sfrankho 		    sizeof (aclent_t), cmp2acls);
7710Sstevel@tonic-gate 	}
7720Sstevel@tonic-gate 
7730Sstevel@tonic-gate 	return (0);
7740Sstevel@tonic-gate }
7750Sstevel@tonic-gate 
7760Sstevel@tonic-gate /*ARGSUSED2*/
7770Sstevel@tonic-gate int
ufs_acl_set(struct inode * ip,vsecattr_t * vsap,int flag,cred_t * cr)7780Sstevel@tonic-gate ufs_acl_set(struct inode *ip, vsecattr_t *vsap, int flag, cred_t *cr)
7790Sstevel@tonic-gate {
7800Sstevel@tonic-gate 	si_t	*sp;
7810Sstevel@tonic-gate 	int	err;
7820Sstevel@tonic-gate 
7830Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&ip->i_contents));
7840Sstevel@tonic-gate 
7850Sstevel@tonic-gate 	if (!CHECK_ACL_ALLOWED(ip->i_mode & IFMT))
7860Sstevel@tonic-gate 		return (ENOSYS);
7870Sstevel@tonic-gate 
7880Sstevel@tonic-gate 	/*
7890Sstevel@tonic-gate 	 * only the owner of the file or privileged users can change the ACLs
7900Sstevel@tonic-gate 	 */
7910Sstevel@tonic-gate 	if (secpolicy_vnode_setdac(cr, ip->i_uid) != 0)
7920Sstevel@tonic-gate 		return (EPERM);
7930Sstevel@tonic-gate 
7940Sstevel@tonic-gate 	/* Convert from vsecattr struct to ufs_acl_entry struct */
7950Sstevel@tonic-gate 	if ((err = vsecattr2aclentry(vsap, &sp)) != 0)
7960Sstevel@tonic-gate 		return (err);
7970Sstevel@tonic-gate 	sp->s_dev = ip->i_dev;
7980Sstevel@tonic-gate 
7990Sstevel@tonic-gate 	/*
8000Sstevel@tonic-gate 	 * Make the user & group objs in the acl list follow what's
8010Sstevel@tonic-gate 	 * in the inode.
8020Sstevel@tonic-gate 	 */
8030Sstevel@tonic-gate #ifdef DEBUG
8040Sstevel@tonic-gate 	if (vsap->vsa_mask == VSA_ACL) {
8050Sstevel@tonic-gate 		ASSERT(sp->aowner);
8060Sstevel@tonic-gate 		ASSERT(sp->agroup);
8070Sstevel@tonic-gate 		ASSERT(sp->aother);
8080Sstevel@tonic-gate 	}
8090Sstevel@tonic-gate #endif	/* DEBUG */
8100Sstevel@tonic-gate 
8110Sstevel@tonic-gate 	if (sp->aowner)
8120Sstevel@tonic-gate 		sp->aowner->acl_ic_who = ip->i_uid;
8130Sstevel@tonic-gate 	if (sp->agroup)
8140Sstevel@tonic-gate 		sp->agroup->acl_ic_who = ip->i_gid;
8150Sstevel@tonic-gate 
8160Sstevel@tonic-gate 	/*
8170Sstevel@tonic-gate 	 * Write and cache the new acl list
8180Sstevel@tonic-gate 	 */
8190Sstevel@tonic-gate 	err = ufs_si_store(ip, sp, 1, cr);
8200Sstevel@tonic-gate 
8210Sstevel@tonic-gate 	return (err);
8220Sstevel@tonic-gate }
8230Sstevel@tonic-gate 
8240Sstevel@tonic-gate /*
8250Sstevel@tonic-gate  * XXX Scan sorted array of acl's, checking for:
8260Sstevel@tonic-gate  * 1) Any duplicate/conflicting entries (same type and id)
8270Sstevel@tonic-gate  * 2) More than 1 of USER_OBJ, GROUP_OBJ, OTHER_OBJ, CLASS_OBJ
8280Sstevel@tonic-gate  * 3) More than 1 of DEF_USER_OBJ, DEF_GROUP_OBJ, DEF_OTHER_OBJ, DEF_CLASS_OBJ
8290Sstevel@tonic-gate  *
8300Sstevel@tonic-gate  * Parameters:
8310Sstevel@tonic-gate  * aclentp - ptr to sorted list of acl entries.
8320Sstevel@tonic-gate  * nentries - # acl entries on the list
8330Sstevel@tonic-gate  * flag - Bitmap (ACL_CHECK and/or DEF_ACL_CHECK) indicating whether the
8340Sstevel@tonic-gate  * list contains regular acls, default acls, or both.
8350Sstevel@tonic-gate  *
8360Sstevel@tonic-gate  * Returns:	0 - Success
8370Sstevel@tonic-gate  * EINVAL - Invalid list (dups or multiple entries of type USER_OBJ, etc)
8380Sstevel@tonic-gate  */
8390Sstevel@tonic-gate static int
acl_validate(aclent_t * aclentp,int nentries,int flag)8400Sstevel@tonic-gate acl_validate(aclent_t *aclentp, int nentries, int flag)
8410Sstevel@tonic-gate {
8420Sstevel@tonic-gate 	int	i;
8430Sstevel@tonic-gate 	int	nuser_objs = 0;
8440Sstevel@tonic-gate 	int	ngroup_objs = 0;
8450Sstevel@tonic-gate 	int	nother_objs = 0;
8460Sstevel@tonic-gate 	int	nclass_objs = 0;
8470Sstevel@tonic-gate 	int	ndef_user_objs = 0;
8480Sstevel@tonic-gate 	int	ndef_group_objs = 0;
8490Sstevel@tonic-gate 	int	ndef_other_objs = 0;
8500Sstevel@tonic-gate 	int	ndef_class_objs = 0;
8510Sstevel@tonic-gate 	int	nusers = 0;
8520Sstevel@tonic-gate 	int	ngroups = 0;
8530Sstevel@tonic-gate 	int	ndef_users = 0;
8540Sstevel@tonic-gate 	int	ndef_groups = 0;
8550Sstevel@tonic-gate 	int	numdefs = 0;
8560Sstevel@tonic-gate 
8570Sstevel@tonic-gate 	/* Null list or list of one */
8580Sstevel@tonic-gate 	if (aclentp == NULL)
8590Sstevel@tonic-gate 		return (0);
8600Sstevel@tonic-gate 
8610Sstevel@tonic-gate 	if (nentries <= 0)
8620Sstevel@tonic-gate 		return (EINVAL);
8630Sstevel@tonic-gate 
8640Sstevel@tonic-gate 	for (i = 1; i < nentries; i++) {
8650Sstevel@tonic-gate 		if (((aclentp[i - 1].a_type == aclentp[i].a_type) &&
8660Sstevel@tonic-gate 		    (aclentp[i - 1].a_id   == aclentp[i].a_id)) ||
8670Sstevel@tonic-gate 		    (aclentp[i - 1].a_perm > 07)) {
8680Sstevel@tonic-gate 			return (EINVAL);
8690Sstevel@tonic-gate 		}
8700Sstevel@tonic-gate 	}
8710Sstevel@tonic-gate 
8720Sstevel@tonic-gate 	if (flag == 0 || (flag != ACL_CHECK && flag != DEF_ACL_CHECK))
8730Sstevel@tonic-gate 		return (EINVAL);
8740Sstevel@tonic-gate 
8750Sstevel@tonic-gate 	/* Count types */
8760Sstevel@tonic-gate 	for (i = 0; i < nentries; i++) {
8770Sstevel@tonic-gate 		switch (aclentp[i].a_type) {
8780Sstevel@tonic-gate 		case USER_OBJ:		/* Owner */
8790Sstevel@tonic-gate 			nuser_objs++;
8800Sstevel@tonic-gate 			break;
8810Sstevel@tonic-gate 		case GROUP_OBJ:		/* Group */
8820Sstevel@tonic-gate 			ngroup_objs++;
8830Sstevel@tonic-gate 			break;
8840Sstevel@tonic-gate 		case OTHER_OBJ:		/* Other */
8850Sstevel@tonic-gate 			nother_objs++;
8860Sstevel@tonic-gate 			break;
8870Sstevel@tonic-gate 		case CLASS_OBJ:		/* Mask */
8880Sstevel@tonic-gate 			nclass_objs++;
8890Sstevel@tonic-gate 			break;
8900Sstevel@tonic-gate 		case DEF_USER_OBJ:	/* Default Owner */
8910Sstevel@tonic-gate 			ndef_user_objs++;
8920Sstevel@tonic-gate 			break;
8930Sstevel@tonic-gate 		case DEF_GROUP_OBJ:	/* Default Group */
8940Sstevel@tonic-gate 			ndef_group_objs++;
8950Sstevel@tonic-gate 			break;
8960Sstevel@tonic-gate 		case DEF_OTHER_OBJ:	/* Default Other */
8970Sstevel@tonic-gate 			ndef_other_objs++;
8980Sstevel@tonic-gate 			break;
8990Sstevel@tonic-gate 		case DEF_CLASS_OBJ:	/* Default Mask */
9000Sstevel@tonic-gate 			ndef_class_objs++;
9010Sstevel@tonic-gate 			break;
9020Sstevel@tonic-gate 		case USER:		/* Users */
9030Sstevel@tonic-gate 			nusers++;
9040Sstevel@tonic-gate 			break;
9050Sstevel@tonic-gate 		case GROUP:		/* Groups */
9060Sstevel@tonic-gate 			ngroups++;
9070Sstevel@tonic-gate 			break;
9080Sstevel@tonic-gate 		case DEF_USER:		/* Default Users */
9090Sstevel@tonic-gate 			ndef_users++;
9100Sstevel@tonic-gate 			break;
9110Sstevel@tonic-gate 		case DEF_GROUP:		/* Default Groups */
9120Sstevel@tonic-gate 			ndef_groups++;
9130Sstevel@tonic-gate 			break;
9140Sstevel@tonic-gate 		default:		/* Unknown type */
9150Sstevel@tonic-gate 			return (EINVAL);
9160Sstevel@tonic-gate 		}
9170Sstevel@tonic-gate 	}
9180Sstevel@tonic-gate 
9190Sstevel@tonic-gate 	/*
9200Sstevel@tonic-gate 	 * For normal acl's, we require there be one (and only one)
9210Sstevel@tonic-gate 	 * USER_OBJ, GROUP_OBJ and OTHER_OBJ.  There is either zero
9220Sstevel@tonic-gate 	 * or one CLASS_OBJ.
9230Sstevel@tonic-gate 	 */
9240Sstevel@tonic-gate 	if (flag & ACL_CHECK) {
9250Sstevel@tonic-gate 		if (nuser_objs != 1 || ngroup_objs != 1 ||
9260Sstevel@tonic-gate 		    nother_objs != 1 || nclass_objs > 1) {
9270Sstevel@tonic-gate 			return (EINVAL);
9280Sstevel@tonic-gate 		}
9290Sstevel@tonic-gate 		/*
9300Sstevel@tonic-gate 		 * If there are ANY group acls, there MUST be a
9310Sstevel@tonic-gate 		 * class_obj(mask) acl (1003.6/D12 p. 29 lines 75-80).
9320Sstevel@tonic-gate 		 */
9330Sstevel@tonic-gate 		if (ngroups && !nclass_objs) {
9340Sstevel@tonic-gate 			return (EINVAL);
9350Sstevel@tonic-gate 		}
9360Sstevel@tonic-gate 		if (nuser_objs + ngroup_objs + nother_objs + nclass_objs +
9370Sstevel@tonic-gate 		    ngroups + nusers > MAX_ACL_ENTRIES)
9380Sstevel@tonic-gate 			return (EINVAL);
9390Sstevel@tonic-gate 	}
9400Sstevel@tonic-gate 
9410Sstevel@tonic-gate 	/*
9420Sstevel@tonic-gate 	 * For default acl's, we require that there be either one (and only one)
9430Sstevel@tonic-gate 	 * DEF_USER_OBJ, DEF_GROUP_OBJ and DEF_OTHER_OBJ
9440Sstevel@tonic-gate 	 * or  there be none of them.
9450Sstevel@tonic-gate 	 */
9460Sstevel@tonic-gate 	if (flag & DEF_ACL_CHECK) {
9470Sstevel@tonic-gate 		if (ndef_other_objs > 1 || ndef_user_objs > 1 ||
9480Sstevel@tonic-gate 		    ndef_group_objs > 1 || ndef_class_objs > 1) {
9490Sstevel@tonic-gate 			return (EINVAL);
9500Sstevel@tonic-gate 		}
9510Sstevel@tonic-gate 
9520Sstevel@tonic-gate 		numdefs = ndef_other_objs + ndef_user_objs + ndef_group_objs;
9530Sstevel@tonic-gate 
9540Sstevel@tonic-gate 		if (numdefs != 0 && numdefs != 3) {
9550Sstevel@tonic-gate 			return (EINVAL);
9560Sstevel@tonic-gate 		}
9570Sstevel@tonic-gate 		/*
9580Sstevel@tonic-gate 		 * If there are ANY def_group acls, there MUST be a
9590Sstevel@tonic-gate 		 * def_class_obj(mask) acl (1003.6/D12 P. 29 lines 75-80).
9600Sstevel@tonic-gate 		 * XXX(jimh) This is inferred.
9610Sstevel@tonic-gate 		 */
9620Sstevel@tonic-gate 		if (ndef_groups && !ndef_class_objs) {
9630Sstevel@tonic-gate 			return (EINVAL);
9640Sstevel@tonic-gate 		}
9650Sstevel@tonic-gate 		if ((ndef_users || ndef_groups) &&
9660Sstevel@tonic-gate 		    ((numdefs != 3) && !ndef_class_objs)) {
9670Sstevel@tonic-gate 			return (EINVAL);
9680Sstevel@tonic-gate 		}
9690Sstevel@tonic-gate 		if (ndef_user_objs + ndef_group_objs + ndef_other_objs +
9700Sstevel@tonic-gate 		    ndef_class_objs + ndef_users + ndef_groups >
9710Sstevel@tonic-gate 		    MAX_ACL_ENTRIES)
9720Sstevel@tonic-gate 			return (EINVAL);
9730Sstevel@tonic-gate 	}
9740Sstevel@tonic-gate 	return (0);
9750Sstevel@tonic-gate }
9760Sstevel@tonic-gate 
9770Sstevel@tonic-gate static int
formacl(ufs_ic_acl_t ** aclpp,aclent_t * aclentp)9780Sstevel@tonic-gate formacl(ufs_ic_acl_t **aclpp, aclent_t *aclentp)
9790Sstevel@tonic-gate {
9800Sstevel@tonic-gate 	ufs_ic_acl_t *uaclp;
9810Sstevel@tonic-gate 
9820Sstevel@tonic-gate 	uaclp = kmem_alloc(sizeof (ufs_ic_acl_t), KM_SLEEP);
9830Sstevel@tonic-gate 	uaclp->acl_ic_perm = aclentp->a_perm;
9840Sstevel@tonic-gate 	uaclp->acl_ic_who = aclentp->a_id;
9850Sstevel@tonic-gate 	uaclp->acl_ic_next = *aclpp;
9860Sstevel@tonic-gate 	*aclpp = uaclp;
9870Sstevel@tonic-gate 	return (0);
9880Sstevel@tonic-gate }
9890Sstevel@tonic-gate 
9900Sstevel@tonic-gate /*
9910Sstevel@tonic-gate  * XXX - Make more efficient
9920Sstevel@tonic-gate  * Convert from the vsecattr struct, used by the VOP interface, to
9930Sstevel@tonic-gate  * the ufs_acl_entry struct used for in-core storage of acl's.
9940Sstevel@tonic-gate  *
9950Sstevel@tonic-gate  * Parameters:
9960Sstevel@tonic-gate  * vsap - Ptr to array of security attributes.
9970Sstevel@tonic-gate  * spp - Ptr to ptr to si struct for the results
9980Sstevel@tonic-gate  *
9990Sstevel@tonic-gate  * Returns:	0 - Success
10000Sstevel@tonic-gate  * 		N - From errno.h
10010Sstevel@tonic-gate  */
10020Sstevel@tonic-gate static int
vsecattr2aclentry(vsecattr_t * vsap,si_t ** spp)10030Sstevel@tonic-gate vsecattr2aclentry(vsecattr_t *vsap, si_t **spp)
10040Sstevel@tonic-gate {
10050Sstevel@tonic-gate 	aclent_t	*aclentp, *aclp;
10060Sstevel@tonic-gate 	si_t		*sp;
10070Sstevel@tonic-gate 	int		err;
10080Sstevel@tonic-gate 	int		i;
10090Sstevel@tonic-gate 
10100Sstevel@tonic-gate 	/* Sort & validate the lists on the vsap */
10110Sstevel@tonic-gate 	ksort((caddr_t)vsap->vsa_aclentp, vsap->vsa_aclcnt,
10124662Sfrankho 	    sizeof (aclent_t), cmp2acls);
10130Sstevel@tonic-gate 	ksort((caddr_t)vsap->vsa_dfaclentp, vsap->vsa_dfaclcnt,
10144662Sfrankho 	    sizeof (aclent_t), cmp2acls);
10150Sstevel@tonic-gate 	if ((err = acl_validate(vsap->vsa_aclentp,
10164662Sfrankho 	    vsap->vsa_aclcnt, ACL_CHECK)) != 0)
10170Sstevel@tonic-gate 		return (err);
10180Sstevel@tonic-gate 	if ((err = acl_validate(vsap->vsa_dfaclentp,
10194662Sfrankho 	    vsap->vsa_dfaclcnt, DEF_ACL_CHECK)) != 0)
10200Sstevel@tonic-gate 		return (err);
10210Sstevel@tonic-gate 
10220Sstevel@tonic-gate 	/* Create new si struct and hang acl's off it */
10230Sstevel@tonic-gate 	sp = kmem_zalloc(sizeof (si_t), KM_SLEEP);
10240Sstevel@tonic-gate 	rw_init(&sp->s_lock, NULL, RW_DEFAULT, NULL);
10250Sstevel@tonic-gate 
10260Sstevel@tonic-gate 	/* Process acl list */
10270Sstevel@tonic-gate 	aclp = (aclent_t *)vsap->vsa_aclentp;
10280Sstevel@tonic-gate 	aclentp = aclp + vsap->vsa_aclcnt - 1;
10290Sstevel@tonic-gate 	for (i = 0; i < vsap->vsa_aclcnt; i++) {
10300Sstevel@tonic-gate 		switch (aclentp->a_type) {
10310Sstevel@tonic-gate 		case USER_OBJ:		/* Owner */
10320Sstevel@tonic-gate 			if (err = formacl(&sp->aowner, aclentp))
10330Sstevel@tonic-gate 				goto error;
10340Sstevel@tonic-gate 			break;
10350Sstevel@tonic-gate 		case GROUP_OBJ:		/* Group */
10360Sstevel@tonic-gate 			if (err = formacl(&sp->agroup, aclentp))
10370Sstevel@tonic-gate 				goto error;
10380Sstevel@tonic-gate 			break;
10390Sstevel@tonic-gate 		case OTHER_OBJ:		/* Other */
10400Sstevel@tonic-gate 			if (err = formacl(&sp->aother, aclentp))
10410Sstevel@tonic-gate 				goto error;
10420Sstevel@tonic-gate 			break;
10430Sstevel@tonic-gate 		case USER:
10440Sstevel@tonic-gate 			if (err = formacl(&sp->ausers, aclentp))
10450Sstevel@tonic-gate 				goto error;
10460Sstevel@tonic-gate 			break;
10470Sstevel@tonic-gate 		case CLASS_OBJ:		/* Mask */
10480Sstevel@tonic-gate 			sp->aclass.acl_ismask = 1;
10490Sstevel@tonic-gate 			sp->aclass.acl_maskbits = aclentp->a_perm;
10500Sstevel@tonic-gate 			break;
10510Sstevel@tonic-gate 		case GROUP:
10520Sstevel@tonic-gate 			if (err = formacl(&sp->agroups, aclentp))
10530Sstevel@tonic-gate 				goto error;
10540Sstevel@tonic-gate 			break;
10550Sstevel@tonic-gate 		default:
10560Sstevel@tonic-gate 			break;
10570Sstevel@tonic-gate 		}
10580Sstevel@tonic-gate 		aclentp--;
10590Sstevel@tonic-gate 	}
10600Sstevel@tonic-gate 
10610Sstevel@tonic-gate 	/* Process default acl list */
10620Sstevel@tonic-gate 	aclp = (aclent_t *)vsap->vsa_dfaclentp;
10630Sstevel@tonic-gate 	aclentp = aclp + vsap->vsa_dfaclcnt - 1;
10640Sstevel@tonic-gate 	for (i = 0; i < vsap->vsa_dfaclcnt; i++) {
10650Sstevel@tonic-gate 		switch (aclentp->a_type) {
10660Sstevel@tonic-gate 		case DEF_USER_OBJ:	/* Default Owner */
10670Sstevel@tonic-gate 			if (err = formacl(&sp->downer, aclentp))
10680Sstevel@tonic-gate 				goto error;
10690Sstevel@tonic-gate 			break;
10700Sstevel@tonic-gate 		case DEF_GROUP_OBJ:	/* Default Group */
10710Sstevel@tonic-gate 			if (err = formacl(&sp->dgroup, aclentp))
10720Sstevel@tonic-gate 				goto error;
10730Sstevel@tonic-gate 			break;
10740Sstevel@tonic-gate 		case DEF_OTHER_OBJ:	/* Default Other */
10750Sstevel@tonic-gate 			if (err = formacl(&sp->dother, aclentp))
10760Sstevel@tonic-gate 				goto error;
10770Sstevel@tonic-gate 			break;
10780Sstevel@tonic-gate 		case DEF_USER:
10790Sstevel@tonic-gate 			if (err = formacl(&sp->dusers, aclentp))
10800Sstevel@tonic-gate 				goto error;
10810Sstevel@tonic-gate 			break;
10820Sstevel@tonic-gate 		case DEF_CLASS_OBJ:	/* Default Mask */
10830Sstevel@tonic-gate 			sp->dclass.acl_ismask = 1;
10840Sstevel@tonic-gate 			sp->dclass.acl_maskbits = aclentp->a_perm;
10850Sstevel@tonic-gate 			break;
10860Sstevel@tonic-gate 		case DEF_GROUP:
10870Sstevel@tonic-gate 			if (err = formacl(&sp->dgroups, aclentp))
10880Sstevel@tonic-gate 				goto error;
10890Sstevel@tonic-gate 			break;
10900Sstevel@tonic-gate 		default:
10910Sstevel@tonic-gate 			break;
10920Sstevel@tonic-gate 		}
10930Sstevel@tonic-gate 		aclentp--;
10940Sstevel@tonic-gate 	}
10950Sstevel@tonic-gate 	*spp = sp;
10960Sstevel@tonic-gate 	return (0);
10970Sstevel@tonic-gate 
10980Sstevel@tonic-gate error:
10990Sstevel@tonic-gate 	ufs_si_free_mem(sp);
11000Sstevel@tonic-gate 	return (err);
11010Sstevel@tonic-gate }
11020Sstevel@tonic-gate 
11030Sstevel@tonic-gate void
formvsec(int obj_type,ufs_ic_acl_t * aclp,aclent_t ** aclentpp)11040Sstevel@tonic-gate formvsec(int obj_type, ufs_ic_acl_t *aclp, aclent_t **aclentpp)
11050Sstevel@tonic-gate {
11060Sstevel@tonic-gate 	for (; aclp; aclp = aclp->acl_ic_next) {
11070Sstevel@tonic-gate 		(*aclentpp)->a_type = obj_type;
11080Sstevel@tonic-gate 		(*aclentpp)->a_perm = aclp->acl_ic_perm;
11090Sstevel@tonic-gate 		(*aclentpp)->a_id = aclp->acl_ic_who;
11100Sstevel@tonic-gate 		(*aclentpp)++;
11110Sstevel@tonic-gate 	}
11120Sstevel@tonic-gate }
11130Sstevel@tonic-gate 
11140Sstevel@tonic-gate /*
11150Sstevel@tonic-gate  * XXX - Make more efficient
11160Sstevel@tonic-gate  * Convert from the ufs_acl_entry struct used for in-core storage of acl's
11170Sstevel@tonic-gate  * to the vsecattr struct,  used by the VOP interface.
11180Sstevel@tonic-gate  *
11190Sstevel@tonic-gate  * Parameters:
11200Sstevel@tonic-gate  * sp - Ptr to si struct with the acls
11210Sstevel@tonic-gate  * vsap - Ptr to a vsecattr struct which will take the results.
11220Sstevel@tonic-gate  *
11230Sstevel@tonic-gate  * Returns:	0 - Success
11240Sstevel@tonic-gate  *		N - From errno table
11250Sstevel@tonic-gate  */
11260Sstevel@tonic-gate static int
aclentry2vsecattr(si_t * sp,vsecattr_t * vsap)11270Sstevel@tonic-gate aclentry2vsecattr(si_t *sp, vsecattr_t *vsap)
11280Sstevel@tonic-gate {
11290Sstevel@tonic-gate 	aclent_t	*aclentp;
11300Sstevel@tonic-gate 	int		numacls = 0;
11310Sstevel@tonic-gate 	int		err;
11320Sstevel@tonic-gate 
11330Sstevel@tonic-gate 	vsap->vsa_aclentp = vsap->vsa_dfaclentp = NULL;
11340Sstevel@tonic-gate 
11350Sstevel@tonic-gate 	numacls = acl_count(sp->aowner) +
11360Sstevel@tonic-gate 	    acl_count(sp->agroup) +
11370Sstevel@tonic-gate 	    acl_count(sp->aother) +
11380Sstevel@tonic-gate 	    acl_count(sp->ausers) +
11390Sstevel@tonic-gate 	    acl_count(sp->agroups);
11400Sstevel@tonic-gate 	if (sp->aclass.acl_ismask)
11410Sstevel@tonic-gate 		numacls++;
11420Sstevel@tonic-gate 
11435718Sjr26306 	if (vsap->vsa_mask & (VSA_ACLCNT | VSA_ACL))
11445718Sjr26306 		vsap->vsa_aclcnt = numacls;
11455718Sjr26306 
11460Sstevel@tonic-gate 	if (numacls == 0)
11470Sstevel@tonic-gate 		goto do_defaults;
11480Sstevel@tonic-gate 
11490Sstevel@tonic-gate 	if (vsap->vsa_mask & VSA_ACL) {
11500Sstevel@tonic-gate 		vsap->vsa_aclentp = kmem_zalloc(numacls * sizeof (aclent_t),
11510Sstevel@tonic-gate 		    KM_SLEEP);
11520Sstevel@tonic-gate 		aclentp = vsap->vsa_aclentp;
11530Sstevel@tonic-gate 
11540Sstevel@tonic-gate 		formvsec(USER_OBJ, sp->aowner, &aclentp);
11550Sstevel@tonic-gate 		formvsec(USER, sp->ausers, &aclentp);
11560Sstevel@tonic-gate 		formvsec(GROUP_OBJ, sp->agroup, &aclentp);
11570Sstevel@tonic-gate 		formvsec(GROUP, sp->agroups, &aclentp);
11580Sstevel@tonic-gate 		formvsec(OTHER_OBJ, sp->aother, &aclentp);
11590Sstevel@tonic-gate 
11600Sstevel@tonic-gate 		if (sp->aclass.acl_ismask) {
11610Sstevel@tonic-gate 			aclentp->a_type = CLASS_OBJ;		/* Mask */
11620Sstevel@tonic-gate 			aclentp->a_perm = sp->aclass.acl_maskbits;
11630Sstevel@tonic-gate 			aclentp->a_id = 0;
11640Sstevel@tonic-gate 			aclentp++;
11650Sstevel@tonic-gate 		}
11660Sstevel@tonic-gate 
11670Sstevel@tonic-gate 		/* Sort the acl list */
11680Sstevel@tonic-gate 		ksort((caddr_t)vsap->vsa_aclentp, vsap->vsa_aclcnt,
11694662Sfrankho 		    sizeof (aclent_t), cmp2acls);
11700Sstevel@tonic-gate 		/* Check the acl list */
11710Sstevel@tonic-gate 		if ((err = acl_validate(vsap->vsa_aclentp,
11724662Sfrankho 		    vsap->vsa_aclcnt, ACL_CHECK)) != 0) {
11734662Sfrankho 			kmem_free(vsap->vsa_aclentp,
11744662Sfrankho 			    numacls * sizeof (aclent_t));
11750Sstevel@tonic-gate 			vsap->vsa_aclentp = NULL;
11760Sstevel@tonic-gate 			return (err);
11770Sstevel@tonic-gate 		}
11780Sstevel@tonic-gate 
11790Sstevel@tonic-gate 	}
11800Sstevel@tonic-gate do_defaults:
11810Sstevel@tonic-gate 	/* Process Defaults */
11820Sstevel@tonic-gate 
11830Sstevel@tonic-gate 	numacls = acl_count(sp->downer) +
11840Sstevel@tonic-gate 	    acl_count(sp->dgroup) +
11850Sstevel@tonic-gate 	    acl_count(sp->dother) +
11860Sstevel@tonic-gate 	    acl_count(sp->dusers) +
11870Sstevel@tonic-gate 	    acl_count(sp->dgroups);
11880Sstevel@tonic-gate 	if (sp->dclass.acl_ismask)
11890Sstevel@tonic-gate 		numacls++;
11900Sstevel@tonic-gate 
11915718Sjr26306 	if (vsap->vsa_mask & (VSA_DFACLCNT | VSA_DFACL))
11925718Sjr26306 		vsap->vsa_dfaclcnt = numacls;
11935718Sjr26306 
11940Sstevel@tonic-gate 	if (numacls == 0)
11950Sstevel@tonic-gate 		goto do_others;
11960Sstevel@tonic-gate 
11970Sstevel@tonic-gate 	if (vsap->vsa_mask & VSA_DFACL) {
11984662Sfrankho 		vsap->vsa_dfaclentp =
11994662Sfrankho 		    kmem_zalloc(numacls * sizeof (aclent_t), KM_SLEEP);
12000Sstevel@tonic-gate 		aclentp = vsap->vsa_dfaclentp;
12010Sstevel@tonic-gate 		formvsec(DEF_USER_OBJ, sp->downer, &aclentp);
12020Sstevel@tonic-gate 		formvsec(DEF_USER, sp->dusers, &aclentp);
12030Sstevel@tonic-gate 		formvsec(DEF_GROUP_OBJ, sp->dgroup, &aclentp);
12040Sstevel@tonic-gate 		formvsec(DEF_GROUP, sp->dgroups, &aclentp);
12050Sstevel@tonic-gate 		formvsec(DEF_OTHER_OBJ, sp->dother, &aclentp);
12060Sstevel@tonic-gate 
12070Sstevel@tonic-gate 		if (sp->dclass.acl_ismask) {
12080Sstevel@tonic-gate 			aclentp->a_type = DEF_CLASS_OBJ;	/* Mask */
12090Sstevel@tonic-gate 			aclentp->a_perm = sp->dclass.acl_maskbits;
12100Sstevel@tonic-gate 			aclentp->a_id = 0;
12110Sstevel@tonic-gate 			aclentp++;
12120Sstevel@tonic-gate 		}
12130Sstevel@tonic-gate 
12140Sstevel@tonic-gate 		/* Sort the default acl list */
12150Sstevel@tonic-gate 		ksort((caddr_t)vsap->vsa_dfaclentp, vsap->vsa_dfaclcnt,
12164662Sfrankho 		    sizeof (aclent_t), cmp2acls);
12170Sstevel@tonic-gate 		if ((err = acl_validate(vsap->vsa_dfaclentp,
12180Sstevel@tonic-gate 		    vsap->vsa_dfaclcnt, DEF_ACL_CHECK)) != 0) {
12190Sstevel@tonic-gate 			if (vsap->vsa_aclentp != NULL)
12200Sstevel@tonic-gate 				kmem_free(vsap->vsa_aclentp,
12210Sstevel@tonic-gate 				    vsap->vsa_aclcnt * sizeof (aclent_t));
12220Sstevel@tonic-gate 			kmem_free(vsap->vsa_dfaclentp,
12230Sstevel@tonic-gate 			    vsap->vsa_dfaclcnt * sizeof (aclent_t));
12240Sstevel@tonic-gate 			vsap->vsa_aclentp = vsap->vsa_dfaclentp = NULL;
12250Sstevel@tonic-gate 			return (err);
12260Sstevel@tonic-gate 		}
12270Sstevel@tonic-gate 	}
12280Sstevel@tonic-gate 
12290Sstevel@tonic-gate do_others:
12300Sstevel@tonic-gate 	return (0);
12310Sstevel@tonic-gate }
12320Sstevel@tonic-gate 
12330Sstevel@tonic-gate static void
acl_free(ufs_ic_acl_t * aclp)12340Sstevel@tonic-gate acl_free(ufs_ic_acl_t *aclp)
12350Sstevel@tonic-gate {
12360Sstevel@tonic-gate 	while (aclp != NULL) {
12370Sstevel@tonic-gate 		ufs_ic_acl_t *nextaclp = aclp->acl_ic_next;
12380Sstevel@tonic-gate 		kmem_free(aclp, sizeof (ufs_ic_acl_t));
12390Sstevel@tonic-gate 		aclp = nextaclp;
12400Sstevel@tonic-gate 	}
12410Sstevel@tonic-gate }
12420Sstevel@tonic-gate 
12430Sstevel@tonic-gate /*
12440Sstevel@tonic-gate  * ufs_si_free_mem will discard the sp, and the acl hanging off of the
12450Sstevel@tonic-gate  * sp.  It is required that the sp not be locked, and not be in the
12460Sstevel@tonic-gate  * cache.
12470Sstevel@tonic-gate  *
12480Sstevel@tonic-gate  * input: pointer to sp to discard.
12490Sstevel@tonic-gate  *
12500Sstevel@tonic-gate  * return - nothing.
12510Sstevel@tonic-gate  *
12520Sstevel@tonic-gate  */
12530Sstevel@tonic-gate static void
ufs_si_free_mem(si_t * sp)12540Sstevel@tonic-gate ufs_si_free_mem(si_t *sp)
12550Sstevel@tonic-gate {
12560Sstevel@tonic-gate 	ASSERT(!(sp->s_flags & SI_CACHED));
12570Sstevel@tonic-gate 	ASSERT(!RW_LOCK_HELD(&sp->s_lock));
12580Sstevel@tonic-gate 	/*
12590Sstevel@tonic-gate 	 *	remove from the cache
12600Sstevel@tonic-gate 	 *	free the acl entries
12610Sstevel@tonic-gate 	 */
12620Sstevel@tonic-gate 	acl_free(sp->aowner);
12630Sstevel@tonic-gate 	acl_free(sp->agroup);
12640Sstevel@tonic-gate 	acl_free(sp->aother);
12650Sstevel@tonic-gate 	acl_free(sp->ausers);
12660Sstevel@tonic-gate 	acl_free(sp->agroups);
12670Sstevel@tonic-gate 
12680Sstevel@tonic-gate 	acl_free(sp->downer);
12690Sstevel@tonic-gate 	acl_free(sp->dgroup);
12700Sstevel@tonic-gate 	acl_free(sp->dother);
12710Sstevel@tonic-gate 	acl_free(sp->dusers);
12720Sstevel@tonic-gate 	acl_free(sp->dgroups);
12730Sstevel@tonic-gate 
12740Sstevel@tonic-gate 	rw_destroy(&sp->s_lock);
12750Sstevel@tonic-gate 	kmem_free(sp, sizeof (si_t));
12760Sstevel@tonic-gate }
12770Sstevel@tonic-gate 
12780Sstevel@tonic-gate void
acl_cpy(ufs_ic_acl_t * saclp,ufs_ic_acl_t * daclp)12790Sstevel@tonic-gate acl_cpy(ufs_ic_acl_t *saclp, ufs_ic_acl_t *daclp)
12800Sstevel@tonic-gate {
12810Sstevel@tonic-gate 	ufs_ic_acl_t  *aclp, *prev_aclp = NULL, *aclp1;
12820Sstevel@tonic-gate 
12830Sstevel@tonic-gate 	if (saclp == NULL) {
12840Sstevel@tonic-gate 		daclp = NULL;
12850Sstevel@tonic-gate 		return;
12860Sstevel@tonic-gate 	}
12870Sstevel@tonic-gate 	prev_aclp = daclp;
12880Sstevel@tonic-gate 
12890Sstevel@tonic-gate 	for (aclp = saclp; aclp != NULL; aclp = aclp->acl_ic_next) {
12900Sstevel@tonic-gate 		aclp1 = kmem_alloc(sizeof (ufs_ic_acl_t), KM_SLEEP);
12910Sstevel@tonic-gate 		aclp1->acl_ic_next = NULL;
12920Sstevel@tonic-gate 		aclp1->acl_ic_who = aclp->acl_ic_who;
12930Sstevel@tonic-gate 		aclp1->acl_ic_perm = aclp->acl_ic_perm;
12940Sstevel@tonic-gate 		prev_aclp->acl_ic_next = aclp1;
12950Sstevel@tonic-gate 		prev_aclp = (ufs_ic_acl_t *)&aclp1->acl_ic_next;
12960Sstevel@tonic-gate 	}
12970Sstevel@tonic-gate }
12980Sstevel@tonic-gate 
12990Sstevel@tonic-gate /*
13000Sstevel@tonic-gate  *	ufs_si_inherit takes a parent acl structure (saclp) and the inode
13010Sstevel@tonic-gate  *	of the object that is inheriting an acl and returns the inode
13020Sstevel@tonic-gate  *	with the acl linked to it.  It also writes the acl to disk if
13030Sstevel@tonic-gate  *	it is a unique inode.
13040Sstevel@tonic-gate  *
13050Sstevel@tonic-gate  *	ip - pointer to inode of object inheriting the acl (contents lock)
13060Sstevel@tonic-gate  *	tdp - parent inode (rw_lock and contents lock)
13070Sstevel@tonic-gate  *	mode - creation modes
13080Sstevel@tonic-gate  *	cr - credentials pointer
13090Sstevel@tonic-gate  */
13100Sstevel@tonic-gate int
ufs_si_inherit(struct inode * ip,struct inode * tdp,o_mode_t mode,cred_t * cr)13110Sstevel@tonic-gate ufs_si_inherit(struct inode *ip, struct inode *tdp, o_mode_t mode, cred_t *cr)
13120Sstevel@tonic-gate {
13130Sstevel@tonic-gate 	si_t *tsp, *sp = tdp->i_ufs_acl;
13140Sstevel@tonic-gate 	int error;
13150Sstevel@tonic-gate 	o_mode_t old_modes, old_uid, old_gid;
13160Sstevel@tonic-gate 	int mask;
13170Sstevel@tonic-gate 
13180Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&ip->i_contents));
13190Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&tdp->i_rwlock));
13200Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&tdp->i_contents));
13210Sstevel@tonic-gate 
13220Sstevel@tonic-gate 	/*
13230Sstevel@tonic-gate 	 * if links/symbolic links, or other invalid acl objects are copied
13240Sstevel@tonic-gate 	 * or moved to a directory with a default acl do not allow inheritance
13250Sstevel@tonic-gate 	 * just return.
13260Sstevel@tonic-gate 	 */
13270Sstevel@tonic-gate 	if (!CHECK_ACL_ALLOWED(ip->i_mode & IFMT))
13280Sstevel@tonic-gate 		return (0);
13290Sstevel@tonic-gate 
13300Sstevel@tonic-gate 	/* lock the parent security information */
13310Sstevel@tonic-gate 	rw_enter(&sp->s_lock, RW_READER);
13320Sstevel@tonic-gate 
13330Sstevel@tonic-gate 	ASSERT(((tdp->i_mode & IFMT) == IFDIR) ||
13344662Sfrankho 	    ((tdp->i_mode & IFMT) == IFATTRDIR));
13350Sstevel@tonic-gate 
13360Sstevel@tonic-gate 	mask = ((sp->downer != NULL) ? 1 : 0) |
13370Sstevel@tonic-gate 	    ((sp->dgroup != NULL) ? 2 : 0) |
13380Sstevel@tonic-gate 	    ((sp->dother != NULL) ? 4 : 0);
13390Sstevel@tonic-gate 
13400Sstevel@tonic-gate 	if (mask == 0) {
13410Sstevel@tonic-gate 		rw_exit(&sp->s_lock);
13420Sstevel@tonic-gate 		return (0);
13430Sstevel@tonic-gate 	}
13440Sstevel@tonic-gate 
13450Sstevel@tonic-gate 	if (mask != 7) {
13460Sstevel@tonic-gate 		rw_exit(&sp->s_lock);
13470Sstevel@tonic-gate 		return (EINVAL);
13480Sstevel@tonic-gate 	}
13490Sstevel@tonic-gate 
13500Sstevel@tonic-gate 	tsp = kmem_zalloc(sizeof (si_t), KM_SLEEP);
13510Sstevel@tonic-gate 	rw_init(&tsp->s_lock, NULL, RW_DEFAULT, NULL);
13520Sstevel@tonic-gate 
13530Sstevel@tonic-gate 	/* copy the default acls */
13540Sstevel@tonic-gate 
13550Sstevel@tonic-gate 	ASSERT(RW_READ_HELD(&sp->s_lock));
13560Sstevel@tonic-gate 	acl_cpy(sp->downer, (ufs_ic_acl_t *)&tsp->aowner);
13570Sstevel@tonic-gate 	acl_cpy(sp->dgroup, (ufs_ic_acl_t *)&tsp->agroup);
13580Sstevel@tonic-gate 	acl_cpy(sp->dother, (ufs_ic_acl_t *)&tsp->aother);
13590Sstevel@tonic-gate 	acl_cpy(sp->dusers, (ufs_ic_acl_t *)&tsp->ausers);
13600Sstevel@tonic-gate 	acl_cpy(sp->dgroups, (ufs_ic_acl_t *)&tsp->agroups);
13610Sstevel@tonic-gate 	tsp->aclass.acl_ismask = sp->dclass.acl_ismask;
13620Sstevel@tonic-gate 	tsp->aclass.acl_maskbits = sp->dclass.acl_maskbits;
13630Sstevel@tonic-gate 
13640Sstevel@tonic-gate 	/*
13650Sstevel@tonic-gate 	 * set the owner, group, and other values from the master
13660Sstevel@tonic-gate 	 * inode.
13670Sstevel@tonic-gate 	 */
13680Sstevel@tonic-gate 
13690Sstevel@tonic-gate 	MODE2ACL(tsp->aowner, (mode >> 6), ip->i_uid);
13700Sstevel@tonic-gate 	MODE2ACL(tsp->agroup, (mode >> 3), ip->i_gid);
13710Sstevel@tonic-gate 	MODE2ACL(tsp->aother, (mode), 0);
13720Sstevel@tonic-gate 
13730Sstevel@tonic-gate 	if (tsp->aclass.acl_ismask) {
13740Sstevel@tonic-gate 		tsp->aclass.acl_maskbits &= mode >> 3;
13750Sstevel@tonic-gate 	}
13760Sstevel@tonic-gate 
13770Sstevel@tonic-gate 
13780Sstevel@tonic-gate 	/* copy default acl if necessary */
13790Sstevel@tonic-gate 
13800Sstevel@tonic-gate 	if (((ip->i_mode & IFMT) == IFDIR) ||
13814662Sfrankho 	    ((ip->i_mode & IFMT) == IFATTRDIR)) {
13820Sstevel@tonic-gate 		acl_cpy(sp->downer, (ufs_ic_acl_t *)&tsp->downer);
13830Sstevel@tonic-gate 		acl_cpy(sp->dgroup, (ufs_ic_acl_t *)&tsp->dgroup);
13840Sstevel@tonic-gate 		acl_cpy(sp->dother, (ufs_ic_acl_t *)&tsp->dother);
13850Sstevel@tonic-gate 		acl_cpy(sp->dusers, (ufs_ic_acl_t *)&tsp->dusers);
13860Sstevel@tonic-gate 		acl_cpy(sp->dgroups, (ufs_ic_acl_t *)&tsp->dgroups);
13870Sstevel@tonic-gate 		tsp->dclass.acl_ismask = sp->dclass.acl_ismask;
13880Sstevel@tonic-gate 		tsp->dclass.acl_maskbits = sp->dclass.acl_maskbits;
13890Sstevel@tonic-gate 	}
13900Sstevel@tonic-gate 	/*
13910Sstevel@tonic-gate 	 * save the new 9 mode bits in the inode (ip->ic_smode) for
13920Sstevel@tonic-gate 	 * ufs_getattr.  Be sure the mode can be recovered if the store
13930Sstevel@tonic-gate 	 * fails.
13940Sstevel@tonic-gate 	 */
13950Sstevel@tonic-gate 	old_modes = ip->i_mode;
13960Sstevel@tonic-gate 	old_uid = ip->i_uid;
13970Sstevel@tonic-gate 	old_gid = ip->i_gid;
13980Sstevel@tonic-gate 	/*
13990Sstevel@tonic-gate 	 * store the acl, and get back a new security anchor if
14000Sstevel@tonic-gate 	 * it is a duplicate.
14010Sstevel@tonic-gate 	 */
14020Sstevel@tonic-gate 	rw_exit(&sp->s_lock);
14030Sstevel@tonic-gate 	rw_enter(&ip->i_rwlock, RW_WRITER);
14040Sstevel@tonic-gate 
14050Sstevel@tonic-gate 	/*
14060Sstevel@tonic-gate 	 * Suppress out of inodes messages if instructed in the
14070Sstevel@tonic-gate 	 * tdp inode.
14080Sstevel@tonic-gate 	 */
14090Sstevel@tonic-gate 	ip->i_flag |= tdp->i_flag & IQUIET;
14100Sstevel@tonic-gate 
14110Sstevel@tonic-gate 	if ((error = ufs_si_store(ip, tsp, 0, cr)) != 0) {
14120Sstevel@tonic-gate 		ip->i_mode = old_modes;
14130Sstevel@tonic-gate 		ip->i_uid = old_uid;
14140Sstevel@tonic-gate 		ip->i_gid = old_gid;
14150Sstevel@tonic-gate 	}
14160Sstevel@tonic-gate 	ip->i_flag &= ~IQUIET;
14170Sstevel@tonic-gate 	rw_exit(&ip->i_rwlock);
14180Sstevel@tonic-gate 	return (error);
14190Sstevel@tonic-gate }
14200Sstevel@tonic-gate 
14210Sstevel@tonic-gate si_t *
ufs_acl_cp(si_t * sp)14220Sstevel@tonic-gate ufs_acl_cp(si_t *sp)
14230Sstevel@tonic-gate {
14240Sstevel@tonic-gate 
14250Sstevel@tonic-gate 	si_t *dsp;
14260Sstevel@tonic-gate 
14270Sstevel@tonic-gate 	ASSERT(RW_READ_HELD(&sp->s_lock));
14280Sstevel@tonic-gate 	ASSERT(sp->s_ref && sp->s_use);
14290Sstevel@tonic-gate 
14300Sstevel@tonic-gate 	dsp = kmem_zalloc(sizeof (si_t), KM_SLEEP);
14310Sstevel@tonic-gate 	rw_init(&dsp->s_lock, NULL, RW_DEFAULT, NULL);
14320Sstevel@tonic-gate 
14330Sstevel@tonic-gate 	acl_cpy(sp->aowner, (ufs_ic_acl_t *)&dsp->aowner);
14340Sstevel@tonic-gate 	acl_cpy(sp->agroup, (ufs_ic_acl_t *)&dsp->agroup);
14350Sstevel@tonic-gate 	acl_cpy(sp->aother, (ufs_ic_acl_t *)&dsp->aother);
14360Sstevel@tonic-gate 	acl_cpy(sp->ausers, (ufs_ic_acl_t *)&dsp->ausers);
14370Sstevel@tonic-gate 	acl_cpy(sp->agroups, (ufs_ic_acl_t *)&dsp->agroups);
14380Sstevel@tonic-gate 
14390Sstevel@tonic-gate 	dsp->aclass.acl_ismask = sp->aclass.acl_ismask;
14400Sstevel@tonic-gate 	dsp->aclass.acl_maskbits = sp->aclass.acl_maskbits;
14410Sstevel@tonic-gate 
14420Sstevel@tonic-gate 	acl_cpy(sp->downer, (ufs_ic_acl_t *)&dsp->downer);
14430Sstevel@tonic-gate 	acl_cpy(sp->dgroup, (ufs_ic_acl_t *)&dsp->dgroup);
14440Sstevel@tonic-gate 	acl_cpy(sp->dother, (ufs_ic_acl_t *)&dsp->dother);
14450Sstevel@tonic-gate 	acl_cpy(sp->dusers, (ufs_ic_acl_t *)&dsp->dusers);
14460Sstevel@tonic-gate 	acl_cpy(sp->dgroups, (ufs_ic_acl_t *)&dsp->dgroups);
14470Sstevel@tonic-gate 
14480Sstevel@tonic-gate 	dsp->dclass.acl_ismask = sp->dclass.acl_ismask;
14490Sstevel@tonic-gate 	dsp->dclass.acl_maskbits = sp->dclass.acl_maskbits;
14500Sstevel@tonic-gate 
14510Sstevel@tonic-gate 	return (dsp);
14520Sstevel@tonic-gate 
14530Sstevel@tonic-gate }
14540Sstevel@tonic-gate 
14550Sstevel@tonic-gate int
ufs_acl_setattr(struct inode * ip,struct vattr * vap,cred_t * cr)14560Sstevel@tonic-gate ufs_acl_setattr(struct inode *ip, struct vattr *vap, cred_t *cr)
14570Sstevel@tonic-gate {
14580Sstevel@tonic-gate 
14590Sstevel@tonic-gate 	si_t *sp;
14600Sstevel@tonic-gate 	int mask = vap->va_mask;
14610Sstevel@tonic-gate 	int error = 0;
14620Sstevel@tonic-gate 
14630Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&ip->i_contents));
14640Sstevel@tonic-gate 
14650Sstevel@tonic-gate 	if (!(mask & (AT_MODE|AT_UID|AT_GID)))
14660Sstevel@tonic-gate 		return (0);
14670Sstevel@tonic-gate 
14680Sstevel@tonic-gate 	/*
14690Sstevel@tonic-gate 	 * if no regular acl's, nothing to do, so let's get out
14700Sstevel@tonic-gate 	 */
14710Sstevel@tonic-gate 	if (!(ip->i_ufs_acl) || !(ip->i_ufs_acl->aowner))
14720Sstevel@tonic-gate 		return (0);
14730Sstevel@tonic-gate 
14740Sstevel@tonic-gate 	rw_enter(&ip->i_ufs_acl->s_lock, RW_READER);
14750Sstevel@tonic-gate 	sp = ufs_acl_cp(ip->i_ufs_acl);
14760Sstevel@tonic-gate 	ASSERT(sp != ip->i_ufs_acl);
14770Sstevel@tonic-gate 
14780Sstevel@tonic-gate 	/*
14790Sstevel@tonic-gate 	 * set the mask to the group permissions if a mask entry
14800Sstevel@tonic-gate 	 * exists.  Otherwise, set the group obj bits to the group
14810Sstevel@tonic-gate 	 * permissions.  Since non-trivial ACLs always have a mask,
14820Sstevel@tonic-gate 	 * and the mask is the final arbiter of group permissions,
14830Sstevel@tonic-gate 	 * setting the mask has the effect of changing the effective
14840Sstevel@tonic-gate 	 * group permissions, even if the group_obj permissions in
14850Sstevel@tonic-gate 	 * the ACL aren't changed.  Posix P1003.1e states that when
14860Sstevel@tonic-gate 	 * an ACL mask exists, chmod(2) must set the acl mask (NOT the
14870Sstevel@tonic-gate 	 * group_obj permissions) to the requested group permissions.
14880Sstevel@tonic-gate 	 */
14890Sstevel@tonic-gate 	if (mask & AT_MODE) {
14900Sstevel@tonic-gate 		sp->aowner->acl_ic_perm = (o_mode_t)(ip->i_mode & 0700) >> 6;
14910Sstevel@tonic-gate 		if (sp->aclass.acl_ismask)
14920Sstevel@tonic-gate 			sp->aclass.acl_maskbits =
14930Sstevel@tonic-gate 			    (o_mode_t)(ip->i_mode & 070) >> 3;
14940Sstevel@tonic-gate 		else
14950Sstevel@tonic-gate 			sp->agroup->acl_ic_perm =
14960Sstevel@tonic-gate 			    (o_mode_t)(ip->i_mode & 070) >> 3;
14970Sstevel@tonic-gate 		sp->aother->acl_ic_perm = (o_mode_t)(ip->i_mode & 07);
14980Sstevel@tonic-gate 	}
14990Sstevel@tonic-gate 
15000Sstevel@tonic-gate 	if (mask & AT_UID) {
15010Sstevel@tonic-gate 		/* Caller has verified our privileges */
15020Sstevel@tonic-gate 		sp->aowner->acl_ic_who = ip->i_uid;
15030Sstevel@tonic-gate 	}
15040Sstevel@tonic-gate 
15050Sstevel@tonic-gate 	if (mask & AT_GID) {
15060Sstevel@tonic-gate 		sp->agroup->acl_ic_who = ip->i_gid;
15070Sstevel@tonic-gate 	}
15080Sstevel@tonic-gate 
15090Sstevel@tonic-gate 	rw_exit(&ip->i_ufs_acl->s_lock);
15100Sstevel@tonic-gate 	error = ufs_si_store(ip, sp, 0, cr);
15110Sstevel@tonic-gate 	return (error);
15120Sstevel@tonic-gate }
15130Sstevel@tonic-gate 
15140Sstevel@tonic-gate static int
acl_count(ufs_ic_acl_t * p)15150Sstevel@tonic-gate acl_count(ufs_ic_acl_t *p)
15160Sstevel@tonic-gate {
15170Sstevel@tonic-gate 	ufs_ic_acl_t	*acl;
15180Sstevel@tonic-gate 	int		count;
15190Sstevel@tonic-gate 
15200Sstevel@tonic-gate 	for (count = 0, acl = p; acl; acl = acl->acl_ic_next, count++)
15210Sstevel@tonic-gate 		;
15220Sstevel@tonic-gate 	return (count);
15230Sstevel@tonic-gate }
15240Sstevel@tonic-gate 
15250Sstevel@tonic-gate /*
15260Sstevel@tonic-gate  *	Takes as input a security structure and generates a buffer
15270Sstevel@tonic-gate  *	with fsd's in a form which be written to the shadow inode.
15280Sstevel@tonic-gate  */
15290Sstevel@tonic-gate static int
ufs_sectobuf(si_t * sp,caddr_t * buf,size_t * len)15300Sstevel@tonic-gate ufs_sectobuf(si_t *sp, caddr_t *buf, size_t *len)
15310Sstevel@tonic-gate {
15320Sstevel@tonic-gate 	size_t		acl_size;
15330Sstevel@tonic-gate 	size_t		def_acl_size;
15340Sstevel@tonic-gate 	caddr_t		buffer;
15350Sstevel@tonic-gate 	struct ufs_fsd	*fsdp;
15360Sstevel@tonic-gate 	ufs_acl_t	*bufaclp;
15370Sstevel@tonic-gate 
15380Sstevel@tonic-gate 	/*
15390Sstevel@tonic-gate 	 * Calc size of buffer to hold all the acls
15400Sstevel@tonic-gate 	 */
15410Sstevel@tonic-gate 	acl_size = acl_count(sp->aowner) +		/* owner */
15420Sstevel@tonic-gate 	    acl_count(sp->agroup) +			/* owner group */
15430Sstevel@tonic-gate 	    acl_count(sp->aother) +			/* owner other */
15440Sstevel@tonic-gate 	    acl_count(sp->ausers) +			/* acl list */
15450Sstevel@tonic-gate 	    acl_count(sp->agroups);			/* group alcs */
15460Sstevel@tonic-gate 	if (sp->aclass.acl_ismask)
15470Sstevel@tonic-gate 		acl_size++;
15480Sstevel@tonic-gate 
15490Sstevel@tonic-gate 	/* Convert to bytes */
15500Sstevel@tonic-gate 	acl_size *= sizeof (ufs_acl_t);
15510Sstevel@tonic-gate 
15520Sstevel@tonic-gate 	/* Add fsd header */
15530Sstevel@tonic-gate 	if (acl_size)
15540Sstevel@tonic-gate 		acl_size += 2 * sizeof (int);
15550Sstevel@tonic-gate 
15560Sstevel@tonic-gate 	/*
15570Sstevel@tonic-gate 	 * Calc size of buffer to hold all the default acls
15580Sstevel@tonic-gate 	 */
15590Sstevel@tonic-gate 	def_acl_size =
15600Sstevel@tonic-gate 	    acl_count(sp->downer) +	/* def owner */
15610Sstevel@tonic-gate 	    acl_count(sp->dgroup) +	/* def owner group */
15620Sstevel@tonic-gate 	    acl_count(sp->dother) +	/* def owner other */
15630Sstevel@tonic-gate 	    acl_count(sp->dusers) +	/* def users  */
15640Sstevel@tonic-gate 	    acl_count(sp->dgroups);	/* def group acls */
15650Sstevel@tonic-gate 	if (sp->dclass.acl_ismask)
15660Sstevel@tonic-gate 		def_acl_size++;
15670Sstevel@tonic-gate 
15680Sstevel@tonic-gate 	/*
15690Sstevel@tonic-gate 	 * Convert to bytes
15700Sstevel@tonic-gate 	 */
15710Sstevel@tonic-gate 	def_acl_size *= sizeof (ufs_acl_t);
15720Sstevel@tonic-gate 
15730Sstevel@tonic-gate 	/*
15740Sstevel@tonic-gate 	 * Add fsd header
15750Sstevel@tonic-gate 	 */
15760Sstevel@tonic-gate 	if (def_acl_size)
15770Sstevel@tonic-gate 		def_acl_size += 2 * sizeof (int);
15780Sstevel@tonic-gate 
15790Sstevel@tonic-gate 	if (acl_size + def_acl_size == 0)
15800Sstevel@tonic-gate 		return (0);
15810Sstevel@tonic-gate 
15820Sstevel@tonic-gate 	buffer = kmem_zalloc((acl_size + def_acl_size), KM_SLEEP);
15830Sstevel@tonic-gate 	bufaclp = (ufs_acl_t *)buffer;
15840Sstevel@tonic-gate 
15850Sstevel@tonic-gate 	if (acl_size == 0)
15860Sstevel@tonic-gate 		goto wrtdefs;
15870Sstevel@tonic-gate 
15880Sstevel@tonic-gate 	/* create fsd and copy acls */
15890Sstevel@tonic-gate 	fsdp = (struct ufs_fsd *)bufaclp;
15900Sstevel@tonic-gate 	fsdp->fsd_type = FSD_ACL;
15910Sstevel@tonic-gate 	bufaclp = (ufs_acl_t *)&fsdp->fsd_data[0];
15920Sstevel@tonic-gate 
15930Sstevel@tonic-gate 	ACL_MOVE(sp->aowner, USER_OBJ, bufaclp);
15940Sstevel@tonic-gate 	ACL_MOVE(sp->agroup, GROUP_OBJ, bufaclp);
15950Sstevel@tonic-gate 	ACL_MOVE(sp->aother, OTHER_OBJ, bufaclp);
15960Sstevel@tonic-gate 	ACL_MOVE(sp->ausers, USER, bufaclp);
15970Sstevel@tonic-gate 	ACL_MOVE(sp->agroups, GROUP, bufaclp);
15980Sstevel@tonic-gate 
15990Sstevel@tonic-gate 	if (sp->aclass.acl_ismask) {
16000Sstevel@tonic-gate 		bufaclp->acl_tag = CLASS_OBJ;
16010Sstevel@tonic-gate 		bufaclp->acl_who = (uid_t)sp->aclass.acl_ismask;
16020Sstevel@tonic-gate 		bufaclp->acl_perm = (o_mode_t)sp->aclass.acl_maskbits;
16030Sstevel@tonic-gate 		bufaclp++;
16040Sstevel@tonic-gate 	}
16050Sstevel@tonic-gate 	ASSERT(acl_size <= INT_MAX);
16060Sstevel@tonic-gate 	fsdp->fsd_size = (int)acl_size;
16070Sstevel@tonic-gate 
16080Sstevel@tonic-gate wrtdefs:
16090Sstevel@tonic-gate 	if (def_acl_size == 0)
16100Sstevel@tonic-gate 		goto alldone;
16110Sstevel@tonic-gate 
16120Sstevel@tonic-gate 	/* if defaults exist then create fsd and copy default acls */
16130Sstevel@tonic-gate 	fsdp = (struct ufs_fsd *)bufaclp;
16140Sstevel@tonic-gate 	fsdp->fsd_type = FSD_DFACL;
16150Sstevel@tonic-gate 	bufaclp = (ufs_acl_t *)&fsdp->fsd_data[0];
16160Sstevel@tonic-gate 
16170Sstevel@tonic-gate 	ACL_MOVE(sp->downer, DEF_USER_OBJ, bufaclp);
16180Sstevel@tonic-gate 	ACL_MOVE(sp->dgroup, DEF_GROUP_OBJ, bufaclp);
16190Sstevel@tonic-gate 	ACL_MOVE(sp->dother, DEF_OTHER_OBJ, bufaclp);
16200Sstevel@tonic-gate 	ACL_MOVE(sp->dusers, DEF_USER, bufaclp);
16210Sstevel@tonic-gate 	ACL_MOVE(sp->dgroups, DEF_GROUP, bufaclp);
16220Sstevel@tonic-gate 	if (sp->dclass.acl_ismask) {
16230Sstevel@tonic-gate 		bufaclp->acl_tag = DEF_CLASS_OBJ;
16240Sstevel@tonic-gate 		bufaclp->acl_who = (uid_t)sp->dclass.acl_ismask;
16250Sstevel@tonic-gate 		bufaclp->acl_perm = (o_mode_t)sp->dclass.acl_maskbits;
16260Sstevel@tonic-gate 		bufaclp++;
16270Sstevel@tonic-gate 	}
16280Sstevel@tonic-gate 	ASSERT(def_acl_size <= INT_MAX);
16290Sstevel@tonic-gate 	fsdp->fsd_size = (int)def_acl_size;
16300Sstevel@tonic-gate 
16310Sstevel@tonic-gate alldone:
16320Sstevel@tonic-gate 	*buf = buffer;
16330Sstevel@tonic-gate 	*len = acl_size + def_acl_size;
16340Sstevel@tonic-gate 
16350Sstevel@tonic-gate 	return (0);
16360Sstevel@tonic-gate }
16370Sstevel@tonic-gate 
16380Sstevel@tonic-gate /*
16390Sstevel@tonic-gate  *  free a shadow inode  on disk and in memory
16400Sstevel@tonic-gate  */
16410Sstevel@tonic-gate int
ufs_si_free(si_t * sp,struct vfs * vfsp,cred_t * cr)16420Sstevel@tonic-gate ufs_si_free(si_t *sp, struct vfs *vfsp, cred_t *cr)
16430Sstevel@tonic-gate {
16440Sstevel@tonic-gate 	struct inode 	*sip;
16450Sstevel@tonic-gate 	int 		shadow;
16460Sstevel@tonic-gate 	int 		err = 0;
16470Sstevel@tonic-gate 	int		refcnt;
16480Sstevel@tonic-gate 	int		signature;
16490Sstevel@tonic-gate 
16500Sstevel@tonic-gate 	ASSERT(vfsp);
16510Sstevel@tonic-gate 	ASSERT(sp);
16520Sstevel@tonic-gate 
16530Sstevel@tonic-gate 	rw_enter(&sp->s_lock, RW_READER);
16540Sstevel@tonic-gate 	ASSERT(sp->s_shadow <= INT_MAX);
16550Sstevel@tonic-gate 	shadow = (int)sp->s_shadow;
16560Sstevel@tonic-gate 	ASSERT(sp->s_ref);
16570Sstevel@tonic-gate 	rw_exit(&sp->s_lock);
16580Sstevel@tonic-gate 
16590Sstevel@tonic-gate 	/*
16600Sstevel@tonic-gate 	 * Decrement link count on the shadow inode,
16610Sstevel@tonic-gate 	 * and decrement reference count on the sip.
16620Sstevel@tonic-gate 	 */
16630Sstevel@tonic-gate 	if ((err = ufs_iget_alloced(vfsp, shadow, &sip, cr)) == 0) {
16640Sstevel@tonic-gate 		rw_enter(&sip->i_contents, RW_WRITER);
16650Sstevel@tonic-gate 		rw_enter(&sp->s_lock, RW_WRITER);
16660Sstevel@tonic-gate 		ASSERT(sp->s_shadow == shadow);
16670Sstevel@tonic-gate 		ASSERT(sip->i_dquot == 0);
16680Sstevel@tonic-gate 		/* Decrement link count */
16690Sstevel@tonic-gate 		ASSERT(sip->i_nlink > 0);
16700Sstevel@tonic-gate 		/*
16710Sstevel@tonic-gate 		 * bug #1264710 assertion failure below
16720Sstevel@tonic-gate 		 */
16730Sstevel@tonic-gate 		sp->s_use = --sip->i_nlink;
16740Sstevel@tonic-gate 		ufs_setreclaim(sip);
16750Sstevel@tonic-gate 		TRANS_INODE(sip->i_ufsvfs, sip);
16760Sstevel@tonic-gate 		sip->i_flag |= ICHG | IMOD;
16770Sstevel@tonic-gate 		sip->i_seq++;
16780Sstevel@tonic-gate 		ITIMES_NOLOCK(sip);
16790Sstevel@tonic-gate 		/* Dec ref counts on si referenced by this ip */
16800Sstevel@tonic-gate 		refcnt = --sp->s_ref;
16810Sstevel@tonic-gate 		signature = sp->s_signature;
16820Sstevel@tonic-gate 		ASSERT(sp->s_ref >= 0 && sp->s_ref <= sp->s_use);
16830Sstevel@tonic-gate 		/*
16840Sstevel@tonic-gate 		 * Release s_lock before calling VN_RELE
16850Sstevel@tonic-gate 		 * (which may want to acquire i_contents).
16860Sstevel@tonic-gate 		 */
16870Sstevel@tonic-gate 		rw_exit(&sp->s_lock);
16880Sstevel@tonic-gate 		rw_exit(&sip->i_contents);
16890Sstevel@tonic-gate 		VN_RELE(ITOV(sip));
16900Sstevel@tonic-gate 	} else {
16910Sstevel@tonic-gate 		rw_enter(&sp->s_lock, RW_WRITER);
16920Sstevel@tonic-gate 		/* Dec ref counts on si referenced by this ip */
16930Sstevel@tonic-gate 		refcnt = --sp->s_ref;
16940Sstevel@tonic-gate 		signature = sp->s_signature;
16950Sstevel@tonic-gate 		ASSERT(sp->s_ref >= 0 && sp->s_ref <= sp->s_use);
16960Sstevel@tonic-gate 		rw_exit(&sp->s_lock);
16970Sstevel@tonic-gate 	}
16980Sstevel@tonic-gate 
16990Sstevel@tonic-gate 	if (refcnt == 0)
17000Sstevel@tonic-gate 		si_cache_del(sp, signature);
17010Sstevel@tonic-gate 	return (err);
17020Sstevel@tonic-gate }
17030Sstevel@tonic-gate 
17040Sstevel@tonic-gate /*
17050Sstevel@tonic-gate  * Seach the si cache for an si structure by inode #.
17060Sstevel@tonic-gate  * Returns a locked si structure.
17070Sstevel@tonic-gate  *
17080Sstevel@tonic-gate  * Parameters:
17090Sstevel@tonic-gate  * ip - Ptr to an inode on this fs
17100Sstevel@tonic-gate  * spp - Ptr to ptr to si struct for the results, if found.
17110Sstevel@tonic-gate  *
17120Sstevel@tonic-gate  * Returns:	0 - Success (results in spp)
17130Sstevel@tonic-gate  *		1 - Failure (spp undefined)
17140Sstevel@tonic-gate  */
17150Sstevel@tonic-gate static int
si_cachei_get(struct inode * ip,si_t ** spp)17160Sstevel@tonic-gate si_cachei_get(struct inode *ip, si_t **spp)
17170Sstevel@tonic-gate {
17180Sstevel@tonic-gate 	si_t	*sp;
17190Sstevel@tonic-gate 
17200Sstevel@tonic-gate 	rw_enter(&si_cache_lock, RW_READER);
17210Sstevel@tonic-gate loop:
17220Sstevel@tonic-gate 	for (sp = si_cachei[SI_HASH(ip->i_shadow)]; sp; sp = sp->s_forw)
17230Sstevel@tonic-gate 		if (sp->s_shadow == ip->i_shadow && sp->s_dev == ip->i_dev)
17240Sstevel@tonic-gate 			break;
17250Sstevel@tonic-gate 
17260Sstevel@tonic-gate 	if (sp == NULL) {
17270Sstevel@tonic-gate 		/* Not in cache */
17280Sstevel@tonic-gate 		rw_exit(&si_cache_lock);
17290Sstevel@tonic-gate 		return (1);
17300Sstevel@tonic-gate 	}
17310Sstevel@tonic-gate 	/* Found it */
17320Sstevel@tonic-gate 	rw_enter(&sp->s_lock, RW_WRITER);
17330Sstevel@tonic-gate alldone:
17340Sstevel@tonic-gate 	rw_exit(&si_cache_lock);
17350Sstevel@tonic-gate 	*spp = sp;
17360Sstevel@tonic-gate 	return (0);
17370Sstevel@tonic-gate }
17380Sstevel@tonic-gate 
17390Sstevel@tonic-gate /*
17400Sstevel@tonic-gate  * Seach the si cache by si structure (ie duplicate of the one passed in).
17410Sstevel@tonic-gate  * In order for a match the signatures must be the same and
17420Sstevel@tonic-gate  * the devices must be the same, the acls must match and
17430Sstevel@tonic-gate  * link count of the cached shadow must be less than the
17440Sstevel@tonic-gate  * size of ic_nlink - 1.  MAXLINK - 1 is used to allow the count
17450Sstevel@tonic-gate  * to be incremented one more time by the caller.
17460Sstevel@tonic-gate  * Returns a locked si structure.
17470Sstevel@tonic-gate  *
17480Sstevel@tonic-gate  * Parameters:
17490Sstevel@tonic-gate  * ip - Ptr to an inode on this fs
17500Sstevel@tonic-gate  * spi - Ptr to si the struct we're searching the cache for.
17510Sstevel@tonic-gate  * spp - Ptr to ptr to si struct for the results, if found.
17520Sstevel@tonic-gate  *
17530Sstevel@tonic-gate  * Returns:	0 - Success (results in spp)
17540Sstevel@tonic-gate  *		1 - Failure (spp undefined)
17550Sstevel@tonic-gate  */
17560Sstevel@tonic-gate static int
si_cachea_get(struct inode * ip,si_t * spi,si_t ** spp)17570Sstevel@tonic-gate si_cachea_get(struct inode *ip, si_t *spi, si_t **spp)
17580Sstevel@tonic-gate {
17590Sstevel@tonic-gate 	si_t	*sp;
17600Sstevel@tonic-gate 
17610Sstevel@tonic-gate 	spi->s_dev = ip->i_dev;
17620Sstevel@tonic-gate 	spi->s_signature = si_signature(spi);
17630Sstevel@tonic-gate 	rw_enter(&si_cache_lock, RW_READER);
17640Sstevel@tonic-gate loop:
17650Sstevel@tonic-gate 	for (sp = si_cachea[SI_HASH(spi->s_signature)]; sp; sp = sp->s_next) {
17660Sstevel@tonic-gate 		if (sp->s_signature == spi->s_signature &&
17670Sstevel@tonic-gate 		    sp->s_dev == spi->s_dev &&
17680Sstevel@tonic-gate 		    sp->s_use > 0 &&			/* deleting */
17690Sstevel@tonic-gate 		    sp->s_use <= (MAXLINK - 1) &&	/* Too many links */
17700Sstevel@tonic-gate 		    !si_cmp(sp, spi))
17710Sstevel@tonic-gate 			break;
17720Sstevel@tonic-gate 	}
17730Sstevel@tonic-gate 
17740Sstevel@tonic-gate 	if (sp == NULL) {
17750Sstevel@tonic-gate 		/* Cache miss */
17760Sstevel@tonic-gate 		rw_exit(&si_cache_lock);
17770Sstevel@tonic-gate 		return (1);
17780Sstevel@tonic-gate 	}
17790Sstevel@tonic-gate 	/* Found it */
17800Sstevel@tonic-gate 	rw_enter(&sp->s_lock, RW_WRITER);
17810Sstevel@tonic-gate alldone:
17820Sstevel@tonic-gate 	spi->s_shadow = sp->s_shadow; /* XXX For debugging */
17830Sstevel@tonic-gate 	rw_exit(&si_cache_lock);
17840Sstevel@tonic-gate 	*spp = sp;
17850Sstevel@tonic-gate 	return (0);
17860Sstevel@tonic-gate }
17870Sstevel@tonic-gate 
17880Sstevel@tonic-gate /*
17890Sstevel@tonic-gate  * Place an si structure in the si cache.  May cause duplicates.
17900Sstevel@tonic-gate  *
17910Sstevel@tonic-gate  * Parameters:
17920Sstevel@tonic-gate  * sp - Ptr to the si struct to add to the cache.
17930Sstevel@tonic-gate  *
17940Sstevel@tonic-gate  * Returns: Nothing (void)
17950Sstevel@tonic-gate  */
17960Sstevel@tonic-gate static void
si_cache_put(si_t * sp)17970Sstevel@tonic-gate si_cache_put(si_t *sp)
17980Sstevel@tonic-gate {
17990Sstevel@tonic-gate 	si_t	**tspp;
18000Sstevel@tonic-gate 
18010Sstevel@tonic-gate 	ASSERT(sp->s_fore == NULL);
18020Sstevel@tonic-gate 	rw_enter(&si_cache_lock, RW_WRITER);
18030Sstevel@tonic-gate 	if (!sp->s_signature)
18040Sstevel@tonic-gate 		sp->s_signature = si_signature(sp);
18050Sstevel@tonic-gate 	sp->s_flags |= SI_CACHED;
18060Sstevel@tonic-gate 	sp->s_fore = NULL;
18070Sstevel@tonic-gate 
18080Sstevel@tonic-gate 	/* The 'by acl' chains */
18090Sstevel@tonic-gate 	tspp = &si_cachea[SI_HASH(sp->s_signature)];
18100Sstevel@tonic-gate 	sp->s_next = *tspp;
18110Sstevel@tonic-gate 	*tspp = sp;
18120Sstevel@tonic-gate 
18130Sstevel@tonic-gate 	/* The 'by inode' chains */
18140Sstevel@tonic-gate 	tspp = &si_cachei[SI_HASH(sp->s_shadow)];
18150Sstevel@tonic-gate 	sp->s_forw = *tspp;
18160Sstevel@tonic-gate 	*tspp = sp;
18170Sstevel@tonic-gate 
18180Sstevel@tonic-gate 	rw_exit(&si_cache_lock);
18190Sstevel@tonic-gate }
18200Sstevel@tonic-gate 
18210Sstevel@tonic-gate /*
18220Sstevel@tonic-gate  * The sp passed in is a candidate for deletion from the cache.  We acquire
18230Sstevel@tonic-gate  * the cache lock first, so no cache searches can be done.  Then we search
18240Sstevel@tonic-gate  * for the acl in the cache, and if we find it we can lock it and check that
18250Sstevel@tonic-gate  * nobody else attached to it while we were acquiring the locks.  If the acl
18260Sstevel@tonic-gate  * is in the cache and still has a zero reference count, then we remove it
18270Sstevel@tonic-gate  * from the cache and deallocate it.  If the reference count is non-zero or
18280Sstevel@tonic-gate  * it is not found in the cache, then someone else attached to it or has
18290Sstevel@tonic-gate  * already freed it, so we just return.
18300Sstevel@tonic-gate  *
18310Sstevel@tonic-gate  * Parameters:
18320Sstevel@tonic-gate  * sp - Ptr to the sp struct which is the candicate for deletion.
18330Sstevel@tonic-gate  * signature - the signature for the acl for lookup in the hash table
18340Sstevel@tonic-gate  *
18350Sstevel@tonic-gate  * Returns: Nothing (void)
18360Sstevel@tonic-gate  */
18370Sstevel@tonic-gate void
si_cache_del(si_t * sp,int signature)18380Sstevel@tonic-gate si_cache_del(si_t *sp, int signature)
18390Sstevel@tonic-gate {
18400Sstevel@tonic-gate 	si_t	**tspp;
18410Sstevel@tonic-gate 	int	hash;
18420Sstevel@tonic-gate 	int	foundacl = 0;
18430Sstevel@tonic-gate 
18440Sstevel@tonic-gate 	/*
18450Sstevel@tonic-gate 	 * Unlink & free the sp from the other queues, then destroy it.
18460Sstevel@tonic-gate 	 * Search the 'by acl' chain first, then the 'by inode' chain
18470Sstevel@tonic-gate 	 * after the acl is locked.
18480Sstevel@tonic-gate 	 */
18490Sstevel@tonic-gate 	rw_enter(&si_cache_lock, RW_WRITER);
18500Sstevel@tonic-gate 	hash = SI_HASH(signature);
18510Sstevel@tonic-gate 	for (tspp = &si_cachea[hash]; *tspp; tspp = &(*tspp)->s_next) {
18520Sstevel@tonic-gate 		if (*tspp == sp) {
18530Sstevel@tonic-gate 			/*
18540Sstevel@tonic-gate 			 * Wait to grab the acl lock until after the acl has
18550Sstevel@tonic-gate 			 * been found in the cache.  Otherwise it might try to
18560Sstevel@tonic-gate 			 * grab a lock that has already been destroyed, or
18570Sstevel@tonic-gate 			 * delete an acl that has already been freed.
18580Sstevel@tonic-gate 			 */
18590Sstevel@tonic-gate 			rw_enter(&sp->s_lock, RW_WRITER);
18600Sstevel@tonic-gate 			/* See if someone else attached to it */
18610Sstevel@tonic-gate 			if (sp->s_ref) {
18620Sstevel@tonic-gate 				rw_exit(&sp->s_lock);
18630Sstevel@tonic-gate 				rw_exit(&si_cache_lock);
18640Sstevel@tonic-gate 				return;
18650Sstevel@tonic-gate 			}
18660Sstevel@tonic-gate 			ASSERT(sp->s_fore == NULL);
18670Sstevel@tonic-gate 			ASSERT(sp->s_flags & SI_CACHED);
18680Sstevel@tonic-gate 			foundacl = 1;
18690Sstevel@tonic-gate 			*tspp = sp->s_next;
18700Sstevel@tonic-gate 			break;
18710Sstevel@tonic-gate 		}
18720Sstevel@tonic-gate 	}
18730Sstevel@tonic-gate 
18740Sstevel@tonic-gate 	/*
18750Sstevel@tonic-gate 	 * If the acl was not in the cache, we assume another thread has
18760Sstevel@tonic-gate 	 * deleted it already. This could happen if another thread attaches to
18770Sstevel@tonic-gate 	 * the acl and then releases it after this thread has already found the
18780Sstevel@tonic-gate 	 * reference count to be zero but has not yet taken the cache lock.
18790Sstevel@tonic-gate 	 * Both threads end up seeing a reference count of zero, and call into
18800Sstevel@tonic-gate 	 * si_cache_del.  See bug 4244827 for details on the race condition.
18810Sstevel@tonic-gate 	 */
18820Sstevel@tonic-gate 	if (foundacl == 0) {
18830Sstevel@tonic-gate 		rw_exit(&si_cache_lock);
18840Sstevel@tonic-gate 		return;
18850Sstevel@tonic-gate 	}
18860Sstevel@tonic-gate 
18870Sstevel@tonic-gate 	/* Now check the 'by inode' chain */
18880Sstevel@tonic-gate 	hash = SI_HASH(sp->s_shadow);
18890Sstevel@tonic-gate 	for (tspp = &si_cachei[hash]; *tspp; tspp = &(*tspp)->s_forw) {
18900Sstevel@tonic-gate 		if (*tspp == sp) {
18910Sstevel@tonic-gate 			*tspp = sp->s_forw;
18920Sstevel@tonic-gate 			break;
18930Sstevel@tonic-gate 		}
18940Sstevel@tonic-gate 	}
18950Sstevel@tonic-gate 
18960Sstevel@tonic-gate 	/*
18970Sstevel@tonic-gate 	 * At this point, we can unlock everything because this si
18980Sstevel@tonic-gate 	 * is no longer in the cache, thus cannot be attached to.
18990Sstevel@tonic-gate 	 */
19000Sstevel@tonic-gate 	rw_exit(&sp->s_lock);
19010Sstevel@tonic-gate 	rw_exit(&si_cache_lock);
19020Sstevel@tonic-gate 	sp->s_flags &= ~SI_CACHED;
19030Sstevel@tonic-gate 	(void) ufs_si_free_mem(sp);
19040Sstevel@tonic-gate }
19050Sstevel@tonic-gate 
19060Sstevel@tonic-gate /*
19070Sstevel@tonic-gate  * Alloc the hash buckets for the si cache & initialize
19080Sstevel@tonic-gate  * the unreferenced anchor and the cache lock.
19090Sstevel@tonic-gate  */
19100Sstevel@tonic-gate void
si_cache_init(void)19110Sstevel@tonic-gate si_cache_init(void)
19120Sstevel@tonic-gate {
19130Sstevel@tonic-gate 	rw_init(&si_cache_lock, NULL, RW_DEFAULT, NULL);
19140Sstevel@tonic-gate 
19150Sstevel@tonic-gate 	/* The 'by acl' headers */
19160Sstevel@tonic-gate 	si_cachea = kmem_zalloc(si_cachecnt * sizeof (si_t *), KM_SLEEP);
19170Sstevel@tonic-gate 	/* The 'by inode' headers */
19180Sstevel@tonic-gate 	si_cachei = kmem_zalloc(si_cachecnt * sizeof (si_t *), KM_SLEEP);
19190Sstevel@tonic-gate }
19200Sstevel@tonic-gate 
19210Sstevel@tonic-gate /*
19220Sstevel@tonic-gate  *  aclcksum takes an acl and generates a checksum.  It takes as input
19230Sstevel@tonic-gate  *  the acl to start at.
19240Sstevel@tonic-gate  *
19250Sstevel@tonic-gate  *  s_aclp - pointer to starting acl
19260Sstevel@tonic-gate  *
19270Sstevel@tonic-gate  *  returns checksum
19280Sstevel@tonic-gate  */
19290Sstevel@tonic-gate static int
aclcksum(ufs_ic_acl_t * s_aclp)19300Sstevel@tonic-gate aclcksum(ufs_ic_acl_t *s_aclp)
19310Sstevel@tonic-gate {
19320Sstevel@tonic-gate 	ufs_ic_acl_t *aclp;
19330Sstevel@tonic-gate 	int signature = 0;
19340Sstevel@tonic-gate 	for (aclp = s_aclp; aclp; aclp = aclp->acl_ic_next) {
19350Sstevel@tonic-gate 		signature += aclp->acl_ic_perm;
19360Sstevel@tonic-gate 		signature += aclp->acl_ic_who;
19370Sstevel@tonic-gate 	}
19380Sstevel@tonic-gate 	return (signature);
19390Sstevel@tonic-gate }
19400Sstevel@tonic-gate 
19410Sstevel@tonic-gate /*
19420Sstevel@tonic-gate  * Generate a unique signature for an si structure.  Used by the
19430Sstevel@tonic-gate  * search routine si_cachea_get() to quickly identify candidates
19440Sstevel@tonic-gate  * prior to calling si_cmp().
19450Sstevel@tonic-gate  * Parameters:
19460Sstevel@tonic-gate  * sp - Ptr to the si struct to generate the signature for.
19470Sstevel@tonic-gate  *
19480Sstevel@tonic-gate  * Returns:  A signature for the si struct (really a checksum)
19490Sstevel@tonic-gate  */
19500Sstevel@tonic-gate static int
si_signature(si_t * sp)19510Sstevel@tonic-gate si_signature(si_t *sp)
19520Sstevel@tonic-gate {
19530Sstevel@tonic-gate 	int signature = sp->s_dev;
19540Sstevel@tonic-gate 
19550Sstevel@tonic-gate 	signature += aclcksum(sp->aowner) + aclcksum(sp->agroup) +
19560Sstevel@tonic-gate 	    aclcksum(sp->aother) + aclcksum(sp->ausers) +
19570Sstevel@tonic-gate 	    aclcksum(sp->agroups) + aclcksum(sp->downer) +
19580Sstevel@tonic-gate 	    aclcksum(sp->dgroup) + aclcksum(sp->dother) +
19590Sstevel@tonic-gate 	    aclcksum(sp->dusers) + aclcksum(sp->dgroups);
19600Sstevel@tonic-gate 	if (sp->aclass.acl_ismask)
19610Sstevel@tonic-gate 		signature += sp->aclass.acl_maskbits;
19620Sstevel@tonic-gate 	if (sp->dclass.acl_ismask)
19630Sstevel@tonic-gate 		signature += sp->dclass.acl_maskbits;
19640Sstevel@tonic-gate 
19650Sstevel@tonic-gate 	return (signature);
19660Sstevel@tonic-gate }
19670Sstevel@tonic-gate 
19680Sstevel@tonic-gate /*
19690Sstevel@tonic-gate  * aclcmp compares to acls to see if they are identical.
19700Sstevel@tonic-gate  *
19710Sstevel@tonic-gate  * sp1 is source
19720Sstevel@tonic-gate  * sp2 is sourceb
19730Sstevel@tonic-gate  *
19740Sstevel@tonic-gate  * returns 0 if equal and 1 if not equal
19750Sstevel@tonic-gate  */
19760Sstevel@tonic-gate static int
aclcmp(ufs_ic_acl_t * aclin1p,ufs_ic_acl_t * aclin2p)19770Sstevel@tonic-gate aclcmp(ufs_ic_acl_t *aclin1p, ufs_ic_acl_t *aclin2p)
19780Sstevel@tonic-gate {
19790Sstevel@tonic-gate 	ufs_ic_acl_t *aclp1;
19800Sstevel@tonic-gate 	ufs_ic_acl_t *aclp2;
19810Sstevel@tonic-gate 
19820Sstevel@tonic-gate 	/*
19830Sstevel@tonic-gate 	 * if the starting pointers are equal then they are equal so
19840Sstevel@tonic-gate 	 * just return.
19850Sstevel@tonic-gate 	 */
19860Sstevel@tonic-gate 	if (aclin1p == aclin2p)
19870Sstevel@tonic-gate 		return (0);
19880Sstevel@tonic-gate 	/*
19890Sstevel@tonic-gate 	 * check element by element
19900Sstevel@tonic-gate 	 */
19910Sstevel@tonic-gate 	for (aclp1 = aclin1p, aclp2 = aclin2p; aclp1 && aclp2;
19920Sstevel@tonic-gate 	    aclp1 = aclp1->acl_ic_next, aclp2 = aclp2->acl_ic_next) {
19930Sstevel@tonic-gate 		if (aclp1->acl_ic_perm != aclp2->acl_ic_perm ||
19940Sstevel@tonic-gate 		    aclp1->acl_ic_who != aclp2->acl_ic_who)
19950Sstevel@tonic-gate 			return (1);
19960Sstevel@tonic-gate 	}
19970Sstevel@tonic-gate 	/*
19980Sstevel@tonic-gate 	 * both must be zero (at the end of the acl)
19990Sstevel@tonic-gate 	 */
20000Sstevel@tonic-gate 	if (aclp1 || aclp2)
20010Sstevel@tonic-gate 		return (1);
20020Sstevel@tonic-gate 
20030Sstevel@tonic-gate 	return (0);
20040Sstevel@tonic-gate }
20050Sstevel@tonic-gate 
20060Sstevel@tonic-gate /*
20070Sstevel@tonic-gate  * Do extensive, field-by-field compare of two si structures.  Returns
20080Sstevel@tonic-gate  * 0 if they are exactly identical, 1 otherwise.
20090Sstevel@tonic-gate  *
20100Sstevel@tonic-gate  * Paramters:
20110Sstevel@tonic-gate  * sp1 - Ptr to 1st si struct
20120Sstevel@tonic-gate  * sp2 - Ptr to 2nd si struct
20130Sstevel@tonic-gate  *
20140Sstevel@tonic-gate  * Returns:
20150Sstevel@tonic-gate  *		0 - Not identical
20160Sstevel@tonic-gate  * 		1 - Identical
20170Sstevel@tonic-gate  */
20180Sstevel@tonic-gate static int
si_cmp(si_t * sp1,si_t * sp2)20190Sstevel@tonic-gate si_cmp(si_t *sp1, si_t *sp2)
20200Sstevel@tonic-gate {
20210Sstevel@tonic-gate 	if (sp1->s_dev != sp2->s_dev)
20220Sstevel@tonic-gate 		return (1);
20230Sstevel@tonic-gate 	if (aclcmp(sp1->aowner, sp2->aowner) ||
20240Sstevel@tonic-gate 	    aclcmp(sp1->agroup, sp2->agroup) ||
20250Sstevel@tonic-gate 	    aclcmp(sp1->aother, sp2->aother) ||
20260Sstevel@tonic-gate 	    aclcmp(sp1->ausers, sp2->ausers) ||
20270Sstevel@tonic-gate 	    aclcmp(sp1->agroups, sp2->agroups) ||
20280Sstevel@tonic-gate 	    aclcmp(sp1->downer, sp2->downer) ||
20290Sstevel@tonic-gate 	    aclcmp(sp1->dgroup, sp2->dgroup) ||
20300Sstevel@tonic-gate 	    aclcmp(sp1->dother, sp2->dother) ||
20310Sstevel@tonic-gate 	    aclcmp(sp1->dusers, sp2->dusers) ||
20320Sstevel@tonic-gate 	    aclcmp(sp1->dgroups, sp2->dgroups))
20330Sstevel@tonic-gate 		return (1);
20340Sstevel@tonic-gate 	if (sp1->aclass.acl_ismask != sp2->aclass.acl_ismask)
20350Sstevel@tonic-gate 		return (1);
20360Sstevel@tonic-gate 	if (sp1->dclass.acl_ismask != sp2->dclass.acl_ismask)
20370Sstevel@tonic-gate 		return (1);
20380Sstevel@tonic-gate 	if (sp1->aclass.acl_ismask &&
20394662Sfrankho 	    sp1->aclass.acl_maskbits != sp2->aclass.acl_maskbits)
20400Sstevel@tonic-gate 		return (1);
20410Sstevel@tonic-gate 	if (sp1->dclass.acl_ismask &&
20424662Sfrankho 	    sp1->dclass.acl_maskbits != sp2->dclass.acl_maskbits)
20430Sstevel@tonic-gate 		return (1);
20440Sstevel@tonic-gate 
20450Sstevel@tonic-gate 	return (0);
20460Sstevel@tonic-gate }
20470Sstevel@tonic-gate 
20480Sstevel@tonic-gate /*
20490Sstevel@tonic-gate  * Remove all acls associated with a device.  All acls must have
20500Sstevel@tonic-gate  * a reference count of zero.
20510Sstevel@tonic-gate  *
20520Sstevel@tonic-gate  * inputs:
20530Sstevel@tonic-gate  *	device - device to remove from the cache
20540Sstevel@tonic-gate  *
20550Sstevel@tonic-gate  * outputs:
20560Sstevel@tonic-gate  *	none
20570Sstevel@tonic-gate  */
20580Sstevel@tonic-gate void
ufs_si_cache_flush(dev_t dev)20590Sstevel@tonic-gate ufs_si_cache_flush(dev_t dev)
20600Sstevel@tonic-gate {
20610Sstevel@tonic-gate 	si_t *tsp, **tspp;
20620Sstevel@tonic-gate 	int i;
20630Sstevel@tonic-gate 
20640Sstevel@tonic-gate 	rw_enter(&si_cache_lock, RW_WRITER);
20650Sstevel@tonic-gate 	for (i = 0; i < si_cachecnt; i++) {
20660Sstevel@tonic-gate 		tspp = &si_cachea[i];
20670Sstevel@tonic-gate 		while (*tspp) {
20680Sstevel@tonic-gate 			if ((*tspp)->s_dev == dev) {
20690Sstevel@tonic-gate 				*tspp = (*tspp)->s_next;
20700Sstevel@tonic-gate 			} else {
20710Sstevel@tonic-gate 				tspp = &(*tspp)->s_next;
20720Sstevel@tonic-gate 			}
20730Sstevel@tonic-gate 		}
20740Sstevel@tonic-gate 	}
20750Sstevel@tonic-gate 	for (i = 0; i < si_cachecnt; i++) {
20760Sstevel@tonic-gate 		tspp = &si_cachei[i];
20770Sstevel@tonic-gate 		while (*tspp) {
20780Sstevel@tonic-gate 			if ((*tspp)->s_dev == dev) {
20790Sstevel@tonic-gate 				tsp = *tspp;
20800Sstevel@tonic-gate 				*tspp = (*tspp)->s_forw;
20810Sstevel@tonic-gate 				tsp->s_flags &= ~SI_CACHED;
20820Sstevel@tonic-gate 				ufs_si_free_mem(tsp);
20830Sstevel@tonic-gate 			} else {
20840Sstevel@tonic-gate 				tspp = &(*tspp)->s_forw;
20850Sstevel@tonic-gate 			}
20860Sstevel@tonic-gate 		}
20870Sstevel@tonic-gate 	}
20880Sstevel@tonic-gate 	rw_exit(&si_cache_lock);
20890Sstevel@tonic-gate }
20900Sstevel@tonic-gate 
20910Sstevel@tonic-gate /*
20920Sstevel@tonic-gate  * ufs_si_del is used to unhook a sp from a inode in memory
20930Sstevel@tonic-gate  *
20940Sstevel@tonic-gate  * ip is the inode to remove the sp from.
20950Sstevel@tonic-gate  */
20960Sstevel@tonic-gate void
ufs_si_del(struct inode * ip)20970Sstevel@tonic-gate ufs_si_del(struct inode *ip)
20980Sstevel@tonic-gate {
20990Sstevel@tonic-gate 	si_t    *sp = ip->i_ufs_acl;
21000Sstevel@tonic-gate 	int	refcnt;
21010Sstevel@tonic-gate 	int	signature;
21020Sstevel@tonic-gate 
21030Sstevel@tonic-gate 	if (sp) {
21040Sstevel@tonic-gate 		rw_enter(&sp->s_lock, RW_WRITER);
21050Sstevel@tonic-gate 		refcnt = --sp->s_ref;
21060Sstevel@tonic-gate 		signature = sp->s_signature;
21070Sstevel@tonic-gate 		ASSERT(sp->s_ref >= 0 && sp->s_ref <= sp->s_use);
21080Sstevel@tonic-gate 		rw_exit(&sp->s_lock);
21090Sstevel@tonic-gate 		if (refcnt == 0)
21100Sstevel@tonic-gate 			si_cache_del(sp, signature);
21110Sstevel@tonic-gate 		ip->i_ufs_acl = NULL;
21120Sstevel@tonic-gate 	}
21130Sstevel@tonic-gate }
2114