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 /* 222621Sllai1 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 232621Sllai1 * Use is subject to license terms. 242621Sllai1 */ 252621Sllai1 262621Sllai1 #pragma ident "%Z%%M% %I% %E% SMI" 272621Sllai1 282621Sllai1 /* 292621Sllai1 * vnode ops for the /dev filesystem 302621Sllai1 * 312621Sllai1 * - VDIR, VCHR, CBLK, and VLNK are considered must supported files 322621Sllai1 * - VREG and VDOOR are used for some internal implementations in 332621Sllai1 * the global zone, e.g. devname and devfsadm communication 342621Sllai1 * - other file types are unusual in this namespace and 352621Sllai1 * not supported for now 362621Sllai1 */ 372621Sllai1 382621Sllai1 #include <sys/types.h> 392621Sllai1 #include <sys/param.h> 402621Sllai1 #include <sys/t_lock.h> 412621Sllai1 #include <sys/systm.h> 422621Sllai1 #include <sys/sysmacros.h> 432621Sllai1 #include <sys/user.h> 442621Sllai1 #include <sys/time.h> 452621Sllai1 #include <sys/vfs.h> 462621Sllai1 #include <sys/vnode.h> 472621Sllai1 #include <sys/file.h> 482621Sllai1 #include <sys/fcntl.h> 492621Sllai1 #include <sys/flock.h> 502621Sllai1 #include <sys/kmem.h> 512621Sllai1 #include <sys/uio.h> 522621Sllai1 #include <sys/errno.h> 532621Sllai1 #include <sys/stat.h> 542621Sllai1 #include <sys/cred.h> 552621Sllai1 #include <sys/cred_impl.h> 562621Sllai1 #include <sys/dirent.h> 572621Sllai1 #include <sys/pathname.h> 582621Sllai1 #include <sys/cmn_err.h> 592621Sllai1 #include <sys/debug.h> 602621Sllai1 #include <sys/policy.h> 612621Sllai1 #include <vm/hat.h> 622621Sllai1 #include <vm/seg_vn.h> 632621Sllai1 #include <vm/seg_map.h> 642621Sllai1 #include <vm/seg.h> 652621Sllai1 #include <vm/as.h> 662621Sllai1 #include <vm/page.h> 672621Sllai1 #include <sys/proc.h> 682621Sllai1 #include <sys/mode.h> 692621Sllai1 #include <sys/sunndi.h> 702621Sllai1 #include <sys/ptms.h> 712621Sllai1 #include <fs/fs_subr.h> 722621Sllai1 #include <sys/fs/dv_node.h> 732621Sllai1 #include <sys/fs/sdev_impl.h> 742621Sllai1 #include <sys/fs/sdev_node.h> 752621Sllai1 762621Sllai1 /*ARGSUSED*/ 772621Sllai1 static int 782621Sllai1 sdev_open(struct vnode **vpp, int flag, struct cred *cred) 792621Sllai1 { 802621Sllai1 struct sdev_node *dv = VTOSDEV(*vpp); 812621Sllai1 struct sdev_node *ddv = dv->sdev_dotdot; 822621Sllai1 int error = 0; 832621Sllai1 842621Sllai1 if ((*vpp)->v_type == VDIR) 852621Sllai1 return (0); 862621Sllai1 872621Sllai1 if (!SDEV_IS_GLOBAL(dv)) 882621Sllai1 return (ENOTSUP); 892621Sllai1 902621Sllai1 ASSERT((*vpp)->v_type == VREG); 912621Sllai1 if ((*vpp)->v_type != VREG) 922621Sllai1 return (ENOTSUP); 932621Sllai1 942621Sllai1 ASSERT(ddv); 952621Sllai1 rw_enter(&ddv->sdev_contents, RW_READER); 962621Sllai1 if (dv->sdev_attrvp == NULL) { 972621Sllai1 rw_exit(&ddv->sdev_contents); 982621Sllai1 return (ENOENT); 992621Sllai1 } 1002621Sllai1 error = VOP_OPEN(&(dv->sdev_attrvp), flag, cred); 1012621Sllai1 rw_exit(&ddv->sdev_contents); 1022621Sllai1 return (error); 1032621Sllai1 } 1042621Sllai1 1052621Sllai1 /*ARGSUSED1*/ 1062621Sllai1 static int 1072621Sllai1 sdev_close(struct vnode *vp, int flag, int count, 1082621Sllai1 offset_t offset, struct cred *cred) 1092621Sllai1 { 1102621Sllai1 struct sdev_node *dv = VTOSDEV(vp); 1112621Sllai1 1122621Sllai1 if (vp->v_type == VDIR) { 1132621Sllai1 cleanlocks(vp, ttoproc(curthread)->p_pid, 0); 1142621Sllai1 cleanshares(vp, ttoproc(curthread)->p_pid); 1152621Sllai1 return (0); 1162621Sllai1 } 1172621Sllai1 1182621Sllai1 if (!SDEV_IS_GLOBAL(dv)) 1192621Sllai1 return (ENOTSUP); 1202621Sllai1 1212621Sllai1 ASSERT(vp->v_type == VREG); 1222621Sllai1 if (vp->v_type != VREG) 1232621Sllai1 return (ENOTSUP); 1242621Sllai1 1252621Sllai1 ASSERT(dv->sdev_attrvp); 1262621Sllai1 return (VOP_CLOSE(dv->sdev_attrvp, flag, count, offset, cred)); 1272621Sllai1 } 1282621Sllai1 1292621Sllai1 /*ARGSUSED*/ 1302621Sllai1 static int 1312621Sllai1 sdev_read(struct vnode *vp, struct uio *uio, int ioflag, struct cred *cred, 1322621Sllai1 struct caller_context *ct) 1332621Sllai1 { 1342621Sllai1 struct sdev_node *dv = (struct sdev_node *)VTOSDEV(vp); 1352621Sllai1 int error; 1362621Sllai1 1372621Sllai1 if (!SDEV_IS_GLOBAL(dv)) 1382621Sllai1 return (EINVAL); 1392621Sllai1 1402621Sllai1 if (vp->v_type == VDIR) 1412621Sllai1 return (EISDIR); 1422621Sllai1 1432621Sllai1 /* only supporting regular files in /dev */ 1442621Sllai1 ASSERT(vp->v_type == VREG); 1452621Sllai1 if (vp->v_type != VREG) 1462621Sllai1 return (EINVAL); 1472621Sllai1 1482621Sllai1 ASSERT(RW_READ_HELD(&VTOSDEV(vp)->sdev_contents)); 1492621Sllai1 ASSERT(dv->sdev_attrvp); 1502621Sllai1 (void) VOP_RWLOCK(dv->sdev_attrvp, 0, NULL); 1512621Sllai1 error = VOP_READ(dv->sdev_attrvp, uio, ioflag, cred, ct); 1522621Sllai1 VOP_RWUNLOCK(dv->sdev_attrvp, 0, NULL); 1532621Sllai1 return (error); 1542621Sllai1 } 1552621Sllai1 1562621Sllai1 /*ARGSUSED*/ 1572621Sllai1 static int 1582621Sllai1 sdev_write(struct vnode *vp, struct uio *uio, int ioflag, struct cred *cred, 1592621Sllai1 struct caller_context *ct) 1602621Sllai1 { 1612621Sllai1 struct sdev_node *dv = VTOSDEV(vp); 1622621Sllai1 int error = 0; 1632621Sllai1 1642621Sllai1 if (!SDEV_IS_GLOBAL(dv)) 1652621Sllai1 return (EINVAL); 1662621Sllai1 1672621Sllai1 if (vp->v_type == VDIR) 1682621Sllai1 return (EISDIR); 1692621Sllai1 1702621Sllai1 /* only supporting regular files in /dev */ 1712621Sllai1 ASSERT(vp->v_type == VREG); 1722621Sllai1 if (vp->v_type != VREG) 1732621Sllai1 return (EINVAL); 1742621Sllai1 1752621Sllai1 ASSERT(dv->sdev_attrvp); 1762621Sllai1 1772621Sllai1 (void) VOP_RWLOCK(dv->sdev_attrvp, 1, NULL); 1782621Sllai1 error = VOP_WRITE(dv->sdev_attrvp, uio, ioflag, cred, ct); 1792621Sllai1 VOP_RWUNLOCK(dv->sdev_attrvp, 1, NULL); 1802621Sllai1 if (error == 0) { 1812621Sllai1 sdev_update_timestamps(dv->sdev_attrvp, kcred, 1822621Sllai1 AT_MTIME); 1832621Sllai1 } 1842621Sllai1 return (error); 1852621Sllai1 } 1862621Sllai1 1872621Sllai1 /*ARGSUSED*/ 1882621Sllai1 static int 1892621Sllai1 sdev_ioctl(struct vnode *vp, int cmd, intptr_t arg, int flag, 1902621Sllai1 struct cred *cred, int *rvalp) 1912621Sllai1 { 1922621Sllai1 struct sdev_node *dv = VTOSDEV(vp); 1932621Sllai1 1942621Sllai1 if (!SDEV_IS_GLOBAL(dv) || (vp->v_type == VDIR)) 1952621Sllai1 return (ENOTTY); 1962621Sllai1 1972621Sllai1 ASSERT(vp->v_type == VREG); 1982621Sllai1 if (vp->v_type != VREG) 1992621Sllai1 return (EINVAL); 2002621Sllai1 2012621Sllai1 ASSERT(dv->sdev_attrvp); 2022621Sllai1 return (VOP_IOCTL(dv->sdev_attrvp, cmd, arg, flag, cred, rvalp)); 2032621Sllai1 } 2042621Sllai1 2052621Sllai1 static int 2062621Sllai1 sdev_getattr(struct vnode *vp, struct vattr *vap, int flags, struct cred *cr) 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); 2312621Sllai1 error = VOP_GETATTR(dv->sdev_attrvp, vap, flags, cr); 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 2472621Sllai1 static int 2482621Sllai1 sdev_setattr(struct vnode *vp, struct vattr *vap, int flags, struct cred *cred) 2492621Sllai1 { 2502621Sllai1 return (devname_setattr_func(vp, vap, flags, cred, NULL, 0)); 2512621Sllai1 } 2522621Sllai1 2532621Sllai1 static int 2542621Sllai1 sdev_getsecattr(struct vnode *vp, struct vsecattr *vsap, int flags, 2552621Sllai1 struct cred *cr) 2562621Sllai1 { 2572621Sllai1 int error; 2582621Sllai1 struct sdev_node *dv = VTOSDEV(vp); 2592621Sllai1 struct vnode *avp = dv->sdev_attrvp; 2602621Sllai1 2612621Sllai1 if (avp == NULL) { 2622621Sllai1 /* return fs_fab_acl() if flavor matches, else do nothing */ 2632621Sllai1 if ((SDEV_ACL_FLAVOR(vp) == _ACL_ACLENT_ENABLED && 2642621Sllai1 (vsap->vsa_mask & (VSA_ACLCNT | VSA_DFACLCNT))) || 2652621Sllai1 (SDEV_ACL_FLAVOR(vp) == _ACL_ACE_ENABLED && 2662621Sllai1 (vsap->vsa_mask & (VSA_ACECNT | VSA_ACE)))) 2672621Sllai1 return (fs_fab_acl(vp, vsap, flags, cr)); 2682621Sllai1 2692621Sllai1 return (ENOSYS); 2702621Sllai1 } 2712621Sllai1 2722621Sllai1 (void) VOP_RWLOCK(avp, 1, NULL); 2732621Sllai1 error = VOP_GETSECATTR(avp, vsap, flags, cr); 2742621Sllai1 VOP_RWUNLOCK(avp, 1, NULL); 2752621Sllai1 return (error); 2762621Sllai1 } 2772621Sllai1 2782621Sllai1 static int 2792621Sllai1 sdev_setsecattr(struct vnode *vp, struct vsecattr *vsap, int flags, 2802621Sllai1 struct cred *cr) 2812621Sllai1 { 2822621Sllai1 int error; 2832621Sllai1 struct sdev_node *dv = VTOSDEV(vp); 2842621Sllai1 struct vnode *avp = dv->sdev_attrvp; 2852621Sllai1 2862621Sllai1 if (dv->sdev_state == SDEV_ZOMBIE) 2872621Sllai1 return (0); 2882621Sllai1 2892621Sllai1 if (avp == NULL) { 2902621Sllai1 if (SDEV_IS_GLOBAL(dv) && !SDEV_IS_PERSIST(dv)) 2912621Sllai1 return (fs_nosys()); 2922621Sllai1 ASSERT(dv->sdev_attr); 2932621Sllai1 /* 2942621Sllai1 * if coming in directly, the acl system call will 2952621Sllai1 * have held the read-write lock via VOP_RWLOCK() 2962621Sllai1 * If coming in via specfs, specfs will have 2972621Sllai1 * held the rw lock on the realvp i.e. us. 2982621Sllai1 */ 2992621Sllai1 ASSERT(RW_WRITE_HELD(&dv->sdev_contents)); 3002621Sllai1 sdev_vattr_merge(dv, dv->sdev_attr); 3012621Sllai1 error = sdev_shadow_node(dv, cr); 3022621Sllai1 if (error) { 3032621Sllai1 return (fs_nosys()); 3042621Sllai1 } 3052621Sllai1 3062621Sllai1 ASSERT(dv->sdev_attrvp); 3072621Sllai1 /* clean out the memory copy if any */ 3082621Sllai1 if (dv->sdev_attr) { 3092621Sllai1 kmem_free(dv->sdev_attr, sizeof (struct vattr)); 3102621Sllai1 dv->sdev_attr = NULL; 3112621Sllai1 } 3122621Sllai1 avp = dv->sdev_attrvp; 3132621Sllai1 } 3142621Sllai1 ASSERT(avp); 3152621Sllai1 3162621Sllai1 (void) VOP_RWLOCK(avp, V_WRITELOCK_TRUE, NULL); 3172621Sllai1 error = VOP_SETSECATTR(avp, vsap, flags, cr); 3182621Sllai1 VOP_RWUNLOCK(avp, V_WRITELOCK_TRUE, NULL); 3192621Sllai1 return (error); 3202621Sllai1 } 3212621Sllai1 3222621Sllai1 int 3232621Sllai1 sdev_unlocked_access(void *vdv, int mode, struct cred *cr) 3242621Sllai1 { 3252621Sllai1 struct sdev_node *dv = vdv; 3262621Sllai1 int shift = 0; 3272621Sllai1 uid_t owner = dv->sdev_attr->va_uid; 3282621Sllai1 3292621Sllai1 if (crgetuid(cr) != owner) { 3302621Sllai1 shift += 3; 3312621Sllai1 if (groupmember(dv->sdev_attr->va_gid, cr) == 0) 3322621Sllai1 shift += 3; 3332621Sllai1 } 3342621Sllai1 3352621Sllai1 mode &= ~(dv->sdev_attr->va_mode << shift); 3362621Sllai1 if (mode == 0) 3372621Sllai1 return (0); 3382621Sllai1 3392621Sllai1 return (secpolicy_vnode_access(cr, SDEVTOV(dv), owner, mode)); 3402621Sllai1 } 3412621Sllai1 3422621Sllai1 static int 3432621Sllai1 sdev_access(struct vnode *vp, int mode, int flags, struct cred *cr) 3442621Sllai1 { 3452621Sllai1 struct sdev_node *dv = VTOSDEV(vp); 3462621Sllai1 int ret = 0; 3472621Sllai1 3482621Sllai1 ASSERT(dv->sdev_attr || dv->sdev_attrvp); 3492621Sllai1 3502621Sllai1 if (dv->sdev_attrvp) { 3512621Sllai1 ret = VOP_ACCESS(dv->sdev_attrvp, mode, flags, cr); 3522621Sllai1 } else if (dv->sdev_attr) { 3532621Sllai1 rw_enter(&dv->sdev_contents, RW_READER); 3542621Sllai1 ret = sdev_unlocked_access(dv, mode, cr); 3552621Sllai1 if (ret) 3562621Sllai1 ret = EACCES; 3572621Sllai1 rw_exit(&dv->sdev_contents); 3582621Sllai1 } 3592621Sllai1 3602621Sllai1 return (ret); 3612621Sllai1 } 3622621Sllai1 3632621Sllai1 /* 3642621Sllai1 * Lookup 3652621Sllai1 */ 3662621Sllai1 /*ARGSUSED3*/ 3672621Sllai1 static int 3682621Sllai1 sdev_lookup(struct vnode *dvp, char *nm, struct vnode **vpp, 3692621Sllai1 struct pathname *pnp, int flags, struct vnode *rdir, struct cred *cred) 3702621Sllai1 { 3712621Sllai1 struct sdev_node *parent; 3722621Sllai1 3732621Sllai1 parent = VTOSDEV(dvp); 3742621Sllai1 ASSERT(parent); 3752621Sllai1 3762621Sllai1 if (!SDEV_IS_GLOBAL(parent)) 3772621Sllai1 return (prof_lookup(dvp, nm, vpp, cred)); 3782621Sllai1 return (devname_lookup_func(parent, nm, vpp, cred, NULL, 0)); 3792621Sllai1 } 3802621Sllai1 3812621Sllai1 /*ARGSUSED2*/ 3822621Sllai1 static int 3832621Sllai1 sdev_create(struct vnode *dvp, char *nm, struct vattr *vap, vcexcl_t excl, 3842621Sllai1 int mode, struct vnode **vpp, struct cred *cred, int flag) 3852621Sllai1 { 3862621Sllai1 struct vnode *vp = NULL; 3872621Sllai1 struct vnode *avp; 3882621Sllai1 struct sdev_node *parent; 3892621Sllai1 struct sdev_node *self = NULL; 3902621Sllai1 int error = 0; 3912621Sllai1 vtype_t type = vap->va_type; 3922621Sllai1 393*2729Sllai1 ASSERT(type != VNON && type != VBAD); 3942621Sllai1 3952621Sllai1 if ((type == VFIFO) || (type == VSOCK) || 3962621Sllai1 (type == VPROC) || (type == VPORT)) 3972621Sllai1 return (ENOTSUP); 3982621Sllai1 3992621Sllai1 parent = VTOSDEV(dvp); 4002621Sllai1 ASSERT(parent); 4012621Sllai1 4022621Sllai1 rw_enter(&parent->sdev_dotdot->sdev_contents, RW_READER); 4032621Sllai1 if (parent->sdev_state == SDEV_ZOMBIE) { 4042621Sllai1 rw_exit(&parent->sdev_dotdot->sdev_contents); 4052621Sllai1 return (ENOENT); 4062621Sllai1 } 4072621Sllai1 4082621Sllai1 /* non-global do not allow pure node creation */ 4092621Sllai1 if (!SDEV_IS_GLOBAL(parent)) { 4102621Sllai1 rw_exit(&parent->sdev_dotdot->sdev_contents); 4112621Sllai1 return (prof_lookup(dvp, nm, vpp, cred)); 4122621Sllai1 } 4132621Sllai1 rw_exit(&parent->sdev_dotdot->sdev_contents); 4142621Sllai1 4152621Sllai1 again: 4162621Sllai1 /* check existing name */ 4172621Sllai1 error = VOP_LOOKUP(dvp, nm, &vp, NULL, 0, NULL, cred); 4182621Sllai1 4192621Sllai1 /* name found */ 4202621Sllai1 if (error == 0) { 4212621Sllai1 ASSERT(vp); 4222621Sllai1 if (excl == EXCL) { 4232621Sllai1 error = EEXIST; 4242621Sllai1 } else if ((vp->v_type == VDIR) && (mode & VWRITE)) { 4252621Sllai1 /* allowing create/read-only an existing directory */ 4262621Sllai1 error = EISDIR; 4272621Sllai1 } else { 4282621Sllai1 error = VOP_ACCESS(vp, mode, flag, cred); 4292621Sllai1 } 4302621Sllai1 4312621Sllai1 if (error) { 4322621Sllai1 VN_RELE(vp); 4332621Sllai1 return (error); 4342621Sllai1 } 4352621Sllai1 4362621Sllai1 /* truncation first */ 4372621Sllai1 if ((vp->v_type == VREG) && (vap->va_mask & AT_SIZE) && 4382621Sllai1 (vap->va_size == 0)) { 4392621Sllai1 ASSERT(parent->sdev_attrvp); 4402621Sllai1 ASSERT(VTOSDEV(vp)->sdev_attrvp); 4412621Sllai1 error = VOP_CREATE(parent->sdev_attrvp, 4422621Sllai1 nm, vap, excl, mode, &avp, cred, flag); 4432621Sllai1 4442621Sllai1 if (error) { 4452621Sllai1 VN_RELE(vp); 4462621Sllai1 return (error); 4472621Sllai1 } 4482621Sllai1 } 4492621Sllai1 4502621Sllai1 sdev_update_timestamps(vp, kcred, 4512621Sllai1 AT_CTIME|AT_MTIME|AT_ATIME); 4522621Sllai1 *vpp = vp; 4532621Sllai1 return (0); 4542621Sllai1 } 4552621Sllai1 4562621Sllai1 /* bail out early */ 4572621Sllai1 if (error != ENOENT) 4582621Sllai1 return (error); 4592621Sllai1 4602621Sllai1 /* 4612621Sllai1 * For memory-based (ROFS) directory: 4622621Sllai1 * - either disallow node creation; 4632621Sllai1 * - or implement VOP_CREATE of its own 4642621Sllai1 */ 4652621Sllai1 rw_enter(&parent->sdev_contents, RW_WRITER); 4662621Sllai1 if (!SDEV_IS_PERSIST(parent)) { 4672621Sllai1 rw_exit(&parent->sdev_contents); 4682621Sllai1 return (ENOTSUP); 4692621Sllai1 } 4702621Sllai1 ASSERT(parent->sdev_attrvp); 4712621Sllai1 error = sdev_mknode(parent, nm, &self, vap, NULL, NULL, 4722621Sllai1 cred, SDEV_READY); 4732621Sllai1 if (error) { 4742621Sllai1 rw_exit(&parent->sdev_contents); 4752621Sllai1 if (self) 4762621Sllai1 SDEV_RELE(self); 4772621Sllai1 return (error); 4782621Sllai1 } 4792621Sllai1 rw_exit(&parent->sdev_contents); 4802621Sllai1 4812621Sllai1 ASSERT(self); 4822621Sllai1 /* take care the timestamps for the node and its parent */ 4832621Sllai1 sdev_update_timestamps(SDEVTOV(self), kcred, 4842621Sllai1 AT_CTIME|AT_MTIME|AT_ATIME); 4852621Sllai1 sdev_update_timestamps(dvp, kcred, AT_MTIME|AT_ATIME); 4862621Sllai1 if (SDEV_IS_GLOBAL(parent)) 4872621Sllai1 atomic_inc_ulong(&parent->sdev_gdir_gen); 4882621Sllai1 4892621Sllai1 /* wake up other threads blocked on looking up this node */ 4902621Sllai1 mutex_enter(&self->sdev_lookup_lock); 4912621Sllai1 SDEV_UNBLOCK_OTHERS(self, SDEV_LOOKUP); 4922621Sllai1 mutex_exit(&self->sdev_lookup_lock); 4932621Sllai1 error = sdev_to_vp(self, vpp); 4942621Sllai1 return (error); 4952621Sllai1 } 4962621Sllai1 4972621Sllai1 static int 4982621Sllai1 sdev_remove(struct vnode *dvp, char *nm, struct cred *cred) 4992621Sllai1 { 5002621Sllai1 int error; 5012621Sllai1 struct sdev_node *parent = (struct sdev_node *)VTOSDEV(dvp); 5022621Sllai1 struct vnode *vp = NULL; 5032621Sllai1 struct sdev_node *dv = NULL; 5042621Sllai1 struct devname_nsmap *map = NULL; 5052621Sllai1 struct devname_ops *dirops = NULL; 5062621Sllai1 int (*fn)(devname_handle_t *); 5072621Sllai1 int len; 5082621Sllai1 int bkstore = 0; 5092621Sllai1 5102621Sllai1 /* bail out early */ 5112621Sllai1 len = strlen(nm); 5122621Sllai1 if (nm[0] == '.') { 5132621Sllai1 if (len == 1) { 5142621Sllai1 return (EINVAL); 5152621Sllai1 } else if (len == 2 && nm[1] == '.') { 5162621Sllai1 return (EEXIST); 5172621Sllai1 } 5182621Sllai1 } 5192621Sllai1 5202621Sllai1 ASSERT(parent); 5212621Sllai1 rw_enter(&parent->sdev_contents, RW_READER); 5222621Sllai1 if (!SDEV_IS_GLOBAL(parent)) { 5232621Sllai1 rw_exit(&parent->sdev_contents); 5242621Sllai1 return (ENOTSUP); 5252621Sllai1 } 5262621Sllai1 5272621Sllai1 /* check existence first */ 5282621Sllai1 dv = sdev_cache_lookup(parent, nm); 5292621Sllai1 if (dv == NULL) { 5302621Sllai1 rw_exit(&parent->sdev_contents); 5312621Sllai1 return (ENOENT); 5322621Sllai1 } 5332621Sllai1 5342621Sllai1 vp = SDEVTOV(dv); 5352621Sllai1 if ((dv->sdev_state == SDEV_INIT) || 5362621Sllai1 (dv->sdev_state == SDEV_ZOMBIE)) { 5372621Sllai1 rw_exit(&parent->sdev_contents); 5382621Sllai1 VN_RELE(vp); 5392621Sllai1 return (ENOENT); 5402621Sllai1 } 5412621Sllai1 5422621Sllai1 /* the module may record/reject removing a device node */ 5432621Sllai1 map = sdev_get_map(parent, 0); 5442621Sllai1 dirops = map ? map->dir_ops : NULL; 5452621Sllai1 if (dirops && ((fn = dirops->devnops_remove) != NULL)) { 5462621Sllai1 error = (*fn)(&(dv->sdev_handle)); 5472621Sllai1 if (error) { 5482621Sllai1 rw_exit(&parent->sdev_contents); 5492621Sllai1 VN_RELE(vp); 5502621Sllai1 return (error); 5512621Sllai1 } 5522621Sllai1 } 5532621Sllai1 5542621Sllai1 /* 5552621Sllai1 * sdev_dirdelete does the real job of: 5562621Sllai1 * - make sure no open ref count 5572621Sllai1 * - destroying the sdev_node 5582621Sllai1 * - releasing the hold on attrvp 5592621Sllai1 */ 5602621Sllai1 bkstore = SDEV_IS_PERSIST(dv) ? 1 : 0; 5612621Sllai1 if (!rw_tryupgrade(&parent->sdev_contents)) { 5622621Sllai1 rw_exit(&parent->sdev_contents); 5632621Sllai1 rw_enter(&parent->sdev_contents, RW_WRITER); 5642621Sllai1 } 5652621Sllai1 error = sdev_cache_update(parent, &dv, nm, SDEV_CACHE_DELETE); 5662621Sllai1 rw_exit(&parent->sdev_contents); 5672621Sllai1 5682621Sllai1 sdcmn_err2(("sdev_remove: cache_update error %d\n", error)); 5692621Sllai1 if (error && (error != EBUSY)) { 5702621Sllai1 /* report errors other than EBUSY */ 5712621Sllai1 VN_RELE(vp); 5722621Sllai1 } else { 5732621Sllai1 sdcmn_err2(("sdev_remove: cleaning node %s from cache " 5742621Sllai1 " with error %d\n", nm, error)); 5752621Sllai1 5762621Sllai1 /* 5772621Sllai1 * best efforts clean up the backing store 5782621Sllai1 */ 5792621Sllai1 if (bkstore) { 5802621Sllai1 ASSERT(parent->sdev_attrvp); 5812621Sllai1 error = VOP_REMOVE(parent->sdev_attrvp, nm, cred); 5822621Sllai1 /* 5832621Sllai1 * do not report BUSY error 5842621Sllai1 * because the backing store ref count is released 5852621Sllai1 * when the last ref count on the sdev_node is 5862621Sllai1 * released. 5872621Sllai1 */ 5882621Sllai1 if (error == EBUSY) { 5892621Sllai1 sdcmn_err2(("sdev_remove: device %s is still on" 5902621Sllai1 "disk %s\n", nm, parent->sdev_path)); 5912621Sllai1 error = 0; 5922621Sllai1 } 5932621Sllai1 } 5942621Sllai1 5952621Sllai1 if (error == EBUSY) 5962621Sllai1 error = 0; 5972621Sllai1 } 5982621Sllai1 5992621Sllai1 return (error); 6002621Sllai1 } 6012621Sllai1 6022621Sllai1 /* 6032621Sllai1 * Some restrictions for this file system: 6042621Sllai1 * - both oldnm and newnm are in the scope of /dev file system, 6052621Sllai1 * to simply the namespace management model. 6062621Sllai1 */ 6072621Sllai1 static int 6082621Sllai1 sdev_rename(struct vnode *odvp, char *onm, struct vnode *ndvp, char *nnm, 6092621Sllai1 struct cred *cred) 6102621Sllai1 { 6112621Sllai1 struct sdev_node *fromparent = NULL; 6122621Sllai1 struct vattr vattr; 6132621Sllai1 struct sdev_node *toparent; 6142621Sllai1 struct sdev_node *fromdv = NULL; /* source node */ 615*2729Sllai1 struct vnode *ovp = NULL; /* source vnode */ 6162621Sllai1 struct sdev_node *todv = NULL; /* destination node */ 617*2729Sllai1 struct vnode *nvp = NULL; /* destination vnode */ 6182621Sllai1 int samedir = 0; /* set if odvp == ndvp */ 6192621Sllai1 struct vnode *realvp; 6202621Sllai1 int len; 6212621Sllai1 char nnm_path[MAXPATHLEN]; 6222621Sllai1 struct devname_nsmap *omap = NULL; 6232621Sllai1 struct devname_ops *odirops = NULL; 6242621Sllai1 int (*fn)(devname_handle_t *, char *); 6252621Sllai1 int (*rmfn)(devname_handle_t *); 6262621Sllai1 int error = 0; 6272621Sllai1 dev_t fsid; 6282621Sllai1 int bkstore = 0; 629*2729Sllai1 vtype_t type; 6302621Sllai1 6312621Sllai1 /* prevent modifying "." and ".." */ 6322621Sllai1 if ((onm[0] == '.' && 633*2729Sllai1 (onm[1] == '\0' || (onm[1] == '.' && onm[2] == '\0'))) || 634*2729Sllai1 (nnm[0] == '.' && 635*2729Sllai1 (nnm[1] == '\0' || (nnm[1] == '.' && nnm[2] == '\0')))) { 6362621Sllai1 return (EINVAL); 6372621Sllai1 } 6382621Sllai1 6392621Sllai1 fromparent = VTOSDEV(odvp); 6402621Sllai1 toparent = VTOSDEV(ndvp); 6412621Sllai1 6422621Sllai1 /* ZOMBIE parent doesn't allow new node creation */ 6432621Sllai1 rw_enter(&fromparent->sdev_dotdot->sdev_contents, RW_READER); 6442621Sllai1 if (fromparent->sdev_state == SDEV_ZOMBIE) { 6452621Sllai1 rw_exit(&fromparent->sdev_dotdot->sdev_contents); 6462621Sllai1 return (ENOENT); 6472621Sllai1 } 6482621Sllai1 6492621Sllai1 /* renaming only supported for global device nodes */ 6502621Sllai1 if (!SDEV_IS_GLOBAL(fromparent)) { 6512621Sllai1 rw_exit(&fromparent->sdev_dotdot->sdev_contents); 6522621Sllai1 return (ENOTSUP); 6532621Sllai1 } 6542621Sllai1 rw_exit(&fromparent->sdev_dotdot->sdev_contents); 6552621Sllai1 6562621Sllai1 rw_enter(&toparent->sdev_dotdot->sdev_contents, RW_READER); 6572621Sllai1 if (toparent->sdev_state == SDEV_ZOMBIE) { 6582621Sllai1 rw_exit(&toparent->sdev_dotdot->sdev_contents); 6592621Sllai1 return (ENOENT); 6602621Sllai1 } 6612621Sllai1 rw_exit(&toparent->sdev_dotdot->sdev_contents); 6622621Sllai1 663*2729Sllai1 /* 664*2729Sllai1 * grabbing the global lock to prevent 665*2729Sllai1 * mount/unmount/other rename activities. 666*2729Sllai1 */ 667*2729Sllai1 mutex_enter(&sdev_lock); 668*2729Sllai1 6692621Sllai1 /* check existence of the source node */ 6702621Sllai1 error = VOP_LOOKUP(odvp, onm, &ovp, NULL, 0, NULL, cred); 6712621Sllai1 if (error) { 6722621Sllai1 sdcmn_err2(("sdev_rename: the source node %s exists\n", 6732621Sllai1 onm)); 674*2729Sllai1 mutex_exit(&sdev_lock); 6752621Sllai1 return (error); 6762621Sllai1 } 6772621Sllai1 6782621Sllai1 if (VOP_REALVP(ovp, &realvp) == 0) { 6792621Sllai1 VN_HOLD(realvp); 6802621Sllai1 VN_RELE(ovp); 6812621Sllai1 ovp = realvp; 6822621Sllai1 } 6832621Sllai1 6842621Sllai1 /* check existence of destination */ 6852621Sllai1 error = VOP_LOOKUP(ndvp, nnm, &nvp, NULL, 0, NULL, cred); 6862621Sllai1 if (error && (error != ENOENT)) { 687*2729Sllai1 mutex_exit(&sdev_lock); 6882621Sllai1 VN_RELE(ovp); 6892621Sllai1 return (error); 6902621Sllai1 } 6912621Sllai1 6922621Sllai1 if (nvp && (VOP_REALVP(nvp, &realvp) == 0)) { 6932621Sllai1 VN_HOLD(realvp); 6942621Sllai1 VN_RELE(nvp); 6952621Sllai1 nvp = realvp; 6962621Sllai1 } 6972621Sllai1 6982621Sllai1 /* 699*2729Sllai1 * make sure the source and the destination are 700*2729Sllai1 * in the same dev filesystem 7012621Sllai1 */ 7022621Sllai1 if (odvp != ndvp) { 7032621Sllai1 vattr.va_mask = AT_FSID; 7042621Sllai1 if (error = VOP_GETATTR(odvp, &vattr, 0, cred)) { 705*2729Sllai1 mutex_exit(&sdev_lock); 7062621Sllai1 VN_RELE(ovp); 7072621Sllai1 return (error); 7082621Sllai1 } 7092621Sllai1 fsid = vattr.va_fsid; 7102621Sllai1 vattr.va_mask = AT_FSID; 7112621Sllai1 if (error = VOP_GETATTR(ndvp, &vattr, 0, cred)) { 712*2729Sllai1 mutex_exit(&sdev_lock); 7132621Sllai1 VN_RELE(ovp); 7142621Sllai1 return (error); 7152621Sllai1 } 7162621Sllai1 if (fsid != vattr.va_fsid) { 717*2729Sllai1 mutex_exit(&sdev_lock); 7182621Sllai1 VN_RELE(ovp); 7192621Sllai1 return (EXDEV); 7202621Sllai1 } 7212621Sllai1 } 7222621Sllai1 7232621Sllai1 /* make sure the old entry can be deleted */ 7242621Sllai1 error = VOP_ACCESS(odvp, VWRITE, 0, cred); 7252621Sllai1 if (error) { 726*2729Sllai1 mutex_exit(&sdev_lock); 7272621Sllai1 VN_RELE(ovp); 7282621Sllai1 return (error); 7292621Sllai1 } 7302621Sllai1 7312621Sllai1 /* make sure the destination allows creation */ 7322621Sllai1 samedir = (fromparent == toparent); 7332621Sllai1 if (!samedir) { 7342621Sllai1 error = VOP_ACCESS(ndvp, VEXEC|VWRITE, 0, cred); 7352621Sllai1 if (error) { 736*2729Sllai1 mutex_exit(&sdev_lock); 7372621Sllai1 VN_RELE(ovp); 7382621Sllai1 return (error); 7392621Sllai1 } 7402621Sllai1 } 7412621Sllai1 7422621Sllai1 fromdv = VTOSDEV(ovp); 7432621Sllai1 ASSERT(fromdv); 7442621Sllai1 7452621Sllai1 /* check with the plug-in modules for the source directory */ 7462621Sllai1 rw_enter(&fromparent->sdev_contents, RW_READER); 7472621Sllai1 omap = sdev_get_map(fromparent, 0); 7482621Sllai1 rw_exit(&fromparent->sdev_contents); 7492621Sllai1 odirops = omap ? omap->dir_ops : NULL; 7502621Sllai1 if (odirops && ((fn = odirops->devnops_rename) != NULL)) { 7512621Sllai1 if (samedir) { 7522621Sllai1 error = (*fn)(&(fromdv->sdev_handle), nnm); 7532621Sllai1 } else { 7542621Sllai1 len = strlen(nnm) + strlen(toparent->sdev_name) + 2; 7552621Sllai1 (void) snprintf(nnm_path, len, "%s/%s", 7562621Sllai1 toparent->sdev_name, nnm); 7572621Sllai1 error = (*fn)(&(fromdv->sdev_handle), nnm); 7582621Sllai1 } 7592621Sllai1 7602621Sllai1 if (error) { 761*2729Sllai1 mutex_exit(&sdev_lock); 762*2729Sllai1 sdcmn_err2(("sdev_rename: DBNR doesn't " 763*2729Sllai1 "allow rename, error %d", error)); 7642621Sllai1 VN_RELE(ovp); 7652621Sllai1 return (error); 7662621Sllai1 } 7672621Sllai1 } 7682621Sllai1 769*2729Sllai1 /* destination file exists */ 7702621Sllai1 if (nvp) { 771*2729Sllai1 todv = VTOSDEV(nvp); 772*2729Sllai1 ASSERT(todv); 7732621Sllai1 } 7742621Sllai1 7752621Sllai1 /* 7762621Sllai1 * link source to new target in the memory 7772621Sllai1 */ 778*2729Sllai1 error = sdev_rnmnode(fromparent, fromdv, toparent, &todv, nnm, cred); 7792621Sllai1 if (error) { 7802621Sllai1 sdcmn_err2(("sdev_rename: renaming %s to %s failed " 7812621Sllai1 " with error %d\n", onm, nnm, error)); 782*2729Sllai1 mutex_exit(&sdev_lock); 783*2729Sllai1 if (nvp) 784*2729Sllai1 VN_RELE(nvp); 7852621Sllai1 VN_RELE(ovp); 786*2729Sllai1 return (error); 787*2729Sllai1 } 788*2729Sllai1 789*2729Sllai1 /* notify the DBNR module the node is going away */ 790*2729Sllai1 if (odirops && ((rmfn = odirops->devnops_remove) != NULL)) { 791*2729Sllai1 (void) (*rmfn)(&(fromdv->sdev_handle)); 792*2729Sllai1 } 793*2729Sllai1 794*2729Sllai1 /* 795*2729Sllai1 * unlink from source 796*2729Sllai1 */ 797*2729Sllai1 rw_enter(&fromparent->sdev_contents, RW_READER); 798*2729Sllai1 fromdv = sdev_cache_lookup(fromparent, onm); 799*2729Sllai1 if (fromdv == NULL) { 800*2729Sllai1 rw_exit(&fromparent->sdev_contents); 801*2729Sllai1 mutex_exit(&sdev_lock); 802*2729Sllai1 sdcmn_err2(("sdev_rename: the source is deleted already\n")); 803*2729Sllai1 return (0); 8042621Sllai1 } 8052621Sllai1 806*2729Sllai1 if (fromdv->sdev_state == SDEV_ZOMBIE) { 807*2729Sllai1 rw_exit(&fromparent->sdev_contents); 808*2729Sllai1 mutex_exit(&sdev_lock); 809*2729Sllai1 VN_RELE(SDEVTOV(fromdv)); 810*2729Sllai1 sdcmn_err2(("sdev_rename: the source is being deleted\n")); 811*2729Sllai1 return (0); 812*2729Sllai1 } 813*2729Sllai1 rw_exit(&fromparent->sdev_contents); 814*2729Sllai1 ASSERT(SDEVTOV(fromdv) == ovp); 815*2729Sllai1 VN_RELE(ovp); 816*2729Sllai1 817*2729Sllai1 /* clean out the directory contents before it can be removed */ 818*2729Sllai1 type = SDEVTOV(fromdv)->v_type; 819*2729Sllai1 if (type == VDIR) { 820*2729Sllai1 error = sdev_cleandir(fromdv, NULL, 0); 821*2729Sllai1 sdcmn_err2(("sdev_rename: cleandir finished with %d\n", 822*2729Sllai1 error)); 823*2729Sllai1 if (error == EBUSY) 824*2729Sllai1 error = 0; 825*2729Sllai1 } 826*2729Sllai1 827*2729Sllai1 rw_enter(&fromparent->sdev_contents, RW_WRITER); 828*2729Sllai1 bkstore = SDEV_IS_PERSIST(fromdv) ? 1 : 0; 829*2729Sllai1 error = sdev_cache_update(fromparent, &fromdv, onm, 830*2729Sllai1 SDEV_CACHE_DELETE); 831*2729Sllai1 832*2729Sllai1 /* best effforts clean up the backing store */ 833*2729Sllai1 if (bkstore) { 834*2729Sllai1 ASSERT(fromparent->sdev_attrvp); 835*2729Sllai1 if (type != VDIR) { 836*2729Sllai1 error = VOP_REMOVE(fromparent->sdev_attrvp, 837*2729Sllai1 onm, kcred); 838*2729Sllai1 } else { 839*2729Sllai1 error = VOP_RMDIR(fromparent->sdev_attrvp, 840*2729Sllai1 onm, fromparent->sdev_attrvp, kcred); 841*2729Sllai1 } 842*2729Sllai1 843*2729Sllai1 if (error) { 844*2729Sllai1 sdcmn_err2(("sdev_rename: device %s is " 845*2729Sllai1 "still on disk %s\n", onm, 846*2729Sllai1 fromparent->sdev_path)); 847*2729Sllai1 error = 0; 848*2729Sllai1 } 849*2729Sllai1 } 850*2729Sllai1 rw_exit(&fromparent->sdev_contents); 851*2729Sllai1 mutex_exit(&sdev_lock); 852*2729Sllai1 853*2729Sllai1 /* once reached to this point, the rename is regarded successful */ 854*2729Sllai1 return (0); 8552621Sllai1 } 8562621Sllai1 8572621Sllai1 /* 8582621Sllai1 * dev-fs version of "ln -s path dev-name" 8592621Sllai1 * tnm - path, e.g. /devices/... or /dev/... 8602621Sllai1 * lnm - dev_name 8612621Sllai1 */ 8622621Sllai1 static int 8632621Sllai1 sdev_symlink(struct vnode *dvp, char *lnm, struct vattr *tva, 8642621Sllai1 char *tnm, struct cred *cred) 8652621Sllai1 { 8662621Sllai1 int error; 8672621Sllai1 struct vnode *vp = NULL; 8682621Sllai1 struct sdev_node *parent = (struct sdev_node *)VTOSDEV(dvp); 8692621Sllai1 struct sdev_node *self = (struct sdev_node *)NULL; 8702621Sllai1 8712621Sllai1 ASSERT(parent); 8722621Sllai1 rw_enter(&parent->sdev_dotdot->sdev_contents, RW_READER); 8732621Sllai1 if (parent->sdev_state == SDEV_ZOMBIE) { 8742621Sllai1 rw_exit(&parent->sdev_dotdot->sdev_contents); 8752621Sllai1 sdcmn_err2(("sdev_symlink: parent %s is ZOMBIED \n", 8762621Sllai1 parent->sdev_name)); 8772621Sllai1 return (ENOENT); 8782621Sllai1 } 8792621Sllai1 8802621Sllai1 if (!SDEV_IS_GLOBAL(parent)) { 8812621Sllai1 rw_exit(&parent->sdev_dotdot->sdev_contents); 8822621Sllai1 return (ENOTSUP); 8832621Sllai1 } 8842621Sllai1 rw_exit(&parent->sdev_dotdot->sdev_contents); 8852621Sllai1 8862621Sllai1 /* find existing name */ 8872621Sllai1 error = VOP_LOOKUP(dvp, lnm, &vp, NULL, 0, NULL, cred); 8882621Sllai1 if (error == 0) { 8892621Sllai1 ASSERT(vp); 8902621Sllai1 VN_RELE(vp); 8912621Sllai1 sdcmn_err2(("sdev_symlink: node %s already exists\n", lnm)); 8922621Sllai1 return (EEXIST); 8932621Sllai1 } 8942621Sllai1 8952621Sllai1 if (error != ENOENT) { 8962621Sllai1 return (error); 8972621Sllai1 } 8982621Sllai1 8992621Sllai1 /* put it into memory cache */ 9002621Sllai1 rw_enter(&parent->sdev_contents, RW_WRITER); 9012621Sllai1 error = sdev_mknode(parent, lnm, &self, tva, NULL, (void *)tnm, 9022621Sllai1 cred, SDEV_READY); 9032621Sllai1 if (error) { 9042621Sllai1 rw_exit(&parent->sdev_contents); 9052621Sllai1 sdcmn_err2(("sdev_symlink: node %s creation failed\n", lnm)); 9062621Sllai1 if (self) 9072621Sllai1 SDEV_RELE(self); 9082621Sllai1 9092621Sllai1 return (error); 9102621Sllai1 } 9112621Sllai1 ASSERT(self && (self->sdev_state == SDEV_READY)); 9122621Sllai1 rw_exit(&parent->sdev_contents); 9132621Sllai1 9142621Sllai1 /* take care the timestamps for the node and its parent */ 9152621Sllai1 sdev_update_timestamps(SDEVTOV(self), kcred, 9162621Sllai1 AT_CTIME|AT_MTIME|AT_ATIME); 9172621Sllai1 sdev_update_timestamps(dvp, kcred, AT_MTIME|AT_ATIME); 9182621Sllai1 if (SDEV_IS_GLOBAL(parent)) 9192621Sllai1 atomic_inc_ulong(&parent->sdev_gdir_gen); 9202621Sllai1 9212621Sllai1 /* wake up other threads blocked on looking up this node */ 9222621Sllai1 mutex_enter(&self->sdev_lookup_lock); 9232621Sllai1 SDEV_UNBLOCK_OTHERS(self, SDEV_LOOKUP); 9242621Sllai1 mutex_exit(&self->sdev_lookup_lock); 9252621Sllai1 SDEV_RELE(self); /* don't return with vnode held */ 9262621Sllai1 return (0); 9272621Sllai1 } 9282621Sllai1 9292621Sllai1 static int 9302621Sllai1 sdev_mkdir(struct vnode *dvp, char *nm, struct vattr *va, struct vnode **vpp, 9312621Sllai1 struct cred *cred) 9322621Sllai1 { 9332621Sllai1 int error; 9342621Sllai1 struct sdev_node *parent = (struct sdev_node *)VTOSDEV(dvp); 9352621Sllai1 struct sdev_node *self = NULL; 9362621Sllai1 struct vnode *vp = NULL; 9372621Sllai1 9382621Sllai1 ASSERT(parent && parent->sdev_dotdot); 9392621Sllai1 rw_enter(&parent->sdev_dotdot->sdev_contents, RW_READER); 9402621Sllai1 if (parent->sdev_state == SDEV_ZOMBIE) { 9412621Sllai1 rw_exit(&parent->sdev_dotdot->sdev_contents); 9422621Sllai1 return (ENOENT); 9432621Sllai1 } 9442621Sllai1 9452621Sllai1 /* non-global do not allow pure directory creation */ 9462621Sllai1 if (!SDEV_IS_GLOBAL(parent)) { 9472621Sllai1 rw_exit(&parent->sdev_dotdot->sdev_contents); 9482621Sllai1 return (prof_lookup(dvp, nm, vpp, cred)); 9492621Sllai1 } 9502621Sllai1 rw_exit(&parent->sdev_dotdot->sdev_contents); 9512621Sllai1 9522621Sllai1 /* find existing name */ 9532621Sllai1 error = VOP_LOOKUP(dvp, nm, &vp, NULL, 0, NULL, cred); 9542621Sllai1 if (error == 0) { 9552621Sllai1 VN_RELE(vp); 9562621Sllai1 return (EEXIST); 9572621Sllai1 } 9582621Sllai1 9592621Sllai1 if (error != ENOENT) 9602621Sllai1 return (error); 9612621Sllai1 9622621Sllai1 /* put it into memory */ 9632621Sllai1 rw_enter(&parent->sdev_contents, RW_WRITER); 9642621Sllai1 error = sdev_mknode(parent, nm, &self, 9652621Sllai1 va, NULL, NULL, cred, SDEV_READY); 9662621Sllai1 if (error) { 9672621Sllai1 rw_exit(&parent->sdev_contents); 9682621Sllai1 if (self) 9692621Sllai1 SDEV_RELE(self); 9702621Sllai1 return (error); 9712621Sllai1 } 9722621Sllai1 ASSERT(self && (self->sdev_state == SDEV_READY)); 9732621Sllai1 rw_exit(&parent->sdev_contents); 9742621Sllai1 9752621Sllai1 /* take care the timestamps for the node and its parent */ 9762621Sllai1 sdev_update_timestamps(SDEVTOV(self), kcred, 9772621Sllai1 AT_CTIME|AT_MTIME|AT_ATIME); 9782621Sllai1 sdev_update_timestamps(dvp, kcred, AT_MTIME|AT_ATIME); 9792621Sllai1 if (SDEV_IS_GLOBAL(parent)) 9802621Sllai1 atomic_inc_ulong(&parent->sdev_gdir_gen); 9812621Sllai1 9822621Sllai1 /* wake up other threads blocked on looking up this node */ 9832621Sllai1 mutex_enter(&self->sdev_lookup_lock); 9842621Sllai1 SDEV_UNBLOCK_OTHERS(self, SDEV_LOOKUP); 9852621Sllai1 mutex_exit(&self->sdev_lookup_lock); 9862621Sllai1 *vpp = SDEVTOV(self); 9872621Sllai1 return (0); 9882621Sllai1 } 9892621Sllai1 9902621Sllai1 /* 9912621Sllai1 * allowing removing an empty directory under /dev 9922621Sllai1 */ 9932621Sllai1 /*ARGSUSED*/ 9942621Sllai1 static int 9952621Sllai1 sdev_rmdir(struct vnode *dvp, char *nm, struct vnode *cdir, struct cred *cred) 9962621Sllai1 { 9972621Sllai1 int error = 0; 9982621Sllai1 struct sdev_node *parent = (struct sdev_node *)VTOSDEV(dvp); 9992621Sllai1 struct sdev_node *self = NULL; 10002621Sllai1 struct vnode *vp = NULL; 10012621Sllai1 10022621Sllai1 /* bail out early */ 10032621Sllai1 if (strcmp(nm, ".") == 0) 10042621Sllai1 return (EINVAL); 10052621Sllai1 if (strcmp(nm, "..") == 0) 10062621Sllai1 return (EEXIST); /* should be ENOTEMPTY */ 10072621Sllai1 10082621Sllai1 /* no destruction of non-global node */ 10092621Sllai1 ASSERT(parent && parent->sdev_dotdot); 10102621Sllai1 rw_enter(&parent->sdev_dotdot->sdev_contents, RW_READER); 10112621Sllai1 if (!SDEV_IS_GLOBAL(parent)) { 10122621Sllai1 rw_exit(&parent->sdev_dotdot->sdev_contents); 10132621Sllai1 return (ENOTSUP); 10142621Sllai1 } 10152621Sllai1 rw_exit(&parent->sdev_dotdot->sdev_contents); 10162621Sllai1 10172621Sllai1 /* check existing name */ 10182621Sllai1 rw_enter(&parent->sdev_contents, RW_WRITER); 10192621Sllai1 self = sdev_cache_lookup(parent, nm); 10202621Sllai1 if (self == NULL) { 10212621Sllai1 rw_exit(&parent->sdev_contents); 10222621Sllai1 return (ENOENT); 10232621Sllai1 } 10242621Sllai1 10252621Sllai1 vp = SDEVTOV(self); 10262621Sllai1 if ((self->sdev_state == SDEV_INIT) || 10272621Sllai1 (self->sdev_state == SDEV_ZOMBIE)) { 10282621Sllai1 rw_exit(&parent->sdev_contents); 10292621Sllai1 VN_RELE(vp); 10302621Sllai1 return (ENOENT); 10312621Sllai1 } 10322621Sllai1 10332621Sllai1 /* some sanity checks */ 10342621Sllai1 if (vp == dvp || vp == cdir) { 10352621Sllai1 rw_exit(&parent->sdev_contents); 10362621Sllai1 VN_RELE(vp); 10372621Sllai1 return (EINVAL); 10382621Sllai1 } 10392621Sllai1 10402621Sllai1 if (vp->v_type != VDIR) { 10412621Sllai1 rw_exit(&parent->sdev_contents); 10422621Sllai1 VN_RELE(vp); 10432621Sllai1 return (ENOTDIR); 10442621Sllai1 } 10452621Sllai1 10462621Sllai1 if (vn_vfswlock(vp)) { 10472621Sllai1 rw_exit(&parent->sdev_contents); 10482621Sllai1 VN_RELE(vp); 10492621Sllai1 return (EBUSY); 10502621Sllai1 } 10512621Sllai1 10522621Sllai1 if (vn_mountedvfs(vp) != NULL) { 10532621Sllai1 rw_exit(&parent->sdev_contents); 10542621Sllai1 vn_vfsunlock(vp); 10552621Sllai1 VN_RELE(vp); 10562621Sllai1 return (EBUSY); 10572621Sllai1 } 10582621Sllai1 10592621Sllai1 self = VTOSDEV(vp); 10602621Sllai1 /* bail out on a non-empty directory */ 10612621Sllai1 rw_enter(&self->sdev_contents, RW_READER); 10622621Sllai1 if (self->sdev_nlink > 2) { 10632621Sllai1 rw_exit(&self->sdev_contents); 10642621Sllai1 rw_exit(&parent->sdev_contents); 10652621Sllai1 vn_vfsunlock(vp); 10662621Sllai1 VN_RELE(vp); 10672621Sllai1 return (ENOTEMPTY); 10682621Sllai1 } 10692621Sllai1 rw_exit(&self->sdev_contents); 10702621Sllai1 10712621Sllai1 /* unlink it from the directory cache */ 10722621Sllai1 error = sdev_cache_update(parent, &self, nm, SDEV_CACHE_DELETE); 10732621Sllai1 rw_exit(&parent->sdev_contents); 10742621Sllai1 vn_vfsunlock(vp); 10752621Sllai1 10762621Sllai1 if (error && (error != EBUSY)) { 10772621Sllai1 VN_RELE(vp); 10782621Sllai1 } else { 10792621Sllai1 sdcmn_err2(("sdev_rmdir: cleaning node %s from directory " 10802621Sllai1 " cache with error %d\n", nm, error)); 10812621Sllai1 10822621Sllai1 /* best effort to clean up the backing store */ 10832621Sllai1 if (SDEV_IS_PERSIST(parent)) { 10842621Sllai1 ASSERT(parent->sdev_attrvp); 10852621Sllai1 error = VOP_RMDIR(parent->sdev_attrvp, nm, 10862621Sllai1 parent->sdev_attrvp, kcred); 10872621Sllai1 sdcmn_err2(("sdev_rmdir: cleaning device %s is on" 10882621Sllai1 " disk error %d\n", parent->sdev_path, error)); 10892621Sllai1 } 10902621Sllai1 10912621Sllai1 if (error == EBUSY) 10922621Sllai1 error = 0; 10932621Sllai1 } 10942621Sllai1 10952621Sllai1 return (error); 10962621Sllai1 } 10972621Sllai1 10982621Sllai1 /* 10992621Sllai1 * read the contents of a symbolic link 11002621Sllai1 */ 11012621Sllai1 static int 11022621Sllai1 sdev_readlink(struct vnode *vp, struct uio *uiop, struct cred *cred) 11032621Sllai1 { 11042621Sllai1 struct sdev_node *dv; 11052621Sllai1 int error = 0; 11062621Sllai1 11072621Sllai1 ASSERT(vp->v_type == VLNK); 11082621Sllai1 11092621Sllai1 dv = VTOSDEV(vp); 11102621Sllai1 11112621Sllai1 if (dv->sdev_attrvp) { 11122621Sllai1 /* non-NULL attrvp implys a persisted node at READY state */ 11132621Sllai1 return (VOP_READLINK(dv->sdev_attrvp, uiop, cred)); 11142621Sllai1 } else if (dv->sdev_symlink != NULL) { 11152621Sllai1 /* memory nodes, e.g. local nodes */ 11162621Sllai1 rw_enter(&dv->sdev_contents, RW_READER); 11172621Sllai1 sdcmn_err2(("sdev_readlink link is %s\n", dv->sdev_symlink)); 11182621Sllai1 error = uiomove(dv->sdev_symlink, strlen(dv->sdev_symlink), 11192621Sllai1 UIO_READ, uiop); 11202621Sllai1 rw_exit(&dv->sdev_contents); 11212621Sllai1 return (error); 11222621Sllai1 } 11232621Sllai1 11242621Sllai1 return (ENOENT); 11252621Sllai1 } 11262621Sllai1 11272621Sllai1 static int 11282621Sllai1 sdev_readdir(struct vnode *dvp, struct uio *uiop, struct cred *cred, int *eofp) 11292621Sllai1 { 11302679Sszhou struct sdev_node *parent = VTOSDEV(dvp); 11312679Sszhou 11322679Sszhou ASSERT(parent); 11332679Sszhou if (!SDEV_IS_GLOBAL(parent)) 11342679Sszhou prof_filldir(parent); 11352621Sllai1 return (devname_readdir_func(dvp, uiop, cred, eofp, SDEV_BROWSE)); 11362621Sllai1 } 11372621Sllai1 11382621Sllai1 /*ARGSUSED1*/ 11392621Sllai1 static void 11402621Sllai1 sdev_inactive(struct vnode *vp, struct cred *cred) 11412621Sllai1 { 11422621Sllai1 int clean; 11432621Sllai1 struct sdev_node *dv = VTOSDEV(vp); 11442621Sllai1 struct sdev_node *ddv = dv->sdev_dotdot; 11452621Sllai1 struct sdev_node *idv; 11462621Sllai1 struct sdev_node *prev = NULL; 11472621Sllai1 int state; 11482621Sllai1 struct devname_nsmap *map = NULL; 11492621Sllai1 struct devname_ops *dirops = NULL; 11502621Sllai1 void (*fn)(devname_handle_t *, struct cred *) = NULL; 11512621Sllai1 11522621Sllai1 rw_enter(&ddv->sdev_contents, RW_WRITER); 11532621Sllai1 state = dv->sdev_state; 11542621Sllai1 11552621Sllai1 mutex_enter(&vp->v_lock); 11562621Sllai1 ASSERT(vp->v_count >= 1); 11572621Sllai1 11582621Sllai1 clean = (vp->v_count == 1) && (state == SDEV_ZOMBIE); 11592621Sllai1 11602621Sllai1 /* 11612621Sllai1 * last ref count on the ZOMBIE node is released. 11622621Sllai1 * clean up the sdev_node, and 11632621Sllai1 * release the hold on the backing store node so that 11642621Sllai1 * the ZOMBIE backing stores also cleaned out. 11652621Sllai1 */ 11662621Sllai1 if (clean) { 11672621Sllai1 ASSERT(ddv); 11682621Sllai1 if (SDEV_IS_GLOBAL(dv)) { 11692621Sllai1 map = ddv->sdev_mapinfo; 11702621Sllai1 dirops = map ? map->dir_ops : NULL; 11712621Sllai1 if (dirops && (fn = dirops->devnops_inactive)) 11722621Sllai1 (*fn)(&(dv->sdev_handle), cred); 11732621Sllai1 } 11742621Sllai1 11752621Sllai1 ddv->sdev_nlink--; 11762621Sllai1 if (vp->v_type == VDIR) { 11772621Sllai1 dv->sdev_nlink--; 11782621Sllai1 } 11792621Sllai1 for (idv = ddv->sdev_dot; idv && idv != dv; 11802621Sllai1 prev = idv, idv = idv->sdev_next); 11812621Sllai1 ASSERT(idv == dv); 11822621Sllai1 if (prev == NULL) 11832621Sllai1 ddv->sdev_dot = dv->sdev_next; 11842621Sllai1 else 11852621Sllai1 prev->sdev_next = dv->sdev_next; 11862621Sllai1 dv->sdev_next = NULL; 11872621Sllai1 dv->sdev_nlink--; 11882621Sllai1 --vp->v_count; 11892621Sllai1 mutex_exit(&vp->v_lock); 11902621Sllai1 sdev_nodedestroy(dv, 0); 11912621Sllai1 } else { 11922621Sllai1 --vp->v_count; 11932621Sllai1 mutex_exit(&vp->v_lock); 11942621Sllai1 } 11952621Sllai1 rw_exit(&ddv->sdev_contents); 11962621Sllai1 } 11972621Sllai1 11982621Sllai1 static int 11992621Sllai1 sdev_fid(struct vnode *vp, struct fid *fidp) 12002621Sllai1 { 12012621Sllai1 struct sdev_node *dv = VTOSDEV(vp); 12022621Sllai1 struct sdev_fid *sdev_fid; 12032621Sllai1 12042621Sllai1 if (fidp->fid_len < (sizeof (struct sdev_fid) - sizeof (ushort_t))) { 12052621Sllai1 fidp->fid_len = sizeof (struct sdev_fid) - sizeof (ushort_t); 12062621Sllai1 return (ENOSPC); 12072621Sllai1 } 12082621Sllai1 12092621Sllai1 sdev_fid = (struct sdev_fid *)fidp; 12102621Sllai1 bzero(sdev_fid, sizeof (struct sdev_fid)); 12112621Sllai1 sdev_fid->sdevfid_len = 12122621Sllai1 (int)sizeof (struct sdev_fid) - sizeof (ushort_t); 12132621Sllai1 sdev_fid->sdevfid_ino = dv->sdev_ino; 12142621Sllai1 12152621Sllai1 return (0); 12162621Sllai1 } 12172621Sllai1 12182621Sllai1 /* 12192621Sllai1 * This pair of routines bracket all VOP_READ, VOP_WRITE 12202621Sllai1 * and VOP_READDIR requests. The contents lock stops things 12212621Sllai1 * moving around while we're looking at them. 12222621Sllai1 */ 12232621Sllai1 static void 12242621Sllai1 sdev_rwlock(struct vnode *vp, int write_flag) 12252621Sllai1 { 12262621Sllai1 rw_enter(&VTOSDEV(vp)->sdev_contents, write_flag ? RW_WRITER : 12272621Sllai1 RW_READER); 12282621Sllai1 } 12292621Sllai1 12302621Sllai1 /*ARGSUSED1*/ 12312621Sllai1 static void 12322621Sllai1 sdev_rwunlock(struct vnode *vp, int write_flag) 12332621Sllai1 { 12342621Sllai1 rw_exit(&VTOSDEV(vp)->sdev_contents); 12352621Sllai1 } 12362621Sllai1 12372621Sllai1 /*ARGSUSED1*/ 12382621Sllai1 static int 12392621Sllai1 sdev_seek(struct vnode *vp, offset_t ooff, offset_t *noffp) 12402621Sllai1 { 12412621Sllai1 struct vnode *attrvp = VTOSDEV(vp)->sdev_attrvp; 12422621Sllai1 12432621Sllai1 ASSERT(vp->v_type != VCHR && 12442621Sllai1 vp->v_type != VBLK && vp->v_type != VLNK); 12452621Sllai1 12462621Sllai1 if (vp->v_type == VDIR) 12472621Sllai1 return (fs_seek(vp, ooff, noffp)); 12482621Sllai1 12492621Sllai1 ASSERT(attrvp); 12502621Sllai1 return (VOP_SEEK(attrvp, ooff, noffp)); 12512621Sllai1 } 12522621Sllai1 12532621Sllai1 /*ARGSUSED1*/ 12542621Sllai1 static int 12552621Sllai1 sdev_frlock(struct vnode *vp, int cmd, struct flock64 *bfp, int flag, 12562621Sllai1 offset_t offset, struct flk_callback *flk_cbp, struct cred *cr) 12572621Sllai1 { 12582621Sllai1 int error; 12592621Sllai1 struct sdev_node *dv = VTOSDEV(vp); 12602621Sllai1 12612621Sllai1 ASSERT(dv); 12622621Sllai1 ASSERT(dv->sdev_attrvp); 12632621Sllai1 error = VOP_FRLOCK(dv->sdev_attrvp, cmd, bfp, flag, offset, 12642621Sllai1 flk_cbp, cr); 12652621Sllai1 12662621Sllai1 return (error); 12672621Sllai1 } 12682621Sllai1 12692621Sllai1 static int 12702621Sllai1 sdev_setfl(struct vnode *vp, int oflags, int nflags, cred_t *cr) 12712621Sllai1 { 12722621Sllai1 struct sdev_node *dv = VTOSDEV(vp); 12732621Sllai1 ASSERT(dv); 12742621Sllai1 ASSERT(dv->sdev_attrvp); 12752621Sllai1 12762621Sllai1 return (VOP_SETFL(dv->sdev_attrvp, oflags, nflags, cr)); 12772621Sllai1 } 12782621Sllai1 12792621Sllai1 static int 12802621Sllai1 sdev_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr) 12812621Sllai1 { 12822621Sllai1 switch (cmd) { 12832621Sllai1 case _PC_ACL_ENABLED: 12842621Sllai1 *valp = SDEV_ACL_FLAVOR(vp); 12852621Sllai1 return (0); 12862621Sllai1 } 12872621Sllai1 12882621Sllai1 return (fs_pathconf(vp, cmd, valp, cr)); 12892621Sllai1 } 12902621Sllai1 12912621Sllai1 vnodeops_t *sdev_vnodeops; 12922621Sllai1 12932621Sllai1 const fs_operation_def_t sdev_vnodeops_tbl[] = { 12942621Sllai1 VOPNAME_OPEN, sdev_open, 12952621Sllai1 VOPNAME_CLOSE, sdev_close, 12962621Sllai1 VOPNAME_READ, sdev_read, 12972621Sllai1 VOPNAME_WRITE, sdev_write, 12982621Sllai1 VOPNAME_IOCTL, sdev_ioctl, 12992621Sllai1 VOPNAME_GETATTR, sdev_getattr, 13002621Sllai1 VOPNAME_SETATTR, sdev_setattr, 13012621Sllai1 VOPNAME_ACCESS, sdev_access, 13022621Sllai1 VOPNAME_LOOKUP, sdev_lookup, 13032621Sllai1 VOPNAME_CREATE, sdev_create, 13042621Sllai1 VOPNAME_RENAME, sdev_rename, 13052621Sllai1 VOPNAME_REMOVE, sdev_remove, 13062621Sllai1 VOPNAME_MKDIR, sdev_mkdir, 13072621Sllai1 VOPNAME_RMDIR, sdev_rmdir, 13082621Sllai1 VOPNAME_READDIR, sdev_readdir, 13092621Sllai1 VOPNAME_SYMLINK, sdev_symlink, 13102621Sllai1 VOPNAME_READLINK, sdev_readlink, /* readlink */ 13112621Sllai1 VOPNAME_FSYNC, (fs_generic_func_p) fs_sync, 13122621Sllai1 VOPNAME_INACTIVE, (fs_generic_func_p)sdev_inactive, 13132621Sllai1 VOPNAME_FID, sdev_fid, 13142621Sllai1 VOPNAME_RWLOCK, (fs_generic_func_p)sdev_rwlock, 13152621Sllai1 VOPNAME_RWUNLOCK, (fs_generic_func_p)sdev_rwunlock, 13162621Sllai1 VOPNAME_SEEK, sdev_seek, 13172621Sllai1 VOPNAME_FRLOCK, sdev_frlock, 13182621Sllai1 VOPNAME_PATHCONF, sdev_pathconf, 13192621Sllai1 VOPNAME_SETFL, sdev_setfl, 13202621Sllai1 VOPNAME_SETSECATTR, sdev_setsecattr, /* setsecattr */ 13212621Sllai1 VOPNAME_GETSECATTR, sdev_getsecattr, /* getsecattr */ 13222621Sllai1 NULL, NULL 13232621Sllai1 }; 13242621Sllai1 13252621Sllai1 int sdev_vnodeops_tbl_size = sizeof (sdev_vnodeops_tbl); 1326