19888c340SDavid Sterba /* SPDX-License-Identifier: GPL-2.0 */
20b86a832SChris Mason /*
30b86a832SChris Mason * Copyright (C) 2007 Oracle. All rights reserved.
40b86a832SChris Mason */
50b86a832SChris Mason
69888c340SDavid Sterba #ifndef BTRFS_VOLUMES_H
79888c340SDavid Sterba #define BTRFS_VOLUMES_H
88790d502SChris Mason
9b2117a39SMiao Xie #include <linux/sort.h>
1055e301fdSFilipe Brandenburger #include <linux/btrfs.h>
118b712842SChris Mason #include "async-thread.h"
12b31bed17SJosef Bacik #include "messages.h"
1327137facSChristoph Hellwig #include "tree-checker.h"
14cb3e217bSQu Wenruo #include "rcu-string.h"
15cea9e445SChris Mason
16fce466eaSQu Wenruo #define BTRFS_MAX_DATA_CHUNK_SIZE (10ULL * SZ_1G)
17fce466eaSQu Wenruo
18*f00545e8SLuca Stefani /*
19*f00545e8SLuca Stefani * Arbitratry maximum size of one discard request to limit potentially long time
20*f00545e8SLuca Stefani * spent in blkdev_issue_discard().
21*f00545e8SLuca Stefani */
22*f00545e8SLuca Stefani #define BTRFS_MAX_DISCARD_CHUNK_SIZE (SZ_1G)
23*f00545e8SLuca Stefani
2467a2c45eSMiao Xie extern struct mutex uuid_mutex;
2567a2c45eSMiao Xie
26ee22184bSByongho Lee #define BTRFS_STRIPE_LEN SZ_64K
27a97699d1SQu Wenruo #define BTRFS_STRIPE_LEN_SHIFT (16)
28a97699d1SQu Wenruo #define BTRFS_STRIPE_LEN_MASK (BTRFS_STRIPE_LEN - 1)
29a97699d1SQu Wenruo
30a97699d1SQu Wenruo static_assert(const_ilog2(BTRFS_STRIPE_LEN) == BTRFS_STRIPE_LEN_SHIFT);
31b2117a39SMiao Xie
32719fae89SQu Wenruo /* Used by sanity check for btrfs_raid_types. */
33719fae89SQu Wenruo #define const_ffs(n) (__builtin_ctzll(n) + 1)
34719fae89SQu Wenruo
35719fae89SQu Wenruo /*
36719fae89SQu Wenruo * The conversion from BTRFS_BLOCK_GROUP_* bits to btrfs_raid_type requires
37719fae89SQu Wenruo * RAID0 always to be the lowest profile bit.
38719fae89SQu Wenruo * Although it's part of on-disk format and should never change, do extra
39719fae89SQu Wenruo * compile-time sanity checks.
40719fae89SQu Wenruo */
41719fae89SQu Wenruo static_assert(const_ffs(BTRFS_BLOCK_GROUP_RAID0) <
42719fae89SQu Wenruo const_ffs(BTRFS_BLOCK_GROUP_PROFILE_MASK & ~BTRFS_BLOCK_GROUP_RAID0));
43719fae89SQu Wenruo static_assert(const_ilog2(BTRFS_BLOCK_GROUP_RAID0) >
44719fae89SQu Wenruo ilog2(BTRFS_BLOCK_GROUP_TYPE_MASK));
45719fae89SQu Wenruo
46719fae89SQu Wenruo /* ilog2() can handle both constants and variables */
47719fae89SQu Wenruo #define BTRFS_BG_FLAG_TO_INDEX(profile) \
48719fae89SQu Wenruo ilog2((profile) >> (ilog2(BTRFS_BLOCK_GROUP_RAID0) - 1))
49719fae89SQu Wenruo
50f04fbcc6SQu Wenruo enum btrfs_raid_types {
51719fae89SQu Wenruo /* SINGLE is the special one as it doesn't have on-disk bit. */
52719fae89SQu Wenruo BTRFS_RAID_SINGLE = 0,
53719fae89SQu Wenruo
54719fae89SQu Wenruo BTRFS_RAID_RAID0 = BTRFS_BG_FLAG_TO_INDEX(BTRFS_BLOCK_GROUP_RAID0),
55719fae89SQu Wenruo BTRFS_RAID_RAID1 = BTRFS_BG_FLAG_TO_INDEX(BTRFS_BLOCK_GROUP_RAID1),
56719fae89SQu Wenruo BTRFS_RAID_DUP = BTRFS_BG_FLAG_TO_INDEX(BTRFS_BLOCK_GROUP_DUP),
57719fae89SQu Wenruo BTRFS_RAID_RAID10 = BTRFS_BG_FLAG_TO_INDEX(BTRFS_BLOCK_GROUP_RAID10),
58719fae89SQu Wenruo BTRFS_RAID_RAID5 = BTRFS_BG_FLAG_TO_INDEX(BTRFS_BLOCK_GROUP_RAID5),
59719fae89SQu Wenruo BTRFS_RAID_RAID6 = BTRFS_BG_FLAG_TO_INDEX(BTRFS_BLOCK_GROUP_RAID6),
60719fae89SQu Wenruo BTRFS_RAID_RAID1C3 = BTRFS_BG_FLAG_TO_INDEX(BTRFS_BLOCK_GROUP_RAID1C3),
61719fae89SQu Wenruo BTRFS_RAID_RAID1C4 = BTRFS_BG_FLAG_TO_INDEX(BTRFS_BLOCK_GROUP_RAID1C4),
62719fae89SQu Wenruo
63f04fbcc6SQu Wenruo BTRFS_NR_RAID_TYPES
64f04fbcc6SQu Wenruo };
65f04fbcc6SQu Wenruo
667cc8e58dSMiao Xie /*
677cc8e58dSMiao Xie * Use sequence counter to get consistent device stat data on
687cc8e58dSMiao Xie * 32-bit processors.
697cc8e58dSMiao Xie */
707cc8e58dSMiao Xie #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
717cc8e58dSMiao Xie #include <linux/seqlock.h>
727cc8e58dSMiao Xie #define __BTRFS_NEED_DEVICE_DATA_ORDERED
73c41ec452SSu Yue #define btrfs_device_data_ordered_init(device) \
74c41ec452SSu Yue seqcount_init(&device->data_seqcount)
757cc8e58dSMiao Xie #else
76c41ec452SSu Yue #define btrfs_device_data_ordered_init(device) do { } while (0)
777cc8e58dSMiao Xie #endif
787cc8e58dSMiao Xie
79ebbede42SAnand Jain #define BTRFS_DEV_STATE_WRITEABLE (0)
80e12c9621SAnand Jain #define BTRFS_DEV_STATE_IN_FS_METADATA (1)
81e6e674bdSAnand Jain #define BTRFS_DEV_STATE_MISSING (2)
82401e29c1SAnand Jain #define BTRFS_DEV_STATE_REPLACE_TGT (3)
831c3063b6SAnand Jain #define BTRFS_DEV_STATE_FLUSH_SENT (4)
8466d204a1SFilipe Manana #define BTRFS_DEV_STATE_NO_READA (5)
85ebbede42SAnand Jain
865b316468SNaohiro Aota struct btrfs_zoned_device_info;
875b316468SNaohiro Aota
880b86a832SChris Mason struct btrfs_device {
890b6f5d40SNikolay Borisov struct list_head dev_list; /* device_list_mutex */
900b6f5d40SNikolay Borisov struct list_head dev_alloc_list; /* chunk mutex */
91bbbf7243SNikolay Borisov struct list_head post_commit_list; /* chunk mutex */
922b82032cSYan Zheng struct btrfs_fs_devices *fs_devices;
93fb456252SJeff Mahoney struct btrfs_fs_info *fs_info;
94ffbd517dSChris Mason
958d1a7aaeSMadhuparna Bhowmik struct rcu_string __rcu *name;
96d5ee37bcSMiao Xie
97d5ee37bcSMiao Xie u64 generation;
98d5ee37bcSMiao Xie
99d5ee37bcSMiao Xie struct block_device *bdev;
100d5ee37bcSMiao Xie
1015b316468SNaohiro Aota struct btrfs_zoned_device_info *zone_info;
1025b316468SNaohiro Aota
1032736e8eeSChristoph Hellwig /* block device holder for blkdev_get/put */
1042736e8eeSChristoph Hellwig void *holder;
105d5ee37bcSMiao Xie
1064889bc05SAnand Jain /*
1074889bc05SAnand Jain * Device's major-minor number. Must be set even if the device is not
1084889bc05SAnand Jain * opened (bdev == NULL), unless the device is missing.
1094889bc05SAnand Jain */
1104889bc05SAnand Jain dev_t devt;
111ebbede42SAnand Jain unsigned long dev_state;
11258efbc9fSOmar Sandoval blk_status_t last_flush_error;
113b3075717SChris Mason
1147cc8e58dSMiao Xie #ifdef __BTRFS_NEED_DEVICE_DATA_ORDERED
115c41ec452SSu Yue seqcount_t data_seqcount;
1167cc8e58dSMiao Xie #endif
1177cc8e58dSMiao Xie
1180b86a832SChris Mason /* the internal btrfs device id */
1190b86a832SChris Mason u64 devid;
1200b86a832SChris Mason
1216ba40b61SMiao Xie /* size of the device in memory */
1220b86a832SChris Mason u64 total_bytes;
1230b86a832SChris Mason
1246ba40b61SMiao Xie /* size of the device on disk */
125d6397baeSChris Ball u64 disk_total_bytes;
126d6397baeSChris Ball
1270b86a832SChris Mason /* bytes used */
1280b86a832SChris Mason u64 bytes_used;
1290b86a832SChris Mason
1300b86a832SChris Mason /* optimal io alignment for this device */
1310b86a832SChris Mason u32 io_align;
1320b86a832SChris Mason
1330b86a832SChris Mason /* optimal io width for this device */
1340b86a832SChris Mason u32 io_width;
1353c45bfc1SDulshani Gunawardhana /* type and info about this device */
1363c45bfc1SDulshani Gunawardhana u64 type;
1370b86a832SChris Mason
1380b86a832SChris Mason /* minimal io size for this device */
1390b86a832SChris Mason u32 sector_size;
1400b86a832SChris Mason
1410b86a832SChris Mason /* physical drive uuid (or lvm uuid) */
142e17cade2SChris Mason u8 uuid[BTRFS_UUID_SIZE];
1438b712842SChris Mason
144935e5cc9SMiao Xie /*
145935e5cc9SMiao Xie * size of the device on the current transaction
146935e5cc9SMiao Xie *
147935e5cc9SMiao Xie * This variant is update when committing the transaction,
148bbbf7243SNikolay Borisov * and protected by chunk mutex
149935e5cc9SMiao Xie */
150935e5cc9SMiao Xie u64 commit_total_bytes;
151935e5cc9SMiao Xie
152ce7213c7SMiao Xie /* bytes used on the current transaction */
153ce7213c7SMiao Xie u64 commit_bytes_used;
154935e5cc9SMiao Xie
155f9e69aa9SChristoph Hellwig /* Bio used for flushing device barriers */
156f9e69aa9SChristoph Hellwig struct bio flush_bio;
1573c45bfc1SDulshani Gunawardhana struct completion flush_wait;
1583c45bfc1SDulshani Gunawardhana
159a2de733cSArne Jansen /* per-device scrub information */
160cadbc0a0SAnand Jain struct scrub_ctx *scrub_ctx;
161a2de733cSArne Jansen
162442a4f63SStefan Behrens /* disk I/O failure stats. For detailed description refer to
163442a4f63SStefan Behrens * enum btrfs_dev_stat_values in ioctl.h */
164733f4fbbSStefan Behrens int dev_stats_valid;
165addc3fa7SMiao Xie
166addc3fa7SMiao Xie /* Counter to record the change of device stats */
167addc3fa7SMiao Xie atomic_t dev_stats_ccnt;
168442a4f63SStefan Behrens atomic_t dev_stat_values[BTRFS_DEV_STAT_VALUES_MAX];
1691c11b63eSJeff Mahoney
1701c11b63eSJeff Mahoney struct extent_io_tree alloc_state;
171668e48afSAnand Jain
172668e48afSAnand Jain struct completion kobj_unregister;
173668e48afSAnand Jain /* For sysfs/FSID/devinfo/devid/ */
174668e48afSAnand Jain struct kobject devid_kobj;
175eb3b5053SDavid Sterba
176eb3b5053SDavid Sterba /* Bandwidth limit for scrub, in bytes */
177eb3b5053SDavid Sterba u64 scrub_speed_max;
1780b86a832SChris Mason };
1790b86a832SChris Mason
1807cc8e58dSMiao Xie /*
1812103da3bSJosef Bacik * Block group or device which contains an active swapfile. Used for preventing
1822103da3bSJosef Bacik * unsafe operations while a swapfile is active.
1832103da3bSJosef Bacik *
1842103da3bSJosef Bacik * These are sorted on (ptr, inode) (note that a block group or device can
1852103da3bSJosef Bacik * contain more than one swapfile). We compare the pointer values because we
1862103da3bSJosef Bacik * don't actually care what the object is, we just need a quick check whether
1872103da3bSJosef Bacik * the object exists in the rbtree.
1882103da3bSJosef Bacik */
1892103da3bSJosef Bacik struct btrfs_swapfile_pin {
1902103da3bSJosef Bacik struct rb_node node;
1912103da3bSJosef Bacik void *ptr;
1922103da3bSJosef Bacik struct inode *inode;
1932103da3bSJosef Bacik /*
1942103da3bSJosef Bacik * If true, ptr points to a struct btrfs_block_group. Otherwise, ptr
1952103da3bSJosef Bacik * points to a struct btrfs_device.
1962103da3bSJosef Bacik */
1972103da3bSJosef Bacik bool is_block_group;
1982103da3bSJosef Bacik /*
1992103da3bSJosef Bacik * Only used when 'is_block_group' is true and it is the number of
2002103da3bSJosef Bacik * extents used by a swapfile for this block group ('ptr' field).
2012103da3bSJosef Bacik */
2022103da3bSJosef Bacik int bg_extent_count;
2032103da3bSJosef Bacik };
2042103da3bSJosef Bacik
2052103da3bSJosef Bacik /*
2067cc8e58dSMiao Xie * If we read those variants at the context of their own lock, we needn't
2077cc8e58dSMiao Xie * use the following helpers, reading them directly is safe.
2087cc8e58dSMiao Xie */
2097cc8e58dSMiao Xie #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
2107cc8e58dSMiao Xie #define BTRFS_DEVICE_GETSET_FUNCS(name) \
2117cc8e58dSMiao Xie static inline u64 \
2127cc8e58dSMiao Xie btrfs_device_get_##name(const struct btrfs_device *dev) \
2137cc8e58dSMiao Xie { \
2147cc8e58dSMiao Xie u64 size; \
2157cc8e58dSMiao Xie unsigned int seq; \
2167cc8e58dSMiao Xie \
2177cc8e58dSMiao Xie do { \
2187cc8e58dSMiao Xie seq = read_seqcount_begin(&dev->data_seqcount); \
2197cc8e58dSMiao Xie size = dev->name; \
2207cc8e58dSMiao Xie } while (read_seqcount_retry(&dev->data_seqcount, seq)); \
2217cc8e58dSMiao Xie return size; \
2227cc8e58dSMiao Xie } \
2237cc8e58dSMiao Xie \
2247cc8e58dSMiao Xie static inline void \
2257cc8e58dSMiao Xie btrfs_device_set_##name(struct btrfs_device *dev, u64 size) \
2267cc8e58dSMiao Xie { \
227c41ec452SSu Yue preempt_disable(); \
2287cc8e58dSMiao Xie write_seqcount_begin(&dev->data_seqcount); \
2297cc8e58dSMiao Xie dev->name = size; \
2307cc8e58dSMiao Xie write_seqcount_end(&dev->data_seqcount); \
231c41ec452SSu Yue preempt_enable(); \
2327cc8e58dSMiao Xie }
23394545870SThomas Gleixner #elif BITS_PER_LONG==32 && defined(CONFIG_PREEMPTION)
2347cc8e58dSMiao Xie #define BTRFS_DEVICE_GETSET_FUNCS(name) \
2357cc8e58dSMiao Xie static inline u64 \
2367cc8e58dSMiao Xie btrfs_device_get_##name(const struct btrfs_device *dev) \
2377cc8e58dSMiao Xie { \
2387cc8e58dSMiao Xie u64 size; \
2397cc8e58dSMiao Xie \
2407cc8e58dSMiao Xie preempt_disable(); \
2417cc8e58dSMiao Xie size = dev->name; \
2427cc8e58dSMiao Xie preempt_enable(); \
2437cc8e58dSMiao Xie return size; \
2447cc8e58dSMiao Xie } \
2457cc8e58dSMiao Xie \
2467cc8e58dSMiao Xie static inline void \
2477cc8e58dSMiao Xie btrfs_device_set_##name(struct btrfs_device *dev, u64 size) \
2487cc8e58dSMiao Xie { \
2497cc8e58dSMiao Xie preempt_disable(); \
2507cc8e58dSMiao Xie dev->name = size; \
2517cc8e58dSMiao Xie preempt_enable(); \
2527cc8e58dSMiao Xie }
2537cc8e58dSMiao Xie #else
2547cc8e58dSMiao Xie #define BTRFS_DEVICE_GETSET_FUNCS(name) \
2557cc8e58dSMiao Xie static inline u64 \
2567cc8e58dSMiao Xie btrfs_device_get_##name(const struct btrfs_device *dev) \
2577cc8e58dSMiao Xie { \
2587cc8e58dSMiao Xie return dev->name; \
2597cc8e58dSMiao Xie } \
2607cc8e58dSMiao Xie \
2617cc8e58dSMiao Xie static inline void \
2627cc8e58dSMiao Xie btrfs_device_set_##name(struct btrfs_device *dev, u64 size) \
2637cc8e58dSMiao Xie { \
2647cc8e58dSMiao Xie dev->name = size; \
2657cc8e58dSMiao Xie }
2667cc8e58dSMiao Xie #endif
2677cc8e58dSMiao Xie
2687cc8e58dSMiao Xie BTRFS_DEVICE_GETSET_FUNCS(total_bytes);
2697cc8e58dSMiao Xie BTRFS_DEVICE_GETSET_FUNCS(disk_total_bytes);
2707cc8e58dSMiao Xie BTRFS_DEVICE_GETSET_FUNCS(bytes_used);
2717cc8e58dSMiao Xie
272c4a816c6SNaohiro Aota enum btrfs_chunk_allocation_policy {
273c4a816c6SNaohiro Aota BTRFS_CHUNK_ALLOC_REGULAR,
2741cd6121fSNaohiro Aota BTRFS_CHUNK_ALLOC_ZONED,
275c4a816c6SNaohiro Aota };
276c4a816c6SNaohiro Aota
27733fd2f71SAnand Jain /*
27833fd2f71SAnand Jain * Read policies for mirrored block group profiles, read picks the stripe based
27933fd2f71SAnand Jain * on these policies.
28033fd2f71SAnand Jain */
28133fd2f71SAnand Jain enum btrfs_read_policy {
28233fd2f71SAnand Jain /* Use process PID to choose the stripe */
28333fd2f71SAnand Jain BTRFS_READ_POLICY_PID,
28433fd2f71SAnand Jain BTRFS_NR_READ_POLICY,
28533fd2f71SAnand Jain };
28633fd2f71SAnand Jain
2878a4b83ccSChris Mason struct btrfs_fs_devices {
2888a4b83ccSChris Mason u8 fsid[BTRFS_FSID_SIZE]; /* FS specific uuid */
289f62c302eSAnand Jain
290f62c302eSAnand Jain /*
291f62c302eSAnand Jain * UUID written into the btree blocks:
292f62c302eSAnand Jain *
293f62c302eSAnand Jain * - If metadata_uuid != fsid then super block must have
294f62c302eSAnand Jain * BTRFS_FEATURE_INCOMPAT_METADATA_UUID flag set.
295f62c302eSAnand Jain *
296f62c302eSAnand Jain * - Following shall be true at all times:
297f62c302eSAnand Jain * - metadata_uuid == btrfs_header::fsid
298f62c302eSAnand Jain * - metadata_uuid == btrfs_dev_item::fsid
299f62c302eSAnand Jain */
3007239ff4bSNikolay Borisov u8 metadata_uuid[BTRFS_FSID_SIZE];
301f62c302eSAnand Jain
302c4babc5eSAnand Jain struct list_head fs_list;
3038a4b83ccSChris Mason
304add9745aSAnand Jain /*
305add9745aSAnand Jain * Number of devices under this fsid including missing and
306add9745aSAnand Jain * replace-target device and excludes seed devices.
307add9745aSAnand Jain */
3088a4b83ccSChris Mason u64 num_devices;
309add9745aSAnand Jain
310add9745aSAnand Jain /*
311add9745aSAnand Jain * The number of devices that successfully opened, including
312add9745aSAnand Jain * replace-target, excludes seed devices.
313add9745aSAnand Jain */
314a0af469bSChris Mason u64 open_devices;
315add9745aSAnand Jain
316add9745aSAnand Jain /* The number of devices that are under the chunk allocation list. */
3172b82032cSYan Zheng u64 rw_devices;
318add9745aSAnand Jain
319add9745aSAnand Jain /* Count of missing devices under this fsid excluding seed device. */
320cd02dca5SChris Mason u64 missing_devices;
3212b82032cSYan Zheng u64 total_rw_bytes;
322add9745aSAnand Jain
323add9745aSAnand Jain /*
324add9745aSAnand Jain * Count of devices from btrfs_super_block::num_devices for this fsid,
325add9745aSAnand Jain * which includes the seed device, excludes the transient replace-target
326add9745aSAnand Jain * device.
327add9745aSAnand Jain */
32802db0844SJosef Bacik u64 total_devices;
329d1a63002SNikolay Borisov
330d1a63002SNikolay Borisov /* Highest generation number of seen devices */
331d1a63002SNikolay Borisov u64 latest_generation;
332d1a63002SNikolay Borisov
333d24fa5c1SAnand Jain /*
334d24fa5c1SAnand Jain * The mount device or a device with highest generation after removal
335d24fa5c1SAnand Jain * or replace.
336d24fa5c1SAnand Jain */
337d24fa5c1SAnand Jain struct btrfs_device *latest_dev;
338e5e9a520SChris Mason
339d85512d5SAnand Jain /*
340d85512d5SAnand Jain * All of the devices in the filesystem, protected by a mutex so we can
341d85512d5SAnand Jain * safely walk it to write out the super blocks without worrying about
342d85512d5SAnand Jain * adding/removing by the multi-device code. Scrubbing super block can
343d85512d5SAnand Jain * kick off supers writing by holding this mutex lock.
344e5e9a520SChris Mason */
345e5e9a520SChris Mason struct mutex device_list_mutex;
3460b6f5d40SNikolay Borisov
3470b6f5d40SNikolay Borisov /* List of all devices, protected by device_list_mutex */
3488a4b83ccSChris Mason struct list_head devices;
349b3075717SChris Mason
350d85512d5SAnand Jain /* Devices which can satisfy space allocation. Protected by * chunk_mutex. */
351b3075717SChris Mason struct list_head alloc_list;
3522b82032cSYan Zheng
353944d3f9fSNikolay Borisov struct list_head seed_list;
3542b82032cSYan Zheng
355d85512d5SAnand Jain /* Count fs-devices opened. */
3562b82032cSYan Zheng int opened;
357c289811cSChris Mason
358d85512d5SAnand Jain /* Set when we find or add a device that doesn't have the nonrot flag set. */
3597f0432d0SJohannes Thumshirn bool rotating;
360d85512d5SAnand Jain /* Devices support TRIM/discard commands. */
36163a7cb13SDavid Sterba bool discardable;
3624693893bSAnand Jain bool fsid_change;
363d85512d5SAnand Jain /* The filesystem is a seed filesystem. */
3644693893bSAnand Jain bool seeding;
3652e7910d6SAnand Jain
3665a13f430SAnand Jain struct btrfs_fs_info *fs_info;
3672e7910d6SAnand Jain /* sysfs kobjects */
368c1b7e474SAnand Jain struct kobject fsid_kobj;
369b5501504SAnand Jain struct kobject *devices_kobj;
370a013d141SAnand Jain struct kobject *devinfo_kobj;
3712e7910d6SAnand Jain struct completion kobj_unregister;
372c4a816c6SNaohiro Aota
373c4a816c6SNaohiro Aota enum btrfs_chunk_allocation_policy chunk_alloc_policy;
37433fd2f71SAnand Jain
375d85512d5SAnand Jain /* Policy used to read the mirrored stripes. */
37633fd2f71SAnand Jain enum btrfs_read_policy read_policy;
3778a4b83ccSChris Mason };
3788a4b83ccSChris Mason
379ab4ba2e1SQu Wenruo #define BTRFS_MAX_DEVS(info) ((BTRFS_MAX_ITEM_SIZE(info) \
380ab4ba2e1SQu Wenruo - sizeof(struct btrfs_chunk)) \
381ab4ba2e1SQu Wenruo / sizeof(struct btrfs_stripe) + 1)
382ab4ba2e1SQu Wenruo
383ab4ba2e1SQu Wenruo #define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \
384ab4ba2e1SQu Wenruo - 2 * sizeof(struct btrfs_disk_key) \
385ab4ba2e1SQu Wenruo - 2 * sizeof(struct btrfs_chunk)) \
386ab4ba2e1SQu Wenruo / sizeof(struct btrfs_stripe) + 1)
387ab4ba2e1SQu Wenruo
3884c664611SQu Wenruo struct btrfs_io_stripe {
389cea9e445SChris Mason struct btrfs_device *dev;
3909ff7ddd3SChristoph Hellwig union {
3919ff7ddd3SChristoph Hellwig /* Block mapping */
392cea9e445SChris Mason u64 physical;
3939ff7ddd3SChristoph Hellwig /* For the endio handler */
3949ff7ddd3SChristoph Hellwig struct btrfs_io_context *bioc;
3959ff7ddd3SChristoph Hellwig };
396a4012f06SChristoph Hellwig };
397a4012f06SChristoph Hellwig
398a4012f06SChristoph Hellwig struct btrfs_discard_stripe {
399a4012f06SChristoph Hellwig struct btrfs_device *dev;
400a4012f06SChristoph Hellwig u64 physical;
401a4012f06SChristoph Hellwig u64 length;
402cea9e445SChris Mason };
403cea9e445SChris Mason
4044c664611SQu Wenruo /*
4054c664611SQu Wenruo * Context for IO subsmission for device stripe.
4064c664611SQu Wenruo *
4074c664611SQu Wenruo * - Track the unfinished mirrors for mirror based profiles
4084c664611SQu Wenruo * Mirror based profiles are SINGLE/DUP/RAID1/RAID10.
4094c664611SQu Wenruo *
4104c664611SQu Wenruo * - Contain the logical -> physical mapping info
4114c664611SQu Wenruo * Used by submit_stripe_bio() for mapping logical bio
4124c664611SQu Wenruo * into physical device address.
4134c664611SQu Wenruo *
4144c664611SQu Wenruo * - Contain device replace info
4154c664611SQu Wenruo * Used by handle_ops_on_dev_replace() to copy logical bios
4164c664611SQu Wenruo * into the new device.
4174c664611SQu Wenruo *
4184c664611SQu Wenruo * - Contain RAID56 full stripe logical bytenrs
4194c664611SQu Wenruo */
4204c664611SQu Wenruo struct btrfs_io_context {
421140475aeSElena Reshetova refcount_t refs;
422c404e0dcSMiao Xie struct btrfs_fs_info *fs_info;
42310f11900SZhao Lei u64 map_type; /* get from map_lookup->type */
4247d2b4daaSChris Mason struct bio *orig_bio;
425a236aed1SChris Mason atomic_t error;
4264ced85f8SQu Wenruo u16 max_errors;
4274ced85f8SQu Wenruo
4284ced85f8SQu Wenruo /*
4294ced85f8SQu Wenruo * The total number of stripes, including the extra duplicated
4304ced85f8SQu Wenruo * stripe for replace.
4314ced85f8SQu Wenruo */
4324ced85f8SQu Wenruo u16 num_stripes;
4334ced85f8SQu Wenruo
4344ced85f8SQu Wenruo /*
4354ced85f8SQu Wenruo * The mirror_num of this bioc.
4364ced85f8SQu Wenruo *
4374ced85f8SQu Wenruo * This is for reads which use 0 as mirror_num, thus we should return a
4384ced85f8SQu Wenruo * valid mirror_num (>0) for the reader.
4394ced85f8SQu Wenruo */
4404ced85f8SQu Wenruo u16 mirror_num;
4414ced85f8SQu Wenruo
4424ced85f8SQu Wenruo /*
4434ced85f8SQu Wenruo * The following two members are for dev-replace case only.
4444ced85f8SQu Wenruo *
4451faf3885SQu Wenruo * @replace_nr_stripes: Number of duplicated stripes which need to be
4464ced85f8SQu Wenruo * written to replace target.
4474ced85f8SQu Wenruo * Should be <= 2 (2 for DUP, otherwise <= 1).
4481faf3885SQu Wenruo * @replace_stripe_src: The array indicates where the duplicated stripes
4491faf3885SQu Wenruo * are from.
4504ced85f8SQu Wenruo *
4511faf3885SQu Wenruo * The @replace_stripe_src[] array is mostly for RAID56 cases.
4524ced85f8SQu Wenruo * As non-RAID56 stripes share the same contents of the mapped range,
4534ced85f8SQu Wenruo * thus no need to bother where the duplicated ones are from.
4544ced85f8SQu Wenruo *
4554ced85f8SQu Wenruo * But for RAID56 case, all stripes contain different contents, thus
4564ced85f8SQu Wenruo * we need a way to know the mapping.
4574ced85f8SQu Wenruo *
4584ced85f8SQu Wenruo * There is an example for the two members, using a RAID5 write:
4594ced85f8SQu Wenruo *
4604ced85f8SQu Wenruo * num_stripes: 4 (3 + 1 duplicated write)
4614ced85f8SQu Wenruo * stripes[0]: dev = devid 1, physical = X
4624ced85f8SQu Wenruo * stripes[1]: dev = devid 2, physical = Y
4634ced85f8SQu Wenruo * stripes[2]: dev = devid 3, physical = Z
4644ced85f8SQu Wenruo * stripes[3]: dev = devid 0, physical = Y
4654ced85f8SQu Wenruo *
4661faf3885SQu Wenruo * replace_nr_stripes = 1
4671faf3885SQu Wenruo * replace_stripe_src = 1 <- Means stripes[1] is involved in replace.
4681faf3885SQu Wenruo * The duplicated stripe index would be
4691faf3885SQu Wenruo * (@num_stripes - 1).
4701faf3885SQu Wenruo *
4711faf3885SQu Wenruo * Note, that we can still have cases replace_nr_stripes = 2 for DUP.
4721faf3885SQu Wenruo * In that case, all stripes share the same content, thus we don't
4731faf3885SQu Wenruo * need to bother @replace_stripe_src value at all.
4744ced85f8SQu Wenruo */
4751faf3885SQu Wenruo u16 replace_nr_stripes;
4761faf3885SQu Wenruo s16 replace_stripe_src;
4778e5cfb55SZhao Lei /*
47818d758a2SQu Wenruo * Logical bytenr of the full stripe start, only for RAID56 cases.
47918d758a2SQu Wenruo *
48018d758a2SQu Wenruo * When this value is set to other than (u64)-1, the stripes[] should
48118d758a2SQu Wenruo * follow this pattern:
48218d758a2SQu Wenruo *
48318d758a2SQu Wenruo * (real_stripes = num_stripes - replace_nr_stripes)
48418d758a2SQu Wenruo * (data_stripes = (is_raid6) ? (real_stripes - 2) : (real_stripes - 1))
48518d758a2SQu Wenruo *
48618d758a2SQu Wenruo * stripes[0]: The first data stripe
48718d758a2SQu Wenruo * stripes[1]: The second data stripe
48818d758a2SQu Wenruo * ...
48918d758a2SQu Wenruo * stripes[data_stripes - 1]: The last data stripe
49018d758a2SQu Wenruo * stripes[data_stripes]: The P stripe
49118d758a2SQu Wenruo * stripes[data_stripes + 1]: The Q stripe (only for RAID6).
4928e5cfb55SZhao Lei */
49318d758a2SQu Wenruo u64 full_stripe_logical;
4944c664611SQu Wenruo struct btrfs_io_stripe stripes[];
495cea9e445SChris Mason };
496cea9e445SChris Mason
497b2117a39SMiao Xie struct btrfs_device_info {
498b2117a39SMiao Xie struct btrfs_device *dev;
499b2117a39SMiao Xie u64 dev_offset;
500b2117a39SMiao Xie u64 max_avail;
50173c5de00SArne Jansen u64 total_avail;
502b2117a39SMiao Xie };
503b2117a39SMiao Xie
50431e50229SLiu Bo struct btrfs_raid_attr {
5058c3e3582SDavid Sterba u8 sub_stripes; /* sub_stripes info for map */
5068c3e3582SDavid Sterba u8 dev_stripes; /* stripes per dev */
5078c3e3582SDavid Sterba u8 devs_max; /* max devs to use */
5088c3e3582SDavid Sterba u8 devs_min; /* min devs needed */
5098c3e3582SDavid Sterba u8 tolerated_failures; /* max tolerated fail devs */
5108c3e3582SDavid Sterba u8 devs_increment; /* ndevs has to be a multiple of this */
5118c3e3582SDavid Sterba u8 ncopies; /* how many copies to data has */
5128c3e3582SDavid Sterba u8 nparity; /* number of stripes worth of bytes to store
513b50836edSHans van Kranenburg * parity information */
5148c3e3582SDavid Sterba u8 mindev_error; /* error code if min devs requisite is unmet */
515ed23467bSAnand Jain const char raid_name[8]; /* name of the raid */
51641a6e891SAnand Jain u64 bg_flag; /* block group flag of the raid */
51731e50229SLiu Bo };
51831e50229SLiu Bo
519af902047SZhao Lei extern const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES];
520af902047SZhao Lei
5211abe9b8aSliubo struct map_lookup {
5221abe9b8aSliubo u64 type;
5231abe9b8aSliubo int io_align;
5241abe9b8aSliubo int io_width;
5251abe9b8aSliubo int num_stripes;
5261abe9b8aSliubo int sub_stripes;
527cf90d884SQu Wenruo int verified_stripes; /* For mount time dev extent verification */
5284c664611SQu Wenruo struct btrfs_io_stripe stripes[];
5291abe9b8aSliubo };
5301abe9b8aSliubo
531a2de733cSArne Jansen #define map_lookup_size(n) (sizeof(struct map_lookup) + \
5324c664611SQu Wenruo (sizeof(struct btrfs_io_stripe) * (n)))
533a2de733cSArne Jansen
534c9e9f97bSIlya Dryomov struct btrfs_balance_args;
53519a39dceSIlya Dryomov struct btrfs_balance_progress;
536c9e9f97bSIlya Dryomov struct btrfs_balance_control {
537c9e9f97bSIlya Dryomov struct btrfs_balance_args data;
538c9e9f97bSIlya Dryomov struct btrfs_balance_args meta;
539c9e9f97bSIlya Dryomov struct btrfs_balance_args sys;
540c9e9f97bSIlya Dryomov
541c9e9f97bSIlya Dryomov u64 flags;
54219a39dceSIlya Dryomov
54319a39dceSIlya Dryomov struct btrfs_balance_progress stat;
544c9e9f97bSIlya Dryomov };
545c9e9f97bSIlya Dryomov
546562d7b15SJosef Bacik /*
547562d7b15SJosef Bacik * Search for a given device by the set parameters
548562d7b15SJosef Bacik */
549562d7b15SJosef Bacik struct btrfs_dev_lookup_args {
550562d7b15SJosef Bacik u64 devid;
551562d7b15SJosef Bacik u8 *uuid;
552562d7b15SJosef Bacik u8 *fsid;
553562d7b15SJosef Bacik bool missing;
554562d7b15SJosef Bacik };
555562d7b15SJosef Bacik
556562d7b15SJosef Bacik /* We have to initialize to -1 because BTRFS_DEV_REPLACE_DEVID is 0 */
557562d7b15SJosef Bacik #define BTRFS_DEV_LOOKUP_ARGS_INIT { .devid = (u64)-1 }
558562d7b15SJosef Bacik
559562d7b15SJosef Bacik #define BTRFS_DEV_LOOKUP_ARGS(name) \
560562d7b15SJosef Bacik struct btrfs_dev_lookup_args name = BTRFS_DEV_LOOKUP_ARGS_INIT
561562d7b15SJosef Bacik
562cf8cddd3SChristoph Hellwig enum btrfs_map_op {
563cf8cddd3SChristoph Hellwig BTRFS_MAP_READ,
564cf8cddd3SChristoph Hellwig BTRFS_MAP_WRITE,
565cf8cddd3SChristoph Hellwig BTRFS_MAP_GET_READ_MIRRORS,
566cf8cddd3SChristoph Hellwig };
567cf8cddd3SChristoph Hellwig
btrfs_op(struct bio * bio)568cf8cddd3SChristoph Hellwig static inline enum btrfs_map_op btrfs_op(struct bio *bio)
569cf8cddd3SChristoph Hellwig {
570cf8cddd3SChristoph Hellwig switch (bio_op(bio)) {
571cf8cddd3SChristoph Hellwig case REQ_OP_WRITE:
572cfe94440SNaohiro Aota case REQ_OP_ZONE_APPEND:
573cf8cddd3SChristoph Hellwig return BTRFS_MAP_WRITE;
574cf8cddd3SChristoph Hellwig default:
575cf8cddd3SChristoph Hellwig WARN_ON_ONCE(1);
576c730ae0cSMarcos Paulo de Souza fallthrough;
577cf8cddd3SChristoph Hellwig case REQ_OP_READ:
578cf8cddd3SChristoph Hellwig return BTRFS_MAP_READ;
579cf8cddd3SChristoph Hellwig }
580cf8cddd3SChristoph Hellwig }
581cf8cddd3SChristoph Hellwig
btrfs_chunk_item_size(int num_stripes)582b31bed17SJosef Bacik static inline unsigned long btrfs_chunk_item_size(int num_stripes)
583b31bed17SJosef Bacik {
584b31bed17SJosef Bacik ASSERT(num_stripes);
585b31bed17SJosef Bacik return sizeof(struct btrfs_chunk) +
586b31bed17SJosef Bacik sizeof(struct btrfs_stripe) * (num_stripes - 1);
587b31bed17SJosef Bacik }
588b31bed17SJosef Bacik
589cb091225SQu Wenruo /*
590cb091225SQu Wenruo * Do the type safe converstion from stripe_nr to offset inside the chunk.
591cb091225SQu Wenruo *
592cb091225SQu Wenruo * @stripe_nr is u32, with left shift it can overflow u32 for chunks larger
593cb091225SQu Wenruo * than 4G. This does the proper type cast to avoid overflow.
594cb091225SQu Wenruo */
btrfs_stripe_nr_to_offset(u32 stripe_nr)595cb091225SQu Wenruo static inline u64 btrfs_stripe_nr_to_offset(u32 stripe_nr)
596cb091225SQu Wenruo {
597cb091225SQu Wenruo return (u64)stripe_nr << BTRFS_STRIPE_LEN_SHIFT;
598cb091225SQu Wenruo }
599cb091225SQu Wenruo
6004c664611SQu Wenruo void btrfs_get_bioc(struct btrfs_io_context *bioc);
6014c664611SQu Wenruo void btrfs_put_bioc(struct btrfs_io_context *bioc);
602cf8cddd3SChristoph Hellwig int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
603cea9e445SChris Mason u64 logical, u64 *length,
604103c1972SChristoph Hellwig struct btrfs_io_context **bioc_ret,
605103c1972SChristoph Hellwig struct btrfs_io_stripe *smap, int *mirror_num_ret,
606103c1972SChristoph Hellwig int need_raid_map);
6074886ff7bSQu Wenruo int btrfs_map_repair_block(struct btrfs_fs_info *fs_info,
6084886ff7bSQu Wenruo struct btrfs_io_stripe *smap, u64 logical,
6094886ff7bSQu Wenruo u32 length, int mirror_num);
610a4012f06SChristoph Hellwig struct btrfs_discard_stripe *btrfs_map_discard(struct btrfs_fs_info *fs_info,
611a4012f06SChristoph Hellwig u64 logical, u64 *length_ret,
612a4012f06SChristoph Hellwig u32 *num_stripes);
6136bccf3abSJeff Mahoney int btrfs_read_sys_array(struct btrfs_fs_info *fs_info);
6145b4aacefSJeff Mahoney int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info);
615f6f39f7aSNikolay Borisov struct btrfs_block_group *btrfs_create_chunk(struct btrfs_trans_handle *trans,
61679bd3712SFilipe Manana u64 type);
617c8bf1b67SDavid Sterba void btrfs_mapping_tree_free(struct extent_map_tree *tree);
6188a4b83ccSChris Mason int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
61905bdb996SChristoph Hellwig blk_mode_t flags, void *holder);
62005bdb996SChristoph Hellwig struct btrfs_device *btrfs_scan_one_device(const char *path, blk_mode_t flags);
62116cab91aSAnand Jain int btrfs_forget_devices(dev_t devt);
62254eed6aeSNikolay Borisov void btrfs_close_devices(struct btrfs_fs_devices *fs_devices);
623bacce86aSAnand Jain void btrfs_free_extra_devids(struct btrfs_fs_devices *fs_devices);
624d6507cf1SNikolay Borisov void btrfs_assign_next_active_device(struct btrfs_device *device,
625d6507cf1SNikolay Borisov struct btrfs_device *this_dev);
626a27a94c2SNikolay Borisov struct btrfs_device *btrfs_find_device_by_devspec(struct btrfs_fs_info *fs_info,
627a27a94c2SNikolay Borisov u64 devid,
628a27a94c2SNikolay Borisov const char *devpath);
629faa775c4SJosef Bacik int btrfs_get_dev_args_from_path(struct btrfs_fs_info *fs_info,
630faa775c4SJosef Bacik struct btrfs_dev_lookup_args *args,
631faa775c4SJosef Bacik const char *path);
63212bd2fc0SIlya Dryomov struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
633bb21e302SAnand Jain const u64 *devid, const u8 *uuid,
634bb21e302SAnand Jain const char *path);
635faa775c4SJosef Bacik void btrfs_put_dev_args_from_path(struct btrfs_dev_lookup_args *args);
6362ff7e61eSJeff Mahoney int btrfs_rm_device(struct btrfs_fs_info *fs_info,
6371a15eb72SJosef Bacik struct btrfs_dev_lookup_args *args,
6382736e8eeSChristoph Hellwig struct block_device **bdev, void **holder);
639ffc5a379SDavid Sterba void __exit btrfs_cleanup_fs_uuids(void);
6405d964051SStefan Behrens int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len);
6418f18cf13SChris Mason int btrfs_grow_device(struct btrfs_trans_handle *trans,
6428f18cf13SChris Mason struct btrfs_device *device, u64 new_size);
643562d7b15SJosef Bacik struct btrfs_device *btrfs_find_device(const struct btrfs_fs_devices *fs_devices,
644562d7b15SJosef Bacik const struct btrfs_dev_lookup_args *args);
6458f18cf13SChris Mason int btrfs_shrink_device(struct btrfs_device *device, u64 new_size);
646da353f6bSDavid Sterba int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *path);
6476fcf6e2bSDavid Sterba int btrfs_balance(struct btrfs_fs_info *fs_info,
6486fcf6e2bSDavid Sterba struct btrfs_balance_control *bctl,
649c9e9f97bSIlya Dryomov struct btrfs_ioctl_balance_args *bargs);
650f89e09cfSAnand Jain void btrfs_describe_block_groups(u64 flags, char *buf, u32 size_buf);
6512b6ba629SIlya Dryomov int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info);
65268310a5eSIlya Dryomov int btrfs_recover_balance(struct btrfs_fs_info *fs_info);
653837d5b6eSIlya Dryomov int btrfs_pause_balance(struct btrfs_fs_info *fs_info);
65418bb8bbfSJohannes Thumshirn int btrfs_relocate_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset);
655a7e99c69SIlya Dryomov int btrfs_cancel_balance(struct btrfs_fs_info *fs_info);
656f7a81ea4SStefan Behrens int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info);
65797f4dd09SNikolay Borisov int btrfs_uuid_scan_kthread(void *data);
658a09f23c3SAnand Jain bool btrfs_chunk_writeable(struct btrfs_fs_info *fs_info, u64 chunk_offset);
659442a4f63SStefan Behrens void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index);
6602ff7e61eSJeff Mahoney int btrfs_get_dev_stats(struct btrfs_fs_info *fs_info,
661b27f7c0cSDavid Sterba struct btrfs_ioctl_get_dev_stats *stats);
662a8d1b164SJohannes Thumshirn int btrfs_init_devices_late(struct btrfs_fs_info *fs_info);
663733f4fbbSStefan Behrens int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info);
664196c9d8dSDavid Sterba int btrfs_run_dev_stats(struct btrfs_trans_handle *trans);
66568a9db5fSNikolay Borisov void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_device *srcdev);
66665237ee3SDavid Sterba void btrfs_rm_dev_replace_free_srcdev(struct btrfs_device *srcdev);
6674f5ad7bdSNikolay Borisov void btrfs_destroy_dev_replace_tgtdev(struct btrfs_device *tgtdev);
668592d92eeSLiu Bo int btrfs_is_parity_mirror(struct btrfs_fs_info *fs_info,
669e4ff5fb5SNikolay Borisov u64 logical, u64 len);
6702ff7e61eSJeff Mahoney unsigned long btrfs_full_stripe_len(struct btrfs_fs_info *fs_info,
67153b381b3SDavid Woodhouse u64 logical);
672bc88b486SQu Wenruo u64 btrfs_calc_stripe_length(const struct extent_map *em);
6730b30f719SQu Wenruo int btrfs_nr_parity_stripes(u64 type);
67479bd3712SFilipe Manana int btrfs_chunk_alloc_add_chunk_item(struct btrfs_trans_handle *trans,
67579bd3712SFilipe Manana struct btrfs_block_group *bg);
67697aff912SNikolay Borisov int btrfs_remove_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset);
67760ca842eSOmar Sandoval struct extent_map *btrfs_get_chunk_map(struct btrfs_fs_info *fs_info,
67860ca842eSOmar Sandoval u64 logical, u64 length);
6798f32380dSJohannes Thumshirn void btrfs_release_disk_super(struct btrfs_super_block *super);
680addc3fa7SMiao Xie
btrfs_dev_stat_inc(struct btrfs_device * dev,int index)681442a4f63SStefan Behrens static inline void btrfs_dev_stat_inc(struct btrfs_device *dev,
682442a4f63SStefan Behrens int index)
683442a4f63SStefan Behrens {
684442a4f63SStefan Behrens atomic_inc(dev->dev_stat_values + index);
6859deae968SNikolay Borisov /*
6869deae968SNikolay Borisov * This memory barrier orders stores updating statistics before stores
6879deae968SNikolay Borisov * updating dev_stats_ccnt.
6889deae968SNikolay Borisov *
6899deae968SNikolay Borisov * It pairs with smp_rmb() in btrfs_run_dev_stats().
6909deae968SNikolay Borisov */
691addc3fa7SMiao Xie smp_mb__before_atomic();
692addc3fa7SMiao Xie atomic_inc(&dev->dev_stats_ccnt);
693442a4f63SStefan Behrens }
694442a4f63SStefan Behrens
btrfs_dev_stat_read(struct btrfs_device * dev,int index)695442a4f63SStefan Behrens static inline int btrfs_dev_stat_read(struct btrfs_device *dev,
696442a4f63SStefan Behrens int index)
697442a4f63SStefan Behrens {
698442a4f63SStefan Behrens return atomic_read(dev->dev_stat_values + index);
699442a4f63SStefan Behrens }
700442a4f63SStefan Behrens
btrfs_dev_stat_read_and_reset(struct btrfs_device * dev,int index)701442a4f63SStefan Behrens static inline int btrfs_dev_stat_read_and_reset(struct btrfs_device *dev,
702442a4f63SStefan Behrens int index)
703442a4f63SStefan Behrens {
704442a4f63SStefan Behrens int ret;
705442a4f63SStefan Behrens
706442a4f63SStefan Behrens ret = atomic_xchg(dev->dev_stat_values + index, 0);
7074660c49fSNikolay Borisov /*
7084660c49fSNikolay Borisov * atomic_xchg implies a full memory barriers as per atomic_t.txt:
7094660c49fSNikolay Borisov * - RMW operations that have a return value are fully ordered;
7104660c49fSNikolay Borisov *
7114660c49fSNikolay Borisov * This implicit memory barriers is paired with the smp_rmb in
7124660c49fSNikolay Borisov * btrfs_run_dev_stats
7134660c49fSNikolay Borisov */
714addc3fa7SMiao Xie atomic_inc(&dev->dev_stats_ccnt);
715442a4f63SStefan Behrens return ret;
716442a4f63SStefan Behrens }
717442a4f63SStefan Behrens
btrfs_dev_stat_set(struct btrfs_device * dev,int index,unsigned long val)718442a4f63SStefan Behrens static inline void btrfs_dev_stat_set(struct btrfs_device *dev,
719442a4f63SStefan Behrens int index, unsigned long val)
720442a4f63SStefan Behrens {
721442a4f63SStefan Behrens atomic_set(dev->dev_stat_values + index, val);
7229deae968SNikolay Borisov /*
7239deae968SNikolay Borisov * This memory barrier orders stores updating statistics before stores
7249deae968SNikolay Borisov * updating dev_stats_ccnt.
7259deae968SNikolay Borisov *
7269deae968SNikolay Borisov * It pairs with smp_rmb() in btrfs_run_dev_stats().
7279deae968SNikolay Borisov */
728addc3fa7SMiao Xie smp_mb__before_atomic();
729addc3fa7SMiao Xie atomic_inc(&dev->dev_stats_ccnt);
730442a4f63SStefan Behrens }
731442a4f63SStefan Behrens
btrfs_dev_name(const struct btrfs_device * device)732cb3e217bSQu Wenruo static inline const char *btrfs_dev_name(const struct btrfs_device *device)
733cb3e217bSQu Wenruo {
734cb3e217bSQu Wenruo if (!device || test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
735cb3e217bSQu Wenruo return "<missing disk>";
736cb3e217bSQu Wenruo else
737cb3e217bSQu Wenruo return rcu_str_deref(device->name);
738cb3e217bSQu Wenruo }
739cb3e217bSQu Wenruo
740bbbf7243SNikolay Borisov void btrfs_commit_device_sizes(struct btrfs_transaction *trans);
74104216820SFilipe Manana
7424143cb8bSDavid Sterba struct list_head * __attribute_const__ btrfs_get_fs_uuids(void);
7436528b99dSAnand Jain bool btrfs_check_rw_degradable(struct btrfs_fs_info *fs_info,
7446528b99dSAnand Jain struct btrfs_device *failing_dev);
745313b0858SJosef Bacik void btrfs_scratch_superblocks(struct btrfs_fs_info *fs_info,
746313b0858SJosef Bacik struct block_device *bdev,
747313b0858SJosef Bacik const char *device_path);
74821634a19SQu Wenruo
749500a44c9SDavid Sterba enum btrfs_raid_types __attribute_const__ btrfs_bg_flags_to_raid_index(u64 flags);
75046df06b8SDavid Sterba int btrfs_bg_type_to_factor(u64 flags);
751158da513SDavid Sterba const char *btrfs_bg_type_to_raid_name(u64 flags);
752cf90d884SQu Wenruo int btrfs_verify_dev_extents(struct btrfs_fs_info *fs_info);
753554aed7dSJohannes Thumshirn bool btrfs_repair_one_zone(struct btrfs_fs_info *fs_info, u64 logical);
75446df06b8SDavid Sterba
755c2e79e86SJosef Bacik bool btrfs_pinned_by_swapfile(struct btrfs_fs_info *fs_info, void *ptr);
7564844c366SAnand Jain u8 *btrfs_sb_fsid_ptr(struct btrfs_super_block *sb);
757c2e79e86SJosef Bacik
7580b86a832SChris Mason #endif
759