volumes.h 16 KB

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