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