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