Skip to content

Commit

Permalink
---
Browse files Browse the repository at this point in the history
yaml
---
r: 128239
b: refs/heads/master
c: d97e63b
h: refs/heads/master
i:
  128237: b1e9587
  128235: 5be3e35
  128231: 0ff5bcf
  128223: 993804e
v: v3
  • Loading branch information
Chris Mason authored and David Woodhouse committed Feb 20, 2007
1 parent 562a5ec commit e9a39c5
Show file tree
Hide file tree
Showing 6 changed files with 237 additions and 34 deletions.
2 changes: 1 addition & 1 deletion [refs]
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
---
refs/heads/master: 74123bd72a4e34c66e5ad95ce233dd352bebc572
refs/heads/master: d97e63b69ef21c02b67e20e41d9968b0e503572e
4 changes: 2 additions & 2 deletions trunk/fs/btrfs/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ CFLAGS= -g -Wall
.c.o:
$(CC) $(CFLAGS) -c $<

ctree: ctree.o disk-io.h ctree.h disk-io.o radix-tree.o radix-tree.h
gcc $(CFLAGS) -o ctree ctree.o disk-io.o radix-tree.o
ctree: ctree.o disk-io.h ctree.h disk-io.o radix-tree.o radix-tree.h mkfs.o
gcc $(CFLAGS) -o ctree ctree.o disk-io.o radix-tree.o mkfs.o

clean:
rm ctree *.o
Expand Down
152 changes: 147 additions & 5 deletions trunk/fs/btrfs/ctree.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "ctree.h"
#include "disk-io.h"

static int refill_alloc_extent(struct ctree_root *root);

