Skip to content

Protect remaining db_data accesses #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: vpsadminos-release-next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion module/zfs/bpobj.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

#include <sys/bpobj.h>
#include <sys/zfs_context.h>
#include <sys/dbuf.h>
#include <sys/zfs_refcount.h>
#include <sys/dsl_pool.h>
#include <sys/zfeature.h>
Expand Down Expand Up @@ -131,7 +132,9 @@ bpobj_free(objset_t *os, uint64_t obj, dmu_tx_t *tx)
ASSERT3U(offset, >=, dbuf->db_offset);
ASSERT3U(offset, <, dbuf->db_offset + dbuf->db_size);

mutex_enter(&((dmu_buf_impl_t *)dbuf)->db_mtx);
objarray = dbuf->db_data;
mutex_exit(&((dmu_buf_impl_t *)dbuf)->db_mtx);
bpobj_free(os, objarray[blkoff], tx);
}
if (dbuf) {
Expand Down Expand Up @@ -176,7 +179,9 @@ bpobj_open(bpobj_t *bpo, objset_t *os, uint64_t object)
bpo->bpo_havecomp = (doi.doi_bonus_size > BPOBJ_SIZE_V0);
bpo->bpo_havesubobj = (doi.doi_bonus_size > BPOBJ_SIZE_V1);
bpo->bpo_havefreed = (doi.doi_bonus_size > BPOBJ_SIZE_V2);
mutex_enter(&((dmu_buf_impl_t *)bpo->bpo_dbuf)->db_mtx);
bpo->bpo_phys = bpo->bpo_dbuf->db_data;
mutex_exit(&((dmu_buf_impl_t *)bpo->bpo_dbuf)->db_mtx);
return (0);
}

Expand Down Expand Up @@ -318,13 +323,16 @@ bpobj_iterate_blkptrs(bpobj_info_t *bpi, bpobj_itor_t func, void *arg,
ASSERT3U(offset, >=, dbuf->db_offset);
ASSERT3U(offset, <, dbuf->db_offset + dbuf->db_size);

mutex_enter(&((dmu_buf_impl_t *)dbuf)->db_mtx);
blkptr_t *bparray = dbuf->db_data;
blkptr_t *bp = &bparray[blkoff];

boolean_t bp_freed = BP_GET_FREE(bp);
err = func(arg, bp, bp_freed, tx);
if (err)
if (err) {
mutex_exit(&((dmu_buf_impl_t *)dbuf)->db_mtx);
break;
}

if (free) {
int sign = bp_freed ? -1 : +1;
Expand All @@ -341,6 +349,7 @@ bpobj_iterate_blkptrs(bpobj_info_t *bpi, bpobj_itor_t func, void *arg,
ASSERT3S(bpo->bpo_phys->bpo_num_freed, >=, 0);
}
}
mutex_exit(&((dmu_buf_impl_t *)dbuf)->db_mtx);
}
if (free) {
propagate_space_reduction(bpi, freed, comp_freed,
Expand Down Expand Up @@ -750,9 +759,11 @@ bpobj_enqueue_subobj(bpobj_t *bpo, uint64_t subobj, dmu_tx_t *tx)
DMU_OT_BPOBJ_SUBOBJ, SPA_OLD_MAXBLOCKSIZE,
DMU_OT_NONE, 0, tx);
}
mutex_enter(&((dmu_buf_impl_t *)subdb)->db_mtx);
dmu_write(bpo->bpo_os, bpo->bpo_phys->bpo_subobjs,
bpo->bpo_phys->bpo_num_subobjs * sizeof (subobj),
numsubsub * sizeof (subobj), subdb->db_data, tx);
mutex_exit(&((dmu_buf_impl_t *)subdb)->db_mtx);
dmu_buf_rele(subdb, FTAG);
bpo->bpo_phys->bpo_num_subobjs += numsubsub;

Expand All @@ -774,10 +785,12 @@ bpobj_enqueue_subobj(bpobj_t *bpo, uint64_t subobj, dmu_tx_t *tx)
* to write more data than we have in our buffer.
*/
VERIFY3U(bps->db_size, >=, numbps * sizeof (blkptr_t));
mutex_enter(&((dmu_buf_impl_t *)bps)->db_mtx);
dmu_write(bpo->bpo_os, bpo->bpo_object,
bpo->bpo_phys->bpo_num_blkptrs * sizeof (blkptr_t),
numbps * sizeof (blkptr_t),
bps->db_data, tx);
mutex_exit(&((dmu_buf_impl_t *)bps)->db_mtx);
dmu_buf_rele(bps, FTAG);
bpo->bpo_phys->bpo_num_blkptrs += numbps;

