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