blob: 18daccd84535068fccdb3f8212826ad1f944f9ce [file] [log] [blame]
Chris Masoneb60cea2007-02-02 09:18:22 -05001#ifndef __CTREE__
2#define __CTREE__
3
Chris Masonfec577f2007-02-26 10:40:21 -05004#define CTREE_BLOCKSIZE 1024
Chris Masoneb60cea2007-02-02 09:18:22 -05005
Chris Masonfec577f2007-02-26 10:40:21 -05006/*
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.
13 *
14 * offset is the starting byte offset for this key in the stream.
15 */
Chris Masoneb60cea2007-02-02 09:18:22 -050016struct key {
17 u64 objectid;
18 u32 flags;
19 u64 offset;
20} __attribute__ ((__packed__));
21
Chris Masonfec577f2007-02-26 10:40:21 -050022/*
23 * every tree block (leaf or node) starts with this header.
24 */
Chris Masoneb60cea2007-02-02 09:18:22 -050025struct header {
26 u64 fsid[2]; /* FS specific uuid */
Chris Masonfec577f2007-02-26 10:40:21 -050027 u64 blocknr; /* which block this node is supposed to live in */
28 u64 parentid; /* objectid of the tree root */
Chris Masoneb60cea2007-02-02 09:18:22 -050029 u32 csum;
30 u32 ham;
31 u16 nritems;
32 u16 flags;
Chris Masonfec577f2007-02-26 10:40:21 -050033 /* generation flags to be added */
Chris Masoneb60cea2007-02-02 09:18:22 -050034} __attribute__ ((__packed__));
35
36#define NODEPTRS_PER_BLOCK ((CTREE_BLOCKSIZE - sizeof(struct header)) / \
37 (sizeof(struct key) + sizeof(u64)))
38
Chris Masond97e63b2007-02-20 16:40:44 -050039#define MAX_LEVEL 8
Chris Masoneb60cea2007-02-02 09:18:22 -050040#define node_level(f) ((f) & (MAX_LEVEL-1))
41#define is_leaf(f) (node_level(f) == 0)
42
43struct tree_buffer;
Chris Masond97e63b2007-02-20 16:40:44 -050044
Chris Masonfec577f2007-02-26 10:40:21 -050045/*
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.
49 */
Chris Masoneb60cea2007-02-02 09:18:22 -050050struct ctree_root {
51 struct tree_buffer *node;
Chris Masond97e63b2007-02-20 16:40:44 -050052 struct ctree_root *extent_root;
Chris Mason9a8dd152007-02-23 08:38:36 -050053 struct key current_insert;
Chris Masoneb60cea2007-02-02 09:18:22 -050054 int fp;
55 struct radix_tree_root cache_radix;
56};
57
Chris Masonfec577f2007-02-26 10:40:21 -050058/*
59 * describes a tree on disk
60 */
Chris Masond97e63b2007-02-20 16:40:44 -050061struct 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 */
Chris Masonfec577f2007-02-26 10:40:21 -050065 u64 tree_root; /* the tree root block */
Chris Masond97e63b2007-02-20 16:40:44 -050066 u32 csum;
67 u32 ham;
Chris Masond97e63b2007-02-20 16:40:44 -050068 u64 snapuuid[2]; /* root specific uuid */
69} __attribute__ ((__packed__));
70
Chris Masonfec577f2007-02-26 10:40:21 -050071/*
72 * the super block basically lists the main trees of the FS
73 * it currently lacks any block count etc etc
74 */
Chris Masoncfaa7292007-02-21 17:04:57 -050075struct ctree_super_block {
76 struct ctree_root_info root_info;
77 struct ctree_root_info extent_info;
78} __attribute__ ((__packed__));
79
Chris Masonfec577f2007-02-26 10:40:21 -050080/*
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)
84 */
Chris Masoneb60cea2007-02-02 09:18:22 -050085struct item {
86 struct key key;
87 u16 offset;
88 u16 size;
89} __attribute__ ((__packed__));
90
Chris Masonfec577f2007-02-26 10:40:21 -050091/*
92 * leaves have an item area and a data area:
93 * [item0, item1....itemN] [free space] [dataN...data1, data0]
94 *
95 * The data is separate from the items to get the keys closer together
96 * during searches.
97 */
Chris Masoneb60cea2007-02-02 09:18:22 -050098#define LEAF_DATA_SIZE (CTREE_BLOCKSIZE - sizeof(struct header))
99struct 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)];
104 };
105} __attribute__ ((__packed__));
106
Chris Masonfec577f2007-02-26 10:40:21 -0500107/*
108 * all non-leaf blocks are nodes, they hold only keys and pointers to
109 * other blocks
110 */
Chris Masoneb60cea2007-02-02 09:18:22 -0500111struct node {
112 struct header header;
113 struct key keys[NODEPTRS_PER_BLOCK];
114 u64 blockptrs[NODEPTRS_PER_BLOCK];
115} __attribute__ ((__packed__));
116
Chris Masonfec577f2007-02-26 10:40:21 -0500117/*
118 * items in the extent btree are used to record the objectid of the
119 * owner of the block and the number of references
120 */
Chris Masond97e63b2007-02-20 16:40:44 -0500121struct extent_item {
122 u32 refs;
123 u64 owner;
124} __attribute__ ((__packed__));
125
Chris Masonfec577f2007-02-26 10:40:21 -0500126/*
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.
130 *
131 * The slots array records the index of the item or block pointer
132 * used while walking the tree.
133 */
Chris Masoneb60cea2007-02-02 09:18:22 -0500134struct ctree_path {
135 struct tree_buffer *nodes[MAX_LEVEL];
136 int slots[MAX_LEVEL];
137};
Chris Mason5de08d72007-02-24 06:24:44 -0500138
139struct tree_buffer *alloc_free_block(struct ctree_root *root);
140int free_extent(struct ctree_root *root, u64 blocknr, u64 num_blocks);
141int search_slot(struct ctree_root *root, struct key *key, struct ctree_path *p, int ins_len);
142void release_path(struct ctree_root *root, struct ctree_path *p);
143void init_path(struct ctree_path *p);
144int del_item(struct ctree_root *root, struct ctree_path *path);
145int insert_item(struct ctree_root *root, struct key *key, void *data, int data_size);
146int next_leaf(struct ctree_root *root, struct ctree_path *path);
147int leaf_free_space(struct leaf *leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -0500148#endif