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, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */ 28 /* All Rights Reserved */ 29 30 /* 31 * University Copyright- Copyright (c) 1982, 1986, 1988 32 * The Regents of the University of California 33 * All Rights Reserved 34 * 35 * University Acknowledgment- Portions of this document are derived from 36 * software developed by the University of California, Berkeley, and its 37 * contributors. 38 */ 39 40 41 #pragma ident "%Z%%M% %I% %E% SMI" 42 43 #include <sys/types.h> 44 #include <sys/param.h> 45 #include <sys/t_lock.h> 46 #include <sys/errno.h> 47 #include <sys/cred.h> 48 #include <sys/user.h> 49 #include <sys/uio.h> 50 #include <sys/file.h> 51 #include <sys/pathname.h> 52 #include <sys/vfs.h> 53 #include <sys/vnode.h> 54 #include <sys/rwstlock.h> 55 #include <sys/fem.h> 56 #include <sys/stat.h> 57 #include <sys/mode.h> 58 #include <sys/conf.h> 59 #include <sys/sysmacros.h> 60 #include <sys/cmn_err.h> 61 #include <sys/systm.h> 62 #include <sys/kmem.h> 63 #include <sys/debug.h> 64 #include <c2/audit.h> 65 #include <sys/acl.h> 66 #include <sys/nbmlock.h> 67 #include <sys/fcntl.h> 68 #include <fs/fs_subr.h> 69 70 /* Determine if this vnode is a file that is read-only */ 71 #define ISROFILE(vp) \ 72 ((vp)->v_type != VCHR && (vp)->v_type != VBLK && \ 73 (vp)->v_type != VFIFO && vn_is_readonly(vp)) 74 75 /* Tunable via /etc/system; used only by admin/install */ 76 int nfs_global_client_only; 77 78 /* 79 * Convert stat(2) formats to vnode types and vice versa. (Knows about 80 * numerical order of S_IFMT and vnode types.) 81 */ 82 enum vtype iftovt_tab[] = { 83 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON, 84 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VNON 85 }; 86 87 ushort_t vttoif_tab[] = { 88 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK, S_IFIFO, 89 S_IFDOOR, 0, S_IFSOCK, S_IFPORT, 0 90 }; 91 92 /* 93 * The system vnode cache. 94 */ 95 96 kmem_cache_t *vn_cache; 97 98 99 /* 100 * Vnode operations vector. 101 */ 102 103 static const fs_operation_trans_def_t vn_ops_table[] = { 104 VOPNAME_OPEN, offsetof(struct vnodeops, vop_open), 105 fs_nosys, fs_nosys, 106 107 VOPNAME_CLOSE, offsetof(struct vnodeops, vop_close), 108 fs_nosys, fs_nosys, 109 110 VOPNAME_READ, offsetof(struct vnodeops, vop_read), 111 fs_nosys, fs_nosys, 112 113 VOPNAME_WRITE, offsetof(struct vnodeops, vop_write), 114 fs_nosys, fs_nosys, 115 116 VOPNAME_IOCTL, offsetof(struct vnodeops, vop_ioctl), 117 fs_nosys, fs_nosys, 118 119 VOPNAME_SETFL, offsetof(struct vnodeops, vop_setfl), 120 fs_setfl, fs_nosys, 121 122 VOPNAME_GETATTR, offsetof(struct vnodeops, vop_getattr), 123 fs_nosys, fs_nosys, 124 125 VOPNAME_SETATTR, offsetof(struct vnodeops, vop_setattr), 126 fs_nosys, fs_nosys, 127 128 VOPNAME_ACCESS, offsetof(struct vnodeops, vop_access), 129 fs_nosys, fs_nosys, 130 131 VOPNAME_LOOKUP, offsetof(struct vnodeops, vop_lookup), 132 fs_nosys, fs_nosys, 133 134 VOPNAME_CREATE, offsetof(struct vnodeops, vop_create), 135 fs_nosys, fs_nosys, 136 137 VOPNAME_REMOVE, offsetof(struct vnodeops, vop_remove), 138 fs_nosys, fs_nosys, 139 140 VOPNAME_LINK, offsetof(struct vnodeops, vop_link), 141 fs_nosys, fs_nosys, 142 143 VOPNAME_RENAME, offsetof(struct vnodeops, vop_rename), 144 fs_nosys, fs_nosys, 145 146 VOPNAME_MKDIR, offsetof(struct vnodeops, vop_mkdir), 147 fs_nosys, fs_nosys, 148 149 VOPNAME_RMDIR, offsetof(struct vnodeops, vop_rmdir), 150 fs_nosys, fs_nosys, 151 152 VOPNAME_READDIR, offsetof(struct vnodeops, vop_readdir), 153 fs_nosys, fs_nosys, 154 155 VOPNAME_SYMLINK, offsetof(struct vnodeops, vop_symlink), 156 fs_nosys, fs_nosys, 157 158 VOPNAME_READLINK, offsetof(struct vnodeops, vop_readlink), 159 fs_nosys, fs_nosys, 160 161 VOPNAME_FSYNC, offsetof(struct vnodeops, vop_fsync), 162 fs_nosys, fs_nosys, 163 164 VOPNAME_INACTIVE, offsetof(struct vnodeops, vop_inactive), 165 fs_nosys, fs_nosys, 166 167 VOPNAME_FID, offsetof(struct vnodeops, vop_fid), 168 fs_nosys, fs_nosys, 169 170 VOPNAME_RWLOCK, offsetof(struct vnodeops, vop_rwlock), 171 fs_rwlock, fs_rwlock, 172 173 VOPNAME_RWUNLOCK, offsetof(struct vnodeops, vop_rwunlock), 174 (fs_generic_func_p) fs_rwunlock, 175 (fs_generic_func_p) fs_rwunlock, /* no errors allowed */ 176 177 VOPNAME_SEEK, offsetof(struct vnodeops, vop_seek), 178 fs_nosys, fs_nosys, 179 180 VOPNAME_CMP, offsetof(struct vnodeops, vop_cmp), 181 fs_cmp, fs_cmp, /* no errors allowed */ 182 183 VOPNAME_FRLOCK, offsetof(struct vnodeops, vop_frlock), 184 fs_frlock, fs_nosys, 185 186 VOPNAME_SPACE, offsetof(struct vnodeops, vop_space), 187 fs_nosys, fs_nosys, 188 189 VOPNAME_REALVP, offsetof(struct vnodeops, vop_realvp), 190 fs_nosys, fs_nosys, 191 192 VOPNAME_GETPAGE, offsetof(struct vnodeops, vop_getpage), 193 fs_nosys, fs_nosys, 194 195 VOPNAME_PUTPAGE, offsetof(struct vnodeops, vop_putpage), 196 fs_nosys, fs_nosys, 197 198 VOPNAME_MAP, offsetof(struct vnodeops, vop_map), 199 (fs_generic_func_p) fs_nosys_map, 200 (fs_generic_func_p) fs_nosys_map, 201 202 VOPNAME_ADDMAP, offsetof(struct vnodeops, vop_addmap), 203 (fs_generic_func_p) fs_nosys_addmap, 204 (fs_generic_func_p) fs_nosys_addmap, 205 206 VOPNAME_DELMAP, offsetof(struct vnodeops, vop_delmap), 207 fs_nosys, fs_nosys, 208 209 VOPNAME_POLL, offsetof(struct vnodeops, vop_poll), 210 (fs_generic_func_p) fs_poll, (fs_generic_func_p) fs_nosys_poll, 211 212 VOPNAME_DUMP, offsetof(struct vnodeops, vop_dump), 213 fs_nosys, fs_nosys, 214 215 VOPNAME_PATHCONF, offsetof(struct vnodeops, vop_pathconf), 216 fs_pathconf, fs_nosys, 217 218 VOPNAME_PAGEIO, offsetof(struct vnodeops, vop_pageio), 219 fs_nosys, fs_nosys, 220 221 VOPNAME_DUMPCTL, offsetof(struct vnodeops, vop_dumpctl), 222 fs_nosys, fs_nosys, 223 224 VOPNAME_DISPOSE, offsetof(struct vnodeops, vop_dispose), 225 (fs_generic_func_p) fs_dispose, 226 (fs_generic_func_p) fs_nodispose, 227 228 VOPNAME_SETSECATTR, offsetof(struct vnodeops, vop_setsecattr), 229 fs_nosys, fs_nosys, 230 231 VOPNAME_GETSECATTR, offsetof(struct vnodeops, vop_getsecattr), 232 fs_fab_acl, fs_nosys, 233 234 VOPNAME_SHRLOCK, offsetof(struct vnodeops, vop_shrlock), 235 fs_shrlock, fs_nosys, 236 237 VOPNAME_VNEVENT, offsetof(struct vnodeops, vop_vnevent), 238 (fs_generic_func_p) fs_vnevent_nosupport, 239 (fs_generic_func_p) fs_vnevent_nosupport, 240 241 NULL, 0, NULL, NULL 242 }; 243 244 245 /* 246 * Read or write a vnode. Called from kernel code. 247 */ 248 int 249 vn_rdwr( 250 enum uio_rw rw, 251 struct vnode *vp, 252 caddr_t base, 253 ssize_t len, 254 offset_t offset, 255 enum uio_seg seg, 256 int ioflag, 257 rlim64_t ulimit, /* meaningful only if rw is UIO_WRITE */ 258 cred_t *cr, 259 ssize_t *residp) 260 { 261 struct uio uio; 262 struct iovec iov; 263 int error; 264 int in_crit = 0; 265 266 if (rw == UIO_WRITE && ISROFILE(vp)) 267 return (EROFS); 268 269 if (len < 0) 270 return (EIO); 271 272 iov.iov_base = base; 273 iov.iov_len = len; 274 uio.uio_iov = &iov; 275 uio.uio_iovcnt = 1; 276 uio.uio_loffset = offset; 277 uio.uio_segflg = (short)seg; 278 uio.uio_resid = len; 279 uio.uio_llimit = ulimit; 280 281 /* 282 * We have to enter the critical region before calling VOP_RWLOCK 283 * to avoid a deadlock with ufs. 284 */ 285 if (nbl_need_check(vp)) { 286 int svmand; 287 288 nbl_start_crit(vp, RW_READER); 289 in_crit = 1; 290 error = nbl_svmand(vp, cr, &svmand); 291 if (error != 0) 292 goto done; 293 if (nbl_conflict(vp, rw == UIO_WRITE ? NBL_WRITE : NBL_READ, 294 uio.uio_offset, uio.uio_resid, svmand)) { 295 error = EACCES; 296 goto done; 297 } 298 } 299 300 (void) VOP_RWLOCK(vp, 301 rw == UIO_WRITE ? V_WRITELOCK_TRUE : V_WRITELOCK_FALSE, NULL); 302 if (rw == UIO_WRITE) { 303 uio.uio_fmode = FWRITE; 304 uio.uio_extflg = UIO_COPY_DEFAULT; 305 error = VOP_WRITE(vp, &uio, ioflag, cr, NULL); 306 } else { 307 uio.uio_fmode = FREAD; 308 uio.uio_extflg = UIO_COPY_CACHED; 309 error = VOP_READ(vp, &uio, ioflag, cr, NULL); 310 } 311 VOP_RWUNLOCK(vp, rw == UIO_WRITE ? V_WRITELOCK_TRUE : V_WRITELOCK_FALSE, 312 NULL); 313 if (residp) 314 *residp = uio.uio_resid; 315 else if (uio.uio_resid) 316 error = EIO; 317 318 done: 319 if (in_crit) 320 nbl_end_crit(vp); 321 return (error); 322 } 323 324 /* 325 * Release a vnode. Call VOP_INACTIVE on last reference or 326 * decrement reference count. 327 * 328 * To avoid race conditions, the v_count is left at 1 for 329 * the call to VOP_INACTIVE. This prevents another thread 330 * from reclaiming and releasing the vnode *before* the 331 * VOP_INACTIVE routine has a chance to destroy the vnode. 332 * We can't have more than 1 thread calling VOP_INACTIVE 333 * on a vnode. 334 */ 335 void 336 vn_rele(vnode_t *vp) 337 { 338 if (vp->v_count == 0) 339 cmn_err(CE_PANIC, "vn_rele: vnode ref count 0"); 340 mutex_enter(&vp->v_lock); 341 if (vp->v_count == 1) { 342 mutex_exit(&vp->v_lock); 343 VOP_INACTIVE(vp, CRED()); 344 } else { 345 vp->v_count--; 346 mutex_exit(&vp->v_lock); 347 } 348 } 349 350 /* 351 * Like vn_rele() except that it clears v_stream under v_lock. 352 * This is used by sockfs when it dismantels the association between 353 * the sockfs node and the vnode in the underlaying file system. 354 * v_lock has to be held to prevent a thread coming through the lookupname 355 * path from accessing a stream head that is going away. 356 */ 357 void 358 vn_rele_stream(vnode_t *vp) 359 { 360 if (vp->v_count == 0) 361 cmn_err(CE_PANIC, "vn_rele: vnode ref count 0"); 362 mutex_enter(&vp->v_lock); 363 vp->v_stream = NULL; 364 if (vp->v_count == 1) { 365 mutex_exit(&vp->v_lock); 366 VOP_INACTIVE(vp, CRED()); 367 } else { 368 vp->v_count--; 369 mutex_exit(&vp->v_lock); 370 } 371 } 372 373 int 374 vn_open( 375 char *pnamep, 376 enum uio_seg seg, 377 int filemode, 378 int createmode, 379 struct vnode **vpp, 380 enum create crwhy, 381 mode_t umask) 382 { 383 return (vn_openat(pnamep, seg, filemode, 384 createmode, vpp, crwhy, umask, NULL)); 385 } 386 387 388 /* 389 * Open/create a vnode. 390 * This may be callable by the kernel, the only known use 391 * of user context being that the current user credentials 392 * are used for permissions. crwhy is defined iff filemode & FCREAT. 393 */ 394 int 395 vn_openat( 396 char *pnamep, 397 enum uio_seg seg, 398 int filemode, 399 int createmode, 400 struct vnode **vpp, 401 enum create crwhy, 402 mode_t umask, 403 struct vnode *startvp) 404 { 405 struct vnode *vp; 406 int mode; 407 int error; 408 int in_crit = 0; 409 struct vattr vattr; 410 enum symfollow follow; 411 412 mode = 0; 413 if (filemode & FREAD) 414 mode |= VREAD; 415 if (filemode & (FWRITE|FTRUNC)) 416 mode |= VWRITE; 417 418 /* symlink interpretation */ 419 if (filemode & FNOFOLLOW) 420 follow = NO_FOLLOW; 421 else 422 follow = FOLLOW; 423 424 top: 425 if (filemode & FCREAT) { 426 enum vcexcl excl; 427 428 /* 429 * Wish to create a file. 430 */ 431 vattr.va_type = VREG; 432 vattr.va_mode = createmode; 433 vattr.va_mask = AT_TYPE|AT_MODE; 434 if (filemode & FTRUNC) { 435 vattr.va_size = 0; 436 vattr.va_mask |= AT_SIZE; 437 } 438 if (filemode & FEXCL) 439 excl = EXCL; 440 else 441 excl = NONEXCL; 442 443 if (error = 444 vn_createat(pnamep, seg, &vattr, excl, mode, &vp, crwhy, 445 (filemode & ~(FTRUNC|FEXCL)), 446 umask, startvp)) 447 return (error); 448 } else { 449 /* 450 * Wish to open a file. Just look it up. 451 */ 452 if (error = lookupnameat(pnamep, seg, follow, 453 NULLVPP, &vp, startvp)) { 454 if (error == ESTALE) 455 goto top; 456 return (error); 457 } 458 459 /* 460 * Get the attributes to check whether file is large. 461 * We do this only if the FOFFMAX flag is not set and 462 * only for regular files. 463 */ 464 465 if (!(filemode & FOFFMAX) && (vp->v_type == VREG)) { 466 vattr.va_mask = AT_SIZE; 467 if ((error = VOP_GETATTR(vp, &vattr, 0, CRED()))) { 468 goto out; 469 } 470 if (vattr.va_size > (u_offset_t)MAXOFF32_T) { 471 /* 472 * Large File API - regular open fails 473 * if FOFFMAX flag is set in file mode 474 */ 475 error = EOVERFLOW; 476 goto out; 477 } 478 } 479 /* 480 * Can't write directories, active texts, or 481 * read-only filesystems. Can't truncate files 482 * on which mandatory locking is in effect. 483 */ 484 if (filemode & (FWRITE|FTRUNC)) { 485 /* 486 * Allow writable directory if VDIROPEN flag is set. 487 */ 488 if (vp->v_type == VDIR && !(vp->v_flag & VDIROPEN)) { 489 error = EISDIR; 490 goto out; 491 } 492 if (ISROFILE(vp)) { 493 error = EROFS; 494 goto out; 495 } 496 /* 497 * Can't truncate files on which mandatory locking 498 * or non-blocking mandatory locking is in effect. 499 */ 500 if (filemode & FTRUNC) { 501 vnode_t *rvp; 502 503 if (VOP_REALVP(vp, &rvp) != 0) 504 rvp = vp; 505 if (nbl_need_check(vp)) { 506 nbl_start_crit(vp, RW_READER); 507 in_crit = 1; 508 vattr.va_mask = AT_MODE|AT_SIZE; 509 if ((error = VOP_GETATTR(vp, &vattr, 0, 510 CRED())) == 0) { 511 if (rvp->v_filocks != NULL) 512 if (MANDLOCK(vp, 513 vattr.va_mode)) 514 error = EAGAIN; 515 if (!error) { 516 if (nbl_conflict(vp, 517 NBL_WRITE, 0, 518 vattr.va_size, 0)) 519 error = EACCES; 520 } 521 } 522 } else if (rvp->v_filocks != NULL) { 523 vattr.va_mask = AT_MODE; 524 if ((error = VOP_GETATTR(vp, &vattr, 525 0, CRED())) == 0 && MANDLOCK(vp, 526 vattr.va_mode)) 527 error = EAGAIN; 528 } 529 } 530 if (error) 531 goto out; 532 } 533 /* 534 * Check permissions. 535 */ 536 if (error = VOP_ACCESS(vp, mode, 0, CRED())) 537 goto out; 538 } 539 540 /* 541 * Do remaining checks for FNOFOLLOW and FNOLINKS. 542 */ 543 if ((filemode & FNOFOLLOW) && vp->v_type == VLNK) { 544 error = EINVAL; 545 goto out; 546 } 547 if (filemode & FNOLINKS) { 548 vattr.va_mask = AT_NLINK; 549 if ((error = VOP_GETATTR(vp, &vattr, 0, CRED()))) { 550 goto out; 551 } 552 if (vattr.va_nlink != 1) { 553 error = EMLINK; 554 goto out; 555 } 556 } 557 558 /* 559 * Opening a socket corresponding to the AF_UNIX pathname 560 * in the filesystem name space is not supported. 561 * However, VSOCK nodes in namefs are supported in order 562 * to make fattach work for sockets. 563 * 564 * XXX This uses VOP_REALVP to distinguish between 565 * an unopened namefs node (where VOP_REALVP returns a 566 * different VSOCK vnode) and a VSOCK created by vn_create 567 * in some file system (where VOP_REALVP would never return 568 * a different vnode). 569 */ 570 if (vp->v_type == VSOCK) { 571 struct vnode *nvp; 572 573 error = VOP_REALVP(vp, &nvp); 574 if (error != 0 || nvp == NULL || nvp == vp || 575 nvp->v_type != VSOCK) { 576 error = EOPNOTSUPP; 577 goto out; 578 } 579 } 580 /* 581 * Do opening protocol. 582 */ 583 error = VOP_OPEN(&vp, filemode, CRED()); 584 /* 585 * Truncate if required. 586 */ 587 if (error == 0 && (filemode & FTRUNC) && !(filemode & FCREAT)) { 588 vattr.va_size = 0; 589 vattr.va_mask = AT_SIZE; 590 if ((error = VOP_SETATTR(vp, &vattr, 0, CRED(), NULL)) != 0) 591 (void) VOP_CLOSE(vp, filemode, 1, (offset_t)0, CRED()); 592 } 593 out: 594 ASSERT(vp->v_count > 0); 595 596 if (in_crit) { 597 nbl_end_crit(vp); 598 in_crit = 0; 599 } 600 if (error) { 601 /* 602 * The following clause was added to handle a problem 603 * with NFS consistency. It is possible that a lookup 604 * of the file to be opened succeeded, but the file 605 * itself doesn't actually exist on the server. This 606 * is chiefly due to the DNLC containing an entry for 607 * the file which has been removed on the server. In 608 * this case, we just start over. If there was some 609 * other cause for the ESTALE error, then the lookup 610 * of the file will fail and the error will be returned 611 * above instead of looping around from here. 612 */ 613 VN_RELE(vp); 614 if (error == ESTALE) 615 goto top; 616 } else 617 *vpp = vp; 618 return (error); 619 } 620 621 int 622 vn_create( 623 char *pnamep, 624 enum uio_seg seg, 625 struct vattr *vap, 626 enum vcexcl excl, 627 int mode, 628 struct vnode **vpp, 629 enum create why, 630 int flag, 631 mode_t umask) 632 { 633 return (vn_createat(pnamep, seg, vap, excl, mode, vpp, 634 why, flag, umask, NULL)); 635 } 636 637 /* 638 * Create a vnode (makenode). 639 */ 640 int 641 vn_createat( 642 char *pnamep, 643 enum uio_seg seg, 644 struct vattr *vap, 645 enum vcexcl excl, 646 int mode, 647 struct vnode **vpp, 648 enum create why, 649 int flag, 650 mode_t umask, 651 struct vnode *startvp) 652 { 653 struct vnode *dvp; /* ptr to parent dir vnode */ 654 struct vnode *vp = NULL; 655 struct pathname pn; 656 int error; 657 int in_crit = 0; 658 struct vattr vattr; 659 enum symfollow follow; 660 661 ASSERT((vap->va_mask & (AT_TYPE|AT_MODE)) == (AT_TYPE|AT_MODE)); 662 663 /* symlink interpretation */ 664 if ((flag & FNOFOLLOW) || excl == EXCL) 665 follow = NO_FOLLOW; 666 else 667 follow = FOLLOW; 668 flag &= ~(FNOFOLLOW|FNOLINKS); 669 670 top: 671 /* 672 * Lookup directory. 673 * If new object is a file, call lower level to create it. 674 * Note that it is up to the lower level to enforce exclusive 675 * creation, if the file is already there. 676 * This allows the lower level to do whatever 677 * locking or protocol that is needed to prevent races. 678 * If the new object is directory call lower level to make 679 * the new directory, with "." and "..". 680 */ 681 if (error = pn_get(pnamep, seg, &pn)) 682 return (error); 683 #ifdef C2_AUDIT 684 if (audit_active) 685 audit_vncreate_start(); 686 #endif /* C2_AUDIT */ 687 dvp = NULL; 688 *vpp = NULL; 689 /* 690 * lookup will find the parent directory for the vnode. 691 * When it is done the pn holds the name of the entry 692 * in the directory. 693 * If this is a non-exclusive create we also find the node itself. 694 */ 695 error = lookuppnat(&pn, NULL, follow, &dvp, 696 (excl == EXCL) ? NULLVPP : vpp, startvp); 697 if (error) { 698 pn_free(&pn); 699 if (error == ESTALE) 700 goto top; 701 if (why == CRMKDIR && error == EINVAL) 702 error = EEXIST; /* SVID */ 703 return (error); 704 } 705 706 if (why != CRMKNOD) 707 vap->va_mode &= ~VSVTX; 708 709 /* 710 * If default ACLs are defined for the directory don't apply the 711 * umask if umask is passed. 712 */ 713 714 if (umask) { 715 716 vsecattr_t vsec; 717 718 vsec.vsa_aclcnt = 0; 719 vsec.vsa_aclentp = NULL; 720 vsec.vsa_dfaclcnt = 0; 721 vsec.vsa_dfaclentp = NULL; 722 vsec.vsa_mask = VSA_DFACLCNT; 723 if (error = VOP_GETSECATTR(dvp, &vsec, 0, CRED())) { 724 if (*vpp != NULL) 725 VN_RELE(*vpp); 726 goto out; 727 } 728 729 /* 730 * Apply the umask if no default ACLs. 731 */ 732 if (vsec.vsa_dfaclcnt == 0) 733 vap->va_mode &= ~umask; 734 735 /* 736 * VOP_GETSECATTR() may have allocated memory for ACLs we 737 * didn't request, so double-check and free it if necessary. 738 */ 739 if (vsec.vsa_aclcnt && vsec.vsa_aclentp != NULL) 740 kmem_free((caddr_t)vsec.vsa_aclentp, 741 vsec.vsa_aclcnt * sizeof (aclent_t)); 742 if (vsec.vsa_dfaclcnt && vsec.vsa_dfaclentp != NULL) 743 kmem_free((caddr_t)vsec.vsa_dfaclentp, 744 vsec.vsa_dfaclcnt * sizeof (aclent_t)); 745 } 746 747 /* 748 * In general we want to generate EROFS if the file system is 749 * readonly. However, POSIX (IEEE Std. 1003.1) section 5.3.1 750 * documents the open system call, and it says that O_CREAT has no 751 * effect if the file already exists. Bug 1119649 states 752 * that open(path, O_CREAT, ...) fails when attempting to open an 753 * existing file on a read only file system. Thus, the first part 754 * of the following if statement has 3 checks: 755 * if the file exists && 756 * it is being open with write access && 757 * the file system is read only 758 * then generate EROFS 759 */ 760 if ((*vpp != NULL && (mode & VWRITE) && ISROFILE(*vpp)) || 761 (*vpp == NULL && dvp->v_vfsp->vfs_flag & VFS_RDONLY)) { 762 if (*vpp) 763 VN_RELE(*vpp); 764 error = EROFS; 765 } else if (excl == NONEXCL && *vpp != NULL) { 766 vnode_t *rvp; 767 768 /* 769 * File already exists. If a mandatory lock has been 770 * applied, return error. 771 */ 772 vp = *vpp; 773 if (VOP_REALVP(vp, &rvp) != 0) 774 rvp = vp; 775 if ((vap->va_mask & AT_SIZE) && nbl_need_check(vp)) { 776 nbl_start_crit(vp, RW_READER); 777 in_crit = 1; 778 } 779 if (rvp->v_filocks != NULL || rvp->v_shrlocks != NULL) { 780 vattr.va_mask = AT_MODE|AT_SIZE; 781 if (error = VOP_GETATTR(vp, &vattr, 0, CRED())) { 782 goto out; 783 } 784 if (MANDLOCK(vp, vattr.va_mode)) { 785 error = EAGAIN; 786 goto out; 787 } 788 /* 789 * File cannot be truncated if non-blocking mandatory 790 * locks are currently on the file. 791 */ 792 if ((vap->va_mask & AT_SIZE) && in_crit) { 793 u_offset_t offset; 794 ssize_t length; 795 796 offset = vap->va_size > vattr.va_size ? 797 vattr.va_size : vap->va_size; 798 length = vap->va_size > vattr.va_size ? 799 vap->va_size - vattr.va_size : 800 vattr.va_size - vap->va_size; 801 if (nbl_conflict(vp, NBL_WRITE, offset, 802 length, 0)) { 803 error = EACCES; 804 goto out; 805 } 806 } 807 } 808 809 /* 810 * If the file is the root of a VFS, we've crossed a 811 * mount point and the "containing" directory that we 812 * acquired above (dvp) is irrelevant because it's in 813 * a different file system. We apply VOP_CREATE to the 814 * target itself instead of to the containing directory 815 * and supply a null path name to indicate (conventionally) 816 * the node itself as the "component" of interest. 817 * 818 * The intercession of the file system is necessary to 819 * ensure that the appropriate permission checks are 820 * done. 821 */ 822 if (vp->v_flag & VROOT) { 823 ASSERT(why != CRMKDIR); 824 error = 825 VOP_CREATE(vp, "", vap, excl, mode, vpp, CRED(), 826 flag); 827 /* 828 * If the create succeeded, it will have created 829 * a new reference to the vnode. Give up the 830 * original reference. The assertion should not 831 * get triggered because NBMAND locks only apply to 832 * VREG files. And if in_crit is non-zero for some 833 * reason, detect that here, rather than when we 834 * deference a null vp. 835 */ 836 ASSERT(in_crit == 0); 837 VN_RELE(vp); 838 vp = NULL; 839 goto out; 840 } 841 842 /* 843 * Large File API - non-large open (FOFFMAX flag not set) 844 * of regular file fails if the file size exceeds MAXOFF32_T. 845 */ 846 if (why != CRMKDIR && 847 !(flag & FOFFMAX) && 848 (vp->v_type == VREG)) { 849 vattr.va_mask = AT_SIZE; 850 if ((error = VOP_GETATTR(vp, &vattr, 0, CRED()))) { 851 goto out; 852 } 853 if ((vattr.va_size > (u_offset_t)MAXOFF32_T)) { 854 error = EOVERFLOW; 855 goto out; 856 } 857 } 858 } 859 860 if (error == 0) { 861 /* 862 * Call mkdir() if specified, otherwise create(). 863 */ 864 int must_be_dir = pn_fixslash(&pn); /* trailing '/'? */ 865 866 if (why == CRMKDIR) 867 error = VOP_MKDIR(dvp, pn.pn_path, vap, vpp, CRED()); 868 else if (!must_be_dir) 869 error = VOP_CREATE(dvp, pn.pn_path, vap, 870 excl, mode, vpp, CRED(), flag); 871 else 872 error = ENOTDIR; 873 } 874 875 out: 876 877 #ifdef C2_AUDIT 878 if (audit_active) 879 audit_vncreate_finish(*vpp, error); 880 #endif /* C2_AUDIT */ 881 if (in_crit) { 882 nbl_end_crit(vp); 883 in_crit = 0; 884 } 885 if (vp != NULL) { 886 VN_RELE(vp); 887 vp = NULL; 888 } 889 pn_free(&pn); 890 VN_RELE(dvp); 891 /* 892 * The following clause was added to handle a problem 893 * with NFS consistency. It is possible that a lookup 894 * of the file to be created succeeded, but the file 895 * itself doesn't actually exist on the server. This 896 * is chiefly due to the DNLC containing an entry for 897 * the file which has been removed on the server. In 898 * this case, we just start over. If there was some 899 * other cause for the ESTALE error, then the lookup 900 * of the file will fail and the error will be returned 901 * above instead of looping around from here. 902 */ 903 if (error == ESTALE) 904 goto top; 905 return (error); 906 } 907 908 int 909 vn_link(char *from, char *to, enum uio_seg seg) 910 { 911 struct vnode *fvp; /* from vnode ptr */ 912 struct vnode *tdvp; /* to directory vnode ptr */ 913 struct pathname pn; 914 int error; 915 struct vattr vattr; 916 dev_t fsid; 917 918 top: 919 fvp = tdvp = NULL; 920 if (error = pn_get(to, seg, &pn)) 921 return (error); 922 if (error = lookupname(from, seg, NO_FOLLOW, NULLVPP, &fvp)) 923 goto out; 924 if (error = lookuppn(&pn, NULL, NO_FOLLOW, &tdvp, NULLVPP)) 925 goto out; 926 /* 927 * Make sure both source vnode and target directory vnode are 928 * in the same vfs and that it is writeable. 929 */ 930 vattr.va_mask = AT_FSID; 931 if (error = VOP_GETATTR(fvp, &vattr, 0, CRED())) 932 goto out; 933 fsid = vattr.va_fsid; 934 vattr.va_mask = AT_FSID; 935 if (error = VOP_GETATTR(tdvp, &vattr, 0, CRED())) 936 goto out; 937 if (fsid != vattr.va_fsid) { 938 error = EXDEV; 939 goto out; 940 } 941 if (tdvp->v_vfsp->vfs_flag & VFS_RDONLY) { 942 error = EROFS; 943 goto out; 944 } 945 /* 946 * Do the link. 947 */ 948 (void) pn_fixslash(&pn); 949 error = VOP_LINK(tdvp, fvp, pn.pn_path, CRED()); 950 out: 951 pn_free(&pn); 952 if (fvp) 953 VN_RELE(fvp); 954 if (tdvp) 955 VN_RELE(tdvp); 956 if (error == ESTALE) 957 goto top; 958 return (error); 959 } 960 961 int 962 vn_rename(char *from, char *to, enum uio_seg seg) 963 { 964 return (vn_renameat(NULL, from, NULL, to, seg)); 965 } 966 967 int 968 vn_renameat(vnode_t *fdvp, char *fname, vnode_t *tdvp, 969 char *tname, enum uio_seg seg) 970 { 971 int error; 972 struct vattr vattr; 973 struct pathname fpn; /* from pathname */ 974 struct pathname tpn; /* to pathname */ 975 dev_t fsid; 976 int in_crit = 0; 977 vnode_t *fromvp, *fvp; 978 vnode_t *tovp; 979 980 top: 981 fvp = fromvp = tovp = NULL; 982 /* 983 * Get to and from pathnames. 984 */ 985 if (error = pn_get(fname, seg, &fpn)) 986 return (error); 987 if (error = pn_get(tname, seg, &tpn)) { 988 pn_free(&fpn); 989 return (error); 990 } 991 992 /* 993 * First we need to resolve the correct directories 994 * The passed in directories may only be a starting point, 995 * but we need the real directories the file(s) live in. 996 * For example the fname may be something like usr/lib/sparc 997 * and we were passed in the / directory, but we need to 998 * use the lib directory for the rename. 999 */ 1000 1001 #ifdef C2_AUDIT 1002 if (audit_active) 1003 audit_setfsat_path(1); 1004 #endif /* C2_AUDIT */ 1005 /* 1006 * Lookup to and from directories. 1007 */ 1008 if (error = lookuppnat(&fpn, NULL, NO_FOLLOW, &fromvp, &fvp, fdvp)) { 1009 goto out; 1010 } 1011 1012 /* 1013 * Make sure there is an entry. 1014 */ 1015 if (fvp == NULL) { 1016 error = ENOENT; 1017 goto out; 1018 } 1019 1020 #ifdef C2_AUDIT 1021 if (audit_active) 1022 audit_setfsat_path(3); 1023 #endif /* C2_AUDIT */ 1024 if (error = lookuppnat(&tpn, NULL, NO_FOLLOW, &tovp, NULLVPP, tdvp)) { 1025 goto out; 1026 } 1027 1028 /* 1029 * Make sure both the from vnode directory and the to directory 1030 * are in the same vfs and the to directory is writable. 1031 * We check fsid's, not vfs pointers, so loopback fs works. 1032 */ 1033 if (fromvp != tovp) { 1034 vattr.va_mask = AT_FSID; 1035 if (error = VOP_GETATTR(fromvp, &vattr, 0, CRED())) 1036 goto out; 1037 fsid = vattr.va_fsid; 1038 vattr.va_mask = AT_FSID; 1039 if (error = VOP_GETATTR(tovp, &vattr, 0, CRED())) 1040 goto out; 1041 if (fsid != vattr.va_fsid) { 1042 error = EXDEV; 1043 goto out; 1044 } 1045 } 1046 1047 if (tovp->v_vfsp->vfs_flag & VFS_RDONLY) { 1048 error = EROFS; 1049 goto out; 1050 } 1051 1052 if (nbl_need_check(fvp)) { 1053 nbl_start_crit(fvp, RW_READER); 1054 in_crit = 1; 1055 if (nbl_conflict(fvp, NBL_RENAME, 0, 0, 0)) { 1056 error = EACCES; 1057 goto out; 1058 } 1059 } 1060 1061 /* 1062 * Do the rename. 1063 */ 1064 (void) pn_fixslash(&tpn); 1065 error = VOP_RENAME(fromvp, fpn.pn_path, tovp, tpn.pn_path, CRED()); 1066 1067 out: 1068 pn_free(&fpn); 1069 pn_free(&tpn); 1070 if (in_crit) { 1071 nbl_end_crit(fvp); 1072 in_crit = 0; 1073 } 1074 if (fromvp) 1075 VN_RELE(fromvp); 1076 if (tovp) 1077 VN_RELE(tovp); 1078 if (fvp) 1079 VN_RELE(fvp); 1080 if (error == ESTALE) 1081 goto top; 1082 return (error); 1083 } 1084 1085 /* 1086 * Remove a file or directory. 1087 */ 1088 int 1089 vn_remove(char *fnamep, enum uio_seg seg, enum rm dirflag) 1090 { 1091 return (vn_removeat(NULL, fnamep, seg, dirflag)); 1092 } 1093 1094 int 1095 vn_removeat(vnode_t *startvp, char *fnamep, enum uio_seg seg, enum rm dirflag) 1096 { 1097 struct vnode *vp; /* entry vnode */ 1098 struct vnode *dvp; /* ptr to parent dir vnode */ 1099 struct vnode *coveredvp; 1100 struct pathname pn; /* name of entry */ 1101 enum vtype vtype; 1102 int error; 1103 struct vfs *vfsp; 1104 struct vfs *dvfsp; /* ptr to parent dir vfs */ 1105 int in_crit = 0; 1106 1107 top: 1108 if (error = pn_get(fnamep, seg, &pn)) 1109 return (error); 1110 dvp = vp = NULL; 1111 if (error = lookuppnat(&pn, NULL, NO_FOLLOW, &dvp, &vp, startvp)) { 1112 pn_free(&pn); 1113 if (error == ESTALE) 1114 goto top; 1115 return (error); 1116 } 1117 1118 /* 1119 * Make sure there is an entry. 1120 */ 1121 if (vp == NULL) { 1122 error = ENOENT; 1123 goto out; 1124 } 1125 1126 vfsp = vp->v_vfsp; 1127 dvfsp = dvp->v_vfsp; 1128 1129 /* 1130 * If the named file is the root of a mounted filesystem, fail, 1131 * unless it's marked unlinkable. In that case, unmount the 1132 * filesystem and proceed to unlink the covered vnode. (If the 1133 * covered vnode is a directory, use rmdir instead of unlink, 1134 * to avoid file system corruption.) 1135 */ 1136 if (vp->v_flag & VROOT) { 1137 if (vfsp->vfs_flag & VFS_UNLINKABLE) { 1138 if (dirflag == RMDIRECTORY) { 1139 /* 1140 * User called rmdir(2) on a file that has 1141 * been namefs mounted on top of. Since 1142 * namefs doesn't allow directories to 1143 * be mounted on other files we know 1144 * vp is not of type VDIR so fail to operation. 1145 */ 1146 error = ENOTDIR; 1147 goto out; 1148 } 1149 coveredvp = vfsp->vfs_vnodecovered; 1150 VN_HOLD(coveredvp); 1151 VN_RELE(vp); 1152 vp = NULL; 1153 if ((error = vn_vfswlock(coveredvp)) == 0) 1154 error = dounmount(vfsp, 0, CRED()); 1155 /* 1156 * Unmounted the namefs file system; now get 1157 * the object it was mounted over. 1158 */ 1159 vp = coveredvp; 1160 /* 1161 * If namefs was mounted over a directory, then 1162 * we want to use rmdir() instead of unlink(). 1163 */ 1164 if (vp->v_type == VDIR) 1165 dirflag = RMDIRECTORY; 1166 } else 1167 error = EBUSY; 1168 1169 if (error) 1170 goto out; 1171 } 1172 1173 /* 1174 * Make sure filesystem is writeable. 1175 * We check the parent directory's vfs in case this is an lofs vnode. 1176 */ 1177 if (dvfsp && dvfsp->vfs_flag & VFS_RDONLY) { 1178 error = EROFS; 1179 goto out; 1180 } 1181 1182 vtype = vp->v_type; 1183 1184 /* 1185 * If there is the possibility of an nbmand share reservation, make 1186 * sure it's okay to remove the file. Keep a reference to the 1187 * vnode, so that we can exit the nbl critical region after 1188 * calling VOP_REMOVE. 1189 * If there is no possibility of an nbmand share reservation, 1190 * release the vnode reference now. Filesystems like NFS may 1191 * behave differently if there is an extra reference, so get rid of 1192 * this one. Fortunately, we can't have nbmand mounts on NFS 1193 * filesystems. 1194 */ 1195 if (nbl_need_check(vp)) { 1196 nbl_start_crit(vp, RW_READER); 1197 in_crit = 1; 1198 if (nbl_conflict(vp, NBL_REMOVE, 0, 0, 0)) { 1199 error = EACCES; 1200 goto out; 1201 } 1202 } else { 1203 VN_RELE(vp); 1204 vp = NULL; 1205 } 1206 1207 if (dirflag == RMDIRECTORY) { 1208 /* 1209 * Caller is using rmdir(2), which can only be applied to 1210 * directories. 1211 */ 1212 if (vtype != VDIR) { 1213 error = ENOTDIR; 1214 } else { 1215 vnode_t *cwd; 1216 proc_t *pp = curproc; 1217 1218 mutex_enter(&pp->p_lock); 1219 cwd = PTOU(pp)->u_cdir; 1220 VN_HOLD(cwd); 1221 mutex_exit(&pp->p_lock); 1222 error = VOP_RMDIR(dvp, pn.pn_path, cwd, CRED()); 1223 VN_RELE(cwd); 1224 } 1225 } else { 1226 /* 1227 * Unlink(2) can be applied to anything. 1228 */ 1229 error = VOP_REMOVE(dvp, pn.pn_path, CRED()); 1230 } 1231 1232 out: 1233 pn_free(&pn); 1234 if (in_crit) { 1235 nbl_end_crit(vp); 1236 in_crit = 0; 1237 } 1238 if (vp != NULL) 1239 VN_RELE(vp); 1240 if (dvp != NULL) 1241 VN_RELE(dvp); 1242 if (error == ESTALE) 1243 goto top; 1244 return (error); 1245 } 1246 1247 /* 1248 * Utility function to compare equality of vnodes. 1249 * Compare the underlying real vnodes, if there are underlying vnodes. 1250 * This is a more thorough comparison than the VN_CMP() macro provides. 1251 */ 1252 int 1253 vn_compare(vnode_t *vp1, vnode_t *vp2) 1254 { 1255 vnode_t *realvp; 1256 1257 if (vp1 != NULL && VOP_REALVP(vp1, &realvp) == 0) 1258 vp1 = realvp; 1259 if (vp2 != NULL && VOP_REALVP(vp2, &realvp) == 0) 1260 vp2 = realvp; 1261 return (VN_CMP(vp1, vp2)); 1262 } 1263 1264 /* 1265 * The number of locks to hash into. This value must be a power 1266 * of 2 minus 1 and should probably also be prime. 1267 */ 1268 #define NUM_BUCKETS 1023 1269 1270 struct vn_vfslocks_bucket { 1271 kmutex_t vb_lock; 1272 vn_vfslocks_entry_t *vb_list; 1273 char pad[64 - sizeof (kmutex_t) - sizeof (void *)]; 1274 }; 1275 1276 /* 1277 * Total number of buckets will be NUM_BUCKETS + 1 . 1278 */ 1279 1280 #pragma align 64(vn_vfslocks_buckets) 1281 static struct vn_vfslocks_bucket vn_vfslocks_buckets[NUM_BUCKETS + 1]; 1282 1283 #define VN_VFSLOCKS_SHIFT 9 1284 1285 #define VN_VFSLOCKS_HASH(vfsvpptr) \ 1286 ((((intptr_t)(vfsvpptr)) >> VN_VFSLOCKS_SHIFT) & NUM_BUCKETS) 1287 1288 /* 1289 * vn_vfslocks_getlock() uses an HASH scheme to generate 1290 * rwstlock using vfs/vnode pointer passed to it. 1291 * 1292 * vn_vfslocks_rele() releases a reference in the 1293 * HASH table which allows the entry allocated by 1294 * vn_vfslocks_getlock() to be freed at a later 1295 * stage when the refcount drops to zero. 1296 */ 1297 1298 vn_vfslocks_entry_t * 1299 vn_vfslocks_getlock(void *vfsvpptr) 1300 { 1301 struct vn_vfslocks_bucket *bp; 1302 vn_vfslocks_entry_t *vep; 1303 vn_vfslocks_entry_t *tvep; 1304 1305 ASSERT(vfsvpptr != NULL); 1306 bp = &vn_vfslocks_buckets[VN_VFSLOCKS_HASH(vfsvpptr)]; 1307 1308 mutex_enter(&bp->vb_lock); 1309 for (vep = bp->vb_list; vep != NULL; vep = vep->ve_next) { 1310 if (vep->ve_vpvfs == vfsvpptr) { 1311 vep->ve_refcnt++; 1312 mutex_exit(&bp->vb_lock); 1313 return (vep); 1314 } 1315 } 1316 mutex_exit(&bp->vb_lock); 1317 vep = kmem_alloc(sizeof (*vep), KM_SLEEP); 1318 rwst_init(&vep->ve_lock, NULL, RW_DEFAULT, NULL); 1319 vep->ve_vpvfs = (char *)vfsvpptr; 1320 vep->ve_refcnt = 1; 1321 mutex_enter(&bp->vb_lock); 1322 for (tvep = bp->vb_list; tvep != NULL; tvep = tvep->ve_next) { 1323 if (tvep->ve_vpvfs == vfsvpptr) { 1324 tvep->ve_refcnt++; 1325 mutex_exit(&bp->vb_lock); 1326 1327 /* 1328 * There is already an entry in the hash 1329 * destroy what we just allocated. 1330 */ 1331 rwst_destroy(&vep->ve_lock); 1332 kmem_free(vep, sizeof (*vep)); 1333 return (tvep); 1334 } 1335 } 1336 vep->ve_next = bp->vb_list; 1337 bp->vb_list = vep; 1338 mutex_exit(&bp->vb_lock); 1339 return (vep); 1340 } 1341 1342 void 1343 vn_vfslocks_rele(vn_vfslocks_entry_t *vepent) 1344 { 1345 struct vn_vfslocks_bucket *bp; 1346 vn_vfslocks_entry_t *vep; 1347 vn_vfslocks_entry_t *pvep; 1348 1349 ASSERT(vepent != NULL); 1350 ASSERT(vepent->ve_vpvfs != NULL); 1351 1352 bp = &vn_vfslocks_buckets[VN_VFSLOCKS_HASH(vepent->ve_vpvfs)]; 1353 1354 mutex_enter(&bp->vb_lock); 1355 vepent->ve_refcnt--; 1356 1357 if ((int32_t)vepent->ve_refcnt < 0) 1358 cmn_err(CE_PANIC, "vn_vfslocks_rele: refcount negative"); 1359 1360 if (vepent->ve_refcnt == 0) { 1361 for (vep = bp->vb_list; vep != NULL; vep = vep->ve_next) { 1362 if (vep->ve_vpvfs == vepent->ve_vpvfs) { 1363 if (bp->vb_list == vep) 1364 bp->vb_list = vep->ve_next; 1365 else { 1366 /* LINTED */ 1367 pvep->ve_next = vep->ve_next; 1368 } 1369 mutex_exit(&bp->vb_lock); 1370 rwst_destroy(&vep->ve_lock); 1371 kmem_free(vep, sizeof (*vep)); 1372 return; 1373 } 1374 pvep = vep; 1375 } 1376 cmn_err(CE_PANIC, "vn_vfslocks_rele: vp/vfs not found"); 1377 } 1378 mutex_exit(&bp->vb_lock); 1379 } 1380 1381 /* 1382 * vn_vfswlock_wait is used to implement a lock which is logically a writers 1383 * lock protecting the v_vfsmountedhere field. 1384 * vn_vfswlock_wait has been modified to be similar to vn_vfswlock, 1385 * except that it blocks to acquire the lock VVFSLOCK. 1386 * 1387 * traverse() and routines re-implementing part of traverse (e.g. autofs) 1388 * need to hold this lock. mount(), vn_rename(), vn_remove() and so on 1389 * need the non-blocking version of the writers lock i.e. vn_vfswlock 1390 */ 1391 int 1392 vn_vfswlock_wait(vnode_t *vp) 1393 { 1394 int retval; 1395 vn_vfslocks_entry_t *vpvfsentry; 1396 ASSERT(vp != NULL); 1397 1398 vpvfsentry = vn_vfslocks_getlock(vp); 1399 retval = rwst_enter_sig(&vpvfsentry->ve_lock, RW_WRITER); 1400 1401 if (retval == EINTR) { 1402 vn_vfslocks_rele(vpvfsentry); 1403 return (EINTR); 1404 } 1405 return (retval); 1406 } 1407 1408 int 1409 vn_vfsrlock_wait(vnode_t *vp) 1410 { 1411 int retval; 1412 vn_vfslocks_entry_t *vpvfsentry; 1413 ASSERT(vp != NULL); 1414 1415 vpvfsentry = vn_vfslocks_getlock(vp); 1416 retval = rwst_enter_sig(&vpvfsentry->ve_lock, RW_READER); 1417 1418 if (retval == EINTR) { 1419 vn_vfslocks_rele(vpvfsentry); 1420 return (EINTR); 1421 } 1422 1423 return (retval); 1424 } 1425 1426 1427 /* 1428 * vn_vfswlock is used to implement a lock which is logically a writers lock 1429 * protecting the v_vfsmountedhere field. 1430 */ 1431 int 1432 vn_vfswlock(vnode_t *vp) 1433 { 1434 vn_vfslocks_entry_t *vpvfsentry; 1435 1436 /* 1437 * If vp is NULL then somebody is trying to lock the covered vnode 1438 * of /. (vfs_vnodecovered is NULL for /). This situation will 1439 * only happen when unmounting /. Since that operation will fail 1440 * anyway, return EBUSY here instead of in VFS_UNMOUNT. 1441 */ 1442 if (vp == NULL) 1443 return (EBUSY); 1444 1445 vpvfsentry = vn_vfslocks_getlock(vp); 1446 1447 if (rwst_tryenter(&vpvfsentry->ve_lock, RW_WRITER)) 1448 return (0); 1449 1450 vn_vfslocks_rele(vpvfsentry); 1451 return (EBUSY); 1452 } 1453 1454 int 1455 vn_vfsrlock(vnode_t *vp) 1456 { 1457 vn_vfslocks_entry_t *vpvfsentry; 1458 1459 /* 1460 * If vp is NULL then somebody is trying to lock the covered vnode 1461 * of /. (vfs_vnodecovered is NULL for /). This situation will 1462 * only happen when unmounting /. Since that operation will fail 1463 * anyway, return EBUSY here instead of in VFS_UNMOUNT. 1464 */ 1465 if (vp == NULL) 1466 return (EBUSY); 1467 1468 vpvfsentry = vn_vfslocks_getlock(vp); 1469 1470 if (rwst_tryenter(&vpvfsentry->ve_lock, RW_READER)) 1471 return (0); 1472 1473 vn_vfslocks_rele(vpvfsentry); 1474 return (EBUSY); 1475 } 1476 1477 void 1478 vn_vfsunlock(vnode_t *vp) 1479 { 1480 vn_vfslocks_entry_t *vpvfsentry; 1481 1482 /* 1483 * ve_refcnt needs to be decremented twice. 1484 * 1. To release refernce after a call to vn_vfslocks_getlock() 1485 * 2. To release the reference from the locking routines like 1486 * vn_vfsrlock/vn_vfswlock etc,. 1487 */ 1488 vpvfsentry = vn_vfslocks_getlock(vp); 1489 vn_vfslocks_rele(vpvfsentry); 1490 1491 rwst_exit(&vpvfsentry->ve_lock); 1492 vn_vfslocks_rele(vpvfsentry); 1493 } 1494 1495 int 1496 vn_vfswlock_held(vnode_t *vp) 1497 { 1498 int held; 1499 vn_vfslocks_entry_t *vpvfsentry; 1500 1501 ASSERT(vp != NULL); 1502 1503 vpvfsentry = vn_vfslocks_getlock(vp); 1504 held = rwst_lock_held(&vpvfsentry->ve_lock, RW_WRITER); 1505 1506 vn_vfslocks_rele(vpvfsentry); 1507 return (held); 1508 } 1509 1510 1511 int 1512 vn_make_ops( 1513 const char *name, /* Name of file system */ 1514 const fs_operation_def_t *templ, /* Operation specification */ 1515 vnodeops_t **actual) /* Return the vnodeops */ 1516 { 1517 int unused_ops; 1518 int error; 1519 1520 *actual = (vnodeops_t *)kmem_alloc(sizeof (vnodeops_t), KM_SLEEP); 1521 1522 (*actual)->vnop_name = name; 1523 1524 error = fs_build_vector(*actual, &unused_ops, vn_ops_table, templ); 1525 if (error) { 1526 kmem_free(*actual, sizeof (vnodeops_t)); 1527 } 1528 1529 #if DEBUG 1530 if (unused_ops != 0) 1531 cmn_err(CE_WARN, "vn_make_ops: %s: %d operations supplied " 1532 "but not used", name, unused_ops); 1533 #endif 1534 1535 return (error); 1536 } 1537 1538 /* 1539 * Free the vnodeops created as a result of vn_make_ops() 1540 */ 1541 void 1542 vn_freevnodeops(vnodeops_t *vnops) 1543 { 1544 kmem_free(vnops, sizeof (vnodeops_t)); 1545 } 1546 1547 /* 1548 * Vnode cache. 1549 */ 1550 1551 /* ARGSUSED */ 1552 static int 1553 vn_cache_constructor(void *buf, void *cdrarg, int kmflags) 1554 { 1555 struct vnode *vp; 1556 1557 vp = buf; 1558 1559 mutex_init(&vp->v_lock, NULL, MUTEX_DEFAULT, NULL); 1560 cv_init(&vp->v_cv, NULL, CV_DEFAULT, NULL); 1561 rw_init(&vp->v_nbllock, NULL, RW_DEFAULT, NULL); 1562 rw_init(&vp->v_mslock, NULL, RW_DEFAULT, NULL); 1563 1564 vp->v_femhead = NULL; /* Must be done before vn_reinit() */ 1565 vp->v_path = NULL; 1566 vp->v_mpssdata = NULL; 1567 1568 return (0); 1569 } 1570 1571 /* ARGSUSED */ 1572 static void 1573 vn_cache_destructor(void *buf, void *cdrarg) 1574 { 1575 struct vnode *vp; 1576 1577 vp = buf; 1578 1579 rw_destroy(&vp->v_mslock); 1580 rw_destroy(&vp->v_nbllock); 1581 cv_destroy(&vp->v_cv); 1582 mutex_destroy(&vp->v_lock); 1583 } 1584 1585 void 1586 vn_create_cache(void) 1587 { 1588 vn_cache = kmem_cache_create("vn_cache", sizeof (struct vnode), 64, 1589 vn_cache_constructor, vn_cache_destructor, NULL, NULL, 1590 NULL, 0); 1591 } 1592 1593 void 1594 vn_destroy_cache(void) 1595 { 1596 kmem_cache_destroy(vn_cache); 1597 } 1598 1599 /* 1600 * Used by file systems when fs-specific nodes (e.g., ufs inodes) are 1601 * cached by the file system and vnodes remain associated. 1602 */ 1603 void 1604 vn_recycle(vnode_t *vp) 1605 { 1606 ASSERT(vp->v_pages == NULL); 1607 1608 /* 1609 * XXX - This really belongs in vn_reinit(), but we have some issues 1610 * with the counts. Best to have it here for clean initialization. 1611 */ 1612 vp->v_rdcnt = 0; 1613 vp->v_wrcnt = 0; 1614 vp->v_mmap_read = 0; 1615 vp->v_mmap_write = 0; 1616 1617 /* 1618 * If FEM was in use, make sure everything gets cleaned up 1619 * NOTE: vp->v_femhead is initialized to NULL in the vnode 1620 * constructor. 1621 */ 1622 if (vp->v_femhead) { 1623 /* XXX - There should be a free_femhead() that does all this */ 1624 ASSERT(vp->v_femhead->femh_list == NULL); 1625 mutex_destroy(&vp->v_femhead->femh_lock); 1626 kmem_free(vp->v_femhead, sizeof (*(vp->v_femhead))); 1627 vp->v_femhead = NULL; 1628 } 1629 if (vp->v_path) { 1630 kmem_free(vp->v_path, strlen(vp->v_path) + 1); 1631 vp->v_path = NULL; 1632 } 1633 vp->v_mpssdata = NULL; 1634 } 1635 1636 /* 1637 * Used to reset the vnode fields including those that are directly accessible 1638 * as well as those which require an accessor function. 1639 * 1640 * Does not initialize: 1641 * synchronization objects: v_lock, v_nbllock, v_cv 1642 * v_data (since FS-nodes and vnodes point to each other and should 1643 * be updated simultaneously) 1644 * v_op (in case someone needs to make a VOP call on this object) 1645 */ 1646 void 1647 vn_reinit(vnode_t *vp) 1648 { 1649 vp->v_count = 1; 1650 vp->v_vfsp = NULL; 1651 vp->v_stream = NULL; 1652 vp->v_vfsmountedhere = NULL; 1653 vp->v_flag = 0; 1654 vp->v_type = VNON; 1655 vp->v_rdev = NODEV; 1656 1657 vp->v_filocks = NULL; 1658 vp->v_shrlocks = NULL; 1659 vp->v_pages = NULL; 1660 vp->v_npages = 0; 1661 vp->v_msnpages = 0; 1662 vp->v_scanfront = NULL; 1663 vp->v_scanback = NULL; 1664 1665 vp->v_locality = NULL; 1666 vp->v_scantime = 0; 1667 vp->v_mset = 0; 1668 vp->v_msflags = 0; 1669 vp->v_msnext = NULL; 1670 vp->v_msprev = NULL; 1671 1672 /* Handles v_femhead, v_path, and the r/w/map counts */ 1673 vn_recycle(vp); 1674 } 1675 1676 vnode_t * 1677 vn_alloc(int kmflag) 1678 { 1679 vnode_t *vp; 1680 1681 vp = kmem_cache_alloc(vn_cache, kmflag); 1682 1683 if (vp != NULL) { 1684 vp->v_femhead = NULL; /* Must be done before vn_reinit() */ 1685 vn_reinit(vp); 1686 } 1687 1688 return (vp); 1689 } 1690 1691 void 1692 vn_free(vnode_t *vp) 1693 { 1694 /* 1695 * Some file systems call vn_free() with v_count of zero, 1696 * some with v_count of 1. In any case, the value should 1697 * never be anything else. 1698 */ 1699 ASSERT((vp->v_count == 0) || (vp->v_count == 1)); 1700 if (vp->v_path != NULL) { 1701 kmem_free(vp->v_path, strlen(vp->v_path) + 1); 1702 vp->v_path = NULL; 1703 } 1704 1705 /* If FEM was in use, make sure everything gets cleaned up */ 1706 if (vp->v_femhead) { 1707 /* XXX - There should be a free_femhead() that does all this */ 1708 ASSERT(vp->v_femhead->femh_list == NULL); 1709 mutex_destroy(&vp->v_femhead->femh_lock); 1710 kmem_free(vp->v_femhead, sizeof (*(vp->v_femhead))); 1711 vp->v_femhead = NULL; 1712 } 1713 vp->v_mpssdata = NULL; 1714 kmem_cache_free(vn_cache, vp); 1715 } 1716 1717 /* 1718 * vnode status changes, should define better states than 1, 0. 1719 */ 1720 void 1721 vn_reclaim(vnode_t *vp) 1722 { 1723 vfs_t *vfsp = vp->v_vfsp; 1724 1725 if (vfsp == NULL || vfsp->vfs_femhead == NULL) { 1726 return; 1727 } 1728 (void) VFS_VNSTATE(vfsp, vp, VNTRANS_RECLAIMED); 1729 } 1730 1731 void 1732 vn_idle(vnode_t *vp) 1733 { 1734 vfs_t *vfsp = vp->v_vfsp; 1735 1736 if (vfsp == NULL || vfsp->vfs_femhead == NULL) { 1737 return; 1738 } 1739 (void) VFS_VNSTATE(vfsp, vp, VNTRANS_IDLED); 1740 } 1741 void 1742 vn_exists(vnode_t *vp) 1743 { 1744 vfs_t *vfsp = vp->v_vfsp; 1745 1746 if (vfsp == NULL || vfsp->vfs_femhead == NULL) { 1747 return; 1748 } 1749 (void) VFS_VNSTATE(vfsp, vp, VNTRANS_EXISTS); 1750 } 1751 1752 void 1753 vn_invalid(vnode_t *vp) 1754 { 1755 vfs_t *vfsp = vp->v_vfsp; 1756 1757 if (vfsp == NULL || vfsp->vfs_femhead == NULL) { 1758 return; 1759 } 1760 (void) VFS_VNSTATE(vfsp, vp, VNTRANS_DESTROYED); 1761 } 1762 1763 /* Vnode event notification */ 1764 1765 int 1766 vnevent_support(vnode_t *vp) 1767 { 1768 if (vp == NULL) 1769 return (EINVAL); 1770 1771 return (VOP_VNEVENT(vp, VE_SUPPORT)); 1772 } 1773 1774 void 1775 vnevent_rename_src(vnode_t *vp) 1776 { 1777 if (vp == NULL || vp->v_femhead == NULL) { 1778 return; 1779 } 1780 (void) VOP_VNEVENT(vp, VE_RENAME_SRC); 1781 } 1782 1783 void 1784 vnevent_rename_dest(vnode_t *vp) 1785 { 1786 if (vp == NULL || vp->v_femhead == NULL) { 1787 return; 1788 } 1789 (void) VOP_VNEVENT(vp, VE_RENAME_DEST); 1790 } 1791 1792 void 1793 vnevent_remove(vnode_t *vp) 1794 { 1795 if (vp == NULL || vp->v_femhead == NULL) { 1796 return; 1797 } 1798 (void) VOP_VNEVENT(vp, VE_REMOVE); 1799 } 1800 1801 void 1802 vnevent_rmdir(vnode_t *vp) 1803 { 1804 if (vp == NULL || vp->v_femhead == NULL) { 1805 return; 1806 } 1807 (void) VOP_VNEVENT(vp, VE_RMDIR); 1808 } 1809 1810 /* 1811 * Vnode accessors. 1812 */ 1813 1814 int 1815 vn_is_readonly(vnode_t *vp) 1816 { 1817 return (vp->v_vfsp->vfs_flag & VFS_RDONLY); 1818 } 1819 1820 int 1821 vn_has_flocks(vnode_t *vp) 1822 { 1823 return (vp->v_filocks != NULL); 1824 } 1825 1826 int 1827 vn_has_mandatory_locks(vnode_t *vp, int mode) 1828 { 1829 return ((vp->v_filocks != NULL) && (MANDLOCK(vp, mode))); 1830 } 1831 1832 int 1833 vn_has_cached_data(vnode_t *vp) 1834 { 1835 return (vp->v_pages != NULL); 1836 } 1837 1838 /* 1839 * Return 0 if the vnode in question shouldn't be permitted into a zone via 1840 * zone_enter(2). 1841 */ 1842 int 1843 vn_can_change_zones(vnode_t *vp) 1844 { 1845 struct vfssw *vswp; 1846 int allow = 1; 1847 vnode_t *rvp; 1848 1849 if (nfs_global_client_only != 0) 1850 return (1); 1851 1852 /* 1853 * We always want to look at the underlying vnode if there is one. 1854 */ 1855 if (VOP_REALVP(vp, &rvp) != 0) 1856 rvp = vp; 1857 /* 1858 * Some pseudo filesystems (including doorfs) don't actually register 1859 * their vfsops_t, so the following may return NULL; we happily let 1860 * such vnodes switch zones. 1861 */ 1862 vswp = vfs_getvfsswbyvfsops(vfs_getops(rvp->v_vfsp)); 1863 if (vswp != NULL) { 1864 if (vswp->vsw_flag & VSW_NOTZONESAFE) 1865 allow = 0; 1866 vfs_unrefvfssw(vswp); 1867 } 1868 return (allow); 1869 } 1870 1871 /* 1872 * Return nonzero if the vnode is a mount point, zero if not. 1873 */ 1874 int 1875 vn_ismntpt(vnode_t *vp) 1876 { 1877 return (vp->v_vfsmountedhere != NULL); 1878 } 1879 1880 /* Retrieve the vfs (if any) mounted on this vnode */ 1881 vfs_t * 1882 vn_mountedvfs(vnode_t *vp) 1883 { 1884 return (vp->v_vfsmountedhere); 1885 } 1886 1887 /* 1888 * vn_is_opened() checks whether a particular file is opened and 1889 * whether the open is for read and/or write. 1890 * 1891 * Vnode counts are only kept on regular files (v_type=VREG). 1892 */ 1893 int 1894 vn_is_opened( 1895 vnode_t *vp, 1896 v_mode_t mode) 1897 { 1898 1899 ASSERT(vp != NULL); 1900 1901 switch (mode) { 1902 case V_WRITE: 1903 if (vp->v_wrcnt) 1904 return (V_TRUE); 1905 break; 1906 case V_RDANDWR: 1907 if (vp->v_rdcnt && vp->v_wrcnt) 1908 return (V_TRUE); 1909 break; 1910 case V_RDORWR: 1911 if (vp->v_rdcnt || vp->v_wrcnt) 1912 return (V_TRUE); 1913 break; 1914 case V_READ: 1915 if (vp->v_rdcnt) 1916 return (V_TRUE); 1917 break; 1918 } 1919 1920 return (V_FALSE); 1921 } 1922 1923 /* 1924 * vn_is_mapped() checks whether a particular file is mapped and whether 1925 * the file is mapped read and/or write. 1926 */ 1927 int 1928 vn_is_mapped( 1929 vnode_t *vp, 1930 v_mode_t mode) 1931 { 1932 1933 ASSERT(vp != NULL); 1934 1935 #if !defined(_LP64) 1936 switch (mode) { 1937 /* 1938 * The atomic_add_64_nv functions force atomicity in the 1939 * case of 32 bit architectures. Otherwise the 64 bit values 1940 * require two fetches. The value of the fields may be 1941 * (potentially) changed between the first fetch and the 1942 * second 1943 */ 1944 case V_WRITE: 1945 if (atomic_add_64_nv((&(vp->v_mmap_write)), 0)) 1946 return (V_TRUE); 1947 break; 1948 case V_RDANDWR: 1949 if ((atomic_add_64_nv((&(vp->v_mmap_read)), 0)) && 1950 (atomic_add_64_nv((&(vp->v_mmap_write)), 0))) 1951 return (V_TRUE); 1952 break; 1953 case V_RDORWR: 1954 if ((atomic_add_64_nv((&(vp->v_mmap_read)), 0)) || 1955 (atomic_add_64_nv((&(vp->v_mmap_write)), 0))) 1956 return (V_TRUE); 1957 break; 1958 case V_READ: 1959 if (atomic_add_64_nv((&(vp->v_mmap_read)), 0)) 1960 return (V_TRUE); 1961 break; 1962 } 1963 #else 1964 switch (mode) { 1965 case V_WRITE: 1966 if (vp->v_mmap_write) 1967 return (V_TRUE); 1968 break; 1969 case V_RDANDWR: 1970 if (vp->v_mmap_read && vp->v_mmap_write) 1971 return (V_TRUE); 1972 break; 1973 case V_RDORWR: 1974 if (vp->v_mmap_read || vp->v_mmap_write) 1975 return (V_TRUE); 1976 break; 1977 case V_READ: 1978 if (vp->v_mmap_read) 1979 return (V_TRUE); 1980 break; 1981 } 1982 #endif 1983 1984 return (V_FALSE); 1985 } 1986 1987 /* 1988 * Set the operations vector for a vnode. 1989 * 1990 * FEM ensures that the v_femhead pointer is filled in before the 1991 * v_op pointer is changed. This means that if the v_femhead pointer 1992 * is NULL, and the v_op field hasn't changed since before which checked 1993 * the v_femhead pointer; then our update is ok - we are not racing with 1994 * FEM. 1995 */ 1996 void 1997 vn_setops(vnode_t *vp, vnodeops_t *vnodeops) 1998 { 1999 vnodeops_t *op; 2000 2001 ASSERT(vp != NULL); 2002 ASSERT(vnodeops != NULL); 2003 2004 op = vp->v_op; 2005 membar_consumer(); 2006 /* 2007 * If vp->v_femhead == NULL, then we'll call casptr() to do the 2008 * compare-and-swap on vp->v_op. If either fails, then FEM is 2009 * in effect on the vnode and we need to have FEM deal with it. 2010 */ 2011 if (vp->v_femhead != NULL || casptr(&vp->v_op, op, vnodeops) != op) { 2012 fem_setvnops(vp, vnodeops); 2013 } 2014 } 2015 2016 /* 2017 * Retrieve the operations vector for a vnode 2018 * As with vn_setops(above); make sure we aren't racing with FEM. 2019 * FEM sets the v_op to a special, internal, vnodeops that wouldn't 2020 * make sense to the callers of this routine. 2021 */ 2022 vnodeops_t * 2023 vn_getops(vnode_t *vp) 2024 { 2025 vnodeops_t *op; 2026 2027 ASSERT(vp != NULL); 2028 2029 op = vp->v_op; 2030 membar_consumer(); 2031 if (vp->v_femhead == NULL && op == vp->v_op) { 2032 return (op); 2033 } else { 2034 return (fem_getvnops(vp)); 2035 } 2036 } 2037 2038 /* 2039 * Returns non-zero (1) if the vnodeops matches that of the vnode. 2040 * Returns zero (0) if not. 2041 */ 2042 int 2043 vn_matchops(vnode_t *vp, vnodeops_t *vnodeops) 2044 { 2045 return (vn_getops(vp) == vnodeops); 2046 } 2047 2048 /* 2049 * Returns non-zero (1) if the specified operation matches the 2050 * corresponding operation for that the vnode. 2051 * Returns zero (0) if not. 2052 */ 2053 2054 #define MATCHNAME(n1, n2) (((n1)[0] == (n2)[0]) && (strcmp((n1), (n2)) == 0)) 2055 2056 int 2057 vn_matchopval(vnode_t *vp, char *vopname, fs_generic_func_p funcp) 2058 { 2059 const fs_operation_trans_def_t *otdp; 2060 fs_generic_func_p *loc = NULL; 2061 vnodeops_t *vop = vn_getops(vp); 2062 2063 ASSERT(vopname != NULL); 2064 2065 for (otdp = vn_ops_table; otdp->name != NULL; otdp++) { 2066 if (MATCHNAME(otdp->name, vopname)) { 2067 loc = (fs_generic_func_p *)((char *)(vop) 2068 + otdp->offset); 2069 break; 2070 } 2071 } 2072 2073 return ((loc != NULL) && (*loc == funcp)); 2074 } 2075 2076 /* 2077 * fs_new_caller_id() needs to return a unique ID on a given local system. 2078 * The IDs do not need to survive across reboots. These are primarily 2079 * used so that (FEM) monitors can detect particular callers (such as 2080 * the NFS server) to a given vnode/vfs operation. 2081 */ 2082 u_longlong_t 2083 fs_new_caller_id() 2084 { 2085 static uint64_t next_caller_id = 0LL; /* First call returns 1 */ 2086 2087 return ((u_longlong_t)atomic_add_64_nv(&next_caller_id, 1)); 2088 } 2089 2090 /* 2091 * Given a starting vnode and a path, updates the path in the target vnode in 2092 * a safe manner. If the vnode already has path information embedded, then the 2093 * cached path is left untouched. 2094 */ 2095 void 2096 vn_setpath(vnode_t *rootvp, struct vnode *startvp, struct vnode *vp, 2097 const char *path, size_t plen) 2098 { 2099 char *rpath; 2100 vnode_t *base; 2101 size_t rpathlen, rpathalloc; 2102 int doslash = 1; 2103 2104 if (*path == '/') { 2105 base = rootvp; 2106 path++; 2107 plen--; 2108 } else { 2109 base = startvp; 2110 } 2111 2112 /* 2113 * We cannot grab base->v_lock while we hold vp->v_lock because of 2114 * the potential for deadlock. 2115 */ 2116 mutex_enter(&base->v_lock); 2117 if (base->v_path == NULL) { 2118 mutex_exit(&base->v_lock); 2119 return; 2120 } 2121 2122 rpathlen = strlen(base->v_path); 2123 rpathalloc = rpathlen + plen + 1; 2124 /* Avoid adding a slash if there's already one there */ 2125 if (base->v_path[rpathlen-1] == '/') 2126 doslash = 0; 2127 else 2128 rpathalloc++; 2129 2130 /* 2131 * We don't want to call kmem_alloc(KM_SLEEP) with kernel locks held, 2132 * so we must do this dance. If, by chance, something changes the path, 2133 * just give up since there is no real harm. 2134 */ 2135 mutex_exit(&base->v_lock); 2136 2137 rpath = kmem_alloc(rpathalloc, KM_SLEEP); 2138 2139 mutex_enter(&base->v_lock); 2140 if (base->v_path == NULL || strlen(base->v_path) != rpathlen) { 2141 mutex_exit(&base->v_lock); 2142 kmem_free(rpath, rpathalloc); 2143 return; 2144 } 2145 bcopy(base->v_path, rpath, rpathlen); 2146 mutex_exit(&base->v_lock); 2147 2148 if (doslash) 2149 rpath[rpathlen++] = '/'; 2150 bcopy(path, rpath + rpathlen, plen); 2151 rpath[rpathlen + plen] = '\0'; 2152 2153 mutex_enter(&vp->v_lock); 2154 if (vp->v_path != NULL) { 2155 mutex_exit(&vp->v_lock); 2156 kmem_free(rpath, rpathalloc); 2157 } else { 2158 vp->v_path = rpath; 2159 mutex_exit(&vp->v_lock); 2160 } 2161 } 2162 2163 /* 2164 * Sets the path to the vnode to be the given string, regardless of current 2165 * context. The string must be a complete path from rootdir. This is only used 2166 * by fsop_root() for setting the path based on the mountpoint. 2167 */ 2168 void 2169 vn_setpath_str(struct vnode *vp, const char *str, size_t len) 2170 { 2171 char *buf = kmem_alloc(len + 1, KM_SLEEP); 2172 2173 mutex_enter(&vp->v_lock); 2174 if (vp->v_path != NULL) { 2175 mutex_exit(&vp->v_lock); 2176 kmem_free(buf, len + 1); 2177 return; 2178 } 2179 2180 vp->v_path = buf; 2181 bcopy(str, vp->v_path, len); 2182 vp->v_path[len] = '\0'; 2183 2184 mutex_exit(&vp->v_lock); 2185 } 2186 2187 /* 2188 * Similar to vn_setpath_str(), this function sets the path of the destination 2189 * vnode to the be the same as the source vnode. 2190 */ 2191 void 2192 vn_copypath(struct vnode *src, struct vnode *dst) 2193 { 2194 char *buf; 2195 int alloc; 2196 2197 mutex_enter(&src->v_lock); 2198 if (src->v_path == NULL) { 2199 mutex_exit(&src->v_lock); 2200 return; 2201 } 2202 alloc = strlen(src->v_path) + 1; 2203 2204 /* avoid kmem_alloc() with lock held */ 2205 mutex_exit(&src->v_lock); 2206 buf = kmem_alloc(alloc, KM_SLEEP); 2207 mutex_enter(&src->v_lock); 2208 if (src->v_path == NULL || strlen(src->v_path) + 1 != alloc) { 2209 mutex_exit(&src->v_lock); 2210 kmem_free(buf, alloc); 2211 return; 2212 } 2213 bcopy(src->v_path, buf, alloc); 2214 mutex_exit(&src->v_lock); 2215 2216 mutex_enter(&dst->v_lock); 2217 if (dst->v_path != NULL) { 2218 mutex_exit(&dst->v_lock); 2219 kmem_free(buf, alloc); 2220 return; 2221 } 2222 dst->v_path = buf; 2223 mutex_exit(&dst->v_lock); 2224 } 2225 2226 /* 2227 * XXX Private interface for segvn routines that handle vnode 2228 * large page segments. 2229 * 2230 * return 1 if vp's file system VOP_PAGEIO() implementation 2231 * can be safely used instead of VOP_GETPAGE() for handling 2232 * pagefaults against regular non swap files. VOP_PAGEIO() 2233 * interface is considered safe here if its implementation 2234 * is very close to VOP_GETPAGE() implementation. 2235 * e.g. It zero's out the part of the page beyond EOF. Doesn't 2236 * panic if there're file holes but instead returns an error. 2237 * Doesn't assume file won't be changed by user writes, etc. 2238 * 2239 * return 0 otherwise. 2240 * 2241 * For now allow segvn to only use VOP_PAGEIO() with ufs and nfs. 2242 */ 2243 int 2244 vn_vmpss_usepageio(vnode_t *vp) 2245 { 2246 vfs_t *vfsp = vp->v_vfsp; 2247 char *fsname = vfssw[vfsp->vfs_fstype].vsw_name; 2248 char *pageio_ok_fss[] = {"ufs", "nfs", NULL}; 2249 char **fsok = pageio_ok_fss; 2250 2251 if (fsname == NULL) { 2252 return (0); 2253 } 2254 2255 for (; *fsok; fsok++) { 2256 if (strcmp(*fsok, fsname) == 0) { 2257 return (1); 2258 } 2259 } 2260 return (0); 2261 } 2262 2263 /* VOP_XXX() macros call the corresponding fop_xxx() function */ 2264 2265 int 2266 fop_open( 2267 vnode_t **vpp, 2268 int mode, 2269 cred_t *cr) 2270 { 2271 int ret; 2272 vnode_t *vp = *vpp; 2273 2274 VN_HOLD(vp); 2275 /* 2276 * Adding to the vnode counts before calling open 2277 * avoids the need for a mutex. It circumvents a race 2278 * condition where a query made on the vnode counts results in a 2279 * false negative. The inquirer goes away believing the file is 2280 * not open when there is an open on the file already under way. 2281 * 2282 * The counts are meant to prevent NFS from granting a delegation 2283 * when it would be dangerous to do so. 2284 * 2285 * The vnode counts are only kept on regular files 2286 */ 2287 if ((*vpp)->v_type == VREG) { 2288 if (mode & FREAD) 2289 atomic_add_32(&((*vpp)->v_rdcnt), 1); 2290 if (mode & FWRITE) 2291 atomic_add_32(&((*vpp)->v_wrcnt), 1); 2292 } 2293 2294 ret = (*(*(vpp))->v_op->vop_open)(vpp, mode, cr); 2295 2296 if (ret) { 2297 /* 2298 * Use the saved vp just in case the vnode ptr got trashed 2299 * by the error. 2300 */ 2301 if ((vp->v_type == VREG) && (mode & FREAD)) 2302 atomic_add_32(&(vp->v_rdcnt), -1); 2303 if ((vp->v_type == VREG) && (mode & FWRITE)) 2304 atomic_add_32(&(vp->v_wrcnt), -1); 2305 } else { 2306 /* 2307 * Some filesystems will return a different vnode, 2308 * but the same path was still used to open it. 2309 * So if we do change the vnode and need to 2310 * copy over the path, do so here, rather than special 2311 * casing each filesystem. Adjust the vnode counts to 2312 * reflect the vnode switch. 2313 */ 2314 2315 if (*vpp != vp && *vpp != NULL) { 2316 vn_copypath(vp, *vpp); 2317 if (((*vpp)->v_type == VREG) && (mode & FREAD)) 2318 atomic_add_32(&((*vpp)->v_rdcnt), 1); 2319 if ((vp->v_type == VREG) && (mode & FREAD)) 2320 atomic_add_32(&(vp->v_rdcnt), -1); 2321 if (((*vpp)->v_type == VREG) && (mode & FWRITE)) 2322 atomic_add_32(&((*vpp)->v_wrcnt), 1); 2323 if ((vp->v_type == VREG) && (mode & FWRITE)) 2324 atomic_add_32(&(vp->v_wrcnt), -1); 2325 } 2326 } 2327 VN_RELE(vp); 2328 return (ret); 2329 } 2330 2331 int 2332 fop_close( 2333 vnode_t *vp, 2334 int flag, 2335 int count, 2336 offset_t offset, 2337 cred_t *cr) 2338 { 2339 int error; 2340 error = (*(vp)->v_op->vop_close)(vp, flag, count, offset, cr); 2341 /* 2342 * Check passed in count to handle possible dups. Vnode counts are only 2343 * kept on regular files 2344 */ 2345 if ((vp->v_type == VREG) && (count == 1)) { 2346 if (flag & FREAD) { 2347 ASSERT(vp->v_rdcnt > 0); 2348 atomic_add_32(&(vp->v_rdcnt), -1); 2349 } 2350 if (flag & FWRITE) { 2351 ASSERT(vp->v_wrcnt > 0); 2352 atomic_add_32(&(vp->v_wrcnt), -1); 2353 } 2354 } 2355 return (error); 2356 } 2357 2358 int 2359 fop_read( 2360 vnode_t *vp, 2361 uio_t *uiop, 2362 int ioflag, 2363 cred_t *cr, 2364 struct caller_context *ct) 2365 { 2366 return (*(vp)->v_op->vop_read)(vp, uiop, ioflag, cr, ct); 2367 } 2368 2369 int 2370 fop_write( 2371 vnode_t *vp, 2372 uio_t *uiop, 2373 int ioflag, 2374 cred_t *cr, 2375 struct caller_context *ct) 2376 { 2377 return (*(vp)->v_op->vop_write)(vp, uiop, ioflag, cr, ct); 2378 } 2379 2380 int 2381 fop_ioctl( 2382 vnode_t *vp, 2383 int cmd, 2384 intptr_t arg, 2385 int flag, 2386 cred_t *cr, 2387 int *rvalp) 2388 { 2389 return (*(vp)->v_op->vop_ioctl)(vp, cmd, arg, flag, cr, rvalp); 2390 } 2391 2392 int 2393 fop_setfl( 2394 vnode_t *vp, 2395 int oflags, 2396 int nflags, 2397 cred_t *cr) 2398 { 2399 return (*(vp)->v_op->vop_setfl)(vp, oflags, nflags, cr); 2400 } 2401 2402 int 2403 fop_getattr( 2404 vnode_t *vp, 2405 vattr_t *vap, 2406 int flags, 2407 cred_t *cr) 2408 { 2409 return (*(vp)->v_op->vop_getattr)(vp, vap, flags, cr); 2410 } 2411 2412 int 2413 fop_setattr( 2414 vnode_t *vp, 2415 vattr_t *vap, 2416 int flags, 2417 cred_t *cr, 2418 caller_context_t *ct) 2419 { 2420 return (*(vp)->v_op->vop_setattr)(vp, vap, flags, cr, ct); 2421 } 2422 2423 int 2424 fop_access( 2425 vnode_t *vp, 2426 int mode, 2427 int flags, 2428 cred_t *cr) 2429 { 2430 return (*(vp)->v_op->vop_access)(vp, mode, flags, cr); 2431 } 2432 2433 int 2434 fop_lookup( 2435 vnode_t *dvp, 2436 char *nm, 2437 vnode_t **vpp, 2438 pathname_t *pnp, 2439 int flags, 2440 vnode_t *rdir, 2441 cred_t *cr) 2442 { 2443 int ret; 2444 2445 ret = (*(dvp)->v_op->vop_lookup)(dvp, nm, vpp, pnp, flags, rdir, cr); 2446 if (ret == 0 && *vpp && (*vpp)->v_path == NULL) 2447 vn_setpath(rootdir, dvp, *vpp, nm, strlen(nm)); 2448 2449 return (ret); 2450 } 2451 2452 int 2453 fop_create( 2454 vnode_t *dvp, 2455 char *name, 2456 vattr_t *vap, 2457 vcexcl_t excl, 2458 int mode, 2459 vnode_t **vpp, 2460 cred_t *cr, 2461 int flag) 2462 { 2463 int ret; 2464 2465 ret = (*(dvp)->v_op->vop_create) 2466 (dvp, name, vap, excl, mode, vpp, cr, flag); 2467 if (ret == 0 && *vpp && (*vpp)->v_path == NULL) 2468 vn_setpath(rootdir, dvp, *vpp, name, strlen(name)); 2469 2470 return (ret); 2471 } 2472 2473 int 2474 fop_remove( 2475 vnode_t *dvp, 2476 char *nm, 2477 cred_t *cr) 2478 { 2479 return (*(dvp)->v_op->vop_remove)(dvp, nm, cr); 2480 } 2481 2482 int 2483 fop_link( 2484 vnode_t *tdvp, 2485 vnode_t *svp, 2486 char *tnm, 2487 cred_t *cr) 2488 { 2489 return (*(tdvp)->v_op->vop_link)(tdvp, svp, tnm, cr); 2490 } 2491 2492 int 2493 fop_rename( 2494 vnode_t *sdvp, 2495 char *snm, 2496 vnode_t *tdvp, 2497 char *tnm, 2498 cred_t *cr) 2499 { 2500 return (*(sdvp)->v_op->vop_rename)(sdvp, snm, tdvp, tnm, cr); 2501 } 2502 2503 int 2504 fop_mkdir( 2505 vnode_t *dvp, 2506 char *dirname, 2507 vattr_t *vap, 2508 vnode_t **vpp, 2509 cred_t *cr) 2510 { 2511 int ret; 2512 2513 ret = (*(dvp)->v_op->vop_mkdir)(dvp, dirname, vap, vpp, cr); 2514 if (ret == 0 && *vpp && (*vpp)->v_path == NULL) 2515 vn_setpath(rootdir, dvp, *vpp, dirname, strlen(dirname)); 2516 2517 return (ret); 2518 } 2519 2520 int 2521 fop_rmdir( 2522 vnode_t *dvp, 2523 char *nm, 2524 vnode_t *cdir, 2525 cred_t *cr) 2526 { 2527 return (*(dvp)->v_op->vop_rmdir)(dvp, nm, cdir, cr); 2528 } 2529 2530 int 2531 fop_readdir( 2532 vnode_t *vp, 2533 uio_t *uiop, 2534 cred_t *cr, 2535 int *eofp) 2536 { 2537 return (*(vp)->v_op->vop_readdir)(vp, uiop, cr, eofp); 2538 } 2539 2540 int 2541 fop_symlink( 2542 vnode_t *dvp, 2543 char *linkname, 2544 vattr_t *vap, 2545 char *target, 2546 cred_t *cr) 2547 { 2548 return (*(dvp)->v_op->vop_symlink) (dvp, linkname, vap, target, cr); 2549 } 2550 2551 int 2552 fop_readlink( 2553 vnode_t *vp, 2554 uio_t *uiop, 2555 cred_t *cr) 2556 { 2557 return (*(vp)->v_op->vop_readlink)(vp, uiop, cr); 2558 } 2559 2560 int 2561 fop_fsync( 2562 vnode_t *vp, 2563 int syncflag, 2564 cred_t *cr) 2565 { 2566 return (*(vp)->v_op->vop_fsync)(vp, syncflag, cr); 2567 } 2568 2569 void 2570 fop_inactive( 2571 vnode_t *vp, 2572 cred_t *cr) 2573 { 2574 (*(vp)->v_op->vop_inactive)(vp, cr); 2575 } 2576 2577 int 2578 fop_fid( 2579 vnode_t *vp, 2580 fid_t *fidp) 2581 { 2582 return (*(vp)->v_op->vop_fid)(vp, fidp); 2583 } 2584 2585 int 2586 fop_rwlock( 2587 vnode_t *vp, 2588 int write_lock, 2589 caller_context_t *ct) 2590 { 2591 return ((*(vp)->v_op->vop_rwlock)(vp, write_lock, ct)); 2592 } 2593 2594 void 2595 fop_rwunlock( 2596 vnode_t *vp, 2597 int write_lock, 2598 caller_context_t *ct) 2599 { 2600 (*(vp)->v_op->vop_rwunlock)(vp, write_lock, ct); 2601 } 2602 2603 int 2604 fop_seek( 2605 vnode_t *vp, 2606 offset_t ooff, 2607 offset_t *noffp) 2608 { 2609 return (*(vp)->v_op->vop_seek)(vp, ooff, noffp); 2610 } 2611 2612 int 2613 fop_cmp( 2614 vnode_t *vp1, 2615 vnode_t *vp2) 2616 { 2617 return (*(vp1)->v_op->vop_cmp)(vp1, vp2); 2618 } 2619 2620 int 2621 fop_frlock( 2622 vnode_t *vp, 2623 int cmd, 2624 flock64_t *bfp, 2625 int flag, 2626 offset_t offset, 2627 struct flk_callback *flk_cbp, 2628 cred_t *cr) 2629 { 2630 return (*(vp)->v_op->vop_frlock) 2631 (vp, cmd, bfp, flag, offset, flk_cbp, cr); 2632 } 2633 2634 int 2635 fop_space( 2636 vnode_t *vp, 2637 int cmd, 2638 flock64_t *bfp, 2639 int flag, 2640 offset_t offset, 2641 cred_t *cr, 2642 caller_context_t *ct) 2643 { 2644 return (*(vp)->v_op->vop_space)(vp, cmd, bfp, flag, offset, cr, ct); 2645 } 2646 2647 int 2648 fop_realvp( 2649 vnode_t *vp, 2650 vnode_t **vpp) 2651 { 2652 return (*(vp)->v_op->vop_realvp)(vp, vpp); 2653 } 2654 2655 int 2656 fop_getpage( 2657 vnode_t *vp, 2658 offset_t off, 2659 size_t len, 2660 uint_t *protp, 2661 page_t **plarr, 2662 size_t plsz, 2663 struct seg *seg, 2664 caddr_t addr, 2665 enum seg_rw rw, 2666 cred_t *cr) 2667 { 2668 return (*(vp)->v_op->vop_getpage) 2669 (vp, off, len, protp, plarr, plsz, seg, addr, rw, cr); 2670 } 2671 2672 int 2673 fop_putpage( 2674 vnode_t *vp, 2675 offset_t off, 2676 size_t len, 2677 int flags, 2678 cred_t *cr) 2679 { 2680 return (*(vp)->v_op->vop_putpage)(vp, off, len, flags, cr); 2681 } 2682 2683 int 2684 fop_map( 2685 vnode_t *vp, 2686 offset_t off, 2687 struct as *as, 2688 caddr_t *addrp, 2689 size_t len, 2690 uchar_t prot, 2691 uchar_t maxprot, 2692 uint_t flags, 2693 cred_t *cr) 2694 { 2695 return (*(vp)->v_op->vop_map) 2696 (vp, off, as, addrp, len, prot, maxprot, flags, cr); 2697 } 2698 2699 int 2700 fop_addmap( 2701 vnode_t *vp, 2702 offset_t off, 2703 struct as *as, 2704 caddr_t addr, 2705 size_t len, 2706 uchar_t prot, 2707 uchar_t maxprot, 2708 uint_t flags, 2709 cred_t *cr) 2710 { 2711 int error; 2712 u_longlong_t delta; 2713 2714 error = (*(vp)->v_op->vop_addmap) 2715 (vp, off, as, addr, len, prot, maxprot, flags, cr); 2716 2717 if ((!error) && (vp->v_type == VREG)) { 2718 delta = (u_longlong_t)btopr(len); 2719 /* 2720 * If file is declared MAP_PRIVATE, it can't be written back 2721 * even if open for write. Handle as read. 2722 */ 2723 if (flags & MAP_PRIVATE) { 2724 atomic_add_64((uint64_t *)(&(vp->v_mmap_read)), 2725 (int64_t)delta); 2726 } else { 2727 /* 2728 * atomic_add_64 forces the fetch of a 64 bit value to 2729 * be atomic on 32 bit machines 2730 */ 2731 if (maxprot & PROT_WRITE) 2732 atomic_add_64((uint64_t *)(&(vp->v_mmap_write)), 2733 (int64_t)delta); 2734 if (maxprot & PROT_READ) 2735 atomic_add_64((uint64_t *)(&(vp->v_mmap_read)), 2736 (int64_t)delta); 2737 if (maxprot & PROT_EXEC) 2738 atomic_add_64((uint64_t *)(&(vp->v_mmap_read)), 2739 (int64_t)delta); 2740 } 2741 } 2742 return (error); 2743 } 2744 2745 int 2746 fop_delmap( 2747 vnode_t *vp, 2748 offset_t off, 2749 struct as *as, 2750 caddr_t addr, 2751 size_t len, 2752 uint_t prot, 2753 uint_t maxprot, 2754 uint_t flags, 2755 cred_t *cr) 2756 { 2757 int error; 2758 u_longlong_t delta; 2759 error = (*(vp)->v_op->vop_delmap) 2760 (vp, off, as, addr, len, prot, maxprot, flags, cr); 2761 2762 /* 2763 * NFS calls into delmap twice, the first time 2764 * it simply establishes a callback mechanism and returns EAGAIN 2765 * while the real work is being done upon the second invocation. 2766 * We have to detect this here and only decrement the counts upon 2767 * the second delmap request. 2768 */ 2769 if ((error != EAGAIN) && (vp->v_type == VREG)) { 2770 2771 delta = (u_longlong_t)btopr(len); 2772 2773 if (flags & MAP_PRIVATE) { 2774 atomic_add_64((uint64_t *)(&(vp->v_mmap_read)), 2775 (int64_t)(-delta)); 2776 } else { 2777 /* 2778 * atomic_add_64 forces the fetch of a 64 bit value 2779 * to be atomic on 32 bit machines 2780 */ 2781 if (maxprot & PROT_WRITE) 2782 atomic_add_64((uint64_t *)(&(vp->v_mmap_write)), 2783 (int64_t)(-delta)); 2784 if (maxprot & PROT_READ) 2785 atomic_add_64((uint64_t *)(&(vp->v_mmap_read)), 2786 (int64_t)(-delta)); 2787 if (maxprot & PROT_EXEC) 2788 atomic_add_64((uint64_t *)(&(vp->v_mmap_read)), 2789 (int64_t)(-delta)); 2790 } 2791 } 2792 return (error); 2793 } 2794 2795 2796 int 2797 fop_poll( 2798 vnode_t *vp, 2799 short events, 2800 int anyyet, 2801 short *reventsp, 2802 struct pollhead **phpp) 2803 { 2804 return (*(vp)->v_op->vop_poll)(vp, events, anyyet, reventsp, phpp); 2805 } 2806 2807 int 2808 fop_dump( 2809 vnode_t *vp, 2810 caddr_t addr, 2811 int lbdn, 2812 int dblks) 2813 { 2814 return (*(vp)->v_op->vop_dump)(vp, addr, lbdn, dblks); 2815 } 2816 2817 int 2818 fop_pathconf( 2819 vnode_t *vp, 2820 int cmd, 2821 ulong_t *valp, 2822 cred_t *cr) 2823 { 2824 return (*(vp)->v_op->vop_pathconf)(vp, cmd, valp, cr); 2825 } 2826 2827 int 2828 fop_pageio( 2829 vnode_t *vp, 2830 struct page *pp, 2831 u_offset_t io_off, 2832 size_t io_len, 2833 int flags, 2834 cred_t *cr) 2835 { 2836 return (*(vp)->v_op->vop_pageio)(vp, pp, io_off, io_len, flags, cr); 2837 } 2838 2839 int 2840 fop_dumpctl( 2841 vnode_t *vp, 2842 int action, 2843 int *blkp) 2844 { 2845 return (*(vp)->v_op->vop_dumpctl)(vp, action, blkp); 2846 } 2847 2848 void 2849 fop_dispose( 2850 vnode_t *vp, 2851 page_t *pp, 2852 int flag, 2853 int dn, 2854 cred_t *cr) 2855 { 2856 (*(vp)->v_op->vop_dispose)(vp, pp, flag, dn, cr); 2857 } 2858 2859 int 2860 fop_setsecattr( 2861 vnode_t *vp, 2862 vsecattr_t *vsap, 2863 int flag, 2864 cred_t *cr) 2865 { 2866 return (*(vp)->v_op->vop_setsecattr) (vp, vsap, flag, cr); 2867 } 2868 2869 int 2870 fop_getsecattr( 2871 vnode_t *vp, 2872 vsecattr_t *vsap, 2873 int flag, 2874 cred_t *cr) 2875 { 2876 return (*(vp)->v_op->vop_getsecattr) (vp, vsap, flag, cr); 2877 } 2878 2879 int 2880 fop_shrlock( 2881 vnode_t *vp, 2882 int cmd, 2883 struct shrlock *shr, 2884 int flag, 2885 cred_t *cr) 2886 { 2887 return (*(vp)->v_op->vop_shrlock)(vp, cmd, shr, flag, cr); 2888 } 2889 2890 int 2891 fop_vnevent(vnode_t *vp, vnevent_t vnevent) 2892 { 2893 return (*(vp)->v_op->vop_vnevent)(vp, vnevent); 2894 } 2895