volumes.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (C) 2007 Oracle. All rights reserved.
  4. */
  5. #ifndef BTRFS_VOLUMES_H
  6. #define BTRFS_VOLUMES_H
  7. #include <linux/bio.h>
  8. #include <linux/sort.h>
  9. #include <linux/btrfs.h>
  10. #include "async-thread.h"
  11. #define BTRFS_MAX_DATA_CHUNK_SIZE (10ULL * SZ_1G)
  12. extern struct mutex uuid_mutex;
  13. #define BTRFS_STRIPE_LEN SZ_64K
  14. struct buffer_head;
  15. struct btrfs_pending_bios {
  16. struct bio *head;
  17. struct bio *tail;
  18. };
  19. /*
  20. * Use sequence counter to get consistent device stat data on
  21. * 32-bit processors.
  22. */
  23. #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
  24. #include <linux/seqlock.h>
  25. #define __BTRFS_NEED_DEVICE_DATA_ORDERED
  26. #define btrfs_device_data_ordered_init(device) \
  27. seqcount_init(&device->data_seqcount)
  28. #else
  29. #define btrfs_device_data_ordered_init(device) do { } while (0)
  30. #endif
  31. #define BTRFS_DEV_STATE_WRITEABLE (0)
  32. #define BTRFS_DEV_STATE_IN_FS_METADATA (1)
  33. #define BTRFS_DEV_STATE_MISSING (2)
  34. #define BTRFS_DEV_STATE_REPLACE_TGT (3)
  35. #define BTRFS_DEV_STATE_FLUSH_SENT (4)
  36. struct btrfs_device {
  37. struct list_head dev_list;
  38. struct list_head dev_alloc_list;
  39. struct btrfs_fs_devices *fs_devices;
  40. struct btrfs_fs_info *fs_info;
  41. struct rcu_string *name;
  42. u64 generation;
  43. spinlock_t io_lock ____cacheline_aligned;
  44. int running_pending;
  45. /* When true means this device has pending chunk alloc in
  46. * current transaction. Protected by chunk_mutex.
  47. */
  48. bool has_pending_chunks;
  49. /* regular prio bios */
  50. struct btrfs_pending_bios pending_bios;
  51. /* sync bios */
  52. struct btrfs_pending_bios pending_sync_bios;
  53. struct block_device *bdev;
  54. /* the mode sent to blkdev_get */
  55. fmode_t mode;
  56. unsigned long dev_state;
  57. blk_status_t last_flush_error;
  58. int flush_bio_sent;
  59. #ifdef __BTRFS_NEED_DEVICE_DATA_ORDERED
  60. seqcount_t data_seqcount;
  61. #endif
  62. /* the internal btrfs device id */
  63. u64 devid;
  64. /* size of the device in memory */
  65. u64 total_bytes;
  66. /* size of the device on disk */
  67. u64 disk_total_bytes;
  68. /* bytes used */
  69. u64 bytes_used;
  70. /* optimal io alignment for this device */
  71. u32 io_align;
  72. /* optimal io width for this device */
  73. u32 io_width;
  74. /* type and info about this device */
  75. u64 type;
  76. /* minimal io size for this device */
  77. u32 sector_size;
  78. /* physical drive uuid (or lvm uuid) */
  79. u8 uuid[BTRFS_UUID_SIZE];
  80. /*
  81. * size of the device on the current transaction
  82. *
  83. * This variant is update when committing the transaction,
  84. * and protected by device_list_mutex
  85. */
  86. u64 commit_total_bytes;
  87. /* bytes used on the current transaction */
  88. u64 commit_bytes_used;
  89. /*
  90. * used to manage the device which is resized
  91. *
  92. * It is protected by chunk_lock.
  93. */
  94. struct list_head resized_list;
  95. /* for sending down flush barriers */
  96. struct bio *flush_bio;
  97. struct completion flush_wait;
  98. /* per-device scrub information */
  99. struct scrub_ctx *scrub_ctx;
  100. struct btrfs_work work;
  101. struct rcu_head rcu;
  102. /* readahead state */
  103. atomic_t reada_in_flight;
  104. u64 reada_next;
  105. struct reada_zone *reada_curr_zone;
  106. struct radix_tree_root reada_zones;
  107. struct radix_tree_root reada_extents;
  108. /* disk I/O failure stats. For detailed description refer to
  109. * enum btrfs_dev_stat_values in ioctl.h */
  110. int dev_stats_valid;
  111. /* Counter to record the change of device stats */
  112. atomic_t dev_stats_ccnt;
  113. atomic_t dev_stat_values[BTRFS_DEV_STAT_VALUES_MAX];
  114. };
  115. /*
  116. * If we read those variants at the context of their own lock, we needn't
  117. * use the following helpers, reading them directly is safe.
  118. */
  119. #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
  120. #define BTRFS_DEVICE_GETSET_FUNCS(name) \
  121. static inline u64 \
  122. btrfs_device_get_##name(const struct btrfs_device *dev) \
  123. { \
  124. u64 size; \
  125. unsigned int seq; \
  126. \
  127. do { \
  128. seq = read_seqcount_begin(&dev->data_seqcount); \
  129. size = dev->name; \
  130. } while (read_seqcount_retry(&dev->data_seqcount, seq)); \
  131. return size; \
  132. } \
  133. \
  134. static inline void \
  135. btrfs_device_set_##name(struct btrfs_device *dev, u64 size) \
  136. { \
  137. preempt_disable(); \
  138. write_seqcount_begin(&dev->data_seqcount); \
  139. dev->name = size; \
  140. write_seqcount_end(&dev->data_seqcount); \
  141. preempt_enable(); \
  142. }
  143. #elif BITS_PER_LONG==32 && defined(CONFIG_PREEMPT)
  144. #define BTRFS_DEVICE_GETSET_FUNCS(name) \
  145. static inline u64 \
  146. btrfs_device_get_##name(const struct btrfs_device *dev) \
  147. { \
  148. u64 size; \
  149. \
  150. preempt_disable(); \
  151. size = dev->name; \
  152. preempt_enable(); \
  153. return size; \
  154. } \
  155. \
  156. static inline void \
  157. btrfs_device_set_##name(struct btrfs_device *dev, u64 size) \
  158. { \
  159. preempt_disable(); \
  160. dev->name = size; \
  161. preempt_enable(); \
  162. }
  163. #else
  164. #define BTRFS_DEVICE_GETSET_FUNCS(name) \
  165. static inline u64 \
  166. btrfs_device_get_##name(const struct btrfs_device *dev) \
  167. { \
  168. return dev->name; \
  169. } \
  170. \
  171. static inline void \
  172. btrfs_device_set_##name(struct btrfs_device *dev, u64 size) \
  173. { \
  174. dev->name = size; \
  175. }
  176. #endif
  177. BTRFS_DEVICE_GETSET_FUNCS(total_bytes);
  178. BTRFS_DEVICE_GETSET_FUNCS(disk_total_bytes);
  179. BTRFS_DEVICE_GETSET_FUNCS(bytes_used);
  180. struct btrfs_fs_devices {
  181. u8 fsid[BTRFS_FSID_SIZE]; /* FS specific uuid */
  182. struct list_head fs_list;
  183. u64 num_devices;
  184. u64 open_devices;
  185. u64 rw_devices;
  186. u64 missing_devices;
  187. u64 total_rw_bytes;
  188. u64 total_devices;
  189. struct block_device *latest_bdev;
  190. /* all of the devices in the FS, protected by a mutex
  191. * so we can safely walk it to write out the supers without
  192. * worrying about add/remove by the multi-device code.
  193. * Scrubbing super can kick off supers writing by holding
  194. * this mutex lock.
  195. */
  196. struct mutex device_list_mutex;
  197. struct list_head devices;
  198. struct list_head resized_devices;
  199. /* devices not currently being allocated */
  200. struct list_head alloc_list;
  201. struct btrfs_fs_devices *seed;
  202. int seeding;
  203. int opened;
  204. /* set when we find or add a device that doesn't have the
  205. * nonrot flag set
  206. */
  207. int rotating;
  208. struct btrfs_fs_info *fs_info;
  209. /* sysfs kobjects */
  210. struct kobject fsid_kobj;
  211. struct kobject *device_dir_kobj;
  212. struct completion kobj_unregister;
  213. };
  214. #define BTRFS_BIO_INLINE_CSUM_SIZE 64
  215. #define BTRFS_MAX_DEVS(info) ((BTRFS_MAX_ITEM_SIZE(info) \
  216. - sizeof(struct btrfs_chunk)) \
  217. / sizeof(struct btrfs_stripe) + 1)
  218. #define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \
  219. - 2 * sizeof(struct btrfs_disk_key) \
  220. - 2 * sizeof(struct btrfs_chunk)) \
  221. / sizeof(struct btrfs_stripe) + 1)
  222. /*
  223. * we need the mirror number and stripe index to be passed around
  224. * the call chain while we are processing end_io (especially errors).
  225. * Really, what we need is a btrfs_bio structure that has this info
  226. * and is properly sized with its stripe array, but we're not there
  227. * quite yet. We have our own btrfs bioset, and all of the bios
  228. * we allocate are actually btrfs_io_bios. We'll cram as much of
  229. * struct btrfs_bio as we can into this over time.
  230. */
  231. typedef void (btrfs_io_bio_end_io_t) (struct btrfs_io_bio *bio, int err);
  232. struct btrfs_io_bio {
  233. unsigned int mirror_num;
  234. unsigned int stripe_index;
  235. u64 logical;
  236. u8 *csum;
  237. u8 csum_inline[BTRFS_BIO_INLINE_CSUM_SIZE];
  238. u8 *csum_allocated;
  239. btrfs_io_bio_end_io_t *end_io;
  240. struct bvec_iter iter;
  241. /*
  242. * This member must come last, bio_alloc_bioset will allocate enough
  243. * bytes for entire btrfs_io_bio but relies on bio being last.
  244. */
  245. struct bio bio;
  246. };
  247. static inline struct btrfs_io_bio *btrfs_io_bio(struct bio *bio)
  248. {
  249. return container_of(bio, struct btrfs_io_bio, bio);
  250. }
  251. struct btrfs_bio_stripe {
  252. struct btrfs_device *dev;
  253. u64 physical;
  254. u64 length; /* only used for discard mappings */
  255. };
  256. struct btrfs_bio;
  257. typedef void (btrfs_bio_end_io_t) (struct btrfs_bio *bio, int err);
  258. struct btrfs_bio {
  259. refcount_t refs;
  260. atomic_t stripes_pending;
  261. struct btrfs_fs_info *fs_info;
  262. u64 map_type; /* get from map_lookup->type */
  263. bio_end_io_t *end_io;
  264. struct bio *orig_bio;
  265. void *private;
  266. atomic_t error;
  267. int max_errors;
  268. int num_stripes;
  269. int mirror_num;
  270. int num_tgtdevs;
  271. int *tgtdev_map;
  272. /*
  273. * logical block numbers for the start of each stripe
  274. * The last one or two are p/q. These are sorted,
  275. * so raid_map[0] is the start of our full stripe
  276. */
  277. u64 *raid_map;
  278. struct btrfs_bio_stripe stripes[];
  279. };
  280. struct btrfs_device_info {
  281. struct btrfs_device *dev;
  282. u64 dev_offset;
  283. u64 max_avail;
  284. u64 total_avail;
  285. };
  286. struct btrfs_raid_attr {
  287. int sub_stripes; /* sub_stripes info for map */
  288. int dev_stripes; /* stripes per dev */
  289. int devs_max; /* max devs to use */
  290. int devs_min; /* min devs needed */
  291. int tolerated_failures; /* max tolerated fail devs */
  292. int devs_increment; /* ndevs has to be a multiple of this */
  293. int ncopies; /* how many copies to data has */
  294. int mindev_error; /* error code if min devs requisite is unmet */
  295. const char raid_name[8]; /* name of the raid */
  296. u64 bg_flag; /* block group flag of the raid */
  297. };
  298. extern const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES];
  299. struct map_lookup {
  300. u64 type;
  301. int io_align;
  302. int io_width;
  303. u64 stripe_len;
  304. int num_stripes;
  305. int sub_stripes;
  306. int verified_stripes; /* For mount time dev extent verification */
  307. struct btrfs_bio_stripe stripes[];
  308. };
  309. #define map_lookup_size(n) (sizeof(struct map_lookup) + \
  310. (sizeof(struct btrfs_bio_stripe) * (n)))
  311. struct btrfs_balance_args;
  312. struct btrfs_balance_progress;
  313. struct btrfs_balance_control {
  314. struct btrfs_balance_args data;
  315. struct btrfs_balance_args meta;
  316. struct btrfs_balance_args sys;
  317. u64 flags;
  318. struct btrfs_balance_progress stat;
  319. };
  320. enum btrfs_map_op {
  321. BTRFS_MAP_READ,
  322. BTRFS_MAP_WRITE,
  323. BTRFS_MAP_DISCARD,
  324. BTRFS_MAP_GET_READ_MIRRORS,
  325. };
  326. static inline enum btrfs_map_op btrfs_op(struct bio *bio)
  327. {
  328. switch (bio_op(bio)) {
  329. case REQ_OP_DISCARD:
  330. return BTRFS_MAP_DISCARD;
  331. case REQ_OP_WRITE:
  332. return BTRFS_MAP_WRITE;
  333. default:
  334. WARN_ON_ONCE(1);
  335. case REQ_OP_READ:
  336. return BTRFS_MAP_READ;
  337. }
  338. }
  339. void btrfs_get_bbio(struct btrfs_bio *bbio);
  340. void btrfs_put_bbio(struct btrfs_bio *bbio);
  341. int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
  342. u64 logical, u64 *length,
  343. struct btrfs_bio **bbio_ret, int mirror_num);
  344. int btrfs_map_sblock(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
  345. u64 logical, u64 *length,
  346. struct btrfs_bio **bbio_ret);
  347. int btrfs_rmap_block(struct btrfs_fs_info *fs_info, u64 chunk_start,
  348. u64 physical, u64 **logical, int *naddrs, int *stripe_len);
  349. int btrfs_read_sys_array(struct btrfs_fs_info *fs_info);
  350. int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info);
  351. int btrfs_alloc_chunk(struct btrfs_trans_handle *trans, u64 type);
  352. void btrfs_mapping_init(struct btrfs_mapping_tree *tree);
  353. void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree);
  354. blk_status_t btrfs_map_bio(struct btrfs_fs_info *fs_info, struct bio *bio,
  355. int mirror_num, int async_submit);
  356. int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
  357. fmode_t flags, void *holder);
  358. struct btrfs_device *btrfs_scan_one_device(const char *path,
  359. fmode_t flags, void *holder);
  360. int btrfs_close_devices(struct btrfs_fs_devices *fs_devices);
  361. void btrfs_free_extra_devids(struct btrfs_fs_devices *fs_devices, int step);
  362. void btrfs_assign_next_active_device(struct btrfs_device *device,
  363. struct btrfs_device *this_dev);
  364. int btrfs_find_device_missing_or_by_path(struct btrfs_fs_info *fs_info,
  365. const char *device_path,
  366. struct btrfs_device **device);
  367. int btrfs_find_device_by_devspec(struct btrfs_fs_info *fs_info, u64 devid,
  368. const char *devpath,
  369. struct btrfs_device **device);
  370. struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
  371. const u64 *devid,
  372. const u8 *uuid);
  373. void btrfs_free_device(struct btrfs_device *device);
  374. int btrfs_rm_device(struct btrfs_fs_info *fs_info,
  375. const char *device_path, u64 devid);
  376. void __exit btrfs_cleanup_fs_uuids(void);
  377. int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len);
  378. int btrfs_grow_device(struct btrfs_trans_handle *trans,
  379. struct btrfs_device *device, u64 new_size);
  380. struct btrfs_device *btrfs_find_device(struct btrfs_fs_devices *fs_devices,
  381. u64 devid, u8 *uuid, u8 *fsid, bool seed);
  382. int btrfs_shrink_device(struct btrfs_device *device, u64 new_size);
  383. int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *path);
  384. int btrfs_balance(struct btrfs_fs_info *fs_info,
  385. struct btrfs_balance_control *bctl,
  386. struct btrfs_ioctl_balance_args *bargs);
  387. int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info);
  388. int btrfs_recover_balance(struct btrfs_fs_info *fs_info);
  389. int btrfs_pause_balance(struct btrfs_fs_info *fs_info);
  390. int btrfs_cancel_balance(struct btrfs_fs_info *fs_info);
  391. int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info);
  392. int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info);
  393. int btrfs_chunk_readonly(struct btrfs_fs_info *fs_info, u64 chunk_offset);
  394. int find_free_dev_extent_start(struct btrfs_transaction *transaction,
  395. struct btrfs_device *device, u64 num_bytes,
  396. u64 search_start, u64 *start, u64 *max_avail);
  397. int find_free_dev_extent(struct btrfs_trans_handle *trans,
  398. struct btrfs_device *device, u64 num_bytes,
  399. u64 *start, u64 *max_avail);
  400. void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index);
  401. int btrfs_get_dev_stats(struct btrfs_fs_info *fs_info,
  402. struct btrfs_ioctl_get_dev_stats *stats);
  403. void btrfs_init_devices_late(struct btrfs_fs_info *fs_info);
  404. int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info);
  405. int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
  406. struct btrfs_fs_info *fs_info);
  407. void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_device *srcdev);
  408. void btrfs_rm_dev_replace_free_srcdev(struct btrfs_fs_info *fs_info,
  409. struct btrfs_device *srcdev);
  410. void btrfs_destroy_dev_replace_tgtdev(struct btrfs_device *tgtdev);
  411. void btrfs_scratch_superblocks(struct block_device *bdev, const char *device_path);
  412. int btrfs_is_parity_mirror(struct btrfs_fs_info *fs_info,
  413. u64 logical, u64 len);
  414. unsigned long btrfs_full_stripe_len(struct btrfs_fs_info *fs_info,
  415. u64 logical);
  416. int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
  417. u64 chunk_offset, u64 chunk_size);
  418. int btrfs_remove_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset);
  419. static inline void btrfs_dev_stat_inc(struct btrfs_device *dev,
  420. int index)
  421. {
  422. atomic_inc(dev->dev_stat_values + index);
  423. /*
  424. * This memory barrier orders stores updating statistics before stores
  425. * updating dev_stats_ccnt.
  426. *
  427. * It pairs with smp_rmb() in btrfs_run_dev_stats().
  428. */
  429. smp_mb__before_atomic();
  430. atomic_inc(&dev->dev_stats_ccnt);
  431. }
  432. static inline int btrfs_dev_stat_read(struct btrfs_device *dev,
  433. int index)
  434. {
  435. return atomic_read(dev->dev_stat_values + index);
  436. }
  437. static inline int btrfs_dev_stat_read_and_reset(struct btrfs_device *dev,
  438. int index)
  439. {
  440. int ret;
  441. ret = atomic_xchg(dev->dev_stat_values + index, 0);
  442. /*
  443. * atomic_xchg implies a full memory barriers as per atomic_t.txt:
  444. * - RMW operations that have a return value are fully ordered;
  445. *
  446. * This implicit memory barriers is paired with the smp_rmb in
  447. * btrfs_run_dev_stats
  448. */
  449. atomic_inc(&dev->dev_stats_ccnt);
  450. return ret;
  451. }
  452. static inline void btrfs_dev_stat_set(struct btrfs_device *dev,
  453. int index, unsigned long val)
  454. {
  455. atomic_set(dev->dev_stat_values + index, val);
  456. /*
  457. * This memory barrier orders stores updating statistics before stores
  458. * updating dev_stats_ccnt.
  459. *
  460. * It pairs with smp_rmb() in btrfs_run_dev_stats().
  461. */
  462. smp_mb__before_atomic();
  463. atomic_inc(&dev->dev_stats_ccnt);
  464. }
  465. static inline void btrfs_dev_stat_reset(struct btrfs_device *dev,
  466. int index)
  467. {
  468. btrfs_dev_stat_set(dev, index, 0);
  469. }
  470. /*
  471. * Convert block group flags (BTRFS_BLOCK_GROUP_*) to btrfs_raid_types, which
  472. * can be used as index to access btrfs_raid_array[].
  473. */
  474. static inline enum btrfs_raid_types btrfs_bg_flags_to_raid_index(u64 flags)
  475. {
  476. if (flags & BTRFS_BLOCK_GROUP_RAID10)
  477. return BTRFS_RAID_RAID10;
  478. else if (flags & BTRFS_BLOCK_GROUP_RAID1)
  479. return BTRFS_RAID_RAID1;
  480. else if (flags & BTRFS_BLOCK_GROUP_DUP)
  481. return BTRFS_RAID_DUP;
  482. else if (flags & BTRFS_BLOCK_GROUP_RAID0)
  483. return BTRFS_RAID_RAID0;
  484. else if (flags & BTRFS_BLOCK_GROUP_RAID5)
  485. return BTRFS_RAID_RAID5;
  486. else if (flags & BTRFS_BLOCK_GROUP_RAID6)
  487. return BTRFS_RAID_RAID6;
  488. return BTRFS_RAID_SINGLE; /* BTRFS_BLOCK_GROUP_SINGLE */
  489. }
  490. const char *get_raid_name(enum btrfs_raid_types type);
  491. void btrfs_update_commit_device_size(struct btrfs_fs_info *fs_info);
  492. void btrfs_update_commit_device_bytes_used(struct btrfs_transaction *trans);
  493. struct list_head *btrfs_get_fs_uuids(void);
  494. void btrfs_set_fs_info_ptr(struct btrfs_fs_info *fs_info);
  495. void btrfs_reset_fs_info_ptr(struct btrfs_fs_info *fs_info);
  496. bool btrfs_check_rw_degradable(struct btrfs_fs_info *fs_info,
  497. struct btrfs_device *failing_dev);
  498. int btrfs_bg_type_to_factor(u64 flags);
  499. int btrfs_verify_dev_extents(struct btrfs_fs_info *fs_info);
  500. #endif