dnode_sync.c revision 347a31bcb38b51837caee115d3979d3a981cc099
0N/A/*
1879N/A * CDDL HEADER START
0N/A *
0N/A * The contents of this file are subject to the terms of the
0N/A * Common Development and Distribution License (the "License").
0N/A * You may not use this file except in compliance with the License.
0N/A *
0N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
0N/A * or http://www.opensolaris.org/os/licensing.
0N/A * See the License for the specific language governing permissions
0N/A * and limitations under the License.
0N/A *
0N/A * When distributing Covered Code, include this CDDL HEADER in each
0N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
0N/A * If applicable, add the following below this CDDL HEADER, with the
0N/A * fields enclosed by brackets "[]" replaced with your own identifying
0N/A * information: Portions Copyright [yyyy] [name of copyright owner]
0N/A *
1472N/A * CDDL HEADER END
1472N/A */
1472N/A/*
0N/A * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
0N/A * Use is subject to license terms.
0N/A */
1879N/A
1879N/A#pragma ident "%Z%%M% %I% %E% SMI"
1879N/A
1879N/A#include <sys/zfs_context.h>
1879N/A#include <sys/dbuf.h>
1879N/A#include <sys/dnode.h>
1879N/A#include <sys/dmu.h>
1879N/A#include <sys/dmu_tx.h>
0N/A#include <sys/dmu_objset.h>
0N/A#include <sys/dsl_dataset.h>
0N/A#include <sys/spa.h>
0N/A#include <sys/zio.h>
0N/A
0N/Astatic void
0N/Adnode_increase_indirection(dnode_t *dn, dmu_tx_t *tx)
0N/A{
0N/A dmu_buf_impl_t *db;
0N/A int i;
0N/A uint64_t txg = tx->tx_txg;
0N/A
0N/A ASSERT(dn->dn_phys->dn_type != DMU_OT_NONE);
0N/A ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
0N/A /* this dnode can't be paged out because it's dirty */
0N/A
0N/A db = dbuf_hold_level(dn, dn->dn_phys->dn_nlevels, 0, FTAG);
0N/A ASSERT(db != NULL);
0N/A for (i = 0; i < dn->dn_phys->dn_nblkptr; i++)
0N/A if (!BP_IS_HOLE(&dn->dn_phys->dn_blkptr[i]))
0N/A break;
0N/A if (i != dn->dn_phys->dn_nblkptr) {
0N/A ASSERT(list_link_active(&db->db_dirty_node[txg&TXG_MASK]));
0N/A
0N/A (void) dbuf_read(db, NULL,
0N/A DB_RF_HAVESTRUCT | DB_RF_MUST_SUCCEED);
0N/A arc_release(db->db_buf, db);
0N/A /* copy dnode's block pointers to new indirect block */
0N/A ASSERT3U(sizeof (blkptr_t) * dn->dn_phys->dn_nblkptr, <=,
0N/A db->db.db_size);
0N/A bcopy(dn->dn_phys->dn_blkptr, db->db.db_data,
0N/A sizeof (blkptr_t) * dn->dn_phys->dn_nblkptr);
0N/A }
0N/A
0N/A dn->dn_phys->dn_nlevels += 1;
0N/A dprintf("os=%p obj=%llu, increase to %d\n",
0N/A dn->dn_objset, dn->dn_object,
0N/A dn->dn_phys->dn_nlevels);
0N/A
0N/A /* set dbuf's parent pointers to new indirect buf */
0N/A for (i = 0; i < dn->dn_phys->dn_nblkptr; i++) {
0N/A dmu_buf_impl_t *child =
0N/A dbuf_find(dn, dn->dn_phys->dn_nlevels-2, i);
0N/A if (child == NULL)
0N/A continue;
0N/A if (child->db_dnode == NULL) {
0N/A mutex_exit(&child->db_mtx);
0N/A continue;
0N/A }
0N/A
0N/A if (child->db_parent == NULL ||
0N/A child->db_parent == dn->dn_dbuf) {
0N/A dprintf_dbuf_bp(child, child->db_blkptr,
0N/A "changing db_blkptr to new indirect %s", "");
0N/A child->db_parent = db;
0N/A dbuf_add_ref(db, child);
0N/A if (db->db.db_data) {
0N/A child->db_blkptr =
0N/A (blkptr_t *)db->db.db_data + i;
0N/A } else {
0N/A child->db_blkptr = NULL;
0N/A }
0N/A dprintf_dbuf_bp(child, child->db_blkptr,
0N/A "changed db_blkptr to new indirect %s", "");
0N/A }
4132N/A ASSERT3P(child->db_parent, ==, db);
0N/A
0N/A mutex_exit(&child->db_mtx);
0N/A }
0N/A
0N/A bzero(dn->dn_phys->dn_blkptr,
0N/A sizeof (blkptr_t) * dn->dn_phys->dn_nblkptr);
0N/A
0N/A dbuf_rele(db, FTAG);
0N/A}
0N/A
0N/Astatic void
0N/Afree_blocks(dnode_t *dn, blkptr_t *bp, int num, dmu_tx_t *tx)
0N/A{
0N/A objset_impl_t *os = dn->dn_objset;
0N/A uint64_t bytesfreed = 0;
0N/A int i;
0N/A
0N/A dprintf("os=%p obj=%llx num=%d\n", os, dn->dn_object, num);
0N/A
0N/A for (i = 0; i < num; i++, bp++) {
0N/A if (BP_IS_HOLE(bp))
0N/A continue;
0N/A
64N/A bytesfreed += BP_GET_ASIZE(bp);
64N/A ASSERT3U(bytesfreed >> DEV_BSHIFT, <=, dn->dn_phys->dn_secphys);
64N/A dsl_dataset_block_kill(os->os_dsl_dataset, bp, tx);
64N/A }
0N/A dnode_diduse_space(dn, -bytesfreed);
0N/A}
0N/A
0N/A#ifdef ZFS_DEBUG
0N/Astatic void
0N/Afree_verify(dmu_buf_impl_t *db, uint64_t start, uint64_t end, dmu_tx_t *tx)
0N/A{
0N/A int off, num;
0N/A int i, err, epbs;
0N/A uint64_t txg = tx->tx_txg;
0N/A
0N/A epbs = db->db_dnode->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
0N/A off = start - (db->db_blkid * 1<<epbs);
0N/A num = end - start + 1;
64N/A
223N/A ASSERT3U(off, >=, 0);
64N/A ASSERT3U(num, >=, 0);
64N/A ASSERT3U(db->db_level, >, 0);
64N/A ASSERT3U(db->db.db_size, ==, 1<<db->db_dnode->dn_phys->dn_indblkshift);
64N/A ASSERT3U(off+num, <=, db->db.db_size >> SPA_BLKPTRSHIFT);
64N/A ASSERT(db->db_blkptr != NULL);
64N/A
64N/A for (i = off; i < off+num; i++) {
64N/A uint64_t *buf;
0N/A int j;
0N/A dmu_buf_impl_t *child;
0N/A
0N/A ASSERT(db->db_level == 1);
0N/A
0N/A rw_enter(&db->db_dnode->dn_struct_rwlock, RW_READER);
0N/A err = dbuf_hold_impl(db->db_dnode, db->db_level-1,
0N/A (db->db_blkid << epbs) + i, TRUE, FTAG, &child);
0N/A rw_exit(&db->db_dnode->dn_struct_rwlock);
0N/A if (err == ENOENT)
74N/A continue;
0N/A ASSERT(err == 0);
0N/A ASSERT(child->db_level == 0);
0N/A ASSERT(!list_link_active(&child->db_dirty_node[txg&TXG_MASK]));
0N/A
0N/A /* db_data_old better be zeroed */
0N/A if (child->db_d.db_data_old[txg & TXG_MASK]) {
0N/A buf = ((arc_buf_t *)child->db_d.db_data_old
0N/A [txg & TXG_MASK])->b_data;
0N/A for (j = 0; j < child->db.db_size >> 3; j++) {
0N/A if (buf[j] != 0) {
0N/A panic("freed data not zero: "
0N/A "child=%p i=%d off=%d num=%d\n",
0N/A child, i, off, num);
400N/A }
400N/A }
64N/A }
64N/A
64N/A /*
64N/A * db_data better be zeroed unless it's dirty in a
0N/A * future txg.
0N/A */
0N/A mutex_enter(&child->db_mtx);
0N/A buf = child->db.db_data;
0N/A if (buf != NULL && child->db_state != DB_FILL &&
4132N/A !list_link_active(&child->db_dirty_node
0N/A [(txg+1) & TXG_MASK]) &&
0N/A !list_link_active(&child->db_dirty_node
0N/A [(txg+2) & TXG_MASK])) {
64N/A for (j = 0; j < child->db.db_size >> 3; j++) {
64N/A if (buf[j] != 0) {
64N/A panic("freed data not zero: "
64N/A "child=%p i=%d off=%d num=%d\n",
64N/A child, i, off, num);
64N/A }
64N/A }
64N/A }
64N/A mutex_exit(&child->db_mtx);
64N/A
64N/A dbuf_rele(child, FTAG);
64N/A }
0N/A}
0N/A#endif
0N/A
0N/Astatic int
0N/Afree_children(dmu_buf_impl_t *db, uint64_t blkid, uint64_t nblks, int trunc,
0N/A dmu_tx_t *tx)
0N/A{
0N/A dnode_t *dn = db->db_dnode;
0N/A blkptr_t *bp;
0N/A dmu_buf_impl_t *subdb;
0N/A uint64_t start, end, dbstart, dbend, i;
0N/A int epbs, shift, err;
0N/A int txgoff = tx->tx_txg & TXG_MASK;
0N/A int all = TRUE;
0N/A
0N/A (void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED);
0N/A arc_release(db->db_buf, db);
0N/A bp = (blkptr_t *)db->db.db_data;
0N/A
0N/A epbs = db->db_dnode->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
2667N/A shift = (db->db_level - 1) * epbs;
0N/A dbstart = db->db_blkid << epbs;
0N/A start = blkid >> shift;
0N/A if (dbstart < start) {
0N/A bp += start - dbstart;
0N/A all = FALSE;
0N/A } else {
0N/A start = dbstart;
0N/A }
0N/A dbend = ((db->db_blkid + 1) << epbs) - 1;
0N/A end = (blkid + nblks - 1) >> shift;
0N/A if (dbend <= end)
0N/A end = dbend;
0N/A else if (all)
0N/A all = trunc;
0N/A ASSERT3U(start, <=, end);
0N/A
0N/A if (db->db_level == 1) {
0N/A FREE_VERIFY(db, start, end, tx);
0N/A free_blocks(dn, bp, end-start+1, tx);
0N/A ASSERT(all || list_link_active(&db->db_dirty_node[txgoff]));
0N/A return (all);
0N/A }
0N/A
0N/A for (i = start; i <= end; i++, bp++) {
0N/A if (BP_IS_HOLE(bp))
0N/A continue;
0N/A rw_enter(&dn->dn_struct_rwlock, RW_READER);
0N/A err = dbuf_hold_impl(dn, db->db_level-1, i, TRUE, FTAG, &subdb);
0N/A ASSERT3U(err, ==, 0);
0N/A rw_exit(&dn->dn_struct_rwlock);
0N/A
0N/A if (free_children(subdb, blkid, nblks, trunc, tx)) {
0N/A ASSERT3P(subdb->db_blkptr, ==, bp);
0N/A free_blocks(dn, bp, 1, tx);
127N/A } else {
127N/A all = FALSE;
0N/A }
0N/A dbuf_rele(subdb, FTAG);
0N/A }
0N/A#ifdef ZFS_DEBUG
0N/A bp -= (end-start)+1;
0N/A for (i = start; i <= end; i++, bp++) {
0N/A if (i == start && blkid != 0)
0N/A continue;
0N/A else if (i == end && !trunc)
0N/A continue;
0N/A ASSERT3U(bp->blk_birth, ==, 0);
0N/A }
0N/A#endif
0N/A ASSERT(all || list_link_active(&db->db_dirty_node[txgoff]));
0N/A return (all);
0N/A}
0N/A
0N/A/*
0N/A * free_range: Traverse the indicated range of the provided file
0N/A * and "free" all the blocks contained there.
0N/A */
0N/Astatic void
0N/Adnode_sync_free_range(dnode_t *dn, uint64_t blkid, uint64_t nblks, dmu_tx_t *tx)
0N/A{
0N/A blkptr_t *bp = dn->dn_phys->dn_blkptr;
0N/A dmu_buf_impl_t *db;
0N/A int trunc, start, end, shift, i, err;
0N/A int dnlevel = dn->dn_phys->dn_nlevels;
0N/A
0N/A if (blkid > dn->dn_phys->dn_maxblkid)
0N/A return;
0N/A
0N/A ASSERT(dn->dn_phys->dn_maxblkid < UINT64_MAX);
0N/A trunc = blkid + nblks > dn->dn_phys->dn_maxblkid;
0N/A if (trunc)
0N/A nblks = dn->dn_phys->dn_maxblkid - blkid + 1;
0N/A
0N/A /* There are no indirect blocks in the object */
0N/A if (dnlevel == 1) {
0N/A if (blkid >= dn->dn_phys->dn_nblkptr) {
0N/A /* this range was never made persistent */
0N/A return;
0N/A }
0N/A ASSERT3U(blkid + nblks, <=, dn->dn_phys->dn_nblkptr);
0N/A free_blocks(dn, bp + blkid, nblks, tx);
0N/A if (trunc) {
0N/A uint64_t off = (dn->dn_phys->dn_maxblkid + 1) *
0N/A (dn->dn_phys->dn_datablkszsec << SPA_MINBLOCKSHIFT);
0N/A dn->dn_phys->dn_maxblkid = (blkid ? blkid - 1 : 0);
0N/A ASSERT(off < dn->dn_phys->dn_maxblkid ||
0N/A dn->dn_phys->dn_maxblkid == 0 ||
0N/A dnode_next_offset(dn, FALSE, &off, 1, 1) == ESRCH);
0N/A }
0N/A return;
0N/A }
0N/A
0N/A shift = (dnlevel - 1) * (dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT);
0N/A start = blkid >> shift;
0N/A ASSERT(start < dn->dn_phys->dn_nblkptr);
0N/A end = (blkid + nblks - 1) >> shift;
0N/A bp += start;
0N/A for (i = start; i <= end; i++, bp++) {
0N/A if (BP_IS_HOLE(bp))
0N/A continue;
0N/A rw_enter(&dn->dn_struct_rwlock, RW_READER);
0N/A err = dbuf_hold_impl(dn, dnlevel-1, i, TRUE, FTAG, &db);
0N/A ASSERT3U(err, ==, 0);
0N/A rw_exit(&dn->dn_struct_rwlock);
0N/A
0N/A if (free_children(db, blkid, nblks, trunc, tx)) {
0N/A ASSERT3P(db->db_blkptr, ==, bp);
0N/A free_blocks(dn, bp, 1, tx);
0N/A }
0N/A dbuf_rele(db, FTAG);
0N/A }
0N/A if (trunc) {
0N/A uint64_t off = (dn->dn_phys->dn_maxblkid + 1) *
0N/A (dn->dn_phys->dn_datablkszsec << SPA_MINBLOCKSHIFT);
0N/A dn->dn_phys->dn_maxblkid = (blkid ? blkid - 1 : 0);
0N/A ASSERT(off < dn->dn_phys->dn_maxblkid ||
0N/A dn->dn_phys->dn_maxblkid == 0 ||
0N/A dnode_next_offset(dn, FALSE, &off, 1, 1) == ESRCH);
0N/A }
0N/A}
0N/A
0N/A/*
0N/A * Try to kick all the dnodes dbufs out of the cache...
0N/A */
0N/Avoid
127N/Adnode_evict_dbufs(dnode_t *dn)
0N/A{
0N/A int progress;
0N/A int pass = 0;
17N/A
0N/A do {
0N/A dmu_buf_impl_t *db, *db_next;
17N/A int evicting = FALSE;
17N/A
17N/A progress = FALSE;
17N/A mutex_enter(&dn->dn_dbufs_mtx);
17N/A for (db = list_head(&dn->dn_dbufs); db; db = db_next) {
0N/A /* dbuf_clear() may remove db from this list */
0N/A db_next = list_next(&dn->dn_dbufs, db);
0N/A
0N/A mutex_enter(&db->db_mtx);
0N/A if (db->db_state == DB_EVICTING) {
0N/A progress = TRUE;
0N/A evicting = TRUE;
0N/A mutex_exit(&db->db_mtx);
0N/A } else if (refcount_is_zero(&db->db_holds)) {
0N/A progress = TRUE;
0N/A ASSERT(!arc_released(db->db_buf));
0N/A dbuf_clear(db); /* exits db_mtx for us */
0N/A } else {
0N/A mutex_exit(&db->db_mtx);
0N/A }
0N/A
0N/A }
0N/A /*
0N/A * NB: we need to drop dn_dbufs_mtx between passes so
0N/A * that any DB_EVICTING dbufs can make progress.
0N/A * Ideally, we would have some cv we could wait on, but
0N/A * since we don't, just wait a bit to give the other
0N/A * thread a chance to run.
0N/A */
0N/A mutex_exit(&dn->dn_dbufs_mtx);
0N/A if (evicting)
0N/A delay(1);
0N/A pass++;
0N/A ASSERT(pass < 100); /* sanity check */
0N/A } while (progress);
0N/A
0N/A /*
0N/A * This function works fine even if it can't evict everything,
0N/A * but all of our callers need this assertion, so let's put it
0N/A * here (for now). Perhaps in the future there will be a try vs
0N/A * doall flag.
0N/A */
0N/A if (list_head(&dn->dn_dbufs) != NULL) {
0N/A panic("dangling dbufs (dn=%p, dbuf=%p)\n",
0N/A dn, list_head(&dn->dn_dbufs));
0N/A }
0N/A
0N/A rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
0N/A if (dn->dn_bonus && refcount_is_zero(&dn->dn_bonus->db_holds)) {
0N/A mutex_enter(&dn->dn_bonus->db_mtx);
0N/A dbuf_evict(dn->dn_bonus);
0N/A dn->dn_bonus = NULL;
127N/A }
0N/A rw_exit(&dn->dn_struct_rwlock);
0N/A
0N/A}
0N/A
0N/Astatic int
0N/Adnode_sync_free(dnode_t *dn, dmu_tx_t *tx)
0N/A{
0N/A dmu_buf_impl_t *db;
0N/A int txgoff = tx->tx_txg & TXG_MASK;
0N/A
0N/A ASSERT(dmu_tx_is_syncing(tx));
0N/A
0N/A /* Undirty all buffers */
0N/A while (db = list_head(&dn->dn_dirty_dbufs[txgoff])) {
0N/A mutex_enter(&db->db_mtx);
0N/A /* XXX - use dbuf_undirty()? */
0N/A list_remove(&dn->dn_dirty_dbufs[txgoff], db);
0N/A if (db->db_level == 0) {
0N/A ASSERT(db->db_blkid == DB_BONUS_BLKID ||
0N/A db->db_d.db_data_old[txgoff] == db->db_buf);
0N/A if (db->db_d.db_overridden_by[txgoff])
0N/A dbuf_unoverride(db, tx->tx_txg);
0N/A db->db_d.db_data_old[txgoff] = NULL;
0N/A }
0N/A db->db_dirtycnt -= 1;
0N/A mutex_exit(&db->db_mtx);
0N/A dbuf_rele(db, (void *)(uintptr_t)tx->tx_txg);
0N/A }
0N/A
0N/A dnode_evict_dbufs(dn);
0N/A ASSERT3P(list_head(&dn->dn_dbufs), ==, NULL);
0N/A
0N/A /*
0N/A * XXX - It would be nice to assert this, but we may still
0N/A * have residual holds from async evictions from the arc...
0N/A *
0N/A * ASSERT3U(refcount_count(&dn->dn_holds), ==, 1);
0N/A */
0N/A
0N/A /* Undirty next bits */
0N/A dn->dn_next_nlevels[txgoff] = 0;
0N/A dn->dn_next_indblkshift[txgoff] = 0;
0N/A dn->dn_next_blksz[txgoff] = 0;
0N/A
0N/A /* free up all the blocks in the file. */
0N/A dnode_sync_free_range(dn, 0, dn->dn_phys->dn_maxblkid+1, tx);
0N/A ASSERT3U(dn->dn_phys->dn_secphys, ==, 0);
0N/A
0N/A /* ASSERT(blkptrs are zero); */
0N/A ASSERT(dn->dn_phys->dn_type != DMU_OT_NONE);
0N/A ASSERT(dn->dn_type != DMU_OT_NONE);
0N/A
0N/A ASSERT(dn->dn_free_txg > 0);
0N/A if (dn->dn_allocated_txg != dn->dn_free_txg)
0N/A dbuf_will_dirty(dn->dn_dbuf, tx);
0N/A bzero(dn->dn_phys, sizeof (dnode_phys_t));
0N/A
0N/A mutex_enter(&dn->dn_mtx);
0N/A dn->dn_type = DMU_OT_NONE;
0N/A dn->dn_maxblkid = 0;
0N/A dn->dn_allocated_txg = 0;
0N/A mutex_exit(&dn->dn_mtx);
0N/A
0N/A ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
0N/A
0N/A dnode_rele(dn, (void *)(uintptr_t)tx->tx_txg);
0N/A /*
0N/A * Now that we've released our hold, the dnode may
0N/A * be evicted, so we musn't access it.
0N/A */
0N/A return (1);
0N/A}
0N/A
0N/A/*
0N/A * Write out the dnode's dirty buffers at the specified level.
0N/A * This may create more dirty buffers at the next level up.
0N/A *
0N/A * NOTE: The dnode is kept in memory by being dirty. Once the
0N/A * dirty bit is cleared, it may be evicted. Beware of this!
0N/A */
0N/Aint
0N/Adnode_sync(dnode_t *dn, int level, zio_t *zio, dmu_tx_t *tx)
0N/A{
0N/A free_range_t *rp;
0N/A int txgoff = tx->tx_txg & TXG_MASK;
0N/A dnode_phys_t *dnp = dn->dn_phys;
0N/A
0N/A ASSERT(dmu_tx_is_syncing(tx));
0N/A ASSERT(dnp->dn_type != DMU_OT_NONE || dn->dn_allocated_txg);
0N/A DNODE_VERIFY(dn);
0N/A
0N/A /*
0N/A * Make sure the dbuf for the dn_phys is released before we modify it.
0N/A */
0N/A if (dn->dn_dbuf)
0N/A arc_release(dn->dn_dbuf->db_buf, dn->dn_dbuf);
0N/A
0N/A mutex_enter(&dn->dn_mtx);
0N/A if (dn->dn_allocated_txg == tx->tx_txg) {
0N/A /* The dnode is newly allocated or reallocated */
0N/A if (dnp->dn_type == DMU_OT_NONE) {
0N/A /* this is a first alloc, not a realloc */
0N/A /* XXX shouldn't the phys already be zeroed? */
0N/A bzero(dnp, DNODE_CORE_SIZE);
0N/A dnp->dn_nlevels = 1;
0N/A }
0N/A
0N/A if (dn->dn_nblkptr > dnp->dn_nblkptr) {
0N/A /* zero the new blkptrs we are gaining */
0N/A bzero(dnp->dn_blkptr + dnp->dn_nblkptr,
0N/A sizeof (blkptr_t) *
0N/A (dn->dn_nblkptr - dnp->dn_nblkptr));
0N/A }
127N/A dnp->dn_type = dn->dn_type;
127N/A dnp->dn_bonustype = dn->dn_bonustype;
127N/A dnp->dn_bonuslen = dn->dn_bonuslen;
0N/A dnp->dn_nblkptr = dn->dn_nblkptr;
0N/A }
0N/A
0N/A ASSERT(level != 0 || dnp->dn_nlevels > 1 ||
0N/A BP_IS_HOLE(&dnp->dn_blkptr[0]) ||
0N/A BP_GET_LSIZE(&dnp->dn_blkptr[0]) ==
1879N/A dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
1879N/A
if (dn->dn_next_blksz[txgoff]) {
ASSERT(P2PHASE(dn->dn_next_blksz[txgoff],
SPA_MINBLOCKSIZE) == 0);
ASSERT(BP_IS_HOLE(&dnp->dn_blkptr[0]) ||
list_head(&dn->dn_dirty_dbufs[txgoff]) != NULL ||
dn->dn_next_blksz[txgoff] >> SPA_MINBLOCKSHIFT ==
dnp->dn_datablkszsec);
dnp->dn_datablkszsec =
dn->dn_next_blksz[txgoff] >> SPA_MINBLOCKSHIFT;
dn->dn_next_blksz[txgoff] = 0;
}
if (dn->dn_next_indblkshift[txgoff]) {
ASSERT(dnp->dn_nlevels == 1);
dnp->dn_indblkshift = dn->dn_next_indblkshift[txgoff];
dn->dn_next_indblkshift[txgoff] = 0;
}
/*
* Just take the live (open-context) values for checksum and compress.
* Strictly speaking it's a future leak, but nothing bad happens if we
* start using the new checksum or compress algorithm a little early.
*/
dnp->dn_checksum = dn->dn_checksum;
dnp->dn_compress = dn->dn_compress;
mutex_exit(&dn->dn_mtx);
/* process all the "freed" ranges in the file */
if (dn->dn_free_txg == 0 || dn->dn_free_txg > tx->tx_txg) {
for (rp = avl_last(&dn->dn_ranges[txgoff]); rp != NULL;
rp = AVL_PREV(&dn->dn_ranges[txgoff], rp))
dnode_sync_free_range(dn,
rp->fr_blkid, rp->fr_nblks, tx);
}
mutex_enter(&dn->dn_mtx);
for (rp = avl_first(&dn->dn_ranges[txgoff]); rp; ) {
free_range_t *last = rp;
rp = AVL_NEXT(&dn->dn_ranges[txgoff], rp);
avl_remove(&dn->dn_ranges[txgoff], last);
kmem_free(last, sizeof (free_range_t));
}
mutex_exit(&dn->dn_mtx);
if (dn->dn_free_txg > 0 && dn->dn_free_txg <= tx->tx_txg) {
ASSERT3U(level, ==, 0);
return (dnode_sync_free(dn, tx));
}
if (dn->dn_next_nlevels[txgoff]) {
int new_lvl = dn->dn_next_nlevels[txgoff];
rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
while (new_lvl > dnp->dn_nlevels)
dnode_increase_indirection(dn, tx);
rw_exit(&dn->dn_struct_rwlock);
dn->dn_next_nlevels[txgoff] = 0;
}
if (level == dnp->dn_nlevels) {
uint64_t off = (dn->dn_phys->dn_maxblkid + 1) *
(dn->dn_phys->dn_datablkszsec << SPA_MINBLOCKSHIFT);
/* we've already synced out all data and indirect blocks */
/* there are no more dirty dbufs under this dnode */
ASSERT3P(list_head(&dn->dn_dirty_dbufs[txgoff]), ==, NULL);
ASSERT(dn->dn_free_txg == 0 || dn->dn_free_txg >= tx->tx_txg);
/* XXX this is expensive. remove once 6343073 is closed. */
/* NB: the "off < maxblkid" is to catch overflow */
/*
* NB: if blocksize is changing, we could get confused,
* so only bother if there are multiple blocks and thus
* it can't be changing.
*/
if (!(off < dn->dn_phys->dn_maxblkid ||
dn->dn_phys->dn_maxblkid == 0 ||
dnode_next_offset(dn, FALSE, &off, 1, 1) == ESRCH))
panic("data after EOF: off=%llu\n", (u_longlong_t)off);
ASSERT(dnp->dn_nlevels > 1 ||
BP_IS_HOLE(&dnp->dn_blkptr[0]) ||
BP_GET_LSIZE(&dnp->dn_blkptr[0]) ==
dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
if (dn->dn_object != DMU_META_DNODE_OBJECT) {
dbuf_will_dirty(dn->dn_dbuf, tx);
dnode_rele(dn, (void *)(uintptr_t)tx->tx_txg);
}
/*
* Now that we've dropped the reference, the dnode may
* be evicted, so we musn't access it.
*/
return (1);
} else {
dmu_buf_impl_t *db, *db_next;
list_t *list = &dn->dn_dirty_dbufs[txgoff];
/*
* Iterate over the list, removing and sync'ing dbufs
* which are on the level we want, and leaving others.
*/
for (db = list_head(list); db; db = db_next) {
db_next = list_next(list, db);
if (db->db_level == level) {
list_remove(list, db);
dbuf_sync(db, zio, tx);
}
}
return (0);
}
}