Home | History | Annotate | Download | only in zfs
      1    789    ahrens /*
      2    789    ahrens  * CDDL HEADER START
      3    789    ahrens  *
      4    789    ahrens  * The contents of this file are subject to the terms of the
      5   1669    perrin  * Common Development and Distribution License (the "License").
      6   1669    perrin  * You may not use this file except in compliance with the License.
      7    789    ahrens  *
      8    789    ahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
      9    789    ahrens  * or http://www.opensolaris.org/os/licensing.
     10    789    ahrens  * See the License for the specific language governing permissions
     11    789    ahrens  * and limitations under the License.
     12    789    ahrens  *
     13    789    ahrens  * When distributing Covered Code, include this CDDL HEADER in each
     14    789    ahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
     15    789    ahrens  * If applicable, add the following below this CDDL HEADER, with the
     16    789    ahrens  * fields enclosed by brackets "[]" replaced with your own identifying
     17    789    ahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
     18    789    ahrens  *
     19    789    ahrens  * CDDL HEADER END
     20    789    ahrens  */
     21    789    ahrens /*
     22   9401      Neil  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
     23    789    ahrens  * Use is subject to license terms.
     24    789    ahrens  */
     25    789    ahrens 
     26    789    ahrens #include <sys/types.h>
     27    789    ahrens #include <sys/param.h>
     28    789    ahrens #include <sys/systm.h>
     29    789    ahrens #include <sys/sysmacros.h>
     30    789    ahrens #include <sys/cmn_err.h>
     31    789    ahrens #include <sys/kmem.h>
     32    789    ahrens #include <sys/thread.h>
     33    789    ahrens #include <sys/file.h>
     34    789    ahrens #include <sys/vfs.h>
     35    789    ahrens #include <sys/zfs_znode.h>
     36    789    ahrens #include <sys/zfs_dir.h>
     37    789    ahrens #include <sys/zil.h>
     38   4620    perrin #include <sys/zil_impl.h>
     39    789    ahrens #include <sys/byteorder.h>
     40    789    ahrens #include <sys/policy.h>
     41    789    ahrens #include <sys/stat.h>
     42    789    ahrens #include <sys/mode.h>
     43    789    ahrens #include <sys/acl.h>
     44    789    ahrens #include <sys/dmu.h>
     45    789    ahrens #include <sys/spa.h>
     46   5331       amw #include <sys/zfs_fuid.h>
     47    789    ahrens #include <sys/ddi.h>
     48   8227      Neil #include <sys/dsl_dataset.h>
     49    789    ahrens 
     50    789    ahrens /*
     51   8227      Neil  * These zfs_log_* functions must be called within a dmu tx, in one
     52   8227      Neil  * of 2 contexts depending on zilog->z_replay:
     53   8227      Neil  *
     54   8227      Neil  * Non replay mode
     55   8227      Neil  * ---------------
     56   8227      Neil  * We need to record the transaction so that if it is committed to
     57   8227      Neil  * the Intent Log then it can be replayed.  An intent log transaction
     58   8227      Neil  * structure (itx_t) is allocated and all the information necessary to
     59   8227      Neil  * possibly replay the transaction is saved in it. The itx is then assigned
     60   8227      Neil  * a sequence number and inserted in the in-memory list anchored in the zilog.
     61   8227      Neil  *
     62   8227      Neil  * Replay mode
     63   8227      Neil  * -----------
     64   8227      Neil  * We need to mark the intent log record as replayed in the log header.
     65   8227      Neil  * This is done in the same transaction as the replay so that they
     66   8227      Neil  * commit atomically.
     67    789    ahrens  */
     68    789    ahrens 
     69   5331       amw int
     70   5331       amw zfs_log_create_txtype(zil_create_t type, vsecattr_t *vsecp, vattr_t *vap)
     71   5331       amw {
     72   5331       amw 	int isxvattr = (vap->va_mask & AT_XVATTR);
     73   5331       amw 	switch (type) {
     74   5331       amw 	case Z_FILE:
     75   5331       amw 		if (vsecp == NULL && !isxvattr)
     76   5331       amw 			return (TX_CREATE);
     77   5331       amw 		if (vsecp && isxvattr)
     78   5331       amw 			return (TX_CREATE_ACL_ATTR);
     79   5331       amw 		if (vsecp)
     80   5331       amw 			return (TX_CREATE_ACL);
     81   5331       amw 		else
     82   5331       amw 			return (TX_CREATE_ATTR);
     83   5331       amw 		/*NOTREACHED*/
     84   5331       amw 	case Z_DIR:
     85   5331       amw 		if (vsecp == NULL && !isxvattr)
     86   5331       amw 			return (TX_MKDIR);
     87   5331       amw 		if (vsecp && isxvattr)
     88   5331       amw 			return (TX_MKDIR_ACL_ATTR);
     89   5331       amw 		if (vsecp)
     90   5331       amw 			return (TX_MKDIR_ACL);
     91   5331       amw 		else
     92   5331       amw 			return (TX_MKDIR_ATTR);
     93   5331       amw 	case Z_XATTRDIR:
     94   5331       amw 		return (TX_MKXATTR);
     95   5331       amw 	}
     96   5331       amw 	ASSERT(0);
     97   5331       amw 	return (TX_MAX_TYPE);
     98   5331       amw }
     99   5331       amw 
    100    789    ahrens /*
    101   5331       amw  * build up the log data necessary for logging xvattr_t
    102   5331       amw  * First lr_attr_t is initialized.  following the lr_attr_t
    103   5331       amw  * is the mapsize and attribute bitmap copied from the xvattr_t.
    104   5331       amw  * Following the bitmap and bitmapsize two 64 bit words are reserved
    105   5331       amw  * for the create time which may be set.  Following the create time
    106   5331       amw  * records a single 64 bit integer which has the bits to set on
    107   5331       amw  * replay for the xvattr.
    108   5331       amw  */
    109   5331       amw static void
    110   5331       amw zfs_log_xvattr(lr_attr_t *lrattr, xvattr_t *xvap)
    111   5331       amw {
    112   5331       amw 	uint32_t	*bitmap;
    113   5331       amw 	uint64_t	*attrs;
    114   5331       amw 	uint64_t	*crtime;
    115   5331       amw 	xoptattr_t	*xoap;
    116   5331       amw 	void		*scanstamp;
    117   5331       amw 	int		i;
    118   5331       amw 
    119   5331       amw 	xoap = xva_getxoptattr(xvap);
    120   5331       amw 	ASSERT(xoap);
    121   5331       amw 
    122   5331       amw 	lrattr->lr_attr_masksize = xvap->xva_mapsize;
    123   5331       amw 	bitmap = &lrattr->lr_attr_bitmap;
    124   5331       amw 	for (i = 0; i != xvap->xva_mapsize; i++, bitmap++) {
    125   5331       amw 		*bitmap = xvap->xva_reqattrmap[i];
    126   5331       amw 	}
    127   5331       amw 
    128   5331       amw 	/* Now pack the attributes up in a single uint64_t */
    129   5331       amw 	attrs = (uint64_t *)bitmap;
    130   5331       amw 	crtime = attrs + 1;
    131   5331       amw 	scanstamp = (caddr_t)(crtime + 2);
    132   5331       amw 	*attrs = 0;
    133   5331       amw 	if (XVA_ISSET_REQ(xvap, XAT_READONLY))
    134   5331       amw 		*attrs |= (xoap->xoa_readonly == 0) ? 0 :
    135   5331       amw 		    XAT0_READONLY;
    136   5331       amw 	if (XVA_ISSET_REQ(xvap, XAT_HIDDEN))
    137   5331       amw 		*attrs |= (xoap->xoa_hidden == 0) ? 0 :
    138   5331       amw 		    XAT0_HIDDEN;
    139   5331       amw 	if (XVA_ISSET_REQ(xvap, XAT_SYSTEM))
    140   5331       amw 		*attrs |= (xoap->xoa_system == 0) ? 0 :
    141   5331       amw 		    XAT0_SYSTEM;
    142   5331       amw 	if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE))
    143   5331       amw 		*attrs |= (xoap->xoa_archive == 0) ? 0 :
    144   5331       amw 		    XAT0_ARCHIVE;
    145   5331       amw 	if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE))
    146   5331       amw 		*attrs |= (xoap->xoa_immutable == 0) ? 0 :
    147   5331       amw 		    XAT0_IMMUTABLE;
    148   5331       amw 	if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK))
    149   5331       amw 		*attrs |= (xoap->xoa_nounlink == 0) ? 0 :
    150   5331       amw 		    XAT0_NOUNLINK;
    151   5331       amw 	if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY))
    152   5331       amw 		*attrs |= (xoap->xoa_appendonly == 0) ? 0 :
    153   5331       amw 		    XAT0_APPENDONLY;
    154   5331       amw 	if (XVA_ISSET_REQ(xvap, XAT_OPAQUE))
    155   5331       amw 		*attrs |= (xoap->xoa_opaque == 0) ? 0 :
    156   5331       amw 		    XAT0_APPENDONLY;
    157   5331       amw 	if (XVA_ISSET_REQ(xvap, XAT_NODUMP))
    158   5331       amw 		*attrs |= (xoap->xoa_nodump == 0) ? 0 :
    159   5331       amw 		    XAT0_NODUMP;
    160   5331       amw 	if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED))
    161   5331       amw 		*attrs |= (xoap->xoa_av_quarantined == 0) ? 0 :
    162   5331       amw 		    XAT0_AV_QUARANTINED;
    163   5331       amw 	if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED))
    164   5331       amw 		*attrs |= (xoap->xoa_av_modified == 0) ? 0 :
    165   5331       amw 		    XAT0_AV_MODIFIED;
    166   5331       amw 	if (XVA_ISSET_REQ(xvap, XAT_CREATETIME))
    167   5331       amw 		ZFS_TIME_ENCODE(&xoap->xoa_createtime, crtime);
    168   5331       amw 	if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
    169   5331       amw 		bcopy(xoap->xoa_av_scanstamp, scanstamp, AV_SCANSTAMP_SZ);
    170  10793       dai 	if (XVA_ISSET_REQ(xvap, XAT_REPARSE))
    171  10793       dai 		*attrs |= (xoap->xoa_reparse == 0) ? 0 :
    172  10793       dai 		    XAT0_REPARSE;
    173   5331       amw }
    174   5331       amw 
    175   5331       amw static void *
    176   5331       amw zfs_log_fuid_ids(zfs_fuid_info_t *fuidp, void *start)
    177   5331       amw {
    178   5331       amw 	zfs_fuid_t *zfuid;
    179   5331       amw 	uint64_t *fuidloc = start;
    180   5331       amw 
    181   5331       amw 	/* First copy in the ACE FUIDs */
    182   5331       amw 	for (zfuid = list_head(&fuidp->z_fuids); zfuid;
    183   5331       amw 	    zfuid = list_next(&fuidp->z_fuids, zfuid)) {
    184   5331       amw 		*fuidloc++ = zfuid->z_logfuid;
    185   5331       amw 	}
    186   5331       amw 	return (fuidloc);
    187   5331       amw }
    188   5331       amw 
    189   5331       amw 
    190   5331       amw static void *
    191   5331       amw zfs_log_fuid_domains(zfs_fuid_info_t *fuidp, void *start)
    192   5331       amw {
    193   5331       amw 	zfs_fuid_domain_t *zdomain;
    194   5331       amw 
    195   5331       amw 	/* now copy in the domain info, if any */
    196   5331       amw 	if (fuidp->z_domain_str_sz != 0) {
    197   5331       amw 		for (zdomain = list_head(&fuidp->z_domains); zdomain;
    198   5331       amw 		    zdomain = list_next(&fuidp->z_domains, zdomain)) {
    199   5331       amw 			bcopy((void *)zdomain->z_domain, start,
    200   5331       amw 			    strlen(zdomain->z_domain) + 1);
    201   5331       amw 			start = (caddr_t)start +
    202   5331       amw 			    strlen(zdomain->z_domain) + 1;
    203   5331       amw 		}
    204   5331       amw 	}
    205   5331       amw 	return (start);
    206   5331       amw }
    207   5331       amw 
    208   5331       amw /*
    209   5331       amw  * zfs_log_create() is used to handle TX_CREATE, TX_CREATE_ATTR, TX_MKDIR,
    210   5331       amw  * TX_MKDIR_ATTR and TX_MKXATTR
    211    789    ahrens  * transactions.
    212   5331       amw  *
    213   5331       amw  * TX_CREATE and TX_MKDIR are standard creates, but they may have FUID
    214   5331       amw  * domain information appended prior to the name.  In this case the
    215   5331       amw  * uid/gid in the log record will be a log centric FUID.
    216   5331       amw  *
    217   5331       amw  * TX_CREATE_ACL_ATTR and TX_MKDIR_ACL_ATTR handle special creates that
    218   5331       amw  * may contain attributes, ACL and optional fuid information.
    219   5331       amw  *
    220   5331       amw  * TX_CREATE_ACL and TX_MKDIR_ACL handle special creates that specify
    221   5331       amw  * and ACL and normal users/groups in the ACEs.
    222   5331       amw  *
    223   5331       amw  * There may be an optional xvattr attribute information similar
    224   5331       amw  * to zfs_log_setattr.
    225   5331       amw  *
    226   5331       amw  * Also, after the file name "domain" strings may be appended.
    227    789    ahrens  */
    228   2638    perrin void
    229   5331       amw zfs_log_create(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
    230   5331       amw     znode_t *dzp, znode_t *zp, char *name, vsecattr_t *vsecp,
    231   5331       amw     zfs_fuid_info_t *fuidp, vattr_t *vap)
    232    789    ahrens {
    233    789    ahrens 	itx_t *itx;
    234    789    ahrens 	uint64_t seq;
    235    789    ahrens 	lr_create_t *lr;
    236   5331       amw 	lr_acl_create_t *lracl;
    237   5331       amw 	size_t aclsize;
    238   5331       amw 	size_t xvatsize = 0;
    239   5331       amw 	size_t txsize;
    240   5331       amw 	xvattr_t *xvap = (xvattr_t *)vap;
    241   5331       amw 	void *end;
    242   5331       amw 	size_t lrsize;
    243    789    ahrens 	size_t namesize = strlen(name) + 1;
    244   5331       amw 	size_t fuidsz = 0;
    245    789    ahrens 
    246  10922      Jeff 	if (zil_replaying(zilog, tx))
    247   2638    perrin 		return;
    248   8227      Neil 
    249   5331       amw 	/*
    250   5331       amw 	 * If we have FUIDs present then add in space for
    251   5331       amw 	 * domains and ACE fuid's if any.
    252   5331       amw 	 */
    253   5331       amw 	if (fuidp) {
    254   5331       amw 		fuidsz += fuidp->z_domain_str_sz;
    255   5331       amw 		fuidsz += fuidp->z_fuid_cnt * sizeof (uint64_t);
    256   5331       amw 	}
    257   5331       amw 
    258   5331       amw 	if (vap->va_mask & AT_XVATTR)
    259   5331       amw 		xvatsize = ZIL_XVAT_SIZE(xvap->xva_mapsize);
    260   5331       amw 
    261   5331       amw 	if ((int)txtype == TX_CREATE_ATTR || (int)txtype == TX_MKDIR_ATTR ||
    262   5331       amw 	    (int)txtype == TX_CREATE || (int)txtype == TX_MKDIR ||
    263   5331       amw 	    (int)txtype == TX_MKXATTR) {
    264   5331       amw 		txsize = sizeof (*lr) + namesize + fuidsz + xvatsize;
    265   5331       amw 		lrsize = sizeof (*lr);
    266   5331       amw 	} else {
    267   5331       amw 		aclsize = (vsecp) ? vsecp->vsa_aclentsz : 0;
    268   5331       amw 		txsize =
    269   5331       amw 		    sizeof (lr_acl_create_t) + namesize + fuidsz +
    270   5435     marks 		    ZIL_ACE_LENGTH(aclsize) + xvatsize;
    271   5331       amw 		lrsize = sizeof (lr_acl_create_t);
    272   5331       amw 	}
    273   5331       amw 
    274   5331       amw 	itx = zil_itx_create(txtype, txsize);
    275   5331       amw 
    276    789    ahrens 	lr = (lr_create_t *)&itx->itx_lr;
    277    789    ahrens 	lr->lr_doid = dzp->z_id;
    278    789    ahrens 	lr->lr_foid = zp->z_id;
    279    789    ahrens 	lr->lr_mode = zp->z_phys->zp_mode;
    280   5331       amw 	if (!IS_EPHEMERAL(zp->z_phys->zp_uid)) {
    281   5331       amw 		lr->lr_uid = (uint64_t)zp->z_phys->zp_uid;
    282   5331       amw 	} else {
    283   5331       amw 		lr->lr_uid = fuidp->z_fuid_owner;
    284   5331       amw 	}
    285   5331       amw 	if (!IS_EPHEMERAL(zp->z_phys->zp_gid)) {
    286   5331       amw 		lr->lr_gid = (uint64_t)zp->z_phys->zp_gid;
    287   5331       amw 	} else {
    288   5331       amw 		lr->lr_gid = fuidp->z_fuid_group;
    289   5331       amw 	}
    290    789    ahrens 	lr->lr_gen = zp->z_phys->zp_gen;
    291    789    ahrens 	lr->lr_crtime[0] = zp->z_phys->zp_crtime[0];
    292    789    ahrens 	lr->lr_crtime[1] = zp->z_phys->zp_crtime[1];
    293    789    ahrens 	lr->lr_rdev = zp->z_phys->zp_rdev;
    294   5331       amw 
    295   5331       amw 	/*
    296   5331       amw 	 * Fill in xvattr info if any
    297   5331       amw 	 */
    298   5331       amw 	if (vap->va_mask & AT_XVATTR) {
    299   5331       amw 		zfs_log_xvattr((lr_attr_t *)((caddr_t)lr + lrsize), xvap);
    300   5331       amw 		end = (caddr_t)lr + lrsize + xvatsize;
    301   5331       amw 	} else {
    302   5331       amw 		end = (caddr_t)lr + lrsize;
    303   5331       amw 	}
    304   5331       amw 
    305   5331       amw 	/* Now fill in any ACL info */
    306   5331       amw 
    307   5331       amw 	if (vsecp) {
    308   5331       amw 		lracl = (lr_acl_create_t *)&itx->itx_lr;
    309   5331       amw 		lracl->lr_aclcnt = vsecp->vsa_aclcnt;
    310   5331       amw 		lracl->lr_acl_bytes = aclsize;
    311   5331       amw 		lracl->lr_domcnt = fuidp ? fuidp->z_domain_cnt : 0;
    312   5331       amw 		lracl->lr_fuidcnt  = fuidp ? fuidp->z_fuid_cnt : 0;
    313   5331       amw 		if (vsecp->vsa_aclflags & VSA_ACE_ACLFLAGS)
    314   5331       amw 			lracl->lr_acl_flags = (uint64_t)vsecp->vsa_aclflags;
    315   5331       amw 		else
    316   5331       amw 			lracl->lr_acl_flags = 0;
    317   5331       amw 
    318   5331       amw 		bcopy(vsecp->vsa_aclentp, end, aclsize);
    319   5435     marks 		end = (caddr_t)end + ZIL_ACE_LENGTH(aclsize);
    320   5331       amw 	}
    321   5331       amw 
    322   5331       amw 	/* drop in FUID info */
    323   5331       amw 	if (fuidp) {
    324   5331       amw 		end = zfs_log_fuid_ids(fuidp, end);
    325   5331       amw 		end = zfs_log_fuid_domains(fuidp, end);
    326   5331       amw 	}
    327   5331       amw 	/*
    328   5331       amw 	 * Now place file name in log record
    329   5331       amw 	 */
    330   5331       amw 	bcopy(name, end, namesize);
    331    789    ahrens 
    332    789    ahrens 	seq = zil_itx_assign(zilog, itx, tx);
    333    789    ahrens 	dzp->z_last_itx = seq;
    334    789    ahrens 	zp->z_last_itx = seq;
    335    789    ahrens }
    336    789    ahrens 
    337    789    ahrens /*
    338    789    ahrens  * zfs_log_remove() handles both TX_REMOVE and TX_RMDIR transactions.
    339    789    ahrens  */
    340   2638    perrin void
    341   5331       amw zfs_log_remove(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
    342    789    ahrens 	znode_t *dzp, char *name)
    343    789    ahrens {
    344    789    ahrens 	itx_t *itx;
    345    789    ahrens 	uint64_t seq;
    346    789    ahrens 	lr_remove_t *lr;
    347    789    ahrens 	size_t namesize = strlen(name) + 1;
    348    789    ahrens 
    349  10922      Jeff 	if (zil_replaying(zilog, tx))
    350   2638    perrin 		return;
    351   8227      Neil 
    352    789    ahrens 	itx = zil_itx_create(txtype, sizeof (*lr) + namesize);
    353    789    ahrens 	lr = (lr_remove_t *)&itx->itx_lr;
    354    789    ahrens 	lr->lr_doid = dzp->z_id;
    355    789    ahrens 	bcopy(name, (char *)(lr + 1), namesize);
    356    789    ahrens 
    357    789    ahrens 	seq = zil_itx_assign(zilog, itx, tx);
    358    789    ahrens 	dzp->z_last_itx = seq;
    359    789    ahrens }
    360    789    ahrens 
    361    789    ahrens /*
    362    789    ahrens  * zfs_log_link() handles TX_LINK transactions.
    363    789    ahrens  */
    364   2638    perrin void
    365   5331       amw zfs_log_link(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
    366    789    ahrens 	znode_t *dzp, znode_t *zp, char *name)
    367    789    ahrens {
    368    789    ahrens 	itx_t *itx;
    369    789    ahrens 	uint64_t seq;
    370    789    ahrens 	lr_link_t *lr;
    371    789    ahrens 	size_t namesize = strlen(name) + 1;
    372    789    ahrens 
    373  10922      Jeff 	if (zil_replaying(zilog, tx))
    374   2638    perrin 		return;
    375    789    ahrens 
    376    789    ahrens 	itx = zil_itx_create(txtype, sizeof (*lr) + namesize);
    377    789    ahrens 	lr = (lr_link_t *)&itx->itx_lr;
    378    789    ahrens 	lr->lr_doid = dzp->z_id;
    379    789    ahrens 	lr->lr_link_obj = zp->z_id;
    380    789    ahrens 	bcopy(name, (char *)(lr + 1), namesize);
    381    789    ahrens 
    382    789    ahrens 	seq = zil_itx_assign(zilog, itx, tx);
    383    789    ahrens 	dzp->z_last_itx = seq;
    384    789    ahrens 	zp->z_last_itx = seq;
    385    789    ahrens }
    386    789    ahrens 
    387    789    ahrens /*
    388    789    ahrens  * zfs_log_symlink() handles TX_SYMLINK transactions.
    389    789    ahrens  */
    390   2638    perrin void
    391   5331       amw zfs_log_symlink(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
    392   5331       amw     znode_t *dzp, znode_t *zp, char *name, char *link)
    393    789    ahrens {
    394    789    ahrens 	itx_t *itx;
    395    789    ahrens 	uint64_t seq;
    396    789    ahrens 	lr_create_t *lr;
    397    789    ahrens 	size_t namesize = strlen(name) + 1;
    398    789    ahrens 	size_t linksize = strlen(link) + 1;
    399    789    ahrens 
    400  10922      Jeff 	if (zil_replaying(zilog, tx))
    401   2638    perrin 		return;
    402    789    ahrens 
    403    789    ahrens 	itx = zil_itx_create(txtype, sizeof (*lr) + namesize + linksize);
    404    789    ahrens 	lr = (lr_create_t *)&itx->itx_lr;
    405    789    ahrens 	lr->lr_doid = dzp->z_id;
    406    789    ahrens 	lr->lr_foid = zp->z_id;
    407    789    ahrens 	lr->lr_mode = zp->z_phys->zp_mode;
    408    789    ahrens 	lr->lr_uid = zp->z_phys->zp_uid;
    409    789    ahrens 	lr->lr_gid = zp->z_phys->zp_gid;
    410    789    ahrens 	lr->lr_gen = zp->z_phys->zp_gen;
    411    789    ahrens 	lr->lr_crtime[0] = zp->z_phys->zp_crtime[0];
    412    789    ahrens 	lr->lr_crtime[1] = zp->z_phys->zp_crtime[1];
    413    789    ahrens 	bcopy(name, (char *)(lr + 1), namesize);
    414    789    ahrens 	bcopy(link, (char *)(lr + 1) + namesize, linksize);
    415    789    ahrens 
    416    789    ahrens 	seq = zil_itx_assign(zilog, itx, tx);
    417    789    ahrens 	dzp->z_last_itx = seq;
    418    789    ahrens 	zp->z_last_itx = seq;
    419    789    ahrens }
    420    789    ahrens 
    421    789    ahrens /*
    422    789    ahrens  * zfs_log_rename() handles TX_RENAME transactions.
    423    789    ahrens  */
    424   2638    perrin void
    425   5331       amw zfs_log_rename(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
    426    789    ahrens 	znode_t *sdzp, char *sname, znode_t *tdzp, char *dname, znode_t *szp)
    427    789    ahrens {
    428    789    ahrens 	itx_t *itx;
    429    789    ahrens 	uint64_t seq;
    430    789    ahrens 	lr_rename_t *lr;
    431    789    ahrens 	size_t snamesize = strlen(sname) + 1;
    432    789    ahrens 	size_t dnamesize = strlen(dname) + 1;
    433    789    ahrens 
    434  10922      Jeff 	if (zil_replaying(zilog, tx))
    435   2638    perrin 		return;
    436   8227      Neil 
    437    789    ahrens 	itx = zil_itx_create(txtype, sizeof (*lr) + snamesize + dnamesize);
    438    789    ahrens 	lr = (lr_rename_t *)&itx->itx_lr;
    439    789    ahrens 	lr->lr_sdoid = sdzp->z_id;
    440    789    ahrens 	lr->lr_tdoid = tdzp->z_id;
    441    789    ahrens 	bcopy(sname, (char *)(lr + 1), snamesize);
    442    789    ahrens 	bcopy(dname, (char *)(lr + 1) + snamesize, dnamesize);
    443    789    ahrens 
    444    789    ahrens 	seq = zil_itx_assign(zilog, itx, tx);
    445    789    ahrens 	sdzp->z_last_itx = seq;
    446    789    ahrens 	tdzp->z_last_itx = seq;
    447    789    ahrens 	szp->z_last_itx = seq;
    448    789    ahrens }
    449    789    ahrens 
    450    789    ahrens /*
    451    789    ahrens  * zfs_log_write() handles TX_WRITE transactions.
    452    789    ahrens  */
    453   2237    maybee ssize_t zfs_immediate_write_sz = 32768;
    454    789    ahrens 
    455   2638    perrin void
    456    789    ahrens zfs_log_write(zilog_t *zilog, dmu_tx_t *tx, int txtype,
    457   4620    perrin 	znode_t *zp, offset_t off, ssize_t resid, int ioflag)
    458    789    ahrens {
    459   1669    perrin 	itx_wr_state_t write_state;
    460   4620    perrin 	boolean_t slogging;
    461   4720  fr157268 	uintptr_t fsync_cnt;
    462  10310      Neil 	ssize_t immediate_write_sz;
    463    789    ahrens 
    464  10922      Jeff 	if (zil_replaying(zilog, tx) || zp->z_unlinked)
    465   2638    perrin 		return;
    466    789    ahrens 
    467  10310      Neil 	immediate_write_sz = (zilog->zl_logbias == ZFS_LOGBIAS_THROUGHPUT)
    468  10310      Neil 	    ? 0 : zfs_immediate_write_sz;
    469  10310      Neil 
    470  10310      Neil 	slogging = spa_has_slogs(zilog->zl_spa) &&
    471  10310      Neil 	    (zilog->zl_logbias == ZFS_LOGBIAS_LATENCY);
    472  10310      Neil 	if (resid > immediate_write_sz && !slogging && resid <= zp->z_blksz)
    473   1669    perrin 		write_state = WR_INDIRECT;
    474   6396    perrin 	else if (ioflag & (FSYNC | FDSYNC))
    475   1669    perrin 		write_state = WR_COPIED;
    476   3638     billm 	else
    477   1669    perrin 		write_state = WR_NEED_COPY;
    478   4720  fr157268 
    479   4720  fr157268 	if ((fsync_cnt = (uintptr_t)tsd_get(zfs_fsyncer_key)) != 0) {
    480   4720  fr157268 		(void) tsd_set(zfs_fsyncer_key, (void *)(fsync_cnt - 1));
    481   4720  fr157268 	}
    482   3638     billm 
    483   4620    perrin 	while (resid) {
    484   4620    perrin 		itx_t *itx;
    485   4620    perrin 		lr_write_t *lr;
    486   4620    perrin 		ssize_t len;
    487   4620    perrin 
    488   4620    perrin 		/*
    489   7360      Neil 		 * If the write would overflow the largest block then split it.
    490   4620    perrin 		 */
    491   7360      Neil 		if (write_state != WR_INDIRECT && resid > ZIL_MAX_LOG_DATA)
    492   4620    perrin 			len = SPA_MAXBLOCKSIZE >> 1;
    493   4620    perrin 		else
    494   4620    perrin 			len = resid;
    495   4620    perrin 
    496   4620    perrin 		itx = zil_itx_create(txtype, sizeof (*lr) +
    497   4620    perrin 		    (write_state == WR_COPIED ? len : 0));
    498   4620    perrin 		lr = (lr_write_t *)&itx->itx_lr;
    499   4620    perrin 		if (write_state == WR_COPIED && dmu_read(zp->z_zfsvfs->z_os,
    500   9512      Neil 		    zp->z_id, off, len, lr + 1, DMU_READ_NO_PREFETCH) != 0) {
    501  10922      Jeff 			zil_itx_destroy(itx);
    502   1669    perrin 			itx = zil_itx_create(txtype, sizeof (*lr));
    503   3638     billm 			lr = (lr_write_t *)&itx->itx_lr;
    504   1669    perrin 			write_state = WR_NEED_COPY;
    505   1669    perrin 		}
    506   4620    perrin 
    507   4620    perrin 		itx->itx_wr_state = write_state;
    508   6101    perrin 		if (write_state == WR_NEED_COPY)
    509   6101    perrin 			itx->itx_sod += len;
    510   4620    perrin 		lr->lr_foid = zp->z_id;
    511   4620    perrin 		lr->lr_offset = off;
    512   4620    perrin 		lr->lr_length = len;
    513   4620    perrin 		lr->lr_blkoff = 0;
    514   4620    perrin 		BP_ZERO(&lr->lr_blkptr);
    515   4620    perrin 
    516   4620    perrin 		itx->itx_private = zp->z_zfsvfs;
    517   4620    perrin 
    518   6396    perrin 		if ((zp->z_sync_cnt != 0) || (fsync_cnt != 0) ||
    519   6396    perrin 		    (ioflag & (FSYNC | FDSYNC)))
    520   4720  fr157268 			itx->itx_sync = B_TRUE;
    521   4720  fr157268 		else
    522   4720  fr157268 			itx->itx_sync = B_FALSE;
    523   4720  fr157268 
    524   4620    perrin 		zp->z_last_itx = zil_itx_assign(zilog, itx, tx);
    525   4620    perrin 
    526   4620    perrin 		off += len;
    527   4620    perrin 		resid -= len;
    528    789    ahrens 	}
    529    789    ahrens }
    530    789    ahrens 
    531    789    ahrens /*
    532    789    ahrens  * zfs_log_truncate() handles TX_TRUNCATE transactions.
    533    789    ahrens  */
    534   2638    perrin void
    535    789    ahrens zfs_log_truncate(zilog_t *zilog, dmu_tx_t *tx, int txtype,
    536    789    ahrens 	znode_t *zp, uint64_t off, uint64_t len)
    537    789    ahrens {
    538    789    ahrens 	itx_t *itx;
    539    789    ahrens 	uint64_t seq;
    540    789    ahrens 	lr_truncate_t *lr;
    541    789    ahrens 
    542  10922      Jeff 	if (zil_replaying(zilog, tx) || zp->z_unlinked)
    543   2638    perrin 		return;
    544   8227      Neil 
    545    789    ahrens 	itx = zil_itx_create(txtype, sizeof (*lr));
    546    789    ahrens 	lr = (lr_truncate_t *)&itx->itx_lr;
    547    789    ahrens 	lr->lr_foid = zp->z_id;
    548    789    ahrens 	lr->lr_offset = off;
    549    789    ahrens 	lr->lr_length = len;
    550    789    ahrens 
    551   3063    perrin 	itx->itx_sync = (zp->z_sync_cnt != 0);
    552    789    ahrens 	seq = zil_itx_assign(zilog, itx, tx);
    553    789    ahrens 	zp->z_last_itx = seq;
    554    789    ahrens }
    555    789    ahrens 
    556    789    ahrens /*
    557    789    ahrens  * zfs_log_setattr() handles TX_SETATTR transactions.
    558    789    ahrens  */
    559   2638    perrin void
    560    789    ahrens zfs_log_setattr(zilog_t *zilog, dmu_tx_t *tx, int txtype,
    561   5331       amw 	znode_t *zp, vattr_t *vap, uint_t mask_applied, zfs_fuid_info_t *fuidp)
    562    789    ahrens {
    563   5331       amw 	itx_t		*itx;
    564   5331       amw 	uint64_t	seq;
    565   5331       amw 	lr_setattr_t	*lr;
    566   5331       amw 	xvattr_t	*xvap = (xvattr_t *)vap;
    567   5331       amw 	size_t		recsize = sizeof (lr_setattr_t);
    568   5331       amw 	void		*start;
    569   5331       amw 
    570  10922      Jeff 	if (zil_replaying(zilog, tx) || zp->z_unlinked)
    571   2638    perrin 		return;
    572    789    ahrens 
    573   5331       amw 	/*
    574   5331       amw 	 * If XVATTR set, then log record size needs to allow
    575   5331       amw 	 * for lr_attr_t + xvattr mask, mapsize and create time
    576   5331       amw 	 * plus actual attribute values
    577   5331       amw 	 */
    578   5331       amw 	if (vap->va_mask & AT_XVATTR)
    579   5331       amw 		recsize = sizeof (*lr) + ZIL_XVAT_SIZE(xvap->xva_mapsize);
    580   5331       amw 
    581   5331       amw 	if (fuidp)
    582   5331       amw 		recsize += fuidp->z_domain_str_sz;
    583   5331       amw 
    584   5331       amw 	itx = zil_itx_create(txtype, recsize);
    585    789    ahrens 	lr = (lr_setattr_t *)&itx->itx_lr;
    586    789    ahrens 	lr->lr_foid = zp->z_id;
    587    789    ahrens 	lr->lr_mask = (uint64_t)mask_applied;
    588    789    ahrens 	lr->lr_mode = (uint64_t)vap->va_mode;
    589   5331       amw 	if ((mask_applied & AT_UID) && IS_EPHEMERAL(vap->va_uid))
    590   5331       amw 		lr->lr_uid = fuidp->z_fuid_owner;
    591   5331       amw 	else
    592   5331       amw 		lr->lr_uid = (uint64_t)vap->va_uid;
    593   5331       amw 
    594   5331       amw 	if ((mask_applied & AT_GID) && IS_EPHEMERAL(vap->va_gid))
    595   5331       amw 		lr->lr_gid = fuidp->z_fuid_group;
    596   5331       amw 	else
    597   5331       amw 		lr->lr_gid = (uint64_t)vap->va_gid;
    598   5331       amw 
    599    789    ahrens 	lr->lr_size = (uint64_t)vap->va_size;
    600    789    ahrens 	ZFS_TIME_ENCODE(&vap->va_atime, lr->lr_atime);
    601    789    ahrens 	ZFS_TIME_ENCODE(&vap->va_mtime, lr->lr_mtime);
    602   5331       amw 	start = (lr_setattr_t *)(lr + 1);
    603   5331       amw 	if (vap->va_mask & AT_XVATTR) {
    604   5331       amw 		zfs_log_xvattr((lr_attr_t *)start, xvap);
    605   5331       amw 		start = (caddr_t)start + ZIL_XVAT_SIZE(xvap->xva_mapsize);
    606   5331       amw 	}
    607   5331       amw 
    608   5331       amw 	/*
    609   5331       amw 	 * Now stick on domain information if any on end
    610   5331       amw 	 */
    611   5331       amw 
    612   5331       amw 	if (fuidp)
    613   5331       amw 		(void) zfs_log_fuid_domains(fuidp, start);
    614    789    ahrens 
    615   3063    perrin 	itx->itx_sync = (zp->z_sync_cnt != 0);
    616    789    ahrens 	seq = zil_itx_assign(zilog, itx, tx);
    617    789    ahrens 	zp->z_last_itx = seq;
    618    789    ahrens }
    619    789    ahrens 
    620    789    ahrens /*
    621    789    ahrens  * zfs_log_acl() handles TX_ACL transactions.
    622    789    ahrens  */
    623   2638    perrin void
    624   5331       amw zfs_log_acl(zilog_t *zilog, dmu_tx_t *tx, znode_t *zp,
    625   5331       amw     vsecattr_t *vsecp, zfs_fuid_info_t *fuidp)
    626    789    ahrens {
    627    789    ahrens 	itx_t *itx;
    628    789    ahrens 	uint64_t seq;
    629   5331       amw 	lr_acl_v0_t *lrv0;
    630    789    ahrens 	lr_acl_t *lr;
    631   5331       amw 	int txtype;
    632   5331       amw 	int lrsize;
    633   5331       amw 	size_t txsize;
    634   5331       amw 	size_t aclbytes = vsecp->vsa_aclentsz;
    635   5331       amw 
    636  10922      Jeff 	if (zil_replaying(zilog, tx) || zp->z_unlinked)
    637   6514     marks 		return;
    638   8227      Neil 
    639   6514     marks 	txtype = (zp->z_zfsvfs->z_version < ZPL_VERSION_FUID) ?
    640   5331       amw 	    TX_ACL_V0 : TX_ACL;
    641   5331       amw 
    642   5331       amw 	if (txtype == TX_ACL)
    643   5331       amw 		lrsize = sizeof (*lr);
    644   5331       amw 	else
    645   5331       amw 		lrsize = sizeof (*lrv0);
    646    789    ahrens 
    647   5435     marks 	txsize = lrsize +
    648   5435     marks 	    ((txtype == TX_ACL) ? ZIL_ACE_LENGTH(aclbytes) : aclbytes) +
    649   5435     marks 	    (fuidp ? fuidp->z_domain_str_sz : 0) +
    650   6514     marks 	    sizeof (uint64_t) * (fuidp ? fuidp->z_fuid_cnt : 0);
    651   5331       amw 
    652   5331       amw 	itx = zil_itx_create(txtype, txsize);
    653   5331       amw 
    654    789    ahrens 	lr = (lr_acl_t *)&itx->itx_lr;
    655    789    ahrens 	lr->lr_foid = zp->z_id;
    656   5331       amw 	if (txtype == TX_ACL) {
    657   5331       amw 		lr->lr_acl_bytes = aclbytes;
    658   5331       amw 		lr->lr_domcnt = fuidp ? fuidp->z_domain_cnt : 0;
    659   5331       amw 		lr->lr_fuidcnt = fuidp ? fuidp->z_fuid_cnt : 0;
    660   5331       amw 		if (vsecp->vsa_mask & VSA_ACE_ACLFLAGS)
    661   5331       amw 			lr->lr_acl_flags = (uint64_t)vsecp->vsa_aclflags;
    662   5331       amw 		else
    663   5331       amw 			lr->lr_acl_flags = 0;
    664   5331       amw 	}
    665   5331       amw 	lr->lr_aclcnt = (uint64_t)vsecp->vsa_aclcnt;
    666   5331       amw 
    667   5331       amw 	if (txtype == TX_ACL_V0) {
    668   5331       amw 		lrv0 = (lr_acl_v0_t *)lr;
    669   5331       amw 		bcopy(vsecp->vsa_aclentp, (ace_t *)(lrv0 + 1), aclbytes);
    670   5331       amw 	} else {
    671   5331       amw 		void *start = (ace_t *)(lr + 1);
    672   5331       amw 
    673   5331       amw 		bcopy(vsecp->vsa_aclentp, start, aclbytes);
    674   5331       amw 
    675   5435     marks 		start = (caddr_t)start + ZIL_ACE_LENGTH(aclbytes);
    676   5331       amw 
    677   5331       amw 		if (fuidp) {
    678   5331       amw 			start = zfs_log_fuid_ids(fuidp, start);
    679   5331       amw 			(void) zfs_log_fuid_domains(fuidp, start);
    680   5331       amw 		}
    681   5331       amw 	}
    682    789    ahrens 
    683   3063    perrin 	itx->itx_sync = (zp->z_sync_cnt != 0);
    684    789    ahrens 	seq = zil_itx_assign(zilog, itx, tx);
    685    789    ahrens 	zp->z_last_itx = seq;
    686    789    ahrens }
    687