17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
580d3443frankho * Common Development and Distribution License (the "License").
680d3443frankho * You may not use this file except in compliance with the License.
77c478bdstevel@tonic-gate *
87c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bdstevel@tonic-gate * See the License for the specific language governing permissions
117c478bdstevel@tonic-gate * and limitations under the License.
127c478bdstevel@tonic-gate *
137c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bdstevel@tonic-gate *
197c478bdstevel@tonic-gate * CDDL HEADER END
207c478bdstevel@tonic-gate */
217c478bdstevel@tonic-gate/*
2280d3443frankho * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bdstevel@tonic-gate * Use is subject to license terms.
247c478bdstevel@tonic-gate */
257c478bdstevel@tonic-gate
267c478bdstevel@tonic-gate#pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bdstevel@tonic-gate
287c478bdstevel@tonic-gate#include <sys/t_lock.h>
297c478bdstevel@tonic-gate#include <sys/param.h>
307c478bdstevel@tonic-gate#include <sys/systm.h>
317c478bdstevel@tonic-gate#include <sys/signal.h>
327c478bdstevel@tonic-gate#include <sys/cred.h>
337c478bdstevel@tonic-gate#include <sys/proc.h>
347c478bdstevel@tonic-gate#include <sys/disp.h>
357c478bdstevel@tonic-gate#include <sys/user.h>
367c478bdstevel@tonic-gate#include <sys/vfs.h>
377c478bdstevel@tonic-gate#include <sys/vnode.h>
387c478bdstevel@tonic-gate#include <sys/stat.h>
397c478bdstevel@tonic-gate#include <sys/mode.h>
407c478bdstevel@tonic-gate#include <sys/buf.h>
417c478bdstevel@tonic-gate#include <sys/uio.h>
427c478bdstevel@tonic-gate#include <sys/dnlc.h>
437c478bdstevel@tonic-gate#include <sys/pathname.h>
447c478bdstevel@tonic-gate#include <sys/fs/ufs_inode.h>
457c478bdstevel@tonic-gate#include <sys/fs/ufs_fs.h>
467c478bdstevel@tonic-gate#include <sys/mount.h>
477c478bdstevel@tonic-gate#include <sys/fs/ufs_fsdir.h>
487c478bdstevel@tonic-gate#include <sys/fs/ufs_trans.h>
497c478bdstevel@tonic-gate#include <sys/fs/ufs_panic.h>
507c478bdstevel@tonic-gate#include <sys/fs/ufs_quota.h>
517c478bdstevel@tonic-gate#include <sys/errno.h>
527c478bdstevel@tonic-gate#include <sys/debug.h>
537c478bdstevel@tonic-gate#include <vm/seg.h>
547c478bdstevel@tonic-gate#include <sys/sysmacros.h>
557c478bdstevel@tonic-gate#include <sys/cmn_err.h>
567c478bdstevel@tonic-gate#include <sys/cpuvar.h>
577c478bdstevel@tonic-gate#include <sys/unistd.h>
587c478bdstevel@tonic-gate
597c478bdstevel@tonic-gateint
607c478bdstevel@tonic-gateufs_xattr_getattrdir(
617c478bdstevel@tonic-gate	vnode_t *dvp,
627c478bdstevel@tonic-gate	struct inode **sip,
637c478bdstevel@tonic-gate	int flags,
647c478bdstevel@tonic-gate	struct cred *cr)
657c478bdstevel@tonic-gate{
667c478bdstevel@tonic-gate	struct vfs	*vfsp;
677c478bdstevel@tonic-gate	struct inode	*ip, *sdp;
687c478bdstevel@tonic-gate	int		error;
697c478bdstevel@tonic-gate
707c478bdstevel@tonic-gate	ip = VTOI(dvp);
717c478bdstevel@tonic-gate	if (flags & LOOKUP_XATTR) {
727c478bdstevel@tonic-gate		if (ip && ((ip->i_oeftflag) != 0)) {
737c478bdstevel@tonic-gate			vfsp = dvp->v_vfsp;
747c478bdstevel@tonic-gate
757c478bdstevel@tonic-gate			error = ufs_iget(vfsp, ip->i_oeftflag, sip, cr);
767c478bdstevel@tonic-gate			if (error)
777c478bdstevel@tonic-gate				return (error);
787c478bdstevel@tonic-gate
797c478bdstevel@tonic-gate			sdp = *sip;
807c478bdstevel@tonic-gate
817c478bdstevel@tonic-gate			/*
827c478bdstevel@tonic-gate			 * Make sure it really is an ATTRDIR
837c478bdstevel@tonic-gate			 */
847c478bdstevel@tonic-gate			if ((sdp->i_mode & IFMT) != IFATTRDIR) {
857c478bdstevel@tonic-gate				cmn_err(CE_NOTE, "ufs_getattrdir: inode %d"
8680d3443frankho				    " points to attribute directory %d "
8780d3443frankho				    "which is not an attribute directory;"
8880d3443frankho				    "run fsck on file system",
8980d3443frankho				    (int)ip->i_number, (int)sdp->i_number);
907c478bdstevel@tonic-gate				VN_RELE(ITOV(sdp));
917c478bdstevel@tonic-gate				return (ENOENT);
927c478bdstevel@tonic-gate			}
937c478bdstevel@tonic-gate			ITOV(sdp)->v_type = VDIR;
947c478bdstevel@tonic-gate			ITOV(sdp)->v_flag |= V_XATTRDIR;
957c478bdstevel@tonic-gate			error = 0;
967c478bdstevel@tonic-gate			goto out;
977c478bdstevel@tonic-gate		} else if (flags & CREATE_XATTR_DIR) {
987c478bdstevel@tonic-gate			error = ufs_xattrmkdir(ip, sip, 1, cr);
997c478bdstevel@tonic-gate		} else {
1007c478bdstevel@tonic-gate			error = ENOENT;
1017c478bdstevel@tonic-gate				goto out;
1027c478bdstevel@tonic-gate		}
1037c478bdstevel@tonic-gate
1047c478bdstevel@tonic-gate	} else if (flags & CREATE_XATTR_DIR) {
1057c478bdstevel@tonic-gate		error = ufs_xattrmkdir(ip, sip, 1, cr);
1067c478bdstevel@tonic-gate	} else {
1077c478bdstevel@tonic-gate		error = ENOENT;
1087c478bdstevel@tonic-gate	}
1097c478bdstevel@tonic-gateout:
1107c478bdstevel@tonic-gate	return (error);
1117c478bdstevel@tonic-gate}
1127c478bdstevel@tonic-gate
1137c478bdstevel@tonic-gate
1147c478bdstevel@tonic-gate/*
1157c478bdstevel@tonic-gate * Unhook an attribute directory from a parent file/dir
1167c478bdstevel@tonic-gate * Only do so, if we are the only user of the vnode.
1177c478bdstevel@tonic-gate */
1187c478bdstevel@tonic-gatevoid
1197c478bdstevel@tonic-gateufs_unhook_shadow(struct inode *ip, struct inode *sip)
1207c478bdstevel@tonic-gate{
1217c478bdstevel@tonic-gate	struct vnode		*datavp = ITOV(ip);
1227c478bdstevel@tonic-gate	struct vnode		*dirvp = ITOV(sip);
1237c478bdstevel@tonic-gate	int			hno;
1247c478bdstevel@tonic-gate	kmutex_t		*ihm;
1257c478bdstevel@tonic-gate
1267c478bdstevel@tonic-gate	ASSERT(RW_WRITE_HELD(&sip->i_contents));
1277c478bdstevel@tonic-gate	ASSERT(RW_WRITE_HELD(&ip->i_contents));
1287c478bdstevel@tonic-gate
1297c478bdstevel@tonic-gate	if (vn_is_readonly(ITOV(ip)))
1307c478bdstevel@tonic-gate		return;
1317c478bdstevel@tonic-gate
1327c478bdstevel@tonic-gate	if (ip->i_ufsvfs == NULL || sip->i_ufsvfs == NULL)
1337c478bdstevel@tonic-gate		return;
1347c478bdstevel@tonic-gate
1357c478bdstevel@tonic-gate	hno = INOHASH(ip->i_number);
1367c478bdstevel@tonic-gate	ihm = &ih_lock[hno];
1377c478bdstevel@tonic-gate	mutex_enter(ihm);
1387c478bdstevel@tonic-gate
1397c478bdstevel@tonic-gate	mutex_enter(&datavp->v_lock);
1407c478bdstevel@tonic-gate	mutex_enter(&dirvp->v_lock);
1417c478bdstevel@tonic-gate
1427c478bdstevel@tonic-gate	if (dirvp->v_count != 1 && datavp->v_count != 1) {
1437c478bdstevel@tonic-gate		mutex_exit(&dirvp->v_lock);
1447c478bdstevel@tonic-gate		mutex_exit(&datavp->v_lock);
1457c478bdstevel@tonic-gate		mutex_exit(ihm);
1467c478bdstevel@tonic-gate		return;
1477c478bdstevel@tonic-gate	}
1487c478bdstevel@tonic-gate
1497c478bdstevel@tonic-gate	/*
1507c478bdstevel@tonic-gate	 * Delete shadow from ip
1517c478bdstevel@tonic-gate	 */
1527c478bdstevel@tonic-gate
1537c478bdstevel@tonic-gate	sip->i_nlink -= 2;
1547c478bdstevel@tonic-gate	ufs_setreclaim(sip);
1557c478bdstevel@tonic-gate	TRANS_INODE(sip->i_ufsvfs, sip);
1567c478bdstevel@tonic-gate	sip->i_flag |= ICHG;
1577c478bdstevel@tonic-gate	sip->i_seq++;
1587c478bdstevel@tonic-gate	ITIMES_NOLOCK(sip);
1597c478bdstevel@tonic-gate
1607c478bdstevel@tonic-gate	/*
1617c478bdstevel@tonic-gate	 * Update src file
1627c478bdstevel@tonic-gate	 */
1637c478bdstevel@tonic-gate	ip->i_oeftflag = 0;
1647c478bdstevel@tonic-gate	TRANS_INODE(ip->i_ufsvfs, ip);
1657c478bdstevel@tonic-gate	ip->i_flag |= ICHG;
1667c478bdstevel@tonic-gate	ip->i_seq++;
1677c478bdstevel@tonic-gate	ufs_iupdat(ip, 1);
1687c478bdstevel@tonic-gate	mutex_exit(&dirvp->v_lock);
1697c478bdstevel@tonic-gate	mutex_exit(&datavp->v_lock);
1707c478bdstevel@tonic-gate	mutex_exit(ihm);
1717c478bdstevel@tonic-gate}
172