1168404Spjd/*
2168404Spjd * CDDL HEADER START
3168404Spjd *
4168404Spjd * The contents of this file are subject to the terms of the
5168404Spjd * Common Development and Distribution License (the "License").
6168404Spjd * You may not use this file except in compliance with the License.
7168404Spjd *
8168404Spjd * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9168404Spjd * or http://www.opensolaris.org/os/licensing.
10168404Spjd * See the License for the specific language governing permissions
11168404Spjd * and limitations under the License.
12168404Spjd *
13168404Spjd * When distributing Covered Code, include this CDDL HEADER in each
14168404Spjd * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15168404Spjd * If applicable, add the following below this CDDL HEADER, with the
16168404Spjd * fields enclosed by brackets "[]" replaced with your own identifying
17168404Spjd * information: Portions Copyright [yyyy] [name of copyright owner]
18168404Spjd *
19168404Spjd * CDDL HEADER END
20168404Spjd */
21168404Spjd/*
22219089Spjd * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23226944Smm * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
24247406Smm * Copyright (c) 2013 by Delphix. All rights reserved.
25226944Smm */
26168404Spjd
27168404Spjd#include <sys/dmu.h>
28168404Spjd#include <sys/dmu_impl.h>
29168404Spjd#include <sys/dbuf.h>
30168404Spjd#include <sys/dmu_tx.h>
31168404Spjd#include <sys/dmu_objset.h>
32168404Spjd#include <sys/dsl_dataset.h> /* for dsl_dataset_block_freeable() */
33168404Spjd#include <sys/dsl_dir.h> /* for dsl_dir_tempreserve_*() */
34168404Spjd#include <sys/dsl_pool.h>
35168404Spjd#include <sys/zap_impl.h> /* for fzap_default_block_shift */
36168404Spjd#include <sys/spa.h>
37219089Spjd#include <sys/sa.h>
38219089Spjd#include <sys/sa_impl.h>
39168404Spjd#include <sys/zfs_context.h>
40219089Spjd#include <sys/varargs.h>
41168404Spjd
42168404Spjdtypedef void (*dmu_tx_hold_func_t)(dmu_tx_t *tx, struct dnode *dn,
43168404Spjd    uint64_t arg1, uint64_t arg2);
44168404Spjd
45168404Spjd
46168404Spjddmu_tx_t *
47168404Spjddmu_tx_create_dd(dsl_dir_t *dd)
48168404Spjd{
49168404Spjd	dmu_tx_t *tx = kmem_zalloc(sizeof (dmu_tx_t), KM_SLEEP);
50168404Spjd	tx->tx_dir = dd;
51249643Smm	if (dd != NULL)
52168404Spjd		tx->tx_pool = dd->dd_pool;
53168404Spjd	list_create(&tx->tx_holds, sizeof (dmu_tx_hold_t),
54168404Spjd	    offsetof(dmu_tx_hold_t, txh_node));
55219089Spjd	list_create(&tx->tx_callbacks, sizeof (dmu_tx_callback_t),
56219089Spjd	    offsetof(dmu_tx_callback_t, dcb_node));
57260764Savg	tx->tx_start = gethrtime();
58168404Spjd#ifdef ZFS_DEBUG
59168404Spjd	refcount_create(&tx->tx_space_written);
60168404Spjd	refcount_create(&tx->tx_space_freed);
61168404Spjd#endif
62168404Spjd	return (tx);
63168404Spjd}
64168404Spjd
65168404Spjddmu_tx_t *
66168404Spjddmu_tx_create(objset_t *os)
67168404Spjd{
68219089Spjd	dmu_tx_t *tx = dmu_tx_create_dd(os->os_dsl_dataset->ds_dir);
69168404Spjd	tx->tx_objset = os;
70219089Spjd	tx->tx_lastsnap_txg = dsl_dataset_prev_snap_txg(os->os_dsl_dataset);
71168404Spjd	return (tx);
72168404Spjd}
73168404Spjd
74168404Spjddmu_tx_t *
75168404Spjddmu_tx_create_assigned(struct dsl_pool *dp, uint64_t txg)
76168404Spjd{
77168404Spjd	dmu_tx_t *tx = dmu_tx_create_dd(NULL);
78168404Spjd
79168404Spjd	ASSERT3U(txg, <=, dp->dp_tx.tx_open_txg);
80168404Spjd	tx->tx_pool = dp;
81168404Spjd	tx->tx_txg = txg;
82168404Spjd	tx->tx_anyobj = TRUE;
83168404Spjd
84168404Spjd	return (tx);
85168404Spjd}
86168404Spjd
87168404Spjdint
88168404Spjddmu_tx_is_syncing(dmu_tx_t *tx)
89168404Spjd{
90168404Spjd	return (tx->tx_anyobj);
91168404Spjd}
92168404Spjd
93168404Spjdint
94168404Spjddmu_tx_private_ok(dmu_tx_t *tx)
95168404Spjd{
96168404Spjd	return (tx->tx_anyobj);
97168404Spjd}
98168404Spjd
99168404Spjdstatic dmu_tx_hold_t *
100168404Spjddmu_tx_hold_object_impl(dmu_tx_t *tx, objset_t *os, uint64_t object,
101168404Spjd    enum dmu_tx_hold_type type, uint64_t arg1, uint64_t arg2)
102168404Spjd{
103168404Spjd	dmu_tx_hold_t *txh;
104168404Spjd	dnode_t *dn = NULL;
105168404Spjd	int err;
106168404Spjd
107168404Spjd	if (object != DMU_NEW_OBJECT) {
108219089Spjd		err = dnode_hold(os, object, tx, &dn);
109168404Spjd		if (err) {
110168404Spjd			tx->tx_err = err;
111168404Spjd			return (NULL);
112168404Spjd		}
113168404Spjd
114168404Spjd		if (err == 0 && tx->tx_txg != 0) {
115168404Spjd			mutex_enter(&dn->dn_mtx);
116168404Spjd			/*
117168404Spjd			 * dn->dn_assigned_txg == tx->tx_txg doesn't pose a
118168404Spjd			 * problem, but there's no way for it to happen (for
119168404Spjd			 * now, at least).
120168404Spjd			 */
121168404Spjd			ASSERT(dn->dn_assigned_txg == 0);
122168404Spjd			dn->dn_assigned_txg = tx->tx_txg;
123168404Spjd			(void) refcount_add(&dn->dn_tx_holds, tx);
124168404Spjd			mutex_exit(&dn->dn_mtx);
125168404Spjd		}
126168404Spjd	}
127168404Spjd
128168404Spjd	txh = kmem_zalloc(sizeof (dmu_tx_hold_t), KM_SLEEP);
129168404Spjd	txh->txh_tx = tx;
130168404Spjd	txh->txh_dnode = dn;
131168404Spjd#ifdef ZFS_DEBUG
132168404Spjd	txh->txh_type = type;
133168404Spjd	txh->txh_arg1 = arg1;
134168404Spjd	txh->txh_arg2 = arg2;
135168404Spjd#endif
136168404Spjd	list_insert_tail(&tx->tx_holds, txh);
137168404Spjd
138168404Spjd	return (txh);
139168404Spjd}
140168404Spjd
141168404Spjdvoid
142168404Spjddmu_tx_add_new_object(dmu_tx_t *tx, objset_t *os, uint64_t object)
143168404Spjd{
144168404Spjd	/*
145168404Spjd	 * If we're syncing, they can manipulate any object anyhow, and
146168404Spjd	 * the hold on the dnode_t can cause problems.
147168404Spjd	 */
148168404Spjd	if (!dmu_tx_is_syncing(tx)) {
149168404Spjd		(void) dmu_tx_hold_object_impl(tx, os,
150168404Spjd		    object, THT_NEWOBJECT, 0, 0);
151168404Spjd	}
152168404Spjd}
153168404Spjd
154168404Spjdstatic int
155168404Spjddmu_tx_check_ioerr(zio_t *zio, dnode_t *dn, int level, uint64_t blkid)
156168404Spjd{
157168404Spjd	int err;
158168404Spjd	dmu_buf_impl_t *db;
159168404Spjd
160168404Spjd	rw_enter(&dn->dn_struct_rwlock, RW_READER);
161168404Spjd	db = dbuf_hold_level(dn, level, blkid, FTAG);
162168404Spjd	rw_exit(&dn->dn_struct_rwlock);
163168404Spjd	if (db == NULL)
164249643Smm		return (SET_ERROR(EIO));
165185029Spjd	err = dbuf_read(db, zio, DB_RF_CANFAIL | DB_RF_NOPREFETCH);
166168404Spjd	dbuf_rele(db, FTAG);
167168404Spjd	return (err);
168168404Spjd}
169168404Spjd
170209962Smmstatic void
171219089Spjddmu_tx_count_twig(dmu_tx_hold_t *txh, dnode_t *dn, dmu_buf_impl_t *db,
172219089Spjd    int level, uint64_t blkid, boolean_t freeable, uint64_t *history)
173209962Smm{
174219089Spjd	objset_t *os = dn->dn_objset;
175219089Spjd	dsl_dataset_t *ds = os->os_dsl_dataset;
176219089Spjd	int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
177219089Spjd	dmu_buf_impl_t *parent = NULL;
178219089Spjd	blkptr_t *bp = NULL;
179219089Spjd	uint64_t space;
180209962Smm
181219089Spjd	if (level >= dn->dn_nlevels || history[level] == blkid)
182209962Smm		return;
183209962Smm
184219089Spjd	history[level] = blkid;
185209962Smm
186219089Spjd	space = (level == 0) ? dn->dn_datablksz : (1ULL << dn->dn_indblkshift);
187219089Spjd
188219089Spjd	if (db == NULL || db == dn->dn_dbuf) {
189219089Spjd		ASSERT(level != 0);
190219089Spjd		db = NULL;
191219089Spjd	} else {
192219089Spjd		ASSERT(DB_DNODE(db) == dn);
193219089Spjd		ASSERT(db->db_level == level);
194219089Spjd		ASSERT(db->db.db_size == space);
195219089Spjd		ASSERT(db->db_blkid == blkid);
196219089Spjd		bp = db->db_blkptr;
197219089Spjd		parent = db->db_parent;
198209962Smm	}
199209962Smm
200219089Spjd	freeable = (bp && (freeable ||
201219089Spjd	    dsl_dataset_block_freeable(ds, bp, bp->blk_birth)));
202209962Smm
203219089Spjd	if (freeable)
204219089Spjd		txh->txh_space_tooverwrite += space;
205219089Spjd	else
206219089Spjd		txh->txh_space_towrite += space;
207219089Spjd	if (bp)
208219089Spjd		txh->txh_space_tounref += bp_get_dsize(os->os_spa, bp);
209219089Spjd
210219089Spjd	dmu_tx_count_twig(txh, dn, parent, level + 1,
211219089Spjd	    blkid >> epbs, freeable, history);
212209962Smm}
213209962Smm
214168404Spjd/* ARGSUSED */
215168404Spjdstatic void
216168404Spjddmu_tx_count_write(dmu_tx_hold_t *txh, uint64_t off, uint64_t len)
217168404Spjd{
218168404Spjd	dnode_t *dn = txh->txh_dnode;
219168404Spjd	uint64_t start, end, i;
220168404Spjd	int min_bs, max_bs, min_ibs, max_ibs, epbs, bits;
221168404Spjd	int err = 0;
222168404Spjd
223168404Spjd	if (len == 0)
224168404Spjd		return;
225168404Spjd
226168404Spjd	min_bs = SPA_MINBLOCKSHIFT;
227168404Spjd	max_bs = SPA_MAXBLOCKSHIFT;
228168404Spjd	min_ibs = DN_MIN_INDBLKSHIFT;
229168404Spjd	max_ibs = DN_MAX_INDBLKSHIFT;
230168404Spjd
231209962Smm	if (dn) {
232219089Spjd		uint64_t history[DN_MAX_LEVELS];
233209962Smm		int nlvls = dn->dn_nlevels;
234209962Smm		int delta;
235168404Spjd
236209962Smm		/*
237209962Smm		 * For i/o error checking, read the first and last level-0
238209962Smm		 * blocks (if they are not aligned), and all the level-1 blocks.
239209962Smm		 */
240168404Spjd		if (dn->dn_maxblkid == 0) {
241209962Smm			delta = dn->dn_datablksz;
242209962Smm			start = (off < dn->dn_datablksz) ? 0 : 1;
243209962Smm			end = (off+len <= dn->dn_datablksz) ? 0 : 1;
244209962Smm			if (start == 0 && (off > 0 || len < dn->dn_datablksz)) {
245209962Smm				err = dmu_tx_check_ioerr(NULL, dn, 0, 0);
246209962Smm				if (err)
247209962Smm					goto out;
248209962Smm				delta -= off;
249209962Smm			}
250168404Spjd		} else {
251168404Spjd			zio_t *zio = zio_root(dn->dn_objset->os_spa,
252168404Spjd			    NULL, NULL, ZIO_FLAG_CANFAIL);
253168404Spjd
254168404Spjd			/* first level-0 block */
255168404Spjd			start = off >> dn->dn_datablkshift;
256168404Spjd			if (P2PHASE(off, dn->dn_datablksz) ||
257168404Spjd			    len < dn->dn_datablksz) {
258168404Spjd				err = dmu_tx_check_ioerr(zio, dn, 0, start);
259168404Spjd				if (err)
260168404Spjd					goto out;
261168404Spjd			}
262168404Spjd
263168404Spjd			/* last level-0 block */
264168404Spjd			end = (off+len-1) >> dn->dn_datablkshift;
265219089Spjd			if (end != start && end <= dn->dn_maxblkid &&
266168404Spjd			    P2PHASE(off+len, dn->dn_datablksz)) {
267168404Spjd				err = dmu_tx_check_ioerr(zio, dn, 0, end);
268168404Spjd				if (err)
269168404Spjd					goto out;
270168404Spjd			}
271168404Spjd
272168404Spjd			/* level-1 blocks */
273209962Smm			if (nlvls > 1) {
274209962Smm				int shft = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
275209962Smm				for (i = (start>>shft)+1; i < end>>shft; i++) {
276168404Spjd					err = dmu_tx_check_ioerr(zio, dn, 1, i);
277168404Spjd					if (err)
278168404Spjd						goto out;
279168404Spjd				}
280168404Spjd			}
281168404Spjd
282168404Spjd			err = zio_wait(zio);
283168404Spjd			if (err)
284168404Spjd				goto out;
285209962Smm			delta = P2NPHASE(off, dn->dn_datablksz);
286168404Spjd		}
287168404Spjd
288247406Smm		min_ibs = max_ibs = dn->dn_indblkshift;
289209962Smm		if (dn->dn_maxblkid > 0) {
290209962Smm			/*
291209962Smm			 * The blocksize can't change,
292209962Smm			 * so we can make a more precise estimate.
293209962Smm			 */
294209962Smm			ASSERT(dn->dn_datablkshift != 0);
295168404Spjd			min_bs = max_bs = dn->dn_datablkshift;
296209962Smm		}
297209962Smm
298209962Smm		/*
299209962Smm		 * If this write is not off the end of the file
300209962Smm		 * we need to account for overwrites/unref.
301209962Smm		 */
302219089Spjd		if (start <= dn->dn_maxblkid) {
303219089Spjd			for (int l = 0; l < DN_MAX_LEVELS; l++)
304219089Spjd				history[l] = -1ULL;
305219089Spjd		}
306209962Smm		while (start <= dn->dn_maxblkid) {
307209962Smm			dmu_buf_impl_t *db;
308209962Smm
309209962Smm			rw_enter(&dn->dn_struct_rwlock, RW_READER);
310219089Spjd			err = dbuf_hold_impl(dn, 0, start, FALSE, FTAG, &db);
311209962Smm			rw_exit(&dn->dn_struct_rwlock);
312219089Spjd
313219089Spjd			if (err) {
314219089Spjd				txh->txh_tx->tx_err = err;
315219089Spjd				return;
316209962Smm			}
317219089Spjd
318219089Spjd			dmu_tx_count_twig(txh, dn, db, 0, start, B_FALSE,
319219089Spjd			    history);
320209962Smm			dbuf_rele(db, FTAG);
321209962Smm			if (++start > end) {
322209962Smm				/*
323209962Smm				 * Account for new indirects appearing
324209962Smm				 * before this IO gets assigned into a txg.
325209962Smm				 */
326209962Smm				bits = 64 - min_bs;
327209962Smm				epbs = min_ibs - SPA_BLKPTRSHIFT;
328209962Smm				for (bits -= epbs * (nlvls - 1);
329209962Smm				    bits >= 0; bits -= epbs)
330209962Smm					txh->txh_fudge += 1ULL << max_ibs;
331209962Smm				goto out;
332209962Smm			}
333209962Smm			off += delta;
334209962Smm			if (len >= delta)
335209962Smm				len -= delta;
336209962Smm			delta = dn->dn_datablksz;
337209962Smm		}
338168404Spjd	}
339168404Spjd
340168404Spjd	/*
341168404Spjd	 * 'end' is the last thing we will access, not one past.
342168404Spjd	 * This way we won't overflow when accessing the last byte.
343168404Spjd	 */
344168404Spjd	start = P2ALIGN(off, 1ULL << max_bs);
345168404Spjd	end = P2ROUNDUP(off + len, 1ULL << max_bs) - 1;
346168404Spjd	txh->txh_space_towrite += end - start + 1;
347168404Spjd
348168404Spjd	start >>= min_bs;
349168404Spjd	end >>= min_bs;
350168404Spjd
351168404Spjd	epbs = min_ibs - SPA_BLKPTRSHIFT;
352168404Spjd
353168404Spjd	/*
354168404Spjd	 * The object contains at most 2^(64 - min_bs) blocks,
355168404Spjd	 * and each indirect level maps 2^epbs.
356168404Spjd	 */
357168404Spjd	for (bits = 64 - min_bs; bits >= 0; bits -= epbs) {
358168404Spjd		start >>= epbs;
359168404Spjd		end >>= epbs;
360209962Smm		ASSERT3U(end, >=, start);
361209962Smm		txh->txh_space_towrite += (end - start + 1) << max_ibs;
362209962Smm		if (start != 0) {
363209962Smm			/*
364209962Smm			 * We also need a new blkid=0 indirect block
365209962Smm			 * to reference any existing file data.
366209962Smm			 */
367168404Spjd			txh->txh_space_towrite += 1ULL << max_ibs;
368209962Smm		}
369168404Spjd	}
370168404Spjd
371209962Smmout:
372209962Smm	if (txh->txh_space_towrite + txh->txh_space_tooverwrite >
373209962Smm	    2 * DMU_MAX_ACCESS)
374249643Smm		err = SET_ERROR(EFBIG);
375168404Spjd
376168404Spjd	if (err)
377168404Spjd		txh->txh_tx->tx_err = err;
378168404Spjd}
379168404Spjd
380168404Spjdstatic void
381168404Spjddmu_tx_count_dnode(dmu_tx_hold_t *txh)
382168404Spjd{
383168404Spjd	dnode_t *dn = txh->txh_dnode;
384219089Spjd	dnode_t *mdn = DMU_META_DNODE(txh->txh_tx->tx_objset);
385168404Spjd	uint64_t space = mdn->dn_datablksz +
386168404Spjd	    ((mdn->dn_nlevels-1) << mdn->dn_indblkshift);
387168404Spjd
388168404Spjd	if (dn && dn->dn_dbuf->db_blkptr &&
389168404Spjd	    dsl_dataset_block_freeable(dn->dn_objset->os_dsl_dataset,
390219089Spjd	    dn->dn_dbuf->db_blkptr, dn->dn_dbuf->db_blkptr->blk_birth)) {
391168404Spjd		txh->txh_space_tooverwrite += space;
392209962Smm		txh->txh_space_tounref += space;
393168404Spjd	} else {
394168404Spjd		txh->txh_space_towrite += space;
395185029Spjd		if (dn && dn->dn_dbuf->db_blkptr)
396185029Spjd			txh->txh_space_tounref += space;
397168404Spjd	}
398168404Spjd}
399168404Spjd
400168404Spjdvoid
401168404Spjddmu_tx_hold_write(dmu_tx_t *tx, uint64_t object, uint64_t off, int len)
402168404Spjd{
403168404Spjd	dmu_tx_hold_t *txh;
404168404Spjd
405168404Spjd	ASSERT(tx->tx_txg == 0);
406168404Spjd	ASSERT(len < DMU_MAX_ACCESS);
407168404Spjd	ASSERT(len == 0 || UINT64_MAX - off >= len - 1);
408168404Spjd
409168404Spjd	txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
410168404Spjd	    object, THT_WRITE, off, len);
411168404Spjd	if (txh == NULL)
412168404Spjd		return;
413168404Spjd
414168404Spjd	dmu_tx_count_write(txh, off, len);
415168404Spjd	dmu_tx_count_dnode(txh);
416168404Spjd}
417168404Spjd
418168404Spjdstatic void
419168404Spjddmu_tx_count_free(dmu_tx_hold_t *txh, uint64_t off, uint64_t len)
420168404Spjd{
421185029Spjd	uint64_t blkid, nblks, lastblk;
422185029Spjd	uint64_t space = 0, unref = 0, skipped = 0;
423168404Spjd	dnode_t *dn = txh->txh_dnode;
424168404Spjd	dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset;
425168404Spjd	spa_t *spa = txh->txh_tx->tx_pool->dp_spa;
426185029Spjd	int epbs;
427243674Smm	uint64_t l0span = 0, nl1blks = 0;
428168404Spjd
429185029Spjd	if (dn->dn_nlevels == 0)
430168404Spjd		return;
431168404Spjd
432168404Spjd	/*
433185029Spjd	 * The struct_rwlock protects us against dn_nlevels
434168404Spjd	 * changing, in case (against all odds) we manage to dirty &
435168404Spjd	 * sync out the changes after we check for being dirty.
436219089Spjd	 * Also, dbuf_hold_impl() wants us to have the struct_rwlock.
437168404Spjd	 */
438168404Spjd	rw_enter(&dn->dn_struct_rwlock, RW_READER);
439185029Spjd	epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
440185029Spjd	if (dn->dn_maxblkid == 0) {
441168404Spjd		if (off == 0 && len >= dn->dn_datablksz) {
442168404Spjd			blkid = 0;
443168404Spjd			nblks = 1;
444168404Spjd		} else {
445168404Spjd			rw_exit(&dn->dn_struct_rwlock);
446168404Spjd			return;
447168404Spjd		}
448168404Spjd	} else {
449168404Spjd		blkid = off >> dn->dn_datablkshift;
450185029Spjd		nblks = (len + dn->dn_datablksz - 1) >> dn->dn_datablkshift;
451168404Spjd
452262084Savg		if (blkid > dn->dn_maxblkid) {
453168404Spjd			rw_exit(&dn->dn_struct_rwlock);
454168404Spjd			return;
455168404Spjd		}
456185029Spjd		if (blkid + nblks > dn->dn_maxblkid)
457262084Savg			nblks = dn->dn_maxblkid - blkid + 1;
458168404Spjd
459168404Spjd	}
460243674Smm	l0span = nblks;    /* save for later use to calc level > 1 overhead */
461185029Spjd	if (dn->dn_nlevels == 1) {
462168404Spjd		int i;
463168404Spjd		for (i = 0; i < nblks; i++) {
464168404Spjd			blkptr_t *bp = dn->dn_phys->dn_blkptr;
465185029Spjd			ASSERT3U(blkid + i, <, dn->dn_nblkptr);
466168404Spjd			bp += blkid + i;
467219089Spjd			if (dsl_dataset_block_freeable(ds, bp, bp->blk_birth)) {
468168404Spjd				dprintf_bp(bp, "can free old%s", "");
469219089Spjd				space += bp_get_dsize(spa, bp);
470168404Spjd			}
471185029Spjd			unref += BP_GET_ASIZE(bp);
472168404Spjd		}
473243674Smm		nl1blks = 1;
474168404Spjd		nblks = 0;
475168404Spjd	}
476168404Spjd
477185029Spjd	lastblk = blkid + nblks - 1;
478168404Spjd	while (nblks) {
479168404Spjd		dmu_buf_impl_t *dbuf;
480185029Spjd		uint64_t ibyte, new_blkid;
481185029Spjd		int epb = 1 << epbs;
482185029Spjd		int err, i, blkoff, tochk;
483185029Spjd		blkptr_t *bp;
484168404Spjd
485185029Spjd		ibyte = blkid << dn->dn_datablkshift;
486185029Spjd		err = dnode_next_offset(dn,
487185029Spjd		    DNODE_FIND_HAVELOCK, &ibyte, 2, 1, 0);
488185029Spjd		new_blkid = ibyte >> dn->dn_datablkshift;
489185029Spjd		if (err == ESRCH) {
490185029Spjd			skipped += (lastblk >> epbs) - (blkid >> epbs) + 1;
491185029Spjd			break;
492185029Spjd		}
493185029Spjd		if (err) {
494185029Spjd			txh->txh_tx->tx_err = err;
495185029Spjd			break;
496185029Spjd		}
497185029Spjd		if (new_blkid > lastblk) {
498185029Spjd			skipped += (lastblk >> epbs) - (blkid >> epbs) + 1;
499185029Spjd			break;
500185029Spjd		}
501168404Spjd
502185029Spjd		if (new_blkid > blkid) {
503185029Spjd			ASSERT((new_blkid >> epbs) > (blkid >> epbs));
504185029Spjd			skipped += (new_blkid >> epbs) - (blkid >> epbs) - 1;
505185029Spjd			nblks -= new_blkid - blkid;
506185029Spjd			blkid = new_blkid;
507185029Spjd		}
508185029Spjd		blkoff = P2PHASE(blkid, epb);
509185029Spjd		tochk = MIN(epb - blkoff, nblks);
510168404Spjd
511219089Spjd		err = dbuf_hold_impl(dn, 1, blkid >> epbs, FALSE, FTAG, &dbuf);
512219089Spjd		if (err) {
513219089Spjd			txh->txh_tx->tx_err = err;
514219089Spjd			break;
515219089Spjd		}
516168404Spjd
517185029Spjd		txh->txh_memory_tohold += dbuf->db.db_size;
518219089Spjd
519219089Spjd		/*
520219089Spjd		 * We don't check memory_tohold against DMU_MAX_ACCESS because
521219089Spjd		 * memory_tohold is an over-estimation (especially the >L1
522219089Spjd		 * indirect blocks), so it could fail.  Callers should have
523219089Spjd		 * already verified that they will not be holding too much
524219089Spjd		 * memory.
525219089Spjd		 */
526219089Spjd
527185029Spjd		err = dbuf_read(dbuf, NULL, DB_RF_HAVESTRUCT | DB_RF_CANFAIL);
528185029Spjd		if (err != 0) {
529168404Spjd			txh->txh_tx->tx_err = err;
530185029Spjd			dbuf_rele(dbuf, FTAG);
531168404Spjd			break;
532168404Spjd		}
533168404Spjd
534185029Spjd		bp = dbuf->db.db_data;
535185029Spjd		bp += blkoff;
536185029Spjd
537185029Spjd		for (i = 0; i < tochk; i++) {
538219089Spjd			if (dsl_dataset_block_freeable(ds, &bp[i],
539219089Spjd			    bp[i].blk_birth)) {
540185029Spjd				dprintf_bp(&bp[i], "can free old%s", "");
541219089Spjd				space += bp_get_dsize(spa, &bp[i]);
542185029Spjd			}
543185029Spjd			unref += BP_GET_ASIZE(bp);
544185029Spjd		}
545185029Spjd		dbuf_rele(dbuf, FTAG);
546185029Spjd
547243674Smm		++nl1blks;
548168404Spjd		blkid += tochk;
549168404Spjd		nblks -= tochk;
550168404Spjd	}
551168404Spjd	rw_exit(&dn->dn_struct_rwlock);
552168404Spjd
553243674Smm	/*
554243674Smm	 * Add in memory requirements of higher-level indirects.
555243674Smm	 * This assumes a worst-possible scenario for dn_nlevels and a
556243674Smm	 * worst-possible distribution of l1-blocks over the region to free.
557243674Smm	 */
558243674Smm	{
559243674Smm		uint64_t blkcnt = 1 + ((l0span >> epbs) >> epbs);
560243674Smm		int level = 2;
561243674Smm		/*
562243674Smm		 * Here we don't use DN_MAX_LEVEL, but calculate it with the
563243674Smm		 * given datablkshift and indblkshift. This makes the
564243674Smm		 * difference between 19 and 8 on large files.
565243674Smm		 */
566243674Smm		int maxlevel = 2 + (DN_MAX_OFFSET_SHIFT - dn->dn_datablkshift) /
567243674Smm		    (dn->dn_indblkshift - SPA_BLKPTRSHIFT);
568243674Smm
569243674Smm		while (level++ < maxlevel) {
570243674Smm			txh->txh_memory_tohold += MAX(MIN(blkcnt, nl1blks), 1)
571243674Smm			    << dn->dn_indblkshift;
572243674Smm			blkcnt = 1 + (blkcnt >> epbs);
573243674Smm		}
574243674Smm	}
575243674Smm
576185029Spjd	/* account for new level 1 indirect blocks that might show up */
577185029Spjd	if (skipped > 0) {
578185029Spjd		txh->txh_fudge += skipped << dn->dn_indblkshift;
579185029Spjd		skipped = MIN(skipped, DMU_MAX_DELETEBLKCNT >> epbs);
580185029Spjd		txh->txh_memory_tohold += skipped << dn->dn_indblkshift;
581185029Spjd	}
582168404Spjd	txh->txh_space_tofree += space;
583185029Spjd	txh->txh_space_tounref += unref;
584168404Spjd}
585168404Spjd
586168404Spjdvoid
587168404Spjddmu_tx_hold_free(dmu_tx_t *tx, uint64_t object, uint64_t off, uint64_t len)
588168404Spjd{
589168404Spjd	dmu_tx_hold_t *txh;
590168404Spjd	dnode_t *dn;
591260722Savg	int err;
592168404Spjd	zio_t *zio;
593168404Spjd
594168404Spjd	ASSERT(tx->tx_txg == 0);
595168404Spjd
596168404Spjd	txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
597168404Spjd	    object, THT_FREE, off, len);
598168404Spjd	if (txh == NULL)
599168404Spjd		return;
600168404Spjd	dn = txh->txh_dnode;
601260764Savg	dmu_tx_count_dnode(txh);
602168404Spjd
603168404Spjd	if (off >= (dn->dn_maxblkid+1) * dn->dn_datablksz)
604168404Spjd		return;
605168404Spjd	if (len == DMU_OBJECT_END)
606168404Spjd		len = (dn->dn_maxblkid+1) * dn->dn_datablksz - off;
607168404Spjd
608260722Savg
609168404Spjd	/*
610260722Savg	 * For i/o error checking, we read the first and last level-0
611260722Savg	 * blocks if they are not aligned, and all the level-1 blocks.
612260722Savg	 *
613260722Savg	 * Note:  dbuf_free_range() assumes that we have not instantiated
614260722Savg	 * any level-0 dbufs that will be completely freed.  Therefore we must
615260722Savg	 * exercise care to not read or count the first and last blocks
616260722Savg	 * if they are blocksize-aligned.
617168404Spjd	 */
618260722Savg	if (dn->dn_datablkshift == 0) {
619260722Savg		if (off != 0 || len < dn->dn_datablksz)
620260722Savg			dmu_tx_count_write(txh, 0, dn->dn_datablksz);
621260722Savg	} else {
622260722Savg		/* first block will be modified if it is not aligned */
623260722Savg		if (!IS_P2ALIGNED(off, 1 << dn->dn_datablkshift))
624260722Savg			dmu_tx_count_write(txh, off, 1);
625260722Savg		/* last block will be modified if it is not aligned */
626260722Savg		if (!IS_P2ALIGNED(off + len, 1 << dn->dn_datablkshift))
627260722Savg			dmu_tx_count_write(txh, off+len, 1);
628260722Savg	}
629260722Savg
630260722Savg	/*
631260722Savg	 * Check level-1 blocks.
632260722Savg	 */
633168404Spjd	if (dn->dn_nlevels > 1) {
634260722Savg		int shift = dn->dn_datablkshift + dn->dn_indblkshift -
635168404Spjd		    SPA_BLKPTRSHIFT;
636260722Savg		uint64_t start = off >> shift;
637260722Savg		uint64_t end = (off + len) >> shift;
638168404Spjd
639260722Savg		ASSERT(dn->dn_indblkshift != 0);
640260722Savg
641262180Savg		/*
642262180Savg		 * dnode_reallocate() can result in an object with indirect
643262180Savg		 * blocks having an odd data block size.  In this case,
644262180Savg		 * just check the single block.
645262180Savg		 */
646262180Savg		if (dn->dn_datablkshift == 0)
647262180Savg			start = end = 0;
648262180Savg
649168404Spjd		zio = zio_root(tx->tx_pool->dp_spa,
650168404Spjd		    NULL, NULL, ZIO_FLAG_CANFAIL);
651260722Savg		for (uint64_t i = start; i <= end; i++) {
652168404Spjd			uint64_t ibyte = i << shift;
653185029Spjd			err = dnode_next_offset(dn, 0, &ibyte, 2, 1, 0);
654168404Spjd			i = ibyte >> shift;
655168404Spjd			if (err == ESRCH)
656168404Spjd				break;
657168404Spjd			if (err) {
658168404Spjd				tx->tx_err = err;
659168404Spjd				return;
660168404Spjd			}
661168404Spjd
662168404Spjd			err = dmu_tx_check_ioerr(zio, dn, 1, i);
663168404Spjd			if (err) {
664168404Spjd				tx->tx_err = err;
665168404Spjd				return;
666168404Spjd			}
667168404Spjd		}
668168404Spjd		err = zio_wait(zio);
669168404Spjd		if (err) {
670168404Spjd			tx->tx_err = err;
671168404Spjd			return;
672168404Spjd		}
673168404Spjd	}
674168404Spjd
675168404Spjd	dmu_tx_count_free(txh, off, len);
676168404Spjd}
677168404Spjd
678168404Spjdvoid
679209962Smmdmu_tx_hold_zap(dmu_tx_t *tx, uint64_t object, int add, const char *name)
680168404Spjd{
681168404Spjd	dmu_tx_hold_t *txh;
682168404Spjd	dnode_t *dn;
683168404Spjd	uint64_t nblocks;
684168404Spjd	int epbs, err;
685168404Spjd
686168404Spjd	ASSERT(tx->tx_txg == 0);
687168404Spjd
688168404Spjd	txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
689168404Spjd	    object, THT_ZAP, add, (uintptr_t)name);
690168404Spjd	if (txh == NULL)
691168404Spjd		return;
692168404Spjd	dn = txh->txh_dnode;
693168404Spjd
694168404Spjd	dmu_tx_count_dnode(txh);
695168404Spjd
696168404Spjd	if (dn == NULL) {
697168404Spjd		/*
698168404Spjd		 * We will be able to fit a new object's entries into one leaf
699168404Spjd		 * block.  So there will be at most 2 blocks total,
700168404Spjd		 * including the header block.
701168404Spjd		 */
702168404Spjd		dmu_tx_count_write(txh, 0, 2 << fzap_default_block_shift);
703168404Spjd		return;
704168404Spjd	}
705168404Spjd
706243674Smm	ASSERT3P(DMU_OT_BYTESWAP(dn->dn_type), ==, DMU_BSWAP_ZAP);
707168404Spjd
708168404Spjd	if (dn->dn_maxblkid == 0 && !add) {
709226944Smm		blkptr_t *bp;
710226944Smm
711168404Spjd		/*
712168404Spjd		 * If there is only one block  (i.e. this is a micro-zap)
713168404Spjd		 * and we are not adding anything, the accounting is simple.
714168404Spjd		 */
715168404Spjd		err = dmu_tx_check_ioerr(NULL, dn, 0, 0);
716168404Spjd		if (err) {
717168404Spjd			tx->tx_err = err;
718168404Spjd			return;
719168404Spjd		}
720168404Spjd
721168404Spjd		/*
722168404Spjd		 * Use max block size here, since we don't know how much
723168404Spjd		 * the size will change between now and the dbuf dirty call.
724168404Spjd		 */
725226944Smm		bp = &dn->dn_phys->dn_blkptr[0];
726168404Spjd		if (dsl_dataset_block_freeable(dn->dn_objset->os_dsl_dataset,
727226944Smm		    bp, bp->blk_birth))
728168404Spjd			txh->txh_space_tooverwrite += SPA_MAXBLOCKSIZE;
729226944Smm		else
730168404Spjd			txh->txh_space_towrite += SPA_MAXBLOCKSIZE;
731226944Smm		if (!BP_IS_HOLE(bp))
732209093Smm			txh->txh_space_tounref += SPA_MAXBLOCKSIZE;
733168404Spjd		return;
734168404Spjd	}
735168404Spjd
736168404Spjd	if (dn->dn_maxblkid > 0 && name) {
737168404Spjd		/*
738168404Spjd		 * access the name in this fat-zap so that we'll check
739168404Spjd		 * for i/o errors to the leaf blocks, etc.
740168404Spjd		 */
741219089Spjd		err = zap_lookup(dn->dn_objset, dn->dn_object, name,
742168404Spjd		    8, 0, NULL);
743168404Spjd		if (err == EIO) {
744168404Spjd			tx->tx_err = err;
745168404Spjd			return;
746168404Spjd		}
747168404Spjd	}
748168404Spjd
749219089Spjd	err = zap_count_write(dn->dn_objset, dn->dn_object, name, add,
750209962Smm	    &txh->txh_space_towrite, &txh->txh_space_tooverwrite);
751168404Spjd
752168404Spjd	/*
753168404Spjd	 * If the modified blocks are scattered to the four winds,
754168404Spjd	 * we'll have to modify an indirect twig for each.
755168404Spjd	 */
756168404Spjd	epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
757168404Spjd	for (nblocks = dn->dn_maxblkid >> epbs; nblocks != 0; nblocks >>= epbs)
758209962Smm		if (dn->dn_objset->os_dsl_dataset->ds_phys->ds_prev_snap_obj)
759209962Smm			txh->txh_space_towrite += 3 << dn->dn_indblkshift;
760209962Smm		else
761209962Smm			txh->txh_space_tooverwrite += 3 << dn->dn_indblkshift;
762168404Spjd}
763168404Spjd
764168404Spjdvoid
765168404Spjddmu_tx_hold_bonus(dmu_tx_t *tx, uint64_t object)
766168404Spjd{
767168404Spjd	dmu_tx_hold_t *txh;
768168404Spjd
769168404Spjd	ASSERT(tx->tx_txg == 0);
770168404Spjd
771168404Spjd	txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
772168404Spjd	    object, THT_BONUS, 0, 0);
773168404Spjd	if (txh)
774168404Spjd		dmu_tx_count_dnode(txh);
775168404Spjd}
776168404Spjd
777168404Spjdvoid
778168404Spjddmu_tx_hold_space(dmu_tx_t *tx, uint64_t space)
779168404Spjd{
780168404Spjd	dmu_tx_hold_t *txh;
781168404Spjd	ASSERT(tx->tx_txg == 0);
782168404Spjd
783168404Spjd	txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
784168404Spjd	    DMU_NEW_OBJECT, THT_SPACE, space, 0);
785168404Spjd
786168404Spjd	txh->txh_space_towrite += space;
787168404Spjd}
788168404Spjd
789168404Spjdint
790168404Spjddmu_tx_holds(dmu_tx_t *tx, uint64_t object)
791168404Spjd{
792168404Spjd	dmu_tx_hold_t *txh;
793168404Spjd	int holds = 0;
794168404Spjd
795168404Spjd	/*
796168404Spjd	 * By asserting that the tx is assigned, we're counting the
797168404Spjd	 * number of dn_tx_holds, which is the same as the number of
798168404Spjd	 * dn_holds.  Otherwise, we'd be counting dn_holds, but
799168404Spjd	 * dn_tx_holds could be 0.
800168404Spjd	 */
801168404Spjd	ASSERT(tx->tx_txg != 0);
802168404Spjd
803168404Spjd	/* if (tx->tx_anyobj == TRUE) */
804168404Spjd		/* return (0); */
805168404Spjd
806168404Spjd	for (txh = list_head(&tx->tx_holds); txh;
807168404Spjd	    txh = list_next(&tx->tx_holds, txh)) {
808168404Spjd		if (txh->txh_dnode && txh->txh_dnode->dn_object == object)
809168404Spjd			holds++;
810168404Spjd	}
811168404Spjd
812168404Spjd	return (holds);
813168404Spjd}
814168404Spjd
815168404Spjd#ifdef ZFS_DEBUG
816168404Spjdvoid
817168404Spjddmu_tx_dirty_buf(dmu_tx_t *tx, dmu_buf_impl_t *db)
818168404Spjd{
819168404Spjd	dmu_tx_hold_t *txh;
820168404Spjd	int match_object = FALSE, match_offset = FALSE;
821219089Spjd	dnode_t *dn;
822168404Spjd
823219089Spjd	DB_DNODE_ENTER(db);
824219089Spjd	dn = DB_DNODE(db);
825168404Spjd	ASSERT(tx->tx_txg != 0);
826219089Spjd	ASSERT(tx->tx_objset == NULL || dn->dn_objset == tx->tx_objset);
827168404Spjd	ASSERT3U(dn->dn_object, ==, db->db.db_object);
828168404Spjd
829219089Spjd	if (tx->tx_anyobj) {
830219089Spjd		DB_DNODE_EXIT(db);
831168404Spjd		return;
832219089Spjd	}
833168404Spjd
834168404Spjd	/* XXX No checking on the meta dnode for now */
835219089Spjd	if (db->db.db_object == DMU_META_DNODE_OBJECT) {
836219089Spjd		DB_DNODE_EXIT(db);
837168404Spjd		return;
838219089Spjd	}
839168404Spjd
840168404Spjd	for (txh = list_head(&tx->tx_holds); txh;
841168404Spjd	    txh = list_next(&tx->tx_holds, txh)) {
842168404Spjd		ASSERT(dn == NULL || dn->dn_assigned_txg == tx->tx_txg);
843168404Spjd		if (txh->txh_dnode == dn && txh->txh_type != THT_NEWOBJECT)
844168404Spjd			match_object = TRUE;
845168404Spjd		if (txh->txh_dnode == NULL || txh->txh_dnode == dn) {
846168404Spjd			int datablkshift = dn->dn_datablkshift ?
847168404Spjd			    dn->dn_datablkshift : SPA_MAXBLOCKSHIFT;
848168404Spjd			int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
849168404Spjd			int shift = datablkshift + epbs * db->db_level;
850168404Spjd			uint64_t beginblk = shift >= 64 ? 0 :
851168404Spjd			    (txh->txh_arg1 >> shift);
852168404Spjd			uint64_t endblk = shift >= 64 ? 0 :
853168404Spjd			    ((txh->txh_arg1 + txh->txh_arg2 - 1) >> shift);
854168404Spjd			uint64_t blkid = db->db_blkid;
855168404Spjd
856168404Spjd			/* XXX txh_arg2 better not be zero... */
857168404Spjd
858168404Spjd			dprintf("found txh type %x beginblk=%llx endblk=%llx\n",
859168404Spjd			    txh->txh_type, beginblk, endblk);
860168404Spjd
861168404Spjd			switch (txh->txh_type) {
862168404Spjd			case THT_WRITE:
863168404Spjd				if (blkid >= beginblk && blkid <= endblk)
864168404Spjd					match_offset = TRUE;
865168404Spjd				/*
866168404Spjd				 * We will let this hold work for the bonus
867219089Spjd				 * or spill buffer so that we don't need to
868219089Spjd				 * hold it when creating a new object.
869168404Spjd				 */
870219089Spjd				if (blkid == DMU_BONUS_BLKID ||
871219089Spjd				    blkid == DMU_SPILL_BLKID)
872168404Spjd					match_offset = TRUE;
873168404Spjd				/*
874168404Spjd				 * They might have to increase nlevels,
875168404Spjd				 * thus dirtying the new TLIBs.  Or the
876168404Spjd				 * might have to change the block size,
877168404Spjd				 * thus dirying the new lvl=0 blk=0.
878168404Spjd				 */
879168404Spjd				if (blkid == 0)
880168404Spjd					match_offset = TRUE;
881168404Spjd				break;
882168404Spjd			case THT_FREE:
883185029Spjd				/*
884185029Spjd				 * We will dirty all the level 1 blocks in
885185029Spjd				 * the free range and perhaps the first and
886185029Spjd				 * last level 0 block.
887185029Spjd				 */
888185029Spjd				if (blkid >= beginblk && (blkid <= endblk ||
889185029Spjd				    txh->txh_arg2 == DMU_OBJECT_END))
890168404Spjd					match_offset = TRUE;
891168404Spjd				break;
892219089Spjd			case THT_SPILL:
893219089Spjd				if (blkid == DMU_SPILL_BLKID)
894219089Spjd					match_offset = TRUE;
895219089Spjd				break;
896168404Spjd			case THT_BONUS:
897219089Spjd				if (blkid == DMU_BONUS_BLKID)
898168404Spjd					match_offset = TRUE;
899168404Spjd				break;
900168404Spjd			case THT_ZAP:
901168404Spjd				match_offset = TRUE;
902168404Spjd				break;
903168404Spjd			case THT_NEWOBJECT:
904168404Spjd				match_object = TRUE;
905168404Spjd				break;
906168404Spjd			default:
907168404Spjd				ASSERT(!"bad txh_type");
908168404Spjd			}
909168404Spjd		}
910219089Spjd		if (match_object && match_offset) {
911219089Spjd			DB_DNODE_EXIT(db);
912168404Spjd			return;
913219089Spjd		}
914168404Spjd	}
915219089Spjd	DB_DNODE_EXIT(db);
916168404Spjd	panic("dirtying dbuf obj=%llx lvl=%u blkid=%llx but not tx_held\n",
917168404Spjd	    (u_longlong_t)db->db.db_object, db->db_level,
918168404Spjd	    (u_longlong_t)db->db_blkid);
919168404Spjd}
920168404Spjd#endif
921168404Spjd
922260764Savg/*
923260764Savg * If we can't do 10 iops, something is wrong.  Let us go ahead
924260764Savg * and hit zfs_dirty_data_max.
925260764Savg */
926260764Savghrtime_t zfs_delay_max_ns = MSEC2NSEC(100);
927260764Savgint zfs_delay_resolution_ns = 100 * 1000; /* 100 microseconds */
928260764Savg
929260764Savg/*
930260764Savg * We delay transactions when we've determined that the backend storage
931260764Savg * isn't able to accommodate the rate of incoming writes.
932260764Savg *
933260764Savg * If there is already a transaction waiting, we delay relative to when
934260764Savg * that transaction finishes waiting.  This way the calculated min_time
935260764Savg * is independent of the number of threads concurrently executing
936260764Savg * transactions.
937260764Savg *
938260764Savg * If we are the only waiter, wait relative to when the transaction
939260764Savg * started, rather than the current time.  This credits the transaction for
940260764Savg * "time already served", e.g. reading indirect blocks.
941260764Savg *
942260764Savg * The minimum time for a transaction to take is calculated as:
943260764Savg *     min_time = scale * (dirty - min) / (max - dirty)
944260764Savg *     min_time is then capped at zfs_delay_max_ns.
945260764Savg *
946260764Savg * The delay has two degrees of freedom that can be adjusted via tunables.
947260764Savg * The percentage of dirty data at which we start to delay is defined by
948260764Savg * zfs_delay_min_dirty_percent. This should typically be at or above
949260764Savg * zfs_vdev_async_write_active_max_dirty_percent so that we only start to
950260764Savg * delay after writing at full speed has failed to keep up with the incoming
951260764Savg * write rate. The scale of the curve is defined by zfs_delay_scale. Roughly
952260764Savg * speaking, this variable determines the amount of delay at the midpoint of
953260764Savg * the curve.
954260764Savg *
955260764Savg * delay
956260764Savg *  10ms +-------------------------------------------------------------*+
957260764Savg *       |                                                             *|
958260764Savg *   9ms +                                                             *+
959260764Savg *       |                                                             *|
960260764Savg *   8ms +                                                             *+
961260764Savg *       |                                                            * |
962260764Savg *   7ms +                                                            * +
963260764Savg *       |                                                            * |
964260764Savg *   6ms +                                                            * +
965260764Savg *       |                                                            * |
966260764Savg *   5ms +                                                           *  +
967260764Savg *       |                                                           *  |
968260764Savg *   4ms +                                                           *  +
969260764Savg *       |                                                           *  |
970260764Savg *   3ms +                                                          *   +
971260764Savg *       |                                                          *   |
972260764Savg *   2ms +                                              (midpoint) *    +
973260764Savg *       |                                                  |    **     |
974260764Savg *   1ms +                                                  v ***       +
975260764Savg *       |             zfs_delay_scale ---------->     ********         |
976260764Savg *     0 +-------------------------------------*********----------------+
977260764Savg *       0%                    <- zfs_dirty_data_max ->               100%
978260764Savg *
979260764Savg * Note that since the delay is added to the outstanding time remaining on the
980260764Savg * most recent transaction, the delay is effectively the inverse of IOPS.
981260764Savg * Here the midpoint of 500us translates to 2000 IOPS. The shape of the curve
982260764Savg * was chosen such that small changes in the amount of accumulated dirty data
983260764Savg * in the first 3/4 of the curve yield relatively small differences in the
984260764Savg * amount of delay.
985260764Savg *
986260764Savg * The effects can be easier to understand when the amount of delay is
987260764Savg * represented on a log scale:
988260764Savg *
989260764Savg * delay
990260764Savg * 100ms +-------------------------------------------------------------++
991260764Savg *       +                                                              +
992260764Savg *       |                                                              |
993260764Savg *       +                                                             *+
994260764Savg *  10ms +                                                             *+
995260764Savg *       +                                                           ** +
996260764Savg *       |                                              (midpoint)  **  |
997260764Savg *       +                                                  |     **    +
998260764Savg *   1ms +                                                  v ****      +
999260764Savg *       +             zfs_delay_scale ---------->        *****         +
1000260764Savg *       |                                             ****             |
1001260764Savg *       +                                          ****                +
1002260764Savg * 100us +                                        **                    +
1003260764Savg *       +                                       *                      +
1004260764Savg *       |                                      *                       |
1005260764Savg *       +                                     *                        +
1006260764Savg *  10us +                                     *                        +
1007260764Savg *       +                                                              +
1008260764Savg *       |                                                              |
1009260764Savg *       +                                                              +
1010260764Savg *       +--------------------------------------------------------------+
1011260764Savg *       0%                    <- zfs_dirty_data_max ->               100%
1012260764Savg *
1013260764Savg * Note here that only as the amount of dirty data approaches its limit does
1014260764Savg * the delay start to increase rapidly. The goal of a properly tuned system
1015260764Savg * should be to keep the amount of dirty data out of that range by first
1016260764Savg * ensuring that the appropriate limits are set for the I/O scheduler to reach
1017260764Savg * optimal throughput on the backend storage, and then by changing the value
1018260764Savg * of zfs_delay_scale to increase the steepness of the curve.
1019260764Savg */
1020260764Savgstatic void
1021260764Savgdmu_tx_delay(dmu_tx_t *tx, uint64_t dirty)
1022260764Savg{
1023260764Savg	dsl_pool_t *dp = tx->tx_pool;
1024260764Savg	uint64_t delay_min_bytes =
1025260764Savg	    zfs_dirty_data_max * zfs_delay_min_dirty_percent / 100;
1026260764Savg	hrtime_t wakeup, min_tx_time, now;
1027260764Savg
1028260764Savg	if (dirty <= delay_min_bytes)
1029260764Savg		return;
1030260764Savg
1031260764Savg	/*
1032260764Savg	 * The caller has already waited until we are under the max.
1033260764Savg	 * We make them pass us the amount of dirty data so we don't
1034260764Savg	 * have to handle the case of it being >= the max, which could
1035260764Savg	 * cause a divide-by-zero if it's == the max.
1036260764Savg	 */
1037260764Savg	ASSERT3U(dirty, <, zfs_dirty_data_max);
1038260764Savg
1039260764Savg	now = gethrtime();
1040260764Savg	min_tx_time = zfs_delay_scale *
1041260764Savg	    (dirty - delay_min_bytes) / (zfs_dirty_data_max - dirty);
1042260764Savg	if (now > tx->tx_start + min_tx_time)
1043260764Savg		return;
1044260764Savg
1045260764Savg	min_tx_time = MIN(min_tx_time, zfs_delay_max_ns);
1046260764Savg
1047260764Savg	DTRACE_PROBE3(delay__mintime, dmu_tx_t *, tx, uint64_t, dirty,
1048260764Savg	    uint64_t, min_tx_time);
1049260764Savg
1050260764Savg	mutex_enter(&dp->dp_lock);
1051260764Savg	wakeup = MAX(tx->tx_start + min_tx_time,
1052260764Savg	    dp->dp_last_wakeup + min_tx_time);
1053260764Savg	dp->dp_last_wakeup = wakeup;
1054260764Savg	mutex_exit(&dp->dp_lock);
1055260764Savg
1056260764Savg#ifdef _KERNEL
1057260764Savg#ifdef illumos
1058260764Savg	mutex_enter(&curthread->t_delay_lock);
1059260764Savg	while (cv_timedwait_hires(&curthread->t_delay_cv,
1060260764Savg	    &curthread->t_delay_lock, wakeup, zfs_delay_resolution_ns,
1061260764Savg	    CALLOUT_FLAG_ABSOLUTE | CALLOUT_FLAG_ROUNDUP) > 0)
1062260764Savg		continue;
1063260764Savg	mutex_exit(&curthread->t_delay_lock);
1064260764Savg#else
1065264505Sjhb	int timo;
1066264505Sjhb
1067260764Savg	/* XXX High resolution callouts are not available */
1068260764Savg	ASSERT(wakeup >= now);
1069264505Sjhb	timo = NSEC_TO_TICK(wakeup - now);
1070264505Sjhb	if (timo != 0)
1071264505Sjhb		pause("dmu_tx_delay", timo);
1072260764Savg#endif
1073260764Savg#else
1074260764Savg	hrtime_t delta = wakeup - gethrtime();
1075260764Savg	struct timespec ts;
1076260764Savg	ts.tv_sec = delta / NANOSEC;
1077260764Savg	ts.tv_nsec = delta % NANOSEC;
1078260764Savg	(void) nanosleep(&ts, NULL);
1079260764Savg#endif
1080260764Savg}
1081260764Savg
1082168404Spjdstatic int
1083249643Smmdmu_tx_try_assign(dmu_tx_t *tx, txg_how_t txg_how)
1084168404Spjd{
1085168404Spjd	dmu_tx_hold_t *txh;
1086185029Spjd	spa_t *spa = tx->tx_pool->dp_spa;
1087185029Spjd	uint64_t memory, asize, fsize, usize;
1088185029Spjd	uint64_t towrite, tofree, tooverwrite, tounref, tohold, fudge;
1089168404Spjd
1090243674Smm	ASSERT0(tx->tx_txg);
1091185029Spjd
1092168404Spjd	if (tx->tx_err)
1093168404Spjd		return (tx->tx_err);
1094168404Spjd
1095185029Spjd	if (spa_suspended(spa)) {
1096185029Spjd		/*
1097185029Spjd		 * If the user has indicated a blocking failure mode
1098185029Spjd		 * then return ERESTART which will block in dmu_tx_wait().
1099185029Spjd		 * Otherwise, return EIO so that an error can get
1100185029Spjd		 * propagated back to the VOP calls.
1101185029Spjd		 *
1102185029Spjd		 * Note that we always honor the txg_how flag regardless
1103185029Spjd		 * of the failuremode setting.
1104185029Spjd		 */
1105185029Spjd		if (spa_get_failmode(spa) == ZIO_FAILURE_MODE_CONTINUE &&
1106185029Spjd		    txg_how != TXG_WAIT)
1107249643Smm			return (SET_ERROR(EIO));
1108185029Spjd
1109249643Smm		return (SET_ERROR(ERESTART));
1110185029Spjd	}
1111185029Spjd
1112260764Savg	if (!tx->tx_waited &&
1113260764Savg	    dsl_pool_need_dirty_delay(tx->tx_pool)) {
1114260764Savg		tx->tx_wait_dirty = B_TRUE;
1115260764Savg		return (SET_ERROR(ERESTART));
1116260764Savg	}
1117260764Savg
1118168404Spjd	tx->tx_txg = txg_hold_open(tx->tx_pool, &tx->tx_txgh);
1119168404Spjd	tx->tx_needassign_txh = NULL;
1120168404Spjd
1121168404Spjd	/*
1122168404Spjd	 * NB: No error returns are allowed after txg_hold_open, but
1123168404Spjd	 * before processing the dnode holds, due to the
1124168404Spjd	 * dmu_tx_unassign() logic.
1125168404Spjd	 */
1126168404Spjd
1127185029Spjd	towrite = tofree = tooverwrite = tounref = tohold = fudge = 0;
1128168404Spjd	for (txh = list_head(&tx->tx_holds); txh;
1129168404Spjd	    txh = list_next(&tx->tx_holds, txh)) {
1130168404Spjd		dnode_t *dn = txh->txh_dnode;
1131168404Spjd		if (dn != NULL) {
1132168404Spjd			mutex_enter(&dn->dn_mtx);
1133168404Spjd			if (dn->dn_assigned_txg == tx->tx_txg - 1) {
1134168404Spjd				mutex_exit(&dn->dn_mtx);
1135168404Spjd				tx->tx_needassign_txh = txh;
1136249643Smm				return (SET_ERROR(ERESTART));
1137168404Spjd			}
1138168404Spjd			if (dn->dn_assigned_txg == 0)
1139168404Spjd				dn->dn_assigned_txg = tx->tx_txg;
1140168404Spjd			ASSERT3U(dn->dn_assigned_txg, ==, tx->tx_txg);
1141168404Spjd			(void) refcount_add(&dn->dn_tx_holds, tx);
1142168404Spjd			mutex_exit(&dn->dn_mtx);
1143168404Spjd		}
1144168404Spjd		towrite += txh->txh_space_towrite;
1145168404Spjd		tofree += txh->txh_space_tofree;
1146168404Spjd		tooverwrite += txh->txh_space_tooverwrite;
1147185029Spjd		tounref += txh->txh_space_tounref;
1148185029Spjd		tohold += txh->txh_memory_tohold;
1149185029Spjd		fudge += txh->txh_fudge;
1150168404Spjd	}
1151168404Spjd
1152168404Spjd	/*
1153168404Spjd	 * If a snapshot has been taken since we made our estimates,
1154168404Spjd	 * assume that we won't be able to free or overwrite anything.
1155168404Spjd	 */
1156168404Spjd	if (tx->tx_objset &&
1157219089Spjd	    dsl_dataset_prev_snap_txg(tx->tx_objset->os_dsl_dataset) >
1158168404Spjd	    tx->tx_lastsnap_txg) {
1159168404Spjd		towrite += tooverwrite;
1160168404Spjd		tooverwrite = tofree = 0;
1161168404Spjd	}
1162168404Spjd
1163185029Spjd	/* needed allocation: worst-case estimate of write space */
1164185029Spjd	asize = spa_get_asize(tx->tx_pool->dp_spa, towrite + tooverwrite);
1165185029Spjd	/* freed space estimate: worst-case overwrite + free estimate */
1166168404Spjd	fsize = spa_get_asize(tx->tx_pool->dp_spa, tooverwrite) + tofree;
1167185029Spjd	/* convert unrefd space to worst-case estimate */
1168185029Spjd	usize = spa_get_asize(tx->tx_pool->dp_spa, tounref);
1169185029Spjd	/* calculate memory footprint estimate */
1170185029Spjd	memory = towrite + tooverwrite + tohold;
1171168404Spjd
1172168404Spjd#ifdef ZFS_DEBUG
1173185029Spjd	/*
1174185029Spjd	 * Add in 'tohold' to account for our dirty holds on this memory
1175185029Spjd	 * XXX - the "fudge" factor is to account for skipped blocks that
1176185029Spjd	 * we missed because dnode_next_offset() misses in-core-only blocks.
1177185029Spjd	 */
1178185029Spjd	tx->tx_space_towrite = asize +
1179185029Spjd	    spa_get_asize(tx->tx_pool->dp_spa, tohold + fudge);
1180168404Spjd	tx->tx_space_tofree = tofree;
1181168404Spjd	tx->tx_space_tooverwrite = tooverwrite;
1182185029Spjd	tx->tx_space_tounref = tounref;
1183168404Spjd#endif
1184168404Spjd
1185168404Spjd	if (tx->tx_dir && asize != 0) {
1186185029Spjd		int err = dsl_dir_tempreserve_space(tx->tx_dir, memory,
1187185029Spjd		    asize, fsize, usize, &tx->tx_tempreserve_cookie, tx);
1188168404Spjd		if (err)
1189168404Spjd			return (err);
1190168404Spjd	}
1191168404Spjd
1192168404Spjd	return (0);
1193168404Spjd}
1194168404Spjd
1195168404Spjdstatic void
1196168404Spjddmu_tx_unassign(dmu_tx_t *tx)
1197168404Spjd{
1198168404Spjd	dmu_tx_hold_t *txh;
1199168404Spjd
1200168404Spjd	if (tx->tx_txg == 0)
1201168404Spjd		return;
1202168404Spjd
1203168404Spjd	txg_rele_to_quiesce(&tx->tx_txgh);
1204168404Spjd
1205252749Sdelphij	/*
1206252749Sdelphij	 * Walk the transaction's hold list, removing the hold on the
1207252749Sdelphij	 * associated dnode, and notifying waiters if the refcount drops to 0.
1208252749Sdelphij	 */
1209168404Spjd	for (txh = list_head(&tx->tx_holds); txh != tx->tx_needassign_txh;
1210168404Spjd	    txh = list_next(&tx->tx_holds, txh)) {
1211168404Spjd		dnode_t *dn = txh->txh_dnode;
1212168404Spjd
1213168404Spjd		if (dn == NULL)
1214168404Spjd			continue;
1215168404Spjd		mutex_enter(&dn->dn_mtx);
1216168404Spjd		ASSERT3U(dn->dn_assigned_txg, ==, tx->tx_txg);
1217168404Spjd
1218168404Spjd		if (refcount_remove(&dn->dn_tx_holds, tx) == 0) {
1219168404Spjd			dn->dn_assigned_txg = 0;
1220168404Spjd			cv_broadcast(&dn->dn_notxholds);
1221168404Spjd		}
1222168404Spjd		mutex_exit(&dn->dn_mtx);
1223168404Spjd	}
1224168404Spjd
1225168404Spjd	txg_rele_to_sync(&tx->tx_txgh);
1226168404Spjd
1227168404Spjd	tx->tx_lasttried_txg = tx->tx_txg;
1228168404Spjd	tx->tx_txg = 0;
1229168404Spjd}
1230168404Spjd
1231168404Spjd/*
1232168404Spjd * Assign tx to a transaction group.  txg_how can be one of:
1233168404Spjd *
1234168404Spjd * (1)	TXG_WAIT.  If the current open txg is full, waits until there's
1235168404Spjd *	a new one.  This should be used when you're not holding locks.
1236249643Smm *	It will only fail if we're truly out of space (or over quota).
1237168404Spjd *
1238168404Spjd * (2)	TXG_NOWAIT.  If we can't assign into the current open txg without
1239168404Spjd *	blocking, returns immediately with ERESTART.  This should be used
1240168404Spjd *	whenever you're holding locks.  On an ERESTART error, the caller
1241168404Spjd *	should drop locks, do a dmu_tx_wait(tx), and try again.
1242260764Savg *
1243260764Savg * (3)  TXG_WAITED.  Like TXG_NOWAIT, but indicates that dmu_tx_wait()
1244260764Savg *      has already been called on behalf of this operation (though
1245260764Savg *      most likely on a different tx).
1246168404Spjd */
1247168404Spjdint
1248249643Smmdmu_tx_assign(dmu_tx_t *tx, txg_how_t txg_how)
1249168404Spjd{
1250168404Spjd	int err;
1251168404Spjd
1252168404Spjd	ASSERT(tx->tx_txg == 0);
1253260764Savg	ASSERT(txg_how == TXG_WAIT || txg_how == TXG_NOWAIT ||
1254260764Savg	    txg_how == TXG_WAITED);
1255168404Spjd	ASSERT(!dsl_pool_sync_context(tx->tx_pool));
1256168404Spjd
1257249643Smm	/* If we might wait, we must not hold the config lock. */
1258249643Smm	ASSERT(txg_how != TXG_WAIT || !dsl_pool_config_held(tx->tx_pool));
1259249643Smm
1260260764Savg	if (txg_how == TXG_WAITED)
1261260764Savg		tx->tx_waited = B_TRUE;
1262260764Savg
1263168404Spjd	while ((err = dmu_tx_try_assign(tx, txg_how)) != 0) {
1264168404Spjd		dmu_tx_unassign(tx);
1265168404Spjd
1266168404Spjd		if (err != ERESTART || txg_how != TXG_WAIT)
1267168404Spjd			return (err);
1268168404Spjd
1269168404Spjd		dmu_tx_wait(tx);
1270168404Spjd	}
1271168404Spjd
1272168404Spjd	txg_rele_to_quiesce(&tx->tx_txgh);
1273168404Spjd
1274168404Spjd	return (0);
1275168404Spjd}
1276168404Spjd
1277168404Spjdvoid
1278168404Spjddmu_tx_wait(dmu_tx_t *tx)
1279168404Spjd{
1280185029Spjd	spa_t *spa = tx->tx_pool->dp_spa;
1281260764Savg	dsl_pool_t *dp = tx->tx_pool;
1282185029Spjd
1283168404Spjd	ASSERT(tx->tx_txg == 0);
1284249643Smm	ASSERT(!dsl_pool_config_held(tx->tx_pool));
1285168404Spjd
1286260764Savg	if (tx->tx_wait_dirty) {
1287260764Savg		/*
1288260764Savg		 * dmu_tx_try_assign() has determined that we need to wait
1289260764Savg		 * because we've consumed much or all of the dirty buffer
1290260764Savg		 * space.
1291260764Savg		 */
1292260764Savg		mutex_enter(&dp->dp_lock);
1293260764Savg		while (dp->dp_dirty_total >= zfs_dirty_data_max)
1294260764Savg			cv_wait(&dp->dp_spaceavail_cv, &dp->dp_lock);
1295260764Savg		uint64_t dirty = dp->dp_dirty_total;
1296260764Savg		mutex_exit(&dp->dp_lock);
1297260764Savg
1298260764Savg		dmu_tx_delay(tx, dirty);
1299260764Savg
1300260764Savg		tx->tx_wait_dirty = B_FALSE;
1301260764Savg
1302260764Savg		/*
1303260764Savg		 * Note: setting tx_waited only has effect if the caller
1304260764Savg		 * used TX_WAIT.  Otherwise they are going to destroy
1305260764Savg		 * this tx and try again.  The common case, zfs_write(),
1306260764Savg		 * uses TX_WAIT.
1307260764Savg		 */
1308260764Savg		tx->tx_waited = B_TRUE;
1309260764Savg	} else if (spa_suspended(spa) || tx->tx_lasttried_txg == 0) {
1310260764Savg		/*
1311260764Savg		 * If the pool is suspended we need to wait until it
1312260764Savg		 * is resumed.  Note that it's possible that the pool
1313260764Savg		 * has become active after this thread has tried to
1314260764Savg		 * obtain a tx.  If that's the case then tx_lasttried_txg
1315260764Savg		 * would not have been set.
1316260764Savg		 */
1317260764Savg		txg_wait_synced(dp, spa_last_synced_txg(spa) + 1);
1318185029Spjd	} else if (tx->tx_needassign_txh) {
1319260764Savg		/*
1320260764Savg		 * A dnode is assigned to the quiescing txg.  Wait for its
1321260764Savg		 * transaction to complete.
1322260764Savg		 */
1323168404Spjd		dnode_t *dn = tx->tx_needassign_txh->txh_dnode;
1324168404Spjd
1325168404Spjd		mutex_enter(&dn->dn_mtx);
1326168404Spjd		while (dn->dn_assigned_txg == tx->tx_lasttried_txg - 1)
1327168404Spjd			cv_wait(&dn->dn_notxholds, &dn->dn_mtx);
1328168404Spjd		mutex_exit(&dn->dn_mtx);
1329168404Spjd		tx->tx_needassign_txh = NULL;
1330168404Spjd	} else {
1331168404Spjd		txg_wait_open(tx->tx_pool, tx->tx_lasttried_txg + 1);
1332168404Spjd	}
1333168404Spjd}
1334168404Spjd
1335168404Spjdvoid
1336168404Spjddmu_tx_willuse_space(dmu_tx_t *tx, int64_t delta)
1337168404Spjd{
1338168404Spjd#ifdef ZFS_DEBUG
1339168404Spjd	if (tx->tx_dir == NULL || delta == 0)
1340168404Spjd		return;
1341168404Spjd
1342168404Spjd	if (delta > 0) {
1343168404Spjd		ASSERT3U(refcount_count(&tx->tx_space_written) + delta, <=,
1344168404Spjd		    tx->tx_space_towrite);
1345168404Spjd		(void) refcount_add_many(&tx->tx_space_written, delta, NULL);
1346168404Spjd	} else {
1347168404Spjd		(void) refcount_add_many(&tx->tx_space_freed, -delta, NULL);
1348168404Spjd	}
1349168404Spjd#endif
1350168404Spjd}
1351168404Spjd
1352168404Spjdvoid
1353168404Spjddmu_tx_commit(dmu_tx_t *tx)
1354168404Spjd{
1355168404Spjd	dmu_tx_hold_t *txh;
1356168404Spjd
1357168404Spjd	ASSERT(tx->tx_txg != 0);
1358168404Spjd
1359252749Sdelphij	/*
1360252749Sdelphij	 * Go through the transaction's hold list and remove holds on
1361252749Sdelphij	 * associated dnodes, notifying waiters if no holds remain.
1362252749Sdelphij	 */
1363168404Spjd	while (txh = list_head(&tx->tx_holds)) {
1364168404Spjd		dnode_t *dn = txh->txh_dnode;
1365168404Spjd
1366168404Spjd		list_remove(&tx->tx_holds, txh);
1367168404Spjd		kmem_free(txh, sizeof (dmu_tx_hold_t));
1368168404Spjd		if (dn == NULL)
1369168404Spjd			continue;
1370168404Spjd		mutex_enter(&dn->dn_mtx);
1371168404Spjd		ASSERT3U(dn->dn_assigned_txg, ==, tx->tx_txg);
1372168404Spjd
1373168404Spjd		if (refcount_remove(&dn->dn_tx_holds, tx) == 0) {
1374168404Spjd			dn->dn_assigned_txg = 0;
1375168404Spjd			cv_broadcast(&dn->dn_notxholds);
1376168404Spjd		}
1377168404Spjd		mutex_exit(&dn->dn_mtx);
1378168404Spjd		dnode_rele(dn, tx);
1379168404Spjd	}
1380168404Spjd
1381168404Spjd	if (tx->tx_tempreserve_cookie)
1382168404Spjd		dsl_dir_tempreserve_clear(tx->tx_tempreserve_cookie, tx);
1383168404Spjd
1384219089Spjd	if (!list_is_empty(&tx->tx_callbacks))
1385219089Spjd		txg_register_callbacks(&tx->tx_txgh, &tx->tx_callbacks);
1386219089Spjd
1387168404Spjd	if (tx->tx_anyobj == FALSE)
1388168404Spjd		txg_rele_to_sync(&tx->tx_txgh);
1389219089Spjd
1390219089Spjd	list_destroy(&tx->tx_callbacks);
1391185029Spjd	list_destroy(&tx->tx_holds);
1392168404Spjd#ifdef ZFS_DEBUG
1393168404Spjd	dprintf("towrite=%llu written=%llu tofree=%llu freed=%llu\n",
1394168404Spjd	    tx->tx_space_towrite, refcount_count(&tx->tx_space_written),
1395168404Spjd	    tx->tx_space_tofree, refcount_count(&tx->tx_space_freed));
1396168404Spjd	refcount_destroy_many(&tx->tx_space_written,
1397168404Spjd	    refcount_count(&tx->tx_space_written));
1398168404Spjd	refcount_destroy_many(&tx->tx_space_freed,
1399168404Spjd	    refcount_count(&tx->tx_space_freed));
1400168404Spjd#endif
1401168404Spjd	kmem_free(tx, sizeof (dmu_tx_t));
1402168404Spjd}
1403168404Spjd
1404168404Spjdvoid
1405168404Spjddmu_tx_abort(dmu_tx_t *tx)
1406168404Spjd{
1407168404Spjd	dmu_tx_hold_t *txh;
1408168404Spjd
1409168404Spjd	ASSERT(tx->tx_txg == 0);
1410168404Spjd
1411168404Spjd	while (txh = list_head(&tx->tx_holds)) {
1412168404Spjd		dnode_t *dn = txh->txh_dnode;
1413168404Spjd
1414168404Spjd		list_remove(&tx->tx_holds, txh);
1415168404Spjd		kmem_free(txh, sizeof (dmu_tx_hold_t));
1416168404Spjd		if (dn != NULL)
1417168404Spjd			dnode_rele(dn, tx);
1418168404Spjd	}
1419219089Spjd
1420219089Spjd	/*
1421219089Spjd	 * Call any registered callbacks with an error code.
1422219089Spjd	 */
1423219089Spjd	if (!list_is_empty(&tx->tx_callbacks))
1424219089Spjd		dmu_tx_do_callbacks(&tx->tx_callbacks, ECANCELED);
1425219089Spjd
1426219089Spjd	list_destroy(&tx->tx_callbacks);
1427185029Spjd	list_destroy(&tx->tx_holds);
1428168404Spjd#ifdef ZFS_DEBUG
1429168404Spjd	refcount_destroy_many(&tx->tx_space_written,
1430168404Spjd	    refcount_count(&tx->tx_space_written));
1431168404Spjd	refcount_destroy_many(&tx->tx_space_freed,
1432168404Spjd	    refcount_count(&tx->tx_space_freed));
1433168404Spjd#endif
1434168404Spjd	kmem_free(tx, sizeof (dmu_tx_t));
1435168404Spjd}
1436168404Spjd
1437168404Spjduint64_t
1438168404Spjddmu_tx_get_txg(dmu_tx_t *tx)
1439168404Spjd{
1440168404Spjd	ASSERT(tx->tx_txg != 0);
1441168404Spjd	return (tx->tx_txg);
1442168404Spjd}
1443219089Spjd
1444249643Smmdsl_pool_t *
1445249643Smmdmu_tx_pool(dmu_tx_t *tx)
1446249643Smm{
1447249643Smm	ASSERT(tx->tx_pool != NULL);
1448249643Smm	return (tx->tx_pool);
1449249643Smm}
1450249643Smm
1451249643Smm
1452219089Spjdvoid
1453219089Spjddmu_tx_callback_register(dmu_tx_t *tx, dmu_tx_callback_func_t *func, void *data)
1454219089Spjd{
1455219089Spjd	dmu_tx_callback_t *dcb;
1456219089Spjd
1457219089Spjd	dcb = kmem_alloc(sizeof (dmu_tx_callback_t), KM_SLEEP);
1458219089Spjd
1459219089Spjd	dcb->dcb_func = func;
1460219089Spjd	dcb->dcb_data = data;
1461219089Spjd
1462219089Spjd	list_insert_tail(&tx->tx_callbacks, dcb);
1463219089Spjd}
1464219089Spjd
1465219089Spjd/*
1466219089Spjd * Call all the commit callbacks on a list, with a given error code.
1467219089Spjd */
1468219089Spjdvoid
1469219089Spjddmu_tx_do_callbacks(list_t *cb_list, int error)
1470219089Spjd{
1471219089Spjd	dmu_tx_callback_t *dcb;
1472219089Spjd
1473219089Spjd	while (dcb = list_head(cb_list)) {
1474219089Spjd		list_remove(cb_list, dcb);
1475219089Spjd		dcb->dcb_func(dcb->dcb_data, error);
1476219089Spjd		kmem_free(dcb, sizeof (dmu_tx_callback_t));
1477219089Spjd	}
1478219089Spjd}
1479219089Spjd
1480219089Spjd/*
1481219089Spjd * Interface to hold a bunch of attributes.
1482219089Spjd * used for creating new files.
1483219089Spjd * attrsize is the total size of all attributes
1484219089Spjd * to be added during object creation
1485219089Spjd *
1486219089Spjd * For updating/adding a single attribute dmu_tx_hold_sa() should be used.
1487219089Spjd */
1488219089Spjd
1489219089Spjd/*
1490219089Spjd * hold necessary attribute name for attribute registration.
1491219089Spjd * should be a very rare case where this is needed.  If it does
1492219089Spjd * happen it would only happen on the first write to the file system.
1493219089Spjd */
1494219089Spjdstatic void
1495219089Spjddmu_tx_sa_registration_hold(sa_os_t *sa, dmu_tx_t *tx)
1496219089Spjd{
1497219089Spjd	int i;
1498219089Spjd
1499219089Spjd	if (!sa->sa_need_attr_registration)
1500219089Spjd		return;
1501219089Spjd
1502219089Spjd	for (i = 0; i != sa->sa_num_attrs; i++) {
1503219089Spjd		if (!sa->sa_attr_table[i].sa_registered) {
1504219089Spjd			if (sa->sa_reg_attr_obj)
1505219089Spjd				dmu_tx_hold_zap(tx, sa->sa_reg_attr_obj,
1506219089Spjd				    B_TRUE, sa->sa_attr_table[i].sa_name);
1507219089Spjd			else
1508219089Spjd				dmu_tx_hold_zap(tx, DMU_NEW_OBJECT,
1509219089Spjd				    B_TRUE, sa->sa_attr_table[i].sa_name);
1510219089Spjd		}
1511219089Spjd	}
1512219089Spjd}
1513219089Spjd
1514219089Spjd
1515219089Spjdvoid
1516219089Spjddmu_tx_hold_spill(dmu_tx_t *tx, uint64_t object)
1517219089Spjd{
1518219089Spjd	dnode_t *dn;
1519219089Spjd	dmu_tx_hold_t *txh;
1520219089Spjd
1521219089Spjd	txh = dmu_tx_hold_object_impl(tx, tx->tx_objset, object,
1522219089Spjd	    THT_SPILL, 0, 0);
1523219089Spjd
1524219089Spjd	dn = txh->txh_dnode;
1525219089Spjd
1526219089Spjd	if (dn == NULL)
1527219089Spjd		return;
1528219089Spjd
1529219089Spjd	/* If blkptr doesn't exist then add space to towrite */
1530219089Spjd	if (!(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR)) {
1531219089Spjd		txh->txh_space_towrite += SPA_MAXBLOCKSIZE;
1532219089Spjd	} else {
1533226944Smm		blkptr_t *bp;
1534226944Smm
1535219089Spjd		bp = &dn->dn_phys->dn_spill;
1536219089Spjd		if (dsl_dataset_block_freeable(dn->dn_objset->os_dsl_dataset,
1537219089Spjd		    bp, bp->blk_birth))
1538219089Spjd			txh->txh_space_tooverwrite += SPA_MAXBLOCKSIZE;
1539219089Spjd		else
1540219089Spjd			txh->txh_space_towrite += SPA_MAXBLOCKSIZE;
1541226944Smm		if (!BP_IS_HOLE(bp))
1542219089Spjd			txh->txh_space_tounref += SPA_MAXBLOCKSIZE;
1543219089Spjd	}
1544219089Spjd}
1545219089Spjd
1546219089Spjdvoid
1547219089Spjddmu_tx_hold_sa_create(dmu_tx_t *tx, int attrsize)
1548219089Spjd{
1549219089Spjd	sa_os_t *sa = tx->tx_objset->os_sa;
1550219089Spjd
1551219089Spjd	dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1552219089Spjd
1553219089Spjd	if (tx->tx_objset->os_sa->sa_master_obj == 0)
1554219089Spjd		return;
1555219089Spjd
1556219089Spjd	if (tx->tx_objset->os_sa->sa_layout_attr_obj)
1557219089Spjd		dmu_tx_hold_zap(tx, sa->sa_layout_attr_obj, B_TRUE, NULL);
1558219089Spjd	else {
1559219089Spjd		dmu_tx_hold_zap(tx, sa->sa_master_obj, B_TRUE, SA_LAYOUTS);
1560219089Spjd		dmu_tx_hold_zap(tx, sa->sa_master_obj, B_TRUE, SA_REGISTRY);
1561219089Spjd		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
1562219089Spjd		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
1563219089Spjd	}
1564219089Spjd
1565219089Spjd	dmu_tx_sa_registration_hold(sa, tx);
1566219089Spjd
1567219089Spjd	if (attrsize <= DN_MAX_BONUSLEN && !sa->sa_force_spill)
1568219089Spjd		return;
1569219089Spjd
1570219089Spjd	(void) dmu_tx_hold_object_impl(tx, tx->tx_objset, DMU_NEW_OBJECT,
1571219089Spjd	    THT_SPILL, 0, 0);
1572219089Spjd}
1573219089Spjd
1574219089Spjd/*
1575219089Spjd * Hold SA attribute
1576219089Spjd *
1577219089Spjd * dmu_tx_hold_sa(dmu_tx_t *tx, sa_handle_t *, attribute, add, size)
1578219089Spjd *
1579219089Spjd * variable_size is the total size of all variable sized attributes
1580219089Spjd * passed to this function.  It is not the total size of all
1581219089Spjd * variable size attributes that *may* exist on this object.
1582219089Spjd */
1583219089Spjdvoid
1584219089Spjddmu_tx_hold_sa(dmu_tx_t *tx, sa_handle_t *hdl, boolean_t may_grow)
1585219089Spjd{
1586219089Spjd	uint64_t object;
1587219089Spjd	sa_os_t *sa = tx->tx_objset->os_sa;
1588219089Spjd
1589219089Spjd	ASSERT(hdl != NULL);
1590219089Spjd
1591219089Spjd	object = sa_handle_object(hdl);
1592219089Spjd
1593219089Spjd	dmu_tx_hold_bonus(tx, object);
1594219089Spjd
1595219089Spjd	if (tx->tx_objset->os_sa->sa_master_obj == 0)
1596219089Spjd		return;
1597219089Spjd
1598219089Spjd	if (tx->tx_objset->os_sa->sa_reg_attr_obj == 0 ||
1599219089Spjd	    tx->tx_objset->os_sa->sa_layout_attr_obj == 0) {
1600219089Spjd		dmu_tx_hold_zap(tx, sa->sa_master_obj, B_TRUE, SA_LAYOUTS);
1601219089Spjd		dmu_tx_hold_zap(tx, sa->sa_master_obj, B_TRUE, SA_REGISTRY);
1602219089Spjd		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
1603219089Spjd		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
1604219089Spjd	}
1605219089Spjd
1606219089Spjd	dmu_tx_sa_registration_hold(sa, tx);
1607219089Spjd
1608219089Spjd	if (may_grow && tx->tx_objset->os_sa->sa_layout_attr_obj)
1609219089Spjd		dmu_tx_hold_zap(tx, sa->sa_layout_attr_obj, B_TRUE, NULL);
1610219089Spjd
1611219089Spjd	if (sa->sa_force_spill || may_grow || hdl->sa_spill) {
1612219089Spjd		ASSERT(tx->tx_txg == 0);
1613219089Spjd		dmu_tx_hold_spill(tx, object);
1614219089Spjd	} else {
1615219089Spjd		dmu_buf_impl_t *db = (dmu_buf_impl_t *)hdl->sa_bonus;
1616219089Spjd		dnode_t *dn;
1617219089Spjd
1618219089Spjd		DB_DNODE_ENTER(db);
1619219089Spjd		dn = DB_DNODE(db);
1620219089Spjd		if (dn->dn_have_spill) {
1621219089Spjd			ASSERT(tx->tx_txg == 0);
1622219089Spjd			dmu_tx_hold_spill(tx, object);
1623219089Spjd		}
1624219089Spjd		DB_DNODE_EXIT(db);
1625219089Spjd	}
1626219089Spjd}
1627