volumes.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // SPDX-License-Identifier: GPL-2.0+
  2. #ifndef __BTRFS_VOLUMES_H__
  3. #define __BTRFS_VOLUMES_H__
  4. #include <fs_internal.h>
  5. #include "ctree.h"
  6. #define BTRFS_STRIPE_LEN SZ_64K
  7. struct btrfs_device {
  8. struct list_head dev_list;
  9. struct btrfs_root *dev_root;
  10. struct btrfs_fs_devices *fs_devices;
  11. struct blk_desc *desc;
  12. struct disk_partition *part;
  13. u64 total_devs;
  14. u64 super_bytes_used;
  15. u64 generation;
  16. /* the internal btrfs device id */
  17. u64 devid;
  18. /* size of the device */
  19. u64 total_bytes;
  20. /* bytes used */
  21. u64 bytes_used;
  22. /* optimal io alignment for this device */
  23. u32 io_align;
  24. /* optimal io width for this device */
  25. u32 io_width;
  26. /* minimal io size for this device */
  27. u32 sector_size;
  28. /* type and info about this device */
  29. u64 type;
  30. /* physical drive uuid (or lvm uuid) */
  31. u8 uuid[BTRFS_UUID_SIZE];
  32. };
  33. struct btrfs_fs_devices {
  34. u8 fsid[BTRFS_FSID_SIZE]; /* FS specific uuid */
  35. u8 metadata_uuid[BTRFS_FSID_SIZE]; /* FS specific uuid */
  36. u64 latest_devid;
  37. u64 lowest_devid;
  38. u64 latest_trans;
  39. u64 total_rw_bytes;
  40. struct list_head devices;
  41. struct list_head list;
  42. int seeding;
  43. struct btrfs_fs_devices *seed;
  44. };
  45. struct btrfs_bio_stripe {
  46. struct btrfs_device *dev;
  47. u64 physical;
  48. };
  49. struct btrfs_multi_bio {
  50. int error;
  51. int num_stripes;
  52. struct btrfs_bio_stripe stripes[];
  53. };
  54. struct map_lookup {
  55. struct cache_extent ce;
  56. u64 type;
  57. int io_align;
  58. int io_width;
  59. int stripe_len;
  60. int sector_size;
  61. int num_stripes;
  62. int sub_stripes;
  63. struct btrfs_bio_stripe stripes[];
  64. };
  65. struct btrfs_raid_attr {
  66. int sub_stripes; /* sub_stripes info for map */
  67. int dev_stripes; /* stripes per dev */
  68. int devs_max; /* max devs to use */
  69. int devs_min; /* min devs needed */
  70. int tolerated_failures; /* max tolerated fail devs */
  71. int devs_increment; /* ndevs has to be a multiple of this */
  72. int ncopies; /* how many copies to data has */
  73. int nparity; /* number of stripes worth of bytes to store
  74. * parity information */
  75. const char raid_name[8]; /* name of the raid */
  76. u64 bg_flag; /* block group flag of the raid */
  77. };
  78. extern const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES];
  79. static inline enum btrfs_raid_types btrfs_bg_flags_to_raid_index(u64 flags)
  80. {
  81. if (flags & BTRFS_BLOCK_GROUP_RAID10)
  82. return BTRFS_RAID_RAID10;
  83. else if (flags & BTRFS_BLOCK_GROUP_RAID1)
  84. return BTRFS_RAID_RAID1;
  85. else if (flags & BTRFS_BLOCK_GROUP_RAID1C3)
  86. return BTRFS_RAID_RAID1C3;
  87. else if (flags & BTRFS_BLOCK_GROUP_RAID1C4)
  88. return BTRFS_RAID_RAID1C4;
  89. else if (flags & BTRFS_BLOCK_GROUP_DUP)
  90. return BTRFS_RAID_DUP;
  91. else if (flags & BTRFS_BLOCK_GROUP_RAID0)
  92. return BTRFS_RAID_RAID0;
  93. else if (flags & BTRFS_BLOCK_GROUP_RAID5)
  94. return BTRFS_RAID_RAID5;
  95. else if (flags & BTRFS_BLOCK_GROUP_RAID6)
  96. return BTRFS_RAID_RAID6;
  97. return BTRFS_RAID_SINGLE; /* BTRFS_BLOCK_GROUP_SINGLE */
  98. }
  99. #define btrfs_multi_bio_size(n) (sizeof(struct btrfs_multi_bio) + \
  100. (sizeof(struct btrfs_bio_stripe) * (n)))
  101. #define btrfs_map_lookup_size(n) (sizeof(struct map_lookup) + \
  102. (sizeof(struct btrfs_bio_stripe) * (n)))
  103. #define BTRFS_RAID5_P_STRIPE ((u64)-2)
  104. #define BTRFS_RAID6_Q_STRIPE ((u64)-1)
  105. static inline u64 calc_stripe_length(u64 type, u64 length, int num_stripes)
  106. {
  107. u64 stripe_size;
  108. if (type & BTRFS_BLOCK_GROUP_RAID0) {
  109. stripe_size = length;
  110. stripe_size /= num_stripes;
  111. } else if (type & BTRFS_BLOCK_GROUP_RAID10) {
  112. stripe_size = length * 2;
  113. stripe_size /= num_stripes;
  114. } else if (type & BTRFS_BLOCK_GROUP_RAID5) {
  115. stripe_size = length;
  116. stripe_size /= (num_stripes - 1);
  117. } else if (type & BTRFS_BLOCK_GROUP_RAID6) {
  118. stripe_size = length;
  119. stripe_size /= (num_stripes - 2);
  120. } else {
  121. stripe_size = length;
  122. }
  123. return stripe_size;
  124. }
  125. #ifndef READ
  126. #define READ 0
  127. #define WRITE 1
  128. #define READA 2
  129. #endif
  130. int __btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
  131. u64 logical, u64 *length, u64 *type,
  132. struct btrfs_multi_bio **multi_ret, int mirror_num,
  133. u64 **raid_map);
  134. int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
  135. u64 logical, u64 *length,
  136. struct btrfs_multi_bio **multi_ret, int mirror_num,
  137. u64 **raid_map_ret);
  138. int btrfs_next_bg(struct btrfs_fs_info *map_tree, u64 *logical,
  139. u64 *size, u64 type);
  140. static inline int btrfs_next_bg_metadata(struct btrfs_fs_info *fs_info,
  141. u64 *logical, u64 *size)
  142. {
  143. return btrfs_next_bg(fs_info, logical, size,
  144. BTRFS_BLOCK_GROUP_METADATA);
  145. }
  146. static inline int btrfs_next_bg_system(struct btrfs_fs_info *fs_info,
  147. u64 *logical, u64 *size)
  148. {
  149. return btrfs_next_bg(fs_info, logical, size,
  150. BTRFS_BLOCK_GROUP_SYSTEM);
  151. }
  152. int btrfs_read_sys_array(struct btrfs_fs_info *fs_info);
  153. int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info);
  154. int btrfs_open_devices(struct btrfs_fs_devices *fs_devices);
  155. int btrfs_close_devices(struct btrfs_fs_devices *fs_devices);
  156. void btrfs_close_all_devices(void);
  157. int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len);
  158. int btrfs_scan_one_device(struct blk_desc *desc, struct disk_partition *part,
  159. struct btrfs_fs_devices **fs_devices_ret,
  160. u64 *total_devs);
  161. struct list_head *btrfs_scanned_uuids(void);
  162. struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
  163. u8 *uuid, u8 *fsid);
  164. int btrfs_check_chunk_valid(struct btrfs_fs_info *fs_info,
  165. struct extent_buffer *leaf,
  166. struct btrfs_chunk *chunk,
  167. int slot, u64 logical);
  168. u64 btrfs_stripe_length(struct btrfs_fs_info *fs_info,
  169. struct extent_buffer *leaf,
  170. struct btrfs_chunk *chunk);
  171. #endif