Expand Down Expand Up @@ -920,8 +933,10 @@ bpobj_enqueue(bpobj_t *bpo, const blkptr_t *bp, boolean_t bp_freed,
}

dmu_buf_will_dirty(bpo->bpo_cached_dbuf, tx);
mutex_enter(&((dmu_buf_impl_t *)bpo->bpo_cached_dbuf)->db_mtx);
bparray = bpo->bpo_cached_dbuf->db_data;
bparray[blkoff] = stored_bp;
mutex_exit(&((dmu_buf_impl_t *)bpo->bpo_cached_dbuf)->db_mtx);

dmu_buf_will_dirty(bpo->bpo_dbuf, tx);
bpo->bpo_phys->bpo_num_blkptrs++;
Expand Down
13 changes: 12 additions & 1 deletion module/zfs/bptree.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <sys/arc.h>
#include <sys/bptree.h>
#include <sys/dmu.h>
#include <sys/dbuf.h>
#include <sys/dmu_objset.h>
#include <sys/dmu_tx.h>
#include <sys/dmu_traverse.h>
Expand Down Expand Up @@ -74,12 +75,14 @@ bptree_alloc(objset_t *os, dmu_tx_t *tx)
*/
VERIFY3U(0, ==, dmu_bonus_hold(os, obj, FTAG, &db));
dmu_buf_will_dirty(db, tx);
mutex_enter(&((dmu_buf_impl_t *)db)->db_mtx);
bt = db->db_data;
bt->bt_begin = 0;
bt->bt_end = 0;
bt->bt_bytes = 0;
bt->bt_comp = 0;
bt->bt_uncomp = 0;
mutex_exit(&((dmu_buf_impl_t *)db)->db_mtx);
dmu_buf_rele(db, FTAG);

return (obj);
Expand All @@ -92,11 +95,13 @@ bptree_free(objset_t *os, uint64_t obj, dmu_tx_t *tx)
bptree_phys_t *bt;

VERIFY3U(0, ==, dmu_bonus_hold(os, obj, FTAG, &db));
mutex_enter(&((dmu_buf_impl_t *)db)->db_mtx);
bt = db->db_data;
ASSERT3U(bt->bt_begin, ==, bt->bt_end);
ASSERT0(bt->bt_bytes);
ASSERT0(bt->bt_comp);
ASSERT0(bt->bt_uncomp);
mutex_exit(&((dmu_buf_impl_t *)db)->db_mtx);
dmu_buf_rele(db, FTAG);

return (dmu_object_free(os, obj, tx));
Expand All @@ -110,8 +115,10 @@ bptree_is_empty(objset_t *os, uint64_t obj)
boolean_t rv;

