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