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