xref: /linux/fs/btrfs/volumes.h (revision 95e3b8dc32179b9d2895fca5605d11a763e9d658)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Copyright (C) 2007 Oracle.  All rights reserved.
4  */
5 
6 #ifndef BTRFS_VOLUMES_H
7 #define BTRFS_VOLUMES_H
8 
9 #include <linux/blk_types.h>
10 #include <linux/blkdev.h>
11 #include <linux/sizes.h>
12 #include <linux/atomic.h>
13 #include <linux/sort.h>
14 #include <linux/list.h>
15 #include <linux/mutex.h>
16 #include <linux/log2.h>
17 #include <linux/kobject.h>
18 #include <linux/refcount.h>
19 #include <linux/completion.h>
20 #include <linux/rbtree.h>
21 #include <uapi/linux/btrfs.h>
22 #include <uapi/linux/btrfs_tree.h>
23 #include "messages.h"
24 #include "extent-io-tree.h"
25 #include "fs.h"
26 
27 struct btrfs_block_group;
28 struct btrfs_trans_handle;
29 struct btrfs_transaction;
30 struct btrfs_zoned_device_info;
31 struct btrfs_space_info;
32 
33 #define BTRFS_MAX_DATA_CHUNK_SIZE	(10ULL * SZ_1G)
34 
35 /*
36  * Arbitrary maximum size of one discard request to limit potentially long time
37  * spent in blkdev_issue_discard().
38  */
39 #define BTRFS_MAX_DISCARD_CHUNK_SIZE	(SZ_1G)
40 
41 extern struct mutex uuid_mutex;
42 
43 #define BTRFS_STRIPE_LEN		SZ_64K
44 #define BTRFS_STRIPE_LEN_SHIFT		(16)
45 #define BTRFS_STRIPE_LEN_MASK		(BTRFS_STRIPE_LEN - 1)
46 
47 static_assert(ilog2(BTRFS_STRIPE_LEN) == BTRFS_STRIPE_LEN_SHIFT);
48 
49 /* Used by sanity check for btrfs_raid_types. */
50 #define const_ffs(n) (__builtin_ctzll(n) + 1)
51 
52 /*
53  * The conversion from BTRFS_BLOCK_GROUP_* bits to btrfs_raid_type requires
54  * RAID0 always to be the lowest profile bit.
55  * Although it's part of on-disk format and should never change, do extra
56  * compile-time sanity checks.
57  */
58 static_assert(const_ffs(BTRFS_BLOCK_GROUP_RAID0) <
59 	      const_ffs(BTRFS_BLOCK_GROUP_PROFILE_MASK & ~BTRFS_BLOCK_GROUP_RAID0));
60 
61 /* ilog2() can handle both constants and variables */
62 #define BTRFS_BG_FLAG_TO_INDEX(profile)					\
63 	ilog2((profile) >> (ilog2(BTRFS_BLOCK_GROUP_RAID0) - 1))
64 
65 enum btrfs_raid_types {
66 	/* SINGLE is the special one as it doesn't have on-disk bit. */
67 	BTRFS_RAID_SINGLE  = 0,
68 
69 	BTRFS_RAID_RAID0   = BTRFS_BG_FLAG_TO_INDEX(BTRFS_BLOCK_GROUP_RAID0),
70 	BTRFS_RAID_RAID1   = BTRFS_BG_FLAG_TO_INDEX(BTRFS_BLOCK_GROUP_RAID1),
71 	BTRFS_RAID_DUP	   = BTRFS_BG_FLAG_TO_INDEX(BTRFS_BLOCK_GROUP_DUP),
72 	BTRFS_RAID_RAID10  = BTRFS_BG_FLAG_TO_INDEX(BTRFS_BLOCK_GROUP_RAID10),
73 	BTRFS_RAID_RAID5   = BTRFS_BG_FLAG_TO_INDEX(BTRFS_BLOCK_GROUP_RAID5),
74 	BTRFS_RAID_RAID6   = BTRFS_BG_FLAG_TO_INDEX(BTRFS_BLOCK_GROUP_RAID6),
75 	BTRFS_RAID_RAID1C3 = BTRFS_BG_FLAG_TO_INDEX(BTRFS_BLOCK_GROUP_RAID1C3),
76 	BTRFS_RAID_RAID1C4 = BTRFS_BG_FLAG_TO_INDEX(BTRFS_BLOCK_GROUP_RAID1C4),
77 
78 	BTRFS_NR_RAID_TYPES
79 };
80 
81 static_assert(BTRFS_RAID_RAID0 == 1);
82 static_assert(BTRFS_RAID_RAID1 == 2);
83 static_assert(BTRFS_RAID_DUP == 3);
84 static_assert(BTRFS_RAID_RAID10 == 4);
85 static_assert(BTRFS_RAID_RAID5 == 5);
86 static_assert(BTRFS_RAID_RAID6 == 6);
87 static_assert(BTRFS_RAID_RAID1C3 == 7);
88 static_assert(BTRFS_RAID_RAID1C4 == 8);
89 
90 /*
91  * Use sequence counter to get consistent device stat data on
92  * 32-bit processors.
93  */
94 #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
95 #include <linux/seqlock.h>
96 #define __BTRFS_NEED_DEVICE_DATA_ORDERED
97 #define btrfs_device_data_ordered_init(device)	\
98 	seqcount_init(&device->data_seqcount)
99 #else
100 #define btrfs_device_data_ordered_init(device) do { } while (0)
101 #endif
102 
103 #define BTRFS_DEV_STATE_WRITEABLE	(0)
104 #define BTRFS_DEV_STATE_IN_FS_METADATA	(1)
105 #define BTRFS_DEV_STATE_MISSING		(2)
106 #define BTRFS_DEV_STATE_REPLACE_TGT	(3)
107 #define BTRFS_DEV_STATE_FLUSH_SENT	(4)
108 #define BTRFS_DEV_STATE_NO_READA	(5)
109 #define BTRFS_DEV_STATE_FLUSH_FAILED	(6)
110 
111 /* Set when the device item is found in chunk tree, used to catch unexpected registered device. */
112 #define BTRFS_DEV_STATE_ITEM_FOUND	(7)
113 
114 /* Special value encoding failure to write primary super block. */
115 #define BTRFS_SUPER_PRIMARY_WRITE_ERROR		(INT_MAX / 2)
116 
117 struct btrfs_fs_devices;
118 
119 struct btrfs_device {
120 	struct list_head dev_list; /* device_list_mutex */
121 	struct list_head dev_alloc_list; /* chunk mutex */
122 	struct list_head post_commit_list; /* chunk mutex */
123 	struct btrfs_fs_devices *fs_devices;
124 	struct btrfs_fs_info *fs_info;
125 
126 	/* Device path or NULL if missing. */
127 	const char __rcu *name;
128 
129 	u64 generation;
130 
131 	struct file *bdev_file;
132 	struct block_device *bdev;
133 
134 	struct btrfs_zoned_device_info *zone_info;
135 
136 	unsigned long dev_state;
137 
138 #ifdef __BTRFS_NEED_DEVICE_DATA_ORDERED
139 	seqcount_t data_seqcount;
140 #endif
141 
142 	/* the internal btrfs device id */
143 	u64 devid;
144 
145 	/* size of the device in memory */
146 	u64 total_bytes;
147 
148 	/* size of the device on disk */
149 	u64 disk_total_bytes;
150 
151 	/* bytes used */
152 	u64 bytes_used;
153 
154 	/* optimal io alignment for this device */
155 	u32 io_align;
156 
157 	/* optimal io width for this device */
158 	u32 io_width;
159 	/* type and info about this device */
160 	u64 type;
161 
162 	/*
163 	 * Counter of super block write errors, values larger than
164 	 * BTRFS_SUPER_PRIMARY_WRITE_ERROR encode primary super block write failure.
165 	 */
166 	atomic_t sb_write_errors;
167 
168 	/* minimal io size for this device */
169 	u32 sector_size;
170 
171 	/* physical drive uuid (or lvm uuid) */
172 	u8 uuid[BTRFS_UUID_SIZE];
173 
174 	/*
175 	 * size of the device on the current transaction
176 	 *
177 	 * This variant is update when committing the transaction,
178 	 * and protected by chunk mutex
179 	 */
180 	u64 commit_total_bytes;
181 
182 	/* bytes used on the current transaction */
183 	u64 commit_bytes_used;
184 
185 	/* Bio used for flushing device barriers */
186 	struct bio flush_bio;
187 	struct completion flush_wait;
188 
189 	/* per-device scrub information */
190 	struct scrub_ctx *scrub_ctx;
191 
192 	/* disk I/O failure stats. For detailed description refer to
193 	 * enum btrfs_dev_stat_values in ioctl.h */
194 	int dev_stats_valid;
195 
196 	/* Counter to record the change of device stats */
197 	atomic_t dev_stats_ccnt;
198 	atomic_t dev_stat_values[BTRFS_DEV_STAT_VALUES_MAX];
199 
200 	/*
201 	 * Device's major-minor number. Must be set even if the device is not
202 	 * opened (bdev == NULL), unless the device is missing.
203 	 */
204 	dev_t devt;
205 
206 	struct extent_io_tree alloc_state;
207 
208 	struct completion kobj_unregister;
209 	/* For sysfs/FSID/devinfo/devid/ */
210 	struct kobject devid_kobj;
211 
212 	/* Bandwidth limit for scrub, in bytes */
213 	u64 scrub_speed_max;
214 
215 	/*
216 	 * A temporary number of allocated space during per-profile
217 	 * available space calculation.
218 	 */
219 	u64 per_profile_allocated;
220 };
221 
222 /*
223  * Block group or device which contains an active swapfile. Used for preventing
224  * unsafe operations while a swapfile is active.
225  *
226  * These are sorted on (ptr, inode) (note that a block group or device can
227  * contain more than one swapfile). We compare the pointer values because we
228  * don't actually care what the object is, we just need a quick check whether
229  * the object exists in the rbtree.
230  */
231 struct btrfs_swapfile_pin {
232 	struct rb_node node;
233 	void *ptr;
234 	struct inode *inode;
235 	/*
236 	 * If true, ptr points to a struct btrfs_block_group. Otherwise, ptr
237 	 * points to a struct btrfs_device.
238 	 */
239 	bool is_block_group;
240 	/*
241 	 * Only used when 'is_block_group' is true and it is the number of
242 	 * extents used by a swapfile for this block group ('ptr' field).
243 	 */
244 	int bg_extent_count;
245 };
246 
247 /*
248  * If we read those variants at the context of their own lock, we needn't
249  * use the following helpers, reading them directly is safe.
250  */
251 #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
252 #define BTRFS_DEVICE_GETSET_FUNCS(name)					\
253 static inline u64							\
254 btrfs_device_get_##name(const struct btrfs_device *dev)			\
255 {									\
256 	u64 size;							\
257 	unsigned int seq;						\
258 									\
259 	do {								\
260 		seq = read_seqcount_begin(&dev->data_seqcount);		\
261 		size = dev->name;					\
262 	} while (read_seqcount_retry(&dev->data_seqcount, seq));	\
263 	return size;							\
264 }									\
265 									\
266 static inline void							\
267 btrfs_device_set_##name(struct btrfs_device *dev, u64 size)		\
268 {									\
269 	preempt_disable();						\
270 	write_seqcount_begin(&dev->data_seqcount);			\
271 	dev->name = size;						\
272 	write_seqcount_end(&dev->data_seqcount);			\
273 	preempt_enable();						\
274 }
275 #elif BITS_PER_LONG==32 && defined(CONFIG_PREEMPTION)
276 #define BTRFS_DEVICE_GETSET_FUNCS(name)					\
277 static inline u64							\
278 btrfs_device_get_##name(const struct btrfs_device *dev)			\
279 {									\
280 	u64 size;							\
281 									\
282 	preempt_disable();						\
283 	size = dev->name;						\
284 	preempt_enable();						\
285 	return size;							\
286 }									\
287 									\
288 static inline void							\
289 btrfs_device_set_##name(struct btrfs_device *dev, u64 size)		\
290 {									\
291 	preempt_disable();						\
292 	dev->name = size;						\
293 	preempt_enable();						\
294 }
295 #else
296 #define BTRFS_DEVICE_GETSET_FUNCS(name)					\
297 static inline u64							\
298 btrfs_device_get_##name(const struct btrfs_device *dev)			\
299 {									\
300 	return dev->name;						\
301 }									\
302 									\
303 static inline void							\
304 btrfs_device_set_##name(struct btrfs_device *dev, u64 size)		\
305 {									\
306 	dev->name = size;						\
307 }
308 #endif
309 
310 BTRFS_DEVICE_GETSET_FUNCS(total_bytes);
311 BTRFS_DEVICE_GETSET_FUNCS(disk_total_bytes);
312 BTRFS_DEVICE_GETSET_FUNCS(bytes_used);
313 
314 enum btrfs_chunk_allocation_policy {
315 	BTRFS_CHUNK_ALLOC_REGULAR,
316 	BTRFS_CHUNK_ALLOC_ZONED,
317 };
318 
319 #define BTRFS_DEFAULT_RR_MIN_CONTIG_READ	(SZ_256K)
320 /* Keep in sync with raid_attr table, current maximum is RAID1C4. */
321 #define BTRFS_RAID1_MAX_MIRRORS			(4)
322 /*
323  * Read policies for mirrored block group profiles, read picks the stripe based
324  * on these policies.
325  */
326 enum btrfs_read_policy {
327 	/* Use process PID to choose the stripe */
328 	BTRFS_READ_POLICY_PID,
329 #ifdef CONFIG_BTRFS_EXPERIMENTAL
330 	/* Balancing RAID1 reads across all striped devices (round-robin). */
331 	BTRFS_READ_POLICY_RR,
332 	/* Read from a specific device. */
333 	BTRFS_READ_POLICY_DEVID,
334 #endif
335 	BTRFS_NR_READ_POLICY,
336 };
337 
338 struct btrfs_fs_devices {
339 	u8 fsid[BTRFS_FSID_SIZE]; /* FS specific uuid */
340 
341 	/*
342 	 * UUID written into the btree blocks:
343 	 *
344 	 * - If metadata_uuid != fsid then super block must have
345 	 *   BTRFS_FEATURE_INCOMPAT_METADATA_UUID flag set.
346 	 *
347 	 * - Following shall be true at all times:
348 	 *   - metadata_uuid == btrfs_header::fsid
349 	 *   - metadata_uuid == btrfs_dev_item::fsid
350 	 *
351 	 * - Relations between fsid and metadata_uuid in sb and fs_devices:
352 	 *   - Normal:
353 	 *       fs_devices->fsid == fs_devices->metadata_uuid == sb->fsid
354 	 *       sb->metadata_uuid == 0
355 	 *
356 	 *   - When the BTRFS_FEATURE_INCOMPAT_METADATA_UUID flag is set:
357 	 *       fs_devices->fsid == sb->fsid
358 	 *       fs_devices->metadata_uuid == sb->metadata_uuid
359 	 *
360 	 *   - When in-memory fs_devices->temp_fsid is true
361 	 *	 fs_devices->fsid = random
362 	 *	 fs_devices->metadata_uuid == sb->fsid
363 	 */
364 	u8 metadata_uuid[BTRFS_FSID_SIZE];
365 
366 	struct list_head fs_list;
367 
368 	/*
369 	 * Number of devices under this fsid including missing and
370 	 * replace-target device and excludes seed devices.
371 	 */
372 	u64 num_devices;
373 
374 	/*
375 	 * The number of devices that successfully opened, including
376 	 * replace-target, excludes seed devices.
377 	 */
378 	u64 open_devices;
379 
380 	/* The number of devices that are under the chunk allocation list. */
381 	u64 rw_devices;
382 
383 	/* Count of missing devices under this fsid excluding seed device. */
384 	u64 missing_devices;
385 	u64 total_rw_bytes;
386 
387 	/*
388 	 * Count of devices from btrfs_super_block::num_devices for this fsid,
389 	 * which includes the seed device, excludes the transient replace-target
390 	 * device.
391 	 */
392 	u64 total_devices;
393 
394 	/* Highest generation number of seen devices */
395 	u64 latest_generation;
396 
397 	/*
398 	 * The mount device or a device with highest generation after removal
399 	 * or replace.
400 	 */
401 	struct btrfs_device *latest_dev;
402 
403 	/*
404 	 * All of the devices in the filesystem, protected by a mutex so we can
405 	 * safely walk it to write out the super blocks without worrying about
406 	 * adding/removing by the multi-device code. Scrubbing super block can
407 	 * kick off supers writing by holding this mutex lock.
408 	 */
409 	struct mutex device_list_mutex;
410 
411 	/* List of all devices, protected by device_list_mutex */
412 	struct list_head devices;
413 
414 	/* Devices which can satisfy space allocation. Protected by * chunk_mutex. */
415 	struct list_head alloc_list;
416 
417 	struct list_head seed_list;
418 
419 	/* Count fs-devices opened. */
420 	int opened;
421 
422 	/*
423 	 * Counter of the processes that are holding this fs_devices but not
424 	 * yet opened.
425 	 * This is for mounting handling, as we can only open the fs_devices
426 	 * after a super block is created.  But we cannot take uuid_mutex
427 	 * during sget_fc(), thus we have to hold the fs_devices (meaning it
428 	 * cannot be released) until a super block is returned.
429 	 */
430 	int holding;
431 
432 	/* Set when we find or add a device that doesn't have the nonrot flag set. */
433 	bool rotating;
434 	/* Devices support TRIM/discard commands. */
435 	bool discardable;
436 	/* The filesystem is a seed filesystem. */
437 	bool seeding;
438 	/* The mount needs to use a randomly generated fsid. */
439 	bool temp_fsid;
440 	/* Enable/disable the filesystem stats tracking. */
441 	bool collect_fs_stats;
442 
443 	struct btrfs_fs_info *fs_info;
444 	/* sysfs kobjects */
445 	struct kobject fsid_kobj;
446 	struct kobject *devices_kobj;
447 	struct kobject *devinfo_kobj;
448 	struct completion kobj_unregister;
449 
450 	enum btrfs_chunk_allocation_policy chunk_alloc_policy;
451 
452 	/* Policy used to read the mirrored stripes. */
453 	enum btrfs_read_policy read_policy;
454 
455 #ifdef CONFIG_BTRFS_EXPERIMENTAL
456 	/*
457 	 * Minimum contiguous reads before switching to next device, the unit
458 	 * is one block/sectorsize.
459 	 */
460 	u32 rr_min_contig_read;
461 
462 	/* Device to be used for reading in case of RAID1. */
463 	u64 read_devid;
464 #endif
465 
466 	/*
467 	 * Each value indicates the available space for that profile.
468 	 * U64_MAX means the estimation is unavailable.
469 	 *
470 	 * Protected by per_profile_lock;
471 	 */
472 	u64 per_profile_avail[BTRFS_NR_RAID_TYPES];
473 	spinlock_t per_profile_lock;
474 };
475 
476 #define BTRFS_MAX_DEVS(info) ((BTRFS_MAX_ITEM_SIZE(info)	\
477 			- sizeof(struct btrfs_chunk))		\
478 			/ sizeof(struct btrfs_stripe) + 1)
479 
480 #define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE	\
481 				- 2 * sizeof(struct btrfs_disk_key)	\
482 				- 2 * sizeof(struct btrfs_chunk))	\
483 				/ sizeof(struct btrfs_stripe) + 1)
484 
485 struct btrfs_io_stripe {
486 	struct btrfs_device *dev;
487 	/* Block mapping. */
488 	u64 physical;
489 	bool rst_search_commit_root;
490 	/* For the endio handler. */
491 	struct btrfs_io_context *bioc;
492 };
493 
494 struct btrfs_discard_stripe {
495 	struct btrfs_device *dev;
496 	u64 physical;
497 	u64 length;
498 };
499 
500 /*
501  * Context for IO submission for device stripe.
502  *
503  * - Track the unfinished mirrors for mirror based profiles
504  *   Mirror based profiles are SINGLE/DUP/RAID1/RAID10.
505  *
506  * - Contain the logical -> physical mapping info
507  *   Used by submit_stripe_bio() for mapping logical bio
508  *   into physical device address.
509  *
510  * - Contain device replace info
511  *   Used by handle_ops_on_dev_replace() to copy logical bios
512  *   into the new device.
513  *
514  * - Contain RAID56 full stripe logical bytenrs
515  */
516 struct btrfs_io_context {
517 	refcount_t refs;
518 	struct btrfs_fs_info *fs_info;
519 	/* Taken from struct btrfs_chunk_map::type. */
520 	u64 map_type;
521 	struct bio *orig_bio;
522 	atomic_t error;
523 	u16 max_errors;
524 	bool use_rst;
525 
526 	u64 logical;
527 	u64 size;
528 	/* Raid stripe tree ordered entry. */
529 	struct list_head rst_ordered_entry;
530 
531 	/*
532 	 * The total number of stripes, including the extra duplicated
533 	 * stripe for replace.
534 	 */
535 	u16 num_stripes;
536 
537 	/*
538 	 * The mirror_num of this bioc.
539 	 *
540 	 * This is for reads which use 0 as mirror_num, thus we should return a
541 	 * valid mirror_num (>0) for the reader.
542 	 */
543 	u16 mirror_num;
544 
545 	/*
546 	 * The following two members are for dev-replace case only.
547 	 *
548 	 * @replace_nr_stripes:	Number of duplicated stripes which need to be
549 	 *			written to replace target.
550 	 *			Should be <= 2 (2 for DUP, otherwise <= 1).
551 	 * @replace_stripe_src:	The array indicates where the duplicated stripes
552 	 *			are from.
553 	 *
554 	 * The @replace_stripe_src[] array is mostly for RAID56 cases.
555 	 * As non-RAID56 stripes share the same contents of the mapped range,
556 	 * thus no need to bother where the duplicated ones are from.
557 	 *
558 	 * But for RAID56 case, all stripes contain different contents, thus
559 	 * we need a way to know the mapping.
560 	 *
561 	 * There is an example for the two members, using a RAID5 write:
562 	 *
563 	 *   num_stripes:	4 (3 + 1 duplicated write)
564 	 *   stripes[0]:	dev = devid 1, physical = X
565 	 *   stripes[1]:	dev = devid 2, physical = Y
566 	 *   stripes[2]:	dev = devid 3, physical = Z
567 	 *   stripes[3]:	dev = devid 0, physical = Y
568 	 *
569 	 * replace_nr_stripes = 1
570 	 * replace_stripe_src = 1	<- Means stripes[1] is involved in replace.
571 	 *				   The duplicated stripe index would be
572 	 *				   (@num_stripes - 1).
573 	 *
574 	 * Note, that we can still have cases replace_nr_stripes = 2 for DUP.
575 	 * In that case, all stripes share the same content, thus we don't
576 	 * need to bother @replace_stripe_src value at all.
577 	 */
578 	u16 replace_nr_stripes;
579 	s16 replace_stripe_src;
580 	/*
581 	 * Logical bytenr of the full stripe start, only for RAID56 cases.
582 	 *
583 	 * When this value is set to other than (u64)-1, the stripes[] should
584 	 * follow this pattern:
585 	 *
586 	 * (real_stripes = num_stripes - replace_nr_stripes)
587 	 * (data_stripes = (is_raid6) ? (real_stripes - 2) : (real_stripes - 1))
588 	 *
589 	 * stripes[0]:			The first data stripe
590 	 * stripes[1]:			The second data stripe
591 	 * ...
592 	 * stripes[data_stripes - 1]:	The last data stripe
593 	 * stripes[data_stripes]:	The P stripe
594 	 * stripes[data_stripes + 1]:	The Q stripe (only for RAID6).
595 	 */
596 	u64 full_stripe_logical;
597 	struct btrfs_io_stripe stripes[];
598 };
599 
600 struct btrfs_device_info {
601 	struct btrfs_device *dev;
602 	u64 dev_offset;
603 	u64 max_avail;
604 	u64 total_avail;
605 };
606 
607 struct btrfs_raid_attr {
608 	u8 sub_stripes;		/* sub_stripes info for map */
609 	u8 dev_stripes;		/* stripes per dev */
610 	u8 devs_max;		/* max devs to use */
611 	u8 devs_min;		/* min devs needed */
612 	u8 tolerated_failures;	/* max tolerated fail devs */
613 	u8 devs_increment;	/* ndevs has to be a multiple of this */
614 	u8 ncopies;		/* how many copies to data has */
615 	u8 nparity;		/* number of stripes worth of bytes to store
616 				 * parity information */
617 	u8 mindev_error;	/* error code if min devs requisite is unmet */
618 	const char raid_name[8]; /* name of the raid */
619 	u64 bg_flag;		/* block group flag of the raid */
620 };
621 
622 extern const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES];
623 
624 struct btrfs_chunk_map {
625 	struct rb_node rb_node;
626 	/* For mount time dev extent verification. */
627 	int verified_stripes;
628 	refcount_t refs;
629 	u64 start;
630 	u64 chunk_len;
631 	u64 stripe_size;
632 	/*
633 	 * The real type that is utilized during logical address mapping.
634 	 *
635 	 * For most profiles it matches @on_disk_type, but for single-data-RAID56,
636 	 * the real type will be set to RAID1/RAID1C3, to avoid unsupported
637 	 * operations from raid56 lib.
638 	 */
639 	u64 type;
640 	u64 on_disk_type;
641 	int num_stripes;
642 	int sub_stripes;
643 	struct btrfs_io_stripe stripes[];
644 };
645 
646 #define btrfs_chunk_map_size(n) (sizeof(struct btrfs_chunk_map) + \
647 				 (sizeof(struct btrfs_io_stripe) * (n)))
648 
649 static inline void btrfs_free_chunk_map(struct btrfs_chunk_map *map)
650 {
651 	if (map && refcount_dec_and_test(&map->refs)) {
652 		ASSERT(RB_EMPTY_NODE(&map->rb_node));
653 		kfree(map);
654 	}
655 }
656 DEFINE_FREE(btrfs_free_chunk_map, struct btrfs_chunk_map *, btrfs_free_chunk_map(_T))
657 
658 struct btrfs_balance_control {
659 	struct btrfs_balance_args data;
660 	struct btrfs_balance_args meta;
661 	struct btrfs_balance_args sys;
662 
663 	u64 flags;
664 
665 	struct btrfs_balance_progress stat;
666 };
667 
668 /*
669  * Search for a given device by the set parameters
670  */
671 struct btrfs_dev_lookup_args {
672 	u64 devid;
673 	u8 *uuid;
674 	u8 *fsid;
675 	/*
676 	 * If devt is specified, all other members will be ignored as it is
677 	 * enough to uniquely locate a device.
678 	 */
679 	dev_t devt;
680 	bool missing;
681 };
682 
683 /* We have to initialize to -1 because BTRFS_DEV_REPLACE_DEVID is 0 */
684 #define BTRFS_DEV_LOOKUP_ARGS_INIT { .devid = (u64)-1 }
685 
686 #define BTRFS_DEV_LOOKUP_ARGS(name) \
687 	struct btrfs_dev_lookup_args name = BTRFS_DEV_LOOKUP_ARGS_INIT
688 
689 enum btrfs_map_op {
690 	BTRFS_MAP_READ,
691 	BTRFS_MAP_WRITE,
692 	BTRFS_MAP_GET_READ_MIRRORS,
693 };
694 
695 static inline enum btrfs_map_op btrfs_op(const struct bio *bio)
696 {
697 	switch (bio_op(bio)) {
698 	case REQ_OP_WRITE:
699 	case REQ_OP_ZONE_APPEND:
700 		return BTRFS_MAP_WRITE;
701 	default:
702 		WARN_ON_ONCE(1);
703 		fallthrough;
704 	case REQ_OP_READ:
705 		return BTRFS_MAP_READ;
706 	}
707 }
708 
709 static inline unsigned long btrfs_chunk_item_size(int num_stripes)
710 {
711 	ASSERT(num_stripes);
712 	return sizeof(struct btrfs_chunk) +
713 		sizeof(struct btrfs_stripe) * (num_stripes - 1);
714 }
715 
716 /*
717  * Do the type safe conversion from stripe_nr to offset inside the chunk.
718  *
719  * @stripe_nr is u32, with left shift it can overflow u32 for chunks larger
720  * than 4G.  This does the proper type cast to avoid overflow.
721  */
722 static inline u64 btrfs_stripe_nr_to_offset(u32 stripe_nr)
723 {
724 	return (u64)stripe_nr << BTRFS_STRIPE_LEN_SHIFT;
725 }
726 
727 void btrfs_get_bioc(struct btrfs_io_context *bioc);
728 void btrfs_put_bioc(struct btrfs_io_context *bioc);
729 int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
730 		    u64 logical, u64 *length,
731 		    struct btrfs_io_context **bioc_ret,
732 		    struct btrfs_io_stripe *smap, int *mirror_num_ret);
733 int btrfs_map_repair_block(struct btrfs_fs_info *fs_info,
734 			   struct btrfs_io_stripe *smap, u64 logical,
735 			   u32 length, int mirror_num);
736 struct btrfs_discard_stripe *btrfs_map_discard(struct btrfs_fs_info *fs_info,
737 					       u64 logical, u64 *length_ret,
738 					       u32 *num_stripes, bool do_remap);
739 int btrfs_read_sys_array(struct btrfs_fs_info *fs_info);
740 int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info);
741 struct btrfs_block_group *btrfs_create_chunk(struct btrfs_trans_handle *trans,
742 					     struct btrfs_space_info *space_info,
743 					     u64 type);
744 void btrfs_mapping_tree_free(struct btrfs_fs_info *fs_info);
745 int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
746 		       blk_mode_t flags, void *holder);
747 struct btrfs_device *btrfs_scan_one_device(const char *path, bool mount_arg_dev);
748 int btrfs_forget_devices(dev_t devt);
749 void btrfs_close_devices(struct btrfs_fs_devices *fs_devices);
750 void btrfs_release_device_allow_freeze(struct file *bdev_file);
751 void btrfs_free_extra_devids(struct btrfs_fs_devices *fs_devices);
752 void btrfs_assign_next_active_device(struct btrfs_device *device,
753 				     struct btrfs_device *this_dev);
754 struct btrfs_device *btrfs_find_device_by_devspec(struct btrfs_fs_info *fs_info,
755 						  u64 devid,
756 						  const char *devpath);
757 int btrfs_get_dev_args_from_path(struct btrfs_fs_info *fs_info,
758 				 struct btrfs_dev_lookup_args *args,
759 				 const char *path);
760 struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
761 					const u64 *devid, const u8 *uuid,
762 					const char *path);
763 void btrfs_put_dev_args_from_path(struct btrfs_dev_lookup_args *args);
764 int btrfs_rm_device(struct btrfs_fs_info *fs_info,
765 		    struct btrfs_dev_lookup_args *args,
766 		    struct file **bdev_file);
767 void __exit btrfs_cleanup_fs_uuids(void);
768 int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len);
769 int btrfs_grow_device(struct btrfs_trans_handle *trans,
770 		      struct btrfs_device *device, u64 new_size);
771 struct btrfs_device *btrfs_find_device(const struct btrfs_fs_devices *fs_devices,
772 				       const struct btrfs_dev_lookup_args *args);
773 int btrfs_shrink_device(struct btrfs_device *device, u64 new_size);
774 int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *path);
775 struct file *btrfs_open_device_deny_freeze(const char *path,
776 					   struct super_block *sb);
777 int btrfs_balance(struct btrfs_fs_info *fs_info,
778 		  struct btrfs_balance_control *bctl,
779 		  struct btrfs_ioctl_balance_args *bargs);
780 void btrfs_describe_block_groups(u64 flags, char *buf, u32 size_buf);
781 int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info);
782 int btrfs_recover_balance(struct btrfs_fs_info *fs_info);
783 int btrfs_pause_balance(struct btrfs_fs_info *fs_info);
784 int btrfs_relocate_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset,
785 			 bool verbose);
786 int btrfs_cancel_balance(struct btrfs_fs_info *fs_info);
787 bool btrfs_chunk_writeable(struct btrfs_fs_info *fs_info, u64 chunk_offset);
788 void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index);
789 int btrfs_get_dev_stats(struct btrfs_fs_info *fs_info,
790 			struct btrfs_ioctl_get_dev_stats *stats);
791 int btrfs_init_devices_late(struct btrfs_fs_info *fs_info);
792 int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info);
793 int btrfs_init_writeback_bio_size(struct btrfs_fs_info *fs_info);
794 int btrfs_run_dev_stats(struct btrfs_trans_handle *trans);
795 void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_device *srcdev);
796 void btrfs_rm_dev_replace_free_srcdev(struct btrfs_device *srcdev);
797 void btrfs_destroy_dev_replace_tgtdev(struct btrfs_device *tgtdev,
798 				      bool allow_freeze);
799 void btrfs_free_device(struct btrfs_device *device);
800 unsigned long btrfs_full_stripe_len(struct btrfs_fs_info *fs_info,
801 				    u64 logical);
802 u64 btrfs_calc_stripe_length(const struct btrfs_chunk_map *map);
803 int btrfs_nr_parity_stripes(u64 type);
804 int btrfs_chunk_alloc_add_chunk_item(struct btrfs_trans_handle *trans,
805 				     struct btrfs_block_group *bg);
806 int btrfs_remove_dev_extents(struct btrfs_trans_handle *trans, struct btrfs_chunk_map *map);
807 int btrfs_remove_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset);
808 
809 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
810 struct btrfs_chunk_map *btrfs_alloc_chunk_map(int num_stripes, gfp_t gfp);
811 int btrfs_add_chunk_map(struct btrfs_fs_info *fs_info, struct btrfs_chunk_map *map);
812 #endif
813 
814 struct btrfs_chunk_map *btrfs_find_chunk_map(struct btrfs_fs_info *fs_info,
815 					     u64 logical, u64 length);
816 struct btrfs_chunk_map *btrfs_find_chunk_map_nolock(struct btrfs_fs_info *fs_info,
817 						    u64 logical, u64 length);
818 struct btrfs_chunk_map *btrfs_get_chunk_map(struct btrfs_fs_info *fs_info,
819 					    u64 logical, u64 length);
820 void btrfs_remove_chunk_map(struct btrfs_fs_info *fs_info, struct btrfs_chunk_map *map);
821 struct btrfs_super_block *btrfs_read_disk_super(struct block_device *bdev,
822 						int copy_num, bool drop_cache);
823 void btrfs_release_disk_super(struct btrfs_super_block *super);
824 
825 static inline void btrfs_dev_stat_inc(struct btrfs_device *dev,
826 				      int index)
827 {
828 	atomic_inc(dev->dev_stat_values + index);
829 	/*
830 	 * This memory barrier orders stores updating statistics before stores
831 	 * updating dev_stats_ccnt.
832 	 *
833 	 * It pairs with smp_rmb() in btrfs_run_dev_stats().
834 	 */
835 	smp_mb__before_atomic();
836 	atomic_inc(&dev->dev_stats_ccnt);
837 }
838 
839 static inline int btrfs_dev_stat_read(struct btrfs_device *dev,
840 				      int index)
841 {
842 	return atomic_read(dev->dev_stat_values + index);
843 }
844 
845 static inline int btrfs_dev_stat_read_and_reset(struct btrfs_device *dev,
846 						int index)
847 {
848 	int ret;
849 
850 	ret = atomic_xchg(dev->dev_stat_values + index, 0);
851 	/*
852 	 * atomic_xchg implies a full memory barriers as per atomic_t.txt:
853 	 * - RMW operations that have a return value are fully ordered;
854 	 *
855 	 * This implicit memory barriers is paired with the smp_rmb in
856 	 * btrfs_run_dev_stats
857 	 */
858 	atomic_inc(&dev->dev_stats_ccnt);
859 	return ret;
860 }
861 
862 static inline void btrfs_dev_stat_set(struct btrfs_device *dev,
863 				      int index, unsigned long val)
864 {
865 	atomic_set(dev->dev_stat_values + index, val);
866 	/*
867 	 * This memory barrier orders stores updating statistics before stores
868 	 * updating dev_stats_ccnt.
869 	 *
870 	 * It pairs with smp_rmb() in btrfs_run_dev_stats().
871 	 */
872 	smp_mb__before_atomic();
873 	atomic_inc(&dev->dev_stats_ccnt);
874 }
875 
876 static inline const char *btrfs_dev_name(const struct btrfs_device *device)
877 {
878 	if (!device || test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
879 		return "<missing disk>";
880 	else
881 		return rcu_dereference(device->name);
882 }
883 
884 static inline void btrfs_warn_unknown_chunk_allocation(enum btrfs_chunk_allocation_policy pol)
885 {
886 	WARN_ONCE(1, "unknown allocation policy %d, fallback to regular", pol);
887 }
888 
889 static inline void btrfs_fs_devices_inc_holding(struct btrfs_fs_devices *fs_devices)
890 {
891 	lockdep_assert_held(&uuid_mutex);
892 	ASSERT(fs_devices->holding >= 0);
893 	fs_devices->holding++;
894 }
895 
896 static inline void btrfs_fs_devices_dec_holding(struct btrfs_fs_devices *fs_devices)
897 {
898 	lockdep_assert_held(&uuid_mutex);
899 	ASSERT(fs_devices->holding > 0);
900 	fs_devices->holding--;
901 }
902 
903 void btrfs_commit_device_sizes(struct btrfs_transaction *trans);
904 
905 struct list_head * __attribute_const__ btrfs_get_fs_uuids(void);
906 bool btrfs_check_rw_degradable(struct btrfs_fs_info *fs_info,
907 					struct btrfs_device *failing_dev);
908 void btrfs_scratch_superblocks(struct btrfs_fs_info *fs_info, struct btrfs_device *device);
909 
910 enum btrfs_raid_types __attribute_const__ btrfs_bg_flags_to_raid_index(u64 flags);
911 int btrfs_bg_type_to_factor(u64 flags);
912 const char *btrfs_bg_type_to_raid_name(u64 flags);
913 int btrfs_verify_dev_extents(struct btrfs_fs_info *fs_info);
914 bool btrfs_verify_dev_items(const struct btrfs_fs_info *fs_info);
915 void btrfs_update_per_profile_avail(struct btrfs_fs_info *fs_info);
916 
917 static inline bool btrfs_get_per_profile_avail(struct btrfs_fs_info *fs_info,
918 					       u64 profile, u64 *avail_ret)
919 {
920 	enum btrfs_raid_types index = btrfs_bg_flags_to_raid_index(profile);
921 	struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
922 	bool uptodate = false;
923 
924 	spin_lock(&fs_devices->per_profile_lock);
925 	if (fs_devices->per_profile_avail[index] != U64_MAX) {
926 		uptodate = true;
927 		*avail_ret = fs_devices->per_profile_avail[index];
928 	}
929 	spin_unlock(&fs_info->fs_devices->per_profile_lock);
930 	return uptodate;
931 }
932 
933 bool btrfs_repair_one_zone(struct btrfs_fs_info *fs_info, u64 logical);
934 
935 bool btrfs_pinned_by_swapfile(struct btrfs_fs_info *fs_info, void *ptr);
936 const u8 *btrfs_sb_fsid_ptr(const struct btrfs_super_block *sb);
937 int btrfs_update_device(struct btrfs_trans_handle *trans, struct btrfs_device *device);
938 void btrfs_chunk_map_device_clear_bits(struct btrfs_chunk_map *map, unsigned int bits);
939 
940 bool btrfs_first_pending_extent(struct btrfs_device *device, u64 start, u64 len,
941 				u64 *pending_start, u64 *pending_end);
942 bool btrfs_find_hole_in_pending_extents(struct btrfs_device *device,
943 					u64 *start, u64 *len, u64 min_hole_size);
944 int btrfs_remove_dev_stat_item(struct btrfs_trans_handle *trans, u64 devid);
945 
946 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
947 struct btrfs_io_context *alloc_btrfs_io_context(struct btrfs_fs_info *fs_info,
948 						u64 logical, u16 total_stripes);
949 #endif
950 
951 #endif
952