blob: 4a7bc4e6e7471a5e6e87a613b96d2096f71e7d5f [file] [log] [blame]
Chris Masoneb60cea2007-02-02 09:18:22 -05001#ifndef __CTREE__
2#define __CTREE__
3
Chris Masoned2ff2c2007-03-01 18:59:40 -05004#include "list.h"
5
Chris Masonfec577f2007-02-26 10:40:21 -05006#define CTREE_BLOCKSIZE 1024
Chris Masoneb60cea2007-02-02 09:18:22 -05007
Chris Masonfec577f2007-02-26 10:40:21 -05008/*
9 * the key defines the order in the tree, and so it also defines (optimal)
10 * block layout. objectid corresonds to the inode number. The flags
11 * tells us things about the object, and is a kind of stream selector.
12 * so for a given inode, keys with flags of 1 might refer to the inode
13 * data, flags of 2 may point to file data in the btree and flags == 3
14 * may point to extents.
15 *
16 * offset is the starting byte offset for this key in the stream.
17 */
Chris Masoneb60cea2007-02-02 09:18:22 -050018struct key {
19 u64 objectid;
20 u32 flags;
21 u64 offset;
22} __attribute__ ((__packed__));
23
Chris Masonfec577f2007-02-26 10:40:21 -050024/*
25 * every tree block (leaf or node) starts with this header.
26 */
Chris Masoneb60cea2007-02-02 09:18:22 -050027struct header {
28 u64 fsid[2]; /* FS specific uuid */
Chris Masonfec577f2007-02-26 10:40:21 -050029 u64 blocknr; /* which block this node is supposed to live in */
30 u64 parentid; /* objectid of the tree root */
Chris Masoneb60cea2007-02-02 09:18:22 -050031 u32 csum;
32 u32 ham;
33 u16 nritems;
34 u16 flags;
Chris Masonfec577f2007-02-26 10:40:21 -050035 /* generation flags to be added */
Chris Masoneb60cea2007-02-02 09:18:22 -050036} __attribute__ ((__packed__));
37
38#define NODEPTRS_PER_BLOCK ((CTREE_BLOCKSIZE - sizeof(struct header)) / \
39 (sizeof(struct key) + sizeof(u64)))
40
Chris Masond97e63b2007-02-20 16:40:44 -050041#define MAX_LEVEL 8
Chris Masoneb60cea2007-02-02 09:18:22 -050042#define node_level(f) ((f) & (MAX_LEVEL-1))
43#define is_leaf(f) (node_level(f) == 0)
44
45struct tree_buffer;
Chris Masond97e63b2007-02-20 16:40:44 -050046
Chris Masonfec577f2007-02-26 10:40:21 -050047/*
48 * in ram representation of the tree. extent_root is used for all allocations
49 * and for the extent tree extent_root root. current_insert is used
50 * only for the extent tree.
51 */
Chris Masoneb60cea2007-02-02 09:18:22 -050052struct ctree_root {
53 struct tree_buffer *node;
Chris Masona28ec192007-03-06 20:08:01 -050054 struct tree_buffer *commit_root;
Chris Masond97e63b2007-02-20 16:40:44 -050055 struct ctree_root *extent_root;
Chris Mason9a8dd152007-02-23 08:38:36 -050056 struct key current_insert;
Chris Masoneb60cea2007-02-02 09:18:22 -050057 int fp;
58 struct radix_tree_root cache_radix;
Chris Masona28ec192007-03-06 20:08:01 -050059 struct radix_tree_root pinned_radix;
Chris Masoned2ff2c2007-03-01 18:59:40 -050060 struct list_head trans;
61 struct list_head cache;
62 int cache_size;
Chris Masoneb60cea2007-02-02 09:18:22 -050063};
64
Chris Masonfec577f2007-02-26 10:40:21 -050065/*
66 * describes a tree on disk
67 */
Chris Masond97e63b2007-02-20 16:40:44 -050068struct ctree_root_info {
69 u64 fsid[2]; /* FS specific uuid */
70 u64 blocknr; /* blocknr of this block */
71 u64 objectid; /* inode number of this root */
Chris Masonfec577f2007-02-26 10:40:21 -050072 u64 tree_root; /* the tree root block */
Chris Masond97e63b2007-02-20 16:40:44 -050073 u32 csum;
74 u32 ham;
Chris Masond97e63b2007-02-20 16:40:44 -050075 u64 snapuuid[2]; /* root specific uuid */
76} __attribute__ ((__packed__));
77
Chris Masonfec577f2007-02-26 10:40:21 -050078/*
79 * the super block basically lists the main trees of the FS
80 * it currently lacks any block count etc etc
81 */
Chris Masoncfaa7292007-02-21 17:04:57 -050082struct ctree_super_block {
83 struct ctree_root_info root_info;
84 struct ctree_root_info extent_info;
85} __attribute__ ((__packed__));
86
Chris Masonfec577f2007-02-26 10:40:21 -050087/*
88 * A leaf is full of items. The exact type of item is defined by
89 * the key flags parameter. offset and size tell us where to find
90 * the item in the leaf (relative to the start of the data area)
91 */
Chris Masoneb60cea2007-02-02 09:18:22 -050092struct item {
93 struct key key;
94 u16 offset;
95 u16 size;
96} __attribute__ ((__packed__));
97
Chris Masonfec577f2007-02-26 10:40:21 -050098/*
99 * leaves have an item area and a data area:
100 * [item0, item1....itemN] [free space] [dataN...data1, data0]
101 *
102 * The data is separate from the items to get the keys closer together
103 * during searches.
104 */
Chris Masoneb60cea2007-02-02 09:18:22 -0500105#define LEAF_DATA_SIZE (CTREE_BLOCKSIZE - sizeof(struct header))
106struct leaf {
107 struct header header;
108 union {
109 struct item items[LEAF_DATA_SIZE/sizeof(struct item)];
110 u8 data[CTREE_BLOCKSIZE-sizeof(struct header)];
111 };
112} __attribute__ ((__packed__));
113
Chris Masonfec577f2007-02-26 10:40:21 -0500114/*
115 * all non-leaf blocks are nodes, they hold only keys and pointers to
116 * other blocks
117 */
Chris Masoneb60cea2007-02-02 09:18:22 -0500118struct node {
119 struct header header;
120 struct key keys[NODEPTRS_PER_BLOCK];
121 u64 blockptrs[NODEPTRS_PER_BLOCK];
122} __attribute__ ((__packed__));
123
Chris Masonfec577f2007-02-26 10:40:21 -0500124/*
125 * items in the extent btree are used to record the objectid of the
126 * owner of the block and the number of references
127 */
Chris Masond97e63b2007-02-20 16:40:44 -0500128struct extent_item {
129 u32 refs;
130 u64 owner;
131} __attribute__ ((__packed__));
132
Chris Masonfec577f2007-02-26 10:40:21 -0500133/*
134 * ctree_paths remember the path taken from the root down to the leaf.
135 * level 0 is always the leaf, and nodes[1...MAX_LEVEL] will point
136 * to any other levels that are present.
137 *
138 * The slots array records the index of the item or block pointer
139 * used while walking the tree.
140 */
Chris Masoneb60cea2007-02-02 09:18:22 -0500141struct ctree_path {
142 struct tree_buffer *nodes[MAX_LEVEL];
143 int slots[MAX_LEVEL];
144};
Chris Mason5de08d72007-02-24 06:24:44 -0500145
146struct tree_buffer *alloc_free_block(struct ctree_root *root);
Chris Mason02217ed2007-03-02 16:08:05 -0500147int btrfs_inc_ref(struct ctree_root *root, struct tree_buffer *buf);
Chris Mason5de08d72007-02-24 06:24:44 -0500148int free_extent(struct ctree_root *root, u64 blocknr, u64 num_blocks);
Chris Mason02217ed2007-03-02 16:08:05 -0500149int search_slot(struct ctree_root *root, struct key *key, struct ctree_path *p, int ins_len, int cow);
Chris Mason5de08d72007-02-24 06:24:44 -0500150void release_path(struct ctree_root *root, struct ctree_path *p);
151void init_path(struct ctree_path *p);
152int del_item(struct ctree_root *root, struct ctree_path *path);
153int insert_item(struct ctree_root *root, struct key *key, void *data, int data_size);
154int next_leaf(struct ctree_root *root, struct ctree_path *path);
155int leaf_free_space(struct leaf *leaf);
Chris Masona28ec192007-03-06 20:08:01 -0500156int btrfs_drop_snapshot(struct ctree_root *root, struct tree_buffer *snap);
157int btrfs_finish_extent_commit(struct ctree_root *root);
Chris Masoneb60cea2007-02-02 09:18:22 -0500158#endif