Skip to content

Commit d9c9960

Browse files
chrismason-xxDavid Woodhouse
authored and
David Woodhouse
committed
Change the super to point to a tree of trees to enable persistent snapshots
1 parent 6a332a7 commit d9c9960

10 files changed

+445
-65
lines changed

Makefile

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
CC=gcc
33
CFLAGS = -g -Wall
44
headers = radix-tree.h ctree.h disk-io.h kerncompat.h print-tree.h list.h
5-
objects = ctree.o disk-io.o radix-tree.o mkfs.o extent-tree.o print-tree.o
5+
objects = ctree.o disk-io.o radix-tree.o mkfs.o extent-tree.o print-tree.o \
6+
root-tree.o
67

78
# if you don't have sparse installed, use ls instead
89
CHECKFLAGS=-D__linux__ -Dlinux -D__STDC__ -Dunix -D__unix__ -Wbitwise \

TODO

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* make a real mkfs and superblock
66
* Do checksumming
77
* Define FS objects in terms of different item types
8+
* add inode tree
89
* Add block mapping tree (simple dm layer)
910
* Add simple tree locking (semaphore per tree)
1011
* Make allocator smarter

ctree.h

+107-16
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,13 @@
44
#include "list.h"
55
#include "kerncompat.h"
66

7+
#define BTRFS_MAGIC "_BtRfS_M"
78
#define BTRFS_BLOCKSIZE 1024
89

