]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/dmu_tx.c
ABD style cleanups
[mirror_zfs.git] / module / zfs / dmu_tx.c
CommitLineData
34dc7c2f
BB
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21/*
428870ff 22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
22cd4a46 23 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
2bce8049 24 * Copyright (c) 2012, 2016 by Delphix. All rights reserved.
22cd4a46 25 */
34dc7c2f 26
34dc7c2f
BB
27#include <sys/dmu.h>
28#include <sys/dmu_impl.h>
29#include <sys/dbuf.h>
30#include <sys/dmu_tx.h>
31#include <sys/dmu_objset.h>
32#include <sys/dsl_dataset.h> /* for dsl_dataset_block_freeable() */
33#include <sys/dsl_dir.h> /* for dsl_dir_tempreserve_*() */
34#include <sys/dsl_pool.h>
35#include <sys/zap_impl.h> /* for fzap_default_block_shift */
36#include <sys/spa.h>
428870ff
BB
37#include <sys/sa.h>
38#include <sys/sa_impl.h>
34dc7c2f 39#include <sys/zfs_context.h>
428870ff 40#include <sys/varargs.h>
49ee64e5 41#include <sys/trace_dmu.h>
34dc7c2f
BB
42
43typedef void (*dmu_tx_hold_func_t)(dmu_tx_t *tx, struct dnode *dn,
44 uint64_t arg1, uint64_t arg2);
45
570827e1
BB
46dmu_tx_stats_t dmu_tx_stats = {
47 { "dmu_tx_assigned", KSTAT_DATA_UINT64 },
48 { "dmu_tx_delay", KSTAT_DATA_UINT64 },
49 { "dmu_tx_error", KSTAT_DATA_UINT64 },
50 { "dmu_tx_suspended", KSTAT_DATA_UINT64 },
51 { "dmu_tx_group", KSTAT_DATA_UINT64 },
570827e1
BB
52 { "dmu_tx_memory_reserve", KSTAT_DATA_UINT64 },
53 { "dmu_tx_memory_reclaim", KSTAT_DATA_UINT64 },
570827e1 54 { "dmu_tx_dirty_throttle", KSTAT_DATA_UINT64 },
e8b96c60
MA
55 { "dmu_tx_dirty_delay", KSTAT_DATA_UINT64 },
56 { "dmu_tx_dirty_over_max", KSTAT_DATA_UINT64 },
570827e1
BB
57 { "dmu_tx_quota", KSTAT_DATA_UINT64 },
58};
59
60static kstat_t *dmu_tx_ksp;
34dc7c2f
BB
61
62dmu_tx_t *
63dmu_tx_create_dd(dsl_dir_t *dd)
64{
79c76d5b 65 dmu_tx_t *tx = kmem_zalloc(sizeof (dmu_tx_t), KM_SLEEP);
34dc7c2f 66 tx->tx_dir = dd;
6f1ffb06 67 if (dd != NULL)
34dc7c2f
BB
68 tx->tx_pool = dd->dd_pool;
69 list_create(&tx->tx_holds, sizeof (dmu_tx_hold_t),
70 offsetof(dmu_tx_hold_t, txh_node));
428870ff
BB
71 list_create(&tx->tx_callbacks, sizeof (dmu_tx_callback_t),
72 offsetof(dmu_tx_callback_t, dcb_node));
e8b96c60 73 tx->tx_start = gethrtime();
1c5de20a 74#ifdef DEBUG_DMU_TX
34dc7c2f
BB
75 refcount_create(&tx->tx_space_written);
76 refcount_create(&tx->tx_space_freed);
77#endif
78 return (tx);
79}
80
81dmu_tx_t *
82dmu_tx_create(objset_t *os)
83{
428870ff 84 dmu_tx_t *tx = dmu_tx_create_dd(os->os_dsl_dataset->ds_dir);
34dc7c2f 85 tx->tx_objset = os;
428870ff 86 tx->tx_lastsnap_txg = dsl_dataset_prev_snap_txg(os->os_dsl_dataset);
34dc7c2f
BB
87 return (tx);
88}
89
90dmu_tx_t *
91dmu_tx_create_assigned(struct dsl_pool *dp, uint64_t txg)
92{
93 dmu_tx_t *tx = dmu_tx_create_dd(NULL);
94
95 ASSERT3U(txg, <=, dp->dp_tx.tx_open_txg);
96 tx->tx_pool = dp;
97 tx->tx_txg = txg;
98 tx->tx_anyobj = TRUE;
99
100 return (tx);
101}
102
103int
104dmu_tx_is_syncing(dmu_tx_t *tx)
105{
106 return (tx->tx_anyobj);
107}
108
109int
110dmu_tx_private_ok(dmu_tx_t *tx)
111{
112 return (tx->tx_anyobj);
113}
114
115static dmu_tx_hold_t *
0eef1bde 116dmu_tx_hold_dnode_impl(dmu_tx_t *tx, dnode_t *dn, enum dmu_tx_hold_type type,
117 uint64_t arg1, uint64_t arg2)
34dc7c2f
BB
118{
119 dmu_tx_hold_t *txh;
34dc7c2f 120
0eef1bde 121 if (dn != NULL) {
122 refcount_add(&dn->dn_holds, tx);
123 if (tx->tx_txg != 0) {
34dc7c2f
BB
124 mutex_enter(&dn->dn_mtx);
125 /*
126 * dn->dn_assigned_txg == tx->tx_txg doesn't pose a
127 * problem, but there's no way for it to happen (for
128 * now, at least).
129 */
130 ASSERT(dn->dn_assigned_txg == 0);
131 dn->dn_assigned_txg = tx->tx_txg;
132 (void) refcount_add(&dn->dn_tx_holds, tx);
133 mutex_exit(&dn->dn_mtx);
134 }
135 }
136
79c76d5b 137 txh = kmem_zalloc(sizeof (dmu_tx_hold_t), KM_SLEEP);
34dc7c2f
BB
138 txh->txh_tx = tx;
139 txh->txh_dnode = dn;
f85c06be
GM
140 refcount_create(&txh->txh_space_towrite);
141 refcount_create(&txh->txh_space_tofree);
142 refcount_create(&txh->txh_space_tooverwrite);
143 refcount_create(&txh->txh_space_tounref);
144 refcount_create(&txh->txh_memory_tohold);
145 refcount_create(&txh->txh_fudge);
1c5de20a 146#ifdef DEBUG_DMU_TX
34dc7c2f
BB
147 txh->txh_type = type;
148 txh->txh_arg1 = arg1;
149 txh->txh_arg2 = arg2;
150#endif
151 list_insert_tail(&tx->tx_holds, txh);
152
153 return (txh);
154}
155
0eef1bde 156static dmu_tx_hold_t *
157dmu_tx_hold_object_impl(dmu_tx_t *tx, objset_t *os, uint64_t object,
158 enum dmu_tx_hold_type type, uint64_t arg1, uint64_t arg2)
159{
160 dnode_t *dn = NULL;
161 dmu_tx_hold_t *txh;
162 int err;
163
164 if (object != DMU_NEW_OBJECT) {
165 err = dnode_hold(os, object, FTAG, &dn);
166 if (err) {
167 tx->tx_err = err;
168 return (NULL);
169 }
170 }
171 txh = dmu_tx_hold_dnode_impl(tx, dn, type, arg1, arg2);
172 if (dn != NULL)
173 dnode_rele(dn, FTAG);
174 return (txh);
175}
176
34dc7c2f 177void
0eef1bde 178dmu_tx_add_new_object(dmu_tx_t *tx, objset_t *os, dnode_t *dn)
34dc7c2f
BB
179{
180 /*
181 * If we're syncing, they can manipulate any object anyhow, and
182 * the hold on the dnode_t can cause problems.
183 */
0eef1bde 184 if (!dmu_tx_is_syncing(tx))
185 (void) dmu_tx_hold_dnode_impl(tx, dn, THT_NEWOBJECT, 0, 0);
34dc7c2f
BB
186}
187
188static int
189dmu_tx_check_ioerr(zio_t *zio, dnode_t *dn, int level, uint64_t blkid)
190{
191 int err;
192 dmu_buf_impl_t *db;
193
194 rw_enter(&dn->dn_struct_rwlock, RW_READER);
195 db = dbuf_hold_level(dn, level, blkid, FTAG);
196 rw_exit(&dn->dn_struct_rwlock);
197 if (db == NULL)
2e528b49 198 return (SET_ERROR(EIO));
34dc7c2f
BB
199 err = dbuf_read(db, zio, DB_RF_CANFAIL | DB_RF_NOPREFETCH);
200 dbuf_rele(db, FTAG);
201 return (err);
202}
203
9babb374 204static void
428870ff
BB
205dmu_tx_count_twig(dmu_tx_hold_t *txh, dnode_t *dn, dmu_buf_impl_t *db,
206 int level, uint64_t blkid, boolean_t freeable, uint64_t *history)
9babb374 207{
428870ff
BB
208 objset_t *os = dn->dn_objset;
209 dsl_dataset_t *ds = os->os_dsl_dataset;
210 int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
211 dmu_buf_impl_t *parent = NULL;
212 blkptr_t *bp = NULL;
213 uint64_t space;
214
215 if (level >= dn->dn_nlevels || history[level] == blkid)
9babb374
BB
216 return;
217
428870ff 218 history[level] = blkid;
9babb374 219
428870ff
BB
220 space = (level == 0) ? dn->dn_datablksz : (1ULL << dn->dn_indblkshift);
221
222 if (db == NULL || db == dn->dn_dbuf) {
223 ASSERT(level != 0);
224 db = NULL;
225 } else {
572e2857 226 ASSERT(DB_DNODE(db) == dn);
428870ff
BB
227 ASSERT(db->db_level == level);
228 ASSERT(db->db.db_size == space);
229 ASSERT(db->db_blkid == blkid);
230 bp = db->db_blkptr;
231 parent = db->db_parent;
9babb374
BB
232 }
233
428870ff
BB
234 freeable = (bp && (freeable ||
235 dsl_dataset_block_freeable(ds, bp, bp->blk_birth)));
9babb374 236
f85c06be
GM
237 if (freeable) {
238 (void) refcount_add_many(&txh->txh_space_tooverwrite,
239 space, FTAG);
240 } else {
241 (void) refcount_add_many(&txh->txh_space_towrite,
242 space, FTAG);
243 }
244
245 if (bp) {
246 (void) refcount_add_many(&txh->txh_space_tounref,
247 bp_get_dsize(os->os_spa, bp), FTAG);
248 }
428870ff
BB
249
250 dmu_tx_count_twig(txh, dn, parent, level + 1,
251 blkid >> epbs, freeable, history);
9babb374
BB
252}
253
34dc7c2f
BB
254/* ARGSUSED */
255static void
256dmu_tx_count_write(dmu_tx_hold_t *txh, uint64_t off, uint64_t len)
257{
258 dnode_t *dn = txh->txh_dnode;
259 uint64_t start, end, i;
260 int min_bs, max_bs, min_ibs, max_ibs, epbs, bits;
261 int err = 0;
d6320ddb 262 int l;
34dc7c2f
BB
263
264 if (len == 0)
265 return;
266
267 min_bs = SPA_MINBLOCKSHIFT;
f1512ee6 268 max_bs = highbit64(txh->txh_tx->tx_objset->os_recordsize) - 1;
34dc7c2f
BB
269 min_ibs = DN_MIN_INDBLKSHIFT;
270 max_ibs = DN_MAX_INDBLKSHIFT;
271
34dc7c2f 272 if (dn) {
428870ff 273 uint64_t history[DN_MAX_LEVELS];
9babb374
BB
274 int nlvls = dn->dn_nlevels;
275 int delta;
276
277 /*
278 * For i/o error checking, read the first and last level-0
279 * blocks (if they are not aligned), and all the level-1 blocks.
280 */
34dc7c2f 281 if (dn->dn_maxblkid == 0) {
9babb374
BB
282 delta = dn->dn_datablksz;
283 start = (off < dn->dn_datablksz) ? 0 : 1;
284 end = (off+len <= dn->dn_datablksz) ? 0 : 1;
285 if (start == 0 && (off > 0 || len < dn->dn_datablksz)) {
b128c09f
BB
286 err = dmu_tx_check_ioerr(NULL, dn, 0, 0);
287 if (err)
288 goto out;
9babb374 289 delta -= off;
b128c09f 290 }
34dc7c2f
BB
291 } else {
292 zio_t *zio = zio_root(dn->dn_objset->os_spa,
293 NULL, NULL, ZIO_FLAG_CANFAIL);
294
295 /* first level-0 block */
296 start = off >> dn->dn_datablkshift;
297 if (P2PHASE(off, dn->dn_datablksz) ||
298 len < dn->dn_datablksz) {
299 err = dmu_tx_check_ioerr(zio, dn, 0, start);
300 if (err)
301 goto out;
302 }
303
304 /* last level-0 block */
305 end = (off+len-1) >> dn->dn_datablkshift;
b128c09f 306 if (end != start && end <= dn->dn_maxblkid &&
34dc7c2f
BB
307 P2PHASE(off+len, dn->dn_datablksz)) {
308 err = dmu_tx_check_ioerr(zio, dn, 0, end);
309 if (err)
310 goto out;
311 }
312
313 /* level-1 blocks */
9babb374
BB
314 if (nlvls > 1) {
315 int shft = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
316 for (i = (start>>shft)+1; i < end>>shft; i++) {
34dc7c2f
BB
317 err = dmu_tx_check_ioerr(zio, dn, 1, i);
318 if (err)
319 goto out;
320 }
321 }
322
323 err = zio_wait(zio);
324 if (err)
325 goto out;
9babb374 326 delta = P2NPHASE(off, dn->dn_datablksz);
34dc7c2f 327 }
34dc7c2f 328
295304be 329 min_ibs = max_ibs = dn->dn_indblkshift;
9babb374
BB
330 if (dn->dn_maxblkid > 0) {
331 /*
332 * The blocksize can't change,
333 * so we can make a more precise estimate.
334 */
335 ASSERT(dn->dn_datablkshift != 0);
34dc7c2f 336 min_bs = max_bs = dn->dn_datablkshift;
f1512ee6
MA
337 } else {
338 /*
339 * The blocksize can increase up to the recordsize,
340 * or if it is already more than the recordsize,
341 * up to the next power of 2.
342 */
343 min_bs = highbit64(dn->dn_datablksz - 1);
344 max_bs = MAX(max_bs, highbit64(dn->dn_datablksz - 1));
9babb374
BB
345 }
346
347 /*
348 * If this write is not off the end of the file
349 * we need to account for overwrites/unref.
350 */
428870ff 351 if (start <= dn->dn_maxblkid) {
d6320ddb 352 for (l = 0; l < DN_MAX_LEVELS; l++)
428870ff
BB
353 history[l] = -1ULL;
354 }
9babb374 355 while (start <= dn->dn_maxblkid) {
9babb374
BB
356 dmu_buf_impl_t *db;
357
358 rw_enter(&dn->dn_struct_rwlock, RW_READER);
fcff0f35
PD
359 err = dbuf_hold_impl(dn, 0, start,
360 FALSE, FALSE, FTAG, &db);
9babb374 361 rw_exit(&dn->dn_struct_rwlock);
428870ff
BB
362
363 if (err) {
364 txh->txh_tx->tx_err = err;
365 return;
9babb374 366 }
428870ff
BB
367
368 dmu_tx_count_twig(txh, dn, db, 0, start, B_FALSE,
369 history);
9babb374
BB
370 dbuf_rele(db, FTAG);
371 if (++start > end) {
372 /*
373 * Account for new indirects appearing
374 * before this IO gets assigned into a txg.
375 */
376 bits = 64 - min_bs;
377 epbs = min_ibs - SPA_BLKPTRSHIFT;
378 for (bits -= epbs * (nlvls - 1);
f85c06be
GM
379 bits >= 0; bits -= epbs) {
380 (void) refcount_add_many(
381 &txh->txh_fudge,
382 1ULL << max_ibs, FTAG);
383 }
9babb374
BB
384 goto out;
385 }
386 off += delta;
387 if (len >= delta)
388 len -= delta;
389 delta = dn->dn_datablksz;
390 }
34dc7c2f
BB
391 }
392
393 /*
394 * 'end' is the last thing we will access, not one past.
395 * This way we won't overflow when accessing the last byte.
396 */
397 start = P2ALIGN(off, 1ULL << max_bs);
398 end = P2ROUNDUP(off + len, 1ULL << max_bs) - 1;
f85c06be
GM
399 (void) refcount_add_many(&txh->txh_space_towrite,
400 end - start + 1, FTAG);
34dc7c2f
BB
401
402 start >>= min_bs;
403 end >>= min_bs;
404
405 epbs = min_ibs - SPA_BLKPTRSHIFT;
406
407 /*
408 * The object contains at most 2^(64 - min_bs) blocks,
409 * and each indirect level maps 2^epbs.
410 */
411 for (bits = 64 - min_bs; bits >= 0; bits -= epbs) {
412 start >>= epbs;
413 end >>= epbs;
9babb374 414 ASSERT3U(end, >=, start);
f85c06be
GM
415 (void) refcount_add_many(&txh->txh_space_towrite,
416 (end - start + 1) << max_ibs, FTAG);
9babb374
BB
417 if (start != 0) {
418 /*
419 * We also need a new blkid=0 indirect block
420 * to reference any existing file data.
421 */
f85c06be
GM
422 (void) refcount_add_many(&txh->txh_space_towrite,
423 1ULL << max_ibs, FTAG);
9babb374 424 }
34dc7c2f
BB
425 }
426
34dc7c2f 427out:
f85c06be
GM
428 if (refcount_count(&txh->txh_space_towrite) +
429 refcount_count(&txh->txh_space_tooverwrite) >
9babb374 430 2 * DMU_MAX_ACCESS)
2e528b49 431 err = SET_ERROR(EFBIG);
9babb374 432
34dc7c2f
BB
433 if (err)
434 txh->txh_tx->tx_err = err;
435}
436
437static void
438dmu_tx_count_dnode(dmu_tx_hold_t *txh)
439{
440 dnode_t *dn = txh->txh_dnode;
572e2857 441 dnode_t *mdn = DMU_META_DNODE(txh->txh_tx->tx_objset);
34dc7c2f 442 uint64_t space = mdn->dn_datablksz +
05852b34 443 ((uint64_t)(mdn->dn_nlevels-1) << mdn->dn_indblkshift);
34dc7c2f
BB
444
445 if (dn && dn->dn_dbuf->db_blkptr &&
446 dsl_dataset_block_freeable(dn->dn_objset->os_dsl_dataset,
428870ff 447 dn->dn_dbuf->db_blkptr, dn->dn_dbuf->db_blkptr->blk_birth)) {
f85c06be
GM
448 (void) refcount_add_many(&txh->txh_space_tooverwrite,
449 space, FTAG);
450 (void) refcount_add_many(&txh->txh_space_tounref, space, FTAG);
34dc7c2f 451 } else {
f85c06be
GM
452 (void) refcount_add_many(&txh->txh_space_towrite, space, FTAG);
453 if (dn && dn->dn_dbuf->db_blkptr) {
454 (void) refcount_add_many(&txh->txh_space_tounref,
455 space, FTAG);
456 }
34dc7c2f
BB
457 }
458}
459
460void
461dmu_tx_hold_write(dmu_tx_t *tx, uint64_t object, uint64_t off, int len)
462{
463 dmu_tx_hold_t *txh;
464
465 ASSERT(tx->tx_txg == 0);
ded576e2 466 ASSERT(len <= DMU_MAX_ACCESS);
34dc7c2f
BB
467 ASSERT(len == 0 || UINT64_MAX - off >= len - 1);
468
469 txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
470 object, THT_WRITE, off, len);
471 if (txh == NULL)
472 return;
473
474 dmu_tx_count_write(txh, off, len);
475 dmu_tx_count_dnode(txh);
476}
477
0eef1bde 478void
479dmu_tx_hold_write_by_dnode(dmu_tx_t *tx, dnode_t *dn, uint64_t off, int len)
480{
481 dmu_tx_hold_t *txh;
482
483 ASSERT(tx->tx_txg == 0);
484 ASSERT(len <= DMU_MAX_ACCESS);
485 ASSERT(len == 0 || UINT64_MAX - off >= len - 1);
486
487 txh = dmu_tx_hold_dnode_impl(tx, dn, THT_WRITE, off, len);
488 if (txh == NULL)
489 return;
490
491 dmu_tx_count_write(txh, off, len);
492 dmu_tx_count_dnode(txh);
493}
494
34dc7c2f
BB
495static void
496dmu_tx_count_free(dmu_tx_hold_t *txh, uint64_t off, uint64_t len)
497{
b128c09f
BB
498 uint64_t blkid, nblks, lastblk;
499 uint64_t space = 0, unref = 0, skipped = 0;
34dc7c2f
BB
500 dnode_t *dn = txh->txh_dnode;
501 dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset;
502 spa_t *spa = txh->txh_tx->tx_pool->dp_spa;
b128c09f 503 int epbs;
ff80d9b1 504 uint64_t l0span = 0, nl1blks = 0;
34dc7c2f 505
b128c09f 506 if (dn->dn_nlevels == 0)
34dc7c2f
BB
507 return;
508
509 /*
b128c09f 510 * The struct_rwlock protects us against dn_nlevels
34dc7c2f
BB
511 * changing, in case (against all odds) we manage to dirty &
512 * sync out the changes after we check for being dirty.
428870ff 513 * Also, dbuf_hold_impl() wants us to have the struct_rwlock.
34dc7c2f
BB
514 */
515 rw_enter(&dn->dn_struct_rwlock, RW_READER);
b128c09f
BB
516 epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
517 if (dn->dn_maxblkid == 0) {
34dc7c2f
BB
518 if (off == 0 && len >= dn->dn_datablksz) {
519 blkid = 0;
520 nblks = 1;
521 } else {
522 rw_exit(&dn->dn_struct_rwlock);
523 return;
524 }
525 } else {
526 blkid = off >> dn->dn_datablkshift;
b128c09f 527 nblks = (len + dn->dn_datablksz - 1) >> dn->dn_datablkshift;
34dc7c2f 528
383fc4a9 529 if (blkid > dn->dn_maxblkid) {
34dc7c2f
BB
530 rw_exit(&dn->dn_struct_rwlock);
531 return;
532 }
b128c09f 533 if (blkid + nblks > dn->dn_maxblkid)
383fc4a9 534 nblks = dn->dn_maxblkid - blkid + 1;
34dc7c2f 535
34dc7c2f 536 }
ff80d9b1 537 l0span = nblks; /* save for later use to calc level > 1 overhead */
b128c09f 538 if (dn->dn_nlevels == 1) {
34dc7c2f
BB
539 int i;
540 for (i = 0; i < nblks; i++) {
541 blkptr_t *bp = dn->dn_phys->dn_blkptr;
b128c09f 542 ASSERT3U(blkid + i, <, dn->dn_nblkptr);
34dc7c2f 543 bp += blkid + i;
428870ff 544 if (dsl_dataset_block_freeable(ds, bp, bp->blk_birth)) {
34dc7c2f 545 dprintf_bp(bp, "can free old%s", "");
428870ff 546 space += bp_get_dsize(spa, bp);
34dc7c2f
BB
547 }
548 unref += BP_GET_ASIZE(bp);
549 }
ff80d9b1 550 nl1blks = 1;
34dc7c2f
BB
551 nblks = 0;
552 }
553
b128c09f 554 lastblk = blkid + nblks - 1;
34dc7c2f
BB
555 while (nblks) {
556 dmu_buf_impl_t *dbuf;
b128c09f
BB
557 uint64_t ibyte, new_blkid;
558 int epb = 1 << epbs;
559 int err, i, blkoff, tochk;
560 blkptr_t *bp;
561
562 ibyte = blkid << dn->dn_datablkshift;
563 err = dnode_next_offset(dn,
564 DNODE_FIND_HAVELOCK, &ibyte, 2, 1, 0);
565 new_blkid = ibyte >> dn->dn_datablkshift;
566 if (err == ESRCH) {
567 skipped += (lastblk >> epbs) - (blkid >> epbs) + 1;
568 break;
569 }
570 if (err) {
571 txh->txh_tx->tx_err = err;
572 break;
573 }
574 if (new_blkid > lastblk) {
575 skipped += (lastblk >> epbs) - (blkid >> epbs) + 1;
576 break;
577 }
34dc7c2f 578
b128c09f
BB
579 if (new_blkid > blkid) {
580 ASSERT((new_blkid >> epbs) > (blkid >> epbs));
581 skipped += (new_blkid >> epbs) - (blkid >> epbs) - 1;
582 nblks -= new_blkid - blkid;
583 blkid = new_blkid;
584 }
585 blkoff = P2PHASE(blkid, epb);
586 tochk = MIN(epb - blkoff, nblks);
34dc7c2f 587
fcff0f35
PD
588 err = dbuf_hold_impl(dn, 1, blkid >> epbs,
589 FALSE, FALSE, FTAG, &dbuf);
428870ff
BB
590 if (err) {
591 txh->txh_tx->tx_err = err;
b128c09f 592 break;
34dc7c2f 593 }
428870ff 594
f85c06be
GM
595 (void) refcount_add_many(&txh->txh_memory_tohold,
596 dbuf->db.db_size, FTAG);
428870ff
BB
597
598 /*
599 * We don't check memory_tohold against DMU_MAX_ACCESS because
600 * memory_tohold is an over-estimation (especially the >L1
601 * indirect blocks), so it could fail. Callers should have
602 * already verified that they will not be holding too much
603 * memory.
604 */
605
b128c09f
BB
606 err = dbuf_read(dbuf, NULL, DB_RF_HAVESTRUCT | DB_RF_CANFAIL);
607 if (err != 0) {
34dc7c2f 608 txh->txh_tx->tx_err = err;
b128c09f 609 dbuf_rele(dbuf, FTAG);
34dc7c2f
BB
610 break;
611 }
612
b128c09f
BB
613 bp = dbuf->db.db_data;
614 bp += blkoff;
615
616 for (i = 0; i < tochk; i++) {
428870ff
BB
617 if (dsl_dataset_block_freeable(ds, &bp[i],
618 bp[i].blk_birth)) {
b128c09f 619 dprintf_bp(&bp[i], "can free old%s", "");
428870ff 620 space += bp_get_dsize(spa, &bp[i]);
b128c09f
BB
621 }
622 unref += BP_GET_ASIZE(bp);
623 }
624 dbuf_rele(dbuf, FTAG);
625
ff80d9b1 626 ++nl1blks;
34dc7c2f
BB
627 blkid += tochk;
628 nblks -= tochk;
629 }
630 rw_exit(&dn->dn_struct_rwlock);
631
ff80d9b1
AJ
632 /*
633 * Add in memory requirements of higher-level indirects.
634 * This assumes a worst-possible scenario for dn_nlevels and a
635 * worst-possible distribution of l1-blocks over the region to free.
636 */
637 {
638 uint64_t blkcnt = 1 + ((l0span >> epbs) >> epbs);
639 int level = 2;
640 /*
641 * Here we don't use DN_MAX_LEVEL, but calculate it with the
642 * given datablkshift and indblkshift. This makes the
643 * difference between 19 and 8 on large files.
644 */
645 int maxlevel = 2 + (DN_MAX_OFFSET_SHIFT - dn->dn_datablkshift) /
646 (dn->dn_indblkshift - SPA_BLKPTRSHIFT);
647
648 while (level++ < maxlevel) {
f85c06be
GM
649 (void) refcount_add_many(&txh->txh_memory_tohold,
650 MAX(MIN(blkcnt, nl1blks), 1) << dn->dn_indblkshift,
651 FTAG);
ff80d9b1
AJ
652 blkcnt = 1 + (blkcnt >> epbs);
653 }
654 }
655
b128c09f
BB
656 /* account for new level 1 indirect blocks that might show up */
657 if (skipped > 0) {
f85c06be
GM
658 (void) refcount_add_many(&txh->txh_fudge,
659 skipped << dn->dn_indblkshift, FTAG);
b128c09f 660 skipped = MIN(skipped, DMU_MAX_DELETEBLKCNT >> epbs);
f85c06be
GM
661 (void) refcount_add_many(&txh->txh_memory_tohold,
662 skipped << dn->dn_indblkshift, FTAG);
b128c09f 663 }
f85c06be
GM
664 (void) refcount_add_many(&txh->txh_space_tofree, space, FTAG);
665 (void) refcount_add_many(&txh->txh_space_tounref, unref, FTAG);
34dc7c2f
BB
666}
667
19d55079
MA
668/*
669 * This function marks the transaction as being a "net free". The end
670 * result is that refquotas will be disabled for this transaction, and
671 * this transaction will be able to use half of the pool space overhead
672 * (see dsl_pool_adjustedsize()). Therefore this function should only
673 * be called for transactions that we expect will not cause a net increase
674 * in the amount of space used (but it's OK if that is occasionally not true).
675 */
676void
677dmu_tx_mark_netfree(dmu_tx_t *tx)
678{
679 dmu_tx_hold_t *txh;
680
681 txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
682 DMU_NEW_OBJECT, THT_FREE, 0, 0);
683
684 /*
685 * Pretend that this operation will free 1GB of space. This
686 * should be large enough to cancel out the largest write.
687 * We don't want to use something like UINT64_MAX, because that would
688 * cause overflows when doing math with these values (e.g. in
689 * dmu_tx_try_assign()).
690 */
f85c06be
GM
691 (void) refcount_add_many(&txh->txh_space_tofree,
692 1024 * 1024 * 1024, FTAG);
693 (void) refcount_add_many(&txh->txh_space_tounref,
694 1024 * 1024 * 1024, FTAG);
19d55079
MA
695}
696
0eef1bde 697static void
698dmu_tx_hold_free_impl(dmu_tx_hold_t *txh, uint64_t off, uint64_t len)
34dc7c2f 699{
0eef1bde 700 dmu_tx_t *tx;
34dc7c2f 701 dnode_t *dn;
ea97f8ce 702 int err;
34dc7c2f
BB
703 zio_t *zio;
704
0eef1bde 705 tx = txh->txh_tx;
34dc7c2f
BB
706 ASSERT(tx->tx_txg == 0);
707
34dc7c2f 708 dn = txh->txh_dnode;
e8b96c60 709 dmu_tx_count_dnode(txh);
34dc7c2f 710
34dc7c2f
BB
711 if (off >= (dn->dn_maxblkid+1) * dn->dn_datablksz)
712 return;
713 if (len == DMU_OBJECT_END)
714 len = (dn->dn_maxblkid+1) * dn->dn_datablksz - off;
715
ea97f8ce
MA
716 dmu_tx_count_dnode(txh);
717
34dc7c2f 718 /*
ea97f8ce
MA
719 * For i/o error checking, we read the first and last level-0
720 * blocks if they are not aligned, and all the level-1 blocks.
721 *
722 * Note: dbuf_free_range() assumes that we have not instantiated
723 * any level-0 dbufs that will be completely freed. Therefore we must
724 * exercise care to not read or count the first and last blocks
725 * if they are blocksize-aligned.
726 */
727 if (dn->dn_datablkshift == 0) {
b663a23d 728 if (off != 0 || len < dn->dn_datablksz)
92bc214c 729 dmu_tx_count_write(txh, 0, dn->dn_datablksz);
ea97f8ce
MA
730 } else {
731 /* first block will be modified if it is not aligned */
732 if (!IS_P2ALIGNED(off, 1 << dn->dn_datablkshift))
733 dmu_tx_count_write(txh, off, 1);
734 /* last block will be modified if it is not aligned */
735 if (!IS_P2ALIGNED(off + len, 1 << dn->dn_datablkshift))
736 dmu_tx_count_write(txh, off+len, 1);
737 }
738
739 /*
740 * Check level-1 blocks.
34dc7c2f
BB
741 */
742 if (dn->dn_nlevels > 1) {
ea97f8ce 743 int shift = dn->dn_datablkshift + dn->dn_indblkshift -
34dc7c2f 744 SPA_BLKPTRSHIFT;
ea97f8ce
MA
745 uint64_t start = off >> shift;
746 uint64_t end = (off + len) >> shift;
747 uint64_t i;
748
ea97f8ce 749 ASSERT(dn->dn_indblkshift != 0);
34dc7c2f 750
2e7b7657
MA
751 /*
752 * dnode_reallocate() can result in an object with indirect
753 * blocks having an odd data block size. In this case,
754 * just check the single block.
755 */
756 if (dn->dn_datablkshift == 0)
757 start = end = 0;
758
34dc7c2f
BB
759 zio = zio_root(tx->tx_pool->dp_spa,
760 NULL, NULL, ZIO_FLAG_CANFAIL);
761 for (i = start; i <= end; i++) {
762 uint64_t ibyte = i << shift;
b128c09f 763 err = dnode_next_offset(dn, 0, &ibyte, 2, 1, 0);
34dc7c2f 764 i = ibyte >> shift;
4bda3bd0 765 if (err == ESRCH || i > end)
34dc7c2f
BB
766 break;
767 if (err) {
768 tx->tx_err = err;
769 return;
770 }
771
772 err = dmu_tx_check_ioerr(zio, dn, 1, i);
773 if (err) {
774 tx->tx_err = err;
775 return;
776 }
777 }
778 err = zio_wait(zio);
779 if (err) {
780 tx->tx_err = err;
781 return;
782 }
783 }
784
34dc7c2f
BB
785 dmu_tx_count_free(txh, off, len);
786}
787
788void
0eef1bde 789dmu_tx_hold_free(dmu_tx_t *tx, uint64_t object, uint64_t off, uint64_t len)
790{
791 dmu_tx_hold_t *txh;
792
793 txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
794 object, THT_FREE, off, len);
795 if (txh == NULL)
796 return;
797 (void) dmu_tx_hold_free_impl(txh, off, len);
798}
799
800void
801dmu_tx_hold_free_by_dnode(dmu_tx_t *tx, dnode_t *dn, uint64_t off, uint64_t len)
34dc7c2f
BB
802{
803 dmu_tx_hold_t *txh;
0eef1bde 804
805 txh = dmu_tx_hold_dnode_impl(tx, dn, THT_FREE, off, len);
806 if (txh == NULL)
807 return;
808 (void) dmu_tx_hold_free_impl(txh, off, len);
809}
810
811static void
812dmu_tx_hold_zap_impl(dmu_tx_hold_t *txh, int add, const char *name)
813{
814 dmu_tx_t *tx = txh->txh_tx;
34dc7c2f 815 dnode_t *dn;
f85c06be
GM
816 int err;
817 int epbs;
d683ddbb 818 dsl_dataset_phys_t *ds_phys;
f85c06be 819 int lvl;
34dc7c2f
BB
820
821 ASSERT(tx->tx_txg == 0);
822
34dc7c2f
BB
823 dn = txh->txh_dnode;
824
825 dmu_tx_count_dnode(txh);
826
827 if (dn == NULL) {
828 /*
829 * We will be able to fit a new object's entries into one leaf
830 * block. So there will be at most 2 blocks total,
831 * including the header block.
832 */
f4bae2ed 833 dmu_tx_count_write(txh, 0, 2ULL << fzap_default_block_shift);
34dc7c2f
BB
834 return;
835 }
836
9ae529ec 837 ASSERT3U(DMU_OT_BYTESWAP(dn->dn_type), ==, DMU_BSWAP_ZAP);
34dc7c2f
BB
838
839 if (dn->dn_maxblkid == 0 && !add) {
22cd4a46
AL
840 blkptr_t *bp;
841
34dc7c2f
BB
842 /*
843 * If there is only one block (i.e. this is a micro-zap)
844 * and we are not adding anything, the accounting is simple.
845 */
846 err = dmu_tx_check_ioerr(NULL, dn, 0, 0);
847 if (err) {
848 tx->tx_err = err;
849 return;
850 }
851
852 /*
853 * Use max block size here, since we don't know how much
854 * the size will change between now and the dbuf dirty call.
855 */
22cd4a46 856 bp = &dn->dn_phys->dn_blkptr[0];
34dc7c2f 857 if (dsl_dataset_block_freeable(dn->dn_objset->os_dsl_dataset,
f85c06be
GM
858 bp, bp->blk_birth)) {
859 (void) refcount_add_many(&txh->txh_space_tooverwrite,
860 MZAP_MAX_BLKSZ, FTAG);
861 } else {
862 (void) refcount_add_many(&txh->txh_space_towrite,
863 MZAP_MAX_BLKSZ, FTAG);
864 }
865 if (!BP_IS_HOLE(bp)) {
866 (void) refcount_add_many(&txh->txh_space_tounref,
867 MZAP_MAX_BLKSZ, FTAG);
868 }
34dc7c2f
BB
869 return;
870 }
871
872 if (dn->dn_maxblkid > 0 && name) {
873 /*
874 * access the name in this fat-zap so that we'll check
875 * for i/o errors to the leaf blocks, etc.
876 */
2bce8049 877 err = zap_lookup_by_dnode(dn, name, 8, 0, NULL);
34dc7c2f
BB
878 if (err == EIO) {
879 tx->tx_err = err;
880 return;
881 }
882 }
883
2bce8049 884 err = zap_count_write_by_dnode(dn, name, add,
45d1cae3 885 &txh->txh_space_towrite, &txh->txh_space_tooverwrite);
34dc7c2f
BB
886
887 /*
888 * If the modified blocks are scattered to the four winds,
f85c06be
GM
889 * we'll have to modify an indirect twig for each. We can make
890 * modifications at up to 3 locations:
891 * - header block at the beginning of the object
892 * - target leaf block
893 * - end of the object, where we might need to write:
894 * - a new leaf block if the target block needs to be split
895 * - the new pointer table, if it is growing
896 * - the new cookie table, if it is growing
34dc7c2f
BB
897 */
898 epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
f85c06be
GM
899 ds_phys =
900 dsl_dataset_phys(dn->dn_objset->os_dsl_dataset);
901 for (lvl = 1; lvl < dn->dn_nlevels; lvl++) {
902 uint64_t num_indirects = 1 + (dn->dn_maxblkid >> (epbs * lvl));
903 uint64_t spc = MIN(3, num_indirects) << dn->dn_indblkshift;
904 if (ds_phys->ds_prev_snap_obj != 0) {
905 (void) refcount_add_many(&txh->txh_space_towrite,
906 spc, FTAG);
907 } else {
908 (void) refcount_add_many(&txh->txh_space_tooverwrite,
909 spc, FTAG);
910 }
911 }
34dc7c2f
BB
912}
913
0eef1bde 914void
915dmu_tx_hold_zap(dmu_tx_t *tx, uint64_t object, int add, const char *name)
916{
917 dmu_tx_hold_t *txh;
918
919 ASSERT(tx->tx_txg == 0);
920
921 txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
922 object, THT_ZAP, add, (uintptr_t)name);
923 if (txh == NULL)
924 return;
925 dmu_tx_hold_zap_impl(txh, add, name);
926}
927
928void
929dmu_tx_hold_zap_by_dnode(dmu_tx_t *tx, dnode_t *dn, int add, const char *name)
930{
931 dmu_tx_hold_t *txh;
932
933 ASSERT(tx->tx_txg == 0);
934 ASSERT(dn != NULL);
935
936 txh = dmu_tx_hold_dnode_impl(tx, dn, THT_ZAP, add, (uintptr_t)name);
937 if (txh == NULL)
938 return;
939 dmu_tx_hold_zap_impl(txh, add, name);
940}
941
34dc7c2f
BB
942void
943dmu_tx_hold_bonus(dmu_tx_t *tx, uint64_t object)
944{
945 dmu_tx_hold_t *txh;
946
947 ASSERT(tx->tx_txg == 0);
948
949 txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
950 object, THT_BONUS, 0, 0);
951 if (txh)
952 dmu_tx_count_dnode(txh);
953}
954
0eef1bde 955void
956dmu_tx_hold_bonus_by_dnode(dmu_tx_t *tx, dnode_t *dn)
957{
958 dmu_tx_hold_t *txh;
959
960 ASSERT(tx->tx_txg == 0);
961
962 txh = dmu_tx_hold_dnode_impl(tx, dn, THT_BONUS, 0, 0);
963 if (txh)
964 dmu_tx_count_dnode(txh);
965}
966
34dc7c2f
BB
967void
968dmu_tx_hold_space(dmu_tx_t *tx, uint64_t space)
969{
970 dmu_tx_hold_t *txh;
7d637211 971
34dc7c2f
BB
972 ASSERT(tx->tx_txg == 0);
973
974 txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
975 DMU_NEW_OBJECT, THT_SPACE, space, 0);
7d637211 976 if (txh)
f85c06be 977 (void) refcount_add_many(&txh->txh_space_towrite, space, FTAG);
34dc7c2f
BB
978}
979
980int
981dmu_tx_holds(dmu_tx_t *tx, uint64_t object)
982{
983 dmu_tx_hold_t *txh;
984 int holds = 0;
985
986 /*
987 * By asserting that the tx is assigned, we're counting the
988 * number of dn_tx_holds, which is the same as the number of
989 * dn_holds. Otherwise, we'd be counting dn_holds, but
990 * dn_tx_holds could be 0.
991 */
992 ASSERT(tx->tx_txg != 0);
993
994 /* if (tx->tx_anyobj == TRUE) */
995 /* return (0); */
996
997 for (txh = list_head(&tx->tx_holds); txh;
998 txh = list_next(&tx->tx_holds, txh)) {
999 if (txh->txh_dnode && txh->txh_dnode->dn_object == object)
1000 holds++;
1001 }
1002
1003 return (holds);
1004}
1005
1c5de20a 1006#ifdef DEBUG_DMU_TX
34dc7c2f
BB
1007void
1008dmu_tx_dirty_buf(dmu_tx_t *tx, dmu_buf_impl_t *db)
1009{
1010 dmu_tx_hold_t *txh;
1011 int match_object = FALSE, match_offset = FALSE;
572e2857 1012 dnode_t *dn;
34dc7c2f 1013
572e2857
BB
1014 DB_DNODE_ENTER(db);
1015 dn = DB_DNODE(db);
99ea23c5 1016 ASSERT(dn != NULL);
34dc7c2f 1017 ASSERT(tx->tx_txg != 0);
428870ff 1018 ASSERT(tx->tx_objset == NULL || dn->dn_objset == tx->tx_objset);
34dc7c2f
BB
1019 ASSERT3U(dn->dn_object, ==, db->db.db_object);
1020
572e2857
BB
1021 if (tx->tx_anyobj) {
1022 DB_DNODE_EXIT(db);
34dc7c2f 1023 return;
572e2857 1024 }
34dc7c2f
BB
1025
1026 /* XXX No checking on the meta dnode for now */
572e2857
BB
1027 if (db->db.db_object == DMU_META_DNODE_OBJECT) {
1028 DB_DNODE_EXIT(db);
34dc7c2f 1029 return;
572e2857 1030 }
34dc7c2f
BB
1031
1032 for (txh = list_head(&tx->tx_holds); txh;
1033 txh = list_next(&tx->tx_holds, txh)) {
99ea23c5 1034 ASSERT3U(dn->dn_assigned_txg, ==, tx->tx_txg);
34dc7c2f
BB
1035 if (txh->txh_dnode == dn && txh->txh_type != THT_NEWOBJECT)
1036 match_object = TRUE;
1037 if (txh->txh_dnode == NULL || txh->txh_dnode == dn) {
1038 int datablkshift = dn->dn_datablkshift ?
1039 dn->dn_datablkshift : SPA_MAXBLOCKSHIFT;
1040 int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
1041 int shift = datablkshift + epbs * db->db_level;
1042 uint64_t beginblk = shift >= 64 ? 0 :
1043 (txh->txh_arg1 >> shift);
1044 uint64_t endblk = shift >= 64 ? 0 :
1045 ((txh->txh_arg1 + txh->txh_arg2 - 1) >> shift);
1046 uint64_t blkid = db->db_blkid;
1047
1048 /* XXX txh_arg2 better not be zero... */
1049
1050 dprintf("found txh type %x beginblk=%llx endblk=%llx\n",
1051 txh->txh_type, beginblk, endblk);
1052
1053 switch (txh->txh_type) {
1054 case THT_WRITE:
1055 if (blkid >= beginblk && blkid <= endblk)
1056 match_offset = TRUE;
1057 /*
1058 * We will let this hold work for the bonus
428870ff
BB
1059 * or spill buffer so that we don't need to
1060 * hold it when creating a new object.
34dc7c2f 1061 */
428870ff
BB
1062 if (blkid == DMU_BONUS_BLKID ||
1063 blkid == DMU_SPILL_BLKID)
34dc7c2f
BB
1064 match_offset = TRUE;
1065 /*
1066 * They might have to increase nlevels,
1067 * thus dirtying the new TLIBs. Or the
1068 * might have to change the block size,
1069 * thus dirying the new lvl=0 blk=0.
1070 */
1071 if (blkid == 0)
1072 match_offset = TRUE;
1073 break;
1074 case THT_FREE:
b128c09f
BB
1075 /*
1076 * We will dirty all the level 1 blocks in
1077 * the free range and perhaps the first and
1078 * last level 0 block.
1079 */
1080 if (blkid >= beginblk && (blkid <= endblk ||
1081 txh->txh_arg2 == DMU_OBJECT_END))
34dc7c2f
BB
1082 match_offset = TRUE;
1083 break;
428870ff
BB
1084 case THT_SPILL:
1085 if (blkid == DMU_SPILL_BLKID)
1086 match_offset = TRUE;
1087 break;
34dc7c2f 1088 case THT_BONUS:
428870ff 1089 if (blkid == DMU_BONUS_BLKID)
34dc7c2f
BB
1090 match_offset = TRUE;
1091 break;
1092 case THT_ZAP:
1093 match_offset = TRUE;
1094 break;
1095 case THT_NEWOBJECT:
1096 match_object = TRUE;
1097 break;
1098 default:
989fd514
BB
1099 cmn_err(CE_PANIC, "bad txh_type %d",
1100 txh->txh_type);
34dc7c2f
BB
1101 }
1102 }
572e2857
BB
1103 if (match_object && match_offset) {
1104 DB_DNODE_EXIT(db);
34dc7c2f 1105 return;
572e2857 1106 }
34dc7c2f 1107 }
572e2857 1108 DB_DNODE_EXIT(db);
34dc7c2f
BB
1109 panic("dirtying dbuf obj=%llx lvl=%u blkid=%llx but not tx_held\n",
1110 (u_longlong_t)db->db.db_object, db->db_level,
1111 (u_longlong_t)db->db_blkid);
1112}
1113#endif
1114
e8b96c60
MA
1115/*
1116 * If we can't do 10 iops, something is wrong. Let us go ahead
1117 * and hit zfs_dirty_data_max.
1118 */
1119hrtime_t zfs_delay_max_ns = 100 * MICROSEC; /* 100 milliseconds */
1120int zfs_delay_resolution_ns = 100 * 1000; /* 100 microseconds */
1121
1122/*
1123 * We delay transactions when we've determined that the backend storage
1124 * isn't able to accommodate the rate of incoming writes.
1125 *
1126 * If there is already a transaction waiting, we delay relative to when
1127 * that transaction finishes waiting. This way the calculated min_time
1128 * is independent of the number of threads concurrently executing
1129 * transactions.
1130 *
1131 * If we are the only waiter, wait relative to when the transaction
1132 * started, rather than the current time. This credits the transaction for
1133 * "time already served", e.g. reading indirect blocks.
1134 *
1135 * The minimum time for a transaction to take is calculated as:
1136 * min_time = scale * (dirty - min) / (max - dirty)
1137 * min_time is then capped at zfs_delay_max_ns.
1138 *
1139 * The delay has two degrees of freedom that can be adjusted via tunables.
1140 * The percentage of dirty data at which we start to delay is defined by
1141 * zfs_delay_min_dirty_percent. This should typically be at or above
1142 * zfs_vdev_async_write_active_max_dirty_percent so that we only start to
1143 * delay after writing at full speed has failed to keep up with the incoming
1144 * write rate. The scale of the curve is defined by zfs_delay_scale. Roughly
1145 * speaking, this variable determines the amount of delay at the midpoint of
1146 * the curve.
1147 *
1148 * delay
1149 * 10ms +-------------------------------------------------------------*+
1150 * | *|
1151 * 9ms + *+
1152 * | *|
1153 * 8ms + *+
1154 * | * |
1155 * 7ms + * +
1156 * | * |
1157 * 6ms + * +
1158 * | * |
1159 * 5ms + * +
1160 * | * |
1161 * 4ms + * +
1162 * | * |
1163 * 3ms + * +
1164 * | * |
1165 * 2ms + (midpoint) * +
1166 * | | ** |
1167 * 1ms + v *** +
1168 * | zfs_delay_scale ----------> ******** |
1169 * 0 +-------------------------------------*********----------------+
1170 * 0% <- zfs_dirty_data_max -> 100%
1171 *
1172 * Note that since the delay is added to the outstanding time remaining on the
1173 * most recent transaction, the delay is effectively the inverse of IOPS.
1174 * Here the midpoint of 500us translates to 2000 IOPS. The shape of the curve
1175 * was chosen such that small changes in the amount of accumulated dirty data
1176 * in the first 3/4 of the curve yield relatively small differences in the
1177 * amount of delay.
1178 *
1179 * The effects can be easier to understand when the amount of delay is
1180 * represented on a log scale:
1181 *
1182 * delay
1183 * 100ms +-------------------------------------------------------------++
1184 * + +
1185 * | |
1186 * + *+
1187 * 10ms + *+
1188 * + ** +
1189 * | (midpoint) ** |
1190 * + | ** +
1191 * 1ms + v **** +
1192 * + zfs_delay_scale ----------> ***** +
1193 * | **** |
1194 * + **** +
1195 * 100us + ** +
1196 * + * +
1197 * | * |
1198 * + * +
1199 * 10us + * +
1200 * + +
1201 * | |
1202 * + +
1203 * +--------------------------------------------------------------+
1204 * 0% <- zfs_dirty_data_max -> 100%
1205 *
1206 * Note here that only as the amount of dirty data approaches its limit does
1207 * the delay start to increase rapidly. The goal of a properly tuned system
1208 * should be to keep the amount of dirty data out of that range by first
1209 * ensuring that the appropriate limits are set for the I/O scheduler to reach
1210 * optimal throughput on the backend storage, and then by changing the value
1211 * of zfs_delay_scale to increase the steepness of the curve.
1212 */
1213static void
1214dmu_tx_delay(dmu_tx_t *tx, uint64_t dirty)
1215{
1216 dsl_pool_t *dp = tx->tx_pool;
1217 uint64_t delay_min_bytes =
1218 zfs_dirty_data_max * zfs_delay_min_dirty_percent / 100;
1219 hrtime_t wakeup, min_tx_time, now;
1220
1221 if (dirty <= delay_min_bytes)
1222 return;
1223
1224 /*
1225 * The caller has already waited until we are under the max.
1226 * We make them pass us the amount of dirty data so we don't
1227 * have to handle the case of it being >= the max, which could
1228 * cause a divide-by-zero if it's == the max.
1229 */
1230 ASSERT3U(dirty, <, zfs_dirty_data_max);
1231
1232 now = gethrtime();
1233 min_tx_time = zfs_delay_scale *
1234 (dirty - delay_min_bytes) / (zfs_dirty_data_max - dirty);
1235 min_tx_time = MIN(min_tx_time, zfs_delay_max_ns);
1236 if (now > tx->tx_start + min_tx_time)
1237 return;
1238
1239 DTRACE_PROBE3(delay__mintime, dmu_tx_t *, tx, uint64_t, dirty,
1240 uint64_t, min_tx_time);
1241
1242 mutex_enter(&dp->dp_lock);
1243 wakeup = MAX(tx->tx_start + min_tx_time,
1244 dp->dp_last_wakeup + min_tx_time);
1245 dp->dp_last_wakeup = wakeup;
1246 mutex_exit(&dp->dp_lock);
1247
1248 zfs_sleep_until(wakeup);
1249}
1250
34dc7c2f 1251static int
13fe0198 1252dmu_tx_try_assign(dmu_tx_t *tx, txg_how_t txg_how)
34dc7c2f
BB
1253{
1254 dmu_tx_hold_t *txh;
1255 spa_t *spa = tx->tx_pool->dp_spa;
b128c09f
BB
1256 uint64_t memory, asize, fsize, usize;
1257 uint64_t towrite, tofree, tooverwrite, tounref, tohold, fudge;
34dc7c2f 1258
c99c9001 1259 ASSERT0(tx->tx_txg);
34dc7c2f 1260
570827e1
BB
1261 if (tx->tx_err) {
1262 DMU_TX_STAT_BUMP(dmu_tx_error);
34dc7c2f 1263 return (tx->tx_err);
570827e1 1264 }
34dc7c2f 1265
b128c09f 1266 if (spa_suspended(spa)) {
570827e1
BB
1267 DMU_TX_STAT_BUMP(dmu_tx_suspended);
1268
34dc7c2f
BB
1269 /*
1270 * If the user has indicated a blocking failure mode
1271 * then return ERESTART which will block in dmu_tx_wait().
1272 * Otherwise, return EIO so that an error can get
1273 * propagated back to the VOP calls.
1274 *
1275 * Note that we always honor the txg_how flag regardless
1276 * of the failuremode setting.
1277 */
1278 if (spa_get_failmode(spa) == ZIO_FAILURE_MODE_CONTINUE &&
1279 txg_how != TXG_WAIT)
2e528b49 1280 return (SET_ERROR(EIO));
34dc7c2f 1281
2e528b49 1282 return (SET_ERROR(ERESTART));
34dc7c2f
BB
1283 }
1284
e8b96c60
MA
1285 if (!tx->tx_waited &&
1286 dsl_pool_need_dirty_delay(tx->tx_pool)) {
1287 tx->tx_wait_dirty = B_TRUE;
1288 DMU_TX_STAT_BUMP(dmu_tx_dirty_delay);
1289 return (ERESTART);
1290 }
1291
34dc7c2f
BB
1292 tx->tx_txg = txg_hold_open(tx->tx_pool, &tx->tx_txgh);
1293 tx->tx_needassign_txh = NULL;
1294
1295 /*
1296 * NB: No error returns are allowed after txg_hold_open, but
1297 * before processing the dnode holds, due to the
1298 * dmu_tx_unassign() logic.
1299 */
1300
b128c09f 1301 towrite = tofree = tooverwrite = tounref = tohold = fudge = 0;
34dc7c2f
BB
1302 for (txh = list_head(&tx->tx_holds); txh;
1303 txh = list_next(&tx->tx_holds, txh)) {
1304 dnode_t *dn = txh->txh_dnode;
1305 if (dn != NULL) {
1306 mutex_enter(&dn->dn_mtx);
1307 if (dn->dn_assigned_txg == tx->tx_txg - 1) {
1308 mutex_exit(&dn->dn_mtx);
1309 tx->tx_needassign_txh = txh;
570827e1 1310 DMU_TX_STAT_BUMP(dmu_tx_group);
2e528b49 1311 return (SET_ERROR(ERESTART));
34dc7c2f
BB
1312 }
1313 if (dn->dn_assigned_txg == 0)
1314 dn->dn_assigned_txg = tx->tx_txg;
1315 ASSERT3U(dn->dn_assigned_txg, ==, tx->tx_txg);
1316 (void) refcount_add(&dn->dn_tx_holds, tx);
1317 mutex_exit(&dn->dn_mtx);
1318 }
f85c06be
GM
1319 towrite += refcount_count(&txh->txh_space_towrite);
1320 tofree += refcount_count(&txh->txh_space_tofree);
1321 tooverwrite += refcount_count(&txh->txh_space_tooverwrite);
1322 tounref += refcount_count(&txh->txh_space_tounref);
1323 tohold += refcount_count(&txh->txh_memory_tohold);
1324 fudge += refcount_count(&txh->txh_fudge);
34dc7c2f
BB
1325 }
1326
34dc7c2f
BB
1327 /*
1328 * If a snapshot has been taken since we made our estimates,
1329 * assume that we won't be able to free or overwrite anything.
1330 */
1331 if (tx->tx_objset &&
428870ff 1332 dsl_dataset_prev_snap_txg(tx->tx_objset->os_dsl_dataset) >
34dc7c2f
BB
1333 tx->tx_lastsnap_txg) {
1334 towrite += tooverwrite;
1335 tooverwrite = tofree = 0;
1336 }
1337
b128c09f
BB
1338 /* needed allocation: worst-case estimate of write space */
1339 asize = spa_get_asize(tx->tx_pool->dp_spa, towrite + tooverwrite);
1340 /* freed space estimate: worst-case overwrite + free estimate */
34dc7c2f 1341 fsize = spa_get_asize(tx->tx_pool->dp_spa, tooverwrite) + tofree;
b128c09f 1342 /* convert unrefd space to worst-case estimate */
34dc7c2f 1343 usize = spa_get_asize(tx->tx_pool->dp_spa, tounref);
b128c09f
BB
1344 /* calculate memory footprint estimate */
1345 memory = towrite + tooverwrite + tohold;
34dc7c2f 1346
1c5de20a 1347#ifdef DEBUG_DMU_TX
b128c09f
BB
1348 /*
1349 * Add in 'tohold' to account for our dirty holds on this memory
1350 * XXX - the "fudge" factor is to account for skipped blocks that
1351 * we missed because dnode_next_offset() misses in-core-only blocks.
1352 */
1353 tx->tx_space_towrite = asize +
1354 spa_get_asize(tx->tx_pool->dp_spa, tohold + fudge);
34dc7c2f
BB
1355 tx->tx_space_tofree = tofree;
1356 tx->tx_space_tooverwrite = tooverwrite;
1357 tx->tx_space_tounref = tounref;
1358#endif
1359
1360 if (tx->tx_dir && asize != 0) {
b128c09f
BB
1361 int err = dsl_dir_tempreserve_space(tx->tx_dir, memory,
1362 asize, fsize, usize, &tx->tx_tempreserve_cookie, tx);
34dc7c2f
BB
1363 if (err)
1364 return (err);
1365 }
1366
570827e1
BB
1367 DMU_TX_STAT_BUMP(dmu_tx_assigned);
1368
34dc7c2f
BB
1369 return (0);
1370}
1371
1372static void
1373dmu_tx_unassign(dmu_tx_t *tx)
1374{
1375 dmu_tx_hold_t *txh;
1376
1377 if (tx->tx_txg == 0)
1378 return;
1379
1380 txg_rele_to_quiesce(&tx->tx_txgh);
1381
e49f1e20
WA
1382 /*
1383 * Walk the transaction's hold list, removing the hold on the
1384 * associated dnode, and notifying waiters if the refcount drops to 0.
1385 */
981b2126 1386 for (txh = list_head(&tx->tx_holds);
1387 txh && txh != tx->tx_needassign_txh;
34dc7c2f
BB
1388 txh = list_next(&tx->tx_holds, txh)) {
1389 dnode_t *dn = txh->txh_dnode;
1390
1391 if (dn == NULL)
1392 continue;
1393 mutex_enter(&dn->dn_mtx);
1394 ASSERT3U(dn->dn_assigned_txg, ==, tx->tx_txg);
1395
1396 if (refcount_remove(&dn->dn_tx_holds, tx) == 0) {
1397 dn->dn_assigned_txg = 0;
1398 cv_broadcast(&dn->dn_notxholds);
1399 }
1400 mutex_exit(&dn->dn_mtx);
1401 }
1402
1403 txg_rele_to_sync(&tx->tx_txgh);
1404
1405 tx->tx_lasttried_txg = tx->tx_txg;
1406 tx->tx_txg = 0;
1407}
1408
1409/*
1410 * Assign tx to a transaction group. txg_how can be one of:
1411 *
1412 * (1) TXG_WAIT. If the current open txg is full, waits until there's
1413 * a new one. This should be used when you're not holding locks.
13fe0198 1414 * It will only fail if we're truly out of space (or over quota).
34dc7c2f
BB
1415 *
1416 * (2) TXG_NOWAIT. If we can't assign into the current open txg without
1417 * blocking, returns immediately with ERESTART. This should be used
1418 * whenever you're holding locks. On an ERESTART error, the caller
1419 * should drop locks, do a dmu_tx_wait(tx), and try again.
e8b96c60
MA
1420 *
1421 * (3) TXG_WAITED. Like TXG_NOWAIT, but indicates that dmu_tx_wait()
1422 * has already been called on behalf of this operation (though
1423 * most likely on a different tx).
34dc7c2f
BB
1424 */
1425int
13fe0198 1426dmu_tx_assign(dmu_tx_t *tx, txg_how_t txg_how)
34dc7c2f
BB
1427{
1428 int err;
1429
1430 ASSERT(tx->tx_txg == 0);
e8b96c60
MA
1431 ASSERT(txg_how == TXG_WAIT || txg_how == TXG_NOWAIT ||
1432 txg_how == TXG_WAITED);
34dc7c2f
BB
1433 ASSERT(!dsl_pool_sync_context(tx->tx_pool));
1434
e8b96c60
MA
1435 if (txg_how == TXG_WAITED)
1436 tx->tx_waited = B_TRUE;
1437
13fe0198
MA
1438 /* If we might wait, we must not hold the config lock. */
1439 ASSERT(txg_how != TXG_WAIT || !dsl_pool_config_held(tx->tx_pool));
1440
34dc7c2f
BB
1441 while ((err = dmu_tx_try_assign(tx, txg_how)) != 0) {
1442 dmu_tx_unassign(tx);
1443
1444 if (err != ERESTART || txg_how != TXG_WAIT)
1445 return (err);
1446
1447 dmu_tx_wait(tx);
1448 }
1449
1450 txg_rele_to_quiesce(&tx->tx_txgh);
1451
1452 return (0);
1453}
1454
1455void
1456dmu_tx_wait(dmu_tx_t *tx)
1457{
1458 spa_t *spa = tx->tx_pool->dp_spa;
e8b96c60 1459 dsl_pool_t *dp = tx->tx_pool;
a77c4c83 1460 hrtime_t before;
34dc7c2f
BB
1461
1462 ASSERT(tx->tx_txg == 0);
13fe0198 1463 ASSERT(!dsl_pool_config_held(tx->tx_pool));
34dc7c2f 1464
a77c4c83
NB
1465 before = gethrtime();
1466
e8b96c60
MA
1467 if (tx->tx_wait_dirty) {
1468 uint64_t dirty;
1469
1470 /*
1471 * dmu_tx_try_assign() has determined that we need to wait
1472 * because we've consumed much or all of the dirty buffer
1473 * space.
1474 */
1475 mutex_enter(&dp->dp_lock);
1476 if (dp->dp_dirty_total >= zfs_dirty_data_max)
1477 DMU_TX_STAT_BUMP(dmu_tx_dirty_over_max);
1478 while (dp->dp_dirty_total >= zfs_dirty_data_max)
1479 cv_wait(&dp->dp_spaceavail_cv, &dp->dp_lock);
1480 dirty = dp->dp_dirty_total;
1481 mutex_exit(&dp->dp_lock);
1482
1483 dmu_tx_delay(tx, dirty);
1484
1485 tx->tx_wait_dirty = B_FALSE;
1486
1487 /*
1488 * Note: setting tx_waited only has effect if the caller
1489 * used TX_WAIT. Otherwise they are going to destroy
1490 * this tx and try again. The common case, zfs_write(),
1491 * uses TX_WAIT.
1492 */
1493 tx->tx_waited = B_TRUE;
1494 } else if (spa_suspended(spa) || tx->tx_lasttried_txg == 0) {
1495 /*
1496 * If the pool is suspended we need to wait until it
1497 * is resumed. Note that it's possible that the pool
1498 * has become active after this thread has tried to
1499 * obtain a tx. If that's the case then tx_lasttried_txg
1500 * would not have been set.
1501 */
1502 txg_wait_synced(dp, spa_last_synced_txg(spa) + 1);
34dc7c2f
BB
1503 } else if (tx->tx_needassign_txh) {
1504 dnode_t *dn = tx->tx_needassign_txh->txh_dnode;
1505
1506 mutex_enter(&dn->dn_mtx);
1507 while (dn->dn_assigned_txg == tx->tx_lasttried_txg - 1)
1508 cv_wait(&dn->dn_notxholds, &dn->dn_mtx);
1509 mutex_exit(&dn->dn_mtx);
1510 tx->tx_needassign_txh = NULL;
1511 } else {
e8b96c60
MA
1512 /*
1513 * A dnode is assigned to the quiescing txg. Wait for its
1514 * transaction to complete.
1515 */
34dc7c2f
BB
1516 txg_wait_open(tx->tx_pool, tx->tx_lasttried_txg + 1);
1517 }
a77c4c83
NB
1518
1519 spa_tx_assign_add_nsecs(spa, gethrtime() - before);
34dc7c2f
BB
1520}
1521
1522void
1523dmu_tx_willuse_space(dmu_tx_t *tx, int64_t delta)
1524{
1c5de20a 1525#ifdef DEBUG_DMU_TX
34dc7c2f
BB
1526 if (tx->tx_dir == NULL || delta == 0)
1527 return;
1528
1529 if (delta > 0) {
1530 ASSERT3U(refcount_count(&tx->tx_space_written) + delta, <=,
1531 tx->tx_space_towrite);
1532 (void) refcount_add_many(&tx->tx_space_written, delta, NULL);
1533 } else {
1534 (void) refcount_add_many(&tx->tx_space_freed, -delta, NULL);
1535 }
1536#endif
1537}
1538
f85c06be
GM
1539static void
1540dmu_tx_destroy(dmu_tx_t *tx)
1541{
1542 dmu_tx_hold_t *txh;
1543
1544 while ((txh = list_head(&tx->tx_holds)) != NULL) {
1545 dnode_t *dn = txh->txh_dnode;
1546
1547 list_remove(&tx->tx_holds, txh);
1548 refcount_destroy_many(&txh->txh_space_towrite,
1549 refcount_count(&txh->txh_space_towrite));
1550 refcount_destroy_many(&txh->txh_space_tofree,
1551 refcount_count(&txh->txh_space_tofree));
1552 refcount_destroy_many(&txh->txh_space_tooverwrite,
1553 refcount_count(&txh->txh_space_tooverwrite));
1554 refcount_destroy_many(&txh->txh_space_tounref,
1555 refcount_count(&txh->txh_space_tounref));
1556 refcount_destroy_many(&txh->txh_memory_tohold,
1557 refcount_count(&txh->txh_memory_tohold));
1558 refcount_destroy_many(&txh->txh_fudge,
1559 refcount_count(&txh->txh_fudge));
1560 kmem_free(txh, sizeof (dmu_tx_hold_t));
1561 if (dn != NULL)
1562 dnode_rele(dn, tx);
1563 }
1564
1565 list_destroy(&tx->tx_callbacks);
1566 list_destroy(&tx->tx_holds);
1567#ifdef DEBUG_DMU_TX
1568 refcount_destroy_many(&tx->tx_space_written,
1569 refcount_count(&tx->tx_space_written));
1570 refcount_destroy_many(&tx->tx_space_freed,
1571 refcount_count(&tx->tx_space_freed));
1572#endif
1573 kmem_free(tx, sizeof (dmu_tx_t));
1574}
1575
34dc7c2f
BB
1576void
1577dmu_tx_commit(dmu_tx_t *tx)
1578{
1579 dmu_tx_hold_t *txh;
1580
1581 ASSERT(tx->tx_txg != 0);
1582
e49f1e20
WA
1583 /*
1584 * Go through the transaction's hold list and remove holds on
1585 * associated dnodes, notifying waiters if no holds remain.
1586 */
f85c06be
GM
1587 for (txh = list_head(&tx->tx_holds); txh != NULL;
1588 txh = list_next(&tx->tx_holds, txh)) {
34dc7c2f
BB
1589 dnode_t *dn = txh->txh_dnode;
1590
34dc7c2f
BB
1591 if (dn == NULL)
1592 continue;
f85c06be 1593
34dc7c2f
BB
1594 mutex_enter(&dn->dn_mtx);
1595 ASSERT3U(dn->dn_assigned_txg, ==, tx->tx_txg);
1596
1597 if (refcount_remove(&dn->dn_tx_holds, tx) == 0) {
1598 dn->dn_assigned_txg = 0;
1599 cv_broadcast(&dn->dn_notxholds);
1600 }
1601 mutex_exit(&dn->dn_mtx);
34dc7c2f
BB
1602 }
1603
1604 if (tx->tx_tempreserve_cookie)
1605 dsl_dir_tempreserve_clear(tx->tx_tempreserve_cookie, tx);
1606
428870ff
BB
1607 if (!list_is_empty(&tx->tx_callbacks))
1608 txg_register_callbacks(&tx->tx_txgh, &tx->tx_callbacks);
1609
34dc7c2f
BB
1610 if (tx->tx_anyobj == FALSE)
1611 txg_rele_to_sync(&tx->tx_txgh);
428870ff 1612
1c5de20a 1613#ifdef DEBUG_DMU_TX
34dc7c2f
BB
1614 dprintf("towrite=%llu written=%llu tofree=%llu freed=%llu\n",
1615 tx->tx_space_towrite, refcount_count(&tx->tx_space_written),
1616 tx->tx_space_tofree, refcount_count(&tx->tx_space_freed));
34dc7c2f 1617#endif
f85c06be 1618 dmu_tx_destroy(tx);
34dc7c2f
BB
1619}
1620
1621void
1622dmu_tx_abort(dmu_tx_t *tx)
1623{
34dc7c2f
BB
1624 ASSERT(tx->tx_txg == 0);
1625
428870ff
BB
1626 /*
1627 * Call any registered callbacks with an error code.
1628 */
1629 if (!list_is_empty(&tx->tx_callbacks))
1630 dmu_tx_do_callbacks(&tx->tx_callbacks, ECANCELED);
1631
f85c06be 1632 dmu_tx_destroy(tx);
34dc7c2f
BB
1633}
1634
1635uint64_t
1636dmu_tx_get_txg(dmu_tx_t *tx)
1637{
1638 ASSERT(tx->tx_txg != 0);
1639 return (tx->tx_txg);
1640}
428870ff 1641
13fe0198
MA
1642dsl_pool_t *
1643dmu_tx_pool(dmu_tx_t *tx)
1644{
1645 ASSERT(tx->tx_pool != NULL);
1646 return (tx->tx_pool);
1647}
1648
428870ff
BB
1649void
1650dmu_tx_callback_register(dmu_tx_t *tx, dmu_tx_callback_func_t *func, void *data)
1651{
1652 dmu_tx_callback_t *dcb;
1653
79c76d5b 1654 dcb = kmem_alloc(sizeof (dmu_tx_callback_t), KM_SLEEP);
428870ff
BB
1655
1656 dcb->dcb_func = func;
1657 dcb->dcb_data = data;
1658
1659 list_insert_tail(&tx->tx_callbacks, dcb);
1660}
1661
1662/*
1663 * Call all the commit callbacks on a list, with a given error code.
1664 */
1665void
1666dmu_tx_do_callbacks(list_t *cb_list, int error)
1667{
1668 dmu_tx_callback_t *dcb;
1669
f85c06be 1670 while ((dcb = list_head(cb_list)) != NULL) {
428870ff
BB
1671 list_remove(cb_list, dcb);
1672 dcb->dcb_func(dcb->dcb_data, error);
1673 kmem_free(dcb, sizeof (dmu_tx_callback_t));
1674 }
1675}
1676
1677/*
1678 * Interface to hold a bunch of attributes.
1679 * used for creating new files.
1680 * attrsize is the total size of all attributes
1681 * to be added during object creation
1682 *
1683 * For updating/adding a single attribute dmu_tx_hold_sa() should be used.
1684 */
1685
1686/*
1687 * hold necessary attribute name for attribute registration.
1688 * should be a very rare case where this is needed. If it does
1689 * happen it would only happen on the first write to the file system.
1690 */
1691static void
1692dmu_tx_sa_registration_hold(sa_os_t *sa, dmu_tx_t *tx)
1693{
1694 int i;
1695
1696 if (!sa->sa_need_attr_registration)
1697 return;
1698
1699 for (i = 0; i != sa->sa_num_attrs; i++) {
1700 if (!sa->sa_attr_table[i].sa_registered) {
1701 if (sa->sa_reg_attr_obj)
1702 dmu_tx_hold_zap(tx, sa->sa_reg_attr_obj,
1703 B_TRUE, sa->sa_attr_table[i].sa_name);
1704 else
1705 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT,
1706 B_TRUE, sa->sa_attr_table[i].sa_name);
1707 }
1708 }
1709}
1710
1711
1712void
1713dmu_tx_hold_spill(dmu_tx_t *tx, uint64_t object)
1714{
1715 dnode_t *dn;
1716 dmu_tx_hold_t *txh;
428870ff
BB
1717
1718 txh = dmu_tx_hold_object_impl(tx, tx->tx_objset, object,
1719 THT_SPILL, 0, 0);
7d637211
NC
1720 if (txh == NULL)
1721 return;
428870ff
BB
1722
1723 dn = txh->txh_dnode;
1724
1725 if (dn == NULL)
1726 return;
1727
1728 /* If blkptr doesn't exist then add space to towrite */
22cd4a46 1729 if (!(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR)) {
f85c06be
GM
1730 (void) refcount_add_many(&txh->txh_space_towrite,
1731 SPA_OLD_MAXBLOCKSIZE, FTAG);
428870ff 1732 } else {
22cd4a46
AL
1733 blkptr_t *bp;
1734
50c957f7 1735 bp = DN_SPILL_BLKPTR(dn->dn_phys);
428870ff 1736 if (dsl_dataset_block_freeable(dn->dn_objset->os_dsl_dataset,
f85c06be
GM
1737 bp, bp->blk_birth)) {
1738 (void) refcount_add_many(&txh->txh_space_tooverwrite,
1739 SPA_OLD_MAXBLOCKSIZE, FTAG);
1740 } else {
1741 (void) refcount_add_many(&txh->txh_space_towrite,
1742 SPA_OLD_MAXBLOCKSIZE, FTAG);
1743 }
1744 if (!BP_IS_HOLE(bp)) {
1745 (void) refcount_add_many(&txh->txh_space_tounref,
1746 SPA_OLD_MAXBLOCKSIZE, FTAG);
1747 }
428870ff
BB
1748 }
1749}
1750
1751void
1752dmu_tx_hold_sa_create(dmu_tx_t *tx, int attrsize)
1753{
1754 sa_os_t *sa = tx->tx_objset->os_sa;
1755
1756 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1757
1758 if (tx->tx_objset->os_sa->sa_master_obj == 0)
1759 return;
1760
1761 if (tx->tx_objset->os_sa->sa_layout_attr_obj)
1762 dmu_tx_hold_zap(tx, sa->sa_layout_attr_obj, B_TRUE, NULL);
1763 else {
1764 dmu_tx_hold_zap(tx, sa->sa_master_obj, B_TRUE, SA_LAYOUTS);
1765 dmu_tx_hold_zap(tx, sa->sa_master_obj, B_TRUE, SA_REGISTRY);
1766 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
1767 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
1768 }
1769
1770 dmu_tx_sa_registration_hold(sa, tx);
1771
50c957f7 1772 if (attrsize <= DN_OLD_MAX_BONUSLEN && !sa->sa_force_spill)
428870ff
BB
1773 return;
1774
1775 (void) dmu_tx_hold_object_impl(tx, tx->tx_objset, DMU_NEW_OBJECT,
1776 THT_SPILL, 0, 0);
1777}
1778
1779/*
1780 * Hold SA attribute
1781 *
1782 * dmu_tx_hold_sa(dmu_tx_t *tx, sa_handle_t *, attribute, add, size)
1783 *
1784 * variable_size is the total size of all variable sized attributes
1785 * passed to this function. It is not the total size of all
1786 * variable size attributes that *may* exist on this object.
1787 */
1788void
1789dmu_tx_hold_sa(dmu_tx_t *tx, sa_handle_t *hdl, boolean_t may_grow)
1790{
1791 uint64_t object;
1792 sa_os_t *sa = tx->tx_objset->os_sa;
1793
1794 ASSERT(hdl != NULL);
1795
1796 object = sa_handle_object(hdl);
1797
1798 dmu_tx_hold_bonus(tx, object);
1799
1800 if (tx->tx_objset->os_sa->sa_master_obj == 0)
1801 return;
1802
1803 if (tx->tx_objset->os_sa->sa_reg_attr_obj == 0 ||
1804 tx->tx_objset->os_sa->sa_layout_attr_obj == 0) {
1805 dmu_tx_hold_zap(tx, sa->sa_master_obj, B_TRUE, SA_LAYOUTS);
1806 dmu_tx_hold_zap(tx, sa->sa_master_obj, B_TRUE, SA_REGISTRY);
1807 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
1808 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
1809 }
1810
1811 dmu_tx_sa_registration_hold(sa, tx);
1812
1813 if (may_grow && tx->tx_objset->os_sa->sa_layout_attr_obj)
1814 dmu_tx_hold_zap(tx, sa->sa_layout_attr_obj, B_TRUE, NULL);
1815
572e2857 1816 if (sa->sa_force_spill || may_grow || hdl->sa_spill) {
428870ff
BB
1817 ASSERT(tx->tx_txg == 0);
1818 dmu_tx_hold_spill(tx, object);
572e2857
BB
1819 } else {
1820 dmu_buf_impl_t *db = (dmu_buf_impl_t *)hdl->sa_bonus;
1821 dnode_t *dn;
1822
1823 DB_DNODE_ENTER(db);
1824 dn = DB_DNODE(db);
1825 if (dn->dn_have_spill) {
1826 ASSERT(tx->tx_txg == 0);
1827 dmu_tx_hold_spill(tx, object);
1828 }
1829 DB_DNODE_EXIT(db);
428870ff
BB
1830 }
1831}
c28b2279 1832
570827e1
BB
1833void
1834dmu_tx_init(void)
1835{
1836 dmu_tx_ksp = kstat_create("zfs", 0, "dmu_tx", "misc",
1837 KSTAT_TYPE_NAMED, sizeof (dmu_tx_stats) / sizeof (kstat_named_t),
1838 KSTAT_FLAG_VIRTUAL);
1839
1840 if (dmu_tx_ksp != NULL) {
1841 dmu_tx_ksp->ks_data = &dmu_tx_stats;
1842 kstat_install(dmu_tx_ksp);
1843 }
1844}
1845
1846void
1847dmu_tx_fini(void)
1848{
1849 if (dmu_tx_ksp != NULL) {
1850 kstat_delete(dmu_tx_ksp);
1851 dmu_tx_ksp = NULL;
1852 }
1853}
1854
c28b2279
BB
1855#if defined(_KERNEL) && defined(HAVE_SPL)
1856EXPORT_SYMBOL(dmu_tx_create);
1857EXPORT_SYMBOL(dmu_tx_hold_write);
0eef1bde 1858EXPORT_SYMBOL(dmu_tx_hold_write_by_dnode);
c28b2279 1859EXPORT_SYMBOL(dmu_tx_hold_free);
0eef1bde 1860EXPORT_SYMBOL(dmu_tx_hold_free_by_dnode);
c28b2279 1861EXPORT_SYMBOL(dmu_tx_hold_zap);
0eef1bde 1862EXPORT_SYMBOL(dmu_tx_hold_zap_by_dnode);
c28b2279 1863EXPORT_SYMBOL(dmu_tx_hold_bonus);
0eef1bde 1864EXPORT_SYMBOL(dmu_tx_hold_bonus_by_dnode);
c28b2279
BB
1865EXPORT_SYMBOL(dmu_tx_abort);
1866EXPORT_SYMBOL(dmu_tx_assign);
1867EXPORT_SYMBOL(dmu_tx_wait);
1868EXPORT_SYMBOL(dmu_tx_commit);
1869EXPORT_SYMBOL(dmu_tx_get_txg);
1870EXPORT_SYMBOL(dmu_tx_callback_register);
1871EXPORT_SYMBOL(dmu_tx_do_callbacks);
1872EXPORT_SYMBOL(dmu_tx_hold_spill);
1873EXPORT_SYMBOL(dmu_tx_hold_sa_create);
1874EXPORT_SYMBOL(dmu_tx_hold_sa);
1875#endif