xref: /illumos-gate/usr/src/uts/common/fs/zfs/dmu_tx.c (revision 2f3d878000c3b33cde13e16c4f0a1ab15d883a18)
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 /*
2201025c89SJohn Harres  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
239dccfd2aSAlbert Lee  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
24bda88194SGeorge Wilson  * Copyright (c) 2013 by Delphix. All rights reserved.
259dccfd2aSAlbert Lee  */
26fa9e4066Sahrens 
27fa9e4066Sahrens #include <sys/dmu.h>
28fa9e4066Sahrens #include <sys/dmu_impl.h>
29fa9e4066Sahrens #include <sys/dbuf.h>
30fa9e4066Sahrens #include <sys/dmu_tx.h>
31fa9e4066Sahrens #include <sys/dmu_objset.h>
32fa9e4066Sahrens #include <sys/dsl_dataset.h> /* for dsl_dataset_block_freeable() */
33fa9e4066Sahrens #include <sys/dsl_dir.h> /* for dsl_dir_tempreserve_*() */
34fa9e4066Sahrens #include <sys/dsl_pool.h>
358a2f1b91Sahrens #include <sys/zap_impl.h> /* for fzap_default_block_shift */
36fa9e4066Sahrens #include <sys/spa.h>
370a586ceaSMark Shellenbaum #include <sys/sa.h>
380a586ceaSMark Shellenbaum #include <sys/sa_impl.h>
39fa9e4066Sahrens #include <sys/zfs_context.h>
400a586ceaSMark Shellenbaum #include <sys/varargs.h>
41fa9e4066Sahrens 
42ea8dc4b6Seschrock typedef void (*dmu_tx_hold_func_t)(dmu_tx_t *tx, struct dnode *dn,
43ea8dc4b6Seschrock     uint64_t arg1, uint64_t arg2);
44ea8dc4b6Seschrock 
45fa9e4066Sahrens 
46fa9e4066Sahrens dmu_tx_t *
471d452cf5Sahrens dmu_tx_create_dd(dsl_dir_t *dd)
48fa9e4066Sahrens {
49fa9e4066Sahrens 	dmu_tx_t *tx = kmem_zalloc(sizeof (dmu_tx_t), KM_SLEEP);
50fa9e4066Sahrens 	tx->tx_dir = dd;
514445fffbSMatthew Ahrens 	if (dd != NULL)
52fa9e4066Sahrens 		tx->tx_pool = dd->dd_pool;
53fa9e4066Sahrens 	list_create(&tx->tx_holds, sizeof (dmu_tx_hold_t),
548a2f1b91Sahrens 	    offsetof(dmu_tx_hold_t, txh_node));
55d20e665cSRicardo M. Correia 	list_create(&tx->tx_callbacks, sizeof (dmu_tx_callback_t),
56d20e665cSRicardo M. Correia 	    offsetof(dmu_tx_callback_t, dcb_node));
578a2f1b91Sahrens #ifdef ZFS_DEBUG
58fa9e4066Sahrens 	refcount_create(&tx->tx_space_written);
59fa9e4066Sahrens 	refcount_create(&tx->tx_space_freed);
608a2f1b91Sahrens #endif
61fa9e4066Sahrens 	return (tx);
62fa9e4066Sahrens }
63fa9e4066Sahrens 
64fa9e4066Sahrens dmu_tx_t *
65fa9e4066Sahrens dmu_tx_create(objset_t *os)
66fa9e4066Sahrens {
67503ad85cSMatthew Ahrens 	dmu_tx_t *tx = dmu_tx_create_dd(os->os_dsl_dataset->ds_dir);
68fa9e4066Sahrens 	tx->tx_objset = os;
69503ad85cSMatthew Ahrens 	tx->tx_lastsnap_txg = dsl_dataset_prev_snap_txg(os->os_dsl_dataset);
70fa9e4066Sahrens 	return (tx);
71fa9e4066Sahrens }
72fa9e4066Sahrens 
73fa9e4066Sahrens dmu_tx_t *
74fa9e4066Sahrens dmu_tx_create_assigned(struct dsl_pool *dp, uint64_t txg)
75fa9e4066Sahrens {
761d452cf5Sahrens 	dmu_tx_t *tx = dmu_tx_create_dd(NULL);
77fa9e4066Sahrens 
78fa9e4066Sahrens 	ASSERT3U(txg, <=, dp->dp_tx.tx_open_txg);
79fa9e4066Sahrens 	tx->tx_pool = dp;
80fa9e4066Sahrens 	tx->tx_txg = txg;
81fa9e4066Sahrens 	tx->tx_anyobj = TRUE;
82fa9e4066Sahrens 
83fa9e4066Sahrens 	return (tx);
84fa9e4066Sahrens }
85fa9e4066Sahrens 
86fa9e4066Sahrens int
87fa9e4066Sahrens dmu_tx_is_syncing(dmu_tx_t *tx)
88fa9e4066Sahrens {
89fa9e4066Sahrens 	return (tx->tx_anyobj);
90fa9e4066Sahrens }
91fa9e4066Sahrens 
92fa9e4066Sahrens int
93fa9e4066Sahrens dmu_tx_private_ok(dmu_tx_t *tx)
94fa9e4066Sahrens {
95ea8dc4b6Seschrock 	return (tx->tx_anyobj);
96fa9e4066Sahrens }
97fa9e4066Sahrens 
988a2f1b91Sahrens static dmu_tx_hold_t *
99fa9e4066Sahrens dmu_tx_hold_object_impl(dmu_tx_t *tx, objset_t *os, uint64_t object,
1008a2f1b91Sahrens     enum dmu_tx_hold_type type, uint64_t arg1, uint64_t arg2)
101fa9e4066Sahrens {
1028a2f1b91Sahrens 	dmu_tx_hold_t *txh;
103fa9e4066Sahrens 	dnode_t *dn = NULL;
104ea8dc4b6Seschrock 	int err;
105fa9e4066Sahrens 
106fa9e4066Sahrens 	if (object != DMU_NEW_OBJECT) {
107503ad85cSMatthew Ahrens 		err = dnode_hold(os, object, tx, &dn);
108ea8dc4b6Seschrock 		if (err) {
109ea8dc4b6Seschrock 			tx->tx_err = err;
1108a2f1b91Sahrens 			return (NULL);
111ea8dc4b6Seschrock 		}
112fa9e4066Sahrens 
113ea8dc4b6Seschrock 		if (err == 0 && tx->tx_txg != 0) {
114fa9e4066Sahrens 			mutex_enter(&dn->dn_mtx);
115fa9e4066Sahrens 			/*
116fa9e4066Sahrens 			 * dn->dn_assigned_txg == tx->tx_txg doesn't pose a
117fa9e4066Sahrens 			 * problem, but there's no way for it to happen (for
118fa9e4066Sahrens 			 * now, at least).
119fa9e4066Sahrens 			 */
120fa9e4066Sahrens 			ASSERT(dn->dn_assigned_txg == 0);
121fa9e4066Sahrens 			dn->dn_assigned_txg = tx->tx_txg;
122fa9e4066Sahrens 			(void) refcount_add(&dn->dn_tx_holds, tx);
123fa9e4066Sahrens 			mutex_exit(&dn->dn_mtx);
124fa9e4066Sahrens 		}
125fa9e4066Sahrens 	}
126fa9e4066Sahrens 
1278a2f1b91Sahrens 	txh = kmem_zalloc(sizeof (dmu_tx_hold_t), KM_SLEEP);
1288a2f1b91Sahrens 	txh->txh_tx = tx;
1298a2f1b91Sahrens 	txh->txh_dnode = dn;
1308a2f1b91Sahrens #ifdef ZFS_DEBUG
1318a2f1b91Sahrens 	txh->txh_type = type;
1328a2f1b91Sahrens 	txh->txh_arg1 = arg1;
1338a2f1b91Sahrens 	txh->txh_arg2 = arg2;
1348a2f1b91Sahrens #endif
1358a2f1b91Sahrens 	list_insert_tail(&tx->tx_holds, txh);
136ea8dc4b6Seschrock 
1378a2f1b91Sahrens 	return (txh);
138fa9e4066Sahrens }
139fa9e4066Sahrens 
140fa9e4066Sahrens void
141fa9e4066Sahrens dmu_tx_add_new_object(dmu_tx_t *tx, objset_t *os, uint64_t object)
142fa9e4066Sahrens {
143fa9e4066Sahrens 	/*
144fa9e4066Sahrens 	 * If we're syncing, they can manipulate any object anyhow, and
145fa9e4066Sahrens 	 * the hold on the dnode_t can cause problems.
146fa9e4066Sahrens 	 */
147fa9e4066Sahrens 	if (!dmu_tx_is_syncing(tx)) {
1488a2f1b91Sahrens 		(void) dmu_tx_hold_object_impl(tx, os,
1498a2f1b91Sahrens 		    object, THT_NEWOBJECT, 0, 0);
150fa9e4066Sahrens 	}
151fa9e4066Sahrens }
152fa9e4066Sahrens 
153ea8dc4b6Seschrock static int
154ea8dc4b6Seschrock dmu_tx_check_ioerr(zio_t *zio, dnode_t *dn, int level, uint64_t blkid)
155ea8dc4b6Seschrock {
156ea8dc4b6Seschrock 	int err;
157ea8dc4b6Seschrock 	dmu_buf_impl_t *db;
158ea8dc4b6Seschrock 
159ea8dc4b6Seschrock 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
160ea8dc4b6Seschrock 	db = dbuf_hold_level(dn, level, blkid, FTAG);
161ea8dc4b6Seschrock 	rw_exit(&dn->dn_struct_rwlock);
162ea8dc4b6Seschrock 	if (db == NULL)
163be6fd75aSMatthew Ahrens 		return (SET_ERROR(EIO));
1641ab7f2deSmaybee 	err = dbuf_read(db, zio, DB_RF_CANFAIL | DB_RF_NOPREFETCH);
165ea8dc4b6Seschrock 	dbuf_rele(db, FTAG);
166ea8dc4b6Seschrock 	return (err);
167ea8dc4b6Seschrock }
168ea8dc4b6Seschrock 
1694a7f2a75SMark Maybee static void
170b24ab676SJeff Bonwick dmu_tx_count_twig(dmu_tx_hold_t *txh, dnode_t *dn, dmu_buf_impl_t *db,
171b24ab676SJeff Bonwick     int level, uint64_t blkid, boolean_t freeable, uint64_t *history)
1724a7f2a75SMark Maybee {
173b24ab676SJeff Bonwick 	objset_t *os = dn->dn_objset;
174b24ab676SJeff Bonwick 	dsl_dataset_t *ds = os->os_dsl_dataset;
175b24ab676SJeff Bonwick 	int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
176b24ab676SJeff Bonwick 	dmu_buf_impl_t *parent = NULL;
177b24ab676SJeff Bonwick 	blkptr_t *bp = NULL;
178b24ab676SJeff Bonwick 	uint64_t space;
179b24ab676SJeff Bonwick 
180b24ab676SJeff Bonwick 	if (level >= dn->dn_nlevels || history[level] == blkid)
1814a7f2a75SMark Maybee 		return;
1824a7f2a75SMark Maybee 
183b24ab676SJeff Bonwick 	history[level] = blkid;
1844a7f2a75SMark Maybee 
185b24ab676SJeff Bonwick 	space = (level == 0) ? dn->dn_datablksz : (1ULL << dn->dn_indblkshift);
1864a7f2a75SMark Maybee 
187b24ab676SJeff Bonwick 	if (db == NULL || db == dn->dn_dbuf) {
188b24ab676SJeff Bonwick 		ASSERT(level != 0);
189b24ab676SJeff Bonwick 		db = NULL;
190b24ab676SJeff Bonwick 	} else {
191744947dcSTom Erickson 		ASSERT(DB_DNODE(db) == dn);
192b24ab676SJeff Bonwick 		ASSERT(db->db_level == level);
193b24ab676SJeff Bonwick 		ASSERT(db->db.db_size == space);
194b24ab676SJeff Bonwick 		ASSERT(db->db_blkid == blkid);
195b24ab676SJeff Bonwick 		bp = db->db_blkptr;
196b24ab676SJeff Bonwick 		parent = db->db_parent;
1974a7f2a75SMark Maybee 	}
198b24ab676SJeff Bonwick 
199b24ab676SJeff Bonwick 	freeable = (bp && (freeable ||
200c7cd2421SGeorge Wilson 	    dsl_dataset_block_freeable(ds, bp, bp->blk_birth)));
201b24ab676SJeff Bonwick 
202b24ab676SJeff Bonwick 	if (freeable)
203b24ab676SJeff Bonwick 		txh->txh_space_tooverwrite += space;
204b24ab676SJeff Bonwick 	else
205b24ab676SJeff Bonwick 		txh->txh_space_towrite += space;
206b24ab676SJeff Bonwick 	if (bp)
207b24ab676SJeff Bonwick 		txh->txh_space_tounref += bp_get_dsize(os->os_spa, bp);
208b24ab676SJeff Bonwick 
209b24ab676SJeff Bonwick 	dmu_tx_count_twig(txh, dn, parent, level + 1,
210b24ab676SJeff Bonwick 	    blkid >> epbs, freeable, history);
2114a7f2a75SMark Maybee }
2124a7f2a75SMark Maybee 
213fa9e4066Sahrens /* ARGSUSED */
214fa9e4066Sahrens static void
2158a2f1b91Sahrens dmu_tx_count_write(dmu_tx_hold_t *txh, uint64_t off, uint64_t len)
216fa9e4066Sahrens {
2178a2f1b91Sahrens 	dnode_t *dn = txh->txh_dnode;
2188a2f1b91Sahrens 	uint64_t start, end, i;
219fa9e4066Sahrens 	int min_bs, max_bs, min_ibs, max_ibs, epbs, bits;
2208a2f1b91Sahrens 	int err = 0;
221fa9e4066Sahrens 
222fa9e4066Sahrens 	if (len == 0)
223fa9e4066Sahrens 		return;
224fa9e4066Sahrens 
225fa9e4066Sahrens 	min_bs = SPA_MINBLOCKSHIFT;
226fa9e4066Sahrens 	max_bs = SPA_MAXBLOCKSHIFT;
227fa9e4066Sahrens 	min_ibs = DN_MIN_INDBLKSHIFT;
228fa9e4066Sahrens 	max_ibs = DN_MAX_INDBLKSHIFT;
229fa9e4066Sahrens 
2308a2f1b91Sahrens 	if (dn) {
231b24ab676SJeff Bonwick 		uint64_t history[DN_MAX_LEVELS];
2324a7f2a75SMark Maybee 		int nlvls = dn->dn_nlevels;
2334a7f2a75SMark Maybee 		int delta;
2344a7f2a75SMark Maybee 
2354a7f2a75SMark Maybee 		/*
2364a7f2a75SMark Maybee 		 * For i/o error checking, read the first and last level-0
2374a7f2a75SMark Maybee 		 * blocks (if they are not aligned), and all the level-1 blocks.
2384a7f2a75SMark Maybee 		 */
239ea8dc4b6Seschrock 		if (dn->dn_maxblkid == 0) {
2404a7f2a75SMark Maybee 			delta = dn->dn_datablksz;
2414a7f2a75SMark Maybee 			start = (off < dn->dn_datablksz) ? 0 : 1;
2424a7f2a75SMark Maybee 			end = (off+len <= dn->dn_datablksz) ? 0 : 1;
2434a7f2a75SMark Maybee 			if (start == 0 && (off > 0 || len < dn->dn_datablksz)) {
24482c9918fSTim Haley 				err = dmu_tx_check_ioerr(NULL, dn, 0, 0);
24582c9918fSTim Haley 				if (err)
24682c9918fSTim Haley 					goto out;
2474a7f2a75SMark Maybee 				delta -= off;
24882c9918fSTim Haley 			}
249ea8dc4b6Seschrock 		} else {
2508a2f1b91Sahrens 			zio_t *zio = zio_root(dn->dn_objset->os_spa,
251ea8dc4b6Seschrock 			    NULL, NULL, ZIO_FLAG_CANFAIL);
252ea8dc4b6Seschrock 
253ea8dc4b6Seschrock 			/* first level-0 block */
25499653d4eSeschrock 			start = off >> dn->dn_datablkshift;
25599653d4eSeschrock 			if (P2PHASE(off, dn->dn_datablksz) ||
25699653d4eSeschrock 			    len < dn->dn_datablksz) {
25799653d4eSeschrock 				err = dmu_tx_check_ioerr(zio, dn, 0, start);
2588a2f1b91Sahrens 				if (err)
2598a2f1b91Sahrens 					goto out;
260ea8dc4b6Seschrock 			}
261ea8dc4b6Seschrock 
262ea8dc4b6Seschrock 			/* last level-0 block */
26399653d4eSeschrock 			end = (off+len-1) >> dn->dn_datablkshift;
26482c9918fSTim Haley 			if (end != start && end <= dn->dn_maxblkid &&
26599653d4eSeschrock 			    P2PHASE(off+len, dn->dn_datablksz)) {
266ea8dc4b6Seschrock 				err = dmu_tx_check_ioerr(zio, dn, 0, end);
2678a2f1b91Sahrens 				if (err)
2688a2f1b91Sahrens 					goto out;
269ea8dc4b6Seschrock 			}
270ea8dc4b6Seschrock 
271ea8dc4b6Seschrock 			/* level-1 blocks */
2724a7f2a75SMark Maybee 			if (nlvls > 1) {
2734a7f2a75SMark Maybee 				int shft = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
2744a7f2a75SMark Maybee 				for (i = (start>>shft)+1; i < end>>shft; i++) {
275ea8dc4b6Seschrock 					err = dmu_tx_check_ioerr(zio, dn, 1, i);
2768a2f1b91Sahrens 					if (err)
2778a2f1b91Sahrens 						goto out;
278ea8dc4b6Seschrock 				}
279ea8dc4b6Seschrock 			}
280ea8dc4b6Seschrock 
281ea8dc4b6Seschrock 			err = zio_wait(zio);
2828a2f1b91Sahrens 			if (err)
2838a2f1b91Sahrens 				goto out;
2844a7f2a75SMark Maybee 			delta = P2NPHASE(off, dn->dn_datablksz);
285ea8dc4b6Seschrock 		}
286ea8dc4b6Seschrock 
287bda88194SGeorge Wilson 		min_ibs = max_ibs = dn->dn_indblkshift;
2884a7f2a75SMark Maybee 		if (dn->dn_maxblkid > 0) {
2894a7f2a75SMark Maybee 			/*
2904a7f2a75SMark Maybee 			 * The blocksize can't change,
2914a7f2a75SMark Maybee 			 * so we can make a more precise estimate.
2924a7f2a75SMark Maybee 			 */
2934a7f2a75SMark Maybee 			ASSERT(dn->dn_datablkshift != 0);
294fa9e4066Sahrens 			min_bs = max_bs = dn->dn_datablkshift;
2954a7f2a75SMark Maybee 		}
2964a7f2a75SMark Maybee 
2974a7f2a75SMark Maybee 		/*
2984a7f2a75SMark Maybee 		 * If this write is not off the end of the file
2994a7f2a75SMark Maybee 		 * we need to account for overwrites/unref.
3004a7f2a75SMark Maybee 		 */
301b24ab676SJeff Bonwick 		if (start <= dn->dn_maxblkid) {
302b24ab676SJeff Bonwick 			for (int l = 0; l < DN_MAX_LEVELS; l++)
303b24ab676SJeff Bonwick 				history[l] = -1ULL;
304b24ab676SJeff Bonwick 		}
3054a7f2a75SMark Maybee 		while (start <= dn->dn_maxblkid) {
3064a7f2a75SMark Maybee 			dmu_buf_impl_t *db;
3074a7f2a75SMark Maybee 
3084a7f2a75SMark Maybee 			rw_enter(&dn->dn_struct_rwlock, RW_READER);
30901025c89SJohn Harres 			err = dbuf_hold_impl(dn, 0, start, FALSE, FTAG, &db);
3104a7f2a75SMark Maybee 			rw_exit(&dn->dn_struct_rwlock);
31101025c89SJohn Harres 
31201025c89SJohn Harres 			if (err) {
31301025c89SJohn Harres 				txh->txh_tx->tx_err = err;
31401025c89SJohn Harres 				return;
31501025c89SJohn Harres 			}
31601025c89SJohn Harres 
317b24ab676SJeff Bonwick 			dmu_tx_count_twig(txh, dn, db, 0, start, B_FALSE,
318b24ab676SJeff Bonwick 			    history);
3194a7f2a75SMark Maybee 			dbuf_rele(db, FTAG);
3204a7f2a75SMark Maybee 			if (++start > end) {
3214a7f2a75SMark Maybee 				/*
3224a7f2a75SMark Maybee 				 * Account for new indirects appearing
3234a7f2a75SMark Maybee 				 * before this IO gets assigned into a txg.
3244a7f2a75SMark Maybee 				 */
3254a7f2a75SMark Maybee 				bits = 64 - min_bs;
3264a7f2a75SMark Maybee 				epbs = min_ibs - SPA_BLKPTRSHIFT;
3274a7f2a75SMark Maybee 				for (bits -= epbs * (nlvls - 1);
3284a7f2a75SMark Maybee 				    bits >= 0; bits -= epbs)
3294a7f2a75SMark Maybee 					txh->txh_fudge += 1ULL << max_ibs;
3304a7f2a75SMark Maybee 				goto out;
3314a7f2a75SMark Maybee 			}
3324a7f2a75SMark Maybee 			off += delta;
3334a7f2a75SMark Maybee 			if (len >= delta)
3344a7f2a75SMark Maybee 				len -= delta;
3354a7f2a75SMark Maybee 			delta = dn->dn_datablksz;
3364a7f2a75SMark Maybee 		}
337fa9e4066Sahrens 	}
338fa9e4066Sahrens 
339fa9e4066Sahrens 	/*
340fa9e4066Sahrens 	 * 'end' is the last thing we will access, not one past.
341fa9e4066Sahrens 	 * This way we won't overflow when accessing the last byte.
342fa9e4066Sahrens 	 */
343fa9e4066Sahrens 	start = P2ALIGN(off, 1ULL << max_bs);
344fa9e4066Sahrens 	end = P2ROUNDUP(off + len, 1ULL << max_bs) - 1;
3458a2f1b91Sahrens 	txh->txh_space_towrite += end - start + 1;
346fa9e4066Sahrens 
347fa9e4066Sahrens 	start >>= min_bs;
348fa9e4066Sahrens 	end >>= min_bs;
349fa9e4066Sahrens 
350fa9e4066Sahrens 	epbs = min_ibs - SPA_BLKPTRSHIFT;
351fa9e4066Sahrens 
352fa9e4066Sahrens 	/*
353fa9e4066Sahrens 	 * The object contains at most 2^(64 - min_bs) blocks,
354fa9e4066Sahrens 	 * and each indirect level maps 2^epbs.
355fa9e4066Sahrens 	 */
356fa9e4066Sahrens 	for (bits = 64 - min_bs; bits >= 0; bits -= epbs) {
357fa9e4066Sahrens 		start >>= epbs;
358fa9e4066Sahrens 		end >>= epbs;
3594a7f2a75SMark Maybee 		ASSERT3U(end, >=, start);
3608a2f1b91Sahrens 		txh->txh_space_towrite += (end - start + 1) << max_ibs;
3614a7f2a75SMark Maybee 		if (start != 0) {
3624a7f2a75SMark Maybee 			/*
3634a7f2a75SMark Maybee 			 * We also need a new blkid=0 indirect block
3644a7f2a75SMark Maybee 			 * to reference any existing file data.
3654a7f2a75SMark Maybee 			 */
3664a7f2a75SMark Maybee 			txh->txh_space_towrite += 1ULL << max_ibs;
3674a7f2a75SMark Maybee 		}
368fa9e4066Sahrens 	}
369fa9e4066Sahrens 
3708a2f1b91Sahrens out:
3714a7f2a75SMark Maybee 	if (txh->txh_space_towrite + txh->txh_space_tooverwrite >
3724a7f2a75SMark Maybee 	    2 * DMU_MAX_ACCESS)
373be6fd75aSMatthew Ahrens 		err = SET_ERROR(EFBIG);
3744a7f2a75SMark Maybee 
3758a2f1b91Sahrens 	if (err)
3768a2f1b91Sahrens 		txh->txh_tx->tx_err = err;
377fa9e4066Sahrens }
378fa9e4066Sahrens 
379fa9e4066Sahrens static void
3808a2f1b91Sahrens dmu_tx_count_dnode(dmu_tx_hold_t *txh)
381fa9e4066Sahrens {
3828a2f1b91Sahrens 	dnode_t *dn = txh->txh_dnode;
383744947dcSTom Erickson 	dnode_t *mdn = DMU_META_DNODE(txh->txh_tx->tx_objset);
3848a2f1b91Sahrens 	uint64_t space = mdn->dn_datablksz +
3858a2f1b91Sahrens 	    ((mdn->dn_nlevels-1) << mdn->dn_indblkshift);
386fa9e4066Sahrens 
387fa9e4066Sahrens 	if (dn && dn->dn_dbuf->db_blkptr &&
388fa9e4066Sahrens 	    dsl_dataset_block_freeable(dn->dn_objset->os_dsl_dataset,
389c7cd2421SGeorge Wilson 	    dn->dn_dbuf->db_blkptr, dn->dn_dbuf->db_blkptr->blk_birth)) {
3908a2f1b91Sahrens 		txh->txh_space_tooverwrite += space;
3914a7f2a75SMark Maybee 		txh->txh_space_tounref += space;
3928a2f1b91Sahrens 	} else {
3938a2f1b91Sahrens 		txh->txh_space_towrite += space;
394a9799022Sck 		if (dn && dn->dn_dbuf->db_blkptr)
395a9799022Sck 			txh->txh_space_tounref += space;
396fa9e4066Sahrens 	}
397fa9e4066Sahrens }
398fa9e4066Sahrens 
399fa9e4066Sahrens void
400fa9e4066Sahrens dmu_tx_hold_write(dmu_tx_t *tx, uint64_t object, uint64_t off, int len)
401fa9e4066Sahrens {
4028a2f1b91Sahrens 	dmu_tx_hold_t *txh;
4038a2f1b91Sahrens 
404fa9e4066Sahrens 	ASSERT(tx->tx_txg == 0);
405ea8dc4b6Seschrock 	ASSERT(len < DMU_MAX_ACCESS);
406dd6ef538Smaybee 	ASSERT(len == 0 || UINT64_MAX - off >= len - 1);
407fa9e4066Sahrens 
4088a2f1b91Sahrens 	txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
4098a2f1b91Sahrens 	    object, THT_WRITE, off, len);
4108a2f1b91Sahrens 	if (txh == NULL)
4118a2f1b91Sahrens 		return;
4128a2f1b91Sahrens 
4138a2f1b91Sahrens 	dmu_tx_count_write(txh, off, len);
4148a2f1b91Sahrens 	dmu_tx_count_dnode(txh);
415fa9e4066Sahrens }
416fa9e4066Sahrens 
417fa9e4066Sahrens static void
4188a2f1b91Sahrens dmu_tx_count_free(dmu_tx_hold_t *txh, uint64_t off, uint64_t len)
419fa9e4066Sahrens {
420cdb0ab79Smaybee 	uint64_t blkid, nblks, lastblk;
421cdb0ab79Smaybee 	uint64_t space = 0, unref = 0, skipped = 0;
4228a2f1b91Sahrens 	dnode_t *dn = txh->txh_dnode;
423fa9e4066Sahrens 	dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset;
4248a2f1b91Sahrens 	spa_t *spa = txh->txh_tx->tx_pool->dp_spa;
425cdb0ab79Smaybee 	int epbs;
42631495a1eSArne Jansen 	uint64_t l0span = 0, nl1blks = 0;
427fa9e4066Sahrens 
428cdb0ab79Smaybee 	if (dn->dn_nlevels == 0)
429fa9e4066Sahrens 		return;
430c543ec06Sahrens 
431fa9e4066Sahrens 	/*
432cdb0ab79Smaybee 	 * The struct_rwlock protects us against dn_nlevels
433c543ec06Sahrens 	 * changing, in case (against all odds) we manage to dirty &
434c543ec06Sahrens 	 * sync out the changes after we check for being dirty.
43501025c89SJohn Harres 	 * Also, dbuf_hold_impl() wants us to have the struct_rwlock.
436fa9e4066Sahrens 	 */
437fa9e4066Sahrens 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
438cdb0ab79Smaybee 	epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
439cdb0ab79Smaybee 	if (dn->dn_maxblkid == 0) {
440c543ec06Sahrens 		if (off == 0 && len >= dn->dn_datablksz) {
441c543ec06Sahrens 			blkid = 0;
442c543ec06Sahrens 			nblks = 1;
443c543ec06Sahrens 		} else {
444c543ec06Sahrens 			rw_exit(&dn->dn_struct_rwlock);
445c543ec06Sahrens 			return;
446c543ec06Sahrens 		}
447c543ec06Sahrens 	} else {
448c543ec06Sahrens 		blkid = off >> dn->dn_datablkshift;
449cdb0ab79Smaybee 		nblks = (len + dn->dn_datablksz - 1) >> dn->dn_datablkshift;
450fa9e4066Sahrens 
451cdb0ab79Smaybee 		if (blkid >= dn->dn_maxblkid) {
452c543ec06Sahrens 			rw_exit(&dn->dn_struct_rwlock);
453c543ec06Sahrens 			return;
454c543ec06Sahrens 		}
455cdb0ab79Smaybee 		if (blkid + nblks > dn->dn_maxblkid)
456cdb0ab79Smaybee 			nblks = dn->dn_maxblkid - blkid;
457fa9e4066Sahrens 
458c543ec06Sahrens 	}
45931495a1eSArne Jansen 	l0span = nblks;    /* save for later use to calc level > 1 overhead */
460cdb0ab79Smaybee 	if (dn->dn_nlevels == 1) {
461fa9e4066Sahrens 		int i;
462fa9e4066Sahrens 		for (i = 0; i < nblks; i++) {
463fa9e4066Sahrens 			blkptr_t *bp = dn->dn_phys->dn_blkptr;
464cdb0ab79Smaybee 			ASSERT3U(blkid + i, <, dn->dn_nblkptr);
465fa9e4066Sahrens 			bp += blkid + i;
466c7cd2421SGeorge Wilson 			if (dsl_dataset_block_freeable(ds, bp, bp->blk_birth)) {
467fa9e4066Sahrens 				dprintf_bp(bp, "can free old%s", "");
468b24ab676SJeff Bonwick 				space += bp_get_dsize(spa, bp);
469fa9e4066Sahrens 			}
470a9799022Sck 			unref += BP_GET_ASIZE(bp);
471fa9e4066Sahrens 		}
47231495a1eSArne Jansen 		nl1blks = 1;
473ea8dc4b6Seschrock 		nblks = 0;
474fa9e4066Sahrens 	}
475fa9e4066Sahrens 
476cdb0ab79Smaybee 	lastblk = blkid + nblks - 1;
477fa9e4066Sahrens 	while (nblks) {
478fa9e4066Sahrens 		dmu_buf_impl_t *dbuf;
479cdb0ab79Smaybee 		uint64_t ibyte, new_blkid;
480cdb0ab79Smaybee 		int epb = 1 << epbs;
481cdb0ab79Smaybee 		int err, i, blkoff, tochk;
482cdb0ab79Smaybee 		blkptr_t *bp;
483cdb0ab79Smaybee 
484cdb0ab79Smaybee 		ibyte = blkid << dn->dn_datablkshift;
485cdb0ab79Smaybee 		err = dnode_next_offset(dn,
486cdb0ab79Smaybee 		    DNODE_FIND_HAVELOCK, &ibyte, 2, 1, 0);
487cdb0ab79Smaybee 		new_blkid = ibyte >> dn->dn_datablkshift;
488b7e50089Smaybee 		if (err == ESRCH) {
489b7e50089Smaybee 			skipped += (lastblk >> epbs) - (blkid >> epbs) + 1;
490cdb0ab79Smaybee 			break;
491b7e50089Smaybee 		}
492cdb0ab79Smaybee 		if (err) {
493cdb0ab79Smaybee 			txh->txh_tx->tx_err = err;
494cdb0ab79Smaybee 			break;
495cdb0ab79Smaybee 		}
496b7e50089Smaybee 		if (new_blkid > lastblk) {
497b7e50089Smaybee 			skipped += (lastblk >> epbs) - (blkid >> epbs) + 1;
498cdb0ab79Smaybee 			break;
499b7e50089Smaybee 		}
500fa9e4066Sahrens 
501cdb0ab79Smaybee 		if (new_blkid > blkid) {
502b7e50089Smaybee 			ASSERT((new_blkid >> epbs) > (blkid >> epbs));
503b7e50089Smaybee 			skipped += (new_blkid >> epbs) - (blkid >> epbs) - 1;
504cdb0ab79Smaybee 			nblks -= new_blkid - blkid;
505cdb0ab79Smaybee 			blkid = new_blkid;
506cdb0ab79Smaybee 		}
507cdb0ab79Smaybee 		blkoff = P2PHASE(blkid, epb);
508cdb0ab79Smaybee 		tochk = MIN(epb - blkoff, nblks);
509fa9e4066Sahrens 
51001025c89SJohn Harres 		err = dbuf_hold_impl(dn, 1, blkid >> epbs, FALSE, FTAG, &dbuf);
51101025c89SJohn Harres 		if (err) {
51201025c89SJohn Harres 			txh->txh_tx->tx_err = err;
51301025c89SJohn Harres 			break;
51401025c89SJohn Harres 		}
515cdb0ab79Smaybee 
516cdb0ab79Smaybee 		txh->txh_memory_tohold += dbuf->db.db_size;
51777179d12SLori Alt 
51877179d12SLori Alt 		/*
51977179d12SLori Alt 		 * We don't check memory_tohold against DMU_MAX_ACCESS because
52077179d12SLori Alt 		 * memory_tohold is an over-estimation (especially the >L1
52177179d12SLori Alt 		 * indirect blocks), so it could fail.  Callers should have
52277179d12SLori Alt 		 * already verified that they will not be holding too much
52377179d12SLori Alt 		 * memory.
52477179d12SLori Alt 		 */
52577179d12SLori Alt 
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++) {
537c7cd2421SGeorge Wilson 			if (dsl_dataset_block_freeable(ds, &bp[i],
538c7cd2421SGeorge Wilson 			    bp[i].blk_birth)) {
539cdb0ab79Smaybee 				dprintf_bp(&bp[i], "can free old%s", "");
540b24ab676SJeff Bonwick 				space += bp_get_dsize(spa, &bp[i]);
541cdb0ab79Smaybee 			}
542cdb0ab79Smaybee 			unref += BP_GET_ASIZE(bp);
543cdb0ab79Smaybee 		}
544cdb0ab79Smaybee 		dbuf_rele(dbuf, FTAG);
545cdb0ab79Smaybee 
54631495a1eSArne Jansen 		++nl1blks;
547fa9e4066Sahrens 		blkid += tochk;
548fa9e4066Sahrens 		nblks -= tochk;
549fa9e4066Sahrens 	}
550fa9e4066Sahrens 	rw_exit(&dn->dn_struct_rwlock);
551fa9e4066Sahrens 
55231495a1eSArne Jansen 	/*
55331495a1eSArne Jansen 	 * Add in memory requirements of higher-level indirects.
55431495a1eSArne Jansen 	 * This assumes a worst-possible scenario for dn_nlevels and a
55531495a1eSArne Jansen 	 * worst-possible distribution of l1-blocks over the region to free.
55631495a1eSArne Jansen 	 */
55731495a1eSArne Jansen 	{
55831495a1eSArne Jansen 		uint64_t blkcnt = 1 + ((l0span >> epbs) >> epbs);
55931495a1eSArne Jansen 		int level = 2;
56031495a1eSArne Jansen 		/*
56131495a1eSArne Jansen 		 * Here we don't use DN_MAX_LEVEL, but calculate it with the
56231495a1eSArne Jansen 		 * given datablkshift and indblkshift. This makes the
56331495a1eSArne Jansen 		 * difference between 19 and 8 on large files.
56431495a1eSArne Jansen 		 */
56531495a1eSArne Jansen 		int maxlevel = 2 + (DN_MAX_OFFSET_SHIFT - dn->dn_datablkshift) /
56631495a1eSArne Jansen 		    (dn->dn_indblkshift - SPA_BLKPTRSHIFT);
56731495a1eSArne Jansen 
56831495a1eSArne Jansen 		while (level++ < maxlevel) {
5698f0b538dSChristopher Siden 			txh->txh_memory_tohold += MAX(MIN(blkcnt, nl1blks), 1)
57031495a1eSArne Jansen 			    << dn->dn_indblkshift;
57131495a1eSArne Jansen 			blkcnt = 1 + (blkcnt >> epbs);
57231495a1eSArne Jansen 		}
57331495a1eSArne Jansen 	}
57431495a1eSArne Jansen 
575cdb0ab79Smaybee 	/* account for new level 1 indirect blocks that might show up */
576b7e50089Smaybee 	if (skipped > 0) {
577715614a4Smaybee 		txh->txh_fudge += skipped << dn->dn_indblkshift;
578cdb0ab79Smaybee 		skipped = MIN(skipped, DMU_MAX_DELETEBLKCNT >> epbs);
579cdb0ab79Smaybee 		txh->txh_memory_tohold += skipped << dn->dn_indblkshift;
580cdb0ab79Smaybee 	}
5818a2f1b91Sahrens 	txh->txh_space_tofree += space;
582a9799022Sck 	txh->txh_space_tounref += unref;
583fa9e4066Sahrens }
584fa9e4066Sahrens 
5858a2f1b91Sahrens void
5868a2f1b91Sahrens dmu_tx_hold_free(dmu_tx_t *tx, uint64_t object, uint64_t off, uint64_t len)
587fa9e4066Sahrens {
5888a2f1b91Sahrens 	dmu_tx_hold_t *txh;
5898a2f1b91Sahrens 	dnode_t *dn;
590*2f3d8780SMatthew Ahrens 	int err;
591ea8dc4b6Seschrock 	zio_t *zio;
592fa9e4066Sahrens 
5938a2f1b91Sahrens 	ASSERT(tx->tx_txg == 0);
5948a2f1b91Sahrens 
5958a2f1b91Sahrens 	txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
5968a2f1b91Sahrens 	    object, THT_FREE, off, len);
5978a2f1b91Sahrens 	if (txh == NULL)
5988a2f1b91Sahrens 		return;
5998a2f1b91Sahrens 	dn = txh->txh_dnode;
6008a2f1b91Sahrens 
601fa9e4066Sahrens 	if (off >= (dn->dn_maxblkid+1) * dn->dn_datablksz)
602fa9e4066Sahrens 		return;
603fa9e4066Sahrens 	if (len == DMU_OBJECT_END)
604fa9e4066Sahrens 		len = (dn->dn_maxblkid+1) * dn->dn_datablksz - off;
605fa9e4066Sahrens 
606*2f3d8780SMatthew Ahrens 	dmu_tx_count_dnode(txh);
607*2f3d8780SMatthew Ahrens 
608ea8dc4b6Seschrock 	/*
609*2f3d8780SMatthew Ahrens 	 * For i/o error checking, we read the first and last level-0
610*2f3d8780SMatthew Ahrens 	 * blocks if they are not aligned, and all the level-1 blocks.
611*2f3d8780SMatthew Ahrens 	 *
612*2f3d8780SMatthew Ahrens 	 * Note:  dbuf_free_range() assumes that we have not instantiated
613*2f3d8780SMatthew Ahrens 	 * any level-0 dbufs that will be completely freed.  Therefore we must
614*2f3d8780SMatthew Ahrens 	 * exercise care to not read or count the first and last blocks
615*2f3d8780SMatthew Ahrens 	 * if they are blocksize-aligned.
616*2f3d8780SMatthew Ahrens 	 */
617*2f3d8780SMatthew Ahrens 	if (dn->dn_datablkshift == 0) {
618*2f3d8780SMatthew Ahrens 		dmu_tx_count_write(txh, off, len);
619*2f3d8780SMatthew Ahrens 	} else {
620*2f3d8780SMatthew Ahrens 		/* first block will be modified if it is not aligned */
621*2f3d8780SMatthew Ahrens 		if (!IS_P2ALIGNED(off, 1 << dn->dn_datablkshift))
622*2f3d8780SMatthew Ahrens 			dmu_tx_count_write(txh, off, 1);
623*2f3d8780SMatthew Ahrens 		/* last block will be modified if it is not aligned */
624*2f3d8780SMatthew Ahrens 		if (!IS_P2ALIGNED(off + len, 1 << dn->dn_datablkshift))
625*2f3d8780SMatthew Ahrens 			dmu_tx_count_write(txh, off+len, 1);
626*2f3d8780SMatthew Ahrens 	}
627*2f3d8780SMatthew Ahrens 
628*2f3d8780SMatthew Ahrens 	/*
629*2f3d8780SMatthew Ahrens 	 * Check level-1 blocks.
630ea8dc4b6Seschrock 	 */
63198572ac1Sahrens 	if (dn->dn_nlevels > 1) {
632*2f3d8780SMatthew Ahrens 		int shift = dn->dn_datablkshift + dn->dn_indblkshift -
63398572ac1Sahrens 		    SPA_BLKPTRSHIFT;
634*2f3d8780SMatthew Ahrens 		uint64_t start = off >> shift;
635*2f3d8780SMatthew Ahrens 		uint64_t end = (off + len) >> shift;
636*2f3d8780SMatthew Ahrens 
637*2f3d8780SMatthew Ahrens 		ASSERT(dn->dn_datablkshift != 0);
638*2f3d8780SMatthew Ahrens 		ASSERT(dn->dn_indblkshift != 0);
63998572ac1Sahrens 
64098572ac1Sahrens 		zio = zio_root(tx->tx_pool->dp_spa,
64198572ac1Sahrens 		    NULL, NULL, ZIO_FLAG_CANFAIL);
642*2f3d8780SMatthew Ahrens 		for (uint64_t i = start; i <= end; i++) {
64398572ac1Sahrens 			uint64_t ibyte = i << shift;
644cdb0ab79Smaybee 			err = dnode_next_offset(dn, 0, &ibyte, 2, 1, 0);
64598572ac1Sahrens 			i = ibyte >> shift;
64698572ac1Sahrens 			if (err == ESRCH)
64798572ac1Sahrens 				break;
64898572ac1Sahrens 			if (err) {
64998572ac1Sahrens 				tx->tx_err = err;
65098572ac1Sahrens 				return;
65198572ac1Sahrens 			}
652ea8dc4b6Seschrock 
65398572ac1Sahrens 			err = dmu_tx_check_ioerr(zio, dn, 1, i);
65498572ac1Sahrens 			if (err) {
65598572ac1Sahrens 				tx->tx_err = err;
65698572ac1Sahrens 				return;
65798572ac1Sahrens 			}
65898572ac1Sahrens 		}
65998572ac1Sahrens 		err = zio_wait(zio);
660ea8dc4b6Seschrock 		if (err) {
661ea8dc4b6Seschrock 			tx->tx_err = err;
662ea8dc4b6Seschrock 			return;
663ea8dc4b6Seschrock 		}
664ea8dc4b6Seschrock 	}
665ea8dc4b6Seschrock 
6668a2f1b91Sahrens 	dmu_tx_count_free(txh, off, len);
667fa9e4066Sahrens }
668fa9e4066Sahrens 
669fa9e4066Sahrens void
67014843421SMatthew Ahrens dmu_tx_hold_zap(dmu_tx_t *tx, uint64_t object, int add, const char *name)
671fa9e4066Sahrens {
6728a2f1b91Sahrens 	dmu_tx_hold_t *txh;
6738a2f1b91Sahrens 	dnode_t *dn;
674fa9e4066Sahrens 	uint64_t nblocks;
675ea8dc4b6Seschrock 	int epbs, err;
676fa9e4066Sahrens 
6778a2f1b91Sahrens 	ASSERT(tx->tx_txg == 0);
6788a2f1b91Sahrens 
6798a2f1b91Sahrens 	txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
6808a2f1b91Sahrens 	    object, THT_ZAP, add, (uintptr_t)name);
6818a2f1b91Sahrens 	if (txh == NULL)
6828a2f1b91Sahrens 		return;
6838a2f1b91Sahrens 	dn = txh->txh_dnode;
6848a2f1b91Sahrens 
6858a2f1b91Sahrens 	dmu_tx_count_dnode(txh);
686fa9e4066Sahrens 
687fa9e4066Sahrens 	if (dn == NULL) {
688fa9e4066Sahrens 		/*
689ea8dc4b6Seschrock 		 * We will be able to fit a new object's entries into one leaf
690fa9e4066Sahrens 		 * block.  So there will be at most 2 blocks total,
691fa9e4066Sahrens 		 * including the header block.
692fa9e4066Sahrens 		 */
6938a2f1b91Sahrens 		dmu_tx_count_write(txh, 0, 2 << fzap_default_block_shift);
694fa9e4066Sahrens 		return;
695fa9e4066Sahrens 	}
696fa9e4066Sahrens 
697ad135b5dSChristopher Siden 	ASSERT3P(DMU_OT_BYTESWAP(dn->dn_type), ==, DMU_BSWAP_ZAP);
698fa9e4066Sahrens 
699ea8dc4b6Seschrock 	if (dn->dn_maxblkid == 0 && !add) {
7009dccfd2aSAlbert Lee 		blkptr_t *bp;
7019dccfd2aSAlbert Lee 
702fa9e4066Sahrens 		/*
703fa9e4066Sahrens 		 * If there is only one block  (i.e. this is a micro-zap)
704ea8dc4b6Seschrock 		 * and we are not adding anything, the accounting is simple.
705fa9e4066Sahrens 		 */
706ea8dc4b6Seschrock 		err = dmu_tx_check_ioerr(NULL, dn, 0, 0);
707ea8dc4b6Seschrock 		if (err) {
708ea8dc4b6Seschrock 			tx->tx_err = err;
709ea8dc4b6Seschrock 			return;
710ea8dc4b6Seschrock 		}
711ea8dc4b6Seschrock 
712b6130eadSmaybee 		/*
713b6130eadSmaybee 		 * Use max block size here, since we don't know how much
714b6130eadSmaybee 		 * the size will change between now and the dbuf dirty call.
715b6130eadSmaybee 		 */
7169dccfd2aSAlbert Lee 		bp = &dn->dn_phys->dn_blkptr[0];
717fa9e4066Sahrens 		if (dsl_dataset_block_freeable(dn->dn_objset->os_dsl_dataset,
7189dccfd2aSAlbert Lee 		    bp, bp->blk_birth))
719b6130eadSmaybee 			txh->txh_space_tooverwrite += SPA_MAXBLOCKSIZE;
7209dccfd2aSAlbert Lee 		else
721b6130eadSmaybee 			txh->txh_space_towrite += SPA_MAXBLOCKSIZE;
7229dccfd2aSAlbert Lee 		if (!BP_IS_HOLE(bp))
723f878aa38SChris Kirby 			txh->txh_space_tounref += SPA_MAXBLOCKSIZE;
724fa9e4066Sahrens 		return;
725fa9e4066Sahrens 	}
726fa9e4066Sahrens 
727ea8dc4b6Seschrock 	if (dn->dn_maxblkid > 0 && name) {
728ea8dc4b6Seschrock 		/*
729ea8dc4b6Seschrock 		 * access the name in this fat-zap so that we'll check
730ea8dc4b6Seschrock 		 * for i/o errors to the leaf blocks, etc.
731ea8dc4b6Seschrock 		 */
732503ad85cSMatthew Ahrens 		err = zap_lookup(dn->dn_objset, dn->dn_object, name,
733ea8dc4b6Seschrock 		    8, 0, NULL);
734ea8dc4b6Seschrock 		if (err == EIO) {
735ea8dc4b6Seschrock 			tx->tx_err = err;
736ea8dc4b6Seschrock 			return;
737ea8dc4b6Seschrock 		}
738ea8dc4b6Seschrock 	}
739ea8dc4b6Seschrock 
740503ad85cSMatthew Ahrens 	err = zap_count_write(dn->dn_objset, dn->dn_object, name, add,
741720d1aa1SSanjeev Bagewadi 	    &txh->txh_space_towrite, &txh->txh_space_tooverwrite);
742fa9e4066Sahrens 
743fa9e4066Sahrens 	/*
744fa9e4066Sahrens 	 * If the modified blocks are scattered to the four winds,
745fa9e4066Sahrens 	 * we'll have to modify an indirect twig for each.
746fa9e4066Sahrens 	 */
747fa9e4066Sahrens 	epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
748fa9e4066Sahrens 	for (nblocks = dn->dn_maxblkid >> epbs; nblocks != 0; nblocks >>= epbs)
7493d692628SSanjeev Bagewadi 		if (dn->dn_objset->os_dsl_dataset->ds_phys->ds_prev_snap_obj)
7503d692628SSanjeev Bagewadi 			txh->txh_space_towrite += 3 << dn->dn_indblkshift;
7513d692628SSanjeev Bagewadi 		else
7523d692628SSanjeev Bagewadi 			txh->txh_space_tooverwrite += 3 << dn->dn_indblkshift;
753fa9e4066Sahrens }
754fa9e4066Sahrens 
755fa9e4066Sahrens void
756fa9e4066Sahrens dmu_tx_hold_bonus(dmu_tx_t *tx, uint64_t object)
757fa9e4066Sahrens {
7588a2f1b91Sahrens 	dmu_tx_hold_t *txh;
759fa9e4066Sahrens 
7608a2f1b91Sahrens 	ASSERT(tx->tx_txg == 0);
761fa9e4066Sahrens 
7628a2f1b91Sahrens 	txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
7638a2f1b91Sahrens 	    object, THT_BONUS, 0, 0);
7648a2f1b91Sahrens 	if (txh)
7658a2f1b91Sahrens 		dmu_tx_count_dnode(txh);
766fa9e4066Sahrens }
767fa9e4066Sahrens 
768fa9e4066Sahrens void
769fa9e4066Sahrens dmu_tx_hold_space(dmu_tx_t *tx, uint64_t space)
770fa9e4066Sahrens {
7718a2f1b91Sahrens 	dmu_tx_hold_t *txh;
772fa9e4066Sahrens 	ASSERT(tx->tx_txg == 0);
773fa9e4066Sahrens 
7748a2f1b91Sahrens 	txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
7758a2f1b91Sahrens 	    DMU_NEW_OBJECT, THT_SPACE, space, 0);
7768a2f1b91Sahrens 
7778a2f1b91Sahrens 	txh->txh_space_towrite += space;
778fa9e4066Sahrens }
779fa9e4066Sahrens 
780fa9e4066Sahrens int
781fa9e4066Sahrens dmu_tx_holds(dmu_tx_t *tx, uint64_t object)
782fa9e4066Sahrens {
7838a2f1b91Sahrens 	dmu_tx_hold_t *txh;
784fa9e4066Sahrens 	int holds = 0;
785fa9e4066Sahrens 
786fa9e4066Sahrens 	/*
787fa9e4066Sahrens 	 * By asserting that the tx is assigned, we're counting the
788fa9e4066Sahrens 	 * number of dn_tx_holds, which is the same as the number of
789fa9e4066Sahrens 	 * dn_holds.  Otherwise, we'd be counting dn_holds, but
790fa9e4066Sahrens 	 * dn_tx_holds could be 0.
791fa9e4066Sahrens 	 */
792fa9e4066Sahrens 	ASSERT(tx->tx_txg != 0);
793fa9e4066Sahrens 
794fa9e4066Sahrens 	/* if (tx->tx_anyobj == TRUE) */
795fa9e4066Sahrens 		/* return (0); */
796fa9e4066Sahrens 
7978a2f1b91Sahrens 	for (txh = list_head(&tx->tx_holds); txh;
7988a2f1b91Sahrens 	    txh = list_next(&tx->tx_holds, txh)) {
7998a2f1b91Sahrens 		if (txh->txh_dnode && txh->txh_dnode->dn_object == object)
800fa9e4066Sahrens 			holds++;
801fa9e4066Sahrens 	}
802fa9e4066Sahrens 
803fa9e4066Sahrens 	return (holds);
804fa9e4066Sahrens }
805fa9e4066Sahrens 
8069c9dc39aSek #ifdef ZFS_DEBUG
807fa9e4066Sahrens void
808fa9e4066Sahrens dmu_tx_dirty_buf(dmu_tx_t *tx, dmu_buf_impl_t *db)
809fa9e4066Sahrens {
8108a2f1b91Sahrens 	dmu_tx_hold_t *txh;
811fa9e4066Sahrens 	int match_object = FALSE, match_offset = FALSE;
812744947dcSTom Erickson 	dnode_t *dn;
813fa9e4066Sahrens 
814744947dcSTom Erickson 	DB_DNODE_ENTER(db);
815744947dcSTom Erickson 	dn = DB_DNODE(db);
816fa9e4066Sahrens 	ASSERT(tx->tx_txg != 0);
817503ad85cSMatthew Ahrens 	ASSERT(tx->tx_objset == NULL || dn->dn_objset == tx->tx_objset);
818fa9e4066Sahrens 	ASSERT3U(dn->dn_object, ==, db->db.db_object);
819fa9e4066Sahrens 
820744947dcSTom Erickson 	if (tx->tx_anyobj) {
821744947dcSTom Erickson 		DB_DNODE_EXIT(db);
822fa9e4066Sahrens 		return;
823744947dcSTom Erickson 	}
824fa9e4066Sahrens 
825fa9e4066Sahrens 	/* XXX No checking on the meta dnode for now */
826744947dcSTom Erickson 	if (db->db.db_object == DMU_META_DNODE_OBJECT) {
827744947dcSTom Erickson 		DB_DNODE_EXIT(db);
828fa9e4066Sahrens 		return;
829744947dcSTom Erickson 	}
830fa9e4066Sahrens 
8318a2f1b91Sahrens 	for (txh = list_head(&tx->tx_holds); txh;
8328a2f1b91Sahrens 	    txh = list_next(&tx->tx_holds, txh)) {
833fa9e4066Sahrens 		ASSERT(dn == NULL || dn->dn_assigned_txg == tx->tx_txg);
8348a2f1b91Sahrens 		if (txh->txh_dnode == dn && txh->txh_type != THT_NEWOBJECT)
835fa9e4066Sahrens 			match_object = TRUE;
8368a2f1b91Sahrens 		if (txh->txh_dnode == NULL || txh->txh_dnode == dn) {
837fa9e4066Sahrens 			int datablkshift = dn->dn_datablkshift ?
838fa9e4066Sahrens 			    dn->dn_datablkshift : SPA_MAXBLOCKSHIFT;
839fa9e4066Sahrens 			int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
840fa9e4066Sahrens 			int shift = datablkshift + epbs * db->db_level;
841fa9e4066Sahrens 			uint64_t beginblk = shift >= 64 ? 0 :
8428a2f1b91Sahrens 			    (txh->txh_arg1 >> shift);
843fa9e4066Sahrens 			uint64_t endblk = shift >= 64 ? 0 :
8448a2f1b91Sahrens 			    ((txh->txh_arg1 + txh->txh_arg2 - 1) >> shift);
845fa9e4066Sahrens 			uint64_t blkid = db->db_blkid;
846fa9e4066Sahrens 
8478a2f1b91Sahrens 			/* XXX txh_arg2 better not be zero... */
848fa9e4066Sahrens 
8498a2f1b91Sahrens 			dprintf("found txh type %x beginblk=%llx endblk=%llx\n",
8508a2f1b91Sahrens 			    txh->txh_type, beginblk, endblk);
851fa9e4066Sahrens 
8528a2f1b91Sahrens 			switch (txh->txh_type) {
853fa9e4066Sahrens 			case THT_WRITE:
854fa9e4066Sahrens 				if (blkid >= beginblk && blkid <= endblk)
855fa9e4066Sahrens 					match_offset = TRUE;
856fa9e4066Sahrens 				/*
857fa9e4066Sahrens 				 * We will let this hold work for the bonus
8580a586ceaSMark Shellenbaum 				 * or spill buffer so that we don't need to
8590a586ceaSMark Shellenbaum 				 * hold it when creating a new object.
860fa9e4066Sahrens 				 */
8610a586ceaSMark Shellenbaum 				if (blkid == DMU_BONUS_BLKID ||
8620a586ceaSMark Shellenbaum 				    blkid == DMU_SPILL_BLKID)
863fa9e4066Sahrens 					match_offset = TRUE;
864fa9e4066Sahrens 				/*
865fa9e4066Sahrens 				 * They might have to increase nlevels,
866fa9e4066Sahrens 				 * thus dirtying the new TLIBs.  Or the
867fa9e4066Sahrens 				 * might have to change the block size,
868fa9e4066Sahrens 				 * thus dirying the new lvl=0 blk=0.
869fa9e4066Sahrens 				 */
870fa9e4066Sahrens 				if (blkid == 0)
871fa9e4066Sahrens 					match_offset = TRUE;
872fa9e4066Sahrens 				break;
873fa9e4066Sahrens 			case THT_FREE:
874cdb0ab79Smaybee 				/*
875cdb0ab79Smaybee 				 * We will dirty all the level 1 blocks in
876cdb0ab79Smaybee 				 * the free range and perhaps the first and
877cdb0ab79Smaybee 				 * last level 0 block.
878cdb0ab79Smaybee 				 */
879cdb0ab79Smaybee 				if (blkid >= beginblk && (blkid <= endblk ||
880cdb0ab79Smaybee 				    txh->txh_arg2 == DMU_OBJECT_END))
881fa9e4066Sahrens 					match_offset = TRUE;
882fa9e4066Sahrens 				break;
8830a586ceaSMark Shellenbaum 			case THT_SPILL:
8840a586ceaSMark Shellenbaum 				if (blkid == DMU_SPILL_BLKID)
8850a586ceaSMark Shellenbaum 					match_offset = TRUE;
8860a586ceaSMark Shellenbaum 				break;
887fa9e4066Sahrens 			case THT_BONUS:
8880a586ceaSMark Shellenbaum 				if (blkid == DMU_BONUS_BLKID)
889fa9e4066Sahrens 					match_offset = TRUE;
890fa9e4066Sahrens 				break;
891fa9e4066Sahrens 			case THT_ZAP:
892fa9e4066Sahrens 				match_offset = TRUE;
893fa9e4066Sahrens 				break;
894fa9e4066Sahrens 			case THT_NEWOBJECT:
895fa9e4066Sahrens 				match_object = TRUE;
896fa9e4066Sahrens 				break;
897fa9e4066Sahrens 			default:
8988a2f1b91Sahrens 				ASSERT(!"bad txh_type");
899fa9e4066Sahrens 			}
900fa9e4066Sahrens 		}
901744947dcSTom Erickson 		if (match_object && match_offset) {
902744947dcSTom Erickson 			DB_DNODE_EXIT(db);
903fa9e4066Sahrens 			return;
904744947dcSTom Erickson 		}
905fa9e4066Sahrens 	}
906744947dcSTom Erickson 	DB_DNODE_EXIT(db);
907fa9e4066Sahrens 	panic("dirtying dbuf obj=%llx lvl=%u blkid=%llx but not tx_held\n",
908fa9e4066Sahrens 	    (u_longlong_t)db->db.db_object, db->db_level,
909fa9e4066Sahrens 	    (u_longlong_t)db->db_blkid);
910fa9e4066Sahrens }
9119c9dc39aSek #endif
912fa9e4066Sahrens 
913fa9e4066Sahrens static int
9143b2aab18SMatthew Ahrens dmu_tx_try_assign(dmu_tx_t *tx, txg_how_t txg_how)
915fa9e4066Sahrens {
9168a2f1b91Sahrens 	dmu_tx_hold_t *txh;
9170a4e9518Sgw 	spa_t *spa = tx->tx_pool->dp_spa;
918cdb0ab79Smaybee 	uint64_t memory, asize, fsize, usize;
919715614a4Smaybee 	uint64_t towrite, tofree, tooverwrite, tounref, tohold, fudge;
920fa9e4066Sahrens 
921fb09f5aaSMadhav Suresh 	ASSERT0(tx->tx_txg);
9220a4e9518Sgw 
9238a2f1b91Sahrens 	if (tx->tx_err)
9248a2f1b91Sahrens 		return (tx->tx_err);
925fa9e4066Sahrens 
926e14bb325SJeff Bonwick 	if (spa_suspended(spa)) {
9270a4e9518Sgw 		/*
9280a4e9518Sgw 		 * If the user has indicated a blocking failure mode
9290a4e9518Sgw 		 * then return ERESTART which will block in dmu_tx_wait().
9300a4e9518Sgw 		 * Otherwise, return EIO so that an error can get
9310a4e9518Sgw 		 * propagated back to the VOP calls.
9320a4e9518Sgw 		 *
9330a4e9518Sgw 		 * Note that we always honor the txg_how flag regardless
9340a4e9518Sgw 		 * of the failuremode setting.
9350a4e9518Sgw 		 */
9360a4e9518Sgw 		if (spa_get_failmode(spa) == ZIO_FAILURE_MODE_CONTINUE &&
9370a4e9518Sgw 		    txg_how != TXG_WAIT)
938be6fd75aSMatthew Ahrens 			return (SET_ERROR(EIO));
9390a4e9518Sgw 
940be6fd75aSMatthew Ahrens 		return (SET_ERROR(ERESTART));
9410a4e9518Sgw 	}
9420a4e9518Sgw 
943fa9e4066Sahrens 	tx->tx_txg = txg_hold_open(tx->tx_pool, &tx->tx_txgh);
9448a2f1b91Sahrens 	tx->tx_needassign_txh = NULL;
945fa9e4066Sahrens 
9468a2f1b91Sahrens 	/*
9478a2f1b91Sahrens 	 * NB: No error returns are allowed after txg_hold_open, but
9488a2f1b91Sahrens 	 * before processing the dnode holds, due to the
9498a2f1b91Sahrens 	 * dmu_tx_unassign() logic.
9508a2f1b91Sahrens 	 */
951fa9e4066Sahrens 
952715614a4Smaybee 	towrite = tofree = tooverwrite = tounref = tohold = fudge = 0;
9538a2f1b91Sahrens 	for (txh = list_head(&tx->tx_holds); txh;
9548a2f1b91Sahrens 	    txh = list_next(&tx->tx_holds, txh)) {
9558a2f1b91Sahrens 		dnode_t *dn = txh->txh_dnode;
956fa9e4066Sahrens 		if (dn != NULL) {
957fa9e4066Sahrens 			mutex_enter(&dn->dn_mtx);
9588a2f1b91Sahrens 			if (dn->dn_assigned_txg == tx->tx_txg - 1) {
9598a2f1b91Sahrens 				mutex_exit(&dn->dn_mtx);
9608a2f1b91Sahrens 				tx->tx_needassign_txh = txh;
961be6fd75aSMatthew Ahrens 				return (SET_ERROR(ERESTART));
962fa9e4066Sahrens 			}
9638a2f1b91Sahrens 			if (dn->dn_assigned_txg == 0)
964fa9e4066Sahrens 				dn->dn_assigned_txg = tx->tx_txg;
9658a2f1b91Sahrens 			ASSERT3U(dn->dn_assigned_txg, ==, tx->tx_txg);
966fa9e4066Sahrens 			(void) refcount_add(&dn->dn_tx_holds, tx);
967fa9e4066Sahrens 			mutex_exit(&dn->dn_mtx);
968fa9e4066Sahrens 		}
9698a2f1b91Sahrens 		towrite += txh->txh_space_towrite;
9708a2f1b91Sahrens 		tofree += txh->txh_space_tofree;
9718a2f1b91Sahrens 		tooverwrite += txh->txh_space_tooverwrite;
972a9799022Sck 		tounref += txh->txh_space_tounref;
973cdb0ab79Smaybee 		tohold += txh->txh_memory_tohold;
974715614a4Smaybee 		fudge += txh->txh_fudge;
975ea8dc4b6Seschrock 	}
976ea8dc4b6Seschrock 
977ea8dc4b6Seschrock 	/*
978ea8dc4b6Seschrock 	 * If a snapshot has been taken since we made our estimates,
979ea8dc4b6Seschrock 	 * assume that we won't be able to free or overwrite anything.
980ea8dc4b6Seschrock 	 */
981ea8dc4b6Seschrock 	if (tx->tx_objset &&
982503ad85cSMatthew Ahrens 	    dsl_dataset_prev_snap_txg(tx->tx_objset->os_dsl_dataset) >
983ea8dc4b6Seschrock 	    tx->tx_lastsnap_txg) {
9848a2f1b91Sahrens 		towrite += tooverwrite;
9858a2f1b91Sahrens 		tooverwrite = tofree = 0;
986fa9e4066Sahrens 	}
987fa9e4066Sahrens 
988cdb0ab79Smaybee 	/* needed allocation: worst-case estimate of write space */
989cdb0ab79Smaybee 	asize = spa_get_asize(tx->tx_pool->dp_spa, towrite + tooverwrite);
990cdb0ab79Smaybee 	/* freed space estimate: worst-case overwrite + free estimate */
9918a2f1b91Sahrens 	fsize = spa_get_asize(tx->tx_pool->dp_spa, tooverwrite) + tofree;
992cdb0ab79Smaybee 	/* convert unrefd space to worst-case estimate */
993a9799022Sck 	usize = spa_get_asize(tx->tx_pool->dp_spa, tounref);
994cdb0ab79Smaybee 	/* calculate memory footprint estimate */
995cdb0ab79Smaybee 	memory = towrite + tooverwrite + tohold;
9968a2f1b91Sahrens 
9978a2f1b91Sahrens #ifdef ZFS_DEBUG
998715614a4Smaybee 	/*
999715614a4Smaybee 	 * Add in 'tohold' to account for our dirty holds on this memory
1000715614a4Smaybee 	 * XXX - the "fudge" factor is to account for skipped blocks that
1001715614a4Smaybee 	 * we missed because dnode_next_offset() misses in-core-only blocks.
1002715614a4Smaybee 	 */
1003cdb0ab79Smaybee 	tx->tx_space_towrite = asize +
1004715614a4Smaybee 	    spa_get_asize(tx->tx_pool->dp_spa, tohold + fudge);
10058a2f1b91Sahrens 	tx->tx_space_tofree = tofree;
10068a2f1b91Sahrens 	tx->tx_space_tooverwrite = tooverwrite;
1007a9799022Sck 	tx->tx_space_tounref = tounref;
10088a2f1b91Sahrens #endif
1009fa9e4066Sahrens 
1010fa9e4066Sahrens 	if (tx->tx_dir && asize != 0) {
1011cdb0ab79Smaybee 		int err = dsl_dir_tempreserve_space(tx->tx_dir, memory,
1012cdb0ab79Smaybee 		    asize, fsize, usize, &tx->tx_tempreserve_cookie, tx);
10138a2f1b91Sahrens 		if (err)
1014fa9e4066Sahrens 			return (err);
1015fa9e4066Sahrens 	}
1016fa9e4066Sahrens 
1017fa9e4066Sahrens 	return (0);
1018fa9e4066Sahrens }
1019fa9e4066Sahrens 
10208a2f1b91Sahrens static void
10218a2f1b91Sahrens dmu_tx_unassign(dmu_tx_t *tx)
1022fa9e4066Sahrens {
10238a2f1b91Sahrens 	dmu_tx_hold_t *txh;
1024fa9e4066Sahrens 
10258a2f1b91Sahrens 	if (tx->tx_txg == 0)
10268a2f1b91Sahrens 		return;
1027fa9e4066Sahrens 
1028fa9e4066Sahrens 	txg_rele_to_quiesce(&tx->tx_txgh);
1029fa9e4066Sahrens 
10303e30c24aSWill Andrews 	/*
10313e30c24aSWill Andrews 	 * Walk the transaction's hold list, removing the hold on the
10323e30c24aSWill Andrews 	 * associated dnode, and notifying waiters if the refcount drops to 0.
10333e30c24aSWill Andrews 	 */
10348a2f1b91Sahrens 	for (txh = list_head(&tx->tx_holds); txh != tx->tx_needassign_txh;
10358a2f1b91Sahrens 	    txh = list_next(&tx->tx_holds, txh)) {
10368a2f1b91Sahrens 		dnode_t *dn = txh->txh_dnode;
1037fa9e4066Sahrens 
1038fa9e4066Sahrens 		if (dn == NULL)
1039fa9e4066Sahrens 			continue;
1040fa9e4066Sahrens 		mutex_enter(&dn->dn_mtx);
10418a2f1b91Sahrens 		ASSERT3U(dn->dn_assigned_txg, ==, tx->tx_txg);
1042fa9e4066Sahrens 
1043fa9e4066Sahrens 		if (refcount_remove(&dn->dn_tx_holds, tx) == 0) {
1044fa9e4066Sahrens 			dn->dn_assigned_txg = 0;
1045fa9e4066Sahrens 			cv_broadcast(&dn->dn_notxholds);
1046fa9e4066Sahrens 		}
1047fa9e4066Sahrens 		mutex_exit(&dn->dn_mtx);
1048fa9e4066Sahrens 	}
1049fa9e4066Sahrens 
1050fa9e4066Sahrens 	txg_rele_to_sync(&tx->tx_txgh);
1051fa9e4066Sahrens 
10528a2f1b91Sahrens 	tx->tx_lasttried_txg = tx->tx_txg;
1053fa9e4066Sahrens 	tx->tx_txg = 0;
1054fa9e4066Sahrens }
1055fa9e4066Sahrens 
1056fa9e4066Sahrens /*
1057fa9e4066Sahrens  * Assign tx to a transaction group.  txg_how can be one of:
1058fa9e4066Sahrens  *
1059fa9e4066Sahrens  * (1)	TXG_WAIT.  If the current open txg is full, waits until there's
1060fa9e4066Sahrens  *	a new one.  This should be used when you're not holding locks.
10613b2aab18SMatthew Ahrens  *	It will only fail if we're truly out of space (or over quota).
1062fa9e4066Sahrens  *
1063fa9e4066Sahrens  * (2)	TXG_NOWAIT.  If we can't assign into the current open txg without
1064fa9e4066Sahrens  *	blocking, returns immediately with ERESTART.  This should be used
1065fa9e4066Sahrens  *	whenever you're holding locks.  On an ERESTART error, the caller
10668a2f1b91Sahrens  *	should drop locks, do a dmu_tx_wait(tx), and try again.
1067fa9e4066Sahrens  */
1068fa9e4066Sahrens int
10693b2aab18SMatthew Ahrens dmu_tx_assign(dmu_tx_t *tx, txg_how_t txg_how)
1070fa9e4066Sahrens {
1071fa9e4066Sahrens 	int err;
1072fa9e4066Sahrens 
1073fa9e4066Sahrens 	ASSERT(tx->tx_txg == 0);
10743b2aab18SMatthew Ahrens 	ASSERT(txg_how == TXG_WAIT || txg_how == TXG_NOWAIT);
1075fa9e4066Sahrens 	ASSERT(!dsl_pool_sync_context(tx->tx_pool));
1076fa9e4066Sahrens 
10773b2aab18SMatthew Ahrens 	/* If we might wait, we must not hold the config lock. */
10783b2aab18SMatthew Ahrens 	ASSERT(txg_how != TXG_WAIT || !dsl_pool_config_held(tx->tx_pool));
10793b2aab18SMatthew Ahrens 
10808a2f1b91Sahrens 	while ((err = dmu_tx_try_assign(tx, txg_how)) != 0) {
10818a2f1b91Sahrens 		dmu_tx_unassign(tx);
1082fa9e4066Sahrens 
1083fa9e4066Sahrens 		if (err != ERESTART || txg_how != TXG_WAIT)
1084fa9e4066Sahrens 			return (err);
1085fa9e4066Sahrens 
10868a2f1b91Sahrens 		dmu_tx_wait(tx);
1087fa9e4066Sahrens 	}
1088fa9e4066Sahrens 
1089fa9e4066Sahrens 	txg_rele_to_quiesce(&tx->tx_txgh);
1090fa9e4066Sahrens 
1091fa9e4066Sahrens 	return (0);
1092fa9e4066Sahrens }
1093fa9e4066Sahrens 
10948a2f1b91Sahrens void
10958a2f1b91Sahrens dmu_tx_wait(dmu_tx_t *tx)
10968a2f1b91Sahrens {
10970a4e9518Sgw 	spa_t *spa = tx->tx_pool->dp_spa;
10980a4e9518Sgw 
10998a2f1b91Sahrens 	ASSERT(tx->tx_txg == 0);
11003b2aab18SMatthew Ahrens 	ASSERT(!dsl_pool_config_held(tx->tx_pool));
11018a2f1b91Sahrens 
11020a4e9518Sgw 	/*
11030a4e9518Sgw 	 * It's possible that the pool has become active after this thread
11040a4e9518Sgw 	 * has tried to obtain a tx. If that's the case then his
11050a4e9518Sgw 	 * tx_lasttried_txg would not have been assigned.
11060a4e9518Sgw 	 */
1107e14bb325SJeff Bonwick 	if (spa_suspended(spa) || tx->tx_lasttried_txg == 0) {
11080a4e9518Sgw 		txg_wait_synced(tx->tx_pool, spa_last_synced_txg(spa) + 1);
11090a4e9518Sgw 	} else if (tx->tx_needassign_txh) {
11108a2f1b91Sahrens 		dnode_t *dn = tx->tx_needassign_txh->txh_dnode;
11118a2f1b91Sahrens 
11128a2f1b91Sahrens 		mutex_enter(&dn->dn_mtx);
11138a2f1b91Sahrens 		while (dn->dn_assigned_txg == tx->tx_lasttried_txg - 1)
11148a2f1b91Sahrens 			cv_wait(&dn->dn_notxholds, &dn->dn_mtx);
11158a2f1b91Sahrens 		mutex_exit(&dn->dn_mtx);
11168a2f1b91Sahrens 		tx->tx_needassign_txh = NULL;
11178a2f1b91Sahrens 	} else {
11188a2f1b91Sahrens 		txg_wait_open(tx->tx_pool, tx->tx_lasttried_txg + 1);
11198a2f1b91Sahrens 	}
11208a2f1b91Sahrens }
11218a2f1b91Sahrens 
1122fa9e4066Sahrens void
1123fa9e4066Sahrens dmu_tx_willuse_space(dmu_tx_t *tx, int64_t delta)
1124fa9e4066Sahrens {
11258a2f1b91Sahrens #ifdef ZFS_DEBUG
1126fa9e4066Sahrens 	if (tx->tx_dir == NULL || delta == 0)
1127fa9e4066Sahrens 		return;
1128fa9e4066Sahrens 
1129fa9e4066Sahrens 	if (delta > 0) {
1130fa9e4066Sahrens 		ASSERT3U(refcount_count(&tx->tx_space_written) + delta, <=,
1131fa9e4066Sahrens 		    tx->tx_space_towrite);
1132fa9e4066Sahrens 		(void) refcount_add_many(&tx->tx_space_written, delta, NULL);
1133fa9e4066Sahrens 	} else {
1134fa9e4066Sahrens 		(void) refcount_add_many(&tx->tx_space_freed, -delta, NULL);
1135fa9e4066Sahrens 	}
11368a2f1b91Sahrens #endif
1137fa9e4066Sahrens }
1138fa9e4066Sahrens 
1139fa9e4066Sahrens void
1140fa9e4066Sahrens dmu_tx_commit(dmu_tx_t *tx)
1141fa9e4066Sahrens {
11428a2f1b91Sahrens 	dmu_tx_hold_t *txh;
1143fa9e4066Sahrens 
1144fa9e4066Sahrens 	ASSERT(tx->tx_txg != 0);
1145fa9e4066Sahrens 
11463e30c24aSWill Andrews 	/*
11473e30c24aSWill Andrews 	 * Go through the transaction's hold list and remove holds on
11483e30c24aSWill Andrews 	 * associated dnodes, notifying waiters if no holds remain.
11493e30c24aSWill Andrews 	 */
11508a2f1b91Sahrens 	while (txh = list_head(&tx->tx_holds)) {
11518a2f1b91Sahrens 		dnode_t *dn = txh->txh_dnode;
1152fa9e4066Sahrens 
11538a2f1b91Sahrens 		list_remove(&tx->tx_holds, txh);
11548a2f1b91Sahrens 		kmem_free(txh, sizeof (dmu_tx_hold_t));
1155fa9e4066Sahrens 		if (dn == NULL)
1156fa9e4066Sahrens 			continue;
1157fa9e4066Sahrens 		mutex_enter(&dn->dn_mtx);
1158fa9e4066Sahrens 		ASSERT3U(dn->dn_assigned_txg, ==, tx->tx_txg);
1159fa9e4066Sahrens 
1160fa9e4066Sahrens 		if (refcount_remove(&dn->dn_tx_holds, tx) == 0) {
1161fa9e4066Sahrens 			dn->dn_assigned_txg = 0;
1162fa9e4066Sahrens 			cv_broadcast(&dn->dn_notxholds);
1163fa9e4066Sahrens 		}
1164fa9e4066Sahrens 		mutex_exit(&dn->dn_mtx);
1165fa9e4066Sahrens 		dnode_rele(dn, tx);
1166fa9e4066Sahrens 	}
1167fa9e4066Sahrens 
11688a2f1b91Sahrens 	if (tx->tx_tempreserve_cookie)
1169fa9e4066Sahrens 		dsl_dir_tempreserve_clear(tx->tx_tempreserve_cookie, tx);
1170fa9e4066Sahrens 
1171d20e665cSRicardo M. Correia 	if (!list_is_empty(&tx->tx_callbacks))
1172d20e665cSRicardo M. Correia 		txg_register_callbacks(&tx->tx_txgh, &tx->tx_callbacks);
1173d20e665cSRicardo M. Correia 
1174fa9e4066Sahrens 	if (tx->tx_anyobj == FALSE)
1175fa9e4066Sahrens 		txg_rele_to_sync(&tx->tx_txgh);
1176d20e665cSRicardo M. Correia 
1177d20e665cSRicardo M. Correia 	list_destroy(&tx->tx_callbacks);
11788f38d419Sek 	list_destroy(&tx->tx_holds);
11798a2f1b91Sahrens #ifdef ZFS_DEBUG
1180fa9e4066Sahrens 	dprintf("towrite=%llu written=%llu tofree=%llu freed=%llu\n",
1181fa9e4066Sahrens 	    tx->tx_space_towrite, refcount_count(&tx->tx_space_written),
1182fa9e4066Sahrens 	    tx->tx_space_tofree, refcount_count(&tx->tx_space_freed));
1183fa9e4066Sahrens 	refcount_destroy_many(&tx->tx_space_written,
1184fa9e4066Sahrens 	    refcount_count(&tx->tx_space_written));
1185fa9e4066Sahrens 	refcount_destroy_many(&tx->tx_space_freed,
1186fa9e4066Sahrens 	    refcount_count(&tx->tx_space_freed));
1187fa9e4066Sahrens #endif
1188fa9e4066Sahrens 	kmem_free(tx, sizeof (dmu_tx_t));
1189fa9e4066Sahrens }
1190fa9e4066Sahrens 
1191fa9e4066Sahrens void
1192fa9e4066Sahrens dmu_tx_abort(dmu_tx_t *tx)
1193fa9e4066Sahrens {
11948a2f1b91Sahrens 	dmu_tx_hold_t *txh;
1195fa9e4066Sahrens 
1196fa9e4066Sahrens 	ASSERT(tx->tx_txg == 0);
1197fa9e4066Sahrens 
11988a2f1b91Sahrens 	while (txh = list_head(&tx->tx_holds)) {
11998a2f1b91Sahrens 		dnode_t *dn = txh->txh_dnode;
1200fa9e4066Sahrens 
12018a2f1b91Sahrens 		list_remove(&tx->tx_holds, txh);
12028a2f1b91Sahrens 		kmem_free(txh, sizeof (dmu_tx_hold_t));
1203fa9e4066Sahrens 		if (dn != NULL)
1204fa9e4066Sahrens 			dnode_rele(dn, tx);
1205fa9e4066Sahrens 	}
1206d20e665cSRicardo M. Correia 
1207d20e665cSRicardo M. Correia 	/*
1208d20e665cSRicardo M. Correia 	 * Call any registered callbacks with an error code.
1209d20e665cSRicardo M. Correia 	 */
1210d20e665cSRicardo M. Correia 	if (!list_is_empty(&tx->tx_callbacks))
1211d20e665cSRicardo M. Correia 		dmu_tx_do_callbacks(&tx->tx_callbacks, ECANCELED);
1212d20e665cSRicardo M. Correia 
1213d20e665cSRicardo M. Correia 	list_destroy(&tx->tx_callbacks);
12148f38d419Sek 	list_destroy(&tx->tx_holds);
12158a2f1b91Sahrens #ifdef ZFS_DEBUG
1216fa9e4066Sahrens 	refcount_destroy_many(&tx->tx_space_written,
1217fa9e4066Sahrens 	    refcount_count(&tx->tx_space_written));
1218fa9e4066Sahrens 	refcount_destroy_many(&tx->tx_space_freed,
1219fa9e4066Sahrens 	    refcount_count(&tx->tx_space_freed));
1220fa9e4066Sahrens #endif
1221fa9e4066Sahrens 	kmem_free(tx, sizeof (dmu_tx_t));
1222fa9e4066Sahrens }
1223fa9e4066Sahrens 
1224fa9e4066Sahrens uint64_t
1225fa9e4066Sahrens dmu_tx_get_txg(dmu_tx_t *tx)
1226fa9e4066Sahrens {
1227fa9e4066Sahrens 	ASSERT(tx->tx_txg != 0);
1228fa9e4066Sahrens 	return (tx->tx_txg);
1229fa9e4066Sahrens }
1230d20e665cSRicardo M. Correia 
12313b2aab18SMatthew Ahrens dsl_pool_t *
12323b2aab18SMatthew Ahrens dmu_tx_pool(dmu_tx_t *tx)
12333b2aab18SMatthew Ahrens {
12343b2aab18SMatthew Ahrens 	ASSERT(tx->tx_pool != NULL);
12353b2aab18SMatthew Ahrens 	return (tx->tx_pool);
12363b2aab18SMatthew Ahrens }
12373b2aab18SMatthew Ahrens 
12383b2aab18SMatthew Ahrens 
1239d20e665cSRicardo M. Correia void
1240d20e665cSRicardo M. Correia dmu_tx_callback_register(dmu_tx_t *tx, dmu_tx_callback_func_t *func, void *data)
1241d20e665cSRicardo M. Correia {
1242d20e665cSRicardo M. Correia 	dmu_tx_callback_t *dcb;
1243d20e665cSRicardo M. Correia 
1244d20e665cSRicardo M. Correia 	dcb = kmem_alloc(sizeof (dmu_tx_callback_t), KM_SLEEP);
1245d20e665cSRicardo M. Correia 
1246d20e665cSRicardo M. Correia 	dcb->dcb_func = func;
1247d20e665cSRicardo M. Correia 	dcb->dcb_data = data;
1248d20e665cSRicardo M. Correia 
1249d20e665cSRicardo M. Correia 	list_insert_tail(&tx->tx_callbacks, dcb);
1250d20e665cSRicardo M. Correia }
1251d20e665cSRicardo M. Correia 
1252d20e665cSRicardo M. Correia /*
1253d20e665cSRicardo M. Correia  * Call all the commit callbacks on a list, with a given error code.
1254d20e665cSRicardo M. Correia  */
1255d20e665cSRicardo M. Correia void
1256d20e665cSRicardo M. Correia dmu_tx_do_callbacks(list_t *cb_list, int error)
1257d20e665cSRicardo M. Correia {
1258d20e665cSRicardo M. Correia 	dmu_tx_callback_t *dcb;
1259d20e665cSRicardo M. Correia 
1260d20e665cSRicardo M. Correia 	while (dcb = list_head(cb_list)) {
1261d20e665cSRicardo M. Correia 		list_remove(cb_list, dcb);
1262d20e665cSRicardo M. Correia 		dcb->dcb_func(dcb->dcb_data, error);
1263d20e665cSRicardo M. Correia 		kmem_free(dcb, sizeof (dmu_tx_callback_t));
1264d20e665cSRicardo M. Correia 	}
1265d20e665cSRicardo M. Correia }
12660a586ceaSMark Shellenbaum 
12670a586ceaSMark Shellenbaum /*
12680a586ceaSMark Shellenbaum  * Interface to hold a bunch of attributes.
12690a586ceaSMark Shellenbaum  * used for creating new files.
12700a586ceaSMark Shellenbaum  * attrsize is the total size of all attributes
12710a586ceaSMark Shellenbaum  * to be added during object creation
12720a586ceaSMark Shellenbaum  *
12730a586ceaSMark Shellenbaum  * For updating/adding a single attribute dmu_tx_hold_sa() should be used.
12740a586ceaSMark Shellenbaum  */
12750a586ceaSMark Shellenbaum 
12760a586ceaSMark Shellenbaum /*
12770a586ceaSMark Shellenbaum  * hold necessary attribute name for attribute registration.
12780a586ceaSMark Shellenbaum  * should be a very rare case where this is needed.  If it does
12790a586ceaSMark Shellenbaum  * happen it would only happen on the first write to the file system.
12800a586ceaSMark Shellenbaum  */
12810a586ceaSMark Shellenbaum static void
12820a586ceaSMark Shellenbaum dmu_tx_sa_registration_hold(sa_os_t *sa, dmu_tx_t *tx)
12830a586ceaSMark Shellenbaum {
12840a586ceaSMark Shellenbaum 	int i;
12850a586ceaSMark Shellenbaum 
12860a586ceaSMark Shellenbaum 	if (!sa->sa_need_attr_registration)
12870a586ceaSMark Shellenbaum 		return;
12880a586ceaSMark Shellenbaum 
12890a586ceaSMark Shellenbaum 	for (i = 0; i != sa->sa_num_attrs; i++) {
12900a586ceaSMark Shellenbaum 		if (!sa->sa_attr_table[i].sa_registered) {
12910a586ceaSMark Shellenbaum 			if (sa->sa_reg_attr_obj)
12920a586ceaSMark Shellenbaum 				dmu_tx_hold_zap(tx, sa->sa_reg_attr_obj,
12930a586ceaSMark Shellenbaum 				    B_TRUE, sa->sa_attr_table[i].sa_name);
12940a586ceaSMark Shellenbaum 			else
12950a586ceaSMark Shellenbaum 				dmu_tx_hold_zap(tx, DMU_NEW_OBJECT,
12960a586ceaSMark Shellenbaum 				    B_TRUE, sa->sa_attr_table[i].sa_name);
12970a586ceaSMark Shellenbaum 		}
12980a586ceaSMark Shellenbaum 	}
12990a586ceaSMark Shellenbaum }
13000a586ceaSMark Shellenbaum 
13010a586ceaSMark Shellenbaum 
13020a586ceaSMark Shellenbaum void
13030a586ceaSMark Shellenbaum dmu_tx_hold_spill(dmu_tx_t *tx, uint64_t object)
13040a586ceaSMark Shellenbaum {
13050a586ceaSMark Shellenbaum 	dnode_t *dn;
13060a586ceaSMark Shellenbaum 	dmu_tx_hold_t *txh;
13070a586ceaSMark Shellenbaum 
13080a586ceaSMark Shellenbaum 	txh = dmu_tx_hold_object_impl(tx, tx->tx_objset, object,
13090a586ceaSMark Shellenbaum 	    THT_SPILL, 0, 0);
13100a586ceaSMark Shellenbaum 
13110a586ceaSMark Shellenbaum 	dn = txh->txh_dnode;
13120a586ceaSMark Shellenbaum 
13130a586ceaSMark Shellenbaum 	if (dn == NULL)
13140a586ceaSMark Shellenbaum 		return;
13150a586ceaSMark Shellenbaum 
13160a586ceaSMark Shellenbaum 	/* If blkptr doesn't exist then add space to towrite */
13179dccfd2aSAlbert Lee 	if (!(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR)) {
13180a586ceaSMark Shellenbaum 		txh->txh_space_towrite += SPA_MAXBLOCKSIZE;
13190a586ceaSMark Shellenbaum 	} else {
13209dccfd2aSAlbert Lee 		blkptr_t *bp;
13219dccfd2aSAlbert Lee 
13229dccfd2aSAlbert Lee 		bp = &dn->dn_phys->dn_spill;
13230a586ceaSMark Shellenbaum 		if (dsl_dataset_block_freeable(dn->dn_objset->os_dsl_dataset,
1324c7cd2421SGeorge Wilson 		    bp, bp->blk_birth))
13250a586ceaSMark Shellenbaum 			txh->txh_space_tooverwrite += SPA_MAXBLOCKSIZE;
13260a586ceaSMark Shellenbaum 		else
13270a586ceaSMark Shellenbaum 			txh->txh_space_towrite += SPA_MAXBLOCKSIZE;
13289dccfd2aSAlbert Lee 		if (!BP_IS_HOLE(bp))
13290a586ceaSMark Shellenbaum 			txh->txh_space_tounref += SPA_MAXBLOCKSIZE;
13300a586ceaSMark Shellenbaum 	}
13310a586ceaSMark Shellenbaum }
13320a586ceaSMark Shellenbaum 
13330a586ceaSMark Shellenbaum void
13340a586ceaSMark Shellenbaum dmu_tx_hold_sa_create(dmu_tx_t *tx, int attrsize)
13350a586ceaSMark Shellenbaum {
13360a586ceaSMark Shellenbaum 	sa_os_t *sa = tx->tx_objset->os_sa;
13370a586ceaSMark Shellenbaum 
13380a586ceaSMark Shellenbaum 	dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
13390a586ceaSMark Shellenbaum 
13400a586ceaSMark Shellenbaum 	if (tx->tx_objset->os_sa->sa_master_obj == 0)
13410a586ceaSMark Shellenbaum 		return;
13420a586ceaSMark Shellenbaum 
13430a586ceaSMark Shellenbaum 	if (tx->tx_objset->os_sa->sa_layout_attr_obj)
13440a586ceaSMark Shellenbaum 		dmu_tx_hold_zap(tx, sa->sa_layout_attr_obj, B_TRUE, NULL);
13450a586ceaSMark Shellenbaum 	else {
13460a586ceaSMark Shellenbaum 		dmu_tx_hold_zap(tx, sa->sa_master_obj, B_TRUE, SA_LAYOUTS);
13470a586ceaSMark Shellenbaum 		dmu_tx_hold_zap(tx, sa->sa_master_obj, B_TRUE, SA_REGISTRY);
13480a586ceaSMark Shellenbaum 		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
13490a586ceaSMark Shellenbaum 		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
13500a586ceaSMark Shellenbaum 	}
13510a586ceaSMark Shellenbaum 
13520a586ceaSMark Shellenbaum 	dmu_tx_sa_registration_hold(sa, tx);
13530a586ceaSMark Shellenbaum 
13540a586ceaSMark Shellenbaum 	if (attrsize <= DN_MAX_BONUSLEN && !sa->sa_force_spill)
13550a586ceaSMark Shellenbaum 		return;
13560a586ceaSMark Shellenbaum 
13570a586ceaSMark Shellenbaum 	(void) dmu_tx_hold_object_impl(tx, tx->tx_objset, DMU_NEW_OBJECT,
13580a586ceaSMark Shellenbaum 	    THT_SPILL, 0, 0);
13590a586ceaSMark Shellenbaum }
13600a586ceaSMark Shellenbaum 
13610a586ceaSMark Shellenbaum /*
13620a586ceaSMark Shellenbaum  * Hold SA attribute
13630a586ceaSMark Shellenbaum  *
13640a586ceaSMark Shellenbaum  * dmu_tx_hold_sa(dmu_tx_t *tx, sa_handle_t *, attribute, add, size)
13650a586ceaSMark Shellenbaum  *
13660a586ceaSMark Shellenbaum  * variable_size is the total size of all variable sized attributes
13670a586ceaSMark Shellenbaum  * passed to this function.  It is not the total size of all
13680a586ceaSMark Shellenbaum  * variable size attributes that *may* exist on this object.
13690a586ceaSMark Shellenbaum  */
13700a586ceaSMark Shellenbaum void
13710a586ceaSMark Shellenbaum dmu_tx_hold_sa(dmu_tx_t *tx, sa_handle_t *hdl, boolean_t may_grow)
13720a586ceaSMark Shellenbaum {
13730a586ceaSMark Shellenbaum 	uint64_t object;
13740a586ceaSMark Shellenbaum 	sa_os_t *sa = tx->tx_objset->os_sa;
13750a586ceaSMark Shellenbaum 
13760a586ceaSMark Shellenbaum 	ASSERT(hdl != NULL);
13770a586ceaSMark Shellenbaum 
13780a586ceaSMark Shellenbaum 	object = sa_handle_object(hdl);
13790a586ceaSMark Shellenbaum 
13800a586ceaSMark Shellenbaum 	dmu_tx_hold_bonus(tx, object);
13810a586ceaSMark Shellenbaum 
13820a586ceaSMark Shellenbaum 	if (tx->tx_objset->os_sa->sa_master_obj == 0)
13830a586ceaSMark Shellenbaum 		return;
13840a586ceaSMark Shellenbaum 
13850a586ceaSMark Shellenbaum 	if (tx->tx_objset->os_sa->sa_reg_attr_obj == 0 ||
13860a586ceaSMark Shellenbaum 	    tx->tx_objset->os_sa->sa_layout_attr_obj == 0) {
13870a586ceaSMark Shellenbaum 		dmu_tx_hold_zap(tx, sa->sa_master_obj, B_TRUE, SA_LAYOUTS);
13880a586ceaSMark Shellenbaum 		dmu_tx_hold_zap(tx, sa->sa_master_obj, B_TRUE, SA_REGISTRY);
13890a586ceaSMark Shellenbaum 		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
13900a586ceaSMark Shellenbaum 		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
13910a586ceaSMark Shellenbaum 	}
13920a586ceaSMark Shellenbaum 
13930a586ceaSMark Shellenbaum 	dmu_tx_sa_registration_hold(sa, tx);
13940a586ceaSMark Shellenbaum 
13950a586ceaSMark Shellenbaum 	if (may_grow && tx->tx_objset->os_sa->sa_layout_attr_obj)
13960a586ceaSMark Shellenbaum 		dmu_tx_hold_zap(tx, sa->sa_layout_attr_obj, B_TRUE, NULL);
13970a586ceaSMark Shellenbaum 
1398744947dcSTom Erickson 	if (sa->sa_force_spill || may_grow || hdl->sa_spill) {
13990a586ceaSMark Shellenbaum 		ASSERT(tx->tx_txg == 0);
14000a586ceaSMark Shellenbaum 		dmu_tx_hold_spill(tx, object);
1401744947dcSTom Erickson 	} else {
1402744947dcSTom Erickson 		dmu_buf_impl_t *db = (dmu_buf_impl_t *)hdl->sa_bonus;
1403744947dcSTom Erickson 		dnode_t *dn;
1404744947dcSTom Erickson 
1405744947dcSTom Erickson 		DB_DNODE_ENTER(db);
1406744947dcSTom Erickson 		dn = DB_DNODE(db);
1407744947dcSTom Erickson 		if (dn->dn_have_spill) {
1408744947dcSTom Erickson 			ASSERT(tx->tx_txg == 0);
1409744947dcSTom Erickson 			dmu_tx_hold_spill(tx, object);
1410744947dcSTom Erickson 		}
1411744947dcSTom Erickson 		DB_DNODE_EXIT(db);
14120a586ceaSMark Shellenbaum 	}
14130a586ceaSMark Shellenbaum }
1414