diff mbox series

[1/8] btrfs: delayed-ref: Introduce better documented delayed ref structures

Message ID 20181206065903.11343-2-wqu@suse.com (mailing list archive)
State New, archived
Headers show
Series btrfs: Refactor delayed ref parameter list | expand

Commit Message

Qu Wenruo Dec. 6, 2018, 6:58 a.m. UTC
Current delayed ref interface has several problems:
- Longer and longer parameter lists
  bytenr
  num_bytes
  parent
  ref_root
  owner
  offset
  for_reloc << Only qgroup code cares.

- Different interpretation for the same parameter
  Above @owner for data ref is ino owning this extent,
  while for tree ref, it's level. They are even in different size range.
  For level we only need 0~8, while for ino it's
  BTRFS_FIRST_FREE_OBJECTID~BTRFS_LAST_FREE_OBJECTID.

  And @offset doesn't even makes sense for tree ref.

  Such parameter reuse may look clever as an hidden union, but it
  destroys code readability.

To solve both problems, we introduce a new structure, btrfs_ref to solve
them:

- Structure instead of long parameter list
  This makes later expansion easier, and better documented.

- Use btrfs_ref::type to distinguish data and tree ref

- Use proper union to store data/tree ref specific structures.

- Use separate functions to fill data/tree ref data, with a common generic
  function to fill common bytenr/num_bytes members.

All parameters will find its place in btrfs_ref, and an extra member,
real_root, inspired by ref-verify code, is newly introduced for later
qgroup code, to record which tree is triggered this extent modification.

This patch doesn't touch any code, but provides the basis for incoming
refactors.

Signed-off-by: Qu Wenruo <wqu@suse.com>
---
 fs/btrfs/delayed-ref.h | 109 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 109 insertions(+)

Comments

