xref: /openbmc/linux/fs/btrfs/volumes.h (revision f00545e8)
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/sort.h>
10 #include <linux/btrfs.h>
11 #include "async-thread.h"
12 #include "messages.h"
13 #include "tree-checker.h"
14 #include "rcu-string.h"
15 
16 #define BTRFS_MAX_DATA_CHUNK_SIZE	(10ULL * SZ_1G)
17 
18 /*
19  * Arbitratry maximum size of one discard request to limit potentially long time
20  * spent in blkdev_issue_discard().
21  */
22 #define BTRFS_MAX_DISCARD_CHUNK_SIZE	(SZ_1G)
23 
24 extern struct mutex uuid_mutex;
25 
26 #define BTRFS_STRIPE_LEN		SZ_64K
27 #define BTRFS_STRIPE_LEN_SHIFT		(16)
28 #define BTRFS_STRIPE_LEN_MASK		(BTRFS_STRIPE_LEN - 1)
29 
30 static_assert(const_ilog2(BTRFS_STRIPE_LEN) == BTRFS_STRIPE_LEN_SHIFT);
31 
32 /* Used by sanity check for btrfs_raid_types. */
33 #define const_ffs(n) (__builtin_ctzll(n) + 1)
34 
35 /*
36  * The conversion from BTRFS_BLOCK_GROUP_* bits to btrfs_raid_type requires
37  * RAID0 always to be the lowest profile bit.
38  * Although it's part of on-disk format and should never change, do extra
39  * compile-time sanity checks.
40  */
41 static_assert(const_ffs(BTRFS_BLOCK_GROUP_RAID0) <
42 	      const_ffs(BTRFS_BLOCK_GROUP_PROFILE_MASK & ~BTRFS_BLOCK_GROUP_RAID0));
43 static_assert(const_ilog2(BTRFS_BLOCK_GROUP_RAID0) >
44 	      ilog2(BTRFS_BLOCK_GROUP_TYPE_MASK));
45 
46 /* ilog2() can handle both constants and variables */
47 #define BTRFS_BG_FLAG_TO_INDEX(profile)					\
48 	ilog2((profile) >> (ilog2(BTRFS_BLOCK_GROUP_RAID0) - 1))
49 
50 enum btrfs_raid_types {
51 	/* SINGLE is the special one as it doesn't have on-disk bit. */
52 	BTRFS_RAID_SINGLE  = 0,
53 
54 	BTRFS_RAID_RAID0   = BTRFS_BG_FLAG_TO_INDEX(BTRFS_BLOCK_GROUP_RAID0),
55 	BTRFS_RAID_RAID1   = BTRFS_BG_FLAG_TO_INDEX(BTRFS_BLOCK_GROUP_RAID1),
56 	BTRFS_RAID_DUP	   = BTRFS_BG_FLAG_TO_INDEX(BTRFS_BLOCK_GROUP_DUP),
57 	BTRFS_RAID_RAID10  = BTRFS_BG_FLAG_TO_INDEX(BTRFS_BLOCK_GROUP_RAID10),
58 	BTRFS_RAID_RAID5   = BTRFS_BG_FLAG_TO_INDEX(BTRFS_BLOCK_GROUP_RAID5),
59 	BTRFS_RAID_RAID6   = BTRFS_BG_FLAG_TO_INDEX(BTRFS_BLOCK_GROUP_RAID6),
60 	BTRFS_RAID_RAID1C3 = BTRFS_BG_FLAG_TO_INDEX(BTRFS_BLOCK_GROUP_RAID1C3),
61 	BTRFS_RAID_RAID1C4 = BTRFS_BG_FLAG_TO_INDEX(BTRFS_BLOCK_GROUP_RAID1C4),
62 
63 	BTRFS_NR_RAID_TYPES
64 };
65 
66 /*
67  * Use sequence counter to get consistent device stat data on
68  * 32-bit processors.
69  */
70 #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
71 #include <linux/seqlock.h>
72 #define __BTRFS_NEED_DEVICE_DATA_ORDERED
73 #define btrfs_device_data_ordered_init(device)	\
74 	seqcount_init(&device->data_seqcount)
75 #else
76 #define btrfs_device_data_ordered_init(device) do { } while (0)
77 #endif
78 
79 #define BTRFS_DEV_STATE_WRITEABLE	(0)
80 #define BTRFS_DEV_STATE_IN_FS_METADATA	(1)
81 #define BTRFS_DEV_STATE_MISSING		(2)
82 #define BTRFS_DEV_STATE_REPLACE_TGT	(3)
83 #define BTRFS_DEV_STATE_FLUSH_SENT	(4)
84 #define BTRFS_DEV_STATE_NO_READA	(5)
85 
86 struct btrfs_zoned_device_info;
87 
88 struct btrfs_device {
89 	struct list_head dev_list; /* device_list_mutex */
90 	struct list_head dev_alloc_list; /* chunk mutex */
91 	struct list_head post_commit_list; /* chunk mutex */
92 	struct btrfs_fs_devices *fs_devices;
93 	struct btrfs_fs_info *fs_info;
94 
95 	struct rcu_string __rcu *name;
96 
97 	u64 generation;
98 
99 	struct block_device *bdev;
100 
101 	struct btrfs_zoned_device_info *zone_info;
102 
103 	/* block device holder for blkdev_get/put */
104 	void *holder;
105 
106 	/*
107 	 * Device's major-minor number. Must be set even if the device is not
108 	 * opened (bdev == NULL), unless the device is missing.
109 	 */
110 	dev_t devt;
111 	unsigned long dev_state;
112 	blk_status_t last_flush_error;
113 
114 #ifdef __BTRFS_NEED_DEVICE_DATA_ORDERED
115 	seqcount_t data_seqcount;
116 #endif
117 
118 	/* the internal btrfs device id */
119 	u64 devid;
120 
121 	/* size of the device in memory */
122 	u64 total_bytes;
123 
124 	/* size of the device on disk */
125 	u64 disk_total_bytes;
126 
127 	/* bytes used */
128 	u64 bytes_used;
129 
130 	/* optimal io alignment for this device */
131 	u32 io_align;
132 
133 	/* optimal io width for this device */
134 	u32 io_width;
135 	/* type and info about this device */
136 	u64 type;
137 
138 	/* minimal io size for this device */
139 	u32 sector_size;
140 
141 	/* physical drive uuid (or lvm uuid) */
142 	u8 uuid[BTRFS_UUID_SIZE];
143 
144 	/*
145 	 * size of the device on the current transaction
146 	 *
147 	 * This variant is update when committing the transaction,
148 	 * and protected by chunk mutex
149 	 */
150 	u64 commit_total_bytes;
151 
152 	/* bytes used on the current transaction */
153 	u64 commit_bytes_used;
154 
155 	/* Bio used for flushing device barriers */
156 	struct bio flush_bio;
157 	struct completion flush_wait;
158 
159 	/* per-device scrub information */
160 	struct scrub_ctx *scrub_ctx;
161 
162 	/* disk I/O failure stats. For detailed description refer to
163 	 * enum btrfs_dev_stat_values in ioctl.h */
164 	int dev_stats_valid;
165 
166 	/* Counter to record the change of device stats */
167 	atomic_t dev_stats_ccnt;
168 	atomic_t dev_stat_values[BTRFS_DEV_STAT_VALUES_MAX];
169 
170 	struct extent_io_tree alloc_state;
171 
172 	struct completion kobj_unregister;
173 	/* For sysfs/FSID/devinfo/devid/ */
174 	struct kobject devid_kobj;
175 
176 	/* Bandwidth limit for scrub, in bytes */
177 	u64 scrub_speed_max;
178 };
179 
180 /*
181  * Block group or device which contains an active swapfile. Used for preventing
182  * unsafe operations while a swapfile is active.
183  *
184  * These are sorted on (ptr, inode) (note that a block group or device can
185  * contain more than one swapfile). We compare the pointer values because we
186  * don't actually care what the object is, we just need a quick check whether
187  * the object exists in the rbtree.
188  */
189 struct btrfs_swapfile_pin {
190 	struct rb_node node;
191 	void *ptr;
192 	struct inode *inode;
193 	/*
194 	 * If true, ptr points to a struct btrfs_block_group. Otherwise, ptr
195 	 * points to a struct btrfs_device.
196 	 */
197 	bool is_block_group;
198 	/*
199 	 * Only used when 'is_block_group' is true and it is the number of
200 	 * extents used by a swapfile for this block group ('ptr' field).
201 	 */
202 	int bg_extent_count;
203 };
204 
205 /*
206  * If we read those variants at the context of their own lock, we needn't
207  * use the following helpers, reading them directly is safe.
208  */
209 #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
210 #define BTRFS_DEVICE_GETSET_FUNCS(name)					\
211 static inline u64							\
212 btrfs_device_get_##name(const struct btrfs_device *dev)			\
213 {									\
214 	u64 size;							\
215 	unsigned int seq;						\
216 									\
217 	do {								\
218 		seq = read_seqcount_begin(&dev->data_seqcount);		\
219 		size = dev->name;					\
220 	} while (read_seqcount_retry(&dev->data_seqcount, seq));	\
221 	return size;							\
222 }									\
223 									\
224 static inline void							\
225 btrfs_device_set_##name(struct btrfs_device *dev, u64 size)		\
226 {									\
227 	preempt_disable();						\
228 	write_seqcount_begin(&dev->data_seqcount);			\
229 	dev->name = size;						\
230 	write_seqcount_end(&dev->data_seqcount);			\
231 	preempt_enable();						\
232 }
233 #elif BITS_PER_LONG==32 && defined(CONFIG_PREEMPTION)
234 #define BTRFS_DEVICE_GETSET_FUNCS(name)					\
235 static inline u64							\
236 btrfs_device_get_##name(const struct btrfs_device *dev)			\
237 {									\
238 	u64 size;							\
239 									\
240 	preempt_disable();						\
241 	size = dev->name;						\
242 	preempt_enable();						\
243 	return size;							\
244 }									\
245 									\
246 static inline void							\
247 btrfs_device_set_##name(struct btrfs_device *dev, u64 size)		\
248 {									\
249 	preempt_disable();						\
250 	dev->name = size;						\
251 	preempt_enable();						\
252 }
253 #else
254 #define BTRFS_DEVICE_GETSET_FUNCS(name)					\
255 static inline u64							\
256 btrfs_device_get_##name(const struct btrfs_device *dev)			\
257 {									\
258 	return dev->name;						\
259 }									\
260 									\
261 static inline void							\
262 btrfs_device_set_##name(struct btrfs_device *dev, u64 size)		\
263 {									\
264 	dev->name = size;						\
265 }
266 #endif
267 
268 BTRFS_DEVICE_GETSET_FUNCS(total_bytes);
269 BTRFS_DEVICE_GETSET_FUNCS(disk_total_bytes);
270 BTRFS_DEVICE_GETSET_FUNCS(bytes_used);
271 
272 enum btrfs_chunk_allocation_policy {
273 	BTRFS_CHUNK_ALLOC_REGULAR,
274 	BTRFS_CHUNK_ALLOC_ZONED,
275 };
276 
277 /*
278  * Read policies for mirrored block group profiles, read picks the stripe based
279  * on these policies.
280  */
281 enum btrfs_read_policy {
282 	/* Use process PID to choose the stripe */
283 	BTRFS_READ_POLICY_PID,
284 	BTRFS_NR_READ_POLICY,
285 };
286 
287 struct btrfs_fs_devices {
288 	u8 fsid[BTRFS_FSID_SIZE]; /* FS specific uuid */
289 
290 	/*
291 	 * UUID written into the btree blocks:
292 	 *
293 	 * - If metadata_uuid != fsid then super block must have
294 	 *   BTRFS_FEATURE_INCOMPAT_METADATA_UUID flag set.
295 	 *
296 	 * - Following shall be true at all times:
297 	 *   - metadata_uuid == btrfs_header::fsid
298 	 *   - metadata_uuid == btrfs_dev_item::fsid
299 	 */
300 	u8 metadata_uuid[BTRFS_FSID_SIZE];
301 
302 	struct list_head fs_list;
303 
304 	/*
305 	 * Number of devices under this fsid including missing and
306 	 * replace-target device and excludes seed devices.
307 	 */
308 	u64 num_devices;
309 
310 	/*
311 	 * The number of devices that successfully opened, including
312 	 * replace-target, excludes seed devices.
313 	 */
314 	u64 open_devices;
315 
316 	/* The number of devices that are under the chunk allocation list. */
317 	u64 rw_devices;
318 
319 	/* Count of missing devices under this fsid excluding seed device. */
320 	u64 missing_devices;
321 	u64 total_rw_bytes;
322 
323 	/*
324 	 * Count of devices from btrfs_super_block::num_devices for this fsid,
325 	 * which includes the seed device, excludes the transient replace-target
326 	 * device.
327 	 */
328 	u64 total_devices;
329 
330 	/* Highest generation number of seen devices */
331 	u64 latest_generation;
332 
333 	/*
334 	 * The mount device or a device with highest generation after removal
335 	 * or replace.
336 	 */
337 	struct btrfs_device *latest_dev;
338 
339 	/*
340 	 * All of the devices in the filesystem, protected by a mutex so we can
341 	 * safely walk it to write out the super blocks without worrying about
342 	 * adding/removing by the multi-device code. Scrubbing super block can
343 	 * kick off supers writing by holding this mutex lock.
344 	 */
345 	struct mutex device_list_mutex;
346 
347 	/* List of all devices, protected by device_list_mutex */
348 	struct list_head devices;
349 
350 	/* Devices which can satisfy space allocation. Protected by * chunk_mutex. */
351 	struct list_head alloc_list;
352 
353 	struct list_head seed_list;
354 
355 	/* Count fs-devices opened. */
356 	int opened;
357 
358 	/* Set when we find or add a device that doesn't have the nonrot flag set. */
359 	bool rotating;
360 	/* Devices support TRIM/discard commands. */
361 	bool discardable;
362 	bool fsid_change;
363 	/* The filesystem is a seed filesystem. */
364 	bool seeding;
365 
366 	struct btrfs_fs_info *fs_info;
367 	/* sysfs kobjects */
368 	struct kobject fsid_kobj;
369 	struct kobject *devices_kobj;
370 	struct kobject *devinfo_kobj;
371 	struct completion kobj_unregister;
372 
373 	enum btrfs_chunk_allocation_policy chunk_alloc_policy;
374 
375 	/* Policy used to read the mirrored stripes. */
376 	enum btrfs_read_policy read_policy;
377 };
378 
379 #define BTRFS_MAX_DEVS(info) ((BTRFS_MAX_ITEM_SIZE(info)	\
380 			- sizeof(struct btrfs_chunk))		\
381 			/ sizeof(struct btrfs_stripe) + 1)
382 
383 #define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE	\
384 				- 2 * sizeof(struct btrfs_disk_key)	\
385 				- 2 * sizeof(struct btrfs_chunk))	\
386 				/ sizeof(struct btrfs_stripe) + 1)
387 
388 struct btrfs_io_stripe {
389 	struct btrfs_device *dev;
390 	union {
391 		/* Block mapping */
392 		u64 physical;
393 		/* For the endio handler */
394 		struct btrfs_io_context *bioc;
395 	};
396 };
397 
398 struct btrfs_discard_stripe {
399 	struct btrfs_device *dev;
400 	u64 physical;
401 	u64 length;
402 };
403 
404 /*
405  * Context for IO subsmission for device stripe.
406  *
407  * - Track the unfinished mirrors for mirror based profiles
408  *   Mirror based profiles are SINGLE/DUP/RAID1/RAID10.
409  *
410  * - Contain the logical -> physical mapping info
411  *   Used by submit_stripe_bio() for mapping logical bio
412  *   into physical device address.
413  *
414  * - Contain device replace info
415  *   Used by handle_ops_on_dev_replace() to copy logical bios
416  *   into the new device.
417  *
418  * - Contain RAID56 full stripe logical bytenrs
419  */
420 struct btrfs_io_context {
421 	refcount_t refs;
422 	struct btrfs_fs_info *fs_info;
423 	u64 map_type; /* get from map_lookup->type */
424 	struct bio *orig_bio;
425 	atomic_t error;
426 	u16 max_errors;
427 
428 	/*
429 	 * The total number of stripes, including the extra duplicated
430 	 * stripe for replace.
431 	 */
432 	u16 num_stripes;
433 
434 	/*
435 	 * The mirror_num of this bioc.
436 	 *
437 	 * This is for reads which use 0 as mirror_num, thus we should return a
438 	 * valid mirror_num (>0) for the reader.
439 	 */
440 	u16 mirror_num;
441 
442 	/*
443 	 * The following two members are for dev-replace case only.
444 	 *
445 	 * @replace_nr_stripes:	Number of duplicated stripes which need to be
446 	 *			written to replace target.
447 	 *			Should be <= 2 (2 for DUP, otherwise <= 1).
448 	 * @replace_stripe_src:	The array indicates where the duplicated stripes
449 	 *			are from.
450 	 *
451 	 * The @replace_stripe_src[] array is mostly for RAID56 cases.
452 	 * As non-RAID56 stripes share the same contents of the mapped range,
453 	 * thus no need to bother where the duplicated ones are from.
454 	 *
455 	 * But for RAID56 case, all stripes contain different contents, thus
456 	 * we need a way to know the mapping.
457 	 *
458 	 * There is an example for the two members, using a RAID5 write:
459 	 *
460 	 *   num_stripes:	4 (3 + 1 duplicated write)
461 	 *   stripes[0]:	dev = devid 1, physical = X
462 	 *   stripes[1]:	dev = devid 2, physical = Y
463 	 *   stripes[2]:	dev = devid 3, physical = Z
464 	 *   stripes[3]:	dev = devid 0, physical = Y
465 	 *
466 	 * replace_nr_stripes = 1
467 	 * replace_stripe_src = 1	<- Means stripes[1] is involved in replace.
468 	 *				   The duplicated stripe index would be
469 	 *				   (@num_stripes - 1).
470 	 *
471 	 * Note, that we can still have cases replace_nr_stripes = 2 for DUP.
472 	 * In that case, all stripes share the same content, thus we don't
473 	 * need to bother @replace_stripe_src value at all.
474 	 */
475 	u16 replace_nr_stripes;
476 	s16 replace_stripe_src;
477 	/*
478 	 * Logical bytenr of the full stripe start, only for RAID56 cases.
479 	 *
480 	 * When this value is set to other than (u64)-1, the stripes[] should
481 	 * follow this pattern:
482 	 *
483 	 * (real_stripes = num_stripes - replace_nr_stripes)
484 	 * (data_stripes = (is_raid6) ? (real_stripes - 2) : (real_stripes - 1))
485 	 *
486 	 * stripes[0]:			The first data stripe
487 	 * stripes[1]:			The second data stripe
488 	 * ...
489 	 * stripes[data_stripes - 1]:	The last data stripe
490 	 * stripes[data_stripes]:	The P stripe
491 	 * stripes[data_stripes + 1]:	The Q stripe (only for RAID6).
492 	 */
493 	u64 full_stripe_logical;
494 	struct btrfs_io_stripe stripes[];
495 };
496 
497 struct btrfs_device_info {
498 	struct btrfs_device *dev;
499 	u64 dev_offset;
500 	u64 max_avail;
501 	u64 total_avail;
502 };
503 
504 struct btrfs_raid_attr {
505 	u8 sub_stripes;		/* sub_stripes info for map */
506 	u8 dev_stripes;		/* stripes per dev */
507 	u8 devs_max;		/* max devs to use */
508 	u8 devs_min;		/* min devs needed */
509 	u8 tolerated_failures;	/* max tolerated fail devs */
510 	u8 devs_increment;	/* ndevs has to be a multiple of this */
511 	u8 ncopies;		/* how many copies to data has */
512 	u8 nparity;		/* number of stripes worth of bytes to store
513 				 * parity information */
514 	u8 mindev_error;	/* error code if min devs requisite is unmet */
515 	const char raid_name[8]; /* name of the raid */
516 	u64 bg_flag;		/* block group flag of the raid */
517 };
518 
519 extern const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES];
520 
521 struct map_lookup {
522 	u64 type;
523 	int io_align;
524 	int io_width;
525 	int num_stripes;
526 	int sub_stripes;
527 	int verified_stripes; /* For mount time dev extent verification */
528 	struct btrfs_io_stripe stripes[];
529 };
530 
531 #define map_lookup_size(n) (sizeof(struct map_lookup) + \
532 			    (sizeof(struct btrfs_io_stripe) * (n)))
533 
534 struct btrfs_balance_args;
535 struct btrfs_balance_progress;
536 struct btrfs_balance_control {
537 	struct btrfs_balance_args data;
538 	struct btrfs_balance_args meta;
539 	struct btrfs_balance_args sys;
540 
541 	u64 flags;
542 
543 	struct btrfs_balance_progress stat;
544 };
545 
546 /*
547  * Search for a given device by the set parameters
548  */
549 struct btrfs_dev_lookup_args {
550 	u64 devid;
551 	u8 *uuid;
552 	u8 *fsid;
553 	bool missing;
554 };
555 
556 /* We have to initialize to -1 because BTRFS_DEV_REPLACE_DEVID is 0 */
557 #define BTRFS_DEV_LOOKUP_ARGS_INIT { .devid = (u64)-1 }
558 
559 #define BTRFS_DEV_LOOKUP_ARGS(name) \
560 	struct btrfs_dev_lookup_args name = BTRFS_DEV_LOOKUP_ARGS_INIT
561 
562 enum btrfs_map_op {
563 	BTRFS_MAP_READ,
564 	BTRFS_MAP_WRITE,
565 	BTRFS_MAP_GET_READ_MIRRORS,
566 };
567 
btrfs_op(struct bio * bio)568 static inline enum btrfs_map_op btrfs_op(struct bio *bio)
569 {
570 	switch (bio_op(bio)) {
571 	case REQ_OP_WRITE:
572 	case REQ_OP_ZONE_APPEND:
573 		return BTRFS_MAP_WRITE;
574 	default:
575 		WARN_ON_ONCE(1);
576 		fallthrough;
577 	case REQ_OP_READ:
578 		return BTRFS_MAP_READ;
579 	}
580 }
581 
btrfs_chunk_item_size(int num_stripes)582 static inline unsigned long btrfs_chunk_item_size(int num_stripes)
583 {
584 	ASSERT(num_stripes);
585 	return sizeof(struct btrfs_chunk) +
586 		sizeof(struct btrfs_stripe) * (num_stripes - 1);
587 }
588 
589 /*
590  * Do the type safe converstion from stripe_nr to offset inside the chunk.
591  *
592  * @stripe_nr is u32, with left shift it can overflow u32 for chunks larger
593  * than 4G.  This does the proper type cast to avoid overflow.
594  */
btrfs_stripe_nr_to_offset(u32 stripe_nr)595 static inline u64 btrfs_stripe_nr_to_offset(u32 stripe_nr)
596 {
597 	return (u64)stripe_nr << BTRFS_STRIPE_LEN_SHIFT;
598 }
599 
600 void btrfs_get_bioc(struct btrfs_io_context *bioc);
601 void btrfs_put_bioc(struct btrfs_io_context *bioc);
602 int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
603 		    u64 logical, u64 *length,
604 		    struct btrfs_io_context **bioc_ret,
605 		    struct btrfs_io_stripe *smap, int *mirror_num_ret,
606 		    int need_raid_map);
607 int btrfs_map_repair_block(struct btrfs_fs_info *fs_info,
608 			   struct btrfs_io_stripe *smap, u64 logical,
609 			   u32 length, int mirror_num);
610 struct btrfs_discard_stripe *btrfs_map_discard(struct btrfs_fs_info *fs_info,
611 					       u64 logical, u64 *length_ret,
612 					       u32 *num_stripes);
613 int btrfs_read_sys_array(struct btrfs_fs_info *fs_info);
614 int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info);
615 struct btrfs_block_group *btrfs_create_chunk(struct btrfs_trans_handle *trans,
616 					    u64 type);
617 void btrfs_mapping_tree_free(struct extent_map_tree *tree);
618 int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
619 		       blk_mode_t flags, void *holder);
620 struct btrfs_device *btrfs_scan_one_device(const char *path, blk_mode_t flags);
621 int btrfs_forget_devices(dev_t devt);
622 void btrfs_close_devices(struct btrfs_fs_devices *fs_devices);
623 void btrfs_free_extra_devids(struct btrfs_fs_devices *fs_devices);
624 void btrfs_assign_next_active_device(struct btrfs_device *device,
625 				     struct btrfs_device *this_dev);
626 struct btrfs_device *btrfs_find_device_by_devspec(struct btrfs_fs_info *fs_info,
627 						  u64 devid,
628 						  const char *devpath);
629 int btrfs_get_dev_args_from_path(struct btrfs_fs_info *fs_info,
630 				 struct btrfs_dev_lookup_args *args,
631 				 const char *path);
632 struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
633 					const u64 *devid, const u8 *uuid,
634 					const char *path);
635 void btrfs_put_dev_args_from_path(struct btrfs_dev_lookup_args *args);
636 int btrfs_rm_device(struct btrfs_fs_info *fs_info,
637 		    struct btrfs_dev_lookup_args *args,
638 		    struct block_device **bdev, void **holder);
639 void __exit btrfs_cleanup_fs_uuids(void);
640 int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len);
641 int btrfs_grow_device(struct btrfs_trans_handle *trans,
642 		      struct btrfs_device *device, u64 new_size);
643 struct btrfs_device *btrfs_find_device(const struct btrfs_fs_devices *fs_devices,
644 				       const struct btrfs_dev_lookup_args *args);
645 int btrfs_shrink_device(struct btrfs_device *device, u64 new_size);
646 int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *path);
647 int btrfs_balance(struct btrfs_fs_info *fs_info,
648 		  struct btrfs_balance_control *bctl,
649 		  struct btrfs_ioctl_balance_args *bargs);
650 void btrfs_describe_block_groups(u64 flags, char *buf, u32 size_buf);
651 int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info);
652 int btrfs_recover_balance(struct btrfs_fs_info *fs_info);
653 int btrfs_pause_balance(struct btrfs_fs_info *fs_info);
654 int btrfs_relocate_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset);
655 int btrfs_cancel_balance(struct btrfs_fs_info *fs_info);
656 int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info);
657 int btrfs_uuid_scan_kthread(void *data);
658 bool btrfs_chunk_writeable(struct btrfs_fs_info *fs_info, u64 chunk_offset);
659 void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index);
660 int btrfs_get_dev_stats(struct btrfs_fs_info *fs_info,
661 			struct btrfs_ioctl_get_dev_stats *stats);
662 int btrfs_init_devices_late(struct btrfs_fs_info *fs_info);
663 int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info);
664 int btrfs_run_dev_stats(struct btrfs_trans_handle *trans);
665 void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_device *srcdev);
666 void btrfs_rm_dev_replace_free_srcdev(struct btrfs_device *srcdev);
667 void btrfs_destroy_dev_replace_tgtdev(struct btrfs_device *tgtdev);
668 int btrfs_is_parity_mirror(struct btrfs_fs_info *fs_info,
669 			   u64 logical, u64 len);
670 unsigned long btrfs_full_stripe_len(struct btrfs_fs_info *fs_info,
671 				    u64 logical);
672 u64 btrfs_calc_stripe_length(const struct extent_map *em);
673 int btrfs_nr_parity_stripes(u64 type);
674 int btrfs_chunk_alloc_add_chunk_item(struct btrfs_trans_handle *trans,
675 				     struct btrfs_block_group *bg);
676 int btrfs_remove_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset);
677 struct extent_map *btrfs_get_chunk_map(struct btrfs_fs_info *fs_info,
678 				       u64 logical, u64 length);
679 void btrfs_release_disk_super(struct btrfs_super_block *super);
680 
btrfs_dev_stat_inc(struct btrfs_device * dev,int index)681 static inline void btrfs_dev_stat_inc(struct btrfs_device *dev,
682 				      int index)
683 {
684 	atomic_inc(dev->dev_stat_values + index);
685 	/*
686 	 * This memory barrier orders stores updating statistics before stores
687 	 * updating dev_stats_ccnt.
688 	 *
689 	 * It pairs with smp_rmb() in btrfs_run_dev_stats().
690 	 */
691 	smp_mb__before_atomic();
692 	atomic_inc(&dev->dev_stats_ccnt);
693 }
694 
btrfs_dev_stat_read(struct btrfs_device * dev,int index)695 static inline int btrfs_dev_stat_read(struct btrfs_device *dev,
696 				      int index)
697 {
698 	return atomic_read(dev->dev_stat_values + index);
699 }
700 
btrfs_dev_stat_read_and_reset(struct btrfs_device * dev,int index)701 static inline int btrfs_dev_stat_read_and_reset(struct btrfs_device *dev,
702 						int index)
703 {
704 	int ret;
705 
706 	ret = atomic_xchg(dev->dev_stat_values + index, 0);
707 	/*
708 	 * atomic_xchg implies a full memory barriers as per atomic_t.txt:
709 	 * - RMW operations that have a return value are fully ordered;
710 	 *
711 	 * This implicit memory barriers is paired with the smp_rmb in
712 	 * btrfs_run_dev_stats
713 	 */
714 	atomic_inc(&dev->dev_stats_ccnt);
715 	return ret;
716 }
717 
btrfs_dev_stat_set(struct btrfs_device * dev,int index,unsigned long val)718 static inline void btrfs_dev_stat_set(struct btrfs_device *dev,
719 				      int index, unsigned long val)
720 {
721 	atomic_set(dev->dev_stat_values + index, val);
722 	/*
723 	 * This memory barrier orders stores updating statistics before stores
724 	 * updating dev_stats_ccnt.
725 	 *
726 	 * It pairs with smp_rmb() in btrfs_run_dev_stats().
727 	 */
728 	smp_mb__before_atomic();
729 	atomic_inc(&dev->dev_stats_ccnt);
730 }
731 
btrfs_dev_name(const struct btrfs_device * device)732 static inline const char *btrfs_dev_name(const struct btrfs_device *device)
733 {
734 	if (!device || test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
735 		return "<missing disk>";
736 	else
737 		return rcu_str_deref(device->name);
738 }
739 
740 void btrfs_commit_device_sizes(struct btrfs_transaction *trans);
741 
742 struct list_head * __attribute_const__ btrfs_get_fs_uuids(void);
743 bool btrfs_check_rw_degradable(struct btrfs_fs_info *fs_info,
744 					struct btrfs_device *failing_dev);
745 void btrfs_scratch_superblocks(struct btrfs_fs_info *fs_info,
746 			       struct block_device *bdev,
747 			       const char *device_path);
748 
749 enum btrfs_raid_types __attribute_const__ btrfs_bg_flags_to_raid_index(u64 flags);
750 int btrfs_bg_type_to_factor(u64 flags);
751 const char *btrfs_bg_type_to_raid_name(u64 flags);
752 int btrfs_verify_dev_extents(struct btrfs_fs_info *fs_info);
753 bool btrfs_repair_one_zone(struct btrfs_fs_info *fs_info, u64 logical);
754 
755 bool btrfs_pinned_by_swapfile(struct btrfs_fs_info *fs_info, void *ptr);
756 u8 *btrfs_sb_fsid_ptr(struct btrfs_super_block *sb);
757 
758 #endif
759