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