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