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