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 http://www.opensolaris.org/os/licensing.
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]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
25 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
29 #include <sys/dmu_impl.h>
31 #include <sys/dmu_tx.h>
32 #include <sys/dmu_objset.h>
33 #include <sys/dsl_dataset.h> /* for dsl_dataset_block_freeable() */
34 #include <sys/dsl_dir.h> /* for dsl_dir_tempreserve_*() */
35 #include <sys/dsl_pool.h>
36 #include <sys/zap_impl.h> /* for fzap_default_block_shift */
39 #include <sys/sa_impl.h>
40 #include <sys/zfs_context.h>
41 #include <sys/varargs.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_how", KSTAT_DATA_UINT64 },
53 { "dmu_tx_memory_reserve", KSTAT_DATA_UINT64 },
54 { "dmu_tx_memory_reclaim", KSTAT_DATA_UINT64 },
55 { "dmu_tx_memory_inflight", KSTAT_DATA_UINT64 },
56 { "dmu_tx_dirty_throttle", KSTAT_DATA_UINT64 },
57 { "dmu_tx_write_limit", KSTAT_DATA_UINT64 },
58 { "dmu_tx_quota", KSTAT_DATA_UINT64 },
61 static kstat_t *dmu_tx_ksp;
64 dmu_tx_create_dd(dsl_dir_t *dd)
66 dmu_tx_t *tx = kmem_zalloc(sizeof (dmu_tx_t), KM_PUSHPAGE);
69 tx->tx_pool = dd->dd_pool;
70 list_create(&tx->tx_holds, sizeof (dmu_tx_hold_t),
71 offsetof(dmu_tx_hold_t, txh_node));
72 list_create(&tx->tx_callbacks, sizeof (dmu_tx_callback_t),
73 offsetof(dmu_tx_callback_t, dcb_node));
75 refcount_create(&tx->tx_space_written);
76 refcount_create(&tx->tx_space_freed);
82 dmu_tx_create(objset_t *os)
84 dmu_tx_t *tx = dmu_tx_create_dd(os->os_dsl_dataset->ds_dir);
86 tx->tx_lastsnap_txg = dsl_dataset_prev_snap_txg(os->os_dsl_dataset);
91 dmu_tx_create_assigned(struct dsl_pool *dp, uint64_t txg)
93 dmu_tx_t *tx = dmu_tx_create_dd(NULL);
95 ASSERT3U(txg, <=, dp->dp_tx.tx_open_txg);
104 dmu_tx_is_syncing(dmu_tx_t *tx)
106 return (tx->tx_anyobj);
110 dmu_tx_private_ok(dmu_tx_t *tx)
112 return (tx->tx_anyobj);
115 static dmu_tx_hold_t *
116 dmu_tx_hold_object_impl(dmu_tx_t *tx, objset_t *os, uint64_t object,
117 enum dmu_tx_hold_type type, uint64_t arg1, uint64_t arg2)
123 if (object != DMU_NEW_OBJECT) {
124 err = dnode_hold(os, object, tx, &dn);
130 if (err == 0 && tx->tx_txg != 0) {
131 mutex_enter(&dn->dn_mtx);
133 * dn->dn_assigned_txg == tx->tx_txg doesn't pose a
134 * problem, but there's no way for it to happen (for
137 ASSERT(dn->dn_assigned_txg == 0);
138 dn->dn_assigned_txg = tx->tx_txg;
139 (void) refcount_add(&dn->dn_tx_holds, tx);
140 mutex_exit(&dn->dn_mtx);
144 txh = kmem_zalloc(sizeof (dmu_tx_hold_t), KM_PUSHPAGE);
148 txh->txh_type = type;
149 txh->txh_arg1 = arg1;
150 txh->txh_arg2 = arg2;
152 list_insert_tail(&tx->tx_holds, txh);
158 dmu_tx_add_new_object(dmu_tx_t *tx, objset_t *os, uint64_t object)
161 * If we're syncing, they can manipulate any object anyhow, and
162 * the hold on the dnode_t can cause problems.
164 if (!dmu_tx_is_syncing(tx)) {
165 (void) dmu_tx_hold_object_impl(tx, os,
166 object, THT_NEWOBJECT, 0, 0);
171 dmu_tx_check_ioerr(zio_t *zio, dnode_t *dn, int level, uint64_t blkid)
176 rw_enter(&dn->dn_struct_rwlock, RW_READER);
177 db = dbuf_hold_level(dn, level, blkid, FTAG);
178 rw_exit(&dn->dn_struct_rwlock);
181 err = dbuf_read(db, zio, DB_RF_CANFAIL | DB_RF_NOPREFETCH);
187 dmu_tx_count_twig(dmu_tx_hold_t *txh, dnode_t *dn, dmu_buf_impl_t *db,
188 int level, uint64_t blkid, boolean_t freeable, uint64_t *history)
190 objset_t *os = dn->dn_objset;
191 dsl_dataset_t *ds = os->os_dsl_dataset;
192 int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
193 dmu_buf_impl_t *parent = NULL;
197 if (level >= dn->dn_nlevels || history[level] == blkid)
200 history[level] = blkid;
202 space = (level == 0) ? dn->dn_datablksz : (1ULL << dn->dn_indblkshift);
204 if (db == NULL || db == dn->dn_dbuf) {
208 ASSERT(DB_DNODE(db) == dn);
209 ASSERT(db->db_level == level);
210 ASSERT(db->db.db_size == space);
211 ASSERT(db->db_blkid == blkid);
213 parent = db->db_parent;
216 freeable = (bp && (freeable ||
217 dsl_dataset_block_freeable(ds, bp, bp->blk_birth)));
220 txh->txh_space_tooverwrite += space;
222 txh->txh_space_towrite += space;
224 txh->txh_space_tounref += bp_get_dsize(os->os_spa, bp);
226 dmu_tx_count_twig(txh, dn, parent, level + 1,
227 blkid >> epbs, freeable, history);
232 dmu_tx_count_write(dmu_tx_hold_t *txh, uint64_t off, uint64_t len)
234 dnode_t *dn = txh->txh_dnode;
235 uint64_t start, end, i;
236 int min_bs, max_bs, min_ibs, max_ibs, epbs, bits;
243 min_bs = SPA_MINBLOCKSHIFT;
244 max_bs = SPA_MAXBLOCKSHIFT;
245 min_ibs = DN_MIN_INDBLKSHIFT;
246 max_ibs = DN_MAX_INDBLKSHIFT;
249 uint64_t history[DN_MAX_LEVELS];
250 int nlvls = dn->dn_nlevels;
254 * For i/o error checking, read the first and last level-0
255 * blocks (if they are not aligned), and all the level-1 blocks.
257 if (dn->dn_maxblkid == 0) {
258 delta = dn->dn_datablksz;
259 start = (off < dn->dn_datablksz) ? 0 : 1;
260 end = (off+len <= dn->dn_datablksz) ? 0 : 1;
261 if (start == 0 && (off > 0 || len < dn->dn_datablksz)) {
262 err = dmu_tx_check_ioerr(NULL, dn, 0, 0);
268 zio_t *zio = zio_root(dn->dn_objset->os_spa,
269 NULL, NULL, ZIO_FLAG_CANFAIL);
271 /* first level-0 block */
272 start = off >> dn->dn_datablkshift;
273 if (P2PHASE(off, dn->dn_datablksz) ||
274 len < dn->dn_datablksz) {
275 err = dmu_tx_check_ioerr(zio, dn, 0, start);
280 /* last level-0 block */
281 end = (off+len-1) >> dn->dn_datablkshift;
282 if (end != start && end <= dn->dn_maxblkid &&
283 P2PHASE(off+len, dn->dn_datablksz)) {
284 err = dmu_tx_check_ioerr(zio, dn, 0, end);
291 int shft = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
292 for (i = (start>>shft)+1; i < end>>shft; i++) {
293 err = dmu_tx_check_ioerr(zio, dn, 1, i);
302 delta = P2NPHASE(off, dn->dn_datablksz);
305 if (dn->dn_maxblkid > 0) {
307 * The blocksize can't change,
308 * so we can make a more precise estimate.
310 ASSERT(dn->dn_datablkshift != 0);
311 min_bs = max_bs = dn->dn_datablkshift;
312 min_ibs = max_ibs = dn->dn_indblkshift;
313 } else if (dn->dn_indblkshift > max_ibs) {
315 * This ensures that if we reduce DN_MAX_INDBLKSHIFT,
316 * the code will still work correctly on older pools.
318 min_ibs = max_ibs = dn->dn_indblkshift;
322 * If this write is not off the end of the file
323 * we need to account for overwrites/unref.
325 if (start <= dn->dn_maxblkid) {
326 for (l = 0; l < DN_MAX_LEVELS; l++)
329 while (start <= dn->dn_maxblkid) {
332 rw_enter(&dn->dn_struct_rwlock, RW_READER);
333 err = dbuf_hold_impl(dn, 0, start, FALSE, FTAG, &db);
334 rw_exit(&dn->dn_struct_rwlock);
337 txh->txh_tx->tx_err = err;
341 dmu_tx_count_twig(txh, dn, db, 0, start, B_FALSE,
346 * Account for new indirects appearing
347 * before this IO gets assigned into a txg.
350 epbs = min_ibs - SPA_BLKPTRSHIFT;
351 for (bits -= epbs * (nlvls - 1);
352 bits >= 0; bits -= epbs)
353 txh->txh_fudge += 1ULL << max_ibs;
359 delta = dn->dn_datablksz;
364 * 'end' is the last thing we will access, not one past.
365 * This way we won't overflow when accessing the last byte.
367 start = P2ALIGN(off, 1ULL << max_bs);
368 end = P2ROUNDUP(off + len, 1ULL << max_bs) - 1;
369 txh->txh_space_towrite += end - start + 1;
374 epbs = min_ibs - SPA_BLKPTRSHIFT;
377 * The object contains at most 2^(64 - min_bs) blocks,
378 * and each indirect level maps 2^epbs.
380 for (bits = 64 - min_bs; bits >= 0; bits -= epbs) {
383 ASSERT3U(end, >=, start);
384 txh->txh_space_towrite += (end - start + 1) << max_ibs;
387 * We also need a new blkid=0 indirect block
388 * to reference any existing file data.
390 txh->txh_space_towrite += 1ULL << max_ibs;
395 if (txh->txh_space_towrite + txh->txh_space_tooverwrite >
400 txh->txh_tx->tx_err = err;
404 dmu_tx_count_dnode(dmu_tx_hold_t *txh)
406 dnode_t *dn = txh->txh_dnode;
407 dnode_t *mdn = DMU_META_DNODE(txh->txh_tx->tx_objset);
408 uint64_t space = mdn->dn_datablksz +
409 ((mdn->dn_nlevels-1) << mdn->dn_indblkshift);
411 if (dn && dn->dn_dbuf->db_blkptr &&
412 dsl_dataset_block_freeable(dn->dn_objset->os_dsl_dataset,
413 dn->dn_dbuf->db_blkptr, dn->dn_dbuf->db_blkptr->blk_birth)) {
414 txh->txh_space_tooverwrite += space;
415 txh->txh_space_tounref += space;
417 txh->txh_space_towrite += space;
418 if (dn && dn->dn_dbuf->db_blkptr)
419 txh->txh_space_tounref += space;
424 dmu_tx_hold_write(dmu_tx_t *tx, uint64_t object, uint64_t off, int len)
428 ASSERT(tx->tx_txg == 0);
429 ASSERT(len < DMU_MAX_ACCESS);
430 ASSERT(len == 0 || UINT64_MAX - off >= len - 1);
432 txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
433 object, THT_WRITE, off, len);
437 dmu_tx_count_write(txh, off, len);
438 dmu_tx_count_dnode(txh);
442 dmu_tx_count_free(dmu_tx_hold_t *txh, uint64_t off, uint64_t len)
444 uint64_t blkid, nblks, lastblk;
445 uint64_t space = 0, unref = 0, skipped = 0;
446 dnode_t *dn = txh->txh_dnode;
447 dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset;
448 spa_t *spa = txh->txh_tx->tx_pool->dp_spa;
451 if (dn->dn_nlevels == 0)
455 * The struct_rwlock protects us against dn_nlevels
456 * changing, in case (against all odds) we manage to dirty &
457 * sync out the changes after we check for being dirty.
458 * Also, dbuf_hold_impl() wants us to have the struct_rwlock.
460 rw_enter(&dn->dn_struct_rwlock, RW_READER);
461 epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
462 if (dn->dn_maxblkid == 0) {
463 if (off == 0 && len >= dn->dn_datablksz) {
467 rw_exit(&dn->dn_struct_rwlock);
471 blkid = off >> dn->dn_datablkshift;
472 nblks = (len + dn->dn_datablksz - 1) >> dn->dn_datablkshift;
474 if (blkid >= dn->dn_maxblkid) {
475 rw_exit(&dn->dn_struct_rwlock);
478 if (blkid + nblks > dn->dn_maxblkid)
479 nblks = dn->dn_maxblkid - blkid;
482 if (dn->dn_nlevels == 1) {
484 for (i = 0; i < nblks; i++) {
485 blkptr_t *bp = dn->dn_phys->dn_blkptr;
486 ASSERT3U(blkid + i, <, dn->dn_nblkptr);
488 if (dsl_dataset_block_freeable(ds, bp, bp->blk_birth)) {
489 dprintf_bp(bp, "can free old%s", "");
490 space += bp_get_dsize(spa, bp);
492 unref += BP_GET_ASIZE(bp);
498 * Add in memory requirements of higher-level indirects.
499 * This assumes a worst-possible scenario for dn_nlevels.
502 uint64_t blkcnt = 1 + ((nblks >> epbs) >> epbs);
503 int level = (dn->dn_nlevels > 1) ? 2 : 1;
505 while (level++ < DN_MAX_LEVELS) {
506 txh->txh_memory_tohold += blkcnt << dn->dn_indblkshift;
507 blkcnt = 1 + (blkcnt >> epbs);
509 ASSERT(blkcnt <= dn->dn_nblkptr);
512 lastblk = blkid + nblks - 1;
514 dmu_buf_impl_t *dbuf;
515 uint64_t ibyte, new_blkid;
517 int err, i, blkoff, tochk;
520 ibyte = blkid << dn->dn_datablkshift;
521 err = dnode_next_offset(dn,
522 DNODE_FIND_HAVELOCK, &ibyte, 2, 1, 0);
523 new_blkid = ibyte >> dn->dn_datablkshift;
525 skipped += (lastblk >> epbs) - (blkid >> epbs) + 1;
529 txh->txh_tx->tx_err = err;
532 if (new_blkid > lastblk) {
533 skipped += (lastblk >> epbs) - (blkid >> epbs) + 1;
537 if (new_blkid > blkid) {
538 ASSERT((new_blkid >> epbs) > (blkid >> epbs));
539 skipped += (new_blkid >> epbs) - (blkid >> epbs) - 1;
540 nblks -= new_blkid - blkid;
543 blkoff = P2PHASE(blkid, epb);
544 tochk = MIN(epb - blkoff, nblks);
546 err = dbuf_hold_impl(dn, 1, blkid >> epbs, FALSE, FTAG, &dbuf);
548 txh->txh_tx->tx_err = err;
552 txh->txh_memory_tohold += dbuf->db.db_size;
555 * We don't check memory_tohold against DMU_MAX_ACCESS because
556 * memory_tohold is an over-estimation (especially the >L1
557 * indirect blocks), so it could fail. Callers should have
558 * already verified that they will not be holding too much
562 err = dbuf_read(dbuf, NULL, DB_RF_HAVESTRUCT | DB_RF_CANFAIL);
564 txh->txh_tx->tx_err = err;
565 dbuf_rele(dbuf, FTAG);
569 bp = dbuf->db.db_data;
572 for (i = 0; i < tochk; i++) {
573 if (dsl_dataset_block_freeable(ds, &bp[i],
575 dprintf_bp(&bp[i], "can free old%s", "");
576 space += bp_get_dsize(spa, &bp[i]);
578 unref += BP_GET_ASIZE(bp);
580 dbuf_rele(dbuf, FTAG);
585 rw_exit(&dn->dn_struct_rwlock);
587 /* account for new level 1 indirect blocks that might show up */
589 txh->txh_fudge += skipped << dn->dn_indblkshift;
590 skipped = MIN(skipped, DMU_MAX_DELETEBLKCNT >> epbs);
591 txh->txh_memory_tohold += skipped << dn->dn_indblkshift;
593 txh->txh_space_tofree += space;
594 txh->txh_space_tounref += unref;
598 dmu_tx_hold_free(dmu_tx_t *tx, uint64_t object, uint64_t off, uint64_t len)
602 uint64_t start, end, i;
606 ASSERT(tx->tx_txg == 0);
608 txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
609 object, THT_FREE, off, len);
616 dmu_tx_count_write(txh, off, 1);
618 if (len != DMU_OBJECT_END)
619 dmu_tx_count_write(txh, off+len, 1);
621 dmu_tx_count_dnode(txh);
623 if (off >= (dn->dn_maxblkid+1) * dn->dn_datablksz)
625 if (len == DMU_OBJECT_END)
626 len = (dn->dn_maxblkid+1) * dn->dn_datablksz - off;
629 * For i/o error checking, read the first and last level-0
630 * blocks, and all the level-1 blocks. The above count_write's
631 * have already taken care of the level-0 blocks.
633 if (dn->dn_nlevels > 1) {
634 shift = dn->dn_datablkshift + dn->dn_indblkshift -
636 start = off >> shift;
637 end = dn->dn_datablkshift ? ((off+len) >> shift) : 0;
639 zio = zio_root(tx->tx_pool->dp_spa,
640 NULL, NULL, ZIO_FLAG_CANFAIL);
641 for (i = start; i <= end; i++) {
642 uint64_t ibyte = i << shift;
643 err = dnode_next_offset(dn, 0, &ibyte, 2, 1, 0);
652 err = dmu_tx_check_ioerr(zio, dn, 1, i);
665 dmu_tx_count_free(txh, off, len);
669 dmu_tx_hold_zap(dmu_tx_t *tx, uint64_t object, int add, const char *name)
676 ASSERT(tx->tx_txg == 0);
678 txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
679 object, THT_ZAP, add, (uintptr_t)name);
684 dmu_tx_count_dnode(txh);
688 * We will be able to fit a new object's entries into one leaf
689 * block. So there will be at most 2 blocks total,
690 * including the header block.
692 dmu_tx_count_write(txh, 0, 2 << fzap_default_block_shift);
696 ASSERT3P(dmu_ot[dn->dn_type].ot_byteswap, ==, zap_byteswap);
698 if (dn->dn_maxblkid == 0 && !add) {
702 * If there is only one block (i.e. this is a micro-zap)
703 * and we are not adding anything, the accounting is simple.
705 err = dmu_tx_check_ioerr(NULL, dn, 0, 0);
712 * Use max block size here, since we don't know how much
713 * the size will change between now and the dbuf dirty call.
715 bp = &dn->dn_phys->dn_blkptr[0];
716 if (dsl_dataset_block_freeable(dn->dn_objset->os_dsl_dataset,
718 txh->txh_space_tooverwrite += SPA_MAXBLOCKSIZE;
720 txh->txh_space_towrite += SPA_MAXBLOCKSIZE;
722 txh->txh_space_tounref += SPA_MAXBLOCKSIZE;
726 if (dn->dn_maxblkid > 0 && name) {
728 * access the name in this fat-zap so that we'll check
729 * for i/o errors to the leaf blocks, etc.
731 err = zap_lookup(dn->dn_objset, dn->dn_object, name,
739 err = zap_count_write(dn->dn_objset, dn->dn_object, name, add,
740 &txh->txh_space_towrite, &txh->txh_space_tooverwrite);
743 * If the modified blocks are scattered to the four winds,
744 * we'll have to modify an indirect twig for each.
746 epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
747 for (nblocks = dn->dn_maxblkid >> epbs; nblocks != 0; nblocks >>= epbs)
748 if (dn->dn_objset->os_dsl_dataset->ds_phys->ds_prev_snap_obj)
749 txh->txh_space_towrite += 3 << dn->dn_indblkshift;
751 txh->txh_space_tooverwrite += 3 << dn->dn_indblkshift;
755 dmu_tx_hold_bonus(dmu_tx_t *tx, uint64_t object)
759 ASSERT(tx->tx_txg == 0);
761 txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
762 object, THT_BONUS, 0, 0);
764 dmu_tx_count_dnode(txh);
768 dmu_tx_hold_space(dmu_tx_t *tx, uint64_t space)
771 ASSERT(tx->tx_txg == 0);
773 txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
774 DMU_NEW_OBJECT, THT_SPACE, space, 0);
776 txh->txh_space_towrite += space;
780 dmu_tx_holds(dmu_tx_t *tx, uint64_t object)
786 * By asserting that the tx is assigned, we're counting the
787 * number of dn_tx_holds, which is the same as the number of
788 * dn_holds. Otherwise, we'd be counting dn_holds, but
789 * dn_tx_holds could be 0.
791 ASSERT(tx->tx_txg != 0);
793 /* if (tx->tx_anyobj == TRUE) */
796 for (txh = list_head(&tx->tx_holds); txh;
797 txh = list_next(&tx->tx_holds, txh)) {
798 if (txh->txh_dnode && txh->txh_dnode->dn_object == object)
807 dmu_tx_dirty_buf(dmu_tx_t *tx, dmu_buf_impl_t *db)
810 int match_object = FALSE, match_offset = FALSE;
816 ASSERT(tx->tx_txg != 0);
817 ASSERT(tx->tx_objset == NULL || dn->dn_objset == tx->tx_objset);
818 ASSERT3U(dn->dn_object, ==, db->db.db_object);
825 /* XXX No checking on the meta dnode for now */
826 if (db->db.db_object == DMU_META_DNODE_OBJECT) {
831 for (txh = list_head(&tx->tx_holds); txh;
832 txh = list_next(&tx->tx_holds, txh)) {
833 ASSERT3U(dn->dn_assigned_txg, ==, tx->tx_txg);
834 if (txh->txh_dnode == dn && txh->txh_type != THT_NEWOBJECT)
836 if (txh->txh_dnode == NULL || txh->txh_dnode == dn) {
837 int datablkshift = dn->dn_datablkshift ?
838 dn->dn_datablkshift : SPA_MAXBLOCKSHIFT;
839 int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
840 int shift = datablkshift + epbs * db->db_level;
841 uint64_t beginblk = shift >= 64 ? 0 :
842 (txh->txh_arg1 >> shift);
843 uint64_t endblk = shift >= 64 ? 0 :
844 ((txh->txh_arg1 + txh->txh_arg2 - 1) >> shift);
845 uint64_t blkid = db->db_blkid;
847 /* XXX txh_arg2 better not be zero... */
849 dprintf("found txh type %x beginblk=%llx endblk=%llx\n",
850 txh->txh_type, beginblk, endblk);
852 switch (txh->txh_type) {
854 if (blkid >= beginblk && blkid <= endblk)
857 * We will let this hold work for the bonus
858 * or spill buffer so that we don't need to
859 * hold it when creating a new object.
861 if (blkid == DMU_BONUS_BLKID ||
862 blkid == DMU_SPILL_BLKID)
865 * They might have to increase nlevels,
866 * thus dirtying the new TLIBs. Or the
867 * might have to change the block size,
868 * thus dirying the new lvl=0 blk=0.
875 * We will dirty all the level 1 blocks in
876 * the free range and perhaps the first and
877 * last level 0 block.
879 if (blkid >= beginblk && (blkid <= endblk ||
880 txh->txh_arg2 == DMU_OBJECT_END))
884 if (blkid == DMU_SPILL_BLKID)
888 if (blkid == DMU_BONUS_BLKID)
898 ASSERT(!"bad txh_type");
901 if (match_object && match_offset) {
907 panic("dirtying dbuf obj=%llx lvl=%u blkid=%llx but not tx_held\n",
908 (u_longlong_t)db->db.db_object, db->db_level,
909 (u_longlong_t)db->db_blkid);
914 dmu_tx_try_assign(dmu_tx_t *tx, uint64_t txg_how)
917 spa_t *spa = tx->tx_pool->dp_spa;
918 uint64_t memory, asize, fsize, usize;
919 uint64_t towrite, tofree, tooverwrite, tounref, tohold, fudge;
921 ASSERT3U(tx->tx_txg, ==, 0);
924 DMU_TX_STAT_BUMP(dmu_tx_error);
928 if (spa_suspended(spa)) {
929 DMU_TX_STAT_BUMP(dmu_tx_suspended);
932 * If the user has indicated a blocking failure mode
933 * then return ERESTART which will block in dmu_tx_wait().
934 * Otherwise, return EIO so that an error can get
935 * propagated back to the VOP calls.
937 * Note that we always honor the txg_how flag regardless
938 * of the failuremode setting.
940 if (spa_get_failmode(spa) == ZIO_FAILURE_MODE_CONTINUE &&
947 tx->tx_txg = txg_hold_open(tx->tx_pool, &tx->tx_txgh);
948 tx->tx_needassign_txh = NULL;
951 * NB: No error returns are allowed after txg_hold_open, but
952 * before processing the dnode holds, due to the
953 * dmu_tx_unassign() logic.
956 towrite = tofree = tooverwrite = tounref = tohold = fudge = 0;
957 for (txh = list_head(&tx->tx_holds); txh;
958 txh = list_next(&tx->tx_holds, txh)) {
959 dnode_t *dn = txh->txh_dnode;
961 mutex_enter(&dn->dn_mtx);
962 if (dn->dn_assigned_txg == tx->tx_txg - 1) {
963 mutex_exit(&dn->dn_mtx);
964 tx->tx_needassign_txh = txh;
965 DMU_TX_STAT_BUMP(dmu_tx_group);
968 if (dn->dn_assigned_txg == 0)
969 dn->dn_assigned_txg = tx->tx_txg;
970 ASSERT3U(dn->dn_assigned_txg, ==, tx->tx_txg);
971 (void) refcount_add(&dn->dn_tx_holds, tx);
972 mutex_exit(&dn->dn_mtx);
974 towrite += txh->txh_space_towrite;
975 tofree += txh->txh_space_tofree;
976 tooverwrite += txh->txh_space_tooverwrite;
977 tounref += txh->txh_space_tounref;
978 tohold += txh->txh_memory_tohold;
979 fudge += txh->txh_fudge;
983 * NB: This check must be after we've held the dnodes, so that
984 * the dmu_tx_unassign() logic will work properly
986 if (txg_how >= TXG_INITIAL && txg_how != tx->tx_txg) {
987 DMU_TX_STAT_BUMP(dmu_tx_how);
992 * If a snapshot has been taken since we made our estimates,
993 * assume that we won't be able to free or overwrite anything.
996 dsl_dataset_prev_snap_txg(tx->tx_objset->os_dsl_dataset) >
997 tx->tx_lastsnap_txg) {
998 towrite += tooverwrite;
999 tooverwrite = tofree = 0;
1002 /* needed allocation: worst-case estimate of write space */
1003 asize = spa_get_asize(tx->tx_pool->dp_spa, towrite + tooverwrite);
1004 /* freed space estimate: worst-case overwrite + free estimate */
1005 fsize = spa_get_asize(tx->tx_pool->dp_spa, tooverwrite) + tofree;
1006 /* convert unrefd space to worst-case estimate */
1007 usize = spa_get_asize(tx->tx_pool->dp_spa, tounref);
1008 /* calculate memory footprint estimate */
1009 memory = towrite + tooverwrite + tohold;
1013 * Add in 'tohold' to account for our dirty holds on this memory
1014 * XXX - the "fudge" factor is to account for skipped blocks that
1015 * we missed because dnode_next_offset() misses in-core-only blocks.
1017 tx->tx_space_towrite = asize +
1018 spa_get_asize(tx->tx_pool->dp_spa, tohold + fudge);
1019 tx->tx_space_tofree = tofree;
1020 tx->tx_space_tooverwrite = tooverwrite;
1021 tx->tx_space_tounref = tounref;
1024 if (tx->tx_dir && asize != 0) {
1025 int err = dsl_dir_tempreserve_space(tx->tx_dir, memory,
1026 asize, fsize, usize, &tx->tx_tempreserve_cookie, tx);
1031 DMU_TX_STAT_BUMP(dmu_tx_assigned);
1037 dmu_tx_unassign(dmu_tx_t *tx)
1041 if (tx->tx_txg == 0)
1044 txg_rele_to_quiesce(&tx->tx_txgh);
1046 for (txh = list_head(&tx->tx_holds); txh != tx->tx_needassign_txh;
1047 txh = list_next(&tx->tx_holds, txh)) {
1048 dnode_t *dn = txh->txh_dnode;
1052 mutex_enter(&dn->dn_mtx);
1053 ASSERT3U(dn->dn_assigned_txg, ==, tx->tx_txg);
1055 if (refcount_remove(&dn->dn_tx_holds, tx) == 0) {
1056 dn->dn_assigned_txg = 0;
1057 cv_broadcast(&dn->dn_notxholds);
1059 mutex_exit(&dn->dn_mtx);
1062 txg_rele_to_sync(&tx->tx_txgh);
1064 tx->tx_lasttried_txg = tx->tx_txg;
1069 * Assign tx to a transaction group. txg_how can be one of:
1071 * (1) TXG_WAIT. If the current open txg is full, waits until there's
1072 * a new one. This should be used when you're not holding locks.
1073 * If will only fail if we're truly out of space (or over quota).
1075 * (2) TXG_NOWAIT. If we can't assign into the current open txg without
1076 * blocking, returns immediately with ERESTART. This should be used
1077 * whenever you're holding locks. On an ERESTART error, the caller
1078 * should drop locks, do a dmu_tx_wait(tx), and try again.
1080 * (3) A specific txg. Use this if you need to ensure that multiple
1081 * transactions all sync in the same txg. Like TXG_NOWAIT, it
1082 * returns ERESTART if it can't assign you into the requested txg.
1085 dmu_tx_assign(dmu_tx_t *tx, uint64_t txg_how)
1089 ASSERT(tx->tx_txg == 0);
1090 ASSERT(txg_how != 0);
1091 ASSERT(!dsl_pool_sync_context(tx->tx_pool));
1093 while ((err = dmu_tx_try_assign(tx, txg_how)) != 0) {
1094 dmu_tx_unassign(tx);
1096 if (err != ERESTART || txg_how != TXG_WAIT)
1102 txg_rele_to_quiesce(&tx->tx_txgh);
1108 dmu_tx_wait(dmu_tx_t *tx)
1110 spa_t *spa = tx->tx_pool->dp_spa;
1112 ASSERT(tx->tx_txg == 0);
1115 * It's possible that the pool has become active after this thread
1116 * has tried to obtain a tx. If that's the case then his
1117 * tx_lasttried_txg would not have been assigned.
1119 if (spa_suspended(spa) || tx->tx_lasttried_txg == 0) {
1120 txg_wait_synced(tx->tx_pool, spa_last_synced_txg(spa) + 1);
1121 } else if (tx->tx_needassign_txh) {
1122 dnode_t *dn = tx->tx_needassign_txh->txh_dnode;
1124 mutex_enter(&dn->dn_mtx);
1125 while (dn->dn_assigned_txg == tx->tx_lasttried_txg - 1)
1126 cv_wait(&dn->dn_notxholds, &dn->dn_mtx);
1127 mutex_exit(&dn->dn_mtx);
1128 tx->tx_needassign_txh = NULL;
1130 txg_wait_open(tx->tx_pool, tx->tx_lasttried_txg + 1);
1135 dmu_tx_willuse_space(dmu_tx_t *tx, int64_t delta)
1138 if (tx->tx_dir == NULL || delta == 0)
1142 ASSERT3U(refcount_count(&tx->tx_space_written) + delta, <=,
1143 tx->tx_space_towrite);
1144 (void) refcount_add_many(&tx->tx_space_written, delta, NULL);
1146 (void) refcount_add_many(&tx->tx_space_freed, -delta, NULL);
1152 dmu_tx_commit(dmu_tx_t *tx)
1156 ASSERT(tx->tx_txg != 0);
1158 while ((txh = list_head(&tx->tx_holds))) {
1159 dnode_t *dn = txh->txh_dnode;
1161 list_remove(&tx->tx_holds, txh);
1162 kmem_free(txh, sizeof (dmu_tx_hold_t));
1165 mutex_enter(&dn->dn_mtx);
1166 ASSERT3U(dn->dn_assigned_txg, ==, tx->tx_txg);
1168 if (refcount_remove(&dn->dn_tx_holds, tx) == 0) {
1169 dn->dn_assigned_txg = 0;
1170 cv_broadcast(&dn->dn_notxholds);
1172 mutex_exit(&dn->dn_mtx);
1176 if (tx->tx_tempreserve_cookie)
1177 dsl_dir_tempreserve_clear(tx->tx_tempreserve_cookie, tx);
1179 if (!list_is_empty(&tx->tx_callbacks))
1180 txg_register_callbacks(&tx->tx_txgh, &tx->tx_callbacks);
1182 if (tx->tx_anyobj == FALSE)
1183 txg_rele_to_sync(&tx->tx_txgh);
1185 list_destroy(&tx->tx_callbacks);
1186 list_destroy(&tx->tx_holds);
1188 dprintf("towrite=%llu written=%llu tofree=%llu freed=%llu\n",
1189 tx->tx_space_towrite, refcount_count(&tx->tx_space_written),
1190 tx->tx_space_tofree, refcount_count(&tx->tx_space_freed));
1191 refcount_destroy_many(&tx->tx_space_written,
1192 refcount_count(&tx->tx_space_written));
1193 refcount_destroy_many(&tx->tx_space_freed,
1194 refcount_count(&tx->tx_space_freed));
1196 kmem_free(tx, sizeof (dmu_tx_t));
1200 dmu_tx_abort(dmu_tx_t *tx)
1204 ASSERT(tx->tx_txg == 0);
1206 while ((txh = list_head(&tx->tx_holds))) {
1207 dnode_t *dn = txh->txh_dnode;
1209 list_remove(&tx->tx_holds, txh);
1210 kmem_free(txh, sizeof (dmu_tx_hold_t));
1216 * Call any registered callbacks with an error code.
1218 if (!list_is_empty(&tx->tx_callbacks))
1219 dmu_tx_do_callbacks(&tx->tx_callbacks, ECANCELED);
1221 list_destroy(&tx->tx_callbacks);
1222 list_destroy(&tx->tx_holds);
1224 refcount_destroy_many(&tx->tx_space_written,
1225 refcount_count(&tx->tx_space_written));
1226 refcount_destroy_many(&tx->tx_space_freed,
1227 refcount_count(&tx->tx_space_freed));
1229 kmem_free(tx, sizeof (dmu_tx_t));
1233 dmu_tx_get_txg(dmu_tx_t *tx)
1235 ASSERT(tx->tx_txg != 0);
1236 return (tx->tx_txg);
1240 dmu_tx_callback_register(dmu_tx_t *tx, dmu_tx_callback_func_t *func, void *data)
1242 dmu_tx_callback_t *dcb;
1244 dcb = kmem_alloc(sizeof (dmu_tx_callback_t), KM_PUSHPAGE);
1246 dcb->dcb_func = func;
1247 dcb->dcb_data = data;
1249 list_insert_tail(&tx->tx_callbacks, dcb);
1253 * Call all the commit callbacks on a list, with a given error code.
1256 dmu_tx_do_callbacks(list_t *cb_list, int error)
1258 dmu_tx_callback_t *dcb;
1260 while ((dcb = list_head(cb_list))) {
1261 list_remove(cb_list, dcb);
1262 dcb->dcb_func(dcb->dcb_data, error);
1263 kmem_free(dcb, sizeof (dmu_tx_callback_t));
1268 * Interface to hold a bunch of attributes.
1269 * used for creating new files.
1270 * attrsize is the total size of all attributes
1271 * to be added during object creation
1273 * For updating/adding a single attribute dmu_tx_hold_sa() should be used.
1277 * hold necessary attribute name for attribute registration.
1278 * should be a very rare case where this is needed. If it does
1279 * happen it would only happen on the first write to the file system.
1282 dmu_tx_sa_registration_hold(sa_os_t *sa, dmu_tx_t *tx)
1286 if (!sa->sa_need_attr_registration)
1289 for (i = 0; i != sa->sa_num_attrs; i++) {
1290 if (!sa->sa_attr_table[i].sa_registered) {
1291 if (sa->sa_reg_attr_obj)
1292 dmu_tx_hold_zap(tx, sa->sa_reg_attr_obj,
1293 B_TRUE, sa->sa_attr_table[i].sa_name);
1295 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT,
1296 B_TRUE, sa->sa_attr_table[i].sa_name);
1303 dmu_tx_hold_spill(dmu_tx_t *tx, uint64_t object)
1308 txh = dmu_tx_hold_object_impl(tx, tx->tx_objset, object,
1311 dn = txh->txh_dnode;
1316 /* If blkptr doesn't exist then add space to towrite */
1317 if (!(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR)) {
1318 txh->txh_space_towrite += SPA_MAXBLOCKSIZE;
1322 bp = &dn->dn_phys->dn_spill;
1323 if (dsl_dataset_block_freeable(dn->dn_objset->os_dsl_dataset,
1325 txh->txh_space_tooverwrite += SPA_MAXBLOCKSIZE;
1327 txh->txh_space_towrite += SPA_MAXBLOCKSIZE;
1328 if (!BP_IS_HOLE(bp))
1329 txh->txh_space_tounref += SPA_MAXBLOCKSIZE;
1334 dmu_tx_hold_sa_create(dmu_tx_t *tx, int attrsize)
1336 sa_os_t *sa = tx->tx_objset->os_sa;
1338 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1340 if (tx->tx_objset->os_sa->sa_master_obj == 0)
1343 if (tx->tx_objset->os_sa->sa_layout_attr_obj)
1344 dmu_tx_hold_zap(tx, sa->sa_layout_attr_obj, B_TRUE, NULL);
1346 dmu_tx_hold_zap(tx, sa->sa_master_obj, B_TRUE, SA_LAYOUTS);
1347 dmu_tx_hold_zap(tx, sa->sa_master_obj, B_TRUE, SA_REGISTRY);
1348 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
1349 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
1352 dmu_tx_sa_registration_hold(sa, tx);
1354 if (attrsize <= DN_MAX_BONUSLEN && !sa->sa_force_spill)
1357 (void) dmu_tx_hold_object_impl(tx, tx->tx_objset, DMU_NEW_OBJECT,
1364 * dmu_tx_hold_sa(dmu_tx_t *tx, sa_handle_t *, attribute, add, size)
1366 * variable_size is the total size of all variable sized attributes
1367 * passed to this function. It is not the total size of all
1368 * variable size attributes that *may* exist on this object.
1371 dmu_tx_hold_sa(dmu_tx_t *tx, sa_handle_t *hdl, boolean_t may_grow)
1374 sa_os_t *sa = tx->tx_objset->os_sa;
1376 ASSERT(hdl != NULL);
1378 object = sa_handle_object(hdl);
1380 dmu_tx_hold_bonus(tx, object);
1382 if (tx->tx_objset->os_sa->sa_master_obj == 0)
1385 if (tx->tx_objset->os_sa->sa_reg_attr_obj == 0 ||
1386 tx->tx_objset->os_sa->sa_layout_attr_obj == 0) {
1387 dmu_tx_hold_zap(tx, sa->sa_master_obj, B_TRUE, SA_LAYOUTS);
1388 dmu_tx_hold_zap(tx, sa->sa_master_obj, B_TRUE, SA_REGISTRY);
1389 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
1390 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
1393 dmu_tx_sa_registration_hold(sa, tx);
1395 if (may_grow && tx->tx_objset->os_sa->sa_layout_attr_obj)
1396 dmu_tx_hold_zap(tx, sa->sa_layout_attr_obj, B_TRUE, NULL);
1398 if (sa->sa_force_spill || may_grow || hdl->sa_spill) {
1399 ASSERT(tx->tx_txg == 0);
1400 dmu_tx_hold_spill(tx, object);
1402 dmu_buf_impl_t *db = (dmu_buf_impl_t *)hdl->sa_bonus;
1407 if (dn->dn_have_spill) {
1408 ASSERT(tx->tx_txg == 0);
1409 dmu_tx_hold_spill(tx, object);
1418 dmu_tx_ksp = kstat_create("zfs", 0, "dmu_tx", "misc",
1419 KSTAT_TYPE_NAMED, sizeof (dmu_tx_stats) / sizeof (kstat_named_t),
1420 KSTAT_FLAG_VIRTUAL);
1422 if (dmu_tx_ksp != NULL) {
1423 dmu_tx_ksp->ks_data = &dmu_tx_stats;
1424 kstat_install(dmu_tx_ksp);
1431 if (dmu_tx_ksp != NULL) {
1432 kstat_delete(dmu_tx_ksp);
1437 #if defined(_KERNEL) && defined(HAVE_SPL)
1438 EXPORT_SYMBOL(dmu_tx_create);
1439 EXPORT_SYMBOL(dmu_tx_hold_write);
1440 EXPORT_SYMBOL(dmu_tx_hold_free);
1441 EXPORT_SYMBOL(dmu_tx_hold_zap);
1442 EXPORT_SYMBOL(dmu_tx_hold_bonus);
1443 EXPORT_SYMBOL(dmu_tx_abort);
1444 EXPORT_SYMBOL(dmu_tx_assign);
1445 EXPORT_SYMBOL(dmu_tx_wait);
1446 EXPORT_SYMBOL(dmu_tx_commit);
1447 EXPORT_SYMBOL(dmu_tx_get_txg);
1448 EXPORT_SYMBOL(dmu_tx_callback_register);
1449 EXPORT_SYMBOL(dmu_tx_do_callbacks);
1450 EXPORT_SYMBOL(dmu_tx_hold_spill);
1451 EXPORT_SYMBOL(dmu_tx_hold_sa_create);
1452 EXPORT_SYMBOL(dmu_tx_hold_sa);