12621Sllai1 /* 22621Sllai1 * CDDL HEADER START 32621Sllai1 * 42621Sllai1 * The contents of this file are subject to the terms of the 52621Sllai1 * Common Development and Distribution License (the "License"). 62621Sllai1 * You may not use this file except in compliance with the License. 72621Sllai1 * 82621Sllai1 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 92621Sllai1 * or http://www.opensolaris.org/os/licensing. 102621Sllai1 * See the License for the specific language governing permissions 112621Sllai1 * and limitations under the License. 122621Sllai1 * 132621Sllai1 * When distributing Covered Code, include this CDDL HEADER in each 142621Sllai1 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 152621Sllai1 * If applicable, add the following below this CDDL HEADER, with the 162621Sllai1 * fields enclosed by brackets "[]" replaced with your own identifying 172621Sllai1 * information: Portions Copyright [yyyy] [name of copyright owner] 182621Sllai1 * 192621Sllai1 * CDDL HEADER END 202621Sllai1 */ 212621Sllai1 /* 2210097SEric.Taylor@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 232621Sllai1 * Use is subject to license terms. 242621Sllai1 */ 252621Sllai1 262621Sllai1 /* 272621Sllai1 * utility routines for the /dev fs 282621Sllai1 */ 292621Sllai1 302621Sllai1 #include <sys/types.h> 312621Sllai1 #include <sys/param.h> 322621Sllai1 #include <sys/t_lock.h> 332621Sllai1 #include <sys/systm.h> 342621Sllai1 #include <sys/sysmacros.h> 352621Sllai1 #include <sys/user.h> 362621Sllai1 #include <sys/time.h> 372621Sllai1 #include <sys/vfs.h> 382621Sllai1 #include <sys/vnode.h> 392621Sllai1 #include <sys/file.h> 402621Sllai1 #include <sys/fcntl.h> 412621Sllai1 #include <sys/flock.h> 422621Sllai1 #include <sys/kmem.h> 432621Sllai1 #include <sys/uio.h> 442621Sllai1 #include <sys/errno.h> 452621Sllai1 #include <sys/stat.h> 462621Sllai1 #include <sys/cred.h> 472621Sllai1 #include <sys/dirent.h> 482621Sllai1 #include <sys/pathname.h> 492621Sllai1 #include <sys/cmn_err.h> 502621Sllai1 #include <sys/debug.h> 512621Sllai1 #include <sys/mode.h> 522621Sllai1 #include <sys/policy.h> 532621Sllai1 #include <fs/fs_subr.h> 542621Sllai1 #include <sys/mount.h> 552621Sllai1 #include <sys/fs/snode.h> 562621Sllai1 #include <sys/fs/dv_node.h> 572621Sllai1 #include <sys/fs/sdev_impl.h> 582621Sllai1 #include <sys/sunndi.h> 592621Sllai1 #include <sys/sunmdi.h> 602621Sllai1 #include <sys/conf.h> 612621Sllai1 #include <sys/proc.h> 622621Sllai1 #include <sys/user.h> 632621Sllai1 #include <sys/modctl.h> 642621Sllai1 652621Sllai1 #ifdef DEBUG 662621Sllai1 int sdev_debug = 0x00000001; 672621Sllai1 int sdev_debug_cache_flags = 0; 682621Sllai1 #endif 692621Sllai1 702621Sllai1 /* 712621Sllai1 * globals 722621Sllai1 */ 732621Sllai1 /* prototype memory vattrs */ 742621Sllai1 vattr_t sdev_vattr_dir = { 752621Sllai1 AT_TYPE|AT_MODE|AT_UID|AT_GID, /* va_mask */ 762621Sllai1 VDIR, /* va_type */ 772621Sllai1 SDEV_DIRMODE_DEFAULT, /* va_mode */ 782621Sllai1 SDEV_UID_DEFAULT, /* va_uid */ 792621Sllai1 SDEV_GID_DEFAULT, /* va_gid */ 802621Sllai1 0, /* va_fsid */ 812621Sllai1 0, /* va_nodeid */ 822621Sllai1 0, /* va_nlink */ 832621Sllai1 0, /* va_size */ 842621Sllai1 0, /* va_atime */ 852621Sllai1 0, /* va_mtime */ 862621Sllai1 0, /* va_ctime */ 872621Sllai1 0, /* va_rdev */ 882621Sllai1 0, /* va_blksize */ 892621Sllai1 0, /* va_nblocks */ 902621Sllai1 0 /* va_vcode */ 912621Sllai1 }; 922621Sllai1 932621Sllai1 vattr_t sdev_vattr_lnk = { 942621Sllai1 AT_TYPE|AT_MODE, /* va_mask */ 952621Sllai1 VLNK, /* va_type */ 962621Sllai1 SDEV_LNKMODE_DEFAULT, /* va_mode */ 972621Sllai1 SDEV_UID_DEFAULT, /* va_uid */ 982621Sllai1 SDEV_GID_DEFAULT, /* va_gid */ 992621Sllai1 0, /* va_fsid */ 1002621Sllai1 0, /* va_nodeid */ 1012621Sllai1 0, /* va_nlink */ 1022621Sllai1 0, /* va_size */ 1032621Sllai1 0, /* va_atime */ 1042621Sllai1 0, /* va_mtime */ 1052621Sllai1 0, /* va_ctime */ 1062621Sllai1 0, /* va_rdev */ 1072621Sllai1 0, /* va_blksize */ 1082621Sllai1 0, /* va_nblocks */ 1092621Sllai1 0 /* va_vcode */ 1102621Sllai1 }; 1112621Sllai1 1122621Sllai1 vattr_t sdev_vattr_blk = { 1132621Sllai1 AT_TYPE|AT_MODE|AT_UID|AT_GID, /* va_mask */ 1142621Sllai1 VBLK, /* va_type */ 1152621Sllai1 S_IFBLK | SDEV_DEVMODE_DEFAULT, /* va_mode */ 1162621Sllai1 SDEV_UID_DEFAULT, /* va_uid */ 1172621Sllai1 SDEV_GID_DEFAULT, /* va_gid */ 1182621Sllai1 0, /* va_fsid */ 1192621Sllai1 0, /* va_nodeid */ 1202621Sllai1 0, /* va_nlink */ 1212621Sllai1 0, /* va_size */ 1222621Sllai1 0, /* va_atime */ 1232621Sllai1 0, /* va_mtime */ 1242621Sllai1 0, /* va_ctime */ 1252621Sllai1 0, /* va_rdev */ 1262621Sllai1 0, /* va_blksize */ 1272621Sllai1 0, /* va_nblocks */ 1282621Sllai1 0 /* va_vcode */ 1292621Sllai1 }; 1302621Sllai1 1312621Sllai1 vattr_t sdev_vattr_chr = { 1322621Sllai1 AT_TYPE|AT_MODE|AT_UID|AT_GID, /* va_mask */ 1332621Sllai1 VCHR, /* va_type */ 1342621Sllai1 S_IFCHR | SDEV_DEVMODE_DEFAULT, /* va_mode */ 1352621Sllai1 SDEV_UID_DEFAULT, /* va_uid */ 1362621Sllai1 SDEV_GID_DEFAULT, /* va_gid */ 1372621Sllai1 0, /* va_fsid */ 1382621Sllai1 0, /* va_nodeid */ 1392621Sllai1 0, /* va_nlink */ 1402621Sllai1 0, /* va_size */ 1412621Sllai1 0, /* va_atime */ 1422621Sllai1 0, /* va_mtime */ 1432621Sllai1 0, /* va_ctime */ 1442621Sllai1 0, /* va_rdev */ 1452621Sllai1 0, /* va_blksize */ 1462621Sllai1 0, /* va_nblocks */ 1472621Sllai1 0 /* va_vcode */ 1482621Sllai1 }; 1492621Sllai1 1502621Sllai1 kmem_cache_t *sdev_node_cache; /* sdev_node cache */ 1512621Sllai1 int devtype; /* fstype */ 1522621Sllai1 1532621Sllai1 /* static */ 1542621Sllai1 static struct vnodeops *sdev_get_vop(struct sdev_node *); 155*10588SEric.Taylor@Sun.COM static void sdev_set_no_negcache(struct sdev_node *); 1562621Sllai1 static fs_operation_def_t *sdev_merge_vtab(const fs_operation_def_t []); 1572621Sllai1 static void sdev_free_vtab(fs_operation_def_t *); 1582621Sllai1 1592621Sllai1 static void 1602621Sllai1 sdev_prof_free(struct sdev_node *dv) 1612621Sllai1 { 1622621Sllai1 ASSERT(!SDEV_IS_GLOBAL(dv)); 1632621Sllai1 if (dv->sdev_prof.dev_name) 1642621Sllai1 nvlist_free(dv->sdev_prof.dev_name); 1652621Sllai1 if (dv->sdev_prof.dev_map) 1662621Sllai1 nvlist_free(dv->sdev_prof.dev_map); 1672621Sllai1 if (dv->sdev_prof.dev_symlink) 1682621Sllai1 nvlist_free(dv->sdev_prof.dev_symlink); 1692621Sllai1 if (dv->sdev_prof.dev_glob_incdir) 1702621Sllai1 nvlist_free(dv->sdev_prof.dev_glob_incdir); 1712621Sllai1 if (dv->sdev_prof.dev_glob_excdir) 1722621Sllai1 nvlist_free(dv->sdev_prof.dev_glob_excdir); 1732621Sllai1 bzero(&dv->sdev_prof, sizeof (dv->sdev_prof)); 1742621Sllai1 } 1752621Sllai1 1766712Stomee /* sdev_node cache constructor */ 1772621Sllai1 /*ARGSUSED1*/ 1782621Sllai1 static int 1792621Sllai1 i_sdev_node_ctor(void *buf, void *cfarg, int flag) 1802621Sllai1 { 1812621Sllai1 struct sdev_node *dv = (struct sdev_node *)buf; 1822621Sllai1 struct vnode *vp; 1832621Sllai1 1842621Sllai1 bzero(buf, sizeof (struct sdev_node)); 1856712Stomee vp = dv->sdev_vnode = vn_alloc(flag); 1866712Stomee if (vp == NULL) { 1876712Stomee return (-1); 1886712Stomee } 1896712Stomee vp->v_data = dv; 1902621Sllai1 rw_init(&dv->sdev_contents, NULL, RW_DEFAULT, NULL); 1912621Sllai1 return (0); 1922621Sllai1 } 1932621Sllai1 1946712Stomee /* sdev_node cache destructor */ 1952621Sllai1 /*ARGSUSED1*/ 1962621Sllai1 static void 1972621Sllai1 i_sdev_node_dtor(void *buf, void *arg) 1982621Sllai1 { 1992621Sllai1 struct sdev_node *dv = (struct sdev_node *)buf; 2002621Sllai1 struct vnode *vp = SDEVTOV(dv); 2012621Sllai1 2022621Sllai1 rw_destroy(&dv->sdev_contents); 2032621Sllai1 vn_free(vp); 2042621Sllai1 } 2052621Sllai1 2062621Sllai1 /* initialize sdev_node cache */ 2072621Sllai1 void 2082621Sllai1 sdev_node_cache_init() 2092621Sllai1 { 2102621Sllai1 int flags = 0; 2112621Sllai1 2122621Sllai1 #ifdef DEBUG 2132621Sllai1 flags = sdev_debug_cache_flags; 2142621Sllai1 if (flags) 2152621Sllai1 sdcmn_err(("cache debug flags 0x%x\n", flags)); 2162621Sllai1 #endif /* DEBUG */ 2172621Sllai1 2182621Sllai1 ASSERT(sdev_node_cache == NULL); 2192621Sllai1 sdev_node_cache = kmem_cache_create("sdev_node_cache", 2202621Sllai1 sizeof (struct sdev_node), 0, i_sdev_node_ctor, i_sdev_node_dtor, 2212621Sllai1 NULL, NULL, NULL, flags); 2222621Sllai1 } 2232621Sllai1 2242621Sllai1 /* destroy sdev_node cache */ 2252621Sllai1 void 2262621Sllai1 sdev_node_cache_fini() 2272621Sllai1 { 2282621Sllai1 ASSERT(sdev_node_cache != NULL); 2292621Sllai1 kmem_cache_destroy(sdev_node_cache); 2302621Sllai1 sdev_node_cache = NULL; 2312621Sllai1 } 2322621Sllai1 2336260Sjg /* 2346260Sjg * Compare two nodes lexographically to balance avl tree 2356260Sjg */ 2366260Sjg static int 2376260Sjg sdev_compare_nodes(const struct sdev_node *dv1, const struct sdev_node *dv2) 2386260Sjg { 2396260Sjg int rv; 2406260Sjg if ((rv = strcmp(dv1->sdev_name, dv2->sdev_name)) == 0) 2416260Sjg return (0); 2426260Sjg return ((rv < 0) ? -1 : 1); 2436260Sjg } 2446260Sjg 2452621Sllai1 void 2462621Sllai1 sdev_set_nodestate(struct sdev_node *dv, sdev_node_state_t state) 2472621Sllai1 { 2482621Sllai1 ASSERT(dv); 2492621Sllai1 ASSERT(RW_WRITE_HELD(&dv->sdev_contents)); 2502621Sllai1 dv->sdev_state = state; 2512621Sllai1 } 2522621Sllai1 2532621Sllai1 static void 2542621Sllai1 sdev_attrinit(struct sdev_node *dv, vattr_t *vap) 2552621Sllai1 { 2562621Sllai1 timestruc_t now; 2572621Sllai1 2582621Sllai1 ASSERT(vap); 2592621Sllai1 2602621Sllai1 dv->sdev_attr = kmem_zalloc(sizeof (struct vattr), KM_SLEEP); 2612621Sllai1 *dv->sdev_attr = *vap; 2622621Sllai1 2632621Sllai1 dv->sdev_attr->va_mode = MAKEIMODE(vap->va_type, vap->va_mode); 2642621Sllai1 2652621Sllai1 gethrestime(&now); 2662621Sllai1 dv->sdev_attr->va_atime = now; 2672621Sllai1 dv->sdev_attr->va_mtime = now; 2682621Sllai1 dv->sdev_attr->va_ctime = now; 2692621Sllai1 } 2702621Sllai1 2712621Sllai1 /* alloc and initialize a sdev_node */ 2722621Sllai1 int 2732621Sllai1 sdev_nodeinit(struct sdev_node *ddv, char *nm, struct sdev_node **newdv, 2742621Sllai1 vattr_t *vap) 2752621Sllai1 { 2762621Sllai1 struct sdev_node *dv = NULL; 2772621Sllai1 struct vnode *vp; 2782621Sllai1 size_t nmlen, len; 2792621Sllai1 devname_handle_t *dhl; 2802621Sllai1 2812621Sllai1 nmlen = strlen(nm) + 1; 2822621Sllai1 if (nmlen > MAXNAMELEN) { 2832621Sllai1 sdcmn_err9(("sdev_nodeinit: node name %s" 2842621Sllai1 " too long\n", nm)); 2852621Sllai1 *newdv = NULL; 2862621Sllai1 return (ENAMETOOLONG); 2872621Sllai1 } 2882621Sllai1 2892621Sllai1 dv = kmem_cache_alloc(sdev_node_cache, KM_SLEEP); 2902621Sllai1 2912621Sllai1 dv->sdev_name = kmem_alloc(nmlen, KM_SLEEP); 2922621Sllai1 bcopy(nm, dv->sdev_name, nmlen); 2932621Sllai1 dv->sdev_namelen = nmlen - 1; /* '\0' not included */ 2942621Sllai1 len = strlen(ddv->sdev_path) + strlen(nm) + 2; 2952621Sllai1 dv->sdev_path = kmem_alloc(len, KM_SLEEP); 2962621Sllai1 (void) snprintf(dv->sdev_path, len, "%s/%s", ddv->sdev_path, nm); 2972621Sllai1 /* overwritten for VLNK nodes */ 2982621Sllai1 dv->sdev_symlink = NULL; 2992621Sllai1 3002621Sllai1 vp = SDEVTOV(dv); 3012621Sllai1 vn_reinit(vp); 3022621Sllai1 vp->v_vfsp = SDEVTOV(ddv)->v_vfsp; 3032621Sllai1 if (vap) 3042621Sllai1 vp->v_type = vap->va_type; 3052621Sllai1 3062621Sllai1 /* 3072621Sllai1 * initialized to the parent's vnodeops. 3082621Sllai1 * maybe overwriten for a VDIR 3092621Sllai1 */ 3102621Sllai1 vn_setops(vp, vn_getops(SDEVTOV(ddv))); 3112621Sllai1 vn_exists(vp); 3122621Sllai1 3132621Sllai1 dv->sdev_dotdot = NULL; 3142621Sllai1 dv->sdev_attrvp = NULL; 3152621Sllai1 if (vap) { 3162621Sllai1 sdev_attrinit(dv, vap); 3172621Sllai1 } else { 3182621Sllai1 dv->sdev_attr = NULL; 3192621Sllai1 } 3202621Sllai1 3212621Sllai1 dv->sdev_ino = sdev_mkino(dv); 3222621Sllai1 dv->sdev_nlink = 0; /* updated on insert */ 3232621Sllai1 dv->sdev_flags = ddv->sdev_flags; /* inherit from the parent first */ 3242621Sllai1 dv->sdev_flags |= SDEV_BUILD; 3252621Sllai1 mutex_init(&dv->sdev_lookup_lock, NULL, MUTEX_DEFAULT, NULL); 3262621Sllai1 cv_init(&dv->sdev_lookup_cv, NULL, CV_DEFAULT, NULL); 3272621Sllai1 if (SDEV_IS_GLOBAL(ddv)) { 3282621Sllai1 dv->sdev_flags |= SDEV_GLOBAL; 3292621Sllai1 dhl = &(dv->sdev_handle); 3302621Sllai1 dhl->dh_data = dv; 3312621Sllai1 dhl->dh_args = NULL; 332*10588SEric.Taylor@Sun.COM sdev_set_no_negcache(dv); 3332621Sllai1 dv->sdev_gdir_gen = 0; 3342621Sllai1 } else { 3352621Sllai1 dv->sdev_flags &= ~SDEV_GLOBAL; 3362621Sllai1 dv->sdev_origin = NULL; /* set later */ 3372621Sllai1 bzero(&dv->sdev_prof, sizeof (dv->sdev_prof)); 3382621Sllai1 dv->sdev_ldir_gen = 0; 3392621Sllai1 dv->sdev_devtree_gen = 0; 3402621Sllai1 } 3412621Sllai1 3422621Sllai1 rw_enter(&dv->sdev_contents, RW_WRITER); 3432621Sllai1 sdev_set_nodestate(dv, SDEV_INIT); 3442621Sllai1 rw_exit(&dv->sdev_contents); 3452621Sllai1 *newdv = dv; 3462621Sllai1 3472621Sllai1 return (0); 3482621Sllai1 } 3492621Sllai1 3502621Sllai1 /* 3512621Sllai1 * transition a sdev_node into SDEV_READY state 3522621Sllai1 */ 3532621Sllai1 int 3542621Sllai1 sdev_nodeready(struct sdev_node *dv, struct vattr *vap, struct vnode *avp, 3552621Sllai1 void *args, struct cred *cred) 3562621Sllai1 { 3572621Sllai1 int error = 0; 3582621Sllai1 struct vnode *vp = SDEVTOV(dv); 3592621Sllai1 vtype_t type; 3602621Sllai1 3612621Sllai1 ASSERT(dv && (dv->sdev_state != SDEV_READY) && vap); 3622621Sllai1 3632621Sllai1 type = vap->va_type; 3642621Sllai1 vp->v_type = type; 3652621Sllai1 vp->v_rdev = vap->va_rdev; 3662621Sllai1 rw_enter(&dv->sdev_contents, RW_WRITER); 3672621Sllai1 if (type == VDIR) { 3682621Sllai1 dv->sdev_nlink = 2; 3692621Sllai1 dv->sdev_flags &= ~SDEV_PERSIST; 3702621Sllai1 dv->sdev_flags &= ~SDEV_DYNAMIC; 3712621Sllai1 vn_setops(vp, sdev_get_vop(dv)); /* from internal vtab */ 3722621Sllai1 ASSERT(dv->sdev_dotdot); 3732621Sllai1 ASSERT(SDEVTOV(dv->sdev_dotdot)->v_type == VDIR); 3742621Sllai1 vp->v_rdev = SDEVTOV(dv->sdev_dotdot)->v_rdev; 3756260Sjg avl_create(&dv->sdev_entries, 3766260Sjg (int (*)(const void *, const void *))sdev_compare_nodes, 3776260Sjg sizeof (struct sdev_node), 3786260Sjg offsetof(struct sdev_node, sdev_avllink)); 3792621Sllai1 } else if (type == VLNK) { 3802621Sllai1 ASSERT(args); 3812621Sllai1 dv->sdev_nlink = 1; 3822621Sllai1 dv->sdev_symlink = i_ddi_strdup((char *)args, KM_SLEEP); 3832621Sllai1 } else { 3842621Sllai1 dv->sdev_nlink = 1; 3852621Sllai1 } 3862621Sllai1 3872621Sllai1 if (!(SDEV_IS_GLOBAL(dv))) { 3882621Sllai1 dv->sdev_origin = (struct sdev_node *)args; 3892621Sllai1 dv->sdev_flags &= ~SDEV_PERSIST; 3902621Sllai1 } 3912621Sllai1 3922621Sllai1 /* 3932621Sllai1 * shadow node is created here OR 3942621Sllai1 * if failed (indicated by dv->sdev_attrvp == NULL), 3952621Sllai1 * created later in sdev_setattr 3962621Sllai1 */ 3972621Sllai1 if (avp) { 3982621Sllai1 dv->sdev_attrvp = avp; 3992621Sllai1 } else { 4002621Sllai1 if (dv->sdev_attr == NULL) 4012621Sllai1 sdev_attrinit(dv, vap); 4022621Sllai1 else 4032621Sllai1 *dv->sdev_attr = *vap; 4042621Sllai1 405*10588SEric.Taylor@Sun.COM if ((dv->sdev_attrvp == NULL) && SDEV_IS_PERSIST(dv)) 4062621Sllai1 error = sdev_shadow_node(dv, cred); 4072621Sllai1 } 4082621Sllai1 4096335Sjg if (error == 0) { 4106335Sjg /* transition to READY state */ 4116335Sjg sdev_set_nodestate(dv, SDEV_READY); 4126335Sjg sdev_nc_node_exists(dv); 4136335Sjg } else { 4146335Sjg sdev_set_nodestate(dv, SDEV_ZOMBIE); 4156335Sjg } 4162621Sllai1 rw_exit(&dv->sdev_contents); 4172621Sllai1 return (error); 4182621Sllai1 } 4192621Sllai1 4202621Sllai1 /* 4212621Sllai1 * setting ZOMBIE state 4222621Sllai1 */ 4232621Sllai1 static int 4242621Sllai1 sdev_nodezombied(struct sdev_node *dv) 4252621Sllai1 { 4262621Sllai1 rw_enter(&dv->sdev_contents, RW_WRITER); 4272621Sllai1 sdev_set_nodestate(dv, SDEV_ZOMBIE); 4282621Sllai1 rw_exit(&dv->sdev_contents); 4292621Sllai1 return (0); 4302621Sllai1 } 4312621Sllai1 4322621Sllai1 /* 4332621Sllai1 * Build the VROOT sdev_node. 4342621Sllai1 */ 4352621Sllai1 /*ARGSUSED*/ 4362621Sllai1 struct sdev_node * 4372621Sllai1 sdev_mkroot(struct vfs *vfsp, dev_t devdev, struct vnode *mvp, 4382621Sllai1 struct vnode *avp, struct cred *cred) 4392621Sllai1 { 4402621Sllai1 struct sdev_node *dv; 4412621Sllai1 struct vnode *vp; 4422621Sllai1 char devdir[] = "/dev"; 4432621Sllai1 4442621Sllai1 ASSERT(sdev_node_cache != NULL); 4452621Sllai1 ASSERT(avp); 4462621Sllai1 dv = kmem_cache_alloc(sdev_node_cache, KM_SLEEP); 4472621Sllai1 vp = SDEVTOV(dv); 4482621Sllai1 vn_reinit(vp); 4492621Sllai1 vp->v_flag |= VROOT; 4502621Sllai1 vp->v_vfsp = vfsp; 4512621Sllai1 vp->v_type = VDIR; 4522621Sllai1 vp->v_rdev = devdev; 4532621Sllai1 vn_setops(vp, sdev_vnodeops); /* apply the default vnodeops at /dev */ 4542621Sllai1 vn_exists(vp); 4552621Sllai1 4562621Sllai1 if (vfsp->vfs_mntpt) 4572621Sllai1 dv->sdev_name = i_ddi_strdup( 4582621Sllai1 (char *)refstr_value(vfsp->vfs_mntpt), KM_SLEEP); 4592621Sllai1 else 4602621Sllai1 /* vfs_mountdev1 set mount point later */ 4612621Sllai1 dv->sdev_name = i_ddi_strdup("/dev", KM_SLEEP); 4622621Sllai1 dv->sdev_namelen = strlen(dv->sdev_name); /* '\0' not included */ 4632621Sllai1 dv->sdev_path = i_ddi_strdup(devdir, KM_SLEEP); 4642621Sllai1 dv->sdev_ino = SDEV_ROOTINO; 4652621Sllai1 dv->sdev_nlink = 2; /* name + . (no sdev_insert) */ 4662621Sllai1 dv->sdev_dotdot = dv; /* .. == self */ 4672621Sllai1 dv->sdev_attrvp = avp; 4682621Sllai1 dv->sdev_attr = NULL; 4692621Sllai1 mutex_init(&dv->sdev_lookup_lock, NULL, MUTEX_DEFAULT, NULL); 4702621Sllai1 cv_init(&dv->sdev_lookup_cv, NULL, CV_DEFAULT, NULL); 4712621Sllai1 if (strcmp(dv->sdev_name, "/dev") == 0) { 4722621Sllai1 dv->sdev_flags = SDEV_BUILD|SDEV_GLOBAL|SDEV_PERSIST; 4732621Sllai1 bzero(&dv->sdev_handle, sizeof (dv->sdev_handle)); 4742621Sllai1 dv->sdev_gdir_gen = 0; 4752621Sllai1 } else { 4762621Sllai1 dv->sdev_flags = SDEV_BUILD; 4772621Sllai1 dv->sdev_flags &= ~SDEV_PERSIST; 4782621Sllai1 bzero(&dv->sdev_prof, sizeof (dv->sdev_prof)); 4792621Sllai1 dv->sdev_ldir_gen = 0; 4802621Sllai1 dv->sdev_devtree_gen = 0; 4812621Sllai1 } 4822621Sllai1 4836260Sjg avl_create(&dv->sdev_entries, 4846260Sjg (int (*)(const void *, const void *))sdev_compare_nodes, 4856260Sjg sizeof (struct sdev_node), 4866260Sjg offsetof(struct sdev_node, sdev_avllink)); 4876260Sjg 4882621Sllai1 rw_enter(&dv->sdev_contents, RW_WRITER); 4892621Sllai1 sdev_set_nodestate(dv, SDEV_READY); 4902621Sllai1 rw_exit(&dv->sdev_contents); 4912621Sllai1 sdev_nc_node_exists(dv); 4922621Sllai1 return (dv); 4932621Sllai1 } 4942621Sllai1 4952621Sllai1 /* directory dependent vop table */ 4962621Sllai1 struct sdev_vop_table { 4972621Sllai1 char *vt_name; /* subdirectory name */ 4982621Sllai1 const fs_operation_def_t *vt_service; /* vnodeops table */ 4992621Sllai1 struct vnodeops *vt_vops; /* constructed vop */ 5002621Sllai1 struct vnodeops **vt_global_vops; /* global container for vop */ 5012621Sllai1 int (*vt_vtor)(struct sdev_node *); /* validate sdev_node */ 5022621Sllai1 int vt_flags; 5032621Sllai1 }; 5042621Sllai1 5052621Sllai1 /* 5062621Sllai1 * A nice improvement would be to provide a plug-in mechanism 5072621Sllai1 * for this table instead of a const table. 5082621Sllai1 */ 5092621Sllai1 static struct sdev_vop_table vtab[] = 5102621Sllai1 { 5112621Sllai1 { "pts", devpts_vnodeops_tbl, NULL, &devpts_vnodeops, devpts_validate, 5122621Sllai1 SDEV_DYNAMIC | SDEV_VTOR }, 5132621Sllai1 5147688SAaron.Zang@Sun.COM { "vt", devvt_vnodeops_tbl, NULL, &devvt_vnodeops, devvt_validate, 5157688SAaron.Zang@Sun.COM SDEV_DYNAMIC | SDEV_VTOR }, 5167688SAaron.Zang@Sun.COM 517*10588SEric.Taylor@Sun.COM { "zvol", devzvol_vnodeops_tbl, NULL, &devzvol_vnodeops, 518*10588SEric.Taylor@Sun.COM devzvol_validate, SDEV_DYNAMIC | SDEV_VTOR | SDEV_SUBDIR }, 519*10588SEric.Taylor@Sun.COM 5202621Sllai1 { "zcons", NULL, NULL, NULL, NULL, SDEV_NO_NCACHE }, 5212621Sllai1 5225895Syz147064 { "net", devnet_vnodeops_tbl, NULL, &devnet_vnodeops, devnet_validate, 5235895Syz147064 SDEV_DYNAMIC | SDEV_VTOR }, 5245895Syz147064 5258023SPhil.Kirk@Sun.COM { "ipnet", devipnet_vnodeops_tbl, NULL, &devipnet_vnodeops, 5268023SPhil.Kirk@Sun.COM devipnet_validate, SDEV_DYNAMIC | SDEV_VTOR | SDEV_NO_NCACHE }, 5278023SPhil.Kirk@Sun.COM 5282621Sllai1 { NULL, NULL, NULL, NULL, NULL, 0} 5292621Sllai1 }; 5302621Sllai1 531*10588SEric.Taylor@Sun.COM struct sdev_vop_table * 532*10588SEric.Taylor@Sun.COM sdev_match(struct sdev_node *dv) 533*10588SEric.Taylor@Sun.COM { 534*10588SEric.Taylor@Sun.COM int vlen; 535*10588SEric.Taylor@Sun.COM int i; 536*10588SEric.Taylor@Sun.COM 537*10588SEric.Taylor@Sun.COM for (i = 0; vtab[i].vt_name; i++) { 538*10588SEric.Taylor@Sun.COM if (strcmp(vtab[i].vt_name, dv->sdev_name) == 0) 539*10588SEric.Taylor@Sun.COM return (&vtab[i]); 540*10588SEric.Taylor@Sun.COM if (vtab[i].vt_flags & SDEV_SUBDIR) { 541*10588SEric.Taylor@Sun.COM char *ptr; 542*10588SEric.Taylor@Sun.COM 543*10588SEric.Taylor@Sun.COM ASSERT(strlen(dv->sdev_path) > 5); 544*10588SEric.Taylor@Sun.COM ptr = dv->sdev_path + 5; 545*10588SEric.Taylor@Sun.COM vlen = strlen(vtab[i].vt_name); 546*10588SEric.Taylor@Sun.COM if ((strncmp(vtab[i].vt_name, ptr, 547*10588SEric.Taylor@Sun.COM vlen - 1) == 0) && ptr[vlen] == '/') 548*10588SEric.Taylor@Sun.COM return (&vtab[i]); 549*10588SEric.Taylor@Sun.COM } 550*10588SEric.Taylor@Sun.COM 551*10588SEric.Taylor@Sun.COM } 552*10588SEric.Taylor@Sun.COM return (NULL); 553*10588SEric.Taylor@Sun.COM } 5542621Sllai1 5552621Sllai1 /* 5562621Sllai1 * sets a directory's vnodeops if the directory is in the vtab; 5572621Sllai1 */ 5582621Sllai1 static struct vnodeops * 5592621Sllai1 sdev_get_vop(struct sdev_node *dv) 5602621Sllai1 { 561*10588SEric.Taylor@Sun.COM struct sdev_vop_table *vtp; 5622621Sllai1 char *path; 5632621Sllai1 5642621Sllai1 path = dv->sdev_path; 5652621Sllai1 ASSERT(path); 5662621Sllai1 5672621Sllai1 /* gets the relative path to /dev/ */ 5682621Sllai1 path += 5; 5692621Sllai1 570*10588SEric.Taylor@Sun.COM /* gets the vtab entry it matches */ 571*10588SEric.Taylor@Sun.COM if ((vtp = sdev_match(dv)) != NULL) { 572*10588SEric.Taylor@Sun.COM dv->sdev_flags |= vtp->vt_flags; 573*10588SEric.Taylor@Sun.COM 574*10588SEric.Taylor@Sun.COM if (vtp->vt_vops) { 575*10588SEric.Taylor@Sun.COM if (vtp->vt_global_vops) 576*10588SEric.Taylor@Sun.COM *(vtp->vt_global_vops) = vtp->vt_vops; 577*10588SEric.Taylor@Sun.COM return (vtp->vt_vops); 5782621Sllai1 } 5792621Sllai1 580*10588SEric.Taylor@Sun.COM if (vtp->vt_service) { 5812621Sllai1 fs_operation_def_t *templ; 582*10588SEric.Taylor@Sun.COM templ = sdev_merge_vtab(vtp->vt_service); 583*10588SEric.Taylor@Sun.COM if (vn_make_ops(vtp->vt_name, 5842621Sllai1 (const fs_operation_def_t *)templ, 585*10588SEric.Taylor@Sun.COM &vtp->vt_vops) != 0) { 5862621Sllai1 cmn_err(CE_PANIC, "%s: malformed vnode ops\n", 587*10588SEric.Taylor@Sun.COM vtp->vt_name); 5882621Sllai1 /*NOTREACHED*/ 5892621Sllai1 } 590*10588SEric.Taylor@Sun.COM if (vtp->vt_global_vops) { 591*10588SEric.Taylor@Sun.COM *(vtp->vt_global_vops) = vtp->vt_vops; 5922621Sllai1 } 5932621Sllai1 sdev_free_vtab(templ); 594*10588SEric.Taylor@Sun.COM return (vtp->vt_vops); 5952621Sllai1 } 5962621Sllai1 return (sdev_vnodeops); 5972621Sllai1 } 5982621Sllai1 5992621Sllai1 /* child inherits the persistence of the parent */ 6002621Sllai1 if (SDEV_IS_PERSIST(dv->sdev_dotdot)) 6012621Sllai1 dv->sdev_flags |= SDEV_PERSIST; 6022621Sllai1 6032621Sllai1 return (sdev_vnodeops); 6042621Sllai1 } 6052621Sllai1 6062621Sllai1 static void 607*10588SEric.Taylor@Sun.COM sdev_set_no_negcache(struct sdev_node *dv) 6082621Sllai1 { 6092621Sllai1 int i; 6102621Sllai1 char *path; 6112621Sllai1 6122621Sllai1 ASSERT(dv->sdev_path); 6132621Sllai1 path = dv->sdev_path + strlen("/dev/"); 6142621Sllai1 6152621Sllai1 for (i = 0; vtab[i].vt_name; i++) { 6162621Sllai1 if (strcmp(vtab[i].vt_name, path) == 0) { 6172621Sllai1 if (vtab[i].vt_flags & SDEV_NO_NCACHE) 6182621Sllai1 dv->sdev_flags |= SDEV_NO_NCACHE; 6192621Sllai1 break; 6202621Sllai1 } 6212621Sllai1 } 6222621Sllai1 } 6232621Sllai1 6242621Sllai1 void * 6252621Sllai1 sdev_get_vtor(struct sdev_node *dv) 6262621Sllai1 { 627*10588SEric.Taylor@Sun.COM struct sdev_vop_table *vtp; 628*10588SEric.Taylor@Sun.COM 629*10588SEric.Taylor@Sun.COM vtp = sdev_match(dv); 630*10588SEric.Taylor@Sun.COM if (vtp) 631*10588SEric.Taylor@Sun.COM return ((void *)vtp->vt_vtor); 632*10588SEric.Taylor@Sun.COM else 633*10588SEric.Taylor@Sun.COM return (NULL); 6342621Sllai1 } 6352621Sllai1 6362621Sllai1 /* 6372621Sllai1 * Build the base root inode 6382621Sllai1 */ 6392621Sllai1 ino_t 6402621Sllai1 sdev_mkino(struct sdev_node *dv) 6412621Sllai1 { 6422621Sllai1 ino_t ino; 6432621Sllai1 6442621Sllai1 /* 6452621Sllai1 * for now, follow the lead of tmpfs here 6462621Sllai1 * need to someday understand the requirements here 6472621Sllai1 */ 6482621Sllai1 ino = (ino_t)(uint32_t)((uintptr_t)dv >> 3); 6492621Sllai1 ino += SDEV_ROOTINO + 1; 6502621Sllai1 6512621Sllai1 return (ino); 6522621Sllai1 } 6532621Sllai1 654*10588SEric.Taylor@Sun.COM int 6552621Sllai1 sdev_getlink(struct vnode *linkvp, char **link) 6562621Sllai1 { 6572621Sllai1 int err; 6582621Sllai1 char *buf; 6592621Sllai1 struct uio uio = {0}; 6602621Sllai1 struct iovec iov = {0}; 6612621Sllai1 6622621Sllai1 if (linkvp == NULL) 6632621Sllai1 return (ENOENT); 6642621Sllai1 ASSERT(linkvp->v_type == VLNK); 6652621Sllai1 6662621Sllai1 buf = kmem_zalloc(MAXPATHLEN, KM_SLEEP); 6672621Sllai1 iov.iov_base = buf; 6682621Sllai1 iov.iov_len = MAXPATHLEN; 6692621Sllai1 uio.uio_iov = &iov; 6702621Sllai1 uio.uio_iovcnt = 1; 6712621Sllai1 uio.uio_resid = MAXPATHLEN; 6722621Sllai1 uio.uio_segflg = UIO_SYSSPACE; 6732621Sllai1 uio.uio_llimit = MAXOFFSET_T; 6742621Sllai1 6755331Samw err = VOP_READLINK(linkvp, &uio, kcred, NULL); 6762621Sllai1 if (err) { 6772621Sllai1 cmn_err(CE_WARN, "readlink %s failed in dev\n", buf); 6782621Sllai1 kmem_free(buf, MAXPATHLEN); 6792621Sllai1 return (ENOENT); 6802621Sllai1 } 6812621Sllai1 6822621Sllai1 /* mission complete */ 6832621Sllai1 *link = i_ddi_strdup(buf, KM_SLEEP); 6842621Sllai1 kmem_free(buf, MAXPATHLEN); 6852621Sllai1 return (0); 6862621Sllai1 } 6872621Sllai1 6882621Sllai1 /* 6892621Sllai1 * A convenient wrapper to get the devfs node vnode for a device 6902621Sllai1 * minor functionality: readlink() of a /dev symlink 6912621Sllai1 * Place the link into dv->sdev_symlink 6922621Sllai1 */ 6932621Sllai1 static int 6942621Sllai1 sdev_follow_link(struct sdev_node *dv) 6952621Sllai1 { 6962621Sllai1 int err; 6972621Sllai1 struct vnode *linkvp; 6982621Sllai1 char *link = NULL; 6992621Sllai1 7002621Sllai1 linkvp = SDEVTOV(dv); 7012621Sllai1 if (linkvp == NULL) 7022621Sllai1 return (ENOENT); 7032621Sllai1 ASSERT(linkvp->v_type == VLNK); 7042621Sllai1 err = sdev_getlink(linkvp, &link); 7052621Sllai1 if (err) { 7062621Sllai1 (void) sdev_nodezombied(dv); 7072621Sllai1 dv->sdev_symlink = NULL; 7082621Sllai1 return (ENOENT); 7092621Sllai1 } 7102621Sllai1 7112621Sllai1 ASSERT(link != NULL); 7122621Sllai1 dv->sdev_symlink = link; 7132621Sllai1 return (0); 7142621Sllai1 } 7152621Sllai1 7162621Sllai1 static int 7172621Sllai1 sdev_node_check(struct sdev_node *dv, struct vattr *nvap, void *nargs) 7182621Sllai1 { 7192621Sllai1 vtype_t otype = SDEVTOV(dv)->v_type; 7202621Sllai1 7212621Sllai1 /* 7222621Sllai1 * existing sdev_node has a different type. 7232621Sllai1 */ 7242621Sllai1 if (otype != nvap->va_type) { 7252621Sllai1 sdcmn_err9(("sdev_node_check: existing node " 7262621Sllai1 " %s type %d does not match new node type %d\n", 7272621Sllai1 dv->sdev_name, otype, nvap->va_type)); 7282621Sllai1 return (EEXIST); 7292621Sllai1 } 7302621Sllai1 7312621Sllai1 /* 7322621Sllai1 * For a symlink, the target should be the same. 7332621Sllai1 */ 7342621Sllai1 if (otype == VLNK) { 7352621Sllai1 ASSERT(nargs != NULL); 7362621Sllai1 ASSERT(dv->sdev_symlink != NULL); 7372621Sllai1 if (strcmp(dv->sdev_symlink, (char *)nargs) != 0) { 7382621Sllai1 sdcmn_err9(("sdev_node_check: existing node " 7392621Sllai1 " %s has different symlink %s as new node " 7402621Sllai1 " %s\n", dv->sdev_name, dv->sdev_symlink, 7412621Sllai1 (char *)nargs)); 7422621Sllai1 return (EEXIST); 7432621Sllai1 } 7442621Sllai1 } 7452621Sllai1 7462621Sllai1 return (0); 7472621Sllai1 } 7482621Sllai1 7492621Sllai1 /* 7502621Sllai1 * sdev_mknode - a wrapper for sdev_nodeinit(), sdev_nodeready() 7512621Sllai1 * 7522621Sllai1 * arguments: 7532621Sllai1 * - ddv (parent) 7542621Sllai1 * - nm (child name) 7552621Sllai1 * - newdv (sdev_node for nm is returned here) 7562621Sllai1 * - vap (vattr for the node to be created, va_type should be set. 7576335Sjg * - avp (attribute vnode) 7582621Sllai1 * the defaults should be used if unknown) 7592621Sllai1 * - cred 7602621Sllai1 * - args 7612621Sllai1 * . tnm (for VLNK) 7622621Sllai1 * . global sdev_node (for !SDEV_GLOBAL) 7632621Sllai1 * - state: SDEV_INIT, SDEV_READY 7642621Sllai1 * 7652621Sllai1 * only ddv, nm, newddv, vap, cred are required for sdev_mknode(SDEV_INIT) 7662621Sllai1 * 7672621Sllai1 * NOTE: directory contents writers lock needs to be held before 7682621Sllai1 * calling this routine. 7692621Sllai1 */ 7702621Sllai1 int 7712621Sllai1 sdev_mknode(struct sdev_node *ddv, char *nm, struct sdev_node **newdv, 7722621Sllai1 struct vattr *vap, struct vnode *avp, void *args, struct cred *cred, 7732621Sllai1 sdev_node_state_t state) 7742621Sllai1 { 7752621Sllai1 int error = 0; 7762621Sllai1 sdev_node_state_t node_state; 7772621Sllai1 struct sdev_node *dv = NULL; 7782621Sllai1 7792621Sllai1 ASSERT(state != SDEV_ZOMBIE); 7802621Sllai1 ASSERT(RW_WRITE_HELD(&ddv->sdev_contents)); 7812621Sllai1 7822621Sllai1 if (*newdv) { 7832621Sllai1 dv = *newdv; 7842621Sllai1 } else { 7852621Sllai1 /* allocate and initialize a sdev_node */ 7862621Sllai1 if (ddv->sdev_state == SDEV_ZOMBIE) { 7872621Sllai1 sdcmn_err9(("sdev_mknode: parent %s ZOMBIEd\n", 7882621Sllai1 ddv->sdev_path)); 7892621Sllai1 return (ENOENT); 7902621Sllai1 } 7912621Sllai1 7922621Sllai1 error = sdev_nodeinit(ddv, nm, &dv, vap); 7932621Sllai1 if (error != 0) { 7942621Sllai1 sdcmn_err9(("sdev_mknode: error %d," 7952621Sllai1 " name %s can not be initialized\n", 7962621Sllai1 error, nm)); 7976335Sjg return (error); 7982621Sllai1 } 7992621Sllai1 ASSERT(dv); 8002621Sllai1 8012621Sllai1 /* insert into the directory cache */ 8022621Sllai1 error = sdev_cache_update(ddv, &dv, nm, SDEV_CACHE_ADD); 8032621Sllai1 if (error) { 8042621Sllai1 sdcmn_err9(("sdev_mknode: node %s can not" 8052621Sllai1 " be added into directory cache\n", nm)); 8062621Sllai1 return (ENOENT); 8072621Sllai1 } 8082621Sllai1 } 8092621Sllai1 8102621Sllai1 ASSERT(dv); 8112621Sllai1 node_state = dv->sdev_state; 8122621Sllai1 ASSERT(node_state != SDEV_ZOMBIE); 8132621Sllai1 8142621Sllai1 if (state == SDEV_READY) { 8152621Sllai1 switch (node_state) { 8162621Sllai1 case SDEV_INIT: 8172621Sllai1 error = sdev_nodeready(dv, vap, avp, args, cred); 8182621Sllai1 if (error) { 8192621Sllai1 sdcmn_err9(("sdev_mknode: node %s can NOT" 8202621Sllai1 " be transitioned into READY state, " 8212621Sllai1 "error %d\n", nm, error)); 8222621Sllai1 } 8232621Sllai1 break; 8242621Sllai1 case SDEV_READY: 8252621Sllai1 /* 8262621Sllai1 * Do some sanity checking to make sure 8272621Sllai1 * the existing sdev_node is what has been 8282621Sllai1 * asked for. 8292621Sllai1 */ 8302621Sllai1 error = sdev_node_check(dv, vap, args); 8312621Sllai1 break; 8322621Sllai1 default: 8332621Sllai1 break; 8342621Sllai1 } 8352621Sllai1 } 8362621Sllai1 8372621Sllai1 if (!error) { 8382621Sllai1 *newdv = dv; 8392621Sllai1 ASSERT((*newdv)->sdev_state != SDEV_ZOMBIE); 8402621Sllai1 } else { 8412621Sllai1 SDEV_SIMPLE_RELE(dv); 8422621Sllai1 *newdv = NULL; 8432621Sllai1 } 8442621Sllai1 8452621Sllai1 return (error); 8462621Sllai1 } 8472621Sllai1 8482621Sllai1 /* 8496335Sjg * convenient wrapper to change vp's ATIME, CTIME and MTIME 8502621Sllai1 */ 8512621Sllai1 void 8522621Sllai1 sdev_update_timestamps(struct vnode *vp, cred_t *cred, uint_t mask) 8532621Sllai1 { 8542621Sllai1 struct vattr attr; 8552621Sllai1 timestruc_t now; 8562621Sllai1 int err; 8572621Sllai1 8582621Sllai1 ASSERT(vp); 8592621Sllai1 gethrestime(&now); 8602621Sllai1 if (mask & AT_CTIME) 8612621Sllai1 attr.va_ctime = now; 8622621Sllai1 if (mask & AT_MTIME) 8632621Sllai1 attr.va_mtime = now; 8642621Sllai1 if (mask & AT_ATIME) 8652621Sllai1 attr.va_atime = now; 8662621Sllai1 8672621Sllai1 attr.va_mask = (mask & AT_TIMES); 8682621Sllai1 err = VOP_SETATTR(vp, &attr, 0, cred, NULL); 8692621Sllai1 if (err && (err != EROFS)) { 8702621Sllai1 sdcmn_err(("update timestamps error %d\n", err)); 8712621Sllai1 } 8722621Sllai1 } 8732621Sllai1 8742621Sllai1 /* 8752621Sllai1 * the backing store vnode is released here 8762621Sllai1 */ 8772621Sllai1 /*ARGSUSED1*/ 8782621Sllai1 void 8792621Sllai1 sdev_nodedestroy(struct sdev_node *dv, uint_t flags) 8802621Sllai1 { 8812621Sllai1 /* no references */ 8822621Sllai1 ASSERT(dv->sdev_nlink == 0); 8832621Sllai1 8842621Sllai1 if (dv->sdev_attrvp != NULLVP) { 8852621Sllai1 VN_RELE(dv->sdev_attrvp); 8862621Sllai1 /* 8872621Sllai1 * reset the attrvp so that no more 8882621Sllai1 * references can be made on this already 8892621Sllai1 * vn_rele() vnode 8902621Sllai1 */ 8912621Sllai1 dv->sdev_attrvp = NULLVP; 8922621Sllai1 } 8932621Sllai1 8942621Sllai1 if (dv->sdev_attr != NULL) { 8952621Sllai1 kmem_free(dv->sdev_attr, sizeof (struct vattr)); 8962621Sllai1 dv->sdev_attr = NULL; 8972621Sllai1 } 8982621Sllai1 8992621Sllai1 if (dv->sdev_name != NULL) { 9002621Sllai1 kmem_free(dv->sdev_name, dv->sdev_namelen + 1); 9012621Sllai1 dv->sdev_name = NULL; 9022621Sllai1 } 9032621Sllai1 9042621Sllai1 if (dv->sdev_symlink != NULL) { 9052621Sllai1 kmem_free(dv->sdev_symlink, strlen(dv->sdev_symlink) + 1); 9062621Sllai1 dv->sdev_symlink = NULL; 9072621Sllai1 } 9082621Sllai1 9092621Sllai1 if (dv->sdev_path) { 9102621Sllai1 kmem_free(dv->sdev_path, strlen(dv->sdev_path) + 1); 9112621Sllai1 dv->sdev_path = NULL; 9122621Sllai1 } 9132621Sllai1 9142621Sllai1 if (!SDEV_IS_GLOBAL(dv)) 9152621Sllai1 sdev_prof_free(dv); 9162621Sllai1 9176260Sjg if (SDEVTOV(dv)->v_type == VDIR) { 9186260Sjg ASSERT(SDEV_FIRST_ENTRY(dv) == NULL); 9196260Sjg avl_destroy(&dv->sdev_entries); 9206260Sjg } 9216260Sjg 9222621Sllai1 mutex_destroy(&dv->sdev_lookup_lock); 9232621Sllai1 cv_destroy(&dv->sdev_lookup_cv); 9242621Sllai1 9252621Sllai1 /* return node to initial state as per constructor */ 9262621Sllai1 (void) memset((void *)&dv->sdev_instance_data, 0, 9272621Sllai1 sizeof (dv->sdev_instance_data)); 9282621Sllai1 vn_invalid(SDEVTOV(dv)); 9292621Sllai1 kmem_cache_free(sdev_node_cache, dv); 9302621Sllai1 } 9312621Sllai1 9322621Sllai1 /* 9332621Sllai1 * DIRECTORY CACHE lookup 9342621Sllai1 */ 9352621Sllai1 struct sdev_node * 9362621Sllai1 sdev_findbyname(struct sdev_node *ddv, char *nm) 9372621Sllai1 { 9382621Sllai1 struct sdev_node *dv; 9396260Sjg struct sdev_node dvtmp; 9406260Sjg avl_index_t where; 9412621Sllai1 9422621Sllai1 ASSERT(RW_LOCK_HELD(&ddv->sdev_contents)); 9436260Sjg 9446260Sjg dvtmp.sdev_name = nm; 9456260Sjg dv = avl_find(&ddv->sdev_entries, &dvtmp, &where); 9466260Sjg if (dv) { 9476260Sjg ASSERT(dv->sdev_dotdot == ddv); 9486260Sjg ASSERT(strcmp(dv->sdev_name, nm) == 0); 9496347Sjg SDEV_HOLD(dv); 9506347Sjg return (dv); 9512621Sllai1 } 9522621Sllai1 return (NULL); 9532621Sllai1 } 9542621Sllai1 9552621Sllai1 /* 9562621Sllai1 * Inserts a new sdev_node in a parent directory 9572621Sllai1 */ 9582621Sllai1 void 9592621Sllai1 sdev_direnter(struct sdev_node *ddv, struct sdev_node *dv) 9602621Sllai1 { 9616260Sjg avl_index_t where; 9626260Sjg 9632621Sllai1 ASSERT(RW_WRITE_HELD(&ddv->sdev_contents)); 9642621Sllai1 ASSERT(SDEVTOV(ddv)->v_type == VDIR); 9652621Sllai1 ASSERT(ddv->sdev_nlink >= 2); 9662621Sllai1 ASSERT(dv->sdev_nlink == 0); 9672621Sllai1 9682621Sllai1 dv->sdev_dotdot = ddv; 9696260Sjg VERIFY(avl_find(&ddv->sdev_entries, dv, &where) == NULL); 9706260Sjg avl_insert(&ddv->sdev_entries, dv, where); 9712621Sllai1 ddv->sdev_nlink++; 9722621Sllai1 } 9732621Sllai1 9742621Sllai1 /* 9752621Sllai1 * The following check is needed because while sdev_nodes are linked 9762621Sllai1 * in SDEV_INIT state, they have their link counts incremented only 9772621Sllai1 * in SDEV_READY state. 9782621Sllai1 */ 9792621Sllai1 static void 9802621Sllai1 decr_link(struct sdev_node *dv) 9812621Sllai1 { 9822621Sllai1 if (dv->sdev_state != SDEV_INIT) 9832621Sllai1 dv->sdev_nlink--; 9842621Sllai1 else 9852621Sllai1 ASSERT(dv->sdev_nlink == 0); 9862621Sllai1 } 9872621Sllai1 9882621Sllai1 /* 9892621Sllai1 * Delete an existing dv from directory cache 9902621Sllai1 * 9912621Sllai1 * In the case of a node is still held by non-zero reference count, 9922621Sllai1 * the node is put into ZOMBIE state. Once the reference count 9932621Sllai1 * reaches "0", the node is unlinked and destroyed, 9942621Sllai1 * in sdev_inactive(). 9952621Sllai1 */ 9962621Sllai1 static int 9972621Sllai1 sdev_dirdelete(struct sdev_node *ddv, struct sdev_node *dv) 9982621Sllai1 { 9992621Sllai1 struct vnode *vp; 10002621Sllai1 10012621Sllai1 ASSERT(RW_WRITE_HELD(&ddv->sdev_contents)); 10022621Sllai1 10032621Sllai1 vp = SDEVTOV(dv); 10042621Sllai1 mutex_enter(&vp->v_lock); 10052621Sllai1 10062621Sllai1 /* dv is held still */ 10072621Sllai1 if (vp->v_count > 1) { 10082621Sllai1 rw_enter(&dv->sdev_contents, RW_WRITER); 10092621Sllai1 if (dv->sdev_state == SDEV_READY) { 10102621Sllai1 sdcmn_err9(( 1011*10588SEric.Taylor@Sun.COM "sdev_dirdelete: node %s busy with count %d\n", 10122621Sllai1 dv->sdev_name, vp->v_count)); 10132621Sllai1 dv->sdev_state = SDEV_ZOMBIE; 10142621Sllai1 } 10152621Sllai1 rw_exit(&dv->sdev_contents); 10162621Sllai1 --vp->v_count; 10172621Sllai1 mutex_exit(&vp->v_lock); 10182621Sllai1 return (EBUSY); 10192621Sllai1 } 10202621Sllai1 ASSERT(vp->v_count == 1); 10212621Sllai1 10222621Sllai1 /* unlink from the memory cache */ 10232621Sllai1 ddv->sdev_nlink--; /* .. to above */ 10242621Sllai1 if (vp->v_type == VDIR) { 10252621Sllai1 decr_link(dv); /* . to self */ 10262621Sllai1 } 10272621Sllai1 10286260Sjg avl_remove(&ddv->sdev_entries, dv); 10292621Sllai1 decr_link(dv); /* name, back to zero */ 10302621Sllai1 vp->v_count--; 10312621Sllai1 mutex_exit(&vp->v_lock); 10322621Sllai1 10332621Sllai1 /* destroy the node */ 10342621Sllai1 sdev_nodedestroy(dv, 0); 10352621Sllai1 return (0); 10362621Sllai1 } 10372621Sllai1 10382621Sllai1 /* 10392621Sllai1 * check if the source is in the path of the target 10402621Sllai1 * 10412621Sllai1 * source and target are different 10422621Sllai1 */ 10432621Sllai1 /*ARGSUSED2*/ 10442621Sllai1 static int 10452621Sllai1 sdev_checkpath(struct sdev_node *sdv, struct sdev_node *tdv, struct cred *cred) 10462621Sllai1 { 10472621Sllai1 int error = 0; 10482621Sllai1 struct sdev_node *dotdot, *dir; 10492621Sllai1 10502621Sllai1 dotdot = tdv->sdev_dotdot; 10512621Sllai1 ASSERT(dotdot); 10522621Sllai1 10532621Sllai1 /* fs root */ 10542621Sllai1 if (dotdot == tdv) { 10552621Sllai1 return (0); 10562621Sllai1 } 10572621Sllai1 10582621Sllai1 for (;;) { 10592621Sllai1 /* 10602621Sllai1 * avoid error cases like 10612621Sllai1 * mv a a/b 10622621Sllai1 * mv a a/b/c 10632621Sllai1 * etc. 10642621Sllai1 */ 10652621Sllai1 if (dotdot == sdv) { 10662621Sllai1 error = EINVAL; 10672621Sllai1 break; 10682621Sllai1 } 10692621Sllai1 10702621Sllai1 dir = dotdot; 10712621Sllai1 dotdot = dir->sdev_dotdot; 10722621Sllai1 10732621Sllai1 /* done checking because root is reached */ 10742621Sllai1 if (dir == dotdot) { 10752621Sllai1 break; 10762621Sllai1 } 10772621Sllai1 } 10782621Sllai1 return (error); 10792621Sllai1 } 10802621Sllai1 10812621Sllai1 int 10822621Sllai1 sdev_rnmnode(struct sdev_node *oddv, struct sdev_node *odv, 10832621Sllai1 struct sdev_node *nddv, struct sdev_node **ndvp, char *nnm, 10842621Sllai1 struct cred *cred) 10852621Sllai1 { 10862621Sllai1 int error = 0; 10872621Sllai1 struct vnode *ovp = SDEVTOV(odv); 10882621Sllai1 struct vnode *nvp; 10892621Sllai1 struct vattr vattr; 10902621Sllai1 int doingdir = (ovp->v_type == VDIR); 10912621Sllai1 char *link = NULL; 10922729Sllai1 int samedir = (oddv == nddv) ? 1 : 0; 10932729Sllai1 int bkstore = 0; 10942729Sllai1 struct sdev_node *idv = NULL; 10952729Sllai1 struct sdev_node *ndv = NULL; 10962729Sllai1 timestruc_t now; 10972729Sllai1 10982729Sllai1 vattr.va_mask = AT_MODE|AT_UID|AT_GID; 10995331Samw error = VOP_GETATTR(ovp, &vattr, 0, cred, NULL); 11002729Sllai1 if (error) 11012729Sllai1 return (error); 11022729Sllai1 11032729Sllai1 if (!samedir) 11042729Sllai1 rw_enter(&oddv->sdev_contents, RW_WRITER); 11052729Sllai1 rw_enter(&nddv->sdev_contents, RW_WRITER); 11062729Sllai1 11072729Sllai1 /* 11082729Sllai1 * the source may have been deleted by another thread before 11092729Sllai1 * we gets here. 11102729Sllai1 */ 11112729Sllai1 if (odv->sdev_state != SDEV_READY) { 11122729Sllai1 error = ENOENT; 11132729Sllai1 goto err_out; 11142729Sllai1 } 11152729Sllai1 11162729Sllai1 if (doingdir && (odv == nddv)) { 11172729Sllai1 error = EINVAL; 11182729Sllai1 goto err_out; 11192729Sllai1 } 11202621Sllai1 11212621Sllai1 /* 11222621Sllai1 * If renaming a directory, and the parents are different (".." must be 11232621Sllai1 * changed) then the source dir must not be in the dir hierarchy above 11242621Sllai1 * the target since it would orphan everything below the source dir. 11252621Sllai1 */ 11262621Sllai1 if (doingdir && (oddv != nddv)) { 11272621Sllai1 error = sdev_checkpath(odv, nddv, cred); 11282621Sllai1 if (error) 11292729Sllai1 goto err_out; 11302621Sllai1 } 11312621Sllai1 11322729Sllai1 /* destination existing */ 11332621Sllai1 if (*ndvp) { 11342621Sllai1 nvp = SDEVTOV(*ndvp); 11352621Sllai1 ASSERT(nvp); 11362621Sllai1 11372621Sllai1 /* handling renaming to itself */ 11382729Sllai1 if (odv == *ndvp) { 11392729Sllai1 error = 0; 11402729Sllai1 goto err_out; 11412729Sllai1 } 11422729Sllai1 11432729Sllai1 if (nvp->v_type == VDIR) { 11442729Sllai1 if (!doingdir) { 11452729Sllai1 error = EISDIR; 11462729Sllai1 goto err_out; 11472729Sllai1 } 11482729Sllai1 11492729Sllai1 if (vn_vfswlock(nvp)) { 11502729Sllai1 error = EBUSY; 11512729Sllai1 goto err_out; 11522729Sllai1 } 11532729Sllai1 11542729Sllai1 if (vn_mountedvfs(nvp) != NULL) { 11552729Sllai1 vn_vfsunlock(nvp); 11562729Sllai1 error = EBUSY; 11572729Sllai1 goto err_out; 11582729Sllai1 } 11592729Sllai1 11602729Sllai1 /* in case dir1 exists in dir2 and "mv dir1 dir2" */ 11612729Sllai1 if ((*ndvp)->sdev_nlink > 2) { 11622729Sllai1 vn_vfsunlock(nvp); 11632729Sllai1 error = EEXIST; 11642729Sllai1 goto err_out; 11652729Sllai1 } 11662729Sllai1 vn_vfsunlock(nvp); 11672729Sllai1 11682729Sllai1 (void) sdev_dirdelete(nddv, *ndvp); 11692729Sllai1 *ndvp = NULL; 11706335Sjg ASSERT(nddv->sdev_attrvp); 11712729Sllai1 error = VOP_RMDIR(nddv->sdev_attrvp, nnm, 11726065Scth nddv->sdev_attrvp, cred, NULL, 0); 11732729Sllai1 if (error) 11742729Sllai1 goto err_out; 11752729Sllai1 } else { 11762729Sllai1 if (doingdir) { 11772729Sllai1 error = ENOTDIR; 11782729Sllai1 goto err_out; 11792729Sllai1 } 11802729Sllai1 11812729Sllai1 if (SDEV_IS_PERSIST((*ndvp))) { 11822729Sllai1 bkstore = 1; 11832729Sllai1 } 11842621Sllai1 11852621Sllai1 /* 11862729Sllai1 * get rid of the node from the directory cache 11872729Sllai1 * note, in case EBUSY is returned, the ZOMBIE 11882729Sllai1 * node is taken care in sdev_mknode. 11892621Sllai1 */ 11902729Sllai1 (void) sdev_dirdelete(nddv, *ndvp); 11912729Sllai1 *ndvp = NULL; 11922729Sllai1 if (bkstore) { 11936335Sjg ASSERT(nddv->sdev_attrvp); 11942729Sllai1 error = VOP_REMOVE(nddv->sdev_attrvp, 11955331Samw nnm, cred, NULL, 0); 11962729Sllai1 if (error) 11976065Scth goto err_out; 11982621Sllai1 } 11992621Sllai1 } 12002621Sllai1 } 12012621Sllai1 12022621Sllai1 /* fix the source for a symlink */ 12032621Sllai1 if (vattr.va_type == VLNK) { 12042621Sllai1 if (odv->sdev_symlink == NULL) { 12052621Sllai1 error = sdev_follow_link(odv); 12062729Sllai1 if (error) { 12072729Sllai1 error = ENOENT; 12082729Sllai1 goto err_out; 12092729Sllai1 } 12102621Sllai1 } 12112621Sllai1 ASSERT(odv->sdev_symlink); 12122621Sllai1 link = i_ddi_strdup(odv->sdev_symlink, KM_SLEEP); 12132621Sllai1 } 12142621Sllai1 12152729Sllai1 /* 12162729Sllai1 * make a fresh node from the source attrs 12172729Sllai1 */ 12182729Sllai1 ASSERT(RW_WRITE_HELD(&nddv->sdev_contents)); 12192729Sllai1 error = sdev_mknode(nddv, nnm, ndvp, &vattr, 12202729Sllai1 NULL, (void *)link, cred, SDEV_READY); 12212621Sllai1 12222621Sllai1 if (link) 12232621Sllai1 kmem_free(link, strlen(link) + 1); 12242621Sllai1 12252729Sllai1 if (error) 12262729Sllai1 goto err_out; 12272729Sllai1 ASSERT(*ndvp); 12282729Sllai1 ASSERT((*ndvp)->sdev_state == SDEV_READY); 12292729Sllai1 12302729Sllai1 /* move dir contents */ 12312729Sllai1 if (doingdir) { 12326260Sjg for (idv = SDEV_FIRST_ENTRY(odv); idv; 12336260Sjg idv = SDEV_NEXT_ENTRY(odv, idv)) { 12342729Sllai1 error = sdev_rnmnode(odv, idv, 12352729Sllai1 (struct sdev_node *)(*ndvp), &ndv, 12362729Sllai1 idv->sdev_name, cred); 12372729Sllai1 if (error) 12382729Sllai1 goto err_out; 12392729Sllai1 ndv = NULL; 12402729Sllai1 } 12412729Sllai1 } 12422729Sllai1 12432729Sllai1 if ((*ndvp)->sdev_attrvp) { 12442729Sllai1 sdev_update_timestamps((*ndvp)->sdev_attrvp, kcred, 12452729Sllai1 AT_CTIME|AT_ATIME); 12462729Sllai1 } else { 12472729Sllai1 ASSERT((*ndvp)->sdev_attr); 12482729Sllai1 gethrestime(&now); 12492729Sllai1 (*ndvp)->sdev_attr->va_ctime = now; 12502729Sllai1 (*ndvp)->sdev_attr->va_atime = now; 12512729Sllai1 } 12522729Sllai1 12532729Sllai1 if (nddv->sdev_attrvp) { 12542729Sllai1 sdev_update_timestamps(nddv->sdev_attrvp, kcred, 12552729Sllai1 AT_MTIME|AT_ATIME); 12562729Sllai1 } else { 12572729Sllai1 ASSERT(nddv->sdev_attr); 12582729Sllai1 gethrestime(&now); 12592729Sllai1 nddv->sdev_attr->va_mtime = now; 12602729Sllai1 nddv->sdev_attr->va_atime = now; 12612729Sllai1 } 12622729Sllai1 rw_exit(&nddv->sdev_contents); 12632729Sllai1 if (!samedir) 12642729Sllai1 rw_exit(&oddv->sdev_contents); 12652729Sllai1 12662621Sllai1 SDEV_RELE(*ndvp); 12672729Sllai1 return (error); 12682729Sllai1 12692729Sllai1 err_out: 12702729Sllai1 rw_exit(&nddv->sdev_contents); 12712729Sllai1 if (!samedir) 12722729Sllai1 rw_exit(&oddv->sdev_contents); 12732729Sllai1 return (error); 12742621Sllai1 } 12752621Sllai1 12762621Sllai1 /* 12772621Sllai1 * Merge sdev_node specific information into an attribute structure. 12782621Sllai1 * 12792621Sllai1 * note: sdev_node is not locked here 12802621Sllai1 */ 12812621Sllai1 void 12822621Sllai1 sdev_vattr_merge(struct sdev_node *dv, struct vattr *vap) 12832621Sllai1 { 12842621Sllai1 struct vnode *vp = SDEVTOV(dv); 12852621Sllai1 12862621Sllai1 vap->va_nlink = dv->sdev_nlink; 12872621Sllai1 vap->va_nodeid = dv->sdev_ino; 12882621Sllai1 vap->va_fsid = SDEVTOV(dv->sdev_dotdot)->v_rdev; 12892621Sllai1 vap->va_type = vp->v_type; 12902621Sllai1 12912621Sllai1 if (vp->v_type == VDIR) { 12922621Sllai1 vap->va_rdev = 0; 12932621Sllai1 vap->va_fsid = vp->v_rdev; 12942621Sllai1 } else if (vp->v_type == VLNK) { 12952621Sllai1 vap->va_rdev = 0; 12962621Sllai1 vap->va_mode &= ~S_IFMT; 12972621Sllai1 vap->va_mode |= S_IFLNK; 12982621Sllai1 } else if ((vp->v_type == VCHR) || (vp->v_type == VBLK)) { 12992621Sllai1 vap->va_rdev = vp->v_rdev; 13002621Sllai1 vap->va_mode &= ~S_IFMT; 13012621Sllai1 if (vap->va_type == VCHR) 13022621Sllai1 vap->va_mode |= S_IFCHR; 13032621Sllai1 else 13042621Sllai1 vap->va_mode |= S_IFBLK; 13052621Sllai1 } else { 13062621Sllai1 vap->va_rdev = 0; 13072621Sllai1 } 13082621Sllai1 } 13092621Sllai1 1310*10588SEric.Taylor@Sun.COM struct vattr * 13112621Sllai1 sdev_getdefault_attr(enum vtype type) 13122621Sllai1 { 13132621Sllai1 if (type == VDIR) 13142621Sllai1 return (&sdev_vattr_dir); 13152621Sllai1 else if (type == VCHR) 13162621Sllai1 return (&sdev_vattr_chr); 13172621Sllai1 else if (type == VBLK) 13182621Sllai1 return (&sdev_vattr_blk); 13192621Sllai1 else if (type == VLNK) 13202621Sllai1 return (&sdev_vattr_lnk); 13212621Sllai1 else 13222621Sllai1 return (NULL); 13232621Sllai1 } 13242621Sllai1 int 13252621Sllai1 sdev_to_vp(struct sdev_node *dv, struct vnode **vpp) 13262621Sllai1 { 13272621Sllai1 int rv = 0; 13282621Sllai1 struct vnode *vp = SDEVTOV(dv); 13292621Sllai1 13302621Sllai1 switch (vp->v_type) { 13312621Sllai1 case VCHR: 13322621Sllai1 case VBLK: 13332621Sllai1 /* 13342621Sllai1 * If vnode is a device, return special vnode instead 13352621Sllai1 * (though it knows all about -us- via sp->s_realvp) 13362621Sllai1 */ 13372621Sllai1 *vpp = specvp(vp, vp->v_rdev, vp->v_type, kcred); 13382621Sllai1 VN_RELE(vp); 13392621Sllai1 if (*vpp == NULLVP) 13402621Sllai1 rv = ENOSYS; 13412621Sllai1 break; 13422621Sllai1 default: /* most types are returned as is */ 13432621Sllai1 *vpp = vp; 13442621Sllai1 break; 13452621Sllai1 } 13462621Sllai1 return (rv); 13472621Sllai1 } 13482621Sllai1 13492621Sllai1 /* 13502621Sllai1 * junction between devname and root file system, e.g. ufs 13512621Sllai1 */ 13522621Sllai1 int 13532621Sllai1 devname_backstore_lookup(struct sdev_node *ddv, char *nm, struct vnode **rvp) 13542621Sllai1 { 13552621Sllai1 struct vnode *rdvp = ddv->sdev_attrvp; 13562621Sllai1 int rval = 0; 13572621Sllai1 13582621Sllai1 ASSERT(rdvp); 13592621Sllai1 13605331Samw rval = VOP_LOOKUP(rdvp, nm, rvp, NULL, 0, NULL, kcred, NULL, NULL, 13615331Samw NULL); 13622621Sllai1 return (rval); 13632621Sllai1 } 13642621Sllai1 13652621Sllai1 static int 13662621Sllai1 sdev_filldir_from_store(struct sdev_node *ddv, int dlen, struct cred *cred) 13672621Sllai1 { 13682621Sllai1 struct sdev_node *dv = NULL; 13692621Sllai1 char *nm; 13702621Sllai1 struct vnode *dirvp; 13712621Sllai1 int error; 13722621Sllai1 vnode_t *vp; 13732621Sllai1 int eof; 13742621Sllai1 struct iovec iov; 13752621Sllai1 struct uio uio; 13762621Sllai1 struct dirent64 *dp; 13772621Sllai1 dirent64_t *dbuf; 13782621Sllai1 size_t dbuflen; 13792621Sllai1 struct vattr vattr; 13802621Sllai1 char *link = NULL; 13812621Sllai1 13822621Sllai1 if (ddv->sdev_attrvp == NULL) 13832621Sllai1 return (0); 13842621Sllai1 if (!(ddv->sdev_flags & SDEV_BUILD)) 13852621Sllai1 return (0); 13862621Sllai1 13872621Sllai1 dirvp = ddv->sdev_attrvp; 13882621Sllai1 VN_HOLD(dirvp); 13892621Sllai1 dbuf = kmem_zalloc(dlen, KM_SLEEP); 13902621Sllai1 13912621Sllai1 uio.uio_iov = &iov; 13922621Sllai1 uio.uio_iovcnt = 1; 13932621Sllai1 uio.uio_segflg = UIO_SYSSPACE; 13942621Sllai1 uio.uio_fmode = 0; 13952621Sllai1 uio.uio_extflg = UIO_COPY_CACHED; 13962621Sllai1 uio.uio_loffset = 0; 13972621Sllai1 uio.uio_llimit = MAXOFFSET_T; 13982621Sllai1 13992621Sllai1 eof = 0; 14002621Sllai1 error = 0; 14012621Sllai1 while (!error && !eof) { 14022621Sllai1 uio.uio_resid = dlen; 14032621Sllai1 iov.iov_base = (char *)dbuf; 14042621Sllai1 iov.iov_len = dlen; 14052621Sllai1 (void) VOP_RWLOCK(dirvp, V_WRITELOCK_FALSE, NULL); 14065331Samw error = VOP_READDIR(dirvp, &uio, kcred, &eof, NULL, 0); 14072621Sllai1 VOP_RWUNLOCK(dirvp, V_WRITELOCK_FALSE, NULL); 14082621Sllai1 14092621Sllai1 dbuflen = dlen - uio.uio_resid; 14102621Sllai1 if (error || dbuflen == 0) 14112621Sllai1 break; 14122621Sllai1 1413*10588SEric.Taylor@Sun.COM if (!(ddv->sdev_flags & SDEV_BUILD)) 14142621Sllai1 break; 14152621Sllai1 14162621Sllai1 for (dp = dbuf; ((intptr_t)dp < 14172621Sllai1 (intptr_t)dbuf + dbuflen); 14182621Sllai1 dp = (dirent64_t *)((intptr_t)dp + dp->d_reclen)) { 14192621Sllai1 nm = dp->d_name; 14202621Sllai1 14212621Sllai1 if (strcmp(nm, ".") == 0 || 14222621Sllai1 strcmp(nm, "..") == 0) 14232621Sllai1 continue; 14242621Sllai1 14252621Sllai1 vp = NULLVP; 14262621Sllai1 dv = sdev_cache_lookup(ddv, nm); 14272621Sllai1 if (dv) { 14282621Sllai1 if (dv->sdev_state != SDEV_ZOMBIE) { 14292621Sllai1 SDEV_SIMPLE_RELE(dv); 14302621Sllai1 } else { 14312621Sllai1 /* 14322621Sllai1 * A ZOMBIE node may not have been 14332621Sllai1 * cleaned up from the backing store, 14342621Sllai1 * bypass this entry in this case, 14352621Sllai1 * and clean it up from the directory 14362621Sllai1 * cache if this is the last call. 14372621Sllai1 */ 14382621Sllai1 (void) sdev_dirdelete(ddv, dv); 14392621Sllai1 } 14402621Sllai1 continue; 14412621Sllai1 } 14422621Sllai1 14432621Sllai1 /* refill the cache if not already */ 14442621Sllai1 error = devname_backstore_lookup(ddv, nm, &vp); 14452621Sllai1 if (error) 14462621Sllai1 continue; 14472621Sllai1 14482621Sllai1 vattr.va_mask = AT_MODE|AT_UID|AT_GID; 14495331Samw error = VOP_GETATTR(vp, &vattr, 0, cred, NULL); 14502621Sllai1 if (error) 14512621Sllai1 continue; 14522621Sllai1 14532621Sllai1 if (vattr.va_type == VLNK) { 14542621Sllai1 error = sdev_getlink(vp, &link); 14552621Sllai1 if (error) { 14562621Sllai1 continue; 14572621Sllai1 } 14582621Sllai1 ASSERT(link != NULL); 14592621Sllai1 } 14602621Sllai1 14612621Sllai1 if (!rw_tryupgrade(&ddv->sdev_contents)) { 14622621Sllai1 rw_exit(&ddv->sdev_contents); 14632621Sllai1 rw_enter(&ddv->sdev_contents, RW_WRITER); 14642621Sllai1 } 14652621Sllai1 error = sdev_mknode(ddv, nm, &dv, &vattr, vp, link, 14662621Sllai1 cred, SDEV_READY); 14672621Sllai1 rw_downgrade(&ddv->sdev_contents); 14682621Sllai1 14692621Sllai1 if (link != NULL) { 14702621Sllai1 kmem_free(link, strlen(link) + 1); 14712621Sllai1 link = NULL; 14722621Sllai1 } 14732621Sllai1 14742621Sllai1 if (!error) { 14752621Sllai1 ASSERT(dv); 14762621Sllai1 ASSERT(dv->sdev_state != SDEV_ZOMBIE); 14772621Sllai1 SDEV_SIMPLE_RELE(dv); 14782621Sllai1 } 14792621Sllai1 vp = NULL; 14802621Sllai1 dv = NULL; 14812621Sllai1 } 14822621Sllai1 } 14832621Sllai1 14842621Sllai1 done: 14852621Sllai1 VN_RELE(dirvp); 14862621Sllai1 kmem_free(dbuf, dlen); 14872621Sllai1 14882621Sllai1 return (error); 14892621Sllai1 } 14902621Sllai1 14913843Sjg void 14922621Sllai1 sdev_filldir_dynamic(struct sdev_node *ddv) 14932621Sllai1 { 14942621Sllai1 int error; 14952621Sllai1 int i; 14962621Sllai1 struct vattr *vap; 14972621Sllai1 char *nm = NULL; 14982621Sllai1 struct sdev_node *dv = NULL; 14992621Sllai1 15003843Sjg ASSERT(RW_WRITE_HELD(&ddv->sdev_contents)); 15013843Sjg ASSERT((ddv->sdev_flags & SDEV_BUILD)); 15022621Sllai1 15032621Sllai1 vap = sdev_getdefault_attr(VDIR); 1504*10588SEric.Taylor@Sun.COM gethrestime(&vap->va_atime); 1505*10588SEric.Taylor@Sun.COM vap->va_mtime = vap->va_atime; 1506*10588SEric.Taylor@Sun.COM vap->va_ctime = vap->va_atime; 15072621Sllai1 for (i = 0; vtab[i].vt_name != NULL; i++) { 15082621Sllai1 nm = vtab[i].vt_name; 15092621Sllai1 ASSERT(RW_WRITE_HELD(&ddv->sdev_contents)); 15103843Sjg dv = NULL; 15112621Sllai1 error = sdev_mknode(ddv, nm, &dv, vap, NULL, 15122621Sllai1 NULL, kcred, SDEV_READY); 15133843Sjg if (error) { 15143843Sjg cmn_err(CE_WARN, "%s/%s: error %d\n", 15153843Sjg ddv->sdev_name, nm, error); 15163843Sjg } else { 15173843Sjg ASSERT(dv); 15183843Sjg ASSERT(dv->sdev_state != SDEV_ZOMBIE); 15193843Sjg SDEV_SIMPLE_RELE(dv); 15203843Sjg } 15212621Sllai1 } 15222621Sllai1 } 15232621Sllai1 15242621Sllai1 /* 15252621Sllai1 * Creating a backing store entry based on sdev_attr. 15262621Sllai1 * This is called either as part of node creation in a persistent directory 15272621Sllai1 * or from setattr/setsecattr to persist access attributes across reboot. 15282621Sllai1 */ 15292621Sllai1 int 15302621Sllai1 sdev_shadow_node(struct sdev_node *dv, struct cred *cred) 15312621Sllai1 { 15322621Sllai1 int error = 0; 15332621Sllai1 struct vnode *dvp = SDEVTOV(dv->sdev_dotdot); 15342621Sllai1 struct vnode *rdvp = VTOSDEV(dvp)->sdev_attrvp; 15352621Sllai1 struct vattr *vap = dv->sdev_attr; 15362621Sllai1 char *nm = dv->sdev_name; 15372621Sllai1 struct vnode *tmpvp, **rvp = &tmpvp, *rrvp = NULL; 15382621Sllai1 15392621Sllai1 ASSERT(dv && dv->sdev_name && rdvp); 15402621Sllai1 ASSERT(RW_WRITE_HELD(&dv->sdev_contents) && dv->sdev_attrvp == NULL); 15412621Sllai1 15422621Sllai1 lookup: 15432621Sllai1 /* try to find it in the backing store */ 15445331Samw error = VOP_LOOKUP(rdvp, nm, rvp, NULL, 0, NULL, cred, NULL, NULL, 15455331Samw NULL); 15462621Sllai1 if (error == 0) { 15475331Samw if (VOP_REALVP(*rvp, &rrvp, NULL) == 0) { 15482621Sllai1 VN_HOLD(rrvp); 15492621Sllai1 VN_RELE(*rvp); 15502621Sllai1 *rvp = rrvp; 15512621Sllai1 } 15522621Sllai1 15532621Sllai1 kmem_free(dv->sdev_attr, sizeof (vattr_t)); 15542621Sllai1 dv->sdev_attr = NULL; 15552621Sllai1 dv->sdev_attrvp = *rvp; 15562621Sllai1 return (0); 15572621Sllai1 } 15582621Sllai1 15592621Sllai1 /* let's try to persist the node */ 15602621Sllai1 gethrestime(&vap->va_atime); 15612621Sllai1 vap->va_mtime = vap->va_atime; 15622621Sllai1 vap->va_ctime = vap->va_atime; 15632621Sllai1 vap->va_mask |= AT_TYPE|AT_MODE; 15642621Sllai1 switch (vap->va_type) { 15652621Sllai1 case VDIR: 15665331Samw error = VOP_MKDIR(rdvp, nm, vap, rvp, cred, NULL, 0, NULL); 15672621Sllai1 sdcmn_err9(("sdev_shadow_node: mkdir vp %p error %d\n", 15682621Sllai1 (void *)(*rvp), error)); 15692621Sllai1 break; 15702621Sllai1 case VCHR: 15712621Sllai1 case VBLK: 15722621Sllai1 case VREG: 15732621Sllai1 case VDOOR: 15742621Sllai1 error = VOP_CREATE(rdvp, nm, vap, NONEXCL, VREAD|VWRITE, 15755331Samw rvp, cred, 0, NULL, NULL); 15762621Sllai1 sdcmn_err9(("sdev_shadow_node: create vp %p, error %d\n", 15772621Sllai1 (void *)(*rvp), error)); 15782621Sllai1 if (!error) 15792621Sllai1 VN_RELE(*rvp); 15802621Sllai1 break; 15812621Sllai1 case VLNK: 15822621Sllai1 ASSERT(dv->sdev_symlink); 15835331Samw error = VOP_SYMLINK(rdvp, nm, vap, dv->sdev_symlink, cred, 15845331Samw NULL, 0); 15852621Sllai1 sdcmn_err9(("sdev_shadow_node: create symlink error %d\n", 15862621Sllai1 error)); 15872621Sllai1 break; 15882621Sllai1 default: 15892621Sllai1 cmn_err(CE_PANIC, "dev: %s: sdev_shadow_node " 15902621Sllai1 "create\n", nm); 15912621Sllai1 /*NOTREACHED*/ 15922621Sllai1 } 15932621Sllai1 15942621Sllai1 /* go back to lookup to factor out spec node and set attrvp */ 15952621Sllai1 if (error == 0) 15962621Sllai1 goto lookup; 15972621Sllai1 15986335Sjg sdcmn_err(("cannot persist %s - error %d\n", dv->sdev_path, error)); 15992621Sllai1 return (error); 16002621Sllai1 } 16012621Sllai1 16022621Sllai1 static int 16032621Sllai1 sdev_cache_add(struct sdev_node *ddv, struct sdev_node **dv, char *nm) 16042621Sllai1 { 16052621Sllai1 int error = 0; 16062621Sllai1 struct sdev_node *dup = NULL; 16072621Sllai1 16082621Sllai1 ASSERT(RW_WRITE_HELD(&ddv->sdev_contents)); 16092621Sllai1 if ((dup = sdev_findbyname(ddv, nm)) == NULL) { 16102621Sllai1 sdev_direnter(ddv, *dv); 16112621Sllai1 } else { 16122621Sllai1 if (dup->sdev_state == SDEV_ZOMBIE) { 16132621Sllai1 error = sdev_dirdelete(ddv, dup); 16142621Sllai1 /* 16152621Sllai1 * The ZOMBIE node is still hanging 16162621Sllai1 * around with more than one reference counts. 16172621Sllai1 * Fail the new node creation so that 16182621Sllai1 * the directory cache won't have 16192621Sllai1 * duplicate entries for the same named node 16202621Sllai1 */ 16212621Sllai1 if (error == EBUSY) { 16222621Sllai1 SDEV_SIMPLE_RELE(*dv); 16232621Sllai1 sdev_nodedestroy(*dv, 0); 16242621Sllai1 *dv = NULL; 16252621Sllai1 return (error); 16262621Sllai1 } 16272621Sllai1 sdev_direnter(ddv, *dv); 16282621Sllai1 } else { 16292621Sllai1 ASSERT((*dv)->sdev_state != SDEV_ZOMBIE); 16302621Sllai1 SDEV_SIMPLE_RELE(*dv); 16312621Sllai1 sdev_nodedestroy(*dv, 0); 16322621Sllai1 *dv = dup; 16332621Sllai1 } 16342621Sllai1 } 16352621Sllai1 16362621Sllai1 return (0); 16372621Sllai1 } 16382621Sllai1 16392621Sllai1 static int 16402621Sllai1 sdev_cache_delete(struct sdev_node *ddv, struct sdev_node **dv) 16412621Sllai1 { 16422621Sllai1 ASSERT(RW_WRITE_HELD(&ddv->sdev_contents)); 16432621Sllai1 return (sdev_dirdelete(ddv, *dv)); 16442621Sllai1 } 16452621Sllai1 16462621Sllai1 /* 16472621Sllai1 * update the in-core directory cache 16482621Sllai1 */ 16492621Sllai1 int 16502621Sllai1 sdev_cache_update(struct sdev_node *ddv, struct sdev_node **dv, char *nm, 16512621Sllai1 sdev_cache_ops_t ops) 16522621Sllai1 { 16532621Sllai1 int error = 0; 16542621Sllai1 16552621Sllai1 ASSERT((SDEV_HELD(*dv))); 16562621Sllai1 16572621Sllai1 ASSERT(RW_WRITE_HELD(&ddv->sdev_contents)); 16582621Sllai1 switch (ops) { 16592621Sllai1 case SDEV_CACHE_ADD: 16602621Sllai1 error = sdev_cache_add(ddv, dv, nm); 16612621Sllai1 break; 16622621Sllai1 case SDEV_CACHE_DELETE: 16632621Sllai1 error = sdev_cache_delete(ddv, dv); 16642621Sllai1 break; 16652621Sllai1 default: 16662621Sllai1 break; 16672621Sllai1 } 16682621Sllai1 16692621Sllai1 return (error); 16702621Sllai1 } 16712621Sllai1 16722621Sllai1 /* 16735331Samw * retrieve the named entry from the directory cache 16742621Sllai1 */ 16752621Sllai1 struct sdev_node * 16762621Sllai1 sdev_cache_lookup(struct sdev_node *ddv, char *nm) 16772621Sllai1 { 16782621Sllai1 struct sdev_node *dv = NULL; 16792621Sllai1 16802621Sllai1 ASSERT(RW_LOCK_HELD(&ddv->sdev_contents)); 16812621Sllai1 dv = sdev_findbyname(ddv, nm); 16822621Sllai1 16832621Sllai1 return (dv); 16842621Sllai1 } 16852621Sllai1 16862621Sllai1 /* 16872621Sllai1 * Implicit reconfig for nodes constructed by a link generator 16882621Sllai1 * Start devfsadm if needed, or if devfsadm is in progress, 16892621Sllai1 * prepare to block on devfsadm either completing or 16902621Sllai1 * constructing the desired node. As devfsadmd is global 16912621Sllai1 * in scope, constructing all necessary nodes, we only 16922621Sllai1 * need to initiate it once. 16932621Sllai1 */ 16942621Sllai1 static int 16952621Sllai1 sdev_call_devfsadmd(struct sdev_node *ddv, struct sdev_node *dv, char *nm) 16962621Sllai1 { 16972621Sllai1 int error = 0; 16982621Sllai1 16992621Sllai1 if (DEVNAME_DEVFSADM_IS_RUNNING(devfsadm_state)) { 17002621Sllai1 sdcmn_err6(("lookup: waiting for %s/%s, 0x%x\n", 17012621Sllai1 ddv->sdev_name, nm, devfsadm_state)); 17022621Sllai1 mutex_enter(&dv->sdev_lookup_lock); 17032621Sllai1 SDEV_BLOCK_OTHERS(dv, (SDEV_LOOKUP | SDEV_LGWAITING)); 17042621Sllai1 mutex_exit(&dv->sdev_lookup_lock); 17052621Sllai1 error = 0; 17062621Sllai1 } else if (!DEVNAME_DEVFSADM_HAS_RUN(devfsadm_state)) { 17072621Sllai1 sdcmn_err6(("lookup %s/%s starting devfsadm, 0x%x\n", 17086065Scth ddv->sdev_name, nm, devfsadm_state)); 17092621Sllai1 17102621Sllai1 sdev_devfsadmd_thread(ddv, dv, kcred); 17112621Sllai1 mutex_enter(&dv->sdev_lookup_lock); 17122621Sllai1 SDEV_BLOCK_OTHERS(dv, 17132621Sllai1 (SDEV_LOOKUP | SDEV_LGWAITING)); 17142621Sllai1 mutex_exit(&dv->sdev_lookup_lock); 17152621Sllai1 error = 0; 17162621Sllai1 } else { 17172621Sllai1 error = -1; 17182621Sllai1 } 17192621Sllai1 17202621Sllai1 return (error); 17212621Sllai1 } 17222621Sllai1 17232621Sllai1 /* 17242621Sllai1 * Support for specialized device naming construction mechanisms 17252621Sllai1 */ 17262621Sllai1 static int 17272621Sllai1 sdev_call_dircallback(struct sdev_node *ddv, struct sdev_node **dvp, char *nm, 17282621Sllai1 int (*callback)(struct sdev_node *, char *, void **, struct cred *, 17292621Sllai1 void *, char *), int flags, struct cred *cred) 17302621Sllai1 { 17312621Sllai1 int rv = 0; 17322621Sllai1 char *physpath = NULL; 17332621Sllai1 struct vattr vattr; 17342621Sllai1 struct vattr *vap; 1735*10588SEric.Taylor@Sun.COM struct sdev_node *dv = NULL; 1736*10588SEric.Taylor@Sun.COM 1737*10588SEric.Taylor@Sun.COM ASSERT(RW_WRITE_HELD(&ddv->sdev_contents)); 1738*10588SEric.Taylor@Sun.COM if (flags & SDEV_VLINK) { 17397688SAaron.Zang@Sun.COM physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP); 17407688SAaron.Zang@Sun.COM rv = callback(ddv, nm, (void *)&physpath, kcred, NULL, 17417688SAaron.Zang@Sun.COM NULL); 17427688SAaron.Zang@Sun.COM if (rv) { 17437688SAaron.Zang@Sun.COM kmem_free(physpath, MAXPATHLEN); 17447688SAaron.Zang@Sun.COM return (-1); 17457688SAaron.Zang@Sun.COM } 17467688SAaron.Zang@Sun.COM 17477688SAaron.Zang@Sun.COM vap = sdev_getdefault_attr(VLNK); 17487688SAaron.Zang@Sun.COM vap->va_size = strlen(physpath); 1749*10588SEric.Taylor@Sun.COM gethrestime(&vap->va_atime); 1750*10588SEric.Taylor@Sun.COM vap->va_mtime = vap->va_atime; 1751*10588SEric.Taylor@Sun.COM vap->va_ctime = vap->va_atime; 1752*10588SEric.Taylor@Sun.COM 17537688SAaron.Zang@Sun.COM rv = sdev_mknode(ddv, nm, &dv, vap, NULL, 17547688SAaron.Zang@Sun.COM (void *)physpath, cred, SDEV_READY); 17557688SAaron.Zang@Sun.COM kmem_free(physpath, MAXPATHLEN); 17567688SAaron.Zang@Sun.COM if (rv) 17577688SAaron.Zang@Sun.COM return (rv); 17582621Sllai1 } else if (flags & SDEV_VATTR) { 17592621Sllai1 /* 17602621Sllai1 * /dev/pts 17612621Sllai1 * 17622621Sllai1 * callback is responsible to set the basic attributes, 17632621Sllai1 * e.g. va_type/va_uid/va_gid/ 17642621Sllai1 * dev_t if VCHR or VBLK/ 17652621Sllai1 */ 17662621Sllai1 ASSERT(callback); 17672621Sllai1 rv = callback(ddv, nm, (void *)&vattr, kcred, NULL, NULL); 17682621Sllai1 if (rv) { 17692621Sllai1 sdcmn_err3(("devname_lookup_func: SDEV_NONE " 17702621Sllai1 "callback failed \n")); 17712621Sllai1 return (-1); 17722621Sllai1 } 17732621Sllai1 17742621Sllai1 rv = sdev_mknode(ddv, nm, &dv, &vattr, NULL, NULL, 17752621Sllai1 cred, SDEV_READY); 17762621Sllai1 17772621Sllai1 if (rv) 17782621Sllai1 return (rv); 17792621Sllai1 17802621Sllai1 } else { 17812621Sllai1 impossible(("lookup: %s/%s by %s not supported (%d)\n", 17822621Sllai1 SDEVTOV(ddv)->v_path, nm, curproc->p_user.u_comm, 17832621Sllai1 __LINE__)); 17842621Sllai1 rv = -1; 17852621Sllai1 } 17862621Sllai1 17872621Sllai1 *dvp = dv; 17882621Sllai1 return (rv); 17892621Sllai1 } 17902621Sllai1 17912621Sllai1 static int 17922621Sllai1 is_devfsadm_thread(char *exec_name) 17932621Sllai1 { 17942621Sllai1 /* 17952621Sllai1 * note: because devfsadmd -> /usr/sbin/devfsadm 17962621Sllai1 * it is safe to use "devfsadm" to capture the lookups 17972621Sllai1 * from devfsadm and its daemon version. 17982621Sllai1 */ 17992621Sllai1 if (strcmp(exec_name, "devfsadm") == 0) 18002621Sllai1 return (1); 18012621Sllai1 return (0); 18022621Sllai1 } 18032621Sllai1 18042621Sllai1 /* 18052621Sllai1 * Lookup Order: 18062621Sllai1 * sdev_node cache; 18072621Sllai1 * backing store (SDEV_PERSIST); 18082621Sllai1 * DBNR: a. dir_ops implemented in the loadable modules; 18092621Sllai1 * b. vnode ops in vtab. 18102621Sllai1 */ 18112621Sllai1 int 18122621Sllai1 devname_lookup_func(struct sdev_node *ddv, char *nm, struct vnode **vpp, 18132621Sllai1 struct cred *cred, int (*callback)(struct sdev_node *, char *, void **, 18142621Sllai1 struct cred *, void *, char *), int flags) 18152621Sllai1 { 18162621Sllai1 int rv = 0, nmlen; 18172621Sllai1 struct vnode *rvp = NULL; 18182621Sllai1 struct sdev_node *dv = NULL; 18192621Sllai1 int retried = 0; 18202621Sllai1 int error = 0; 18212621Sllai1 struct vattr vattr; 18222621Sllai1 char *lookup_thread = curproc->p_user.u_comm; 18232621Sllai1 int failed_flags = 0; 18242621Sllai1 int (*vtor)(struct sdev_node *) = NULL; 18252621Sllai1 int state; 18262621Sllai1 int parent_state; 18272621Sllai1 char *link = NULL; 18282621Sllai1 18292621Sllai1 if (SDEVTOV(ddv)->v_type != VDIR) 18302621Sllai1 return (ENOTDIR); 18312621Sllai1 18322621Sllai1 /* 18332621Sllai1 * Empty name or ., return node itself. 18342621Sllai1 */ 18352621Sllai1 nmlen = strlen(nm); 18362621Sllai1 if ((nmlen == 0) || ((nmlen == 1) && (nm[0] == '.'))) { 18372621Sllai1 *vpp = SDEVTOV(ddv); 18382621Sllai1 VN_HOLD(*vpp); 18392621Sllai1 return (0); 18402621Sllai1 } 18412621Sllai1 18422621Sllai1 /* 18432621Sllai1 * .., return the parent directory 18442621Sllai1 */ 18452621Sllai1 if ((nmlen == 2) && (strcmp(nm, "..") == 0)) { 18462621Sllai1 *vpp = SDEVTOV(ddv->sdev_dotdot); 18472621Sllai1 VN_HOLD(*vpp); 18482621Sllai1 return (0); 18492621Sllai1 } 18502621Sllai1 18512621Sllai1 rw_enter(&ddv->sdev_contents, RW_READER); 18522621Sllai1 if (ddv->sdev_flags & SDEV_VTOR) { 18532621Sllai1 vtor = (int (*)(struct sdev_node *))sdev_get_vtor(ddv); 18542621Sllai1 ASSERT(vtor); 18552621Sllai1 } 18562621Sllai1 18572621Sllai1 tryagain: 18582621Sllai1 /* 18592621Sllai1 * (a) directory cache lookup: 18602621Sllai1 */ 18612621Sllai1 ASSERT(RW_READ_HELD(&ddv->sdev_contents)); 18622621Sllai1 parent_state = ddv->sdev_state; 18632621Sllai1 dv = sdev_cache_lookup(ddv, nm); 18642621Sllai1 if (dv) { 18652621Sllai1 state = dv->sdev_state; 18662621Sllai1 switch (state) { 18672621Sllai1 case SDEV_INIT: 18682621Sllai1 if (is_devfsadm_thread(lookup_thread)) 18692621Sllai1 break; 18702621Sllai1 18712621Sllai1 /* ZOMBIED parent won't allow node creation */ 18722621Sllai1 if (parent_state == SDEV_ZOMBIE) { 18732621Sllai1 SD_TRACE_FAILED_LOOKUP(ddv, nm, 18742621Sllai1 retried); 18752621Sllai1 goto nolock_notfound; 18762621Sllai1 } 18772621Sllai1 18782621Sllai1 mutex_enter(&dv->sdev_lookup_lock); 18792621Sllai1 /* compensate the threads started after devfsadm */ 18802621Sllai1 if (DEVNAME_DEVFSADM_IS_RUNNING(devfsadm_state) && 18812621Sllai1 !(SDEV_IS_LOOKUP(dv))) 18822621Sllai1 SDEV_BLOCK_OTHERS(dv, 18832621Sllai1 (SDEV_LOOKUP | SDEV_LGWAITING)); 18842621Sllai1 18852621Sllai1 if (SDEV_IS_LOOKUP(dv)) { 18862621Sllai1 failed_flags |= SLF_REBUILT; 18872621Sllai1 rw_exit(&ddv->sdev_contents); 18882621Sllai1 error = sdev_wait4lookup(dv, SDEV_LOOKUP); 18892621Sllai1 mutex_exit(&dv->sdev_lookup_lock); 18902621Sllai1 rw_enter(&ddv->sdev_contents, RW_READER); 18912621Sllai1 18922621Sllai1 if (error != 0) { 18932621Sllai1 SD_TRACE_FAILED_LOOKUP(ddv, nm, 18942621Sllai1 retried); 18952621Sllai1 goto nolock_notfound; 18962621Sllai1 } 18972621Sllai1 18982621Sllai1 state = dv->sdev_state; 18992621Sllai1 if (state == SDEV_INIT) { 19002621Sllai1 SD_TRACE_FAILED_LOOKUP(ddv, nm, 19012621Sllai1 retried); 19022621Sllai1 goto nolock_notfound; 19032621Sllai1 } else if (state == SDEV_READY) { 19042621Sllai1 goto found; 19052621Sllai1 } else if (state == SDEV_ZOMBIE) { 19062621Sllai1 rw_exit(&ddv->sdev_contents); 19072621Sllai1 SD_TRACE_FAILED_LOOKUP(ddv, nm, 19082621Sllai1 retried); 19092621Sllai1 SDEV_RELE(dv); 19102621Sllai1 goto lookup_failed; 19112621Sllai1 } 19122621Sllai1 } else { 19132621Sllai1 mutex_exit(&dv->sdev_lookup_lock); 19142621Sllai1 } 19152621Sllai1 break; 19162621Sllai1 case SDEV_READY: 19172621Sllai1 goto found; 19182621Sllai1 case SDEV_ZOMBIE: 19192621Sllai1 rw_exit(&ddv->sdev_contents); 19202621Sllai1 SD_TRACE_FAILED_LOOKUP(ddv, nm, retried); 19212621Sllai1 SDEV_RELE(dv); 19222621Sllai1 goto lookup_failed; 19232621Sllai1 default: 19242621Sllai1 rw_exit(&ddv->sdev_contents); 19252621Sllai1 SD_TRACE_FAILED_LOOKUP(ddv, nm, retried); 19262621Sllai1 sdev_lookup_failed(ddv, nm, failed_flags); 19272621Sllai1 *vpp = NULLVP; 19282621Sllai1 return (ENOENT); 19292621Sllai1 } 19302621Sllai1 } 19312621Sllai1 ASSERT(RW_READ_HELD(&ddv->sdev_contents)); 19322621Sllai1 19332621Sllai1 /* 19342621Sllai1 * ZOMBIED parent does not allow new node creation. 19352621Sllai1 * bail out early 19362621Sllai1 */ 19372621Sllai1 if (parent_state == SDEV_ZOMBIE) { 19382621Sllai1 rw_exit(&ddv->sdev_contents); 1939*10588SEric.Taylor@Sun.COM *vpp = NULLVP; 19402621Sllai1 SD_TRACE_FAILED_LOOKUP(ddv, nm, retried); 19412621Sllai1 return (ENOENT); 19422621Sllai1 } 19432621Sllai1 19442621Sllai1 /* 19452621Sllai1 * (b0): backing store lookup 19462621Sllai1 * SDEV_PERSIST is default except: 19472621Sllai1 * 1) pts nodes 19482621Sllai1 * 2) non-chmod'ed local nodes 1949*10588SEric.Taylor@Sun.COM * 3) zvol nodes 19502621Sllai1 */ 19512621Sllai1 if (SDEV_IS_PERSIST(ddv)) { 19522621Sllai1 error = devname_backstore_lookup(ddv, nm, &rvp); 19532621Sllai1 19542621Sllai1 if (!error) { 19552621Sllai1 19562621Sllai1 vattr.va_mask = AT_MODE|AT_UID|AT_GID; 19575331Samw error = VOP_GETATTR(rvp, &vattr, 0, cred, NULL); 19582621Sllai1 if (error) { 19592621Sllai1 rw_exit(&ddv->sdev_contents); 19602621Sllai1 if (dv) 19612621Sllai1 SDEV_RELE(dv); 19622621Sllai1 SD_TRACE_FAILED_LOOKUP(ddv, nm, retried); 19632621Sllai1 sdev_lookup_failed(ddv, nm, failed_flags); 19642621Sllai1 *vpp = NULLVP; 19652621Sllai1 return (ENOENT); 19662621Sllai1 } 19672621Sllai1 19682621Sllai1 if (vattr.va_type == VLNK) { 19692621Sllai1 error = sdev_getlink(rvp, &link); 19702621Sllai1 if (error) { 19712621Sllai1 rw_exit(&ddv->sdev_contents); 19722621Sllai1 if (dv) 19732621Sllai1 SDEV_RELE(dv); 19742621Sllai1 SD_TRACE_FAILED_LOOKUP(ddv, nm, 19752621Sllai1 retried); 19762621Sllai1 sdev_lookup_failed(ddv, nm, 19772621Sllai1 failed_flags); 19782621Sllai1 *vpp = NULLVP; 19792621Sllai1 return (ENOENT); 19802621Sllai1 } 19812621Sllai1 ASSERT(link != NULL); 19822621Sllai1 } 19832621Sllai1 19842621Sllai1 if (!rw_tryupgrade(&ddv->sdev_contents)) { 19852621Sllai1 rw_exit(&ddv->sdev_contents); 19862621Sllai1 rw_enter(&ddv->sdev_contents, RW_WRITER); 19872621Sllai1 } 19882621Sllai1 error = sdev_mknode(ddv, nm, &dv, &vattr, 19892621Sllai1 rvp, link, cred, SDEV_READY); 19902621Sllai1 rw_downgrade(&ddv->sdev_contents); 19912621Sllai1 19922621Sllai1 if (link != NULL) { 19932621Sllai1 kmem_free(link, strlen(link) + 1); 19942621Sllai1 link = NULL; 19952621Sllai1 } 19962621Sllai1 19972621Sllai1 if (error) { 19982621Sllai1 SD_TRACE_FAILED_LOOKUP(ddv, nm, retried); 19992621Sllai1 rw_exit(&ddv->sdev_contents); 20002621Sllai1 if (dv) 20012621Sllai1 SDEV_RELE(dv); 20022621Sllai1 goto lookup_failed; 20032621Sllai1 } else { 20042621Sllai1 goto found; 20052621Sllai1 } 20062621Sllai1 } else if (retried) { 20072621Sllai1 rw_exit(&ddv->sdev_contents); 20082621Sllai1 sdcmn_err3(("retry of lookup of %s/%s: failed\n", 20092621Sllai1 ddv->sdev_name, nm)); 20102621Sllai1 if (dv) 20112621Sllai1 SDEV_RELE(dv); 20122621Sllai1 SD_TRACE_FAILED_LOOKUP(ddv, nm, retried); 20132621Sllai1 sdev_lookup_failed(ddv, nm, failed_flags); 20142621Sllai1 *vpp = NULLVP; 20152621Sllai1 return (ENOENT); 20162621Sllai1 } 20172621Sllai1 } 20182621Sllai1 20198023SPhil.Kirk@Sun.COM lookup_create_node: 20202621Sllai1 /* first thread that is doing the lookup on this node */ 2021*10588SEric.Taylor@Sun.COM if (callback) { 2022*10588SEric.Taylor@Sun.COM ASSERT(dv == NULL); 2023*10588SEric.Taylor@Sun.COM if (!rw_tryupgrade(&ddv->sdev_contents)) { 2024*10588SEric.Taylor@Sun.COM rw_exit(&ddv->sdev_contents); 2025*10588SEric.Taylor@Sun.COM rw_enter(&ddv->sdev_contents, RW_WRITER); 2026*10588SEric.Taylor@Sun.COM } 2027*10588SEric.Taylor@Sun.COM error = sdev_call_dircallback(ddv, &dv, nm, callback, 2028*10588SEric.Taylor@Sun.COM flags, cred); 2029*10588SEric.Taylor@Sun.COM rw_downgrade(&ddv->sdev_contents); 2030*10588SEric.Taylor@Sun.COM if (error == 0) { 2031*10588SEric.Taylor@Sun.COM goto found; 2032*10588SEric.Taylor@Sun.COM } else { 2033*10588SEric.Taylor@Sun.COM SD_TRACE_FAILED_LOOKUP(ddv, nm, retried); 2034*10588SEric.Taylor@Sun.COM rw_exit(&ddv->sdev_contents); 2035*10588SEric.Taylor@Sun.COM goto lookup_failed; 2036*10588SEric.Taylor@Sun.COM } 2037*10588SEric.Taylor@Sun.COM } 20382621Sllai1 if (!dv) { 20392621Sllai1 if (!rw_tryupgrade(&ddv->sdev_contents)) { 20402621Sllai1 rw_exit(&ddv->sdev_contents); 20412621Sllai1 rw_enter(&ddv->sdev_contents, RW_WRITER); 20422621Sllai1 } 20432621Sllai1 error = sdev_mknode(ddv, nm, &dv, NULL, NULL, NULL, 20442621Sllai1 cred, SDEV_INIT); 20452621Sllai1 if (!dv) { 20462621Sllai1 rw_exit(&ddv->sdev_contents); 20472621Sllai1 SD_TRACE_FAILED_LOOKUP(ddv, nm, retried); 20482621Sllai1 sdev_lookup_failed(ddv, nm, failed_flags); 20492621Sllai1 *vpp = NULLVP; 20502621Sllai1 return (ENOENT); 20512621Sllai1 } 20522621Sllai1 rw_downgrade(&ddv->sdev_contents); 20532621Sllai1 } 20542621Sllai1 20552621Sllai1 /* 20562621Sllai1 * (b1) invoking devfsadm once per life time for devfsadm nodes 20572621Sllai1 */ 2058*10588SEric.Taylor@Sun.COM ASSERT(SDEV_HELD(dv)); 2059*10588SEric.Taylor@Sun.COM 2060*10588SEric.Taylor@Sun.COM if (SDEV_IS_NO_NCACHE(dv)) 2061*10588SEric.Taylor@Sun.COM failed_flags |= SLF_NO_NCACHE; 2062*10588SEric.Taylor@Sun.COM if (sdev_reconfig_boot || !i_ddi_io_initialized() || 2063*10588SEric.Taylor@Sun.COM SDEV_IS_DYNAMIC(ddv) || SDEV_IS_NO_NCACHE(dv) || 2064*10588SEric.Taylor@Sun.COM ((moddebug & MODDEBUG_FINI_EBUSY) != 0)) { 2065*10588SEric.Taylor@Sun.COM ASSERT(SDEV_HELD(dv)); 2066*10588SEric.Taylor@Sun.COM SD_TRACE_FAILED_LOOKUP(ddv, nm, retried); 2067*10588SEric.Taylor@Sun.COM goto nolock_notfound; 20682621Sllai1 } 20692621Sllai1 20702621Sllai1 /* 2071*10588SEric.Taylor@Sun.COM * filter out known non-existent devices recorded 2072*10588SEric.Taylor@Sun.COM * during initial reconfiguration boot for which 2073*10588SEric.Taylor@Sun.COM * reconfig should not be done and lookup may 2074*10588SEric.Taylor@Sun.COM * be short-circuited now. 20752621Sllai1 */ 2076*10588SEric.Taylor@Sun.COM if (sdev_lookup_filter(ddv, nm)) { 2077*10588SEric.Taylor@Sun.COM SD_TRACE_FAILED_LOOKUP(ddv, nm, retried); 2078*10588SEric.Taylor@Sun.COM goto nolock_notfound; 2079*10588SEric.Taylor@Sun.COM } 2080*10588SEric.Taylor@Sun.COM 2081*10588SEric.Taylor@Sun.COM /* bypassing devfsadm internal nodes */ 2082*10588SEric.Taylor@Sun.COM if (is_devfsadm_thread(lookup_thread)) { 2083*10588SEric.Taylor@Sun.COM SD_TRACE_FAILED_LOOKUP(ddv, nm, retried); 2084*10588SEric.Taylor@Sun.COM goto nolock_notfound; 2085*10588SEric.Taylor@Sun.COM } 2086*10588SEric.Taylor@Sun.COM 2087*10588SEric.Taylor@Sun.COM if (sdev_reconfig_disable) { 2088*10588SEric.Taylor@Sun.COM SD_TRACE_FAILED_LOOKUP(ddv, nm, retried); 2089*10588SEric.Taylor@Sun.COM goto nolock_notfound; 2090*10588SEric.Taylor@Sun.COM } 2091*10588SEric.Taylor@Sun.COM 2092*10588SEric.Taylor@Sun.COM error = sdev_call_devfsadmd(ddv, dv, nm); 2093*10588SEric.Taylor@Sun.COM if (error == 0) { 2094*10588SEric.Taylor@Sun.COM sdcmn_err8(("lookup of %s/%s by %s: reconfig\n", 2095*10588SEric.Taylor@Sun.COM ddv->sdev_name, nm, curproc->p_user.u_comm)); 2096*10588SEric.Taylor@Sun.COM if (sdev_reconfig_verbose) { 2097*10588SEric.Taylor@Sun.COM cmn_err(CE_CONT, 2098*10588SEric.Taylor@Sun.COM "?lookup of %s/%s by %s: reconfig\n", 2099*10588SEric.Taylor@Sun.COM ddv->sdev_name, nm, curproc->p_user.u_comm); 21002621Sllai1 } 2101*10588SEric.Taylor@Sun.COM retried = 1; 2102*10588SEric.Taylor@Sun.COM failed_flags |= SLF_REBUILT; 2103*10588SEric.Taylor@Sun.COM ASSERT(dv->sdev_state != SDEV_ZOMBIE); 2104*10588SEric.Taylor@Sun.COM SDEV_SIMPLE_RELE(dv); 2105*10588SEric.Taylor@Sun.COM goto tryagain; 2106*10588SEric.Taylor@Sun.COM } else { 2107*10588SEric.Taylor@Sun.COM SD_TRACE_FAILED_LOOKUP(ddv, nm, retried); 2108*10588SEric.Taylor@Sun.COM goto nolock_notfound; 21092621Sllai1 } 21102621Sllai1 21112621Sllai1 found: 21122621Sllai1 ASSERT(!(dv->sdev_flags & SDEV_STALE)); 21132621Sllai1 ASSERT(dv->sdev_state == SDEV_READY); 21142621Sllai1 if (vtor) { 21152621Sllai1 /* 21162621Sllai1 * Check validity of returned node 21172621Sllai1 */ 21182621Sllai1 switch (vtor(dv)) { 21192621Sllai1 case SDEV_VTOR_VALID: 21202621Sllai1 break; 21218023SPhil.Kirk@Sun.COM case SDEV_VTOR_STALE: 21228023SPhil.Kirk@Sun.COM /* 21238023SPhil.Kirk@Sun.COM * The name exists, but the cache entry is 21248023SPhil.Kirk@Sun.COM * stale and needs to be re-created. 21258023SPhil.Kirk@Sun.COM */ 21268023SPhil.Kirk@Sun.COM ASSERT(RW_READ_HELD(&ddv->sdev_contents)); 21278023SPhil.Kirk@Sun.COM if (rw_tryupgrade(&ddv->sdev_contents) == 0) { 21288023SPhil.Kirk@Sun.COM rw_exit(&ddv->sdev_contents); 21298023SPhil.Kirk@Sun.COM rw_enter(&ddv->sdev_contents, RW_WRITER); 21308023SPhil.Kirk@Sun.COM } 21318023SPhil.Kirk@Sun.COM error = sdev_cache_update(ddv, &dv, nm, 21328023SPhil.Kirk@Sun.COM SDEV_CACHE_DELETE); 21338023SPhil.Kirk@Sun.COM rw_downgrade(&ddv->sdev_contents); 21348023SPhil.Kirk@Sun.COM if (error == 0) { 21358023SPhil.Kirk@Sun.COM dv = NULL; 21368023SPhil.Kirk@Sun.COM goto lookup_create_node; 21378023SPhil.Kirk@Sun.COM } 21388023SPhil.Kirk@Sun.COM /* FALLTHRU */ 21392621Sllai1 case SDEV_VTOR_INVALID: 21402621Sllai1 SD_TRACE_FAILED_LOOKUP(ddv, nm, retried); 21412621Sllai1 sdcmn_err7(("lookup: destroy invalid " 21422621Sllai1 "node: %s(%p)\n", dv->sdev_name, (void *)dv)); 21432621Sllai1 goto nolock_notfound; 21442621Sllai1 case SDEV_VTOR_SKIP: 21452621Sllai1 sdcmn_err7(("lookup: node not applicable - " 21462621Sllai1 "skipping: %s(%p)\n", dv->sdev_name, (void *)dv)); 21472621Sllai1 rw_exit(&ddv->sdev_contents); 21482621Sllai1 SD_TRACE_FAILED_LOOKUP(ddv, nm, retried); 21492621Sllai1 SDEV_RELE(dv); 21502621Sllai1 goto lookup_failed; 21512621Sllai1 default: 21522621Sllai1 cmn_err(CE_PANIC, 21532621Sllai1 "dev fs: validator failed: %s(%p)\n", 21542621Sllai1 dv->sdev_name, (void *)dv); 21552621Sllai1 break; 21562621Sllai1 } 21572621Sllai1 } 21582621Sllai1 21592621Sllai1 rw_exit(&ddv->sdev_contents); 21602621Sllai1 rv = sdev_to_vp(dv, vpp); 21612621Sllai1 sdcmn_err3(("devname_lookup_func: returning vp %p v_count %d state %d " 21622621Sllai1 "for nm %s, error %d\n", (void *)*vpp, (*vpp)->v_count, 21632621Sllai1 dv->sdev_state, nm, rv)); 21642621Sllai1 return (rv); 21652621Sllai1 21662621Sllai1 nolock_notfound: 21672621Sllai1 /* 21682621Sllai1 * Destroy the node that is created for synchronization purposes. 21692621Sllai1 */ 21702621Sllai1 sdcmn_err3(("devname_lookup_func: %s with state %d\n", 21712621Sllai1 nm, dv->sdev_state)); 21722621Sllai1 ASSERT(RW_READ_HELD(&ddv->sdev_contents)); 21732621Sllai1 if (dv->sdev_state == SDEV_INIT) { 21742621Sllai1 if (!rw_tryupgrade(&ddv->sdev_contents)) { 21752621Sllai1 rw_exit(&ddv->sdev_contents); 21762621Sllai1 rw_enter(&ddv->sdev_contents, RW_WRITER); 21772621Sllai1 } 21782621Sllai1 21792621Sllai1 /* 21802621Sllai1 * Node state may have changed during the lock 21812621Sllai1 * changes. Re-check. 21822621Sllai1 */ 21832621Sllai1 if (dv->sdev_state == SDEV_INIT) { 21842621Sllai1 (void) sdev_dirdelete(ddv, dv); 21852621Sllai1 rw_exit(&ddv->sdev_contents); 21862621Sllai1 sdev_lookup_failed(ddv, nm, failed_flags); 21872621Sllai1 *vpp = NULL; 21882621Sllai1 return (ENOENT); 21892621Sllai1 } 21902621Sllai1 } 21912621Sllai1 21922621Sllai1 rw_exit(&ddv->sdev_contents); 21932621Sllai1 SDEV_RELE(dv); 21942621Sllai1 21952621Sllai1 lookup_failed: 21962621Sllai1 sdev_lookup_failed(ddv, nm, failed_flags); 21972621Sllai1 *vpp = NULL; 21982621Sllai1 return (ENOENT); 21992621Sllai1 } 22002621Sllai1 22012621Sllai1 /* 22022621Sllai1 * Given a directory node, mark all nodes beneath as 22032621Sllai1 * STALE, i.e. nodes that don't exist as far as new 22046347Sjg * consumers are concerned. Remove them from the 22056347Sjg * list of directory entries so that no lookup or 22066347Sjg * directory traversal will find them. The node 22076347Sjg * not deallocated so existing holds are not affected. 22082621Sllai1 */ 22092621Sllai1 void 22102621Sllai1 sdev_stale(struct sdev_node *ddv) 22112621Sllai1 { 22122621Sllai1 struct sdev_node *dv; 22132621Sllai1 struct vnode *vp; 22142621Sllai1 22152621Sllai1 ASSERT(SDEVTOV(ddv)->v_type == VDIR); 22162621Sllai1 22172621Sllai1 rw_enter(&ddv->sdev_contents, RW_WRITER); 22186260Sjg for (dv = SDEV_FIRST_ENTRY(ddv); dv; dv = SDEV_NEXT_ENTRY(ddv, dv)) { 22192621Sllai1 vp = SDEVTOV(dv); 22202621Sllai1 if (vp->v_type == VDIR) 22212621Sllai1 sdev_stale(dv); 22222621Sllai1 22232621Sllai1 sdcmn_err9(("sdev_stale: setting stale %s\n", 22246347Sjg dv->sdev_path)); 22252621Sllai1 dv->sdev_flags |= SDEV_STALE; 22266347Sjg avl_remove(&ddv->sdev_entries, dv); 22272621Sllai1 } 22282621Sllai1 ddv->sdev_flags |= SDEV_BUILD; 22292621Sllai1 rw_exit(&ddv->sdev_contents); 22302621Sllai1 } 22312621Sllai1 22322621Sllai1 /* 22332621Sllai1 * Given a directory node, clean out all the nodes beneath. 22342621Sllai1 * If expr is specified, clean node with names matching expr. 22352621Sllai1 * If SDEV_ENFORCE is specified in flags, busy nodes are made stale, 22362621Sllai1 * so they are excluded from future lookups. 22372621Sllai1 */ 22382621Sllai1 int 22392621Sllai1 sdev_cleandir(struct sdev_node *ddv, char *expr, uint_t flags) 22402621Sllai1 { 22412621Sllai1 int error = 0; 22422621Sllai1 int busy = 0; 22432621Sllai1 struct vnode *vp; 22442621Sllai1 struct sdev_node *dv, *next = NULL; 22452621Sllai1 int bkstore = 0; 22462621Sllai1 int len = 0; 22472621Sllai1 char *bks_name = NULL; 22482621Sllai1 22492621Sllai1 ASSERT(SDEVTOV(ddv)->v_type == VDIR); 22502621Sllai1 22512621Sllai1 /* 22522621Sllai1 * We try our best to destroy all unused sdev_node's 22532621Sllai1 */ 22542621Sllai1 rw_enter(&ddv->sdev_contents, RW_WRITER); 22556260Sjg for (dv = SDEV_FIRST_ENTRY(ddv); dv; dv = next) { 22566260Sjg next = SDEV_NEXT_ENTRY(ddv, dv); 22572621Sllai1 vp = SDEVTOV(dv); 22582621Sllai1 22592621Sllai1 if (expr && gmatch(dv->sdev_name, expr) == 0) 22602621Sllai1 continue; 22612621Sllai1 22622621Sllai1 if (vp->v_type == VDIR && 22632621Sllai1 sdev_cleandir(dv, NULL, flags) != 0) { 22642621Sllai1 sdcmn_err9(("sdev_cleandir: dir %s busy\n", 22652621Sllai1 dv->sdev_name)); 22662621Sllai1 busy++; 22672621Sllai1 continue; 22682621Sllai1 } 22692621Sllai1 22702621Sllai1 if (vp->v_count > 0 && (flags & SDEV_ENFORCE) == 0) { 22712621Sllai1 sdcmn_err9(("sdev_cleandir: dir %s busy\n", 22722621Sllai1 dv->sdev_name)); 22732621Sllai1 busy++; 22742621Sllai1 continue; 22752621Sllai1 } 22762621Sllai1 22772621Sllai1 /* 22782621Sllai1 * at this point, either dv is not held or SDEV_ENFORCE 22792621Sllai1 * is specified. In either case, dv needs to be deleted 22802621Sllai1 */ 22812621Sllai1 SDEV_HOLD(dv); 22822621Sllai1 22832621Sllai1 bkstore = SDEV_IS_PERSIST(dv) ? 1 : 0; 22842621Sllai1 if (bkstore && (vp->v_type == VDIR)) 22852621Sllai1 bkstore += 1; 22862621Sllai1 22872621Sllai1 if (bkstore) { 22882621Sllai1 len = strlen(dv->sdev_name) + 1; 22892621Sllai1 bks_name = kmem_alloc(len, KM_SLEEP); 22902621Sllai1 bcopy(dv->sdev_name, bks_name, len); 22912621Sllai1 } 22922621Sllai1 22932621Sllai1 error = sdev_dirdelete(ddv, dv); 22942621Sllai1 22952621Sllai1 if (error == EBUSY) { 22962621Sllai1 sdcmn_err9(("sdev_cleandir: dir busy\n")); 22972621Sllai1 busy++; 22982621Sllai1 } 22992621Sllai1 23002621Sllai1 /* take care the backing store clean up */ 23012621Sllai1 if (bkstore && (error == 0)) { 23022621Sllai1 ASSERT(bks_name); 23032621Sllai1 ASSERT(ddv->sdev_attrvp); 23042621Sllai1 23052621Sllai1 if (bkstore == 1) { 23062621Sllai1 error = VOP_REMOVE(ddv->sdev_attrvp, 23075331Samw bks_name, kcred, NULL, 0); 23082621Sllai1 } else if (bkstore == 2) { 23092621Sllai1 error = VOP_RMDIR(ddv->sdev_attrvp, 23105331Samw bks_name, ddv->sdev_attrvp, kcred, NULL, 0); 23112621Sllai1 } 23122621Sllai1 23132621Sllai1 /* do not propagate the backing store errors */ 23142621Sllai1 if (error) { 23152621Sllai1 sdcmn_err9(("sdev_cleandir: backing store" 23162621Sllai1 "not cleaned\n")); 23172621Sllai1 error = 0; 23182621Sllai1 } 23192621Sllai1 23202621Sllai1 bkstore = 0; 23212621Sllai1 kmem_free(bks_name, len); 23222621Sllai1 bks_name = NULL; 23232621Sllai1 len = 0; 23242621Sllai1 } 23252621Sllai1 } 23262621Sllai1 23272621Sllai1 ddv->sdev_flags |= SDEV_BUILD; 23282621Sllai1 rw_exit(&ddv->sdev_contents); 23292621Sllai1 23302621Sllai1 if (busy) { 23312621Sllai1 error = EBUSY; 23322621Sllai1 } 23332621Sllai1 23342621Sllai1 return (error); 23352621Sllai1 } 23362621Sllai1 23372621Sllai1 /* 23382621Sllai1 * a convenient wrapper for readdir() funcs 23392621Sllai1 */ 23402621Sllai1 size_t 23412621Sllai1 add_dir_entry(dirent64_t *de, char *nm, size_t size, ino_t ino, offset_t off) 23422621Sllai1 { 23432621Sllai1 size_t reclen = DIRENT64_RECLEN(strlen(nm)); 23442621Sllai1 if (reclen > size) 23452621Sllai1 return (0); 23462621Sllai1 23472621Sllai1 de->d_ino = (ino64_t)ino; 23482621Sllai1 de->d_off = (off64_t)off + 1; 23492621Sllai1 de->d_reclen = (ushort_t)reclen; 23502621Sllai1 (void) strncpy(de->d_name, nm, DIRENT64_NAMELEN(reclen)); 23512621Sllai1 return (reclen); 23522621Sllai1 } 23532621Sllai1 23542621Sllai1 /* 23552621Sllai1 * sdev_mount service routines 23562621Sllai1 */ 23572621Sllai1 int 23582621Sllai1 sdev_copyin_mountargs(struct mounta *uap, struct sdev_mountargs *args) 23592621Sllai1 { 23602621Sllai1 int error; 23612621Sllai1 23622621Sllai1 if (uap->datalen != sizeof (*args)) 23632621Sllai1 return (EINVAL); 23642621Sllai1 23652621Sllai1 if (error = copyin(uap->dataptr, args, sizeof (*args))) { 23662621Sllai1 cmn_err(CE_WARN, "sdev_copyin_mountargs: can not" 23672621Sllai1 "get user data. error %d\n", error); 23682621Sllai1 return (EFAULT); 23692621Sllai1 } 23702621Sllai1 23712621Sllai1 return (0); 23722621Sllai1 } 23732621Sllai1 23742621Sllai1 #ifdef nextdp 23752621Sllai1 #undef nextdp 23762621Sllai1 #endif 23773133Sjg #define nextdp(dp) ((struct dirent64 *) \ 23783133Sjg (intptr_t)((char *)(dp) + (dp)->d_reclen)) 23792621Sllai1 23802621Sllai1 /* 23812621Sllai1 * readdir helper func 23822621Sllai1 */ 23832621Sllai1 int 23842621Sllai1 devname_readdir_func(vnode_t *vp, uio_t *uiop, cred_t *cred, int *eofp, 23852621Sllai1 int flags) 23862621Sllai1 { 23872621Sllai1 struct sdev_node *ddv = VTOSDEV(vp); 23882621Sllai1 struct sdev_node *dv; 23892621Sllai1 dirent64_t *dp; 23902621Sllai1 ulong_t outcount = 0; 23912621Sllai1 size_t namelen; 23922621Sllai1 ulong_t alloc_count; 23932621Sllai1 void *outbuf; 23942621Sllai1 struct iovec *iovp; 23952621Sllai1 int error = 0; 23962621Sllai1 size_t reclen; 23972621Sllai1 offset_t diroff; 23982621Sllai1 offset_t soff; 23992621Sllai1 int this_reclen; 24002621Sllai1 int (*vtor)(struct sdev_node *) = NULL; 24012621Sllai1 struct vattr attr; 24022621Sllai1 timestruc_t now; 24032621Sllai1 24042621Sllai1 ASSERT(ddv->sdev_attr || ddv->sdev_attrvp); 24052621Sllai1 ASSERT(RW_READ_HELD(&ddv->sdev_contents)); 24062621Sllai1 24072621Sllai1 if (uiop->uio_loffset >= MAXOFF_T) { 24082621Sllai1 if (eofp) 24092621Sllai1 *eofp = 1; 24102621Sllai1 return (0); 24112621Sllai1 } 24122621Sllai1 24132621Sllai1 if (uiop->uio_iovcnt != 1) 24142621Sllai1 return (EINVAL); 24152621Sllai1 24162621Sllai1 if (vp->v_type != VDIR) 24172621Sllai1 return (ENOTDIR); 24182621Sllai1 24192621Sllai1 if (ddv->sdev_flags & SDEV_VTOR) { 24202621Sllai1 vtor = (int (*)(struct sdev_node *))sdev_get_vtor(ddv); 24212621Sllai1 ASSERT(vtor); 24222621Sllai1 } 24232621Sllai1 24242621Sllai1 if (eofp != NULL) 24252621Sllai1 *eofp = 0; 24262621Sllai1 24273133Sjg soff = uiop->uio_loffset; 24282621Sllai1 iovp = uiop->uio_iov; 24292621Sllai1 alloc_count = iovp->iov_len; 24302621Sllai1 dp = outbuf = kmem_alloc(alloc_count, KM_SLEEP); 24312621Sllai1 outcount = 0; 24322621Sllai1 24332621Sllai1 if (ddv->sdev_state == SDEV_ZOMBIE) 24342621Sllai1 goto get_cache; 24352621Sllai1 24362679Sszhou if (SDEV_IS_GLOBAL(ddv)) { 243710097SEric.Taylor@Sun.COM 243810097SEric.Taylor@Sun.COM if ((sdev_boot_state == SDEV_BOOT_STATE_COMPLETE) && 24392621Sllai1 !sdev_reconfig_boot && (flags & SDEV_BROWSE) && 24402621Sllai1 !SDEV_IS_DYNAMIC(ddv) && !SDEV_IS_NO_NCACHE(ddv) && 24412621Sllai1 ((moddebug & MODDEBUG_FINI_EBUSY) == 0) && 24422621Sllai1 !DEVNAME_DEVFSADM_HAS_RUN(devfsadm_state) && 24432621Sllai1 !DEVNAME_DEVFSADM_IS_RUNNING(devfsadm_state) && 24442621Sllai1 !sdev_reconfig_disable) { 24452621Sllai1 /* 24462621Sllai1 * invoking "devfsadm" to do system device reconfig 24472621Sllai1 */ 24482621Sllai1 mutex_enter(&ddv->sdev_lookup_lock); 24492621Sllai1 SDEV_BLOCK_OTHERS(ddv, 24502621Sllai1 (SDEV_READDIR|SDEV_LGWAITING)); 24512621Sllai1 mutex_exit(&ddv->sdev_lookup_lock); 24522621Sllai1 24532621Sllai1 sdcmn_err8(("readdir of %s by %s: reconfig\n", 24542621Sllai1 ddv->sdev_path, curproc->p_user.u_comm)); 24552621Sllai1 if (sdev_reconfig_verbose) { 24562621Sllai1 cmn_err(CE_CONT, 24572621Sllai1 "?readdir of %s by %s: reconfig\n", 24582621Sllai1 ddv->sdev_path, curproc->p_user.u_comm); 24592621Sllai1 } 24602621Sllai1 24612621Sllai1 sdev_devfsadmd_thread(ddv, NULL, kcred); 24622621Sllai1 } else if (DEVNAME_DEVFSADM_IS_RUNNING(devfsadm_state)) { 24632621Sllai1 /* 24642621Sllai1 * compensate the "ls" started later than "devfsadm" 24652621Sllai1 */ 24662621Sllai1 mutex_enter(&ddv->sdev_lookup_lock); 24672621Sllai1 SDEV_BLOCK_OTHERS(ddv, (SDEV_READDIR|SDEV_LGWAITING)); 24682621Sllai1 mutex_exit(&ddv->sdev_lookup_lock); 24692621Sllai1 } 24702621Sllai1 24712621Sllai1 /* 24722621Sllai1 * release the contents lock so that 24733843Sjg * the cache may be updated by devfsadmd 24742621Sllai1 */ 24752621Sllai1 rw_exit(&ddv->sdev_contents); 24762621Sllai1 mutex_enter(&ddv->sdev_lookup_lock); 24772621Sllai1 if (SDEV_IS_READDIR(ddv)) 24782621Sllai1 (void) sdev_wait4lookup(ddv, SDEV_READDIR); 24792621Sllai1 mutex_exit(&ddv->sdev_lookup_lock); 24802621Sllai1 rw_enter(&ddv->sdev_contents, RW_READER); 24812621Sllai1 24822621Sllai1 sdcmn_err4(("readdir of directory %s by %s\n", 24832621Sllai1 ddv->sdev_name, curproc->p_user.u_comm)); 24843843Sjg if (ddv->sdev_flags & SDEV_BUILD) { 24852621Sllai1 if (SDEV_IS_PERSIST(ddv)) { 24862621Sllai1 error = sdev_filldir_from_store(ddv, 24872621Sllai1 alloc_count, cred); 24882621Sllai1 } 24893843Sjg ddv->sdev_flags &= ~SDEV_BUILD; 24902621Sllai1 } 24912621Sllai1 } 24922621Sllai1 24932621Sllai1 get_cache: 24942621Sllai1 /* handle "." and ".." */ 24952621Sllai1 diroff = 0; 24962621Sllai1 if (soff == 0) { 24972621Sllai1 /* first time */ 24982621Sllai1 this_reclen = DIRENT64_RECLEN(1); 24992621Sllai1 if (alloc_count < this_reclen) { 25002621Sllai1 error = EINVAL; 25012621Sllai1 goto done; 25022621Sllai1 } 25032621Sllai1 25042621Sllai1 dp->d_ino = (ino64_t)ddv->sdev_ino; 25052621Sllai1 dp->d_off = (off64_t)1; 25062621Sllai1 dp->d_reclen = (ushort_t)this_reclen; 25072621Sllai1 25082621Sllai1 (void) strncpy(dp->d_name, ".", 25092621Sllai1 DIRENT64_NAMELEN(this_reclen)); 25102621Sllai1 outcount += dp->d_reclen; 25112621Sllai1 dp = nextdp(dp); 25122621Sllai1 } 25132621Sllai1 25142621Sllai1 diroff++; 25152621Sllai1 if (soff <= 1) { 25162621Sllai1 this_reclen = DIRENT64_RECLEN(2); 25172621Sllai1 if (alloc_count < outcount + this_reclen) { 25182621Sllai1 error = EINVAL; 25192621Sllai1 goto done; 25202621Sllai1 } 25212621Sllai1 25222621Sllai1 dp->d_reclen = (ushort_t)this_reclen; 25232621Sllai1 dp->d_ino = (ino64_t)ddv->sdev_dotdot->sdev_ino; 25242621Sllai1 dp->d_off = (off64_t)2; 25252621Sllai1 25262621Sllai1 (void) strncpy(dp->d_name, "..", 25272621Sllai1 DIRENT64_NAMELEN(this_reclen)); 25282621Sllai1 outcount += dp->d_reclen; 25292621Sllai1 25302621Sllai1 dp = nextdp(dp); 25312621Sllai1 } 25322621Sllai1 25332621Sllai1 25342621Sllai1 /* gets the cache */ 25352621Sllai1 diroff++; 25366260Sjg for (dv = SDEV_FIRST_ENTRY(ddv); dv; 25376260Sjg dv = SDEV_NEXT_ENTRY(ddv, dv), diroff++) { 25382621Sllai1 sdcmn_err3(("sdev_readdir: diroff %lld soff %lld for '%s' \n", 25392621Sllai1 diroff, soff, dv->sdev_name)); 25402621Sllai1 25412621Sllai1 /* bypassing pre-matured nodes */ 25422621Sllai1 if (diroff < soff || (dv->sdev_state != SDEV_READY)) { 25432621Sllai1 sdcmn_err3(("sdev_readdir: pre-mature node " 2544*10588SEric.Taylor@Sun.COM "%s %d\n", dv->sdev_name, dv->sdev_state)); 25452621Sllai1 continue; 25462621Sllai1 } 25472621Sllai1 25482621Sllai1 /* 25492621Sllai1 * Check validity of node 25502621Sllai1 */ 25512621Sllai1 if (vtor) { 25522621Sllai1 switch (vtor(dv)) { 25532621Sllai1 case SDEV_VTOR_VALID: 25542621Sllai1 break; 25552621Sllai1 case SDEV_VTOR_INVALID: 25562621Sllai1 case SDEV_VTOR_SKIP: 25572621Sllai1 continue; 25582621Sllai1 default: 25592621Sllai1 cmn_err(CE_PANIC, 25602621Sllai1 "dev fs: validator failed: %s(%p)\n", 25612621Sllai1 dv->sdev_name, (void *)dv); 25622621Sllai1 break; 25632621Sllai1 /*NOTREACHED*/ 25642621Sllai1 } 25652621Sllai1 } 25662621Sllai1 25672621Sllai1 namelen = strlen(dv->sdev_name); 25682621Sllai1 reclen = DIRENT64_RECLEN(namelen); 25692621Sllai1 if (outcount + reclen > alloc_count) { 25702621Sllai1 goto full; 25712621Sllai1 } 25722621Sllai1 dp->d_reclen = (ushort_t)reclen; 25732621Sllai1 dp->d_ino = (ino64_t)dv->sdev_ino; 25742621Sllai1 dp->d_off = (off64_t)diroff + 1; 25752621Sllai1 (void) strncpy(dp->d_name, dv->sdev_name, 25762621Sllai1 DIRENT64_NAMELEN(reclen)); 25772621Sllai1 outcount += reclen; 25782621Sllai1 dp = nextdp(dp); 25792621Sllai1 } 25802621Sllai1 25812621Sllai1 full: 25822621Sllai1 sdcmn_err4(("sdev_readdir: moving %lu bytes: " 25832621Sllai1 "diroff %lld, soff %lld, dv %p\n", outcount, diroff, soff, 25842621Sllai1 (void *)dv)); 25852621Sllai1 25862621Sllai1 if (outcount) 25872621Sllai1 error = uiomove(outbuf, outcount, UIO_READ, uiop); 25882621Sllai1 25892621Sllai1 if (!error) { 25903133Sjg uiop->uio_loffset = diroff; 25912621Sllai1 if (eofp) 25922621Sllai1 *eofp = dv ? 0 : 1; 25932621Sllai1 } 25942621Sllai1 25952621Sllai1 25962621Sllai1 if (ddv->sdev_attrvp) { 25972621Sllai1 gethrestime(&now); 25982621Sllai1 attr.va_ctime = now; 25992621Sllai1 attr.va_atime = now; 26002621Sllai1 attr.va_mask = AT_CTIME|AT_ATIME; 26012621Sllai1 26022621Sllai1 (void) VOP_SETATTR(ddv->sdev_attrvp, &attr, 0, kcred, NULL); 26032621Sllai1 } 26042621Sllai1 done: 26052621Sllai1 kmem_free(outbuf, alloc_count); 26062621Sllai1 return (error); 26072621Sllai1 } 26082621Sllai1 26092621Sllai1 static int 26102621Sllai1 sdev_modctl_lookup(const char *path, vnode_t **r_vp) 26112621Sllai1 { 26122621Sllai1 vnode_t *vp; 26132621Sllai1 vnode_t *cvp; 26142621Sllai1 struct sdev_node *svp; 26152621Sllai1 char *nm; 26162621Sllai1 struct pathname pn; 26172621Sllai1 int error; 26182621Sllai1 int persisted = 0; 26192621Sllai1 26207988SJerry.Gilliam@Sun.COM ASSERT(INGLOBALZONE(curproc)); 26217988SJerry.Gilliam@Sun.COM 26222621Sllai1 if (error = pn_get((char *)path, UIO_SYSSPACE, &pn)) 26232621Sllai1 return (error); 26242621Sllai1 nm = kmem_alloc(MAXNAMELEN, KM_SLEEP); 26252621Sllai1 26262621Sllai1 vp = rootdir; 26272621Sllai1 VN_HOLD(vp); 26282621Sllai1 26292621Sllai1 while (pn_pathleft(&pn)) { 26307988SJerry.Gilliam@Sun.COM ASSERT(vp->v_type == VDIR || vp->v_type == VLNK); 26312621Sllai1 (void) pn_getcomponent(&pn, nm); 26327988SJerry.Gilliam@Sun.COM 26337988SJerry.Gilliam@Sun.COM /* 26347988SJerry.Gilliam@Sun.COM * Deal with the .. special case where we may be 26357988SJerry.Gilliam@Sun.COM * traversing up across a mount point, to the 26367988SJerry.Gilliam@Sun.COM * root of this filesystem or global root. 26377988SJerry.Gilliam@Sun.COM */ 26387988SJerry.Gilliam@Sun.COM if (nm[0] == '.' && nm[1] == '.' && nm[2] == 0) { 26397988SJerry.Gilliam@Sun.COM checkforroot: 26407988SJerry.Gilliam@Sun.COM if (VN_CMP(vp, rootdir)) { 26417988SJerry.Gilliam@Sun.COM nm[1] = 0; 26427988SJerry.Gilliam@Sun.COM } else if (vp->v_flag & VROOT) { 26437988SJerry.Gilliam@Sun.COM vfs_t *vfsp; 26447988SJerry.Gilliam@Sun.COM cvp = vp; 26457988SJerry.Gilliam@Sun.COM vfsp = cvp->v_vfsp; 26467988SJerry.Gilliam@Sun.COM vfs_rlock_wait(vfsp); 26477988SJerry.Gilliam@Sun.COM vp = cvp->v_vfsp->vfs_vnodecovered; 26487988SJerry.Gilliam@Sun.COM if (vp == NULL || 26497988SJerry.Gilliam@Sun.COM (cvp->v_vfsp->vfs_flag & VFS_UNMOUNTED)) { 26507988SJerry.Gilliam@Sun.COM vfs_unlock(vfsp); 26517988SJerry.Gilliam@Sun.COM VN_RELE(cvp); 26527988SJerry.Gilliam@Sun.COM error = EIO; 26537988SJerry.Gilliam@Sun.COM break; 26547988SJerry.Gilliam@Sun.COM } 26557988SJerry.Gilliam@Sun.COM VN_HOLD(vp); 26567988SJerry.Gilliam@Sun.COM vfs_unlock(vfsp); 26577988SJerry.Gilliam@Sun.COM VN_RELE(cvp); 26587988SJerry.Gilliam@Sun.COM cvp = NULL; 26597988SJerry.Gilliam@Sun.COM goto checkforroot; 26607988SJerry.Gilliam@Sun.COM } 26617988SJerry.Gilliam@Sun.COM } 26627988SJerry.Gilliam@Sun.COM 26635331Samw error = VOP_LOOKUP(vp, nm, &cvp, NULL, 0, NULL, kcred, NULL, 26645331Samw NULL, NULL); 26657988SJerry.Gilliam@Sun.COM if (error) { 26667988SJerry.Gilliam@Sun.COM VN_RELE(vp); 26672621Sllai1 break; 26687988SJerry.Gilliam@Sun.COM } 26692621Sllai1 26702621Sllai1 /* traverse mount points encountered on our journey */ 26712621Sllai1 if (vn_ismntpt(cvp) && (error = traverse(&cvp)) != 0) { 26727988SJerry.Gilliam@Sun.COM VN_RELE(vp); 26732621Sllai1 VN_RELE(cvp); 26742621Sllai1 break; 26752621Sllai1 } 26762621Sllai1 26772621Sllai1 /* 26787988SJerry.Gilliam@Sun.COM * symbolic link, can be either relative and absolute 26797988SJerry.Gilliam@Sun.COM */ 26807988SJerry.Gilliam@Sun.COM if ((cvp->v_type == VLNK) && pn_pathleft(&pn)) { 26817988SJerry.Gilliam@Sun.COM struct pathname linkpath; 26827988SJerry.Gilliam@Sun.COM pn_alloc(&linkpath); 26837988SJerry.Gilliam@Sun.COM if (error = pn_getsymlink(cvp, &linkpath, kcred)) { 26847988SJerry.Gilliam@Sun.COM pn_free(&linkpath); 26857988SJerry.Gilliam@Sun.COM break; 26867988SJerry.Gilliam@Sun.COM } 26877988SJerry.Gilliam@Sun.COM if (pn_pathleft(&linkpath) == 0) 26887988SJerry.Gilliam@Sun.COM (void) pn_set(&linkpath, "."); 26897988SJerry.Gilliam@Sun.COM error = pn_insert(&pn, &linkpath, strlen(nm)); 26907988SJerry.Gilliam@Sun.COM pn_free(&linkpath); 26917988SJerry.Gilliam@Sun.COM if (pn.pn_pathlen == 0) { 26927988SJerry.Gilliam@Sun.COM VN_RELE(vp); 26937988SJerry.Gilliam@Sun.COM return (ENOENT); 26947988SJerry.Gilliam@Sun.COM } 26957988SJerry.Gilliam@Sun.COM if (pn.pn_path[0] == '/') { 26967988SJerry.Gilliam@Sun.COM pn_skipslash(&pn); 26977988SJerry.Gilliam@Sun.COM VN_RELE(vp); 26987988SJerry.Gilliam@Sun.COM VN_RELE(cvp); 26997988SJerry.Gilliam@Sun.COM vp = rootdir; 27007988SJerry.Gilliam@Sun.COM VN_HOLD(vp); 27017988SJerry.Gilliam@Sun.COM } else { 27027988SJerry.Gilliam@Sun.COM VN_RELE(cvp); 27037988SJerry.Gilliam@Sun.COM } 27047988SJerry.Gilliam@Sun.COM continue; 27057988SJerry.Gilliam@Sun.COM } 27067988SJerry.Gilliam@Sun.COM 27077988SJerry.Gilliam@Sun.COM VN_RELE(vp); 27087988SJerry.Gilliam@Sun.COM 27097988SJerry.Gilliam@Sun.COM /* 27102621Sllai1 * Direct the operation to the persisting filesystem 27112621Sllai1 * underlying /dev. Bail if we encounter a 27122621Sllai1 * non-persistent dev entity here. 27132621Sllai1 */ 27142621Sllai1 if (cvp->v_vfsp->vfs_fstype == devtype) { 27152621Sllai1 27162621Sllai1 if ((VTOSDEV(cvp)->sdev_flags & SDEV_PERSIST) == 0) { 27172621Sllai1 error = ENOENT; 27182621Sllai1 VN_RELE(cvp); 27192621Sllai1 break; 27202621Sllai1 } 27212621Sllai1 27222621Sllai1 if (VTOSDEV(cvp) == NULL) { 27232621Sllai1 error = ENOENT; 27242621Sllai1 VN_RELE(cvp); 27252621Sllai1 break; 27262621Sllai1 } 27272621Sllai1 svp = VTOSDEV(cvp); 27282621Sllai1 if ((vp = svp->sdev_attrvp) == NULL) { 27292621Sllai1 error = ENOENT; 27302621Sllai1 VN_RELE(cvp); 27312621Sllai1 break; 27322621Sllai1 } 27332621Sllai1 persisted = 1; 27342621Sllai1 VN_HOLD(vp); 27352621Sllai1 VN_RELE(cvp); 27362621Sllai1 cvp = vp; 27372621Sllai1 } 27382621Sllai1 27392621Sllai1 vp = cvp; 27402621Sllai1 pn_skipslash(&pn); 27412621Sllai1 } 27422621Sllai1 27432621Sllai1 kmem_free(nm, MAXNAMELEN); 27442621Sllai1 pn_free(&pn); 27452621Sllai1 27462621Sllai1 if (error) 27472621Sllai1 return (error); 27482621Sllai1 27492621Sllai1 /* 27502621Sllai1 * Only return persisted nodes in the filesystem underlying /dev. 27512621Sllai1 */ 27522621Sllai1 if (!persisted) { 27532621Sllai1 VN_RELE(vp); 27542621Sllai1 return (ENOENT); 27552621Sllai1 } 27562621Sllai1 27572621Sllai1 *r_vp = vp; 27582621Sllai1 return (0); 27592621Sllai1 } 27602621Sllai1 27612621Sllai1 int 27622621Sllai1 sdev_modctl_readdir(const char *dir, char ***dirlistp, 27636065Scth int *npathsp, int *npathsp_alloc, int checking_empty) 27642621Sllai1 { 27652621Sllai1 char **pathlist = NULL; 27662621Sllai1 char **newlist = NULL; 27672621Sllai1 int npaths = 0; 27682621Sllai1 int npaths_alloc = 0; 27692621Sllai1 dirent64_t *dbuf = NULL; 27702621Sllai1 int n; 27712621Sllai1 char *s; 27722621Sllai1 int error; 27732621Sllai1 vnode_t *vp; 27742621Sllai1 int eof; 27752621Sllai1 struct iovec iov; 27762621Sllai1 struct uio uio; 27772621Sllai1 struct dirent64 *dp; 27782621Sllai1 size_t dlen; 27792621Sllai1 size_t dbuflen; 27802621Sllai1 int ndirents = 64; 27812621Sllai1 char *nm; 27822621Sllai1 27832621Sllai1 error = sdev_modctl_lookup(dir, &vp); 27842621Sllai1 sdcmn_err11(("modctl readdir: %s by %s: %s\n", 27852621Sllai1 dir, curproc->p_user.u_comm, 27862621Sllai1 (error == 0) ? "ok" : "failed")); 27872621Sllai1 if (error) 27882621Sllai1 return (error); 27892621Sllai1 27902621Sllai1 dlen = ndirents * (sizeof (*dbuf)); 27912621Sllai1 dbuf = kmem_alloc(dlen, KM_SLEEP); 27922621Sllai1 27932621Sllai1 uio.uio_iov = &iov; 27942621Sllai1 uio.uio_iovcnt = 1; 27952621Sllai1 uio.uio_segflg = UIO_SYSSPACE; 27962621Sllai1 uio.uio_fmode = 0; 27972621Sllai1 uio.uio_extflg = UIO_COPY_CACHED; 27982621Sllai1 uio.uio_loffset = 0; 27992621Sllai1 uio.uio_llimit = MAXOFFSET_T; 28002621Sllai1 28012621Sllai1 eof = 0; 28022621Sllai1 error = 0; 28032621Sllai1 while (!error && !eof) { 28042621Sllai1 uio.uio_resid = dlen; 28052621Sllai1 iov.iov_base = (char *)dbuf; 28062621Sllai1 iov.iov_len = dlen; 28072621Sllai1 28082621Sllai1 (void) VOP_RWLOCK(vp, V_WRITELOCK_FALSE, NULL); 28095331Samw error = VOP_READDIR(vp, &uio, kcred, &eof, NULL, 0); 28102621Sllai1 VOP_RWUNLOCK(vp, V_WRITELOCK_FALSE, NULL); 28112621Sllai1 28122621Sllai1 dbuflen = dlen - uio.uio_resid; 28132621Sllai1 28142621Sllai1 if (error || dbuflen == 0) 28152621Sllai1 break; 28162621Sllai1 28172621Sllai1 for (dp = dbuf; ((intptr_t)dp < (intptr_t)dbuf + dbuflen); 28186065Scth dp = (dirent64_t *)((intptr_t)dp + dp->d_reclen)) { 28192621Sllai1 28202621Sllai1 nm = dp->d_name; 28212621Sllai1 28222621Sllai1 if (strcmp(nm, ".") == 0 || strcmp(nm, "..") == 0) 28232621Sllai1 continue; 28242621Sllai1 if (npaths == npaths_alloc) { 28252621Sllai1 npaths_alloc += 64; 28262621Sllai1 newlist = (char **) 28272621Sllai1 kmem_zalloc((npaths_alloc + 1) * 28286065Scth sizeof (char *), KM_SLEEP); 28292621Sllai1 if (pathlist) { 28302621Sllai1 bcopy(pathlist, newlist, 28312621Sllai1 npaths * sizeof (char *)); 28322621Sllai1 kmem_free(pathlist, 28332621Sllai1 (npaths + 1) * sizeof (char *)); 28342621Sllai1 } 28352621Sllai1 pathlist = newlist; 28362621Sllai1 } 28372621Sllai1 n = strlen(nm) + 1; 28382621Sllai1 s = kmem_alloc(n, KM_SLEEP); 28392621Sllai1 bcopy(nm, s, n); 28402621Sllai1 pathlist[npaths++] = s; 28412621Sllai1 sdcmn_err11((" %s/%s\n", dir, s)); 28426065Scth 28436065Scth /* if checking empty, one entry is as good as many */ 28446065Scth if (checking_empty) { 28456065Scth eof = 1; 28466065Scth break; 28476065Scth } 28482621Sllai1 } 28492621Sllai1 } 28502621Sllai1 28512621Sllai1 exit: 28522621Sllai1 VN_RELE(vp); 28532621Sllai1 28542621Sllai1 if (dbuf) 28552621Sllai1 kmem_free(dbuf, dlen); 28562621Sllai1 28572621Sllai1 if (error) 28582621Sllai1 return (error); 28592621Sllai1 28602621Sllai1 *dirlistp = pathlist; 28612621Sllai1 *npathsp = npaths; 28622621Sllai1 *npathsp_alloc = npaths_alloc; 28632621Sllai1 28642621Sllai1 return (0); 28652621Sllai1 } 28662621Sllai1 28672621Sllai1 void 28682621Sllai1 sdev_modctl_readdir_free(char **pathlist, int npaths, int npaths_alloc) 28692621Sllai1 { 28702621Sllai1 int i, n; 28712621Sllai1 28722621Sllai1 for (i = 0; i < npaths; i++) { 28732621Sllai1 n = strlen(pathlist[i]) + 1; 28742621Sllai1 kmem_free(pathlist[i], n); 28752621Sllai1 } 28762621Sllai1 28772621Sllai1 kmem_free(pathlist, (npaths_alloc + 1) * sizeof (char *)); 28782621Sllai1 } 28792621Sllai1 28802621Sllai1 int 28812621Sllai1 sdev_modctl_devexists(const char *path) 28822621Sllai1 { 28832621Sllai1 vnode_t *vp; 28842621Sllai1 int error; 28852621Sllai1 28862621Sllai1 error = sdev_modctl_lookup(path, &vp); 28872621Sllai1 sdcmn_err11(("modctl dev exists: %s by %s: %s\n", 28882621Sllai1 path, curproc->p_user.u_comm, 28892621Sllai1 (error == 0) ? "ok" : "failed")); 28902621Sllai1 if (error == 0) 28912621Sllai1 VN_RELE(vp); 28922621Sllai1 28932621Sllai1 return (error); 28942621Sllai1 } 28952621Sllai1 28962621Sllai1 extern int sdev_vnodeops_tbl_size; 28972621Sllai1 28982621Sllai1 /* 28992621Sllai1 * construct a new template with overrides from vtab 29002621Sllai1 */ 29012621Sllai1 static fs_operation_def_t * 29022621Sllai1 sdev_merge_vtab(const fs_operation_def_t tab[]) 29032621Sllai1 { 29042621Sllai1 fs_operation_def_t *new; 29052621Sllai1 const fs_operation_def_t *tab_entry; 29062621Sllai1 29072621Sllai1 /* make a copy of standard vnode ops table */ 29082621Sllai1 new = kmem_alloc(sdev_vnodeops_tbl_size, KM_SLEEP); 29092621Sllai1 bcopy((void *)sdev_vnodeops_tbl, new, sdev_vnodeops_tbl_size); 29102621Sllai1 29112621Sllai1 /* replace the overrides from tab */ 29122621Sllai1 for (tab_entry = tab; tab_entry->name != NULL; tab_entry++) { 29132621Sllai1 fs_operation_def_t *std_entry = new; 29142621Sllai1 while (std_entry->name) { 29152621Sllai1 if (strcmp(tab_entry->name, std_entry->name) == 0) { 29162621Sllai1 std_entry->func = tab_entry->func; 29172621Sllai1 break; 29182621Sllai1 } 29192621Sllai1 std_entry++; 29202621Sllai1 } 29212621Sllai1 if (std_entry->name == NULL) 29222621Sllai1 cmn_err(CE_NOTE, "sdev_merge_vtab: entry %s unused.", 29232621Sllai1 tab_entry->name); 29242621Sllai1 } 29252621Sllai1 29262621Sllai1 return (new); 29272621Sllai1 } 29282621Sllai1 29292621Sllai1 /* free memory allocated by sdev_merge_vtab */ 29302621Sllai1 static void 29312621Sllai1 sdev_free_vtab(fs_operation_def_t *new) 29322621Sllai1 { 29332621Sllai1 kmem_free(new, sdev_vnodeops_tbl_size); 29342621Sllai1 } 29352621Sllai1 29362621Sllai1 /* 29372621Sllai1 * a generic setattr() function 29382621Sllai1 * 29392621Sllai1 * note: flags only supports AT_UID and AT_GID. 29402621Sllai1 * Future enhancements can be done for other types, e.g. AT_MODE 29412621Sllai1 */ 29422621Sllai1 int 29432621Sllai1 devname_setattr_func(struct vnode *vp, struct vattr *vap, int flags, 29442621Sllai1 struct cred *cred, int (*callback)(struct sdev_node *, struct vattr *, 29452621Sllai1 int), int protocol) 29462621Sllai1 { 29472621Sllai1 struct sdev_node *dv = VTOSDEV(vp); 29482621Sllai1 struct sdev_node *parent = dv->sdev_dotdot; 29492621Sllai1 struct vattr *get; 29502621Sllai1 uint_t mask = vap->va_mask; 29512621Sllai1 int error; 29522621Sllai1 29532621Sllai1 /* some sanity checks */ 29542621Sllai1 if (vap->va_mask & AT_NOSET) 29552621Sllai1 return (EINVAL); 29562621Sllai1 29572621Sllai1 if (vap->va_mask & AT_SIZE) { 29582621Sllai1 if (vp->v_type == VDIR) { 29592621Sllai1 return (EISDIR); 29602621Sllai1 } 29612621Sllai1 } 29622621Sllai1 29632621Sllai1 /* no need to set attribute, but do not fail either */ 29642621Sllai1 ASSERT(parent); 29652621Sllai1 rw_enter(&parent->sdev_contents, RW_READER); 29662621Sllai1 if (dv->sdev_state == SDEV_ZOMBIE) { 29672621Sllai1 rw_exit(&parent->sdev_contents); 29682621Sllai1 return (0); 29692621Sllai1 } 29702621Sllai1 29712621Sllai1 /* If backing store exists, just set it. */ 29722621Sllai1 if (dv->sdev_attrvp) { 29732621Sllai1 rw_exit(&parent->sdev_contents); 29742621Sllai1 return (VOP_SETATTR(dv->sdev_attrvp, vap, flags, cred, NULL)); 29752621Sllai1 } 29762621Sllai1 29772621Sllai1 /* 29782621Sllai1 * Otherwise, for nodes with the persistence attribute, create it. 29792621Sllai1 */ 29802621Sllai1 ASSERT(dv->sdev_attr); 29812621Sllai1 if (SDEV_IS_PERSIST(dv) || 29822621Sllai1 ((vap->va_mask & ~AT_TIMES) != 0 && !SDEV_IS_DYNAMIC(dv))) { 29832621Sllai1 sdev_vattr_merge(dv, vap); 29842621Sllai1 rw_enter(&dv->sdev_contents, RW_WRITER); 29852621Sllai1 error = sdev_shadow_node(dv, cred); 29862621Sllai1 rw_exit(&dv->sdev_contents); 29872621Sllai1 rw_exit(&parent->sdev_contents); 29882621Sllai1 29892621Sllai1 if (error) 29902621Sllai1 return (error); 29912621Sllai1 return (VOP_SETATTR(dv->sdev_attrvp, vap, flags, cred, NULL)); 29922621Sllai1 } 29932621Sllai1 29942621Sllai1 29952621Sllai1 /* 29962621Sllai1 * sdev_attr was allocated in sdev_mknode 29972621Sllai1 */ 29982621Sllai1 rw_enter(&dv->sdev_contents, RW_WRITER); 29995331Samw error = secpolicy_vnode_setattr(cred, vp, vap, 30005331Samw dv->sdev_attr, flags, sdev_unlocked_access, dv); 30012621Sllai1 if (error) { 30022621Sllai1 rw_exit(&dv->sdev_contents); 30032621Sllai1 rw_exit(&parent->sdev_contents); 30042621Sllai1 return (error); 30052621Sllai1 } 30062621Sllai1 30072621Sllai1 get = dv->sdev_attr; 30082621Sllai1 if (mask & AT_MODE) { 30092621Sllai1 get->va_mode &= S_IFMT; 30102621Sllai1 get->va_mode |= vap->va_mode & ~S_IFMT; 30112621Sllai1 } 30122621Sllai1 30132621Sllai1 if ((mask & AT_UID) || (mask & AT_GID)) { 30142621Sllai1 if (mask & AT_UID) 30152621Sllai1 get->va_uid = vap->va_uid; 30162621Sllai1 if (mask & AT_GID) 30172621Sllai1 get->va_gid = vap->va_gid; 30182621Sllai1 /* 30192621Sllai1 * a callback must be provided if the protocol is set 30202621Sllai1 */ 30212621Sllai1 if ((protocol & AT_UID) || (protocol & AT_GID)) { 30222621Sllai1 ASSERT(callback); 30232621Sllai1 error = callback(dv, get, protocol); 30242621Sllai1 if (error) { 30252621Sllai1 rw_exit(&dv->sdev_contents); 30262621Sllai1 rw_exit(&parent->sdev_contents); 30272621Sllai1 return (error); 30282621Sllai1 } 30292621Sllai1 } 30302621Sllai1 } 30312621Sllai1 30322621Sllai1 if (mask & AT_ATIME) 30332621Sllai1 get->va_atime = vap->va_atime; 30342621Sllai1 if (mask & AT_MTIME) 30352621Sllai1 get->va_mtime = vap->va_mtime; 30362621Sllai1 if (mask & (AT_MODE | AT_UID | AT_GID | AT_CTIME)) { 30372621Sllai1 gethrestime(&get->va_ctime); 30382621Sllai1 } 30392621Sllai1 30402621Sllai1 sdev_vattr_merge(dv, get); 30412621Sllai1 rw_exit(&dv->sdev_contents); 30422621Sllai1 rw_exit(&parent->sdev_contents); 30432621Sllai1 return (0); 30442621Sllai1 } 30455895Syz147064 30465895Syz147064 /* 30475895Syz147064 * a generic inactive() function 30485895Syz147064 */ 304910097SEric.Taylor@Sun.COM /*ARGSUSED*/ 30505895Syz147064 void 30515895Syz147064 devname_inactive_func(struct vnode *vp, struct cred *cred, 30525895Syz147064 void (*callback)(struct vnode *)) 30535895Syz147064 { 30545895Syz147064 int clean; 30555895Syz147064 struct sdev_node *dv = VTOSDEV(vp); 30565895Syz147064 struct sdev_node *ddv = dv->sdev_dotdot; 30575895Syz147064 int state; 30585895Syz147064 30595895Syz147064 rw_enter(&ddv->sdev_contents, RW_WRITER); 30605895Syz147064 state = dv->sdev_state; 30615895Syz147064 30625895Syz147064 mutex_enter(&vp->v_lock); 30635895Syz147064 ASSERT(vp->v_count >= 1); 30645895Syz147064 30655895Syz147064 if (vp->v_count == 1 && callback != NULL) 30665895Syz147064 callback(vp); 30675895Syz147064 30685895Syz147064 clean = (vp->v_count == 1) && (state == SDEV_ZOMBIE); 30695895Syz147064 30705895Syz147064 /* 30715895Syz147064 * last ref count on the ZOMBIE node is released. 30725895Syz147064 * clean up the sdev_node, and 30735895Syz147064 * release the hold on the backing store node so that 30745895Syz147064 * the ZOMBIE backing stores also cleaned out. 30755895Syz147064 */ 30765895Syz147064 if (clean) { 30775895Syz147064 ASSERT(ddv); 30785895Syz147064 30795895Syz147064 ddv->sdev_nlink--; 30805895Syz147064 if (vp->v_type == VDIR) { 30815895Syz147064 dv->sdev_nlink--; 30825895Syz147064 } 30836347Sjg if ((dv->sdev_flags & SDEV_STALE) == 0) 30846347Sjg avl_remove(&ddv->sdev_entries, dv); 30855895Syz147064 dv->sdev_nlink--; 30865895Syz147064 --vp->v_count; 30875895Syz147064 mutex_exit(&vp->v_lock); 30885895Syz147064 sdev_nodedestroy(dv, 0); 30895895Syz147064 } else { 30905895Syz147064 --vp->v_count; 30915895Syz147064 mutex_exit(&vp->v_lock); 30925895Syz147064 } 30935895Syz147064 rw_exit(&ddv->sdev_contents); 30945895Syz147064 } 3095