xref: /openbmc/linux/fs/btrfs/volumes.h (revision 00d82525)
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 
205f141126SNikolay Borisov struct btrfs_io_geometry {
215f141126SNikolay Borisov 	/* remaining bytes before crossing a stripe */
225f141126SNikolay Borisov 	u64 len;
235f141126SNikolay Borisov 	/* offset of logical address in chunk */
245f141126SNikolay Borisov 	u64 offset;
255f141126SNikolay Borisov 	/* length of single IO stripe */
265f141126SNikolay Borisov 	u64 stripe_len;
275f141126SNikolay Borisov 	/* number of stripe where address falls */
285f141126SNikolay Borisov 	u64 stripe_nr;
295f141126SNikolay Borisov 	/* offset of address in stripe */
305f141126SNikolay Borisov 	u64 stripe_offset;
315f141126SNikolay Borisov 	/* offset of raid56 stripe into the chunk */
325f141126SNikolay Borisov 	u64 raid56_stripe_offset;
335f141126SNikolay Borisov };
345f141126SNikolay Borisov 
357cc8e58dSMiao Xie /*
367cc8e58dSMiao Xie  * Use sequence counter to get consistent device stat data on
377cc8e58dSMiao Xie  * 32-bit processors.
387cc8e58dSMiao Xie  */
397cc8e58dSMiao Xie #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
407cc8e58dSMiao Xie #include <linux/seqlock.h>
417cc8e58dSMiao Xie #define __BTRFS_NEED_DEVICE_DATA_ORDERED
42c41ec452SSu Yue #define btrfs_device_data_ordered_init(device)	\
43c41ec452SSu Yue 	seqcount_init(&device->data_seqcount)
447cc8e58dSMiao Xie #else
45c41ec452SSu Yue #define btrfs_device_data_ordered_init(device) do { } while (0)
467cc8e58dSMiao Xie #endif
477cc8e58dSMiao Xie 
48ebbede42SAnand Jain #define BTRFS_DEV_STATE_WRITEABLE	(0)
49e12c9621SAnand Jain #define BTRFS_DEV_STATE_IN_FS_METADATA	(1)
50e6e674bdSAnand Jain #define BTRFS_DEV_STATE_MISSING		(2)
51401e29c1SAnand Jain #define BTRFS_DEV_STATE_REPLACE_TGT	(3)
521c3063b6SAnand Jain #define BTRFS_DEV_STATE_FLUSH_SENT	(4)
5366d204a1SFilipe Manana #define BTRFS_DEV_STATE_NO_READA	(5)
54ebbede42SAnand Jain 
555b316468SNaohiro Aota struct btrfs_zoned_device_info;
565b316468SNaohiro Aota 
570b86a832SChris Mason struct btrfs_device {
580b6f5d40SNikolay Borisov 	struct list_head dev_list; /* device_list_mutex */
590b6f5d40SNikolay Borisov 	struct list_head dev_alloc_list; /* chunk mutex */
60bbbf7243SNikolay Borisov 	struct list_head post_commit_list; /* chunk mutex */
612b82032cSYan Zheng 	struct btrfs_fs_devices *fs_devices;
62fb456252SJeff Mahoney 	struct btrfs_fs_info *fs_info;
63ffbd517dSChris Mason 
648d1a7aaeSMadhuparna Bhowmik 	struct rcu_string __rcu *name;
65d5ee37bcSMiao Xie 
66d5ee37bcSMiao Xie 	u64 generation;
67d5ee37bcSMiao Xie 
68d5ee37bcSMiao Xie 	struct block_device *bdev;
69d5ee37bcSMiao Xie 
705b316468SNaohiro Aota 	struct btrfs_zoned_device_info *zone_info;
715b316468SNaohiro Aota 
72d5ee37bcSMiao Xie 	/* the mode sent to blkdev_get */
73d5ee37bcSMiao Xie 	fmode_t mode;
74d5ee37bcSMiao Xie 
754889bc05SAnand Jain 	/*
764889bc05SAnand Jain 	 * Device's major-minor number. Must be set even if the device is not
774889bc05SAnand Jain 	 * opened (bdev == NULL), unless the device is missing.
784889bc05SAnand Jain 	 */
794889bc05SAnand Jain 	dev_t devt;
80ebbede42SAnand Jain 	unsigned long dev_state;
8158efbc9fSOmar Sandoval 	blk_status_t last_flush_error;
82b3075717SChris Mason 
837cc8e58dSMiao Xie #ifdef __BTRFS_NEED_DEVICE_DATA_ORDERED
84c41ec452SSu Yue 	seqcount_t data_seqcount;
857cc8e58dSMiao Xie #endif
867cc8e58dSMiao Xie 
870b86a832SChris Mason 	/* the internal btrfs device id */
880b86a832SChris Mason 	u64 devid;
890b86a832SChris Mason 
906ba40b61SMiao Xie 	/* size of the device in memory */
910b86a832SChris Mason 	u64 total_bytes;
920b86a832SChris Mason 
936ba40b61SMiao Xie 	/* size of the device on disk */
94d6397baeSChris Ball 	u64 disk_total_bytes;
95d6397baeSChris Ball 
960b86a832SChris Mason 	/* bytes used */
970b86a832SChris Mason 	u64 bytes_used;
980b86a832SChris Mason 
990b86a832SChris Mason 	/* optimal io alignment for this device */
1000b86a832SChris Mason 	u32 io_align;
1010b86a832SChris Mason 
1020b86a832SChris Mason 	/* optimal io width for this device */
1030b86a832SChris Mason 	u32 io_width;
1043c45bfc1SDulshani Gunawardhana 	/* type and info about this device */
1053c45bfc1SDulshani Gunawardhana 	u64 type;
1060b86a832SChris Mason 
1070b86a832SChris Mason 	/* minimal io size for this device */
1080b86a832SChris Mason 	u32 sector_size;
1090b86a832SChris Mason 
1100b86a832SChris Mason 	/* physical drive uuid (or lvm uuid) */
111e17cade2SChris Mason 	u8 uuid[BTRFS_UUID_SIZE];
1128b712842SChris Mason 
113935e5cc9SMiao Xie 	/*
114935e5cc9SMiao Xie 	 * size of the device on the current transaction
115935e5cc9SMiao Xie 	 *
116935e5cc9SMiao Xie 	 * This variant is update when committing the transaction,
117bbbf7243SNikolay Borisov 	 * and protected by chunk mutex
118935e5cc9SMiao Xie 	 */
119935e5cc9SMiao Xie 	u64 commit_total_bytes;
120935e5cc9SMiao Xie 
121ce7213c7SMiao Xie 	/* bytes used on the current transaction */
122ce7213c7SMiao Xie 	u64 commit_bytes_used;
123935e5cc9SMiao Xie 
1243c45bfc1SDulshani Gunawardhana 	/* for sending down flush barriers */
1253c45bfc1SDulshani Gunawardhana 	struct bio *flush_bio;
1263c45bfc1SDulshani Gunawardhana 	struct completion flush_wait;
1273c45bfc1SDulshani Gunawardhana 
128a2de733cSArne Jansen 	/* per-device scrub information */
129cadbc0a0SAnand Jain 	struct scrub_ctx *scrub_ctx;
130a2de733cSArne Jansen 
131442a4f63SStefan Behrens 	/* disk I/O failure stats. For detailed description refer to
132442a4f63SStefan Behrens 	 * enum btrfs_dev_stat_values in ioctl.h */
133733f4fbbSStefan Behrens 	int dev_stats_valid;
134addc3fa7SMiao Xie 
135addc3fa7SMiao Xie 	/* Counter to record the change of device stats */
136addc3fa7SMiao Xie 	atomic_t dev_stats_ccnt;
137442a4f63SStefan Behrens 	atomic_t dev_stat_values[BTRFS_DEV_STAT_VALUES_MAX];
1381c11b63eSJeff Mahoney 
1391c11b63eSJeff Mahoney 	struct extent_io_tree alloc_state;
140668e48afSAnand Jain 
141668e48afSAnand Jain 	struct completion kobj_unregister;
142668e48afSAnand Jain 	/* For sysfs/FSID/devinfo/devid/ */
143668e48afSAnand Jain 	struct kobject devid_kobj;
144eb3b5053SDavid Sterba 
145eb3b5053SDavid Sterba 	/* Bandwidth limit for scrub, in bytes */
146eb3b5053SDavid Sterba 	u64 scrub_speed_max;
1470b86a832SChris Mason };
1480b86a832SChris Mason 
1497cc8e58dSMiao Xie /*
1507cc8e58dSMiao Xie  * If we read those variants at the context of their own lock, we needn't
1517cc8e58dSMiao Xie  * use the following helpers, reading them directly is safe.
1527cc8e58dSMiao Xie  */
1537cc8e58dSMiao Xie #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
1547cc8e58dSMiao Xie #define BTRFS_DEVICE_GETSET_FUNCS(name)					\
1557cc8e58dSMiao Xie static inline u64							\
1567cc8e58dSMiao Xie btrfs_device_get_##name(const struct btrfs_device *dev)			\
1577cc8e58dSMiao Xie {									\
1587cc8e58dSMiao Xie 	u64 size;							\
1597cc8e58dSMiao Xie 	unsigned int seq;						\
1607cc8e58dSMiao Xie 									\
1617cc8e58dSMiao Xie 	do {								\
1627cc8e58dSMiao Xie 		seq = read_seqcount_begin(&dev->data_seqcount);		\
1637cc8e58dSMiao Xie 		size = dev->name;					\
1647cc8e58dSMiao Xie 	} while (read_seqcount_retry(&dev->data_seqcount, seq));	\
1657cc8e58dSMiao Xie 	return size;							\
1667cc8e58dSMiao Xie }									\
1677cc8e58dSMiao Xie 									\
1687cc8e58dSMiao Xie static inline void							\
1697cc8e58dSMiao Xie btrfs_device_set_##name(struct btrfs_device *dev, u64 size)		\
1707cc8e58dSMiao Xie {									\
171c41ec452SSu Yue 	preempt_disable();						\
1727cc8e58dSMiao Xie 	write_seqcount_begin(&dev->data_seqcount);			\
1737cc8e58dSMiao Xie 	dev->name = size;						\
1747cc8e58dSMiao Xie 	write_seqcount_end(&dev->data_seqcount);			\
175c41ec452SSu Yue 	preempt_enable();						\
1767cc8e58dSMiao Xie }
17794545870SThomas Gleixner #elif BITS_PER_LONG==32 && defined(CONFIG_PREEMPTION)
1787cc8e58dSMiao Xie #define BTRFS_DEVICE_GETSET_FUNCS(name)					\
1797cc8e58dSMiao Xie static inline u64							\
1807cc8e58dSMiao Xie btrfs_device_get_##name(const struct btrfs_device *dev)			\
1817cc8e58dSMiao Xie {									\
1827cc8e58dSMiao Xie 	u64 size;							\
1837cc8e58dSMiao Xie 									\
1847cc8e58dSMiao Xie 	preempt_disable();						\
1857cc8e58dSMiao Xie 	size = dev->name;						\
1867cc8e58dSMiao Xie 	preempt_enable();						\
1877cc8e58dSMiao Xie 	return size;							\
1887cc8e58dSMiao Xie }									\
1897cc8e58dSMiao Xie 									\
1907cc8e58dSMiao Xie static inline void							\
1917cc8e58dSMiao Xie btrfs_device_set_##name(struct btrfs_device *dev, u64 size)		\
1927cc8e58dSMiao Xie {									\
1937cc8e58dSMiao Xie 	preempt_disable();						\
1947cc8e58dSMiao Xie 	dev->name = size;						\
1957cc8e58dSMiao Xie 	preempt_enable();						\
1967cc8e58dSMiao Xie }
1977cc8e58dSMiao Xie #else
1987cc8e58dSMiao Xie #define BTRFS_DEVICE_GETSET_FUNCS(name)					\
1997cc8e58dSMiao Xie static inline u64							\
2007cc8e58dSMiao Xie btrfs_device_get_##name(const struct btrfs_device *dev)			\
2017cc8e58dSMiao Xie {									\
2027cc8e58dSMiao Xie 	return dev->name;						\
2037cc8e58dSMiao Xie }									\
2047cc8e58dSMiao Xie 									\
2057cc8e58dSMiao Xie static inline void							\
2067cc8e58dSMiao Xie btrfs_device_set_##name(struct btrfs_device *dev, u64 size)		\
2077cc8e58dSMiao Xie {									\
2087cc8e58dSMiao Xie 	dev->name = size;						\
2097cc8e58dSMiao Xie }
2107cc8e58dSMiao Xie #endif
2117cc8e58dSMiao Xie 
2127cc8e58dSMiao Xie BTRFS_DEVICE_GETSET_FUNCS(total_bytes);
2137cc8e58dSMiao Xie BTRFS_DEVICE_GETSET_FUNCS(disk_total_bytes);
2147cc8e58dSMiao Xie BTRFS_DEVICE_GETSET_FUNCS(bytes_used);
2157cc8e58dSMiao Xie 
216c4a816c6SNaohiro Aota enum btrfs_chunk_allocation_policy {
217c4a816c6SNaohiro Aota 	BTRFS_CHUNK_ALLOC_REGULAR,
2181cd6121fSNaohiro Aota 	BTRFS_CHUNK_ALLOC_ZONED,
219c4a816c6SNaohiro Aota };
220c4a816c6SNaohiro Aota 
22133fd2f71SAnand Jain /*
22233fd2f71SAnand Jain  * Read policies for mirrored block group profiles, read picks the stripe based
22333fd2f71SAnand Jain  * on these policies.
22433fd2f71SAnand Jain  */
22533fd2f71SAnand Jain enum btrfs_read_policy {
22633fd2f71SAnand Jain 	/* Use process PID to choose the stripe */
22733fd2f71SAnand Jain 	BTRFS_READ_POLICY_PID,
22833fd2f71SAnand Jain 	BTRFS_NR_READ_POLICY,
22933fd2f71SAnand Jain };
23033fd2f71SAnand Jain 
2318a4b83ccSChris Mason struct btrfs_fs_devices {
2328a4b83ccSChris Mason 	u8 fsid[BTRFS_FSID_SIZE]; /* FS specific uuid */
2337239ff4bSNikolay Borisov 	u8 metadata_uuid[BTRFS_FSID_SIZE];
234d1a63002SNikolay Borisov 	bool fsid_change;
235c4babc5eSAnand Jain 	struct list_head fs_list;
2368a4b83ccSChris Mason 
237add9745aSAnand Jain 	/*
238add9745aSAnand Jain 	 * Number of devices under this fsid including missing and
239add9745aSAnand Jain 	 * replace-target device and excludes seed devices.
240add9745aSAnand Jain 	 */
2418a4b83ccSChris Mason 	u64 num_devices;
242add9745aSAnand Jain 
243add9745aSAnand Jain 	/*
244add9745aSAnand Jain 	 * The number of devices that successfully opened, including
245add9745aSAnand Jain 	 * replace-target, excludes seed devices.
246add9745aSAnand Jain 	 */
247a0af469bSChris Mason 	u64 open_devices;
248add9745aSAnand Jain 
249add9745aSAnand Jain 	/* The number of devices that are under the chunk allocation list. */
2502b82032cSYan Zheng 	u64 rw_devices;
251add9745aSAnand Jain 
252add9745aSAnand Jain 	/* Count of missing devices under this fsid excluding seed device. */
253cd02dca5SChris Mason 	u64 missing_devices;
2542b82032cSYan Zheng 	u64 total_rw_bytes;
255add9745aSAnand Jain 
256add9745aSAnand Jain 	/*
257add9745aSAnand Jain 	 * Count of devices from btrfs_super_block::num_devices for this fsid,
258add9745aSAnand Jain 	 * which includes the seed device, excludes the transient replace-target
259add9745aSAnand Jain 	 * device.
260add9745aSAnand Jain 	 */
26102db0844SJosef Bacik 	u64 total_devices;
262d1a63002SNikolay Borisov 
263d1a63002SNikolay Borisov 	/* Highest generation number of seen devices */
264d1a63002SNikolay Borisov 	u64 latest_generation;
265d1a63002SNikolay Borisov 
266d24fa5c1SAnand Jain 	/*
267d24fa5c1SAnand Jain 	 * The mount device or a device with highest generation after removal
268d24fa5c1SAnand Jain 	 * or replace.
269d24fa5c1SAnand Jain 	 */
270d24fa5c1SAnand Jain 	struct btrfs_device *latest_dev;
271e5e9a520SChris Mason 
272e5e9a520SChris Mason 	/* all of the devices in the FS, protected by a mutex
273e5e9a520SChris Mason 	 * so we can safely walk it to write out the supers without
2749b011adfSWang Shilong 	 * worrying about add/remove by the multi-device code.
2759b011adfSWang Shilong 	 * Scrubbing super can kick off supers writing by holding
2769b011adfSWang Shilong 	 * this mutex lock.
277e5e9a520SChris Mason 	 */
278e5e9a520SChris Mason 	struct mutex device_list_mutex;
2790b6f5d40SNikolay Borisov 
2800b6f5d40SNikolay Borisov 	/* List of all devices, protected by device_list_mutex */
2818a4b83ccSChris Mason 	struct list_head devices;
282b3075717SChris Mason 
2830b6f5d40SNikolay Borisov 	/*
2840b6f5d40SNikolay Borisov 	 * Devices which can satisfy space allocation. Protected by
2850b6f5d40SNikolay Borisov 	 * chunk_mutex
2860b6f5d40SNikolay Borisov 	 */
287b3075717SChris Mason 	struct list_head alloc_list;
2882b82032cSYan Zheng 
289944d3f9fSNikolay Borisov 	struct list_head seed_list;
2900395d84fSJohannes Thumshirn 	bool seeding;
2912b82032cSYan Zheng 
2922b82032cSYan Zheng 	int opened;
293c289811cSChris Mason 
294c289811cSChris Mason 	/* set when we find or add a device that doesn't have the
295c289811cSChris Mason 	 * nonrot flag set
296c289811cSChris Mason 	 */
2977f0432d0SJohannes Thumshirn 	bool rotating;
2982e7910d6SAnand Jain 
2995a13f430SAnand Jain 	struct btrfs_fs_info *fs_info;
3002e7910d6SAnand Jain 	/* sysfs kobjects */
301c1b7e474SAnand Jain 	struct kobject fsid_kobj;
302b5501504SAnand Jain 	struct kobject *devices_kobj;
303a013d141SAnand Jain 	struct kobject *devinfo_kobj;
3042e7910d6SAnand Jain 	struct completion kobj_unregister;
305c4a816c6SNaohiro Aota 
306c4a816c6SNaohiro Aota 	enum btrfs_chunk_allocation_policy chunk_alloc_policy;
30733fd2f71SAnand Jain 
30833fd2f71SAnand Jain 	/* Policy used to read the mirrored stripes */
30933fd2f71SAnand Jain 	enum btrfs_read_policy read_policy;
3108a4b83ccSChris Mason };
3118a4b83ccSChris Mason 
312facc8a22SMiao Xie #define BTRFS_BIO_INLINE_CSUM_SIZE	64
313facc8a22SMiao Xie 
314ab4ba2e1SQu Wenruo #define BTRFS_MAX_DEVS(info) ((BTRFS_MAX_ITEM_SIZE(info)	\
315ab4ba2e1SQu Wenruo 			- sizeof(struct btrfs_chunk))		\
316ab4ba2e1SQu Wenruo 			/ sizeof(struct btrfs_stripe) + 1)
317ab4ba2e1SQu Wenruo 
318ab4ba2e1SQu Wenruo #define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE	\
319ab4ba2e1SQu Wenruo 				- 2 * sizeof(struct btrfs_disk_key)	\
320ab4ba2e1SQu Wenruo 				- 2 * sizeof(struct btrfs_chunk))	\
321ab4ba2e1SQu Wenruo 				/ sizeof(struct btrfs_stripe) + 1)
322ab4ba2e1SQu Wenruo 
3239be3395bSChris Mason /*
324c3a3b19bSQu Wenruo  * Additional info to pass along bio.
325c3a3b19bSQu Wenruo  *
326c3a3b19bSQu Wenruo  * Mostly for btrfs specific features like csum and mirror_num.
3279be3395bSChris Mason  */
328c3a3b19bSQu Wenruo struct btrfs_bio {
329c1dc0896SMiao Xie 	unsigned int mirror_num;
330c3a3b19bSQu Wenruo 
331*00d82525SChristoph Hellwig 	/* for direct I/O */
332*00d82525SChristoph Hellwig 	u64 file_offset;
333*00d82525SChristoph Hellwig 
334c3a3b19bSQu Wenruo 	/* @device is for stripe IO submission. */
335c31efbdfSNikolay Borisov 	struct btrfs_device *device;
336facc8a22SMiao Xie 	u8 *csum;
337facc8a22SMiao Xie 	u8 csum_inline[BTRFS_BIO_INLINE_CSUM_SIZE];
33817347cecSLiu Bo 	struct bvec_iter iter;
339c3a3b19bSQu Wenruo 
340fa1bcbe0SDavid Sterba 	/*
341fa1bcbe0SDavid Sterba 	 * This member must come last, bio_alloc_bioset will allocate enough
342c3a3b19bSQu Wenruo 	 * bytes for entire btrfs_bio but relies on bio being last.
343fa1bcbe0SDavid Sterba 	 */
3449be3395bSChris Mason 	struct bio bio;
3459be3395bSChris Mason };
3469be3395bSChris Mason 
347c3a3b19bSQu Wenruo static inline struct btrfs_bio *btrfs_bio(struct bio *bio)
3489be3395bSChris Mason {
349c3a3b19bSQu Wenruo 	return container_of(bio, struct btrfs_bio, bio);
3509be3395bSChris Mason }
3519be3395bSChris Mason 
352c3a3b19bSQu Wenruo static inline void btrfs_bio_free_csum(struct btrfs_bio *bbio)
353b3a0dd50SDavid Sterba {
354c3a3b19bSQu Wenruo 	if (bbio->csum != bbio->csum_inline) {
355c3a3b19bSQu Wenruo 		kfree(bbio->csum);
356c3a3b19bSQu Wenruo 		bbio->csum = NULL;
357b3a0dd50SDavid Sterba 	}
358b3a0dd50SDavid Sterba }
359b3a0dd50SDavid Sterba 
3604c664611SQu Wenruo struct btrfs_io_stripe {
361cea9e445SChris Mason 	struct btrfs_device *dev;
362cea9e445SChris Mason 	u64 physical;
363fce3bb9aSLi Dongyang 	u64 length; /* only used for discard mappings */
364cea9e445SChris Mason };
365cea9e445SChris Mason 
3664c664611SQu Wenruo /*
3674c664611SQu Wenruo  * Context for IO subsmission for device stripe.
3684c664611SQu Wenruo  *
3694c664611SQu Wenruo  * - Track the unfinished mirrors for mirror based profiles
3704c664611SQu Wenruo  *   Mirror based profiles are SINGLE/DUP/RAID1/RAID10.
3714c664611SQu Wenruo  *
3724c664611SQu Wenruo  * - Contain the logical -> physical mapping info
3734c664611SQu Wenruo  *   Used by submit_stripe_bio() for mapping logical bio
3744c664611SQu Wenruo  *   into physical device address.
3754c664611SQu Wenruo  *
3764c664611SQu Wenruo  * - Contain device replace info
3774c664611SQu Wenruo  *   Used by handle_ops_on_dev_replace() to copy logical bios
3784c664611SQu Wenruo  *   into the new device.
3794c664611SQu Wenruo  *
3804c664611SQu Wenruo  * - Contain RAID56 full stripe logical bytenrs
3814c664611SQu Wenruo  */
3824c664611SQu Wenruo struct btrfs_io_context {
383140475aeSElena Reshetova 	refcount_t refs;
384cea9e445SChris Mason 	atomic_t stripes_pending;
385c404e0dcSMiao Xie 	struct btrfs_fs_info *fs_info;
38610f11900SZhao Lei 	u64 map_type; /* get from map_lookup->type */
387cea9e445SChris Mason 	bio_end_io_t *end_io;
3887d2b4daaSChris Mason 	struct bio *orig_bio;
389cea9e445SChris Mason 	void *private;
390a236aed1SChris Mason 	atomic_t error;
391a236aed1SChris Mason 	int max_errors;
392cea9e445SChris Mason 	int num_stripes;
393a1d3c478SJan Schmidt 	int mirror_num;
3942c8cdd6eSMiao Xie 	int num_tgtdevs;
3952c8cdd6eSMiao Xie 	int *tgtdev_map;
3968e5cfb55SZhao Lei 	/*
3978e5cfb55SZhao Lei 	 * logical block numbers for the start of each stripe
3988e5cfb55SZhao Lei 	 * The last one or two are p/q.  These are sorted,
3998e5cfb55SZhao Lei 	 * so raid_map[0] is the start of our full stripe
4008e5cfb55SZhao Lei 	 */
4018e5cfb55SZhao Lei 	u64 *raid_map;
4024c664611SQu Wenruo 	struct btrfs_io_stripe stripes[];
403cea9e445SChris Mason };
404cea9e445SChris Mason 
405b2117a39SMiao Xie struct btrfs_device_info {
406b2117a39SMiao Xie 	struct btrfs_device *dev;
407b2117a39SMiao Xie 	u64 dev_offset;
408b2117a39SMiao Xie 	u64 max_avail;
40973c5de00SArne Jansen 	u64 total_avail;
410b2117a39SMiao Xie };
411b2117a39SMiao Xie 
41231e50229SLiu Bo struct btrfs_raid_attr {
4138c3e3582SDavid Sterba 	u8 sub_stripes;		/* sub_stripes info for map */
4148c3e3582SDavid Sterba 	u8 dev_stripes;		/* stripes per dev */
4158c3e3582SDavid Sterba 	u8 devs_max;		/* max devs to use */
4168c3e3582SDavid Sterba 	u8 devs_min;		/* min devs needed */
4178c3e3582SDavid Sterba 	u8 tolerated_failures;	/* max tolerated fail devs */
4188c3e3582SDavid Sterba 	u8 devs_increment;	/* ndevs has to be a multiple of this */
4198c3e3582SDavid Sterba 	u8 ncopies;		/* how many copies to data has */
4208c3e3582SDavid Sterba 	u8 nparity;		/* number of stripes worth of bytes to store
421b50836edSHans van Kranenburg 				 * parity information */
4228c3e3582SDavid Sterba 	u8 mindev_error;	/* error code if min devs requisite is unmet */
423ed23467bSAnand Jain 	const char raid_name[8]; /* name of the raid */
42441a6e891SAnand Jain 	u64 bg_flag;		/* block group flag of the raid */
42531e50229SLiu Bo };
42631e50229SLiu Bo 
427af902047SZhao Lei extern const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES];
428af902047SZhao Lei 
4291abe9b8aSliubo struct map_lookup {
4301abe9b8aSliubo 	u64 type;
4311abe9b8aSliubo 	int io_align;
4321abe9b8aSliubo 	int io_width;
4333d8da678SLiu Bo 	u64 stripe_len;
4341abe9b8aSliubo 	int num_stripes;
4351abe9b8aSliubo 	int sub_stripes;
436cf90d884SQu Wenruo 	int verified_stripes; /* For mount time dev extent verification */
4374c664611SQu Wenruo 	struct btrfs_io_stripe stripes[];
4381abe9b8aSliubo };
4391abe9b8aSliubo 
440a2de733cSArne Jansen #define map_lookup_size(n) (sizeof(struct map_lookup) + \
4414c664611SQu Wenruo 			    (sizeof(struct btrfs_io_stripe) * (n)))
442a2de733cSArne Jansen 
443c9e9f97bSIlya Dryomov struct btrfs_balance_args;
44419a39dceSIlya Dryomov struct btrfs_balance_progress;
445c9e9f97bSIlya Dryomov struct btrfs_balance_control {
446c9e9f97bSIlya Dryomov 	struct btrfs_balance_args data;
447c9e9f97bSIlya Dryomov 	struct btrfs_balance_args meta;
448c9e9f97bSIlya Dryomov 	struct btrfs_balance_args sys;
449c9e9f97bSIlya Dryomov 
450c9e9f97bSIlya Dryomov 	u64 flags;
45119a39dceSIlya Dryomov 
45219a39dceSIlya Dryomov 	struct btrfs_balance_progress stat;
453c9e9f97bSIlya Dryomov };
454c9e9f97bSIlya Dryomov 
455562d7b15SJosef Bacik /*
456562d7b15SJosef Bacik  * Search for a given device by the set parameters
457562d7b15SJosef Bacik  */
458562d7b15SJosef Bacik struct btrfs_dev_lookup_args {
459562d7b15SJosef Bacik 	u64 devid;
460562d7b15SJosef Bacik 	u8 *uuid;
461562d7b15SJosef Bacik 	u8 *fsid;
462562d7b15SJosef Bacik 	bool missing;
463562d7b15SJosef Bacik };
464562d7b15SJosef Bacik 
465562d7b15SJosef Bacik /* We have to initialize to -1 because BTRFS_DEV_REPLACE_DEVID is 0 */
466562d7b15SJosef Bacik #define BTRFS_DEV_LOOKUP_ARGS_INIT { .devid = (u64)-1 }
467562d7b15SJosef Bacik 
468562d7b15SJosef Bacik #define BTRFS_DEV_LOOKUP_ARGS(name) \
469562d7b15SJosef Bacik 	struct btrfs_dev_lookup_args name = BTRFS_DEV_LOOKUP_ARGS_INIT
470562d7b15SJosef Bacik 
471cf8cddd3SChristoph Hellwig enum btrfs_map_op {
472cf8cddd3SChristoph Hellwig 	BTRFS_MAP_READ,
473cf8cddd3SChristoph Hellwig 	BTRFS_MAP_WRITE,
474cf8cddd3SChristoph Hellwig 	BTRFS_MAP_DISCARD,
475cf8cddd3SChristoph Hellwig 	BTRFS_MAP_GET_READ_MIRRORS,
476cf8cddd3SChristoph Hellwig };
477cf8cddd3SChristoph Hellwig 
478cf8cddd3SChristoph Hellwig static inline enum btrfs_map_op btrfs_op(struct bio *bio)
479cf8cddd3SChristoph Hellwig {
480cf8cddd3SChristoph Hellwig 	switch (bio_op(bio)) {
481cf8cddd3SChristoph Hellwig 	case REQ_OP_DISCARD:
482cf8cddd3SChristoph Hellwig 		return BTRFS_MAP_DISCARD;
483cf8cddd3SChristoph Hellwig 	case REQ_OP_WRITE:
484cfe94440SNaohiro Aota 	case REQ_OP_ZONE_APPEND:
485cf8cddd3SChristoph Hellwig 		return BTRFS_MAP_WRITE;
486cf8cddd3SChristoph Hellwig 	default:
487cf8cddd3SChristoph Hellwig 		WARN_ON_ONCE(1);
488c730ae0cSMarcos Paulo de Souza 		fallthrough;
489cf8cddd3SChristoph Hellwig 	case REQ_OP_READ:
490cf8cddd3SChristoph Hellwig 		return BTRFS_MAP_READ;
491cf8cddd3SChristoph Hellwig 	}
492cf8cddd3SChristoph Hellwig }
493cf8cddd3SChristoph Hellwig 
4944c664611SQu Wenruo void btrfs_get_bioc(struct btrfs_io_context *bioc);
4954c664611SQu Wenruo void btrfs_put_bioc(struct btrfs_io_context *bioc);
496cf8cddd3SChristoph Hellwig int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
497cea9e445SChris Mason 		    u64 logical, u64 *length,
4984c664611SQu Wenruo 		    struct btrfs_io_context **bioc_ret, int mirror_num);
499cf8cddd3SChristoph Hellwig int btrfs_map_sblock(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
500af8e2d1dSMiao Xie 		     u64 logical, u64 *length,
5014c664611SQu Wenruo 		     struct btrfs_io_context **bioc_ret);
50242034313SMichal Rostecki int btrfs_get_io_geometry(struct btrfs_fs_info *fs_info, struct extent_map *map,
50343c0d1a5SQu Wenruo 			  enum btrfs_map_op op, u64 logical,
50442034313SMichal Rostecki 			  struct btrfs_io_geometry *io_geom);
5056bccf3abSJeff Mahoney int btrfs_read_sys_array(struct btrfs_fs_info *fs_info);
5065b4aacefSJeff Mahoney int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info);
507f6f39f7aSNikolay Borisov struct btrfs_block_group *btrfs_create_chunk(struct btrfs_trans_handle *trans,
50879bd3712SFilipe Manana 					    u64 type);
509c8bf1b67SDavid Sterba void btrfs_mapping_tree_free(struct extent_map_tree *tree);
51058efbc9fSOmar Sandoval blk_status_t btrfs_map_bio(struct btrfs_fs_info *fs_info, struct bio *bio,
51108635baeSChris Mason 			   int mirror_num);
5128a4b83ccSChris Mason int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
51397288f2cSChristoph Hellwig 		       fmode_t flags, void *holder);
51436350e95SGu Jinxiang struct btrfs_device *btrfs_scan_one_device(const char *path,
51536350e95SGu Jinxiang 					   fmode_t flags, void *holder);
51616cab91aSAnand Jain int btrfs_forget_devices(dev_t devt);
51754eed6aeSNikolay Borisov void btrfs_close_devices(struct btrfs_fs_devices *fs_devices);
518bacce86aSAnand Jain void btrfs_free_extra_devids(struct btrfs_fs_devices *fs_devices);
519d6507cf1SNikolay Borisov void btrfs_assign_next_active_device(struct btrfs_device *device,
520d6507cf1SNikolay Borisov 				     struct btrfs_device *this_dev);
521a27a94c2SNikolay Borisov struct btrfs_device *btrfs_find_device_by_devspec(struct btrfs_fs_info *fs_info,
522a27a94c2SNikolay Borisov 						  u64 devid,
523a27a94c2SNikolay Borisov 						  const char *devpath);
524faa775c4SJosef Bacik int btrfs_get_dev_args_from_path(struct btrfs_fs_info *fs_info,
525faa775c4SJosef Bacik 				 struct btrfs_dev_lookup_args *args,
526faa775c4SJosef Bacik 				 const char *path);
52712bd2fc0SIlya Dryomov struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
52812bd2fc0SIlya Dryomov 					const u64 *devid,
52912bd2fc0SIlya Dryomov 					const u8 *uuid);
530faa775c4SJosef Bacik void btrfs_put_dev_args_from_path(struct btrfs_dev_lookup_args *args);
531a425f9d4SDavid Sterba void btrfs_free_device(struct btrfs_device *device);
5322ff7e61eSJeff Mahoney int btrfs_rm_device(struct btrfs_fs_info *fs_info,
5331a15eb72SJosef Bacik 		    struct btrfs_dev_lookup_args *args,
5343fa421deSJosef Bacik 		    struct block_device **bdev, fmode_t *mode);
535ffc5a379SDavid Sterba void __exit btrfs_cleanup_fs_uuids(void);
5365d964051SStefan Behrens int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len);
5378f18cf13SChris Mason int btrfs_grow_device(struct btrfs_trans_handle *trans,
5388f18cf13SChris Mason 		      struct btrfs_device *device, u64 new_size);
539562d7b15SJosef Bacik struct btrfs_device *btrfs_find_device(const struct btrfs_fs_devices *fs_devices,
540562d7b15SJosef Bacik 				       const struct btrfs_dev_lookup_args *args);
5418f18cf13SChris Mason int btrfs_shrink_device(struct btrfs_device *device, u64 new_size);
542da353f6bSDavid Sterba int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *path);
5436fcf6e2bSDavid Sterba int btrfs_balance(struct btrfs_fs_info *fs_info,
5446fcf6e2bSDavid Sterba 		  struct btrfs_balance_control *bctl,
545c9e9f97bSIlya Dryomov 		  struct btrfs_ioctl_balance_args *bargs);
546f89e09cfSAnand Jain void btrfs_describe_block_groups(u64 flags, char *buf, u32 size_buf);
5472b6ba629SIlya Dryomov int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info);
54868310a5eSIlya Dryomov int btrfs_recover_balance(struct btrfs_fs_info *fs_info);
549837d5b6eSIlya Dryomov int btrfs_pause_balance(struct btrfs_fs_info *fs_info);
55018bb8bbfSJohannes Thumshirn int btrfs_relocate_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset);
551a7e99c69SIlya Dryomov int btrfs_cancel_balance(struct btrfs_fs_info *fs_info);
552f7a81ea4SStefan Behrens int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info);
55397f4dd09SNikolay Borisov int btrfs_uuid_scan_kthread(void *data);
554a09f23c3SAnand Jain bool btrfs_chunk_writeable(struct btrfs_fs_info *fs_info, u64 chunk_offset);
55560dfdf25SNikolay Borisov int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes,
556ba1bf481SJosef Bacik 			 u64 *start, u64 *max_avail);
557442a4f63SStefan Behrens void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index);
5582ff7e61eSJeff Mahoney int btrfs_get_dev_stats(struct btrfs_fs_info *fs_info,
559b27f7c0cSDavid Sterba 			struct btrfs_ioctl_get_dev_stats *stats);
560cb517eabSMiao Xie void btrfs_init_devices_late(struct btrfs_fs_info *fs_info);
561733f4fbbSStefan Behrens int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info);
562196c9d8dSDavid Sterba int btrfs_run_dev_stats(struct btrfs_trans_handle *trans);
56368a9db5fSNikolay Borisov void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_device *srcdev);
56465237ee3SDavid Sterba void btrfs_rm_dev_replace_free_srcdev(struct btrfs_device *srcdev);
5654f5ad7bdSNikolay Borisov void btrfs_destroy_dev_replace_tgtdev(struct btrfs_device *tgtdev);
566592d92eeSLiu Bo int btrfs_is_parity_mirror(struct btrfs_fs_info *fs_info,
567e4ff5fb5SNikolay Borisov 			   u64 logical, u64 len);
5682ff7e61eSJeff Mahoney unsigned long btrfs_full_stripe_len(struct btrfs_fs_info *fs_info,
56953b381b3SDavid Woodhouse 				    u64 logical);
57079bd3712SFilipe Manana int btrfs_chunk_alloc_add_chunk_item(struct btrfs_trans_handle *trans,
57179bd3712SFilipe Manana 				     struct btrfs_block_group *bg);
57297aff912SNikolay Borisov int btrfs_remove_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset);
57360ca842eSOmar Sandoval struct extent_map *btrfs_get_chunk_map(struct btrfs_fs_info *fs_info,
57460ca842eSOmar Sandoval 				       u64 logical, u64 length);
5758f32380dSJohannes Thumshirn void btrfs_release_disk_super(struct btrfs_super_block *super);
576addc3fa7SMiao Xie 
577442a4f63SStefan Behrens static inline void btrfs_dev_stat_inc(struct btrfs_device *dev,
578442a4f63SStefan Behrens 				      int index)
579442a4f63SStefan Behrens {
580442a4f63SStefan Behrens 	atomic_inc(dev->dev_stat_values + index);
5819deae968SNikolay Borisov 	/*
5829deae968SNikolay Borisov 	 * This memory barrier orders stores updating statistics before stores
5839deae968SNikolay Borisov 	 * updating dev_stats_ccnt.
5849deae968SNikolay Borisov 	 *
5859deae968SNikolay Borisov 	 * It pairs with smp_rmb() in btrfs_run_dev_stats().
5869deae968SNikolay Borisov 	 */
587addc3fa7SMiao Xie 	smp_mb__before_atomic();
588addc3fa7SMiao Xie 	atomic_inc(&dev->dev_stats_ccnt);
589442a4f63SStefan Behrens }
590442a4f63SStefan Behrens 
591442a4f63SStefan Behrens static inline int btrfs_dev_stat_read(struct btrfs_device *dev,
592442a4f63SStefan Behrens 				      int index)
593442a4f63SStefan Behrens {
594442a4f63SStefan Behrens 	return atomic_read(dev->dev_stat_values + index);
595442a4f63SStefan Behrens }
596442a4f63SStefan Behrens 
597442a4f63SStefan Behrens static inline int btrfs_dev_stat_read_and_reset(struct btrfs_device *dev,
598442a4f63SStefan Behrens 						int index)
599442a4f63SStefan Behrens {
600442a4f63SStefan Behrens 	int ret;
601442a4f63SStefan Behrens 
602442a4f63SStefan Behrens 	ret = atomic_xchg(dev->dev_stat_values + index, 0);
6034660c49fSNikolay Borisov 	/*
6044660c49fSNikolay Borisov 	 * atomic_xchg implies a full memory barriers as per atomic_t.txt:
6054660c49fSNikolay Borisov 	 * - RMW operations that have a return value are fully ordered;
6064660c49fSNikolay Borisov 	 *
6074660c49fSNikolay Borisov 	 * This implicit memory barriers is paired with the smp_rmb in
6084660c49fSNikolay Borisov 	 * btrfs_run_dev_stats
6094660c49fSNikolay Borisov 	 */
610addc3fa7SMiao Xie 	atomic_inc(&dev->dev_stats_ccnt);
611442a4f63SStefan Behrens 	return ret;
612442a4f63SStefan Behrens }
613442a4f63SStefan Behrens 
614442a4f63SStefan Behrens static inline void btrfs_dev_stat_set(struct btrfs_device *dev,
615442a4f63SStefan Behrens 				      int index, unsigned long val)
616442a4f63SStefan Behrens {
617442a4f63SStefan Behrens 	atomic_set(dev->dev_stat_values + index, val);
6189deae968SNikolay Borisov 	/*
6199deae968SNikolay Borisov 	 * This memory barrier orders stores updating statistics before stores
6209deae968SNikolay Borisov 	 * updating dev_stats_ccnt.
6219deae968SNikolay Borisov 	 *
6229deae968SNikolay Borisov 	 * It pairs with smp_rmb() in btrfs_run_dev_stats().
6239deae968SNikolay Borisov 	 */
624addc3fa7SMiao Xie 	smp_mb__before_atomic();
625addc3fa7SMiao Xie 	atomic_inc(&dev->dev_stats_ccnt);
626442a4f63SStefan Behrens }
627442a4f63SStefan Behrens 
628bbbf7243SNikolay Borisov void btrfs_commit_device_sizes(struct btrfs_transaction *trans);
62904216820SFilipe Manana 
6304143cb8bSDavid Sterba struct list_head * __attribute_const__ btrfs_get_fs_uuids(void);
6316528b99dSAnand Jain bool btrfs_check_rw_degradable(struct btrfs_fs_info *fs_info,
6326528b99dSAnand Jain 					struct btrfs_device *failing_dev);
633313b0858SJosef Bacik void btrfs_scratch_superblocks(struct btrfs_fs_info *fs_info,
634313b0858SJosef Bacik 			       struct block_device *bdev,
635313b0858SJosef Bacik 			       const char *device_path);
63621634a19SQu Wenruo 
637500a44c9SDavid Sterba enum btrfs_raid_types __attribute_const__ btrfs_bg_flags_to_raid_index(u64 flags);
63846df06b8SDavid Sterba int btrfs_bg_type_to_factor(u64 flags);
639158da513SDavid Sterba const char *btrfs_bg_type_to_raid_name(u64 flags);
640cf90d884SQu Wenruo int btrfs_verify_dev_extents(struct btrfs_fs_info *fs_info);
641554aed7dSJohannes Thumshirn bool btrfs_repair_one_zone(struct btrfs_fs_info *fs_info, u64 logical);
64246df06b8SDavid Sterba 
6430b86a832SChris Mason #endif
644