1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved. 23 */ 24 25 /* 26 * vnode ops for the /dev filesystem 27 * 28 * - VDIR, VCHR, CBLK, and VLNK are considered must supported files 29 * - VREG and VDOOR are used for some internal implementations in 30 * the global zone, e.g. devname and devfsadm communication 31 * - other file types are unusual in this namespace and 32 * not supported for now 33 */ 34 35 #include <sys/types.h> 36 #include <sys/param.h> 37 #include <sys/t_lock.h> 38 #include <sys/systm.h> 39 #include <sys/sysmacros.h> 40 #include <sys/user.h> 41 #include <sys/time.h> 42 #include <sys/vfs.h> 43 #include <sys/vnode.h> 44 #include <sys/vfs_opreg.h> 45 #include <sys/file.h> 46 #include <sys/fcntl.h> 47 #include <sys/flock.h> 48 #include <sys/kmem.h> 49 #include <sys/uio.h> 50 #include <sys/errno.h> 51 #include <sys/stat.h> 52 #include <sys/cred.h> 53 #include <sys/dirent.h> 54 #include <sys/pathname.h> 55 #include <sys/cmn_err.h> 56 #include <sys/debug.h> 57 #include <sys/policy.h> 58 #include <vm/hat.h> 59 #include <vm/seg_vn.h> 60 #include <vm/seg_map.h> 61 #include <vm/seg.h> 62 #include <vm/as.h> 63 #include <vm/page.h> 64 #include <sys/proc.h> 65 #include <sys/mode.h> 66 #include <sys/sunndi.h> 67 #include <sys/ptms.h> 68 #include <fs/fs_subr.h> 69 #include <sys/fs/dv_node.h> 70 #include <sys/fs/sdev_impl.h> 71 72 /*ARGSUSED*/ 73 static int 74 sdev_open(struct vnode **vpp, int flag, struct cred *cred, caller_context_t *ct) 75 { 76 struct sdev_node *dv = VTOSDEV(*vpp); 77 struct sdev_node *ddv = dv->sdev_dotdot; 78 int error = 0; 79 80 if ((*vpp)->v_type == VDIR) 81 return (0); 82 83 if (!SDEV_IS_GLOBAL(dv)) 84 return (ENOTSUP); 85 86 if ((*vpp)->v_type == VLNK) 87 return (ENOENT); 88 ASSERT((*vpp)->v_type == VREG); 89 if ((*vpp)->v_type != VREG) 90 return (ENOTSUP); 91 92 ASSERT(ddv); 93 rw_enter(&ddv->sdev_contents, RW_READER); 94 if (dv->sdev_attrvp == NULL) { 95 rw_exit(&ddv->sdev_contents); 96 return (ENOENT); 97 } 98 error = VOP_OPEN(&(dv->sdev_attrvp), flag, cred, ct); 99 rw_exit(&ddv->sdev_contents); 100 return (error); 101 } 102 103 /*ARGSUSED1*/ 104 static int 105 sdev_close(struct vnode *vp, int flag, int count, 106 offset_t offset, struct cred *cred, caller_context_t *ct) 107 { 108 struct sdev_node *dv = VTOSDEV(vp); 109 110 if (vp->v_type == VDIR) { 111 cleanlocks(vp, ttoproc(curthread)->p_pid, 0); 112 cleanshares(vp, ttoproc(curthread)->p_pid); 113 return (0); 114 } 115 116 if (!SDEV_IS_GLOBAL(dv)) 117 return (ENOTSUP); 118 119 ASSERT(vp->v_type == VREG); 120 if (vp->v_type != VREG) 121 return (ENOTSUP); 122 123 ASSERT(dv->sdev_attrvp); 124 return (VOP_CLOSE(dv->sdev_attrvp, flag, count, offset, cred, ct)); 125 } 126 127 /*ARGSUSED*/ 128 static int 129 sdev_read(struct vnode *vp, struct uio *uio, int ioflag, struct cred *cred, 130 struct caller_context *ct) 131 { 132 struct sdev_node *dv = (struct sdev_node *)VTOSDEV(vp); 133 int error; 134 135 if (!SDEV_IS_GLOBAL(dv)) 136 return (EINVAL); 137 138 if (vp->v_type == VDIR) 139 return (EISDIR); 140 141 /* only supporting regular files in /dev */ 142 ASSERT(vp->v_type == VREG); 143 if (vp->v_type != VREG) 144 return (EINVAL); 145 146 ASSERT(RW_READ_HELD(&VTOSDEV(vp)->sdev_contents)); 147 ASSERT(dv->sdev_attrvp); 148 (void) VOP_RWLOCK(dv->sdev_attrvp, 0, ct); 149 error = VOP_READ(dv->sdev_attrvp, uio, ioflag, cred, ct); 150 VOP_RWUNLOCK(dv->sdev_attrvp, 0, ct); 151 return (error); 152 } 153 154 /*ARGSUSED*/ 155 static int 156 sdev_write(struct vnode *vp, struct uio *uio, int ioflag, struct cred *cred, 157 struct caller_context *ct) 158 { 159 struct sdev_node *dv = VTOSDEV(vp); 160 int error = 0; 161 162 if (!SDEV_IS_GLOBAL(dv)) 163 return (EINVAL); 164 165 if (vp->v_type == VDIR) 166 return (EISDIR); 167 168 /* only supporting regular files in /dev */ 169 ASSERT(vp->v_type == VREG); 170 if (vp->v_type != VREG) 171 return (EINVAL); 172 173 ASSERT(dv->sdev_attrvp); 174 175 (void) VOP_RWLOCK(dv->sdev_attrvp, 1, ct); 176 error = VOP_WRITE(dv->sdev_attrvp, uio, ioflag, cred, ct); 177 VOP_RWUNLOCK(dv->sdev_attrvp, 1, ct); 178 if (error == 0) { 179 sdev_update_timestamps(dv->sdev_attrvp, kcred, 180 AT_MTIME); 181 } 182 return (error); 183 } 184 185 /*ARGSUSED*/ 186 static int 187 sdev_ioctl(struct vnode *vp, int cmd, intptr_t arg, int flag, 188 struct cred *cred, int *rvalp, caller_context_t *ct) 189 { 190 struct sdev_node *dv = VTOSDEV(vp); 191 192 if (!SDEV_IS_GLOBAL(dv) || (vp->v_type == VDIR)) 193 return (ENOTTY); 194 195 ASSERT(vp->v_type == VREG); 196 if (vp->v_type != VREG) 197 return (EINVAL); 198 199 ASSERT(dv->sdev_attrvp); 200 return (VOP_IOCTL(dv->sdev_attrvp, cmd, arg, flag, cred, rvalp, ct)); 201 } 202 203 static int 204 sdev_getattr(struct vnode *vp, struct vattr *vap, int flags, 205 struct cred *cr, caller_context_t *ct) 206 { 207 int error = 0; 208 struct sdev_node *dv = VTOSDEV(vp); 209 struct sdev_node *parent = dv->sdev_dotdot; 210 211 ASSERT(parent); 212 213 rw_enter(&parent->sdev_contents, RW_READER); 214 ASSERT(dv->sdev_attr || dv->sdev_attrvp); 215 216 /* 217 * search order: 218 * - for persistent nodes (SDEV_PERSIST): backstore 219 * - for non-persistent nodes: module ops if global, then memory 220 */ 221 if (dv->sdev_attrvp) { 222 rw_exit(&parent->sdev_contents); 223 error = VOP_GETATTR(dv->sdev_attrvp, vap, flags, cr, ct); 224 sdev_vattr_merge(dv, vap); 225 } else { 226 ASSERT(dv->sdev_attr); 227 *vap = *dv->sdev_attr; 228 sdev_vattr_merge(dv, vap); 229 rw_exit(&parent->sdev_contents); 230 } 231 232 return (error); 233 } 234 235 /*ARGSUSED4*/ 236 static int 237 sdev_setattr(struct vnode *vp, struct vattr *vap, int flags, 238 struct cred *cred, caller_context_t *ctp) 239 { 240 return (devname_setattr_func(vp, vap, flags, cred, NULL, 0)); 241 } 242 243 static int 244 sdev_getsecattr(struct vnode *vp, struct vsecattr *vsap, int flags, 245 struct cred *cr, caller_context_t *ct) 246 { 247 int error; 248 struct sdev_node *dv = VTOSDEV(vp); 249 struct vnode *avp = dv->sdev_attrvp; 250 251 if (avp == NULL) { 252 /* return fs_fab_acl() if flavor matches, else do nothing */ 253 if ((SDEV_ACL_FLAVOR(vp) == _ACL_ACLENT_ENABLED && 254 (vsap->vsa_mask & (VSA_ACLCNT | VSA_DFACLCNT))) || 255 (SDEV_ACL_FLAVOR(vp) == _ACL_ACE_ENABLED && 256 (vsap->vsa_mask & (VSA_ACECNT | VSA_ACE)))) 257 return (fs_fab_acl(vp, vsap, flags, cr, ct)); 258 259 return (ENOSYS); 260 } 261 262 (void) VOP_RWLOCK(avp, 1, ct); 263 error = VOP_GETSECATTR(avp, vsap, flags, cr, ct); 264 VOP_RWUNLOCK(avp, 1, ct); 265 return (error); 266 } 267 268 static int 269 sdev_setsecattr(struct vnode *vp, struct vsecattr *vsap, int flags, 270 struct cred *cr, caller_context_t *ct) 271 { 272 int error; 273 struct sdev_node *dv = VTOSDEV(vp); 274 struct vnode *avp = dv->sdev_attrvp; 275 276 if (dv->sdev_state == SDEV_ZOMBIE) 277 return (0); 278 279 if (avp == NULL) { 280 if (SDEV_IS_GLOBAL(dv) && !SDEV_IS_PERSIST(dv)) 281 return (fs_nosys()); 282 ASSERT(dv->sdev_attr); 283 /* 284 * if coming in directly, the acl system call will 285 * have held the read-write lock via VOP_RWLOCK() 286 * If coming in via specfs, specfs will have 287 * held the rw lock on the realvp i.e. us. 288 */ 289 ASSERT(RW_WRITE_HELD(&dv->sdev_contents)); 290 sdev_vattr_merge(dv, dv->sdev_attr); 291 error = sdev_shadow_node(dv, cr); 292 if (error) { 293 return (fs_nosys()); 294 } 295 296 ASSERT(dv->sdev_attrvp); 297 /* clean out the memory copy if any */ 298 if (dv->sdev_attr) { 299 kmem_free(dv->sdev_attr, sizeof (struct vattr)); 300 dv->sdev_attr = NULL; 301 } 302 avp = dv->sdev_attrvp; 303 } 304 ASSERT(avp); 305 306 (void) VOP_RWLOCK(avp, V_WRITELOCK_TRUE, ct); 307 error = VOP_SETSECATTR(avp, vsap, flags, cr, ct); 308 VOP_RWUNLOCK(avp, V_WRITELOCK_TRUE, ct); 309 return (error); 310 } 311 312 int 313 sdev_unlocked_access(void *vdv, int mode, struct cred *cr) 314 { 315 struct sdev_node *dv = vdv; 316 int shift = 0; 317 uid_t owner = dv->sdev_attr->va_uid; 318 319 if (crgetuid(cr) != owner) { 320 shift += 3; 321 if (groupmember(dv->sdev_attr->va_gid, cr) == 0) 322 shift += 3; 323 } 324 325 return (secpolicy_vnode_access2(cr, SDEVTOV(dv), owner, 326 dv->sdev_attr->va_mode << shift, mode)); 327 } 328 329 static int 330 sdev_access(struct vnode *vp, int mode, int flags, struct cred *cr, 331 caller_context_t *ct) 332 { 333 struct sdev_node *dv = VTOSDEV(vp); 334 int ret = 0; 335 336 ASSERT(dv->sdev_attr || dv->sdev_attrvp); 337 338 if (dv->sdev_attrvp) { 339 ret = VOP_ACCESS(dv->sdev_attrvp, mode, flags, cr, ct); 340 } else if (dv->sdev_attr) { 341 rw_enter(&dv->sdev_contents, RW_READER); 342 ret = sdev_unlocked_access(dv, mode, cr); 343 if (ret) 344 ret = EACCES; 345 rw_exit(&dv->sdev_contents); 346 } 347 348 return (ret); 349 } 350 351 /* 352 * Lookup 353 */ 354 /*ARGSUSED3*/ 355 static int 356 sdev_lookup(struct vnode *dvp, char *nm, struct vnode **vpp, 357 struct pathname *pnp, int flags, struct vnode *rdir, struct cred *cred, 358 caller_context_t *ct, int *direntflags, pathname_t *realpnp) 359 { 360 struct sdev_node *parent; 361 int error; 362 363 parent = VTOSDEV(dvp); 364 ASSERT(parent); 365 366 /* execute access is required to search the directory */ 367 if ((error = VOP_ACCESS(dvp, VEXEC, 0, cred, ct)) != 0) 368 return (error); 369 370 if (!SDEV_IS_GLOBAL(parent)) 371 return (prof_lookup(dvp, nm, vpp, cred)); 372 return (devname_lookup_func(parent, nm, vpp, cred, NULL, 0)); 373 } 374 375 /*ARGSUSED2*/ 376 static int 377 sdev_create(struct vnode *dvp, char *nm, struct vattr *vap, vcexcl_t excl, 378 int mode, struct vnode **vpp, struct cred *cred, int flag, 379 caller_context_t *ct, vsecattr_t *vsecp) 380 { 381 struct vnode *vp = NULL; 382 struct vnode *avp; 383 struct sdev_node *parent; 384 struct sdev_node *self = NULL; 385 int error = 0; 386 vtype_t type = vap->va_type; 387 388 ASSERT(type != VNON && type != VBAD); 389 390 if ((type == VFIFO) || (type == VSOCK) || 391 (type == VPROC) || (type == VPORT)) 392 return (ENOTSUP); 393 394 parent = VTOSDEV(dvp); 395 ASSERT(parent); 396 397 rw_enter(&parent->sdev_dotdot->sdev_contents, RW_READER); 398 if (parent->sdev_state == SDEV_ZOMBIE) { 399 rw_exit(&parent->sdev_dotdot->sdev_contents); 400 return (ENOENT); 401 } 402 403 /* non-global do not allow pure node creation */ 404 if (!SDEV_IS_GLOBAL(parent)) { 405 rw_exit(&parent->sdev_dotdot->sdev_contents); 406 return (prof_lookup(dvp, nm, vpp, cred)); 407 } 408 rw_exit(&parent->sdev_dotdot->sdev_contents); 409 410 /* execute access is required to search the directory */ 411 if ((error = VOP_ACCESS(dvp, VEXEC, 0, cred, ct)) != 0) 412 return (error); 413 414 /* check existing name */ 415 /* XXXci - We may need to translate the C-I flags on VOP_LOOKUP */ 416 error = VOP_LOOKUP(dvp, nm, &vp, NULL, 0, NULL, cred, ct, NULL, NULL); 417 418 /* name found */ 419 if (error == 0) { 420 ASSERT(vp); 421 if (excl == EXCL) { 422 error = EEXIST; 423 } else if ((vp->v_type == VDIR) && (mode & VWRITE)) { 424 /* allowing create/read-only an existing directory */ 425 error = EISDIR; 426 } else { 427 error = VOP_ACCESS(vp, mode, 0, cred, ct); 428 } 429 430 if (error) { 431 VN_RELE(vp); 432 return (error); 433 } 434 435 /* truncation first */ 436 if ((vp->v_type == VREG) && (vap->va_mask & AT_SIZE) && 437 (vap->va_size == 0)) { 438 ASSERT(parent->sdev_attrvp); 439 error = VOP_CREATE(parent->sdev_attrvp, 440 nm, vap, excl, mode, &avp, cred, flag, ct, vsecp); 441 442 if (error) { 443 VN_RELE(vp); 444 return (error); 445 } 446 } 447 448 sdev_update_timestamps(vp, kcred, 449 AT_CTIME|AT_MTIME|AT_ATIME); 450 *vpp = vp; 451 return (0); 452 } 453 454 /* bail out early */ 455 if (error != ENOENT) 456 return (error); 457 458 /* verify write access - compliance specifies ENXIO */ 459 if ((error = VOP_ACCESS(dvp, VEXEC|VWRITE, 0, cred, ct)) != 0) { 460 if (error == EACCES) 461 error = ENXIO; 462 return (error); 463 } 464 465 /* 466 * For memory-based (ROFS) directory: 467 * - either disallow node creation; 468 * - or implement VOP_CREATE of its own 469 */ 470 rw_enter(&parent->sdev_contents, RW_WRITER); 471 if (!SDEV_IS_PERSIST(parent)) { 472 rw_exit(&parent->sdev_contents); 473 return (ENOTSUP); 474 } 475 ASSERT(parent->sdev_attrvp); 476 error = sdev_mknode(parent, nm, &self, vap, NULL, NULL, 477 cred, SDEV_READY); 478 if (error) { 479 rw_exit(&parent->sdev_contents); 480 if (self) 481 SDEV_RELE(self); 482 return (error); 483 } 484 rw_exit(&parent->sdev_contents); 485 486 ASSERT(self); 487 /* take care the timestamps for the node and its parent */ 488 sdev_update_timestamps(SDEVTOV(self), kcred, 489 AT_CTIME|AT_MTIME|AT_ATIME); 490 sdev_update_timestamps(dvp, kcred, AT_MTIME|AT_ATIME); 491 if (SDEV_IS_GLOBAL(parent)) 492 atomic_inc_ulong(&parent->sdev_gdir_gen); 493 494 /* wake up other threads blocked on looking up this node */ 495 mutex_enter(&self->sdev_lookup_lock); 496 SDEV_UNBLOCK_OTHERS(self, SDEV_LOOKUP); 497 mutex_exit(&self->sdev_lookup_lock); 498 error = sdev_to_vp(self, vpp); 499 return (error); 500 } 501 502 static int 503 sdev_remove(struct vnode *dvp, char *nm, struct cred *cred, 504 caller_context_t *ct, int flags) 505 { 506 int error; 507 struct sdev_node *parent = (struct sdev_node *)VTOSDEV(dvp); 508 struct vnode *vp = NULL; 509 struct sdev_node *dv = NULL; 510 int len; 511 int bkstore = 0; 512 513 /* bail out early */ 514 len = strlen(nm); 515 if (nm[0] == '.') { 516 if (len == 1) { 517 return (EINVAL); 518 } else if (len == 2 && nm[1] == '.') { 519 return (EEXIST); 520 } 521 } 522 523 ASSERT(parent); 524 rw_enter(&parent->sdev_contents, RW_READER); 525 if (!SDEV_IS_GLOBAL(parent)) { 526 rw_exit(&parent->sdev_contents); 527 return (ENOTSUP); 528 } 529 530 /* execute access is required to search the directory */ 531 if ((error = VOP_ACCESS(dvp, VEXEC, 0, cred, ct)) != 0) { 532 rw_exit(&parent->sdev_contents); 533 return (error); 534 } 535 536 /* check existence first */ 537 dv = sdev_cache_lookup(parent, nm); 538 if (dv == NULL) { 539 rw_exit(&parent->sdev_contents); 540 return (ENOENT); 541 } 542 543 vp = SDEVTOV(dv); 544 if ((dv->sdev_state == SDEV_INIT) || 545 (dv->sdev_state == SDEV_ZOMBIE)) { 546 rw_exit(&parent->sdev_contents); 547 VN_RELE(vp); 548 return (ENOENT); 549 } 550 551 /* write access is required to remove an entry */ 552 if ((error = VOP_ACCESS(dvp, VWRITE, 0, cred, ct)) != 0) { 553 rw_exit(&parent->sdev_contents); 554 VN_RELE(vp); 555 return (error); 556 } 557 558 /* 559 * sdev_dirdelete does the real job of: 560 * - make sure no open ref count 561 * - destroying the sdev_node 562 * - releasing the hold on attrvp 563 */ 564 bkstore = SDEV_IS_PERSIST(dv) ? 1 : 0; 565 if (!rw_tryupgrade(&parent->sdev_contents)) { 566 rw_exit(&parent->sdev_contents); 567 rw_enter(&parent->sdev_contents, RW_WRITER); 568 } 569 error = sdev_cache_update(parent, &dv, nm, SDEV_CACHE_DELETE); 570 rw_exit(&parent->sdev_contents); 571 572 sdcmn_err2(("sdev_remove: cache_update error %d\n", error)); 573 if (error && (error != EBUSY)) { 574 /* report errors other than EBUSY */ 575 VN_RELE(vp); 576 } else { 577 sdcmn_err2(("sdev_remove: cleaning node %s from cache " 578 " with error %d\n", nm, error)); 579 580 /* 581 * best efforts clean up the backing store 582 */ 583 if (bkstore) { 584 ASSERT(parent->sdev_attrvp); 585 error = VOP_REMOVE(parent->sdev_attrvp, nm, cred, 586 ct, flags); 587 /* 588 * do not report BUSY error 589 * because the backing store ref count is released 590 * when the last ref count on the sdev_node is 591 * released. 592 */ 593 if (error == EBUSY) { 594 sdcmn_err2(("sdev_remove: device %s is still on" 595 "disk %s\n", nm, parent->sdev_path)); 596 error = 0; 597 } 598 } 599 600 if (error == EBUSY) 601 error = 0; 602 } 603 604 return (error); 605 } 606 607 /* 608 * Some restrictions for this file system: 609 * - both oldnm and newnm are in the scope of /dev file system, 610 * to simply the namespace management model. 611 */ 612 /*ARGSUSED6*/ 613 static int 614 sdev_rename(struct vnode *odvp, char *onm, struct vnode *ndvp, char *nnm, 615 struct cred *cred, caller_context_t *ct, int flags) 616 { 617 struct sdev_node *fromparent = NULL; 618 struct vattr vattr; 619 struct sdev_node *toparent; 620 struct sdev_node *fromdv = NULL; /* source node */ 621 struct vnode *ovp = NULL; /* source vnode */ 622 struct sdev_node *todv = NULL; /* destination node */ 623 struct vnode *nvp = NULL; /* destination vnode */ 624 int samedir = 0; /* set if odvp == ndvp */ 625 struct vnode *realvp; 626 int error = 0; 627 dev_t fsid; 628 int bkstore = 0; 629 vtype_t type; 630 631 /* prevent modifying "." and ".." */ 632 if ((onm[0] == '.' && 633 (onm[1] == '\0' || (onm[1] == '.' && onm[2] == '\0'))) || 634 (nnm[0] == '.' && 635 (nnm[1] == '\0' || (nnm[1] == '.' && nnm[2] == '\0')))) { 636 return (EINVAL); 637 } 638 639 fromparent = VTOSDEV(odvp); 640 toparent = VTOSDEV(ndvp); 641 642 /* ZOMBIE parent doesn't allow new node creation */ 643 rw_enter(&fromparent->sdev_dotdot->sdev_contents, RW_READER); 644 if (fromparent->sdev_state == SDEV_ZOMBIE) { 645 rw_exit(&fromparent->sdev_dotdot->sdev_contents); 646 return (ENOENT); 647 } 648 649 /* renaming only supported for global device nodes */ 650 if (!SDEV_IS_GLOBAL(fromparent)) { 651 rw_exit(&fromparent->sdev_dotdot->sdev_contents); 652 return (ENOTSUP); 653 } 654 rw_exit(&fromparent->sdev_dotdot->sdev_contents); 655 656 rw_enter(&toparent->sdev_dotdot->sdev_contents, RW_READER); 657 if (toparent->sdev_state == SDEV_ZOMBIE) { 658 rw_exit(&toparent->sdev_dotdot->sdev_contents); 659 return (ENOENT); 660 } 661 rw_exit(&toparent->sdev_dotdot->sdev_contents); 662 663 /* 664 * acquire the global lock to prevent 665 * mount/unmount/other rename activities. 666 */ 667 mutex_enter(&sdev_lock); 668 669 /* check existence of the source node */ 670 /* XXXci - We may need to translate the C-I flags on VOP_LOOKUP */ 671 error = VOP_LOOKUP(odvp, onm, &ovp, NULL, 0, NULL, cred, ct, 672 NULL, NULL); 673 if (error) { 674 sdcmn_err2(("sdev_rename: the source node %s exists\n", 675 onm)); 676 mutex_exit(&sdev_lock); 677 return (error); 678 } 679 680 if (VOP_REALVP(ovp, &realvp, ct) == 0) { 681 VN_HOLD(realvp); 682 VN_RELE(ovp); 683 ovp = realvp; 684 } 685 686 /* check existence of destination */ 687 /* XXXci - We may need to translate the C-I flags on VOP_LOOKUP */ 688 error = VOP_LOOKUP(ndvp, nnm, &nvp, NULL, 0, NULL, cred, ct, 689 NULL, NULL); 690 if (error && (error != ENOENT)) { 691 mutex_exit(&sdev_lock); 692 VN_RELE(ovp); 693 return (error); 694 } 695 696 if (nvp && (VOP_REALVP(nvp, &realvp, ct) == 0)) { 697 VN_HOLD(realvp); 698 VN_RELE(nvp); 699 nvp = realvp; 700 } 701 702 /* 703 * make sure the source and the destination are 704 * in the same dev filesystem 705 */ 706 if (odvp != ndvp) { 707 vattr.va_mask = AT_FSID; 708 if (error = VOP_GETATTR(odvp, &vattr, 0, cred, ct)) { 709 mutex_exit(&sdev_lock); 710 VN_RELE(ovp); 711 return (error); 712 } 713 fsid = vattr.va_fsid; 714 vattr.va_mask = AT_FSID; 715 if (error = VOP_GETATTR(ndvp, &vattr, 0, cred, ct)) { 716 mutex_exit(&sdev_lock); 717 VN_RELE(ovp); 718 return (error); 719 } 720 if (fsid != vattr.va_fsid) { 721 mutex_exit(&sdev_lock); 722 VN_RELE(ovp); 723 return (EXDEV); 724 } 725 } 726 727 /* make sure the old entry can be deleted */ 728 error = VOP_ACCESS(odvp, VWRITE, 0, cred, ct); 729 if (error) { 730 mutex_exit(&sdev_lock); 731 VN_RELE(ovp); 732 return (error); 733 } 734 735 /* make sure the destination allows creation */ 736 samedir = (fromparent == toparent); 737 if (!samedir) { 738 error = VOP_ACCESS(ndvp, VEXEC|VWRITE, 0, cred, ct); 739 if (error) { 740 mutex_exit(&sdev_lock); 741 VN_RELE(ovp); 742 return (error); 743 } 744 } 745 746 fromdv = VTOSDEV(ovp); 747 ASSERT(fromdv); 748 749 /* destination file exists */ 750 if (nvp) { 751 todv = VTOSDEV(nvp); 752 ASSERT(todv); 753 } 754 755 /* 756 * link source to new target in the memory 757 */ 758 error = sdev_rnmnode(fromparent, fromdv, toparent, &todv, nnm, cred); 759 if (error) { 760 sdcmn_err2(("sdev_rename: renaming %s to %s failed " 761 " with error %d\n", onm, nnm, error)); 762 mutex_exit(&sdev_lock); 763 if (nvp) 764 VN_RELE(nvp); 765 VN_RELE(ovp); 766 return (error); 767 } 768 769 /* 770 * unlink from source 771 */ 772 rw_enter(&fromparent->sdev_contents, RW_READER); 773 fromdv = sdev_cache_lookup(fromparent, onm); 774 if (fromdv == NULL) { 775 rw_exit(&fromparent->sdev_contents); 776 mutex_exit(&sdev_lock); 777 sdcmn_err2(("sdev_rename: the source is deleted already\n")); 778 return (0); 779 } 780 781 if (fromdv->sdev_state == SDEV_ZOMBIE) { 782 rw_exit(&fromparent->sdev_contents); 783 mutex_exit(&sdev_lock); 784 VN_RELE(SDEVTOV(fromdv)); 785 sdcmn_err2(("sdev_rename: the source is being deleted\n")); 786 return (0); 787 } 788 rw_exit(&fromparent->sdev_contents); 789 ASSERT(SDEVTOV(fromdv) == ovp); 790 VN_RELE(ovp); 791 792 /* clean out the directory contents before it can be removed */ 793 type = SDEVTOV(fromdv)->v_type; 794 if (type == VDIR) { 795 error = sdev_cleandir(fromdv, NULL, 0); 796 sdcmn_err2(("sdev_rename: cleandir finished with %d\n", 797 error)); 798 if (error == EBUSY) 799 error = 0; 800 } 801 802 rw_enter(&fromparent->sdev_contents, RW_WRITER); 803 bkstore = SDEV_IS_PERSIST(fromdv) ? 1 : 0; 804 error = sdev_cache_update(fromparent, &fromdv, onm, 805 SDEV_CACHE_DELETE); 806 807 /* best effforts clean up the backing store */ 808 if (bkstore) { 809 ASSERT(fromparent->sdev_attrvp); 810 if (type != VDIR) { 811 /* XXXci - We may need to translate the C-I flags on VOP_REMOVE */ 812 error = VOP_REMOVE(fromparent->sdev_attrvp, 813 onm, kcred, ct, 0); 814 } else { 815 /* XXXci - We may need to translate the C-I flags on VOP_RMDIR */ 816 error = VOP_RMDIR(fromparent->sdev_attrvp, 817 onm, fromparent->sdev_attrvp, kcred, ct, 0); 818 } 819 820 if (error) { 821 sdcmn_err2(("sdev_rename: device %s is " 822 "still on disk %s\n", onm, 823 fromparent->sdev_path)); 824 error = 0; 825 } 826 } 827 rw_exit(&fromparent->sdev_contents); 828 mutex_exit(&sdev_lock); 829 830 /* once reached to this point, the rename is regarded successful */ 831 return (0); 832 } 833 834 /* 835 * dev-fs version of "ln -s path dev-name" 836 * tnm - path, e.g. /devices/... or /dev/... 837 * lnm - dev_name 838 */ 839 /*ARGSUSED6*/ 840 static int 841 sdev_symlink(struct vnode *dvp, char *lnm, struct vattr *tva, 842 char *tnm, struct cred *cred, caller_context_t *ct, int flags) 843 { 844 int error; 845 struct vnode *vp = NULL; 846 struct sdev_node *parent = (struct sdev_node *)VTOSDEV(dvp); 847 struct sdev_node *self = (struct sdev_node *)NULL; 848 849 ASSERT(parent); 850 rw_enter(&parent->sdev_dotdot->sdev_contents, RW_READER); 851 if (parent->sdev_state == SDEV_ZOMBIE) { 852 rw_exit(&parent->sdev_dotdot->sdev_contents); 853 sdcmn_err2(("sdev_symlink: parent %s is ZOMBIED \n", 854 parent->sdev_name)); 855 return (ENOENT); 856 } 857 858 if (!SDEV_IS_GLOBAL(parent)) { 859 rw_exit(&parent->sdev_dotdot->sdev_contents); 860 return (ENOTSUP); 861 } 862 rw_exit(&parent->sdev_dotdot->sdev_contents); 863 864 /* execute access is required to search a directory */ 865 if ((error = VOP_ACCESS(dvp, VEXEC, 0, cred, ct)) != 0) 866 return (error); 867 868 /* find existing name */ 869 /* XXXci - We may need to translate the C-I flags here */ 870 error = VOP_LOOKUP(dvp, lnm, &vp, NULL, 0, NULL, cred, ct, NULL, NULL); 871 if (error == 0) { 872 ASSERT(vp); 873 VN_RELE(vp); 874 sdcmn_err2(("sdev_symlink: node %s already exists\n", lnm)); 875 return (EEXIST); 876 } 877 if (error != ENOENT) 878 return (error); 879 880 /* write access is required to create a symlink */ 881 if ((error = VOP_ACCESS(dvp, VWRITE, 0, cred, ct)) != 0) 882 return (error); 883 884 /* put it into memory cache */ 885 rw_enter(&parent->sdev_contents, RW_WRITER); 886 error = sdev_mknode(parent, lnm, &self, tva, NULL, (void *)tnm, 887 cred, SDEV_READY); 888 if (error) { 889 rw_exit(&parent->sdev_contents); 890 sdcmn_err2(("sdev_symlink: node %s creation failed\n", lnm)); 891 if (self) 892 SDEV_RELE(self); 893 894 return (error); 895 } 896 ASSERT(self && (self->sdev_state == SDEV_READY)); 897 rw_exit(&parent->sdev_contents); 898 899 /* take care the timestamps for the node and its parent */ 900 sdev_update_timestamps(SDEVTOV(self), kcred, 901 AT_CTIME|AT_MTIME|AT_ATIME); 902 sdev_update_timestamps(dvp, kcred, AT_MTIME|AT_ATIME); 903 if (SDEV_IS_GLOBAL(parent)) 904 atomic_inc_ulong(&parent->sdev_gdir_gen); 905 906 /* wake up other threads blocked on looking up this node */ 907 mutex_enter(&self->sdev_lookup_lock); 908 SDEV_UNBLOCK_OTHERS(self, SDEV_LOOKUP); 909 mutex_exit(&self->sdev_lookup_lock); 910 SDEV_RELE(self); /* don't return with vnode held */ 911 return (0); 912 } 913 914 /*ARGSUSED6*/ 915 static int 916 sdev_mkdir(struct vnode *dvp, char *nm, struct vattr *va, struct vnode **vpp, 917 struct cred *cred, caller_context_t *ct, int flags, vsecattr_t *vsecp) 918 { 919 int error; 920 struct sdev_node *parent = (struct sdev_node *)VTOSDEV(dvp); 921 struct sdev_node *self = NULL; 922 struct vnode *vp = NULL; 923 924 ASSERT(parent && parent->sdev_dotdot); 925 rw_enter(&parent->sdev_dotdot->sdev_contents, RW_READER); 926 if (parent->sdev_state == SDEV_ZOMBIE) { 927 rw_exit(&parent->sdev_dotdot->sdev_contents); 928 return (ENOENT); 929 } 930 931 /* non-global do not allow pure directory creation */ 932 if (!SDEV_IS_GLOBAL(parent)) { 933 rw_exit(&parent->sdev_dotdot->sdev_contents); 934 return (prof_lookup(dvp, nm, vpp, cred)); 935 } 936 rw_exit(&parent->sdev_dotdot->sdev_contents); 937 938 /* execute access is required to search the directory */ 939 if ((error = VOP_ACCESS(dvp, VEXEC, 0, cred, ct)) != 0) { 940 return (error); 941 } 942 943 /* find existing name */ 944 /* XXXci - We may need to translate the C-I flags on VOP_LOOKUP */ 945 error = VOP_LOOKUP(dvp, nm, &vp, NULL, 0, NULL, cred, ct, NULL, NULL); 946 if (error == 0) { 947 VN_RELE(vp); 948 return (EEXIST); 949 } 950 if (error != ENOENT) 951 return (error); 952 953 /* require write access to create a directory */ 954 if ((error = VOP_ACCESS(dvp, VWRITE, 0, cred, ct)) != 0) { 955 return (error); 956 } 957 958 /* put it into memory */ 959 rw_enter(&parent->sdev_contents, RW_WRITER); 960 error = sdev_mknode(parent, nm, &self, 961 va, NULL, NULL, cred, SDEV_READY); 962 if (error) { 963 rw_exit(&parent->sdev_contents); 964 if (self) 965 SDEV_RELE(self); 966 return (error); 967 } 968 ASSERT(self && (self->sdev_state == SDEV_READY)); 969 rw_exit(&parent->sdev_contents); 970 971 /* take care the timestamps for the node and its parent */ 972 sdev_update_timestamps(SDEVTOV(self), kcred, 973 AT_CTIME|AT_MTIME|AT_ATIME); 974 sdev_update_timestamps(dvp, kcred, AT_MTIME|AT_ATIME); 975 if (SDEV_IS_GLOBAL(parent)) 976 atomic_inc_ulong(&parent->sdev_gdir_gen); 977 978 /* wake up other threads blocked on looking up this node */ 979 mutex_enter(&self->sdev_lookup_lock); 980 SDEV_UNBLOCK_OTHERS(self, SDEV_LOOKUP); 981 mutex_exit(&self->sdev_lookup_lock); 982 *vpp = SDEVTOV(self); 983 return (0); 984 } 985 986 /* 987 * allowing removing an empty directory under /dev 988 */ 989 /*ARGSUSED*/ 990 static int 991 sdev_rmdir(struct vnode *dvp, char *nm, struct vnode *cdir, struct cred *cred, 992 caller_context_t *ct, int flags) 993 { 994 int error = 0; 995 struct sdev_node *parent = (struct sdev_node *)VTOSDEV(dvp); 996 struct sdev_node *self = NULL; 997 struct vnode *vp = NULL; 998 999 /* bail out early */ 1000 if (strcmp(nm, ".") == 0) 1001 return (EINVAL); 1002 if (strcmp(nm, "..") == 0) 1003 return (EEXIST); /* should be ENOTEMPTY */ 1004 1005 /* no destruction of non-global node */ 1006 ASSERT(parent && parent->sdev_dotdot); 1007 rw_enter(&parent->sdev_dotdot->sdev_contents, RW_READER); 1008 if (!SDEV_IS_GLOBAL(parent)) { 1009 rw_exit(&parent->sdev_dotdot->sdev_contents); 1010 return (ENOTSUP); 1011 } 1012 rw_exit(&parent->sdev_dotdot->sdev_contents); 1013 1014 /* execute access is required to search the directory */ 1015 if ((error = VOP_ACCESS(dvp, VEXEC|VWRITE, 0, cred, ct)) != 0) 1016 return (error); 1017 1018 /* check existing name */ 1019 rw_enter(&parent->sdev_contents, RW_WRITER); 1020 self = sdev_cache_lookup(parent, nm); 1021 if (self == NULL) { 1022 rw_exit(&parent->sdev_contents); 1023 return (ENOENT); 1024 } 1025 1026 vp = SDEVTOV(self); 1027 if ((self->sdev_state == SDEV_INIT) || 1028 (self->sdev_state == SDEV_ZOMBIE)) { 1029 rw_exit(&parent->sdev_contents); 1030 VN_RELE(vp); 1031 return (ENOENT); 1032 } 1033 1034 /* some sanity checks */ 1035 if (vp == dvp || vp == cdir) { 1036 rw_exit(&parent->sdev_contents); 1037 VN_RELE(vp); 1038 return (EINVAL); 1039 } 1040 1041 if (vp->v_type != VDIR) { 1042 rw_exit(&parent->sdev_contents); 1043 VN_RELE(vp); 1044 return (ENOTDIR); 1045 } 1046 1047 if (vn_vfswlock(vp)) { 1048 rw_exit(&parent->sdev_contents); 1049 VN_RELE(vp); 1050 return (EBUSY); 1051 } 1052 1053 if (vn_mountedvfs(vp) != NULL) { 1054 rw_exit(&parent->sdev_contents); 1055 vn_vfsunlock(vp); 1056 VN_RELE(vp); 1057 return (EBUSY); 1058 } 1059 1060 self = VTOSDEV(vp); 1061 /* bail out on a non-empty directory */ 1062 rw_enter(&self->sdev_contents, RW_READER); 1063 if (self->sdev_nlink > 2) { 1064 rw_exit(&self->sdev_contents); 1065 rw_exit(&parent->sdev_contents); 1066 vn_vfsunlock(vp); 1067 VN_RELE(vp); 1068 return (ENOTEMPTY); 1069 } 1070 rw_exit(&self->sdev_contents); 1071 1072 /* unlink it from the directory cache */ 1073 error = sdev_cache_update(parent, &self, nm, SDEV_CACHE_DELETE); 1074 rw_exit(&parent->sdev_contents); 1075 vn_vfsunlock(vp); 1076 1077 if (error && (error != EBUSY)) { 1078 VN_RELE(vp); 1079 } else { 1080 sdcmn_err2(("sdev_rmdir: cleaning node %s from directory " 1081 " cache with error %d\n", nm, error)); 1082 1083 /* best effort to clean up the backing store */ 1084 if (SDEV_IS_PERSIST(parent)) { 1085 ASSERT(parent->sdev_attrvp); 1086 error = VOP_RMDIR(parent->sdev_attrvp, nm, 1087 parent->sdev_attrvp, kcred, ct, flags); 1088 sdcmn_err2(("sdev_rmdir: cleaning device %s is on" 1089 " disk error %d\n", parent->sdev_path, error)); 1090 } 1091 1092 if (error == EBUSY) 1093 error = 0; 1094 } 1095 1096 return (error); 1097 } 1098 1099 /* 1100 * read the contents of a symbolic link 1101 */ 1102 static int 1103 sdev_readlink(struct vnode *vp, struct uio *uiop, struct cred *cred, 1104 caller_context_t *ct) 1105 { 1106 struct sdev_node *dv; 1107 int error = 0; 1108 1109 ASSERT(vp->v_type == VLNK); 1110 1111 dv = VTOSDEV(vp); 1112 1113 if (dv->sdev_attrvp) { 1114 /* non-NULL attrvp implys a persisted node at READY state */ 1115 return (VOP_READLINK(dv->sdev_attrvp, uiop, cred, ct)); 1116 } else if (dv->sdev_symlink != NULL) { 1117 /* memory nodes, e.g. local nodes */ 1118 rw_enter(&dv->sdev_contents, RW_READER); 1119 sdcmn_err2(("sdev_readlink link is %s\n", dv->sdev_symlink)); 1120 error = uiomove(dv->sdev_symlink, strlen(dv->sdev_symlink), 1121 UIO_READ, uiop); 1122 rw_exit(&dv->sdev_contents); 1123 return (error); 1124 } 1125 1126 return (ENOENT); 1127 } 1128 1129 /*ARGSUSED4*/ 1130 static int 1131 sdev_readdir(struct vnode *dvp, struct uio *uiop, struct cred *cred, int *eofp, 1132 caller_context_t *ct, int flags) 1133 { 1134 struct sdev_node *parent = VTOSDEV(dvp); 1135 int error; 1136 1137 /* execute access is required to search the directory */ 1138 if ((error = VOP_ACCESS(dvp, VEXEC, 0, cred, ct)) != 0) 1139 return (error); 1140 1141 ASSERT(parent); 1142 if (!SDEV_IS_GLOBAL(parent)) 1143 prof_filldir(parent); 1144 return (devname_readdir_func(dvp, uiop, cred, eofp, SDEV_BROWSE)); 1145 } 1146 1147 /*ARGSUSED1*/ 1148 static void 1149 sdev_inactive(struct vnode *vp, struct cred *cred, caller_context_t *ct) 1150 { 1151 devname_inactive_func(vp, cred, NULL); 1152 } 1153 1154 /*ARGSUSED2*/ 1155 static int 1156 sdev_fid(struct vnode *vp, struct fid *fidp, caller_context_t *ct) 1157 { 1158 struct sdev_node *dv = VTOSDEV(vp); 1159 struct sdev_fid *sdev_fid; 1160 1161 if (fidp->fid_len < (sizeof (struct sdev_fid) - sizeof (ushort_t))) { 1162 fidp->fid_len = sizeof (struct sdev_fid) - sizeof (ushort_t); 1163 return (ENOSPC); 1164 } 1165 1166 sdev_fid = (struct sdev_fid *)fidp; 1167 bzero(sdev_fid, sizeof (struct sdev_fid)); 1168 sdev_fid->sdevfid_len = 1169 (int)sizeof (struct sdev_fid) - sizeof (ushort_t); 1170 sdev_fid->sdevfid_ino = dv->sdev_ino; 1171 1172 return (0); 1173 } 1174 1175 /* 1176 * This pair of routines bracket all VOP_READ, VOP_WRITE 1177 * and VOP_READDIR requests. The contents lock stops things 1178 * moving around while we're looking at them. 1179 */ 1180 /*ARGSUSED2*/ 1181 static int 1182 sdev_rwlock(struct vnode *vp, int write_flag, caller_context_t *ctp) 1183 { 1184 rw_enter(&VTOSDEV(vp)->sdev_contents, 1185 write_flag ? RW_WRITER : RW_READER); 1186 return (write_flag ? V_WRITELOCK_TRUE : V_WRITELOCK_FALSE); 1187 } 1188 1189 /*ARGSUSED1*/ 1190 static void 1191 sdev_rwunlock(struct vnode *vp, int write_flag, caller_context_t *ctp) 1192 { 1193 rw_exit(&VTOSDEV(vp)->sdev_contents); 1194 } 1195 1196 /*ARGSUSED1*/ 1197 static int 1198 sdev_seek(struct vnode *vp, offset_t ooff, offset_t *noffp, 1199 caller_context_t *ct) 1200 { 1201 struct vnode *attrvp = VTOSDEV(vp)->sdev_attrvp; 1202 1203 ASSERT(vp->v_type != VCHR && 1204 vp->v_type != VBLK && vp->v_type != VLNK); 1205 1206 if (vp->v_type == VDIR) 1207 return (fs_seek(vp, ooff, noffp, ct)); 1208 1209 ASSERT(attrvp); 1210 return (VOP_SEEK(attrvp, ooff, noffp, ct)); 1211 } 1212 1213 /*ARGSUSED1*/ 1214 static int 1215 sdev_frlock(struct vnode *vp, int cmd, struct flock64 *bfp, int flag, 1216 offset_t offset, struct flk_callback *flk_cbp, struct cred *cr, 1217 caller_context_t *ct) 1218 { 1219 int error; 1220 struct sdev_node *dv = VTOSDEV(vp); 1221 1222 ASSERT(dv); 1223 ASSERT(dv->sdev_attrvp); 1224 error = VOP_FRLOCK(dv->sdev_attrvp, cmd, bfp, flag, offset, 1225 flk_cbp, cr, ct); 1226 1227 return (error); 1228 } 1229 1230 static int 1231 sdev_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr, 1232 caller_context_t *ct) 1233 { 1234 switch (cmd) { 1235 case _PC_ACL_ENABLED: 1236 *valp = SDEV_ACL_FLAVOR(vp); 1237 return (0); 1238 } 1239 1240 return (fs_pathconf(vp, cmd, valp, cr, ct)); 1241 } 1242 1243 vnodeops_t *sdev_vnodeops; 1244 1245 const fs_operation_def_t sdev_vnodeops_tbl[] = { 1246 VOPNAME_OPEN, { .vop_open = sdev_open }, 1247 VOPNAME_CLOSE, { .vop_close = sdev_close }, 1248 VOPNAME_READ, { .vop_read = sdev_read }, 1249 VOPNAME_WRITE, { .vop_write = sdev_write }, 1250 VOPNAME_IOCTL, { .vop_ioctl = sdev_ioctl }, 1251 VOPNAME_GETATTR, { .vop_getattr = sdev_getattr }, 1252 VOPNAME_SETATTR, { .vop_setattr = sdev_setattr }, 1253 VOPNAME_ACCESS, { .vop_access = sdev_access }, 1254 VOPNAME_LOOKUP, { .vop_lookup = sdev_lookup }, 1255 VOPNAME_CREATE, { .vop_create = sdev_create }, 1256 VOPNAME_RENAME, { .vop_rename = sdev_rename }, 1257 VOPNAME_REMOVE, { .vop_remove = sdev_remove }, 1258 VOPNAME_MKDIR, { .vop_mkdir = sdev_mkdir }, 1259 VOPNAME_RMDIR, { .vop_rmdir = sdev_rmdir }, 1260 VOPNAME_READDIR, { .vop_readdir = sdev_readdir }, 1261 VOPNAME_SYMLINK, { .vop_symlink = sdev_symlink }, 1262 VOPNAME_READLINK, { .vop_readlink = sdev_readlink }, 1263 VOPNAME_INACTIVE, { .vop_inactive = sdev_inactive }, 1264 VOPNAME_FID, { .vop_fid = sdev_fid }, 1265 VOPNAME_RWLOCK, { .vop_rwlock = sdev_rwlock }, 1266 VOPNAME_RWUNLOCK, { .vop_rwunlock = sdev_rwunlock }, 1267 VOPNAME_SEEK, { .vop_seek = sdev_seek }, 1268 VOPNAME_FRLOCK, { .vop_frlock = sdev_frlock }, 1269 VOPNAME_PATHCONF, { .vop_pathconf = sdev_pathconf }, 1270 VOPNAME_SETSECATTR, { .vop_setsecattr = sdev_setsecattr }, 1271 VOPNAME_GETSECATTR, { .vop_getsecattr = sdev_getsecattr }, 1272 NULL, NULL 1273 }; 1274 1275 int sdev_vnodeops_tbl_size = sizeof (sdev_vnodeops_tbl); 1276