1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 25*7c478bd9Sstevel@tonic-gate */ 26*7c478bd9Sstevel@tonic-gate 27*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*7c478bd9Sstevel@tonic-gate 29*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 30*7c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h> 31*7c478bd9Sstevel@tonic-gate #include <sys/debug.h> 32*7c478bd9Sstevel@tonic-gate #include <sys/dirent.h> 33*7c478bd9Sstevel@tonic-gate #include <sys/kmem.h> 34*7c478bd9Sstevel@tonic-gate #include <sys/mman.h> 35*7c478bd9Sstevel@tonic-gate #include <sys/mutex.h> 36*7c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 37*7c478bd9Sstevel@tonic-gate #include <sys/systm.h> 38*7c478bd9Sstevel@tonic-gate #include <sys/uio.h> 39*7c478bd9Sstevel@tonic-gate #include <sys/vmsystm.h> 40*7c478bd9Sstevel@tonic-gate #include <sys/vfs.h> 41*7c478bd9Sstevel@tonic-gate #include <sys/vnode.h> 42*7c478bd9Sstevel@tonic-gate 43*7c478bd9Sstevel@tonic-gate #include <vm/as.h> 44*7c478bd9Sstevel@tonic-gate #include <vm/seg_vn.h> 45*7c478bd9Sstevel@tonic-gate 46*7c478bd9Sstevel@tonic-gate #include <sys/gfs.h> 47*7c478bd9Sstevel@tonic-gate 48*7c478bd9Sstevel@tonic-gate /* 49*7c478bd9Sstevel@tonic-gate * Generic pseudo-filesystem routines. 50*7c478bd9Sstevel@tonic-gate * 51*7c478bd9Sstevel@tonic-gate * There are significant similarities between the implementation of certain file 52*7c478bd9Sstevel@tonic-gate * system entry points across different filesystems. While one could attempt to 53*7c478bd9Sstevel@tonic-gate * "choke up on the bat" and incorporate common functionality into a VOP 54*7c478bd9Sstevel@tonic-gate * preamable or postamble, such an approach is limited in the benefit it can 55*7c478bd9Sstevel@tonic-gate * provide. In this file we instead define a toolkit of routines which can be 56*7c478bd9Sstevel@tonic-gate * called from a filesystem (with in-kernel pseudo-filesystems being the focus 57*7c478bd9Sstevel@tonic-gate * of the exercise) in a more component-like fashion. 58*7c478bd9Sstevel@tonic-gate * 59*7c478bd9Sstevel@tonic-gate * There are three basic classes of routines: 60*7c478bd9Sstevel@tonic-gate * 61*7c478bd9Sstevel@tonic-gate * 1) Lowlevel support routines 62*7c478bd9Sstevel@tonic-gate * 63*7c478bd9Sstevel@tonic-gate * These routines are designed to play a support role for existing 64*7c478bd9Sstevel@tonic-gate * pseudo-filesystems (such as procfs). They simplif ycommon tasks, 65*7c478bd9Sstevel@tonic-gate * without enforcing the filesystem to hand over management to GFS. The 66*7c478bd9Sstevel@tonic-gate * routines covered are: 67*7c478bd9Sstevel@tonic-gate * 68*7c478bd9Sstevel@tonic-gate * gfs_readdir_init() 69*7c478bd9Sstevel@tonic-gate * gfs_readdir_emit() 70*7c478bd9Sstevel@tonic-gate * gfs_readdir_emitn() 71*7c478bd9Sstevel@tonic-gate * gfs_readdir_pred() 72*7c478bd9Sstevel@tonic-gate * gfs_readdir_fini() 73*7c478bd9Sstevel@tonic-gate * gfs_lookup_dot() 74*7c478bd9Sstevel@tonic-gate * 75*7c478bd9Sstevel@tonic-gate * 2) Complete GFS management 76*7c478bd9Sstevel@tonic-gate * 77*7c478bd9Sstevel@tonic-gate * These routines take a more active role in management of the 78*7c478bd9Sstevel@tonic-gate * pseudo-filesystem. They handle the relationship between vnode private 79*7c478bd9Sstevel@tonic-gate * data and VFS data, as well as the relationship between vnodes in the 80*7c478bd9Sstevel@tonic-gate * directory heirarchy. 81*7c478bd9Sstevel@tonic-gate * 82*7c478bd9Sstevel@tonic-gate * In order to use these interfaces, the first member of every private 83*7c478bd9Sstevel@tonic-gate * v_data must be a gfs_file_t or a gfs_dir_t. This hands over all control 84*7c478bd9Sstevel@tonic-gate * to GFS. 85*7c478bd9Sstevel@tonic-gate * 86*7c478bd9Sstevel@tonic-gate * gfs_file_create() 87*7c478bd9Sstevel@tonic-gate * gfs_dir_create() 88*7c478bd9Sstevel@tonic-gate * gfs_root_create() 89*7c478bd9Sstevel@tonic-gate * 90*7c478bd9Sstevel@tonic-gate * gfs_file_inactive() 91*7c478bd9Sstevel@tonic-gate * gfs_dir_inactive() 92*7c478bd9Sstevel@tonic-gate * gfs_dir_lookup() 93*7c478bd9Sstevel@tonic-gate * gfs_dir_readdir() 94*7c478bd9Sstevel@tonic-gate * 95*7c478bd9Sstevel@tonic-gate * gfs_vop_inactive() 96*7c478bd9Sstevel@tonic-gate * gfs_vop_lookup() 97*7c478bd9Sstevel@tonic-gate * gfs_vop_readdir() 98*7c478bd9Sstevel@tonic-gate * gfs_vop_map() 99*7c478bd9Sstevel@tonic-gate */ 100*7c478bd9Sstevel@tonic-gate 101*7c478bd9Sstevel@tonic-gate /* 102*7c478bd9Sstevel@tonic-gate * gfs_make_opsvec: take an array of vnode type definitions and create 103*7c478bd9Sstevel@tonic-gate * their vnodeops_t structures 104*7c478bd9Sstevel@tonic-gate * 105*7c478bd9Sstevel@tonic-gate * This routine takes an array of gfs_opsvec_t's. It could 106*7c478bd9Sstevel@tonic-gate * alternatively take an array of gfs_opsvec_t*'s, which would allow 107*7c478bd9Sstevel@tonic-gate * vnode types to be completely defined in files external to the caller 108*7c478bd9Sstevel@tonic-gate * of gfs_make_opsvec(). As it stands, much more sharing takes place -- 109*7c478bd9Sstevel@tonic-gate * both the caller and the vnode type provider need to access gfsv_ops 110*7c478bd9Sstevel@tonic-gate * and gfsv_template, and the caller also needs to know gfsv_name. 111*7c478bd9Sstevel@tonic-gate */ 112*7c478bd9Sstevel@tonic-gate int 113*7c478bd9Sstevel@tonic-gate gfs_make_opsvec(gfs_opsvec_t *vec) 114*7c478bd9Sstevel@tonic-gate { 115*7c478bd9Sstevel@tonic-gate int error, i; 116*7c478bd9Sstevel@tonic-gate 117*7c478bd9Sstevel@tonic-gate for (i = 0; ; i++) { 118*7c478bd9Sstevel@tonic-gate if (vec[i].gfsv_name == NULL) 119*7c478bd9Sstevel@tonic-gate return (0); 120*7c478bd9Sstevel@tonic-gate error = vn_make_ops(vec[i].gfsv_name, vec[i].gfsv_template, 121*7c478bd9Sstevel@tonic-gate vec[i].gfsv_ops); 122*7c478bd9Sstevel@tonic-gate if (error) 123*7c478bd9Sstevel@tonic-gate break; 124*7c478bd9Sstevel@tonic-gate } 125*7c478bd9Sstevel@tonic-gate 126*7c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "gfs_make_opsvec: bad vnode ops template for '%s'", 127*7c478bd9Sstevel@tonic-gate vec[i].gfsv_name); 128*7c478bd9Sstevel@tonic-gate for (i--; i >= 0; i--) { 129*7c478bd9Sstevel@tonic-gate vn_freevnodeops(*vec[i].gfsv_ops); 130*7c478bd9Sstevel@tonic-gate *vec[i].gfsv_ops = NULL; 131*7c478bd9Sstevel@tonic-gate } 132*7c478bd9Sstevel@tonic-gate return (error); 133*7c478bd9Sstevel@tonic-gate } 134*7c478bd9Sstevel@tonic-gate 135*7c478bd9Sstevel@tonic-gate /* 136*7c478bd9Sstevel@tonic-gate * Low level directory routines 137*7c478bd9Sstevel@tonic-gate * 138*7c478bd9Sstevel@tonic-gate * These routines provide some simple abstractions for reading directories. 139*7c478bd9Sstevel@tonic-gate * They are designed to be used by existing pseudo filesystems (namely procfs) 140*7c478bd9Sstevel@tonic-gate * that already have a complicated management infrastructure. 141*7c478bd9Sstevel@tonic-gate */ 142*7c478bd9Sstevel@tonic-gate 143*7c478bd9Sstevel@tonic-gate /* 144*7c478bd9Sstevel@tonic-gate * gfs_readdir_init: initiate a generic readdir 145*7c478bd9Sstevel@tonic-gate * st - a pointer to an uninitialized gfs_readdir_state_t structure 146*7c478bd9Sstevel@tonic-gate * name_max - the directory's maximum file name length 147*7c478bd9Sstevel@tonic-gate * ureclen - the exported file-space record length (1 for non-legacy FSs) 148*7c478bd9Sstevel@tonic-gate * uiop - the uiop passed to readdir 149*7c478bd9Sstevel@tonic-gate * parent - the parent directory's inode 150*7c478bd9Sstevel@tonic-gate * self - this directory's inode 151*7c478bd9Sstevel@tonic-gate * 152*7c478bd9Sstevel@tonic-gate * Returns 0 or a non-zero errno. 153*7c478bd9Sstevel@tonic-gate * 154*7c478bd9Sstevel@tonic-gate * Typical VOP_READDIR usage of gfs_readdir_*: 155*7c478bd9Sstevel@tonic-gate * 156*7c478bd9Sstevel@tonic-gate * if ((error = gfs_readdir_init(...)) != 0) 157*7c478bd9Sstevel@tonic-gate * return (error); 158*7c478bd9Sstevel@tonic-gate * eof = 0; 159*7c478bd9Sstevel@tonic-gate * while ((error = gfs_readdir_pred(..., &voffset)) != 0) { 160*7c478bd9Sstevel@tonic-gate * if (!consumer_entry_at(voffset)) 161*7c478bd9Sstevel@tonic-gate * voffset = consumer_next_entry(voffset); 162*7c478bd9Sstevel@tonic-gate * if (consumer_eof(voffset)) { 163*7c478bd9Sstevel@tonic-gate * eof = 1 164*7c478bd9Sstevel@tonic-gate * break; 165*7c478bd9Sstevel@tonic-gate * } 166*7c478bd9Sstevel@tonic-gate * if ((error = gfs_readdir_emit(..., voffset, 167*7c478bd9Sstevel@tonic-gate * consumer_ino(voffset), consumer_name(voffset))) != 0) 168*7c478bd9Sstevel@tonic-gate * break; 169*7c478bd9Sstevel@tonic-gate * } 170*7c478bd9Sstevel@tonic-gate * return (gfs_readdir_fini(..., error, eofp, eof)); 171*7c478bd9Sstevel@tonic-gate * 172*7c478bd9Sstevel@tonic-gate * As you can see, a zero result from gfs_readdir_pred() or 173*7c478bd9Sstevel@tonic-gate * gfs_readdir_emit() indicates that processing should continue, 174*7c478bd9Sstevel@tonic-gate * whereas a non-zero result indicates that the loop should terminate. 175*7c478bd9Sstevel@tonic-gate * Most consumers need do nothing more than let gfs_readdir_fini() 176*7c478bd9Sstevel@tonic-gate * determine what the cause of failure was and return the appropriate 177*7c478bd9Sstevel@tonic-gate * value. 178*7c478bd9Sstevel@tonic-gate */ 179*7c478bd9Sstevel@tonic-gate int 180*7c478bd9Sstevel@tonic-gate gfs_readdir_init(gfs_readdir_state_t *st, int name_max, int ureclen, 181*7c478bd9Sstevel@tonic-gate uio_t *uiop, ino64_t parent, ino64_t self) 182*7c478bd9Sstevel@tonic-gate { 183*7c478bd9Sstevel@tonic-gate if (uiop->uio_loffset < 0 || uiop->uio_resid <= 0 || 184*7c478bd9Sstevel@tonic-gate (uiop->uio_loffset % ureclen) != 0) 185*7c478bd9Sstevel@tonic-gate return (EINVAL); 186*7c478bd9Sstevel@tonic-gate 187*7c478bd9Sstevel@tonic-gate st->grd_ureclen = ureclen; 188*7c478bd9Sstevel@tonic-gate st->grd_oresid = uiop->uio_resid; 189*7c478bd9Sstevel@tonic-gate st->grd_namlen = name_max; 190*7c478bd9Sstevel@tonic-gate st->grd_dirent = kmem_zalloc(DIRENT64_RECLEN(st->grd_namlen), KM_SLEEP); 191*7c478bd9Sstevel@tonic-gate st->grd_parent = parent; 192*7c478bd9Sstevel@tonic-gate st->grd_self = self; 193*7c478bd9Sstevel@tonic-gate 194*7c478bd9Sstevel@tonic-gate return (0); 195*7c478bd9Sstevel@tonic-gate } 196*7c478bd9Sstevel@tonic-gate 197*7c478bd9Sstevel@tonic-gate /* 198*7c478bd9Sstevel@tonic-gate * gfs_readdir_emit_int: internal routine to emit directory entry 199*7c478bd9Sstevel@tonic-gate * 200*7c478bd9Sstevel@tonic-gate * st - the current readdir state, which must have d_ino and d_name 201*7c478bd9Sstevel@tonic-gate * set 202*7c478bd9Sstevel@tonic-gate * uiop - caller-supplied uio pointer 203*7c478bd9Sstevel@tonic-gate * off - the offset of the current entry 204*7c478bd9Sstevel@tonic-gate * next - the offset of the next entry 205*7c478bd9Sstevel@tonic-gate */ 206*7c478bd9Sstevel@tonic-gate static int 207*7c478bd9Sstevel@tonic-gate gfs_readdir_emit_int(gfs_readdir_state_t *st, uio_t *uiop, offset_t off, 208*7c478bd9Sstevel@tonic-gate offset_t next) 209*7c478bd9Sstevel@tonic-gate { 210*7c478bd9Sstevel@tonic-gate int reclen; 211*7c478bd9Sstevel@tonic-gate 212*7c478bd9Sstevel@tonic-gate reclen = DIRENT64_RECLEN(strlen(st->grd_dirent->d_name)); 213*7c478bd9Sstevel@tonic-gate 214*7c478bd9Sstevel@tonic-gate if (reclen > uiop->uio_resid) { 215*7c478bd9Sstevel@tonic-gate /* 216*7c478bd9Sstevel@tonic-gate * Error if no entries were returned yet 217*7c478bd9Sstevel@tonic-gate */ 218*7c478bd9Sstevel@tonic-gate if (uiop->uio_resid == st->grd_oresid) 219*7c478bd9Sstevel@tonic-gate return (EINVAL); 220*7c478bd9Sstevel@tonic-gate return (-1); 221*7c478bd9Sstevel@tonic-gate } 222*7c478bd9Sstevel@tonic-gate 223*7c478bd9Sstevel@tonic-gate st->grd_dirent->d_off = off; 224*7c478bd9Sstevel@tonic-gate st->grd_dirent->d_reclen = (ushort_t)reclen; 225*7c478bd9Sstevel@tonic-gate 226*7c478bd9Sstevel@tonic-gate if (uiomove((caddr_t)st->grd_dirent, reclen, UIO_READ, uiop)) 227*7c478bd9Sstevel@tonic-gate return (EFAULT); 228*7c478bd9Sstevel@tonic-gate 229*7c478bd9Sstevel@tonic-gate uiop->uio_loffset = next; 230*7c478bd9Sstevel@tonic-gate 231*7c478bd9Sstevel@tonic-gate return (0); 232*7c478bd9Sstevel@tonic-gate } 233*7c478bd9Sstevel@tonic-gate 234*7c478bd9Sstevel@tonic-gate /* 235*7c478bd9Sstevel@tonic-gate * gfs_readdir_emit: emit a directory entry 236*7c478bd9Sstevel@tonic-gate * voff - the virtual offset (obtained from gfs_readdir_pred) 237*7c478bd9Sstevel@tonic-gate * ino - the entry's inode 238*7c478bd9Sstevel@tonic-gate * name - the entry's name 239*7c478bd9Sstevel@tonic-gate * 240*7c478bd9Sstevel@tonic-gate * Returns a 0 on success, a non-zero errno on failure, or -1 if the 241*7c478bd9Sstevel@tonic-gate * readdir loop should terminate. A non-zero result (either errno or 242*7c478bd9Sstevel@tonic-gate * -1) from this function is typically passed directly to 243*7c478bd9Sstevel@tonic-gate * gfs_readdir_fini(). 244*7c478bd9Sstevel@tonic-gate */ 245*7c478bd9Sstevel@tonic-gate int 246*7c478bd9Sstevel@tonic-gate gfs_readdir_emit(gfs_readdir_state_t *st, uio_t *uiop, offset_t voff, 247*7c478bd9Sstevel@tonic-gate ino64_t ino, const char *name) 248*7c478bd9Sstevel@tonic-gate { 249*7c478bd9Sstevel@tonic-gate offset_t off = (voff + 2) * st->grd_ureclen; 250*7c478bd9Sstevel@tonic-gate 251*7c478bd9Sstevel@tonic-gate st->grd_dirent->d_ino = ino; 252*7c478bd9Sstevel@tonic-gate (void) strncpy(st->grd_dirent->d_name, name, st->grd_namlen); 253*7c478bd9Sstevel@tonic-gate 254*7c478bd9Sstevel@tonic-gate /* 255*7c478bd9Sstevel@tonic-gate * Inter-entry offsets are invalid, so we assume a record size of 256*7c478bd9Sstevel@tonic-gate * grd_ureclen and explicitly set the offset appropriately. 257*7c478bd9Sstevel@tonic-gate */ 258*7c478bd9Sstevel@tonic-gate return (gfs_readdir_emit_int(st, uiop, off, off + st->grd_ureclen)); 259*7c478bd9Sstevel@tonic-gate } 260*7c478bd9Sstevel@tonic-gate 261*7c478bd9Sstevel@tonic-gate /* 262*7c478bd9Sstevel@tonic-gate * gfs_readdir_emitn: like gfs_readdir_emit(), but takes an integer 263*7c478bd9Sstevel@tonic-gate * instead of a string for the entry's name. 264*7c478bd9Sstevel@tonic-gate */ 265*7c478bd9Sstevel@tonic-gate int 266*7c478bd9Sstevel@tonic-gate gfs_readdir_emitn(gfs_readdir_state_t *st, uio_t *uiop, offset_t voff, 267*7c478bd9Sstevel@tonic-gate ino64_t ino, unsigned long num) 268*7c478bd9Sstevel@tonic-gate { 269*7c478bd9Sstevel@tonic-gate char buf[40]; 270*7c478bd9Sstevel@tonic-gate 271*7c478bd9Sstevel@tonic-gate numtos(num, buf); 272*7c478bd9Sstevel@tonic-gate return (gfs_readdir_emit(st, uiop, voff, ino, buf)); 273*7c478bd9Sstevel@tonic-gate } 274*7c478bd9Sstevel@tonic-gate 275*7c478bd9Sstevel@tonic-gate /* 276*7c478bd9Sstevel@tonic-gate * gfs_readdir_pred: readdir loop predicate 277*7c478bd9Sstevel@tonic-gate * voffp - a pointer in which the next virtual offset should be stored 278*7c478bd9Sstevel@tonic-gate * 279*7c478bd9Sstevel@tonic-gate * Returns a 0 on success, a non-zero errno on failure, or -1 if the 280*7c478bd9Sstevel@tonic-gate * readdir loop should terminate. A non-zero result (either errno or 281*7c478bd9Sstevel@tonic-gate * -1) from this function is typically passed directly to 282*7c478bd9Sstevel@tonic-gate * gfs_readdir_fini(). 283*7c478bd9Sstevel@tonic-gate */ 284*7c478bd9Sstevel@tonic-gate int 285*7c478bd9Sstevel@tonic-gate gfs_readdir_pred(gfs_readdir_state_t *st, uio_t *uiop, offset_t *voffp) 286*7c478bd9Sstevel@tonic-gate { 287*7c478bd9Sstevel@tonic-gate offset_t off, voff; 288*7c478bd9Sstevel@tonic-gate int error; 289*7c478bd9Sstevel@tonic-gate 290*7c478bd9Sstevel@tonic-gate top: 291*7c478bd9Sstevel@tonic-gate if (uiop->uio_resid <= 0) 292*7c478bd9Sstevel@tonic-gate return (-1); 293*7c478bd9Sstevel@tonic-gate 294*7c478bd9Sstevel@tonic-gate off = uiop->uio_loffset / st->grd_ureclen; 295*7c478bd9Sstevel@tonic-gate voff = off - 2; 296*7c478bd9Sstevel@tonic-gate if (off == 0) { 297*7c478bd9Sstevel@tonic-gate if ((error = gfs_readdir_emit(st, uiop, voff, st->grd_self, 298*7c478bd9Sstevel@tonic-gate ".")) == 0) 299*7c478bd9Sstevel@tonic-gate goto top; 300*7c478bd9Sstevel@tonic-gate } else if (off == 1) { 301*7c478bd9Sstevel@tonic-gate if ((error = gfs_readdir_emit(st, uiop, voff, st->grd_parent, 302*7c478bd9Sstevel@tonic-gate "..")) == 0) 303*7c478bd9Sstevel@tonic-gate goto top; 304*7c478bd9Sstevel@tonic-gate } else { 305*7c478bd9Sstevel@tonic-gate *voffp = voff; 306*7c478bd9Sstevel@tonic-gate return (0); 307*7c478bd9Sstevel@tonic-gate } 308*7c478bd9Sstevel@tonic-gate 309*7c478bd9Sstevel@tonic-gate return (error); 310*7c478bd9Sstevel@tonic-gate } 311*7c478bd9Sstevel@tonic-gate 312*7c478bd9Sstevel@tonic-gate /* 313*7c478bd9Sstevel@tonic-gate * gfs_readdir_fini: generic readdir cleanup 314*7c478bd9Sstevel@tonic-gate * error - if positive, an error to return 315*7c478bd9Sstevel@tonic-gate * eofp - the eofp passed to readdir 316*7c478bd9Sstevel@tonic-gate * eof - the eof value 317*7c478bd9Sstevel@tonic-gate * 318*7c478bd9Sstevel@tonic-gate * Returns a 0 on success, a non-zero errno on failure. This result 319*7c478bd9Sstevel@tonic-gate * should be returned from readdir. 320*7c478bd9Sstevel@tonic-gate */ 321*7c478bd9Sstevel@tonic-gate int 322*7c478bd9Sstevel@tonic-gate gfs_readdir_fini(gfs_readdir_state_t *st, int error, int *eofp, int eof) 323*7c478bd9Sstevel@tonic-gate { 324*7c478bd9Sstevel@tonic-gate kmem_free(st->grd_dirent, DIRENT64_RECLEN(st->grd_namlen)); 325*7c478bd9Sstevel@tonic-gate if (error > 0) 326*7c478bd9Sstevel@tonic-gate return (error); 327*7c478bd9Sstevel@tonic-gate if (eofp) 328*7c478bd9Sstevel@tonic-gate *eofp = eof; 329*7c478bd9Sstevel@tonic-gate return (0); 330*7c478bd9Sstevel@tonic-gate } 331*7c478bd9Sstevel@tonic-gate 332*7c478bd9Sstevel@tonic-gate /* 333*7c478bd9Sstevel@tonic-gate * gfs_lookup_dot 334*7c478bd9Sstevel@tonic-gate * 335*7c478bd9Sstevel@tonic-gate * Performs a basic check for "." and ".." directory entries. 336*7c478bd9Sstevel@tonic-gate */ 337*7c478bd9Sstevel@tonic-gate int 338*7c478bd9Sstevel@tonic-gate gfs_lookup_dot(vnode_t **vpp, vnode_t *dvp, vnode_t *pvp, const char *nm) 339*7c478bd9Sstevel@tonic-gate { 340*7c478bd9Sstevel@tonic-gate if (*nm == '\0' || strcmp(nm, ".") == 0) { 341*7c478bd9Sstevel@tonic-gate VN_HOLD(dvp); 342*7c478bd9Sstevel@tonic-gate *vpp = dvp; 343*7c478bd9Sstevel@tonic-gate return (0); 344*7c478bd9Sstevel@tonic-gate } else if (strcmp(nm, "..") == 0) { 345*7c478bd9Sstevel@tonic-gate if (pvp == NULL) { 346*7c478bd9Sstevel@tonic-gate ASSERT(dvp->v_flag & VROOT); 347*7c478bd9Sstevel@tonic-gate VN_HOLD(dvp); 348*7c478bd9Sstevel@tonic-gate *vpp = dvp; 349*7c478bd9Sstevel@tonic-gate } else { 350*7c478bd9Sstevel@tonic-gate VN_HOLD(pvp); 351*7c478bd9Sstevel@tonic-gate *vpp = pvp; 352*7c478bd9Sstevel@tonic-gate } 353*7c478bd9Sstevel@tonic-gate return (0); 354*7c478bd9Sstevel@tonic-gate } 355*7c478bd9Sstevel@tonic-gate 356*7c478bd9Sstevel@tonic-gate return (-1); 357*7c478bd9Sstevel@tonic-gate } 358*7c478bd9Sstevel@tonic-gate 359*7c478bd9Sstevel@tonic-gate /* 360*7c478bd9Sstevel@tonic-gate * gfs_file_create(): create a new GFS file 361*7c478bd9Sstevel@tonic-gate * 362*7c478bd9Sstevel@tonic-gate * size - size of private data structure (v_data) 363*7c478bd9Sstevel@tonic-gate * pvp - parent vnode (GFS directory) 364*7c478bd9Sstevel@tonic-gate * ops - vnode operations vector 365*7c478bd9Sstevel@tonic-gate * 366*7c478bd9Sstevel@tonic-gate * In order to use this interface, the parent vnode must have been created by 367*7c478bd9Sstevel@tonic-gate * gfs_dir_create(), and the private data stored in v_data must have a 368*7c478bd9Sstevel@tonic-gate * 'gfs_file_t' as its first field. 369*7c478bd9Sstevel@tonic-gate * 370*7c478bd9Sstevel@tonic-gate * Given these constraints, this routine will automatically: 371*7c478bd9Sstevel@tonic-gate * 372*7c478bd9Sstevel@tonic-gate * - Allocate v_data for the vnode 373*7c478bd9Sstevel@tonic-gate * - Initialize necessary fields in the vnode 374*7c478bd9Sstevel@tonic-gate * - Hold the parent 375*7c478bd9Sstevel@tonic-gate */ 376*7c478bd9Sstevel@tonic-gate vnode_t * 377*7c478bd9Sstevel@tonic-gate gfs_file_create(size_t size, vnode_t *pvp, vnodeops_t *ops) 378*7c478bd9Sstevel@tonic-gate { 379*7c478bd9Sstevel@tonic-gate gfs_file_t *fp; 380*7c478bd9Sstevel@tonic-gate vnode_t *vp; 381*7c478bd9Sstevel@tonic-gate 382*7c478bd9Sstevel@tonic-gate /* 383*7c478bd9Sstevel@tonic-gate * Allocate vnode and internal data structure 384*7c478bd9Sstevel@tonic-gate */ 385*7c478bd9Sstevel@tonic-gate fp = kmem_zalloc(size, KM_SLEEP); 386*7c478bd9Sstevel@tonic-gate vp = vn_alloc(KM_SLEEP); 387*7c478bd9Sstevel@tonic-gate 388*7c478bd9Sstevel@tonic-gate /* 389*7c478bd9Sstevel@tonic-gate * Set up various pointers 390*7c478bd9Sstevel@tonic-gate */ 391*7c478bd9Sstevel@tonic-gate fp->gfs_vnode = vp; 392*7c478bd9Sstevel@tonic-gate fp->gfs_parent = pvp; 393*7c478bd9Sstevel@tonic-gate vp->v_data = fp; 394*7c478bd9Sstevel@tonic-gate fp->gfs_size = size; 395*7c478bd9Sstevel@tonic-gate fp->gfs_type = GFS_FILE; 396*7c478bd9Sstevel@tonic-gate 397*7c478bd9Sstevel@tonic-gate /* 398*7c478bd9Sstevel@tonic-gate * Initialize vnode and hold parent. 399*7c478bd9Sstevel@tonic-gate */ 400*7c478bd9Sstevel@tonic-gate vn_setops(vp, ops); 401*7c478bd9Sstevel@tonic-gate if (pvp) { 402*7c478bd9Sstevel@tonic-gate VN_SET_VFS_TYPE_DEV(vp, pvp->v_vfsp, VREG, 0); 403*7c478bd9Sstevel@tonic-gate VN_HOLD(pvp); 404*7c478bd9Sstevel@tonic-gate } 405*7c478bd9Sstevel@tonic-gate 406*7c478bd9Sstevel@tonic-gate return (vp); 407*7c478bd9Sstevel@tonic-gate } 408*7c478bd9Sstevel@tonic-gate 409*7c478bd9Sstevel@tonic-gate /* 410*7c478bd9Sstevel@tonic-gate * gfs_dir_create: creates a new directory in the parent 411*7c478bd9Sstevel@tonic-gate * 412*7c478bd9Sstevel@tonic-gate * size - size of private data structure (v_data) 413*7c478bd9Sstevel@tonic-gate * pvp - parent vnode (GFS directory) 414*7c478bd9Sstevel@tonic-gate * ops - vnode operations vector 415*7c478bd9Sstevel@tonic-gate * entries - NULL-terminated list of static entries (if any) 416*7c478bd9Sstevel@tonic-gate * maxlen - maximum length of a directory entry 417*7c478bd9Sstevel@tonic-gate * readdir_cb - readdir callback (see gfs_dir_readdir) 418*7c478bd9Sstevel@tonic-gate * inode_cb - inode callback (see gfs_dir_readdir) 419*7c478bd9Sstevel@tonic-gate * lookup_cb - lookup callback (see gfs_dir_lookup) 420*7c478bd9Sstevel@tonic-gate * 421*7c478bd9Sstevel@tonic-gate * In order to use this function, the first member of the private vnode 422*7c478bd9Sstevel@tonic-gate * structure (v_data) must be a gfs_dir_t. For each directory, there are 423*7c478bd9Sstevel@tonic-gate * static entries, defined when the structure is initialized, and dynamic 424*7c478bd9Sstevel@tonic-gate * entries, retrieved through callbacks. 425*7c478bd9Sstevel@tonic-gate * 426*7c478bd9Sstevel@tonic-gate * If a directory has static entries, then it must supply a inode callback, 427*7c478bd9Sstevel@tonic-gate * which will compute the inode number based on the parent and the index. 428*7c478bd9Sstevel@tonic-gate * For a directory with dynamic entries, the caller must supply a readdir 429*7c478bd9Sstevel@tonic-gate * callback and a lookup callback. If a static lookup fails, we fall back to 430*7c478bd9Sstevel@tonic-gate * the supplied lookup callback, if any. 431*7c478bd9Sstevel@tonic-gate * 432*7c478bd9Sstevel@tonic-gate * This function also performs the same initialization as gfs_file_create(). 433*7c478bd9Sstevel@tonic-gate */ 434*7c478bd9Sstevel@tonic-gate vnode_t * 435*7c478bd9Sstevel@tonic-gate gfs_dir_create(size_t struct_size, vnode_t *pvp, vnodeops_t *ops, 436*7c478bd9Sstevel@tonic-gate gfs_dirent_t *entries, gfs_inode_cb inode_cb, int maxlen, 437*7c478bd9Sstevel@tonic-gate gfs_readdir_cb readdir_cb, gfs_lookup_cb lookup_cb) 438*7c478bd9Sstevel@tonic-gate { 439*7c478bd9Sstevel@tonic-gate vnode_t *vp; 440*7c478bd9Sstevel@tonic-gate gfs_dir_t *dp; 441*7c478bd9Sstevel@tonic-gate gfs_dirent_t *de; 442*7c478bd9Sstevel@tonic-gate 443*7c478bd9Sstevel@tonic-gate vp = gfs_file_create(struct_size, pvp, ops); 444*7c478bd9Sstevel@tonic-gate vp->v_type = VDIR; 445*7c478bd9Sstevel@tonic-gate 446*7c478bd9Sstevel@tonic-gate dp = vp->v_data; 447*7c478bd9Sstevel@tonic-gate dp->gfsd_file.gfs_type = GFS_DIR; 448*7c478bd9Sstevel@tonic-gate dp->gfsd_maxlen = maxlen; 449*7c478bd9Sstevel@tonic-gate 450*7c478bd9Sstevel@tonic-gate if (entries != NULL) { 451*7c478bd9Sstevel@tonic-gate for (de = entries; de->gfse_name != NULL; de++) 452*7c478bd9Sstevel@tonic-gate dp->gfsd_nstatic++; 453*7c478bd9Sstevel@tonic-gate 454*7c478bd9Sstevel@tonic-gate dp->gfsd_static = kmem_alloc( 455*7c478bd9Sstevel@tonic-gate dp->gfsd_nstatic * sizeof (gfs_dirent_t), KM_SLEEP); 456*7c478bd9Sstevel@tonic-gate bcopy(entries, dp->gfsd_static, 457*7c478bd9Sstevel@tonic-gate dp->gfsd_nstatic * sizeof (gfs_dirent_t)); 458*7c478bd9Sstevel@tonic-gate } 459*7c478bd9Sstevel@tonic-gate 460*7c478bd9Sstevel@tonic-gate dp->gfsd_readdir = readdir_cb; 461*7c478bd9Sstevel@tonic-gate dp->gfsd_lookup = lookup_cb; 462*7c478bd9Sstevel@tonic-gate dp->gfsd_inode = inode_cb; 463*7c478bd9Sstevel@tonic-gate 464*7c478bd9Sstevel@tonic-gate mutex_init(&dp->gfsd_lock, NULL, MUTEX_DEFAULT, NULL); 465*7c478bd9Sstevel@tonic-gate 466*7c478bd9Sstevel@tonic-gate return (vp); 467*7c478bd9Sstevel@tonic-gate } 468*7c478bd9Sstevel@tonic-gate 469*7c478bd9Sstevel@tonic-gate /* 470*7c478bd9Sstevel@tonic-gate * gfs_root_create(): create a root vnode for a GFS filesystem 471*7c478bd9Sstevel@tonic-gate * 472*7c478bd9Sstevel@tonic-gate * Similar to gfs_dir_create(), this creates a root vnode for a filesystem. The 473*7c478bd9Sstevel@tonic-gate * only difference is that it takes a vfs_t instead of a vnode_t as its parent. 474*7c478bd9Sstevel@tonic-gate */ 475*7c478bd9Sstevel@tonic-gate vnode_t * 476*7c478bd9Sstevel@tonic-gate gfs_root_create(size_t size, vfs_t *vfsp, vnodeops_t *ops, ino64_t ino, 477*7c478bd9Sstevel@tonic-gate gfs_dirent_t *entries, gfs_inode_cb inode_cb, int maxlen, 478*7c478bd9Sstevel@tonic-gate gfs_readdir_cb readdir_cb, gfs_lookup_cb lookup_cb) 479*7c478bd9Sstevel@tonic-gate { 480*7c478bd9Sstevel@tonic-gate vnode_t *vp = gfs_dir_create(size, NULL, ops, entries, inode_cb, 481*7c478bd9Sstevel@tonic-gate maxlen, readdir_cb, lookup_cb); 482*7c478bd9Sstevel@tonic-gate 483*7c478bd9Sstevel@tonic-gate /* Manually set the inode */ 484*7c478bd9Sstevel@tonic-gate ((gfs_file_t *)vp->v_data)->gfs_ino = ino; 485*7c478bd9Sstevel@tonic-gate 486*7c478bd9Sstevel@tonic-gate VFS_HOLD(vfsp); 487*7c478bd9Sstevel@tonic-gate VN_SET_VFS_TYPE_DEV(vp, vfsp, VDIR, 0); 488*7c478bd9Sstevel@tonic-gate vp->v_flag |= VROOT | VNOCACHE | VNOMAP | VNOSWAP | VNOMOUNT; 489*7c478bd9Sstevel@tonic-gate 490*7c478bd9Sstevel@tonic-gate return (vp); 491*7c478bd9Sstevel@tonic-gate } 492*7c478bd9Sstevel@tonic-gate 493*7c478bd9Sstevel@tonic-gate /* 494*7c478bd9Sstevel@tonic-gate * gfs_file_inactive() 495*7c478bd9Sstevel@tonic-gate * 496*7c478bd9Sstevel@tonic-gate * Called from the VOP_INACTIVE() routine. If necessary, this routine will 497*7c478bd9Sstevel@tonic-gate * remove the given vnode from the parent directory and clean up any references 498*7c478bd9Sstevel@tonic-gate * in the VFS layer. 499*7c478bd9Sstevel@tonic-gate * 500*7c478bd9Sstevel@tonic-gate * If the vnode was not removed (due to a race with vget), then NULL is 501*7c478bd9Sstevel@tonic-gate * returned. Otherwise, a pointer to the private data is returned. 502*7c478bd9Sstevel@tonic-gate */ 503*7c478bd9Sstevel@tonic-gate void * 504*7c478bd9Sstevel@tonic-gate gfs_file_inactive(vnode_t *vp) 505*7c478bd9Sstevel@tonic-gate { 506*7c478bd9Sstevel@tonic-gate int i; 507*7c478bd9Sstevel@tonic-gate gfs_dirent_t *ge = NULL; 508*7c478bd9Sstevel@tonic-gate gfs_file_t *fp = vp->v_data; 509*7c478bd9Sstevel@tonic-gate gfs_dir_t *dp = NULL; 510*7c478bd9Sstevel@tonic-gate void *data; 511*7c478bd9Sstevel@tonic-gate 512*7c478bd9Sstevel@tonic-gate if (fp->gfs_parent == NULL) 513*7c478bd9Sstevel@tonic-gate goto found; 514*7c478bd9Sstevel@tonic-gate 515*7c478bd9Sstevel@tonic-gate dp = fp->gfs_parent->v_data; 516*7c478bd9Sstevel@tonic-gate 517*7c478bd9Sstevel@tonic-gate /* 518*7c478bd9Sstevel@tonic-gate * First, see if this vnode is cached in the parent. 519*7c478bd9Sstevel@tonic-gate */ 520*7c478bd9Sstevel@tonic-gate gfs_dir_lock(dp); 521*7c478bd9Sstevel@tonic-gate 522*7c478bd9Sstevel@tonic-gate /* 523*7c478bd9Sstevel@tonic-gate * Find it in the set of static entries. 524*7c478bd9Sstevel@tonic-gate */ 525*7c478bd9Sstevel@tonic-gate for (i = 0; i < dp->gfsd_nstatic; i++) { 526*7c478bd9Sstevel@tonic-gate ge = &dp->gfsd_static[i]; 527*7c478bd9Sstevel@tonic-gate 528*7c478bd9Sstevel@tonic-gate if (ge->gfse_vnode == vp) 529*7c478bd9Sstevel@tonic-gate goto found; 530*7c478bd9Sstevel@tonic-gate } 531*7c478bd9Sstevel@tonic-gate 532*7c478bd9Sstevel@tonic-gate /* 533*7c478bd9Sstevel@tonic-gate * If 'ge' is NULL, then it is a dynamic entry. 534*7c478bd9Sstevel@tonic-gate */ 535*7c478bd9Sstevel@tonic-gate ge = NULL; 536*7c478bd9Sstevel@tonic-gate 537*7c478bd9Sstevel@tonic-gate found: 538*7c478bd9Sstevel@tonic-gate mutex_enter(&vp->v_lock); 539*7c478bd9Sstevel@tonic-gate if (vp->v_count == 1) { 540*7c478bd9Sstevel@tonic-gate /* 541*7c478bd9Sstevel@tonic-gate * Really remove this vnode 542*7c478bd9Sstevel@tonic-gate */ 543*7c478bd9Sstevel@tonic-gate data = vp->v_data; 544*7c478bd9Sstevel@tonic-gate if (ge != NULL) { 545*7c478bd9Sstevel@tonic-gate /* 546*7c478bd9Sstevel@tonic-gate * If this was a statically cached entry, simply set the 547*7c478bd9Sstevel@tonic-gate * cached vnode to NULL. 548*7c478bd9Sstevel@tonic-gate */ 549*7c478bd9Sstevel@tonic-gate ge->gfse_vnode = NULL; 550*7c478bd9Sstevel@tonic-gate } 551*7c478bd9Sstevel@tonic-gate mutex_exit(&vp->v_lock); 552*7c478bd9Sstevel@tonic-gate 553*7c478bd9Sstevel@tonic-gate /* 554*7c478bd9Sstevel@tonic-gate * Free vnode and release parent 555*7c478bd9Sstevel@tonic-gate */ 556*7c478bd9Sstevel@tonic-gate if (fp->gfs_parent) { 557*7c478bd9Sstevel@tonic-gate gfs_dir_unlock(dp); 558*7c478bd9Sstevel@tonic-gate VN_RELE(fp->gfs_parent); 559*7c478bd9Sstevel@tonic-gate } else { 560*7c478bd9Sstevel@tonic-gate ASSERT(vp->v_vfsp != NULL); 561*7c478bd9Sstevel@tonic-gate VFS_RELE(vp->v_vfsp); 562*7c478bd9Sstevel@tonic-gate } 563*7c478bd9Sstevel@tonic-gate vn_free(vp); 564*7c478bd9Sstevel@tonic-gate } else { 565*7c478bd9Sstevel@tonic-gate vp->v_count--; 566*7c478bd9Sstevel@tonic-gate data = NULL; 567*7c478bd9Sstevel@tonic-gate mutex_exit(&vp->v_lock); 568*7c478bd9Sstevel@tonic-gate if (dp) 569*7c478bd9Sstevel@tonic-gate gfs_dir_unlock(dp); 570*7c478bd9Sstevel@tonic-gate } 571*7c478bd9Sstevel@tonic-gate 572*7c478bd9Sstevel@tonic-gate return (data); 573*7c478bd9Sstevel@tonic-gate } 574*7c478bd9Sstevel@tonic-gate 575*7c478bd9Sstevel@tonic-gate /* 576*7c478bd9Sstevel@tonic-gate * gfs_dir_inactive() 577*7c478bd9Sstevel@tonic-gate * 578*7c478bd9Sstevel@tonic-gate * Same as above, but for directories. 579*7c478bd9Sstevel@tonic-gate */ 580*7c478bd9Sstevel@tonic-gate void * 581*7c478bd9Sstevel@tonic-gate gfs_dir_inactive(vnode_t *vp) 582*7c478bd9Sstevel@tonic-gate { 583*7c478bd9Sstevel@tonic-gate gfs_dir_t *dp; 584*7c478bd9Sstevel@tonic-gate 585*7c478bd9Sstevel@tonic-gate ASSERT(vp->v_type == VDIR); 586*7c478bd9Sstevel@tonic-gate 587*7c478bd9Sstevel@tonic-gate if ((dp = gfs_file_inactive(vp)) != NULL) { 588*7c478bd9Sstevel@tonic-gate mutex_destroy(&dp->gfsd_lock); 589*7c478bd9Sstevel@tonic-gate if (dp->gfsd_nstatic) 590*7c478bd9Sstevel@tonic-gate kmem_free(dp->gfsd_static, 591*7c478bd9Sstevel@tonic-gate dp->gfsd_nstatic * sizeof (gfs_dirent_t)); 592*7c478bd9Sstevel@tonic-gate } 593*7c478bd9Sstevel@tonic-gate 594*7c478bd9Sstevel@tonic-gate return (dp); 595*7c478bd9Sstevel@tonic-gate } 596*7c478bd9Sstevel@tonic-gate 597*7c478bd9Sstevel@tonic-gate /* 598*7c478bd9Sstevel@tonic-gate * gfs_dir_lookup() 599*7c478bd9Sstevel@tonic-gate * 600*7c478bd9Sstevel@tonic-gate * Looks up the given name in the directory and returns the corresponding vnode, 601*7c478bd9Sstevel@tonic-gate * if found. 602*7c478bd9Sstevel@tonic-gate * 603*7c478bd9Sstevel@tonic-gate * First, we search statically defined entries, if any. If a match is found, 604*7c478bd9Sstevel@tonic-gate * and GFS_CACHE_VNODE is set and the vnode exists, we simply return the 605*7c478bd9Sstevel@tonic-gate * existing vnode. Otherwise, we call the static entry's callback routine, 606*7c478bd9Sstevel@tonic-gate * caching the result if necessary. 607*7c478bd9Sstevel@tonic-gate * 608*7c478bd9Sstevel@tonic-gate * If no static entry is found, we invoke the lookup callback, if any. The 609*7c478bd9Sstevel@tonic-gate * arguments to this callback are: 610*7c478bd9Sstevel@tonic-gate * 611*7c478bd9Sstevel@tonic-gate * int gfs_lookup_cb(vnode_t *pvp, const char *nm, vnode_t **vpp); 612*7c478bd9Sstevel@tonic-gate * 613*7c478bd9Sstevel@tonic-gate * pvp - parent vnode 614*7c478bd9Sstevel@tonic-gate * nm - name of entry 615*7c478bd9Sstevel@tonic-gate * vpp - pointer to resulting vnode 616*7c478bd9Sstevel@tonic-gate * 617*7c478bd9Sstevel@tonic-gate * Returns 0 on success, non-zero on error. 618*7c478bd9Sstevel@tonic-gate */ 619*7c478bd9Sstevel@tonic-gate int 620*7c478bd9Sstevel@tonic-gate gfs_dir_lookup(vnode_t *dvp, const char *nm, vnode_t **vpp) 621*7c478bd9Sstevel@tonic-gate { 622*7c478bd9Sstevel@tonic-gate int i; 623*7c478bd9Sstevel@tonic-gate gfs_dirent_t *ge; 624*7c478bd9Sstevel@tonic-gate vnode_t *vp; 625*7c478bd9Sstevel@tonic-gate gfs_dir_t *dp = dvp->v_data; 626*7c478bd9Sstevel@tonic-gate int ret = 0; 627*7c478bd9Sstevel@tonic-gate 628*7c478bd9Sstevel@tonic-gate ASSERT(dvp->v_type == VDIR); 629*7c478bd9Sstevel@tonic-gate 630*7c478bd9Sstevel@tonic-gate if (gfs_lookup_dot(vpp, dvp, dp->gfsd_file.gfs_parent, nm) == 0) 631*7c478bd9Sstevel@tonic-gate return (0); 632*7c478bd9Sstevel@tonic-gate 633*7c478bd9Sstevel@tonic-gate gfs_dir_lock(dp); 634*7c478bd9Sstevel@tonic-gate 635*7c478bd9Sstevel@tonic-gate /* 636*7c478bd9Sstevel@tonic-gate * Search static entries. 637*7c478bd9Sstevel@tonic-gate */ 638*7c478bd9Sstevel@tonic-gate for (i = 0; i < dp->gfsd_nstatic; i++) { 639*7c478bd9Sstevel@tonic-gate ge = &dp->gfsd_static[i]; 640*7c478bd9Sstevel@tonic-gate 641*7c478bd9Sstevel@tonic-gate if (strcmp(ge->gfse_name, nm) == 0) { 642*7c478bd9Sstevel@tonic-gate if (ge->gfse_vnode) { 643*7c478bd9Sstevel@tonic-gate ASSERT(ge->gfse_flags & GFS_CACHE_VNODE); 644*7c478bd9Sstevel@tonic-gate vp = ge->gfse_vnode; 645*7c478bd9Sstevel@tonic-gate VN_HOLD(vp); 646*7c478bd9Sstevel@tonic-gate goto out; 647*7c478bd9Sstevel@tonic-gate } 648*7c478bd9Sstevel@tonic-gate 649*7c478bd9Sstevel@tonic-gate /* 650*7c478bd9Sstevel@tonic-gate * We drop the directory lock, as the constuctor will 651*7c478bd9Sstevel@tonic-gate * need to do KM_SLEEP allocations. If we return from 652*7c478bd9Sstevel@tonic-gate * the constructor only to find that a parallel 653*7c478bd9Sstevel@tonic-gate * operation has completed, and GFS_CACHE_VNODE is set 654*7c478bd9Sstevel@tonic-gate * for this entry, we discard the result in favor of the 655*7c478bd9Sstevel@tonic-gate * cached vnode. 656*7c478bd9Sstevel@tonic-gate */ 657*7c478bd9Sstevel@tonic-gate gfs_dir_unlock(dp); 658*7c478bd9Sstevel@tonic-gate vp = ge->gfse_ctor(dvp); 659*7c478bd9Sstevel@tonic-gate gfs_dir_lock(dp); 660*7c478bd9Sstevel@tonic-gate 661*7c478bd9Sstevel@tonic-gate ((gfs_file_t *)vp->v_data)->gfs_index = i; 662*7c478bd9Sstevel@tonic-gate 663*7c478bd9Sstevel@tonic-gate /* Set the inode according to the callback. */ 664*7c478bd9Sstevel@tonic-gate ((gfs_file_t *)vp->v_data)->gfs_ino = 665*7c478bd9Sstevel@tonic-gate dp->gfsd_inode(dvp, i); 666*7c478bd9Sstevel@tonic-gate 667*7c478bd9Sstevel@tonic-gate if (ge->gfse_flags & GFS_CACHE_VNODE) { 668*7c478bd9Sstevel@tonic-gate if (ge->gfse_vnode == NULL) { 669*7c478bd9Sstevel@tonic-gate ge->gfse_vnode = vp; 670*7c478bd9Sstevel@tonic-gate } else { 671*7c478bd9Sstevel@tonic-gate /* 672*7c478bd9Sstevel@tonic-gate * A parallel constructor beat us to it; 673*7c478bd9Sstevel@tonic-gate * return existing vnode. We have to be 674*7c478bd9Sstevel@tonic-gate * careful because we can't release the 675*7c478bd9Sstevel@tonic-gate * current vnode while holding the 676*7c478bd9Sstevel@tonic-gate * directory lock; its inactive routine 677*7c478bd9Sstevel@tonic-gate * will try to lock this directory. 678*7c478bd9Sstevel@tonic-gate */ 679*7c478bd9Sstevel@tonic-gate vnode_t *oldvp = vp; 680*7c478bd9Sstevel@tonic-gate vp = ge->gfse_vnode; 681*7c478bd9Sstevel@tonic-gate VN_HOLD(vp); 682*7c478bd9Sstevel@tonic-gate 683*7c478bd9Sstevel@tonic-gate gfs_dir_unlock(dp); 684*7c478bd9Sstevel@tonic-gate VN_RELE(oldvp); 685*7c478bd9Sstevel@tonic-gate gfs_dir_lock(dp); 686*7c478bd9Sstevel@tonic-gate } 687*7c478bd9Sstevel@tonic-gate } 688*7c478bd9Sstevel@tonic-gate 689*7c478bd9Sstevel@tonic-gate goto out; 690*7c478bd9Sstevel@tonic-gate } 691*7c478bd9Sstevel@tonic-gate } 692*7c478bd9Sstevel@tonic-gate 693*7c478bd9Sstevel@tonic-gate /* 694*7c478bd9Sstevel@tonic-gate * See if there is a dynamic constructor. 695*7c478bd9Sstevel@tonic-gate */ 696*7c478bd9Sstevel@tonic-gate if (dp->gfsd_lookup) { 697*7c478bd9Sstevel@tonic-gate ino64_t ino; 698*7c478bd9Sstevel@tonic-gate gfs_file_t *fp; 699*7c478bd9Sstevel@tonic-gate 700*7c478bd9Sstevel@tonic-gate /* 701*7c478bd9Sstevel@tonic-gate * Once again, drop the directory lock, as the lookup routine 702*7c478bd9Sstevel@tonic-gate * will need to allocate memory, or otherwise deadlock on this 703*7c478bd9Sstevel@tonic-gate * directory. 704*7c478bd9Sstevel@tonic-gate */ 705*7c478bd9Sstevel@tonic-gate gfs_dir_unlock(dp); 706*7c478bd9Sstevel@tonic-gate ret = dp->gfsd_lookup(dvp, nm, &vp, &ino); 707*7c478bd9Sstevel@tonic-gate gfs_dir_lock(dp); 708*7c478bd9Sstevel@tonic-gate if (ret != 0) 709*7c478bd9Sstevel@tonic-gate goto out; 710*7c478bd9Sstevel@tonic-gate 711*7c478bd9Sstevel@tonic-gate fp = (gfs_file_t *)vp->v_data; 712*7c478bd9Sstevel@tonic-gate fp->gfs_index = -1; 713*7c478bd9Sstevel@tonic-gate fp->gfs_ino = ino; 714*7c478bd9Sstevel@tonic-gate } else { 715*7c478bd9Sstevel@tonic-gate /* 716*7c478bd9Sstevel@tonic-gate * No static entry found, and there is no lookup callback, so 717*7c478bd9Sstevel@tonic-gate * return ENOENT. 718*7c478bd9Sstevel@tonic-gate */ 719*7c478bd9Sstevel@tonic-gate ret = ENOENT; 720*7c478bd9Sstevel@tonic-gate } 721*7c478bd9Sstevel@tonic-gate 722*7c478bd9Sstevel@tonic-gate out: 723*7c478bd9Sstevel@tonic-gate gfs_dir_unlock(dp); 724*7c478bd9Sstevel@tonic-gate 725*7c478bd9Sstevel@tonic-gate *vpp = vp; 726*7c478bd9Sstevel@tonic-gate 727*7c478bd9Sstevel@tonic-gate return (ret); 728*7c478bd9Sstevel@tonic-gate } 729*7c478bd9Sstevel@tonic-gate 730*7c478bd9Sstevel@tonic-gate /* 731*7c478bd9Sstevel@tonic-gate * gfs_dir_readdir: does a readdir() on the given directory 732*7c478bd9Sstevel@tonic-gate * 733*7c478bd9Sstevel@tonic-gate * dvp - directory vnode 734*7c478bd9Sstevel@tonic-gate * uiop - uio structure 735*7c478bd9Sstevel@tonic-gate * eofp - eof pointer 736*7c478bd9Sstevel@tonic-gate * data - arbitrary data passed to readdir callback 737*7c478bd9Sstevel@tonic-gate * 738*7c478bd9Sstevel@tonic-gate * This routine does all the readdir() dirty work. Even so, the caller must 739*7c478bd9Sstevel@tonic-gate * supply two callbacks in order to get full compatibility. 740*7c478bd9Sstevel@tonic-gate * 741*7c478bd9Sstevel@tonic-gate * If the directory contains static entries, an inode callback must be 742*7c478bd9Sstevel@tonic-gate * specified. This avoids having to create every vnode and call VOP_GETATTR() 743*7c478bd9Sstevel@tonic-gate * when reading the directory. This function has the following arguments: 744*7c478bd9Sstevel@tonic-gate * 745*7c478bd9Sstevel@tonic-gate * ino_t gfs_inode_cb(vnode_t *vp, int index); 746*7c478bd9Sstevel@tonic-gate * 747*7c478bd9Sstevel@tonic-gate * vp - vnode for the directory 748*7c478bd9Sstevel@tonic-gate * index - index in original gfs_dirent_t array 749*7c478bd9Sstevel@tonic-gate * 750*7c478bd9Sstevel@tonic-gate * Returns the inode number for the given entry. 751*7c478bd9Sstevel@tonic-gate * 752*7c478bd9Sstevel@tonic-gate * For directories with dynamic entries, a readdir callback must be provided. 753*7c478bd9Sstevel@tonic-gate * This is significantly more complex, thanks to the particulars of 754*7c478bd9Sstevel@tonic-gate * VOP_READDIR(). 755*7c478bd9Sstevel@tonic-gate * 756*7c478bd9Sstevel@tonic-gate * int gfs_readdir_cb(vnode_t *vp, struct dirent64 *dp, int *eofp, 757*7c478bd9Sstevel@tonic-gate * offset_t *off, offset_t *nextoff, void *data) 758*7c478bd9Sstevel@tonic-gate * 759*7c478bd9Sstevel@tonic-gate * vp - directory vnode 760*7c478bd9Sstevel@tonic-gate * dp - directory entry, sized according to maxlen given to 761*7c478bd9Sstevel@tonic-gate * gfs_dir_create(). callback must fill in d_name and 762*7c478bd9Sstevel@tonic-gate * d_ino. 763*7c478bd9Sstevel@tonic-gate * eofp - callback must set to 1 when EOF has been reached 764*7c478bd9Sstevel@tonic-gate * off - on entry, the last offset read from the directory. Callback 765*7c478bd9Sstevel@tonic-gate * must set to the offset of the current entry, typically left 766*7c478bd9Sstevel@tonic-gate * untouched. 767*7c478bd9Sstevel@tonic-gate * nextoff - callback must set to offset of next entry. Typically 768*7c478bd9Sstevel@tonic-gate * (off + 1) 769*7c478bd9Sstevel@tonic-gate * data - caller-supplied data 770*7c478bd9Sstevel@tonic-gate * 771*7c478bd9Sstevel@tonic-gate * Return 0 on success, or error on failure. 772*7c478bd9Sstevel@tonic-gate */ 773*7c478bd9Sstevel@tonic-gate int 774*7c478bd9Sstevel@tonic-gate gfs_dir_readdir(vnode_t *dvp, uio_t *uiop, int *eofp, void *data) 775*7c478bd9Sstevel@tonic-gate { 776*7c478bd9Sstevel@tonic-gate gfs_readdir_state_t gstate; 777*7c478bd9Sstevel@tonic-gate int error, eof = 0; 778*7c478bd9Sstevel@tonic-gate ino64_t ino, pino; 779*7c478bd9Sstevel@tonic-gate offset_t off, next; 780*7c478bd9Sstevel@tonic-gate gfs_dir_t *dp = dvp->v_data; 781*7c478bd9Sstevel@tonic-gate 782*7c478bd9Sstevel@tonic-gate ino = dp->gfsd_file.gfs_ino; 783*7c478bd9Sstevel@tonic-gate 784*7c478bd9Sstevel@tonic-gate if (dp->gfsd_file.gfs_parent == NULL) 785*7c478bd9Sstevel@tonic-gate pino = ino; /* root of filesystem */ 786*7c478bd9Sstevel@tonic-gate else 787*7c478bd9Sstevel@tonic-gate pino = ((gfs_file_t *) 788*7c478bd9Sstevel@tonic-gate (dp->gfsd_file.gfs_parent->v_data))->gfs_ino; 789*7c478bd9Sstevel@tonic-gate 790*7c478bd9Sstevel@tonic-gate if ((error = gfs_readdir_init(&gstate, dp->gfsd_maxlen, 1, uiop, 791*7c478bd9Sstevel@tonic-gate pino, ino)) != 0) 792*7c478bd9Sstevel@tonic-gate return (error); 793*7c478bd9Sstevel@tonic-gate 794*7c478bd9Sstevel@tonic-gate while ((error = gfs_readdir_pred(&gstate, uiop, &off)) == 0 && 795*7c478bd9Sstevel@tonic-gate !eof) { 796*7c478bd9Sstevel@tonic-gate 797*7c478bd9Sstevel@tonic-gate if (off >= 0 && off < dp->gfsd_nstatic) { 798*7c478bd9Sstevel@tonic-gate ino = dp->gfsd_inode(dvp, off); 799*7c478bd9Sstevel@tonic-gate 800*7c478bd9Sstevel@tonic-gate if ((error = gfs_readdir_emit(&gstate, uiop, 801*7c478bd9Sstevel@tonic-gate off, ino, dp->gfsd_static[off].gfse_name)) 802*7c478bd9Sstevel@tonic-gate != 0) 803*7c478bd9Sstevel@tonic-gate break; 804*7c478bd9Sstevel@tonic-gate 805*7c478bd9Sstevel@tonic-gate } else if (dp->gfsd_readdir) { 806*7c478bd9Sstevel@tonic-gate off -= dp->gfsd_nstatic; 807*7c478bd9Sstevel@tonic-gate 808*7c478bd9Sstevel@tonic-gate if ((error = dp->gfsd_readdir(dvp, 809*7c478bd9Sstevel@tonic-gate gstate.grd_dirent, &eof, &off, &next, 810*7c478bd9Sstevel@tonic-gate data)) != 0 || eof) 811*7c478bd9Sstevel@tonic-gate break; 812*7c478bd9Sstevel@tonic-gate 813*7c478bd9Sstevel@tonic-gate off += dp->gfsd_nstatic + 2; 814*7c478bd9Sstevel@tonic-gate next += dp->gfsd_nstatic + 2; 815*7c478bd9Sstevel@tonic-gate 816*7c478bd9Sstevel@tonic-gate if ((error = gfs_readdir_emit_int(&gstate, uiop, 817*7c478bd9Sstevel@tonic-gate off, next)) != 0) 818*7c478bd9Sstevel@tonic-gate break; 819*7c478bd9Sstevel@tonic-gate } else { 820*7c478bd9Sstevel@tonic-gate /* 821*7c478bd9Sstevel@tonic-gate * Offset is beyond the end of the static entries, and 822*7c478bd9Sstevel@tonic-gate * we have no dynamic entries. Set EOF. 823*7c478bd9Sstevel@tonic-gate */ 824*7c478bd9Sstevel@tonic-gate eof = 1; 825*7c478bd9Sstevel@tonic-gate } 826*7c478bd9Sstevel@tonic-gate } 827*7c478bd9Sstevel@tonic-gate 828*7c478bd9Sstevel@tonic-gate return (gfs_readdir_fini(&gstate, error, eofp, eof)); 829*7c478bd9Sstevel@tonic-gate } 830*7c478bd9Sstevel@tonic-gate 831*7c478bd9Sstevel@tonic-gate 832*7c478bd9Sstevel@tonic-gate /* 833*7c478bd9Sstevel@tonic-gate * gfs_vop_lookup: VOP_LOOKUP() entry point 834*7c478bd9Sstevel@tonic-gate * 835*7c478bd9Sstevel@tonic-gate * For use directly in vnode ops table. Given a GFS directory, calls 836*7c478bd9Sstevel@tonic-gate * gfs_dir_lookup() as necessary. 837*7c478bd9Sstevel@tonic-gate */ 838*7c478bd9Sstevel@tonic-gate /* ARGSUSED */ 839*7c478bd9Sstevel@tonic-gate int 840*7c478bd9Sstevel@tonic-gate gfs_vop_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, pathname_t *pnp, 841*7c478bd9Sstevel@tonic-gate int flags, vnode_t *rdir, cred_t *cr) 842*7c478bd9Sstevel@tonic-gate { 843*7c478bd9Sstevel@tonic-gate return (gfs_dir_lookup(dvp, nm, vpp)); 844*7c478bd9Sstevel@tonic-gate } 845*7c478bd9Sstevel@tonic-gate 846*7c478bd9Sstevel@tonic-gate /* 847*7c478bd9Sstevel@tonic-gate * gfs_vop_readdir: VOP_READDIR() entry point 848*7c478bd9Sstevel@tonic-gate * 849*7c478bd9Sstevel@tonic-gate * For use directly in vnode ops table. Given a GFS directory, calls 850*7c478bd9Sstevel@tonic-gate * gfs_dir_readdir() as necessary. 851*7c478bd9Sstevel@tonic-gate */ 852*7c478bd9Sstevel@tonic-gate /* ARGSUSED */ 853*7c478bd9Sstevel@tonic-gate int 854*7c478bd9Sstevel@tonic-gate gfs_vop_readdir(vnode_t *vp, uio_t *uiop, cred_t *cr, int *eofp) 855*7c478bd9Sstevel@tonic-gate { 856*7c478bd9Sstevel@tonic-gate return (gfs_dir_readdir(vp, uiop, eofp, NULL)); 857*7c478bd9Sstevel@tonic-gate } 858*7c478bd9Sstevel@tonic-gate 859*7c478bd9Sstevel@tonic-gate 860*7c478bd9Sstevel@tonic-gate /* 861*7c478bd9Sstevel@tonic-gate * gfs_vop_map: VOP_MAP() entry point 862*7c478bd9Sstevel@tonic-gate * 863*7c478bd9Sstevel@tonic-gate * Convenient routine for handling pseudo-files that wish to allow mmap() calls. 864*7c478bd9Sstevel@tonic-gate * This function only works for readonly files, and uses the read function for 865*7c478bd9Sstevel@tonic-gate * the vnode to fill in the data. The mapped data is immediately faulted in and 866*7c478bd9Sstevel@tonic-gate * filled with the necessary data during this call; there are no getpage() or 867*7c478bd9Sstevel@tonic-gate * putpage() routines. 868*7c478bd9Sstevel@tonic-gate */ 869*7c478bd9Sstevel@tonic-gate /* ARGSUSED */ 870*7c478bd9Sstevel@tonic-gate int 871*7c478bd9Sstevel@tonic-gate gfs_vop_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp, 872*7c478bd9Sstevel@tonic-gate size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cred) 873*7c478bd9Sstevel@tonic-gate { 874*7c478bd9Sstevel@tonic-gate int rv; 875*7c478bd9Sstevel@tonic-gate ssize_t resid = len; 876*7c478bd9Sstevel@tonic-gate 877*7c478bd9Sstevel@tonic-gate /* 878*7c478bd9Sstevel@tonic-gate * Check for bad parameters 879*7c478bd9Sstevel@tonic-gate */ 880*7c478bd9Sstevel@tonic-gate #ifdef _ILP32 881*7c478bd9Sstevel@tonic-gate if (len > MAXOFF_T) 882*7c478bd9Sstevel@tonic-gate return (ENOMEM); 883*7c478bd9Sstevel@tonic-gate #endif 884*7c478bd9Sstevel@tonic-gate if (vp->v_flag & VNOMAP) 885*7c478bd9Sstevel@tonic-gate return (ENOTSUP); 886*7c478bd9Sstevel@tonic-gate if (off > MAXOFF_T) 887*7c478bd9Sstevel@tonic-gate return (EFBIG); 888*7c478bd9Sstevel@tonic-gate if ((long)off < 0 || (long)(off + len) < 0) 889*7c478bd9Sstevel@tonic-gate return (EINVAL); 890*7c478bd9Sstevel@tonic-gate if (vp->v_type != VREG) 891*7c478bd9Sstevel@tonic-gate return (ENODEV); 892*7c478bd9Sstevel@tonic-gate if ((prot & (PROT_EXEC | PROT_WRITE)) != 0) 893*7c478bd9Sstevel@tonic-gate return (EACCES); 894*7c478bd9Sstevel@tonic-gate 895*7c478bd9Sstevel@tonic-gate /* 896*7c478bd9Sstevel@tonic-gate * Find appropriate address if needed, otherwise clear address range. 897*7c478bd9Sstevel@tonic-gate */ 898*7c478bd9Sstevel@tonic-gate as_rangelock(as); 899*7c478bd9Sstevel@tonic-gate if ((flags & MAP_FIXED) == 0) { 900*7c478bd9Sstevel@tonic-gate map_addr(addrp, len, (offset_t)off, 1, flags); 901*7c478bd9Sstevel@tonic-gate if (*addrp == NULL) { 902*7c478bd9Sstevel@tonic-gate as_rangeunlock(as); 903*7c478bd9Sstevel@tonic-gate return (ENOMEM); 904*7c478bd9Sstevel@tonic-gate } 905*7c478bd9Sstevel@tonic-gate } else { 906*7c478bd9Sstevel@tonic-gate (void) as_unmap(as, *addrp, len); 907*7c478bd9Sstevel@tonic-gate } 908*7c478bd9Sstevel@tonic-gate 909*7c478bd9Sstevel@tonic-gate /* 910*7c478bd9Sstevel@tonic-gate * Create mapping 911*7c478bd9Sstevel@tonic-gate */ 912*7c478bd9Sstevel@tonic-gate rv = as_map(as, *addrp, len, segvn_create, zfod_argsp); 913*7c478bd9Sstevel@tonic-gate as_rangeunlock(as); 914*7c478bd9Sstevel@tonic-gate if (rv != 0) 915*7c478bd9Sstevel@tonic-gate return (rv); 916*7c478bd9Sstevel@tonic-gate 917*7c478bd9Sstevel@tonic-gate /* 918*7c478bd9Sstevel@tonic-gate * Fill with data from read() 919*7c478bd9Sstevel@tonic-gate */ 920*7c478bd9Sstevel@tonic-gate rv = vn_rdwr(UIO_READ, vp, *addrp, len, off, UIO_USERSPACE, 921*7c478bd9Sstevel@tonic-gate 0, (rlim64_t)0, cred, &resid); 922*7c478bd9Sstevel@tonic-gate 923*7c478bd9Sstevel@tonic-gate if (rv == 0 && resid != 0) 924*7c478bd9Sstevel@tonic-gate rv = ENXIO; 925*7c478bd9Sstevel@tonic-gate 926*7c478bd9Sstevel@tonic-gate if (rv != 0) { 927*7c478bd9Sstevel@tonic-gate as_rangelock(as); 928*7c478bd9Sstevel@tonic-gate (void) as_unmap(as, *addrp, len); 929*7c478bd9Sstevel@tonic-gate as_rangeunlock(as); 930*7c478bd9Sstevel@tonic-gate } 931*7c478bd9Sstevel@tonic-gate 932*7c478bd9Sstevel@tonic-gate return (rv); 933*7c478bd9Sstevel@tonic-gate } 934*7c478bd9Sstevel@tonic-gate 935*7c478bd9Sstevel@tonic-gate /* 936*7c478bd9Sstevel@tonic-gate * gfs_vop_inactive: VOP_INACTIVE() entry point 937*7c478bd9Sstevel@tonic-gate * 938*7c478bd9Sstevel@tonic-gate * Given a vnode that is a GFS file or directory, call gfs_file_inactive() or 939*7c478bd9Sstevel@tonic-gate * gfs_dir_inactive() as necessary, and kmem_free()s associated private data. 940*7c478bd9Sstevel@tonic-gate */ 941*7c478bd9Sstevel@tonic-gate /* ARGSUSED */ 942*7c478bd9Sstevel@tonic-gate void 943*7c478bd9Sstevel@tonic-gate gfs_vop_inactive(vnode_t *vp, cred_t *cr) 944*7c478bd9Sstevel@tonic-gate { 945*7c478bd9Sstevel@tonic-gate gfs_file_t *fp = vp->v_data; 946*7c478bd9Sstevel@tonic-gate void *data; 947*7c478bd9Sstevel@tonic-gate 948*7c478bd9Sstevel@tonic-gate if (fp->gfs_type == GFS_DIR) 949*7c478bd9Sstevel@tonic-gate data = gfs_dir_inactive(vp); 950*7c478bd9Sstevel@tonic-gate else 951*7c478bd9Sstevel@tonic-gate data = gfs_file_inactive(vp); 952*7c478bd9Sstevel@tonic-gate 953*7c478bd9Sstevel@tonic-gate if (data != NULL) 954*7c478bd9Sstevel@tonic-gate kmem_free(data, fp->gfs_size); 955*7c478bd9Sstevel@tonic-gate } 956