19888c340SDavid Sterba /* SPDX-License-Identifier: GPL-2.0 */ 20b86a832SChris Mason /* 30b86a832SChris Mason * Copyright (C) 2007 Oracle. All rights reserved. 40b86a832SChris Mason */ 50b86a832SChris Mason 69888c340SDavid Sterba #ifndef BTRFS_VOLUMES_H 79888c340SDavid Sterba #define BTRFS_VOLUMES_H 88790d502SChris Mason 9cea9e445SChris Mason #include <linux/bio.h> 10b2117a39SMiao Xie #include <linux/sort.h> 1155e301fdSFilipe Brandenburger #include <linux/btrfs.h> 128b712842SChris Mason #include "async-thread.h" 13cea9e445SChris Mason 1467a2c45eSMiao Xie extern struct mutex uuid_mutex; 1567a2c45eSMiao Xie 16ee22184bSByongho Lee #define BTRFS_STRIPE_LEN SZ_64K 17b2117a39SMiao Xie 18f2984462SChris Mason struct buffer_head; 19ffbd517dSChris Mason struct btrfs_pending_bios { 20ffbd517dSChris Mason struct bio *head; 21ffbd517dSChris Mason struct bio *tail; 22ffbd517dSChris Mason }; 23ffbd517dSChris Mason 247cc8e58dSMiao Xie /* 257cc8e58dSMiao Xie * Use sequence counter to get consistent device stat data on 267cc8e58dSMiao Xie * 32-bit processors. 277cc8e58dSMiao Xie */ 287cc8e58dSMiao Xie #if BITS_PER_LONG==32 && defined(CONFIG_SMP) 297cc8e58dSMiao Xie #include <linux/seqlock.h> 307cc8e58dSMiao Xie #define __BTRFS_NEED_DEVICE_DATA_ORDERED 317cc8e58dSMiao Xie #define btrfs_device_data_ordered_init(device) \ 327cc8e58dSMiao Xie seqcount_init(&device->data_seqcount) 337cc8e58dSMiao Xie #else 347cc8e58dSMiao Xie #define btrfs_device_data_ordered_init(device) do { } while (0) 357cc8e58dSMiao Xie #endif 367cc8e58dSMiao Xie 37ebbede42SAnand Jain #define BTRFS_DEV_STATE_WRITEABLE (0) 38e12c9621SAnand Jain #define BTRFS_DEV_STATE_IN_FS_METADATA (1) 39e6e674bdSAnand Jain #define BTRFS_DEV_STATE_MISSING (2) 40401e29c1SAnand Jain #define BTRFS_DEV_STATE_REPLACE_TGT (3) 411c3063b6SAnand Jain #define BTRFS_DEV_STATE_FLUSH_SENT (4) 42ebbede42SAnand Jain 430b86a832SChris Mason struct btrfs_device { 440b86a832SChris Mason struct list_head dev_list; 45b3075717SChris Mason struct list_head dev_alloc_list; 462b82032cSYan Zheng struct btrfs_fs_devices *fs_devices; 47fb456252SJeff Mahoney struct btrfs_fs_info *fs_info; 48ffbd517dSChris Mason 49d5ee37bcSMiao Xie struct rcu_string *name; 50d5ee37bcSMiao Xie 51d5ee37bcSMiao Xie u64 generation; 52d5ee37bcSMiao Xie 53d5ee37bcSMiao Xie spinlock_t io_lock ____cacheline_aligned; 54d5ee37bcSMiao Xie int running_pending; 55ffbd517dSChris Mason /* regular prio bios */ 56ffbd517dSChris Mason struct btrfs_pending_bios pending_bios; 5770fd7614SChristoph Hellwig /* sync bios */ 58ffbd517dSChris Mason struct btrfs_pending_bios pending_sync_bios; 59ffbd517dSChris Mason 60d5ee37bcSMiao Xie struct block_device *bdev; 61d5ee37bcSMiao Xie 62d5ee37bcSMiao Xie /* the mode sent to blkdev_get */ 63d5ee37bcSMiao Xie fmode_t mode; 64d5ee37bcSMiao Xie 65ebbede42SAnand Jain unsigned long dev_state; 6658efbc9fSOmar Sandoval blk_status_t last_flush_error; 67e0ae9994SDavid Sterba int flush_bio_sent; 68b3075717SChris Mason 697cc8e58dSMiao Xie #ifdef __BTRFS_NEED_DEVICE_DATA_ORDERED 707cc8e58dSMiao Xie seqcount_t data_seqcount; 717cc8e58dSMiao Xie #endif 727cc8e58dSMiao Xie 730b86a832SChris Mason /* the internal btrfs device id */ 740b86a832SChris Mason u64 devid; 750b86a832SChris Mason 766ba40b61SMiao Xie /* size of the device in memory */ 770b86a832SChris Mason u64 total_bytes; 780b86a832SChris Mason 796ba40b61SMiao Xie /* size of the device on disk */ 80d6397baeSChris Ball u64 disk_total_bytes; 81d6397baeSChris Ball 820b86a832SChris Mason /* bytes used */ 830b86a832SChris Mason u64 bytes_used; 840b86a832SChris Mason 850b86a832SChris Mason /* optimal io alignment for this device */ 860b86a832SChris Mason u32 io_align; 870b86a832SChris Mason 880b86a832SChris Mason /* optimal io width for this device */ 890b86a832SChris Mason u32 io_width; 903c45bfc1SDulshani Gunawardhana /* type and info about this device */ 913c45bfc1SDulshani Gunawardhana u64 type; 920b86a832SChris Mason 930b86a832SChris Mason /* minimal io size for this device */ 940b86a832SChris Mason u32 sector_size; 950b86a832SChris Mason 960b86a832SChris Mason /* physical drive uuid (or lvm uuid) */ 97e17cade2SChris Mason u8 uuid[BTRFS_UUID_SIZE]; 988b712842SChris Mason 99935e5cc9SMiao Xie /* 100935e5cc9SMiao Xie * size of the device on the current transaction 101935e5cc9SMiao Xie * 102935e5cc9SMiao Xie * This variant is update when committing the transaction, 103935e5cc9SMiao Xie * and protected by device_list_mutex 104935e5cc9SMiao Xie */ 105935e5cc9SMiao Xie u64 commit_total_bytes; 106935e5cc9SMiao Xie 107ce7213c7SMiao Xie /* bytes used on the current transaction */ 108ce7213c7SMiao Xie u64 commit_bytes_used; 109935e5cc9SMiao Xie /* 110935e5cc9SMiao Xie * used to manage the device which is resized 111935e5cc9SMiao Xie * 112935e5cc9SMiao Xie * It is protected by chunk_lock. 113935e5cc9SMiao Xie */ 114935e5cc9SMiao Xie struct list_head resized_list; 115935e5cc9SMiao Xie 1163c45bfc1SDulshani Gunawardhana /* for sending down flush barriers */ 1173c45bfc1SDulshani Gunawardhana struct bio *flush_bio; 1183c45bfc1SDulshani Gunawardhana struct completion flush_wait; 1193c45bfc1SDulshani Gunawardhana 120a2de733cSArne Jansen /* per-device scrub information */ 121cadbc0a0SAnand Jain struct scrub_ctx *scrub_ctx; 122a2de733cSArne Jansen 123d458b054SQu Wenruo struct btrfs_work work; 1241f78160cSXiao Guangrong struct rcu_head rcu; 12590519d66SArne Jansen 12690519d66SArne Jansen /* readahead state */ 12790519d66SArne Jansen atomic_t reada_in_flight; 12890519d66SArne Jansen u64 reada_next; 12990519d66SArne Jansen struct reada_zone *reada_curr_zone; 13090519d66SArne Jansen struct radix_tree_root reada_zones; 13190519d66SArne Jansen struct radix_tree_root reada_extents; 132387125fcSChris Mason 133442a4f63SStefan Behrens /* disk I/O failure stats. For detailed description refer to 134442a4f63SStefan Behrens * enum btrfs_dev_stat_values in ioctl.h */ 135733f4fbbSStefan Behrens int dev_stats_valid; 136addc3fa7SMiao Xie 137addc3fa7SMiao Xie /* Counter to record the change of device stats */ 138addc3fa7SMiao Xie atomic_t dev_stats_ccnt; 139442a4f63SStefan Behrens atomic_t dev_stat_values[BTRFS_DEV_STAT_VALUES_MAX]; 1400b86a832SChris Mason }; 1410b86a832SChris Mason 1427cc8e58dSMiao Xie /* 1437cc8e58dSMiao Xie * If we read those variants at the context of their own lock, we needn't 1447cc8e58dSMiao Xie * use the following helpers, reading them directly is safe. 1457cc8e58dSMiao Xie */ 1467cc8e58dSMiao Xie #if BITS_PER_LONG==32 && defined(CONFIG_SMP) 1477cc8e58dSMiao Xie #define BTRFS_DEVICE_GETSET_FUNCS(name) \ 1487cc8e58dSMiao Xie static inline u64 \ 1497cc8e58dSMiao Xie btrfs_device_get_##name(const struct btrfs_device *dev) \ 1507cc8e58dSMiao Xie { \ 1517cc8e58dSMiao Xie u64 size; \ 1527cc8e58dSMiao Xie unsigned int seq; \ 1537cc8e58dSMiao Xie \ 1547cc8e58dSMiao Xie do { \ 1557cc8e58dSMiao Xie seq = read_seqcount_begin(&dev->data_seqcount); \ 1567cc8e58dSMiao Xie size = dev->name; \ 1577cc8e58dSMiao Xie } while (read_seqcount_retry(&dev->data_seqcount, seq)); \ 1587cc8e58dSMiao Xie return size; \ 1597cc8e58dSMiao Xie } \ 1607cc8e58dSMiao Xie \ 1617cc8e58dSMiao Xie static inline void \ 1627cc8e58dSMiao Xie btrfs_device_set_##name(struct btrfs_device *dev, u64 size) \ 1637cc8e58dSMiao Xie { \ 1647cc8e58dSMiao Xie preempt_disable(); \ 1657cc8e58dSMiao Xie write_seqcount_begin(&dev->data_seqcount); \ 1667cc8e58dSMiao Xie dev->name = size; \ 1677cc8e58dSMiao Xie write_seqcount_end(&dev->data_seqcount); \ 1687cc8e58dSMiao Xie preempt_enable(); \ 1697cc8e58dSMiao Xie } 1707cc8e58dSMiao Xie #elif BITS_PER_LONG==32 && defined(CONFIG_PREEMPT) 1717cc8e58dSMiao Xie #define BTRFS_DEVICE_GETSET_FUNCS(name) \ 1727cc8e58dSMiao Xie static inline u64 \ 1737cc8e58dSMiao Xie btrfs_device_get_##name(const struct btrfs_device *dev) \ 1747cc8e58dSMiao Xie { \ 1757cc8e58dSMiao Xie u64 size; \ 1767cc8e58dSMiao Xie \ 1777cc8e58dSMiao Xie preempt_disable(); \ 1787cc8e58dSMiao Xie size = dev->name; \ 1797cc8e58dSMiao Xie preempt_enable(); \ 1807cc8e58dSMiao Xie return size; \ 1817cc8e58dSMiao Xie } \ 1827cc8e58dSMiao Xie \ 1837cc8e58dSMiao Xie static inline void \ 1847cc8e58dSMiao Xie btrfs_device_set_##name(struct btrfs_device *dev, u64 size) \ 1857cc8e58dSMiao Xie { \ 1867cc8e58dSMiao Xie preempt_disable(); \ 1877cc8e58dSMiao Xie dev->name = size; \ 1887cc8e58dSMiao Xie preempt_enable(); \ 1897cc8e58dSMiao Xie } 1907cc8e58dSMiao Xie #else 1917cc8e58dSMiao Xie #define BTRFS_DEVICE_GETSET_FUNCS(name) \ 1927cc8e58dSMiao Xie static inline u64 \ 1937cc8e58dSMiao Xie btrfs_device_get_##name(const struct btrfs_device *dev) \ 1947cc8e58dSMiao Xie { \ 1957cc8e58dSMiao Xie return dev->name; \ 1967cc8e58dSMiao Xie } \ 1977cc8e58dSMiao Xie \ 1987cc8e58dSMiao Xie static inline void \ 1997cc8e58dSMiao Xie btrfs_device_set_##name(struct btrfs_device *dev, u64 size) \ 2007cc8e58dSMiao Xie { \ 2017cc8e58dSMiao Xie dev->name = size; \ 2027cc8e58dSMiao Xie } 2037cc8e58dSMiao Xie #endif 2047cc8e58dSMiao Xie 2057cc8e58dSMiao Xie BTRFS_DEVICE_GETSET_FUNCS(total_bytes); 2067cc8e58dSMiao Xie BTRFS_DEVICE_GETSET_FUNCS(disk_total_bytes); 2077cc8e58dSMiao Xie BTRFS_DEVICE_GETSET_FUNCS(bytes_used); 2087cc8e58dSMiao Xie 2098a4b83ccSChris Mason struct btrfs_fs_devices { 2108a4b83ccSChris Mason u8 fsid[BTRFS_FSID_SIZE]; /* FS specific uuid */ 211*c4babc5eSAnand Jain struct list_head fs_list; 2128a4b83ccSChris Mason 2138a4b83ccSChris Mason u64 num_devices; 214a0af469bSChris Mason u64 open_devices; 2152b82032cSYan Zheng u64 rw_devices; 216cd02dca5SChris Mason u64 missing_devices; 2172b82032cSYan Zheng u64 total_rw_bytes; 21802db0844SJosef Bacik u64 total_devices; 2198a4b83ccSChris Mason struct block_device *latest_bdev; 220e5e9a520SChris Mason 221e5e9a520SChris Mason /* all of the devices in the FS, protected by a mutex 222e5e9a520SChris Mason * so we can safely walk it to write out the supers without 2239b011adfSWang Shilong * worrying about add/remove by the multi-device code. 2249b011adfSWang Shilong * Scrubbing super can kick off supers writing by holding 2259b011adfSWang Shilong * this mutex lock. 226e5e9a520SChris Mason */ 227e5e9a520SChris Mason struct mutex device_list_mutex; 2288a4b83ccSChris Mason struct list_head devices; 229b3075717SChris Mason 230935e5cc9SMiao Xie struct list_head resized_devices; 231b3075717SChris Mason /* devices not currently being allocated */ 232b3075717SChris Mason struct list_head alloc_list; 2332b82032cSYan Zheng 2342b82032cSYan Zheng struct btrfs_fs_devices *seed; 2352b82032cSYan Zheng int seeding; 2362b82032cSYan Zheng 2372b82032cSYan Zheng int opened; 238c289811cSChris Mason 239c289811cSChris Mason /* set when we find or add a device that doesn't have the 240c289811cSChris Mason * nonrot flag set 241c289811cSChris Mason */ 242c289811cSChris Mason int rotating; 2432e7910d6SAnand Jain 2445a13f430SAnand Jain struct btrfs_fs_info *fs_info; 2452e7910d6SAnand Jain /* sysfs kobjects */ 246c1b7e474SAnand Jain struct kobject fsid_kobj; 2472e7910d6SAnand Jain struct kobject *device_dir_kobj; 2482e7910d6SAnand Jain struct completion kobj_unregister; 2498a4b83ccSChris Mason }; 2508a4b83ccSChris Mason 251facc8a22SMiao Xie #define BTRFS_BIO_INLINE_CSUM_SIZE 64 252facc8a22SMiao Xie 2539be3395bSChris Mason /* 2549be3395bSChris Mason * we need the mirror number and stripe index to be passed around 2559be3395bSChris Mason * the call chain while we are processing end_io (especially errors). 2569be3395bSChris Mason * Really, what we need is a btrfs_bio structure that has this info 2579be3395bSChris Mason * and is properly sized with its stripe array, but we're not there 2589be3395bSChris Mason * quite yet. We have our own btrfs bioset, and all of the bios 2599be3395bSChris Mason * we allocate are actually btrfs_io_bios. We'll cram as much of 2609be3395bSChris Mason * struct btrfs_bio as we can into this over time. 2619be3395bSChris Mason */ 262facc8a22SMiao Xie typedef void (btrfs_io_bio_end_io_t) (struct btrfs_io_bio *bio, int err); 2639be3395bSChris Mason struct btrfs_io_bio { 264c1dc0896SMiao Xie unsigned int mirror_num; 265c1dc0896SMiao Xie unsigned int stripe_index; 266c1dc0896SMiao Xie u64 logical; 267facc8a22SMiao Xie u8 *csum; 268facc8a22SMiao Xie u8 csum_inline[BTRFS_BIO_INLINE_CSUM_SIZE]; 269facc8a22SMiao Xie u8 *csum_allocated; 270facc8a22SMiao Xie btrfs_io_bio_end_io_t *end_io; 27117347cecSLiu Bo struct bvec_iter iter; 272fa1bcbe0SDavid Sterba /* 273fa1bcbe0SDavid Sterba * This member must come last, bio_alloc_bioset will allocate enough 274fa1bcbe0SDavid Sterba * bytes for entire btrfs_io_bio but relies on bio being last. 275fa1bcbe0SDavid Sterba */ 2769be3395bSChris Mason struct bio bio; 2779be3395bSChris Mason }; 2789be3395bSChris Mason 2799be3395bSChris Mason static inline struct btrfs_io_bio *btrfs_io_bio(struct bio *bio) 2809be3395bSChris Mason { 2819be3395bSChris Mason return container_of(bio, struct btrfs_io_bio, bio); 2829be3395bSChris Mason } 2839be3395bSChris Mason 284cea9e445SChris Mason struct btrfs_bio_stripe { 285cea9e445SChris Mason struct btrfs_device *dev; 286cea9e445SChris Mason u64 physical; 287fce3bb9aSLi Dongyang u64 length; /* only used for discard mappings */ 288cea9e445SChris Mason }; 289cea9e445SChris Mason 290a1d3c478SJan Schmidt struct btrfs_bio; 291a1d3c478SJan Schmidt typedef void (btrfs_bio_end_io_t) (struct btrfs_bio *bio, int err); 292a1d3c478SJan Schmidt 293a1d3c478SJan Schmidt struct btrfs_bio { 294140475aeSElena Reshetova refcount_t refs; 295cea9e445SChris Mason atomic_t stripes_pending; 296c404e0dcSMiao Xie struct btrfs_fs_info *fs_info; 29710f11900SZhao Lei u64 map_type; /* get from map_lookup->type */ 298cea9e445SChris Mason bio_end_io_t *end_io; 2997d2b4daaSChris Mason struct bio *orig_bio; 300c55f1396SMiao Xie unsigned long flags; 301cea9e445SChris Mason void *private; 302a236aed1SChris Mason atomic_t error; 303a236aed1SChris Mason int max_errors; 304cea9e445SChris Mason int num_stripes; 305a1d3c478SJan Schmidt int mirror_num; 3062c8cdd6eSMiao Xie int num_tgtdevs; 3072c8cdd6eSMiao Xie int *tgtdev_map; 3088e5cfb55SZhao Lei /* 3098e5cfb55SZhao Lei * logical block numbers for the start of each stripe 3108e5cfb55SZhao Lei * The last one or two are p/q. These are sorted, 3118e5cfb55SZhao Lei * so raid_map[0] is the start of our full stripe 3128e5cfb55SZhao Lei */ 3138e5cfb55SZhao Lei u64 *raid_map; 314cea9e445SChris Mason struct btrfs_bio_stripe stripes[]; 315cea9e445SChris Mason }; 316cea9e445SChris Mason 317b2117a39SMiao Xie struct btrfs_device_info { 318b2117a39SMiao Xie struct btrfs_device *dev; 319b2117a39SMiao Xie u64 dev_offset; 320b2117a39SMiao Xie u64 max_avail; 32173c5de00SArne Jansen u64 total_avail; 322b2117a39SMiao Xie }; 323b2117a39SMiao Xie 32431e50229SLiu Bo struct btrfs_raid_attr { 32531e50229SLiu Bo int sub_stripes; /* sub_stripes info for map */ 32631e50229SLiu Bo int dev_stripes; /* stripes per dev */ 32731e50229SLiu Bo int devs_max; /* max devs to use */ 32831e50229SLiu Bo int devs_min; /* min devs needed */ 3298789f4feSZhao Lei int tolerated_failures; /* max tolerated fail devs */ 33031e50229SLiu Bo int devs_increment; /* ndevs has to be a multiple of this */ 33131e50229SLiu Bo int ncopies; /* how many copies to data has */ 33231e50229SLiu Bo }; 33331e50229SLiu Bo 334af902047SZhao Lei extern const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES]; 335621292baSDavid Sterba extern const int btrfs_raid_mindev_error[BTRFS_NR_RAID_TYPES]; 336af902047SZhao Lei extern const u64 btrfs_raid_group[BTRFS_NR_RAID_TYPES]; 337af902047SZhao Lei 3381abe9b8aSliubo struct map_lookup { 3391abe9b8aSliubo u64 type; 3401abe9b8aSliubo int io_align; 3411abe9b8aSliubo int io_width; 3423d8da678SLiu Bo u64 stripe_len; 3431abe9b8aSliubo int num_stripes; 3441abe9b8aSliubo int sub_stripes; 3451abe9b8aSliubo struct btrfs_bio_stripe stripes[]; 3461abe9b8aSliubo }; 3471abe9b8aSliubo 348a2de733cSArne Jansen #define map_lookup_size(n) (sizeof(struct map_lookup) + \ 349a2de733cSArne Jansen (sizeof(struct btrfs_bio_stripe) * (n))) 350a2de733cSArne Jansen 351c9e9f97bSIlya Dryomov struct btrfs_balance_args; 35219a39dceSIlya Dryomov struct btrfs_balance_progress; 353c9e9f97bSIlya Dryomov struct btrfs_balance_control { 354c9e9f97bSIlya Dryomov struct btrfs_fs_info *fs_info; 355c9e9f97bSIlya Dryomov 356c9e9f97bSIlya Dryomov struct btrfs_balance_args data; 357c9e9f97bSIlya Dryomov struct btrfs_balance_args meta; 358c9e9f97bSIlya Dryomov struct btrfs_balance_args sys; 359c9e9f97bSIlya Dryomov 360c9e9f97bSIlya Dryomov u64 flags; 36119a39dceSIlya Dryomov 36219a39dceSIlya Dryomov struct btrfs_balance_progress stat; 363c9e9f97bSIlya Dryomov }; 364c9e9f97bSIlya Dryomov 365cf8cddd3SChristoph Hellwig enum btrfs_map_op { 366cf8cddd3SChristoph Hellwig BTRFS_MAP_READ, 367cf8cddd3SChristoph Hellwig BTRFS_MAP_WRITE, 368cf8cddd3SChristoph Hellwig BTRFS_MAP_DISCARD, 369cf8cddd3SChristoph Hellwig BTRFS_MAP_GET_READ_MIRRORS, 370cf8cddd3SChristoph Hellwig }; 371cf8cddd3SChristoph Hellwig 372cf8cddd3SChristoph Hellwig static inline enum btrfs_map_op btrfs_op(struct bio *bio) 373cf8cddd3SChristoph Hellwig { 374cf8cddd3SChristoph Hellwig switch (bio_op(bio)) { 375cf8cddd3SChristoph Hellwig case REQ_OP_DISCARD: 376cf8cddd3SChristoph Hellwig return BTRFS_MAP_DISCARD; 377cf8cddd3SChristoph Hellwig case REQ_OP_WRITE: 378cf8cddd3SChristoph Hellwig return BTRFS_MAP_WRITE; 379cf8cddd3SChristoph Hellwig default: 380cf8cddd3SChristoph Hellwig WARN_ON_ONCE(1); 381cf8cddd3SChristoph Hellwig case REQ_OP_READ: 382cf8cddd3SChristoph Hellwig return BTRFS_MAP_READ; 383cf8cddd3SChristoph Hellwig } 384cf8cddd3SChristoph Hellwig } 385cf8cddd3SChristoph Hellwig 3866d07bcecSMiao Xie int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start, 3876d07bcecSMiao Xie u64 end, u64 *length); 3886e9606d2SZhao Lei void btrfs_get_bbio(struct btrfs_bio *bbio); 3896e9606d2SZhao Lei void btrfs_put_bbio(struct btrfs_bio *bbio); 390cf8cddd3SChristoph Hellwig int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op, 391cea9e445SChris Mason u64 logical, u64 *length, 392a1d3c478SJan Schmidt struct btrfs_bio **bbio_ret, int mirror_num); 393cf8cddd3SChristoph Hellwig int btrfs_map_sblock(struct btrfs_fs_info *fs_info, enum btrfs_map_op op, 394af8e2d1dSMiao Xie u64 logical, u64 *length, 395825ad4c9SDavid Sterba struct btrfs_bio **bbio_ret); 396ab8d0fc4SJeff Mahoney int btrfs_rmap_block(struct btrfs_fs_info *fs_info, 397a512bbf8SYan Zheng u64 chunk_start, u64 physical, u64 devid, 398a512bbf8SYan Zheng u64 **logical, int *naddrs, int *stripe_len); 3996bccf3abSJeff Mahoney int btrfs_read_sys_array(struct btrfs_fs_info *fs_info); 4005b4aacefSJeff Mahoney int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info); 4010b86a832SChris Mason int btrfs_alloc_chunk(struct btrfs_trans_handle *trans, 4022ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info, u64 type); 4030b86a832SChris Mason void btrfs_mapping_init(struct btrfs_mapping_tree *tree); 4040b86a832SChris Mason void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree); 40558efbc9fSOmar Sandoval blk_status_t btrfs_map_bio(struct btrfs_fs_info *fs_info, struct bio *bio, 4068b712842SChris Mason int mirror_num, int async_submit); 4078a4b83ccSChris Mason int btrfs_open_devices(struct btrfs_fs_devices *fs_devices, 40897288f2cSChristoph Hellwig fmode_t flags, void *holder); 40997288f2cSChristoph Hellwig int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder, 4108a4b83ccSChris Mason struct btrfs_fs_devices **fs_devices_ret); 4118a4b83ccSChris Mason int btrfs_close_devices(struct btrfs_fs_devices *fs_devices); 4129b99b115SAnand Jain void btrfs_free_extra_devids(struct btrfs_fs_devices *fs_devices, int step); 41388acff64SAnand Jain void btrfs_assign_next_active_device(struct btrfs_fs_info *fs_info, 41488acff64SAnand Jain struct btrfs_device *device, struct btrfs_device *this_dev); 4152ff7e61eSJeff Mahoney int btrfs_find_device_missing_or_by_path(struct btrfs_fs_info *fs_info, 416da353f6bSDavid Sterba const char *device_path, 4177ba15b7dSStefan Behrens struct btrfs_device **device); 4182ff7e61eSJeff Mahoney int btrfs_find_device_by_devspec(struct btrfs_fs_info *fs_info, u64 devid, 419da353f6bSDavid Sterba const char *devpath, 42024e0474bSAnand Jain struct btrfs_device **device); 42112bd2fc0SIlya Dryomov struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info, 42212bd2fc0SIlya Dryomov const u64 *devid, 42312bd2fc0SIlya Dryomov const u8 *uuid); 4242ff7e61eSJeff Mahoney int btrfs_rm_device(struct btrfs_fs_info *fs_info, 425da353f6bSDavid Sterba const char *device_path, u64 devid); 426ffc5a379SDavid Sterba void __exit btrfs_cleanup_fs_uuids(void); 4275d964051SStefan Behrens int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len); 4288f18cf13SChris Mason int btrfs_grow_device(struct btrfs_trans_handle *trans, 4298f18cf13SChris Mason struct btrfs_device *device, u64 new_size); 430aa1b8cd4SStefan Behrens struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid, 4312b82032cSYan Zheng u8 *uuid, u8 *fsid); 4328f18cf13SChris Mason int btrfs_shrink_device(struct btrfs_device *device, u64 new_size); 433da353f6bSDavid Sterba int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *path); 4342ff7e61eSJeff Mahoney int btrfs_init_dev_replace_tgtdev(struct btrfs_fs_info *fs_info, 435da353f6bSDavid Sterba const char *device_path, 4361c43366dSMiao Xie struct btrfs_device *srcdev, 437e93c89c1SStefan Behrens struct btrfs_device **device_out); 438c9e9f97bSIlya Dryomov int btrfs_balance(struct btrfs_balance_control *bctl, 439c9e9f97bSIlya Dryomov struct btrfs_ioctl_balance_args *bargs); 4402b6ba629SIlya Dryomov int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info); 44168310a5eSIlya Dryomov int btrfs_recover_balance(struct btrfs_fs_info *fs_info); 442837d5b6eSIlya Dryomov int btrfs_pause_balance(struct btrfs_fs_info *fs_info); 443a7e99c69SIlya Dryomov int btrfs_cancel_balance(struct btrfs_fs_info *fs_info); 444f7a81ea4SStefan Behrens int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info); 44570f80175SStefan Behrens int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info); 4462ff7e61eSJeff Mahoney int btrfs_chunk_readonly(struct btrfs_fs_info *fs_info, u64 chunk_offset); 447499f377fSJeff Mahoney int find_free_dev_extent_start(struct btrfs_transaction *transaction, 448499f377fSJeff Mahoney struct btrfs_device *device, u64 num_bytes, 449499f377fSJeff Mahoney u64 search_start, u64 *start, u64 *max_avail); 4506df9a95eSJosef Bacik int find_free_dev_extent(struct btrfs_trans_handle *trans, 4516df9a95eSJosef Bacik struct btrfs_device *device, u64 num_bytes, 452ba1bf481SJosef Bacik u64 *start, u64 *max_avail); 453442a4f63SStefan Behrens void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index); 4542ff7e61eSJeff Mahoney int btrfs_get_dev_stats(struct btrfs_fs_info *fs_info, 455b27f7c0cSDavid Sterba struct btrfs_ioctl_get_dev_stats *stats); 456cb517eabSMiao Xie void btrfs_init_devices_late(struct btrfs_fs_info *fs_info); 457733f4fbbSStefan Behrens int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info); 458733f4fbbSStefan Behrens int btrfs_run_dev_stats(struct btrfs_trans_handle *trans, 459733f4fbbSStefan Behrens struct btrfs_fs_info *fs_info); 460084b6e7cSQu Wenruo void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_fs_info *fs_info, 461084b6e7cSQu Wenruo struct btrfs_device *srcdev); 462084b6e7cSQu Wenruo void btrfs_rm_dev_replace_free_srcdev(struct btrfs_fs_info *fs_info, 463e93c89c1SStefan Behrens struct btrfs_device *srcdev); 464e93c89c1SStefan Behrens void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info, 465e93c89c1SStefan Behrens struct btrfs_device *tgtdev); 466da353f6bSDavid Sterba void btrfs_scratch_superblocks(struct block_device *bdev, const char *device_path); 467592d92eeSLiu Bo int btrfs_is_parity_mirror(struct btrfs_fs_info *fs_info, 468e4ff5fb5SNikolay Borisov u64 logical, u64 len); 4692ff7e61eSJeff Mahoney unsigned long btrfs_full_stripe_len(struct btrfs_fs_info *fs_info, 47053b381b3SDavid Woodhouse u64 logical); 4716df9a95eSJosef Bacik int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans, 4726bccf3abSJeff Mahoney struct btrfs_fs_info *fs_info, 4736df9a95eSJosef Bacik u64 chunk_offset, u64 chunk_size); 47447ab2a6cSJosef Bacik int btrfs_remove_chunk(struct btrfs_trans_handle *trans, 4755b4aacefSJeff Mahoney struct btrfs_fs_info *fs_info, u64 chunk_offset); 476addc3fa7SMiao Xie 477442a4f63SStefan Behrens static inline void btrfs_dev_stat_inc(struct btrfs_device *dev, 478442a4f63SStefan Behrens int index) 479442a4f63SStefan Behrens { 480442a4f63SStefan Behrens atomic_inc(dev->dev_stat_values + index); 4819deae968SNikolay Borisov /* 4829deae968SNikolay Borisov * This memory barrier orders stores updating statistics before stores 4839deae968SNikolay Borisov * updating dev_stats_ccnt. 4849deae968SNikolay Borisov * 4859deae968SNikolay Borisov * It pairs with smp_rmb() in btrfs_run_dev_stats(). 4869deae968SNikolay Borisov */ 487addc3fa7SMiao Xie smp_mb__before_atomic(); 488addc3fa7SMiao Xie atomic_inc(&dev->dev_stats_ccnt); 489442a4f63SStefan Behrens } 490442a4f63SStefan Behrens 491442a4f63SStefan Behrens static inline int btrfs_dev_stat_read(struct btrfs_device *dev, 492442a4f63SStefan Behrens int index) 493442a4f63SStefan Behrens { 494442a4f63SStefan Behrens return atomic_read(dev->dev_stat_values + index); 495442a4f63SStefan Behrens } 496442a4f63SStefan Behrens 497442a4f63SStefan Behrens static inline int btrfs_dev_stat_read_and_reset(struct btrfs_device *dev, 498442a4f63SStefan Behrens int index) 499442a4f63SStefan Behrens { 500442a4f63SStefan Behrens int ret; 501442a4f63SStefan Behrens 502442a4f63SStefan Behrens ret = atomic_xchg(dev->dev_stat_values + index, 0); 5034660c49fSNikolay Borisov /* 5044660c49fSNikolay Borisov * atomic_xchg implies a full memory barriers as per atomic_t.txt: 5054660c49fSNikolay Borisov * - RMW operations that have a return value are fully ordered; 5064660c49fSNikolay Borisov * 5074660c49fSNikolay Borisov * This implicit memory barriers is paired with the smp_rmb in 5084660c49fSNikolay Borisov * btrfs_run_dev_stats 5094660c49fSNikolay Borisov */ 510addc3fa7SMiao Xie atomic_inc(&dev->dev_stats_ccnt); 511442a4f63SStefan Behrens return ret; 512442a4f63SStefan Behrens } 513442a4f63SStefan Behrens 514442a4f63SStefan Behrens static inline void btrfs_dev_stat_set(struct btrfs_device *dev, 515442a4f63SStefan Behrens int index, unsigned long val) 516442a4f63SStefan Behrens { 517442a4f63SStefan Behrens atomic_set(dev->dev_stat_values + index, val); 5189deae968SNikolay Borisov /* 5199deae968SNikolay Borisov * This memory barrier orders stores updating statistics before stores 5209deae968SNikolay Borisov * updating dev_stats_ccnt. 5219deae968SNikolay Borisov * 5229deae968SNikolay Borisov * It pairs with smp_rmb() in btrfs_run_dev_stats(). 5239deae968SNikolay Borisov */ 524addc3fa7SMiao Xie smp_mb__before_atomic(); 525addc3fa7SMiao Xie atomic_inc(&dev->dev_stats_ccnt); 526442a4f63SStefan Behrens } 527442a4f63SStefan Behrens 528442a4f63SStefan Behrens static inline void btrfs_dev_stat_reset(struct btrfs_device *dev, 529442a4f63SStefan Behrens int index) 530442a4f63SStefan Behrens { 531442a4f63SStefan Behrens btrfs_dev_stat_set(dev, index, 0); 532442a4f63SStefan Behrens } 533935e5cc9SMiao Xie 5343e72ee88SQu Wenruo /* 5353e72ee88SQu Wenruo * Convert block group flags (BTRFS_BLOCK_GROUP_*) to btrfs_raid_types, which 5363e72ee88SQu Wenruo * can be used as index to access btrfs_raid_array[]. 5373e72ee88SQu Wenruo */ 5383e72ee88SQu Wenruo static inline enum btrfs_raid_types btrfs_bg_flags_to_raid_index(u64 flags) 5393e72ee88SQu Wenruo { 5403e72ee88SQu Wenruo if (flags & BTRFS_BLOCK_GROUP_RAID10) 5413e72ee88SQu Wenruo return BTRFS_RAID_RAID10; 5423e72ee88SQu Wenruo else if (flags & BTRFS_BLOCK_GROUP_RAID1) 5433e72ee88SQu Wenruo return BTRFS_RAID_RAID1; 5443e72ee88SQu Wenruo else if (flags & BTRFS_BLOCK_GROUP_DUP) 5453e72ee88SQu Wenruo return BTRFS_RAID_DUP; 5463e72ee88SQu Wenruo else if (flags & BTRFS_BLOCK_GROUP_RAID0) 5473e72ee88SQu Wenruo return BTRFS_RAID_RAID0; 5483e72ee88SQu Wenruo else if (flags & BTRFS_BLOCK_GROUP_RAID5) 5493e72ee88SQu Wenruo return BTRFS_RAID_RAID5; 5503e72ee88SQu Wenruo else if (flags & BTRFS_BLOCK_GROUP_RAID6) 5513e72ee88SQu Wenruo return BTRFS_RAID_RAID6; 5523e72ee88SQu Wenruo 5533e72ee88SQu Wenruo return BTRFS_RAID_SINGLE; /* BTRFS_BLOCK_GROUP_SINGLE */ 5543e72ee88SQu Wenruo } 5553e72ee88SQu Wenruo 556935e5cc9SMiao Xie void btrfs_update_commit_device_size(struct btrfs_fs_info *fs_info); 557e9b919b1SNikolay Borisov void btrfs_update_commit_device_bytes_used(struct btrfs_transaction *trans); 55804216820SFilipe Manana 559c73eccf7SAnand Jain struct list_head *btrfs_get_fs_uuids(void); 5605a13f430SAnand Jain void btrfs_set_fs_info_ptr(struct btrfs_fs_info *fs_info); 5615a13f430SAnand Jain void btrfs_reset_fs_info_ptr(struct btrfs_fs_info *fs_info); 5626528b99dSAnand Jain bool btrfs_check_rw_degradable(struct btrfs_fs_info *fs_info, 5636528b99dSAnand Jain struct btrfs_device *failing_dev); 56421634a19SQu Wenruo 5650b86a832SChris Mason #endif 566