volumes.h 16 KB

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