0001
0002
0003
0004
0005
0006 #ifndef BTRFS_VOLUMES_H
0007 #define BTRFS_VOLUMES_H
0008
0009 #include <linux/bio.h>
0010 #include <linux/sort.h>
0011 #include <linux/btrfs.h>
0012 #include "async-thread.h"
0013
0014 #define BTRFS_MAX_DATA_CHUNK_SIZE (10ULL * SZ_1G)
0015
0016 extern struct mutex uuid_mutex;
0017
0018 #define BTRFS_STRIPE_LEN SZ_64K
0019
0020
0021 #define const_ffs(n) (__builtin_ctzll(n) + 1)
0022
0023
0024
0025
0026
0027
0028
0029 static_assert(const_ffs(BTRFS_BLOCK_GROUP_RAID0) <
0030 const_ffs(BTRFS_BLOCK_GROUP_PROFILE_MASK & ~BTRFS_BLOCK_GROUP_RAID0));
0031 static_assert(const_ilog2(BTRFS_BLOCK_GROUP_RAID0) >
0032 ilog2(BTRFS_BLOCK_GROUP_TYPE_MASK));
0033
0034
0035 #define BTRFS_BG_FLAG_TO_INDEX(profile) \
0036 ilog2((profile) >> (ilog2(BTRFS_BLOCK_GROUP_RAID0) - 1))
0037
0038 enum btrfs_raid_types {
0039
0040 BTRFS_RAID_SINGLE = 0,
0041
0042 BTRFS_RAID_RAID0 = BTRFS_BG_FLAG_TO_INDEX(BTRFS_BLOCK_GROUP_RAID0),
0043 BTRFS_RAID_RAID1 = BTRFS_BG_FLAG_TO_INDEX(BTRFS_BLOCK_GROUP_RAID1),
0044 BTRFS_RAID_DUP = BTRFS_BG_FLAG_TO_INDEX(BTRFS_BLOCK_GROUP_DUP),
0045 BTRFS_RAID_RAID10 = BTRFS_BG_FLAG_TO_INDEX(BTRFS_BLOCK_GROUP_RAID10),
0046 BTRFS_RAID_RAID5 = BTRFS_BG_FLAG_TO_INDEX(BTRFS_BLOCK_GROUP_RAID5),
0047 BTRFS_RAID_RAID6 = BTRFS_BG_FLAG_TO_INDEX(BTRFS_BLOCK_GROUP_RAID6),
0048 BTRFS_RAID_RAID1C3 = BTRFS_BG_FLAG_TO_INDEX(BTRFS_BLOCK_GROUP_RAID1C3),
0049 BTRFS_RAID_RAID1C4 = BTRFS_BG_FLAG_TO_INDEX(BTRFS_BLOCK_GROUP_RAID1C4),
0050
0051 BTRFS_NR_RAID_TYPES
0052 };
0053
0054 struct btrfs_io_geometry {
0055
0056 u64 len;
0057
0058 u64 offset;
0059
0060 u32 stripe_len;
0061
0062 u32 stripe_offset;
0063
0064 u64 stripe_nr;
0065
0066 u64 raid56_stripe_offset;
0067 };
0068
0069
0070
0071
0072
0073 #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
0074 #include <linux/seqlock.h>
0075 #define __BTRFS_NEED_DEVICE_DATA_ORDERED
0076 #define btrfs_device_data_ordered_init(device) \
0077 seqcount_init(&device->data_seqcount)
0078 #else
0079 #define btrfs_device_data_ordered_init(device) do { } while (0)
0080 #endif
0081
0082 #define BTRFS_DEV_STATE_WRITEABLE (0)
0083 #define BTRFS_DEV_STATE_IN_FS_METADATA (1)
0084 #define BTRFS_DEV_STATE_MISSING (2)
0085 #define BTRFS_DEV_STATE_REPLACE_TGT (3)
0086 #define BTRFS_DEV_STATE_FLUSH_SENT (4)
0087 #define BTRFS_DEV_STATE_NO_READA (5)
0088
0089 struct btrfs_zoned_device_info;
0090
0091 struct btrfs_device {
0092 struct list_head dev_list;
0093 struct list_head dev_alloc_list;
0094 struct list_head post_commit_list;
0095 struct btrfs_fs_devices *fs_devices;
0096 struct btrfs_fs_info *fs_info;
0097
0098 struct rcu_string __rcu *name;
0099
0100 u64 generation;
0101
0102 struct block_device *bdev;
0103
0104 struct btrfs_zoned_device_info *zone_info;
0105
0106
0107 fmode_t mode;
0108
0109
0110
0111
0112
0113 dev_t devt;
0114 unsigned long dev_state;
0115 blk_status_t last_flush_error;
0116
0117 #ifdef __BTRFS_NEED_DEVICE_DATA_ORDERED
0118 seqcount_t data_seqcount;
0119 #endif
0120
0121
0122 u64 devid;
0123
0124
0125 u64 total_bytes;
0126
0127
0128 u64 disk_total_bytes;
0129
0130
0131 u64 bytes_used;
0132
0133
0134 u32 io_align;
0135
0136
0137 u32 io_width;
0138
0139 u64 type;
0140
0141
0142 u32 sector_size;
0143
0144
0145 u8 uuid[BTRFS_UUID_SIZE];
0146
0147
0148
0149
0150
0151
0152
0153 u64 commit_total_bytes;
0154
0155
0156 u64 commit_bytes_used;
0157
0158
0159 struct bio flush_bio;
0160 struct completion flush_wait;
0161
0162
0163 struct scrub_ctx *scrub_ctx;
0164
0165
0166
0167 int dev_stats_valid;
0168
0169
0170 atomic_t dev_stats_ccnt;
0171 atomic_t dev_stat_values[BTRFS_DEV_STAT_VALUES_MAX];
0172
0173 struct extent_io_tree alloc_state;
0174
0175 struct completion kobj_unregister;
0176
0177 struct kobject devid_kobj;
0178
0179
0180 u64 scrub_speed_max;
0181 };
0182
0183
0184
0185
0186
0187 #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
0188 #define BTRFS_DEVICE_GETSET_FUNCS(name) \
0189 static inline u64 \
0190 btrfs_device_get_##name(const struct btrfs_device *dev) \
0191 { \
0192 u64 size; \
0193 unsigned int seq; \
0194 \
0195 do { \
0196 seq = read_seqcount_begin(&dev->data_seqcount); \
0197 size = dev->name; \
0198 } while (read_seqcount_retry(&dev->data_seqcount, seq)); \
0199 return size; \
0200 } \
0201 \
0202 static inline void \
0203 btrfs_device_set_##name(struct btrfs_device *dev, u64 size) \
0204 { \
0205 preempt_disable(); \
0206 write_seqcount_begin(&dev->data_seqcount); \
0207 dev->name = size; \
0208 write_seqcount_end(&dev->data_seqcount); \
0209 preempt_enable(); \
0210 }
0211 #elif BITS_PER_LONG==32 && defined(CONFIG_PREEMPTION)
0212 #define BTRFS_DEVICE_GETSET_FUNCS(name) \
0213 static inline u64 \
0214 btrfs_device_get_##name(const struct btrfs_device *dev) \
0215 { \
0216 u64 size; \
0217 \
0218 preempt_disable(); \
0219 size = dev->name; \
0220 preempt_enable(); \
0221 return size; \
0222 } \
0223 \
0224 static inline void \
0225 btrfs_device_set_##name(struct btrfs_device *dev, u64 size) \
0226 { \
0227 preempt_disable(); \
0228 dev->name = size; \
0229 preempt_enable(); \
0230 }
0231 #else
0232 #define BTRFS_DEVICE_GETSET_FUNCS(name) \
0233 static inline u64 \
0234 btrfs_device_get_##name(const struct btrfs_device *dev) \
0235 { \
0236 return dev->name; \
0237 } \
0238 \
0239 static inline void \
0240 btrfs_device_set_##name(struct btrfs_device *dev, u64 size) \
0241 { \
0242 dev->name = size; \
0243 }
0244 #endif
0245
0246 BTRFS_DEVICE_GETSET_FUNCS(total_bytes);
0247 BTRFS_DEVICE_GETSET_FUNCS(disk_total_bytes);
0248 BTRFS_DEVICE_GETSET_FUNCS(bytes_used);
0249
0250 enum btrfs_chunk_allocation_policy {
0251 BTRFS_CHUNK_ALLOC_REGULAR,
0252 BTRFS_CHUNK_ALLOC_ZONED,
0253 };
0254
0255
0256
0257
0258
0259 enum btrfs_read_policy {
0260
0261 BTRFS_READ_POLICY_PID,
0262 BTRFS_NR_READ_POLICY,
0263 };
0264
0265 struct btrfs_fs_devices {
0266 u8 fsid[BTRFS_FSID_SIZE];
0267 u8 metadata_uuid[BTRFS_FSID_SIZE];
0268 bool fsid_change;
0269 struct list_head fs_list;
0270
0271
0272
0273
0274
0275 u64 num_devices;
0276
0277
0278
0279
0280
0281 u64 open_devices;
0282
0283
0284 u64 rw_devices;
0285
0286
0287 u64 missing_devices;
0288 u64 total_rw_bytes;
0289
0290
0291
0292
0293
0294
0295 u64 total_devices;
0296
0297
0298 u64 latest_generation;
0299
0300
0301
0302
0303
0304 struct btrfs_device *latest_dev;
0305
0306
0307
0308
0309
0310
0311
0312 struct mutex device_list_mutex;
0313
0314
0315 struct list_head devices;
0316
0317
0318
0319
0320
0321 struct list_head alloc_list;
0322
0323 struct list_head seed_list;
0324 bool seeding;
0325
0326 int opened;
0327
0328
0329
0330
0331 bool rotating;
0332
0333 struct btrfs_fs_info *fs_info;
0334
0335 struct kobject fsid_kobj;
0336 struct kobject *devices_kobj;
0337 struct kobject *devinfo_kobj;
0338 struct completion kobj_unregister;
0339
0340 enum btrfs_chunk_allocation_policy chunk_alloc_policy;
0341
0342
0343 enum btrfs_read_policy read_policy;
0344 };
0345
0346 #define BTRFS_BIO_INLINE_CSUM_SIZE 64
0347
0348 #define BTRFS_MAX_DEVS(info) ((BTRFS_MAX_ITEM_SIZE(info) \
0349 - sizeof(struct btrfs_chunk)) \
0350 / sizeof(struct btrfs_stripe) + 1)
0351
0352 #define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \
0353 - 2 * sizeof(struct btrfs_disk_key) \
0354 - 2 * sizeof(struct btrfs_chunk)) \
0355 / sizeof(struct btrfs_stripe) + 1)
0356
0357
0358
0359
0360
0361
0362 #define BTRFS_MAX_BIO_SECTORS (256)
0363
0364
0365
0366
0367
0368
0369 struct btrfs_bio {
0370 unsigned int mirror_num;
0371
0372
0373 u64 file_offset;
0374
0375
0376 struct btrfs_device *device;
0377 u8 *csum;
0378 u8 csum_inline[BTRFS_BIO_INLINE_CSUM_SIZE];
0379 struct bvec_iter iter;
0380
0381
0382 struct work_struct end_io_work;
0383
0384
0385
0386
0387
0388 struct bio bio;
0389 };
0390
0391 static inline struct btrfs_bio *btrfs_bio(struct bio *bio)
0392 {
0393 return container_of(bio, struct btrfs_bio, bio);
0394 }
0395
0396 static inline void btrfs_bio_free_csum(struct btrfs_bio *bbio)
0397 {
0398 if (bbio->csum != bbio->csum_inline) {
0399 kfree(bbio->csum);
0400 bbio->csum = NULL;
0401 }
0402 }
0403
0404
0405
0406
0407
0408
0409
0410
0411
0412 #define btrfs_bio_for_each_sector(fs_info, bvl, bbio, iter, bio_offset) \
0413 for ((iter) = (bbio)->iter, (bio_offset) = 0; \
0414 (iter).bi_size && \
0415 (((bvl) = bio_iter_iovec((&(bbio)->bio), (iter))), 1); \
0416 (bio_offset) += fs_info->sectorsize, \
0417 bio_advance_iter_single(&(bbio)->bio, &(iter), \
0418 (fs_info)->sectorsize))
0419
0420 struct btrfs_io_stripe {
0421 struct btrfs_device *dev;
0422 union {
0423
0424 u64 physical;
0425
0426 struct btrfs_io_context *bioc;
0427 };
0428 };
0429
0430 struct btrfs_discard_stripe {
0431 struct btrfs_device *dev;
0432 u64 physical;
0433 u64 length;
0434 };
0435
0436
0437
0438
0439
0440
0441
0442
0443
0444
0445
0446
0447
0448
0449
0450
0451
0452 struct btrfs_io_context {
0453 refcount_t refs;
0454 atomic_t stripes_pending;
0455 struct btrfs_fs_info *fs_info;
0456 u64 map_type;
0457 bio_end_io_t *end_io;
0458 struct bio *orig_bio;
0459 void *private;
0460 atomic_t error;
0461 int max_errors;
0462 int num_stripes;
0463 int mirror_num;
0464 int num_tgtdevs;
0465 int *tgtdev_map;
0466
0467
0468
0469
0470
0471 u64 *raid_map;
0472 struct btrfs_io_stripe stripes[];
0473 };
0474
0475 struct btrfs_device_info {
0476 struct btrfs_device *dev;
0477 u64 dev_offset;
0478 u64 max_avail;
0479 u64 total_avail;
0480 };
0481
0482 struct btrfs_raid_attr {
0483 u8 sub_stripes;
0484 u8 dev_stripes;
0485 u8 devs_max;
0486 u8 devs_min;
0487 u8 tolerated_failures;
0488 u8 devs_increment;
0489 u8 ncopies;
0490 u8 nparity;
0491
0492 u8 mindev_error;
0493 const char raid_name[8];
0494 u64 bg_flag;
0495 };
0496
0497 extern const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES];
0498
0499 struct map_lookup {
0500 u64 type;
0501 int io_align;
0502 int io_width;
0503 u32 stripe_len;
0504 int num_stripes;
0505 int sub_stripes;
0506 int verified_stripes;
0507 struct btrfs_io_stripe stripes[];
0508 };
0509
0510 #define map_lookup_size(n) (sizeof(struct map_lookup) + \
0511 (sizeof(struct btrfs_io_stripe) * (n)))
0512
0513 struct btrfs_balance_args;
0514 struct btrfs_balance_progress;
0515 struct btrfs_balance_control {
0516 struct btrfs_balance_args data;
0517 struct btrfs_balance_args meta;
0518 struct btrfs_balance_args sys;
0519
0520 u64 flags;
0521
0522 struct btrfs_balance_progress stat;
0523 };
0524
0525
0526
0527
0528 struct btrfs_dev_lookup_args {
0529 u64 devid;
0530 u8 *uuid;
0531 u8 *fsid;
0532 bool missing;
0533 };
0534
0535
0536 #define BTRFS_DEV_LOOKUP_ARGS_INIT { .devid = (u64)-1 }
0537
0538 #define BTRFS_DEV_LOOKUP_ARGS(name) \
0539 struct btrfs_dev_lookup_args name = BTRFS_DEV_LOOKUP_ARGS_INIT
0540
0541 enum btrfs_map_op {
0542 BTRFS_MAP_READ,
0543 BTRFS_MAP_WRITE,
0544 BTRFS_MAP_DISCARD,
0545 BTRFS_MAP_GET_READ_MIRRORS,
0546 };
0547
0548 static inline enum btrfs_map_op btrfs_op(struct bio *bio)
0549 {
0550 switch (bio_op(bio)) {
0551 case REQ_OP_DISCARD:
0552 return BTRFS_MAP_DISCARD;
0553 case REQ_OP_WRITE:
0554 case REQ_OP_ZONE_APPEND:
0555 return BTRFS_MAP_WRITE;
0556 default:
0557 WARN_ON_ONCE(1);
0558 fallthrough;
0559 case REQ_OP_READ:
0560 return BTRFS_MAP_READ;
0561 }
0562 }
0563
0564 void btrfs_get_bioc(struct btrfs_io_context *bioc);
0565 void btrfs_put_bioc(struct btrfs_io_context *bioc);
0566 int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
0567 u64 logical, u64 *length,
0568 struct btrfs_io_context **bioc_ret, int mirror_num);
0569 int btrfs_map_sblock(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
0570 u64 logical, u64 *length,
0571 struct btrfs_io_context **bioc_ret);
0572 struct btrfs_discard_stripe *btrfs_map_discard(struct btrfs_fs_info *fs_info,
0573 u64 logical, u64 *length_ret,
0574 u32 *num_stripes);
0575 int btrfs_get_io_geometry(struct btrfs_fs_info *fs_info, struct extent_map *map,
0576 enum btrfs_map_op op, u64 logical,
0577 struct btrfs_io_geometry *io_geom);
0578 int btrfs_read_sys_array(struct btrfs_fs_info *fs_info);
0579 int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info);
0580 struct btrfs_block_group *btrfs_create_chunk(struct btrfs_trans_handle *trans,
0581 u64 type);
0582 void btrfs_mapping_tree_free(struct extent_map_tree *tree);
0583 void btrfs_submit_bio(struct btrfs_fs_info *fs_info, struct bio *bio, int mirror_num);
0584 int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
0585 fmode_t flags, void *holder);
0586 struct btrfs_device *btrfs_scan_one_device(const char *path,
0587 fmode_t flags, void *holder);
0588 int btrfs_forget_devices(dev_t devt);
0589 void btrfs_close_devices(struct btrfs_fs_devices *fs_devices);
0590 void btrfs_free_extra_devids(struct btrfs_fs_devices *fs_devices);
0591 void btrfs_assign_next_active_device(struct btrfs_device *device,
0592 struct btrfs_device *this_dev);
0593 struct btrfs_device *btrfs_find_device_by_devspec(struct btrfs_fs_info *fs_info,
0594 u64 devid,
0595 const char *devpath);
0596 int btrfs_get_dev_args_from_path(struct btrfs_fs_info *fs_info,
0597 struct btrfs_dev_lookup_args *args,
0598 const char *path);
0599 struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
0600 const u64 *devid,
0601 const u8 *uuid);
0602 void btrfs_put_dev_args_from_path(struct btrfs_dev_lookup_args *args);
0603 void btrfs_free_device(struct btrfs_device *device);
0604 int btrfs_rm_device(struct btrfs_fs_info *fs_info,
0605 struct btrfs_dev_lookup_args *args,
0606 struct block_device **bdev, fmode_t *mode);
0607 void __exit btrfs_cleanup_fs_uuids(void);
0608 int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len);
0609 int btrfs_grow_device(struct btrfs_trans_handle *trans,
0610 struct btrfs_device *device, u64 new_size);
0611 struct btrfs_device *btrfs_find_device(const struct btrfs_fs_devices *fs_devices,
0612 const struct btrfs_dev_lookup_args *args);
0613 int btrfs_shrink_device(struct btrfs_device *device, u64 new_size);
0614 int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *path);
0615 int btrfs_balance(struct btrfs_fs_info *fs_info,
0616 struct btrfs_balance_control *bctl,
0617 struct btrfs_ioctl_balance_args *bargs);
0618 void btrfs_describe_block_groups(u64 flags, char *buf, u32 size_buf);
0619 int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info);
0620 int btrfs_recover_balance(struct btrfs_fs_info *fs_info);
0621 int btrfs_pause_balance(struct btrfs_fs_info *fs_info);
0622 int btrfs_relocate_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset);
0623 int btrfs_cancel_balance(struct btrfs_fs_info *fs_info);
0624 int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info);
0625 int btrfs_uuid_scan_kthread(void *data);
0626 bool btrfs_chunk_writeable(struct btrfs_fs_info *fs_info, u64 chunk_offset);
0627 int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes,
0628 u64 *start, u64 *max_avail);
0629 void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index);
0630 int btrfs_get_dev_stats(struct btrfs_fs_info *fs_info,
0631 struct btrfs_ioctl_get_dev_stats *stats);
0632 void btrfs_init_devices_late(struct btrfs_fs_info *fs_info);
0633 int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info);
0634 int btrfs_run_dev_stats(struct btrfs_trans_handle *trans);
0635 void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_device *srcdev);
0636 void btrfs_rm_dev_replace_free_srcdev(struct btrfs_device *srcdev);
0637 void btrfs_destroy_dev_replace_tgtdev(struct btrfs_device *tgtdev);
0638 int btrfs_is_parity_mirror(struct btrfs_fs_info *fs_info,
0639 u64 logical, u64 len);
0640 unsigned long btrfs_full_stripe_len(struct btrfs_fs_info *fs_info,
0641 u64 logical);
0642 u64 btrfs_calc_stripe_length(const struct extent_map *em);
0643 int btrfs_nr_parity_stripes(u64 type);
0644 int btrfs_chunk_alloc_add_chunk_item(struct btrfs_trans_handle *trans,
0645 struct btrfs_block_group *bg);
0646 int btrfs_remove_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset);
0647 struct extent_map *btrfs_get_chunk_map(struct btrfs_fs_info *fs_info,
0648 u64 logical, u64 length);
0649 void btrfs_release_disk_super(struct btrfs_super_block *super);
0650
0651 static inline void btrfs_dev_stat_inc(struct btrfs_device *dev,
0652 int index)
0653 {
0654 atomic_inc(dev->dev_stat_values + index);
0655
0656
0657
0658
0659
0660
0661 smp_mb__before_atomic();
0662 atomic_inc(&dev->dev_stats_ccnt);
0663 }
0664
0665 static inline int btrfs_dev_stat_read(struct btrfs_device *dev,
0666 int index)
0667 {
0668 return atomic_read(dev->dev_stat_values + index);
0669 }
0670
0671 static inline int btrfs_dev_stat_read_and_reset(struct btrfs_device *dev,
0672 int index)
0673 {
0674 int ret;
0675
0676 ret = atomic_xchg(dev->dev_stat_values + index, 0);
0677
0678
0679
0680
0681
0682
0683
0684 atomic_inc(&dev->dev_stats_ccnt);
0685 return ret;
0686 }
0687
0688 static inline void btrfs_dev_stat_set(struct btrfs_device *dev,
0689 int index, unsigned long val)
0690 {
0691 atomic_set(dev->dev_stat_values + index, val);
0692
0693
0694
0695
0696
0697
0698 smp_mb__before_atomic();
0699 atomic_inc(&dev->dev_stats_ccnt);
0700 }
0701
0702 void btrfs_commit_device_sizes(struct btrfs_transaction *trans);
0703
0704 struct list_head * __attribute_const__ btrfs_get_fs_uuids(void);
0705 bool btrfs_check_rw_degradable(struct btrfs_fs_info *fs_info,
0706 struct btrfs_device *failing_dev);
0707 void btrfs_scratch_superblocks(struct btrfs_fs_info *fs_info,
0708 struct block_device *bdev,
0709 const char *device_path);
0710
0711 enum btrfs_raid_types __attribute_const__ btrfs_bg_flags_to_raid_index(u64 flags);
0712 int btrfs_bg_type_to_factor(u64 flags);
0713 const char *btrfs_bg_type_to_raid_name(u64 flags);
0714 int btrfs_verify_dev_extents(struct btrfs_fs_info *fs_info);
0715 bool btrfs_repair_one_zone(struct btrfs_fs_info *fs_info, u64 logical);
0716
0717 #endif