Print this page
Possibility to physically reserve space without writing leaf blocks


1004         mutex_exit(&db->db_mtx);
1005 
1006         dnode_willuse_space(dn, size-osize, tx);
1007         DB_DNODE_EXIT(db);
1008 }
1009 
1010 void
1011 dbuf_release_bp(dmu_buf_impl_t *db)
1012 {
1013         objset_t *os = db->db_objset;
1014 
1015         ASSERT(dsl_pool_sync_context(dmu_objset_pool(os)));
1016         ASSERT(arc_released(os->os_phys_buf) ||
1017             list_link_active(&os->os_dsl_dataset->ds_synced_link));
1018         ASSERT(db->db_parent == NULL || arc_released(db->db_parent->db_buf));
1019 
1020         (void) arc_release(db->db_buf, db);
1021 }
1022 
1023 dbuf_dirty_record_t *
1024 dbuf_dirty(dmu_buf_impl_t *db, dmu_tx_t *tx)








1025 {
1026         dnode_t *dn;
1027         objset_t *os;
1028         dbuf_dirty_record_t **drp, *dr;
1029         int drop_struct_lock = FALSE;
1030         boolean_t do_free_accounting = B_FALSE;
1031         int txgoff = tx->tx_txg & TXG_MASK;
1032 
1033         ASSERT(tx->tx_txg != 0);
1034         ASSERT(!refcount_is_zero(&db->db_holds));
1035         DMU_TX_DIRTY_BUF(tx, db);
1036 
1037         DB_DNODE_ENTER(db);
1038         dn = DB_DNODE(db);
1039         /*
1040          * Shouldn't dirty a regular buffer in syncing context.  Private
1041          * objects may be dirtied in syncing context, but only if they
1042          * were already pre-dirtied in open context.
1043          */
1044         ASSERT(!dmu_tx_is_syncing(tx) ||


1135         dprintf_dbuf(db, "size=%llx\n", (u_longlong_t)db->db.db_size);
1136 
1137         if (db->db_blkid != DMU_BONUS_BLKID) {
1138                 /*
1139                  * Update the accounting.
1140                  * Note: we delay "free accounting" until after we drop
1141                  * the db_mtx.  This keeps us from grabbing other locks
1142                  * (and possibly deadlocking) in bp_get_dsize() while
1143                  * also holding the db_mtx.
1144                  */
1145                 dnode_willuse_space(dn, db->db.db_size, tx);
1146                 do_free_accounting = dbuf_block_freeable(db);
1147         }
1148 
1149         /*
1150          * If this buffer is dirty in an old transaction group we need
1151          * to make a copy of it so that the changes we make in this
1152          * transaction group won't leak out when we sync the older txg.
1153          */
1154         dr = kmem_zalloc(sizeof (dbuf_dirty_record_t), KM_SLEEP);

1155         if (db->db_level == 0) {
1156                 void *data_old = db->db_buf;
1157 
1158                 if (db->db_state != DB_NOFILL) {
1159                         if (db->db_blkid == DMU_BONUS_BLKID) {
1160                                 dbuf_fix_old_data(db, tx->tx_txg);
1161                                 data_old = db->db.db_data;
1162                         } else if (db->db.db_object != DMU_META_DNODE_OBJECT) {
1163                                 /*
1164                                  * Release the data buffer from the cache so
1165                                  * that we can modify it without impacting
1166                                  * possible other users of this cached data
1167                                  * block.  Note that indirect blocks and
1168                                  * private objects are not released until the
1169                                  * syncing state (since they are only modified
1170                                  * then).
1171                                  */
1172                                 arc_release(db->db_buf, db);
1173                                 dbuf_fix_old_data(db, tx->tx_txg);
1174                                 data_old = db->db_buf;


1248                 dnode_new_blkid(dn, db->db_blkid, tx, drop_struct_lock);
1249                 ASSERT(dn->dn_maxblkid >= db->db_blkid);
1250         }
1251 
1252         if (db->db_level+1 < dn->dn_nlevels) {
1253                 dmu_buf_impl_t *parent = db->db_parent;
1254                 dbuf_dirty_record_t *di;
1255                 int parent_held = FALSE;
1256 
1257                 if (db->db_parent == NULL || db->db_parent == dn->dn_dbuf) {
1258                         int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
1259 
1260                         parent = dbuf_hold_level(dn, db->db_level+1,
1261                             db->db_blkid >> epbs, FTAG);
1262                         ASSERT(parent != NULL);
1263                         parent_held = TRUE;
1264                 }
1265                 if (drop_struct_lock)
1266                         rw_exit(&dn->dn_struct_rwlock);
1267                 ASSERT3U(db->db_level+1, ==, parent->db_level);
1268                 di = dbuf_dirty(parent, tx);
1269                 if (parent_held)
1270                         dbuf_rele(parent, FTAG);
1271 
1272                 mutex_enter(&db->db_mtx);
1273                 /*
1274                  * Since we've dropped the mutex, it's possible that
1275                  * dbuf_undirty() might have changed this out from under us.
1276                  */
1277                 if (db->db_last_dirty == dr ||
1278                     dn->dn_object == DMU_META_DNODE_OBJECT) {
1279                         mutex_enter(&di->dt.di.dr_mtx);
1280                         ASSERT3U(di->dr_txg, ==, tx->tx_txg);
1281                         ASSERT(!list_link_active(&dr->dr_dirty_node));
1282                         list_insert_tail(&di->dt.di.dr_children, dr);
1283                         mutex_exit(&di->dt.di.dr_mtx);
1284                         dr->dr_parent = di;
1285                 }
1286                 mutex_exit(&db->db_mtx);
1287         } else {
1288                 ASSERT(db->db_level+1 == dn->dn_nlevels);


1391                 return (B_TRUE);
1392         }
1393 
1394         return (B_FALSE);
1395 }
1396 
1397 void
1398 dmu_buf_will_dirty(dmu_buf_t *db_fake, dmu_tx_t *tx)
1399 {
1400         dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1401         int rf = DB_RF_MUST_SUCCEED | DB_RF_NOPREFETCH;
1402 
1403         ASSERT(tx->tx_txg != 0);
1404         ASSERT(!refcount_is_zero(&db->db_holds));
1405 
1406         DB_DNODE_ENTER(db);
1407         if (RW_WRITE_HELD(&DB_DNODE(db)->dn_struct_rwlock))
1408                 rf |= DB_RF_HAVESTRUCT;
1409         DB_DNODE_EXIT(db);
1410         (void) dbuf_read(db, NULL, rf);
1411         (void) dbuf_dirty(db, tx);
1412 }
1413 
1414 void
1415 dmu_buf_will_not_fill(dmu_buf_t *db_fake, dmu_tx_t *tx)
1416 {
1417         dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1418 
1419         db->db_state = DB_NOFILL;
1420 
1421         dmu_buf_will_fill(db_fake, tx);
1422 }
1423 
1424 void
1425 dmu_buf_will_fill(dmu_buf_t *db_fake, dmu_tx_t *tx)
1426 {
1427         dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1428 
1429         ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1430         ASSERT(tx->tx_txg != 0);
1431         ASSERT(db->db_level == 0);
1432         ASSERT(!refcount_is_zero(&db->db_holds));
1433 
1434         ASSERT(db->db.db_object != DMU_META_DNODE_OBJECT ||
1435             dmu_tx_private_ok(tx));
1436 
1437         dbuf_noread(db);
1438         (void) dbuf_dirty(db, tx);


















1439 }
1440 
1441 #pragma weak dmu_buf_fill_done = dbuf_fill_done
1442 /* ARGSUSED */
1443 void
1444 dbuf_fill_done(dmu_buf_impl_t *db, dmu_tx_t *tx)
1445 {
1446         mutex_enter(&db->db_mtx);
1447         DBUF_VERIFY(db);
1448 
1449         if (db->db_state == DB_FILL) {
1450                 if (db->db_level == 0 && db->db_freed_in_flight) {
1451                         ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1452                         /* we were freed while filling */
1453                         /* XXX dbuf_undirty? */
1454                         bzero(db->db.db_data, db->db.db_size);
1455                         db->db_freed_in_flight = FALSE;
1456                 }
1457                 db->db_state = DB_CACHED;
1458                 cv_broadcast(&db->db_changed);


1503         ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1504         ASSERT(db->db_level == 0);
1505         ASSERT(DBUF_GET_BUFC_TYPE(db) == ARC_BUFC_DATA);
1506         ASSERT(buf != NULL);
1507         ASSERT(arc_buf_size(buf) == db->db.db_size);
1508         ASSERT(tx->tx_txg != 0);
1509 
1510         arc_return_buf(buf, db);
1511         ASSERT(arc_released(buf));
1512 
1513         mutex_enter(&db->db_mtx);
1514 
1515         while (db->db_state == DB_READ || db->db_state == DB_FILL)
1516                 cv_wait(&db->db_changed, &db->db_mtx);
1517 
1518         ASSERT(db->db_state == DB_CACHED || db->db_state == DB_UNCACHED);
1519 
1520         if (db->db_state == DB_CACHED &&
1521             refcount_count(&db->db_holds) - 1 > db->db_dirtycnt) {
1522                 mutex_exit(&db->db_mtx);
1523                 (void) dbuf_dirty(db, tx);
1524                 bcopy(buf->b_data, db->db.db_data, db->db.db_size);
1525                 VERIFY(arc_buf_remove_ref(buf, db));
1526                 xuio_stat_wbuf_copied();
1527                 return;
1528         }
1529 
1530         xuio_stat_wbuf_nocopy();
1531         if (db->db_state == DB_CACHED) {
1532                 dbuf_dirty_record_t *dr = db->db_last_dirty;
1533 
1534                 ASSERT(db->db_buf != NULL);
1535                 if (dr != NULL && dr->dr_txg == tx->tx_txg) {
1536                         ASSERT(dr->dt.dl.dr_data == db->db_buf);
1537                         if (!arc_released(db->db_buf)) {
1538                                 ASSERT(dr->dt.dl.dr_override_state ==
1539                                     DR_OVERRIDDEN);
1540                                 arc_release(db->db_buf, db);
1541                         }
1542                         dr->dt.dl.dr_data = buf;
1543                         VERIFY(arc_buf_remove_ref(db->db_buf, db));
1544                 } else if (dr == NULL || dr->dt.dl.dr_data != db->db_buf) {
1545                         arc_release(db->db_buf, db);
1546                         VERIFY(arc_buf_remove_ref(db->db_buf, db));
1547                 }
1548                 db->db_buf = NULL;
1549         }
1550         ASSERT(db->db_buf == NULL);
1551         dbuf_set_data(db, buf);
1552         db->db_state = DB_FILL;
1553         mutex_exit(&db->db_mtx);
1554         (void) dbuf_dirty(db, tx);
1555         dmu_buf_fill_done(&db->db, tx);
1556 }
1557 
1558 /*
1559  * "Clear" the contents of this dbuf.  This will mark the dbuf
1560  * EVICTING and clear *most* of its references.  Unfortunately,
1561  * when we are not holding the dn_dbufs_mtx, we can't clear the
1562  * entry in the dn_dbufs list.  We have to wait until dbuf_destroy()
1563  * in this case.  For callers from the DMU we will usually see:
1564  *      dbuf_clear()->arc_clear_callback()->dbuf_do_evict()->dbuf_destroy()
1565  * For the arc callback, we will usually see:
1566  *      dbuf_do_evict()->dbuf_clear();dbuf_destroy()
1567  * Sometimes, though, we will get a mix of these two:
1568  *      DMU: dbuf_clear()->arc_clear_callback()
1569  *      ARC: dbuf_do_evict()->dbuf_destroy()
1570  *
1571  * This routine will dissociate the dbuf from the arc, by calling
1572  * arc_clear_callback(), but will not evict the data from the ARC.
1573  */
1574 void


2803                         ASSERT3P(db->db_blkptr, ==,
2804                             &dn->dn_phys->dn_blkptr[db->db_blkid]);
2805                 zio = dn->dn_zio;
2806         }
2807 
2808         ASSERT(db->db_level == 0 || data == db->db_buf);
2809         ASSERT3U(db->db_blkptr->blk_birth, <=, txg);
2810         ASSERT(zio);
2811 
2812         SET_BOOKMARK(&zb, os->os_dsl_dataset ?
2813             os->os_dsl_dataset->ds_object : DMU_META_OBJSET,
2814             db->db.db_object, db->db_level, db->db_blkid);
2815 
2816         if (db->db_blkid == DMU_SPILL_BLKID)
2817                 wp_flag = WP_SPILL;
2818         wp_flag |= (db->db_state == DB_NOFILL) ? WP_NOFILL : 0;
2819 
2820         dmu_write_policy(os, dn, db->db_level, wp_flag, &zp);
2821         DB_DNODE_EXIT(db);
2822 










2823         if (db->db_level == 0 &&
2824             dr->dt.dl.dr_override_state == DR_OVERRIDDEN) {
2825                 /*
2826                  * The BP for this block has been provided by open context
2827                  * (by dmu_sync() or dmu_buf_write_embedded()).
2828                  */
2829                 void *contents = (data != NULL) ? data->b_data : NULL;
2830 
2831                 dr->dr_zio = zio_write(zio, os->os_spa, txg,
2832                     db->db_blkptr, contents, db->db.db_size, &zp,
2833                     dbuf_write_override_ready, NULL, dbuf_write_override_done,
2834                     dr, ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
2835                 mutex_enter(&db->db_mtx);
2836                 dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN;
2837                 zio_write_override(dr->dr_zio, &dr->dt.dl.dr_overridden_by,
2838                     dr->dt.dl.dr_copies, dr->dt.dl.dr_nopwrite);
2839                 mutex_exit(&db->db_mtx);
2840         } else if (db->db_state == DB_NOFILL) {
2841                 ASSERT(zp.zp_checksum == ZIO_CHECKSUM_OFF ||
2842                     zp.zp_checksum == ZIO_CHECKSUM_NOPARITY);


1004         mutex_exit(&db->db_mtx);
1005 
1006         dnode_willuse_space(dn, size-osize, tx);
1007         DB_DNODE_EXIT(db);
1008 }
1009 
1010 void
1011 dbuf_release_bp(dmu_buf_impl_t *db)
1012 {
1013         objset_t *os = db->db_objset;
1014 
1015         ASSERT(dsl_pool_sync_context(dmu_objset_pool(os)));
1016         ASSERT(arc_released(os->os_phys_buf) ||
1017             list_link_active(&os->os_dsl_dataset->ds_synced_link));
1018         ASSERT(db->db_parent == NULL || arc_released(db->db_parent->db_buf));
1019 
1020         (void) arc_release(db->db_buf, db);
1021 }
1022 
1023 dbuf_dirty_record_t *
1024 dbuf_zero_dirty(dmu_buf_impl_t *db, dmu_tx_t *tx)
1025 {
1026         ASSERT(db->db_objset != NULL);
1027 
1028         return (dbuf_dirty(db, tx, B_TRUE));
1029 }
1030 
1031 dbuf_dirty_record_t *
1032 dbuf_dirty(dmu_buf_impl_t *db, dmu_tx_t *tx, boolean_t zero_write)
1033 {
1034         dnode_t *dn;
1035         objset_t *os;
1036         dbuf_dirty_record_t **drp, *dr;
1037         int drop_struct_lock = FALSE;
1038         boolean_t do_free_accounting = B_FALSE;
1039         int txgoff = tx->tx_txg & TXG_MASK;
1040 
1041         ASSERT(tx->tx_txg != 0);
1042         ASSERT(!refcount_is_zero(&db->db_holds));
1043         DMU_TX_DIRTY_BUF(tx, db);
1044 
1045         DB_DNODE_ENTER(db);
1046         dn = DB_DNODE(db);
1047         /*
1048          * Shouldn't dirty a regular buffer in syncing context.  Private
1049          * objects may be dirtied in syncing context, but only if they
1050          * were already pre-dirtied in open context.
1051          */
1052         ASSERT(!dmu_tx_is_syncing(tx) ||


1143         dprintf_dbuf(db, "size=%llx\n", (u_longlong_t)db->db.db_size);
1144 
1145         if (db->db_blkid != DMU_BONUS_BLKID) {
1146                 /*
1147                  * Update the accounting.
1148                  * Note: we delay "free accounting" until after we drop
1149                  * the db_mtx.  This keeps us from grabbing other locks
1150                  * (and possibly deadlocking) in bp_get_dsize() while
1151                  * also holding the db_mtx.
1152                  */
1153                 dnode_willuse_space(dn, db->db.db_size, tx);
1154                 do_free_accounting = dbuf_block_freeable(db);
1155         }
1156 
1157         /*
1158          * If this buffer is dirty in an old transaction group we need
1159          * to make a copy of it so that the changes we make in this
1160          * transaction group won't leak out when we sync the older txg.
1161          */
1162         dr = kmem_zalloc(sizeof (dbuf_dirty_record_t), KM_SLEEP);
1163         dr->dr_zero_write = zero_write;
1164         if (db->db_level == 0) {
1165                 void *data_old = db->db_buf;
1166 
1167                 if (db->db_state != DB_NOFILL) {
1168                         if (db->db_blkid == DMU_BONUS_BLKID) {
1169                                 dbuf_fix_old_data(db, tx->tx_txg);
1170                                 data_old = db->db.db_data;
1171                         } else if (db->db.db_object != DMU_META_DNODE_OBJECT) {
1172                                 /*
1173                                  * Release the data buffer from the cache so
1174                                  * that we can modify it without impacting
1175                                  * possible other users of this cached data
1176                                  * block.  Note that indirect blocks and
1177                                  * private objects are not released until the
1178                                  * syncing state (since they are only modified
1179                                  * then).
1180                                  */
1181                                 arc_release(db->db_buf, db);
1182                                 dbuf_fix_old_data(db, tx->tx_txg);
1183                                 data_old = db->db_buf;


1257                 dnode_new_blkid(dn, db->db_blkid, tx, drop_struct_lock);
1258                 ASSERT(dn->dn_maxblkid >= db->db_blkid);
1259         }
1260 
1261         if (db->db_level+1 < dn->dn_nlevels) {
1262                 dmu_buf_impl_t *parent = db->db_parent;
1263                 dbuf_dirty_record_t *di;
1264                 int parent_held = FALSE;
1265 
1266                 if (db->db_parent == NULL || db->db_parent == dn->dn_dbuf) {
1267                         int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
1268 
1269                         parent = dbuf_hold_level(dn, db->db_level+1,
1270                             db->db_blkid >> epbs, FTAG);
1271                         ASSERT(parent != NULL);
1272                         parent_held = TRUE;
1273                 }
1274                 if (drop_struct_lock)
1275                         rw_exit(&dn->dn_struct_rwlock);
1276                 ASSERT3U(db->db_level+1, ==, parent->db_level);
1277                 di = dbuf_dirty(parent, tx, B_FALSE);
1278                 if (parent_held)
1279                         dbuf_rele(parent, FTAG);
1280 
1281                 mutex_enter(&db->db_mtx);
1282                 /*
1283                  * Since we've dropped the mutex, it's possible that
1284                  * dbuf_undirty() might have changed this out from under us.
1285                  */
1286                 if (db->db_last_dirty == dr ||
1287                     dn->dn_object == DMU_META_DNODE_OBJECT) {
1288                         mutex_enter(&di->dt.di.dr_mtx);
1289                         ASSERT3U(di->dr_txg, ==, tx->tx_txg);
1290                         ASSERT(!list_link_active(&dr->dr_dirty_node));
1291                         list_insert_tail(&di->dt.di.dr_children, dr);
1292                         mutex_exit(&di->dt.di.dr_mtx);
1293                         dr->dr_parent = di;
1294                 }
1295                 mutex_exit(&db->db_mtx);
1296         } else {
1297                 ASSERT(db->db_level+1 == dn->dn_nlevels);


1400                 return (B_TRUE);
1401         }
1402 
1403         return (B_FALSE);
1404 }
1405 
1406 void
1407 dmu_buf_will_dirty(dmu_buf_t *db_fake, dmu_tx_t *tx)
1408 {
1409         dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1410         int rf = DB_RF_MUST_SUCCEED | DB_RF_NOPREFETCH;
1411 
1412         ASSERT(tx->tx_txg != 0);
1413         ASSERT(!refcount_is_zero(&db->db_holds));
1414 
1415         DB_DNODE_ENTER(db);
1416         if (RW_WRITE_HELD(&DB_DNODE(db)->dn_struct_rwlock))
1417                 rf |= DB_RF_HAVESTRUCT;
1418         DB_DNODE_EXIT(db);
1419         (void) dbuf_read(db, NULL, rf);
1420         (void) dbuf_dirty(db, tx, B_FALSE);
1421 }
1422 
1423 void
1424 dmu_buf_will_not_fill(dmu_buf_t *db_fake, dmu_tx_t *tx)
1425 {
1426         dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1427 
1428         db->db_state = DB_NOFILL;
1429 
1430         dmu_buf_will_fill(db_fake, tx);
1431 }
1432 
1433 void
1434 dmu_buf_will_fill(dmu_buf_t *db_fake, dmu_tx_t *tx)
1435 {
1436         dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1437 
1438         ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1439         ASSERT(tx->tx_txg != 0);
1440         ASSERT(db->db_level == 0);
1441         ASSERT(!refcount_is_zero(&db->db_holds));
1442 
1443         ASSERT(db->db.db_object != DMU_META_DNODE_OBJECT ||
1444             dmu_tx_private_ok(tx));
1445 
1446         dbuf_noread(db);
1447         (void) dbuf_dirty(db, tx, B_FALSE);
1448 }
1449 
1450 
1451 void
1452 dmu_buf_will_zero_fill(dmu_buf_t *db_fake, dmu_tx_t *tx)
1453 {
1454         dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1455 
1456         ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1457         ASSERT(tx->tx_txg != 0);
1458         ASSERT(db->db_level == 0);
1459         ASSERT(!refcount_is_zero(&db->db_holds));
1460 
1461         ASSERT(db->db.db_object != DMU_META_DNODE_OBJECT ||
1462             dmu_tx_private_ok(tx));
1463 
1464         dbuf_noread(db);
1465         (void) dbuf_zero_dirty(db, tx);
1466 }
1467 
1468 #pragma weak dmu_buf_fill_done = dbuf_fill_done
1469 /* ARGSUSED */
1470 void
1471 dbuf_fill_done(dmu_buf_impl_t *db, dmu_tx_t *tx)
1472 {
1473         mutex_enter(&db->db_mtx);
1474         DBUF_VERIFY(db);
1475 
1476         if (db->db_state == DB_FILL) {
1477                 if (db->db_level == 0 && db->db_freed_in_flight) {
1478                         ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1479                         /* we were freed while filling */
1480                         /* XXX dbuf_undirty? */
1481                         bzero(db->db.db_data, db->db.db_size);
1482                         db->db_freed_in_flight = FALSE;
1483                 }
1484                 db->db_state = DB_CACHED;
1485                 cv_broadcast(&db->db_changed);


1530         ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1531         ASSERT(db->db_level == 0);
1532         ASSERT(DBUF_GET_BUFC_TYPE(db) == ARC_BUFC_DATA);
1533         ASSERT(buf != NULL);
1534         ASSERT(arc_buf_size(buf) == db->db.db_size);
1535         ASSERT(tx->tx_txg != 0);
1536 
1537         arc_return_buf(buf, db);
1538         ASSERT(arc_released(buf));
1539 
1540         mutex_enter(&db->db_mtx);
1541 
1542         while (db->db_state == DB_READ || db->db_state == DB_FILL)
1543                 cv_wait(&db->db_changed, &db->db_mtx);
1544 
1545         ASSERT(db->db_state == DB_CACHED || db->db_state == DB_UNCACHED);
1546 
1547         if (db->db_state == DB_CACHED &&
1548             refcount_count(&db->db_holds) - 1 > db->db_dirtycnt) {
1549                 mutex_exit(&db->db_mtx);
1550                 (void) dbuf_dirty(db, tx, B_FALSE);
1551                 bcopy(buf->b_data, db->db.db_data, db->db.db_size);
1552                 VERIFY(arc_buf_remove_ref(buf, db));
1553                 xuio_stat_wbuf_copied();
1554                 return;
1555         }
1556 
1557         xuio_stat_wbuf_nocopy();
1558         if (db->db_state == DB_CACHED) {
1559                 dbuf_dirty_record_t *dr = db->db_last_dirty;
1560 
1561                 ASSERT(db->db_buf != NULL);
1562                 if (dr != NULL && dr->dr_txg == tx->tx_txg) {
1563                         ASSERT(dr->dt.dl.dr_data == db->db_buf);
1564                         if (!arc_released(db->db_buf)) {
1565                                 ASSERT(dr->dt.dl.dr_override_state ==
1566                                     DR_OVERRIDDEN);
1567                                 arc_release(db->db_buf, db);
1568                         }
1569                         dr->dt.dl.dr_data = buf;
1570                         VERIFY(arc_buf_remove_ref(db->db_buf, db));
1571                 } else if (dr == NULL || dr->dt.dl.dr_data != db->db_buf) {
1572                         arc_release(db->db_buf, db);
1573                         VERIFY(arc_buf_remove_ref(db->db_buf, db));
1574                 }
1575                 db->db_buf = NULL;
1576         }
1577         ASSERT(db->db_buf == NULL);
1578         dbuf_set_data(db, buf);
1579         db->db_state = DB_FILL;
1580         mutex_exit(&db->db_mtx);
1581         (void) dbuf_dirty(db, tx, B_FALSE);
1582         dmu_buf_fill_done(&db->db, tx);
1583 }
1584 
1585 /*
1586  * "Clear" the contents of this dbuf.  This will mark the dbuf
1587  * EVICTING and clear *most* of its references.  Unfortunately,
1588  * when we are not holding the dn_dbufs_mtx, we can't clear the
1589  * entry in the dn_dbufs list.  We have to wait until dbuf_destroy()
1590  * in this case.  For callers from the DMU we will usually see:
1591  *      dbuf_clear()->arc_clear_callback()->dbuf_do_evict()->dbuf_destroy()
1592  * For the arc callback, we will usually see:
1593  *      dbuf_do_evict()->dbuf_clear();dbuf_destroy()
1594  * Sometimes, though, we will get a mix of these two:
1595  *      DMU: dbuf_clear()->arc_clear_callback()
1596  *      ARC: dbuf_do_evict()->dbuf_destroy()
1597  *
1598  * This routine will dissociate the dbuf from the arc, by calling
1599  * arc_clear_callback(), but will not evict the data from the ARC.
1600  */
1601 void


2830                         ASSERT3P(db->db_blkptr, ==,
2831                             &dn->dn_phys->dn_blkptr[db->db_blkid]);
2832                 zio = dn->dn_zio;
2833         }
2834 
2835         ASSERT(db->db_level == 0 || data == db->db_buf);
2836         ASSERT3U(db->db_blkptr->blk_birth, <=, txg);
2837         ASSERT(zio);
2838 
2839         SET_BOOKMARK(&zb, os->os_dsl_dataset ?
2840             os->os_dsl_dataset->ds_object : DMU_META_OBJSET,
2841             db->db.db_object, db->db_level, db->db_blkid);
2842 
2843         if (db->db_blkid == DMU_SPILL_BLKID)
2844                 wp_flag = WP_SPILL;
2845         wp_flag |= (db->db_state == DB_NOFILL) ? WP_NOFILL : 0;
2846 
2847         dmu_write_policy(os, dn, db->db_level, wp_flag, &zp);
2848         DB_DNODE_EXIT(db);
2849 
2850         if (dr->dr_zero_write) {
2851                 zp.zp_zero_write = B_TRUE;
2852 
2853                 if (!spa_feature_is_active(os->os_spa, SPA_FEATURE_SPACE_RESERVATION))
2854                 {
2855                         spa_feature_incr(os->os_spa,
2856                             SPA_FEATURE_SPACE_RESERVATION, tx);
2857                 }
2858         }
2859 
2860         if (db->db_level == 0 &&
2861             dr->dt.dl.dr_override_state == DR_OVERRIDDEN) {
2862                 /*
2863                  * The BP for this block has been provided by open context
2864                  * (by dmu_sync() or dmu_buf_write_embedded()).
2865                  */
2866                 void *contents = (data != NULL) ? data->b_data : NULL;
2867 
2868                 dr->dr_zio = zio_write(zio, os->os_spa, txg,
2869                     db->db_blkptr, contents, db->db.db_size, &zp,
2870                     dbuf_write_override_ready, NULL, dbuf_write_override_done,
2871                     dr, ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
2872                 mutex_enter(&db->db_mtx);
2873                 dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN;
2874                 zio_write_override(dr->dr_zio, &dr->dt.dl.dr_overridden_by,
2875                     dr->dt.dl.dr_copies, dr->dt.dl.dr_nopwrite);
2876                 mutex_exit(&db->db_mtx);
2877         } else if (db->db_state == DB_NOFILL) {
2878                 ASSERT(zp.zp_checksum == ZIO_CHECKSUM_OFF ||
2879                     zp.zp_checksum == ZIO_CHECKSUM_NOPARITY);