xref: /illumos-gate/usr/src/uts/common/fs/ufs/ufs_xattr.c (revision 7c478bd95313f5f23a4c958a745db2134aa0324)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
30*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
31*7c478bd9Sstevel@tonic-gate #include <sys/systm.h>
32*7c478bd9Sstevel@tonic-gate #include <sys/signal.h>
33*7c478bd9Sstevel@tonic-gate #include <sys/cred.h>
34*7c478bd9Sstevel@tonic-gate #include <sys/proc.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/disp.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/user.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
39*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
40*7c478bd9Sstevel@tonic-gate #include <sys/mode.h>
41*7c478bd9Sstevel@tonic-gate #include <sys/buf.h>
42*7c478bd9Sstevel@tonic-gate #include <sys/uio.h>
43*7c478bd9Sstevel@tonic-gate #include <sys/dnlc.h>
44*7c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
45*7c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_inode.h>
46*7c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_fs.h>
47*7c478bd9Sstevel@tonic-gate #include <sys/mount.h>
48*7c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_fsdir.h>
49*7c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_trans.h>
50*7c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_panic.h>
51*7c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_quota.h>
52*7c478bd9Sstevel@tonic-gate #include <sys/errno.h>
53*7c478bd9Sstevel@tonic-gate #include <sys/debug.h>
54*7c478bd9Sstevel@tonic-gate #include <vm/seg.h>
55*7c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
56*7c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
57*7c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
58*7c478bd9Sstevel@tonic-gate #include <sys/unistd.h>
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate int
61*7c478bd9Sstevel@tonic-gate ufs_xattr_getattrdir(
62*7c478bd9Sstevel@tonic-gate 	vnode_t *dvp,
63*7c478bd9Sstevel@tonic-gate 	struct inode **sip,
64*7c478bd9Sstevel@tonic-gate 	int flags,
65*7c478bd9Sstevel@tonic-gate 	struct cred *cr)
66*7c478bd9Sstevel@tonic-gate {
67*7c478bd9Sstevel@tonic-gate 	struct vfs	*vfsp;
68*7c478bd9Sstevel@tonic-gate 	struct inode	*ip, *sdp;
69*7c478bd9Sstevel@tonic-gate 	int		error;
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate 	ip = VTOI(dvp);
72*7c478bd9Sstevel@tonic-gate 	if (flags & LOOKUP_XATTR) {
73*7c478bd9Sstevel@tonic-gate 		if (ip && ((ip->i_oeftflag) != 0)) {
74*7c478bd9Sstevel@tonic-gate 			vfsp = dvp->v_vfsp;
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate 			error = ufs_iget(vfsp, ip->i_oeftflag, sip, cr);
77*7c478bd9Sstevel@tonic-gate 			if (error)
78*7c478bd9Sstevel@tonic-gate 				return (error);
79*7c478bd9Sstevel@tonic-gate 
80*7c478bd9Sstevel@tonic-gate 			sdp = *sip;
81*7c478bd9Sstevel@tonic-gate 
82*7c478bd9Sstevel@tonic-gate 			/*
83*7c478bd9Sstevel@tonic-gate 			 * Make sure it really is an ATTRDIR
84*7c478bd9Sstevel@tonic-gate 			 */
85*7c478bd9Sstevel@tonic-gate 			if ((sdp->i_mode & IFMT) != IFATTRDIR) {
86*7c478bd9Sstevel@tonic-gate 				cmn_err(CE_NOTE, "ufs_getattrdir: inode %d"
87*7c478bd9Sstevel@tonic-gate 					" points to attribute directory %d "
88*7c478bd9Sstevel@tonic-gate 					"which is not an attribute directory;"
89*7c478bd9Sstevel@tonic-gate 					"run fsck on file system",
90*7c478bd9Sstevel@tonic-gate 					(int)ip->i_number, (int)sdp->i_number);
91*7c478bd9Sstevel@tonic-gate 				VN_RELE(ITOV(sdp));
92*7c478bd9Sstevel@tonic-gate 				return (ENOENT);
93*7c478bd9Sstevel@tonic-gate 			}
94*7c478bd9Sstevel@tonic-gate 			ITOV(sdp)->v_type = VDIR;
95*7c478bd9Sstevel@tonic-gate 			ITOV(sdp)->v_flag |= V_XATTRDIR;
96*7c478bd9Sstevel@tonic-gate 			error = 0;
97*7c478bd9Sstevel@tonic-gate 			goto out;
98*7c478bd9Sstevel@tonic-gate 		} else if (flags & CREATE_XATTR_DIR) {
99*7c478bd9Sstevel@tonic-gate 			error = ufs_xattrmkdir(ip, sip, 1, cr);
100*7c478bd9Sstevel@tonic-gate 		} else {
101*7c478bd9Sstevel@tonic-gate 			error = ENOENT;
102*7c478bd9Sstevel@tonic-gate 				goto out;
103*7c478bd9Sstevel@tonic-gate 		}
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate 	} else if (flags & CREATE_XATTR_DIR) {
106*7c478bd9Sstevel@tonic-gate 		error = ufs_xattrmkdir(ip, sip, 1, cr);
107*7c478bd9Sstevel@tonic-gate 	} else {
108*7c478bd9Sstevel@tonic-gate 		error = ENOENT;
109*7c478bd9Sstevel@tonic-gate 	}
110*7c478bd9Sstevel@tonic-gate out:
111*7c478bd9Sstevel@tonic-gate 	return (error);
112*7c478bd9Sstevel@tonic-gate }
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate 
115*7c478bd9Sstevel@tonic-gate /*
116*7c478bd9Sstevel@tonic-gate  * Unhook an attribute directory from a parent file/dir
117*7c478bd9Sstevel@tonic-gate  * Only do so, if we are the only user of the vnode.
118*7c478bd9Sstevel@tonic-gate  */
119*7c478bd9Sstevel@tonic-gate void
120*7c478bd9Sstevel@tonic-gate ufs_unhook_shadow(struct inode *ip, struct inode *sip)
121*7c478bd9Sstevel@tonic-gate {
122*7c478bd9Sstevel@tonic-gate 	struct vnode		*datavp = ITOV(ip);
123*7c478bd9Sstevel@tonic-gate 	struct vnode		*dirvp = ITOV(sip);
124*7c478bd9Sstevel@tonic-gate 	int			hno;
125*7c478bd9Sstevel@tonic-gate 	kmutex_t		*ihm;
126*7c478bd9Sstevel@tonic-gate 
127*7c478bd9Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&sip->i_contents));
128*7c478bd9Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&ip->i_contents));
129*7c478bd9Sstevel@tonic-gate 
130*7c478bd9Sstevel@tonic-gate 	if (vn_is_readonly(ITOV(ip)))
131*7c478bd9Sstevel@tonic-gate 		return;
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate 	if (ip->i_ufsvfs == NULL || sip->i_ufsvfs == NULL)
134*7c478bd9Sstevel@tonic-gate 		return;
135*7c478bd9Sstevel@tonic-gate 
136*7c478bd9Sstevel@tonic-gate 	hno = INOHASH(ip->i_number);
137*7c478bd9Sstevel@tonic-gate 	ihm = &ih_lock[hno];
138*7c478bd9Sstevel@tonic-gate 	mutex_enter(ihm);
139*7c478bd9Sstevel@tonic-gate 
140*7c478bd9Sstevel@tonic-gate 	mutex_enter(&datavp->v_lock);
141*7c478bd9Sstevel@tonic-gate 	mutex_enter(&dirvp->v_lock);
142*7c478bd9Sstevel@tonic-gate 
143*7c478bd9Sstevel@tonic-gate 	if (dirvp->v_count != 1 && datavp->v_count != 1) {
144*7c478bd9Sstevel@tonic-gate 		mutex_exit(&dirvp->v_lock);
145*7c478bd9Sstevel@tonic-gate 		mutex_exit(&datavp->v_lock);
146*7c478bd9Sstevel@tonic-gate 		mutex_exit(ihm);
147*7c478bd9Sstevel@tonic-gate 		return;
148*7c478bd9Sstevel@tonic-gate 	}
149*7c478bd9Sstevel@tonic-gate 
150*7c478bd9Sstevel@tonic-gate 	/*
151*7c478bd9Sstevel@tonic-gate 	 * Delete shadow from ip
152*7c478bd9Sstevel@tonic-gate 	 */
153*7c478bd9Sstevel@tonic-gate 
154*7c478bd9Sstevel@tonic-gate 	sip->i_nlink -= 2;
155*7c478bd9Sstevel@tonic-gate 	ufs_setreclaim(sip);
156*7c478bd9Sstevel@tonic-gate 	TRANS_INODE(sip->i_ufsvfs, sip);
157*7c478bd9Sstevel@tonic-gate 	sip->i_flag |= ICHG;
158*7c478bd9Sstevel@tonic-gate 	sip->i_seq++;
159*7c478bd9Sstevel@tonic-gate 	ITIMES_NOLOCK(sip);
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate 	/*
162*7c478bd9Sstevel@tonic-gate 	 * Update src file
163*7c478bd9Sstevel@tonic-gate 	 */
164*7c478bd9Sstevel@tonic-gate 	ip->i_oeftflag = 0;
165*7c478bd9Sstevel@tonic-gate 	TRANS_INODE(ip->i_ufsvfs, ip);
166*7c478bd9Sstevel@tonic-gate 	ip->i_flag |= ICHG;
167*7c478bd9Sstevel@tonic-gate 	ip->i_seq++;
168*7c478bd9Sstevel@tonic-gate 	ufs_iupdat(ip, 1);
169*7c478bd9Sstevel@tonic-gate 	mutex_exit(&dirvp->v_lock);
170*7c478bd9Sstevel@tonic-gate 	mutex_exit(&datavp->v_lock);
171*7c478bd9Sstevel@tonic-gate 	mutex_exit(ihm);
172*7c478bd9Sstevel@tonic-gate }
173