xref: /titanic_44/usr/src/uts/common/fs/gfs.c (revision 60946fe0e0cab23f683e9de92451aa45b7913135)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5092e3d7cSjk115741  * Common Development and Distribution License (the "License").
6092e3d7cSjk115741  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21092e3d7cSjk115741 /* Portions Copyright 2007 Shivakumar GN */
227c478bd9Sstevel@tonic-gate /*
23*60946fe0Smec  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
317c478bd9Sstevel@tonic-gate #include <sys/debug.h>
327c478bd9Sstevel@tonic-gate #include <sys/dirent.h>
337c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
347c478bd9Sstevel@tonic-gate #include <sys/mman.h>
357c478bd9Sstevel@tonic-gate #include <sys/mutex.h>
367c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
377c478bd9Sstevel@tonic-gate #include <sys/systm.h>
387c478bd9Sstevel@tonic-gate #include <sys/uio.h>
397c478bd9Sstevel@tonic-gate #include <sys/vmsystm.h>
407c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
417c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #include <vm/as.h>
447c478bd9Sstevel@tonic-gate #include <vm/seg_vn.h>
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate #include <sys/gfs.h>
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate /*
497c478bd9Sstevel@tonic-gate  * Generic pseudo-filesystem routines.
507c478bd9Sstevel@tonic-gate  *
517c478bd9Sstevel@tonic-gate  * There are significant similarities between the implementation of certain file
527c478bd9Sstevel@tonic-gate  * system entry points across different filesystems.  While one could attempt to
537c478bd9Sstevel@tonic-gate  * "choke up on the bat" and incorporate common functionality into a VOP
54092e3d7cSjk115741  * preamble or postamble, such an approach is limited in the benefit it can
557c478bd9Sstevel@tonic-gate  * provide.  In this file we instead define a toolkit of routines which can be
567c478bd9Sstevel@tonic-gate  * called from a filesystem (with in-kernel pseudo-filesystems being the focus
577c478bd9Sstevel@tonic-gate  * of the exercise) in a more component-like fashion.
587c478bd9Sstevel@tonic-gate  *
597c478bd9Sstevel@tonic-gate  * There are three basic classes of routines:
607c478bd9Sstevel@tonic-gate  *
617c478bd9Sstevel@tonic-gate  * 1) Lowlevel support routines
627c478bd9Sstevel@tonic-gate  *
637c478bd9Sstevel@tonic-gate  *    These routines are designed to play a support role for existing
647c478bd9Sstevel@tonic-gate  *    pseudo-filesystems (such as procfs).  They simplify common tasks,
65da6c28aaSamw  *    without forcing the filesystem to hand over management to GFS.  The
667c478bd9Sstevel@tonic-gate  *    routines covered are:
677c478bd9Sstevel@tonic-gate  *
687c478bd9Sstevel@tonic-gate  *	gfs_readdir_init()
697c478bd9Sstevel@tonic-gate  *	gfs_readdir_emit()
707c478bd9Sstevel@tonic-gate  *	gfs_readdir_emitn()
717c478bd9Sstevel@tonic-gate  *	gfs_readdir_pred()
727c478bd9Sstevel@tonic-gate  *	gfs_readdir_fini()
737c478bd9Sstevel@tonic-gate  *	gfs_lookup_dot()
747c478bd9Sstevel@tonic-gate  *
757c478bd9Sstevel@tonic-gate  * 2) Complete GFS management
767c478bd9Sstevel@tonic-gate  *
777c478bd9Sstevel@tonic-gate  *    These routines take a more active role in management of the
787c478bd9Sstevel@tonic-gate  *    pseudo-filesystem.  They handle the relationship between vnode private
797c478bd9Sstevel@tonic-gate  *    data and VFS data, as well as the relationship between vnodes in the
80092e3d7cSjk115741  *    directory hierarchy.
817c478bd9Sstevel@tonic-gate  *
827c478bd9Sstevel@tonic-gate  *    In order to use these interfaces, the first member of every private
837c478bd9Sstevel@tonic-gate  *    v_data must be a gfs_file_t or a gfs_dir_t.  This hands over all control
847c478bd9Sstevel@tonic-gate  *    to GFS.
857c478bd9Sstevel@tonic-gate  *
867c478bd9Sstevel@tonic-gate  * 	gfs_file_create()
877c478bd9Sstevel@tonic-gate  * 	gfs_dir_create()
887c478bd9Sstevel@tonic-gate  * 	gfs_root_create()
897c478bd9Sstevel@tonic-gate  *
907c478bd9Sstevel@tonic-gate  *	gfs_file_inactive()
917c478bd9Sstevel@tonic-gate  *	gfs_dir_inactive()
927c478bd9Sstevel@tonic-gate  *	gfs_dir_lookup()
937c478bd9Sstevel@tonic-gate  *	gfs_dir_readdir()
947c478bd9Sstevel@tonic-gate  *
957c478bd9Sstevel@tonic-gate  * 	gfs_vop_inactive()
967c478bd9Sstevel@tonic-gate  * 	gfs_vop_lookup()
977c478bd9Sstevel@tonic-gate  * 	gfs_vop_readdir()
987c478bd9Sstevel@tonic-gate  * 	gfs_vop_map()
99a237e38eSth199096  *
100a237e38eSth199096  * 3) Single File pseudo-filesystems
101a237e38eSth199096  *
102a237e38eSth199096  *    This routine creates a rooted file to be overlayed ontop of another
103a237e38eSth199096  *    file in the physical filespace.
104a237e38eSth199096  *
105a237e38eSth199096  *    Note that the parent is NULL (actually the vfs), but there is nothing
106a237e38eSth199096  *    technically keeping such a file from utilizing the "Complete GFS
107a237e38eSth199096  *    management" set of routines.
108a237e38eSth199096  *
109a237e38eSth199096  * 	gfs_root_create_file()
1107c478bd9Sstevel@tonic-gate  */
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate /*
1137c478bd9Sstevel@tonic-gate  * gfs_make_opsvec: take an array of vnode type definitions and create
1147c478bd9Sstevel@tonic-gate  * their vnodeops_t structures
1157c478bd9Sstevel@tonic-gate  *
1167c478bd9Sstevel@tonic-gate  * This routine takes an array of gfs_opsvec_t's.  It could
1177c478bd9Sstevel@tonic-gate  * alternatively take an array of gfs_opsvec_t*'s, which would allow
1187c478bd9Sstevel@tonic-gate  * vnode types to be completely defined in files external to the caller
1197c478bd9Sstevel@tonic-gate  * of gfs_make_opsvec().  As it stands, much more sharing takes place --
1207c478bd9Sstevel@tonic-gate  * both the caller and the vnode type provider need to access gfsv_ops
1217c478bd9Sstevel@tonic-gate  * and gfsv_template, and the caller also needs to know gfsv_name.
1227c478bd9Sstevel@tonic-gate  */
1237c478bd9Sstevel@tonic-gate int
1247c478bd9Sstevel@tonic-gate gfs_make_opsvec(gfs_opsvec_t *vec)
1257c478bd9Sstevel@tonic-gate {
1267c478bd9Sstevel@tonic-gate 	int error, i;
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	for (i = 0; ; i++) {
1297c478bd9Sstevel@tonic-gate 		if (vec[i].gfsv_name == NULL)
1307c478bd9Sstevel@tonic-gate 			return (0);
1317c478bd9Sstevel@tonic-gate 		error = vn_make_ops(vec[i].gfsv_name, vec[i].gfsv_template,
1327c478bd9Sstevel@tonic-gate 		    vec[i].gfsv_ops);
1337c478bd9Sstevel@tonic-gate 		if (error)
1347c478bd9Sstevel@tonic-gate 			break;
1357c478bd9Sstevel@tonic-gate 	}
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	cmn_err(CE_WARN, "gfs_make_opsvec: bad vnode ops template for '%s'",
1387c478bd9Sstevel@tonic-gate 	    vec[i].gfsv_name);
1397c478bd9Sstevel@tonic-gate 	for (i--; i >= 0; i--) {
1407c478bd9Sstevel@tonic-gate 		vn_freevnodeops(*vec[i].gfsv_ops);
1417c478bd9Sstevel@tonic-gate 		*vec[i].gfsv_ops = NULL;
1427c478bd9Sstevel@tonic-gate 	}
1437c478bd9Sstevel@tonic-gate 	return (error);
1447c478bd9Sstevel@tonic-gate }
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate /*
1477c478bd9Sstevel@tonic-gate  * Low level directory routines
1487c478bd9Sstevel@tonic-gate  *
1497c478bd9Sstevel@tonic-gate  * These routines provide some simple abstractions for reading directories.
1507c478bd9Sstevel@tonic-gate  * They are designed to be used by existing pseudo filesystems (namely procfs)
1517c478bd9Sstevel@tonic-gate  * that already have a complicated management infrastructure.
1527c478bd9Sstevel@tonic-gate  */
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate /*
155b38f0970Sck153898  * gfs_get_parent_ino: used to obtain a parent inode number and the
156b38f0970Sck153898  * inode number of the given vnode in preparation for calling gfs_readdir_init.
157b38f0970Sck153898  */
158b38f0970Sck153898 int
159b38f0970Sck153898 gfs_get_parent_ino(vnode_t *dvp, cred_t *cr, caller_context_t *ct,
160b38f0970Sck153898     ino64_t *pino, ino64_t *ino)
161b38f0970Sck153898 {
162b38f0970Sck153898 	vnode_t *parent;
163b38f0970Sck153898 	gfs_dir_t *dp = dvp->v_data;
164b38f0970Sck153898 	int error;
165b38f0970Sck153898 
166b38f0970Sck153898 	*ino = dp->gfsd_file.gfs_ino;
167b38f0970Sck153898 	parent = dp->gfsd_file.gfs_parent;
168b38f0970Sck153898 
169b38f0970Sck153898 	if (parent == NULL) {
170b38f0970Sck153898 		*pino = *ino;		/* root of filesystem */
171b38f0970Sck153898 	} else if (dvp->v_flag & V_XATTRDIR) {
172b38f0970Sck153898 		vattr_t va;
173b38f0970Sck153898 
174b38f0970Sck153898 		va.va_mask = AT_NODEID;
175b38f0970Sck153898 		error = VOP_GETATTR(parent, &va, 0, cr, ct);
176b38f0970Sck153898 		if (error)
177b38f0970Sck153898 			return (error);
178b38f0970Sck153898 		*pino = va.va_nodeid;
179b38f0970Sck153898 	} else {
180b38f0970Sck153898 		*pino = ((gfs_file_t *)(parent->v_data))->gfs_ino;
181b38f0970Sck153898 	}
182b38f0970Sck153898 
183b38f0970Sck153898 	return (0);
184b38f0970Sck153898 }
185b38f0970Sck153898 
186b38f0970Sck153898 /*
1877c478bd9Sstevel@tonic-gate  * gfs_readdir_init: initiate a generic readdir
1887c478bd9Sstevel@tonic-gate  *   st		- a pointer to an uninitialized gfs_readdir_state_t structure
1897c478bd9Sstevel@tonic-gate  *   name_max	- the directory's maximum file name length
1907c478bd9Sstevel@tonic-gate  *   ureclen	- the exported file-space record length (1 for non-legacy FSs)
1917c478bd9Sstevel@tonic-gate  *   uiop	- the uiop passed to readdir
1927c478bd9Sstevel@tonic-gate  *   parent	- the parent directory's inode
1937c478bd9Sstevel@tonic-gate  *   self	- this directory's inode
194b38f0970Sck153898  *   flags	- flags from VOP_READDIR
1957c478bd9Sstevel@tonic-gate  *
1967c478bd9Sstevel@tonic-gate  * Returns 0 or a non-zero errno.
1977c478bd9Sstevel@tonic-gate  *
1987c478bd9Sstevel@tonic-gate  * Typical VOP_READDIR usage of gfs_readdir_*:
1997c478bd9Sstevel@tonic-gate  *
2007c478bd9Sstevel@tonic-gate  *	if ((error = gfs_readdir_init(...)) != 0)
2017c478bd9Sstevel@tonic-gate  *		return (error);
2027c478bd9Sstevel@tonic-gate  *	eof = 0;
2037c478bd9Sstevel@tonic-gate  *	while ((error = gfs_readdir_pred(..., &voffset)) != 0) {
2047c478bd9Sstevel@tonic-gate  *		if (!consumer_entry_at(voffset))
2057c478bd9Sstevel@tonic-gate  *			voffset = consumer_next_entry(voffset);
2067c478bd9Sstevel@tonic-gate  *		if (consumer_eof(voffset)) {
2077c478bd9Sstevel@tonic-gate  *			eof = 1
2087c478bd9Sstevel@tonic-gate  *			break;
2097c478bd9Sstevel@tonic-gate  *		}
2107c478bd9Sstevel@tonic-gate  *		if ((error = gfs_readdir_emit(..., voffset,
2117c478bd9Sstevel@tonic-gate  *		    consumer_ino(voffset), consumer_name(voffset))) != 0)
2127c478bd9Sstevel@tonic-gate  *			break;
2137c478bd9Sstevel@tonic-gate  *	}
2147c478bd9Sstevel@tonic-gate  *	return (gfs_readdir_fini(..., error, eofp, eof));
2157c478bd9Sstevel@tonic-gate  *
2167c478bd9Sstevel@tonic-gate  * As you can see, a zero result from gfs_readdir_pred() or
2177c478bd9Sstevel@tonic-gate  * gfs_readdir_emit() indicates that processing should continue,
2187c478bd9Sstevel@tonic-gate  * whereas a non-zero result indicates that the loop should terminate.
2197c478bd9Sstevel@tonic-gate  * Most consumers need do nothing more than let gfs_readdir_fini()
2207c478bd9Sstevel@tonic-gate  * determine what the cause of failure was and return the appropriate
2217c478bd9Sstevel@tonic-gate  * value.
2227c478bd9Sstevel@tonic-gate  */
2237c478bd9Sstevel@tonic-gate int
2247c478bd9Sstevel@tonic-gate gfs_readdir_init(gfs_readdir_state_t *st, int name_max, int ureclen,
225b38f0970Sck153898     uio_t *uiop, ino64_t parent, ino64_t self, int flags)
2267c478bd9Sstevel@tonic-gate {
227b38f0970Sck153898 	size_t dirent_size;
228b38f0970Sck153898 
2297c478bd9Sstevel@tonic-gate 	if (uiop->uio_loffset < 0 || uiop->uio_resid <= 0 ||
2307c478bd9Sstevel@tonic-gate 	    (uiop->uio_loffset % ureclen) != 0)
2317c478bd9Sstevel@tonic-gate 		return (EINVAL);
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 	st->grd_ureclen = ureclen;
2347c478bd9Sstevel@tonic-gate 	st->grd_oresid = uiop->uio_resid;
2357c478bd9Sstevel@tonic-gate 	st->grd_namlen = name_max;
236b38f0970Sck153898 	if (flags & V_RDDIR_ENTFLAGS)
237b38f0970Sck153898 		dirent_size = EDIRENT_RECLEN(st->grd_namlen);
238b38f0970Sck153898 	else
239b38f0970Sck153898 		dirent_size = DIRENT64_RECLEN(st->grd_namlen);
240b38f0970Sck153898 	st->grd_dirent = kmem_zalloc(dirent_size, KM_SLEEP);
2417c478bd9Sstevel@tonic-gate 	st->grd_parent = parent;
2427c478bd9Sstevel@tonic-gate 	st->grd_self = self;
243b38f0970Sck153898 	st->grd_flags = flags;
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	return (0);
2467c478bd9Sstevel@tonic-gate }
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate /*
2497c478bd9Sstevel@tonic-gate  * gfs_readdir_emit_int: internal routine to emit directory entry
2507c478bd9Sstevel@tonic-gate  *
251b38f0970Sck153898  *   st		- the current readdir state, which must have d_ino/ed_ino
252b38f0970Sck153898  *		  and d_name/ed_name set
2537c478bd9Sstevel@tonic-gate  *   uiop	- caller-supplied uio pointer
2547c478bd9Sstevel@tonic-gate  *   next	- the offset of the next entry
2557c478bd9Sstevel@tonic-gate  */
2567c478bd9Sstevel@tonic-gate static int
2573f480432Smaybee gfs_readdir_emit_int(gfs_readdir_state_t *st, uio_t *uiop, offset_t next)
2587c478bd9Sstevel@tonic-gate {
2597c478bd9Sstevel@tonic-gate 	int reclen;
260b38f0970Sck153898 	dirent64_t *dp;
261b38f0970Sck153898 	edirent_t *edp;
2627c478bd9Sstevel@tonic-gate 
263b38f0970Sck153898 	if (st->grd_flags & V_RDDIR_ENTFLAGS) {
264b38f0970Sck153898 		edp = st->grd_dirent;
265b38f0970Sck153898 		reclen = EDIRENT_RECLEN(strlen(edp->ed_name));
266b38f0970Sck153898 	} else {
267b38f0970Sck153898 		dp = st->grd_dirent;
268b38f0970Sck153898 		reclen = DIRENT64_RECLEN(strlen(dp->d_name));
269b38f0970Sck153898 	}
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 	if (reclen > uiop->uio_resid) {
2727c478bd9Sstevel@tonic-gate 		/*
2737c478bd9Sstevel@tonic-gate 		 * Error if no entries were returned yet
2747c478bd9Sstevel@tonic-gate 		 */
2757c478bd9Sstevel@tonic-gate 		if (uiop->uio_resid == st->grd_oresid)
2767c478bd9Sstevel@tonic-gate 			return (EINVAL);
2777c478bd9Sstevel@tonic-gate 		return (-1);
2787c478bd9Sstevel@tonic-gate 	}
2797c478bd9Sstevel@tonic-gate 
280b38f0970Sck153898 	if (st->grd_flags & V_RDDIR_ENTFLAGS) {
281b38f0970Sck153898 		edp->ed_off = next;
282b38f0970Sck153898 		edp->ed_reclen = (ushort_t)reclen;
283b38f0970Sck153898 	} else {
284b38f0970Sck153898 		dp->d_off = next;
285b38f0970Sck153898 		dp->d_reclen = (ushort_t)reclen;
286b38f0970Sck153898 	}
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 	if (uiomove((caddr_t)st->grd_dirent, reclen, UIO_READ, uiop))
2897c478bd9Sstevel@tonic-gate 		return (EFAULT);
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 	uiop->uio_loffset = next;
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 	return (0);
2947c478bd9Sstevel@tonic-gate }
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate /*
2977c478bd9Sstevel@tonic-gate  * gfs_readdir_emit: emit a directory entry
2987c478bd9Sstevel@tonic-gate  *   voff       - the virtual offset (obtained from gfs_readdir_pred)
2997c478bd9Sstevel@tonic-gate  *   ino        - the entry's inode
3007c478bd9Sstevel@tonic-gate  *   name       - the entry's name
301b38f0970Sck153898  *   eflags	- value for ed_eflags (if processing edirent_t)
3027c478bd9Sstevel@tonic-gate  *
3037c478bd9Sstevel@tonic-gate  * Returns a 0 on success, a non-zero errno on failure, or -1 if the
3047c478bd9Sstevel@tonic-gate  * readdir loop should terminate.  A non-zero result (either errno or
3057c478bd9Sstevel@tonic-gate  * -1) from this function is typically passed directly to
3067c478bd9Sstevel@tonic-gate  * gfs_readdir_fini().
3077c478bd9Sstevel@tonic-gate  */
3087c478bd9Sstevel@tonic-gate int
3097c478bd9Sstevel@tonic-gate gfs_readdir_emit(gfs_readdir_state_t *st, uio_t *uiop, offset_t voff,
310b38f0970Sck153898     ino64_t ino, const char *name, int eflags)
3117c478bd9Sstevel@tonic-gate {
3127c478bd9Sstevel@tonic-gate 	offset_t off = (voff + 2) * st->grd_ureclen;
3137c478bd9Sstevel@tonic-gate 
314b38f0970Sck153898 	if (st->grd_flags & V_RDDIR_ENTFLAGS) {
315b38f0970Sck153898 		edirent_t *edp = st->grd_dirent;
316b38f0970Sck153898 
317b38f0970Sck153898 		edp->ed_ino = ino;
318b38f0970Sck153898 		(void) strncpy(edp->ed_name, name, st->grd_namlen);
319b38f0970Sck153898 		edp->ed_eflags = eflags;
320b38f0970Sck153898 	} else {
321b38f0970Sck153898 		dirent64_t *dp = st->grd_dirent;
322b38f0970Sck153898 
323b38f0970Sck153898 		dp->d_ino = ino;
324b38f0970Sck153898 		(void) strncpy(dp->d_name, name, st->grd_namlen);
325b38f0970Sck153898 	}
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 	/*
3287c478bd9Sstevel@tonic-gate 	 * Inter-entry offsets are invalid, so we assume a record size of
3297c478bd9Sstevel@tonic-gate 	 * grd_ureclen and explicitly set the offset appropriately.
3307c478bd9Sstevel@tonic-gate 	 */
3313f480432Smaybee 	return (gfs_readdir_emit_int(st, uiop, off + st->grd_ureclen));
3327c478bd9Sstevel@tonic-gate }
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate /*
3357c478bd9Sstevel@tonic-gate  * gfs_readdir_emitn: like gfs_readdir_emit(), but takes an integer
3367c478bd9Sstevel@tonic-gate  * instead of a string for the entry's name.
3377c478bd9Sstevel@tonic-gate  */
3387c478bd9Sstevel@tonic-gate int
3397c478bd9Sstevel@tonic-gate gfs_readdir_emitn(gfs_readdir_state_t *st, uio_t *uiop, offset_t voff,
3407c478bd9Sstevel@tonic-gate     ino64_t ino, unsigned long num)
3417c478bd9Sstevel@tonic-gate {
3427c478bd9Sstevel@tonic-gate 	char buf[40];
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 	numtos(num, buf);
345b38f0970Sck153898 	return (gfs_readdir_emit(st, uiop, voff, ino, buf, 0));
3467c478bd9Sstevel@tonic-gate }
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate /*
3497c478bd9Sstevel@tonic-gate  * gfs_readdir_pred: readdir loop predicate
3507c478bd9Sstevel@tonic-gate  *   voffp - a pointer in which the next virtual offset should be stored
3517c478bd9Sstevel@tonic-gate  *
3527c478bd9Sstevel@tonic-gate  * Returns a 0 on success, a non-zero errno on failure, or -1 if the
3537c478bd9Sstevel@tonic-gate  * readdir loop should terminate.  A non-zero result (either errno or
3547c478bd9Sstevel@tonic-gate  * -1) from this function is typically passed directly to
3557c478bd9Sstevel@tonic-gate  * gfs_readdir_fini().
3567c478bd9Sstevel@tonic-gate  */
3577c478bd9Sstevel@tonic-gate int
3587c478bd9Sstevel@tonic-gate gfs_readdir_pred(gfs_readdir_state_t *st, uio_t *uiop, offset_t *voffp)
3597c478bd9Sstevel@tonic-gate {
3607c478bd9Sstevel@tonic-gate 	offset_t off, voff;
3617c478bd9Sstevel@tonic-gate 	int error;
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate top:
3647c478bd9Sstevel@tonic-gate 	if (uiop->uio_resid <= 0)
3657c478bd9Sstevel@tonic-gate 		return (-1);
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 	off = uiop->uio_loffset / st->grd_ureclen;
3687c478bd9Sstevel@tonic-gate 	voff = off - 2;
3697c478bd9Sstevel@tonic-gate 	if (off == 0) {
3707c478bd9Sstevel@tonic-gate 		if ((error = gfs_readdir_emit(st, uiop, voff, st->grd_self,
371b38f0970Sck153898 		    ".", 0)) == 0)
3727c478bd9Sstevel@tonic-gate 			goto top;
3737c478bd9Sstevel@tonic-gate 	} else if (off == 1) {
3747c478bd9Sstevel@tonic-gate 		if ((error = gfs_readdir_emit(st, uiop, voff, st->grd_parent,
375b38f0970Sck153898 		    "..", 0)) == 0)
3767c478bd9Sstevel@tonic-gate 			goto top;
3777c478bd9Sstevel@tonic-gate 	} else {
3787c478bd9Sstevel@tonic-gate 		*voffp = voff;
3797c478bd9Sstevel@tonic-gate 		return (0);
3807c478bd9Sstevel@tonic-gate 	}
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	return (error);
3837c478bd9Sstevel@tonic-gate }
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate /*
3867c478bd9Sstevel@tonic-gate  * gfs_readdir_fini: generic readdir cleanup
3877c478bd9Sstevel@tonic-gate  *   error	- if positive, an error to return
3887c478bd9Sstevel@tonic-gate  *   eofp	- the eofp passed to readdir
3897c478bd9Sstevel@tonic-gate  *   eof	- the eof value
3907c478bd9Sstevel@tonic-gate  *
3917c478bd9Sstevel@tonic-gate  * Returns a 0 on success, a non-zero errno on failure.  This result
3927c478bd9Sstevel@tonic-gate  * should be returned from readdir.
3937c478bd9Sstevel@tonic-gate  */
3947c478bd9Sstevel@tonic-gate int
3957c478bd9Sstevel@tonic-gate gfs_readdir_fini(gfs_readdir_state_t *st, int error, int *eofp, int eof)
3967c478bd9Sstevel@tonic-gate {
397b38f0970Sck153898 	size_t dirent_size;
398b38f0970Sck153898 
399b38f0970Sck153898 	if (st->grd_flags & V_RDDIR_ENTFLAGS)
400b38f0970Sck153898 		dirent_size = EDIRENT_RECLEN(st->grd_namlen);
401b38f0970Sck153898 	else
402b38f0970Sck153898 		dirent_size = DIRENT64_RECLEN(st->grd_namlen);
403b38f0970Sck153898 	kmem_free(st->grd_dirent, dirent_size);
4047c478bd9Sstevel@tonic-gate 	if (error > 0)
4057c478bd9Sstevel@tonic-gate 		return (error);
4067c478bd9Sstevel@tonic-gate 	if (eofp)
4077c478bd9Sstevel@tonic-gate 		*eofp = eof;
4087c478bd9Sstevel@tonic-gate 	return (0);
4097c478bd9Sstevel@tonic-gate }
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate /*
4127c478bd9Sstevel@tonic-gate  * gfs_lookup_dot
4137c478bd9Sstevel@tonic-gate  *
4147c478bd9Sstevel@tonic-gate  * Performs a basic check for "." and ".." directory entries.
4157c478bd9Sstevel@tonic-gate  */
4167c478bd9Sstevel@tonic-gate int
4177c478bd9Sstevel@tonic-gate gfs_lookup_dot(vnode_t **vpp, vnode_t *dvp, vnode_t *pvp, const char *nm)
4187c478bd9Sstevel@tonic-gate {
4197c478bd9Sstevel@tonic-gate 	if (*nm == '\0' || strcmp(nm, ".") == 0) {
4207c478bd9Sstevel@tonic-gate 		VN_HOLD(dvp);
4217c478bd9Sstevel@tonic-gate 		*vpp = dvp;
4227c478bd9Sstevel@tonic-gate 		return (0);
4237c478bd9Sstevel@tonic-gate 	} else if (strcmp(nm, "..") == 0) {
4247c478bd9Sstevel@tonic-gate 		if (pvp == NULL) {
4257c478bd9Sstevel@tonic-gate 			ASSERT(dvp->v_flag & VROOT);
4267c478bd9Sstevel@tonic-gate 			VN_HOLD(dvp);
4277c478bd9Sstevel@tonic-gate 			*vpp = dvp;
4287c478bd9Sstevel@tonic-gate 		} else {
4297c478bd9Sstevel@tonic-gate 			VN_HOLD(pvp);
4307c478bd9Sstevel@tonic-gate 			*vpp = pvp;
4317c478bd9Sstevel@tonic-gate 		}
4327c478bd9Sstevel@tonic-gate 		return (0);
4337c478bd9Sstevel@tonic-gate 	}
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 	return (-1);
4367c478bd9Sstevel@tonic-gate }
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate /*
4397c478bd9Sstevel@tonic-gate  * gfs_file_create(): create a new GFS file
4407c478bd9Sstevel@tonic-gate  *
4417c478bd9Sstevel@tonic-gate  *   size	- size of private data structure (v_data)
4427c478bd9Sstevel@tonic-gate  *   pvp	- parent vnode (GFS directory)
4437c478bd9Sstevel@tonic-gate  *   ops	- vnode operations vector
4447c478bd9Sstevel@tonic-gate  *
4457c478bd9Sstevel@tonic-gate  * In order to use this interface, the parent vnode must have been created by
4467c478bd9Sstevel@tonic-gate  * gfs_dir_create(), and the private data stored in v_data must have a
4477c478bd9Sstevel@tonic-gate  * 'gfs_file_t' as its first field.
4487c478bd9Sstevel@tonic-gate  *
4497c478bd9Sstevel@tonic-gate  * Given these constraints, this routine will automatically:
4507c478bd9Sstevel@tonic-gate  *
4517c478bd9Sstevel@tonic-gate  * 	- Allocate v_data for the vnode
4527c478bd9Sstevel@tonic-gate  * 	- Initialize necessary fields in the vnode
4537c478bd9Sstevel@tonic-gate  * 	- Hold the parent
4547c478bd9Sstevel@tonic-gate  */
4557c478bd9Sstevel@tonic-gate vnode_t *
4567c478bd9Sstevel@tonic-gate gfs_file_create(size_t size, vnode_t *pvp, vnodeops_t *ops)
4577c478bd9Sstevel@tonic-gate {
4587c478bd9Sstevel@tonic-gate 	gfs_file_t *fp;
4597c478bd9Sstevel@tonic-gate 	vnode_t *vp;
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 	/*
4627c478bd9Sstevel@tonic-gate 	 * Allocate vnode and internal data structure
4637c478bd9Sstevel@tonic-gate 	 */
4647c478bd9Sstevel@tonic-gate 	fp = kmem_zalloc(size, KM_SLEEP);
4657c478bd9Sstevel@tonic-gate 	vp = vn_alloc(KM_SLEEP);
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 	/*
4687c478bd9Sstevel@tonic-gate 	 * Set up various pointers
4697c478bd9Sstevel@tonic-gate 	 */
4707c478bd9Sstevel@tonic-gate 	fp->gfs_vnode = vp;
4717c478bd9Sstevel@tonic-gate 	fp->gfs_parent = pvp;
4727c478bd9Sstevel@tonic-gate 	vp->v_data = fp;
4737c478bd9Sstevel@tonic-gate 	fp->gfs_size = size;
4747c478bd9Sstevel@tonic-gate 	fp->gfs_type = GFS_FILE;
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 	/*
4777c478bd9Sstevel@tonic-gate 	 * Initialize vnode and hold parent.
4787c478bd9Sstevel@tonic-gate 	 */
4797c478bd9Sstevel@tonic-gate 	vn_setops(vp, ops);
4807c478bd9Sstevel@tonic-gate 	if (pvp) {
4817c478bd9Sstevel@tonic-gate 		VN_SET_VFS_TYPE_DEV(vp, pvp->v_vfsp, VREG, 0);
4827c478bd9Sstevel@tonic-gate 		VN_HOLD(pvp);
4837c478bd9Sstevel@tonic-gate 	}
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 	return (vp);
4867c478bd9Sstevel@tonic-gate }
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate /*
4897c478bd9Sstevel@tonic-gate  * gfs_dir_create: creates a new directory in the parent
4907c478bd9Sstevel@tonic-gate  *
4917c478bd9Sstevel@tonic-gate  *   size	- size of private data structure (v_data)
4927c478bd9Sstevel@tonic-gate  *   pvp	- parent vnode (GFS directory)
4937c478bd9Sstevel@tonic-gate  *   ops	- vnode operations vector
4947c478bd9Sstevel@tonic-gate  *   entries	- NULL-terminated list of static entries (if any)
4957c478bd9Sstevel@tonic-gate  *   maxlen	- maximum length of a directory entry
4967c478bd9Sstevel@tonic-gate  *   readdir_cb	- readdir callback (see gfs_dir_readdir)
4977c478bd9Sstevel@tonic-gate  *   inode_cb	- inode callback (see gfs_dir_readdir)
4987c478bd9Sstevel@tonic-gate  *   lookup_cb	- lookup callback (see gfs_dir_lookup)
4997c478bd9Sstevel@tonic-gate  *
5007c478bd9Sstevel@tonic-gate  * In order to use this function, the first member of the private vnode
5017c478bd9Sstevel@tonic-gate  * structure (v_data) must be a gfs_dir_t.  For each directory, there are
5027c478bd9Sstevel@tonic-gate  * static entries, defined when the structure is initialized, and dynamic
5037c478bd9Sstevel@tonic-gate  * entries, retrieved through callbacks.
5047c478bd9Sstevel@tonic-gate  *
5057c478bd9Sstevel@tonic-gate  * If a directory has static entries, then it must supply a inode callback,
5067c478bd9Sstevel@tonic-gate  * which will compute the inode number based on the parent and the index.
5077c478bd9Sstevel@tonic-gate  * For a directory with dynamic entries, the caller must supply a readdir
5087c478bd9Sstevel@tonic-gate  * callback and a lookup callback.  If a static lookup fails, we fall back to
5097c478bd9Sstevel@tonic-gate  * the supplied lookup callback, if any.
5107c478bd9Sstevel@tonic-gate  *
5117c478bd9Sstevel@tonic-gate  * This function also performs the same initialization as gfs_file_create().
5127c478bd9Sstevel@tonic-gate  */
5137c478bd9Sstevel@tonic-gate vnode_t *
5147c478bd9Sstevel@tonic-gate gfs_dir_create(size_t struct_size, vnode_t *pvp, vnodeops_t *ops,
5157c478bd9Sstevel@tonic-gate     gfs_dirent_t *entries, gfs_inode_cb inode_cb, int maxlen,
5167c478bd9Sstevel@tonic-gate     gfs_readdir_cb readdir_cb, gfs_lookup_cb lookup_cb)
5177c478bd9Sstevel@tonic-gate {
5187c478bd9Sstevel@tonic-gate 	vnode_t *vp;
5197c478bd9Sstevel@tonic-gate 	gfs_dir_t *dp;
5207c478bd9Sstevel@tonic-gate 	gfs_dirent_t *de;
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate 	vp = gfs_file_create(struct_size, pvp, ops);
5237c478bd9Sstevel@tonic-gate 	vp->v_type = VDIR;
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 	dp = vp->v_data;
5267c478bd9Sstevel@tonic-gate 	dp->gfsd_file.gfs_type = GFS_DIR;
5277c478bd9Sstevel@tonic-gate 	dp->gfsd_maxlen = maxlen;
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate 	if (entries != NULL) {
5307c478bd9Sstevel@tonic-gate 		for (de = entries; de->gfse_name != NULL; de++)
5317c478bd9Sstevel@tonic-gate 			dp->gfsd_nstatic++;
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 		dp->gfsd_static = kmem_alloc(
5347c478bd9Sstevel@tonic-gate 		    dp->gfsd_nstatic * sizeof (gfs_dirent_t), KM_SLEEP);
5357c478bd9Sstevel@tonic-gate 		bcopy(entries, dp->gfsd_static,
5367c478bd9Sstevel@tonic-gate 		    dp->gfsd_nstatic * sizeof (gfs_dirent_t));
5377c478bd9Sstevel@tonic-gate 	}
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 	dp->gfsd_readdir = readdir_cb;
5407c478bd9Sstevel@tonic-gate 	dp->gfsd_lookup = lookup_cb;
5417c478bd9Sstevel@tonic-gate 	dp->gfsd_inode = inode_cb;
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate 	mutex_init(&dp->gfsd_lock, NULL, MUTEX_DEFAULT, NULL);
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate 	return (vp);
5467c478bd9Sstevel@tonic-gate }
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate /*
5497c478bd9Sstevel@tonic-gate  * gfs_root_create(): create a root vnode for a GFS filesystem
5507c478bd9Sstevel@tonic-gate  *
5517c478bd9Sstevel@tonic-gate  * Similar to gfs_dir_create(), this creates a root vnode for a filesystem.  The
5527c478bd9Sstevel@tonic-gate  * only difference is that it takes a vfs_t instead of a vnode_t as its parent.
5537c478bd9Sstevel@tonic-gate  */
5547c478bd9Sstevel@tonic-gate vnode_t *
5557c478bd9Sstevel@tonic-gate gfs_root_create(size_t size, vfs_t *vfsp, vnodeops_t *ops, ino64_t ino,
5567c478bd9Sstevel@tonic-gate     gfs_dirent_t *entries, gfs_inode_cb inode_cb, int maxlen,
5577c478bd9Sstevel@tonic-gate     gfs_readdir_cb readdir_cb, gfs_lookup_cb lookup_cb)
5587c478bd9Sstevel@tonic-gate {
5597c478bd9Sstevel@tonic-gate 	vnode_t *vp = gfs_dir_create(size, NULL, ops, entries, inode_cb,
5607c478bd9Sstevel@tonic-gate 	    maxlen, readdir_cb, lookup_cb);
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 	/* Manually set the inode */
5637c478bd9Sstevel@tonic-gate 	((gfs_file_t *)vp->v_data)->gfs_ino = ino;
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate 	VFS_HOLD(vfsp);
5667c478bd9Sstevel@tonic-gate 	VN_SET_VFS_TYPE_DEV(vp, vfsp, VDIR, 0);
5677c478bd9Sstevel@tonic-gate 	vp->v_flag |= VROOT | VNOCACHE | VNOMAP | VNOSWAP | VNOMOUNT;
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate 	return (vp);
5707c478bd9Sstevel@tonic-gate }
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate /*
573a237e38eSth199096  * gfs_root_create_file(): create a root vnode for a GFS file as a filesystem
574a237e38eSth199096  *
575a237e38eSth199096  * Similar to gfs_root_create(), this creates a root vnode for a file to
576a237e38eSth199096  * be the pseudo-filesystem.
577a237e38eSth199096  */
578a237e38eSth199096 vnode_t *
579a237e38eSth199096 gfs_root_create_file(size_t size, vfs_t *vfsp, vnodeops_t *ops, ino64_t ino)
580a237e38eSth199096 {
581a237e38eSth199096 	vnode_t	*vp = gfs_file_create(size, NULL, ops);
582a237e38eSth199096 
583a237e38eSth199096 	((gfs_file_t *)vp->v_data)->gfs_ino = ino;
584a237e38eSth199096 
585a237e38eSth199096 	VFS_HOLD(vfsp);
586a237e38eSth199096 	VN_SET_VFS_TYPE_DEV(vp, vfsp, VREG, 0);
587a237e38eSth199096 	vp->v_flag |= VROOT | VNOCACHE | VNOMAP | VNOSWAP | VNOMOUNT;
588a237e38eSth199096 
589a237e38eSth199096 	return (vp);
590a237e38eSth199096 }
591a237e38eSth199096 
592a237e38eSth199096 /*
5937c478bd9Sstevel@tonic-gate  * gfs_file_inactive()
5947c478bd9Sstevel@tonic-gate  *
5957c478bd9Sstevel@tonic-gate  * Called from the VOP_INACTIVE() routine.  If necessary, this routine will
5967c478bd9Sstevel@tonic-gate  * remove the given vnode from the parent directory and clean up any references
5977c478bd9Sstevel@tonic-gate  * in the VFS layer.
5987c478bd9Sstevel@tonic-gate  *
5997c478bd9Sstevel@tonic-gate  * If the vnode was not removed (due to a race with vget), then NULL is
6007c478bd9Sstevel@tonic-gate  * returned.  Otherwise, a pointer to the private data is returned.
6017c478bd9Sstevel@tonic-gate  */
6027c478bd9Sstevel@tonic-gate void *
6037c478bd9Sstevel@tonic-gate gfs_file_inactive(vnode_t *vp)
6047c478bd9Sstevel@tonic-gate {
6057c478bd9Sstevel@tonic-gate 	int i;
6067c478bd9Sstevel@tonic-gate 	gfs_dirent_t *ge = NULL;
6077c478bd9Sstevel@tonic-gate 	gfs_file_t *fp = vp->v_data;
6087c478bd9Sstevel@tonic-gate 	gfs_dir_t *dp = NULL;
6097c478bd9Sstevel@tonic-gate 	void *data;
6107c478bd9Sstevel@tonic-gate 
611da6c28aaSamw 	if (fp->gfs_parent == NULL || (vp->v_flag & V_XATTRDIR))
6127c478bd9Sstevel@tonic-gate 		goto found;
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 	dp = fp->gfs_parent->v_data;
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate 	/*
6177c478bd9Sstevel@tonic-gate 	 * First, see if this vnode is cached in the parent.
6187c478bd9Sstevel@tonic-gate 	 */
6197c478bd9Sstevel@tonic-gate 	gfs_dir_lock(dp);
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 	/*
6227c478bd9Sstevel@tonic-gate 	 * Find it in the set of static entries.
6237c478bd9Sstevel@tonic-gate 	 */
6247c478bd9Sstevel@tonic-gate 	for (i = 0; i < dp->gfsd_nstatic; i++)  {
6257c478bd9Sstevel@tonic-gate 		ge = &dp->gfsd_static[i];
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate 		if (ge->gfse_vnode == vp)
6287c478bd9Sstevel@tonic-gate 			goto found;
6297c478bd9Sstevel@tonic-gate 	}
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate 	/*
6327c478bd9Sstevel@tonic-gate 	 * If 'ge' is NULL, then it is a dynamic entry.
6337c478bd9Sstevel@tonic-gate 	 */
6347c478bd9Sstevel@tonic-gate 	ge = NULL;
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate found:
637da6c28aaSamw 	if (vp->v_flag & V_XATTRDIR) {
638da6c28aaSamw 		mutex_enter(&fp->gfs_parent->v_lock);
639da6c28aaSamw 	}
6407c478bd9Sstevel@tonic-gate 	mutex_enter(&vp->v_lock);
6417c478bd9Sstevel@tonic-gate 	if (vp->v_count == 1) {
6427c478bd9Sstevel@tonic-gate 		/*
6437c478bd9Sstevel@tonic-gate 		 * Really remove this vnode
6447c478bd9Sstevel@tonic-gate 		 */
6457c478bd9Sstevel@tonic-gate 		data = vp->v_data;
6467c478bd9Sstevel@tonic-gate 		if (ge != NULL) {
6477c478bd9Sstevel@tonic-gate 			/*
6487c478bd9Sstevel@tonic-gate 			 * If this was a statically cached entry, simply set the
6497c478bd9Sstevel@tonic-gate 			 * cached vnode to NULL.
6507c478bd9Sstevel@tonic-gate 			 */
6517c478bd9Sstevel@tonic-gate 			ge->gfse_vnode = NULL;
6527c478bd9Sstevel@tonic-gate 		}
653da6c28aaSamw 		if (vp->v_flag & V_XATTRDIR) {
654da6c28aaSamw 			fp->gfs_parent->v_xattrdir = NULL;
655da6c28aaSamw 			mutex_exit(&fp->gfs_parent->v_lock);
656da6c28aaSamw 		}
6577c478bd9Sstevel@tonic-gate 		mutex_exit(&vp->v_lock);
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 		/*
6607c478bd9Sstevel@tonic-gate 		 * Free vnode and release parent
6617c478bd9Sstevel@tonic-gate 		 */
6627c478bd9Sstevel@tonic-gate 		if (fp->gfs_parent) {
663da6c28aaSamw 			if (dp) {
6647c478bd9Sstevel@tonic-gate 				gfs_dir_unlock(dp);
665da6c28aaSamw 			}
6667c478bd9Sstevel@tonic-gate 			VN_RELE(fp->gfs_parent);
6677c478bd9Sstevel@tonic-gate 		} else {
6687c478bd9Sstevel@tonic-gate 			ASSERT(vp->v_vfsp != NULL);
6697c478bd9Sstevel@tonic-gate 			VFS_RELE(vp->v_vfsp);
6707c478bd9Sstevel@tonic-gate 		}
6717c478bd9Sstevel@tonic-gate 		vn_free(vp);
6727c478bd9Sstevel@tonic-gate 	} else {
6737c478bd9Sstevel@tonic-gate 		vp->v_count--;
6747c478bd9Sstevel@tonic-gate 		data = NULL;
6757c478bd9Sstevel@tonic-gate 		mutex_exit(&vp->v_lock);
676da6c28aaSamw 		if (vp->v_flag & V_XATTRDIR) {
677da6c28aaSamw 			mutex_exit(&fp->gfs_parent->v_lock);
678da6c28aaSamw 		}
6797c478bd9Sstevel@tonic-gate 		if (dp)
6807c478bd9Sstevel@tonic-gate 			gfs_dir_unlock(dp);
6817c478bd9Sstevel@tonic-gate 	}
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate 	return (data);
6847c478bd9Sstevel@tonic-gate }
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate /*
6877c478bd9Sstevel@tonic-gate  * gfs_dir_inactive()
6887c478bd9Sstevel@tonic-gate  *
6897c478bd9Sstevel@tonic-gate  * Same as above, but for directories.
6907c478bd9Sstevel@tonic-gate  */
6917c478bd9Sstevel@tonic-gate void *
6927c478bd9Sstevel@tonic-gate gfs_dir_inactive(vnode_t *vp)
6937c478bd9Sstevel@tonic-gate {
6947c478bd9Sstevel@tonic-gate 	gfs_dir_t *dp;
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate 	ASSERT(vp->v_type == VDIR);
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate 	if ((dp = gfs_file_inactive(vp)) != NULL) {
6997c478bd9Sstevel@tonic-gate 		mutex_destroy(&dp->gfsd_lock);
7007c478bd9Sstevel@tonic-gate 		if (dp->gfsd_nstatic)
7017c478bd9Sstevel@tonic-gate 			kmem_free(dp->gfsd_static,
7027c478bd9Sstevel@tonic-gate 			    dp->gfsd_nstatic * sizeof (gfs_dirent_t));
7037c478bd9Sstevel@tonic-gate 	}
7047c478bd9Sstevel@tonic-gate 
7057c478bd9Sstevel@tonic-gate 	return (dp);
7067c478bd9Sstevel@tonic-gate }
7077c478bd9Sstevel@tonic-gate 
7087c478bd9Sstevel@tonic-gate /*
7097c478bd9Sstevel@tonic-gate  * gfs_dir_lookup()
7107c478bd9Sstevel@tonic-gate  *
7117c478bd9Sstevel@tonic-gate  * Looks up the given name in the directory and returns the corresponding vnode,
7127c478bd9Sstevel@tonic-gate  * if found.
7137c478bd9Sstevel@tonic-gate  *
7147c478bd9Sstevel@tonic-gate  * First, we search statically defined entries, if any.  If a match is found,
7157c478bd9Sstevel@tonic-gate  * and GFS_CACHE_VNODE is set and the vnode exists, we simply return the
7167c478bd9Sstevel@tonic-gate  * existing vnode.  Otherwise, we call the static entry's callback routine,
7177c478bd9Sstevel@tonic-gate  * caching the result if necessary.
7187c478bd9Sstevel@tonic-gate  *
7197c478bd9Sstevel@tonic-gate  * If no static entry is found, we invoke the lookup callback, if any.  The
7207c478bd9Sstevel@tonic-gate  * arguments to this callback are:
7217c478bd9Sstevel@tonic-gate  *
722da6c28aaSamw  * int gfs_lookup_cb(vnode_t *pvp, const char *nm, vnode_t **vpp, cred_t *cr);
7237c478bd9Sstevel@tonic-gate  *
7247c478bd9Sstevel@tonic-gate  *	pvp	- parent vnode
7257c478bd9Sstevel@tonic-gate  *	nm	- name of entry
7267c478bd9Sstevel@tonic-gate  *	vpp	- pointer to resulting vnode
727da6c28aaSamw  *	cr	- pointer to cred
7287c478bd9Sstevel@tonic-gate  *
7297c478bd9Sstevel@tonic-gate  * 	Returns 0 on success, non-zero on error.
7307c478bd9Sstevel@tonic-gate  */
7317c478bd9Sstevel@tonic-gate int
732da6c28aaSamw gfs_dir_lookup(vnode_t *dvp, const char *nm, vnode_t **vpp, cred_t *cr)
7337c478bd9Sstevel@tonic-gate {
7347c478bd9Sstevel@tonic-gate 	int i;
7357c478bd9Sstevel@tonic-gate 	gfs_dirent_t *ge;
7367c478bd9Sstevel@tonic-gate 	vnode_t *vp;
7377c478bd9Sstevel@tonic-gate 	gfs_dir_t *dp = dvp->v_data;
7387c478bd9Sstevel@tonic-gate 	int ret = 0;
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate 	ASSERT(dvp->v_type == VDIR);
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate 	if (gfs_lookup_dot(vpp, dvp, dp->gfsd_file.gfs_parent, nm) == 0)
7437c478bd9Sstevel@tonic-gate 		return (0);
7447c478bd9Sstevel@tonic-gate 
7457c478bd9Sstevel@tonic-gate 	gfs_dir_lock(dp);
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate 	/*
7487c478bd9Sstevel@tonic-gate 	 * Search static entries.
7497c478bd9Sstevel@tonic-gate 	 */
7507c478bd9Sstevel@tonic-gate 	for (i = 0; i < dp->gfsd_nstatic; i++) {
7517c478bd9Sstevel@tonic-gate 		ge = &dp->gfsd_static[i];
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate 		if (strcmp(ge->gfse_name, nm) == 0) {
7547c478bd9Sstevel@tonic-gate 			if (ge->gfse_vnode) {
7557c478bd9Sstevel@tonic-gate 				ASSERT(ge->gfse_flags & GFS_CACHE_VNODE);
7567c478bd9Sstevel@tonic-gate 				vp = ge->gfse_vnode;
7577c478bd9Sstevel@tonic-gate 				VN_HOLD(vp);
7587c478bd9Sstevel@tonic-gate 				goto out;
7597c478bd9Sstevel@tonic-gate 			}
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate 			/*
762092e3d7cSjk115741 			 * We drop the directory lock, as the constructor will
7637c478bd9Sstevel@tonic-gate 			 * need to do KM_SLEEP allocations.  If we return from
7647c478bd9Sstevel@tonic-gate 			 * the constructor only to find that a parallel
7657c478bd9Sstevel@tonic-gate 			 * operation has completed, and GFS_CACHE_VNODE is set
7667c478bd9Sstevel@tonic-gate 			 * for this entry, we discard the result in favor of the
7677c478bd9Sstevel@tonic-gate 			 * cached vnode.
7687c478bd9Sstevel@tonic-gate 			 */
7697c478bd9Sstevel@tonic-gate 			gfs_dir_unlock(dp);
7707c478bd9Sstevel@tonic-gate 			vp = ge->gfse_ctor(dvp);
7717c478bd9Sstevel@tonic-gate 			gfs_dir_lock(dp);
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate 			((gfs_file_t *)vp->v_data)->gfs_index = i;
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate 			/* Set the inode according to the callback. */
7767c478bd9Sstevel@tonic-gate 			((gfs_file_t *)vp->v_data)->gfs_ino =
7777c478bd9Sstevel@tonic-gate 			    dp->gfsd_inode(dvp, i);
7787c478bd9Sstevel@tonic-gate 
7797c478bd9Sstevel@tonic-gate 			if (ge->gfse_flags & GFS_CACHE_VNODE) {
7807c478bd9Sstevel@tonic-gate 				if (ge->gfse_vnode == NULL) {
7817c478bd9Sstevel@tonic-gate 					ge->gfse_vnode = vp;
7827c478bd9Sstevel@tonic-gate 				} else {
7837c478bd9Sstevel@tonic-gate 					/*
7847c478bd9Sstevel@tonic-gate 					 * A parallel constructor beat us to it;
7857c478bd9Sstevel@tonic-gate 					 * return existing vnode.  We have to be
7867c478bd9Sstevel@tonic-gate 					 * careful because we can't release the
7877c478bd9Sstevel@tonic-gate 					 * current vnode while holding the
7887c478bd9Sstevel@tonic-gate 					 * directory lock; its inactive routine
7897c478bd9Sstevel@tonic-gate 					 * will try to lock this directory.
7907c478bd9Sstevel@tonic-gate 					 */
7917c478bd9Sstevel@tonic-gate 					vnode_t *oldvp = vp;
7927c478bd9Sstevel@tonic-gate 					vp = ge->gfse_vnode;
7937c478bd9Sstevel@tonic-gate 					VN_HOLD(vp);
7947c478bd9Sstevel@tonic-gate 
7957c478bd9Sstevel@tonic-gate 					gfs_dir_unlock(dp);
7967c478bd9Sstevel@tonic-gate 					VN_RELE(oldvp);
7977c478bd9Sstevel@tonic-gate 					gfs_dir_lock(dp);
7987c478bd9Sstevel@tonic-gate 				}
7997c478bd9Sstevel@tonic-gate 			}
8007c478bd9Sstevel@tonic-gate 
8017c478bd9Sstevel@tonic-gate 			goto out;
8027c478bd9Sstevel@tonic-gate 		}
8037c478bd9Sstevel@tonic-gate 	}
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate 	/*
8067c478bd9Sstevel@tonic-gate 	 * See if there is a dynamic constructor.
8077c478bd9Sstevel@tonic-gate 	 */
8087c478bd9Sstevel@tonic-gate 	if (dp->gfsd_lookup) {
8097c478bd9Sstevel@tonic-gate 		ino64_t ino;
8107c478bd9Sstevel@tonic-gate 		gfs_file_t *fp;
8117c478bd9Sstevel@tonic-gate 
8127c478bd9Sstevel@tonic-gate 		/*
8137c478bd9Sstevel@tonic-gate 		 * Once again, drop the directory lock, as the lookup routine
8147c478bd9Sstevel@tonic-gate 		 * will need to allocate memory, or otherwise deadlock on this
8157c478bd9Sstevel@tonic-gate 		 * directory.
8167c478bd9Sstevel@tonic-gate 		 */
8177c478bd9Sstevel@tonic-gate 		gfs_dir_unlock(dp);
818da6c28aaSamw 		ret = dp->gfsd_lookup(dvp, nm, &vp, &ino, cr);
8197c478bd9Sstevel@tonic-gate 		gfs_dir_lock(dp);
8207c478bd9Sstevel@tonic-gate 		if (ret != 0)
8217c478bd9Sstevel@tonic-gate 			goto out;
8227c478bd9Sstevel@tonic-gate 
823da6c28aaSamw 		/*
824da6c28aaSamw 		 * The lookup_cb might be returning a non-GFS vnode.
825da6c28aaSamw 		 * Currently this is true for extended attributes,
826da6c28aaSamw 		 * where we're returning a vnode with v_data from an
827da6c28aaSamw 		 * underlying fs.
828da6c28aaSamw 		 */
829da6c28aaSamw 		if ((dvp->v_flag & V_XATTRDIR) == 0) {
8307c478bd9Sstevel@tonic-gate 			fp = (gfs_file_t *)vp->v_data;
8317c478bd9Sstevel@tonic-gate 			fp->gfs_index = -1;
8327c478bd9Sstevel@tonic-gate 			fp->gfs_ino = ino;
833da6c28aaSamw 		}
8347c478bd9Sstevel@tonic-gate 	} else {
8357c478bd9Sstevel@tonic-gate 		/*
8367c478bd9Sstevel@tonic-gate 		 * No static entry found, and there is no lookup callback, so
8377c478bd9Sstevel@tonic-gate 		 * return ENOENT.
8387c478bd9Sstevel@tonic-gate 		 */
8397c478bd9Sstevel@tonic-gate 		ret = ENOENT;
8407c478bd9Sstevel@tonic-gate 	}
8417c478bd9Sstevel@tonic-gate 
8427c478bd9Sstevel@tonic-gate out:
8437c478bd9Sstevel@tonic-gate 	gfs_dir_unlock(dp);
8447c478bd9Sstevel@tonic-gate 
845d13f2f50Smarks 	if (ret == 0)
8467c478bd9Sstevel@tonic-gate 		*vpp = vp;
847d13f2f50Smarks 	else
848d13f2f50Smarks 		*vpp = NULL;
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate 	return (ret);
8517c478bd9Sstevel@tonic-gate }
8527c478bd9Sstevel@tonic-gate 
8537c478bd9Sstevel@tonic-gate /*
8547c478bd9Sstevel@tonic-gate  * gfs_dir_readdir: does a readdir() on the given directory
8557c478bd9Sstevel@tonic-gate  *
8567c478bd9Sstevel@tonic-gate  *    dvp	- directory vnode
8577c478bd9Sstevel@tonic-gate  *    uiop	- uio structure
8587c478bd9Sstevel@tonic-gate  *    eofp	- eof pointer
8597c478bd9Sstevel@tonic-gate  *    data	- arbitrary data passed to readdir callback
8607c478bd9Sstevel@tonic-gate  *
8617c478bd9Sstevel@tonic-gate  * This routine does all the readdir() dirty work.  Even so, the caller must
8627c478bd9Sstevel@tonic-gate  * supply two callbacks in order to get full compatibility.
8637c478bd9Sstevel@tonic-gate  *
8647c478bd9Sstevel@tonic-gate  * If the directory contains static entries, an inode callback must be
8657c478bd9Sstevel@tonic-gate  * specified.  This avoids having to create every vnode and call VOP_GETATTR()
8667c478bd9Sstevel@tonic-gate  * when reading the directory.  This function has the following arguments:
8677c478bd9Sstevel@tonic-gate  *
8687c478bd9Sstevel@tonic-gate  *	ino_t gfs_inode_cb(vnode_t *vp, int index);
8697c478bd9Sstevel@tonic-gate  *
8707c478bd9Sstevel@tonic-gate  * 	vp	- vnode for the directory
8717c478bd9Sstevel@tonic-gate  * 	index	- index in original gfs_dirent_t array
8727c478bd9Sstevel@tonic-gate  *
8737c478bd9Sstevel@tonic-gate  * 	Returns the inode number for the given entry.
8747c478bd9Sstevel@tonic-gate  *
8757c478bd9Sstevel@tonic-gate  * For directories with dynamic entries, a readdir callback must be provided.
8767c478bd9Sstevel@tonic-gate  * This is significantly more complex, thanks to the particulars of
8777c478bd9Sstevel@tonic-gate  * VOP_READDIR().
8787c478bd9Sstevel@tonic-gate  *
879b38f0970Sck153898  *	int gfs_readdir_cb(vnode_t *vp, void *dp, int *eofp,
880b38f0970Sck153898  *	    offset_t *off, offset_t *nextoff, void *data, int flags)
8817c478bd9Sstevel@tonic-gate  *
8827c478bd9Sstevel@tonic-gate  *	vp	- directory vnode
8837c478bd9Sstevel@tonic-gate  *	dp	- directory entry, sized according to maxlen given to
8847c478bd9Sstevel@tonic-gate  *		  gfs_dir_create().  callback must fill in d_name and
885b38f0970Sck153898  *		  d_ino (if a dirent64_t), or ed_name, ed_ino, and ed_eflags
886b38f0970Sck153898  *		  (if an edirent_t). edirent_t is used if V_RDDIR_ENTFLAGS
887b38f0970Sck153898  *		  is set in 'flags'.
8887c478bd9Sstevel@tonic-gate  *	eofp	- callback must set to 1 when EOF has been reached
8897c478bd9Sstevel@tonic-gate  *	off	- on entry, the last offset read from the directory.  Callback
8907c478bd9Sstevel@tonic-gate  *		  must set to the offset of the current entry, typically left
8917c478bd9Sstevel@tonic-gate  *		  untouched.
8927c478bd9Sstevel@tonic-gate  *	nextoff	- callback must set to offset of next entry.  Typically
8937c478bd9Sstevel@tonic-gate  *		  (off + 1)
8947c478bd9Sstevel@tonic-gate  *	data	- caller-supplied data
895b38f0970Sck153898  *	flags	- VOP_READDIR flags
8967c478bd9Sstevel@tonic-gate  *
8977c478bd9Sstevel@tonic-gate  *	Return 0 on success, or error on failure.
8987c478bd9Sstevel@tonic-gate  */
8997c478bd9Sstevel@tonic-gate int
900da6c28aaSamw gfs_dir_readdir(vnode_t *dvp, uio_t *uiop, int *eofp, void *data, cred_t *cr,
901b38f0970Sck153898     caller_context_t *ct, int flags)
9027c478bd9Sstevel@tonic-gate {
9037c478bd9Sstevel@tonic-gate 	gfs_readdir_state_t gstate;
9047c478bd9Sstevel@tonic-gate 	int error, eof = 0;
9057c478bd9Sstevel@tonic-gate 	ino64_t ino, pino;
9067c478bd9Sstevel@tonic-gate 	offset_t off, next;
9077c478bd9Sstevel@tonic-gate 	gfs_dir_t *dp = dvp->v_data;
9087c478bd9Sstevel@tonic-gate 
909b38f0970Sck153898 	error = gfs_get_parent_ino(dvp, cr, ct, &pino, &ino);
910da6c28aaSamw 	if (error)
911da6c28aaSamw 		return (error);
9127c478bd9Sstevel@tonic-gate 
9137c478bd9Sstevel@tonic-gate 	if ((error = gfs_readdir_init(&gstate, dp->gfsd_maxlen, 1, uiop,
914b38f0970Sck153898 	    pino, ino, flags)) != 0)
9157c478bd9Sstevel@tonic-gate 		return (error);
9167c478bd9Sstevel@tonic-gate 
9177c478bd9Sstevel@tonic-gate 	while ((error = gfs_readdir_pred(&gstate, uiop, &off)) == 0 &&
9187c478bd9Sstevel@tonic-gate 	    !eof) {
9197c478bd9Sstevel@tonic-gate 
9207c478bd9Sstevel@tonic-gate 		if (off >= 0 && off < dp->gfsd_nstatic) {
9217c478bd9Sstevel@tonic-gate 			ino = dp->gfsd_inode(dvp, off);
9227c478bd9Sstevel@tonic-gate 
9237c478bd9Sstevel@tonic-gate 			if ((error = gfs_readdir_emit(&gstate, uiop,
924b38f0970Sck153898 			    off, ino, dp->gfsd_static[off].gfse_name, 0))
9257c478bd9Sstevel@tonic-gate 			    != 0)
9267c478bd9Sstevel@tonic-gate 				break;
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate 		} else if (dp->gfsd_readdir) {
9297c478bd9Sstevel@tonic-gate 			off -= dp->gfsd_nstatic;
9307c478bd9Sstevel@tonic-gate 
9317c478bd9Sstevel@tonic-gate 			if ((error = dp->gfsd_readdir(dvp,
9327c478bd9Sstevel@tonic-gate 			    gstate.grd_dirent, &eof, &off, &next,
933b38f0970Sck153898 			    data, flags)) != 0 || eof)
9347c478bd9Sstevel@tonic-gate 				break;
9357c478bd9Sstevel@tonic-gate 
9367c478bd9Sstevel@tonic-gate 			off += dp->gfsd_nstatic + 2;
9377c478bd9Sstevel@tonic-gate 			next += dp->gfsd_nstatic + 2;
9387c478bd9Sstevel@tonic-gate 
9397c478bd9Sstevel@tonic-gate 			if ((error = gfs_readdir_emit_int(&gstate, uiop,
9403f480432Smaybee 			    next)) != 0)
9417c478bd9Sstevel@tonic-gate 				break;
9427c478bd9Sstevel@tonic-gate 		} else {
9437c478bd9Sstevel@tonic-gate 			/*
9447c478bd9Sstevel@tonic-gate 			 * Offset is beyond the end of the static entries, and
9457c478bd9Sstevel@tonic-gate 			 * we have no dynamic entries.  Set EOF.
9467c478bd9Sstevel@tonic-gate 			 */
9477c478bd9Sstevel@tonic-gate 			eof = 1;
9487c478bd9Sstevel@tonic-gate 		}
9497c478bd9Sstevel@tonic-gate 	}
9507c478bd9Sstevel@tonic-gate 
9517c478bd9Sstevel@tonic-gate 	return (gfs_readdir_fini(&gstate, error, eofp, eof));
9527c478bd9Sstevel@tonic-gate }
9537c478bd9Sstevel@tonic-gate 
9547c478bd9Sstevel@tonic-gate 
9557c478bd9Sstevel@tonic-gate /*
9567c478bd9Sstevel@tonic-gate  * gfs_vop_lookup: VOP_LOOKUP() entry point
9577c478bd9Sstevel@tonic-gate  *
9587c478bd9Sstevel@tonic-gate  * For use directly in vnode ops table.  Given a GFS directory, calls
9597c478bd9Sstevel@tonic-gate  * gfs_dir_lookup() as necessary.
9607c478bd9Sstevel@tonic-gate  */
9617c478bd9Sstevel@tonic-gate /* ARGSUSED */
9627c478bd9Sstevel@tonic-gate int
9637c478bd9Sstevel@tonic-gate gfs_vop_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, pathname_t *pnp,
964da6c28aaSamw     int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct,
965da6c28aaSamw     int *direntflags, pathname_t *realpnp)
9667c478bd9Sstevel@tonic-gate {
967da6c28aaSamw 	return (gfs_dir_lookup(dvp, nm, vpp, cr));
9687c478bd9Sstevel@tonic-gate }
9697c478bd9Sstevel@tonic-gate 
9707c478bd9Sstevel@tonic-gate /*
9717c478bd9Sstevel@tonic-gate  * gfs_vop_readdir: VOP_READDIR() entry point
9727c478bd9Sstevel@tonic-gate  *
9737c478bd9Sstevel@tonic-gate  * For use directly in vnode ops table.  Given a GFS directory, calls
9747c478bd9Sstevel@tonic-gate  * gfs_dir_readdir() as necessary.
9757c478bd9Sstevel@tonic-gate  */
9767c478bd9Sstevel@tonic-gate /* ARGSUSED */
9777c478bd9Sstevel@tonic-gate int
978da6c28aaSamw gfs_vop_readdir(vnode_t *vp, uio_t *uiop, cred_t *cr, int *eofp,
979da6c28aaSamw     caller_context_t *ct, int flags)
9807c478bd9Sstevel@tonic-gate {
981b38f0970Sck153898 	return (gfs_dir_readdir(vp, uiop, eofp, NULL, cr, ct, flags));
9827c478bd9Sstevel@tonic-gate }
9837c478bd9Sstevel@tonic-gate 
9847c478bd9Sstevel@tonic-gate 
9857c478bd9Sstevel@tonic-gate /*
9867c478bd9Sstevel@tonic-gate  * gfs_vop_map: VOP_MAP() entry point
9877c478bd9Sstevel@tonic-gate  *
9887c478bd9Sstevel@tonic-gate  * Convenient routine for handling pseudo-files that wish to allow mmap() calls.
9897c478bd9Sstevel@tonic-gate  * This function only works for readonly files, and uses the read function for
9907c478bd9Sstevel@tonic-gate  * the vnode to fill in the data.  The mapped data is immediately faulted in and
9917c478bd9Sstevel@tonic-gate  * filled with the necessary data during this call; there are no getpage() or
9927c478bd9Sstevel@tonic-gate  * putpage() routines.
9937c478bd9Sstevel@tonic-gate  */
9947c478bd9Sstevel@tonic-gate /* ARGSUSED */
9957c478bd9Sstevel@tonic-gate int
9967c478bd9Sstevel@tonic-gate gfs_vop_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp,
997da6c28aaSamw     size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cred,
998da6c28aaSamw     caller_context_t *ct)
9997c478bd9Sstevel@tonic-gate {
10007c478bd9Sstevel@tonic-gate 	int rv;
10017c478bd9Sstevel@tonic-gate 	ssize_t resid = len;
10027c478bd9Sstevel@tonic-gate 
10037c478bd9Sstevel@tonic-gate 	/*
10047c478bd9Sstevel@tonic-gate 	 * Check for bad parameters
10057c478bd9Sstevel@tonic-gate 	 */
10067c478bd9Sstevel@tonic-gate #ifdef _ILP32
10077c478bd9Sstevel@tonic-gate 	if (len > MAXOFF_T)
10087c478bd9Sstevel@tonic-gate 		return (ENOMEM);
10097c478bd9Sstevel@tonic-gate #endif
10107c478bd9Sstevel@tonic-gate 	if (vp->v_flag & VNOMAP)
10117c478bd9Sstevel@tonic-gate 		return (ENOTSUP);
10127c478bd9Sstevel@tonic-gate 	if (off > MAXOFF_T)
10137c478bd9Sstevel@tonic-gate 		return (EFBIG);
10147c478bd9Sstevel@tonic-gate 	if ((long)off < 0 || (long)(off + len) < 0)
10157c478bd9Sstevel@tonic-gate 		return (EINVAL);
10167c478bd9Sstevel@tonic-gate 	if (vp->v_type != VREG)
10177c478bd9Sstevel@tonic-gate 		return (ENODEV);
10187c478bd9Sstevel@tonic-gate 	if ((prot & (PROT_EXEC | PROT_WRITE)) != 0)
10197c478bd9Sstevel@tonic-gate 		return (EACCES);
10207c478bd9Sstevel@tonic-gate 
10217c478bd9Sstevel@tonic-gate 	/*
10227c478bd9Sstevel@tonic-gate 	 * Find appropriate address if needed, otherwise clear address range.
10237c478bd9Sstevel@tonic-gate 	 */
10247c478bd9Sstevel@tonic-gate 	as_rangelock(as);
1025*60946fe0Smec 	rv = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags);
1026*60946fe0Smec 	if (rv != 0) {
10277c478bd9Sstevel@tonic-gate 		as_rangeunlock(as);
1028*60946fe0Smec 		return (rv);
10297c478bd9Sstevel@tonic-gate 	}
10307c478bd9Sstevel@tonic-gate 
10317c478bd9Sstevel@tonic-gate 	/*
10327c478bd9Sstevel@tonic-gate 	 * Create mapping
10337c478bd9Sstevel@tonic-gate 	 */
10347c478bd9Sstevel@tonic-gate 	rv = as_map(as, *addrp, len, segvn_create, zfod_argsp);
10357c478bd9Sstevel@tonic-gate 	as_rangeunlock(as);
10367c478bd9Sstevel@tonic-gate 	if (rv != 0)
10377c478bd9Sstevel@tonic-gate 		return (rv);
10387c478bd9Sstevel@tonic-gate 
10397c478bd9Sstevel@tonic-gate 	/*
10407c478bd9Sstevel@tonic-gate 	 * Fill with data from read()
10417c478bd9Sstevel@tonic-gate 	 */
10427c478bd9Sstevel@tonic-gate 	rv = vn_rdwr(UIO_READ, vp, *addrp, len, off, UIO_USERSPACE,
10437c478bd9Sstevel@tonic-gate 	    0, (rlim64_t)0, cred, &resid);
10447c478bd9Sstevel@tonic-gate 
10457c478bd9Sstevel@tonic-gate 	if (rv == 0 && resid != 0)
10467c478bd9Sstevel@tonic-gate 		rv = ENXIO;
10477c478bd9Sstevel@tonic-gate 
10487c478bd9Sstevel@tonic-gate 	if (rv != 0) {
10497c478bd9Sstevel@tonic-gate 		as_rangelock(as);
10507c478bd9Sstevel@tonic-gate 		(void) as_unmap(as, *addrp, len);
10517c478bd9Sstevel@tonic-gate 		as_rangeunlock(as);
10527c478bd9Sstevel@tonic-gate 	}
10537c478bd9Sstevel@tonic-gate 
10547c478bd9Sstevel@tonic-gate 	return (rv);
10557c478bd9Sstevel@tonic-gate }
10567c478bd9Sstevel@tonic-gate 
10577c478bd9Sstevel@tonic-gate /*
10587c478bd9Sstevel@tonic-gate  * gfs_vop_inactive: VOP_INACTIVE() entry point
10597c478bd9Sstevel@tonic-gate  *
10607c478bd9Sstevel@tonic-gate  * Given a vnode that is a GFS file or directory, call gfs_file_inactive() or
10617c478bd9Sstevel@tonic-gate  * gfs_dir_inactive() as necessary, and kmem_free()s associated private data.
10627c478bd9Sstevel@tonic-gate  */
10637c478bd9Sstevel@tonic-gate /* ARGSUSED */
10647c478bd9Sstevel@tonic-gate void
1065da6c28aaSamw gfs_vop_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
10667c478bd9Sstevel@tonic-gate {
10677c478bd9Sstevel@tonic-gate 	gfs_file_t *fp = vp->v_data;
10687c478bd9Sstevel@tonic-gate 	void *data;
10697c478bd9Sstevel@tonic-gate 
10707c478bd9Sstevel@tonic-gate 	if (fp->gfs_type == GFS_DIR)
10717c478bd9Sstevel@tonic-gate 		data = gfs_dir_inactive(vp);
10727c478bd9Sstevel@tonic-gate 	else
10737c478bd9Sstevel@tonic-gate 		data = gfs_file_inactive(vp);
10747c478bd9Sstevel@tonic-gate 
10757c478bd9Sstevel@tonic-gate 	if (data != NULL)
10767c478bd9Sstevel@tonic-gate 		kmem_free(data, fp->gfs_size);
10777c478bd9Sstevel@tonic-gate }
1078