--- sys/contrib/openzfs/include/sys/dbuf.h.orig +++ sys/contrib/openzfs/include/sys/dbuf.h @@ -389,6 +389,7 @@ dbuf_dirty_record_t *dbuf_dirty_lightweight(dnode_t *dn, uint64_t blkid, dmu_tx_t *tx); boolean_t dbuf_undirty(dmu_buf_impl_t *db, dmu_tx_t *tx); +boolean_t dbuf_is_hole(dmu_buf_impl_t *db, dnode_t *dn, blkptr_t *bp); int dmu_buf_get_bp_from_dbuf(dmu_buf_impl_t *db, blkptr_t **bp); int dmu_buf_untransform_direct(dmu_buf_impl_t *db, spa_t *spa); void dmu_buf_write_embedded(dmu_buf_t *dbuf, void *data, --- sys/contrib/openzfs/include/sys/dnode.h.orig +++ sys/contrib/openzfs/include/sys/dnode.h @@ -449,6 +449,8 @@ void dnode_new_blkid(dnode_t *dn, uint64_t blkid, dmu_tx_t *tx, boolean_t have_read, boolean_t force); uint64_t dnode_block_freed(dnode_t *dn, uint64_t blkid); +uint64_t dnode_block_freed_after(dnode_t *dn, uint64_t blkid, + uint64_t override_txg); void dnode_init(void); void dnode_fini(void); int dnode_next_offset(dnode_t *dn, int flags, uint64_t *off, --- sys/contrib/openzfs/module/zfs/dbuf.c.orig +++ sys/contrib/openzfs/module/zfs/dbuf.c @@ -1464,6 +1464,48 @@ } } +/* + * Check for a hole, including frees which have not yet been synced. + * The caller must hold db_mtx to stabilize the dirty records. + */ +boolean_t +dbuf_is_hole(dmu_buf_impl_t *db, dnode_t *dn, blkptr_t *bp) +{ + ASSERT(MUTEX_HELD(&db->db_mtx)); + + boolean_t is_hole = bp == NULL || BP_IS_HOLE(bp); + /* + * For level 0 blocks only, if the above check didn't find a hole, + * consult dnode_block_freed() to check for pending frees. + * + * If the block has been overridden by a block clone or direct I/O + * write, we can't use dnode_block_freed() directly because it would + * find frees from TXGs before the override, which should not make + * the block appear freed. Instead, check only free ranges from + * TXGs after the override. + * + * Otherwise, recheck BP_IS_HOLE() after dnode_block_freed(): + * dnode_sync_free_ranges() clears the bp before it removes the + * free range, and free_blocks() does not take the parent's + * db_rwlock for blkptrs held in the dnode. If we wait for dn_mtx + * while that happens, block_freed says "no" but bp is now a hole, + * which must not be handed to arc_read(). + */ + if (!is_hole && db->db_level == 0) { + dbuf_dirty_record_t *dr = list_head(&db->db_dirty_records); + if (dr != NULL && + (dr->dt.dl.dr_brtwrite || dr->dt.dl.dr_diowrite)) { + is_hole = dnode_block_freed_after(dn, + db->db_blkid, dr->dr_txg); + } else { + is_hole = dnode_block_freed(dn, db->db_blkid) || + BP_IS_HOLE(bp); + } + } + + return (is_hole); +} + /* * Handle reads on dbufs that are holes, if necessary. This function * requires that the dbuf's mutex is held. Returns success (0) if action @@ -1475,21 +1517,7 @@ ASSERT(MUTEX_HELD(&db->db_mtx)); arc_buf_t *db_data; - int is_hole = bp == NULL || BP_IS_HOLE(bp); - /* - * For level 0 blocks only, if the above check fails: - * Recheck BP_IS_HOLE() after dnode_block_freed() in case dnode_sync() - * processes the delete record and clears the bp while we are waiting - * for the dn_mtx (resulting in a "no" from block_freed). - * - * If bp != db->db_blkptr, it means that it was overridden (by a block - * clone or direct I/O write). We cannot rely on dnode_block_freed as - * the range can be freed in an earlier TXG but overridden in later. - */ - if (!is_hole && db->db_level == 0 && bp == db->db_blkptr) - is_hole = dnode_block_freed(dn, db->db_blkid) || BP_IS_HOLE(bp); - - if (is_hole) { + if (dbuf_is_hole(db, dn, bp)) { db_data = dbuf_alloc_arcbuf(db); memset(db_data->b_data, 0, db->db.db_size); --- sys/contrib/openzfs/module/zfs/dmu.c.orig +++ sys/contrib/openzfs/module/zfs/dmu.c @@ -2668,6 +2668,44 @@ bp = db->db_blkptr; } + /* + * A block with a pending free (a truncate or hole + * punch not yet synced) must not be cloned: the clone + * would add a BRT reference to a block about to be + * freed, so the pending clone would apply onto an + * already-freed DVA and double free it. Report + * EAGAIN and let the caller retry once the free + * syncs (the block then reads as a hole). + * + * A hole (or absent BP) has nothing to free and is + * cloned as a hole below. When the head dirty record + * overrode the BP (a clone in this txg) only frees + * recorded after the override count against it; + * earlier frees predate it. This mirrors + * dbuf_read_hole(). + */ + if (bp != NULL && !BP_IS_HOLE(bp)) { + dbuf_dirty_record_t *dr = + list_head(&db->db_dirty_records); + boolean_t freed; + + DB_DNODE_ENTER(db); + dnode_t *rdn = DB_DNODE(db); + if (dr != NULL && dr->dt.dl.dr_brtwrite) { + freed = dnode_block_freed_after(rdn, + db->db_blkid, dr->dr_txg); + } else { + freed = dnode_block_freed(rdn, + db->db_blkid); + } + DB_DNODE_EXIT(db); + if (freed) { + mutex_exit(&db->db_mtx); + error = SET_ERROR(EAGAIN); + goto out; + } + } + mutex_exit(&db->db_mtx); if (bp == NULL) { --- sys/contrib/openzfs/module/zfs/dmu_direct.c.orig +++ sys/contrib/openzfs/module/zfs/dmu_direct.c @@ -298,7 +298,7 @@ * cached. This will not be considered a direct read for IO * accounting in the same way that an ARC hit is not counted. */ - if (bp == NULL || BP_IS_HOLE(bp) || db->db_state == DB_CACHED) { + if (db->db_state == DB_CACHED || dbuf_is_hole(db, dn, bp)) { size_t aoff = offset < db->db.db_offset ? db->db.db_offset - offset : 0; size_t boff = offset > db->db.db_offset ? --- sys/contrib/openzfs/module/zfs/dnode.c.orig +++ sys/contrib/openzfs/module/zfs/dnode.c @@ -2468,6 +2468,37 @@ return (i < TXG_SIZE); } +/* + * Check if a level-0 block was freed in a TXG after override_txg. + * + * When a block has been overridden (e.g., by block clone or direct I/O), + * we can't use dnode_block_freed() because it checks all active TXGs. + * A free from a TXG *before* the override should not make the block appear + * freed. + */ +uint64_t +dnode_block_freed_after(dnode_t *dn, uint64_t blkid, uint64_t override_txg) +{ + ASSERT(blkid != DMU_BONUS_BLKID); + ASSERT(blkid != DMU_SPILL_BLKID); + + if (dn->dn_free_txg) + return (TRUE); + + mutex_enter(&dn->dn_mtx); + uint64_t open = spa_open_txg(dmu_objset_spa(dn->dn_objset)); + for (uint64_t txg = override_txg + 1; txg <= open; txg++) { + int i = txg & TXG_MASK; + if (dn->dn_free_ranges[i] != NULL && + zfs_range_tree_contains(dn->dn_free_ranges[i], blkid, 1)) { + mutex_exit(&dn->dn_mtx); + return (TRUE); + } + } + mutex_exit(&dn->dn_mtx); + return (FALSE); +} + /* call from syncing context when we actually write/free space for this dnode */ void dnode_diduse_space(dnode_t *dn, int64_t delta)