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