xref: /titanic_50/usr/src/uts/common/fs/xattr.c (revision bda89588bd7667394a834e8a9a34612cce2ae9c3)
1da6c28aaSamw /*
2da6c28aaSamw  * CDDL HEADER START
3da6c28aaSamw  *
4da6c28aaSamw  * The contents of this file are subject to the terms of the
5da6c28aaSamw  * Common Development and Distribution License (the "License").
6da6c28aaSamw  * You may not use this file except in compliance with the License.
7da6c28aaSamw  *
8da6c28aaSamw  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9da6c28aaSamw  * or http://www.opensolaris.org/os/licensing.
10da6c28aaSamw  * See the License for the specific language governing permissions
11da6c28aaSamw  * and limitations under the License.
12da6c28aaSamw  *
13da6c28aaSamw  * When distributing Covered Code, include this CDDL HEADER in each
14da6c28aaSamw  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15da6c28aaSamw  * If applicable, add the following below this CDDL HEADER, with the
16da6c28aaSamw  * fields enclosed by brackets "[]" replaced with your own identifying
17da6c28aaSamw  * information: Portions Copyright [yyyy] [name of copyright owner]
18da6c28aaSamw  *
19da6c28aaSamw  * CDDL HEADER END
20da6c28aaSamw  */
21da6c28aaSamw /*
22f5f5959bSck153898  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23da6c28aaSamw  * Use is subject to license terms.
24da6c28aaSamw  */
25da6c28aaSamw 
26da6c28aaSamw #pragma ident	"%Z%%M%	%I%	%E% SMI"
27da6c28aaSamw 
28da6c28aaSamw #include <sys/param.h>
29da6c28aaSamw #include <sys/isa_defs.h>
30da6c28aaSamw #include <sys/types.h>
31da6c28aaSamw #include <sys/sysmacros.h>
32da6c28aaSamw #include <sys/cred.h>
33da6c28aaSamw #include <sys/systm.h>
34da6c28aaSamw #include <sys/errno.h>
35da6c28aaSamw #include <sys/fcntl.h>
36da6c28aaSamw #include <sys/pathname.h>
37da6c28aaSamw #include <sys/stat.h>
38da6c28aaSamw #include <sys/vfs.h>
39da6c28aaSamw #include <sys/acl.h>
40da6c28aaSamw #include <sys/file.h>
41da6c28aaSamw #include <sys/sunddi.h>
42da6c28aaSamw #include <sys/debug.h>
43da6c28aaSamw #include <sys/cmn_err.h>
44da6c28aaSamw #include <sys/vnode.h>
45da6c28aaSamw #include <sys/mode.h>
46da6c28aaSamw #include <sys/nvpair.h>
47da6c28aaSamw #include <sys/attr.h>
48da6c28aaSamw #include <sys/gfs.h>
49da6c28aaSamw #include <sys/mutex.h>
50da6c28aaSamw #include <fs/fs_subr.h>
51da6c28aaSamw #include <sys/kidmap.h>
52da6c28aaSamw 
53da6c28aaSamw typedef struct {
54da6c28aaSamw 	gfs_file_t	gfs_private;
55da6c28aaSamw 	xattr_view_t	xattr_view;
56da6c28aaSamw } xattr_file_t;
57da6c28aaSamw 
58da6c28aaSamw /* ARGSUSED */
59da6c28aaSamw static int
60da6c28aaSamw xattr_file_open(vnode_t **vpp, int flags, cred_t *cr, caller_context_t *ct)
61da6c28aaSamw {
62da6c28aaSamw 	xattr_file_t *np = (*vpp)->v_data;
63da6c28aaSamw 
64da6c28aaSamw 	if ((np->xattr_view == XATTR_VIEW_READONLY) && (flags & FWRITE))
65da6c28aaSamw 		return (EACCES);
66da6c28aaSamw 
67da6c28aaSamw 	return (0);
68da6c28aaSamw }
69da6c28aaSamw 
70da6c28aaSamw /* ARGSUSED */
71da6c28aaSamw static int
72da6c28aaSamw xattr_file_access(vnode_t *vp, int mode, int flags, cred_t *cr,
73da6c28aaSamw     caller_context_t *ct)
74da6c28aaSamw {
75da6c28aaSamw 	xattr_file_t *np = vp->v_data;
76da6c28aaSamw 
77da6c28aaSamw 	if ((np->xattr_view == XATTR_VIEW_READONLY) && (mode & VWRITE))
78da6c28aaSamw 		return (EACCES);
79da6c28aaSamw 
80da6c28aaSamw 	return (0);
81da6c28aaSamw }
82da6c28aaSamw 
83da6c28aaSamw /* ARGSUSED */
84da6c28aaSamw static int
85da6c28aaSamw xattr_file_close(vnode_t *vp, int flags, int count, offset_t off,
86da6c28aaSamw     cred_t *cr, caller_context_t *ct)
87da6c28aaSamw {
88da6c28aaSamw 	cleanlocks(vp, ddi_get_pid(), 0);
89da6c28aaSamw 	cleanshares(vp, ddi_get_pid());
90da6c28aaSamw 	return (0);
91da6c28aaSamw }
92da6c28aaSamw 
93da6c28aaSamw static int
94da6c28aaSamw xattr_common_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
95da6c28aaSamw {
96da6c28aaSamw 	xattr_fid_t	*xfidp;
97da6c28aaSamw 	vnode_t		*pvp, *savevp;
98da6c28aaSamw 	int		error;
99da6c28aaSamw 	uint16_t	orig_len;
100da6c28aaSamw 
101da6c28aaSamw 	if (fidp->fid_len < XATTR_FIDSZ) {
102da6c28aaSamw 		fidp->fid_len = XATTR_FIDSZ;
103da6c28aaSamw 		return (ENOSPC);
104da6c28aaSamw 	}
105da6c28aaSamw 
106da6c28aaSamw 	savevp = pvp = gfs_file_parent(vp);
107da6c28aaSamw 	mutex_enter(&savevp->v_lock);
108da6c28aaSamw 	if (pvp->v_flag & V_XATTRDIR) {
109da6c28aaSamw 		pvp = gfs_file_parent(pvp);
110da6c28aaSamw 	}
111da6c28aaSamw 	mutex_exit(&savevp->v_lock);
112da6c28aaSamw 
113da6c28aaSamw 	xfidp = (xattr_fid_t *)fidp;
114da6c28aaSamw 	orig_len = fidp->fid_len;
115da6c28aaSamw 	fidp->fid_len = sizeof (xfidp->parent_fid);
116da6c28aaSamw 
117da6c28aaSamw 	error = VOP_FID(pvp, fidp, ct);
118da6c28aaSamw 	if (error) {
119da6c28aaSamw 		fidp->fid_len = orig_len;
120da6c28aaSamw 		return (error);
121da6c28aaSamw 	}
122da6c28aaSamw 
123da6c28aaSamw 	xfidp->parent_len = fidp->fid_len;
124da6c28aaSamw 	fidp->fid_len = XATTR_FIDSZ;
125da6c28aaSamw 	xfidp->dir_offset = gfs_file_inode(vp);
126da6c28aaSamw 
127da6c28aaSamw 	return (0);
128da6c28aaSamw }
129da6c28aaSamw 
130da6c28aaSamw /* ARGSUSED */
131da6c28aaSamw static int
132da6c28aaSamw xattr_fill_nvlist(vnode_t *vp, xattr_view_t xattr_view, nvlist_t *nvlp,
133da6c28aaSamw     cred_t *cr, caller_context_t *ct)
134da6c28aaSamw {
135da6c28aaSamw 	int error;
136da6c28aaSamw 	f_attr_t attr;
137da6c28aaSamw 	uint64_t fsid;
138da6c28aaSamw 	xvattr_t xvattr;
139da6c28aaSamw 	xoptattr_t *xoap;	/* Pointer to optional attributes */
140da6c28aaSamw 	vnode_t *ppvp;
141da6c28aaSamw 	const char *domain;
142da6c28aaSamw 	uint32_t rid;
143da6c28aaSamw 
144da6c28aaSamw 	xva_init(&xvattr);
145da6c28aaSamw 
146da6c28aaSamw 	if ((xoap = xva_getxoptattr(&xvattr)) == NULL)
147da6c28aaSamw 		return (EINVAL);
148da6c28aaSamw 
149da6c28aaSamw 	/*
150da6c28aaSamw 	 * For detecting ephemeral uid/gid
151da6c28aaSamw 	 */
152da6c28aaSamw 	xvattr.xva_vattr.va_mask |= (AT_UID|AT_GID);
153da6c28aaSamw 
154da6c28aaSamw 	/*
155da6c28aaSamw 	 * We need to access the real fs object.
156da6c28aaSamw 	 * vp points to a GFS file; ppvp points to the real object.
157da6c28aaSamw 	 */
158da6c28aaSamw 	ppvp = gfs_file_parent(gfs_file_parent(vp));
159da6c28aaSamw 
160da6c28aaSamw 	/*
161da6c28aaSamw 	 * Iterate through the attrs associated with this view
162da6c28aaSamw 	 */
163da6c28aaSamw 
164da6c28aaSamw 	for (attr = 0; attr < F_ATTR_ALL; attr++) {
165da6c28aaSamw 		if (xattr_view != attr_to_xattr_view(attr)) {
166da6c28aaSamw 			continue;
167da6c28aaSamw 		}
168da6c28aaSamw 
169da6c28aaSamw 		switch (attr) {
170da6c28aaSamw 		case F_SYSTEM:
171da6c28aaSamw 			XVA_SET_REQ(&xvattr, XAT_SYSTEM);
172da6c28aaSamw 			break;
173da6c28aaSamw 		case F_READONLY:
174da6c28aaSamw 			XVA_SET_REQ(&xvattr, XAT_READONLY);
175da6c28aaSamw 			break;
176da6c28aaSamw 		case F_HIDDEN:
177da6c28aaSamw 			XVA_SET_REQ(&xvattr, XAT_HIDDEN);
178da6c28aaSamw 			break;
179da6c28aaSamw 		case F_ARCHIVE:
180da6c28aaSamw 			XVA_SET_REQ(&xvattr, XAT_ARCHIVE);
181da6c28aaSamw 			break;
182da6c28aaSamw 		case F_IMMUTABLE:
183da6c28aaSamw 			XVA_SET_REQ(&xvattr, XAT_IMMUTABLE);
184da6c28aaSamw 			break;
185da6c28aaSamw 		case F_APPENDONLY:
186da6c28aaSamw 			XVA_SET_REQ(&xvattr, XAT_APPENDONLY);
187da6c28aaSamw 			break;
188da6c28aaSamw 		case F_NOUNLINK:
189da6c28aaSamw 			XVA_SET_REQ(&xvattr, XAT_NOUNLINK);
190da6c28aaSamw 			break;
191da6c28aaSamw 		case F_OPAQUE:
192da6c28aaSamw 			XVA_SET_REQ(&xvattr, XAT_OPAQUE);
193da6c28aaSamw 			break;
194da6c28aaSamw 		case F_NODUMP:
195da6c28aaSamw 			XVA_SET_REQ(&xvattr, XAT_NODUMP);
196da6c28aaSamw 			break;
197da6c28aaSamw 		case F_AV_QUARANTINED:
198da6c28aaSamw 			XVA_SET_REQ(&xvattr, XAT_AV_QUARANTINED);
199da6c28aaSamw 			break;
200da6c28aaSamw 		case F_AV_MODIFIED:
201da6c28aaSamw 			XVA_SET_REQ(&xvattr, XAT_AV_MODIFIED);
202da6c28aaSamw 			break;
203da6c28aaSamw 		case F_AV_SCANSTAMP:
204da6c28aaSamw 			if (ppvp->v_type == VREG)
205da6c28aaSamw 				XVA_SET_REQ(&xvattr, XAT_AV_SCANSTAMP);
206da6c28aaSamw 			break;
207da6c28aaSamw 		case F_CRTIME:
208da6c28aaSamw 			XVA_SET_REQ(&xvattr, XAT_CREATETIME);
209da6c28aaSamw 			break;
210da6c28aaSamw 		case F_FSID:
211da6c28aaSamw 			fsid = (((uint64_t)vp->v_vfsp->vfs_fsid.val[0] << 32) |
212da6c28aaSamw 			    (uint64_t)(vp->v_vfsp->vfs_fsid.val[1] &
213da6c28aaSamw 			    0xffffffff));
214da6c28aaSamw 			VERIFY(nvlist_add_uint64(nvlp, attr_to_name(attr),
215da6c28aaSamw 			    fsid) == 0);
216da6c28aaSamw 			break;
217da6c28aaSamw 		default:
218da6c28aaSamw 			break;
219da6c28aaSamw 		}
220da6c28aaSamw 	}
221da6c28aaSamw 
222da6c28aaSamw 	error = VOP_GETATTR(ppvp, &xvattr.xva_vattr, 0, cr, ct);
223da6c28aaSamw 	if (error)
224da6c28aaSamw 		return (error);
225da6c28aaSamw 
226da6c28aaSamw 	/*
227da6c28aaSamw 	 * Process all the optional attributes together here.  Notice that
228da6c28aaSamw 	 * xoap was set when the optional attribute bits were set above.
229da6c28aaSamw 	 */
230da6c28aaSamw 	if ((xvattr.xva_vattr.va_mask & AT_XVATTR) && xoap) {
231da6c28aaSamw 		if (XVA_ISSET_RTN(&xvattr, XAT_READONLY)) {
232da6c28aaSamw 			VERIFY(nvlist_add_boolean_value(nvlp,
233da6c28aaSamw 			    attr_to_name(F_READONLY),
234da6c28aaSamw 			    xoap->xoa_readonly) == 0);
235da6c28aaSamw 		}
236da6c28aaSamw 		if (XVA_ISSET_RTN(&xvattr, XAT_HIDDEN)) {
237da6c28aaSamw 			VERIFY(nvlist_add_boolean_value(nvlp,
238da6c28aaSamw 			    attr_to_name(F_HIDDEN),
239da6c28aaSamw 			    xoap->xoa_hidden) == 0);
240da6c28aaSamw 		}
241da6c28aaSamw 		if (XVA_ISSET_RTN(&xvattr, XAT_SYSTEM)) {
242da6c28aaSamw 			VERIFY(nvlist_add_boolean_value(nvlp,
243da6c28aaSamw 			    attr_to_name(F_SYSTEM),
244da6c28aaSamw 			    xoap->xoa_system) == 0);
245da6c28aaSamw 		}
246da6c28aaSamw 		if (XVA_ISSET_RTN(&xvattr, XAT_ARCHIVE)) {
247da6c28aaSamw 			VERIFY(nvlist_add_boolean_value(nvlp,
248da6c28aaSamw 			    attr_to_name(F_ARCHIVE),
249da6c28aaSamw 			    xoap->xoa_archive) == 0);
250da6c28aaSamw 		}
251da6c28aaSamw 		if (XVA_ISSET_RTN(&xvattr, XAT_IMMUTABLE)) {
252da6c28aaSamw 			VERIFY(nvlist_add_boolean_value(nvlp,
253da6c28aaSamw 			    attr_to_name(F_IMMUTABLE),
254da6c28aaSamw 			    xoap->xoa_immutable) == 0);
255da6c28aaSamw 		}
256da6c28aaSamw 		if (XVA_ISSET_RTN(&xvattr, XAT_NOUNLINK)) {
257da6c28aaSamw 			VERIFY(nvlist_add_boolean_value(nvlp,
258da6c28aaSamw 			    attr_to_name(F_NOUNLINK),
259da6c28aaSamw 			    xoap->xoa_nounlink) == 0);
260da6c28aaSamw 		}
261da6c28aaSamw 		if (XVA_ISSET_RTN(&xvattr, XAT_APPENDONLY)) {
262da6c28aaSamw 			VERIFY(nvlist_add_boolean_value(nvlp,
263da6c28aaSamw 			    attr_to_name(F_APPENDONLY),
264da6c28aaSamw 			    xoap->xoa_appendonly) == 0);
265da6c28aaSamw 		}
266da6c28aaSamw 		if (XVA_ISSET_RTN(&xvattr, XAT_NODUMP)) {
267da6c28aaSamw 			VERIFY(nvlist_add_boolean_value(nvlp,
268da6c28aaSamw 			    attr_to_name(F_NODUMP),
269da6c28aaSamw 			    xoap->xoa_nodump) == 0);
270da6c28aaSamw 		}
271da6c28aaSamw 		if (XVA_ISSET_RTN(&xvattr, XAT_OPAQUE)) {
272da6c28aaSamw 			VERIFY(nvlist_add_boolean_value(nvlp,
273da6c28aaSamw 			    attr_to_name(F_OPAQUE),
274da6c28aaSamw 			    xoap->xoa_opaque) == 0);
275da6c28aaSamw 		}
276da6c28aaSamw 		if (XVA_ISSET_RTN(&xvattr, XAT_AV_QUARANTINED)) {
277da6c28aaSamw 			VERIFY(nvlist_add_boolean_value(nvlp,
278da6c28aaSamw 			    attr_to_name(F_AV_QUARANTINED),
279da6c28aaSamw 			    xoap->xoa_av_quarantined) == 0);
280da6c28aaSamw 		}
281da6c28aaSamw 		if (XVA_ISSET_RTN(&xvattr, XAT_AV_MODIFIED)) {
282da6c28aaSamw 			VERIFY(nvlist_add_boolean_value(nvlp,
283da6c28aaSamw 			    attr_to_name(F_AV_MODIFIED),
284da6c28aaSamw 			    xoap->xoa_av_modified) == 0);
285da6c28aaSamw 		}
286da6c28aaSamw 		if (XVA_ISSET_RTN(&xvattr, XAT_AV_SCANSTAMP)) {
287da6c28aaSamw 			VERIFY(nvlist_add_uint8_array(nvlp,
288da6c28aaSamw 			    attr_to_name(F_AV_SCANSTAMP),
289da6c28aaSamw 			    xoap->xoa_av_scanstamp,
290da6c28aaSamw 			    sizeof (xoap->xoa_av_scanstamp)) == 0);
291da6c28aaSamw 		}
292da6c28aaSamw 		if (XVA_ISSET_RTN(&xvattr, XAT_CREATETIME)) {
293da6c28aaSamw 			VERIFY(nvlist_add_uint64_array(nvlp,
294da6c28aaSamw 			    attr_to_name(F_CRTIME),
295da6c28aaSamw 			    (uint64_t *)&(xoap->xoa_createtime),
296da6c28aaSamw 			    sizeof (xoap->xoa_createtime) /
297da6c28aaSamw 			    sizeof (uint64_t)) == 0);
298da6c28aaSamw 		}
299da6c28aaSamw 	}
300da6c28aaSamw 	/*
301da6c28aaSamw 	 * Check for optional ownersid/groupsid
302da6c28aaSamw 	 */
303da6c28aaSamw 
304da6c28aaSamw 	if (xvattr.xva_vattr.va_uid > MAXUID) {
305da6c28aaSamw 		nvlist_t *nvl_sid;
306da6c28aaSamw 
307da6c28aaSamw 		if (nvlist_alloc(&nvl_sid, NV_UNIQUE_NAME, KM_SLEEP))
308da6c28aaSamw 			return (ENOMEM);
309da6c28aaSamw 
310*bda89588Sjp151216 		if (kidmap_getsidbyuid(crgetzone(cr), xvattr.xva_vattr.va_uid,
311da6c28aaSamw 		    &domain, &rid) == 0) {
312da6c28aaSamw 			VERIFY(nvlist_add_string(nvl_sid,
313da6c28aaSamw 			    SID_DOMAIN, domain) == 0);
314da6c28aaSamw 			VERIFY(nvlist_add_uint32(nvl_sid, SID_RID, rid) == 0);
315da6c28aaSamw 			VERIFY(nvlist_add_nvlist(nvlp, attr_to_name(F_OWNERSID),
316da6c28aaSamw 			    nvl_sid) == 0);
317da6c28aaSamw 		}
318da6c28aaSamw 		nvlist_free(nvl_sid);
319da6c28aaSamw 	}
320da6c28aaSamw 	if (xvattr.xva_vattr.va_gid > MAXUID) {
321da6c28aaSamw 		nvlist_t *nvl_sid;
322da6c28aaSamw 
323da6c28aaSamw 		if (nvlist_alloc(&nvl_sid, NV_UNIQUE_NAME, KM_SLEEP))
324da6c28aaSamw 			return (ENOMEM);
325da6c28aaSamw 
326*bda89588Sjp151216 		if (kidmap_getsidbygid(crgetzone(cr), xvattr.xva_vattr.va_gid,
327da6c28aaSamw 		    &domain, &rid) == 0) {
328da6c28aaSamw 			VERIFY(nvlist_add_string(nvl_sid,
329da6c28aaSamw 			    SID_DOMAIN, domain) == 0);
330da6c28aaSamw 			VERIFY(nvlist_add_uint32(nvl_sid, SID_RID, rid) == 0);
331da6c28aaSamw 			VERIFY(nvlist_add_nvlist(nvlp, attr_to_name(F_GROUPSID),
332da6c28aaSamw 			    nvl_sid) == 0);
333da6c28aaSamw 		}
334da6c28aaSamw 		nvlist_free(nvl_sid);
335da6c28aaSamw 	}
336da6c28aaSamw 
337da6c28aaSamw 	return (0);
338da6c28aaSamw }
339da6c28aaSamw 
340da6c28aaSamw /*
341da6c28aaSamw  * The size of a sysattr file is the size of the nvlist that will be
342da6c28aaSamw  * returned by xattr_file_read().  A call to xattr_file_write() could
343da6c28aaSamw  * change the size of that nvlist.  That size is not stored persistently
344da6c28aaSamw  * so xattr_fill_nvlist() calls VOP_GETATTR so that it can be calculated.
345da6c28aaSamw  */
346da6c28aaSamw static int
347da6c28aaSamw xattr_file_size(vnode_t *vp, xattr_view_t xattr_view, size_t *size,
348da6c28aaSamw     cred_t *cr, caller_context_t *ct)
349da6c28aaSamw {
350da6c28aaSamw 	nvlist_t *nvl;
351da6c28aaSamw 
352da6c28aaSamw 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP)) {
353da6c28aaSamw 		return (ENOMEM);
354da6c28aaSamw 	}
355da6c28aaSamw 
356da6c28aaSamw 	if (xattr_fill_nvlist(vp, xattr_view, nvl, cr, ct)) {
357da6c28aaSamw 		nvlist_free(nvl);
358da6c28aaSamw 		return (EFAULT);
359da6c28aaSamw 	}
360da6c28aaSamw 
361da6c28aaSamw 	VERIFY(nvlist_size(nvl, size, NV_ENCODE_XDR) == 0);
362da6c28aaSamw 	nvlist_free(nvl);
363da6c28aaSamw 	return (0);
364da6c28aaSamw }
365da6c28aaSamw 
366da6c28aaSamw /* ARGSUSED */
367da6c28aaSamw static int
368da6c28aaSamw xattr_file_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
369da6c28aaSamw     caller_context_t *ct)
370da6c28aaSamw {
371da6c28aaSamw 	xattr_file_t *np = vp->v_data;
372da6c28aaSamw 	timestruc_t now;
373da6c28aaSamw 	size_t size;
374da6c28aaSamw 	int error;
375da6c28aaSamw 	vnode_t *pvp;
376da6c28aaSamw 	vattr_t pvattr;
377da6c28aaSamw 
378da6c28aaSamw 	vap->va_type = VREG;
379da6c28aaSamw 	vap->va_mode = MAKEIMODE(vap->va_type,
380da6c28aaSamw 	    (np->xattr_view == XATTR_VIEW_READONLY ? 0444 : 0644));
381da6c28aaSamw 	vap->va_nodeid = gfs_file_inode(vp);
382da6c28aaSamw 	vap->va_nlink = 1;
383da6c28aaSamw 	pvp = gfs_file_parent(vp);
384da6c28aaSamw 	(void) memset(&pvattr, 0, sizeof (pvattr));
385da6c28aaSamw 	pvattr.va_mask = AT_CTIME|AT_MTIME;
386da6c28aaSamw 	error = VOP_GETATTR(pvp, &pvattr, flags, cr, ct);
387da6c28aaSamw 	if (error) {
388da6c28aaSamw 		return (error);
389da6c28aaSamw 	}
390da6c28aaSamw 	vap->va_ctime = pvattr.va_ctime;
391da6c28aaSamw 	vap->va_mtime = pvattr.va_mtime;
392da6c28aaSamw 	gethrestime(&now);
393da6c28aaSamw 	vap->va_atime = now;
394da6c28aaSamw 	vap->va_uid = 0;
395da6c28aaSamw 	vap->va_gid = 0;
396da6c28aaSamw 	vap->va_rdev = 0;
397da6c28aaSamw 	vap->va_blksize = DEV_BSIZE;
398da6c28aaSamw 	vap->va_seq = 0;
399da6c28aaSamw 	vap->va_fsid = vp->v_vfsp->vfs_dev;
400da6c28aaSamw 	error = xattr_file_size(vp, np->xattr_view, &size, cr, ct);
401da6c28aaSamw 	vap->va_size = size;
402da6c28aaSamw 	vap->va_nblocks = howmany(vap->va_size, vap->va_blksize);
403da6c28aaSamw 	return (error);
404da6c28aaSamw }
405da6c28aaSamw 
406da6c28aaSamw /* ARGSUSED */
407da6c28aaSamw static int
408da6c28aaSamw xattr_file_read(vnode_t *vp, uio_t *uiop, int ioflag, cred_t *cr,
409da6c28aaSamw     caller_context_t *ct)
410da6c28aaSamw {
411da6c28aaSamw 	xattr_file_t *np = vp->v_data;
412da6c28aaSamw 	xattr_view_t xattr_view = np->xattr_view;
413da6c28aaSamw 	char *buf;
414da6c28aaSamw 	size_t filesize;
415da6c28aaSamw 	nvlist_t *nvl;
416da6c28aaSamw 	int error;
417da6c28aaSamw 
418da6c28aaSamw 	/*
419da6c28aaSamw 	 * Validate file offset and fasttrack empty reads
420da6c28aaSamw 	 */
421da6c28aaSamw 	if (uiop->uio_loffset < (offset_t)0)
422da6c28aaSamw 		return (EINVAL);
423da6c28aaSamw 
424da6c28aaSamw 	if (uiop->uio_resid == 0)
425da6c28aaSamw 		return (0);
426da6c28aaSamw 
427da6c28aaSamw 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP))
428da6c28aaSamw 		return (ENOMEM);
429da6c28aaSamw 
430da6c28aaSamw 	if (xattr_fill_nvlist(vp, xattr_view, nvl, cr, ct)) {
431da6c28aaSamw 		nvlist_free(nvl);
432da6c28aaSamw 		return (EFAULT);
433da6c28aaSamw 	}
434da6c28aaSamw 
435da6c28aaSamw 	VERIFY(nvlist_size(nvl, &filesize, NV_ENCODE_XDR) == 0);
436da6c28aaSamw 
437da6c28aaSamw 	if (uiop->uio_loffset >= filesize) {
438da6c28aaSamw 		nvlist_free(nvl);
439da6c28aaSamw 		return (0);
440da6c28aaSamw 	}
441da6c28aaSamw 
442da6c28aaSamw 	buf = kmem_alloc(filesize, KM_SLEEP);
443da6c28aaSamw 	VERIFY(nvlist_pack(nvl, &buf, &filesize, NV_ENCODE_XDR,
444da6c28aaSamw 	    KM_SLEEP) == 0);
445da6c28aaSamw 
446da6c28aaSamw 	error = uiomove((caddr_t)buf, filesize, UIO_READ, uiop);
447da6c28aaSamw 	kmem_free(buf, filesize);
448da6c28aaSamw 	nvlist_free(nvl);
449da6c28aaSamw 	return (error);
450da6c28aaSamw }
451da6c28aaSamw 
452da6c28aaSamw /* ARGSUSED */
453da6c28aaSamw static int
454da6c28aaSamw xattr_file_write(vnode_t *vp, uio_t *uiop, int ioflag, cred_t *cr,
455da6c28aaSamw     caller_context_t *ct)
456da6c28aaSamw {
457da6c28aaSamw 	int error = 0;
458da6c28aaSamw 	char *buf;
459da6c28aaSamw 	char *domain;
460da6c28aaSamw 	uint32_t rid;
461da6c28aaSamw 	ssize_t size = uiop->uio_resid;
462da6c28aaSamw 	nvlist_t *nvp;
463da6c28aaSamw 	nvpair_t *pair = NULL;
464da6c28aaSamw 	vnode_t *ppvp;
465da6c28aaSamw 	xvattr_t xvattr;
466da6c28aaSamw 	xoptattr_t *xoap = NULL;	/* Pointer to optional attributes */
467da6c28aaSamw 
468da6c28aaSamw 	/*
469da6c28aaSamw 	 * Validate file offset and size.
470da6c28aaSamw 	 */
471da6c28aaSamw 	if (uiop->uio_loffset < (offset_t)0)
472da6c28aaSamw 		return (EINVAL);
473da6c28aaSamw 
474da6c28aaSamw 	if (size == 0)
475da6c28aaSamw 		return (EINVAL);
476da6c28aaSamw 
477da6c28aaSamw 	xva_init(&xvattr);
478da6c28aaSamw 
479da6c28aaSamw 	if ((xoap = xva_getxoptattr(&xvattr)) == NULL) {
480da6c28aaSamw 		return (EINVAL);
481da6c28aaSamw 	}
482da6c28aaSamw 
483da6c28aaSamw 	/*
484da6c28aaSamw 	 * Copy and unpack the nvlist
485da6c28aaSamw 	 */
486da6c28aaSamw 	buf = kmem_alloc(size, KM_SLEEP);
487da6c28aaSamw 	if (uiomove((caddr_t)buf, size, UIO_WRITE, uiop)) {
488da6c28aaSamw 		return (EFAULT);
489da6c28aaSamw 	}
490da6c28aaSamw 
491da6c28aaSamw 	if (nvlist_unpack(buf, size, &nvp, KM_SLEEP) != 0) {
492da6c28aaSamw 		kmem_free(buf, size);
493da6c28aaSamw 		uiop->uio_resid = size;
494da6c28aaSamw 		return (EINVAL);
495da6c28aaSamw 	}
496da6c28aaSamw 	kmem_free(buf, size);
497da6c28aaSamw 
498da6c28aaSamw 	/*
499da6c28aaSamw 	 * Fasttrack empty writes (nvlist with no nvpairs)
500da6c28aaSamw 	 */
501da6c28aaSamw 	if (nvlist_next_nvpair(nvp, NULL) == 0)
502da6c28aaSamw 		return (0);
503da6c28aaSamw 
504da6c28aaSamw 	ppvp = gfs_file_parent(gfs_file_parent(vp));
505da6c28aaSamw 
506da6c28aaSamw 	while (pair = nvlist_next_nvpair(nvp, pair)) {
507da6c28aaSamw 		data_type_t type;
508da6c28aaSamw 		f_attr_t attr;
509da6c28aaSamw 		boolean_t value;
510da6c28aaSamw 		uint64_t *time, *times;
511da6c28aaSamw 		uint_t elem, nelems;
512da6c28aaSamw 		nvlist_t *nvp_sid;
513da6c28aaSamw 		uint8_t *scanstamp;
514da6c28aaSamw 
515da6c28aaSamw 		/*
516da6c28aaSamw 		 * Validate the name and type of each attribute.
517da6c28aaSamw 		 * Log any unknown names and continue.  This will
518da6c28aaSamw 		 * help if additional attributes are added later.
519da6c28aaSamw 		 */
520da6c28aaSamw 		type = nvpair_type(pair);
521da6c28aaSamw 		if ((attr = name_to_attr(nvpair_name(pair))) == F_ATTR_INVAL) {
522da6c28aaSamw 			cmn_err(CE_WARN, "Unknown attribute %s",
523da6c28aaSamw 			    nvpair_name(pair));
524da6c28aaSamw 			continue;
525da6c28aaSamw 		}
526da6c28aaSamw 
527da6c28aaSamw 		/*
528da6c28aaSamw 		 * Verify nvlist type matches required type and view is OK
529da6c28aaSamw 		 */
530da6c28aaSamw 
531da6c28aaSamw 		if (type != attr_to_data_type(attr) ||
532da6c28aaSamw 		    (attr_to_xattr_view(attr) == XATTR_VIEW_READONLY)) {
533da6c28aaSamw 			nvlist_free(nvp);
534da6c28aaSamw 			return (EINVAL);
535da6c28aaSamw 		}
536da6c28aaSamw 
537da6c28aaSamw 		/*
538da6c28aaSamw 		 * For OWNERSID/GROUPSID make sure the target
539da6c28aaSamw 		 * file system support ephemeral ID's
540da6c28aaSamw 		 */
541da6c28aaSamw 		if ((attr == F_OWNERSID || attr == F_GROUPSID) &&
542da6c28aaSamw 		    (!(vp->v_vfsp->vfs_flag & VFS_XID))) {
543da6c28aaSamw 			nvlist_free(nvp);
544da6c28aaSamw 			return (EINVAL);
545da6c28aaSamw 		}
546da6c28aaSamw 
547da6c28aaSamw 		/*
548da6c28aaSamw 		 * Retrieve data from nvpair
549da6c28aaSamw 		 */
550da6c28aaSamw 		switch (type) {
551da6c28aaSamw 		case DATA_TYPE_BOOLEAN_VALUE:
552da6c28aaSamw 			if (nvpair_value_boolean_value(pair, &value)) {
553da6c28aaSamw 				nvlist_free(nvp);
554da6c28aaSamw 				return (EINVAL);
555da6c28aaSamw 			}
556da6c28aaSamw 			break;
557da6c28aaSamw 		case DATA_TYPE_UINT64_ARRAY:
558da6c28aaSamw 			if (nvpair_value_uint64_array(pair, &times, &nelems)) {
559da6c28aaSamw 				nvlist_free(nvp);
560da6c28aaSamw 				return (EINVAL);
561da6c28aaSamw 			}
562da6c28aaSamw 			break;
563da6c28aaSamw 		case DATA_TYPE_NVLIST:
564da6c28aaSamw 			if (nvpair_value_nvlist(pair, &nvp_sid)) {
565da6c28aaSamw 				nvlist_free(nvp);
566da6c28aaSamw 				return (EINVAL);
567da6c28aaSamw 			}
568da6c28aaSamw 			break;
569da6c28aaSamw 		case DATA_TYPE_UINT8_ARRAY:
570da6c28aaSamw 			if (nvpair_value_uint8_array(pair,
571da6c28aaSamw 			    &scanstamp, &nelems)) {
572da6c28aaSamw 				nvlist_free(nvp);
573da6c28aaSamw 				return (EINVAL);
574da6c28aaSamw 			}
575da6c28aaSamw 			break;
576da6c28aaSamw 		default:
577da6c28aaSamw 			nvlist_free(nvp);
578da6c28aaSamw 			return (EINVAL);
579da6c28aaSamw 		}
580da6c28aaSamw 
581da6c28aaSamw 		switch (attr) {
582da6c28aaSamw 		/*
583da6c28aaSamw 		 * If we have several similar optional attributes to
584da6c28aaSamw 		 * process then we should do it all together here so that
585da6c28aaSamw 		 * xoap and the requested bitmap can be set in one place.
586da6c28aaSamw 		 */
587da6c28aaSamw 		case F_READONLY:
588da6c28aaSamw 			XVA_SET_REQ(&xvattr, XAT_READONLY);
589da6c28aaSamw 			xoap->xoa_readonly = value;
590da6c28aaSamw 			break;
591da6c28aaSamw 		case F_HIDDEN:
592da6c28aaSamw 			XVA_SET_REQ(&xvattr, XAT_HIDDEN);
593da6c28aaSamw 			xoap->xoa_hidden = value;
594da6c28aaSamw 			break;
595da6c28aaSamw 		case F_SYSTEM:
596da6c28aaSamw 			XVA_SET_REQ(&xvattr, XAT_SYSTEM);
597da6c28aaSamw 			xoap->xoa_system = value;
598da6c28aaSamw 			break;
599da6c28aaSamw 		case F_ARCHIVE:
600da6c28aaSamw 			XVA_SET_REQ(&xvattr, XAT_ARCHIVE);
601da6c28aaSamw 			xoap->xoa_archive = value;
602da6c28aaSamw 			break;
603da6c28aaSamw 		case F_IMMUTABLE:
604da6c28aaSamw 			XVA_SET_REQ(&xvattr, XAT_IMMUTABLE);
605da6c28aaSamw 			xoap->xoa_immutable = value;
606da6c28aaSamw 			break;
607da6c28aaSamw 		case F_NOUNLINK:
608da6c28aaSamw 			XVA_SET_REQ(&xvattr, XAT_NOUNLINK);
609da6c28aaSamw 			xoap->xoa_nounlink = value;
610da6c28aaSamw 			break;
611da6c28aaSamw 		case F_APPENDONLY:
612da6c28aaSamw 			XVA_SET_REQ(&xvattr, XAT_APPENDONLY);
613da6c28aaSamw 			xoap->xoa_appendonly = value;
614da6c28aaSamw 			break;
615da6c28aaSamw 		case F_NODUMP:
616da6c28aaSamw 			XVA_SET_REQ(&xvattr, XAT_NODUMP);
617da6c28aaSamw 			xoap->xoa_nodump = value;
618da6c28aaSamw 			break;
619da6c28aaSamw 		case F_AV_QUARANTINED:
620da6c28aaSamw 			XVA_SET_REQ(&xvattr, XAT_AV_QUARANTINED);
621da6c28aaSamw 			xoap->xoa_av_quarantined = value;
622da6c28aaSamw 			break;
623da6c28aaSamw 		case F_AV_MODIFIED:
624da6c28aaSamw 			XVA_SET_REQ(&xvattr, XAT_AV_MODIFIED);
625da6c28aaSamw 			xoap->xoa_av_modified = value;
626da6c28aaSamw 			break;
627da6c28aaSamw 		case F_CRTIME:
628da6c28aaSamw 			XVA_SET_REQ(&xvattr, XAT_CREATETIME);
629da6c28aaSamw 			time = (uint64_t *)&(xoap->xoa_createtime);
630da6c28aaSamw 			for (elem = 0; elem < nelems; elem++)
631da6c28aaSamw 				*time++ = times[elem];
632da6c28aaSamw 			break;
633da6c28aaSamw 		case F_OWNERSID:
634da6c28aaSamw 		case F_GROUPSID:
635da6c28aaSamw 			if (nvlist_lookup_string(nvp_sid, SID_DOMAIN,
636da6c28aaSamw 			    &domain) || nvlist_lookup_uint32(nvp_sid, SID_RID,
637da6c28aaSamw 			    &rid)) {
638da6c28aaSamw 				nvlist_free(nvp);
639da6c28aaSamw 				return (EINVAL);
640da6c28aaSamw 			}
641da6c28aaSamw 
642da6c28aaSamw 			/*
643da6c28aaSamw 			 * Now map domain+rid to ephemeral id's
644da6c28aaSamw 			 *
645da6c28aaSamw 			 * If mapping fails, then the uid/gid will
646da6c28aaSamw 			 * be set to UID_NOBODY by Winchester.
647da6c28aaSamw 			 */
648da6c28aaSamw 
649da6c28aaSamw 			if (attr == F_OWNERSID) {
650*bda89588Sjp151216 				(void) kidmap_getuidbysid(crgetzone(cr), domain,
651*bda89588Sjp151216 				    rid, &xvattr.xva_vattr.va_uid);
652da6c28aaSamw 				xvattr.xva_vattr.va_mask |= AT_UID;
653da6c28aaSamw 			} else {
654*bda89588Sjp151216 				(void) kidmap_getgidbysid(crgetzone(cr), domain,
655*bda89588Sjp151216 				    rid, &xvattr.xva_vattr.va_gid);
656da6c28aaSamw 				xvattr.xva_vattr.va_mask |= AT_GID;
657da6c28aaSamw 			}
658da6c28aaSamw 			break;
659da6c28aaSamw 		case F_AV_SCANSTAMP:
660da6c28aaSamw 			if (ppvp->v_type == VREG) {
661da6c28aaSamw 				XVA_SET_REQ(&xvattr, XAT_AV_SCANSTAMP);
662da6c28aaSamw 				(void) memcpy(xoap->xoa_av_scanstamp,
663da6c28aaSamw 				    scanstamp, nelems);
664da6c28aaSamw 			} else {
665da6c28aaSamw 				nvlist_free(nvp);
666da6c28aaSamw 				return (EINVAL);
667da6c28aaSamw 			}
668da6c28aaSamw 			break;
669da6c28aaSamw 		default:
670da6c28aaSamw 			break;
671da6c28aaSamw 		}
672da6c28aaSamw 	}
673da6c28aaSamw 
674da6c28aaSamw 	ppvp = gfs_file_parent(gfs_file_parent(vp));
675da6c28aaSamw 	error = VOP_SETATTR(ppvp, &xvattr.xva_vattr, 0, cr, ct);
676da6c28aaSamw 	if (error)
677da6c28aaSamw 		uiop->uio_resid = size;
678da6c28aaSamw 
679da6c28aaSamw 	nvlist_free(nvp);
680da6c28aaSamw 	return (error);
681da6c28aaSamw }
682da6c28aaSamw 
683da6c28aaSamw static int
684da6c28aaSamw xattr_file_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
685da6c28aaSamw     caller_context_t *ct)
686da6c28aaSamw {
687da6c28aaSamw 	switch (cmd) {
688da6c28aaSamw 	case _PC_XATTR_EXISTS:
689da6c28aaSamw 	case _PC_SATTR_ENABLED:
690da6c28aaSamw 	case _PC_SATTR_EXISTS:
691da6c28aaSamw 		*valp = 0;
692da6c28aaSamw 		return (0);
693da6c28aaSamw 	default:
694da6c28aaSamw 		return (fs_pathconf(vp, cmd, valp, cr, ct));
695da6c28aaSamw 	}
696da6c28aaSamw }
697da6c28aaSamw 
698da6c28aaSamw vnodeops_t *xattr_file_ops;
699da6c28aaSamw 
700da6c28aaSamw static const fs_operation_def_t xattr_file_tops[] = {
701da6c28aaSamw 	{ VOPNAME_OPEN,		{ .vop_open = xattr_file_open }		},
702da6c28aaSamw 	{ VOPNAME_CLOSE,	{ .vop_close = xattr_file_close }	},
703da6c28aaSamw 	{ VOPNAME_READ,		{ .vop_read = xattr_file_read }		},
704da6c28aaSamw 	{ VOPNAME_WRITE,	{ .vop_write = xattr_file_write }	},
705da6c28aaSamw 	{ VOPNAME_IOCTL,	{ .error = fs_ioctl }			},
706da6c28aaSamw 	{ VOPNAME_GETATTR,	{ .vop_getattr = xattr_file_getattr }	},
707da6c28aaSamw 	{ VOPNAME_ACCESS,	{ .vop_access = xattr_file_access }	},
708da6c28aaSamw 	{ VOPNAME_READDIR,	{ .error = fs_notdir }			},
709da6c28aaSamw 	{ VOPNAME_SEEK,		{ .vop_seek = fs_seek }			},
710da6c28aaSamw 	{ VOPNAME_INACTIVE,	{ .vop_inactive = gfs_vop_inactive }	},
711da6c28aaSamw 	{ VOPNAME_FID,		{ .vop_fid = xattr_common_fid }		},
712da6c28aaSamw 	{ VOPNAME_PATHCONF,	{ .vop_pathconf = xattr_file_pathconf }	},
713da6c28aaSamw 	{ VOPNAME_PUTPAGE,	{ .error = fs_putpage }			},
714da6c28aaSamw 	{ VOPNAME_FSYNC,	{ .error = fs_fsync }			},
715da6c28aaSamw 	{ NULL }
716da6c28aaSamw };
717da6c28aaSamw 
718da6c28aaSamw vnode_t *
719da6c28aaSamw xattr_mkfile(vnode_t *pvp, xattr_view_t xattr_view)
720da6c28aaSamw {
721da6c28aaSamw 	vnode_t *vp;
722da6c28aaSamw 	xattr_file_t *np;
723da6c28aaSamw 
724da6c28aaSamw 	vp = gfs_file_create(sizeof (xattr_file_t), pvp, xattr_file_ops);
725da6c28aaSamw 	np = vp->v_data;
726da6c28aaSamw 	np->xattr_view = xattr_view;
727da6c28aaSamw 	vp->v_flag |= V_SYSATTR;
728da6c28aaSamw 	return (vp);
729da6c28aaSamw }
730da6c28aaSamw 
731da6c28aaSamw vnode_t *
732da6c28aaSamw xattr_mkfile_ro(vnode_t *pvp)
733da6c28aaSamw {
734da6c28aaSamw 	return (xattr_mkfile(pvp, XATTR_VIEW_READONLY));
735da6c28aaSamw }
736da6c28aaSamw 
737da6c28aaSamw vnode_t *
738da6c28aaSamw xattr_mkfile_rw(vnode_t *pvp)
739da6c28aaSamw {
740da6c28aaSamw 	return (xattr_mkfile(pvp, XATTR_VIEW_READWRITE));
741da6c28aaSamw }
742da6c28aaSamw 
743da6c28aaSamw vnodeops_t *xattr_dir_ops;
744da6c28aaSamw 
745da6c28aaSamw static gfs_dirent_t xattr_dirents[] = {
746da6c28aaSamw 	{ VIEW_READONLY, xattr_mkfile_ro, GFS_CACHE_VNODE, },
747da6c28aaSamw 	{ VIEW_READWRITE, xattr_mkfile_rw, GFS_CACHE_VNODE, },
748da6c28aaSamw 	{ NULL },
749da6c28aaSamw };
750da6c28aaSamw 
751da6c28aaSamw #define	XATTRDIR_NENTS	((sizeof (xattr_dirents) / sizeof (gfs_dirent_t)) - 1)
752da6c28aaSamw 
753da6c28aaSamw static int
754da6c28aaSamw is_sattr_name(char *s)
755da6c28aaSamw {
756da6c28aaSamw 	int i;
757da6c28aaSamw 
758da6c28aaSamw 	for (i = 0; i < XATTRDIR_NENTS; ++i) {
759da6c28aaSamw 		if (strcmp(s, xattr_dirents[i].gfse_name) == 0) {
760da6c28aaSamw 			return (1);
761da6c28aaSamw 		}
762da6c28aaSamw 	}
763da6c28aaSamw 	return (0);
764da6c28aaSamw }
765da6c28aaSamw 
766b38f0970Sck153898 /*
767b38f0970Sck153898  * Given the name of an extended attribute file, determine if there is a
768b38f0970Sck153898  * normalization conflict with a sysattr view name.
769b38f0970Sck153898  */
770b38f0970Sck153898 int
771b38f0970Sck153898 xattr_sysattr_casechk(char *s)
772b38f0970Sck153898 {
773b38f0970Sck153898 	int i;
774b38f0970Sck153898 
775b38f0970Sck153898 	for (i = 0; i < XATTRDIR_NENTS; ++i) {
776b38f0970Sck153898 		if (strcasecmp(s, xattr_dirents[i].gfse_name) == 0)
777b38f0970Sck153898 			return (1);
778b38f0970Sck153898 	}
779b38f0970Sck153898 	return (0);
780b38f0970Sck153898 }
781b38f0970Sck153898 
782da6c28aaSamw static int
783da6c28aaSamw xattr_copy(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm,
784da6c28aaSamw     cred_t *cr, caller_context_t *ct)
785da6c28aaSamw {
786da6c28aaSamw 	xvattr_t xvattr;
787da6c28aaSamw 	vnode_t *pdvp;
788da6c28aaSamw 	int error;
789da6c28aaSamw 
790da6c28aaSamw 	/*
791da6c28aaSamw 	 * Only copy system attrs if the views are the same
792da6c28aaSamw 	 */
793da6c28aaSamw 	if (strcmp(snm, tnm) != 0)
794da6c28aaSamw 		return (EINVAL);
795da6c28aaSamw 
796da6c28aaSamw 	xva_init(&xvattr);
797da6c28aaSamw 
798da6c28aaSamw 	XVA_SET_REQ(&xvattr, XAT_SYSTEM);
799da6c28aaSamw 	XVA_SET_REQ(&xvattr, XAT_READONLY);
800da6c28aaSamw 	XVA_SET_REQ(&xvattr, XAT_HIDDEN);
801da6c28aaSamw 	XVA_SET_REQ(&xvattr, XAT_ARCHIVE);
802da6c28aaSamw 	XVA_SET_REQ(&xvattr, XAT_APPENDONLY);
803da6c28aaSamw 	XVA_SET_REQ(&xvattr, XAT_NOUNLINK);
804da6c28aaSamw 	XVA_SET_REQ(&xvattr, XAT_IMMUTABLE);
805da6c28aaSamw 	XVA_SET_REQ(&xvattr, XAT_NODUMP);
806da6c28aaSamw 	XVA_SET_REQ(&xvattr, XAT_AV_MODIFIED);
807da6c28aaSamw 	XVA_SET_REQ(&xvattr, XAT_AV_QUARANTINED);
808da6c28aaSamw 	XVA_SET_REQ(&xvattr, XAT_CREATETIME);
809da6c28aaSamw 
810da6c28aaSamw 	pdvp = gfs_file_parent(sdvp);
811da6c28aaSamw 	error = VOP_GETATTR(pdvp, &xvattr.xva_vattr, 0, cr, ct);
812da6c28aaSamw 	if (error)
813da6c28aaSamw 		return (error);
814da6c28aaSamw 
815da6c28aaSamw 	pdvp = gfs_file_parent(tdvp);
816da6c28aaSamw 	error = VOP_SETATTR(pdvp, &xvattr.xva_vattr, 0, cr, ct);
817da6c28aaSamw 	return (error);
818da6c28aaSamw }
819da6c28aaSamw 
820da6c28aaSamw static int
821da6c28aaSamw xattr_dir_realdir(vnode_t *dvp, vnode_t **realdvp, int lookup_flags,
822da6c28aaSamw     cred_t *cr, caller_context_t *ct)
823da6c28aaSamw {
824da6c28aaSamw 	vnode_t *pvp;
825da6c28aaSamw 	int error;
826da6c28aaSamw 	struct pathname pn;
827da6c28aaSamw 	char *startnm = "";
828da6c28aaSamw 
829da6c28aaSamw 	*realdvp = NULL;
830da6c28aaSamw 
831da6c28aaSamw 	pvp = gfs_file_parent(dvp);
832da6c28aaSamw 
833da6c28aaSamw 	error = pn_get(startnm, UIO_SYSSPACE, &pn);
834da6c28aaSamw 	if (error) {
835da6c28aaSamw 		VN_RELE(pvp);
836da6c28aaSamw 		return (error);
837da6c28aaSamw 	}
838da6c28aaSamw 
839da6c28aaSamw 	/*
840da6c28aaSamw 	 * Set the LOOKUP_HAVE_SYSATTR_DIR flag so that we don't get into an
841da6c28aaSamw 	 * infinite loop with fop_lookup calling back to xattr_dir_lookup.
842da6c28aaSamw 	 */
843da6c28aaSamw 	lookup_flags |= LOOKUP_HAVE_SYSATTR_DIR;
844da6c28aaSamw 	error = VOP_LOOKUP(pvp, startnm, realdvp, &pn, lookup_flags,
845da6c28aaSamw 	    rootvp, cr, ct, NULL, NULL);
846da6c28aaSamw 	pn_free(&pn);
847da6c28aaSamw 
848da6c28aaSamw 	return (error);
849da6c28aaSamw }
850da6c28aaSamw 
851da6c28aaSamw /* ARGSUSED */
852da6c28aaSamw static int
853da6c28aaSamw xattr_dir_open(vnode_t **vpp, int flags, cred_t *cr, caller_context_t *ct)
854da6c28aaSamw {
855da6c28aaSamw 	if (flags & FWRITE) {
856da6c28aaSamw 		return (EACCES);
857da6c28aaSamw 	}
858da6c28aaSamw 
859da6c28aaSamw 	return (0);
860da6c28aaSamw }
861da6c28aaSamw 
862da6c28aaSamw /* ARGSUSED */
863da6c28aaSamw static int
864da6c28aaSamw xattr_dir_close(vnode_t *vpp, int flags, int count, offset_t off, cred_t *cr,
865da6c28aaSamw     caller_context_t *ct)
866da6c28aaSamw {
867da6c28aaSamw 	return (0);
868da6c28aaSamw }
869da6c28aaSamw 
870da6c28aaSamw /* ARGSUSED */
871da6c28aaSamw static int
872da6c28aaSamw xattr_dir_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
873da6c28aaSamw     caller_context_t *ct)
874da6c28aaSamw {
875da6c28aaSamw 	timestruc_t now;
876da6c28aaSamw 	vnode_t *pvp;
877da6c28aaSamw 	int error;
878da6c28aaSamw 	vattr_t pvattr;
879da6c28aaSamw 
880da6c28aaSamw 	error = xattr_dir_realdir(vp, &pvp, LOOKUP_XATTR, cr, ct);
881da6c28aaSamw 	if (error == 0) {
882da6c28aaSamw 		error = VOP_GETATTR(pvp, vap, 0, cr, ct);
883da6c28aaSamw 		VN_RELE(pvp);
884da6c28aaSamw 		if (error) {
885da6c28aaSamw 			return (error);
886da6c28aaSamw 		}
887da6c28aaSamw 		vap->va_nlink += XATTRDIR_NENTS;
888da6c28aaSamw 		vap->va_size += XATTRDIR_NENTS;
889da6c28aaSamw 		return (0);
890da6c28aaSamw 	}
891da6c28aaSamw 
892da6c28aaSamw 	/*
893da6c28aaSamw 	 * There is no real xattr directory.  Cobble together
894da6c28aaSamw 	 * an entry using info from the parent object.
895da6c28aaSamw 	 */
896da6c28aaSamw 	pvp = gfs_file_parent(vp);
897da6c28aaSamw 	(void) memset(&pvattr, 0, sizeof (pvattr));
898da6c28aaSamw 	pvattr.va_mask = AT_UID|AT_GID|AT_RDEV|AT_CTIME|AT_MTIME;
899da6c28aaSamw 	error = VOP_GETATTR(pvp, &pvattr, 0, cr, ct);
900da6c28aaSamw 	if (error) {
901da6c28aaSamw 		return (error);
902da6c28aaSamw 	}
903da6c28aaSamw 	*vap = pvattr;
904da6c28aaSamw 	vap->va_type = VDIR;
905da6c28aaSamw 	vap->va_mode = MAKEIMODE(vap->va_type, S_ISVTX | 0777);
906da6c28aaSamw 	vap->va_fsid = vp->v_vfsp->vfs_dev;
907da6c28aaSamw 	vap->va_nodeid = gfs_file_inode(vp);
908da6c28aaSamw 	vap->va_nlink = XATTRDIR_NENTS+2;
909da6c28aaSamw 	vap->va_size = vap->va_nlink;
910da6c28aaSamw 	gethrestime(&now);
911da6c28aaSamw 	vap->va_atime = now;
912da6c28aaSamw 	vap->va_blksize = 0;
913da6c28aaSamw 	vap->va_nblocks = 0;
914da6c28aaSamw 	vap->va_seq = 0;
915da6c28aaSamw 	return (0);
916da6c28aaSamw }
917da6c28aaSamw 
918da6c28aaSamw static int
919da6c28aaSamw xattr_dir_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
920da6c28aaSamw     caller_context_t *ct)
921da6c28aaSamw {
922da6c28aaSamw 	vnode_t *realvp;
923da6c28aaSamw 	int error;
924da6c28aaSamw 
925da6c28aaSamw 	/*
926da6c28aaSamw 	 * If there is a real xattr directory, do the setattr there.
927da6c28aaSamw 	 * Otherwise, just return success.  The GFS directory is transient,
928da6c28aaSamw 	 * and any setattr changes can disappear anyway.
929da6c28aaSamw 	 */
930da6c28aaSamw 	error = xattr_dir_realdir(vp, &realvp, LOOKUP_XATTR, cr, ct);
931da6c28aaSamw 	if (error == 0) {
932da6c28aaSamw 		error = VOP_SETATTR(realvp, vap, flags, cr, ct);
933da6c28aaSamw 		VN_RELE(realvp);
934da6c28aaSamw 	}
935da6c28aaSamw 	if (error == ENOENT) {
936da6c28aaSamw 		error = 0;
937da6c28aaSamw 	}
938da6c28aaSamw 	return (error);
939da6c28aaSamw }
940da6c28aaSamw 
941da6c28aaSamw /* ARGSUSED */
942da6c28aaSamw static int
943da6c28aaSamw xattr_dir_access(vnode_t *vp, int mode, int flags, cred_t *cr,
944da6c28aaSamw     caller_context_t *ct)
945da6c28aaSamw {
946da6c28aaSamw 	int error;
947da6c28aaSamw 	vnode_t *realvp = NULL;
948da6c28aaSamw 
949da6c28aaSamw 	if (mode & VWRITE) {
950da6c28aaSamw 		return (EACCES);
951da6c28aaSamw 	}
952da6c28aaSamw 
953da6c28aaSamw 	error = xattr_dir_realdir(vp, &realvp, LOOKUP_XATTR, cr, ct);
954da6c28aaSamw 
955da6c28aaSamw 	if (realvp)
956da6c28aaSamw 		VN_RELE(realvp);
957da6c28aaSamw 
958da6c28aaSamw 	/*
959da6c28aaSamw 	 * No real xattr dir isn't an error
960da6c28aaSamw 	 * an error of EINVAL indicates attributes on attributes
961da6c28aaSamw 	 * are not supported.  In that case just allow access to the
962da6c28aaSamw 	 * transient directory.
963da6c28aaSamw 	 */
964da6c28aaSamw 	return ((error == ENOENT || error == EINVAL) ? 0 : error);
965da6c28aaSamw }
966da6c28aaSamw 
967da6c28aaSamw static int
968da6c28aaSamw xattr_dir_create(vnode_t *dvp, char *name, vattr_t *vap, vcexcl_t excl,
969da6c28aaSamw     int mode, vnode_t **vpp, cred_t *cr, int flag, caller_context_t *ct,
970da6c28aaSamw     vsecattr_t *vsecp)
971da6c28aaSamw {
972da6c28aaSamw 	vnode_t *pvp;
973da6c28aaSamw 	int error;
974da6c28aaSamw 
975da6c28aaSamw 	*vpp = NULL;
976da6c28aaSamw 
977da6c28aaSamw 	/*
978da6c28aaSamw 	 * Don't allow creation of extended attributes with sysattr names.
979da6c28aaSamw 	 */
980da6c28aaSamw 	if (is_sattr_name(name)) {
981da6c28aaSamw 		return (gfs_dir_lookup(dvp, name, vpp, cr));
982da6c28aaSamw 	}
983da6c28aaSamw 
984da6c28aaSamw 	error = xattr_dir_realdir(dvp, &pvp, LOOKUP_XATTR|CREATE_XATTR_DIR,
985da6c28aaSamw 	    cr, ct);
986da6c28aaSamw 	if (error == 0) {
987da6c28aaSamw 		error = VOP_CREATE(pvp, name, vap, excl, mode, vpp, cr, flag,
988da6c28aaSamw 		    ct, vsecp);
989da6c28aaSamw 		VN_RELE(pvp);
990da6c28aaSamw 	}
991da6c28aaSamw 	return (error);
992da6c28aaSamw }
993da6c28aaSamw 
994da6c28aaSamw static int
995da6c28aaSamw xattr_dir_remove(vnode_t *dvp, char *name, cred_t *cr, caller_context_t *ct,
996da6c28aaSamw     int flags)
997da6c28aaSamw {
998da6c28aaSamw 	vnode_t *pvp;
999da6c28aaSamw 	int error;
1000da6c28aaSamw 
1001da6c28aaSamw 	if (is_sattr_name(name)) {
1002da6c28aaSamw 		return (EACCES);
1003da6c28aaSamw 	}
1004da6c28aaSamw 
1005da6c28aaSamw 	error = xattr_dir_realdir(dvp, &pvp, LOOKUP_XATTR, cr, ct);
1006da6c28aaSamw 	if (error == 0) {
1007da6c28aaSamw 		error = VOP_REMOVE(pvp, name, cr, ct, flags);
1008da6c28aaSamw 		VN_RELE(pvp);
1009da6c28aaSamw 	}
1010da6c28aaSamw 	return (error);
1011da6c28aaSamw }
1012da6c28aaSamw 
1013da6c28aaSamw static int
1014da6c28aaSamw xattr_dir_link(vnode_t *tdvp, vnode_t *svp, char *name, cred_t *cr,
1015da6c28aaSamw     caller_context_t *ct, int flags)
1016da6c28aaSamw {
1017da6c28aaSamw 	vnode_t *pvp;
1018da6c28aaSamw 	int error;
1019da6c28aaSamw 
1020da6c28aaSamw 	if (svp->v_flag & V_SYSATTR) {
1021da6c28aaSamw 		return (EINVAL);
1022da6c28aaSamw 	}
1023da6c28aaSamw 
1024da6c28aaSamw 	error = xattr_dir_realdir(tdvp, &pvp, LOOKUP_XATTR, cr, ct);
1025da6c28aaSamw 	if (error == 0) {
1026da6c28aaSamw 		error = VOP_LINK(pvp, svp, name, cr, ct, flags);
1027da6c28aaSamw 		VN_RELE(pvp);
1028da6c28aaSamw 	}
1029da6c28aaSamw 	return (error);
1030da6c28aaSamw }
1031da6c28aaSamw 
1032da6c28aaSamw static int
1033da6c28aaSamw xattr_dir_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm,
1034da6c28aaSamw     cred_t *cr, caller_context_t *ct, int flags)
1035da6c28aaSamw {
1036da6c28aaSamw 	vnode_t *spvp, *tpvp;
1037da6c28aaSamw 	int error;
1038da6c28aaSamw 	int held_tgt;
1039da6c28aaSamw 
1040da6c28aaSamw 	if (is_sattr_name(snm) || is_sattr_name(tnm))
1041da6c28aaSamw 		return (xattr_copy(sdvp, snm, tdvp, tnm, cr, ct));
1042da6c28aaSamw 	/*
1043da6c28aaSamw 	 * We know that sdvp is a GFS dir, or we wouldn't be here.
1044da6c28aaSamw 	 * Get the real unnamed directory.
1045da6c28aaSamw 	 */
1046da6c28aaSamw 	error = xattr_dir_realdir(sdvp, &spvp, LOOKUP_XATTR, cr, ct);
1047da6c28aaSamw 	if (error) {
1048da6c28aaSamw 		return (error);
1049da6c28aaSamw 	}
1050da6c28aaSamw 
1051da6c28aaSamw 	if (sdvp == tdvp) {
1052da6c28aaSamw 		/*
1053da6c28aaSamw 		 * If the source and target are the same GFS directory, the
1054da6c28aaSamw 		 * underlying unnamed source and target dir will be the same.
1055da6c28aaSamw 		 */
1056da6c28aaSamw 		tpvp = spvp;
1057da6c28aaSamw 		VN_HOLD(tpvp);
1058da6c28aaSamw 		held_tgt = 1;
1059da6c28aaSamw 	} else if (tdvp->v_flag & V_SYSATTR) {
1060da6c28aaSamw 		/*
1061da6c28aaSamw 		 * If the target dir is a different GFS directory,
1062da6c28aaSamw 		 * find its underlying unnamed dir.
1063da6c28aaSamw 		 */
1064da6c28aaSamw 		error = xattr_dir_realdir(tdvp, &tpvp, LOOKUP_XATTR, cr, ct);
1065da6c28aaSamw 		if (error) {
1066da6c28aaSamw 			VN_RELE(spvp);
1067da6c28aaSamw 			return (error);
1068da6c28aaSamw 		}
1069da6c28aaSamw 		held_tgt = 1;
1070da6c28aaSamw 	} else {
1071da6c28aaSamw 		/*
1072da6c28aaSamw 		 * Target dir is outside of GFS, pass it on through.
1073da6c28aaSamw 		 */
1074da6c28aaSamw 		tpvp = tdvp;
1075da6c28aaSamw 		held_tgt = 0;
1076da6c28aaSamw 	}
1077da6c28aaSamw 
1078da6c28aaSamw 	error = VOP_RENAME(spvp, snm, tpvp, tnm, cr, ct, flags);
1079da6c28aaSamw 
1080da6c28aaSamw 	if (held_tgt) {
1081da6c28aaSamw 		VN_RELE(tpvp);
1082da6c28aaSamw 	}
1083da6c28aaSamw 	VN_RELE(spvp);
1084da6c28aaSamw 
1085da6c28aaSamw 	return (error);
1086da6c28aaSamw }
1087da6c28aaSamw 
1088b38f0970Sck153898 /*
1089b38f0970Sck153898  * readdir_xattr_casecmp: given a system attribute name, see if there
1090b38f0970Sck153898  * is a real xattr with the same normalized name.
1091b38f0970Sck153898  */
1092b38f0970Sck153898 static int
1093b38f0970Sck153898 readdir_xattr_casecmp(vnode_t *dvp, char *nm, cred_t *cr, caller_context_t *ct,
1094b38f0970Sck153898     int *eflags)
1095b38f0970Sck153898 {
1096b38f0970Sck153898 	int error;
1097b38f0970Sck153898 	vnode_t *vp;
1098b38f0970Sck153898 	struct pathname pn;
1099b38f0970Sck153898 	int flags = FIGNORECASE;
1100b38f0970Sck153898 
1101b38f0970Sck153898 	*eflags = 0;
1102b38f0970Sck153898 
1103b38f0970Sck153898 	error = pn_get(nm, UIO_SYSSPACE, &pn);
1104b38f0970Sck153898 	if (error == 0) {
1105b38f0970Sck153898 		error = VOP_LOOKUP(dvp, nm, &vp, &pn, LOOKUP_XATTR, rootvp,
1106b38f0970Sck153898 		    cr, ct, &flags, NULL);
1107b38f0970Sck153898 		if (error == 0) {
1108b38f0970Sck153898 			*eflags = ED_CASE_CONFLICT;
1109b38f0970Sck153898 			VN_RELE(vp);
1110b38f0970Sck153898 		} else if (error == ENOENT) {
1111b38f0970Sck153898 			error = 0;
1112b38f0970Sck153898 		}
1113b38f0970Sck153898 		pn_free(&pn);
1114b38f0970Sck153898 	}
1115b38f0970Sck153898 
1116b38f0970Sck153898 	return (error);
1117b38f0970Sck153898 }
1118b38f0970Sck153898 
1119da6c28aaSamw static int
1120da6c28aaSamw xattr_dir_readdir(vnode_t *dvp, uio_t *uiop, cred_t *cr, int *eofp,
1121da6c28aaSamw     caller_context_t *ct, int flags)
1122da6c28aaSamw {
1123da6c28aaSamw 	vnode_t *pvp;
1124da6c28aaSamw 	int error;
1125f5f5959bSck153898 	int local_eof;
1126da6c28aaSamw 	int reset_off = 0;
1127da6c28aaSamw 	int has_xattrs = 0;
1128da6c28aaSamw 
1129da6c28aaSamw 	if (eofp == NULL) {
1130da6c28aaSamw 		eofp = &local_eof;
1131da6c28aaSamw 	}
1132f5f5959bSck153898 	*eofp = 0;
1133da6c28aaSamw 
1134da6c28aaSamw 	/*
1135da6c28aaSamw 	 * See if there is a real extended attribute directory.
1136da6c28aaSamw 	 */
1137da6c28aaSamw 	error = xattr_dir_realdir(dvp, &pvp, LOOKUP_XATTR, cr, ct);
1138da6c28aaSamw 	if (error == 0) {
1139da6c28aaSamw 		has_xattrs = 1;
1140da6c28aaSamw 	}
1141da6c28aaSamw 
1142da6c28aaSamw 	/*
1143da6c28aaSamw 	 * Start by reading up the static entries.
1144da6c28aaSamw 	 */
1145da6c28aaSamw 	if (uiop->uio_loffset == 0) {
1146b38f0970Sck153898 		ino64_t pino, ino;
1147b38f0970Sck153898 		offset_t off;
1148b38f0970Sck153898 		gfs_dir_t *dp = dvp->v_data;
1149b38f0970Sck153898 		gfs_readdir_state_t gstate;
1150b38f0970Sck153898 
1151da6c28aaSamw 		if (has_xattrs) {
1152da6c28aaSamw 			/*
1153da6c28aaSamw 			 * If there is a real xattr dir, skip . and ..
1154da6c28aaSamw 			 * in the GFS dir.  We'll pick them up below
1155da6c28aaSamw 			 * when we call into the underlying fs.
1156da6c28aaSamw 			 */
1157da6c28aaSamw 			uiop->uio_loffset = GFS_STATIC_ENTRY_OFFSET;
1158da6c28aaSamw 		}
1159b38f0970Sck153898 		error = gfs_get_parent_ino(dvp, cr, ct, &pino, &ino);
1160b38f0970Sck153898 		if (error == 0) {
1161b38f0970Sck153898 			error = gfs_readdir_init(&gstate, dp->gfsd_maxlen, 1,
1162b38f0970Sck153898 			    uiop, pino, ino, flags);
1163da6c28aaSamw 		}
1164b38f0970Sck153898 		if (error) {
1165b38f0970Sck153898 			if (has_xattrs)
1166b38f0970Sck153898 				VN_RELE(pvp);
1167da6c28aaSamw 			return (error);
1168da6c28aaSamw 		}
1169b38f0970Sck153898 
1170b38f0970Sck153898 		while ((error = gfs_readdir_pred(&gstate, uiop, &off)) == 0 &&
1171b38f0970Sck153898 		    !*eofp) {
1172b38f0970Sck153898 			if (off >= 0 && off < dp->gfsd_nstatic) {
1173b38f0970Sck153898 				int eflags = 0;
1174b38f0970Sck153898 
1175b38f0970Sck153898 				/*
1176b38f0970Sck153898 				 * Check to see if this sysattr set name has a
1177b38f0970Sck153898 				 * case-insensitive conflict with a real xattr
1178b38f0970Sck153898 				 * name.
1179b38f0970Sck153898 				 */
1180b38f0970Sck153898 				if ((flags & V_RDDIR_ENTFLAGS) && has_xattrs) {
1181b38f0970Sck153898 					error = readdir_xattr_casecmp(pvp,
1182b38f0970Sck153898 					    dp->gfsd_static[off].gfse_name,
1183b38f0970Sck153898 					    cr, ct, &eflags);
1184b38f0970Sck153898 					if (error)
1185b38f0970Sck153898 						break;
1186b38f0970Sck153898 				}
1187b38f0970Sck153898 				ino = dp->gfsd_inode(dvp, off);
1188b38f0970Sck153898 
1189b38f0970Sck153898 				error = gfs_readdir_emit(&gstate, uiop, off,
1190b38f0970Sck153898 				    ino, dp->gfsd_static[off].gfse_name,
1191b38f0970Sck153898 				    eflags);
1192b38f0970Sck153898 				if (error)
1193b38f0970Sck153898 					break;
1194b38f0970Sck153898 			} else {
1195b38f0970Sck153898 				*eofp = 1;
1196b38f0970Sck153898 			}
1197b38f0970Sck153898 		}
1198b38f0970Sck153898 
1199b38f0970Sck153898 		error = gfs_readdir_fini(&gstate, error, eofp, *eofp);
1200b38f0970Sck153898 		if (error) {
1201b38f0970Sck153898 			if (has_xattrs)
1202b38f0970Sck153898 				VN_RELE(pvp);
1203b38f0970Sck153898 			return (error);
1204b38f0970Sck153898 		}
1205b38f0970Sck153898 
1206da6c28aaSamw 		/*
1207da6c28aaSamw 		 * We must read all of the static entries in the first
1208da6c28aaSamw 		 * call.  Otherwise we won't know if uio_loffset in a
1209da6c28aaSamw 		 * subsequent call refers to the static entries or to those
1210da6c28aaSamw 		 * in an underlying fs.
1211da6c28aaSamw 		 */
1212da6c28aaSamw 		ASSERT(*eofp);
1213da6c28aaSamw 		reset_off = 1;
1214da6c28aaSamw 	}
1215da6c28aaSamw 
1216da6c28aaSamw 	if (!has_xattrs) {
1217da6c28aaSamw 		*eofp = 1;
1218da6c28aaSamw 		return (0);
1219da6c28aaSamw 	}
1220da6c28aaSamw 
1221da6c28aaSamw 	*eofp = 0;
1222da6c28aaSamw 	if (reset_off) {
1223da6c28aaSamw 		uiop->uio_loffset = 0;
1224da6c28aaSamw 	}
1225da6c28aaSamw 	(void) VOP_RWLOCK(pvp, V_WRITELOCK_FALSE, NULL);
1226da6c28aaSamw 	error = VOP_READDIR(pvp, uiop, cr, eofp, ct, flags);
1227da6c28aaSamw 	VOP_RWUNLOCK(pvp, V_WRITELOCK_FALSE, NULL);
1228da6c28aaSamw 	VN_RELE(pvp);
1229da6c28aaSamw 
1230da6c28aaSamw 	return (error);
1231da6c28aaSamw }
1232da6c28aaSamw 
1233da6c28aaSamw /* ARGSUSED */
1234da6c28aaSamw static void
1235da6c28aaSamw xattr_dir_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
1236da6c28aaSamw {
1237da6c28aaSamw 	gfs_file_t *fp;
1238da6c28aaSamw 
1239da6c28aaSamw 	fp = gfs_dir_inactive(vp);
1240da6c28aaSamw 	if (fp != NULL) {
1241da6c28aaSamw 		kmem_free(fp, fp->gfs_size);
1242da6c28aaSamw 	}
1243da6c28aaSamw }
1244da6c28aaSamw 
1245da6c28aaSamw static int
1246da6c28aaSamw xattr_dir_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
1247da6c28aaSamw     caller_context_t *ct)
1248da6c28aaSamw {
1249da6c28aaSamw 	switch (cmd) {
1250da6c28aaSamw 	case _PC_XATTR_EXISTS:
1251da6c28aaSamw 	case _PC_SATTR_ENABLED:
1252da6c28aaSamw 	case _PC_SATTR_EXISTS:
1253da6c28aaSamw 		*valp = 0;
1254da6c28aaSamw 		return (0);
1255da6c28aaSamw 	default:
1256da6c28aaSamw 		return (fs_pathconf(vp, cmd, valp, cr, ct));
1257da6c28aaSamw 	}
1258da6c28aaSamw }
1259da6c28aaSamw 
1260da6c28aaSamw static const fs_operation_def_t xattr_dir_tops[] = {
1261da6c28aaSamw 	{ VOPNAME_OPEN,		{ .vop_open = xattr_dir_open }		},
1262da6c28aaSamw 	{ VOPNAME_CLOSE,	{ .vop_close = xattr_dir_close }	},
1263da6c28aaSamw 	{ VOPNAME_IOCTL,	{ .error = fs_inval }			},
1264da6c28aaSamw 	{ VOPNAME_GETATTR,	{ .vop_getattr = xattr_dir_getattr }	},
1265da6c28aaSamw 	{ VOPNAME_SETATTR,	{ .vop_setattr = xattr_dir_setattr }	},
1266da6c28aaSamw 	{ VOPNAME_ACCESS,	{ .vop_access = xattr_dir_access }	},
1267da6c28aaSamw 	{ VOPNAME_READDIR,	{ .vop_readdir = xattr_dir_readdir }	},
1268da6c28aaSamw 	{ VOPNAME_LOOKUP,	{ .vop_lookup = gfs_vop_lookup }	},
1269da6c28aaSamw 	{ VOPNAME_CREATE,	{ .vop_create = xattr_dir_create }	},
1270da6c28aaSamw 	{ VOPNAME_REMOVE,	{ .vop_remove = xattr_dir_remove }	},
1271da6c28aaSamw 	{ VOPNAME_LINK,		{ .vop_link = xattr_dir_link }		},
1272da6c28aaSamw 	{ VOPNAME_RENAME,	{ .vop_rename = xattr_dir_rename }	},
1273da6c28aaSamw 	{ VOPNAME_MKDIR,	{ .error = fs_inval }			},
1274da6c28aaSamw 	{ VOPNAME_SEEK,		{ .vop_seek = fs_seek }			},
1275da6c28aaSamw 	{ VOPNAME_INACTIVE,	{ .vop_inactive = xattr_dir_inactive }	},
1276da6c28aaSamw 	{ VOPNAME_FID,		{ .vop_fid = xattr_common_fid }		},
1277da6c28aaSamw 	{ VOPNAME_PATHCONF,	{ .vop_pathconf = xattr_dir_pathconf }	},
1278da6c28aaSamw 	{ NULL, NULL }
1279da6c28aaSamw };
1280da6c28aaSamw 
1281da6c28aaSamw static gfs_opsvec_t xattr_opsvec[] = {
1282da6c28aaSamw 	{ "xattr dir", xattr_dir_tops, &xattr_dir_ops },
1283da6c28aaSamw 	{ "system attributes", xattr_file_tops, &xattr_file_ops },
1284da6c28aaSamw 	{ NULL, NULL, NULL }
1285da6c28aaSamw };
1286da6c28aaSamw 
1287da6c28aaSamw static int
1288da6c28aaSamw xattr_lookup_cb(vnode_t *vp, const char *nm, vnode_t **vpp, ino64_t *inop,
1289da6c28aaSamw     cred_t *cr)
1290da6c28aaSamw {
1291da6c28aaSamw 	vnode_t *pvp;
1292da6c28aaSamw 	struct pathname pn;
1293da6c28aaSamw 	int error;
1294da6c28aaSamw 
1295da6c28aaSamw 	*vpp = NULL;
1296da6c28aaSamw 	*inop = 0;
1297da6c28aaSamw 
1298da6c28aaSamw 	error = xattr_dir_realdir(vp, &pvp, LOOKUP_XATTR|CREATE_XATTR_DIR,
1299da6c28aaSamw 	    cr, NULL);
1300da6c28aaSamw 
1301da6c28aaSamw 	/*
1302da6c28aaSamw 	 * Return ENOENT for EACCES requests during lookup.  Once an
1303da6c28aaSamw 	 * attribute create is attempted EACCES will be returned.
1304da6c28aaSamw 	 */
1305da6c28aaSamw 	if (error) {
1306da6c28aaSamw 		if (error == EACCES)
1307da6c28aaSamw 			return (ENOENT);
1308da6c28aaSamw 		return (error);
1309da6c28aaSamw 	}
1310da6c28aaSamw 
1311da6c28aaSamw 	error = pn_get((char *)nm, UIO_SYSSPACE, &pn);
1312da6c28aaSamw 	if (error == 0) {
1313da6c28aaSamw 		error = VOP_LOOKUP(pvp, (char *)nm, vpp, &pn, 0, rootvp,
1314da6c28aaSamw 		    cr, NULL, NULL, NULL);
1315da6c28aaSamw 		pn_free(&pn);
1316da6c28aaSamw 	}
1317da6c28aaSamw 	VN_RELE(pvp);
1318da6c28aaSamw 
1319da6c28aaSamw 	return (error);
1320da6c28aaSamw }
1321da6c28aaSamw 
1322da6c28aaSamw /* ARGSUSED */
1323da6c28aaSamw static ino64_t
1324da6c28aaSamw xattrdir_do_ino(vnode_t *vp, int index)
1325da6c28aaSamw {
1326da6c28aaSamw 	/*
1327da6c28aaSamw 	 * We use index 0 for the directory fid.  Start
1328da6c28aaSamw 	 * the file numbering at 1.
1329da6c28aaSamw 	 */
1330da6c28aaSamw 	return ((ino64_t)index+1);
1331da6c28aaSamw }
1332da6c28aaSamw 
1333da6c28aaSamw void
1334da6c28aaSamw xattr_init(void)
1335da6c28aaSamw {
1336da6c28aaSamw 	VERIFY(gfs_make_opsvec(xattr_opsvec) == 0);
1337da6c28aaSamw }
1338da6c28aaSamw 
1339da6c28aaSamw int
1340da6c28aaSamw xattr_dir_lookup(vnode_t *dvp, vnode_t **vpp, int flags, cred_t *cr)
1341da6c28aaSamw {
1342da6c28aaSamw 	int error = 0;
1343da6c28aaSamw 
1344da6c28aaSamw 	*vpp = NULL;
1345da6c28aaSamw 
1346da6c28aaSamw 	if (dvp->v_type != VDIR && dvp->v_type != VREG)
1347da6c28aaSamw 		return (EINVAL);
1348da6c28aaSamw 
1349da6c28aaSamw 	mutex_enter(&dvp->v_lock);
1350da6c28aaSamw 
1351da6c28aaSamw 	/*
1352da6c28aaSamw 	 * If we're already in sysattr space, don't allow creation
1353da6c28aaSamw 	 * of another level of sysattrs.
1354da6c28aaSamw 	 */
1355da6c28aaSamw 	if (dvp->v_flag & V_SYSATTR) {
1356da6c28aaSamw 		mutex_exit(&dvp->v_lock);
1357da6c28aaSamw 		return (EINVAL);
1358da6c28aaSamw 	}
1359da6c28aaSamw 
1360da6c28aaSamw 	if (dvp->v_xattrdir != NULL) {
1361da6c28aaSamw 		*vpp = dvp->v_xattrdir;
1362da6c28aaSamw 		VN_HOLD(*vpp);
1363da6c28aaSamw 	} else {
1364da6c28aaSamw 		ulong_t val;
1365da6c28aaSamw 		int xattrs_allowed = dvp->v_vfsp->vfs_flag & VFS_XATTR;
1366da6c28aaSamw 		int sysattrs_allowed = 1;
1367da6c28aaSamw 
1368da6c28aaSamw 		/*
1369da6c28aaSamw 		 * We have to drop the lock on dvp.  gfs_dir_create will
1370da6c28aaSamw 		 * grab it for a VN_HOLD.
1371da6c28aaSamw 		 */
1372da6c28aaSamw 		mutex_exit(&dvp->v_lock);
1373da6c28aaSamw 
1374da6c28aaSamw 		/*
1375da6c28aaSamw 		 * If dvp allows xattr creation, but not sysattr
1376da6c28aaSamw 		 * creation, return the real xattr dir vp. We can't
1377da6c28aaSamw 		 * use the vfs feature mask here because _PC_SATTR_ENABLED
1378da6c28aaSamw 		 * has vnode-level granularity (e.g. .zfs).
1379da6c28aaSamw 		 */
1380da6c28aaSamw 		error = VOP_PATHCONF(dvp, _PC_SATTR_ENABLED, &val, cr, NULL);
1381da6c28aaSamw 		if (error != 0 || val == 0)
1382da6c28aaSamw 			sysattrs_allowed = 0;
1383da6c28aaSamw 
1384da6c28aaSamw 		if (!xattrs_allowed && !sysattrs_allowed)
1385da6c28aaSamw 			return (EINVAL);
1386da6c28aaSamw 
1387da6c28aaSamw 		if (!sysattrs_allowed) {
1388da6c28aaSamw 			struct pathname pn;
1389da6c28aaSamw 			char *nm = "";
1390da6c28aaSamw 
1391da6c28aaSamw 			error = pn_get(nm, UIO_SYSSPACE, &pn);
1392da6c28aaSamw 			if (error)
1393da6c28aaSamw 				return (error);
1394da6c28aaSamw 			error = VOP_LOOKUP(dvp, nm, vpp, &pn,
1395da6c28aaSamw 			    flags|LOOKUP_HAVE_SYSATTR_DIR, rootvp, cr, NULL,
1396da6c28aaSamw 			    NULL, NULL);
1397da6c28aaSamw 			pn_free(&pn);
1398da6c28aaSamw 			return (error);
1399da6c28aaSamw 		}
1400da6c28aaSamw 
1401da6c28aaSamw 		/*
1402da6c28aaSamw 		 * Note that we act as if we were given CREATE_XATTR_DIR,
1403da6c28aaSamw 		 * but only for creation of the GFS directory.
1404da6c28aaSamw 		 */
1405da6c28aaSamw 		*vpp = gfs_dir_create(
1406da6c28aaSamw 		    sizeof (gfs_dir_t), dvp, xattr_dir_ops, xattr_dirents,
1407da6c28aaSamw 		    xattrdir_do_ino, MAXNAMELEN, NULL, xattr_lookup_cb);
1408da6c28aaSamw 		mutex_enter(&dvp->v_lock);
1409da6c28aaSamw 		if (dvp->v_xattrdir != NULL) {
1410da6c28aaSamw 			/*
1411da6c28aaSamw 			 * We lost the race to create the xattr dir.
1412da6c28aaSamw 			 * Destroy this one, use the winner.  We can't
1413da6c28aaSamw 			 * just call VN_RELE(*vpp), because the vnode
1414da6c28aaSamw 			 * is only partially initialized.
1415da6c28aaSamw 			 */
1416da6c28aaSamw 			gfs_dir_t *dp = (*vpp)->v_data;
1417da6c28aaSamw 
1418da6c28aaSamw 			ASSERT((*vpp)->v_count == 1);
1419da6c28aaSamw 			vn_free(*vpp);
1420da6c28aaSamw 
1421da6c28aaSamw 			mutex_destroy(&dp->gfsd_lock);
1422da6c28aaSamw 			kmem_free(dp->gfsd_static,
1423da6c28aaSamw 			    dp->gfsd_nstatic * sizeof (gfs_dirent_t));
1424da6c28aaSamw 			kmem_free(dp, dp->gfsd_file.gfs_size);
1425da6c28aaSamw 
1426da6c28aaSamw 			/*
1427da6c28aaSamw 			 * There is an implied VN_HOLD(dvp) here.  We should
1428da6c28aaSamw 			 * be doing a VN_RELE(dvp) to clean up the reference
1429da6c28aaSamw 			 * from *vpp, and then a VN_HOLD(dvp) for the new
1430da6c28aaSamw 			 * reference.  Instead, we just leave the count alone.
1431da6c28aaSamw 			 */
1432da6c28aaSamw 
1433da6c28aaSamw 			*vpp = dvp->v_xattrdir;
1434da6c28aaSamw 			VN_HOLD(*vpp);
1435da6c28aaSamw 		} else {
1436da6c28aaSamw 			(*vpp)->v_flag |= (V_XATTRDIR|V_SYSATTR);
1437da6c28aaSamw 			dvp->v_xattrdir = *vpp;
1438da6c28aaSamw 		}
1439da6c28aaSamw 	}
1440da6c28aaSamw 	mutex_exit(&dvp->v_lock);
1441da6c28aaSamw 
1442da6c28aaSamw 	return (error);
1443da6c28aaSamw }
1444da6c28aaSamw 
1445da6c28aaSamw int
1446da6c28aaSamw xattr_dir_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp)
1447da6c28aaSamw {
1448da6c28aaSamw 	int error;
1449da6c28aaSamw 	vnode_t *pvp, *dvp;
1450da6c28aaSamw 	xattr_fid_t *xfidp;
1451da6c28aaSamw 	struct pathname pn;
1452da6c28aaSamw 	char *nm;
1453da6c28aaSamw 	uint16_t orig_len;
1454da6c28aaSamw 
1455da6c28aaSamw 	*vpp = NULL;
1456da6c28aaSamw 
1457da6c28aaSamw 	if (fidp->fid_len < XATTR_FIDSZ)
1458da6c28aaSamw 		return (EINVAL);
1459da6c28aaSamw 
1460da6c28aaSamw 	xfidp = (xattr_fid_t *)fidp;
1461da6c28aaSamw 	orig_len = fidp->fid_len;
1462da6c28aaSamw 	fidp->fid_len = xfidp->parent_len;
1463da6c28aaSamw 
1464da6c28aaSamw 	error = VFS_VGET(vfsp, &pvp, fidp);
1465da6c28aaSamw 	fidp->fid_len = orig_len;
1466da6c28aaSamw 	if (error)
1467da6c28aaSamw 		return (error);
1468da6c28aaSamw 
1469da6c28aaSamw 	/*
1470da6c28aaSamw 	 * Start by getting the GFS sysattr directory.	We might need
1471da6c28aaSamw 	 * to recreate it during the VOP_LOOKUP.
1472da6c28aaSamw 	 */
1473da6c28aaSamw 	nm = "";
1474da6c28aaSamw 	error = pn_get(nm, UIO_SYSSPACE, &pn);
1475da6c28aaSamw 	if (error) {
1476da6c28aaSamw 		VN_RELE(pvp);
1477da6c28aaSamw 		return (EINVAL);
1478da6c28aaSamw 	}
1479da6c28aaSamw 
1480da6c28aaSamw 	error = VOP_LOOKUP(pvp, nm, &dvp, &pn, LOOKUP_XATTR|CREATE_XATTR_DIR,
1481da6c28aaSamw 	    rootvp, CRED(), NULL, NULL, NULL);
1482da6c28aaSamw 	pn_free(&pn);
1483da6c28aaSamw 	VN_RELE(pvp);
1484da6c28aaSamw 	if (error)
1485da6c28aaSamw 		return (error);
1486da6c28aaSamw 
1487da6c28aaSamw 	if (xfidp->dir_offset == 0) {
1488da6c28aaSamw 		/*
1489da6c28aaSamw 		 * If we were looking for the directory, we're done.
1490da6c28aaSamw 		 */
1491da6c28aaSamw 		*vpp = dvp;
1492da6c28aaSamw 		return (0);
1493da6c28aaSamw 	}
1494da6c28aaSamw 
1495da6c28aaSamw 	if (xfidp->dir_offset > XATTRDIR_NENTS) {
1496da6c28aaSamw 		VN_RELE(dvp);
1497da6c28aaSamw 		return (EINVAL);
1498da6c28aaSamw 	}
1499da6c28aaSamw 
1500da6c28aaSamw 	nm = xattr_dirents[xfidp->dir_offset - 1].gfse_name;
1501da6c28aaSamw 
1502da6c28aaSamw 	error = pn_get(nm, UIO_SYSSPACE, &pn);
1503da6c28aaSamw 	if (error) {
1504da6c28aaSamw 		VN_RELE(dvp);
1505da6c28aaSamw 		return (EINVAL);
1506da6c28aaSamw 	}
1507da6c28aaSamw 
1508da6c28aaSamw 	error = VOP_LOOKUP(dvp, nm, vpp, &pn, 0, rootvp, CRED(), NULL,
1509da6c28aaSamw 	    NULL, NULL);
1510da6c28aaSamw 
1511da6c28aaSamw 	pn_free(&pn);
1512da6c28aaSamw 	VN_RELE(dvp);
1513da6c28aaSamw 
1514da6c28aaSamw 	return (error);
1515da6c28aaSamw }
1516