aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWale Zhang <wale.zhang.ftd@gmail.com>2026-11-05 04:31:09 +0000
committerRyan Moeller <rmoeller@FreeBSD.org>2026-11-14 04:46:09 +0000
commitafc7fc2abe807780e6f9cba96ab73105ebd2a07f (patch)
treec88325d22b8fff4c6e52de4fdaad9b90cd7eb6a9
parent78ca640f79c585ea737c03a0c427bfe53582fd30 (diff)
Fix direct reads of blocks pending free
Direct reads only check the on-disk block pointer for holes, so they can read stale data after a block has been freed but before the free has synced. Share buffered read hole detection with direct reads to honor pending frees as well. Keep cached data ahead of hole detection and preserve the transaction ordering of block clone and direct write overrides. Retain the block pointer recheck after dnode_block_freed() for concurrent syncing. Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov> Signed-off-by: Wale Zhang <wale.zhang.ftd@gmail.com> Closes #19633 Approved by: kevans (mentor) (cherry picked from commit 924b8c89c04e324649855b05986f686b58ca4cb9)
-rw-r--r--sys/contrib/openzfs/include/sys/dbuf.h1
-rw-r--r--sys/contrib/openzfs/module/zfs/dbuf.c28
-rw-r--r--sys/contrib/openzfs/module/zfs/dmu_direct.c2
3 files changed, 22 insertions, 9 deletions
diff --git a/sys/contrib/openzfs/include/sys/dbuf.h b/sys/contrib/openzfs/include/sys/dbuf.h
index 0b379ee79caa..a583cbb4d95c 100644
--- a/sys/contrib/openzfs/include/sys/dbuf.h
+++ b/sys/contrib/openzfs/include/sys/dbuf.h
@@ -389,6 +389,7 @@ dbuf_dirty_record_t *dbuf_dirty(dmu_buf_impl_t *db, dmu_tx_t *tx);
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,
diff --git a/sys/contrib/openzfs/module/zfs/dbuf.c b/sys/contrib/openzfs/module/zfs/dbuf.c
index 3e3cd9d64eac..e291fcf6def8 100644
--- a/sys/contrib/openzfs/module/zfs/dbuf.c
+++ b/sys/contrib/openzfs/module/zfs/dbuf.c
@@ -1464,17 +1464,15 @@ dbuf_handle_indirect_hole(void *data, dnode_t *dn, blkptr_t *dbbp)
}
/*
- * Handle reads on dbufs that are holes, if necessary. This function
- * requires that the dbuf's mutex is held. Returns success (0) if action
- * was taken, ENOENT if no action was taken.
+ * Check for a hole, including frees which have not yet been synced.
+ * The caller must hold db_mtx to stabilize the dirty records.
*/
-static int
-dbuf_read_hole(dmu_buf_impl_t *db, dnode_t *dn, blkptr_t *bp)
+boolean_t
+dbuf_is_hole(dmu_buf_impl_t *db, dnode_t *dn, blkptr_t *bp)
{
ASSERT(MUTEX_HELD(&db->db_mtx));
- arc_buf_t *db_data;
- int is_hole = bp == NULL || BP_IS_HOLE(bp);
+ 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.
@@ -1504,7 +1502,21 @@ dbuf_read_hole(dmu_buf_impl_t *db, dnode_t *dn, blkptr_t *bp)
}
}
- if (is_hole) {
+ 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
+ * was taken, ENOENT if no action was taken.
+ */
+static int
+dbuf_read_hole(dmu_buf_impl_t *db, dnode_t *dn, blkptr_t *bp)
+{
+ ASSERT(MUTEX_HELD(&db->db_mtx));
+ arc_buf_t *db_data;
+
+ if (dbuf_is_hole(db, dn, bp)) {
db_data = dbuf_alloc_arcbuf(db);
memset(db_data->b_data, 0, db->db.db_size);
diff --git a/sys/contrib/openzfs/module/zfs/dmu_direct.c b/sys/contrib/openzfs/module/zfs/dmu_direct.c
index 5b00698da728..496f4d2afdf2 100644
--- a/sys/contrib/openzfs/module/zfs/dmu_direct.c
+++ b/sys/contrib/openzfs/module/zfs/dmu_direct.c
@@ -298,7 +298,7 @@ dmu_read_abd(dnode_t *dn, uint64_t offset, uint64_t size,
* 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 ?