volumes.h 16 KB

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