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.
24238fb8bbSMatthew Ahrens * Copyright (c) 2012, 2015 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 *
dmu_tx_create_dd(dsl_dir_t * dd)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));
5769962b56SMatthew Ahrens tx->tx_start = gethrtime();
588a2f1b91Sahrens #ifdef ZFS_DEBUG
59fa9e4066Sahrens refcount_create(&tx->tx_space_written);
60fa9e4066Sahrens refcount_create(&tx->tx_space_freed);
618a2f1b91Sahrens #endif
62fa9e4066Sahrens return (tx);
63fa9e4066Sahrens }
64fa9e4066Sahrens
65fa9e4066Sahrens dmu_tx_t *
dmu_tx_create(objset_t * os)66fa9e4066Sahrens dmu_tx_create(objset_t *os)
67fa9e4066Sahrens {
68503ad85cSMatthew Ahrens dmu_tx_t *tx = dmu_tx_create_dd(os->os_dsl_dataset->ds_dir);
69fa9e4066Sahrens tx->tx_objset = os;
70503ad85cSMatthew Ahrens tx->tx_lastsnap_txg = dsl_dataset_prev_snap_txg(os->os_dsl_dataset);
71fa9e4066Sahrens return (tx);
72fa9e4066Sahrens }
73fa9e4066Sahrens
74fa9e4066Sahrens dmu_tx_t *
dmu_tx_create_assigned(struct dsl_pool * dp,uint64_t txg)75fa9e4066Sahrens dmu_tx_create_assigned(struct dsl_pool *dp, uint64_t txg)
76fa9e4066Sahrens {
771d452cf5Sahrens dmu_tx_t *tx = dmu_tx_create_dd(NULL);
78fa9e4066Sahrens
79fa9e4066Sahrens ASSERT3U(txg, <=, dp->dp_tx.tx_open_txg);
80fa9e4066Sahrens tx->tx_pool = dp;
81fa9e4066Sahrens tx->tx_txg = txg;
82fa9e4066Sahrens tx->tx_anyobj = TRUE;
83fa9e4066Sahrens
84fa9e4066Sahrens return (tx);
85fa9e4066Sahrens }
86fa9e4066Sahrens
87fa9e4066Sahrens int
dmu_tx_is_syncing(dmu_tx_t * tx)88fa9e4066Sahrens dmu_tx_is_syncing(dmu_tx_t *tx)
89fa9e4066Sahrens {
90fa9e4066Sahrens return (tx->tx_anyobj);
91fa9e4066Sahrens }
92fa9e4066Sahrens
93fa9e4066Sahrens int
dmu_tx_private_ok(dmu_tx_t * tx)94fa9e4066Sahrens dmu_tx_private_ok(dmu_tx_t *tx)
95fa9e4066Sahrens {
96ea8dc4b6Seschrock return (tx->tx_anyobj);
97fa9e4066Sahrens }
98fa9e4066Sahrens
998a2f1b91Sahrens static dmu_tx_hold_t *
dmu_tx_hold_object_impl(dmu_tx_t * tx,objset_t * os,uint64_t object,enum dmu_tx_hold_type type,uint64_t arg1,uint64_t arg2)100fa9e4066Sahrens dmu_tx_hold_object_impl(dmu_tx_t *tx, objset_t *os, uint64_t object,
1018a2f1b91Sahrens enum dmu_tx_hold_type type, uint64_t arg1, uint64_t arg2)
102fa9e4066Sahrens {
1038a2f1b91Sahrens dmu_tx_hold_t *txh;
104fa9e4066Sahrens dnode_t *dn = NULL;
105ea8dc4b6Seschrock int err;
106fa9e4066Sahrens
107fa9e4066Sahrens if (object != DMU_NEW_OBJECT) {
108503ad85cSMatthew Ahrens err = dnode_hold(os, object, tx, &dn);
109ea8dc4b6Seschrock if (err) {
110ea8dc4b6Seschrock tx->tx_err = err;
1118a2f1b91Sahrens return (NULL);
112ea8dc4b6Seschrock }
113fa9e4066Sahrens
114ea8dc4b6Seschrock if (err == 0 && tx->tx_txg != 0) {
115fa9e4066Sahrens mutex_enter(&dn->dn_mtx);
116fa9e4066Sahrens /*
117fa9e4066Sahrens * dn->dn_assigned_txg == tx->tx_txg doesn't pose a
118fa9e4066Sahrens * problem, but there's no way for it to happen (for
119fa9e4066Sahrens * now, at least).
120fa9e4066Sahrens */
121fa9e4066Sahrens ASSERT(dn->dn_assigned_txg == 0);
122fa9e4066Sahrens dn->dn_assigned_txg = tx->tx_txg;
123fa9e4066Sahrens (void) refcount_add(&dn->dn_tx_holds, tx);
124fa9e4066Sahrens mutex_exit(&dn->dn_mtx);
125fa9e4066Sahrens }
126fa9e4066Sahrens }
127fa9e4066Sahrens
1288a2f1b91Sahrens txh = kmem_zalloc(sizeof (dmu_tx_hold_t), KM_SLEEP);
1298a2f1b91Sahrens txh->txh_tx = tx;
1308a2f1b91Sahrens txh->txh_dnode = dn;
1318a2f1b91Sahrens #ifdef ZFS_DEBUG
1328a2f1b91Sahrens txh->txh_type = type;
1338a2f1b91Sahrens txh->txh_arg1 = arg1;
1348a2f1b91Sahrens txh->txh_arg2 = arg2;
1358a2f1b91Sahrens #endif
1368a2f1b91Sahrens list_insert_tail(&tx->tx_holds, txh);
137ea8dc4b6Seschrock
1388a2f1b91Sahrens return (txh);
139fa9e4066Sahrens }
140fa9e4066Sahrens
141fa9e4066Sahrens void
dmu_tx_add_new_object(dmu_tx_t * tx,objset_t * os,uint64_t object)142fa9e4066Sahrens dmu_tx_add_new_object(dmu_tx_t *tx, objset_t *os, uint64_t object)
143fa9e4066Sahrens {
144fa9e4066Sahrens /*
145fa9e4066Sahrens * If we're syncing, they can manipulate any object anyhow, and
146fa9e4066Sahrens * the hold on the dnode_t can cause problems.
147fa9e4066Sahrens */
148fa9e4066Sahrens if (!dmu_tx_is_syncing(tx)) {
1498a2f1b91Sahrens (void) dmu_tx_hold_object_impl(tx, os,
1508a2f1b91Sahrens object, THT_NEWOBJECT, 0, 0);
151fa9e4066Sahrens }
152fa9e4066Sahrens }
153fa9e4066Sahrens
154ea8dc4b6Seschrock static int
dmu_tx_check_ioerr(zio_t * zio,dnode_t * dn,int level,uint64_t blkid)155ea8dc4b6Seschrock dmu_tx_check_ioerr(zio_t *zio, dnode_t *dn, int level, uint64_t blkid)
156ea8dc4b6Seschrock {
157ea8dc4b6Seschrock int err;
158ea8dc4b6Seschrock dmu_buf_impl_t *db;
159ea8dc4b6Seschrock
160ea8dc4b6Seschrock rw_enter(&dn->dn_struct_rwlock, RW_READER);
161ea8dc4b6Seschrock db = dbuf_hold_level(dn, level, blkid, FTAG);
162ea8dc4b6Seschrock rw_exit(&dn->dn_struct_rwlock);
163ea8dc4b6Seschrock if (db == NULL)
164be6fd75aSMatthew Ahrens return (SET_ERROR(EIO));
1651ab7f2deSmaybee err = dbuf_read(db, zio, DB_RF_CANFAIL | DB_RF_NOPREFETCH);
166ea8dc4b6Seschrock dbuf_rele(db, FTAG);
167ea8dc4b6Seschrock return (err);
168ea8dc4b6Seschrock }
169ea8dc4b6Seschrock
1704a7f2a75SMark Maybee static void
dmu_tx_count_twig(dmu_tx_hold_t * txh,dnode_t * dn,dmu_buf_impl_t * db,int level,uint64_t blkid,boolean_t freeable,uint64_t * history)171b24ab676SJeff Bonwick dmu_tx_count_twig(dmu_tx_hold_t *txh, dnode_t *dn, dmu_buf_impl_t *db,
172b24ab676SJeff Bonwick int level, uint64_t blkid, boolean_t freeable, uint64_t *history)
1734a7f2a75SMark Maybee {
174b24ab676SJeff Bonwick objset_t *os = dn->dn_objset;
175b24ab676SJeff Bonwick dsl_dataset_t *ds = os->os_dsl_dataset;
176b24ab676SJeff Bonwick int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
177b24ab676SJeff Bonwick dmu_buf_impl_t *parent = NULL;
178b24ab676SJeff Bonwick blkptr_t *bp = NULL;
179b24ab676SJeff Bonwick uint64_t space;
1804a7f2a75SMark Maybee
181b24ab676SJeff Bonwick if (level >= dn->dn_nlevels || history[level] == blkid)
1824a7f2a75SMark Maybee return;
1834a7f2a75SMark Maybee
184b24ab676SJeff Bonwick history[level] = blkid;
1854a7f2a75SMark Maybee
186b24ab676SJeff Bonwick space = (level == 0) ? dn->dn_datablksz : (1ULL << dn->dn_indblkshift);
187b24ab676SJeff Bonwick
188b24ab676SJeff Bonwick if (db == NULL || db == dn->dn_dbuf) {
189b24ab676SJeff Bonwick ASSERT(level != 0);
190b24ab676SJeff Bonwick db = NULL;
191b24ab676SJeff Bonwick } else {
192744947dcSTom Erickson ASSERT(DB_DNODE(db) == dn);
193b24ab676SJeff Bonwick ASSERT(db->db_level == level);
194b24ab676SJeff Bonwick ASSERT(db->db.db_size == space);
195b24ab676SJeff Bonwick ASSERT(db->db_blkid == blkid);
196b24ab676SJeff Bonwick bp = db->db_blkptr;
197b24ab676SJeff Bonwick parent = db->db_parent;
1984a7f2a75SMark Maybee }
1994a7f2a75SMark Maybee
200b24ab676SJeff Bonwick freeable = (bp && (freeable ||
201c7cd2421SGeorge Wilson dsl_dataset_block_freeable(ds, bp, bp->blk_birth)));
2024a7f2a75SMark Maybee
2034a7f2a75SMark Maybee if (freeable)
2044a7f2a75SMark Maybee txh->txh_space_tooverwrite += space;
2054a7f2a75SMark Maybee else
2064a7f2a75SMark Maybee txh->txh_space_towrite += space;
207b24ab676SJeff Bonwick if (bp)
208b24ab676SJeff Bonwick txh->txh_space_tounref += bp_get_dsize(os->os_spa, bp);
209b24ab676SJeff Bonwick
210b24ab676SJeff Bonwick dmu_tx_count_twig(txh, dn, parent, level + 1,
211b24ab676SJeff Bonwick blkid >> epbs, freeable, history);
2124a7f2a75SMark Maybee }
2134a7f2a75SMark Maybee
214fa9e4066Sahrens /* ARGSUSED */
215fa9e4066Sahrens static void
dmu_tx_count_write(dmu_tx_hold_t * txh,uint64_t off,uint64_t len)2168a2f1b91Sahrens dmu_tx_count_write(dmu_tx_hold_t *txh, uint64_t off, uint64_t len)
217fa9e4066Sahrens {
2188a2f1b91Sahrens dnode_t *dn = txh->txh_dnode;
2198a2f1b91Sahrens uint64_t start, end, i;
220fa9e4066Sahrens int min_bs, max_bs, min_ibs, max_ibs, epbs, bits;
2218a2f1b91Sahrens int err = 0;
222fa9e4066Sahrens
223fa9e4066Sahrens if (len == 0)
224fa9e4066Sahrens return;
225fa9e4066Sahrens
226fa9e4066Sahrens min_bs = SPA_MINBLOCKSHIFT;
227d1a98260SMatthew Ahrens max_bs = highbit64(txh->txh_tx->tx_objset->os_recordsize) - 1;
228fa9e4066Sahrens min_ibs = DN_MIN_INDBLKSHIFT;
229fa9e4066Sahrens max_ibs = DN_MAX_INDBLKSHIFT;
230fa9e4066Sahrens
2314a7f2a75SMark Maybee if (dn) {
232b24ab676SJeff Bonwick uint64_t history[DN_MAX_LEVELS];
2334a7f2a75SMark Maybee int nlvls = dn->dn_nlevels;
2344a7f2a75SMark Maybee int delta;
2354a7f2a75SMark Maybee
236fa9e4066Sahrens /*
237ea8dc4b6Seschrock * For i/o error checking, read the first and last level-0
23899653d4eSeschrock * blocks (if they are not aligned), and all the level-1 blocks.
239ea8dc4b6Seschrock */
240ea8dc4b6Seschrock if (dn->dn_maxblkid == 0) {
2414a7f2a75SMark Maybee delta = dn->dn_datablksz;
2424a7f2a75SMark Maybee start = (off < dn->dn_datablksz) ? 0 : 1;
2434a7f2a75SMark Maybee end = (off+len <= dn->dn_datablksz) ? 0 : 1;
2444a7f2a75SMark Maybee if (start == 0 && (off > 0 || len < dn->dn_datablksz)) {
245ea8dc4b6Seschrock err = dmu_tx_check_ioerr(NULL, dn, 0, 0);
2468a2f1b91Sahrens if (err)
2478a2f1b91Sahrens goto out;
2484a7f2a75SMark Maybee delta -= off;
24982c9918fSTim Haley }
250ea8dc4b6Seschrock } else {
2518a2f1b91Sahrens zio_t *zio = zio_root(dn->dn_objset->os_spa,
252ea8dc4b6Seschrock NULL, NULL, ZIO_FLAG_CANFAIL);
253ea8dc4b6Seschrock
254ea8dc4b6Seschrock /* first level-0 block */
25599653d4eSeschrock start = off >> dn->dn_datablkshift;
25699653d4eSeschrock if (P2PHASE(off, dn->dn_datablksz) ||
25799653d4eSeschrock len < dn->dn_datablksz) {
258ea8dc4b6Seschrock err = dmu_tx_check_ioerr(zio, dn, 0, start);
2598a2f1b91Sahrens if (err)
2608a2f1b91Sahrens goto out;
26199653d4eSeschrock }
262ea8dc4b6Seschrock
263ea8dc4b6Seschrock /* last level-0 block */
26499653d4eSeschrock end = (off+len-1) >> dn->dn_datablkshift;
26582c9918fSTim Haley if (end != start && end <= dn->dn_maxblkid &&
26699653d4eSeschrock P2PHASE(off+len, dn->dn_datablksz)) {
267ea8dc4b6Seschrock err = dmu_tx_check_ioerr(zio, dn, 0, end);
2688a2f1b91Sahrens if (err)
2698a2f1b91Sahrens goto out;
270ea8dc4b6Seschrock }
271ea8dc4b6Seschrock
272ea8dc4b6Seschrock /* level-1 blocks */
2734a7f2a75SMark Maybee if (nlvls > 1) {
2744a7f2a75SMark Maybee int shft = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
2754a7f2a75SMark Maybee for (i = (start>>shft)+1; i < end>>shft; i++) {
276ea8dc4b6Seschrock err = dmu_tx_check_ioerr(zio, dn, 1, i);
2778a2f1b91Sahrens if (err)
2788a2f1b91Sahrens goto out;
279ea8dc4b6Seschrock }
280ea8dc4b6Seschrock }
281ea8dc4b6Seschrock
282ea8dc4b6Seschrock err = zio_wait(zio);
2838a2f1b91Sahrens if (err)
2848a2f1b91Sahrens goto out;
2854a7f2a75SMark Maybee delta = P2NPHASE(off, dn->dn_datablksz);
286ea8dc4b6Seschrock }
2874a7f2a75SMark Maybee
288bda88194SGeorge Wilson min_ibs = max_ibs = dn->dn_indblkshift;
2894a7f2a75SMark Maybee if (dn->dn_maxblkid > 0) {
2904a7f2a75SMark Maybee /*
2914a7f2a75SMark Maybee * The blocksize can't change,
2924a7f2a75SMark Maybee * so we can make a more precise estimate.
2934a7f2a75SMark Maybee */
2944a7f2a75SMark Maybee ASSERT(dn->dn_datablkshift != 0);
2954a7f2a75SMark Maybee min_bs = max_bs = dn->dn_datablkshift;
296d1a98260SMatthew Ahrens } else {
297d1a98260SMatthew Ahrens /*
298d1a98260SMatthew Ahrens * The blocksize can increase up to the recordsize,
299d1a98260SMatthew Ahrens * or if it is already more than the recordsize,
300d1a98260SMatthew Ahrens * up to the next power of 2.
301d1a98260SMatthew Ahrens */
302d1a98260SMatthew Ahrens min_bs = highbit64(dn->dn_datablksz - 1);
303d1a98260SMatthew Ahrens max_bs = MAX(max_bs, highbit64(dn->dn_datablksz - 1));
304ea8dc4b6Seschrock }
305ea8dc4b6Seschrock
306ea8dc4b6Seschrock /*
3074a7f2a75SMark Maybee * If this write is not off the end of the file
3084a7f2a75SMark Maybee * we need to account for overwrites/unref.
309fa9e4066Sahrens */
310b24ab676SJeff Bonwick if (start <= dn->dn_maxblkid) {
311b24ab676SJeff Bonwick for (int l = 0; l < DN_MAX_LEVELS; l++)
312b24ab676SJeff Bonwick history[l] = -1ULL;
313b24ab676SJeff Bonwick }
3144a7f2a75SMark Maybee while (start <= dn->dn_maxblkid) {
3154a7f2a75SMark Maybee dmu_buf_impl_t *db;
3164a7f2a75SMark Maybee
3174a7f2a75SMark Maybee rw_enter(&dn->dn_struct_rwlock, RW_READER);
318*5f9bb2f3SPaul Dagnelie err = dbuf_hold_impl(dn, 0, start,
319*5f9bb2f3SPaul Dagnelie FALSE, FALSE, FTAG, &db);
3204a7f2a75SMark Maybee rw_exit(&dn->dn_struct_rwlock);
32101025c89SJohn Harres
32201025c89SJohn Harres if (err) {
32301025c89SJohn Harres txh->txh_tx->tx_err = err;
32401025c89SJohn Harres return;
32501025c89SJohn Harres }
32601025c89SJohn Harres
327b24ab676SJeff Bonwick dmu_tx_count_twig(txh, dn, db, 0, start, B_FALSE,
328b24ab676SJeff Bonwick history);
3294a7f2a75SMark Maybee dbuf_rele(db, FTAG);
3304a7f2a75SMark Maybee if (++start > end) {
3314a7f2a75SMark Maybee /*
3324a7f2a75SMark Maybee * Account for new indirects appearing
3334a7f2a75SMark Maybee * before this IO gets assigned into a txg.
3344a7f2a75SMark Maybee */
3354a7f2a75SMark Maybee bits = 64 - min_bs;
3364a7f2a75SMark Maybee epbs = min_ibs - SPA_BLKPTRSHIFT;
3374a7f2a75SMark Maybee for (bits -= epbs * (nlvls - 1);
3384a7f2a75SMark Maybee bits >= 0; bits -= epbs)
3394a7f2a75SMark Maybee txh->txh_fudge += 1ULL << max_ibs;
3404a7f2a75SMark Maybee goto out;
3414a7f2a75SMark Maybee }
3424a7f2a75SMark Maybee off += delta;
3434a7f2a75SMark Maybee if (len >= delta)
3444a7f2a75SMark Maybee len -= delta;
3454a7f2a75SMark Maybee delta = dn->dn_datablksz;
3464a7f2a75SMark Maybee }
347fa9e4066Sahrens }
348fa9e4066Sahrens
349fa9e4066Sahrens /*
350fa9e4066Sahrens * 'end' is the last thing we will access, not one past.
351fa9e4066Sahrens * This way we won't overflow when accessing the last byte.
352fa9e4066Sahrens */
353fa9e4066Sahrens start = P2ALIGN(off, 1ULL << max_bs);
354fa9e4066Sahrens end = P2ROUNDUP(off + len, 1ULL << max_bs) - 1;
3558a2f1b91Sahrens txh->txh_space_towrite += end - start + 1;
356fa9e4066Sahrens
357fa9e4066Sahrens start >>= min_bs;
358fa9e4066Sahrens end >>= min_bs;
359fa9e4066Sahrens
360fa9e4066Sahrens epbs = min_ibs - SPA_BLKPTRSHIFT;
361fa9e4066Sahrens
362fa9e4066Sahrens /*
363fa9e4066Sahrens * The object contains at most 2^(64 - min_bs) blocks,
364fa9e4066Sahrens * and each indirect level maps 2^epbs.
365fa9e4066Sahrens */
366fa9e4066Sahrens for (bits = 64 - min_bs; bits >= 0; bits -= epbs) {
367fa9e4066Sahrens start >>= epbs;
368fa9e4066Sahrens end >>= epbs;
3694a7f2a75SMark Maybee ASSERT3U(end, >=, start);
3708a2f1b91Sahrens txh->txh_space_towrite += (end - start + 1) << max_ibs;
3714a7f2a75SMark Maybee if (start != 0) {
3724a7f2a75SMark Maybee /*
3734a7f2a75SMark Maybee * We also need a new blkid=0 indirect block
3744a7f2a75SMark Maybee * to reference any existing file data.
3754a7f2a75SMark Maybee */
3764a7f2a75SMark Maybee txh->txh_space_towrite += 1ULL << max_ibs;
3774a7f2a75SMark Maybee }
378fa9e4066Sahrens }
379fa9e4066Sahrens
3808a2f1b91Sahrens out:
3814a7f2a75SMark Maybee if (txh->txh_space_towrite + txh->txh_space_tooverwrite >
3824a7f2a75SMark Maybee 2 * DMU_MAX_ACCESS)
383be6fd75aSMatthew Ahrens err = SET_ERROR(EFBIG);
3844a7f2a75SMark Maybee
3858a2f1b91Sahrens if (err)
3868a2f1b91Sahrens txh->txh_tx->tx_err = err;
387fa9e4066Sahrens }
388fa9e4066Sahrens
389fa9e4066Sahrens static void
dmu_tx_count_dnode(dmu_tx_hold_t * txh)3908a2f1b91Sahrens dmu_tx_count_dnode(dmu_tx_hold_t *txh)
391fa9e4066Sahrens {
3928a2f1b91Sahrens dnode_t *dn = txh->txh_dnode;
393744947dcSTom Erickson dnode_t *mdn = DMU_META_DNODE(txh->txh_tx->tx_objset);
3948a2f1b91Sahrens uint64_t space = mdn->dn_datablksz +
3958a2f1b91Sahrens ((mdn->dn_nlevels-1) << mdn->dn_indblkshift);
396fa9e4066Sahrens
397fa9e4066Sahrens if (dn && dn->dn_dbuf->db_blkptr &&
398fa9e4066Sahrens dsl_dataset_block_freeable(dn->dn_objset->os_dsl_dataset,
399c7cd2421SGeorge Wilson dn->dn_dbuf->db_blkptr, dn->dn_dbuf->db_blkptr->blk_birth)) {
4008a2f1b91Sahrens txh->txh_space_tooverwrite += space;
4014a7f2a75SMark Maybee txh->txh_space_tounref += space;
4028a2f1b91Sahrens } else {
4038a2f1b91Sahrens txh->txh_space_towrite += space;
404a9799022Sck153898 if (dn && dn->dn_dbuf->db_blkptr)
405a9799022Sck153898 txh->txh_space_tounref += space;
406fa9e4066Sahrens }
407fa9e4066Sahrens }
408fa9e4066Sahrens
409fa9e4066Sahrens void
dmu_tx_hold_write(dmu_tx_t * tx,uint64_t object,uint64_t off,int len)410fa9e4066Sahrens dmu_tx_hold_write(dmu_tx_t *tx, uint64_t object, uint64_t off, int len)
411fa9e4066Sahrens {
4128a2f1b91Sahrens dmu_tx_hold_t *txh;
4138a2f1b91Sahrens
414fa9e4066Sahrens ASSERT(tx->tx_txg == 0);
415ea8dc4b6Seschrock ASSERT(len < DMU_MAX_ACCESS);
416dd6ef538Smaybee ASSERT(len == 0 || UINT64_MAX - off >= len - 1);
417fa9e4066Sahrens
4188a2f1b91Sahrens txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
4198a2f1b91Sahrens object, THT_WRITE, off, len);
4208a2f1b91Sahrens if (txh == NULL)
4218a2f1b91Sahrens return;
4228a2f1b91Sahrens
4238a2f1b91Sahrens dmu_tx_count_write(txh, off, len);
4248a2f1b91Sahrens dmu_tx_count_dnode(txh);
425fa9e4066Sahrens }
426fa9e4066Sahrens
427fa9e4066Sahrens static void
dmu_tx_count_free(dmu_tx_hold_t * txh,uint64_t off,uint64_t len)4288a2f1b91Sahrens dmu_tx_count_free(dmu_tx_hold_t *txh, uint64_t off, uint64_t len)
429fa9e4066Sahrens {
430cdb0ab79Smaybee uint64_t blkid, nblks, lastblk;
431cdb0ab79Smaybee uint64_t space = 0, unref = 0, skipped = 0;
4328a2f1b91Sahrens dnode_t *dn = txh->txh_dnode;
433fa9e4066Sahrens dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset;
4348a2f1b91Sahrens spa_t *spa = txh->txh_tx->tx_pool->dp_spa;
435cdb0ab79Smaybee int epbs;
43631495a1eSArne Jansen uint64_t l0span = 0, nl1blks = 0;
437fa9e4066Sahrens
438cdb0ab79Smaybee if (dn->dn_nlevels == 0)
439c543ec06Sahrens return;
440c543ec06Sahrens
441c543ec06Sahrens /*
442cdb0ab79Smaybee * The struct_rwlock protects us against dn_nlevels
443c543ec06Sahrens * changing, in case (against all odds) we manage to dirty &
444c543ec06Sahrens * sync out the changes after we check for being dirty.
44501025c89SJohn Harres * Also, dbuf_hold_impl() wants us to have the struct_rwlock.
446fa9e4066Sahrens */
447fa9e4066Sahrens rw_enter(&dn->dn_struct_rwlock, RW_READER);
448cdb0ab79Smaybee epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
449cdb0ab79Smaybee if (dn->dn_maxblkid == 0) {
450c543ec06Sahrens if (off == 0 && len >= dn->dn_datablksz) {
451c543ec06Sahrens blkid = 0;
452c543ec06Sahrens nblks = 1;
453c543ec06Sahrens } else {
454ea8dc4b6Seschrock rw_exit(&dn->dn_struct_rwlock);
455ea8dc4b6Seschrock return;
456ea8dc4b6Seschrock }
457c543ec06Sahrens } else {
458c543ec06Sahrens blkid = off >> dn->dn_datablkshift;
459cdb0ab79Smaybee nblks = (len + dn->dn_datablksz - 1) >> dn->dn_datablkshift;
460fa9e4066Sahrens
461be9000ccSMatthew Ahrens if (blkid > dn->dn_maxblkid) {
462c543ec06Sahrens rw_exit(&dn->dn_struct_rwlock);
463c543ec06Sahrens return;
464c543ec06Sahrens }
465cdb0ab79Smaybee if (blkid + nblks > dn->dn_maxblkid)
466be9000ccSMatthew Ahrens nblks = dn->dn_maxblkid - blkid + 1;
467c543ec06Sahrens
468c543ec06Sahrens }
46931495a1eSArne Jansen l0span = nblks; /* save for later use to calc level > 1 overhead */
470cdb0ab79Smaybee if (dn->dn_nlevels == 1) {
471fa9e4066Sahrens int i;
472fa9e4066Sahrens for (i = 0; i < nblks; i++) {
473fa9e4066Sahrens blkptr_t *bp = dn->dn_phys->dn_blkptr;
474cdb0ab79Smaybee ASSERT3U(blkid + i, <, dn->dn_nblkptr);
475fa9e4066Sahrens bp += blkid + i;
476c7cd2421SGeorge Wilson if (dsl_dataset_block_freeable(ds, bp, bp->blk_birth)) {
477fa9e4066Sahrens dprintf_bp(bp, "can free old%s", "");
478b24ab676SJeff Bonwick space += bp_get_dsize(spa, bp);
479fa9e4066Sahrens }
480a9799022Sck153898 unref += BP_GET_ASIZE(bp);
481fa9e4066Sahrens }
48231495a1eSArne Jansen nl1blks = 1;
483ea8dc4b6Seschrock nblks = 0;
484fa9e4066Sahrens }
485fa9e4066Sahrens
486cdb0ab79Smaybee lastblk = blkid + nblks - 1;
487fa9e4066Sahrens while (nblks) {
488fa9e4066Sahrens dmu_buf_impl_t *dbuf;
489cdb0ab79Smaybee uint64_t ibyte, new_blkid;
490cdb0ab79Smaybee int epb = 1 << epbs;
491cdb0ab79Smaybee int err, i, blkoff, tochk;
492fa9e4066Sahrens blkptr_t *bp;
493fa9e4066Sahrens
494cdb0ab79Smaybee ibyte = blkid << dn->dn_datablkshift;
495cdb0ab79Smaybee err = dnode_next_offset(dn,
496cdb0ab79Smaybee DNODE_FIND_HAVELOCK, &ibyte, 2, 1, 0);
497cdb0ab79Smaybee new_blkid = ibyte >> dn->dn_datablkshift;
498b7e50089Smaybee if (err == ESRCH) {
499b7e50089Smaybee skipped += (lastblk >> epbs) - (blkid >> epbs) + 1;
500cdb0ab79Smaybee break;
501b7e50089Smaybee }
502cdb0ab79Smaybee if (err) {
503cdb0ab79Smaybee txh->txh_tx->tx_err = err;
504cdb0ab79Smaybee break;
505cdb0ab79Smaybee }
506b7e50089Smaybee if (new_blkid > lastblk) {
507b7e50089Smaybee skipped += (lastblk >> epbs) - (blkid >> epbs) + 1;
508cdb0ab79Smaybee break;
509b7e50089Smaybee }
510cdb0ab79Smaybee
511cdb0ab79Smaybee if (new_blkid > blkid) {
512b7e50089Smaybee ASSERT((new_blkid >> epbs) > (blkid >> epbs));
513b7e50089Smaybee skipped += (new_blkid >> epbs) - (blkid >> epbs) - 1;
514cdb0ab79Smaybee nblks -= new_blkid - blkid;
515cdb0ab79Smaybee blkid = new_blkid;
516cdb0ab79Smaybee }
517cdb0ab79Smaybee blkoff = P2PHASE(blkid, epb);
518cdb0ab79Smaybee tochk = MIN(epb - blkoff, nblks);
519cdb0ab79Smaybee
520*5f9bb2f3SPaul Dagnelie err = dbuf_hold_impl(dn, 1, blkid >> epbs,
521*5f9bb2f3SPaul Dagnelie FALSE, FALSE, FTAG, &dbuf);
52201025c89SJohn Harres if (err) {
52301025c89SJohn Harres txh->txh_tx->tx_err = err;
52401025c89SJohn Harres break;
52501025c89SJohn Harres }
526cdb0ab79Smaybee
527cdb0ab79Smaybee txh->txh_memory_tohold += dbuf->db.db_size;
52877179d12SLori Alt
52977179d12SLori Alt /*
53077179d12SLori Alt * We don't check memory_tohold against DMU_MAX_ACCESS because
53177179d12SLori Alt * memory_tohold is an over-estimation (especially the >L1
53277179d12SLori Alt * indirect blocks), so it could fail. Callers should have
53377179d12SLori Alt * already verified that they will not be holding too much
53477179d12SLori Alt * memory.
53577179d12SLori Alt */
53677179d12SLori Alt
537cdb0ab79Smaybee err = dbuf_read(dbuf, NULL, DB_RF_HAVESTRUCT | DB_RF_CANFAIL);
538ea8dc4b6Seschrock if (err != 0) {
5398a2f1b91Sahrens txh->txh_tx->tx_err = err;
540ea8dc4b6Seschrock dbuf_rele(dbuf, FTAG);
541ea8dc4b6Seschrock break;
542ea8dc4b6Seschrock }
543fa9e4066Sahrens
544fa9e4066Sahrens bp = dbuf->db.db_data;
545fa9e4066Sahrens bp += blkoff;
546fa9e4066Sahrens
547fa9e4066Sahrens for (i = 0; i < tochk; i++) {
548c7cd2421SGeorge Wilson if (dsl_dataset_block_freeable(ds, &bp[i],
549c7cd2421SGeorge Wilson bp[i].blk_birth)) {
550cdb0ab79Smaybee dprintf_bp(&bp[i], "can free old%s", "");
551b24ab676SJeff Bonwick space += bp_get_dsize(spa, &bp[i]);
552fa9e4066Sahrens }
553a9799022Sck153898 unref += BP_GET_ASIZE(bp);
554fa9e4066Sahrens }
555ea8dc4b6Seschrock dbuf_rele(dbuf, FTAG);
556fa9e4066Sahrens
55731495a1eSArne Jansen ++nl1blks;
558fa9e4066Sahrens blkid += tochk;
559fa9e4066Sahrens nblks -= tochk;
560fa9e4066Sahrens }
561fa9e4066Sahrens rw_exit(&dn->dn_struct_rwlock);
562fa9e4066Sahrens
56331495a1eSArne Jansen /*
56431495a1eSArne Jansen * Add in memory requirements of higher-level indirects.
56531495a1eSArne Jansen * This assumes a worst-possible scenario for dn_nlevels and a
56631495a1eSArne Jansen * worst-possible distribution of l1-blocks over the region to free.
56731495a1eSArne Jansen */
56831495a1eSArne Jansen {
56931495a1eSArne Jansen uint64_t blkcnt = 1 + ((l0span >> epbs) >> epbs);
57031495a1eSArne Jansen int level = 2;
57131495a1eSArne Jansen /*
57231495a1eSArne Jansen * Here we don't use DN_MAX_LEVEL, but calculate it with the
57331495a1eSArne Jansen * given datablkshift and indblkshift. This makes the
57431495a1eSArne Jansen * difference between 19 and 8 on large files.
57531495a1eSArne Jansen */
57631495a1eSArne Jansen int maxlevel = 2 + (DN_MAX_OFFSET_SHIFT - dn->dn_datablkshift) /
57731495a1eSArne Jansen (dn->dn_indblkshift - SPA_BLKPTRSHIFT);
57831495a1eSArne Jansen
57931495a1eSArne Jansen while (level++ < maxlevel) {
5808f0b538dSChristopher Siden txh->txh_memory_tohold += MAX(MIN(blkcnt, nl1blks), 1)
58131495a1eSArne Jansen << dn->dn_indblkshift;
58231495a1eSArne Jansen blkcnt = 1 + (blkcnt >> epbs);
58331495a1eSArne Jansen }
58431495a1eSArne Jansen }
58531495a1eSArne Jansen
586cdb0ab79Smaybee /* account for new level 1 indirect blocks that might show up */
587b7e50089Smaybee if (skipped > 0) {
588715614a4Smaybee txh->txh_fudge += skipped << dn->dn_indblkshift;
589cdb0ab79Smaybee skipped = MIN(skipped, DMU_MAX_DELETEBLKCNT >> epbs);
590cdb0ab79Smaybee txh->txh_memory_tohold += skipped << dn->dn_indblkshift;
591cdb0ab79Smaybee }
5928a2f1b91Sahrens txh->txh_space_tofree += space;
593a9799022Sck153898 txh->txh_space_tounref += unref;
594fa9e4066Sahrens }
595fa9e4066Sahrens
596c34b74fdSMatthew Ahrens /*
597c34b74fdSMatthew Ahrens * This function marks the transaction as being a "net free". The end
598c34b74fdSMatthew Ahrens * result is that refquotas will be disabled for this transaction, and
599c34b74fdSMatthew Ahrens * this transaction will be able to use half of the pool space overhead
600c34b74fdSMatthew Ahrens * (see dsl_pool_adjustedsize()). Therefore this function should only
601c34b74fdSMatthew Ahrens * be called for transactions that we expect will not cause a net increase
602c34b74fdSMatthew Ahrens * in the amount of space used (but it's OK if that is occasionally not true).
603c34b74fdSMatthew Ahrens */
604c34b74fdSMatthew Ahrens void
dmu_tx_mark_netfree(dmu_tx_t * tx)605c34b74fdSMatthew Ahrens dmu_tx_mark_netfree(dmu_tx_t *tx)
606c34b74fdSMatthew Ahrens {
607c34b74fdSMatthew Ahrens dmu_tx_hold_t *txh;
608c34b74fdSMatthew Ahrens
609c34b74fdSMatthew Ahrens txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
610c34b74fdSMatthew Ahrens DMU_NEW_OBJECT, THT_FREE, 0, 0);
611c34b74fdSMatthew Ahrens
612c34b74fdSMatthew Ahrens /*
613c34b74fdSMatthew Ahrens * Pretend that this operation will free 1GB of space. This
614c34b74fdSMatthew Ahrens * should be large enough to cancel out the largest write.
615c34b74fdSMatthew Ahrens * We don't want to use something like UINT64_MAX, because that would
616c34b74fdSMatthew Ahrens * cause overflows when doing math with these values (e.g. in
617c34b74fdSMatthew Ahrens * dmu_tx_try_assign()).
618c34b74fdSMatthew Ahrens */
619c34b74fdSMatthew Ahrens txh->txh_space_tofree = txh->txh_space_tounref = 1024 * 1024 * 1024;
620c34b74fdSMatthew Ahrens }
621c34b74fdSMatthew Ahrens
6228a2f1b91Sahrens void
dmu_tx_hold_free(dmu_tx_t * tx,uint64_t object,uint64_t off,uint64_t len)6238a2f1b91Sahrens dmu_tx_hold_free(dmu_tx_t *tx, uint64_t object, uint64_t off, uint64_t len)
624fa9e4066Sahrens {
6258a2f1b91Sahrens dmu_tx_hold_t *txh;
6268a2f1b91Sahrens dnode_t *dn;
6272f3d8780SMatthew Ahrens int err;
628ea8dc4b6Seschrock zio_t *zio;
629fa9e4066Sahrens
6308a2f1b91Sahrens ASSERT(tx->tx_txg == 0);
6318a2f1b91Sahrens
6328a2f1b91Sahrens txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
6338a2f1b91Sahrens object, THT_FREE, off, len);
6348a2f1b91Sahrens if (txh == NULL)
6358a2f1b91Sahrens return;
6368a2f1b91Sahrens dn = txh->txh_dnode;
63769962b56SMatthew Ahrens dmu_tx_count_dnode(txh);
6388a2f1b91Sahrens
639fa9e4066Sahrens if (off >= (dn->dn_maxblkid+1) * dn->dn_datablksz)
640fa9e4066Sahrens return;
641fa9e4066Sahrens if (len == DMU_OBJECT_END)
642fa9e4066Sahrens len = (dn->dn_maxblkid+1) * dn->dn_datablksz - off;
643fa9e4066Sahrens
644ea8dc4b6Seschrock /*
6452f3d8780SMatthew Ahrens * For i/o error checking, we read the first and last level-0
6462f3d8780SMatthew Ahrens * blocks if they are not aligned, and all the level-1 blocks.
6472f3d8780SMatthew Ahrens *
6482f3d8780SMatthew Ahrens * Note: dbuf_free_range() assumes that we have not instantiated
6492f3d8780SMatthew Ahrens * any level-0 dbufs that will be completely freed. Therefore we must
6502f3d8780SMatthew Ahrens * exercise care to not read or count the first and last blocks
6512f3d8780SMatthew Ahrens * if they are blocksize-aligned.
6522f3d8780SMatthew Ahrens */
6532f3d8780SMatthew Ahrens if (dn->dn_datablkshift == 0) {
654713d6c20SMatthew Ahrens if (off != 0 || len < dn->dn_datablksz)
6555253393bSMatthew Ahrens dmu_tx_count_write(txh, 0, dn->dn_datablksz);
6562f3d8780SMatthew Ahrens } else {
6572f3d8780SMatthew Ahrens /* first block will be modified if it is not aligned */
6582f3d8780SMatthew Ahrens if (!IS_P2ALIGNED(off, 1 << dn->dn_datablkshift))
6592f3d8780SMatthew Ahrens dmu_tx_count_write(txh, off, 1);
6602f3d8780SMatthew Ahrens /* last block will be modified if it is not aligned */
6612f3d8780SMatthew Ahrens if (!IS_P2ALIGNED(off + len, 1 << dn->dn_datablkshift))
6622f3d8780SMatthew Ahrens dmu_tx_count_write(txh, off+len, 1);
6632f3d8780SMatthew Ahrens }
6642f3d8780SMatthew Ahrens
6652f3d8780SMatthew Ahrens /*
6662f3d8780SMatthew Ahrens * Check level-1 blocks.
667ea8dc4b6Seschrock */
66898572ac1Sahrens if (dn->dn_nlevels > 1) {
6692f3d8780SMatthew Ahrens int shift = dn->dn_datablkshift + dn->dn_indblkshift -
67098572ac1Sahrens SPA_BLKPTRSHIFT;
6712f3d8780SMatthew Ahrens uint64_t start = off >> shift;
6722f3d8780SMatthew Ahrens uint64_t end = (off + len) >> shift;
6732f3d8780SMatthew Ahrens
6742f3d8780SMatthew Ahrens ASSERT(dn->dn_indblkshift != 0);
675ea8dc4b6Seschrock
6769b072031SMatthew Ahrens /*
6779b072031SMatthew Ahrens * dnode_reallocate() can result in an object with indirect
6789b072031SMatthew Ahrens * blocks having an odd data block size. In this case,
6799b072031SMatthew Ahrens * just check the single block.
6809b072031SMatthew Ahrens */
6819b072031SMatthew Ahrens if (dn->dn_datablkshift == 0)
6829b072031SMatthew Ahrens start = end = 0;
6839b072031SMatthew Ahrens
68498572ac1Sahrens zio = zio_root(tx->tx_pool->dp_spa,
68598572ac1Sahrens NULL, NULL, ZIO_FLAG_CANFAIL);
6862f3d8780SMatthew Ahrens for (uint64_t i = start; i <= end; i++) {
687ea8dc4b6Seschrock uint64_t ibyte = i << shift;
688cdb0ab79Smaybee err = dnode_next_offset(dn, 0, &ibyte, 2, 1, 0);
689ea8dc4b6Seschrock i = ibyte >> shift;
690238fb8bbSMatthew Ahrens if (err == ESRCH || i > end)
691ea8dc4b6Seschrock break;
692ea8dc4b6Seschrock if (err) {
693ea8dc4b6Seschrock tx->tx_err = err;
694ea8dc4b6Seschrock return;
695ea8dc4b6Seschrock }
696ea8dc4b6Seschrock
697ea8dc4b6Seschrock err = dmu_tx_check_ioerr(zio, dn, 1, i);
698ea8dc4b6Seschrock if (err) {
699ea8dc4b6Seschrock tx->tx_err = err;
700ea8dc4b6Seschrock return;
701ea8dc4b6Seschrock }
702ea8dc4b6Seschrock }
703ea8dc4b6Seschrock err = zio_wait(zio);
704ea8dc4b6Seschrock if (err) {
705ea8dc4b6Seschrock tx->tx_err = err;
706ea8dc4b6Seschrock return;
707ea8dc4b6Seschrock }
70898572ac1Sahrens }
709ea8dc4b6Seschrock
7108a2f1b91Sahrens dmu_tx_count_free(txh, off, len);
711fa9e4066Sahrens }
712fa9e4066Sahrens
713fa9e4066Sahrens void
dmu_tx_hold_zap(dmu_tx_t * tx,uint64_t object,int add,const char * name)71414843421SMatthew Ahrens dmu_tx_hold_zap(dmu_tx_t *tx, uint64_t object, int add, const char *name)
715fa9e4066Sahrens {
7168a2f1b91Sahrens dmu_tx_hold_t *txh;
7178a2f1b91Sahrens dnode_t *dn;
7188d62b223SJustin T. Gibbs dsl_dataset_phys_t *ds_phys;
719fa9e4066Sahrens uint64_t nblocks;
720ea8dc4b6Seschrock int epbs, err;
721fa9e4066Sahrens
7228a2f1b91Sahrens ASSERT(tx->tx_txg == 0);
7238a2f1b91Sahrens
7248a2f1b91Sahrens txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
7258a2f1b91Sahrens object, THT_ZAP, add, (uintptr_t)name);
7268a2f1b91Sahrens if (txh == NULL)
7278a2f1b91Sahrens return;
7288a2f1b91Sahrens dn = txh->txh_dnode;
7298a2f1b91Sahrens
7308a2f1b91Sahrens dmu_tx_count_dnode(txh);
731fa9e4066Sahrens
732fa9e4066Sahrens if (dn == NULL) {
733fa9e4066Sahrens /*
734ea8dc4b6Seschrock * We will be able to fit a new object's entries into one leaf
735fa9e4066Sahrens * block. So there will be at most 2 blocks total,
736fa9e4066Sahrens * including the header block.
737fa9e4066Sahrens */
7388a2f1b91Sahrens dmu_tx_count_write(txh, 0, 2 << fzap_default_block_shift);
739fa9e4066Sahrens return;
740fa9e4066Sahrens }
741fa9e4066Sahrens
742ad135b5dSChristopher Siden ASSERT3P(DMU_OT_BYTESWAP(dn->dn_type), ==, DMU_BSWAP_ZAP);
743fa9e4066Sahrens
744ea8dc4b6Seschrock if (dn->dn_maxblkid == 0 && !add) {
7459dccfd2aSAlbert Lee blkptr_t *bp;
7469dccfd2aSAlbert Lee
747fa9e4066Sahrens /*
748fa9e4066Sahrens * If there is only one block (i.e. this is a micro-zap)
749ea8dc4b6Seschrock * and we are not adding anything, the accounting is simple.
750fa9e4066Sahrens */
751ea8dc4b6Seschrock err = dmu_tx_check_ioerr(NULL, dn, 0, 0);
752ea8dc4b6Seschrock if (err) {
753ea8dc4b6Seschrock tx->tx_err = err;
754ea8dc4b6Seschrock return;
755ea8dc4b6Seschrock }
756ea8dc4b6Seschrock
757b6130eadSmaybee /*
758b6130eadSmaybee * Use max block size here, since we don't know how much
759b6130eadSmaybee * the size will change between now and the dbuf dirty call.
760b6130eadSmaybee */
7619dccfd2aSAlbert Lee bp = &dn->dn_phys->dn_blkptr[0];
762fa9e4066Sahrens if (dsl_dataset_block_freeable(dn->dn_objset->os_dsl_dataset,
7639dccfd2aSAlbert Lee bp, bp->blk_birth))
764d1a98260SMatthew Ahrens txh->txh_space_tooverwrite += MZAP_MAX_BLKSZ;
7659dccfd2aSAlbert Lee else
766d1a98260SMatthew Ahrens txh->txh_space_towrite += MZAP_MAX_BLKSZ;
7679dccfd2aSAlbert Lee if (!BP_IS_HOLE(bp))
768d1a98260SMatthew Ahrens txh->txh_space_tounref += MZAP_MAX_BLKSZ;
769fa9e4066Sahrens return;
770fa9e4066Sahrens }
771fa9e4066Sahrens
772ea8dc4b6Seschrock if (dn->dn_maxblkid > 0 && name) {
773fa9e4066Sahrens /*
774ea8dc4b6Seschrock * access the name in this fat-zap so that we'll check
775ea8dc4b6Seschrock * for i/o errors to the leaf blocks, etc.
776ea8dc4b6Seschrock */
777503ad85cSMatthew Ahrens err = zap_lookup(dn->dn_objset, dn->dn_object, name,
778ea8dc4b6Seschrock 8, 0, NULL);
779ea8dc4b6Seschrock if (err == EIO) {
780ea8dc4b6Seschrock tx->tx_err = err;
781ea8dc4b6Seschrock return;
782ea8dc4b6Seschrock }
783ea8dc4b6Seschrock }
784ea8dc4b6Seschrock
785503ad85cSMatthew Ahrens err = zap_count_write(dn->dn_objset, dn->dn_object, name, add,
786720d1aa1SSanjeev Bagewadi &txh->txh_space_towrite, &txh->txh_space_tooverwrite);
787fa9e4066Sahrens
788fa9e4066Sahrens /*
789fa9e4066Sahrens * If the modified blocks are scattered to the four winds,
790fa9e4066Sahrens * we'll have to modify an indirect twig for each.
791fa9e4066Sahrens */
792fa9e4066Sahrens epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
7938d62b223SJustin T. Gibbs ds_phys = dsl_dataset_phys(dn->dn_objset->os_dsl_dataset);
794fa9e4066Sahrens for (nblocks = dn->dn_maxblkid >> epbs; nblocks != 0; nblocks >>= epbs)
7958d62b223SJustin T. Gibbs if (ds_phys->ds_prev_snap_obj)
7968a2f1b91Sahrens txh->txh_space_towrite += 3 << dn->dn_indblkshift;
7973d692628SSanjeev Bagewadi else
7983d692628SSanjeev Bagewadi txh->txh_space_tooverwrite += 3 << dn->dn_indblkshift;
799fa9e4066Sahrens }
800fa9e4066Sahrens
801fa9e4066Sahrens void
dmu_tx_hold_bonus(dmu_tx_t * tx,uint64_t object)802fa9e4066Sahrens dmu_tx_hold_bonus(dmu_tx_t *tx, uint64_t object)
803fa9e4066Sahrens {
8048a2f1b91Sahrens dmu_tx_hold_t *txh;
8058a2f1b91Sahrens
806fa9e4066Sahrens ASSERT(tx->tx_txg == 0);
807fa9e4066Sahrens
8088a2f1b91Sahrens txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
8098a2f1b91Sahrens object, THT_BONUS, 0, 0);
8108a2f1b91Sahrens if (txh)
8118a2f1b91Sahrens dmu_tx_count_dnode(txh);
812fa9e4066Sahrens }
813fa9e4066Sahrens
814fa9e4066Sahrens void
dmu_tx_hold_space(dmu_tx_t * tx,uint64_t space)815fa9e4066Sahrens dmu_tx_hold_space(dmu_tx_t *tx, uint64_t space)
816fa9e4066Sahrens {
8178a2f1b91Sahrens dmu_tx_hold_t *txh;
818fa9e4066Sahrens ASSERT(tx->tx_txg == 0);
819fa9e4066Sahrens
8208a2f1b91Sahrens txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
8218a2f1b91Sahrens DMU_NEW_OBJECT, THT_SPACE, space, 0);
8228a2f1b91Sahrens
8238a2f1b91Sahrens txh->txh_space_towrite += space;
824fa9e4066Sahrens }
825fa9e4066Sahrens
826fa9e4066Sahrens int
dmu_tx_holds(dmu_tx_t * tx,uint64_t object)827fa9e4066Sahrens dmu_tx_holds(dmu_tx_t *tx, uint64_t object)
828fa9e4066Sahrens {
8298a2f1b91Sahrens dmu_tx_hold_t *txh;
830fa9e4066Sahrens int holds = 0;
831fa9e4066Sahrens
832fa9e4066Sahrens /*
833fa9e4066Sahrens * By asserting that the tx is assigned, we're counting the
834fa9e4066Sahrens * number of dn_tx_holds, which is the same as the number of
835fa9e4066Sahrens * dn_holds. Otherwise, we'd be counting dn_holds, but
836fa9e4066Sahrens * dn_tx_holds could be 0.
837fa9e4066Sahrens */
838fa9e4066Sahrens ASSERT(tx->tx_txg != 0);
839fa9e4066Sahrens
840fa9e4066Sahrens /* if (tx->tx_anyobj == TRUE) */
841fa9e4066Sahrens /* return (0); */
842fa9e4066Sahrens
8438a2f1b91Sahrens for (txh = list_head(&tx->tx_holds); txh;
8448a2f1b91Sahrens txh = list_next(&tx->tx_holds, txh)) {
8458a2f1b91Sahrens if (txh->txh_dnode && txh->txh_dnode->dn_object == object)
846fa9e4066Sahrens holds++;
847fa9e4066Sahrens }
848fa9e4066Sahrens
849fa9e4066Sahrens return (holds);
850fa9e4066Sahrens }
851fa9e4066Sahrens
8529c9dc39aSek110237 #ifdef ZFS_DEBUG
853fa9e4066Sahrens void
dmu_tx_dirty_buf(dmu_tx_t * tx,dmu_buf_impl_t * db)854fa9e4066Sahrens dmu_tx_dirty_buf(dmu_tx_t *tx, dmu_buf_impl_t *db)
855fa9e4066Sahrens {
8568a2f1b91Sahrens dmu_tx_hold_t *txh;
857fa9e4066Sahrens int match_object = FALSE, match_offset = FALSE;
858744947dcSTom Erickson dnode_t *dn;
859fa9e4066Sahrens
860744947dcSTom Erickson DB_DNODE_ENTER(db);
861744947dcSTom Erickson dn = DB_DNODE(db);
862fa9e4066Sahrens ASSERT(tx->tx_txg != 0);
863503ad85cSMatthew Ahrens ASSERT(tx->tx_objset == NULL || dn->dn_objset == tx->tx_objset);
864fa9e4066Sahrens ASSERT3U(dn->dn_object, ==, db->db.db_object);
865fa9e4066Sahrens
866744947dcSTom Erickson if (tx->tx_anyobj) {
867744947dcSTom Erickson DB_DNODE_EXIT(db);
868fa9e4066Sahrens return;
869744947dcSTom Erickson }
870fa9e4066Sahrens
871fa9e4066Sahrens /* XXX No checking on the meta dnode for now */
872744947dcSTom Erickson if (db->db.db_object == DMU_META_DNODE_OBJECT) {
873744947dcSTom Erickson DB_DNODE_EXIT(db);
874fa9e4066Sahrens return;
875744947dcSTom Erickson }
876fa9e4066Sahrens
8778a2f1b91Sahrens for (txh = list_head(&tx->tx_holds); txh;
8788a2f1b91Sahrens txh = list_next(&tx->tx_holds, txh)) {
879fa9e4066Sahrens ASSERT(dn == NULL || dn->dn_assigned_txg == tx->tx_txg);
8808a2f1b91Sahrens if (txh->txh_dnode == dn && txh->txh_type != THT_NEWOBJECT)
881fa9e4066Sahrens match_object = TRUE;
8828a2f1b91Sahrens if (txh->txh_dnode == NULL || txh->txh_dnode == dn) {
883fa9e4066Sahrens int datablkshift = dn->dn_datablkshift ?
884fa9e4066Sahrens dn->dn_datablkshift : SPA_MAXBLOCKSHIFT;
885fa9e4066Sahrens int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
886fa9e4066Sahrens int shift = datablkshift + epbs * db->db_level;
887fa9e4066Sahrens uint64_t beginblk = shift >= 64 ? 0 :
8888a2f1b91Sahrens (txh->txh_arg1 >> shift);
889fa9e4066Sahrens uint64_t endblk = shift >= 64 ? 0 :
8908a2f1b91Sahrens ((txh->txh_arg1 + txh->txh_arg2 - 1) >> shift);
891fa9e4066Sahrens uint64_t blkid = db->db_blkid;
892fa9e4066Sahrens
8938a2f1b91Sahrens /* XXX txh_arg2 better not be zero... */
894fa9e4066Sahrens
8958a2f1b91Sahrens dprintf("found txh type %x beginblk=%llx endblk=%llx\n",
8968a2f1b91Sahrens txh->txh_type, beginblk, endblk);
897fa9e4066Sahrens
8988a2f1b91Sahrens switch (txh->txh_type) {
899fa9e4066Sahrens case THT_WRITE:
900fa9e4066Sahrens if (blkid >= beginblk && blkid <= endblk)
901fa9e4066Sahrens match_offset = TRUE;
902fa9e4066Sahrens /*
903fa9e4066Sahrens * We will let this hold work for the bonus
9040a586ceaSMark Shellenbaum * or spill buffer so that we don't need to
9050a586ceaSMark Shellenbaum * hold it when creating a new object.
906fa9e4066Sahrens */
9070a586ceaSMark Shellenbaum if (blkid == DMU_BONUS_BLKID ||
9080a586ceaSMark Shellenbaum blkid == DMU_SPILL_BLKID)
909fa9e4066Sahrens match_offset = TRUE;
910fa9e4066Sahrens /*
911fa9e4066Sahrens * They might have to increase nlevels,
912fa9e4066Sahrens * thus dirtying the new TLIBs. Or the
913fa9e4066Sahrens * might have to change the block size,
914fa9e4066Sahrens * thus dirying the new lvl=0 blk=0.
915fa9e4066Sahrens */
916fa9e4066Sahrens if (blkid == 0)
917fa9e4066Sahrens match_offset = TRUE;
918fa9e4066Sahrens break;
919fa9e4066Sahrens case THT_FREE:
920cdb0ab79Smaybee /*
921cdb0ab79Smaybee * We will dirty all the level 1 blocks in
922cdb0ab79Smaybee * the free range and perhaps the first and
923cdb0ab79Smaybee * last level 0 block.
924cdb0ab79Smaybee */
925cdb0ab79Smaybee if (blkid >= beginblk && (blkid <= endblk ||
926cdb0ab79Smaybee txh->txh_arg2 == DMU_OBJECT_END))
927fa9e4066Sahrens match_offset = TRUE;
928fa9e4066Sahrens break;
9290a586ceaSMark Shellenbaum case THT_SPILL:
9300a586ceaSMark Shellenbaum if (blkid == DMU_SPILL_BLKID)
9310a586ceaSMark Shellenbaum match_offset = TRUE;
9320a586ceaSMark Shellenbaum break;
933fa9e4066Sahrens case THT_BONUS:
9340a586ceaSMark Shellenbaum if (blkid == DMU_BONUS_BLKID)
935fa9e4066Sahrens match_offset = TRUE;
936fa9e4066Sahrens break;
937fa9e4066Sahrens case THT_ZAP:
938fa9e4066Sahrens match_offset = TRUE;
939fa9e4066Sahrens break;
940fa9e4066Sahrens case THT_NEWOBJECT:
941fa9e4066Sahrens match_object = TRUE;
942fa9e4066Sahrens break;
943fa9e4066Sahrens default:
9448a2f1b91Sahrens ASSERT(!"bad txh_type");
945fa9e4066Sahrens }
946fa9e4066Sahrens }
947744947dcSTom Erickson if (match_object && match_offset) {
948744947dcSTom Erickson DB_DNODE_EXIT(db);
949fa9e4066Sahrens return;
950fa9e4066Sahrens }
951744947dcSTom Erickson }
952744947dcSTom Erickson DB_DNODE_EXIT(db);
953fa9e4066Sahrens panic("dirtying dbuf obj=%llx lvl=%u blkid=%llx but not tx_held\n",
954fa9e4066Sahrens (u_longlong_t)db->db.db_object, db->db_level,
955fa9e4066Sahrens (u_longlong_t)db->db_blkid);
956fa9e4066Sahrens }
9579c9dc39aSek110237 #endif
958fa9e4066Sahrens
95969962b56SMatthew Ahrens /*
96069962b56SMatthew Ahrens * If we can't do 10 iops, something is wrong. Let us go ahead
96169962b56SMatthew Ahrens * and hit zfs_dirty_data_max.
96269962b56SMatthew Ahrens */
96369962b56SMatthew Ahrens hrtime_t zfs_delay_max_ns = MSEC2NSEC(100);
96469962b56SMatthew Ahrens int zfs_delay_resolution_ns = 100 * 1000; /* 100 microseconds */
96569962b56SMatthew Ahrens
96669962b56SMatthew Ahrens /*
96769962b56SMatthew Ahrens * We delay transactions when we've determined that the backend storage
96869962b56SMatthew Ahrens * isn't able to accommodate the rate of incoming writes.
96969962b56SMatthew Ahrens *
97069962b56SMatthew Ahrens * If there is already a transaction waiting, we delay relative to when
97169962b56SMatthew Ahrens * that transaction finishes waiting. This way the calculated min_time
97269962b56SMatthew Ahrens * is independent of the number of threads concurrently executing
97369962b56SMatthew Ahrens * transactions.
97469962b56SMatthew Ahrens *
97569962b56SMatthew Ahrens * If we are the only waiter, wait relative to when the transaction
97669962b56SMatthew Ahrens * started, rather than the current time. This credits the transaction for
97769962b56SMatthew Ahrens * "time already served", e.g. reading indirect blocks.
97869962b56SMatthew Ahrens *
97969962b56SMatthew Ahrens * The minimum time for a transaction to take is calculated as:
98069962b56SMatthew Ahrens * min_time = scale * (dirty - min) / (max - dirty)
98169962b56SMatthew Ahrens * min_time is then capped at zfs_delay_max_ns.
98269962b56SMatthew Ahrens *
98369962b56SMatthew Ahrens * The delay has two degrees of freedom that can be adjusted via tunables.
98469962b56SMatthew Ahrens * The percentage of dirty data at which we start to delay is defined by
98569962b56SMatthew Ahrens * zfs_delay_min_dirty_percent. This should typically be at or above
98669962b56SMatthew Ahrens * zfs_vdev_async_write_active_max_dirty_percent so that we only start to
98769962b56SMatthew Ahrens * delay after writing at full speed has failed to keep up with the incoming
98869962b56SMatthew Ahrens * write rate. The scale of the curve is defined by zfs_delay_scale. Roughly
98969962b56SMatthew Ahrens * speaking, this variable determines the amount of delay at the midpoint of
99069962b56SMatthew Ahrens * the curve.
99169962b56SMatthew Ahrens *
99269962b56SMatthew Ahrens * delay
99369962b56SMatthew Ahrens * 10ms +-------------------------------------------------------------*+
99469962b56SMatthew Ahrens * | *|
99569962b56SMatthew Ahrens * 9ms + *+
99669962b56SMatthew Ahrens * | *|
99769962b56SMatthew Ahrens * 8ms + *+
99869962b56SMatthew Ahrens * | * |
99969962b56SMatthew Ahrens * 7ms + * +
100069962b56SMatthew Ahrens * | * |
100169962b56SMatthew Ahrens * 6ms + * +
100269962b56SMatthew Ahrens * | * |
100369962b56SMatthew Ahrens * 5ms + * +
100469962b56SMatthew Ahrens * | * |
100569962b56SMatthew Ahrens * 4ms + * +
100669962b56SMatthew Ahrens * | * |
100769962b56SMatthew Ahrens * 3ms + * +
100869962b56SMatthew Ahrens * | * |
100969962b56SMatthew Ahrens * 2ms + (midpoint) * +
101069962b56SMatthew Ahrens * | | ** |
101169962b56SMatthew Ahrens * 1ms + v *** +
101269962b56SMatthew Ahrens * | zfs_delay_scale ----------> ******** |
101369962b56SMatthew Ahrens * 0 +-------------------------------------*********----------------+
101469962b56SMatthew Ahrens * 0% <- zfs_dirty_data_max -> 100%
101569962b56SMatthew Ahrens *
101669962b56SMatthew Ahrens * Note that since the delay is added to the outstanding time remaining on the
101769962b56SMatthew Ahrens * most recent transaction, the delay is effectively the inverse of IOPS.
101869962b56SMatthew Ahrens * Here the midpoint of 500us translates to 2000 IOPS. The shape of the curve
101969962b56SMatthew Ahrens * was chosen such that small changes in the amount of accumulated dirty data
102069962b56SMatthew Ahrens * in the first 3/4 of the curve yield relatively small differences in the
102169962b56SMatthew Ahrens * amount of delay.
102269962b56SMatthew Ahrens *
102369962b56SMatthew Ahrens * The effects can be easier to understand when the amount of delay is
102469962b56SMatthew Ahrens * represented on a log scale:
102569962b56SMatthew Ahrens *
102669962b56SMatthew Ahrens * delay
102769962b56SMatthew Ahrens * 100ms +-------------------------------------------------------------++
102869962b56SMatthew Ahrens * + +
102969962b56SMatthew Ahrens * | |
103069962b56SMatthew Ahrens * + *+
103169962b56SMatthew Ahrens * 10ms + *+
103269962b56SMatthew Ahrens * + ** +
103369962b56SMatthew Ahrens * | (midpoint) ** |
103469962b56SMatthew Ahrens * + | ** +
103569962b56SMatthew Ahrens * 1ms + v **** +
103669962b56SMatthew Ahrens * + zfs_delay_scale ----------> ***** +
103769962b56SMatthew Ahrens * | **** |
103869962b56SMatthew Ahrens * + **** +
103969962b56SMatthew Ahrens * 100us + ** +
104069962b56SMatthew Ahrens * + * +
104169962b56SMatthew Ahrens * | * |
104269962b56SMatthew Ahrens * + * +
104369962b56SMatthew Ahrens * 10us + * +
104469962b56SMatthew Ahrens * + +
104569962b56SMatthew Ahrens * | |
104669962b56SMatthew Ahrens * + +
104769962b56SMatthew Ahrens * +--------------------------------------------------------------+
104869962b56SMatthew Ahrens * 0% <- zfs_dirty_data_max -> 100%
104969962b56SMatthew Ahrens *
105069962b56SMatthew Ahrens * Note here that only as the amount of dirty data approaches its limit does
105169962b56SMatthew Ahrens * the delay start to increase rapidly. The goal of a properly tuned system
105269962b56SMatthew Ahrens * should be to keep the amount of dirty data out of that range by first
105369962b56SMatthew Ahrens * ensuring that the appropriate limits are set for the I/O scheduler to reach
105469962b56SMatthew Ahrens * optimal throughput on the backend storage, and then by changing the value
105569962b56SMatthew Ahrens * of zfs_delay_scale to increase the steepness of the curve.
105669962b56SMatthew Ahrens */
105769962b56SMatthew Ahrens static void
dmu_tx_delay(dmu_tx_t * tx,uint64_t dirty)105869962b56SMatthew Ahrens dmu_tx_delay(dmu_tx_t *tx, uint64_t dirty)
105969962b56SMatthew Ahrens {
106069962b56SMatthew Ahrens dsl_pool_t *dp = tx->tx_pool;
106169962b56SMatthew Ahrens uint64_t delay_min_bytes =
106269962b56SMatthew Ahrens zfs_dirty_data_max * zfs_delay_min_dirty_percent / 100;
106369962b56SMatthew Ahrens hrtime_t wakeup, min_tx_time, now;
106469962b56SMatthew Ahrens
106569962b56SMatthew Ahrens if (dirty <= delay_min_bytes)
106669962b56SMatthew Ahrens return;
106769962b56SMatthew Ahrens
106869962b56SMatthew Ahrens /*
106969962b56SMatthew Ahrens * The caller has already waited until we are under the max.
107069962b56SMatthew Ahrens * We make them pass us the amount of dirty data so we don't
107169962b56SMatthew Ahrens * have to handle the case of it being >= the max, which could
107269962b56SMatthew Ahrens * cause a divide-by-zero if it's == the max.
107369962b56SMatthew Ahrens */
107469962b56SMatthew Ahrens ASSERT3U(dirty, <, zfs_dirty_data_max);
107569962b56SMatthew Ahrens
107669962b56SMatthew Ahrens now = gethrtime();
107769962b56SMatthew Ahrens min_tx_time = zfs_delay_scale *
107869962b56SMatthew Ahrens (dirty - delay_min_bytes) / (zfs_dirty_data_max - dirty);
107969962b56SMatthew Ahrens if (now > tx->tx_start + min_tx_time)
108069962b56SMatthew Ahrens return;
108169962b56SMatthew Ahrens
108269962b56SMatthew Ahrens min_tx_time = MIN(min_tx_time, zfs_delay_max_ns);
108369962b56SMatthew Ahrens
108469962b56SMatthew Ahrens DTRACE_PROBE3(delay__mintime, dmu_tx_t *, tx, uint64_t, dirty,
108569962b56SMatthew Ahrens uint64_t, min_tx_time);
108669962b56SMatthew Ahrens
108769962b56SMatthew Ahrens mutex_enter(&dp->dp_lock);
108869962b56SMatthew Ahrens wakeup = MAX(tx->tx_start + min_tx_time,
108969962b56SMatthew Ahrens dp->dp_last_wakeup + min_tx_time);
109069962b56SMatthew Ahrens dp->dp_last_wakeup = wakeup;
109169962b56SMatthew Ahrens mutex_exit(&dp->dp_lock);
109269962b56SMatthew Ahrens
109369962b56SMatthew Ahrens #ifdef _KERNEL
109469962b56SMatthew Ahrens mutex_enter(&curthread->t_delay_lock);
109569962b56SMatthew Ahrens while (cv_timedwait_hires(&curthread->t_delay_cv,
109669962b56SMatthew Ahrens &curthread->t_delay_lock, wakeup, zfs_delay_resolution_ns,
109769962b56SMatthew Ahrens CALLOUT_FLAG_ABSOLUTE | CALLOUT_FLAG_ROUNDUP) > 0)
109869962b56SMatthew Ahrens continue;
109969962b56SMatthew Ahrens mutex_exit(&curthread->t_delay_lock);
110069962b56SMatthew Ahrens #else
110169962b56SMatthew Ahrens hrtime_t delta = wakeup - gethrtime();
110269962b56SMatthew Ahrens struct timespec ts;
110369962b56SMatthew Ahrens ts.tv_sec = delta / NANOSEC;
110469962b56SMatthew Ahrens ts.tv_nsec = delta % NANOSEC;
110569962b56SMatthew Ahrens (void) nanosleep(&ts, NULL);
110669962b56SMatthew Ahrens #endif
110769962b56SMatthew Ahrens }
110869962b56SMatthew Ahrens
1109fa9e4066Sahrens static int
dmu_tx_try_assign(dmu_tx_t * tx,txg_how_t txg_how)11103b2aab18SMatthew Ahrens dmu_tx_try_assign(dmu_tx_t *tx, txg_how_t txg_how)
1111fa9e4066Sahrens {
11128a2f1b91Sahrens dmu_tx_hold_t *txh;
11130a4e9518Sgw25295 spa_t *spa = tx->tx_pool->dp_spa;
1114cdb0ab79Smaybee uint64_t memory, asize, fsize, usize;
1115715614a4Smaybee uint64_t towrite, tofree, tooverwrite, tounref, tohold, fudge;
1116fa9e4066Sahrens
1117fb09f5aaSMadhav Suresh ASSERT0(tx->tx_txg);
11180a4e9518Sgw25295
1119ea8dc4b6Seschrock if (tx->tx_err)
1120ea8dc4b6Seschrock return (tx->tx_err);
1121fa9e4066Sahrens
1122e14bb325SJeff Bonwick if (spa_suspended(spa)) {
11230a4e9518Sgw25295 /*
11240a4e9518Sgw25295 * If the user has indicated a blocking failure mode
11250a4e9518Sgw25295 * then return ERESTART which will block in dmu_tx_wait().
11260a4e9518Sgw25295 * Otherwise, return EIO so that an error can get
11270a4e9518Sgw25295 * propagated back to the VOP calls.
11280a4e9518Sgw25295 *
11290a4e9518Sgw25295 * Note that we always honor the txg_how flag regardless
11300a4e9518Sgw25295 * of the failuremode setting.
11310a4e9518Sgw25295 */
11320a4e9518Sgw25295 if (spa_get_failmode(spa) == ZIO_FAILURE_MODE_CONTINUE &&
11330a4e9518Sgw25295 txg_how != TXG_WAIT)
1134be6fd75aSMatthew Ahrens return (SET_ERROR(EIO));
11350a4e9518Sgw25295
1136be6fd75aSMatthew Ahrens return (SET_ERROR(ERESTART));
11370a4e9518Sgw25295 }
11380a4e9518Sgw25295
113969962b56SMatthew Ahrens if (!tx->tx_waited &&
114069962b56SMatthew Ahrens dsl_pool_need_dirty_delay(tx->tx_pool)) {
114169962b56SMatthew Ahrens tx->tx_wait_dirty = B_TRUE;
114269962b56SMatthew Ahrens return (SET_ERROR(ERESTART));
114369962b56SMatthew Ahrens }
114469962b56SMatthew Ahrens
11458a2f1b91Sahrens tx->tx_txg = txg_hold_open(tx->tx_pool, &tx->tx_txgh);
11468a2f1b91Sahrens tx->tx_needassign_txh = NULL;
11478a2f1b91Sahrens
11488a2f1b91Sahrens /*
11498a2f1b91Sahrens * NB: No error returns are allowed after txg_hold_open, but
11508a2f1b91Sahrens * before processing the dnode holds, due to the
11518a2f1b91Sahrens * dmu_tx_unassign() logic.
11528a2f1b91Sahrens */
11538a2f1b91Sahrens
1154715614a4Smaybee towrite = tofree = tooverwrite = tounref = tohold = fudge = 0;
11558a2f1b91Sahrens for (txh = list_head(&tx->tx_holds); txh;
11568a2f1b91Sahrens txh = list_next(&tx->tx_holds, txh)) {
11578a2f1b91Sahrens dnode_t *dn = txh->txh_dnode;
1158fa9e4066Sahrens if (dn != NULL) {
1159fa9e4066Sahrens mutex_enter(&dn->dn_mtx);
11608a2f1b91Sahrens if (dn->dn_assigned_txg == tx->tx_txg - 1) {
1161fa9e4066Sahrens mutex_exit(&dn->dn_mtx);
11628a2f1b91Sahrens tx->tx_needassign_txh = txh;
1163be6fd75aSMatthew Ahrens return (SET_ERROR(ERESTART));
1164fa9e4066Sahrens }
11658a2f1b91Sahrens if (dn->dn_assigned_txg == 0)
1166fa9e4066Sahrens dn->dn_assigned_txg = tx->tx_txg;
11678a2f1b91Sahrens ASSERT3U(dn->dn_assigned_txg, ==, tx->tx_txg);
1168fa9e4066Sahrens (void) refcount_add(&dn->dn_tx_holds, tx);
1169fa9e4066Sahrens mutex_exit(&dn->dn_mtx);
1170fa9e4066Sahrens }
11718a2f1b91Sahrens towrite += txh->txh_space_towrite;
11728a2f1b91Sahrens tofree += txh->txh_space_tofree;
11738a2f1b91Sahrens tooverwrite += txh->txh_space_tooverwrite;
1174a9799022Sck153898 tounref += txh->txh_space_tounref;
1175cdb0ab79Smaybee tohold += txh->txh_memory_tohold;
1176715614a4Smaybee fudge += txh->txh_fudge;
1177ea8dc4b6Seschrock }
1178ea8dc4b6Seschrock
1179ea8dc4b6Seschrock /*
1180ea8dc4b6Seschrock * If a snapshot has been taken since we made our estimates,
1181ea8dc4b6Seschrock * assume that we won't be able to free or overwrite anything.
1182ea8dc4b6Seschrock */
1183ea8dc4b6Seschrock if (tx->tx_objset &&
1184503ad85cSMatthew Ahrens dsl_dataset_prev_snap_txg(tx->tx_objset->os_dsl_dataset) >
1185ea8dc4b6Seschrock tx->tx_lastsnap_txg) {
11868a2f1b91Sahrens towrite += tooverwrite;
11878a2f1b91Sahrens tooverwrite = tofree = 0;
1188fa9e4066Sahrens }
1189fa9e4066Sahrens
1190cdb0ab79Smaybee /* needed allocation: worst-case estimate of write space */
1191cdb0ab79Smaybee asize = spa_get_asize(tx->tx_pool->dp_spa, towrite + tooverwrite);
1192cdb0ab79Smaybee /* freed space estimate: worst-case overwrite + free estimate */
11938a2f1b91Sahrens fsize = spa_get_asize(tx->tx_pool->dp_spa, tooverwrite) + tofree;
1194cdb0ab79Smaybee /* convert unrefd space to worst-case estimate */
1195a9799022Sck153898 usize = spa_get_asize(tx->tx_pool->dp_spa, tounref);
1196cdb0ab79Smaybee /* calculate memory footprint estimate */
1197cdb0ab79Smaybee memory = towrite + tooverwrite + tohold;
11988a2f1b91Sahrens
11998a2f1b91Sahrens #ifdef ZFS_DEBUG
1200715614a4Smaybee /*
1201715614a4Smaybee * Add in 'tohold' to account for our dirty holds on this memory
1202715614a4Smaybee * XXX - the "fudge" factor is to account for skipped blocks that
1203715614a4Smaybee * we missed because dnode_next_offset() misses in-core-only blocks.
1204715614a4Smaybee */
1205cdb0ab79Smaybee tx->tx_space_towrite = asize +
1206715614a4Smaybee spa_get_asize(tx->tx_pool->dp_spa, tohold + fudge);
12078a2f1b91Sahrens tx->tx_space_tofree = tofree;
12088a2f1b91Sahrens tx->tx_space_tooverwrite = tooverwrite;
1209a9799022Sck153898 tx->tx_space_tounref = tounref;
12108a2f1b91Sahrens #endif
1211fa9e4066Sahrens
1212fa9e4066Sahrens if (tx->tx_dir && asize != 0) {
1213cdb0ab79Smaybee int err = dsl_dir_tempreserve_space(tx->tx_dir, memory,
1214cdb0ab79Smaybee asize, fsize, usize, &tx->tx_tempreserve_cookie, tx);
12158a2f1b91Sahrens if (err)
1216fa9e4066Sahrens return (err);
1217fa9e4066Sahrens }
1218fa9e4066Sahrens
1219fa9e4066Sahrens return (0);
1220fa9e4066Sahrens }
1221fa9e4066Sahrens
12228a2f1b91Sahrens static void
dmu_tx_unassign(dmu_tx_t * tx)12238a2f1b91Sahrens dmu_tx_unassign(dmu_tx_t *tx)
1224fa9e4066Sahrens {
12258a2f1b91Sahrens dmu_tx_hold_t *txh;
1226fa9e4066Sahrens
12278a2f1b91Sahrens if (tx->tx_txg == 0)
12288a2f1b91Sahrens return;
1229fa9e4066Sahrens
1230fa9e4066Sahrens txg_rele_to_quiesce(&tx->tx_txgh);
1231fa9e4066Sahrens
12323e30c24aSWill Andrews /*
12333e30c24aSWill Andrews * Walk the transaction's hold list, removing the hold on the
12343e30c24aSWill Andrews * associated dnode, and notifying waiters if the refcount drops to 0.
12353e30c24aSWill Andrews */
12368a2f1b91Sahrens for (txh = list_head(&tx->tx_holds); txh != tx->tx_needassign_txh;
12378a2f1b91Sahrens txh = list_next(&tx->tx_holds, txh)) {
12388a2f1b91Sahrens dnode_t *dn = txh->txh_dnode;
1239fa9e4066Sahrens
1240fa9e4066Sahrens if (dn == NULL)
1241fa9e4066Sahrens continue;
1242fa9e4066Sahrens mutex_enter(&dn->dn_mtx);
12438a2f1b91Sahrens ASSERT3U(dn->dn_assigned_txg, ==, tx->tx_txg);
1244fa9e4066Sahrens
1245fa9e4066Sahrens if (refcount_remove(&dn->dn_tx_holds, tx) == 0) {
1246fa9e4066Sahrens dn->dn_assigned_txg = 0;
1247fa9e4066Sahrens cv_broadcast(&dn->dn_notxholds);
1248fa9e4066Sahrens }
1249fa9e4066Sahrens mutex_exit(&dn->dn_mtx);
1250fa9e4066Sahrens }
1251fa9e4066Sahrens
1252fa9e4066Sahrens txg_rele_to_sync(&tx->tx_txgh);
1253fa9e4066Sahrens
12548a2f1b91Sahrens tx->tx_lasttried_txg = tx->tx_txg;
1255fa9e4066Sahrens tx->tx_txg = 0;
1256fa9e4066Sahrens }
1257fa9e4066Sahrens
1258fa9e4066Sahrens /*
1259fa9e4066Sahrens * Assign tx to a transaction group. txg_how can be one of:
1260fa9e4066Sahrens *
1261fa9e4066Sahrens * (1) TXG_WAIT. If the current open txg is full, waits until there's
1262fa9e4066Sahrens * a new one. This should be used when you're not holding locks.
12633b2aab18SMatthew Ahrens * It will only fail if we're truly out of space (or over quota).
1264fa9e4066Sahrens *
1265fa9e4066Sahrens * (2) TXG_NOWAIT. If we can't assign into the current open txg without
1266fa9e4066Sahrens * blocking, returns immediately with ERESTART. This should be used
1267fa9e4066Sahrens * whenever you're holding locks. On an ERESTART error, the caller
12688a2f1b91Sahrens * should drop locks, do a dmu_tx_wait(tx), and try again.
126969962b56SMatthew Ahrens *
127069962b56SMatthew Ahrens * (3) TXG_WAITED. Like TXG_NOWAIT, but indicates that dmu_tx_wait()
127169962b56SMatthew Ahrens * has already been called on behalf of this operation (though
127269962b56SMatthew Ahrens * most likely on a different tx).
1273fa9e4066Sahrens */
1274fa9e4066Sahrens int
dmu_tx_assign(dmu_tx_t * tx,txg_how_t txg_how)12753b2aab18SMatthew Ahrens dmu_tx_assign(dmu_tx_t *tx, txg_how_t txg_how)
1276fa9e4066Sahrens {
1277fa9e4066Sahrens int err;
1278fa9e4066Sahrens
1279fa9e4066Sahrens ASSERT(tx->tx_txg == 0);
128069962b56SMatthew Ahrens ASSERT(txg_how == TXG_WAIT || txg_how == TXG_NOWAIT ||
128169962b56SMatthew Ahrens txg_how == TXG_WAITED);
1282fa9e4066Sahrens ASSERT(!dsl_pool_sync_context(tx->tx_pool));
1283fa9e4066Sahrens
12843b2aab18SMatthew Ahrens /* If we might wait, we must not hold the config lock. */
12853b2aab18SMatthew Ahrens ASSERT(txg_how != TXG_WAIT || !dsl_pool_config_held(tx->tx_pool));
12863b2aab18SMatthew Ahrens
128769962b56SMatthew Ahrens if (txg_how == TXG_WAITED)
128869962b56SMatthew Ahrens tx->tx_waited = B_TRUE;
128969962b56SMatthew Ahrens
12908a2f1b91Sahrens while ((err = dmu_tx_try_assign(tx, txg_how)) != 0) {
12918a2f1b91Sahrens dmu_tx_unassign(tx);
1292fa9e4066Sahrens
1293fa9e4066Sahrens if (err != ERESTART || txg_how != TXG_WAIT)
1294fa9e4066Sahrens return (err);
1295fa9e4066Sahrens
12968a2f1b91Sahrens dmu_tx_wait(tx);
1297fa9e4066Sahrens }
1298fa9e4066Sahrens
1299fa9e4066Sahrens txg_rele_to_quiesce(&tx->tx_txgh);
1300fa9e4066Sahrens
1301fa9e4066Sahrens return (0);
1302fa9e4066Sahrens }
1303fa9e4066Sahrens
1304fa9e4066Sahrens void
dmu_tx_wait(dmu_tx_t * tx)13058a2f1b91Sahrens dmu_tx_wait(dmu_tx_t *tx)
13068a2f1b91Sahrens {
13070a4e9518Sgw25295 spa_t *spa = tx->tx_pool->dp_spa;
130869962b56SMatthew Ahrens dsl_pool_t *dp = tx->tx_pool;
13098a2f1b91Sahrens
13100a4e9518Sgw25295 ASSERT(tx->tx_txg == 0);
13113b2aab18SMatthew Ahrens ASSERT(!dsl_pool_config_held(tx->tx_pool));
13120a4e9518Sgw25295
131369962b56SMatthew Ahrens if (tx->tx_wait_dirty) {
13140a4e9518Sgw25295 /*
131569962b56SMatthew Ahrens * dmu_tx_try_assign() has determined that we need to wait
131669962b56SMatthew Ahrens * because we've consumed much or all of the dirty buffer
131769962b56SMatthew Ahrens * space.
13180a4e9518Sgw25295 */
131969962b56SMatthew Ahrens mutex_enter(&dp->dp_lock);
132069962b56SMatthew Ahrens while (dp->dp_dirty_total >= zfs_dirty_data_max)
132169962b56SMatthew Ahrens cv_wait(&dp->dp_spaceavail_cv, &dp->dp_lock);
132269962b56SMatthew Ahrens uint64_t dirty = dp->dp_dirty_total;
132369962b56SMatthew Ahrens mutex_exit(&dp->dp_lock);
132469962b56SMatthew Ahrens
132569962b56SMatthew Ahrens dmu_tx_delay(tx, dirty);
132669962b56SMatthew Ahrens
132769962b56SMatthew Ahrens tx->tx_wait_dirty = B_FALSE;
132869962b56SMatthew Ahrens
132969962b56SMatthew Ahrens /*
133069962b56SMatthew Ahrens * Note: setting tx_waited only has effect if the caller
133169962b56SMatthew Ahrens * used TX_WAIT. Otherwise they are going to destroy
133269962b56SMatthew Ahrens * this tx and try again. The common case, zfs_write(),
133369962b56SMatthew Ahrens * uses TX_WAIT.
133469962b56SMatthew Ahrens */
133569962b56SMatthew Ahrens tx->tx_waited = B_TRUE;
133669962b56SMatthew Ahrens } else if (spa_suspended(spa) || tx->tx_lasttried_txg == 0) {
133769962b56SMatthew Ahrens /*
133869962b56SMatthew Ahrens * If the pool is suspended we need to wait until it
133969962b56SMatthew Ahrens * is resumed. Note that it's possible that the pool
134069962b56SMatthew Ahrens * has become active after this thread has tried to
134169962b56SMatthew Ahrens * obtain a tx. If that's the case then tx_lasttried_txg
134269962b56SMatthew Ahrens * would not have been set.
134369962b56SMatthew Ahrens */
134469962b56SMatthew Ahrens txg_wait_synced(dp, spa_last_synced_txg(spa) + 1);
13450a4e9518Sgw25295 } else if (tx->tx_needassign_txh) {
134669962b56SMatthew Ahrens /*
134769962b56SMatthew Ahrens * A dnode is assigned to the quiescing txg. Wait for its
134869962b56SMatthew Ahrens * transaction to complete.
134969962b56SMatthew Ahrens */
13508a2f1b91Sahrens dnode_t *dn = tx->tx_needassign_txh->txh_dnode;
13518a2f1b91Sahrens
13528a2f1b91Sahrens mutex_enter(&dn->dn_mtx);
13538a2f1b91Sahrens while (dn->dn_assigned_txg == tx->tx_lasttried_txg - 1)
13548a2f1b91Sahrens cv_wait(&dn->dn_notxholds, &dn->dn_mtx);
13558a2f1b91Sahrens mutex_exit(&dn->dn_mtx);
13568a2f1b91Sahrens tx->tx_needassign_txh = NULL;
13578a2f1b91Sahrens } else {
13588a2f1b91Sahrens txg_wait_open(tx->tx_pool, tx->tx_lasttried_txg + 1);
13598a2f1b91Sahrens }
13608a2f1b91Sahrens }
13618a2f1b91Sahrens
13628a2f1b91Sahrens void
dmu_tx_willuse_space(dmu_tx_t * tx,int64_t delta)1363fa9e4066Sahrens dmu_tx_willuse_space(dmu_tx_t *tx, int64_t delta)
1364fa9e4066Sahrens {
13658a2f1b91Sahrens #ifdef ZFS_DEBUG
1366fa9e4066Sahrens if (tx->tx_dir == NULL || delta == 0)
1367fa9e4066Sahrens return;
1368fa9e4066Sahrens
1369fa9e4066Sahrens if (delta > 0) {
1370fa9e4066Sahrens ASSERT3U(refcount_count(&tx->tx_space_written) + delta, <=,
1371fa9e4066Sahrens tx->tx_space_towrite);
1372fa9e4066Sahrens (void) refcount_add_many(&tx->tx_space_written, delta, NULL);
1373fa9e4066Sahrens } else {
1374fa9e4066Sahrens (void) refcount_add_many(&tx->tx_space_freed, -delta, NULL);
1375fa9e4066Sahrens }
13768a2f1b91Sahrens #endif
1377fa9e4066Sahrens }
1378fa9e4066Sahrens
1379fa9e4066Sahrens void
dmu_tx_commit(dmu_tx_t * tx)1380fa9e4066Sahrens dmu_tx_commit(dmu_tx_t *tx)
1381fa9e4066Sahrens {
13828a2f1b91Sahrens dmu_tx_hold_t *txh;
1383fa9e4066Sahrens
1384fa9e4066Sahrens ASSERT(tx->tx_txg != 0);
1385fa9e4066Sahrens
13863e30c24aSWill Andrews /*
13873e30c24aSWill Andrews * Go through the transaction's hold list and remove holds on
13883e30c24aSWill Andrews * associated dnodes, notifying waiters if no holds remain.
13893e30c24aSWill Andrews */
13908a2f1b91Sahrens while (txh = list_head(&tx->tx_holds)) {
13918a2f1b91Sahrens dnode_t *dn = txh->txh_dnode;
1392fa9e4066Sahrens
13938a2f1b91Sahrens list_remove(&tx->tx_holds, txh);
13948a2f1b91Sahrens kmem_free(txh, sizeof (dmu_tx_hold_t));
1395fa9e4066Sahrens if (dn == NULL)
1396fa9e4066Sahrens continue;
1397fa9e4066Sahrens mutex_enter(&dn->dn_mtx);
1398fa9e4066Sahrens ASSERT3U(dn->dn_assigned_txg, ==, tx->tx_txg);
1399fa9e4066Sahrens
1400fa9e4066Sahrens if (refcount_remove(&dn->dn_tx_holds, tx) == 0) {
1401fa9e4066Sahrens dn->dn_assigned_txg = 0;
1402fa9e4066Sahrens cv_broadcast(&dn->dn_notxholds);
1403fa9e4066Sahrens }
1404fa9e4066Sahrens mutex_exit(&dn->dn_mtx);
1405fa9e4066Sahrens dnode_rele(dn, tx);
1406fa9e4066Sahrens }
1407fa9e4066Sahrens
14088a2f1b91Sahrens if (tx->tx_tempreserve_cookie)
1409fa9e4066Sahrens dsl_dir_tempreserve_clear(tx->tx_tempreserve_cookie, tx);
1410fa9e4066Sahrens
1411d20e665cSRicardo M. Correia if (!list_is_empty(&tx->tx_callbacks))
1412d20e665cSRicardo M. Correia txg_register_callbacks(&tx->tx_txgh, &tx->tx_callbacks);
1413d20e665cSRicardo M. Correia
1414fa9e4066Sahrens if (tx->tx_anyobj == FALSE)
1415fa9e4066Sahrens txg_rele_to_sync(&tx->tx_txgh);
1416d20e665cSRicardo M. Correia
1417d20e665cSRicardo M. Correia list_destroy(&tx->tx_callbacks);
14188f38d419Sek110237 list_destroy(&tx->tx_holds);
14198a2f1b91Sahrens #ifdef ZFS_DEBUG
1420fa9e4066Sahrens dprintf("towrite=%llu written=%llu tofree=%llu freed=%llu\n",
1421fa9e4066Sahrens tx->tx_space_towrite, refcount_count(&tx->tx_space_written),
1422fa9e4066Sahrens tx->tx_space_tofree, refcount_count(&tx->tx_space_freed));
1423fa9e4066Sahrens refcount_destroy_many(&tx->tx_space_written,
1424fa9e4066Sahrens refcount_count(&tx->tx_space_written));
1425fa9e4066Sahrens refcount_destroy_many(&tx->tx_space_freed,
1426fa9e4066Sahrens refcount_count(&tx->tx_space_freed));
1427fa9e4066Sahrens #endif
1428fa9e4066Sahrens kmem_free(tx, sizeof (dmu_tx_t));
1429fa9e4066Sahrens }
1430fa9e4066Sahrens
1431fa9e4066Sahrens void
dmu_tx_abort(dmu_tx_t * tx)1432fa9e4066Sahrens dmu_tx_abort(dmu_tx_t *tx)
1433fa9e4066Sahrens {
14348a2f1b91Sahrens dmu_tx_hold_t *txh;
1435fa9e4066Sahrens
1436fa9e4066Sahrens ASSERT(tx->tx_txg == 0);
1437fa9e4066Sahrens
14388a2f1b91Sahrens while (txh = list_head(&tx->tx_holds)) {
14398a2f1b91Sahrens dnode_t *dn = txh->txh_dnode;
1440fa9e4066Sahrens
14418a2f1b91Sahrens list_remove(&tx->tx_holds, txh);
14428a2f1b91Sahrens kmem_free(txh, sizeof (dmu_tx_hold_t));
1443fa9e4066Sahrens if (dn != NULL)
1444fa9e4066Sahrens dnode_rele(dn, tx);
1445fa9e4066Sahrens }
1446d20e665cSRicardo M. Correia
1447d20e665cSRicardo M. Correia /*
1448d20e665cSRicardo M. Correia * Call any registered callbacks with an error code.
1449d20e665cSRicardo M. Correia */
1450d20e665cSRicardo M. Correia if (!list_is_empty(&tx->tx_callbacks))
1451d20e665cSRicardo M. Correia dmu_tx_do_callbacks(&tx->tx_callbacks, ECANCELED);
1452d20e665cSRicardo M. Correia
1453d20e665cSRicardo M. Correia list_destroy(&tx->tx_callbacks);
14548f38d419Sek110237 list_destroy(&tx->tx_holds);
14558a2f1b91Sahrens #ifdef ZFS_DEBUG
1456fa9e4066Sahrens refcount_destroy_many(&tx->tx_space_written,
1457fa9e4066Sahrens refcount_count(&tx->tx_space_written));
1458fa9e4066Sahrens refcount_destroy_many(&tx->tx_space_freed,
1459fa9e4066Sahrens refcount_count(&tx->tx_space_freed));
1460fa9e4066Sahrens #endif
1461fa9e4066Sahrens kmem_free(tx, sizeof (dmu_tx_t));
1462fa9e4066Sahrens }
1463fa9e4066Sahrens
1464fa9e4066Sahrens uint64_t
dmu_tx_get_txg(dmu_tx_t * tx)1465fa9e4066Sahrens dmu_tx_get_txg(dmu_tx_t *tx)
1466fa9e4066Sahrens {
1467fa9e4066Sahrens ASSERT(tx->tx_txg != 0);
1468fa9e4066Sahrens return (tx->tx_txg);
1469fa9e4066Sahrens }
1470d20e665cSRicardo M. Correia
14713b2aab18SMatthew Ahrens dsl_pool_t *
dmu_tx_pool(dmu_tx_t * tx)14723b2aab18SMatthew Ahrens dmu_tx_pool(dmu_tx_t *tx)
14733b2aab18SMatthew Ahrens {
14743b2aab18SMatthew Ahrens ASSERT(tx->tx_pool != NULL);
14753b2aab18SMatthew Ahrens return (tx->tx_pool);
14763b2aab18SMatthew Ahrens }
14773b2aab18SMatthew Ahrens
14783b2aab18SMatthew Ahrens
1479d20e665cSRicardo M. Correia void
dmu_tx_callback_register(dmu_tx_t * tx,dmu_tx_callback_func_t * func,void * data)1480d20e665cSRicardo M. Correia dmu_tx_callback_register(dmu_tx_t *tx, dmu_tx_callback_func_t *func, void *data)
1481d20e665cSRicardo M. Correia {
1482d20e665cSRicardo M. Correia dmu_tx_callback_t *dcb;
1483d20e665cSRicardo M. Correia
1484d20e665cSRicardo M. Correia dcb = kmem_alloc(sizeof (dmu_tx_callback_t), KM_SLEEP);
1485d20e665cSRicardo M. Correia
1486d20e665cSRicardo M. Correia dcb->dcb_func = func;
1487d20e665cSRicardo M. Correia dcb->dcb_data = data;
1488d20e665cSRicardo M. Correia
1489d20e665cSRicardo M. Correia list_insert_tail(&tx->tx_callbacks, dcb);
1490d20e665cSRicardo M. Correia }
1491d20e665cSRicardo M. Correia
1492d20e665cSRicardo M. Correia /*
1493d20e665cSRicardo M. Correia * Call all the commit callbacks on a list, with a given error code.
1494d20e665cSRicardo M. Correia */
1495d20e665cSRicardo M. Correia void
dmu_tx_do_callbacks(list_t * cb_list,int error)1496d20e665cSRicardo M. Correia dmu_tx_do_callbacks(list_t *cb_list, int error)
1497d20e665cSRicardo M. Correia {
1498d20e665cSRicardo M. Correia dmu_tx_callback_t *dcb;
1499d20e665cSRicardo M. Correia
1500d20e665cSRicardo M. Correia while (dcb = list_head(cb_list)) {
1501d20e665cSRicardo M. Correia list_remove(cb_list, dcb);
1502d20e665cSRicardo M. Correia dcb->dcb_func(dcb->dcb_data, error);
1503d20e665cSRicardo M. Correia kmem_free(dcb, sizeof (dmu_tx_callback_t));
1504d20e665cSRicardo M. Correia }
1505d20e665cSRicardo M. Correia }
15060a586ceaSMark Shellenbaum
15070a586ceaSMark Shellenbaum /*
15080a586ceaSMark Shellenbaum * Interface to hold a bunch of attributes.
15090a586ceaSMark Shellenbaum * used for creating new files.
15100a586ceaSMark Shellenbaum * attrsize is the total size of all attributes
15110a586ceaSMark Shellenbaum * to be added during object creation
15120a586ceaSMark Shellenbaum *
15130a586ceaSMark Shellenbaum * For updating/adding a single attribute dmu_tx_hold_sa() should be used.
15140a586ceaSMark Shellenbaum */
15150a586ceaSMark Shellenbaum
15160a586ceaSMark Shellenbaum /*
15170a586ceaSMark Shellenbaum * hold necessary attribute name for attribute registration.
15180a586ceaSMark Shellenbaum * should be a very rare case where this is needed. If it does
15190a586ceaSMark Shellenbaum * happen it would only happen on the first write to the file system.
15200a586ceaSMark Shellenbaum */
15210a586ceaSMark Shellenbaum static void
dmu_tx_sa_registration_hold(sa_os_t * sa,dmu_tx_t * tx)15220a586ceaSMark Shellenbaum dmu_tx_sa_registration_hold(sa_os_t *sa, dmu_tx_t *tx)
15230a586ceaSMark Shellenbaum {
15240a586ceaSMark Shellenbaum int i;
15250a586ceaSMark Shellenbaum
15260a586ceaSMark Shellenbaum if (!sa->sa_need_attr_registration)
15270a586ceaSMark Shellenbaum return;
15280a586ceaSMark Shellenbaum
15290a586ceaSMark Shellenbaum for (i = 0; i != sa->sa_num_attrs; i++) {
15300a586ceaSMark Shellenbaum if (!sa->sa_attr_table[i].sa_registered) {
15310a586ceaSMark Shellenbaum if (sa->sa_reg_attr_obj)
15320a586ceaSMark Shellenbaum dmu_tx_hold_zap(tx, sa->sa_reg_attr_obj,
15330a586ceaSMark Shellenbaum B_TRUE, sa->sa_attr_table[i].sa_name);
15340a586ceaSMark Shellenbaum else
15350a586ceaSMark Shellenbaum dmu_tx_hold_zap(tx, DMU_NEW_OBJECT,
15360a586ceaSMark Shellenbaum B_TRUE, sa->sa_attr_table[i].sa_name);
15370a586ceaSMark Shellenbaum }
15380a586ceaSMark Shellenbaum }
15390a586ceaSMark Shellenbaum }
15400a586ceaSMark Shellenbaum
15410a586ceaSMark Shellenbaum
15420a586ceaSMark Shellenbaum void
dmu_tx_hold_spill(dmu_tx_t * tx,uint64_t object)15430a586ceaSMark Shellenbaum dmu_tx_hold_spill(dmu_tx_t *tx, uint64_t object)
15440a586ceaSMark Shellenbaum {
15450a586ceaSMark Shellenbaum dnode_t *dn;
15460a586ceaSMark Shellenbaum dmu_tx_hold_t *txh;
15470a586ceaSMark Shellenbaum
15480a586ceaSMark Shellenbaum txh = dmu_tx_hold_object_impl(tx, tx->tx_objset, object,
15490a586ceaSMark Shellenbaum THT_SPILL, 0, 0);
15500a586ceaSMark Shellenbaum
15510a586ceaSMark Shellenbaum dn = txh->txh_dnode;
15520a586ceaSMark Shellenbaum
15530a586ceaSMark Shellenbaum if (dn == NULL)
15540a586ceaSMark Shellenbaum return;
15550a586ceaSMark Shellenbaum
15560a586ceaSMark Shellenbaum /* If blkptr doesn't exist then add space to towrite */
15579dccfd2aSAlbert Lee if (!(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR)) {
1558d1a98260SMatthew Ahrens txh->txh_space_towrite += SPA_OLD_MAXBLOCKSIZE;
15590a586ceaSMark Shellenbaum } else {
15609dccfd2aSAlbert Lee blkptr_t *bp;
15619dccfd2aSAlbert Lee
15629dccfd2aSAlbert Lee bp = &dn->dn_phys->dn_spill;
15630a586ceaSMark Shellenbaum if (dsl_dataset_block_freeable(dn->dn_objset->os_dsl_dataset,
1564c7cd2421SGeorge Wilson bp, bp->blk_birth))
1565d1a98260SMatthew Ahrens txh->txh_space_tooverwrite += SPA_OLD_MAXBLOCKSIZE;
15660a586ceaSMark Shellenbaum else
1567d1a98260SMatthew Ahrens txh->txh_space_towrite += SPA_OLD_MAXBLOCKSIZE;
15689dccfd2aSAlbert Lee if (!BP_IS_HOLE(bp))
1569d1a98260SMatthew Ahrens txh->txh_space_tounref += SPA_OLD_MAXBLOCKSIZE;
15700a586ceaSMark Shellenbaum }
15710a586ceaSMark Shellenbaum }
15720a586ceaSMark Shellenbaum
15730a586ceaSMark Shellenbaum void
dmu_tx_hold_sa_create(dmu_tx_t * tx,int attrsize)15740a586ceaSMark Shellenbaum dmu_tx_hold_sa_create(dmu_tx_t *tx, int attrsize)
15750a586ceaSMark Shellenbaum {
15760a586ceaSMark Shellenbaum sa_os_t *sa = tx->tx_objset->os_sa;
15770a586ceaSMark Shellenbaum
15780a586ceaSMark Shellenbaum dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
15790a586ceaSMark Shellenbaum
15800a586ceaSMark Shellenbaum if (tx->tx_objset->os_sa->sa_master_obj == 0)
15810a586ceaSMark Shellenbaum return;
15820a586ceaSMark Shellenbaum
15830a586ceaSMark Shellenbaum if (tx->tx_objset->os_sa->sa_layout_attr_obj)
15840a586ceaSMark Shellenbaum dmu_tx_hold_zap(tx, sa->sa_layout_attr_obj, B_TRUE, NULL);
15850a586ceaSMark Shellenbaum else {
15860a586ceaSMark Shellenbaum dmu_tx_hold_zap(tx, sa->sa_master_obj, B_TRUE, SA_LAYOUTS);
15870a586ceaSMark Shellenbaum dmu_tx_hold_zap(tx, sa->sa_master_obj, B_TRUE, SA_REGISTRY);
15880a586ceaSMark Shellenbaum dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
15890a586ceaSMark Shellenbaum dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
15900a586ceaSMark Shellenbaum }
15910a586ceaSMark Shellenbaum
15920a586ceaSMark Shellenbaum dmu_tx_sa_registration_hold(sa, tx);
15930a586ceaSMark Shellenbaum
15940a586ceaSMark Shellenbaum if (attrsize <= DN_MAX_BONUSLEN && !sa->sa_force_spill)
15950a586ceaSMark Shellenbaum return;
15960a586ceaSMark Shellenbaum
15970a586ceaSMark Shellenbaum (void) dmu_tx_hold_object_impl(tx, tx->tx_objset, DMU_NEW_OBJECT,
15980a586ceaSMark Shellenbaum THT_SPILL, 0, 0);
15990a586ceaSMark Shellenbaum }
16000a586ceaSMark Shellenbaum
16010a586ceaSMark Shellenbaum /*
16020a586ceaSMark Shellenbaum * Hold SA attribute
16030a586ceaSMark Shellenbaum *
16040a586ceaSMark Shellenbaum * dmu_tx_hold_sa(dmu_tx_t *tx, sa_handle_t *, attribute, add, size)
16050a586ceaSMark Shellenbaum *
16060a586ceaSMark Shellenbaum * variable_size is the total size of all variable sized attributes
16070a586ceaSMark Shellenbaum * passed to this function. It is not the total size of all
16080a586ceaSMark Shellenbaum * variable size attributes that *may* exist on this object.
16090a586ceaSMark Shellenbaum */
16100a586ceaSMark Shellenbaum void
dmu_tx_hold_sa(dmu_tx_t * tx,sa_handle_t * hdl,boolean_t may_grow)16110a586ceaSMark Shellenbaum dmu_tx_hold_sa(dmu_tx_t *tx, sa_handle_t *hdl, boolean_t may_grow)
16120a586ceaSMark Shellenbaum {
16130a586ceaSMark Shellenbaum uint64_t object;
16140a586ceaSMark Shellenbaum sa_os_t *sa = tx->tx_objset->os_sa;
16150a586ceaSMark Shellenbaum
16160a586ceaSMark Shellenbaum ASSERT(hdl != NULL);
16170a586ceaSMark Shellenbaum
16180a586ceaSMark Shellenbaum object = sa_handle_object(hdl);
16190a586ceaSMark Shellenbaum
16200a586ceaSMark Shellenbaum dmu_tx_hold_bonus(tx, object);
16210a586ceaSMark Shellenbaum
16220a586ceaSMark Shellenbaum if (tx->tx_objset->os_sa->sa_master_obj == 0)
16230a586ceaSMark Shellenbaum return;
16240a586ceaSMark Shellenbaum
16250a586ceaSMark Shellenbaum if (tx->tx_objset->os_sa->sa_reg_attr_obj == 0 ||
16260a586ceaSMark Shellenbaum tx->tx_objset->os_sa->sa_layout_attr_obj == 0) {
16270a586ceaSMark Shellenbaum dmu_tx_hold_zap(tx, sa->sa_master_obj, B_TRUE, SA_LAYOUTS);
16280a586ceaSMark Shellenbaum dmu_tx_hold_zap(tx, sa->sa_master_obj, B_TRUE, SA_REGISTRY);
16290a586ceaSMark Shellenbaum dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
16300a586ceaSMark Shellenbaum dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
16310a586ceaSMark Shellenbaum }
16320a586ceaSMark Shellenbaum
16330a586ceaSMark Shellenbaum dmu_tx_sa_registration_hold(sa, tx);
16340a586ceaSMark Shellenbaum
16350a586ceaSMark Shellenbaum if (may_grow && tx->tx_objset->os_sa->sa_layout_attr_obj)
16360a586ceaSMark Shellenbaum dmu_tx_hold_zap(tx, sa->sa_layout_attr_obj, B_TRUE, NULL);
16370a586ceaSMark Shellenbaum
1638744947dcSTom Erickson if (sa->sa_force_spill || may_grow || hdl->sa_spill) {
16390a586ceaSMark Shellenbaum ASSERT(tx->tx_txg == 0);
16400a586ceaSMark Shellenbaum dmu_tx_hold_spill(tx, object);
1641744947dcSTom Erickson } else {
1642744947dcSTom Erickson dmu_buf_impl_t *db = (dmu_buf_impl_t *)hdl->sa_bonus;
1643744947dcSTom Erickson dnode_t *dn;
1644744947dcSTom Erickson
1645744947dcSTom Erickson DB_DNODE_ENTER(db);
1646744947dcSTom Erickson dn = DB_DNODE(db);
1647744947dcSTom Erickson if (dn->dn_have_spill) {
1648744947dcSTom Erickson ASSERT(tx->tx_txg == 0);
1649744947dcSTom Erickson dmu_tx_hold_spill(tx, object);
1650744947dcSTom Erickson }
1651744947dcSTom Erickson DB_DNODE_EXIT(db);
16520a586ceaSMark Shellenbaum }
16530a586ceaSMark Shellenbaum }
1654