btrfs: add and use helpers for reading and writing last_trans_committed
Currently the last_trans_committed field of struct btrfs_fs_info is modified and read without any locking or other protection. For example early in the fsync path, skip_inode_logging() is called which reads fs_info->last_trans_committed, but at the same time we can have a transaction commit completing and updating that field. In the case of an fsync this is harmless and any data race should be rare and at most cause an unnecessary logging of an inode. To avoid data race warnings from tools like KCSAN and other issues such as load and store tearing (amongst others, see [1]), create helpers to access the last_trans_committed field of struct btrfs_fs_info using READ_ONCE() and WRITE_ONCE(), and use these helpers everywhere. [1] https://lwn.net/Articles/793253/ Signed-off-by: Filipe Manana <fdmanana@suse.com> Reviewed-by: David Sterba <dsterba@suse.com> Signed-off-by: David Sterba <dsterba@suse.com>
This commit is contained in:
parent
4a4f8fe2b0
commit
0124855ff1
@ -244,6 +244,7 @@ blk_status_t btree_csum_one_bio(struct btrfs_bio *bbio)
|
||||
struct extent_buffer *eb = bbio->private;
|
||||
struct btrfs_fs_info *fs_info = eb->fs_info;
|
||||
u64 found_start = btrfs_header_bytenr(eb);
|
||||
u64 last_trans;
|
||||
u8 result[BTRFS_CSUM_SIZE];
|
||||
int ret;
|
||||
|
||||
@ -281,12 +282,12 @@ blk_status_t btree_csum_one_bio(struct btrfs_bio *bbio)
|
||||
* Also check the generation, the eb reached here must be newer than
|
||||
* last committed. Or something seriously wrong happened.
|
||||
*/
|
||||
if (unlikely(btrfs_header_generation(eb) <= fs_info->last_trans_committed)) {
|
||||
last_trans = btrfs_get_last_trans_committed(fs_info);
|
||||
if (unlikely(btrfs_header_generation(eb) <= last_trans)) {
|
||||
ret = -EUCLEAN;
|
||||
btrfs_err(fs_info,
|
||||
"block=%llu bad generation, have %llu expect > %llu",
|
||||
eb->start, btrfs_header_generation(eb),
|
||||
fs_info->last_trans_committed);
|
||||
eb->start, btrfs_header_generation(eb), last_trans);
|
||||
goto error;
|
||||
}
|
||||
write_extent_buffer(eb, result, 0, fs_info->csum_size);
|
||||
@ -2653,7 +2654,7 @@ static int __cold init_tree_roots(struct btrfs_fs_info *fs_info)
|
||||
|
||||
/* All successful */
|
||||
fs_info->generation = btrfs_header_generation(tree_root->node);
|
||||
fs_info->last_trans_committed = fs_info->generation;
|
||||
btrfs_set_last_trans_committed(fs_info, fs_info->generation);
|
||||
fs_info->last_reloc_trans = 0;
|
||||
|
||||
/* Always begin writing backup roots after the one being used */
|
||||
|
@ -1760,7 +1760,7 @@ static inline bool skip_inode_logging(const struct btrfs_log_ctx *ctx)
|
||||
* and for a fast fsync we don't wait for that, we only wait for the
|
||||
* writeback to complete.
|
||||
*/
|
||||
if (inode->last_trans <= fs_info->last_trans_committed &&
|
||||
if (inode->last_trans <= btrfs_get_last_trans_committed(fs_info) &&
|
||||
(test_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags) ||
|
||||
list_empty(&ctx->ordered_extents)))
|
||||
return true;
|
||||
|
@ -423,6 +423,10 @@ struct btrfs_fs_info {
|
||||
* Should always be updated using btrfs_set_fs_generation().
|
||||
*/
|
||||
u64 generation;
|
||||
/*
|
||||
* Always use btrfs_get_last_trans_committed() and
|
||||
* btrfs_set_last_trans_committed() to read and update this field.
|
||||
*/
|
||||
u64 last_trans_committed;
|
||||
/*
|
||||
* Generation of the last transaction used for block group relocation
|
||||
@ -833,6 +837,16 @@ static inline void btrfs_set_fs_generation(struct btrfs_fs_info *fs_info, u64 ge
|
||||
WRITE_ONCE(fs_info->generation, gen);
|
||||
}
|
||||
|
||||
static inline u64 btrfs_get_last_trans_committed(const struct btrfs_fs_info *fs_info)
|
||||
{
|
||||
return READ_ONCE(fs_info->last_trans_committed);
|
||||
}
|
||||
|
||||
static inline void btrfs_set_last_trans_committed(struct btrfs_fs_info *fs_info, u64 gen)
|
||||
{
|
||||
WRITE_ONCE(fs_info->last_trans_committed, gen);
|
||||
}
|
||||
|
||||
static inline void btrfs_set_last_root_drop_gen(struct btrfs_fs_info *fs_info,
|
||||
u64 gen)
|
||||
{
|
||||
|
@ -3131,7 +3131,7 @@ static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
|
||||
return PTR_ERR(trans);
|
||||
|
||||
/* No running transaction, don't bother */
|
||||
transid = root->fs_info->last_trans_committed;
|
||||
transid = btrfs_get_last_trans_committed(root->fs_info);
|
||||
goto out;
|
||||
}
|
||||
transid = trans->transid;
|
||||
|
@ -2787,7 +2787,7 @@ static noinline_for_stack int scrub_supers(struct scrub_ctx *sctx,
|
||||
if (scrub_dev->fs_devices != fs_info->fs_devices)
|
||||
gen = scrub_dev->generation;
|
||||
else
|
||||
gen = fs_info->last_trans_committed;
|
||||
gen = btrfs_get_last_trans_committed(fs_info);
|
||||
|
||||
for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
|
||||
bytenr = btrfs_sb_offset(i);
|
||||
|
@ -2217,6 +2217,7 @@ static int check_dev_super(struct btrfs_device *dev)
|
||||
{
|
||||
struct btrfs_fs_info *fs_info = dev->fs_info;
|
||||
struct btrfs_super_block *sb;
|
||||
u64 last_trans;
|
||||
u16 csum_type;
|
||||
int ret = 0;
|
||||
|
||||
@ -2252,10 +2253,10 @@ static int check_dev_super(struct btrfs_device *dev)
|
||||
if (ret < 0)
|
||||
goto out;
|
||||
|
||||
if (btrfs_super_generation(sb) != fs_info->last_trans_committed) {
|
||||
last_trans = btrfs_get_last_trans_committed(fs_info);
|
||||
if (btrfs_super_generation(sb) != last_trans) {
|
||||
btrfs_err(fs_info, "transid mismatch, has %llu expect %llu",
|
||||
btrfs_super_generation(sb),
|
||||
fs_info->last_trans_committed);
|
||||
btrfs_super_generation(sb), last_trans);
|
||||
ret = -EUCLEAN;
|
||||
goto out;
|
||||
}
|
||||
|
@ -980,7 +980,7 @@ int btrfs_wait_for_commit(struct btrfs_fs_info *fs_info, u64 transid)
|
||||
int ret = 0;
|
||||
|
||||
if (transid) {
|
||||
if (transid <= fs_info->last_trans_committed)
|
||||
if (transid <= btrfs_get_last_trans_committed(fs_info))
|
||||
goto out;
|
||||
|
||||
/* find specified transaction */
|
||||
@ -1004,7 +1004,7 @@ int btrfs_wait_for_commit(struct btrfs_fs_info *fs_info, u64 transid)
|
||||
* raced with btrfs_commit_transaction
|
||||
*/
|
||||
if (!cur_trans) {
|
||||
if (transid > fs_info->last_trans_committed)
|
||||
if (transid > btrfs_get_last_trans_committed(fs_info))
|
||||
ret = -EINVAL;
|
||||
goto out;
|
||||
}
|
||||
@ -2587,7 +2587,7 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans)
|
||||
if (test_bit(BTRFS_TRANS_HAVE_FREE_BGS, &cur_trans->flags))
|
||||
btrfs_clear_space_info_full(fs_info);
|
||||
|
||||
fs_info->last_trans_committed = cur_trans->transid;
|
||||
btrfs_set_last_trans_committed(fs_info, cur_trans->transid);
|
||||
/*
|
||||
* We needn't acquire the lock here because there is no other task
|
||||
* which can change it.
|
||||
|
@ -2051,7 +2051,7 @@ int btrfs_verify_level_key(struct extent_buffer *eb, int level,
|
||||
* So we only checks tree blocks which is read from disk, whose
|
||||
* generation <= fs_info->last_trans_committed.
|
||||
*/
|
||||
if (btrfs_header_generation(eb) > fs_info->last_trans_committed)
|
||||
if (btrfs_header_generation(eb) > btrfs_get_last_trans_committed(fs_info))
|
||||
return 0;
|
||||
|
||||
/* We have @first_key, so this @eb must have at least one item */
|
||||
|
Loading…
Reference in New Issue
Block a user