VERIFY0(dmu_bonus_hold(os, obj, FTAG, &db));
mutex_enter(&((dmu_buf_impl_t *)db)->db_mtx);
bt = db->db_data;
rv = (bt->bt_begin == bt->bt_end);
mutex_exit(&((dmu_buf_impl_t *)db)->db_mtx);
dmu_buf_rele(db, FTAG);
return (rv);
}
Expand All @@ -132,15 +139,17 @@ bptree_add(objset_t *os, uint64_t obj, blkptr_t *bp, uint64_t birth_txg,
ASSERT(dmu_tx_is_syncing(tx));

VERIFY3U(0, ==, dmu_bonus_hold(os, obj, FTAG, &db));
dmu_buf_will_dirty(db, tx);
mutex_enter(&((dmu_buf_impl_t *)db)->db_mtx);
bt = db->db_data;

bte = kmem_zalloc(sizeof (*bte), KM_SLEEP);
bte->be_birth_txg = birth_txg;
bte->be_bp = *bp;
dmu_write(os, obj, bt->bt_end * sizeof (*bte), sizeof (*bte), bte, tx);
mutex_exit(&((dmu_buf_impl_t *)db)->db_mtx);
kmem_free(bte, sizeof (*bte));

dmu_buf_will_dirty(db, tx);
bt->bt_end++;
bt->bt_bytes += bytes;
bt->bt_comp += comp;
Expand Down Expand Up @@ -204,7 +213,9 @@ bptree_iterate(objset_t *os, uint64_t obj, boolean_t free, bptree_itor_t func,
if (free)
dmu_buf_will_dirty(db, tx);

mutex_enter(&((dmu_buf_impl_t *)db)->db_mtx);
ba.ba_phys = db->db_data;
mutex_exit(&((dmu_buf_impl_t *)db)->db_mtx);
ba.ba_free = free;
ba.ba_func = func;
ba.ba_arg = arg;
Expand Down
5 changes: 5 additions & 0 deletions module/zfs/brt.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <sys/ddt.h>
#include <sys/bitmap.h>
#include <sys/zap.h>
#include <sys/dbuf.h>
#include <sys/dmu_tx.h>
#include <sys/arc.h>
#include <sys/dsl_pool.h>
Expand Down Expand Up @@ -552,12 +553,14 @@ brt_vdev_load(spa_t *spa, brt_vdev_t *brtvd)
if (error != 0)
return (error);

mutex_enter(&((dmu_buf_impl_t *)db)->db_mtx);
bvphys = db->db_data;
if (spa->spa_brt_rangesize == 0) {
spa->spa_brt_rangesize = bvphys->bvp_rangesize;
} else {
ASSERT3U(spa->spa_brt_rangesize, ==, bvphys->bvp_rangesize);
}
mutex_exit(&((dmu_buf_impl_t *)db)->db_mtx);

brt_vdev_realloc(spa, brtvd);

Expand Down Expand Up @@ -802,6 +805,7 @@ brt_vdev_sync(spa_t *spa, brt_vdev_t *brtvd, dmu_tx_t *tx)
}

dmu_buf_will_dirty(db, tx);
mutex_enter(&((dmu_buf_impl_t *)db)->db_mtx);
bvphys = db->db_data;
bvphys->bvp_mos_entries = brtvd->bv_mos_entries;
bvphys->bvp_size = brtvd->bv_size;
Expand All @@ -814,6 +818,7 @@ brt_vdev_sync(spa_t *spa, brt_vdev_t *brtvd, dmu_tx_t *tx)
bvphys->bvp_rangesize = spa->spa_brt_rangesize;
bvphys->bvp_usedspace = brtvd->bv_usedspace;
bvphys->bvp_savedspace = brtvd->bv_savedspace;
mutex_exit(&((dmu_buf_impl_t *)db)->db_mtx);
dmu_buf_rele(db, FTAG);

brtvd->bv_meta_dirty = FALSE;
Expand Down
12 changes: 11 additions & 1 deletion module/zfs/ddt_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,14 @@ ddt_log_update_header(ddt_t *ddt, ddt_log_t *ddl, dmu_tx_t *tx)
VERIFY0(dmu_bonus_hold(ddt->ddt_os, ddl->ddl_object, FTAG, &db));
dmu_buf_will_dirty(db, tx);

mutex_enter(&((dmu_buf_impl_t *)db)->db_mtx);
ddt_log_header_t *hdr = (ddt_log_header_t *)db->db_data;
DLH_SET_VERSION(hdr, 1);
DLH_SET_FLAGS(hdr, ddl->ddl_flags);
hdr->dlh_length = ddl->ddl_length;
hdr->dlh_first_txg = ddl->ddl_first_txg;
hdr->dlh_checkpoint = ddl->ddl_checkpoint;
mutex_exit(&((dmu_buf_impl_t *)db)->db_mtx);

dmu_buf_rele(db, FTAG);
}
Expand Down Expand Up @@ -290,10 +292,13 @@ ddt_log_entry(ddt_t *ddt, ddt_lightweight_entry_t *ddlwe, ddt_log_update_t *dlu)
*/
if (dlu->dlu_offset == 0) {
dmu_buf_will_fill(db, dlu->dlu_tx, B_FALSE);
mutex_enter(&((dmu_buf_impl_t *)db)->db_mtx);
memset(db->db_data, 0, db->db_size);
mutex_exit(&((dmu_buf_impl_t *)db)->db_mtx);
}

/* Create the log record directly in the buffer */
mutex_enter(&((dmu_buf_impl_t *)db)->db_mtx);
ddt_log_record_t *dlr = (db->db_data + dlu->dlu_offset);
DLR_SET_TYPE(dlr, DLR_ENTRY);
DLR_SET_RECLEN(dlr, dlu->dlu_reclen);
Expand All @@ -307,6 +312,7 @@ ddt_log_entry(ddt_t *ddt, ddt_lightweight_entry_t *ddlwe, ddt_log_update_t *dlu)

/* Advance offset for next record. */
dlu->dlu_offset += dlu->dlu_reclen;
mutex_exit(&((dmu_buf_impl_t *)db)->db_mtx);
}

