Skip to content

Commit

Permalink
Btrfs: Change the super to point to a tree of trees to enable persist…
Browse files Browse the repository at this point in the history
…ent snapshots

Signed-off-by: Chris Mason <chris.mason@oracle.com>
  • Loading branch information
Chris Mason authored and David Woodhouse committed Mar 13, 2007
1 parent eaee50e commit 3768f36
Show file tree
Hide file tree
Showing 10 changed files with 445 additions and 65 deletions.
3 changes: 2 additions & 1 deletion fs/btrfs/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
CC=gcc
CFLAGS = -g -Wall
headers = radix-tree.h ctree.h disk-io.h kerncompat.h print-tree.h list.h
objects = ctree.o disk-io.o radix-tree.o mkfs.o extent-tree.o print-tree.o
objects = ctree.o disk-io.o radix-tree.o mkfs.o extent-tree.o print-tree.o \
root-tree.o

# if you don't have sparse installed, use ls instead
CHECKFLAGS=-D__linux__ -Dlinux -D__STDC__ -Dunix -D__unix__ -Wbitwise \
Expand Down
1 change: 1 addition & 0 deletions fs/btrfs/TODO
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* make a real mkfs and superblock
* Do checksumming
* Define FS objects in terms of different item types
* add inode tree
* Add block mapping tree (simple dm layer)
* Add simple tree locking (semaphore per tree)
* Make allocator smarter
Expand Down
123 changes: 107 additions & 16 deletions fs/btrfs/ctree.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@
#include "list.h"
#include "kerncompat.h"

#define BTRFS_MAGIC "_BtRfS_M"
#define BTRFS_BLOCKSIZE 1024

#define BTRFS_ROOT_TREE_OBJECTID 1
#define BTRFS_EXTENT_TREE_OBJECTID 2
#define BTRFS_FS_TREE_OBJECTID 3

/*
* the key defines the order in the tree, and so it also defines (optimal)
* block layout. objectid corresonds to the inode number. The flags
Expand Down Expand Up @@ -36,7 +41,7 @@ struct btrfs_key {
* every tree block (leaf or node) starts with this header.
*/
struct btrfs_header {
__le64 fsid[2]; /* FS specific uuid */
u8 fsid[16]; /* FS specific uuid */
__le64 blocknr; /* which block this node is supposed to live in */
__le64 parentid; /* objectid of the tree root */
__le32 csum;
Expand All @@ -52,6 +57,14 @@ struct btrfs_header {

struct btrfs_buffer;

struct btrfs_root_item {
__le64 blocknr;
__le32 flags;
__le64 block_limit;
__le64 blocks_used;
__le32 refs;
};

/*
* in ram representation of the tree. extent_root is used for all allocations
* and for the extent tree extent_root root. current_insert is used
Expand All @@ -61,6 +74,7 @@ struct btrfs_root {
struct btrfs_buffer *node;
struct btrfs_buffer *commit_root;
struct btrfs_root *extent_root;
struct btrfs_root *tree_root;
struct btrfs_key current_insert;
struct btrfs_key last_insert;
int fp;
Expand All @@ -69,28 +83,25 @@ struct btrfs_root {
struct list_head trans;
struct list_head cache;
int cache_size;
int ref_cows;
struct btrfs_root_item root_item;
struct btrfs_key root_key;
};

/*
* describes a tree on disk
*/
struct btrfs_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 block */
u32 csum;
u32 ham;
u64 snapuuid[2]; /* root specific uuid */
} __attribute__ ((__packed__));

/*
* the super block basically lists the main trees of the FS
* it currently lacks any block count etc etc
*/
struct btrfs_super_block {
struct btrfs_root_info root_info;
struct btrfs_root_info extent_info;
u8 fsid[16]; /* FS specific uuid */
__le64 blocknr; /* this block number */
__le32 csum;
__le64 magic;
__le16 blocksize;
__le64 generation;
__le64 root;
__le64 total_blocks;
__le64 blocks_used;
} __attribute__ ((__packed__));

/*
Expand Down Expand Up @@ -317,6 +328,79 @@ static inline int btrfs_is_leaf(struct btrfs_node *n)
return (btrfs_header_level(&n->header) == 0);
}

static inline u64 btrfs_root_blocknr(struct btrfs_root_item *item)
{
return le64_to_cpu(item->blocknr);
}

static inline void btrfs_set_root_blocknr(struct btrfs_root_item *item, u64 val)
{
item->blocknr = cpu_to_le64(val);
}

static inline u32 btrfs_root_refs(struct btrfs_root_item *item)
{
return le32_to_cpu(item->refs);
}

static inline void btrfs_set_root_refs(struct btrfs_root_item *item, u32 val)
{
item->refs = cpu_to_le32(val);
}

static inline u64 btrfs_super_blocknr(struct btrfs_super_block *s)
{
return le64_to_cpu(s->blocknr);
}

static inline void btrfs_set_super_blocknr(struct btrfs_super_block *s, u64 val)
{
s->blocknr = cpu_to_le64(val);
}

static inline u64 btrfs_super_root(struct btrfs_super_block *s)
{
return le64_to_cpu(s->root);
}

static inline void btrfs_set_super_root(struct btrfs_super_block *s, u64 val)
{
s->root = cpu_to_le64(val);
}

static inline u64 btrfs_super_total_blocks(struct btrfs_super_block *s)
{
return le64_to_cpu(s->total_blocks);
}

static inline void btrfs_set_super_total_blocks(struct btrfs_super_block *s,
u64 val)
{
s->total_blocks = cpu_to_le64(val);
}

static inline u64 btrfs_super_blocks_used(struct btrfs_super_block *s)
{
return le64_to_cpu(s->blocks_used);
}

static inline void btrfs_set_super_blocks_used(struct btrfs_super_block *s,
u64 val)
{
s->blocks_used = cpu_to_le64(val);
}

static inline u16 btrfs_super_blocksize(struct btrfs_super_block *s)
{
return le16_to_cpu(s->blocksize);
}

static inline void btrfs_set_super_blocksize(struct btrfs_super_block *s,
u16 val)
{
s->blocksize = cpu_to_le16(val);
}

struct btrfs_buffer *btrfs_alloc_free_block(struct btrfs_root *root);
int btrfs_inc_ref(struct btrfs_root *root, struct btrfs_buffer *buf);
int btrfs_free_extent(struct btrfs_root *root, u64 blocknr, u64 num_blocks);
Expand All @@ -331,4 +415,11 @@ int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path);
int btrfs_leaf_free_space(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);
int btrfs_insert_root(struct btrfs_root *root, struct btrfs_key *key,
struct btrfs_root_item *item);
int btrfs_update_root(struct btrfs_root *root, struct btrfs_key *key,
struct btrfs_root_item *item);
int btrfs_find_last_root(struct btrfs_root *root, u64 objectid,
struct btrfs_root_item *item, struct btrfs_key *key);
#endif
4 changes: 3 additions & 1 deletion fs/btrfs/debug-tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ int main(int ac, char **av) {
struct btrfs_root *root;
radix_tree_init();
root = open_ctree("dbfile", &super);
printf("root tree\n");
printf("fs tree\n");
btrfs_print_tree(root, root->node);
printf("map tree\n");
btrfs_print_tree(root->extent_root, root->extent_root->node);
printf("root tree\n");
btrfs_print_tree(root->tree_root, root->tree_root->node);
return 0;
}
Loading

0 comments on commit 3768f36

Please sign in to comment.