]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/btrfs/ctree.h
Btrfs: get/set for struct header fields
[net-next-2.6.git] / fs / btrfs / ctree.h
CommitLineData
eb60ceac
CM
1#ifndef __CTREE__
2#define __CTREE__
3
ed2ff2cb
CM
4#include "list.h"
5
fec577fb 6#define CTREE_BLOCKSIZE 1024
eb60ceac 7
fec577fb
CM
8/*
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 */
eb60ceac
CM
18struct key {
19 u64 objectid;
20 u32 flags;
21 u64 offset;
22} __attribute__ ((__packed__));
23
fec577fb
CM
24/*
25 * every tree block (leaf or node) starts with this header.
26 */
eb60ceac
CM
27struct header {
28 u64 fsid[2]; /* FS specific uuid */
fec577fb
CM
29 u64 blocknr; /* which block this node is supposed to live in */
30 u64 parentid; /* objectid of the tree root */
eb60ceac
CM
31 u32 csum;
32 u32 ham;
33 u16 nritems;
34 u16 flags;
fec577fb 35 /* generation flags to be added */
eb60ceac
CM
36} __attribute__ ((__packed__));
37
7518a238 38#define MAX_LEVEL 8
eb60ceac
CM
39#define NODEPTRS_PER_BLOCK ((CTREE_BLOCKSIZE - sizeof(struct header)) / \
40 (sizeof(struct key) + sizeof(u64)))
41
eb60ceac 42struct tree_buffer;
d97e63b6 43
fec577fb
CM
44/*
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 */
eb60ceac
CM
49struct ctree_root {
50 struct tree_buffer *node;
a28ec197 51 struct tree_buffer *commit_root;
d97e63b6 52 struct ctree_root *extent_root;
9a8dd150 53 struct key current_insert;
0579da42 54 struct key last_insert;
eb60ceac
CM
55 int fp;
56 struct radix_tree_root cache_radix;
a28ec197 57 struct radix_tree_root pinned_radix;
ed2ff2cb
CM
58 struct list_head trans;
59 struct list_head cache;
60 int cache_size;
eb60ceac
CM
61};
62
fec577fb
CM
63/*
64 * describes a tree on disk
65 */
d97e63b6
CM
66struct 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 */
fec577fb 70 u64 tree_root; /* the tree root block */
d97e63b6
CM
71 u32 csum;
72 u32 ham;
d97e63b6
CM
73 u64 snapuuid[2]; /* root specific uuid */
74} __attribute__ ((__packed__));
75
fec577fb
CM
76/*
77 * the super block basically lists the main trees of the FS
78 * it currently lacks any block count etc etc
79 */
cfaa7295
CM
80struct ctree_super_block {
81 struct ctree_root_info root_info;
82 struct ctree_root_info extent_info;
83} __attribute__ ((__packed__));
84
fec577fb
CM
85/*
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 */
eb60ceac
CM
90struct item {
91 struct key key;
92 u16 offset;
93 u16 size;
94} __attribute__ ((__packed__));
95
fec577fb
CM
96/*
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 */
eb60ceac
CM
103#define LEAF_DATA_SIZE (CTREE_BLOCKSIZE - sizeof(struct header))
104struct leaf {
105 struct header header;
106 union {
107 struct item items[LEAF_DATA_SIZE/sizeof(struct item)];
108 u8 data[CTREE_BLOCKSIZE-sizeof(struct header)];
109 };
110} __attribute__ ((__packed__));
111
fec577fb
CM
112/*
113 * all non-leaf blocks are nodes, they hold only keys and pointers to
114 * other blocks
115 */
eb60ceac
CM
116struct node {
117 struct header header;
118 struct key keys[NODEPTRS_PER_BLOCK];
119 u64 blockptrs[NODEPTRS_PER_BLOCK];
120} __attribute__ ((__packed__));
121
fec577fb
CM
122/*
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 */
d97e63b6
CM
126struct extent_item {
127 u32 refs;
128 u64 owner;
129} __attribute__ ((__packed__));
130
fec577fb
CM
131/*
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 */
eb60ceac
CM
139struct ctree_path {
140 struct tree_buffer *nodes[MAX_LEVEL];
141 int slots[MAX_LEVEL];
142};
5de08d7d 143
7518a238
CM
144static inline u64 btrfs_header_blocknr(struct header *h)
145{
146 return h->blocknr;
147}
148
149static inline void btrfs_set_header_blocknr(struct header *h, u64 blocknr)
150{
151 h->blocknr = blocknr;
152}
153
154static inline u64 btrfs_header_parentid(struct header *h)
155{
156 return h->parentid;
157}
158
159static inline void btrfs_set_header_parentid(struct header *h, u64 parentid)
160{
161 h->parentid = parentid;
162}
163
164static inline u32 btrfs_header_nritems(struct header *h)
165{
166 return h->nritems;
167}
168
169static inline void btrfs_set_header_nritems(struct header *h, u32 val)
170{
171 h->nritems = val;
172}
173
174static inline u32 btrfs_header_flags(struct header *h)
175{
176 return h->flags;
177}
178
179static inline void btrfs_set_header_flags(struct header *h, u32 val)
180{
181 h->flags = val;
182}
183
184static inline int btrfs_header_level(struct header *h)
185{
186 return btrfs_header_flags(h) & (MAX_LEVEL - 1);
187}
188
189static inline void btrfs_set_header_level(struct header *h, int level)
190{
191 u32 flags;
192 BUG_ON(level > MAX_LEVEL);
193 flags = btrfs_header_flags(h) & ~(MAX_LEVEL - 1);
194 btrfs_set_header_flags(h, flags | level);
195}
196
197static inline int btrfs_is_leaf(struct node *n)
198{
199 return (btrfs_header_level(&n->header) == 0);
200}
201
5de08d7d 202struct tree_buffer *alloc_free_block(struct ctree_root *root);
02217ed2 203int btrfs_inc_ref(struct ctree_root *root, struct tree_buffer *buf);
5de08d7d 204int free_extent(struct ctree_root *root, u64 blocknr, u64 num_blocks);
02217ed2 205int search_slot(struct ctree_root *root, struct key *key, struct ctree_path *p, int ins_len, int cow);
5de08d7d
CM
206void release_path(struct ctree_root *root, struct ctree_path *p);
207void init_path(struct ctree_path *p);
208int del_item(struct ctree_root *root, struct ctree_path *path);
209int insert_item(struct ctree_root *root, struct key *key, void *data, int data_size);
210int next_leaf(struct ctree_root *root, struct ctree_path *path);
211int leaf_free_space(struct leaf *leaf);
a28ec197
CM
212int btrfs_drop_snapshot(struct ctree_root *root, struct tree_buffer *snap);
213int btrfs_finish_extent_commit(struct ctree_root *root);
eb60ceac 214#endif