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