public inbox for linux-btrfs@vger.kernel.org
 help / color / mirror / Atom feed
From: David Sterba <dsterba@suse.cz>
To: Qu Wenruo <wqu@suse.com>
Cc: linux-btrfs@vger.kernel.org
Subject: Re: [PATCH v2 5/6] btrfs-progs: fix-data-checksum: update csum items to fix csum mismatch
Date: Fri, 30 May 2025 13:19:45 +0200	[thread overview]
Message-ID: <20250530111945.GU4037@twin.jikos.cz> (raw)
In-Reply-To: <c78f6903cbb952acad86ac026dd597645d0af31b.1747295965.git.wqu@suse.com>

On Thu, May 15, 2025 at 05:30:20PM +0930, Qu Wenruo wrote:
> +static int update_csum_item(struct btrfs_fs_info *fs_info, u64 logical,
> +			    unsigned int mirror)
> +{
> +	struct btrfs_trans_handle *trans;
> +	struct btrfs_root *csum_root = btrfs_csum_root(fs_info, logical);
> +	struct btrfs_path path = { 0 };
> +	struct btrfs_csum_item *citem;
> +	u64 read_len = fs_info->sectorsize;
> +	u8 csum[BTRFS_CSUM_SIZE] = { 0 };
> +	u8 *buf;
> +	int ret;
> +
> +	buf = malloc(fs_info->sectorsize);
> +	if (!buf)
> +		return -ENOMEM;

Not fixed, but the block buffers can be on stack as well if they're just
for the single function, the size is bounded.

> +	ret = read_data_from_disk(fs_info, buf, logical, &read_len, mirror);
> +	if (ret < 0) {
> +		errno = -ret;
> +		error("failed to read block at logical %llu mirror %u: %m",
> +			logical, mirror);
> +		goto out;
> +	}
> +	trans = btrfs_start_transaction(csum_root, 1);
> +	if (IS_ERR(trans)) {
> +		ret = PTR_ERR(trans);
> +		errno = -ret;
> +		error_msg(ERROR_MSG_START_TRANS, "%m");
> +		goto out;
> +	}
> +	citem = btrfs_lookup_csum(trans, csum_root, &path, logical,
> +				  BTRFS_EXTENT_CSUM_OBJECTID, fs_info->csum_type, 1);
> +	if (IS_ERR(citem)) {
> +		ret = PTR_ERR(citem);
> +		errno = -ret;
> +		error("failed to find csum item for logical %llu: $m", logical);
> +		btrfs_abort_transaction(trans, ret);
> +		goto out;
> +	}
> +	btrfs_csum_data(fs_info, fs_info->csum_type, buf, csum, fs_info->sectorsize);
> +	write_extent_buffer(path.nodes[0], csum, (unsigned long)citem, fs_info->csum_size);
> +	btrfs_release_path(&path);
> +	ret = btrfs_commit_transaction(trans, csum_root);
> +	if (ret < 0) {
> +		errno = -ret;
> +		error_msg(ERROR_MSG_COMMIT_TRANS, "%m");
> +	}
> +	printf("Csum item for logical %llu updated using data from mirror %u\n",
> +		logical, mirror);
> +out:
> +	free(buf);
> +	btrfs_release_path(&path);
> +	return ret;
>  }
>  
>  static void report_corrupted_blocks(struct btrfs_fs_info *fs_info,

  reply	other threads:[~2025-05-30 11:19 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-05-15  8:00 [PATCH v2 0/6] btrfs-progs: introduce "btrfs rescue fix-data-checksum" Qu Wenruo
2025-05-15  8:00 ` [PATCH v2 1/6] " Qu Wenruo
2025-05-30 11:10   ` David Sterba
2025-05-15  8:00 ` [PATCH v2 2/6] btrfs-progs: fix a bug in btrfs_find_item() Qu Wenruo
2025-05-30 11:11   ` David Sterba
2025-05-15  8:00 ` [PATCH v2 3/6] btrfs-progs: fix-data-checksum: show affected files Qu Wenruo
2025-05-30 11:14   ` David Sterba
2025-05-15  8:00 ` [PATCH v2 4/6] btrfs-progs: fix-data-checksum: introduce interactive mode Qu Wenruo
2025-05-30 11:18   ` David Sterba
2025-05-15  8:00 ` [PATCH v2 5/6] btrfs-progs: fix-data-checksum: update csum items to fix csum mismatch Qu Wenruo
2025-05-30 11:19   ` David Sterba [this message]
2025-05-30 22:23     ` Qu Wenruo
2025-05-15  8:00 ` [PATCH v2 6/6] btrfs-progs: fix-data-checksum: introduce -m|--mirror option Qu Wenruo
2025-05-30 11:07 ` [PATCH v2 0/6] btrfs-progs: introduce "btrfs rescue fix-data-checksum" David Sterba
2025-05-31 16:58 ` Goffredo Baroncelli
2025-05-31 22:07   ` Qu Wenruo
2025-06-02 17:28     ` Goffredo Baroncelli
2025-06-02 22:55       ` Qu Wenruo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250530111945.GU4037@twin.jikos.cz \
    --to=dsterba@suse.cz \
    --cc=linux-btrfs@vger.kernel.org \
    --cc=wqu@suse.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox