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