Commit 123abc88 authored by Chris Mason's avatar Chris Mason Committed by David Woodhouse

Btrfs: variable block size support

Signed-off-by: default avatarChris Mason <chris.mason@oracle.com>
parent 4beb1b8b
This diff is collapsed.
......@@ -5,7 +5,6 @@
#include "kerncompat.h"
#define BTRFS_MAGIC "_BtRfS_M"
#define BTRFS_BLOCKSIZE 1024
#define BTRFS_ROOT_TREE_OBJECTID 1
#define BTRFS_EXTENT_TREE_OBJECTID 2
......@@ -52,8 +51,11 @@ struct btrfs_header {
} __attribute__ ((__packed__));
#define BTRFS_MAX_LEVEL 8
#define NODEPTRS_PER_BLOCK ((BTRFS_BLOCKSIZE - sizeof(struct btrfs_header)) / \
#define BTRFS_NODEPTRS_PER_BLOCK(r) (((r)->blocksize - \
sizeof(struct btrfs_header)) / \
(sizeof(struct btrfs_disk_key) + sizeof(u64)))
#define __BTRFS_LEAF_DATA_SIZE(bs) ((bs) - sizeof(struct btrfs_header))
#define BTRFS_LEAF_DATA_SIZE(r) (__BTRFS_LEAF_DATA_SIZE(r->blocksize))
struct btrfs_buffer;
......@@ -86,6 +88,7 @@ struct btrfs_root {
int ref_cows;
struct btrfs_root_item root_item;
struct btrfs_key root_key;
u32 blocksize;
};
/*
......@@ -97,7 +100,7 @@ struct btrfs_super_block {
__le64 blocknr; /* this block number */
__le32 csum;
__le64 magic;
__le16 blocksize;
__le32 blocksize;
__le64 generation;
__le64 root;
__le64 total_blocks;
......@@ -111,7 +114,7 @@ struct btrfs_super_block {
*/
struct btrfs_item {
struct btrfs_disk_key key;
__le16 offset;
__le32 offset;
__le16 size;
} __attribute__ ((__packed__));
......@@ -122,24 +125,23 @@ struct btrfs_item {
* The data is separate from the items to get the keys closer together
* during searches.
*/
#define LEAF_DATA_SIZE (BTRFS_BLOCKSIZE - sizeof(struct btrfs_header))
struct btrfs_leaf {
struct btrfs_header header;
union {
struct btrfs_item items[LEAF_DATA_SIZE/
sizeof(struct btrfs_item)];
u8 data[BTRFS_BLOCKSIZE - sizeof(struct btrfs_header)];
};
struct btrfs_item items[];
} __attribute__ ((__packed__));
/*
* all non-leaf blocks are nodes, they hold only keys and pointers to
* other blocks
*/
struct btrfs_key_ptr {
struct btrfs_disk_key key;
__le64 blockptr;
} __attribute__ ((__packed__));
struct btrfs_node {
struct btrfs_header header;
struct btrfs_disk_key keys[NODEPTRS_PER_BLOCK];
__le64 blockptrs[NODEPTRS_PER_BLOCK];
struct btrfs_key_ptr ptrs[];
} __attribute__ ((__packed__));
/*
......@@ -186,28 +188,28 @@ static inline void btrfs_set_extent_refs(struct btrfs_extent_item *ei, u32 val)
static inline u64 btrfs_node_blockptr(struct btrfs_node *n, int nr)
{
return le64_to_cpu(n->blockptrs[nr]);
return le64_to_cpu(n->ptrs[nr].blockptr);
}
static inline void btrfs_set_node_blockptr(struct btrfs_node *n, int nr,
u64 val)
{
n->blockptrs[nr] = cpu_to_le64(val);
n->ptrs[nr].blockptr = cpu_to_le64(val);
}
static inline u16 btrfs_item_offset(struct btrfs_item *item)
static inline u32 btrfs_item_offset(struct btrfs_item *item)
{
return le16_to_cpu(item->offset);
return le32_to_cpu(item->offset);
}
static inline void btrfs_set_item_offset(struct btrfs_item *item, u16 val)
static inline void btrfs_set_item_offset(struct btrfs_item *item, u32 val)
{
item->offset = cpu_to_le16(val);
item->offset = cpu_to_le32(val);
}
static inline u16 btrfs_item_end(struct btrfs_item *item)
static inline u32 btrfs_item_end(struct btrfs_item *item)
{
return le16_to_cpu(item->offset) + le16_to_cpu(item->size);
return le32_to_cpu(item->offset) + le16_to_cpu(item->size);
}
static inline u16 btrfs_item_size(struct btrfs_item *item)
......@@ -390,20 +392,26 @@ static inline void btrfs_set_super_blocks_used(struct btrfs_super_block *s,
s->blocks_used = cpu_to_le64(val);
}
static inline u16 btrfs_super_blocksize(struct btrfs_super_block *s)
static inline u32 btrfs_super_blocksize(struct btrfs_super_block *s)
{
return le16_to_cpu(s->blocksize);
return le32_to_cpu(s->blocksize);
}
static inline void btrfs_set_super_blocksize(struct btrfs_super_block *s,
u16 val)
u32 val)
{
s->blocksize = cpu_to_le32(val);
}
static inline u8 *btrfs_leaf_data(struct btrfs_leaf *l)
{
s->blocksize = cpu_to_le16(val);
return (u8 *)l->items;
}
/* helper function to cast into the data area of the leaf. */
#define btrfs_item_ptr(leaf, slot, type) \
((type *)((leaf)->data + btrfs_item_offset((leaf)->items + (slot))))
((type *)(btrfs_leaf_data(leaf) + \
btrfs_item_offset((leaf)->items + (slot))))
struct btrfs_buffer *btrfs_alloc_free_block(struct btrfs_root *root);
int btrfs_inc_ref(struct btrfs_root *root, struct btrfs_buffer *buf);
......@@ -416,7 +424,7 @@ int btrfs_del_item(struct btrfs_root *root, struct btrfs_path *path);
int btrfs_insert_item(struct btrfs_root *root, struct btrfs_key *key,
void *data, int data_size);
int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path);
int btrfs_leaf_free_space(struct btrfs_leaf *leaf);
int btrfs_leaf_free_space(struct btrfs_root *root, struct btrfs_leaf *leaf);
int btrfs_drop_snapshot(struct btrfs_root *root, struct btrfs_buffer *snap);
int btrfs_finish_extent_commit(struct btrfs_root *root);
int btrfs_del_root(struct btrfs_root *root, struct btrfs_key *key);
......
......@@ -46,7 +46,8 @@ struct btrfs_buffer *alloc_tree_block(struct btrfs_root *root, u64 blocknr)
{
struct btrfs_buffer *buf;
int ret;
buf = malloc(sizeof(struct btrfs_buffer));
buf = malloc(sizeof(struct btrfs_buffer) + root->blocksize);
if (!buf)
return buf;
allocated_blocks++;
......@@ -84,7 +85,7 @@ struct btrfs_buffer *find_tree_block(struct btrfs_root *root, u64 blocknr)
struct btrfs_buffer *read_tree_block(struct btrfs_root *root, u64 blocknr)
{
loff_t offset = blocknr * BTRFS_BLOCKSIZE;
loff_t offset = blocknr * root->blocksize;
struct btrfs_buffer *buf;
int ret;
......@@ -95,8 +96,8 @@ struct btrfs_buffer *read_tree_block(struct btrfs_root *root, u64 blocknr)
buf = alloc_tree_block(root, blocknr);
if (!buf)
return NULL;
ret = pread(root->fp, &buf->node, BTRFS_BLOCKSIZE, offset);
if (ret != BTRFS_BLOCKSIZE) {
ret = pread(root->fp, &buf->node, root->blocksize, offset);
if (ret != root->blocksize) {
free(buf);
return NULL;
}
......@@ -127,13 +128,13 @@ int clean_tree_block(struct btrfs_root *root, struct btrfs_buffer *buf)
int write_tree_block(struct btrfs_root *root, struct btrfs_buffer *buf)
{
u64 blocknr = buf->blocknr;
loff_t offset = blocknr * BTRFS_BLOCKSIZE;
loff_t offset = blocknr * root->blocksize;
int ret;
if (buf->blocknr != btrfs_header_blocknr(&buf->node.header))
BUG();
ret = pwrite(root->fp, &buf->node, BTRFS_BLOCKSIZE, offset);
if (ret != BTRFS_BLOCKSIZE)
ret = pwrite(root->fp, &buf->node, root->blocksize, offset);
if (ret != root->blocksize)
return ret;
return 0;
}
......@@ -215,7 +216,8 @@ int btrfs_commit_transaction(struct btrfs_root *root,
return ret;
}
static int __setup_root(struct btrfs_root *root, u64 objectid, int fp)
static int __setup_root(struct btrfs_super_block *super,
struct btrfs_root *root, u64 objectid, int fp)
{
INIT_LIST_HEAD(&root->trans);
INIT_LIST_HEAD(&root->cache);
......@@ -223,6 +225,8 @@ static int __setup_root(struct btrfs_root *root, u64 objectid, int fp)
root->fp = fp;
root->node = NULL;
root->commit_root = NULL;
root->blocksize = btrfs_super_blocksize(super);
root->ref_cows = 0;
memset(&root->current_insert, 0, sizeof(root->current_insert));
memset(&root->last_insert, 0, sizeof(root->last_insert));
memset(&root->root_key, 0, sizeof(root->root_key));
......@@ -230,19 +234,19 @@ static int __setup_root(struct btrfs_root *root, u64 objectid, int fp)
return 0;
}
static int find_and_setup_root(struct btrfs_root *tree_root, u64 objectid,
static int find_and_setup_root(struct btrfs_super_block *super,
struct btrfs_root *tree_root, u64 objectid,
struct btrfs_root *root, int fp)
{
int ret;
__setup_root(root, objectid, fp);
__setup_root(super, root, objectid, fp);
ret = btrfs_find_last_root(tree_root, objectid,
&root->root_item, &root->root_key);
BUG_ON(ret);
root->node = read_tree_block(root,
btrfs_root_blocknr(&root->root_item));
root->ref_cows = 0;
BUG_ON(!root->node);
return 0;
}
......@@ -277,28 +281,28 @@ struct btrfs_root *open_ctree(char *filename, struct btrfs_super_block *super)
INIT_RADIX_TREE(&tree_root->cache_radix, GFP_KERNEL);
ret = pread(fp, super, sizeof(struct btrfs_super_block),
BTRFS_SUPER_INFO_OFFSET(BTRFS_BLOCKSIZE));
BTRFS_SUPER_INFO_OFFSET);
if (ret == 0 || btrfs_super_root(super) == 0) {
printf("making new FS!\n");
ret = mkfs(fp, 0, BTRFS_BLOCKSIZE);
ret = mkfs(fp, 0, 1024);
if (ret)
return NULL;
ret = pread(fp, super, sizeof(struct btrfs_super_block),
BTRFS_SUPER_INFO_OFFSET(BTRFS_BLOCKSIZE));
BTRFS_SUPER_INFO_OFFSET);
if (ret != sizeof(struct btrfs_super_block))
return NULL;
}
BUG_ON(ret < 0);
__setup_root(tree_root, BTRFS_ROOT_TREE_OBJECTID, fp);
__setup_root(super, tree_root, BTRFS_ROOT_TREE_OBJECTID, fp);
tree_root->node = read_tree_block(tree_root, btrfs_super_root(super));
BUG_ON(!tree_root->node);
ret = find_and_setup_root(tree_root, BTRFS_EXTENT_TREE_OBJECTID,
ret = find_and_setup_root(super, tree_root, BTRFS_EXTENT_TREE_OBJECTID,
extent_root, fp);
BUG_ON(ret);
ret = find_and_setup_root(tree_root, BTRFS_FS_TREE_OBJECTID,
ret = find_and_setup_root(super, tree_root, BTRFS_FS_TREE_OBJECTID,
root, fp);
BUG_ON(ret);
......@@ -313,7 +317,7 @@ int write_ctree_super(struct btrfs_root *root, struct btrfs_super_block *s)
int ret;
btrfs_set_super_root(s, root->tree_root->node->blocknr);
ret = pwrite(root->fp, s, sizeof(*s),
BTRFS_SUPER_INFO_OFFSET(BTRFS_BLOCKSIZE));
BTRFS_SUPER_INFO_OFFSET);
if (ret != sizeof(*s)) {
fprintf(stderr, "failed to write new super block err %d\n", ret);
return ret;
......
......@@ -5,12 +5,12 @@
struct btrfs_buffer {
u64 blocknr;
int count;
struct list_head dirty;
struct list_head cache;
union {
struct btrfs_node node;
struct btrfs_leaf leaf;
};
struct list_head dirty;
struct list_head cache;
};
struct btrfs_buffer *read_tree_block(struct btrfs_root *root, u64 blocknr);
......@@ -24,9 +24,8 @@ struct btrfs_root *open_ctree(char *filename, struct btrfs_super_block *s);
int close_ctree(struct btrfs_root *root, struct btrfs_super_block *s);
void btrfs_block_release(struct btrfs_root *root, struct btrfs_buffer *buf);
int write_ctree_super(struct btrfs_root *root, struct btrfs_super_block *s);
int mkfs(int fd, u64 num_blocks, u16 blocksize);
int mkfs(int fd, u64 num_blocks, u32 blocksize);
#define BTRFS_SUPER_INFO_OFFSET(bs) (16 * (bs))
#define BTRFS_SUPER_INFO_OFFSET (16 * 1024)
#endif
......@@ -143,7 +143,6 @@ static int __free_extent(struct btrfs_root *root, u64 blocknr, u64 num_blocks)
struct btrfs_key key;
struct btrfs_root *extent_root = root->extent_root;
int ret;
struct btrfs_item *item;
struct btrfs_extent_item *ei;
struct btrfs_key ins;
u32 refs;
......@@ -161,9 +160,8 @@ static int __free_extent(struct btrfs_root *root, u64 blocknr, u64 num_blocks)
printf("failed to find %Lu\n", key.objectid);
BUG();
}
item = path.nodes[0]->leaf.items + path.slots[0];
ei = (struct btrfs_extent_item *)(path.nodes[0]->leaf.data +
btrfs_item_offset(item));
ei = btrfs_item_ptr(&path.nodes[0]->leaf, path.slots[0],
struct btrfs_extent_item);
BUG_ON(ei->refs == 0);
refs = btrfs_extent_refs(ei) - 1;
btrfs_set_extent_refs(ei, refs);
......
......@@ -10,19 +10,20 @@
#include "ctree.h"
#include "disk-io.h"
int mkfs(int fd, u64 num_blocks, u16 blocksize)
int mkfs(int fd, u64 num_blocks, u32 blocksize)
{
struct btrfs_super_block super;
struct btrfs_leaf empty_leaf;
struct btrfs_leaf *empty_leaf;
struct btrfs_root_item root_item;
struct btrfs_item item;
struct btrfs_extent_item extent_item;
char *block;
int ret;
u16 itemoff;
u32 itemoff;
u32 start_block = BTRFS_SUPER_INFO_OFFSET / blocksize;
btrfs_set_super_blocknr(&super, 16);
btrfs_set_super_root(&super, 17);
btrfs_set_super_blocknr(&super, start_block);
btrfs_set_super_root(&super, start_block + 1);
strcpy((char *)(&super.magic), BTRFS_MAGIC);
btrfs_set_super_blocksize(&super, blocksize);
btrfs_set_super_total_blocks(&super, num_blocks);
......@@ -32,168 +33,98 @@ int mkfs(int fd, u64 num_blocks, u16 blocksize)
memset(block, 0, blocksize);
BUG_ON(sizeof(super) > blocksize);
memcpy(block, &super, sizeof(super));
ret = pwrite(fd, block, blocksize, BTRFS_SUPER_INFO_OFFSET(blocksize));
ret = pwrite(fd, block, blocksize, BTRFS_SUPER_INFO_OFFSET);
BUG_ON(ret != blocksize);
/* create the tree of root objects */
memset(&empty_leaf, 0, sizeof(empty_leaf));
btrfs_set_header_parentid(&empty_leaf.header, BTRFS_ROOT_TREE_OBJECTID);
btrfs_set_header_blocknr(&empty_leaf.header, 17);
btrfs_set_header_nritems(&empty_leaf.header, 2);
empty_leaf = malloc(blocksize);
memset(empty_leaf, 0, blocksize);
btrfs_set_header_parentid(&empty_leaf->header,
BTRFS_ROOT_TREE_OBJECTID);
btrfs_set_header_blocknr(&empty_leaf->header, start_block + 1);
btrfs_set_header_nritems(&empty_leaf->header, 2);
/* create the items for the root tree */
btrfs_set_root_blocknr(&root_item, 18);
btrfs_set_root_blocknr(&root_item, start_block + 2);
btrfs_set_root_refs(&root_item, 1);
itemoff = LEAF_DATA_SIZE - sizeof(root_item);
itemoff = __BTRFS_LEAF_DATA_SIZE(blocksize) - sizeof(root_item);
btrfs_set_item_offset(&item, itemoff);
btrfs_set_item_size(&item, sizeof(root_item));
btrfs_set_key_objectid(&item.key, BTRFS_EXTENT_TREE_OBJECTID);
btrfs_set_key_offset(&item.key, 0);
btrfs_set_key_flags(&item.key, 0);
memcpy(empty_leaf.items, &item, sizeof(item));
memcpy(empty_leaf.data + itemoff, &root_item, sizeof(root_item));
memcpy(empty_leaf->items, &item, sizeof(item));
memcpy(btrfs_leaf_data(empty_leaf) + itemoff,
&root_item, sizeof(root_item));
btrfs_set_root_blocknr(&root_item, 19);
btrfs_set_root_blocknr(&root_item, start_block + 3);
itemoff = itemoff - sizeof(root_item);
btrfs_set_item_offset(&item, itemoff);
btrfs_set_key_objectid(&item.key, BTRFS_FS_TREE_OBJECTID);
memcpy(empty_leaf.items + 1, &item, sizeof(item));
memcpy(empty_leaf.data + itemoff, &root_item, sizeof(root_item));
ret = pwrite(fd, &empty_leaf, blocksize, 17 * blocksize);
memcpy(empty_leaf->items + 1, &item, sizeof(item));
memcpy(btrfs_leaf_data(empty_leaf) + itemoff,
&root_item, sizeof(root_item));
ret = pwrite(fd, empty_leaf, blocksize, (start_block + 1) * blocksize);
/* create the items for the extent tree */
btrfs_set_header_parentid(&empty_leaf.header,
btrfs_set_header_parentid(&empty_leaf->header,
BTRFS_EXTENT_TREE_OBJECTID);
btrfs_set_header_blocknr(&empty_leaf.header, 18);
btrfs_set_header_nritems(&empty_leaf.header, 4);
btrfs_set_header_blocknr(&empty_leaf->header, start_block + 2);
btrfs_set_header_nritems(&empty_leaf->header, 4);
/* item1, reserve blocks 0-16 */
btrfs_set_key_objectid(&item.key, 0);
btrfs_set_key_offset(&item.key, 17);
btrfs_set_key_offset(&item.key, start_block + 1);
btrfs_set_key_flags(&item.key, 0);
itemoff = LEAF_DATA_SIZE - sizeof(struct btrfs_extent_item);
itemoff = __BTRFS_LEAF_DATA_SIZE(blocksize) -
sizeof(struct btrfs_extent_item);
btrfs_set_item_offset(&item, itemoff);
btrfs_set_item_size(&item, sizeof(struct btrfs_extent_item));
btrfs_set_extent_refs(&extent_item, 1);
btrfs_set_extent_owner(&extent_item, 0);
memcpy(empty_leaf.items, &item, sizeof(item));
memcpy(empty_leaf.data + btrfs_item_offset(&item), &extent_item,
btrfs_item_size(&item));
memcpy(empty_leaf->items, &item, sizeof(item));
memcpy(btrfs_leaf_data(empty_leaf) + btrfs_item_offset(&item),
&extent_item, btrfs_item_size(&item));
/* item2, give block 17 to the root */
btrfs_set_key_objectid(&item.key, 17);
btrfs_set_key_objectid(&item.key, start_block + 1);
btrfs_set_key_offset(&item.key, 1);
itemoff = itemoff - sizeof(struct btrfs_extent_item);
btrfs_set_item_offset(&item, itemoff);
btrfs_set_extent_owner(&extent_item, BTRFS_ROOT_TREE_OBJECTID);
memcpy(empty_leaf.items + 1, &item, sizeof(item));
memcpy(empty_leaf.data + btrfs_item_offset(&item), &extent_item,
btrfs_item_size(&item));
memcpy(empty_leaf->items + 1, &item, sizeof(item));
memcpy(btrfs_leaf_data(empty_leaf) + btrfs_item_offset(&item),
&extent_item, btrfs_item_size(&item));
/* item3, give block 18 to the extent root */
btrfs_set_key_objectid(&item.key, 18);
btrfs_set_key_objectid(&item.key, start_block + 2);
btrfs_set_key_offset(&item.key, 1);
itemoff = itemoff - sizeof(struct btrfs_extent_item);
btrfs_set_item_offset(&item, itemoff);
btrfs_set_extent_owner(&extent_item, BTRFS_EXTENT_TREE_OBJECTID);
memcpy(empty_leaf.items + 2, &item, sizeof(item));
memcpy(empty_leaf.data + btrfs_item_offset(&item), &extent_item,
btrfs_item_size(&item));
memcpy(empty_leaf->items + 2, &item, sizeof(item));
memcpy(btrfs_leaf_data(empty_leaf) + btrfs_item_offset(&item),
&extent_item, btrfs_item_size(&item));
/* item4, give block 19 to the FS root */
btrfs_set_key_objectid(&item.key, 19);
btrfs_set_key_objectid(&item.key, start_block + 3);
btrfs_set_key_offset(&item.key, 1);
itemoff = itemoff - sizeof(struct btrfs_extent_item);
btrfs_set_item_offset(&item, itemoff);
btrfs_set_extent_owner(&extent_item, BTRFS_FS_TREE_OBJECTID);
memcpy(empty_leaf.items + 3, &item, sizeof(item));
memcpy(empty_leaf.data + btrfs_item_offset(&item), &extent_item,
btrfs_item_size(&item));
ret = pwrite(fd, &empty_leaf, blocksize, 18 * blocksize);
if (ret != sizeof(empty_leaf))
memcpy(empty_leaf->items + 3, &item, sizeof(item));
memcpy(btrfs_leaf_data(empty_leaf) + btrfs_item_offset(&item),
&extent_item, btrfs_item_size(&item));
ret = pwrite(fd, empty_leaf, blocksize, (start_block + 2) * blocksize);
if (ret != blocksize)
return -1;
/* finally create the FS root */
btrfs_set_header_parentid(&empty_leaf.header, BTRFS_FS_TREE_OBJECTID);
btrfs_set_header_blocknr(&empty_leaf.header, 19);
btrfs_set_header_nritems(&empty_leaf.header, 0);
ret = pwrite(fd, &empty_leaf, blocksize, 19 * blocksize);
if (ret != sizeof(empty_leaf))
btrfs_set_header_parentid(&empty_leaf->header, BTRFS_FS_TREE_OBJECTID);
btrfs_set_header_blocknr(&empty_leaf->header, start_block + 3);
btrfs_set_header_nritems(&empty_leaf->header, 0);
ret = pwrite(fd, empty_leaf, blocksize, (start_block + 3) * blocksize);
if (ret != blocksize)
return -1;
return 0;
}
#if 0
int mkfs(int fd)
{
struct btrfs_root_info info[2];
struct btrfs_leaf empty_leaf;
struct btrfs_item item;
struct btrfs_extent_item extent_item;
int ret;
/* setup the super block area */
memset(info, 0, sizeof(info));
btrfs_set_root_blocknr(info, 16);
btrfs_set_root_objectid(info, 1);
btrfs_set_root_tree_root(info, 17);
btrfs_set_root_blocknr(info + 1, 16);
btrfs_set_root_objectid(info + 1, 2);
btrfs_set_root_tree_root(info + 1, 18);
ret = pwrite(fd, info, sizeof(info),
BTRFS_SUPER_INFO_OFFSET(BTRFS_BLOCKSIZE));
if (ret != sizeof(info))
return -1;
/* create leaves for the tree root and extent root */
memset(&empty_leaf, 0, sizeof(empty_leaf));
btrfs_set_header_parentid(&empty_leaf.header, 1);
btrfs_set_header_blocknr(&empty_leaf.header, 17);
ret = pwrite(fd, &empty_leaf, sizeof(empty_leaf), 17 * BTRFS_BLOCKSIZE);
if (ret != sizeof(empty_leaf))
return -1;
btrfs_set_header_parentid(&empty_leaf.header, 2);
btrfs_set_header_blocknr(&empty_leaf.header, 18);
btrfs_set_header_nritems(&empty_leaf.header, 3);
/* item1, reserve blocks 0-16 */
btrfs_set_key_objectid(&item.key, 0);
btrfs_set_key_offset(&item.key, 17);
btrfs_set_key_flags(&item.key, 0);
btrfs_set_item_offset(&item, LEAF_DATA_SIZE -
sizeof(struct btrfs_extent_item));
btrfs_set_item_size(&item, sizeof(struct btrfs_extent_item));
btrfs_set_extent_refs(&extent_item, 1);
btrfs_set_extent_owner(&extent_item, 0);
memcpy(empty_leaf.items, &item, sizeof(item));
memcpy(empty_leaf.data + btrfs_item_offset(&item), &extent_item,
btrfs_item_size(&item));
/* item2, give block 17 to the root */
btrfs_set_key_objectid(&item.key, 17);
btrfs_set_key_offset(&item.key, 1);
btrfs_set_item_offset(&item, LEAF_DATA_SIZE -
sizeof(struct btrfs_extent_item) * 2);
btrfs_set_extent_owner(&extent_item, 1);
memcpy(empty_leaf.items + 1, &item, sizeof(item));
memcpy(empty_leaf.data + btrfs_item_offset(&item), &extent_item,
btrfs_item_size(&item));
/* item3, give block 18 for the extent root */
btrfs_set_key_objectid(&item.key, 18);
btrfs_set_key_offset(&item.key, 1);
btrfs_set_item_offset(&item, LEAF_DATA_SIZE -
sizeof(struct btrfs_extent_item) * 3);
btrfs_set_extent_owner(&extent_item, 2);
memcpy(empty_leaf.items + 2, &item, sizeof(item));
memcpy(empty_leaf.data + btrfs_item_offset(&item), &extent_item,
btrfs_item_size(&item));
ret = pwrite(fd, &empty_leaf, sizeof(empty_leaf), 18 * BTRFS_BLOCKSIZE);
if (ret != sizeof(empty_leaf))
return -1;
return 0;
}
#endif
......@@ -5,7 +5,7 @@
#include "ctree.h"
#include "disk-io.h"
void btrfs_print_leaf(struct btrfs_leaf *l)
void btrfs_print_leaf(struct btrfs_root *root, struct btrfs_leaf *l)
{
int i;
u32 nr = btrfs_header_nritems(&l->header);
......@@ -13,7 +13,8 @@ void btrfs_print_leaf(struct btrfs_leaf *l)
struct btrfs_extent_item *ei;
struct btrfs_root_item *ri;
printf("leaf %Lu total ptrs %d free space %d\n",
btrfs_header_blocknr(&l->header), nr, btrfs_leaf_free_space(l));
btrfs_header_blocknr(&l->header), nr,
btrfs_leaf_free_space(root, l));
fflush(stdout);
for (i = 0 ; i < nr ; i++) {
item = l->items + i;
......@@ -25,7 +26,7 @@ void btrfs_print_leaf(struct btrfs_leaf *l)
btrfs_item_offset(item),
btrfs_item_size(item));
printf("\t\titem data %.*s\n", btrfs_item_size(item),
l->data + btrfs_item_offset(item));
btrfs_leaf_data(l) + btrfs_item_offset(item));
ei = btrfs_item_ptr(l, i, struct btrfs_extent_item);
printf("\t\textent data refs %u owner %Lu\n",
btrfs_extent_refs(ei), btrfs_extent_owner(ei));
......@@ -46,18 +47,18 @@ void btrfs_print_tree(struct btrfs_root *root, struct btrfs_buffer *t)
c = &t->node;
nr = btrfs_header_nritems(&c->header);
if (btrfs_is_leaf(c)) {
btrfs_print_leaf((struct btrfs_leaf *)c);
btrfs_print_leaf(root, (struct btrfs_leaf *)c);
return;
}
printf("node %Lu level %d total ptrs %d free spc %u\n", t->blocknr,
btrfs_header_level(&c->header), nr,
(u32)NODEPTRS_PER_BLOCK - nr);
(u32)BTRFS_NODEPTRS_PER_BLOCK(root) - nr);
fflush(stdout);
for (i = 0; i < nr; i++) {
printf("\tkey %d (%Lu %u %Lu) block %Lu\n",
i,
c->keys[i].objectid, c->keys[i].flags, c->keys[i].offset,
btrfs_node_blockptr(c, i));
c->ptrs[i].key.objectid, c->ptrs[i].key.flags,
c->ptrs[i].key.offset, btrfs_node_blockptr(c, i));
fflush(stdout);
}
for (i = 0; i < nr; i++) {
......
void btrfs_print_leaf(struct btrfs_leaf *l);
void btrfs_print_leaf(struct btrfs_root *root, struct btrfs_leaf *l);
void btrfs_print_tree(struct btrfs_root *root, struct btrfs_buffer *t);
......@@ -73,7 +73,7 @@ int main(int ac, char **av) {
printf("node %p level %d total ptrs %d free spc %lu\n", root->node,
btrfs_header_level(&root->node->node.header),
btrfs_header_nritems(&root->node->node.header),
NODEPTRS_PER_BLOCK -
BTRFS_NODEPTRS_PER_BLOCK(root) -
btrfs_header_nritems(&root->node->node.header));
printf("all searches good, deleting some items\n");
i = 0;
......
......@@ -31,7 +31,7 @@ int btrfs_find_last_root(struct btrfs_root *root, u64 objectid,
ret = 1;
goto out;
}
memcpy(item, l->data + btrfs_item_offset(l->items + slot),
memcpy(item, btrfs_item_ptr(l, slot, struct btrfs_root_item),
sizeof(*item));
btrfs_disk_key_to_cpu(key, &l->items[slot].key);
btrfs_release_path(root, &path);
......@@ -55,7 +55,7 @@ int btrfs_update_root(struct btrfs_root *root, struct btrfs_key *key,
BUG_ON(ret != 0);
l = &path.nodes[0]->leaf;
slot = path.slots[0];
memcpy(l->data + btrfs_item_offset(l->items + slot), item,
memcpy(btrfs_item_ptr(l, slot, struct btrfs_root_item), item,
sizeof(*item));
out:
btrfs_release_path(root, &path);
......
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