xref: /titanic_51/usr/src/uts/common/fs/gfs.c (revision ab04eb8ef60d9dc9614d6cccffc474f24ca1d162)
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 /*
2360946fe0Smec  * 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>
38*ab04eb8eStimh #include <sys/sunddi.h>
397c478bd9Sstevel@tonic-gate #include <sys/uio.h>
407c478bd9Sstevel@tonic-gate #include <sys/vmsystm.h>
417c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
427c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #include <vm/as.h>
457c478bd9Sstevel@tonic-gate #include <vm/seg_vn.h>
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate #include <sys/gfs.h>
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate /*
507c478bd9Sstevel@tonic-gate  * Generic pseudo-filesystem routines.
517c478bd9Sstevel@tonic-gate  *
527c478bd9Sstevel@tonic-gate  * There are significant similarities between the implementation of certain file
537c478bd9Sstevel@tonic-gate  * system entry points across different filesystems.  While one could attempt to
547c478bd9Sstevel@tonic-gate  * "choke up on the bat" and incorporate common functionality into a VOP
55092e3d7cSjk115741  * preamble or postamble, such an approach is limited in the benefit it can
567c478bd9Sstevel@tonic-gate  * provide.  In this file we instead define a toolkit of routines which can be
577c478bd9Sstevel@tonic-gate  * called from a filesystem (with in-kernel pseudo-filesystems being the focus
587c478bd9Sstevel@tonic-gate  * of the exercise) in a more component-like fashion.
597c478bd9Sstevel@tonic-gate  *
607c478bd9Sstevel@tonic-gate  * There are three basic classes of routines:
617c478bd9Sstevel@tonic-gate  *
627c478bd9Sstevel@tonic-gate  * 1) Lowlevel support routines
637c478bd9Sstevel@tonic-gate  *
647c478bd9Sstevel@tonic-gate  *    These routines are designed to play a support role for existing
657c478bd9Sstevel@tonic-gate  *    pseudo-filesystems (such as procfs).  They simplify common tasks,
66da6c28aaSamw  *    without forcing the filesystem to hand over management to GFS.  The
677c478bd9Sstevel@tonic-gate  *    routines covered are:
687c478bd9Sstevel@tonic-gate  *
697c478bd9Sstevel@tonic-gate  *	gfs_readdir_init()
707c478bd9Sstevel@tonic-gate  *	gfs_readdir_emit()
717c478bd9Sstevel@tonic-gate  *	gfs_readdir_emitn()
727c478bd9Sstevel@tonic-gate  *	gfs_readdir_pred()
737c478bd9Sstevel@tonic-gate  *	gfs_readdir_fini()
747c478bd9Sstevel@tonic-gate  *	gfs_lookup_dot()
757c478bd9Sstevel@tonic-gate  *
767c478bd9Sstevel@tonic-gate  * 2) Complete GFS management
777c478bd9Sstevel@tonic-gate  *
787c478bd9Sstevel@tonic-gate  *    These routines take a more active role in management of the
797c478bd9Sstevel@tonic-gate  *    pseudo-filesystem.  They handle the relationship between vnode private
807c478bd9Sstevel@tonic-gate  *    data and VFS data, as well as the relationship between vnodes in the
81092e3d7cSjk115741  *    directory hierarchy.
827c478bd9Sstevel@tonic-gate  *
837c478bd9Sstevel@tonic-gate  *    In order to use these interfaces, the first member of every private
847c478bd9Sstevel@tonic-gate  *    v_data must be a gfs_file_t or a gfs_dir_t.  This hands over all control
857c478bd9Sstevel@tonic-gate  *    to GFS.
867c478bd9Sstevel@tonic-gate  *
877c478bd9Sstevel@tonic-gate  * 	gfs_file_create()
887c478bd9Sstevel@tonic-gate  * 	gfs_dir_create()
897c478bd9Sstevel@tonic-gate  * 	gfs_root_create()
907c478bd9Sstevel@tonic-gate  *
917c478bd9Sstevel@tonic-gate  *	gfs_file_inactive()
927c478bd9Sstevel@tonic-gate  *	gfs_dir_inactive()
937c478bd9Sstevel@tonic-gate  *	gfs_dir_lookup()
947c478bd9Sstevel@tonic-gate  *	gfs_dir_readdir()
957c478bd9Sstevel@tonic-gate  *
967c478bd9Sstevel@tonic-gate  * 	gfs_vop_inactive()
977c478bd9Sstevel@tonic-gate  * 	gfs_vop_lookup()
987c478bd9Sstevel@tonic-gate  * 	gfs_vop_readdir()
997c478bd9Sstevel@tonic-gate  * 	gfs_vop_map()
100a237e38eSth199096  *
101a237e38eSth199096  * 3) Single File pseudo-filesystems
102a237e38eSth199096  *
103a237e38eSth199096  *    This routine creates a rooted file to be overlayed ontop of another
104a237e38eSth199096  *    file in the physical filespace.
105a237e38eSth199096  *
106a237e38eSth199096  *    Note that the parent is NULL (actually the vfs), but there is nothing
107a237e38eSth199096  *    technically keeping such a file from utilizing the "Complete GFS
108a237e38eSth199096  *    management" set of routines.
109a237e38eSth199096  *
110a237e38eSth199096  * 	gfs_root_create_file()
1117c478bd9Sstevel@tonic-gate  */
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate /*
1147c478bd9Sstevel@tonic-gate  * gfs_make_opsvec: take an array of vnode type definitions and create
1157c478bd9Sstevel@tonic-gate  * their vnodeops_t structures
1167c478bd9Sstevel@tonic-gate  *
1177c478bd9Sstevel@tonic-gate  * This routine takes an array of gfs_opsvec_t's.  It could
1187c478bd9Sstevel@tonic-gate  * alternatively take an array of gfs_opsvec_t*'s, which would allow
1197c478bd9Sstevel@tonic-gate  * vnode types to be completely defined in files external to the caller
1207c478bd9Sstevel@tonic-gate  * of gfs_make_opsvec().  As it stands, much more sharing takes place --
1217c478bd9Sstevel@tonic-gate  * both the caller and the vnode type provider need to access gfsv_ops
1227c478bd9Sstevel@tonic-gate  * and gfsv_template, and the caller also needs to know gfsv_name.
1237c478bd9Sstevel@tonic-gate  */
1247c478bd9Sstevel@tonic-gate int
1257c478bd9Sstevel@tonic-gate gfs_make_opsvec(gfs_opsvec_t *vec)
1267c478bd9Sstevel@tonic-gate {
1277c478bd9Sstevel@tonic-gate 	int error, i;
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	for (i = 0; ; i++) {
1307c478bd9Sstevel@tonic-gate 		if (vec[i].gfsv_name == NULL)
1317c478bd9Sstevel@tonic-gate 			return (0);
1327c478bd9Sstevel@tonic-gate 		error = vn_make_ops(vec[i].gfsv_name, vec[i].gfsv_template,
1337c478bd9Sstevel@tonic-gate 		    vec[i].gfsv_ops);
1347c478bd9Sstevel@tonic-gate 		if (error)
1357c478bd9Sstevel@tonic-gate 			break;
1367c478bd9Sstevel@tonic-gate 	}
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	cmn_err(CE_WARN, "gfs_make_opsvec: bad vnode ops template for '%s'",
1397c478bd9Sstevel@tonic-gate 	    vec[i].gfsv_name);
1407c478bd9Sstevel@tonic-gate 	for (i--; i >= 0; i--) {
1417c478bd9Sstevel@tonic-gate 		vn_freevnodeops(*vec[i].gfsv_ops);
1427c478bd9Sstevel@tonic-gate 		*vec[i].gfsv_ops = NULL;
1437c478bd9Sstevel@tonic-gate 	}
1447c478bd9Sstevel@tonic-gate 	return (error);
1457c478bd9Sstevel@tonic-gate }
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate /*
1487c478bd9Sstevel@tonic-gate  * Low level directory routines
1497c478bd9Sstevel@tonic-gate  *
1507c478bd9Sstevel@tonic-gate  * These routines provide some simple abstractions for reading directories.
1517c478bd9Sstevel@tonic-gate  * They are designed to be used by existing pseudo filesystems (namely procfs)
1527c478bd9Sstevel@tonic-gate  * that already have a complicated management infrastructure.
1537c478bd9Sstevel@tonic-gate  */
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate /*
156b38f0970Sck153898  * gfs_get_parent_ino: used to obtain a parent inode number and the
157b38f0970Sck153898  * inode number of the given vnode in preparation for calling gfs_readdir_init.
158b38f0970Sck153898  */
159b38f0970Sck153898 int
160b38f0970Sck153898 gfs_get_parent_ino(vnode_t *dvp, cred_t *cr, caller_context_t *ct,
161b38f0970Sck153898     ino64_t *pino, ino64_t *ino)
162b38f0970Sck153898 {
163b38f0970Sck153898 	vnode_t *parent;
164b38f0970Sck153898 	gfs_dir_t *dp = dvp->v_data;
165b38f0970Sck153898 	int error;
166b38f0970Sck153898 
167b38f0970Sck153898 	*ino = dp->gfsd_file.gfs_ino;
168b38f0970Sck153898 	parent = dp->gfsd_file.gfs_parent;
169b38f0970Sck153898 
170b38f0970Sck153898 	if (parent == NULL) {
171b38f0970Sck153898 		*pino = *ino;		/* root of filesystem */
172b38f0970Sck153898 	} else if (dvp->v_flag & V_XATTRDIR) {
173b38f0970Sck153898 		vattr_t va;
174b38f0970Sck153898 
175b38f0970Sck153898 		va.va_mask = AT_NODEID;
176b38f0970Sck153898 		error = VOP_GETATTR(parent, &va, 0, cr, ct);
177b38f0970Sck153898 		if (error)
178b38f0970Sck153898 			return (error);
179b38f0970Sck153898 		*pino = va.va_nodeid;
180b38f0970Sck153898 	} else {
181b38f0970Sck153898 		*pino = ((gfs_file_t *)(parent->v_data))->gfs_ino;
182b38f0970Sck153898 	}
183b38f0970Sck153898 
184b38f0970Sck153898 	return (0);
185b38f0970Sck153898 }
186b38f0970Sck153898 
187b38f0970Sck153898 /*
1887c478bd9Sstevel@tonic-gate  * gfs_readdir_init: initiate a generic readdir
1897c478bd9Sstevel@tonic-gate  *   st		- a pointer to an uninitialized gfs_readdir_state_t structure
1907c478bd9Sstevel@tonic-gate  *   name_max	- the directory's maximum file name length
1917c478bd9Sstevel@tonic-gate  *   ureclen	- the exported file-space record length (1 for non-legacy FSs)
1927c478bd9Sstevel@tonic-gate  *   uiop	- the uiop passed to readdir
1937c478bd9Sstevel@tonic-gate  *   parent	- the parent directory's inode
1947c478bd9Sstevel@tonic-gate  *   self	- this directory's inode
195b38f0970Sck153898  *   flags	- flags from VOP_READDIR
1967c478bd9Sstevel@tonic-gate  *
1977c478bd9Sstevel@tonic-gate  * Returns 0 or a non-zero errno.
1987c478bd9Sstevel@tonic-gate  *
1997c478bd9Sstevel@tonic-gate  * Typical VOP_READDIR usage of gfs_readdir_*:
2007c478bd9Sstevel@tonic-gate  *
2017c478bd9Sstevel@tonic-gate  *	if ((error = gfs_readdir_init(...)) != 0)
2027c478bd9Sstevel@tonic-gate  *		return (error);
2037c478bd9Sstevel@tonic-gate  *	eof = 0;
2047c478bd9Sstevel@tonic-gate  *	while ((error = gfs_readdir_pred(..., &voffset)) != 0) {
2057c478bd9Sstevel@tonic-gate  *		if (!consumer_entry_at(voffset))
2067c478bd9Sstevel@tonic-gate  *			voffset = consumer_next_entry(voffset);
2077c478bd9Sstevel@tonic-gate  *		if (consumer_eof(voffset)) {
2087c478bd9Sstevel@tonic-gate  *			eof = 1
2097c478bd9Sstevel@tonic-gate  *			break;
2107c478bd9Sstevel@tonic-gate  *		}
2117c478bd9Sstevel@tonic-gate  *		if ((error = gfs_readdir_emit(..., voffset,
2127c478bd9Sstevel@tonic-gate  *		    consumer_ino(voffset), consumer_name(voffset))) != 0)
2137c478bd9Sstevel@tonic-gate  *			break;
2147c478bd9Sstevel@tonic-gate  *	}
2157c478bd9Sstevel@tonic-gate  *	return (gfs_readdir_fini(..., error, eofp, eof));
2167c478bd9Sstevel@tonic-gate  *
2177c478bd9Sstevel@tonic-gate  * As you can see, a zero result from gfs_readdir_pred() or
2187c478bd9Sstevel@tonic-gate  * gfs_readdir_emit() indicates that processing should continue,
2197c478bd9Sstevel@tonic-gate  * whereas a non-zero result indicates that the loop should terminate.
2207c478bd9Sstevel@tonic-gate  * Most consumers need do nothing more than let gfs_readdir_fini()
2217c478bd9Sstevel@tonic-gate  * determine what the cause of failure was and return the appropriate
2227c478bd9Sstevel@tonic-gate  * value.
2237c478bd9Sstevel@tonic-gate  */
2247c478bd9Sstevel@tonic-gate int
2257c478bd9Sstevel@tonic-gate gfs_readdir_init(gfs_readdir_state_t *st, int name_max, int ureclen,
226b38f0970Sck153898     uio_t *uiop, ino64_t parent, ino64_t self, int flags)
2277c478bd9Sstevel@tonic-gate {
228b38f0970Sck153898 	size_t dirent_size;
229b38f0970Sck153898 
2307c478bd9Sstevel@tonic-gate 	if (uiop->uio_loffset < 0 || uiop->uio_resid <= 0 ||
2317c478bd9Sstevel@tonic-gate 	    (uiop->uio_loffset % ureclen) != 0)
2327c478bd9Sstevel@tonic-gate 		return (EINVAL);
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	st->grd_ureclen = ureclen;
2357c478bd9Sstevel@tonic-gate 	st->grd_oresid = uiop->uio_resid;
2367c478bd9Sstevel@tonic-gate 	st->grd_namlen = name_max;
237b38f0970Sck153898 	if (flags & V_RDDIR_ENTFLAGS)
238b38f0970Sck153898 		dirent_size = EDIRENT_RECLEN(st->grd_namlen);
239b38f0970Sck153898 	else
240b38f0970Sck153898 		dirent_size = DIRENT64_RECLEN(st->grd_namlen);
241b38f0970Sck153898 	st->grd_dirent = kmem_zalloc(dirent_size, KM_SLEEP);
2427c478bd9Sstevel@tonic-gate 	st->grd_parent = parent;
2437c478bd9Sstevel@tonic-gate 	st->grd_self = self;
244b38f0970Sck153898 	st->grd_flags = flags;
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	return (0);
2477c478bd9Sstevel@tonic-gate }
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate /*
2507c478bd9Sstevel@tonic-gate  * gfs_readdir_emit_int: internal routine to emit directory entry
2517c478bd9Sstevel@tonic-gate  *
252b38f0970Sck153898  *   st		- the current readdir state, which must have d_ino/ed_ino
253b38f0970Sck153898  *		  and d_name/ed_name set
2547c478bd9Sstevel@tonic-gate  *   uiop	- caller-supplied uio pointer
2557c478bd9Sstevel@tonic-gate  *   next	- the offset of the next entry
2567c478bd9Sstevel@tonic-gate  */
2577c478bd9Sstevel@tonic-gate static int
2583f480432Smaybee gfs_readdir_emit_int(gfs_readdir_state_t *st, uio_t *uiop, offset_t next)
2597c478bd9Sstevel@tonic-gate {
2607c478bd9Sstevel@tonic-gate 	int reclen;
261b38f0970Sck153898 	dirent64_t *dp;
262b38f0970Sck153898 	edirent_t *edp;
2637c478bd9Sstevel@tonic-gate 
264b38f0970Sck153898 	if (st->grd_flags & V_RDDIR_ENTFLAGS) {
265b38f0970Sck153898 		edp = st->grd_dirent;
266b38f0970Sck153898 		reclen = EDIRENT_RECLEN(strlen(edp->ed_name));
267b38f0970Sck153898 	} else {
268b38f0970Sck153898 		dp = st->grd_dirent;
269b38f0970Sck153898 		reclen = DIRENT64_RECLEN(strlen(dp->d_name));
270b38f0970Sck153898 	}
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 	if (reclen > uiop->uio_resid) {
2737c478bd9Sstevel@tonic-gate 		/*
2747c478bd9Sstevel@tonic-gate 		 * Error if no entries were returned yet
2757c478bd9Sstevel@tonic-gate 		 */
2767c478bd9Sstevel@tonic-gate 		if (uiop->uio_resid == st->grd_oresid)
2777c478bd9Sstevel@tonic-gate 			return (EINVAL);
2787c478bd9Sstevel@tonic-gate 		return (-1);
2797c478bd9Sstevel@tonic-gate 	}
2807c478bd9Sstevel@tonic-gate 
281b38f0970Sck153898 	if (st->grd_flags & V_RDDIR_ENTFLAGS) {
282b38f0970Sck153898 		edp->ed_off = next;
283b38f0970Sck153898 		edp->ed_reclen = (ushort_t)reclen;
284b38f0970Sck153898 	} else {
285b38f0970Sck153898 		dp->d_off = next;
286b38f0970Sck153898 		dp->d_reclen = (ushort_t)reclen;
287b38f0970Sck153898 	}
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 	if (uiomove((caddr_t)st->grd_dirent, reclen, UIO_READ, uiop))
2907c478bd9Sstevel@tonic-gate 		return (EFAULT);
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 	uiop->uio_loffset = next;
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 	return (0);
2957c478bd9Sstevel@tonic-gate }
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate /*
2987c478bd9Sstevel@tonic-gate  * gfs_readdir_emit: emit a directory entry
2997c478bd9Sstevel@tonic-gate  *   voff       - the virtual offset (obtained from gfs_readdir_pred)
3007c478bd9Sstevel@tonic-gate  *   ino        - the entry's inode
3017c478bd9Sstevel@tonic-gate  *   name       - the entry's name
302b38f0970Sck153898  *   eflags	- value for ed_eflags (if processing edirent_t)
3037c478bd9Sstevel@tonic-gate  *
3047c478bd9Sstevel@tonic-gate  * Returns a 0 on success, a non-zero errno on failure, or -1 if the
3057c478bd9Sstevel@tonic-gate  * readdir loop should terminate.  A non-zero result (either errno or
3067c478bd9Sstevel@tonic-gate  * -1) from this function is typically passed directly to
3077c478bd9Sstevel@tonic-gate  * gfs_readdir_fini().
3087c478bd9Sstevel@tonic-gate  */
3097c478bd9Sstevel@tonic-gate int
3107c478bd9Sstevel@tonic-gate gfs_readdir_emit(gfs_readdir_state_t *st, uio_t *uiop, offset_t voff,
311b38f0970Sck153898     ino64_t ino, const char *name, int eflags)
3127c478bd9Sstevel@tonic-gate {
3137c478bd9Sstevel@tonic-gate 	offset_t off = (voff + 2) * st->grd_ureclen;
3147c478bd9Sstevel@tonic-gate 
315b38f0970Sck153898 	if (st->grd_flags & V_RDDIR_ENTFLAGS) {
316b38f0970Sck153898 		edirent_t *edp = st->grd_dirent;
317b38f0970Sck153898 
318b38f0970Sck153898 		edp->ed_ino = ino;
319b38f0970Sck153898 		(void) strncpy(edp->ed_name, name, st->grd_namlen);
320b38f0970Sck153898 		edp->ed_eflags = eflags;
321b38f0970Sck153898 	} else {
322b38f0970Sck153898 		dirent64_t *dp = st->grd_dirent;
323b38f0970Sck153898 
324b38f0970Sck153898 		dp->d_ino = ino;
325b38f0970Sck153898 		(void) strncpy(dp->d_name, name, st->grd_namlen);
326b38f0970Sck153898 	}
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 	/*
3297c478bd9Sstevel@tonic-gate 	 * Inter-entry offsets are invalid, so we assume a record size of
3307c478bd9Sstevel@tonic-gate 	 * grd_ureclen and explicitly set the offset appropriately.
3317c478bd9Sstevel@tonic-gate 	 */
3323f480432Smaybee 	return (gfs_readdir_emit_int(st, uiop, off + st->grd_ureclen));
3337c478bd9Sstevel@tonic-gate }
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate /*
3367c478bd9Sstevel@tonic-gate  * gfs_readdir_emitn: like gfs_readdir_emit(), but takes an integer
3377c478bd9Sstevel@tonic-gate  * instead of a string for the entry's name.
3387c478bd9Sstevel@tonic-gate  */
3397c478bd9Sstevel@tonic-gate int
3407c478bd9Sstevel@tonic-gate gfs_readdir_emitn(gfs_readdir_state_t *st, uio_t *uiop, offset_t voff,
3417c478bd9Sstevel@tonic-gate     ino64_t ino, unsigned long num)
3427c478bd9Sstevel@tonic-gate {
3437c478bd9Sstevel@tonic-gate 	char buf[40];
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate 	numtos(num, buf);
346b38f0970Sck153898 	return (gfs_readdir_emit(st, uiop, voff, ino, buf, 0));
3477c478bd9Sstevel@tonic-gate }
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate /*
3507c478bd9Sstevel@tonic-gate  * gfs_readdir_pred: readdir loop predicate
3517c478bd9Sstevel@tonic-gate  *   voffp - a pointer in which the next virtual offset should be stored
3527c478bd9Sstevel@tonic-gate  *
3537c478bd9Sstevel@tonic-gate  * Returns a 0 on success, a non-zero errno on failure, or -1 if the
3547c478bd9Sstevel@tonic-gate  * readdir loop should terminate.  A non-zero result (either errno or
3557c478bd9Sstevel@tonic-gate  * -1) from this function is typically passed directly to
3567c478bd9Sstevel@tonic-gate  * gfs_readdir_fini().
3577c478bd9Sstevel@tonic-gate  */
3587c478bd9Sstevel@tonic-gate int
3597c478bd9Sstevel@tonic-gate gfs_readdir_pred(gfs_readdir_state_t *st, uio_t *uiop, offset_t *voffp)
3607c478bd9Sstevel@tonic-gate {
3617c478bd9Sstevel@tonic-gate 	offset_t off, voff;
3627c478bd9Sstevel@tonic-gate 	int error;
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate top:
3657c478bd9Sstevel@tonic-gate 	if (uiop->uio_resid <= 0)
3667c478bd9Sstevel@tonic-gate 		return (-1);
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 	off = uiop->uio_loffset / st->grd_ureclen;
3697c478bd9Sstevel@tonic-gate 	voff = off - 2;
3707c478bd9Sstevel@tonic-gate 	if (off == 0) {
3717c478bd9Sstevel@tonic-gate 		if ((error = gfs_readdir_emit(st, uiop, voff, st->grd_self,
372b38f0970Sck153898 		    ".", 0)) == 0)
3737c478bd9Sstevel@tonic-gate 			goto top;
3747c478bd9Sstevel@tonic-gate 	} else if (off == 1) {
3757c478bd9Sstevel@tonic-gate 		if ((error = gfs_readdir_emit(st, uiop, voff, st->grd_parent,
376b38f0970Sck153898 		    "..", 0)) == 0)
3777c478bd9Sstevel@tonic-gate 			goto top;
3787c478bd9Sstevel@tonic-gate 	} else {
3797c478bd9Sstevel@tonic-gate 		*voffp = voff;
3807c478bd9Sstevel@tonic-gate 		return (0);
3817c478bd9Sstevel@tonic-gate 	}
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate 	return (error);
3847c478bd9Sstevel@tonic-gate }
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate /*
3877c478bd9Sstevel@tonic-gate  * gfs_readdir_fini: generic readdir cleanup
3887c478bd9Sstevel@tonic-gate  *   error	- if positive, an error to return
3897c478bd9Sstevel@tonic-gate  *   eofp	- the eofp passed to readdir
3907c478bd9Sstevel@tonic-gate  *   eof	- the eof value
3917c478bd9Sstevel@tonic-gate  *
3927c478bd9Sstevel@tonic-gate  * Returns a 0 on success, a non-zero errno on failure.  This result
3937c478bd9Sstevel@tonic-gate  * should be returned from readdir.
3947c478bd9Sstevel@tonic-gate  */
3957c478bd9Sstevel@tonic-gate int
3967c478bd9Sstevel@tonic-gate gfs_readdir_fini(gfs_readdir_state_t *st, int error, int *eofp, int eof)
3977c478bd9Sstevel@tonic-gate {
398b38f0970Sck153898 	size_t dirent_size;
399b38f0970Sck153898 
400b38f0970Sck153898 	if (st->grd_flags & V_RDDIR_ENTFLAGS)
401b38f0970Sck153898 		dirent_size = EDIRENT_RECLEN(st->grd_namlen);
402b38f0970Sck153898 	else
403b38f0970Sck153898 		dirent_size = DIRENT64_RECLEN(st->grd_namlen);
404b38f0970Sck153898 	kmem_free(st->grd_dirent, dirent_size);
4057c478bd9Sstevel@tonic-gate 	if (error > 0)
4067c478bd9Sstevel@tonic-gate 		return (error);
4077c478bd9Sstevel@tonic-gate 	if (eofp)
4087c478bd9Sstevel@tonic-gate 		*eofp = eof;
4097c478bd9Sstevel@tonic-gate 	return (0);
4107c478bd9Sstevel@tonic-gate }
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate /*
4137c478bd9Sstevel@tonic-gate  * gfs_lookup_dot
4147c478bd9Sstevel@tonic-gate  *
4157c478bd9Sstevel@tonic-gate  * Performs a basic check for "." and ".." directory entries.
4167c478bd9Sstevel@tonic-gate  */
4177c478bd9Sstevel@tonic-gate int
4187c478bd9Sstevel@tonic-gate gfs_lookup_dot(vnode_t **vpp, vnode_t *dvp, vnode_t *pvp, const char *nm)
4197c478bd9Sstevel@tonic-gate {
4207c478bd9Sstevel@tonic-gate 	if (*nm == '\0' || strcmp(nm, ".") == 0) {
4217c478bd9Sstevel@tonic-gate 		VN_HOLD(dvp);
4227c478bd9Sstevel@tonic-gate 		*vpp = dvp;
4237c478bd9Sstevel@tonic-gate 		return (0);
4247c478bd9Sstevel@tonic-gate 	} else if (strcmp(nm, "..") == 0) {
4257c478bd9Sstevel@tonic-gate 		if (pvp == NULL) {
4267c478bd9Sstevel@tonic-gate 			ASSERT(dvp->v_flag & VROOT);
4277c478bd9Sstevel@tonic-gate 			VN_HOLD(dvp);
4287c478bd9Sstevel@tonic-gate 			*vpp = dvp;
4297c478bd9Sstevel@tonic-gate 		} else {
4307c478bd9Sstevel@tonic-gate 			VN_HOLD(pvp);
4317c478bd9Sstevel@tonic-gate 			*vpp = pvp;
4327c478bd9Sstevel@tonic-gate 		}
4337c478bd9Sstevel@tonic-gate 		return (0);
4347c478bd9Sstevel@tonic-gate 	}
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 	return (-1);
4377c478bd9Sstevel@tonic-gate }
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate /*
4407c478bd9Sstevel@tonic-gate  * gfs_file_create(): create a new GFS file
4417c478bd9Sstevel@tonic-gate  *
4427c478bd9Sstevel@tonic-gate  *   size	- size of private data structure (v_data)
4437c478bd9Sstevel@tonic-gate  *   pvp	- parent vnode (GFS directory)
4447c478bd9Sstevel@tonic-gate  *   ops	- vnode operations vector
4457c478bd9Sstevel@tonic-gate  *
4467c478bd9Sstevel@tonic-gate  * In order to use this interface, the parent vnode must have been created by
4477c478bd9Sstevel@tonic-gate  * gfs_dir_create(), and the private data stored in v_data must have a
4487c478bd9Sstevel@tonic-gate  * 'gfs_file_t' as its first field.
4497c478bd9Sstevel@tonic-gate  *
4507c478bd9Sstevel@tonic-gate  * Given these constraints, this routine will automatically:
4517c478bd9Sstevel@tonic-gate  *
4527c478bd9Sstevel@tonic-gate  * 	- Allocate v_data for the vnode
4537c478bd9Sstevel@tonic-gate  * 	- Initialize necessary fields in the vnode
4547c478bd9Sstevel@tonic-gate  * 	- Hold the parent
4557c478bd9Sstevel@tonic-gate  */
4567c478bd9Sstevel@tonic-gate vnode_t *
4577c478bd9Sstevel@tonic-gate gfs_file_create(size_t size, vnode_t *pvp, vnodeops_t *ops)
4587c478bd9Sstevel@tonic-gate {
4597c478bd9Sstevel@tonic-gate 	gfs_file_t *fp;
4607c478bd9Sstevel@tonic-gate 	vnode_t *vp;
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 	/*
4637c478bd9Sstevel@tonic-gate 	 * Allocate vnode and internal data structure
4647c478bd9Sstevel@tonic-gate 	 */
4657c478bd9Sstevel@tonic-gate 	fp = kmem_zalloc(size, KM_SLEEP);
4667c478bd9Sstevel@tonic-gate 	vp = vn_alloc(KM_SLEEP);
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate 	/*
4697c478bd9Sstevel@tonic-gate 	 * Set up various pointers
4707c478bd9Sstevel@tonic-gate 	 */
4717c478bd9Sstevel@tonic-gate 	fp->gfs_vnode = vp;
4727c478bd9Sstevel@tonic-gate 	fp->gfs_parent = pvp;
4737c478bd9Sstevel@tonic-gate 	vp->v_data = fp;
4747c478bd9Sstevel@tonic-gate 	fp->gfs_size = size;
4757c478bd9Sstevel@tonic-gate 	fp->gfs_type = GFS_FILE;
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 	/*
4787c478bd9Sstevel@tonic-gate 	 * Initialize vnode and hold parent.
4797c478bd9Sstevel@tonic-gate 	 */
4807c478bd9Sstevel@tonic-gate 	vn_setops(vp, ops);
4817c478bd9Sstevel@tonic-gate 	if (pvp) {
4827c478bd9Sstevel@tonic-gate 		VN_SET_VFS_TYPE_DEV(vp, pvp->v_vfsp, VREG, 0);
4837c478bd9Sstevel@tonic-gate 		VN_HOLD(pvp);
4847c478bd9Sstevel@tonic-gate 	}
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate 	return (vp);
4877c478bd9Sstevel@tonic-gate }
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate /*
4907c478bd9Sstevel@tonic-gate  * gfs_dir_create: creates a new directory in the parent
4917c478bd9Sstevel@tonic-gate  *
4927c478bd9Sstevel@tonic-gate  *   size	- size of private data structure (v_data)
4937c478bd9Sstevel@tonic-gate  *   pvp	- parent vnode (GFS directory)
4947c478bd9Sstevel@tonic-gate  *   ops	- vnode operations vector
4957c478bd9Sstevel@tonic-gate  *   entries	- NULL-terminated list of static entries (if any)
4967c478bd9Sstevel@tonic-gate  *   maxlen	- maximum length of a directory entry
4977c478bd9Sstevel@tonic-gate  *   readdir_cb	- readdir callback (see gfs_dir_readdir)
4987c478bd9Sstevel@tonic-gate  *   inode_cb	- inode callback (see gfs_dir_readdir)
4997c478bd9Sstevel@tonic-gate  *   lookup_cb	- lookup callback (see gfs_dir_lookup)
5007c478bd9Sstevel@tonic-gate  *
5017c478bd9Sstevel@tonic-gate  * In order to use this function, the first member of the private vnode
5027c478bd9Sstevel@tonic-gate  * structure (v_data) must be a gfs_dir_t.  For each directory, there are
5037c478bd9Sstevel@tonic-gate  * static entries, defined when the structure is initialized, and dynamic
5047c478bd9Sstevel@tonic-gate  * entries, retrieved through callbacks.
5057c478bd9Sstevel@tonic-gate  *
5067c478bd9Sstevel@tonic-gate  * If a directory has static entries, then it must supply a inode callback,
5077c478bd9Sstevel@tonic-gate  * which will compute the inode number based on the parent and the index.
5087c478bd9Sstevel@tonic-gate  * For a directory with dynamic entries, the caller must supply a readdir
5097c478bd9Sstevel@tonic-gate  * callback and a lookup callback.  If a static lookup fails, we fall back to
5107c478bd9Sstevel@tonic-gate  * the supplied lookup callback, if any.
5117c478bd9Sstevel@tonic-gate  *
5127c478bd9Sstevel@tonic-gate  * This function also performs the same initialization as gfs_file_create().
5137c478bd9Sstevel@tonic-gate  */
5147c478bd9Sstevel@tonic-gate vnode_t *
5157c478bd9Sstevel@tonic-gate gfs_dir_create(size_t struct_size, vnode_t *pvp, vnodeops_t *ops,
5167c478bd9Sstevel@tonic-gate     gfs_dirent_t *entries, gfs_inode_cb inode_cb, int maxlen,
5177c478bd9Sstevel@tonic-gate     gfs_readdir_cb readdir_cb, gfs_lookup_cb lookup_cb)
5187c478bd9Sstevel@tonic-gate {
5197c478bd9Sstevel@tonic-gate 	vnode_t *vp;
5207c478bd9Sstevel@tonic-gate 	gfs_dir_t *dp;
5217c478bd9Sstevel@tonic-gate 	gfs_dirent_t *de;
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate 	vp = gfs_file_create(struct_size, pvp, ops);
5247c478bd9Sstevel@tonic-gate 	vp->v_type = VDIR;
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate 	dp = vp->v_data;
5277c478bd9Sstevel@tonic-gate 	dp->gfsd_file.gfs_type = GFS_DIR;
5287c478bd9Sstevel@tonic-gate 	dp->gfsd_maxlen = maxlen;
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate 	if (entries != NULL) {
5317c478bd9Sstevel@tonic-gate 		for (de = entries; de->gfse_name != NULL; de++)
5327c478bd9Sstevel@tonic-gate 			dp->gfsd_nstatic++;
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate 		dp->gfsd_static = kmem_alloc(
5357c478bd9Sstevel@tonic-gate 		    dp->gfsd_nstatic * sizeof (gfs_dirent_t), KM_SLEEP);
5367c478bd9Sstevel@tonic-gate 		bcopy(entries, dp->gfsd_static,
5377c478bd9Sstevel@tonic-gate 		    dp->gfsd_nstatic * sizeof (gfs_dirent_t));
5387c478bd9Sstevel@tonic-gate 	}
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate 	dp->gfsd_readdir = readdir_cb;
5417c478bd9Sstevel@tonic-gate 	dp->gfsd_lookup = lookup_cb;
5427c478bd9Sstevel@tonic-gate 	dp->gfsd_inode = inode_cb;
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 	mutex_init(&dp->gfsd_lock, NULL, MUTEX_DEFAULT, NULL);
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 	return (vp);
5477c478bd9Sstevel@tonic-gate }
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate /*
5507c478bd9Sstevel@tonic-gate  * gfs_root_create(): create a root vnode for a GFS filesystem
5517c478bd9Sstevel@tonic-gate  *
5527c478bd9Sstevel@tonic-gate  * Similar to gfs_dir_create(), this creates a root vnode for a filesystem.  The
5537c478bd9Sstevel@tonic-gate  * only difference is that it takes a vfs_t instead of a vnode_t as its parent.
5547c478bd9Sstevel@tonic-gate  */
5557c478bd9Sstevel@tonic-gate vnode_t *
5567c478bd9Sstevel@tonic-gate gfs_root_create(size_t size, vfs_t *vfsp, vnodeops_t *ops, ino64_t ino,
5577c478bd9Sstevel@tonic-gate     gfs_dirent_t *entries, gfs_inode_cb inode_cb, int maxlen,
5587c478bd9Sstevel@tonic-gate     gfs_readdir_cb readdir_cb, gfs_lookup_cb lookup_cb)
5597c478bd9Sstevel@tonic-gate {
5607c478bd9Sstevel@tonic-gate 	vnode_t *vp = gfs_dir_create(size, NULL, ops, entries, inode_cb,
5617c478bd9Sstevel@tonic-gate 	    maxlen, readdir_cb, lookup_cb);
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate 	/* Manually set the inode */
5647c478bd9Sstevel@tonic-gate 	((gfs_file_t *)vp->v_data)->gfs_ino = ino;
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 	VFS_HOLD(vfsp);
5677c478bd9Sstevel@tonic-gate 	VN_SET_VFS_TYPE_DEV(vp, vfsp, VDIR, 0);
5687c478bd9Sstevel@tonic-gate 	vp->v_flag |= VROOT | VNOCACHE | VNOMAP | VNOSWAP | VNOMOUNT;
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate 	return (vp);
5717c478bd9Sstevel@tonic-gate }
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate /*
574a237e38eSth199096  * gfs_root_create_file(): create a root vnode for a GFS file as a filesystem
575a237e38eSth199096  *
576a237e38eSth199096  * Similar to gfs_root_create(), this creates a root vnode for a file to
577a237e38eSth199096  * be the pseudo-filesystem.
578a237e38eSth199096  */
579a237e38eSth199096 vnode_t *
580a237e38eSth199096 gfs_root_create_file(size_t size, vfs_t *vfsp, vnodeops_t *ops, ino64_t ino)
581a237e38eSth199096 {
582a237e38eSth199096 	vnode_t	*vp = gfs_file_create(size, NULL, ops);
583a237e38eSth199096 
584a237e38eSth199096 	((gfs_file_t *)vp->v_data)->gfs_ino = ino;
585a237e38eSth199096 
586a237e38eSth199096 	VFS_HOLD(vfsp);
587a237e38eSth199096 	VN_SET_VFS_TYPE_DEV(vp, vfsp, VREG, 0);
588a237e38eSth199096 	vp->v_flag |= VROOT | VNOCACHE | VNOMAP | VNOSWAP | VNOMOUNT;
589a237e38eSth199096 
590a237e38eSth199096 	return (vp);
591a237e38eSth199096 }
592a237e38eSth199096 
593a237e38eSth199096 /*
5947c478bd9Sstevel@tonic-gate  * gfs_file_inactive()
5957c478bd9Sstevel@tonic-gate  *
5967c478bd9Sstevel@tonic-gate  * Called from the VOP_INACTIVE() routine.  If necessary, this routine will
5977c478bd9Sstevel@tonic-gate  * remove the given vnode from the parent directory and clean up any references
5987c478bd9Sstevel@tonic-gate  * in the VFS layer.
5997c478bd9Sstevel@tonic-gate  *
6007c478bd9Sstevel@tonic-gate  * If the vnode was not removed (due to a race with vget), then NULL is
6017c478bd9Sstevel@tonic-gate  * returned.  Otherwise, a pointer to the private data is returned.
6027c478bd9Sstevel@tonic-gate  */
6037c478bd9Sstevel@tonic-gate void *
6047c478bd9Sstevel@tonic-gate gfs_file_inactive(vnode_t *vp)
6057c478bd9Sstevel@tonic-gate {
6067c478bd9Sstevel@tonic-gate 	int i;
6077c478bd9Sstevel@tonic-gate 	gfs_dirent_t *ge = NULL;
6087c478bd9Sstevel@tonic-gate 	gfs_file_t *fp = vp->v_data;
6097c478bd9Sstevel@tonic-gate 	gfs_dir_t *dp = NULL;
6107c478bd9Sstevel@tonic-gate 	void *data;
6117c478bd9Sstevel@tonic-gate 
612da6c28aaSamw 	if (fp->gfs_parent == NULL || (vp->v_flag & V_XATTRDIR))
6137c478bd9Sstevel@tonic-gate 		goto found;
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate 	dp = fp->gfs_parent->v_data;
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate 	/*
6187c478bd9Sstevel@tonic-gate 	 * First, see if this vnode is cached in the parent.
6197c478bd9Sstevel@tonic-gate 	 */
6207c478bd9Sstevel@tonic-gate 	gfs_dir_lock(dp);
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate 	/*
6237c478bd9Sstevel@tonic-gate 	 * Find it in the set of static entries.
6247c478bd9Sstevel@tonic-gate 	 */
6257c478bd9Sstevel@tonic-gate 	for (i = 0; i < dp->gfsd_nstatic; i++)  {
6267c478bd9Sstevel@tonic-gate 		ge = &dp->gfsd_static[i];
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate 		if (ge->gfse_vnode == vp)
6297c478bd9Sstevel@tonic-gate 			goto found;
6307c478bd9Sstevel@tonic-gate 	}
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate 	/*
6337c478bd9Sstevel@tonic-gate 	 * If 'ge' is NULL, then it is a dynamic entry.
6347c478bd9Sstevel@tonic-gate 	 */
6357c478bd9Sstevel@tonic-gate 	ge = NULL;
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate found:
638da6c28aaSamw 	if (vp->v_flag & V_XATTRDIR) {
639da6c28aaSamw 		mutex_enter(&fp->gfs_parent->v_lock);
640da6c28aaSamw 	}
6417c478bd9Sstevel@tonic-gate 	mutex_enter(&vp->v_lock);
6427c478bd9Sstevel@tonic-gate 	if (vp->v_count == 1) {
6437c478bd9Sstevel@tonic-gate 		/*
6447c478bd9Sstevel@tonic-gate 		 * Really remove this vnode
6457c478bd9Sstevel@tonic-gate 		 */
6467c478bd9Sstevel@tonic-gate 		data = vp->v_data;
6477c478bd9Sstevel@tonic-gate 		if (ge != NULL) {
6487c478bd9Sstevel@tonic-gate 			/*
6497c478bd9Sstevel@tonic-gate 			 * If this was a statically cached entry, simply set the
6507c478bd9Sstevel@tonic-gate 			 * cached vnode to NULL.
6517c478bd9Sstevel@tonic-gate 			 */
6527c478bd9Sstevel@tonic-gate 			ge->gfse_vnode = NULL;
6537c478bd9Sstevel@tonic-gate 		}
654da6c28aaSamw 		if (vp->v_flag & V_XATTRDIR) {
655da6c28aaSamw 			fp->gfs_parent->v_xattrdir = NULL;
656da6c28aaSamw 			mutex_exit(&fp->gfs_parent->v_lock);
657da6c28aaSamw 		}
6587c478bd9Sstevel@tonic-gate 		mutex_exit(&vp->v_lock);
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 		/*
6617c478bd9Sstevel@tonic-gate 		 * Free vnode and release parent
6627c478bd9Sstevel@tonic-gate 		 */
6637c478bd9Sstevel@tonic-gate 		if (fp->gfs_parent) {
664da6c28aaSamw 			if (dp) {
6657c478bd9Sstevel@tonic-gate 				gfs_dir_unlock(dp);
666da6c28aaSamw 			}
6677c478bd9Sstevel@tonic-gate 			VN_RELE(fp->gfs_parent);
6687c478bd9Sstevel@tonic-gate 		} else {
6697c478bd9Sstevel@tonic-gate 			ASSERT(vp->v_vfsp != NULL);
6707c478bd9Sstevel@tonic-gate 			VFS_RELE(vp->v_vfsp);
6717c478bd9Sstevel@tonic-gate 		}
6727c478bd9Sstevel@tonic-gate 		vn_free(vp);
6737c478bd9Sstevel@tonic-gate 	} else {
6747c478bd9Sstevel@tonic-gate 		vp->v_count--;
6757c478bd9Sstevel@tonic-gate 		data = NULL;
6767c478bd9Sstevel@tonic-gate 		mutex_exit(&vp->v_lock);
677da6c28aaSamw 		if (vp->v_flag & V_XATTRDIR) {
678da6c28aaSamw 			mutex_exit(&fp->gfs_parent->v_lock);
679da6c28aaSamw 		}
6807c478bd9Sstevel@tonic-gate 		if (dp)
6817c478bd9Sstevel@tonic-gate 			gfs_dir_unlock(dp);
6827c478bd9Sstevel@tonic-gate 	}
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 	return (data);
6857c478bd9Sstevel@tonic-gate }
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate /*
6887c478bd9Sstevel@tonic-gate  * gfs_dir_inactive()
6897c478bd9Sstevel@tonic-gate  *
6907c478bd9Sstevel@tonic-gate  * Same as above, but for directories.
6917c478bd9Sstevel@tonic-gate  */
6927c478bd9Sstevel@tonic-gate void *
6937c478bd9Sstevel@tonic-gate gfs_dir_inactive(vnode_t *vp)
6947c478bd9Sstevel@tonic-gate {
6957c478bd9Sstevel@tonic-gate 	gfs_dir_t *dp;
6967c478bd9Sstevel@tonic-gate 
6977c478bd9Sstevel@tonic-gate 	ASSERT(vp->v_type == VDIR);
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate 	if ((dp = gfs_file_inactive(vp)) != NULL) {
7007c478bd9Sstevel@tonic-gate 		mutex_destroy(&dp->gfsd_lock);
7017c478bd9Sstevel@tonic-gate 		if (dp->gfsd_nstatic)
7027c478bd9Sstevel@tonic-gate 			kmem_free(dp->gfsd_static,
7037c478bd9Sstevel@tonic-gate 			    dp->gfsd_nstatic * sizeof (gfs_dirent_t));
7047c478bd9Sstevel@tonic-gate 	}
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate 	return (dp);
7077c478bd9Sstevel@tonic-gate }
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate /*
710*ab04eb8eStimh  * gfs_dir_lookup_dynamic()
7117c478bd9Sstevel@tonic-gate  *
712*ab04eb8eStimh  * This routine looks up the provided name amongst the dynamic entries
713*ab04eb8eStimh  * in the gfs directory and returns the corresponding vnode, if found.
7147c478bd9Sstevel@tonic-gate  *
715*ab04eb8eStimh  * The gfs directory is expected to be locked by the caller prior to
716*ab04eb8eStimh  * calling this function.  The directory will be unlocked during the
717*ab04eb8eStimh  * execution of this function, but will be locked upon return from the
718*ab04eb8eStimh  * function.  This function returns 0 on success, non-zero on error.
7197c478bd9Sstevel@tonic-gate  *
720*ab04eb8eStimh  * The dynamic lookups are performed by invoking the lookup
721*ab04eb8eStimh  * callback, which is passed to this function as the first argument.
722*ab04eb8eStimh  * The arguments to the callback are:
7237c478bd9Sstevel@tonic-gate  *
724*ab04eb8eStimh  * int gfs_lookup_cb(vnode_t *pvp, const char *nm, vnode_t **vpp, cred_t *cr,
725*ab04eb8eStimh  *     int flags, int *deflgs, pathname_t *rpnp);
7267c478bd9Sstevel@tonic-gate  *
7277c478bd9Sstevel@tonic-gate  *	pvp	- parent vnode
7287c478bd9Sstevel@tonic-gate  *	nm	- name of entry
7297c478bd9Sstevel@tonic-gate  *	vpp	- pointer to resulting vnode
730da6c28aaSamw  *	cr	- pointer to cred
731*ab04eb8eStimh  *	flags	- flags value from lookup request
732*ab04eb8eStimh  *		ignored here; currently only used to request
733*ab04eb8eStimh  *		insensitive lookups
734*ab04eb8eStimh  *	direntflgs - output parameter, directory entry flags
735*ab04eb8eStimh  *		ignored here; currently only used to indicate a lookup
736*ab04eb8eStimh  *		has more than one possible match when case is not considered
737*ab04eb8eStimh  *	realpnp	- output parameter, real pathname
738*ab04eb8eStimh  *		ignored here; when lookup was performed case-insensitively,
739*ab04eb8eStimh  *		this field contains the "real" name of the file.
7407c478bd9Sstevel@tonic-gate  *
7417c478bd9Sstevel@tonic-gate  * 	Returns 0 on success, non-zero on error.
7427c478bd9Sstevel@tonic-gate  */
743*ab04eb8eStimh static int
744*ab04eb8eStimh gfs_dir_lookup_dynamic(gfs_lookup_cb callback, gfs_dir_t *dp,
745*ab04eb8eStimh     const char *nm, vnode_t *dvp, vnode_t **vpp, cred_t *cr, int flags,
746*ab04eb8eStimh     int *direntflags, pathname_t *realpnp)
7477c478bd9Sstevel@tonic-gate {
748*ab04eb8eStimh 	gfs_file_t *fp;
749*ab04eb8eStimh 	ino64_t ino;
750*ab04eb8eStimh 	int ret;
7517c478bd9Sstevel@tonic-gate 
752*ab04eb8eStimh 	ASSERT(GFS_DIR_LOCKED(dp));
7537c478bd9Sstevel@tonic-gate 
754*ab04eb8eStimh 	/*
755*ab04eb8eStimh 	 * Drop the directory lock, as the lookup routine
756*ab04eb8eStimh 	 * will need to allocate memory, or otherwise deadlock on this
757*ab04eb8eStimh 	 * directory.
758*ab04eb8eStimh 	 */
759*ab04eb8eStimh 	gfs_dir_unlock(dp);
760*ab04eb8eStimh 	ret = callback(dvp, nm, vpp, &ino, cr, flags, direntflags, realpnp);
7617c478bd9Sstevel@tonic-gate 	gfs_dir_lock(dp);
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate 	/*
764*ab04eb8eStimh 	 * The callback for extended attributes returns a vnode
765*ab04eb8eStimh 	 * with v_data from an underlying fs.
766*ab04eb8eStimh 	 */
767*ab04eb8eStimh 	if (ret == 0 && !IS_XATTRDIR(dvp)) {
768*ab04eb8eStimh 		fp = (gfs_file_t *)((*vpp)->v_data);
769*ab04eb8eStimh 		fp->gfs_index = -1;
770*ab04eb8eStimh 		fp->gfs_ino = ino;
771*ab04eb8eStimh 	}
772*ab04eb8eStimh 
773*ab04eb8eStimh 	return (ret);
774*ab04eb8eStimh }
775*ab04eb8eStimh 
776*ab04eb8eStimh /*
777*ab04eb8eStimh  * gfs_dir_lookup_static()
778*ab04eb8eStimh  *
779*ab04eb8eStimh  * This routine looks up the provided name amongst the static entries
780*ab04eb8eStimh  * in the gfs directory and returns the corresponding vnode, if found.
781*ab04eb8eStimh  * The first argument to the function is a pointer to the comparison
782*ab04eb8eStimh  * function this function should use to decide if names are a match.
783*ab04eb8eStimh  *
784*ab04eb8eStimh  * If a match is found, and GFS_CACHE_VNODE is set and the vnode
785*ab04eb8eStimh  * exists, we simply return the existing vnode.  Otherwise, we call
786*ab04eb8eStimh  * the static entry's callback routine, caching the result if
787*ab04eb8eStimh  * necessary.  If the idx pointer argument is non-NULL, we use it to
788*ab04eb8eStimh  * return the index of the matching static entry.
789*ab04eb8eStimh  *
790*ab04eb8eStimh  * The gfs directory is expected to be locked by the caller prior to calling
791*ab04eb8eStimh  * this function.  The directory may be unlocked during the execution of
792*ab04eb8eStimh  * this function, but will be locked upon return from the function.
793*ab04eb8eStimh  *
794*ab04eb8eStimh  * This function returns 0 if a match is found, ENOENT if not.
795*ab04eb8eStimh  */
796*ab04eb8eStimh static int
797*ab04eb8eStimh gfs_dir_lookup_static(int (*compare)(const char *, const char *),
798*ab04eb8eStimh     gfs_dir_t *dp, const char *nm, vnode_t *dvp, int *idx,
799*ab04eb8eStimh     vnode_t **vpp, pathname_t *rpnp)
800*ab04eb8eStimh {
801*ab04eb8eStimh 	gfs_dirent_t *ge;
802*ab04eb8eStimh 	vnode_t *vp = NULL;
803*ab04eb8eStimh 	int i;
804*ab04eb8eStimh 
805*ab04eb8eStimh 	ASSERT(GFS_DIR_LOCKED(dp));
806*ab04eb8eStimh 
807*ab04eb8eStimh 	/*
8087c478bd9Sstevel@tonic-gate 	 * Search static entries.
8097c478bd9Sstevel@tonic-gate 	 */
8107c478bd9Sstevel@tonic-gate 	for (i = 0; i < dp->gfsd_nstatic; i++) {
8117c478bd9Sstevel@tonic-gate 		ge = &dp->gfsd_static[i];
8127c478bd9Sstevel@tonic-gate 
813*ab04eb8eStimh 		if (compare(ge->gfse_name, nm) == 0) {
814*ab04eb8eStimh 			if (rpnp)
815*ab04eb8eStimh 				(void) strlcpy(rpnp->pn_buf, ge->gfse_name,
816*ab04eb8eStimh 				    rpnp->pn_bufsize);
817*ab04eb8eStimh 
8187c478bd9Sstevel@tonic-gate 			if (ge->gfse_vnode) {
8197c478bd9Sstevel@tonic-gate 				ASSERT(ge->gfse_flags & GFS_CACHE_VNODE);
8207c478bd9Sstevel@tonic-gate 				vp = ge->gfse_vnode;
8217c478bd9Sstevel@tonic-gate 				VN_HOLD(vp);
822*ab04eb8eStimh 				break;
8237c478bd9Sstevel@tonic-gate 			}
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate 			/*
826092e3d7cSjk115741 			 * We drop the directory lock, as the constructor will
8277c478bd9Sstevel@tonic-gate 			 * need to do KM_SLEEP allocations.  If we return from
8287c478bd9Sstevel@tonic-gate 			 * the constructor only to find that a parallel
8297c478bd9Sstevel@tonic-gate 			 * operation has completed, and GFS_CACHE_VNODE is set
830*ab04eb8eStimh 			 * for this entry, we discard the result in favor of
831*ab04eb8eStimh 			 * the cached vnode.
8327c478bd9Sstevel@tonic-gate 			 */
8337c478bd9Sstevel@tonic-gate 			gfs_dir_unlock(dp);
8347c478bd9Sstevel@tonic-gate 			vp = ge->gfse_ctor(dvp);
8357c478bd9Sstevel@tonic-gate 			gfs_dir_lock(dp);
8367c478bd9Sstevel@tonic-gate 
8377c478bd9Sstevel@tonic-gate 			((gfs_file_t *)vp->v_data)->gfs_index = i;
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate 			/* Set the inode according to the callback. */
8407c478bd9Sstevel@tonic-gate 			((gfs_file_t *)vp->v_data)->gfs_ino =
8417c478bd9Sstevel@tonic-gate 			    dp->gfsd_inode(dvp, i);
8427c478bd9Sstevel@tonic-gate 
8437c478bd9Sstevel@tonic-gate 			if (ge->gfse_flags & GFS_CACHE_VNODE) {
8447c478bd9Sstevel@tonic-gate 				if (ge->gfse_vnode == NULL) {
8457c478bd9Sstevel@tonic-gate 					ge->gfse_vnode = vp;
8467c478bd9Sstevel@tonic-gate 				} else {
8477c478bd9Sstevel@tonic-gate 					/*
8487c478bd9Sstevel@tonic-gate 					 * A parallel constructor beat us to it;
8497c478bd9Sstevel@tonic-gate 					 * return existing vnode.  We have to be
8507c478bd9Sstevel@tonic-gate 					 * careful because we can't release the
8517c478bd9Sstevel@tonic-gate 					 * current vnode while holding the
8527c478bd9Sstevel@tonic-gate 					 * directory lock; its inactive routine
8537c478bd9Sstevel@tonic-gate 					 * will try to lock this directory.
8547c478bd9Sstevel@tonic-gate 					 */
8557c478bd9Sstevel@tonic-gate 					vnode_t *oldvp = vp;
8567c478bd9Sstevel@tonic-gate 					vp = ge->gfse_vnode;
8577c478bd9Sstevel@tonic-gate 					VN_HOLD(vp);
8587c478bd9Sstevel@tonic-gate 
8597c478bd9Sstevel@tonic-gate 					gfs_dir_unlock(dp);
8607c478bd9Sstevel@tonic-gate 					VN_RELE(oldvp);
8617c478bd9Sstevel@tonic-gate 					gfs_dir_lock(dp);
8627c478bd9Sstevel@tonic-gate 				}
8637c478bd9Sstevel@tonic-gate 			}
864*ab04eb8eStimh 			break;
8657c478bd9Sstevel@tonic-gate 		}
8667c478bd9Sstevel@tonic-gate 	}
8677c478bd9Sstevel@tonic-gate 
868*ab04eb8eStimh 	if (vp == NULL)
869*ab04eb8eStimh 		return (ENOENT);
870*ab04eb8eStimh 	else if (idx)
871*ab04eb8eStimh 		*idx = i;
872*ab04eb8eStimh 	*vpp = vp;
873*ab04eb8eStimh 	return (0);
874*ab04eb8eStimh }
8757c478bd9Sstevel@tonic-gate 
8767c478bd9Sstevel@tonic-gate /*
877*ab04eb8eStimh  * gfs_dir_lookup()
878*ab04eb8eStimh  *
879*ab04eb8eStimh  * Looks up the given name in the directory and returns the corresponding
880*ab04eb8eStimh  * vnode, if found.
881*ab04eb8eStimh  *
882*ab04eb8eStimh  * First, we search statically defined entries, if any, with a call to
883*ab04eb8eStimh  * gfs_dir_lookup_static().  If no static entry is found, and we have
884*ab04eb8eStimh  * a callback function we try a dynamic lookup via gfs_dir_lookup_dynamic().
885*ab04eb8eStimh  *
886*ab04eb8eStimh  * This function returns 0 on success, non-zero on error.
8877c478bd9Sstevel@tonic-gate  */
888*ab04eb8eStimh int
889*ab04eb8eStimh gfs_dir_lookup(vnode_t *dvp, const char *nm, vnode_t **vpp, cred_t *cr,
890*ab04eb8eStimh     int flags, int *direntflags, pathname_t *realpnp)
891*ab04eb8eStimh {
892*ab04eb8eStimh 	gfs_dir_t *dp = dvp->v_data;
893*ab04eb8eStimh 	boolean_t casecheck;
894*ab04eb8eStimh 	vnode_t *dynvp = NULL;
895*ab04eb8eStimh 	vnode_t *vp = NULL;
896*ab04eb8eStimh 	int (*compare)(const char *, const char *);
897*ab04eb8eStimh 	int error, idx;
898*ab04eb8eStimh 
899*ab04eb8eStimh 	ASSERT(dvp->v_type == VDIR);
900*ab04eb8eStimh 
901*ab04eb8eStimh 	if (gfs_lookup_dot(vpp, dvp, dp->gfsd_file.gfs_parent, nm) == 0)
902*ab04eb8eStimh 		return (0);
903*ab04eb8eStimh 
904*ab04eb8eStimh 	casecheck = (flags & FIGNORECASE) != 0 && direntflags != NULL;
905*ab04eb8eStimh 	if (vfs_has_feature(dvp->v_vfsp, VFSFT_NOCASESENSITIVE) ||
906*ab04eb8eStimh 	    (flags & FIGNORECASE))
907*ab04eb8eStimh 		compare = strcasecmp;
908*ab04eb8eStimh 	else
909*ab04eb8eStimh 		compare = strcmp;
910*ab04eb8eStimh 
9117c478bd9Sstevel@tonic-gate 	gfs_dir_lock(dp);
9127c478bd9Sstevel@tonic-gate 
913*ab04eb8eStimh 	error = gfs_dir_lookup_static(compare, dp, nm, dvp, &idx, &vp, realpnp);
914*ab04eb8eStimh 
915*ab04eb8eStimh 	if (vp && casecheck) {
916*ab04eb8eStimh 		gfs_dirent_t *ge;
917*ab04eb8eStimh 		int i;
918*ab04eb8eStimh 
919*ab04eb8eStimh 		for (i = idx + 1; i < dp->gfsd_nstatic; i++) {
920*ab04eb8eStimh 			ge = &dp->gfsd_static[i];
921*ab04eb8eStimh 
922*ab04eb8eStimh 			if (strcasecmp(ge->gfse_name, nm) == 0) {
923*ab04eb8eStimh 				*direntflags |= ED_CASE_CONFLICT;
924*ab04eb8eStimh 				goto out;
925da6c28aaSamw 			}
926*ab04eb8eStimh 		}
927*ab04eb8eStimh 	}
928*ab04eb8eStimh 
929*ab04eb8eStimh 	if ((error || casecheck) && dp->gfsd_lookup)
930*ab04eb8eStimh 		error = gfs_dir_lookup_dynamic(dp->gfsd_lookup, dp, nm, dvp,
931*ab04eb8eStimh 		    &dynvp, cr, flags, direntflags, vp ? NULL : realpnp);
932*ab04eb8eStimh 
933*ab04eb8eStimh 	if (vp && dynvp) {
934*ab04eb8eStimh 		/* static and dynamic entries are case-insensitive conflict */
935*ab04eb8eStimh 		ASSERT(casecheck);
936*ab04eb8eStimh 		*direntflags |= ED_CASE_CONFLICT;
937*ab04eb8eStimh 		VN_RELE(dynvp);
938*ab04eb8eStimh 	} else if (vp == NULL) {
939*ab04eb8eStimh 		vp = dynvp;
940*ab04eb8eStimh 	} else if (error == ENOENT) {
941*ab04eb8eStimh 		error = 0;
942*ab04eb8eStimh 	} else if (error) {
943*ab04eb8eStimh 		VN_RELE(vp);
944*ab04eb8eStimh 		vp = NULL;
9457c478bd9Sstevel@tonic-gate 	}
9467c478bd9Sstevel@tonic-gate 
9477c478bd9Sstevel@tonic-gate out:
9487c478bd9Sstevel@tonic-gate 	gfs_dir_unlock(dp);
9497c478bd9Sstevel@tonic-gate 
9507c478bd9Sstevel@tonic-gate 	*vpp = vp;
951*ab04eb8eStimh 	return (error);
9527c478bd9Sstevel@tonic-gate }
9537c478bd9Sstevel@tonic-gate 
9547c478bd9Sstevel@tonic-gate /*
9557c478bd9Sstevel@tonic-gate  * gfs_dir_readdir: does a readdir() on the given directory
9567c478bd9Sstevel@tonic-gate  *
9577c478bd9Sstevel@tonic-gate  *    dvp	- directory vnode
9587c478bd9Sstevel@tonic-gate  *    uiop	- uio structure
9597c478bd9Sstevel@tonic-gate  *    eofp	- eof pointer
9607c478bd9Sstevel@tonic-gate  *    data	- arbitrary data passed to readdir callback
9617c478bd9Sstevel@tonic-gate  *
9627c478bd9Sstevel@tonic-gate  * This routine does all the readdir() dirty work.  Even so, the caller must
9637c478bd9Sstevel@tonic-gate  * supply two callbacks in order to get full compatibility.
9647c478bd9Sstevel@tonic-gate  *
9657c478bd9Sstevel@tonic-gate  * If the directory contains static entries, an inode callback must be
9667c478bd9Sstevel@tonic-gate  * specified.  This avoids having to create every vnode and call VOP_GETATTR()
9677c478bd9Sstevel@tonic-gate  * when reading the directory.  This function has the following arguments:
9687c478bd9Sstevel@tonic-gate  *
9697c478bd9Sstevel@tonic-gate  *	ino_t gfs_inode_cb(vnode_t *vp, int index);
9707c478bd9Sstevel@tonic-gate  *
9717c478bd9Sstevel@tonic-gate  * 	vp	- vnode for the directory
9727c478bd9Sstevel@tonic-gate  * 	index	- index in original gfs_dirent_t array
9737c478bd9Sstevel@tonic-gate  *
9747c478bd9Sstevel@tonic-gate  * 	Returns the inode number for the given entry.
9757c478bd9Sstevel@tonic-gate  *
9767c478bd9Sstevel@tonic-gate  * For directories with dynamic entries, a readdir callback must be provided.
9777c478bd9Sstevel@tonic-gate  * This is significantly more complex, thanks to the particulars of
9787c478bd9Sstevel@tonic-gate  * VOP_READDIR().
9797c478bd9Sstevel@tonic-gate  *
980b38f0970Sck153898  *	int gfs_readdir_cb(vnode_t *vp, void *dp, int *eofp,
981b38f0970Sck153898  *	    offset_t *off, offset_t *nextoff, void *data, int flags)
9827c478bd9Sstevel@tonic-gate  *
9837c478bd9Sstevel@tonic-gate  *	vp	- directory vnode
9847c478bd9Sstevel@tonic-gate  *	dp	- directory entry, sized according to maxlen given to
9857c478bd9Sstevel@tonic-gate  *		  gfs_dir_create().  callback must fill in d_name and
986b38f0970Sck153898  *		  d_ino (if a dirent64_t), or ed_name, ed_ino, and ed_eflags
987b38f0970Sck153898  *		  (if an edirent_t). edirent_t is used if V_RDDIR_ENTFLAGS
988b38f0970Sck153898  *		  is set in 'flags'.
9897c478bd9Sstevel@tonic-gate  *	eofp	- callback must set to 1 when EOF has been reached
9907c478bd9Sstevel@tonic-gate  *	off	- on entry, the last offset read from the directory.  Callback
9917c478bd9Sstevel@tonic-gate  *		  must set to the offset of the current entry, typically left
9927c478bd9Sstevel@tonic-gate  *		  untouched.
9937c478bd9Sstevel@tonic-gate  *	nextoff	- callback must set to offset of next entry.  Typically
9947c478bd9Sstevel@tonic-gate  *		  (off + 1)
9957c478bd9Sstevel@tonic-gate  *	data	- caller-supplied data
996b38f0970Sck153898  *	flags	- VOP_READDIR flags
9977c478bd9Sstevel@tonic-gate  *
9987c478bd9Sstevel@tonic-gate  *	Return 0 on success, or error on failure.
9997c478bd9Sstevel@tonic-gate  */
10007c478bd9Sstevel@tonic-gate int
1001da6c28aaSamw gfs_dir_readdir(vnode_t *dvp, uio_t *uiop, int *eofp, void *data, cred_t *cr,
1002b38f0970Sck153898     caller_context_t *ct, int flags)
10037c478bd9Sstevel@tonic-gate {
10047c478bd9Sstevel@tonic-gate 	gfs_readdir_state_t gstate;
10057c478bd9Sstevel@tonic-gate 	int error, eof = 0;
10067c478bd9Sstevel@tonic-gate 	ino64_t ino, pino;
10077c478bd9Sstevel@tonic-gate 	offset_t off, next;
10087c478bd9Sstevel@tonic-gate 	gfs_dir_t *dp = dvp->v_data;
10097c478bd9Sstevel@tonic-gate 
1010b38f0970Sck153898 	error = gfs_get_parent_ino(dvp, cr, ct, &pino, &ino);
1011da6c28aaSamw 	if (error)
1012da6c28aaSamw 		return (error);
10137c478bd9Sstevel@tonic-gate 
10147c478bd9Sstevel@tonic-gate 	if ((error = gfs_readdir_init(&gstate, dp->gfsd_maxlen, 1, uiop,
1015b38f0970Sck153898 	    pino, ino, flags)) != 0)
10167c478bd9Sstevel@tonic-gate 		return (error);
10177c478bd9Sstevel@tonic-gate 
10187c478bd9Sstevel@tonic-gate 	while ((error = gfs_readdir_pred(&gstate, uiop, &off)) == 0 &&
10197c478bd9Sstevel@tonic-gate 	    !eof) {
10207c478bd9Sstevel@tonic-gate 
10217c478bd9Sstevel@tonic-gate 		if (off >= 0 && off < dp->gfsd_nstatic) {
10227c478bd9Sstevel@tonic-gate 			ino = dp->gfsd_inode(dvp, off);
10237c478bd9Sstevel@tonic-gate 
10247c478bd9Sstevel@tonic-gate 			if ((error = gfs_readdir_emit(&gstate, uiop,
1025b38f0970Sck153898 			    off, ino, dp->gfsd_static[off].gfse_name, 0))
10267c478bd9Sstevel@tonic-gate 			    != 0)
10277c478bd9Sstevel@tonic-gate 				break;
10287c478bd9Sstevel@tonic-gate 
10297c478bd9Sstevel@tonic-gate 		} else if (dp->gfsd_readdir) {
10307c478bd9Sstevel@tonic-gate 			off -= dp->gfsd_nstatic;
10317c478bd9Sstevel@tonic-gate 
10327c478bd9Sstevel@tonic-gate 			if ((error = dp->gfsd_readdir(dvp,
10337c478bd9Sstevel@tonic-gate 			    gstate.grd_dirent, &eof, &off, &next,
1034b38f0970Sck153898 			    data, flags)) != 0 || eof)
10357c478bd9Sstevel@tonic-gate 				break;
10367c478bd9Sstevel@tonic-gate 
10377c478bd9Sstevel@tonic-gate 			off += dp->gfsd_nstatic + 2;
10387c478bd9Sstevel@tonic-gate 			next += dp->gfsd_nstatic + 2;
10397c478bd9Sstevel@tonic-gate 
10407c478bd9Sstevel@tonic-gate 			if ((error = gfs_readdir_emit_int(&gstate, uiop,
10413f480432Smaybee 			    next)) != 0)
10427c478bd9Sstevel@tonic-gate 				break;
10437c478bd9Sstevel@tonic-gate 		} else {
10447c478bd9Sstevel@tonic-gate 			/*
10457c478bd9Sstevel@tonic-gate 			 * Offset is beyond the end of the static entries, and
10467c478bd9Sstevel@tonic-gate 			 * we have no dynamic entries.  Set EOF.
10477c478bd9Sstevel@tonic-gate 			 */
10487c478bd9Sstevel@tonic-gate 			eof = 1;
10497c478bd9Sstevel@tonic-gate 		}
10507c478bd9Sstevel@tonic-gate 	}
10517c478bd9Sstevel@tonic-gate 
10527c478bd9Sstevel@tonic-gate 	return (gfs_readdir_fini(&gstate, error, eofp, eof));
10537c478bd9Sstevel@tonic-gate }
10547c478bd9Sstevel@tonic-gate 
10557c478bd9Sstevel@tonic-gate 
10567c478bd9Sstevel@tonic-gate /*
10577c478bd9Sstevel@tonic-gate  * gfs_vop_lookup: VOP_LOOKUP() entry point
10587c478bd9Sstevel@tonic-gate  *
10597c478bd9Sstevel@tonic-gate  * For use directly in vnode ops table.  Given a GFS directory, calls
10607c478bd9Sstevel@tonic-gate  * gfs_dir_lookup() as necessary.
10617c478bd9Sstevel@tonic-gate  */
10627c478bd9Sstevel@tonic-gate /* ARGSUSED */
10637c478bd9Sstevel@tonic-gate int
10647c478bd9Sstevel@tonic-gate gfs_vop_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, pathname_t *pnp,
1065da6c28aaSamw     int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct,
1066da6c28aaSamw     int *direntflags, pathname_t *realpnp)
10677c478bd9Sstevel@tonic-gate {
1068*ab04eb8eStimh 	return (gfs_dir_lookup(dvp, nm, vpp, cr, flags, direntflags, realpnp));
10697c478bd9Sstevel@tonic-gate }
10707c478bd9Sstevel@tonic-gate 
10717c478bd9Sstevel@tonic-gate /*
10727c478bd9Sstevel@tonic-gate  * gfs_vop_readdir: VOP_READDIR() entry point
10737c478bd9Sstevel@tonic-gate  *
10747c478bd9Sstevel@tonic-gate  * For use directly in vnode ops table.  Given a GFS directory, calls
10757c478bd9Sstevel@tonic-gate  * gfs_dir_readdir() as necessary.
10767c478bd9Sstevel@tonic-gate  */
10777c478bd9Sstevel@tonic-gate /* ARGSUSED */
10787c478bd9Sstevel@tonic-gate int
1079da6c28aaSamw gfs_vop_readdir(vnode_t *vp, uio_t *uiop, cred_t *cr, int *eofp,
1080da6c28aaSamw     caller_context_t *ct, int flags)
10817c478bd9Sstevel@tonic-gate {
1082b38f0970Sck153898 	return (gfs_dir_readdir(vp, uiop, eofp, NULL, cr, ct, flags));
10837c478bd9Sstevel@tonic-gate }
10847c478bd9Sstevel@tonic-gate 
10857c478bd9Sstevel@tonic-gate 
10867c478bd9Sstevel@tonic-gate /*
10877c478bd9Sstevel@tonic-gate  * gfs_vop_map: VOP_MAP() entry point
10887c478bd9Sstevel@tonic-gate  *
10897c478bd9Sstevel@tonic-gate  * Convenient routine for handling pseudo-files that wish to allow mmap() calls.
10907c478bd9Sstevel@tonic-gate  * This function only works for readonly files, and uses the read function for
10917c478bd9Sstevel@tonic-gate  * the vnode to fill in the data.  The mapped data is immediately faulted in and
10927c478bd9Sstevel@tonic-gate  * filled with the necessary data during this call; there are no getpage() or
10937c478bd9Sstevel@tonic-gate  * putpage() routines.
10947c478bd9Sstevel@tonic-gate  */
10957c478bd9Sstevel@tonic-gate /* ARGSUSED */
10967c478bd9Sstevel@tonic-gate int
10977c478bd9Sstevel@tonic-gate gfs_vop_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp,
1098da6c28aaSamw     size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cred,
1099da6c28aaSamw     caller_context_t *ct)
11007c478bd9Sstevel@tonic-gate {
11017c478bd9Sstevel@tonic-gate 	int rv;
11027c478bd9Sstevel@tonic-gate 	ssize_t resid = len;
11037c478bd9Sstevel@tonic-gate 
11047c478bd9Sstevel@tonic-gate 	/*
11057c478bd9Sstevel@tonic-gate 	 * Check for bad parameters
11067c478bd9Sstevel@tonic-gate 	 */
11077c478bd9Sstevel@tonic-gate #ifdef _ILP32
11087c478bd9Sstevel@tonic-gate 	if (len > MAXOFF_T)
11097c478bd9Sstevel@tonic-gate 		return (ENOMEM);
11107c478bd9Sstevel@tonic-gate #endif
11117c478bd9Sstevel@tonic-gate 	if (vp->v_flag & VNOMAP)
11127c478bd9Sstevel@tonic-gate 		return (ENOTSUP);
11137c478bd9Sstevel@tonic-gate 	if (off > MAXOFF_T)
11147c478bd9Sstevel@tonic-gate 		return (EFBIG);
11157c478bd9Sstevel@tonic-gate 	if ((long)off < 0 || (long)(off + len) < 0)
11167c478bd9Sstevel@tonic-gate 		return (EINVAL);
11177c478bd9Sstevel@tonic-gate 	if (vp->v_type != VREG)
11187c478bd9Sstevel@tonic-gate 		return (ENODEV);
11197c478bd9Sstevel@tonic-gate 	if ((prot & (PROT_EXEC | PROT_WRITE)) != 0)
11207c478bd9Sstevel@tonic-gate 		return (EACCES);
11217c478bd9Sstevel@tonic-gate 
11227c478bd9Sstevel@tonic-gate 	/*
11237c478bd9Sstevel@tonic-gate 	 * Find appropriate address if needed, otherwise clear address range.
11247c478bd9Sstevel@tonic-gate 	 */
11257c478bd9Sstevel@tonic-gate 	as_rangelock(as);
112660946fe0Smec 	rv = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags);
112760946fe0Smec 	if (rv != 0) {
11287c478bd9Sstevel@tonic-gate 		as_rangeunlock(as);
112960946fe0Smec 		return (rv);
11307c478bd9Sstevel@tonic-gate 	}
11317c478bd9Sstevel@tonic-gate 
11327c478bd9Sstevel@tonic-gate 	/*
11337c478bd9Sstevel@tonic-gate 	 * Create mapping
11347c478bd9Sstevel@tonic-gate 	 */
11357c478bd9Sstevel@tonic-gate 	rv = as_map(as, *addrp, len, segvn_create, zfod_argsp);
11367c478bd9Sstevel@tonic-gate 	as_rangeunlock(as);
11377c478bd9Sstevel@tonic-gate 	if (rv != 0)
11387c478bd9Sstevel@tonic-gate 		return (rv);
11397c478bd9Sstevel@tonic-gate 
11407c478bd9Sstevel@tonic-gate 	/*
11417c478bd9Sstevel@tonic-gate 	 * Fill with data from read()
11427c478bd9Sstevel@tonic-gate 	 */
11437c478bd9Sstevel@tonic-gate 	rv = vn_rdwr(UIO_READ, vp, *addrp, len, off, UIO_USERSPACE,
11447c478bd9Sstevel@tonic-gate 	    0, (rlim64_t)0, cred, &resid);
11457c478bd9Sstevel@tonic-gate 
11467c478bd9Sstevel@tonic-gate 	if (rv == 0 && resid != 0)
11477c478bd9Sstevel@tonic-gate 		rv = ENXIO;
11487c478bd9Sstevel@tonic-gate 
11497c478bd9Sstevel@tonic-gate 	if (rv != 0) {
11507c478bd9Sstevel@tonic-gate 		as_rangelock(as);
11517c478bd9Sstevel@tonic-gate 		(void) as_unmap(as, *addrp, len);
11527c478bd9Sstevel@tonic-gate 		as_rangeunlock(as);
11537c478bd9Sstevel@tonic-gate 	}
11547c478bd9Sstevel@tonic-gate 
11557c478bd9Sstevel@tonic-gate 	return (rv);
11567c478bd9Sstevel@tonic-gate }
11577c478bd9Sstevel@tonic-gate 
11587c478bd9Sstevel@tonic-gate /*
11597c478bd9Sstevel@tonic-gate  * gfs_vop_inactive: VOP_INACTIVE() entry point
11607c478bd9Sstevel@tonic-gate  *
11617c478bd9Sstevel@tonic-gate  * Given a vnode that is a GFS file or directory, call gfs_file_inactive() or
11627c478bd9Sstevel@tonic-gate  * gfs_dir_inactive() as necessary, and kmem_free()s associated private data.
11637c478bd9Sstevel@tonic-gate  */
11647c478bd9Sstevel@tonic-gate /* ARGSUSED */
11657c478bd9Sstevel@tonic-gate void
1166da6c28aaSamw gfs_vop_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
11677c478bd9Sstevel@tonic-gate {
11687c478bd9Sstevel@tonic-gate 	gfs_file_t *fp = vp->v_data;
11697c478bd9Sstevel@tonic-gate 	void *data;
11707c478bd9Sstevel@tonic-gate 
11717c478bd9Sstevel@tonic-gate 	if (fp->gfs_type == GFS_DIR)
11727c478bd9Sstevel@tonic-gate 		data = gfs_dir_inactive(vp);
11737c478bd9Sstevel@tonic-gate 	else
11747c478bd9Sstevel@tonic-gate 		data = gfs_file_inactive(vp);
11757c478bd9Sstevel@tonic-gate 
11767c478bd9Sstevel@tonic-gate 	if (data != NULL)
11777c478bd9Sstevel@tonic-gate 		kmem_free(data, fp->gfs_size);
11787c478bd9Sstevel@tonic-gate }
1179