volumes.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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 (64 * 1024)
  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. struct btrfs_device {
  44. struct list_head dev_list;
  45. struct list_head dev_alloc_list;
  46. struct btrfs_fs_devices *fs_devices;
  47. struct btrfs_root *dev_root;
  48. struct rcu_string *name;
  49. u64 generation;
  50. spinlock_t io_lock ____cacheline_aligned;
  51. int running_pending;
  52. /* regular prio bios */
  53. struct btrfs_pending_bios pending_bios;
  54. /* WRITE_SYNC bios */
  55. struct btrfs_pending_bios pending_sync_bios;
  56. struct block_device *bdev;
  57. /* the mode sent to blkdev_get */
  58. fmode_t mode;
  59. int writeable;
  60. int in_fs_metadata;
  61. int missing;
  62. int can_discard;
  63. int is_tgtdev_for_dev_replace;
  64. #ifdef __BTRFS_NEED_DEVICE_DATA_ORDERED
  65. seqcount_t data_seqcount;
  66. #endif
  67. /* the internal btrfs device id */
  68. u64 devid;
  69. /* size of the device in memory */
  70. u64 total_bytes;
  71. /* size of the device on disk */
  72. u64 disk_total_bytes;
  73. /* bytes used */
  74. u64 bytes_used;
  75. /* optimal io alignment for this device */
  76. u32 io_align;
  77. /* optimal io width for this device */
  78. u32 io_width;
  79. /* type and info about this device */
  80. u64 type;
  81. /* minimal io size for this device */
  82. u32 sector_size;
  83. /* physical drive uuid (or lvm uuid) */
  84. u8 uuid[BTRFS_UUID_SIZE];
  85. /*
  86. * size of the device on the current transaction
  87. *
  88. * This variant is update when committing the transaction,
  89. * and protected by device_list_mutex
  90. */
  91. u64 commit_total_bytes;
  92. /* bytes used on the current transaction */
  93. u64 commit_bytes_used;
  94. /*
  95. * used to manage the device which is resized
  96. *
  97. * It is protected by chunk_lock.
  98. */
  99. struct list_head resized_list;
  100. /* for sending down flush barriers */
  101. int nobarriers;
  102. struct bio *flush_bio;
  103. struct completion flush_wait;
  104. /* per-device scrub information */
  105. struct scrub_ctx *scrub_device;
  106. struct btrfs_work work;
  107. struct rcu_head rcu;
  108. struct work_struct rcu_work;
  109. /* readahead state */
  110. spinlock_t reada_lock;
  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 super_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 bio bio;
  242. };
  243. static inline struct btrfs_io_bio *btrfs_io_bio(struct bio *bio)
  244. {
  245. return container_of(bio, struct btrfs_io_bio, bio);
  246. }
  247. struct btrfs_bio_stripe {
  248. struct btrfs_device *dev;
  249. u64 physical;
  250. u64 length; /* only used for discard mappings */
  251. };
  252. struct btrfs_bio;
  253. typedef void (btrfs_bio_end_io_t) (struct btrfs_bio *bio, int err);
  254. struct btrfs_bio {
  255. atomic_t refs;
  256. atomic_t stripes_pending;
  257. struct btrfs_fs_info *fs_info;
  258. u64 map_type; /* get from map_lookup->type */
  259. bio_end_io_t *end_io;
  260. struct bio *orig_bio;
  261. unsigned long flags;
  262. void *private;
  263. atomic_t error;
  264. int max_errors;
  265. int num_stripes;
  266. int mirror_num;
  267. int num_tgtdevs;
  268. int *tgtdev_map;
  269. /*
  270. * logical block numbers for the start of each stripe
  271. * The last one or two are p/q. These are sorted,
  272. * so raid_map[0] is the start of our full stripe
  273. */
  274. u64 *raid_map;
  275. struct btrfs_bio_stripe stripes[];
  276. };
  277. struct btrfs_device_info {
  278. struct btrfs_device *dev;
  279. u64 dev_offset;
  280. u64 max_avail;
  281. u64 total_avail;
  282. };
  283. struct btrfs_raid_attr {
  284. int sub_stripes; /* sub_stripes info for map */
  285. int dev_stripes; /* stripes per dev */
  286. int devs_max; /* max devs to use */
  287. int devs_min; /* min devs needed */
  288. int devs_increment; /* ndevs has to be a multiple of this */
  289. int ncopies; /* how many copies to data has */
  290. };
  291. struct map_lookup {
  292. u64 type;
  293. int io_align;
  294. int io_width;
  295. int stripe_len;
  296. int sector_size;
  297. int num_stripes;
  298. int sub_stripes;
  299. struct btrfs_bio_stripe stripes[];
  300. };
  301. #define map_lookup_size(n) (sizeof(struct map_lookup) + \
  302. (sizeof(struct btrfs_bio_stripe) * (n)))
  303. /*
  304. * Restriper's general type filter
  305. */
  306. #define BTRFS_BALANCE_DATA (1ULL << 0)
  307. #define BTRFS_BALANCE_SYSTEM (1ULL << 1)
  308. #define BTRFS_BALANCE_METADATA (1ULL << 2)
  309. #define BTRFS_BALANCE_TYPE_MASK (BTRFS_BALANCE_DATA | \
  310. BTRFS_BALANCE_SYSTEM | \
  311. BTRFS_BALANCE_METADATA)
  312. #define BTRFS_BALANCE_FORCE (1ULL << 3)
  313. #define BTRFS_BALANCE_RESUME (1ULL << 4)
  314. /*
  315. * Balance filters
  316. */
  317. #define BTRFS_BALANCE_ARGS_PROFILES (1ULL << 0)
  318. #define BTRFS_BALANCE_ARGS_USAGE (1ULL << 1)
  319. #define BTRFS_BALANCE_ARGS_DEVID (1ULL << 2)
  320. #define BTRFS_BALANCE_ARGS_DRANGE (1ULL << 3)
  321. #define BTRFS_BALANCE_ARGS_VRANGE (1ULL << 4)
  322. #define BTRFS_BALANCE_ARGS_LIMIT (1ULL << 5)
  323. /*
  324. * Profile changing flags. When SOFT is set we won't relocate chunk if
  325. * it already has the target profile (even though it may be
  326. * half-filled).
  327. */
  328. #define BTRFS_BALANCE_ARGS_CONVERT (1ULL << 8)
  329. #define BTRFS_BALANCE_ARGS_SOFT (1ULL << 9)
  330. struct btrfs_balance_args;
  331. struct btrfs_balance_progress;
  332. struct btrfs_balance_control {
  333. struct btrfs_fs_info *fs_info;
  334. struct btrfs_balance_args data;
  335. struct btrfs_balance_args meta;
  336. struct btrfs_balance_args sys;
  337. u64 flags;
  338. struct btrfs_balance_progress stat;
  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, int rw,
  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, int rw,
  348. u64 logical, u64 *length,
  349. struct btrfs_bio **bbio_ret, int mirror_num,
  350. int need_raid_map);
  351. int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
  352. u64 chunk_start, u64 physical, u64 devid,
  353. u64 **logical, int *naddrs, int *stripe_len);
  354. int btrfs_read_sys_array(struct btrfs_root *root);
  355. int btrfs_read_chunk_tree(struct btrfs_root *root);
  356. int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
  357. struct btrfs_root *extent_root, u64 type);
  358. void btrfs_mapping_init(struct btrfs_mapping_tree *tree);
  359. void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree);
  360. int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
  361. int mirror_num, int async_submit);
  362. int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
  363. fmode_t flags, void *holder);
  364. int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
  365. struct btrfs_fs_devices **fs_devices_ret);
  366. int btrfs_close_devices(struct btrfs_fs_devices *fs_devices);
  367. void btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices, int step);
  368. int btrfs_find_device_missing_or_by_path(struct btrfs_root *root,
  369. char *device_path,
  370. struct btrfs_device **device);
  371. struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
  372. const u64 *devid,
  373. const u8 *uuid);
  374. int btrfs_rm_device(struct btrfs_root *root, char *device_path);
  375. void btrfs_cleanup_fs_uuids(void);
  376. int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len);
  377. int btrfs_grow_device(struct btrfs_trans_handle *trans,
  378. struct btrfs_device *device, u64 new_size);
  379. struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
  380. u8 *uuid, u8 *fsid);
  381. int btrfs_shrink_device(struct btrfs_device *device, u64 new_size);
  382. int btrfs_init_new_device(struct btrfs_root *root, char *path);
  383. int btrfs_init_dev_replace_tgtdev(struct btrfs_root *root, char *device_path,
  384. struct btrfs_device *srcdev,
  385. struct btrfs_device **device_out);
  386. int btrfs_balance(struct btrfs_balance_control *bctl,
  387. struct btrfs_ioctl_balance_args *bargs);
  388. int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info);
  389. int btrfs_recover_balance(struct btrfs_fs_info *fs_info);
  390. int btrfs_pause_balance(struct btrfs_fs_info *fs_info);
  391. int btrfs_cancel_balance(struct btrfs_fs_info *fs_info);
  392. int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info);
  393. int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info);
  394. int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset);
  395. int find_free_dev_extent(struct btrfs_trans_handle *trans,
  396. struct btrfs_device *device, u64 num_bytes,
  397. u64 *start, u64 *max_avail);
  398. void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index);
  399. int btrfs_get_dev_stats(struct btrfs_root *root,
  400. struct btrfs_ioctl_get_dev_stats *stats);
  401. void btrfs_init_devices_late(struct btrfs_fs_info *fs_info);
  402. int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info);
  403. int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
  404. struct btrfs_fs_info *fs_info);
  405. void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_fs_info *fs_info,
  406. struct btrfs_device *srcdev);
  407. void btrfs_rm_dev_replace_free_srcdev(struct btrfs_fs_info *fs_info,
  408. struct btrfs_device *srcdev);
  409. void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
  410. struct btrfs_device *tgtdev);
  411. void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info,
  412. struct btrfs_device *tgtdev);
  413. int btrfs_scratch_superblock(struct btrfs_device *device);
  414. int btrfs_is_parity_mirror(struct btrfs_mapping_tree *map_tree,
  415. u64 logical, u64 len, int mirror_num);
  416. unsigned long btrfs_full_stripe_len(struct btrfs_root *root,
  417. struct btrfs_mapping_tree *map_tree,
  418. u64 logical);
  419. int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
  420. struct btrfs_root *extent_root,
  421. u64 chunk_offset, u64 chunk_size);
  422. int btrfs_remove_chunk(struct btrfs_trans_handle *trans,
  423. struct btrfs_root *root, u64 chunk_offset);
  424. static inline int btrfs_dev_stats_dirty(struct btrfs_device *dev)
  425. {
  426. return atomic_read(&dev->dev_stats_ccnt);
  427. }
  428. static inline void btrfs_dev_stat_inc(struct btrfs_device *dev,
  429. int index)
  430. {
  431. atomic_inc(dev->dev_stat_values + index);
  432. smp_mb__before_atomic();
  433. atomic_inc(&dev->dev_stats_ccnt);
  434. }
  435. static inline int btrfs_dev_stat_read(struct btrfs_device *dev,
  436. int index)
  437. {
  438. return atomic_read(dev->dev_stat_values + index);
  439. }
  440. static inline int btrfs_dev_stat_read_and_reset(struct btrfs_device *dev,
  441. int index)
  442. {
  443. int ret;
  444. ret = atomic_xchg(dev->dev_stat_values + index, 0);
  445. smp_mb__before_atomic();
  446. atomic_inc(&dev->dev_stats_ccnt);
  447. return ret;
  448. }
  449. static inline void btrfs_dev_stat_set(struct btrfs_device *dev,
  450. int index, unsigned long val)
  451. {
  452. atomic_set(dev->dev_stat_values + index, val);
  453. smp_mb__before_atomic();
  454. atomic_inc(&dev->dev_stats_ccnt);
  455. }
  456. static inline void btrfs_dev_stat_reset(struct btrfs_device *dev,
  457. int index)
  458. {
  459. btrfs_dev_stat_set(dev, index, 0);
  460. }
  461. void btrfs_update_commit_device_size(struct btrfs_fs_info *fs_info);
  462. void btrfs_update_commit_device_bytes_used(struct btrfs_root *root,
  463. struct btrfs_transaction *transaction);
  464. static inline void lock_chunks(struct btrfs_root *root)
  465. {
  466. mutex_lock(&root->fs_info->chunk_mutex);
  467. }
  468. static inline void unlock_chunks(struct btrfs_root *root)
  469. {
  470. mutex_unlock(&root->fs_info->chunk_mutex);
  471. }
  472. struct list_head *btrfs_get_fs_uuids(void);
  473. void btrfs_set_fs_info_ptr(struct btrfs_fs_info *fs_info);
  474. void btrfs_reset_fs_info_ptr(struct btrfs_fs_info *fs_info);
  475. #endif