static inline void init_path(struct ctree_path *p)
{
memset(p, 0, sizeof(*p));
Expand All @@ -29,7 +31,7 @@ static inline unsigned int leaf_data_end(struct leaf *leaf)
{
unsigned int nr = leaf->header.nritems;
if (nr == 0)
return ARRAY_SIZE(leaf->data);
return sizeof(leaf->data);
return leaf->items[nr-1].offset;
}

Expand Down Expand Up @@ -421,7 +423,7 @@ int insert_ptr(struct ctree_root *root,
* due to splitting. Once we've done all the splitting required
* do the inserts based on the data in the bal array.
*/
memset(bal, 0, ARRAY_SIZE(bal));
memset(bal, 0, sizeof(bal));
while(t && t->node.header.nritems == NODEPTRS_PER_BLOCK) {
c = &t->node;
if (push_node_left(root, path,
Expand Down Expand Up @@ -756,6 +758,7 @@ int insert_item(struct ctree_root *root, struct key *key,
if (leaf_free_space(leaf) < 0)
BUG();
release_path(root, &path);
refill_alloc_extent(root);
return 0;
}

Expand Down Expand Up @@ -884,6 +887,135 @@ int del_item(struct ctree_root *root, struct ctree_path *path)
return 0;
}

int next_leaf(struct ctree_root *root, struct ctree_path *path)
{
int slot;
int level = 1;
u64 blocknr;
struct tree_buffer *c;
struct tree_buffer *next;

while(level < MAX_LEVEL) {
if (!path->nodes[level])
return -1;
slot = path->slots[level] + 1;
c = path->nodes[level];
if (slot >= c->node.header.nritems) {
level++;
continue;
}
blocknr = c->node.blockptrs[slot];
next = read_tree_block(root, blocknr);
break;
}
path->slots[level] = slot;
while(1) {
level--;
c = path->nodes[level];
tree_block_release(root, c);
path->nodes[level] = next;
path->slots[level] = 0;
if (!level)
break;
next = read_tree_block(root, next->node.blockptrs[0]);
}
return 0;
}

int alloc_extent(struct ctree_root *root, u64 num_blocks, u64 search_start,
u64 search_end, u64 owner, struct key *ins)
{
struct ctree_path path;
struct key *key;
int ret;
u64 hole_size = 0;
int slot = 0;
u64 last_block;
int start_found = 0;
struct leaf *l;
struct extent_item extent_item;

init_path(&path);
ins->objectid = search_start;
ins->offset = 0;
ins->flags = 0;

ret = search_slot(root, ins, &path);
while (1) {
l = &path.nodes[0]->leaf;
slot = path.slots[0];
if (!l) {
// FIXME allocate root
}
if (slot >= l->header.nritems) {
ret = next_leaf(root, &path);
if (ret == 0)
continue;
if (!start_found) {
ins->objectid = search_start;
ins->offset = num_blocks;
hole_size = search_end - search_start;
goto insert;
}
ins->objectid = last_block;
ins->offset = num_blocks;
hole_size = search_end - last_block;
goto insert;
}
key = &l->items[slot].key;
if (start_found) {
hole_size = key->objectid - last_block;
if (hole_size > num_blocks) {
ins->objectid = last_block;
ins->offset = num_blocks;
goto insert;
}
} else
start_found = 1;
last_block = key->objectid + key->offset;
path.slots[0]++;
printf("last block is not %lu\n", last_block);
}
// FIXME -ENOSPC
insert:
extent_item.refs = 1;
extent_item.owner = owner;
ret = insert_item(root, ins, &extent_item, sizeof(extent_item));
return ret;
}

static int refill_alloc_extent(struct ctree_root *root)
{
struct alloc_extent *ae = root->alloc_extent;
struct key key;
int ret;
int min_blocks = MAX_LEVEL * 2;

printf("refill alloc root %p, numused %lu total %lu\n", root, ae->num_used, ae->num_blocks);
if (ae->num_blocks > ae->num_used && ae->num_blocks - ae->num_used >
min_blocks)
return 0;
ae = root->reserve_extent;
if (ae->num_blocks > ae->num_used) {
if (root->alloc_extent->num_blocks == 0) {
/* we should swap reserve/alloc_extent when alloc
* fills up
*/
BUG();
}
if (ae->num_blocks - ae->num_used < min_blocks)
BUG();
return 0;
}
// FIXME, this recurses
ret = alloc_extent(root->extent_root,
min_blocks * 2, 0, (unsigned long)-1, 0, &key);
ae->blocknr = key.objectid;
ae->num_blocks = key.offset;
ae->num_used = 0;
return ret;
}

void print_leaf(struct leaf *l)
{
int i;
Expand Down Expand Up @@ -948,8 +1080,8 @@ void print_tree(struct ctree_root *root, struct tree_buffer *t)

/* for testing only */
int next_key(int i, int max_key) {
return rand() % max_key;
// return i;
// return rand() % max_key;
return i;
}

int main() {
Expand All @@ -960,7 +1092,7 @@ int main() {
int i;
int num;
int ret;
int run_size = 25000;
int run_size = 256;
int max_key = 100000000;
int tree_size = 0;
struct ctree_path path;
Expand All @@ -980,10 +1112,20 @@ int main() {
ins.objectid = num;
ins.offset = 0;
ins.flags = 0;
printf("insert %d\n", i);
ret = insert_item(root, &ins, buf, strlen(buf));
if (!ret)
tree_size++;
printf("done insert %d\n", i);
}
printf("root used: %lu\n", root->alloc_extent->num_used);
printf("root tree\n");
print_tree(root, root->node);
printf("map tree\n");
printf("map used: %lu\n", root->extent_root->alloc_extent->num_used);
print_tree(root->extent_root, root->extent_root->node);
exit(1);

close_ctree(root);
root = open_ctree("dbfile");
printf("starting search\n");
Expand Down
34 changes: 31 additions & 3 deletions trunk/fs/btrfs/ctree.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef __CTREE__
#define __CTREE__

#define CTREE_BLOCKSIZE 4096
#define CTREE_BLOCKSIZE 256

struct key {
u64 objectid;
Expand All @@ -22,18 +22,41 @@ struct header {
#define NODEPTRS_PER_BLOCK ((CTREE_BLOCKSIZE - sizeof(struct header)) / \
(sizeof(struct key) + sizeof(u64)))

#define LEVEL_BITS 3
#define MAX_LEVEL (1 << LEVEL_BITS)
#define MAX_LEVEL 8
#define node_level(f) ((f) & (MAX_LEVEL-1))
#define is_leaf(f) (node_level(f) == 0)

struct tree_buffer;

struct alloc_extent {
u64 blocknr;
u64 num_blocks;
u64 num_used;
} __attribute__ ((__packed__));

struct ctree_root {
struct tree_buffer *node;
struct ctree_root *extent_root;
struct alloc_extent *alloc_extent;
struct alloc_extent *reserve_extent;
int fp;
struct radix_tree_root cache_radix;
struct alloc_extent ai1;
struct alloc_extent ai2;
};

struct ctree_root_info {
u64 fsid[2]; /* FS specific uuid */
u64 blocknr; /* blocknr of this block */
u64 objectid; /* inode number of this root */
u64 tree_root; /* the tree root */
u32 csum;
u32 ham;
struct alloc_extent alloc_extent;
struct alloc_extent reserve_extent;
u64 snapuuid[2]; /* root specific uuid */
} __attribute__ ((__packed__));

struct item {
struct key key;
u16 offset;
Expand All @@ -55,6 +78,11 @@ struct node {
u64 blockptrs[NODEPTRS_PER_BLOCK];
} __attribute__ ((__packed__));

struct extent_item {
u32 refs;
u64 owner;
} __attribute__ ((__packed__));

struct ctree_path {
struct tree_buffer *nodes[MAX_LEVEL];
int slots[MAX_LEVEL];
Expand Down
Loading

0 comments on commit e9a39c5

Please sign in to comment.