volumes.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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. 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 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 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 tolerated_failures; /* max tolerated fail devs */
  289. int devs_increment; /* ndevs has to be a multiple of this */
  290. int ncopies; /* how many copies to data has */
  291. };
  292. extern const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES];
  293. extern const int btrfs_raid_mindev_error[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. u64 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. struct btrfs_balance_args;
  308. struct btrfs_balance_progress;
  309. struct btrfs_balance_control {
  310. struct btrfs_fs_info *fs_info;
  311. struct btrfs_balance_args data;
  312. struct btrfs_balance_args meta;
  313. struct btrfs_balance_args sys;
  314. u64 flags;
  315. struct btrfs_balance_progress stat;
  316. };
  317. int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
  318. u64 end, u64 *length);
  319. void btrfs_get_bbio(struct btrfs_bio *bbio);
  320. void btrfs_put_bbio(struct btrfs_bio *bbio);
  321. int btrfs_map_block(struct btrfs_fs_info *fs_info, int op,
  322. u64 logical, u64 *length,
  323. struct btrfs_bio **bbio_ret, int mirror_num);
  324. int btrfs_map_sblock(struct btrfs_fs_info *fs_info, int op,
  325. u64 logical, u64 *length,
  326. struct btrfs_bio **bbio_ret, int mirror_num,
  327. int need_raid_map);
  328. int btrfs_rmap_block(struct btrfs_fs_info *fs_info,
  329. u64 chunk_start, u64 physical, u64 devid,
  330. u64 **logical, int *naddrs, int *stripe_len);
  331. int btrfs_read_sys_array(struct btrfs_root *root);
  332. int btrfs_read_chunk_tree(struct btrfs_root *root);
  333. int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
  334. struct btrfs_root *extent_root, u64 type);
  335. void btrfs_mapping_init(struct btrfs_mapping_tree *tree);
  336. void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree);
  337. int btrfs_map_bio(struct btrfs_root *root, struct bio *bio,
  338. int mirror_num, int async_submit);
  339. int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
  340. fmode_t flags, void *holder);
  341. int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
  342. struct btrfs_fs_devices **fs_devices_ret);
  343. int btrfs_close_devices(struct btrfs_fs_devices *fs_devices);
  344. void btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices, int step);
  345. void btrfs_assign_next_active_device(struct btrfs_fs_info *fs_info,
  346. struct btrfs_device *device, struct btrfs_device *this_dev);
  347. int btrfs_find_device_missing_or_by_path(struct btrfs_root *root,
  348. char *device_path,
  349. struct btrfs_device **device);
  350. int btrfs_find_device_by_devspec(struct btrfs_root *root, u64 devid,
  351. char *devpath,
  352. struct btrfs_device **device);
  353. struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
  354. const u64 *devid,
  355. const u8 *uuid);
  356. int btrfs_rm_device(struct btrfs_root *root, char *device_path, u64 devid);
  357. void btrfs_cleanup_fs_uuids(void);
  358. int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len);
  359. int btrfs_grow_device(struct btrfs_trans_handle *trans,
  360. struct btrfs_device *device, u64 new_size);
  361. struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
  362. u8 *uuid, u8 *fsid);
  363. int btrfs_shrink_device(struct btrfs_device *device, u64 new_size);
  364. int btrfs_init_new_device(struct btrfs_root *root, char *path);
  365. int btrfs_init_dev_replace_tgtdev(struct btrfs_root *root, char *device_path,
  366. struct btrfs_device *srcdev,
  367. struct btrfs_device **device_out);
  368. int btrfs_balance(struct btrfs_balance_control *bctl,
  369. struct btrfs_ioctl_balance_args *bargs);
  370. int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info);
  371. int btrfs_recover_balance(struct btrfs_fs_info *fs_info);
  372. int btrfs_pause_balance(struct btrfs_fs_info *fs_info);
  373. int btrfs_cancel_balance(struct btrfs_fs_info *fs_info);
  374. int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info);
  375. int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info);
  376. int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset);
  377. int find_free_dev_extent_start(struct btrfs_transaction *transaction,
  378. struct btrfs_device *device, u64 num_bytes,
  379. u64 search_start, u64 *start, u64 *max_avail);
  380. int find_free_dev_extent(struct btrfs_trans_handle *trans,
  381. struct btrfs_device *device, u64 num_bytes,
  382. u64 *start, u64 *max_avail);
  383. void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index);
  384. int btrfs_get_dev_stats(struct btrfs_root *root,
  385. struct btrfs_ioctl_get_dev_stats *stats);
  386. void btrfs_init_devices_late(struct btrfs_fs_info *fs_info);
  387. int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info);
  388. int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
  389. struct btrfs_fs_info *fs_info);
  390. void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_fs_info *fs_info,
  391. struct btrfs_device *srcdev);
  392. void btrfs_rm_dev_replace_free_srcdev(struct btrfs_fs_info *fs_info,
  393. struct btrfs_device *srcdev);
  394. void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
  395. struct btrfs_device *tgtdev);
  396. void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info,
  397. struct btrfs_device *tgtdev);
  398. void btrfs_scratch_superblocks(struct block_device *bdev, char *device_path);
  399. int btrfs_is_parity_mirror(struct btrfs_mapping_tree *map_tree,
  400. u64 logical, u64 len, int mirror_num);
  401. unsigned long btrfs_full_stripe_len(struct btrfs_root *root,
  402. struct btrfs_mapping_tree *map_tree,
  403. u64 logical);
  404. int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
  405. struct btrfs_root *extent_root,
  406. u64 chunk_offset, u64 chunk_size);
  407. int btrfs_remove_chunk(struct btrfs_trans_handle *trans,
  408. struct btrfs_root *root, u64 chunk_offset);
  409. static inline int btrfs_dev_stats_dirty(struct btrfs_device *dev)
  410. {
  411. return atomic_read(&dev->dev_stats_ccnt);
  412. }
  413. static inline void btrfs_dev_stat_inc(struct btrfs_device *dev,
  414. int index)
  415. {
  416. atomic_inc(dev->dev_stat_values + index);
  417. smp_mb__before_atomic();
  418. atomic_inc(&dev->dev_stats_ccnt);
  419. }
  420. static inline int btrfs_dev_stat_read(struct btrfs_device *dev,
  421. int index)
  422. {
  423. return atomic_read(dev->dev_stat_values + index);
  424. }
  425. static inline int btrfs_dev_stat_read_and_reset(struct btrfs_device *dev,
  426. int index)
  427. {
  428. int ret;
  429. ret = atomic_xchg(dev->dev_stat_values + index, 0);
  430. smp_mb__before_atomic();
  431. atomic_inc(&dev->dev_stats_ccnt);
  432. return ret;
  433. }
  434. static inline void btrfs_dev_stat_set(struct btrfs_device *dev,
  435. int index, unsigned long val)
  436. {
  437. atomic_set(dev->dev_stat_values + index, val);
  438. smp_mb__before_atomic();
  439. atomic_inc(&dev->dev_stats_ccnt);
  440. }
  441. static inline void btrfs_dev_stat_reset(struct btrfs_device *dev,
  442. int index)
  443. {
  444. btrfs_dev_stat_set(dev, index, 0);
  445. }
  446. void btrfs_update_commit_device_size(struct btrfs_fs_info *fs_info);
  447. void btrfs_update_commit_device_bytes_used(struct btrfs_root *root,
  448. struct btrfs_transaction *transaction);
  449. static inline void lock_chunks(struct btrfs_root *root)
  450. {
  451. mutex_lock(&root->fs_info->chunk_mutex);
  452. }
  453. static inline void unlock_chunks(struct btrfs_root *root)
  454. {
  455. mutex_unlock(&root->fs_info->chunk_mutex);
  456. }
  457. struct list_head *btrfs_get_fs_uuids(void);
  458. void btrfs_set_fs_info_ptr(struct btrfs_fs_info *fs_info);
  459. void btrfs_reset_fs_info_ptr(struct btrfs_fs_info *fs_info);
  460. #endif