xref: /freebsd/sys/contrib/openzfs/module/zfs/dmu_tx.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 2011 Nexenta Systems, Inc.  All rights reserved.
15  * Copyright (c) 2012, 2017 by Delphix. All rights reserved.
16  * Copyright (c) 2024, 2025, Klara, Inc.
17  */
18 
19 #include <sys/dmu.h>
20 #include <sys/dmu_impl.h>
21 #include <sys/dbuf.h>
22 #include <sys/dmu_tx.h>
23 #include <sys/dmu_objset.h>
24 #include <sys/dsl_dataset.h>
25 #include <sys/dsl_dir.h>
26 #include <sys/dsl_pool.h>
27 #include <sys/zap_impl.h>
28 #include <sys/spa.h>
29 #include <sys/brt.h>
30 #include <sys/brt_impl.h>
31 #include <sys/sa.h>
32 #include <sys/sa_impl.h>
33 #include <sys/zfs_context.h>
34 #include <sys/trace_zfs.h>
35 
36 typedef void (*dmu_tx_hold_func_t)(dmu_tx_t *tx, struct dnode *dn,
37     uint64_t arg1, uint64_t arg2);
38 
39 dmu_tx_stats_t dmu_tx_stats = {
40 	{ "dmu_tx_assigned",		KSTAT_DATA_UINT64 },
41 	{ "dmu_tx_delay",		KSTAT_DATA_UINT64 },
42 	{ "dmu_tx_error",		KSTAT_DATA_UINT64 },
43 	{ "dmu_tx_suspended",		KSTAT_DATA_UINT64 },
44 	{ "dmu_tx_group",		KSTAT_DATA_UINT64 },
45 	{ "dmu_tx_memory_reserve",	KSTAT_DATA_UINT64 },
46 	{ "dmu_tx_memory_reclaim",	KSTAT_DATA_UINT64 },
47 	{ "dmu_tx_dirty_throttle",	KSTAT_DATA_UINT64 },
48 	{ "dmu_tx_dirty_delay",		KSTAT_DATA_UINT64 },
49 	{ "dmu_tx_dirty_over_max",	KSTAT_DATA_UINT64 },
50 	{ "dmu_tx_dirty_frees_delay",	KSTAT_DATA_UINT64 },
51 	{ "dmu_tx_wrlog_delay",		KSTAT_DATA_UINT64 },
52 	{ "dmu_tx_quota",		KSTAT_DATA_UINT64 },
53 };
54 
55 static kstat_t *dmu_tx_ksp;
56 
57 dmu_tx_t *
dmu_tx_create_dd(dsl_dir_t * dd)58 dmu_tx_create_dd(dsl_dir_t *dd)
59 {
60 	dmu_tx_t *tx = kmem_zalloc(sizeof (dmu_tx_t), KM_SLEEP);
61 	tx->tx_dir = dd;
62 	if (dd != NULL)
63 		tx->tx_pool = dd->dd_pool;
64 	list_create(&tx->tx_holds, sizeof (dmu_tx_hold_t),
65 	    offsetof(dmu_tx_hold_t, txh_node));
66 	list_create(&tx->tx_callbacks, sizeof (dmu_tx_callback_t),
67 	    offsetof(dmu_tx_callback_t, dcb_node));
68 	tx->tx_start = gethrtime();
69 	return (tx);
70 }
71 
72 dmu_tx_t *
dmu_tx_create(objset_t * os)73 dmu_tx_create(objset_t *os)
74 {
75 	dmu_tx_t *tx = dmu_tx_create_dd(os->os_dsl_dataset->ds_dir);
76 	tx->tx_objset = os;
77 	return (tx);
78 }
79 
80 dmu_tx_t *
dmu_tx_create_assigned(struct dsl_pool * dp,uint64_t txg)81 dmu_tx_create_assigned(struct dsl_pool *dp, uint64_t txg)
82 {
83 	dmu_tx_t *tx = dmu_tx_create_dd(NULL);
84 
85 	TXG_VERIFY(dp->dp_spa, txg);
86 	tx->tx_pool = dp;
87 	tx->tx_txg = txg;
88 	tx->tx_anyobj = TRUE;
89 
90 	return (tx);
91 }
92 
93 int
dmu_tx_is_syncing(dmu_tx_t * tx)94 dmu_tx_is_syncing(dmu_tx_t *tx)
95 {
96 	return (tx->tx_anyobj);
97 }
98 
99 int
dmu_tx_private_ok(dmu_tx_t * tx)100 dmu_tx_private_ok(dmu_tx_t *tx)
101 {
102 	return (tx->tx_anyobj);
103 }
104 
105 static dmu_tx_hold_t *
dmu_tx_hold_dnode_impl(dmu_tx_t * tx,dnode_t * dn,enum dmu_tx_hold_type type,uint64_t arg1,uint64_t arg2)106 dmu_tx_hold_dnode_impl(dmu_tx_t *tx, dnode_t *dn, enum dmu_tx_hold_type type,
107     uint64_t arg1, uint64_t arg2)
108 {
109 	dmu_tx_hold_t *txh;
110 
111 	if (dn != NULL) {
112 		(void) zfs_refcount_add(&dn->dn_holds, tx);
113 		if (tx->tx_txg != 0) {
114 			mutex_enter(&dn->dn_mtx);
115 			/*
116 			 * dn->dn_assigned_txg == tx->tx_txg doesn't pose a
117 			 * problem, but there's no way for it to happen (for
118 			 * now, at least).
119 			 */
120 			ASSERT0(dn->dn_assigned_txg);
121 			dn->dn_assigned_txg = tx->tx_txg;
122 			(void) zfs_refcount_add(&dn->dn_tx_holds, tx);
123 			mutex_exit(&dn->dn_mtx);
124 		}
125 	}
126 
127 	txh = kmem_zalloc(sizeof (dmu_tx_hold_t), KM_SLEEP);
128 	txh->txh_tx = tx;
129 	txh->txh_dnode = dn;
130 	zfs_refcount_create(&txh->txh_space_towrite);
131 	zfs_refcount_create(&txh->txh_memory_tohold);
132 	txh->txh_type = type;
133 	txh->txh_arg1 = arg1;
134 	txh->txh_arg2 = arg2;
135 	list_insert_tail(&tx->tx_holds, txh);
136 
137 	return (txh);
138 }
139 
140 static dmu_tx_hold_t *
dmu_tx_hold_object_impl(dmu_tx_t * tx,objset_t * os,uint64_t object,enum dmu_tx_hold_type type,uint64_t arg1,uint64_t arg2)141 dmu_tx_hold_object_impl(dmu_tx_t *tx, objset_t *os, uint64_t object,
142     enum dmu_tx_hold_type type, uint64_t arg1, uint64_t arg2)
143 {
144 	dnode_t *dn = NULL;
145 	dmu_tx_hold_t *txh;
146 	int err;
147 
148 	if (object != DMU_NEW_OBJECT) {
149 		err = dnode_hold(os, object, FTAG, &dn);
150 		if (err != 0) {
151 			tx->tx_err = err;
152 			return (NULL);
153 		}
154 	}
155 	txh = dmu_tx_hold_dnode_impl(tx, dn, type, arg1, arg2);
156 	if (dn != NULL)
157 		dnode_rele(dn, FTAG);
158 	return (txh);
159 }
160 
161 void
dmu_tx_add_new_object(dmu_tx_t * tx,dnode_t * dn)162 dmu_tx_add_new_object(dmu_tx_t *tx, dnode_t *dn)
163 {
164 	/*
165 	 * If we're syncing, they can manipulate any object anyhow, and
166 	 * the hold on the dnode_t can cause problems.
167 	 */
168 	if (!dmu_tx_is_syncing(tx))
169 		(void) dmu_tx_hold_dnode_impl(tx, dn, THT_NEWOBJECT, 0, 0);
170 }
171 
172 /*
173  * This function reads specified data from disk.  The specified data will
174  * be needed to perform the transaction -- i.e, it will be read after
175  * we do dmu_tx_assign().  There are two reasons that we read the data now
176  * (before dmu_tx_assign()):
177  *
178  * 1. Reading it now has potentially better performance.  The transaction
179  * has not yet been assigned, so the TXG is not held open, and also the
180  * caller typically has less locks held when calling dmu_tx_hold_*() than
181  * after the transaction has been assigned.  This reduces the lock (and txg)
182  * hold times, thus reducing lock contention.
183  *
184  * 2. It is easier for callers (primarily the ZPL) to handle i/o errors
185  * that are detected before they start making changes to the DMU state
186  * (i.e. now).  Once the transaction has been assigned, and some DMU
187  * state has been changed, it can be difficult to recover from an i/o
188  * error (e.g. to undo the changes already made in memory at the DMU
189  * layer).  Typically code to do so does not exist in the caller -- it
190  * assumes that the data has already been cached and thus i/o errors are
191  * not possible.
192  *
193  * It has been observed that the i/o initiated here can be a performance
194  * problem, and it appears to be optional, because we don't look at the
195  * data which is read.  However, removing this read would only serve to
196  * move the work elsewhere (after the dmu_tx_assign()), where it may
197  * have a greater impact on performance (in addition to the impact on
198  * fault tolerance noted above).
199  */
200 static int
dmu_tx_check_ioerr(zio_t * zio,dnode_t * dn,int level,uint64_t blkid)201 dmu_tx_check_ioerr(zio_t *zio, dnode_t *dn, int level, uint64_t blkid)
202 {
203 	int err;
204 	dmu_buf_impl_t *db;
205 
206 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
207 	err = dbuf_hold_impl(dn, level, blkid, TRUE, FALSE, FTAG, &db);
208 	rw_exit(&dn->dn_struct_rwlock);
209 	if (err == ENOENT)
210 		return (0);
211 	if (err != 0)
212 		return (err);
213 	/*
214 	 * DMU_IS_PREFETCH keeps the buffer temporarily in DBUF cache and ARC
215 	 * to avoid immediate eviction after the check.  It will be promoted
216 	 * to demand access when dmu_buf_will_dirty() read it again.
217 	 */
218 	err = dbuf_read(db, zio, DB_RF_CANFAIL | DMU_READ_NO_PREFETCH |
219 	    (level == 0 ? (DMU_KEEP_CACHING | DMU_IS_PREFETCH) : 0));
220 	dbuf_rele(db, FTAG);
221 	return (err);
222 }
223 
224 static void
dmu_tx_count_write(dmu_tx_hold_t * txh,uint64_t off,uint64_t len)225 dmu_tx_count_write(dmu_tx_hold_t *txh, uint64_t off, uint64_t len)
226 {
227 	dnode_t *dn = txh->txh_dnode;
228 	int err = 0;
229 
230 	if (len == 0)
231 		return;
232 
233 	(void) zfs_refcount_add_many(&txh->txh_space_towrite, len, FTAG);
234 
235 	if (dn == NULL)
236 		return;
237 
238 	/*
239 	 * For i/o error checking, read the blocks that will be needed
240 	 * to perform the write: the first and last level-0 blocks (if
241 	 * they are not aligned, i.e. if they are partial-block writes),
242 	 * and all the level-1 blocks.
243 	 */
244 	if (dn->dn_maxblkid == 0) {
245 		if (off < dn->dn_datablksz &&
246 		    (off > 0 || len < dn->dn_datablksz)) {
247 			err = dmu_tx_check_ioerr(NULL, dn, 0, 0);
248 			if (err != 0) {
249 				txh->txh_tx->tx_err = err;
250 			}
251 		}
252 	} else {
253 		zio_t *zio = zio_root(dn->dn_objset->os_spa,
254 		    NULL, NULL, ZIO_FLAG_CANFAIL);
255 
256 		/* first level-0 block */
257 		uint64_t start = off >> dn->dn_datablkshift;
258 		if (P2PHASE(off, dn->dn_datablksz) || len < dn->dn_datablksz) {
259 			err = dmu_tx_check_ioerr(zio, dn, 0, start);
260 			if (err != 0) {
261 				txh->txh_tx->tx_err = err;
262 			}
263 		}
264 
265 		/* last level-0 block */
266 		uint64_t end = (off + len - 1) >> dn->dn_datablkshift;
267 		if (end != start && end <= dn->dn_maxblkid &&
268 		    P2PHASE(off + len, dn->dn_datablksz)) {
269 			err = dmu_tx_check_ioerr(zio, dn, 0, end);
270 			if (err != 0) {
271 				txh->txh_tx->tx_err = err;
272 			}
273 		}
274 
275 		/* level-1 blocks */
276 		if (dn->dn_nlevels > 1) {
277 			int shft = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
278 			for (uint64_t i = (start >> shft) + 1;
279 			    i < end >> shft; i++) {
280 				err = dmu_tx_check_ioerr(zio, dn, 1, i);
281 				if (err != 0) {
282 					txh->txh_tx->tx_err = err;
283 				}
284 			}
285 		}
286 
287 		err = zio_wait(zio);
288 		if (err != 0) {
289 			txh->txh_tx->tx_err = err;
290 		}
291 	}
292 }
293 
294 static void
dmu_tx_count_append(dmu_tx_hold_t * txh,uint64_t off,uint64_t len)295 dmu_tx_count_append(dmu_tx_hold_t *txh, uint64_t off, uint64_t len)
296 {
297 	dnode_t *dn = txh->txh_dnode;
298 	int err = 0;
299 
300 	if (len == 0)
301 		return;
302 
303 	(void) zfs_refcount_add_many(&txh->txh_space_towrite, len, FTAG);
304 
305 	if (dn == NULL)
306 		return;
307 
308 	/*
309 	 * For i/o error checking, read the blocks that will be needed
310 	 * to perform the append; first level-0 block (if not aligned, i.e.
311 	 * if they are partial-block writes), no additional blocks are read.
312 	 */
313 	if (dn->dn_maxblkid == 0) {
314 		if (off < dn->dn_datablksz &&
315 		    (off > 0 || len < dn->dn_datablksz)) {
316 			err = dmu_tx_check_ioerr(NULL, dn, 0, 0);
317 			if (err != 0) {
318 				txh->txh_tx->tx_err = err;
319 			}
320 		}
321 	} else {
322 		zio_t *zio = zio_root(dn->dn_objset->os_spa,
323 		    NULL, NULL, ZIO_FLAG_CANFAIL);
324 
325 		/* first level-0 block */
326 		uint64_t start = off >> dn->dn_datablkshift;
327 		if (P2PHASE(off, dn->dn_datablksz) || len < dn->dn_datablksz) {
328 			err = dmu_tx_check_ioerr(zio, dn, 0, start);
329 			if (err != 0) {
330 				txh->txh_tx->tx_err = err;
331 			}
332 		}
333 
334 		err = zio_wait(zio);
335 		if (err != 0) {
336 			txh->txh_tx->tx_err = err;
337 		}
338 	}
339 }
340 
341 static void
dmu_tx_count_dnode(dmu_tx_hold_t * txh)342 dmu_tx_count_dnode(dmu_tx_hold_t *txh)
343 {
344 	(void) zfs_refcount_add_many(&txh->txh_space_towrite,
345 	    DNODE_MIN_SIZE, FTAG);
346 }
347 
348 void
dmu_tx_hold_write(dmu_tx_t * tx,uint64_t object,uint64_t off,int len)349 dmu_tx_hold_write(dmu_tx_t *tx, uint64_t object, uint64_t off, int len)
350 {
351 	dmu_tx_hold_t *txh;
352 
353 	ASSERT0(tx->tx_txg);
354 	ASSERT3U(len, <=, DMU_MAX_ACCESS);
355 	ASSERT(len == 0 || UINT64_MAX - off >= len - 1);
356 
357 	txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
358 	    object, THT_WRITE, off, len);
359 	if (txh != NULL) {
360 		dmu_tx_count_write(txh, off, len);
361 		dmu_tx_count_dnode(txh);
362 	}
363 }
364 
365 void
dmu_tx_hold_write_by_dnode(dmu_tx_t * tx,dnode_t * dn,uint64_t off,int len)366 dmu_tx_hold_write_by_dnode(dmu_tx_t *tx, dnode_t *dn, uint64_t off, int len)
367 {
368 	dmu_tx_hold_t *txh;
369 
370 	ASSERT0(tx->tx_txg);
371 	ASSERT3U(len, <=, DMU_MAX_ACCESS);
372 	ASSERT(len == 0 || UINT64_MAX - off >= len - 1);
373 
374 	txh = dmu_tx_hold_dnode_impl(tx, dn, THT_WRITE, off, len);
375 	if (txh != NULL) {
376 		dmu_tx_count_write(txh, off, len);
377 		dmu_tx_count_dnode(txh);
378 	}
379 }
380 
381 /*
382  * Should be used when appending to an object and the exact offset is unknown.
383  * The write must occur at or beyond the specified offset.  Only the L0 block
384  * at provided offset will be prefetched.
385  */
386 void
dmu_tx_hold_append(dmu_tx_t * tx,uint64_t object,uint64_t off,int len)387 dmu_tx_hold_append(dmu_tx_t *tx, uint64_t object, uint64_t off, int len)
388 {
389 	dmu_tx_hold_t *txh;
390 
391 	ASSERT0(tx->tx_txg);
392 	ASSERT3U(len, <=, DMU_MAX_ACCESS);
393 
394 	txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
395 	    object, THT_APPEND, off, DMU_OBJECT_END);
396 	if (txh != NULL) {
397 		dmu_tx_count_append(txh, off, len);
398 		dmu_tx_count_dnode(txh);
399 	}
400 }
401 
402 void
dmu_tx_hold_append_by_dnode(dmu_tx_t * tx,dnode_t * dn,uint64_t off,int len)403 dmu_tx_hold_append_by_dnode(dmu_tx_t *tx, dnode_t *dn, uint64_t off, int len)
404 {
405 	dmu_tx_hold_t *txh;
406 
407 	ASSERT0(tx->tx_txg);
408 	ASSERT3U(len, <=, DMU_MAX_ACCESS);
409 
410 	txh = dmu_tx_hold_dnode_impl(tx, dn, THT_APPEND, off, DMU_OBJECT_END);
411 	if (txh != NULL) {
412 		dmu_tx_count_append(txh, off, len);
413 		dmu_tx_count_dnode(txh);
414 	}
415 }
416 
417 /*
418  * This function marks the transaction as being a "net free".  The end
419  * result is that refquotas will be disabled for this transaction, and
420  * this transaction will be able to use half of the pool space overhead
421  * (see dsl_pool_adjustedsize()).  Therefore this function should only
422  * be called for transactions that we expect will not cause a net increase
423  * in the amount of space used (but it's OK if that is occasionally not true).
424  */
425 void
dmu_tx_mark_netfree(dmu_tx_t * tx)426 dmu_tx_mark_netfree(dmu_tx_t *tx)
427 {
428 	tx->tx_netfree = B_TRUE;
429 }
430 
431 static void
dmu_tx_count_free(dmu_tx_hold_t * txh,uint64_t off,uint64_t len)432 dmu_tx_count_free(dmu_tx_hold_t *txh, uint64_t off, uint64_t len)
433 {
434 	dmu_tx_t *tx = txh->txh_tx;
435 	dnode_t *dn = txh->txh_dnode;
436 	int err;
437 
438 	ASSERT0(tx->tx_txg);
439 
440 	if (off >= (dn->dn_maxblkid + 1) * dn->dn_datablksz)
441 		return;
442 	if (len == DMU_OBJECT_END)
443 		len = (dn->dn_maxblkid + 1) * dn->dn_datablksz - off;
444 
445 	/*
446 	 * For i/o error checking, we read the first and last level-0
447 	 * blocks if they are not aligned, and all the level-1 blocks.
448 	 *
449 	 * Note:  dbuf_free_range() assumes that we have not instantiated
450 	 * any level-0 dbufs that will be completely freed.  Therefore we must
451 	 * exercise care to not read or count the first and last blocks
452 	 * if they are blocksize-aligned.
453 	 */
454 	if (dn->dn_datablkshift == 0) {
455 		if (off != 0 || len < dn->dn_datablksz)
456 			dmu_tx_count_write(txh, 0, dn->dn_datablksz);
457 	} else {
458 		/* first block will be modified if it is not aligned */
459 		if (!IS_P2ALIGNED(off, 1 << dn->dn_datablkshift))
460 			dmu_tx_count_write(txh, off, 1);
461 		/* last block will be modified if it is not aligned */
462 		if (!IS_P2ALIGNED(off + len, 1 << dn->dn_datablkshift))
463 			dmu_tx_count_write(txh, off + len, 1);
464 	}
465 
466 	/*
467 	 * Check level-1 blocks.
468 	 */
469 	if (dn->dn_nlevels > 1) {
470 		int shift = dn->dn_datablkshift + dn->dn_indblkshift -
471 		    SPA_BLKPTRSHIFT;
472 		uint64_t start = off >> shift;
473 		uint64_t end = (off + len) >> shift;
474 
475 		ASSERT(dn->dn_indblkshift != 0);
476 
477 		/*
478 		 * dnode_reallocate() can result in an object with indirect
479 		 * blocks having an odd data block size.  In this case,
480 		 * just check the single block.
481 		 */
482 		if (dn->dn_datablkshift == 0)
483 			start = end = 0;
484 
485 		zio_t *zio = zio_root(tx->tx_pool->dp_spa,
486 		    NULL, NULL, ZIO_FLAG_CANFAIL);
487 		for (uint64_t i = start; i <= end; i++) {
488 			uint64_t ibyte = i << shift;
489 			err = dnode_next_offset(dn, 0, &ibyte, 2, 1, 0);
490 			i = ibyte >> shift;
491 			if (err == ESRCH || i > end)
492 				break;
493 			if (err != 0) {
494 				tx->tx_err = err;
495 				(void) zio_wait(zio);
496 				return;
497 			}
498 
499 			(void) zfs_refcount_add_many(&txh->txh_memory_tohold,
500 			    1 << dn->dn_indblkshift, FTAG);
501 
502 			err = dmu_tx_check_ioerr(zio, dn, 1, i);
503 			if (err != 0) {
504 				tx->tx_err = err;
505 				(void) zio_wait(zio);
506 				return;
507 			}
508 		}
509 		err = zio_wait(zio);
510 		if (err != 0) {
511 			tx->tx_err = err;
512 			return;
513 		}
514 	}
515 }
516 
517 void
dmu_tx_hold_free(dmu_tx_t * tx,uint64_t object,uint64_t off,uint64_t len)518 dmu_tx_hold_free(dmu_tx_t *tx, uint64_t object, uint64_t off, uint64_t len)
519 {
520 	dmu_tx_hold_t *txh;
521 
522 	txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
523 	    object, THT_FREE, off, len);
524 	if (txh != NULL) {
525 		dmu_tx_count_dnode(txh);
526 		dmu_tx_count_free(txh, off, len);
527 	}
528 }
529 
530 void
dmu_tx_hold_free_by_dnode(dmu_tx_t * tx,dnode_t * dn,uint64_t off,uint64_t len)531 dmu_tx_hold_free_by_dnode(dmu_tx_t *tx, dnode_t *dn, uint64_t off, uint64_t len)
532 {
533 	dmu_tx_hold_t *txh;
534 
535 	txh = dmu_tx_hold_dnode_impl(tx, dn, THT_FREE, off, len);
536 	if (txh != NULL) {
537 		dmu_tx_count_dnode(txh);
538 		dmu_tx_count_free(txh, off, len);
539 	}
540 }
541 
542 static void
dmu_tx_count_clone(dmu_tx_hold_t * txh,uint64_t off,uint64_t len,uint_t blksz)543 dmu_tx_count_clone(dmu_tx_hold_t *txh, uint64_t off, uint64_t len,
544     uint_t blksz)
545 {
546 	dmu_tx_t *tx = txh->txh_tx;
547 	dnode_t *dn = txh->txh_dnode;
548 	int err;
549 
550 	ASSERT0(tx->tx_txg);
551 	ASSERT(dn->dn_indblkshift != 0);
552 	ASSERT(blksz != 0);
553 	ASSERT0(off % blksz);
554 
555 	/*
556 	 * The last block of the range is cloned in full even if the range
557 	 * covers it only partially, in case it is the last block of the file.
558 	 */
559 	len = P2ROUNDUP(len, (uint64_t)blksz);
560 
561 	(void) zfs_refcount_add_many(&txh->txh_memory_tohold,
562 	    len / blksz * sizeof (brt_entry_t), FTAG);
563 
564 	int shift = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
565 	uint64_t start = off / blksz >> shift;
566 	uint64_t end = (off + len) / blksz >> shift;
567 
568 	(void) zfs_refcount_add_many(&txh->txh_space_towrite,
569 	    (end - start + 1) << dn->dn_indblkshift, FTAG);
570 
571 	zio_t *zio = zio_root(tx->tx_pool->dp_spa,
572 	    NULL, NULL, ZIO_FLAG_CANFAIL);
573 	for (uint64_t i = start; i <= end; i++) {
574 		err = dmu_tx_check_ioerr(zio, dn, 1, i);
575 		if (err != 0) {
576 			tx->tx_err = err;
577 			(void) zio_wait(zio);
578 			return;
579 		}
580 	}
581 	err = zio_wait(zio);
582 	if (err != 0) {
583 		tx->tx_err = err;
584 		return;
585 	}
586 
587 	/*
588 	 * Each cloned block may dirty one BRT ZAP leaf in sync context.
589 	 */
590 	tx->tx_sync_reserve += len / blksz *
591 	    brt_sync_dirty_est(tx->tx_pool->dp_spa);
592 }
593 
594 void
dmu_tx_hold_clone_by_dnode(dmu_tx_t * tx,dnode_t * dn,uint64_t off,uint64_t len,uint_t blksz)595 dmu_tx_hold_clone_by_dnode(dmu_tx_t *tx, dnode_t *dn, uint64_t off,
596     uint64_t len, uint_t blksz)
597 {
598 	dmu_tx_hold_t *txh;
599 
600 	ASSERT0(tx->tx_txg);
601 	ASSERT(len == 0 || UINT64_MAX - off >= len - 1);
602 
603 	txh = dmu_tx_hold_dnode_impl(tx, dn, THT_CLONE, off, len);
604 	if (txh != NULL) {
605 		dmu_tx_count_dnode(txh);
606 		dmu_tx_count_clone(txh, off, len, blksz);
607 	}
608 }
609 
610 static void
dmu_tx_hold_zap_impl(dmu_tx_hold_t * txh,const char * name)611 dmu_tx_hold_zap_impl(dmu_tx_hold_t *txh, const char *name)
612 {
613 	dmu_tx_t *tx = txh->txh_tx;
614 	dnode_t *dn = txh->txh_dnode;
615 	int err;
616 
617 	ASSERT0(tx->tx_txg);
618 
619 	dmu_tx_count_dnode(txh);
620 
621 	/*
622 	 * Modifying a almost-full microzap is around the worst case (128KB)
623 	 *
624 	 * If it is a fat zap, the worst case would be 7*16KB=112KB:
625 	 * - 3 blocks overwritten: target leaf, ptrtbl block, header block
626 	 * - 4 new blocks written if adding:
627 	 *    - 2 blocks for possibly split leaves,
628 	 *    - 2 grown ptrtbl blocks
629 	 */
630 	(void) zfs_refcount_add_many(&txh->txh_space_towrite,
631 	    zap_get_micro_max_size(tx->tx_pool->dp_spa), FTAG);
632 
633 	if (dn == NULL)
634 		return;
635 
636 	ASSERT3U(DMU_OT_BYTESWAP(dn->dn_type), ==, DMU_BSWAP_ZAP);
637 
638 	if (dn->dn_maxblkid == 0 || name == NULL) {
639 		/*
640 		 * This is a microzap (only one block), or we don't know
641 		 * the name.  Check the first block for i/o errors.
642 		 */
643 		err = dmu_tx_check_ioerr(NULL, dn, 0, 0);
644 		if (err != 0) {
645 			tx->tx_err = err;
646 		}
647 	} else {
648 		/*
649 		 * Access the name so that we'll check for i/o errors to
650 		 * the leaf blocks, etc.  We ignore ENOENT, as this name
651 		 * may not yet exist.
652 		 */
653 		err = zap_lookup_by_dnode(dn, name, 8, 0, NULL);
654 		if (err == EIO || err == ECKSUM || err == ENXIO) {
655 			tx->tx_err = err;
656 		}
657 	}
658 }
659 
660 void
dmu_tx_hold_zap(dmu_tx_t * tx,uint64_t object,int add,const char * name)661 dmu_tx_hold_zap(dmu_tx_t *tx, uint64_t object, int add, const char *name)
662 {
663 	dmu_tx_hold_t *txh;
664 
665 	ASSERT0(tx->tx_txg);
666 
667 	txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
668 	    object, THT_ZAP, add, (uintptr_t)name);
669 	if (txh != NULL)
670 		dmu_tx_hold_zap_impl(txh, name);
671 }
672 
673 void
dmu_tx_hold_zap_by_dnode(dmu_tx_t * tx,dnode_t * dn,int add,const char * name)674 dmu_tx_hold_zap_by_dnode(dmu_tx_t *tx, dnode_t *dn, int add, const char *name)
675 {
676 	dmu_tx_hold_t *txh;
677 
678 	ASSERT0(tx->tx_txg);
679 	ASSERT(dn != NULL);
680 
681 	txh = dmu_tx_hold_dnode_impl(tx, dn, THT_ZAP, add, (uintptr_t)name);
682 	if (txh != NULL)
683 		dmu_tx_hold_zap_impl(txh, name);
684 }
685 
686 void
dmu_tx_hold_bonus(dmu_tx_t * tx,uint64_t object)687 dmu_tx_hold_bonus(dmu_tx_t *tx, uint64_t object)
688 {
689 	dmu_tx_hold_t *txh;
690 
691 	ASSERT0(tx->tx_txg);
692 
693 	txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
694 	    object, THT_BONUS, 0, 0);
695 	if (txh)
696 		dmu_tx_count_dnode(txh);
697 }
698 
699 void
dmu_tx_hold_bonus_by_dnode(dmu_tx_t * tx,dnode_t * dn)700 dmu_tx_hold_bonus_by_dnode(dmu_tx_t *tx, dnode_t *dn)
701 {
702 	dmu_tx_hold_t *txh;
703 
704 	ASSERT0(tx->tx_txg);
705 
706 	txh = dmu_tx_hold_dnode_impl(tx, dn, THT_BONUS, 0, 0);
707 	if (txh)
708 		dmu_tx_count_dnode(txh);
709 }
710 
711 void
dmu_tx_hold_space(dmu_tx_t * tx,uint64_t space)712 dmu_tx_hold_space(dmu_tx_t *tx, uint64_t space)
713 {
714 	dmu_tx_hold_t *txh;
715 
716 	ASSERT0(tx->tx_txg);
717 
718 	txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
719 	    DMU_NEW_OBJECT, THT_SPACE, space, 0);
720 	if (txh) {
721 		(void) zfs_refcount_add_many(
722 		    &txh->txh_space_towrite, space, FTAG);
723 	}
724 }
725 
726 #ifdef ZFS_DEBUG
727 void
dmu_tx_dirty_buf(dmu_tx_t * tx,dmu_buf_impl_t * db)728 dmu_tx_dirty_buf(dmu_tx_t *tx, dmu_buf_impl_t *db)
729 {
730 	boolean_t match_object = B_FALSE;
731 	boolean_t match_offset = B_FALSE;
732 
733 	DB_DNODE_ENTER(db);
734 	dnode_t *dn = DB_DNODE(db);
735 	ASSERT(tx->tx_txg != 0);
736 	ASSERT(tx->tx_objset == NULL || dn->dn_objset == tx->tx_objset);
737 	ASSERT3U(dn->dn_object, ==, db->db.db_object);
738 
739 	if (tx->tx_anyobj) {
740 		DB_DNODE_EXIT(db);
741 		return;
742 	}
743 
744 	/* XXX No checking on the meta dnode for now */
745 	if (db->db.db_object == DMU_META_DNODE_OBJECT) {
746 		DB_DNODE_EXIT(db);
747 		return;
748 	}
749 
750 	for (dmu_tx_hold_t *txh = list_head(&tx->tx_holds); txh != NULL;
751 	    txh = list_next(&tx->tx_holds, txh)) {
752 		ASSERT3U(dn->dn_assigned_txg, ==, tx->tx_txg);
753 		if (txh->txh_dnode == dn && txh->txh_type != THT_NEWOBJECT)
754 			match_object = TRUE;
755 		if (txh->txh_dnode == NULL || txh->txh_dnode == dn) {
756 			int datablkshift = dn->dn_datablkshift ?
757 			    dn->dn_datablkshift : SPA_MAXBLOCKSHIFT;
758 			int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
759 			int shift = datablkshift + epbs * db->db_level;
760 			uint64_t beginblk = shift >= 64 ? 0 :
761 			    (txh->txh_arg1 >> shift);
762 			uint64_t endblk = shift >= 64 ? 0 :
763 			    ((txh->txh_arg1 + txh->txh_arg2 - 1) >> shift);
764 			uint64_t blkid = db->db_blkid;
765 
766 			/* XXX txh_arg2 better not be zero... */
767 
768 			dprintf("found txh type %x beginblk=%llx endblk=%llx\n",
769 			    txh->txh_type, (u_longlong_t)beginblk,
770 			    (u_longlong_t)endblk);
771 
772 			switch (txh->txh_type) {
773 			case THT_WRITE:
774 				if (blkid >= beginblk && blkid <= endblk)
775 					match_offset = TRUE;
776 				/*
777 				 * We will let this hold work for the bonus
778 				 * or spill buffer so that we don't need to
779 				 * hold it when creating a new object.
780 				 */
781 				if (blkid == DMU_BONUS_BLKID ||
782 				    blkid == DMU_SPILL_BLKID)
783 					match_offset = TRUE;
784 				/*
785 				 * They might have to increase nlevels,
786 				 * thus dirtying the new TLIBs.  Or the
787 				 * might have to change the block size,
788 				 * thus dirying the new lvl=0 blk=0.
789 				 */
790 				if (blkid == 0)
791 					match_offset = TRUE;
792 				break;
793 			case THT_APPEND:
794 				if (blkid >= beginblk && (blkid <= endblk ||
795 				    txh->txh_arg2 == DMU_OBJECT_END))
796 					match_offset = TRUE;
797 
798 				/*
799 				 * THT_WRITE used for bonus and spill blocks.
800 				 */
801 				ASSERT(blkid != DMU_BONUS_BLKID &&
802 				    blkid != DMU_SPILL_BLKID);
803 
804 				/*
805 				 * They might have to increase nlevels,
806 				 * thus dirtying the new TLIBs.  Or the
807 				 * might have to change the block size,
808 				 * thus dirying the new lvl=0 blk=0.
809 				 */
810 				if (blkid == 0)
811 					match_offset = TRUE;
812 				break;
813 			case THT_FREE:
814 				/*
815 				 * We will dirty all the level 1 blocks in
816 				 * the free range and perhaps the first and
817 				 * last level 0 block.
818 				 */
819 				if (blkid >= beginblk && (blkid <= endblk ||
820 				    txh->txh_arg2 == DMU_OBJECT_END))
821 					match_offset = TRUE;
822 				break;
823 			case THT_SPILL:
824 				if (blkid == DMU_SPILL_BLKID)
825 					match_offset = TRUE;
826 				break;
827 			case THT_BONUS:
828 				if (blkid == DMU_BONUS_BLKID)
829 					match_offset = TRUE;
830 				break;
831 			case THT_ZAP:
832 				match_offset = TRUE;
833 				break;
834 			case THT_NEWOBJECT:
835 				match_object = TRUE;
836 				break;
837 			case THT_CLONE:
838 				if (blkid >= beginblk && blkid <= endblk)
839 					match_offset = TRUE;
840 				/*
841 				 * They might have to increase nlevels,
842 				 * thus dirtying the new TLIBs.  Or the
843 				 * might have to change the block size,
844 				 * thus dirying the new lvl=0 blk=0.
845 				 */
846 				if (blkid == 0)
847 					match_offset = TRUE;
848 				break;
849 			default:
850 				cmn_err(CE_PANIC, "bad txh_type %d",
851 				    txh->txh_type);
852 			}
853 		}
854 		if (match_object && match_offset) {
855 			DB_DNODE_EXIT(db);
856 			return;
857 		}
858 	}
859 	DB_DNODE_EXIT(db);
860 	panic("dirtying dbuf obj=%llx lvl=%u blkid=%llx but not tx_held\n",
861 	    (u_longlong_t)db->db.db_object, db->db_level,
862 	    (u_longlong_t)db->db_blkid);
863 }
864 #endif
865 
866 /*
867  * If we can't do 10 iops, something is wrong.  Let us go ahead
868  * and hit zfs_dirty_data_max.
869  */
870 static const hrtime_t zfs_delay_max_ns = 100 * MICROSEC; /* 100 milliseconds */
871 
872 /*
873  * We delay transactions when we've determined that the backend storage
874  * isn't able to accommodate the rate of incoming writes.
875  *
876  * If there is already a transaction waiting, we delay relative to when
877  * that transaction finishes waiting.  This way the calculated min_time
878  * is independent of the number of threads concurrently executing
879  * transactions.
880  *
881  * If we are the only waiter, wait relative to when the transaction
882  * started, rather than the current time.  This credits the transaction for
883  * "time already served", e.g. reading indirect blocks.
884  *
885  * The minimum time for a transaction to take is calculated as:
886  *     min_time = scale * (dirty - min) / (max - dirty)
887  *     min_time is then capped at zfs_delay_max_ns.
888  *
889  * The delay has two degrees of freedom that can be adjusted via tunables.
890  * The percentage of dirty data at which we start to delay is defined by
891  * zfs_delay_min_dirty_percent. This should typically be at or above
892  * zfs_vdev_async_write_active_max_dirty_percent so that we only start to
893  * delay after writing at full speed has failed to keep up with the incoming
894  * write rate. The scale of the curve is defined by zfs_delay_scale. Roughly
895  * speaking, this variable determines the amount of delay at the midpoint of
896  * the curve.
897  *
898  * delay
899  *  10ms +-------------------------------------------------------------*+
900  *       |                                                             *|
901  *   9ms +                                                             *+
902  *       |                                                             *|
903  *   8ms +                                                             *+
904  *       |                                                            * |
905  *   7ms +                                                            * +
906  *       |                                                            * |
907  *   6ms +                                                            * +
908  *       |                                                            * |
909  *   5ms +                                                           *  +
910  *       |                                                           *  |
911  *   4ms +                                                           *  +
912  *       |                                                           *  |
913  *   3ms +                                                          *   +
914  *       |                                                          *   |
915  *   2ms +                                              (midpoint) *    +
916  *       |                                                  |    **     |
917  *   1ms +                                                  v ***       +
918  *       |             zfs_delay_scale ---------->     ********         |
919  *     0 +-------------------------------------*********----------------+
920  *       0%                    <- zfs_dirty_data_max ->               100%
921  *
922  * Note that since the delay is added to the outstanding time remaining on the
923  * most recent transaction, the delay is effectively the inverse of IOPS.
924  * Here the midpoint of 500us translates to 2000 IOPS. The shape of the curve
925  * was chosen such that small changes in the amount of accumulated dirty data
926  * in the first 3/4 of the curve yield relatively small differences in the
927  * amount of delay.
928  *
929  * The effects can be easier to understand when the amount of delay is
930  * represented on a log scale:
931  *
932  * delay
933  * 100ms +-------------------------------------------------------------++
934  *       +                                                              +
935  *       |                                                              |
936  *       +                                                             *+
937  *  10ms +                                                             *+
938  *       +                                                           ** +
939  *       |                                              (midpoint)  **  |
940  *       +                                                  |     **    +
941  *   1ms +                                                  v ****      +
942  *       +             zfs_delay_scale ---------->        *****         +
943  *       |                                             ****             |
944  *       +                                          ****                +
945  * 100us +                                        **                    +
946  *       +                                       *                      +
947  *       |                                      *                       |
948  *       +                                     *                        +
949  *  10us +                                     *                        +
950  *       +                                                              +
951  *       |                                                              |
952  *       +                                                              +
953  *       +--------------------------------------------------------------+
954  *       0%                    <- zfs_dirty_data_max ->               100%
955  *
956  * Note here that only as the amount of dirty data approaches its limit does
957  * the delay start to increase rapidly. The goal of a properly tuned system
958  * should be to keep the amount of dirty data out of that range by first
959  * ensuring that the appropriate limits are set for the I/O scheduler to reach
960  * optimal throughput on the backend storage, and then by changing the value
961  * of zfs_delay_scale to increase the steepness of the curve.
962  */
963 static void
dmu_tx_delay(dmu_tx_t * tx,uint64_t dirty)964 dmu_tx_delay(dmu_tx_t *tx, uint64_t dirty)
965 {
966 	dsl_pool_t *dp = tx->tx_pool;
967 	uint64_t delay_min_bytes, wrlog;
968 	hrtime_t wakeup, tx_time = 0, now;
969 
970 	/* Calculate minimum transaction time for the dirty data amount. */
971 	delay_min_bytes =
972 	    zfs_dirty_data_max * zfs_delay_min_dirty_percent / 100;
973 	if (dirty >= zfs_dirty_data_max) {
974 		/*
975 		 * The caller has already waited until the dirty data is
976 		 * under the max, but the sync context reservations added
977 		 * on top of it may push the sum beyond it.
978 		 */
979 		tx_time = zfs_delay_max_ns;
980 	} else if (dirty > delay_min_bytes) {
981 		tx_time = zfs_delay_scale * (dirty - delay_min_bytes) /
982 		    (zfs_dirty_data_max - dirty);
983 	}
984 
985 	/* Calculate minimum transaction time for the TX_WRITE log size. */
986 	wrlog = aggsum_upper_bound(&dp->dp_wrlog_total);
987 	delay_min_bytes =
988 	    zfs_wrlog_data_max * zfs_delay_min_dirty_percent / 100;
989 	if (wrlog >= zfs_wrlog_data_max) {
990 		tx_time = zfs_delay_max_ns;
991 	} else if (wrlog > delay_min_bytes) {
992 		tx_time = MAX(zfs_delay_scale * (wrlog - delay_min_bytes) /
993 		    (zfs_wrlog_data_max - wrlog), tx_time);
994 	}
995 
996 	if (tx_time == 0)
997 		return;
998 
999 	tx_time = MIN(tx_time, zfs_delay_max_ns);
1000 	now = gethrtime();
1001 	if (now > tx->tx_start + tx_time)
1002 		return;
1003 
1004 	DTRACE_PROBE3(delay__mintime, dmu_tx_t *, tx, uint64_t, dirty,
1005 	    uint64_t, tx_time);
1006 
1007 	mutex_enter(&dp->dp_lock);
1008 	wakeup = MAX(tx->tx_start + tx_time, dp->dp_last_wakeup + tx_time);
1009 	dp->dp_last_wakeup = wakeup;
1010 	mutex_exit(&dp->dp_lock);
1011 
1012 	zfs_sleep_until(wakeup);
1013 }
1014 
1015 /*
1016  * This routine attempts to assign the transaction to a transaction group.
1017  * To do so, we must determine if there is sufficient free space on disk.
1018  *
1019  * If this is a "netfree" transaction (i.e. we called dmu_tx_mark_netfree()
1020  * on it), then it is assumed that there is sufficient free space,
1021  * unless there's insufficient slop space in the pool (see the comment
1022  * above spa_slop_shift in spa_misc.c).
1023  *
1024  * If it is not a "netfree" transaction, then if the data already on disk
1025  * is over the allowed usage (e.g. quota), this will fail with EDQUOT or
1026  * ENOSPC.  Otherwise, if the current rough estimate of pending changes,
1027  * plus the rough estimate of this transaction's changes, may exceed the
1028  * allowed usage, then this will fail with ERESTART, which will cause the
1029  * caller to wait for the pending changes to be written to disk (by waiting
1030  * for the next TXG to open), and then check the space usage again.
1031  *
1032  * The rough estimate of pending changes is comprised of the sum of:
1033  *
1034  *  - this transaction's holds' txh_space_towrite
1035  *
1036  *  - dd_tempreserved[], which is the sum of in-flight transactions'
1037  *    holds' txh_space_towrite (i.e. those transactions that have called
1038  *    dmu_tx_assign() but not yet called dmu_tx_commit()).
1039  *
1040  *  - dd_space_towrite[], which is the amount of dirtied dbufs.
1041  *
1042  * Note that all of these values are inflated by spa_get_worst_case_asize(),
1043  * which means that we may get ERESTART well before we are actually in danger
1044  * of running out of space, but this also mitigates any small inaccuracies
1045  * in the rough estimate (e.g. txh_space_towrite doesn't take into account
1046  * indirect blocks, and dd_space_towrite[] doesn't take into account changes
1047  * to the MOS).
1048  *
1049  * Note that due to this algorithm, it is possible to exceed the allowed
1050  * usage by one transaction.  Also, as we approach the allowed usage,
1051  * we will allow a very limited amount of changes into each TXG, thus
1052  * decreasing performance.
1053  */
1054 static int
dmu_tx_try_assign(dmu_tx_t * tx)1055 dmu_tx_try_assign(dmu_tx_t *tx)
1056 {
1057 	spa_t *spa = tx->tx_pool->dp_spa;
1058 
1059 	ASSERT0(tx->tx_txg);
1060 
1061 	if (tx->tx_err) {
1062 		DMU_TX_STAT_BUMP(dmu_tx_error);
1063 		return (SET_ERROR(EIO));
1064 	}
1065 
1066 	if (spa_suspended(spa)) {
1067 		DMU_TX_STAT_BUMP(dmu_tx_suspended);
1068 
1069 		/*
1070 		 * Let dmu_tx_assign() know specifically what happened, so
1071 		 * it can make the right choice based on the caller flags.
1072 		 */
1073 		return (SET_ERROR(ESHUTDOWN));
1074 	}
1075 
1076 	if (!tx->tx_dirty_delayed &&
1077 	    dsl_pool_need_wrlog_delay(tx->tx_pool)) {
1078 		tx->tx_wait_dirty = B_TRUE;
1079 		DMU_TX_STAT_BUMP(dmu_tx_wrlog_delay);
1080 		return (SET_ERROR(ERESTART));
1081 	}
1082 
1083 	if (!tx->tx_dirty_delayed &&
1084 	    dsl_pool_need_dirty_delay(tx->tx_pool)) {
1085 		tx->tx_wait_dirty = B_TRUE;
1086 		DMU_TX_STAT_BUMP(dmu_tx_dirty_delay);
1087 		return (SET_ERROR(ERESTART));
1088 	}
1089 
1090 	tx->tx_txg = txg_hold_open(tx->tx_pool, &tx->tx_txgh);
1091 	tx->tx_needassign_txh = NULL;
1092 
1093 	/*
1094 	 * NB: No error returns are allowed after txg_hold_open, but
1095 	 * before processing the dnode holds, due to the
1096 	 * dmu_tx_unassign() logic.
1097 	 */
1098 
1099 	uint64_t towrite = 0;
1100 	uint64_t tohold = 0;
1101 	for (dmu_tx_hold_t *txh = list_head(&tx->tx_holds); txh != NULL;
1102 	    txh = list_next(&tx->tx_holds, txh)) {
1103 		dnode_t *dn = txh->txh_dnode;
1104 		if (dn != NULL) {
1105 			/*
1106 			 * This thread can't hold the dn_struct_rwlock
1107 			 * while assigning the tx, because this can lead to
1108 			 * deadlock. Specifically, if this dnode is already
1109 			 * assigned to an earlier txg, this thread may need
1110 			 * to wait for that txg to sync (the ERESTART case
1111 			 * below).  The other thread that has assigned this
1112 			 * dnode to an earlier txg prevents this txg from
1113 			 * syncing until its tx can complete (calling
1114 			 * dmu_tx_commit()), but it may need to acquire the
1115 			 * dn_struct_rwlock to do so (e.g. via
1116 			 * dmu_buf_hold*()).
1117 			 *
1118 			 * Note that this thread can't hold the lock for
1119 			 * read either, but the rwlock doesn't record
1120 			 * enough information to make that assertion.
1121 			 */
1122 			ASSERT(!RW_WRITE_HELD(&dn->dn_struct_rwlock));
1123 
1124 			mutex_enter(&dn->dn_mtx);
1125 			if (dn->dn_assigned_txg == tx->tx_txg - 1) {
1126 				mutex_exit(&dn->dn_mtx);
1127 				tx->tx_needassign_txh = txh;
1128 				DMU_TX_STAT_BUMP(dmu_tx_group);
1129 				return (SET_ERROR(ERESTART));
1130 			}
1131 			if (dn->dn_assigned_txg == 0)
1132 				dn->dn_assigned_txg = tx->tx_txg;
1133 			ASSERT3U(dn->dn_assigned_txg, ==, tx->tx_txg);
1134 			(void) zfs_refcount_add(&dn->dn_tx_holds, tx);
1135 			mutex_exit(&dn->dn_mtx);
1136 		}
1137 		towrite += zfs_refcount_count(&txh->txh_space_towrite);
1138 		tohold += zfs_refcount_count(&txh->txh_memory_tohold);
1139 	}
1140 
1141 	/* needed allocation: worst-case estimate of write space */
1142 	uint64_t asize = spa_get_worst_case_asize(tx->tx_pool->dp_spa, towrite);
1143 	/* calculate memory footprint estimate */
1144 	uint64_t memory = towrite + tohold;
1145 
1146 	if (tx->tx_dir != NULL && asize != 0) {
1147 		int err = dsl_dir_tempreserve_space(tx->tx_dir, memory,
1148 		    asize, tx->tx_netfree, &tx->tx_tempreserve_cookie, tx);
1149 		if (err != 0)
1150 			return (err);
1151 	}
1152 
1153 	dsl_pool_sync_reserve(tx->tx_pool, tx->tx_sync_reserve, tx);
1154 
1155 	DMU_TX_STAT_BUMP(dmu_tx_assigned);
1156 
1157 	return (0);
1158 }
1159 
1160 static void
dmu_tx_unassign(dmu_tx_t * tx)1161 dmu_tx_unassign(dmu_tx_t *tx)
1162 {
1163 	if (tx->tx_txg == 0)
1164 		return;
1165 
1166 	txg_rele_to_quiesce(&tx->tx_txgh);
1167 
1168 	/*
1169 	 * Walk the transaction's hold list, removing the hold on the
1170 	 * associated dnode, and notifying waiters if the refcount drops to 0.
1171 	 */
1172 	for (dmu_tx_hold_t *txh = list_head(&tx->tx_holds);
1173 	    txh && txh != tx->tx_needassign_txh;
1174 	    txh = list_next(&tx->tx_holds, txh)) {
1175 		dnode_t *dn = txh->txh_dnode;
1176 
1177 		if (dn == NULL)
1178 			continue;
1179 		mutex_enter(&dn->dn_mtx);
1180 		ASSERT3U(dn->dn_assigned_txg, ==, tx->tx_txg);
1181 
1182 		if (zfs_refcount_remove(&dn->dn_tx_holds, tx) == 0) {
1183 			dn->dn_assigned_txg = 0;
1184 			cv_broadcast(&dn->dn_notxholds);
1185 		}
1186 		mutex_exit(&dn->dn_mtx);
1187 	}
1188 
1189 	txg_rele_to_sync(&tx->tx_txgh);
1190 
1191 	tx->tx_lasttried_txg = tx->tx_txg;
1192 	tx->tx_txg = 0;
1193 }
1194 
1195 /*
1196  * Assign tx to a transaction group; `flags` is a bitmask:
1197  *
1198  * If DMU_TX_WAIT is set and the currently open txg is full, this function
1199  * will wait until there's a new txg. This should be used when no locks
1200  * are being held. With this bit set, this function will only fail if
1201  * we're truly out of space (ENOSPC), over quota (EDQUOT), or required
1202  * data for the transaction could not be read from disk (EIO).
1203  *
1204  * If DMU_TX_WAIT is *not* set and we can't assign into the currently open
1205  * txg without blocking, this function will return immediately with
1206  * ERESTART. This should be used whenever locks are being held.  On an
1207  * ERESTART error, the caller should drop all locks, call dmu_tx_wait(),
1208  * and try again.
1209  *
1210  * If DMU_TX_NOTHROTTLE is set, this indicates that this tx should not be
1211  * delayed due on the ZFS Write Throttle (see comments in dsl_pool.c for
1212  * details on the throttle). This is used by the VFS operations, after
1213  * they have already called dmu_tx_wait() (though most likely on a
1214  * different tx).
1215  *
1216  * If DMU_TX_SUSPEND is set, this indicates that this tx should ignore
1217  * the pool being or becoming suspending while it is in progress. This will
1218  * cause dmu_tx_assign() (and dmu_tx_wait()) to block until the pool resumes.
1219  * If this flag is not set and the pool suspends, the return will be either
1220  * ERESTART or EIO, depending on the value of the pool's failmode= property.
1221  *
1222  * It is guaranteed that subsequent successful calls to dmu_tx_assign()
1223  * will assign the tx to monotonically increasing txgs. Of course this is
1224  * not strong monotonicity, because the same txg can be returned multiple
1225  * times in a row. This guarantee holds both for subsequent calls from
1226  * one thread and for multiple threads. For example, it is impossible to
1227  * observe the following sequence of events:
1228  *
1229  *          Thread 1                            Thread 2
1230  *
1231  *     dmu_tx_assign(T1, ...)
1232  *     1 <- dmu_tx_get_txg(T1)
1233  *                                       dmu_tx_assign(T2, ...)
1234  *                                       2 <- dmu_tx_get_txg(T2)
1235  *     dmu_tx_assign(T3, ...)
1236  *     1 <- dmu_tx_get_txg(T3)
1237  */
1238 int
dmu_tx_assign(dmu_tx_t * tx,dmu_tx_flag_t flags)1239 dmu_tx_assign(dmu_tx_t *tx, dmu_tx_flag_t flags)
1240 {
1241 	int err;
1242 
1243 	ASSERT0(tx->tx_txg);
1244 	ASSERT0(flags & ~(DMU_TX_WAIT | DMU_TX_NOTHROTTLE | DMU_TX_SUSPEND));
1245 	IMPLY(flags & DMU_TX_SUSPEND, flags & DMU_TX_WAIT);
1246 	ASSERT(!dsl_pool_sync_context(tx->tx_pool));
1247 
1248 	/* If we might wait, we must not hold the config lock. */
1249 	IMPLY((flags & DMU_TX_WAIT), !dsl_pool_config_held(tx->tx_pool));
1250 
1251 	if ((flags & DMU_TX_NOTHROTTLE))
1252 		tx->tx_dirty_delayed = B_TRUE;
1253 
1254 	if (!(flags & DMU_TX_SUSPEND))
1255 		tx->tx_break_on_suspend = B_TRUE;
1256 
1257 	while ((err = dmu_tx_try_assign(tx)) != 0) {
1258 		dmu_tx_unassign(tx);
1259 
1260 		boolean_t suspended = (err == ESHUTDOWN);
1261 		if (suspended) {
1262 			/*
1263 			 * Pool suspended. We need to decide whether to block
1264 			 * and retry, or return error, depending on the
1265 			 * caller's flags and the pool config.
1266 			 */
1267 			if (flags & DMU_TX_SUSPEND)
1268 				/*
1269 				 * The caller expressly does not care about
1270 				 * suspend, so treat it as a normal retry.
1271 				 */
1272 				err = SET_ERROR(ERESTART);
1273 			else if ((flags & DMU_TX_WAIT) &&
1274 			    spa_get_failmode(tx->tx_pool->dp_spa) ==
1275 			    ZIO_FAILURE_MODE_CONTINUE)
1276 				/*
1277 				 * Caller wants to wait, but pool config is
1278 				 * overriding that, so return EIO to be
1279 				 * propagated back to userspace.
1280 				 */
1281 				err = SET_ERROR(EIO);
1282 			else
1283 				/* Anything else, we should just block. */
1284 				err = SET_ERROR(ERESTART);
1285 		}
1286 
1287 		/*
1288 		 * Return unless we decided to retry, or the caller does not
1289 		 * want to block.
1290 		 */
1291 		if (err != ERESTART || !(flags & DMU_TX_WAIT)) {
1292 			ASSERT(err == EDQUOT || err == ENOSPC ||
1293 			    err == ERESTART || err == EIO);
1294 			return (err);
1295 		}
1296 
1297 		/*
1298 		 * Wait until there's room in this txg, or until it's been
1299 		 * synced out and a new one is available.
1300 		 *
1301 		 * If we're here because the pool suspended above, then we
1302 		 * unset tx_break_on_suspend to make sure that if dmu_tx_wait()
1303 		 * has to fall back to a txg_wait_synced_flags(), it doesn't
1304 		 * immediately return because the pool is suspended. That would
1305 		 * then immediately return here, and we'd end up in a busy loop
1306 		 * until the pool resumes.
1307 		 *
1308 		 * On the other hand, if the pool hasn't suspended yet, then it
1309 		 * should be allowed to break a txg wait if the pool does
1310 		 * suspend, so we can loop and reassess it in
1311 		 * dmu_tx_try_assign().
1312 		 */
1313 		if (suspended)
1314 			tx->tx_break_on_suspend = B_FALSE;
1315 
1316 		dmu_tx_wait(tx);
1317 
1318 		/*
1319 		 * Reset tx_break_on_suspend for DMU_TX_SUSPEND. We do this
1320 		 * here so that it's available if we return for some other
1321 		 * reason, and then the caller calls dmu_tx_wait().
1322 		 */
1323 		if (!(flags & DMU_TX_SUSPEND))
1324 			tx->tx_break_on_suspend = B_TRUE;
1325 	}
1326 
1327 	txg_rele_to_quiesce(&tx->tx_txgh);
1328 
1329 	return (0);
1330 }
1331 
1332 void
dmu_tx_wait(dmu_tx_t * tx)1333 dmu_tx_wait(dmu_tx_t *tx)
1334 {
1335 	spa_t *spa = tx->tx_pool->dp_spa;
1336 	dsl_pool_t *dp = tx->tx_pool;
1337 	hrtime_t before;
1338 
1339 	ASSERT0(tx->tx_txg);
1340 	ASSERT(!dsl_pool_config_held(tx->tx_pool));
1341 
1342 	/*
1343 	 * Break on suspend according to whether or not DMU_TX_SUSPEND was
1344 	 * supplied to the previous dmu_tx_assign() call. For clients, this
1345 	 * ensures that after dmu_tx_assign() fails, the followup dmu_tx_wait()
1346 	 * gets the same behaviour wrt suspend. See also the comments in
1347 	 * dmu_tx_assign().
1348 	 */
1349 	txg_wait_flag_t flags =
1350 	    (tx->tx_break_on_suspend ? TXG_WAIT_SUSPEND : TXG_WAIT_NONE);
1351 
1352 	before = gethrtime();
1353 
1354 	if (tx->tx_wait_dirty) {
1355 		uint64_t dirty;
1356 
1357 		/*
1358 		 * dmu_tx_try_assign() has determined that we need to wait
1359 		 * because we've consumed much or all of the dirty buffer
1360 		 * space.
1361 		 */
1362 		mutex_enter(&dp->dp_lock);
1363 		if (dp->dp_dirty_total >= zfs_dirty_data_max)
1364 			DMU_TX_STAT_BUMP(dmu_tx_dirty_over_max);
1365 		while (dp->dp_dirty_total >= zfs_dirty_data_max)
1366 			cv_wait(&dp->dp_spaceavail_cv, &dp->dp_lock);
1367 		dirty = dp->dp_dirty_total + dp->dp_sync_reserve_total;
1368 		mutex_exit(&dp->dp_lock);
1369 
1370 		dmu_tx_delay(tx, dirty);
1371 
1372 		tx->tx_wait_dirty = B_FALSE;
1373 
1374 		/*
1375 		 * Note: setting tx_dirty_delayed only has effect if the
1376 		 * caller used DMU_TX_WAIT.  Otherwise they are going to
1377 		 * destroy this tx and try again.  The common case,
1378 		 * zfs_write(), uses DMU_TX_WAIT.
1379 		 */
1380 		tx->tx_dirty_delayed = B_TRUE;
1381 	} else if (spa_suspended(spa) || tx->tx_lasttried_txg == 0) {
1382 		/*
1383 		 * If the pool is suspended we need to wait until it
1384 		 * is resumed.  Note that it's possible that the pool
1385 		 * has become active after this thread has tried to
1386 		 * obtain a tx.  If that's the case then tx_lasttried_txg
1387 		 * would not have been set.
1388 		 */
1389 		txg_wait_synced_flags(dp, spa_last_synced_txg(spa) + 1, flags);
1390 	} else if (tx->tx_needassign_txh) {
1391 		dnode_t *dn = tx->tx_needassign_txh->txh_dnode;
1392 
1393 		mutex_enter(&dn->dn_mtx);
1394 		while (dn->dn_assigned_txg == tx->tx_lasttried_txg - 1)
1395 			cv_wait(&dn->dn_notxholds, &dn->dn_mtx);
1396 		mutex_exit(&dn->dn_mtx);
1397 		tx->tx_needassign_txh = NULL;
1398 	} else {
1399 		/*
1400 		 * If we have a lot of dirty data just wait until we sync
1401 		 * out a TXG at which point we'll hopefully have synced
1402 		 * a portion of the changes.
1403 		 */
1404 		txg_wait_synced_flags(dp, spa_last_synced_txg(spa) + 1, flags);
1405 	}
1406 
1407 	spa_tx_assign_add_nsecs(spa, gethrtime() - before);
1408 }
1409 
1410 static void
dmu_tx_destroy(dmu_tx_t * tx)1411 dmu_tx_destroy(dmu_tx_t *tx)
1412 {
1413 	dmu_tx_hold_t *txh;
1414 
1415 	while ((txh = list_head(&tx->tx_holds)) != NULL) {
1416 		dnode_t *dn = txh->txh_dnode;
1417 
1418 		list_remove(&tx->tx_holds, txh);
1419 		zfs_refcount_destroy_many(&txh->txh_space_towrite,
1420 		    zfs_refcount_count(&txh->txh_space_towrite));
1421 		zfs_refcount_destroy_many(&txh->txh_memory_tohold,
1422 		    zfs_refcount_count(&txh->txh_memory_tohold));
1423 		kmem_free(txh, sizeof (dmu_tx_hold_t));
1424 		if (dn != NULL)
1425 			dnode_rele(dn, tx);
1426 	}
1427 
1428 	list_destroy(&tx->tx_callbacks);
1429 	list_destroy(&tx->tx_holds);
1430 	kmem_free(tx, sizeof (dmu_tx_t));
1431 }
1432 
1433 void
dmu_tx_commit(dmu_tx_t * tx)1434 dmu_tx_commit(dmu_tx_t *tx)
1435 {
1436 	/* This function should only be used on assigned transactions. */
1437 	ASSERT(tx->tx_txg != 0);
1438 
1439 	/*
1440 	 * Go through the transaction's hold list and remove holds on
1441 	 * associated dnodes, notifying waiters if no holds remain.
1442 	 */
1443 	for (dmu_tx_hold_t *txh = list_head(&tx->tx_holds); txh != NULL;
1444 	    txh = list_next(&tx->tx_holds, txh)) {
1445 		dnode_t *dn = txh->txh_dnode;
1446 
1447 		if (dn == NULL)
1448 			continue;
1449 
1450 		mutex_enter(&dn->dn_mtx);
1451 		ASSERT3U(dn->dn_assigned_txg, ==, tx->tx_txg);
1452 
1453 		if (zfs_refcount_remove(&dn->dn_tx_holds, tx) == 0) {
1454 			dn->dn_assigned_txg = 0;
1455 			cv_broadcast(&dn->dn_notxholds);
1456 		}
1457 		mutex_exit(&dn->dn_mtx);
1458 	}
1459 
1460 	if (tx->tx_tempreserve_cookie)
1461 		dsl_dir_tempreserve_clear(tx->tx_tempreserve_cookie, tx);
1462 
1463 	if (!list_is_empty(&tx->tx_callbacks))
1464 		txg_register_callbacks(&tx->tx_txgh, &tx->tx_callbacks);
1465 
1466 	if (tx->tx_anyobj == FALSE)
1467 		txg_rele_to_sync(&tx->tx_txgh);
1468 
1469 	dmu_tx_destroy(tx);
1470 }
1471 
1472 void
dmu_tx_abort(dmu_tx_t * tx)1473 dmu_tx_abort(dmu_tx_t *tx)
1474 {
1475 	/* This function should not be used on assigned transactions. */
1476 	ASSERT0(tx->tx_txg);
1477 
1478 	/* Should not be needed, but better be safe than sorry. */
1479 	if (tx->tx_tempreserve_cookie)
1480 		dsl_dir_tempreserve_clear(tx->tx_tempreserve_cookie, tx);
1481 
1482 	/*
1483 	 * Call any registered callbacks with an error code.
1484 	 */
1485 	if (!list_is_empty(&tx->tx_callbacks))
1486 		dmu_tx_do_callbacks(&tx->tx_callbacks, SET_ERROR(ECANCELED));
1487 
1488 	/* Should not be needed, but better be safe than sorry. */
1489 	dmu_tx_unassign(tx);
1490 
1491 	dmu_tx_destroy(tx);
1492 }
1493 
1494 uint64_t
dmu_tx_get_txg(dmu_tx_t * tx)1495 dmu_tx_get_txg(dmu_tx_t *tx)
1496 {
1497 	ASSERT(tx->tx_txg != 0);
1498 	return (tx->tx_txg);
1499 }
1500 
1501 dsl_pool_t *
dmu_tx_pool(dmu_tx_t * tx)1502 dmu_tx_pool(dmu_tx_t *tx)
1503 {
1504 	ASSERT(tx->tx_pool != NULL);
1505 	return (tx->tx_pool);
1506 }
1507 
1508 /*
1509  * Register a callback to be executed at the end of a TXG.
1510  *
1511  * Note: This currently exists for outside consumers, specifically the ZFS OSD
1512  * for Lustre. Please do not remove before checking that project. For examples
1513  * on how to use this see `ztest_commit_callback`.
1514  */
1515 void
dmu_tx_callback_register(dmu_tx_t * tx,dmu_tx_callback_func_t * func,void * data)1516 dmu_tx_callback_register(dmu_tx_t *tx, dmu_tx_callback_func_t *func, void *data)
1517 {
1518 	dmu_tx_callback_t *dcb;
1519 
1520 	dcb = kmem_alloc(sizeof (dmu_tx_callback_t), KM_SLEEP);
1521 
1522 	dcb->dcb_func = func;
1523 	dcb->dcb_data = data;
1524 
1525 	list_insert_tail(&tx->tx_callbacks, dcb);
1526 }
1527 
1528 /*
1529  * Call all the commit callbacks on a list, with a given error code.
1530  */
1531 void
dmu_tx_do_callbacks(list_t * cb_list,int error)1532 dmu_tx_do_callbacks(list_t *cb_list, int error)
1533 {
1534 	dmu_tx_callback_t *dcb;
1535 
1536 	while ((dcb = list_remove_tail(cb_list)) != NULL) {
1537 		dcb->dcb_func(dcb->dcb_data, error);
1538 		kmem_free(dcb, sizeof (dmu_tx_callback_t));
1539 	}
1540 }
1541 
1542 /*
1543  * Interface to hold a bunch of attributes.
1544  * used for creating new files.
1545  * attrsize is the total size of all attributes
1546  * to be added during object creation
1547  *
1548  * For updating/adding a single attribute dmu_tx_hold_sa() should be used.
1549  */
1550 
1551 /*
1552  * hold necessary attribute name for attribute registration.
1553  * should be a very rare case where this is needed.  If it does
1554  * happen it would only happen on the first write to the file system.
1555  */
1556 static void
dmu_tx_sa_registration_hold(sa_os_t * sa,dmu_tx_t * tx)1557 dmu_tx_sa_registration_hold(sa_os_t *sa, dmu_tx_t *tx)
1558 {
1559 	if (!sa->sa_need_attr_registration)
1560 		return;
1561 
1562 	for (int i = 0; i != sa->sa_num_attrs; i++) {
1563 		if (!sa->sa_attr_table[i].sa_registered) {
1564 			if (sa->sa_reg_attr_obj)
1565 				dmu_tx_hold_zap(tx, sa->sa_reg_attr_obj,
1566 				    B_TRUE, sa->sa_attr_table[i].sa_name);
1567 			else
1568 				dmu_tx_hold_zap(tx, DMU_NEW_OBJECT,
1569 				    B_TRUE, sa->sa_attr_table[i].sa_name);
1570 		}
1571 	}
1572 }
1573 
1574 void
dmu_tx_hold_spill(dmu_tx_t * tx,uint64_t object)1575 dmu_tx_hold_spill(dmu_tx_t *tx, uint64_t object)
1576 {
1577 	dmu_tx_hold_t *txh;
1578 
1579 	txh = dmu_tx_hold_object_impl(tx, tx->tx_objset, object,
1580 	    THT_SPILL, 0, 0);
1581 	if (txh != NULL)
1582 		(void) zfs_refcount_add_many(&txh->txh_space_towrite,
1583 		    SPA_OLD_MAXBLOCKSIZE, FTAG);
1584 }
1585 
1586 void
dmu_tx_hold_sa_create(dmu_tx_t * tx,int attrsize)1587 dmu_tx_hold_sa_create(dmu_tx_t *tx, int attrsize)
1588 {
1589 	sa_os_t *sa = tx->tx_objset->os_sa;
1590 
1591 	dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1592 
1593 	if (tx->tx_objset->os_sa->sa_master_obj == 0)
1594 		return;
1595 
1596 	if (tx->tx_objset->os_sa->sa_layout_attr_obj) {
1597 		dmu_tx_hold_zap(tx, sa->sa_layout_attr_obj, B_TRUE, NULL);
1598 	} else {
1599 		dmu_tx_hold_zap(tx, sa->sa_master_obj, B_TRUE, SA_LAYOUTS);
1600 		dmu_tx_hold_zap(tx, sa->sa_master_obj, B_TRUE, SA_REGISTRY);
1601 		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
1602 		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
1603 	}
1604 
1605 	dmu_tx_sa_registration_hold(sa, tx);
1606 
1607 	if (attrsize <= DN_OLD_MAX_BONUSLEN && !sa->sa_force_spill)
1608 		return;
1609 
1610 	(void) dmu_tx_hold_object_impl(tx, tx->tx_objset, DMU_NEW_OBJECT,
1611 	    THT_SPILL, 0, 0);
1612 }
1613 
1614 /*
1615  * Hold SA attribute
1616  *
1617  * dmu_tx_hold_sa(dmu_tx_t *tx, sa_handle_t *, attribute, add, size)
1618  *
1619  * variable_size is the total size of all variable sized attributes
1620  * passed to this function.  It is not the total size of all
1621  * variable size attributes that *may* exist on this object.
1622  */
1623 void
dmu_tx_hold_sa(dmu_tx_t * tx,sa_handle_t * hdl,boolean_t may_grow)1624 dmu_tx_hold_sa(dmu_tx_t *tx, sa_handle_t *hdl, boolean_t may_grow)
1625 {
1626 	uint64_t object;
1627 	sa_os_t *sa = tx->tx_objset->os_sa;
1628 
1629 	ASSERT(hdl != NULL);
1630 
1631 	object = sa_handle_object(hdl);
1632 
1633 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)hdl->sa_bonus;
1634 	DB_DNODE_ENTER(db);
1635 	dmu_tx_hold_bonus_by_dnode(tx, DB_DNODE(db));
1636 	DB_DNODE_EXIT(db);
1637 
1638 	if (tx->tx_objset->os_sa->sa_master_obj == 0)
1639 		return;
1640 
1641 	if (tx->tx_objset->os_sa->sa_reg_attr_obj == 0 ||
1642 	    tx->tx_objset->os_sa->sa_layout_attr_obj == 0) {
1643 		dmu_tx_hold_zap(tx, sa->sa_master_obj, B_TRUE, SA_LAYOUTS);
1644 		dmu_tx_hold_zap(tx, sa->sa_master_obj, B_TRUE, SA_REGISTRY);
1645 		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
1646 		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
1647 	}
1648 
1649 	dmu_tx_sa_registration_hold(sa, tx);
1650 
1651 	if (may_grow && tx->tx_objset->os_sa->sa_layout_attr_obj)
1652 		dmu_tx_hold_zap(tx, sa->sa_layout_attr_obj, B_TRUE, NULL);
1653 
1654 	if (sa->sa_force_spill || may_grow || hdl->sa_spill) {
1655 		ASSERT0(tx->tx_txg);
1656 		dmu_tx_hold_spill(tx, object);
1657 	} else {
1658 		DB_DNODE_ENTER(db);
1659 		if (DB_DNODE(db)->dn_have_spill) {
1660 			ASSERT0(tx->tx_txg);
1661 			dmu_tx_hold_spill(tx, object);
1662 		}
1663 		DB_DNODE_EXIT(db);
1664 	}
1665 }
1666 
1667 void
dmu_tx_init(void)1668 dmu_tx_init(void)
1669 {
1670 	dmu_tx_ksp = kstat_create("zfs", 0, "dmu_tx", "misc",
1671 	    KSTAT_TYPE_NAMED, sizeof (dmu_tx_stats) / sizeof (kstat_named_t),
1672 	    KSTAT_FLAG_VIRTUAL);
1673 
1674 	if (dmu_tx_ksp != NULL) {
1675 		dmu_tx_ksp->ks_data = &dmu_tx_stats;
1676 		kstat_install(dmu_tx_ksp);
1677 	}
1678 }
1679 
1680 void
dmu_tx_fini(void)1681 dmu_tx_fini(void)
1682 {
1683 	if (dmu_tx_ksp != NULL) {
1684 		kstat_delete(dmu_tx_ksp);
1685 		dmu_tx_ksp = NULL;
1686 	}
1687 }
1688 
1689 #if defined(_KERNEL)
1690 EXPORT_SYMBOL(dmu_tx_create);
1691 EXPORT_SYMBOL(dmu_tx_hold_write);
1692 EXPORT_SYMBOL(dmu_tx_hold_write_by_dnode);
1693 EXPORT_SYMBOL(dmu_tx_hold_append);
1694 EXPORT_SYMBOL(dmu_tx_hold_append_by_dnode);
1695 EXPORT_SYMBOL(dmu_tx_hold_free);
1696 EXPORT_SYMBOL(dmu_tx_hold_free_by_dnode);
1697 EXPORT_SYMBOL(dmu_tx_hold_zap);
1698 EXPORT_SYMBOL(dmu_tx_hold_zap_by_dnode);
1699 EXPORT_SYMBOL(dmu_tx_hold_bonus);
1700 EXPORT_SYMBOL(dmu_tx_hold_bonus_by_dnode);
1701 EXPORT_SYMBOL(dmu_tx_abort);
1702 EXPORT_SYMBOL(dmu_tx_assign);
1703 EXPORT_SYMBOL(dmu_tx_wait);
1704 EXPORT_SYMBOL(dmu_tx_commit);
1705 EXPORT_SYMBOL(dmu_tx_mark_netfree);
1706 EXPORT_SYMBOL(dmu_tx_get_txg);
1707 EXPORT_SYMBOL(dmu_tx_callback_register);
1708 EXPORT_SYMBOL(dmu_tx_do_callbacks);
1709 EXPORT_SYMBOL(dmu_tx_hold_spill);
1710 EXPORT_SYMBOL(dmu_tx_hold_sa_create);
1711 EXPORT_SYMBOL(dmu_tx_hold_sa);
1712 #endif
1713