Skip to content

Commit

Permalink
Fix a race condition in dsl_dataset_sync() when activating features
Browse files Browse the repository at this point in the history
The zio returned from arc_write() in dmu_objset_sync() uses
zio_nowait(). However we may reach the end of dsl_dataset_sync()
which checks if we need to activate features in the filesystem
without knowing if that zio has even run through the ZIO pipeline yet.
In that case we will flag features to be activated in
dsl_dataset_block_born() but dsl_dataset_sync() has already
completed its run and those features will not actually be activated.
Mitigate this by moving the feature activation code in the callback of
arc_write(), dmu_objset_write_done(). Also add new ASSERTs in
dsl_scan_visitbp() checking if a block contradicts any filesystem
flags.

Signed-off-by: George Amanakis <gamanakis@gmail.com>
  • Loading branch information
gamanakis committed Nov 19, 2022
1 parent b0657a5 commit 9a1c7e7
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 11 deletions.
1 change: 1 addition & 0 deletions include/sys/dsl_dataset.h
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ int dsl_dataset_rename_snapshot(const char *fsname,
const char *oldsnapname, const char *newsnapname, boolean_t recursive);
int dsl_dataset_snapshot_tmp(const char *fsname, const char *snapname,
minor_t cleanup_minor, const char *htag);
boolean_t zfeature_active(spa_feature_t f, void *arg);

blkptr_t *dsl_dataset_get_blkptr(dsl_dataset_t *ds);

Expand Down
16 changes: 16 additions & 0 deletions module/zfs/dmu_objset.c
Original file line number Diff line number Diff line change
Expand Up @@ -1613,6 +1613,22 @@ dmu_objset_write_done(zio_t *zio, arc_buf_t *abuf, void *arg)

(void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE);
dsl_dataset_block_born(ds, bp, tx);

if (ds != NULL) {
for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
if (zfeature_active(f,
ds->ds_feature_activation[f])) {
if (zfeature_active(f,
ds->ds_feature[f]))
continue;
dsl_dataset_activate_feature(
ds->ds_object, f,
ds->ds_feature_activation[f], tx);
ds->ds_feature[f] =
ds->ds_feature_activation[f];
}
}
}
}
kmem_free(bp, sizeof (*bp));
}
Expand Down
1 change: 1 addition & 0 deletions module/zfs/dmu_send.c
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,7 @@ dmu_dump_write(dmu_send_cookie_t *dscp, dmu_object_type_t type, uint64_t object,
(bp != NULL ? BP_GET_COMPRESS(bp) != ZIO_COMPRESS_OFF &&
io_compressed : lsize != psize);
if (raw || compressed) {
ASSERT(bp != NULL);
ASSERT(raw || dscp->dsc_featureflags &
DMU_BACKUP_FEATURE_COMPRESSED);
ASSERT(!BP_IS_EMBEDDED(bp));
Expand Down
12 changes: 1 addition & 11 deletions module/zfs/dsl_dataset.c
Original file line number Diff line number Diff line change
Expand Up @@ -1039,7 +1039,7 @@ dsl_dataset_has_owner(dsl_dataset_t *ds)
return (rv);
}

static boolean_t
boolean_t
zfeature_active(spa_feature_t f, void *arg)
{
switch (spa_feature_table[f].fi_type) {
Expand Down Expand Up @@ -2118,16 +2118,6 @@ dsl_dataset_sync(dsl_dataset_t *ds, zio_t *zio, dmu_tx_t *tx)
}

dmu_objset_sync(ds->ds_objset, zio, tx);

for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
if (zfeature_active(f, ds->ds_feature_activation[f])) {
if (zfeature_active(f, ds->ds_feature[f]))
continue;
dsl_dataset_activate_feature(ds->ds_object, f,
ds->ds_feature_activation[f], tx);
ds->ds_feature[f] = ds->ds_feature_activation[f];
}
}
}

/*
Expand Down
20 changes: 20 additions & 0 deletions module/zfs/dsl_scan.c
Original file line number Diff line number Diff line change
Expand Up @@ -2024,6 +2024,26 @@ dsl_scan_visitbp(blkptr_t *bp, const zbookmark_phys_t *zb,
return;
}

/*
* Check if this block contradicts any filesystem flags.
*/
spa_feature_t f = SPA_FEATURE_LARGE_BLOCKS;
if (BP_GET_LSIZE(bp) > SPA_OLD_MAXBLOCKSIZE)
ASSERT3B(dsl_dataset_feature_is_active(ds, f), ==, B_TRUE);

f = zio_checksum_to_feature(BP_GET_CHECKSUM(bp));
if (f != SPA_FEATURE_NONE)
ASSERT3B(dsl_dataset_feature_is_active(ds, f), ==, B_TRUE);

f = zio_compress_to_feature(BP_GET_COMPRESS(bp));
if (f != SPA_FEATURE_NONE)
ASSERT3B(dsl_dataset_feature_is_active(ds, f), ==, B_TRUE);

if (bp->blk_birth <= scn->scn_phys.scn_cur_min_txg) {
scn->scn_lt_min_this_txg++;
return;
}

if (bp->blk_birth <= scn->scn_phys.scn_cur_min_txg) {
scn->scn_lt_min_this_txg++;
return;
Expand Down

0 comments on commit 9a1c7e7

Please sign in to comment.