10+
#define BTRFS_ROOT_TREE_OBJECTID 1
11+
#define BTRFS_EXTENT_TREE_OBJECTID 2
12+
#define BTRFS_FS_TREE_OBJECTID 3
13+
914
/*
1015
* the key defines the order in the tree, and so it also defines (optimal)
1116
* block layout. objectid corresonds to the inode number. The flags
@@ -36,7 +41,7 @@ struct btrfs_key {
3641
* every tree block (leaf or node) starts with this header.
3742
*/
3843
struct btrfs_header {
39-
__le64 fsid[2]; /* FS specific uuid */
44+
u8 fsid[16]; /* FS specific uuid */
4045
__le64 blocknr; /* which block this node is supposed to live in */
4146
__le64 parentid; /* objectid of the tree root */
4247
__le32 csum;
@@ -52,6 +57,14 @@ struct btrfs_header {
5257

5358
struct btrfs_buffer;
5459

60+
struct btrfs_root_item {
61+
__le64 blocknr;
62+
__le32 flags;
63+
__le64 block_limit;
64+
__le64 blocks_used;
65+
__le32 refs;
66+
};
67+
5568
/*
5669
* in ram representation of the tree. extent_root is used for all allocations
5770
* and for the extent tree extent_root root. current_insert is used
@@ -61,6 +74,7 @@ struct btrfs_root {
6174
struct btrfs_buffer *node;
6275
struct btrfs_buffer *commit_root;
6376
struct btrfs_root *extent_root;
77+
struct btrfs_root *tree_root;
6478
struct btrfs_key current_insert;
6579
struct btrfs_key last_insert;
6680
int fp;
@@ -69,28 +83,25 @@ struct btrfs_root {
6983
struct list_head trans;
7084
struct list_head cache;
7185
int cache_size;
86+
int ref_cows;
87+
struct btrfs_root_item root_item;
88+
struct btrfs_key root_key;
7289
};
7390

74-
/*
75-
* describes a tree on disk
76-
*/
77-
struct btrfs_root_info {
78-
u64 fsid[2]; /* FS specific uuid */
79-
u64 blocknr; /* blocknr of this block */
80-
u64 objectid; /* inode number of this root */
81-
u64 tree_root; /* the tree root block */
82-
u32 csum;
83-
u32 ham;
84-
u64 snapuuid[2]; /* root specific uuid */
85-
} __attribute__ ((__packed__));
86-
8791
/*
8892
* the super block basically lists the main trees of the FS
8993
* it currently lacks any block count etc etc
9094
*/
9195
struct btrfs_super_block {
92-
struct btrfs_root_info root_info;
93-
struct btrfs_root_info extent_info;
96+
u8 fsid[16]; /* FS specific uuid */
97+
__le64 blocknr; /* this block number */
98+
__le32 csum;
99+
__le64 magic;
100+
__le16 blocksize;
101+
__le64 generation;
102+
__le64 root;
103+
__le64 total_blocks;
104+
__le64 blocks_used;
94105
} __attribute__ ((__packed__));
95106

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

331+
static inline u64 btrfs_root_blocknr(struct btrfs_root_item *item)
332+
{
333+
return le64_to_cpu(item->blocknr);
334+
}
335+
336+
static inline void btrfs_set_root_blocknr(struct btrfs_root_item *item, u64 val)
337+
{
338+
item->blocknr = cpu_to_le64(val);
339+
}
340+
341+
static inline u32 btrfs_root_refs(struct btrfs_root_item *item)
342+
{
343+
return le32_to_cpu(item->refs);
344+
}
345+
346+
static inline void btrfs_set_root_refs(struct btrfs_root_item *item, u32 val)
347+
{
348+
item->refs = cpu_to_le32(val);
349+
}
350+
351+
static inline u64 btrfs_super_blocknr(struct btrfs_super_block *s)
352+
{
353+
return le64_to_cpu(s->blocknr);
354+
}
355+
356+
static inline void btrfs_set_super_blocknr(struct btrfs_super_block *s, u64 val)
357+
{
358+
s->blocknr = cpu_to_le64(val);
359+
}
360+
361+
static inline u64 btrfs_super_root(struct btrfs_super_block *s)
362+
{
363+
return le64_to_cpu(s->root);
364+
}
365+
366+
static inline void btrfs_set_super_root(struct btrfs_super_block *s, u64 val)
367+
{
368+
s->root = cpu_to_le64(val);
369+
}
370+
371+
static inline u64 btrfs_super_total_blocks(struct btrfs_super_block *s)
372+
{
373+
return le64_to_cpu(s->total_blocks);
374+
}
375+
376+
static inline void btrfs_set_super_total_blocks(struct btrfs_super_block *s,
377+
u64 val)
378+
{
379+
s->total_blocks = cpu_to_le64(val);
380+
}
381+
382+
static inline u64 btrfs_super_blocks_used(struct btrfs_super_block *s)
383+
{
384+
return le64_to_cpu(s->blocks_used);
385+
}
386+
387+
static inline void btrfs_set_super_blocks_used(struct btrfs_super_block *s,
388+
u64 val)
389+
{
390+
s->blocks_used = cpu_to_le64(val);
391+
}
392+
393+
static inline u16 btrfs_super_blocksize(struct btrfs_super_block *s)
394+
{
395+
return le16_to_cpu(s->blocksize);
396+
}
397+
398+
static inline void btrfs_set_super_blocksize(struct btrfs_super_block *s,
399+
u16 val)
400+
{
401+
s->blocksize = cpu_to_le16(val);
402+
}
403+
320404
struct btrfs_buffer *btrfs_alloc_free_block(struct btrfs_root *root);
321405
int btrfs_inc_ref(struct btrfs_root *root, struct btrfs_buffer *buf);
322406
int btrfs_free_extent(struct btrfs_root *root, u64 blocknr, u64 num_blocks);
@@ -331,4 +415,11 @@ int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path);
331415
int btrfs_leaf_free_space(struct btrfs_leaf *leaf);
332416
int btrfs_drop_snapshot(struct btrfs_root *root, struct btrfs_buffer *snap);
333417
int btrfs_finish_extent_commit(struct btrfs_root *root);
418+
int btrfs_del_root(struct btrfs_root *root, struct btrfs_key *key);
419+
int btrfs_insert_root(struct btrfs_root *root, struct btrfs_key *key,
420+
struct btrfs_root_item *item);
421+
int btrfs_update_root(struct btrfs_root *root, struct btrfs_key *key,
422+
struct btrfs_root_item *item);
423+
int btrfs_find_last_root(struct btrfs_root *root, u64 objectid,
424+
struct btrfs_root_item *item, struct btrfs_key *key);
334425
#endif

debug-tree.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ int main(int ac, char **av) {
1111
struct btrfs_root *root;
1212
radix_tree_init();
1313
root = open_ctree("dbfile", &super);
14-
printf("root tree\n");
14+
printf("fs tree\n");
1515
btrfs_print_tree(root, root->node);
1616
printf("map tree\n");
1717
btrfs_print_tree(root->extent_root, root->extent_root->node);
18+
printf("root tree\n");
19+
btrfs_print_tree(root->tree_root, root->tree_root->node);
1820
return 0;
1921
}

0 commit comments

Comments
 (0)