btrfs: move btrfs on-disk definitions out of ctree.h
[linux-2.6-block.git] / include / uapi / linux / btrfs_tree.h
CommitLineData
6f52b16c 1/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
db671160
JM
2#ifndef _BTRFS_CTREE_H_
3#define _BTRFS_CTREE_H_
4
3a4e7f56 5#include <linux/btrfs.h>
9078b4ee 6#include <linux/types.h>
1465af12
QW
7#ifdef __KERNEL__
8#include <linux/stddef.h>
9#else
10#include <stddef.h>
11#endif
9078b4ee 12
4300c58f
JB
13/* ASCII for _BHRfS_M, no terminating nul */
14#define BTRFS_MAGIC 0x4D5F53665248425FULL
15
16#define BTRFS_MAX_LEVEL 8
17
db671160
JM
18/*
19 * This header contains the structure definitions and constants used
20 * by file system objects that can be retrieved using
21 * the BTRFS_IOC_SEARCH_TREE ioctl. That means basically anything that
22 * is needed to describe a leaf node's key or item contents.
23 */
24
25/* holds pointers to all of the tree roots */
26#define BTRFS_ROOT_TREE_OBJECTID 1ULL
27
28/* stores information about which extents are in use, and reference counts */
29#define BTRFS_EXTENT_TREE_OBJECTID 2ULL
30
31/*
32 * chunk tree stores translations from logical -> physical block numbering
33 * the super block points to the chunk tree
34 */
35#define BTRFS_CHUNK_TREE_OBJECTID 3ULL
36
37/*
38 * stores information about which areas of a given device are in use.
39 * one per device. The tree of tree roots points to the device tree
40 */
41#define BTRFS_DEV_TREE_OBJECTID 4ULL
42
43/* one per subvolume, storing files and directories */
44#define BTRFS_FS_TREE_OBJECTID 5ULL
45
46/* directory objectid inside the root tree */
47#define BTRFS_ROOT_TREE_DIR_OBJECTID 6ULL
48
49/* holds checksums of all the data extents */
50#define BTRFS_CSUM_TREE_OBJECTID 7ULL
51
52/* holds quota configuration and tracking */
53#define BTRFS_QUOTA_TREE_OBJECTID 8ULL
54
55/* for storing items that use the BTRFS_UUID_KEY* types */
56#define BTRFS_UUID_TREE_OBJECTID 9ULL
57
58/* tracks free space in block groups. */
59#define BTRFS_FREE_SPACE_TREE_OBJECTID 10ULL
60
9c54e80d
JB
61/* Holds the block group items for extent tree v2. */
62#define BTRFS_BLOCK_GROUP_TREE_OBJECTID 11ULL
63
db671160
JM
64/* device stats in the device tree */
65#define BTRFS_DEV_STATS_OBJECTID 0ULL
66
67/* for storing balance parameters in the root tree */
68#define BTRFS_BALANCE_OBJECTID -4ULL
69
1a9fd417 70/* orphan objectid for tracking unlinked/truncated files */
db671160
JM
71#define BTRFS_ORPHAN_OBJECTID -5ULL
72
73/* does write ahead logging to speed up fsyncs */
74#define BTRFS_TREE_LOG_OBJECTID -6ULL
75#define BTRFS_TREE_LOG_FIXUP_OBJECTID -7ULL
76
77/* for space balancing */
78#define BTRFS_TREE_RELOC_OBJECTID -8ULL
79#define BTRFS_DATA_RELOC_TREE_OBJECTID -9ULL
80
81/*
82 * extent checksums all have this objectid
83 * this allows them to share the logging tree
84 * for fsyncs
85 */
86#define BTRFS_EXTENT_CSUM_OBJECTID -10ULL
87
88/* For storing free space cache */
89#define BTRFS_FREE_SPACE_OBJECTID -11ULL
90
91/*
92 * The inode number assigned to the special inode for storing
93 * free ino cache
94 */
95#define BTRFS_FREE_INO_OBJECTID -12ULL
96
97/* dummy objectid represents multiple objectids */
98#define BTRFS_MULTIPLE_OBJECTIDS -255ULL
99
100/*
101 * All files have objectids in this range.
102 */
103#define BTRFS_FIRST_FREE_OBJECTID 256ULL
104#define BTRFS_LAST_FREE_OBJECTID -256ULL
105#define BTRFS_FIRST_CHUNK_TREE_OBJECTID 256ULL
106
107
108/*
109 * the device items go into the chunk tree. The key is in the form
110 * [ 1 BTRFS_DEV_ITEM_KEY device_id ]
111 */
112#define BTRFS_DEV_ITEMS_OBJECTID 1ULL
113
114#define BTRFS_BTREE_INODE_OBJECTID 1
115
116#define BTRFS_EMPTY_SUBVOL_DIR_OBJECTID 2
117
118#define BTRFS_DEV_REPLACE_DEVID 0ULL
119
120/*
121 * inode items have the data typically returned from stat and store other
122 * info about object characteristics. There is one for every file and dir in
123 * the FS
124 */
125#define BTRFS_INODE_ITEM_KEY 1
126#define BTRFS_INODE_REF_KEY 12
127#define BTRFS_INODE_EXTREF_KEY 13
128#define BTRFS_XATTR_ITEM_KEY 24
14605409
BB
129
130/*
131 * fs verity items are stored under two different key types on disk.
132 * The descriptor items:
133 * [ inode objectid, BTRFS_VERITY_DESC_ITEM_KEY, offset ]
134 *
135 * At offset 0, we store a btrfs_verity_descriptor_item which tracks the size
136 * of the descriptor item and some extra data for encryption.
137 * Starting at offset 1, these hold the generic fs verity descriptor. The
138 * latter are opaque to btrfs, we just read and write them as a blob for the
139 * higher level verity code. The most common descriptor size is 256 bytes.
140 *
141 * The merkle tree items:
142 * [ inode objectid, BTRFS_VERITY_MERKLE_ITEM_KEY, offset ]
143 *
144 * These also start at offset 0, and correspond to the merkle tree bytes. When
145 * fsverity asks for page 0 of the merkle tree, we pull up one page starting at
146 * offset 0 for this key type. These are also opaque to btrfs, we're blindly
147 * storing whatever fsverity sends down.
148 */
149#define BTRFS_VERITY_DESC_ITEM_KEY 36
150#define BTRFS_VERITY_MERKLE_ITEM_KEY 37
151
db671160
JM
152#define BTRFS_ORPHAN_ITEM_KEY 48
153/* reserve 2-15 close to the inode for later flexibility */
154
155/*
156 * dir items are the name -> inode pointers in a directory. There is one
ccae4a19
FM
157 * for every name in a directory. BTRFS_DIR_LOG_ITEM_KEY is no longer used
158 * but it's still defined here for documentation purposes and to help avoid
159 * having its numerical value reused in the future.
db671160
JM
160 */
161#define BTRFS_DIR_LOG_ITEM_KEY 60
162#define BTRFS_DIR_LOG_INDEX_KEY 72
163#define BTRFS_DIR_ITEM_KEY 84
164#define BTRFS_DIR_INDEX_KEY 96
165/*
166 * extent data is for file data
167 */
168#define BTRFS_EXTENT_DATA_KEY 108
169
170/*
171 * extent csums are stored in a separate tree and hold csums for
172 * an entire extent on disk.
173 */
174#define BTRFS_EXTENT_CSUM_KEY 128
175
176/*
177 * root items point to tree roots. They are typically in the root
178 * tree used by the super block to find all the other trees
179 */
180#define BTRFS_ROOT_ITEM_KEY 132
181
182/*
183 * root backrefs tie subvols and snapshots to the directory entries that
184 * reference them
185 */
186#define BTRFS_ROOT_BACKREF_KEY 144
187
188/*
189 * root refs make a fast index for listing all of the snapshots and
190 * subvolumes referenced by a given root. They point directly to the
191 * directory item in the root that references the subvol
192 */
193#define BTRFS_ROOT_REF_KEY 156
194
195/*
196 * extent items are in the extent map tree. These record which blocks
197 * are used, and how many references there are to each block
198 */
199#define BTRFS_EXTENT_ITEM_KEY 168
200
201/*
202 * The same as the BTRFS_EXTENT_ITEM_KEY, except it's metadata we already know
203 * the length, so we save the level in key->offset instead of the length.
204 */
205#define BTRFS_METADATA_ITEM_KEY 169
206
207#define BTRFS_TREE_BLOCK_REF_KEY 176
208
209#define BTRFS_EXTENT_DATA_REF_KEY 178
210
211#define BTRFS_EXTENT_REF_V0_KEY 180
212
213#define BTRFS_SHARED_BLOCK_REF_KEY 182
214
215#define BTRFS_SHARED_DATA_REF_KEY 184
216
217/*
218 * block groups give us hints into the extent allocation trees. Which
219 * blocks are free etc etc
220 */
221#define BTRFS_BLOCK_GROUP_ITEM_KEY 192
222
223/*
224 * Every block group is represented in the free space tree by a free space info
225 * item, which stores some accounting information. It is keyed on
226 * (block_group_start, FREE_SPACE_INFO, block_group_length).
227 */
228#define BTRFS_FREE_SPACE_INFO_KEY 198
229
230/*
231 * A free space extent tracks an extent of space that is free in a block group.
232 * It is keyed on (start, FREE_SPACE_EXTENT, length).
233 */
234#define BTRFS_FREE_SPACE_EXTENT_KEY 199
235
236/*
237 * When a block group becomes very fragmented, we convert it to use bitmaps
238 * instead of extents. A free space bitmap is keyed on
239 * (start, FREE_SPACE_BITMAP, length); the corresponding item is a bitmap with
240 * (length / sectorsize) bits.
241 */
242#define BTRFS_FREE_SPACE_BITMAP_KEY 200
243
244#define BTRFS_DEV_EXTENT_KEY 204
245#define BTRFS_DEV_ITEM_KEY 216
246#define BTRFS_CHUNK_ITEM_KEY 228
247
248/*
249 * Records the overall state of the qgroups.
250 * There's only one instance of this key present,
251 * (0, BTRFS_QGROUP_STATUS_KEY, 0)
252 */
253#define BTRFS_QGROUP_STATUS_KEY 240
254/*
255 * Records the currently used space of the qgroup.
256 * One key per qgroup, (0, BTRFS_QGROUP_INFO_KEY, qgroupid).
257 */
258#define BTRFS_QGROUP_INFO_KEY 242
259/*
260 * Contains the user configured limits for the qgroup.
261 * One key per qgroup, (0, BTRFS_QGROUP_LIMIT_KEY, qgroupid).
262 */
263#define BTRFS_QGROUP_LIMIT_KEY 244
264/*
265 * Records the child-parent relationship of qgroups. For
266 * each relation, 2 keys are present:
267 * (childid, BTRFS_QGROUP_RELATION_KEY, parentid)
268 * (parentid, BTRFS_QGROUP_RELATION_KEY, childid)
269 */
270#define BTRFS_QGROUP_RELATION_KEY 246
271
272/*
273 * Obsolete name, see BTRFS_TEMPORARY_ITEM_KEY.
274 */
275#define BTRFS_BALANCE_ITEM_KEY 248
276
277/*
278 * The key type for tree items that are stored persistently, but do not need to
279 * exist for extended period of time. The items can exist in any tree.
280 *
281 * [subtype, BTRFS_TEMPORARY_ITEM_KEY, data]
282 *
283 * Existing items:
284 *
285 * - balance status item
286 * (BTRFS_BALANCE_OBJECTID, BTRFS_TEMPORARY_ITEM_KEY, 0)
287 */
288#define BTRFS_TEMPORARY_ITEM_KEY 248
289
290/*
291 * Obsolete name, see BTRFS_PERSISTENT_ITEM_KEY
292 */
293#define BTRFS_DEV_STATS_KEY 249
294
295/*
296 * The key type for tree items that are stored persistently and usually exist
297 * for a long period, eg. filesystem lifetime. The item kinds can be status
298 * information, stats or preference values. The item can exist in any tree.
299 *
300 * [subtype, BTRFS_PERSISTENT_ITEM_KEY, data]
301 *
302 * Existing items:
303 *
304 * - device statistics, store IO stats in the device tree, one key for all
305 * stats
306 * (BTRFS_DEV_STATS_OBJECTID, BTRFS_DEV_STATS_KEY, 0)
307 */
308#define BTRFS_PERSISTENT_ITEM_KEY 249
309
310/*
1a9fd417 311 * Persistently stores the device replace state in the device tree.
db671160
JM
312 * The key is built like this: (0, BTRFS_DEV_REPLACE_KEY, 0).
313 */
314#define BTRFS_DEV_REPLACE_KEY 250
315
316/*
317 * Stores items that allow to quickly map UUIDs to something else.
318 * These items are part of the filesystem UUID tree.
319 * The key is built like this:
320 * (UUID_upper_64_bits, BTRFS_UUID_KEY*, UUID_lower_64_bits).
321 */
322#if BTRFS_UUID_SIZE != 16
323#error "UUID items require BTRFS_UUID_SIZE == 16!"
324#endif
325#define BTRFS_UUID_KEY_SUBVOL 251 /* for UUIDs assigned to subvols */
326#define BTRFS_UUID_KEY_RECEIVED_SUBVOL 252 /* for UUIDs assigned to
327 * received subvols */
328
329/*
330 * string items are for debugging. They just store a short string of
331 * data in the FS
332 */
333#define BTRFS_STRING_ITEM_KEY 253
334
deb67895
QW
335/* Maximum metadata block size (nodesize) */
336#define BTRFS_MAX_METADATA_BLOCKSIZE 65536
db671160
JM
337
338/* 32 bytes in various csum fields */
339#define BTRFS_CSUM_SIZE 32
340
341/* csum types */
e35b79a1
JT
342enum btrfs_csum_type {
343 BTRFS_CSUM_TYPE_CRC32 = 0,
3951e7f0 344 BTRFS_CSUM_TYPE_XXHASH = 1,
3831bf00 345 BTRFS_CSUM_TYPE_SHA256 = 2,
352ae07b 346 BTRFS_CSUM_TYPE_BLAKE2 = 3,
e35b79a1 347};
db671160
JM
348
349/*
350 * flags definitions for directory entry item type
351 *
352 * Used by:
353 * struct btrfs_dir_item.type
7d157c3d
PP
354 *
355 * Values 0..7 must match common file type values in fs_types.h.
db671160
JM
356 */
357#define BTRFS_FT_UNKNOWN 0
358#define BTRFS_FT_REG_FILE 1
359#define BTRFS_FT_DIR 2
360#define BTRFS_FT_CHRDEV 3
361#define BTRFS_FT_BLKDEV 4
362#define BTRFS_FT_FIFO 5
363#define BTRFS_FT_SOCK 6
364#define BTRFS_FT_SYMLINK 7
365#define BTRFS_FT_XATTR 8
366#define BTRFS_FT_MAX 9
367
4300c58f
JB
368/*
369 * Inode flags
370 */
371#define BTRFS_INODE_NODATASUM (1U << 0)
372#define BTRFS_INODE_NODATACOW (1U << 1)
373#define BTRFS_INODE_READONLY (1U << 2)
374#define BTRFS_INODE_NOCOMPRESS (1U << 3)
375#define BTRFS_INODE_PREALLOC (1U << 4)
376#define BTRFS_INODE_SYNC (1U << 5)
377#define BTRFS_INODE_IMMUTABLE (1U << 6)
378#define BTRFS_INODE_APPEND (1U << 7)
379#define BTRFS_INODE_NODUMP (1U << 8)
380#define BTRFS_INODE_NOATIME (1U << 9)
381#define BTRFS_INODE_DIRSYNC (1U << 10)
382#define BTRFS_INODE_COMPRESS (1U << 11)
383
384#define BTRFS_INODE_ROOT_ITEM_INIT (1U << 31)
385
386#define BTRFS_INODE_FLAG_MASK \
387 (BTRFS_INODE_NODATASUM | \
388 BTRFS_INODE_NODATACOW | \
389 BTRFS_INODE_READONLY | \
390 BTRFS_INODE_NOCOMPRESS | \
391 BTRFS_INODE_PREALLOC | \
392 BTRFS_INODE_SYNC | \
393 BTRFS_INODE_IMMUTABLE | \
394 BTRFS_INODE_APPEND | \
395 BTRFS_INODE_NODUMP | \
396 BTRFS_INODE_NOATIME | \
397 BTRFS_INODE_DIRSYNC | \
398 BTRFS_INODE_COMPRESS | \
399 BTRFS_INODE_ROOT_ITEM_INIT)
400
401#define BTRFS_INODE_RO_VERITY (1U << 0)
402
403#define BTRFS_INODE_RO_FLAG_MASK (BTRFS_INODE_RO_VERITY)
404
db671160
JM
405/*
406 * The key defines the order in the tree, and so it also defines (optimal)
407 * block layout.
408 *
409 * objectid corresponds to the inode number.
410 *
411 * type tells us things about the object, and is a kind of stream selector.
412 * so for a given inode, keys with type of 1 might refer to the inode data,
413 * type of 2 may point to file data in the btree and type == 3 may point to
414 * extents.
415 *
416 * offset is the starting byte offset for this key in the stream.
417 *
418 * btrfs_disk_key is in disk byte order. struct btrfs_key is always
419 * in cpu native order. Otherwise they are identical and their sizes
420 * should be the same (ie both packed)
421 */
422struct btrfs_disk_key {
423 __le64 objectid;
14b05c51 424 __u8 type;
db671160
JM
425 __le64 offset;
426} __attribute__ ((__packed__));
427
428struct btrfs_key {
14b05c51
JM
429 __u64 objectid;
430 __u8 type;
431 __u64 offset;
db671160
JM
432} __attribute__ ((__packed__));
433
4300c58f
JB
434/*
435 * Every tree block (leaf or node) starts with this header.
436 */
437struct btrfs_header {
438 /* These first four must match the super block */
439 __u8 csum[BTRFS_CSUM_SIZE];
440 /* FS specific uuid */
441 __u8 fsid[BTRFS_FSID_SIZE];
442 /* Which block this node is supposed to live in */
443 __le64 bytenr;
444 __le64 flags;
445
446 /* Allowed to be different from the super from here on down */
447 __u8 chunk_tree_uuid[BTRFS_UUID_SIZE];
448 __le64 generation;
449 __le64 owner;
450 __le32 nritems;
451 __u8 level;
452} __attribute__ ((__packed__));
453
454/*
455 * This is a very generous portion of the super block, giving us room to
456 * translate 14 chunks with 3 stripes each.
457 */
458#define BTRFS_SYSTEM_CHUNK_ARRAY_SIZE 2048
459
460/*
461 * Just in case we somehow lose the roots and are not able to mount, we store
462 * an array of the roots from previous transactions in the super.
463 */
464#define BTRFS_NUM_BACKUP_ROOTS 4
465struct btrfs_root_backup {
466 __le64 tree_root;
467 __le64 tree_root_gen;
468
469 __le64 chunk_root;
470 __le64 chunk_root_gen;
471
472 __le64 extent_root;
473 __le64 extent_root_gen;
474
475 __le64 fs_root;
476 __le64 fs_root_gen;
477
478 __le64 dev_root;
479 __le64 dev_root_gen;
480
481 __le64 csum_root;
482 __le64 csum_root_gen;
483
484 __le64 total_bytes;
485 __le64 bytes_used;
486 __le64 num_devices;
487 /* future */
488 __le64 unused_64[4];
489
490 __u8 tree_root_level;
491 __u8 chunk_root_level;
492 __u8 extent_root_level;
493 __u8 fs_root_level;
494 __u8 dev_root_level;
495 __u8 csum_root_level;
496 /* future and to align */
497 __u8 unused_8[10];
498} __attribute__ ((__packed__));
499
500/*
501 * A leaf is full of items. offset and size tell us where to find the item in
502 * the leaf (relative to the start of the data area)
503 */
504struct btrfs_item {
505 struct btrfs_disk_key key;
506 __le32 offset;
507 __le32 size;
508} __attribute__ ((__packed__));
509
510/*
511 * Leaves have an item area and a data area:
512 * [item0, item1....itemN] [free space] [dataN...data1, data0]
513 *
514 * The data is separate from the items to get the keys closer together during
515 * searches.
516 */
517struct btrfs_leaf {
518 struct btrfs_header header;
519 struct btrfs_item items[];
520} __attribute__ ((__packed__));
521
522/*
523 * All non-leaf blocks are nodes, they hold only keys and pointers to other
524 * blocks.
525 */
526struct btrfs_key_ptr {
527 struct btrfs_disk_key key;
528 __le64 blockptr;
529 __le64 generation;
530} __attribute__ ((__packed__));
531
532struct btrfs_node {
533 struct btrfs_header header;
534 struct btrfs_key_ptr ptrs[];
535} __attribute__ ((__packed__));
536
db671160
JM
537struct btrfs_dev_item {
538 /* the internal btrfs device id */
539 __le64 devid;
540
541 /* size of the device */
542 __le64 total_bytes;
543
544 /* bytes used */
545 __le64 bytes_used;
546
547 /* optimal io alignment for this device */
548 __le32 io_align;
549
550 /* optimal io width for this device */
551 __le32 io_width;
552
553 /* minimal io size for this device */
554 __le32 sector_size;
555
556 /* type and info about this device */
557 __le64 type;
558
559 /* expected generation for this device */
560 __le64 generation;
561
562 /*
563 * starting byte of this partition on the device,
564 * to allow for stripe alignment in the future
565 */
566 __le64 start_offset;
567
568 /* grouping information for allocation decisions */
569 __le32 dev_group;
570
571 /* seek speed 0-100 where 100 is fastest */
14b05c51 572 __u8 seek_speed;
db671160
JM
573
574 /* bandwidth 0-100 where 100 is fastest */
14b05c51 575 __u8 bandwidth;
db671160
JM
576
577 /* btrfs generated uuid for this device */
14b05c51 578 __u8 uuid[BTRFS_UUID_SIZE];
db671160
JM
579
580 /* uuid of FS who owns this device */
14b05c51 581 __u8 fsid[BTRFS_UUID_SIZE];
db671160
JM
582} __attribute__ ((__packed__));
583
584struct btrfs_stripe {
585 __le64 devid;
586 __le64 offset;
14b05c51 587 __u8 dev_uuid[BTRFS_UUID_SIZE];
db671160
JM
588} __attribute__ ((__packed__));
589
590struct btrfs_chunk {
591 /* size of this chunk in bytes */
592 __le64 length;
593
594 /* objectid of the root referencing this chunk */
595 __le64 owner;
596
597 __le64 stripe_len;
598 __le64 type;
599
600 /* optimal io alignment for this chunk */
601 __le32 io_align;
602
603 /* optimal io width for this chunk */
604 __le32 io_width;
605
606 /* minimal io size for this chunk */
607 __le32 sector_size;
608
609 /* 2^16 stripes is quite a lot, a second limit is the size of a single
610 * item in the btree
611 */
612 __le16 num_stripes;
613
614 /* sub stripes only matter for raid10 */
615 __le16 sub_stripes;
616 struct btrfs_stripe stripe;
617 /* additional stripes go here */
618} __attribute__ ((__packed__));
619
4300c58f
JB
620/*
621 * The super block basically lists the main trees of the FS.
622 */
623struct btrfs_super_block {
624 /* The first 4 fields must match struct btrfs_header */
625 __u8 csum[BTRFS_CSUM_SIZE];
626 /* FS specific UUID, visible to user */
627 __u8 fsid[BTRFS_FSID_SIZE];
628 /* This block number */
629 __le64 bytenr;
630 __le64 flags;
631
632 /* Allowed to be different from the btrfs_header from here own down */
633 __le64 magic;
634 __le64 generation;
635 __le64 root;
636 __le64 chunk_root;
637 __le64 log_root;
638
639 /*
640 * This member has never been utilized since the very beginning, thus
641 * it's always 0 regardless of kernel version. We always use
642 * generation + 1 to read log tree root. So here we mark it deprecated.
643 */
644 __le64 __unused_log_root_transid;
645 __le64 total_bytes;
646 __le64 bytes_used;
647 __le64 root_dir_objectid;
648 __le64 num_devices;
649 __le32 sectorsize;
650 __le32 nodesize;
651 __le32 __unused_leafsize;
652 __le32 stripesize;
653 __le32 sys_chunk_array_size;
654 __le64 chunk_root_generation;
655 __le64 compat_flags;
656 __le64 compat_ro_flags;
657 __le64 incompat_flags;
658 __le16 csum_type;
659 __u8 root_level;
660 __u8 chunk_root_level;
661 __u8 log_root_level;
662 struct btrfs_dev_item dev_item;
663
664 char label[BTRFS_LABEL_SIZE];
665
666 __le64 cache_generation;
667 __le64 uuid_tree_generation;
668
669 /* The UUID written into btree blocks */
670 __u8 metadata_uuid[BTRFS_FSID_SIZE];
671
672 /* Future expansion */
673 __u8 reserved8[8];
674 __le64 reserved[27];
675 __u8 sys_chunk_array[BTRFS_SYSTEM_CHUNK_ARRAY_SIZE];
676 struct btrfs_root_backup super_roots[BTRFS_NUM_BACKUP_ROOTS];
677
678 /* Padded to 4096 bytes */
679 __u8 padding[565];
680} __attribute__ ((__packed__));
681
db671160
JM
682#define BTRFS_FREE_SPACE_EXTENT 1
683#define BTRFS_FREE_SPACE_BITMAP 2
684
685struct btrfs_free_space_entry {
686 __le64 offset;
687 __le64 bytes;
14b05c51 688 __u8 type;
db671160
JM
689} __attribute__ ((__packed__));
690
691struct btrfs_free_space_header {
692 struct btrfs_disk_key location;
693 __le64 generation;
694 __le64 num_entries;
695 __le64 num_bitmaps;
696} __attribute__ ((__packed__));
697
698#define BTRFS_HEADER_FLAG_WRITTEN (1ULL << 0)
699#define BTRFS_HEADER_FLAG_RELOC (1ULL << 1)
700
701/* Super block flags */
702/* Errors detected */
703#define BTRFS_SUPER_FLAG_ERROR (1ULL << 2)
704
705#define BTRFS_SUPER_FLAG_SEEDING (1ULL << 32)
706#define BTRFS_SUPER_FLAG_METADUMP (1ULL << 33)
e2731e55 707#define BTRFS_SUPER_FLAG_METADUMP_V2 (1ULL << 34)
98820a7e 708#define BTRFS_SUPER_FLAG_CHANGING_FSID (1ULL << 35)
7239ff4b 709#define BTRFS_SUPER_FLAG_CHANGING_FSID_V2 (1ULL << 36)
db671160
JM
710
711
712/*
713 * items in the extent btree are used to record the objectid of the
714 * owner of the block and the number of references
715 */
716
717struct btrfs_extent_item {
718 __le64 refs;
719 __le64 generation;
720 __le64 flags;
721} __attribute__ ((__packed__));
722
723struct btrfs_extent_item_v0 {
724 __le32 refs;
725} __attribute__ ((__packed__));
726
727
728#define BTRFS_EXTENT_FLAG_DATA (1ULL << 0)
729#define BTRFS_EXTENT_FLAG_TREE_BLOCK (1ULL << 1)
730
731/* following flags only apply to tree blocks */
732
733/* use full backrefs for extent pointers in the block */
734#define BTRFS_BLOCK_FLAG_FULL_BACKREF (1ULL << 8)
735
4300c58f
JB
736#define BTRFS_BACKREF_REV_MAX 256
737#define BTRFS_BACKREF_REV_SHIFT 56
738#define BTRFS_BACKREF_REV_MASK (((u64)BTRFS_BACKREF_REV_MAX - 1) << \
739 BTRFS_BACKREF_REV_SHIFT)
740
741#define BTRFS_OLD_BACKREF_REV 0
742#define BTRFS_MIXED_BACKREF_REV 1
743
db671160
JM
744/*
745 * this flag is only used internally by scrub and may be changed at any time
746 * it is only declared here to avoid collisions
747 */
748#define BTRFS_EXTENT_FLAG_SUPER (1ULL << 48)
749
750struct btrfs_tree_block_info {
751 struct btrfs_disk_key key;
14b05c51 752 __u8 level;
db671160
JM
753} __attribute__ ((__packed__));
754
755struct btrfs_extent_data_ref {
756 __le64 root;
757 __le64 objectid;
758 __le64 offset;
759 __le32 count;
760} __attribute__ ((__packed__));
761
762struct btrfs_shared_data_ref {
763 __le32 count;
764} __attribute__ ((__packed__));
765
766struct btrfs_extent_inline_ref {
14b05c51 767 __u8 type;
db671160
JM
768 __le64 offset;
769} __attribute__ ((__packed__));
770
db671160
JM
771/* dev extents record free space on individual devices. The owner
772 * field points back to the chunk allocation mapping tree that allocated
773 * the extent. The chunk tree uuid field is a way to double check the owner
774 */
775struct btrfs_dev_extent {
776 __le64 chunk_tree;
777 __le64 chunk_objectid;
778 __le64 chunk_offset;
779 __le64 length;
14b05c51 780 __u8 chunk_tree_uuid[BTRFS_UUID_SIZE];
db671160
JM
781} __attribute__ ((__packed__));
782
783struct btrfs_inode_ref {
784 __le64 index;
785 __le16 name_len;
786 /* name goes here */
787} __attribute__ ((__packed__));
788
789struct btrfs_inode_extref {
790 __le64 parent_objectid;
791 __le64 index;
792 __le16 name_len;
94dfc73e 793 __u8 name[];
db671160
JM
794 /* name goes here */
795} __attribute__ ((__packed__));
796
797struct btrfs_timespec {
798 __le64 sec;
799 __le32 nsec;
800} __attribute__ ((__packed__));
801
802struct btrfs_inode_item {
803 /* nfs style generation number */
804 __le64 generation;
805 /* transid that last touched this inode */
806 __le64 transid;
807 __le64 size;
808 __le64 nbytes;
809 __le64 block_group;
810 __le32 nlink;
811 __le32 uid;
812 __le32 gid;
813 __le32 mode;
814 __le64 rdev;
815 __le64 flags;
816
817 /* modification sequence number for NFS */
818 __le64 sequence;
819
820 /*
821 * a little future expansion, for more than this we can
822 * just grow the inode item and version it
823 */
824 __le64 reserved[4];
825 struct btrfs_timespec atime;
826 struct btrfs_timespec ctime;
827 struct btrfs_timespec mtime;
828 struct btrfs_timespec otime;
829} __attribute__ ((__packed__));
830
831struct btrfs_dir_log_item {
832 __le64 end;
833} __attribute__ ((__packed__));
834
835struct btrfs_dir_item {
836 struct btrfs_disk_key location;
837 __le64 transid;
838 __le16 data_len;
839 __le16 name_len;
14b05c51 840 __u8 type;
db671160
JM
841} __attribute__ ((__packed__));
842
843#define BTRFS_ROOT_SUBVOL_RDONLY (1ULL << 0)
844
845/*
846 * Internal in-memory flag that a subvolume has been marked for deletion but
847 * still visible as a directory
848 */
849#define BTRFS_ROOT_SUBVOL_DEAD (1ULL << 48)
850
851struct btrfs_root_item {
852 struct btrfs_inode_item inode;
853 __le64 generation;
854 __le64 root_dirid;
855 __le64 bytenr;
856 __le64 byte_limit;
857 __le64 bytes_used;
858 __le64 last_snapshot;
859 __le64 flags;
860 __le32 refs;
861 struct btrfs_disk_key drop_progress;
14b05c51
JM
862 __u8 drop_level;
863 __u8 level;
db671160
JM
864
865 /*
866 * The following fields appear after subvol_uuids+subvol_times
867 * were introduced.
868 */
869
870 /*
871 * This generation number is used to test if the new fields are valid
872 * and up to date while reading the root item. Every time the root item
873 * is written out, the "generation" field is copied into this field. If
874 * anyone ever mounted the fs with an older kernel, we will have
875 * mismatching generation values here and thus must invalidate the
876 * new fields. See btrfs_update_root and btrfs_find_last_root for
877 * details.
878 * the offset of generation_v2 is also used as the start for the memset
879 * when invalidating the fields.
880 */
881 __le64 generation_v2;
14b05c51
JM
882 __u8 uuid[BTRFS_UUID_SIZE];
883 __u8 parent_uuid[BTRFS_UUID_SIZE];
884 __u8 received_uuid[BTRFS_UUID_SIZE];
db671160
JM
885 __le64 ctransid; /* updated when an inode changes */
886 __le64 otransid; /* trans when created */
887 __le64 stransid; /* trans when sent. non-zero for received subvol */
888 __le64 rtransid; /* trans when received. non-zero for received subvol */
889 struct btrfs_timespec ctime;
890 struct btrfs_timespec otime;
891 struct btrfs_timespec stime;
892 struct btrfs_timespec rtime;
893 __le64 reserved[8]; /* for future */
894} __attribute__ ((__packed__));
895
1465af12
QW
896/*
897 * Btrfs root item used to be smaller than current size. The old format ends
898 * at where member generation_v2 is.
899 */
900static inline __u32 btrfs_legacy_root_item_size(void)
901{
902 return offsetof(struct btrfs_root_item, generation_v2);
903}
904
db671160
JM
905/*
906 * this is used for both forward and backward root refs
907 */
908struct btrfs_root_ref {
909 __le64 dirid;
910 __le64 sequence;
911 __le16 name_len;
912} __attribute__ ((__packed__));
913
914struct btrfs_disk_balance_args {
915 /*
916 * profiles to operate on, single is denoted by
917 * BTRFS_AVAIL_ALLOC_BIT_SINGLE
918 */
919 __le64 profiles;
920
921 /*
922 * usage filter
923 * BTRFS_BALANCE_ARGS_USAGE with a single value means '0..N'
924 * BTRFS_BALANCE_ARGS_USAGE_RANGE - range syntax, min..max
925 */
926 union {
927 __le64 usage;
928 struct {
929 __le32 usage_min;
930 __le32 usage_max;
931 };
932 };
933
934 /* devid filter */
935 __le64 devid;
936
937 /* devid subset filter [pstart..pend) */
938 __le64 pstart;
939 __le64 pend;
940
941 /* btrfs virtual address space subset filter [vstart..vend) */
942 __le64 vstart;
943 __le64 vend;
944
945 /*
946 * profile to convert to, single is denoted by
947 * BTRFS_AVAIL_ALLOC_BIT_SINGLE
948 */
949 __le64 target;
950
951 /* BTRFS_BALANCE_ARGS_* */
952 __le64 flags;
953
954 /*
955 * BTRFS_BALANCE_ARGS_LIMIT with value 'limit'
956 * BTRFS_BALANCE_ARGS_LIMIT_RANGE - the extend version can use minimum
957 * and maximum
958 */
959 union {
960 __le64 limit;
961 struct {
962 __le32 limit_min;
963 __le32 limit_max;
964 };
965 };
966
967 /*
968 * Process chunks that cross stripes_min..stripes_max devices,
969 * BTRFS_BALANCE_ARGS_STRIPES_RANGE
970 */
971 __le32 stripes_min;
972 __le32 stripes_max;
973
974 __le64 unused[6];
975} __attribute__ ((__packed__));
976
977/*
978 * store balance parameters to disk so that balance can be properly
979 * resumed after crash or unmount
980 */
981struct btrfs_balance_item {
982 /* BTRFS_BALANCE_* */
983 __le64 flags;
984
985 struct btrfs_disk_balance_args data;
986 struct btrfs_disk_balance_args meta;
987 struct btrfs_disk_balance_args sys;
988
989 __le64 unused[4];
990} __attribute__ ((__packed__));
991
b9b1a53e
CX
992enum {
993 BTRFS_FILE_EXTENT_INLINE = 0,
994 BTRFS_FILE_EXTENT_REG = 1,
995 BTRFS_FILE_EXTENT_PREALLOC = 2,
996 BTRFS_NR_FILE_EXTENT_TYPES = 3,
997};
db671160
JM
998
999struct btrfs_file_extent_item {
1000 /*
1001 * transaction id that created this extent
1002 */
1003 __le64 generation;
1004 /*
1005 * max number of bytes to hold this extent in ram
1006 * when we split a compressed extent we can't know how big
1007 * each of the resulting pieces will be. So, this is
1008 * an upper limit on the size of the extent in ram instead of
1009 * an exact limit.
1010 */
1011 __le64 ram_bytes;
1012
1013 /*
1014 * 32 bits for the various ways we might encode the data,
1015 * including compression and encryption. If any of these
1016 * are set to something a given disk format doesn't understand
1017 * it is treated like an incompat flag for reading and writing,
1018 * but not for stat.
1019 */
14b05c51
JM
1020 __u8 compression;
1021 __u8 encryption;
db671160
JM
1022 __le16 other_encoding; /* spare for later use */
1023
1024 /* are we inline data or a real extent? */
14b05c51 1025 __u8 type;
db671160
JM
1026
1027 /*
1028 * disk space consumed by the extent, checksum blocks are included
1029 * in these numbers
1030 *
1031 * At this offset in the structure, the inline extent data start.
1032 */
1033 __le64 disk_bytenr;
1034 __le64 disk_num_bytes;
1035 /*
1036 * the logical offset in file blocks (no csums)
1037 * this extent record is for. This allows a file extent to point
1038 * into the middle of an existing extent on disk, sharing it
1039 * between two snapshots (useful if some bytes in the middle of the
1040 * extent have changed
1041 */
1042 __le64 offset;
1043 /*
1044 * the logical number of file blocks (no csums included). This
1045 * always reflects the size uncompressed and without encoding.
1046 */
1047 __le64 num_bytes;
1048
1049} __attribute__ ((__packed__));
1050
1051struct btrfs_csum_item {
14b05c51 1052 __u8 csum;
db671160
JM
1053} __attribute__ ((__packed__));
1054
1055struct btrfs_dev_stats_item {
1056 /*
1057 * grow this item struct at the end for future enhancements and keep
1058 * the existing values unchanged
1059 */
1060 __le64 values[BTRFS_DEV_STAT_VALUES_MAX];
1061} __attribute__ ((__packed__));
1062
1063#define BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_ALWAYS 0
1064#define BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID 1
db671160
JM
1065
1066struct btrfs_dev_replace_item {
1067 /*
1068 * grow this item struct at the end for future enhancements and keep
1069 * the existing values unchanged
1070 */
1071 __le64 src_devid;
1072 __le64 cursor_left;
1073 __le64 cursor_right;
1074 __le64 cont_reading_from_srcdev_mode;
1075
1076 __le64 replace_state;
1077 __le64 time_started;
1078 __le64 time_stopped;
1079 __le64 num_write_errors;
1080 __le64 num_uncorrectable_read_errors;
1081} __attribute__ ((__packed__));
1082
1083/* different types of block groups (and chunks) */
1084#define BTRFS_BLOCK_GROUP_DATA (1ULL << 0)
1085#define BTRFS_BLOCK_GROUP_SYSTEM (1ULL << 1)
1086#define BTRFS_BLOCK_GROUP_METADATA (1ULL << 2)
1087#define BTRFS_BLOCK_GROUP_RAID0 (1ULL << 3)
1088#define BTRFS_BLOCK_GROUP_RAID1 (1ULL << 4)
1089#define BTRFS_BLOCK_GROUP_DUP (1ULL << 5)
1090#define BTRFS_BLOCK_GROUP_RAID10 (1ULL << 6)
1091#define BTRFS_BLOCK_GROUP_RAID5 (1ULL << 7)
1092#define BTRFS_BLOCK_GROUP_RAID6 (1ULL << 8)
47e6f742 1093#define BTRFS_BLOCK_GROUP_RAID1C3 (1ULL << 9)
8d6fac00 1094#define BTRFS_BLOCK_GROUP_RAID1C4 (1ULL << 10)
db671160
JM
1095#define BTRFS_BLOCK_GROUP_RESERVED (BTRFS_AVAIL_ALLOC_BIT_SINGLE | \
1096 BTRFS_SPACE_INFO_GLOBAL_RSV)
1097
db671160
JM
1098#define BTRFS_BLOCK_GROUP_TYPE_MASK (BTRFS_BLOCK_GROUP_DATA | \
1099 BTRFS_BLOCK_GROUP_SYSTEM | \
1100 BTRFS_BLOCK_GROUP_METADATA)
1101
1102#define BTRFS_BLOCK_GROUP_PROFILE_MASK (BTRFS_BLOCK_GROUP_RAID0 | \
1103 BTRFS_BLOCK_GROUP_RAID1 | \
47e6f742 1104 BTRFS_BLOCK_GROUP_RAID1C3 | \
8d6fac00 1105 BTRFS_BLOCK_GROUP_RAID1C4 | \
db671160
JM
1106 BTRFS_BLOCK_GROUP_RAID5 | \
1107 BTRFS_BLOCK_GROUP_RAID6 | \
1108 BTRFS_BLOCK_GROUP_DUP | \
1109 BTRFS_BLOCK_GROUP_RAID10)
1110#define BTRFS_BLOCK_GROUP_RAID56_MASK (BTRFS_BLOCK_GROUP_RAID5 | \
1111 BTRFS_BLOCK_GROUP_RAID6)
1112
47e6f742 1113#define BTRFS_BLOCK_GROUP_RAID1_MASK (BTRFS_BLOCK_GROUP_RAID1 | \
8d6fac00
DS
1114 BTRFS_BLOCK_GROUP_RAID1C3 | \
1115 BTRFS_BLOCK_GROUP_RAID1C4)
c7369b3f 1116
db671160
JM
1117/*
1118 * We need a bit for restriper to be able to tell when chunks of type
1119 * SINGLE are available. This "extended" profile format is used in
1120 * fs_info->avail_*_alloc_bits (in-memory) and balance item fields
1121 * (on-disk). The corresponding on-disk bit in chunk.type is reserved
1122 * to avoid remappings between two formats in future.
1123 */
1124#define BTRFS_AVAIL_ALLOC_BIT_SINGLE (1ULL << 48)
1125
1126/*
1127 * A fake block group type that is used to communicate global block reserve
1128 * size to userspace via the SPACE_INFO ioctl.
1129 */
1130#define BTRFS_SPACE_INFO_GLOBAL_RSV (1ULL << 49)
1131
1132#define BTRFS_EXTENDED_PROFILE_MASK (BTRFS_BLOCK_GROUP_PROFILE_MASK | \
1133 BTRFS_AVAIL_ALLOC_BIT_SINGLE)
1134
14b05c51 1135static inline __u64 chunk_to_extended(__u64 flags)
db671160
JM
1136{
1137 if ((flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0)
1138 flags |= BTRFS_AVAIL_ALLOC_BIT_SINGLE;
1139
1140 return flags;
1141}
14b05c51 1142static inline __u64 extended_to_chunk(__u64 flags)
db671160
JM
1143{
1144 return flags & ~BTRFS_AVAIL_ALLOC_BIT_SINGLE;
1145}
1146
1147struct btrfs_block_group_item {
1148 __le64 used;
1149 __le64 chunk_objectid;
1150 __le64 flags;
1151} __attribute__ ((__packed__));
1152
1153struct btrfs_free_space_info {
1154 __le32 extent_count;
1155 __le32 flags;
1156} __attribute__ ((__packed__));
1157
1158#define BTRFS_FREE_SPACE_USING_BITMAPS (1ULL << 0)
1159
1160#define BTRFS_QGROUP_LEVEL_SHIFT 48
06f67c47 1161static inline __u16 btrfs_qgroup_level(__u64 qgroupid)
db671160 1162{
06f67c47 1163 return (__u16)(qgroupid >> BTRFS_QGROUP_LEVEL_SHIFT);
db671160
JM
1164}
1165
1166/*
1167 * is subvolume quota turned on?
1168 */
1169#define BTRFS_QGROUP_STATUS_FLAG_ON (1ULL << 0)
1170/*
1171 * RESCAN is set during the initialization phase
1172 */
1173#define BTRFS_QGROUP_STATUS_FLAG_RESCAN (1ULL << 1)
1174/*
1175 * Some qgroup entries are known to be out of date,
1176 * either because the configuration has changed in a way that
1177 * makes a rescan necessary, or because the fs has been mounted
1178 * with a non-qgroup-aware version.
1179 * Turning qouta off and on again makes it inconsistent, too.
1180 */
1181#define BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT (1ULL << 2)
1182
e71564c0
QW
1183#define BTRFS_QGROUP_STATUS_FLAGS_MASK (BTRFS_QGROUP_STATUS_FLAG_ON | \
1184 BTRFS_QGROUP_STATUS_FLAG_RESCAN | \
1185 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT)
1186
db671160
JM
1187#define BTRFS_QGROUP_STATUS_VERSION 1
1188
1189struct btrfs_qgroup_status_item {
1190 __le64 version;
1191 /*
1192 * the generation is updated during every commit. As older
1193 * versions of btrfs are not aware of qgroups, it will be
1194 * possible to detect inconsistencies by checking the
1195 * generation on mount time
1196 */
1197 __le64 generation;
1198
1199 /* flag definitions see above */
1200 __le64 flags;
1201
1202 /*
1203 * only used during scanning to record the progress
1204 * of the scan. It contains a logical address
1205 */
1206 __le64 rescan;
1207} __attribute__ ((__packed__));
1208
1209struct btrfs_qgroup_info_item {
1210 __le64 generation;
1211 __le64 rfer;
1212 __le64 rfer_cmpr;
1213 __le64 excl;
1214 __le64 excl_cmpr;
1215} __attribute__ ((__packed__));
1216
1217struct btrfs_qgroup_limit_item {
1218 /*
1219 * only updated when any of the other values change
1220 */
1221 __le64 flags;
1222 __le64 max_rfer;
1223 __le64 max_excl;
1224 __le64 rsv_rfer;
1225 __le64 rsv_excl;
1226} __attribute__ ((__packed__));
1227
14605409
BB
1228struct btrfs_verity_descriptor_item {
1229 /* Size of the verity descriptor in bytes */
1230 __le64 size;
1231 /*
1232 * When we implement support for fscrypt, we will need to encrypt the
1233 * Merkle tree for encrypted verity files. These 128 bits are for the
1234 * eventual storage of an fscrypt initialization vector.
1235 */
1236 __le64 reserved[2];
1237 __u8 encryption;
1238} __attribute__ ((__packed__));
1239
db671160 1240#endif /* _BTRFS_CTREE_H_ */