zfs_debug: Restore log size limit for userspace
[zfs.git] / module / zfs / dmu_tx.c
blob3fdcebdff9182f8af5f2fb957dba8a84938be65e
1 /*
2 * CDDL HEADER START
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.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or https://opensource.org/licenses/CDDL-1.0.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
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]
19 * CDDL HEADER END
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
24 * Copyright (c) 2012, 2017 by Delphix. All rights reserved.
25 * Copyright (c) 2024, Klara, Inc.
28 #include <sys/dmu.h>
29 #include <sys/dmu_impl.h>
30 #include <sys/dbuf.h>
31 #include <sys/dmu_tx.h>
32 #include <sys/dmu_objset.h>
33 #include <sys/dsl_dataset.h>
34 #include <sys/dsl_dir.h>
35 #include <sys/dsl_pool.h>
36 #include <sys/zap_impl.h>
37 #include <sys/spa.h>
38 #include <sys/sa.h>
39 #include <sys/sa_impl.h>
40 #include <sys/zfs_context.h>
41 #include <sys/trace_zfs.h>
43 typedef void (*dmu_tx_hold_func_t)(dmu_tx_t *tx, struct dnode *dn,
44 uint64_t arg1, uint64_t arg2);
46 dmu_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 },
52 { "dmu_tx_memory_reserve", KSTAT_DATA_UINT64 },
53 { "dmu_tx_memory_reclaim", KSTAT_DATA_UINT64 },
54 { "dmu_tx_dirty_throttle", KSTAT_DATA_UINT64 },
55 { "dmu_tx_dirty_delay", KSTAT_DATA_UINT64 },
56 { "dmu_tx_dirty_over_max", KSTAT_DATA_UINT64 },
57 { "dmu_tx_dirty_frees_delay", KSTAT_DATA_UINT64 },
58 { "dmu_tx_wrlog_delay", KSTAT_DATA_UINT64 },
59 { "dmu_tx_quota", KSTAT_DATA_UINT64 },
62 static kstat_t *dmu_tx_ksp;
64 dmu_tx_t *
65 dmu_tx_create_dd(dsl_dir_t *dd)
67 dmu_tx_t *tx = kmem_zalloc(sizeof (dmu_tx_t), KM_SLEEP);
68 tx->tx_dir = dd;
69 if (dd != NULL)
70 tx->tx_pool = dd->dd_pool;
71 list_create(&tx->tx_holds, sizeof (dmu_tx_hold_t),
72 offsetof(dmu_tx_hold_t, txh_node));
73 list_create(&tx->tx_callbacks, sizeof (dmu_tx_callback_t),
74 offsetof(dmu_tx_callback_t, dcb_node));
75 tx->tx_start = gethrtime();
76 return (tx);
79 dmu_tx_t *
80 dmu_tx_create(objset_t *os)
82 dmu_tx_t *tx = dmu_tx_create_dd(os->os_dsl_dataset->ds_dir);
83 tx->tx_objset = os;
84 return (tx);
87 dmu_tx_t *
88 dmu_tx_create_assigned(struct dsl_pool *dp, uint64_t txg)
90 dmu_tx_t *tx = dmu_tx_create_dd(NULL);
92 TXG_VERIFY(dp->dp_spa, txg);
93 tx->tx_pool = dp;
94 tx->tx_txg = txg;
95 tx->tx_anyobj = TRUE;
97 return (tx);
101 dmu_tx_is_syncing(dmu_tx_t *tx)
103 return (tx->tx_anyobj);
107 dmu_tx_private_ok(dmu_tx_t *tx)
109 return (tx->tx_anyobj);
112 static dmu_tx_hold_t *
113 dmu_tx_hold_dnode_impl(dmu_tx_t *tx, dnode_t *dn, enum dmu_tx_hold_type type,
114 uint64_t arg1, uint64_t arg2)
116 dmu_tx_hold_t *txh;
118 if (dn != NULL) {
119 (void) zfs_refcount_add(&dn->dn_holds, tx);
120 if (tx->tx_txg != 0) {
121 mutex_enter(&dn->dn_mtx);
123 * dn->dn_assigned_txg == tx->tx_txg doesn't pose a
124 * problem, but there's no way for it to happen (for
125 * now, at least).
127 ASSERT(dn->dn_assigned_txg == 0);
128 dn->dn_assigned_txg = tx->tx_txg;
129 (void) zfs_refcount_add(&dn->dn_tx_holds, tx);
130 mutex_exit(&dn->dn_mtx);
134 txh = kmem_zalloc(sizeof (dmu_tx_hold_t), KM_SLEEP);
135 txh->txh_tx = tx;
136 txh->txh_dnode = dn;
137 zfs_refcount_create(&txh->txh_space_towrite);
138 zfs_refcount_create(&txh->txh_memory_tohold);
139 txh->txh_type = type;
140 txh->txh_arg1 = arg1;
141 txh->txh_arg2 = arg2;
142 list_insert_tail(&tx->tx_holds, txh);
144 return (txh);
147 static dmu_tx_hold_t *
148 dmu_tx_hold_object_impl(dmu_tx_t *tx, objset_t *os, uint64_t object,
149 enum dmu_tx_hold_type type, uint64_t arg1, uint64_t arg2)
151 dnode_t *dn = NULL;
152 dmu_tx_hold_t *txh;
153 int err;
155 if (object != DMU_NEW_OBJECT) {
156 err = dnode_hold(os, object, FTAG, &dn);
157 if (err != 0) {
158 tx->tx_err = err;
159 return (NULL);
162 txh = dmu_tx_hold_dnode_impl(tx, dn, type, arg1, arg2);
163 if (dn != NULL)
164 dnode_rele(dn, FTAG);
165 return (txh);
168 void
169 dmu_tx_add_new_object(dmu_tx_t *tx, dnode_t *dn)
172 * If we're syncing, they can manipulate any object anyhow, and
173 * the hold on the dnode_t can cause problems.
175 if (!dmu_tx_is_syncing(tx))
176 (void) dmu_tx_hold_dnode_impl(tx, dn, THT_NEWOBJECT, 0, 0);
180 * This function reads specified data from disk. The specified data will
181 * be needed to perform the transaction -- i.e, it will be read after
182 * we do dmu_tx_assign(). There are two reasons that we read the data now
183 * (before dmu_tx_assign()):
185 * 1. Reading it now has potentially better performance. The transaction
186 * has not yet been assigned, so the TXG is not held open, and also the
187 * caller typically has less locks held when calling dmu_tx_hold_*() than
188 * after the transaction has been assigned. This reduces the lock (and txg)
189 * hold times, thus reducing lock contention.
191 * 2. It is easier for callers (primarily the ZPL) to handle i/o errors
192 * that are detected before they start making changes to the DMU state
193 * (i.e. now). Once the transaction has been assigned, and some DMU
194 * state has been changed, it can be difficult to recover from an i/o
195 * error (e.g. to undo the changes already made in memory at the DMU
196 * layer). Typically code to do so does not exist in the caller -- it
197 * assumes that the data has already been cached and thus i/o errors are
198 * not possible.
200 * It has been observed that the i/o initiated here can be a performance
201 * problem, and it appears to be optional, because we don't look at the
202 * data which is read. However, removing this read would only serve to
203 * move the work elsewhere (after the dmu_tx_assign()), where it may
204 * have a greater impact on performance (in addition to the impact on
205 * fault tolerance noted above).
207 static int
208 dmu_tx_check_ioerr(zio_t *zio, dnode_t *dn, int level, uint64_t blkid)
210 int err;
211 dmu_buf_impl_t *db;
213 rw_enter(&dn->dn_struct_rwlock, RW_READER);
214 err = dbuf_hold_impl(dn, level, blkid, TRUE, FALSE, FTAG, &db);
215 rw_exit(&dn->dn_struct_rwlock);
216 if (err == ENOENT)
217 return (0);
218 if (err != 0)
219 return (err);
221 * PARTIAL_FIRST allows caching for uncacheable blocks. It will
222 * be cleared after dmu_buf_will_dirty() call dbuf_read() again.
224 err = dbuf_read(db, zio, DB_RF_CANFAIL | DB_RF_NOPREFETCH |
225 (level == 0 ? DB_RF_PARTIAL_FIRST : 0));
226 dbuf_rele(db, FTAG);
227 return (err);
230 static void
231 dmu_tx_count_write(dmu_tx_hold_t *txh, uint64_t off, uint64_t len)
233 dnode_t *dn = txh->txh_dnode;
234 int err = 0;
236 if (len == 0)
237 return;
239 (void) zfs_refcount_add_many(&txh->txh_space_towrite, len, FTAG);
241 if (dn == NULL)
242 return;
245 * For i/o error checking, read the blocks that will be needed
246 * to perform the write: the first and last level-0 blocks (if
247 * they are not aligned, i.e. if they are partial-block writes),
248 * and all the level-1 blocks.
250 if (dn->dn_maxblkid == 0) {
251 if (off < dn->dn_datablksz &&
252 (off > 0 || len < dn->dn_datablksz)) {
253 err = dmu_tx_check_ioerr(NULL, dn, 0, 0);
254 if (err != 0) {
255 txh->txh_tx->tx_err = err;
258 } else {
259 zio_t *zio = zio_root(dn->dn_objset->os_spa,
260 NULL, NULL, ZIO_FLAG_CANFAIL);
262 /* first level-0 block */
263 uint64_t start = off >> dn->dn_datablkshift;
264 if (P2PHASE(off, dn->dn_datablksz) || len < dn->dn_datablksz) {
265 err = dmu_tx_check_ioerr(zio, dn, 0, start);
266 if (err != 0) {
267 txh->txh_tx->tx_err = err;
271 /* last level-0 block */
272 uint64_t end = (off + len - 1) >> dn->dn_datablkshift;
273 if (end != start && end <= dn->dn_maxblkid &&
274 P2PHASE(off + len, dn->dn_datablksz)) {
275 err = dmu_tx_check_ioerr(zio, dn, 0, end);
276 if (err != 0) {
277 txh->txh_tx->tx_err = err;
281 /* level-1 blocks */
282 if (dn->dn_nlevels > 1) {
283 int shft = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
284 for (uint64_t i = (start >> shft) + 1;
285 i < end >> shft; i++) {
286 err = dmu_tx_check_ioerr(zio, dn, 1, i);
287 if (err != 0) {
288 txh->txh_tx->tx_err = err;
293 err = zio_wait(zio);
294 if (err != 0) {
295 txh->txh_tx->tx_err = err;
300 static void
301 dmu_tx_count_append(dmu_tx_hold_t *txh, uint64_t off, uint64_t len)
303 dnode_t *dn = txh->txh_dnode;
304 int err = 0;
306 if (len == 0)
307 return;
309 (void) zfs_refcount_add_many(&txh->txh_space_towrite, len, FTAG);
311 if (dn == NULL)
312 return;
315 * For i/o error checking, read the blocks that will be needed
316 * to perform the append; first level-0 block (if not aligned, i.e.
317 * if they are partial-block writes), no additional blocks are read.
319 if (dn->dn_maxblkid == 0) {
320 if (off < dn->dn_datablksz &&
321 (off > 0 || len < dn->dn_datablksz)) {
322 err = dmu_tx_check_ioerr(NULL, dn, 0, 0);
323 if (err != 0) {
324 txh->txh_tx->tx_err = err;
327 } else {
328 zio_t *zio = zio_root(dn->dn_objset->os_spa,
329 NULL, NULL, ZIO_FLAG_CANFAIL);
331 /* first level-0 block */
332 uint64_t start = off >> dn->dn_datablkshift;
333 if (P2PHASE(off, dn->dn_datablksz) || len < dn->dn_datablksz) {
334 err = dmu_tx_check_ioerr(zio, dn, 0, start);
335 if (err != 0) {
336 txh->txh_tx->tx_err = err;
340 err = zio_wait(zio);
341 if (err != 0) {
342 txh->txh_tx->tx_err = err;
347 static void
348 dmu_tx_count_dnode(dmu_tx_hold_t *txh)
350 (void) zfs_refcount_add_many(&txh->txh_space_towrite,
351 DNODE_MIN_SIZE, FTAG);
354 void
355 dmu_tx_hold_write(dmu_tx_t *tx, uint64_t object, uint64_t off, int len)
357 dmu_tx_hold_t *txh;
359 ASSERT0(tx->tx_txg);
360 ASSERT3U(len, <=, DMU_MAX_ACCESS);
361 ASSERT(len == 0 || UINT64_MAX - off >= len - 1);
363 txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
364 object, THT_WRITE, off, len);
365 if (txh != NULL) {
366 dmu_tx_count_write(txh, off, len);
367 dmu_tx_count_dnode(txh);
371 void
372 dmu_tx_hold_write_by_dnode(dmu_tx_t *tx, dnode_t *dn, uint64_t off, int len)
374 dmu_tx_hold_t *txh;
376 ASSERT0(tx->tx_txg);
377 ASSERT3U(len, <=, DMU_MAX_ACCESS);
378 ASSERT(len == 0 || UINT64_MAX - off >= len - 1);
380 txh = dmu_tx_hold_dnode_impl(tx, dn, THT_WRITE, off, len);
381 if (txh != NULL) {
382 dmu_tx_count_write(txh, off, len);
383 dmu_tx_count_dnode(txh);
388 * Should be used when appending to an object and the exact offset is unknown.
389 * The write must occur at or beyond the specified offset. Only the L0 block
390 * at provided offset will be prefetched.
392 void
393 dmu_tx_hold_append(dmu_tx_t *tx, uint64_t object, uint64_t off, int len)
395 dmu_tx_hold_t *txh;
397 ASSERT0(tx->tx_txg);
398 ASSERT3U(len, <=, DMU_MAX_ACCESS);
400 txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
401 object, THT_APPEND, off, DMU_OBJECT_END);
402 if (txh != NULL) {
403 dmu_tx_count_append(txh, off, len);
404 dmu_tx_count_dnode(txh);
408 void
409 dmu_tx_hold_append_by_dnode(dmu_tx_t *tx, dnode_t *dn, uint64_t off, int len)
411 dmu_tx_hold_t *txh;
413 ASSERT0(tx->tx_txg);
414 ASSERT3U(len, <=, DMU_MAX_ACCESS);
416 txh = dmu_tx_hold_dnode_impl(tx, dn, THT_APPEND, off, DMU_OBJECT_END);
417 if (txh != NULL) {
418 dmu_tx_count_append(txh, off, len);
419 dmu_tx_count_dnode(txh);
424 * This function marks the transaction as being a "net free". The end
425 * result is that refquotas will be disabled for this transaction, and
426 * this transaction will be able to use half of the pool space overhead
427 * (see dsl_pool_adjustedsize()). Therefore this function should only
428 * be called for transactions that we expect will not cause a net increase
429 * in the amount of space used (but it's OK if that is occasionally not true).
431 void
432 dmu_tx_mark_netfree(dmu_tx_t *tx)
434 tx->tx_netfree = B_TRUE;
437 static void
438 dmu_tx_count_free(dmu_tx_hold_t *txh, uint64_t off, uint64_t len)
440 dmu_tx_t *tx = txh->txh_tx;
441 dnode_t *dn = txh->txh_dnode;
442 int err;
444 ASSERT(tx->tx_txg == 0);
446 if (off >= (dn->dn_maxblkid + 1) * dn->dn_datablksz)
447 return;
448 if (len == DMU_OBJECT_END)
449 len = (dn->dn_maxblkid + 1) * dn->dn_datablksz - off;
452 * For i/o error checking, we read the first and last level-0
453 * blocks if they are not aligned, and all the level-1 blocks.
455 * Note: dbuf_free_range() assumes that we have not instantiated
456 * any level-0 dbufs that will be completely freed. Therefore we must
457 * exercise care to not read or count the first and last blocks
458 * if they are blocksize-aligned.
460 if (dn->dn_datablkshift == 0) {
461 if (off != 0 || len < dn->dn_datablksz)
462 dmu_tx_count_write(txh, 0, dn->dn_datablksz);
463 } else {
464 /* first block will be modified if it is not aligned */
465 if (!IS_P2ALIGNED(off, 1 << dn->dn_datablkshift))
466 dmu_tx_count_write(txh, off, 1);
467 /* last block will be modified if it is not aligned */
468 if (!IS_P2ALIGNED(off + len, 1 << dn->dn_datablkshift))
469 dmu_tx_count_write(txh, off + len, 1);
473 * Check level-1 blocks.
475 if (dn->dn_nlevels > 1) {
476 int shift = dn->dn_datablkshift + dn->dn_indblkshift -
477 SPA_BLKPTRSHIFT;
478 uint64_t start = off >> shift;
479 uint64_t end = (off + len) >> shift;
481 ASSERT(dn->dn_indblkshift != 0);
484 * dnode_reallocate() can result in an object with indirect
485 * blocks having an odd data block size. In this case,
486 * just check the single block.
488 if (dn->dn_datablkshift == 0)
489 start = end = 0;
491 zio_t *zio = zio_root(tx->tx_pool->dp_spa,
492 NULL, NULL, ZIO_FLAG_CANFAIL);
493 for (uint64_t i = start; i <= end; i++) {
494 uint64_t ibyte = i << shift;
495 err = dnode_next_offset(dn, 0, &ibyte, 2, 1, 0);
496 i = ibyte >> shift;
497 if (err == ESRCH || i > end)
498 break;
499 if (err != 0) {
500 tx->tx_err = err;
501 (void) zio_wait(zio);
502 return;
505 (void) zfs_refcount_add_many(&txh->txh_memory_tohold,
506 1 << dn->dn_indblkshift, FTAG);
508 err = dmu_tx_check_ioerr(zio, dn, 1, i);
509 if (err != 0) {
510 tx->tx_err = err;
511 (void) zio_wait(zio);
512 return;
515 err = zio_wait(zio);
516 if (err != 0) {
517 tx->tx_err = err;
518 return;
523 void
524 dmu_tx_hold_free(dmu_tx_t *tx, uint64_t object, uint64_t off, uint64_t len)
526 dmu_tx_hold_t *txh;
528 txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
529 object, THT_FREE, off, len);
530 if (txh != NULL) {
531 dmu_tx_count_dnode(txh);
532 dmu_tx_count_free(txh, off, len);
536 void
537 dmu_tx_hold_free_by_dnode(dmu_tx_t *tx, dnode_t *dn, uint64_t off, uint64_t len)
539 dmu_tx_hold_t *txh;
541 txh = dmu_tx_hold_dnode_impl(tx, dn, THT_FREE, off, len);
542 if (txh != NULL) {
543 dmu_tx_count_dnode(txh);
544 dmu_tx_count_free(txh, off, len);
548 static void
549 dmu_tx_count_clone(dmu_tx_hold_t *txh, uint64_t off, uint64_t len)
553 * Reuse dmu_tx_count_free(), it does exactly what we need for clone.
555 dmu_tx_count_free(txh, off, len);
558 void
559 dmu_tx_hold_clone_by_dnode(dmu_tx_t *tx, dnode_t *dn, uint64_t off, int len)
561 dmu_tx_hold_t *txh;
563 ASSERT0(tx->tx_txg);
564 ASSERT(len == 0 || UINT64_MAX - off >= len - 1);
566 txh = dmu_tx_hold_dnode_impl(tx, dn, THT_CLONE, off, len);
567 if (txh != NULL) {
568 dmu_tx_count_dnode(txh);
569 dmu_tx_count_clone(txh, off, len);
573 static void
574 dmu_tx_hold_zap_impl(dmu_tx_hold_t *txh, const char *name)
576 dmu_tx_t *tx = txh->txh_tx;
577 dnode_t *dn = txh->txh_dnode;
578 int err;
580 ASSERT(tx->tx_txg == 0);
582 dmu_tx_count_dnode(txh);
585 * Modifying a almost-full microzap is around the worst case (128KB)
587 * If it is a fat zap, the worst case would be 7*16KB=112KB:
588 * - 3 blocks overwritten: target leaf, ptrtbl block, header block
589 * - 4 new blocks written if adding:
590 * - 2 blocks for possibly split leaves,
591 * - 2 grown ptrtbl blocks
593 (void) zfs_refcount_add_many(&txh->txh_space_towrite,
594 zap_get_micro_max_size(tx->tx_pool->dp_spa), FTAG);
596 if (dn == NULL)
597 return;
599 ASSERT3U(DMU_OT_BYTESWAP(dn->dn_type), ==, DMU_BSWAP_ZAP);
601 if (dn->dn_maxblkid == 0 || name == NULL) {
603 * This is a microzap (only one block), or we don't know
604 * the name. Check the first block for i/o errors.
606 err = dmu_tx_check_ioerr(NULL, dn, 0, 0);
607 if (err != 0) {
608 tx->tx_err = err;
610 } else {
612 * Access the name so that we'll check for i/o errors to
613 * the leaf blocks, etc. We ignore ENOENT, as this name
614 * may not yet exist.
616 err = zap_lookup_by_dnode(dn, name, 8, 0, NULL);
617 if (err == EIO || err == ECKSUM || err == ENXIO) {
618 tx->tx_err = err;
623 void
624 dmu_tx_hold_zap(dmu_tx_t *tx, uint64_t object, int add, const char *name)
626 dmu_tx_hold_t *txh;
628 ASSERT0(tx->tx_txg);
630 txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
631 object, THT_ZAP, add, (uintptr_t)name);
632 if (txh != NULL)
633 dmu_tx_hold_zap_impl(txh, name);
636 void
637 dmu_tx_hold_zap_by_dnode(dmu_tx_t *tx, dnode_t *dn, int add, const char *name)
639 dmu_tx_hold_t *txh;
641 ASSERT0(tx->tx_txg);
642 ASSERT(dn != NULL);
644 txh = dmu_tx_hold_dnode_impl(tx, dn, THT_ZAP, add, (uintptr_t)name);
645 if (txh != NULL)
646 dmu_tx_hold_zap_impl(txh, name);
649 void
650 dmu_tx_hold_bonus(dmu_tx_t *tx, uint64_t object)
652 dmu_tx_hold_t *txh;
654 ASSERT(tx->tx_txg == 0);
656 txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
657 object, THT_BONUS, 0, 0);
658 if (txh)
659 dmu_tx_count_dnode(txh);
662 void
663 dmu_tx_hold_bonus_by_dnode(dmu_tx_t *tx, dnode_t *dn)
665 dmu_tx_hold_t *txh;
667 ASSERT0(tx->tx_txg);
669 txh = dmu_tx_hold_dnode_impl(tx, dn, THT_BONUS, 0, 0);
670 if (txh)
671 dmu_tx_count_dnode(txh);
674 void
675 dmu_tx_hold_space(dmu_tx_t *tx, uint64_t space)
677 dmu_tx_hold_t *txh;
679 ASSERT(tx->tx_txg == 0);
681 txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
682 DMU_NEW_OBJECT, THT_SPACE, space, 0);
683 if (txh) {
684 (void) zfs_refcount_add_many(
685 &txh->txh_space_towrite, space, FTAG);
689 #ifdef ZFS_DEBUG
690 void
691 dmu_tx_dirty_buf(dmu_tx_t *tx, dmu_buf_impl_t *db)
693 boolean_t match_object = B_FALSE;
694 boolean_t match_offset = B_FALSE;
696 DB_DNODE_ENTER(db);
697 dnode_t *dn = DB_DNODE(db);
698 ASSERT(tx->tx_txg != 0);
699 ASSERT(tx->tx_objset == NULL || dn->dn_objset == tx->tx_objset);
700 ASSERT3U(dn->dn_object, ==, db->db.db_object);
702 if (tx->tx_anyobj) {
703 DB_DNODE_EXIT(db);
704 return;
707 /* XXX No checking on the meta dnode for now */
708 if (db->db.db_object == DMU_META_DNODE_OBJECT) {
709 DB_DNODE_EXIT(db);
710 return;
713 for (dmu_tx_hold_t *txh = list_head(&tx->tx_holds); txh != NULL;
714 txh = list_next(&tx->tx_holds, txh)) {
715 ASSERT3U(dn->dn_assigned_txg, ==, tx->tx_txg);
716 if (txh->txh_dnode == dn && txh->txh_type != THT_NEWOBJECT)
717 match_object = TRUE;
718 if (txh->txh_dnode == NULL || txh->txh_dnode == dn) {
719 int datablkshift = dn->dn_datablkshift ?
720 dn->dn_datablkshift : SPA_MAXBLOCKSHIFT;
721 int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
722 int shift = datablkshift + epbs * db->db_level;
723 uint64_t beginblk = shift >= 64 ? 0 :
724 (txh->txh_arg1 >> shift);
725 uint64_t endblk = shift >= 64 ? 0 :
726 ((txh->txh_arg1 + txh->txh_arg2 - 1) >> shift);
727 uint64_t blkid = db->db_blkid;
729 /* XXX txh_arg2 better not be zero... */
731 dprintf("found txh type %x beginblk=%llx endblk=%llx\n",
732 txh->txh_type, (u_longlong_t)beginblk,
733 (u_longlong_t)endblk);
735 switch (txh->txh_type) {
736 case THT_WRITE:
737 if (blkid >= beginblk && blkid <= endblk)
738 match_offset = TRUE;
740 * We will let this hold work for the bonus
741 * or spill buffer so that we don't need to
742 * hold it when creating a new object.
744 if (blkid == DMU_BONUS_BLKID ||
745 blkid == DMU_SPILL_BLKID)
746 match_offset = TRUE;
748 * They might have to increase nlevels,
749 * thus dirtying the new TLIBs. Or the
750 * might have to change the block size,
751 * thus dirying the new lvl=0 blk=0.
753 if (blkid == 0)
754 match_offset = TRUE;
755 break;
756 case THT_APPEND:
757 if (blkid >= beginblk && (blkid <= endblk ||
758 txh->txh_arg2 == DMU_OBJECT_END))
759 match_offset = TRUE;
762 * THT_WRITE used for bonus and spill blocks.
764 ASSERT(blkid != DMU_BONUS_BLKID &&
765 blkid != DMU_SPILL_BLKID);
768 * They might have to increase nlevels,
769 * thus dirtying the new TLIBs. Or the
770 * might have to change the block size,
771 * thus dirying the new lvl=0 blk=0.
773 if (blkid == 0)
774 match_offset = TRUE;
775 break;
776 case THT_FREE:
778 * We will dirty all the level 1 blocks in
779 * the free range and perhaps the first and
780 * last level 0 block.
782 if (blkid >= beginblk && (blkid <= endblk ||
783 txh->txh_arg2 == DMU_OBJECT_END))
784 match_offset = TRUE;
785 break;
786 case THT_SPILL:
787 if (blkid == DMU_SPILL_BLKID)
788 match_offset = TRUE;
789 break;
790 case THT_BONUS:
791 if (blkid == DMU_BONUS_BLKID)
792 match_offset = TRUE;
793 break;
794 case THT_ZAP:
795 match_offset = TRUE;
796 break;
797 case THT_NEWOBJECT:
798 match_object = TRUE;
799 break;
800 case THT_CLONE:
801 if (blkid >= beginblk && blkid <= endblk)
802 match_offset = TRUE;
803 break;
804 default:
805 cmn_err(CE_PANIC, "bad txh_type %d",
806 txh->txh_type);
809 if (match_object && match_offset) {
810 DB_DNODE_EXIT(db);
811 return;
814 DB_DNODE_EXIT(db);
815 panic("dirtying dbuf obj=%llx lvl=%u blkid=%llx but not tx_held\n",
816 (u_longlong_t)db->db.db_object, db->db_level,
817 (u_longlong_t)db->db_blkid);
819 #endif
822 * If we can't do 10 iops, something is wrong. Let us go ahead
823 * and hit zfs_dirty_data_max.
825 static const hrtime_t zfs_delay_max_ns = 100 * MICROSEC; /* 100 milliseconds */
828 * We delay transactions when we've determined that the backend storage
829 * isn't able to accommodate the rate of incoming writes.
831 * If there is already a transaction waiting, we delay relative to when
832 * that transaction finishes waiting. This way the calculated min_time
833 * is independent of the number of threads concurrently executing
834 * transactions.
836 * If we are the only waiter, wait relative to when the transaction
837 * started, rather than the current time. This credits the transaction for
838 * "time already served", e.g. reading indirect blocks.
840 * The minimum time for a transaction to take is calculated as:
841 * min_time = scale * (dirty - min) / (max - dirty)
842 * min_time is then capped at zfs_delay_max_ns.
844 * The delay has two degrees of freedom that can be adjusted via tunables.
845 * The percentage of dirty data at which we start to delay is defined by
846 * zfs_delay_min_dirty_percent. This should typically be at or above
847 * zfs_vdev_async_write_active_max_dirty_percent so that we only start to
848 * delay after writing at full speed has failed to keep up with the incoming
849 * write rate. The scale of the curve is defined by zfs_delay_scale. Roughly
850 * speaking, this variable determines the amount of delay at the midpoint of
851 * the curve.
853 * delay
854 * 10ms +-------------------------------------------------------------*+
855 * | *|
856 * 9ms + *+
857 * | *|
858 * 8ms + *+
859 * | * |
860 * 7ms + * +
861 * | * |
862 * 6ms + * +
863 * | * |
864 * 5ms + * +
865 * | * |
866 * 4ms + * +
867 * | * |
868 * 3ms + * +
869 * | * |
870 * 2ms + (midpoint) * +
871 * | | ** |
872 * 1ms + v *** +
873 * | zfs_delay_scale ----------> ******** |
874 * 0 +-------------------------------------*********----------------+
875 * 0% <- zfs_dirty_data_max -> 100%
877 * Note that since the delay is added to the outstanding time remaining on the
878 * most recent transaction, the delay is effectively the inverse of IOPS.
879 * Here the midpoint of 500us translates to 2000 IOPS. The shape of the curve
880 * was chosen such that small changes in the amount of accumulated dirty data
881 * in the first 3/4 of the curve yield relatively small differences in the
882 * amount of delay.
884 * The effects can be easier to understand when the amount of delay is
885 * represented on a log scale:
887 * delay
888 * 100ms +-------------------------------------------------------------++
889 * + +
890 * | |
891 * + *+
892 * 10ms + *+
893 * + ** +
894 * | (midpoint) ** |
895 * + | ** +
896 * 1ms + v **** +
897 * + zfs_delay_scale ----------> ***** +
898 * | **** |
899 * + **** +
900 * 100us + ** +
901 * + * +
902 * | * |
903 * + * +
904 * 10us + * +
905 * + +
906 * | |
907 * + +
908 * +--------------------------------------------------------------+
909 * 0% <- zfs_dirty_data_max -> 100%
911 * Note here that only as the amount of dirty data approaches its limit does
912 * the delay start to increase rapidly. The goal of a properly tuned system
913 * should be to keep the amount of dirty data out of that range by first
914 * ensuring that the appropriate limits are set for the I/O scheduler to reach
915 * optimal throughput on the backend storage, and then by changing the value
916 * of zfs_delay_scale to increase the steepness of the curve.
918 static void
919 dmu_tx_delay(dmu_tx_t *tx, uint64_t dirty)
921 dsl_pool_t *dp = tx->tx_pool;
922 uint64_t delay_min_bytes, wrlog;
923 hrtime_t wakeup, tx_time = 0, now;
925 /* Calculate minimum transaction time for the dirty data amount. */
926 delay_min_bytes =
927 zfs_dirty_data_max * zfs_delay_min_dirty_percent / 100;
928 if (dirty > delay_min_bytes) {
930 * The caller has already waited until we are under the max.
931 * We make them pass us the amount of dirty data so we don't
932 * have to handle the case of it being >= the max, which
933 * could cause a divide-by-zero if it's == the max.
935 ASSERT3U(dirty, <, zfs_dirty_data_max);
937 tx_time = zfs_delay_scale * (dirty - delay_min_bytes) /
938 (zfs_dirty_data_max - dirty);
941 /* Calculate minimum transaction time for the TX_WRITE log size. */
942 wrlog = aggsum_upper_bound(&dp->dp_wrlog_total);
943 delay_min_bytes =
944 zfs_wrlog_data_max * zfs_delay_min_dirty_percent / 100;
945 if (wrlog >= zfs_wrlog_data_max) {
946 tx_time = zfs_delay_max_ns;
947 } else if (wrlog > delay_min_bytes) {
948 tx_time = MAX(zfs_delay_scale * (wrlog - delay_min_bytes) /
949 (zfs_wrlog_data_max - wrlog), tx_time);
952 if (tx_time == 0)
953 return;
955 tx_time = MIN(tx_time, zfs_delay_max_ns);
956 now = gethrtime();
957 if (now > tx->tx_start + tx_time)
958 return;
960 DTRACE_PROBE3(delay__mintime, dmu_tx_t *, tx, uint64_t, dirty,
961 uint64_t, tx_time);
963 mutex_enter(&dp->dp_lock);
964 wakeup = MAX(tx->tx_start + tx_time, dp->dp_last_wakeup + tx_time);
965 dp->dp_last_wakeup = wakeup;
966 mutex_exit(&dp->dp_lock);
968 zfs_sleep_until(wakeup);
972 * This routine attempts to assign the transaction to a transaction group.
973 * To do so, we must determine if there is sufficient free space on disk.
975 * If this is a "netfree" transaction (i.e. we called dmu_tx_mark_netfree()
976 * on it), then it is assumed that there is sufficient free space,
977 * unless there's insufficient slop space in the pool (see the comment
978 * above spa_slop_shift in spa_misc.c).
980 * If it is not a "netfree" transaction, then if the data already on disk
981 * is over the allowed usage (e.g. quota), this will fail with EDQUOT or
982 * ENOSPC. Otherwise, if the current rough estimate of pending changes,
983 * plus the rough estimate of this transaction's changes, may exceed the
984 * allowed usage, then this will fail with ERESTART, which will cause the
985 * caller to wait for the pending changes to be written to disk (by waiting
986 * for the next TXG to open), and then check the space usage again.
988 * The rough estimate of pending changes is comprised of the sum of:
990 * - this transaction's holds' txh_space_towrite
992 * - dd_tempreserved[], which is the sum of in-flight transactions'
993 * holds' txh_space_towrite (i.e. those transactions that have called
994 * dmu_tx_assign() but not yet called dmu_tx_commit()).
996 * - dd_space_towrite[], which is the amount of dirtied dbufs.
998 * Note that all of these values are inflated by spa_get_worst_case_asize(),
999 * which means that we may get ERESTART well before we are actually in danger
1000 * of running out of space, but this also mitigates any small inaccuracies
1001 * in the rough estimate (e.g. txh_space_towrite doesn't take into account
1002 * indirect blocks, and dd_space_towrite[] doesn't take into account changes
1003 * to the MOS).
1005 * Note that due to this algorithm, it is possible to exceed the allowed
1006 * usage by one transaction. Also, as we approach the allowed usage,
1007 * we will allow a very limited amount of changes into each TXG, thus
1008 * decreasing performance.
1010 static int
1011 dmu_tx_try_assign(dmu_tx_t *tx, uint64_t txg_how)
1013 spa_t *spa = tx->tx_pool->dp_spa;
1015 ASSERT0(tx->tx_txg);
1017 if (tx->tx_err) {
1018 DMU_TX_STAT_BUMP(dmu_tx_error);
1019 return (tx->tx_err);
1022 if (spa_suspended(spa)) {
1023 DMU_TX_STAT_BUMP(dmu_tx_suspended);
1026 * If the user has indicated a blocking failure mode
1027 * then return ERESTART which will block in dmu_tx_wait().
1028 * Otherwise, return EIO so that an error can get
1029 * propagated back to the VOP calls.
1031 * Note that we always honor the txg_how flag regardless
1032 * of the failuremode setting.
1034 if (spa_get_failmode(spa) == ZIO_FAILURE_MODE_CONTINUE &&
1035 !(txg_how & TXG_WAIT))
1036 return (SET_ERROR(EIO));
1038 return (SET_ERROR(ERESTART));
1041 if (!tx->tx_dirty_delayed &&
1042 dsl_pool_need_wrlog_delay(tx->tx_pool)) {
1043 tx->tx_wait_dirty = B_TRUE;
1044 DMU_TX_STAT_BUMP(dmu_tx_wrlog_delay);
1045 return (SET_ERROR(ERESTART));
1048 if (!tx->tx_dirty_delayed &&
1049 dsl_pool_need_dirty_delay(tx->tx_pool)) {
1050 tx->tx_wait_dirty = B_TRUE;
1051 DMU_TX_STAT_BUMP(dmu_tx_dirty_delay);
1052 return (SET_ERROR(ERESTART));
1055 tx->tx_txg = txg_hold_open(tx->tx_pool, &tx->tx_txgh);
1056 tx->tx_needassign_txh = NULL;
1059 * NB: No error returns are allowed after txg_hold_open, but
1060 * before processing the dnode holds, due to the
1061 * dmu_tx_unassign() logic.
1064 uint64_t towrite = 0;
1065 uint64_t tohold = 0;
1066 for (dmu_tx_hold_t *txh = list_head(&tx->tx_holds); txh != NULL;
1067 txh = list_next(&tx->tx_holds, txh)) {
1068 dnode_t *dn = txh->txh_dnode;
1069 if (dn != NULL) {
1071 * This thread can't hold the dn_struct_rwlock
1072 * while assigning the tx, because this can lead to
1073 * deadlock. Specifically, if this dnode is already
1074 * assigned to an earlier txg, this thread may need
1075 * to wait for that txg to sync (the ERESTART case
1076 * below). The other thread that has assigned this
1077 * dnode to an earlier txg prevents this txg from
1078 * syncing until its tx can complete (calling
1079 * dmu_tx_commit()), but it may need to acquire the
1080 * dn_struct_rwlock to do so (e.g. via
1081 * dmu_buf_hold*()).
1083 * Note that this thread can't hold the lock for
1084 * read either, but the rwlock doesn't record
1085 * enough information to make that assertion.
1087 ASSERT(!RW_WRITE_HELD(&dn->dn_struct_rwlock));
1089 mutex_enter(&dn->dn_mtx);
1090 if (dn->dn_assigned_txg == tx->tx_txg - 1) {
1091 mutex_exit(&dn->dn_mtx);
1092 tx->tx_needassign_txh = txh;
1093 DMU_TX_STAT_BUMP(dmu_tx_group);
1094 return (SET_ERROR(ERESTART));
1096 if (dn->dn_assigned_txg == 0)
1097 dn->dn_assigned_txg = tx->tx_txg;
1098 ASSERT3U(dn->dn_assigned_txg, ==, tx->tx_txg);
1099 (void) zfs_refcount_add(&dn->dn_tx_holds, tx);
1100 mutex_exit(&dn->dn_mtx);
1102 towrite += zfs_refcount_count(&txh->txh_space_towrite);
1103 tohold += zfs_refcount_count(&txh->txh_memory_tohold);
1106 /* needed allocation: worst-case estimate of write space */
1107 uint64_t asize = spa_get_worst_case_asize(tx->tx_pool->dp_spa, towrite);
1108 /* calculate memory footprint estimate */
1109 uint64_t memory = towrite + tohold;
1111 if (tx->tx_dir != NULL && asize != 0) {
1112 int err = dsl_dir_tempreserve_space(tx->tx_dir, memory,
1113 asize, tx->tx_netfree, &tx->tx_tempreserve_cookie, tx);
1114 if (err != 0)
1115 return (err);
1118 DMU_TX_STAT_BUMP(dmu_tx_assigned);
1120 return (0);
1123 static void
1124 dmu_tx_unassign(dmu_tx_t *tx)
1126 if (tx->tx_txg == 0)
1127 return;
1129 txg_rele_to_quiesce(&tx->tx_txgh);
1132 * Walk the transaction's hold list, removing the hold on the
1133 * associated dnode, and notifying waiters if the refcount drops to 0.
1135 for (dmu_tx_hold_t *txh = list_head(&tx->tx_holds);
1136 txh && txh != tx->tx_needassign_txh;
1137 txh = list_next(&tx->tx_holds, txh)) {
1138 dnode_t *dn = txh->txh_dnode;
1140 if (dn == NULL)
1141 continue;
1142 mutex_enter(&dn->dn_mtx);
1143 ASSERT3U(dn->dn_assigned_txg, ==, tx->tx_txg);
1145 if (zfs_refcount_remove(&dn->dn_tx_holds, tx) == 0) {
1146 dn->dn_assigned_txg = 0;
1147 cv_broadcast(&dn->dn_notxholds);
1149 mutex_exit(&dn->dn_mtx);
1152 txg_rele_to_sync(&tx->tx_txgh);
1154 tx->tx_lasttried_txg = tx->tx_txg;
1155 tx->tx_txg = 0;
1159 * Assign tx to a transaction group; txg_how is a bitmask:
1161 * If TXG_WAIT is set and the currently open txg is full, this function
1162 * will wait until there's a new txg. This should be used when no locks
1163 * are being held. With this bit set, this function will only fail if
1164 * we're truly out of space (or over quota).
1166 * If TXG_WAIT is *not* set and we can't assign into the currently open
1167 * txg without blocking, this function will return immediately with
1168 * ERESTART. This should be used whenever locks are being held. On an
1169 * ERESTART error, the caller should drop all locks, call dmu_tx_wait(),
1170 * and try again.
1172 * If TXG_NOTHROTTLE is set, this indicates that this tx should not be
1173 * delayed due on the ZFS Write Throttle (see comments in dsl_pool.c for
1174 * details on the throttle). This is used by the VFS operations, after
1175 * they have already called dmu_tx_wait() (though most likely on a
1176 * different tx).
1178 * It is guaranteed that subsequent successful calls to dmu_tx_assign()
1179 * will assign the tx to monotonically increasing txgs. Of course this is
1180 * not strong monotonicity, because the same txg can be returned multiple
1181 * times in a row. This guarantee holds both for subsequent calls from
1182 * one thread and for multiple threads. For example, it is impossible to
1183 * observe the following sequence of events:
1185 * Thread 1 Thread 2
1187 * dmu_tx_assign(T1, ...)
1188 * 1 <- dmu_tx_get_txg(T1)
1189 * dmu_tx_assign(T2, ...)
1190 * 2 <- dmu_tx_get_txg(T2)
1191 * dmu_tx_assign(T3, ...)
1192 * 1 <- dmu_tx_get_txg(T3)
1195 dmu_tx_assign(dmu_tx_t *tx, uint64_t txg_how)
1197 int err;
1199 ASSERT(tx->tx_txg == 0);
1200 ASSERT0(txg_how & ~(TXG_WAIT | TXG_NOTHROTTLE));
1201 ASSERT(!dsl_pool_sync_context(tx->tx_pool));
1203 /* If we might wait, we must not hold the config lock. */
1204 IMPLY((txg_how & TXG_WAIT), !dsl_pool_config_held(tx->tx_pool));
1206 if ((txg_how & TXG_NOTHROTTLE))
1207 tx->tx_dirty_delayed = B_TRUE;
1209 while ((err = dmu_tx_try_assign(tx, txg_how)) != 0) {
1210 dmu_tx_unassign(tx);
1212 if (err != ERESTART || !(txg_how & TXG_WAIT))
1213 return (err);
1215 dmu_tx_wait(tx);
1218 txg_rele_to_quiesce(&tx->tx_txgh);
1220 return (0);
1223 void
1224 dmu_tx_wait(dmu_tx_t *tx)
1226 spa_t *spa = tx->tx_pool->dp_spa;
1227 dsl_pool_t *dp = tx->tx_pool;
1228 hrtime_t before;
1230 ASSERT(tx->tx_txg == 0);
1231 ASSERT(!dsl_pool_config_held(tx->tx_pool));
1233 before = gethrtime();
1235 if (tx->tx_wait_dirty) {
1236 uint64_t dirty;
1239 * dmu_tx_try_assign() has determined that we need to wait
1240 * because we've consumed much or all of the dirty buffer
1241 * space.
1243 mutex_enter(&dp->dp_lock);
1244 if (dp->dp_dirty_total >= zfs_dirty_data_max)
1245 DMU_TX_STAT_BUMP(dmu_tx_dirty_over_max);
1246 while (dp->dp_dirty_total >= zfs_dirty_data_max)
1247 cv_wait(&dp->dp_spaceavail_cv, &dp->dp_lock);
1248 dirty = dp->dp_dirty_total;
1249 mutex_exit(&dp->dp_lock);
1251 dmu_tx_delay(tx, dirty);
1253 tx->tx_wait_dirty = B_FALSE;
1256 * Note: setting tx_dirty_delayed only has effect if the
1257 * caller used TX_WAIT. Otherwise they are going to
1258 * destroy this tx and try again. The common case,
1259 * zfs_write(), uses TX_WAIT.
1261 tx->tx_dirty_delayed = B_TRUE;
1262 } else if (spa_suspended(spa) || tx->tx_lasttried_txg == 0) {
1264 * If the pool is suspended we need to wait until it
1265 * is resumed. Note that it's possible that the pool
1266 * has become active after this thread has tried to
1267 * obtain a tx. If that's the case then tx_lasttried_txg
1268 * would not have been set.
1270 txg_wait_synced(dp, spa_last_synced_txg(spa) + 1);
1271 } else if (tx->tx_needassign_txh) {
1272 dnode_t *dn = tx->tx_needassign_txh->txh_dnode;
1274 mutex_enter(&dn->dn_mtx);
1275 while (dn->dn_assigned_txg == tx->tx_lasttried_txg - 1)
1276 cv_wait(&dn->dn_notxholds, &dn->dn_mtx);
1277 mutex_exit(&dn->dn_mtx);
1278 tx->tx_needassign_txh = NULL;
1279 } else {
1281 * If we have a lot of dirty data just wait until we sync
1282 * out a TXG at which point we'll hopefully have synced
1283 * a portion of the changes.
1285 txg_wait_synced(dp, spa_last_synced_txg(spa) + 1);
1288 spa_tx_assign_add_nsecs(spa, gethrtime() - before);
1291 static void
1292 dmu_tx_destroy(dmu_tx_t *tx)
1294 dmu_tx_hold_t *txh;
1296 while ((txh = list_head(&tx->tx_holds)) != NULL) {
1297 dnode_t *dn = txh->txh_dnode;
1299 list_remove(&tx->tx_holds, txh);
1300 zfs_refcount_destroy_many(&txh->txh_space_towrite,
1301 zfs_refcount_count(&txh->txh_space_towrite));
1302 zfs_refcount_destroy_many(&txh->txh_memory_tohold,
1303 zfs_refcount_count(&txh->txh_memory_tohold));
1304 kmem_free(txh, sizeof (dmu_tx_hold_t));
1305 if (dn != NULL)
1306 dnode_rele(dn, tx);
1309 list_destroy(&tx->tx_callbacks);
1310 list_destroy(&tx->tx_holds);
1311 kmem_free(tx, sizeof (dmu_tx_t));
1314 void
1315 dmu_tx_commit(dmu_tx_t *tx)
1317 ASSERT(tx->tx_txg != 0);
1320 * Go through the transaction's hold list and remove holds on
1321 * associated dnodes, notifying waiters if no holds remain.
1323 for (dmu_tx_hold_t *txh = list_head(&tx->tx_holds); txh != NULL;
1324 txh = list_next(&tx->tx_holds, txh)) {
1325 dnode_t *dn = txh->txh_dnode;
1327 if (dn == NULL)
1328 continue;
1330 mutex_enter(&dn->dn_mtx);
1331 ASSERT3U(dn->dn_assigned_txg, ==, tx->tx_txg);
1333 if (zfs_refcount_remove(&dn->dn_tx_holds, tx) == 0) {
1334 dn->dn_assigned_txg = 0;
1335 cv_broadcast(&dn->dn_notxholds);
1337 mutex_exit(&dn->dn_mtx);
1340 if (tx->tx_tempreserve_cookie)
1341 dsl_dir_tempreserve_clear(tx->tx_tempreserve_cookie, tx);
1343 if (!list_is_empty(&tx->tx_callbacks))
1344 txg_register_callbacks(&tx->tx_txgh, &tx->tx_callbacks);
1346 if (tx->tx_anyobj == FALSE)
1347 txg_rele_to_sync(&tx->tx_txgh);
1349 dmu_tx_destroy(tx);
1352 void
1353 dmu_tx_abort(dmu_tx_t *tx)
1355 ASSERT(tx->tx_txg == 0);
1358 * Call any registered callbacks with an error code.
1360 if (!list_is_empty(&tx->tx_callbacks))
1361 dmu_tx_do_callbacks(&tx->tx_callbacks, SET_ERROR(ECANCELED));
1363 dmu_tx_destroy(tx);
1366 uint64_t
1367 dmu_tx_get_txg(dmu_tx_t *tx)
1369 ASSERT(tx->tx_txg != 0);
1370 return (tx->tx_txg);
1373 dsl_pool_t *
1374 dmu_tx_pool(dmu_tx_t *tx)
1376 ASSERT(tx->tx_pool != NULL);
1377 return (tx->tx_pool);
1380 void
1381 dmu_tx_callback_register(dmu_tx_t *tx, dmu_tx_callback_func_t *func, void *data)
1383 dmu_tx_callback_t *dcb;
1385 dcb = kmem_alloc(sizeof (dmu_tx_callback_t), KM_SLEEP);
1387 dcb->dcb_func = func;
1388 dcb->dcb_data = data;
1390 list_insert_tail(&tx->tx_callbacks, dcb);
1394 * Call all the commit callbacks on a list, with a given error code.
1396 void
1397 dmu_tx_do_callbacks(list_t *cb_list, int error)
1399 dmu_tx_callback_t *dcb;
1401 while ((dcb = list_remove_tail(cb_list)) != NULL) {
1402 dcb->dcb_func(dcb->dcb_data, error);
1403 kmem_free(dcb, sizeof (dmu_tx_callback_t));
1408 * Interface to hold a bunch of attributes.
1409 * used for creating new files.
1410 * attrsize is the total size of all attributes
1411 * to be added during object creation
1413 * For updating/adding a single attribute dmu_tx_hold_sa() should be used.
1417 * hold necessary attribute name for attribute registration.
1418 * should be a very rare case where this is needed. If it does
1419 * happen it would only happen on the first write to the file system.
1421 static void
1422 dmu_tx_sa_registration_hold(sa_os_t *sa, dmu_tx_t *tx)
1424 if (!sa->sa_need_attr_registration)
1425 return;
1427 for (int i = 0; i != sa->sa_num_attrs; i++) {
1428 if (!sa->sa_attr_table[i].sa_registered) {
1429 if (sa->sa_reg_attr_obj)
1430 dmu_tx_hold_zap(tx, sa->sa_reg_attr_obj,
1431 B_TRUE, sa->sa_attr_table[i].sa_name);
1432 else
1433 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT,
1434 B_TRUE, sa->sa_attr_table[i].sa_name);
1439 void
1440 dmu_tx_hold_spill(dmu_tx_t *tx, uint64_t object)
1442 dmu_tx_hold_t *txh;
1444 txh = dmu_tx_hold_object_impl(tx, tx->tx_objset, object,
1445 THT_SPILL, 0, 0);
1446 if (txh != NULL)
1447 (void) zfs_refcount_add_many(&txh->txh_space_towrite,
1448 SPA_OLD_MAXBLOCKSIZE, FTAG);
1451 void
1452 dmu_tx_hold_sa_create(dmu_tx_t *tx, int attrsize)
1454 sa_os_t *sa = tx->tx_objset->os_sa;
1456 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1458 if (tx->tx_objset->os_sa->sa_master_obj == 0)
1459 return;
1461 if (tx->tx_objset->os_sa->sa_layout_attr_obj) {
1462 dmu_tx_hold_zap(tx, sa->sa_layout_attr_obj, B_TRUE, NULL);
1463 } else {
1464 dmu_tx_hold_zap(tx, sa->sa_master_obj, B_TRUE, SA_LAYOUTS);
1465 dmu_tx_hold_zap(tx, sa->sa_master_obj, B_TRUE, SA_REGISTRY);
1466 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
1467 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
1470 dmu_tx_sa_registration_hold(sa, tx);
1472 if (attrsize <= DN_OLD_MAX_BONUSLEN && !sa->sa_force_spill)
1473 return;
1475 (void) dmu_tx_hold_object_impl(tx, tx->tx_objset, DMU_NEW_OBJECT,
1476 THT_SPILL, 0, 0);
1480 * Hold SA attribute
1482 * dmu_tx_hold_sa(dmu_tx_t *tx, sa_handle_t *, attribute, add, size)
1484 * variable_size is the total size of all variable sized attributes
1485 * passed to this function. It is not the total size of all
1486 * variable size attributes that *may* exist on this object.
1488 void
1489 dmu_tx_hold_sa(dmu_tx_t *tx, sa_handle_t *hdl, boolean_t may_grow)
1491 uint64_t object;
1492 sa_os_t *sa = tx->tx_objset->os_sa;
1494 ASSERT(hdl != NULL);
1496 object = sa_handle_object(hdl);
1498 dmu_buf_impl_t *db = (dmu_buf_impl_t *)hdl->sa_bonus;
1499 DB_DNODE_ENTER(db);
1500 dmu_tx_hold_bonus_by_dnode(tx, DB_DNODE(db));
1501 DB_DNODE_EXIT(db);
1503 if (tx->tx_objset->os_sa->sa_master_obj == 0)
1504 return;
1506 if (tx->tx_objset->os_sa->sa_reg_attr_obj == 0 ||
1507 tx->tx_objset->os_sa->sa_layout_attr_obj == 0) {
1508 dmu_tx_hold_zap(tx, sa->sa_master_obj, B_TRUE, SA_LAYOUTS);
1509 dmu_tx_hold_zap(tx, sa->sa_master_obj, B_TRUE, SA_REGISTRY);
1510 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
1511 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
1514 dmu_tx_sa_registration_hold(sa, tx);
1516 if (may_grow && tx->tx_objset->os_sa->sa_layout_attr_obj)
1517 dmu_tx_hold_zap(tx, sa->sa_layout_attr_obj, B_TRUE, NULL);
1519 if (sa->sa_force_spill || may_grow || hdl->sa_spill) {
1520 ASSERT(tx->tx_txg == 0);
1521 dmu_tx_hold_spill(tx, object);
1522 } else {
1523 DB_DNODE_ENTER(db);
1524 if (DB_DNODE(db)->dn_have_spill) {
1525 ASSERT(tx->tx_txg == 0);
1526 dmu_tx_hold_spill(tx, object);
1528 DB_DNODE_EXIT(db);
1532 void
1533 dmu_tx_init(void)
1535 dmu_tx_ksp = kstat_create("zfs", 0, "dmu_tx", "misc",
1536 KSTAT_TYPE_NAMED, sizeof (dmu_tx_stats) / sizeof (kstat_named_t),
1537 KSTAT_FLAG_VIRTUAL);
1539 if (dmu_tx_ksp != NULL) {
1540 dmu_tx_ksp->ks_data = &dmu_tx_stats;
1541 kstat_install(dmu_tx_ksp);
1545 void
1546 dmu_tx_fini(void)
1548 if (dmu_tx_ksp != NULL) {
1549 kstat_delete(dmu_tx_ksp);
1550 dmu_tx_ksp = NULL;
1554 #if defined(_KERNEL)
1555 EXPORT_SYMBOL(dmu_tx_create);
1556 EXPORT_SYMBOL(dmu_tx_hold_write);
1557 EXPORT_SYMBOL(dmu_tx_hold_write_by_dnode);
1558 EXPORT_SYMBOL(dmu_tx_hold_append);
1559 EXPORT_SYMBOL(dmu_tx_hold_append_by_dnode);
1560 EXPORT_SYMBOL(dmu_tx_hold_free);
1561 EXPORT_SYMBOL(dmu_tx_hold_free_by_dnode);
1562 EXPORT_SYMBOL(dmu_tx_hold_zap);
1563 EXPORT_SYMBOL(dmu_tx_hold_zap_by_dnode);
1564 EXPORT_SYMBOL(dmu_tx_hold_bonus);
1565 EXPORT_SYMBOL(dmu_tx_hold_bonus_by_dnode);
1566 EXPORT_SYMBOL(dmu_tx_abort);
1567 EXPORT_SYMBOL(dmu_tx_assign);
1568 EXPORT_SYMBOL(dmu_tx_wait);
1569 EXPORT_SYMBOL(dmu_tx_commit);
1570 EXPORT_SYMBOL(dmu_tx_mark_netfree);
1571 EXPORT_SYMBOL(dmu_tx_get_txg);
1572 EXPORT_SYMBOL(dmu_tx_callback_register);
1573 EXPORT_SYMBOL(dmu_tx_do_callbacks);
1574 EXPORT_SYMBOL(dmu_tx_hold_spill);
1575 EXPORT_SYMBOL(dmu_tx_hold_sa_create);
1576 EXPORT_SYMBOL(dmu_tx_hold_sa);
1577 #endif