zfs_log.c revision 209962
1168404Spjd/*
2168404Spjd * CDDL HEADER START
3168404Spjd *
4168404Spjd * The contents of this file are subject to the terms of the
5168404Spjd * Common Development and Distribution License (the "License").
6168404Spjd * You may not use this file except in compliance with the License.
7168404Spjd *
8168404Spjd * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9168404Spjd * or http://www.opensolaris.org/os/licensing.
10168404Spjd * See the License for the specific language governing permissions
11168404Spjd * and limitations under the License.
12168404Spjd *
13168404Spjd * When distributing Covered Code, include this CDDL HEADER in each
14168404Spjd * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15168404Spjd * If applicable, add the following below this CDDL HEADER, with the
16168404Spjd * fields enclosed by brackets "[]" replaced with your own identifying
17168404Spjd * information: Portions Copyright [yyyy] [name of copyright owner]
18168404Spjd *
19168404Spjd * CDDL HEADER END
20168404Spjd */
21168404Spjd/*
22185029Spjd * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23168404Spjd * Use is subject to license terms.
24168404Spjd */
25168404Spjd
26168404Spjd#include <sys/types.h>
27168404Spjd#include <sys/param.h>
28168404Spjd#include <sys/systm.h>
29168404Spjd#include <sys/sysmacros.h>
30168404Spjd#include <sys/cmn_err.h>
31168404Spjd#include <sys/kmem.h>
32168404Spjd#include <sys/file.h>
33168404Spjd#include <sys/vfs.h>
34168404Spjd#include <sys/zfs_znode.h>
35168404Spjd#include <sys/zfs_dir.h>
36168404Spjd#include <sys/zil.h>
37185029Spjd#include <sys/zil_impl.h>
38168404Spjd#include <sys/byteorder.h>
39168962Spjd#include <sys/policy.h>
40168404Spjd#include <sys/stat.h>
41168404Spjd#include <sys/acl.h>
42168404Spjd#include <sys/dmu.h>
43168404Spjd#include <sys/spa.h>
44185029Spjd#include <sys/zfs_fuid.h>
45209962Smm#include <sys/dsl_dataset.h>
46168404Spjd
47209962Smm#define	ZFS_HANDLE_REPLAY(zilog, tx) \
48209962Smm	if (zilog->zl_replay) { \
49209962Smm		dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); \
50209962Smm		zilog->zl_replayed_seq[dmu_tx_get_txg(tx) & TXG_MASK] = \
51209962Smm		    zilog->zl_replaying_seq; \
52209962Smm		return; \
53209962Smm	}
54209962Smm
55168404Spjd/*
56209962Smm * These zfs_log_* functions must be called within a dmu tx, in one
57209962Smm * of 2 contexts depending on zilog->z_replay:
58209962Smm *
59209962Smm * Non replay mode
60209962Smm * ---------------
61209962Smm * We need to record the transaction so that if it is committed to
62209962Smm * the Intent Log then it can be replayed.  An intent log transaction
63209962Smm * structure (itx_t) is allocated and all the information necessary to
64209962Smm * possibly replay the transaction is saved in it. The itx is then assigned
65209962Smm * a sequence number and inserted in the in-memory list anchored in the zilog.
66209962Smm *
67209962Smm * Replay mode
68209962Smm * -----------
69209962Smm * We need to mark the intent log record as replayed in the log header.
70209962Smm * This is done in the same transaction as the replay so that they
71209962Smm * commit atomically.
72168404Spjd */
73168404Spjd
74185029Spjdint
75185029Spjdzfs_log_create_txtype(zil_create_t type, vsecattr_t *vsecp, vattr_t *vap)
76185029Spjd{
77185029Spjd	int isxvattr = (vap->va_mask & AT_XVATTR);
78185029Spjd	switch (type) {
79185029Spjd	case Z_FILE:
80185029Spjd		if (vsecp == NULL && !isxvattr)
81185029Spjd			return (TX_CREATE);
82185029Spjd		if (vsecp && isxvattr)
83185321Strasz#ifdef TODO
84185029Spjd			return (TX_CREATE_ACL_ATTR);
85185321Strasz#else
86185321Strasz			panic("%s:%u: unsupported condition", __func__, __LINE__);
87185321Strasz#endif
88185029Spjd		if (vsecp)
89185029Spjd			return (TX_CREATE_ACL);
90185029Spjd		else
91185029Spjd			return (TX_CREATE_ATTR);
92185029Spjd		/*NOTREACHED*/
93185029Spjd	case Z_DIR:
94185029Spjd		if (vsecp == NULL && !isxvattr)
95185029Spjd			return (TX_MKDIR);
96185029Spjd		if (vsecp && isxvattr)
97185321Strasz#ifdef TODO
98185029Spjd			return (TX_MKDIR_ACL_ATTR);
99185321Strasz#else
100185321Strasz			panic("%s:%u: unsupported condition", __func__, __LINE__);
101185321Strasz#endif
102185029Spjd		if (vsecp)
103185029Spjd			return (TX_MKDIR_ACL);
104185029Spjd		else
105185029Spjd			return (TX_MKDIR_ATTR);
106185029Spjd	case Z_XATTRDIR:
107185029Spjd		return (TX_MKXATTR);
108185029Spjd	}
109185029Spjd	ASSERT(0);
110185029Spjd	return (TX_MAX_TYPE);
111185029Spjd}
112185029Spjd
113168404Spjd/*
114185029Spjd * build up the log data necessary for logging xvattr_t
115185029Spjd * First lr_attr_t is initialized.  following the lr_attr_t
116185029Spjd * is the mapsize and attribute bitmap copied from the xvattr_t.
117185029Spjd * Following the bitmap and bitmapsize two 64 bit words are reserved
118185029Spjd * for the create time which may be set.  Following the create time
119185029Spjd * records a single 64 bit integer which has the bits to set on
120185029Spjd * replay for the xvattr.
121185029Spjd */
122185029Spjdstatic void
123185029Spjdzfs_log_xvattr(lr_attr_t *lrattr, xvattr_t *xvap)
124185029Spjd{
125185029Spjd	uint32_t	*bitmap;
126185029Spjd	uint64_t	*attrs;
127185029Spjd	uint64_t	*crtime;
128185029Spjd	xoptattr_t	*xoap;
129185029Spjd	void		*scanstamp;
130185029Spjd	int		i;
131185029Spjd
132185029Spjd	xoap = xva_getxoptattr(xvap);
133185029Spjd	ASSERT(xoap);
134185029Spjd
135185029Spjd	lrattr->lr_attr_masksize = xvap->xva_mapsize;
136185029Spjd	bitmap = &lrattr->lr_attr_bitmap;
137185029Spjd	for (i = 0; i != xvap->xva_mapsize; i++, bitmap++) {
138185029Spjd		*bitmap = xvap->xva_reqattrmap[i];
139185029Spjd	}
140185029Spjd
141185029Spjd	/* Now pack the attributes up in a single uint64_t */
142185029Spjd	attrs = (uint64_t *)bitmap;
143185029Spjd	crtime = attrs + 1;
144185029Spjd	scanstamp = (caddr_t)(crtime + 2);
145185029Spjd	*attrs = 0;
146185029Spjd	if (XVA_ISSET_REQ(xvap, XAT_READONLY))
147185029Spjd		*attrs |= (xoap->xoa_readonly == 0) ? 0 :
148185029Spjd		    XAT0_READONLY;
149185029Spjd	if (XVA_ISSET_REQ(xvap, XAT_HIDDEN))
150185029Spjd		*attrs |= (xoap->xoa_hidden == 0) ? 0 :
151185029Spjd		    XAT0_HIDDEN;
152185029Spjd	if (XVA_ISSET_REQ(xvap, XAT_SYSTEM))
153185029Spjd		*attrs |= (xoap->xoa_system == 0) ? 0 :
154185029Spjd		    XAT0_SYSTEM;
155185029Spjd	if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE))
156185029Spjd		*attrs |= (xoap->xoa_archive == 0) ? 0 :
157185029Spjd		    XAT0_ARCHIVE;
158185029Spjd	if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE))
159185029Spjd		*attrs |= (xoap->xoa_immutable == 0) ? 0 :
160185029Spjd		    XAT0_IMMUTABLE;
161185029Spjd	if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK))
162185029Spjd		*attrs |= (xoap->xoa_nounlink == 0) ? 0 :
163185029Spjd		    XAT0_NOUNLINK;
164185029Spjd	if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY))
165185029Spjd		*attrs |= (xoap->xoa_appendonly == 0) ? 0 :
166185029Spjd		    XAT0_APPENDONLY;
167185029Spjd	if (XVA_ISSET_REQ(xvap, XAT_OPAQUE))
168185029Spjd		*attrs |= (xoap->xoa_opaque == 0) ? 0 :
169185029Spjd		    XAT0_APPENDONLY;
170185029Spjd	if (XVA_ISSET_REQ(xvap, XAT_NODUMP))
171185029Spjd		*attrs |= (xoap->xoa_nodump == 0) ? 0 :
172185029Spjd		    XAT0_NODUMP;
173185029Spjd	if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED))
174185029Spjd		*attrs |= (xoap->xoa_av_quarantined == 0) ? 0 :
175185029Spjd		    XAT0_AV_QUARANTINED;
176185029Spjd	if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED))
177185029Spjd		*attrs |= (xoap->xoa_av_modified == 0) ? 0 :
178185029Spjd		    XAT0_AV_MODIFIED;
179185029Spjd	if (XVA_ISSET_REQ(xvap, XAT_CREATETIME))
180185029Spjd		ZFS_TIME_ENCODE(&xoap->xoa_createtime, crtime);
181185029Spjd	if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
182185029Spjd		bcopy(xoap->xoa_av_scanstamp, scanstamp, AV_SCANSTAMP_SZ);
183185029Spjd}
184185029Spjd
185185029Spjdstatic void *
186185029Spjdzfs_log_fuid_ids(zfs_fuid_info_t *fuidp, void *start)
187185029Spjd{
188185029Spjd	zfs_fuid_t *zfuid;
189185029Spjd	uint64_t *fuidloc = start;
190185029Spjd
191185029Spjd	/* First copy in the ACE FUIDs */
192185029Spjd	for (zfuid = list_head(&fuidp->z_fuids); zfuid;
193185029Spjd	    zfuid = list_next(&fuidp->z_fuids, zfuid)) {
194185029Spjd		*fuidloc++ = zfuid->z_logfuid;
195185029Spjd	}
196185029Spjd	return (fuidloc);
197185029Spjd}
198185029Spjd
199185029Spjd
200185029Spjdstatic void *
201185029Spjdzfs_log_fuid_domains(zfs_fuid_info_t *fuidp, void *start)
202185029Spjd{
203185029Spjd	zfs_fuid_domain_t *zdomain;
204185029Spjd
205185029Spjd	/* now copy in the domain info, if any */
206185029Spjd	if (fuidp->z_domain_str_sz != 0) {
207185029Spjd		for (zdomain = list_head(&fuidp->z_domains); zdomain;
208185029Spjd		    zdomain = list_next(&fuidp->z_domains, zdomain)) {
209185029Spjd			bcopy((void *)zdomain->z_domain, start,
210185029Spjd			    strlen(zdomain->z_domain) + 1);
211185029Spjd			start = (caddr_t)start +
212185029Spjd			    strlen(zdomain->z_domain) + 1;
213185029Spjd		}
214185029Spjd	}
215185029Spjd	return (start);
216185029Spjd}
217185029Spjd
218185029Spjd/*
219185029Spjd * zfs_log_create() is used to handle TX_CREATE, TX_CREATE_ATTR, TX_MKDIR,
220185029Spjd * TX_MKDIR_ATTR and TX_MKXATTR
221168404Spjd * transactions.
222185029Spjd *
223185029Spjd * TX_CREATE and TX_MKDIR are standard creates, but they may have FUID
224185029Spjd * domain information appended prior to the name.  In this case the
225185029Spjd * uid/gid in the log record will be a log centric FUID.
226185029Spjd *
227185029Spjd * TX_CREATE_ACL_ATTR and TX_MKDIR_ACL_ATTR handle special creates that
228185029Spjd * may contain attributes, ACL and optional fuid information.
229185029Spjd *
230185029Spjd * TX_CREATE_ACL and TX_MKDIR_ACL handle special creates that specify
231185029Spjd * and ACL and normal users/groups in the ACEs.
232185029Spjd *
233185029Spjd * There may be an optional xvattr attribute information similar
234185029Spjd * to zfs_log_setattr.
235185029Spjd *
236185029Spjd * Also, after the file name "domain" strings may be appended.
237168404Spjd */
238168404Spjdvoid
239185029Spjdzfs_log_create(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
240185029Spjd    znode_t *dzp, znode_t *zp, char *name, vsecattr_t *vsecp,
241185029Spjd    zfs_fuid_info_t *fuidp, vattr_t *vap)
242168404Spjd{
243168404Spjd	itx_t *itx;
244168404Spjd	uint64_t seq;
245168404Spjd	lr_create_t *lr;
246185029Spjd	lr_acl_create_t *lracl;
247185029Spjd	size_t aclsize;
248185029Spjd	size_t xvatsize = 0;
249185029Spjd	size_t txsize;
250185029Spjd	xvattr_t *xvap = (xvattr_t *)vap;
251185029Spjd	void *end;
252185029Spjd	size_t lrsize;
253168404Spjd	size_t namesize = strlen(name) + 1;
254185029Spjd	size_t fuidsz = 0;
255168404Spjd
256168404Spjd	if (zilog == NULL)
257168404Spjd		return;
258168404Spjd
259209962Smm	ZFS_HANDLE_REPLAY(zilog, tx); /* exits if replay */
260209962Smm
261185029Spjd	/*
262185029Spjd	 * If we have FUIDs present then add in space for
263185029Spjd	 * domains and ACE fuid's if any.
264185029Spjd	 */
265185029Spjd	if (fuidp) {
266185029Spjd		fuidsz += fuidp->z_domain_str_sz;
267185029Spjd		fuidsz += fuidp->z_fuid_cnt * sizeof (uint64_t);
268185029Spjd	}
269185029Spjd
270185029Spjd	if (vap->va_mask & AT_XVATTR)
271185029Spjd		xvatsize = ZIL_XVAT_SIZE(xvap->xva_mapsize);
272185029Spjd
273185029Spjd	if ((int)txtype == TX_CREATE_ATTR || (int)txtype == TX_MKDIR_ATTR ||
274185029Spjd	    (int)txtype == TX_CREATE || (int)txtype == TX_MKDIR ||
275185029Spjd	    (int)txtype == TX_MKXATTR) {
276185029Spjd		txsize = sizeof (*lr) + namesize + fuidsz + xvatsize;
277185029Spjd		lrsize = sizeof (*lr);
278185029Spjd	} else {
279185029Spjd		aclsize = (vsecp) ? vsecp->vsa_aclentsz : 0;
280185029Spjd		txsize =
281185029Spjd		    sizeof (lr_acl_create_t) + namesize + fuidsz +
282185029Spjd		    ZIL_ACE_LENGTH(aclsize) + xvatsize;
283185029Spjd		lrsize = sizeof (lr_acl_create_t);
284185029Spjd	}
285185029Spjd
286185029Spjd	itx = zil_itx_create(txtype, txsize);
287185029Spjd
288168404Spjd	lr = (lr_create_t *)&itx->itx_lr;
289168404Spjd	lr->lr_doid = dzp->z_id;
290168404Spjd	lr->lr_foid = zp->z_id;
291168404Spjd	lr->lr_mode = zp->z_phys->zp_mode;
292185029Spjd	if (!IS_EPHEMERAL(zp->z_phys->zp_uid)) {
293185029Spjd		lr->lr_uid = (uint64_t)zp->z_phys->zp_uid;
294185029Spjd	} else {
295185029Spjd		lr->lr_uid = fuidp->z_fuid_owner;
296185029Spjd	}
297185029Spjd	if (!IS_EPHEMERAL(zp->z_phys->zp_gid)) {
298185029Spjd		lr->lr_gid = (uint64_t)zp->z_phys->zp_gid;
299185029Spjd	} else {
300185029Spjd		lr->lr_gid = fuidp->z_fuid_group;
301185029Spjd	}
302168404Spjd	lr->lr_gen = zp->z_phys->zp_gen;
303168404Spjd	lr->lr_crtime[0] = zp->z_phys->zp_crtime[0];
304168404Spjd	lr->lr_crtime[1] = zp->z_phys->zp_crtime[1];
305168404Spjd	lr->lr_rdev = zp->z_phys->zp_rdev;
306168404Spjd
307185029Spjd	/*
308185029Spjd	 * Fill in xvattr info if any
309185029Spjd	 */
310185029Spjd	if (vap->va_mask & AT_XVATTR) {
311185029Spjd		zfs_log_xvattr((lr_attr_t *)((caddr_t)lr + lrsize), xvap);
312185029Spjd		end = (caddr_t)lr + lrsize + xvatsize;
313185029Spjd	} else {
314185029Spjd		end = (caddr_t)lr + lrsize;
315185029Spjd	}
316185029Spjd
317185029Spjd	/* Now fill in any ACL info */
318185029Spjd
319185029Spjd	if (vsecp) {
320185029Spjd		lracl = (lr_acl_create_t *)&itx->itx_lr;
321185029Spjd		lracl->lr_aclcnt = vsecp->vsa_aclcnt;
322185029Spjd		lracl->lr_acl_bytes = aclsize;
323185029Spjd		lracl->lr_domcnt = fuidp ? fuidp->z_domain_cnt : 0;
324185029Spjd		lracl->lr_fuidcnt  = fuidp ? fuidp->z_fuid_cnt : 0;
325185029Spjd		if (vsecp->vsa_aclflags & VSA_ACE_ACLFLAGS)
326185029Spjd			lracl->lr_acl_flags = (uint64_t)vsecp->vsa_aclflags;
327185029Spjd		else
328185029Spjd			lracl->lr_acl_flags = 0;
329185029Spjd
330185029Spjd		bcopy(vsecp->vsa_aclentp, end, aclsize);
331185029Spjd		end = (caddr_t)end + ZIL_ACE_LENGTH(aclsize);
332185029Spjd	}
333185029Spjd
334185029Spjd	/* drop in FUID info */
335185029Spjd	if (fuidp) {
336185029Spjd		end = zfs_log_fuid_ids(fuidp, end);
337185029Spjd		end = zfs_log_fuid_domains(fuidp, end);
338185029Spjd	}
339185029Spjd	/*
340185029Spjd	 * Now place file name in log record
341185029Spjd	 */
342185029Spjd	bcopy(name, end, namesize);
343185029Spjd
344168404Spjd	seq = zil_itx_assign(zilog, itx, tx);
345168404Spjd	dzp->z_last_itx = seq;
346168404Spjd	zp->z_last_itx = seq;
347168404Spjd}
348168404Spjd
349168404Spjd/*
350168404Spjd * zfs_log_remove() handles both TX_REMOVE and TX_RMDIR transactions.
351168404Spjd */
352168404Spjdvoid
353185029Spjdzfs_log_remove(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
354168404Spjd	znode_t *dzp, char *name)
355168404Spjd{
356168404Spjd	itx_t *itx;
357168404Spjd	uint64_t seq;
358168404Spjd	lr_remove_t *lr;
359168404Spjd	size_t namesize = strlen(name) + 1;
360168404Spjd
361168404Spjd	if (zilog == NULL)
362168404Spjd		return;
363168404Spjd
364209962Smm	ZFS_HANDLE_REPLAY(zilog, tx); /* exits if replay */
365209962Smm
366168404Spjd	itx = zil_itx_create(txtype, sizeof (*lr) + namesize);
367168404Spjd	lr = (lr_remove_t *)&itx->itx_lr;
368168404Spjd	lr->lr_doid = dzp->z_id;
369168404Spjd	bcopy(name, (char *)(lr + 1), namesize);
370168404Spjd
371168404Spjd	seq = zil_itx_assign(zilog, itx, tx);
372168404Spjd	dzp->z_last_itx = seq;
373168404Spjd}
374168404Spjd
375168404Spjd/*
376168404Spjd * zfs_log_link() handles TX_LINK transactions.
377168404Spjd */
378168404Spjdvoid
379185029Spjdzfs_log_link(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
380168404Spjd	znode_t *dzp, znode_t *zp, char *name)
381168404Spjd{
382168404Spjd	itx_t *itx;
383168404Spjd	uint64_t seq;
384168404Spjd	lr_link_t *lr;
385168404Spjd	size_t namesize = strlen(name) + 1;
386168404Spjd
387168404Spjd	if (zilog == NULL)
388168404Spjd		return;
389168404Spjd
390209962Smm	ZFS_HANDLE_REPLAY(zilog, tx); /* exits if replay */
391209962Smm
392168404Spjd	itx = zil_itx_create(txtype, sizeof (*lr) + namesize);
393168404Spjd	lr = (lr_link_t *)&itx->itx_lr;
394168404Spjd	lr->lr_doid = dzp->z_id;
395168404Spjd	lr->lr_link_obj = zp->z_id;
396168404Spjd	bcopy(name, (char *)(lr + 1), namesize);
397168404Spjd
398168404Spjd	seq = zil_itx_assign(zilog, itx, tx);
399168404Spjd	dzp->z_last_itx = seq;
400168404Spjd	zp->z_last_itx = seq;
401168404Spjd}
402168404Spjd
403168404Spjd/*
404168404Spjd * zfs_log_symlink() handles TX_SYMLINK transactions.
405168404Spjd */
406168404Spjdvoid
407185029Spjdzfs_log_symlink(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
408185029Spjd    znode_t *dzp, znode_t *zp, char *name, char *link)
409168404Spjd{
410168404Spjd	itx_t *itx;
411168404Spjd	uint64_t seq;
412168404Spjd	lr_create_t *lr;
413168404Spjd	size_t namesize = strlen(name) + 1;
414168404Spjd	size_t linksize = strlen(link) + 1;
415168404Spjd
416168404Spjd	if (zilog == NULL)
417168404Spjd		return;
418168404Spjd
419209962Smm	ZFS_HANDLE_REPLAY(zilog, tx); /* exits if replay */
420209962Smm
421168404Spjd	itx = zil_itx_create(txtype, sizeof (*lr) + namesize + linksize);
422168404Spjd	lr = (lr_create_t *)&itx->itx_lr;
423168404Spjd	lr->lr_doid = dzp->z_id;
424168404Spjd	lr->lr_foid = zp->z_id;
425168404Spjd	lr->lr_mode = zp->z_phys->zp_mode;
426168404Spjd	lr->lr_uid = zp->z_phys->zp_uid;
427168404Spjd	lr->lr_gid = zp->z_phys->zp_gid;
428168404Spjd	lr->lr_gen = zp->z_phys->zp_gen;
429168404Spjd	lr->lr_crtime[0] = zp->z_phys->zp_crtime[0];
430168404Spjd	lr->lr_crtime[1] = zp->z_phys->zp_crtime[1];
431168404Spjd	bcopy(name, (char *)(lr + 1), namesize);
432168404Spjd	bcopy(link, (char *)(lr + 1) + namesize, linksize);
433168404Spjd
434168404Spjd	seq = zil_itx_assign(zilog, itx, tx);
435168404Spjd	dzp->z_last_itx = seq;
436168404Spjd	zp->z_last_itx = seq;
437168404Spjd}
438168404Spjd
439168404Spjd/*
440168404Spjd * zfs_log_rename() handles TX_RENAME transactions.
441168404Spjd */
442168404Spjdvoid
443185029Spjdzfs_log_rename(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
444168404Spjd	znode_t *sdzp, char *sname, znode_t *tdzp, char *dname, znode_t *szp)
445168404Spjd{
446168404Spjd	itx_t *itx;
447168404Spjd	uint64_t seq;
448168404Spjd	lr_rename_t *lr;
449168404Spjd	size_t snamesize = strlen(sname) + 1;
450168404Spjd	size_t dnamesize = strlen(dname) + 1;
451168404Spjd
452168404Spjd	if (zilog == NULL)
453168404Spjd		return;
454168404Spjd
455209962Smm	ZFS_HANDLE_REPLAY(zilog, tx); /* exits if replay */
456209962Smm
457168404Spjd	itx = zil_itx_create(txtype, sizeof (*lr) + snamesize + dnamesize);
458168404Spjd	lr = (lr_rename_t *)&itx->itx_lr;
459168404Spjd	lr->lr_sdoid = sdzp->z_id;
460168404Spjd	lr->lr_tdoid = tdzp->z_id;
461168404Spjd	bcopy(sname, (char *)(lr + 1), snamesize);
462168404Spjd	bcopy(dname, (char *)(lr + 1) + snamesize, dnamesize);
463168404Spjd
464168404Spjd	seq = zil_itx_assign(zilog, itx, tx);
465168404Spjd	sdzp->z_last_itx = seq;
466168404Spjd	tdzp->z_last_itx = seq;
467168404Spjd	szp->z_last_itx = seq;
468168404Spjd}
469168404Spjd
470168404Spjd/*
471168404Spjd * zfs_log_write() handles TX_WRITE transactions.
472168404Spjd */
473168404Spjdssize_t zfs_immediate_write_sz = 32768;
474168404Spjd
475185029Spjd#define	ZIL_MAX_LOG_DATA (SPA_MAXBLOCKSIZE - sizeof (zil_trailer_t) - \
476185029Spjd    sizeof (lr_write_t))
477185029Spjd
478168404Spjdvoid
479168404Spjdzfs_log_write(zilog_t *zilog, dmu_tx_t *tx, int txtype,
480185029Spjd	znode_t *zp, offset_t off, ssize_t resid, int ioflag)
481168404Spjd{
482168404Spjd	itx_wr_state_t write_state;
483185029Spjd	boolean_t slogging;
484185029Spjd	uintptr_t fsync_cnt;
485168404Spjd
486168404Spjd	if (zilog == NULL || zp->z_unlinked)
487168404Spjd		return;
488168404Spjd
489209962Smm	ZFS_HANDLE_REPLAY(zilog, tx); /* exits if replay */
490209962Smm
491168404Spjd	/*
492168404Spjd	 * Writes are handled in three different ways:
493168404Spjd	 *
494168404Spjd	 * WR_INDIRECT:
495185029Spjd	 *    In this mode, if we need to commit the write later, then the block
496185029Spjd	 *    is immediately written into the file system (using dmu_sync),
497185029Spjd	 *    and a pointer to the block is put into the log record.
498185029Spjd	 *    When the txg commits the block is linked in.
499185029Spjd	 *    This saves additionally writing the data into the log record.
500185029Spjd	 *    There are a few requirements for this to occur:
501185029Spjd	 *	- write is greater than zfs_immediate_write_sz
502185029Spjd	 *	- not using slogs (as slogs are assumed to always be faster
503185029Spjd	 *	  than writing into the main pool)
504185029Spjd	 *	- the write occupies only one block
505168404Spjd	 * WR_COPIED:
506168404Spjd	 *    If we know we'll immediately be committing the
507185029Spjd	 *    transaction (FSYNC or FDSYNC), the we allocate a larger
508168404Spjd	 *    log record here for the data and copy the data in.
509168404Spjd	 * WR_NEED_COPY:
510168404Spjd	 *    Otherwise we don't allocate a buffer, and *if* we need to
511168404Spjd	 *    flush the write later then a buffer is allocated and
512168404Spjd	 *    we retrieve the data using the dmu.
513168404Spjd	 */
514185029Spjd	slogging = spa_has_slogs(zilog->zl_spa);
515185029Spjd	if (resid > zfs_immediate_write_sz && !slogging && resid <= zp->z_blksz)
516168404Spjd		write_state = WR_INDIRECT;
517185029Spjd	else if (ioflag & (FSYNC | FDSYNC))
518168404Spjd		write_state = WR_COPIED;
519168404Spjd	else
520168404Spjd		write_state = WR_NEED_COPY;
521168404Spjd
522185029Spjd	if ((fsync_cnt = (uintptr_t)tsd_get(zfs_fsyncer_key)) != 0) {
523185029Spjd		(void) tsd_set(zfs_fsyncer_key, (void *)(fsync_cnt - 1));
524185029Spjd	}
525185029Spjd
526185029Spjd	while (resid) {
527185029Spjd		itx_t *itx;
528185029Spjd		lr_write_t *lr;
529185029Spjd		ssize_t len;
530185029Spjd
531185029Spjd		/*
532185029Spjd		 * If the write would overflow the largest block then split it.
533185029Spjd		 */
534185029Spjd		if (write_state != WR_INDIRECT && resid > ZIL_MAX_LOG_DATA)
535185029Spjd			len = SPA_MAXBLOCKSIZE >> 1;
536185029Spjd		else
537185029Spjd			len = resid;
538185029Spjd
539185029Spjd		itx = zil_itx_create(txtype, sizeof (*lr) +
540185029Spjd		    (write_state == WR_COPIED ? len : 0));
541185029Spjd		lr = (lr_write_t *)&itx->itx_lr;
542185029Spjd		if (write_state == WR_COPIED && dmu_read(zp->z_zfsvfs->z_os,
543209962Smm		    zp->z_id, off, len, lr + 1, DMU_READ_NO_PREFETCH) != 0) {
544168404Spjd			kmem_free(itx, offsetof(itx_t, itx_lr) +
545168404Spjd			    itx->itx_lr.lrc_reclen);
546168404Spjd			itx = zil_itx_create(txtype, sizeof (*lr));
547168404Spjd			lr = (lr_write_t *)&itx->itx_lr;
548168404Spjd			write_state = WR_NEED_COPY;
549168404Spjd		}
550168404Spjd
551185029Spjd		itx->itx_wr_state = write_state;
552185029Spjd		if (write_state == WR_NEED_COPY)
553185029Spjd			itx->itx_sod += len;
554185029Spjd		lr->lr_foid = zp->z_id;
555185029Spjd		lr->lr_offset = off;
556185029Spjd		lr->lr_length = len;
557185029Spjd		lr->lr_blkoff = 0;
558185029Spjd		BP_ZERO(&lr->lr_blkptr);
559168404Spjd
560185029Spjd		itx->itx_private = zp->z_zfsvfs;
561168404Spjd
562185029Spjd		if ((zp->z_sync_cnt != 0) || (fsync_cnt != 0) ||
563185029Spjd		    (ioflag & (FSYNC | FDSYNC)))
564185029Spjd			itx->itx_sync = B_TRUE;
565185029Spjd		else
566185029Spjd			itx->itx_sync = B_FALSE;
567185029Spjd
568185029Spjd		zp->z_last_itx = zil_itx_assign(zilog, itx, tx);
569185029Spjd
570185029Spjd		off += len;
571185029Spjd		resid -= len;
572185029Spjd	}
573168404Spjd}
574168404Spjd
575168404Spjd/*
576168404Spjd * zfs_log_truncate() handles TX_TRUNCATE transactions.
577168404Spjd */
578168404Spjdvoid
579168404Spjdzfs_log_truncate(zilog_t *zilog, dmu_tx_t *tx, int txtype,
580168404Spjd	znode_t *zp, uint64_t off, uint64_t len)
581168404Spjd{
582168404Spjd	itx_t *itx;
583168404Spjd	uint64_t seq;
584168404Spjd	lr_truncate_t *lr;
585168404Spjd
586168404Spjd	if (zilog == NULL || zp->z_unlinked)
587168404Spjd		return;
588168404Spjd
589209962Smm	ZFS_HANDLE_REPLAY(zilog, tx); /* exits if replay */
590209962Smm
591168404Spjd	itx = zil_itx_create(txtype, sizeof (*lr));
592168404Spjd	lr = (lr_truncate_t *)&itx->itx_lr;
593168404Spjd	lr->lr_foid = zp->z_id;
594168404Spjd	lr->lr_offset = off;
595168404Spjd	lr->lr_length = len;
596168404Spjd
597168404Spjd	itx->itx_sync = (zp->z_sync_cnt != 0);
598168404Spjd	seq = zil_itx_assign(zilog, itx, tx);
599168404Spjd	zp->z_last_itx = seq;
600168404Spjd}
601168404Spjd
602168404Spjd/*
603168404Spjd * zfs_log_setattr() handles TX_SETATTR transactions.
604168404Spjd */
605168404Spjdvoid
606168404Spjdzfs_log_setattr(zilog_t *zilog, dmu_tx_t *tx, int txtype,
607185029Spjd	znode_t *zp, vattr_t *vap, uint_t mask_applied, zfs_fuid_info_t *fuidp)
608168404Spjd{
609185029Spjd	itx_t		*itx;
610185029Spjd	uint64_t	seq;
611185029Spjd	lr_setattr_t	*lr;
612185029Spjd	xvattr_t	*xvap = (xvattr_t *)vap;
613185029Spjd	size_t		recsize = sizeof (lr_setattr_t);
614185029Spjd	void		*start;
615168404Spjd
616185029Spjd
617168404Spjd	if (zilog == NULL || zp->z_unlinked)
618168404Spjd		return;
619168404Spjd
620209962Smm	ZFS_HANDLE_REPLAY(zilog, tx); /* exits if replay */
621209962Smm
622185029Spjd	/*
623185029Spjd	 * If XVATTR set, then log record size needs to allow
624185029Spjd	 * for lr_attr_t + xvattr mask, mapsize and create time
625185029Spjd	 * plus actual attribute values
626185029Spjd	 */
627185029Spjd	if (vap->va_mask & AT_XVATTR)
628185029Spjd		recsize = sizeof (*lr) + ZIL_XVAT_SIZE(xvap->xva_mapsize);
629185029Spjd
630185029Spjd	if (fuidp)
631185029Spjd		recsize += fuidp->z_domain_str_sz;
632185029Spjd
633185029Spjd	itx = zil_itx_create(txtype, recsize);
634168404Spjd	lr = (lr_setattr_t *)&itx->itx_lr;
635168404Spjd	lr->lr_foid = zp->z_id;
636168404Spjd	lr->lr_mask = (uint64_t)mask_applied;
637168404Spjd	lr->lr_mode = (uint64_t)vap->va_mode;
638185029Spjd	if ((mask_applied & AT_UID) && IS_EPHEMERAL(vap->va_uid))
639185029Spjd		lr->lr_uid = fuidp->z_fuid_owner;
640185029Spjd	else
641185029Spjd		lr->lr_uid = (uint64_t)vap->va_uid;
642185029Spjd
643185029Spjd	if ((mask_applied & AT_GID) && IS_EPHEMERAL(vap->va_gid))
644185029Spjd		lr->lr_gid = fuidp->z_fuid_group;
645185029Spjd	else
646185029Spjd		lr->lr_gid = (uint64_t)vap->va_gid;
647185029Spjd
648168404Spjd	lr->lr_size = (uint64_t)vap->va_size;
649168404Spjd	ZFS_TIME_ENCODE(&vap->va_atime, lr->lr_atime);
650168404Spjd	ZFS_TIME_ENCODE(&vap->va_mtime, lr->lr_mtime);
651185029Spjd	start = (lr_setattr_t *)(lr + 1);
652185029Spjd	if (vap->va_mask & AT_XVATTR) {
653185029Spjd		zfs_log_xvattr((lr_attr_t *)start, xvap);
654185029Spjd		start = (caddr_t)start + ZIL_XVAT_SIZE(xvap->xva_mapsize);
655185029Spjd	}
656168404Spjd
657185029Spjd	/*
658185029Spjd	 * Now stick on domain information if any on end
659185029Spjd	 */
660185029Spjd
661185029Spjd	if (fuidp)
662185029Spjd		(void) zfs_log_fuid_domains(fuidp, start);
663185029Spjd
664168404Spjd	itx->itx_sync = (zp->z_sync_cnt != 0);
665168404Spjd	seq = zil_itx_assign(zilog, itx, tx);
666168404Spjd	zp->z_last_itx = seq;
667168404Spjd}
668168404Spjd
669168404Spjd/*
670168404Spjd * zfs_log_acl() handles TX_ACL transactions.
671168404Spjd */
672168404Spjdvoid
673185029Spjdzfs_log_acl(zilog_t *zilog, dmu_tx_t *tx, znode_t *zp,
674185029Spjd    vsecattr_t *vsecp, zfs_fuid_info_t *fuidp)
675168404Spjd{
676168404Spjd	itx_t *itx;
677168404Spjd	uint64_t seq;
678185029Spjd	lr_acl_v0_t *lrv0;
679168404Spjd	lr_acl_t *lr;
680185029Spjd	int txtype;
681185029Spjd	int lrsize;
682185029Spjd	size_t txsize;
683185029Spjd	size_t aclbytes = vsecp->vsa_aclentsz;
684168404Spjd
685168404Spjd	if (zilog == NULL || zp->z_unlinked)
686168404Spjd		return;
687168404Spjd
688209962Smm	ZFS_HANDLE_REPLAY(zilog, tx); /* exits if replay */
689209962Smm
690185029Spjd	txtype = (zp->z_zfsvfs->z_version < ZPL_VERSION_FUID) ?
691185029Spjd	    TX_ACL_V0 : TX_ACL;
692185029Spjd
693185029Spjd	if (txtype == TX_ACL)
694185029Spjd		lrsize = sizeof (*lr);
695185029Spjd	else
696185029Spjd		lrsize = sizeof (*lrv0);
697185029Spjd
698185029Spjd	txsize = lrsize +
699185029Spjd	    ((txtype == TX_ACL) ? ZIL_ACE_LENGTH(aclbytes) : aclbytes) +
700185029Spjd	    (fuidp ? fuidp->z_domain_str_sz : 0) +
701185029Spjd	    sizeof (uint64_t) * (fuidp ? fuidp->z_fuid_cnt : 0);
702185029Spjd
703185029Spjd	itx = zil_itx_create(txtype, txsize);
704185029Spjd
705168404Spjd	lr = (lr_acl_t *)&itx->itx_lr;
706168404Spjd	lr->lr_foid = zp->z_id;
707185029Spjd	if (txtype == TX_ACL) {
708185029Spjd		lr->lr_acl_bytes = aclbytes;
709185029Spjd		lr->lr_domcnt = fuidp ? fuidp->z_domain_cnt : 0;
710185029Spjd		lr->lr_fuidcnt = fuidp ? fuidp->z_fuid_cnt : 0;
711185029Spjd		if (vsecp->vsa_mask & VSA_ACE_ACLFLAGS)
712185029Spjd			lr->lr_acl_flags = (uint64_t)vsecp->vsa_aclflags;
713185029Spjd		else
714185029Spjd			lr->lr_acl_flags = 0;
715185029Spjd	}
716185029Spjd	lr->lr_aclcnt = (uint64_t)vsecp->vsa_aclcnt;
717168404Spjd
718185029Spjd	if (txtype == TX_ACL_V0) {
719185029Spjd		lrv0 = (lr_acl_v0_t *)lr;
720185029Spjd		bcopy(vsecp->vsa_aclentp, (ace_t *)(lrv0 + 1), aclbytes);
721185029Spjd	} else {
722185029Spjd		void *start = (ace_t *)(lr + 1);
723185029Spjd
724185029Spjd		bcopy(vsecp->vsa_aclentp, start, aclbytes);
725185029Spjd
726185029Spjd		start = (caddr_t)start + ZIL_ACE_LENGTH(aclbytes);
727185029Spjd
728185029Spjd		if (fuidp) {
729185029Spjd			start = zfs_log_fuid_ids(fuidp, start);
730185029Spjd			(void) zfs_log_fuid_domains(fuidp, start);
731185029Spjd		}
732185029Spjd	}
733185029Spjd
734168404Spjd	itx->itx_sync = (zp->z_sync_cnt != 0);
735168404Spjd	seq = zil_itx_assign(zilog, itx, tx);
736168404Spjd	zp->z_last_itx = seq;
737168404Spjd}
738