10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
53921Sjk115741 * Common Development and Distribution License (the "License").
63921Sjk115741 * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
213921Sjk115741 /* Portions Copyright 2007 Shivakumar GN */
220Sstevel@tonic-gate /*
236036Smec * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
280Sstevel@tonic-gate
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <sys/cmn_err.h>
310Sstevel@tonic-gate #include <sys/debug.h>
320Sstevel@tonic-gate #include <sys/dirent.h>
330Sstevel@tonic-gate #include <sys/kmem.h>
340Sstevel@tonic-gate #include <sys/mman.h>
350Sstevel@tonic-gate #include <sys/mutex.h>
360Sstevel@tonic-gate #include <sys/sysmacros.h>
370Sstevel@tonic-gate #include <sys/systm.h>
38*6492Stimh #include <sys/sunddi.h>
390Sstevel@tonic-gate #include <sys/uio.h>
400Sstevel@tonic-gate #include <sys/vmsystm.h>
410Sstevel@tonic-gate #include <sys/vfs.h>
420Sstevel@tonic-gate #include <sys/vnode.h>
430Sstevel@tonic-gate
440Sstevel@tonic-gate #include <vm/as.h>
450Sstevel@tonic-gate #include <vm/seg_vn.h>
460Sstevel@tonic-gate
470Sstevel@tonic-gate #include <sys/gfs.h>
480Sstevel@tonic-gate
490Sstevel@tonic-gate /*
500Sstevel@tonic-gate * Generic pseudo-filesystem routines.
510Sstevel@tonic-gate *
520Sstevel@tonic-gate * There are significant similarities between the implementation of certain file
530Sstevel@tonic-gate * system entry points across different filesystems. While one could attempt to
540Sstevel@tonic-gate * "choke up on the bat" and incorporate common functionality into a VOP
553921Sjk115741 * preamble or postamble, such an approach is limited in the benefit it can
560Sstevel@tonic-gate * provide. In this file we instead define a toolkit of routines which can be
570Sstevel@tonic-gate * called from a filesystem (with in-kernel pseudo-filesystems being the focus
580Sstevel@tonic-gate * of the exercise) in a more component-like fashion.
590Sstevel@tonic-gate *
600Sstevel@tonic-gate * There are three basic classes of routines:
610Sstevel@tonic-gate *
620Sstevel@tonic-gate * 1) Lowlevel support routines
630Sstevel@tonic-gate *
640Sstevel@tonic-gate * These routines are designed to play a support role for existing
653921Sjk115741 * pseudo-filesystems (such as procfs). They simplify common tasks,
665331Samw * without forcing the filesystem to hand over management to GFS. The
670Sstevel@tonic-gate * routines covered are:
680Sstevel@tonic-gate *
690Sstevel@tonic-gate * gfs_readdir_init()
700Sstevel@tonic-gate * gfs_readdir_emit()
710Sstevel@tonic-gate * gfs_readdir_emitn()
720Sstevel@tonic-gate * gfs_readdir_pred()
730Sstevel@tonic-gate * gfs_readdir_fini()
740Sstevel@tonic-gate * gfs_lookup_dot()
750Sstevel@tonic-gate *
760Sstevel@tonic-gate * 2) Complete GFS management
770Sstevel@tonic-gate *
780Sstevel@tonic-gate * These routines take a more active role in management of the
790Sstevel@tonic-gate * pseudo-filesystem. They handle the relationship between vnode private
800Sstevel@tonic-gate * data and VFS data, as well as the relationship between vnodes in the
813921Sjk115741 * directory hierarchy.
820Sstevel@tonic-gate *
830Sstevel@tonic-gate * In order to use these interfaces, the first member of every private
840Sstevel@tonic-gate * v_data must be a gfs_file_t or a gfs_dir_t. This hands over all control
850Sstevel@tonic-gate * to GFS.
860Sstevel@tonic-gate *
870Sstevel@tonic-gate * gfs_file_create()
880Sstevel@tonic-gate * gfs_dir_create()
890Sstevel@tonic-gate * gfs_root_create()
900Sstevel@tonic-gate *
910Sstevel@tonic-gate * gfs_file_inactive()
920Sstevel@tonic-gate * gfs_dir_inactive()
930Sstevel@tonic-gate * gfs_dir_lookup()
940Sstevel@tonic-gate * gfs_dir_readdir()
950Sstevel@tonic-gate *
960Sstevel@tonic-gate * gfs_vop_inactive()
970Sstevel@tonic-gate * gfs_vop_lookup()
980Sstevel@tonic-gate * gfs_vop_readdir()
990Sstevel@tonic-gate * gfs_vop_map()
1003957Sth199096 *
1013957Sth199096 * 3) Single File pseudo-filesystems
1023957Sth199096 *
1033957Sth199096 * This routine creates a rooted file to be overlayed ontop of another
1043957Sth199096 * file in the physical filespace.
1053957Sth199096 *
1063957Sth199096 * Note that the parent is NULL (actually the vfs), but there is nothing
1073957Sth199096 * technically keeping such a file from utilizing the "Complete GFS
1083957Sth199096 * management" set of routines.
1093957Sth199096 *
1103957Sth199096 * gfs_root_create_file()
1110Sstevel@tonic-gate */
1120Sstevel@tonic-gate
1130Sstevel@tonic-gate /*
1140Sstevel@tonic-gate * gfs_make_opsvec: take an array of vnode type definitions and create
1150Sstevel@tonic-gate * their vnodeops_t structures
1160Sstevel@tonic-gate *
1170Sstevel@tonic-gate * This routine takes an array of gfs_opsvec_t's. It could
1180Sstevel@tonic-gate * alternatively take an array of gfs_opsvec_t*'s, which would allow
1190Sstevel@tonic-gate * vnode types to be completely defined in files external to the caller
1200Sstevel@tonic-gate * of gfs_make_opsvec(). As it stands, much more sharing takes place --
1210Sstevel@tonic-gate * both the caller and the vnode type provider need to access gfsv_ops
1220Sstevel@tonic-gate * and gfsv_template, and the caller also needs to know gfsv_name.
1230Sstevel@tonic-gate */
1240Sstevel@tonic-gate int
gfs_make_opsvec(gfs_opsvec_t * vec)1250Sstevel@tonic-gate gfs_make_opsvec(gfs_opsvec_t *vec)
1260Sstevel@tonic-gate {
1270Sstevel@tonic-gate int error, i;
1280Sstevel@tonic-gate
1290Sstevel@tonic-gate for (i = 0; ; i++) {
1300Sstevel@tonic-gate if (vec[i].gfsv_name == NULL)
1310Sstevel@tonic-gate return (0);
1320Sstevel@tonic-gate error = vn_make_ops(vec[i].gfsv_name, vec[i].gfsv_template,
1330Sstevel@tonic-gate vec[i].gfsv_ops);
1340Sstevel@tonic-gate if (error)
1350Sstevel@tonic-gate break;
1360Sstevel@tonic-gate }
1370Sstevel@tonic-gate
1380Sstevel@tonic-gate cmn_err(CE_WARN, "gfs_make_opsvec: bad vnode ops template for '%s'",
1390Sstevel@tonic-gate vec[i].gfsv_name);
1400Sstevel@tonic-gate for (i--; i >= 0; i--) {
1410Sstevel@tonic-gate vn_freevnodeops(*vec[i].gfsv_ops);
1420Sstevel@tonic-gate *vec[i].gfsv_ops = NULL;
1430Sstevel@tonic-gate }
1440Sstevel@tonic-gate return (error);
1450Sstevel@tonic-gate }
1460Sstevel@tonic-gate
1470Sstevel@tonic-gate /*
1480Sstevel@tonic-gate * Low level directory routines
1490Sstevel@tonic-gate *
1500Sstevel@tonic-gate * These routines provide some simple abstractions for reading directories.
1510Sstevel@tonic-gate * They are designed to be used by existing pseudo filesystems (namely procfs)
1520Sstevel@tonic-gate * that already have a complicated management infrastructure.
1530Sstevel@tonic-gate */
1540Sstevel@tonic-gate
1550Sstevel@tonic-gate /*
1565663Sck153898 * gfs_get_parent_ino: used to obtain a parent inode number and the
1575663Sck153898 * inode number of the given vnode in preparation for calling gfs_readdir_init.
1585663Sck153898 */
1595663Sck153898 int
gfs_get_parent_ino(vnode_t * dvp,cred_t * cr,caller_context_t * ct,ino64_t * pino,ino64_t * ino)1605663Sck153898 gfs_get_parent_ino(vnode_t *dvp, cred_t *cr, caller_context_t *ct,
1615663Sck153898 ino64_t *pino, ino64_t *ino)
1625663Sck153898 {
1635663Sck153898 vnode_t *parent;
1645663Sck153898 gfs_dir_t *dp = dvp->v_data;
1655663Sck153898 int error;
1665663Sck153898
1675663Sck153898 *ino = dp->gfsd_file.gfs_ino;
1685663Sck153898 parent = dp->gfsd_file.gfs_parent;
1695663Sck153898
1705663Sck153898 if (parent == NULL) {
1715663Sck153898 *pino = *ino; /* root of filesystem */
1725663Sck153898 } else if (dvp->v_flag & V_XATTRDIR) {
1735663Sck153898 vattr_t va;
1745663Sck153898
1755663Sck153898 va.va_mask = AT_NODEID;
1765663Sck153898 error = VOP_GETATTR(parent, &va, 0, cr, ct);
1775663Sck153898 if (error)
1785663Sck153898 return (error);
1795663Sck153898 *pino = va.va_nodeid;
1805663Sck153898 } else {
1815663Sck153898 *pino = ((gfs_file_t *)(parent->v_data))->gfs_ino;
1825663Sck153898 }
1835663Sck153898
1845663Sck153898 return (0);
1855663Sck153898 }
1865663Sck153898
1875663Sck153898 /*
1880Sstevel@tonic-gate * gfs_readdir_init: initiate a generic readdir
1890Sstevel@tonic-gate * st - a pointer to an uninitialized gfs_readdir_state_t structure
1900Sstevel@tonic-gate * name_max - the directory's maximum file name length
1910Sstevel@tonic-gate * ureclen - the exported file-space record length (1 for non-legacy FSs)
1920Sstevel@tonic-gate * uiop - the uiop passed to readdir
1930Sstevel@tonic-gate * parent - the parent directory's inode
1940Sstevel@tonic-gate * self - this directory's inode
1955663Sck153898 * flags - flags from VOP_READDIR
1960Sstevel@tonic-gate *
1970Sstevel@tonic-gate * Returns 0 or a non-zero errno.
1980Sstevel@tonic-gate *
1990Sstevel@tonic-gate * Typical VOP_READDIR usage of gfs_readdir_*:
2000Sstevel@tonic-gate *
2010Sstevel@tonic-gate * if ((error = gfs_readdir_init(...)) != 0)
2020Sstevel@tonic-gate * return (error);
2030Sstevel@tonic-gate * eof = 0;
2040Sstevel@tonic-gate * while ((error = gfs_readdir_pred(..., &voffset)) != 0) {
2050Sstevel@tonic-gate * if (!consumer_entry_at(voffset))
2060Sstevel@tonic-gate * voffset = consumer_next_entry(voffset);
2070Sstevel@tonic-gate * if (consumer_eof(voffset)) {
2080Sstevel@tonic-gate * eof = 1
2090Sstevel@tonic-gate * break;
2100Sstevel@tonic-gate * }
2110Sstevel@tonic-gate * if ((error = gfs_readdir_emit(..., voffset,
2120Sstevel@tonic-gate * consumer_ino(voffset), consumer_name(voffset))) != 0)
2130Sstevel@tonic-gate * break;
2140Sstevel@tonic-gate * }
2150Sstevel@tonic-gate * return (gfs_readdir_fini(..., error, eofp, eof));
2160Sstevel@tonic-gate *
2170Sstevel@tonic-gate * As you can see, a zero result from gfs_readdir_pred() or
2180Sstevel@tonic-gate * gfs_readdir_emit() indicates that processing should continue,
2190Sstevel@tonic-gate * whereas a non-zero result indicates that the loop should terminate.
2200Sstevel@tonic-gate * Most consumers need do nothing more than let gfs_readdir_fini()
2210Sstevel@tonic-gate * determine what the cause of failure was and return the appropriate
2220Sstevel@tonic-gate * value.
2230Sstevel@tonic-gate */
2240Sstevel@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)2250Sstevel@tonic-gate gfs_readdir_init(gfs_readdir_state_t *st, int name_max, int ureclen,
2265663Sck153898 uio_t *uiop, ino64_t parent, ino64_t self, int flags)
2270Sstevel@tonic-gate {
2285663Sck153898 size_t dirent_size;
2295663Sck153898
2300Sstevel@tonic-gate if (uiop->uio_loffset < 0 || uiop->uio_resid <= 0 ||
2310Sstevel@tonic-gate (uiop->uio_loffset % ureclen) != 0)
2320Sstevel@tonic-gate return (EINVAL);
2330Sstevel@tonic-gate
2340Sstevel@tonic-gate st->grd_ureclen = ureclen;
2350Sstevel@tonic-gate st->grd_oresid = uiop->uio_resid;
2360Sstevel@tonic-gate st->grd_namlen = name_max;
2375663Sck153898 if (flags & V_RDDIR_ENTFLAGS)
2385663Sck153898 dirent_size = EDIRENT_RECLEN(st->grd_namlen);
2395663Sck153898 else
2405663Sck153898 dirent_size = DIRENT64_RECLEN(st->grd_namlen);
2415663Sck153898 st->grd_dirent = kmem_zalloc(dirent_size, KM_SLEEP);
2420Sstevel@tonic-gate st->grd_parent = parent;
2430Sstevel@tonic-gate st->grd_self = self;
2445663Sck153898 st->grd_flags = flags;
2450Sstevel@tonic-gate
2460Sstevel@tonic-gate return (0);
2470Sstevel@tonic-gate }
2480Sstevel@tonic-gate
2490Sstevel@tonic-gate /*
2500Sstevel@tonic-gate * gfs_readdir_emit_int: internal routine to emit directory entry
2510Sstevel@tonic-gate *
2525663Sck153898 * st - the current readdir state, which must have d_ino/ed_ino
2535663Sck153898 * and d_name/ed_name set
2540Sstevel@tonic-gate * uiop - caller-supplied uio pointer
2550Sstevel@tonic-gate * next - the offset of the next entry
2560Sstevel@tonic-gate */
2570Sstevel@tonic-gate static int
gfs_readdir_emit_int(gfs_readdir_state_t * st,uio_t * uiop,offset_t next)258847Smaybee gfs_readdir_emit_int(gfs_readdir_state_t *st, uio_t *uiop, offset_t next)
2590Sstevel@tonic-gate {
2600Sstevel@tonic-gate int reclen;
2615663Sck153898 dirent64_t *dp;
2625663Sck153898 edirent_t *edp;
2630Sstevel@tonic-gate
2645663Sck153898 if (st->grd_flags & V_RDDIR_ENTFLAGS) {
2655663Sck153898 edp = st->grd_dirent;
2665663Sck153898 reclen = EDIRENT_RECLEN(strlen(edp->ed_name));
2675663Sck153898 } else {
2685663Sck153898 dp = st->grd_dirent;
2695663Sck153898 reclen = DIRENT64_RECLEN(strlen(dp->d_name));
2705663Sck153898 }
2710Sstevel@tonic-gate
2720Sstevel@tonic-gate if (reclen > uiop->uio_resid) {
2730Sstevel@tonic-gate /*
2740Sstevel@tonic-gate * Error if no entries were returned yet
2750Sstevel@tonic-gate */
2760Sstevel@tonic-gate if (uiop->uio_resid == st->grd_oresid)
2770Sstevel@tonic-gate return (EINVAL);
2780Sstevel@tonic-gate return (-1);
2790Sstevel@tonic-gate }
2800Sstevel@tonic-gate
2815663Sck153898 if (st->grd_flags & V_RDDIR_ENTFLAGS) {
2825663Sck153898 edp->ed_off = next;
2835663Sck153898 edp->ed_reclen = (ushort_t)reclen;
2845663Sck153898 } else {
2855663Sck153898 dp->d_off = next;
2865663Sck153898 dp->d_reclen = (ushort_t)reclen;
2875663Sck153898 }
2880Sstevel@tonic-gate
2890Sstevel@tonic-gate if (uiomove((caddr_t)st->grd_dirent, reclen, UIO_READ, uiop))
2900Sstevel@tonic-gate return (EFAULT);
2910Sstevel@tonic-gate
2920Sstevel@tonic-gate uiop->uio_loffset = next;
2930Sstevel@tonic-gate
2940Sstevel@tonic-gate return (0);
2950Sstevel@tonic-gate }
2960Sstevel@tonic-gate
2970Sstevel@tonic-gate /*
2980Sstevel@tonic-gate * gfs_readdir_emit: emit a directory entry
2990Sstevel@tonic-gate * voff - the virtual offset (obtained from gfs_readdir_pred)
3000Sstevel@tonic-gate * ino - the entry's inode
3010Sstevel@tonic-gate * name - the entry's name
3025663Sck153898 * eflags - value for ed_eflags (if processing edirent_t)
3030Sstevel@tonic-gate *
3040Sstevel@tonic-gate * Returns a 0 on success, a non-zero errno on failure, or -1 if the
3050Sstevel@tonic-gate * readdir loop should terminate. A non-zero result (either errno or
3060Sstevel@tonic-gate * -1) from this function is typically passed directly to
3070Sstevel@tonic-gate * gfs_readdir_fini().
3080Sstevel@tonic-gate */
3090Sstevel@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)3100Sstevel@tonic-gate gfs_readdir_emit(gfs_readdir_state_t *st, uio_t *uiop, offset_t voff,
3115663Sck153898 ino64_t ino, const char *name, int eflags)
3120Sstevel@tonic-gate {
3130Sstevel@tonic-gate offset_t off = (voff + 2) * st->grd_ureclen;
3140Sstevel@tonic-gate
3155663Sck153898 if (st->grd_flags & V_RDDIR_ENTFLAGS) {
3165663Sck153898 edirent_t *edp = st->grd_dirent;
3175663Sck153898
3185663Sck153898 edp->ed_ino = ino;
3195663Sck153898 (void) strncpy(edp->ed_name, name, st->grd_namlen);
3205663Sck153898 edp->ed_eflags = eflags;
3215663Sck153898 } else {
3225663Sck153898 dirent64_t *dp = st->grd_dirent;
3235663Sck153898
3245663Sck153898 dp->d_ino = ino;
3255663Sck153898 (void) strncpy(dp->d_name, name, st->grd_namlen);
3265663Sck153898 }
3270Sstevel@tonic-gate
3280Sstevel@tonic-gate /*
3290Sstevel@tonic-gate * Inter-entry offsets are invalid, so we assume a record size of
3300Sstevel@tonic-gate * grd_ureclen and explicitly set the offset appropriately.
3310Sstevel@tonic-gate */
332847Smaybee return (gfs_readdir_emit_int(st, uiop, off + st->grd_ureclen));
3330Sstevel@tonic-gate }
3340Sstevel@tonic-gate
3350Sstevel@tonic-gate /*
3360Sstevel@tonic-gate * gfs_readdir_emitn: like gfs_readdir_emit(), but takes an integer
3370Sstevel@tonic-gate * instead of a string for the entry's name.
3380Sstevel@tonic-gate */
3390Sstevel@tonic-gate int
gfs_readdir_emitn(gfs_readdir_state_t * st,uio_t * uiop,offset_t voff,ino64_t ino,unsigned long num)3400Sstevel@tonic-gate gfs_readdir_emitn(gfs_readdir_state_t *st, uio_t *uiop, offset_t voff,
3410Sstevel@tonic-gate ino64_t ino, unsigned long num)
3420Sstevel@tonic-gate {
3430Sstevel@tonic-gate char buf[40];
3440Sstevel@tonic-gate
3450Sstevel@tonic-gate numtos(num, buf);
3465663Sck153898 return (gfs_readdir_emit(st, uiop, voff, ino, buf, 0));
3470Sstevel@tonic-gate }
3480Sstevel@tonic-gate
3490Sstevel@tonic-gate /*
3500Sstevel@tonic-gate * gfs_readdir_pred: readdir loop predicate
3510Sstevel@tonic-gate * voffp - a pointer in which the next virtual offset should be stored
3520Sstevel@tonic-gate *
3530Sstevel@tonic-gate * Returns a 0 on success, a non-zero errno on failure, or -1 if the
3540Sstevel@tonic-gate * readdir loop should terminate. A non-zero result (either errno or
3550Sstevel@tonic-gate * -1) from this function is typically passed directly to
3560Sstevel@tonic-gate * gfs_readdir_fini().
3570Sstevel@tonic-gate */
3580Sstevel@tonic-gate int
gfs_readdir_pred(gfs_readdir_state_t * st,uio_t * uiop,offset_t * voffp)3590Sstevel@tonic-gate gfs_readdir_pred(gfs_readdir_state_t *st, uio_t *uiop, offset_t *voffp)
3600Sstevel@tonic-gate {
3610Sstevel@tonic-gate offset_t off, voff;
3620Sstevel@tonic-gate int error;
3630Sstevel@tonic-gate
3640Sstevel@tonic-gate top:
3650Sstevel@tonic-gate if (uiop->uio_resid <= 0)
3660Sstevel@tonic-gate return (-1);
3670Sstevel@tonic-gate
3680Sstevel@tonic-gate off = uiop->uio_loffset / st->grd_ureclen;
3690Sstevel@tonic-gate voff = off - 2;
3700Sstevel@tonic-gate if (off == 0) {
3710Sstevel@tonic-gate if ((error = gfs_readdir_emit(st, uiop, voff, st->grd_self,
3725663Sck153898 ".", 0)) == 0)
3730Sstevel@tonic-gate goto top;
3740Sstevel@tonic-gate } else if (off == 1) {
3750Sstevel@tonic-gate if ((error = gfs_readdir_emit(st, uiop, voff, st->grd_parent,
3765663Sck153898 "..", 0)) == 0)
3770Sstevel@tonic-gate goto top;
3780Sstevel@tonic-gate } else {
3790Sstevel@tonic-gate *voffp = voff;
3800Sstevel@tonic-gate return (0);
3810Sstevel@tonic-gate }
3820Sstevel@tonic-gate
3830Sstevel@tonic-gate return (error);
3840Sstevel@tonic-gate }
3850Sstevel@tonic-gate
3860Sstevel@tonic-gate /*
3870Sstevel@tonic-gate * gfs_readdir_fini: generic readdir cleanup
3880Sstevel@tonic-gate * error - if positive, an error to return
3890Sstevel@tonic-gate * eofp - the eofp passed to readdir
3900Sstevel@tonic-gate * eof - the eof value
3910Sstevel@tonic-gate *
3920Sstevel@tonic-gate * Returns a 0 on success, a non-zero errno on failure. This result
3930Sstevel@tonic-gate * should be returned from readdir.
3940Sstevel@tonic-gate */
3950Sstevel@tonic-gate int
gfs_readdir_fini(gfs_readdir_state_t * st,int error,int * eofp,int eof)3960Sstevel@tonic-gate gfs_readdir_fini(gfs_readdir_state_t *st, int error, int *eofp, int eof)
3970Sstevel@tonic-gate {
3985663Sck153898 size_t dirent_size;
3995663Sck153898
4005663Sck153898 if (st->grd_flags & V_RDDIR_ENTFLAGS)
4015663Sck153898 dirent_size = EDIRENT_RECLEN(st->grd_namlen);
4025663Sck153898 else
4035663Sck153898 dirent_size = DIRENT64_RECLEN(st->grd_namlen);
4045663Sck153898 kmem_free(st->grd_dirent, dirent_size);
4050Sstevel@tonic-gate if (error > 0)
4060Sstevel@tonic-gate return (error);
4070Sstevel@tonic-gate if (eofp)
4080Sstevel@tonic-gate *eofp = eof;
4090Sstevel@tonic-gate return (0);
4100Sstevel@tonic-gate }
4110Sstevel@tonic-gate
4120Sstevel@tonic-gate /*
4130Sstevel@tonic-gate * gfs_lookup_dot
4140Sstevel@tonic-gate *
4150Sstevel@tonic-gate * Performs a basic check for "." and ".." directory entries.
4160Sstevel@tonic-gate */
4170Sstevel@tonic-gate int
gfs_lookup_dot(vnode_t ** vpp,vnode_t * dvp,vnode_t * pvp,const char * nm)4180Sstevel@tonic-gate gfs_lookup_dot(vnode_t **vpp, vnode_t *dvp, vnode_t *pvp, const char *nm)
4190Sstevel@tonic-gate {
4200Sstevel@tonic-gate if (*nm == '\0' || strcmp(nm, ".") == 0) {
4210Sstevel@tonic-gate VN_HOLD(dvp);
4220Sstevel@tonic-gate *vpp = dvp;
4230Sstevel@tonic-gate return (0);
4240Sstevel@tonic-gate } else if (strcmp(nm, "..") == 0) {
4250Sstevel@tonic-gate if (pvp == NULL) {
4260Sstevel@tonic-gate ASSERT(dvp->v_flag & VROOT);
4270Sstevel@tonic-gate VN_HOLD(dvp);
4280Sstevel@tonic-gate *vpp = dvp;
4290Sstevel@tonic-gate } else {
4300Sstevel@tonic-gate VN_HOLD(pvp);
4310Sstevel@tonic-gate *vpp = pvp;
4320Sstevel@tonic-gate }
4330Sstevel@tonic-gate return (0);
4340Sstevel@tonic-gate }
4350Sstevel@tonic-gate
4360Sstevel@tonic-gate return (-1);
4370Sstevel@tonic-gate }
4380Sstevel@tonic-gate
4390Sstevel@tonic-gate /*
4400Sstevel@tonic-gate * gfs_file_create(): create a new GFS file
4410Sstevel@tonic-gate *
4420Sstevel@tonic-gate * size - size of private data structure (v_data)
4430Sstevel@tonic-gate * pvp - parent vnode (GFS directory)
4440Sstevel@tonic-gate * ops - vnode operations vector
4450Sstevel@tonic-gate *
4460Sstevel@tonic-gate * In order to use this interface, the parent vnode must have been created by
4470Sstevel@tonic-gate * gfs_dir_create(), and the private data stored in v_data must have a
4480Sstevel@tonic-gate * 'gfs_file_t' as its first field.
4490Sstevel@tonic-gate *
4500Sstevel@tonic-gate * Given these constraints, this routine will automatically:
4510Sstevel@tonic-gate *
4520Sstevel@tonic-gate * - Allocate v_data for the vnode
4530Sstevel@tonic-gate * - Initialize necessary fields in the vnode
4540Sstevel@tonic-gate * - Hold the parent
4550Sstevel@tonic-gate */
4560Sstevel@tonic-gate vnode_t *
gfs_file_create(size_t size,vnode_t * pvp,vnodeops_t * ops)4570Sstevel@tonic-gate gfs_file_create(size_t size, vnode_t *pvp, vnodeops_t *ops)
4580Sstevel@tonic-gate {
4590Sstevel@tonic-gate gfs_file_t *fp;
4600Sstevel@tonic-gate vnode_t *vp;
4610Sstevel@tonic-gate
4620Sstevel@tonic-gate /*
4630Sstevel@tonic-gate * Allocate vnode and internal data structure
4640Sstevel@tonic-gate */
4650Sstevel@tonic-gate fp = kmem_zalloc(size, KM_SLEEP);
4660Sstevel@tonic-gate vp = vn_alloc(KM_SLEEP);
4670Sstevel@tonic-gate
4680Sstevel@tonic-gate /*
4690Sstevel@tonic-gate * Set up various pointers
4700Sstevel@tonic-gate */
4710Sstevel@tonic-gate fp->gfs_vnode = vp;
4720Sstevel@tonic-gate fp->gfs_parent = pvp;
4730Sstevel@tonic-gate vp->v_data = fp;
4740Sstevel@tonic-gate fp->gfs_size = size;
4750Sstevel@tonic-gate fp->gfs_type = GFS_FILE;
4760Sstevel@tonic-gate
4770Sstevel@tonic-gate /*
4780Sstevel@tonic-gate * Initialize vnode and hold parent.
4790Sstevel@tonic-gate */
4800Sstevel@tonic-gate vn_setops(vp, ops);
4810Sstevel@tonic-gate if (pvp) {
4820Sstevel@tonic-gate VN_SET_VFS_TYPE_DEV(vp, pvp->v_vfsp, VREG, 0);
4830Sstevel@tonic-gate VN_HOLD(pvp);
4840Sstevel@tonic-gate }
4850Sstevel@tonic-gate
4860Sstevel@tonic-gate return (vp);
4870Sstevel@tonic-gate }
4880Sstevel@tonic-gate
4890Sstevel@tonic-gate /*
4900Sstevel@tonic-gate * gfs_dir_create: creates a new directory in the parent
4910Sstevel@tonic-gate *
4920Sstevel@tonic-gate * size - size of private data structure (v_data)
4930Sstevel@tonic-gate * pvp - parent vnode (GFS directory)
4940Sstevel@tonic-gate * ops - vnode operations vector
4950Sstevel@tonic-gate * entries - NULL-terminated list of static entries (if any)
4960Sstevel@tonic-gate * maxlen - maximum length of a directory entry
4970Sstevel@tonic-gate * readdir_cb - readdir callback (see gfs_dir_readdir)
4980Sstevel@tonic-gate * inode_cb - inode callback (see gfs_dir_readdir)
4990Sstevel@tonic-gate * lookup_cb - lookup callback (see gfs_dir_lookup)
5000Sstevel@tonic-gate *
5010Sstevel@tonic-gate * In order to use this function, the first member of the private vnode
5020Sstevel@tonic-gate * structure (v_data) must be a gfs_dir_t. For each directory, there are
5030Sstevel@tonic-gate * static entries, defined when the structure is initialized, and dynamic
5040Sstevel@tonic-gate * entries, retrieved through callbacks.
5050Sstevel@tonic-gate *
5060Sstevel@tonic-gate * If a directory has static entries, then it must supply a inode callback,
5070Sstevel@tonic-gate * which will compute the inode number based on the parent and the index.
5080Sstevel@tonic-gate * For a directory with dynamic entries, the caller must supply a readdir
5090Sstevel@tonic-gate * callback and a lookup callback. If a static lookup fails, we fall back to
5100Sstevel@tonic-gate * the supplied lookup callback, if any.
5110Sstevel@tonic-gate *
5120Sstevel@tonic-gate * This function also performs the same initialization as gfs_file_create().
5130Sstevel@tonic-gate */
5140Sstevel@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)5150Sstevel@tonic-gate gfs_dir_create(size_t struct_size, vnode_t *pvp, vnodeops_t *ops,
5160Sstevel@tonic-gate gfs_dirent_t *entries, gfs_inode_cb inode_cb, int maxlen,
5170Sstevel@tonic-gate gfs_readdir_cb readdir_cb, gfs_lookup_cb lookup_cb)
5180Sstevel@tonic-gate {
5190Sstevel@tonic-gate vnode_t *vp;
5200Sstevel@tonic-gate gfs_dir_t *dp;
5210Sstevel@tonic-gate gfs_dirent_t *de;
5220Sstevel@tonic-gate
5230Sstevel@tonic-gate vp = gfs_file_create(struct_size, pvp, ops);
5240Sstevel@tonic-gate vp->v_type = VDIR;
5250Sstevel@tonic-gate
5260Sstevel@tonic-gate dp = vp->v_data;
5270Sstevel@tonic-gate dp->gfsd_file.gfs_type = GFS_DIR;
5280Sstevel@tonic-gate dp->gfsd_maxlen = maxlen;
5290Sstevel@tonic-gate
5300Sstevel@tonic-gate if (entries != NULL) {
5310Sstevel@tonic-gate for (de = entries; de->gfse_name != NULL; de++)
5320Sstevel@tonic-gate dp->gfsd_nstatic++;
5330Sstevel@tonic-gate
5340Sstevel@tonic-gate dp->gfsd_static = kmem_alloc(
5350Sstevel@tonic-gate dp->gfsd_nstatic * sizeof (gfs_dirent_t), KM_SLEEP);
5360Sstevel@tonic-gate bcopy(entries, dp->gfsd_static,
5370Sstevel@tonic-gate dp->gfsd_nstatic * sizeof (gfs_dirent_t));
5380Sstevel@tonic-gate }
5390Sstevel@tonic-gate
5400Sstevel@tonic-gate dp->gfsd_readdir = readdir_cb;
5410Sstevel@tonic-gate dp->gfsd_lookup = lookup_cb;
5420Sstevel@tonic-gate dp->gfsd_inode = inode_cb;
5430Sstevel@tonic-gate
5440Sstevel@tonic-gate mutex_init(&dp->gfsd_lock, NULL, MUTEX_DEFAULT, NULL);
5450Sstevel@tonic-gate
5460Sstevel@tonic-gate return (vp);
5470Sstevel@tonic-gate }
5480Sstevel@tonic-gate
5490Sstevel@tonic-gate /*
5500Sstevel@tonic-gate * gfs_root_create(): create a root vnode for a GFS filesystem
5510Sstevel@tonic-gate *
5520Sstevel@tonic-gate * Similar to gfs_dir_create(), this creates a root vnode for a filesystem. The
5530Sstevel@tonic-gate * only difference is that it takes a vfs_t instead of a vnode_t as its parent.
5540Sstevel@tonic-gate */
5550Sstevel@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)5560Sstevel@tonic-gate gfs_root_create(size_t size, vfs_t *vfsp, vnodeops_t *ops, ino64_t ino,
5570Sstevel@tonic-gate gfs_dirent_t *entries, gfs_inode_cb inode_cb, int maxlen,
5580Sstevel@tonic-gate gfs_readdir_cb readdir_cb, gfs_lookup_cb lookup_cb)
5590Sstevel@tonic-gate {
5600Sstevel@tonic-gate vnode_t *vp = gfs_dir_create(size, NULL, ops, entries, inode_cb,
5610Sstevel@tonic-gate maxlen, readdir_cb, lookup_cb);
5620Sstevel@tonic-gate
5630Sstevel@tonic-gate /* Manually set the inode */
5640Sstevel@tonic-gate ((gfs_file_t *)vp->v_data)->gfs_ino = ino;
5650Sstevel@tonic-gate
5660Sstevel@tonic-gate VFS_HOLD(vfsp);
5670Sstevel@tonic-gate VN_SET_VFS_TYPE_DEV(vp, vfsp, VDIR, 0);
5680Sstevel@tonic-gate vp->v_flag |= VROOT | VNOCACHE | VNOMAP | VNOSWAP | VNOMOUNT;
5690Sstevel@tonic-gate
5700Sstevel@tonic-gate return (vp);
5710Sstevel@tonic-gate }
5720Sstevel@tonic-gate
5730Sstevel@tonic-gate /*
5743957Sth199096 * gfs_root_create_file(): create a root vnode for a GFS file as a filesystem
5753957Sth199096 *
5763957Sth199096 * Similar to gfs_root_create(), this creates a root vnode for a file to
5773957Sth199096 * be the pseudo-filesystem.
5783957Sth199096 */
5793957Sth199096 vnode_t *
gfs_root_create_file(size_t size,vfs_t * vfsp,vnodeops_t * ops,ino64_t ino)5803957Sth199096 gfs_root_create_file(size_t size, vfs_t *vfsp, vnodeops_t *ops, ino64_t ino)
5813957Sth199096 {
5823957Sth199096 vnode_t *vp = gfs_file_create(size, NULL, ops);
5833957Sth199096
5843957Sth199096 ((gfs_file_t *)vp->v_data)->gfs_ino = ino;
5853957Sth199096
5863957Sth199096 VFS_HOLD(vfsp);
5873957Sth199096 VN_SET_VFS_TYPE_DEV(vp, vfsp, VREG, 0);
5883957Sth199096 vp->v_flag |= VROOT | VNOCACHE | VNOMAP | VNOSWAP | VNOMOUNT;
5893957Sth199096
5903957Sth199096 return (vp);
5913957Sth199096 }
5923957Sth199096
5933957Sth199096 /*
5940Sstevel@tonic-gate * gfs_file_inactive()
5950Sstevel@tonic-gate *
5960Sstevel@tonic-gate * Called from the VOP_INACTIVE() routine. If necessary, this routine will
5970Sstevel@tonic-gate * remove the given vnode from the parent directory and clean up any references
5980Sstevel@tonic-gate * in the VFS layer.
5990Sstevel@tonic-gate *
6000Sstevel@tonic-gate * If the vnode was not removed (due to a race with vget), then NULL is
6010Sstevel@tonic-gate * returned. Otherwise, a pointer to the private data is returned.
6020Sstevel@tonic-gate */
6030Sstevel@tonic-gate void *
gfs_file_inactive(vnode_t * vp)6040Sstevel@tonic-gate gfs_file_inactive(vnode_t *vp)
6050Sstevel@tonic-gate {
6060Sstevel@tonic-gate int i;
6070Sstevel@tonic-gate gfs_dirent_t *ge = NULL;
6080Sstevel@tonic-gate gfs_file_t *fp = vp->v_data;
6090Sstevel@tonic-gate gfs_dir_t *dp = NULL;
6100Sstevel@tonic-gate void *data;
6110Sstevel@tonic-gate
6125331Samw if (fp->gfs_parent == NULL || (vp->v_flag & V_XATTRDIR))
6130Sstevel@tonic-gate goto found;
6140Sstevel@tonic-gate
6150Sstevel@tonic-gate dp = fp->gfs_parent->v_data;
6160Sstevel@tonic-gate
6170Sstevel@tonic-gate /*
6180Sstevel@tonic-gate * First, see if this vnode is cached in the parent.
6190Sstevel@tonic-gate */
6200Sstevel@tonic-gate gfs_dir_lock(dp);
6210Sstevel@tonic-gate
6220Sstevel@tonic-gate /*
6230Sstevel@tonic-gate * Find it in the set of static entries.
6240Sstevel@tonic-gate */
6250Sstevel@tonic-gate for (i = 0; i < dp->gfsd_nstatic; i++) {
6260Sstevel@tonic-gate ge = &dp->gfsd_static[i];
6270Sstevel@tonic-gate
6280Sstevel@tonic-gate if (ge->gfse_vnode == vp)
6290Sstevel@tonic-gate goto found;
6300Sstevel@tonic-gate }
6310Sstevel@tonic-gate
6320Sstevel@tonic-gate /*
6330Sstevel@tonic-gate * If 'ge' is NULL, then it is a dynamic entry.
6340Sstevel@tonic-gate */
6350Sstevel@tonic-gate ge = NULL;
6360Sstevel@tonic-gate
6370Sstevel@tonic-gate found:
6385331Samw if (vp->v_flag & V_XATTRDIR) {
6395331Samw mutex_enter(&fp->gfs_parent->v_lock);
6405331Samw }
6410Sstevel@tonic-gate mutex_enter(&vp->v_lock);
6420Sstevel@tonic-gate if (vp->v_count == 1) {
6430Sstevel@tonic-gate /*
6440Sstevel@tonic-gate * Really remove this vnode
6450Sstevel@tonic-gate */
6460Sstevel@tonic-gate data = vp->v_data;
6470Sstevel@tonic-gate if (ge != NULL) {
6480Sstevel@tonic-gate /*
6490Sstevel@tonic-gate * If this was a statically cached entry, simply set the
6500Sstevel@tonic-gate * cached vnode to NULL.
6510Sstevel@tonic-gate */
6520Sstevel@tonic-gate ge->gfse_vnode = NULL;
6530Sstevel@tonic-gate }
6545331Samw if (vp->v_flag & V_XATTRDIR) {
6555331Samw fp->gfs_parent->v_xattrdir = NULL;
6565331Samw mutex_exit(&fp->gfs_parent->v_lock);
6575331Samw }
6580Sstevel@tonic-gate mutex_exit(&vp->v_lock);
6590Sstevel@tonic-gate
6600Sstevel@tonic-gate /*
6610Sstevel@tonic-gate * Free vnode and release parent
6620Sstevel@tonic-gate */
6630Sstevel@tonic-gate if (fp->gfs_parent) {
6645331Samw if (dp) {
6655331Samw gfs_dir_unlock(dp);
6665331Samw }
6670Sstevel@tonic-gate VN_RELE(fp->gfs_parent);
6680Sstevel@tonic-gate } else {
6690Sstevel@tonic-gate ASSERT(vp->v_vfsp != NULL);
6700Sstevel@tonic-gate VFS_RELE(vp->v_vfsp);
6710Sstevel@tonic-gate }
6720Sstevel@tonic-gate vn_free(vp);
6730Sstevel@tonic-gate } else {
6740Sstevel@tonic-gate vp->v_count--;
6750Sstevel@tonic-gate data = NULL;
6760Sstevel@tonic-gate mutex_exit(&vp->v_lock);
6775331Samw if (vp->v_flag & V_XATTRDIR) {
6785331Samw mutex_exit(&fp->gfs_parent->v_lock);
6795331Samw }
6800Sstevel@tonic-gate if (dp)
6810Sstevel@tonic-gate gfs_dir_unlock(dp);
6820Sstevel@tonic-gate }
6830Sstevel@tonic-gate
6840Sstevel@tonic-gate return (data);
6850Sstevel@tonic-gate }
6860Sstevel@tonic-gate
6870Sstevel@tonic-gate /*
6880Sstevel@tonic-gate * gfs_dir_inactive()
6890Sstevel@tonic-gate *
6900Sstevel@tonic-gate * Same as above, but for directories.
6910Sstevel@tonic-gate */
6920Sstevel@tonic-gate void *
gfs_dir_inactive(vnode_t * vp)6930Sstevel@tonic-gate gfs_dir_inactive(vnode_t *vp)
6940Sstevel@tonic-gate {
6950Sstevel@tonic-gate gfs_dir_t *dp;
6960Sstevel@tonic-gate
6970Sstevel@tonic-gate ASSERT(vp->v_type == VDIR);
6980Sstevel@tonic-gate
6990Sstevel@tonic-gate if ((dp = gfs_file_inactive(vp)) != NULL) {
7000Sstevel@tonic-gate mutex_destroy(&dp->gfsd_lock);
7010Sstevel@tonic-gate if (dp->gfsd_nstatic)
7020Sstevel@tonic-gate kmem_free(dp->gfsd_static,
7030Sstevel@tonic-gate dp->gfsd_nstatic * sizeof (gfs_dirent_t));
7040Sstevel@tonic-gate }
7050Sstevel@tonic-gate
7060Sstevel@tonic-gate return (dp);
7070Sstevel@tonic-gate }
7080Sstevel@tonic-gate
7090Sstevel@tonic-gate /*
710*6492Stimh * gfs_dir_lookup_dynamic()
7110Sstevel@tonic-gate *
712*6492Stimh * This routine looks up the provided name amongst the dynamic entries
713*6492Stimh * in the gfs directory and returns the corresponding vnode, if found.
7140Sstevel@tonic-gate *
715*6492Stimh * The gfs directory is expected to be locked by the caller prior to
716*6492Stimh * calling this function. The directory will be unlocked during the
717*6492Stimh * execution of this function, but will be locked upon return from the
718*6492Stimh * function. This function returns 0 on success, non-zero on error.
7190Sstevel@tonic-gate *
720*6492Stimh * The dynamic lookups are performed by invoking the lookup
721*6492Stimh * callback, which is passed to this function as the first argument.
722*6492Stimh * The arguments to the callback are:
7230Sstevel@tonic-gate *
724*6492Stimh * int gfs_lookup_cb(vnode_t *pvp, const char *nm, vnode_t **vpp, cred_t *cr,
725*6492Stimh * int flags, int *deflgs, pathname_t *rpnp);
7260Sstevel@tonic-gate *
7270Sstevel@tonic-gate * pvp - parent vnode
7280Sstevel@tonic-gate * nm - name of entry
7290Sstevel@tonic-gate * vpp - pointer to resulting vnode
7305331Samw * cr - pointer to cred
731*6492Stimh * flags - flags value from lookup request
732*6492Stimh * ignored here; currently only used to request
733*6492Stimh * insensitive lookups
734*6492Stimh * direntflgs - output parameter, directory entry flags
735*6492Stimh * ignored here; currently only used to indicate a lookup
736*6492Stimh * has more than one possible match when case is not considered
737*6492Stimh * realpnp - output parameter, real pathname
738*6492Stimh * ignored here; when lookup was performed case-insensitively,
739*6492Stimh * this field contains the "real" name of the file.
7400Sstevel@tonic-gate *
7410Sstevel@tonic-gate * Returns 0 on success, non-zero on error.
7420Sstevel@tonic-gate */
743*6492Stimh 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)744*6492Stimh gfs_dir_lookup_dynamic(gfs_lookup_cb callback, gfs_dir_t *dp,
745*6492Stimh const char *nm, vnode_t *dvp, vnode_t **vpp, cred_t *cr, int flags,
746*6492Stimh int *direntflags, pathname_t *realpnp)
7470Sstevel@tonic-gate {
748*6492Stimh gfs_file_t *fp;
749*6492Stimh ino64_t ino;
750*6492Stimh int ret;
751*6492Stimh
752*6492Stimh ASSERT(GFS_DIR_LOCKED(dp));
753*6492Stimh
754*6492Stimh /*
755*6492Stimh * Drop the directory lock, as the lookup routine
756*6492Stimh * will need to allocate memory, or otherwise deadlock on this
757*6492Stimh * directory.
758*6492Stimh */
759*6492Stimh gfs_dir_unlock(dp);
760*6492Stimh ret = callback(dvp, nm, vpp, &ino, cr, flags, direntflags, realpnp);
761*6492Stimh gfs_dir_lock(dp);
762*6492Stimh
763*6492Stimh /*
764*6492Stimh * The callback for extended attributes returns a vnode
765*6492Stimh * with v_data from an underlying fs.
766*6492Stimh */
767*6492Stimh if (ret == 0 && !IS_XATTRDIR(dvp)) {
768*6492Stimh fp = (gfs_file_t *)((*vpp)->v_data);
769*6492Stimh fp->gfs_index = -1;
770*6492Stimh fp->gfs_ino = ino;
771*6492Stimh }
772*6492Stimh
773*6492Stimh return (ret);
774*6492Stimh }
775*6492Stimh
776*6492Stimh /*
777*6492Stimh * gfs_dir_lookup_static()
778*6492Stimh *
779*6492Stimh * This routine looks up the provided name amongst the static entries
780*6492Stimh * in the gfs directory and returns the corresponding vnode, if found.
781*6492Stimh * The first argument to the function is a pointer to the comparison
782*6492Stimh * function this function should use to decide if names are a match.
783*6492Stimh *
784*6492Stimh * If a match is found, and GFS_CACHE_VNODE is set and the vnode
785*6492Stimh * exists, we simply return the existing vnode. Otherwise, we call
786*6492Stimh * the static entry's callback routine, caching the result if
787*6492Stimh * necessary. If the idx pointer argument is non-NULL, we use it to
788*6492Stimh * return the index of the matching static entry.
789*6492Stimh *
790*6492Stimh * The gfs directory is expected to be locked by the caller prior to calling
791*6492Stimh * this function. The directory may be unlocked during the execution of
792*6492Stimh * this function, but will be locked upon return from the function.
793*6492Stimh *
794*6492Stimh * This function returns 0 if a match is found, ENOENT if not.
795*6492Stimh */
796*6492Stimh 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)797*6492Stimh gfs_dir_lookup_static(int (*compare)(const char *, const char *),
798*6492Stimh gfs_dir_t *dp, const char *nm, vnode_t *dvp, int *idx,
799*6492Stimh vnode_t **vpp, pathname_t *rpnp)
800*6492Stimh {
8010Sstevel@tonic-gate gfs_dirent_t *ge;
802*6492Stimh vnode_t *vp = NULL;
803*6492Stimh int i;
8040Sstevel@tonic-gate
805*6492Stimh ASSERT(GFS_DIR_LOCKED(dp));
8060Sstevel@tonic-gate
8070Sstevel@tonic-gate /*
8080Sstevel@tonic-gate * Search static entries.
8090Sstevel@tonic-gate */
8100Sstevel@tonic-gate for (i = 0; i < dp->gfsd_nstatic; i++) {
8110Sstevel@tonic-gate ge = &dp->gfsd_static[i];
8120Sstevel@tonic-gate
813*6492Stimh if (compare(ge->gfse_name, nm) == 0) {
814*6492Stimh if (rpnp)
815*6492Stimh (void) strlcpy(rpnp->pn_buf, ge->gfse_name,
816*6492Stimh rpnp->pn_bufsize);
817*6492Stimh
8180Sstevel@tonic-gate if (ge->gfse_vnode) {
8190Sstevel@tonic-gate ASSERT(ge->gfse_flags & GFS_CACHE_VNODE);
8200Sstevel@tonic-gate vp = ge->gfse_vnode;
8210Sstevel@tonic-gate VN_HOLD(vp);
822*6492Stimh break;
8230Sstevel@tonic-gate }
8240Sstevel@tonic-gate
8250Sstevel@tonic-gate /*
8263921Sjk115741 * We drop the directory lock, as the constructor will
8270Sstevel@tonic-gate * need to do KM_SLEEP allocations. If we return from
8280Sstevel@tonic-gate * the constructor only to find that a parallel
8290Sstevel@tonic-gate * operation has completed, and GFS_CACHE_VNODE is set
830*6492Stimh * for this entry, we discard the result in favor of
831*6492Stimh * the cached vnode.
8320Sstevel@tonic-gate */
8330Sstevel@tonic-gate gfs_dir_unlock(dp);
8340Sstevel@tonic-gate vp = ge->gfse_ctor(dvp);
8350Sstevel@tonic-gate gfs_dir_lock(dp);
8360Sstevel@tonic-gate
8370Sstevel@tonic-gate ((gfs_file_t *)vp->v_data)->gfs_index = i;
8380Sstevel@tonic-gate
8390Sstevel@tonic-gate /* Set the inode according to the callback. */
8400Sstevel@tonic-gate ((gfs_file_t *)vp->v_data)->gfs_ino =
8410Sstevel@tonic-gate dp->gfsd_inode(dvp, i);
8420Sstevel@tonic-gate
8430Sstevel@tonic-gate if (ge->gfse_flags & GFS_CACHE_VNODE) {
8440Sstevel@tonic-gate if (ge->gfse_vnode == NULL) {
8450Sstevel@tonic-gate ge->gfse_vnode = vp;
8460Sstevel@tonic-gate } else {
8470Sstevel@tonic-gate /*
8480Sstevel@tonic-gate * A parallel constructor beat us to it;
8490Sstevel@tonic-gate * return existing vnode. We have to be
8500Sstevel@tonic-gate * careful because we can't release the
8510Sstevel@tonic-gate * current vnode while holding the
8520Sstevel@tonic-gate * directory lock; its inactive routine
8530Sstevel@tonic-gate * will try to lock this directory.
8540Sstevel@tonic-gate */
8550Sstevel@tonic-gate vnode_t *oldvp = vp;
8560Sstevel@tonic-gate vp = ge->gfse_vnode;
8570Sstevel@tonic-gate VN_HOLD(vp);
8580Sstevel@tonic-gate
8590Sstevel@tonic-gate gfs_dir_unlock(dp);
8600Sstevel@tonic-gate VN_RELE(oldvp);
8610Sstevel@tonic-gate gfs_dir_lock(dp);
8620Sstevel@tonic-gate }
8630Sstevel@tonic-gate }
864*6492Stimh break;
8650Sstevel@tonic-gate }
8660Sstevel@tonic-gate }
8670Sstevel@tonic-gate
868*6492Stimh if (vp == NULL)
869*6492Stimh return (ENOENT);
870*6492Stimh else if (idx)
871*6492Stimh *idx = i;
872*6492Stimh *vpp = vp;
873*6492Stimh return (0);
874*6492Stimh }
8750Sstevel@tonic-gate
876*6492Stimh /*
877*6492Stimh * gfs_dir_lookup()
878*6492Stimh *
879*6492Stimh * Looks up the given name in the directory and returns the corresponding
880*6492Stimh * vnode, if found.
881*6492Stimh *
882*6492Stimh * First, we search statically defined entries, if any, with a call to
883*6492Stimh * gfs_dir_lookup_static(). If no static entry is found, and we have
884*6492Stimh * a callback function we try a dynamic lookup via gfs_dir_lookup_dynamic().
885*6492Stimh *
886*6492Stimh * This function returns 0 on success, non-zero on error.
887*6492Stimh */
888*6492Stimh int
gfs_dir_lookup(vnode_t * dvp,const char * nm,vnode_t ** vpp,cred_t * cr,int flags,int * direntflags,pathname_t * realpnp)889*6492Stimh gfs_dir_lookup(vnode_t *dvp, const char *nm, vnode_t **vpp, cred_t *cr,
890*6492Stimh int flags, int *direntflags, pathname_t *realpnp)
891*6492Stimh {
892*6492Stimh gfs_dir_t *dp = dvp->v_data;
893*6492Stimh boolean_t casecheck;
894*6492Stimh vnode_t *dynvp = NULL;
895*6492Stimh vnode_t *vp = NULL;
896*6492Stimh int (*compare)(const char *, const char *);
897*6492Stimh int error, idx;
898*6492Stimh
899*6492Stimh ASSERT(dvp->v_type == VDIR);
900*6492Stimh
901*6492Stimh if (gfs_lookup_dot(vpp, dvp, dp->gfsd_file.gfs_parent, nm) == 0)
902*6492Stimh return (0);
9030Sstevel@tonic-gate
904*6492Stimh casecheck = (flags & FIGNORECASE) != 0 && direntflags != NULL;
905*6492Stimh if (vfs_has_feature(dvp->v_vfsp, VFSFT_NOCASESENSITIVE) ||
906*6492Stimh (flags & FIGNORECASE))
907*6492Stimh compare = strcasecmp;
908*6492Stimh else
909*6492Stimh compare = strcmp;
910*6492Stimh
911*6492Stimh gfs_dir_lock(dp);
912*6492Stimh
913*6492Stimh error = gfs_dir_lookup_static(compare, dp, nm, dvp, &idx, &vp, realpnp);
914*6492Stimh
915*6492Stimh if (vp && casecheck) {
916*6492Stimh gfs_dirent_t *ge;
917*6492Stimh int i;
918*6492Stimh
919*6492Stimh for (i = idx + 1; i < dp->gfsd_nstatic; i++) {
920*6492Stimh ge = &dp->gfsd_static[i];
921*6492Stimh
922*6492Stimh if (strcasecmp(ge->gfse_name, nm) == 0) {
923*6492Stimh *direntflags |= ED_CASE_CONFLICT;
924*6492Stimh goto out;
925*6492Stimh }
9265331Samw }
927*6492Stimh }
928*6492Stimh
929*6492Stimh if ((error || casecheck) && dp->gfsd_lookup)
930*6492Stimh error = gfs_dir_lookup_dynamic(dp->gfsd_lookup, dp, nm, dvp,
931*6492Stimh &dynvp, cr, flags, direntflags, vp ? NULL : realpnp);
932*6492Stimh
933*6492Stimh if (vp && dynvp) {
934*6492Stimh /* static and dynamic entries are case-insensitive conflict */
935*6492Stimh ASSERT(casecheck);
936*6492Stimh *direntflags |= ED_CASE_CONFLICT;
937*6492Stimh VN_RELE(dynvp);
938*6492Stimh } else if (vp == NULL) {
939*6492Stimh vp = dynvp;
940*6492Stimh } else if (error == ENOENT) {
941*6492Stimh error = 0;
942*6492Stimh } else if (error) {
943*6492Stimh VN_RELE(vp);
944*6492Stimh vp = NULL;
9450Sstevel@tonic-gate }
9460Sstevel@tonic-gate
9470Sstevel@tonic-gate out:
9480Sstevel@tonic-gate gfs_dir_unlock(dp);
9490Sstevel@tonic-gate
950*6492Stimh *vpp = vp;
951*6492Stimh return (error);
9520Sstevel@tonic-gate }
9530Sstevel@tonic-gate
9540Sstevel@tonic-gate /*
9550Sstevel@tonic-gate * gfs_dir_readdir: does a readdir() on the given directory
9560Sstevel@tonic-gate *
9570Sstevel@tonic-gate * dvp - directory vnode
9580Sstevel@tonic-gate * uiop - uio structure
9590Sstevel@tonic-gate * eofp - eof pointer
9600Sstevel@tonic-gate * data - arbitrary data passed to readdir callback
9610Sstevel@tonic-gate *
9620Sstevel@tonic-gate * This routine does all the readdir() dirty work. Even so, the caller must
9630Sstevel@tonic-gate * supply two callbacks in order to get full compatibility.
9640Sstevel@tonic-gate *
9650Sstevel@tonic-gate * If the directory contains static entries, an inode callback must be
9660Sstevel@tonic-gate * specified. This avoids having to create every vnode and call VOP_GETATTR()
9670Sstevel@tonic-gate * when reading the directory. This function has the following arguments:
9680Sstevel@tonic-gate *
9690Sstevel@tonic-gate * ino_t gfs_inode_cb(vnode_t *vp, int index);
9700Sstevel@tonic-gate *
9710Sstevel@tonic-gate * vp - vnode for the directory
9720Sstevel@tonic-gate * index - index in original gfs_dirent_t array
9730Sstevel@tonic-gate *
9740Sstevel@tonic-gate * Returns the inode number for the given entry.
9750Sstevel@tonic-gate *
9760Sstevel@tonic-gate * For directories with dynamic entries, a readdir callback must be provided.
9770Sstevel@tonic-gate * This is significantly more complex, thanks to the particulars of
9780Sstevel@tonic-gate * VOP_READDIR().
9790Sstevel@tonic-gate *
9805663Sck153898 * int gfs_readdir_cb(vnode_t *vp, void *dp, int *eofp,
9815663Sck153898 * offset_t *off, offset_t *nextoff, void *data, int flags)
9820Sstevel@tonic-gate *
9830Sstevel@tonic-gate * vp - directory vnode
9840Sstevel@tonic-gate * dp - directory entry, sized according to maxlen given to
9850Sstevel@tonic-gate * gfs_dir_create(). callback must fill in d_name and
9865663Sck153898 * d_ino (if a dirent64_t), or ed_name, ed_ino, and ed_eflags
9875663Sck153898 * (if an edirent_t). edirent_t is used if V_RDDIR_ENTFLAGS
9885663Sck153898 * is set in 'flags'.
9890Sstevel@tonic-gate * eofp - callback must set to 1 when EOF has been reached
9900Sstevel@tonic-gate * off - on entry, the last offset read from the directory. Callback
9910Sstevel@tonic-gate * must set to the offset of the current entry, typically left
9920Sstevel@tonic-gate * untouched.
9930Sstevel@tonic-gate * nextoff - callback must set to offset of next entry. Typically
9940Sstevel@tonic-gate * (off + 1)
9950Sstevel@tonic-gate * data - caller-supplied data
9965663Sck153898 * flags - VOP_READDIR flags
9970Sstevel@tonic-gate *
9980Sstevel@tonic-gate * Return 0 on success, or error on failure.
9990Sstevel@tonic-gate */
10000Sstevel@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)10015331Samw gfs_dir_readdir(vnode_t *dvp, uio_t *uiop, int *eofp, void *data, cred_t *cr,
10025663Sck153898 caller_context_t *ct, int flags)
10030Sstevel@tonic-gate {
10040Sstevel@tonic-gate gfs_readdir_state_t gstate;
10050Sstevel@tonic-gate int error, eof = 0;
10060Sstevel@tonic-gate ino64_t ino, pino;
10070Sstevel@tonic-gate offset_t off, next;
10080Sstevel@tonic-gate gfs_dir_t *dp = dvp->v_data;
10090Sstevel@tonic-gate
10105663Sck153898 error = gfs_get_parent_ino(dvp, cr, ct, &pino, &ino);
10115663Sck153898 if (error)
10125663Sck153898 return (error);
10130Sstevel@tonic-gate
10140Sstevel@tonic-gate if ((error = gfs_readdir_init(&gstate, dp->gfsd_maxlen, 1, uiop,
10155663Sck153898 pino, ino, flags)) != 0)
10160Sstevel@tonic-gate return (error);
10170Sstevel@tonic-gate
10180Sstevel@tonic-gate while ((error = gfs_readdir_pred(&gstate, uiop, &off)) == 0 &&
10190Sstevel@tonic-gate !eof) {
10200Sstevel@tonic-gate
10210Sstevel@tonic-gate if (off >= 0 && off < dp->gfsd_nstatic) {
10220Sstevel@tonic-gate ino = dp->gfsd_inode(dvp, off);
10230Sstevel@tonic-gate
10240Sstevel@tonic-gate if ((error = gfs_readdir_emit(&gstate, uiop,
10255663Sck153898 off, ino, dp->gfsd_static[off].gfse_name, 0))
10260Sstevel@tonic-gate != 0)
10270Sstevel@tonic-gate break;
10280Sstevel@tonic-gate
10290Sstevel@tonic-gate } else if (dp->gfsd_readdir) {
10300Sstevel@tonic-gate off -= dp->gfsd_nstatic;
10310Sstevel@tonic-gate
10320Sstevel@tonic-gate if ((error = dp->gfsd_readdir(dvp,
10330Sstevel@tonic-gate gstate.grd_dirent, &eof, &off, &next,
10345663Sck153898 data, flags)) != 0 || eof)
10350Sstevel@tonic-gate break;
10360Sstevel@tonic-gate
10370Sstevel@tonic-gate off += dp->gfsd_nstatic + 2;
10380Sstevel@tonic-gate next += dp->gfsd_nstatic + 2;
10390Sstevel@tonic-gate
10400Sstevel@tonic-gate if ((error = gfs_readdir_emit_int(&gstate, uiop,
1041847Smaybee next)) != 0)
10420Sstevel@tonic-gate break;
10430Sstevel@tonic-gate } else {
10440Sstevel@tonic-gate /*
10450Sstevel@tonic-gate * Offset is beyond the end of the static entries, and
10460Sstevel@tonic-gate * we have no dynamic entries. Set EOF.
10470Sstevel@tonic-gate */
10480Sstevel@tonic-gate eof = 1;
10490Sstevel@tonic-gate }
10500Sstevel@tonic-gate }
10510Sstevel@tonic-gate
10520Sstevel@tonic-gate return (gfs_readdir_fini(&gstate, error, eofp, eof));
10530Sstevel@tonic-gate }
10540Sstevel@tonic-gate
10550Sstevel@tonic-gate
10560Sstevel@tonic-gate /*
10570Sstevel@tonic-gate * gfs_vop_lookup: VOP_LOOKUP() entry point
10580Sstevel@tonic-gate *
10590Sstevel@tonic-gate * For use directly in vnode ops table. Given a GFS directory, calls
10600Sstevel@tonic-gate * gfs_dir_lookup() as necessary.
10610Sstevel@tonic-gate */
10620Sstevel@tonic-gate /* ARGSUSED */
10630Sstevel@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)10640Sstevel@tonic-gate gfs_vop_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, pathname_t *pnp,
10655331Samw int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct,
10665331Samw int *direntflags, pathname_t *realpnp)
10670Sstevel@tonic-gate {
1068*6492Stimh return (gfs_dir_lookup(dvp, nm, vpp, cr, flags, direntflags, realpnp));
10690Sstevel@tonic-gate }
10700Sstevel@tonic-gate
10710Sstevel@tonic-gate /*
10720Sstevel@tonic-gate * gfs_vop_readdir: VOP_READDIR() entry point
10730Sstevel@tonic-gate *
10740Sstevel@tonic-gate * For use directly in vnode ops table. Given a GFS directory, calls
10750Sstevel@tonic-gate * gfs_dir_readdir() as necessary.
10760Sstevel@tonic-gate */
10770Sstevel@tonic-gate /* ARGSUSED */
10780Sstevel@tonic-gate int
gfs_vop_readdir(vnode_t * vp,uio_t * uiop,cred_t * cr,int * eofp,caller_context_t * ct,int flags)10795331Samw gfs_vop_readdir(vnode_t *vp, uio_t *uiop, cred_t *cr, int *eofp,
10805331Samw caller_context_t *ct, int flags)
10810Sstevel@tonic-gate {
10825663Sck153898 return (gfs_dir_readdir(vp, uiop, eofp, NULL, cr, ct, flags));
10830Sstevel@tonic-gate }
10840Sstevel@tonic-gate
10850Sstevel@tonic-gate
10860Sstevel@tonic-gate /*
10870Sstevel@tonic-gate * gfs_vop_map: VOP_MAP() entry point
10880Sstevel@tonic-gate *
10890Sstevel@tonic-gate * Convenient routine for handling pseudo-files that wish to allow mmap() calls.
10900Sstevel@tonic-gate * This function only works for readonly files, and uses the read function for
10910Sstevel@tonic-gate * the vnode to fill in the data. The mapped data is immediately faulted in and
10920Sstevel@tonic-gate * filled with the necessary data during this call; there are no getpage() or
10930Sstevel@tonic-gate * putpage() routines.
10940Sstevel@tonic-gate */
10950Sstevel@tonic-gate /* ARGSUSED */
10960Sstevel@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)10970Sstevel@tonic-gate gfs_vop_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp,
10985331Samw size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cred,
10995331Samw caller_context_t *ct)
11000Sstevel@tonic-gate {
11010Sstevel@tonic-gate int rv;
11020Sstevel@tonic-gate ssize_t resid = len;
11030Sstevel@tonic-gate
11040Sstevel@tonic-gate /*
11050Sstevel@tonic-gate * Check for bad parameters
11060Sstevel@tonic-gate */
11070Sstevel@tonic-gate #ifdef _ILP32
11080Sstevel@tonic-gate if (len > MAXOFF_T)
11090Sstevel@tonic-gate return (ENOMEM);
11100Sstevel@tonic-gate #endif
11110Sstevel@tonic-gate if (vp->v_flag & VNOMAP)
11120Sstevel@tonic-gate return (ENOTSUP);
11130Sstevel@tonic-gate if (off > MAXOFF_T)
11140Sstevel@tonic-gate return (EFBIG);
11150Sstevel@tonic-gate if ((long)off < 0 || (long)(off + len) < 0)
11160Sstevel@tonic-gate return (EINVAL);
11170Sstevel@tonic-gate if (vp->v_type != VREG)
11180Sstevel@tonic-gate return (ENODEV);
11190Sstevel@tonic-gate if ((prot & (PROT_EXEC | PROT_WRITE)) != 0)
11200Sstevel@tonic-gate return (EACCES);
11210Sstevel@tonic-gate
11220Sstevel@tonic-gate /*
11230Sstevel@tonic-gate * Find appropriate address if needed, otherwise clear address range.
11240Sstevel@tonic-gate */
11250Sstevel@tonic-gate as_rangelock(as);
11266036Smec rv = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags);
11276036Smec if (rv != 0) {
11286036Smec as_rangeunlock(as);
11296036Smec return (rv);
11300Sstevel@tonic-gate }
11310Sstevel@tonic-gate
11320Sstevel@tonic-gate /*
11330Sstevel@tonic-gate * Create mapping
11340Sstevel@tonic-gate */
11350Sstevel@tonic-gate rv = as_map(as, *addrp, len, segvn_create, zfod_argsp);
11360Sstevel@tonic-gate as_rangeunlock(as);
11370Sstevel@tonic-gate if (rv != 0)
11380Sstevel@tonic-gate return (rv);
11390Sstevel@tonic-gate
11400Sstevel@tonic-gate /*
11410Sstevel@tonic-gate * Fill with data from read()
11420Sstevel@tonic-gate */
11430Sstevel@tonic-gate rv = vn_rdwr(UIO_READ, vp, *addrp, len, off, UIO_USERSPACE,
11440Sstevel@tonic-gate 0, (rlim64_t)0, cred, &resid);
11450Sstevel@tonic-gate
11460Sstevel@tonic-gate if (rv == 0 && resid != 0)
11470Sstevel@tonic-gate rv = ENXIO;
11480Sstevel@tonic-gate
11490Sstevel@tonic-gate if (rv != 0) {
11500Sstevel@tonic-gate as_rangelock(as);
11510Sstevel@tonic-gate (void) as_unmap(as, *addrp, len);
11520Sstevel@tonic-gate as_rangeunlock(as);
11530Sstevel@tonic-gate }
11540Sstevel@tonic-gate
11550Sstevel@tonic-gate return (rv);
11560Sstevel@tonic-gate }
11570Sstevel@tonic-gate
11580Sstevel@tonic-gate /*
11590Sstevel@tonic-gate * gfs_vop_inactive: VOP_INACTIVE() entry point
11600Sstevel@tonic-gate *
11610Sstevel@tonic-gate * Given a vnode that is a GFS file or directory, call gfs_file_inactive() or
11620Sstevel@tonic-gate * gfs_dir_inactive() as necessary, and kmem_free()s associated private data.
11630Sstevel@tonic-gate */
11640Sstevel@tonic-gate /* ARGSUSED */
11650Sstevel@tonic-gate void
gfs_vop_inactive(vnode_t * vp,cred_t * cr,caller_context_t * ct)11665331Samw gfs_vop_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
11670Sstevel@tonic-gate {
11680Sstevel@tonic-gate gfs_file_t *fp = vp->v_data;
11690Sstevel@tonic-gate void *data;
11700Sstevel@tonic-gate
11710Sstevel@tonic-gate if (fp->gfs_type == GFS_DIR)
11720Sstevel@tonic-gate data = gfs_dir_inactive(vp);
11730Sstevel@tonic-gate else
11740Sstevel@tonic-gate data = gfs_file_inactive(vp);
11750Sstevel@tonic-gate
11760Sstevel@tonic-gate if (data != NULL)
11770Sstevel@tonic-gate kmem_free(data, fp->gfs_size);
11780Sstevel@tonic-gate }
1179