xref: /openbmc/linux/fs/btrfs/volumes.h (revision 4844c366)
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 
9b2117a39SMiao Xie #include <linux/sort.h>
1055e301fdSFilipe Brandenburger #include <linux/btrfs.h>
118b712842SChris Mason #include "async-thread.h"
12b31bed17SJosef Bacik #include "messages.h"
1327137facSChristoph Hellwig #include "tree-checker.h"
14cb3e217bSQu Wenruo #include "rcu-string.h"
15cea9e445SChris Mason 
16fce466eaSQu Wenruo #define BTRFS_MAX_DATA_CHUNK_SIZE	(10ULL * SZ_1G)
17fce466eaSQu Wenruo 
1867a2c45eSMiao Xie extern struct mutex uuid_mutex;
1967a2c45eSMiao Xie 
20ee22184bSByongho Lee #define BTRFS_STRIPE_LEN		SZ_64K
21a97699d1SQu Wenruo #define BTRFS_STRIPE_LEN_SHIFT		(16)
22a97699d1SQu Wenruo #define BTRFS_STRIPE_LEN_MASK		(BTRFS_STRIPE_LEN - 1)
23a97699d1SQu Wenruo 
24a97699d1SQu Wenruo static_assert(const_ilog2(BTRFS_STRIPE_LEN) == BTRFS_STRIPE_LEN_SHIFT);
25b2117a39SMiao Xie 
26719fae89SQu Wenruo /* Used by sanity check for btrfs_raid_types. */
27719fae89SQu Wenruo #define const_ffs(n) (__builtin_ctzll(n) + 1)
28719fae89SQu Wenruo 
29719fae89SQu Wenruo /*
30719fae89SQu Wenruo  * The conversion from BTRFS_BLOCK_GROUP_* bits to btrfs_raid_type requires
31719fae89SQu Wenruo  * RAID0 always to be the lowest profile bit.
32719fae89SQu Wenruo  * Although it's part of on-disk format and should never change, do extra
33719fae89SQu Wenruo  * compile-time sanity checks.
34719fae89SQu Wenruo  */
35719fae89SQu Wenruo static_assert(const_ffs(BTRFS_BLOCK_GROUP_RAID0) <
36719fae89SQu Wenruo 	      const_ffs(BTRFS_BLOCK_GROUP_PROFILE_MASK & ~BTRFS_BLOCK_GROUP_RAID0));
37719fae89SQu Wenruo static_assert(const_ilog2(BTRFS_BLOCK_GROUP_RAID0) >
38719fae89SQu Wenruo 	      ilog2(BTRFS_BLOCK_GROUP_TYPE_MASK));
39719fae89SQu Wenruo 
40719fae89SQu Wenruo /* ilog2() can handle both constants and variables */
41719fae89SQu Wenruo #define BTRFS_BG_FLAG_TO_INDEX(profile)					\
42719fae89SQu Wenruo 	ilog2((profile) >> (ilog2(BTRFS_BLOCK_GROUP_RAID0) - 1))
43719fae89SQu Wenruo 
44f04fbcc6SQu Wenruo enum btrfs_raid_types {
45719fae89SQu Wenruo 	/* SINGLE is the special one as it doesn't have on-disk bit. */
46719fae89SQu Wenruo 	BTRFS_RAID_SINGLE  = 0,
47719fae89SQu Wenruo 
48719fae89SQu Wenruo 	BTRFS_RAID_RAID0   = BTRFS_BG_FLAG_TO_INDEX(BTRFS_BLOCK_GROUP_RAID0),
49719fae89SQu Wenruo 	BTRFS_RAID_RAID1   = BTRFS_BG_FLAG_TO_INDEX(BTRFS_BLOCK_GROUP_RAID1),
50719fae89SQu Wenruo 	BTRFS_RAID_DUP	   = BTRFS_BG_FLAG_TO_INDEX(BTRFS_BLOCK_GROUP_DUP),
51719fae89SQu Wenruo 	BTRFS_RAID_RAID10  = BTRFS_BG_FLAG_TO_INDEX(BTRFS_BLOCK_GROUP_RAID10),
52719fae89SQu Wenruo 	BTRFS_RAID_RAID5   = BTRFS_BG_FLAG_TO_INDEX(BTRFS_BLOCK_GROUP_RAID5),
53719fae89SQu Wenruo 	BTRFS_RAID_RAID6   = BTRFS_BG_FLAG_TO_INDEX(BTRFS_BLOCK_GROUP_RAID6),
54719fae89SQu Wenruo 	BTRFS_RAID_RAID1C3 = BTRFS_BG_FLAG_TO_INDEX(BTRFS_BLOCK_GROUP_RAID1C3),
55719fae89SQu Wenruo 	BTRFS_RAID_RAID1C4 = BTRFS_BG_FLAG_TO_INDEX(BTRFS_BLOCK_GROUP_RAID1C4),
56719fae89SQu Wenruo 
57f04fbcc6SQu Wenruo 	BTRFS_NR_RAID_TYPES
58f04fbcc6SQu Wenruo };
59f04fbcc6SQu Wenruo 
607cc8e58dSMiao Xie /*
617cc8e58dSMiao Xie  * Use sequence counter to get consistent device stat data on
627cc8e58dSMiao Xie  * 32-bit processors.
637cc8e58dSMiao Xie  */
647cc8e58dSMiao Xie #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
657cc8e58dSMiao Xie #include <linux/seqlock.h>
667cc8e58dSMiao Xie #define __BTRFS_NEED_DEVICE_DATA_ORDERED
67c41ec452SSu Yue #define btrfs_device_data_ordered_init(device)	\
68c41ec452SSu Yue 	seqcount_init(&device->data_seqcount)
697cc8e58dSMiao Xie #else
70c41ec452SSu Yue #define btrfs_device_data_ordered_init(device) do { } while (0)
717cc8e58dSMiao Xie #endif
727cc8e58dSMiao Xie 
73ebbede42SAnand Jain #define BTRFS_DEV_STATE_WRITEABLE	(0)
74e12c9621SAnand Jain #define BTRFS_DEV_STATE_IN_FS_METADATA	(1)
75e6e674bdSAnand Jain #define BTRFS_DEV_STATE_MISSING		(2)
76401e29c1SAnand Jain #define BTRFS_DEV_STATE_REPLACE_TGT	(3)
771c3063b6SAnand Jain #define BTRFS_DEV_STATE_FLUSH_SENT	(4)
7866d204a1SFilipe Manana #define BTRFS_DEV_STATE_NO_READA	(5)
79ebbede42SAnand Jain 
805b316468SNaohiro Aota struct btrfs_zoned_device_info;
815b316468SNaohiro Aota 
820b86a832SChris Mason struct btrfs_device {
830b6f5d40SNikolay Borisov 	struct list_head dev_list; /* device_list_mutex */
840b6f5d40SNikolay Borisov 	struct list_head dev_alloc_list; /* chunk mutex */
85bbbf7243SNikolay Borisov 	struct list_head post_commit_list; /* chunk mutex */
862b82032cSYan Zheng 	struct btrfs_fs_devices *fs_devices;
87fb456252SJeff Mahoney 	struct btrfs_fs_info *fs_info;
88ffbd517dSChris Mason 
898d1a7aaeSMadhuparna Bhowmik 	struct rcu_string __rcu *name;
90d5ee37bcSMiao Xie 
91d5ee37bcSMiao Xie 	u64 generation;
92d5ee37bcSMiao Xie 
93d5ee37bcSMiao Xie 	struct block_device *bdev;
94d5ee37bcSMiao Xie 
955b316468SNaohiro Aota 	struct btrfs_zoned_device_info *zone_info;
965b316468SNaohiro Aota 
972736e8eeSChristoph Hellwig 	/* block device holder for blkdev_get/put */
982736e8eeSChristoph Hellwig 	void *holder;
99d5ee37bcSMiao Xie 
1004889bc05SAnand Jain 	/*
1014889bc05SAnand Jain 	 * Device's major-minor number. Must be set even if the device is not
1024889bc05SAnand Jain 	 * opened (bdev == NULL), unless the device is missing.
1034889bc05SAnand Jain 	 */
1044889bc05SAnand Jain 	dev_t devt;
105ebbede42SAnand Jain 	unsigned long dev_state;
10658efbc9fSOmar Sandoval 	blk_status_t last_flush_error;
107b3075717SChris Mason 
1087cc8e58dSMiao Xie #ifdef __BTRFS_NEED_DEVICE_DATA_ORDERED
109c41ec452SSu Yue 	seqcount_t data_seqcount;
1107cc8e58dSMiao Xie #endif
1117cc8e58dSMiao Xie 
1120b86a832SChris Mason 	/* the internal btrfs device id */
1130b86a832SChris Mason 	u64 devid;
1140b86a832SChris Mason 
1156ba40b61SMiao Xie 	/* size of the device in memory */
1160b86a832SChris Mason 	u64 total_bytes;
1170b86a832SChris Mason 
1186ba40b61SMiao Xie 	/* size of the device on disk */
119d6397baeSChris Ball 	u64 disk_total_bytes;
120d6397baeSChris Ball 
1210b86a832SChris Mason 	/* bytes used */
1220b86a832SChris Mason 	u64 bytes_used;
1230b86a832SChris Mason 
1240b86a832SChris Mason 	/* optimal io alignment for this device */
1250b86a832SChris Mason 	u32 io_align;
1260b86a832SChris Mason 
1270b86a832SChris Mason 	/* optimal io width for this device */
1280b86a832SChris Mason 	u32 io_width;
1293c45bfc1SDulshani Gunawardhana 	/* type and info about this device */
1303c45bfc1SDulshani Gunawardhana 	u64 type;
1310b86a832SChris Mason 
1320b86a832SChris Mason 	/* minimal io size for this device */
1330b86a832SChris Mason 	u32 sector_size;
1340b86a832SChris Mason 
1350b86a832SChris Mason 	/* physical drive uuid (or lvm uuid) */
136e17cade2SChris Mason 	u8 uuid[BTRFS_UUID_SIZE];
1378b712842SChris Mason 
138935e5cc9SMiao Xie 	/*
139935e5cc9SMiao Xie 	 * size of the device on the current transaction
140935e5cc9SMiao Xie 	 *
141935e5cc9SMiao Xie 	 * This variant is update when committing the transaction,
142bbbf7243SNikolay Borisov 	 * and protected by chunk mutex
143935e5cc9SMiao Xie 	 */
144935e5cc9SMiao Xie 	u64 commit_total_bytes;
145935e5cc9SMiao Xie 
146ce7213c7SMiao Xie 	/* bytes used on the current transaction */
147ce7213c7SMiao Xie 	u64 commit_bytes_used;
148935e5cc9SMiao Xie 
149f9e69aa9SChristoph Hellwig 	/* Bio used for flushing device barriers */
150f9e69aa9SChristoph Hellwig 	struct bio flush_bio;
1513c45bfc1SDulshani Gunawardhana 	struct completion flush_wait;
1523c45bfc1SDulshani Gunawardhana 
153a2de733cSArne Jansen 	/* per-device scrub information */
154cadbc0a0SAnand Jain 	struct scrub_ctx *scrub_ctx;
155a2de733cSArne Jansen 
156442a4f63SStefan Behrens 	/* disk I/O failure stats. For detailed description refer to
157442a4f63SStefan Behrens 	 * enum btrfs_dev_stat_values in ioctl.h */
158733f4fbbSStefan Behrens 	int dev_stats_valid;
159addc3fa7SMiao Xie 
160addc3fa7SMiao Xie 	/* Counter to record the change of device stats */
161addc3fa7SMiao Xie 	atomic_t dev_stats_ccnt;
162442a4f63SStefan Behrens 	atomic_t dev_stat_values[BTRFS_DEV_STAT_VALUES_MAX];
1631c11b63eSJeff Mahoney 
1641c11b63eSJeff Mahoney 	struct extent_io_tree alloc_state;
165668e48afSAnand Jain 
166668e48afSAnand Jain 	struct completion kobj_unregister;
167668e48afSAnand Jain 	/* For sysfs/FSID/devinfo/devid/ */
168668e48afSAnand Jain 	struct kobject devid_kobj;
169eb3b5053SDavid Sterba 
170eb3b5053SDavid Sterba 	/* Bandwidth limit for scrub, in bytes */
171eb3b5053SDavid Sterba 	u64 scrub_speed_max;
1720b86a832SChris Mason };
1730b86a832SChris Mason 
1747cc8e58dSMiao Xie /*
1752103da3bSJosef Bacik  * Block group or device which contains an active swapfile. Used for preventing
1762103da3bSJosef Bacik  * unsafe operations while a swapfile is active.
1772103da3bSJosef Bacik  *
1782103da3bSJosef Bacik  * These are sorted on (ptr, inode) (note that a block group or device can
1792103da3bSJosef Bacik  * contain more than one swapfile). We compare the pointer values because we
1802103da3bSJosef Bacik  * don't actually care what the object is, we just need a quick check whether
1812103da3bSJosef Bacik  * the object exists in the rbtree.
1822103da3bSJosef Bacik  */
1832103da3bSJosef Bacik struct btrfs_swapfile_pin {
1842103da3bSJosef Bacik 	struct rb_node node;
1852103da3bSJosef Bacik 	void *ptr;
1862103da3bSJosef Bacik 	struct inode *inode;
1872103da3bSJosef Bacik 	/*
1882103da3bSJosef Bacik 	 * If true, ptr points to a struct btrfs_block_group. Otherwise, ptr
1892103da3bSJosef Bacik 	 * points to a struct btrfs_device.
1902103da3bSJosef Bacik 	 */
1912103da3bSJosef Bacik 	bool is_block_group;
1922103da3bSJosef Bacik 	/*
1932103da3bSJosef Bacik 	 * Only used when 'is_block_group' is true and it is the number of
1942103da3bSJosef Bacik 	 * extents used by a swapfile for this block group ('ptr' field).
1952103da3bSJosef Bacik 	 */
1962103da3bSJosef Bacik 	int bg_extent_count;
1972103da3bSJosef Bacik };
1982103da3bSJosef Bacik 
1992103da3bSJosef Bacik /*
2007cc8e58dSMiao Xie  * If we read those variants at the context of their own lock, we needn't
2017cc8e58dSMiao Xie  * use the following helpers, reading them directly is safe.
2027cc8e58dSMiao Xie  */
2037cc8e58dSMiao Xie #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
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 	u64 size;							\
2097cc8e58dSMiao Xie 	unsigned int seq;						\
2107cc8e58dSMiao Xie 									\
2117cc8e58dSMiao Xie 	do {								\
2127cc8e58dSMiao Xie 		seq = read_seqcount_begin(&dev->data_seqcount);		\
2137cc8e58dSMiao Xie 		size = dev->name;					\
2147cc8e58dSMiao Xie 	} while (read_seqcount_retry(&dev->data_seqcount, seq));	\
2157cc8e58dSMiao Xie 	return size;							\
2167cc8e58dSMiao Xie }									\
2177cc8e58dSMiao Xie 									\
2187cc8e58dSMiao Xie static inline void							\
2197cc8e58dSMiao Xie btrfs_device_set_##name(struct btrfs_device *dev, u64 size)		\
2207cc8e58dSMiao Xie {									\
221c41ec452SSu Yue 	preempt_disable();						\
2227cc8e58dSMiao Xie 	write_seqcount_begin(&dev->data_seqcount);			\
2237cc8e58dSMiao Xie 	dev->name = size;						\
2247cc8e58dSMiao Xie 	write_seqcount_end(&dev->data_seqcount);			\
225c41ec452SSu Yue 	preempt_enable();						\
2267cc8e58dSMiao Xie }
22794545870SThomas Gleixner #elif BITS_PER_LONG==32 && defined(CONFIG_PREEMPTION)
2287cc8e58dSMiao Xie #define BTRFS_DEVICE_GETSET_FUNCS(name)					\
2297cc8e58dSMiao Xie static inline u64							\
2307cc8e58dSMiao Xie btrfs_device_get_##name(const struct btrfs_device *dev)			\
2317cc8e58dSMiao Xie {									\
2327cc8e58dSMiao Xie 	u64 size;							\
2337cc8e58dSMiao Xie 									\
2347cc8e58dSMiao Xie 	preempt_disable();						\
2357cc8e58dSMiao Xie 	size = dev->name;						\
2367cc8e58dSMiao Xie 	preempt_enable();						\
2377cc8e58dSMiao Xie 	return size;							\
2387cc8e58dSMiao Xie }									\
2397cc8e58dSMiao Xie 									\
2407cc8e58dSMiao Xie static inline void							\
2417cc8e58dSMiao Xie btrfs_device_set_##name(struct btrfs_device *dev, u64 size)		\
2427cc8e58dSMiao Xie {									\
2437cc8e58dSMiao Xie 	preempt_disable();						\
2447cc8e58dSMiao Xie 	dev->name = size;						\
2457cc8e58dSMiao Xie 	preempt_enable();						\
2467cc8e58dSMiao Xie }
2477cc8e58dSMiao Xie #else
2487cc8e58dSMiao Xie #define BTRFS_DEVICE_GETSET_FUNCS(name)					\
2497cc8e58dSMiao Xie static inline u64							\
2507cc8e58dSMiao Xie btrfs_device_get_##name(const struct btrfs_device *dev)			\
2517cc8e58dSMiao Xie {									\
2527cc8e58dSMiao Xie 	return dev->name;						\
2537cc8e58dSMiao Xie }									\
2547cc8e58dSMiao Xie 									\
2557cc8e58dSMiao Xie static inline void							\
2567cc8e58dSMiao Xie btrfs_device_set_##name(struct btrfs_device *dev, u64 size)		\
2577cc8e58dSMiao Xie {									\
2587cc8e58dSMiao Xie 	dev->name = size;						\
2597cc8e58dSMiao Xie }
2607cc8e58dSMiao Xie #endif
2617cc8e58dSMiao Xie 
2627cc8e58dSMiao Xie BTRFS_DEVICE_GETSET_FUNCS(total_bytes);
2637cc8e58dSMiao Xie BTRFS_DEVICE_GETSET_FUNCS(disk_total_bytes);
2647cc8e58dSMiao Xie BTRFS_DEVICE_GETSET_FUNCS(bytes_used);
2657cc8e58dSMiao Xie 
266c4a816c6SNaohiro Aota enum btrfs_chunk_allocation_policy {
267c4a816c6SNaohiro Aota 	BTRFS_CHUNK_ALLOC_REGULAR,
2681cd6121fSNaohiro Aota 	BTRFS_CHUNK_ALLOC_ZONED,
269c4a816c6SNaohiro Aota };
270c4a816c6SNaohiro Aota 
27133fd2f71SAnand Jain /*
27233fd2f71SAnand Jain  * Read policies for mirrored block group profiles, read picks the stripe based
27333fd2f71SAnand Jain  * on these policies.
27433fd2f71SAnand Jain  */
27533fd2f71SAnand Jain enum btrfs_read_policy {
27633fd2f71SAnand Jain 	/* Use process PID to choose the stripe */
27733fd2f71SAnand Jain 	BTRFS_READ_POLICY_PID,
27833fd2f71SAnand Jain 	BTRFS_NR_READ_POLICY,
27933fd2f71SAnand Jain };
28033fd2f71SAnand Jain 
2818a4b83ccSChris Mason struct btrfs_fs_devices {
2828a4b83ccSChris Mason 	u8 fsid[BTRFS_FSID_SIZE]; /* FS specific uuid */
283f62c302eSAnand Jain 
284f62c302eSAnand Jain 	/*
285f62c302eSAnand Jain 	 * UUID written into the btree blocks:
286f62c302eSAnand Jain 	 *
287f62c302eSAnand Jain 	 * - If metadata_uuid != fsid then super block must have
288f62c302eSAnand Jain 	 *   BTRFS_FEATURE_INCOMPAT_METADATA_UUID flag set.
289f62c302eSAnand Jain 	 *
290f62c302eSAnand Jain 	 * - Following shall be true at all times:
291f62c302eSAnand Jain 	 *   - metadata_uuid == btrfs_header::fsid
292f62c302eSAnand Jain 	 *   - metadata_uuid == btrfs_dev_item::fsid
293f62c302eSAnand Jain 	 */
2947239ff4bSNikolay Borisov 	u8 metadata_uuid[BTRFS_FSID_SIZE];
295f62c302eSAnand Jain 
296c4babc5eSAnand Jain 	struct list_head fs_list;
2978a4b83ccSChris Mason 
298add9745aSAnand Jain 	/*
299add9745aSAnand Jain 	 * Number of devices under this fsid including missing and
300add9745aSAnand Jain 	 * replace-target device and excludes seed devices.
301add9745aSAnand Jain 	 */
3028a4b83ccSChris Mason 	u64 num_devices;
303add9745aSAnand Jain 
304add9745aSAnand Jain 	/*
305add9745aSAnand Jain 	 * The number of devices that successfully opened, including
306add9745aSAnand Jain 	 * replace-target, excludes seed devices.
307add9745aSAnand Jain 	 */
308a0af469bSChris Mason 	u64 open_devices;
309add9745aSAnand Jain 
310add9745aSAnand Jain 	/* The number of devices that are under the chunk allocation list. */
3112b82032cSYan Zheng 	u64 rw_devices;
312add9745aSAnand Jain 
313add9745aSAnand Jain 	/* Count of missing devices under this fsid excluding seed device. */
314cd02dca5SChris Mason 	u64 missing_devices;
3152b82032cSYan Zheng 	u64 total_rw_bytes;
316add9745aSAnand Jain 
317add9745aSAnand Jain 	/*
318add9745aSAnand Jain 	 * Count of devices from btrfs_super_block::num_devices for this fsid,
319add9745aSAnand Jain 	 * which includes the seed device, excludes the transient replace-target
320add9745aSAnand Jain 	 * device.
321add9745aSAnand Jain 	 */
32202db0844SJosef Bacik 	u64 total_devices;
323d1a63002SNikolay Borisov 
324d1a63002SNikolay Borisov 	/* Highest generation number of seen devices */
325d1a63002SNikolay Borisov 	u64 latest_generation;
326d1a63002SNikolay Borisov 
327d24fa5c1SAnand Jain 	/*
328d24fa5c1SAnand Jain 	 * The mount device or a device with highest generation after removal
329d24fa5c1SAnand Jain 	 * or replace.
330d24fa5c1SAnand Jain 	 */
331d24fa5c1SAnand Jain 	struct btrfs_device *latest_dev;
332e5e9a520SChris Mason 
333d85512d5SAnand Jain 	/*
334d85512d5SAnand Jain 	 * All of the devices in the filesystem, protected by a mutex so we can
335d85512d5SAnand Jain 	 * safely walk it to write out the super blocks without worrying about
336d85512d5SAnand Jain 	 * adding/removing by the multi-device code. Scrubbing super block can
337d85512d5SAnand Jain 	 * kick off supers writing by holding this mutex lock.
338e5e9a520SChris Mason 	 */
339e5e9a520SChris Mason 	struct mutex device_list_mutex;
3400b6f5d40SNikolay Borisov 
3410b6f5d40SNikolay Borisov 	/* List of all devices, protected by device_list_mutex */
3428a4b83ccSChris Mason 	struct list_head devices;
343b3075717SChris Mason 
344d85512d5SAnand Jain 	/* Devices which can satisfy space allocation. Protected by * chunk_mutex. */
345b3075717SChris Mason 	struct list_head alloc_list;
3462b82032cSYan Zheng 
347944d3f9fSNikolay Borisov 	struct list_head seed_list;
3482b82032cSYan Zheng 
349d85512d5SAnand Jain 	/* Count fs-devices opened. */
3502b82032cSYan Zheng 	int opened;
351c289811cSChris Mason 
352d85512d5SAnand Jain 	/* Set when we find or add a device that doesn't have the nonrot flag set. */
3537f0432d0SJohannes Thumshirn 	bool rotating;
354d85512d5SAnand Jain 	/* Devices support TRIM/discard commands. */
35563a7cb13SDavid Sterba 	bool discardable;
3564693893bSAnand Jain 	bool fsid_change;
357d85512d5SAnand Jain 	/* The filesystem is a seed filesystem. */
3584693893bSAnand Jain 	bool seeding;
3592e7910d6SAnand Jain 
3605a13f430SAnand Jain 	struct btrfs_fs_info *fs_info;
3612e7910d6SAnand Jain 	/* sysfs kobjects */
362c1b7e474SAnand Jain 	struct kobject fsid_kobj;
363b5501504SAnand Jain 	struct kobject *devices_kobj;
364a013d141SAnand Jain 	struct kobject *devinfo_kobj;
3652e7910d6SAnand Jain 	struct completion kobj_unregister;
366c4a816c6SNaohiro Aota 
367c4a816c6SNaohiro Aota 	enum btrfs_chunk_allocation_policy chunk_alloc_policy;
36833fd2f71SAnand Jain 
369d85512d5SAnand Jain 	/* Policy used to read the mirrored stripes. */
37033fd2f71SAnand Jain 	enum btrfs_read_policy read_policy;
3718a4b83ccSChris Mason };
3728a4b83ccSChris Mason 
373ab4ba2e1SQu Wenruo #define BTRFS_MAX_DEVS(info) ((BTRFS_MAX_ITEM_SIZE(info)	\
374ab4ba2e1SQu Wenruo 			- sizeof(struct btrfs_chunk))		\
375ab4ba2e1SQu Wenruo 			/ sizeof(struct btrfs_stripe) + 1)
376ab4ba2e1SQu Wenruo 
377ab4ba2e1SQu Wenruo #define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE	\
378ab4ba2e1SQu Wenruo 				- 2 * sizeof(struct btrfs_disk_key)	\
379ab4ba2e1SQu Wenruo 				- 2 * sizeof(struct btrfs_chunk))	\
380ab4ba2e1SQu Wenruo 				/ sizeof(struct btrfs_stripe) + 1)
381ab4ba2e1SQu Wenruo 
3824c664611SQu Wenruo struct btrfs_io_stripe {
383cea9e445SChris Mason 	struct btrfs_device *dev;
3849ff7ddd3SChristoph Hellwig 	union {
3859ff7ddd3SChristoph Hellwig 		/* Block mapping */
386cea9e445SChris Mason 		u64 physical;
3879ff7ddd3SChristoph Hellwig 		/* For the endio handler */
3889ff7ddd3SChristoph Hellwig 		struct btrfs_io_context *bioc;
3899ff7ddd3SChristoph Hellwig 	};
390a4012f06SChristoph Hellwig };
391a4012f06SChristoph Hellwig 
392a4012f06SChristoph Hellwig struct btrfs_discard_stripe {
393a4012f06SChristoph Hellwig 	struct btrfs_device *dev;
394a4012f06SChristoph Hellwig 	u64 physical;
395a4012f06SChristoph Hellwig 	u64 length;
396cea9e445SChris Mason };
397cea9e445SChris Mason 
3984c664611SQu Wenruo /*
3994c664611SQu Wenruo  * Context for IO subsmission for device stripe.
4004c664611SQu Wenruo  *
4014c664611SQu Wenruo  * - Track the unfinished mirrors for mirror based profiles
4024c664611SQu Wenruo  *   Mirror based profiles are SINGLE/DUP/RAID1/RAID10.
4034c664611SQu Wenruo  *
4044c664611SQu Wenruo  * - Contain the logical -> physical mapping info
4054c664611SQu Wenruo  *   Used by submit_stripe_bio() for mapping logical bio
4064c664611SQu Wenruo  *   into physical device address.
4074c664611SQu Wenruo  *
4084c664611SQu Wenruo  * - Contain device replace info
4094c664611SQu Wenruo  *   Used by handle_ops_on_dev_replace() to copy logical bios
4104c664611SQu Wenruo  *   into the new device.
4114c664611SQu Wenruo  *
4124c664611SQu Wenruo  * - Contain RAID56 full stripe logical bytenrs
4134c664611SQu Wenruo  */
4144c664611SQu Wenruo struct btrfs_io_context {
415140475aeSElena Reshetova 	refcount_t refs;
416c404e0dcSMiao Xie 	struct btrfs_fs_info *fs_info;
41710f11900SZhao Lei 	u64 map_type; /* get from map_lookup->type */
4187d2b4daaSChris Mason 	struct bio *orig_bio;
419a236aed1SChris Mason 	atomic_t error;
4204ced85f8SQu Wenruo 	u16 max_errors;
4214ced85f8SQu Wenruo 
4224ced85f8SQu Wenruo 	/*
4234ced85f8SQu Wenruo 	 * The total number of stripes, including the extra duplicated
4244ced85f8SQu Wenruo 	 * stripe for replace.
4254ced85f8SQu Wenruo 	 */
4264ced85f8SQu Wenruo 	u16 num_stripes;
4274ced85f8SQu Wenruo 
4284ced85f8SQu Wenruo 	/*
4294ced85f8SQu Wenruo 	 * The mirror_num of this bioc.
4304ced85f8SQu Wenruo 	 *
4314ced85f8SQu Wenruo 	 * This is for reads which use 0 as mirror_num, thus we should return a
4324ced85f8SQu Wenruo 	 * valid mirror_num (>0) for the reader.
4334ced85f8SQu Wenruo 	 */
4344ced85f8SQu Wenruo 	u16 mirror_num;
4354ced85f8SQu Wenruo 
4364ced85f8SQu Wenruo 	/*
4374ced85f8SQu Wenruo 	 * The following two members are for dev-replace case only.
4384ced85f8SQu Wenruo 	 *
4391faf3885SQu Wenruo 	 * @replace_nr_stripes:	Number of duplicated stripes which need to be
4404ced85f8SQu Wenruo 	 *			written to replace target.
4414ced85f8SQu Wenruo 	 *			Should be <= 2 (2 for DUP, otherwise <= 1).
4421faf3885SQu Wenruo 	 * @replace_stripe_src:	The array indicates where the duplicated stripes
4431faf3885SQu Wenruo 	 *			are from.
4444ced85f8SQu Wenruo 	 *
4451faf3885SQu Wenruo 	 * The @replace_stripe_src[] array is mostly for RAID56 cases.
4464ced85f8SQu Wenruo 	 * As non-RAID56 stripes share the same contents of the mapped range,
4474ced85f8SQu Wenruo 	 * thus no need to bother where the duplicated ones are from.
4484ced85f8SQu Wenruo 	 *
4494ced85f8SQu Wenruo 	 * But for RAID56 case, all stripes contain different contents, thus
4504ced85f8SQu Wenruo 	 * we need a way to know the mapping.
4514ced85f8SQu Wenruo 	 *
4524ced85f8SQu Wenruo 	 * There is an example for the two members, using a RAID5 write:
4534ced85f8SQu Wenruo 	 *
4544ced85f8SQu Wenruo 	 *   num_stripes:	4 (3 + 1 duplicated write)
4554ced85f8SQu Wenruo 	 *   stripes[0]:	dev = devid 1, physical = X
4564ced85f8SQu Wenruo 	 *   stripes[1]:	dev = devid 2, physical = Y
4574ced85f8SQu Wenruo 	 *   stripes[2]:	dev = devid 3, physical = Z
4584ced85f8SQu Wenruo 	 *   stripes[3]:	dev = devid 0, physical = Y
4594ced85f8SQu Wenruo 	 *
4601faf3885SQu Wenruo 	 * replace_nr_stripes = 1
4611faf3885SQu Wenruo 	 * replace_stripe_src = 1	<- Means stripes[1] is involved in replace.
4621faf3885SQu Wenruo 	 *				   The duplicated stripe index would be
4631faf3885SQu Wenruo 	 *				   (@num_stripes - 1).
4641faf3885SQu Wenruo 	 *
4651faf3885SQu Wenruo 	 * Note, that we can still have cases replace_nr_stripes = 2 for DUP.
4661faf3885SQu Wenruo 	 * In that case, all stripes share the same content, thus we don't
4671faf3885SQu Wenruo 	 * need to bother @replace_stripe_src value at all.
4684ced85f8SQu Wenruo 	 */
4691faf3885SQu Wenruo 	u16 replace_nr_stripes;
4701faf3885SQu Wenruo 	s16 replace_stripe_src;
4718e5cfb55SZhao Lei 	/*
47218d758a2SQu Wenruo 	 * Logical bytenr of the full stripe start, only for RAID56 cases.
47318d758a2SQu Wenruo 	 *
47418d758a2SQu Wenruo 	 * When this value is set to other than (u64)-1, the stripes[] should
47518d758a2SQu Wenruo 	 * follow this pattern:
47618d758a2SQu Wenruo 	 *
47718d758a2SQu Wenruo 	 * (real_stripes = num_stripes - replace_nr_stripes)
47818d758a2SQu Wenruo 	 * (data_stripes = (is_raid6) ? (real_stripes - 2) : (real_stripes - 1))
47918d758a2SQu Wenruo 	 *
48018d758a2SQu Wenruo 	 * stripes[0]:			The first data stripe
48118d758a2SQu Wenruo 	 * stripes[1]:			The second data stripe
48218d758a2SQu Wenruo 	 * ...
48318d758a2SQu Wenruo 	 * stripes[data_stripes - 1]:	The last data stripe
48418d758a2SQu Wenruo 	 * stripes[data_stripes]:	The P stripe
48518d758a2SQu Wenruo 	 * stripes[data_stripes + 1]:	The Q stripe (only for RAID6).
4868e5cfb55SZhao Lei 	 */
48718d758a2SQu Wenruo 	u64 full_stripe_logical;
4884c664611SQu Wenruo 	struct btrfs_io_stripe stripes[];
489cea9e445SChris Mason };
490cea9e445SChris Mason 
491b2117a39SMiao Xie struct btrfs_device_info {
492b2117a39SMiao Xie 	struct btrfs_device *dev;
493b2117a39SMiao Xie 	u64 dev_offset;
494b2117a39SMiao Xie 	u64 max_avail;
49573c5de00SArne Jansen 	u64 total_avail;
496b2117a39SMiao Xie };
497b2117a39SMiao Xie 
49831e50229SLiu Bo struct btrfs_raid_attr {
4998c3e3582SDavid Sterba 	u8 sub_stripes;		/* sub_stripes info for map */
5008c3e3582SDavid Sterba 	u8 dev_stripes;		/* stripes per dev */
5018c3e3582SDavid Sterba 	u8 devs_max;		/* max devs to use */
5028c3e3582SDavid Sterba 	u8 devs_min;		/* min devs needed */
5038c3e3582SDavid Sterba 	u8 tolerated_failures;	/* max tolerated fail devs */
5048c3e3582SDavid Sterba 	u8 devs_increment;	/* ndevs has to be a multiple of this */
5058c3e3582SDavid Sterba 	u8 ncopies;		/* how many copies to data has */
5068c3e3582SDavid Sterba 	u8 nparity;		/* number of stripes worth of bytes to store
507b50836edSHans van Kranenburg 				 * parity information */
5088c3e3582SDavid Sterba 	u8 mindev_error;	/* error code if min devs requisite is unmet */
509ed23467bSAnand Jain 	const char raid_name[8]; /* name of the raid */
51041a6e891SAnand Jain 	u64 bg_flag;		/* block group flag of the raid */
51131e50229SLiu Bo };
51231e50229SLiu Bo 
513af902047SZhao Lei extern const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES];
514af902047SZhao Lei 
5151abe9b8aSliubo struct map_lookup {
5161abe9b8aSliubo 	u64 type;
5171abe9b8aSliubo 	int io_align;
5181abe9b8aSliubo 	int io_width;
5191abe9b8aSliubo 	int num_stripes;
5201abe9b8aSliubo 	int sub_stripes;
521cf90d884SQu Wenruo 	int verified_stripes; /* For mount time dev extent verification */
5224c664611SQu Wenruo 	struct btrfs_io_stripe stripes[];
5231abe9b8aSliubo };
5241abe9b8aSliubo 
525a2de733cSArne Jansen #define map_lookup_size(n) (sizeof(struct map_lookup) + \
5264c664611SQu Wenruo 			    (sizeof(struct btrfs_io_stripe) * (n)))
527a2de733cSArne Jansen 
528c9e9f97bSIlya Dryomov struct btrfs_balance_args;
52919a39dceSIlya Dryomov struct btrfs_balance_progress;
530c9e9f97bSIlya Dryomov struct btrfs_balance_control {
531c9e9f97bSIlya Dryomov 	struct btrfs_balance_args data;
532c9e9f97bSIlya Dryomov 	struct btrfs_balance_args meta;
533c9e9f97bSIlya Dryomov 	struct btrfs_balance_args sys;
534c9e9f97bSIlya Dryomov 
535c9e9f97bSIlya Dryomov 	u64 flags;
53619a39dceSIlya Dryomov 
53719a39dceSIlya Dryomov 	struct btrfs_balance_progress stat;
538c9e9f97bSIlya Dryomov };
539c9e9f97bSIlya Dryomov 
540562d7b15SJosef Bacik /*
541562d7b15SJosef Bacik  * Search for a given device by the set parameters
542562d7b15SJosef Bacik  */
543562d7b15SJosef Bacik struct btrfs_dev_lookup_args {
544562d7b15SJosef Bacik 	u64 devid;
545562d7b15SJosef Bacik 	u8 *uuid;
546562d7b15SJosef Bacik 	u8 *fsid;
547562d7b15SJosef Bacik 	bool missing;
548562d7b15SJosef Bacik };
549562d7b15SJosef Bacik 
550562d7b15SJosef Bacik /* We have to initialize to -1 because BTRFS_DEV_REPLACE_DEVID is 0 */
551562d7b15SJosef Bacik #define BTRFS_DEV_LOOKUP_ARGS_INIT { .devid = (u64)-1 }
552562d7b15SJosef Bacik 
553562d7b15SJosef Bacik #define BTRFS_DEV_LOOKUP_ARGS(name) \
554562d7b15SJosef Bacik 	struct btrfs_dev_lookup_args name = BTRFS_DEV_LOOKUP_ARGS_INIT
555562d7b15SJosef Bacik 
556cf8cddd3SChristoph Hellwig enum btrfs_map_op {
557cf8cddd3SChristoph Hellwig 	BTRFS_MAP_READ,
558cf8cddd3SChristoph Hellwig 	BTRFS_MAP_WRITE,
559cf8cddd3SChristoph Hellwig 	BTRFS_MAP_GET_READ_MIRRORS,
560cf8cddd3SChristoph Hellwig };
561cf8cddd3SChristoph Hellwig 
btrfs_op(struct bio * bio)562cf8cddd3SChristoph Hellwig static inline enum btrfs_map_op btrfs_op(struct bio *bio)
563cf8cddd3SChristoph Hellwig {
564cf8cddd3SChristoph Hellwig 	switch (bio_op(bio)) {
565cf8cddd3SChristoph Hellwig 	case REQ_OP_WRITE:
566cfe94440SNaohiro Aota 	case REQ_OP_ZONE_APPEND:
567cf8cddd3SChristoph Hellwig 		return BTRFS_MAP_WRITE;
568cf8cddd3SChristoph Hellwig 	default:
569cf8cddd3SChristoph Hellwig 		WARN_ON_ONCE(1);
570c730ae0cSMarcos Paulo de Souza 		fallthrough;
571cf8cddd3SChristoph Hellwig 	case REQ_OP_READ:
572cf8cddd3SChristoph Hellwig 		return BTRFS_MAP_READ;
573cf8cddd3SChristoph Hellwig 	}
574cf8cddd3SChristoph Hellwig }
575cf8cddd3SChristoph Hellwig 
btrfs_chunk_item_size(int num_stripes)576b31bed17SJosef Bacik static inline unsigned long btrfs_chunk_item_size(int num_stripes)
577b31bed17SJosef Bacik {
578b31bed17SJosef Bacik 	ASSERT(num_stripes);
579b31bed17SJosef Bacik 	return sizeof(struct btrfs_chunk) +
580b31bed17SJosef Bacik 		sizeof(struct btrfs_stripe) * (num_stripes - 1);
581b31bed17SJosef Bacik }
582b31bed17SJosef Bacik 
583cb091225SQu Wenruo /*
584cb091225SQu Wenruo  * Do the type safe converstion from stripe_nr to offset inside the chunk.
585cb091225SQu Wenruo  *
586cb091225SQu Wenruo  * @stripe_nr is u32, with left shift it can overflow u32 for chunks larger
587cb091225SQu Wenruo  * than 4G.  This does the proper type cast to avoid overflow.
588cb091225SQu Wenruo  */
btrfs_stripe_nr_to_offset(u32 stripe_nr)589cb091225SQu Wenruo static inline u64 btrfs_stripe_nr_to_offset(u32 stripe_nr)
590cb091225SQu Wenruo {
591cb091225SQu Wenruo 	return (u64)stripe_nr << BTRFS_STRIPE_LEN_SHIFT;
592cb091225SQu Wenruo }
593cb091225SQu Wenruo 
5944c664611SQu Wenruo void btrfs_get_bioc(struct btrfs_io_context *bioc);
5954c664611SQu Wenruo void btrfs_put_bioc(struct btrfs_io_context *bioc);
596cf8cddd3SChristoph Hellwig int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
597cea9e445SChris Mason 		    u64 logical, u64 *length,
598103c1972SChristoph Hellwig 		    struct btrfs_io_context **bioc_ret,
599103c1972SChristoph Hellwig 		    struct btrfs_io_stripe *smap, int *mirror_num_ret,
600103c1972SChristoph Hellwig 		    int need_raid_map);
6014886ff7bSQu Wenruo int btrfs_map_repair_block(struct btrfs_fs_info *fs_info,
6024886ff7bSQu Wenruo 			   struct btrfs_io_stripe *smap, u64 logical,
6034886ff7bSQu Wenruo 			   u32 length, int mirror_num);
604a4012f06SChristoph Hellwig struct btrfs_discard_stripe *btrfs_map_discard(struct btrfs_fs_info *fs_info,
605a4012f06SChristoph Hellwig 					       u64 logical, u64 *length_ret,
606a4012f06SChristoph Hellwig 					       u32 *num_stripes);
6076bccf3abSJeff Mahoney int btrfs_read_sys_array(struct btrfs_fs_info *fs_info);
6085b4aacefSJeff Mahoney int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info);
609f6f39f7aSNikolay Borisov struct btrfs_block_group *btrfs_create_chunk(struct btrfs_trans_handle *trans,
61079bd3712SFilipe Manana 					    u64 type);
611c8bf1b67SDavid Sterba void btrfs_mapping_tree_free(struct extent_map_tree *tree);
6128a4b83ccSChris Mason int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
61305bdb996SChristoph Hellwig 		       blk_mode_t flags, void *holder);
61405bdb996SChristoph Hellwig struct btrfs_device *btrfs_scan_one_device(const char *path, blk_mode_t flags);
61516cab91aSAnand Jain int btrfs_forget_devices(dev_t devt);
61654eed6aeSNikolay Borisov void btrfs_close_devices(struct btrfs_fs_devices *fs_devices);
617bacce86aSAnand Jain void btrfs_free_extra_devids(struct btrfs_fs_devices *fs_devices);
618d6507cf1SNikolay Borisov void btrfs_assign_next_active_device(struct btrfs_device *device,
619d6507cf1SNikolay Borisov 				     struct btrfs_device *this_dev);
620a27a94c2SNikolay Borisov struct btrfs_device *btrfs_find_device_by_devspec(struct btrfs_fs_info *fs_info,
621a27a94c2SNikolay Borisov 						  u64 devid,
622a27a94c2SNikolay Borisov 						  const char *devpath);
623faa775c4SJosef Bacik int btrfs_get_dev_args_from_path(struct btrfs_fs_info *fs_info,
624faa775c4SJosef Bacik 				 struct btrfs_dev_lookup_args *args,
625faa775c4SJosef Bacik 				 const char *path);
62612bd2fc0SIlya Dryomov struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
627bb21e302SAnand Jain 					const u64 *devid, const u8 *uuid,
628bb21e302SAnand Jain 					const char *path);
629faa775c4SJosef Bacik void btrfs_put_dev_args_from_path(struct btrfs_dev_lookup_args *args);
6302ff7e61eSJeff Mahoney int btrfs_rm_device(struct btrfs_fs_info *fs_info,
6311a15eb72SJosef Bacik 		    struct btrfs_dev_lookup_args *args,
6322736e8eeSChristoph Hellwig 		    struct block_device **bdev, void **holder);
633ffc5a379SDavid Sterba void __exit btrfs_cleanup_fs_uuids(void);
6345d964051SStefan Behrens int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len);
6358f18cf13SChris Mason int btrfs_grow_device(struct btrfs_trans_handle *trans,
6368f18cf13SChris Mason 		      struct btrfs_device *device, u64 new_size);
637562d7b15SJosef Bacik struct btrfs_device *btrfs_find_device(const struct btrfs_fs_devices *fs_devices,
638562d7b15SJosef Bacik 				       const struct btrfs_dev_lookup_args *args);
6398f18cf13SChris Mason int btrfs_shrink_device(struct btrfs_device *device, u64 new_size);
640da353f6bSDavid Sterba int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *path);
6416fcf6e2bSDavid Sterba int btrfs_balance(struct btrfs_fs_info *fs_info,
6426fcf6e2bSDavid Sterba 		  struct btrfs_balance_control *bctl,
643c9e9f97bSIlya Dryomov 		  struct btrfs_ioctl_balance_args *bargs);
644f89e09cfSAnand Jain void btrfs_describe_block_groups(u64 flags, char *buf, u32 size_buf);
6452b6ba629SIlya Dryomov int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info);
64668310a5eSIlya Dryomov int btrfs_recover_balance(struct btrfs_fs_info *fs_info);
647837d5b6eSIlya Dryomov int btrfs_pause_balance(struct btrfs_fs_info *fs_info);
64818bb8bbfSJohannes Thumshirn int btrfs_relocate_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset);
649a7e99c69SIlya Dryomov int btrfs_cancel_balance(struct btrfs_fs_info *fs_info);
650f7a81ea4SStefan Behrens int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info);
65197f4dd09SNikolay Borisov int btrfs_uuid_scan_kthread(void *data);
652a09f23c3SAnand Jain bool btrfs_chunk_writeable(struct btrfs_fs_info *fs_info, u64 chunk_offset);
653442a4f63SStefan Behrens void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index);
6542ff7e61eSJeff Mahoney int btrfs_get_dev_stats(struct btrfs_fs_info *fs_info,
655b27f7c0cSDavid Sterba 			struct btrfs_ioctl_get_dev_stats *stats);
656a8d1b164SJohannes Thumshirn int btrfs_init_devices_late(struct btrfs_fs_info *fs_info);
657733f4fbbSStefan Behrens int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info);
658196c9d8dSDavid Sterba int btrfs_run_dev_stats(struct btrfs_trans_handle *trans);
65968a9db5fSNikolay Borisov void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_device *srcdev);
66065237ee3SDavid Sterba void btrfs_rm_dev_replace_free_srcdev(struct btrfs_device *srcdev);
6614f5ad7bdSNikolay Borisov void btrfs_destroy_dev_replace_tgtdev(struct btrfs_device *tgtdev);
662592d92eeSLiu Bo int btrfs_is_parity_mirror(struct btrfs_fs_info *fs_info,
663e4ff5fb5SNikolay Borisov 			   u64 logical, u64 len);
6642ff7e61eSJeff Mahoney unsigned long btrfs_full_stripe_len(struct btrfs_fs_info *fs_info,
66553b381b3SDavid Woodhouse 				    u64 logical);
666bc88b486SQu Wenruo u64 btrfs_calc_stripe_length(const struct extent_map *em);
6670b30f719SQu Wenruo int btrfs_nr_parity_stripes(u64 type);
66879bd3712SFilipe Manana int btrfs_chunk_alloc_add_chunk_item(struct btrfs_trans_handle *trans,
66979bd3712SFilipe Manana 				     struct btrfs_block_group *bg);
67097aff912SNikolay Borisov int btrfs_remove_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset);
67160ca842eSOmar Sandoval struct extent_map *btrfs_get_chunk_map(struct btrfs_fs_info *fs_info,
67260ca842eSOmar Sandoval 				       u64 logical, u64 length);
6738f32380dSJohannes Thumshirn void btrfs_release_disk_super(struct btrfs_super_block *super);
674addc3fa7SMiao Xie 
btrfs_dev_stat_inc(struct btrfs_device * dev,int index)675442a4f63SStefan Behrens static inline void btrfs_dev_stat_inc(struct btrfs_device *dev,
676442a4f63SStefan Behrens 				      int index)
677442a4f63SStefan Behrens {
678442a4f63SStefan Behrens 	atomic_inc(dev->dev_stat_values + index);
6799deae968SNikolay Borisov 	/*
6809deae968SNikolay Borisov 	 * This memory barrier orders stores updating statistics before stores
6819deae968SNikolay Borisov 	 * updating dev_stats_ccnt.
6829deae968SNikolay Borisov 	 *
6839deae968SNikolay Borisov 	 * It pairs with smp_rmb() in btrfs_run_dev_stats().
6849deae968SNikolay Borisov 	 */
685addc3fa7SMiao Xie 	smp_mb__before_atomic();
686addc3fa7SMiao Xie 	atomic_inc(&dev->dev_stats_ccnt);
687442a4f63SStefan Behrens }
688442a4f63SStefan Behrens 
btrfs_dev_stat_read(struct btrfs_device * dev,int index)689442a4f63SStefan Behrens static inline int btrfs_dev_stat_read(struct btrfs_device *dev,
690442a4f63SStefan Behrens 				      int index)
691442a4f63SStefan Behrens {
692442a4f63SStefan Behrens 	return atomic_read(dev->dev_stat_values + index);
693442a4f63SStefan Behrens }
694442a4f63SStefan Behrens 
btrfs_dev_stat_read_and_reset(struct btrfs_device * dev,int index)695442a4f63SStefan Behrens static inline int btrfs_dev_stat_read_and_reset(struct btrfs_device *dev,
696442a4f63SStefan Behrens 						int index)
697442a4f63SStefan Behrens {
698442a4f63SStefan Behrens 	int ret;
699442a4f63SStefan Behrens 
700442a4f63SStefan Behrens 	ret = atomic_xchg(dev->dev_stat_values + index, 0);
7014660c49fSNikolay Borisov 	/*
7024660c49fSNikolay Borisov 	 * atomic_xchg implies a full memory barriers as per atomic_t.txt:
7034660c49fSNikolay Borisov 	 * - RMW operations that have a return value are fully ordered;
7044660c49fSNikolay Borisov 	 *
7054660c49fSNikolay Borisov 	 * This implicit memory barriers is paired with the smp_rmb in
7064660c49fSNikolay Borisov 	 * btrfs_run_dev_stats
7074660c49fSNikolay Borisov 	 */
708addc3fa7SMiao Xie 	atomic_inc(&dev->dev_stats_ccnt);
709442a4f63SStefan Behrens 	return ret;
710442a4f63SStefan Behrens }
711442a4f63SStefan Behrens 
btrfs_dev_stat_set(struct btrfs_device * dev,int index,unsigned long val)712442a4f63SStefan Behrens static inline void btrfs_dev_stat_set(struct btrfs_device *dev,
713442a4f63SStefan Behrens 				      int index, unsigned long val)
714442a4f63SStefan Behrens {
715442a4f63SStefan Behrens 	atomic_set(dev->dev_stat_values + index, val);
7169deae968SNikolay Borisov 	/*
7179deae968SNikolay Borisov 	 * This memory barrier orders stores updating statistics before stores
7189deae968SNikolay Borisov 	 * updating dev_stats_ccnt.
7199deae968SNikolay Borisov 	 *
7209deae968SNikolay Borisov 	 * It pairs with smp_rmb() in btrfs_run_dev_stats().
7219deae968SNikolay Borisov 	 */
722addc3fa7SMiao Xie 	smp_mb__before_atomic();
723addc3fa7SMiao Xie 	atomic_inc(&dev->dev_stats_ccnt);
724442a4f63SStefan Behrens }
725442a4f63SStefan Behrens 
btrfs_dev_name(const struct btrfs_device * device)726cb3e217bSQu Wenruo static inline const char *btrfs_dev_name(const struct btrfs_device *device)
727cb3e217bSQu Wenruo {
728cb3e217bSQu Wenruo 	if (!device || test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
729cb3e217bSQu Wenruo 		return "<missing disk>";
730cb3e217bSQu Wenruo 	else
731cb3e217bSQu Wenruo 		return rcu_str_deref(device->name);
732cb3e217bSQu Wenruo }
733cb3e217bSQu Wenruo 
734bbbf7243SNikolay Borisov void btrfs_commit_device_sizes(struct btrfs_transaction *trans);
73504216820SFilipe Manana 
7364143cb8bSDavid Sterba struct list_head * __attribute_const__ btrfs_get_fs_uuids(void);
7376528b99dSAnand Jain bool btrfs_check_rw_degradable(struct btrfs_fs_info *fs_info,
7386528b99dSAnand Jain 					struct btrfs_device *failing_dev);
739313b0858SJosef Bacik void btrfs_scratch_superblocks(struct btrfs_fs_info *fs_info,
740313b0858SJosef Bacik 			       struct block_device *bdev,
741313b0858SJosef Bacik 			       const char *device_path);
74221634a19SQu Wenruo 
743500a44c9SDavid Sterba enum btrfs_raid_types __attribute_const__ btrfs_bg_flags_to_raid_index(u64 flags);
74446df06b8SDavid Sterba int btrfs_bg_type_to_factor(u64 flags);
745158da513SDavid Sterba const char *btrfs_bg_type_to_raid_name(u64 flags);
746cf90d884SQu Wenruo int btrfs_verify_dev_extents(struct btrfs_fs_info *fs_info);
747554aed7dSJohannes Thumshirn bool btrfs_repair_one_zone(struct btrfs_fs_info *fs_info, u64 logical);
74846df06b8SDavid Sterba 
749c2e79e86SJosef Bacik bool btrfs_pinned_by_swapfile(struct btrfs_fs_info *fs_info, void *ptr);
750*4844c366SAnand Jain u8 *btrfs_sb_fsid_ptr(struct btrfs_super_block *sb);
751c2e79e86SJosef Bacik 
7520b86a832SChris Mason #endif
753