1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 /*
  22  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
  23  * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
  24  * Copyright 2014 Nexenta Systems, Inc.  All rights reserved.
  25  * Copyright (c) 2014 Integros [integros.com]
  26  * Copyright 2016 Joyent, Inc.
  27  */
  28 
  29 /* Portions Copyright 2007 Jeremy Teo */
  30 /* Portions Copyright 2010 Robert Milkowski */
  31 
  32 #include <sys/types.h>
  33 #include <sys/param.h>
  34 #include <sys/time.h>
  35 #include <sys/systm.h>
  36 #include <sys/sysmacros.h>
  37 #include <sys/resource.h>
  38 #include <sys/vfs.h>
  39 #include <sys/vfs_opreg.h>
  40 #include <sys/vnode.h>
  41 #include <sys/file.h>
  42 #include <sys/stat.h>
  43 #include <sys/kmem.h>
  44 #include <sys/taskq.h>
  45 #include <sys/uio.h>
  46 #include <sys/vmsystm.h>
  47 #include <sys/atomic.h>
  48 #include <sys/vm.h>
  49 #include <vm/seg_vn.h>
  50 #include <vm/pvn.h>
  51 #include <vm/as.h>
  52 #include <vm/kpm.h>
  53 #include <vm/seg_kpm.h>
  54 #include <sys/mman.h>
  55 #include <sys/pathname.h>
  56 #include <sys/cmn_err.h>
  57 #include <sys/errno.h>
  58 #include <sys/unistd.h>
  59 #include <sys/zfs_dir.h>
  60 #include <sys/zfs_acl.h>
  61 #include <sys/zfs_ioctl.h>
  62 #include <sys/fs/zfs.h>
  63 #include <sys/dmu.h>
  64 #include <sys/dmu_objset.h>
  65 #include <sys/spa.h>
  66 #include <sys/txg.h>
  67 #include <sys/dbuf.h>
  68 #include <sys/zap.h>
  69 #include <sys/sa.h>
  70 #include <sys/dirent.h>
  71 #include <sys/policy.h>
  72 #include <sys/sunddi.h>
  73 #include <sys/filio.h>
  74 #include <sys/sid.h>
  75 #include "fs/fs_subr.h"
  76 #include <sys/zfs_ctldir.h>
  77 #include <sys/zfs_fuid.h>
  78 #include <sys/zfs_sa.h>
  79 #include <sys/dnlc.h>
  80 #include <sys/zfs_rlock.h>
  81 #include <sys/extdirent.h>
  82 #include <sys/kidmap.h>
  83 #include <sys/cred.h>
  84 #include <sys/attr.h>
  85 
  86 /*
  87  * Programming rules.
  88  *
  89  * Each vnode op performs some logical unit of work.  To do this, the ZPL must
  90  * properly lock its in-core state, create a DMU transaction, do the work,
  91  * record this work in the intent log (ZIL), commit the DMU transaction,
  92  * and wait for the intent log to commit if it is a synchronous operation.
  93  * Moreover, the vnode ops must work in both normal and log replay context.
  94  * The ordering of events is important to avoid deadlocks and references
  95  * to freed memory.  The example below illustrates the following Big Rules:
  96  *
  97  *  (1) A check must be made in each zfs thread for a mounted file system.
  98  *      This is done avoiding races using ZFS_ENTER(zfsvfs).
  99  *      A ZFS_EXIT(zfsvfs) is needed before all returns.  Any znodes
 100  *      must be checked with ZFS_VERIFY_ZP(zp).  Both of these macros
 101  *      can return EIO from the calling function.
 102  *
 103  *  (2) VN_RELE() should always be the last thing except for zil_commit()
 104  *      (if necessary) and ZFS_EXIT(). This is for 3 reasons:
 105  *      First, if it's the last reference, the vnode/znode
 106  *      can be freed, so the zp may point to freed memory.  Second, the last
 107  *      reference will call zfs_zinactive(), which may induce a lot of work --
 108  *      pushing cached pages (which acquires range locks) and syncing out
 109  *      cached atime changes.  Third, zfs_zinactive() may require a new tx,
 110  *      which could deadlock the system if you were already holding one.
 111  *      If you must call VN_RELE() within a tx then use VN_RELE_ASYNC().
 112  *
 113  *  (3) All range locks must be grabbed before calling dmu_tx_assign(),
 114  *      as they can span dmu_tx_assign() calls.
 115  *
 116  *  (4) If ZPL locks are held, pass TXG_NOWAIT as the second argument to
 117  *      dmu_tx_assign().  This is critical because we don't want to block
 118  *      while holding locks.
 119  *
 120  *      If no ZPL locks are held (aside from ZFS_ENTER()), use TXG_WAIT.  This
 121  *      reduces lock contention and CPU usage when we must wait (note that if
 122  *      throughput is constrained by the storage, nearly every transaction
 123  *      must wait).
 124  *
 125  *      Note, in particular, that if a lock is sometimes acquired before
 126  *      the tx assigns, and sometimes after (e.g. z_lock), then failing
 127  *      to use a non-blocking assign can deadlock the system.  The scenario:
 128  *
 129  *      Thread A has grabbed a lock before calling dmu_tx_assign().
 130  *      Thread B is in an already-assigned tx, and blocks for this lock.
 131  *      Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open()
 132  *      forever, because the previous txg can't quiesce until B's tx commits.
 133  *
 134  *      If dmu_tx_assign() returns ERESTART and zfsvfs->z_assign is TXG_NOWAIT,
 135  *      then drop all locks, call dmu_tx_wait(), and try again.  On subsequent
 136  *      calls to dmu_tx_assign(), pass TXG_WAITED rather than TXG_NOWAIT,
 137  *      to indicate that this operation has already called dmu_tx_wait().
 138  *      This will ensure that we don't retry forever, waiting a short bit
 139  *      each time.
 140  *
 141  *  (5) If the operation succeeded, generate the intent log entry for it
 142  *      before dropping locks.  This ensures that the ordering of events
 143  *      in the intent log matches the order in which they actually occurred.
 144  *      During ZIL replay the zfs_log_* functions will update the sequence
 145  *      number to indicate the zil transaction has replayed.
 146  *
 147  *  (6) At the end of each vnode op, the DMU tx must always commit,
 148  *      regardless of whether there were any errors.
 149  *
 150  *  (7) After dropping all locks, invoke zil_commit(zilog, foid)
 151  *      to ensure that synchronous semantics are provided when necessary.
 152  *
 153  * In general, this is how things should be ordered in each vnode op:
 154  *
 155  *      ZFS_ENTER(zfsvfs);              // exit if unmounted
 156  * top:
 157  *      zfs_dirent_lock(&dl, ...)   // lock directory entry (may VN_HOLD())
 158  *      rw_enter(...);                  // grab any other locks you need
 159  *      tx = dmu_tx_create(...);        // get DMU tx
 160  *      dmu_tx_hold_*();                // hold each object you might modify
 161  *      error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
 162  *      if (error) {
 163  *              rw_exit(...);           // drop locks
 164  *              zfs_dirent_unlock(dl);  // unlock directory entry
 165  *              VN_RELE(...);           // release held vnodes
 166  *              if (error == ERESTART) {
 167  *                      waited = B_TRUE;
 168  *                      dmu_tx_wait(tx);
 169  *                      dmu_tx_abort(tx);
 170  *                      goto top;
 171  *              }
 172  *              dmu_tx_abort(tx);       // abort DMU tx
 173  *              ZFS_EXIT(zfsvfs);       // finished in zfs
 174  *              return (error);         // really out of space
 175  *      }
 176  *      error = do_real_work();         // do whatever this VOP does
 177  *      if (error == 0)
 178  *              zfs_log_*(...);         // on success, make ZIL entry
 179  *      dmu_tx_commit(tx);              // commit DMU tx -- error or not
 180  *      rw_exit(...);                   // drop locks
 181  *      zfs_dirent_unlock(dl);          // unlock directory entry
 182  *      VN_RELE(...);                   // release held vnodes
 183  *      zil_commit(zilog, foid);        // synchronous when necessary
 184  *      ZFS_EXIT(zfsvfs);               // finished in zfs
 185  *      return (error);                 // done, report error
 186  */
 187 
 188 /* ARGSUSED */
 189 static int
 190 zfs_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct)
 191 {
 192         znode_t *zp = VTOZ(*vpp);
 193         zfsvfs_t *zfsvfs = zp->z_zfsvfs;
 194 
 195         ZFS_ENTER(zfsvfs);
 196         ZFS_VERIFY_ZP(zp);
 197 
 198         if ((flag & FWRITE) && (zp->z_pflags & ZFS_APPENDONLY) &&
 199             ((flag & FAPPEND) == 0)) {
 200                 ZFS_EXIT(zfsvfs);
 201                 return (SET_ERROR(EPERM));
 202         }
 203 
 204         if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
 205             ZTOV(zp)->v_type == VREG &&
 206             !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0) {
 207                 if (fs_vscan(*vpp, cr, 0) != 0) {
 208                         ZFS_EXIT(zfsvfs);
 209                         return (SET_ERROR(EACCES));
 210                 }
 211         }
 212 
 213         /* Keep a count of the synchronous opens in the znode */
 214         if (flag & (FSYNC | FDSYNC))
 215                 atomic_inc_32(&zp->z_sync_cnt);
 216 
 217         ZFS_EXIT(zfsvfs);
 218         return (0);
 219 }
 220 
 221 /* ARGSUSED */
 222 static int
 223 zfs_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr,
 224     caller_context_t *ct)
 225 {
 226         znode_t *zp = VTOZ(vp);
 227         zfsvfs_t *zfsvfs = zp->z_zfsvfs;
 228 
 229         /*
 230          * Clean up any locks held by this process on the vp.
 231          */
 232         cleanlocks(vp, ddi_get_pid(), 0);
 233         cleanshares(vp, ddi_get_pid());
 234 
 235         ZFS_ENTER(zfsvfs);
 236         ZFS_VERIFY_ZP(zp);
 237 
 238         /* Decrement the synchronous opens in the znode */
 239         if ((flag & (FSYNC | FDSYNC)) && (count == 1))
 240                 atomic_dec_32(&zp->z_sync_cnt);
 241 
 242         if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
 243             ZTOV(zp)->v_type == VREG &&
 244             !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0)
 245                 VERIFY(fs_vscan(vp, cr, 1) == 0);
 246 
 247         ZFS_EXIT(zfsvfs);
 248         return (0);
 249 }
 250 
 251 /*
 252  * Lseek support for finding holes (cmd == _FIO_SEEK_HOLE) and
 253  * data (cmd == _FIO_SEEK_DATA). "off" is an in/out parameter.
 254  */
 255 static int
 256 zfs_holey(vnode_t *vp, int cmd, offset_t *off)
 257 {
 258         znode_t *zp = VTOZ(vp);
 259         uint64_t noff = (uint64_t)*off; /* new offset */
 260         uint64_t file_sz;
 261         int error;
 262         boolean_t hole;
 263 
 264         file_sz = zp->z_size;
 265         if (noff >= file_sz)  {
 266                 return (SET_ERROR(ENXIO));
 267         }
 268 
 269         if (cmd == _FIO_SEEK_HOLE)
 270                 hole = B_TRUE;
 271         else
 272                 hole = B_FALSE;
 273 
 274         error = dmu_offset_next(zp->z_zfsvfs->z_os, zp->z_id, hole, &noff);
 275 
 276         if (error == ESRCH)
 277                 return (SET_ERROR(ENXIO));
 278 
 279         /*
 280          * We could find a hole that begins after the logical end-of-file,
 281          * because dmu_offset_next() only works on whole blocks.  If the
 282          * EOF falls mid-block, then indicate that the "virtual hole"
 283          * at the end of the file begins at the logical EOF, rather than
 284          * at the end of the last block.
 285          */
 286         if (noff > file_sz) {
 287                 ASSERT(hole);
 288                 noff = file_sz;
 289         }
 290 
 291         if (noff < *off)
 292                 return (error);
 293         *off = noff;
 294         return (error);
 295 }
 296 
 297 /* ARGSUSED */
 298 static int
 299 zfs_ioctl(vnode_t *vp, int com, intptr_t data, int flag, cred_t *cred,
 300     int *rvalp, caller_context_t *ct)
 301 {
 302         offset_t off;
 303         offset_t ndata;
 304         dmu_object_info_t doi;
 305         int error;
 306         zfsvfs_t *zfsvfs;
 307         znode_t *zp;
 308 
 309         switch (com) {
 310         case _FIOFFS:
 311         {
 312                 return (zfs_sync(vp->v_vfsp, 0, cred));
 313 
 314                 /*
 315                  * The following two ioctls are used by bfu.  Faking out,
 316                  * necessary to avoid bfu errors.
 317                  */
 318         }
 319         case _FIOGDIO:
 320         case _FIOSDIO:
 321         {
 322                 return (0);
 323         }
 324 
 325         case _FIO_SEEK_DATA:
 326         case _FIO_SEEK_HOLE:
 327         {
 328                 if (ddi_copyin((void *)data, &off, sizeof (off), flag))
 329                         return (SET_ERROR(EFAULT));
 330 
 331                 zp = VTOZ(vp);
 332                 zfsvfs = zp->z_zfsvfs;
 333                 ZFS_ENTER(zfsvfs);
 334                 ZFS_VERIFY_ZP(zp);
 335 
 336                 /* offset parameter is in/out */
 337                 error = zfs_holey(vp, com, &off);
 338                 ZFS_EXIT(zfsvfs);
 339                 if (error)
 340                         return (error);
 341                 if (ddi_copyout(&off, (void *)data, sizeof (off), flag))
 342                         return (SET_ERROR(EFAULT));
 343                 return (0);
 344         }
 345         case _FIO_COUNT_FILLED:
 346         {
 347                 /*
 348                  * _FIO_COUNT_FILLED adds a new ioctl command which
 349                  * exposes the number of filled blocks in a
 350                  * ZFS object.
 351                  */
 352                 zp = VTOZ(vp);
 353                 zfsvfs = zp->z_zfsvfs;
 354                 ZFS_ENTER(zfsvfs);
 355                 ZFS_VERIFY_ZP(zp);
 356 
 357                 /*
 358                  * Wait for all dirty blocks for this object
 359                  * to get synced out to disk, and the DMU info
 360                  * updated.
 361                  */
 362                 error = dmu_object_wait_synced(zfsvfs->z_os, zp->z_id);
 363                 if (error) {
 364                         ZFS_EXIT(zfsvfs);
 365                         return (error);
 366                 }
 367 
 368                 /*
 369                  * Retrieve fill count from DMU object.
 370                  */
 371                 error = dmu_object_info(zfsvfs->z_os, zp->z_id, &doi);
 372                 if (error) {
 373                         ZFS_EXIT(zfsvfs);
 374                         return (error);
 375                 }
 376 
 377                 ndata = doi.doi_fill_count;
 378 
 379                 ZFS_EXIT(zfsvfs);
 380                 if (ddi_copyout(&ndata, (void *)data, sizeof (ndata), flag))
 381                         return (SET_ERROR(EFAULT));
 382                 return (0);
 383         }
 384         }
 385         return (SET_ERROR(ENOTTY));
 386 }
 387 
 388 /*
 389  * Utility functions to map and unmap a single physical page.  These
 390  * are used to manage the mappable copies of ZFS file data, and therefore
 391  * do not update ref/mod bits.
 392  */
 393 caddr_t
 394 zfs_map_page(page_t *pp, enum seg_rw rw)
 395 {
 396         if (kpm_enable)
 397                 return (hat_kpm_mapin(pp, 0));
 398         ASSERT(rw == S_READ || rw == S_WRITE);
 399         return (ppmapin(pp, PROT_READ | ((rw == S_WRITE) ? PROT_WRITE : 0),
 400             (caddr_t)-1));
 401 }
 402 
 403 void
 404 zfs_unmap_page(page_t *pp, caddr_t addr)
 405 {
 406         if (kpm_enable) {
 407                 hat_kpm_mapout(pp, 0, addr);
 408         } else {
 409                 ppmapout(addr);
 410         }
 411 }
 412 
 413 /*
 414  * When a file is memory mapped, we must keep the IO data synchronized
 415  * between the DMU cache and the memory mapped pages.  What this means:
 416  *
 417  * On Write:    If we find a memory mapped page, we write to *both*
 418  *              the page and the dmu buffer.
 419  */
 420 static void
 421 update_pages(vnode_t *vp, int64_t start, int len, objset_t *os, uint64_t oid)
 422 {
 423         int64_t off;
 424 
 425         off = start & PAGEOFFSET;
 426         for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
 427                 page_t *pp;
 428                 uint64_t nbytes = MIN(PAGESIZE - off, len);
 429 
 430                 if (pp = page_lookup(vp, start, SE_SHARED)) {
 431                         caddr_t va;
 432 
 433                         va = zfs_map_page(pp, S_WRITE);
 434                         (void) dmu_read(os, oid, start+off, nbytes, va+off,
 435                             DMU_READ_PREFETCH);
 436                         zfs_unmap_page(pp, va);
 437                         page_unlock(pp);
 438                 }
 439                 len -= nbytes;
 440                 off = 0;
 441         }
 442 }
 443 
 444 /*
 445  * When a file is memory mapped, we must keep the IO data synchronized
 446  * between the DMU cache and the memory mapped pages.  What this means:
 447  *
 448  * On Read:     We "read" preferentially from memory mapped pages,
 449  *              else we default from the dmu buffer.
 450  *
 451  * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
 452  *       the file is memory mapped.
 453  */
 454 static int
 455 mappedread(vnode_t *vp, int nbytes, uio_t *uio)
 456 {
 457         znode_t *zp = VTOZ(vp);
 458         int64_t start, off;
 459         int len = nbytes;
 460         int error = 0;
 461 
 462         start = uio->uio_loffset;
 463         off = start & PAGEOFFSET;
 464         for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
 465                 page_t *pp;
 466                 uint64_t bytes = MIN(PAGESIZE - off, len);
 467 
 468                 if (pp = page_lookup(vp, start, SE_SHARED)) {
 469                         caddr_t va;
 470 
 471                         va = zfs_map_page(pp, S_READ);
 472                         error = uiomove(va + off, bytes, UIO_READ, uio);
 473                         zfs_unmap_page(pp, va);
 474                         page_unlock(pp);
 475                 } else {
 476                         error = dmu_read_uio_dbuf(sa_get_db(zp->z_sa_hdl),
 477                             uio, bytes);
 478                 }
 479                 len -= bytes;
 480                 off = 0;
 481                 if (error)
 482                         break;
 483         }
 484         return (error);
 485 }
 486 
 487 offset_t zfs_read_chunk_size = 1024 * 1024; /* Tunable */
 488 
 489 /*
 490  * Read bytes from specified file into supplied buffer.
 491  *
 492  *      IN:     vp      - vnode of file to be read from.
 493  *              uio     - structure supplying read location, range info,
 494  *                        and return buffer.
 495  *              ioflag  - SYNC flags; used to provide FRSYNC semantics.
 496  *              cr      - credentials of caller.
 497  *              ct      - caller context
 498  *
 499  *      OUT:    uio     - updated offset and range, buffer filled.
 500  *
 501  *      RETURN: 0 on success, error code on failure.
 502  *
 503  * Side Effects:
 504  *      vp - atime updated if byte count > 0
 505  */
 506 /* ARGSUSED */
 507 static int
 508 zfs_read(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
 509 {
 510         znode_t         *zp = VTOZ(vp);
 511         zfsvfs_t        *zfsvfs = zp->z_zfsvfs;
 512         ssize_t         n, nbytes;
 513         int             error = 0;
 514         rl_t            *rl;
 515         xuio_t          *xuio = NULL;
 516 
 517         ZFS_ENTER(zfsvfs);
 518         ZFS_VERIFY_ZP(zp);
 519 
 520         if (zp->z_pflags & ZFS_AV_QUARANTINED) {
 521                 ZFS_EXIT(zfsvfs);
 522                 return (SET_ERROR(EACCES));
 523         }
 524 
 525         /*
 526          * Validate file offset
 527          */
 528         if (uio->uio_loffset < (offset_t)0) {
 529                 ZFS_EXIT(zfsvfs);
 530                 return (SET_ERROR(EINVAL));
 531         }
 532 
 533         /*
 534          * Fasttrack empty reads
 535          */
 536         if (uio->uio_resid == 0) {
 537                 ZFS_EXIT(zfsvfs);
 538                 return (0);
 539         }
 540 
 541         /*
 542          * Check for mandatory locks
 543          */
 544         if (MANDMODE(zp->z_mode)) {
 545                 if (error = chklock(vp, FREAD,
 546                     uio->uio_loffset, uio->uio_resid, uio->uio_fmode, ct)) {
 547                         ZFS_EXIT(zfsvfs);
 548                         return (error);
 549                 }
 550         }
 551 
 552         /*
 553          * If we're in FRSYNC mode, sync out this znode before reading it.
 554          */
 555         if (ioflag & FRSYNC || zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
 556                 zil_commit(zfsvfs->z_log, zp->z_id);
 557 
 558         /*
 559          * Lock the range against changes.
 560          */
 561         rl = zfs_range_lock(zp, uio->uio_loffset, uio->uio_resid, RL_READER);
 562 
 563         /*
 564          * If we are reading past end-of-file we can skip
 565          * to the end; but we might still need to set atime.
 566          */
 567         if (uio->uio_loffset >= zp->z_size) {
 568                 error = 0;
 569                 goto out;
 570         }
 571 
 572         ASSERT(uio->uio_loffset < zp->z_size);
 573         n = MIN(uio->uio_resid, zp->z_size - uio->uio_loffset);
 574 
 575         if ((uio->uio_extflg == UIO_XUIO) &&
 576             (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY)) {
 577                 int nblk;
 578                 int blksz = zp->z_blksz;
 579                 uint64_t offset = uio->uio_loffset;
 580 
 581                 xuio = (xuio_t *)uio;
 582                 if ((ISP2(blksz))) {
 583                         nblk = (P2ROUNDUP(offset + n, blksz) - P2ALIGN(offset,
 584                             blksz)) / blksz;
 585                 } else {
 586                         ASSERT(offset + n <= blksz);
 587                         nblk = 1;
 588                 }
 589                 (void) dmu_xuio_init(xuio, nblk);
 590 
 591                 if (vn_has_cached_data(vp)) {
 592                         /*
 593                          * For simplicity, we always allocate a full buffer
 594                          * even if we only expect to read a portion of a block.
 595                          */
 596                         while (--nblk >= 0) {
 597                                 (void) dmu_xuio_add(xuio,
 598                                     dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
 599                                     blksz), 0, blksz);
 600                         }
 601                 }
 602         }
 603 
 604         while (n > 0) {
 605                 nbytes = MIN(n, zfs_read_chunk_size -
 606                     P2PHASE(uio->uio_loffset, zfs_read_chunk_size));
 607 
 608                 if (vn_has_cached_data(vp)) {
 609                         error = mappedread(vp, nbytes, uio);
 610                 } else {
 611                         error = dmu_read_uio_dbuf(sa_get_db(zp->z_sa_hdl),
 612                             uio, nbytes);
 613                 }
 614                 if (error) {
 615                         /* convert checksum errors into IO errors */
 616                         if (error == ECKSUM)
 617                                 error = SET_ERROR(EIO);
 618                         break;
 619                 }
 620 
 621                 n -= nbytes;
 622         }
 623 out:
 624         zfs_range_unlock(rl);
 625 
 626         ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
 627         ZFS_EXIT(zfsvfs);
 628         return (error);
 629 }
 630 
 631 /*
 632  * Write the bytes to a file.
 633  *
 634  *      IN:     vp      - vnode of file to be written to.
 635  *              uio     - structure supplying write location, range info,
 636  *                        and data buffer.
 637  *              ioflag  - FAPPEND, FSYNC, and/or FDSYNC.  FAPPEND is
 638  *                        set if in append mode.
 639  *              cr      - credentials of caller.
 640  *              ct      - caller context (NFS/CIFS fem monitor only)
 641  *
 642  *      OUT:    uio     - updated offset and range.
 643  *
 644  *      RETURN: 0 on success, error code on failure.
 645  *
 646  * Timestamps:
 647  *      vp - ctime|mtime updated if byte count > 0
 648  */
 649 
 650 /* ARGSUSED */
 651 static int
 652 zfs_write(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
 653 {
 654         znode_t         *zp = VTOZ(vp);
 655         rlim64_t        limit = uio->uio_llimit;
 656         ssize_t         start_resid = uio->uio_resid;
 657         ssize_t         tx_bytes;
 658         uint64_t        end_size;
 659         dmu_tx_t        *tx;
 660         zfsvfs_t        *zfsvfs = zp->z_zfsvfs;
 661         zilog_t         *zilog;
 662         offset_t        woff;
 663         ssize_t         n, nbytes;
 664         rl_t            *rl;
 665         int             max_blksz = zfsvfs->z_max_blksz;
 666         int             error = 0;
 667         int             prev_error;
 668         arc_buf_t       *abuf;
 669         iovec_t         *aiov = NULL;
 670         xuio_t          *xuio = NULL;
 671         int             i_iov = 0;
 672         int             iovcnt = uio->uio_iovcnt;
 673         iovec_t         *iovp = uio->uio_iov;
 674         int             write_eof;
 675         int             count = 0;
 676         sa_bulk_attr_t  bulk[4];
 677         uint64_t        mtime[2], ctime[2];
 678 
 679         /*
 680          * Fasttrack empty write
 681          */
 682         n = start_resid;
 683         if (n == 0)
 684                 return (0);
 685 
 686         if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
 687                 limit = MAXOFFSET_T;
 688 
 689         ZFS_ENTER(zfsvfs);
 690         ZFS_VERIFY_ZP(zp);
 691 
 692         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
 693         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
 694         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
 695             &zp->z_size, 8);
 696         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
 697             &zp->z_pflags, 8);
 698 
 699         /*
 700          * In a case vp->v_vfsp != zp->z_zfsvfs->z_vfs (e.g. snapshots) our
 701          * callers might not be able to detect properly that we are read-only,
 702          * so check it explicitly here.
 703          */
 704         if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
 705                 ZFS_EXIT(zfsvfs);
 706                 return (SET_ERROR(EROFS));
 707         }
 708 
 709         /*
 710          * If immutable or not appending then return EPERM
 711          */
 712         if ((zp->z_pflags & (ZFS_IMMUTABLE | ZFS_READONLY)) ||
 713             ((zp->z_pflags & ZFS_APPENDONLY) && !(ioflag & FAPPEND) &&
 714             (uio->uio_loffset < zp->z_size))) {
 715                 ZFS_EXIT(zfsvfs);
 716                 return (SET_ERROR(EPERM));
 717         }
 718 
 719         zilog = zfsvfs->z_log;
 720 
 721         /*
 722          * Validate file offset
 723          */
 724         woff = ioflag & FAPPEND ? zp->z_size : uio->uio_loffset;
 725         if (woff < 0) {
 726                 ZFS_EXIT(zfsvfs);
 727                 return (SET_ERROR(EINVAL));
 728         }
 729 
 730         /*
 731          * Check for mandatory locks before calling zfs_range_lock()
 732          * in order to prevent a deadlock with locks set via fcntl().
 733          */
 734         if (MANDMODE((mode_t)zp->z_mode) &&
 735             (error = chklock(vp, FWRITE, woff, n, uio->uio_fmode, ct)) != 0) {
 736                 ZFS_EXIT(zfsvfs);
 737                 return (error);
 738         }
 739 
 740         /*
 741          * Pre-fault the pages to ensure slow (eg NFS) pages
 742          * don't hold up txg.
 743          * Skip this if uio contains loaned arc_buf.
 744          */
 745         if ((uio->uio_extflg == UIO_XUIO) &&
 746             (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY))
 747                 xuio = (xuio_t *)uio;
 748         else
 749                 uio_prefaultpages(MIN(n, max_blksz), uio);
 750 
 751         /*
 752          * If in append mode, set the io offset pointer to eof.
 753          */
 754         if (ioflag & FAPPEND) {
 755                 /*
 756                  * Obtain an appending range lock to guarantee file append
 757                  * semantics.  We reset the write offset once we have the lock.
 758                  */
 759                 rl = zfs_range_lock(zp, 0, n, RL_APPEND);
 760                 woff = rl->r_off;
 761                 if (rl->r_len == UINT64_MAX) {
 762                         /*
 763                          * We overlocked the file because this write will cause
 764                          * the file block size to increase.
 765                          * Note that zp_size cannot change with this lock held.
 766                          */
 767                         woff = zp->z_size;
 768                 }
 769                 uio->uio_loffset = woff;
 770         } else {
 771                 /*
 772                  * Note that if the file block size will change as a result of
 773                  * this write, then this range lock will lock the entire file
 774                  * so that we can re-write the block safely.
 775                  */
 776                 rl = zfs_range_lock(zp, woff, n, RL_WRITER);
 777         }
 778 
 779         if (woff >= limit) {
 780                 zfs_range_unlock(rl);
 781                 ZFS_EXIT(zfsvfs);
 782                 return (SET_ERROR(EFBIG));
 783         }
 784 
 785         if ((woff + n) > limit || woff > (limit - n))
 786                 n = limit - woff;
 787 
 788         /* Will this write extend the file length? */
 789         write_eof = (woff + n > zp->z_size);
 790 
 791         end_size = MAX(zp->z_size, woff + n);
 792 
 793         /*
 794          * Write the file in reasonable size chunks.  Each chunk is written
 795          * in a separate transaction; this keeps the intent log records small
 796          * and allows us to do more fine-grained space accounting.
 797          */
 798         while (n > 0) {
 799                 abuf = NULL;
 800                 woff = uio->uio_loffset;
 801                 if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) ||
 802                     zfs_owner_overquota(zfsvfs, zp, B_TRUE)) {
 803                         if (abuf != NULL)
 804                                 dmu_return_arcbuf(abuf);
 805                         error = SET_ERROR(EDQUOT);
 806                         break;
 807                 }
 808 
 809                 if (xuio && abuf == NULL) {
 810                         ASSERT(i_iov < iovcnt);
 811                         aiov = &iovp[i_iov];
 812                         abuf = dmu_xuio_arcbuf(xuio, i_iov);
 813                         dmu_xuio_clear(xuio, i_iov);
 814                         DTRACE_PROBE3(zfs_cp_write, int, i_iov,
 815                             iovec_t *, aiov, arc_buf_t *, abuf);
 816                         ASSERT((aiov->iov_base == abuf->b_data) ||
 817                             ((char *)aiov->iov_base - (char *)abuf->b_data +
 818                             aiov->iov_len == arc_buf_size(abuf)));
 819                         i_iov++;
 820                 } else if (abuf == NULL && n >= max_blksz &&
 821                     woff >= zp->z_size &&
 822                     P2PHASE(woff, max_blksz) == 0 &&
 823                     zp->z_blksz == max_blksz) {
 824                         /*
 825                          * This write covers a full block.  "Borrow" a buffer
 826                          * from the dmu so that we can fill it before we enter
 827                          * a transaction.  This avoids the possibility of
 828                          * holding up the transaction if the data copy hangs
 829                          * up on a pagefault (e.g., from an NFS server mapping).
 830                          */
 831                         size_t cbytes;
 832 
 833                         abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
 834                             max_blksz);
 835                         ASSERT(abuf != NULL);
 836                         ASSERT(arc_buf_size(abuf) == max_blksz);
 837                         if (error = uiocopy(abuf->b_data, max_blksz,
 838                             UIO_WRITE, uio, &cbytes)) {
 839                                 dmu_return_arcbuf(abuf);
 840                                 break;
 841                         }
 842                         ASSERT(cbytes == max_blksz);
 843                 }
 844 
 845                 /*
 846                  * Start a transaction.
 847                  */
 848                 tx = dmu_tx_create(zfsvfs->z_os);
 849                 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
 850                 dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz));
 851                 zfs_sa_upgrade_txholds(tx, zp);
 852                 error = dmu_tx_assign(tx, TXG_WAIT);
 853                 if (error) {
 854                         dmu_tx_abort(tx);
 855                         if (abuf != NULL)
 856                                 dmu_return_arcbuf(abuf);
 857                         break;
 858                 }
 859 
 860                 /*
 861                  * If zfs_range_lock() over-locked we grow the blocksize
 862                  * and then reduce the lock range.  This will only happen
 863                  * on the first iteration since zfs_range_reduce() will
 864                  * shrink down r_len to the appropriate size.
 865                  */
 866                 if (rl->r_len == UINT64_MAX) {
 867                         uint64_t new_blksz;
 868 
 869                         if (zp->z_blksz > max_blksz) {
 870                                 /*
 871                                  * File's blocksize is already larger than the
 872                                  * "recordsize" property.  Only let it grow to
 873                                  * the next power of 2.
 874                                  */
 875                                 ASSERT(!ISP2(zp->z_blksz));
 876                                 new_blksz = MIN(end_size,
 877                                     1 << highbit64(zp->z_blksz));
 878                         } else {
 879                                 new_blksz = MIN(end_size, max_blksz);
 880                         }
 881                         zfs_grow_blocksize(zp, new_blksz, tx);
 882                         zfs_range_reduce(rl, woff, n);
 883                 }
 884 
 885                 /*
 886                  * XXX - should we really limit each write to z_max_blksz?
 887                  * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
 888                  */
 889                 nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz));
 890 
 891                 if (abuf == NULL) {
 892                         tx_bytes = uio->uio_resid;
 893                         error = dmu_write_uio_dbuf(sa_get_db(zp->z_sa_hdl),
 894                             uio, nbytes, tx);
 895                         tx_bytes -= uio->uio_resid;
 896                 } else {
 897                         tx_bytes = nbytes;
 898                         ASSERT(xuio == NULL || tx_bytes == aiov->iov_len);
 899                         /*
 900                          * If this is not a full block write, but we are
 901                          * extending the file past EOF and this data starts
 902                          * block-aligned, use assign_arcbuf().  Otherwise,
 903                          * write via dmu_write().
 904                          */
 905                         if (tx_bytes < max_blksz && (!write_eof ||
 906                             aiov->iov_base != abuf->b_data)) {
 907                                 ASSERT(xuio);
 908                                 dmu_write(zfsvfs->z_os, zp->z_id, woff,
 909                                     aiov->iov_len, aiov->iov_base, tx);
 910                                 dmu_return_arcbuf(abuf);
 911                                 xuio_stat_wbuf_copied();
 912                         } else {
 913                                 ASSERT(xuio || tx_bytes == max_blksz);
 914                                 dmu_assign_arcbuf(sa_get_db(zp->z_sa_hdl),
 915                                     woff, abuf, tx);
 916                         }
 917                         ASSERT(tx_bytes <= uio->uio_resid);
 918                         uioskip(uio, tx_bytes);
 919                 }
 920                 if (tx_bytes && vn_has_cached_data(vp)) {
 921                         update_pages(vp, woff,
 922                             tx_bytes, zfsvfs->z_os, zp->z_id);
 923                 }
 924 
 925                 /*
 926                  * If we made no progress, we're done.  If we made even
 927                  * partial progress, update the znode and ZIL accordingly.
 928                  */
 929                 if (tx_bytes == 0) {
 930                         (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
 931                             (void *)&zp->z_size, sizeof (uint64_t), tx);
 932                         dmu_tx_commit(tx);
 933                         ASSERT(error != 0);
 934                         break;
 935                 }
 936 
 937                 /*
 938                  * Clear Set-UID/Set-GID bits on successful write if not
 939                  * privileged and at least one of the excute bits is set.
 940                  *
 941                  * It would be nice to to this after all writes have
 942                  * been done, but that would still expose the ISUID/ISGID
 943                  * to another app after the partial write is committed.
 944                  *
 945                  * Note: we don't call zfs_fuid_map_id() here because
 946                  * user 0 is not an ephemeral uid.
 947                  */
 948                 mutex_enter(&zp->z_acl_lock);
 949                 if ((zp->z_mode & (S_IXUSR | (S_IXUSR >> 3) |
 950                     (S_IXUSR >> 6))) != 0 &&
 951                     (zp->z_mode & (S_ISUID | S_ISGID)) != 0 &&
 952                     secpolicy_vnode_setid_retain(cr,
 953                     (zp->z_mode & S_ISUID) != 0 && zp->z_uid == 0) != 0) {
 954                         uint64_t newmode;
 955                         zp->z_mode &= ~(S_ISUID | S_ISGID);
 956                         newmode = zp->z_mode;
 957                         (void) sa_update(zp->z_sa_hdl, SA_ZPL_MODE(zfsvfs),
 958                             (void *)&newmode, sizeof (uint64_t), tx);
 959                 }
 960                 mutex_exit(&zp->z_acl_lock);
 961 
 962                 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
 963                     B_TRUE);
 964 
 965                 /*
 966                  * Update the file size (zp_size) if it has changed;
 967                  * account for possible concurrent updates.
 968                  */
 969                 while ((end_size = zp->z_size) < uio->uio_loffset) {
 970                         (void) atomic_cas_64(&zp->z_size, end_size,
 971                             uio->uio_loffset);
 972                 }
 973                 /*
 974                  * If we are replaying and eof is non zero then force
 975                  * the file size to the specified eof. Note, there's no
 976                  * concurrency during replay.
 977                  */
 978                 if (zfsvfs->z_replay && zfsvfs->z_replay_eof != 0)
 979                         zp->z_size = zfsvfs->z_replay_eof;
 980 
 981                 /*
 982                  * Keep track of a possible pre-existing error from a partial
 983                  * write via dmu_write_uio_dbuf above.
 984                  */
 985                 prev_error = error;
 986                 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
 987 
 988                 zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, ioflag);
 989                 dmu_tx_commit(tx);
 990 
 991                 if (prev_error != 0 || error != 0)
 992                         break;
 993                 ASSERT(tx_bytes == nbytes);
 994                 n -= nbytes;
 995 
 996                 if (!xuio && n > 0)
 997                         uio_prefaultpages(MIN(n, max_blksz), uio);
 998         }
 999 
