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 2013 Nexenta Systems, Inc.  All rights reserved.
  24  * Copyright (c) 2015 by Delphix. All rights reserved.
  25  * Copyright (c) 2014 Integros [integros.com]
  26  */
  27 
  28 #include <sys/types.h>
  29 #include <sys/param.h>
  30 #include <sys/systm.h>
  31 #include <sys/sysmacros.h>
  32 #include <sys/cmn_err.h>
  33 #include <sys/kmem.h>
  34 #include <sys/thread.h>
  35 #include <sys/file.h>
  36 #include <sys/vfs.h>
  37 #include <sys/zfs_znode.h>
  38 #include <sys/zfs_dir.h>
  39 #include <sys/zil.h>
  40 #include <sys/zil_impl.h>
  41 #include <sys/byteorder.h>
  42 #include <sys/policy.h>
  43 #include <sys/stat.h>
  44 #include <sys/mode.h>
  45 #include <sys/acl.h>
  46 #include <sys/dmu.h>
  47 #include <sys/spa.h>
  48 #include <sys/spa_impl.h>
  49 #include <sys/zfs_fuid.h>
  50 #include <sys/ddi.h>
  51 #include <sys/dsl_dataset.h>
  52 #include <sys/special.h>
  53 
  54 /*
  55  * These zfs_log_* functions must be called within a dmu tx, in one
  56  * of 2 contexts depending on zilog->z_replay:
  57  *
  58  * Non replay mode
  59  * ---------------
  60  * We need to record the transaction so that if it is committed to
  61  * the Intent Log then it can be replayed.  An intent log transaction
  62  * structure (itx_t) is allocated and all the information necessary to
  63  * possibly replay the transaction is saved in it. The itx is then assigned
  64  * a sequence number and inserted in the in-memory list anchored in the zilog.
  65  *
  66  * Replay mode
  67  * -----------
  68  * We need to mark the intent log record as replayed in the log header.
  69  * This is done in the same transaction as the replay so that they
  70  * commit atomically.
  71  */
  72 
  73 int
  74 zfs_log_create_txtype(zil_create_t type, vsecattr_t *vsecp, vattr_t *vap)
  75 {
  76         int isxvattr = (vap->va_mask & AT_XVATTR);
  77         switch (type) {
  78         case Z_FILE:
  79                 if (vsecp == NULL && !isxvattr)
  80                         return (TX_CREATE);
  81                 if (vsecp && isxvattr)
  82                         return (TX_CREATE_ACL_ATTR);
  83                 if (vsecp)
  84                         return (TX_CREATE_ACL);
  85                 else
  86                         return (TX_CREATE_ATTR);
  87                 /*NOTREACHED*/
  88         case Z_DIR:
  89                 if (vsecp == NULL && !isxvattr)
  90                         return (TX_MKDIR);
  91                 if (vsecp && isxvattr)
  92                         return (TX_MKDIR_ACL_ATTR);
  93                 if (vsecp)
  94                         return (TX_MKDIR_ACL);
  95                 else
  96                         return (TX_MKDIR_ATTR);
  97         case Z_XATTRDIR:
  98                 return (TX_MKXATTR);
  99         }
 100         ASSERT(0);
 101         return (TX_MAX_TYPE);
 102 }
 103 
 104 /*
 105  * build up the log data necessary for logging xvattr_t
 106  * First lr_attr_t is initialized.  following the lr_attr_t
 107  * is the mapsize and attribute bitmap copied from the xvattr_t.
 108  * Following the bitmap and bitmapsize two 64 bit words are reserved
 109  * for the create time which may be set.  Following the create time
 110  * records a single 64 bit integer which has the bits to set on
 111  * replay for the xvattr.
 112  */
 113 static void
 114 zfs_log_xvattr(lr_attr_t *lrattr, xvattr_t *xvap)
 115 {
 116         uint32_t        *bitmap;
 117         uint64_t        *attrs;
 118         uint64_t        *crtime;
 119         xoptattr_t      *xoap;
 120         void            *scanstamp;
 121         int             i;
 122 
 123         xoap = xva_getxoptattr(xvap);
 124         ASSERT(xoap);
 125 
 126         lrattr->lr_attr_masksize = xvap->xva_mapsize;
 127         bitmap = &lrattr->lr_attr_bitmap;
 128         for (i = 0; i != xvap->xva_mapsize; i++, bitmap++) {
 129                 *bitmap = xvap->xva_reqattrmap[i];
 130         }
 131 
 132         /* Now pack the attributes up in a single uint64_t */
 133         attrs = (uint64_t *)bitmap;
 134         crtime = attrs + 1;
 135         scanstamp = (caddr_t)(crtime + 2);
 136         *attrs = 0;
 137         if (XVA_ISSET_REQ(xvap, XAT_READONLY))
 138                 *attrs |= (xoap->xoa_readonly == 0) ? 0 :
 139                     XAT0_READONLY;
 140         if (XVA_ISSET_REQ(xvap, XAT_HIDDEN))
 141                 *attrs |= (xoap->xoa_hidden == 0) ? 0 :
 142                     XAT0_HIDDEN;
 143         if (XVA_ISSET_REQ(xvap, XAT_SYSTEM))
 144                 *attrs |= (xoap->xoa_system == 0) ? 0 :
 145                     XAT0_SYSTEM;
 146         if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE))
 147                 *attrs |= (xoap->xoa_archive == 0) ? 0 :
 148                     XAT0_ARCHIVE;
 149         if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE))
 150                 *attrs |= (xoap->xoa_immutable == 0) ? 0 :
 151                     XAT0_IMMUTABLE;
 152         if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK))
 153                 *attrs |= (xoap->xoa_nounlink == 0) ? 0 :
 154                     XAT0_NOUNLINK;
 155         if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY))
 156                 *attrs |= (xoap->xoa_appendonly == 0) ? 0 :
 157                     XAT0_APPENDONLY;
 158         if (XVA_ISSET_REQ(xvap, XAT_OPAQUE))
 159                 *attrs |= (xoap->xoa_opaque == 0) ? 0 :
 160                     XAT0_APPENDONLY;
 161         if (XVA_ISSET_REQ(xvap, XAT_NODUMP))
 162                 *attrs |= (xoap->xoa_nodump == 0) ? 0 :
 163                     XAT0_NODUMP;
 164         if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED))
 165                 *attrs |= (xoap->xoa_av_quarantined == 0) ? 0 :
 166                     XAT0_AV_QUARANTINED;
 167         if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED))
 168                 *attrs |= (xoap->xoa_av_modified == 0) ? 0 :
 169                     XAT0_AV_MODIFIED;
 170         if (XVA_ISSET_REQ(xvap, XAT_CREATETIME))
 171                 ZFS_TIME_ENCODE(&xoap->xoa_createtime, crtime);
 172         if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
 173                 bcopy(xoap->xoa_av_scanstamp, scanstamp, AV_SCANSTAMP_SZ);
 174         if (XVA_ISSET_REQ(xvap, XAT_REPARSE))
 175                 *attrs |= (xoap->xoa_reparse == 0) ? 0 :
 176                     XAT0_REPARSE;
 177         if (XVA_ISSET_REQ(xvap, XAT_OFFLINE))
 178                 *attrs |= (xoap->xoa_offline == 0) ? 0 :
 179                     XAT0_OFFLINE;
 180         if (XVA_ISSET_REQ(xvap, XAT_SPARSE))
 181                 *attrs |= (xoap->xoa_sparse == 0) ? 0 :
 182                     XAT0_SPARSE;
 183 }
 184 
 185 static void *
 186 zfs_log_fuid_ids(zfs_fuid_info_t *fuidp, void *start)
 187 {
 188         zfs_fuid_t *zfuid;
 189         uint64_t *fuidloc = start;
 190 
 191         /* First copy in the ACE FUIDs */
 192         for (zfuid = list_head(&fuidp->z_fuids); zfuid;
 193             zfuid = list_next(&fuidp->z_fuids, zfuid)) {
 194                 *fuidloc++ = zfuid->z_logfuid;
 195         }
 196         return (fuidloc);
 197 }
 198 
 199 
 200 static void *
 201 zfs_log_fuid_domains(zfs_fuid_info_t *fuidp, void *start)
 202 {
 203         zfs_fuid_domain_t *zdomain;
 204 
 205         /* now copy in the domain info, if any */
 206         if (fuidp->z_domain_str_sz != 0) {
 207                 for (zdomain = list_head(&fuidp->z_domains); zdomain;
 208                     zdomain = list_next(&fuidp->z_domains, zdomain)) {
 209                         bcopy((void *)zdomain->z_domain, start,
 210                             strlen(zdomain->z_domain) + 1);
 211                         start = (caddr_t)start +
 212                             strlen(zdomain->z_domain) + 1;
 213                 }
 214         }
 215         return (start);
 216 }
 217 
 218 /*
 219  * Handles TX_CREATE, TX_CREATE_ATTR, TX_MKDIR, TX_MKDIR_ATTR and
 220  * TK_MKXATTR transactions.
 221  *
 222  * TX_CREATE and TX_MKDIR are standard creates, but they may have FUID
 223  * domain information appended prior to the name.  In this case the
 224  * uid/gid in the log record will be a log centric FUID.
 225  *
 226  * TX_CREATE_ACL_ATTR and TX_MKDIR_ACL_ATTR handle special creates that
 227  * may contain attributes, ACL and optional fuid information.
 228  *
 229  * TX_CREATE_ACL and TX_MKDIR_ACL handle special creates that specify
 230  * and ACL and normal users/groups in the ACEs.
 231  *
 232  * There may be an optional xvattr attribute information similar
 233  * to zfs_log_setattr.
 234  *
 235  * Also, after the file name "domain" strings may be appended.
 236  */
 237 void
 238 zfs_log_create(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
 239     znode_t *dzp, znode_t *zp, char *name, vsecattr_t *vsecp,
 240     zfs_fuid_info_t *fuidp, vattr_t *vap)
 241 {
 242         itx_t *itx;
 243         lr_create_t *lr;
 244         lr_acl_create_t *lracl;
 245         size_t aclsize = (vsecp != NULL) ? vsecp->vsa_aclentsz : 0;
 246         size_t xvatsize = 0;
 247         size_t txsize;
 248         xvattr_t *xvap = (xvattr_t *)vap;
 249         void *end;
 250         size_t lrsize;
 251         size_t namesize = strlen(name) + 1;
 252         size_t fuidsz = 0;
 253 
 254         if (zil_replaying(zilog, tx))
 255                 return;
 256 
 257         /*
 258          * If we have FUIDs present then add in space for
 259          * domains and ACE fuid's if any.
 260          */
 261         if (fuidp) {
 262                 fuidsz += fuidp->z_domain_str_sz;
 263                 fuidsz += fuidp->z_fuid_cnt * sizeof (uint64_t);
 264         }
 265 
 266         if (vap->va_mask & AT_XVATTR)
 267                 xvatsize = ZIL_XVAT_SIZE(xvap->xva_mapsize);
 268 
 269         if ((int)txtype == TX_CREATE_ATTR || (int)txtype == TX_MKDIR_ATTR ||
 270             (int)txtype == TX_CREATE || (int)txtype == TX_MKDIR ||
 271             (int)txtype == TX_MKXATTR) {
 272                 txsize = sizeof (*lr) + namesize + fuidsz + xvatsize;
 273                 lrsize = sizeof (*lr);
 274         } else {
 275                 txsize =
 276                     sizeof (lr_acl_create_t) + namesize + fuidsz +
 277                     ZIL_ACE_LENGTH(aclsize) + xvatsize;
 278                 lrsize = sizeof (lr_acl_create_t);
 279         }
 280 
 281         itx = zil_itx_create(txtype, txsize);
 282 
 283         lr = (lr_create_t *)&itx->itx_lr;
 284         lr->lr_doid = dzp->z_id;
 285         lr->lr_foid = zp->z_id;
 286         lr->lr_mode = zp->z_mode;
 287         if (!IS_EPHEMERAL(zp->z_uid)) {
 288                 lr->lr_uid = (uint64_t)zp->z_uid;
 289         } else {
 290                 lr->lr_uid = fuidp->z_fuid_owner;
 291         }
 292         if (!IS_EPHEMERAL(zp->z_gid)) {
 293                 lr->lr_gid = (uint64_t)zp->z_gid;
 294         } else {
 295                 lr->lr_gid = fuidp->z_fuid_group;
 296         }
 297         (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zp->z_zfsvfs), &lr->lr_gen,
 298             sizeof (uint64_t));
 299         (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(zp->z_zfsvfs),
 300             lr->lr_crtime, sizeof (uint64_t) * 2);
 301 
 302         if (sa_lookup(zp->z_sa_hdl, SA_ZPL_RDEV(zp->z_zfsvfs), &lr->lr_rdev,
 303             sizeof (lr->lr_rdev)) != 0)
 304                 lr->lr_rdev = 0;
 305 
 306         /*
 307          * Fill in xvattr info if any
 308          */
 309         if (vap->va_mask & AT_XVATTR) {
 310                 zfs_log_xvattr((lr_attr_t *)((caddr_t)lr + lrsize), xvap);
 311                 end = (caddr_t)lr + lrsize + xvatsize;
 312         } else {
 313                 end = (caddr_t)lr + lrsize;
 314         }
 315 
 316         /* Now fill in any ACL info */
 317 
 318         if (vsecp) {
 319                 lracl = (lr_acl_create_t *)&itx->itx_lr;
 320                 lracl->lr_aclcnt = vsecp->vsa_aclcnt;
 321                 lracl->lr_acl_bytes = aclsize;
 322                 lracl->lr_domcnt = fuidp ? fuidp->z_domain_cnt : 0;
 323                 lracl->lr_fuidcnt  = fuidp ? fuidp->z_fuid_cnt : 0;
 324                 if (vsecp->vsa_aclflags & VSA_ACE_ACLFLAGS)
 325                         lracl->lr_acl_flags = (uint64_t)vsecp->vsa_aclflags;
 326                 else
 327                         lracl->lr_acl_flags = 0;
 328 
 329                 bcopy(vsecp->vsa_aclentp, end, aclsize);
 330                 end = (caddr_t)end + ZIL_ACE_LENGTH(aclsize);
 331         }
 332 
 333         /* drop in FUID info */
 334         if (fuidp) {
 335                 end = zfs_log_fuid_ids(fuidp, end);
 336                 end = zfs_log_fuid_domains(fuidp, end);
 337         }
 338         /*
 339          * Now place file name in log record
 340          */
 341         bcopy(name, end, namesize);
 342 
 343         zil_itx_assign(zilog, itx, tx);
 344 }
 345 
 346 /*
 347  * Handles both TX_REMOVE and TX_RMDIR transactions.
 348  */
 349 void
 350 zfs_log_remove(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
 351     znode_t *dzp, char *name, uint64_t foid)
 352 {
 353         itx_t *itx;
 354         lr_remove_t *lr;
 355         size_t namesize = strlen(name) + 1;
 356 
 357         if (zil_replaying(zilog, tx))
 358                 return;
 359 
 360         itx = zil_itx_create(txtype, sizeof (*lr) + namesize);
 361         lr = (lr_remove_t *)&itx->itx_lr;
 362         lr->lr_doid = dzp->z_id;
 363         bcopy(name, (char *)(lr + 1), namesize);
 364 
 365         itx->itx_oid = foid;
 366 
 367         zil_itx_assign(zilog, itx, tx);
 368 }
 369 
 370 /*
 371  * Handles TX_LINK transactions.
 372  */
 373 void
 374 zfs_log_link(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
 375     znode_t *dzp, znode_t *zp, char *name)
 376 {
 377         itx_t *itx;
 378         lr_link_t *lr;
 379         size_t namesize = strlen(name) + 1;
 380 
 381         if (zil_replaying(zilog, tx))
 382                 return;
 383 
 384         itx = zil_itx_create(txtype, sizeof (*lr) + namesize);
 385         lr = (lr_link_t *)&itx->itx_lr;
 386         lr->lr_doid = dzp->z_id;
 387         lr->lr_link_obj = zp->z_id;
 388         bcopy(name, (char *)(lr + 1), namesize);
 389 
 390         zil_itx_assign(zilog, itx, tx);
 391 }
 392 
 393 /*
 394  * Handles TX_SYMLINK transactions.
 395  */
 396 void
 397 zfs_log_symlink(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
 398     znode_t *dzp, znode_t *zp, char *name, char *link)
 399 {
 400         itx_t *itx;
 401         lr_create_t *lr;
 402         size_t namesize = strlen(name) + 1;
 403         size_t linksize = strlen(link) + 1;
 404 
 405         if (zil_replaying(zilog, tx))
 406                 return;
 407 
 408         itx = zil_itx_create(txtype, sizeof (*lr) + namesize + linksize);
 409         lr = (lr_create_t *)&itx->itx_lr;
 410         lr->lr_doid = dzp->z_id;
 411         lr->lr_foid = zp->z_id;
 412         lr->lr_uid = zp->z_uid;
 413         lr->lr_gid = zp->z_gid;
 414         lr->lr_mode = zp->z_mode;
 415         (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zp->z_zfsvfs), &lr->lr_gen,
 416             sizeof (uint64_t));
 417         (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(zp->z_zfsvfs),
 418             lr->lr_crtime, sizeof (uint64_t) * 2);
 419         bcopy(name, (char *)(lr + 1), namesize);
 420         bcopy(link, (char *)(lr + 1) + namesize, linksize);
 421 
 422         zil_itx_assign(zilog, itx, tx);
 423 }
 424 
 425 /*
 426  * Handles TX_RENAME transactions.
 427  */
 428 void
 429 zfs_log_rename(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
 430     znode_t *sdzp, char *sname, znode_t *tdzp, char *dname, znode_t *szp)
 431 {
 432         itx_t *itx;
 433         lr_rename_t *lr;
 434         size_t snamesize = strlen(sname) + 1;
 435         size_t dnamesize = strlen(dname) + 1;
 436 
 437         if (zil_replaying(zilog, tx))
 438                 return;
 439 
 440         itx = zil_itx_create(txtype, sizeof (*lr) + snamesize + dnamesize);
 441         lr = (lr_rename_t *)&itx->itx_lr;
 442         lr->lr_sdoid = sdzp->z_id;
 443         lr->lr_tdoid = tdzp->z_id;
 444         bcopy(sname, (char *)(lr + 1), snamesize);
 445         bcopy(dname, (char *)(lr + 1) + snamesize, dnamesize);
 446         itx->itx_oid = szp->z_id;
 447 
 448         zil_itx_assign(zilog, itx, tx);
 449 }
 450 
 451 /*
 452  * Handles TX_WRITE transactions.
 453  */
 454 ssize_t zfs_immediate_write_sz = 32768;
 455 
 456 void
 457 zfs_log_write(zilog_t *zilog, dmu_tx_t *tx, int txtype,
 458     znode_t *zp, offset_t off, ssize_t resid, int ioflag)
 459 {
 460         spa_t *spa = zilog->zl_spa;
 461         spa_meta_placement_t *mp = &spa->spa_meta_policy;
 462         itx_wr_state_t write_state;
 463         boolean_t slogging, zil_to_special, write_to_special;
 464         size_t immediate_write_sz;
 465         uint32_t blocksize = zp->z_blksz;
 466         uintptr_t fsync_cnt;
 467 
 468         if (zil_replaying(zilog, tx) || zp->z_unlinked)
 469                 return;
 470 
 471         /*
 472          * Decide how to handle the write:
 473          * - WR_INDIRECT  - synchronously write in zfs format, via dmu_sync()
 474          * - WR_COPIED    - write to slog following the tx descriptor as
 475          *                  immediate data
 476          * - WR_NEED_COPY - copy out in the future (e.g. with next sync)
 477          *
 478          * Special vdevs are as fast as slogs - therefore a conservative
 479          * extension to the existing logic allows for the following
 480          * zpool-configurable options:
 481          *
 482          * (1) SYNC_TO_SPECIAL_DISABLED: do not use special vdev,
 483          *     neither for zil, nor for WR_INDIRECT
 484          * (2) SYNC_TO_SPECIAL_STANDARD (default): use special vdev
 485          *     exactly like slog
 486          * The remaining two options add the capability to sync data to
 487          * special vdev:
 488          * (3) SYNC_TO_SPECIAL_BALANCED: same as "standard", plus
 489          *     load balance writes to the special vdev
 490          * (4) SYNC_TO_SPECIAL_ALWAYS: same as "standard" plus always
 491          *     write to the special vdev
 492          *
 493          * Presence of special vdev has no affect if slog is configured:
 494          * the latter indicates that user expects conventional zfs
 495          * sync-write behavior.
 496          */
 497 
 498         immediate_write_sz = (zilog->zl_logbias == ZFS_LOGBIAS_THROUGHPUT)
 499             ? 0 : zfs_immediate_write_sz;
 500 
 501         /* use special only if all of the following is true */
 502         zil_to_special = !spa_has_slogs(spa) &&
 503             spa_can_special_be_used(spa) &&
 504             mp->spa_sync_to_special != SYNC_TO_SPECIAL_DISABLED;
 505 
 506         /*
 507          * synchronously write data to special in zfs format - the
 508          * WR_INDIRECT case
 509          *
 510          * for the "balanced" option distribute the load based on the
 511          * special-to-normal ratio - the value that is periodically
 512          * recomputed by the load balancer implementing one of
 513          * SPA_SPECIAL_SELECTION_LATENCY etc. strategies
 514          */
 515         write_to_special = !spa_has_slogs(spa) &&
 516             spa_write_data_to_special(spa, zilog->zl_os) &&
 517             (mp->spa_sync_to_special == SYNC_TO_SPECIAL_ALWAYS ||
 518             (mp->spa_sync_to_special == SYNC_TO_SPECIAL_BALANCED &&
 519             spa->spa_avg_stat_rotor % 100 < spa->spa_special_to_normal_ratio));
 520 
 521         slogging = (spa_has_slogs(spa) || zil_to_special) &&
 522             zilog->zl_logbias == ZFS_LOGBIAS_LATENCY;
 523 
 524         if (resid > immediate_write_sz && !slogging && resid <= blocksize)
 525                 write_state = WR_INDIRECT;
 526         else if (zilog->zl_logbias == ZFS_LOGBIAS_THROUGHPUT)
 527                 write_state = WR_INDIRECT;
 528         else if (!spa_has_slogs(zilog->zl_spa) &&
 529             resid >= zfs_immediate_write_sz)
 530                 write_state = WR_INDIRECT;
 531         else if (write_to_special)
 532                 write_state = WR_INDIRECT;
 533         else if (ioflag & (FSYNC | FDSYNC))
 534                 write_state = WR_COPIED;
 535         else
 536                 write_state = WR_NEED_COPY;
 537 
 538         DTRACE_PROBE3(zfs_lwr, ssize_t, immediate_write_sz,
 539             itx_wr_state_t, write_state, uint_t, zp->z_blksz);
 540 
 541         if ((fsync_cnt = (uintptr_t)tsd_get(zfs_fsyncer_key)) != 0) {
 542                 (void) tsd_set(zfs_fsyncer_key, (void *)(fsync_cnt - 1));
 543         }
 544 
 545         while (resid) {
 546                 itx_t *itx;
 547                 lr_write_t *lr;
 548                 itx_wr_state_t wr_state = write_state;
 549                 ssize_t len = resid;
 550 
 551                 if (wr_state == WR_COPIED && resid > ZIL_MAX_COPIED_DATA)
 552                         wr_state = WR_NEED_COPY;
 553                 else if (wr_state == WR_INDIRECT)
 554                         len = MIN(blocksize - P2PHASE(off, blocksize), resid);
 555 
 556                 itx = zil_itx_create(txtype, sizeof (*lr) +
 557                     (wr_state == WR_COPIED ? len : 0));
 558                 lr = (lr_write_t *)&itx->itx_lr;
 559                 if (wr_state == WR_COPIED && dmu_read(zp->z_zfsvfs->z_os,
 560                     zp->z_id, off, len, lr + 1, DMU_READ_NO_PREFETCH) != 0) {
 561                         zil_itx_destroy(itx);
 562                         itx = zil_itx_create(txtype, sizeof (*lr));
 563                         lr = (lr_write_t *)&itx->itx_lr;
 564                         wr_state = WR_NEED_COPY;
 565                 }
 566 
 567                 itx->itx_wr_state = wr_state;
 568                 lr->lr_foid = zp->z_id;
 569                 lr->lr_offset = off;
 570                 lr->lr_length = len;
 571                 lr->lr_blkoff = 0;
 572                 BP_ZERO(&lr->lr_blkptr);
 573 
 574                 itx->itx_private = zp->z_zfsvfs;
 575 
 576                 if (!(ioflag & (FSYNC | FDSYNC)) && (zp->z_sync_cnt == 0) &&
 577                     (fsync_cnt == 0))
 578                         itx->itx_sync = B_FALSE;
 579 
 580                 zil_itx_assign(zilog, itx, tx);
 581 
 582                 off += len;
 583                 resid -= len;
 584         }
 585 }
 586 
 587 /*
 588  * Handles TX_TRUNCATE transactions.
 589  */
 590 void
 591 zfs_log_truncate(zilog_t *zilog, dmu_tx_t *tx, int txtype,
 592     znode_t *zp, uint64_t off, uint64_t len)
 593 {
 594         itx_t *itx;
 595         lr_truncate_t *lr;
 596 
 597         if (zil_replaying(zilog, tx) || zp->z_unlinked)
 598                 return;
 599 
 600         itx = zil_itx_create(txtype, sizeof (*lr));
 601         lr = (lr_truncate_t *)&itx->itx_lr;
 602         lr->lr_foid = zp->z_id;
 603         lr->lr_offset = off;
 604         lr->lr_length = len;
 605 
 606         itx->itx_sync = (zp->z_sync_cnt != 0);
 607         zil_itx_assign(zilog, itx, tx);
 608 }
 609 
 610 /*
 611  * Handles TX_SETATTR transactions.
 612  */
 613 void
 614 zfs_log_setattr(zilog_t *zilog, dmu_tx_t *tx, int txtype,
 615     znode_t *zp, vattr_t *vap, uint_t mask_applied, zfs_fuid_info_t *fuidp)
 616 {
 617         itx_t           *itx;
 618         lr_setattr_t    *lr;
 619         xvattr_t        *xvap = (xvattr_t *)vap;
 620         size_t          recsize = sizeof (lr_setattr_t);
 621         void            *start;
 622 
 623         if (zil_replaying(zilog, tx) || zp->z_unlinked)
 624                 return;
 625 
 626         /*
 627          * If XVATTR set, then log record size needs to allow
 628          * for lr_attr_t + xvattr mask, mapsize and create time
 629          * plus actual attribute values
 630          */
 631         if (vap->va_mask & AT_XVATTR)
 632                 recsize = sizeof (*lr) + ZIL_XVAT_SIZE(xvap->xva_mapsize);
 633 
 634         if (fuidp)
 635                 recsize += fuidp->z_domain_str_sz;
 636 
 637         itx = zil_itx_create(txtype, recsize);
 638         lr = (lr_setattr_t *)&itx->itx_lr;
 639         lr->lr_foid = zp->z_id;
 640         lr->lr_mask = (uint64_t)mask_applied;
 641         lr->lr_mode = (uint64_t)vap->va_mode;
 642         if ((mask_applied & AT_UID) && IS_EPHEMERAL(vap->va_uid))
 643                 lr->lr_uid = fuidp->z_fuid_owner;
 644         else
 645                 lr->lr_uid = (uint64_t)vap->va_uid;
 646 
 647         if ((mask_applied & AT_GID) && IS_EPHEMERAL(vap->va_gid))
 648                 lr->lr_gid = fuidp->z_fuid_group;
 649         else
 650                 lr->lr_gid = (uint64_t)vap->va_gid;
 651 
 652         lr->lr_size = (uint64_t)vap->va_size;
 653         ZFS_TIME_ENCODE(&vap->va_atime, lr->lr_atime);
 654         ZFS_TIME_ENCODE(&vap->va_mtime, lr->lr_mtime);
 655         start = (lr_setattr_t *)(lr + 1);
 656         if (vap->va_mask & AT_XVATTR) {
 657                 zfs_log_xvattr((lr_attr_t *)start, xvap);
 658                 start = (caddr_t)start + ZIL_XVAT_SIZE(xvap->xva_mapsize);
 659         }
 660 
 661         /*
 662          * Now stick on domain information if any on end
 663          */
 664 
 665         if (fuidp)
 666                 (void) zfs_log_fuid_domains(fuidp, start);
 667 
 668         itx->itx_sync = (zp->z_sync_cnt != 0);
 669         zil_itx_assign(zilog, itx, tx);
 670 }
 671 
 672 /*
 673  * Handles TX_ACL transactions.
 674  */
 675 void
 676 zfs_log_acl(zilog_t *zilog, dmu_tx_t *tx, znode_t *zp,
 677     vsecattr_t *vsecp, zfs_fuid_info_t *fuidp)
 678 {
 679         itx_t *itx;
 680         lr_acl_v0_t *lrv0;
 681         lr_acl_t *lr;
 682         int txtype;
 683         int lrsize;
 684         size_t txsize;
 685         size_t aclbytes = vsecp->vsa_aclentsz;
 686 
 687         if (zil_replaying(zilog, tx) || zp->z_unlinked)
 688                 return;
 689 
 690         txtype = (zp->z_zfsvfs->z_version < ZPL_VERSION_FUID) ?
 691             TX_ACL_V0 : TX_ACL;
 692 
 693         if (txtype == TX_ACL)
 694                 lrsize = sizeof (*lr);
 695         else
 696                 lrsize = sizeof (*lrv0);
 697 
 698         txsize = lrsize +
 699             ((txtype == TX_ACL) ? ZIL_ACE_LENGTH(aclbytes) : aclbytes) +
 700             (fuidp ? fuidp->z_domain_str_sz : 0) +
 701             sizeof (uint64_t) * (fuidp ? fuidp->z_fuid_cnt : 0);
 702 
 703         itx = zil_itx_create(txtype, txsize);
 704 
 705         lr = (lr_acl_t *)&itx->itx_lr;
 706         lr->lr_foid = zp->z_id;
 707         if (txtype == TX_ACL) {
 708                 lr->lr_acl_bytes = aclbytes;
 709                 lr->lr_domcnt = fuidp ? fuidp->z_domain_cnt : 0;
 710                 lr->lr_fuidcnt = fuidp ? fuidp->z_fuid_cnt : 0;
 711                 if (vsecp->vsa_mask & VSA_ACE_ACLFLAGS)
 712                         lr->lr_acl_flags = (uint64_t)vsecp->vsa_aclflags;
 713                 else
 714                         lr->lr_acl_flags = 0;
 715         }
 716         lr->lr_aclcnt = (uint64_t)vsecp->vsa_aclcnt;
 717 
 718         if (txtype == TX_ACL_V0) {
 719                 lrv0 = (lr_acl_v0_t *)lr;
 720                 bcopy(vsecp->vsa_aclentp, (ace_t *)(lrv0 + 1), aclbytes);
 721         } else {
 722                 void *start = (ace_t *)(lr + 1);
 723 
 724                 bcopy(vsecp->vsa_aclentp, start, aclbytes);
 725 
 726                 start = (caddr_t)start + ZIL_ACE_LENGTH(aclbytes);
 727 
 728                 if (fuidp) {
 729                         start = zfs_log_fuid_ids(fuidp, start);
 730                         (void) zfs_log_fuid_domains(fuidp, start);
 731                 }
 732         }
 733 
 734         itx->itx_sync = (zp->z_sync_cnt != 0);
 735         zil_itx_assign(zilog, itx, tx);
 736 }