blob: e1aaca66d5900b2d2b87f32a4acc5999c5a7f561 [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 Masonbb492bb2007-03-12 12:29:44 -040027struct btrfs_header {
28 __le64 fsid[2]; /* FS specific uuid */
29 __le64 blocknr; /* which block this node is supposed to live in */
30 __le64 parentid; /* objectid of the tree root */
31 __le32 csum;
32 __le32 ham;
33 __le16 nritems;
34 __le16 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
Chris Mason7518a232007-03-12 12:01:18 -040038#define MAX_LEVEL 8
Chris Masonbb492bb2007-03-12 12:29:44 -040039#define NODEPTRS_PER_BLOCK ((CTREE_BLOCKSIZE - sizeof(struct btrfs_header)) / \
Chris Masoneb60cea2007-02-02 09:18:22 -050040 (sizeof(struct key) + sizeof(u64)))
41
Chris Masoneb60cea2007-02-02 09:18:22 -050042struct tree_buffer;
Chris Masond97e63b2007-02-20 16:40:44 -050043
Chris Masonfec577f2007-02-26 10:40:21 -050044/*
45 * in ram representation of the tree. extent_root is used for all allocations
46 * and for the extent tree extent_root root. current_insert is used
47 * only for the extent tree.
48 */
Chris Masoneb60cea2007-02-02 09:18:22 -050049struct ctree_root {
50 struct tree_buffer *node;
Chris Masona28ec192007-03-06 20:08:01 -050051 struct tree_buffer *commit_root;
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 Mason0579da42007-03-07 16:15:30 -050054 struct key last_insert;
Chris Masoneb60cea2007-02-02 09:18:22 -050055 int fp;
56 struct radix_tree_root cache_radix;
Chris Masona28ec192007-03-06 20:08:01 -050057 struct radix_tree_root pinned_radix;
Chris Masoned2ff2c2007-03-01 18:59:40 -050058 struct list_head trans;
59 struct list_head cache;
60 int cache_size;
Chris Masoneb60cea2007-02-02 09:18:22 -050061};
62
Chris Masonfec577f2007-02-26 10:40:21 -050063/*
64 * describes a tree on disk
65 */
Chris Masond97e63b2007-02-20 16:40:44 -050066struct ctree_root_info {
67 u64 fsid[2]; /* FS specific uuid */
68 u64 blocknr; /* blocknr of this block */
69 u64 objectid; /* inode number of this root */
Chris Masonfec577f2007-02-26 10:40:21 -050070 u64 tree_root; /* the tree root block */
Chris Masond97e63b2007-02-20 16:40:44 -050071 u32 csum;
72 u32 ham;
Chris Masond97e63b2007-02-20 16:40:44 -050073 u64 snapuuid[2]; /* root specific uuid */
74} __attribute__ ((__packed__));
75
Chris Masonfec577f2007-02-26 10:40:21 -050076/*
77 * the super block basically lists the main trees of the FS
78 * it currently lacks any block count etc etc
79 */
Chris Masoncfaa7292007-02-21 17:04:57 -050080struct ctree_super_block {
81 struct ctree_root_info root_info;
82 struct ctree_root_info extent_info;
83} __attribute__ ((__packed__));
84
Chris Masonfec577f2007-02-26 10:40:21 -050085/*
86 * A leaf is full of items. The exact type of item is defined by
87 * the key flags parameter. offset and size tell us where to find
88 * the item in the leaf (relative to the start of the data area)
89 */
Chris Masoneb60cea2007-02-02 09:18:22 -050090struct item {
91 struct key key;
92 u16 offset;
93 u16 size;
94} __attribute__ ((__packed__));
95
Chris Masonfec577f2007-02-26 10:40:21 -050096/*
97 * leaves have an item area and a data area:
98 * [item0, item1....itemN] [free space] [dataN...data1, data0]
99 *
100 * The data is separate from the items to get the keys closer together
101 * during searches.
102 */
Chris Masonbb492bb2007-03-12 12:29:44 -0400103#define LEAF_DATA_SIZE (CTREE_BLOCKSIZE - sizeof(struct btrfs_header))
Chris Masoneb60cea2007-02-02 09:18:22 -0500104struct leaf {
Chris Masonbb492bb2007-03-12 12:29:44 -0400105 struct btrfs_header header;
Chris Masoneb60cea2007-02-02 09:18:22 -0500106 union {
107 struct item items[LEAF_DATA_SIZE/sizeof(struct item)];
Chris Masonbb492bb2007-03-12 12:29:44 -0400108 u8 data[CTREE_BLOCKSIZE-sizeof(struct btrfs_header)];
Chris Masoneb60cea2007-02-02 09:18:22 -0500109 };
110} __attribute__ ((__packed__));
111
Chris Masonfec577f2007-02-26 10:40:21 -0500112/*
113 * all non-leaf blocks are nodes, they hold only keys and pointers to
114 * other blocks
115 */
Chris Masoneb60cea2007-02-02 09:18:22 -0500116struct node {
Chris Masonbb492bb2007-03-12 12:29:44 -0400117 struct btrfs_header header;
Chris Masoneb60cea2007-02-02 09:18:22 -0500118 struct key keys[NODEPTRS_PER_BLOCK];
119 u64 blockptrs[NODEPTRS_PER_BLOCK];
120} __attribute__ ((__packed__));
121
Chris Masonfec577f2007-02-26 10:40:21 -0500122/*
123 * items in the extent btree are used to record the objectid of the
124 * owner of the block and the number of references
125 */
Chris Masond97e63b2007-02-20 16:40:44 -0500126struct extent_item {
127 u32 refs;
128 u64 owner;
129} __attribute__ ((__packed__));
130
Chris Masonfec577f2007-02-26 10:40:21 -0500131/*
132 * ctree_paths remember the path taken from the root down to the leaf.
133 * level 0 is always the leaf, and nodes[1...MAX_LEVEL] will point
134 * to any other levels that are present.
135 *
136 * The slots array records the index of the item or block pointer
137 * used while walking the tree.
138 */
Chris Masoneb60cea2007-02-02 09:18:22 -0500139struct ctree_path {
140 struct tree_buffer *nodes[MAX_LEVEL];
141 int slots[MAX_LEVEL];
142};
Chris Mason5de08d72007-02-24 06:24:44 -0500143
Chris Masonbb492bb2007-03-12 12:29:44 -0400144static inline u64 btrfs_header_blocknr(struct btrfs_header *h)
Chris Mason7518a232007-03-12 12:01:18 -0400145{
Chris Masonbb492bb2007-03-12 12:29:44 -0400146 return le64_to_cpu(h->blocknr);
Chris Mason7518a232007-03-12 12:01:18 -0400147}
148
Chris Masonbb492bb2007-03-12 12:29:44 -0400149static inline void btrfs_set_header_blocknr(struct btrfs_header *h, u64 blocknr)
Chris Mason7518a232007-03-12 12:01:18 -0400150{
Chris Masonbb492bb2007-03-12 12:29:44 -0400151 h->blocknr = cpu_to_le64(blocknr);
Chris Mason7518a232007-03-12 12:01:18 -0400152}
153
Chris Masonbb492bb2007-03-12 12:29:44 -0400154static inline u64 btrfs_header_parentid(struct btrfs_header *h)
Chris Mason7518a232007-03-12 12:01:18 -0400155{
Chris Masonbb492bb2007-03-12 12:29:44 -0400156 return le64_to_cpu(h->parentid);
Chris Mason7518a232007-03-12 12:01:18 -0400157}
158
Chris Masonbb492bb2007-03-12 12:29:44 -0400159static inline void btrfs_set_header_parentid(struct btrfs_header *h,
160 u64 parentid)
Chris Mason7518a232007-03-12 12:01:18 -0400161{
Chris Masonbb492bb2007-03-12 12:29:44 -0400162 h->parentid = cpu_to_le64(parentid);
Chris Mason7518a232007-03-12 12:01:18 -0400163}
164
Chris Masonbb492bb2007-03-12 12:29:44 -0400165static inline u16 btrfs_header_nritems(struct btrfs_header *h)
Chris Mason7518a232007-03-12 12:01:18 -0400166{
Chris Masonbb492bb2007-03-12 12:29:44 -0400167 return le16_to_cpu(h->nritems);
Chris Mason7518a232007-03-12 12:01:18 -0400168}
169
Chris Masonbb492bb2007-03-12 12:29:44 -0400170static inline void btrfs_set_header_nritems(struct btrfs_header *h, u16 val)
Chris Mason7518a232007-03-12 12:01:18 -0400171{
Chris Masonbb492bb2007-03-12 12:29:44 -0400172 h->nritems = cpu_to_le16(val);
Chris Mason7518a232007-03-12 12:01:18 -0400173}
174
Chris Masonbb492bb2007-03-12 12:29:44 -0400175static inline u16 btrfs_header_flags(struct btrfs_header *h)
Chris Mason7518a232007-03-12 12:01:18 -0400176{
Chris Masonbb492bb2007-03-12 12:29:44 -0400177 return le16_to_cpu(h->flags);
Chris Mason7518a232007-03-12 12:01:18 -0400178}
179
Chris Masonbb492bb2007-03-12 12:29:44 -0400180static inline void btrfs_set_header_flags(struct btrfs_header *h, u16 val)
Chris Mason7518a232007-03-12 12:01:18 -0400181{
Chris Masonbb492bb2007-03-12 12:29:44 -0400182 h->flags = cpu_to_le16(val);
Chris Mason7518a232007-03-12 12:01:18 -0400183}
184
Chris Masonbb492bb2007-03-12 12:29:44 -0400185static inline int btrfs_header_level(struct btrfs_header *h)
Chris Mason7518a232007-03-12 12:01:18 -0400186{
187 return btrfs_header_flags(h) & (MAX_LEVEL - 1);
188}
189
Chris Masonbb492bb2007-03-12 12:29:44 -0400190static inline void btrfs_set_header_level(struct btrfs_header *h, int level)
Chris Mason7518a232007-03-12 12:01:18 -0400191{
Chris Masonbb492bb2007-03-12 12:29:44 -0400192 u16 flags;
Chris Mason7518a232007-03-12 12:01:18 -0400193 BUG_ON(level > MAX_LEVEL);
194 flags = btrfs_header_flags(h) & ~(MAX_LEVEL - 1);
195 btrfs_set_header_flags(h, flags | level);
196}
197
198static inline int btrfs_is_leaf(struct node *n)
199{
200 return (btrfs_header_level(&n->header) == 0);
201}
202
Chris Mason5de08d72007-02-24 06:24:44 -0500203struct tree_buffer *alloc_free_block(struct ctree_root *root);
Chris Mason02217ed2007-03-02 16:08:05 -0500204int btrfs_inc_ref(struct ctree_root *root, struct tree_buffer *buf);
Chris Mason5de08d72007-02-24 06:24:44 -0500205int free_extent(struct ctree_root *root, u64 blocknr, u64 num_blocks);
Chris Mason02217ed2007-03-02 16:08:05 -0500206int 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 -0500207void release_path(struct ctree_root *root, struct ctree_path *p);
208void init_path(struct ctree_path *p);
209int del_item(struct ctree_root *root, struct ctree_path *path);
210int insert_item(struct ctree_root *root, struct key *key, void *data, int data_size);
211int next_leaf(struct ctree_root *root, struct ctree_path *path);
212int leaf_free_space(struct leaf *leaf);
Chris Masona28ec192007-03-06 20:08:01 -0500213int btrfs_drop_snapshot(struct ctree_root *root, struct tree_buffer *snap);
214int btrfs_finish_extent_commit(struct ctree_root *root);
Chris Masoneb60cea2007-02-02 09:18:22 -0500215#endif