• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef _BTRFS_H_
2 #define _BTRFS_H_
3 
4 #include <stdint.h>
5 #include <zconf.h>
6 
7 typedef uint8_t u8;
8 typedef uint16_t u16;
9 typedef uint32_t u32;
10 typedef uint64_t u64;
11 /* type that store on disk, but it is same as cpu type for i386 arch */
12 typedef u16 __le16;
13 typedef u32 __le32;
14 typedef u64 __le64;
15 
16 #include "crc32c.h"
17 #define btrfs_crc32c crc32c_le
18 
19 #define BTRFS_SUPER_INFO_OFFSET (64 * 1024)
20 #define BTRFS_SUPER_INFO_SIZE 4096
21 #define BTRFS_MAX_LEAF_SIZE 4096
22 #define BTRFS_BLOCK_SHIFT 12
23 #define BTRFS_BLOCK_SIZE  (1 << BTRFS_BLOCK_SHIFT)
24 
25 #define BTRFS_SUPER_MIRROR_MAX   3
26 #define BTRFS_SUPER_MIRROR_SHIFT 12
27 #define BTRFS_CSUM_SIZE 32
28 #define BTRFS_FSID_SIZE 16
29 #define BTRFS_LABEL_SIZE 256
30 #define BTRFS_SYSTEM_CHUNK_ARRAY_SIZE 2048
31 #define BTRFS_UUID_SIZE 16
32 
33 #define BTRFS_MAGIC "_BHRfS_M"
34 #define BTRFS_MAGIC_L 8
35 #define BTRFS_MAGIC_N 0x4d5f53665248425f
36 
37 #define BTRFS_SUPER_FLAG_METADUMP	(1ULL << 33)
38 
39 #define BTRFS_DEV_ITEM_KEY	216
40 #define BTRFS_CHUNK_ITEM_KEY	228
41 #define BTRFS_ROOT_REF_KEY	156
42 #define BTRFS_ROOT_ITEM_KEY	132
43 #define BTRFS_EXTENT_DATA_KEY	108
44 #define BTRFS_DIR_ITEM_KEY	84
45 #define BTRFS_INODE_ITEM_KEY	1
46 
47 #define BTRFS_EXTENT_TREE_OBJECTID 2ULL
48 #define BTRFS_FS_TREE_OBJECTID 5ULL
49 
50 #define BTRFS_FIRST_CHUNK_TREE_OBJECTID 256ULL
51 
52 #define BTRFS_FILE_EXTENT_INLINE 0
53 #define BTRFS_FILE_EXTENT_REG 1
54 #define BTRFS_FILE_EXTENT_PREALLOC 2
55 
56 #define BTRFS_MAX_LEVEL 8
57 #define BTRFS_MAX_CHUNK_ENTRIES 256
58 
59 #define BTRFS_FT_REG_FILE	1
60 #define BTRFS_FT_DIR		2
61 #define BTRFS_FT_SYMLINK	7
62 
63 #define ROOT_DIR_WORD 0x002f
64 
65 struct btrfs_dev_item {
66 	__le64 devid;
67 	__le64 total_bytes;
68 	__le64 bytes_used;
69 	__le32 io_align;
70 	__le32 io_width;
71 	__le32 sector_size;
72 	__le64 type;
73 	__le64 generation;
74 	__le64 start_offset;
75 	__le32 dev_group;
76 	u8 seek_speed;
77 	u8 bandwidth;
78 	u8 uuid[BTRFS_UUID_SIZE];
79 	u8 fsid[BTRFS_UUID_SIZE];
80 } __attribute__ ((__packed__));
81 
82 struct btrfs_super_block {
83 	u8 csum[BTRFS_CSUM_SIZE];
84 	/* the first 3 fields must match struct btrfs_header */
85 	u8 fsid[BTRFS_FSID_SIZE];    /* FS specific uuid */
86 	__le64 bytenr; /* this block number */
87 	__le64 flags;
88 
89 	/* allowed to be different from the btrfs_header from here own down */
90 	__le64 magic;
91 	__le64 generation;
92 	__le64 root;
93 	__le64 chunk_root;
94 	__le64 log_root;
95 
96 	/* this will help find the new super based on the log root */
97 	__le64 log_root_transid;
98 	__le64 total_bytes;
99 	__le64 bytes_used;
100 	__le64 root_dir_objectid;
101 	__le64 num_devices;
102 	__le32 sectorsize;
103 	__le32 nodesize;
104 	__le32 leafsize;
105 	__le32 stripesize;
106 	__le32 sys_chunk_array_size;
107 	__le64 chunk_root_generation;
108 	__le64 compat_flags;
109 	__le64 compat_ro_flags;
110 	__le64 incompat_flags;
111 	__le16 csum_type;
112 	u8 root_level;
113 	u8 chunk_root_level;
114 	u8 log_root_level;
115 	struct btrfs_dev_item dev_item;
116 
117 	char label[BTRFS_LABEL_SIZE];
118 
119 	/* future expansion */
120 	__le64 reserved[32];
121 	u8 sys_chunk_array[BTRFS_SYSTEM_CHUNK_ARRAY_SIZE];
122 } __attribute__ ((__packed__));
123 
124 struct btrfs_disk_key {
125 	__le64 objectid;
126 	u8 type;
127 	__le64 offset;
128 } __attribute__ ((__packed__));
129 
130 struct btrfs_stripe {
131 	__le64 devid;
132 	__le64 offset;
133 	u8 dev_uuid[BTRFS_UUID_SIZE];
134 } __attribute__ ((__packed__));
135 
136 struct btrfs_chunk {
137 	__le64 length;
138 	__le64 owner;
139 	__le64 stripe_len;
140 	__le64 type;
141 	__le32 io_align;
142 	__le32 io_width;
143 	__le32 sector_size;
144 	__le16 num_stripes;
145 	__le16 sub_stripes;
146 	struct btrfs_stripe stripe;
147 } __attribute__ ((__packed__));
148 
149 struct btrfs_header {
150 	/* these first four must match the super block */
151 	u8 csum[BTRFS_CSUM_SIZE];
152 	u8 fsid[BTRFS_FSID_SIZE]; /* FS specific uuid */
153 	__le64 bytenr; /* which block this node is supposed to live in */
154 	__le64 flags;
155 
156 	/* allowed to be different from the super from here on down */
157 	u8 chunk_tree_uuid[BTRFS_UUID_SIZE];
158 	__le64 generation;
159 	__le64 owner;
160 	__le32 nritems;
161 	u8 level;
162 } __attribute__ ((__packed__));
163 
164 struct btrfs_item {
165 	struct btrfs_disk_key key;
166 	__le32 offset;
167 	__le32 size;
168 } __attribute__ ((__packed__));
169 
170 struct btrfs_leaf {
171 	struct btrfs_header header;
172 	struct btrfs_item items[];
173 } __attribute__ ((__packed__));
174 
175 struct btrfs_key_ptr {
176 	struct btrfs_disk_key key;
177 	__le64 blockptr;
178 	__le64 generation;
179 } __attribute__ ((__packed__));
180 
181 struct btrfs_node {
182 	struct btrfs_header header;
183 	struct btrfs_key_ptr ptrs[];
184 } __attribute__ ((__packed__));
185 
186 /* remember how we get to a node/leaf */
187 struct btrfs_path {
188 	u64 offsets[BTRFS_MAX_LEVEL];
189 	int itemsnr[BTRFS_MAX_LEVEL];
190 	int slots[BTRFS_MAX_LEVEL];
191 	/* remember last slot's item and data */
192 	struct btrfs_item item;
193 	u8 data[BTRFS_MAX_LEAF_SIZE];
194 };
195 
196 /* store logical offset to physical offset mapping */
197 struct btrfs_chunk_map_item {
198 	u64 logical;
199 	u64 length;
200 	u64 devid;
201 	u64 physical;
202 };
203 
204 struct btrfs_chunk_map {
205 	struct btrfs_chunk_map_item *map;
206 	u32 map_length;
207 	u32 cur_length;
208 };
209 
210 struct btrfs_timespec {
211 	__le64 sec;
212 	__le32 nsec;
213 } __attribute__ ((__packed__));
214 
215 struct btrfs_inode_item {
216 	/* nfs style generation number */
217 	__le64 generation;
218 	/* transid that last touched this inode */
219 	__le64 transid;
220 	__le64 size;
221 	__le64 nbytes;
222 	__le64 block_group;
223 	__le32 nlink;
224 	__le32 uid;
225 	__le32 gid;
226 	__le32 mode;
227 	__le64 rdev;
228 	__le64 flags;
229 
230 	/* modification sequence number for NFS */
231 	__le64 sequence;
232 
233 	/*
234 	 * a little future expansion, for more than this we can
235 	 * just grow the inode item and version it
236 	 */
237 	__le64 reserved[4];
238 	struct btrfs_timespec atime;
239 	struct btrfs_timespec ctime;
240 	struct btrfs_timespec mtime;
241 	struct btrfs_timespec otime;
242 } __attribute__ ((__packed__));
243 
244 struct btrfs_root_item {
245 	struct btrfs_inode_item inode;
246 	__le64 generation;
247 	__le64 root_dirid;
248 	__le64 bytenr;
249 	__le64 byte_limit;
250 	__le64 bytes_used;
251 	__le64 last_snapshot;
252 	__le64 flags;
253 	__le32 refs;
254 	struct btrfs_disk_key drop_progress;
255 	u8 drop_level;
256 	u8 level;
257 } __attribute__ ((__packed__));
258 
259 struct btrfs_dir_item {
260 	struct btrfs_disk_key location;
261 	__le64 transid;
262 	__le16 data_len;
263 	__le16 name_len;
264 	u8 type;
265 } __attribute__ ((__packed__));
266 
267 struct btrfs_file_extent_item {
268 	__le64 generation;
269 	__le64 ram_bytes;
270 	u8 compression;
271 	u8 encryption;
272 	__le16 other_encoding; /* spare for later use */
273 	u8 type;
274 	__le64 disk_bytenr;
275 	__le64 disk_num_bytes;
276 	__le64 offset;
277 	__le64 num_bytes;
278 } __attribute__ ((__packed__));
279 
280 struct btrfs_root_ref {
281 	__le64 dirid;
282 	__le64 sequence;
283 	__le16 name_len;
284 } __attribute__ ((__packed__));
285 
286 /*
287  * btrfs private inode information
288  */
289 struct btrfs_pvt_inode {
290     uint64_t offset;
291 };
292 
293 #define PVT(i) ((struct btrfs_pvt_inode *)((i)->pvt))
294 
295 #endif
296