17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5092e3d7cSjk115741 * Common Development and Distribution License (the "License"). 6092e3d7cSjk115741 * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 21092e3d7cSjk115741 /* Portions Copyright 2007 Shivakumar GN */ 227c478bd9Sstevel@tonic-gate /* 23092e3d7cSjk115741 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate #include <sys/types.h> 307c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h> 317c478bd9Sstevel@tonic-gate #include <sys/debug.h> 327c478bd9Sstevel@tonic-gate #include <sys/dirent.h> 337c478bd9Sstevel@tonic-gate #include <sys/kmem.h> 347c478bd9Sstevel@tonic-gate #include <sys/mman.h> 357c478bd9Sstevel@tonic-gate #include <sys/mutex.h> 367c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 377c478bd9Sstevel@tonic-gate #include <sys/systm.h> 387c478bd9Sstevel@tonic-gate #include <sys/uio.h> 397c478bd9Sstevel@tonic-gate #include <sys/vmsystm.h> 407c478bd9Sstevel@tonic-gate #include <sys/vfs.h> 417c478bd9Sstevel@tonic-gate #include <sys/vnode.h> 427c478bd9Sstevel@tonic-gate 437c478bd9Sstevel@tonic-gate #include <vm/as.h> 447c478bd9Sstevel@tonic-gate #include <vm/seg_vn.h> 457c478bd9Sstevel@tonic-gate 467c478bd9Sstevel@tonic-gate #include <sys/gfs.h> 477c478bd9Sstevel@tonic-gate 487c478bd9Sstevel@tonic-gate /* 497c478bd9Sstevel@tonic-gate * Generic pseudo-filesystem routines. 507c478bd9Sstevel@tonic-gate * 517c478bd9Sstevel@tonic-gate * There are significant similarities between the implementation of certain file 527c478bd9Sstevel@tonic-gate * system entry points across different filesystems. While one could attempt to 537c478bd9Sstevel@tonic-gate * "choke up on the bat" and incorporate common functionality into a VOP 54092e3d7cSjk115741 * preamble or postamble, such an approach is limited in the benefit it can 557c478bd9Sstevel@tonic-gate * provide. In this file we instead define a toolkit of routines which can be 567c478bd9Sstevel@tonic-gate * called from a filesystem (with in-kernel pseudo-filesystems being the focus 577c478bd9Sstevel@tonic-gate * of the exercise) in a more component-like fashion. 587c478bd9Sstevel@tonic-gate * 597c478bd9Sstevel@tonic-gate * There are three basic classes of routines: 607c478bd9Sstevel@tonic-gate * 617c478bd9Sstevel@tonic-gate * 1) Lowlevel support routines 627c478bd9Sstevel@tonic-gate * 637c478bd9Sstevel@tonic-gate * These routines are designed to play a support role for existing 647c478bd9Sstevel@tonic-gate * pseudo-filesystems (such as procfs). They simplify common tasks, 657c478bd9Sstevel@tonic-gate * without enforcing the filesystem to hand over management to GFS. The 667c478bd9Sstevel@tonic-gate * routines covered are: 677c478bd9Sstevel@tonic-gate * 687c478bd9Sstevel@tonic-gate * gfs_readdir_init() 697c478bd9Sstevel@tonic-gate * gfs_readdir_emit() 707c478bd9Sstevel@tonic-gate * gfs_readdir_emitn() 717c478bd9Sstevel@tonic-gate * gfs_readdir_pred() 727c478bd9Sstevel@tonic-gate * gfs_readdir_fini() 737c478bd9Sstevel@tonic-gate * gfs_lookup_dot() 747c478bd9Sstevel@tonic-gate * 757c478bd9Sstevel@tonic-gate * 2) Complete GFS management 767c478bd9Sstevel@tonic-gate * 777c478bd9Sstevel@tonic-gate * These routines take a more active role in management of the 787c478bd9Sstevel@tonic-gate * pseudo-filesystem. They handle the relationship between vnode private 797c478bd9Sstevel@tonic-gate * data and VFS data, as well as the relationship between vnodes in the 80092e3d7cSjk115741 * directory hierarchy. 817c478bd9Sstevel@tonic-gate * 827c478bd9Sstevel@tonic-gate * In order to use these interfaces, the first member of every private 837c478bd9Sstevel@tonic-gate * v_data must be a gfs_file_t or a gfs_dir_t. This hands over all control 847c478bd9Sstevel@tonic-gate * to GFS. 857c478bd9Sstevel@tonic-gate * 867c478bd9Sstevel@tonic-gate * gfs_file_create() 877c478bd9Sstevel@tonic-gate * gfs_dir_create() 887c478bd9Sstevel@tonic-gate * gfs_root_create() 897c478bd9Sstevel@tonic-gate * 907c478bd9Sstevel@tonic-gate * gfs_file_inactive() 917c478bd9Sstevel@tonic-gate * gfs_dir_inactive() 927c478bd9Sstevel@tonic-gate * gfs_dir_lookup() 937c478bd9Sstevel@tonic-gate * gfs_dir_readdir() 947c478bd9Sstevel@tonic-gate * 957c478bd9Sstevel@tonic-gate * gfs_vop_inactive() 967c478bd9Sstevel@tonic-gate * gfs_vop_lookup() 977c478bd9Sstevel@tonic-gate * gfs_vop_readdir() 987c478bd9Sstevel@tonic-gate * gfs_vop_map() 99*a237e38eSth199096 * 100*a237e38eSth199096 * 3) Single File pseudo-filesystems 101*a237e38eSth199096 * 102*a237e38eSth199096 * This routine creates a rooted file to be overlayed ontop of another 103*a237e38eSth199096 * file in the physical filespace. 104*a237e38eSth199096 * 105*a237e38eSth199096 * Note that the parent is NULL (actually the vfs), but there is nothing 106*a237e38eSth199096 * technically keeping such a file from utilizing the "Complete GFS 107*a237e38eSth199096 * management" set of routines. 108*a237e38eSth199096 * 109*a237e38eSth199096 * gfs_root_create_file() 1107c478bd9Sstevel@tonic-gate */ 1117c478bd9Sstevel@tonic-gate 1127c478bd9Sstevel@tonic-gate /* 1137c478bd9Sstevel@tonic-gate * gfs_make_opsvec: take an array of vnode type definitions and create 1147c478bd9Sstevel@tonic-gate * their vnodeops_t structures 1157c478bd9Sstevel@tonic-gate * 1167c478bd9Sstevel@tonic-gate * This routine takes an array of gfs_opsvec_t's. It could 1177c478bd9Sstevel@tonic-gate * alternatively take an array of gfs_opsvec_t*'s, which would allow 1187c478bd9Sstevel@tonic-gate * vnode types to be completely defined in files external to the caller 1197c478bd9Sstevel@tonic-gate * of gfs_make_opsvec(). As it stands, much more sharing takes place -- 1207c478bd9Sstevel@tonic-gate * both the caller and the vnode type provider need to access gfsv_ops 1217c478bd9Sstevel@tonic-gate * and gfsv_template, and the caller also needs to know gfsv_name. 1227c478bd9Sstevel@tonic-gate */ 1237c478bd9Sstevel@tonic-gate int 1247c478bd9Sstevel@tonic-gate gfs_make_opsvec(gfs_opsvec_t *vec) 1257c478bd9Sstevel@tonic-gate { 1267c478bd9Sstevel@tonic-gate int error, i; 1277c478bd9Sstevel@tonic-gate 1287c478bd9Sstevel@tonic-gate for (i = 0; ; i++) { 1297c478bd9Sstevel@tonic-gate if (vec[i].gfsv_name == NULL) 1307c478bd9Sstevel@tonic-gate return (0); 1317c478bd9Sstevel@tonic-gate error = vn_make_ops(vec[i].gfsv_name, vec[i].gfsv_template, 1327c478bd9Sstevel@tonic-gate vec[i].gfsv_ops); 1337c478bd9Sstevel@tonic-gate if (error) 1347c478bd9Sstevel@tonic-gate break; 1357c478bd9Sstevel@tonic-gate } 1367c478bd9Sstevel@tonic-gate 1377c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "gfs_make_opsvec: bad vnode ops template for '%s'", 1387c478bd9Sstevel@tonic-gate vec[i].gfsv_name); 1397c478bd9Sstevel@tonic-gate for (i--; i >= 0; i--) { 1407c478bd9Sstevel@tonic-gate vn_freevnodeops(*vec[i].gfsv_ops); 1417c478bd9Sstevel@tonic-gate *vec[i].gfsv_ops = NULL; 1427c478bd9Sstevel@tonic-gate } 1437c478bd9Sstevel@tonic-gate return (error); 1447c478bd9Sstevel@tonic-gate } 1457c478bd9Sstevel@tonic-gate 1467c478bd9Sstevel@tonic-gate /* 1477c478bd9Sstevel@tonic-gate * Low level directory routines 1487c478bd9Sstevel@tonic-gate * 1497c478bd9Sstevel@tonic-gate * These routines provide some simple abstractions for reading directories. 1507c478bd9Sstevel@tonic-gate * They are designed to be used by existing pseudo filesystems (namely procfs) 1517c478bd9Sstevel@tonic-gate * that already have a complicated management infrastructure. 1527c478bd9Sstevel@tonic-gate */ 1537c478bd9Sstevel@tonic-gate 1547c478bd9Sstevel@tonic-gate /* 1557c478bd9Sstevel@tonic-gate * gfs_readdir_init: initiate a generic readdir 1567c478bd9Sstevel@tonic-gate * st - a pointer to an uninitialized gfs_readdir_state_t structure 1577c478bd9Sstevel@tonic-gate * name_max - the directory's maximum file name length 1587c478bd9Sstevel@tonic-gate * ureclen - the exported file-space record length (1 for non-legacy FSs) 1597c478bd9Sstevel@tonic-gate * uiop - the uiop passed to readdir 1607c478bd9Sstevel@tonic-gate * parent - the parent directory's inode 1617c478bd9Sstevel@tonic-gate * self - this directory's inode 1627c478bd9Sstevel@tonic-gate * 1637c478bd9Sstevel@tonic-gate * Returns 0 or a non-zero errno. 1647c478bd9Sstevel@tonic-gate * 1657c478bd9Sstevel@tonic-gate * Typical VOP_READDIR usage of gfs_readdir_*: 1667c478bd9Sstevel@tonic-gate * 1677c478bd9Sstevel@tonic-gate * if ((error = gfs_readdir_init(...)) != 0) 1687c478bd9Sstevel@tonic-gate * return (error); 1697c478bd9Sstevel@tonic-gate * eof = 0; 1707c478bd9Sstevel@tonic-gate * while ((error = gfs_readdir_pred(..., &voffset)) != 0) { 1717c478bd9Sstevel@tonic-gate * if (!consumer_entry_at(voffset)) 1727c478bd9Sstevel@tonic-gate * voffset = consumer_next_entry(voffset); 1737c478bd9Sstevel@tonic-gate * if (consumer_eof(voffset)) { 1747c478bd9Sstevel@tonic-gate * eof = 1 1757c478bd9Sstevel@tonic-gate * break; 1767c478bd9Sstevel@tonic-gate * } 1777c478bd9Sstevel@tonic-gate * if ((error = gfs_readdir_emit(..., voffset, 1787c478bd9Sstevel@tonic-gate * consumer_ino(voffset), consumer_name(voffset))) != 0) 1797c478bd9Sstevel@tonic-gate * break; 1807c478bd9Sstevel@tonic-gate * } 1817c478bd9Sstevel@tonic-gate * return (gfs_readdir_fini(..., error, eofp, eof)); 1827c478bd9Sstevel@tonic-gate * 1837c478bd9Sstevel@tonic-gate * As you can see, a zero result from gfs_readdir_pred() or 1847c478bd9Sstevel@tonic-gate * gfs_readdir_emit() indicates that processing should continue, 1857c478bd9Sstevel@tonic-gate * whereas a non-zero result indicates that the loop should terminate. 1867c478bd9Sstevel@tonic-gate * Most consumers need do nothing more than let gfs_readdir_fini() 1877c478bd9Sstevel@tonic-gate * determine what the cause of failure was and return the appropriate 1887c478bd9Sstevel@tonic-gate * value. 1897c478bd9Sstevel@tonic-gate */ 1907c478bd9Sstevel@tonic-gate int 1917c478bd9Sstevel@tonic-gate gfs_readdir_init(gfs_readdir_state_t *st, int name_max, int ureclen, 1927c478bd9Sstevel@tonic-gate uio_t *uiop, ino64_t parent, ino64_t self) 1937c478bd9Sstevel@tonic-gate { 1947c478bd9Sstevel@tonic-gate if (uiop->uio_loffset < 0 || uiop->uio_resid <= 0 || 1957c478bd9Sstevel@tonic-gate (uiop->uio_loffset % ureclen) != 0) 1967c478bd9Sstevel@tonic-gate return (EINVAL); 1977c478bd9Sstevel@tonic-gate 1987c478bd9Sstevel@tonic-gate st->grd_ureclen = ureclen; 1997c478bd9Sstevel@tonic-gate st->grd_oresid = uiop->uio_resid; 2007c478bd9Sstevel@tonic-gate st->grd_namlen = name_max; 2017c478bd9Sstevel@tonic-gate st->grd_dirent = kmem_zalloc(DIRENT64_RECLEN(st->grd_namlen), KM_SLEEP); 2027c478bd9Sstevel@tonic-gate st->grd_parent = parent; 2037c478bd9Sstevel@tonic-gate st->grd_self = self; 2047c478bd9Sstevel@tonic-gate 2057c478bd9Sstevel@tonic-gate return (0); 2067c478bd9Sstevel@tonic-gate } 2077c478bd9Sstevel@tonic-gate 2087c478bd9Sstevel@tonic-gate /* 2097c478bd9Sstevel@tonic-gate * gfs_readdir_emit_int: internal routine to emit directory entry 2107c478bd9Sstevel@tonic-gate * 2117c478bd9Sstevel@tonic-gate * st - the current readdir state, which must have d_ino and d_name 2127c478bd9Sstevel@tonic-gate * set 2137c478bd9Sstevel@tonic-gate * uiop - caller-supplied uio pointer 2147c478bd9Sstevel@tonic-gate * next - the offset of the next entry 2157c478bd9Sstevel@tonic-gate */ 2167c478bd9Sstevel@tonic-gate static int 2173f480432Smaybee gfs_readdir_emit_int(gfs_readdir_state_t *st, uio_t *uiop, offset_t next) 2187c478bd9Sstevel@tonic-gate { 2197c478bd9Sstevel@tonic-gate int reclen; 2207c478bd9Sstevel@tonic-gate 2217c478bd9Sstevel@tonic-gate reclen = DIRENT64_RECLEN(strlen(st->grd_dirent->d_name)); 2227c478bd9Sstevel@tonic-gate 2237c478bd9Sstevel@tonic-gate if (reclen > uiop->uio_resid) { 2247c478bd9Sstevel@tonic-gate /* 2257c478bd9Sstevel@tonic-gate * Error if no entries were returned yet 2267c478bd9Sstevel@tonic-gate */ 2277c478bd9Sstevel@tonic-gate if (uiop->uio_resid == st->grd_oresid) 2287c478bd9Sstevel@tonic-gate return (EINVAL); 2297c478bd9Sstevel@tonic-gate return (-1); 2307c478bd9Sstevel@tonic-gate } 2317c478bd9Sstevel@tonic-gate 2320aa600e3Smaybee st->grd_dirent->d_off = next; 2337c478bd9Sstevel@tonic-gate st->grd_dirent->d_reclen = (ushort_t)reclen; 2347c478bd9Sstevel@tonic-gate 2357c478bd9Sstevel@tonic-gate if (uiomove((caddr_t)st->grd_dirent, reclen, UIO_READ, uiop)) 2367c478bd9Sstevel@tonic-gate return (EFAULT); 2377c478bd9Sstevel@tonic-gate 2387c478bd9Sstevel@tonic-gate uiop->uio_loffset = next; 2397c478bd9Sstevel@tonic-gate 2407c478bd9Sstevel@tonic-gate return (0); 2417c478bd9Sstevel@tonic-gate } 2427c478bd9Sstevel@tonic-gate 2437c478bd9Sstevel@tonic-gate /* 2447c478bd9Sstevel@tonic-gate * gfs_readdir_emit: emit a directory entry 2457c478bd9Sstevel@tonic-gate * voff - the virtual offset (obtained from gfs_readdir_pred) 2467c478bd9Sstevel@tonic-gate * ino - the entry's inode 2477c478bd9Sstevel@tonic-gate * name - the entry's name 2487c478bd9Sstevel@tonic-gate * 2497c478bd9Sstevel@tonic-gate * Returns a 0 on success, a non-zero errno on failure, or -1 if the 2507c478bd9Sstevel@tonic-gate * readdir loop should terminate. A non-zero result (either errno or 2517c478bd9Sstevel@tonic-gate * -1) from this function is typically passed directly to 2527c478bd9Sstevel@tonic-gate * gfs_readdir_fini(). 2537c478bd9Sstevel@tonic-gate */ 2547c478bd9Sstevel@tonic-gate int 2557c478bd9Sstevel@tonic-gate gfs_readdir_emit(gfs_readdir_state_t *st, uio_t *uiop, offset_t voff, 2567c478bd9Sstevel@tonic-gate ino64_t ino, const char *name) 2577c478bd9Sstevel@tonic-gate { 2587c478bd9Sstevel@tonic-gate offset_t off = (voff + 2) * st->grd_ureclen; 2597c478bd9Sstevel@tonic-gate 2607c478bd9Sstevel@tonic-gate st->grd_dirent->d_ino = ino; 2617c478bd9Sstevel@tonic-gate (void) strncpy(st->grd_dirent->d_name, name, st->grd_namlen); 2627c478bd9Sstevel@tonic-gate 2637c478bd9Sstevel@tonic-gate /* 2647c478bd9Sstevel@tonic-gate * Inter-entry offsets are invalid, so we assume a record size of 2657c478bd9Sstevel@tonic-gate * grd_ureclen and explicitly set the offset appropriately. 2667c478bd9Sstevel@tonic-gate */ 2673f480432Smaybee return (gfs_readdir_emit_int(st, uiop, off + st->grd_ureclen)); 2687c478bd9Sstevel@tonic-gate } 2697c478bd9Sstevel@tonic-gate 2707c478bd9Sstevel@tonic-gate /* 2717c478bd9Sstevel@tonic-gate * gfs_readdir_emitn: like gfs_readdir_emit(), but takes an integer 2727c478bd9Sstevel@tonic-gate * instead of a string for the entry's name. 2737c478bd9Sstevel@tonic-gate */ 2747c478bd9Sstevel@tonic-gate int 2757c478bd9Sstevel@tonic-gate gfs_readdir_emitn(gfs_readdir_state_t *st, uio_t *uiop, offset_t voff, 2767c478bd9Sstevel@tonic-gate ino64_t ino, unsigned long num) 2777c478bd9Sstevel@tonic-gate { 2787c478bd9Sstevel@tonic-gate char buf[40]; 2797c478bd9Sstevel@tonic-gate 2807c478bd9Sstevel@tonic-gate numtos(num, buf); 2817c478bd9Sstevel@tonic-gate return (gfs_readdir_emit(st, uiop, voff, ino, buf)); 2827c478bd9Sstevel@tonic-gate } 2837c478bd9Sstevel@tonic-gate 2847c478bd9Sstevel@tonic-gate /* 2857c478bd9Sstevel@tonic-gate * gfs_readdir_pred: readdir loop predicate 2867c478bd9Sstevel@tonic-gate * voffp - a pointer in which the next virtual offset should be stored 2877c478bd9Sstevel@tonic-gate * 2887c478bd9Sstevel@tonic-gate * Returns a 0 on success, a non-zero errno on failure, or -1 if the 2897c478bd9Sstevel@tonic-gate * readdir loop should terminate. A non-zero result (either errno or 2907c478bd9Sstevel@tonic-gate * -1) from this function is typically passed directly to 2917c478bd9Sstevel@tonic-gate * gfs_readdir_fini(). 2927c478bd9Sstevel@tonic-gate */ 2937c478bd9Sstevel@tonic-gate int 2947c478bd9Sstevel@tonic-gate gfs_readdir_pred(gfs_readdir_state_t *st, uio_t *uiop, offset_t *voffp) 2957c478bd9Sstevel@tonic-gate { 2967c478bd9Sstevel@tonic-gate offset_t off, voff; 2977c478bd9Sstevel@tonic-gate int error; 2987c478bd9Sstevel@tonic-gate 2997c478bd9Sstevel@tonic-gate top: 3007c478bd9Sstevel@tonic-gate if (uiop->uio_resid <= 0) 3017c478bd9Sstevel@tonic-gate return (-1); 3027c478bd9Sstevel@tonic-gate 3037c478bd9Sstevel@tonic-gate off = uiop->uio_loffset / st->grd_ureclen; 3047c478bd9Sstevel@tonic-gate voff = off - 2; 3057c478bd9Sstevel@tonic-gate if (off == 0) { 3067c478bd9Sstevel@tonic-gate if ((error = gfs_readdir_emit(st, uiop, voff, st->grd_self, 3077c478bd9Sstevel@tonic-gate ".")) == 0) 3087c478bd9Sstevel@tonic-gate goto top; 3097c478bd9Sstevel@tonic-gate } else if (off == 1) { 3107c478bd9Sstevel@tonic-gate if ((error = gfs_readdir_emit(st, uiop, voff, st->grd_parent, 3117c478bd9Sstevel@tonic-gate "..")) == 0) 3127c478bd9Sstevel@tonic-gate goto top; 3137c478bd9Sstevel@tonic-gate } else { 3147c478bd9Sstevel@tonic-gate *voffp = voff; 3157c478bd9Sstevel@tonic-gate return (0); 3167c478bd9Sstevel@tonic-gate } 3177c478bd9Sstevel@tonic-gate 3187c478bd9Sstevel@tonic-gate return (error); 3197c478bd9Sstevel@tonic-gate } 3207c478bd9Sstevel@tonic-gate 3217c478bd9Sstevel@tonic-gate /* 3227c478bd9Sstevel@tonic-gate * gfs_readdir_fini: generic readdir cleanup 3237c478bd9Sstevel@tonic-gate * error - if positive, an error to return 3247c478bd9Sstevel@tonic-gate * eofp - the eofp passed to readdir 3257c478bd9Sstevel@tonic-gate * eof - the eof value 3267c478bd9Sstevel@tonic-gate * 3277c478bd9Sstevel@tonic-gate * Returns a 0 on success, a non-zero errno on failure. This result 3287c478bd9Sstevel@tonic-gate * should be returned from readdir. 3297c478bd9Sstevel@tonic-gate */ 3307c478bd9Sstevel@tonic-gate int 3317c478bd9Sstevel@tonic-gate gfs_readdir_fini(gfs_readdir_state_t *st, int error, int *eofp, int eof) 3327c478bd9Sstevel@tonic-gate { 3337c478bd9Sstevel@tonic-gate kmem_free(st->grd_dirent, DIRENT64_RECLEN(st->grd_namlen)); 3347c478bd9Sstevel@tonic-gate if (error > 0) 3357c478bd9Sstevel@tonic-gate return (error); 3367c478bd9Sstevel@tonic-gate if (eofp) 3377c478bd9Sstevel@tonic-gate *eofp = eof; 3387c478bd9Sstevel@tonic-gate return (0); 3397c478bd9Sstevel@tonic-gate } 3407c478bd9Sstevel@tonic-gate 3417c478bd9Sstevel@tonic-gate /* 3427c478bd9Sstevel@tonic-gate * gfs_lookup_dot 3437c478bd9Sstevel@tonic-gate * 3447c478bd9Sstevel@tonic-gate * Performs a basic check for "." and ".." directory entries. 3457c478bd9Sstevel@tonic-gate */ 3467c478bd9Sstevel@tonic-gate int 3477c478bd9Sstevel@tonic-gate gfs_lookup_dot(vnode_t **vpp, vnode_t *dvp, vnode_t *pvp, const char *nm) 3487c478bd9Sstevel@tonic-gate { 3497c478bd9Sstevel@tonic-gate if (*nm == '\0' || strcmp(nm, ".") == 0) { 3507c478bd9Sstevel@tonic-gate VN_HOLD(dvp); 3517c478bd9Sstevel@tonic-gate *vpp = dvp; 3527c478bd9Sstevel@tonic-gate return (0); 3537c478bd9Sstevel@tonic-gate } else if (strcmp(nm, "..") == 0) { 3547c478bd9Sstevel@tonic-gate if (pvp == NULL) { 3557c478bd9Sstevel@tonic-gate ASSERT(dvp->v_flag & VROOT); 3567c478bd9Sstevel@tonic-gate VN_HOLD(dvp); 3577c478bd9Sstevel@tonic-gate *vpp = dvp; 3587c478bd9Sstevel@tonic-gate } else { 3597c478bd9Sstevel@tonic-gate VN_HOLD(pvp); 3607c478bd9Sstevel@tonic-gate *vpp = pvp; 3617c478bd9Sstevel@tonic-gate } 3627c478bd9Sstevel@tonic-gate return (0); 3637c478bd9Sstevel@tonic-gate } 3647c478bd9Sstevel@tonic-gate 3657c478bd9Sstevel@tonic-gate return (-1); 3667c478bd9Sstevel@tonic-gate } 3677c478bd9Sstevel@tonic-gate 3687c478bd9Sstevel@tonic-gate /* 3697c478bd9Sstevel@tonic-gate * gfs_file_create(): create a new GFS file 3707c478bd9Sstevel@tonic-gate * 3717c478bd9Sstevel@tonic-gate * size - size of private data structure (v_data) 3727c478bd9Sstevel@tonic-gate * pvp - parent vnode (GFS directory) 3737c478bd9Sstevel@tonic-gate * ops - vnode operations vector 3747c478bd9Sstevel@tonic-gate * 3757c478bd9Sstevel@tonic-gate * In order to use this interface, the parent vnode must have been created by 3767c478bd9Sstevel@tonic-gate * gfs_dir_create(), and the private data stored in v_data must have a 3777c478bd9Sstevel@tonic-gate * 'gfs_file_t' as its first field. 3787c478bd9Sstevel@tonic-gate * 3797c478bd9Sstevel@tonic-gate * Given these constraints, this routine will automatically: 3807c478bd9Sstevel@tonic-gate * 3817c478bd9Sstevel@tonic-gate * - Allocate v_data for the vnode 3827c478bd9Sstevel@tonic-gate * - Initialize necessary fields in the vnode 3837c478bd9Sstevel@tonic-gate * - Hold the parent 3847c478bd9Sstevel@tonic-gate */ 3857c478bd9Sstevel@tonic-gate vnode_t * 3867c478bd9Sstevel@tonic-gate gfs_file_create(size_t size, vnode_t *pvp, vnodeops_t *ops) 3877c478bd9Sstevel@tonic-gate { 3887c478bd9Sstevel@tonic-gate gfs_file_t *fp; 3897c478bd9Sstevel@tonic-gate vnode_t *vp; 3907c478bd9Sstevel@tonic-gate 3917c478bd9Sstevel@tonic-gate /* 3927c478bd9Sstevel@tonic-gate * Allocate vnode and internal data structure 3937c478bd9Sstevel@tonic-gate */ 3947c478bd9Sstevel@tonic-gate fp = kmem_zalloc(size, KM_SLEEP); 3957c478bd9Sstevel@tonic-gate vp = vn_alloc(KM_SLEEP); 3967c478bd9Sstevel@tonic-gate 3977c478bd9Sstevel@tonic-gate /* 3987c478bd9Sstevel@tonic-gate * Set up various pointers 3997c478bd9Sstevel@tonic-gate */ 4007c478bd9Sstevel@tonic-gate fp->gfs_vnode = vp; 4017c478bd9Sstevel@tonic-gate fp->gfs_parent = pvp; 4027c478bd9Sstevel@tonic-gate vp->v_data = fp; 4037c478bd9Sstevel@tonic-gate fp->gfs_size = size; 4047c478bd9Sstevel@tonic-gate fp->gfs_type = GFS_FILE; 4057c478bd9Sstevel@tonic-gate 4067c478bd9Sstevel@tonic-gate /* 4077c478bd9Sstevel@tonic-gate * Initialize vnode and hold parent. 4087c478bd9Sstevel@tonic-gate */ 4097c478bd9Sstevel@tonic-gate vn_setops(vp, ops); 4107c478bd9Sstevel@tonic-gate if (pvp) { 4117c478bd9Sstevel@tonic-gate VN_SET_VFS_TYPE_DEV(vp, pvp->v_vfsp, VREG, 0); 4127c478bd9Sstevel@tonic-gate VN_HOLD(pvp); 4137c478bd9Sstevel@tonic-gate } 4147c478bd9Sstevel@tonic-gate 4157c478bd9Sstevel@tonic-gate return (vp); 4167c478bd9Sstevel@tonic-gate } 4177c478bd9Sstevel@tonic-gate 4187c478bd9Sstevel@tonic-gate /* 4197c478bd9Sstevel@tonic-gate * gfs_dir_create: creates a new directory in the parent 4207c478bd9Sstevel@tonic-gate * 4217c478bd9Sstevel@tonic-gate * size - size of private data structure (v_data) 4227c478bd9Sstevel@tonic-gate * pvp - parent vnode (GFS directory) 4237c478bd9Sstevel@tonic-gate * ops - vnode operations vector 4247c478bd9Sstevel@tonic-gate * entries - NULL-terminated list of static entries (if any) 4257c478bd9Sstevel@tonic-gate * maxlen - maximum length of a directory entry 4267c478bd9Sstevel@tonic-gate * readdir_cb - readdir callback (see gfs_dir_readdir) 4277c478bd9Sstevel@tonic-gate * inode_cb - inode callback (see gfs_dir_readdir) 4287c478bd9Sstevel@tonic-gate * lookup_cb - lookup callback (see gfs_dir_lookup) 4297c478bd9Sstevel@tonic-gate * 4307c478bd9Sstevel@tonic-gate * In order to use this function, the first member of the private vnode 4317c478bd9Sstevel@tonic-gate * structure (v_data) must be a gfs_dir_t. For each directory, there are 4327c478bd9Sstevel@tonic-gate * static entries, defined when the structure is initialized, and dynamic 4337c478bd9Sstevel@tonic-gate * entries, retrieved through callbacks. 4347c478bd9Sstevel@tonic-gate * 4357c478bd9Sstevel@tonic-gate * If a directory has static entries, then it must supply a inode callback, 4367c478bd9Sstevel@tonic-gate * which will compute the inode number based on the parent and the index. 4377c478bd9Sstevel@tonic-gate * For a directory with dynamic entries, the caller must supply a readdir 4387c478bd9Sstevel@tonic-gate * callback and a lookup callback. If a static lookup fails, we fall back to 4397c478bd9Sstevel@tonic-gate * the supplied lookup callback, if any. 4407c478bd9Sstevel@tonic-gate * 4417c478bd9Sstevel@tonic-gate * This function also performs the same initialization as gfs_file_create(). 4427c478bd9Sstevel@tonic-gate */ 4437c478bd9Sstevel@tonic-gate vnode_t * 4447c478bd9Sstevel@tonic-gate gfs_dir_create(size_t struct_size, vnode_t *pvp, vnodeops_t *ops, 4457c478bd9Sstevel@tonic-gate gfs_dirent_t *entries, gfs_inode_cb inode_cb, int maxlen, 4467c478bd9Sstevel@tonic-gate gfs_readdir_cb readdir_cb, gfs_lookup_cb lookup_cb) 4477c478bd9Sstevel@tonic-gate { 4487c478bd9Sstevel@tonic-gate vnode_t *vp; 4497c478bd9Sstevel@tonic-gate gfs_dir_t *dp; 4507c478bd9Sstevel@tonic-gate gfs_dirent_t *de; 4517c478bd9Sstevel@tonic-gate 4527c478bd9Sstevel@tonic-gate vp = gfs_file_create(struct_size, pvp, ops); 4537c478bd9Sstevel@tonic-gate vp->v_type = VDIR; 4547c478bd9Sstevel@tonic-gate 4557c478bd9Sstevel@tonic-gate dp = vp->v_data; 4567c478bd9Sstevel@tonic-gate dp->gfsd_file.gfs_type = GFS_DIR; 4577c478bd9Sstevel@tonic-gate dp->gfsd_maxlen = maxlen; 4587c478bd9Sstevel@tonic-gate 4597c478bd9Sstevel@tonic-gate if (entries != NULL) { 4607c478bd9Sstevel@tonic-gate for (de = entries; de->gfse_name != NULL; de++) 4617c478bd9Sstevel@tonic-gate dp->gfsd_nstatic++; 4627c478bd9Sstevel@tonic-gate 4637c478bd9Sstevel@tonic-gate dp->gfsd_static = kmem_alloc( 4647c478bd9Sstevel@tonic-gate dp->gfsd_nstatic * sizeof (gfs_dirent_t), KM_SLEEP); 4657c478bd9Sstevel@tonic-gate bcopy(entries, dp->gfsd_static, 4667c478bd9Sstevel@tonic-gate dp->gfsd_nstatic * sizeof (gfs_dirent_t)); 4677c478bd9Sstevel@tonic-gate } 4687c478bd9Sstevel@tonic-gate 4697c478bd9Sstevel@tonic-gate dp->gfsd_readdir = readdir_cb; 4707c478bd9Sstevel@tonic-gate dp->gfsd_lookup = lookup_cb; 4717c478bd9Sstevel@tonic-gate dp->gfsd_inode = inode_cb; 4727c478bd9Sstevel@tonic-gate 4737c478bd9Sstevel@tonic-gate mutex_init(&dp->gfsd_lock, NULL, MUTEX_DEFAULT, NULL); 4747c478bd9Sstevel@tonic-gate 4757c478bd9Sstevel@tonic-gate return (vp); 4767c478bd9Sstevel@tonic-gate } 4777c478bd9Sstevel@tonic-gate 4787c478bd9Sstevel@tonic-gate /* 4797c478bd9Sstevel@tonic-gate * gfs_root_create(): create a root vnode for a GFS filesystem 4807c478bd9Sstevel@tonic-gate * 4817c478bd9Sstevel@tonic-gate * Similar to gfs_dir_create(), this creates a root vnode for a filesystem. The 4827c478bd9Sstevel@tonic-gate * only difference is that it takes a vfs_t instead of a vnode_t as its parent. 4837c478bd9Sstevel@tonic-gate */ 4847c478bd9Sstevel@tonic-gate vnode_t * 4857c478bd9Sstevel@tonic-gate gfs_root_create(size_t size, vfs_t *vfsp, vnodeops_t *ops, ino64_t ino, 4867c478bd9Sstevel@tonic-gate gfs_dirent_t *entries, gfs_inode_cb inode_cb, int maxlen, 4877c478bd9Sstevel@tonic-gate gfs_readdir_cb readdir_cb, gfs_lookup_cb lookup_cb) 4887c478bd9Sstevel@tonic-gate { 4897c478bd9Sstevel@tonic-gate vnode_t *vp = gfs_dir_create(size, NULL, ops, entries, inode_cb, 4907c478bd9Sstevel@tonic-gate maxlen, readdir_cb, lookup_cb); 4917c478bd9Sstevel@tonic-gate 4927c478bd9Sstevel@tonic-gate /* Manually set the inode */ 4937c478bd9Sstevel@tonic-gate ((gfs_file_t *)vp->v_data)->gfs_ino = ino; 4947c478bd9Sstevel@tonic-gate 4957c478bd9Sstevel@tonic-gate VFS_HOLD(vfsp); 4967c478bd9Sstevel@tonic-gate VN_SET_VFS_TYPE_DEV(vp, vfsp, VDIR, 0); 4977c478bd9Sstevel@tonic-gate vp->v_flag |= VROOT | VNOCACHE | VNOMAP | VNOSWAP | VNOMOUNT; 4987c478bd9Sstevel@tonic-gate 4997c478bd9Sstevel@tonic-gate return (vp); 5007c478bd9Sstevel@tonic-gate } 5017c478bd9Sstevel@tonic-gate 5027c478bd9Sstevel@tonic-gate /* 503*a237e38eSth199096 * gfs_root_create_file(): create a root vnode for a GFS file as a filesystem 504*a237e38eSth199096 * 505*a237e38eSth199096 * Similar to gfs_root_create(), this creates a root vnode for a file to 506*a237e38eSth199096 * be the pseudo-filesystem. 507*a237e38eSth199096 */ 508*a237e38eSth199096 vnode_t * 509*a237e38eSth199096 gfs_root_create_file(size_t size, vfs_t *vfsp, vnodeops_t *ops, ino64_t ino) 510*a237e38eSth199096 { 511*a237e38eSth199096 vnode_t *vp = gfs_file_create(size, NULL, ops); 512*a237e38eSth199096 513*a237e38eSth199096 ((gfs_file_t *)vp->v_data)->gfs_ino = ino; 514*a237e38eSth199096 515*a237e38eSth199096 VFS_HOLD(vfsp); 516*a237e38eSth199096 VN_SET_VFS_TYPE_DEV(vp, vfsp, VREG, 0); 517*a237e38eSth199096 vp->v_flag |= VROOT | VNOCACHE | VNOMAP | VNOSWAP | VNOMOUNT; 518*a237e38eSth199096 519*a237e38eSth199096 return (vp); 520*a237e38eSth199096 } 521*a237e38eSth199096 522*a237e38eSth199096 /* 5237c478bd9Sstevel@tonic-gate * gfs_file_inactive() 5247c478bd9Sstevel@tonic-gate * 5257c478bd9Sstevel@tonic-gate * Called from the VOP_INACTIVE() routine. If necessary, this routine will 5267c478bd9Sstevel@tonic-gate * remove the given vnode from the parent directory and clean up any references 5277c478bd9Sstevel@tonic-gate * in the VFS layer. 5287c478bd9Sstevel@tonic-gate * 5297c478bd9Sstevel@tonic-gate * If the vnode was not removed (due to a race with vget), then NULL is 5307c478bd9Sstevel@tonic-gate * returned. Otherwise, a pointer to the private data is returned. 5317c478bd9Sstevel@tonic-gate */ 5327c478bd9Sstevel@tonic-gate void * 5337c478bd9Sstevel@tonic-gate gfs_file_inactive(vnode_t *vp) 5347c478bd9Sstevel@tonic-gate { 5357c478bd9Sstevel@tonic-gate int i; 5367c478bd9Sstevel@tonic-gate gfs_dirent_t *ge = NULL; 5377c478bd9Sstevel@tonic-gate gfs_file_t *fp = vp->v_data; 5387c478bd9Sstevel@tonic-gate gfs_dir_t *dp = NULL; 5397c478bd9Sstevel@tonic-gate void *data; 5407c478bd9Sstevel@tonic-gate 5417c478bd9Sstevel@tonic-gate if (fp->gfs_parent == NULL) 5427c478bd9Sstevel@tonic-gate goto found; 5437c478bd9Sstevel@tonic-gate 5447c478bd9Sstevel@tonic-gate dp = fp->gfs_parent->v_data; 5457c478bd9Sstevel@tonic-gate 5467c478bd9Sstevel@tonic-gate /* 5477c478bd9Sstevel@tonic-gate * First, see if this vnode is cached in the parent. 5487c478bd9Sstevel@tonic-gate */ 5497c478bd9Sstevel@tonic-gate gfs_dir_lock(dp); 5507c478bd9Sstevel@tonic-gate 5517c478bd9Sstevel@tonic-gate /* 5527c478bd9Sstevel@tonic-gate * Find it in the set of static entries. 5537c478bd9Sstevel@tonic-gate */ 5547c478bd9Sstevel@tonic-gate for (i = 0; i < dp->gfsd_nstatic; i++) { 5557c478bd9Sstevel@tonic-gate ge = &dp->gfsd_static[i]; 5567c478bd9Sstevel@tonic-gate 5577c478bd9Sstevel@tonic-gate if (ge->gfse_vnode == vp) 5587c478bd9Sstevel@tonic-gate goto found; 5597c478bd9Sstevel@tonic-gate } 5607c478bd9Sstevel@tonic-gate 5617c478bd9Sstevel@tonic-gate /* 5627c478bd9Sstevel@tonic-gate * If 'ge' is NULL, then it is a dynamic entry. 5637c478bd9Sstevel@tonic-gate */ 5647c478bd9Sstevel@tonic-gate ge = NULL; 5657c478bd9Sstevel@tonic-gate 5667c478bd9Sstevel@tonic-gate found: 5677c478bd9Sstevel@tonic-gate mutex_enter(&vp->v_lock); 5687c478bd9Sstevel@tonic-gate if (vp->v_count == 1) { 5697c478bd9Sstevel@tonic-gate /* 5707c478bd9Sstevel@tonic-gate * Really remove this vnode 5717c478bd9Sstevel@tonic-gate */ 5727c478bd9Sstevel@tonic-gate data = vp->v_data; 5737c478bd9Sstevel@tonic-gate if (ge != NULL) { 5747c478bd9Sstevel@tonic-gate /* 5757c478bd9Sstevel@tonic-gate * If this was a statically cached entry, simply set the 5767c478bd9Sstevel@tonic-gate * cached vnode to NULL. 5777c478bd9Sstevel@tonic-gate */ 5787c478bd9Sstevel@tonic-gate ge->gfse_vnode = NULL; 5797c478bd9Sstevel@tonic-gate } 5807c478bd9Sstevel@tonic-gate mutex_exit(&vp->v_lock); 5817c478bd9Sstevel@tonic-gate 5827c478bd9Sstevel@tonic-gate /* 5837c478bd9Sstevel@tonic-gate * Free vnode and release parent 5847c478bd9Sstevel@tonic-gate */ 5857c478bd9Sstevel@tonic-gate if (fp->gfs_parent) { 5867c478bd9Sstevel@tonic-gate gfs_dir_unlock(dp); 5877c478bd9Sstevel@tonic-gate VN_RELE(fp->gfs_parent); 5887c478bd9Sstevel@tonic-gate } else { 5897c478bd9Sstevel@tonic-gate ASSERT(vp->v_vfsp != NULL); 5907c478bd9Sstevel@tonic-gate VFS_RELE(vp->v_vfsp); 5917c478bd9Sstevel@tonic-gate } 5927c478bd9Sstevel@tonic-gate vn_free(vp); 5937c478bd9Sstevel@tonic-gate } else { 5947c478bd9Sstevel@tonic-gate vp->v_count--; 5957c478bd9Sstevel@tonic-gate data = NULL; 5967c478bd9Sstevel@tonic-gate mutex_exit(&vp->v_lock); 5977c478bd9Sstevel@tonic-gate if (dp) 5987c478bd9Sstevel@tonic-gate gfs_dir_unlock(dp); 5997c478bd9Sstevel@tonic-gate } 6007c478bd9Sstevel@tonic-gate 6017c478bd9Sstevel@tonic-gate return (data); 6027c478bd9Sstevel@tonic-gate } 6037c478bd9Sstevel@tonic-gate 6047c478bd9Sstevel@tonic-gate /* 6057c478bd9Sstevel@tonic-gate * gfs_dir_inactive() 6067c478bd9Sstevel@tonic-gate * 6077c478bd9Sstevel@tonic-gate * Same as above, but for directories. 6087c478bd9Sstevel@tonic-gate */ 6097c478bd9Sstevel@tonic-gate void * 6107c478bd9Sstevel@tonic-gate gfs_dir_inactive(vnode_t *vp) 6117c478bd9Sstevel@tonic-gate { 6127c478bd9Sstevel@tonic-gate gfs_dir_t *dp; 6137c478bd9Sstevel@tonic-gate 6147c478bd9Sstevel@tonic-gate ASSERT(vp->v_type == VDIR); 6157c478bd9Sstevel@tonic-gate 6167c478bd9Sstevel@tonic-gate if ((dp = gfs_file_inactive(vp)) != NULL) { 6177c478bd9Sstevel@tonic-gate mutex_destroy(&dp->gfsd_lock); 6187c478bd9Sstevel@tonic-gate if (dp->gfsd_nstatic) 6197c478bd9Sstevel@tonic-gate kmem_free(dp->gfsd_static, 6207c478bd9Sstevel@tonic-gate dp->gfsd_nstatic * sizeof (gfs_dirent_t)); 6217c478bd9Sstevel@tonic-gate } 6227c478bd9Sstevel@tonic-gate 6237c478bd9Sstevel@tonic-gate return (dp); 6247c478bd9Sstevel@tonic-gate } 6257c478bd9Sstevel@tonic-gate 6267c478bd9Sstevel@tonic-gate /* 6277c478bd9Sstevel@tonic-gate * gfs_dir_lookup() 6287c478bd9Sstevel@tonic-gate * 6297c478bd9Sstevel@tonic-gate * Looks up the given name in the directory and returns the corresponding vnode, 6307c478bd9Sstevel@tonic-gate * if found. 6317c478bd9Sstevel@tonic-gate * 6327c478bd9Sstevel@tonic-gate * First, we search statically defined entries, if any. If a match is found, 6337c478bd9Sstevel@tonic-gate * and GFS_CACHE_VNODE is set and the vnode exists, we simply return the 6347c478bd9Sstevel@tonic-gate * existing vnode. Otherwise, we call the static entry's callback routine, 6357c478bd9Sstevel@tonic-gate * caching the result if necessary. 6367c478bd9Sstevel@tonic-gate * 6377c478bd9Sstevel@tonic-gate * If no static entry is found, we invoke the lookup callback, if any. The 6387c478bd9Sstevel@tonic-gate * arguments to this callback are: 6397c478bd9Sstevel@tonic-gate * 6407c478bd9Sstevel@tonic-gate * int gfs_lookup_cb(vnode_t *pvp, const char *nm, vnode_t **vpp); 6417c478bd9Sstevel@tonic-gate * 6427c478bd9Sstevel@tonic-gate * pvp - parent vnode 6437c478bd9Sstevel@tonic-gate * nm - name of entry 6447c478bd9Sstevel@tonic-gate * vpp - pointer to resulting vnode 6457c478bd9Sstevel@tonic-gate * 6467c478bd9Sstevel@tonic-gate * Returns 0 on success, non-zero on error. 6477c478bd9Sstevel@tonic-gate */ 6487c478bd9Sstevel@tonic-gate int 6497c478bd9Sstevel@tonic-gate gfs_dir_lookup(vnode_t *dvp, const char *nm, vnode_t **vpp) 6507c478bd9Sstevel@tonic-gate { 6517c478bd9Sstevel@tonic-gate int i; 6527c478bd9Sstevel@tonic-gate gfs_dirent_t *ge; 6537c478bd9Sstevel@tonic-gate vnode_t *vp; 6547c478bd9Sstevel@tonic-gate gfs_dir_t *dp = dvp->v_data; 6557c478bd9Sstevel@tonic-gate int ret = 0; 6567c478bd9Sstevel@tonic-gate 6577c478bd9Sstevel@tonic-gate ASSERT(dvp->v_type == VDIR); 6587c478bd9Sstevel@tonic-gate 6597c478bd9Sstevel@tonic-gate if (gfs_lookup_dot(vpp, dvp, dp->gfsd_file.gfs_parent, nm) == 0) 6607c478bd9Sstevel@tonic-gate return (0); 6617c478bd9Sstevel@tonic-gate 6627c478bd9Sstevel@tonic-gate gfs_dir_lock(dp); 6637c478bd9Sstevel@tonic-gate 6647c478bd9Sstevel@tonic-gate /* 6657c478bd9Sstevel@tonic-gate * Search static entries. 6667c478bd9Sstevel@tonic-gate */ 6677c478bd9Sstevel@tonic-gate for (i = 0; i < dp->gfsd_nstatic; i++) { 6687c478bd9Sstevel@tonic-gate ge = &dp->gfsd_static[i]; 6697c478bd9Sstevel@tonic-gate 6707c478bd9Sstevel@tonic-gate if (strcmp(ge->gfse_name, nm) == 0) { 6717c478bd9Sstevel@tonic-gate if (ge->gfse_vnode) { 6727c478bd9Sstevel@tonic-gate ASSERT(ge->gfse_flags & GFS_CACHE_VNODE); 6737c478bd9Sstevel@tonic-gate vp = ge->gfse_vnode; 6747c478bd9Sstevel@tonic-gate VN_HOLD(vp); 6757c478bd9Sstevel@tonic-gate goto out; 6767c478bd9Sstevel@tonic-gate } 6777c478bd9Sstevel@tonic-gate 6787c478bd9Sstevel@tonic-gate /* 679092e3d7cSjk115741 * We drop the directory lock, as the constructor will 6807c478bd9Sstevel@tonic-gate * need to do KM_SLEEP allocations. If we return from 6817c478bd9Sstevel@tonic-gate * the constructor only to find that a parallel 6827c478bd9Sstevel@tonic-gate * operation has completed, and GFS_CACHE_VNODE is set 6837c478bd9Sstevel@tonic-gate * for this entry, we discard the result in favor of the 6847c478bd9Sstevel@tonic-gate * cached vnode. 6857c478bd9Sstevel@tonic-gate */ 6867c478bd9Sstevel@tonic-gate gfs_dir_unlock(dp); 6877c478bd9Sstevel@tonic-gate vp = ge->gfse_ctor(dvp); 6887c478bd9Sstevel@tonic-gate gfs_dir_lock(dp); 6897c478bd9Sstevel@tonic-gate 6907c478bd9Sstevel@tonic-gate ((gfs_file_t *)vp->v_data)->gfs_index = i; 6917c478bd9Sstevel@tonic-gate 6927c478bd9Sstevel@tonic-gate /* Set the inode according to the callback. */ 6937c478bd9Sstevel@tonic-gate ((gfs_file_t *)vp->v_data)->gfs_ino = 6947c478bd9Sstevel@tonic-gate dp->gfsd_inode(dvp, i); 6957c478bd9Sstevel@tonic-gate 6967c478bd9Sstevel@tonic-gate if (ge->gfse_flags & GFS_CACHE_VNODE) { 6977c478bd9Sstevel@tonic-gate if (ge->gfse_vnode == NULL) { 6987c478bd9Sstevel@tonic-gate ge->gfse_vnode = vp; 6997c478bd9Sstevel@tonic-gate } else { 7007c478bd9Sstevel@tonic-gate /* 7017c478bd9Sstevel@tonic-gate * A parallel constructor beat us to it; 7027c478bd9Sstevel@tonic-gate * return existing vnode. We have to be 7037c478bd9Sstevel@tonic-gate * careful because we can't release the 7047c478bd9Sstevel@tonic-gate * current vnode while holding the 7057c478bd9Sstevel@tonic-gate * directory lock; its inactive routine 7067c478bd9Sstevel@tonic-gate * will try to lock this directory. 7077c478bd9Sstevel@tonic-gate */ 7087c478bd9Sstevel@tonic-gate vnode_t *oldvp = vp; 7097c478bd9Sstevel@tonic-gate vp = ge->gfse_vnode; 7107c478bd9Sstevel@tonic-gate VN_HOLD(vp); 7117c478bd9Sstevel@tonic-gate 7127c478bd9Sstevel@tonic-gate gfs_dir_unlock(dp); 7137c478bd9Sstevel@tonic-gate VN_RELE(oldvp); 7147c478bd9Sstevel@tonic-gate gfs_dir_lock(dp); 7157c478bd9Sstevel@tonic-gate } 7167c478bd9Sstevel@tonic-gate } 7177c478bd9Sstevel@tonic-gate 7187c478bd9Sstevel@tonic-gate goto out; 7197c478bd9Sstevel@tonic-gate } 7207c478bd9Sstevel@tonic-gate } 7217c478bd9Sstevel@tonic-gate 7227c478bd9Sstevel@tonic-gate /* 7237c478bd9Sstevel@tonic-gate * See if there is a dynamic constructor. 7247c478bd9Sstevel@tonic-gate */ 7257c478bd9Sstevel@tonic-gate if (dp->gfsd_lookup) { 7267c478bd9Sstevel@tonic-gate ino64_t ino; 7277c478bd9Sstevel@tonic-gate gfs_file_t *fp; 7287c478bd9Sstevel@tonic-gate 7297c478bd9Sstevel@tonic-gate /* 7307c478bd9Sstevel@tonic-gate * Once again, drop the directory lock, as the lookup routine 7317c478bd9Sstevel@tonic-gate * will need to allocate memory, or otherwise deadlock on this 7327c478bd9Sstevel@tonic-gate * directory. 7337c478bd9Sstevel@tonic-gate */ 7347c478bd9Sstevel@tonic-gate gfs_dir_unlock(dp); 7357c478bd9Sstevel@tonic-gate ret = dp->gfsd_lookup(dvp, nm, &vp, &ino); 7367c478bd9Sstevel@tonic-gate gfs_dir_lock(dp); 7377c478bd9Sstevel@tonic-gate if (ret != 0) 7387c478bd9Sstevel@tonic-gate goto out; 7397c478bd9Sstevel@tonic-gate 7407c478bd9Sstevel@tonic-gate fp = (gfs_file_t *)vp->v_data; 7417c478bd9Sstevel@tonic-gate fp->gfs_index = -1; 7427c478bd9Sstevel@tonic-gate fp->gfs_ino = ino; 7437c478bd9Sstevel@tonic-gate } else { 7447c478bd9Sstevel@tonic-gate /* 7457c478bd9Sstevel@tonic-gate * No static entry found, and there is no lookup callback, so 7467c478bd9Sstevel@tonic-gate * return ENOENT. 7477c478bd9Sstevel@tonic-gate */ 7487c478bd9Sstevel@tonic-gate ret = ENOENT; 7497c478bd9Sstevel@tonic-gate } 7507c478bd9Sstevel@tonic-gate 7517c478bd9Sstevel@tonic-gate out: 7527c478bd9Sstevel@tonic-gate gfs_dir_unlock(dp); 7537c478bd9Sstevel@tonic-gate 754d13f2f50Smarks if (ret == 0) 7557c478bd9Sstevel@tonic-gate *vpp = vp; 756d13f2f50Smarks else 757d13f2f50Smarks *vpp = NULL; 7587c478bd9Sstevel@tonic-gate 7597c478bd9Sstevel@tonic-gate return (ret); 7607c478bd9Sstevel@tonic-gate } 7617c478bd9Sstevel@tonic-gate 7627c478bd9Sstevel@tonic-gate /* 7637c478bd9Sstevel@tonic-gate * gfs_dir_readdir: does a readdir() on the given directory 7647c478bd9Sstevel@tonic-gate * 7657c478bd9Sstevel@tonic-gate * dvp - directory vnode 7667c478bd9Sstevel@tonic-gate * uiop - uio structure 7677c478bd9Sstevel@tonic-gate * eofp - eof pointer 7687c478bd9Sstevel@tonic-gate * data - arbitrary data passed to readdir callback 7697c478bd9Sstevel@tonic-gate * 7707c478bd9Sstevel@tonic-gate * This routine does all the readdir() dirty work. Even so, the caller must 7717c478bd9Sstevel@tonic-gate * supply two callbacks in order to get full compatibility. 7727c478bd9Sstevel@tonic-gate * 7737c478bd9Sstevel@tonic-gate * If the directory contains static entries, an inode callback must be 7747c478bd9Sstevel@tonic-gate * specified. This avoids having to create every vnode and call VOP_GETATTR() 7757c478bd9Sstevel@tonic-gate * when reading the directory. This function has the following arguments: 7767c478bd9Sstevel@tonic-gate * 7777c478bd9Sstevel@tonic-gate * ino_t gfs_inode_cb(vnode_t *vp, int index); 7787c478bd9Sstevel@tonic-gate * 7797c478bd9Sstevel@tonic-gate * vp - vnode for the directory 7807c478bd9Sstevel@tonic-gate * index - index in original gfs_dirent_t array 7817c478bd9Sstevel@tonic-gate * 7827c478bd9Sstevel@tonic-gate * Returns the inode number for the given entry. 7837c478bd9Sstevel@tonic-gate * 7847c478bd9Sstevel@tonic-gate * For directories with dynamic entries, a readdir callback must be provided. 7857c478bd9Sstevel@tonic-gate * This is significantly more complex, thanks to the particulars of 7867c478bd9Sstevel@tonic-gate * VOP_READDIR(). 7877c478bd9Sstevel@tonic-gate * 7887c478bd9Sstevel@tonic-gate * int gfs_readdir_cb(vnode_t *vp, struct dirent64 *dp, int *eofp, 7897c478bd9Sstevel@tonic-gate * offset_t *off, offset_t *nextoff, void *data) 7907c478bd9Sstevel@tonic-gate * 7917c478bd9Sstevel@tonic-gate * vp - directory vnode 7927c478bd9Sstevel@tonic-gate * dp - directory entry, sized according to maxlen given to 7937c478bd9Sstevel@tonic-gate * gfs_dir_create(). callback must fill in d_name and 7947c478bd9Sstevel@tonic-gate * d_ino. 7957c478bd9Sstevel@tonic-gate * eofp - callback must set to 1 when EOF has been reached 7967c478bd9Sstevel@tonic-gate * off - on entry, the last offset read from the directory. Callback 7977c478bd9Sstevel@tonic-gate * must set to the offset of the current entry, typically left 7987c478bd9Sstevel@tonic-gate * untouched. 7997c478bd9Sstevel@tonic-gate * nextoff - callback must set to offset of next entry. Typically 8007c478bd9Sstevel@tonic-gate * (off + 1) 8017c478bd9Sstevel@tonic-gate * data - caller-supplied data 8027c478bd9Sstevel@tonic-gate * 8037c478bd9Sstevel@tonic-gate * Return 0 on success, or error on failure. 8047c478bd9Sstevel@tonic-gate */ 8057c478bd9Sstevel@tonic-gate int 8067c478bd9Sstevel@tonic-gate gfs_dir_readdir(vnode_t *dvp, uio_t *uiop, int *eofp, void *data) 8077c478bd9Sstevel@tonic-gate { 8087c478bd9Sstevel@tonic-gate gfs_readdir_state_t gstate; 8097c478bd9Sstevel@tonic-gate int error, eof = 0; 8107c478bd9Sstevel@tonic-gate ino64_t ino, pino; 8117c478bd9Sstevel@tonic-gate offset_t off, next; 8127c478bd9Sstevel@tonic-gate gfs_dir_t *dp = dvp->v_data; 8137c478bd9Sstevel@tonic-gate 8147c478bd9Sstevel@tonic-gate ino = dp->gfsd_file.gfs_ino; 8157c478bd9Sstevel@tonic-gate 8167c478bd9Sstevel@tonic-gate if (dp->gfsd_file.gfs_parent == NULL) 8177c478bd9Sstevel@tonic-gate pino = ino; /* root of filesystem */ 8187c478bd9Sstevel@tonic-gate else 8197c478bd9Sstevel@tonic-gate pino = ((gfs_file_t *) 8207c478bd9Sstevel@tonic-gate (dp->gfsd_file.gfs_parent->v_data))->gfs_ino; 8217c478bd9Sstevel@tonic-gate 8227c478bd9Sstevel@tonic-gate if ((error = gfs_readdir_init(&gstate, dp->gfsd_maxlen, 1, uiop, 8237c478bd9Sstevel@tonic-gate pino, ino)) != 0) 8247c478bd9Sstevel@tonic-gate return (error); 8257c478bd9Sstevel@tonic-gate 8267c478bd9Sstevel@tonic-gate while ((error = gfs_readdir_pred(&gstate, uiop, &off)) == 0 && 8277c478bd9Sstevel@tonic-gate !eof) { 8287c478bd9Sstevel@tonic-gate 8297c478bd9Sstevel@tonic-gate if (off >= 0 && off < dp->gfsd_nstatic) { 8307c478bd9Sstevel@tonic-gate ino = dp->gfsd_inode(dvp, off); 8317c478bd9Sstevel@tonic-gate 8327c478bd9Sstevel@tonic-gate if ((error = gfs_readdir_emit(&gstate, uiop, 8337c478bd9Sstevel@tonic-gate off, ino, dp->gfsd_static[off].gfse_name)) 8347c478bd9Sstevel@tonic-gate != 0) 8357c478bd9Sstevel@tonic-gate break; 8367c478bd9Sstevel@tonic-gate 8377c478bd9Sstevel@tonic-gate } else if (dp->gfsd_readdir) { 8387c478bd9Sstevel@tonic-gate off -= dp->gfsd_nstatic; 8397c478bd9Sstevel@tonic-gate 8407c478bd9Sstevel@tonic-gate if ((error = dp->gfsd_readdir(dvp, 8417c478bd9Sstevel@tonic-gate gstate.grd_dirent, &eof, &off, &next, 8427c478bd9Sstevel@tonic-gate data)) != 0 || eof) 8437c478bd9Sstevel@tonic-gate break; 8447c478bd9Sstevel@tonic-gate 8457c478bd9Sstevel@tonic-gate off += dp->gfsd_nstatic + 2; 8467c478bd9Sstevel@tonic-gate next += dp->gfsd_nstatic + 2; 8477c478bd9Sstevel@tonic-gate 8487c478bd9Sstevel@tonic-gate if ((error = gfs_readdir_emit_int(&gstate, uiop, 8493f480432Smaybee next)) != 0) 8507c478bd9Sstevel@tonic-gate break; 8517c478bd9Sstevel@tonic-gate } else { 8527c478bd9Sstevel@tonic-gate /* 8537c478bd9Sstevel@tonic-gate * Offset is beyond the end of the static entries, and 8547c478bd9Sstevel@tonic-gate * we have no dynamic entries. Set EOF. 8557c478bd9Sstevel@tonic-gate */ 8567c478bd9Sstevel@tonic-gate eof = 1; 8577c478bd9Sstevel@tonic-gate } 8587c478bd9Sstevel@tonic-gate } 8597c478bd9Sstevel@tonic-gate 8607c478bd9Sstevel@tonic-gate return (gfs_readdir_fini(&gstate, error, eofp, eof)); 8617c478bd9Sstevel@tonic-gate } 8627c478bd9Sstevel@tonic-gate 8637c478bd9Sstevel@tonic-gate 8647c478bd9Sstevel@tonic-gate /* 8657c478bd9Sstevel@tonic-gate * gfs_vop_lookup: VOP_LOOKUP() entry point 8667c478bd9Sstevel@tonic-gate * 8677c478bd9Sstevel@tonic-gate * For use directly in vnode ops table. Given a GFS directory, calls 8687c478bd9Sstevel@tonic-gate * gfs_dir_lookup() as necessary. 8697c478bd9Sstevel@tonic-gate */ 8707c478bd9Sstevel@tonic-gate /* ARGSUSED */ 8717c478bd9Sstevel@tonic-gate int 8727c478bd9Sstevel@tonic-gate gfs_vop_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, pathname_t *pnp, 8737c478bd9Sstevel@tonic-gate int flags, vnode_t *rdir, cred_t *cr) 8747c478bd9Sstevel@tonic-gate { 8757c478bd9Sstevel@tonic-gate return (gfs_dir_lookup(dvp, nm, vpp)); 8767c478bd9Sstevel@tonic-gate } 8777c478bd9Sstevel@tonic-gate 8787c478bd9Sstevel@tonic-gate /* 8797c478bd9Sstevel@tonic-gate * gfs_vop_readdir: VOP_READDIR() entry point 8807c478bd9Sstevel@tonic-gate * 8817c478bd9Sstevel@tonic-gate * For use directly in vnode ops table. Given a GFS directory, calls 8827c478bd9Sstevel@tonic-gate * gfs_dir_readdir() as necessary. 8837c478bd9Sstevel@tonic-gate */ 8847c478bd9Sstevel@tonic-gate /* ARGSUSED */ 8857c478bd9Sstevel@tonic-gate int 8867c478bd9Sstevel@tonic-gate gfs_vop_readdir(vnode_t *vp, uio_t *uiop, cred_t *cr, int *eofp) 8877c478bd9Sstevel@tonic-gate { 8887c478bd9Sstevel@tonic-gate return (gfs_dir_readdir(vp, uiop, eofp, NULL)); 8897c478bd9Sstevel@tonic-gate } 8907c478bd9Sstevel@tonic-gate 8917c478bd9Sstevel@tonic-gate 8927c478bd9Sstevel@tonic-gate /* 8937c478bd9Sstevel@tonic-gate * gfs_vop_map: VOP_MAP() entry point 8947c478bd9Sstevel@tonic-gate * 8957c478bd9Sstevel@tonic-gate * Convenient routine for handling pseudo-files that wish to allow mmap() calls. 8967c478bd9Sstevel@tonic-gate * This function only works for readonly files, and uses the read function for 8977c478bd9Sstevel@tonic-gate * the vnode to fill in the data. The mapped data is immediately faulted in and 8987c478bd9Sstevel@tonic-gate * filled with the necessary data during this call; there are no getpage() or 8997c478bd9Sstevel@tonic-gate * putpage() routines. 9007c478bd9Sstevel@tonic-gate */ 9017c478bd9Sstevel@tonic-gate /* ARGSUSED */ 9027c478bd9Sstevel@tonic-gate int 9037c478bd9Sstevel@tonic-gate gfs_vop_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp, 9047c478bd9Sstevel@tonic-gate size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cred) 9057c478bd9Sstevel@tonic-gate { 9067c478bd9Sstevel@tonic-gate int rv; 9077c478bd9Sstevel@tonic-gate ssize_t resid = len; 9087c478bd9Sstevel@tonic-gate 9097c478bd9Sstevel@tonic-gate /* 9107c478bd9Sstevel@tonic-gate * Check for bad parameters 9117c478bd9Sstevel@tonic-gate */ 9127c478bd9Sstevel@tonic-gate #ifdef _ILP32 9137c478bd9Sstevel@tonic-gate if (len > MAXOFF_T) 9147c478bd9Sstevel@tonic-gate return (ENOMEM); 9157c478bd9Sstevel@tonic-gate #endif 9167c478bd9Sstevel@tonic-gate if (vp->v_flag & VNOMAP) 9177c478bd9Sstevel@tonic-gate return (ENOTSUP); 9187c478bd9Sstevel@tonic-gate if (off > MAXOFF_T) 9197c478bd9Sstevel@tonic-gate return (EFBIG); 9207c478bd9Sstevel@tonic-gate if ((long)off < 0 || (long)(off + len) < 0) 9217c478bd9Sstevel@tonic-gate return (EINVAL); 9227c478bd9Sstevel@tonic-gate if (vp->v_type != VREG) 9237c478bd9Sstevel@tonic-gate return (ENODEV); 9247c478bd9Sstevel@tonic-gate if ((prot & (PROT_EXEC | PROT_WRITE)) != 0) 9257c478bd9Sstevel@tonic-gate return (EACCES); 9267c478bd9Sstevel@tonic-gate 9277c478bd9Sstevel@tonic-gate /* 9287c478bd9Sstevel@tonic-gate * Find appropriate address if needed, otherwise clear address range. 9297c478bd9Sstevel@tonic-gate */ 9307c478bd9Sstevel@tonic-gate as_rangelock(as); 9317c478bd9Sstevel@tonic-gate if ((flags & MAP_FIXED) == 0) { 9327c478bd9Sstevel@tonic-gate map_addr(addrp, len, (offset_t)off, 1, flags); 9337c478bd9Sstevel@tonic-gate if (*addrp == NULL) { 9347c478bd9Sstevel@tonic-gate as_rangeunlock(as); 9357c478bd9Sstevel@tonic-gate return (ENOMEM); 9367c478bd9Sstevel@tonic-gate } 9377c478bd9Sstevel@tonic-gate } else { 9387c478bd9Sstevel@tonic-gate (void) as_unmap(as, *addrp, len); 9397c478bd9Sstevel@tonic-gate } 9407c478bd9Sstevel@tonic-gate 9417c478bd9Sstevel@tonic-gate /* 9427c478bd9Sstevel@tonic-gate * Create mapping 9437c478bd9Sstevel@tonic-gate */ 9447c478bd9Sstevel@tonic-gate rv = as_map(as, *addrp, len, segvn_create, zfod_argsp); 9457c478bd9Sstevel@tonic-gate as_rangeunlock(as); 9467c478bd9Sstevel@tonic-gate if (rv != 0) 9477c478bd9Sstevel@tonic-gate return (rv); 9487c478bd9Sstevel@tonic-gate 9497c478bd9Sstevel@tonic-gate /* 9507c478bd9Sstevel@tonic-gate * Fill with data from read() 9517c478bd9Sstevel@tonic-gate */ 9527c478bd9Sstevel@tonic-gate rv = vn_rdwr(UIO_READ, vp, *addrp, len, off, UIO_USERSPACE, 9537c478bd9Sstevel@tonic-gate 0, (rlim64_t)0, cred, &resid); 9547c478bd9Sstevel@tonic-gate 9557c478bd9Sstevel@tonic-gate if (rv == 0 && resid != 0) 9567c478bd9Sstevel@tonic-gate rv = ENXIO; 9577c478bd9Sstevel@tonic-gate 9587c478bd9Sstevel@tonic-gate if (rv != 0) { 9597c478bd9Sstevel@tonic-gate as_rangelock(as); 9607c478bd9Sstevel@tonic-gate (void) as_unmap(as, *addrp, len); 9617c478bd9Sstevel@tonic-gate as_rangeunlock(as); 9627c478bd9Sstevel@tonic-gate } 9637c478bd9Sstevel@tonic-gate 9647c478bd9Sstevel@tonic-gate return (rv); 9657c478bd9Sstevel@tonic-gate } 9667c478bd9Sstevel@tonic-gate 9677c478bd9Sstevel@tonic-gate /* 9687c478bd9Sstevel@tonic-gate * gfs_vop_inactive: VOP_INACTIVE() entry point 9697c478bd9Sstevel@tonic-gate * 9707c478bd9Sstevel@tonic-gate * Given a vnode that is a GFS file or directory, call gfs_file_inactive() or 9717c478bd9Sstevel@tonic-gate * gfs_dir_inactive() as necessary, and kmem_free()s associated private data. 9727c478bd9Sstevel@tonic-gate */ 9737c478bd9Sstevel@tonic-gate /* ARGSUSED */ 9747c478bd9Sstevel@tonic-gate void 9757c478bd9Sstevel@tonic-gate gfs_vop_inactive(vnode_t *vp, cred_t *cr) 9767c478bd9Sstevel@tonic-gate { 9777c478bd9Sstevel@tonic-gate gfs_file_t *fp = vp->v_data; 9787c478bd9Sstevel@tonic-gate void *data; 9797c478bd9Sstevel@tonic-gate 9807c478bd9Sstevel@tonic-gate if (fp->gfs_type == GFS_DIR) 9817c478bd9Sstevel@tonic-gate data = gfs_dir_inactive(vp); 9827c478bd9Sstevel@tonic-gate else 9837c478bd9Sstevel@tonic-gate data = gfs_file_inactive(vp); 9847c478bd9Sstevel@tonic-gate 9857c478bd9Sstevel@tonic-gate if (data != NULL) 9867c478bd9Sstevel@tonic-gate kmem_free(data, fp->gfs_size); 9877c478bd9Sstevel@tonic-gate } 988