linux-btrfs.vger.kernel.org archive mirror
 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 v1.1 3/3] btrfs: tree-checker: Add EXTENT_DATA_REF check
Date: Thu, 8 Aug 2019 16:59:32 +0200	[thread overview]
Message-ID: <20190808145932.GB8267@twin.jikos.cz> (raw)
In-Reply-To: <20190807140843.2728-4-wqu@suse.com>

On Wed, Aug 07, 2019 at 10:08:43PM +0800, Qu Wenruo wrote:
> EXTENT_DATA_REF is a little like DIR_ITEM which contains hash in its
> key->offset.
> 
> This patch will check the following contents:
> - Key->objectid
>   Basic alignment check.
> 
> - Hash
>   Hash of each extent_data_ref item must match key->offset.
> 
> - Offset
>   Basic alignment check.
> 
> Signed-off-by: Qu Wenruo <wqu@suse.com>
> ---
>  fs/btrfs/ctree.h        |  1 +
>  fs/btrfs/extent-tree.c  |  2 +-
>  fs/btrfs/tree-checker.c | 48 +++++++++++++++++++++++++++++++++++++++++
>  3 files changed, 50 insertions(+), 1 deletion(-)
> 
> diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h
> index 0a61dff27f57..710ea3a6608c 100644
> --- a/fs/btrfs/ctree.h
> +++ b/fs/btrfs/ctree.h
> @@ -2679,6 +2679,7 @@ enum btrfs_inline_ref_type {
>  int btrfs_get_extent_inline_ref_type(const struct extent_buffer *eb,
>  				     struct btrfs_extent_inline_ref *iref,
>  				     enum btrfs_inline_ref_type is_data);
> +u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset);
>  
>  u64 btrfs_csum_bytes_to_leaves(struct btrfs_fs_info *fs_info, u64 csum_bytes);
>  
> diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c
> index b4e9e36b65f1..c0888ed503df 100644
> --- a/fs/btrfs/extent-tree.c
> +++ b/fs/btrfs/extent-tree.c
> @@ -1114,7 +1114,7 @@ int btrfs_get_extent_inline_ref_type(const struct extent_buffer *eb,
>  	return BTRFS_REF_TYPE_INVALID;
>  }
>  
> -static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
> +u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
>  {
>  	u32 high_crc = ~(u32)0;
>  	u32 low_crc = ~(u32)0;
> diff --git a/fs/btrfs/tree-checker.c b/fs/btrfs/tree-checker.c
> index 6aaf3650b13d..5755a7a8477f 100644
> --- a/fs/btrfs/tree-checker.c
> +++ b/fs/btrfs/tree-checker.c
> @@ -1178,6 +1178,51 @@ static int check_simple_keyed_refs(struct extent_buffer *leaf,
>  	return 0;
>  }
>  
> +static int check_extent_data_ref(struct extent_buffer *leaf,
> +				 struct btrfs_key *key, int slot)
> +{
> +	struct btrfs_extent_data_ref *dref;
> +	u64 ptr = btrfs_item_ptr_offset(leaf, slot);
> +	u64 end = ptr + btrfs_item_size_nr(leaf, slot);

same here, unsigned long

> +
> +	if (btrfs_item_size_nr(leaf, slot) % sizeof(*dref) != 0) {
> +		generic_err(leaf, slot,
> +	"invalid item size, have %u expect aligned to %lu for key type %u",
> +			    btrfs_item_size_nr(leaf, slot),
> +			    sizeof(*dref), key->type);

sizeof needs %zu

> +	}
> +	if (!IS_ALIGNED(key->objectid, leaf->fs_info->sectorsize)) {
> +		generic_err(leaf, slot,
> +"invalid key objectid for shared block ref, have %llu expect aligned to %u",
> +			    key->objectid, leaf->fs_info->sectorsize);
> +		return -EUCLEAN;
> +	}
> +	for (; ptr < end; ptr += sizeof(*dref)) {
> +		u64 root_objectid;
> +		u64 owner;
> +		u64 offset;
> +		u64 hash;
> +
> +		dref = (struct btrfs_extent_data_ref *)ptr;
> +		root_objectid = btrfs_extent_data_ref_root(leaf, dref);
> +		owner = btrfs_extent_data_ref_objectid(leaf, dref);
> +		offset = btrfs_extent_data_ref_offset(leaf, dref);
> +		hash = hash_extent_data_ref(root_objectid, owner, offset);
> +		if (hash != key->offset) {
> +			extent_err(leaf, slot,
> +	"invalid extent data ref hash, item have 0x%016llx key have 0x%016llx",
> +				   hash, key->offset);
> +			return -EUCLEAN;
> +		}
> +		if (!IS_ALIGNED(offset, leaf->fs_info->sectorsize)) {
> +			extent_err(leaf, slot,
> +	"invalid extent data backref offset, have %llu expect aligned to %u",
> +				   offset, leaf->fs_info->sectorsize);
> +		}
> +	}
> +	return 0;
> +}
> +
>  /*
>   * Common point to switch the item-specific validation.
>   */
> @@ -1225,6 +1270,9 @@ static int check_leaf_item(struct extent_buffer *leaf,
>  	case BTRFS_SHARED_BLOCK_REF_KEY:
>  		ret = check_simple_keyed_refs(leaf, key, slot);
>  		break;
> +	case BTRFS_EXTENT_DATA_REF_KEY:
> +		ret = check_extent_data_ref(leaf, key, slot);
> +		break;
>  	}
>  	return ret;
>  }
> -- 
> 2.22.0

      reply	other threads:[~2019-08-08 14:59 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-08-07 14:08 [PATCH v1.1 0/3] btrfs: tree-checker: Add extent items check Qu Wenruo
2019-08-07 14:08 ` [PATCH v1.1 1/3] btrfs: tree-checker: Add EXTENT_ITEM and METADATA_ITEM check Qu Wenruo
2019-08-08 14:54   ` David Sterba
2019-08-08 23:55     ` WenRuo Qu
2019-08-09 13:34       ` David Sterba
2019-08-07 14:08 ` [PATCH v1.1 2/3] btrfs: tree-checker: Add simple keyed refs check Qu Wenruo
2019-08-07 14:08 ` [PATCH v1.1 3/3] btrfs: tree-checker: Add EXTENT_DATA_REF check Qu Wenruo
2019-08-08 14:59   ` David Sterba [this message]

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=20190808145932.GB8267@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;
as well as URLs for NNTP newsgroup(s).