提交 7518a238 编写于 作者: C Chris Mason 提交者: David Woodhouse

Btrfs: get/set for struct header fields

Signed-off-by: NChris Mason <chris.mason@oracle.com>
上级 83e15a28
......@@ -25,7 +25,7 @@ tester: $(objects) random-test.o
quick-test: $(objects) quick-test.o
gcc $(CFLAGS) -o quick-test $(objects) quick-test.o
$(objects) : $(headers)
$(objects): $(headers)
clean :
rm debug-tree tester *.o
......
* cleanup, add more error checking, get rid of BUG_ONs
* endian fixes
* Make IO functions look more like the page cache
* Fix ENOSPC handling
* make blocksize a mkfs parameter instead of #define
......
此差异已折叠。
......@@ -35,13 +35,10 @@ struct header {
/* generation flags to be added */
} __attribute__ ((__packed__));
#define MAX_LEVEL 8
#define NODEPTRS_PER_BLOCK ((CTREE_BLOCKSIZE - sizeof(struct header)) / \
(sizeof(struct key) + sizeof(u64)))
#define MAX_LEVEL 8
#define node_level(f) ((f) & (MAX_LEVEL-1))
#define is_leaf(f) (node_level(f) == 0)
struct tree_buffer;
/*
......@@ -144,6 +141,64 @@ struct ctree_path {
int slots[MAX_LEVEL];
};
static inline u64 btrfs_header_blocknr(struct header *h)
{
return h->blocknr;
}
static inline void btrfs_set_header_blocknr(struct header *h, u64 blocknr)
{
h->blocknr = blocknr;
}
static inline u64 btrfs_header_parentid(struct header *h)
{
return h->parentid;
}
static inline void btrfs_set_header_parentid(struct header *h, u64 parentid)
{
h->parentid = parentid;
}
static inline u32 btrfs_header_nritems(struct header *h)
{
return h->nritems;
}
static inline void btrfs_set_header_nritems(struct header *h, u32 val)
{
h->nritems = val;
}
static inline u32 btrfs_header_flags(struct header *h)
{
return h->flags;
}
static inline void btrfs_set_header_flags(struct header *h, u32 val)
{
h->flags = val;
}
static inline int btrfs_header_level(struct header *h)
{
return btrfs_header_flags(h) & (MAX_LEVEL - 1);
}
static inline void btrfs_set_header_level(struct header *h, int level)
{
u32 flags;
BUG_ON(level > MAX_LEVEL);
flags = btrfs_header_flags(h) & ~(MAX_LEVEL - 1);
btrfs_set_header_flags(h, flags | level);
}
static inline int btrfs_is_leaf(struct node *n)
{
return (btrfs_header_level(&n->header) == 0);
}
struct tree_buffer *alloc_free_block(struct ctree_root *root);
int btrfs_inc_ref(struct ctree_root *root, struct tree_buffer *buf);
int free_extent(struct ctree_root *root, u64 blocknr, u64 num_blocks);
......
......@@ -15,9 +15,10 @@ int cache_max = 10000;
static int check_tree_block(struct ctree_root *root, struct tree_buffer *buf)
{
if (buf->blocknr != buf->node.header.blocknr)
if (buf->blocknr != btrfs_header_blocknr(&buf->node.header))
BUG();
if (root->node && buf->node.header.parentid != root->node->node.header.parentid)
if (root->node && btrfs_header_parentid(&buf->node.header) !=
btrfs_header_parentid(&root->node->node.header))
BUG();
return 0;
}
......@@ -129,7 +130,7 @@ int write_tree_block(struct ctree_root *root, struct tree_buffer *buf)
loff_t offset = blocknr * CTREE_BLOCKSIZE;
int ret;
if (buf->blocknr != buf->node.header.blocknr)
if (buf->blocknr != btrfs_header_blocknr(&buf->node.header))
BUG();
ret = pwrite(root->fp, &buf->node, CTREE_BLOCKSIZE, offset);
if (ret != CTREE_BLOCKSIZE)
......
......@@ -79,10 +79,10 @@ int btrfs_inc_ref(struct ctree_root *root, struct tree_buffer *buf)
if (root == root->extent_root)
return 0;
if (is_leaf(buf->node.header.flags))
if (btrfs_is_leaf(&buf->node))
return 0;
for (i = 0; i < buf->node.header.nritems; i++) {
for (i = 0; i < btrfs_header_nritems(&buf->node.header); i++) {
blocknr = buf->node.blockptrs[i];
inc_block_ref(root, blocknr);
}
......@@ -119,7 +119,8 @@ static int finish_current_insert(struct ctree_root *extent_root)
int ret;
extent_item.refs = 1;
extent_item.owner = extent_root->node->node.header.parentid;
extent_item.owner =
btrfs_header_parentid(&extent_root->node->node.header);
ins.offset = 1;
ins.flags = 0;
......@@ -269,7 +270,7 @@ static int find_free_extent(struct ctree_root *orig_root, u64 num_blocks,
struct ctree_root * root = orig_root->extent_root;
int total_needed = num_blocks;
total_needed += (node_level(root->node->node.header.flags) + 1) * 3;
total_needed += (btrfs_header_level(&root->node->node.header) + 1) * 3;
if (root->last_insert.objectid > search_start)
search_start = root->last_insert.objectid;
check_failed:
......@@ -288,7 +289,7 @@ static int find_free_extent(struct ctree_root *orig_root, u64 num_blocks,
while (1) {
l = &path.nodes[0]->leaf;
slot = path.slots[0];
if (slot >= l->header.nritems) {
if (slot >= btrfs_header_nritems(&l->header)) {
ret = next_leaf(root, &path);
if (ret == 0)
continue;
......@@ -404,7 +405,7 @@ struct tree_buffer *alloc_free_block(struct ctree_root *root)
struct tree_buffer *buf;
ret = alloc_extent(root, 1, 0, (unsigned long)-1,
root->node->node.header.parentid,
btrfs_header_parentid(&root->node->node.header),
&ins);
if (ret) {
BUG();
......@@ -429,7 +430,8 @@ int walk_down_tree(struct ctree_root *root, struct ctree_path *path, int *level)
goto out;
while(*level > 0) {
cur = path->nodes[*level];
if (path->slots[*level] >= cur->node.header.nritems)
if (path->slots[*level] >=
btrfs_header_nritems(&cur->node.header))
break;
blocknr = cur->node.blockptrs[path->slots[*level]];
ret = lookup_block_ref(root, blocknr, &refs);
......@@ -444,7 +446,7 @@ int walk_down_tree(struct ctree_root *root, struct ctree_path *path, int *level)
if (path->nodes[*level-1])
tree_block_release(root, path->nodes[*level-1]);
path->nodes[*level-1] = next;
*level = node_level(next->node.header.flags);
*level = btrfs_header_level(&next->node.header);
path->slots[*level] = 0;
}
out:
......@@ -463,7 +465,8 @@ int walk_up_tree(struct ctree_root *root, struct ctree_path *path, int *level)
int ret;
for(i = *level; i < MAX_LEVEL - 1 && path->nodes[i]; i++) {
slot = path->slots[i];
if (slot < path->nodes[i]->node.header.nritems - 1) {
if (slot <
btrfs_header_nritems(&path->nodes[i]->node.header)- 1) {
path->slots[i]++;
*level = i;
return 0;
......@@ -489,7 +492,7 @@ int btrfs_drop_snapshot(struct ctree_root *root, struct tree_buffer *snap)
init_path(&path);
level = node_level(snap->node.header.flags);
level = btrfs_header_level(&snap->node.header);
orig_level = level;
path.nodes[level] = snap;
path.slots[level] = 0;
......@@ -509,33 +512,3 @@ int btrfs_drop_snapshot(struct ctree_root *root, struct tree_buffer *snap)
return 0;
}
#if 0
int btrfs_drop_snapshot(struct ctree_root *root, struct tree_buffer *snap)
{
int ret;
int level;
int refs;
u64 blocknr = snap->blocknr;
level = node_level(snap->node.header.flags);
ret = lookup_block_ref(root, snap->blocknr, &refs);
BUG_ON(ret);
if (refs == 1 && level != 0) {
struct node *n = &snap->node;
struct tree_buffer *b;
int i;
for (i = 0; i < n->header.nritems; i++) {
b = read_tree_block(root, n->blockptrs[i]);
/* FIXME, don't recurse here */
ret = btrfs_drop_snapshot(root, b);
BUG_ON(ret);
tree_block_release(root, b);
}
}
ret = free_extent(root, blocknr, 1);
BUG_ON(ret);
return 0;
}
#endif
......@@ -34,15 +34,15 @@ int mkfs(int fd)
/* create leaves for the tree root and extent root */
memset(&empty_leaf, 0, sizeof(empty_leaf));
empty_leaf.header.parentid = 1;
empty_leaf.header.blocknr = 17;
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 * CTREE_BLOCKSIZE);
if (ret != sizeof(empty_leaf))
return -1;
empty_leaf.header.parentid = 2;
empty_leaf.header.blocknr = 18;
empty_leaf.header.nritems = 3;
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 */
item.key.objectid = 0;
......
......@@ -8,11 +8,11 @@
void print_leaf(struct leaf *l)
{
int i;
int nr = l->header.nritems;
u32 nr = btrfs_header_nritems(&l->header);
struct item *item;
struct extent_item *ei;
printf("leaf %Lu total ptrs %d free space %d\n", l->header.blocknr, nr,
leaf_free_space(l));
printf("leaf %Lu total ptrs %d free space %d\n",
btrfs_header_blocknr(&l->header), nr, leaf_free_space(l));
fflush(stdout);
for (i = 0 ; i < nr ; i++) {
item = l->items + i;
......@@ -32,22 +32,20 @@ void print_leaf(struct leaf *l)
void print_tree(struct ctree_root *root, struct tree_buffer *t)
{
int i;
int nr;
u32 nr;
struct node *c;
if (!t)
return;
c = &t->node;
nr = c->header.nritems;
if (c->header.blocknr != t->blocknr)
BUG();
if (is_leaf(c->header.flags)) {
nr = btrfs_header_nritems(&c->header);
if (btrfs_is_leaf(c)) {
print_leaf((struct leaf *)c);
return;
}
printf("node %Lu level %d total ptrs %d free spc %u\n", t->blocknr,
node_level(c->header.flags), c->header.nritems,
(u32)NODEPTRS_PER_BLOCK - c->header.nritems);
btrfs_header_level(&c->header), nr,
(u32)NODEPTRS_PER_BLOCK - nr);
fflush(stdout);
for (i = 0; i < nr; i++) {
printf("\tkey %d (%Lu %u %Lu) block %Lu\n",
......@@ -60,11 +58,11 @@ void print_tree(struct ctree_root *root, struct tree_buffer *t)
struct tree_buffer *next_buf = read_tree_block(root,
c->blockptrs[i]);
struct node *next = &next_buf->node;
if (is_leaf(next->header.flags) &&
node_level(c->header.flags) != 1)
if (btrfs_is_leaf(next) &&
btrfs_header_level(&c->header) != 1)
BUG();
if (node_level(next->header.flags) !=
node_level(c->header.flags) - 1)
if (btrfs_header_level(&next->header) !=
btrfs_header_level(&c->header) - 1)
BUG();
print_tree(root, next_buf);
tree_block_release(root, next_buf);
......
......@@ -71,9 +71,10 @@ int main(int ac, char **av) {
close_ctree(root, &super);
root = open_ctree("dbfile", &super);
printf("node %p level %d total ptrs %d free spc %lu\n", root->node,
node_level(root->node->node.header.flags),
root->node->node.header.nritems,
NODEPTRS_PER_BLOCK - root->node->node.header.nritems);
btrfs_header_level(&root->node->node.header),
btrfs_header_nritems(&root->node->node.header),
NODEPTRS_PER_BLOCK -
btrfs_header_nritems(&root->node->node.header));
printf("all searches good, deleting some items\n");
i = 0;
srand(55);
......@@ -126,7 +127,8 @@ int main(int ac, char **av) {
release_path(root, &path);
}
printf("starting big long delete run\n");
while(root->node && root->node->node.header.nritems > 0) {
while(root->node &&
btrfs_header_nritems(&root->node->node.header) > 0) {
struct leaf *leaf;
int slot;
ins.objectid = (u64)-1;
......@@ -137,7 +139,7 @@ int main(int ac, char **av) {
leaf = &path.nodes[0]->leaf;
slot = path.slots[0];
if (slot != leaf->header.nritems)
if (slot != btrfs_header_nritems(&leaf->header))
BUG();
while(path.slots[0] > 0) {
path.slots[0] -= 1;
......
......@@ -358,8 +358,8 @@ int main(int ac, char **av)
}
if (i && i % 5000 == 0) {
printf("open & close, root level %d nritems %d\n",
node_level(root->node->node.header.flags),
root->node->node.header.nritems);
btrfs_header_level(&root->node->node.header),
btrfs_header_nritems(&root->node->node.header));
close_ctree(root, &super);
root = open_ctree("dbfile", &super);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册