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 /*
  23  * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
  24  * Copyright 2016, Joyent, Inc.
  25  */
  26 
  27 /*      Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
  28 /*        All Rights Reserved   */
  29 
  30 /*
  31  * University Copyright- Copyright (c) 1982, 1986, 1988
  32  * The Regents of the University of California
  33  * All Rights Reserved
  34  *
  35  * University Acknowledgment- Portions of this document are derived from
  36  * software developed by the University of California, Berkeley, and its
  37  * contributors.
  38  */
  39 
  40 #include <sys/types.h>
  41 #include <sys/param.h>
  42 #include <sys/t_lock.h>
  43 #include <sys/errno.h>
  44 #include <sys/cred.h>
  45 #include <sys/user.h>
  46 #include <sys/uio.h>
  47 #include <sys/file.h>
  48 #include <sys/pathname.h>
  49 #include <sys/vfs.h>
  50 #include <sys/vfs_opreg.h>
  51 #include <sys/vnode.h>
  52 #include <sys/rwstlock.h>
  53 #include <sys/fem.h>
  54 #include <sys/stat.h>
  55 #include <sys/mode.h>
  56 #include <sys/conf.h>
  57 #include <sys/sysmacros.h>
  58 #include <sys/cmn_err.h>
  59 #include <sys/systm.h>
  60 #include <sys/kmem.h>
  61 #include <sys/debug.h>
  62 #include <c2/audit.h>
  63 #include <sys/acl.h>
  64 #include <sys/nbmlock.h>
  65 #include <sys/fcntl.h>
  66 #include <fs/fs_subr.h>
  67 #include <sys/taskq.h>
  68 #include <fs/fs_reparse.h>
  69 #include <sys/time.h>
  70 #include <sys/sdt.h>
  71 
  72 /* Determine if this vnode is a file that is read-only */
  73 #define ISROFILE(vp)    \
  74         ((vp)->v_type != VCHR && (vp)->v_type != VBLK && \
  75             (vp)->v_type != VFIFO && vn_is_readonly(vp))
  76 
  77 /* Tunable via /etc/system; used only by admin/install */
  78 int nfs_global_client_only;
  79 
  80 /*
  81  * Array of vopstats_t for per-FS-type vopstats.  This array has the same
  82  * number of entries as and parallel to the vfssw table.  (Arguably, it could
  83  * be part of the vfssw table.)  Once it's initialized, it's accessed using
  84  * the same fstype index that is used to index into the vfssw table.
  85  */
  86 vopstats_t **vopstats_fstype;
  87 
  88 /* vopstats initialization template used for fast initialization via bcopy() */
  89 static vopstats_t *vs_templatep;
  90 
  91 /* Kmem cache handle for vsk_anchor_t allocations */
  92 kmem_cache_t *vsk_anchor_cache;
  93 
  94 /* file events cleanup routine */
  95 extern void free_fopdata(vnode_t *);
  96 
  97 /*
  98  * Root of AVL tree for the kstats associated with vopstats.  Lock protects
  99  * updates to vsktat_tree.
 100  */
 101 avl_tree_t      vskstat_tree;
 102 kmutex_t        vskstat_tree_lock;
 103 
 104 /* Global variable which enables/disables the vopstats collection */
 105 int vopstats_enabled = 1;
 106 
 107 /* Global used for empty/invalid v_path */
 108 char *vn_vpath_empty = "";
 109 
 110 /*
 111  * forward declarations for internal vnode specific data (vsd)
 112  */
 113 static void *vsd_realloc(void *, size_t, size_t);
 114 
 115 /*
 116  * forward declarations for reparse point functions
 117  */
 118 static int fs_reparse_mark(char *target, vattr_t *vap, xvattr_t *xvattr);
 119 
 120 /*
 121  * VSD -- VNODE SPECIFIC DATA
 122  * The v_data pointer is typically used by a file system to store a
 123  * pointer to the file system's private node (e.g. ufs inode, nfs rnode).
 124  * However, there are times when additional project private data needs
 125  * to be stored separately from the data (node) pointed to by v_data.
 126  * This additional data could be stored by the file system itself or
 127  * by a completely different kernel entity.  VSD provides a way for
 128  * callers to obtain a key and store a pointer to private data associated
 129  * with a vnode.
 130  *
 131  * Callers are responsible for protecting the vsd by holding v_vsd_lock
 132  * for calls to vsd_set() and vsd_get().
 133  */
 134 
 135 /*
 136  * vsd_lock protects:
 137  *   vsd_nkeys - creation and deletion of vsd keys
 138  *   vsd_list - insertion and deletion of vsd_node in the vsd_list
 139  *   vsd_destructor - adding and removing destructors to the list
 140  */
 141 static kmutex_t         vsd_lock;
 142 static uint_t           vsd_nkeys;       /* size of destructor array */
 143 /* list of vsd_node's */
 144 static list_t *vsd_list = NULL;
 145 /* per-key destructor funcs */
 146 static void             (**vsd_destructor)(void *);
 147 
 148 /*
 149  * The following is the common set of actions needed to update the
 150  * vopstats structure from a vnode op.  Both VOPSTATS_UPDATE() and
 151  * VOPSTATS_UPDATE_IO() do almost the same thing, except for the
 152  * recording of the bytes transferred.  Since the code is similar
 153  * but small, it is nearly a duplicate.  Consequently any changes
 154  * to one may need to be reflected in the other.
 155  * Rundown of the variables:
 156  * vp - Pointer to the vnode
 157  * counter - Partial name structure member to update in vopstats for counts
 158  * bytecounter - Partial name structure member to update in vopstats for bytes
 159  * bytesval - Value to update in vopstats for bytes
 160  * fstype - Index into vsanchor_fstype[], same as index into vfssw[]
 161  * vsp - Pointer to vopstats structure (either in vfs or vsanchor_fstype[i])
 162  */
 163 
 164 #define VOPSTATS_UPDATE(vp, counter) {                                  \
 165         vfs_t *vfsp = (vp)->v_vfsp;                                  \
 166         if (vfsp && vfsp->vfs_implp &&                                       \
 167             (vfsp->vfs_flag & VFS_STATS) && (vp)->v_type != VBAD) {   \
 168                 vopstats_t *vsp = &vfsp->vfs_vopstats;                   \
 169                 uint64_t *stataddr = &(vsp->n##counter.value.ui64);      \
 170                 extern void __dtrace_probe___fsinfo_##counter(vnode_t *, \
 171                     size_t, uint64_t *);                                \
 172                 __dtrace_probe___fsinfo_##counter(vp, 0, stataddr);     \
 173                 (*stataddr)++;                                          \
 174                 if ((vsp = vfsp->vfs_fstypevsp) != NULL) {           \
 175                         vsp->n##counter.value.ui64++;                        \
 176                 }                                                       \
 177         }                                                               \
 178 }
 179 
 180 #define VOPSTATS_UPDATE_IO(vp, counter, bytecounter, bytesval) {        \
 181         vfs_t *vfsp = (vp)->v_vfsp;                                  \
 182         if (vfsp && vfsp->vfs_implp &&                                       \
 183             (vfsp->vfs_flag & VFS_STATS) && (vp)->v_type != VBAD) {   \
 184                 vopstats_t *vsp = &vfsp->vfs_vopstats;                   \
 185                 uint64_t *stataddr = &(vsp->n##counter.value.ui64);      \
 186                 extern void __dtrace_probe___fsinfo_##counter(vnode_t *, \
 187                     size_t, uint64_t *);                                \
 188                 __dtrace_probe___fsinfo_##counter(vp, bytesval, stataddr); \
 189                 (*stataddr)++;                                          \
 190                 vsp->bytecounter.value.ui64 += bytesval;             \
 191                 if ((vsp = vfsp->vfs_fstypevsp) != NULL) {           \
 192                         vsp->n##counter.value.ui64++;                        \
 193                         vsp->bytecounter.value.ui64 += bytesval;     \
 194                 }                                                       \
 195         }                                                               \
 196 }
 197 
 198 /*
 199  * If the filesystem does not support XIDs map credential
 200  * If the vfsp is NULL, perhaps we should also map?
 201  */
 202 #define VOPXID_MAP_CR(vp, cr)   {                                       \
 203         vfs_t *vfsp = (vp)->v_vfsp;                                  \
 204         if (vfsp != NULL && (vfsp->vfs_flag & VFS_XID) == 0)             \
 205                 cr = crgetmapped(cr);                                   \
 206         }
 207 
 208 #define VOP_LATENCY_10MS        10000000
 209 #define VOP_LATENCY_100MS       100000000
 210 #define VOP_LATENCY_1S          1000000000
 211 #define VOP_LATENCY_10S         10000000000
 212 
 213 /*
 214  * Convert stat(2) formats to vnode types and vice versa.  (Knows about
 215  * numerical order of S_IFMT and vnode types.)
 216  */
 217 enum vtype iftovt_tab[] = {
 218         VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
 219         VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VNON
 220 };
 221 
 222 ushort_t vttoif_tab[] = {
 223         0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK, S_IFIFO,
 224         S_IFDOOR, 0, S_IFSOCK, S_IFPORT, 0
 225 };
 226 
 227 /*
 228  * The system vnode cache.
 229  */
 230 
 231 kmem_cache_t *vn_cache;
 232 
 233 
 234 /*
 235  * Vnode operations vector.
 236  */
 237 
 238 static const fs_operation_trans_def_t vn_ops_table[] = {
 239         VOPNAME_OPEN, offsetof(struct vnodeops, vop_open),
 240             fs_nosys, fs_nosys,
 241 
 242         VOPNAME_CLOSE, offsetof(struct vnodeops, vop_close),
 243             fs_nosys, fs_nosys,
 244 
 245         VOPNAME_READ, offsetof(struct vnodeops, vop_read),
 246             fs_nosys, fs_nosys,
 247 
 248         VOPNAME_WRITE, offsetof(struct vnodeops, vop_write),
 249             fs_nosys, fs_nosys,
 250 
 251         VOPNAME_IOCTL, offsetof(struct vnodeops, vop_ioctl),
 252             fs_nosys, fs_nosys,
 253 
 254         VOPNAME_SETFL, offsetof(struct vnodeops, vop_setfl),
 255             fs_setfl, fs_nosys,
 256 
 257         VOPNAME_GETATTR, offsetof(struct vnodeops, vop_getattr),
 258             fs_nosys, fs_nosys,
 259 
 260         VOPNAME_SETATTR, offsetof(struct vnodeops, vop_setattr),
 261             fs_nosys, fs_nosys,
 262 
 263         VOPNAME_ACCESS, offsetof(struct vnodeops, vop_access),
 264             fs_nosys, fs_nosys,
 265 
 266         VOPNAME_LOOKUP, offsetof(struct vnodeops, vop_lookup),
 267             fs_nosys, fs_nosys,
 268 
 269         VOPNAME_CREATE, offsetof(struct vnodeops, vop_create),
 270             fs_nosys, fs_nosys,
 271 
 272         VOPNAME_REMOVE, offsetof(struct vnodeops, vop_remove),
 273             fs_nosys, fs_nosys,
 274 
 275         VOPNAME_LINK, offsetof(struct vnodeops, vop_link),
 276             fs_nosys, fs_nosys,
 277 
 278         VOPNAME_RENAME, offsetof(struct vnodeops, vop_rename),
 279             fs_nosys, fs_nosys,
 280 
 281         VOPNAME_MKDIR, offsetof(struct vnodeops, vop_mkdir),
 282             fs_nosys, fs_nosys,
 283 
 284         VOPNAME_RMDIR, offsetof(struct vnodeops, vop_rmdir),
 285             fs_nosys, fs_nosys,
 286 
 287         VOPNAME_READDIR, offsetof(struct vnodeops, vop_readdir),
 288             fs_nosys, fs_nosys,
 289 
 290         VOPNAME_SYMLINK, offsetof(struct vnodeops, vop_symlink),
 291             fs_nosys, fs_nosys,
 292 
 293         VOPNAME_READLINK, offsetof(struct vnodeops, vop_readlink),
 294             fs_nosys, fs_nosys,
 295 
 296         VOPNAME_FSYNC, offsetof(struct vnodeops, vop_fsync),
 297             fs_nosys, fs_nosys,
 298 
 299         VOPNAME_INACTIVE, offsetof(struct vnodeops, vop_inactive),
 300             fs_nosys, fs_nosys,
 301 
 302         VOPNAME_FID, offsetof(struct vnodeops, vop_fid),
 303             fs_nosys, fs_nosys,
 304 
 305         VOPNAME_RWLOCK, offsetof(struct vnodeops, vop_rwlock),
 306             fs_rwlock, fs_rwlock,
 307 
 308         VOPNAME_RWUNLOCK, offsetof(struct vnodeops, vop_rwunlock),
 309             (fs_generic_func_p) fs_rwunlock,
 310             (fs_generic_func_p) fs_rwunlock,    /* no errors allowed */
 311 
 312         VOPNAME_SEEK, offsetof(struct vnodeops, vop_seek),
 313             fs_nosys, fs_nosys,
 314 
 315         VOPNAME_CMP, offsetof(struct vnodeops, vop_cmp),
 316             fs_cmp, fs_cmp,             /* no errors allowed */
 317 
 318         VOPNAME_FRLOCK, offsetof(struct vnodeops, vop_frlock),
 319             fs_frlock, fs_nosys,
 320 
 321         VOPNAME_SPACE, offsetof(struct vnodeops, vop_space),
 322             fs_nosys, fs_nosys,
 323 
 324         VOPNAME_REALVP, offsetof(struct vnodeops, vop_realvp),
 325             fs_nosys, fs_nosys,
 326 
 327         VOPNAME_GETPAGE, offsetof(struct vnodeops, vop_getpage),
 328             fs_nosys, fs_nosys,
 329 
 330         VOPNAME_PUTPAGE, offsetof(struct vnodeops, vop_putpage),
 331             fs_nosys, fs_nosys,
 332 
 333         VOPNAME_MAP, offsetof(struct vnodeops, vop_map),
 334             (fs_generic_func_p) fs_nosys_map,
 335             (fs_generic_func_p) fs_nosys_map,
 336 
 337         VOPNAME_ADDMAP, offsetof(struct vnodeops, vop_addmap),
 338             (fs_generic_func_p) fs_nosys_addmap,
 339             (fs_generic_func_p) fs_nosys_addmap,
 340 
 341         VOPNAME_DELMAP, offsetof(struct vnodeops, vop_delmap),
 342             fs_nosys, fs_nosys,
 343 
 344         VOPNAME_POLL, offsetof(struct vnodeops, vop_poll),
 345             (fs_generic_func_p) fs_poll, (fs_generic_func_p) fs_nosys_poll,
 346 
 347         VOPNAME_DUMP, offsetof(struct vnodeops, vop_dump),
 348             fs_nosys, fs_nosys,
 349 
 350         VOPNAME_PATHCONF, offsetof(struct vnodeops, vop_pathconf),
 351             fs_pathconf, fs_nosys,
 352 
 353         VOPNAME_PAGEIO, offsetof(struct vnodeops, vop_pageio),
 354             fs_nosys, fs_nosys,
 355 
 356         VOPNAME_DUMPCTL, offsetof(struct vnodeops, vop_dumpctl),
 357             fs_nosys, fs_nosys,
 358 
 359         VOPNAME_DISPOSE, offsetof(struct vnodeops, vop_dispose),
 360             (fs_generic_func_p) fs_dispose,
 361             (fs_generic_func_p) fs_nodispose,
 362 
 363         VOPNAME_SETSECATTR, offsetof(struct vnodeops, vop_setsecattr),
 364             fs_nosys, fs_nosys,
 365 
 366         VOPNAME_GETSECATTR, offsetof(struct vnodeops, vop_getsecattr),
 367             fs_fab_acl, fs_nosys,
 368 
 369         VOPNAME_SHRLOCK, offsetof(struct vnodeops, vop_shrlock),
 370             fs_shrlock, fs_nosys,
 371 
 372         VOPNAME_VNEVENT, offsetof(struct vnodeops, vop_vnevent),
 373             (fs_generic_func_p) fs_vnevent_nosupport,
 374             (fs_generic_func_p) fs_vnevent_nosupport,
 375 
 376         VOPNAME_REQZCBUF, offsetof(struct vnodeops, vop_reqzcbuf),
 377             fs_nosys, fs_nosys,
 378 
 379         VOPNAME_RETZCBUF, offsetof(struct vnodeops, vop_retzcbuf),
 380             fs_nosys, fs_nosys,
 381 
 382         NULL, 0, NULL, NULL
 383 };
 384 
 385 /* Extensible attribute (xva) routines. */
 386 
 387 /*
 388  * Zero out the structure, set the size of the requested/returned bitmaps,
 389  * set AT_XVATTR in the embedded vattr_t's va_mask, and set up the pointer
 390  * to the returned attributes array.
 391  */
 392 void
 393 xva_init(xvattr_t *xvap)
 394 {
 395         bzero(xvap, sizeof (xvattr_t));
 396         xvap->xva_mapsize = XVA_MAPSIZE;
 397         xvap->xva_magic = XVA_MAGIC;
 398         xvap->xva_vattr.va_mask = AT_XVATTR;
 399         xvap->xva_rtnattrmapp = &(xvap->xva_rtnattrmap)[0];
 400 }
 401 
 402 /*
 403  * If AT_XVATTR is set, returns a pointer to the embedded xoptattr_t
 404  * structure.  Otherwise, returns NULL.
 405  */
 406 xoptattr_t *
 407 xva_getxoptattr(xvattr_t *xvap)
 408 {
 409         xoptattr_t *xoap = NULL;
 410         if (xvap->xva_vattr.va_mask & AT_XVATTR)
 411                 xoap = &xvap->xva_xoptattrs;
 412         return (xoap);
 413 }
 414 
 415 /*
 416  * Used by the AVL routines to compare two vsk_anchor_t structures in the tree.
 417  * We use the f_fsid reported by VFS_STATVFS() since we use that for the
 418  * kstat name.
 419  */
 420 static int
 421 vska_compar(const void *n1, const void *n2)
 422 {
 423         int ret;
 424         ulong_t p1 = ((vsk_anchor_t *)n1)->vsk_fsid;
 425         ulong_t p2 = ((vsk_anchor_t *)n2)->vsk_fsid;
 426 
 427         if (p1 < p2) {
 428                 ret = -1;
 429         } else if (p1 > p2) {
 430                 ret = 1;
 431         } else {
 432                 ret = 0;
 433         }
 434 
 435         return (ret);
 436 }
 437 
 438 /*
 439  * Used to create a single template which will be bcopy()ed to a newly
 440  * allocated vsanchor_combo_t structure in new_vsanchor(), below.
 441  */
 442 static vopstats_t *
 443 create_vopstats_template()
 444 {
 445         vopstats_t              *vsp;
 446 
 447         vsp = kmem_alloc(sizeof (vopstats_t), KM_SLEEP);
 448         bzero(vsp, sizeof (*vsp));      /* Start fresh */
 449 
 450         /* VOP_OPEN */
 451         kstat_named_init(&vsp->nopen, "nopen", KSTAT_DATA_UINT64);
 452         /* VOP_CLOSE */
 453         kstat_named_init(&vsp->nclose, "nclose", KSTAT_DATA_UINT64);
 454         /* VOP_READ I/O */
 455         kstat_named_init(&vsp->nread, "nread", KSTAT_DATA_UINT64);
 456         kstat_named_init(&vsp->read_bytes, "read_bytes", KSTAT_DATA_UINT64);
 457         /* VOP_WRITE I/O */
 458         kstat_named_init(&vsp->nwrite, "nwrite", KSTAT_DATA_UINT64);
 459         kstat_named_init(&vsp->write_bytes, "write_bytes", KSTAT_DATA_UINT64);
 460         /* VOP_IOCTL */
 461         kstat_named_init(&vsp->nioctl, "nioctl", KSTAT_DATA_UINT64);
 462         /* VOP_SETFL */
 463         kstat_named_init(&vsp->nsetfl, "nsetfl", KSTAT_DATA_UINT64);
 464         /* VOP_GETATTR */
 465         kstat_named_init(&vsp->ngetattr, "ngetattr", KSTAT_DATA_UINT64);
 466         /* VOP_SETATTR */
 467         kstat_named_init(&vsp->nsetattr, "nsetattr", KSTAT_DATA_UINT64);
 468         /* VOP_ACCESS */
 469         kstat_named_init(&vsp->naccess, "naccess", KSTAT_DATA_UINT64);
 470         /* VOP_LOOKUP */
 471         kstat_named_init(&vsp->nlookup, "nlookup", KSTAT_DATA_UINT64);
 472         /* VOP_CREATE */
 473         kstat_named_init(&vsp->ncreate, "ncreate", KSTAT_DATA_UINT64);
 474         /* VOP_REMOVE */
 475         kstat_named_init(&vsp->nremove, "nremove", KSTAT_DATA_UINT64);
 476         /* VOP_LINK */
 477         kstat_named_init(&vsp->nlink, "nlink", KSTAT_DATA_UINT64);
 478         /* VOP_RENAME */
 479         kstat_named_init(&vsp->nrename, "nrename", KSTAT_DATA_UINT64);
 480         /* VOP_MKDIR */
 481         kstat_named_init(&vsp->nmkdir, "nmkdir", KSTAT_DATA_UINT64);
 482         /* VOP_RMDIR */
 483         kstat_named_init(&vsp->nrmdir, "nrmdir", KSTAT_DATA_UINT64);
 484         /* VOP_READDIR I/O */
 485         kstat_named_init(&vsp->nreaddir, "nreaddir", KSTAT_DATA_UINT64);
 486         kstat_named_init(&vsp->readdir_bytes, "readdir_bytes",
 487             KSTAT_DATA_UINT64);
 488         /* VOP_SYMLINK */
 489         kstat_named_init(&vsp->nsymlink, "nsymlink", KSTAT_DATA_UINT64);
 490         /* VOP_READLINK */
 491         kstat_named_init(&vsp->nreadlink, "nreadlink", KSTAT_DATA_UINT64);
 492         /* VOP_FSYNC */
 493         kstat_named_init(&vsp->nfsync, "nfsync", KSTAT_DATA_UINT64);
 494         /* VOP_INACTIVE */
 495         kstat_named_init(&vsp->ninactive, "ninactive", KSTAT_DATA_UINT64);
 496         /* VOP_FID */
 497         kstat_named_init(&vsp->nfid, "nfid", KSTAT_DATA_UINT64);
 498         /* VOP_RWLOCK */
 499         kstat_named_init(&vsp->nrwlock, "nrwlock", KSTAT_DATA_UINT64);
 500         /* VOP_RWUNLOCK */
 501         kstat_named_init(&vsp->nrwunlock, "nrwunlock", KSTAT_DATA_UINT64);
 502         /* VOP_SEEK */
 503         kstat_named_init(&vsp->nseek, "nseek", KSTAT_DATA_UINT64);
 504         /* VOP_CMP */
 505         kstat_named_init(&vsp->ncmp, "ncmp", KSTAT_DATA_UINT64);
 506         /* VOP_FRLOCK */
 507         kstat_named_init(&vsp->nfrlock, "nfrlock", KSTAT_DATA_UINT64);
 508         /* VOP_SPACE */
 509         kstat_named_init(&vsp->nspace, "nspace", KSTAT_DATA_UINT64);
 510         /* VOP_REALVP */
 511         kstat_named_init(&vsp->nrealvp, "nrealvp", KSTAT_DATA_UINT64);
 512         /* VOP_GETPAGE */
 513         kstat_named_init(&vsp->ngetpage, "ngetpage", KSTAT_DATA_UINT64);
 514         /* VOP_PUTPAGE */
 515         kstat_named_init(&vsp->nputpage, "nputpage", KSTAT_DATA_UINT64);
 516         /* VOP_MAP */
 517         kstat_named_init(&vsp->nmap, "nmap", KSTAT_DATA_UINT64);
 518         /* VOP_ADDMAP */
 519         kstat_named_init(&vsp->naddmap, "naddmap", KSTAT_DATA_UINT64);
 520         /* VOP_DELMAP */
 521         kstat_named_init(&vsp->ndelmap, "ndelmap", KSTAT_DATA_UINT64);
 522         /* VOP_POLL */
 523         kstat_named_init(&vsp->npoll, "npoll", KSTAT_DATA_UINT64);
 524         /* VOP_DUMP */
 525         kstat_named_init(&vsp->ndump, "ndump", KSTAT_DATA_UINT64);
 526         /* VOP_PATHCONF */
 527         kstat_named_init(&vsp->npathconf, "npathconf", KSTAT_DATA_UINT64);
 528         /* VOP_PAGEIO */
 529         kstat_named_init(&vsp->npageio, "npageio", KSTAT_DATA_UINT64);
 530         /* VOP_DUMPCTL */
 531         kstat_named_init(&vsp->ndumpctl, "ndumpctl", KSTAT_DATA_UINT64);
 532         /* VOP_DISPOSE */
 533         kstat_named_init(&vsp->ndispose, "ndispose", KSTAT_DATA_UINT64);
 534         /* VOP_SETSECATTR */
 535         kstat_named_init(&vsp->nsetsecattr, "nsetsecattr", KSTAT_DATA_UINT64);
 536         /* VOP_GETSECATTR */
 537         kstat_named_init(&vsp->ngetsecattr, "ngetsecattr", KSTAT_DATA_UINT64);
 538         /* VOP_SHRLOCK */
 539         kstat_named_init(&vsp->nshrlock, "nshrlock", KSTAT_DATA_UINT64);
 540         /* VOP_VNEVENT */
 541         kstat_named_init(&vsp->nvnevent, "nvnevent", KSTAT_DATA_UINT64);
 542         /* VOP_REQZCBUF */
 543         kstat_named_init(&vsp->nreqzcbuf, "nreqzcbuf", KSTAT_DATA_UINT64);
 544         /* VOP_RETZCBUF */
 545         kstat_named_init(&vsp->nretzcbuf, "nretzcbuf", KSTAT_DATA_UINT64);
 546 
 547         return (vsp);
 548 }
 549 
 550 /*
 551  * Creates a kstat structure associated with a vopstats structure.
 552  */
 553 kstat_t *
 554 new_vskstat(char *ksname, vopstats_t *vsp)
 555 {
 556         kstat_t         *ksp;
 557 
 558         if (!vopstats_enabled) {
 559                 return (NULL);
 560         }
 561 
 562         ksp = kstat_create("unix", 0, ksname, "misc", KSTAT_TYPE_NAMED,
 563             sizeof (vopstats_t)/sizeof (kstat_named_t),
 564             KSTAT_FLAG_VIRTUAL|KSTAT_FLAG_WRITABLE);
 565         if (ksp) {
 566                 ksp->ks_data = vsp;
 567                 kstat_install(ksp);
 568         }
 569 
 570         return (ksp);
 571 }
 572 
 573 /*
 574  * Called from vfsinit() to initialize the support mechanisms for vopstats
 575  */
 576 void
 577 vopstats_startup()
 578 {
 579         if (!vopstats_enabled)
 580                 return;
 581 
 582         /*
 583          * Creates the AVL tree which holds per-vfs vopstat anchors.  This
 584          * is necessary since we need to check if a kstat exists before we
 585          * attempt to create it.  Also, initialize its lock.
 586          */
 587         avl_create(&vskstat_tree, vska_compar, sizeof (vsk_anchor_t),
 588             offsetof(vsk_anchor_t, vsk_node));
 589         mutex_init(&vskstat_tree_lock, NULL, MUTEX_DEFAULT, NULL);
 590 
 591         vsk_anchor_cache = kmem_cache_create("vsk_anchor_cache",
 592             sizeof (vsk_anchor_t), sizeof (uintptr_t), NULL, NULL, NULL,
 593             NULL, NULL, 0);
 594 
 595         /*
 596          * Set up the array of pointers for the vopstats-by-FS-type.
 597          * The entries will be allocated/initialized as each file system
 598          * goes through modload/mod_installfs.
 599          */
 600         vopstats_fstype = (vopstats_t **)kmem_zalloc(
 601             (sizeof (vopstats_t *) * nfstype), KM_SLEEP);
 602 
 603         /* Set up the global vopstats initialization template */
 604         vs_templatep = create_vopstats_template();
 605 }
 606 
 607 /*
 608  * We need to have the all of the counters zeroed.
 609  * The initialization of the vopstats_t includes on the order of
 610  * 50 calls to kstat_named_init().  Rather that do that on every call,
 611  * we do it once in a template (vs_templatep) then bcopy it over.
 612  */
 613 void
 614 initialize_vopstats(vopstats_t *vsp)
 615 {
 616         if (vsp == NULL)
 617                 return;
 618 
 619         bcopy(vs_templatep, vsp, sizeof (vopstats_t));
 620 }
 621 
 622 /*
 623  * If possible, determine which vopstats by fstype to use and
 624  * return a pointer to the caller.
 625  */
 626 vopstats_t *
 627 get_fstype_vopstats(vfs_t *vfsp, struct vfssw *vswp)
 628 {
 629         int             fstype = 0;     /* Index into vfssw[] */
 630         vopstats_t      *vsp = NULL;
 631 
 632         if (vfsp == NULL || (vfsp->vfs_flag & VFS_STATS) == 0 ||
 633             !vopstats_enabled)
 634                 return (NULL);
 635         /*
 636          * Set up the fstype.  We go to so much trouble because all versions
 637          * of NFS use the same fstype in their vfs even though they have
 638          * distinct entries in the vfssw[] table.
 639          * NOTE: A special vfs (e.g., EIO_vfs) may not have an entry.
 640          */
 641         if (vswp) {
 642                 fstype = vswp - vfssw;  /* Gets us the index */
 643         } else {
 644                 fstype = vfsp->vfs_fstype;
 645         }
 646 
 647         /*
 648          * Point to the per-fstype vopstats. The only valid values are
 649          * non-zero positive values less than the number of vfssw[] table
 650          * entries.
 651          */
 652         if (fstype > 0 && fstype < nfstype) {
 653                 vsp = vopstats_fstype[fstype];
 654         }
 655 
 656         return (vsp);
 657 }
 658 
 659 /*
 660  * Generate a kstat name, create the kstat structure, and allocate a
 661  * vsk_anchor_t to hold it together.  Return the pointer to the vsk_anchor_t
 662  * to the caller.  This must only be called from a mount.
 663  */
 664 vsk_anchor_t *
 665 get_vskstat_anchor(vfs_t *vfsp)
 666 {
 667         char            kstatstr[KSTAT_STRLEN]; /* kstat name for vopstats */
 668         statvfs64_t     statvfsbuf;             /* Needed to find f_fsid */
 669         vsk_anchor_t    *vskp = NULL;           /* vfs <--> kstat anchor */
 670         kstat_t         *ksp;                   /* Ptr to new kstat */
 671         avl_index_t     where;                  /* Location in the AVL tree */
 672 
 673         if (vfsp == NULL || vfsp->vfs_implp == NULL ||
 674             (vfsp->vfs_flag & VFS_STATS) == 0 || !vopstats_enabled)
 675                 return (NULL);
 676 
 677         /* Need to get the fsid to build a kstat name */
 678         if (VFS_STATVFS(vfsp, &statvfsbuf) == 0) {
 679                 /* Create a name for our kstats based on fsid */
 680                 (void) snprintf(kstatstr, KSTAT_STRLEN, "%s%lx",
 681                     VOPSTATS_STR, statvfsbuf.f_fsid);
 682 
 683                 /* Allocate and initialize the vsk_anchor_t */
 684                 vskp = kmem_cache_alloc(vsk_anchor_cache, KM_SLEEP);
 685                 bzero(vskp, sizeof (*vskp));
 686                 vskp->vsk_fsid = statvfsbuf.f_fsid;
 687 
 688                 mutex_enter(&vskstat_tree_lock);
 689                 if (avl_find(&vskstat_tree, vskp, &where) == NULL) {
 690                         avl_insert(&vskstat_tree, vskp, where);
 691                         mutex_exit(&vskstat_tree_lock);
 692 
 693                         /*
 694                          * Now that we've got the anchor in the AVL
 695                          * tree, we can create the kstat.
 696                          */
 697                         ksp = new_vskstat(kstatstr, &vfsp->vfs_vopstats);
 698                         if (ksp) {
 699                                 vskp->vsk_ksp = ksp;
 700                         }
 701                 } else {
 702                         /* Oops, found one! Release memory and lock. */
 703                         mutex_exit(&vskstat_tree_lock);
 704                         kmem_cache_free(vsk_anchor_cache, vskp);
 705                         vskp = NULL;
 706                 }
 707         }
 708         return (vskp);
 709 }
 710 
 711 /*
 712  * We're in the process of tearing down the vfs and need to cleanup
 713  * the data structures associated with the vopstats. Must only be called
 714  * from dounmount().
 715  */
 716 void
 717 teardown_vopstats(vfs_t *vfsp)
 718 {
 719         vsk_anchor_t    *vskap;
 720         avl_index_t     where;
 721 
 722         if (vfsp == NULL || vfsp->vfs_implp == NULL ||
 723             (vfsp->vfs_flag & VFS_STATS) == 0 || !vopstats_enabled)
 724                 return;
 725 
 726         /* This is a safe check since VFS_STATS must be set (see above) */
 727         if ((vskap = vfsp->vfs_vskap) == NULL)
 728                 return;
 729 
 730         /* Whack the pointer right away */
 731         vfsp->vfs_vskap = NULL;
 732 
 733         /* Lock the tree, remove the node, and delete the kstat */
 734         mutex_enter(&vskstat_tree_lock);
 735         if (avl_find(&vskstat_tree, vskap, &where)) {
 736                 avl_remove(&vskstat_tree, vskap);
 737         }
 738 
 739         if (vskap->vsk_ksp) {
 740                 kstat_delete(vskap->vsk_ksp);
 741         }
 742         mutex_exit(&vskstat_tree_lock);
 743 
 744         kmem_cache_free(vsk_anchor_cache, vskap);
 745 }
 746 
 747 /*
 748  * Read or write a vnode.  Called from kernel code.
 749  */
 750 int
 751 vn_rdwr(
 752         enum uio_rw rw,
 753         struct vnode *vp,
 754         caddr_t base,
 755         ssize_t len,
 756         offset_t offset,
 757         enum uio_seg seg,
 758         int ioflag,
 759         rlim64_t ulimit,        /* meaningful only if rw is UIO_WRITE */
 760         cred_t *cr,
 761         ssize_t *residp)
 762 {
 763         struct uio uio;
 764         struct iovec iov;
 765         int error;
 766         int in_crit = 0;
 767 
 768         if (rw == UIO_WRITE && ISROFILE(vp))
 769                 return (EROFS);
 770 
 771         if (len < 0)
 772                 return (EIO);
 773 
 774         VOPXID_MAP_CR(vp, cr);
 775 
 776         iov.iov_base = base;
 777         iov.iov_len = len;
 778         uio.uio_iov = &iov;
 779         uio.uio_iovcnt = 1;
 780         uio.uio_loffset = offset;
 781         uio.uio_segflg = (short)seg;
 782         uio.uio_resid = len;
 783         uio.uio_llimit = ulimit;
 784 
 785         /*
 786          * We have to enter the critical region before calling VOP_RWLOCK
 787          * to avoid a deadlock with ufs.
 788          */
 789         if (nbl_need_check(vp)) {
 790                 int svmand;
 791 
 792                 nbl_start_crit(vp, RW_READER);
 793                 in_crit = 1;
 794                 error = nbl_svmand(vp, cr, &svmand);
 795                 if (error != 0)
 796                         goto done;
 797                 if (nbl_conflict(vp, rw == UIO_WRITE ? NBL_WRITE : NBL_READ,
 798                     uio.uio_offset, uio.uio_resid, svmand, NULL)) {
 799                         error = EACCES;
 800                         goto done;
 801                 }
 802         }
 803 
 804         (void) VOP_RWLOCK(vp,
 805             rw == UIO_WRITE ? V_WRITELOCK_TRUE : V_WRITELOCK_FALSE, NULL);
 806         if (rw == UIO_WRITE) {
 807                 uio.uio_fmode = FWRITE;
 808                 uio.uio_extflg = UIO_COPY_DEFAULT;
 809                 error = VOP_WRITE(vp, &uio, ioflag, cr, NULL);
 810         } else {
 811                 uio.uio_fmode = FREAD;
 812                 uio.uio_extflg = UIO_COPY_CACHED;
 813                 error = VOP_READ(vp, &uio, ioflag, cr, NULL);
 814         }
 815         VOP_RWUNLOCK(vp,
 816             rw == UIO_WRITE ? V_WRITELOCK_TRUE : V_WRITELOCK_FALSE, NULL);
 817         if (residp)
 818                 *residp = uio.uio_resid;
 819         else if (uio.uio_resid)
 820                 error = EIO;
 821 
 822 done:
 823         if (in_crit)
 824                 nbl_end_crit(vp);
 825         return (error);
 826 }
 827 
 828 /*
 829  * Release a vnode.  Call VOP_INACTIVE on last reference or
 830  * decrement reference count.
 831  *
 832  * To avoid race conditions, the v_count is left at 1 for
 833  * the call to VOP_INACTIVE. This prevents another thread
 834  * from reclaiming and releasing the vnode *before* the
 835  * VOP_INACTIVE routine has a chance to destroy the vnode.
 836  * We can't have more than 1 thread calling VOP_INACTIVE
 837  * on a vnode.
 838  */
 839 void
 840 vn_rele(vnode_t *vp)
 841 {
 842         VERIFY(vp->v_count > 0);
 843         mutex_enter(&vp->v_lock);
 844         if (vp->v_count == 1) {
 845                 mutex_exit(&vp->v_lock);
 846                 VOP_INACTIVE(vp, CRED(), NULL);
 847                 return;
 848         }
 849         vp->v_count--;
 850         mutex_exit(&vp->v_lock);
 851 }
 852 
 853 /*
 854  * Release a vnode referenced by the DNLC. Multiple DNLC references are treated
 855  * as a single reference, so v_count is not decremented until the last DNLC hold
 856  * is released. This makes it possible to distinguish vnodes that are referenced
 857  * only by the DNLC.
 858  */
 859 void
 860 vn_rele_dnlc(vnode_t *vp)
 861 {
 862         VERIFY((vp->v_count > 0) && (vp->v_count_dnlc > 0));
 863         mutex_enter(&vp->v_lock);
 864         if (--vp->v_count_dnlc == 0) {
 865                 if (vp->v_count == 1) {
 866                         mutex_exit(&vp->v_lock);
 867                         VOP_INACTIVE(vp, CRED(), NULL);
 868                         return;
 869                 }
 870                 vp->v_count--;
 871         }
 872         mutex_exit(&vp->v_lock);
 873 }
 874 
 875 /*
 876  * Like vn_rele() except that it clears v_stream under v_lock.
 877  * This is used by sockfs when it dismantels the association between
 878  * the sockfs node and the vnode in the underlaying file system.
 879  * v_lock has to be held to prevent a thread coming through the lookupname
 880  * path from accessing a stream head that is going away.
 881  */
 882 void
 883 vn_rele_stream(vnode_t *vp)
 884 {
 885         VERIFY(vp->v_count > 0);
 886         mutex_enter(&vp->v_lock);
 887         vp->v_stream = NULL;
 888         if (vp->v_count == 1) {
 889                 mutex_exit(&vp->v_lock);
 890                 VOP_INACTIVE(vp, CRED(), NULL);
 891                 return;
 892         }
 893         vp->v_count--;
 894         mutex_exit(&vp->v_lock);
 895 }
 896 
 897 static void
 898 vn_rele_inactive(vnode_t *vp)
 899 {
 900         VOP_INACTIVE(vp, CRED(), NULL);
 901 }
 902 
 903 /*
 904  * Like vn_rele() except if we are going to call VOP_INACTIVE() then do it
 905  * asynchronously using a taskq. This can avoid deadlocks caused by re-entering
 906  * the file system as a result of releasing the vnode. Note, file systems
 907  * already have to handle the race where the vnode is incremented before the
 908  * inactive routine is called and does its locking.
 909  *
 910  * Warning: Excessive use of this routine can lead to performance problems.
 911  * This is because taskqs throttle back allocation if too many are created.
 912  */
 913 void
 914 vn_rele_async(vnode_t *vp, taskq_t *taskq)
 915 {
 916         VERIFY(vp->v_count > 0);
 917         mutex_enter(&vp->v_lock);
 918         if (vp->v_count == 1) {
 919                 mutex_exit(&vp->v_lock);
 920                 VERIFY(taskq_dispatch(taskq, (task_func_t *)vn_rele_inactive,
 921                     vp, TQ_SLEEP) != NULL);
 922                 return;
 923         }
 924         vp->v_count--;
 925         mutex_exit(&vp->v_lock);
 926 }
 927 
 928 int
 929 vn_open(
 930         char *pnamep,
 931         enum uio_seg seg,
 932         int filemode,
 933         int createmode,
 934         struct vnode **vpp,
 935         enum create crwhy,
 936         mode_t umask)
 937 {
 938         return (vn_openat(pnamep, seg, filemode, createmode, vpp, crwhy,
 939             umask, NULL, -1));
 940 }
 941 
 942 
 943 /*
 944  * Open/create a vnode.
 945  * This may be callable by the kernel, the only known use
 946  * of user context being that the current user credentials
 947  * are used for permissions.  crwhy is defined iff filemode & FCREAT.
 948  */
 949 int
 950 vn_openat(
 951         char *pnamep,
 952         enum uio_seg seg,
 953         int filemode,
 954         int createmode,
 955         struct vnode **vpp,
 956         enum create crwhy,
 957         mode_t umask,
 958         struct vnode *startvp,
 959         int fd)
 960 {
 961         struct vnode *vp;
 962         int mode;
 963         int accessflags;
 964         int error;
 965         int in_crit = 0;
 966         int open_done = 0;
 967         int shrlock_done = 0;
 968         struct vattr vattr;
 969         enum symfollow follow;
 970         int estale_retry = 0;
 971         struct shrlock shr;
 972         struct shr_locowner shr_own;
 973 
 974         mode = 0;
 975         accessflags = 0;
 976         if (filemode & FREAD)
 977                 mode |= VREAD;
 978         if (filemode & (FWRITE|FTRUNC))
 979                 mode |= VWRITE;
 980         if (filemode & (FSEARCH|FEXEC|FXATTRDIROPEN))
 981                 mode |= VEXEC;
 982 
 983         /* symlink interpretation */
 984         if (filemode & FNOFOLLOW)
 985                 follow = NO_FOLLOW;
 986         else
 987                 follow = FOLLOW;
 988 
 989         if (filemode & FAPPEND)
 990                 accessflags |= V_APPEND;
 991 
 992 top:
 993         if (filemode & FCREAT) {
 994                 enum vcexcl excl;
 995 
 996                 /*
 997                  * Wish to create a file.
 998                  */
 999                 vattr.va_type = VREG;
1000                 vattr.va_mode = createmode;
1001                 vattr.va_mask = AT_TYPE|AT_MODE;
1002                 if (filemode & FTRUNC) {
1003                         vattr.va_size = 0;
1004                         vattr.va_mask |= AT_SIZE;
1005                 }
1006                 if (filemode & FEXCL)
1007                         excl = EXCL;
1008                 else
1009                         excl = NONEXCL;
1010 
1011                 if (error =
1012                     vn_createat(pnamep, seg, &vattr, excl, mode, &vp, crwhy,
1013                     (filemode & ~(FTRUNC|FEXCL)), umask, startvp))
1014                         return (error);
1015         } else {
1016                 /*
1017                  * Wish to open a file.  Just look it up.
1018                  */
1019                 if (error = lookupnameat(pnamep, seg, follow,
1020                     NULLVPP, &vp, startvp)) {
1021                         if ((error == ESTALE) &&
1022                             fs_need_estale_retry(estale_retry++))
1023                                 goto top;
1024                         return (error);
1025                 }
1026 
1027                 /*
1028                  * Get the attributes to check whether file is large.
1029                  * We do this only if the FOFFMAX flag is not set and
1030                  * only for regular files.
1031                  */
1032 
1033                 if (!(filemode & FOFFMAX) && (vp->v_type == VREG)) {
1034                         vattr.va_mask = AT_SIZE;
1035                         if ((error = VOP_GETATTR(vp, &vattr, 0,
1036                             CRED(), NULL))) {
1037                                 goto out;
1038                         }
1039                         if (vattr.va_size > (u_offset_t)MAXOFF32_T) {
1040                                 /*
1041                                  * Large File API - regular open fails
1042                                  * if FOFFMAX flag is set in file mode
1043                                  */
1044                                 error = EOVERFLOW;
1045                                 goto out;
1046                         }
1047                 }
1048                 /*
1049                  * Can't write directories, active texts, or
1050                  * read-only filesystems.  Can't truncate files
1051                  * on which mandatory locking is in effect.
1052                  */
1053                 if (filemode & (FWRITE|FTRUNC)) {
1054                         /*
1055                          * Allow writable directory if VDIROPEN flag is set.
1056                          */
1057                         if (vp->v_type == VDIR && !(vp->v_flag & VDIROPEN)) {
1058                                 error = EISDIR;
1059                                 goto out;
1060                         }
1061                         if (ISROFILE(vp)) {
1062                                 error = EROFS;
1063                                 goto out;
1064                         }
1065                         /*
1066                          * Can't truncate files on which
1067                          * sysv mandatory locking is in effect.
1068                          */
1069                         if (filemode & FTRUNC) {
1070                                 vnode_t *rvp;
1071 
1072                                 if (VOP_REALVP(vp, &rvp, NULL) != 0)
1073                                         rvp = vp;
1074                                 if (rvp->v_filocks != NULL) {
1075                                         vattr.va_mask = AT_MODE;
1076                                         if ((error = VOP_GETATTR(vp,
1077                                             &vattr, 0, CRED(), NULL)) == 0 &&
1078                                             MANDLOCK(vp, vattr.va_mode))
1079                                                 error = EAGAIN;
1080                                 }
1081                         }
1082                         if (error)
1083                                 goto out;
1084                 }
1085                 /*
1086                  * Check permissions.
1087                  */
1088                 if (error = VOP_ACCESS(vp, mode, accessflags, CRED(), NULL))
1089                         goto out;
1090                 /*
1091                  * Require FSEARCH to return a directory.
1092                  * Require FEXEC to return a regular file.
1093                  */
1094                 if ((filemode & FSEARCH) && vp->v_type != VDIR) {
1095                         error = ENOTDIR;
1096                         goto out;
1097                 }
1098                 if ((filemode & FEXEC) && vp->v_type != VREG) {
1099                         error = ENOEXEC;        /* XXX: error code? */
1100                         goto out;
1101                 }
1102         }
1103 
1104         /*
1105          * Do remaining checks for FNOFOLLOW and FNOLINKS.
1106          */
1107         if ((filemode & FNOFOLLOW) && vp->v_type == VLNK) {
1108                 error = ELOOP;
1109                 goto out;
1110         }
1111         if (filemode & FNOLINKS) {
1112                 vattr.va_mask = AT_NLINK;
1113                 if ((error = VOP_GETATTR(vp, &vattr, 0, CRED(), NULL))) {
1114                         goto out;
1115                 }
1116                 if (vattr.va_nlink != 1) {
1117                         error = EMLINK;
1118                         goto out;
1119                 }
1120         }
1121 
1122         /*
1123          * Opening a socket corresponding to the AF_UNIX pathname
1124          * in the filesystem name space is not supported.
1125          * However, VSOCK nodes in namefs are supported in order
1126          * to make fattach work for sockets.
1127          *
1128          * XXX This uses VOP_REALVP to distinguish between
1129          * an unopened namefs node (where VOP_REALVP returns a
1130          * different VSOCK vnode) and a VSOCK created by vn_create
1131          * in some file system (where VOP_REALVP would never return
1132          * a different vnode).
1133          */
1134         if (vp->v_type == VSOCK) {
1135                 struct vnode *nvp;
1136 
1137                 error = VOP_REALVP(vp, &nvp, NULL);
1138                 if (error != 0 || nvp == NULL || nvp == vp ||
1139                     nvp->v_type != VSOCK) {
1140                         error = EOPNOTSUPP;
1141                         goto out;
1142                 }
1143         }
1144 
1145         if ((vp->v_type == VREG) && nbl_need_check(vp)) {
1146                 /* get share reservation */
1147                 shr.s_access = 0;
1148                 if (filemode & FWRITE)
1149                         shr.s_access |= F_WRACC;
1150                 if (filemode & FREAD)
1151                         shr.s_access |= F_RDACC;
1152                 shr.s_deny = 0;
1153                 shr.s_sysid = 0;
1154                 shr.s_pid = ttoproc(curthread)->p_pid;
1155                 shr_own.sl_pid = shr.s_pid;
1156                 shr_own.sl_id = fd;
1157                 shr.s_own_len = sizeof (shr_own);
1158                 shr.s_owner = (caddr_t)&shr_own;
1159                 error = VOP_SHRLOCK(vp, F_SHARE_NBMAND, &shr, filemode, CRED(),
1160                     NULL);
1161                 if (error)
1162                         goto out;
1163                 shrlock_done = 1;
1164 
1165                 /* nbmand conflict check if truncating file */
1166                 if ((filemode & FTRUNC) && !(filemode & FCREAT)) {
1167                         nbl_start_crit(vp, RW_READER);
1168                         in_crit = 1;
1169 
1170                         vattr.va_mask = AT_SIZE;
1171                         if (error = VOP_GETATTR(vp, &vattr, 0, CRED(), NULL))
1172                                 goto out;
1173                         if (nbl_conflict(vp, NBL_WRITE, 0, vattr.va_size, 0,
1174                             NULL)) {
1175                                 error = EACCES;
1176                                 goto out;
1177                         }
1178                 }
1179         }
1180 
1181         /*
1182          * Do opening protocol.
1183          */
1184         error = VOP_OPEN(&vp, filemode, CRED(), NULL);
1185         if (error)
1186                 goto out;
1187         open_done = 1;
1188 
1189         /*
1190          * Truncate if required.
1191          */
1192         if ((filemode & FTRUNC) && !(filemode & FCREAT)) {
1193                 vattr.va_size = 0;
1194                 vattr.va_mask = AT_SIZE;
1195                 if ((error = VOP_SETATTR(vp, &vattr, 0, CRED(), NULL)) != 0)
1196                         goto out;
1197         }
1198 out:
1199         ASSERT(vp->v_count > 0);
1200 
1201         if (in_crit) {
1202                 nbl_end_crit(vp);
1203                 in_crit = 0;
1204         }
1205         if (error) {
1206                 if (open_done) {
1207                         (void) VOP_CLOSE(vp, filemode, 1, (offset_t)0, CRED(),
1208                             NULL);
1209                         open_done = 0;
1210                         shrlock_done = 0;
1211                 }
1212                 if (shrlock_done) {
1213                         (void) VOP_SHRLOCK(vp, F_UNSHARE, &shr, 0, CRED(),
1214                             NULL);
1215                         shrlock_done = 0;
1216                 }
1217 
1218                 /*
1219                  * The following clause was added to handle a problem
1220                  * with NFS consistency.  It is possible that a lookup
1221                  * of the file to be opened succeeded, but the file
1222                  * itself doesn't actually exist on the server.  This
1223                  * is chiefly due to the DNLC containing an entry for
1224                  * the file which has been removed on the server.  In
1225                  * this case, we just start over.  If there was some
1226                  * other cause for the ESTALE error, then the lookup
1227                  * of the file will fail and the error will be returned
1228                  * above instead of looping around from here.
1229                  */
1230                 VN_RELE(vp);
1231                 if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
1232                         goto top;
1233         } else
1234                 *vpp = vp;
1235         return (error);
1236 }
1237 
1238 /*
1239  * The following two accessor functions are for the NFSv4 server.  Since there
1240  * is no VOP_OPEN_UP/DOWNGRADE we need a way for the NFS server to keep the
1241  * vnode open counts correct when a client "upgrades" an open or does an
1242  * open_downgrade.  In NFS, an upgrade or downgrade can not only change the
1243  * open mode (add or subtract read or write), but also change the share/deny
1244  * modes.  However, share reservations are not integrated with OPEN, yet, so
1245  * we need to handle each separately.  These functions are cleaner than having
1246  * the NFS server manipulate the counts directly, however, nobody else should
1247  * use these functions.
1248  */
1249 void
1250 vn_open_upgrade(
1251         vnode_t *vp,
1252         int filemode)
1253 {
1254         ASSERT(vp->v_type == VREG);
1255 
1256         if (filemode & FREAD)
1257                 atomic_inc_32(&vp->v_rdcnt);
1258         if (filemode & FWRITE)
1259                 atomic_inc_32(&vp->v_wrcnt);
1260 
1261 }
1262 
1263 void
1264 vn_open_downgrade(
1265         vnode_t *vp,
1266         int filemode)
1267 {
1268         ASSERT(vp->v_type == VREG);
1269 
1270         if (filemode & FREAD) {
1271                 ASSERT(vp->v_rdcnt > 0);
1272                 atomic_dec_32(&vp->v_rdcnt);
1273         }
1274         if (filemode & FWRITE) {
1275                 ASSERT(vp->v_wrcnt > 0);
1276                 atomic_dec_32(&vp->v_wrcnt);
1277         }
1278 
1279 }
1280 
1281 int
1282 vn_create(
1283         char *pnamep,
1284         enum uio_seg seg,
1285         struct vattr *vap,
1286         enum vcexcl excl,
1287         int mode,
1288         struct vnode **vpp,
1289         enum create why,
1290         int flag,
1291         mode_t umask)
1292 {
1293         return (vn_createat(pnamep, seg, vap, excl, mode, vpp, why, flag,
1294             umask, NULL));
1295 }
1296 
1297 /*
1298  * Create a vnode (makenode).
1299  */
1300 int
1301 vn_createat(
1302         char *pnamep,
1303         enum uio_seg seg,
1304         struct vattr *vap,
1305         enum vcexcl excl,
1306         int mode,
1307         struct vnode **vpp,
1308         enum create why,
1309         int flag,
1310         mode_t umask,
1311         struct vnode *startvp)
1312 {
1313         struct vnode *dvp;      /* ptr to parent dir vnode */
1314         struct vnode *vp = NULL;
1315         struct pathname pn;
1316         int error;
1317         int in_crit = 0;
1318         struct vattr vattr;
1319         enum symfollow follow;
1320         int estale_retry = 0;
1321         uint32_t auditing = AU_AUDITING();
1322 
1323         ASSERT((vap->va_mask & (AT_TYPE|AT_MODE)) == (AT_TYPE|AT_MODE));
1324 
1325         /* symlink interpretation */
1326         if ((flag & FNOFOLLOW) || excl == EXCL)
1327                 follow = NO_FOLLOW;
1328         else
1329                 follow = FOLLOW;
1330         flag &= ~(FNOFOLLOW|FNOLINKS);
1331 
1332 top:
1333         /*
1334          * Lookup directory.
1335          * If new object is a file, call lower level to create it.
1336          * Note that it is up to the lower level to enforce exclusive
1337          * creation, if the file is already there.
1338          * This allows the lower level to do whatever
1339          * locking or protocol that is needed to prevent races.
1340          * If the new object is directory call lower level to make
1341          * the new directory, with "." and "..".
1342          */
1343         if (error = pn_get(pnamep, seg, &pn))
1344                 return (error);
1345         if (auditing)
1346                 audit_vncreate_start();
1347         dvp = NULL;
1348         *vpp = NULL;
1349         /*
1350          * lookup will find the parent directory for the vnode.
1351          * When it is done the pn holds the name of the entry
1352          * in the directory.
1353          * If this is a non-exclusive create we also find the node itself.
1354          */
1355         error = lookuppnat(&pn, NULL, follow, &dvp,
1356             (excl == EXCL) ? NULLVPP : vpp, startvp);
1357         if (error) {
1358                 pn_free(&pn);
1359                 if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
1360                         goto top;
1361                 if (why == CRMKDIR && error == EINVAL)
1362                         error = EEXIST;         /* SVID */
1363                 return (error);
1364         }
1365 
1366         if (why != CRMKNOD)
1367                 vap->va_mode &= ~VSVTX;
1368 
1369         /*
1370          * If default ACLs are defined for the directory don't apply the
1371          * umask if umask is passed.
1372          */
1373 
1374         if (umask) {
1375 
1376                 vsecattr_t vsec;
1377 
1378                 vsec.vsa_aclcnt = 0;
1379                 vsec.vsa_aclentp = NULL;
1380                 vsec.vsa_dfaclcnt = 0;
1381                 vsec.vsa_dfaclentp = NULL;
1382                 vsec.vsa_mask = VSA_DFACLCNT;
1383                 error = VOP_GETSECATTR(dvp, &vsec, 0, CRED(), NULL);
1384                 /*
1385                  * If error is ENOSYS then treat it as no error
1386                  * Don't want to force all file systems to support
1387                  * aclent_t style of ACL's.
1388                  */
1389                 if (error == ENOSYS)
1390                         error = 0;
1391                 if (error) {
1392                         if (*vpp != NULL)
1393                                 VN_RELE(*vpp);
1394                         goto out;
1395                 } else {
1396                         /*
1397                          * Apply the umask if no default ACLs.
1398                          */
1399                         if (vsec.vsa_dfaclcnt == 0)
1400                                 vap->va_mode &= ~umask;
1401 
1402                         /*
1403                          * VOP_GETSECATTR() may have allocated memory for
1404                          * ACLs we didn't request, so double-check and
1405                          * free it if necessary.
1406                          */
1407                         if (vsec.vsa_aclcnt && vsec.vsa_aclentp != NULL)
1408                                 kmem_free((caddr_t)vsec.vsa_aclentp,
1409                                     vsec.vsa_aclcnt * sizeof (aclent_t));
1410                         if (vsec.vsa_dfaclcnt && vsec.vsa_dfaclentp != NULL)
1411                                 kmem_free((caddr_t)vsec.vsa_dfaclentp,
1412                                     vsec.vsa_dfaclcnt * sizeof (aclent_t));
1413                 }
1414         }
1415 
1416         /*
1417          * In general we want to generate EROFS if the file system is
1418          * readonly.  However, POSIX (IEEE Std. 1003.1) section 5.3.1
1419          * documents the open system call, and it says that O_CREAT has no
1420          * effect if the file already exists.  Bug 1119649 states
1421          * that open(path, O_CREAT, ...) fails when attempting to open an
1422          * existing file on a read only file system.  Thus, the first part
1423          * of the following if statement has 3 checks:
1424          *      if the file exists &&
1425          *              it is being open with write access &&
1426          *              the file system is read only
1427          *      then generate EROFS
1428          */
1429         if ((*vpp != NULL && (mode & VWRITE) && ISROFILE(*vpp)) ||
1430             (*vpp == NULL && dvp->v_vfsp->vfs_flag & VFS_RDONLY)) {
1431                 if (*vpp)
1432                         VN_RELE(*vpp);
1433                 error = EROFS;
1434         } else if (excl == NONEXCL && *vpp != NULL) {
1435                 vnode_t *rvp;
1436 
1437                 /*
1438                  * File already exists.  If a mandatory lock has been
1439                  * applied, return error.
1440                  */
1441                 vp = *vpp;
1442                 if (VOP_REALVP(vp, &rvp, NULL) != 0)
1443                         rvp = vp;
1444                 if ((vap->va_mask & AT_SIZE) && nbl_need_check(vp)) {
1445                         nbl_start_crit(vp, RW_READER);
1446                         in_crit = 1;
1447                 }
1448                 if (rvp->v_filocks != NULL || rvp->v_shrlocks != NULL) {
1449                         vattr.va_mask = AT_MODE|AT_SIZE;
1450                         if (error = VOP_GETATTR(vp, &vattr, 0, CRED(), NULL)) {
1451                                 goto out;
1452                         }
1453                         if (MANDLOCK(vp, vattr.va_mode)) {
1454                                 error = EAGAIN;
1455                                 goto out;
1456                         }
1457                         /*
1458                          * File cannot be truncated if non-blocking mandatory
1459                          * locks are currently on the file.
1460                          */
1461                         if ((vap->va_mask & AT_SIZE) && in_crit) {
1462                                 u_offset_t offset;
1463                                 ssize_t length;
1464 
1465                                 offset = vap->va_size > vattr.va_size ?
1466                                     vattr.va_size : vap->va_size;
1467                                 length = vap->va_size > vattr.va_size ?
1468                                     vap->va_size - vattr.va_size :
1469                                     vattr.va_size - vap->va_size;
1470                                 if (nbl_conflict(vp, NBL_WRITE, offset,
1471                                     length, 0, NULL)) {
1472                                         error = EACCES;
1473                                         goto out;
1474                                 }
1475                         }
1476                 }
1477 
1478                 /*
1479                  * If the file is the root of a VFS, we've crossed a
1480                  * mount point and the "containing" directory that we
1481                  * acquired above (dvp) is irrelevant because it's in
1482                  * a different file system.  We apply VOP_CREATE to the
1483                  * target itself instead of to the containing directory
1484                  * and supply a null path name to indicate (conventionally)
1485                  * the node itself as the "component" of interest.
1486                  *
1487                  * The intercession of the file system is necessary to
1488                  * ensure that the appropriate permission checks are
1489                  * done.
1490                  */
1491                 if (vp->v_flag & VROOT) {
1492                         ASSERT(why != CRMKDIR);
1493                         error = VOP_CREATE(vp, "", vap, excl, mode, vpp,
1494                             CRED(), flag, NULL, NULL);
1495                         /*
1496                          * If the create succeeded, it will have created
1497                          * a new reference to the vnode.  Give up the
1498                          * original reference.  The assertion should not
1499                          * get triggered because NBMAND locks only apply to
1500                          * VREG files.  And if in_crit is non-zero for some
1501                          * reason, detect that here, rather than when we
1502                          * deference a null vp.
1503                          */
1504                         ASSERT(in_crit == 0);
1505                         VN_RELE(vp);
1506                         vp = NULL;
1507                         goto out;
1508                 }
1509 
1510                 /*
1511                  * Large File API - non-large open (FOFFMAX flag not set)
1512                  * of regular file fails if the file size exceeds MAXOFF32_T.
1513                  */
1514                 if (why != CRMKDIR &&
1515                     !(flag & FOFFMAX) &&
1516                     (vp->v_type == VREG)) {
1517                         vattr.va_mask = AT_SIZE;
1518                         if ((error = VOP_GETATTR(vp, &vattr, 0,
1519                             CRED(), NULL))) {
1520                                 goto out;
1521                         }
1522                         if ((vattr.va_size > (u_offset_t)MAXOFF32_T)) {
1523                                 error = EOVERFLOW;
1524                                 goto out;
1525                         }
1526                 }
1527         }
1528 
1529         if (error == 0) {
1530                 /*
1531                  * Call mkdir() if specified, otherwise create().
1532                  */
1533                 int must_be_dir = pn_fixslash(&pn); /* trailing '/'? */
1534 
1535                 if (why == CRMKDIR)
1536                         /*
1537                          * N.B., if vn_createat() ever requests
1538                          * case-insensitive behavior then it will need
1539                          * to be passed to VOP_MKDIR().  VOP_CREATE()
1540                          * will already get it via "flag"
1541                          */
1542                         error = VOP_MKDIR(dvp, pn.pn_path, vap, vpp, CRED(),
1543                             NULL, 0, NULL);
1544                 else if (!must_be_dir)
1545                         error = VOP_CREATE(dvp, pn.pn_path, vap,
1546                             excl, mode, vpp, CRED(), flag, NULL, NULL);
1547                 else
1548                         error = ENOTDIR;
1549         }
1550 
1551 out:
1552 
1553         if (auditing)
1554                 audit_vncreate_finish(*vpp, error);
1555         if (in_crit) {
1556                 nbl_end_crit(vp);
1557                 in_crit = 0;
1558         }
1559         if (vp != NULL) {
1560                 VN_RELE(vp);
1561                 vp = NULL;
1562         }
1563         pn_free(&pn);
1564         VN_RELE(dvp);
1565         /*
1566          * The following clause was added to handle a problem
1567          * with NFS consistency.  It is possible that a lookup
1568          * of the file to be created succeeded, but the file
1569          * itself doesn't actually exist on the server.  This
1570          * is chiefly due to the DNLC containing an entry for
1571          * the file which has been removed on the server.  In
1572          * this case, we just start over.  If there was some
1573          * other cause for the ESTALE error, then the lookup
1574          * of the file will fail and the error will be returned
1575          * above instead of looping around from here.
1576          */
1577         if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
1578                 goto top;
1579         return (error);
1580 }
1581 
1582 int
1583 vn_link(char *from, char *to, enum uio_seg seg)
1584 {
1585         return (vn_linkat(NULL, from, NO_FOLLOW, NULL, to, seg));
1586 }
1587 
1588 int
1589 vn_linkat(vnode_t *fstartvp, char *from, enum symfollow follow,
1590     vnode_t *tstartvp, char *to, enum uio_seg seg)
1591 {
1592         struct vnode *fvp;              /* from vnode ptr */
1593         struct vnode *tdvp;             /* to directory vnode ptr */
1594         struct pathname pn;
1595         int error;
1596         struct vattr vattr;
1597         dev_t fsid;
1598         int estale_retry = 0;
1599         uint32_t auditing = AU_AUDITING();
1600 
1601 top:
1602         fvp = tdvp = NULL;
1603         if (error = pn_get(to, seg, &pn))
1604                 return (error);
1605         if (auditing && fstartvp != NULL)
1606                 audit_setfsat_path(1);
1607         if (error = lookupnameat(from, seg, follow, NULLVPP, &fvp, fstartvp))
1608                 goto out;
1609         if (auditing && tstartvp != NULL)
1610                 audit_setfsat_path(3);
1611         if (error = lookuppnat(&pn, NULL, NO_FOLLOW, &tdvp, NULLVPP, tstartvp))
1612                 goto out;
1613         /*
1614          * Make sure both source vnode and target directory vnode are
1615          * in the same vfs and that it is writeable.
1616          */
1617         vattr.va_mask = AT_FSID;
1618         if (error = VOP_GETATTR(fvp, &vattr, 0, CRED(), NULL))
1619                 goto out;
1620         fsid = vattr.va_fsid;
1621         vattr.va_mask = AT_FSID;
1622         if (error = VOP_GETATTR(tdvp, &vattr, 0, CRED(), NULL))
1623                 goto out;
1624         if (fsid != vattr.va_fsid) {
1625                 error = EXDEV;
1626                 goto out;
1627         }
1628         if (tdvp->v_vfsp->vfs_flag & VFS_RDONLY) {
1629                 error = EROFS;
1630                 goto out;
1631         }
1632         /*
1633          * Do the link.
1634          */
1635         (void) pn_fixslash(&pn);
1636         error = VOP_LINK(tdvp, fvp, pn.pn_path, CRED(), NULL, 0);
1637 out:
1638         pn_free(&pn);
1639         if (fvp)
1640                 VN_RELE(fvp);
1641         if (tdvp)
1642                 VN_RELE(tdvp);
1643         if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
1644                 goto top;
1645         return (error);
1646 }
1647 
1648 int
1649 vn_rename(char *from, char *to, enum uio_seg seg)
1650 {
1651         return (vn_renameat(NULL, from, NULL, to, seg));
1652 }
1653 
1654 int
1655 vn_renameat(vnode_t *fdvp, char *fname, vnode_t *tdvp,
1656     char *tname, enum uio_seg seg)
1657 {
1658         int error;
1659         struct vattr vattr;
1660         struct pathname fpn;            /* from pathname */
1661         struct pathname tpn;            /* to pathname */
1662         dev_t fsid;
1663         int in_crit_src, in_crit_targ;
1664         vnode_t *fromvp, *fvp;
1665         vnode_t *tovp, *targvp;
1666         int estale_retry = 0;
1667         uint32_t auditing = AU_AUDITING();
1668 
1669 top:
1670         fvp = fromvp = tovp = targvp = NULL;
1671         in_crit_src = in_crit_targ = 0;
1672         /*
1673          * Get to and from pathnames.
1674          */
1675         if (error = pn_get(fname, seg, &fpn))
1676                 return (error);
1677         if (error = pn_get(tname, seg, &tpn)) {
1678                 pn_free(&fpn);
1679                 return (error);
1680         }
1681 
1682         /*
1683          * First we need to resolve the correct directories
1684          * The passed in directories may only be a starting point,
1685          * but we need the real directories the file(s) live in.
1686          * For example the fname may be something like usr/lib/sparc
1687          * and we were passed in the / directory, but we need to
1688          * use the lib directory for the rename.
1689          */
1690 
1691         if (auditing && fdvp != NULL)
1692                 audit_setfsat_path(1);
1693         /*
1694          * Lookup to and from directories.
1695          */
1696         if (error = lookuppnat(&fpn, NULL, NO_FOLLOW, &fromvp, &fvp, fdvp)) {
1697                 goto out;
1698         }
1699 
1700         /*
1701          * Make sure there is an entry.
1702          */
1703         if (fvp == NULL) {
1704                 error = ENOENT;
1705                 goto out;
1706         }
1707 
1708         if (auditing && tdvp != NULL)
1709                 audit_setfsat_path(3);
1710         if (error = lookuppnat(&tpn, NULL, NO_FOLLOW, &tovp, &targvp, tdvp)) {
1711                 goto out;
1712         }
1713 
1714         /*
1715          * Make sure both the from vnode directory and the to directory
1716          * are in the same vfs and the to directory is writable.
1717          * We check fsid's, not vfs pointers, so loopback fs works.
1718          */
1719         if (fromvp != tovp) {
1720                 vattr.va_mask = AT_FSID;
1721                 if (error = VOP_GETATTR(fromvp, &vattr, 0, CRED(), NULL))
1722                         goto out;
1723                 fsid = vattr.va_fsid;
1724                 vattr.va_mask = AT_FSID;
1725                 if (error = VOP_GETATTR(tovp, &vattr, 0, CRED(), NULL))
1726                         goto out;
1727                 if (fsid != vattr.va_fsid) {
1728                         error = EXDEV;
1729                         goto out;
1730                 }
1731         }
1732 
1733         if (tovp->v_vfsp->vfs_flag & VFS_RDONLY) {
1734                 error = EROFS;
1735                 goto out;
1736         }
1737 
1738         if (targvp && (fvp != targvp)) {
1739                 nbl_start_crit(targvp, RW_READER);
1740                 in_crit_targ = 1;
1741                 if (nbl_conflict(targvp, NBL_REMOVE, 0, 0, 0, NULL)) {
1742                         error = EACCES;
1743                         goto out;
1744                 }
1745         }
1746 
1747         if (nbl_need_check(fvp)) {
1748                 nbl_start_crit(fvp, RW_READER);
1749                 in_crit_src = 1;
1750                 if (nbl_conflict(fvp, NBL_RENAME, 0, 0, 0, NULL)) {
1751                         error = EACCES;
1752                         goto out;
1753                 }
1754         }
1755 
1756         /*
1757          * Do the rename.
1758          */
1759         (void) pn_fixslash(&tpn);
1760         error = VOP_RENAME(fromvp, fpn.pn_path, tovp, tpn.pn_path, CRED(),
1761             NULL, 0);
1762 
1763 out:
1764         pn_free(&fpn);
1765         pn_free(&tpn);
1766         if (in_crit_src)
1767                 nbl_end_crit(fvp);
1768         if (in_crit_targ)
1769                 nbl_end_crit(targvp);
1770         if (fromvp)
1771                 VN_RELE(fromvp);
1772         if (tovp)
1773                 VN_RELE(tovp);
1774         if (targvp)
1775                 VN_RELE(targvp);
1776         if (fvp)
1777                 VN_RELE(fvp);
1778         if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
1779                 goto top;
1780         return (error);
1781 }
1782 
1783 /*
1784  * Remove a file or directory.
1785  */
1786 int
1787 vn_remove(char *fnamep, enum uio_seg seg, enum rm dirflag)
1788 {
1789         return (vn_removeat(NULL, fnamep, seg, dirflag));
1790 }
1791 
1792 int
1793 vn_removeat(vnode_t *startvp, char *fnamep, enum uio_seg seg, enum rm dirflag)
1794 {
1795         struct vnode *vp;               /* entry vnode */
1796         struct vnode *dvp;              /* ptr to parent dir vnode */
1797         struct vnode *coveredvp;
1798         struct pathname pn;             /* name of entry */
1799         enum vtype vtype;
1800         int error;
1801         struct vfs *vfsp;
1802         struct vfs *dvfsp;      /* ptr to parent dir vfs */
1803         int in_crit = 0;
1804         int estale_retry = 0;
1805 
1806 top:
1807         if (error = pn_get(fnamep, seg, &pn))
1808                 return (error);
1809         dvp = vp = NULL;
1810         if (error = lookuppnat(&pn, NULL, NO_FOLLOW, &dvp, &vp, startvp)) {
1811                 pn_free(&pn);
1812                 if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
1813                         goto top;
1814                 return (error);
1815         }
1816 
1817         /*
1818          * Make sure there is an entry.
1819          */
1820         if (vp == NULL) {
1821                 error = ENOENT;
1822                 goto out;
1823         }
1824 
1825         vfsp = vp->v_vfsp;
1826         dvfsp = dvp->v_vfsp;
1827 
1828         /*
1829          * If the named file is the root of a mounted filesystem, fail,
1830          * unless it's marked unlinkable.  In that case, unmount the
1831          * filesystem and proceed to unlink the covered vnode.  (If the
1832          * covered vnode is a directory, use rmdir instead of unlink,
1833          * to avoid file system corruption.)
1834          */
1835         if (vp->v_flag & VROOT) {
1836                 if ((vfsp->vfs_flag & VFS_UNLINKABLE) == 0) {
1837                         error = EBUSY;
1838                         goto out;
1839                 }
1840 
1841                 /*
1842                  * Namefs specific code starts here.
1843                  */
1844 
1845                 if (dirflag == RMDIRECTORY) {
1846                         /*
1847                          * User called rmdir(2) on a file that has
1848                          * been namefs mounted on top of.  Since
1849                          * namefs doesn't allow directories to
1850                          * be mounted on other files we know
1851                          * vp is not of type VDIR so fail to operation.
1852                          */
1853                         error = ENOTDIR;
1854                         goto out;
1855                 }
1856 
1857                 /*
1858                  * If VROOT is still set after grabbing vp->v_lock,
1859                  * noone has finished nm_unmount so far and coveredvp
1860                  * is valid.
1861                  * If we manage to grab vn_vfswlock(coveredvp) before releasing
1862                  * vp->v_lock, any race window is eliminated.
1863                  */
1864 
1865                 mutex_enter(&vp->v_lock);
1866                 if ((vp->v_flag & VROOT) == 0) {
1867                         /* Someone beat us to the unmount */
1868                         mutex_exit(&vp->v_lock);
1869                         error = EBUSY;
1870                         goto out;
1871                 }
1872                 vfsp = vp->v_vfsp;
1873                 coveredvp = vfsp->vfs_vnodecovered;
1874                 ASSERT(coveredvp);
1875                 /*
1876                  * Note: Implementation of vn_vfswlock shows that ordering of
1877                  * v_lock / vn_vfswlock is not an issue here.
1878                  */
1879                 error = vn_vfswlock(coveredvp);
1880                 mutex_exit(&vp->v_lock);
1881 
1882                 if (error)
1883                         goto out;
1884 
1885                 VN_HOLD(coveredvp);
1886                 VN_RELE(vp);
1887                 error = dounmount(vfsp, 0, CRED());
1888 
1889                 /*
1890                  * Unmounted the namefs file system; now get
1891                  * the object it was mounted over.
1892                  */
1893                 vp = coveredvp;
1894                 /*
1895                  * If namefs was mounted over a directory, then
1896                  * we want to use rmdir() instead of unlink().
1897                  */
1898                 if (vp->v_type == VDIR)
1899                         dirflag = RMDIRECTORY;
1900 
1901                 if (error)
1902                         goto out;
1903         }
1904 
1905         /*
1906          * Make sure filesystem is writeable.
1907          * We check the parent directory's vfs in case this is an lofs vnode.
1908          */
1909         if (dvfsp && dvfsp->vfs_flag & VFS_RDONLY) {
1910                 error = EROFS;
1911                 goto out;
1912         }
1913 
1914         vtype = vp->v_type;
1915 
1916         /*
1917          * If there is the possibility of an nbmand share reservation, make
1918          * sure it's okay to remove the file.  Keep a reference to the
1919          * vnode, so that we can exit the nbl critical region after
1920          * calling VOP_REMOVE.
1921          * If there is no possibility of an nbmand share reservation,
1922          * release the vnode reference now.  Filesystems like NFS may
1923          * behave differently if there is an extra reference, so get rid of
1924          * this one.  Fortunately, we can't have nbmand mounts on NFS
1925          * filesystems.
1926          */
1927         if (nbl_need_check(vp)) {
1928                 nbl_start_crit(vp, RW_READER);
1929                 in_crit = 1;
1930                 if (nbl_conflict(vp, NBL_REMOVE, 0, 0, 0, NULL)) {
1931                         error = EACCES;
1932                         goto out;
1933                 }
1934         } else {
1935                 VN_RELE(vp);
1936                 vp = NULL;
1937         }
1938 
1939         if (dirflag == RMDIRECTORY) {
1940                 /*
1941                  * Caller is using rmdir(2), which can only be applied to
1942                  * directories.
1943                  */
1944                 if (vtype != VDIR) {
1945                         error = ENOTDIR;
1946                 } else {
1947                         vnode_t *cwd;
1948                         proc_t *pp = curproc;
1949 
1950                         mutex_enter(&pp->p_lock);
1951                         cwd = PTOU(pp)->u_cdir;
1952                         VN_HOLD(cwd);
1953                         mutex_exit(&pp->p_lock);
1954                         error = VOP_RMDIR(dvp, pn.pn_path, cwd, CRED(),
1955                             NULL, 0);
1956                         VN_RELE(cwd);
1957                 }
1958         } else {
1959                 /*
1960                  * Unlink(2) can be applied to anything.
1961                  */
1962                 error = VOP_REMOVE(dvp, pn.pn_path, CRED(), NULL, 0);
1963         }
1964 
1965 out:
1966         pn_free(&pn);
1967         if (in_crit) {
1968                 nbl_end_crit(vp);
1969                 in_crit = 0;
1970         }
1971         if (vp != NULL)
1972                 VN_RELE(vp);
1973         if (dvp != NULL)
1974                 VN_RELE(dvp);
1975         if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
1976                 goto top;
1977         return (error);
1978 }
1979 
1980 /*
1981  * Utility function to compare equality of vnodes.
1982  * Compare the underlying real vnodes, if there are underlying vnodes.
1983  * This is a more thorough comparison than the VN_CMP() macro provides.
1984  */
1985 int
1986 vn_compare(vnode_t *vp1, vnode_t *vp2)
1987 {
1988         vnode_t *realvp;
1989 
1990         if (vp1 != NULL && VOP_REALVP(vp1, &realvp, NULL) == 0)
1991                 vp1 = realvp;
1992         if (vp2 != NULL && VOP_REALVP(vp2, &realvp, NULL) == 0)
1993                 vp2 = realvp;
1994         return (VN_CMP(vp1, vp2));
1995 }
1996 
1997 /*
1998  * The number of locks to hash into.  This value must be a power
1999  * of 2 minus 1 and should probably also be prime.
2000  */
2001 #define NUM_BUCKETS     1023
2002 
2003 struct  vn_vfslocks_bucket {
2004         kmutex_t vb_lock;
2005         vn_vfslocks_entry_t *vb_list;
2006         char pad[64 - sizeof (kmutex_t) - sizeof (void *)];
2007 };
2008 
2009 /*
2010  * Total number of buckets will be NUM_BUCKETS + 1 .
2011  */
2012 
2013 #pragma align   64(vn_vfslocks_buckets)
2014 static  struct vn_vfslocks_bucket       vn_vfslocks_buckets[NUM_BUCKETS + 1];
2015 
2016 #define VN_VFSLOCKS_SHIFT       9
2017 
2018 #define VN_VFSLOCKS_HASH(vfsvpptr)      \
2019         ((((intptr_t)(vfsvpptr)) >> VN_VFSLOCKS_SHIFT) & NUM_BUCKETS)
2020 
2021 /*
2022  * vn_vfslocks_getlock() uses an HASH scheme to generate
2023  * rwstlock using vfs/vnode pointer passed to it.
2024  *
2025  * vn_vfslocks_rele() releases a reference in the
2026  * HASH table which allows the entry allocated by
2027  * vn_vfslocks_getlock() to be freed at a later
2028  * stage when the refcount drops to zero.
2029  */
2030 
2031 vn_vfslocks_entry_t *
2032 vn_vfslocks_getlock(void *vfsvpptr)
2033 {
2034         struct vn_vfslocks_bucket *bp;
2035         vn_vfslocks_entry_t *vep;
2036         vn_vfslocks_entry_t *tvep;
2037 
2038         ASSERT(vfsvpptr != NULL);
2039         bp = &vn_vfslocks_buckets[VN_VFSLOCKS_HASH(vfsvpptr)];
2040 
2041         mutex_enter(&bp->vb_lock);
2042         for (vep = bp->vb_list; vep != NULL; vep = vep->ve_next) {
2043                 if (vep->ve_vpvfs == vfsvpptr) {
2044                         vep->ve_refcnt++;
2045                         mutex_exit(&bp->vb_lock);
2046                         return (vep);
2047                 }
2048         }
2049         mutex_exit(&bp->vb_lock);
2050         vep = kmem_alloc(sizeof (*vep), KM_SLEEP);
2051         rwst_init(&vep->ve_lock, NULL, RW_DEFAULT, NULL);
2052         vep->ve_vpvfs = (char *)vfsvpptr;
2053         vep->ve_refcnt = 1;
2054         mutex_enter(&bp->vb_lock);
2055         for (tvep = bp->vb_list; tvep != NULL; tvep = tvep->ve_next) {
2056                 if (tvep->ve_vpvfs == vfsvpptr) {
2057                         tvep->ve_refcnt++;
2058                         mutex_exit(&bp->vb_lock);
2059 
2060                         /*
2061                          * There is already an entry in the hash
2062                          * destroy what we just allocated.
2063                          */
2064                         rwst_destroy(&vep->ve_lock);
2065                         kmem_free(vep, sizeof (*vep));
2066                         return (tvep);
2067                 }
2068         }
2069         vep->ve_next = bp->vb_list;
2070         bp->vb_list = vep;
2071         mutex_exit(&bp->vb_lock);
2072         return (vep);
2073 }
2074 
2075 void
2076 vn_vfslocks_rele(vn_vfslocks_entry_t *vepent)
2077 {
2078         struct vn_vfslocks_bucket *bp;
2079         vn_vfslocks_entry_t *vep;
2080         vn_vfslocks_entry_t *pvep;
2081 
2082         ASSERT(vepent != NULL);
2083         ASSERT(vepent->ve_vpvfs != NULL);
2084 
2085         bp = &vn_vfslocks_buckets[VN_VFSLOCKS_HASH(vepent->ve_vpvfs)];
2086 
2087         mutex_enter(&bp->vb_lock);
2088         vepent->ve_refcnt--;
2089 
2090         if ((int32_t)vepent->ve_refcnt < 0)
2091                 cmn_err(CE_PANIC, "vn_vfslocks_rele: refcount negative");
2092 
2093         if (vepent->ve_refcnt == 0) {
2094                 for (vep = bp->vb_list; vep != NULL; vep = vep->ve_next) {
2095                         if (vep->ve_vpvfs == vepent->ve_vpvfs) {
2096                                 if (bp->vb_list == vep)
2097                                         bp->vb_list = vep->ve_next;
2098                                 else {
2099                                         /* LINTED */
2100                                         pvep->ve_next = vep->ve_next;
2101                                 }
2102                                 mutex_exit(&bp->vb_lock);
2103                                 rwst_destroy(&vep->ve_lock);
2104                                 kmem_free(vep, sizeof (*vep));
2105                                 return;
2106                         }
2107                         pvep = vep;
2108                 }
2109                 cmn_err(CE_PANIC, "vn_vfslocks_rele: vp/vfs not found");
2110         }
2111         mutex_exit(&bp->vb_lock);
2112 }
2113 
2114 /*
2115  * vn_vfswlock_wait is used to implement a lock which is logically a writers
2116  * lock protecting the v_vfsmountedhere field.
2117  * vn_vfswlock_wait has been modified to be similar to vn_vfswlock,
2118  * except that it blocks to acquire the lock VVFSLOCK.
2119  *
2120  * traverse() and routines re-implementing part of traverse (e.g. autofs)
2121  * need to hold this lock. mount(), vn_rename(), vn_remove() and so on
2122  * need the non-blocking version of the writers lock i.e. vn_vfswlock
2123  */
2124 int
2125 vn_vfswlock_wait(vnode_t *vp)
2126 {
2127         int retval;
2128         vn_vfslocks_entry_t *vpvfsentry;
2129         ASSERT(vp != NULL);
2130 
2131         vpvfsentry = vn_vfslocks_getlock(vp);
2132         retval = rwst_enter_sig(&vpvfsentry->ve_lock, RW_WRITER);
2133 
2134         if (retval == EINTR) {
2135                 vn_vfslocks_rele(vpvfsentry);
2136                 return (EINTR);
2137         }
2138         return (retval);
2139 }
2140 
2141 int
2142 vn_vfsrlock_wait(vnode_t *vp)
2143 {
2144         int retval;
2145         vn_vfslocks_entry_t *vpvfsentry;
2146         ASSERT(vp != NULL);
2147 
2148         vpvfsentry = vn_vfslocks_getlock(vp);
2149         retval = rwst_enter_sig(&vpvfsentry->ve_lock, RW_READER);
2150 
2151         if (retval == EINTR) {
2152                 vn_vfslocks_rele(vpvfsentry);
2153                 return (EINTR);
2154         }
2155 
2156         return (retval);
2157 }
2158 
2159 
2160 /*
2161  * vn_vfswlock is used to implement a lock which is logically a writers lock
2162  * protecting the v_vfsmountedhere field.
2163  */
2164 int
2165 vn_vfswlock(vnode_t *vp)
2166 {
2167         vn_vfslocks_entry_t *vpvfsentry;
2168 
2169         /*
2170          * If vp is NULL then somebody is trying to lock the covered vnode
2171          * of /.  (vfs_vnodecovered is NULL for /).  This situation will
2172          * only happen when unmounting /.  Since that operation will fail
2173          * anyway, return EBUSY here instead of in VFS_UNMOUNT.
2174          */
2175         if (vp == NULL)
2176                 return (EBUSY);
2177 
2178         vpvfsentry = vn_vfslocks_getlock(vp);
2179 
2180         if (rwst_tryenter(&vpvfsentry->ve_lock, RW_WRITER))
2181                 return (0);
2182 
2183         vn_vfslocks_rele(vpvfsentry);
2184         return (EBUSY);
2185 }
2186 
2187 int
2188 vn_vfsrlock(vnode_t *vp)
2189 {
2190         vn_vfslocks_entry_t *vpvfsentry;
2191 
2192         /*
2193          * If vp is NULL then somebody is trying to lock the covered vnode
2194          * of /.  (vfs_vnodecovered is NULL for /).  This situation will
2195          * only happen when unmounting /.  Since that operation will fail
2196          * anyway, return EBUSY here instead of in VFS_UNMOUNT.
2197          */
2198         if (vp == NULL)
2199                 return (EBUSY);
2200 
2201         vpvfsentry = vn_vfslocks_getlock(vp);
2202 
2203         if (rwst_tryenter(&vpvfsentry->ve_lock, RW_READER))
2204                 return (0);
2205 
2206         vn_vfslocks_rele(vpvfsentry);
2207         return (EBUSY);
2208 }
2209 
2210 void
2211 vn_vfsunlock(vnode_t *vp)
2212 {
2213         vn_vfslocks_entry_t *vpvfsentry;
2214 
2215         /*
2216          * ve_refcnt needs to be decremented twice.
2217          * 1. To release refernce after a call to vn_vfslocks_getlock()
2218          * 2. To release the reference from the locking routines like
2219          *    vn_vfsrlock/vn_vfswlock etc,.
2220          */
2221         vpvfsentry = vn_vfslocks_getlock(vp);
2222         vn_vfslocks_rele(vpvfsentry);
2223 
2224         rwst_exit(&vpvfsentry->ve_lock);
2225         vn_vfslocks_rele(vpvfsentry);
2226 }
2227 
2228 int
2229 vn_vfswlock_held(vnode_t *vp)
2230 {
2231         int held;
2232         vn_vfslocks_entry_t *vpvfsentry;
2233 
2234         ASSERT(vp != NULL);
2235 
2236         vpvfsentry = vn_vfslocks_getlock(vp);
2237         held = rwst_lock_held(&vpvfsentry->ve_lock, RW_WRITER);
2238 
2239         vn_vfslocks_rele(vpvfsentry);
2240         return (held);
2241 }
2242 
2243 
2244 int
2245 vn_make_ops(
2246         const char *name,                       /* Name of file system */
2247         const fs_operation_def_t *templ,        /* Operation specification */
2248         vnodeops_t **actual)                    /* Return the vnodeops */
2249 {
2250         int unused_ops;
2251         int error;
2252 
2253         *actual = (vnodeops_t *)kmem_alloc(sizeof (vnodeops_t), KM_SLEEP);
2254 
2255         (*actual)->vnop_name = name;
2256 
2257         error = fs_build_vector(*actual, &unused_ops, vn_ops_table, templ);
2258         if (error) {
2259                 kmem_free(*actual, sizeof (vnodeops_t));
2260         }
2261 
2262 #if DEBUG
2263         if (unused_ops != 0)
2264                 cmn_err(CE_WARN, "vn_make_ops: %s: %d operations supplied "
2265                     "but not used", name, unused_ops);
2266 #endif
2267 
2268         return (error);
2269 }
2270 
2271 /*
2272  * Free the vnodeops created as a result of vn_make_ops()
2273  */
2274 void
2275 vn_freevnodeops(vnodeops_t *vnops)
2276 {
2277         kmem_free(vnops, sizeof (vnodeops_t));
2278 }
2279 
2280 /*
2281  * Vnode cache.
2282  */
2283 
2284 /* ARGSUSED */
2285 static int
2286 vn_cache_constructor(void *buf, void *cdrarg, int kmflags)
2287 {
2288         struct vnode *vp;
2289 
2290         vp = buf;
2291 
2292         mutex_init(&vp->v_lock, NULL, MUTEX_DEFAULT, NULL);
2293         mutex_init(&vp->v_vsd_lock, NULL, MUTEX_DEFAULT, NULL);
2294         cv_init(&vp->v_cv, NULL, CV_DEFAULT, NULL);
2295         rw_init(&vp->v_nbllock, NULL, RW_DEFAULT, NULL);
2296         vp->v_femhead = NULL;        /* Must be done before vn_reinit() */
2297         vp->v_path = vn_vpath_empty;
2298         vp->v_path_stamp = 0;
2299         vp->v_mpssdata = NULL;
2300         vp->v_vsd = NULL;
2301         vp->v_fopdata = NULL;
2302 
2303         return (0);
2304 }
2305 
2306 /* ARGSUSED */
2307 static void
2308 vn_cache_destructor(void *buf, void *cdrarg)
2309 {
2310         struct vnode *vp;
2311 
2312         vp = buf;
2313 
2314         rw_destroy(&vp->v_nbllock);
2315         cv_destroy(&vp->v_cv);
2316         mutex_destroy(&vp->v_vsd_lock);
2317         mutex_destroy(&vp->v_lock);
2318 }
2319 
2320 void
2321 vn_create_cache(void)
2322 {
2323         /* LINTED */
2324         ASSERT((1 << VNODE_ALIGN_LOG2) ==
2325             P2ROUNDUP(sizeof (struct vnode), VNODE_ALIGN));
2326         vn_cache = kmem_cache_create("vn_cache", sizeof (struct vnode),
2327             VNODE_ALIGN, vn_cache_constructor, vn_cache_destructor, NULL, NULL,
2328             NULL, 0);
2329 }
2330 
2331 void
2332 vn_destroy_cache(void)
2333 {
2334         kmem_cache_destroy(vn_cache);
2335 }
2336 
2337 /*
2338  * Used by file systems when fs-specific nodes (e.g., ufs inodes) are
2339  * cached by the file system and vnodes remain associated.
2340  */
2341 void
2342 vn_recycle(vnode_t *vp)
2343 {
2344         ASSERT(vp->v_pages == NULL);
2345         VERIFY(vp->v_path != NULL);
2346 
2347         /*
2348          * XXX - This really belongs in vn_reinit(), but we have some issues
2349          * with the counts.  Best to have it here for clean initialization.
2350          */
2351         vp->v_rdcnt = 0;
2352         vp->v_wrcnt = 0;
2353         vp->v_mmap_read = 0;
2354         vp->v_mmap_write = 0;
2355 
2356         /*
2357          * If FEM was in use, make sure everything gets cleaned up
2358          * NOTE: vp->v_femhead is initialized to NULL in the vnode
2359          * constructor.
2360          */
2361         if (vp->v_femhead) {
2362                 /* XXX - There should be a free_femhead() that does all this */
2363                 ASSERT(vp->v_femhead->femh_list == NULL);
2364                 mutex_destroy(&vp->v_femhead->femh_lock);
2365                 kmem_free(vp->v_femhead, sizeof (*(vp->v_femhead)));
2366                 vp->v_femhead = NULL;
2367         }
2368         if (vp->v_path != vn_vpath_empty) {
2369                 kmem_free(vp->v_path, strlen(vp->v_path) + 1);
2370                 vp->v_path = vn_vpath_empty;
2371         }
2372         vp->v_path_stamp = 0;
2373 
2374         if (vp->v_fopdata != NULL) {
2375                 free_fopdata(vp);
2376         }
2377         vp->v_mpssdata = NULL;
2378         vsd_free(vp);
2379 }
2380 
2381 /*
2382  * Used to reset the vnode fields including those that are directly accessible
2383  * as well as those which require an accessor function.
2384  *
2385  * Does not initialize:
2386  *      synchronization objects: v_lock, v_vsd_lock, v_nbllock, v_cv
2387  *      v_data (since FS-nodes and vnodes point to each other and should
2388  *              be updated simultaneously)
2389  *      v_op (in case someone needs to make a VOP call on this object)
2390  */
2391 void
2392 vn_reinit(vnode_t *vp)
2393 {
2394         vp->v_count = 1;
2395         vp->v_count_dnlc = 0;
2396         vp->v_vfsp = NULL;
2397         vp->v_stream = NULL;
2398         vp->v_vfsmountedhere = NULL;
2399         vp->v_flag = 0;
2400         vp->v_type = VNON;
2401         vp->v_rdev = NODEV;
2402 
2403         vp->v_filocks = NULL;
2404         vp->v_shrlocks = NULL;
2405         vp->v_pages = NULL;
2406 
2407         vp->v_locality = NULL;
2408         vp->v_xattrdir = NULL;
2409 
2410         /* Handles v_femhead, v_path, and the r/w/map counts */
2411         vn_recycle(vp);
2412 }
2413 
2414 vnode_t *
2415 vn_alloc(int kmflag)
2416 {
2417         vnode_t *vp;
2418 
2419         vp = kmem_cache_alloc(vn_cache, kmflag);
2420 
2421         if (vp != NULL) {
2422                 vp->v_femhead = NULL;        /* Must be done before vn_reinit() */
2423                 vp->v_fopdata = NULL;
2424                 vn_reinit(vp);
2425         }
2426 
2427         return (vp);
2428 }
2429 
2430 void
2431 vn_free(vnode_t *vp)
2432 {
2433         ASSERT(vp->v_shrlocks == NULL);
2434         ASSERT(vp->v_filocks == NULL);
2435 
2436         /*
2437          * Some file systems call vn_free() with v_count of zero,
2438          * some with v_count of 1.  In any case, the value should
2439          * never be anything else.
2440          */
2441         ASSERT((vp->v_count == 0) || (vp->v_count == 1));
2442         ASSERT(vp->v_count_dnlc == 0);
2443         VERIFY(vp->v_path != NULL);
2444         if (vp->v_path != vn_vpath_empty) {
2445                 kmem_free(vp->v_path, strlen(vp->v_path) + 1);
2446                 vp->v_path = vn_vpath_empty;
2447         }
2448 
2449         /* If FEM was in use, make sure everything gets cleaned up */
2450         if (vp->v_femhead) {
2451                 /* XXX - There should be a free_femhead() that does all this */
2452                 ASSERT(vp->v_femhead->femh_list == NULL);
2453                 mutex_destroy(&vp->v_femhead->femh_lock);
2454                 kmem_free(vp->v_femhead, sizeof (*(vp->v_femhead)));
2455                 vp->v_femhead = NULL;
2456         }
2457 
2458         if (vp->v_fopdata != NULL) {
2459                 free_fopdata(vp);
2460         }
2461         vp->v_mpssdata = NULL;
2462         vsd_free(vp);
2463         kmem_cache_free(vn_cache, vp);
2464 }
2465 
2466 /*
2467  * vnode status changes, should define better states than 1, 0.
2468  */
2469 void
2470 vn_reclaim(vnode_t *vp)
2471 {
2472         vfs_t   *vfsp = vp->v_vfsp;
2473 
2474         if (vfsp == NULL ||
2475             vfsp->vfs_implp == NULL || vfsp->vfs_femhead == NULL) {
2476                 return;
2477         }
2478         (void) VFS_VNSTATE(vfsp, vp, VNTRANS_RECLAIMED);
2479 }
2480 
2481 void
2482 vn_idle(vnode_t *vp)
2483 {
2484         vfs_t   *vfsp = vp->v_vfsp;
2485 
2486         if (vfsp == NULL ||
2487             vfsp->vfs_implp == NULL || vfsp->vfs_femhead == NULL) {
2488                 return;
2489         }
2490         (void) VFS_VNSTATE(vfsp, vp, VNTRANS_IDLED);
2491 }
2492 void
2493 vn_exists(vnode_t *vp)
2494 {
2495         vfs_t   *vfsp = vp->v_vfsp;
2496 
2497         if (vfsp == NULL ||
2498             vfsp->vfs_implp == NULL || vfsp->vfs_femhead == NULL) {
2499                 return;
2500         }
2501         (void) VFS_VNSTATE(vfsp, vp, VNTRANS_EXISTS);
2502 }
2503 
2504 void
2505 vn_invalid(vnode_t *vp)
2506 {
2507         vfs_t   *vfsp = vp->v_vfsp;
2508 
2509         if (vfsp == NULL ||
2510             vfsp->vfs_implp == NULL || vfsp->vfs_femhead == NULL) {
2511                 return;
2512         }
2513         (void) VFS_VNSTATE(vfsp, vp, VNTRANS_DESTROYED);
2514 }
2515 
2516 /* Vnode event notification */
2517 
2518 int
2519 vnevent_support(vnode_t *vp, caller_context_t *ct)
2520 {
2521         if (vp == NULL)
2522                 return (EINVAL);
2523 
2524         return (VOP_VNEVENT(vp, VE_SUPPORT, NULL, NULL, ct));
2525 }
2526 
2527 void
2528 vnevent_rename_src(vnode_t *vp, vnode_t *dvp, char *name, caller_context_t *ct)
2529 {
2530         if (vp == NULL || vp->v_femhead == NULL) {
2531                 return;
2532         }
2533         (void) VOP_VNEVENT(dvp, VE_RENAME_SRC_DIR, vp, name, ct);
2534         (void) VOP_VNEVENT(vp, VE_RENAME_SRC, dvp, name, ct);
2535 }
2536 
2537 void
2538 vnevent_rename_dest(vnode_t *vp, vnode_t *dvp, char *name,
2539     caller_context_t *ct)
2540 {
2541         if (vp == NULL || vp->v_femhead == NULL) {
2542                 return;
2543         }
2544         (void) VOP_VNEVENT(vp, VE_RENAME_DEST, dvp, name, ct);
2545 }
2546 
2547 void
2548 vnevent_rename_dest_dir(vnode_t *vp, vnode_t *nvp, char *name,
2549     caller_context_t *ct)
2550 {
2551         if (vp == NULL || vp->v_femhead == NULL) {
2552                 return;
2553         }
2554         (void) VOP_VNEVENT(vp, VE_RENAME_DEST_DIR, nvp, name, ct);
2555 }
2556 
2557 void
2558 vnevent_remove(vnode_t *vp, vnode_t *dvp, char *name, caller_context_t *ct)
2559 {
2560         if (vp == NULL || vp->v_femhead == NULL) {
2561                 return;
2562         }
2563         (void) VOP_VNEVENT(vp, VE_REMOVE, dvp, name, ct);
2564 }
2565 
2566 void
2567 vnevent_rmdir(vnode_t *vp, vnode_t *dvp, char *name, caller_context_t *ct)
2568 {
2569         if (vp == NULL || vp->v_femhead == NULL) {
2570                 return;
2571         }
2572         (void) VOP_VNEVENT(vp, VE_RMDIR, dvp, name, ct);
2573 }
2574 
2575 void
2576 vnevent_pre_rename_src(vnode_t *vp, vnode_t *dvp, char *name,
2577     caller_context_t *ct)
2578 {
2579         if (vp == NULL || vp->v_femhead == NULL) {
2580                 return;
2581         }
2582         (void) VOP_VNEVENT(vp, VE_PRE_RENAME_SRC, dvp, name, ct);
2583 }
2584 
2585 void
2586 vnevent_pre_rename_dest(vnode_t *vp, vnode_t *dvp, char *name,
2587     caller_context_t *ct)
2588 {
2589         if (vp == NULL || vp->v_femhead == NULL) {
2590                 return;
2591         }
2592         (void) VOP_VNEVENT(vp, VE_PRE_RENAME_DEST, dvp, name, ct);
2593 }
2594 
2595 void
2596 vnevent_pre_rename_dest_dir(vnode_t *vp, vnode_t *nvp, char *name,
2597     caller_context_t *ct)
2598 {
2599         if (vp == NULL || vp->v_femhead == NULL) {
2600                 return;
2601         }
2602         (void) VOP_VNEVENT(vp, VE_PRE_RENAME_DEST_DIR, nvp, name, ct);
2603 }
2604 
2605 void
2606 vnevent_create(vnode_t *vp, caller_context_t *ct)
2607 {
2608         if (vp == NULL || vp->v_femhead == NULL) {
2609                 return;
2610         }
2611         (void) VOP_VNEVENT(vp, VE_CREATE, NULL, NULL, ct);
2612 }
2613 
2614 void
2615 vnevent_link(vnode_t *vp, caller_context_t *ct)
2616 {
2617         if (vp == NULL || vp->v_femhead == NULL) {
2618                 return;
2619         }
2620         (void) VOP_VNEVENT(vp, VE_LINK, NULL, NULL, ct);
2621 }
2622 
2623 void
2624 vnevent_mountedover(vnode_t *vp, caller_context_t *ct)
2625 {
2626         if (vp == NULL || vp->v_femhead == NULL) {
2627                 return;
2628         }
2629         (void) VOP_VNEVENT(vp, VE_MOUNTEDOVER, NULL, NULL, ct);
2630 }
2631 
2632 void
2633 vnevent_truncate(vnode_t *vp, caller_context_t *ct)
2634 {
2635         if (vp == NULL || vp->v_femhead == NULL) {
2636                 return;
2637         }
2638         (void) VOP_VNEVENT(vp, VE_TRUNCATE, NULL, NULL, ct);
2639 }
2640 
2641 void
2642 vnevent_resize(vnode_t *vp, caller_context_t *ct)
2643 {
2644         if (vp == NULL || vp->v_femhead == NULL) {
2645                 return;
2646         }
2647         (void) VOP_VNEVENT(vp, VE_RESIZE, NULL, NULL, ct);
2648 }
2649 
2650 /*
2651  * Vnode accessors.
2652  */
2653 
2654 int
2655 vn_is_readonly(vnode_t *vp)
2656 {
2657         return (vp->v_vfsp->vfs_flag & VFS_RDONLY);
2658 }
2659 
2660 int
2661 vn_has_flocks(vnode_t *vp)
2662 {
2663         return (vp->v_filocks != NULL);
2664 }
2665 
2666 int
2667 vn_has_mandatory_locks(vnode_t *vp, int mode)
2668 {
2669         return ((vp->v_filocks != NULL) && (MANDLOCK(vp, mode)));
2670 }
2671 
2672 int
2673 vn_has_cached_data(vnode_t *vp)
2674 {
2675         return (vp->v_pages != NULL);
2676 }
2677 
2678 /*
2679  * Return 0 if the vnode in question shouldn't be permitted into a zone via
2680  * zone_enter(2).
2681  */
2682 int
2683 vn_can_change_zones(vnode_t *vp)
2684 {
2685         struct vfssw *vswp;
2686         int allow = 1;
2687         vnode_t *rvp;
2688 
2689         if (nfs_global_client_only != 0)
2690                 return (1);
2691 
2692         /*
2693          * We always want to look at the underlying vnode if there is one.
2694          */
2695         if (VOP_REALVP(vp, &rvp, NULL) != 0)
2696                 rvp = vp;
2697         /*
2698          * Some pseudo filesystems (including doorfs) don't actually register
2699          * their vfsops_t, so the following may return NULL; we happily let
2700          * such vnodes switch zones.
2701          */
2702         vswp = vfs_getvfsswbyvfsops(vfs_getops(rvp->v_vfsp));
2703         if (vswp != NULL) {
2704                 if (vswp->vsw_flag & VSW_NOTZONESAFE)
2705                         allow = 0;
2706                 vfs_unrefvfssw(vswp);
2707         }
2708         return (allow);
2709 }
2710 
2711 /*
2712  * Return nonzero if the vnode is a mount point, zero if not.
2713  */
2714 int
2715 vn_ismntpt(vnode_t *vp)
2716 {
2717         return (vp->v_vfsmountedhere != NULL);
2718 }
2719 
2720 /* Retrieve the vfs (if any) mounted on this vnode */
2721 vfs_t *
2722 vn_mountedvfs(vnode_t *vp)
2723 {
2724         return (vp->v_vfsmountedhere);
2725 }
2726 
2727 /*
2728  * Return nonzero if the vnode is referenced by the dnlc, zero if not.
2729  */
2730 int
2731 vn_in_dnlc(vnode_t *vp)
2732 {
2733         return (vp->v_count_dnlc > 0);
2734 }
2735 
2736 /*
2737  * vn_has_other_opens() checks whether a particular file is opened by more than
2738  * just the caller and whether the open is for read and/or write.
2739  * This routine is for calling after the caller has already called VOP_OPEN()
2740  * and the caller wishes to know if they are the only one with it open for
2741  * the mode(s) specified.
2742  *
2743  * Vnode counts are only kept on regular files (v_type=VREG).
2744  */
2745 int
2746 vn_has_other_opens(
2747         vnode_t *vp,
2748         v_mode_t mode)
2749 {
2750 
2751         ASSERT(vp != NULL);
2752 
2753         switch (mode) {
2754         case V_WRITE:
2755                 if (vp->v_wrcnt > 1)
2756                         return (V_TRUE);
2757                 break;
2758         case V_RDORWR:
2759                 if ((vp->v_rdcnt > 1) || (vp->v_wrcnt > 1))
2760                         return (V_TRUE);
2761                 break;
2762         case V_RDANDWR:
2763                 if ((vp->v_rdcnt > 1) && (vp->v_wrcnt > 1))
2764                         return (V_TRUE);
2765                 break;
2766         case V_READ:
2767                 if (vp->v_rdcnt > 1)
2768                         return (V_TRUE);
2769                 break;
2770         }
2771 
2772         return (V_FALSE);
2773 }
2774 
2775 /*
2776  * vn_is_opened() checks whether a particular file is opened and
2777  * whether the open is for read and/or write.
2778  *
2779  * Vnode counts are only kept on regular files (v_type=VREG).
2780  */
2781 int
2782 vn_is_opened(
2783         vnode_t *vp,
2784         v_mode_t mode)
2785 {
2786 
2787         ASSERT(vp != NULL);
2788 
2789         switch (mode) {
2790         case V_WRITE:
2791                 if (vp->v_wrcnt)
2792                         return (V_TRUE);
2793                 break;
2794         case V_RDANDWR:
2795                 if (vp->v_rdcnt && vp->v_wrcnt)
2796                         return (V_TRUE);
2797                 break;
2798         case V_RDORWR:
2799                 if (vp->v_rdcnt || vp->v_wrcnt)
2800                         return (V_TRUE);
2801                 break;
2802         case V_READ:
2803                 if (vp->v_rdcnt)
2804                         return (V_TRUE);
2805                 break;
2806         }
2807 
2808         return (V_FALSE);
2809 }
2810 
2811 /*
2812  * vn_is_mapped() checks whether a particular file is mapped and whether
2813  * the file is mapped read and/or write.
2814  */
2815 int
2816 vn_is_mapped(
2817         vnode_t *vp,
2818         v_mode_t mode)
2819 {
2820 
2821         ASSERT(vp != NULL);
2822 
2823 #if !defined(_LP64)
2824         switch (mode) {
2825         /*
2826          * The atomic_add_64_nv functions force atomicity in the
2827          * case of 32 bit architectures. Otherwise the 64 bit values
2828          * require two fetches. The value of the fields may be
2829          * (potentially) changed between the first fetch and the
2830          * second
2831          */
2832         case V_WRITE:
2833                 if (atomic_add_64_nv((&(vp->v_mmap_write)), 0))
2834                         return (V_TRUE);
2835                 break;
2836         case V_RDANDWR:
2837                 if ((atomic_add_64_nv((&(vp->v_mmap_read)), 0)) &&
2838                     (atomic_add_64_nv((&(vp->v_mmap_write)), 0)))
2839                         return (V_TRUE);
2840                 break;
2841         case V_RDORWR:
2842                 if ((atomic_add_64_nv((&(vp->v_mmap_read)), 0)) ||
2843                     (atomic_add_64_nv((&(vp->v_mmap_write)), 0)))
2844                         return (V_TRUE);
2845                 break;
2846         case V_READ:
2847                 if (atomic_add_64_nv((&(vp->v_mmap_read)), 0))
2848                         return (V_TRUE);
2849                 break;
2850         }
2851 #else
2852         switch (mode) {
2853         case V_WRITE:
2854                 if (vp->v_mmap_write)
2855                         return (V_TRUE);
2856                 break;
2857         case V_RDANDWR:
2858                 if (vp->v_mmap_read && vp->v_mmap_write)
2859                         return (V_TRUE);
2860                 break;
2861         case V_RDORWR:
2862                 if (vp->v_mmap_read || vp->v_mmap_write)
2863                         return (V_TRUE);
2864                 break;
2865         case V_READ:
2866                 if (vp->v_mmap_read)
2867                         return (V_TRUE);
2868                 break;
2869         }
2870 #endif
2871 
2872         return (V_FALSE);
2873 }
2874 
2875 /*
2876  * Set the operations vector for a vnode.
2877  *
2878  * FEM ensures that the v_femhead pointer is filled in before the
2879  * v_op pointer is changed.  This means that if the v_femhead pointer
2880  * is NULL, and the v_op field hasn't changed since before which checked
2881  * the v_femhead pointer; then our update is ok - we are not racing with
2882  * FEM.
2883  */
2884 void
2885 vn_setops(vnode_t *vp, vnodeops_t *vnodeops)
2886 {
2887         vnodeops_t      *op;
2888 
2889         ASSERT(vp != NULL);
2890         ASSERT(vnodeops != NULL);
2891 
2892         op = vp->v_op;
2893         membar_consumer();
2894         /*
2895          * If vp->v_femhead == NULL, then we'll call atomic_cas_ptr() to do
2896          * the compare-and-swap on vp->v_op.  If either fails, then FEM is
2897          * in effect on the vnode and we need to have FEM deal with it.
2898          */
2899         if (vp->v_femhead != NULL || atomic_cas_ptr(&vp->v_op, op, vnodeops) !=
2900             op) {
2901                 fem_setvnops(vp, vnodeops);
2902         }
2903 }
2904 
2905 /*
2906  * Retrieve the operations vector for a vnode
2907  * As with vn_setops(above); make sure we aren't racing with FEM.
2908  * FEM sets the v_op to a special, internal, vnodeops that wouldn't
2909  * make sense to the callers of this routine.
2910  */
2911 vnodeops_t *
2912 vn_getops(vnode_t *vp)
2913 {
2914         vnodeops_t      *op;
2915 
2916         ASSERT(vp != NULL);
2917 
2918         op = vp->v_op;
2919         membar_consumer();
2920         if (vp->v_femhead == NULL && op == vp->v_op) {
2921                 return (op);
2922         } else {
2923                 return (fem_getvnops(vp));
2924         }
2925 }
2926 
2927 /*
2928  * Returns non-zero (1) if the vnodeops matches that of the vnode.
2929  * Returns zero (0) if not.
2930  */
2931 int
2932 vn_matchops(vnode_t *vp, vnodeops_t *vnodeops)
2933 {
2934         return (vn_getops(vp) == vnodeops);
2935 }
2936 
2937 /*
2938  * Returns non-zero (1) if the specified operation matches the
2939  * corresponding operation for that the vnode.
2940  * Returns zero (0) if not.
2941  */
2942 
2943 #define MATCHNAME(n1, n2) (((n1)[0] == (n2)[0]) && (strcmp((n1), (n2)) == 0))
2944 
2945 int
2946 vn_matchopval(vnode_t *vp, char *vopname, fs_generic_func_p funcp)
2947 {
2948         const fs_operation_trans_def_t *otdp;
2949         fs_generic_func_p *loc = NULL;
2950         vnodeops_t      *vop = vn_getops(vp);
2951 
2952         ASSERT(vopname != NULL);
2953 
2954         for (otdp = vn_ops_table; otdp->name != NULL; otdp++) {
2955                 if (MATCHNAME(otdp->name, vopname)) {
2956                         loc = (fs_generic_func_p *)
2957                             ((char *)(vop) + otdp->offset);
2958                         break;
2959                 }
2960         }
2961 
2962         return ((loc != NULL) && (*loc == funcp));
2963 }
2964 
2965 /*
2966  * fs_new_caller_id() needs to return a unique ID on a given local system.
2967  * The IDs do not need to survive across reboots.  These are primarily
2968  * used so that (FEM) monitors can detect particular callers (such as
2969  * the NFS server) to a given vnode/vfs operation.
2970  */
2971 u_longlong_t
2972 fs_new_caller_id()
2973 {
2974         static uint64_t next_caller_id = 0LL; /* First call returns 1 */
2975 
2976         return ((u_longlong_t)atomic_inc_64_nv(&next_caller_id));
2977 }
2978 
2979 /*
2980  * The value stored in v_path is relative to rootdir, located in the global
2981  * zone.  Zones or chroot environments which reside deeper inside the VFS
2982  * hierarchy will have a relative view of MAXPATHLEN since they are unaware of
2983  * what lies below their perceived root.  In order to keep v_path usable for
2984  * these child environments, its allocations are allowed to exceed MAXPATHLEN.
2985  *
2986  * An upper bound of max_vnode_path is placed upon v_path allocations to
2987  * prevent the system from going too wild at the behest of pathological
2988  * behavior from the operator.
2989  */
2990 size_t max_vnode_path = 4 * MAXPATHLEN;
2991 
2992 
2993 void
2994 vn_clearpath(vnode_t *vp, hrtime_t compare_stamp)
2995 {
2996         char *buf;
2997 
2998         mutex_enter(&vp->v_lock);
2999         /*
3000          * If the snapshot of v_path_stamp passed in via compare_stamp does not
3001          * match the present value on the vnode, it indicates that subsequent
3002          * changes have occurred.  The v_path value is not cleared in this case
3003          * since the new value may be valid.
3004          */
3005         if (compare_stamp != 0 && vp->v_path_stamp != compare_stamp) {
3006                 mutex_exit(&vp->v_lock);
3007                 return;
3008         }
3009         buf = vp->v_path;
3010         vp->v_path = vn_vpath_empty;
3011         vp->v_path_stamp = 0;
3012         mutex_exit(&vp->v_lock);
3013         if (buf != vn_vpath_empty) {
3014                 kmem_free(buf, strlen(buf) + 1);
3015         }
3016 }
3017 
3018 static void
3019 vn_setpath_common(vnode_t *pvp, vnode_t *vp, const char *name, size_t len,
3020     boolean_t is_rename)
3021 {
3022         char *buf, *oldbuf;
3023         hrtime_t pstamp;
3024         size_t baselen, buflen = 0;
3025 
3026         /* Handle the vn_setpath_str case. */
3027         if (pvp == NULL) {
3028                 if (len + 1 > max_vnode_path) {
3029                         DTRACE_PROBE4(vn__setpath__too__long, vnode_t *, pvp,
3030                             vnode_t *, vp, char *, name, size_t, len + 1);
3031                         return;
3032                 }
3033                 buf = kmem_alloc(len + 1, KM_SLEEP);
3034                 bcopy(name, buf, len);
3035                 buf[len] = '\0';
3036 
3037                 mutex_enter(&vp->v_lock);
3038                 oldbuf = vp->v_path;
3039                 vp->v_path = buf;
3040                 vp->v_path_stamp = gethrtime();
3041                 mutex_exit(&vp->v_lock);
3042                 if (oldbuf != vn_vpath_empty) {
3043                         kmem_free(oldbuf, strlen(oldbuf) + 1);
3044                 }
3045                 return;
3046         }
3047 
3048         /* Take snapshot of parent dir */
3049         mutex_enter(&pvp->v_lock);
3050 retrybuf:
3051         if (pvp->v_path == vn_vpath_empty) {
3052                 /*
3053                  * Without v_path from the parent directory, generating a child
3054                  * path from the name is impossible.
3055                  */
3056                 if (len > 0) {
3057                         pstamp = pvp->v_path_stamp;
3058                         mutex_exit(&pvp->v_lock);
3059                         vn_clearpath(vp, pstamp);
3060                         return;
3061                 }
3062 
3063                 /*
3064                  * The only feasible case here is where a NUL lookup is being
3065                  * performed on rootdir prior to its v_path being populated.
3066                  */
3067                 ASSERT(pvp->v_path_stamp = 0);
3068                 baselen = 0;
3069                 pstamp = 0;
3070         } else {
3071                 pstamp = pvp->v_path_stamp;
3072                 baselen = strlen(pvp->v_path);
3073                 /* ignore a trailing slash if present */
3074                 if (pvp->v_path[baselen - 1] == '/') {
3075                         /* This should only the be case for rootdir */
3076                         ASSERT(baselen == 1 && pvp == rootdir);
3077                         baselen--;
3078                 }
3079         }
3080         mutex_exit(&pvp->v_lock);
3081 
3082         if (buflen != 0) {
3083                 /* Free the existing (mis-sized) buffer in case of retry */
3084                 kmem_free(buf, buflen);
3085         }
3086         /* base, '/', name and trailing NUL */
3087         buflen = baselen + len + 2;
3088         if (buflen > max_vnode_path) {
3089                 DTRACE_PROBE4(vn__setpath_too__long, vnode_t *, pvp,
3090                     vnode_t *, vp, char *, name, size_t, buflen);
3091                 return;
3092         }
3093         buf = kmem_alloc(buflen, KM_SLEEP);
3094 
3095         mutex_enter(&pvp->v_lock);
3096         if (pvp->v_path_stamp != pstamp) {
3097                 size_t vlen;
3098 
3099                 /*
3100                  * Since v_path_stamp changed on the parent, it is likely that
3101                  * v_path has been altered as well.  If the length does not
3102                  * exactly match what was previously measured, the buffer
3103                  * allocation must be repeated for proper sizing.
3104                  */
3105                 if (pvp->v_path == vn_vpath_empty) {
3106                         /* Give up if parent lack v_path */
3107                         mutex_exit(&pvp->v_lock);
3108                         kmem_free(buf, buflen);
3109                         return;
3110                 }
3111                 vlen = strlen(pvp->v_path);
3112                 if (pvp->v_path[vlen - 1] == '/') {
3113                         vlen--;
3114                 }
3115                 if (vlen != baselen) {
3116                         goto retrybuf;
3117                 }
3118         }
3119         bcopy(pvp->v_path, buf, baselen);
3120         mutex_exit(&pvp->v_lock);
3121 
3122         buf[baselen] = '/';
3123         baselen++;
3124         bcopy(name, &buf[baselen], len + 1);
3125 
3126         mutex_enter(&vp->v_lock);
3127         if (vp->v_path_stamp == 0) {
3128                 /* never-visited vnode can inherit stamp from parent */
3129                 ASSERT(vp->v_path == vn_vpath_empty);
3130                 vp->v_path_stamp = pstamp;
3131                 vp->v_path = buf;
3132                 mutex_exit(&vp->v_lock);
3133         } else if (vp->v_path_stamp < pstamp || is_rename) {
3134                 /*
3135                  * Install the updated path and stamp, ensuring that the v_path
3136                  * pointer is valid at all times for dtrace.
3137                  */
3138                 oldbuf = vp->v_path;
3139                 vp->v_path = buf;
3140                 vp->v_path_stamp = gethrtime();
3141                 mutex_exit(&vp->v_lock);
3142                 kmem_free(oldbuf, strlen(oldbuf) + 1);
3143         } else {
3144                 /*
3145                  * If the timestamp matches or is greater, it means another
3146                  * thread performed the update first while locks were dropped
3147                  * here to make the allocation.  We defer to the newer value.
3148                  */
3149                 mutex_exit(&vp->v_lock);
3150                 kmem_free(buf, buflen);
3151         }
3152         ASSERT(MUTEX_NOT_HELD(&vp->v_lock));
3153 }
3154 
3155 void
3156 vn_updatepath(vnode_t *pvp, vnode_t *vp, const char *name)
3157 {
3158         size_t len;
3159 
3160         /*
3161          * If the parent is older or empty, there's nothing further to do.
3162          */
3163         if (pvp->v_path == vn_vpath_empty ||
3164             pvp->v_path_stamp <= vp->v_path_stamp) {
3165                 return;
3166         }
3167 
3168         /*
3169          * Given the lack of appropriate context, meaningful updates to v_path
3170          * cannot be made for during lookups for the '.' or '..' entries.
3171          */
3172         len = strlen(name);
3173         if (len == 0 || (len == 1 && name[0] == '.') ||
3174             (len == 2 && name[0] == '.' && name[1] == '.')) {
3175                 return;
3176         }
3177 
3178         vn_setpath_common(pvp, vp, name, len, B_FALSE);
3179 }
3180 
3181 /*
3182  * Given a starting vnode and a path, updates the path in the target vnode in
3183  * a safe manner.  If the vnode already has path information embedded, then the
3184  * cached path is left untouched.
3185  */
3186 /* ARGSUSED */
3187 void
3188 vn_setpath(vnode_t *rootvp, vnode_t *pvp, vnode_t *vp, const char *name,
3189     size_t len)
3190 {
3191         vn_setpath_common(pvp, vp, name, len, B_FALSE);
3192 }
3193 
3194 /*
3195  * Sets the path to the vnode to be the given string, regardless of current
3196  * context.  The string must be a complete path from rootdir.  This is only used
3197  * by fsop_root() for setting the path based on the mountpoint.
3198  */
3199 void
3200 vn_setpath_str(vnode_t *vp, const char *str, size_t len)
3201 {
3202         vn_setpath_common(NULL, vp, str, len, B_FALSE);
3203 }
3204 
3205 /*
3206  * Called from within filesystem's vop_rename() to handle renames once the
3207  * target vnode is available.
3208  */
3209 void
3210 vn_renamepath(vnode_t *pvp, vnode_t *vp, const char *name, size_t len)
3211 {
3212         vn_setpath_common(pvp, vp, name, len, B_TRUE);
3213 }
3214 
3215 /*
3216  * Similar to vn_setpath_str(), this function sets the path of the destination
3217  * vnode to the be the same as the source vnode.
3218  */
3219 void
3220 vn_copypath(struct vnode *src, struct vnode *dst)
3221 {
3222         char *buf;
3223         hrtime_t stamp;
3224         size_t buflen;
3225 
3226         mutex_enter(&src->v_lock);
3227         if (src->v_path == vn_vpath_empty) {
3228                 mutex_exit(&src->v_lock);
3229                 return;
3230         }
3231         buflen = strlen(src->v_path) + 1;
3232         mutex_exit(&src->v_lock);
3233 
3234         buf = kmem_alloc(buflen, KM_SLEEP);
3235 
3236         mutex_enter(&src->v_lock);
3237         if (src->v_path == vn_vpath_empty ||
3238             strlen(src->v_path) + 1 != buflen) {
3239                 mutex_exit(&src->v_lock);
3240                 kmem_free(buf, buflen);
3241                 return;
3242         }
3243         bcopy(src->v_path, buf, buflen);
3244         stamp = src->v_path_stamp;
3245         mutex_exit(&src->v_lock);
3246 
3247         mutex_enter(&dst->v_lock);
3248         if (dst->v_path != vn_vpath_empty) {
3249                 mutex_exit(&dst->v_lock);
3250                 kmem_free(buf, buflen);
3251                 return;
3252         }
3253         dst->v_path = buf;
3254         dst->v_path_stamp = stamp;
3255         mutex_exit(&dst->v_lock);
3256 }
3257 
3258 
3259 /*
3260  * XXX Private interface for segvn routines that handle vnode
3261  * large page segments.
3262  *
3263  * return 1 if vp's file system VOP_PAGEIO() implementation
3264  * can be safely used instead of VOP_GETPAGE() for handling
3265  * pagefaults against regular non swap files. VOP_PAGEIO()
3266  * interface is considered safe here if its implementation
3267  * is very close to VOP_GETPAGE() implementation.
3268  * e.g. It zero's out the part of the page beyond EOF. Doesn't
3269  * panic if there're file holes but instead returns an error.
3270  * Doesn't assume file won't be changed by user writes, etc.
3271  *
3272  * return 0 otherwise.
3273  *
3274  * For now allow segvn to only use VOP_PAGEIO() with ufs and nfs.
3275  */
3276 int
3277 vn_vmpss_usepageio(vnode_t *vp)
3278 {
3279         vfs_t   *vfsp = vp->v_vfsp;
3280         char *fsname = vfssw[vfsp->vfs_fstype].vsw_name;
3281         char *pageio_ok_fss[] = {"ufs", "nfs", NULL};
3282         char **fsok = pageio_ok_fss;
3283 
3284         if (fsname == NULL) {
3285                 return (0);
3286         }
3287 
3288         for (; *fsok; fsok++) {
3289                 if (strcmp(*fsok, fsname) == 0) {
3290                         return (1);
3291                 }
3292         }
3293         return (0);
3294 }
3295 
3296 /* VOP_XXX() macros call the corresponding fop_xxx() function */
3297 
3298 int
3299 fop_open(
3300         vnode_t **vpp,
3301         int mode,
3302         cred_t *cr,
3303         caller_context_t *ct)
3304 {
3305         int ret;
3306         vnode_t *vp = *vpp;
3307 
3308         VN_HOLD(vp);
3309         /*
3310          * Adding to the vnode counts before calling open
3311          * avoids the need for a mutex. It circumvents a race
3312          * condition where a query made on the vnode counts results in a
3313          * false negative. The inquirer goes away believing the file is
3314          * not open when there is an open on the file already under way.
3315          *
3316          * The counts are meant to prevent NFS from granting a delegation
3317          * when it would be dangerous to do so.
3318          *
3319          * The vnode counts are only kept on regular files
3320          */
3321         if ((*vpp)->v_type == VREG) {
3322                 if (mode & FREAD)
3323                         atomic_inc_32(&(*vpp)->v_rdcnt);
3324                 if (mode & FWRITE)
3325                         atomic_inc_32(&(*vpp)->v_wrcnt);
3326         }
3327 
3328         VOPXID_MAP_CR(vp, cr);
3329 
3330         ret = (*(*(vpp))->v_op->vop_open)(vpp, mode, cr, ct);
3331 
3332         if (ret) {
3333                 /*
3334                  * Use the saved vp just in case the vnode ptr got trashed
3335                  * by the error.
3336                  */
3337                 VOPSTATS_UPDATE(vp, open);
3338                 if ((vp->v_type == VREG) && (mode & FREAD))
3339                         atomic_dec_32(&vp->v_rdcnt);
3340                 if ((vp->v_type == VREG) && (mode & FWRITE))
3341                         atomic_dec_32(&vp->v_wrcnt);
3342         } else {
3343                 /*
3344                  * Some filesystems will return a different vnode,
3345                  * but the same path was still used to open it.
3346                  * So if we do change the vnode and need to
3347                  * copy over the path, do so here, rather than special
3348                  * casing each filesystem. Adjust the vnode counts to
3349                  * reflect the vnode switch.
3350                  */
3351                 VOPSTATS_UPDATE(*vpp, open);
3352                 if (*vpp != vp && *vpp != NULL) {
3353                         vn_copypath(vp, *vpp);
3354                         if (((*vpp)->v_type == VREG) && (mode & FREAD))
3355                                 atomic_inc_32(&(*vpp)->v_rdcnt);
3356                         if ((vp->v_type == VREG) && (mode & FREAD))
3357                                 atomic_dec_32(&vp->v_rdcnt);
3358                         if (((*vpp)->v_type == VREG) && (mode & FWRITE))
3359                                 atomic_inc_32(&(*vpp)->v_wrcnt);
3360                         if ((vp->v_type == VREG) && (mode & FWRITE))
3361                                 atomic_dec_32(&vp->v_wrcnt);
3362                 }
3363         }
3364         VN_RELE(vp);
3365         return (ret);
3366 }
3367 
3368 int
3369 fop_close(
3370         vnode_t *vp,
3371         int flag,
3372         int count,
3373         offset_t offset,
3374         cred_t *cr,
3375         caller_context_t *ct)
3376 {
3377         int err;
3378 
3379         VOPXID_MAP_CR(vp, cr);
3380 
3381         err = (*(vp)->v_op->vop_close)(vp, flag, count, offset, cr, ct);
3382         VOPSTATS_UPDATE(vp, close);
3383         /*
3384          * Check passed in count to handle possible dups. Vnode counts are only
3385          * kept on regular files
3386          */
3387         if ((vp->v_type == VREG) && (count == 1))  {
3388                 if (flag & FREAD) {
3389                         ASSERT(vp->v_rdcnt > 0);
3390                         atomic_dec_32(&vp->v_rdcnt);
3391                 }
3392                 if (flag & FWRITE) {
3393                         ASSERT(vp->v_wrcnt > 0);
3394                         atomic_dec_32(&vp->v_wrcnt);
3395                 }
3396         }
3397         return (err);
3398 }
3399 
3400 int
3401 fop_read(
3402         vnode_t *vp,
3403         uio_t *uiop,
3404         int ioflag,
3405         cred_t *cr,
3406         caller_context_t *ct)
3407 {
3408         ssize_t resid_start = uiop->uio_resid;
3409         zone_t  *zonep = curzone;
3410         zone_vfs_kstat_t *zvp = zonep->zone_vfs_stats;
3411 
3412         hrtime_t start = 0, lat;
3413         ssize_t len;
3414         int err;
3415 
3416         if ((vp->v_type == VREG || vp->v_type == VDIR || vp->v_type == VBLK) &&
3417             vp->v_vfsp != NULL && (vp->v_vfsp->vfs_flag & VFS_STATS)) {
3418                 start = gethrtime();
3419 
3420                 mutex_enter(&zonep->zone_vfs_lock);
3421                 kstat_runq_enter(&zonep->zone_vfs_rwstats);
3422                 mutex_exit(&zonep->zone_vfs_lock);
3423         }
3424 
3425         VOPXID_MAP_CR(vp, cr);
3426 
3427         err = (*(vp)->v_op->vop_read)(vp, uiop, ioflag, cr, ct);
3428         len = resid_start - uiop->uio_resid;
3429 
3430         VOPSTATS_UPDATE_IO(vp, read, read_bytes, len);
3431 
3432         if (start != 0) {
3433                 mutex_enter(&zonep->zone_vfs_lock);
3434                 zonep->zone_vfs_rwstats.reads++;
3435                 zonep->zone_vfs_rwstats.nread += len;
3436                 kstat_runq_exit(&zonep->zone_vfs_rwstats);
3437                 mutex_exit(&zonep->zone_vfs_lock);
3438 
3439                 lat = gethrtime() - start;
3440 
3441                 if (lat >= VOP_LATENCY_10MS) {
3442                         if (lat < VOP_LATENCY_100MS)
3443                                 atomic_inc_64(&zvp->zv_10ms_ops.value.ui64);
3444                         else if (lat < VOP_LATENCY_1S) {
3445                                 atomic_inc_64(&zvp->zv_10ms_ops.value.ui64);
3446                                 atomic_inc_64(&zvp->zv_100ms_ops.value.ui64);
3447                         } else if (lat < VOP_LATENCY_10S) {
3448                                 atomic_inc_64(&zvp->zv_10ms_ops.value.ui64);
3449                                 atomic_inc_64(&zvp->zv_100ms_ops.value.ui64);
3450                                 atomic_inc_64(&zvp->zv_1s_ops.value.ui64);
3451                         } else {
3452                                 atomic_inc_64(&zvp->zv_10ms_ops.value.ui64);
3453                                 atomic_inc_64(&zvp->zv_100ms_ops.value.ui64);
3454                                 atomic_inc_64(&zvp->zv_1s_ops.value.ui64);
3455                                 atomic_inc_64(&zvp->zv_10s_ops.value.ui64);
3456                         }
3457                 }
3458         }
3459 
3460         return (err);
3461 }
3462 
3463 int
3464 fop_write(
3465         vnode_t *vp,
3466         uio_t *uiop,
3467         int ioflag,
3468         cred_t *cr,
3469         caller_context_t *ct)
3470 {
3471         ssize_t resid_start = uiop->uio_resid;
3472         zone_t  *zonep = curzone;
3473         zone_vfs_kstat_t *zvp = zonep->zone_vfs_stats;
3474 
3475         hrtime_t start = 0, lat;
3476         ssize_t len;
3477         int     err;
3478 
3479         /*
3480          * For the purposes of VFS kstat consumers, the "waitq" calculation is
3481          * repurposed as the active queue for VFS write operations.  There's no
3482          * actual wait queue for VFS operations.
3483          */
3484         if ((vp->v_type == VREG || vp->v_type == VDIR || vp->v_type == VBLK) &&
3485             vp->v_vfsp != NULL && (vp->v_vfsp->vfs_flag & VFS_STATS)) {
3486                 start = gethrtime();
3487 
3488                 mutex_enter(&zonep->zone_vfs_lock);
3489                 kstat_waitq_enter(&zonep->zone_vfs_rwstats);
3490                 mutex_exit(&zonep->zone_vfs_lock);
3491         }
3492 
3493         VOPXID_MAP_CR(vp, cr);
3494 
3495         err = (*(vp)->v_op->vop_write)(vp, uiop, ioflag, cr, ct);
3496         len = resid_start - uiop->uio_resid;
3497 
3498         VOPSTATS_UPDATE_IO(vp, write, write_bytes, len);
3499 
3500         if (start != 0) {
3501                 mutex_enter(&zonep->zone_vfs_lock);
3502                 zonep->zone_vfs_rwstats.writes++;
3503                 zonep->zone_vfs_rwstats.nwritten += len;
3504                 kstat_waitq_exit(&zonep->zone_vfs_rwstats);
3505                 mutex_exit(&zonep->zone_vfs_lock);
3506 
3507                 lat = gethrtime() - start;
3508 
3509                 if (lat >= VOP_LATENCY_10MS) {
3510                         if (lat < VOP_LATENCY_100MS)
3511                                 atomic_inc_64(&zvp->zv_10ms_ops.value.ui64);
3512                         else if (lat < VOP_LATENCY_1S) {
3513                                 atomic_inc_64(&zvp->zv_10ms_ops.value.ui64);
3514                                 atomic_inc_64(&zvp->zv_100ms_ops.value.ui64);
3515                         } else if (lat < VOP_LATENCY_10S) {
3516                                 atomic_inc_64(&zvp->zv_10ms_ops.value.ui64);
3517                                 atomic_inc_64(&zvp->zv_100ms_ops.value.ui64);
3518                                 atomic_inc_64(&zvp->zv_1s_ops.value.ui64);
3519                         } else {
3520                                 atomic_inc_64(&zvp->zv_10ms_ops.value.ui64);
3521                                 atomic_inc_64(&zvp->zv_100ms_ops.value.ui64);
3522                                 atomic_inc_64(&zvp->zv_1s_ops.value.ui64);
3523                                 atomic_inc_64(&zvp->zv_10s_ops.value.ui64);
3524                         }
3525                 }
3526         }
3527 
3528         return (err);
3529 }
3530 
3531 int
3532 fop_ioctl(
3533         vnode_t *vp,
3534         int cmd,
3535         intptr_t arg,
3536         int flag,
3537         cred_t *cr,
3538         int *rvalp,
3539         caller_context_t *ct)
3540 {
3541         int     err;
3542 
3543         VOPXID_MAP_CR(vp, cr);
3544 
3545         err = (*(vp)->v_op->vop_ioctl)(vp, cmd, arg, flag, cr, rvalp, ct);
3546         VOPSTATS_UPDATE(vp, ioctl);
3547         return (err);
3548 }
3549 
3550 int
3551 fop_setfl(
3552         vnode_t *vp,
3553         int oflags,
3554         int nflags,
3555         cred_t *cr,
3556         caller_context_t *ct)
3557 {
3558         int     err;
3559 
3560         VOPXID_MAP_CR(vp, cr);
3561 
3562         err = (*(vp)->v_op->vop_setfl)(vp, oflags, nflags, cr, ct);
3563         VOPSTATS_UPDATE(vp, setfl);
3564         return (err);
3565 }
3566 
3567 int
3568 fop_getattr(
3569         vnode_t *vp,
3570         vattr_t *vap,
3571         int flags,
3572         cred_t *cr,
3573         caller_context_t *ct)
3574 {
3575         int     err;
3576 
3577         VOPXID_MAP_CR(vp, cr);
3578 
3579         /*
3580          * If this file system doesn't understand the xvattr extensions
3581          * then turn off the xvattr bit.
3582          */
3583         if (vfs_has_feature(vp->v_vfsp, VFSFT_XVATTR) == 0) {
3584                 vap->va_mask &= ~AT_XVATTR;
3585         }
3586 
3587         /*
3588          * We're only allowed to skip the ACL check iff we used a 32 bit
3589          * ACE mask with VOP_ACCESS() to determine permissions.
3590          */
3591         if ((flags & ATTR_NOACLCHECK) &&
3592             vfs_has_feature(vp->v_vfsp, VFSFT_ACEMASKONACCESS) == 0) {
3593                 return (EINVAL);
3594         }
3595         err = (*(vp)->v_op->vop_getattr)(vp, vap, flags, cr, ct);
3596         VOPSTATS_UPDATE(vp, getattr);
3597         return (err);
3598 }
3599 
3600 int
3601 fop_setattr(
3602         vnode_t *vp,
3603         vattr_t *vap,
3604         int flags,
3605         cred_t *cr,
3606         caller_context_t *ct)
3607 {
3608         int     err;
3609 
3610         VOPXID_MAP_CR(vp, cr);
3611 
3612         /*
3613          * If this file system doesn't understand the xvattr extensions
3614          * then turn off the xvattr bit.
3615          */
3616         if (vfs_has_feature(vp->v_vfsp, VFSFT_XVATTR) == 0) {
3617                 vap->va_mask &= ~AT_XVATTR;
3618         }
3619 
3620         /*
3621          * We're only allowed to skip the ACL check iff we used a 32 bit
3622          * ACE mask with VOP_ACCESS() to determine permissions.
3623          */
3624         if ((flags & ATTR_NOACLCHECK) &&
3625             vfs_has_feature(vp->v_vfsp, VFSFT_ACEMASKONACCESS) == 0) {
3626                 return (EINVAL);
3627         }
3628         err = (*(vp)->v_op->vop_setattr)(vp, vap, flags, cr, ct);
3629         VOPSTATS_UPDATE(vp, setattr);
3630         return (err);
3631 }
3632 
3633 int
3634 fop_access(
3635         vnode_t *vp,
3636         int mode,
3637         int flags,
3638         cred_t *cr,
3639         caller_context_t *ct)
3640 {
3641         int     err;
3642 
3643         if ((flags & V_ACE_MASK) &&
3644             vfs_has_feature(vp->v_vfsp, VFSFT_ACEMASKONACCESS) == 0) {
3645                 return (EINVAL);
3646         }
3647 
3648         VOPXID_MAP_CR(vp, cr);
3649 
3650         err = (*(vp)->v_op->vop_access)(vp, mode, flags, cr, ct);
3651         VOPSTATS_UPDATE(vp, access);
3652         return (err);
3653 }
3654 
3655 int
3656 fop_lookup(
3657         vnode_t *dvp,
3658         char *nm,
3659         vnode_t **vpp,
3660         pathname_t *pnp,
3661         int flags,
3662         vnode_t *rdir,
3663         cred_t *cr,
3664         caller_context_t *ct,
3665         int *deflags,           /* Returned per-dirent flags */
3666         pathname_t *ppnp)       /* Returned case-preserved name in directory */
3667 {
3668         int ret;
3669 
3670         /*
3671          * If this file system doesn't support case-insensitive access
3672          * and said access is requested, fail quickly.  It is required
3673          * that if the vfs supports case-insensitive lookup, it also
3674          * supports extended dirent flags.
3675          */
3676         if (flags & FIGNORECASE &&
3677             (vfs_has_feature(dvp->v_vfsp, VFSFT_CASEINSENSITIVE) == 0 &&
3678             vfs_has_feature(dvp->v_vfsp, VFSFT_NOCASESENSITIVE) == 0))
3679                 return (EINVAL);
3680 
3681         VOPXID_MAP_CR(dvp, cr);
3682 
3683         if ((flags & LOOKUP_XATTR) && (flags & LOOKUP_HAVE_SYSATTR_DIR) == 0) {
3684                 ret = xattr_dir_lookup(dvp, vpp, flags, cr);
3685         } else {
3686                 ret = (*(dvp)->v_op->vop_lookup)
3687                     (dvp, nm, vpp, pnp, flags, rdir, cr, ct, deflags, ppnp);
3688         }
3689         if (ret == 0 && *vpp) {
3690                 VOPSTATS_UPDATE(*vpp, lookup);
3691                 vn_updatepath(dvp, *vpp, nm);
3692         }
3693 
3694         return (ret);
3695 }
3696 
3697 int
3698 fop_create(
3699         vnode_t *dvp,
3700         char *name,
3701         vattr_t *vap,
3702         vcexcl_t excl,
3703         int mode,
3704         vnode_t **vpp,
3705         cred_t *cr,
3706         int flags,
3707         caller_context_t *ct,
3708         vsecattr_t *vsecp)      /* ACL to set during create */
3709 {
3710         int ret;
3711 
3712         if (vsecp != NULL &&
3713             vfs_has_feature(dvp->v_vfsp, VFSFT_ACLONCREATE) == 0) {
3714                 return (EINVAL);
3715         }
3716         /*
3717          * If this file system doesn't support case-insensitive access
3718          * and said access is requested, fail quickly.
3719          */
3720         if (flags & FIGNORECASE &&
3721             (vfs_has_feature(dvp->v_vfsp, VFSFT_CASEINSENSITIVE) == 0 &&
3722             vfs_has_feature(dvp->v_vfsp, VFSFT_NOCASESENSITIVE) == 0))
3723                 return (EINVAL);
3724 
3725         VOPXID_MAP_CR(dvp, cr);
3726 
3727         ret = (*(dvp)->v_op->vop_create)
3728             (dvp, name, vap, excl, mode, vpp, cr, flags, ct, vsecp);
3729         if (ret == 0 && *vpp) {
3730                 VOPSTATS_UPDATE(*vpp, create);
3731                 vn_updatepath(dvp, *vpp, name);
3732         }
3733 
3734         return (ret);
3735 }
3736 
3737 int
3738 fop_remove(
3739         vnode_t *dvp,
3740         char *nm,
3741         cred_t *cr,
3742         caller_context_t *ct,
3743         int flags)
3744 {
3745         int     err;
3746 
3747         /*
3748          * If this file system doesn't support case-insensitive access
3749          * and said access is requested, fail quickly.
3750          */
3751         if (flags & FIGNORECASE &&
3752             (vfs_has_feature(dvp->v_vfsp, VFSFT_CASEINSENSITIVE) == 0 &&
3753             vfs_has_feature(dvp->v_vfsp, VFSFT_NOCASESENSITIVE) == 0))
3754                 return (EINVAL);
3755 
3756         VOPXID_MAP_CR(dvp, cr);
3757 
3758         err = (*(dvp)->v_op->vop_remove)(dvp, nm, cr, ct, flags);
3759         VOPSTATS_UPDATE(dvp, remove);
3760         return (err);
3761 }
3762 
3763 int
3764 fop_link(
3765         vnode_t *tdvp,
3766         vnode_t *svp,
3767         char *tnm,
3768         cred_t *cr,
3769         caller_context_t *ct,
3770         int flags)
3771 {
3772         int     err;
3773 
3774         /*
3775          * If the target file system doesn't support case-insensitive access
3776          * and said access is requested, fail quickly.
3777          */
3778         if (flags & FIGNORECASE &&
3779             (vfs_has_feature(tdvp->v_vfsp, VFSFT_CASEINSENSITIVE) == 0 &&
3780             vfs_has_feature(tdvp->v_vfsp, VFSFT_NOCASESENSITIVE) == 0))
3781                 return (EINVAL);
3782 
3783         VOPXID_MAP_CR(tdvp, cr);
3784 
3785         err = (*(tdvp)->v_op->vop_link)(tdvp, svp, tnm, cr, ct, flags);
3786         VOPSTATS_UPDATE(tdvp, link);
3787         return (err);
3788 }
3789 
3790 int
3791 fop_rename(
3792         vnode_t *sdvp,
3793         char *snm,
3794         vnode_t *tdvp,
3795         char *tnm,
3796         cred_t *cr,
3797         caller_context_t *ct,
3798         int flags)
3799 {
3800         int     err;
3801 
3802         /*
3803          * If the file system involved does not support
3804          * case-insensitive access and said access is requested, fail
3805          * quickly.
3806          */
3807         if (flags & FIGNORECASE &&
3808             ((vfs_has_feature(sdvp->v_vfsp, VFSFT_CASEINSENSITIVE) == 0 &&
3809             vfs_has_feature(sdvp->v_vfsp, VFSFT_NOCASESENSITIVE) == 0)))
3810                 return (EINVAL);
3811 
3812         VOPXID_MAP_CR(tdvp, cr);
3813 
3814         err = (*(sdvp)->v_op->vop_rename)(sdvp, snm, tdvp, tnm, cr, ct, flags);
3815         VOPSTATS_UPDATE(sdvp, rename);
3816         return (err);
3817 }
3818 
3819 int
3820 fop_mkdir(
3821         vnode_t *dvp,
3822         char *dirname,
3823         vattr_t *vap,
3824         vnode_t **vpp,
3825         cred_t *cr,
3826         caller_context_t *ct,
3827         int flags,
3828         vsecattr_t *vsecp)      /* ACL to set during create */
3829 {
3830         int ret;
3831 
3832         if (vsecp != NULL &&
3833             vfs_has_feature(dvp->v_vfsp, VFSFT_ACLONCREATE) == 0) {
3834                 return (EINVAL);
3835         }
3836         /*
3837          * If this file system doesn't support case-insensitive access
3838          * and said access is requested, fail quickly.
3839          */
3840         if (flags & FIGNORECASE &&
3841             (vfs_has_feature(dvp->v_vfsp, VFSFT_CASEINSENSITIVE) == 0 &&
3842             vfs_has_feature(dvp->v_vfsp, VFSFT_NOCASESENSITIVE) == 0))
3843                 return (EINVAL);
3844 
3845         VOPXID_MAP_CR(dvp, cr);
3846 
3847         ret = (*(dvp)->v_op->vop_mkdir)
3848             (dvp, dirname, vap, vpp, cr, ct, flags, vsecp);
3849         if (ret == 0 && *vpp) {
3850                 VOPSTATS_UPDATE(*vpp, mkdir);
3851                 vn_updatepath(dvp, *vpp, dirname);
3852         }
3853 
3854         return (ret);
3855 }
3856 
3857 int
3858 fop_rmdir(
3859         vnode_t *dvp,
3860         char *nm,
3861         vnode_t *cdir,
3862         cred_t *cr,
3863         caller_context_t *ct,
3864         int flags)
3865 {
3866         int     err;
3867 
3868         /*
3869          * If this file system doesn't support case-insensitive access
3870          * and said access is requested, fail quickly.
3871          */
3872         if (flags & FIGNORECASE &&
3873             (vfs_has_feature(dvp->v_vfsp, VFSFT_CASEINSENSITIVE) == 0 &&
3874             vfs_has_feature(dvp->v_vfsp, VFSFT_NOCASESENSITIVE) == 0))
3875                 return (EINVAL);
3876 
3877         VOPXID_MAP_CR(dvp, cr);
3878 
3879         err = (*(dvp)->v_op->vop_rmdir)(dvp, nm, cdir, cr, ct, flags);
3880         VOPSTATS_UPDATE(dvp, rmdir);
3881         return (err);
3882 }
3883 
3884 int
3885 fop_readdir(
3886         vnode_t *vp,
3887         uio_t *uiop,
3888         cred_t *cr,
3889         int *eofp,
3890         caller_context_t *ct,
3891         int flags)
3892 {
3893         int     err;
3894         ssize_t resid_start = uiop->uio_resid;
3895 
3896         /*
3897          * If this file system doesn't support retrieving directory
3898          * entry flags and said access is requested, fail quickly.
3899          */
3900         if (flags & V_RDDIR_ENTFLAGS &&
3901             vfs_has_feature(vp->v_vfsp, VFSFT_DIRENTFLAGS) == 0)
3902                 return (EINVAL);
3903 
3904         VOPXID_MAP_CR(vp, cr);
3905 
3906         err = (*(vp)->v_op->vop_readdir)(vp, uiop, cr, eofp, ct, flags);
3907         VOPSTATS_UPDATE_IO(vp, readdir,
3908             readdir_bytes, (resid_start - uiop->uio_resid));
3909         return (err);
3910 }
3911 
3912 int
3913 fop_symlink(
3914         vnode_t *dvp,
3915         char *linkname,
3916         vattr_t *vap,
3917         char *target,
3918         cred_t *cr,
3919         caller_context_t *ct,
3920         int flags)
3921 {
3922         int     err;
3923         xvattr_t xvattr;
3924 
3925         /*
3926          * If this file system doesn't support case-insensitive access
3927          * and said access is requested, fail quickly.
3928          */
3929         if (flags & FIGNORECASE &&
3930             (vfs_has_feature(dvp->v_vfsp, VFSFT_CASEINSENSITIVE) == 0 &&
3931             vfs_has_feature(dvp->v_vfsp, VFSFT_NOCASESENSITIVE) == 0))
3932                 return (EINVAL);
3933 
3934         VOPXID_MAP_CR(dvp, cr);
3935 
3936         /* check for reparse point */
3937         if ((vfs_has_feature(dvp->v_vfsp, VFSFT_REPARSE)) &&
3938             (strncmp(target, FS_REPARSE_TAG_STR,
3939             strlen(FS_REPARSE_TAG_STR)) == 0)) {
3940                 if (!fs_reparse_mark(target, vap, &xvattr))
3941                         vap = (vattr_t *)&xvattr;
3942         }
3943 
3944         err = (*(dvp)->v_op->vop_symlink)
3945             (dvp, linkname, vap, target, cr, ct, flags);
3946         VOPSTATS_UPDATE(dvp, symlink);
3947         return (err);
3948 }
3949 
3950 int
3951 fop_readlink(
3952         vnode_t *vp,
3953         uio_t *uiop,
3954         cred_t *cr,
3955         caller_context_t *ct)
3956 {
3957         int     err;
3958 
3959         VOPXID_MAP_CR(vp, cr);
3960 
3961         err = (*(vp)->v_op->vop_readlink)(vp, uiop, cr, ct);
3962         VOPSTATS_UPDATE(vp, readlink);
3963         return (err);
3964 }
3965 
3966 int
3967 fop_fsync(
3968         vnode_t *vp,
3969         int syncflag,
3970         cred_t *cr,
3971         caller_context_t *ct)
3972 {
3973         int     err;
3974 
3975         VOPXID_MAP_CR(vp, cr);
3976 
3977         err = (*(vp)->v_op->vop_fsync)(vp, syncflag, cr, ct);
3978         VOPSTATS_UPDATE(vp, fsync);
3979         return (err);
3980 }
3981 
3982 void
3983 fop_inactive(
3984         vnode_t *vp,
3985         cred_t *cr,
3986         caller_context_t *ct)
3987 {
3988         /* Need to update stats before vop call since we may lose the vnode */
3989         VOPSTATS_UPDATE(vp, inactive);
3990 
3991         VOPXID_MAP_CR(vp, cr);
3992 
3993         (*(vp)->v_op->vop_inactive)(vp, cr, ct);
3994 }
3995 
3996 int
3997 fop_fid(
3998         vnode_t *vp,
3999         fid_t *fidp,
4000         caller_context_t *ct)
4001 {
4002         int     err;
4003 
4004         err = (*(vp)->v_op->vop_fid)(vp, fidp, ct);
4005         VOPSTATS_UPDATE(vp, fid);
4006         return (err);
4007 }
4008 
4009 int
4010 fop_rwlock(
4011         vnode_t *vp,
4012         int write_lock,
4013         caller_context_t *ct)
4014 {
4015         int     ret;
4016 
4017         ret = ((*(vp)->v_op->vop_rwlock)(vp, write_lock, ct));
4018         VOPSTATS_UPDATE(vp, rwlock);
4019         return (ret);
4020 }
4021 
4022 void
4023 fop_rwunlock(
4024         vnode_t *vp,
4025         int write_lock,
4026         caller_context_t *ct)
4027 {
4028         (*(vp)->v_op->vop_rwunlock)(vp, write_lock, ct);
4029         VOPSTATS_UPDATE(vp, rwunlock);
4030 }
4031 
4032 int
4033 fop_seek(
4034         vnode_t *vp,
4035         offset_t ooff,
4036         offset_t *noffp,
4037         caller_context_t *ct)
4038 {
4039         int     err;
4040 
4041         err = (*(vp)->v_op->vop_seek)(vp, ooff, noffp, ct);
4042         VOPSTATS_UPDATE(vp, seek);
4043         return (err);
4044 }
4045 
4046 int
4047 fop_cmp(
4048         vnode_t *vp1,
4049         vnode_t *vp2,
4050         caller_context_t *ct)
4051 {
4052         int     err;
4053 
4054         err = (*(vp1)->v_op->vop_cmp)(vp1, vp2, ct);
4055         VOPSTATS_UPDATE(vp1, cmp);
4056         return (err);
4057 }
4058 
4059 int
4060 fop_frlock(
4061         vnode_t *vp,
4062         int cmd,
4063         flock64_t *bfp,
4064         int flag,
4065         offset_t offset,
4066         struct flk_callback *flk_cbp,
4067         cred_t *cr,
4068         caller_context_t *ct)
4069 {
4070         int     err;
4071 
4072         VOPXID_MAP_CR(vp, cr);
4073 
4074         err = (*(vp)->v_op->vop_frlock)
4075             (vp, cmd, bfp, flag, offset, flk_cbp, cr, ct);
4076         VOPSTATS_UPDATE(vp, frlock);
4077         return (err);
4078 }
4079 
4080 int
4081 fop_space(
4082         vnode_t *vp,
4083         int cmd,
4084         flock64_t *bfp,
4085         int flag,
4086         offset_t offset,
4087         cred_t *cr,
4088         caller_context_t *ct)
4089 {
4090         int     err;
4091 
4092         VOPXID_MAP_CR(vp, cr);
4093 
4094         err = (*(vp)->v_op->vop_space)(vp, cmd, bfp, flag, offset, cr, ct);
4095         VOPSTATS_UPDATE(vp, space);
4096         return (err);
4097 }
4098 
4099 int
4100 fop_realvp(
4101         vnode_t *vp,
4102         vnode_t **vpp,
4103         caller_context_t *ct)
4104 {
4105         int     err;
4106 
4107         err = (*(vp)->v_op->vop_realvp)(vp, vpp, ct);
4108         VOPSTATS_UPDATE(vp, realvp);
4109         return (err);
4110 }
4111 
4112 int
4113 fop_getpage(
4114         vnode_t *vp,
4115         offset_t off,
4116         size_t len,
4117         uint_t *protp,
4118         page_t **plarr,
4119         size_t plsz,
4120         struct seg *seg,
4121         caddr_t addr,
4122         enum seg_rw rw,
4123         cred_t *cr,
4124         caller_context_t *ct)
4125 {
4126         int     err;
4127 
4128         VOPXID_MAP_CR(vp, cr);
4129 
4130         err = (*(vp)->v_op->vop_getpage)
4131             (vp, off, len, protp, plarr, plsz, seg, addr, rw, cr, ct);
4132         VOPSTATS_UPDATE(vp, getpage);
4133         return (err);
4134 }
4135 
4136 int
4137 fop_putpage(
4138         vnode_t *vp,
4139         offset_t off,
4140         size_t len,
4141         int flags,
4142         cred_t *cr,
4143         caller_context_t *ct)
4144 {
4145         int     err;
4146 
4147         VOPXID_MAP_CR(vp, cr);
4148 
4149         err = (*(vp)->v_op->vop_putpage)(vp, off, len, flags, cr, ct);
4150         VOPSTATS_UPDATE(vp, putpage);
4151         return (err);
4152 }
4153 
4154 int
4155 fop_map(
4156         vnode_t *vp,
4157         offset_t off,
4158         struct as *as,
4159         caddr_t *addrp,
4160         size_t len,
4161         uchar_t prot,
4162         uchar_t maxprot,
4163         uint_t flags,
4164         cred_t *cr,
4165         caller_context_t *ct)
4166 {
4167         int     err;
4168 
4169         VOPXID_MAP_CR(vp, cr);
4170 
4171         err = (*(vp)->v_op->vop_map)
4172             (vp, off, as, addrp, len, prot, maxprot, flags, cr, ct);
4173         VOPSTATS_UPDATE(vp, map);
4174         return (err);
4175 }
4176 
4177 int
4178 fop_addmap(
4179         vnode_t *vp,
4180         offset_t off,
4181         struct as *as,
4182         caddr_t addr,
4183         size_t len,
4184         uchar_t prot,
4185         uchar_t maxprot,
4186         uint_t flags,
4187         cred_t *cr,
4188         caller_context_t *ct)
4189 {
4190         int error;
4191         u_longlong_t delta;
4192 
4193         VOPXID_MAP_CR(vp, cr);
4194 
4195         error = (*(vp)->v_op->vop_addmap)
4196             (vp, off, as, addr, len, prot, maxprot, flags, cr, ct);
4197 
4198         if ((!error) && (vp->v_type == VREG)) {
4199                 delta = (u_longlong_t)btopr(len);
4200                 /*
4201                  * If file is declared MAP_PRIVATE, it can't be written back
4202                  * even if open for write. Handle as read.
4203                  */
4204                 if (flags & MAP_PRIVATE) {
4205                         atomic_add_64((uint64_t *)(&(vp->v_mmap_read)),
4206                             (int64_t)delta);
4207                 } else {
4208                         /*
4209                          * atomic_add_64 forces the fetch of a 64 bit value to
4210                          * be atomic on 32 bit machines
4211                          */
4212                         if (maxprot & PROT_WRITE)
4213                                 atomic_add_64((uint64_t *)(&(vp->v_mmap_write)),
4214                                     (int64_t)delta);
4215                         if (maxprot & PROT_READ)
4216                                 atomic_add_64((uint64_t *)(&(vp->v_mmap_read)),
4217                                     (int64_t)delta);
4218                         if (maxprot & PROT_EXEC)
4219                                 atomic_add_64((uint64_t *)(&(vp->v_mmap_read)),
4220                                     (int64_t)delta);
4221                 }
4222         }
4223         VOPSTATS_UPDATE(vp, addmap);
4224         return (error);
4225 }
4226 
4227 int
4228 fop_delmap(
4229         vnode_t *vp,
4230         offset_t off,
4231         struct as *as,
4232         caddr_t addr,
4233         size_t len,
4234         uint_t prot,
4235         uint_t maxprot,
4236         uint_t flags,
4237         cred_t *cr,
4238         caller_context_t *ct)
4239 {
4240         int error;
4241         u_longlong_t delta;
4242 
4243         VOPXID_MAP_CR(vp, cr);
4244 
4245         error = (*(vp)->v_op->vop_delmap)
4246             (vp, off, as, addr, len, prot, maxprot, flags, cr, ct);
4247 
4248         /*
4249          * NFS calls into delmap twice, the first time
4250          * it simply establishes a callback mechanism and returns EAGAIN
4251          * while the real work is being done upon the second invocation.
4252          * We have to detect this here and only decrement the counts upon
4253          * the second delmap request.
4254          */
4255         if ((error != EAGAIN) && (vp->v_type == VREG)) {
4256 
4257                 delta = (u_longlong_t)btopr(len);
4258 
4259                 if (flags & MAP_PRIVATE) {
4260                         atomic_add_64((uint64_t *)(&(vp->v_mmap_read)),
4261                             (int64_t)(-delta));
4262                 } else {
4263                         /*
4264                          * atomic_add_64 forces the fetch of a 64 bit value
4265                          * to be atomic on 32 bit machines
4266                          */
4267                         if (maxprot & PROT_WRITE)
4268                                 atomic_add_64((uint64_t *)(&(vp->v_mmap_write)),
4269                                     (int64_t)(-delta));
4270                         if (maxprot & PROT_READ)
4271                                 atomic_add_64((uint64_t *)(&(vp->v_mmap_read)),
4272                                     (int64_t)(-delta));
4273                         if (maxprot & PROT_EXEC)
4274                                 atomic_add_64((uint64_t *)(&(vp->v_mmap_read)),
4275                                     (int64_t)(-delta));
4276                 }
4277         }
4278         VOPSTATS_UPDATE(vp, delmap);
4279         return (error);
4280 }
4281 
4282 
4283 int
4284 fop_poll(
4285         vnode_t *vp,
4286         short events,
4287         int anyyet,
4288         short *reventsp,
4289         struct pollhead **phpp,
4290         caller_context_t *ct)
4291 {
4292         int     err;
4293 
4294         err = (*(vp)->v_op->vop_poll)(vp, events, anyyet, reventsp, phpp, ct);
4295         VOPSTATS_UPDATE(vp, poll);
4296         return (err);
4297 }
4298 
4299 int
4300 fop_dump(
4301         vnode_t *vp,
4302         caddr_t addr,
4303         offset_t lbdn,
4304         offset_t dblks,
4305         caller_context_t *ct)
4306 {
4307         int     err;
4308 
4309         /* ensure lbdn and dblks can be passed safely to bdev_dump */
4310         if ((lbdn != (daddr_t)lbdn) || (dblks != (int)dblks))
4311                 return (EIO);
4312 
4313         err = (*(vp)->v_op->vop_dump)(vp, addr, lbdn, dblks, ct);
4314         VOPSTATS_UPDATE(vp, dump);
4315         return (err);
4316 }
4317 
4318 int
4319 fop_pathconf(
4320         vnode_t *vp,
4321         int cmd,
4322         ulong_t *valp,
4323         cred_t *cr,
4324         caller_context_t *ct)
4325 {
4326         int     err;
4327 
4328         VOPXID_MAP_CR(vp, cr);
4329 
4330         err = (*(vp)->v_op->vop_pathconf)(vp, cmd, valp, cr, ct);
4331         VOPSTATS_UPDATE(vp, pathconf);
4332         return (err);
4333 }
4334 
4335 int
4336 fop_pageio(
4337         vnode_t *vp,
4338         struct page *pp,
4339         u_offset_t io_off,
4340         size_t io_len,
4341         int flags,
4342         cred_t *cr,
4343         caller_context_t *ct)
4344 {
4345         int     err;
4346 
4347         VOPXID_MAP_CR(vp, cr);
4348 
4349         err = (*(vp)->v_op->vop_pageio)(vp, pp, io_off, io_len, flags, cr, ct);
4350         VOPSTATS_UPDATE(vp, pageio);
4351         return (err);
4352 }
4353 
4354 int
4355 fop_dumpctl(
4356         vnode_t *vp,
4357         int action,
4358         offset_t *blkp,
4359         caller_context_t *ct)
4360 {
4361         int     err;
4362         err = (*(vp)->v_op->vop_dumpctl)(vp, action, blkp, ct);
4363         VOPSTATS_UPDATE(vp, dumpctl);
4364         return (err);
4365 }
4366 
4367 void
4368 fop_dispose(
4369         vnode_t *vp,
4370         page_t *pp,
4371         int flag,
4372         int dn,
4373         cred_t *cr,
4374         caller_context_t *ct)
4375 {
4376         /* Must do stats first since it's possible to lose the vnode */
4377         VOPSTATS_UPDATE(vp, dispose);
4378 
4379         VOPXID_MAP_CR(vp, cr);
4380 
4381         (*(vp)->v_op->vop_dispose)(vp, pp, flag, dn, cr, ct);
4382 }
4383 
4384 int
4385 fop_setsecattr(
4386         vnode_t *vp,
4387         vsecattr_t *vsap,
4388         int flag,
4389         cred_t *cr,
4390         caller_context_t *ct)
4391 {
4392         int     err;
4393 
4394         VOPXID_MAP_CR(vp, cr);
4395 
4396         /*
4397          * We're only allowed to skip the ACL check iff we used a 32 bit
4398          * ACE mask with VOP_ACCESS() to determine permissions.
4399          */
4400         if ((flag & ATTR_NOACLCHECK) &&
4401             vfs_has_feature(vp->v_vfsp, VFSFT_ACEMASKONACCESS) == 0) {
4402                 return (EINVAL);
4403         }
4404         err = (*(vp)->v_op->vop_setsecattr) (vp, vsap, flag, cr, ct);
4405         VOPSTATS_UPDATE(vp, setsecattr);
4406         return (err);
4407 }
4408 
4409 int
4410 fop_getsecattr(
4411         vnode_t *vp,
4412         vsecattr_t *vsap,
4413         int flag,
4414         cred_t *cr,
4415         caller_context_t *ct)
4416 {
4417         int     err;
4418 
4419         /*
4420          * We're only allowed to skip the ACL check iff we used a 32 bit
4421          * ACE mask with VOP_ACCESS() to determine permissions.
4422          */
4423         if ((flag & ATTR_NOACLCHECK) &&
4424             vfs_has_feature(vp->v_vfsp, VFSFT_ACEMASKONACCESS) == 0) {
4425                 return (EINVAL);
4426         }
4427 
4428         VOPXID_MAP_CR(vp, cr);
4429 
4430         err = (*(vp)->v_op->vop_getsecattr) (vp, vsap, flag, cr, ct);
4431         VOPSTATS_UPDATE(vp, getsecattr);
4432         return (err);
4433 }
4434 
4435 int
4436 fop_shrlock(
4437         vnode_t *vp,
4438         int cmd,
4439         struct shrlock *shr,
4440         int flag,
4441         cred_t *cr,
4442         caller_context_t *ct)
4443 {
4444         int     err;
4445 
4446         VOPXID_MAP_CR(vp, cr);
4447 
4448         err = (*(vp)->v_op->vop_shrlock)(vp, cmd, shr, flag, cr, ct);
4449         VOPSTATS_UPDATE(vp, shrlock);
4450         return (err);
4451 }
4452 
4453 int
4454 fop_vnevent(vnode_t *vp, vnevent_t vnevent, vnode_t *dvp, char *fnm,
4455     caller_context_t *ct)
4456 {
4457         int     err;
4458 
4459         err = (*(vp)->v_op->vop_vnevent)(vp, vnevent, dvp, fnm, ct);
4460         VOPSTATS_UPDATE(vp, vnevent);
4461         return (err);
4462 }
4463 
4464 int
4465 fop_reqzcbuf(vnode_t *vp, enum uio_rw ioflag, xuio_t *uiop, cred_t *cr,
4466     caller_context_t *ct)
4467 {
4468         int err;
4469 
4470         if (vfs_has_feature(vp->v_vfsp, VFSFT_ZEROCOPY_SUPPORTED) == 0)
4471                 return (ENOTSUP);
4472         err = (*(vp)->v_op->vop_reqzcbuf)(vp, ioflag, uiop, cr, ct);
4473         VOPSTATS_UPDATE(vp, reqzcbuf);
4474         return (err);
4475 }
4476 
4477 int
4478 fop_retzcbuf(vnode_t *vp, xuio_t *uiop, cred_t *cr, caller_context_t *ct)
4479 {
4480         int err;
4481 
4482         if (vfs_has_feature(vp->v_vfsp, VFSFT_ZEROCOPY_SUPPORTED) == 0)
4483                 return (ENOTSUP);
4484         err = (*(vp)->v_op->vop_retzcbuf)(vp, uiop, cr, ct);
4485         VOPSTATS_UPDATE(vp, retzcbuf);
4486         return (err);
4487 }
4488 
4489 /*
4490  * Default destructor
4491  *      Needed because NULL destructor means that the key is unused
4492  */
4493 /* ARGSUSED */
4494 void
4495 vsd_defaultdestructor(void *value)
4496 {}
4497 
4498 /*
4499  * Create a key (index into per vnode array)
4500  *      Locks out vsd_create, vsd_destroy, and vsd_free
4501  *      May allocate memory with lock held
4502  */
4503 void
4504 vsd_create(uint_t *keyp, void (*destructor)(void *))
4505 {
4506         int     i;
4507         uint_t  nkeys;
4508 
4509         /*
4510          * if key is allocated, do nothing
4511          */
4512         mutex_enter(&vsd_lock);
4513         if (*keyp) {
4514                 mutex_exit(&vsd_lock);
4515                 return;
4516         }
4517         /*
4518          * find an unused key
4519          */
4520         if (destructor == NULL)
4521                 destructor = vsd_defaultdestructor;
4522 
4523         for (i = 0; i < vsd_nkeys; ++i)
4524                 if (vsd_destructor[i] == NULL)
4525                         break;
4526 
4527         /*
4528          * if no unused keys, increase the size of the destructor array
4529          */
4530         if (i == vsd_nkeys) {
4531                 if ((nkeys = (vsd_nkeys << 1)) == 0)
4532                         nkeys = 1;
4533                 vsd_destructor =
4534                     (void (**)(void *))vsd_realloc((void *)vsd_destructor,
4535                     (size_t)(vsd_nkeys * sizeof (void (*)(void *))),
4536                     (size_t)(nkeys * sizeof (void (*)(void *))));
4537                 vsd_nkeys = nkeys;
4538         }
4539 
4540         /*
4541          * allocate the next available unused key
4542          */
4543         vsd_destructor[i] = destructor;
4544         *keyp = i + 1;
4545 
4546         /* create vsd_list, if it doesn't exist */
4547         if (vsd_list == NULL) {
4548                 vsd_list = kmem_alloc(sizeof (list_t), KM_SLEEP);
4549                 list_create(vsd_list, sizeof (struct vsd_node),
4550                     offsetof(struct vsd_node, vs_nodes));
4551         }
4552 
4553         mutex_exit(&vsd_lock);
4554 }
4555 
4556 /*
4557  * Destroy a key
4558  *
4559  * Assumes that the caller is preventing vsd_set and vsd_get
4560  * Locks out vsd_create, vsd_destroy, and vsd_free
4561  * May free memory with lock held
4562  */
4563 void
4564 vsd_destroy(uint_t *keyp)
4565 {
4566         uint_t key;
4567         struct vsd_node *vsd;
4568 
4569         /*
4570          * protect the key namespace and our destructor lists
4571          */
4572         mutex_enter(&vsd_lock);
4573         key = *keyp;
4574         *keyp = 0;
4575 
4576         ASSERT(key <= vsd_nkeys);
4577 
4578         /*
4579          * if the key is valid
4580          */
4581         if (key != 0) {
4582                 uint_t k = key - 1;
4583                 /*
4584                  * for every vnode with VSD, call key's destructor
4585                  */
4586                 for (vsd = list_head(vsd_list); vsd != NULL;
4587                     vsd = list_next(vsd_list, vsd)) {
4588                         /*
4589                          * no VSD for key in this vnode
4590                          */
4591                         if (key > vsd->vs_nkeys)
4592                                 continue;
4593                         /*
4594                          * call destructor for key
4595                          */
4596                         if (vsd->vs_value[k] && vsd_destructor[k])
4597                                 (*vsd_destructor[k])(vsd->vs_value[k]);
4598                         /*
4599                          * reset value for key
4600                          */
4601                         vsd->vs_value[k] = NULL;
4602                 }
4603                 /*
4604                  * actually free the key (NULL destructor == unused)
4605                  */
4606                 vsd_destructor[k] = NULL;
4607         }
4608 
4609         mutex_exit(&vsd_lock);
4610 }
4611 
4612 /*
4613  * Quickly return the per vnode value that was stored with the specified key
4614  * Assumes the caller is protecting key from vsd_create and vsd_destroy
4615  * Assumes the caller is holding v_vsd_lock to protect the vsd.
4616  */
4617 void *
4618 vsd_get(vnode_t *vp, uint_t key)
4619 {
4620         struct vsd_node *vsd;
4621 
4622         ASSERT(vp != NULL);
4623         ASSERT(mutex_owned(&vp->v_vsd_lock));
4624 
4625         vsd = vp->v_vsd;
4626 
4627         if (key && vsd != NULL && key <= vsd->vs_nkeys)
4628                 return (vsd->vs_value[key - 1]);
4629         return (NULL);
4630 }
4631 
4632 /*
4633  * Set a per vnode value indexed with the specified key
4634  * Assumes the caller is holding v_vsd_lock to protect the vsd.
4635  */
4636 int
4637 vsd_set(vnode_t *vp, uint_t key, void *value)
4638 {
4639         struct vsd_node *vsd;
4640 
4641         ASSERT(vp != NULL);
4642         ASSERT(mutex_owned(&vp->v_vsd_lock));
4643 
4644         if (key == 0)
4645                 return (EINVAL);
4646 
4647         vsd = vp->v_vsd;
4648         if (vsd == NULL)
4649                 vsd = vp->v_vsd = kmem_zalloc(sizeof (*vsd), KM_SLEEP);
4650 
4651         /*
4652          * If the vsd was just allocated, vs_nkeys will be 0, so the following
4653          * code won't happen and we will continue down and allocate space for
4654          * the vs_value array.
4655          * If the caller is replacing one value with another, then it is up
4656          * to the caller to free/rele/destroy the previous value (if needed).
4657          */
4658         if (key <= vsd->vs_nkeys) {
4659                 vsd->vs_value[key - 1] = value;
4660                 return (0);
4661         }
4662 
4663         ASSERT(key <= vsd_nkeys);
4664 
4665         if (vsd->vs_nkeys == 0) {
4666                 mutex_enter(&vsd_lock);     /* lock out vsd_destroy() */
4667                 /*
4668                  * Link onto list of all VSD nodes.
4669                  */
4670                 list_insert_head(vsd_list, vsd);
4671                 mutex_exit(&vsd_lock);
4672         }
4673 
4674         /*
4675          * Allocate vnode local storage and set the value for key
4676          */
4677         vsd->vs_value = vsd_realloc(vsd->vs_value,
4678             vsd->vs_nkeys * sizeof (void *),
4679             key * sizeof (void *));
4680         vsd->vs_nkeys = key;
4681         vsd->vs_value[key - 1] = value;
4682 
4683         return (0);
4684 }
4685 
4686 /*
4687  * Called from vn_free() to run the destructor function for each vsd
4688  *      Locks out vsd_create and vsd_destroy
4689  *      Assumes that the destructor *DOES NOT* use vsd
4690  */
4691 void
4692 vsd_free(vnode_t *vp)
4693 {
4694         int i;
4695         struct vsd_node *vsd = vp->v_vsd;
4696 
4697         if (vsd == NULL)
4698                 return;
4699 
4700         if (vsd->vs_nkeys == 0) {
4701                 kmem_free(vsd, sizeof (*vsd));
4702                 vp->v_vsd = NULL;
4703                 return;
4704         }
4705 
4706         /*
4707          * lock out vsd_create and vsd_destroy, call
4708          * the destructor, and mark the value as destroyed.
4709          */
4710         mutex_enter(&vsd_lock);
4711 
4712         for (i = 0; i < vsd->vs_nkeys; i++) {
4713                 if (vsd->vs_value[i] && vsd_destructor[i])
4714                         (*vsd_destructor[i])(vsd->vs_value[i]);
4715                 vsd->vs_value[i] = NULL;
4716         }
4717 
4718         /*
4719          * remove from linked list of VSD nodes
4720          */
4721         list_remove(vsd_list, vsd);
4722 
4723         mutex_exit(&vsd_lock);
4724 
4725         /*
4726          * free up the VSD
4727          */
4728         kmem_free(vsd->vs_value, vsd->vs_nkeys * sizeof (void *));
4729         kmem_free(vsd, sizeof (struct vsd_node));
4730         vp->v_vsd = NULL;
4731 }
4732 
4733 /*
4734  * realloc
4735  */
4736 static void *
4737 vsd_realloc(void *old, size_t osize, size_t nsize)
4738 {
4739         void *new;
4740 
4741         new = kmem_zalloc(nsize, KM_SLEEP);
4742         if (old) {
4743                 bcopy(old, new, osize);
4744                 kmem_free(old, osize);
4745         }
4746         return (new);
4747 }
4748 
4749 /*
4750  * Setup the extensible system attribute for creating a reparse point.
4751  * The symlink data 'target' is validated for proper format of a reparse
4752  * string and a check also made to make sure the symlink data does not
4753  * point to an existing file.
4754  *
4755  * return 0 if ok else -1.
4756  */
4757 static int
4758 fs_reparse_mark(char *target, vattr_t *vap, xvattr_t *xvattr)
4759 {
4760         xoptattr_t *xoap;
4761 
4762         if ((!target) || (!vap) || (!xvattr))
4763                 return (-1);
4764 
4765         /* validate reparse string */
4766         if (reparse_validate((const char *)target))
4767                 return (-1);
4768 
4769         xva_init(xvattr);
4770         xvattr->xva_vattr = *vap;
4771         xvattr->xva_vattr.va_mask |= AT_XVATTR;
4772         xoap = xva_getxoptattr(xvattr);
4773         ASSERT(xoap);
4774         XVA_SET_REQ(xvattr, XAT_REPARSE);
4775         xoap->xoa_reparse = 1;
4776 
4777         return (0);
4778 }
4779 
4780 /*
4781  * Function to check whether a symlink is a reparse point.
4782  * Return B_TRUE if it is a reparse point, else return B_FALSE
4783  */
4784 boolean_t
4785 vn_is_reparse(vnode_t *vp, cred_t *cr, caller_context_t *ct)
4786 {
4787         xvattr_t xvattr;
4788         xoptattr_t *xoap;
4789 
4790         if ((vp->v_type != VLNK) ||
4791             !(vfs_has_feature(vp->v_vfsp, VFSFT_XVATTR)))
4792                 return (B_FALSE);
4793 
4794         xva_init(&xvattr);
4795         xoap = xva_getxoptattr(&xvattr);
4796         ASSERT(xoap);
4797         XVA_SET_REQ(&xvattr, XAT_REPARSE);
4798 
4799         if (VOP_GETATTR(vp, &xvattr.xva_vattr, 0, cr, ct))
4800                 return (B_FALSE);
4801 
4802         if ((!(xvattr.xva_vattr.va_mask & AT_XVATTR)) ||
4803             (!(XVA_ISSET_RTN(&xvattr, XAT_REPARSE))))
4804                 return (B_FALSE);
4805 
4806         return (xoap->xoa_reparse ? B_TRUE : B_FALSE);
4807 }