Skip to contentSkip to conversation

btrfs: Simplify offset calculation method for ctree.h

PATCH In review Zhaolei 3 participants 5 messages Dec 18, 2009 Btrfs file system

lore
Share & tools
lore

Zhaolei

author
#0 lore raw

PATCH 1/1 btrfs: Simplify offset calculation method for ctree.h

Use simple struct operation instead of address calculation.

---
 fs/btrfs/ctree.h |   46 ++++++++++++++--------------------------------
 1 files changed, 14 insertions(+), 32 deletions(-)
fs/btrfs/ctree.h +14 -32
index e5dd628..44a3e98 100644
@@ -1264,12 +1264,12 @@ BTRFS_SETGET_STACK_FUNCS(stack_device_generation, struct btrfs_dev_item,
 
 static inline char *btrfs_device_uuid(struct btrfs_dev_item *d)
 {
-	return (char *)d + offsetof(struct btrfs_dev_item, uuid);
+	return (char *)&d->uuid;
 }
 
 static inline char *btrfs_device_fsid(struct btrfs_dev_item *d)
 {
-	return (char *)d + offsetof(struct btrfs_dev_item, fsid);
+	return (char *)&d->fsid;
 }
 
 BTRFS_SETGET_FUNCS(chunk_length, struct btrfs_chunk, length, 64);
@@ -1286,7 +1286,7 @@ BTRFS_SETGET_FUNCS(stripe_offset, struct btrfs_stripe, offset, 64);
 
 static inline char *btrfs_stripe_dev_uuid(struct btrfs_stripe *s)
 {
-	return (char *)s + offsetof(struct btrfs_stripe, dev_uuid);
+	return (char *)&s->dev_uuid;
 }
 
 BTRFS_SETGET_STACK_FUNCS(stack_chunk_length, struct btrfs_chunk, length, 64);
@@ -1310,10 +1310,7 @@ BTRFS_SETGET_STACK_FUNCS(stack_stripe_offset, struct btrfs_stripe, offset, 64);
 static inline struct btrfs_stripe *btrfs_stripe_nr(struct btrfs_chunk *c,
 						   int nr)
 {
-	unsigned long offset = (unsigned long)c;
-	offset += offsetof(struct btrfs_chunk, stripe);
-	offset += nr * sizeof(struct btrfs_stripe);
-	return (struct btrfs_stripe *)offset;
+	return &c->stripe + nr;
 }
 
 static inline char *btrfs_stripe_dev_uuid_nr(struct btrfs_chunk *c, int nr)
@@ -1383,33 +1380,25 @@ BTRFS_SETGET_FUNCS(inode_flags, struct btrfs_inode_item, flags, 64);
 static inline struct btrfs_timespec *
 btrfs_inode_atime(struct btrfs_inode_item *inode_item)
 {
-	unsigned long ptr = (unsigned long)inode_item;
-	ptr += offsetof(struct btrfs_inode_item, atime);
-	return (struct btrfs_timespec *)ptr;
+	return &inode_item->atime;
 }
 
 static inline struct btrfs_timespec *
 btrfs_inode_mtime(struct btrfs_inode_item *inode_item)
 {
-	unsigned long ptr = (unsigned long)inode_item;
-	ptr += offsetof(struct btrfs_inode_item, mtime);
-	return (struct btrfs_timespec *)ptr;
+	return &inode_item->mtime;
 }
 
 static inline struct btrfs_timespec *
 btrfs_inode_ctime(struct btrfs_inode_item *inode_item)
 {
-	unsigned long ptr = (unsigned long)inode_item;
-	ptr += offsetof(struct btrfs_inode_item, ctime);
-	return (struct btrfs_timespec *)ptr;
+	return &inode_item->ctime;
 }
 
 static inline struct btrfs_timespec *
 btrfs_inode_otime(struct btrfs_inode_item *inode_item)
 {
-	unsigned long ptr = (unsigned long)inode_item;
-	ptr += offsetof(struct btrfs_inode_item, otime);
-	return (struct btrfs_timespec *)ptr;
+	return &inode_item->otime;
 }
 
 BTRFS_SETGET_FUNCS(timespec_sec, struct btrfs_timespec, sec, 64);
@@ -1426,8 +1415,7 @@ BTRFS_SETGET_FUNCS(dev_extent_length, struct btrfs_dev_extent, length, 64);
 
 static inline u8 *btrfs_dev_extent_chunk_tree_uuid(struct btrfs_dev_extent *dev)
 {
-	unsigned long ptr = offsetof(struct btrfs_dev_extent, chunk_tree_uuid);
-	return (u8 *)((unsigned long)dev + ptr);
+	return (u8 *)&dev->chunk_tree_uuid;
 }
 
 BTRFS_SETGET_FUNCS(extent_refs, struct btrfs_extent_item, refs, 64);
@@ -1731,26 +1719,22 @@ static inline void btrfs_set_header_backref_rev(struct extent_buffer *eb,
 
 static inline u8 *btrfs_header_fsid(struct extent_buffer *eb)
 {
-	unsigned long ptr = offsetof(struct btrfs_header, fsid);
-	return (u8 *)ptr;
+	return (u8 *)offsetof(struct btrfs_header, fsid);
 }
 
 static inline u8 *btrfs_header_chunk_tree_uuid(struct extent_buffer *eb)
 {
-	unsigned long ptr = offsetof(struct btrfs_header, chunk_tree_uuid);
-	return (u8 *)ptr;
+	return (u8 *)offsetof(struct btrfs_header, chunk_tree_uuid);
 }
 
 static inline u8 *btrfs_super_fsid(struct extent_buffer *eb)
 {
-	unsigned long ptr = offsetof(struct btrfs_super_block, fsid);
-	return (u8 *)ptr;
+	return (u8 *)offsetof(struct btrfs_super_block, fsid);
 }
 
 static inline u8 *btrfs_header_csum(struct extent_buffer *eb)
 {
-	unsigned long ptr = offsetof(struct btrfs_header, csum);
-	return (u8 *)ptr;
+	return (u8 *)offsetof(struct btrfs_header, csum);
 }
 
 static inline struct btrfs_node *btrfs_buffer_node(struct extent_buffer *eb)
@@ -1858,9 +1842,7 @@ BTRFS_SETGET_FUNCS(file_extent_type, struct btrfs_file_extent_item, type, 8);
 static inline unsigned long
 btrfs_file_extent_inline_start(struct btrfs_file_extent_item *e)
 {
-	unsigned long offset = (unsigned long)e;
-	offset += offsetof(struct btrfs_file_extent_item, disk_bytenr);
-	return offset;
+	return (unsigned long)&e->disk_bytenr;
 }
 
 static inline u32 btrfs_file_extent_calc_inline_size(u32 datasize)
Signature · 1 line
1.5.5.3
#1 lore raw

No, many pointers in btrfs function arguments are not pointing to an absolute address, but relative to the start address of extent.

Take following function as example, argument inode_item is an offset value to the beginning of leaf. So we can't reach its member with &inode_item->xxx

Quoted 4 lines

=C2=A0static inline struct btrfs_timespec * =C2=A0btrfs_inode_mtime(struct btrfs_inode_item *inode_item) =C2=A0{
- =C2=A0 =C2=A0 =C2=A0 unsigned long ptr =3D (unsigned long)inode_ite=

m;

Quoted 1 line

- =C2=A0 =C2=A0 =C2=A0 ptr +=3D offsetof(struct btrfs_inode_item, mti=

me);

Quoted 3 lines

- =C2=A0 =C2=A0 =C2=A0 return (struct btrfs_timespec *)ptr;
+ =C2=A0 =C2=A0 =C2=A0 return &inode_item->mtime; =C2=A0}

author
#2 lore raw
Quoted 5 lines · sniper

No, many pointers in btrfs function arguments are not pointing to an absolute address, but relative to the start address of extent. Take following function as example, argument inode_item is an offset value to the beginning of leaf. So we can't reach its member with &inode_item->xxx

Notice that we are not reach its member, we are getting pointer of its member with &inode_item->mtime.
This operation have same effect as old one.

Thanks
Zhaolei

Quoted 9 lines
Quoted 9 lines

static inline struct btrfs_timespec * btrfs_inode_mtime(struct btrfs_inode_item *inode_item) {

fs/btrfs/ctree.h +1 -3
-       unsigned long ptr = (unsigned long)inode_item;
-       ptr += offsetof(struct btrfs_inode_item, mtime);
-       return (struct btrfs_timespec *)ptr;
+       return &inode_item->mtime;
 }
#3 lore raw

2009/12/18 Zhaolei <[email protected]>:

Quoted 10 lines
Quoted 5 lines · sniper

No, many pointers in btrfs function arguments are not pointing to an absolute address, but relative to the start address of extent. Take following function as example, argument inode_item is an offset value to the beginning of leaf. So we can't reach its member with &inode_item->xxx

Notice that we are not reach its member, we are getting pointer of its member with &inode_item->mtime.
This operation have same effect as old one.

right!

#4 lore raw
In reply to Zhaolei
Quoted 10 lines · Zhaolei
Quoted 5 lines · sniper

No, many pointers in btrfs function arguments are not pointing to an absolute address, but relative to the start address of extent. Take following function as example, argument inode_item is an offset value to the beginning of leaf. So we can't reach its member with &inode_item->xxx

Notice that we are not reach its member, we are getting pointer of its member with &inode_item->mtime.
This operation have same effect as old one.

It relies on gcc doing something specific with the pointer address manipulation while the original code makes it clear we're just taking the offset. We do rely on gcc being gcc in lots of places in the code, but in this case I prefer the offsetof.

-chris