xref: /freebsd/sys/contrib/openzfs/module/zfs/zfs_log.c (revision 22649d4dba730d46244fd2dff4fd174903c8379f)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * This file and its contents are supplied under the terms of the
4  * Common Development and Distribution License ("CDDL"), version 1.0.
5  * You may only use this file in accordance with the terms of version
6  * 1.0 of the CDDL.
7  *
8  * A full copy of the text of the CDDL should have accompanied this
9  * source.  A copy of the CDDL is also available via the Internet at
10  * https://opensource.org/license/CDDL-1.0.
11  */
12 /*
13  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
14  * Copyright (c) 2015, 2018 by Delphix. All rights reserved.
15  * Copyright (c) 2022 by Pawel Jakub Dawidek
16  */
17 
18 
19 #include <sys/types.h>
20 #include <sys/param.h>
21 #include <sys/sysmacros.h>
22 #include <sys/cmn_err.h>
23 #include <sys/kmem.h>
24 #include <sys/thread.h>
25 #include <sys/file.h>
26 #include <sys/vfs.h>
27 #include <sys/zfs_znode.h>
28 #include <sys/zfs_dir.h>
29 #include <sys/zil.h>
30 #include <sys/zil_impl.h>
31 #include <sys/byteorder.h>
32 #include <sys/policy.h>
33 #include <sys/stat.h>
34 #include <sys/acl.h>
35 #include <sys/dmu.h>
36 #include <sys/dbuf.h>
37 #include <sys/spa.h>
38 #include <sys/zfs_fuid.h>
39 #include <sys/dsl_dataset.h>
40 
41 /*
42  * These zfs_log_* functions must be called within a dmu tx, in one
43  * of 2 contexts depending on zilog->z_replay:
44  *
45  * Non replay mode
46  * ---------------
47  * We need to record the transaction so that if it is committed to
48  * the Intent Log then it can be replayed.  An intent log transaction
49  * structure (itx_t) is allocated and all the information necessary to
50  * possibly replay the transaction is saved in it. The itx is then assigned
51  * a sequence number and inserted in the in-memory list anchored in the zilog.
52  *
53  * Replay mode
54  * -----------
55  * We need to mark the intent log record as replayed in the log header.
56  * This is done in the same transaction as the replay so that they
57  * commit atomically.
58  */
59 
60 int
zfs_log_create_txtype(zil_create_t type,vsecattr_t * vsecp,vattr_t * vap)61 zfs_log_create_txtype(zil_create_t type, vsecattr_t *vsecp, vattr_t *vap)
62 {
63 	int isxvattr = (vap->va_mask & ATTR_XVATTR);
64 	switch (type) {
65 	case Z_FILE:
66 		if (vsecp == NULL && !isxvattr)
67 			return (TX_CREATE);
68 		if (vsecp && isxvattr)
69 			return (TX_CREATE_ACL_ATTR);
70 		if (vsecp)
71 			return (TX_CREATE_ACL);
72 		else
73 			return (TX_CREATE_ATTR);
74 	case Z_DIR:
75 		if (vsecp == NULL && !isxvattr)
76 			return (TX_MKDIR);
77 		if (vsecp && isxvattr)
78 			return (TX_MKDIR_ACL_ATTR);
79 		if (vsecp)
80 			return (TX_MKDIR_ACL);
81 		else
82 			return (TX_MKDIR_ATTR);
83 	case Z_XATTRDIR:
84 		return (TX_MKXATTR);
85 	}
86 	ASSERT(0);
87 	return (TX_MAX_TYPE);
88 }
89 
90 /*
91  * build up the log data necessary for logging xvattr_t
92  * First lr_attr_t is initialized.  following the lr_attr_t
93  * is the mapsize and attribute bitmap copied from the xvattr_t.
94  * Following the bitmap and bitmapsize two 64 bit words are reserved
95  * for the create time which may be set.  Following the create time
96  * records a single 64 bit integer which has the bits to set on
97  * replay for the xvattr.
98  */
99 static void
zfs_log_xvattr(lr_attr_t * lrattr,xvattr_t * xvap)100 zfs_log_xvattr(lr_attr_t *lrattr, xvattr_t *xvap)
101 {
102 	xoptattr_t *xoap;
103 
104 	xoap = xva_getxoptattr(xvap);
105 	ASSERT(xoap);
106 
107 	lrattr->lr_attr_masksize = xvap->xva_mapsize;
108 	uint32_t *bitmap = &lrattr->lr_attr_bitmap;
109 	for (int i = 0; i != xvap->xva_mapsize; i++, bitmap++)
110 		*bitmap = xvap->xva_reqattrmap[i];
111 
112 	lr_attr_end_t *end = (lr_attr_end_t *)bitmap;
113 	end->lr_attr_attrs = 0;
114 	end->lr_attr_crtime[0] = 0;
115 	end->lr_attr_crtime[1] = 0;
116 	memset(end->lr_attr_scanstamp, 0, AV_SCANSTAMP_SZ);
117 
118 	if (XVA_ISSET_REQ(xvap, XAT_READONLY))
119 		end->lr_attr_attrs |= (xoap->xoa_readonly == 0) ? 0 :
120 		    XAT0_READONLY;
121 	if (XVA_ISSET_REQ(xvap, XAT_HIDDEN))
122 		end->lr_attr_attrs |= (xoap->xoa_hidden == 0) ? 0 :
123 		    XAT0_HIDDEN;
124 	if (XVA_ISSET_REQ(xvap, XAT_SYSTEM))
125 		end->lr_attr_attrs |= (xoap->xoa_system == 0) ? 0 :
126 		    XAT0_SYSTEM;
127 	if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE))
128 		end->lr_attr_attrs |= (xoap->xoa_archive == 0) ? 0 :
129 		    XAT0_ARCHIVE;
130 	if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE))
131 		end->lr_attr_attrs |= (xoap->xoa_immutable == 0) ? 0 :
132 		    XAT0_IMMUTABLE;
133 	if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK))
134 		end->lr_attr_attrs |= (xoap->xoa_nounlink == 0) ? 0 :
135 		    XAT0_NOUNLINK;
136 	if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY))
137 		end->lr_attr_attrs |= (xoap->xoa_appendonly == 0) ? 0 :
138 		    XAT0_APPENDONLY;
139 	if (XVA_ISSET_REQ(xvap, XAT_OPAQUE))
140 		end->lr_attr_attrs |= (xoap->xoa_opaque == 0) ? 0 :
141 		    XAT0_APPENDONLY;
142 	if (XVA_ISSET_REQ(xvap, XAT_NODUMP))
143 		end->lr_attr_attrs |= (xoap->xoa_nodump == 0) ? 0 :
144 		    XAT0_NODUMP;
145 	if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED))
146 		end->lr_attr_attrs |= (xoap->xoa_av_quarantined == 0) ? 0 :
147 		    XAT0_AV_QUARANTINED;
148 	if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED))
149 		end->lr_attr_attrs |= (xoap->xoa_av_modified == 0) ? 0 :
150 		    XAT0_AV_MODIFIED;
151 	if (XVA_ISSET_REQ(xvap, XAT_CREATETIME))
152 		ZFS_TIME_ENCODE(&xoap->xoa_createtime, end->lr_attr_crtime);
153 	if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) {
154 		ASSERT(!XVA_ISSET_REQ(xvap, XAT_PROJID));
155 
156 		memcpy(end->lr_attr_scanstamp, xoap->xoa_av_scanstamp,
157 		    AV_SCANSTAMP_SZ);
158 	} else if (XVA_ISSET_REQ(xvap, XAT_PROJID)) {
159 		/*
160 		 * XAT_PROJID and XAT_AV_SCANSTAMP will never be valid
161 		 * at the same time, so we can share the same space.
162 		 */
163 		memcpy(end->lr_attr_scanstamp, &xoap->xoa_projid,
164 		    sizeof (uint64_t));
165 	}
166 	if (XVA_ISSET_REQ(xvap, XAT_REPARSE))
167 		end->lr_attr_attrs |= (xoap->xoa_reparse == 0) ? 0 :
168 		    XAT0_REPARSE;
169 	if (XVA_ISSET_REQ(xvap, XAT_OFFLINE))
170 		end->lr_attr_attrs |= (xoap->xoa_offline == 0) ? 0 :
171 		    XAT0_OFFLINE;
172 	if (XVA_ISSET_REQ(xvap, XAT_SPARSE))
173 		end->lr_attr_attrs |= (xoap->xoa_sparse == 0) ? 0 :
174 		    XAT0_SPARSE;
175 	if (XVA_ISSET_REQ(xvap, XAT_PROJINHERIT))
176 		end->lr_attr_attrs |= (xoap->xoa_projinherit == 0) ? 0 :
177 		    XAT0_PROJINHERIT;
178 }
179 
180 static void *
zfs_log_fuid_ids(zfs_fuid_info_t * fuidp,void * start)181 zfs_log_fuid_ids(zfs_fuid_info_t *fuidp, void *start)
182 {
183 	zfs_fuid_t *zfuid;
184 	uint64_t *fuidloc = start;
185 
186 	/* First copy in the ACE FUIDs */
187 	for (zfuid = list_head(&fuidp->z_fuids); zfuid;
188 	    zfuid = list_next(&fuidp->z_fuids, zfuid)) {
189 		*fuidloc++ = zfuid->z_logfuid;
190 	}
191 	return (fuidloc);
192 }
193 
194 
195 static void *
zfs_log_fuid_domains(zfs_fuid_info_t * fuidp,void * start)196 zfs_log_fuid_domains(zfs_fuid_info_t *fuidp, void *start)
197 {
198 	zfs_fuid_domain_t *zdomain;
199 
200 	/* now copy in the domain info, if any */
201 	if (fuidp->z_domain_str_sz != 0) {
202 		for (zdomain = list_head(&fuidp->z_domains); zdomain;
203 		    zdomain = list_next(&fuidp->z_domains, zdomain)) {
204 			memcpy(start, zdomain->z_domain,
205 			    strlen(zdomain->z_domain) + 1);
206 			start = (caddr_t)start +
207 			    strlen(zdomain->z_domain) + 1;
208 		}
209 	}
210 	return (start);
211 }
212 
213 /*
214  * If zp is an xattr node, check whether the xattr owner is unlinked.
215  * We don't want to log anything if the owner is unlinked.
216  */
217 static int
zfs_xattr_owner_unlinked(znode_t * zp)218 zfs_xattr_owner_unlinked(znode_t *zp)
219 {
220 	int unlinked = 0;
221 	znode_t *dzp;
222 #ifdef __FreeBSD__
223 	znode_t *tzp = zp;
224 
225 	/*
226 	 * zrele drops the vnode lock which violates the VOP locking contract
227 	 * on FreeBSD. See comment at the top of zfs_replay.c for more detail.
228 	 */
229 	/*
230 	 * if zp is XATTR node, keep walking up via z_xattr_parent until we
231 	 * get the owner
232 	 */
233 	while (tzp->z_pflags & ZFS_XATTR) {
234 		ASSERT3U(zp->z_xattr_parent, !=, 0);
235 		if (zfs_zget(ZTOZSB(tzp), tzp->z_xattr_parent, &dzp) != 0) {
236 			unlinked = 1;
237 			break;
238 		}
239 
240 		if (tzp != zp)
241 			zrele(tzp);
242 		tzp = dzp;
243 		unlinked = tzp->z_unlinked;
244 	}
245 	if (tzp != zp)
246 		zrele(tzp);
247 #else
248 	zhold(zp);
249 	/*
250 	 * if zp is XATTR node, keep walking up via z_xattr_parent until we
251 	 * get the owner
252 	 */
253 	while (zp->z_pflags & ZFS_XATTR) {
254 		ASSERT3U(zp->z_xattr_parent, !=, 0);
255 		if (zfs_zget(ZTOZSB(zp), zp->z_xattr_parent, &dzp) != 0) {
256 			unlinked = 1;
257 			break;
258 		}
259 
260 		zrele(zp);
261 		zp = dzp;
262 		unlinked = zp->z_unlinked;
263 	}
264 	zrele(zp);
265 #endif
266 	return (unlinked);
267 }
268 
269 /*
270  * Handles TX_CREATE, TX_CREATE_ATTR, TX_MKDIR, TX_MKDIR_ATTR and
271  * TK_MKXATTR transactions.
272  *
273  * TX_CREATE and TX_MKDIR are standard creates, but they may have FUID
274  * domain information appended prior to the name.  In this case the
275  * uid/gid in the log record will be a log centric FUID.
276  *
277  * TX_CREATE_ACL_ATTR and TX_MKDIR_ACL_ATTR handle special creates that
278  * may contain attributes, ACL and optional fuid information.
279  *
280  * TX_CREATE_ACL and TX_MKDIR_ACL handle special creates that specify
281  * and ACL and normal users/groups in the ACEs.
282  *
283  * There may be an optional xvattr attribute information similar
284  * to zfs_log_setattr.
285  *
286  * Also, after the file name "domain" strings may be appended.
287  */
288 void
zfs_log_create(zilog_t * zilog,dmu_tx_t * tx,uint64_t txtype,znode_t * dzp,znode_t * zp,const char * name,vsecattr_t * vsecp,zfs_fuid_info_t * fuidp,vattr_t * vap)289 zfs_log_create(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
290     znode_t *dzp, znode_t *zp, const char *name, vsecattr_t *vsecp,
291     zfs_fuid_info_t *fuidp, vattr_t *vap)
292 {
293 	itx_t *itx;
294 	_lr_create_t *lr;
295 	lr_acl_create_t *lracl = NULL;
296 	uint8_t *lrdata;
297 	size_t aclsize = 0;
298 	size_t xvatsize = 0;
299 	size_t txsize;
300 	xvattr_t *xvap = (xvattr_t *)vap;
301 	size_t namesize = strlen(name) + 1;
302 	size_t fuidsz = 0;
303 
304 	if (zil_replaying(zilog, tx) || zfs_xattr_owner_unlinked(dzp))
305 		return;
306 
307 	/*
308 	 * If we have FUIDs present then add in space for
309 	 * domains and ACE fuid's if any.
310 	 */
311 	if (fuidp) {
312 		fuidsz += fuidp->z_domain_str_sz;
313 		fuidsz += fuidp->z_fuid_cnt * sizeof (uint64_t);
314 	}
315 
316 	if (vap->va_mask & ATTR_XVATTR)
317 		xvatsize = ZIL_XVAT_SIZE(xvap->xva_mapsize);
318 
319 	if ((int)txtype == TX_CREATE_ATTR || (int)txtype == TX_MKDIR_ATTR ||
320 	    (int)txtype == TX_CREATE || (int)txtype == TX_MKDIR ||
321 	    (int)txtype == TX_MKXATTR) {
322 		txsize = sizeof (lr_create_t) + namesize + fuidsz + xvatsize;
323 		itx = zil_itx_create(txtype, txsize);
324 		lr_create_t *lrc = (lr_create_t *)&itx->itx_lr;
325 		lrdata = &lrc->lr_data[0];
326 	} else {
327 		txsize =
328 		    sizeof (lr_acl_create_t) + namesize + fuidsz +
329 		    ZIL_ACE_LENGTH(aclsize) + xvatsize;
330 		itx = zil_itx_create(txtype, txsize);
331 		lracl = (lr_acl_create_t *)&itx->itx_lr;
332 		lrdata = &lracl->lr_data[0];
333 	}
334 
335 
336 	lr = (_lr_create_t *)&itx->itx_lr;
337 	lr->lr_doid = dzp->z_id;
338 	lr->lr_foid = zp->z_id;
339 	/* Store dnode slot count in 8 bits above object id. */
340 	LR_FOID_SET_SLOTS(lr->lr_foid, zp->z_dnodesize >> DNODE_SHIFT);
341 	lr->lr_mode = zp->z_mode;
342 	if (!IS_EPHEMERAL(KUID_TO_SUID(ZTOUID(zp)))) {
343 		lr->lr_uid = (uint64_t)KUID_TO_SUID(ZTOUID(zp));
344 	} else {
345 		lr->lr_uid = fuidp->z_fuid_owner;
346 	}
347 	if (!IS_EPHEMERAL(KGID_TO_SGID(ZTOGID(zp)))) {
348 		lr->lr_gid = (uint64_t)KGID_TO_SGID(ZTOGID(zp));
349 	} else {
350 		lr->lr_gid = fuidp->z_fuid_group;
351 	}
352 	(void) sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(ZTOZSB(zp)), &lr->lr_gen,
353 	    sizeof (uint64_t));
354 	(void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(ZTOZSB(zp)),
355 	    lr->lr_crtime, sizeof (uint64_t) * 2);
356 
357 	if (sa_lookup(zp->z_sa_hdl, SA_ZPL_RDEV(ZTOZSB(zp)), &lr->lr_rdev,
358 	    sizeof (lr->lr_rdev)) != 0)
359 		lr->lr_rdev = 0;
360 
361 	/*
362 	 * Fill in xvattr info if any
363 	 */
364 	if (vap->va_mask & ATTR_XVATTR) {
365 		zfs_log_xvattr((lr_attr_t *)lrdata, xvap);
366 		lrdata = &lrdata[xvatsize];
367 	}
368 
369 	/* Now fill in any ACL info */
370 
371 	if (vsecp) {
372 		ASSERT3P(lracl, !=, NULL);
373 		lracl->lr_aclcnt = vsecp->vsa_aclcnt;
374 		lracl->lr_acl_bytes = aclsize;
375 		lracl->lr_domcnt = fuidp ? fuidp->z_domain_cnt : 0;
376 		lracl->lr_fuidcnt  = fuidp ? fuidp->z_fuid_cnt : 0;
377 		if (vsecp->vsa_aclflags & VSA_ACE_ACLFLAGS)
378 			lracl->lr_acl_flags = (uint64_t)vsecp->vsa_aclflags;
379 		else
380 			lracl->lr_acl_flags = 0;
381 
382 		memcpy(lrdata, vsecp->vsa_aclentp, aclsize);
383 		lrdata = &lrdata[ZIL_ACE_LENGTH(aclsize)];
384 	}
385 
386 	/* drop in FUID info */
387 	if (fuidp) {
388 		lrdata = zfs_log_fuid_ids(fuidp, lrdata);
389 		lrdata = zfs_log_fuid_domains(fuidp, lrdata);
390 	}
391 	/*
392 	 * Now place file name in log record
393 	 */
394 	memcpy(lrdata, name, namesize);
395 
396 	zil_itx_assign(zilog, itx, tx);
397 }
398 
399 /*
400  * Handles both TX_REMOVE and TX_RMDIR transactions.
401  */
402 void
zfs_log_remove(zilog_t * zilog,dmu_tx_t * tx,uint64_t txtype,znode_t * dzp,const char * name,uint64_t foid,boolean_t unlinked)403 zfs_log_remove(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
404     znode_t *dzp, const char *name, uint64_t foid, boolean_t unlinked)
405 {
406 	itx_t *itx;
407 	lr_remove_t *lr;
408 	size_t namesize = strlen(name) + 1;
409 
410 	if (zil_replaying(zilog, tx) || zfs_xattr_owner_unlinked(dzp))
411 		return;
412 
413 	itx = zil_itx_create(txtype, sizeof (*lr) + namesize);
414 	lr = (lr_remove_t *)&itx->itx_lr;
415 	lr->lr_doid = dzp->z_id;
416 	memcpy(&lr->lr_data[0], name, namesize);
417 
418 	itx->itx_oid = foid;
419 
420 	/*
421 	 * Object ids can be re-instantiated in the next txg so
422 	 * remove any async transactions to avoid future leaks.
423 	 * This can happen if a fsync occurs on the re-instantiated
424 	 * object for a WR_INDIRECT or WR_NEED_COPY write, which gets
425 	 * the new file data and flushes a write record for the old object.
426 	 */
427 	if (unlinked) {
428 		ASSERT((txtype & ~TX_CI) == TX_REMOVE);
429 		zil_remove_async(zilog, foid);
430 	}
431 	zil_itx_assign(zilog, itx, tx);
432 }
433 
434 /*
435  * Handles TX_LINK transactions.
436  */
437 void
zfs_log_link(zilog_t * zilog,dmu_tx_t * tx,uint64_t txtype,znode_t * dzp,znode_t * zp,const char * name)438 zfs_log_link(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
439     znode_t *dzp, znode_t *zp, const char *name)
440 {
441 	itx_t *itx;
442 	lr_link_t *lr;
443 	size_t namesize = strlen(name) + 1;
444 
445 	if (zil_replaying(zilog, tx))
446 		return;
447 
448 	itx = zil_itx_create(txtype, sizeof (*lr) + namesize);
449 	lr = (lr_link_t *)&itx->itx_lr;
450 	lr->lr_doid = dzp->z_id;
451 	lr->lr_link_obj = zp->z_id;
452 	memcpy(&lr->lr_data[0], name, namesize);
453 
454 	zil_itx_assign(zilog, itx, tx);
455 }
456 
457 /*
458  * Handles TX_SYMLINK transactions.
459  */
460 void
zfs_log_symlink(zilog_t * zilog,dmu_tx_t * tx,uint64_t txtype,znode_t * dzp,znode_t * zp,const char * name,const char * link)461 zfs_log_symlink(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
462     znode_t *dzp, znode_t *zp, const char *name, const char *link)
463 {
464 	itx_t *itx;
465 	_lr_create_t *lr;
466 	lr_create_t *lrc;
467 	size_t namesize = strlen(name) + 1;
468 	size_t linksize = strlen(link) + 1;
469 
470 	if (zil_replaying(zilog, tx))
471 		return;
472 
473 	itx = zil_itx_create(txtype, sizeof (*lrc) + namesize + linksize);
474 	lrc = (lr_create_t *)&itx->itx_lr;
475 	lr = &lrc->lr_create;
476 	lr->lr_doid = dzp->z_id;
477 	lr->lr_foid = zp->z_id;
478 	lr->lr_uid = KUID_TO_SUID(ZTOUID(zp));
479 	lr->lr_gid = KGID_TO_SGID(ZTOGID(zp));
480 	lr->lr_mode = zp->z_mode;
481 	(void) sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(ZTOZSB(zp)), &lr->lr_gen,
482 	    sizeof (uint64_t));
483 	(void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(ZTOZSB(zp)),
484 	    lr->lr_crtime, sizeof (uint64_t) * 2);
485 	memcpy(&lrc->lr_data[0], name, namesize);
486 	memcpy(&lrc->lr_data[namesize], link, linksize);
487 
488 	zil_itx_assign(zilog, itx, tx);
489 }
490 
491 static void
do_zfs_log_rename(zilog_t * zilog,dmu_tx_t * tx,uint64_t txtype,znode_t * sdzp,const char * sname,znode_t * tdzp,const char * dname,znode_t * szp)492 do_zfs_log_rename(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype, znode_t *sdzp,
493     const char *sname, znode_t *tdzp, const char *dname, znode_t *szp)
494 {
495 	itx_t *itx;
496 	_lr_rename_t *lr;
497 	lr_rename_t *lrr;
498 	size_t snamesize = strlen(sname) + 1;
499 	size_t dnamesize = strlen(dname) + 1;
500 
501 	if (zil_replaying(zilog, tx))
502 		return;
503 
504 	itx = zil_itx_create(txtype, sizeof (*lr) + snamesize + dnamesize);
505 	lrr = (lr_rename_t *)&itx->itx_lr;
506 	lr = &lrr->lr_rename;
507 	lr->lr_sdoid = sdzp->z_id;
508 	lr->lr_tdoid = tdzp->z_id;
509 	memcpy(&lrr->lr_data[0], sname, snamesize);
510 	memcpy(&lrr->lr_data[snamesize], dname, dnamesize);
511 	itx->itx_oid = szp->z_id;
512 
513 	zil_itx_assign(zilog, itx, tx);
514 }
515 
516 /*
517  * Handles TX_RENAME transactions.
518  */
519 void
zfs_log_rename(zilog_t * zilog,dmu_tx_t * tx,uint64_t txtype,znode_t * sdzp,const char * sname,znode_t * tdzp,const char * dname,znode_t * szp)520 zfs_log_rename(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype, znode_t *sdzp,
521     const char *sname, znode_t *tdzp, const char *dname, znode_t *szp)
522 {
523 	txtype |= TX_RENAME;
524 	do_zfs_log_rename(zilog, tx, txtype, sdzp, sname, tdzp, dname, szp);
525 }
526 
527 /*
528  * Handles TX_RENAME_EXCHANGE transactions.
529  */
530 void
zfs_log_rename_exchange(zilog_t * zilog,dmu_tx_t * tx,uint64_t txtype,znode_t * sdzp,const char * sname,znode_t * tdzp,const char * dname,znode_t * szp)531 zfs_log_rename_exchange(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
532     znode_t *sdzp, const char *sname, znode_t *tdzp, const char *dname,
533     znode_t *szp)
534 {
535 	txtype |= TX_RENAME_EXCHANGE;
536 	do_zfs_log_rename(zilog, tx, txtype, sdzp, sname, tdzp, dname, szp);
537 }
538 
539 /*
540  * Handles TX_RENAME_WHITEOUT transactions.
541  *
542  * Unfortunately we cannot reuse do_zfs_log_rename because we we need to call
543  * zfs_mknode() on replay which requires stashing bits as with TX_CREATE.
544  */
545 void
zfs_log_rename_whiteout(zilog_t * zilog,dmu_tx_t * tx,uint64_t txtype,znode_t * sdzp,const char * sname,znode_t * tdzp,const char * dname,znode_t * szp,znode_t * wzp)546 zfs_log_rename_whiteout(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
547     znode_t *sdzp, const char *sname, znode_t *tdzp, const char *dname,
548     znode_t *szp, znode_t *wzp)
549 {
550 	itx_t *itx;
551 	lr_rename_whiteout_t *lr;
552 	size_t snamesize = strlen(sname) + 1;
553 	size_t dnamesize = strlen(dname) + 1;
554 
555 	if (zil_replaying(zilog, tx))
556 		return;
557 
558 	txtype |= TX_RENAME_WHITEOUT;
559 	itx = zil_itx_create(txtype, sizeof (*lr) + snamesize + dnamesize);
560 	lr = (lr_rename_whiteout_t *)&itx->itx_lr;
561 	lr->lr_rename.lr_sdoid = sdzp->z_id;
562 	lr->lr_rename.lr_tdoid = tdzp->z_id;
563 
564 	/*
565 	 * RENAME_WHITEOUT will create an entry at the source znode, so we need
566 	 * to store the same data that the equivalent call to zfs_log_create()
567 	 * would.
568 	 */
569 	lr->lr_wfoid = wzp->z_id;
570 	LR_FOID_SET_SLOTS(lr->lr_wfoid, wzp->z_dnodesize >> DNODE_SHIFT);
571 	(void) sa_lookup(wzp->z_sa_hdl, SA_ZPL_GEN(ZTOZSB(wzp)), &lr->lr_wgen,
572 	    sizeof (uint64_t));
573 	(void) sa_lookup(wzp->z_sa_hdl, SA_ZPL_CRTIME(ZTOZSB(wzp)),
574 	    lr->lr_wcrtime, sizeof (uint64_t) * 2);
575 	lr->lr_wmode = wzp->z_mode;
576 	lr->lr_wuid = (uint64_t)KUID_TO_SUID(ZTOUID(wzp));
577 	lr->lr_wgid = (uint64_t)KGID_TO_SGID(ZTOGID(wzp));
578 
579 	/*
580 	 * This rdev will always be makdevice(0, 0) but because the ZIL log and
581 	 * replay code needs to be platform independent (and there is no
582 	 * platform independent makdev()) we need to copy the one created
583 	 * during the rename operation.
584 	 */
585 	(void) sa_lookup(wzp->z_sa_hdl, SA_ZPL_RDEV(ZTOZSB(wzp)), &lr->lr_wrdev,
586 	    sizeof (lr->lr_wrdev));
587 
588 	memcpy(&lr->lr_data[0], sname, snamesize);
589 	memcpy(&lr->lr_data[snamesize], dname, dnamesize);
590 	itx->itx_oid = szp->z_id;
591 
592 	zil_itx_assign(zilog, itx, tx);
593 }
594 
595 /*
596  * zfs_log_write() handles TX_WRITE transactions. The specified callback is
597  * called as soon as the write is on stable storage (be it via a DMU sync or a
598  * ZIL commit).
599  */
600 void
zfs_log_write(zilog_t * zilog,dmu_tx_t * tx,int txtype,znode_t * zp,offset_t off,ssize_t resid,boolean_t commit,boolean_t o_direct,zil_callback_t callback,void * callback_data)601 zfs_log_write(zilog_t *zilog, dmu_tx_t *tx, int txtype,
602     znode_t *zp, offset_t off, ssize_t resid, boolean_t commit,
603     boolean_t o_direct, zil_callback_t callback, void *callback_data)
604 {
605 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)sa_get_db(zp->z_sa_hdl);
606 	uint32_t blocksize = zp->z_blksz;
607 	itx_wr_state_t write_state;
608 	uint64_t gen = 0, log_size = 0;
609 
610 	if (zil_replaying(zilog, tx) || zp->z_unlinked ||
611 	    zfs_xattr_owner_unlinked(zp)) {
612 		if (callback != NULL)
613 			callback(callback_data, 0);
614 		return;
615 	}
616 
617 	write_state = zil_write_state(zilog, resid, blocksize, o_direct,
618 	    commit);
619 
620 	(void) sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(ZTOZSB(zp)), &gen,
621 	    sizeof (gen));
622 
623 	while (resid) {
624 		itx_t *itx;
625 		lr_write_t *lr;
626 		itx_wr_state_t wr_state = write_state;
627 		ssize_t len = resid;
628 
629 		/*
630 		 * A WR_COPIED record must fit entirely in one log block.
631 		 * Large writes can use WR_NEED_COPY, which the ZIL will
632 		 * split into multiple records across several log blocks
633 		 * if necessary.
634 		 */
635 		if (wr_state == WR_COPIED &&
636 		    resid > zil_max_copied_data(zilog))
637 			wr_state = WR_NEED_COPY;
638 		else if (wr_state == WR_INDIRECT)
639 			len = MIN(blocksize - P2PHASE(off, blocksize), resid);
640 
641 		itx = zil_itx_create(txtype, sizeof (*lr) +
642 		    (wr_state == WR_COPIED ? len : 0));
643 		lr = (lr_write_t *)&itx->itx_lr;
644 
645 		/*
646 		 * For WR_COPIED records, copy the data into the lr_write_t.
647 		 */
648 		if (wr_state == WR_COPIED) {
649 			int err;
650 			DB_DNODE_ENTER(db);
651 			err = dmu_read_by_dnode(DB_DNODE(db), off, len,
652 			    &lr->lr_data[0], DMU_READ_NO_PREFETCH |
653 			    DMU_KEEP_CACHING);
654 			DB_DNODE_EXIT(db);
655 			if (err != 0) {
656 				zil_itx_destroy(itx, 0);
657 				itx = zil_itx_create(txtype, sizeof (*lr));
658 				lr = (lr_write_t *)&itx->itx_lr;
659 				wr_state = WR_NEED_COPY;
660 			}
661 		}
662 
663 		log_size += itx->itx_size;
664 		if (wr_state == WR_NEED_COPY)
665 			log_size += len;
666 
667 		itx->itx_wr_state = wr_state;
668 		lr->lr_foid = zp->z_id;
669 		lr->lr_offset = off;
670 		lr->lr_length = len;
671 		lr->lr_blkoff = 0;
672 		BP_ZERO(&lr->lr_blkptr);
673 
674 		itx->itx_private = ZTOZSB(zp);
675 		itx->itx_sync = (zp->z_sync_cnt != 0);
676 		itx->itx_gen = gen;
677 
678 		if (resid == len) {
679 			itx->itx_callback = callback;
680 			itx->itx_callback_data = callback_data;
681 		}
682 
683 		zil_itx_assign(zilog, itx, tx);
684 
685 		off += len;
686 		resid -= len;
687 	}
688 
689 	dsl_pool_wrlog_count(zilog->zl_dmu_pool, log_size, tx->tx_txg);
690 }
691 
692 /*
693  * Handles TX_TRUNCATE transactions.
694  */
695 void
zfs_log_truncate(zilog_t * zilog,dmu_tx_t * tx,int txtype,znode_t * zp,uint64_t off,uint64_t len)696 zfs_log_truncate(zilog_t *zilog, dmu_tx_t *tx, int txtype,
697     znode_t *zp, uint64_t off, uint64_t len)
698 {
699 	itx_t *itx;
700 	lr_truncate_t *lr;
701 
702 	if (zil_replaying(zilog, tx) || zp->z_unlinked ||
703 	    zfs_xattr_owner_unlinked(zp))
704 		return;
705 
706 	itx = zil_itx_create(txtype, sizeof (*lr));
707 	lr = (lr_truncate_t *)&itx->itx_lr;
708 	lr->lr_foid = zp->z_id;
709 	lr->lr_offset = off;
710 	lr->lr_length = len;
711 
712 	itx->itx_sync = (zp->z_sync_cnt != 0);
713 	zil_itx_assign(zilog, itx, tx);
714 }
715 
716 /*
717  * Handles TX_SETATTR transactions.
718  */
719 void
zfs_log_setattr(zilog_t * zilog,dmu_tx_t * tx,int txtype,znode_t * zp,vattr_t * vap,uint_t mask_applied,zfs_fuid_info_t * fuidp)720 zfs_log_setattr(zilog_t *zilog, dmu_tx_t *tx, int txtype,
721     znode_t *zp, vattr_t *vap, uint_t mask_applied, zfs_fuid_info_t *fuidp)
722 {
723 	itx_t		*itx;
724 	lr_setattr_t	*lr;
725 	xvattr_t	*xvap = (xvattr_t *)vap;
726 	size_t		recsize = sizeof (lr_setattr_t);
727 	uint8_t		*start;
728 
729 	if (zil_replaying(zilog, tx) || zp->z_unlinked)
730 		return;
731 
732 	/*
733 	 * If XVATTR set, then log record size needs to allow
734 	 * for lr_attr_t + xvattr mask, mapsize and create time
735 	 * plus actual attribute values
736 	 */
737 	if (vap->va_mask & ATTR_XVATTR)
738 		recsize = sizeof (*lr) + ZIL_XVAT_SIZE(xvap->xva_mapsize);
739 
740 	if (fuidp)
741 		recsize += fuidp->z_domain_str_sz;
742 
743 	itx = zil_itx_create(txtype, recsize);
744 	lr = (lr_setattr_t *)&itx->itx_lr;
745 	lr->lr_foid = zp->z_id;
746 	lr->lr_mask = (uint64_t)mask_applied;
747 	lr->lr_mode = (uint64_t)vap->va_mode;
748 	if ((mask_applied & ATTR_UID) && IS_EPHEMERAL(vap->va_uid))
749 		lr->lr_uid = fuidp->z_fuid_owner;
750 	else
751 		lr->lr_uid = (uint64_t)vap->va_uid;
752 
753 	if ((mask_applied & ATTR_GID) && IS_EPHEMERAL(vap->va_gid))
754 		lr->lr_gid = fuidp->z_fuid_group;
755 	else
756 		lr->lr_gid = (uint64_t)vap->va_gid;
757 
758 	lr->lr_size = (uint64_t)vap->va_size;
759 	ZFS_TIME_ENCODE(&vap->va_atime, lr->lr_atime);
760 	ZFS_TIME_ENCODE(&vap->va_mtime, lr->lr_mtime);
761 	start = &lr->lr_data[0];
762 	if (vap->va_mask & ATTR_XVATTR) {
763 		zfs_log_xvattr((lr_attr_t *)start, xvap);
764 		start = &lr->lr_data[ZIL_XVAT_SIZE(xvap->xva_mapsize)];
765 	}
766 
767 	/*
768 	 * Now stick on domain information if any on end
769 	 */
770 
771 	if (fuidp)
772 		(void) zfs_log_fuid_domains(fuidp, start);
773 
774 	itx->itx_sync = (zp->z_sync_cnt != 0);
775 	zil_itx_assign(zilog, itx, tx);
776 }
777 
778 /*
779  * Handles TX_SETSAXATTR transactions.
780  */
781 void
zfs_log_setsaxattr(zilog_t * zilog,dmu_tx_t * tx,int txtype,znode_t * zp,const char * name,const void * value,size_t size)782 zfs_log_setsaxattr(zilog_t *zilog, dmu_tx_t *tx, int txtype,
783     znode_t *zp, const char *name, const void *value, size_t size)
784 {
785 	itx_t		*itx;
786 	lr_setsaxattr_t	*lr;
787 	size_t		recsize = sizeof (lr_setsaxattr_t);
788 	int		namelen;
789 
790 	if (zil_replaying(zilog, tx) || zp->z_unlinked)
791 		return;
792 
793 	namelen = strlen(name) + 1;
794 	recsize += (namelen + size);
795 	itx = zil_itx_create(txtype, recsize);
796 	lr = (lr_setsaxattr_t *)&itx->itx_lr;
797 	lr->lr_foid = zp->z_id;
798 	memcpy(&lr->lr_data[0], name, namelen);
799 	if (value != NULL) {
800 		memcpy(&lr->lr_data[namelen], value, size);
801 		lr->lr_size = size;
802 	} else {
803 		lr->lr_size = 0;
804 	}
805 
806 	itx->itx_sync = (zp->z_sync_cnt != 0);
807 	zil_itx_assign(zilog, itx, tx);
808 }
809 
810 /*
811  * Handles TX_ACL transactions.
812  */
813 void
zfs_log_acl(zilog_t * zilog,dmu_tx_t * tx,znode_t * zp,vsecattr_t * vsecp,zfs_fuid_info_t * fuidp)814 zfs_log_acl(zilog_t *zilog, dmu_tx_t *tx, znode_t *zp,
815     vsecattr_t *vsecp, zfs_fuid_info_t *fuidp)
816 {
817 	itx_t *itx;
818 	lr_acl_v0_t *lrv0;
819 	lr_acl_t *lr;
820 	int txtype;
821 	int lrsize;
822 	size_t txsize;
823 	size_t aclbytes = vsecp->vsa_aclentsz;
824 
825 	if (zil_replaying(zilog, tx) || zp->z_unlinked)
826 		return;
827 
828 	txtype = (ZTOZSB(zp)->z_version < ZPL_VERSION_FUID) ?
829 	    TX_ACL_V0 : TX_ACL;
830 
831 	if (txtype == TX_ACL)
832 		lrsize = sizeof (*lr);
833 	else
834 		lrsize = sizeof (*lrv0);
835 
836 	txsize = lrsize +
837 	    ((txtype == TX_ACL) ? ZIL_ACE_LENGTH(aclbytes) : aclbytes) +
838 	    (fuidp ? fuidp->z_domain_str_sz : 0) +
839 	    sizeof (uint64_t) * (fuidp ? fuidp->z_fuid_cnt : 0);
840 
841 	itx = zil_itx_create(txtype, txsize);
842 
843 	lr = (lr_acl_t *)&itx->itx_lr;
844 	lr->lr_foid = zp->z_id;
845 	if (txtype == TX_ACL) {
846 		lr->lr_acl_bytes = aclbytes;
847 		lr->lr_domcnt = fuidp ? fuidp->z_domain_cnt : 0;
848 		lr->lr_fuidcnt = fuidp ? fuidp->z_fuid_cnt : 0;
849 		if (vsecp->vsa_mask & VSA_ACE_ACLFLAGS)
850 			lr->lr_acl_flags = (uint64_t)vsecp->vsa_aclflags;
851 		else
852 			lr->lr_acl_flags = 0;
853 	}
854 	lr->lr_aclcnt = (uint64_t)vsecp->vsa_aclcnt;
855 
856 	if (txtype == TX_ACL_V0) {
857 		lrv0 = (lr_acl_v0_t *)lr;
858 		memcpy(&lrv0->lr_data[0], vsecp->vsa_aclentp, aclbytes);
859 	} else {
860 		uint8_t *start = &lr->lr_data[0];
861 
862 		memcpy(start, vsecp->vsa_aclentp, aclbytes);
863 
864 		start = &lr->lr_data[ZIL_ACE_LENGTH(aclbytes)];
865 
866 		if (fuidp) {
867 			start = zfs_log_fuid_ids(fuidp, start);
868 			(void) zfs_log_fuid_domains(fuidp, start);
869 		}
870 	}
871 
872 	itx->itx_sync = (zp->z_sync_cnt != 0);
873 	zil_itx_assign(zilog, itx, tx);
874 }
875 
876 /*
877  * Handles TX_CLONE_RANGE transactions.
878  */
879 void
zfs_log_clone_range(zilog_t * zilog,dmu_tx_t * tx,int txtype,znode_t * zp,uint64_t off,uint64_t len,uint64_t blksz,const blkptr_t * bps,size_t nbps)880 zfs_log_clone_range(zilog_t *zilog, dmu_tx_t *tx, int txtype, znode_t *zp,
881     uint64_t off, uint64_t len, uint64_t blksz, const blkptr_t *bps,
882     size_t nbps)
883 {
884 	itx_t *itx;
885 	lr_clone_range_t *lr;
886 	uint64_t partlen, max_log_data;
887 	size_t partnbps;
888 
889 	if (zil_replaying(zilog, tx) || zp->z_unlinked)
890 		return;
891 
892 	max_log_data = zil_max_log_data(zilog, sizeof (lr_clone_range_t));
893 
894 	while (nbps > 0) {
895 		partnbps = MIN(nbps, max_log_data / sizeof (bps[0]));
896 		partlen = partnbps * blksz;
897 		ASSERT3U(partlen, <, len + blksz);
898 		partlen = MIN(partlen, len);
899 
900 		itx = zil_itx_create(txtype,
901 		    sizeof (*lr) + sizeof (bps[0]) * partnbps);
902 		lr = (lr_clone_range_t *)&itx->itx_lr;
903 		lr->lr_foid = zp->z_id;
904 		lr->lr_offset = off;
905 		lr->lr_length = partlen;
906 		lr->lr_blksz = blksz;
907 		lr->lr_nbps = partnbps;
908 		memcpy(lr->lr_bps, bps, sizeof (bps[0]) * partnbps);
909 
910 		itx->itx_sync = (zp->z_sync_cnt != 0);
911 
912 		zil_itx_assign(zilog, itx, tx);
913 
914 		bps += partnbps;
915 		ASSERT3U(nbps, >=, partnbps);
916 		nbps -= partnbps;
917 		off += partlen;
918 		ASSERT3U(len, >=, partlen);
919 		len -= partlen;
920 	}
921 }
922