Btrfs: Fixup last found extent caching
[firefly-linux-kernel-4.4.55.git] / fs / btrfs / ctree.h
1 #ifndef __CTREE__
2 #define __CTREE__
3
4 #include "list.h"
5
6 #define CTREE_BLOCKSIZE 1024
7
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  */
18 struct key {
19         u64 objectid;
20         u32 flags;
21         u64 offset;
22 } __attribute__ ((__packed__));
23
24 /*
25  * every tree block (leaf or node) starts with this header.
26  */
27 struct header {
28         u64 fsid[2]; /* FS specific uuid */
29         u64 blocknr; /* which block this node is supposed to live in */
30         u64 parentid; /* objectid of the tree root */
31         u32 csum;
32         u32 ham;
33         u16 nritems;
34         u16 flags;
35         /* generation flags to be added */
36 } __attribute__ ((__packed__));
37
38 #define NODEPTRS_PER_BLOCK ((CTREE_BLOCKSIZE - sizeof(struct header)) / \
39                             (sizeof(struct key) + sizeof(u64)))
40
41 #define MAX_LEVEL 8
42 #define node_level(f) ((f) & (MAX_LEVEL-1))
43 #define is_leaf(f) (node_level(f) == 0)
44
45 struct tree_buffer;
46
47 /*
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  */
52 struct ctree_root {
53         struct tree_buffer *node;
54         struct tree_buffer *commit_root;
55         struct ctree_root *extent_root;
56         struct key current_insert;
57         struct key last_insert;
58         int fp;
59         struct radix_tree_root cache_radix;
60         struct radix_tree_root pinned_radix;
61         struct list_head trans;
62         struct list_head cache;
63         int cache_size;
64 };
65
66 /*
67  * describes a tree on disk
68  */
69 struct ctree_root_info {
70         u64 fsid[2]; /* FS specific uuid */
71         u64 blocknr; /* blocknr of this block */
72         u64 objectid; /* inode number of this root */
73         u64 tree_root; /* the tree root block */
74         u32 csum;
75         u32 ham;
76         u64 snapuuid[2]; /* root specific uuid */
77 } __attribute__ ((__packed__));
78
79 /*
80  * the super block basically lists the main trees of the FS
81  * it currently lacks any block count etc etc
82  */
83 struct ctree_super_block {
84         struct ctree_root_info root_info;
85         struct ctree_root_info extent_info;
86 } __attribute__ ((__packed__));
87
88 /*
89  * A leaf is full of items.  The exact type of item is defined by
90  * the key flags parameter.  offset and size tell us where to find
91  * the item in the leaf (relative to the start of the data area)
92  */
93 struct item {
94         struct key key;
95         u16 offset;
96         u16 size;
97 } __attribute__ ((__packed__));
98
99 /*
100  * leaves have an item area and a data area:
101  * [item0, item1....itemN] [free space] [dataN...data1, data0]
102  *
103  * The data is separate from the items to get the keys closer together
104  * during searches.
105  */
106 #define LEAF_DATA_SIZE (CTREE_BLOCKSIZE - sizeof(struct header))
107 struct leaf {
108         struct header header;
109         union {
110                 struct item items[LEAF_DATA_SIZE/sizeof(struct item)];
111                 u8 data[CTREE_BLOCKSIZE-sizeof(struct header)];
112         };
113 } __attribute__ ((__packed__));
114
115 /*
116  * all non-leaf blocks are nodes, they hold only keys and pointers to
117  * other blocks
118  */
119 struct node {
120         struct header header;
121         struct key keys[NODEPTRS_PER_BLOCK];
122         u64 blockptrs[NODEPTRS_PER_BLOCK];
123 } __attribute__ ((__packed__));
124
125 /*
126  * items in the extent btree are used to record the objectid of the
127  * owner of the block and the number of references
128  */
129 struct extent_item {
130         u32 refs;
131         u64 owner;
132 } __attribute__ ((__packed__));
133
134 /*
135  * ctree_paths remember the path taken from the root down to the leaf.
136  * level 0 is always the leaf, and nodes[1...MAX_LEVEL] will point
137  * to any other levels that are present.
138  *
139  * The slots array records the index of the item or block pointer
140  * used while walking the tree.
141  */
142 struct ctree_path {
143         struct tree_buffer *nodes[MAX_LEVEL];
144         int slots[MAX_LEVEL];
145 };
146
147 struct tree_buffer *alloc_free_block(struct ctree_root *root);
148 int btrfs_inc_ref(struct ctree_root *root, struct tree_buffer *buf);
149 int free_extent(struct ctree_root *root, u64 blocknr, u64 num_blocks);
150 int search_slot(struct ctree_root *root, struct key *key, struct ctree_path *p, int ins_len, int cow);
151 void release_path(struct ctree_root *root, struct ctree_path *p);
152 void init_path(struct ctree_path *p);
153 int del_item(struct ctree_root *root, struct ctree_path *path);
154 int insert_item(struct ctree_root *root, struct key *key, void *data, int data_size);
155 int next_leaf(struct ctree_root *root, struct ctree_path *path);
156 int leaf_free_space(struct leaf *leaf);
157 int btrfs_drop_snapshot(struct ctree_root *root, struct tree_buffer *snap);
158 int btrfs_finish_extent_commit(struct ctree_root *root);
159 #endif