1 // SPDX-License-Identifier: GPL-2.0+
3 #ifndef __BTRFS_VOLUMES_H__
4 #define __BTRFS_VOLUMES_H__
6 #include <fs_internal.h>
9 #define BTRFS_STRIPE_LEN SZ_64K
12 struct list_head dev_list
;
13 struct btrfs_root
*dev_root
;
14 struct btrfs_fs_devices
*fs_devices
;
16 struct blk_desc
*desc
;
17 struct disk_partition
*part
;
24 /* the internal btrfs device id */
27 /* size of the device */
33 /* optimal io alignment for this device */
36 /* optimal io width for this device */
39 /* minimal io size for this device */
42 /* type and info about this device */
45 /* physical drive uuid (or lvm uuid) */
46 u8 uuid
[BTRFS_UUID_SIZE
];
49 struct btrfs_fs_devices
{
50 u8 fsid
[BTRFS_FSID_SIZE
]; /* FS specific uuid */
51 u8 metadata_uuid
[BTRFS_FSID_SIZE
]; /* FS specific uuid */
59 struct list_head devices
;
60 struct list_head list
;
63 struct btrfs_fs_devices
*seed
;
66 struct btrfs_bio_stripe
{
67 struct btrfs_device
*dev
;
71 struct btrfs_multi_bio
{
74 struct btrfs_bio_stripe stripes
[];
78 struct cache_extent ce
;
86 struct btrfs_bio_stripe stripes
[];
89 struct btrfs_raid_attr
{
90 int sub_stripes
; /* sub_stripes info for map */
91 int dev_stripes
; /* stripes per dev */
92 int devs_max
; /* max devs to use */
93 int devs_min
; /* min devs needed */
94 int tolerated_failures
; /* max tolerated fail devs */
95 int devs_increment
; /* ndevs has to be a multiple of this */
96 int ncopies
; /* how many copies to data has */
97 int nparity
; /* number of stripes worth of bytes to store
98 * parity information */
99 const char raid_name
[8]; /* name of the raid */
100 u64 bg_flag
; /* block group flag of the raid */
103 extern const struct btrfs_raid_attr btrfs_raid_array
[BTRFS_NR_RAID_TYPES
];
105 static inline enum btrfs_raid_types
btrfs_bg_flags_to_raid_index(u64 flags
)
107 if (flags
& BTRFS_BLOCK_GROUP_RAID10
)
108 return BTRFS_RAID_RAID10
;
109 else if (flags
& BTRFS_BLOCK_GROUP_RAID1
)
110 return BTRFS_RAID_RAID1
;
111 else if (flags
& BTRFS_BLOCK_GROUP_RAID1C3
)
112 return BTRFS_RAID_RAID1C3
;
113 else if (flags
& BTRFS_BLOCK_GROUP_RAID1C4
)
114 return BTRFS_RAID_RAID1C4
;
115 else if (flags
& BTRFS_BLOCK_GROUP_DUP
)
116 return BTRFS_RAID_DUP
;
117 else if (flags
& BTRFS_BLOCK_GROUP_RAID0
)
118 return BTRFS_RAID_RAID0
;
119 else if (flags
& BTRFS_BLOCK_GROUP_RAID5
)
120 return BTRFS_RAID_RAID5
;
121 else if (flags
& BTRFS_BLOCK_GROUP_RAID6
)
122 return BTRFS_RAID_RAID6
;
124 return BTRFS_RAID_SINGLE
; /* BTRFS_BLOCK_GROUP_SINGLE */
127 #define btrfs_multi_bio_size(n) (sizeof(struct btrfs_multi_bio) + \
128 (sizeof(struct btrfs_bio_stripe) * (n)))
129 #define btrfs_map_lookup_size(n) (sizeof(struct map_lookup) + \
130 (sizeof(struct btrfs_bio_stripe) * (n)))
132 #define BTRFS_RAID5_P_STRIPE ((u64)-2)
133 #define BTRFS_RAID6_Q_STRIPE ((u64)-1)
135 static inline u64
calc_stripe_length(u64 type
, u64 length
, int num_stripes
)
139 if (type
& BTRFS_BLOCK_GROUP_RAID0
) {
140 stripe_size
= length
;
141 stripe_size
/= num_stripes
;
142 } else if (type
& BTRFS_BLOCK_GROUP_RAID10
) {
143 stripe_size
= length
* 2;
144 stripe_size
/= num_stripes
;
145 } else if (type
& BTRFS_BLOCK_GROUP_RAID5
) {
146 stripe_size
= length
;
147 stripe_size
/= (num_stripes
- 1);
148 } else if (type
& BTRFS_BLOCK_GROUP_RAID6
) {
149 stripe_size
= length
;
150 stripe_size
/= (num_stripes
- 2);
152 stripe_size
= length
;
163 int __btrfs_map_block(struct btrfs_fs_info
*fs_info
, int rw
,
164 u64 logical
, u64
*length
, u64
*type
,
165 struct btrfs_multi_bio
**multi_ret
, int mirror_num
,
167 int btrfs_map_block(struct btrfs_fs_info
*fs_info
, int rw
,
168 u64 logical
, u64
*length
,
169 struct btrfs_multi_bio
**multi_ret
, int mirror_num
,
171 int btrfs_next_bg(struct btrfs_fs_info
*map_tree
, u64
*logical
,
172 u64
*size
, u64 type
);
173 static inline int btrfs_next_bg_metadata(struct btrfs_fs_info
*fs_info
,
174 u64
*logical
, u64
*size
)
176 return btrfs_next_bg(fs_info
, logical
, size
,
177 BTRFS_BLOCK_GROUP_METADATA
);
179 static inline int btrfs_next_bg_system(struct btrfs_fs_info
*fs_info
,
180 u64
*logical
, u64
*size
)
182 return btrfs_next_bg(fs_info
, logical
, size
,
183 BTRFS_BLOCK_GROUP_SYSTEM
);
185 int btrfs_read_sys_array(struct btrfs_fs_info
*fs_info
);
186 int btrfs_read_chunk_tree(struct btrfs_fs_info
*fs_info
);
187 int btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
);
188 int btrfs_close_devices(struct btrfs_fs_devices
*fs_devices
);
189 void btrfs_close_all_devices(void);
190 int btrfs_num_copies(struct btrfs_fs_info
*fs_info
, u64 logical
, u64 len
);
191 int btrfs_scan_one_device(struct blk_desc
*desc
, struct disk_partition
*part
,
192 struct btrfs_fs_devices
**fs_devices_ret
,
194 struct list_head
*btrfs_scanned_uuids(void);
195 struct btrfs_device
*btrfs_find_device(struct btrfs_fs_info
*fs_info
, u64 devid
,
197 int btrfs_check_chunk_valid(struct btrfs_fs_info
*fs_info
,
198 struct extent_buffer
*leaf
,
199 struct btrfs_chunk
*chunk
,
200 int slot
, u64 logical
);
201 u64
btrfs_stripe_length(struct btrfs_fs_info
*fs_info
,
202 struct extent_buffer
*leaf
,
203 struct btrfs_chunk
*chunk
);