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 /* 22*9198SJerry.Gilliam@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 232621Sllai1 * Use is subject to license terms. 242621Sllai1 */ 252621Sllai1 262621Sllai1 /* 272621Sllai1 * vnode ops for the /dev filesystem 282621Sllai1 * 292621Sllai1 * - VDIR, VCHR, CBLK, and VLNK are considered must supported files 302621Sllai1 * - VREG and VDOOR are used for some internal implementations in 312621Sllai1 * the global zone, e.g. devname and devfsadm communication 322621Sllai1 * - other file types are unusual in this namespace and 332621Sllai1 * not supported for now 342621Sllai1 */ 352621Sllai1 362621Sllai1 #include <sys/types.h> 372621Sllai1 #include <sys/param.h> 382621Sllai1 #include <sys/t_lock.h> 392621Sllai1 #include <sys/systm.h> 402621Sllai1 #include <sys/sysmacros.h> 412621Sllai1 #include <sys/user.h> 422621Sllai1 #include <sys/time.h> 432621Sllai1 #include <sys/vfs.h> 442621Sllai1 #include <sys/vnode.h> 453898Srsb #include <sys/vfs_opreg.h> 462621Sllai1 #include <sys/file.h> 472621Sllai1 #include <sys/fcntl.h> 482621Sllai1 #include <sys/flock.h> 492621Sllai1 #include <sys/kmem.h> 502621Sllai1 #include <sys/uio.h> 512621Sllai1 #include <sys/errno.h> 522621Sllai1 #include <sys/stat.h> 532621Sllai1 #include <sys/cred.h> 542621Sllai1 #include <sys/cred_impl.h> 552621Sllai1 #include <sys/dirent.h> 562621Sllai1 #include <sys/pathname.h> 572621Sllai1 #include <sys/cmn_err.h> 582621Sllai1 #include <sys/debug.h> 592621Sllai1 #include <sys/policy.h> 602621Sllai1 #include <vm/hat.h> 612621Sllai1 #include <vm/seg_vn.h> 622621Sllai1 #include <vm/seg_map.h> 632621Sllai1 #include <vm/seg.h> 642621Sllai1 #include <vm/as.h> 652621Sllai1 #include <vm/page.h> 662621Sllai1 #include <sys/proc.h> 672621Sllai1 #include <sys/mode.h> 682621Sllai1 #include <sys/sunndi.h> 692621Sllai1 #include <sys/ptms.h> 702621Sllai1 #include <fs/fs_subr.h> 712621Sllai1 #include <sys/fs/dv_node.h> 722621Sllai1 #include <sys/fs/sdev_impl.h> 732621Sllai1 #include <sys/fs/sdev_node.h> 742621Sllai1 752621Sllai1 /*ARGSUSED*/ 762621Sllai1 static int 775331Samw sdev_open(struct vnode **vpp, int flag, struct cred *cred, caller_context_t *ct) 782621Sllai1 { 792621Sllai1 struct sdev_node *dv = VTOSDEV(*vpp); 802621Sllai1 struct sdev_node *ddv = dv->sdev_dotdot; 812621Sllai1 int error = 0; 822621Sllai1 832621Sllai1 if ((*vpp)->v_type == VDIR) 842621Sllai1 return (0); 852621Sllai1 862621Sllai1 if (!SDEV_IS_GLOBAL(dv)) 872621Sllai1 return (ENOTSUP); 882621Sllai1 892621Sllai1 ASSERT((*vpp)->v_type == VREG); 902621Sllai1 if ((*vpp)->v_type != VREG) 912621Sllai1 return (ENOTSUP); 922621Sllai1 932621Sllai1 ASSERT(ddv); 942621Sllai1 rw_enter(&ddv->sdev_contents, RW_READER); 952621Sllai1 if (dv->sdev_attrvp == NULL) { 962621Sllai1 rw_exit(&ddv->sdev_contents); 972621Sllai1 return (ENOENT); 982621Sllai1 } 995331Samw error = VOP_OPEN(&(dv->sdev_attrvp), flag, cred, ct); 1002621Sllai1 rw_exit(&ddv->sdev_contents); 1012621Sllai1 return (error); 1022621Sllai1 } 1032621Sllai1 1042621Sllai1 /*ARGSUSED1*/ 1052621Sllai1 static int 1062621Sllai1 sdev_close(struct vnode *vp, int flag, int count, 1075331Samw offset_t offset, struct cred *cred, caller_context_t *ct) 1082621Sllai1 { 1092621Sllai1 struct sdev_node *dv = VTOSDEV(vp); 1102621Sllai1 1112621Sllai1 if (vp->v_type == VDIR) { 1122621Sllai1 cleanlocks(vp, ttoproc(curthread)->p_pid, 0); 1132621Sllai1 cleanshares(vp, ttoproc(curthread)->p_pid); 1142621Sllai1 return (0); 1152621Sllai1 } 1162621Sllai1 1172621Sllai1 if (!SDEV_IS_GLOBAL(dv)) 1182621Sllai1 return (ENOTSUP); 1192621Sllai1 1202621Sllai1 ASSERT(vp->v_type == VREG); 1212621Sllai1 if (vp->v_type != VREG) 1222621Sllai1 return (ENOTSUP); 1232621Sllai1 1242621Sllai1 ASSERT(dv->sdev_attrvp); 1255331Samw return (VOP_CLOSE(dv->sdev_attrvp, flag, count, offset, cred, ct)); 1262621Sllai1 } 1272621Sllai1 1282621Sllai1 /*ARGSUSED*/ 1292621Sllai1 static int 1302621Sllai1 sdev_read(struct vnode *vp, struct uio *uio, int ioflag, struct cred *cred, 1312621Sllai1 struct caller_context *ct) 1322621Sllai1 { 1332621Sllai1 struct sdev_node *dv = (struct sdev_node *)VTOSDEV(vp); 1342621Sllai1 int error; 1352621Sllai1 1362621Sllai1 if (!SDEV_IS_GLOBAL(dv)) 1372621Sllai1 return (EINVAL); 1382621Sllai1 1392621Sllai1 if (vp->v_type == VDIR) 1402621Sllai1 return (EISDIR); 1412621Sllai1 1422621Sllai1 /* only supporting regular files in /dev */ 1432621Sllai1 ASSERT(vp->v_type == VREG); 1442621Sllai1 if (vp->v_type != VREG) 1452621Sllai1 return (EINVAL); 1462621Sllai1 1472621Sllai1 ASSERT(RW_READ_HELD(&VTOSDEV(vp)->sdev_contents)); 1482621Sllai1 ASSERT(dv->sdev_attrvp); 1495331Samw (void) VOP_RWLOCK(dv->sdev_attrvp, 0, ct); 1502621Sllai1 error = VOP_READ(dv->sdev_attrvp, uio, ioflag, cred, ct); 1515331Samw VOP_RWUNLOCK(dv->sdev_attrvp, 0, ct); 1522621Sllai1 return (error); 1532621Sllai1 } 1542621Sllai1 1552621Sllai1 /*ARGSUSED*/ 1562621Sllai1 static int 1572621Sllai1 sdev_write(struct vnode *vp, struct uio *uio, int ioflag, struct cred *cred, 1582621Sllai1 struct caller_context *ct) 1592621Sllai1 { 1602621Sllai1 struct sdev_node *dv = VTOSDEV(vp); 1612621Sllai1 int error = 0; 1622621Sllai1 1632621Sllai1 if (!SDEV_IS_GLOBAL(dv)) 1642621Sllai1 return (EINVAL); 1652621Sllai1 1662621Sllai1 if (vp->v_type == VDIR) 1672621Sllai1 return (EISDIR); 1682621Sllai1 1692621Sllai1 /* only supporting regular files in /dev */ 1702621Sllai1 ASSERT(vp->v_type == VREG); 1712621Sllai1 if (vp->v_type != VREG) 1722621Sllai1 return (EINVAL); 1732621Sllai1 1742621Sllai1 ASSERT(dv->sdev_attrvp); 1752621Sllai1 1765331Samw (void) VOP_RWLOCK(dv->sdev_attrvp, 1, ct); 1772621Sllai1 error = VOP_WRITE(dv->sdev_attrvp, uio, ioflag, cred, ct); 1785331Samw VOP_RWUNLOCK(dv->sdev_attrvp, 1, ct); 1792621Sllai1 if (error == 0) { 1802621Sllai1 sdev_update_timestamps(dv->sdev_attrvp, kcred, 1812621Sllai1 AT_MTIME); 1822621Sllai1 } 1832621Sllai1 return (error); 1842621Sllai1 } 1852621Sllai1 1862621Sllai1 /*ARGSUSED*/ 1872621Sllai1 static int 1882621Sllai1 sdev_ioctl(struct vnode *vp, int cmd, intptr_t arg, int flag, 1895331Samw struct cred *cred, int *rvalp, caller_context_t *ct) 1902621Sllai1 { 1912621Sllai1 struct sdev_node *dv = VTOSDEV(vp); 1922621Sllai1 1932621Sllai1 if (!SDEV_IS_GLOBAL(dv) || (vp->v_type == VDIR)) 1942621Sllai1 return (ENOTTY); 1952621Sllai1 1962621Sllai1 ASSERT(vp->v_type == VREG); 1972621Sllai1 if (vp->v_type != VREG) 1982621Sllai1 return (EINVAL); 1992621Sllai1 2002621Sllai1 ASSERT(dv->sdev_attrvp); 2015331Samw return (VOP_IOCTL(dv->sdev_attrvp, cmd, arg, flag, cred, rvalp, ct)); 2022621Sllai1 } 2032621Sllai1 2042621Sllai1 static int 2055331Samw sdev_getattr(struct vnode *vp, struct vattr *vap, int flags, 2065331Samw struct cred *cr, caller_context_t *ct) 2072621Sllai1 { 2082621Sllai1 int error = 0; 2092621Sllai1 struct sdev_node *dv = VTOSDEV(vp); 2102621Sllai1 struct sdev_node *parent = dv->sdev_dotdot; 2112621Sllai1 struct devname_nsmap *map = NULL; 2122621Sllai1 struct devname_ops *dirops = NULL; 2132621Sllai1 int (*fn)(devname_handle_t *, struct vattr *, struct cred *); 2142621Sllai1 2152621Sllai1 ASSERT(parent); 2162621Sllai1 2172621Sllai1 rw_enter(&parent->sdev_contents, RW_READER); 2182621Sllai1 ASSERT(dv->sdev_attr || dv->sdev_attrvp); 2192621Sllai1 if (SDEV_IS_GLOBAL(dv) && (dv->sdev_state != SDEV_ZOMBIE)) { 2202621Sllai1 map = sdev_get_map(parent, 0); 2212621Sllai1 dirops = map ? map->dir_ops : NULL; 2222621Sllai1 } 2232621Sllai1 2242621Sllai1 /* 2252621Sllai1 * search order: 2262621Sllai1 * - for persistent nodes (SDEV_PERSIST): backstore 2272621Sllai1 * - for non-persistent nodes: module ops if global, then memory 2282621Sllai1 */ 2292621Sllai1 if (dv->sdev_attrvp) { 2302621Sllai1 rw_exit(&parent->sdev_contents); 2315331Samw error = VOP_GETATTR(dv->sdev_attrvp, vap, flags, cr, ct); 2322621Sllai1 sdev_vattr_merge(dv, vap); 2332621Sllai1 } else if (dirops && (fn = dirops->devnops_getattr)) { 2342621Sllai1 sdev_vattr_merge(dv, vap); 2352621Sllai1 rw_exit(&parent->sdev_contents); 2362621Sllai1 error = (*fn)(&(dv->sdev_handle), vap, cr); 2372621Sllai1 } else { 2382621Sllai1 ASSERT(dv->sdev_attr); 2392621Sllai1 *vap = *dv->sdev_attr; 2402621Sllai1 sdev_vattr_merge(dv, vap); 2412621Sllai1 rw_exit(&parent->sdev_contents); 2422621Sllai1 } 2432621Sllai1 2442621Sllai1 return (error); 2452621Sllai1 } 2462621Sllai1 2473748Sjg /*ARGSUSED4*/ 2482621Sllai1 static int 2493748Sjg sdev_setattr(struct vnode *vp, struct vattr *vap, int flags, 2503748Sjg struct cred *cred, caller_context_t *ctp) 2512621Sllai1 { 2522621Sllai1 return (devname_setattr_func(vp, vap, flags, cred, NULL, 0)); 2532621Sllai1 } 2542621Sllai1 2552621Sllai1 static int 2562621Sllai1 sdev_getsecattr(struct vnode *vp, struct vsecattr *vsap, int flags, 2575331Samw struct cred *cr, caller_context_t *ct) 2582621Sllai1 { 2592621Sllai1 int error; 2602621Sllai1 struct sdev_node *dv = VTOSDEV(vp); 2612621Sllai1 struct vnode *avp = dv->sdev_attrvp; 2622621Sllai1 2632621Sllai1 if (avp == NULL) { 2642621Sllai1 /* return fs_fab_acl() if flavor matches, else do nothing */ 2652621Sllai1 if ((SDEV_ACL_FLAVOR(vp) == _ACL_ACLENT_ENABLED && 2662621Sllai1 (vsap->vsa_mask & (VSA_ACLCNT | VSA_DFACLCNT))) || 2672621Sllai1 (SDEV_ACL_FLAVOR(vp) == _ACL_ACE_ENABLED && 2682621Sllai1 (vsap->vsa_mask & (VSA_ACECNT | VSA_ACE)))) 2695331Samw return (fs_fab_acl(vp, vsap, flags, cr, ct)); 2702621Sllai1 2712621Sllai1 return (ENOSYS); 2722621Sllai1 } 2732621Sllai1 2745331Samw (void) VOP_RWLOCK(avp, 1, ct); 2755331Samw error = VOP_GETSECATTR(avp, vsap, flags, cr, ct); 2765331Samw VOP_RWUNLOCK(avp, 1, ct); 2772621Sllai1 return (error); 2782621Sllai1 } 2792621Sllai1 2802621Sllai1 static int 2812621Sllai1 sdev_setsecattr(struct vnode *vp, struct vsecattr *vsap, int flags, 2825331Samw struct cred *cr, caller_context_t *ct) 2832621Sllai1 { 2842621Sllai1 int error; 2852621Sllai1 struct sdev_node *dv = VTOSDEV(vp); 2862621Sllai1 struct vnode *avp = dv->sdev_attrvp; 2872621Sllai1 2882621Sllai1 if (dv->sdev_state == SDEV_ZOMBIE) 2892621Sllai1 return (0); 2902621Sllai1 2912621Sllai1 if (avp == NULL) { 2922621Sllai1 if (SDEV_IS_GLOBAL(dv) && !SDEV_IS_PERSIST(dv)) 2932621Sllai1 return (fs_nosys()); 2942621Sllai1 ASSERT(dv->sdev_attr); 2952621Sllai1 /* 2962621Sllai1 * if coming in directly, the acl system call will 2972621Sllai1 * have held the read-write lock via VOP_RWLOCK() 2982621Sllai1 * If coming in via specfs, specfs will have 2992621Sllai1 * held the rw lock on the realvp i.e. us. 3002621Sllai1 */ 3012621Sllai1 ASSERT(RW_WRITE_HELD(&dv->sdev_contents)); 3022621Sllai1 sdev_vattr_merge(dv, dv->sdev_attr); 3035331Samw error = sdev_shadow_node(dv, cr); 3042621Sllai1 if (error) { 3052621Sllai1 return (fs_nosys()); 3062621Sllai1 } 3072621Sllai1 3082621Sllai1 ASSERT(dv->sdev_attrvp); 3092621Sllai1 /* clean out the memory copy if any */ 3102621Sllai1 if (dv->sdev_attr) { 3112621Sllai1 kmem_free(dv->sdev_attr, sizeof (struct vattr)); 3122621Sllai1 dv->sdev_attr = NULL; 3132621Sllai1 } 3142621Sllai1 avp = dv->sdev_attrvp; 3152621Sllai1 } 3162621Sllai1 ASSERT(avp); 3172621Sllai1 3185331Samw (void) VOP_RWLOCK(avp, V_WRITELOCK_TRUE, ct); 3195331Samw error = VOP_SETSECATTR(avp, vsap, flags, cr, ct); 3205331Samw VOP_RWUNLOCK(avp, V_WRITELOCK_TRUE, ct); 3212621Sllai1 return (error); 3222621Sllai1 } 3232621Sllai1 3242621Sllai1 int 3252621Sllai1 sdev_unlocked_access(void *vdv, int mode, struct cred *cr) 3262621Sllai1 { 3272621Sllai1 struct sdev_node *dv = vdv; 3282621Sllai1 int shift = 0; 3292621Sllai1 uid_t owner = dv->sdev_attr->va_uid; 3302621Sllai1 3312621Sllai1 if (crgetuid(cr) != owner) { 3322621Sllai1 shift += 3; 3332621Sllai1 if (groupmember(dv->sdev_attr->va_gid, cr) == 0) 3342621Sllai1 shift += 3; 3352621Sllai1 } 3362621Sllai1 3372621Sllai1 mode &= ~(dv->sdev_attr->va_mode << shift); 3382621Sllai1 if (mode == 0) 3392621Sllai1 return (0); 3402621Sllai1 3412621Sllai1 return (secpolicy_vnode_access(cr, SDEVTOV(dv), owner, mode)); 3422621Sllai1 } 3432621Sllai1 3442621Sllai1 static int 3455331Samw sdev_access(struct vnode *vp, int mode, int flags, struct cred *cr, 3465331Samw caller_context_t *ct) 3472621Sllai1 { 3482621Sllai1 struct sdev_node *dv = VTOSDEV(vp); 3492621Sllai1 int ret = 0; 3502621Sllai1 3512621Sllai1 ASSERT(dv->sdev_attr || dv->sdev_attrvp); 3522621Sllai1 3532621Sllai1 if (dv->sdev_attrvp) { 3545331Samw ret = VOP_ACCESS(dv->sdev_attrvp, mode, flags, cr, ct); 3552621Sllai1 } else if (dv->sdev_attr) { 3562621Sllai1 rw_enter(&dv->sdev_contents, RW_READER); 3572621Sllai1 ret = sdev_unlocked_access(dv, mode, cr); 3582621Sllai1 if (ret) 3592621Sllai1 ret = EACCES; 3602621Sllai1 rw_exit(&dv->sdev_contents); 3612621Sllai1 } 3622621Sllai1 3632621Sllai1 return (ret); 3642621Sllai1 } 3652621Sllai1 3662621Sllai1 /* 3672621Sllai1 * Lookup 3682621Sllai1 */ 3692621Sllai1 /*ARGSUSED3*/ 3702621Sllai1 static int 3712621Sllai1 sdev_lookup(struct vnode *dvp, char *nm, struct vnode **vpp, 3725331Samw struct pathname *pnp, int flags, struct vnode *rdir, struct cred *cred, 3735331Samw caller_context_t *ct, int *direntflags, pathname_t *realpnp) 3742621Sllai1 { 3753843Sjg struct sdev_node *parent; 3763843Sjg int error; 3772621Sllai1 3782621Sllai1 parent = VTOSDEV(dvp); 3792621Sllai1 ASSERT(parent); 3802621Sllai1 3813843Sjg /* execute access is required to search the directory */ 3825331Samw if ((error = VOP_ACCESS(dvp, VEXEC, 0, cred, ct)) != 0) 3833843Sjg return (error); 3843843Sjg 3852621Sllai1 if (!SDEV_IS_GLOBAL(parent)) 3862621Sllai1 return (prof_lookup(dvp, nm, vpp, cred)); 3872621Sllai1 return (devname_lookup_func(parent, nm, vpp, cred, NULL, 0)); 3882621Sllai1 } 3892621Sllai1 3902621Sllai1 /*ARGSUSED2*/ 3912621Sllai1 static int 3922621Sllai1 sdev_create(struct vnode *dvp, char *nm, struct vattr *vap, vcexcl_t excl, 3935331Samw int mode, struct vnode **vpp, struct cred *cred, int flag, 3945331Samw caller_context_t *ct, vsecattr_t *vsecp) 3952621Sllai1 { 3962621Sllai1 struct vnode *vp = NULL; 3972621Sllai1 struct vnode *avp; 3982621Sllai1 struct sdev_node *parent; 3992621Sllai1 struct sdev_node *self = NULL; 4002621Sllai1 int error = 0; 4012621Sllai1 vtype_t type = vap->va_type; 4022621Sllai1 4032729Sllai1 ASSERT(type != VNON && type != VBAD); 4042621Sllai1 4052621Sllai1 if ((type == VFIFO) || (type == VSOCK) || 4062621Sllai1 (type == VPROC) || (type == VPORT)) 4072621Sllai1 return (ENOTSUP); 4082621Sllai1 4092621Sllai1 parent = VTOSDEV(dvp); 4102621Sllai1 ASSERT(parent); 4112621Sllai1 4122621Sllai1 rw_enter(&parent->sdev_dotdot->sdev_contents, RW_READER); 4132621Sllai1 if (parent->sdev_state == SDEV_ZOMBIE) { 4142621Sllai1 rw_exit(&parent->sdev_dotdot->sdev_contents); 4152621Sllai1 return (ENOENT); 4162621Sllai1 } 4172621Sllai1 4182621Sllai1 /* non-global do not allow pure node creation */ 4192621Sllai1 if (!SDEV_IS_GLOBAL(parent)) { 4202621Sllai1 rw_exit(&parent->sdev_dotdot->sdev_contents); 4212621Sllai1 return (prof_lookup(dvp, nm, vpp, cred)); 4222621Sllai1 } 4232621Sllai1 rw_exit(&parent->sdev_dotdot->sdev_contents); 4242621Sllai1 4253843Sjg /* execute access is required to search the directory */ 426*9198SJerry.Gilliam@Sun.COM if ((error = VOP_ACCESS(dvp, VEXEC, 0, cred, ct)) != 0) 4273843Sjg return (error); 4283843Sjg 4292621Sllai1 /* check existing name */ 4305331Samw /* XXXci - We may need to translate the C-I flags on VOP_LOOKUP */ 4315331Samw error = VOP_LOOKUP(dvp, nm, &vp, NULL, 0, NULL, cred, ct, NULL, NULL); 4322621Sllai1 4332621Sllai1 /* name found */ 4342621Sllai1 if (error == 0) { 4352621Sllai1 ASSERT(vp); 4362621Sllai1 if (excl == EXCL) { 4372621Sllai1 error = EEXIST; 4382621Sllai1 } else if ((vp->v_type == VDIR) && (mode & VWRITE)) { 4392621Sllai1 /* allowing create/read-only an existing directory */ 4402621Sllai1 error = EISDIR; 4412621Sllai1 } else { 4425686Sjg error = VOP_ACCESS(vp, mode, 0, cred, ct); 4432621Sllai1 } 4442621Sllai1 4452621Sllai1 if (error) { 4462621Sllai1 VN_RELE(vp); 4472621Sllai1 return (error); 4482621Sllai1 } 4492621Sllai1 4502621Sllai1 /* truncation first */ 4512621Sllai1 if ((vp->v_type == VREG) && (vap->va_mask & AT_SIZE) && 4522621Sllai1 (vap->va_size == 0)) { 4532621Sllai1 ASSERT(parent->sdev_attrvp); 4542621Sllai1 error = VOP_CREATE(parent->sdev_attrvp, 4555331Samw nm, vap, excl, mode, &avp, cred, flag, ct, vsecp); 4562621Sllai1 4572621Sllai1 if (error) { 4582621Sllai1 VN_RELE(vp); 4592621Sllai1 return (error); 4602621Sllai1 } 4612621Sllai1 } 4622621Sllai1 4632621Sllai1 sdev_update_timestamps(vp, kcred, 4642621Sllai1 AT_CTIME|AT_MTIME|AT_ATIME); 4652621Sllai1 *vpp = vp; 4662621Sllai1 return (0); 4672621Sllai1 } 4682621Sllai1 4692621Sllai1 /* bail out early */ 4702621Sllai1 if (error != ENOENT) 4712621Sllai1 return (error); 4722621Sllai1 473*9198SJerry.Gilliam@Sun.COM /* verify write access - compliance specifies ENXIO */ 474*9198SJerry.Gilliam@Sun.COM if ((error = VOP_ACCESS(dvp, VEXEC|VWRITE, 0, cred, ct)) != 0) { 475*9198SJerry.Gilliam@Sun.COM if (error == EACCES) 476*9198SJerry.Gilliam@Sun.COM error = ENXIO; 477*9198SJerry.Gilliam@Sun.COM return (error); 478*9198SJerry.Gilliam@Sun.COM } 479*9198SJerry.Gilliam@Sun.COM 4802621Sllai1 /* 4812621Sllai1 * For memory-based (ROFS) directory: 4822621Sllai1 * - either disallow node creation; 4832621Sllai1 * - or implement VOP_CREATE of its own 4842621Sllai1 */ 4852621Sllai1 rw_enter(&parent->sdev_contents, RW_WRITER); 4862621Sllai1 if (!SDEV_IS_PERSIST(parent)) { 4872621Sllai1 rw_exit(&parent->sdev_contents); 4882621Sllai1 return (ENOTSUP); 4892621Sllai1 } 4902621Sllai1 ASSERT(parent->sdev_attrvp); 4912621Sllai1 error = sdev_mknode(parent, nm, &self, vap, NULL, NULL, 4922621Sllai1 cred, SDEV_READY); 4932621Sllai1 if (error) { 4942621Sllai1 rw_exit(&parent->sdev_contents); 4952621Sllai1 if (self) 4962621Sllai1 SDEV_RELE(self); 4972621Sllai1 return (error); 4982621Sllai1 } 4992621Sllai1 rw_exit(&parent->sdev_contents); 5002621Sllai1 5012621Sllai1 ASSERT(self); 5022621Sllai1 /* take care the timestamps for the node and its parent */ 5032621Sllai1 sdev_update_timestamps(SDEVTOV(self), kcred, 5042621Sllai1 AT_CTIME|AT_MTIME|AT_ATIME); 5052621Sllai1 sdev_update_timestamps(dvp, kcred, AT_MTIME|AT_ATIME); 5062621Sllai1 if (SDEV_IS_GLOBAL(parent)) 5072621Sllai1 atomic_inc_ulong(&parent->sdev_gdir_gen); 5082621Sllai1 5092621Sllai1 /* wake up other threads blocked on looking up this node */ 5102621Sllai1 mutex_enter(&self->sdev_lookup_lock); 5112621Sllai1 SDEV_UNBLOCK_OTHERS(self, SDEV_LOOKUP); 5122621Sllai1 mutex_exit(&self->sdev_lookup_lock); 5132621Sllai1 error = sdev_to_vp(self, vpp); 5142621Sllai1 return (error); 5152621Sllai1 } 5162621Sllai1 5172621Sllai1 static int 5185331Samw sdev_remove(struct vnode *dvp, char *nm, struct cred *cred, 5195331Samw caller_context_t *ct, int flags) 5202621Sllai1 { 5212621Sllai1 int error; 5222621Sllai1 struct sdev_node *parent = (struct sdev_node *)VTOSDEV(dvp); 5232621Sllai1 struct vnode *vp = NULL; 5242621Sllai1 struct sdev_node *dv = NULL; 5252621Sllai1 struct devname_nsmap *map = NULL; 5262621Sllai1 struct devname_ops *dirops = NULL; 5272621Sllai1 int (*fn)(devname_handle_t *); 5282621Sllai1 int len; 5292621Sllai1 int bkstore = 0; 5302621Sllai1 5312621Sllai1 /* bail out early */ 5322621Sllai1 len = strlen(nm); 5332621Sllai1 if (nm[0] == '.') { 5342621Sllai1 if (len == 1) { 5352621Sllai1 return (EINVAL); 5362621Sllai1 } else if (len == 2 && nm[1] == '.') { 5372621Sllai1 return (EEXIST); 5382621Sllai1 } 5392621Sllai1 } 5402621Sllai1 5412621Sllai1 ASSERT(parent); 5422621Sllai1 rw_enter(&parent->sdev_contents, RW_READER); 5432621Sllai1 if (!SDEV_IS_GLOBAL(parent)) { 5442621Sllai1 rw_exit(&parent->sdev_contents); 5452621Sllai1 return (ENOTSUP); 5462621Sllai1 } 5472621Sllai1 5483843Sjg /* execute access is required to search the directory */ 5495331Samw if ((error = VOP_ACCESS(dvp, VEXEC, 0, cred, ct)) != 0) { 5503843Sjg rw_exit(&parent->sdev_contents); 5513843Sjg return (error); 5523843Sjg } 5533843Sjg 5542621Sllai1 /* check existence first */ 5552621Sllai1 dv = sdev_cache_lookup(parent, nm); 5562621Sllai1 if (dv == NULL) { 5572621Sllai1 rw_exit(&parent->sdev_contents); 5582621Sllai1 return (ENOENT); 5592621Sllai1 } 5602621Sllai1 5612621Sllai1 vp = SDEVTOV(dv); 5622621Sllai1 if ((dv->sdev_state == SDEV_INIT) || 5632621Sllai1 (dv->sdev_state == SDEV_ZOMBIE)) { 5642621Sllai1 rw_exit(&parent->sdev_contents); 5652621Sllai1 VN_RELE(vp); 5662621Sllai1 return (ENOENT); 5672621Sllai1 } 5682621Sllai1 5693843Sjg /* write access is required to remove an entry */ 5705331Samw if ((error = VOP_ACCESS(dvp, VWRITE, 0, cred, ct)) != 0) { 5713843Sjg rw_exit(&parent->sdev_contents); 5723843Sjg VN_RELE(vp); 5733843Sjg return (error); 5743843Sjg } 5753843Sjg 5762621Sllai1 /* the module may record/reject removing a device node */ 5772621Sllai1 map = sdev_get_map(parent, 0); 5782621Sllai1 dirops = map ? map->dir_ops : NULL; 5792621Sllai1 if (dirops && ((fn = dirops->devnops_remove) != NULL)) { 5802621Sllai1 error = (*fn)(&(dv->sdev_handle)); 5812621Sllai1 if (error) { 5822621Sllai1 rw_exit(&parent->sdev_contents); 5832621Sllai1 VN_RELE(vp); 5842621Sllai1 return (error); 5852621Sllai1 } 5862621Sllai1 } 5872621Sllai1 5882621Sllai1 /* 5892621Sllai1 * sdev_dirdelete does the real job of: 5902621Sllai1 * - make sure no open ref count 5912621Sllai1 * - destroying the sdev_node 5922621Sllai1 * - releasing the hold on attrvp 5932621Sllai1 */ 5942621Sllai1 bkstore = SDEV_IS_PERSIST(dv) ? 1 : 0; 5952621Sllai1 if (!rw_tryupgrade(&parent->sdev_contents)) { 5962621Sllai1 rw_exit(&parent->sdev_contents); 5972621Sllai1 rw_enter(&parent->sdev_contents, RW_WRITER); 5982621Sllai1 } 5992621Sllai1 error = sdev_cache_update(parent, &dv, nm, SDEV_CACHE_DELETE); 6002621Sllai1 rw_exit(&parent->sdev_contents); 6012621Sllai1 6022621Sllai1 sdcmn_err2(("sdev_remove: cache_update error %d\n", error)); 6032621Sllai1 if (error && (error != EBUSY)) { 6042621Sllai1 /* report errors other than EBUSY */ 6052621Sllai1 VN_RELE(vp); 6062621Sllai1 } else { 6072621Sllai1 sdcmn_err2(("sdev_remove: cleaning node %s from cache " 6082621Sllai1 " with error %d\n", nm, error)); 6092621Sllai1 6102621Sllai1 /* 6112621Sllai1 * best efforts clean up the backing store 6122621Sllai1 */ 6132621Sllai1 if (bkstore) { 6142621Sllai1 ASSERT(parent->sdev_attrvp); 6155331Samw error = VOP_REMOVE(parent->sdev_attrvp, nm, cred, 6165331Samw ct, flags); 6172621Sllai1 /* 6182621Sllai1 * do not report BUSY error 6192621Sllai1 * because the backing store ref count is released 6202621Sllai1 * when the last ref count on the sdev_node is 6212621Sllai1 * released. 6222621Sllai1 */ 6232621Sllai1 if (error == EBUSY) { 6242621Sllai1 sdcmn_err2(("sdev_remove: device %s is still on" 6252621Sllai1 "disk %s\n", nm, parent->sdev_path)); 6262621Sllai1 error = 0; 6272621Sllai1 } 6282621Sllai1 } 6292621Sllai1 6302621Sllai1 if (error == EBUSY) 6312621Sllai1 error = 0; 6322621Sllai1 } 6332621Sllai1 6342621Sllai1 return (error); 6352621Sllai1 } 6362621Sllai1 6372621Sllai1 /* 6382621Sllai1 * Some restrictions for this file system: 6392621Sllai1 * - both oldnm and newnm are in the scope of /dev file system, 6402621Sllai1 * to simply the namespace management model. 6412621Sllai1 */ 6425331Samw /*ARGSUSED6*/ 6432621Sllai1 static int 6442621Sllai1 sdev_rename(struct vnode *odvp, char *onm, struct vnode *ndvp, char *nnm, 6455331Samw struct cred *cred, caller_context_t *ct, int flags) 6462621Sllai1 { 6472621Sllai1 struct sdev_node *fromparent = NULL; 6482621Sllai1 struct vattr vattr; 6492621Sllai1 struct sdev_node *toparent; 6502621Sllai1 struct sdev_node *fromdv = NULL; /* source node */ 6512729Sllai1 struct vnode *ovp = NULL; /* source vnode */ 6522621Sllai1 struct sdev_node *todv = NULL; /* destination node */ 6532729Sllai1 struct vnode *nvp = NULL; /* destination vnode */ 6542621Sllai1 int samedir = 0; /* set if odvp == ndvp */ 6552621Sllai1 struct vnode *realvp; 6562621Sllai1 int len; 6572621Sllai1 char nnm_path[MAXPATHLEN]; 6582621Sllai1 struct devname_nsmap *omap = NULL; 6592621Sllai1 struct devname_ops *odirops = NULL; 6602621Sllai1 int (*fn)(devname_handle_t *, char *); 6612621Sllai1 int (*rmfn)(devname_handle_t *); 6622621Sllai1 int error = 0; 6632621Sllai1 dev_t fsid; 6642621Sllai1 int bkstore = 0; 6652729Sllai1 vtype_t type; 6662621Sllai1 6672621Sllai1 /* prevent modifying "." and ".." */ 6682621Sllai1 if ((onm[0] == '.' && 6692729Sllai1 (onm[1] == '\0' || (onm[1] == '.' && onm[2] == '\0'))) || 6702729Sllai1 (nnm[0] == '.' && 6712729Sllai1 (nnm[1] == '\0' || (nnm[1] == '.' && nnm[2] == '\0')))) { 6722621Sllai1 return (EINVAL); 6732621Sllai1 } 6742621Sllai1 6752621Sllai1 fromparent = VTOSDEV(odvp); 6762621Sllai1 toparent = VTOSDEV(ndvp); 6772621Sllai1 6782621Sllai1 /* ZOMBIE parent doesn't allow new node creation */ 6792621Sllai1 rw_enter(&fromparent->sdev_dotdot->sdev_contents, RW_READER); 6802621Sllai1 if (fromparent->sdev_state == SDEV_ZOMBIE) { 6812621Sllai1 rw_exit(&fromparent->sdev_dotdot->sdev_contents); 6822621Sllai1 return (ENOENT); 6832621Sllai1 } 6842621Sllai1 6852621Sllai1 /* renaming only supported for global device nodes */ 6862621Sllai1 if (!SDEV_IS_GLOBAL(fromparent)) { 6872621Sllai1 rw_exit(&fromparent->sdev_dotdot->sdev_contents); 6882621Sllai1 return (ENOTSUP); 6892621Sllai1 } 6902621Sllai1 rw_exit(&fromparent->sdev_dotdot->sdev_contents); 6912621Sllai1 6922621Sllai1 rw_enter(&toparent->sdev_dotdot->sdev_contents, RW_READER); 6932621Sllai1 if (toparent->sdev_state == SDEV_ZOMBIE) { 6942621Sllai1 rw_exit(&toparent->sdev_dotdot->sdev_contents); 6952621Sllai1 return (ENOENT); 6962621Sllai1 } 6972621Sllai1 rw_exit(&toparent->sdev_dotdot->sdev_contents); 6982621Sllai1 6992729Sllai1 /* 7003843Sjg * acquire the global lock to prevent 7012729Sllai1 * mount/unmount/other rename activities. 7022729Sllai1 */ 7032729Sllai1 mutex_enter(&sdev_lock); 7042729Sllai1 7052621Sllai1 /* check existence of the source node */ 7065331Samw /* XXXci - We may need to translate the C-I flags on VOP_LOOKUP */ 7075331Samw error = VOP_LOOKUP(odvp, onm, &ovp, NULL, 0, NULL, cred, ct, 7085331Samw NULL, NULL); 7092621Sllai1 if (error) { 7102621Sllai1 sdcmn_err2(("sdev_rename: the source node %s exists\n", 7112621Sllai1 onm)); 7122729Sllai1 mutex_exit(&sdev_lock); 7132621Sllai1 return (error); 7142621Sllai1 } 7152621Sllai1 7165331Samw if (VOP_REALVP(ovp, &realvp, ct) == 0) { 7172621Sllai1 VN_HOLD(realvp); 7182621Sllai1 VN_RELE(ovp); 7192621Sllai1 ovp = realvp; 7202621Sllai1 } 7212621Sllai1 7222621Sllai1 /* check existence of destination */ 7235331Samw /* XXXci - We may need to translate the C-I flags on VOP_LOOKUP */ 7245331Samw error = VOP_LOOKUP(ndvp, nnm, &nvp, NULL, 0, NULL, cred, ct, 7255331Samw NULL, NULL); 7262621Sllai1 if (error && (error != ENOENT)) { 7272729Sllai1 mutex_exit(&sdev_lock); 7282621Sllai1 VN_RELE(ovp); 7292621Sllai1 return (error); 7302621Sllai1 } 7312621Sllai1 7325331Samw if (nvp && (VOP_REALVP(nvp, &realvp, ct) == 0)) { 7332621Sllai1 VN_HOLD(realvp); 7342621Sllai1 VN_RELE(nvp); 7352621Sllai1 nvp = realvp; 7362621Sllai1 } 7372621Sllai1 7382621Sllai1 /* 7392729Sllai1 * make sure the source and the destination are 7402729Sllai1 * in the same dev filesystem 7412621Sllai1 */ 7422621Sllai1 if (odvp != ndvp) { 7432621Sllai1 vattr.va_mask = AT_FSID; 7445331Samw if (error = VOP_GETATTR(odvp, &vattr, 0, cred, ct)) { 7452729Sllai1 mutex_exit(&sdev_lock); 7462621Sllai1 VN_RELE(ovp); 7472621Sllai1 return (error); 7482621Sllai1 } 7492621Sllai1 fsid = vattr.va_fsid; 7502621Sllai1 vattr.va_mask = AT_FSID; 7515331Samw if (error = VOP_GETATTR(ndvp, &vattr, 0, cred, ct)) { 7522729Sllai1 mutex_exit(&sdev_lock); 7532621Sllai1 VN_RELE(ovp); 7542621Sllai1 return (error); 7552621Sllai1 } 7562621Sllai1 if (fsid != vattr.va_fsid) { 7572729Sllai1 mutex_exit(&sdev_lock); 7582621Sllai1 VN_RELE(ovp); 7592621Sllai1 return (EXDEV); 7602621Sllai1 } 7612621Sllai1 } 7622621Sllai1 7632621Sllai1 /* make sure the old entry can be deleted */ 7645331Samw error = VOP_ACCESS(odvp, VWRITE, 0, cred, ct); 7652621Sllai1 if (error) { 7662729Sllai1 mutex_exit(&sdev_lock); 7672621Sllai1 VN_RELE(ovp); 7682621Sllai1 return (error); 7692621Sllai1 } 7702621Sllai1 7712621Sllai1 /* make sure the destination allows creation */ 7722621Sllai1 samedir = (fromparent == toparent); 7732621Sllai1 if (!samedir) { 7745331Samw error = VOP_ACCESS(ndvp, VEXEC|VWRITE, 0, cred, ct); 7752621Sllai1 if (error) { 7762729Sllai1 mutex_exit(&sdev_lock); 7772621Sllai1 VN_RELE(ovp); 7782621Sllai1 return (error); 7792621Sllai1 } 7802621Sllai1 } 7812621Sllai1 7822621Sllai1 fromdv = VTOSDEV(ovp); 7832621Sllai1 ASSERT(fromdv); 7842621Sllai1 7852621Sllai1 /* check with the plug-in modules for the source directory */ 7862621Sllai1 rw_enter(&fromparent->sdev_contents, RW_READER); 7872621Sllai1 omap = sdev_get_map(fromparent, 0); 7882621Sllai1 rw_exit(&fromparent->sdev_contents); 7892621Sllai1 odirops = omap ? omap->dir_ops : NULL; 7902621Sllai1 if (odirops && ((fn = odirops->devnops_rename) != NULL)) { 7912621Sllai1 if (samedir) { 7922621Sllai1 error = (*fn)(&(fromdv->sdev_handle), nnm); 7932621Sllai1 } else { 7942621Sllai1 len = strlen(nnm) + strlen(toparent->sdev_name) + 2; 7952621Sllai1 (void) snprintf(nnm_path, len, "%s/%s", 7962621Sllai1 toparent->sdev_name, nnm); 7972621Sllai1 error = (*fn)(&(fromdv->sdev_handle), nnm); 7982621Sllai1 } 7992621Sllai1 8002621Sllai1 if (error) { 8012729Sllai1 mutex_exit(&sdev_lock); 8022729Sllai1 sdcmn_err2(("sdev_rename: DBNR doesn't " 8032729Sllai1 "allow rename, error %d", error)); 8042621Sllai1 VN_RELE(ovp); 8052621Sllai1 return (error); 8062621Sllai1 } 8072621Sllai1 } 8082621Sllai1 8092729Sllai1 /* destination file exists */ 8102621Sllai1 if (nvp) { 8112729Sllai1 todv = VTOSDEV(nvp); 8122729Sllai1 ASSERT(todv); 8132621Sllai1 } 8142621Sllai1 8152621Sllai1 /* 8162621Sllai1 * link source to new target in the memory 8172621Sllai1 */ 8182729Sllai1 error = sdev_rnmnode(fromparent, fromdv, toparent, &todv, nnm, cred); 8192621Sllai1 if (error) { 8202621Sllai1 sdcmn_err2(("sdev_rename: renaming %s to %s failed " 8212621Sllai1 " with error %d\n", onm, nnm, error)); 8222729Sllai1 mutex_exit(&sdev_lock); 8232729Sllai1 if (nvp) 8242729Sllai1 VN_RELE(nvp); 8252621Sllai1 VN_RELE(ovp); 8262729Sllai1 return (error); 8272729Sllai1 } 8282729Sllai1 8292729Sllai1 /* notify the DBNR module the node is going away */ 8302729Sllai1 if (odirops && ((rmfn = odirops->devnops_remove) != NULL)) { 8312729Sllai1 (void) (*rmfn)(&(fromdv->sdev_handle)); 8322729Sllai1 } 8332729Sllai1 8342729Sllai1 /* 8352729Sllai1 * unlink from source 8362729Sllai1 */ 8372729Sllai1 rw_enter(&fromparent->sdev_contents, RW_READER); 8382729Sllai1 fromdv = sdev_cache_lookup(fromparent, onm); 8392729Sllai1 if (fromdv == NULL) { 8402729Sllai1 rw_exit(&fromparent->sdev_contents); 8412729Sllai1 mutex_exit(&sdev_lock); 8422729Sllai1 sdcmn_err2(("sdev_rename: the source is deleted already\n")); 8432729Sllai1 return (0); 8442621Sllai1 } 8452621Sllai1 8462729Sllai1 if (fromdv->sdev_state == SDEV_ZOMBIE) { 8472729Sllai1 rw_exit(&fromparent->sdev_contents); 8482729Sllai1 mutex_exit(&sdev_lock); 8492729Sllai1 VN_RELE(SDEVTOV(fromdv)); 8502729Sllai1 sdcmn_err2(("sdev_rename: the source is being deleted\n")); 8512729Sllai1 return (0); 8522729Sllai1 } 8532729Sllai1 rw_exit(&fromparent->sdev_contents); 8542729Sllai1 ASSERT(SDEVTOV(fromdv) == ovp); 8552729Sllai1 VN_RELE(ovp); 8562729Sllai1 8572729Sllai1 /* clean out the directory contents before it can be removed */ 8582729Sllai1 type = SDEVTOV(fromdv)->v_type; 8592729Sllai1 if (type == VDIR) { 8602729Sllai1 error = sdev_cleandir(fromdv, NULL, 0); 8612729Sllai1 sdcmn_err2(("sdev_rename: cleandir finished with %d\n", 8622729Sllai1 error)); 8632729Sllai1 if (error == EBUSY) 8642729Sllai1 error = 0; 8652729Sllai1 } 8662729Sllai1 8672729Sllai1 rw_enter(&fromparent->sdev_contents, RW_WRITER); 8682729Sllai1 bkstore = SDEV_IS_PERSIST(fromdv) ? 1 : 0; 8692729Sllai1 error = sdev_cache_update(fromparent, &fromdv, onm, 8702729Sllai1 SDEV_CACHE_DELETE); 8712729Sllai1 8722729Sllai1 /* best effforts clean up the backing store */ 8732729Sllai1 if (bkstore) { 8742729Sllai1 ASSERT(fromparent->sdev_attrvp); 8752729Sllai1 if (type != VDIR) { 8765331Samw /* XXXci - We may need to translate the C-I flags on VOP_REMOVE */ 8772729Sllai1 error = VOP_REMOVE(fromparent->sdev_attrvp, 8785331Samw onm, kcred, ct, 0); 8792729Sllai1 } else { 8805331Samw /* XXXci - We may need to translate the C-I flags on VOP_RMDIR */ 8812729Sllai1 error = VOP_RMDIR(fromparent->sdev_attrvp, 8825331Samw onm, fromparent->sdev_attrvp, kcred, ct, 0); 8832729Sllai1 } 8842729Sllai1 8852729Sllai1 if (error) { 8862729Sllai1 sdcmn_err2(("sdev_rename: device %s is " 8872729Sllai1 "still on disk %s\n", onm, 8882729Sllai1 fromparent->sdev_path)); 8892729Sllai1 error = 0; 8902729Sllai1 } 8912729Sllai1 } 8922729Sllai1 rw_exit(&fromparent->sdev_contents); 8932729Sllai1 mutex_exit(&sdev_lock); 8942729Sllai1 8952729Sllai1 /* once reached to this point, the rename is regarded successful */ 8962729Sllai1 return (0); 8972621Sllai1 } 8982621Sllai1 8992621Sllai1 /* 9002621Sllai1 * dev-fs version of "ln -s path dev-name" 9012621Sllai1 * tnm - path, e.g. /devices/... or /dev/... 9022621Sllai1 * lnm - dev_name 9032621Sllai1 */ 9045331Samw /*ARGSUSED6*/ 9052621Sllai1 static int 9062621Sllai1 sdev_symlink(struct vnode *dvp, char *lnm, struct vattr *tva, 9075331Samw char *tnm, struct cred *cred, caller_context_t *ct, int flags) 9082621Sllai1 { 9092621Sllai1 int error; 9102621Sllai1 struct vnode *vp = NULL; 9112621Sllai1 struct sdev_node *parent = (struct sdev_node *)VTOSDEV(dvp); 9122621Sllai1 struct sdev_node *self = (struct sdev_node *)NULL; 9132621Sllai1 9142621Sllai1 ASSERT(parent); 9152621Sllai1 rw_enter(&parent->sdev_dotdot->sdev_contents, RW_READER); 9162621Sllai1 if (parent->sdev_state == SDEV_ZOMBIE) { 9172621Sllai1 rw_exit(&parent->sdev_dotdot->sdev_contents); 9182621Sllai1 sdcmn_err2(("sdev_symlink: parent %s is ZOMBIED \n", 9192621Sllai1 parent->sdev_name)); 9202621Sllai1 return (ENOENT); 9212621Sllai1 } 9222621Sllai1 9232621Sllai1 if (!SDEV_IS_GLOBAL(parent)) { 9242621Sllai1 rw_exit(&parent->sdev_dotdot->sdev_contents); 9252621Sllai1 return (ENOTSUP); 9262621Sllai1 } 9272621Sllai1 rw_exit(&parent->sdev_dotdot->sdev_contents); 9282621Sllai1 9293843Sjg /* execute access is required to search a directory */ 9305331Samw if ((error = VOP_ACCESS(dvp, VEXEC, 0, cred, ct)) != 0) 9313843Sjg return (error); 9323843Sjg 9332621Sllai1 /* find existing name */ 9345331Samw /* XXXci - We may need to translate the C-I flags here */ 9355331Samw error = VOP_LOOKUP(dvp, lnm, &vp, NULL, 0, NULL, cred, ct, NULL, NULL); 9362621Sllai1 if (error == 0) { 9372621Sllai1 ASSERT(vp); 9382621Sllai1 VN_RELE(vp); 9392621Sllai1 sdcmn_err2(("sdev_symlink: node %s already exists\n", lnm)); 9402621Sllai1 return (EEXIST); 9412621Sllai1 } 9423843Sjg if (error != ENOENT) 9433843Sjg return (error); 9442621Sllai1 9453843Sjg /* write access is required to create a symlink */ 9465331Samw if ((error = VOP_ACCESS(dvp, VWRITE, 0, cred, ct)) != 0) 9472621Sllai1 return (error); 9482621Sllai1 9492621Sllai1 /* put it into memory cache */ 9502621Sllai1 rw_enter(&parent->sdev_contents, RW_WRITER); 9512621Sllai1 error = sdev_mknode(parent, lnm, &self, tva, NULL, (void *)tnm, 9522621Sllai1 cred, SDEV_READY); 9532621Sllai1 if (error) { 9542621Sllai1 rw_exit(&parent->sdev_contents); 9552621Sllai1 sdcmn_err2(("sdev_symlink: node %s creation failed\n", lnm)); 9562621Sllai1 if (self) 9572621Sllai1 SDEV_RELE(self); 9582621Sllai1 9592621Sllai1 return (error); 9602621Sllai1 } 9612621Sllai1 ASSERT(self && (self->sdev_state == SDEV_READY)); 9622621Sllai1 rw_exit(&parent->sdev_contents); 9632621Sllai1 9642621Sllai1 /* take care the timestamps for the node and its parent */ 9652621Sllai1 sdev_update_timestamps(SDEVTOV(self), kcred, 9662621Sllai1 AT_CTIME|AT_MTIME|AT_ATIME); 9672621Sllai1 sdev_update_timestamps(dvp, kcred, AT_MTIME|AT_ATIME); 9682621Sllai1 if (SDEV_IS_GLOBAL(parent)) 9692621Sllai1 atomic_inc_ulong(&parent->sdev_gdir_gen); 9702621Sllai1 9712621Sllai1 /* wake up other threads blocked on looking up this node */ 9722621Sllai1 mutex_enter(&self->sdev_lookup_lock); 9732621Sllai1 SDEV_UNBLOCK_OTHERS(self, SDEV_LOOKUP); 9742621Sllai1 mutex_exit(&self->sdev_lookup_lock); 9752621Sllai1 SDEV_RELE(self); /* don't return with vnode held */ 9762621Sllai1 return (0); 9772621Sllai1 } 9782621Sllai1 9795331Samw /*ARGSUSED6*/ 9802621Sllai1 static int 9812621Sllai1 sdev_mkdir(struct vnode *dvp, char *nm, struct vattr *va, struct vnode **vpp, 9825331Samw struct cred *cred, caller_context_t *ct, int flags, vsecattr_t *vsecp) 9832621Sllai1 { 9842621Sllai1 int error; 9852621Sllai1 struct sdev_node *parent = (struct sdev_node *)VTOSDEV(dvp); 9862621Sllai1 struct sdev_node *self = NULL; 9872621Sllai1 struct vnode *vp = NULL; 9882621Sllai1 9892621Sllai1 ASSERT(parent && parent->sdev_dotdot); 9902621Sllai1 rw_enter(&parent->sdev_dotdot->sdev_contents, RW_READER); 9912621Sllai1 if (parent->sdev_state == SDEV_ZOMBIE) { 9922621Sllai1 rw_exit(&parent->sdev_dotdot->sdev_contents); 9932621Sllai1 return (ENOENT); 9942621Sllai1 } 9952621Sllai1 9962621Sllai1 /* non-global do not allow pure directory creation */ 9972621Sllai1 if (!SDEV_IS_GLOBAL(parent)) { 9982621Sllai1 rw_exit(&parent->sdev_dotdot->sdev_contents); 9992621Sllai1 return (prof_lookup(dvp, nm, vpp, cred)); 10002621Sllai1 } 10012621Sllai1 rw_exit(&parent->sdev_dotdot->sdev_contents); 10022621Sllai1 10033843Sjg /* execute access is required to search the directory */ 10045331Samw if ((error = VOP_ACCESS(dvp, VEXEC, 0, cred, ct)) != 0) { 10053843Sjg return (error); 10063843Sjg } 10073843Sjg 10082621Sllai1 /* find existing name */ 10095331Samw /* XXXci - We may need to translate the C-I flags on VOP_LOOKUP */ 10105331Samw error = VOP_LOOKUP(dvp, nm, &vp, NULL, 0, NULL, cred, ct, NULL, NULL); 10112621Sllai1 if (error == 0) { 10122621Sllai1 VN_RELE(vp); 10132621Sllai1 return (EEXIST); 10142621Sllai1 } 10152621Sllai1 if (error != ENOENT) 10162621Sllai1 return (error); 10172621Sllai1 10183843Sjg /* require write access to create a directory */ 10195331Samw if ((error = VOP_ACCESS(dvp, VWRITE, 0, cred, ct)) != 0) { 10203843Sjg return (error); 10213843Sjg } 10223843Sjg 10232621Sllai1 /* put it into memory */ 10242621Sllai1 rw_enter(&parent->sdev_contents, RW_WRITER); 10252621Sllai1 error = sdev_mknode(parent, nm, &self, 10262621Sllai1 va, NULL, NULL, cred, SDEV_READY); 10272621Sllai1 if (error) { 10282621Sllai1 rw_exit(&parent->sdev_contents); 10292621Sllai1 if (self) 10302621Sllai1 SDEV_RELE(self); 10312621Sllai1 return (error); 10322621Sllai1 } 10332621Sllai1 ASSERT(self && (self->sdev_state == SDEV_READY)); 10342621Sllai1 rw_exit(&parent->sdev_contents); 10352621Sllai1 10362621Sllai1 /* take care the timestamps for the node and its parent */ 10372621Sllai1 sdev_update_timestamps(SDEVTOV(self), kcred, 10382621Sllai1 AT_CTIME|AT_MTIME|AT_ATIME); 10392621Sllai1 sdev_update_timestamps(dvp, kcred, AT_MTIME|AT_ATIME); 10402621Sllai1 if (SDEV_IS_GLOBAL(parent)) 10412621Sllai1 atomic_inc_ulong(&parent->sdev_gdir_gen); 10422621Sllai1 10432621Sllai1 /* wake up other threads blocked on looking up this node */ 10442621Sllai1 mutex_enter(&self->sdev_lookup_lock); 10452621Sllai1 SDEV_UNBLOCK_OTHERS(self, SDEV_LOOKUP); 10462621Sllai1 mutex_exit(&self->sdev_lookup_lock); 10472621Sllai1 *vpp = SDEVTOV(self); 10482621Sllai1 return (0); 10492621Sllai1 } 10502621Sllai1 10512621Sllai1 /* 10522621Sllai1 * allowing removing an empty directory under /dev 10532621Sllai1 */ 10542621Sllai1 /*ARGSUSED*/ 10552621Sllai1 static int 10565331Samw sdev_rmdir(struct vnode *dvp, char *nm, struct vnode *cdir, struct cred *cred, 10575331Samw caller_context_t *ct, int flags) 10582621Sllai1 { 10592621Sllai1 int error = 0; 10602621Sllai1 struct sdev_node *parent = (struct sdev_node *)VTOSDEV(dvp); 10612621Sllai1 struct sdev_node *self = NULL; 10622621Sllai1 struct vnode *vp = NULL; 10632621Sllai1 10642621Sllai1 /* bail out early */ 10652621Sllai1 if (strcmp(nm, ".") == 0) 10662621Sllai1 return (EINVAL); 10672621Sllai1 if (strcmp(nm, "..") == 0) 10682621Sllai1 return (EEXIST); /* should be ENOTEMPTY */ 10692621Sllai1 10702621Sllai1 /* no destruction of non-global node */ 10712621Sllai1 ASSERT(parent && parent->sdev_dotdot); 10722621Sllai1 rw_enter(&parent->sdev_dotdot->sdev_contents, RW_READER); 10732621Sllai1 if (!SDEV_IS_GLOBAL(parent)) { 10742621Sllai1 rw_exit(&parent->sdev_dotdot->sdev_contents); 10752621Sllai1 return (ENOTSUP); 10762621Sllai1 } 10772621Sllai1 rw_exit(&parent->sdev_dotdot->sdev_contents); 10782621Sllai1 10793843Sjg /* execute access is required to search the directory */ 10805331Samw if ((error = VOP_ACCESS(dvp, VEXEC, 0, cred, ct)) != 0) 10813843Sjg return (error); 10823843Sjg 10832621Sllai1 /* check existing name */ 10842621Sllai1 rw_enter(&parent->sdev_contents, RW_WRITER); 10852621Sllai1 self = sdev_cache_lookup(parent, nm); 10862621Sllai1 if (self == NULL) { 10872621Sllai1 rw_exit(&parent->sdev_contents); 10882621Sllai1 return (ENOENT); 10892621Sllai1 } 10902621Sllai1 10912621Sllai1 vp = SDEVTOV(self); 10922621Sllai1 if ((self->sdev_state == SDEV_INIT) || 10932621Sllai1 (self->sdev_state == SDEV_ZOMBIE)) { 10942621Sllai1 rw_exit(&parent->sdev_contents); 10952621Sllai1 VN_RELE(vp); 10962621Sllai1 return (ENOENT); 10972621Sllai1 } 10982621Sllai1 10993843Sjg /* write access is required to remove a directory */ 11005331Samw if ((error = VOP_ACCESS(dvp, VWRITE, 0, cred, ct)) != 0) { 11013843Sjg rw_exit(&parent->sdev_contents); 11023843Sjg VN_RELE(vp); 11033843Sjg return (error); 11043843Sjg } 11053843Sjg 11062621Sllai1 /* some sanity checks */ 11072621Sllai1 if (vp == dvp || vp == cdir) { 11082621Sllai1 rw_exit(&parent->sdev_contents); 11092621Sllai1 VN_RELE(vp); 11102621Sllai1 return (EINVAL); 11112621Sllai1 } 11122621Sllai1 11132621Sllai1 if (vp->v_type != VDIR) { 11142621Sllai1 rw_exit(&parent->sdev_contents); 11152621Sllai1 VN_RELE(vp); 11162621Sllai1 return (ENOTDIR); 11172621Sllai1 } 11182621Sllai1 11192621Sllai1 if (vn_vfswlock(vp)) { 11202621Sllai1 rw_exit(&parent->sdev_contents); 11212621Sllai1 VN_RELE(vp); 11222621Sllai1 return (EBUSY); 11232621Sllai1 } 11242621Sllai1 11252621Sllai1 if (vn_mountedvfs(vp) != NULL) { 11262621Sllai1 rw_exit(&parent->sdev_contents); 11272621Sllai1 vn_vfsunlock(vp); 11282621Sllai1 VN_RELE(vp); 11292621Sllai1 return (EBUSY); 11302621Sllai1 } 11312621Sllai1 11322621Sllai1 self = VTOSDEV(vp); 11332621Sllai1 /* bail out on a non-empty directory */ 11342621Sllai1 rw_enter(&self->sdev_contents, RW_READER); 11352621Sllai1 if (self->sdev_nlink > 2) { 11362621Sllai1 rw_exit(&self->sdev_contents); 11372621Sllai1 rw_exit(&parent->sdev_contents); 11382621Sllai1 vn_vfsunlock(vp); 11392621Sllai1 VN_RELE(vp); 11402621Sllai1 return (ENOTEMPTY); 11412621Sllai1 } 11422621Sllai1 rw_exit(&self->sdev_contents); 11432621Sllai1 11442621Sllai1 /* unlink it from the directory cache */ 11452621Sllai1 error = sdev_cache_update(parent, &self, nm, SDEV_CACHE_DELETE); 11462621Sllai1 rw_exit(&parent->sdev_contents); 11472621Sllai1 vn_vfsunlock(vp); 11482621Sllai1 11492621Sllai1 if (error && (error != EBUSY)) { 11502621Sllai1 VN_RELE(vp); 11512621Sllai1 } else { 11522621Sllai1 sdcmn_err2(("sdev_rmdir: cleaning node %s from directory " 11532621Sllai1 " cache with error %d\n", nm, error)); 11542621Sllai1 11552621Sllai1 /* best effort to clean up the backing store */ 11562621Sllai1 if (SDEV_IS_PERSIST(parent)) { 11572621Sllai1 ASSERT(parent->sdev_attrvp); 11582621Sllai1 error = VOP_RMDIR(parent->sdev_attrvp, nm, 11595331Samw parent->sdev_attrvp, kcred, ct, flags); 11602621Sllai1 sdcmn_err2(("sdev_rmdir: cleaning device %s is on" 11612621Sllai1 " disk error %d\n", parent->sdev_path, error)); 11622621Sllai1 } 11632621Sllai1 11642621Sllai1 if (error == EBUSY) 11652621Sllai1 error = 0; 11662621Sllai1 } 11672621Sllai1 11682621Sllai1 return (error); 11692621Sllai1 } 11702621Sllai1 11712621Sllai1 /* 11722621Sllai1 * read the contents of a symbolic link 11732621Sllai1 */ 11742621Sllai1 static int 11755331Samw sdev_readlink(struct vnode *vp, struct uio *uiop, struct cred *cred, 11765331Samw caller_context_t *ct) 11772621Sllai1 { 11782621Sllai1 struct sdev_node *dv; 11792621Sllai1 int error = 0; 11802621Sllai1 11812621Sllai1 ASSERT(vp->v_type == VLNK); 11822621Sllai1 11832621Sllai1 dv = VTOSDEV(vp); 11842621Sllai1 11852621Sllai1 if (dv->sdev_attrvp) { 11862621Sllai1 /* non-NULL attrvp implys a persisted node at READY state */ 11875331Samw return (VOP_READLINK(dv->sdev_attrvp, uiop, cred, ct)); 11882621Sllai1 } else if (dv->sdev_symlink != NULL) { 11892621Sllai1 /* memory nodes, e.g. local nodes */ 11902621Sllai1 rw_enter(&dv->sdev_contents, RW_READER); 11912621Sllai1 sdcmn_err2(("sdev_readlink link is %s\n", dv->sdev_symlink)); 11922621Sllai1 error = uiomove(dv->sdev_symlink, strlen(dv->sdev_symlink), 11932621Sllai1 UIO_READ, uiop); 11942621Sllai1 rw_exit(&dv->sdev_contents); 11952621Sllai1 return (error); 11962621Sllai1 } 11972621Sllai1 11982621Sllai1 return (ENOENT); 11992621Sllai1 } 12002621Sllai1 12015331Samw /*ARGSUSED4*/ 12022621Sllai1 static int 12035331Samw sdev_readdir(struct vnode *dvp, struct uio *uiop, struct cred *cred, int *eofp, 12045331Samw caller_context_t *ct, int flags) 12052621Sllai1 { 12062679Sszhou struct sdev_node *parent = VTOSDEV(dvp); 12073843Sjg int error; 12083843Sjg 12093843Sjg /* execute access is required to search the directory */ 12105331Samw if ((error = VOP_ACCESS(dvp, VEXEC, 0, cred, ct)) != 0) 12113843Sjg return (error); 12122679Sszhou 12132679Sszhou ASSERT(parent); 12142679Sszhou if (!SDEV_IS_GLOBAL(parent)) 12152679Sszhou prof_filldir(parent); 12162621Sllai1 return (devname_readdir_func(dvp, uiop, cred, eofp, SDEV_BROWSE)); 12172621Sllai1 } 12182621Sllai1 12192621Sllai1 /*ARGSUSED1*/ 12202621Sllai1 static void 12215331Samw sdev_inactive(struct vnode *vp, struct cred *cred, caller_context_t *ct) 12222621Sllai1 { 12235895Syz147064 devname_inactive_func(vp, cred, NULL); 12242621Sllai1 } 12252621Sllai1 12265331Samw /*ARGSUSED2*/ 12272621Sllai1 static int 12285331Samw sdev_fid(struct vnode *vp, struct fid *fidp, caller_context_t *ct) 12292621Sllai1 { 12302621Sllai1 struct sdev_node *dv = VTOSDEV(vp); 12312621Sllai1 struct sdev_fid *sdev_fid; 12322621Sllai1 12332621Sllai1 if (fidp->fid_len < (sizeof (struct sdev_fid) - sizeof (ushort_t))) { 12342621Sllai1 fidp->fid_len = sizeof (struct sdev_fid) - sizeof (ushort_t); 12352621Sllai1 return (ENOSPC); 12362621Sllai1 } 12372621Sllai1 12382621Sllai1 sdev_fid = (struct sdev_fid *)fidp; 12392621Sllai1 bzero(sdev_fid, sizeof (struct sdev_fid)); 12402621Sllai1 sdev_fid->sdevfid_len = 12412621Sllai1 (int)sizeof (struct sdev_fid) - sizeof (ushort_t); 12422621Sllai1 sdev_fid->sdevfid_ino = dv->sdev_ino; 12432621Sllai1 12442621Sllai1 return (0); 12452621Sllai1 } 12462621Sllai1 12472621Sllai1 /* 12482621Sllai1 * This pair of routines bracket all VOP_READ, VOP_WRITE 12492621Sllai1 * and VOP_READDIR requests. The contents lock stops things 12502621Sllai1 * moving around while we're looking at them. 12512621Sllai1 */ 12523748Sjg /*ARGSUSED2*/ 12533748Sjg static int 12543748Sjg sdev_rwlock(struct vnode *vp, int write_flag, caller_context_t *ctp) 12552621Sllai1 { 12563748Sjg rw_enter(&VTOSDEV(vp)->sdev_contents, 12573748Sjg write_flag ? RW_WRITER : RW_READER); 12583748Sjg return (write_flag ? V_WRITELOCK_TRUE : V_WRITELOCK_FALSE); 12592621Sllai1 } 12602621Sllai1 12612621Sllai1 /*ARGSUSED1*/ 12622621Sllai1 static void 12633748Sjg sdev_rwunlock(struct vnode *vp, int write_flag, caller_context_t *ctp) 12642621Sllai1 { 12652621Sllai1 rw_exit(&VTOSDEV(vp)->sdev_contents); 12662621Sllai1 } 12672621Sllai1 12682621Sllai1 /*ARGSUSED1*/ 12692621Sllai1 static int 12705331Samw sdev_seek(struct vnode *vp, offset_t ooff, offset_t *noffp, 12715331Samw caller_context_t *ct) 12722621Sllai1 { 12732621Sllai1 struct vnode *attrvp = VTOSDEV(vp)->sdev_attrvp; 12742621Sllai1 12752621Sllai1 ASSERT(vp->v_type != VCHR && 12762621Sllai1 vp->v_type != VBLK && vp->v_type != VLNK); 12772621Sllai1 12782621Sllai1 if (vp->v_type == VDIR) 12795331Samw return (fs_seek(vp, ooff, noffp, ct)); 12802621Sllai1 12812621Sllai1 ASSERT(attrvp); 12825331Samw return (VOP_SEEK(attrvp, ooff, noffp, ct)); 12832621Sllai1 } 12842621Sllai1 12852621Sllai1 /*ARGSUSED1*/ 12862621Sllai1 static int 12872621Sllai1 sdev_frlock(struct vnode *vp, int cmd, struct flock64 *bfp, int flag, 12885331Samw offset_t offset, struct flk_callback *flk_cbp, struct cred *cr, 12895331Samw caller_context_t *ct) 12902621Sllai1 { 12912621Sllai1 int error; 12922621Sllai1 struct sdev_node *dv = VTOSDEV(vp); 12932621Sllai1 12942621Sllai1 ASSERT(dv); 12952621Sllai1 ASSERT(dv->sdev_attrvp); 12962621Sllai1 error = VOP_FRLOCK(dv->sdev_attrvp, cmd, bfp, flag, offset, 12975331Samw flk_cbp, cr, ct); 12982621Sllai1 12992621Sllai1 return (error); 13002621Sllai1 } 13012621Sllai1 13022621Sllai1 static int 13035331Samw sdev_setfl(struct vnode *vp, int oflags, int nflags, cred_t *cr, 13045331Samw caller_context_t *ct) 13052621Sllai1 { 13062621Sllai1 struct sdev_node *dv = VTOSDEV(vp); 13072621Sllai1 ASSERT(dv); 13082621Sllai1 ASSERT(dv->sdev_attrvp); 13092621Sllai1 13105331Samw return (VOP_SETFL(dv->sdev_attrvp, oflags, nflags, cr, ct)); 13112621Sllai1 } 13122621Sllai1 13132621Sllai1 static int 13145331Samw sdev_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr, 13155331Samw caller_context_t *ct) 13162621Sllai1 { 13172621Sllai1 switch (cmd) { 13182621Sllai1 case _PC_ACL_ENABLED: 13192621Sllai1 *valp = SDEV_ACL_FLAVOR(vp); 13202621Sllai1 return (0); 13212621Sllai1 } 13222621Sllai1 13235331Samw return (fs_pathconf(vp, cmd, valp, cr, ct)); 13242621Sllai1 } 13252621Sllai1 13262621Sllai1 vnodeops_t *sdev_vnodeops; 13272621Sllai1 13282621Sllai1 const fs_operation_def_t sdev_vnodeops_tbl[] = { 13293898Srsb VOPNAME_OPEN, { .vop_open = sdev_open }, 13303898Srsb VOPNAME_CLOSE, { .vop_close = sdev_close }, 13313898Srsb VOPNAME_READ, { .vop_read = sdev_read }, 13323898Srsb VOPNAME_WRITE, { .vop_write = sdev_write }, 13333898Srsb VOPNAME_IOCTL, { .vop_ioctl = sdev_ioctl }, 13343898Srsb VOPNAME_GETATTR, { .vop_getattr = sdev_getattr }, 13353898Srsb VOPNAME_SETATTR, { .vop_setattr = sdev_setattr }, 13363898Srsb VOPNAME_ACCESS, { .vop_access = sdev_access }, 13373898Srsb VOPNAME_LOOKUP, { .vop_lookup = sdev_lookup }, 13383898Srsb VOPNAME_CREATE, { .vop_create = sdev_create }, 13393898Srsb VOPNAME_RENAME, { .vop_rename = sdev_rename }, 13403898Srsb VOPNAME_REMOVE, { .vop_remove = sdev_remove }, 13413898Srsb VOPNAME_MKDIR, { .vop_mkdir = sdev_mkdir }, 13423898Srsb VOPNAME_RMDIR, { .vop_rmdir = sdev_rmdir }, 13433898Srsb VOPNAME_READDIR, { .vop_readdir = sdev_readdir }, 13443898Srsb VOPNAME_SYMLINK, { .vop_symlink = sdev_symlink }, 13453898Srsb VOPNAME_READLINK, { .vop_readlink = sdev_readlink }, 13463898Srsb VOPNAME_INACTIVE, { .vop_inactive = sdev_inactive }, 13473898Srsb VOPNAME_FID, { .vop_fid = sdev_fid }, 13483898Srsb VOPNAME_RWLOCK, { .vop_rwlock = sdev_rwlock }, 13493898Srsb VOPNAME_RWUNLOCK, { .vop_rwunlock = sdev_rwunlock }, 13503898Srsb VOPNAME_SEEK, { .vop_seek = sdev_seek }, 13513898Srsb VOPNAME_FRLOCK, { .vop_frlock = sdev_frlock }, 13523898Srsb VOPNAME_PATHCONF, { .vop_pathconf = sdev_pathconf }, 13533898Srsb VOPNAME_SETFL, { .vop_setfl = sdev_setfl }, 13543898Srsb VOPNAME_SETSECATTR, { .vop_setsecattr = sdev_setsecattr }, 13553898Srsb VOPNAME_GETSECATTR, { .vop_getsecattr = sdev_getsecattr }, 13563898Srsb NULL, NULL 13572621Sllai1 }; 13582621Sllai1 13592621Sllai1 int sdev_vnodeops_tbl_size = sizeof (sdev_vnodeops_tbl); 1360