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