xref: /openbmc/linux/fs/btrfs/volumes.h (revision 917f32a2)
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 
20719fae89SQu Wenruo /* Used by sanity check for btrfs_raid_types. */
21719fae89SQu Wenruo #define const_ffs(n) (__builtin_ctzll(n) + 1)
22719fae89SQu Wenruo 
23719fae89SQu Wenruo /*
24719fae89SQu Wenruo  * The conversion from BTRFS_BLOCK_GROUP_* bits to btrfs_raid_type requires
25719fae89SQu Wenruo  * RAID0 always to be the lowest profile bit.
26719fae89SQu Wenruo  * Although it's part of on-disk format and should never change, do extra
27719fae89SQu Wenruo  * compile-time sanity checks.
28719fae89SQu Wenruo  */
29719fae89SQu Wenruo static_assert(const_ffs(BTRFS_BLOCK_GROUP_RAID0) <
30719fae89SQu Wenruo 	      const_ffs(BTRFS_BLOCK_GROUP_PROFILE_MASK & ~BTRFS_BLOCK_GROUP_RAID0));
31719fae89SQu Wenruo static_assert(const_ilog2(BTRFS_BLOCK_GROUP_RAID0) >
32719fae89SQu Wenruo 	      ilog2(BTRFS_BLOCK_GROUP_TYPE_MASK));
33719fae89SQu Wenruo 
34719fae89SQu Wenruo /* ilog2() can handle both constants and variables */
35719fae89SQu Wenruo #define BTRFS_BG_FLAG_TO_INDEX(profile)					\
36719fae89SQu Wenruo 	ilog2((profile) >> (ilog2(BTRFS_BLOCK_GROUP_RAID0) - 1))
37719fae89SQu Wenruo 
38f04fbcc6SQu Wenruo enum btrfs_raid_types {
39719fae89SQu Wenruo 	/* SINGLE is the special one as it doesn't have on-disk bit. */
40719fae89SQu Wenruo 	BTRFS_RAID_SINGLE  = 0,
41719fae89SQu Wenruo 
42719fae89SQu Wenruo 	BTRFS_RAID_RAID0   = BTRFS_BG_FLAG_TO_INDEX(BTRFS_BLOCK_GROUP_RAID0),
43719fae89SQu Wenruo 	BTRFS_RAID_RAID1   = BTRFS_BG_FLAG_TO_INDEX(BTRFS_BLOCK_GROUP_RAID1),
44719fae89SQu Wenruo 	BTRFS_RAID_DUP	   = BTRFS_BG_FLAG_TO_INDEX(BTRFS_BLOCK_GROUP_DUP),
45719fae89SQu Wenruo 	BTRFS_RAID_RAID10  = BTRFS_BG_FLAG_TO_INDEX(BTRFS_BLOCK_GROUP_RAID10),
46719fae89SQu Wenruo 	BTRFS_RAID_RAID5   = BTRFS_BG_FLAG_TO_INDEX(BTRFS_BLOCK_GROUP_RAID5),
47719fae89SQu Wenruo 	BTRFS_RAID_RAID6   = BTRFS_BG_FLAG_TO_INDEX(BTRFS_BLOCK_GROUP_RAID6),
48719fae89SQu Wenruo 	BTRFS_RAID_RAID1C3 = BTRFS_BG_FLAG_TO_INDEX(BTRFS_BLOCK_GROUP_RAID1C3),
49719fae89SQu Wenruo 	BTRFS_RAID_RAID1C4 = BTRFS_BG_FLAG_TO_INDEX(BTRFS_BLOCK_GROUP_RAID1C4),
50719fae89SQu Wenruo 
51f04fbcc6SQu Wenruo 	BTRFS_NR_RAID_TYPES
52f04fbcc6SQu Wenruo };
53f04fbcc6SQu Wenruo 
545f141126SNikolay Borisov struct btrfs_io_geometry {
555f141126SNikolay Borisov 	/* remaining bytes before crossing a stripe */
565f141126SNikolay Borisov 	u64 len;
575f141126SNikolay Borisov 	/* offset of logical address in chunk */
585f141126SNikolay Borisov 	u64 offset;
595f141126SNikolay Borisov 	/* length of single IO stripe */
60cc353a8bSQu Wenruo 	u32 stripe_len;
61cc353a8bSQu Wenruo 	/* offset of address in stripe */
62cc353a8bSQu Wenruo 	u32 stripe_offset;
635f141126SNikolay Borisov 	/* number of stripe where address falls */
645f141126SNikolay Borisov 	u64 stripe_nr;
655f141126SNikolay Borisov 	/* offset of raid56 stripe into the chunk */
665f141126SNikolay Borisov 	u64 raid56_stripe_offset;
675f141126SNikolay Borisov };
685f141126SNikolay Borisov 
697cc8e58dSMiao Xie /*
707cc8e58dSMiao Xie  * Use sequence counter to get consistent device stat data on
717cc8e58dSMiao Xie  * 32-bit processors.
727cc8e58dSMiao Xie  */
737cc8e58dSMiao Xie #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
747cc8e58dSMiao Xie #include <linux/seqlock.h>
757cc8e58dSMiao Xie #define __BTRFS_NEED_DEVICE_DATA_ORDERED
76c41ec452SSu Yue #define btrfs_device_data_ordered_init(device)	\
77c41ec452SSu Yue 	seqcount_init(&device->data_seqcount)
787cc8e58dSMiao Xie #else
79c41ec452SSu Yue #define btrfs_device_data_ordered_init(device) do { } while (0)
807cc8e58dSMiao Xie #endif
817cc8e58dSMiao Xie 
82ebbede42SAnand Jain #define BTRFS_DEV_STATE_WRITEABLE	(0)
83e12c9621SAnand Jain #define BTRFS_DEV_STATE_IN_FS_METADATA	(1)
84e6e674bdSAnand Jain #define BTRFS_DEV_STATE_MISSING		(2)
85401e29c1SAnand Jain #define BTRFS_DEV_STATE_REPLACE_TGT	(3)
861c3063b6SAnand Jain #define BTRFS_DEV_STATE_FLUSH_SENT	(4)
8766d204a1SFilipe Manana #define BTRFS_DEV_STATE_NO_READA	(5)
88ebbede42SAnand Jain 
895b316468SNaohiro Aota struct btrfs_zoned_device_info;
905b316468SNaohiro Aota 
910b86a832SChris Mason struct btrfs_device {
920b6f5d40SNikolay Borisov 	struct list_head dev_list; /* device_list_mutex */
930b6f5d40SNikolay Borisov 	struct list_head dev_alloc_list; /* chunk mutex */
94bbbf7243SNikolay Borisov 	struct list_head post_commit_list; /* chunk mutex */
952b82032cSYan Zheng 	struct btrfs_fs_devices *fs_devices;
96fb456252SJeff Mahoney 	struct btrfs_fs_info *fs_info;
97ffbd517dSChris Mason 
988d1a7aaeSMadhuparna Bhowmik 	struct rcu_string __rcu *name;
99d5ee37bcSMiao Xie 
100d5ee37bcSMiao Xie 	u64 generation;
101d5ee37bcSMiao Xie 
102d5ee37bcSMiao Xie 	struct block_device *bdev;
103d5ee37bcSMiao Xie 
1045b316468SNaohiro Aota 	struct btrfs_zoned_device_info *zone_info;
1055b316468SNaohiro Aota 
106d5ee37bcSMiao Xie 	/* the mode sent to blkdev_get */
107d5ee37bcSMiao Xie 	fmode_t mode;
108d5ee37bcSMiao Xie 
1094889bc05SAnand Jain 	/*
1104889bc05SAnand Jain 	 * Device's major-minor number. Must be set even if the device is not
1114889bc05SAnand Jain 	 * opened (bdev == NULL), unless the device is missing.
1124889bc05SAnand Jain 	 */
1134889bc05SAnand Jain 	dev_t devt;
114ebbede42SAnand Jain 	unsigned long dev_state;
11558efbc9fSOmar Sandoval 	blk_status_t last_flush_error;
116b3075717SChris Mason 
1177cc8e58dSMiao Xie #ifdef __BTRFS_NEED_DEVICE_DATA_ORDERED
118c41ec452SSu Yue 	seqcount_t data_seqcount;
1197cc8e58dSMiao Xie #endif
1207cc8e58dSMiao Xie 
1210b86a832SChris Mason 	/* the internal btrfs device id */
1220b86a832SChris Mason 	u64 devid;
1230b86a832SChris Mason 
1246ba40b61SMiao Xie 	/* size of the device in memory */
1250b86a832SChris Mason 	u64 total_bytes;
1260b86a832SChris Mason 
1276ba40b61SMiao Xie 	/* size of the device on disk */
128d6397baeSChris Ball 	u64 disk_total_bytes;
129d6397baeSChris Ball 
1300b86a832SChris Mason 	/* bytes used */
1310b86a832SChris Mason 	u64 bytes_used;
1320b86a832SChris Mason 
1330b86a832SChris Mason 	/* optimal io alignment for this device */
1340b86a832SChris Mason 	u32 io_align;
1350b86a832SChris Mason 
1360b86a832SChris Mason 	/* optimal io width for this device */
1370b86a832SChris Mason 	u32 io_width;
1383c45bfc1SDulshani Gunawardhana 	/* type and info about this device */
1393c45bfc1SDulshani Gunawardhana 	u64 type;
1400b86a832SChris Mason 
1410b86a832SChris Mason 	/* minimal io size for this device */
1420b86a832SChris Mason 	u32 sector_size;
1430b86a832SChris Mason 
1440b86a832SChris Mason 	/* physical drive uuid (or lvm uuid) */
145e17cade2SChris Mason 	u8 uuid[BTRFS_UUID_SIZE];
1468b712842SChris Mason 
147935e5cc9SMiao Xie 	/*
148935e5cc9SMiao Xie 	 * size of the device on the current transaction
149935e5cc9SMiao Xie 	 *
150935e5cc9SMiao Xie 	 * This variant is update when committing the transaction,
151bbbf7243SNikolay Borisov 	 * and protected by chunk mutex
152935e5cc9SMiao Xie 	 */
153935e5cc9SMiao Xie 	u64 commit_total_bytes;
154935e5cc9SMiao Xie 
155ce7213c7SMiao Xie 	/* bytes used on the current transaction */
156ce7213c7SMiao Xie 	u64 commit_bytes_used;
157935e5cc9SMiao Xie 
158f9e69aa9SChristoph Hellwig 	/* Bio used for flushing device barriers */
159f9e69aa9SChristoph Hellwig 	struct bio flush_bio;
1603c45bfc1SDulshani Gunawardhana 	struct completion flush_wait;
1613c45bfc1SDulshani Gunawardhana 
162a2de733cSArne Jansen 	/* per-device scrub information */
163cadbc0a0SAnand Jain 	struct scrub_ctx *scrub_ctx;
164a2de733cSArne Jansen 
165442a4f63SStefan Behrens 	/* disk I/O failure stats. For detailed description refer to
166442a4f63SStefan Behrens 	 * enum btrfs_dev_stat_values in ioctl.h */
167733f4fbbSStefan Behrens 	int dev_stats_valid;
168addc3fa7SMiao Xie 
169addc3fa7SMiao Xie 	/* Counter to record the change of device stats */
170addc3fa7SMiao Xie 	atomic_t dev_stats_ccnt;
171442a4f63SStefan Behrens 	atomic_t dev_stat_values[BTRFS_DEV_STAT_VALUES_MAX];
1721c11b63eSJeff Mahoney 
1731c11b63eSJeff Mahoney 	struct extent_io_tree alloc_state;
174668e48afSAnand Jain 
175668e48afSAnand Jain 	struct completion kobj_unregister;
176668e48afSAnand Jain 	/* For sysfs/FSID/devinfo/devid/ */
177668e48afSAnand Jain 	struct kobject devid_kobj;
178eb3b5053SDavid Sterba 
179eb3b5053SDavid Sterba 	/* Bandwidth limit for scrub, in bytes */
180eb3b5053SDavid Sterba 	u64 scrub_speed_max;
1810b86a832SChris Mason };
1820b86a832SChris Mason 
1837cc8e58dSMiao Xie /*
1847cc8e58dSMiao Xie  * If we read those variants at the context of their own lock, we needn't
1857cc8e58dSMiao Xie  * use the following helpers, reading them directly is safe.
1867cc8e58dSMiao Xie  */
1877cc8e58dSMiao Xie #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
1887cc8e58dSMiao Xie #define BTRFS_DEVICE_GETSET_FUNCS(name)					\
1897cc8e58dSMiao Xie static inline u64							\
1907cc8e58dSMiao Xie btrfs_device_get_##name(const struct btrfs_device *dev)			\
1917cc8e58dSMiao Xie {									\
1927cc8e58dSMiao Xie 	u64 size;							\
1937cc8e58dSMiao Xie 	unsigned int seq;						\
1947cc8e58dSMiao Xie 									\
1957cc8e58dSMiao Xie 	do {								\
1967cc8e58dSMiao Xie 		seq = read_seqcount_begin(&dev->data_seqcount);		\
1977cc8e58dSMiao Xie 		size = dev->name;					\
1987cc8e58dSMiao Xie 	} while (read_seqcount_retry(&dev->data_seqcount, seq));	\
1997cc8e58dSMiao Xie 	return size;							\
2007cc8e58dSMiao Xie }									\
2017cc8e58dSMiao Xie 									\
2027cc8e58dSMiao Xie static inline void							\
2037cc8e58dSMiao Xie btrfs_device_set_##name(struct btrfs_device *dev, u64 size)		\
2047cc8e58dSMiao Xie {									\
205c41ec452SSu Yue 	preempt_disable();						\
2067cc8e58dSMiao Xie 	write_seqcount_begin(&dev->data_seqcount);			\
2077cc8e58dSMiao Xie 	dev->name = size;						\
2087cc8e58dSMiao Xie 	write_seqcount_end(&dev->data_seqcount);			\
209c41ec452SSu Yue 	preempt_enable();						\
2107cc8e58dSMiao Xie }
21194545870SThomas Gleixner #elif BITS_PER_LONG==32 && defined(CONFIG_PREEMPTION)
2127cc8e58dSMiao Xie #define BTRFS_DEVICE_GETSET_FUNCS(name)					\
2137cc8e58dSMiao Xie static inline u64							\
2147cc8e58dSMiao Xie btrfs_device_get_##name(const struct btrfs_device *dev)			\
2157cc8e58dSMiao Xie {									\
2167cc8e58dSMiao Xie 	u64 size;							\
2177cc8e58dSMiao Xie 									\
2187cc8e58dSMiao Xie 	preempt_disable();						\
2197cc8e58dSMiao Xie 	size = dev->name;						\
2207cc8e58dSMiao Xie 	preempt_enable();						\
2217cc8e58dSMiao Xie 	return size;							\
2227cc8e58dSMiao Xie }									\
2237cc8e58dSMiao Xie 									\
2247cc8e58dSMiao Xie static inline void							\
2257cc8e58dSMiao Xie btrfs_device_set_##name(struct btrfs_device *dev, u64 size)		\
2267cc8e58dSMiao Xie {									\
2277cc8e58dSMiao Xie 	preempt_disable();						\
2287cc8e58dSMiao Xie 	dev->name = size;						\
2297cc8e58dSMiao Xie 	preempt_enable();						\
2307cc8e58dSMiao Xie }
2317cc8e58dSMiao Xie #else
2327cc8e58dSMiao Xie #define BTRFS_DEVICE_GETSET_FUNCS(name)					\
2337cc8e58dSMiao Xie static inline u64							\
2347cc8e58dSMiao Xie btrfs_device_get_##name(const struct btrfs_device *dev)			\
2357cc8e58dSMiao Xie {									\
2367cc8e58dSMiao Xie 	return dev->name;						\
2377cc8e58dSMiao Xie }									\
2387cc8e58dSMiao Xie 									\
2397cc8e58dSMiao Xie static inline void							\
2407cc8e58dSMiao Xie btrfs_device_set_##name(struct btrfs_device *dev, u64 size)		\
2417cc8e58dSMiao Xie {									\
2427cc8e58dSMiao Xie 	dev->name = size;						\
2437cc8e58dSMiao Xie }
2447cc8e58dSMiao Xie #endif
2457cc8e58dSMiao Xie 
2467cc8e58dSMiao Xie BTRFS_DEVICE_GETSET_FUNCS(total_bytes);
2477cc8e58dSMiao Xie BTRFS_DEVICE_GETSET_FUNCS(disk_total_bytes);
2487cc8e58dSMiao Xie BTRFS_DEVICE_GETSET_FUNCS(bytes_used);
2497cc8e58dSMiao Xie 
250c4a816c6SNaohiro Aota enum btrfs_chunk_allocation_policy {
251c4a816c6SNaohiro Aota 	BTRFS_CHUNK_ALLOC_REGULAR,
2521cd6121fSNaohiro Aota 	BTRFS_CHUNK_ALLOC_ZONED,
253c4a816c6SNaohiro Aota };
254c4a816c6SNaohiro Aota 
25533fd2f71SAnand Jain /*
25633fd2f71SAnand Jain  * Read policies for mirrored block group profiles, read picks the stripe based
25733fd2f71SAnand Jain  * on these policies.
25833fd2f71SAnand Jain  */
25933fd2f71SAnand Jain enum btrfs_read_policy {
26033fd2f71SAnand Jain 	/* Use process PID to choose the stripe */
26133fd2f71SAnand Jain 	BTRFS_READ_POLICY_PID,
26233fd2f71SAnand Jain 	BTRFS_NR_READ_POLICY,
26333fd2f71SAnand Jain };
26433fd2f71SAnand Jain 
2658a4b83ccSChris Mason struct btrfs_fs_devices {
2668a4b83ccSChris Mason 	u8 fsid[BTRFS_FSID_SIZE]; /* FS specific uuid */
2677239ff4bSNikolay Borisov 	u8 metadata_uuid[BTRFS_FSID_SIZE];
268d1a63002SNikolay Borisov 	bool fsid_change;
269c4babc5eSAnand Jain 	struct list_head fs_list;
2708a4b83ccSChris Mason 
271add9745aSAnand Jain 	/*
272add9745aSAnand Jain 	 * Number of devices under this fsid including missing and
273add9745aSAnand Jain 	 * replace-target device and excludes seed devices.
274add9745aSAnand Jain 	 */
2758a4b83ccSChris Mason 	u64 num_devices;
276add9745aSAnand Jain 
277add9745aSAnand Jain 	/*
278add9745aSAnand Jain 	 * The number of devices that successfully opened, including
279add9745aSAnand Jain 	 * replace-target, excludes seed devices.
280add9745aSAnand Jain 	 */
281a0af469bSChris Mason 	u64 open_devices;
282add9745aSAnand Jain 
283add9745aSAnand Jain 	/* The number of devices that are under the chunk allocation list. */
2842b82032cSYan Zheng 	u64 rw_devices;
285add9745aSAnand Jain 
286add9745aSAnand Jain 	/* Count of missing devices under this fsid excluding seed device. */
287cd02dca5SChris Mason 	u64 missing_devices;
2882b82032cSYan Zheng 	u64 total_rw_bytes;
289add9745aSAnand Jain 
290add9745aSAnand Jain 	/*
291add9745aSAnand Jain 	 * Count of devices from btrfs_super_block::num_devices for this fsid,
292add9745aSAnand Jain 	 * which includes the seed device, excludes the transient replace-target
293add9745aSAnand Jain 	 * device.
294add9745aSAnand Jain 	 */
29502db0844SJosef Bacik 	u64 total_devices;
296d1a63002SNikolay Borisov 
297d1a63002SNikolay Borisov 	/* Highest generation number of seen devices */
298d1a63002SNikolay Borisov 	u64 latest_generation;
299d1a63002SNikolay Borisov 
300d24fa5c1SAnand Jain 	/*
301d24fa5c1SAnand Jain 	 * The mount device or a device with highest generation after removal
302d24fa5c1SAnand Jain 	 * or replace.
303d24fa5c1SAnand Jain 	 */
304d24fa5c1SAnand Jain 	struct btrfs_device *latest_dev;
305e5e9a520SChris Mason 
306e5e9a520SChris Mason 	/* all of the devices in the FS, protected by a mutex
307e5e9a520SChris Mason 	 * so we can safely walk it to write out the supers without
3089b011adfSWang Shilong 	 * worrying about add/remove by the multi-device code.
3099b011adfSWang Shilong 	 * Scrubbing super can kick off supers writing by holding
3109b011adfSWang Shilong 	 * this mutex lock.
311e5e9a520SChris Mason 	 */
312e5e9a520SChris Mason 	struct mutex device_list_mutex;
3130b6f5d40SNikolay Borisov 
3140b6f5d40SNikolay Borisov 	/* List of all devices, protected by device_list_mutex */
3158a4b83ccSChris Mason 	struct list_head devices;
316b3075717SChris Mason 
3170b6f5d40SNikolay Borisov 	/*
3180b6f5d40SNikolay Borisov 	 * Devices which can satisfy space allocation. Protected by
3190b6f5d40SNikolay Borisov 	 * chunk_mutex
3200b6f5d40SNikolay Borisov 	 */
321b3075717SChris Mason 	struct list_head alloc_list;
3222b82032cSYan Zheng 
323944d3f9fSNikolay Borisov 	struct list_head seed_list;
3240395d84fSJohannes Thumshirn 	bool seeding;
3252b82032cSYan Zheng 
3262b82032cSYan Zheng 	int opened;
327c289811cSChris Mason 
328c289811cSChris Mason 	/* set when we find or add a device that doesn't have the
329c289811cSChris Mason 	 * nonrot flag set
330c289811cSChris Mason 	 */
3317f0432d0SJohannes Thumshirn 	bool rotating;
3322e7910d6SAnand Jain 
3335a13f430SAnand Jain 	struct btrfs_fs_info *fs_info;
3342e7910d6SAnand Jain 	/* sysfs kobjects */
335c1b7e474SAnand Jain 	struct kobject fsid_kobj;
336b5501504SAnand Jain 	struct kobject *devices_kobj;
337a013d141SAnand Jain 	struct kobject *devinfo_kobj;
3382e7910d6SAnand Jain 	struct completion kobj_unregister;
339c4a816c6SNaohiro Aota 
340c4a816c6SNaohiro Aota 	enum btrfs_chunk_allocation_policy chunk_alloc_policy;
34133fd2f71SAnand Jain 
34233fd2f71SAnand Jain 	/* Policy used to read the mirrored stripes */
34333fd2f71SAnand Jain 	enum btrfs_read_policy read_policy;
3448a4b83ccSChris Mason };
3458a4b83ccSChris Mason 
346facc8a22SMiao Xie #define BTRFS_BIO_INLINE_CSUM_SIZE	64
347facc8a22SMiao Xie 
348ab4ba2e1SQu Wenruo #define BTRFS_MAX_DEVS(info) ((BTRFS_MAX_ITEM_SIZE(info)	\
349ab4ba2e1SQu Wenruo 			- sizeof(struct btrfs_chunk))		\
350ab4ba2e1SQu Wenruo 			/ sizeof(struct btrfs_stripe) + 1)
351ab4ba2e1SQu Wenruo 
352ab4ba2e1SQu Wenruo #define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE	\
353ab4ba2e1SQu Wenruo 				- 2 * sizeof(struct btrfs_disk_key)	\
354ab4ba2e1SQu Wenruo 				- 2 * sizeof(struct btrfs_chunk))	\
355ab4ba2e1SQu Wenruo 				/ sizeof(struct btrfs_stripe) + 1)
356ab4ba2e1SQu Wenruo 
3579be3395bSChris Mason /*
358ee5b46a3SChristoph Hellwig  * Maximum number of sectors for a single bio to limit the size of the
359ee5b46a3SChristoph Hellwig  * checksum array.  This matches the number of bio_vecs per bio and thus the
360ee5b46a3SChristoph Hellwig  * I/O size for buffered I/O.
361ee5b46a3SChristoph Hellwig  */
362ee5b46a3SChristoph Hellwig #define BTRFS_MAX_BIO_SECTORS				(256)
363ee5b46a3SChristoph Hellwig 
364*917f32a2SChristoph Hellwig typedef void (*btrfs_bio_end_io_t)(struct btrfs_bio *bbio);
365*917f32a2SChristoph Hellwig 
366ee5b46a3SChristoph Hellwig /*
367c3a3b19bSQu Wenruo  * Additional info to pass along bio.
368c3a3b19bSQu Wenruo  *
369c3a3b19bSQu Wenruo  * Mostly for btrfs specific features like csum and mirror_num.
3709be3395bSChris Mason  */
371c3a3b19bSQu Wenruo struct btrfs_bio {
372c1dc0896SMiao Xie 	unsigned int mirror_num;
373c3a3b19bSQu Wenruo 
37400d82525SChristoph Hellwig 	/* for direct I/O */
37500d82525SChristoph Hellwig 	u64 file_offset;
37600d82525SChristoph Hellwig 
377c3a3b19bSQu Wenruo 	/* @device is for stripe IO submission. */
378c31efbdfSNikolay Borisov 	struct btrfs_device *device;
379facc8a22SMiao Xie 	u8 *csum;
380facc8a22SMiao Xie 	u8 csum_inline[BTRFS_BIO_INLINE_CSUM_SIZE];
38117347cecSLiu Bo 	struct bvec_iter iter;
382c3a3b19bSQu Wenruo 
383*917f32a2SChristoph Hellwig 	/* End I/O information supplied to btrfs_bio_alloc */
384*917f32a2SChristoph Hellwig 	btrfs_bio_end_io_t end_io;
385*917f32a2SChristoph Hellwig 	void *private;
386*917f32a2SChristoph Hellwig 
387d7b9416fSChristoph Hellwig 	/* For read end I/O handling */
388d7b9416fSChristoph Hellwig 	struct work_struct end_io_work;
389d7b9416fSChristoph Hellwig 
390fa1bcbe0SDavid Sterba 	/*
391fa1bcbe0SDavid Sterba 	 * This member must come last, bio_alloc_bioset will allocate enough
392c3a3b19bSQu Wenruo 	 * bytes for entire btrfs_bio but relies on bio being last.
393fa1bcbe0SDavid Sterba 	 */
3949be3395bSChris Mason 	struct bio bio;
3959be3395bSChris Mason };
3969be3395bSChris Mason 
397c3a3b19bSQu Wenruo static inline struct btrfs_bio *btrfs_bio(struct bio *bio)
3989be3395bSChris Mason {
399c3a3b19bSQu Wenruo 	return container_of(bio, struct btrfs_bio, bio);
4009be3395bSChris Mason }
4019be3395bSChris Mason 
402d45cfb88SChristoph Hellwig int __init btrfs_bioset_init(void);
403d45cfb88SChristoph Hellwig void __cold btrfs_bioset_exit(void);
404d45cfb88SChristoph Hellwig 
405*917f32a2SChristoph Hellwig struct bio *btrfs_bio_alloc(unsigned int nr_vecs, blk_opf_t opf,
406*917f32a2SChristoph Hellwig 			    btrfs_bio_end_io_t end_io, void *private);
407*917f32a2SChristoph Hellwig struct bio *btrfs_bio_clone_partial(struct bio *orig, u64 offset, u64 size,
408*917f32a2SChristoph Hellwig 				    btrfs_bio_end_io_t end_io, void *private);
409*917f32a2SChristoph Hellwig 
410*917f32a2SChristoph Hellwig static inline void btrfs_bio_end_io(struct btrfs_bio *bbio, blk_status_t status)
411*917f32a2SChristoph Hellwig {
412*917f32a2SChristoph Hellwig 	bbio->bio.bi_status = status;
413*917f32a2SChristoph Hellwig 	bbio->end_io(bbio);
414*917f32a2SChristoph Hellwig }
415d45cfb88SChristoph Hellwig 
416c3a3b19bSQu Wenruo static inline void btrfs_bio_free_csum(struct btrfs_bio *bbio)
417b3a0dd50SDavid Sterba {
418c3a3b19bSQu Wenruo 	if (bbio->csum != bbio->csum_inline) {
419c3a3b19bSQu Wenruo 		kfree(bbio->csum);
420c3a3b19bSQu Wenruo 		bbio->csum = NULL;
421b3a0dd50SDavid Sterba 	}
422b3a0dd50SDavid Sterba }
423b3a0dd50SDavid Sterba 
424261d812bSQu Wenruo /*
425261d812bSQu Wenruo  * Iterate through a btrfs_bio (@bbio) on a per-sector basis.
426261d812bSQu Wenruo  *
427261d812bSQu Wenruo  * bvl        - struct bio_vec
428261d812bSQu Wenruo  * bbio       - struct btrfs_bio
429261d812bSQu Wenruo  * iters      - struct bvec_iter
430261d812bSQu Wenruo  * bio_offset - unsigned int
431261d812bSQu Wenruo  */
432261d812bSQu Wenruo #define btrfs_bio_for_each_sector(fs_info, bvl, bbio, iter, bio_offset)	\
433261d812bSQu Wenruo 	for ((iter) = (bbio)->iter, (bio_offset) = 0;			\
434261d812bSQu Wenruo 	     (iter).bi_size &&					\
435261d812bSQu Wenruo 	     (((bvl) = bio_iter_iovec((&(bbio)->bio), (iter))), 1);	\
436261d812bSQu Wenruo 	     (bio_offset) += fs_info->sectorsize,			\
437261d812bSQu Wenruo 	     bio_advance_iter_single(&(bbio)->bio, &(iter),		\
438261d812bSQu Wenruo 	     (fs_info)->sectorsize))
439261d812bSQu Wenruo 
4404c664611SQu Wenruo struct btrfs_io_stripe {
441cea9e445SChris Mason 	struct btrfs_device *dev;
4429ff7ddd3SChristoph Hellwig 	union {
4439ff7ddd3SChristoph Hellwig 		/* Block mapping */
444cea9e445SChris Mason 		u64 physical;
4459ff7ddd3SChristoph Hellwig 		/* For the endio handler */
4469ff7ddd3SChristoph Hellwig 		struct btrfs_io_context *bioc;
4479ff7ddd3SChristoph Hellwig 	};
448a4012f06SChristoph Hellwig };
449a4012f06SChristoph Hellwig 
450a4012f06SChristoph Hellwig struct btrfs_discard_stripe {
451a4012f06SChristoph Hellwig 	struct btrfs_device *dev;
452a4012f06SChristoph Hellwig 	u64 physical;
453a4012f06SChristoph Hellwig 	u64 length;
454cea9e445SChris Mason };
455cea9e445SChris Mason 
4564c664611SQu Wenruo /*
4574c664611SQu Wenruo  * Context for IO subsmission for device stripe.
4584c664611SQu Wenruo  *
4594c664611SQu Wenruo  * - Track the unfinished mirrors for mirror based profiles
4604c664611SQu Wenruo  *   Mirror based profiles are SINGLE/DUP/RAID1/RAID10.
4614c664611SQu Wenruo  *
4624c664611SQu Wenruo  * - Contain the logical -> physical mapping info
4634c664611SQu Wenruo  *   Used by submit_stripe_bio() for mapping logical bio
4644c664611SQu Wenruo  *   into physical device address.
4654c664611SQu Wenruo  *
4664c664611SQu Wenruo  * - Contain device replace info
4674c664611SQu Wenruo  *   Used by handle_ops_on_dev_replace() to copy logical bios
4684c664611SQu Wenruo  *   into the new device.
4694c664611SQu Wenruo  *
4704c664611SQu Wenruo  * - Contain RAID56 full stripe logical bytenrs
4714c664611SQu Wenruo  */
4724c664611SQu Wenruo struct btrfs_io_context {
473140475aeSElena Reshetova 	refcount_t refs;
474c404e0dcSMiao Xie 	struct btrfs_fs_info *fs_info;
47510f11900SZhao Lei 	u64 map_type; /* get from map_lookup->type */
4767d2b4daaSChris Mason 	struct bio *orig_bio;
477a236aed1SChris Mason 	atomic_t error;
478a236aed1SChris Mason 	int max_errors;
479cea9e445SChris Mason 	int num_stripes;
480a1d3c478SJan Schmidt 	int mirror_num;
4812c8cdd6eSMiao Xie 	int num_tgtdevs;
4822c8cdd6eSMiao Xie 	int *tgtdev_map;
4838e5cfb55SZhao Lei 	/*
4848e5cfb55SZhao Lei 	 * logical block numbers for the start of each stripe
4858e5cfb55SZhao Lei 	 * The last one or two are p/q.  These are sorted,
4868e5cfb55SZhao Lei 	 * so raid_map[0] is the start of our full stripe
4878e5cfb55SZhao Lei 	 */
4888e5cfb55SZhao Lei 	u64 *raid_map;
4894c664611SQu Wenruo 	struct btrfs_io_stripe stripes[];
490cea9e445SChris Mason };
491cea9e445SChris Mason 
492b2117a39SMiao Xie struct btrfs_device_info {
493b2117a39SMiao Xie 	struct btrfs_device *dev;
494b2117a39SMiao Xie 	u64 dev_offset;
495b2117a39SMiao Xie 	u64 max_avail;
49673c5de00SArne Jansen 	u64 total_avail;
497b2117a39SMiao Xie };
498b2117a39SMiao Xie 
49931e50229SLiu Bo struct btrfs_raid_attr {
5008c3e3582SDavid Sterba 	u8 sub_stripes;		/* sub_stripes info for map */
5018c3e3582SDavid Sterba 	u8 dev_stripes;		/* stripes per dev */
5028c3e3582SDavid Sterba 	u8 devs_max;		/* max devs to use */
5038c3e3582SDavid Sterba 	u8 devs_min;		/* min devs needed */
5048c3e3582SDavid Sterba 	u8 tolerated_failures;	/* max tolerated fail devs */
5058c3e3582SDavid Sterba 	u8 devs_increment;	/* ndevs has to be a multiple of this */
5068c3e3582SDavid Sterba 	u8 ncopies;		/* how many copies to data has */
5078c3e3582SDavid Sterba 	u8 nparity;		/* number of stripes worth of bytes to store
508b50836edSHans van Kranenburg 				 * parity information */
5098c3e3582SDavid Sterba 	u8 mindev_error;	/* error code if min devs requisite is unmet */
510ed23467bSAnand Jain 	const char raid_name[8]; /* name of the raid */
51141a6e891SAnand Jain 	u64 bg_flag;		/* block group flag of the raid */
51231e50229SLiu Bo };
51331e50229SLiu Bo 
514af902047SZhao Lei extern const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES];
515af902047SZhao Lei 
5161abe9b8aSliubo struct map_lookup {
5171abe9b8aSliubo 	u64 type;
5181abe9b8aSliubo 	int io_align;
5191abe9b8aSliubo 	int io_width;
520cc353a8bSQu Wenruo 	u32 stripe_len;
5211abe9b8aSliubo 	int num_stripes;
5221abe9b8aSliubo 	int sub_stripes;
523cf90d884SQu Wenruo 	int verified_stripes; /* For mount time dev extent verification */
5244c664611SQu Wenruo 	struct btrfs_io_stripe stripes[];
5251abe9b8aSliubo };
5261abe9b8aSliubo 
527a2de733cSArne Jansen #define map_lookup_size(n) (sizeof(struct map_lookup) + \
5284c664611SQu Wenruo 			    (sizeof(struct btrfs_io_stripe) * (n)))
529a2de733cSArne Jansen 
530c9e9f97bSIlya Dryomov struct btrfs_balance_args;
53119a39dceSIlya Dryomov struct btrfs_balance_progress;
532c9e9f97bSIlya Dryomov struct btrfs_balance_control {
533c9e9f97bSIlya Dryomov 	struct btrfs_balance_args data;
534c9e9f97bSIlya Dryomov 	struct btrfs_balance_args meta;
535c9e9f97bSIlya Dryomov 	struct btrfs_balance_args sys;
536c9e9f97bSIlya Dryomov 
537c9e9f97bSIlya Dryomov 	u64 flags;
53819a39dceSIlya Dryomov 
53919a39dceSIlya Dryomov 	struct btrfs_balance_progress stat;
540c9e9f97bSIlya Dryomov };
541c9e9f97bSIlya Dryomov 
542562d7b15SJosef Bacik /*
543562d7b15SJosef Bacik  * Search for a given device by the set parameters
544562d7b15SJosef Bacik  */
545562d7b15SJosef Bacik struct btrfs_dev_lookup_args {
546562d7b15SJosef Bacik 	u64 devid;
547562d7b15SJosef Bacik 	u8 *uuid;
548562d7b15SJosef Bacik 	u8 *fsid;
549562d7b15SJosef Bacik 	bool missing;
550562d7b15SJosef Bacik };
551562d7b15SJosef Bacik 
552562d7b15SJosef Bacik /* We have to initialize to -1 because BTRFS_DEV_REPLACE_DEVID is 0 */
553562d7b15SJosef Bacik #define BTRFS_DEV_LOOKUP_ARGS_INIT { .devid = (u64)-1 }
554562d7b15SJosef Bacik 
555562d7b15SJosef Bacik #define BTRFS_DEV_LOOKUP_ARGS(name) \
556562d7b15SJosef Bacik 	struct btrfs_dev_lookup_args name = BTRFS_DEV_LOOKUP_ARGS_INIT
557562d7b15SJosef Bacik 
558cf8cddd3SChristoph Hellwig enum btrfs_map_op {
559cf8cddd3SChristoph Hellwig 	BTRFS_MAP_READ,
560cf8cddd3SChristoph Hellwig 	BTRFS_MAP_WRITE,
561cf8cddd3SChristoph Hellwig 	BTRFS_MAP_DISCARD,
562cf8cddd3SChristoph Hellwig 	BTRFS_MAP_GET_READ_MIRRORS,
563cf8cddd3SChristoph Hellwig };
564cf8cddd3SChristoph Hellwig 
565cf8cddd3SChristoph Hellwig static inline enum btrfs_map_op btrfs_op(struct bio *bio)
566cf8cddd3SChristoph Hellwig {
567cf8cddd3SChristoph Hellwig 	switch (bio_op(bio)) {
568cf8cddd3SChristoph Hellwig 	case REQ_OP_DISCARD:
569cf8cddd3SChristoph Hellwig 		return BTRFS_MAP_DISCARD;
570cf8cddd3SChristoph Hellwig 	case REQ_OP_WRITE:
571cfe94440SNaohiro Aota 	case REQ_OP_ZONE_APPEND:
572cf8cddd3SChristoph Hellwig 		return BTRFS_MAP_WRITE;
573cf8cddd3SChristoph Hellwig 	default:
574cf8cddd3SChristoph Hellwig 		WARN_ON_ONCE(1);
575c730ae0cSMarcos Paulo de Souza 		fallthrough;
576cf8cddd3SChristoph Hellwig 	case REQ_OP_READ:
577cf8cddd3SChristoph Hellwig 		return BTRFS_MAP_READ;
578cf8cddd3SChristoph Hellwig 	}
579cf8cddd3SChristoph Hellwig }
580cf8cddd3SChristoph Hellwig 
5814c664611SQu Wenruo void btrfs_get_bioc(struct btrfs_io_context *bioc);
5824c664611SQu Wenruo void btrfs_put_bioc(struct btrfs_io_context *bioc);
583cf8cddd3SChristoph Hellwig int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
584cea9e445SChris Mason 		    u64 logical, u64 *length,
5854c664611SQu Wenruo 		    struct btrfs_io_context **bioc_ret, int mirror_num);
586cf8cddd3SChristoph Hellwig int btrfs_map_sblock(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
587af8e2d1dSMiao Xie 		     u64 logical, u64 *length,
5884c664611SQu Wenruo 		     struct btrfs_io_context **bioc_ret);
589a4012f06SChristoph Hellwig struct btrfs_discard_stripe *btrfs_map_discard(struct btrfs_fs_info *fs_info,
590a4012f06SChristoph Hellwig 					       u64 logical, u64 *length_ret,
591a4012f06SChristoph Hellwig 					       u32 *num_stripes);
59242034313SMichal Rostecki int btrfs_get_io_geometry(struct btrfs_fs_info *fs_info, struct extent_map *map,
59343c0d1a5SQu Wenruo 			  enum btrfs_map_op op, u64 logical,
59442034313SMichal Rostecki 			  struct btrfs_io_geometry *io_geom);
5956bccf3abSJeff Mahoney int btrfs_read_sys_array(struct btrfs_fs_info *fs_info);
5965b4aacefSJeff Mahoney int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info);
597f6f39f7aSNikolay Borisov struct btrfs_block_group *btrfs_create_chunk(struct btrfs_trans_handle *trans,
59879bd3712SFilipe Manana 					    u64 type);
599c8bf1b67SDavid Sterba void btrfs_mapping_tree_free(struct extent_map_tree *tree);
6001a722d8fSChristoph Hellwig void btrfs_submit_bio(struct btrfs_fs_info *fs_info, struct bio *bio, int mirror_num);
6018a4b83ccSChris Mason int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
60297288f2cSChristoph Hellwig 		       fmode_t flags, void *holder);
60336350e95SGu Jinxiang struct btrfs_device *btrfs_scan_one_device(const char *path,
60436350e95SGu Jinxiang 					   fmode_t flags, void *holder);
60516cab91aSAnand Jain int btrfs_forget_devices(dev_t devt);
60654eed6aeSNikolay Borisov void btrfs_close_devices(struct btrfs_fs_devices *fs_devices);
607bacce86aSAnand Jain void btrfs_free_extra_devids(struct btrfs_fs_devices *fs_devices);
608d6507cf1SNikolay Borisov void btrfs_assign_next_active_device(struct btrfs_device *device,
609d6507cf1SNikolay Borisov 				     struct btrfs_device *this_dev);
610a27a94c2SNikolay Borisov struct btrfs_device *btrfs_find_device_by_devspec(struct btrfs_fs_info *fs_info,
611a27a94c2SNikolay Borisov 						  u64 devid,
612a27a94c2SNikolay Borisov 						  const char *devpath);
613faa775c4SJosef Bacik int btrfs_get_dev_args_from_path(struct btrfs_fs_info *fs_info,
614faa775c4SJosef Bacik 				 struct btrfs_dev_lookup_args *args,
615faa775c4SJosef Bacik 				 const char *path);
61612bd2fc0SIlya Dryomov struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
61712bd2fc0SIlya Dryomov 					const u64 *devid,
61812bd2fc0SIlya Dryomov 					const u8 *uuid);
619faa775c4SJosef Bacik void btrfs_put_dev_args_from_path(struct btrfs_dev_lookup_args *args);
620a425f9d4SDavid Sterba void btrfs_free_device(struct btrfs_device *device);
6212ff7e61eSJeff Mahoney int btrfs_rm_device(struct btrfs_fs_info *fs_info,
6221a15eb72SJosef Bacik 		    struct btrfs_dev_lookup_args *args,
6233fa421deSJosef Bacik 		    struct block_device **bdev, fmode_t *mode);
624ffc5a379SDavid Sterba void __exit btrfs_cleanup_fs_uuids(void);
6255d964051SStefan Behrens int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len);
6268f18cf13SChris Mason int btrfs_grow_device(struct btrfs_trans_handle *trans,
6278f18cf13SChris Mason 		      struct btrfs_device *device, u64 new_size);
628562d7b15SJosef Bacik struct btrfs_device *btrfs_find_device(const struct btrfs_fs_devices *fs_devices,
629562d7b15SJosef Bacik 				       const struct btrfs_dev_lookup_args *args);
6308f18cf13SChris Mason int btrfs_shrink_device(struct btrfs_device *device, u64 new_size);
631da353f6bSDavid Sterba int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *path);
6326fcf6e2bSDavid Sterba int btrfs_balance(struct btrfs_fs_info *fs_info,
6336fcf6e2bSDavid Sterba 		  struct btrfs_balance_control *bctl,
634c9e9f97bSIlya Dryomov 		  struct btrfs_ioctl_balance_args *bargs);
635f89e09cfSAnand Jain void btrfs_describe_block_groups(u64 flags, char *buf, u32 size_buf);
6362b6ba629SIlya Dryomov int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info);
63768310a5eSIlya Dryomov int btrfs_recover_balance(struct btrfs_fs_info *fs_info);
638837d5b6eSIlya Dryomov int btrfs_pause_balance(struct btrfs_fs_info *fs_info);
63918bb8bbfSJohannes Thumshirn int btrfs_relocate_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset);
640a7e99c69SIlya Dryomov int btrfs_cancel_balance(struct btrfs_fs_info *fs_info);
641f7a81ea4SStefan Behrens int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info);
64297f4dd09SNikolay Borisov int btrfs_uuid_scan_kthread(void *data);
643a09f23c3SAnand Jain bool btrfs_chunk_writeable(struct btrfs_fs_info *fs_info, u64 chunk_offset);
64460dfdf25SNikolay Borisov int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes,
645ba1bf481SJosef Bacik 			 u64 *start, u64 *max_avail);
646442a4f63SStefan Behrens void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index);
6472ff7e61eSJeff Mahoney int btrfs_get_dev_stats(struct btrfs_fs_info *fs_info,
648b27f7c0cSDavid Sterba 			struct btrfs_ioctl_get_dev_stats *stats);
649cb517eabSMiao Xie void btrfs_init_devices_late(struct btrfs_fs_info *fs_info);
650733f4fbbSStefan Behrens int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info);
651196c9d8dSDavid Sterba int btrfs_run_dev_stats(struct btrfs_trans_handle *trans);
65268a9db5fSNikolay Borisov void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_device *srcdev);
65365237ee3SDavid Sterba void btrfs_rm_dev_replace_free_srcdev(struct btrfs_device *srcdev);
6544f5ad7bdSNikolay Borisov void btrfs_destroy_dev_replace_tgtdev(struct btrfs_device *tgtdev);
655592d92eeSLiu Bo int btrfs_is_parity_mirror(struct btrfs_fs_info *fs_info,
656e4ff5fb5SNikolay Borisov 			   u64 logical, u64 len);
6572ff7e61eSJeff Mahoney unsigned long btrfs_full_stripe_len(struct btrfs_fs_info *fs_info,
65853b381b3SDavid Woodhouse 				    u64 logical);
659bc88b486SQu Wenruo u64 btrfs_calc_stripe_length(const struct extent_map *em);
6600b30f719SQu Wenruo int btrfs_nr_parity_stripes(u64 type);
66179bd3712SFilipe Manana int btrfs_chunk_alloc_add_chunk_item(struct btrfs_trans_handle *trans,
66279bd3712SFilipe Manana 				     struct btrfs_block_group *bg);
66397aff912SNikolay Borisov int btrfs_remove_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset);
66460ca842eSOmar Sandoval struct extent_map *btrfs_get_chunk_map(struct btrfs_fs_info *fs_info,
66560ca842eSOmar Sandoval 				       u64 logical, u64 length);
6668f32380dSJohannes Thumshirn void btrfs_release_disk_super(struct btrfs_super_block *super);
667addc3fa7SMiao Xie 
668442a4f63SStefan Behrens static inline void btrfs_dev_stat_inc(struct btrfs_device *dev,
669442a4f63SStefan Behrens 				      int index)
670442a4f63SStefan Behrens {
671442a4f63SStefan Behrens 	atomic_inc(dev->dev_stat_values + index);
6729deae968SNikolay Borisov 	/*
6739deae968SNikolay Borisov 	 * This memory barrier orders stores updating statistics before stores
6749deae968SNikolay Borisov 	 * updating dev_stats_ccnt.
6759deae968SNikolay Borisov 	 *
6769deae968SNikolay Borisov 	 * It pairs with smp_rmb() in btrfs_run_dev_stats().
6779deae968SNikolay Borisov 	 */
678addc3fa7SMiao Xie 	smp_mb__before_atomic();
679addc3fa7SMiao Xie 	atomic_inc(&dev->dev_stats_ccnt);
680442a4f63SStefan Behrens }
681442a4f63SStefan Behrens 
682442a4f63SStefan Behrens static inline int btrfs_dev_stat_read(struct btrfs_device *dev,
683442a4f63SStefan Behrens 				      int index)
684442a4f63SStefan Behrens {
685442a4f63SStefan Behrens 	return atomic_read(dev->dev_stat_values + index);
686442a4f63SStefan Behrens }
687442a4f63SStefan Behrens 
688442a4f63SStefan Behrens static inline int btrfs_dev_stat_read_and_reset(struct btrfs_device *dev,
689442a4f63SStefan Behrens 						int index)
690442a4f63SStefan Behrens {
691442a4f63SStefan Behrens 	int ret;
692442a4f63SStefan Behrens 
693442a4f63SStefan Behrens 	ret = atomic_xchg(dev->dev_stat_values + index, 0);
6944660c49fSNikolay Borisov 	/*
6954660c49fSNikolay Borisov 	 * atomic_xchg implies a full memory barriers as per atomic_t.txt:
6964660c49fSNikolay Borisov 	 * - RMW operations that have a return value are fully ordered;
6974660c49fSNikolay Borisov 	 *
6984660c49fSNikolay Borisov 	 * This implicit memory barriers is paired with the smp_rmb in
6994660c49fSNikolay Borisov 	 * btrfs_run_dev_stats
7004660c49fSNikolay Borisov 	 */
701addc3fa7SMiao Xie 	atomic_inc(&dev->dev_stats_ccnt);
702442a4f63SStefan Behrens 	return ret;
703442a4f63SStefan Behrens }
704442a4f63SStefan Behrens 
705442a4f63SStefan Behrens static inline void btrfs_dev_stat_set(struct btrfs_device *dev,
706442a4f63SStefan Behrens 				      int index, unsigned long val)
707442a4f63SStefan Behrens {
708442a4f63SStefan Behrens 	atomic_set(dev->dev_stat_values + index, val);
7099deae968SNikolay Borisov 	/*
7109deae968SNikolay Borisov 	 * This memory barrier orders stores updating statistics before stores
7119deae968SNikolay Borisov 	 * updating dev_stats_ccnt.
7129deae968SNikolay Borisov 	 *
7139deae968SNikolay Borisov 	 * It pairs with smp_rmb() in btrfs_run_dev_stats().
7149deae968SNikolay Borisov 	 */
715addc3fa7SMiao Xie 	smp_mb__before_atomic();
716addc3fa7SMiao Xie 	atomic_inc(&dev->dev_stats_ccnt);
717442a4f63SStefan Behrens }
718442a4f63SStefan Behrens 
719bbbf7243SNikolay Borisov void btrfs_commit_device_sizes(struct btrfs_transaction *trans);
72004216820SFilipe Manana 
7214143cb8bSDavid Sterba struct list_head * __attribute_const__ btrfs_get_fs_uuids(void);
7226528b99dSAnand Jain bool btrfs_check_rw_degradable(struct btrfs_fs_info *fs_info,
7236528b99dSAnand Jain 					struct btrfs_device *failing_dev);
724313b0858SJosef Bacik void btrfs_scratch_superblocks(struct btrfs_fs_info *fs_info,
725313b0858SJosef Bacik 			       struct block_device *bdev,
726313b0858SJosef Bacik 			       const char *device_path);
72721634a19SQu Wenruo 
728500a44c9SDavid Sterba enum btrfs_raid_types __attribute_const__ btrfs_bg_flags_to_raid_index(u64 flags);
72946df06b8SDavid Sterba int btrfs_bg_type_to_factor(u64 flags);
730158da513SDavid Sterba const char *btrfs_bg_type_to_raid_name(u64 flags);
731cf90d884SQu Wenruo int btrfs_verify_dev_extents(struct btrfs_fs_info *fs_info);
732554aed7dSJohannes Thumshirn bool btrfs_repair_one_zone(struct btrfs_fs_info *fs_info, u64 logical);
73346df06b8SDavid Sterba 
7340b86a832SChris Mason #endif
735