1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Copyright (C) 2007 Oracle. All rights reserved.
6 #ifndef BTRFS_VOLUMES_H
7 #define BTRFS_VOLUMES_H
9 #include <linux/sort.h>
10 #include <linux/btrfs.h>
11 #include "async-thread.h"
13 #include "tree-checker.h"
14 #include "rcu-string.h"
16 #define BTRFS_MAX_DATA_CHUNK_SIZE (10ULL * SZ_1G)
18 extern struct mutex uuid_mutex;
20 #define BTRFS_STRIPE_LEN SZ_64K
22 /* Used by sanity check for btrfs_raid_types. */
23 #define const_ffs(n) (__builtin_ctzll(n) + 1)
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.
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));
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))
40 enum btrfs_raid_types {
41 /* SINGLE is the special one as it doesn't have on-disk bit. */
42 BTRFS_RAID_SINGLE = 0,
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),
57 * Use sequence counter to get consistent device stat data on
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)
66 #define btrfs_device_data_ordered_init(device) do { } while (0)
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)
76 struct btrfs_zoned_device_info;
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;
85 struct rcu_string __rcu *name;
89 struct block_device *bdev;
91 struct btrfs_zoned_device_info *zone_info;
93 /* the mode sent to blkdev_get */
97 * Device's major-minor number. Must be set even if the device is not
98 * opened (bdev == NULL), unless the device is missing.
101 unsigned long dev_state;
102 blk_status_t last_flush_error;
104 #ifdef __BTRFS_NEED_DEVICE_DATA_ORDERED
105 seqcount_t data_seqcount;
108 /* the internal btrfs device id */
111 /* size of the device in memory */
114 /* size of the device on disk */
115 u64 disk_total_bytes;
120 /* optimal io alignment for this device */
123 /* optimal io width for this device */
125 /* type and info about this device */
128 /* minimal io size for this device */
131 /* physical drive uuid (or lvm uuid) */
132 u8 uuid[BTRFS_UUID_SIZE];
135 * size of the device on the current transaction
137 * This variant is update when committing the transaction,
138 * and protected by chunk mutex
140 u64 commit_total_bytes;
142 /* bytes used on the current transaction */
143 u64 commit_bytes_used;
145 /* Bio used for flushing device barriers */
146 struct bio flush_bio;
147 struct completion flush_wait;
149 /* per-device scrub information */
150 struct scrub_ctx *scrub_ctx;
152 /* disk I/O failure stats. For detailed description refer to
153 * enum btrfs_dev_stat_values in ioctl.h */
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];
160 struct extent_io_tree alloc_state;
162 struct completion kobj_unregister;
163 /* For sysfs/FSID/devinfo/devid/ */
164 struct kobject devid_kobj;
166 /* Bandwidth limit for scrub, in bytes */
171 * Block group or device which contains an active swapfile. Used for preventing
172 * unsafe operations while a swapfile is active.
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.
179 struct btrfs_swapfile_pin {
184 * If true, ptr points to a struct btrfs_block_group. Otherwise, ptr
185 * points to a struct btrfs_device.
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).
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.
199 #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
200 #define BTRFS_DEVICE_GETSET_FUNCS(name) \
202 btrfs_device_get_##name(const struct btrfs_device *dev) \
208 seq = read_seqcount_begin(&dev->data_seqcount); \
210 } while (read_seqcount_retry(&dev->data_seqcount, seq)); \
215 btrfs_device_set_##name(struct btrfs_device *dev, u64 size) \
218 write_seqcount_begin(&dev->data_seqcount); \
220 write_seqcount_end(&dev->data_seqcount); \
223 #elif BITS_PER_LONG==32 && defined(CONFIG_PREEMPTION)
224 #define BTRFS_DEVICE_GETSET_FUNCS(name) \
226 btrfs_device_get_##name(const struct btrfs_device *dev) \
237 btrfs_device_set_##name(struct btrfs_device *dev, u64 size) \
244 #define BTRFS_DEVICE_GETSET_FUNCS(name) \
246 btrfs_device_get_##name(const struct btrfs_device *dev) \
252 btrfs_device_set_##name(struct btrfs_device *dev, u64 size) \
258 BTRFS_DEVICE_GETSET_FUNCS(total_bytes);
259 BTRFS_DEVICE_GETSET_FUNCS(disk_total_bytes);
260 BTRFS_DEVICE_GETSET_FUNCS(bytes_used);
262 enum btrfs_chunk_allocation_policy {
263 BTRFS_CHUNK_ALLOC_REGULAR,
264 BTRFS_CHUNK_ALLOC_ZONED,
268 * Read policies for mirrored block group profiles, read picks the stripe based
271 enum btrfs_read_policy {
272 /* Use process PID to choose the stripe */
273 BTRFS_READ_POLICY_PID,
274 BTRFS_NR_READ_POLICY,
277 struct btrfs_fs_devices {
278 u8 fsid[BTRFS_FSID_SIZE]; /* FS specific uuid */
279 u8 metadata_uuid[BTRFS_FSID_SIZE];
281 struct list_head fs_list;
284 * Number of devices under this fsid including missing and
285 * replace-target device and excludes seed devices.
290 * The number of devices that successfully opened, including
291 * replace-target, excludes seed devices.
295 /* The number of devices that are under the chunk allocation list. */
298 /* Count of missing devices under this fsid excluding seed device. */
303 * Count of devices from btrfs_super_block::num_devices for this fsid,
304 * which includes the seed device, excludes the transient replace-target
309 /* Highest generation number of seen devices */
310 u64 latest_generation;
313 * The mount device or a device with highest generation after removal
316 struct btrfs_device *latest_dev;
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
324 struct mutex device_list_mutex;
326 /* List of all devices, protected by device_list_mutex */
327 struct list_head devices;
330 * Devices which can satisfy space allocation. Protected by
333 struct list_head alloc_list;
335 struct list_head seed_list;
340 /* set when we find or add a device that doesn't have the
344 /* Devices support TRIM/discard commands */
347 struct btrfs_fs_info *fs_info;
349 struct kobject fsid_kobj;
350 struct kobject *devices_kobj;
351 struct kobject *devinfo_kobj;
352 struct completion kobj_unregister;
354 enum btrfs_chunk_allocation_policy chunk_alloc_policy;
356 /* Policy used to read the mirrored stripes */
357 enum btrfs_read_policy read_policy;
360 #define BTRFS_MAX_DEVS(info) ((BTRFS_MAX_ITEM_SIZE(info) \
361 - sizeof(struct btrfs_chunk)) \
362 / sizeof(struct btrfs_stripe) + 1)
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)
369 struct btrfs_io_stripe {
370 struct btrfs_device *dev;
374 /* For the endio handler */
375 struct btrfs_io_context *bioc;
379 struct btrfs_discard_stripe {
380 struct btrfs_device *dev;
386 * Context for IO subsmission for device stripe.
388 * - Track the unfinished mirrors for mirror based profiles
389 * Mirror based profiles are SINGLE/DUP/RAID1/RAID10.
391 * - Contain the logical -> physical mapping info
392 * Used by submit_stripe_bio() for mapping logical bio
393 * into physical device address.
395 * - Contain device replace info
396 * Used by handle_ops_on_dev_replace() to copy logical bios
397 * into the new device.
399 * - Contain RAID56 full stripe logical bytenrs
401 struct btrfs_io_context {
403 struct btrfs_fs_info *fs_info;
404 u64 map_type; /* get from map_lookup->type */
405 struct bio *orig_bio;
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
418 struct btrfs_io_stripe stripes[];
421 struct btrfs_device_info {
422 struct btrfs_device *dev;
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 */
443 extern const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES];
452 int verified_stripes; /* For mount time dev extent verification */
453 struct btrfs_io_stripe stripes[];
456 #define map_lookup_size(n) (sizeof(struct map_lookup) + \
457 (sizeof(struct btrfs_io_stripe) * (n)))
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;
468 struct btrfs_balance_progress stat;
472 * Search for a given device by the set parameters
474 struct btrfs_dev_lookup_args {
481 /* We have to initialize to -1 because BTRFS_DEV_REPLACE_DEVID is 0 */
482 #define BTRFS_DEV_LOOKUP_ARGS_INIT { .devid = (u64)-1 }
484 #define BTRFS_DEV_LOOKUP_ARGS(name) \
485 struct btrfs_dev_lookup_args name = BTRFS_DEV_LOOKUP_ARGS_INIT
491 BTRFS_MAP_GET_READ_MIRRORS,
494 static inline enum btrfs_map_op btrfs_op(struct bio *bio)
496 switch (bio_op(bio)) {
498 return BTRFS_MAP_DISCARD;
500 case REQ_OP_ZONE_APPEND:
501 return BTRFS_MAP_WRITE;
506 return BTRFS_MAP_READ;
510 static inline unsigned long btrfs_chunk_item_size(int num_stripes)
513 return sizeof(struct btrfs_chunk) +
514 sizeof(struct btrfs_stripe) * (num_stripes - 1);
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,
530 struct btrfs_discard_stripe *btrfs_map_discard(struct btrfs_fs_info *fs_info,
531 u64 logical, u64 *length_ret,
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,
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,
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,
553 struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
554 const u64 *devid, const u8 *uuid,
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,
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);
605 static inline void btrfs_dev_stat_inc(struct btrfs_device *dev,
608 atomic_inc(dev->dev_stat_values + index);
610 * This memory barrier orders stores updating statistics before stores
611 * updating dev_stats_ccnt.
613 * It pairs with smp_rmb() in btrfs_run_dev_stats().
615 smp_mb__before_atomic();
616 atomic_inc(&dev->dev_stats_ccnt);
619 static inline int btrfs_dev_stat_read(struct btrfs_device *dev,
622 return atomic_read(dev->dev_stat_values + index);
625 static inline int btrfs_dev_stat_read_and_reset(struct btrfs_device *dev,
630 ret = atomic_xchg(dev->dev_stat_values + index, 0);
632 * atomic_xchg implies a full memory barriers as per atomic_t.txt:
633 * - RMW operations that have a return value are fully ordered;
635 * This implicit memory barriers is paired with the smp_rmb in
636 * btrfs_run_dev_stats
638 atomic_inc(&dev->dev_stats_ccnt);
642 static inline void btrfs_dev_stat_set(struct btrfs_device *dev,
643 int index, unsigned long val)
645 atomic_set(dev->dev_stat_values + index, val);
647 * This memory barrier orders stores updating statistics before stores
648 * updating dev_stats_ccnt.
650 * It pairs with smp_rmb() in btrfs_run_dev_stats().
652 smp_mb__before_atomic();
653 atomic_inc(&dev->dev_stats_ccnt);
656 static inline const char *btrfs_dev_name(const struct btrfs_device *device)
658 if (!device || test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
659 return "<missing disk>";
661 return rcu_str_deref(device->name);
664 void btrfs_commit_device_sizes(struct btrfs_transaction *trans);
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);
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);
679 bool btrfs_pinned_by_swapfile(struct btrfs_fs_info *fs_info, void *ptr);