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