xref: /illumos-gate/usr/src/uts/common/fs/zfs/dmu_tx.c (revision b24ab6762772a3f6a89393947930c7fa61306783)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5f65e61c0Sahrens  * Common Development and Distribution License (the "License").
6f65e61c0Sahrens  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21fa9e4066Sahrens /*
224a7f2a75SMark Maybee  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23fa9e4066Sahrens  * Use is subject to license terms.
24fa9e4066Sahrens  */
25fa9e4066Sahrens 
26fa9e4066Sahrens #include <sys/dmu.h>
27fa9e4066Sahrens #include <sys/dmu_impl.h>
28fa9e4066Sahrens #include <sys/dbuf.h>
29fa9e4066Sahrens #include <sys/dmu_tx.h>
30fa9e4066Sahrens #include <sys/dmu_objset.h>
31fa9e4066Sahrens #include <sys/dsl_dataset.h> /* for dsl_dataset_block_freeable() */
32fa9e4066Sahrens #include <sys/dsl_dir.h> /* for dsl_dir_tempreserve_*() */
33fa9e4066Sahrens #include <sys/dsl_pool.h>
348a2f1b91Sahrens #include <sys/zap_impl.h> /* for fzap_default_block_shift */
35fa9e4066Sahrens #include <sys/spa.h>
36fa9e4066Sahrens #include <sys/zfs_context.h>
37fa9e4066Sahrens 
38ea8dc4b6Seschrock typedef void (*dmu_tx_hold_func_t)(dmu_tx_t *tx, struct dnode *dn,
39ea8dc4b6Seschrock     uint64_t arg1, uint64_t arg2);
40ea8dc4b6Seschrock 
41fa9e4066Sahrens 
42fa9e4066Sahrens dmu_tx_t *
431d452cf5Sahrens dmu_tx_create_dd(dsl_dir_t *dd)
44fa9e4066Sahrens {
45fa9e4066Sahrens 	dmu_tx_t *tx = kmem_zalloc(sizeof (dmu_tx_t), KM_SLEEP);
46fa9e4066Sahrens 	tx->tx_dir = dd;
47fa9e4066Sahrens 	if (dd)
48fa9e4066Sahrens 		tx->tx_pool = dd->dd_pool;
49fa9e4066Sahrens 	list_create(&tx->tx_holds, sizeof (dmu_tx_hold_t),
508a2f1b91Sahrens 	    offsetof(dmu_tx_hold_t, txh_node));
51d20e665cSRicardo M. Correia 	list_create(&tx->tx_callbacks, sizeof (dmu_tx_callback_t),
52d20e665cSRicardo M. Correia 	    offsetof(dmu_tx_callback_t, dcb_node));
538a2f1b91Sahrens #ifdef ZFS_DEBUG
54fa9e4066Sahrens 	refcount_create(&tx->tx_space_written);
55fa9e4066Sahrens 	refcount_create(&tx->tx_space_freed);
568a2f1b91Sahrens #endif
57fa9e4066Sahrens 	return (tx);
58fa9e4066Sahrens }
59fa9e4066Sahrens 
60fa9e4066Sahrens dmu_tx_t *
61fa9e4066Sahrens dmu_tx_create(objset_t *os)
62fa9e4066Sahrens {
63503ad85cSMatthew Ahrens 	dmu_tx_t *tx = dmu_tx_create_dd(os->os_dsl_dataset->ds_dir);
64fa9e4066Sahrens 	tx->tx_objset = os;
65503ad85cSMatthew Ahrens 	tx->tx_lastsnap_txg = dsl_dataset_prev_snap_txg(os->os_dsl_dataset);
66fa9e4066Sahrens 	return (tx);
67fa9e4066Sahrens }
68fa9e4066Sahrens 
69fa9e4066Sahrens dmu_tx_t *
70fa9e4066Sahrens dmu_tx_create_assigned(struct dsl_pool *dp, uint64_t txg)
71fa9e4066Sahrens {
721d452cf5Sahrens 	dmu_tx_t *tx = dmu_tx_create_dd(NULL);
73fa9e4066Sahrens 
74fa9e4066Sahrens 	ASSERT3U(txg, <=, dp->dp_tx.tx_open_txg);
75fa9e4066Sahrens 	tx->tx_pool = dp;
76fa9e4066Sahrens 	tx->tx_txg = txg;
77fa9e4066Sahrens 	tx->tx_anyobj = TRUE;
78fa9e4066Sahrens 
79fa9e4066Sahrens 	return (tx);
80fa9e4066Sahrens }
81fa9e4066Sahrens 
82fa9e4066Sahrens int
83fa9e4066Sahrens dmu_tx_is_syncing(dmu_tx_t *tx)
84fa9e4066Sahrens {
85fa9e4066Sahrens 	return (tx->tx_anyobj);
86fa9e4066Sahrens }
87fa9e4066Sahrens 
88fa9e4066Sahrens int
89fa9e4066Sahrens dmu_tx_private_ok(dmu_tx_t *tx)
90fa9e4066Sahrens {
91ea8dc4b6Seschrock 	return (tx->tx_anyobj);
92fa9e4066Sahrens }
93fa9e4066Sahrens 
948a2f1b91Sahrens static dmu_tx_hold_t *
95fa9e4066Sahrens dmu_tx_hold_object_impl(dmu_tx_t *tx, objset_t *os, uint64_t object,
968a2f1b91Sahrens     enum dmu_tx_hold_type type, uint64_t arg1, uint64_t arg2)
97fa9e4066Sahrens {
988a2f1b91Sahrens 	dmu_tx_hold_t *txh;
99fa9e4066Sahrens 	dnode_t *dn = NULL;
100ea8dc4b6Seschrock 	int err;
101fa9e4066Sahrens 
102fa9e4066Sahrens 	if (object != DMU_NEW_OBJECT) {
103503ad85cSMatthew Ahrens 		err = dnode_hold(os, object, tx, &dn);
104ea8dc4b6Seschrock 		if (err) {
105ea8dc4b6Seschrock 			tx->tx_err = err;
1068a2f1b91Sahrens 			return (NULL);
107ea8dc4b6Seschrock 		}
108fa9e4066Sahrens 
109ea8dc4b6Seschrock 		if (err == 0 && tx->tx_txg != 0) {
110fa9e4066Sahrens 			mutex_enter(&dn->dn_mtx);
111fa9e4066Sahrens 			/*
112fa9e4066Sahrens 			 * dn->dn_assigned_txg == tx->tx_txg doesn't pose a
113fa9e4066Sahrens 			 * problem, but there's no way for it to happen (for
114fa9e4066Sahrens 			 * now, at least).
115fa9e4066Sahrens 			 */
116fa9e4066Sahrens 			ASSERT(dn->dn_assigned_txg == 0);
117fa9e4066Sahrens 			dn->dn_assigned_txg = tx->tx_txg;
118fa9e4066Sahrens 			(void) refcount_add(&dn->dn_tx_holds, tx);
119fa9e4066Sahrens 			mutex_exit(&dn->dn_mtx);
120fa9e4066Sahrens 		}
121fa9e4066Sahrens 	}
122fa9e4066Sahrens 
1238a2f1b91Sahrens 	txh = kmem_zalloc(sizeof (dmu_tx_hold_t), KM_SLEEP);
1248a2f1b91Sahrens 	txh->txh_tx = tx;
1258a2f1b91Sahrens 	txh->txh_dnode = dn;
1268a2f1b91Sahrens #ifdef ZFS_DEBUG
1278a2f1b91Sahrens 	txh->txh_type = type;
1288a2f1b91Sahrens 	txh->txh_arg1 = arg1;
1298a2f1b91Sahrens 	txh->txh_arg2 = arg2;
1308a2f1b91Sahrens #endif
1318a2f1b91Sahrens 	list_insert_tail(&tx->tx_holds, txh);
132ea8dc4b6Seschrock 
1338a2f1b91Sahrens 	return (txh);
134fa9e4066Sahrens }
135fa9e4066Sahrens 
136fa9e4066Sahrens void
137fa9e4066Sahrens dmu_tx_add_new_object(dmu_tx_t *tx, objset_t *os, uint64_t object)
138fa9e4066Sahrens {
139fa9e4066Sahrens 	/*
140fa9e4066Sahrens 	 * If we're syncing, they can manipulate any object anyhow, and
141fa9e4066Sahrens 	 * the hold on the dnode_t can cause problems.
142fa9e4066Sahrens 	 */
143fa9e4066Sahrens 	if (!dmu_tx_is_syncing(tx)) {
1448a2f1b91Sahrens 		(void) dmu_tx_hold_object_impl(tx, os,
1458a2f1b91Sahrens 		    object, THT_NEWOBJECT, 0, 0);
146fa9e4066Sahrens 	}
147fa9e4066Sahrens }
148fa9e4066Sahrens 
149ea8dc4b6Seschrock static int
150ea8dc4b6Seschrock dmu_tx_check_ioerr(zio_t *zio, dnode_t *dn, int level, uint64_t blkid)
151ea8dc4b6Seschrock {
152ea8dc4b6Seschrock 	int err;
153ea8dc4b6Seschrock 	dmu_buf_impl_t *db;
154ea8dc4b6Seschrock 
155ea8dc4b6Seschrock 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
156ea8dc4b6Seschrock 	db = dbuf_hold_level(dn, level, blkid, FTAG);
157ea8dc4b6Seschrock 	rw_exit(&dn->dn_struct_rwlock);
158ea8dc4b6Seschrock 	if (db == NULL)
159ea8dc4b6Seschrock 		return (EIO);
1601ab7f2deSmaybee 	err = dbuf_read(db, zio, DB_RF_CANFAIL | DB_RF_NOPREFETCH);
161ea8dc4b6Seschrock 	dbuf_rele(db, FTAG);
162ea8dc4b6Seschrock 	return (err);
163ea8dc4b6Seschrock }
164ea8dc4b6Seschrock 
1654a7f2a75SMark Maybee static void
166*b24ab676SJeff Bonwick dmu_tx_count_twig(dmu_tx_hold_t *txh, dnode_t *dn, dmu_buf_impl_t *db,
167*b24ab676SJeff Bonwick     int level, uint64_t blkid, boolean_t freeable, uint64_t *history)
1684a7f2a75SMark Maybee {
169*b24ab676SJeff Bonwick 	objset_t *os = dn->dn_objset;
170*b24ab676SJeff Bonwick 	dsl_dataset_t *ds = os->os_dsl_dataset;
171*b24ab676SJeff Bonwick 	int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
172*b24ab676SJeff Bonwick 	dmu_buf_impl_t *parent = NULL;
173*b24ab676SJeff Bonwick 	blkptr_t *bp = NULL;
174*b24ab676SJeff Bonwick 	uint64_t space;
175*b24ab676SJeff Bonwick 
176*b24ab676SJeff Bonwick 	if (level >= dn->dn_nlevels || history[level] == blkid)
1774a7f2a75SMark Maybee 		return;
1784a7f2a75SMark Maybee 
179*b24ab676SJeff Bonwick 	history[level] = blkid;
1804a7f2a75SMark Maybee 
181*b24ab676SJeff Bonwick 	space = (level == 0) ? dn->dn_datablksz : (1ULL << dn->dn_indblkshift);
1824a7f2a75SMark Maybee 
183*b24ab676SJeff Bonwick 	if (db == NULL || db == dn->dn_dbuf) {
184*b24ab676SJeff Bonwick 		ASSERT(level != 0);
185*b24ab676SJeff Bonwick 		db = NULL;
186*b24ab676SJeff Bonwick 	} else {
187*b24ab676SJeff Bonwick 		ASSERT(db->db_dnode == dn);
188*b24ab676SJeff Bonwick 		ASSERT(db->db_level == level);
189*b24ab676SJeff Bonwick 		ASSERT(db->db.db_size == space);
190*b24ab676SJeff Bonwick 		ASSERT(db->db_blkid == blkid);
191*b24ab676SJeff Bonwick 		bp = db->db_blkptr;
192*b24ab676SJeff Bonwick 		parent = db->db_parent;
1934a7f2a75SMark Maybee 	}
194*b24ab676SJeff Bonwick 
195*b24ab676SJeff Bonwick 	freeable = (bp && (freeable ||
196*b24ab676SJeff Bonwick 	    dsl_dataset_block_freeable(ds, bp->blk_birth)));
197*b24ab676SJeff Bonwick 
198*b24ab676SJeff Bonwick 	if (freeable)
199*b24ab676SJeff Bonwick 		txh->txh_space_tooverwrite += space;
200*b24ab676SJeff Bonwick 	else
201*b24ab676SJeff Bonwick 		txh->txh_space_towrite += space;
202*b24ab676SJeff Bonwick 	if (bp)
203*b24ab676SJeff Bonwick 		txh->txh_space_tounref += bp_get_dsize(os->os_spa, bp);
204*b24ab676SJeff Bonwick 
205*b24ab676SJeff Bonwick 	dmu_tx_count_twig(txh, dn, parent, level + 1,
206*b24ab676SJeff Bonwick 	    blkid >> epbs, freeable, history);
2074a7f2a75SMark Maybee }
2084a7f2a75SMark Maybee 
209fa9e4066Sahrens /* ARGSUSED */
210fa9e4066Sahrens static void
2118a2f1b91Sahrens dmu_tx_count_write(dmu_tx_hold_t *txh, uint64_t off, uint64_t len)
212fa9e4066Sahrens {
2138a2f1b91Sahrens 	dnode_t *dn = txh->txh_dnode;
2148a2f1b91Sahrens 	uint64_t start, end, i;
215fa9e4066Sahrens 	int min_bs, max_bs, min_ibs, max_ibs, epbs, bits;
2168a2f1b91Sahrens 	int err = 0;
217fa9e4066Sahrens 
218fa9e4066Sahrens 	if (len == 0)
219fa9e4066Sahrens 		return;
220fa9e4066Sahrens 
221fa9e4066Sahrens 	min_bs = SPA_MINBLOCKSHIFT;
222fa9e4066Sahrens 	max_bs = SPA_MAXBLOCKSHIFT;
223fa9e4066Sahrens 	min_ibs = DN_MIN_INDBLKSHIFT;
224fa9e4066Sahrens 	max_ibs = DN_MAX_INDBLKSHIFT;
225fa9e4066Sahrens 
2268a2f1b91Sahrens 	if (dn) {
227*b24ab676SJeff Bonwick 		uint64_t history[DN_MAX_LEVELS];
2284a7f2a75SMark Maybee 		int nlvls = dn->dn_nlevels;
2294a7f2a75SMark Maybee 		int delta;
2304a7f2a75SMark Maybee 
2314a7f2a75SMark Maybee 		/*
2324a7f2a75SMark Maybee 		 * For i/o error checking, read the first and last level-0
2334a7f2a75SMark Maybee 		 * blocks (if they are not aligned), and all the level-1 blocks.
2344a7f2a75SMark Maybee 		 */
235ea8dc4b6Seschrock 		if (dn->dn_maxblkid == 0) {
2364a7f2a75SMark Maybee 			delta = dn->dn_datablksz;
2374a7f2a75SMark Maybee 			start = (off < dn->dn_datablksz) ? 0 : 1;
2384a7f2a75SMark Maybee 			end = (off+len <= dn->dn_datablksz) ? 0 : 1;
2394a7f2a75SMark Maybee 			if (start == 0 && (off > 0 || len < dn->dn_datablksz)) {
24082c9918fSTim Haley 				err = dmu_tx_check_ioerr(NULL, dn, 0, 0);
24182c9918fSTim Haley 				if (err)
24282c9918fSTim Haley 					goto out;
2434a7f2a75SMark Maybee 				delta -= off;
24482c9918fSTim Haley 			}
245ea8dc4b6Seschrock 		} else {
2468a2f1b91Sahrens 			zio_t *zio = zio_root(dn->dn_objset->os_spa,
247ea8dc4b6Seschrock 			    NULL, NULL, ZIO_FLAG_CANFAIL);
248ea8dc4b6Seschrock 
249ea8dc4b6Seschrock 			/* first level-0 block */
25099653d4eSeschrock 			start = off >> dn->dn_datablkshift;
25199653d4eSeschrock 			if (P2PHASE(off, dn->dn_datablksz) ||
25299653d4eSeschrock 			    len < dn->dn_datablksz) {
25399653d4eSeschrock 				err = dmu_tx_check_ioerr(zio, dn, 0, start);
2548a2f1b91Sahrens 				if (err)
2558a2f1b91Sahrens 					goto out;
256ea8dc4b6Seschrock 			}
257ea8dc4b6Seschrock 
258ea8dc4b6Seschrock 			/* last level-0 block */
25999653d4eSeschrock 			end = (off+len-1) >> dn->dn_datablkshift;
26082c9918fSTim Haley 			if (end != start && end <= dn->dn_maxblkid &&
26199653d4eSeschrock 			    P2PHASE(off+len, dn->dn_datablksz)) {
262ea8dc4b6Seschrock 				err = dmu_tx_check_ioerr(zio, dn, 0, end);
2638a2f1b91Sahrens 				if (err)
2648a2f1b91Sahrens 					goto out;
265ea8dc4b6Seschrock 			}
266ea8dc4b6Seschrock 
267ea8dc4b6Seschrock 			/* level-1 blocks */
2684a7f2a75SMark Maybee 			if (nlvls > 1) {
2694a7f2a75SMark Maybee 				int shft = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
2704a7f2a75SMark Maybee 				for (i = (start>>shft)+1; i < end>>shft; i++) {
271ea8dc4b6Seschrock 					err = dmu_tx_check_ioerr(zio, dn, 1, i);
2728a2f1b91Sahrens 					if (err)
2738a2f1b91Sahrens 						goto out;
274ea8dc4b6Seschrock 				}
275ea8dc4b6Seschrock 			}
276ea8dc4b6Seschrock 
277ea8dc4b6Seschrock 			err = zio_wait(zio);
2788a2f1b91Sahrens 			if (err)
2798a2f1b91Sahrens 				goto out;
2804a7f2a75SMark Maybee 			delta = P2NPHASE(off, dn->dn_datablksz);
281ea8dc4b6Seschrock 		}
282ea8dc4b6Seschrock 
2834a7f2a75SMark Maybee 		if (dn->dn_maxblkid > 0) {
2844a7f2a75SMark Maybee 			/*
2854a7f2a75SMark Maybee 			 * The blocksize can't change,
2864a7f2a75SMark Maybee 			 * so we can make a more precise estimate.
2874a7f2a75SMark Maybee 			 */
2884a7f2a75SMark Maybee 			ASSERT(dn->dn_datablkshift != 0);
289fa9e4066Sahrens 			min_bs = max_bs = dn->dn_datablkshift;
2904a7f2a75SMark Maybee 			min_ibs = max_ibs = dn->dn_indblkshift;
2914a7f2a75SMark Maybee 		} else if (dn->dn_indblkshift > max_ibs) {
2924a7f2a75SMark Maybee 			/*
2934a7f2a75SMark Maybee 			 * This ensures that if we reduce DN_MAX_INDBLKSHIFT,
2944a7f2a75SMark Maybee 			 * the code will still work correctly on older pools.
2954a7f2a75SMark Maybee 			 */
2964a7f2a75SMark Maybee 			min_ibs = max_ibs = dn->dn_indblkshift;
2974a7f2a75SMark Maybee 		}
2984a7f2a75SMark Maybee 
2994a7f2a75SMark Maybee 		/*
3004a7f2a75SMark Maybee 		 * If this write is not off the end of the file
3014a7f2a75SMark Maybee 		 * we need to account for overwrites/unref.
3024a7f2a75SMark Maybee 		 */
303*b24ab676SJeff Bonwick 		if (start <= dn->dn_maxblkid) {
304*b24ab676SJeff Bonwick 			for (int l = 0; l < DN_MAX_LEVELS; l++)
305*b24ab676SJeff Bonwick 				history[l] = -1ULL;
306*b24ab676SJeff Bonwick 		}
3074a7f2a75SMark Maybee 		while (start <= dn->dn_maxblkid) {
3084a7f2a75SMark Maybee 			dmu_buf_impl_t *db;
3094a7f2a75SMark Maybee 
3104a7f2a75SMark Maybee 			rw_enter(&dn->dn_struct_rwlock, RW_READER);
3114a7f2a75SMark Maybee 			db = dbuf_hold_level(dn, 0, start, FTAG);
3124a7f2a75SMark Maybee 			rw_exit(&dn->dn_struct_rwlock);
313*b24ab676SJeff Bonwick 			dmu_tx_count_twig(txh, dn, db, 0, start, B_FALSE,
314*b24ab676SJeff Bonwick 			    history);
3154a7f2a75SMark Maybee 			dbuf_rele(db, FTAG);
3164a7f2a75SMark Maybee 			if (++start > end) {
3174a7f2a75SMark Maybee 				/*
3184a7f2a75SMark Maybee 				 * Account for new indirects appearing
3194a7f2a75SMark Maybee 				 * before this IO gets assigned into a txg.
3204a7f2a75SMark Maybee 				 */
3214a7f2a75SMark Maybee 				bits = 64 - min_bs;
3224a7f2a75SMark Maybee 				epbs = min_ibs - SPA_BLKPTRSHIFT;
3234a7f2a75SMark Maybee 				for (bits -= epbs * (nlvls - 1);
3244a7f2a75SMark Maybee 				    bits >= 0; bits -= epbs)
3254a7f2a75SMark Maybee 					txh->txh_fudge += 1ULL << max_ibs;
3264a7f2a75SMark Maybee 				goto out;
3274a7f2a75SMark Maybee 			}
3284a7f2a75SMark Maybee 			off += delta;
3294a7f2a75SMark Maybee 			if (len >= delta)
3304a7f2a75SMark Maybee 				len -= delta;
3314a7f2a75SMark Maybee 			delta = dn->dn_datablksz;
3324a7f2a75SMark Maybee 		}
333fa9e4066Sahrens 	}
334fa9e4066Sahrens 
335fa9e4066Sahrens 	/*
336fa9e4066Sahrens 	 * 'end' is the last thing we will access, not one past.
337fa9e4066Sahrens 	 * This way we won't overflow when accessing the last byte.
338fa9e4066Sahrens 	 */
339fa9e4066Sahrens 	start = P2ALIGN(off, 1ULL << max_bs);
340fa9e4066Sahrens 	end = P2ROUNDUP(off + len, 1ULL << max_bs) - 1;
3418a2f1b91Sahrens 	txh->txh_space_towrite += end - start + 1;
342fa9e4066Sahrens 
343fa9e4066Sahrens 	start >>= min_bs;
344fa9e4066Sahrens 	end >>= min_bs;
345fa9e4066Sahrens 
346fa9e4066Sahrens 	epbs = min_ibs - SPA_BLKPTRSHIFT;
347fa9e4066Sahrens 
348fa9e4066Sahrens 	/*
349fa9e4066Sahrens 	 * The object contains at most 2^(64 - min_bs) blocks,
350fa9e4066Sahrens 	 * and each indirect level maps 2^epbs.
351fa9e4066Sahrens 	 */
352fa9e4066Sahrens 	for (bits = 64 - min_bs; bits >= 0; bits -= epbs) {
353fa9e4066Sahrens 		start >>= epbs;
354fa9e4066Sahrens 		end >>= epbs;
3554a7f2a75SMark Maybee 		ASSERT3U(end, >=, start);
3568a2f1b91Sahrens 		txh->txh_space_towrite += (end - start + 1) << max_ibs;
3574a7f2a75SMark Maybee 		if (start != 0) {
3584a7f2a75SMark Maybee 			/*
3594a7f2a75SMark Maybee 			 * We also need a new blkid=0 indirect block
3604a7f2a75SMark Maybee 			 * to reference any existing file data.
3614a7f2a75SMark Maybee 			 */
3624a7f2a75SMark Maybee 			txh->txh_space_towrite += 1ULL << max_ibs;
3634a7f2a75SMark Maybee 		}
364fa9e4066Sahrens 	}
365fa9e4066Sahrens 
3668a2f1b91Sahrens out:
3674a7f2a75SMark Maybee 	if (txh->txh_space_towrite + txh->txh_space_tooverwrite >
3684a7f2a75SMark Maybee 	    2 * DMU_MAX_ACCESS)
3694a7f2a75SMark Maybee 		err = EFBIG;
3704a7f2a75SMark Maybee 
3718a2f1b91Sahrens 	if (err)
3728a2f1b91Sahrens 		txh->txh_tx->tx_err = err;
373fa9e4066Sahrens }
374fa9e4066Sahrens 
375fa9e4066Sahrens static void
3768a2f1b91Sahrens dmu_tx_count_dnode(dmu_tx_hold_t *txh)
377fa9e4066Sahrens {
3788a2f1b91Sahrens 	dnode_t *dn = txh->txh_dnode;
379503ad85cSMatthew Ahrens 	dnode_t *mdn = txh->txh_tx->tx_objset->os_meta_dnode;
3808a2f1b91Sahrens 	uint64_t space = mdn->dn_datablksz +
3818a2f1b91Sahrens 	    ((mdn->dn_nlevels-1) << mdn->dn_indblkshift);
382fa9e4066Sahrens 
383fa9e4066Sahrens 	if (dn && dn->dn_dbuf->db_blkptr &&
384fa9e4066Sahrens 	    dsl_dataset_block_freeable(dn->dn_objset->os_dsl_dataset,
385ea8dc4b6Seschrock 	    dn->dn_dbuf->db_blkptr->blk_birth)) {
3868a2f1b91Sahrens 		txh->txh_space_tooverwrite += space;
3874a7f2a75SMark Maybee 		txh->txh_space_tounref += space;
3888a2f1b91Sahrens 	} else {
3898a2f1b91Sahrens 		txh->txh_space_towrite += space;
390a9799022Sck 		if (dn && dn->dn_dbuf->db_blkptr)
391a9799022Sck 			txh->txh_space_tounref += space;
392fa9e4066Sahrens 	}
393fa9e4066Sahrens }
394fa9e4066Sahrens 
395fa9e4066Sahrens void
396fa9e4066Sahrens dmu_tx_hold_write(dmu_tx_t *tx, uint64_t object, uint64_t off, int len)
397fa9e4066Sahrens {
3988a2f1b91Sahrens 	dmu_tx_hold_t *txh;
3998a2f1b91Sahrens 
400fa9e4066Sahrens 	ASSERT(tx->tx_txg == 0);
401ea8dc4b6Seschrock 	ASSERT(len < DMU_MAX_ACCESS);
402dd6ef538Smaybee 	ASSERT(len == 0 || UINT64_MAX - off >= len - 1);
403fa9e4066Sahrens 
4048a2f1b91Sahrens 	txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
4058a2f1b91Sahrens 	    object, THT_WRITE, off, len);
4068a2f1b91Sahrens 	if (txh == NULL)
4078a2f1b91Sahrens 		return;
4088a2f1b91Sahrens 
4098a2f1b91Sahrens 	dmu_tx_count_write(txh, off, len);
4108a2f1b91Sahrens 	dmu_tx_count_dnode(txh);
411fa9e4066Sahrens }
412fa9e4066Sahrens 
413fa9e4066Sahrens static void
4148a2f1b91Sahrens dmu_tx_count_free(dmu_tx_hold_t *txh, uint64_t off, uint64_t len)
415fa9e4066Sahrens {
416cdb0ab79Smaybee 	uint64_t blkid, nblks, lastblk;
417cdb0ab79Smaybee 	uint64_t space = 0, unref = 0, skipped = 0;
4188a2f1b91Sahrens 	dnode_t *dn = txh->txh_dnode;
419fa9e4066Sahrens 	dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset;
4208a2f1b91Sahrens 	spa_t *spa = txh->txh_tx->tx_pool->dp_spa;
421cdb0ab79Smaybee 	int epbs;
422fa9e4066Sahrens 
423cdb0ab79Smaybee 	if (dn->dn_nlevels == 0)
424fa9e4066Sahrens 		return;
425c543ec06Sahrens 
426fa9e4066Sahrens 	/*
427cdb0ab79Smaybee 	 * The struct_rwlock protects us against dn_nlevels
428c543ec06Sahrens 	 * changing, in case (against all odds) we manage to dirty &
429c543ec06Sahrens 	 * sync out the changes after we check for being dirty.
430cdb0ab79Smaybee 	 * Also, dbuf_hold_level() wants us to have the struct_rwlock.
431fa9e4066Sahrens 	 */
432fa9e4066Sahrens 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
433cdb0ab79Smaybee 	epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
434cdb0ab79Smaybee 	if (dn->dn_maxblkid == 0) {
435c543ec06Sahrens 		if (off == 0 && len >= dn->dn_datablksz) {
436c543ec06Sahrens 			blkid = 0;
437c543ec06Sahrens 			nblks = 1;
438c543ec06Sahrens 		} else {
439c543ec06Sahrens 			rw_exit(&dn->dn_struct_rwlock);
440c543ec06Sahrens 			return;
441c543ec06Sahrens 		}
442c543ec06Sahrens 	} else {
443c543ec06Sahrens 		blkid = off >> dn->dn_datablkshift;
444cdb0ab79Smaybee 		nblks = (len + dn->dn_datablksz - 1) >> dn->dn_datablkshift;
445fa9e4066Sahrens 
446cdb0ab79Smaybee 		if (blkid >= dn->dn_maxblkid) {
447c543ec06Sahrens 			rw_exit(&dn->dn_struct_rwlock);
448c543ec06Sahrens 			return;
449c543ec06Sahrens 		}
450cdb0ab79Smaybee 		if (blkid + nblks > dn->dn_maxblkid)
451cdb0ab79Smaybee 			nblks = dn->dn_maxblkid - blkid;
452fa9e4066Sahrens 
453c543ec06Sahrens 	}
454cdb0ab79Smaybee 	if (dn->dn_nlevels == 1) {
455fa9e4066Sahrens 		int i;
456fa9e4066Sahrens 		for (i = 0; i < nblks; i++) {
457fa9e4066Sahrens 			blkptr_t *bp = dn->dn_phys->dn_blkptr;
458cdb0ab79Smaybee 			ASSERT3U(blkid + i, <, dn->dn_nblkptr);
459fa9e4066Sahrens 			bp += blkid + i;
460ea8dc4b6Seschrock 			if (dsl_dataset_block_freeable(ds, bp->blk_birth)) {
461fa9e4066Sahrens 				dprintf_bp(bp, "can free old%s", "");
462*b24ab676SJeff Bonwick 				space += bp_get_dsize(spa, bp);
463fa9e4066Sahrens 			}
464a9799022Sck 			unref += BP_GET_ASIZE(bp);
465fa9e4066Sahrens 		}
466ea8dc4b6Seschrock 		nblks = 0;
467fa9e4066Sahrens 	}
468fa9e4066Sahrens 
469cdb0ab79Smaybee 	/*
470715614a4Smaybee 	 * Add in memory requirements of higher-level indirects.
471715614a4Smaybee 	 * This assumes a worst-possible scenario for dn_nlevels.
472cdb0ab79Smaybee 	 */
473715614a4Smaybee 	{
474cdb0ab79Smaybee 		uint64_t blkcnt = 1 + ((nblks >> epbs) >> epbs);
475b7e50089Smaybee 		int level = (dn->dn_nlevels > 1) ? 2 : 1;
476cdb0ab79Smaybee 
477715614a4Smaybee 		while (level++ < DN_MAX_LEVELS) {
478cdb0ab79Smaybee 			txh->txh_memory_tohold += blkcnt << dn->dn_indblkshift;
479cdb0ab79Smaybee 			blkcnt = 1 + (blkcnt >> epbs);
480cdb0ab79Smaybee 		}
481cdb0ab79Smaybee 		ASSERT(blkcnt <= dn->dn_nblkptr);
482cdb0ab79Smaybee 	}
483cdb0ab79Smaybee 
484cdb0ab79Smaybee 	lastblk = blkid + nblks - 1;
485fa9e4066Sahrens 	while (nblks) {
486fa9e4066Sahrens 		dmu_buf_impl_t *dbuf;
487cdb0ab79Smaybee 		uint64_t ibyte, new_blkid;
488cdb0ab79Smaybee 		int epb = 1 << epbs;
489cdb0ab79Smaybee 		int err, i, blkoff, tochk;
490cdb0ab79Smaybee 		blkptr_t *bp;
491cdb0ab79Smaybee 
492cdb0ab79Smaybee 		ibyte = blkid << dn->dn_datablkshift;
493cdb0ab79Smaybee 		err = dnode_next_offset(dn,
494cdb0ab79Smaybee 		    DNODE_FIND_HAVELOCK, &ibyte, 2, 1, 0);
495cdb0ab79Smaybee 		new_blkid = ibyte >> dn->dn_datablkshift;
496b7e50089Smaybee 		if (err == ESRCH) {
497b7e50089Smaybee 			skipped += (lastblk >> epbs) - (blkid >> epbs) + 1;
498cdb0ab79Smaybee 			break;
499b7e50089Smaybee 		}
500cdb0ab79Smaybee 		if (err) {
501cdb0ab79Smaybee 			txh->txh_tx->tx_err = err;
502cdb0ab79Smaybee 			break;
503cdb0ab79Smaybee 		}
504b7e50089Smaybee 		if (new_blkid > lastblk) {
505b7e50089Smaybee 			skipped += (lastblk >> epbs) - (blkid >> epbs) + 1;
506cdb0ab79Smaybee 			break;
507b7e50089Smaybee 		}
508fa9e4066Sahrens 
509cdb0ab79Smaybee 		if (new_blkid > blkid) {
510b7e50089Smaybee 			ASSERT((new_blkid >> epbs) > (blkid >> epbs));
511b7e50089Smaybee 			skipped += (new_blkid >> epbs) - (blkid >> epbs) - 1;
512cdb0ab79Smaybee 			nblks -= new_blkid - blkid;
513cdb0ab79Smaybee 			blkid = new_blkid;
514cdb0ab79Smaybee 		}
515cdb0ab79Smaybee 		blkoff = P2PHASE(blkid, epb);
516cdb0ab79Smaybee 		tochk = MIN(epb - blkoff, nblks);
517fa9e4066Sahrens 
518cdb0ab79Smaybee 		dbuf = dbuf_hold_level(dn, 1, blkid >> epbs, FTAG);
519cdb0ab79Smaybee 
520cdb0ab79Smaybee 		txh->txh_memory_tohold += dbuf->db.db_size;
521cdb0ab79Smaybee 		if (txh->txh_memory_tohold > DMU_MAX_ACCESS) {
522cdb0ab79Smaybee 			txh->txh_tx->tx_err = E2BIG;
523ea8dc4b6Seschrock 			dbuf_rele(dbuf, FTAG);
524cdb0ab79Smaybee 			break;
525c543ec06Sahrens 		}
526cdb0ab79Smaybee 		err = dbuf_read(dbuf, NULL, DB_RF_HAVESTRUCT | DB_RF_CANFAIL);
527cdb0ab79Smaybee 		if (err != 0) {
5288a2f1b91Sahrens 			txh->txh_tx->tx_err = err;
529cdb0ab79Smaybee 			dbuf_rele(dbuf, FTAG);
530c543ec06Sahrens 			break;
531fa9e4066Sahrens 		}
532fa9e4066Sahrens 
533cdb0ab79Smaybee 		bp = dbuf->db.db_data;
534cdb0ab79Smaybee 		bp += blkoff;
535cdb0ab79Smaybee 
536cdb0ab79Smaybee 		for (i = 0; i < tochk; i++) {
537cdb0ab79Smaybee 			if (dsl_dataset_block_freeable(ds, bp[i].blk_birth)) {
538cdb0ab79Smaybee 				dprintf_bp(&bp[i], "can free old%s", "");
539*b24ab676SJeff Bonwick 				space += bp_get_dsize(spa, &bp[i]);
540cdb0ab79Smaybee 			}
541cdb0ab79Smaybee 			unref += BP_GET_ASIZE(bp);
542cdb0ab79Smaybee 		}
543cdb0ab79Smaybee 		dbuf_rele(dbuf, FTAG);
544cdb0ab79Smaybee 
545fa9e4066Sahrens 		blkid += tochk;
546fa9e4066Sahrens 		nblks -= tochk;
547fa9e4066Sahrens 	}
548fa9e4066Sahrens 	rw_exit(&dn->dn_struct_rwlock);
549fa9e4066Sahrens 
550cdb0ab79Smaybee 	/* account for new level 1 indirect blocks that might show up */
551b7e50089Smaybee 	if (skipped > 0) {
552715614a4Smaybee 		txh->txh_fudge += skipped << dn->dn_indblkshift;
553cdb0ab79Smaybee 		skipped = MIN(skipped, DMU_MAX_DELETEBLKCNT >> epbs);
554cdb0ab79Smaybee 		txh->txh_memory_tohold += skipped << dn->dn_indblkshift;
555cdb0ab79Smaybee 	}
5568a2f1b91Sahrens 	txh->txh_space_tofree += space;
557a9799022Sck 	txh->txh_space_tounref += unref;
558fa9e4066Sahrens }
559fa9e4066Sahrens 
5608a2f1b91Sahrens void
5618a2f1b91Sahrens dmu_tx_hold_free(dmu_tx_t *tx, uint64_t object, uint64_t off, uint64_t len)
562fa9e4066Sahrens {
5638a2f1b91Sahrens 	dmu_tx_hold_t *txh;
5648a2f1b91Sahrens 	dnode_t *dn;
565ea8dc4b6Seschrock 	uint64_t start, end, i;
566c543ec06Sahrens 	int err, shift;
567ea8dc4b6Seschrock 	zio_t *zio;
568fa9e4066Sahrens 
5698a2f1b91Sahrens 	ASSERT(tx->tx_txg == 0);
5708a2f1b91Sahrens 
5718a2f1b91Sahrens 	txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
5728a2f1b91Sahrens 	    object, THT_FREE, off, len);
5738a2f1b91Sahrens 	if (txh == NULL)
5748a2f1b91Sahrens 		return;
5758a2f1b91Sahrens 	dn = txh->txh_dnode;
5768a2f1b91Sahrens 
577fa9e4066Sahrens 	/* first block */
57898572ac1Sahrens 	if (off != 0)
5798a2f1b91Sahrens 		dmu_tx_count_write(txh, off, 1);
580fa9e4066Sahrens 	/* last block */
581fa9e4066Sahrens 	if (len != DMU_OBJECT_END)
5828a2f1b91Sahrens 		dmu_tx_count_write(txh, off+len, 1);
583fa9e4066Sahrens 
584*b24ab676SJeff Bonwick 	dmu_tx_count_dnode(txh);
585*b24ab676SJeff Bonwick 
586fa9e4066Sahrens 	if (off >= (dn->dn_maxblkid+1) * dn->dn_datablksz)
587fa9e4066Sahrens 		return;
588fa9e4066Sahrens 	if (len == DMU_OBJECT_END)
589fa9e4066Sahrens 		len = (dn->dn_maxblkid+1) * dn->dn_datablksz - off;
590fa9e4066Sahrens 
591ea8dc4b6Seschrock 	/*
592ea8dc4b6Seschrock 	 * For i/o error checking, read the first and last level-0
593ea8dc4b6Seschrock 	 * blocks, and all the level-1 blocks.  The above count_write's
594cdb0ab79Smaybee 	 * have already taken care of the level-0 blocks.
595ea8dc4b6Seschrock 	 */
59698572ac1Sahrens 	if (dn->dn_nlevels > 1) {
59798572ac1Sahrens 		shift = dn->dn_datablkshift + dn->dn_indblkshift -
59898572ac1Sahrens 		    SPA_BLKPTRSHIFT;
59998572ac1Sahrens 		start = off >> shift;
60098572ac1Sahrens 		end = dn->dn_datablkshift ? ((off+len) >> shift) : 0;
60198572ac1Sahrens 
60298572ac1Sahrens 		zio = zio_root(tx->tx_pool->dp_spa,
60398572ac1Sahrens 		    NULL, NULL, ZIO_FLAG_CANFAIL);
60498572ac1Sahrens 		for (i = start; i <= end; i++) {
60598572ac1Sahrens 			uint64_t ibyte = i << shift;
606cdb0ab79Smaybee 			err = dnode_next_offset(dn, 0, &ibyte, 2, 1, 0);
60798572ac1Sahrens 			i = ibyte >> shift;
60898572ac1Sahrens 			if (err == ESRCH)
60998572ac1Sahrens 				break;
61098572ac1Sahrens 			if (err) {
61198572ac1Sahrens 				tx->tx_err = err;
61298572ac1Sahrens 				return;
61398572ac1Sahrens 			}
614ea8dc4b6Seschrock 
61598572ac1Sahrens 			err = dmu_tx_check_ioerr(zio, dn, 1, i);
61698572ac1Sahrens 			if (err) {
61798572ac1Sahrens 				tx->tx_err = err;
61898572ac1Sahrens 				return;
61998572ac1Sahrens 			}
62098572ac1Sahrens 		}
62198572ac1Sahrens 		err = zio_wait(zio);
622ea8dc4b6Seschrock 		if (err) {
623ea8dc4b6Seschrock 			tx->tx_err = err;
624ea8dc4b6Seschrock 			return;
625ea8dc4b6Seschrock 		}
626ea8dc4b6Seschrock 	}
627ea8dc4b6Seschrock 
6288a2f1b91Sahrens 	dmu_tx_count_free(txh, off, len);
629fa9e4066Sahrens }
630fa9e4066Sahrens 
631fa9e4066Sahrens void
63214843421SMatthew Ahrens dmu_tx_hold_zap(dmu_tx_t *tx, uint64_t object, int add, const char *name)
633fa9e4066Sahrens {
6348a2f1b91Sahrens 	dmu_tx_hold_t *txh;
6358a2f1b91Sahrens 	dnode_t *dn;
636fa9e4066Sahrens 	uint64_t nblocks;
637ea8dc4b6Seschrock 	int epbs, err;
638fa9e4066Sahrens 
6398a2f1b91Sahrens 	ASSERT(tx->tx_txg == 0);
6408a2f1b91Sahrens 
6418a2f1b91Sahrens 	txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
6428a2f1b91Sahrens 	    object, THT_ZAP, add, (uintptr_t)name);
6438a2f1b91Sahrens 	if (txh == NULL)
6448a2f1b91Sahrens 		return;
6458a2f1b91Sahrens 	dn = txh->txh_dnode;
6468a2f1b91Sahrens 
6478a2f1b91Sahrens 	dmu_tx_count_dnode(txh);
648fa9e4066Sahrens 
649fa9e4066Sahrens 	if (dn == NULL) {
650fa9e4066Sahrens 		/*
651ea8dc4b6Seschrock 		 * We will be able to fit a new object's entries into one leaf
652fa9e4066Sahrens 		 * block.  So there will be at most 2 blocks total,
653fa9e4066Sahrens 		 * including the header block.
654fa9e4066Sahrens 		 */
6558a2f1b91Sahrens 		dmu_tx_count_write(txh, 0, 2 << fzap_default_block_shift);
656fa9e4066Sahrens 		return;
657fa9e4066Sahrens 	}
658fa9e4066Sahrens 
659fa9e4066Sahrens 	ASSERT3P(dmu_ot[dn->dn_type].ot_byteswap, ==, zap_byteswap);
660fa9e4066Sahrens 
661ea8dc4b6Seschrock 	if (dn->dn_maxblkid == 0 && !add) {
662fa9e4066Sahrens 		/*
663fa9e4066Sahrens 		 * If there is only one block  (i.e. this is a micro-zap)
664ea8dc4b6Seschrock 		 * and we are not adding anything, the accounting is simple.
665fa9e4066Sahrens 		 */
666ea8dc4b6Seschrock 		err = dmu_tx_check_ioerr(NULL, dn, 0, 0);
667ea8dc4b6Seschrock 		if (err) {
668ea8dc4b6Seschrock 			tx->tx_err = err;
669ea8dc4b6Seschrock 			return;
670ea8dc4b6Seschrock 		}
671ea8dc4b6Seschrock 
672b6130eadSmaybee 		/*
673b6130eadSmaybee 		 * Use max block size here, since we don't know how much
674b6130eadSmaybee 		 * the size will change between now and the dbuf dirty call.
675b6130eadSmaybee 		 */
676fa9e4066Sahrens 		if (dsl_dataset_block_freeable(dn->dn_objset->os_dsl_dataset,
677a9799022Sck 		    dn->dn_phys->dn_blkptr[0].blk_birth)) {
678b6130eadSmaybee 			txh->txh_space_tooverwrite += SPA_MAXBLOCKSIZE;
679a9799022Sck 		} else {
680b6130eadSmaybee 			txh->txh_space_towrite += SPA_MAXBLOCKSIZE;
681a9799022Sck 		}
682f878aa38SChris Kirby 		if (dn->dn_phys->dn_blkptr[0].blk_birth)
683f878aa38SChris Kirby 			txh->txh_space_tounref += SPA_MAXBLOCKSIZE;
684fa9e4066Sahrens 		return;
685fa9e4066Sahrens 	}
686fa9e4066Sahrens 
687ea8dc4b6Seschrock 	if (dn->dn_maxblkid > 0 && name) {
688ea8dc4b6Seschrock 		/*
689ea8dc4b6Seschrock 		 * access the name in this fat-zap so that we'll check
690ea8dc4b6Seschrock 		 * for i/o errors to the leaf blocks, etc.
691ea8dc4b6Seschrock 		 */
692503ad85cSMatthew Ahrens 		err = zap_lookup(dn->dn_objset, dn->dn_object, name,
693ea8dc4b6Seschrock 		    8, 0, NULL);
694ea8dc4b6Seschrock 		if (err == EIO) {
695ea8dc4b6Seschrock 			tx->tx_err = err;
696ea8dc4b6Seschrock 			return;
697ea8dc4b6Seschrock 		}
698ea8dc4b6Seschrock 	}
699ea8dc4b6Seschrock 
700503ad85cSMatthew Ahrens 	err = zap_count_write(dn->dn_objset, dn->dn_object, name, add,
701720d1aa1SSanjeev Bagewadi 	    &txh->txh_space_towrite, &txh->txh_space_tooverwrite);
702fa9e4066Sahrens 
703fa9e4066Sahrens 	/*
704fa9e4066Sahrens 	 * If the modified blocks are scattered to the four winds,
705fa9e4066Sahrens 	 * we'll have to modify an indirect twig for each.
706fa9e4066Sahrens 	 */
707fa9e4066Sahrens 	epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
708fa9e4066Sahrens 	for (nblocks = dn->dn_maxblkid >> epbs; nblocks != 0; nblocks >>= epbs)
7093d692628SSanjeev Bagewadi 		if (dn->dn_objset->os_dsl_dataset->ds_phys->ds_prev_snap_obj)
7103d692628SSanjeev Bagewadi 			txh->txh_space_towrite += 3 << dn->dn_indblkshift;
7113d692628SSanjeev Bagewadi 		else
7123d692628SSanjeev Bagewadi 			txh->txh_space_tooverwrite += 3 << dn->dn_indblkshift;
713fa9e4066Sahrens }
714fa9e4066Sahrens 
715fa9e4066Sahrens void
716fa9e4066Sahrens dmu_tx_hold_bonus(dmu_tx_t *tx, uint64_t object)
717fa9e4066Sahrens {
7188a2f1b91Sahrens 	dmu_tx_hold_t *txh;
719fa9e4066Sahrens 
7208a2f1b91Sahrens 	ASSERT(tx->tx_txg == 0);
721fa9e4066Sahrens 
7228a2f1b91Sahrens 	txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
7238a2f1b91Sahrens 	    object, THT_BONUS, 0, 0);
7248a2f1b91Sahrens 	if (txh)
7258a2f1b91Sahrens 		dmu_tx_count_dnode(txh);
726fa9e4066Sahrens }
727fa9e4066Sahrens 
728fa9e4066Sahrens void
729fa9e4066Sahrens dmu_tx_hold_space(dmu_tx_t *tx, uint64_t space)
730fa9e4066Sahrens {
7318a2f1b91Sahrens 	dmu_tx_hold_t *txh;
732fa9e4066Sahrens 	ASSERT(tx->tx_txg == 0);
733fa9e4066Sahrens 
7348a2f1b91Sahrens 	txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
7358a2f1b91Sahrens 	    DMU_NEW_OBJECT, THT_SPACE, space, 0);
7368a2f1b91Sahrens 
7378a2f1b91Sahrens 	txh->txh_space_towrite += space;
738fa9e4066Sahrens }
739fa9e4066Sahrens 
740fa9e4066Sahrens int
741fa9e4066Sahrens dmu_tx_holds(dmu_tx_t *tx, uint64_t object)
742fa9e4066Sahrens {
7438a2f1b91Sahrens 	dmu_tx_hold_t *txh;
744fa9e4066Sahrens 	int holds = 0;
745fa9e4066Sahrens 
746fa9e4066Sahrens 	/*
747fa9e4066Sahrens 	 * By asserting that the tx is assigned, we're counting the
748fa9e4066Sahrens 	 * number of dn_tx_holds, which is the same as the number of
749fa9e4066Sahrens 	 * dn_holds.  Otherwise, we'd be counting dn_holds, but
750fa9e4066Sahrens 	 * dn_tx_holds could be 0.
751fa9e4066Sahrens 	 */
752fa9e4066Sahrens 	ASSERT(tx->tx_txg != 0);
753fa9e4066Sahrens 
754fa9e4066Sahrens 	/* if (tx->tx_anyobj == TRUE) */
755fa9e4066Sahrens 		/* return (0); */
756fa9e4066Sahrens 
7578a2f1b91Sahrens 	for (txh = list_head(&tx->tx_holds); txh;
7588a2f1b91Sahrens 	    txh = list_next(&tx->tx_holds, txh)) {
7598a2f1b91Sahrens 		if (txh->txh_dnode && txh->txh_dnode->dn_object == object)
760fa9e4066Sahrens 			holds++;
761fa9e4066Sahrens 	}
762fa9e4066Sahrens 
763fa9e4066Sahrens 	return (holds);
764fa9e4066Sahrens }
765fa9e4066Sahrens 
7669c9dc39aSek #ifdef ZFS_DEBUG
767fa9e4066Sahrens void
768fa9e4066Sahrens dmu_tx_dirty_buf(dmu_tx_t *tx, dmu_buf_impl_t *db)
769fa9e4066Sahrens {
7708a2f1b91Sahrens 	dmu_tx_hold_t *txh;
771fa9e4066Sahrens 	int match_object = FALSE, match_offset = FALSE;
772fa9e4066Sahrens 	dnode_t *dn = db->db_dnode;
773fa9e4066Sahrens 
774fa9e4066Sahrens 	ASSERT(tx->tx_txg != 0);
775503ad85cSMatthew Ahrens 	ASSERT(tx->tx_objset == NULL || dn->dn_objset == tx->tx_objset);
776fa9e4066Sahrens 	ASSERT3U(dn->dn_object, ==, db->db.db_object);
777fa9e4066Sahrens 
778fa9e4066Sahrens 	if (tx->tx_anyobj)
779fa9e4066Sahrens 		return;
780fa9e4066Sahrens 
781fa9e4066Sahrens 	/* XXX No checking on the meta dnode for now */
782ea8dc4b6Seschrock 	if (db->db.db_object == DMU_META_DNODE_OBJECT)
783fa9e4066Sahrens 		return;
784fa9e4066Sahrens 
7858a2f1b91Sahrens 	for (txh = list_head(&tx->tx_holds); txh;
7868a2f1b91Sahrens 	    txh = list_next(&tx->tx_holds, txh)) {
787fa9e4066Sahrens 		ASSERT(dn == NULL || dn->dn_assigned_txg == tx->tx_txg);
7888a2f1b91Sahrens 		if (txh->txh_dnode == dn && txh->txh_type != THT_NEWOBJECT)
789fa9e4066Sahrens 			match_object = TRUE;
7908a2f1b91Sahrens 		if (txh->txh_dnode == NULL || txh->txh_dnode == dn) {
791fa9e4066Sahrens 			int datablkshift = dn->dn_datablkshift ?
792fa9e4066Sahrens 			    dn->dn_datablkshift : SPA_MAXBLOCKSHIFT;
793fa9e4066Sahrens 			int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
794fa9e4066Sahrens 			int shift = datablkshift + epbs * db->db_level;
795fa9e4066Sahrens 			uint64_t beginblk = shift >= 64 ? 0 :
7968a2f1b91Sahrens 			    (txh->txh_arg1 >> shift);
797fa9e4066Sahrens 			uint64_t endblk = shift >= 64 ? 0 :
7988a2f1b91Sahrens 			    ((txh->txh_arg1 + txh->txh_arg2 - 1) >> shift);
799fa9e4066Sahrens 			uint64_t blkid = db->db_blkid;
800fa9e4066Sahrens 
8018a2f1b91Sahrens 			/* XXX txh_arg2 better not be zero... */
802fa9e4066Sahrens 
8038a2f1b91Sahrens 			dprintf("found txh type %x beginblk=%llx endblk=%llx\n",
8048a2f1b91Sahrens 			    txh->txh_type, beginblk, endblk);
805fa9e4066Sahrens 
8068a2f1b91Sahrens 			switch (txh->txh_type) {
807fa9e4066Sahrens 			case THT_WRITE:
808fa9e4066Sahrens 				if (blkid >= beginblk && blkid <= endblk)
809fa9e4066Sahrens 					match_offset = TRUE;
810fa9e4066Sahrens 				/*
811fa9e4066Sahrens 				 * We will let this hold work for the bonus
812fa9e4066Sahrens 				 * buffer so that we don't need to hold it
813fa9e4066Sahrens 				 * when creating a new object.
814fa9e4066Sahrens 				 */
815fa9e4066Sahrens 				if (blkid == DB_BONUS_BLKID)
816fa9e4066Sahrens 					match_offset = TRUE;
817fa9e4066Sahrens 				/*
818fa9e4066Sahrens 				 * They might have to increase nlevels,
819fa9e4066Sahrens 				 * thus dirtying the new TLIBs.  Or the
820fa9e4066Sahrens 				 * might have to change the block size,
821fa9e4066Sahrens 				 * thus dirying the new lvl=0 blk=0.
822fa9e4066Sahrens 				 */
823fa9e4066Sahrens 				if (blkid == 0)
824fa9e4066Sahrens 					match_offset = TRUE;
825fa9e4066Sahrens 				break;
826fa9e4066Sahrens 			case THT_FREE:
827cdb0ab79Smaybee 				/*
828cdb0ab79Smaybee 				 * We will dirty all the level 1 blocks in
829cdb0ab79Smaybee 				 * the free range and perhaps the first and
830cdb0ab79Smaybee 				 * last level 0 block.
831cdb0ab79Smaybee 				 */
832cdb0ab79Smaybee 				if (blkid >= beginblk && (blkid <= endblk ||
833cdb0ab79Smaybee 				    txh->txh_arg2 == DMU_OBJECT_END))
834fa9e4066Sahrens 					match_offset = TRUE;
835fa9e4066Sahrens 				break;
836fa9e4066Sahrens 			case THT_BONUS:
837fa9e4066Sahrens 				if (blkid == DB_BONUS_BLKID)
838fa9e4066Sahrens 					match_offset = TRUE;
839fa9e4066Sahrens 				break;
840fa9e4066Sahrens 			case THT_ZAP:
841fa9e4066Sahrens 				match_offset = TRUE;
842fa9e4066Sahrens 				break;
843fa9e4066Sahrens 			case THT_NEWOBJECT:
844fa9e4066Sahrens 				match_object = TRUE;
845fa9e4066Sahrens 				break;
846fa9e4066Sahrens 			default:
8478a2f1b91Sahrens 				ASSERT(!"bad txh_type");
848fa9e4066Sahrens 			}
849fa9e4066Sahrens 		}
850fa9e4066Sahrens 		if (match_object && match_offset)
851fa9e4066Sahrens 			return;
852fa9e4066Sahrens 	}
853fa9e4066Sahrens 	panic("dirtying dbuf obj=%llx lvl=%u blkid=%llx but not tx_held\n",
854fa9e4066Sahrens 	    (u_longlong_t)db->db.db_object, db->db_level,
855fa9e4066Sahrens 	    (u_longlong_t)db->db_blkid);
856fa9e4066Sahrens }
8579c9dc39aSek #endif
858fa9e4066Sahrens 
859fa9e4066Sahrens static int
8608a2f1b91Sahrens dmu_tx_try_assign(dmu_tx_t *tx, uint64_t txg_how)
861fa9e4066Sahrens {
8628a2f1b91Sahrens 	dmu_tx_hold_t *txh;
8630a4e9518Sgw 	spa_t *spa = tx->tx_pool->dp_spa;
864cdb0ab79Smaybee 	uint64_t memory, asize, fsize, usize;
865715614a4Smaybee 	uint64_t towrite, tofree, tooverwrite, tounref, tohold, fudge;
866fa9e4066Sahrens 
8678a2f1b91Sahrens 	ASSERT3U(tx->tx_txg, ==, 0);
8680a4e9518Sgw 
8698a2f1b91Sahrens 	if (tx->tx_err)
8708a2f1b91Sahrens 		return (tx->tx_err);
871fa9e4066Sahrens 
872e14bb325SJeff Bonwick 	if (spa_suspended(spa)) {
8730a4e9518Sgw 		/*
8740a4e9518Sgw 		 * If the user has indicated a blocking failure mode
8750a4e9518Sgw 		 * then return ERESTART which will block in dmu_tx_wait().
8760a4e9518Sgw 		 * Otherwise, return EIO so that an error can get
8770a4e9518Sgw 		 * propagated back to the VOP calls.
8780a4e9518Sgw 		 *
8790a4e9518Sgw 		 * Note that we always honor the txg_how flag regardless
8800a4e9518Sgw 		 * of the failuremode setting.
8810a4e9518Sgw 		 */
8820a4e9518Sgw 		if (spa_get_failmode(spa) == ZIO_FAILURE_MODE_CONTINUE &&
8830a4e9518Sgw 		    txg_how != TXG_WAIT)
8840a4e9518Sgw 			return (EIO);
8850a4e9518Sgw 
8860a4e9518Sgw 		return (ERESTART);
8870a4e9518Sgw 	}
8880a4e9518Sgw 
889fa9e4066Sahrens 	tx->tx_txg = txg_hold_open(tx->tx_pool, &tx->tx_txgh);
8908a2f1b91Sahrens 	tx->tx_needassign_txh = NULL;
891fa9e4066Sahrens 
8928a2f1b91Sahrens 	/*
8938a2f1b91Sahrens 	 * NB: No error returns are allowed after txg_hold_open, but
8948a2f1b91Sahrens 	 * before processing the dnode holds, due to the
8958a2f1b91Sahrens 	 * dmu_tx_unassign() logic.
8968a2f1b91Sahrens 	 */
897fa9e4066Sahrens 
898715614a4Smaybee 	towrite = tofree = tooverwrite = tounref = tohold = fudge = 0;
8998a2f1b91Sahrens 	for (txh = list_head(&tx->tx_holds); txh;
9008a2f1b91Sahrens 	    txh = list_next(&tx->tx_holds, txh)) {
9018a2f1b91Sahrens 		dnode_t *dn = txh->txh_dnode;
902fa9e4066Sahrens 		if (dn != NULL) {
903fa9e4066Sahrens 			mutex_enter(&dn->dn_mtx);
9048a2f1b91Sahrens 			if (dn->dn_assigned_txg == tx->tx_txg - 1) {
9058a2f1b91Sahrens 				mutex_exit(&dn->dn_mtx);
9068a2f1b91Sahrens 				tx->tx_needassign_txh = txh;
9078a2f1b91Sahrens 				return (ERESTART);
908fa9e4066Sahrens 			}
9098a2f1b91Sahrens 			if (dn->dn_assigned_txg == 0)
910fa9e4066Sahrens 				dn->dn_assigned_txg = tx->tx_txg;
9118a2f1b91Sahrens 			ASSERT3U(dn->dn_assigned_txg, ==, tx->tx_txg);
912fa9e4066Sahrens 			(void) refcount_add(&dn->dn_tx_holds, tx);
913fa9e4066Sahrens 			mutex_exit(&dn->dn_mtx);
914fa9e4066Sahrens 		}
9158a2f1b91Sahrens 		towrite += txh->txh_space_towrite;
9168a2f1b91Sahrens 		tofree += txh->txh_space_tofree;
9178a2f1b91Sahrens 		tooverwrite += txh->txh_space_tooverwrite;
918a9799022Sck 		tounref += txh->txh_space_tounref;
919cdb0ab79Smaybee 		tohold += txh->txh_memory_tohold;
920715614a4Smaybee 		fudge += txh->txh_fudge;
921ea8dc4b6Seschrock 	}
922ea8dc4b6Seschrock 
9238a2f1b91Sahrens 	/*
9248a2f1b91Sahrens 	 * NB: This check must be after we've held the dnodes, so that
9258a2f1b91Sahrens 	 * the dmu_tx_unassign() logic will work properly
9268a2f1b91Sahrens 	 */
9278a2f1b91Sahrens 	if (txg_how >= TXG_INITIAL && txg_how != tx->tx_txg)
9288a2f1b91Sahrens 		return (ERESTART);
9298a2f1b91Sahrens 
930ea8dc4b6Seschrock 	/*
931ea8dc4b6Seschrock 	 * If a snapshot has been taken since we made our estimates,
932ea8dc4b6Seschrock 	 * assume that we won't be able to free or overwrite anything.
933ea8dc4b6Seschrock 	 */
934ea8dc4b6Seschrock 	if (tx->tx_objset &&
935503ad85cSMatthew Ahrens 	    dsl_dataset_prev_snap_txg(tx->tx_objset->os_dsl_dataset) >
936ea8dc4b6Seschrock 	    tx->tx_lastsnap_txg) {
9378a2f1b91Sahrens 		towrite += tooverwrite;
9388a2f1b91Sahrens 		tooverwrite = tofree = 0;
939fa9e4066Sahrens 	}
940fa9e4066Sahrens 
941cdb0ab79Smaybee 	/* needed allocation: worst-case estimate of write space */
942cdb0ab79Smaybee 	asize = spa_get_asize(tx->tx_pool->dp_spa, towrite + tooverwrite);
943cdb0ab79Smaybee 	/* freed space estimate: worst-case overwrite + free estimate */
9448a2f1b91Sahrens 	fsize = spa_get_asize(tx->tx_pool->dp_spa, tooverwrite) + tofree;
945cdb0ab79Smaybee 	/* convert unrefd space to worst-case estimate */
946a9799022Sck 	usize = spa_get_asize(tx->tx_pool->dp_spa, tounref);
947cdb0ab79Smaybee 	/* calculate memory footprint estimate */
948cdb0ab79Smaybee 	memory = towrite + tooverwrite + tohold;
9498a2f1b91Sahrens 
9508a2f1b91Sahrens #ifdef ZFS_DEBUG
951715614a4Smaybee 	/*
952715614a4Smaybee 	 * Add in 'tohold' to account for our dirty holds on this memory
953715614a4Smaybee 	 * XXX - the "fudge" factor is to account for skipped blocks that
954715614a4Smaybee 	 * we missed because dnode_next_offset() misses in-core-only blocks.
955715614a4Smaybee 	 */
956cdb0ab79Smaybee 	tx->tx_space_towrite = asize +
957715614a4Smaybee 	    spa_get_asize(tx->tx_pool->dp_spa, tohold + fudge);
9588a2f1b91Sahrens 	tx->tx_space_tofree = tofree;
9598a2f1b91Sahrens 	tx->tx_space_tooverwrite = tooverwrite;
960a9799022Sck 	tx->tx_space_tounref = tounref;
9618a2f1b91Sahrens #endif
962fa9e4066Sahrens 
963fa9e4066Sahrens 	if (tx->tx_dir && asize != 0) {
964cdb0ab79Smaybee 		int err = dsl_dir_tempreserve_space(tx->tx_dir, memory,
965cdb0ab79Smaybee 		    asize, fsize, usize, &tx->tx_tempreserve_cookie, tx);
9668a2f1b91Sahrens 		if (err)
967fa9e4066Sahrens 			return (err);
968fa9e4066Sahrens 	}
969fa9e4066Sahrens 
970fa9e4066Sahrens 	return (0);
971fa9e4066Sahrens }
972fa9e4066Sahrens 
9738a2f1b91Sahrens static void
9748a2f1b91Sahrens dmu_tx_unassign(dmu_tx_t *tx)
975fa9e4066Sahrens {
9768a2f1b91Sahrens 	dmu_tx_hold_t *txh;
977fa9e4066Sahrens 
9788a2f1b91Sahrens 	if (tx->tx_txg == 0)
9798a2f1b91Sahrens 		return;
980fa9e4066Sahrens 
981fa9e4066Sahrens 	txg_rele_to_quiesce(&tx->tx_txgh);
982fa9e4066Sahrens 
9838a2f1b91Sahrens 	for (txh = list_head(&tx->tx_holds); txh != tx->tx_needassign_txh;
9848a2f1b91Sahrens 	    txh = list_next(&tx->tx_holds, txh)) {
9858a2f1b91Sahrens 		dnode_t *dn = txh->txh_dnode;
986fa9e4066Sahrens 
987fa9e4066Sahrens 		if (dn == NULL)
988fa9e4066Sahrens 			continue;
989fa9e4066Sahrens 		mutex_enter(&dn->dn_mtx);
9908a2f1b91Sahrens 		ASSERT3U(dn->dn_assigned_txg, ==, tx->tx_txg);
991fa9e4066Sahrens 
992fa9e4066Sahrens 		if (refcount_remove(&dn->dn_tx_holds, tx) == 0) {
993fa9e4066Sahrens 			dn->dn_assigned_txg = 0;
994fa9e4066Sahrens 			cv_broadcast(&dn->dn_notxholds);
995fa9e4066Sahrens 		}
996fa9e4066Sahrens 		mutex_exit(&dn->dn_mtx);
997fa9e4066Sahrens 	}
998fa9e4066Sahrens 
999fa9e4066Sahrens 	txg_rele_to_sync(&tx->tx_txgh);
1000fa9e4066Sahrens 
10018a2f1b91Sahrens 	tx->tx_lasttried_txg = tx->tx_txg;
1002fa9e4066Sahrens 	tx->tx_txg = 0;
1003fa9e4066Sahrens }
1004fa9e4066Sahrens 
1005fa9e4066Sahrens /*
1006fa9e4066Sahrens  * Assign tx to a transaction group.  txg_how can be one of:
1007fa9e4066Sahrens  *
1008fa9e4066Sahrens  * (1)	TXG_WAIT.  If the current open txg is full, waits until there's
1009fa9e4066Sahrens  *	a new one.  This should be used when you're not holding locks.
1010fa9e4066Sahrens  *	If will only fail if we're truly out of space (or over quota).
1011fa9e4066Sahrens  *
1012fa9e4066Sahrens  * (2)	TXG_NOWAIT.  If we can't assign into the current open txg without
1013fa9e4066Sahrens  *	blocking, returns immediately with ERESTART.  This should be used
1014fa9e4066Sahrens  *	whenever you're holding locks.  On an ERESTART error, the caller
10158a2f1b91Sahrens  *	should drop locks, do a dmu_tx_wait(tx), and try again.
1016fa9e4066Sahrens  *
1017fa9e4066Sahrens  * (3)	A specific txg.  Use this if you need to ensure that multiple
1018fa9e4066Sahrens  *	transactions all sync in the same txg.  Like TXG_NOWAIT, it
1019fa9e4066Sahrens  *	returns ERESTART if it can't assign you into the requested txg.
1020fa9e4066Sahrens  */
1021fa9e4066Sahrens int
1022fa9e4066Sahrens dmu_tx_assign(dmu_tx_t *tx, uint64_t txg_how)
1023fa9e4066Sahrens {
1024fa9e4066Sahrens 	int err;
1025fa9e4066Sahrens 
1026fa9e4066Sahrens 	ASSERT(tx->tx_txg == 0);
1027fa9e4066Sahrens 	ASSERT(txg_how != 0);
1028fa9e4066Sahrens 	ASSERT(!dsl_pool_sync_context(tx->tx_pool));
1029fa9e4066Sahrens 
10308a2f1b91Sahrens 	while ((err = dmu_tx_try_assign(tx, txg_how)) != 0) {
10318a2f1b91Sahrens 		dmu_tx_unassign(tx);
1032fa9e4066Sahrens 
1033fa9e4066Sahrens 		if (err != ERESTART || txg_how != TXG_WAIT)
1034fa9e4066Sahrens 			return (err);
1035fa9e4066Sahrens 
10368a2f1b91Sahrens 		dmu_tx_wait(tx);
1037fa9e4066Sahrens 	}
1038fa9e4066Sahrens 
1039fa9e4066Sahrens 	txg_rele_to_quiesce(&tx->tx_txgh);
1040fa9e4066Sahrens 
1041fa9e4066Sahrens 	return (0);
1042fa9e4066Sahrens }
1043fa9e4066Sahrens 
10448a2f1b91Sahrens void
10458a2f1b91Sahrens dmu_tx_wait(dmu_tx_t *tx)
10468a2f1b91Sahrens {
10470a4e9518Sgw 	spa_t *spa = tx->tx_pool->dp_spa;
10480a4e9518Sgw 
10498a2f1b91Sahrens 	ASSERT(tx->tx_txg == 0);
10508a2f1b91Sahrens 
10510a4e9518Sgw 	/*
10520a4e9518Sgw 	 * It's possible that the pool has become active after this thread
10530a4e9518Sgw 	 * has tried to obtain a tx. If that's the case then his
10540a4e9518Sgw 	 * tx_lasttried_txg would not have been assigned.
10550a4e9518Sgw 	 */
1056e14bb325SJeff Bonwick 	if (spa_suspended(spa) || tx->tx_lasttried_txg == 0) {
10570a4e9518Sgw 		txg_wait_synced(tx->tx_pool, spa_last_synced_txg(spa) + 1);
10580a4e9518Sgw 	} else if (tx->tx_needassign_txh) {
10598a2f1b91Sahrens 		dnode_t *dn = tx->tx_needassign_txh->txh_dnode;
10608a2f1b91Sahrens 
10618a2f1b91Sahrens 		mutex_enter(&dn->dn_mtx);
10628a2f1b91Sahrens 		while (dn->dn_assigned_txg == tx->tx_lasttried_txg - 1)
10638a2f1b91Sahrens 			cv_wait(&dn->dn_notxholds, &dn->dn_mtx);
10648a2f1b91Sahrens 		mutex_exit(&dn->dn_mtx);
10658a2f1b91Sahrens 		tx->tx_needassign_txh = NULL;
10668a2f1b91Sahrens 	} else {
10678a2f1b91Sahrens 		txg_wait_open(tx->tx_pool, tx->tx_lasttried_txg + 1);
10688a2f1b91Sahrens 	}
10698a2f1b91Sahrens }
10708a2f1b91Sahrens 
1071fa9e4066Sahrens void
1072fa9e4066Sahrens dmu_tx_willuse_space(dmu_tx_t *tx, int64_t delta)
1073fa9e4066Sahrens {
10748a2f1b91Sahrens #ifdef ZFS_DEBUG
1075fa9e4066Sahrens 	if (tx->tx_dir == NULL || delta == 0)
1076fa9e4066Sahrens 		return;
1077fa9e4066Sahrens 
1078fa9e4066Sahrens 	if (delta > 0) {
1079fa9e4066Sahrens 		ASSERT3U(refcount_count(&tx->tx_space_written) + delta, <=,
1080fa9e4066Sahrens 		    tx->tx_space_towrite);
1081fa9e4066Sahrens 		(void) refcount_add_many(&tx->tx_space_written, delta, NULL);
1082fa9e4066Sahrens 	} else {
1083fa9e4066Sahrens 		(void) refcount_add_many(&tx->tx_space_freed, -delta, NULL);
1084fa9e4066Sahrens 	}
10858a2f1b91Sahrens #endif
1086fa9e4066Sahrens }
1087fa9e4066Sahrens 
1088fa9e4066Sahrens void
1089fa9e4066Sahrens dmu_tx_commit(dmu_tx_t *tx)
1090fa9e4066Sahrens {
10918a2f1b91Sahrens 	dmu_tx_hold_t *txh;
1092fa9e4066Sahrens 
1093fa9e4066Sahrens 	ASSERT(tx->tx_txg != 0);
1094fa9e4066Sahrens 
10958a2f1b91Sahrens 	while (txh = list_head(&tx->tx_holds)) {
10968a2f1b91Sahrens 		dnode_t *dn = txh->txh_dnode;
1097fa9e4066Sahrens 
10988a2f1b91Sahrens 		list_remove(&tx->tx_holds, txh);
10998a2f1b91Sahrens 		kmem_free(txh, sizeof (dmu_tx_hold_t));
1100fa9e4066Sahrens 		if (dn == NULL)
1101fa9e4066Sahrens 			continue;
1102fa9e4066Sahrens 		mutex_enter(&dn->dn_mtx);
1103fa9e4066Sahrens 		ASSERT3U(dn->dn_assigned_txg, ==, tx->tx_txg);
1104fa9e4066Sahrens 
1105fa9e4066Sahrens 		if (refcount_remove(&dn->dn_tx_holds, tx) == 0) {
1106fa9e4066Sahrens 			dn->dn_assigned_txg = 0;
1107fa9e4066Sahrens 			cv_broadcast(&dn->dn_notxholds);
1108fa9e4066Sahrens 		}
1109fa9e4066Sahrens 		mutex_exit(&dn->dn_mtx);
1110fa9e4066Sahrens 		dnode_rele(dn, tx);
1111fa9e4066Sahrens 	}
1112fa9e4066Sahrens 
11138a2f1b91Sahrens 	if (tx->tx_tempreserve_cookie)
1114fa9e4066Sahrens 		dsl_dir_tempreserve_clear(tx->tx_tempreserve_cookie, tx);
1115fa9e4066Sahrens 
1116d20e665cSRicardo M. Correia 	if (!list_is_empty(&tx->tx_callbacks))
1117d20e665cSRicardo M. Correia 		txg_register_callbacks(&tx->tx_txgh, &tx->tx_callbacks);
1118d20e665cSRicardo M. Correia 
1119fa9e4066Sahrens 	if (tx->tx_anyobj == FALSE)
1120fa9e4066Sahrens 		txg_rele_to_sync(&tx->tx_txgh);
1121d20e665cSRicardo M. Correia 
1122d20e665cSRicardo M. Correia 	list_destroy(&tx->tx_callbacks);
11238f38d419Sek 	list_destroy(&tx->tx_holds);
11248a2f1b91Sahrens #ifdef ZFS_DEBUG
1125fa9e4066Sahrens 	dprintf("towrite=%llu written=%llu tofree=%llu freed=%llu\n",
1126fa9e4066Sahrens 	    tx->tx_space_towrite, refcount_count(&tx->tx_space_written),
1127fa9e4066Sahrens 	    tx->tx_space_tofree, refcount_count(&tx->tx_space_freed));
1128fa9e4066Sahrens 	refcount_destroy_many(&tx->tx_space_written,
1129fa9e4066Sahrens 	    refcount_count(&tx->tx_space_written));
1130fa9e4066Sahrens 	refcount_destroy_many(&tx->tx_space_freed,
1131fa9e4066Sahrens 	    refcount_count(&tx->tx_space_freed));
1132fa9e4066Sahrens #endif
1133fa9e4066Sahrens 	kmem_free(tx, sizeof (dmu_tx_t));
1134fa9e4066Sahrens }
1135fa9e4066Sahrens 
1136fa9e4066Sahrens void
1137fa9e4066Sahrens dmu_tx_abort(dmu_tx_t *tx)
1138fa9e4066Sahrens {
11398a2f1b91Sahrens 	dmu_tx_hold_t *txh;
1140fa9e4066Sahrens 
1141fa9e4066Sahrens 	ASSERT(tx->tx_txg == 0);
1142fa9e4066Sahrens 
11438a2f1b91Sahrens 	while (txh = list_head(&tx->tx_holds)) {
11448a2f1b91Sahrens 		dnode_t *dn = txh->txh_dnode;
1145fa9e4066Sahrens 
11468a2f1b91Sahrens 		list_remove(&tx->tx_holds, txh);
11478a2f1b91Sahrens 		kmem_free(txh, sizeof (dmu_tx_hold_t));
1148fa9e4066Sahrens 		if (dn != NULL)
1149fa9e4066Sahrens 			dnode_rele(dn, tx);
1150fa9e4066Sahrens 	}
1151d20e665cSRicardo M. Correia 
1152d20e665cSRicardo M. Correia 	/*
1153d20e665cSRicardo M. Correia 	 * Call any registered callbacks with an error code.
1154d20e665cSRicardo M. Correia 	 */
1155d20e665cSRicardo M. Correia 	if (!list_is_empty(&tx->tx_callbacks))
1156d20e665cSRicardo M. Correia 		dmu_tx_do_callbacks(&tx->tx_callbacks, ECANCELED);
1157d20e665cSRicardo M. Correia 
1158d20e665cSRicardo M. Correia 	list_destroy(&tx->tx_callbacks);
11598f38d419Sek 	list_destroy(&tx->tx_holds);
11608a2f1b91Sahrens #ifdef ZFS_DEBUG
1161fa9e4066Sahrens 	refcount_destroy_many(&tx->tx_space_written,
1162fa9e4066Sahrens 	    refcount_count(&tx->tx_space_written));
1163fa9e4066Sahrens 	refcount_destroy_many(&tx->tx_space_freed,
1164fa9e4066Sahrens 	    refcount_count(&tx->tx_space_freed));
1165fa9e4066Sahrens #endif
1166fa9e4066Sahrens 	kmem_free(tx, sizeof (dmu_tx_t));
1167fa9e4066Sahrens }
1168fa9e4066Sahrens 
1169fa9e4066Sahrens uint64_t
1170fa9e4066Sahrens dmu_tx_get_txg(dmu_tx_t *tx)
1171fa9e4066Sahrens {
1172fa9e4066Sahrens 	ASSERT(tx->tx_txg != 0);
1173fa9e4066Sahrens 	return (tx->tx_txg);
1174fa9e4066Sahrens }
1175d20e665cSRicardo M. Correia 
1176d20e665cSRicardo M. Correia void
1177d20e665cSRicardo M. Correia dmu_tx_callback_register(dmu_tx_t *tx, dmu_tx_callback_func_t *func, void *data)
1178d20e665cSRicardo M. Correia {
1179d20e665cSRicardo M. Correia 	dmu_tx_callback_t *dcb;
1180d20e665cSRicardo M. Correia 
1181d20e665cSRicardo M. Correia 	dcb = kmem_alloc(sizeof (dmu_tx_callback_t), KM_SLEEP);
1182d20e665cSRicardo M. Correia 
1183d20e665cSRicardo M. Correia 	dcb->dcb_func = func;
1184d20e665cSRicardo M. Correia 	dcb->dcb_data = data;
1185d20e665cSRicardo M. Correia 
1186d20e665cSRicardo M. Correia 	list_insert_tail(&tx->tx_callbacks, dcb);
1187d20e665cSRicardo M. Correia }
1188d20e665cSRicardo M. Correia 
1189d20e665cSRicardo M. Correia /*
1190d20e665cSRicardo M. Correia  * Call all the commit callbacks on a list, with a given error code.
1191d20e665cSRicardo M. Correia  */
1192d20e665cSRicardo M. Correia void
1193d20e665cSRicardo M. Correia dmu_tx_do_callbacks(list_t *cb_list, int error)
1194d20e665cSRicardo M. Correia {
1195d20e665cSRicardo M. Correia 	dmu_tx_callback_t *dcb;
1196d20e665cSRicardo M. Correia 
1197d20e665cSRicardo M. Correia 	while (dcb = list_head(cb_list)) {
1198d20e665cSRicardo M. Correia 		list_remove(cb_list, dcb);
1199d20e665cSRicardo M. Correia 		dcb->dcb_func(dcb->dcb_data, error);
1200d20e665cSRicardo M. Correia 		kmem_free(dcb, sizeof (dmu_tx_callback_t));
1201d20e665cSRicardo M. Correia 	}
1202d20e665cSRicardo M. Correia }
1203