xref: /openbmc/linux/fs/btrfs/volumes.h (revision 27137fac)
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"
13b31bed17SJosef Bacik #include "messages.h"
14*27137facSChristoph Hellwig #include "tree-checker.h"
15cb3e217bSQu Wenruo #include "rcu-string.h"
16cea9e445SChris Mason 
17fce466eaSQu Wenruo #define BTRFS_MAX_DATA_CHUNK_SIZE	(10ULL * SZ_1G)
18fce466eaSQu Wenruo 
1967a2c45eSMiao Xie extern struct mutex uuid_mutex;
2067a2c45eSMiao Xie 
21ee22184bSByongho Lee #define BTRFS_STRIPE_LEN	SZ_64K
22b2117a39SMiao Xie 
23719fae89SQu Wenruo /* Used by sanity check for btrfs_raid_types. */
24719fae89SQu Wenruo #define const_ffs(n) (__builtin_ctzll(n) + 1)
25719fae89SQu Wenruo 
26719fae89SQu Wenruo /*
27719fae89SQu Wenruo  * The conversion from BTRFS_BLOCK_GROUP_* bits to btrfs_raid_type requires
28719fae89SQu Wenruo  * RAID0 always to be the lowest profile bit.
29719fae89SQu Wenruo  * Although it's part of on-disk format and should never change, do extra
30719fae89SQu Wenruo  * compile-time sanity checks.
31719fae89SQu Wenruo  */
32719fae89SQu Wenruo static_assert(const_ffs(BTRFS_BLOCK_GROUP_RAID0) <
33719fae89SQu Wenruo 	      const_ffs(BTRFS_BLOCK_GROUP_PROFILE_MASK & ~BTRFS_BLOCK_GROUP_RAID0));
34719fae89SQu Wenruo static_assert(const_ilog2(BTRFS_BLOCK_GROUP_RAID0) >
35719fae89SQu Wenruo 	      ilog2(BTRFS_BLOCK_GROUP_TYPE_MASK));
36719fae89SQu Wenruo 
37719fae89SQu Wenruo /* ilog2() can handle both constants and variables */
38719fae89SQu Wenruo #define BTRFS_BG_FLAG_TO_INDEX(profile)					\
39719fae89SQu Wenruo 	ilog2((profile) >> (ilog2(BTRFS_BLOCK_GROUP_RAID0) - 1))
40719fae89SQu Wenruo 
41f04fbcc6SQu Wenruo enum btrfs_raid_types {
42719fae89SQu Wenruo 	/* SINGLE is the special one as it doesn't have on-disk bit. */
43719fae89SQu Wenruo 	BTRFS_RAID_SINGLE  = 0,
44719fae89SQu Wenruo 
45719fae89SQu Wenruo 	BTRFS_RAID_RAID0   = BTRFS_BG_FLAG_TO_INDEX(BTRFS_BLOCK_GROUP_RAID0),
46719fae89SQu Wenruo 	BTRFS_RAID_RAID1   = BTRFS_BG_FLAG_TO_INDEX(BTRFS_BLOCK_GROUP_RAID1),
47719fae89SQu Wenruo 	BTRFS_RAID_DUP	   = BTRFS_BG_FLAG_TO_INDEX(BTRFS_BLOCK_GROUP_DUP),
48719fae89SQu Wenruo 	BTRFS_RAID_RAID10  = BTRFS_BG_FLAG_TO_INDEX(BTRFS_BLOCK_GROUP_RAID10),
49719fae89SQu Wenruo 	BTRFS_RAID_RAID5   = BTRFS_BG_FLAG_TO_INDEX(BTRFS_BLOCK_GROUP_RAID5),
50719fae89SQu Wenruo 	BTRFS_RAID_RAID6   = BTRFS_BG_FLAG_TO_INDEX(BTRFS_BLOCK_GROUP_RAID6),
51719fae89SQu Wenruo 	BTRFS_RAID_RAID1C3 = BTRFS_BG_FLAG_TO_INDEX(BTRFS_BLOCK_GROUP_RAID1C3),
52719fae89SQu Wenruo 	BTRFS_RAID_RAID1C4 = BTRFS_BG_FLAG_TO_INDEX(BTRFS_BLOCK_GROUP_RAID1C4),
53719fae89SQu Wenruo 
54f04fbcc6SQu Wenruo 	BTRFS_NR_RAID_TYPES
55f04fbcc6SQu Wenruo };
56f04fbcc6SQu Wenruo 
575f141126SNikolay Borisov struct btrfs_io_geometry {
585f141126SNikolay Borisov 	/* remaining bytes before crossing a stripe */
595f141126SNikolay Borisov 	u64 len;
605f141126SNikolay Borisov 	/* offset of logical address in chunk */
615f141126SNikolay Borisov 	u64 offset;
625f141126SNikolay Borisov 	/* length of single IO stripe */
63cc353a8bSQu Wenruo 	u32 stripe_len;
64cc353a8bSQu Wenruo 	/* offset of address in stripe */
65cc353a8bSQu Wenruo 	u32 stripe_offset;
665f141126SNikolay Borisov 	/* number of stripe where address falls */
675f141126SNikolay Borisov 	u64 stripe_nr;
685f141126SNikolay Borisov 	/* offset of raid56 stripe into the chunk */
695f141126SNikolay Borisov 	u64 raid56_stripe_offset;
705f141126SNikolay Borisov };
715f141126SNikolay Borisov 
727cc8e58dSMiao Xie /*
737cc8e58dSMiao Xie  * Use sequence counter to get consistent device stat data on
747cc8e58dSMiao Xie  * 32-bit processors.
757cc8e58dSMiao Xie  */
767cc8e58dSMiao Xie #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
777cc8e58dSMiao Xie #include <linux/seqlock.h>
787cc8e58dSMiao Xie #define __BTRFS_NEED_DEVICE_DATA_ORDERED
79c41ec452SSu Yue #define btrfs_device_data_ordered_init(device)	\
80c41ec452SSu Yue 	seqcount_init(&device->data_seqcount)
817cc8e58dSMiao Xie #else
82c41ec452SSu Yue #define btrfs_device_data_ordered_init(device) do { } while (0)
837cc8e58dSMiao Xie #endif
847cc8e58dSMiao Xie 
85ebbede42SAnand Jain #define BTRFS_DEV_STATE_WRITEABLE	(0)
86e12c9621SAnand Jain #define BTRFS_DEV_STATE_IN_FS_METADATA	(1)
87e6e674bdSAnand Jain #define BTRFS_DEV_STATE_MISSING		(2)
88401e29c1SAnand Jain #define BTRFS_DEV_STATE_REPLACE_TGT	(3)
891c3063b6SAnand Jain #define BTRFS_DEV_STATE_FLUSH_SENT	(4)
9066d204a1SFilipe Manana #define BTRFS_DEV_STATE_NO_READA	(5)
91ebbede42SAnand Jain 
925b316468SNaohiro Aota struct btrfs_zoned_device_info;
935b316468SNaohiro Aota 
940b86a832SChris Mason struct btrfs_device {
950b6f5d40SNikolay Borisov 	struct list_head dev_list; /* device_list_mutex */
960b6f5d40SNikolay Borisov 	struct list_head dev_alloc_list; /* chunk mutex */
97bbbf7243SNikolay Borisov 	struct list_head post_commit_list; /* chunk mutex */
982b82032cSYan Zheng 	struct btrfs_fs_devices *fs_devices;
99fb456252SJeff Mahoney 	struct btrfs_fs_info *fs_info;
100ffbd517dSChris Mason 
1018d1a7aaeSMadhuparna Bhowmik 	struct rcu_string __rcu *name;
102d5ee37bcSMiao Xie 
103d5ee37bcSMiao Xie 	u64 generation;
104d5ee37bcSMiao Xie 
105d5ee37bcSMiao Xie 	struct block_device *bdev;
106d5ee37bcSMiao Xie 
1075b316468SNaohiro Aota 	struct btrfs_zoned_device_info *zone_info;
1085b316468SNaohiro Aota 
109d5ee37bcSMiao Xie 	/* the mode sent to blkdev_get */
110d5ee37bcSMiao Xie 	fmode_t mode;
111d5ee37bcSMiao Xie 
1124889bc05SAnand Jain 	/*
1134889bc05SAnand Jain 	 * Device's major-minor number. Must be set even if the device is not
1144889bc05SAnand Jain 	 * opened (bdev == NULL), unless the device is missing.
1154889bc05SAnand Jain 	 */
1164889bc05SAnand Jain 	dev_t devt;
117ebbede42SAnand Jain 	unsigned long dev_state;
11858efbc9fSOmar Sandoval 	blk_status_t last_flush_error;
119b3075717SChris Mason 
1207cc8e58dSMiao Xie #ifdef __BTRFS_NEED_DEVICE_DATA_ORDERED
121c41ec452SSu Yue 	seqcount_t data_seqcount;
1227cc8e58dSMiao Xie #endif
1237cc8e58dSMiao Xie 
1240b86a832SChris Mason 	/* the internal btrfs device id */
1250b86a832SChris Mason 	u64 devid;
1260b86a832SChris Mason 
1276ba40b61SMiao Xie 	/* size of the device in memory */
1280b86a832SChris Mason 	u64 total_bytes;
1290b86a832SChris Mason 
1306ba40b61SMiao Xie 	/* size of the device on disk */
131d6397baeSChris Ball 	u64 disk_total_bytes;
132d6397baeSChris Ball 
1330b86a832SChris Mason 	/* bytes used */
1340b86a832SChris Mason 	u64 bytes_used;
1350b86a832SChris Mason 
1360b86a832SChris Mason 	/* optimal io alignment for this device */
1370b86a832SChris Mason 	u32 io_align;
1380b86a832SChris Mason 
1390b86a832SChris Mason 	/* optimal io width for this device */
1400b86a832SChris Mason 	u32 io_width;
1413c45bfc1SDulshani Gunawardhana 	/* type and info about this device */
1423c45bfc1SDulshani Gunawardhana 	u64 type;
1430b86a832SChris Mason 
1440b86a832SChris Mason 	/* minimal io size for this device */
1450b86a832SChris Mason 	u32 sector_size;
1460b86a832SChris Mason 
1470b86a832SChris Mason 	/* physical drive uuid (or lvm uuid) */
148e17cade2SChris Mason 	u8 uuid[BTRFS_UUID_SIZE];
1498b712842SChris Mason 
150935e5cc9SMiao Xie 	/*
151935e5cc9SMiao Xie 	 * size of the device on the current transaction
152935e5cc9SMiao Xie 	 *
153935e5cc9SMiao Xie 	 * This variant is update when committing the transaction,
154bbbf7243SNikolay Borisov 	 * and protected by chunk mutex
155935e5cc9SMiao Xie 	 */
156935e5cc9SMiao Xie 	u64 commit_total_bytes;
157935e5cc9SMiao Xie 
158ce7213c7SMiao Xie 	/* bytes used on the current transaction */
159ce7213c7SMiao Xie 	u64 commit_bytes_used;
160935e5cc9SMiao Xie 
161f9e69aa9SChristoph Hellwig 	/* Bio used for flushing device barriers */
162f9e69aa9SChristoph Hellwig 	struct bio flush_bio;
1633c45bfc1SDulshani Gunawardhana 	struct completion flush_wait;
1643c45bfc1SDulshani Gunawardhana 
165a2de733cSArne Jansen 	/* per-device scrub information */
166cadbc0a0SAnand Jain 	struct scrub_ctx *scrub_ctx;
167a2de733cSArne Jansen 
168442a4f63SStefan Behrens 	/* disk I/O failure stats. For detailed description refer to
169442a4f63SStefan Behrens 	 * enum btrfs_dev_stat_values in ioctl.h */
170733f4fbbSStefan Behrens 	int dev_stats_valid;
171addc3fa7SMiao Xie 
172addc3fa7SMiao Xie 	/* Counter to record the change of device stats */
173addc3fa7SMiao Xie 	atomic_t dev_stats_ccnt;
174442a4f63SStefan Behrens 	atomic_t dev_stat_values[BTRFS_DEV_STAT_VALUES_MAX];
1751c11b63eSJeff Mahoney 
1761c11b63eSJeff Mahoney 	struct extent_io_tree alloc_state;
177668e48afSAnand Jain 
178668e48afSAnand Jain 	struct completion kobj_unregister;
179668e48afSAnand Jain 	/* For sysfs/FSID/devinfo/devid/ */
180668e48afSAnand Jain 	struct kobject devid_kobj;
181eb3b5053SDavid Sterba 
182eb3b5053SDavid Sterba 	/* Bandwidth limit for scrub, in bytes */
183eb3b5053SDavid Sterba 	u64 scrub_speed_max;
1840b86a832SChris Mason };
1850b86a832SChris Mason 
1867cc8e58dSMiao Xie /*
1872103da3bSJosef Bacik  * Block group or device which contains an active swapfile. Used for preventing
1882103da3bSJosef Bacik  * unsafe operations while a swapfile is active.
1892103da3bSJosef Bacik  *
1902103da3bSJosef Bacik  * These are sorted on (ptr, inode) (note that a block group or device can
1912103da3bSJosef Bacik  * contain more than one swapfile). We compare the pointer values because we
1922103da3bSJosef Bacik  * don't actually care what the object is, we just need a quick check whether
1932103da3bSJosef Bacik  * the object exists in the rbtree.
1942103da3bSJosef Bacik  */
1952103da3bSJosef Bacik struct btrfs_swapfile_pin {
1962103da3bSJosef Bacik 	struct rb_node node;
1972103da3bSJosef Bacik 	void *ptr;
1982103da3bSJosef Bacik 	struct inode *inode;
1992103da3bSJosef Bacik 	/*
2002103da3bSJosef Bacik 	 * If true, ptr points to a struct btrfs_block_group. Otherwise, ptr
2012103da3bSJosef Bacik 	 * points to a struct btrfs_device.
2022103da3bSJosef Bacik 	 */
2032103da3bSJosef Bacik 	bool is_block_group;
2042103da3bSJosef Bacik 	/*
2052103da3bSJosef Bacik 	 * Only used when 'is_block_group' is true and it is the number of
2062103da3bSJosef Bacik 	 * extents used by a swapfile for this block group ('ptr' field).
2072103da3bSJosef Bacik 	 */
2082103da3bSJosef Bacik 	int bg_extent_count;
2092103da3bSJosef Bacik };
2102103da3bSJosef Bacik 
2112103da3bSJosef Bacik /*
2127cc8e58dSMiao Xie  * If we read those variants at the context of their own lock, we needn't
2137cc8e58dSMiao Xie  * use the following helpers, reading them directly is safe.
2147cc8e58dSMiao Xie  */
2157cc8e58dSMiao Xie #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
2167cc8e58dSMiao Xie #define BTRFS_DEVICE_GETSET_FUNCS(name)					\
2177cc8e58dSMiao Xie static inline u64							\
2187cc8e58dSMiao Xie btrfs_device_get_##name(const struct btrfs_device *dev)			\
2197cc8e58dSMiao Xie {									\
2207cc8e58dSMiao Xie 	u64 size;							\
2217cc8e58dSMiao Xie 	unsigned int seq;						\
2227cc8e58dSMiao Xie 									\
2237cc8e58dSMiao Xie 	do {								\
2247cc8e58dSMiao Xie 		seq = read_seqcount_begin(&dev->data_seqcount);		\
2257cc8e58dSMiao Xie 		size = dev->name;					\
2267cc8e58dSMiao Xie 	} while (read_seqcount_retry(&dev->data_seqcount, seq));	\
2277cc8e58dSMiao Xie 	return size;							\
2287cc8e58dSMiao Xie }									\
2297cc8e58dSMiao Xie 									\
2307cc8e58dSMiao Xie static inline void							\
2317cc8e58dSMiao Xie btrfs_device_set_##name(struct btrfs_device *dev, u64 size)		\
2327cc8e58dSMiao Xie {									\
233c41ec452SSu Yue 	preempt_disable();						\
2347cc8e58dSMiao Xie 	write_seqcount_begin(&dev->data_seqcount);			\
2357cc8e58dSMiao Xie 	dev->name = size;						\
2367cc8e58dSMiao Xie 	write_seqcount_end(&dev->data_seqcount);			\
237c41ec452SSu Yue 	preempt_enable();						\
2387cc8e58dSMiao Xie }
23994545870SThomas Gleixner #elif BITS_PER_LONG==32 && defined(CONFIG_PREEMPTION)
2407cc8e58dSMiao Xie #define BTRFS_DEVICE_GETSET_FUNCS(name)					\
2417cc8e58dSMiao Xie static inline u64							\
2427cc8e58dSMiao Xie btrfs_device_get_##name(const struct btrfs_device *dev)			\
2437cc8e58dSMiao Xie {									\
2447cc8e58dSMiao Xie 	u64 size;							\
2457cc8e58dSMiao Xie 									\
2467cc8e58dSMiao Xie 	preempt_disable();						\
2477cc8e58dSMiao Xie 	size = dev->name;						\
2487cc8e58dSMiao Xie 	preempt_enable();						\
2497cc8e58dSMiao Xie 	return size;							\
2507cc8e58dSMiao Xie }									\
2517cc8e58dSMiao Xie 									\
2527cc8e58dSMiao Xie static inline void							\
2537cc8e58dSMiao Xie btrfs_device_set_##name(struct btrfs_device *dev, u64 size)		\
2547cc8e58dSMiao Xie {									\
2557cc8e58dSMiao Xie 	preempt_disable();						\
2567cc8e58dSMiao Xie 	dev->name = size;						\
2577cc8e58dSMiao Xie 	preempt_enable();						\
2587cc8e58dSMiao Xie }
2597cc8e58dSMiao Xie #else
2607cc8e58dSMiao Xie #define BTRFS_DEVICE_GETSET_FUNCS(name)					\
2617cc8e58dSMiao Xie static inline u64							\
2627cc8e58dSMiao Xie btrfs_device_get_##name(const struct btrfs_device *dev)			\
2637cc8e58dSMiao Xie {									\
2647cc8e58dSMiao Xie 	return dev->name;						\
2657cc8e58dSMiao Xie }									\
2667cc8e58dSMiao Xie 									\
2677cc8e58dSMiao Xie static inline void							\
2687cc8e58dSMiao Xie btrfs_device_set_##name(struct btrfs_device *dev, u64 size)		\
2697cc8e58dSMiao Xie {									\
2707cc8e58dSMiao Xie 	dev->name = size;						\
2717cc8e58dSMiao Xie }
2727cc8e58dSMiao Xie #endif
2737cc8e58dSMiao Xie 
2747cc8e58dSMiao Xie BTRFS_DEVICE_GETSET_FUNCS(total_bytes);
2757cc8e58dSMiao Xie BTRFS_DEVICE_GETSET_FUNCS(disk_total_bytes);
2767cc8e58dSMiao Xie BTRFS_DEVICE_GETSET_FUNCS(bytes_used);
2777cc8e58dSMiao Xie 
278c4a816c6SNaohiro Aota enum btrfs_chunk_allocation_policy {
279c4a816c6SNaohiro Aota 	BTRFS_CHUNK_ALLOC_REGULAR,
2801cd6121fSNaohiro Aota 	BTRFS_CHUNK_ALLOC_ZONED,
281c4a816c6SNaohiro Aota };
282c4a816c6SNaohiro Aota 
28333fd2f71SAnand Jain /*
28433fd2f71SAnand Jain  * Read policies for mirrored block group profiles, read picks the stripe based
28533fd2f71SAnand Jain  * on these policies.
28633fd2f71SAnand Jain  */
28733fd2f71SAnand Jain enum btrfs_read_policy {
28833fd2f71SAnand Jain 	/* Use process PID to choose the stripe */
28933fd2f71SAnand Jain 	BTRFS_READ_POLICY_PID,
29033fd2f71SAnand Jain 	BTRFS_NR_READ_POLICY,
29133fd2f71SAnand Jain };
29233fd2f71SAnand Jain 
2938a4b83ccSChris Mason struct btrfs_fs_devices {
2948a4b83ccSChris Mason 	u8 fsid[BTRFS_FSID_SIZE]; /* FS specific uuid */
2957239ff4bSNikolay Borisov 	u8 metadata_uuid[BTRFS_FSID_SIZE];
296d1a63002SNikolay Borisov 	bool fsid_change;
297c4babc5eSAnand Jain 	struct list_head fs_list;
2988a4b83ccSChris Mason 
299add9745aSAnand Jain 	/*
300add9745aSAnand Jain 	 * Number of devices under this fsid including missing and
301add9745aSAnand Jain 	 * replace-target device and excludes seed devices.
302add9745aSAnand Jain 	 */
3038a4b83ccSChris Mason 	u64 num_devices;
304add9745aSAnand Jain 
305add9745aSAnand Jain 	/*
306add9745aSAnand Jain 	 * The number of devices that successfully opened, including
307add9745aSAnand Jain 	 * replace-target, excludes seed devices.
308add9745aSAnand Jain 	 */
309a0af469bSChris Mason 	u64 open_devices;
310add9745aSAnand Jain 
311add9745aSAnand Jain 	/* The number of devices that are under the chunk allocation list. */
3122b82032cSYan Zheng 	u64 rw_devices;
313add9745aSAnand Jain 
314add9745aSAnand Jain 	/* Count of missing devices under this fsid excluding seed device. */
315cd02dca5SChris Mason 	u64 missing_devices;
3162b82032cSYan Zheng 	u64 total_rw_bytes;
317add9745aSAnand Jain 
318add9745aSAnand Jain 	/*
319add9745aSAnand Jain 	 * Count of devices from btrfs_super_block::num_devices for this fsid,
320add9745aSAnand Jain 	 * which includes the seed device, excludes the transient replace-target
321add9745aSAnand Jain 	 * device.
322add9745aSAnand Jain 	 */
32302db0844SJosef Bacik 	u64 total_devices;
324d1a63002SNikolay Borisov 
325d1a63002SNikolay Borisov 	/* Highest generation number of seen devices */
326d1a63002SNikolay Borisov 	u64 latest_generation;
327d1a63002SNikolay Borisov 
328d24fa5c1SAnand Jain 	/*
329d24fa5c1SAnand Jain 	 * The mount device or a device with highest generation after removal
330d24fa5c1SAnand Jain 	 * or replace.
331d24fa5c1SAnand Jain 	 */
332d24fa5c1SAnand Jain 	struct btrfs_device *latest_dev;
333e5e9a520SChris Mason 
334e5e9a520SChris Mason 	/* all of the devices in the FS, protected by a mutex
335e5e9a520SChris Mason 	 * so we can safely walk it to write out the supers without
3369b011adfSWang Shilong 	 * worrying about add/remove by the multi-device code.
3379b011adfSWang Shilong 	 * Scrubbing super can kick off supers writing by holding
3389b011adfSWang Shilong 	 * this mutex lock.
339e5e9a520SChris Mason 	 */
340e5e9a520SChris Mason 	struct mutex device_list_mutex;
3410b6f5d40SNikolay Borisov 
3420b6f5d40SNikolay Borisov 	/* List of all devices, protected by device_list_mutex */
3438a4b83ccSChris Mason 	struct list_head devices;
344b3075717SChris Mason 
3450b6f5d40SNikolay Borisov 	/*
3460b6f5d40SNikolay Borisov 	 * Devices which can satisfy space allocation. Protected by
3470b6f5d40SNikolay Borisov 	 * chunk_mutex
3480b6f5d40SNikolay Borisov 	 */
349b3075717SChris Mason 	struct list_head alloc_list;
3502b82032cSYan Zheng 
351944d3f9fSNikolay Borisov 	struct list_head seed_list;
3520395d84fSJohannes Thumshirn 	bool seeding;
3532b82032cSYan Zheng 
3542b82032cSYan Zheng 	int opened;
355c289811cSChris Mason 
356c289811cSChris Mason 	/* set when we find or add a device that doesn't have the
357c289811cSChris Mason 	 * nonrot flag set
358c289811cSChris Mason 	 */
3597f0432d0SJohannes Thumshirn 	bool rotating;
36063a7cb13SDavid Sterba 	/* Devices support TRIM/discard commands */
36163a7cb13SDavid Sterba 	bool discardable;
3622e7910d6SAnand Jain 
3635a13f430SAnand Jain 	struct btrfs_fs_info *fs_info;
3642e7910d6SAnand Jain 	/* sysfs kobjects */
365c1b7e474SAnand Jain 	struct kobject fsid_kobj;
366b5501504SAnand Jain 	struct kobject *devices_kobj;
367a013d141SAnand Jain 	struct kobject *devinfo_kobj;
3682e7910d6SAnand Jain 	struct completion kobj_unregister;
369c4a816c6SNaohiro Aota 
370c4a816c6SNaohiro Aota 	enum btrfs_chunk_allocation_policy chunk_alloc_policy;
37133fd2f71SAnand Jain 
37233fd2f71SAnand Jain 	/* Policy used to read the mirrored stripes */
37333fd2f71SAnand Jain 	enum btrfs_read_policy read_policy;
3748a4b83ccSChris Mason };
3758a4b83ccSChris Mason 
376facc8a22SMiao Xie #define BTRFS_BIO_INLINE_CSUM_SIZE	64
377facc8a22SMiao Xie 
378ab4ba2e1SQu Wenruo #define BTRFS_MAX_DEVS(info) ((BTRFS_MAX_ITEM_SIZE(info)	\
379ab4ba2e1SQu Wenruo 			- sizeof(struct btrfs_chunk))		\
380ab4ba2e1SQu Wenruo 			/ sizeof(struct btrfs_stripe) + 1)
381ab4ba2e1SQu Wenruo 
382ab4ba2e1SQu Wenruo #define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE	\
383ab4ba2e1SQu Wenruo 				- 2 * sizeof(struct btrfs_disk_key)	\
384ab4ba2e1SQu Wenruo 				- 2 * sizeof(struct btrfs_chunk))	\
385ab4ba2e1SQu Wenruo 				/ sizeof(struct btrfs_stripe) + 1)
386ab4ba2e1SQu Wenruo 
3879be3395bSChris Mason /*
388ee5b46a3SChristoph Hellwig  * Maximum number of sectors for a single bio to limit the size of the
389ee5b46a3SChristoph Hellwig  * checksum array.  This matches the number of bio_vecs per bio and thus the
390ee5b46a3SChristoph Hellwig  * I/O size for buffered I/O.
391ee5b46a3SChristoph Hellwig  */
392ee5b46a3SChristoph Hellwig #define BTRFS_MAX_BIO_SECTORS				(256)
393ee5b46a3SChristoph Hellwig 
394917f32a2SChristoph Hellwig typedef void (*btrfs_bio_end_io_t)(struct btrfs_bio *bbio);
395917f32a2SChristoph Hellwig 
396ee5b46a3SChristoph Hellwig /*
397c3a3b19bSQu Wenruo  * Additional info to pass along bio.
398c3a3b19bSQu Wenruo  *
399c3a3b19bSQu Wenruo  * Mostly for btrfs specific features like csum and mirror_num.
4009be3395bSChris Mason  */
401c3a3b19bSQu Wenruo struct btrfs_bio {
402947a6299SQu Wenruo 	unsigned int mirror_num:7;
403947a6299SQu Wenruo 
404947a6299SQu Wenruo 	/*
405947a6299SQu Wenruo 	 * Extra indicator for metadata bios.
406947a6299SQu Wenruo 	 * For some btrfs bios they use pages without a mapping, thus
407947a6299SQu Wenruo 	 * we can not rely on page->mapping->host to determine if
408947a6299SQu Wenruo 	 * it's a metadata bio.
409947a6299SQu Wenruo 	 */
410947a6299SQu Wenruo 	unsigned int is_metadata:1;
411ae0e5df4SDavid Sterba 	struct bvec_iter iter;
412c3a3b19bSQu Wenruo 
41300d82525SChristoph Hellwig 	/* for direct I/O */
41400d82525SChristoph Hellwig 	u64 file_offset;
41500d82525SChristoph Hellwig 
416c3a3b19bSQu Wenruo 	/* @device is for stripe IO submission. */
417c31efbdfSNikolay Borisov 	struct btrfs_device *device;
418947a6299SQu Wenruo 	union {
419947a6299SQu Wenruo 		/* For data checksum verification. */
420947a6299SQu Wenruo 		struct {
421facc8a22SMiao Xie 			u8 *csum;
422facc8a22SMiao Xie 			u8 csum_inline[BTRFS_BIO_INLINE_CSUM_SIZE];
423947a6299SQu Wenruo 		};
424947a6299SQu Wenruo 
425947a6299SQu Wenruo 		/* For metadata parentness verification. */
426947a6299SQu Wenruo 		struct btrfs_tree_parent_check parent_check;
427947a6299SQu Wenruo 	};
428c3a3b19bSQu Wenruo 
429917f32a2SChristoph Hellwig 	/* End I/O information supplied to btrfs_bio_alloc */
430917f32a2SChristoph Hellwig 	btrfs_bio_end_io_t end_io;
431917f32a2SChristoph Hellwig 	void *private;
432917f32a2SChristoph Hellwig 
433d7b9416fSChristoph Hellwig 	/* For read end I/O handling */
434d7b9416fSChristoph Hellwig 	struct work_struct end_io_work;
435d7b9416fSChristoph Hellwig 
436fa1bcbe0SDavid Sterba 	/*
437fa1bcbe0SDavid Sterba 	 * This member must come last, bio_alloc_bioset will allocate enough
438c3a3b19bSQu Wenruo 	 * bytes for entire btrfs_bio but relies on bio being last.
439fa1bcbe0SDavid Sterba 	 */
4409be3395bSChris Mason 	struct bio bio;
4419be3395bSChris Mason };
4429be3395bSChris Mason 
443c3a3b19bSQu Wenruo static inline struct btrfs_bio *btrfs_bio(struct bio *bio)
4449be3395bSChris Mason {
445c3a3b19bSQu Wenruo 	return container_of(bio, struct btrfs_bio, bio);
4469be3395bSChris Mason }
4479be3395bSChris Mason 
448d45cfb88SChristoph Hellwig int __init btrfs_bioset_init(void);
449d45cfb88SChristoph Hellwig void __cold btrfs_bioset_exit(void);
450d45cfb88SChristoph Hellwig 
451917f32a2SChristoph Hellwig struct bio *btrfs_bio_alloc(unsigned int nr_vecs, blk_opf_t opf,
452917f32a2SChristoph Hellwig 			    btrfs_bio_end_io_t end_io, void *private);
453917f32a2SChristoph Hellwig struct bio *btrfs_bio_clone_partial(struct bio *orig, u64 offset, u64 size,
454917f32a2SChristoph Hellwig 				    btrfs_bio_end_io_t end_io, void *private);
455917f32a2SChristoph Hellwig 
456917f32a2SChristoph Hellwig static inline void btrfs_bio_end_io(struct btrfs_bio *bbio, blk_status_t status)
457917f32a2SChristoph Hellwig {
458917f32a2SChristoph Hellwig 	bbio->bio.bi_status = status;
459917f32a2SChristoph Hellwig 	bbio->end_io(bbio);
460917f32a2SChristoph Hellwig }
461d45cfb88SChristoph Hellwig 
462c3a3b19bSQu Wenruo static inline void btrfs_bio_free_csum(struct btrfs_bio *bbio)
463b3a0dd50SDavid Sterba {
464947a6299SQu Wenruo 	if (bbio->is_metadata)
465947a6299SQu Wenruo 		return;
466c3a3b19bSQu Wenruo 	if (bbio->csum != bbio->csum_inline) {
467c3a3b19bSQu Wenruo 		kfree(bbio->csum);
468c3a3b19bSQu Wenruo 		bbio->csum = NULL;
469b3a0dd50SDavid Sterba 	}
470b3a0dd50SDavid Sterba }
471b3a0dd50SDavid Sterba 
472261d812bSQu Wenruo /*
473261d812bSQu Wenruo  * Iterate through a btrfs_bio (@bbio) on a per-sector basis.
474261d812bSQu Wenruo  *
475261d812bSQu Wenruo  * bvl        - struct bio_vec
476261d812bSQu Wenruo  * bbio       - struct btrfs_bio
477261d812bSQu Wenruo  * iters      - struct bvec_iter
478261d812bSQu Wenruo  * bio_offset - unsigned int
479261d812bSQu Wenruo  */
480261d812bSQu Wenruo #define btrfs_bio_for_each_sector(fs_info, bvl, bbio, iter, bio_offset)	\
481261d812bSQu Wenruo 	for ((iter) = (bbio)->iter, (bio_offset) = 0;			\
482261d812bSQu Wenruo 	     (iter).bi_size &&					\
483261d812bSQu Wenruo 	     (((bvl) = bio_iter_iovec((&(bbio)->bio), (iter))), 1);	\
484261d812bSQu Wenruo 	     (bio_offset) += fs_info->sectorsize,			\
485261d812bSQu Wenruo 	     bio_advance_iter_single(&(bbio)->bio, &(iter),		\
486261d812bSQu Wenruo 	     (fs_info)->sectorsize))
487261d812bSQu Wenruo 
4884c664611SQu Wenruo struct btrfs_io_stripe {
489cea9e445SChris Mason 	struct btrfs_device *dev;
4909ff7ddd3SChristoph Hellwig 	union {
4919ff7ddd3SChristoph Hellwig 		/* Block mapping */
492cea9e445SChris Mason 		u64 physical;
4939ff7ddd3SChristoph Hellwig 		/* For the endio handler */
4949ff7ddd3SChristoph Hellwig 		struct btrfs_io_context *bioc;
4959ff7ddd3SChristoph Hellwig 	};
496a4012f06SChristoph Hellwig };
497a4012f06SChristoph Hellwig 
498a4012f06SChristoph Hellwig struct btrfs_discard_stripe {
499a4012f06SChristoph Hellwig 	struct btrfs_device *dev;
500a4012f06SChristoph Hellwig 	u64 physical;
501a4012f06SChristoph Hellwig 	u64 length;
502cea9e445SChris Mason };
503cea9e445SChris Mason 
5044c664611SQu Wenruo /*
5054c664611SQu Wenruo  * Context for IO subsmission for device stripe.
5064c664611SQu Wenruo  *
5074c664611SQu Wenruo  * - Track the unfinished mirrors for mirror based profiles
5084c664611SQu Wenruo  *   Mirror based profiles are SINGLE/DUP/RAID1/RAID10.
5094c664611SQu Wenruo  *
5104c664611SQu Wenruo  * - Contain the logical -> physical mapping info
5114c664611SQu Wenruo  *   Used by submit_stripe_bio() for mapping logical bio
5124c664611SQu Wenruo  *   into physical device address.
5134c664611SQu Wenruo  *
5144c664611SQu Wenruo  * - Contain device replace info
5154c664611SQu Wenruo  *   Used by handle_ops_on_dev_replace() to copy logical bios
5164c664611SQu Wenruo  *   into the new device.
5174c664611SQu Wenruo  *
5184c664611SQu Wenruo  * - Contain RAID56 full stripe logical bytenrs
5194c664611SQu Wenruo  */
5204c664611SQu Wenruo struct btrfs_io_context {
521140475aeSElena Reshetova 	refcount_t refs;
522c404e0dcSMiao Xie 	struct btrfs_fs_info *fs_info;
52310f11900SZhao Lei 	u64 map_type; /* get from map_lookup->type */
5247d2b4daaSChris Mason 	struct bio *orig_bio;
525a236aed1SChris Mason 	atomic_t error;
526a236aed1SChris Mason 	int max_errors;
527cea9e445SChris Mason 	int num_stripes;
528a1d3c478SJan Schmidt 	int mirror_num;
5292c8cdd6eSMiao Xie 	int num_tgtdevs;
5302c8cdd6eSMiao Xie 	int *tgtdev_map;
5318e5cfb55SZhao Lei 	/*
5328e5cfb55SZhao Lei 	 * logical block numbers for the start of each stripe
5338e5cfb55SZhao Lei 	 * The last one or two are p/q.  These are sorted,
5348e5cfb55SZhao Lei 	 * so raid_map[0] is the start of our full stripe
5358e5cfb55SZhao Lei 	 */
5368e5cfb55SZhao Lei 	u64 *raid_map;
5374c664611SQu Wenruo 	struct btrfs_io_stripe stripes[];
538cea9e445SChris Mason };
539cea9e445SChris Mason 
540b2117a39SMiao Xie struct btrfs_device_info {
541b2117a39SMiao Xie 	struct btrfs_device *dev;
542b2117a39SMiao Xie 	u64 dev_offset;
543b2117a39SMiao Xie 	u64 max_avail;
54473c5de00SArne Jansen 	u64 total_avail;
545b2117a39SMiao Xie };
546b2117a39SMiao Xie 
54731e50229SLiu Bo struct btrfs_raid_attr {
5488c3e3582SDavid Sterba 	u8 sub_stripes;		/* sub_stripes info for map */
5498c3e3582SDavid Sterba 	u8 dev_stripes;		/* stripes per dev */
5508c3e3582SDavid Sterba 	u8 devs_max;		/* max devs to use */
5518c3e3582SDavid Sterba 	u8 devs_min;		/* min devs needed */
5528c3e3582SDavid Sterba 	u8 tolerated_failures;	/* max tolerated fail devs */
5538c3e3582SDavid Sterba 	u8 devs_increment;	/* ndevs has to be a multiple of this */
5548c3e3582SDavid Sterba 	u8 ncopies;		/* how many copies to data has */
5558c3e3582SDavid Sterba 	u8 nparity;		/* number of stripes worth of bytes to store
556b50836edSHans van Kranenburg 				 * parity information */
5578c3e3582SDavid Sterba 	u8 mindev_error;	/* error code if min devs requisite is unmet */
558ed23467bSAnand Jain 	const char raid_name[8]; /* name of the raid */
55941a6e891SAnand Jain 	u64 bg_flag;		/* block group flag of the raid */
56031e50229SLiu Bo };
56131e50229SLiu Bo 
562af902047SZhao Lei extern const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES];
563af902047SZhao Lei 
5641abe9b8aSliubo struct map_lookup {
5651abe9b8aSliubo 	u64 type;
5661abe9b8aSliubo 	int io_align;
5671abe9b8aSliubo 	int io_width;
568cc353a8bSQu Wenruo 	u32 stripe_len;
5691abe9b8aSliubo 	int num_stripes;
5701abe9b8aSliubo 	int sub_stripes;
571cf90d884SQu Wenruo 	int verified_stripes; /* For mount time dev extent verification */
5724c664611SQu Wenruo 	struct btrfs_io_stripe stripes[];
5731abe9b8aSliubo };
5741abe9b8aSliubo 
575a2de733cSArne Jansen #define map_lookup_size(n) (sizeof(struct map_lookup) + \
5764c664611SQu Wenruo 			    (sizeof(struct btrfs_io_stripe) * (n)))
577a2de733cSArne Jansen 
578c9e9f97bSIlya Dryomov struct btrfs_balance_args;
57919a39dceSIlya Dryomov struct btrfs_balance_progress;
580c9e9f97bSIlya Dryomov struct btrfs_balance_control {
581c9e9f97bSIlya Dryomov 	struct btrfs_balance_args data;
582c9e9f97bSIlya Dryomov 	struct btrfs_balance_args meta;
583c9e9f97bSIlya Dryomov 	struct btrfs_balance_args sys;
584c9e9f97bSIlya Dryomov 
585c9e9f97bSIlya Dryomov 	u64 flags;
58619a39dceSIlya Dryomov 
58719a39dceSIlya Dryomov 	struct btrfs_balance_progress stat;
588c9e9f97bSIlya Dryomov };
589c9e9f97bSIlya Dryomov 
590562d7b15SJosef Bacik /*
591562d7b15SJosef Bacik  * Search for a given device by the set parameters
592562d7b15SJosef Bacik  */
593562d7b15SJosef Bacik struct btrfs_dev_lookup_args {
594562d7b15SJosef Bacik 	u64 devid;
595562d7b15SJosef Bacik 	u8 *uuid;
596562d7b15SJosef Bacik 	u8 *fsid;
597562d7b15SJosef Bacik 	bool missing;
598562d7b15SJosef Bacik };
599562d7b15SJosef Bacik 
600562d7b15SJosef Bacik /* We have to initialize to -1 because BTRFS_DEV_REPLACE_DEVID is 0 */
601562d7b15SJosef Bacik #define BTRFS_DEV_LOOKUP_ARGS_INIT { .devid = (u64)-1 }
602562d7b15SJosef Bacik 
603562d7b15SJosef Bacik #define BTRFS_DEV_LOOKUP_ARGS(name) \
604562d7b15SJosef Bacik 	struct btrfs_dev_lookup_args name = BTRFS_DEV_LOOKUP_ARGS_INIT
605562d7b15SJosef Bacik 
606cf8cddd3SChristoph Hellwig enum btrfs_map_op {
607cf8cddd3SChristoph Hellwig 	BTRFS_MAP_READ,
608cf8cddd3SChristoph Hellwig 	BTRFS_MAP_WRITE,
609cf8cddd3SChristoph Hellwig 	BTRFS_MAP_DISCARD,
610cf8cddd3SChristoph Hellwig 	BTRFS_MAP_GET_READ_MIRRORS,
611cf8cddd3SChristoph Hellwig };
612cf8cddd3SChristoph Hellwig 
613cf8cddd3SChristoph Hellwig static inline enum btrfs_map_op btrfs_op(struct bio *bio)
614cf8cddd3SChristoph Hellwig {
615cf8cddd3SChristoph Hellwig 	switch (bio_op(bio)) {
616cf8cddd3SChristoph Hellwig 	case REQ_OP_DISCARD:
617cf8cddd3SChristoph Hellwig 		return BTRFS_MAP_DISCARD;
618cf8cddd3SChristoph Hellwig 	case REQ_OP_WRITE:
619cfe94440SNaohiro Aota 	case REQ_OP_ZONE_APPEND:
620cf8cddd3SChristoph Hellwig 		return BTRFS_MAP_WRITE;
621cf8cddd3SChristoph Hellwig 	default:
622cf8cddd3SChristoph Hellwig 		WARN_ON_ONCE(1);
623c730ae0cSMarcos Paulo de Souza 		fallthrough;
624cf8cddd3SChristoph Hellwig 	case REQ_OP_READ:
625cf8cddd3SChristoph Hellwig 		return BTRFS_MAP_READ;
626cf8cddd3SChristoph Hellwig 	}
627cf8cddd3SChristoph Hellwig }
628cf8cddd3SChristoph Hellwig 
629b31bed17SJosef Bacik static inline unsigned long btrfs_chunk_item_size(int num_stripes)
630b31bed17SJosef Bacik {
631b31bed17SJosef Bacik 	ASSERT(num_stripes);
632b31bed17SJosef Bacik 	return sizeof(struct btrfs_chunk) +
633b31bed17SJosef Bacik 		sizeof(struct btrfs_stripe) * (num_stripes - 1);
634b31bed17SJosef Bacik }
635b31bed17SJosef Bacik 
6364c664611SQu Wenruo void btrfs_get_bioc(struct btrfs_io_context *bioc);
6374c664611SQu Wenruo void btrfs_put_bioc(struct btrfs_io_context *bioc);
638cf8cddd3SChristoph Hellwig int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
639cea9e445SChris Mason 		    u64 logical, u64 *length,
6404c664611SQu Wenruo 		    struct btrfs_io_context **bioc_ret, int mirror_num);
641cf8cddd3SChristoph Hellwig int btrfs_map_sblock(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
642af8e2d1dSMiao Xie 		     u64 logical, u64 *length,
6434c664611SQu Wenruo 		     struct btrfs_io_context **bioc_ret);
644a4012f06SChristoph Hellwig struct btrfs_discard_stripe *btrfs_map_discard(struct btrfs_fs_info *fs_info,
645a4012f06SChristoph Hellwig 					       u64 logical, u64 *length_ret,
646a4012f06SChristoph Hellwig 					       u32 *num_stripes);
64742034313SMichal Rostecki int btrfs_get_io_geometry(struct btrfs_fs_info *fs_info, struct extent_map *map,
64843c0d1a5SQu Wenruo 			  enum btrfs_map_op op, u64 logical,
64942034313SMichal Rostecki 			  struct btrfs_io_geometry *io_geom);
6506bccf3abSJeff Mahoney int btrfs_read_sys_array(struct btrfs_fs_info *fs_info);
6515b4aacefSJeff Mahoney int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info);
652f6f39f7aSNikolay Borisov struct btrfs_block_group *btrfs_create_chunk(struct btrfs_trans_handle *trans,
65379bd3712SFilipe Manana 					    u64 type);
654c8bf1b67SDavid Sterba void btrfs_mapping_tree_free(struct extent_map_tree *tree);
6551a722d8fSChristoph Hellwig void btrfs_submit_bio(struct btrfs_fs_info *fs_info, struct bio *bio, int mirror_num);
6568a4b83ccSChris Mason int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
65797288f2cSChristoph Hellwig 		       fmode_t flags, void *holder);
65836350e95SGu Jinxiang struct btrfs_device *btrfs_scan_one_device(const char *path,
65936350e95SGu Jinxiang 					   fmode_t flags, void *holder);
66016cab91aSAnand Jain int btrfs_forget_devices(dev_t devt);
66154eed6aeSNikolay Borisov void btrfs_close_devices(struct btrfs_fs_devices *fs_devices);
662bacce86aSAnand Jain void btrfs_free_extra_devids(struct btrfs_fs_devices *fs_devices);
663d6507cf1SNikolay Borisov void btrfs_assign_next_active_device(struct btrfs_device *device,
664d6507cf1SNikolay Borisov 				     struct btrfs_device *this_dev);
665a27a94c2SNikolay Borisov struct btrfs_device *btrfs_find_device_by_devspec(struct btrfs_fs_info *fs_info,
666a27a94c2SNikolay Borisov 						  u64 devid,
667a27a94c2SNikolay Borisov 						  const char *devpath);
668faa775c4SJosef Bacik int btrfs_get_dev_args_from_path(struct btrfs_fs_info *fs_info,
669faa775c4SJosef Bacik 				 struct btrfs_dev_lookup_args *args,
670faa775c4SJosef Bacik 				 const char *path);
67112bd2fc0SIlya Dryomov struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
672bb21e302SAnand Jain 					const u64 *devid, const u8 *uuid,
673bb21e302SAnand Jain 					const char *path);
674faa775c4SJosef Bacik void btrfs_put_dev_args_from_path(struct btrfs_dev_lookup_args *args);
675a425f9d4SDavid Sterba void btrfs_free_device(struct btrfs_device *device);
6762ff7e61eSJeff Mahoney int btrfs_rm_device(struct btrfs_fs_info *fs_info,
6771a15eb72SJosef Bacik 		    struct btrfs_dev_lookup_args *args,
6783fa421deSJosef Bacik 		    struct block_device **bdev, fmode_t *mode);
679ffc5a379SDavid Sterba void __exit btrfs_cleanup_fs_uuids(void);
6805d964051SStefan Behrens int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len);
6818f18cf13SChris Mason int btrfs_grow_device(struct btrfs_trans_handle *trans,
6828f18cf13SChris Mason 		      struct btrfs_device *device, u64 new_size);
683562d7b15SJosef Bacik struct btrfs_device *btrfs_find_device(const struct btrfs_fs_devices *fs_devices,
684562d7b15SJosef Bacik 				       const struct btrfs_dev_lookup_args *args);
6858f18cf13SChris Mason int btrfs_shrink_device(struct btrfs_device *device, u64 new_size);
686da353f6bSDavid Sterba int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *path);
6876fcf6e2bSDavid Sterba int btrfs_balance(struct btrfs_fs_info *fs_info,
6886fcf6e2bSDavid Sterba 		  struct btrfs_balance_control *bctl,
689c9e9f97bSIlya Dryomov 		  struct btrfs_ioctl_balance_args *bargs);
690f89e09cfSAnand Jain void btrfs_describe_block_groups(u64 flags, char *buf, u32 size_buf);
6912b6ba629SIlya Dryomov int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info);
69268310a5eSIlya Dryomov int btrfs_recover_balance(struct btrfs_fs_info *fs_info);
693837d5b6eSIlya Dryomov int btrfs_pause_balance(struct btrfs_fs_info *fs_info);
69418bb8bbfSJohannes Thumshirn int btrfs_relocate_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset);
695a7e99c69SIlya Dryomov int btrfs_cancel_balance(struct btrfs_fs_info *fs_info);
696f7a81ea4SStefan Behrens int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info);
69797f4dd09SNikolay Borisov int btrfs_uuid_scan_kthread(void *data);
698a09f23c3SAnand Jain bool btrfs_chunk_writeable(struct btrfs_fs_info *fs_info, u64 chunk_offset);
69960dfdf25SNikolay Borisov int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes,
700ba1bf481SJosef Bacik 			 u64 *start, u64 *max_avail);
701442a4f63SStefan Behrens void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index);
7022ff7e61eSJeff Mahoney int btrfs_get_dev_stats(struct btrfs_fs_info *fs_info,
703b27f7c0cSDavid Sterba 			struct btrfs_ioctl_get_dev_stats *stats);
704a8d1b164SJohannes Thumshirn int btrfs_init_devices_late(struct btrfs_fs_info *fs_info);
705733f4fbbSStefan Behrens int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info);
706196c9d8dSDavid Sterba int btrfs_run_dev_stats(struct btrfs_trans_handle *trans);
70768a9db5fSNikolay Borisov void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_device *srcdev);
70865237ee3SDavid Sterba void btrfs_rm_dev_replace_free_srcdev(struct btrfs_device *srcdev);
7094f5ad7bdSNikolay Borisov void btrfs_destroy_dev_replace_tgtdev(struct btrfs_device *tgtdev);
710592d92eeSLiu Bo int btrfs_is_parity_mirror(struct btrfs_fs_info *fs_info,
711e4ff5fb5SNikolay Borisov 			   u64 logical, u64 len);
7122ff7e61eSJeff Mahoney unsigned long btrfs_full_stripe_len(struct btrfs_fs_info *fs_info,
71353b381b3SDavid Woodhouse 				    u64 logical);
714bc88b486SQu Wenruo u64 btrfs_calc_stripe_length(const struct extent_map *em);
7150b30f719SQu Wenruo int btrfs_nr_parity_stripes(u64 type);
71679bd3712SFilipe Manana int btrfs_chunk_alloc_add_chunk_item(struct btrfs_trans_handle *trans,
71779bd3712SFilipe Manana 				     struct btrfs_block_group *bg);
71897aff912SNikolay Borisov int btrfs_remove_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset);
71960ca842eSOmar Sandoval struct extent_map *btrfs_get_chunk_map(struct btrfs_fs_info *fs_info,
72060ca842eSOmar Sandoval 				       u64 logical, u64 length);
7218f32380dSJohannes Thumshirn void btrfs_release_disk_super(struct btrfs_super_block *super);
722addc3fa7SMiao Xie 
723442a4f63SStefan Behrens static inline void btrfs_dev_stat_inc(struct btrfs_device *dev,
724442a4f63SStefan Behrens 				      int index)
725442a4f63SStefan Behrens {
726442a4f63SStefan Behrens 	atomic_inc(dev->dev_stat_values + index);
7279deae968SNikolay Borisov 	/*
7289deae968SNikolay Borisov 	 * This memory barrier orders stores updating statistics before stores
7299deae968SNikolay Borisov 	 * updating dev_stats_ccnt.
7309deae968SNikolay Borisov 	 *
7319deae968SNikolay Borisov 	 * It pairs with smp_rmb() in btrfs_run_dev_stats().
7329deae968SNikolay Borisov 	 */
733addc3fa7SMiao Xie 	smp_mb__before_atomic();
734addc3fa7SMiao Xie 	atomic_inc(&dev->dev_stats_ccnt);
735442a4f63SStefan Behrens }
736442a4f63SStefan Behrens 
737442a4f63SStefan Behrens static inline int btrfs_dev_stat_read(struct btrfs_device *dev,
738442a4f63SStefan Behrens 				      int index)
739442a4f63SStefan Behrens {
740442a4f63SStefan Behrens 	return atomic_read(dev->dev_stat_values + index);
741442a4f63SStefan Behrens }
742442a4f63SStefan Behrens 
743442a4f63SStefan Behrens static inline int btrfs_dev_stat_read_and_reset(struct btrfs_device *dev,
744442a4f63SStefan Behrens 						int index)
745442a4f63SStefan Behrens {
746442a4f63SStefan Behrens 	int ret;
747442a4f63SStefan Behrens 
748442a4f63SStefan Behrens 	ret = atomic_xchg(dev->dev_stat_values + index, 0);
7494660c49fSNikolay Borisov 	/*
7504660c49fSNikolay Borisov 	 * atomic_xchg implies a full memory barriers as per atomic_t.txt:
7514660c49fSNikolay Borisov 	 * - RMW operations that have a return value are fully ordered;
7524660c49fSNikolay Borisov 	 *
7534660c49fSNikolay Borisov 	 * This implicit memory barriers is paired with the smp_rmb in
7544660c49fSNikolay Borisov 	 * btrfs_run_dev_stats
7554660c49fSNikolay Borisov 	 */
756addc3fa7SMiao Xie 	atomic_inc(&dev->dev_stats_ccnt);
757442a4f63SStefan Behrens 	return ret;
758442a4f63SStefan Behrens }
759442a4f63SStefan Behrens 
760442a4f63SStefan Behrens static inline void btrfs_dev_stat_set(struct btrfs_device *dev,
761442a4f63SStefan Behrens 				      int index, unsigned long val)
762442a4f63SStefan Behrens {
763442a4f63SStefan Behrens 	atomic_set(dev->dev_stat_values + index, val);
7649deae968SNikolay Borisov 	/*
7659deae968SNikolay Borisov 	 * This memory barrier orders stores updating statistics before stores
7669deae968SNikolay Borisov 	 * updating dev_stats_ccnt.
7679deae968SNikolay Borisov 	 *
7689deae968SNikolay Borisov 	 * It pairs with smp_rmb() in btrfs_run_dev_stats().
7699deae968SNikolay Borisov 	 */
770addc3fa7SMiao Xie 	smp_mb__before_atomic();
771addc3fa7SMiao Xie 	atomic_inc(&dev->dev_stats_ccnt);
772442a4f63SStefan Behrens }
773442a4f63SStefan Behrens 
774cb3e217bSQu Wenruo static inline const char *btrfs_dev_name(const struct btrfs_device *device)
775cb3e217bSQu Wenruo {
776cb3e217bSQu Wenruo 	if (!device || test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
777cb3e217bSQu Wenruo 		return "<missing disk>";
778cb3e217bSQu Wenruo 	else
779cb3e217bSQu Wenruo 		return rcu_str_deref(device->name);
780cb3e217bSQu Wenruo }
781cb3e217bSQu Wenruo 
782bbbf7243SNikolay Borisov void btrfs_commit_device_sizes(struct btrfs_transaction *trans);
78304216820SFilipe Manana 
7844143cb8bSDavid Sterba struct list_head * __attribute_const__ btrfs_get_fs_uuids(void);
7856528b99dSAnand Jain bool btrfs_check_rw_degradable(struct btrfs_fs_info *fs_info,
7866528b99dSAnand Jain 					struct btrfs_device *failing_dev);
787313b0858SJosef Bacik void btrfs_scratch_superblocks(struct btrfs_fs_info *fs_info,
788313b0858SJosef Bacik 			       struct block_device *bdev,
789313b0858SJosef Bacik 			       const char *device_path);
79021634a19SQu Wenruo 
791500a44c9SDavid Sterba enum btrfs_raid_types __attribute_const__ btrfs_bg_flags_to_raid_index(u64 flags);
79246df06b8SDavid Sterba int btrfs_bg_type_to_factor(u64 flags);
793158da513SDavid Sterba const char *btrfs_bg_type_to_raid_name(u64 flags);
794cf90d884SQu Wenruo int btrfs_verify_dev_extents(struct btrfs_fs_info *fs_info);
795554aed7dSJohannes Thumshirn bool btrfs_repair_one_zone(struct btrfs_fs_info *fs_info, u64 logical);
79646df06b8SDavid Sterba 
797c2e79e86SJosef Bacik bool btrfs_pinned_by_swapfile(struct btrfs_fs_info *fs_info, void *ptr);
798c2e79e86SJosef Bacik 
7990b86a832SChris Mason #endif
800