Nikolay Borisov Dec. 10, 2018, 9:48 a.m. UTC | #1
On 6.12.18 г. 8:58 ч., Qu Wenruo wrote:
> Current delayed ref interface has several problems:
> - Longer and longer parameter lists
>   bytenr
>   num_bytes
>   parent
>   ref_root
>   owner
>   offset
>   for_reloc << Only qgroup code cares.
> 
> - Different interpretation for the same parameter
>   Above @owner for data ref is ino owning this extent,
>   while for tree ref, it's level. They are even in different size range.
>   For level we only need 0~8, while for ino it's
>   BTRFS_FIRST_FREE_OBJECTID~BTRFS_LAST_FREE_OBJECTID.
> 
>   And @offset doesn't even makes sense for tree ref.
> 
>   Such parameter reuse may look clever as an hidden union, but it
>   destroys code readability.
> 
> To solve both problems, we introduce a new structure, btrfs_ref to solve
> them:
> 
> - Structure instead of long parameter list
>   This makes later expansion easier, and better documented.
> 
> - Use btrfs_ref::type to distinguish data and tree ref
> 
> - Use proper union to store data/tree ref specific structures.
> 
> - Use separate functions to fill data/tree ref data, with a common generic
>   function to fill common bytenr/num_bytes members.
> 
> All parameters will find its place in btrfs_ref, and an extra member,
> real_root, inspired by ref-verify code, is newly introduced for later
> qgroup code, to record which tree is triggered this extent modification.
> 
> This patch doesn't touch any code, but provides the basis for incoming
> refactors.
> 
> Signed-off-by: Qu Wenruo <wqu@suse.com>
> ---
>  fs/btrfs/delayed-ref.h | 109 +++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 109 insertions(+)
> 
> diff --git a/fs/btrfs/delayed-ref.h b/fs/btrfs/delayed-ref.h
> index d8fa12d3f2cc..e36d6b05d85e 100644
> --- a/fs/btrfs/delayed-ref.h
> +++ b/fs/btrfs/delayed-ref.h
> @@ -176,6 +176,81 @@ struct btrfs_delayed_ref_root {
>  	u64 qgroup_to_skip;
>  };
>  
> +enum btrfs_ref_type {
> +	BTRFS_REF_NOT_SET = 0,

No need for explicit initialisation when your enums start from 0.

> +	BTRFS_REF_DATA,
> +	BTRFS_REF_METADATA,
> +	BTRFS_REF_LAST,
> +};
> +
> +struct btrfs_data_ref {
> +	/*
> +	 * For EXTENT_DATA_REF
> +	 *
> +	 * @ref_root:	current owner of the extent.
> +	 * 		may differ from btrfs_ref::real_root.

Here 'owner' is a bit ambiguous. Isn't this the root of the owner

> +	 * @ino: 	inode number of the owner. 

And the owner is really the owning inode?

> +	 * @offset:	*CALCULATED* offset. Not EXTENT_DATA key offset.

What does calculated mean here?

> +	 *
> +	 */

Ugh, this is ugly, why not have a single /* */ line above each member
and document them like that?

> +	u64 ref_root;
> +	u64 ino;
> +	u64 offset;
> +};
> +
> +struct btrfs_tree_ref {
> +	/* Common for all sub types and skinny combination */
> +	int level;

Like you've done here. Mention that this is the level in the tree that
this reference is located at.

> +
> +	/*
> +	 * For TREE_BLOCK_REF (skinny metadata, either inline or keyed)
> +	 *
> +	 * root here may differ from btrfs_ref::real_root.
> +	 */

Make it refer to the documentation of the generic btrfs_ref structure
because if someone reads this comment and doesn't read the one in
btrfs_ref then it's not clear under what conditions they might differ.

> +	u64 root;
> +
> +	/* For non-skinny metadata, no special member needed */
> +};
> +
> +struct btrfs_ref {
> +	enum btrfs_ref_type type;
> +	int action;
> +
> +	/*
> +	 * Use full backref(SHARED_BLOCK_REF or SHARED_DATA_REF) for this
> +	 * extent and its children.
> +	 * Set for reloc trees.
> +	 */
> +	unsigned int use_fullback:1;

'fullback' is too terse, use_backreferences or something conveying more
information? Also please use explicit bool type:

bool xxxx:1

> +
> +	/*
> +	 * Whether this extent should go through qgroup record.
> +	 * Normally false, but for certain case like delayed subtree scan,
> +	 * this can hugely reduce qgroup overhead.
> +	 */
> +	unsigned int skip_qgroup:1;

again, explicit bool type please.

> +
> +	/*
> +	 * Who owns this reference modification, optional.
> +	 *
> +	 * One example:
> +	 * When creating reloc tree for source fs, it will increase tree block
> +	 * ref for children tree blocks.
> +	 * In that case, btrfs_ref::real_root = reloc tree,
> +	 * while btrfs_ref::tree_ref::root = fs tree.
> +	 */
> +	u64 real_root;
> +	u64 bytenr;
> +	u64 len;
> +
> +	/* Common @parent for SHARED_DATA_REF/SHARED_BLOCK_REF */

OK, it's common but what does it really contain? Either document it or
rename it to parent_block or something.

> +	u64 parent;
> +	union {
> +		struct btrfs_data_ref data_ref;
> +		struct btrfs_tree_ref tree_ref;
> +	};
> +};
> +
>  extern struct kmem_cache *btrfs_delayed_ref_head_cachep;
>  extern struct kmem_cache *btrfs_delayed_tree_ref_cachep;
>  extern struct kmem_cache *btrfs_delayed_data_ref_cachep;
> @@ -184,6 +259,40 @@ extern struct kmem_cache *btrfs_delayed_extent_op_cachep;
>  int __init btrfs_delayed_ref_init(void);
>  void __cold btrfs_delayed_ref_exit(void);
>  
> +static inline void btrfs_init_generic_ref(struct btrfs_ref *generic_ref,
> +				int action, u64 bytenr, u64 len, u64 real_root,
> +				u64 parent)
> +{
> +	generic_ref->action = action;

IMO it makes sense in this series to have a patch which converts the
action defines to an enum and subsequently modify functions/structs to
actually be of enum type.

> +	generic_ref->bytenr = bytenr;
> +	generic_ref->len = len;
> +	generic_ref->real_root = real_root;
> +	generic_ref->parent = parent;
> +}
> +
> +static inline void btrfs_init_tree_ref(struct btrfs_ref *generic_ref,
> +				int level, u64 root)
> +{
> +	/* If @real_root not set, use @root as fallback */
> +	if (!generic_ref->real_root)
> +		generic_ref->real_root = root;
> +	generic_ref->tree_ref.level = level;
> +	generic_ref->tree_ref.root = root;
> +	generic_ref->type = BTRFS_REF_METADATA;
> +}
> +
> +static inline void btrfs_init_data_ref(struct btrfs_ref *generic_ref,
> +				u64 ref_root, u64 ino, u64 offset)
> +{
> +	/* If @real_root not set, use @root as fallback */
> +	if (!generic_ref->real_root)
> +		generic_ref->real_root = ref_root;
> +	generic_ref->data_ref.ref_root = ref_root;
> +	generic_ref->data_ref.ino = ino;
> +	generic_ref->data_ref.offset = offset;
> +	generic_ref->type = BTRFS_REF_DATA;
> +}
> +
>  static inline struct btrfs_delayed_extent_op *
>  btrfs_alloc_delayed_extent_op(void)
>  {
>
Qu Wenruo Dec. 10, 2018, 11:20 a.m. UTC | #2
On 2018/12/10 下午5:48, Nikolay Borisov wrote:
> 
> 
> On 6.12.18 г. 8:58 ч., Qu Wenruo wrote:
>> Current delayed ref interface has several problems:
>> - Longer and longer parameter lists
>>   bytenr
>>   num_bytes
>>   parent
>>   ref_root
>>   owner
>>   offset
>>   for_reloc << Only qgroup code cares.
>>
>> - Different interpretation for the same parameter
>>   Above @owner for data ref is ino owning this extent,
>>   while for tree ref, it's level. They are even in different size range.
>>   For level we only need 0~8, while for ino it's
>>   BTRFS_FIRST_FREE_OBJECTID~BTRFS_LAST_FREE_OBJECTID.
>>
>>   And @offset doesn't even makes sense for tree ref.
>>
>>   Such parameter reuse may look clever as an hidden union, but it
>>   destroys code readability.
>>
>> To solve both problems, we introduce a new structure, btrfs_ref to solve
>> them:
>>
>> - Structure instead of long parameter list
>>   This makes later expansion easier, and better documented.
>>
>> - Use btrfs_ref::type to distinguish data and tree ref
>>
>> - Use proper union to store data/tree ref specific structures.
>>
>> - Use separate functions to fill data/tree ref data, with a common generic
>>   function to fill common bytenr/num_bytes members.
>>
>> All parameters will find its place in btrfs_ref, and an extra member,
>> real_root, inspired by ref-verify code, is newly introduced for later
>> qgroup code, to record which tree is triggered this extent modification.
>>
>> This patch doesn't touch any code, but provides the basis for incoming
>> refactors.
>>
>> Signed-off-by: Qu Wenruo <wqu@suse.com>
>> ---
>>  fs/btrfs/delayed-ref.h | 109 +++++++++++++++++++++++++++++++++++++++++
>>  1 file changed, 109 insertions(+)
>>
>> diff --git a/fs/btrfs/delayed-ref.h b/fs/btrfs/delayed-ref.h
>> index d8fa12d3f2cc..e36d6b05d85e 100644
>> --- a/fs/btrfs/delayed-ref.h
>> +++ b/fs/btrfs/delayed-ref.h
>> @@ -176,6 +176,81 @@ struct btrfs_delayed_ref_root {
>>  	u64 qgroup_to_skip;
>>  };
>>  
>> +enum btrfs_ref_type {
>> +	BTRFS_REF_NOT_SET = 0,
> 
> No need for explicit initialisation when your enums start from 0.

Forgot that.

> 
>> +	BTRFS_REF_DATA,
>> +	BTRFS_REF_METADATA,
>> +	BTRFS_REF_LAST,
>> +};
>> +
>> +struct btrfs_data_ref {
>> +	/*
>> +	 * For EXTENT_DATA_REF
>> +	 *
>> +	 * @ref_root:	current owner of the extent.
>> +	 * 		may differ from btrfs_ref::real_root.
> 
> Here 'owner' is a bit ambiguous. Isn't this the root of the owner

Well, the @ref_root is a little confusing.
Here it could mean "the current btrfs_header_owner(eb)".

I don't really know the correct term here.

> 
>> +	 * @ino: 	inode number of the owner. 
> 
> And the owner is really the owning inode?

Yes, the owning inode.

> 
>> +	 * @offset:	*CALCULATED* offset. Not EXTENT_DATA key offset.
> 
> What does calculated mean here?

Because btrfs data backref instead of using the offset of the owning
EXTENT_DATA key, it uses key->offset - extent_offset.

> 
>> +	 *
>> +	 */
> 
> Ugh, this is ugly, why not have a single /* */ line above each member
> and document them like that?

Because the complexity of @ref_root can't really go one line.

> 
>> +	u64 ref_root;
>> +	u64 ino;
>> +	u64 offset;
>> +};
>> +
>> +struct btrfs_tree_ref {
>> +	/* Common for all sub types and skinny combination */
>> +	int level;
> 
> Like you've done here. Mention that this is the level in the tree that
> this reference is located at.
> 
>> +
>> +	/*
>> +	 * For TREE_BLOCK_REF (skinny metadata, either inline or keyed)
>> +	 *
>> +	 * root here may differ from btrfs_ref::real_root.
>> +	 */
> 
> Make it refer to the documentation of the generic btrfs_ref structure
> because if someone reads this comment and doesn't read the one in
> btrfs_ref then it's not clear under what conditions they might differ.
> 
>> +	u64 root;
>> +
>> +	/* For non-skinny metadata, no special member needed */
>> +};
>> +
>> +struct btrfs_ref {
>> +	enum btrfs_ref_type type;
>> +	int action;
>> +
>> +	/*
>> +	 * Use full backref(SHARED_BLOCK_REF or SHARED_DATA_REF) for this
>> +	 * extent and its children.
>> +	 * Set for reloc trees.
>> +	 */
>> +	unsigned int use_fullback:1;
> 
> 'fullback' is too terse, use_backreferences or something conveying more
> information?

@use_backreferences looks even stranger to me.

Here the point is, if this bit set, all backref will use
SHARED_BLOCK_REF or SHARED_DATA_REF subtype, and just leave a pointer to
its parent.

Any good idea for explaining this?

> Also please use explicit bool type:
> 
> bool xxxx:1

Is this valid? Haven't seen such usage in kernel code IIRC.

> 
>> +
>> +	/*
>> +	 * Whether this extent should go through qgroup record.
>> +	 * Normally false, but for certain case like delayed subtree scan,
>> +	 * this can hugely reduce qgroup overhead.
>> +	 */
>> +	unsigned int skip_qgroup:1;
> 
> again, explicit bool type please.
> 
>> +
>> +	/*
>> +	 * Who owns this reference modification, optional.
>> +	 *
>> +	 * One example:
>> +	 * When creating reloc tree for source fs, it will increase tree block
>> +	 * ref for children tree blocks.
>> +	 * In that case, btrfs_ref::real_root = reloc tree,
>> +	 * while btrfs_ref::tree_ref::root = fs tree.
>> +	 */
>> +	u64 real_root;
>> +	u64 bytenr;
>> +	u64 len;
>> +
>> +	/* Common @parent for SHARED_DATA_REF/SHARED_BLOCK_REF */
> 
> OK, it's common but what does it really contain? Either document it or
> rename it to parent_block or something.
> 
>> +	u64 parent;
>> +	union {
>> +		struct btrfs_data_ref data_ref;
>> +		struct btrfs_tree_ref tree_ref;
>> +	};
>> +};
>> +
>>  extern struct kmem_cache *btrfs_delayed_ref_head_cachep;
>>  extern struct kmem_cache *btrfs_delayed_tree_ref_cachep;
>>  extern struct kmem_cache *btrfs_delayed_data_ref_cachep;
>> @@ -184,6 +259,40 @@ extern struct kmem_cache *btrfs_delayed_extent_op_cachep;
>>  int __init btrfs_delayed_ref_init(void);
>>  void __cold btrfs_delayed_ref_exit(void);
>>  
>> +static inline void btrfs_init_generic_ref(struct btrfs_ref *generic_ref,
>> +				int action, u64 bytenr, u64 len, u64 real_root,
>> +				u64 parent)
>> +{
>> +	generic_ref->action = action;
> 
> IMO it makes sense in this series to have a patch which converts the
> action defines to an enum and subsequently modify functions/structs to
> actually be of enum type.

Makes sense.

Thanks,
Qu

> 
>> +	generic_ref->bytenr = bytenr;
>> +	generic_ref->len = len;
>> +	generic_ref->real_root = real_root;
>> +	generic_ref->parent = parent;
>> +}
>> +
>> +static inline void btrfs_init_tree_ref(struct btrfs_ref *generic_ref,
>> +				int level, u64 root)
>> +{
>> +	/* If @real_root not set, use @root as fallback */
>> +	if (!generic_ref->real_root)
>> +		generic_ref->real_root = root;
>> +	generic_ref->tree_ref.level = level;
>> +	generic_ref->tree_ref.root = root;
>> +	generic_ref->type = BTRFS_REF_METADATA;
>> +}
>> +
>> +static inline void btrfs_init_data_ref(struct btrfs_ref *generic_ref,
>> +				u64 ref_root, u64 ino, u64 offset)
>> +{
>> +	/* If @real_root not set, use @root as fallback */
>> +	if (!generic_ref->real_root)
>> +		generic_ref->real_root = ref_root;
>> +	generic_ref->data_ref.ref_root = ref_root;
>> +	generic_ref->data_ref.ino = ino;
>> +	generic_ref->data_ref.offset = offset;
>> +	generic_ref->type = BTRFS_REF_DATA;
>> +}
>> +
>>  static inline struct btrfs_delayed_extent_op *
>>  btrfs_alloc_delayed_extent_op(void)
>>  {
>>
Nikolay Borisov Dec. 10, 2018, 11:32 a.m. UTC | #3
On 10.12.18 г. 13:20 ч., Qu Wenruo wrote:
> 
> 
> On 2018/12/10 下午5:48, Nikolay Borisov wrote:
>>
>>
>> On 6.12.18 г. 8:58 ч., Qu Wenruo wrote:
>>> Current delayed ref interface has several problems:
>>> - Longer and longer parameter lists
>>>   bytenr
>>>   num_bytes
>>>   parent
>>>   ref_root
>>>   owner
>>>   offset
>>>   for_reloc << Only qgroup code cares.
>>>

<snip>

>>> +struct btrfs_data_ref {
>>> +	/*
>>> +	 * For EXTENT_DATA_REF
>>> +	 *
>>> +	 * @ref_root:	current owner of the extent.
>>> +	 * 		may differ from btrfs_ref::real_root.
>>
>> Here 'owner' is a bit ambiguous. Isn't this the root of the owner
> 
> Well, the @ref_root is a little confusing.
> Here it could mean "the current btrfs_header_owner(eb)".
> 
> I don't really know the correct term here.

My point was that the word owner is being overloaded. I guess the whole
sentence can be rephrased as:

"The root this reference originates from. For cases where ref_root
differs from btrfs_ref::real_root consult btrfs_ref comments".  Or
something along those lines.

> 
>>
>>> +	 * @ino: 	inode number of the owner. 
>>
>> And the owner is really the owning inode?
> 
> Yes, the owning inode.
> 
>>
>>> +	 * @offset:	*CALCULATED* offset. Not EXTENT_DATA key offset.
>>
>> What does calculated mean here?
> 
> Because btrfs data backref instead of using the offset of the owning
> EXTENT_DATA key, it uses key->offset - extent_offset.

There's no way anyone reading: *CALCULATED* offset could infer that.
Please reword. Even this sentence is a bit terse. When writing
documentation assume people who are going to read it will have *very*
little knowledge of the internal structure of the code.

> 
>>
>>> +	 *
>>> +	 */
>>
>> Ugh, this is ugly, why not have a single /* */ line above each member
>> and document them like that?
> 
> Because the complexity of @ref_root can't really go one line.

What I meant is to have the documentation for every member right above
the respective member, rather than having the documentation in one
place, followed by the variables. If it takes more than a single line to
document a member so be it.

<snip>

>>> +	/*
>>> +	 * Use full backref(SHARED_BLOCK_REF or SHARED_DATA_REF) for this
>>> +	 * extent and its children.
>>> +	 * Set for reloc trees.
>>> +	 */
>>> +	unsigned int use_fullback:1;
>>
>> 'fullback' is too terse, use_backreferences or something conveying more
>> information?
> 
> @use_backreferences looks even stranger to me.
> 
> Here the point is, if this bit set, all backref will use
> SHARED_BLOCK_REF or SHARED_DATA_REF subtype, and just leave a pointer to
> its parent.
> 
> Any good idea for explaining this?

What's the alternative if this bit is not set, how would the tree look like?


> 
>> Also please use explicit bool type:
>>
>> bool xxxx:1
> 
> Is this valid? Haven't seen such usage in kernel code IIRC.

git grep 'bool .*:1' | wc -l
417

<snip>
>
Qu Wenruo Dec. 10, 2018, 11:37 a.m. UTC | #4
On 2018/12/10 下午7:32, Nikolay Borisov wrote:
> 
> 
> On 10.12.18 г. 13:20 ч., Qu Wenruo wrote:
>>
>>
>> On 2018/12/10 下午5:48, Nikolay Borisov wrote:
>>>
>>>
>>> On 6.12.18 г. 8:58 ч., Qu Wenruo wrote:
>>>> Current delayed ref interface has several problems:
>>>> - Longer and longer parameter lists
>>>>   bytenr
>>>>   num_bytes
>>>>   parent
>>>>   ref_root
>>>>   owner
>>>>   offset
>>>>   for_reloc << Only qgroup code cares.
>>>>
> 
> <snip>
> 
>>>> +struct btrfs_data_ref {
>>>> +	/*
>>>> +	 * For EXTENT_DATA_REF
>>>> +	 *
>>>> +	 * @ref_root:	current owner of the extent.
>>>> +	 * 		may differ from btrfs_ref::real_root.
>>>
>>> Here 'owner' is a bit ambiguous. Isn't this the root of the owner
>>
>> Well, the @ref_root is a little confusing.
>> Here it could mean "the current btrfs_header_owner(eb)".
>>
>> I don't really know the correct term here.
> 
> My point was that the word owner is being overloaded. I guess the whole
> sentence can be rephrased as:
> 
> "The root this reference originates from. For cases where ref_root
> differs from btrfs_ref::real_root consult btrfs_ref comments".  Or
> something along those lines.
> 
>>
>>>
>>>> +	 * @ino: 	inode number of the owner. 
>>>
>>> And the owner is really the owning inode?
>>
>> Yes, the owning inode.
>>
>>>
>>>> +	 * @offset:	*CALCULATED* offset. Not EXTENT_DATA key offset.
>>>
>>> What does calculated mean here?
>>
>> Because btrfs data backref instead of using the offset of the owning
>> EXTENT_DATA key, it uses key->offset - extent_offset.
> 
> There's no way anyone reading: *CALCULATED* offset could infer that.
> Please reword. Even this sentence is a bit terse. When writing
> documentation assume people who are going to read it will have *very*
> little knowledge of the internal structure of the code.
> 
>>
>>>
>>>> +	 *
>>>> +	 */
>>>
>>> Ugh, this is ugly, why not have a single /* */ line above each member
>>> and document them like that?
>>
>> Because the complexity of @ref_root can't really go one line.
> 
> What I meant is to have the documentation for every member right above
> the respective member, rather than having the documentation in one
> place, followed by the variables. If it takes more than a single line to
> document a member so be it.

Understood.

> 
> <snip>
> 
>>>> +	/*
>>>> +	 * Use full backref(SHARED_BLOCK_REF or SHARED_DATA_REF) for this
>>>> +	 * extent and its children.
>>>> +	 * Set for reloc trees.
>>>> +	 */
>>>> +	unsigned int use_fullback:1;
>>>
>>> 'fullback' is too terse, use_backreferences or something conveying more
>>> information?
>>
>> @use_backreferences looks even stranger to me.
>>
>> Here the point is, if this bit set, all backref will use
>> SHARED_BLOCK_REF or SHARED_DATA_REF subtype, and just leave a pointer to
>> its parent.
>>
>> Any good idea for explaining this?
> 
> What's the alternative if this bit is not set, how would the tree look like?

If not set and @parent is 0, it will goes normal backref like
EXTENT_DATA_REF (root, ino, offset), or TREE_BLOCK_REF (root, level).

If not set and @parent is not 0, it will go SHARED_BLOCK_REF or
SHARED_DATA_REF (parent).

> 
> 
>>
>>> Also please use explicit bool type:
>>>
>>> bool xxxx:1
>>
>> Is this valid? Haven't seen such usage in kernel code IIRC.
> 
> git grep 'bool .*:1' | wc -l
> 417

grep -IR 'bool .*:1' fs/btrfs/ | wc -l
0

So I guess another cleanup?

Thanks,
Qu

> 
> <snip>
>>
David Sterba Dec. 10, 2018, 5:03 p.m. UTC | #5
On Mon, Dec 10, 2018 at 07:37:57PM +0800, Qu Wenruo wrote:
> >>> Also please use explicit bool type:
> >>>
> >>> bool xxxx:1
> >>
> >> Is this valid? Haven't seen such usage in kernel code IIRC.
> > 
> > git grep 'bool .*:1' | wc -l
> > 417
> 
> grep -IR 'bool .*:1' fs/btrfs/ | wc -l
> 0
> 
> So I guess another cleanup?

No bool bitfields unless there's a space saved in the structure, please.
If there are less than 4 bool types in a structure, it's same as 4 bits
in u32 and is ok. The bit manipulation needs more instructions, checking
zero/non-zero of a byte is typically cheaper.

Looking at the structure, the 'int action' can be reduced and the hole
to the next u64 has enough bytes to squeeze a number of bools if needed.
Qu Wenruo Dec. 12, 2018, 5:09 a.m. UTC | #6
On 2018/12/10 下午5:48, Nikolay Borisov wrote:
> 
> 
[snip]
> 
> IMO it makes sense in this series to have a patch which converts the
> action defines to an enum and subsequently modify functions/structs to
> actually be of enum type.
> 

I'm completely OK to convert it to enum, for its conflict free nature.

However I'm not sure to which degree we should go.

For current David's enum cleanup, we all go anonymous enum, which just
acts as an auto-increasing #define.
However AFAIK compiler can further compress the size of named enum if
needed, and it provides much better type check to prevent us to do
stupid type convert.

I'm wondering which way should we go. The named enum (which needs to
modify all parameters) or anonymous enum for less code modification?

Thanks,
Qu
diff mbox series

Patch

diff --git a/fs/btrfs/delayed-ref.h b/fs/btrfs/delayed-ref.h
index d8fa12d3f2cc..e36d6b05d85e 100644
--- a/fs/btrfs/delayed-ref.h
+++ b/fs/btrfs/delayed-ref.h
@@ -176,6 +176,81 @@  struct btrfs_delayed_ref_root {
 	u64 qgroup_to_skip;
 };
 
+enum btrfs_ref_type {
+	BTRFS_REF_NOT_SET = 0,
+	BTRFS_REF_DATA,
+	BTRFS_REF_METADATA,
+	BTRFS_REF_LAST,
+};
+
+struct btrfs_data_ref {
+	/*
+	 * For EXTENT_DATA_REF
+	 *
+	 * @ref_root:	current owner of the extent.
+	 * 		may differ from btrfs_ref::real_root.
+	 * @ino: 	inode number of the owner.
+	 * @offset:	*CALCULATED* offset. Not EXTENT_DATA key offset.
+	 *
+	 */
+	u64 ref_root;
+	u64 ino;
+	u64 offset;
+};
+
+struct btrfs_tree_ref {
+	/* Common for all sub types and skinny combination */
+	int level;
+
+	/*
+	 * For TREE_BLOCK_REF (skinny metadata, either inline or keyed)
+	 *
+	 * root here may differ from btrfs_ref::real_root.
+	 */
+	u64 root;
+
+	/* For non-skinny metadata, no special member needed */
+};
+
+struct btrfs_ref {
+	enum btrfs_ref_type type;
+	int action;
+
+	/*
+	 * Use full backref(SHARED_BLOCK_REF or SHARED_DATA_REF) for this
+	 * extent and its children.
+	 * Set for reloc trees.
+	 */
+	unsigned int use_fullback:1;
+
+	/*
+	 * Whether this extent should go through qgroup record.
+	 * Normally false, but for certain case like delayed subtree scan,
+	 * this can hugely reduce qgroup overhead.
+	 */
+	unsigned int skip_qgroup:1;
+
+	/*
+	 * Who owns this reference modification, optional.
+	 *
+	 * One example:
+	 * When creating reloc tree for source fs, it will increase tree block
+	 * ref for children tree blocks.
+	 * In that case, btrfs_ref::real_root = reloc tree,
+	 * while btrfs_ref::tree_ref::root = fs tree.
+	 */
+	u64 real_root;
+	u64 bytenr;
+	u64 len;
+
+	/* Common @parent for SHARED_DATA_REF/SHARED_BLOCK_REF */
+	u64 parent;
+	union {
+		struct btrfs_data_ref data_ref;
+		struct btrfs_tree_ref tree_ref;
+	};
+};
+
 extern struct kmem_cache *btrfs_delayed_ref_head_cachep;
 extern struct kmem_cache *btrfs_delayed_tree_ref_cachep;
 extern struct kmem_cache *btrfs_delayed_data_ref_cachep;
@@ -184,6 +259,40 @@  extern struct kmem_cache *btrfs_delayed_extent_op_cachep;
 int __init btrfs_delayed_ref_init(void);
 void __cold btrfs_delayed_ref_exit(void);
 
+static inline void btrfs_init_generic_ref(struct btrfs_ref *generic_ref,
+				int action, u64 bytenr, u64 len, u64 real_root,
+				u64 parent)
+{
+	generic_ref->action = action;
+	generic_ref->bytenr = bytenr;
+	generic_ref->len = len;
+	generic_ref->real_root = real_root;
+	generic_ref->parent = parent;
+}
+
+static inline void btrfs_init_tree_ref(struct btrfs_ref *generic_ref,
+				int level, u64 root)
+{
+	/* If @real_root not set, use @root as fallback */
+	if (!generic_ref->real_root)
+		generic_ref->real_root = root;
+	generic_ref->tree_ref.level = level;
+	generic_ref->tree_ref.root = root;
+	generic_ref->type = BTRFS_REF_METADATA;
+}
+
+static inline void btrfs_init_data_ref(struct btrfs_ref *generic_ref,
+				u64 ref_root, u64 ino, u64 offset)
+{
+	/* If @real_root not set, use @root as fallback */
+	if (!generic_ref->real_root)
+		generic_ref->real_root = ref_root;
+	generic_ref->data_ref.ref_root = ref_root;
+	generic_ref->data_ref.ino = ino;
+	generic_ref->data_ref.offset = offset;
+	generic_ref->type = BTRFS_REF_DATA;
+}
+
 static inline struct btrfs_delayed_extent_op *
 btrfs_alloc_delayed_extent_op(void)
 {