1000         zfs_range_unlock(rl);
1001 
1002         /*
1003          * If we're in replay mode, or we made no progress, return error.
1004          * Otherwise, it's at least a partial write, so it's successful.
1005          */
1006         if (zfsvfs->z_replay || uio->uio_resid == start_resid) {
1007                 ZFS_EXIT(zfsvfs);
1008                 return (error);
1009         }
1010 
1011         if (ioflag & (FSYNC | FDSYNC) ||
1012             zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1013                 zil_commit(zilog, zp->z_id);
1014 
1015         ZFS_EXIT(zfsvfs);
1016         return (0);
1017 }
1018 
1019 void
1020 zfs_get_done(zgd_t *zgd, int error)
1021 {
1022         znode_t *zp = zgd->zgd_private;
1023         objset_t *os = zp->z_zfsvfs->z_os;
1024 
1025         if (zgd->zgd_db)
1026                 dmu_buf_rele(zgd->zgd_db, zgd);
1027 
1028         zfs_range_unlock(zgd->zgd_rl);
1029 
1030         /*
1031          * Release the vnode asynchronously as we currently have the
1032          * txg stopped from syncing.
1033          */
1034         VN_RELE_ASYNC(ZTOV(zp), dsl_pool_vnrele_taskq(dmu_objset_pool(os)));
1035 
1036         if (error == 0 && zgd->zgd_bp)
1037                 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
1038 
1039         kmem_free(zgd, sizeof (zgd_t));
1040 }
1041 
1042 #ifdef DEBUG
1043 static int zil_fault_io = 0;
1044 #endif
1045 
1046 /*
1047  * Get data to generate a TX_WRITE intent log record.
1048  */
1049 int
1050 zfs_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
1051 {
1052         zfsvfs_t *zfsvfs = arg;
1053         objset_t *os = zfsvfs->z_os;
1054         znode_t *zp;
1055         uint64_t object = lr->lr_foid;
1056         uint64_t offset = lr->lr_offset;
1057         uint64_t size = lr->lr_length;
1058         blkptr_t *bp = &lr->lr_blkptr;
1059         dmu_buf_t *db;
1060         zgd_t *zgd;
1061         int error = 0;
1062 
1063         ASSERT(zio != NULL);
1064         ASSERT(size != 0);
1065 
1066         /*
1067          * Nothing to do if the file has been removed
1068          */
1069         if (zfs_zget(zfsvfs, object, &zp) != 0)
1070                 return (SET_ERROR(ENOENT));
1071         if (zp->z_unlinked) {
1072                 /*
1073                  * Release the vnode asynchronously as we currently have the
1074                  * txg stopped from syncing.
1075                  */
1076                 VN_RELE_ASYNC(ZTOV(zp),
1077                     dsl_pool_vnrele_taskq(dmu_objset_pool(os)));
1078                 return (SET_ERROR(ENOENT));
1079         }
1080 
1081         zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
1082         zgd->zgd_zilog = zfsvfs->z_log;
1083         zgd->zgd_private = zp;
1084 
1085         /*
1086          * Write records come in two flavors: immediate and indirect.
1087          * For small writes it's cheaper to store the data with the
1088          * log record (immediate); for large writes it's cheaper to
1089          * sync the data and get a pointer to it (indirect) so that
1090          * we don't have to write the data twice.
1091          */
1092         if (buf != NULL) { /* immediate write */
1093                 zgd->zgd_rl = zfs_range_lock(zp, offset, size, RL_READER);
1094                 /* test for truncation needs to be done while range locked */
1095                 if (offset >= zp->z_size) {
1096                         error = SET_ERROR(ENOENT);
1097                 } else {
1098                         error = dmu_read(os, object, offset, size, buf,
1099                             DMU_READ_NO_PREFETCH);
1100                 }
1101                 ASSERT(error == 0 || error == ENOENT);
1102         } else { /* indirect write */
1103                 /*
1104                  * Have to lock the whole block to ensure when it's
1105                  * written out and it's checksum is being calculated
1106                  * that no one can change the data. We need to re-check
1107                  * blocksize after we get the lock in case it's changed!
1108                  */
1109                 for (;;) {
1110                         uint64_t blkoff;
1111                         size = zp->z_blksz;
1112                         blkoff = ISP2(size) ? P2PHASE(offset, size) : offset;
1113                         offset -= blkoff;
1114                         zgd->zgd_rl = zfs_range_lock(zp, offset, size,
1115                             RL_READER);
1116                         if (zp->z_blksz == size)
1117                                 break;
1118                         offset += blkoff;
1119                         zfs_range_unlock(zgd->zgd_rl);
1120                 }
1121                 /* test for truncation needs to be done while range locked */
1122                 if (lr->lr_offset >= zp->z_size)
1123                         error = SET_ERROR(ENOENT);
1124 #ifdef DEBUG
1125                 if (zil_fault_io) {
1126                         error = SET_ERROR(EIO);
1127                         zil_fault_io = 0;
1128                 }
1129 #endif
1130                 if (error == 0)
1131                         error = dmu_buf_hold(os, object, offset, zgd, &db,
1132                             DMU_READ_NO_PREFETCH);
1133 
1134                 if (error == 0) {
1135                         blkptr_t *obp = dmu_buf_get_blkptr(db);
1136                         if (obp) {
1137                                 ASSERT(BP_IS_HOLE(bp));
1138                                 *bp = *obp;
1139                         }
1140 
1141                         zgd->zgd_db = db;
1142                         zgd->zgd_bp = bp;
1143 
1144                         ASSERT(db->db_offset == offset);
1145                         ASSERT(db->db_size == size);
1146 
1147                         error = dmu_sync(zio, lr->lr_common.lrc_txg,
1148                             zfs_get_done, zgd);
1149                         ASSERT(error || lr->lr_length <= zp->z_blksz);
1150 
1151                         /*
1152                          * On success, we need to wait for the write I/O
1153                          * initiated by dmu_sync() to complete before we can
1154                          * release this dbuf.  We will finish everything up
1155                          * in the zfs_get_done() callback.
1156                          */
1157                         if (error == 0)
1158                                 return (0);
1159 
1160                         if (error == EALREADY) {
1161                                 lr->lr_common.lrc_txtype = TX_WRITE2;
1162                                 error = 0;
1163                         }
1164                 }
1165         }
1166 
1167         zfs_get_done(zgd, error);
1168 
1169         return (error);
1170 }
1171 
1172 /*ARGSUSED*/
1173 static int
1174 zfs_access(vnode_t *vp, int mode, int flag, cred_t *cr,
1175     caller_context_t *ct)
1176 {
1177         znode_t *zp = VTOZ(vp);
1178         zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1179         int error;
1180 
1181         ZFS_ENTER(zfsvfs);
1182         ZFS_VERIFY_ZP(zp);
1183 
1184         if (flag & V_ACE_MASK)
1185                 error = zfs_zaccess(zp, mode, flag, B_FALSE, cr);
1186         else
1187                 error = zfs_zaccess_rwx(zp, mode, flag, cr);
1188 
1189         ZFS_EXIT(zfsvfs);
1190         return (error);
1191 }
1192 
1193 /*
1194  * If vnode is for a device return a specfs vnode instead.
1195  */
1196 static int
1197 specvp_check(vnode_t **vpp, cred_t *cr)
1198 {
1199         int error = 0;
1200 
1201         if (IS_DEVVP(*vpp)) {
1202                 struct vnode *svp;
1203 
1204                 svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
1205                 VN_RELE(*vpp);
1206                 if (svp == NULL)
1207                         error = SET_ERROR(ENOSYS);
1208                 *vpp = svp;
1209         }
1210         return (error);
1211 }
1212 
1213 
1214 /*
1215  * Lookup an entry in a directory, or an extended attribute directory.
1216  * If it exists, return a held vnode reference for it.
1217  *
1218  *      IN:     dvp     - vnode of directory to search.
1219  *              nm      - name of entry to lookup.
1220  *              pnp     - full pathname to lookup [UNUSED].
1221  *              flags   - LOOKUP_XATTR set if looking for an attribute.
1222  *              rdir    - root directory vnode [UNUSED].
1223  *              cr      - credentials of caller.
1224  *              ct      - caller context
1225  *              direntflags - directory lookup flags
1226  *              realpnp - returned pathname.
1227  *
1228  *      OUT:    vpp     - vnode of located entry, NULL if not found.
1229  *
1230  *      RETURN: 0 on success, error code on failure.
1231  *
1232  * Timestamps:
1233  *      NA
1234  */
1235 /* ARGSUSED */
1236 static int
1237 zfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp,
1238     int flags, vnode_t *rdir, cred_t *cr,  caller_context_t *ct,
1239     int *direntflags, pathname_t *realpnp)
1240 {
1241         znode_t *zdp = VTOZ(dvp);
1242         zfsvfs_t *zfsvfs = zdp->z_zfsvfs;
1243         int     error = 0;
1244 
1245         /* fast path */
1246         if (!(flags & (LOOKUP_XATTR | FIGNORECASE))) {
1247 
1248                 if (dvp->v_type != VDIR) {
1249                         return (SET_ERROR(ENOTDIR));
1250                 } else if (zdp->z_sa_hdl == NULL) {
1251                         return (SET_ERROR(EIO));
1252                 }
1253 
1254                 if (nm[0] == 0 || (nm[0] == '.' && nm[1] == '\0')) {
1255                         error = zfs_fastaccesschk_execute(zdp, cr);
1256                         if (!error) {
1257                                 *vpp = dvp;
1258                                 VN_HOLD(*vpp);
1259                                 return (0);
1260                         }
1261                         return (error);
1262                 } else {
1263                         vnode_t *tvp = dnlc_lookup(dvp, nm);
1264 
1265                         if (tvp) {
1266                                 error = zfs_fastaccesschk_execute(zdp, cr);
1267                                 if (error) {
1268                                         VN_RELE(tvp);
1269                                         return (error);
1270                                 }
1271                                 if (tvp == DNLC_NO_VNODE) {
1272                                         VN_RELE(tvp);
1273                                         return (SET_ERROR(ENOENT));
1274                                 } else {
1275                                         *vpp = tvp;
1276                                         return (specvp_check(vpp, cr));
1277                                 }
1278                         }
1279                 }
1280         }
1281 
1282         DTRACE_PROBE2(zfs__fastpath__lookup__miss, vnode_t *, dvp, char *, nm);
1283 
1284         ZFS_ENTER(zfsvfs);
1285         ZFS_VERIFY_ZP(zdp);
1286 
1287         *vpp = NULL;
1288 
1289         if (flags & LOOKUP_XATTR) {
1290                 /*
1291                  * If the xattr property is off, refuse the lookup request.
1292                  */
1293                 if (!(zfsvfs->z_vfs->vfs_flag & VFS_XATTR)) {
1294                         ZFS_EXIT(zfsvfs);
1295                         return (SET_ERROR(EINVAL));
1296                 }
1297 
1298                 /*
1299                  * We don't allow recursive attributes..
1300                  * Maybe someday we will.
1301                  */
1302                 if (zdp->z_pflags & ZFS_XATTR) {
1303                         ZFS_EXIT(zfsvfs);
1304                         return (SET_ERROR(EINVAL));
1305                 }
1306 
1307                 if (error = zfs_get_xattrdir(VTOZ(dvp), vpp, cr, flags)) {
1308                         ZFS_EXIT(zfsvfs);
1309                         return (error);
1310                 }
1311 
1312                 /*
1313                  * Do we have permission to get into attribute directory?
1314                  */
1315 
1316                 if (error = zfs_zaccess(VTOZ(*vpp), ACE_EXECUTE, 0,
1317                     B_FALSE, cr)) {
1318                         VN_RELE(*vpp);
1319                         *vpp = NULL;
1320                 }
1321 
1322                 ZFS_EXIT(zfsvfs);
1323                 return (error);
1324         }
1325 
1326         if (dvp->v_type != VDIR) {
1327                 ZFS_EXIT(zfsvfs);
1328                 return (SET_ERROR(ENOTDIR));
1329         }
1330 
1331         /*
1332          * Check accessibility of directory.
1333          */
1334 
1335         if (error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr)) {
1336                 ZFS_EXIT(zfsvfs);
1337                 return (error);
1338         }
1339 
1340         if (zfsvfs->z_utf8 && u8_validate(nm, strlen(nm),
1341             NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1342                 ZFS_EXIT(zfsvfs);
1343                 return (SET_ERROR(EILSEQ));
1344         }
1345 
1346         error = zfs_dirlook(zdp, nm, vpp, flags, direntflags, realpnp);
1347         if (error == 0)
1348                 error = specvp_check(vpp, cr);
1349 
1350         ZFS_EXIT(zfsvfs);
1351         return (error);
1352 }
1353 
1354 /*
1355  * Attempt to create a new entry in a directory.  If the entry
1356  * already exists, truncate the file if permissible, else return
1357  * an error.  Return the vp of the created or trunc'd file.
1358  *
1359  *      IN:     dvp     - vnode of directory to put new file entry in.
1360  *              name    - name of new file entry.
1361  *              vap     - attributes of new file.
1362  *              excl    - flag indicating exclusive or non-exclusive mode.
1363  *              mode    - mode to open file with.
1364  *              cr      - credentials of caller.
1365  *              flag    - large file flag [UNUSED].
1366  *              ct      - caller context
1367  *              vsecp   - ACL to be set
1368  *
1369  *      OUT:    vpp     - vnode of created or trunc'd entry.
1370  *
1371  *      RETURN: 0 on success, error code on failure.
1372  *
1373  * Timestamps:
1374  *      dvp - ctime|mtime updated if new entry created
1375  *       vp - ctime|mtime always, atime if new
1376  */
1377 
1378 /* ARGSUSED */
1379 static int
1380 zfs_create(vnode_t *dvp, char *name, vattr_t *vap, vcexcl_t excl,
1381     int mode, vnode_t **vpp, cred_t *cr, int flag, caller_context_t *ct,
1382     vsecattr_t *vsecp)
1383 {
1384         znode_t         *zp, *dzp = VTOZ(dvp);
1385         zfsvfs_t        *zfsvfs = dzp->z_zfsvfs;
1386         zilog_t         *zilog;
1387         objset_t        *os;
1388         zfs_dirlock_t   *dl;
1389         dmu_tx_t        *tx;
1390         int             error;
1391         ksid_t          *ksid;
1392         uid_t           uid;
1393         gid_t           gid = crgetgid(cr);
1394         zfs_acl_ids_t   acl_ids;
1395         boolean_t       fuid_dirtied;
1396         boolean_t       have_acl = B_FALSE;
1397         boolean_t       waited = B_FALSE;
1398 
1399         /*
1400          * If we have an ephemeral id, ACL, or XVATTR then
1401          * make sure file system is at proper version
1402          */
1403 
1404         ksid = crgetsid(cr, KSID_OWNER);
1405         if (ksid)
1406                 uid = ksid_getid(ksid);
1407         else
1408                 uid = crgetuid(cr);
1409 
1410         if (zfsvfs->z_use_fuids == B_FALSE &&
1411             (vsecp || (vap->va_mask & AT_XVATTR) ||
1412             IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
1413                 return (SET_ERROR(EINVAL));
1414 
1415         ZFS_ENTER(zfsvfs);
1416         ZFS_VERIFY_ZP(dzp);
1417         os = zfsvfs->z_os;
1418         zilog = zfsvfs->z_log;
1419 
1420         if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
1421             NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1422                 ZFS_EXIT(zfsvfs);
1423                 return (SET_ERROR(EILSEQ));
1424         }
1425 
1426         if (vap->va_mask & AT_XVATTR) {
1427                 if ((error = secpolicy_xvattr((xvattr_t *)vap,
1428                     crgetuid(cr), cr, vap->va_type)) != 0) {
1429                         ZFS_EXIT(zfsvfs);
1430                         return (error);
1431                 }
1432         }
1433 top:
1434         *vpp = NULL;
1435 
1436         if ((vap->va_mode & VSVTX) && secpolicy_vnode_stky_modify(cr))
1437                 vap->va_mode &= ~VSVTX;
1438 
1439         if (*name == '\0') {
1440                 /*
1441                  * Null component name refers to the directory itself.
1442                  */
1443                 VN_HOLD(dvp);
1444                 zp = dzp;
1445                 dl = NULL;
1446                 error = 0;
1447         } else {
1448                 /* possible VN_HOLD(zp) */
1449                 int zflg = 0;
1450 
1451                 if (flag & FIGNORECASE)
1452                         zflg |= ZCILOOK;
1453 
1454                 error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1455                     NULL, NULL);
1456                 if (error) {
1457                         if (have_acl)
1458                                 zfs_acl_ids_free(&acl_ids);
1459                         if (strcmp(name, "..") == 0)
1460                                 error = SET_ERROR(EISDIR);
1461                         ZFS_EXIT(zfsvfs);
1462                         return (error);
1463                 }
1464         }
1465 
1466         if (zp == NULL) {
1467                 uint64_t txtype;
1468 
1469                 /*
1470                  * Create a new file object and update the directory
1471                  * to reference it.
1472                  */
1473                 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
1474                         if (have_acl)
1475                                 zfs_acl_ids_free(&acl_ids);
1476                         goto out;
1477                 }
1478 
1479                 /*
1480                  * We only support the creation of regular files in
1481                  * extended attribute directories.
1482                  */
1483 
1484                 if ((dzp->z_pflags & ZFS_XATTR) &&
1485                     (vap->va_type != VREG)) {
1486                         if (have_acl)
1487                                 zfs_acl_ids_free(&acl_ids);
1488                         error = SET_ERROR(EINVAL);
1489                         goto out;
1490                 }
1491 
1492                 if (!have_acl && (error = zfs_acl_ids_create(dzp, 0, vap,
1493                     cr, vsecp, &acl_ids)) != 0)
1494                         goto out;
1495                 have_acl = B_TRUE;
1496 
1497                 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
1498                         zfs_acl_ids_free(&acl_ids);
1499                         error = SET_ERROR(EDQUOT);
1500                         goto out;
1501                 }
1502 
1503                 tx = dmu_tx_create(os);
1504 
1505                 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1506                     ZFS_SA_BASE_ATTR_SIZE);
1507 
1508                 fuid_dirtied = zfsvfs->z_fuid_dirty;
1509                 if (fuid_dirtied)
1510                         zfs_fuid_txhold(zfsvfs, tx);
1511                 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
1512                 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
1513                 if (!zfsvfs->z_use_sa &&
1514                     acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1515                         dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1516                             0, acl_ids.z_aclp->z_acl_bytes);
1517                 }
1518                 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
1519                 if (error) {
1520                         zfs_dirent_unlock(dl);
1521                         if (error == ERESTART) {
1522                                 waited = B_TRUE;
1523                                 dmu_tx_wait(tx);
1524                                 dmu_tx_abort(tx);
1525                                 goto top;
1526                         }
1527                         zfs_acl_ids_free(&acl_ids);
1528                         dmu_tx_abort(tx);
1529                         ZFS_EXIT(zfsvfs);
1530                         return (error);
1531                 }
1532                 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
1533 
1534                 if (fuid_dirtied)
1535                         zfs_fuid_sync(zfsvfs, tx);
1536 
1537                 (void) zfs_link_create(dl, zp, tx, ZNEW);
1538                 txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap);
1539                 if (flag & FIGNORECASE)
1540                         txtype |= TX_CI;
1541                 zfs_log_create(zilog, tx, txtype, dzp, zp, name,
1542                     vsecp, acl_ids.z_fuidp, vap);
1543                 zfs_acl_ids_free(&acl_ids);
1544                 dmu_tx_commit(tx);
1545         } else {
1546                 int aflags = (flag & FAPPEND) ? V_APPEND : 0;
1547 
1548                 if (have_acl)
1549                         zfs_acl_ids_free(&acl_ids);
1550                 have_acl = B_FALSE;
1551 
1552                 /*
1553                  * A directory entry already exists for this name.
1554                  */
1555                 /*
1556                  * Can't truncate an existing file if in exclusive mode.
1557                  */
1558                 if (excl == EXCL) {
1559                         error = SET_ERROR(EEXIST);
1560                         goto out;
1561                 }
1562                 /*
1563                  * Can't open a directory for writing.
1564                  */
1565                 if ((ZTOV(zp)->v_type == VDIR) && (mode & S_IWRITE)) {
1566                         error = SET_ERROR(EISDIR);
1567                         goto out;
1568                 }
1569                 /*
1570                  * Verify requested access to file.
1571                  */
1572                 if (mode && (error = zfs_zaccess_rwx(zp, mode, aflags, cr))) {
1573                         goto out;
1574                 }
1575 
1576                 mutex_enter(&dzp->z_lock);
1577                 dzp->z_seq++;
1578                 mutex_exit(&dzp->z_lock);
1579 
1580                 /*
1581                  * Truncate regular files if requested.
1582                  */
1583                 if ((ZTOV(zp)->v_type == VREG) &&
1584                     (vap->va_mask & AT_SIZE) && (vap->va_size == 0)) {
1585                         /* we can't hold any locks when calling zfs_freesp() */
1586                         zfs_dirent_unlock(dl);
1587                         dl = NULL;
1588                         error = zfs_freesp(zp, 0, 0, mode, TRUE);
1589                         if (error == 0) {
1590                                 vnevent_create(ZTOV(zp), ct);
1591                         }
1592                 }
1593         }
1594 out:
1595 
1596         if (dl)
1597                 zfs_dirent_unlock(dl);
1598 
1599         if (error) {
1600                 if (zp)
1601                         VN_RELE(ZTOV(zp));
1602         } else {
1603                 *vpp = ZTOV(zp);
1604                 error = specvp_check(vpp, cr);
1605         }
1606 
1607         if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1608                 zil_commit(zilog, 0);
1609 
1610         ZFS_EXIT(zfsvfs);
1611         return (error);
1612 }
1613 
1614 /*
1615  * Remove an entry from a directory.
1616  *
1617  *      IN:     dvp     - vnode of directory to remove entry from.
1618  *              name    - name of entry to remove.
1619  *              cr      - credentials of caller.
1620  *              ct      - caller context
1621  *              flags   - case flags
1622  *
1623  *      RETURN: 0 on success, error code on failure.
1624  *
1625  * Timestamps:
1626  *      dvp - ctime|mtime
1627  *       vp - ctime (if nlink > 0)
1628  */
1629 
1630 uint64_t null_xattr = 0;
1631 
1632 /*ARGSUSED*/
1633 static int
1634 zfs_remove(vnode_t *dvp, char *name, cred_t *cr, caller_context_t *ct,
1635     int flags)
1636 {
1637         znode_t         *zp, *dzp = VTOZ(dvp);
1638         znode_t         *xzp;
1639         vnode_t         *vp;
1640         zfsvfs_t        *zfsvfs = dzp->z_zfsvfs;
1641         zilog_t         *zilog;
1642         uint64_t        acl_obj, xattr_obj;
1643         uint64_t        xattr_obj_unlinked = 0;
1644         uint64_t        obj = 0;
1645         zfs_dirlock_t   *dl;
1646         dmu_tx_t        *tx;
1647         boolean_t       may_delete_now, delete_now = FALSE;
1648         boolean_t       unlinked, toobig = FALSE;
1649         uint64_t        txtype;
1650         pathname_t      *realnmp = NULL;
1651         pathname_t      realnm;
1652         int             error;
1653         int             zflg = ZEXISTS;
1654         boolean_t       waited = B_FALSE;
1655 
1656         ZFS_ENTER(zfsvfs);
1657         ZFS_VERIFY_ZP(dzp);
1658         zilog = zfsvfs->z_log;
1659 
1660         if (flags & FIGNORECASE) {
1661                 zflg |= ZCILOOK;
1662                 pn_alloc(&realnm);
1663                 realnmp = &realnm;
1664         }
1665 
1666 top:
1667         xattr_obj = 0;
1668         xzp = NULL;
1669         /*
1670          * Attempt to lock directory; fail if entry doesn't exist.
1671          */
1672         if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1673             NULL, realnmp)) {
1674                 if (realnmp)
1675                         pn_free(realnmp);
1676                 ZFS_EXIT(zfsvfs);
1677                 return (error);
1678         }
1679 
1680         vp = ZTOV(zp);
1681 
1682         if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1683                 goto out;
1684         }
1685 
1686         /*
1687          * Need to use rmdir for removing directories.
1688          */
1689         if (vp->v_type == VDIR) {
1690                 error = SET_ERROR(EPERM);
1691                 goto out;
1692         }
1693 
1694         vnevent_remove(vp, dvp, name, ct);
1695 
1696         if (realnmp)
1697                 dnlc_remove(dvp, realnmp->pn_buf);
1698         else
1699                 dnlc_remove(dvp, name);
1700 
1701         mutex_enter(&vp->v_lock);
1702         may_delete_now = vp->v_count == 1 && !vn_has_cached_data(vp);
1703         mutex_exit(&vp->v_lock);
1704 
1705         /*
1706          * We may delete the znode now, or we may put it in the unlinked set;
1707          * it depends on whether we're the last link, and on whether there are
1708          * other holds on the vnode.  So we dmu_tx_hold() the right things to
1709          * allow for either case.
1710          */
1711         obj = zp->z_id;
1712         tx = dmu_tx_create(zfsvfs->z_os);
1713         dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1714         dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1715         zfs_sa_upgrade_txholds(tx, zp);
1716         zfs_sa_upgrade_txholds(tx, dzp);
1717         if (may_delete_now) {
1718                 toobig =
1719                     zp->z_size > zp->z_blksz * DMU_MAX_DELETEBLKCNT;
1720                 /* if the file is too big, only hold_free a token amount */
1721                 dmu_tx_hold_free(tx, zp->z_id, 0,
1722                     (toobig ? DMU_MAX_ACCESS : DMU_OBJECT_END));
1723         }
1724 
1725         /* are there any extended attributes? */
1726         error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
1727             &xattr_obj, sizeof (xattr_obj));
1728         if (error == 0 && xattr_obj) {
1729                 error = zfs_zget(zfsvfs, xattr_obj, &xzp);
1730                 ASSERT0(error);
1731                 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
1732                 dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE);
1733         }
1734 
1735         mutex_enter(&zp->z_lock);
1736         if ((acl_obj = zfs_external_acl(zp)) != 0 && may_delete_now)
1737                 dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
1738         mutex_exit(&zp->z_lock);
1739 
1740         /* charge as an update -- would be nice not to charge at all */
1741         dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
1742 
1743         /*
1744          * Mark this transaction as typically resulting in a net free of space
1745          */
1746         dmu_tx_mark_netfree(tx);
1747 
1748         error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
1749         if (error) {
1750                 zfs_dirent_unlock(dl);
1751                 VN_RELE(vp);
1752                 if (xzp)
1753                         VN_RELE(ZTOV(xzp));
1754                 if (error == ERESTART) {
1755                         waited = B_TRUE;
1756                         dmu_tx_wait(tx);
1757                         dmu_tx_abort(tx);
1758                         goto top;
1759                 }
1760                 if (realnmp)
1761                         pn_free(realnmp);
1762                 dmu_tx_abort(tx);
1763                 ZFS_EXIT(zfsvfs);
1764                 return (error);
1765         }
1766 
1767         /*
1768          * Remove the directory entry.
1769          */
1770         error = zfs_link_destroy(dl, zp, tx, zflg, &unlinked);
1771 
1772         if (error) {
1773                 dmu_tx_commit(tx);
1774                 goto out;
1775         }
1776 
1777         if (unlinked) {
1778                 /*
1779                  * Hold z_lock so that we can make sure that the ACL obj
1780                  * hasn't changed.  Could have been deleted due to
1781                  * zfs_sa_upgrade().
1782                  */
1783                 mutex_enter(&zp->z_lock);
1784                 mutex_enter(&vp->v_lock);
1785                 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
1786                     &xattr_obj_unlinked, sizeof (xattr_obj_unlinked));
1787                 delete_now = may_delete_now && !toobig &&
1788                     vp->v_count == 1 && !vn_has_cached_data(vp) &&
1789                     xattr_obj == xattr_obj_unlinked && zfs_external_acl(zp) ==
1790                     acl_obj;
1791                 mutex_exit(&vp->v_lock);
1792         }
1793 
1794         if (delete_now) {
1795                 if (xattr_obj_unlinked) {
1796                         ASSERT3U(xzp->z_links, ==, 2);
1797                         mutex_enter(&xzp->z_lock);
1798                         xzp->z_unlinked = 1;
1799                         xzp->z_links = 0;
1800                         error = sa_update(xzp->z_sa_hdl, SA_ZPL_LINKS(zfsvfs),
1801                             &xzp->z_links, sizeof (xzp->z_links), tx);
1802                         ASSERT3U(error,  ==,  0);
1803                         mutex_exit(&xzp->z_lock);
1804                         zfs_unlinked_add(xzp, tx);
1805 
1806                         if (zp->z_is_sa)
1807                                 error = sa_remove(zp->z_sa_hdl,
1808                                     SA_ZPL_XATTR(zfsvfs), tx);
1809                         else
1810                                 error = sa_update(zp->z_sa_hdl,
1811                                     SA_ZPL_XATTR(zfsvfs), &null_xattr,
1812                                     sizeof (uint64_t), tx);
1813                         ASSERT0(error);
1814                 }
1815                 mutex_enter(&vp->v_lock);
1816                 vp->v_count--;
1817                 ASSERT0(vp->v_count);
1818                 mutex_exit(&vp->v_lock);
1819                 mutex_exit(&zp->z_lock);
1820                 zfs_znode_delete(zp, tx);
1821         } else if (unlinked) {
1822                 mutex_exit(&zp->z_lock);
1823                 zfs_unlinked_add(zp, tx);
1824         }
1825 
1826         txtype = TX_REMOVE;
1827         if (flags & FIGNORECASE)
1828                 txtype |= TX_CI;
1829         zfs_log_remove(zilog, tx, txtype, dzp, name, obj);
1830 
1831         dmu_tx_commit(tx);
1832 out:
1833         if (realnmp)
1834                 pn_free(realnmp);
1835 
1836         zfs_dirent_unlock(dl);
1837 
1838         if (!delete_now)
1839                 VN_RELE(vp);
1840         if (xzp)
1841                 VN_RELE(ZTOV(xzp));
1842 
1843         if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1844                 zil_commit(zilog, 0);
1845 
1846         ZFS_EXIT(zfsvfs);
1847         return (error);
1848 }
1849 
1850 /*
1851  * Create a new directory and insert it into dvp using the name
1852  * provided.  Return a pointer to the inserted directory.
1853  *
1854  *      IN:     dvp     - vnode of directory to add subdir to.
1855  *              dirname - name of new directory.
1856  *              vap     - attributes of new directory.
1857  *              cr      - credentials of caller.
1858  *              ct      - caller context
1859  *              flags   - case flags
1860  *              vsecp   - ACL to be set
1861  *
1862  *      OUT:    vpp     - vnode of created directory.
1863  *
1864  *      RETURN: 0 on success, error code on failure.
1865  *
1866  * Timestamps:
1867  *      dvp - ctime|mtime updated
1868  *       vp - ctime|mtime|atime updated
1869  */
1870 /*ARGSUSED*/
1871 static int
1872 zfs_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, cred_t *cr,
1873     caller_context_t *ct, int flags, vsecattr_t *vsecp)
1874 {
1875         znode_t         *zp, *dzp = VTOZ(dvp);
1876         zfsvfs_t        *zfsvfs = dzp->z_zfsvfs;
1877         zilog_t         *zilog;
1878         zfs_dirlock_t   *dl;
1879         uint64_t        txtype;
1880         dmu_tx_t        *tx;
1881         int             error;
1882         int             zf = ZNEW;
1883         ksid_t          *ksid;
1884         uid_t           uid;
1885         gid_t           gid = crgetgid(cr);
1886         zfs_acl_ids_t   acl_ids;
1887         boolean_t       fuid_dirtied;
1888         boolean_t       waited = B_FALSE;
1889 
1890         ASSERT(vap->va_type == VDIR);
1891 
1892         /*
1893          * If we have an ephemeral id, ACL, or XVATTR then
1894          * make sure file system is at proper version
1895          */
1896 
1897         ksid = crgetsid(cr, KSID_OWNER);
1898         if (ksid)
1899                 uid = ksid_getid(ksid);
1900         else
1901                 uid = crgetuid(cr);
1902         if (zfsvfs->z_use_fuids == B_FALSE &&
1903             (vsecp || (vap->va_mask & AT_XVATTR) ||
1904             IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
1905                 return (SET_ERROR(EINVAL));
1906 
1907         ZFS_ENTER(zfsvfs);
1908         ZFS_VERIFY_ZP(dzp);
1909         zilog = zfsvfs->z_log;
1910 
1911         if (dzp->z_pflags & ZFS_XATTR) {
1912                 ZFS_EXIT(zfsvfs);
1913                 return (SET_ERROR(EINVAL));
1914         }
1915 
1916         if (zfsvfs->z_utf8 && u8_validate(dirname,
1917             strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1918                 ZFS_EXIT(zfsvfs);
1919                 return (SET_ERROR(EILSEQ));
1920         }
1921         if (flags & FIGNORECASE)
1922                 zf |= ZCILOOK;
1923 
1924         if (vap->va_mask & AT_XVATTR) {
1925                 if ((error = secpolicy_xvattr((xvattr_t *)vap,
1926                     crgetuid(cr), cr, vap->va_type)) != 0) {
1927                         ZFS_EXIT(zfsvfs);
1928                         return (error);
1929                 }
1930         }
1931 
1932         if ((error = zfs_acl_ids_create(dzp, 0, vap, cr,
1933             vsecp, &acl_ids)) != 0) {
1934                 ZFS_EXIT(zfsvfs);
1935                 return (error);
1936         }
1937         /*
1938          * First make sure the new directory doesn't exist.
1939          *
1940          * Existence is checked first to make sure we don't return
1941          * EACCES instead of EEXIST which can cause some applications
1942          * to fail.
1943          */
1944 top:
1945         *vpp = NULL;
1946 
1947         if (error = zfs_dirent_lock(&dl, dzp, dirname, &zp, zf,
1948             NULL, NULL)) {
1949                 zfs_acl_ids_free(&acl_ids);
1950                 ZFS_EXIT(zfsvfs);
1951                 return (error);
1952         }
1953 
1954         if (error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr)) {
1955                 zfs_acl_ids_free(&acl_ids);
1956                 zfs_dirent_unlock(dl);
1957                 ZFS_EXIT(zfsvfs);
1958                 return (error);
1959         }
1960 
1961         if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
1962                 zfs_acl_ids_free(&acl_ids);
1963                 zfs_dirent_unlock(dl);
1964                 ZFS_EXIT(zfsvfs);
1965                 return (SET_ERROR(EDQUOT));
1966         }
1967 
1968         /*
1969          * Add a new entry to the directory.
1970          */
1971         tx = dmu_tx_create(zfsvfs->z_os);
1972         dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname);
1973         dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
1974         fuid_dirtied = zfsvfs->z_fuid_dirty;
1975         if (fuid_dirtied)
1976                 zfs_fuid_txhold(zfsvfs, tx);
1977         if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1978                 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
1979                     acl_ids.z_aclp->z_acl_bytes);
1980         }
1981 
1982         dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1983             ZFS_SA_BASE_ATTR_SIZE);
1984 
1985         error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
1986         if (error) {
1987                 zfs_dirent_unlock(dl);
1988                 if (error == ERESTART) {
1989                         waited = B_TRUE;
1990                         dmu_tx_wait(tx);
1991                         dmu_tx_abort(tx);
1992                         goto top;
1993                 }
1994                 zfs_acl_ids_free(&acl_ids);
1995                 dmu_tx_abort(tx);
1996                 ZFS_EXIT(zfsvfs);
1997                 return (error);
1998         }
1999 
2000         /*
2001          * Create new node.
2002          */
2003         zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
2004 
2005         if (fuid_dirtied)
2006                 zfs_fuid_sync(zfsvfs, tx);
2007 
2008         /*
2009          * Now put new name in parent dir.
2010          */
2011         (void) zfs_link_create(dl, zp, tx, ZNEW);
2012 
2013         *vpp = ZTOV(zp);
2014 
2015         txtype = zfs_log_create_txtype(Z_DIR, vsecp, vap);
2016         if (flags & FIGNORECASE)
2017                 txtype |= TX_CI;
2018         zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, vsecp,
2019             acl_ids.z_fuidp, vap);
2020 
2021         zfs_acl_ids_free(&acl_ids);
2022 
2023         dmu_tx_commit(tx);
2024 
2025         zfs_dirent_unlock(dl);
2026 
2027         if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
2028                 zil_commit(zilog, 0);
2029 
2030         ZFS_EXIT(zfsvfs);
2031         return (0);
2032 }
2033 
2034 /*
2035  * Remove a directory subdir entry.  If the current working
2036  * directory is the same as the subdir to be removed, the
2037  * remove will fail.
2038  *
2039  *      IN:     dvp     - vnode of directory to remove from.
2040  *              name    - name of directory to be removed.
2041  *              cwd     - vnode of current working directory.
2042  *              cr      - credentials of caller.
2043  *              ct      - caller context
2044  *              flags   - case flags
2045  *
2046  *      RETURN: 0 on success, error code on failure.
2047  *
2048  * Timestamps:
2049  *      dvp - ctime|mtime updated
2050  */
2051 /*ARGSUSED*/
2052 static int
2053 zfs_rmdir(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr,
2054     caller_context_t *ct, int flags)
2055 {
2056         znode_t         *dzp = VTOZ(dvp);
2057         znode_t         *zp;
2058         vnode_t         *vp;
2059         zfsvfs_t        *zfsvfs = dzp->z_zfsvfs;
2060         zilog_t         *zilog;
2061         zfs_dirlock_t   *dl;
2062         dmu_tx_t        *tx;
2063         int             error;
2064         int             zflg = ZEXISTS;
2065         boolean_t       waited = B_FALSE;
2066 
2067         ZFS_ENTER(zfsvfs);
2068         ZFS_VERIFY_ZP(dzp);
2069         zilog = zfsvfs->z_log;
2070 
2071         if (flags & FIGNORECASE)
2072                 zflg |= ZCILOOK;
2073 top:
2074         zp = NULL;
2075 
2076         /*
2077          * Attempt to lock directory; fail if entry doesn't exist.
2078          */
2079         if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
2080             NULL, NULL)) {
2081                 ZFS_EXIT(zfsvfs);
2082                 return (error);
2083         }
2084 
2085         vp = ZTOV(zp);
2086 
2087         if (error = zfs_zaccess_delete(dzp, zp, cr)) {
2088                 goto out;
2089         }
2090 
2091         if (vp->v_type != VDIR) {
2092                 error = SET_ERROR(ENOTDIR);
2093                 goto out;
2094         }
2095 
2096         if (vp == cwd) {
2097                 error = SET_ERROR(EINVAL);
2098                 goto out;
2099         }
2100 
2101         vnevent_rmdir(vp, dvp, name, ct);
2102 
2103         /*
2104          * Grab a lock on the directory to make sure that noone is
2105          * trying to add (or lookup) entries while we are removing it.
2106          */
2107         rw_enter(&zp->z_name_lock, RW_WRITER);
2108 
2109         /*
2110          * Grab a lock on the parent pointer to make sure we play well
2111          * with the treewalk and directory rename code.
2112          */
2113         rw_enter(&zp->z_parent_lock, RW_WRITER);
2114 
2115         tx = dmu_tx_create(zfsvfs->z_os);
2116         dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
2117         dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
2118         dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
2119         zfs_sa_upgrade_txholds(tx, zp);
2120         zfs_sa_upgrade_txholds(tx, dzp);
2121         dmu_tx_mark_netfree(tx);
2122         error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
2123         if (error) {
2124                 rw_exit(&zp->z_parent_lock);
2125                 rw_exit(&zp->z_name_lock);
2126                 zfs_dirent_unlock(dl);
2127                 VN_RELE(vp);
2128                 if (error == ERESTART) {
2129                         waited = B_TRUE;
2130                         dmu_tx_wait(tx);
2131                         dmu_tx_abort(tx);
2132                         goto top;
2133                 }
2134                 dmu_tx_abort(tx);
2135                 ZFS_EXIT(zfsvfs);
2136                 return (error);
2137         }
2138 
2139         error = zfs_link_destroy(dl, zp, tx, zflg, NULL);
2140 
2141         if (error == 0) {
2142                 uint64_t txtype = TX_RMDIR;
2143                 if (flags & FIGNORECASE)
2144                         txtype |= TX_CI;
2145                 zfs_log_remove(zilog, tx, txtype, dzp, name, ZFS_NO_OBJECT);
2146         }
2147 
2148         dmu_tx_commit(tx);
2149 
2150         rw_exit(&zp->z_parent_lock);
2151         rw_exit(&zp->z_name_lock);
2152 out:
2153         zfs_dirent_unlock(dl);
2154 
2155         VN_RELE(vp);
2156 
2157         if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
2158                 zil_commit(zilog, 0);
2159 
2160         ZFS_EXIT(zfsvfs);
2161         return (error);
2162 }
2163 
2164 /*
2165  * Read as many directory entries as will fit into the provided
2166  * buffer from the given directory cursor position (specified in
2167  * the uio structure).
2168  *
2169  *      IN:     vp      - vnode of directory to read.
2170  *              uio     - structure supplying read location, range info,
2171  *                        and return buffer.
2172  *              cr      - credentials of caller.
2173  *              ct      - caller context
2174  *              flags   - case flags
2175  *
2176  *      OUT:    uio     - updated offset and range, buffer filled.
2177  *              eofp    - set to true if end-of-file detected.
2178  *
2179  *      RETURN: 0 on success, error code on failure.
2180  *
2181  * Timestamps:
2182  *      vp - atime updated
2183  *
2184  * Note that the low 4 bits of the cookie returned by zap is always zero.
2185  * This allows us to use the low range for "special" directory entries:
2186  * We use 0 for '.', and 1 for '..'.  If this is the root of the filesystem,
2187  * we use the offset 2 for the '.zfs' directory.
2188  */
2189 /* ARGSUSED */
2190 static int
2191 zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp,
2192     caller_context_t *ct, int flags)
2193 {
2194         znode_t         *zp = VTOZ(vp);
2195         iovec_t         *iovp;
2196         edirent_t       *eodp;
2197         dirent64_t      *odp;
2198         zfsvfs_t        *zfsvfs = zp->z_zfsvfs;
2199         objset_t        *os;
2200         caddr_t         outbuf;
2201         size_t          bufsize;
2202         zap_cursor_t    zc;
2203         zap_attribute_t zap;
2204         uint_t          bytes_wanted;
2205         uint64_t        offset; /* must be unsigned; checks for < 1 */
2206         uint64_t        parent;
2207         int             local_eof;
2208         int             outcount;
2209         int             error;
2210         uint8_t         prefetch;
2211         boolean_t       check_sysattrs;
2212 
2213         ZFS_ENTER(zfsvfs);
2214         ZFS_VERIFY_ZP(zp);
2215 
2216         if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
2217             &parent, sizeof (parent))) != 0) {
2218                 ZFS_EXIT(zfsvfs);
2219                 return (error);
2220         }
2221 
2222         /*
2223          * If we are not given an eof variable,
2224          * use a local one.
2225          */
2226         if (eofp == NULL)
2227                 eofp = &local_eof;
2228 
2229         /*
2230          * Check for valid iov_len.
2231          */
2232         if (uio->uio_iov->iov_len <= 0) {
2233                 ZFS_EXIT(zfsvfs);
2234                 return (SET_ERROR(EINVAL));
2235         }
2236 
2237         /*
2238          * Quit if directory has been removed (posix)
2239          */
2240         if ((*eofp = zp->z_unlinked) != 0) {
2241                 ZFS_EXIT(zfsvfs);
2242                 return (0);
2243         }
2244 
2245         error = 0;
2246         os = zfsvfs->z_os;
2247         offset = uio->uio_loffset;
2248         prefetch = zp->z_zn_prefetch;
2249 
2250         /*
2251          * Initialize the iterator cursor.
2252          */
2253         if (offset <= 3) {
2254                 /*
2255                  * Start iteration from the beginning of the directory.
2256                  */
2257                 zap_cursor_init(&zc, os, zp->z_id);
2258         } else {
2259                 /*
2260                  * The offset is a serialized cursor.
2261                  */
2262                 zap_cursor_init_serialized(&zc, os, zp->z_id, offset);
2263         }
2264 
2265         /*
2266          * Get space to change directory entries into fs independent format.
2267          */
2268         iovp = uio->uio_iov;
2269         bytes_wanted = iovp->iov_len;
2270         if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) {
2271                 bufsize = bytes_wanted;
2272                 outbuf = kmem_alloc(bufsize, KM_SLEEP);
2273                 odp = (struct dirent64 *)outbuf;
2274         } else {
2275                 bufsize = bytes_wanted;
2276                 outbuf = NULL;
2277                 odp = (struct dirent64 *)iovp->iov_base;
2278         }
2279         eodp = (struct edirent *)odp;
2280 
2281         /*
2282          * If this VFS supports the system attribute view interface; and
2283          * we're looking at an extended attribute directory; and we care
2284          * about normalization conflicts on this vfs; then we must check
2285          * for normalization conflicts with the sysattr name space.
2286          */
2287         check_sysattrs = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
2288             (vp->v_flag & V_XATTRDIR) && zfsvfs->z_norm &&
2289             (flags & V_RDDIR_ENTFLAGS);
2290 
2291         /*
2292          * Transform to file-system independent format
2293          */
2294         outcount = 0;
2295         while (outcount < bytes_wanted) {
2296                 ino64_t objnum;
2297                 ushort_t reclen;
2298                 off64_t *next = NULL;
2299 
2300                 /*
2301                  * Special case `.', `..', and `.zfs'.
2302                  */
2303                 if (offset == 0) {
2304                         (void) strcpy(zap.za_name, ".");
2305                         zap.za_normalization_conflict = 0;
2306                         objnum = zp->z_id;
2307                 } else if (offset == 1) {
2308                         (void) strcpy(zap.za_name, "..");
2309                         zap.za_normalization_conflict = 0;
2310                         objnum = parent;
2311                 } else if (offset == 2 && zfs_show_ctldir(zp)) {
2312                         (void) strcpy(zap.za_name, ZFS_CTLDIR_NAME);
2313                         zap.za_normalization_conflict = 0;
2314                         objnum = ZFSCTL_INO_ROOT;
2315                 } else {
2316                         /*
2317                          * Grab next entry.
2318                          */
2319                         if (error = zap_cursor_retrieve(&zc, &zap)) {
2320                                 if ((*eofp = (error == ENOENT)) != 0)
2321                                         break;
2322                                 else
2323                                         goto update;
2324                         }
2325 
2326                         if (zap.za_integer_length != 8 ||
2327                             zap.za_num_integers != 1) {
2328                                 cmn_err(CE_WARN, "zap_readdir: bad directory "
2329                                     "entry, obj = %lld, offset = %lld\n",
2330                                     (u_longlong_t)zp->z_id,
2331                                     (u_longlong_t)offset);
2332                                 error = SET_ERROR(ENXIO);
2333                                 goto update;
2334                         }
2335 
2336                         objnum = ZFS_DIRENT_OBJ(zap.za_first_integer);
2337                         /*
2338                          * MacOS X can extract the object type here such as:
2339                          * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer);
2340                          */
2341 
2342                         if (check_sysattrs && !zap.za_normalization_conflict) {
2343                                 zap.za_normalization_conflict =
2344                                     xattr_sysattr_casechk(zap.za_name);
2345                         }
2346                 }
2347 
2348                 if (flags & V_RDDIR_ACCFILTER) {
2349                         /*
2350                          * If we have no access at all, don't include
2351                          * this entry in the returned information
2352                          */
2353                         znode_t *ezp;
2354                         if (zfs_zget(zp->z_zfsvfs, objnum, &ezp) != 0)
2355                                 goto skip_entry;
2356                         if (!zfs_has_access(ezp, cr)) {
2357                                 VN_RELE(ZTOV(ezp));
2358                                 goto skip_entry;
2359                         }
2360                         VN_RELE(ZTOV(ezp));
2361                 }
2362 
2363                 if (flags & V_RDDIR_ENTFLAGS)
2364                         reclen = EDIRENT_RECLEN(strlen(zap.za_name));
2365                 else
2366                         reclen = DIRENT64_RECLEN(strlen(zap.za_name));
2367 
2368                 /*
2369                  * Will this entry fit in the buffer?
2370                  */
2371                 if (outcount + reclen > bufsize) {
2372                         /*
2373                          * Did we manage to fit anything in the buffer?
2374                          */
2375                         if (!outcount) {
2376                                 error = SET_ERROR(EINVAL);
2377                                 goto update;
2378                         }
2379                         break;
2380                 }
2381                 if (flags & V_RDDIR_ENTFLAGS) {
2382                         /*
2383                          * Add extended flag entry:
2384                          */
2385                         eodp->ed_ino = objnum;
2386                         eodp->ed_reclen = reclen;
2387                         /* NOTE: ed_off is the offset for the *next* entry */
2388                         next = &(eodp->ed_off);
2389                         eodp->ed_eflags = zap.za_normalization_conflict ?
2390                             ED_CASE_CONFLICT : 0;
2391                         (void) strncpy(eodp->ed_name, zap.za_name,
2392                             EDIRENT_NAMELEN(reclen));
2393                         eodp = (edirent_t *)((intptr_t)eodp + reclen);
2394                 } else {
2395                         /*
2396                          * Add normal entry:
2397                          */
2398                         odp->d_ino = objnum;
2399                         odp->d_reclen = reclen;
2400                         /* NOTE: d_off is the offset for the *next* entry */
2401                         next = &(odp->d_off);
2402                         (void) strncpy(odp->d_name, zap.za_name,
2403                             DIRENT64_NAMELEN(reclen));
2404                         odp = (dirent64_t *)((intptr_t)odp + reclen);
2405                 }
2406                 outcount += reclen;
2407 
2408                 ASSERT(outcount <= bufsize);
2409 
2410                 /* Prefetch znode */
2411                 if (prefetch)
2412                         dmu_prefetch(os, objnum, 0, 0, 0,
2413                             ZIO_PRIORITY_SYNC_READ);
2414 
2415         skip_entry:
2416                 /*
2417                  * Move to the next entry, fill in the previous offset.
2418                  */
2419                 if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) {
2420                         zap_cursor_advance(&zc);
2421                         offset = zap_cursor_serialize(&zc);
2422                 } else {
2423                         offset += 1;
2424                 }
2425                 if (next)
2426                         *next = offset;
2427         }
2428         zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */
2429 
2430         if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) {
2431                 iovp->iov_base += outcount;
2432                 iovp->iov_len -= outcount;
2433                 uio->uio_resid -= outcount;
2434         } else if (error = uiomove(outbuf, (long)outcount, UIO_READ, uio)) {
2435                 /*
2436                  * Reset the pointer.
2437                  */
2438                 offset = uio->uio_loffset;
2439         }
2440 
2441 update:
2442         zap_cursor_fini(&zc);
2443         if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
2444                 kmem_free(outbuf, bufsize);
2445 
2446         if (error == ENOENT)
2447                 error = 0;
2448 
2449         ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
2450 
2451         uio->uio_loffset = offset;
2452         ZFS_EXIT(zfsvfs);
2453         return (error);
2454 }
2455 
2456 ulong_t zfs_fsync_sync_cnt = 4;
2457 
2458 static int
2459 zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct)
2460 {
2461         znode_t *zp = VTOZ(vp);
2462         zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2463 
2464         /*
2465          * Regardless of whether this is required for standards conformance,
2466          * this is the logical behavior when fsync() is called on a file with
2467          * dirty pages.  We use B_ASYNC since the ZIL transactions are already
2468          * going to be pushed out as part of the zil_commit().
2469          */
2470         if (vn_has_cached_data(vp) && !(syncflag & FNODSYNC) &&
2471             (vp->v_type == VREG) && !(IS_SWAPVP(vp)))
2472                 (void) VOP_PUTPAGE(vp, (offset_t)0, (size_t)0, B_ASYNC, cr, ct);
2473 
2474         (void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt);
2475 
2476         if (zfsvfs->z_os->os_sync != ZFS_SYNC_DISABLED) {
2477                 ZFS_ENTER(zfsvfs);
2478                 ZFS_VERIFY_ZP(zp);
2479                 zil_commit(zfsvfs->z_log, zp->z_id);
2480                 ZFS_EXIT(zfsvfs);
2481         }
2482         return (0);
2483 }
2484 
2485 
2486 /*
2487  * Get the requested file attributes and place them in the provided
2488  * vattr structure.
2489  *
2490  *      IN:     vp      - vnode of file.
2491  *              vap     - va_mask identifies requested attributes.
2492  *                        If AT_XVATTR set, then optional attrs are requested
2493  *              flags   - ATTR_NOACLCHECK (CIFS server context)
2494  *              cr      - credentials of caller.
2495  *              ct      - caller context
2496  *
2497  *      OUT:    vap     - attribute values.
2498  *
2499  *      RETURN: 0 (always succeeds).
2500  */
2501 /* ARGSUSED */
2502 static int
2503 zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
2504     caller_context_t *ct)
2505 {
2506         znode_t *zp = VTOZ(vp);
2507         zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2508         int     error = 0;
2509         uint64_t links;
2510         uint64_t mtime[2], ctime[2];
2511         xvattr_t *xvap = (xvattr_t *)vap;       /* vap may be an xvattr_t * */
2512         xoptattr_t *xoap = NULL;
2513         boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2514         sa_bulk_attr_t bulk[2];
2515         int count = 0;
2516 
2517         ZFS_ENTER(zfsvfs);
2518         ZFS_VERIFY_ZP(zp);
2519 
2520         zfs_fuid_map_ids(zp, cr, &vap->va_uid, &vap->va_gid);
2521 
2522         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
2523         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
2524 
2525         if ((error = sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) != 0) {
2526                 ZFS_EXIT(zfsvfs);
2527                 return (error);
2528         }
2529 
2530         /*
2531          * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
2532          * Also, if we are the owner don't bother, since owner should
2533          * always be allowed to read basic attributes of file.
2534          */
2535         if (!(zp->z_pflags & ZFS_ACL_TRIVIAL) &&
2536             (vap->va_uid != crgetuid(cr))) {
2537                 if (error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0,
2538                     skipaclchk, cr)) {
2539                         ZFS_EXIT(zfsvfs);
2540                         return (error);
2541                 }
2542         }
2543 
2544         /*
2545          * Return all attributes.  It's cheaper to provide the answer
2546          * than to determine whether we were asked the question.
2547          */
2548 
2549         mutex_enter(&zp->z_lock);
2550         vap->va_type = vp->v_type;
2551         vap->va_mode = zp->z_mode & MODEMASK;
2552         vap->va_fsid = zp->z_zfsvfs->z_vfs->vfs_dev;
2553         vap->va_nodeid = zp->z_id;
2554         if ((vp->v_flag & VROOT) && zfs_show_ctldir(zp))
2555                 links = zp->z_links + 1;
2556         else
2557                 links = zp->z_links;
2558         vap->va_nlink = MIN(links, UINT32_MAX);      /* nlink_t limit! */
2559         vap->va_size = zp->z_size;
2560         vap->va_rdev = vp->v_rdev;
2561         vap->va_seq = zp->z_seq;
2562 
2563         /*
2564          * Add in any requested optional attributes and the create time.
2565          * Also set the corresponding bits in the returned attribute bitmap.
2566          */
2567         if ((xoap = xva_getxoptattr(xvap)) != NULL && zfsvfs->z_use_fuids) {
2568                 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
2569                         xoap->xoa_archive =
2570                             ((zp->z_pflags & ZFS_ARCHIVE) != 0);
2571                         XVA_SET_RTN(xvap, XAT_ARCHIVE);
2572                 }
2573 
2574                 if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
2575                         xoap->xoa_readonly =
2576                             ((zp->z_pflags & ZFS_READONLY) != 0);
2577                         XVA_SET_RTN(xvap, XAT_READONLY);
2578                 }
2579 
2580                 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
2581                         xoap->xoa_system =
2582                             ((zp->z_pflags & ZFS_SYSTEM) != 0);
2583                         XVA_SET_RTN(xvap, XAT_SYSTEM);
2584                 }
2585 
2586                 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
2587                         xoap->xoa_hidden =
2588                             ((zp->z_pflags & ZFS_HIDDEN) != 0);
2589                         XVA_SET_RTN(xvap, XAT_HIDDEN);
2590                 }
2591 
2592                 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2593                         xoap->xoa_nounlink =
2594                             ((zp->z_pflags & ZFS_NOUNLINK) != 0);
2595                         XVA_SET_RTN(xvap, XAT_NOUNLINK);
2596                 }
2597 
2598                 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2599                         xoap->xoa_immutable =
2600                             ((zp->z_pflags & ZFS_IMMUTABLE) != 0);
2601                         XVA_SET_RTN(xvap, XAT_IMMUTABLE);
2602                 }
2603 
2604                 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2605                         xoap->xoa_appendonly =
2606                             ((zp->z_pflags & ZFS_APPENDONLY) != 0);
2607                         XVA_SET_RTN(xvap, XAT_APPENDONLY);
2608                 }
2609 
2610                 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2611                         xoap->xoa_nodump =
2612                             ((zp->z_pflags & ZFS_NODUMP) != 0);
2613                         XVA_SET_RTN(xvap, XAT_NODUMP);
2614                 }
2615 
2616                 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
2617                         xoap->xoa_opaque =
2618                             ((zp->z_pflags & ZFS_OPAQUE) != 0);
2619                         XVA_SET_RTN(xvap, XAT_OPAQUE);
2620                 }
2621 
2622                 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2623                         xoap->xoa_av_quarantined =
2624                             ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0);
2625                         XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
2626                 }
2627 
2628                 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2629                         xoap->xoa_av_modified =
2630                             ((zp->z_pflags & ZFS_AV_MODIFIED) != 0);
2631                         XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
2632                 }
2633 
2634                 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) &&
2635                     vp->v_type == VREG) {
2636                         zfs_sa_get_scanstamp(zp, xvap);
2637                 }
2638 
2639                 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
2640                         uint64_t times[2];
2641 
2642                         (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(zfsvfs),
2643                             times, sizeof (times));
2644                         ZFS_TIME_DECODE(&xoap->xoa_createtime, times);
2645                         XVA_SET_RTN(xvap, XAT_CREATETIME);
2646                 }
2647 
2648                 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
2649                         xoap->xoa_reparse = ((zp->z_pflags & ZFS_REPARSE) != 0);
2650                         XVA_SET_RTN(xvap, XAT_REPARSE);
2651                 }
2652                 if (XVA_ISSET_REQ(xvap, XAT_GEN)) {
2653                         xoap->xoa_generation = zp->z_gen;
2654                         XVA_SET_RTN(xvap, XAT_GEN);
2655                 }
2656 
2657                 if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) {
2658                         xoap->xoa_offline =
2659                             ((zp->z_pflags & ZFS_OFFLINE) != 0);
2660                         XVA_SET_RTN(xvap, XAT_OFFLINE);
2661                 }
2662 
2663                 if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) {
2664                         xoap->xoa_sparse =
2665                             ((zp->z_pflags & ZFS_SPARSE) != 0);
2666                         XVA_SET_RTN(xvap, XAT_SPARSE);
2667                 }
2668         }
2669 
2670         ZFS_TIME_DECODE(&vap->va_atime, zp->z_atime);
2671         ZFS_TIME_DECODE(&vap->va_mtime, mtime);
2672         ZFS_TIME_DECODE(&vap->va_ctime, ctime);
2673 
2674         mutex_exit(&zp->z_lock);
2675 
2676         sa_object_size(zp->z_sa_hdl, &vap->va_blksize, &vap->va_nblocks);
2677 
2678         if (zp->z_blksz == 0) {
2679                 /*
2680                  * Block size hasn't been set; suggest maximal I/O transfers.
2681                  */
2682                 vap->va_blksize = zfsvfs->z_max_blksz;
2683         }
2684 
2685         ZFS_EXIT(zfsvfs);
2686         return (0);
2687 }
2688 
2689 /*
2690  * Set the file attributes to the values contained in the
2691  * vattr structure.
2692  *
2693  *      IN:     vp      - vnode of file to be modified.
2694  *              vap     - new attribute values.
2695  *                        If AT_XVATTR set, then optional attrs are being set
2696  *              flags   - ATTR_UTIME set if non-default time values provided.
2697  *                      - ATTR_NOACLCHECK (CIFS context only).
2698  *              cr      - credentials of caller.
2699  *              ct      - caller context
2700  *
2701  *      RETURN: 0 on success, error code on failure.
2702  *
2703  * Timestamps:
2704  *      vp - ctime updated, mtime updated if size changed.
2705  */
2706 /* ARGSUSED */
2707 static int
2708 zfs_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
2709     caller_context_t *ct)
2710 {
2711         znode_t         *zp = VTOZ(vp);
2712         zfsvfs_t        *zfsvfs = zp->z_zfsvfs;
2713         zilog_t         *zilog;
2714         dmu_tx_t        *tx;
2715         vattr_t         oldva;
2716         xvattr_t        tmpxvattr;
2717         uint_t          mask = vap->va_mask;
2718         uint_t          saved_mask = 0;
2719         int             trim_mask = 0;
2720         uint64_t        new_mode;
2721         uint64_t        new_uid, new_gid;
2722         uint64_t        xattr_obj;
2723         uint64_t        mtime[2], ctime[2];
2724         znode_t         *attrzp;
2725         int             need_policy = FALSE;
2726         int             err, err2;
2727         zfs_fuid_info_t *fuidp = NULL;
2728         xvattr_t *xvap = (xvattr_t *)vap;       /* vap may be an xvattr_t * */
2729         xoptattr_t      *xoap;
2730         zfs_acl_t       *aclp;
2731         boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2732         boolean_t       fuid_dirtied = B_FALSE;
2733         sa_bulk_attr_t  bulk[7], xattr_bulk[7];
2734         int             count = 0, xattr_count = 0;
2735 
2736         if (mask == 0)
2737                 return (0);
2738 
2739         if (mask & AT_NOSET)
2740                 return (SET_ERROR(EINVAL));
2741 
2742         ZFS_ENTER(zfsvfs);
2743         ZFS_VERIFY_ZP(zp);
2744 
2745         zilog = zfsvfs->z_log;
2746 
2747         /*
2748          * Make sure that if we have ephemeral uid/gid or xvattr specified
2749          * that file system is at proper version level
2750          */
2751 
2752         if (zfsvfs->z_use_fuids == B_FALSE &&
2753             (((mask & AT_UID) && IS_EPHEMERAL(vap->va_uid)) ||
2754             ((mask & AT_GID) && IS_EPHEMERAL(vap->va_gid)) ||
2755             (mask & AT_XVATTR))) {
2756                 ZFS_EXIT(zfsvfs);
2757                 return (SET_ERROR(EINVAL));
2758         }
2759 
2760         if (mask & AT_SIZE && vp->v_type == VDIR) {
2761                 ZFS_EXIT(zfsvfs);
2762                 return (SET_ERROR(EISDIR));
2763         }
2764 
2765         if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO) {
2766                 ZFS_EXIT(zfsvfs);
2767                 return (SET_ERROR(EINVAL));
2768         }
2769 
2770         /*
2771          * If this is an xvattr_t, then get a pointer to the structure of
2772          * optional attributes.  If this is NULL, then we have a vattr_t.
2773          */
2774         xoap = xva_getxoptattr(xvap);
2775 
2776         xva_init(&tmpxvattr);
2777 
2778         /*
2779          * Immutable files can only alter immutable bit and atime
2780          */
2781         if ((zp->z_pflags & ZFS_IMMUTABLE) &&
2782             ((mask & (AT_SIZE|AT_UID|AT_GID|AT_MTIME|AT_MODE)) ||
2783             ((mask & AT_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) {
2784                 ZFS_EXIT(zfsvfs);
2785                 return (SET_ERROR(EPERM));
2786         }
2787 
2788         if ((mask & AT_SIZE) && (zp->z_pflags & ZFS_READONLY)) {
2789                 ZFS_EXIT(zfsvfs);
2790                 return (SET_ERROR(EPERM));
2791         }
2792 
2793         /*
2794          * Verify timestamps doesn't overflow 32 bits.
2795          * ZFS can handle large timestamps, but 32bit syscalls can't
2796          * handle times greater than 2039.  This check should be removed
2797          * once large timestamps are fully supported.
2798          */
2799         if (mask & (AT_ATIME | AT_MTIME)) {
2800                 if (((mask & AT_ATIME) && TIMESPEC_OVERFLOW(&vap->va_atime)) ||
2801                     ((mask & AT_MTIME) && TIMESPEC_OVERFLOW(&vap->va_mtime))) {
2802                         ZFS_EXIT(zfsvfs);
2803                         return (SET_ERROR(EOVERFLOW));
2804                 }
2805         }
2806 
2807 top:
2808         attrzp = NULL;
2809         aclp = NULL;
2810 
2811         /* Can this be moved to before the top label? */
2812         if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
2813                 ZFS_EXIT(zfsvfs);
2814                 return (SET_ERROR(EROFS));
2815         }
2816 
2817         /*
2818          * First validate permissions
2819          */
2820 
2821         if (mask & AT_SIZE) {
2822                 err = zfs_zaccess(zp, ACE_WRITE_DATA, 0, skipaclchk, cr);
2823                 if (err) {
2824                         ZFS_EXIT(zfsvfs);
2825                         return (err);
2826                 }
2827                 /*
2828                  * XXX - Note, we are not providing any open
2829                  * mode flags here (like FNDELAY), so we may
2830                  * block if there are locks present... this
2831                  * should be addressed in openat().
2832                  */
2833                 /* XXX - would it be OK to generate a log record here? */
2834                 err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE);
2835                 if (err) {
2836                         ZFS_EXIT(zfsvfs);
2837                         return (err);
2838                 }
2839 
2840                 if (vap->va_size == 0) {
2841                         vnevent_truncate(ZTOV(zp), ct);
2842                 } else {
2843                         vnevent_resize(ZTOV(zp), ct);
2844                 }
2845         }
2846 
2847         if (mask & (AT_ATIME|AT_MTIME) ||
2848             ((mask & AT_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) ||
2849             XVA_ISSET_REQ(xvap, XAT_READONLY) ||
2850             XVA_ISSET_REQ(xvap, XAT_ARCHIVE) ||
2851             XVA_ISSET_REQ(xvap, XAT_OFFLINE) ||
2852             XVA_ISSET_REQ(xvap, XAT_SPARSE) ||
2853             XVA_ISSET_REQ(xvap, XAT_CREATETIME) ||
2854             XVA_ISSET_REQ(xvap, XAT_SYSTEM)))) {
2855                 need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0,
2856                     skipaclchk, cr);
2857         }
2858 
2859         if (mask & (AT_UID|AT_GID)) {
2860                 int     idmask = (mask & (AT_UID|AT_GID));
2861                 int     take_owner;
2862                 int     take_group;
2863 
2864                 /*
2865                  * NOTE: even if a new mode is being set,
2866                  * we may clear S_ISUID/S_ISGID bits.
2867                  */
2868 
2869                 if (!(mask & AT_MODE))
2870                         vap->va_mode = zp->z_mode;
2871 
2872                 /*
2873                  * Take ownership or chgrp to group we are a member of
2874                  */
2875 
2876                 take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr));
2877                 take_group = (mask & AT_GID) &&
2878                     zfs_groupmember(zfsvfs, vap->va_gid, cr);
2879 
2880                 /*
2881                  * If both AT_UID and AT_GID are set then take_owner and
2882                  * take_group must both be set in order to allow taking
2883                  * ownership.
2884                  *
2885                  * Otherwise, send the check through secpolicy_vnode_setattr()
2886                  *
2887                  */
2888 
2889                 if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) ||
2890                     ((idmask == AT_UID) && take_owner) ||
2891                     ((idmask == AT_GID) && take_group)) {
2892                         if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0,
2893                             skipaclchk, cr) == 0) {
2894                                 /*
2895                                  * Remove setuid/setgid for non-privileged users
2896                                  */
2897                                 secpolicy_setid_clear(vap, cr);
2898                                 trim_mask = (mask & (AT_UID|AT_GID));
2899                         } else {
2900                                 need_policy =  TRUE;
2901                         }
2902                 } else {
2903                         need_policy =  TRUE;
2904                 }
2905         }
2906 
2907         mutex_enter(&zp->z_lock);
2908         oldva.va_mode = zp->z_mode;
2909         zfs_fuid_map_ids(zp, cr, &oldva.va_uid, &oldva.va_gid);
2910         if (mask & AT_XVATTR) {
2911                 /*
2912                  * Update xvattr mask to include only those attributes
2913                  * that are actually changing.
2914                  *
2915                  * the bits will be restored prior to actually setting
2916                  * the attributes so the caller thinks they were set.
2917                  */
2918                 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2919                         if (xoap->xoa_appendonly !=
2920                             ((zp->z_pflags & ZFS_APPENDONLY) != 0)) {
2921                                 need_policy = TRUE;
2922                         } else {
2923                                 XVA_CLR_REQ(xvap, XAT_APPENDONLY);
2924                                 XVA_SET_REQ(&tmpxvattr, XAT_APPENDONLY);
2925                         }
2926                 }
2927 
2928                 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2929                         if (xoap->xoa_nounlink !=
2930                             ((zp->z_pflags & ZFS_NOUNLINK) != 0)) {
2931                                 need_policy = TRUE;
2932                         } else {
2933                                 XVA_CLR_REQ(xvap, XAT_NOUNLINK);
2934                                 XVA_SET_REQ(&tmpxvattr, XAT_NOUNLINK);
2935                         }
2936                 }
2937 
2938                 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2939                         if (xoap->xoa_immutable !=
2940                             ((zp->z_pflags & ZFS_IMMUTABLE) != 0)) {
2941                                 need_policy = TRUE;
2942                         } else {
2943                                 XVA_CLR_REQ(xvap, XAT_IMMUTABLE);
2944                                 XVA_SET_REQ(&tmpxvattr, XAT_IMMUTABLE);
2945                         }
2946                 }
2947 
2948                 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2949                         if (xoap->xoa_nodump !=
2950                             ((zp->z_pflags & ZFS_NODUMP) != 0)) {
2951                                 need_policy = TRUE;
2952                         } else {
2953                                 XVA_CLR_REQ(xvap, XAT_NODUMP);
2954                                 XVA_SET_REQ(&tmpxvattr, XAT_NODUMP);
2955                         }
2956                 }
2957 
2958                 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2959                         if (xoap->xoa_av_modified !=
2960                             ((zp->z_pflags & ZFS_AV_MODIFIED) != 0)) {
2961                                 need_policy = TRUE;
2962                         } else {
2963                                 XVA_CLR_REQ(xvap, XAT_AV_MODIFIED);
2964                                 XVA_SET_REQ(&tmpxvattr, XAT_AV_MODIFIED);
2965                         }
2966                 }
2967 
2968                 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2969                         if ((vp->v_type != VREG &&
2970                             xoap->xoa_av_quarantined) ||
2971                             xoap->xoa_av_quarantined !=
2972                             ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0)) {
2973                                 need_policy = TRUE;
2974                         } else {
2975                                 XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED);
2976                                 XVA_SET_REQ(&tmpxvattr, XAT_AV_QUARANTINED);
2977                         }
2978                 }
2979 
2980                 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
2981                         mutex_exit(&zp->z_lock);
2982                         ZFS_EXIT(zfsvfs);
2983                         return (SET_ERROR(EPERM));
2984                 }
2985 
2986                 if (need_policy == FALSE &&
2987                     (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) ||
2988                     XVA_ISSET_REQ(xvap, XAT_OPAQUE))) {
2989                         need_policy = TRUE;
2990                 }
2991         }
2992 
2993         mutex_exit(&zp->z_lock);
2994 
2995         if (mask & AT_MODE) {
2996                 if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) {
2997                         err = secpolicy_setid_setsticky_clear(vp, vap,
2998                             &oldva, cr);
2999                         if (err) {
3000                                 ZFS_EXIT(zfsvfs);
3001                                 return (err);
3002                         }
3003                         trim_mask |= AT_MODE;
3004                 } else {
3005                         need_policy = TRUE;
3006                 }
3007         }
3008 
3009         if (need_policy) {
3010                 /*
3011                  * If trim_mask is set then take ownership
3012                  * has been granted or write_acl is present and user
3013                  * has the ability to modify mode.  In that case remove
3014                  * UID|GID and or MODE from mask so that
3015                  * secpolicy_vnode_setattr() doesn't revoke it.
3016                  */
3017 
3018                 if (trim_mask) {
3019                         saved_mask = vap->va_mask;
3020                         vap->va_mask &= ~trim_mask;
3021                 }
3022                 err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags,
3023                     (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp);
3024                 if (err) {
3025                         ZFS_EXIT(zfsvfs);
3026                         return (err);
3027                 }
3028 
3029                 if (trim_mask)
3030                         vap->va_mask |= saved_mask;
3031         }
3032 
3033         /*
3034          * secpolicy_vnode_setattr, or take ownership may have
3035          * changed va_mask
3036          */
3037         mask = vap->va_mask;
3038 
3039         if ((mask & (AT_UID | AT_GID))) {
3040                 err = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
3041                     &xattr_obj, sizeof (xattr_obj));
3042 
3043                 if (err == 0 && xattr_obj) {
3044                         err = zfs_zget(zp->z_zfsvfs, xattr_obj, &attrzp);
3045                         if (err)
3046                                 goto out2;
3047                 }
3048                 if (mask & AT_UID) {
3049                         new_uid = zfs_fuid_create(zfsvfs,
3050                             (uint64_t)vap->va_uid, cr, ZFS_OWNER, &fuidp);
3051                         if (new_uid != zp->z_uid &&
3052                             zfs_fuid_overquota(zfsvfs, B_FALSE, new_uid)) {
3053                                 if (attrzp)
3054                                         VN_RELE(ZTOV(attrzp));
3055                                 err = SET_ERROR(EDQUOT);
3056                                 goto out2;
3057                         }
3058                 }
3059 
3060                 if (mask & AT_GID) {
3061                         new_gid = zfs_fuid_create(zfsvfs, (uint64_t)vap->va_gid,
3062                             cr, ZFS_GROUP, &fuidp);
3063                         if (new_gid != zp->z_gid &&
3064                             zfs_fuid_overquota(zfsvfs, B_TRUE, new_gid)) {
3065                                 if (attrzp)
3066                                         VN_RELE(ZTOV(attrzp));
3067                                 err = SET_ERROR(EDQUOT);
3068                                 goto out2;
3069                         }
3070                 }
3071         }
3072         tx = dmu_tx_create(zfsvfs->z_os);
3073 
3074         if (mask & AT_MODE) {
3075                 uint64_t pmode = zp->z_mode;
3076                 uint64_t acl_obj;
3077                 new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT);
3078 
3079                 if (zp->z_zfsvfs->z_acl_mode == ZFS_ACL_RESTRICTED &&
3080                     !(zp->z_pflags & ZFS_ACL_TRIVIAL)) {
3081                         err = SET_ERROR(EPERM);
3082                         goto out;
3083                 }
3084 
3085                 if (err = zfs_acl_chmod_setattr(zp, &aclp, new_mode))
3086                         goto out;
3087 
3088                 mutex_enter(&zp->z_lock);
3089                 if (!zp->z_is_sa && ((acl_obj = zfs_external_acl(zp)) != 0)) {
3090                         /*
3091                          * Are we upgrading ACL from old V0 format
3092                          * to V1 format?
3093                          */
3094                         if (zfsvfs->z_version >= ZPL_VERSION_FUID &&
3095                             zfs_znode_acl_version(zp) ==
3096                             ZFS_ACL_VERSION_INITIAL) {
3097                                 dmu_tx_hold_free(tx, acl_obj, 0,
3098                                     DMU_OBJECT_END);
3099                                 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
3100                                     0, aclp->z_acl_bytes);
3101                         } else {
3102                                 dmu_tx_hold_write(tx, acl_obj, 0,
3103                                     aclp->z_acl_bytes);
3104                         }
3105                 } else if (!zp->z_is_sa && aclp->z_acl_bytes > ZFS_ACE_SPACE) {
3106                         dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
3107                             0, aclp->z_acl_bytes);
3108                 }
3109                 mutex_exit(&zp->z_lock);
3110                 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
3111         } else {
3112                 if ((mask & AT_XVATTR) &&
3113                     XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
3114                         dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
3115                 else
3116                         dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
3117         }
3118 
3119         if (attrzp) {
3120                 dmu_tx_hold_sa(tx, attrzp->z_sa_hdl, B_FALSE);
3121         }
3122 
3123         fuid_dirtied = zfsvfs->z_fuid_dirty;
3124         if (fuid_dirtied)
3125                 zfs_fuid_txhold(zfsvfs, tx);
3126 
3127         zfs_sa_upgrade_txholds(tx, zp);
3128 
3129         err = dmu_tx_assign(tx, TXG_WAIT);
3130         if (err)
3131                 goto out;
3132 
3133         count = 0;
3134         /*
3135          * Set each attribute requested.
3136          * We group settings according to the locks they need to acquire.
3137          *
3138          * Note: you cannot set ctime directly, although it will be
3139          * updated as a side-effect of calling this function.
3140          */
3141 
3142 
3143         if (mask & (AT_UID|AT_GID|AT_MODE))
3144                 mutex_enter(&zp->z_acl_lock);
3145         mutex_enter(&zp->z_lock);
3146 
3147         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
3148             &zp->z_pflags, sizeof (zp->z_pflags));
3149 
3150         if (attrzp) {
3151                 if (mask & (AT_UID|AT_GID|AT_MODE))
3152                         mutex_enter(&attrzp->z_acl_lock);
3153                 mutex_enter(&attrzp->z_lock);
3154                 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3155                     SA_ZPL_FLAGS(zfsvfs), NULL, &attrzp->z_pflags,
3156                     sizeof (attrzp->z_pflags));
3157         }
3158 
3159         if (mask & (AT_UID|AT_GID)) {
3160 
3161                 if (mask & AT_UID) {
3162                         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
3163                             &new_uid, sizeof (new_uid));
3164                         zp->z_uid = new_uid;
3165                         if (attrzp) {
3166                                 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3167                                     SA_ZPL_UID(zfsvfs), NULL, &new_uid,
3168                                     sizeof (new_uid));
3169                                 attrzp->z_uid = new_uid;
3170                         }
3171                 }
3172 
3173                 if (mask & AT_GID) {
3174                         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs),
3175                             NULL, &new_gid, sizeof (new_gid));
3176                         zp->z_gid = new_gid;
3177                         if (attrzp) {
3178                                 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3179                                     SA_ZPL_GID(zfsvfs), NULL, &new_gid,
3180                                     sizeof (new_gid));
3181                                 attrzp->z_gid = new_gid;
3182                         }
3183                 }
3184                 if (!(mask & AT_MODE)) {
3185                         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs),
3186                             NULL, &new_mode, sizeof (new_mode));
3187                         new_mode = zp->z_mode;
3188                 }
3189                 err = zfs_acl_chown_setattr(zp);
3190                 ASSERT(err == 0);
3191                 if (attrzp) {
3192                         err = zfs_acl_chown_setattr(attrzp);
3193                         ASSERT(err == 0);
3194                 }
3195         }
3196 
3197         if (mask & AT_MODE) {
3198                 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL,
3199                     &new_mode, sizeof (new_mode));
3200                 zp->z_mode = new_mode;
3201                 ASSERT3U((uintptr_t)aclp, !=, NULL);
3202                 err = zfs_aclset_common(zp, aclp, cr, tx);
3203                 ASSERT0(err);
3204                 if (zp->z_acl_cached)
3205                         zfs_acl_free(zp->z_acl_cached);
3206                 zp->z_acl_cached = aclp;
3207                 aclp = NULL;
3208         }
3209 
3210 
3211         if (mask & AT_ATIME) {
3212                 ZFS_TIME_ENCODE(&vap->va_atime, zp->z_atime);
3213                 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
3214                     &zp->z_atime, sizeof (zp->z_atime));
3215         }
3216 
3217         if (mask & AT_MTIME) {
3218                 ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
3219                 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
3220                     mtime, sizeof (mtime));
3221         }
3222 
3223         /* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */
3224         if (mask & AT_SIZE && !(mask & AT_MTIME)) {
3225                 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs),
3226                     NULL, mtime, sizeof (mtime));
3227                 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
3228                     &ctime, sizeof (ctime));
3229                 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
3230                     B_TRUE);
3231         } else if (mask != 0) {
3232                 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
3233                     &ctime, sizeof (ctime));
3234                 zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime, ctime,
3235                     B_TRUE);
3236                 if (attrzp) {
3237                         SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3238                             SA_ZPL_CTIME(zfsvfs), NULL,
3239                             &ctime, sizeof (ctime));
3240                         zfs_tstamp_update_setup(attrzp, STATE_CHANGED,
3241                             mtime, ctime, B_TRUE);
3242                 }
3243         }
3244         /*
3245          * Do this after setting timestamps to prevent timestamp
3246          * update from toggling bit
3247          */
3248 
3249         if (xoap && (mask & AT_XVATTR)) {
3250 
3251                 /*
3252                  * restore trimmed off masks
3253                  * so that return masks can be set for caller.
3254                  */
3255 
3256                 if (XVA_ISSET_REQ(&tmpxvattr, XAT_APPENDONLY)) {
3257                         XVA_SET_REQ(xvap, XAT_APPENDONLY);
3258                 }
3259                 if (XVA_ISSET_REQ(&tmpxvattr, XAT_NOUNLINK)) {
3260                         XVA_SET_REQ(xvap, XAT_NOUNLINK);
3261                 }
3262                 if (XVA_ISSET_REQ(&tmpxvattr, XAT_IMMUTABLE)) {
3263                         XVA_SET_REQ(xvap, XAT_IMMUTABLE);
3264                 }
3265                 if (XVA_ISSET_REQ(&tmpxvattr, XAT_NODUMP)) {
3266                         XVA_SET_REQ(xvap, XAT_NODUMP);
3267                 }
3268                 if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_MODIFIED)) {
3269                         XVA_SET_REQ(xvap, XAT_AV_MODIFIED);
3270                 }
3271                 if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_QUARANTINED)) {
3272                         XVA_SET_REQ(xvap, XAT_AV_QUARANTINED);
3273                 }
3274 
3275                 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
3276                         ASSERT(vp->v_type == VREG);
3277 
3278                 zfs_xvattr_set(zp, xvap, tx);
3279         }
3280 
3281         if (fuid_dirtied)
3282                 zfs_fuid_sync(zfsvfs, tx);
3283 
3284         if (mask != 0)
3285                 zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp);
3286 
3287         mutex_exit(&zp->z_lock);
3288         if (mask & (AT_UID|AT_GID|AT_MODE))
3289                 mutex_exit(&zp->z_acl_lock);
3290 
3291         if (attrzp) {
3292                 if (mask & (AT_UID|AT_GID|AT_MODE))
3293                         mutex_exit(&attrzp->z_acl_lock);
3294                 mutex_exit(&attrzp->z_lock);
3295         }
3296 out:
3297         if (err == 0 && attrzp) {
3298                 err2 = sa_bulk_update(attrzp->z_sa_hdl, xattr_bulk,
3299                     xattr_count, tx);
3300                 ASSERT(err2 == 0);
3301         }
3302 
3303         if (attrzp)
3304                 VN_RELE(ZTOV(attrzp));
3305 
3306         if (aclp)
3307                 zfs_acl_free(aclp);
3308 
3309         if (fuidp) {
3310                 zfs_fuid_info_free(fuidp);
3311                 fuidp = NULL;
3312         }
3313 
3314         if (err) {
3315                 dmu_tx_abort(tx);
3316                 if (err == ERESTART)
3317                         goto top;
3318         } else {
3319                 err2 = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
3320                 dmu_tx_commit(tx);
3321         }
3322 
3323 out2:
3324         if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3325                 zil_commit(zilog, 0);
3326 
3327         ZFS_EXIT(zfsvfs);
3328         return (err);
3329 }
3330 
3331 typedef struct zfs_zlock {
3332         krwlock_t       *zl_rwlock;     /* lock we acquired */
3333         znode_t         *zl_znode;      /* znode we held */
3334         struct zfs_zlock *zl_next;      /* next in list */
3335 } zfs_zlock_t;
3336 
3337 /*
3338  * Drop locks and release vnodes that were held by zfs_rename_lock().
3339  */
3340 static void
3341 zfs_rename_unlock(zfs_zlock_t **zlpp)
3342 {
3343         zfs_zlock_t *zl;
3344 
3345         while ((zl = *zlpp) != NULL) {
3346                 if (zl->zl_znode != NULL)
3347                         VN_RELE(ZTOV(zl->zl_znode));
3348                 rw_exit(zl->zl_rwlock);
3349                 *zlpp = zl->zl_next;
3350                 kmem_free(zl, sizeof (*zl));
3351         }
3352 }
3353 
3354 /*
3355  * Search back through the directory tree, using the ".." entries.
3356  * Lock each directory in the chain to prevent concurrent renames.
3357  * Fail any attempt to move a directory into one of its own descendants.
3358  * XXX - z_parent_lock can overlap with map or grow locks
3359  */
3360 static int
3361 zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp)
3362 {
3363         zfs_zlock_t     *zl;
3364         znode_t         *zp = tdzp;
3365         uint64_t        rootid = zp->z_zfsvfs->z_root;
3366         uint64_t        oidp = zp->z_id;
3367         krwlock_t       *rwlp = &szp->z_parent_lock;
3368         krw_t           rw = RW_WRITER;
3369 
3370         /*
3371          * First pass write-locks szp and compares to zp->z_id.
3372          * Later passes read-lock zp and compare to zp->z_parent.
3373          */
3374         do {
3375                 if (!rw_tryenter(rwlp, rw)) {
3376                         /*
3377                          * Another thread is renaming in this path.
3378                          * Note that if we are a WRITER, we don't have any
3379                          * parent_locks held yet.
3380                          */
3381                         if (rw == RW_READER && zp->z_id > szp->z_id) {
3382                                 /*
3383                                  * Drop our locks and restart
3384                                  */
3385                                 zfs_rename_unlock(&zl);
3386                                 *zlpp = NULL;
3387                                 zp = tdzp;
3388                                 oidp = zp->z_id;
3389                                 rwlp = &szp->z_parent_lock;
3390                                 rw = RW_WRITER;
3391                                 continue;
3392                         } else {
3393                                 /*
3394                                  * Wait for other thread to drop its locks
3395                                  */
3396                                 rw_enter(rwlp, rw);
3397                         }
3398                 }
3399 
3400                 zl = kmem_alloc(sizeof (*zl), KM_SLEEP);
3401                 zl->zl_rwlock = rwlp;
3402                 zl->zl_znode = NULL;
3403                 zl->zl_next = *zlpp;
3404                 *zlpp = zl;
3405 
3406                 if (oidp == szp->z_id)               /* We're a descendant of szp */
3407                         return (SET_ERROR(EINVAL));
3408 
3409                 if (oidp == rootid)             /* We've hit the top */
3410                         return (0);
3411 
3412                 if (rw == RW_READER) {          /* i.e. not the first pass */
3413                         int error = zfs_zget(zp->z_zfsvfs, oidp, &zp);
3414                         if (error)
3415                                 return (error);
3416                         zl->zl_znode = zp;
3417                 }
3418                 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zp->z_zfsvfs),
3419                     &oidp, sizeof (oidp));
3420                 rwlp = &zp->z_parent_lock;
3421                 rw = RW_READER;
3422 
3423         } while (zp->z_id != sdzp->z_id);
3424 
3425         return (0);
3426 }
3427 
3428 /*
3429  * Move an entry from the provided source directory to the target
3430  * directory.  Change the entry name as indicated.
3431  *
3432  *      IN:     sdvp    - Source directory containing the "old entry".
3433  *              snm     - Old entry name.
3434  *              tdvp    - Target directory to contain the "new entry".
3435  *              tnm     - New entry name.
3436  *              cr      - credentials of caller.
3437  *              ct      - caller context
3438  *              flags   - case flags
3439  *
3440  *      RETURN: 0 on success, error code on failure.
3441  *
3442  * Timestamps:
3443  *      sdvp,tdvp - ctime|mtime updated
3444  */
3445 /*ARGSUSED*/
3446 static int
3447 zfs_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm, cred_t *cr,
3448     caller_context_t *ct, int flags)
3449 {
3450         znode_t         *tdzp, *szp, *tzp;
3451         znode_t         *sdzp = VTOZ(sdvp);
3452         zfsvfs_t        *zfsvfs = sdzp->z_zfsvfs;
3453         zilog_t         *zilog;
3454         vnode_t         *realvp;
3455         zfs_dirlock_t   *sdl, *tdl;
3456         dmu_tx_t        *tx;
3457         zfs_zlock_t     *zl;
3458         int             cmp, serr, terr;
3459         int             error = 0, rm_err = 0;
3460         int             zflg = 0;
3461         boolean_t       waited = B_FALSE;
3462 
3463         ZFS_ENTER(zfsvfs);
3464         ZFS_VERIFY_ZP(sdzp);
3465         zilog = zfsvfs->z_log;
3466 
3467         /*
3468          * Make sure we have the real vp for the target directory.
3469          */
3470         if (VOP_REALVP(tdvp, &realvp, ct) == 0)
3471                 tdvp = realvp;
3472 
3473         tdzp = VTOZ(tdvp);
3474         ZFS_VERIFY_ZP(tdzp);
3475 
3476         /*
3477          * We check z_zfsvfs rather than v_vfsp here, because snapshots and the
3478          * ctldir appear to have the same v_vfsp.
3479          */
3480         if (tdzp->z_zfsvfs != zfsvfs || zfsctl_is_node(tdvp)) {
3481                 ZFS_EXIT(zfsvfs);
3482                 return (SET_ERROR(EXDEV));
3483         }
3484 
3485         if (zfsvfs->z_utf8 && u8_validate(tnm,
3486             strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3487                 ZFS_EXIT(zfsvfs);
3488                 return (SET_ERROR(EILSEQ));
3489         }
3490 
3491         if (flags & FIGNORECASE)
3492                 zflg |= ZCILOOK;
3493 
3494 top:
3495         szp = NULL;
3496         tzp = NULL;
3497         zl = NULL;
3498 
3499         /*
3500          * This is to prevent the creation of links into attribute space
3501          * by renaming a linked file into/outof an attribute directory.
3502          * See the comment in zfs_link() for why this is considered bad.
3503          */
3504         if ((tdzp->z_pflags & ZFS_XATTR) != (sdzp->z_pflags & ZFS_XATTR)) {
3505                 ZFS_EXIT(zfsvfs);
3506                 return (SET_ERROR(EINVAL));
3507         }
3508 
3509         /*
3510          * Lock source and target directory entries.  To prevent deadlock,
3511          * a lock ordering must be defined.  We lock the directory with
3512          * the smallest object id first, or if it's a tie, the one with
3513          * the lexically first name.
3514          */
3515         if (sdzp->z_id < tdzp->z_id) {
3516                 cmp = -1;
3517         } else if (sdzp->z_id > tdzp->z_id) {
3518                 cmp = 1;
3519         } else {
3520                 /*
3521                  * First compare the two name arguments without
3522                  * considering any case folding.
3523                  */
3524                 int nofold = (zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER);
3525 
3526                 cmp = u8_strcmp(snm, tnm, 0, nofold, U8_UNICODE_LATEST, &error);
3527                 ASSERT(error == 0 || !zfsvfs->z_utf8);
3528                 if (cmp == 0) {
3529                         /*
3530                          * POSIX: "If the old argument and the new argument
3531                          * both refer to links to the same existing file,
3532                          * the rename() function shall return successfully
3533                          * and perform no other action."
3534                          */
3535                         ZFS_EXIT(zfsvfs);
3536                         return (0);
3537                 }
3538                 /*
3539                  * If the file system is case-folding, then we may
3540                  * have some more checking to do.  A case-folding file
3541                  * system is either supporting mixed case sensitivity
3542                  * access or is completely case-insensitive.  Note
3543                  * that the file system is always case preserving.
3544                  *
3545                  * In mixed sensitivity mode case sensitive behavior
3546                  * is the default.  FIGNORECASE must be used to
3547                  * explicitly request case insensitive behavior.
3548                  *
3549                  * If the source and target names provided differ only
3550                  * by case (e.g., a request to rename 'tim' to 'Tim'),
3551                  * we will treat this as a special case in the
3552                  * case-insensitive mode: as long as the source name
3553                  * is an exact match, we will allow this to proceed as
3554                  * a name-change request.
3555                  */
3556                 if ((zfsvfs->z_case == ZFS_CASE_INSENSITIVE ||
3557                     (zfsvfs->z_case == ZFS_CASE_MIXED &&
3558                     flags & FIGNORECASE)) &&
3559                     u8_strcmp(snm, tnm, 0, zfsvfs->z_norm, U8_UNICODE_LATEST,
3560                     &error) == 0) {
3561                         /*
3562                          * case preserving rename request, require exact
3563                          * name matches
3564                          */
3565                         zflg |= ZCIEXACT;
3566                         zflg &= ~ZCILOOK;
3567                 }
3568         }
3569 
3570         /*
3571          * If the source and destination directories are the same, we should
3572          * grab the z_name_lock of that directory only once.
3573          */
3574         if (sdzp == tdzp) {
3575                 zflg |= ZHAVELOCK;
3576                 rw_enter(&sdzp->z_name_lock, RW_READER);
3577         }
3578 
3579         if (cmp < 0) {
3580                 serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp,
3581                     ZEXISTS | zflg, NULL, NULL);
3582                 terr = zfs_dirent_lock(&tdl,
3583                     tdzp, tnm, &tzp, ZRENAMING | zflg, NULL, NULL);
3584         } else {
3585                 terr = zfs_dirent_lock(&tdl,
3586                     tdzp, tnm, &tzp, zflg, NULL, NULL);
3587                 serr = zfs_dirent_lock(&sdl,
3588                     sdzp, snm, &szp, ZEXISTS | ZRENAMING | zflg,
3589                     NULL, NULL);
3590         }
3591 
3592         if (serr) {
3593                 /*
3594                  * Source entry invalid or not there.
3595                  */
3596                 if (!terr) {
3597                         zfs_dirent_unlock(tdl);
3598                         if (tzp)
3599                                 VN_RELE(ZTOV(tzp));
3600                 }
3601 
3602                 if (sdzp == tdzp)
3603                         rw_exit(&sdzp->z_name_lock);
3604 
3605                 if (strcmp(snm, "..") == 0)
3606                         serr = SET_ERROR(EINVAL);
3607                 ZFS_EXIT(zfsvfs);
3608                 return (serr);
3609         }
3610         if (terr) {
3611                 zfs_dirent_unlock(sdl);
3612                 VN_RELE(ZTOV(szp));
3613 
3614                 if (sdzp == tdzp)
3615                         rw_exit(&sdzp->z_name_lock);
3616 
3617                 if (strcmp(tnm, "..") == 0)
3618                         terr = SET_ERROR(EINVAL);
3619                 ZFS_EXIT(zfsvfs);
3620                 return (terr);
3621         }
3622 
3623         /*
3624          * Must have write access at the source to remove the old entry
3625          * and write access at the target to create the new entry.
3626          * Note that if target and source are the same, this can be
3627          * done in a single check.
3628          */
3629 
3630         if (error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr))
3631                 goto out;
3632 
3633         if (ZTOV(szp)->v_type == VDIR) {
3634                 /*
3635                  * Check to make sure rename is valid.
3636                  * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
3637                  */
3638                 if (error = zfs_rename_lock(szp, tdzp, sdzp, &zl))
3639                         goto out;
3640         }
3641 
3642         /*
3643          * Does target exist?
3644          */
3645         if (tzp) {
3646                 /*
3647                  * Source and target must be the same type.
3648                  */
3649                 if (ZTOV(szp)->v_type == VDIR) {
3650                         if (ZTOV(tzp)->v_type != VDIR) {
3651                                 error = SET_ERROR(ENOTDIR);
3652                                 goto out;
3653                         }
3654                 } else {
3655                         if (ZTOV(tzp)->v_type == VDIR) {
3656                                 error = SET_ERROR(EISDIR);
3657                                 goto out;
3658                         }
3659                 }
3660                 /*
3661                  * POSIX dictates that when the source and target
3662                  * entries refer to the same file object, rename
3663                  * must do nothing and exit without error.
3664                  */
3665                 if (szp->z_id == tzp->z_id) {
3666                         error = 0;
3667                         goto out;
3668                 }
3669         }
3670 
3671         vnevent_pre_rename_src(ZTOV(szp), sdvp, snm, ct);
3672         if (tzp)
3673                 vnevent_pre_rename_dest(ZTOV(tzp), tdvp, tnm, ct);
3674 
3675         /*
3676          * notify the target directory if it is not the same
3677          * as source directory.
3678          */
3679         if (tdvp != sdvp) {
3680                 vnevent_pre_rename_dest_dir(tdvp, ZTOV(szp), tnm, ct);
3681         }
3682 
3683         tx = dmu_tx_create(zfsvfs->z_os);
3684         dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
3685         dmu_tx_hold_sa(tx, sdzp->z_sa_hdl, B_FALSE);
3686         dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm);
3687         dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm);
3688         if (sdzp != tdzp) {
3689                 dmu_tx_hold_sa(tx, tdzp->z_sa_hdl, B_FALSE);
3690                 zfs_sa_upgrade_txholds(tx, tdzp);
3691         }
3692         if (tzp) {
3693                 dmu_tx_hold_sa(tx, tzp->z_sa_hdl, B_FALSE);
3694                 zfs_sa_upgrade_txholds(tx, tzp);
3695         }
3696 
3697         zfs_sa_upgrade_txholds(tx, szp);
3698         dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
3699         error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
3700         if (error) {
3701                 if (zl != NULL)
3702                         zfs_rename_unlock(&zl);
3703                 zfs_dirent_unlock(sdl);
3704                 zfs_dirent_unlock(tdl);
3705 
3706                 if (sdzp == tdzp)
3707                         rw_exit(&sdzp->z_name_lock);
3708 
3709                 VN_RELE(ZTOV(szp));
3710                 if (tzp)
3711                         VN_RELE(ZTOV(tzp));
3712                 if (error == ERESTART) {
3713                         waited = B_TRUE;
3714                         dmu_tx_wait(tx);
3715                         dmu_tx_abort(tx);
3716                         goto top;
3717                 }
3718                 dmu_tx_abort(tx);
3719                 ZFS_EXIT(zfsvfs);
3720                 return (error);
3721         }
3722 
3723         if (tzp)        /* Attempt to remove the existing target */
3724                 error = rm_err = zfs_link_destroy(tdl, tzp, tx, zflg, NULL);
3725 
3726         if (error == 0) {
3727                 error = zfs_link_create(tdl, szp, tx, ZRENAMING);
3728                 if (error == 0) {
3729                         szp->z_pflags |= ZFS_AV_MODIFIED;
3730 
3731                         error = sa_update(szp->z_sa_hdl, SA_ZPL_FLAGS(zfsvfs),
3732                             (void *)&szp->z_pflags, sizeof (uint64_t), tx);
3733                         ASSERT0(error);
3734 
3735                         error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL);
3736                         if (error == 0) {
3737                                 zfs_log_rename(zilog, tx, TX_RENAME |
3738                                     (flags & FIGNORECASE ? TX_CI : 0), sdzp,
3739                                     sdl->dl_name, tdzp, tdl->dl_name, szp);
3740 
3741                                 /*
3742                                  * Update path information for the target vnode
3743                                  */
3744                                 vn_renamepath(tdvp, ZTOV(szp), tnm,
3745                                     strlen(tnm));
3746                         } else {
3747                                 /*
3748                                  * At this point, we have successfully created
3749                                  * the target name, but have failed to remove
3750                                  * the source name.  Since the create was done
3751                                  * with the ZRENAMING flag, there are
3752                                  * complications; for one, the link count is
3753                                  * wrong.  The easiest way to deal with this
3754                                  * is to remove the newly created target, and
3755                                  * return the original error.  This must
3756                                  * succeed; fortunately, it is very unlikely to
3757                                  * fail, since we just created it.
3758                                  */
3759                                 VERIFY3U(zfs_link_destroy(tdl, szp, tx,
3760                                     ZRENAMING, NULL), ==, 0);
3761                         }
3762                 }
3763         }
3764 
3765         dmu_tx_commit(tx);
3766 
3767         if (tzp && rm_err == 0)
3768                 vnevent_rename_dest(ZTOV(tzp), tdvp, tnm, ct);
3769 
3770         if (error == 0) {
3771                 vnevent_rename_src(ZTOV(szp), sdvp, snm, ct);
3772                 vnevent_rename_dest_dir(tdvp, ZTOV(szp), tnm, ct);
3773         }
3774 out:
3775         if (zl != NULL)
3776                 zfs_rename_unlock(&zl);
3777 
3778         zfs_dirent_unlock(sdl);
3779         zfs_dirent_unlock(tdl);
3780 
3781         if (sdzp == tdzp)
3782                 rw_exit(&sdzp->z_name_lock);
3783 
3784 
3785         VN_RELE(ZTOV(szp));
3786         if (tzp)
3787                 VN_RELE(ZTOV(tzp));
3788 
3789         if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3790                 zil_commit(zilog, 0);
3791 
3792         ZFS_EXIT(zfsvfs);
3793         return (error);
3794 }
3795 
3796 /*
3797  * Insert the indicated symbolic reference entry into the directory.
3798  *
3799  *      IN:     dvp     - Directory to contain new symbolic link.
3800  *              link    - Name for new symlink entry.
3801  *              vap     - Attributes of new entry.
3802  *              cr      - credentials of caller.
3803  *              ct      - caller context
3804  *              flags   - case flags
3805  *
3806  *      RETURN: 0 on success, error code on failure.
3807  *
3808  * Timestamps:
3809  *      dvp - ctime|mtime updated
3810  */
3811 /*ARGSUSED*/
3812 static int
3813 zfs_symlink(vnode_t *dvp, char *name, vattr_t *vap, char *link, cred_t *cr,
3814     caller_context_t *ct, int flags)
3815 {
3816         znode_t         *zp, *dzp = VTOZ(dvp);
3817         zfs_dirlock_t   *dl;
3818         dmu_tx_t        *tx;
3819         zfsvfs_t        *zfsvfs = dzp->z_zfsvfs;
3820         zilog_t         *zilog;
3821         uint64_t        len = strlen(link);
3822         int             error;
3823         int             zflg = ZNEW;
3824         zfs_acl_ids_t   acl_ids;
3825         boolean_t       fuid_dirtied;
3826         uint64_t        txtype = TX_SYMLINK;
3827         boolean_t       waited = B_FALSE;
3828 
3829         ASSERT(vap->va_type == VLNK);
3830 
3831         ZFS_ENTER(zfsvfs);
3832         ZFS_VERIFY_ZP(dzp);
3833         zilog = zfsvfs->z_log;
3834 
3835         if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
3836             NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3837                 ZFS_EXIT(zfsvfs);
3838                 return (SET_ERROR(EILSEQ));
3839         }
3840         if (flags & FIGNORECASE)
3841                 zflg |= ZCILOOK;
3842 
3843         if (len > MAXPATHLEN) {
3844                 ZFS_EXIT(zfsvfs);
3845                 return (SET_ERROR(ENAMETOOLONG));
3846         }
3847 
3848         if ((error = zfs_acl_ids_create(dzp, 0,
3849             vap, cr, NULL, &acl_ids)) != 0) {
3850                 ZFS_EXIT(zfsvfs);
3851                 return (error);
3852         }
3853 top:
3854         /*
3855          * Attempt to lock directory; fail if entry already exists.
3856          */
3857         error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, NULL, NULL);
3858         if (error) {
3859                 zfs_acl_ids_free(&acl_ids);
3860                 ZFS_EXIT(zfsvfs);
3861                 return (error);
3862         }
3863 
3864         if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
3865                 zfs_acl_ids_free(&acl_ids);
3866                 zfs_dirent_unlock(dl);
3867                 ZFS_EXIT(zfsvfs);
3868                 return (error);
3869         }
3870 
3871         if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
3872                 zfs_acl_ids_free(&acl_ids);
3873                 zfs_dirent_unlock(dl);
3874                 ZFS_EXIT(zfsvfs);
3875                 return (SET_ERROR(EDQUOT));
3876         }
3877         tx = dmu_tx_create(zfsvfs->z_os);
3878         fuid_dirtied = zfsvfs->z_fuid_dirty;
3879         dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len));
3880         dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
3881         dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
3882             ZFS_SA_BASE_ATTR_SIZE + len);
3883         dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
3884         if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
3885                 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
3886                     acl_ids.z_aclp->z_acl_bytes);
3887         }
3888         if (fuid_dirtied)
3889                 zfs_fuid_txhold(zfsvfs, tx);
3890         error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
3891         if (error) {
3892                 zfs_dirent_unlock(dl);
3893                 if (error == ERESTART) {
3894                         waited = B_TRUE;
3895                         dmu_tx_wait(tx);
3896                         dmu_tx_abort(tx);
3897                         goto top;
3898                 }
3899                 zfs_acl_ids_free(&acl_ids);
3900                 dmu_tx_abort(tx);
3901                 ZFS_EXIT(zfsvfs);
3902                 return (error);
3903         }
3904 
3905         /*
3906          * Create a new object for the symlink.
3907          * for version 4 ZPL datsets the symlink will be an SA attribute
3908          */
3909         zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
3910 
3911         if (fuid_dirtied)
3912                 zfs_fuid_sync(zfsvfs, tx);
3913 
3914         mutex_enter(&zp->z_lock);
3915         if (zp->z_is_sa)
3916                 error = sa_update(zp->z_sa_hdl, SA_ZPL_SYMLINK(zfsvfs),
3917                     link, len, tx);
3918         else
3919                 zfs_sa_symlink(zp, link, len, tx);
3920         mutex_exit(&zp->z_lock);
3921 
3922         zp->z_size = len;
3923         (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
3924             &zp->z_size, sizeof (zp->z_size), tx);
3925         /*
3926          * Insert the new object into the directory.
3927          */
3928         (void) zfs_link_create(dl, zp, tx, ZNEW);
3929 
3930         if (flags & FIGNORECASE)
3931                 txtype |= TX_CI;
3932         zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link);
3933 
3934         zfs_acl_ids_free(&acl_ids);
3935 
3936         dmu_tx_commit(tx);
3937 
3938         zfs_dirent_unlock(dl);
3939 
3940         VN_RELE(ZTOV(zp));
3941 
3942         if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3943                 zil_commit(zilog, 0);
3944 
3945         ZFS_EXIT(zfsvfs);
3946         return (error);
3947 }
3948 
3949 /*
3950  * Return, in the buffer contained in the provided uio structure,
3951  * the symbolic path referred to by vp.
3952  *
3953  *      IN:     vp      - vnode of symbolic link.
3954  *              uio     - structure to contain the link path.
3955  *              cr      - credentials of caller.
3956  *              ct      - caller context
3957  *
3958  *      OUT:    uio     - structure containing the link path.
3959  *
3960  *      RETURN: 0 on success, error code on failure.
3961  *
3962  * Timestamps:
3963  *      vp - atime updated
3964  */
3965 /* ARGSUSED */
3966 static int
3967 zfs_readlink(vnode_t *vp, uio_t *uio, cred_t *cr, caller_context_t *ct)
3968 {
3969         znode_t         *zp = VTOZ(vp);
3970         zfsvfs_t        *zfsvfs = zp->z_zfsvfs;
3971         int             error;
3972 
3973         ZFS_ENTER(zfsvfs);
3974         ZFS_VERIFY_ZP(zp);
3975 
3976         mutex_enter(&zp->z_lock);
3977         if (zp->z_is_sa)
3978                 error = sa_lookup_uio(zp->z_sa_hdl,
3979                     SA_ZPL_SYMLINK(zfsvfs), uio);
3980         else
3981                 error = zfs_sa_readlink(zp, uio);
3982         mutex_exit(&zp->z_lock);
3983 
3984         ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
3985 
3986         ZFS_EXIT(zfsvfs);
3987         return (error);
3988 }
3989 
3990 /*
3991  * Insert a new entry into directory tdvp referencing svp.
3992  *
3993  *      IN:     tdvp    - Directory to contain new entry.
3994  *              svp     - vnode of new entry.
3995  *              name    - name of new entry.
3996  *              cr      - credentials of caller.
3997  *              ct      - caller context
3998  *
3999  *      RETURN: 0 on success, error code on failure.
4000  *
4001  * Timestamps:
4002  *      tdvp - ctime|mtime updated
4003  *       svp - ctime updated
4004  */
4005 /* ARGSUSED */
4006 static int
4007 zfs_link(vnode_t *tdvp, vnode_t *svp, char *name, cred_t *cr,
4008     caller_context_t *ct, int flags)
4009 {
4010         znode_t         *dzp = VTOZ(tdvp);
4011         znode_t         *tzp, *szp;
4012         zfsvfs_t        *zfsvfs = dzp->z_zfsvfs;
4013         zilog_t         *zilog;
4014         zfs_dirlock_t   *dl;
4015         dmu_tx_t        *tx;
4016         vnode_t         *realvp;
4017         int             error;
4018         int             zf = ZNEW;
4019         uint64_t        parent;
4020         uid_t           owner;
4021         boolean_t       waited = B_FALSE;
4022 
4023         ASSERT(tdvp->v_type == VDIR);
4024 
4025         ZFS_ENTER(zfsvfs);
4026         ZFS_VERIFY_ZP(dzp);
4027         zilog = zfsvfs->z_log;
4028 
4029         if (VOP_REALVP(svp, &realvp, ct) == 0)
4030                 svp = realvp;
4031 
4032         /*
4033          * POSIX dictates that we return EPERM here.
4034          * Better choices include ENOTSUP or EISDIR.
4035          */
4036         if (svp->v_type == VDIR) {
4037                 ZFS_EXIT(zfsvfs);
4038                 return (SET_ERROR(EPERM));
4039         }
4040 
4041         szp = VTOZ(svp);
4042         ZFS_VERIFY_ZP(szp);
4043 
4044         /*
4045          * We check z_zfsvfs rather than v_vfsp here, because snapshots and the
4046          * ctldir appear to have the same v_vfsp.
4047          */
4048         if (szp->z_zfsvfs != zfsvfs || zfsctl_is_node(svp)) {
4049                 ZFS_EXIT(zfsvfs);
4050                 return (SET_ERROR(EXDEV));
4051         }
4052 
4053         /* Prevent links to .zfs/shares files */
4054 
4055         if ((error = sa_lookup(szp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
4056             &parent, sizeof (uint64_t))) != 0) {
4057                 ZFS_EXIT(zfsvfs);
4058                 return (error);
4059         }
4060         if (parent == zfsvfs->z_shares_dir) {
4061                 ZFS_EXIT(zfsvfs);
4062                 return (SET_ERROR(EPERM));
4063         }
4064 
4065         if (zfsvfs->z_utf8 && u8_validate(name,
4066             strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
4067                 ZFS_EXIT(zfsvfs);
4068                 return (SET_ERROR(EILSEQ));
4069         }
4070         if (flags & FIGNORECASE)
4071                 zf |= ZCILOOK;
4072 
4073         /*
4074          * We do not support links between attributes and non-attributes
4075          * because of the potential security risk of creating links
4076          * into "normal" file space in order to circumvent restrictions
4077          * imposed in attribute space.
4078          */
4079         if ((szp->z_pflags & ZFS_XATTR) != (dzp->z_pflags & ZFS_XATTR)) {
4080                 ZFS_EXIT(zfsvfs);
4081                 return (SET_ERROR(EINVAL));
4082         }
4083 
4084 
4085         owner = zfs_fuid_map_id(zfsvfs, szp->z_uid, cr, ZFS_OWNER);
4086         if (owner != crgetuid(cr) && secpolicy_basic_link(cr) != 0) {
4087                 ZFS_EXIT(zfsvfs);
4088                 return (SET_ERROR(EPERM));
4089         }
4090 
4091         if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
4092                 ZFS_EXIT(zfsvfs);
4093                 return (error);
4094         }
4095 
4096 top:
4097         /*
4098          * Attempt to lock directory; fail if entry already exists.
4099          */
4100         error = zfs_dirent_lock(&dl, dzp, name, &tzp, zf, NULL, NULL);
4101         if (error) {
4102                 ZFS_EXIT(zfsvfs);
4103                 return (error);
4104         }
4105 
4106         tx = dmu_tx_create(zfsvfs->z_os);
4107         dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
4108         dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
4109         zfs_sa_upgrade_txholds(tx, szp);
4110         zfs_sa_upgrade_txholds(tx, dzp);
4111         error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
4112         if (error) {
4113                 zfs_dirent_unlock(dl);
4114                 if (error == ERESTART) {
4115                         waited = B_TRUE;
4116                         dmu_tx_wait(tx);
4117                         dmu_tx_abort(tx);
4118                         goto top;
4119                 }
4120                 dmu_tx_abort(tx);
4121                 ZFS_EXIT(zfsvfs);
4122                 return (error);
4123         }
4124 
4125         error = zfs_link_create(dl, szp, tx, 0);
4126 
4127         if (error == 0) {
4128                 uint64_t txtype = TX_LINK;
4129                 if (flags & FIGNORECASE)
4130                         txtype |= TX_CI;
4131                 zfs_log_link(zilog, tx, txtype, dzp, szp, name);
4132         }
4133 
4134         dmu_tx_commit(tx);
4135 
4136         zfs_dirent_unlock(dl);
4137 
4138         if (error == 0) {
4139                 vnevent_link(svp, ct);
4140         }
4141 
4142         if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4143                 zil_commit(zilog, 0);
4144 
4145         ZFS_EXIT(zfsvfs);
4146         return (error);
4147 }
4148 
4149 /*
4150  * zfs_null_putapage() is used when the file system has been force
4151  * unmounted. It just drops the pages.
4152  */
4153 /* ARGSUSED */
4154 static int
4155 zfs_null_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
4156     size_t *lenp, int flags, cred_t *cr)
4157 {
4158         pvn_write_done(pp, B_INVAL|B_FORCE|B_ERROR);
4159         return (0);
4160 }
4161 
4162 /*
4163  * Push a page out to disk, klustering if possible.
4164  *
4165  *      IN:     vp      - file to push page to.
4166  *              pp      - page to push.
4167  *              flags   - additional flags.
4168  *              cr      - credentials of caller.
4169  *
4170  *      OUT:    offp    - start of range pushed.
4171  *              lenp    - len of range pushed.
4172  *
4173  *      RETURN: 0 on success, error code on failure.
4174  *
4175  * NOTE: callers must have locked the page to be pushed.  On
4176  * exit, the page (and all other pages in the kluster) must be
4177  * unlocked.
4178  */
4179 /* ARGSUSED */
4180 static int
4181 zfs_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
4182     size_t *lenp, int flags, cred_t *cr)
4183 {
4184         znode_t         *zp = VTOZ(vp);
4185         zfsvfs_t        *zfsvfs = zp->z_zfsvfs;
4186         dmu_tx_t        *tx;
4187         u_offset_t      off, koff;
4188         size_t          len, klen;
4189         int             err;
4190 
4191         off = pp->p_offset;
4192         len = PAGESIZE;
4193         /*
4194          * If our blocksize is bigger than the page size, try to kluster
4195          * multiple pages so that we write a full block (thus avoiding
4196          * a read-modify-write).
4197          */
4198         if (off < zp->z_size && zp->z_blksz > PAGESIZE) {
4199                 klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE);
4200                 koff = ISP2(klen) ? P2ALIGN(off, (u_offset_t)klen) : 0;
4201                 ASSERT(koff <= zp->z_size);
4202                 if (koff + klen > zp->z_size)
4203                         klen = P2ROUNDUP(zp->z_size - koff, (uint64_t)PAGESIZE);
4204                 pp = pvn_write_kluster(vp, pp, &off, &len, koff, klen, flags);
4205         }
4206         ASSERT3U(btop(len), ==, btopr(len));
4207 
4208         /*
4209          * Can't push pages past end-of-file.
4210          */
4211         if (off >= zp->z_size) {
4212                 /* ignore all pages */
4213                 err = 0;
4214                 goto out;
4215         } else if (off + len > zp->z_size) {
4216                 int npages = btopr(zp->z_size - off);
4217                 page_t *trunc;
4218 
4219                 page_list_break(&pp, &trunc, npages);
4220                 /* ignore pages past end of file */
4221                 if (trunc)
4222                         pvn_write_done(trunc, flags);
4223                 len = zp->z_size - off;
4224         }
4225 
4226         if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) ||
4227             zfs_owner_overquota(zfsvfs, zp, B_TRUE)) {
4228                 err = SET_ERROR(EDQUOT);
4229                 goto out;
4230         }
4231         tx = dmu_tx_create(zfsvfs->z_os);
4232         dmu_tx_hold_write(tx, zp->z_id, off, len);
4233 
4234         dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
4235         zfs_sa_upgrade_txholds(tx, zp);
4236         err = dmu_tx_assign(tx, TXG_WAIT);
4237         if (err != 0) {
4238                 dmu_tx_abort(tx);
4239                 goto out;
4240         }
4241 
4242         if (zp->z_blksz <= PAGESIZE) {
4243                 caddr_t va = zfs_map_page(pp, S_READ);
4244                 ASSERT3U(len, <=, PAGESIZE);
4245                 dmu_write(zfsvfs->z_os, zp->z_id, off, len, va, tx);
4246                 zfs_unmap_page(pp, va);
4247         } else {
4248                 err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, pp, tx);
4249         }
4250 
4251         if (err == 0) {
4252                 uint64_t mtime[2], ctime[2];
4253                 sa_bulk_attr_t bulk[3];
4254                 int count = 0;
4255 
4256                 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
4257                     &mtime, 16);
4258                 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
4259                     &ctime, 16);
4260                 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
4261                     &zp->z_pflags, 8);
4262                 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
4263                     B_TRUE);
4264                 zfs_log_write(zfsvfs->z_log, tx, TX_WRITE, zp, off, len, 0);
4265         }
4266         dmu_tx_commit(tx);
4267 
4268 out:
4269         pvn_write_done(pp, (err ? B_ERROR : 0) | flags);
4270         if (offp)
4271                 *offp = off;
4272         if (lenp)
4273                 *lenp = len;
4274 
4275         return (err);
4276 }
4277 
4278 /*
4279  * Copy the portion of the file indicated from pages into the file.
4280  * The pages are stored in a page list attached to the files vnode.
4281  *
4282  *      IN:     vp      - vnode of file to push page data to.
4283  *              off     - position in file to put data.
4284  *              len     - amount of data to write.
4285  *              flags   - flags to control the operation.
4286  *              cr      - credentials of caller.
4287  *              ct      - caller context.
4288  *
4289  *      RETURN: 0 on success, error code on failure.
4290  *
4291  * Timestamps:
4292  *      vp - ctime|mtime updated
4293  */
4294 /*ARGSUSED*/
4295 static int
4296 zfs_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr,
4297     caller_context_t *ct)
4298 {
4299         znode_t         *zp = VTOZ(vp);
4300         zfsvfs_t        *zfsvfs = zp->z_zfsvfs;
4301         page_t          *pp;
4302         size_t          io_len;
4303         u_offset_t      io_off;
4304         uint_t          blksz;
4305         rl_t            *rl;
4306         int             error = 0;
4307 
4308         ZFS_ENTER(zfsvfs);
4309         ZFS_VERIFY_ZP(zp);
4310 
4311         /*
4312          * There's nothing to do if no data is cached.
4313          */
4314         if (!vn_has_cached_data(vp)) {
4315                 ZFS_EXIT(zfsvfs);
4316                 return (0);
4317         }
4318 
4319         /*
4320          * Align this request to the file block size in case we kluster.
4321          * XXX - this can result in pretty aggresive locking, which can
4322          * impact simultanious read/write access.  One option might be
4323          * to break up long requests (len == 0) into block-by-block
4324          * operations to get narrower locking.
4325          */
4326         blksz = zp->z_blksz;
4327         if (ISP2(blksz))
4328                 io_off = P2ALIGN_TYPED(off, blksz, u_offset_t);
4329         else
4330                 io_off = 0;
4331         if (len > 0 && ISP2(blksz))
4332                 io_len = P2ROUNDUP_TYPED(len + (off - io_off), blksz, size_t);
4333         else
4334                 io_len = 0;
4335 
4336         if (io_len == 0) {
4337                 /*
4338                  * Search the entire vp list for pages >= io_off.
4339                  */
4340                 rl = zfs_range_lock(zp, io_off, UINT64_MAX, RL_WRITER);
4341                 error = pvn_vplist_dirty(vp, io_off, zfs_putapage, flags, cr);
4342                 goto out;
4343         }
4344         rl = zfs_range_lock(zp, io_off, io_len, RL_WRITER);
4345 
4346         if (off > zp->z_size) {
4347                 /* past end of file */
4348                 zfs_range_unlock(rl);
4349                 ZFS_EXIT(zfsvfs);
4350                 return (0);
4351         }
4352 
4353         len = MIN(io_len, P2ROUNDUP(zp->z_size, PAGESIZE) - io_off);
4354 
4355         for (off = io_off; io_off < off + len; io_off += io_len) {
4356                 if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) {
4357                         pp = page_lookup(vp, io_off,
4358                             (flags & (B_INVAL | B_FREE)) ? SE_EXCL : SE_SHARED);
4359                 } else {
4360                         pp = page_lookup_nowait(vp, io_off,
4361                             (flags & B_FREE) ? SE_EXCL : SE_SHARED);
4362                 }
4363 
4364                 if (pp != NULL && pvn_getdirty(pp, flags)) {
4365                         int err;
4366 
4367                         /*
4368                          * Found a dirty page to push
4369                          */
4370                         err = zfs_putapage(vp, pp, &io_off, &io_len, flags, cr);
4371                         if (err)
4372                                 error = err;
4373                 } else {
4374                         io_len = PAGESIZE;
4375                 }
4376         }
4377 out:
4378         zfs_range_unlock(rl);
4379         if ((flags & B_ASYNC) == 0 || zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4380                 zil_commit(zfsvfs->z_log, zp->z_id);
4381         ZFS_EXIT(zfsvfs);
4382         return (error);
4383 }
4384 
4385 /*ARGSUSED*/
4386 void
4387 zfs_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
4388 {
4389         znode_t *zp = VTOZ(vp);
4390         zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4391         int error;
4392 
4393         rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER);
4394         if (zp->z_sa_hdl == NULL) {
4395                 /*
4396                  * The fs has been unmounted, or we did a
4397                  * suspend/resume and this file no longer exists.
4398                  */
4399                 if (vn_has_cached_data(vp)) {
4400                         (void) pvn_vplist_dirty(vp, 0, zfs_null_putapage,
4401                             B_INVAL, cr);
4402                 }
4403 
4404                 mutex_enter(&zp->z_lock);
4405                 mutex_enter(&vp->v_lock);
4406                 ASSERT(vp->v_count == 1);
4407                 vp->v_count = 0;
4408                 mutex_exit(&vp->v_lock);
4409                 mutex_exit(&zp->z_lock);
4410                 rw_exit(&zfsvfs->z_teardown_inactive_lock);
4411                 zfs_znode_free(zp);
4412                 return;
4413         }
4414 
4415         /*
4416          * Attempt to push any data in the page cache.  If this fails
4417          * we will get kicked out later in zfs_zinactive().
4418          */
4419         if (vn_has_cached_data(vp)) {
4420                 (void) pvn_vplist_dirty(vp, 0, zfs_putapage, B_INVAL|B_ASYNC,
4421                     cr);
4422         }
4423 
4424         if (zp->z_atime_dirty && zp->z_unlinked == 0) {
4425                 dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os);
4426 
4427                 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
4428                 zfs_sa_upgrade_txholds(tx, zp);
4429                 error = dmu_tx_assign(tx, TXG_WAIT);
4430                 if (error) {
4431                         dmu_tx_abort(tx);
4432                 } else {
4433                         mutex_enter(&zp->z_lock);
4434                         (void) sa_update(zp->z_sa_hdl, SA_ZPL_ATIME(zfsvfs),
4435                             (void *)&zp->z_atime, sizeof (zp->z_atime), tx);
4436                         zp->z_atime_dirty = 0;
4437                         mutex_exit(&zp->z_lock);
4438                         dmu_tx_commit(tx);
4439                 }
4440         }
4441 
4442         zfs_zinactive(zp);
4443         rw_exit(&zfsvfs->z_teardown_inactive_lock);
4444 }
4445 
4446 /*
4447  * Bounds-check the seek operation.
4448  *
4449  *      IN:     vp      - vnode seeking within
4450  *              ooff    - old file offset
4451  *              noffp   - pointer to new file offset
4452  *              ct      - caller context
4453  *
4454  *      RETURN: 0 on success, EINVAL if new offset invalid.
4455  */
4456 /* ARGSUSED */
4457 static int
4458 zfs_seek(vnode_t *vp, offset_t ooff, offset_t *noffp,
4459     caller_context_t *ct)
4460 {
4461         if (vp->v_type == VDIR)
4462                 return (0);
4463         return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
4464 }
4465 
4466 /*
4467  * Pre-filter the generic locking function to trap attempts to place
4468  * a mandatory lock on a memory mapped file.
4469  */
4470 static int
4471 zfs_frlock(vnode_t *vp, int cmd, flock64_t *bfp, int flag, offset_t offset,
4472     flk_callback_t *flk_cbp, cred_t *cr, caller_context_t *ct)
4473 {
4474         znode_t *zp = VTOZ(vp);
4475         zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4476 
4477         ZFS_ENTER(zfsvfs);
4478         ZFS_VERIFY_ZP(zp);
4479 
4480         /*
4481          * We are following the UFS semantics with respect to mapcnt
4482          * here: If we see that the file is mapped already, then we will
4483          * return an error, but we don't worry about races between this
4484          * function and zfs_map().
4485          */
4486         if (zp->z_mapcnt > 0 && MANDMODE(zp->z_mode)) {
4487                 ZFS_EXIT(zfsvfs);
4488                 return (SET_ERROR(EAGAIN));
4489         }
4490         ZFS_EXIT(zfsvfs);
4491         return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct));
4492 }
4493 
4494 /*
4495  * If we can't find a page in the cache, we will create a new page
4496  * and fill it with file data.  For efficiency, we may try to fill
4497  * multiple pages at once (klustering) to fill up the supplied page
4498  * list.  Note that the pages to be filled are held with an exclusive
4499  * lock to prevent access by other threads while they are being filled.
4500  */
4501 static int
4502 zfs_fillpage(vnode_t *vp, u_offset_t off, struct seg *seg,
4503     caddr_t addr, page_t *pl[], size_t plsz, enum seg_rw rw)
4504 {
4505         znode_t *zp = VTOZ(vp);
4506         page_t *pp, *cur_pp;
4507         objset_t *os = zp->z_zfsvfs->z_os;
4508         u_offset_t io_off, total;
4509         size_t io_len;
4510         int err;
4511 
4512         if (plsz == PAGESIZE || zp->z_blksz <= PAGESIZE) {
4513                 /*
4514                  * We only have a single page, don't bother klustering
4515                  */
4516                 io_off = off;
4517                 io_len = PAGESIZE;
4518                 pp = page_create_va(vp, io_off, io_len,
4519                     PG_EXCL | PG_WAIT, seg, addr);
4520         } else {
4521                 /*
4522                  * Try to find enough pages to fill the page list
4523                  */
4524                 pp = pvn_read_kluster(vp, off, seg, addr, &io_off,
4525                     &io_len, off, plsz, 0);
4526         }
4527         if (pp == NULL) {
4528                 /*
4529                  * The page already exists, nothing to do here.
4530                  */
4531                 *pl = NULL;
4532                 return (0);
4533         }
4534 
4535         /*
4536          * Fill the pages in the kluster.
4537          */
4538         cur_pp = pp;
4539         for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) {
4540                 caddr_t va;
4541 
4542                 ASSERT3U(io_off, ==, cur_pp->p_offset);
4543                 va = zfs_map_page(cur_pp, S_WRITE);
4544                 err = dmu_read(os, zp->z_id, io_off, PAGESIZE, va,
4545                     DMU_READ_PREFETCH);
4546                 zfs_unmap_page(cur_pp, va);
4547                 if (err) {
4548                         /* On error, toss the entire kluster */
4549                         pvn_read_done(pp, B_ERROR);
4550                         /* convert checksum errors into IO errors */
4551                         if (err == ECKSUM)
4552                                 err = SET_ERROR(EIO);
4553                         return (err);
4554                 }
4555                 cur_pp = cur_pp->p_next;
4556         }
4557 
4558         /*
4559          * Fill in the page list array from the kluster starting
4560          * from the desired offset `off'.
4561          * NOTE: the page list will always be null terminated.
4562          */
4563         pvn_plist_init(pp, pl, plsz, off, io_len, rw);
4564         ASSERT(pl == NULL || (*pl)->p_offset == off);
4565 
4566         return (0);
4567 }
4568 
4569 /*
4570  * Return pointers to the pages for the file region [off, off + len]
4571  * in the pl array.  If plsz is greater than len, this function may
4572  * also return page pointers from after the specified region
4573  * (i.e. the region [off, off + plsz]).  These additional pages are
4574  * only returned if they are already in the cache, or were created as
4575  * part of a klustered read.
4576  *
4577  *      IN:     vp      - vnode of file to get data from.
4578  *              off     - position in file to get data from.
4579  *              len     - amount of data to retrieve.
4580  *              plsz    - length of provided page list.
4581  *              seg     - segment to obtain pages for.
4582  *              addr    - virtual address of fault.
4583  *              rw      - mode of created pages.
4584  *              cr      - credentials of caller.
4585  *              ct      - caller context.
4586  *
4587  *      OUT:    protp   - protection mode of created pages.
4588  *              pl      - list of pages created.
4589  *
4590  *      RETURN: 0 on success, error code on failure.
4591  *
4592  * Timestamps:
4593  *      vp - atime updated
4594  */
4595 /* ARGSUSED */
4596 static int
4597 zfs_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp,
4598     page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
4599     enum seg_rw rw, cred_t *cr, caller_context_t *ct)
4600 {
4601         znode_t         *zp = VTOZ(vp);
4602         zfsvfs_t        *zfsvfs = zp->z_zfsvfs;
4603         page_t          **pl0 = pl;
4604         int             err = 0;
4605 
4606         /* we do our own caching, faultahead is unnecessary */
4607         if (pl == NULL)
4608                 return (0);
4609         else if (len > plsz)
4610                 len = plsz;
4611         else
4612                 len = P2ROUNDUP(len, PAGESIZE);
4613         ASSERT(plsz >= len);
4614 
4615         ZFS_ENTER(zfsvfs);
4616         ZFS_VERIFY_ZP(zp);
4617 
4618         if (protp)
4619                 *protp = PROT_ALL;
4620 
4621         /*
4622          * Loop through the requested range [off, off + len) looking
4623          * for pages.  If we don't find a page, we will need to create
4624          * a new page and fill it with data from the file.
4625          */
4626         while (len > 0) {
4627                 if (*pl = page_lookup(vp, off, SE_SHARED))
4628                         *(pl+1) = NULL;
4629                 else if (err = zfs_fillpage(vp, off, seg, addr, pl, plsz, rw))
4630                         goto out;
4631                 while (*pl) {
4632                         ASSERT3U((*pl)->p_offset, ==, off);
4633                         off += PAGESIZE;
4634                         addr += PAGESIZE;
4635                         if (len > 0) {
4636                                 ASSERT3U(len, >=, PAGESIZE);
4637                                 len -= PAGESIZE;
4638                         }
4639                         ASSERT3U(plsz, >=, PAGESIZE);
4640                         plsz -= PAGESIZE;
4641                         pl++;
4642                 }
4643         }
4644 
4645         /*
4646          * Fill out the page array with any pages already in the cache.
4647          */
4648         while (plsz > 0 &&
4649             (*pl++ = page_lookup_nowait(vp, off, SE_SHARED))) {
4650                         off += PAGESIZE;
4651                         plsz -= PAGESIZE;
4652         }
4653 out:
4654         if (err) {
4655                 /*
4656                  * Release any pages we have previously locked.
4657                  */
4658                 while (pl > pl0)
4659                         page_unlock(*--pl);
4660         } else {
4661                 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
4662         }
4663 
4664         *pl = NULL;
4665 
4666         ZFS_EXIT(zfsvfs);
4667         return (err);
4668 }
4669 
4670 /*
4671  * Request a memory map for a section of a file.  This code interacts
4672  * with common code and the VM system as follows:
4673  *
4674  * - common code calls mmap(), which ends up in smmap_common()
4675  * - this calls VOP_MAP(), which takes you into (say) zfs
4676  * - zfs_map() calls as_map(), passing segvn_create() as the callback
4677  * - segvn_create() creates the new segment and calls VOP_ADDMAP()
4678  * - zfs_addmap() updates z_mapcnt
4679  */
4680 /*ARGSUSED*/
4681 static int
4682 zfs_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp,
4683     size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
4684     caller_context_t *ct)
4685 {
4686         znode_t *zp = VTOZ(vp);
4687         zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4688         segvn_crargs_t  vn_a;
4689         int             error;
4690 
4691         ZFS_ENTER(zfsvfs);
4692         ZFS_VERIFY_ZP(zp);
4693 
4694         if ((prot & PROT_WRITE) && (zp->z_pflags &
4695             (ZFS_IMMUTABLE | ZFS_READONLY | ZFS_APPENDONLY))) {
4696                 ZFS_EXIT(zfsvfs);
4697                 return (SET_ERROR(EPERM));
4698         }
4699 
4700         if ((prot & (PROT_READ | PROT_EXEC)) &&
4701             (zp->z_pflags & ZFS_AV_QUARANTINED)) {
4702                 ZFS_EXIT(zfsvfs);
4703                 return (SET_ERROR(EACCES));
4704         }
4705 
4706         if (vp->v_flag & VNOMAP) {
4707                 ZFS_EXIT(zfsvfs);
4708                 return (SET_ERROR(ENOSYS));
4709         }
4710 
4711         if (off < 0 || len > MAXOFFSET_T - off) {
4712                 ZFS_EXIT(zfsvfs);
4713                 return (SET_ERROR(ENXIO));
4714         }
4715 
4716         if (vp->v_type != VREG) {
4717                 ZFS_EXIT(zfsvfs);
4718                 return (SET_ERROR(ENODEV));
4719         }
4720 
4721         /*
4722          * If file is locked, disallow mapping.
4723          */
4724         if (MANDMODE(zp->z_mode) && vn_has_flocks(vp)) {
4725                 ZFS_EXIT(zfsvfs);
4726                 return (SET_ERROR(EAGAIN));
4727         }
4728 
4729         as_rangelock(as);
4730         error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags);
4731         if (error != 0) {
4732                 as_rangeunlock(as);
4733                 ZFS_EXIT(zfsvfs);
4734                 return (error);
4735         }
4736 
4737         vn_a.vp = vp;
4738         vn_a.offset = (u_offset_t)off;
4739         vn_a.type = flags & MAP_TYPE;
4740         vn_a.prot = prot;
4741         vn_a.maxprot = maxprot;
4742         vn_a.cred = cr;
4743         vn_a.amp = NULL;
4744         vn_a.flags = flags & ~MAP_TYPE;
4745         vn_a.szc = 0;
4746         vn_a.lgrp_mem_policy_flags = 0;
4747 
4748         error = as_map(as, *addrp, len, segvn_create, &vn_a);
4749 
4750         as_rangeunlock(as);
4751         ZFS_EXIT(zfsvfs);
4752         return (error);
4753 }
4754 
4755 /* ARGSUSED */
4756 static int
4757 zfs_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
4758     size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
4759     caller_context_t *ct)
4760 {
4761         uint64_t pages = btopr(len);
4762 
4763         atomic_add_64(&VTOZ(vp)->z_mapcnt, pages);
4764         return (0);
4765 }
4766 
4767 /*
4768  * The reason we push dirty pages as part of zfs_delmap() is so that we get a
4769  * more accurate mtime for the associated file.  Since we don't have a way of
4770  * detecting when the data was actually modified, we have to resort to
4771  * heuristics.  If an explicit msync() is done, then we mark the mtime when the
4772  * last page is pushed.  The problem occurs when the msync() call is omitted,
4773  * which by far the most common case:
4774  *
4775  *      open()
4776  *      mmap()
4777  *      <modify memory>
4778  *      munmap()
4779  *      close()
4780  *      <time lapse>
4781  *      putpage() via fsflush
4782  *
4783  * If we wait until fsflush to come along, we can have a modification time that
4784  * is some arbitrary point in the future.  In order to prevent this in the
4785  * common case, we flush pages whenever a (MAP_SHARED, PROT_WRITE) mapping is
4786  * torn down.
4787  */
4788 /* ARGSUSED */
4789 static int
4790 zfs_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
4791     size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr,
4792     caller_context_t *ct)
4793 {
4794         uint64_t pages = btopr(len);
4795 
4796         ASSERT3U(VTOZ(vp)->z_mapcnt, >=, pages);
4797         atomic_add_64(&VTOZ(vp)->z_mapcnt, -pages);
4798 
4799         if ((flags & MAP_SHARED) && (prot & PROT_WRITE) &&
4800             vn_has_cached_data(vp))
4801                 (void) VOP_PUTPAGE(vp, off, len, B_ASYNC, cr, ct);
4802 
4803         return (0);
4804 }
4805 
4806 /*
4807  * Free or allocate space in a file.  Currently, this function only
4808  * supports the `F_FREESP' command.  However, this command is somewhat
4809  * misnamed, as its functionality includes the ability to allocate as
4810  * well as free space.
4811  *
4812  *      IN:     vp      - vnode of file to free data in.
4813  *              cmd     - action to take (only F_FREESP supported).
4814  *              bfp     - section of file to free/alloc.
4815  *              flag    - current file open mode flags.
4816  *              offset  - current file offset.
4817  *              cr      - credentials of caller [UNUSED].
4818  *              ct      - caller context.
4819  *
4820  *      RETURN: 0 on success, error code on failure.
4821  *
4822  * Timestamps:
4823  *      vp - ctime|mtime updated
4824  */
4825 /* ARGSUSED */
4826 static int
4827 zfs_space(vnode_t *vp, int cmd, flock64_t *bfp, int flag,
4828     offset_t offset, cred_t *cr, caller_context_t *ct)
4829 {
4830         znode_t         *zp = VTOZ(vp);
4831         zfsvfs_t        *zfsvfs = zp->z_zfsvfs;
4832         uint64_t        off, len;
4833         int             error;
4834 
4835         ZFS_ENTER(zfsvfs);
4836         ZFS_VERIFY_ZP(zp);
4837 
4838         if (cmd != F_FREESP) {
4839                 ZFS_EXIT(zfsvfs);
4840                 return (SET_ERROR(EINVAL));
4841         }
4842 
4843         /*
4844          * In a case vp->v_vfsp != zp->z_zfsvfs->z_vfs (e.g. snapshots) our
4845          * callers might not be able to detect properly that we are read-only,
4846          * so check it explicitly here.
4847          */
4848         if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
4849                 ZFS_EXIT(zfsvfs);
4850                 return (SET_ERROR(EROFS));
4851         }
4852 
4853         if (error = convoff(vp, bfp, 0, offset)) {
4854                 ZFS_EXIT(zfsvfs);
4855                 return (error);
4856         }
4857 
4858         if (bfp->l_len < 0) {
4859                 ZFS_EXIT(zfsvfs);
4860                 return (SET_ERROR(EINVAL));
4861         }
4862 
4863         off = bfp->l_start;
4864         len = bfp->l_len; /* 0 means from off to end of file */
4865 
4866         error = zfs_freesp(zp, off, len, flag, TRUE);
4867 
4868         if (error == 0 && len == 0) {
4869                 if (off == 0) {
4870                         vnevent_truncate(ZTOV(zp), ct);
4871                 } else {
4872                         vnevent_resize(ZTOV(zp), ct);
4873                 }
4874         }
4875 
4876         ZFS_EXIT(zfsvfs);
4877         return (error);
4878 }
4879 
4880 /*ARGSUSED*/
4881 static int
4882 zfs_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
4883 {
4884         znode_t         *zp = VTOZ(vp);
4885         zfsvfs_t        *zfsvfs = zp->z_zfsvfs;
4886         uint32_t        gen;
4887         uint64_t        gen64;
4888         uint64_t        object = zp->z_id;
4889         zfid_short_t    *zfid;
4890         int             size, i, error;
4891 
4892         ZFS_ENTER(zfsvfs);
4893         ZFS_VERIFY_ZP(zp);
4894 
4895         if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs),
4896             &gen64, sizeof (uint64_t))) != 0) {
4897                 ZFS_EXIT(zfsvfs);
4898                 return (error);
4899         }
4900 
4901         gen = (uint32_t)gen64;
4902 
4903         size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN;
4904         if (fidp->fid_len < size) {
4905                 fidp->fid_len = size;
4906                 ZFS_EXIT(zfsvfs);
4907                 return (SET_ERROR(ENOSPC));
4908         }
4909 
4910         zfid = (zfid_short_t *)fidp;
4911 
4912         zfid->zf_len = size;
4913 
4914         for (i = 0; i < sizeof (zfid->zf_object); i++)
4915                 zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
4916 
4917         /* Must have a non-zero generation number to distinguish from .zfs */
4918         if (gen == 0)
4919                 gen = 1;
4920         for (i = 0; i < sizeof (zfid->zf_gen); i++)
4921                 zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
4922 
4923         if (size == LONG_FID_LEN) {
4924                 uint64_t        objsetid = dmu_objset_id(zfsvfs->z_os);
4925                 zfid_long_t     *zlfid;
4926 
4927                 zlfid = (zfid_long_t *)fidp;
4928 
4929                 for (i = 0; i < sizeof (zlfid->zf_setid); i++)
4930                         zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i));
4931 
4932                 /* XXX - this should be the generation number for the objset */
4933                 for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
4934                         zlfid->zf_setgen[i] = 0;
4935         }
4936 
4937         ZFS_EXIT(zfsvfs);
4938         return (0);
4939 }
4940 
4941 static int
4942 zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
4943     caller_context_t *ct)
4944 {
4945         znode_t         *zp, *xzp;
4946         zfsvfs_t        *zfsvfs;
4947         zfs_dirlock_t   *dl;
4948         int             error;
4949 
4950         switch (cmd) {
4951         case _PC_LINK_MAX:
4952                 *valp = ULONG_MAX;
4953                 return (0);
4954 
4955         case _PC_FILESIZEBITS:
4956                 *valp = 64;
4957                 return (0);
4958 
4959         case _PC_XATTR_EXISTS:
4960                 zp = VTOZ(vp);
4961                 zfsvfs = zp->z_zfsvfs;
4962                 ZFS_ENTER(zfsvfs);
4963                 ZFS_VERIFY_ZP(zp);
4964                 *valp = 0;
4965                 error = zfs_dirent_lock(&dl, zp, "", &xzp,
4966                     ZXATTR | ZEXISTS | ZSHARED, NULL, NULL);
4967                 if (error == 0) {
4968                         zfs_dirent_unlock(dl);
4969                         if (!zfs_dirempty(xzp))
4970                                 *valp = 1;
4971                         VN_RELE(ZTOV(xzp));
4972                 } else if (error == ENOENT) {
4973                         /*
4974                          * If there aren't extended attributes, it's the
4975                          * same as having zero of them.
4976                          */
4977                         error = 0;
4978                 }
4979                 ZFS_EXIT(zfsvfs);
4980                 return (error);
4981 
4982         case _PC_SATTR_ENABLED:
4983         case _PC_SATTR_EXISTS:
4984                 *valp = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
4985                     (vp->v_type == VREG || vp->v_type == VDIR);
4986                 return (0);
4987 
4988         case _PC_ACCESS_FILTERING:
4989                 *valp = vfs_has_feature(vp->v_vfsp, VFSFT_ACCESS_FILTER) &&
4990                     vp->v_type == VDIR;
4991                 return (0);
4992 
4993         case _PC_ACL_ENABLED:
4994                 *valp = _ACL_ACE_ENABLED;
4995                 return (0);
4996 
4997         case _PC_MIN_HOLE_SIZE:
4998                 *valp = (ulong_t)SPA_MINBLOCKSIZE;
4999                 return (0);
5000 
5001         case _PC_TIMESTAMP_RESOLUTION:
5002                 /* nanosecond timestamp resolution */
5003                 *valp = 1L;
5004                 return (0);
5005 
5006         default:
5007                 return (fs_pathconf(vp, cmd, valp, cr, ct));
5008         }
5009 }
5010 
5011 /*ARGSUSED*/
5012 static int
5013 zfs_getsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
5014     caller_context_t *ct)
5015 {
5016         znode_t *zp = VTOZ(vp);
5017         zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5018         int error;
5019         boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
5020 
5021         ZFS_ENTER(zfsvfs);
5022         ZFS_VERIFY_ZP(zp);
5023         error = zfs_getacl(zp, vsecp, skipaclchk, cr);
5024         ZFS_EXIT(zfsvfs);
5025 
5026         return (error);
5027 }
5028 
5029 /*ARGSUSED*/
5030 static int
5031 zfs_setsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
5032     caller_context_t *ct)
5033 {
5034         znode_t *zp = VTOZ(vp);
5035         zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5036         int error;
5037         boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
5038         zilog_t *zilog = zfsvfs->z_log;
5039 
5040         ZFS_ENTER(zfsvfs);
5041         ZFS_VERIFY_ZP(zp);
5042 
5043         error = zfs_setacl(zp, vsecp, skipaclchk, cr);
5044 
5045         if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
5046                 zil_commit(zilog, 0);
5047 
5048         ZFS_EXIT(zfsvfs);
5049         return (error);
5050 }
5051 
5052 /*
5053  * The smallest read we may consider to loan out an arcbuf.
5054  * This must be a power of 2.
5055  */
5056 int zcr_blksz_min = (1 << 10);    /* 1K */
5057 /*
5058  * If set to less than the file block size, allow loaning out of an
5059  * arcbuf for a partial block read.  This must be a power of 2.
5060  */
5061 int zcr_blksz_max = (1 << 17);    /* 128K */
5062 
5063 /*ARGSUSED*/
5064 static int
5065 zfs_reqzcbuf(vnode_t *vp, enum uio_rw ioflag, xuio_t *xuio, cred_t *cr,
5066     caller_context_t *ct)
5067 {
5068         znode_t *zp = VTOZ(vp);
5069         zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5070         int max_blksz = zfsvfs->z_max_blksz;
5071         uio_t *uio = &xuio->xu_uio;
5072         ssize_t size = uio->uio_resid;
5073         offset_t offset = uio->uio_loffset;
5074         int blksz;
5075         int fullblk, i;
5076         arc_buf_t *abuf;
5077         ssize_t maxsize;
5078         int preamble, postamble;
5079 
5080         if (xuio->xu_type != UIOTYPE_ZEROCOPY)
5081                 return (SET_ERROR(EINVAL));
5082 
5083         ZFS_ENTER(zfsvfs);
5084         ZFS_VERIFY_ZP(zp);
5085         switch (ioflag) {
5086         case UIO_WRITE:
5087                 /*
5088                  * Loan out an arc_buf for write if write size is bigger than
5089                  * max_blksz, and the file's block size is also max_blksz.
5090                  */
5091                 blksz = max_blksz;
5092                 if (size < blksz || zp->z_blksz != blksz) {
5093                         ZFS_EXIT(zfsvfs);
5094                         return (SET_ERROR(EINVAL));
5095                 }
5096                 /*
5097                  * Caller requests buffers for write before knowing where the
5098                  * write offset might be (e.g. NFS TCP write).
5099                  */
5100                 if (offset == -1) {
5101                         preamble = 0;
5102                 } else {
5103                         preamble = P2PHASE(offset, blksz);
5104                         if (preamble) {
5105                                 preamble = blksz - preamble;
5106                                 size -= preamble;
5107                         }
5108                 }
5109 
5110                 postamble = P2PHASE(size, blksz);
5111                 size -= postamble;
5112 
5113                 fullblk = size / blksz;
5114                 (void) dmu_xuio_init(xuio,
5115                     (preamble != 0) + fullblk + (postamble != 0));
5116                 DTRACE_PROBE3(zfs_reqzcbuf_align, int, preamble,
5117                     int, postamble, int,
5118                     (preamble != 0) + fullblk + (postamble != 0));
5119 
5120                 /*
5121                  * Have to fix iov base/len for partial buffers.  They
5122                  * currently represent full arc_buf's.
5123                  */
5124                 if (preamble) {
5125                         /* data begins in the middle of the arc_buf */
5126                         abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
5127                             blksz);
5128                         ASSERT(abuf);
5129                         (void) dmu_xuio_add(xuio, abuf,
5130                             blksz - preamble, preamble);
5131                 }
5132 
5133                 for (i = 0; i < fullblk; i++) {
5134                         abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
5135                             blksz);
5136                         ASSERT(abuf);
5137                         (void) dmu_xuio_add(xuio, abuf, 0, blksz);
5138                 }
5139 
5140                 if (postamble) {
5141                         /* data ends in the middle of the arc_buf */
5142                         abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
5143                             blksz);
5144                         ASSERT(abuf);
5145                         (void) dmu_xuio_add(xuio, abuf, 0, postamble);
5146                 }
5147                 break;
5148         case UIO_READ:
5149                 /*
5150                  * Loan out an arc_buf for read if the read size is larger than
5151                  * the current file block size.  Block alignment is not
5152                  * considered.  Partial arc_buf will be loaned out for read.
5153                  */
5154                 blksz = zp->z_blksz;
5155                 if (blksz < zcr_blksz_min)
5156                         blksz = zcr_blksz_min;
5157                 if (blksz > zcr_blksz_max)
5158                         blksz = zcr_blksz_max;
5159                 /* avoid potential complexity of dealing with it */
5160                 if (blksz > max_blksz) {
5161                         ZFS_EXIT(zfsvfs);
5162                         return (SET_ERROR(EINVAL));
5163                 }
5164 
5165                 maxsize = zp->z_size - uio->uio_loffset;
5166                 if (size > maxsize)
5167                         size = maxsize;
5168 
5169                 if (size < blksz || vn_has_cached_data(vp)) {
5170                         ZFS_EXIT(zfsvfs);
5171                         return (SET_ERROR(EINVAL));
5172                 }
5173                 break;
5174         default:
5175                 ZFS_EXIT(zfsvfs);
5176                 return (SET_ERROR(EINVAL));
5177         }
5178 
5179         uio->uio_extflg = UIO_XUIO;
5180         XUIO_XUZC_RW(xuio) = ioflag;
5181         ZFS_EXIT(zfsvfs);
5182         return (0);
5183 }
5184 
5185 /*ARGSUSED*/
5186 static int
5187 zfs_retzcbuf(vnode_t *vp, xuio_t *xuio, cred_t *cr, caller_context_t *ct)
5188 {
5189         int i;
5190         arc_buf_t *abuf;
5191         int ioflag = XUIO_XUZC_RW(xuio);
5192 
5193         ASSERT(xuio->xu_type == UIOTYPE_ZEROCOPY);
5194 
5195         i = dmu_xuio_cnt(xuio);
5196         while (i-- > 0) {
5197                 abuf = dmu_xuio_arcbuf(xuio, i);
5198                 /*
5199                  * if abuf == NULL, it must be a write buffer
5200                  * that has been returned in zfs_write().
5201                  */
5202                 if (abuf)
5203                         dmu_return_arcbuf(abuf);
5204                 ASSERT(abuf || ioflag == UIO_WRITE);
5205         }
5206 
5207         dmu_xuio_fini(xuio);
5208         return (0);
5209 }
5210 
5211 /*
5212  * Predeclare these here so that the compiler assumes that
5213  * this is an "old style" function declaration that does
5214  * not include arguments => we won't get type mismatch errors
5215  * in the initializations that follow.
5216  */
5217 static int zfs_inval();
5218 static int zfs_isdir();
5219 
5220 static int
5221 zfs_inval()
5222 {
5223         return (SET_ERROR(EINVAL));
5224 }
5225 
5226 static int
5227 zfs_isdir()
5228 {
5229         return (SET_ERROR(EISDIR));
5230 }
5231 /*
5232  * Directory vnode operations template
5233  */
5234 vnodeops_t *zfs_dvnodeops;
5235 const fs_operation_def_t zfs_dvnodeops_template[] = {
5236         VOPNAME_OPEN,           { .vop_open = zfs_open },
5237         VOPNAME_CLOSE,          { .vop_close = zfs_close },
5238         VOPNAME_READ,           { .error = zfs_isdir },
5239         VOPNAME_WRITE,          { .error = zfs_isdir },
5240         VOPNAME_IOCTL,          { .vop_ioctl = zfs_ioctl },
5241         VOPNAME_GETATTR,        { .vop_getattr = zfs_getattr },
5242         VOPNAME_SETATTR,        { .vop_setattr = zfs_setattr },
5243         VOPNAME_ACCESS,         { .vop_access = zfs_access },
5244         VOPNAME_LOOKUP,         { .vop_lookup = zfs_lookup },
5245         VOPNAME_CREATE,         { .vop_create = zfs_create },
5246         VOPNAME_REMOVE,         { .vop_remove = zfs_remove },
5247         VOPNAME_LINK,           { .vop_link = zfs_link },
5248         VOPNAME_RENAME,         { .vop_rename = zfs_rename },
5249         VOPNAME_MKDIR,          { .vop_mkdir = zfs_mkdir },
5250         VOPNAME_RMDIR,          { .vop_rmdir = zfs_rmdir },
5251         VOPNAME_READDIR,        { .vop_readdir = zfs_readdir },
5252         VOPNAME_SYMLINK,        { .vop_symlink = zfs_symlink },
5253         VOPNAME_FSYNC,          { .vop_fsync = zfs_fsync },
5254         VOPNAME_INACTIVE,       { .vop_inactive = zfs_inactive },
5255         VOPNAME_FID,            { .vop_fid = zfs_fid },
5256         VOPNAME_SEEK,           { .vop_seek = zfs_seek },
5257         VOPNAME_PATHCONF,       { .vop_pathconf = zfs_pathconf },
5258         VOPNAME_GETSECATTR,     { .vop_getsecattr = zfs_getsecattr },
5259         VOPNAME_SETSECATTR,     { .vop_setsecattr = zfs_setsecattr },
5260         VOPNAME_VNEVENT,        { .vop_vnevent = fs_vnevent_support },
5261         NULL,                   NULL
5262 };
5263 
5264 /*
5265  * Regular file vnode operations template
5266  */
5267 vnodeops_t *zfs_fvnodeops;
5268 const fs_operation_def_t zfs_fvnodeops_template[] = {
5269         VOPNAME_OPEN,           { .vop_open = zfs_open },
5270         VOPNAME_CLOSE,          { .vop_close = zfs_close },
5271         VOPNAME_READ,           { .vop_read = zfs_read },
5272         VOPNAME_WRITE,          { .vop_write = zfs_write },
5273         VOPNAME_IOCTL,          { .vop_ioctl = zfs_ioctl },
5274         VOPNAME_GETATTR,        { .vop_getattr = zfs_getattr },
5275         VOPNAME_SETATTR,        { .vop_setattr = zfs_setattr },
5276         VOPNAME_ACCESS,         { .vop_access = zfs_access },
5277         VOPNAME_LOOKUP,         { .vop_lookup = zfs_lookup },
5278         VOPNAME_RENAME,         { .vop_rename = zfs_rename },
5279         VOPNAME_FSYNC,          { .vop_fsync = zfs_fsync },
5280         VOPNAME_INACTIVE,       { .vop_inactive = zfs_inactive },
5281         VOPNAME_FID,            { .vop_fid = zfs_fid },
5282         VOPNAME_SEEK,           { .vop_seek = zfs_seek },
5283         VOPNAME_FRLOCK,         { .vop_frlock = zfs_frlock },
5284         VOPNAME_SPACE,          { .vop_space = zfs_space },
5285         VOPNAME_GETPAGE,        { .vop_getpage = zfs_getpage },
5286         VOPNAME_PUTPAGE,        { .vop_putpage = zfs_putpage },
5287         VOPNAME_MAP,            { .vop_map = zfs_map },
5288         VOPNAME_ADDMAP,         { .vop_addmap = zfs_addmap },
5289         VOPNAME_DELMAP,         { .vop_delmap = zfs_delmap },
5290         VOPNAME_PATHCONF,       { .vop_pathconf = zfs_pathconf },
5291         VOPNAME_GETSECATTR,     { .vop_getsecattr = zfs_getsecattr },
5292         VOPNAME_SETSECATTR,     { .vop_setsecattr = zfs_setsecattr },
5293         VOPNAME_VNEVENT,        { .vop_vnevent = fs_vnevent_support },
5294         VOPNAME_REQZCBUF,       { .vop_reqzcbuf = zfs_reqzcbuf },
5295         VOPNAME_RETZCBUF,       { .vop_retzcbuf = zfs_retzcbuf },
5296         NULL,                   NULL
5297 };
5298 
5299 /*
5300  * Symbolic link vnode operations template
5301  */
5302 vnodeops_t *zfs_symvnodeops;
5303 const fs_operation_def_t zfs_symvnodeops_template[] = {
5304         VOPNAME_GETATTR,        { .vop_getattr = zfs_getattr },
5305         VOPNAME_SETATTR,        { .vop_setattr = zfs_setattr },
5306         VOPNAME_ACCESS,         { .vop_access = zfs_access },
5307         VOPNAME_RENAME,         { .vop_rename = zfs_rename },
5308         VOPNAME_READLINK,       { .vop_readlink = zfs_readlink },
5309         VOPNAME_INACTIVE,       { .vop_inactive = zfs_inactive },
5310         VOPNAME_FID,            { .vop_fid = zfs_fid },
5311         VOPNAME_PATHCONF,       { .vop_pathconf = zfs_pathconf },
5312         VOPNAME_VNEVENT,        { .vop_vnevent = fs_vnevent_support },
5313         NULL,                   NULL
5314 };
5315 
5316 /*
5317  * special share hidden files vnode operations template
5318  */
5319 vnodeops_t *zfs_sharevnodeops;
5320 const fs_operation_def_t zfs_sharevnodeops_template[] = {
5321         VOPNAME_GETATTR,        { .vop_getattr = zfs_getattr },
5322         VOPNAME_ACCESS,         { .vop_access = zfs_access },
5323         VOPNAME_INACTIVE,       { .vop_inactive = zfs_inactive },
5324         VOPNAME_FID,            { .vop_fid = zfs_fid },
5325         VOPNAME_PATHCONF,       { .vop_pathconf = zfs_pathconf },
5326         VOPNAME_GETSECATTR,     { .vop_getsecattr = zfs_getsecattr },
5327         VOPNAME_SETSECATTR,     { .vop_setsecattr = zfs_setsecattr },
5328         VOPNAME_VNEVENT,        { .vop_vnevent = fs_vnevent_support },
5329         NULL,                   NULL
5330 };
5331 
5332 /*
5333  * Extended attribute directory vnode operations template
5334  *
5335  * This template is identical to the directory vnodes
5336  * operation template except for restricted operations:
5337  *      VOP_MKDIR()
5338  *      VOP_SYMLINK()
5339  *
5340  * Note that there are other restrictions embedded in:
5341  *      zfs_create()    - restrict type to VREG
5342  *      zfs_link()      - no links into/out of attribute space
5343  *      zfs_rename()    - no moves into/out of attribute space
5344  */
5345 vnodeops_t *zfs_xdvnodeops;
5346 const fs_operation_def_t zfs_xdvnodeops_template[] = {
5347         VOPNAME_OPEN,           { .vop_open = zfs_open },
5348         VOPNAME_CLOSE,          { .vop_close = zfs_close },
5349         VOPNAME_IOCTL,          { .vop_ioctl = zfs_ioctl },
5350         VOPNAME_GETATTR,        { .vop_getattr = zfs_getattr },
5351         VOPNAME_SETATTR,        { .vop_setattr = zfs_setattr },
5352         VOPNAME_ACCESS,         { .vop_access = zfs_access },
5353         VOPNAME_LOOKUP,         { .vop_lookup = zfs_lookup },
5354         VOPNAME_CREATE,         { .vop_create = zfs_create },
5355         VOPNAME_REMOVE,         { .vop_remove = zfs_remove },
5356         VOPNAME_LINK,           { .vop_link = zfs_link },
5357         VOPNAME_RENAME,         { .vop_rename = zfs_rename },
5358         VOPNAME_MKDIR,          { .error = zfs_inval },
5359         VOPNAME_RMDIR,          { .vop_rmdir = zfs_rmdir },
5360         VOPNAME_READDIR,        { .vop_readdir = zfs_readdir },
5361         VOPNAME_SYMLINK,        { .error = zfs_inval },
5362         VOPNAME_FSYNC,          { .vop_fsync = zfs_fsync },
5363         VOPNAME_INACTIVE,       { .vop_inactive = zfs_inactive },
5364         VOPNAME_FID,            { .vop_fid = zfs_fid },
5365         VOPNAME_SEEK,           { .vop_seek = zfs_seek },
5366         VOPNAME_PATHCONF,       { .vop_pathconf = zfs_pathconf },
5367         VOPNAME_GETSECATTR,     { .vop_getsecattr = zfs_getsecattr },
5368         VOPNAME_SETSECATTR,     { .vop_setsecattr = zfs_setsecattr },
5369         VOPNAME_VNEVENT,        { .vop_vnevent = fs_vnevent_support },
5370         NULL,                   NULL
5371 };
5372 
5373 /*
5374  * Error vnode operations template
5375  */
5376 vnodeops_t *zfs_evnodeops;
5377 const fs_operation_def_t zfs_evnodeops_template[] = {
5378         VOPNAME_INACTIVE,       { .vop_inactive = zfs_inactive },
5379         VOPNAME_PATHCONF,       { .vop_pathconf = zfs_pathconf },
5380         NULL,                   NULL
5381 };