volumes.h 17 KB

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