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