xref: /openbmc/linux/fs/btrfs/volumes.h (revision 5f141126)
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 
14fce466eaSQu Wenruo #define BTRFS_MAX_DATA_CHUNK_SIZE	(10ULL * SZ_1G)
15fce466eaSQu Wenruo 
1667a2c45eSMiao Xie extern struct mutex uuid_mutex;
1767a2c45eSMiao Xie 
18ee22184bSByongho Lee #define BTRFS_STRIPE_LEN	SZ_64K
19b2117a39SMiao Xie 
20f2984462SChris Mason struct buffer_head;
21ffbd517dSChris Mason struct btrfs_pending_bios {
22ffbd517dSChris Mason 	struct bio *head;
23ffbd517dSChris Mason 	struct bio *tail;
24ffbd517dSChris Mason };
25ffbd517dSChris Mason 
265f141126SNikolay Borisov struct btrfs_io_geometry {
275f141126SNikolay Borisov 	/* remaining bytes before crossing a stripe */
285f141126SNikolay Borisov 	u64 len;
295f141126SNikolay Borisov 	/* offset of logical address in chunk */
305f141126SNikolay Borisov 	u64 offset;
315f141126SNikolay Borisov 	/* length of single IO stripe */
325f141126SNikolay Borisov 	u64 stripe_len;
335f141126SNikolay Borisov 	/* number of stripe where address falls */
345f141126SNikolay Borisov 	u64 stripe_nr;
355f141126SNikolay Borisov 	/* offset of address in stripe */
365f141126SNikolay Borisov 	u64 stripe_offset;
375f141126SNikolay Borisov 	/* offset of raid56 stripe into the chunk */
385f141126SNikolay Borisov 	u64 raid56_stripe_offset;
395f141126SNikolay Borisov };
405f141126SNikolay Borisov 
417cc8e58dSMiao Xie /*
427cc8e58dSMiao Xie  * Use sequence counter to get consistent device stat data on
437cc8e58dSMiao Xie  * 32-bit processors.
447cc8e58dSMiao Xie  */
457cc8e58dSMiao Xie #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
467cc8e58dSMiao Xie #include <linux/seqlock.h>
477cc8e58dSMiao Xie #define __BTRFS_NEED_DEVICE_DATA_ORDERED
487cc8e58dSMiao Xie #define btrfs_device_data_ordered_init(device)	\
497cc8e58dSMiao Xie 	seqcount_init(&device->data_seqcount)
507cc8e58dSMiao Xie #else
517cc8e58dSMiao Xie #define btrfs_device_data_ordered_init(device) do { } while (0)
527cc8e58dSMiao Xie #endif
537cc8e58dSMiao Xie 
54ebbede42SAnand Jain #define BTRFS_DEV_STATE_WRITEABLE	(0)
55e12c9621SAnand Jain #define BTRFS_DEV_STATE_IN_FS_METADATA	(1)
56e6e674bdSAnand Jain #define BTRFS_DEV_STATE_MISSING		(2)
57401e29c1SAnand Jain #define BTRFS_DEV_STATE_REPLACE_TGT	(3)
581c3063b6SAnand Jain #define BTRFS_DEV_STATE_FLUSH_SENT	(4)
59ebbede42SAnand Jain 
600b86a832SChris Mason struct btrfs_device {
610b6f5d40SNikolay Borisov 	struct list_head dev_list; /* device_list_mutex */
620b6f5d40SNikolay Borisov 	struct list_head dev_alloc_list; /* chunk mutex */
63bbbf7243SNikolay Borisov 	struct list_head post_commit_list; /* chunk mutex */
642b82032cSYan Zheng 	struct btrfs_fs_devices *fs_devices;
65fb456252SJeff Mahoney 	struct btrfs_fs_info *fs_info;
66ffbd517dSChris Mason 
67d5ee37bcSMiao Xie 	struct rcu_string *name;
68d5ee37bcSMiao Xie 
69d5ee37bcSMiao Xie 	u64 generation;
70d5ee37bcSMiao Xie 
71d5ee37bcSMiao Xie 	spinlock_t io_lock ____cacheline_aligned;
72d5ee37bcSMiao Xie 	int running_pending;
73ffbd517dSChris Mason 	/* regular prio bios */
74ffbd517dSChris Mason 	struct btrfs_pending_bios pending_bios;
7570fd7614SChristoph Hellwig 	/* sync bios */
76ffbd517dSChris Mason 	struct btrfs_pending_bios pending_sync_bios;
77ffbd517dSChris Mason 
78d5ee37bcSMiao Xie 	struct block_device *bdev;
79d5ee37bcSMiao Xie 
80d5ee37bcSMiao Xie 	/* the mode sent to blkdev_get */
81d5ee37bcSMiao Xie 	fmode_t mode;
82d5ee37bcSMiao Xie 
83ebbede42SAnand Jain 	unsigned long dev_state;
8458efbc9fSOmar Sandoval 	blk_status_t last_flush_error;
85e0ae9994SDavid Sterba 	int flush_bio_sent;
86b3075717SChris Mason 
877cc8e58dSMiao Xie #ifdef __BTRFS_NEED_DEVICE_DATA_ORDERED
887cc8e58dSMiao Xie 	seqcount_t data_seqcount;
897cc8e58dSMiao Xie #endif
907cc8e58dSMiao Xie 
910b86a832SChris Mason 	/* the internal btrfs device id */
920b86a832SChris Mason 	u64 devid;
930b86a832SChris Mason 
946ba40b61SMiao Xie 	/* size of the device in memory */
950b86a832SChris Mason 	u64 total_bytes;
960b86a832SChris Mason 
976ba40b61SMiao Xie 	/* size of the device on disk */
98d6397baeSChris Ball 	u64 disk_total_bytes;
99d6397baeSChris Ball 
1000b86a832SChris Mason 	/* bytes used */
1010b86a832SChris Mason 	u64 bytes_used;
1020b86a832SChris Mason 
1030b86a832SChris Mason 	/* optimal io alignment for this device */
1040b86a832SChris Mason 	u32 io_align;
1050b86a832SChris Mason 
1060b86a832SChris Mason 	/* optimal io width for this device */
1070b86a832SChris Mason 	u32 io_width;
1083c45bfc1SDulshani Gunawardhana 	/* type and info about this device */
1093c45bfc1SDulshani Gunawardhana 	u64 type;
1100b86a832SChris Mason 
1110b86a832SChris Mason 	/* minimal io size for this device */
1120b86a832SChris Mason 	u32 sector_size;
1130b86a832SChris Mason 
1140b86a832SChris Mason 	/* physical drive uuid (or lvm uuid) */
115e17cade2SChris Mason 	u8 uuid[BTRFS_UUID_SIZE];
1168b712842SChris Mason 
117935e5cc9SMiao Xie 	/*
118935e5cc9SMiao Xie 	 * size of the device on the current transaction
119935e5cc9SMiao Xie 	 *
120935e5cc9SMiao Xie 	 * This variant is update when committing the transaction,
121bbbf7243SNikolay Borisov 	 * and protected by chunk mutex
122935e5cc9SMiao Xie 	 */
123935e5cc9SMiao Xie 	u64 commit_total_bytes;
124935e5cc9SMiao Xie 
125ce7213c7SMiao Xie 	/* bytes used on the current transaction */
126ce7213c7SMiao Xie 	u64 commit_bytes_used;
127935e5cc9SMiao Xie 
1283c45bfc1SDulshani Gunawardhana 	/* for sending down flush barriers */
1293c45bfc1SDulshani Gunawardhana 	struct bio *flush_bio;
1303c45bfc1SDulshani Gunawardhana 	struct completion flush_wait;
1313c45bfc1SDulshani Gunawardhana 
132a2de733cSArne Jansen 	/* per-device scrub information */
133cadbc0a0SAnand Jain 	struct scrub_ctx *scrub_ctx;
134a2de733cSArne Jansen 
135d458b054SQu Wenruo 	struct btrfs_work work;
13690519d66SArne Jansen 
13790519d66SArne Jansen 	/* readahead state */
13890519d66SArne Jansen 	atomic_t reada_in_flight;
13990519d66SArne Jansen 	u64 reada_next;
14090519d66SArne Jansen 	struct reada_zone *reada_curr_zone;
14190519d66SArne Jansen 	struct radix_tree_root reada_zones;
14290519d66SArne Jansen 	struct radix_tree_root reada_extents;
143387125fcSChris Mason 
144442a4f63SStefan Behrens 	/* disk I/O failure stats. For detailed description refer to
145442a4f63SStefan Behrens 	 * enum btrfs_dev_stat_values in ioctl.h */
146733f4fbbSStefan Behrens 	int dev_stats_valid;
147addc3fa7SMiao Xie 
148addc3fa7SMiao Xie 	/* Counter to record the change of device stats */
149addc3fa7SMiao Xie 	atomic_t dev_stats_ccnt;
150442a4f63SStefan Behrens 	atomic_t dev_stat_values[BTRFS_DEV_STAT_VALUES_MAX];
1511c11b63eSJeff Mahoney 
1521c11b63eSJeff Mahoney 	struct extent_io_tree alloc_state;
1530b86a832SChris Mason };
1540b86a832SChris Mason 
1557cc8e58dSMiao Xie /*
1567cc8e58dSMiao Xie  * If we read those variants at the context of their own lock, we needn't
1577cc8e58dSMiao Xie  * use the following helpers, reading them directly is safe.
1587cc8e58dSMiao Xie  */
1597cc8e58dSMiao Xie #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
1607cc8e58dSMiao Xie #define BTRFS_DEVICE_GETSET_FUNCS(name)					\
1617cc8e58dSMiao Xie static inline u64							\
1627cc8e58dSMiao Xie btrfs_device_get_##name(const struct btrfs_device *dev)			\
1637cc8e58dSMiao Xie {									\
1647cc8e58dSMiao Xie 	u64 size;							\
1657cc8e58dSMiao Xie 	unsigned int seq;						\
1667cc8e58dSMiao Xie 									\
1677cc8e58dSMiao Xie 	do {								\
1687cc8e58dSMiao Xie 		seq = read_seqcount_begin(&dev->data_seqcount);		\
1697cc8e58dSMiao Xie 		size = dev->name;					\
1707cc8e58dSMiao Xie 	} while (read_seqcount_retry(&dev->data_seqcount, seq));	\
1717cc8e58dSMiao Xie 	return size;							\
1727cc8e58dSMiao Xie }									\
1737cc8e58dSMiao Xie 									\
1747cc8e58dSMiao Xie static inline void							\
1757cc8e58dSMiao Xie btrfs_device_set_##name(struct btrfs_device *dev, u64 size)		\
1767cc8e58dSMiao Xie {									\
1777cc8e58dSMiao Xie 	preempt_disable();						\
1787cc8e58dSMiao Xie 	write_seqcount_begin(&dev->data_seqcount);			\
1797cc8e58dSMiao Xie 	dev->name = size;						\
1807cc8e58dSMiao Xie 	write_seqcount_end(&dev->data_seqcount);			\
1817cc8e58dSMiao Xie 	preempt_enable();						\
1827cc8e58dSMiao Xie }
1837cc8e58dSMiao Xie #elif BITS_PER_LONG==32 && defined(CONFIG_PREEMPT)
1847cc8e58dSMiao Xie #define BTRFS_DEVICE_GETSET_FUNCS(name)					\
1857cc8e58dSMiao Xie static inline u64							\
1867cc8e58dSMiao Xie btrfs_device_get_##name(const struct btrfs_device *dev)			\
1877cc8e58dSMiao Xie {									\
1887cc8e58dSMiao Xie 	u64 size;							\
1897cc8e58dSMiao Xie 									\
1907cc8e58dSMiao Xie 	preempt_disable();						\
1917cc8e58dSMiao Xie 	size = dev->name;						\
1927cc8e58dSMiao Xie 	preempt_enable();						\
1937cc8e58dSMiao Xie 	return size;							\
1947cc8e58dSMiao Xie }									\
1957cc8e58dSMiao Xie 									\
1967cc8e58dSMiao Xie static inline void							\
1977cc8e58dSMiao Xie btrfs_device_set_##name(struct btrfs_device *dev, u64 size)		\
1987cc8e58dSMiao Xie {									\
1997cc8e58dSMiao Xie 	preempt_disable();						\
2007cc8e58dSMiao Xie 	dev->name = size;						\
2017cc8e58dSMiao Xie 	preempt_enable();						\
2027cc8e58dSMiao Xie }
2037cc8e58dSMiao Xie #else
2047cc8e58dSMiao Xie #define BTRFS_DEVICE_GETSET_FUNCS(name)					\
2057cc8e58dSMiao Xie static inline u64							\
2067cc8e58dSMiao Xie btrfs_device_get_##name(const struct btrfs_device *dev)			\
2077cc8e58dSMiao Xie {									\
2087cc8e58dSMiao Xie 	return dev->name;						\
2097cc8e58dSMiao Xie }									\
2107cc8e58dSMiao Xie 									\
2117cc8e58dSMiao Xie static inline void							\
2127cc8e58dSMiao Xie btrfs_device_set_##name(struct btrfs_device *dev, u64 size)		\
2137cc8e58dSMiao Xie {									\
2147cc8e58dSMiao Xie 	dev->name = size;						\
2157cc8e58dSMiao Xie }
2167cc8e58dSMiao Xie #endif
2177cc8e58dSMiao Xie 
2187cc8e58dSMiao Xie BTRFS_DEVICE_GETSET_FUNCS(total_bytes);
2197cc8e58dSMiao Xie BTRFS_DEVICE_GETSET_FUNCS(disk_total_bytes);
2207cc8e58dSMiao Xie BTRFS_DEVICE_GETSET_FUNCS(bytes_used);
2217cc8e58dSMiao Xie 
2228a4b83ccSChris Mason struct btrfs_fs_devices {
2238a4b83ccSChris Mason 	u8 fsid[BTRFS_FSID_SIZE]; /* FS specific uuid */
2247239ff4bSNikolay Borisov 	u8 metadata_uuid[BTRFS_FSID_SIZE];
225d1a63002SNikolay Borisov 	bool fsid_change;
226c4babc5eSAnand Jain 	struct list_head fs_list;
2278a4b83ccSChris Mason 
2288a4b83ccSChris Mason 	u64 num_devices;
229a0af469bSChris Mason 	u64 open_devices;
2302b82032cSYan Zheng 	u64 rw_devices;
231cd02dca5SChris Mason 	u64 missing_devices;
2322b82032cSYan Zheng 	u64 total_rw_bytes;
23302db0844SJosef Bacik 	u64 total_devices;
234d1a63002SNikolay Borisov 
235d1a63002SNikolay Borisov 	/* Highest generation number of seen devices */
236d1a63002SNikolay Borisov 	u64 latest_generation;
237d1a63002SNikolay Borisov 
2388a4b83ccSChris Mason 	struct block_device *latest_bdev;
239e5e9a520SChris Mason 
240e5e9a520SChris Mason 	/* all of the devices in the FS, protected by a mutex
241e5e9a520SChris Mason 	 * so we can safely walk it to write out the supers without
2429b011adfSWang Shilong 	 * worrying about add/remove by the multi-device code.
2439b011adfSWang Shilong 	 * Scrubbing super can kick off supers writing by holding
2449b011adfSWang Shilong 	 * this mutex lock.
245e5e9a520SChris Mason 	 */
246e5e9a520SChris Mason 	struct mutex device_list_mutex;
2470b6f5d40SNikolay Borisov 
2480b6f5d40SNikolay Borisov 	/* List of all devices, protected by device_list_mutex */
2498a4b83ccSChris Mason 	struct list_head devices;
250b3075717SChris Mason 
2510b6f5d40SNikolay Borisov 	/*
2520b6f5d40SNikolay Borisov 	 * Devices which can satisfy space allocation. Protected by
2530b6f5d40SNikolay Borisov 	 * chunk_mutex
2540b6f5d40SNikolay Borisov 	 */
255b3075717SChris Mason 	struct list_head alloc_list;
2562b82032cSYan Zheng 
2572b82032cSYan Zheng 	struct btrfs_fs_devices *seed;
2582b82032cSYan Zheng 	int seeding;
2592b82032cSYan Zheng 
2602b82032cSYan Zheng 	int opened;
261c289811cSChris Mason 
262c289811cSChris Mason 	/* set when we find or add a device that doesn't have the
263c289811cSChris Mason 	 * nonrot flag set
264c289811cSChris Mason 	 */
265c289811cSChris Mason 	int rotating;
2662e7910d6SAnand Jain 
2675a13f430SAnand Jain 	struct btrfs_fs_info *fs_info;
2682e7910d6SAnand Jain 	/* sysfs kobjects */
269c1b7e474SAnand Jain 	struct kobject fsid_kobj;
2702e7910d6SAnand Jain 	struct kobject *device_dir_kobj;
2712e7910d6SAnand Jain 	struct completion kobj_unregister;
2728a4b83ccSChris Mason };
2738a4b83ccSChris Mason 
274facc8a22SMiao Xie #define BTRFS_BIO_INLINE_CSUM_SIZE	64
275facc8a22SMiao Xie 
276ab4ba2e1SQu Wenruo #define BTRFS_MAX_DEVS(info) ((BTRFS_MAX_ITEM_SIZE(info)	\
277ab4ba2e1SQu Wenruo 			- sizeof(struct btrfs_chunk))		\
278ab4ba2e1SQu Wenruo 			/ sizeof(struct btrfs_stripe) + 1)
279ab4ba2e1SQu Wenruo 
280ab4ba2e1SQu Wenruo #define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE	\
281ab4ba2e1SQu Wenruo 				- 2 * sizeof(struct btrfs_disk_key)	\
282ab4ba2e1SQu Wenruo 				- 2 * sizeof(struct btrfs_chunk))	\
283ab4ba2e1SQu Wenruo 				/ sizeof(struct btrfs_stripe) + 1)
284ab4ba2e1SQu Wenruo 
2859be3395bSChris Mason /*
2869be3395bSChris Mason  * we need the mirror number and stripe index to be passed around
2879be3395bSChris Mason  * the call chain while we are processing end_io (especially errors).
2889be3395bSChris Mason  * Really, what we need is a btrfs_bio structure that has this info
2899be3395bSChris Mason  * and is properly sized with its stripe array, but we're not there
2909be3395bSChris Mason  * quite yet.  We have our own btrfs bioset, and all of the bios
2919be3395bSChris Mason  * we allocate are actually btrfs_io_bios.  We'll cram as much of
2929be3395bSChris Mason  * struct btrfs_bio as we can into this over time.
2939be3395bSChris Mason  */
2949be3395bSChris Mason struct btrfs_io_bio {
295c1dc0896SMiao Xie 	unsigned int mirror_num;
296c1dc0896SMiao Xie 	unsigned int stripe_index;
297c1dc0896SMiao Xie 	u64 logical;
298facc8a22SMiao Xie 	u8 *csum;
299facc8a22SMiao Xie 	u8 csum_inline[BTRFS_BIO_INLINE_CSUM_SIZE];
30017347cecSLiu Bo 	struct bvec_iter iter;
301fa1bcbe0SDavid Sterba 	/*
302fa1bcbe0SDavid Sterba 	 * This member must come last, bio_alloc_bioset will allocate enough
303fa1bcbe0SDavid Sterba 	 * bytes for entire btrfs_io_bio but relies on bio being last.
304fa1bcbe0SDavid Sterba 	 */
3059be3395bSChris Mason 	struct bio bio;
3069be3395bSChris Mason };
3079be3395bSChris Mason 
3089be3395bSChris Mason static inline struct btrfs_io_bio *btrfs_io_bio(struct bio *bio)
3099be3395bSChris Mason {
3109be3395bSChris Mason 	return container_of(bio, struct btrfs_io_bio, bio);
3119be3395bSChris Mason }
3129be3395bSChris Mason 
313b3a0dd50SDavid Sterba static inline void btrfs_io_bio_free_csum(struct btrfs_io_bio *io_bio)
314b3a0dd50SDavid Sterba {
315b3a0dd50SDavid Sterba 	if (io_bio->csum != io_bio->csum_inline) {
316b3a0dd50SDavid Sterba 		kfree(io_bio->csum);
317b3a0dd50SDavid Sterba 		io_bio->csum = NULL;
318b3a0dd50SDavid Sterba 	}
319b3a0dd50SDavid Sterba }
320b3a0dd50SDavid Sterba 
321cea9e445SChris Mason struct btrfs_bio_stripe {
322cea9e445SChris Mason 	struct btrfs_device *dev;
323cea9e445SChris Mason 	u64 physical;
324fce3bb9aSLi Dongyang 	u64 length; /* only used for discard mappings */
325cea9e445SChris Mason };
326cea9e445SChris Mason 
327a1d3c478SJan Schmidt struct btrfs_bio {
328140475aeSElena Reshetova 	refcount_t refs;
329cea9e445SChris Mason 	atomic_t stripes_pending;
330c404e0dcSMiao Xie 	struct btrfs_fs_info *fs_info;
33110f11900SZhao Lei 	u64 map_type; /* get from map_lookup->type */
332cea9e445SChris Mason 	bio_end_io_t *end_io;
3337d2b4daaSChris Mason 	struct bio *orig_bio;
334c55f1396SMiao Xie 	unsigned long flags;
335cea9e445SChris Mason 	void *private;
336a236aed1SChris Mason 	atomic_t error;
337a236aed1SChris Mason 	int max_errors;
338cea9e445SChris Mason 	int num_stripes;
339a1d3c478SJan Schmidt 	int mirror_num;
3402c8cdd6eSMiao Xie 	int num_tgtdevs;
3412c8cdd6eSMiao Xie 	int *tgtdev_map;
3428e5cfb55SZhao Lei 	/*
3438e5cfb55SZhao Lei 	 * logical block numbers for the start of each stripe
3448e5cfb55SZhao Lei 	 * The last one or two are p/q.  These are sorted,
3458e5cfb55SZhao Lei 	 * so raid_map[0] is the start of our full stripe
3468e5cfb55SZhao Lei 	 */
3478e5cfb55SZhao Lei 	u64 *raid_map;
348cea9e445SChris Mason 	struct btrfs_bio_stripe stripes[];
349cea9e445SChris Mason };
350cea9e445SChris Mason 
351b2117a39SMiao Xie struct btrfs_device_info {
352b2117a39SMiao Xie 	struct btrfs_device *dev;
353b2117a39SMiao Xie 	u64 dev_offset;
354b2117a39SMiao Xie 	u64 max_avail;
35573c5de00SArne Jansen 	u64 total_avail;
356b2117a39SMiao Xie };
357b2117a39SMiao Xie 
35831e50229SLiu Bo struct btrfs_raid_attr {
3598c3e3582SDavid Sterba 	u8 sub_stripes;		/* sub_stripes info for map */
3608c3e3582SDavid Sterba 	u8 dev_stripes;		/* stripes per dev */
3618c3e3582SDavid Sterba 	u8 devs_max;		/* max devs to use */
3628c3e3582SDavid Sterba 	u8 devs_min;		/* min devs needed */
3638c3e3582SDavid Sterba 	u8 tolerated_failures;	/* max tolerated fail devs */
3648c3e3582SDavid Sterba 	u8 devs_increment;	/* ndevs has to be a multiple of this */
3658c3e3582SDavid Sterba 	u8 ncopies;		/* how many copies to data has */
3668c3e3582SDavid Sterba 	u8 nparity;		/* number of stripes worth of bytes to store
367b50836edSHans van Kranenburg 				 * parity information */
3688c3e3582SDavid Sterba 	u8 mindev_error;	/* error code if min devs requisite is unmet */
369ed23467bSAnand Jain 	const char raid_name[8]; /* name of the raid */
37041a6e891SAnand Jain 	u64 bg_flag;		/* block group flag of the raid */
37131e50229SLiu Bo };
37231e50229SLiu Bo 
373af902047SZhao Lei extern const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES];
374af902047SZhao Lei 
3751abe9b8aSliubo struct map_lookup {
3761abe9b8aSliubo 	u64 type;
3771abe9b8aSliubo 	int io_align;
3781abe9b8aSliubo 	int io_width;
3793d8da678SLiu Bo 	u64 stripe_len;
3801abe9b8aSliubo 	int num_stripes;
3811abe9b8aSliubo 	int sub_stripes;
382cf90d884SQu Wenruo 	int verified_stripes; /* For mount time dev extent verification */
3831abe9b8aSliubo 	struct btrfs_bio_stripe stripes[];
3841abe9b8aSliubo };
3851abe9b8aSliubo 
386a2de733cSArne Jansen #define map_lookup_size(n) (sizeof(struct map_lookup) + \
387a2de733cSArne Jansen 			    (sizeof(struct btrfs_bio_stripe) * (n)))
388a2de733cSArne Jansen 
389c9e9f97bSIlya Dryomov struct btrfs_balance_args;
39019a39dceSIlya Dryomov struct btrfs_balance_progress;
391c9e9f97bSIlya Dryomov struct btrfs_balance_control {
392c9e9f97bSIlya Dryomov 	struct btrfs_balance_args data;
393c9e9f97bSIlya Dryomov 	struct btrfs_balance_args meta;
394c9e9f97bSIlya Dryomov 	struct btrfs_balance_args sys;
395c9e9f97bSIlya Dryomov 
396c9e9f97bSIlya Dryomov 	u64 flags;
39719a39dceSIlya Dryomov 
39819a39dceSIlya Dryomov 	struct btrfs_balance_progress stat;
399c9e9f97bSIlya Dryomov };
400c9e9f97bSIlya Dryomov 
401cf8cddd3SChristoph Hellwig enum btrfs_map_op {
402cf8cddd3SChristoph Hellwig 	BTRFS_MAP_READ,
403cf8cddd3SChristoph Hellwig 	BTRFS_MAP_WRITE,
404cf8cddd3SChristoph Hellwig 	BTRFS_MAP_DISCARD,
405cf8cddd3SChristoph Hellwig 	BTRFS_MAP_GET_READ_MIRRORS,
406cf8cddd3SChristoph Hellwig };
407cf8cddd3SChristoph Hellwig 
408cf8cddd3SChristoph Hellwig static inline enum btrfs_map_op btrfs_op(struct bio *bio)
409cf8cddd3SChristoph Hellwig {
410cf8cddd3SChristoph Hellwig 	switch (bio_op(bio)) {
411cf8cddd3SChristoph Hellwig 	case REQ_OP_DISCARD:
412cf8cddd3SChristoph Hellwig 		return BTRFS_MAP_DISCARD;
413cf8cddd3SChristoph Hellwig 	case REQ_OP_WRITE:
414cf8cddd3SChristoph Hellwig 		return BTRFS_MAP_WRITE;
415cf8cddd3SChristoph Hellwig 	default:
416cf8cddd3SChristoph Hellwig 		WARN_ON_ONCE(1);
4170a4c9265SGustavo A. R. Silva 		/* fall through */
418cf8cddd3SChristoph Hellwig 	case REQ_OP_READ:
419cf8cddd3SChristoph Hellwig 		return BTRFS_MAP_READ;
420cf8cddd3SChristoph Hellwig 	}
421cf8cddd3SChristoph Hellwig }
422cf8cddd3SChristoph Hellwig 
4236e9606d2SZhao Lei void btrfs_get_bbio(struct btrfs_bio *bbio);
4246e9606d2SZhao Lei void btrfs_put_bbio(struct btrfs_bio *bbio);
425cf8cddd3SChristoph Hellwig int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
426cea9e445SChris Mason 		    u64 logical, u64 *length,
427a1d3c478SJan Schmidt 		    struct btrfs_bio **bbio_ret, int mirror_num);
428cf8cddd3SChristoph Hellwig int btrfs_map_sblock(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
429af8e2d1dSMiao Xie 		     u64 logical, u64 *length,
430825ad4c9SDavid Sterba 		     struct btrfs_bio **bbio_ret);
4315f141126SNikolay Borisov int btrfs_get_io_geometry(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
4325f141126SNikolay Borisov 			  u64 logical, u64 len, struct btrfs_io_geometry *io_geom);
43363a9c7b9SNikolay Borisov int btrfs_rmap_block(struct btrfs_fs_info *fs_info, u64 chunk_start,
43463a9c7b9SNikolay Borisov 		     u64 physical, u64 **logical, int *naddrs, int *stripe_len);
4356bccf3abSJeff Mahoney int btrfs_read_sys_array(struct btrfs_fs_info *fs_info);
4365b4aacefSJeff Mahoney int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info);
437c216b203SNikolay Borisov int btrfs_alloc_chunk(struct btrfs_trans_handle *trans, u64 type);
438c8bf1b67SDavid Sterba void btrfs_mapping_tree_free(struct extent_map_tree *tree);
43958efbc9fSOmar Sandoval blk_status_t btrfs_map_bio(struct btrfs_fs_info *fs_info, struct bio *bio,
4408b712842SChris Mason 			   int mirror_num, int async_submit);
4418a4b83ccSChris Mason int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
44297288f2cSChristoph Hellwig 		       fmode_t flags, void *holder);
44336350e95SGu Jinxiang struct btrfs_device *btrfs_scan_one_device(const char *path,
44436350e95SGu Jinxiang 					   fmode_t flags, void *holder);
445228a73abSAnand Jain int btrfs_forget_devices(const char *path);
4468a4b83ccSChris Mason int btrfs_close_devices(struct btrfs_fs_devices *fs_devices);
4479b99b115SAnand Jain void btrfs_free_extra_devids(struct btrfs_fs_devices *fs_devices, int step);
448d6507cf1SNikolay Borisov void btrfs_assign_next_active_device(struct btrfs_device *device,
449d6507cf1SNikolay Borisov 				     struct btrfs_device *this_dev);
450a27a94c2SNikolay Borisov struct btrfs_device *btrfs_find_device_by_devspec(struct btrfs_fs_info *fs_info,
451a27a94c2SNikolay Borisov 						  u64 devid,
452a27a94c2SNikolay Borisov 						  const char *devpath);
45312bd2fc0SIlya Dryomov struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
45412bd2fc0SIlya Dryomov 					const u64 *devid,
45512bd2fc0SIlya Dryomov 					const u8 *uuid);
456a425f9d4SDavid Sterba void btrfs_free_device(struct btrfs_device *device);
4572ff7e61eSJeff Mahoney int btrfs_rm_device(struct btrfs_fs_info *fs_info,
458da353f6bSDavid Sterba 		    const char *device_path, u64 devid);
459ffc5a379SDavid Sterba void __exit btrfs_cleanup_fs_uuids(void);
4605d964051SStefan Behrens int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len);
4618f18cf13SChris Mason int btrfs_grow_device(struct btrfs_trans_handle *trans,
4628f18cf13SChris Mason 		      struct btrfs_device *device, u64 new_size);
463e4319cd9SAnand Jain struct btrfs_device *btrfs_find_device(struct btrfs_fs_devices *fs_devices,
46409ba3bc9SAnand Jain 				       u64 devid, u8 *uuid, u8 *fsid, bool seed);
4658f18cf13SChris Mason int btrfs_shrink_device(struct btrfs_device *device, u64 new_size);
466da353f6bSDavid Sterba int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *path);
4676fcf6e2bSDavid Sterba int btrfs_balance(struct btrfs_fs_info *fs_info,
4686fcf6e2bSDavid Sterba 		  struct btrfs_balance_control *bctl,
469c9e9f97bSIlya Dryomov 		  struct btrfs_ioctl_balance_args *bargs);
470f89e09cfSAnand Jain void btrfs_describe_block_groups(u64 flags, char *buf, u32 size_buf);
4712b6ba629SIlya Dryomov int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info);
47268310a5eSIlya Dryomov int btrfs_recover_balance(struct btrfs_fs_info *fs_info);
473837d5b6eSIlya Dryomov int btrfs_pause_balance(struct btrfs_fs_info *fs_info);
474a7e99c69SIlya Dryomov int btrfs_cancel_balance(struct btrfs_fs_info *fs_info);
475f7a81ea4SStefan Behrens int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info);
47670f80175SStefan Behrens int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info);
4772ff7e61eSJeff Mahoney int btrfs_chunk_readonly(struct btrfs_fs_info *fs_info, u64 chunk_offset);
47860dfdf25SNikolay Borisov int find_free_dev_extent_start(struct btrfs_device *device, u64 num_bytes,
479499f377fSJeff Mahoney 			       u64 search_start, u64 *start, u64 *max_avail);
48060dfdf25SNikolay Borisov int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes,
481ba1bf481SJosef Bacik 			 u64 *start, u64 *max_avail);
482442a4f63SStefan Behrens void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index);
4832ff7e61eSJeff Mahoney int btrfs_get_dev_stats(struct btrfs_fs_info *fs_info,
484b27f7c0cSDavid Sterba 			struct btrfs_ioctl_get_dev_stats *stats);
485cb517eabSMiao Xie void btrfs_init_devices_late(struct btrfs_fs_info *fs_info);
486733f4fbbSStefan Behrens int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info);
487196c9d8dSDavid Sterba int btrfs_run_dev_stats(struct btrfs_trans_handle *trans);
48868a9db5fSNikolay Borisov void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_device *srcdev);
48965237ee3SDavid Sterba void btrfs_rm_dev_replace_free_srcdev(struct btrfs_device *srcdev);
4904f5ad7bdSNikolay Borisov void btrfs_destroy_dev_replace_tgtdev(struct btrfs_device *tgtdev);
491da353f6bSDavid Sterba void btrfs_scratch_superblocks(struct block_device *bdev, const char *device_path);
492592d92eeSLiu Bo int btrfs_is_parity_mirror(struct btrfs_fs_info *fs_info,
493e4ff5fb5SNikolay Borisov 			   u64 logical, u64 len);
4942ff7e61eSJeff Mahoney unsigned long btrfs_full_stripe_len(struct btrfs_fs_info *fs_info,
49553b381b3SDavid Woodhouse 				    u64 logical);
4966df9a95eSJosef Bacik int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
4976df9a95eSJosef Bacik 			     u64 chunk_offset, u64 chunk_size);
49897aff912SNikolay Borisov int btrfs_remove_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset);
49960ca842eSOmar Sandoval struct extent_map *btrfs_get_chunk_map(struct btrfs_fs_info *fs_info,
50060ca842eSOmar Sandoval 				       u64 logical, u64 length);
501addc3fa7SMiao Xie 
502442a4f63SStefan Behrens static inline void btrfs_dev_stat_inc(struct btrfs_device *dev,
503442a4f63SStefan Behrens 				      int index)
504442a4f63SStefan Behrens {
505442a4f63SStefan Behrens 	atomic_inc(dev->dev_stat_values + index);
5069deae968SNikolay Borisov 	/*
5079deae968SNikolay Borisov 	 * This memory barrier orders stores updating statistics before stores
5089deae968SNikolay Borisov 	 * updating dev_stats_ccnt.
5099deae968SNikolay Borisov 	 *
5109deae968SNikolay Borisov 	 * It pairs with smp_rmb() in btrfs_run_dev_stats().
5119deae968SNikolay Borisov 	 */
512addc3fa7SMiao Xie 	smp_mb__before_atomic();
513addc3fa7SMiao Xie 	atomic_inc(&dev->dev_stats_ccnt);
514442a4f63SStefan Behrens }
515442a4f63SStefan Behrens 
516442a4f63SStefan Behrens static inline int btrfs_dev_stat_read(struct btrfs_device *dev,
517442a4f63SStefan Behrens 				      int index)
518442a4f63SStefan Behrens {
519442a4f63SStefan Behrens 	return atomic_read(dev->dev_stat_values + index);
520442a4f63SStefan Behrens }
521442a4f63SStefan Behrens 
522442a4f63SStefan Behrens static inline int btrfs_dev_stat_read_and_reset(struct btrfs_device *dev,
523442a4f63SStefan Behrens 						int index)
524442a4f63SStefan Behrens {
525442a4f63SStefan Behrens 	int ret;
526442a4f63SStefan Behrens 
527442a4f63SStefan Behrens 	ret = atomic_xchg(dev->dev_stat_values + index, 0);
5284660c49fSNikolay Borisov 	/*
5294660c49fSNikolay Borisov 	 * atomic_xchg implies a full memory barriers as per atomic_t.txt:
5304660c49fSNikolay Borisov 	 * - RMW operations that have a return value are fully ordered;
5314660c49fSNikolay Borisov 	 *
5324660c49fSNikolay Borisov 	 * This implicit memory barriers is paired with the smp_rmb in
5334660c49fSNikolay Borisov 	 * btrfs_run_dev_stats
5344660c49fSNikolay Borisov 	 */
535addc3fa7SMiao Xie 	atomic_inc(&dev->dev_stats_ccnt);
536442a4f63SStefan Behrens 	return ret;
537442a4f63SStefan Behrens }
538442a4f63SStefan Behrens 
539442a4f63SStefan Behrens static inline void btrfs_dev_stat_set(struct btrfs_device *dev,
540442a4f63SStefan Behrens 				      int index, unsigned long val)
541442a4f63SStefan Behrens {
542442a4f63SStefan Behrens 	atomic_set(dev->dev_stat_values + index, val);
5439deae968SNikolay Borisov 	/*
5449deae968SNikolay Borisov 	 * This memory barrier orders stores updating statistics before stores
5459deae968SNikolay Borisov 	 * updating dev_stats_ccnt.
5469deae968SNikolay Borisov 	 *
5479deae968SNikolay Borisov 	 * It pairs with smp_rmb() in btrfs_run_dev_stats().
5489deae968SNikolay Borisov 	 */
549addc3fa7SMiao Xie 	smp_mb__before_atomic();
550addc3fa7SMiao Xie 	atomic_inc(&dev->dev_stats_ccnt);
551442a4f63SStefan Behrens }
552442a4f63SStefan Behrens 
553442a4f63SStefan Behrens static inline void btrfs_dev_stat_reset(struct btrfs_device *dev,
554442a4f63SStefan Behrens 					int index)
555442a4f63SStefan Behrens {
556442a4f63SStefan Behrens 	btrfs_dev_stat_set(dev, index, 0);
557442a4f63SStefan Behrens }
558935e5cc9SMiao Xie 
5593e72ee88SQu Wenruo /*
5603e72ee88SQu Wenruo  * Convert block group flags (BTRFS_BLOCK_GROUP_*) to btrfs_raid_types, which
5613e72ee88SQu Wenruo  * can be used as index to access btrfs_raid_array[].
5623e72ee88SQu Wenruo  */
5633e72ee88SQu Wenruo static inline enum btrfs_raid_types btrfs_bg_flags_to_raid_index(u64 flags)
5643e72ee88SQu Wenruo {
5653e72ee88SQu Wenruo 	if (flags & BTRFS_BLOCK_GROUP_RAID10)
5663e72ee88SQu Wenruo 		return BTRFS_RAID_RAID10;
5673e72ee88SQu Wenruo 	else if (flags & BTRFS_BLOCK_GROUP_RAID1)
5683e72ee88SQu Wenruo 		return BTRFS_RAID_RAID1;
5693e72ee88SQu Wenruo 	else if (flags & BTRFS_BLOCK_GROUP_DUP)
5703e72ee88SQu Wenruo 		return BTRFS_RAID_DUP;
5713e72ee88SQu Wenruo 	else if (flags & BTRFS_BLOCK_GROUP_RAID0)
5723e72ee88SQu Wenruo 		return BTRFS_RAID_RAID0;
5733e72ee88SQu Wenruo 	else if (flags & BTRFS_BLOCK_GROUP_RAID5)
5743e72ee88SQu Wenruo 		return BTRFS_RAID_RAID5;
5753e72ee88SQu Wenruo 	else if (flags & BTRFS_BLOCK_GROUP_RAID6)
5763e72ee88SQu Wenruo 		return BTRFS_RAID_RAID6;
5773e72ee88SQu Wenruo 
5783e72ee88SQu Wenruo 	return BTRFS_RAID_SINGLE; /* BTRFS_BLOCK_GROUP_SINGLE */
5793e72ee88SQu Wenruo }
5803e72ee88SQu Wenruo 
581bbbf7243SNikolay Borisov void btrfs_commit_device_sizes(struct btrfs_transaction *trans);
58204216820SFilipe Manana 
583c73eccf7SAnand Jain struct list_head *btrfs_get_fs_uuids(void);
5845a13f430SAnand Jain void btrfs_set_fs_info_ptr(struct btrfs_fs_info *fs_info);
5855a13f430SAnand Jain void btrfs_reset_fs_info_ptr(struct btrfs_fs_info *fs_info);
5866528b99dSAnand Jain bool btrfs_check_rw_degradable(struct btrfs_fs_info *fs_info,
5876528b99dSAnand Jain 					struct btrfs_device *failing_dev);
58821634a19SQu Wenruo 
58946df06b8SDavid Sterba int btrfs_bg_type_to_factor(u64 flags);
590158da513SDavid Sterba const char *btrfs_bg_type_to_raid_name(u64 flags);
591cf90d884SQu Wenruo int btrfs_verify_dev_extents(struct btrfs_fs_info *fs_info);
59246df06b8SDavid Sterba 
5930b86a832SChris Mason #endif
594