void
Expand Down Expand Up @@ -563,7 +569,9 @@ ddt_log_load_one(ddt_t *ddt, uint_t n)
dnode_rele(dn, FTAG);
return (err);
}
mutex_enter(&((dmu_buf_impl_t *)db)->db_mtx);
memcpy(&hdr, db->db_data, sizeof (ddt_log_header_t));
mutex_exit(&((dmu_buf_impl_t *)db)->db_mtx);
dmu_buf_rele(db, FTAG);

if (DLH_GET_VERSION(&hdr) != 1) {
Expand Down Expand Up @@ -599,6 +607,7 @@ ddt_log_load_one(ddt_t *ddt, uint_t n)
}

uint64_t boffset = 0;
mutex_enter(&((dmu_buf_impl_t *)db)->db_mtx);
while (boffset < db->db_size) {
ddt_log_record_t *dlr =
(ddt_log_record_t *)(db->db_data + boffset);
Expand All @@ -614,6 +623,7 @@ ddt_log_load_one(ddt_t *ddt, uint_t n)
break;

default:
mutex_exit(&((dmu_buf_impl_t *)db)->db_mtx);
dmu_buf_rele(db, FTAG);
dnode_rele(dn, FTAG);
ddt_log_empty(ddt, ddl);
Expand All @@ -622,7 +632,7 @@ ddt_log_load_one(ddt_t *ddt, uint_t n)

boffset += DLR_GET_RECLEN(dlr);
}

