more return code checking
[btrfs-progs-unstable.git] / ctree.h
blob18daccd84535068fccdb3f8212826ad1f944f9ce
1 #ifndef __CTREE__
2 #define __CTREE__
4 #define CTREE_BLOCKSIZE 1024
6 /*
7 * the key defines the order in the tree, and so it also defines (optimal)
8 * block layout. objectid corresonds to the inode number. The flags
9 * tells us things about the object, and is a kind of stream selector.
10 * so for a given inode, keys with flags of 1 might refer to the inode
11 * data, flags of 2 may point to file data in the btree and flags == 3
12 * may point to extents.
14 * offset is the starting byte offset for this key in the stream.
16 struct key {
17 u64 objectid;
18 u32 flags;
19 u64 offset;
20 } __attribute__ ((__packed__));
23 * every tree block (leaf or node) starts with this header.
25 struct header {
26 u64 fsid[2]; /* FS specific uuid */
27 u64 blocknr; /* which block this node is supposed to live in */
28 u64 parentid; /* objectid of the tree root */
29 u32 csum;
30 u32 ham;
31 u16 nritems;
32 u16 flags;
33 /* generation flags to be added */
34 } __attribute__ ((__packed__));
36 #define NODEPTRS_PER_BLOCK ((CTREE_BLOCKSIZE - sizeof(struct header)) / \
37 (sizeof(struct key) + sizeof(u64)))
39 #define MAX_LEVEL 8
40 #define node_level(f) ((f) & (MAX_LEVEL-1))
41 #define is_leaf(f) (node_level(f) == 0)
43 struct tree_buffer;
46 * in ram representation of the tree. extent_root is used for all allocations
47 * and for the extent tree extent_root root. current_insert is used
48 * only for the extent tree.
50 struct ctree_root {
51 struct tree_buffer *node;
52 struct ctree_root *extent_root;
53 struct key current_insert;
54 int fp;
55 struct radix_tree_root cache_radix;
59 * describes a tree on disk
61 struct ctree_root_info {
62 u64 fsid[2]; /* FS specific uuid */
63 u64 blocknr; /* blocknr of this block */
64 u64 objectid; /* inode number of this root */
65 u64 tree_root; /* the tree root block */
66 u32 csum;
67 u32 ham;
68 u64 snapuuid[2]; /* root specific uuid */
69 } __attribute__ ((__packed__));
72 * the super block basically lists the main trees of the FS
73 * it currently lacks any block count etc etc
75 struct ctree_super_block {
76 struct ctree_root_info root_info;
77 struct ctree_root_info extent_info;
78 } __attribute__ ((__packed__));
81 * A leaf is full of items. The exact type of item is defined by
82 * the key flags parameter. offset and size tell us where to find
83 * the item in the leaf (relative to the start of the data area)
85 struct item {
86 struct key key;
87 u16 offset;
88 u16 size;
89 } __attribute__ ((__packed__));
92 * leaves have an item area and a data area:
93 * [item0, item1....itemN] [free space] [dataN...data1, data0]
95 * The data is separate from the items to get the keys closer together
96 * during searches.
98 #define LEAF_DATA_SIZE (CTREE_BLOCKSIZE - sizeof(struct header))
99 struct leaf {
100 struct header header;
101 union {
102 struct item items[LEAF_DATA_SIZE/sizeof(struct item)];
103 u8 data[CTREE_BLOCKSIZE-sizeof(struct header)];
105 } __attribute__ ((__packed__));
108 * all non-leaf blocks are nodes, they hold only keys and pointers to
109 * other blocks
111 struct node {
112 struct header header;
113 struct key keys[NODEPTRS_PER_BLOCK];
114 u64 blockptrs[NODEPTRS_PER_BLOCK];
115 } __attribute__ ((__packed__));
118 * items in the extent btree are used to record the objectid of the
119 * owner of the block and the number of references
121 struct extent_item {
122 u32 refs;
123 u64 owner;
124 } __attribute__ ((__packed__));
127 * ctree_paths remember the path taken from the root down to the leaf.
128 * level 0 is always the leaf, and nodes[1...MAX_LEVEL] will point
129 * to any other levels that are present.
131 * The slots array records the index of the item or block pointer
132 * used while walking the tree.
134 struct ctree_path {
135 struct tree_buffer *nodes[MAX_LEVEL];
136 int slots[MAX_LEVEL];
139 struct tree_buffer *alloc_free_block(struct ctree_root *root);
140 int free_extent(struct ctree_root *root, u64 blocknr, u64 num_blocks);
141 int search_slot(struct ctree_root *root, struct key *key, struct ctree_path *p, int ins_len);
142 void release_path(struct ctree_root *root, struct ctree_path *p);
143 void init_path(struct ctree_path *p);
144 int del_item(struct ctree_root *root, struct ctree_path *path);
145 int insert_item(struct ctree_root *root, struct key *key, void *data, int data_size);
146 int next_leaf(struct ctree_root *root, struct ctree_path *path);
147 int leaf_free_space(struct leaf *leaf);
148 #endif