Commit 0783fcfc authored by Chris Mason's avatar Chris Mason Committed by David Woodhouse

Btrfs: struct item endian fixes

Signed-off-by: default avatarChris Mason <chris.mason@oracle.com>
parent e2fa7227
This diff is collapsed.
...@@ -98,10 +98,10 @@ struct ctree_super_block { ...@@ -98,10 +98,10 @@ struct ctree_super_block {
* the key flags parameter. offset and size tell us where to find * the key flags parameter. offset and size tell us where to find
* the item in the leaf (relative to the start of the data area) * the item in the leaf (relative to the start of the data area)
*/ */
struct item { struct btrfs_item {
struct btrfs_disk_key key; struct btrfs_disk_key key;
u16 offset; __le16 offset;
u16 size; __le16 size;
} __attribute__ ((__packed__)); } __attribute__ ((__packed__));
/* /*
...@@ -115,7 +115,8 @@ struct item { ...@@ -115,7 +115,8 @@ struct item {
struct leaf { struct leaf {
struct btrfs_header header; struct btrfs_header header;
union { union {
struct item items[LEAF_DATA_SIZE/sizeof(struct item)]; struct btrfs_item items[LEAF_DATA_SIZE/
sizeof(struct btrfs_item)];
u8 data[CTREE_BLOCKSIZE-sizeof(struct btrfs_header)]; u8 data[CTREE_BLOCKSIZE-sizeof(struct btrfs_header)];
}; };
} __attribute__ ((__packed__)); } __attribute__ ((__packed__));
...@@ -152,6 +153,31 @@ struct ctree_path { ...@@ -152,6 +153,31 @@ struct ctree_path {
int slots[MAX_LEVEL]; int slots[MAX_LEVEL];
}; };
static inline u16 btrfs_item_offset(struct btrfs_item *item)
{
return le16_to_cpu(item->offset);
}
static inline void btrfs_set_item_offset(struct btrfs_item *item, u16 val)
{
item->offset = cpu_to_le16(val);
}
static inline u16 btrfs_item_end(struct btrfs_item *item)
{
return le16_to_cpu(item->offset) + le16_to_cpu(item->size);
}
static inline u16 btrfs_item_size(struct btrfs_item *item)
{
return le16_to_cpu(item->size);
}
static inline void btrfs_set_item_size(struct btrfs_item *item, u16 val)
{
item->size = cpu_to_le16(val);
}
static inline void btrfs_disk_key_to_cpu(struct btrfs_key *cpu, static inline void btrfs_disk_key_to_cpu(struct btrfs_key *cpu,
struct btrfs_disk_key *disk) struct btrfs_disk_key *disk)
{ {
......
...@@ -40,8 +40,8 @@ static int inc_block_ref(struct ctree_root *root, u64 blocknr) ...@@ -40,8 +40,8 @@ static int inc_block_ref(struct ctree_root *root, u64 blocknr)
BUG(); BUG();
BUG_ON(ret != 0); BUG_ON(ret != 0);
l = &path.nodes[0]->leaf; l = &path.nodes[0]->leaf;
item = (struct extent_item *)(l->data + item = (struct extent_item *)(l->data + btrfs_item_offset(l->items +
l->items[path.slots[0]].offset); path.slots[0]));
item->refs++; item->refs++;
BUG_ON(list_empty(&path.nodes[0]->dirty)); BUG_ON(list_empty(&path.nodes[0]->dirty));
...@@ -67,7 +67,8 @@ static int lookup_block_ref(struct ctree_root *root, u64 blocknr, u32 *refs) ...@@ -67,7 +67,8 @@ static int lookup_block_ref(struct ctree_root *root, u64 blocknr, u32 *refs)
BUG(); BUG();
l = &path.nodes[0]->leaf; l = &path.nodes[0]->leaf;
item = (struct extent_item *)(l->data + item = (struct extent_item *)(l->data +
l->items[path.slots[0]].offset); btrfs_item_offset(l->items +
path.slots[0]));
*refs = item->refs; *refs = item->refs;
release_path(root->extent_root, &path); release_path(root->extent_root, &path);
return 0; return 0;
...@@ -144,7 +145,7 @@ int __free_extent(struct ctree_root *root, u64 blocknr, u64 num_blocks) ...@@ -144,7 +145,7 @@ int __free_extent(struct ctree_root *root, u64 blocknr, u64 num_blocks)
struct btrfs_key key; struct btrfs_key key;
struct ctree_root *extent_root = root->extent_root; struct ctree_root *extent_root = root->extent_root;
int ret; int ret;
struct item *item; struct btrfs_item *item;
struct extent_item *ei; struct extent_item *ei;
struct btrfs_key ins; struct btrfs_key ins;
...@@ -162,7 +163,8 @@ int __free_extent(struct ctree_root *root, u64 blocknr, u64 num_blocks) ...@@ -162,7 +163,8 @@ int __free_extent(struct ctree_root *root, u64 blocknr, u64 num_blocks)
BUG(); BUG();
} }
item = path.nodes[0]->leaf.items + path.slots[0]; item = path.nodes[0]->leaf.items + path.slots[0];
ei = (struct extent_item *)(path.nodes[0]->leaf.data + item->offset); ei = (struct extent_item *)(path.nodes[0]->leaf.data +
btrfs_item_offset(item));
BUG_ON(ei->refs == 0); BUG_ON(ei->refs == 0);
ei->refs--; ei->refs--;
if (ei->refs == 0) { if (ei->refs == 0) {
......
...@@ -14,7 +14,7 @@ int mkfs(int fd) ...@@ -14,7 +14,7 @@ int mkfs(int fd)
{ {
struct ctree_root_info info[2]; struct ctree_root_info info[2];
struct leaf empty_leaf; struct leaf empty_leaf;
struct item item; struct btrfs_item item;
struct extent_item extent_item; struct extent_item extent_item;
int ret; int ret;
...@@ -45,31 +45,37 @@ int mkfs(int fd) ...@@ -45,31 +45,37 @@ int mkfs(int fd)
btrfs_set_header_nritems(&empty_leaf.header, 3); btrfs_set_header_nritems(&empty_leaf.header, 3);
/* item1, reserve blocks 0-16 */ /* item1, reserve blocks 0-16 */
item.key.objectid = cpu_to_le64(0); btrfs_set_key_objectid(&item.key, 0);
item.key.offset = cpu_to_le64(17); btrfs_set_key_offset(&item.key, 17);
item.key.flags = cpu_to_le32(0); btrfs_set_key_flags(&item.key, 0);
item.offset = LEAF_DATA_SIZE - sizeof(struct extent_item); btrfs_set_item_offset(&item,
item.size = sizeof(struct extent_item); LEAF_DATA_SIZE - sizeof(struct extent_item));
btrfs_set_item_size(&item, sizeof(struct extent_item));
extent_item.refs = 1; extent_item.refs = 1;
extent_item.owner = 0; extent_item.owner = 0;
memcpy(empty_leaf.items, &item, sizeof(item)); memcpy(empty_leaf.items, &item, sizeof(item));
memcpy(empty_leaf.data + item.offset, &extent_item, item.size); memcpy(empty_leaf.data + btrfs_item_offset(&item), &extent_item,
btrfs_item_size(&item));
/* item2, give block 17 to the root */ /* item2, give block 17 to the root */
item.key.objectid = cpu_to_le64(17); btrfs_set_key_objectid(&item.key, 17);
item.key.offset = cpu_to_le64(1); btrfs_set_key_offset(&item.key, 1);
item.offset = LEAF_DATA_SIZE - sizeof(struct extent_item) * 2; btrfs_set_item_offset(&item,
LEAF_DATA_SIZE - sizeof(struct extent_item) * 2);
extent_item.owner = 1; extent_item.owner = 1;
memcpy(empty_leaf.items + 1, &item, sizeof(item)); memcpy(empty_leaf.items + 1, &item, sizeof(item));
memcpy(empty_leaf.data + item.offset, &extent_item, item.size); memcpy(empty_leaf.data + btrfs_item_offset(&item), &extent_item,
btrfs_item_size(&item));
/* item3, give block 18 for the extent root */ /* item3, give block 18 for the extent root */
item.key.objectid = cpu_to_le64(18); btrfs_set_key_objectid(&item.key, 18);
item.key.offset = cpu_to_le64(1); btrfs_set_key_offset(&item.key, 1);
item.offset = LEAF_DATA_SIZE - sizeof(struct extent_item) * 3; btrfs_set_item_offset(&item,
LEAF_DATA_SIZE - sizeof(struct extent_item) * 3);
extent_item.owner = 2; extent_item.owner = 2;
memcpy(empty_leaf.items + 2, &item, sizeof(item)); memcpy(empty_leaf.items + 2, &item, sizeof(item));
memcpy(empty_leaf.data + item.offset, &extent_item, item.size); memcpy(empty_leaf.data + btrfs_item_offset(&item), &extent_item,
btrfs_item_size(&item));
ret = pwrite(fd, &empty_leaf, sizeof(empty_leaf), 18 * CTREE_BLOCKSIZE); ret = pwrite(fd, &empty_leaf, sizeof(empty_leaf), 18 * CTREE_BLOCKSIZE);
if (ret != sizeof(empty_leaf)) if (ret != sizeof(empty_leaf))
return -1; return -1;
......
...@@ -9,7 +9,7 @@ void print_leaf(struct leaf *l) ...@@ -9,7 +9,7 @@ void print_leaf(struct leaf *l)
{ {
int i; int i;
u32 nr = btrfs_header_nritems(&l->header); u32 nr = btrfs_header_nritems(&l->header);
struct item *item; struct btrfs_item *item;
struct extent_item *ei; struct extent_item *ei;
printf("leaf %Lu total ptrs %d free space %d\n", printf("leaf %Lu total ptrs %d free space %d\n",
btrfs_header_blocknr(&l->header), nr, leaf_free_space(l)); btrfs_header_blocknr(&l->header), nr, leaf_free_space(l));
...@@ -18,12 +18,15 @@ void print_leaf(struct leaf *l) ...@@ -18,12 +18,15 @@ void print_leaf(struct leaf *l)
item = l->items + i; item = l->items + i;
printf("\titem %d key (%Lu %u %Lu) itemoff %d itemsize %d\n", printf("\titem %d key (%Lu %u %Lu) itemoff %d itemsize %d\n",
i, i,
item->key.objectid, item->key.flags, item->key.offset, btrfs_key_objectid(&item->key),
item->offset, item->size); btrfs_key_flags(&item->key),
btrfs_key_offset(&item->key),
btrfs_item_offset(item),
btrfs_item_size(item));
fflush(stdout); fflush(stdout);
printf("\t\titem data %.*s\n", item->size, printf("\t\titem data %.*s\n", btrfs_item_size(item),
l->data+item->offset); l->data + btrfs_item_offset(item));
ei = (struct extent_item *)(l->data + item->offset); ei = (struct extent_item *)(l->data + btrfs_item_offset(item));
printf("\t\textent data refs %u owner %Lu\n", ei->refs, printf("\t\textent data refs %u owner %Lu\n", ei->refs,
ei->owner); ei->owner);
fflush(stdout); fflush(stdout);
......
...@@ -173,7 +173,7 @@ static int empty_tree(struct ctree_root *root, struct radix_tree_root *radix, ...@@ -173,7 +173,7 @@ static int empty_tree(struct ctree_root *root, struct radix_tree_root *radix,
path.slots[0] -= 1; path.slots[0] -= 1;
} }
slot = path.slots[0]; slot = path.slots[0];
found = path.nodes[0]->leaf.items[slot].key.objectid; found=btrfs_key_objectid(&path.nodes[0]->leaf.items[slot].key);
ret = del_item(root, &path); ret = del_item(root, &path);
count++; count++;
if (ret) { if (ret) {
...@@ -274,7 +274,8 @@ static int fill_radix(struct ctree_root *root, struct radix_tree_root *radix) ...@@ -274,7 +274,8 @@ static int fill_radix(struct ctree_root *root, struct radix_tree_root *radix)
slot -= 1; slot -= 1;
} }
for (i = slot; i >= 0; i--) { for (i = slot; i >= 0; i--) {
found = path.nodes[0]->leaf.items[i].key.objectid; found = btrfs_key_objectid(&path.nodes[0]->
leaf.items[i].key);
radix_tree_preload(GFP_KERNEL); radix_tree_preload(GFP_KERNEL);
ret = radix_tree_insert(radix, found, (void *)found); ret = radix_tree_insert(radix, found, (void *)found);
if (ret) { if (ret) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment