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.
24b7b2590dSMatthew Ahrens * Copyright (c) 2012, 2017 by Delphix. All rights reserved.
25c3d26abcSMatthew Ahrens * Copyright (c) 2014 Integros [integros.com]
269dccfd2aSAlbert Lee */
27fa9e4066Sahrens
28fa9e4066Sahrens #include <sys/dmu.h>
29fa9e4066Sahrens #include <sys/dmu_impl.h>
30fa9e4066Sahrens #include <sys/dbuf.h>
31fa9e4066Sahrens #include <sys/dmu_tx.h>
32fa9e4066Sahrens #include <sys/dmu_objset.h>
3361e255ceSMatthew Ahrens #include <sys/dsl_dataset.h>
3461e255ceSMatthew Ahrens #include <sys/dsl_dir.h>
35fa9e4066Sahrens #include <sys/dsl_pool.h>
3661e255ceSMatthew Ahrens #include <sys/zap_impl.h>
37fa9e4066Sahrens #include <sys/spa.h>
380a586ceaSMark Shellenbaum #include <sys/sa.h>
390a586ceaSMark Shellenbaum #include <sys/sa_impl.h>
40fa9e4066Sahrens #include <sys/zfs_context.h>
410a586ceaSMark Shellenbaum #include <sys/varargs.h>
42fa9e4066Sahrens
43ea8dc4b6Seschrock typedef void (*dmu_tx_hold_func_t)(dmu_tx_t *tx, struct dnode *dn,
44ea8dc4b6Seschrock uint64_t arg1, uint64_t arg2);
45ea8dc4b6Seschrock
46fa9e4066Sahrens
47fa9e4066Sahrens dmu_tx_t *
dmu_tx_create_dd(dsl_dir_t * dd)481d452cf5Sahrens dmu_tx_create_dd(dsl_dir_t *dd)
49fa9e4066Sahrens {
50fa9e4066Sahrens dmu_tx_t *tx = kmem_zalloc(sizeof (dmu_tx_t), KM_SLEEP);
51fa9e4066Sahrens tx->tx_dir = dd;
524445fffbSMatthew Ahrens if (dd != NULL)
53fa9e4066Sahrens tx->tx_pool = dd->dd_pool;
54fa9e4066Sahrens list_create(&tx->tx_holds, sizeof (dmu_tx_hold_t),
558a2f1b91Sahrens offsetof(dmu_tx_hold_t, txh_node));
56d20e665cSRicardo M. Correia list_create(&tx->tx_callbacks, sizeof (dmu_tx_callback_t),
57d20e665cSRicardo M. Correia offsetof(dmu_tx_callback_t, dcb_node));
5869962b56SMatthew Ahrens tx->tx_start = gethrtime();
59fa9e4066Sahrens return (tx);
60fa9e4066Sahrens }
61fa9e4066Sahrens
62fa9e4066Sahrens dmu_tx_t *
dmu_tx_create(objset_t * os)63fa9e4066Sahrens dmu_tx_create(objset_t *os)
64fa9e4066Sahrens {
65503ad85cSMatthew Ahrens dmu_tx_t *tx = dmu_tx_create_dd(os->os_dsl_dataset->ds_dir);
66fa9e4066Sahrens tx->tx_objset = os;
67fa9e4066Sahrens return (tx);
68fa9e4066Sahrens }
69fa9e4066Sahrens
70fa9e4066Sahrens dmu_tx_t *
dmu_tx_create_assigned(struct dsl_pool * dp,uint64_t txg)71fa9e4066Sahrens dmu_tx_create_assigned(struct dsl_pool *dp, uint64_t txg)
72fa9e4066Sahrens {
731d452cf5Sahrens dmu_tx_t *tx = dmu_tx_create_dd(NULL);
74fa9e4066Sahrens
75b7b2590dSMatthew Ahrens txg_verify(dp->dp_spa, txg);
76fa9e4066Sahrens tx->tx_pool = dp;
77fa9e4066Sahrens tx->tx_txg = txg;
78fa9e4066Sahrens tx->tx_anyobj = TRUE;
79fa9e4066Sahrens
80fa9e4066Sahrens return (tx);
81fa9e4066Sahrens }
82fa9e4066Sahrens
83fa9e4066Sahrens int
dmu_tx_is_syncing(dmu_tx_t * tx)84fa9e4066Sahrens dmu_tx_is_syncing(dmu_tx_t *tx)
85fa9e4066Sahrens {
86fa9e4066Sahrens return (tx->tx_anyobj);
87fa9e4066Sahrens }
88fa9e4066Sahrens
89fa9e4066Sahrens int
dmu_tx_private_ok(dmu_tx_t * tx)90fa9e4066Sahrens dmu_tx_private_ok(dmu_tx_t *tx)
91fa9e4066Sahrens {
92ea8dc4b6Seschrock return (tx->tx_anyobj);
93fa9e4066Sahrens }
94fa9e4066Sahrens
958a2f1b91Sahrens static dmu_tx_hold_t *
dmu_tx_hold_dnode_impl(dmu_tx_t * tx,dnode_t * dn,enum dmu_tx_hold_type type,uint64_t arg1,uint64_t arg2)96b0c42cd4Sbzzz dmu_tx_hold_dnode_impl(dmu_tx_t *tx, dnode_t *dn, enum dmu_tx_hold_type type,
97b0c42cd4Sbzzz uint64_t arg1, uint64_t arg2)
98fa9e4066Sahrens {
998a2f1b91Sahrens dmu_tx_hold_t *txh;
100fa9e4066Sahrens
101b0c42cd4Sbzzz if (dn != NULL) {
102*e914ace2STim Schumacher (void) zfs_refcount_add(&dn->dn_holds, tx);
103b0c42cd4Sbzzz if (tx->tx_txg != 0) {
104fa9e4066Sahrens mutex_enter(&dn->dn_mtx);
105fa9e4066Sahrens /*
106fa9e4066Sahrens * dn->dn_assigned_txg == tx->tx_txg doesn't pose a
107fa9e4066Sahrens * problem, but there's no way for it to happen (for
108fa9e4066Sahrens * now, at least).
109fa9e4066Sahrens */
110fa9e4066Sahrens ASSERT(dn->dn_assigned_txg == 0);
111fa9e4066Sahrens dn->dn_assigned_txg = tx->tx_txg;
112*e914ace2STim Schumacher (void) zfs_refcount_add(&dn->dn_tx_holds, tx);
113fa9e4066Sahrens mutex_exit(&dn->dn_mtx);
114fa9e4066Sahrens }
115fa9e4066Sahrens }
116fa9e4066Sahrens
1178a2f1b91Sahrens txh = kmem_zalloc(sizeof (dmu_tx_hold_t), KM_SLEEP);
1188a2f1b91Sahrens txh->txh_tx = tx;
1198a2f1b91Sahrens txh->txh_dnode = dn;
120*e914ace2STim Schumacher zfs_refcount_create(&txh->txh_space_towrite);
121*e914ace2STim Schumacher zfs_refcount_create(&txh->txh_memory_tohold);
1228a2f1b91Sahrens txh->txh_type = type;
1238a2f1b91Sahrens txh->txh_arg1 = arg1;
1248a2f1b91Sahrens txh->txh_arg2 = arg2;
1258a2f1b91Sahrens list_insert_tail(&tx->tx_holds, txh);
126ea8dc4b6Seschrock
1278a2f1b91Sahrens return (txh);
128fa9e4066Sahrens }
129fa9e4066Sahrens
130b0c42cd4Sbzzz 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)131b0c42cd4Sbzzz dmu_tx_hold_object_impl(dmu_tx_t *tx, objset_t *os, uint64_t object,
132b0c42cd4Sbzzz enum dmu_tx_hold_type type, uint64_t arg1, uint64_t arg2)
133b0c42cd4Sbzzz {
134b0c42cd4Sbzzz dnode_t *dn = NULL;
135b0c42cd4Sbzzz dmu_tx_hold_t *txh;
136b0c42cd4Sbzzz int err;
137b0c42cd4Sbzzz
138b0c42cd4Sbzzz if (object != DMU_NEW_OBJECT) {
139b0c42cd4Sbzzz err = dnode_hold(os, object, FTAG, &dn);
140b0c42cd4Sbzzz if (err != 0) {
141b0c42cd4Sbzzz tx->tx_err = err;
142b0c42cd4Sbzzz return (NULL);
143b0c42cd4Sbzzz }
144b0c42cd4Sbzzz }
145b0c42cd4Sbzzz txh = dmu_tx_hold_dnode_impl(tx, dn, type, arg1, arg2);
146b0c42cd4Sbzzz if (dn != NULL)
147b0c42cd4Sbzzz dnode_rele(dn, FTAG);
148b0c42cd4Sbzzz return (txh);
149b0c42cd4Sbzzz }
150b0c42cd4Sbzzz
151fa9e4066Sahrens void
dmu_tx_add_new_object(dmu_tx_t * tx,dnode_t * dn)152b0c42cd4Sbzzz dmu_tx_add_new_object(dmu_tx_t *tx, dnode_t *dn)
153fa9e4066Sahrens {
154fa9e4066Sahrens /*
155fa9e4066Sahrens * If we're syncing, they can manipulate any object anyhow, and
156fa9e4066Sahrens * the hold on the dnode_t can cause problems.
157fa9e4066Sahrens */
158b0c42cd4Sbzzz if (!dmu_tx_is_syncing(tx))
159b0c42cd4Sbzzz (void) dmu_tx_hold_dnode_impl(tx, dn, THT_NEWOBJECT, 0, 0);
160fa9e4066Sahrens }
161fa9e4066Sahrens
16261e255ceSMatthew Ahrens /*
16361e255ceSMatthew Ahrens * This function reads specified data from disk. The specified data will
16461e255ceSMatthew Ahrens * be needed to perform the transaction -- i.e, it will be read after
16561e255ceSMatthew Ahrens * we do dmu_tx_assign(). There are two reasons that we read the data now
16661e255ceSMatthew Ahrens * (before dmu_tx_assign()):
16761e255ceSMatthew Ahrens *
16861e255ceSMatthew Ahrens * 1. Reading it now has potentially better performance. The transaction
16961e255ceSMatthew Ahrens * has not yet been assigned, so the TXG is not held open, and also the
17061e255ceSMatthew Ahrens * caller typically has less locks held when calling dmu_tx_hold_*() than
17161e255ceSMatthew Ahrens * after the transaction has been assigned. This reduces the lock (and txg)
17261e255ceSMatthew Ahrens * hold times, thus reducing lock contention.
17361e255ceSMatthew Ahrens *
17461e255ceSMatthew Ahrens * 2. It is easier for callers (primarily the ZPL) to handle i/o errors
17561e255ceSMatthew Ahrens * that are detected before they start making changes to the DMU state
17661e255ceSMatthew Ahrens * (i.e. now). Once the transaction has been assigned, and some DMU
17761e255ceSMatthew Ahrens * state has been changed, it can be difficult to recover from an i/o
17861e255ceSMatthew Ahrens * error (e.g. to undo the changes already made in memory at the DMU
17961e255ceSMatthew Ahrens * layer). Typically code to do so does not exist in the caller -- it
18061e255ceSMatthew Ahrens * assumes that the data has already been cached and thus i/o errors are
18161e255ceSMatthew Ahrens * not possible.
18261e255ceSMatthew Ahrens *
18361e255ceSMatthew Ahrens * It has been observed that the i/o initiated here can be a performance
18461e255ceSMatthew Ahrens * problem, and it appears to be optional, because we don't look at the
18561e255ceSMatthew Ahrens * data which is read. However, removing this read would only serve to
18661e255ceSMatthew Ahrens * move the work elsewhere (after the dmu_tx_assign()), where it may
18761e255ceSMatthew Ahrens * have a greater impact on performance (in addition to the impact on
18861e255ceSMatthew Ahrens * fault tolerance noted above).
18961e255ceSMatthew Ahrens */
190ea8dc4b6Seschrock static int
dmu_tx_check_ioerr(zio_t * zio,dnode_t * dn,int level,uint64_t blkid)191ea8dc4b6Seschrock dmu_tx_check_ioerr(zio_t *zio, dnode_t *dn, int level, uint64_t blkid)
192ea8dc4b6Seschrock {
193ea8dc4b6Seschrock int err;
194ea8dc4b6Seschrock dmu_buf_impl_t *db;
195ea8dc4b6Seschrock
196ea8dc4b6Seschrock rw_enter(&dn->dn_struct_rwlock, RW_READER);
197ea8dc4b6Seschrock db = dbuf_hold_level(dn, level, blkid, FTAG);
198ea8dc4b6Seschrock rw_exit(&dn->dn_struct_rwlock);
199ea8dc4b6Seschrock if (db == NULL)
200be6fd75aSMatthew Ahrens return (SET_ERROR(EIO));
2011ab7f2deSmaybee err = dbuf_read(db, zio, DB_RF_CANFAIL | DB_RF_NOPREFETCH);
202ea8dc4b6Seschrock dbuf_rele(db, FTAG);
203ea8dc4b6Seschrock return (err);
204ea8dc4b6Seschrock }
205ea8dc4b6Seschrock
206fa9e4066Sahrens /* ARGSUSED */
207fa9e4066Sahrens static void
dmu_tx_count_write(dmu_tx_hold_t * txh,uint64_t off,uint64_t len)2088a2f1b91Sahrens dmu_tx_count_write(dmu_tx_hold_t *txh, uint64_t off, uint64_t len)
209fa9e4066Sahrens {
2108a2f1b91Sahrens dnode_t *dn = txh->txh_dnode;
2118a2f1b91Sahrens int err = 0;
212fa9e4066Sahrens
213fa9e4066Sahrens if (len == 0)
214fa9e4066Sahrens return;
215fa9e4066Sahrens
216*e914ace2STim Schumacher (void) zfs_refcount_add_many(&txh->txh_space_towrite, len, FTAG);
2174a7f2a75SMark Maybee
218*e914ace2STim Schumacher if (zfs_refcount_count(&txh->txh_space_towrite) > 2 * DMU_MAX_ACCESS)
21961e255ceSMatthew Ahrens err = SET_ERROR(EFBIG);
220ea8dc4b6Seschrock
22161e255ceSMatthew Ahrens if (dn == NULL)
22261e255ceSMatthew Ahrens return;
223ea8dc4b6Seschrock
22461e255ceSMatthew Ahrens /*
22561e255ceSMatthew Ahrens * For i/o error checking, read the blocks that will be needed
22661e255ceSMatthew Ahrens * to perform the write: the first and last level-0 blocks (if
22761e255ceSMatthew Ahrens * they are not aligned, i.e. if they are partial-block writes),
22861e255ceSMatthew Ahrens * and all the level-1 blocks.
22961e255ceSMatthew Ahrens */
23061e255ceSMatthew Ahrens if (dn->dn_maxblkid == 0) {
23161e255ceSMatthew Ahrens if (off < dn->dn_datablksz &&
23261e255ceSMatthew Ahrens (off > 0 || len < dn->dn_datablksz)) {
23361e255ceSMatthew Ahrens err = dmu_tx_check_ioerr(NULL, dn, 0, 0);
23461e255ceSMatthew Ahrens if (err != 0) {
23561e255ceSMatthew Ahrens txh->txh_tx->tx_err = err;
236ea8dc4b6Seschrock }
2374a7f2a75SMark Maybee }
23861e255ceSMatthew Ahrens } else {
23961e255ceSMatthew Ahrens zio_t *zio = zio_root(dn->dn_objset->os_spa,
24061e255ceSMatthew Ahrens NULL, NULL, ZIO_FLAG_CANFAIL);
2414a7f2a75SMark Maybee
24261e255ceSMatthew Ahrens /* first level-0 block */
24361e255ceSMatthew Ahrens uint64_t start = off >> dn->dn_datablkshift;
24461e255ceSMatthew Ahrens if (P2PHASE(off, dn->dn_datablksz) || len < dn->dn_datablksz) {
24561e255ceSMatthew Ahrens err = dmu_tx_check_ioerr(zio, dn, 0, start);
24661e255ceSMatthew Ahrens if (err != 0) {
24761e255ceSMatthew Ahrens txh->txh_tx->tx_err = err;
24861e255ceSMatthew Ahrens }
249b24ab676SJeff Bonwick }
2504a7f2a75SMark Maybee
25161e255ceSMatthew Ahrens /* last level-0 block */
25261e255ceSMatthew Ahrens uint64_t end = (off + len - 1) >> dn->dn_datablkshift;
25361e255ceSMatthew Ahrens if (end != start && end <= dn->dn_maxblkid &&
25461e255ceSMatthew Ahrens P2PHASE(off + len, dn->dn_datablksz)) {
25561e255ceSMatthew Ahrens err = dmu_tx_check_ioerr(zio, dn, 0, end);
25661e255ceSMatthew Ahrens if (err != 0) {
25701025c89SJohn Harres txh->txh_tx->tx_err = err;
25801025c89SJohn Harres }
25961e255ceSMatthew Ahrens }
26001025c89SJohn Harres
26161e255ceSMatthew Ahrens /* level-1 blocks */
26261e255ceSMatthew Ahrens if (dn->dn_nlevels > 1) {
26361e255ceSMatthew Ahrens int shft = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
26461e255ceSMatthew Ahrens for (uint64_t i = (start >> shft) + 1;
26561e255ceSMatthew Ahrens i < end >> shft; i++) {
26661e255ceSMatthew Ahrens err = dmu_tx_check_ioerr(zio, dn, 1, i);
26761e255ceSMatthew Ahrens if (err != 0) {
26861e255ceSMatthew Ahrens txh->txh_tx->tx_err = err;
2690c779ad4SMatthew Ahrens }
2704a7f2a75SMark Maybee }
2714a7f2a75SMark Maybee }
272fa9e4066Sahrens
27361e255ceSMatthew Ahrens err = zio_wait(zio);
27461e255ceSMatthew Ahrens if (err != 0) {
27561e255ceSMatthew Ahrens txh->txh_tx->tx_err = err;
2764a7f2a75SMark Maybee }
277fa9e4066Sahrens }
278fa9e4066Sahrens }
279fa9e4066Sahrens
280fa9e4066Sahrens static void
dmu_tx_count_dnode(dmu_tx_hold_t * txh)2818a2f1b91Sahrens dmu_tx_count_dnode(dmu_tx_hold_t *txh)
282fa9e4066Sahrens {
283*e914ace2STim Schumacher (void) zfs_refcount_add_many(&txh->txh_space_towrite, DNODE_MIN_SIZE,
284*e914ace2STim Schumacher FTAG);
285fa9e4066Sahrens }
286fa9e4066Sahrens
287fa9e4066Sahrens void
dmu_tx_hold_write(dmu_tx_t * tx,uint64_t object,uint64_t off,int len)288fa9e4066Sahrens dmu_tx_hold_write(dmu_tx_t *tx, uint64_t object, uint64_t off, int len)
289fa9e4066Sahrens {
2908a2f1b91Sahrens dmu_tx_hold_t *txh;
2918a2f1b91Sahrens
29261e255ceSMatthew Ahrens ASSERT0(tx->tx_txg);
29361e255ceSMatthew Ahrens ASSERT3U(len, <=, DMU_MAX_ACCESS);
294dd6ef538Smaybee ASSERT(len == 0 || UINT64_MAX - off >= len - 1);
295fa9e4066Sahrens
2968a2f1b91Sahrens txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
2978a2f1b91Sahrens object, THT_WRITE, off, len);
298b0c42cd4Sbzzz if (txh != NULL) {
299b0c42cd4Sbzzz dmu_tx_count_write(txh, off, len);
300b0c42cd4Sbzzz dmu_tx_count_dnode(txh);
301b0c42cd4Sbzzz }
302b0c42cd4Sbzzz }
3038a2f1b91Sahrens
3045cabbc6bSPrashanth Sreenivasa void
dmu_tx_hold_remap_l1indirect(dmu_tx_t * tx,uint64_t object)3055cabbc6bSPrashanth Sreenivasa dmu_tx_hold_remap_l1indirect(dmu_tx_t *tx, uint64_t object)
3065cabbc6bSPrashanth Sreenivasa {
3075cabbc6bSPrashanth Sreenivasa dmu_tx_hold_t *txh;
3085cabbc6bSPrashanth Sreenivasa
3095cabbc6bSPrashanth Sreenivasa ASSERT(tx->tx_txg == 0);
3105cabbc6bSPrashanth Sreenivasa txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
3115cabbc6bSPrashanth Sreenivasa object, THT_WRITE, 0, 0);
3125cabbc6bSPrashanth Sreenivasa if (txh == NULL)
3135cabbc6bSPrashanth Sreenivasa return;
3145cabbc6bSPrashanth Sreenivasa
3155cabbc6bSPrashanth Sreenivasa dnode_t *dn = txh->txh_dnode;
316*e914ace2STim Schumacher (void) zfs_refcount_add_many(&txh->txh_space_towrite,
3175cabbc6bSPrashanth Sreenivasa 1ULL << dn->dn_indblkshift, FTAG);
3185cabbc6bSPrashanth Sreenivasa dmu_tx_count_dnode(txh);
3195cabbc6bSPrashanth Sreenivasa }
3205cabbc6bSPrashanth Sreenivasa
321b0c42cd4Sbzzz void
dmu_tx_hold_write_by_dnode(dmu_tx_t * tx,dnode_t * dn,uint64_t off,int len)322b0c42cd4Sbzzz dmu_tx_hold_write_by_dnode(dmu_tx_t *tx, dnode_t *dn, uint64_t off, int len)
323b0c42cd4Sbzzz {
324b0c42cd4Sbzzz dmu_tx_hold_t *txh;
325b0c42cd4Sbzzz
326b0c42cd4Sbzzz ASSERT0(tx->tx_txg);
327b0c42cd4Sbzzz ASSERT3U(len, <=, DMU_MAX_ACCESS);
328b0c42cd4Sbzzz ASSERT(len == 0 || UINT64_MAX - off >= len - 1);
329b0c42cd4Sbzzz
330b0c42cd4Sbzzz txh = dmu_tx_hold_dnode_impl(tx, dn, THT_WRITE, off, len);
331b0c42cd4Sbzzz if (txh != NULL) {
332b0c42cd4Sbzzz dmu_tx_count_write(txh, off, len);
333b0c42cd4Sbzzz dmu_tx_count_dnode(txh);
334b0c42cd4Sbzzz }
335fa9e4066Sahrens }
336fa9e4066Sahrens
3374bb73804SMatthew Ahrens /*
3384bb73804SMatthew Ahrens * This function marks the transaction as being a "net free". The end
3394bb73804SMatthew Ahrens * result is that refquotas will be disabled for this transaction, and
3404bb73804SMatthew Ahrens * this transaction will be able to use half of the pool space overhead
3414bb73804SMatthew Ahrens * (see dsl_pool_adjustedsize()). Therefore this function should only
3424bb73804SMatthew Ahrens * be called for transactions that we expect will not cause a net increase
3434bb73804SMatthew Ahrens * in the amount of space used (but it's OK if that is occasionally not true).
3444bb73804SMatthew Ahrens */
3454bb73804SMatthew Ahrens void
dmu_tx_mark_netfree(dmu_tx_t * tx)3464bb73804SMatthew Ahrens dmu_tx_mark_netfree(dmu_tx_t *tx)
3474bb73804SMatthew Ahrens {
34861e255ceSMatthew Ahrens tx->tx_netfree = B_TRUE;
3494bb73804SMatthew Ahrens }
3504bb73804SMatthew Ahrens
351b0c42cd4Sbzzz static void
dmu_tx_hold_free_impl(dmu_tx_hold_t * txh,uint64_t off,uint64_t len)352b0c42cd4Sbzzz dmu_tx_hold_free_impl(dmu_tx_hold_t *txh, uint64_t off, uint64_t len)
353fa9e4066Sahrens {
354b0c42cd4Sbzzz dmu_tx_t *tx;
355b0c42cd4Sbzzz dnode_t *dn;
3562f3d8780SMatthew Ahrens int err;
357fa9e4066Sahrens
358b0c42cd4Sbzzz tx = txh->txh_tx;
3598a2f1b91Sahrens ASSERT(tx->tx_txg == 0);
3608a2f1b91Sahrens
361b0c42cd4Sbzzz dn = txh->txh_dnode;
36269962b56SMatthew Ahrens dmu_tx_count_dnode(txh);
3638a2f1b91Sahrens
36461e255ceSMatthew Ahrens if (off >= (dn->dn_maxblkid + 1) * dn->dn_datablksz)
365fa9e4066Sahrens return;
366fa9e4066Sahrens if (len == DMU_OBJECT_END)
36761e255ceSMatthew Ahrens len = (dn->dn_maxblkid + 1) * dn->dn_datablksz - off;
368fa9e4066Sahrens
369ea8dc4b6Seschrock /*
3702f3d8780SMatthew Ahrens * For i/o error checking, we read the first and last level-0
3712f3d8780SMatthew Ahrens * blocks if they are not aligned, and all the level-1 blocks.
3722f3d8780SMatthew Ahrens *
3732f3d8780SMatthew Ahrens * Note: dbuf_free_range() assumes that we have not instantiated
3742f3d8780SMatthew Ahrens * any level-0 dbufs that will be completely freed. Therefore we must
3752f3d8780SMatthew Ahrens * exercise care to not read or count the first and last blocks
3762f3d8780SMatthew Ahrens * if they are blocksize-aligned.
3772f3d8780SMatthew Ahrens */
3782f3d8780SMatthew Ahrens if (dn->dn_datablkshift == 0) {
379713d6c20SMatthew Ahrens if (off != 0 || len < dn->dn_datablksz)
3805253393bSMatthew Ahrens dmu_tx_count_write(txh, 0, dn->dn_datablksz);
3812f3d8780SMatthew Ahrens } else {
3822f3d8780SMatthew Ahrens /* first block will be modified if it is not aligned */
3832f3d8780SMatthew Ahrens if (!IS_P2ALIGNED(off, 1 << dn->dn_datablkshift))
3842f3d8780SMatthew Ahrens dmu_tx_count_write(txh, off, 1);
3852f3d8780SMatthew Ahrens /* last block will be modified if it is not aligned */
3862f3d8780SMatthew Ahrens if (!IS_P2ALIGNED(off + len, 1 << dn->dn_datablkshift))
38761e255ceSMatthew Ahrens dmu_tx_count_write(txh, off + len, 1);
3882f3d8780SMatthew Ahrens }
3892f3d8780SMatthew Ahrens
3902f3d8780SMatthew Ahrens /*
3912f3d8780SMatthew Ahrens * Check level-1 blocks.
392ea8dc4b6Seschrock */
39398572ac1Sahrens if (dn->dn_nlevels > 1) {
3942f3d8780SMatthew Ahrens int shift = dn->dn_datablkshift + dn->dn_indblkshift -
39598572ac1Sahrens SPA_BLKPTRSHIFT;
3962f3d8780SMatthew Ahrens uint64_t start = off >> shift;
3972f3d8780SMatthew Ahrens uint64_t end = (off + len) >> shift;
3982f3d8780SMatthew Ahrens
3992f3d8780SMatthew Ahrens ASSERT(dn->dn_indblkshift != 0);
40098572ac1Sahrens
401bb411a08SMatthew Ahrens /*
402bb411a08SMatthew Ahrens * dnode_reallocate() can result in an object with indirect
403bb411a08SMatthew Ahrens * blocks having an odd data block size. In this case,
404bb411a08SMatthew Ahrens * just check the single block.
405bb411a08SMatthew Ahrens */
406bb411a08SMatthew Ahrens if (dn->dn_datablkshift == 0)
407bb411a08SMatthew Ahrens start = end = 0;
408bb411a08SMatthew Ahrens
40961e255ceSMatthew Ahrens zio_t *zio = zio_root(tx->tx_pool->dp_spa,
41098572ac1Sahrens NULL, NULL, ZIO_FLAG_CANFAIL);
4112f3d8780SMatthew Ahrens for (uint64_t i = start; i <= end; i++) {
41298572ac1Sahrens uint64_t ibyte = i << shift;
413cdb0ab79Smaybee err = dnode_next_offset(dn, 0, &ibyte, 2, 1, 0);
41498572ac1Sahrens i = ibyte >> shift;
41546e1baa6SMatthew Ahrens if (err == ESRCH || i > end)
41698572ac1Sahrens break;
41761e255ceSMatthew Ahrens if (err != 0) {
41898572ac1Sahrens tx->tx_err = err;
41961e255ceSMatthew Ahrens (void) zio_wait(zio);
42098572ac1Sahrens return;
42198572ac1Sahrens }
422ea8dc4b6Seschrock
423*e914ace2STim Schumacher (void) zfs_refcount_add_many(&txh->txh_memory_tohold,
42461e255ceSMatthew Ahrens 1 << dn->dn_indblkshift, FTAG);
42561e255ceSMatthew Ahrens
42698572ac1Sahrens err = dmu_tx_check_ioerr(zio, dn, 1, i);
42761e255ceSMatthew Ahrens if (err != 0) {
42898572ac1Sahrens tx->tx_err = err;
42961e255ceSMatthew Ahrens (void) zio_wait(zio);
43098572ac1Sahrens return;
43198572ac1Sahrens }
43298572ac1Sahrens }
43398572ac1Sahrens err = zio_wait(zio);
43461e255ceSMatthew Ahrens if (err != 0) {
435ea8dc4b6Seschrock tx->tx_err = err;
436ea8dc4b6Seschrock return;
437ea8dc4b6Seschrock }
438ea8dc4b6Seschrock }
439fa9e4066Sahrens }
440fa9e4066Sahrens
441fa9e4066Sahrens void
dmu_tx_hold_free(dmu_tx_t * tx,uint64_t object,uint64_t off,uint64_t len)442b0c42cd4Sbzzz dmu_tx_hold_free(dmu_tx_t *tx, uint64_t object, uint64_t off, uint64_t len)
443b0c42cd4Sbzzz {
444b0c42cd4Sbzzz dmu_tx_hold_t *txh;
445b0c42cd4Sbzzz
446b0c42cd4Sbzzz txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
447b0c42cd4Sbzzz object, THT_FREE, off, len);
448b0c42cd4Sbzzz if (txh != NULL)
449b0c42cd4Sbzzz (void) dmu_tx_hold_free_impl(txh, off, len);
450b0c42cd4Sbzzz }
451b0c42cd4Sbzzz
452b0c42cd4Sbzzz void
dmu_tx_hold_free_by_dnode(dmu_tx_t * tx,dnode_t * dn,uint64_t off,uint64_t len)453b0c42cd4Sbzzz dmu_tx_hold_free_by_dnode(dmu_tx_t *tx, dnode_t *dn, uint64_t off, uint64_t len)
454b0c42cd4Sbzzz {
455b0c42cd4Sbzzz dmu_tx_hold_t *txh;
456b0c42cd4Sbzzz
457b0c42cd4Sbzzz txh = dmu_tx_hold_dnode_impl(tx, dn, THT_FREE, off, len);
458b0c42cd4Sbzzz if (txh != NULL)
459b0c42cd4Sbzzz (void) dmu_tx_hold_free_impl(txh, off, len);
460b0c42cd4Sbzzz }
461b0c42cd4Sbzzz
462b0c42cd4Sbzzz static void
dmu_tx_hold_zap_impl(dmu_tx_hold_t * txh,const char * name)463411be58aSMatthew Ahrens dmu_tx_hold_zap_impl(dmu_tx_hold_t *txh, const char *name)
464fa9e4066Sahrens {
465b0c42cd4Sbzzz dmu_tx_t *tx = txh->txh_tx;
466b0c42cd4Sbzzz dnode_t *dn;
4670c779ad4SMatthew Ahrens int err;
468fa9e4066Sahrens
4698a2f1b91Sahrens ASSERT(tx->tx_txg == 0);
4708a2f1b91Sahrens
471b0c42cd4Sbzzz dn = txh->txh_dnode;
4728a2f1b91Sahrens
4738a2f1b91Sahrens dmu_tx_count_dnode(txh);
474fa9e4066Sahrens
47561e255ceSMatthew Ahrens /*
47661e255ceSMatthew Ahrens * Modifying a almost-full microzap is around the worst case (128KB)
47761e255ceSMatthew Ahrens *
47861e255ceSMatthew Ahrens * If it is a fat zap, the worst case would be 7*16KB=112KB:
47961e255ceSMatthew Ahrens * - 3 blocks overwritten: target leaf, ptrtbl block, header block
48061e255ceSMatthew Ahrens * - 4 new blocks written if adding:
48161e255ceSMatthew Ahrens * - 2 blocks for possibly split leaves,
48261e255ceSMatthew Ahrens * - 2 grown ptrtbl blocks
48361e255ceSMatthew Ahrens */
484*e914ace2STim Schumacher (void) zfs_refcount_add_many(&txh->txh_space_towrite,
48561e255ceSMatthew Ahrens MZAP_MAX_BLKSZ, FTAG);
48661e255ceSMatthew Ahrens
48761e255ceSMatthew Ahrens if (dn == NULL)
488fa9e4066Sahrens return;
489fa9e4066Sahrens
490ad135b5dSChristopher Siden ASSERT3P(DMU_OT_BYTESWAP(dn->dn_type), ==, DMU_BSWAP_ZAP);
491fa9e4066Sahrens
49261e255ceSMatthew Ahrens if (dn->dn_maxblkid == 0 || name == NULL) {
493fa9e4066Sahrens /*
49461e255ceSMatthew Ahrens * This is a microzap (only one block), or we don't know
49561e255ceSMatthew Ahrens * the name. Check the first block for i/o errors.
496fa9e4066Sahrens */
497ea8dc4b6Seschrock err = dmu_tx_check_ioerr(NULL, dn, 0, 0);
49861e255ceSMatthew Ahrens if (err != 0) {
499ea8dc4b6Seschrock tx->tx_err = err;
5000c779ad4SMatthew Ahrens }
50161e255ceSMatthew Ahrens } else {
502ea8dc4b6Seschrock /*
50361e255ceSMatthew Ahrens * Access the name so that we'll check for i/o errors to
50461e255ceSMatthew Ahrens * the leaf blocks, etc. We ignore ENOENT, as this name
50561e255ceSMatthew Ahrens * may not yet exist.
506ea8dc4b6Seschrock */
50779d72832SMatthew Ahrens err = zap_lookup_by_dnode(dn, name, 8, 0, NULL);
50861e255ceSMatthew Ahrens if (err == EIO || err == ECKSUM || err == ENXIO) {
509ea8dc4b6Seschrock tx->tx_err = err;
5100c779ad4SMatthew Ahrens }
5110c779ad4SMatthew Ahrens }
512fa9e4066Sahrens }
513fa9e4066Sahrens
514b0c42cd4Sbzzz void
dmu_tx_hold_zap(dmu_tx_t * tx,uint64_t object,int add,const char * name)515b0c42cd4Sbzzz dmu_tx_hold_zap(dmu_tx_t *tx, uint64_t object, int add, const char *name)
516b0c42cd4Sbzzz {
517b0c42cd4Sbzzz dmu_tx_hold_t *txh;
518b0c42cd4Sbzzz
519b0c42cd4Sbzzz ASSERT0(tx->tx_txg);
520b0c42cd4Sbzzz
521b0c42cd4Sbzzz txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
522b0c42cd4Sbzzz object, THT_ZAP, add, (uintptr_t)name);
523b0c42cd4Sbzzz if (txh != NULL)
524411be58aSMatthew Ahrens dmu_tx_hold_zap_impl(txh, name);
525b0c42cd4Sbzzz }
526b0c42cd4Sbzzz
527b0c42cd4Sbzzz void
dmu_tx_hold_zap_by_dnode(dmu_tx_t * tx,dnode_t * dn,int add,const char * name)528b0c42cd4Sbzzz dmu_tx_hold_zap_by_dnode(dmu_tx_t *tx, dnode_t *dn, int add, const char *name)
529b0c42cd4Sbzzz {
530b0c42cd4Sbzzz dmu_tx_hold_t *txh;
531b0c42cd4Sbzzz
532b0c42cd4Sbzzz ASSERT0(tx->tx_txg);
533b0c42cd4Sbzzz ASSERT(dn != NULL);
534b0c42cd4Sbzzz
535b0c42cd4Sbzzz txh = dmu_tx_hold_dnode_impl(tx, dn, THT_ZAP, add, (uintptr_t)name);
536b0c42cd4Sbzzz if (txh != NULL)
537411be58aSMatthew Ahrens dmu_tx_hold_zap_impl(txh, name);
538b0c42cd4Sbzzz }
539b0c42cd4Sbzzz
540fa9e4066Sahrens void
dmu_tx_hold_bonus(dmu_tx_t * tx,uint64_t object)541fa9e4066Sahrens dmu_tx_hold_bonus(dmu_tx_t *tx, uint64_t object)
542fa9e4066Sahrens {
5438a2f1b91Sahrens dmu_tx_hold_t *txh;
544fa9e4066Sahrens
5458a2f1b91Sahrens ASSERT(tx->tx_txg == 0);
546fa9e4066Sahrens
5478a2f1b91Sahrens txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
5488a2f1b91Sahrens object, THT_BONUS, 0, 0);
5498a2f1b91Sahrens if (txh)
5508a2f1b91Sahrens dmu_tx_count_dnode(txh);
551fa9e4066Sahrens }
552fa9e4066Sahrens
553b0c42cd4Sbzzz void
dmu_tx_hold_bonus_by_dnode(dmu_tx_t * tx,dnode_t * dn)554b0c42cd4Sbzzz dmu_tx_hold_bonus_by_dnode(dmu_tx_t *tx, dnode_t *dn)
555b0c42cd4Sbzzz {
556b0c42cd4Sbzzz dmu_tx_hold_t *txh;
557b0c42cd4Sbzzz
558b0c42cd4Sbzzz ASSERT0(tx->tx_txg);
559b0c42cd4Sbzzz
560b0c42cd4Sbzzz txh = dmu_tx_hold_dnode_impl(tx, dn, THT_BONUS, 0, 0);
561b0c42cd4Sbzzz if (txh)
562b0c42cd4Sbzzz dmu_tx_count_dnode(txh);
563b0c42cd4Sbzzz }
564b0c42cd4Sbzzz
565fa9e4066Sahrens void
dmu_tx_hold_space(dmu_tx_t * tx,uint64_t space)566fa9e4066Sahrens dmu_tx_hold_space(dmu_tx_t *tx, uint64_t space)
567fa9e4066Sahrens {
5688a2f1b91Sahrens dmu_tx_hold_t *txh;
569fa9e4066Sahrens ASSERT(tx->tx_txg == 0);
570fa9e4066Sahrens
5718a2f1b91Sahrens txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
5728a2f1b91Sahrens DMU_NEW_OBJECT, THT_SPACE, space, 0);
5738a2f1b91Sahrens
574*e914ace2STim Schumacher (void) zfs_refcount_add_many(&txh->txh_space_towrite, space, FTAG);
575fa9e4066Sahrens }
576fa9e4066Sahrens
5779c9dc39aSek #ifdef ZFS_DEBUG
578fa9e4066Sahrens void
dmu_tx_dirty_buf(dmu_tx_t * tx,dmu_buf_impl_t * db)579fa9e4066Sahrens dmu_tx_dirty_buf(dmu_tx_t *tx, dmu_buf_impl_t *db)
580fa9e4066Sahrens {
58161e255ceSMatthew Ahrens boolean_t match_object = B_FALSE;
58261e255ceSMatthew Ahrens boolean_t match_offset = B_FALSE;
583fa9e4066Sahrens
584744947dcSTom Erickson DB_DNODE_ENTER(db);
58561e255ceSMatthew Ahrens dnode_t *dn = DB_DNODE(db);
586fa9e4066Sahrens ASSERT(tx->tx_txg != 0);
587503ad85cSMatthew Ahrens ASSERT(tx->tx_objset == NULL || dn->dn_objset == tx->tx_objset);
588fa9e4066Sahrens ASSERT3U(dn->dn_object, ==, db->db.db_object);
589fa9e4066Sahrens
590744947dcSTom Erickson if (tx->tx_anyobj) {
591744947dcSTom Erickson DB_DNODE_EXIT(db);
592fa9e4066Sahrens return;
593744947dcSTom Erickson }
594fa9e4066Sahrens
595fa9e4066Sahrens /* XXX No checking on the meta dnode for now */
596744947dcSTom Erickson if (db->db.db_object == DMU_META_DNODE_OBJECT) {
597744947dcSTom Erickson DB_DNODE_EXIT(db);
598fa9e4066Sahrens return;
599744947dcSTom Erickson }
600fa9e4066Sahrens
60161e255ceSMatthew Ahrens for (dmu_tx_hold_t *txh = list_head(&tx->tx_holds); txh != NULL;
6028a2f1b91Sahrens txh = list_next(&tx->tx_holds, txh)) {
603fa9e4066Sahrens ASSERT(dn == NULL || dn->dn_assigned_txg == tx->tx_txg);
6048a2f1b91Sahrens if (txh->txh_dnode == dn && txh->txh_type != THT_NEWOBJECT)
605fa9e4066Sahrens match_object = TRUE;
6068a2f1b91Sahrens if (txh->txh_dnode == NULL || txh->txh_dnode == dn) {
607fa9e4066Sahrens int datablkshift = dn->dn_datablkshift ?
608fa9e4066Sahrens dn->dn_datablkshift : SPA_MAXBLOCKSHIFT;
609fa9e4066Sahrens int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
610fa9e4066Sahrens int shift = datablkshift + epbs * db->db_level;
611fa9e4066Sahrens uint64_t beginblk = shift >= 64 ? 0 :
6128a2f1b91Sahrens (txh->txh_arg1 >> shift);
613fa9e4066Sahrens uint64_t endblk = shift >= 64 ? 0 :
6148a2f1b91Sahrens ((txh->txh_arg1 + txh->txh_arg2 - 1) >> shift);
615fa9e4066Sahrens uint64_t blkid = db->db_blkid;
616fa9e4066Sahrens
6178a2f1b91Sahrens /* XXX txh_arg2 better not be zero... */
618fa9e4066Sahrens
6198a2f1b91Sahrens dprintf("found txh type %x beginblk=%llx endblk=%llx\n",
6208a2f1b91Sahrens txh->txh_type, beginblk, endblk);
621fa9e4066Sahrens
6228a2f1b91Sahrens switch (txh->txh_type) {
623fa9e4066Sahrens case THT_WRITE:
624fa9e4066Sahrens if (blkid >= beginblk && blkid <= endblk)
625fa9e4066Sahrens match_offset = TRUE;
626fa9e4066Sahrens /*
627fa9e4066Sahrens * We will let this hold work for the bonus
6280a586ceaSMark Shellenbaum * or spill buffer so that we don't need to
6290a586ceaSMark Shellenbaum * hold it when creating a new object.
630fa9e4066Sahrens */
6310a586ceaSMark Shellenbaum if (blkid == DMU_BONUS_BLKID ||
6320a586ceaSMark Shellenbaum blkid == DMU_SPILL_BLKID)
633fa9e4066Sahrens match_offset = TRUE;
634fa9e4066Sahrens /*
635fa9e4066Sahrens * They might have to increase nlevels,
636fa9e4066Sahrens * thus dirtying the new TLIBs. Or the
637fa9e4066Sahrens * might have to change the block size,
638fa9e4066Sahrens * thus dirying the new lvl=0 blk=0.
639fa9e4066Sahrens */
640fa9e4066Sahrens if (blkid == 0)
641fa9e4066Sahrens match_offset = TRUE;
642fa9e4066Sahrens break;
643fa9e4066Sahrens case THT_FREE:
644cdb0ab79Smaybee /*
645cdb0ab79Smaybee * We will dirty all the level 1 blocks in
646cdb0ab79Smaybee * the free range and perhaps the first and
647cdb0ab79Smaybee * last level 0 block.
648cdb0ab79Smaybee */
649cdb0ab79Smaybee if (blkid >= beginblk && (blkid <= endblk ||
650cdb0ab79Smaybee txh->txh_arg2 == DMU_OBJECT_END))
651fa9e4066Sahrens match_offset = TRUE;
652fa9e4066Sahrens break;
6530a586ceaSMark Shellenbaum case THT_SPILL:
6540a586ceaSMark Shellenbaum if (blkid == DMU_SPILL_BLKID)
6550a586ceaSMark Shellenbaum match_offset = TRUE;
6560a586ceaSMark Shellenbaum break;
657fa9e4066Sahrens case THT_BONUS:
6580a586ceaSMark Shellenbaum if (blkid == DMU_BONUS_BLKID)
659fa9e4066Sahrens match_offset = TRUE;
660fa9e4066Sahrens break;
661fa9e4066Sahrens case THT_ZAP:
662fa9e4066Sahrens match_offset = TRUE;
663fa9e4066Sahrens break;
664fa9e4066Sahrens case THT_NEWOBJECT:
665fa9e4066Sahrens match_object = TRUE;
666fa9e4066Sahrens break;
667fa9e4066Sahrens default:
6688a2f1b91Sahrens ASSERT(!"bad txh_type");
669fa9e4066Sahrens }
670fa9e4066Sahrens }
671744947dcSTom Erickson if (match_object && match_offset) {
672744947dcSTom Erickson DB_DNODE_EXIT(db);
673fa9e4066Sahrens return;
674744947dcSTom Erickson }
675fa9e4066Sahrens }
676744947dcSTom Erickson DB_DNODE_EXIT(db);
677fa9e4066Sahrens panic("dirtying dbuf obj=%llx lvl=%u blkid=%llx but not tx_held\n",
678fa9e4066Sahrens (u_longlong_t)db->db.db_object, db->db_level,
679fa9e4066Sahrens (u_longlong_t)db->db_blkid);
680fa9e4066Sahrens }
6819c9dc39aSek #endif
682fa9e4066Sahrens
68369962b56SMatthew Ahrens /*
68469962b56SMatthew Ahrens * If we can't do 10 iops, something is wrong. Let us go ahead
68569962b56SMatthew Ahrens * and hit zfs_dirty_data_max.
68669962b56SMatthew Ahrens */
68769962b56SMatthew Ahrens hrtime_t zfs_delay_max_ns = MSEC2NSEC(100);
68869962b56SMatthew Ahrens int zfs_delay_resolution_ns = 100 * 1000; /* 100 microseconds */
68969962b56SMatthew Ahrens
69069962b56SMatthew Ahrens /*
69169962b56SMatthew Ahrens * We delay transactions when we've determined that the backend storage
69269962b56SMatthew Ahrens * isn't able to accommodate the rate of incoming writes.
69369962b56SMatthew Ahrens *
69469962b56SMatthew Ahrens * If there is already a transaction waiting, we delay relative to when
69569962b56SMatthew Ahrens * that transaction finishes waiting. This way the calculated min_time
69669962b56SMatthew Ahrens * is independent of the number of threads concurrently executing
69769962b56SMatthew Ahrens * transactions.
69869962b56SMatthew Ahrens *
69969962b56SMatthew Ahrens * If we are the only waiter, wait relative to when the transaction
70069962b56SMatthew Ahrens * started, rather than the current time. This credits the transaction for
70169962b56SMatthew Ahrens * "time already served", e.g. reading indirect blocks.
70269962b56SMatthew Ahrens *
70369962b56SMatthew Ahrens * The minimum time for a transaction to take is calculated as:
70469962b56SMatthew Ahrens * min_time = scale * (dirty - min) / (max - dirty)
70569962b56SMatthew Ahrens * min_time is then capped at zfs_delay_max_ns.
70669962b56SMatthew Ahrens *
70769962b56SMatthew Ahrens * The delay has two degrees of freedom that can be adjusted via tunables.
70869962b56SMatthew Ahrens * The percentage of dirty data at which we start to delay is defined by
70969962b56SMatthew Ahrens * zfs_delay_min_dirty_percent. This should typically be at or above
71069962b56SMatthew Ahrens * zfs_vdev_async_write_active_max_dirty_percent so that we only start to
71169962b56SMatthew Ahrens * delay after writing at full speed has failed to keep up with the incoming
71269962b56SMatthew Ahrens * write rate. The scale of the curve is defined by zfs_delay_scale. Roughly
71369962b56SMatthew Ahrens * speaking, this variable determines the amount of delay at the midpoint of
71469962b56SMatthew Ahrens * the curve.
71569962b56SMatthew Ahrens *
71669962b56SMatthew Ahrens * delay
71769962b56SMatthew Ahrens * 10ms +-------------------------------------------------------------*+
71869962b56SMatthew Ahrens * | *|
71969962b56SMatthew Ahrens * 9ms + *+
72069962b56SMatthew Ahrens * | *|
72169962b56SMatthew Ahrens * 8ms + *+
72269962b56SMatthew Ahrens * | * |
72369962b56SMatthew Ahrens * 7ms + * +
72469962b56SMatthew Ahrens * | * |
72569962b56SMatthew Ahrens * 6ms + * +
72669962b56SMatthew Ahrens * | * |
72769962b56SMatthew Ahrens * 5ms + * +
72869962b56SMatthew Ahrens * | * |
72969962b56SMatthew Ahrens * 4ms + * +
73069962b56SMatthew Ahrens * | * |
73169962b56SMatthew Ahrens * 3ms + * +
73269962b56SMatthew Ahrens * | * |
73369962b56SMatthew Ahrens * 2ms + (midpoint) * +
73469962b56SMatthew Ahrens * | | ** |
73569962b56SMatthew Ahrens * 1ms + v *** +
73669962b56SMatthew Ahrens * | zfs_delay_scale ----------> ******** |
73769962b56SMatthew Ahrens * 0 +-------------------------------------*********----------------+
73869962b56SMatthew Ahrens * 0% <- zfs_dirty_data_max -> 100%
73969962b56SMatthew Ahrens *
74069962b56SMatthew Ahrens * Note that since the delay is added to the outstanding time remaining on the
74169962b56SMatthew Ahrens * most recent transaction, the delay is effectively the inverse of IOPS.
74269962b56SMatthew Ahrens * Here the midpoint of 500us translates to 2000 IOPS. The shape of the curve
74369962b56SMatthew Ahrens * was chosen such that small changes in the amount of accumulated dirty data
74469962b56SMatthew Ahrens * in the first 3/4 of the curve yield relatively small differences in the
74569962b56SMatthew Ahrens * amount of delay.
74669962b56SMatthew Ahrens *
74769962b56SMatthew Ahrens * The effects can be easier to understand when the amount of delay is
74869962b56SMatthew Ahrens * represented on a log scale:
74969962b56SMatthew Ahrens *
75069962b56SMatthew Ahrens * delay
75169962b56SMatthew Ahrens * 100ms +-------------------------------------------------------------++
75269962b56SMatthew Ahrens * + +
75369962b56SMatthew Ahrens * | |
75469962b56SMatthew Ahrens * + *+
75569962b56SMatthew Ahrens * 10ms + *+
75669962b56SMatthew Ahrens * + ** +
75769962b56SMatthew Ahrens * | (midpoint) ** |
75869962b56SMatthew Ahrens * + | ** +
75969962b56SMatthew Ahrens * 1ms + v **** +
76069962b56SMatthew Ahrens * + zfs_delay_scale ----------> ***** +
76169962b56SMatthew Ahrens * | **** |
76269962b56SMatthew Ahrens * + **** +
76369962b56SMatthew Ahrens * 100us + ** +
76469962b56SMatthew Ahrens * + * +
76569962b56SMatthew Ahrens * | * |
76669962b56SMatthew Ahrens * + * +
76769962b56SMatthew Ahrens * 10us + * +
76869962b56SMatthew Ahrens * + +
76969962b56SMatthew Ahrens * | |
77069962b56SMatthew Ahrens * + +
77169962b56SMatthew Ahrens * +--------------------------------------------------------------+
77269962b56SMatthew Ahrens * 0% <- zfs_dirty_data_max -> 100%
77369962b56SMatthew Ahrens *
77469962b56SMatthew Ahrens * Note here that only as the amount of dirty data approaches its limit does
77569962b56SMatthew Ahrens * the delay start to increase rapidly. The goal of a properly tuned system
77669962b56SMatthew Ahrens * should be to keep the amount of dirty data out of that range by first
77769962b56SMatthew Ahrens * ensuring that the appropriate limits are set for the I/O scheduler to reach
77869962b56SMatthew Ahrens * optimal throughput on the backend storage, and then by changing the value
77969962b56SMatthew Ahrens * of zfs_delay_scale to increase the steepness of the curve.
78069962b56SMatthew Ahrens */
78169962b56SMatthew Ahrens static void
dmu_tx_delay(dmu_tx_t * tx,uint64_t dirty)78269962b56SMatthew Ahrens dmu_tx_delay(dmu_tx_t *tx, uint64_t dirty)
78369962b56SMatthew Ahrens {
78469962b56SMatthew Ahrens dsl_pool_t *dp = tx->tx_pool;
78569962b56SMatthew Ahrens uint64_t delay_min_bytes =
78669962b56SMatthew Ahrens zfs_dirty_data_max * zfs_delay_min_dirty_percent / 100;
78769962b56SMatthew Ahrens hrtime_t wakeup, min_tx_time, now;
78869962b56SMatthew Ahrens
78969962b56SMatthew Ahrens if (dirty <= delay_min_bytes)
79069962b56SMatthew Ahrens return;
79169962b56SMatthew Ahrens
79269962b56SMatthew Ahrens /*
79369962b56SMatthew Ahrens * The caller has already waited until we are under the max.
79469962b56SMatthew Ahrens * We make them pass us the amount of dirty data so we don't
79569962b56SMatthew Ahrens * have to handle the case of it being >= the max, which could
79669962b56SMatthew Ahrens * cause a divide-by-zero if it's == the max.
79769962b56SMatthew Ahrens */
79869962b56SMatthew Ahrens ASSERT3U(dirty, <, zfs_dirty_data_max);
79969962b56SMatthew Ahrens
80069962b56SMatthew Ahrens now = gethrtime();
80169962b56SMatthew Ahrens min_tx_time = zfs_delay_scale *
80269962b56SMatthew Ahrens (dirty - delay_min_bytes) / (zfs_dirty_data_max - dirty);
80369962b56SMatthew Ahrens if (now > tx->tx_start + min_tx_time)
80469962b56SMatthew Ahrens return;
80569962b56SMatthew Ahrens
80669962b56SMatthew Ahrens min_tx_time = MIN(min_tx_time, zfs_delay_max_ns);
80769962b56SMatthew Ahrens
80869962b56SMatthew Ahrens DTRACE_PROBE3(delay__mintime, dmu_tx_t *, tx, uint64_t, dirty,
80969962b56SMatthew Ahrens uint64_t, min_tx_time);
81069962b56SMatthew Ahrens
81169962b56SMatthew Ahrens mutex_enter(&dp->dp_lock);
81269962b56SMatthew Ahrens wakeup = MAX(tx->tx_start + min_tx_time,
81369962b56SMatthew Ahrens dp->dp_last_wakeup + min_tx_time);
81469962b56SMatthew Ahrens dp->dp_last_wakeup = wakeup;
81569962b56SMatthew Ahrens mutex_exit(&dp->dp_lock);
81669962b56SMatthew Ahrens
81769962b56SMatthew Ahrens #ifdef _KERNEL
81869962b56SMatthew Ahrens mutex_enter(&curthread->t_delay_lock);
81969962b56SMatthew Ahrens while (cv_timedwait_hires(&curthread->t_delay_cv,
82069962b56SMatthew Ahrens &curthread->t_delay_lock, wakeup, zfs_delay_resolution_ns,
82169962b56SMatthew Ahrens CALLOUT_FLAG_ABSOLUTE | CALLOUT_FLAG_ROUNDUP) > 0)
82269962b56SMatthew Ahrens continue;
82369962b56SMatthew Ahrens mutex_exit(&curthread->t_delay_lock);
82469962b56SMatthew Ahrens #else
82569962b56SMatthew Ahrens hrtime_t delta = wakeup - gethrtime();
82669962b56SMatthew Ahrens struct timespec ts;
82769962b56SMatthew Ahrens ts.tv_sec = delta / NANOSEC;
82869962b56SMatthew Ahrens ts.tv_nsec = delta % NANOSEC;
82969962b56SMatthew Ahrens (void) nanosleep(&ts, NULL);
83069962b56SMatthew Ahrens #endif
83169962b56SMatthew Ahrens }
83269962b56SMatthew Ahrens
83361e255ceSMatthew Ahrens /*
83461e255ceSMatthew Ahrens * This routine attempts to assign the transaction to a transaction group.
83561e255ceSMatthew Ahrens * To do so, we must determine if there is sufficient free space on disk.
83661e255ceSMatthew Ahrens *
83761e255ceSMatthew Ahrens * If this is a "netfree" transaction (i.e. we called dmu_tx_mark_netfree()
83861e255ceSMatthew Ahrens * on it), then it is assumed that there is sufficient free space,
83961e255ceSMatthew Ahrens * unless there's insufficient slop space in the pool (see the comment
84061e255ceSMatthew Ahrens * above spa_slop_shift in spa_misc.c).
84161e255ceSMatthew Ahrens *
84261e255ceSMatthew Ahrens * If it is not a "netfree" transaction, then if the data already on disk
84361e255ceSMatthew Ahrens * is over the allowed usage (e.g. quota), this will fail with EDQUOT or
84461e255ceSMatthew Ahrens * ENOSPC. Otherwise, if the current rough estimate of pending changes,
84561e255ceSMatthew Ahrens * plus the rough estimate of this transaction's changes, may exceed the
84661e255ceSMatthew Ahrens * allowed usage, then this will fail with ERESTART, which will cause the
84761e255ceSMatthew Ahrens * caller to wait for the pending changes to be written to disk (by waiting
84861e255ceSMatthew Ahrens * for the next TXG to open), and then check the space usage again.
84961e255ceSMatthew Ahrens *
85061e255ceSMatthew Ahrens * The rough estimate of pending changes is comprised of the sum of:
85161e255ceSMatthew Ahrens *
85261e255ceSMatthew Ahrens * - this transaction's holds' txh_space_towrite
85361e255ceSMatthew Ahrens *
85461e255ceSMatthew Ahrens * - dd_tempreserved[], which is the sum of in-flight transactions'
85561e255ceSMatthew Ahrens * holds' txh_space_towrite (i.e. those transactions that have called
85661e255ceSMatthew Ahrens * dmu_tx_assign() but not yet called dmu_tx_commit()).
85761e255ceSMatthew Ahrens *
85861e255ceSMatthew Ahrens * - dd_space_towrite[], which is the amount of dirtied dbufs.
85961e255ceSMatthew Ahrens *
86061e255ceSMatthew Ahrens * Note that all of these values are inflated by spa_get_worst_case_asize(),
86161e255ceSMatthew Ahrens * which means that we may get ERESTART well before we are actually in danger
86261e255ceSMatthew Ahrens * of running out of space, but this also mitigates any small inaccuracies
86361e255ceSMatthew Ahrens * in the rough estimate (e.g. txh_space_towrite doesn't take into account
86461e255ceSMatthew Ahrens * indirect blocks, and dd_space_towrite[] doesn't take into account changes
86561e255ceSMatthew Ahrens * to the MOS).
86661e255ceSMatthew Ahrens *
86761e255ceSMatthew Ahrens * Note that due to this algorithm, it is possible to exceed the allowed
86861e255ceSMatthew Ahrens * usage by one transaction. Also, as we approach the allowed usage,
86961e255ceSMatthew Ahrens * we will allow a very limited amount of changes into each TXG, thus
87061e255ceSMatthew Ahrens * decreasing performance.
87161e255ceSMatthew Ahrens */
872fa9e4066Sahrens static int
dmu_tx_try_assign(dmu_tx_t * tx,uint64_t txg_how)873f864f99eSPrakash Surya dmu_tx_try_assign(dmu_tx_t *tx, uint64_t txg_how)
874fa9e4066Sahrens {
8750a4e9518Sgw spa_t *spa = tx->tx_pool->dp_spa;
876fa9e4066Sahrens
877fb09f5aaSMadhav Suresh ASSERT0(tx->tx_txg);
8780a4e9518Sgw
8798a2f1b91Sahrens if (tx->tx_err)
8808a2f1b91Sahrens return (tx->tx_err);
881fa9e4066Sahrens
882e14bb325SJeff Bonwick if (spa_suspended(spa)) {
8830a4e9518Sgw /*
8840a4e9518Sgw * If the user has indicated a blocking failure mode
8850a4e9518Sgw * then return ERESTART which will block in dmu_tx_wait().
8860a4e9518Sgw * Otherwise, return EIO so that an error can get
8870a4e9518Sgw * propagated back to the VOP calls.
8880a4e9518Sgw *
8890a4e9518Sgw * Note that we always honor the txg_how flag regardless
8900a4e9518Sgw * of the failuremode setting.
8910a4e9518Sgw */
8920a4e9518Sgw if (spa_get_failmode(spa) == ZIO_FAILURE_MODE_CONTINUE &&
893f864f99eSPrakash Surya !(txg_how & TXG_WAIT))
894be6fd75aSMatthew Ahrens return (SET_ERROR(EIO));
8950a4e9518Sgw
896be6fd75aSMatthew Ahrens return (SET_ERROR(ERESTART));
8970a4e9518Sgw }
8980a4e9518Sgw
899f864f99eSPrakash Surya if (!tx->tx_dirty_delayed &&
90069962b56SMatthew Ahrens dsl_pool_need_dirty_delay(tx->tx_pool)) {
90169962b56SMatthew Ahrens tx->tx_wait_dirty = B_TRUE;
90269962b56SMatthew Ahrens return (SET_ERROR(ERESTART));
90369962b56SMatthew Ahrens }
90469962b56SMatthew Ahrens
905fa9e4066Sahrens tx->tx_txg = txg_hold_open(tx->tx_pool, &tx->tx_txgh);
9068a2f1b91Sahrens tx->tx_needassign_txh = NULL;
907fa9e4066Sahrens
9088a2f1b91Sahrens /*
9098a2f1b91Sahrens * NB: No error returns are allowed after txg_hold_open, but
9108a2f1b91Sahrens * before processing the dnode holds, due to the
9118a2f1b91Sahrens * dmu_tx_unassign() logic.
9128a2f1b91Sahrens */
913fa9e4066Sahrens
91461e255ceSMatthew Ahrens uint64_t towrite = 0;
91561e255ceSMatthew Ahrens uint64_t tohold = 0;
91661e255ceSMatthew Ahrens for (dmu_tx_hold_t *txh = list_head(&tx->tx_holds); txh != NULL;
9178a2f1b91Sahrens txh = list_next(&tx->tx_holds, txh)) {
9188a2f1b91Sahrens dnode_t *dn = txh->txh_dnode;
919fa9e4066Sahrens if (dn != NULL) {
920fa9e4066Sahrens mutex_enter(&dn->dn_mtx);
9218a2f1b91Sahrens if (dn->dn_assigned_txg == tx->tx_txg - 1) {
9228a2f1b91Sahrens mutex_exit(&dn->dn_mtx);
9238a2f1b91Sahrens tx->tx_needassign_txh = txh;
924be6fd75aSMatthew Ahrens return (SET_ERROR(ERESTART));
925fa9e4066Sahrens }
9268a2f1b91Sahrens if (dn->dn_assigned_txg == 0)
927fa9e4066Sahrens dn->dn_assigned_txg = tx->tx_txg;
9288a2f1b91Sahrens ASSERT3U(dn->dn_assigned_txg, ==, tx->tx_txg);
929*e914ace2STim Schumacher (void) zfs_refcount_add(&dn->dn_tx_holds, tx);
930fa9e4066Sahrens mutex_exit(&dn->dn_mtx);
931fa9e4066Sahrens }
932*e914ace2STim Schumacher towrite += zfs_refcount_count(&txh->txh_space_towrite);
933*e914ace2STim Schumacher tohold += zfs_refcount_count(&txh->txh_memory_tohold);
934fa9e4066Sahrens }
935fa9e4066Sahrens
936cdb0ab79Smaybee /* needed allocation: worst-case estimate of write space */
93761e255ceSMatthew Ahrens uint64_t asize = spa_get_worst_case_asize(tx->tx_pool->dp_spa, towrite);
938cdb0ab79Smaybee /* calculate memory footprint estimate */
93961e255ceSMatthew Ahrens uint64_t memory = towrite + tohold;
940fa9e4066Sahrens
94161e255ceSMatthew Ahrens if (tx->tx_dir != NULL && asize != 0) {
942cdb0ab79Smaybee int err = dsl_dir_tempreserve_space(tx->tx_dir, memory,
94361e255ceSMatthew Ahrens asize, tx->tx_netfree, &tx->tx_tempreserve_cookie, tx);
94461e255ceSMatthew Ahrens if (err != 0)
945fa9e4066Sahrens return (err);
946fa9e4066Sahrens }
947fa9e4066Sahrens
948fa9e4066Sahrens return (0);
949fa9e4066Sahrens }
950fa9e4066Sahrens
9518a2f1b91Sahrens static void
dmu_tx_unassign(dmu_tx_t * tx)9528a2f1b91Sahrens dmu_tx_unassign(dmu_tx_t *tx)
953fa9e4066Sahrens {
9548a2f1b91Sahrens if (tx->tx_txg == 0)
9558a2f1b91Sahrens return;
956fa9e4066Sahrens
957fa9e4066Sahrens txg_rele_to_quiesce(&tx->tx_txgh);
958fa9e4066Sahrens
9593e30c24aSWill Andrews /*
9603e30c24aSWill Andrews * Walk the transaction's hold list, removing the hold on the
9613e30c24aSWill Andrews * associated dnode, and notifying waiters if the refcount drops to 0.
9623e30c24aSWill Andrews */
96361e255ceSMatthew Ahrens for (dmu_tx_hold_t *txh = list_head(&tx->tx_holds);
96461e255ceSMatthew Ahrens txh != tx->tx_needassign_txh;
9658a2f1b91Sahrens txh = list_next(&tx->tx_holds, txh)) {
9668a2f1b91Sahrens dnode_t *dn = txh->txh_dnode;
967fa9e4066Sahrens
968fa9e4066Sahrens if (dn == NULL)
969fa9e4066Sahrens continue;
970fa9e4066Sahrens mutex_enter(&dn->dn_mtx);
9718a2f1b91Sahrens ASSERT3U(dn->dn_assigned_txg, ==, tx->tx_txg);
972fa9e4066Sahrens
973*e914ace2STim Schumacher if (zfs_refcount_remove(&dn->dn_tx_holds, tx) == 0) {
974fa9e4066Sahrens dn->dn_assigned_txg = 0;
975fa9e4066Sahrens cv_broadcast(&dn->dn_notxholds);
976fa9e4066Sahrens }
977fa9e4066Sahrens mutex_exit(&dn->dn_mtx);
978fa9e4066Sahrens }
979fa9e4066Sahrens
980fa9e4066Sahrens txg_rele_to_sync(&tx->tx_txgh);
981fa9e4066Sahrens
9828a2f1b91Sahrens tx->tx_lasttried_txg = tx->tx_txg;
983fa9e4066Sahrens tx->tx_txg = 0;
984fa9e4066Sahrens }
985fa9e4066Sahrens
986fa9e4066Sahrens /*
987f864f99eSPrakash Surya * Assign tx to a transaction group; txg_how is a bitmask:
988fa9e4066Sahrens *
989f864f99eSPrakash Surya * If TXG_WAIT is set and the currently open txg is full, this function
990f864f99eSPrakash Surya * will wait until there's a new txg. This should be used when no locks
991f864f99eSPrakash Surya * are being held. With this bit set, this function will only fail if
992f864f99eSPrakash Surya * we're truly out of space (or over quota).
993fa9e4066Sahrens *
994f864f99eSPrakash Surya * If TXG_WAIT is *not* set and we can't assign into the currently open
995f864f99eSPrakash Surya * txg without blocking, this function will return immediately with
996f864f99eSPrakash Surya * ERESTART. This should be used whenever locks are being held. On an
997f864f99eSPrakash Surya * ERESTART error, the caller should drop all locks, call dmu_tx_wait(),
998f864f99eSPrakash Surya * and try again.
99969962b56SMatthew Ahrens *
1000f864f99eSPrakash Surya * If TXG_NOTHROTTLE is set, this indicates that this tx should not be
1001f864f99eSPrakash Surya * delayed due on the ZFS Write Throttle (see comments in dsl_pool.c for
1002f864f99eSPrakash Surya * details on the throttle). This is used by the VFS operations, after
1003f864f99eSPrakash Surya * they have already called dmu_tx_wait() (though most likely on a
1004f864f99eSPrakash Surya * different tx).
1005fa9e4066Sahrens */
1006fa9e4066Sahrens int
dmu_tx_assign(dmu_tx_t * tx,uint64_t txg_how)1007f864f99eSPrakash Surya dmu_tx_assign(dmu_tx_t *tx, uint64_t txg_how)
1008fa9e4066Sahrens {
1009fa9e4066Sahrens int err;
1010fa9e4066Sahrens
1011fa9e4066Sahrens ASSERT(tx->tx_txg == 0);
1012f864f99eSPrakash Surya ASSERT0(txg_how & ~(TXG_WAIT | TXG_NOTHROTTLE));
1013fa9e4066Sahrens ASSERT(!dsl_pool_sync_context(tx->tx_pool));
1014fa9e4066Sahrens
10153b2aab18SMatthew Ahrens /* If we might wait, we must not hold the config lock. */
1016f864f99eSPrakash Surya IMPLY((txg_how & TXG_WAIT), !dsl_pool_config_held(tx->tx_pool));
10173b2aab18SMatthew Ahrens
1018f864f99eSPrakash Surya if ((txg_how & TXG_NOTHROTTLE))
1019f864f99eSPrakash Surya tx->tx_dirty_delayed = B_TRUE;
102069962b56SMatthew Ahrens
10218a2f1b91Sahrens while ((err = dmu_tx_try_assign(tx, txg_how)) != 0) {
10228a2f1b91Sahrens dmu_tx_unassign(tx);
1023fa9e4066Sahrens
1024f864f99eSPrakash Surya if (err != ERESTART || !(txg_how & TXG_WAIT))
1025fa9e4066Sahrens return (err);
1026fa9e4066Sahrens
10278a2f1b91Sahrens dmu_tx_wait(tx);
1028fa9e4066Sahrens }
1029fa9e4066Sahrens
1030fa9e4066Sahrens txg_rele_to_quiesce(&tx->tx_txgh);
1031fa9e4066Sahrens
1032fa9e4066Sahrens return (0);
1033fa9e4066Sahrens }
1034fa9e4066Sahrens
10358a2f1b91Sahrens void
dmu_tx_wait(dmu_tx_t * tx)10368a2f1b91Sahrens dmu_tx_wait(dmu_tx_t *tx)
10378a2f1b91Sahrens {
10380a4e9518Sgw spa_t *spa = tx->tx_pool->dp_spa;
103969962b56SMatthew Ahrens dsl_pool_t *dp = tx->tx_pool;
10400a4e9518Sgw
10418a2f1b91Sahrens ASSERT(tx->tx_txg == 0);
10423b2aab18SMatthew Ahrens ASSERT(!dsl_pool_config_held(tx->tx_pool));
10438a2f1b91Sahrens
104469962b56SMatthew Ahrens if (tx->tx_wait_dirty) {
104569962b56SMatthew Ahrens /*
104669962b56SMatthew Ahrens * dmu_tx_try_assign() has determined that we need to wait
104769962b56SMatthew Ahrens * because we've consumed much or all of the dirty buffer
104869962b56SMatthew Ahrens * space.
104969962b56SMatthew Ahrens */
105069962b56SMatthew Ahrens mutex_enter(&dp->dp_lock);
105169962b56SMatthew Ahrens while (dp->dp_dirty_total >= zfs_dirty_data_max)
105269962b56SMatthew Ahrens cv_wait(&dp->dp_spaceavail_cv, &dp->dp_lock);
105369962b56SMatthew Ahrens uint64_t dirty = dp->dp_dirty_total;
105469962b56SMatthew Ahrens mutex_exit(&dp->dp_lock);
105569962b56SMatthew Ahrens
105669962b56SMatthew Ahrens dmu_tx_delay(tx, dirty);
105769962b56SMatthew Ahrens
105869962b56SMatthew Ahrens tx->tx_wait_dirty = B_FALSE;
105969962b56SMatthew Ahrens
106069962b56SMatthew Ahrens /*
1061f864f99eSPrakash Surya * Note: setting tx_dirty_delayed only has effect if the
1062f864f99eSPrakash Surya * caller used TX_WAIT. Otherwise they are going to
1063f864f99eSPrakash Surya * destroy this tx and try again. The common case,
1064f864f99eSPrakash Surya * zfs_write(), uses TX_WAIT.
106569962b56SMatthew Ahrens */
1066f864f99eSPrakash Surya tx->tx_dirty_delayed = B_TRUE;
106769962b56SMatthew Ahrens } else if (spa_suspended(spa) || tx->tx_lasttried_txg == 0) {
106869962b56SMatthew Ahrens /*
106969962b56SMatthew Ahrens * If the pool is suspended we need to wait until it
107069962b56SMatthew Ahrens * is resumed. Note that it's possible that the pool
107169962b56SMatthew Ahrens * has become active after this thread has tried to
107269962b56SMatthew Ahrens * obtain a tx. If that's the case then tx_lasttried_txg
107369962b56SMatthew Ahrens * would not have been set.
107469962b56SMatthew Ahrens */
107569962b56SMatthew Ahrens txg_wait_synced(dp, spa_last_synced_txg(spa) + 1);
10760a4e9518Sgw } else if (tx->tx_needassign_txh) {
107769962b56SMatthew Ahrens /*
107869962b56SMatthew Ahrens * A dnode is assigned to the quiescing txg. Wait for its
107969962b56SMatthew Ahrens * transaction to complete.
108069962b56SMatthew Ahrens */
10818a2f1b91Sahrens dnode_t *dn = tx->tx_needassign_txh->txh_dnode;
10828a2f1b91Sahrens
10838a2f1b91Sahrens mutex_enter(&dn->dn_mtx);
10848a2f1b91Sahrens while (dn->dn_assigned_txg == tx->tx_lasttried_txg - 1)
10858a2f1b91Sahrens cv_wait(&dn->dn_notxholds, &dn->dn_mtx);
10868a2f1b91Sahrens mutex_exit(&dn->dn_mtx);
10878a2f1b91Sahrens tx->tx_needassign_txh = NULL;
10888a2f1b91Sahrens } else {
1089fa41d87dSSerapheim Dimitropoulos /*
1090fa41d87dSSerapheim Dimitropoulos * If we have a lot of dirty data just wait until we sync
1091fa41d87dSSerapheim Dimitropoulos * out a TXG at which point we'll hopefully have synced
1092fa41d87dSSerapheim Dimitropoulos * a portion of the changes.
1093fa41d87dSSerapheim Dimitropoulos */
1094fa41d87dSSerapheim Dimitropoulos txg_wait_synced(dp, spa_last_synced_txg(spa) + 1);
10958a2f1b91Sahrens }
10968a2f1b91Sahrens }
10978a2f1b91Sahrens
10980c779ad4SMatthew Ahrens static void
dmu_tx_destroy(dmu_tx_t * tx)10990c779ad4SMatthew Ahrens dmu_tx_destroy(dmu_tx_t *tx)
1100fa9e4066Sahrens {
11018a2f1b91Sahrens dmu_tx_hold_t *txh;
1102fa9e4066Sahrens
11030c779ad4SMatthew Ahrens while ((txh = list_head(&tx->tx_holds)) != NULL) {
11040c779ad4SMatthew Ahrens dnode_t *dn = txh->txh_dnode;
11050c779ad4SMatthew Ahrens
11060c779ad4SMatthew Ahrens list_remove(&tx->tx_holds, txh);
1107*e914ace2STim Schumacher zfs_refcount_destroy_many(&txh->txh_space_towrite,
1108*e914ace2STim Schumacher zfs_refcount_count(&txh->txh_space_towrite));
1109*e914ace2STim Schumacher zfs_refcount_destroy_many(&txh->txh_memory_tohold,
1110*e914ace2STim Schumacher zfs_refcount_count(&txh->txh_memory_tohold));
11110c779ad4SMatthew Ahrens kmem_free(txh, sizeof (dmu_tx_hold_t));
11120c779ad4SMatthew Ahrens if (dn != NULL)
11130c779ad4SMatthew Ahrens dnode_rele(dn, tx);
11140c779ad4SMatthew Ahrens }
11150c779ad4SMatthew Ahrens
11160c779ad4SMatthew Ahrens list_destroy(&tx->tx_callbacks);
11170c779ad4SMatthew Ahrens list_destroy(&tx->tx_holds);
11180c779ad4SMatthew Ahrens kmem_free(tx, sizeof (dmu_tx_t));
11190c779ad4SMatthew Ahrens }
11200c779ad4SMatthew Ahrens
11210c779ad4SMatthew Ahrens void
dmu_tx_commit(dmu_tx_t * tx)11220c779ad4SMatthew Ahrens dmu_tx_commit(dmu_tx_t *tx)
11230c779ad4SMatthew Ahrens {
1124fa9e4066Sahrens ASSERT(tx->tx_txg != 0);
1125fa9e4066Sahrens
11263e30c24aSWill Andrews /*
11273e30c24aSWill Andrews * Go through the transaction's hold list and remove holds on
11283e30c24aSWill Andrews * associated dnodes, notifying waiters if no holds remain.
11293e30c24aSWill Andrews */
11300c779ad4SMatthew Ahrens for (dmu_tx_hold_t *txh = list_head(&tx->tx_holds); txh != NULL;
11310c779ad4SMatthew Ahrens txh = list_next(&tx->tx_holds, txh)) {
11328a2f1b91Sahrens dnode_t *dn = txh->txh_dnode;
1133fa9e4066Sahrens
1134fa9e4066Sahrens if (dn == NULL)
1135fa9e4066Sahrens continue;
11360c779ad4SMatthew Ahrens
1137fa9e4066Sahrens mutex_enter(&dn->dn_mtx);
1138fa9e4066Sahrens ASSERT3U(dn->dn_assigned_txg, ==, tx->tx_txg);
1139fa9e4066Sahrens
1140*e914ace2STim Schumacher if (zfs_refcount_remove(&dn->dn_tx_holds, tx) == 0) {
1141fa9e4066Sahrens dn->dn_assigned_txg = 0;
1142fa9e4066Sahrens cv_broadcast(&dn->dn_notxholds);
1143fa9e4066Sahrens }
1144fa9e4066Sahrens mutex_exit(&dn->dn_mtx);
1145fa9e4066Sahrens }
1146fa9e4066Sahrens
11478a2f1b91Sahrens if (tx->tx_tempreserve_cookie)
1148fa9e4066Sahrens dsl_dir_tempreserve_clear(tx->tx_tempreserve_cookie, tx);
1149fa9e4066Sahrens
1150d20e665cSRicardo M. Correia if (!list_is_empty(&tx->tx_callbacks))
1151d20e665cSRicardo M. Correia txg_register_callbacks(&tx->tx_txgh, &tx->tx_callbacks);
1152d20e665cSRicardo M. Correia
1153fa9e4066Sahrens if (tx->tx_anyobj == FALSE)
1154fa9e4066Sahrens txg_rele_to_sync(&tx->tx_txgh);
1155d20e665cSRicardo M. Correia
11560c779ad4SMatthew Ahrens dmu_tx_destroy(tx);
1157fa9e4066Sahrens }
1158fa9e4066Sahrens
1159fa9e4066Sahrens void
dmu_tx_abort(dmu_tx_t * tx)1160fa9e4066Sahrens dmu_tx_abort(dmu_tx_t *tx)
1161fa9e4066Sahrens {
1162fa9e4066Sahrens ASSERT(tx->tx_txg == 0);
1163fa9e4066Sahrens
1164d20e665cSRicardo M. Correia /*
1165d20e665cSRicardo M. Correia * Call any registered callbacks with an error code.
1166d20e665cSRicardo M. Correia */
1167d20e665cSRicardo M. Correia if (!list_is_empty(&tx->tx_callbacks))
1168d20e665cSRicardo M. Correia dmu_tx_do_callbacks(&tx->tx_callbacks, ECANCELED);
1169d20e665cSRicardo M. Correia
11700c779ad4SMatthew Ahrens dmu_tx_destroy(tx);
1171fa9e4066Sahrens }
1172fa9e4066Sahrens
1173fa9e4066Sahrens uint64_t
dmu_tx_get_txg(dmu_tx_t * tx)1174fa9e4066Sahrens dmu_tx_get_txg(dmu_tx_t *tx)
1175fa9e4066Sahrens {
1176fa9e4066Sahrens ASSERT(tx->tx_txg != 0);
1177fa9e4066Sahrens return (tx->tx_txg);
1178fa9e4066Sahrens }
1179d20e665cSRicardo M. Correia
11803b2aab18SMatthew Ahrens dsl_pool_t *
dmu_tx_pool(dmu_tx_t * tx)11813b2aab18SMatthew Ahrens dmu_tx_pool(dmu_tx_t *tx)
11823b2aab18SMatthew Ahrens {
11833b2aab18SMatthew Ahrens ASSERT(tx->tx_pool != NULL);
11843b2aab18SMatthew Ahrens return (tx->tx_pool);
11853b2aab18SMatthew Ahrens }
11863b2aab18SMatthew Ahrens
1187d20e665cSRicardo M. Correia void
dmu_tx_callback_register(dmu_tx_t * tx,dmu_tx_callback_func_t * func,void * data)1188d20e665cSRicardo M. Correia dmu_tx_callback_register(dmu_tx_t *tx, dmu_tx_callback_func_t *func, void *data)
1189d20e665cSRicardo M. Correia {
1190d20e665cSRicardo M. Correia dmu_tx_callback_t *dcb;
1191d20e665cSRicardo M. Correia
1192d20e665cSRicardo M. Correia dcb = kmem_alloc(sizeof (dmu_tx_callback_t), KM_SLEEP);
1193d20e665cSRicardo M. Correia
1194d20e665cSRicardo M. Correia dcb->dcb_func = func;
1195d20e665cSRicardo M. Correia dcb->dcb_data = data;
1196d20e665cSRicardo M. Correia
1197d20e665cSRicardo M. Correia list_insert_tail(&tx->tx_callbacks, dcb);
1198d20e665cSRicardo M. Correia }
1199d20e665cSRicardo M. Correia
1200d20e665cSRicardo M. Correia /*
1201d20e665cSRicardo M. Correia * Call all the commit callbacks on a list, with a given error code.
1202d20e665cSRicardo M. Correia */
1203d20e665cSRicardo M. Correia void
dmu_tx_do_callbacks(list_t * cb_list,int error)1204d20e665cSRicardo M. Correia dmu_tx_do_callbacks(list_t *cb_list, int error)
1205d20e665cSRicardo M. Correia {
1206d20e665cSRicardo M. Correia dmu_tx_callback_t *dcb;
1207d20e665cSRicardo M. Correia
12080c779ad4SMatthew Ahrens while ((dcb = list_head(cb_list)) != NULL) {
1209d20e665cSRicardo M. Correia list_remove(cb_list, dcb);
1210d20e665cSRicardo M. Correia dcb->dcb_func(dcb->dcb_data, error);
1211d20e665cSRicardo M. Correia kmem_free(dcb, sizeof (dmu_tx_callback_t));
1212d20e665cSRicardo M. Correia }
1213d20e665cSRicardo M. Correia }
12140a586ceaSMark Shellenbaum
12150a586ceaSMark Shellenbaum /*
12160a586ceaSMark Shellenbaum * Interface to hold a bunch of attributes.
12170a586ceaSMark Shellenbaum * used for creating new files.
12180a586ceaSMark Shellenbaum * attrsize is the total size of all attributes
12190a586ceaSMark Shellenbaum * to be added during object creation
12200a586ceaSMark Shellenbaum *
12210a586ceaSMark Shellenbaum * For updating/adding a single attribute dmu_tx_hold_sa() should be used.
12220a586ceaSMark Shellenbaum */
12230a586ceaSMark Shellenbaum
12240a586ceaSMark Shellenbaum /*
12250a586ceaSMark Shellenbaum * hold necessary attribute name for attribute registration.
12260a586ceaSMark Shellenbaum * should be a very rare case where this is needed. If it does
12270a586ceaSMark Shellenbaum * happen it would only happen on the first write to the file system.
12280a586ceaSMark Shellenbaum */
12290a586ceaSMark Shellenbaum static void
dmu_tx_sa_registration_hold(sa_os_t * sa,dmu_tx_t * tx)12300a586ceaSMark Shellenbaum dmu_tx_sa_registration_hold(sa_os_t *sa, dmu_tx_t *tx)
12310a586ceaSMark Shellenbaum {
12320a586ceaSMark Shellenbaum if (!sa->sa_need_attr_registration)
12330a586ceaSMark Shellenbaum return;
12340a586ceaSMark Shellenbaum
123561e255ceSMatthew Ahrens for (int i = 0; i != sa->sa_num_attrs; i++) {
12360a586ceaSMark Shellenbaum if (!sa->sa_attr_table[i].sa_registered) {
12370a586ceaSMark Shellenbaum if (sa->sa_reg_attr_obj)
12380a586ceaSMark Shellenbaum dmu_tx_hold_zap(tx, sa->sa_reg_attr_obj,
12390a586ceaSMark Shellenbaum B_TRUE, sa->sa_attr_table[i].sa_name);
12400a586ceaSMark Shellenbaum else
12410a586ceaSMark Shellenbaum dmu_tx_hold_zap(tx, DMU_NEW_OBJECT,
12420a586ceaSMark Shellenbaum B_TRUE, sa->sa_attr_table[i].sa_name);
12430a586ceaSMark Shellenbaum }
12440a586ceaSMark Shellenbaum }
12450a586ceaSMark Shellenbaum }
12460a586ceaSMark Shellenbaum
12470a586ceaSMark Shellenbaum void
dmu_tx_hold_spill(dmu_tx_t * tx,uint64_t object)12480a586ceaSMark Shellenbaum dmu_tx_hold_spill(dmu_tx_t *tx, uint64_t object)
12490a586ceaSMark Shellenbaum {
125054811da5SToomas Soome dmu_tx_hold_t *txh;
12510a586ceaSMark Shellenbaum
125254811da5SToomas Soome txh = dmu_tx_hold_object_impl(tx, tx->tx_objset, object,
125354811da5SToomas Soome THT_SPILL, 0, 0);
125454811da5SToomas Soome if (txh != NULL)
1255*e914ace2STim Schumacher (void) zfs_refcount_add_many(&txh->txh_space_towrite,
125654811da5SToomas Soome SPA_OLD_MAXBLOCKSIZE, FTAG);
12570a586ceaSMark Shellenbaum }
12580a586ceaSMark Shellenbaum
12590a586ceaSMark Shellenbaum void
dmu_tx_hold_sa_create(dmu_tx_t * tx,int attrsize)12600a586ceaSMark Shellenbaum dmu_tx_hold_sa_create(dmu_tx_t *tx, int attrsize)
12610a586ceaSMark Shellenbaum {
12620a586ceaSMark Shellenbaum sa_os_t *sa = tx->tx_objset->os_sa;
12630a586ceaSMark Shellenbaum
12640a586ceaSMark Shellenbaum dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
12650a586ceaSMark Shellenbaum
12660a586ceaSMark Shellenbaum if (tx->tx_objset->os_sa->sa_master_obj == 0)
12670a586ceaSMark Shellenbaum return;
12680a586ceaSMark Shellenbaum
126961e255ceSMatthew Ahrens if (tx->tx_objset->os_sa->sa_layout_attr_obj) {
12700a586ceaSMark Shellenbaum dmu_tx_hold_zap(tx, sa->sa_layout_attr_obj, B_TRUE, NULL);
127161e255ceSMatthew Ahrens } else {
12720a586ceaSMark Shellenbaum dmu_tx_hold_zap(tx, sa->sa_master_obj, B_TRUE, SA_LAYOUTS);
12730a586ceaSMark Shellenbaum dmu_tx_hold_zap(tx, sa->sa_master_obj, B_TRUE, SA_REGISTRY);
12740a586ceaSMark Shellenbaum dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
12750a586ceaSMark Shellenbaum dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
12760a586ceaSMark Shellenbaum }
12770a586ceaSMark Shellenbaum
12780a586ceaSMark Shellenbaum dmu_tx_sa_registration_hold(sa, tx);
12790a586ceaSMark Shellenbaum
128054811da5SToomas Soome if (attrsize <= DN_OLD_MAX_BONUSLEN && !sa->sa_force_spill)
12810a586ceaSMark Shellenbaum return;
12820a586ceaSMark Shellenbaum
12830a586ceaSMark Shellenbaum (void) dmu_tx_hold_object_impl(tx, tx->tx_objset, DMU_NEW_OBJECT,
12840a586ceaSMark Shellenbaum THT_SPILL, 0, 0);
12850a586ceaSMark Shellenbaum }
12860a586ceaSMark Shellenbaum
12870a586ceaSMark Shellenbaum /*
12880a586ceaSMark Shellenbaum * Hold SA attribute
12890a586ceaSMark Shellenbaum *
12900a586ceaSMark Shellenbaum * dmu_tx_hold_sa(dmu_tx_t *tx, sa_handle_t *, attribute, add, size)
12910a586ceaSMark Shellenbaum *
12920a586ceaSMark Shellenbaum * variable_size is the total size of all variable sized attributes
12930a586ceaSMark Shellenbaum * passed to this function. It is not the total size of all
12940a586ceaSMark Shellenbaum * variable size attributes that *may* exist on this object.
12950a586ceaSMark Shellenbaum */
12960a586ceaSMark Shellenbaum void
dmu_tx_hold_sa(dmu_tx_t * tx,sa_handle_t * hdl,boolean_t may_grow)12970a586ceaSMark Shellenbaum dmu_tx_hold_sa(dmu_tx_t *tx, sa_handle_t *hdl, boolean_t may_grow)
12980a586ceaSMark Shellenbaum {
12990a586ceaSMark Shellenbaum uint64_t object;
13000a586ceaSMark Shellenbaum sa_os_t *sa = tx->tx_objset->os_sa;
13010a586ceaSMark Shellenbaum
13020a586ceaSMark Shellenbaum ASSERT(hdl != NULL);
13030a586ceaSMark Shellenbaum
13040a586ceaSMark Shellenbaum object = sa_handle_object(hdl);
13050a586ceaSMark Shellenbaum
13060a586ceaSMark Shellenbaum dmu_tx_hold_bonus(tx, object);
13070a586ceaSMark Shellenbaum
13080a586ceaSMark Shellenbaum if (tx->tx_objset->os_sa->sa_master_obj == 0)
13090a586ceaSMark Shellenbaum return;
13100a586ceaSMark Shellenbaum
13110a586ceaSMark Shellenbaum if (tx->tx_objset->os_sa->sa_reg_attr_obj == 0 ||
13120a586ceaSMark Shellenbaum tx->tx_objset->os_sa->sa_layout_attr_obj == 0) {
13130a586ceaSMark Shellenbaum dmu_tx_hold_zap(tx, sa->sa_master_obj, B_TRUE, SA_LAYOUTS);
13140a586ceaSMark Shellenbaum dmu_tx_hold_zap(tx, sa->sa_master_obj, B_TRUE, SA_REGISTRY);
13150a586ceaSMark Shellenbaum dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
13160a586ceaSMark Shellenbaum dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
13170a586ceaSMark Shellenbaum }
13180a586ceaSMark Shellenbaum
13190a586ceaSMark Shellenbaum dmu_tx_sa_registration_hold(sa, tx);
13200a586ceaSMark Shellenbaum
13210a586ceaSMark Shellenbaum if (may_grow && tx->tx_objset->os_sa->sa_layout_attr_obj)
13220a586ceaSMark Shellenbaum dmu_tx_hold_zap(tx, sa->sa_layout_attr_obj, B_TRUE, NULL);
13230a586ceaSMark Shellenbaum
1324744947dcSTom Erickson if (sa->sa_force_spill || may_grow || hdl->sa_spill) {
13250a586ceaSMark Shellenbaum ASSERT(tx->tx_txg == 0);
13260a586ceaSMark Shellenbaum dmu_tx_hold_spill(tx, object);
1327744947dcSTom Erickson } else {
1328744947dcSTom Erickson dmu_buf_impl_t *db = (dmu_buf_impl_t *)hdl->sa_bonus;
1329744947dcSTom Erickson dnode_t *dn;
1330744947dcSTom Erickson
1331744947dcSTom Erickson DB_DNODE_ENTER(db);
1332744947dcSTom Erickson dn = DB_DNODE(db);
1333744947dcSTom Erickson if (dn->dn_have_spill) {
1334744947dcSTom Erickson ASSERT(tx->tx_txg == 0);
1335744947dcSTom Erickson dmu_tx_hold_spill(tx, object);
1336744947dcSTom Erickson }
1337744947dcSTom Erickson DB_DNODE_EXIT(db);
13380a586ceaSMark Shellenbaum }
13390a586ceaSMark Shellenbaum }
1340