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