mutex_exit(&((dmu_buf_impl_t *)db)->db_mtx);
dmu_buf_rele(db, FTAG);
}
}
Expand Down
20 changes: 17 additions & 3 deletions module/zfs/dmu.c
Original file line number Diff line number Diff line change
Expand Up @@ -1222,8 +1222,11 @@ dmu_read_impl(dnode_t *dn, uint64_t offset, uint64_t size,
bufoff = offset - db->db_offset;
tocpy = MIN(db->db_size - bufoff, size);

/* Ensure stable copy of db_data */
mutex_enter(&((dmu_buf_impl_t *)db)->db_mtx);
ASSERT(db->db_data != NULL);
(void) memcpy(buf, (char *)db->db_data + bufoff, tocpy);
memcpy(buf, (char *)db->db_data + bufoff, tocpy);
mutex_exit(&((dmu_buf_impl_t *)db)->db_mtx);

offset += tocpy;
size -= tocpy;
Expand Down Expand Up @@ -1280,8 +1283,11 @@ dmu_write_impl(dmu_buf_t **dbp, int numbufs, uint64_t offset, uint64_t size,
else
dmu_buf_will_dirty(db, tx);

/* Serialise modifications to db_data */
mutex_enter(&((dmu_buf_impl_t *)db)->db_mtx);
ASSERT(db->db_data != NULL);
(void) memcpy((char *)db->db_data + bufoff, buf, tocpy);
memcpy((char *)db->db_data + bufoff, buf, tocpy);
mutex_exit(&((dmu_buf_impl_t *)db)->db_mtx);

if (tocpy == db->db_size)
dmu_buf_fill_done(db, tx, B_FALSE);
Expand Down Expand Up @@ -1429,9 +1435,12 @@ dmu_read_uio_dnode(dnode_t *dn, zfs_uio_t *uio, uint64_t size)
bufoff = zfs_uio_offset(uio) - db->db_offset;
tocpy = MIN(db->db_size - bufoff, size);

/* Hold db_mtx during fault move */
mutex_enter(&((dmu_buf_impl_t *)db)->db_mtx);
ASSERT(db->db_data != NULL);
err = zfs_uio_fault_move((char *)db->db_data + bufoff, tocpy,
UIO_READ, uio);
mutex_exit(&((dmu_buf_impl_t *)db)->db_mtx);

if (err)
break;
Expand Down Expand Up @@ -1554,9 +1563,12 @@ dmu_write_uio_dnode(dnode_t *dn, zfs_uio_t *uio, uint64_t size, dmu_tx_t *tx)
else
dmu_buf_will_dirty(db, tx);

/* Protect db_data during write fault move */
mutex_enter(&((dmu_buf_impl_t *)db)->db_mtx);
ASSERT(db->db_data != NULL);
err = zfs_uio_fault_move((char *)db->db_data + bufoff,
tocpy, UIO_WRITE, uio);
mutex_exit(&((dmu_buf_impl_t *)db)->db_mtx);

if (tocpy == db->db_size && dmu_buf_fill_done(db, tx, err)) {
/* The fill was reverted. Undo any uio progress. */
Expand Down Expand Up @@ -2038,11 +2050,13 @@ dmu_sync_late_arrival(zio_t *pio, objset_t *os, dmu_sync_cb_t *done, zgd_t *zgd,
*/
zp->zp_nopwrite = B_FALSE;

mutex_enter(&((dmu_buf_impl_t *)zgd->zgd_db)->db_mtx);
zio_nowait(zio_write(pio, os->os_spa, dmu_tx_get_txg(tx), zgd->zgd_bp,
abd_get_from_buf(zgd->zgd_db->db_data, zgd->zgd_db->db_size),
zgd->zgd_db->db_size, zgd->zgd_db->db_size, zp,
dmu_sync_late_arrival_ready, NULL, dmu_sync_late_arrival_done,
dmu_sync_late_arrival_ready, NULL, dmu_sync_late_arrival_done,
dsa, ZIO_PRIORITY_SYNC_WRITE, ZIO_FLAG_CANFAIL, zb));
mutex_exit(&((dmu_buf_impl_t *)zgd->zgd_db)->db_mtx);

return (0);
}
Expand Down
5 changes: 5 additions & 0 deletions module/zfs/dmu_recv.c
Original file line number Diff line number Diff line change
Expand Up @@ -1664,8 +1664,10 @@ receive_object_is_same_generation(objset_t *os, uint64_t object,
err = dmu_bonus_hold(os, object, FTAG, &old_bonus_dbuf);
if (err != 0)
return (err);
mutex_enter(&((dmu_buf_impl_t *)old_bonus_dbuf)->db_mtx);
err = dmu_get_file_info(os, old_bonus_type, old_bonus_dbuf->db_data,
&zoi);
mutex_exit(&((dmu_buf_impl_t *)old_bonus_dbuf)->db_mtx);
dmu_buf_rele(old_bonus_dbuf, FTAG);
if (err != 0)
return (err);
Expand Down Expand Up @@ -2146,6 +2148,8 @@ receive_object(struct receive_writer_arg *rwa, struct drr_object *drro,
dmu_buf_will_dirty(db, tx);

ASSERT3U(db->db_size, >=, drro->drr_bonuslen);

mutex_enter(&((dmu_buf_impl_t *)db)->db_mtx);
memcpy(db->db_data, data, DRR_OBJECT_PAYLOAD_SIZE(drro));

/*
Expand All @@ -2158,6 +2162,7 @@ receive_object(struct receive_writer_arg *rwa, struct drr_object *drro,
dmu_ot_byteswap[byteswap].ob_func(db->db_data,
DRR_OBJECT_PAYLOAD_SIZE(drro));
}
mutex_exit(&((dmu_buf_impl_t *)db)->db_mtx);
dmu_buf_rele(db, FTAG);
dnode_rele(dn, FTAG);
}
Expand Down
6 changes: 6 additions & 0 deletions module/zfs/dsl_bookmark.c
Original file line number Diff line number Diff line change
Expand Up @@ -494,11 +494,15 @@ dsl_bookmark_create_sync_impl_snap(const char *bookmark, const char *snapshot,
dmu_buf_will_fill(db, tx, B_FALSE);
VERIFY0(dbuf_spill_set_blksz(db, P2ROUNDUP(bonuslen,
SPA_MINBLOCKSIZE), tx));
mutex_enter(&((dmu_buf_impl_t *)db)->db_mtx);
local_rl->rl_phys = db->db_data;
local_rl->rl_dbuf = db;
mutex_exit(&((dmu_buf_impl_t *)db)->db_mtx);
}
mutex_enter(&((dmu_buf_impl_t *)local_rl->rl_dbuf)->db_mtx);
memcpy(local_rl->rl_phys->rlp_snaps, redact_snaps,
sizeof (uint64_t) * num_redact_snaps);
mutex_exit(&((dmu_buf_impl_t *)local_rl->rl_dbuf)->db_mtx);
local_rl->rl_phys->rlp_num_snaps = num_redact_snaps;
if (bookmark_redacted) {
ASSERT3P(redaction_list, ==, NULL);
Expand Down Expand Up @@ -1278,7 +1282,9 @@ dsl_redaction_list_hold_obj(dsl_pool_t *dp, uint64_t rlobj, const void *tag,
rl->rl_dbuf = dbuf;
}
rl->rl_object = rlobj;
mutex_enter(&((dmu_buf_impl_t *)rl->rl_dbuf)->db_mtx);
rl->rl_phys = rl->rl_dbuf->db_data;
mutex_exit(&((dmu_buf_impl_t *)rl->rl_dbuf)->db_mtx);
rl->rl_mos = dp->dp_meta_objset;
zfs_refcount_create(&rl->rl_longholds);
dmu_buf_init_user(&rl->rl_dbu, redaction_list_evict_sync, NULL,
Expand Down
Loading
Loading