BTRFS: Implement BTree::Path and change _Find.
[haiku.git] / src / add-ons / kernel / file_systems / btrfs / btrfs.h
blobd2f4518c0049ace5e1522d099a822b7a4bd89c27
1 /*
2 * Copyright 2011, Jérôme Duval, korli@users.berlios.de.
3 * Distributed under the terms of the MIT License.
4 */
5 #ifndef BTRFS_H
6 #define BTRFS_H
9 #include "system_dependencies.h"
12 typedef uint64 fileblock_t; // file block number
13 typedef uint64 fsblock_t; // filesystem block number
15 #define BTRFS_SUPER_BLOCK_OFFSET 0x10000
16 #define BTRFS_NUM_ROOT_BACKUPS 4
19 struct btrfs_backup_roots {
20 uint64 root;
21 uint64 root_generation;
22 uint64 chunk_root;
23 uint64 chunk_root_generation;
24 uint64 extent_root;
25 uint64 extent_root_generation;
26 uint64 fs_root;
27 uint64 fs_root_generation;
28 uint64 device_root;
29 uint64 device_root_generation;
30 uint64 csum_root;
31 uint64 csum_root_generation;
32 uint64 total_size;
33 uint64 used_size;
34 uint64 num_devices;
35 uint8 unused_1[32];
36 uint8 root_level;
37 uint8 chunk_root_level;
38 uint8 extent_root_level;
39 uint8 fs_root_level;
40 uint8 device_root_level;
41 uint8 csum_root_level;
42 uint8 unused_2[10];
44 uint64 Root() const { return B_LENDIAN_TO_HOST_INT64(root); }
45 uint64 RootGen() const
46 { return B_LENDIAN_TO_HOST_INT64(root_generation); }
47 uint64 ChunkRoot() const { return B_LENDIAN_TO_HOST_INT64(chunk_root); }
48 uint64 ChunkRootGen() const
49 { return B_LENDIAN_TO_HOST_INT64(chunk_root_generation); }
50 uint64 ExtentRoot() const { return B_LENDIAN_TO_HOST_INT64(extent_root); }
51 uint64 ExtentRootGen() const
52 { return B_LENDIAN_TO_HOST_INT64(extent_root_generation); }
53 uint64 FSRoot() const { return B_LENDIAN_TO_HOST_INT64(fs_root); }
54 uint64 FSRootGen() const
55 { return B_LENDIAN_TO_HOST_INT64(fs_root_generation); }
56 uint64 DeviceRoot() const { return B_LENDIAN_TO_HOST_INT64(device_root); }
57 uint64 DeviceRootGen() const
58 { return B_LENDIAN_TO_HOST_INT64(device_root_generation); }
59 uint64 CSumRoot() const { return B_LENDIAN_TO_HOST_INT64(csum_root); }
60 uint64 CSumRootGen() const
61 { return B_LENDIAN_TO_HOST_INT64(csum_root_generation); }
62 uint8 RootLevel() const { return root_level; }
63 uint8 ChunkRootLevel() const { return chunk_root_level; }
64 uint8 ExtentRootLevel() const { return extent_root_level; }
65 uint8 FSRootLevel() const { return fs_root_level; }
66 uint8 DeviceRootLevel() const { return device_root_level; }
67 uint8 CSumRootLevel() const { return csum_root_level; }
68 } _PACKED;
71 struct btrfs_key {
72 uint64 object_id;
73 uint8 type;
74 uint64 offset;
76 uint64 ObjectID() const { return B_LENDIAN_TO_HOST_INT64(object_id); }
77 uint8 Type() const { return type; }
78 uint64 Offset() const { return B_LENDIAN_TO_HOST_INT64(offset); }
79 void SetObjectID(uint64 id) { object_id = B_HOST_TO_LENDIAN_INT64(id); }
80 void SetType(uint8 key_type) { type = key_type; }
81 void SetOffset(uint64 off) { offset = B_HOST_TO_LENDIAN_INT64(off); }
82 int32 Compare(const btrfs_key& key) const;
83 //implemented in BTree.cpp
84 } _PACKED;
87 struct btrfs_timespec {
88 uint64 seconds;
89 uint32 nanoseconds;
90 } _PACKED;
93 struct btrfs_header {
94 uint8 checksum[32];
95 uint8 fsid[16];
96 uint64 logical_address;
97 uint64 flags;
98 uint8 chunk_tree_uuid[16];
99 uint64 generation;
100 uint64 owner;
101 uint32 item_count;
102 uint8 level;
103 uint64 LogicalAddress() const
104 { return B_LENDIAN_TO_HOST_INT64(logical_address); }
105 uint64 Flags() const { return B_LENDIAN_TO_HOST_INT64(flags); }
106 uint64 Generation() const
107 { return B_LENDIAN_TO_HOST_INT64(generation); }
108 uint64 Owner() const
109 { return B_LENDIAN_TO_HOST_INT64(owner); }
110 uint32 ItemCount() const
111 { return B_LENDIAN_TO_HOST_INT32(item_count); }
112 uint8 Level() const { return level; }
113 } _PACKED;
116 struct btrfs_index {
117 btrfs_key key;
118 uint64 logical_address;
119 uint64 generation;
120 uint64 LogicalAddress() const
121 { return B_LENDIAN_TO_HOST_INT64(logical_address); }
122 uint64 Generation() const
123 { return B_LENDIAN_TO_HOST_INT64(generation); }
124 } _PACKED;
127 struct btrfs_entry {
128 btrfs_key key;
129 uint32 offset;
130 uint32 size;
131 uint32 Offset() const
132 { return B_LENDIAN_TO_HOST_INT32(offset); }
133 uint32 Size() const
134 { return B_LENDIAN_TO_HOST_INT32(size); }
135 } _PACKED;
138 struct btrfs_stream {
139 btrfs_header header;
140 union {
141 btrfs_entry entries[0];
142 btrfs_index index[0];
144 } _PACKED;
147 struct btrfs_stripe {
148 uint64 device_id;
149 uint64 offset;
150 uint8 device_uuid[16];
151 uint64 DeviceID() const { return B_LENDIAN_TO_HOST_INT64(device_id); }
152 uint64 Offset() const { return B_LENDIAN_TO_HOST_INT64(offset); }
153 } _PACKED;
156 struct btrfs_chunk {
157 uint64 length;
158 uint64 owner;
159 uint64 stripe_length;
160 uint64 type;
161 uint32 io_align;
162 uint32 io_width;
163 uint32 sector_size;
164 uint16 stripe_count;
165 uint16 sub_stripes;
166 btrfs_stripe stripes[0];
167 uint64 Length() const { return B_LENDIAN_TO_HOST_INT64(length); }
168 uint64 Owner() const { return B_LENDIAN_TO_HOST_INT64(owner); }
169 uint64 StripeLength() const
170 { return B_LENDIAN_TO_HOST_INT64(stripe_length); }
171 uint64 Type() const { return B_LENDIAN_TO_HOST_INT64(type); }
172 uint32 IOAlign() const { return B_LENDIAN_TO_HOST_INT32(io_align); }
173 uint32 IOWidth() const { return B_LENDIAN_TO_HOST_INT32(io_width); }
174 uint32 SectorSize() const
175 { return B_LENDIAN_TO_HOST_INT32(sector_size); }
176 uint16 StripeCount() const
177 { return B_LENDIAN_TO_HOST_INT16(stripe_count); }
178 uint16 SubStripes() const
179 { return B_LENDIAN_TO_HOST_INT16(sub_stripes); }
180 } _PACKED;
183 struct btrfs_device {
184 uint64 id;
185 uint64 total_size;
186 uint64 used_size;
187 uint32 io_align;
188 uint32 io_width;
189 uint32 sector_size;
190 uint64 type;
191 uint64 generation;
192 uint64 start_offset;
193 uint32 group;
194 uint8 seek_speed;
195 uint8 bandwidth;
196 uint8 uuid[16];
197 uint8 fsid[16];
198 } _PACKED;
201 struct btrfs_super_block {
202 uint8 checksum[32];
203 uint8 fsid[16];
204 uint64 blocknum;
205 uint64 flags;
206 char magic[8];
207 uint64 generation;
208 uint64 root;
209 uint64 chunk_root;
210 uint64 log_root;
211 uint64 log_root_transaction_id;
212 uint64 total_size;
213 uint64 used_size;
214 uint64 root_dir_object_id;
215 uint64 num_devices;
216 uint32 sector_size;
217 uint32 node_size;
218 uint32 leaf_size;
219 uint32 stripe_size;
220 uint32 system_chunk_array_size;
221 uint64 chunk_root_generation;
222 uint64 compat_flags;
223 uint64 readonly_flags;
224 uint64 incompat_flags;
225 uint16 checksum_type;
226 uint8 root_level;
227 uint8 chunk_root_level;
228 uint8 log_root_level;
229 btrfs_device device;
230 char label[256];
231 uint64 reserved[32];
232 uint8 system_chunk_array[2048];
233 btrfs_backup_roots backup_roots[BTRFS_NUM_ROOT_BACKUPS];
235 bool IsValid();
236 // implemented in Volume.cpp
237 uint64 TotalSize() const { return B_LENDIAN_TO_HOST_INT64(total_size); }
238 uint32 BlockSize() const { return B_LENDIAN_TO_HOST_INT32(node_size); }
239 uint32 SectorSize() const { return B_LENDIAN_TO_HOST_INT32(sector_size); }
240 uint64 RootDirObjectID() const
241 { return B_LENDIAN_TO_HOST_INT64(root_dir_object_id); }
242 uint64 Generation() const
243 { return B_LENDIAN_TO_HOST_INT64(generation); }
244 uint64 Root() const
245 { return B_LENDIAN_TO_HOST_INT64(root); }
246 uint64 ChunkRoot() const
247 { return B_LENDIAN_TO_HOST_INT64(chunk_root); }
248 uint64 LogRoot() const
249 { return B_LENDIAN_TO_HOST_INT64(log_root); }
250 uint8 ChunkRootLevel() const { return chunk_root_level; }
251 } _PACKED;
254 struct btrfs_inode {
255 uint64 generation;
256 uint64 transaction_id;
257 uint64 size;
258 uint64 nbytes;
259 uint64 blockgroup;
260 uint32 num_links;
261 uint32 uid;
262 uint32 gid;
263 uint32 mode;
264 uint64 rdev;
265 uint64 flags;
266 uint64 sequence;
267 uint64 reserved[4];
268 btrfs_timespec access_time;
269 btrfs_timespec change_time;
270 btrfs_timespec modification_time;
271 btrfs_timespec creation_time;
272 uint64 Generation() const { return B_LENDIAN_TO_HOST_INT64(generation); }
273 uint64 Size() const { return B_LENDIAN_TO_HOST_INT64(size); }
274 uint32 UserID() const { return B_LENDIAN_TO_HOST_INT32(uid); }
275 uint32 GroupID() const { return B_LENDIAN_TO_HOST_INT32(gid); }
276 uint32 Mode() const { return B_LENDIAN_TO_HOST_INT32(mode); }
277 uint64 Flags() const { return B_LENDIAN_TO_HOST_INT64(flags); }
278 uint64 Sequence() const { return B_LENDIAN_TO_HOST_INT64(sequence); }
279 static void _DecodeTime(struct timespec& timespec,
280 const btrfs_timespec& time)
282 timespec.tv_sec = B_LENDIAN_TO_HOST_INT64(time.seconds);
283 timespec.tv_nsec = B_LENDIAN_TO_HOST_INT32(time.nanoseconds);
285 void GetAccessTime(struct timespec& timespec) const
286 { _DecodeTime(timespec, access_time); }
287 void GetChangeTime(struct timespec& timespec) const
288 { _DecodeTime(timespec, change_time); }
289 void GetModificationTime(struct timespec& timespec) const
290 { _DecodeTime(timespec, modification_time); }
291 void GetCreationTime(struct timespec& timespec) const
292 { _DecodeTime(timespec, creation_time); }
293 } _PACKED;
296 struct btrfs_inode_ref {
297 uint8 index;
298 uint16 name_length;
299 uint8 name[];
301 uint8 Index() const { return index; }
302 uint16 NameLength() const { return B_LENDIAN_TO_HOST_INT16(name_length); }
303 } _PACKED;
306 struct btrfs_root {
307 btrfs_inode inode;
308 uint64 generation;
309 uint64 root_dirid;
310 uint64 logical_address;
311 uint64 limit_bytes;
312 uint64 used_bytes;
313 uint64 last_snapshot;
314 uint64 flags;
315 uint32 refs;
316 btrfs_key drop_progress;
317 uint8 drop_level;
318 uint8 level;
319 uint64 Generation() const
320 { return B_LENDIAN_TO_HOST_INT64(generation); }
321 uint64 LogicalAddress() const
322 { return B_LENDIAN_TO_HOST_INT64(logical_address); }
323 } _PACKED;
326 struct btrfs_dir_entry {
327 btrfs_key location;
328 uint64 transaction_id;
329 uint16 data_length;
330 uint16 name_length;
331 uint8 type;
332 uint16 DataLength() const { return B_LENDIAN_TO_HOST_INT16(data_length); }
333 uint16 NameLength() const { return B_LENDIAN_TO_HOST_INT16(name_length); }
334 ino_t InodeID() const { return location.ObjectID(); }
335 uint16 Length() const
336 { return sizeof(*this) + NameLength() + DataLength(); }
337 } _PACKED;
340 struct btrfs_extent_data {
341 uint64 generation;
342 uint64 memory_size;
343 uint8 compression;
344 uint8 encryption;
345 uint16 reserved;
346 uint8 type;
347 union {
348 struct {
349 uint64 disk_offset;
350 uint64 disk_size;
351 uint64 extent_offset;
352 uint64 size;
354 uint8 inline_data[0];
356 uint64 Generation() const
357 { return B_LENDIAN_TO_HOST_INT64(generation); }
358 uint64 MemoryBytes() const
359 { return B_LENDIAN_TO_HOST_INT64(memory_size); }
360 uint8 Compression() const { return compression; }
361 uint8 Type() const { return type; }
362 uint64 DiskOffset() const
363 { return B_LENDIAN_TO_HOST_INT64(disk_offset); }
364 uint64 DiskSize() const
365 { return B_LENDIAN_TO_HOST_INT64(disk_size); }
366 uint64 ExtentOffset() const
367 { return B_LENDIAN_TO_HOST_INT64(extent_offset); }
368 uint64 Size() const
369 { return B_LENDIAN_TO_HOST_INT64(size); }
370 } _PACKED;
373 struct btrfs_block_group {
374 uint64 used_space;
375 uint64 chunk_object_id;
376 uint64 flags;
378 uint64 UsedSpace() const { return B_LENDIAN_TO_HOST_INT64(used_space); }
379 uint64 ChunkObjectID() const
380 { return B_HOST_TO_LENDIAN_INT64(chunk_object_id); }
381 uint64 Flags() const { return B_LENDIAN_TO_HOST_INT64(flags); }
382 } _PACKED;
385 struct btrfs_extent {
386 uint64 refs;
387 uint64 generation;
388 uint64 flags;
390 uint64 RefCount() const { return B_LENDIAN_TO_HOST_INT64(refs); }
391 uint64 Generation() const { return B_LENDIAN_TO_HOST_INT64(generation); }
392 uint64 Flags() const { return B_LENDIAN_TO_HOST_INT64(flags); }
393 } _PACKED;
396 struct btrfs_extent_inline_ref {
397 uint8 type;
398 uint64 offset;
400 uint8 Type() const { return type; }
401 uint64 Offset() const { return B_LENDIAN_TO_HOST_INT64(offset); }
402 } _PACKED;
405 struct btrfs_extent_data_ref {
406 uint64 root_id;
407 uint64 inode_id;
408 uint64 offset;
409 uint32 ref_count;
411 uint64 RootID() const { return B_LENDIAN_TO_HOST_INT64(root_id); }
412 uint64 InodeID() const { return B_LENDIAN_TO_HOST_INT64(inode_id); }
413 uint64 Offset() const { return B_LENDIAN_TO_HOST_INT64(offset);}
414 uint32 RefCount() const { return B_LENDIAN_TO_HOST_INT32(ref_count); }
415 } _PACKED;
418 #define BTRFS_SUPER_BLOCK_MAGIC "_BHRfS_M"
419 #define BTRFS_FIRST_SUBVOLUME 256
421 #define BTRFS_OBJECT_ID_ROOT_TREE 1
422 #define BTRFS_OBJECT_ID_EXTENT_TREE 2
423 #define BTRFS_OBJECT_ID_CHUNK_TREE 3
424 #define BTRFS_OBJECT_ID_DEV_TREE 4
425 #define BTRFS_OBJECT_ID_FS_TREE 5
426 #define BTRFS_OBJECT_ID_ROOT_TREE_DIR 6
427 #define BTRFS_OBJECT_ID_CHECKSUM_TREE 7
428 #define BTRFS_OBJECT_ID_FIRST_CHUNK_TREE 256
430 #define BTRFS_KEY_TYPE_ANY 0
431 #define BTRFS_KEY_TYPE_INODE_ITEM 1
432 #define BTRFS_KEY_TYPE_INODE_REF 12
433 #define BTRFS_KEY_TYPE_XATTR_ITEM 24
434 #define BTRFS_KEY_TYPE_DIR_ITEM 84
435 #define BTRFS_KEY_TYPE_DIR_INDEX 96
436 #define BTRFS_KEY_TYPE_EXTENT_DATA 108
437 #define BTRFS_KEY_TYPE_ROOT_ITEM 132
438 #define BTRFS_KEY_TYPE_EXTENT_ITEM 168
439 #define BTRFS_KEY_TYPE_METADATA_ITEM 169
440 #define BTRFS_KEY_TYPE_EXTENT_DATA_REF 178
441 #define BTRFS_KEY_TYPE_BLOCKGROUP_ITEM 192
442 #define BTRFS_KEY_TYPE_CHUNK_ITEM 228
444 #define BTRFS_EXTENT_COMPRESS_NONE 0
445 #define BTRFS_EXTENT_COMPRESS_ZLIB 1
446 #define BTRFS_EXTENT_COMPRESS_LZO 2
447 #define BTRFS_EXTENT_DATA_INLINE 0
448 #define BTRFS_EXTENT_DATA_REGULAR 1
449 #define BTRFS_EXTENT_DATA_PRE 2
450 #define BTRFS_EXTENT_FLAG_DATA 1
451 #define BTRFS_EXTENT_FLAG_TREE_BLOCK 2
453 #define BTRFS_BLOCKGROUP_FLAG_DATA 1
454 #define BTRFS_BLOCKGROUP_FLAG_SYSTEM 2
455 #define BTRFS_BLOCKGROUP_FLAG_METADA 4
456 #define BTRFS_BLOCKGROUP_FLAG_RAID0 8
457 #define BTRFS_BLOCKGROUP_FLAG_RAID1 16
458 #define BTRFS_BLOCKGROUP_FLAG_DUP 32
459 #define BTRFS_BLOCKGROUP_FLAG_RAID10 64
460 #define BTRFS_BLOCKGROUP_FLAG_RAID5 128
461 #define BTRFS_BLOCKGROUP_FLAG_RAID6 256
462 #define BTRFS_BLOCKGROUP_FLAG_MASK 511
465 struct file_cookie {
466 bigtime_t last_notification;
467 off_t last_size;
468 int open_mode;
472 #define BTRFS_OPEN_MODE_USER_MASK 0x7fffffff
474 extern fs_volume_ops gBtrfsVolumeOps;
475 extern fs_vnode_ops gBtrfsVnodeOps;
478 #endif // BTRFS_H