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 1867a2c45eSMiao Xie extern struct mutex uuid_mutex; 1967a2c45eSMiao Xie 20ee22184bSByongho Lee #define BTRFS_STRIPE_LEN SZ_64K 21a97699d1SQu Wenruo #define BTRFS_STRIPE_LEN_SHIFT (16) 22a97699d1SQu Wenruo #define BTRFS_STRIPE_LEN_MASK (BTRFS_STRIPE_LEN - 1) 23a97699d1SQu Wenruo 24a97699d1SQu Wenruo static_assert(const_ilog2(BTRFS_STRIPE_LEN) == BTRFS_STRIPE_LEN_SHIFT); 25b2117a39SMiao Xie 26719fae89SQu Wenruo /* Used by sanity check for btrfs_raid_types. */ 27719fae89SQu Wenruo #define const_ffs(n) (__builtin_ctzll(n) + 1) 28719fae89SQu Wenruo 29719fae89SQu Wenruo /* 30719fae89SQu Wenruo * The conversion from BTRFS_BLOCK_GROUP_* bits to btrfs_raid_type requires 31719fae89SQu Wenruo * RAID0 always to be the lowest profile bit. 32719fae89SQu Wenruo * Although it's part of on-disk format and should never change, do extra 33719fae89SQu Wenruo * compile-time sanity checks. 34719fae89SQu Wenruo */ 35719fae89SQu Wenruo static_assert(const_ffs(BTRFS_BLOCK_GROUP_RAID0) < 36719fae89SQu Wenruo const_ffs(BTRFS_BLOCK_GROUP_PROFILE_MASK & ~BTRFS_BLOCK_GROUP_RAID0)); 37719fae89SQu Wenruo static_assert(const_ilog2(BTRFS_BLOCK_GROUP_RAID0) > 38719fae89SQu Wenruo ilog2(BTRFS_BLOCK_GROUP_TYPE_MASK)); 39719fae89SQu Wenruo 40719fae89SQu Wenruo /* ilog2() can handle both constants and variables */ 41719fae89SQu Wenruo #define BTRFS_BG_FLAG_TO_INDEX(profile) \ 42719fae89SQu Wenruo ilog2((profile) >> (ilog2(BTRFS_BLOCK_GROUP_RAID0) - 1)) 43719fae89SQu Wenruo 44f04fbcc6SQu Wenruo enum btrfs_raid_types { 45719fae89SQu Wenruo /* SINGLE is the special one as it doesn't have on-disk bit. */ 46719fae89SQu Wenruo BTRFS_RAID_SINGLE = 0, 47719fae89SQu Wenruo 48719fae89SQu Wenruo BTRFS_RAID_RAID0 = BTRFS_BG_FLAG_TO_INDEX(BTRFS_BLOCK_GROUP_RAID0), 49719fae89SQu Wenruo BTRFS_RAID_RAID1 = BTRFS_BG_FLAG_TO_INDEX(BTRFS_BLOCK_GROUP_RAID1), 50719fae89SQu Wenruo BTRFS_RAID_DUP = BTRFS_BG_FLAG_TO_INDEX(BTRFS_BLOCK_GROUP_DUP), 51719fae89SQu Wenruo BTRFS_RAID_RAID10 = BTRFS_BG_FLAG_TO_INDEX(BTRFS_BLOCK_GROUP_RAID10), 52719fae89SQu Wenruo BTRFS_RAID_RAID5 = BTRFS_BG_FLAG_TO_INDEX(BTRFS_BLOCK_GROUP_RAID5), 53719fae89SQu Wenruo BTRFS_RAID_RAID6 = BTRFS_BG_FLAG_TO_INDEX(BTRFS_BLOCK_GROUP_RAID6), 54719fae89SQu Wenruo BTRFS_RAID_RAID1C3 = BTRFS_BG_FLAG_TO_INDEX(BTRFS_BLOCK_GROUP_RAID1C3), 55719fae89SQu Wenruo BTRFS_RAID_RAID1C4 = BTRFS_BG_FLAG_TO_INDEX(BTRFS_BLOCK_GROUP_RAID1C4), 56719fae89SQu Wenruo 57f04fbcc6SQu Wenruo BTRFS_NR_RAID_TYPES 58f04fbcc6SQu Wenruo }; 59f04fbcc6SQu Wenruo 607cc8e58dSMiao Xie /* 617cc8e58dSMiao Xie * Use sequence counter to get consistent device stat data on 627cc8e58dSMiao Xie * 32-bit processors. 637cc8e58dSMiao Xie */ 647cc8e58dSMiao Xie #if BITS_PER_LONG==32 && defined(CONFIG_SMP) 657cc8e58dSMiao Xie #include <linux/seqlock.h> 667cc8e58dSMiao Xie #define __BTRFS_NEED_DEVICE_DATA_ORDERED 67c41ec452SSu Yue #define btrfs_device_data_ordered_init(device) \ 68c41ec452SSu Yue seqcount_init(&device->data_seqcount) 697cc8e58dSMiao Xie #else 70c41ec452SSu Yue #define btrfs_device_data_ordered_init(device) do { } while (0) 717cc8e58dSMiao Xie #endif 727cc8e58dSMiao Xie 73ebbede42SAnand Jain #define BTRFS_DEV_STATE_WRITEABLE (0) 74e12c9621SAnand Jain #define BTRFS_DEV_STATE_IN_FS_METADATA (1) 75e6e674bdSAnand Jain #define BTRFS_DEV_STATE_MISSING (2) 76401e29c1SAnand Jain #define BTRFS_DEV_STATE_REPLACE_TGT (3) 771c3063b6SAnand Jain #define BTRFS_DEV_STATE_FLUSH_SENT (4) 7866d204a1SFilipe Manana #define BTRFS_DEV_STATE_NO_READA (5) 79ebbede42SAnand Jain 805b316468SNaohiro Aota struct btrfs_zoned_device_info; 815b316468SNaohiro Aota 820b86a832SChris Mason struct btrfs_device { 830b6f5d40SNikolay Borisov struct list_head dev_list; /* device_list_mutex */ 840b6f5d40SNikolay Borisov struct list_head dev_alloc_list; /* chunk mutex */ 85bbbf7243SNikolay Borisov struct list_head post_commit_list; /* chunk mutex */ 862b82032cSYan Zheng struct btrfs_fs_devices *fs_devices; 87fb456252SJeff Mahoney struct btrfs_fs_info *fs_info; 88ffbd517dSChris Mason 898d1a7aaeSMadhuparna Bhowmik struct rcu_string __rcu *name; 90d5ee37bcSMiao Xie 91d5ee37bcSMiao Xie u64 generation; 92d5ee37bcSMiao Xie 93d5ee37bcSMiao Xie struct block_device *bdev; 94d5ee37bcSMiao Xie 955b316468SNaohiro Aota struct btrfs_zoned_device_info *zone_info; 965b316468SNaohiro Aota 97d5ee37bcSMiao Xie /* the mode sent to blkdev_get */ 98d5ee37bcSMiao Xie fmode_t mode; 99d5ee37bcSMiao Xie 1004889bc05SAnand Jain /* 1014889bc05SAnand Jain * Device's major-minor number. Must be set even if the device is not 1024889bc05SAnand Jain * opened (bdev == NULL), unless the device is missing. 1034889bc05SAnand Jain */ 1044889bc05SAnand Jain dev_t devt; 105ebbede42SAnand Jain unsigned long dev_state; 10658efbc9fSOmar Sandoval blk_status_t last_flush_error; 107b3075717SChris Mason 1087cc8e58dSMiao Xie #ifdef __BTRFS_NEED_DEVICE_DATA_ORDERED 109c41ec452SSu Yue seqcount_t data_seqcount; 1107cc8e58dSMiao Xie #endif 1117cc8e58dSMiao Xie 1120b86a832SChris Mason /* the internal btrfs device id */ 1130b86a832SChris Mason u64 devid; 1140b86a832SChris Mason 1156ba40b61SMiao Xie /* size of the device in memory */ 1160b86a832SChris Mason u64 total_bytes; 1170b86a832SChris Mason 1186ba40b61SMiao Xie /* size of the device on disk */ 119d6397baeSChris Ball u64 disk_total_bytes; 120d6397baeSChris Ball 1210b86a832SChris Mason /* bytes used */ 1220b86a832SChris Mason u64 bytes_used; 1230b86a832SChris Mason 1240b86a832SChris Mason /* optimal io alignment for this device */ 1250b86a832SChris Mason u32 io_align; 1260b86a832SChris Mason 1270b86a832SChris Mason /* optimal io width for this device */ 1280b86a832SChris Mason u32 io_width; 1293c45bfc1SDulshani Gunawardhana /* type and info about this device */ 1303c45bfc1SDulshani Gunawardhana u64 type; 1310b86a832SChris Mason 1320b86a832SChris Mason /* minimal io size for this device */ 1330b86a832SChris Mason u32 sector_size; 1340b86a832SChris Mason 1350b86a832SChris Mason /* physical drive uuid (or lvm uuid) */ 136e17cade2SChris Mason u8 uuid[BTRFS_UUID_SIZE]; 1378b712842SChris Mason 138935e5cc9SMiao Xie /* 139935e5cc9SMiao Xie * size of the device on the current transaction 140935e5cc9SMiao Xie * 141935e5cc9SMiao Xie * This variant is update when committing the transaction, 142bbbf7243SNikolay Borisov * and protected by chunk mutex 143935e5cc9SMiao Xie */ 144935e5cc9SMiao Xie u64 commit_total_bytes; 145935e5cc9SMiao Xie 146ce7213c7SMiao Xie /* bytes used on the current transaction */ 147ce7213c7SMiao Xie u64 commit_bytes_used; 148935e5cc9SMiao Xie 149f9e69aa9SChristoph Hellwig /* Bio used for flushing device barriers */ 150f9e69aa9SChristoph Hellwig struct bio flush_bio; 1513c45bfc1SDulshani Gunawardhana struct completion flush_wait; 1523c45bfc1SDulshani Gunawardhana 153a2de733cSArne Jansen /* per-device scrub information */ 154cadbc0a0SAnand Jain struct scrub_ctx *scrub_ctx; 155a2de733cSArne Jansen 156442a4f63SStefan Behrens /* disk I/O failure stats. For detailed description refer to 157442a4f63SStefan Behrens * enum btrfs_dev_stat_values in ioctl.h */ 158733f4fbbSStefan Behrens int dev_stats_valid; 159addc3fa7SMiao Xie 160addc3fa7SMiao Xie /* Counter to record the change of device stats */ 161addc3fa7SMiao Xie atomic_t dev_stats_ccnt; 162442a4f63SStefan Behrens atomic_t dev_stat_values[BTRFS_DEV_STAT_VALUES_MAX]; 1631c11b63eSJeff Mahoney 1641c11b63eSJeff Mahoney struct extent_io_tree alloc_state; 165668e48afSAnand Jain 166668e48afSAnand Jain struct completion kobj_unregister; 167668e48afSAnand Jain /* For sysfs/FSID/devinfo/devid/ */ 168668e48afSAnand Jain struct kobject devid_kobj; 169eb3b5053SDavid Sterba 170eb3b5053SDavid Sterba /* Bandwidth limit for scrub, in bytes */ 171eb3b5053SDavid Sterba u64 scrub_speed_max; 1720b86a832SChris Mason }; 1730b86a832SChris Mason 1747cc8e58dSMiao Xie /* 1752103da3bSJosef Bacik * Block group or device which contains an active swapfile. Used for preventing 1762103da3bSJosef Bacik * unsafe operations while a swapfile is active. 1772103da3bSJosef Bacik * 1782103da3bSJosef Bacik * These are sorted on (ptr, inode) (note that a block group or device can 1792103da3bSJosef Bacik * contain more than one swapfile). We compare the pointer values because we 1802103da3bSJosef Bacik * don't actually care what the object is, we just need a quick check whether 1812103da3bSJosef Bacik * the object exists in the rbtree. 1822103da3bSJosef Bacik */ 1832103da3bSJosef Bacik struct btrfs_swapfile_pin { 1842103da3bSJosef Bacik struct rb_node node; 1852103da3bSJosef Bacik void *ptr; 1862103da3bSJosef Bacik struct inode *inode; 1872103da3bSJosef Bacik /* 1882103da3bSJosef Bacik * If true, ptr points to a struct btrfs_block_group. Otherwise, ptr 1892103da3bSJosef Bacik * points to a struct btrfs_device. 1902103da3bSJosef Bacik */ 1912103da3bSJosef Bacik bool is_block_group; 1922103da3bSJosef Bacik /* 1932103da3bSJosef Bacik * Only used when 'is_block_group' is true and it is the number of 1942103da3bSJosef Bacik * extents used by a swapfile for this block group ('ptr' field). 1952103da3bSJosef Bacik */ 1962103da3bSJosef Bacik int bg_extent_count; 1972103da3bSJosef Bacik }; 1982103da3bSJosef Bacik 1992103da3bSJosef Bacik /* 2007cc8e58dSMiao Xie * If we read those variants at the context of their own lock, we needn't 2017cc8e58dSMiao Xie * use the following helpers, reading them directly is safe. 2027cc8e58dSMiao Xie */ 2037cc8e58dSMiao Xie #if BITS_PER_LONG==32 && defined(CONFIG_SMP) 2047cc8e58dSMiao Xie #define BTRFS_DEVICE_GETSET_FUNCS(name) \ 2057cc8e58dSMiao Xie static inline u64 \ 2067cc8e58dSMiao Xie btrfs_device_get_##name(const struct btrfs_device *dev) \ 2077cc8e58dSMiao Xie { \ 2087cc8e58dSMiao Xie u64 size; \ 2097cc8e58dSMiao Xie unsigned int seq; \ 2107cc8e58dSMiao Xie \ 2117cc8e58dSMiao Xie do { \ 2127cc8e58dSMiao Xie seq = read_seqcount_begin(&dev->data_seqcount); \ 2137cc8e58dSMiao Xie size = dev->name; \ 2147cc8e58dSMiao Xie } while (read_seqcount_retry(&dev->data_seqcount, seq)); \ 2157cc8e58dSMiao Xie return size; \ 2167cc8e58dSMiao Xie } \ 2177cc8e58dSMiao Xie \ 2187cc8e58dSMiao Xie static inline void \ 2197cc8e58dSMiao Xie btrfs_device_set_##name(struct btrfs_device *dev, u64 size) \ 2207cc8e58dSMiao Xie { \ 221c41ec452SSu Yue preempt_disable(); \ 2227cc8e58dSMiao Xie write_seqcount_begin(&dev->data_seqcount); \ 2237cc8e58dSMiao Xie dev->name = size; \ 2247cc8e58dSMiao Xie write_seqcount_end(&dev->data_seqcount); \ 225c41ec452SSu Yue preempt_enable(); \ 2267cc8e58dSMiao Xie } 22794545870SThomas Gleixner #elif BITS_PER_LONG==32 && defined(CONFIG_PREEMPTION) 2287cc8e58dSMiao Xie #define BTRFS_DEVICE_GETSET_FUNCS(name) \ 2297cc8e58dSMiao Xie static inline u64 \ 2307cc8e58dSMiao Xie btrfs_device_get_##name(const struct btrfs_device *dev) \ 2317cc8e58dSMiao Xie { \ 2327cc8e58dSMiao Xie u64 size; \ 2337cc8e58dSMiao Xie \ 2347cc8e58dSMiao Xie preempt_disable(); \ 2357cc8e58dSMiao Xie size = dev->name; \ 2367cc8e58dSMiao Xie preempt_enable(); \ 2377cc8e58dSMiao Xie return size; \ 2387cc8e58dSMiao Xie } \ 2397cc8e58dSMiao Xie \ 2407cc8e58dSMiao Xie static inline void \ 2417cc8e58dSMiao Xie btrfs_device_set_##name(struct btrfs_device *dev, u64 size) \ 2427cc8e58dSMiao Xie { \ 2437cc8e58dSMiao Xie preempt_disable(); \ 2447cc8e58dSMiao Xie dev->name = size; \ 2457cc8e58dSMiao Xie preempt_enable(); \ 2467cc8e58dSMiao Xie } 2477cc8e58dSMiao Xie #else 2487cc8e58dSMiao Xie #define BTRFS_DEVICE_GETSET_FUNCS(name) \ 2497cc8e58dSMiao Xie static inline u64 \ 2507cc8e58dSMiao Xie btrfs_device_get_##name(const struct btrfs_device *dev) \ 2517cc8e58dSMiao Xie { \ 2527cc8e58dSMiao Xie return dev->name; \ 2537cc8e58dSMiao Xie } \ 2547cc8e58dSMiao Xie \ 2557cc8e58dSMiao Xie static inline void \ 2567cc8e58dSMiao Xie btrfs_device_set_##name(struct btrfs_device *dev, u64 size) \ 2577cc8e58dSMiao Xie { \ 2587cc8e58dSMiao Xie dev->name = size; \ 2597cc8e58dSMiao Xie } 2607cc8e58dSMiao Xie #endif 2617cc8e58dSMiao Xie 2627cc8e58dSMiao Xie BTRFS_DEVICE_GETSET_FUNCS(total_bytes); 2637cc8e58dSMiao Xie BTRFS_DEVICE_GETSET_FUNCS(disk_total_bytes); 2647cc8e58dSMiao Xie BTRFS_DEVICE_GETSET_FUNCS(bytes_used); 2657cc8e58dSMiao Xie 266c4a816c6SNaohiro Aota enum btrfs_chunk_allocation_policy { 267c4a816c6SNaohiro Aota BTRFS_CHUNK_ALLOC_REGULAR, 2681cd6121fSNaohiro Aota BTRFS_CHUNK_ALLOC_ZONED, 269c4a816c6SNaohiro Aota }; 270c4a816c6SNaohiro Aota 27133fd2f71SAnand Jain /* 27233fd2f71SAnand Jain * Read policies for mirrored block group profiles, read picks the stripe based 27333fd2f71SAnand Jain * on these policies. 27433fd2f71SAnand Jain */ 27533fd2f71SAnand Jain enum btrfs_read_policy { 27633fd2f71SAnand Jain /* Use process PID to choose the stripe */ 27733fd2f71SAnand Jain BTRFS_READ_POLICY_PID, 27833fd2f71SAnand Jain BTRFS_NR_READ_POLICY, 27933fd2f71SAnand Jain }; 28033fd2f71SAnand Jain 2818a4b83ccSChris Mason struct btrfs_fs_devices { 2828a4b83ccSChris Mason u8 fsid[BTRFS_FSID_SIZE]; /* FS specific uuid */ 2837239ff4bSNikolay Borisov u8 metadata_uuid[BTRFS_FSID_SIZE]; 284d1a63002SNikolay Borisov bool fsid_change; 285c4babc5eSAnand Jain struct list_head fs_list; 2868a4b83ccSChris Mason 287add9745aSAnand Jain /* 288add9745aSAnand Jain * Number of devices under this fsid including missing and 289add9745aSAnand Jain * replace-target device and excludes seed devices. 290add9745aSAnand Jain */ 2918a4b83ccSChris Mason u64 num_devices; 292add9745aSAnand Jain 293add9745aSAnand Jain /* 294add9745aSAnand Jain * The number of devices that successfully opened, including 295add9745aSAnand Jain * replace-target, excludes seed devices. 296add9745aSAnand Jain */ 297a0af469bSChris Mason u64 open_devices; 298add9745aSAnand Jain 299add9745aSAnand Jain /* The number of devices that are under the chunk allocation list. */ 3002b82032cSYan Zheng u64 rw_devices; 301add9745aSAnand Jain 302add9745aSAnand Jain /* Count of missing devices under this fsid excluding seed device. */ 303cd02dca5SChris Mason u64 missing_devices; 3042b82032cSYan Zheng u64 total_rw_bytes; 305add9745aSAnand Jain 306add9745aSAnand Jain /* 307add9745aSAnand Jain * Count of devices from btrfs_super_block::num_devices for this fsid, 308add9745aSAnand Jain * which includes the seed device, excludes the transient replace-target 309add9745aSAnand Jain * device. 310add9745aSAnand Jain */ 31102db0844SJosef Bacik u64 total_devices; 312d1a63002SNikolay Borisov 313d1a63002SNikolay Borisov /* Highest generation number of seen devices */ 314d1a63002SNikolay Borisov u64 latest_generation; 315d1a63002SNikolay Borisov 316d24fa5c1SAnand Jain /* 317d24fa5c1SAnand Jain * The mount device or a device with highest generation after removal 318d24fa5c1SAnand Jain * or replace. 319d24fa5c1SAnand Jain */ 320d24fa5c1SAnand Jain struct btrfs_device *latest_dev; 321e5e9a520SChris Mason 322e5e9a520SChris Mason /* all of the devices in the FS, protected by a mutex 323e5e9a520SChris Mason * so we can safely walk it to write out the supers without 3249b011adfSWang Shilong * worrying about add/remove by the multi-device code. 3259b011adfSWang Shilong * Scrubbing super can kick off supers writing by holding 3269b011adfSWang Shilong * this mutex lock. 327e5e9a520SChris Mason */ 328e5e9a520SChris Mason struct mutex device_list_mutex; 3290b6f5d40SNikolay Borisov 3300b6f5d40SNikolay Borisov /* List of all devices, protected by device_list_mutex */ 3318a4b83ccSChris Mason struct list_head devices; 332b3075717SChris Mason 3330b6f5d40SNikolay Borisov /* 3340b6f5d40SNikolay Borisov * Devices which can satisfy space allocation. Protected by 3350b6f5d40SNikolay Borisov * chunk_mutex 3360b6f5d40SNikolay Borisov */ 337b3075717SChris Mason struct list_head alloc_list; 3382b82032cSYan Zheng 339944d3f9fSNikolay Borisov struct list_head seed_list; 3400395d84fSJohannes Thumshirn bool seeding; 3412b82032cSYan Zheng 3422b82032cSYan Zheng int opened; 343c289811cSChris Mason 344c289811cSChris Mason /* set when we find or add a device that doesn't have the 345c289811cSChris Mason * nonrot flag set 346c289811cSChris Mason */ 3477f0432d0SJohannes Thumshirn bool rotating; 34863a7cb13SDavid Sterba /* Devices support TRIM/discard commands */ 34963a7cb13SDavid Sterba bool discardable; 3502e7910d6SAnand Jain 3515a13f430SAnand Jain struct btrfs_fs_info *fs_info; 3522e7910d6SAnand Jain /* sysfs kobjects */ 353c1b7e474SAnand Jain struct kobject fsid_kobj; 354b5501504SAnand Jain struct kobject *devices_kobj; 355a013d141SAnand Jain struct kobject *devinfo_kobj; 3562e7910d6SAnand Jain struct completion kobj_unregister; 357c4a816c6SNaohiro Aota 358c4a816c6SNaohiro Aota enum btrfs_chunk_allocation_policy chunk_alloc_policy; 35933fd2f71SAnand Jain 36033fd2f71SAnand Jain /* Policy used to read the mirrored stripes */ 36133fd2f71SAnand Jain enum btrfs_read_policy read_policy; 3628a4b83ccSChris Mason }; 3638a4b83ccSChris Mason 364ab4ba2e1SQu Wenruo #define BTRFS_MAX_DEVS(info) ((BTRFS_MAX_ITEM_SIZE(info) \ 365ab4ba2e1SQu Wenruo - sizeof(struct btrfs_chunk)) \ 366ab4ba2e1SQu Wenruo / sizeof(struct btrfs_stripe) + 1) 367ab4ba2e1SQu Wenruo 368ab4ba2e1SQu Wenruo #define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \ 369ab4ba2e1SQu Wenruo - 2 * sizeof(struct btrfs_disk_key) \ 370ab4ba2e1SQu Wenruo - 2 * sizeof(struct btrfs_chunk)) \ 371ab4ba2e1SQu Wenruo / sizeof(struct btrfs_stripe) + 1) 372ab4ba2e1SQu Wenruo 3734c664611SQu Wenruo struct btrfs_io_stripe { 374cea9e445SChris Mason struct btrfs_device *dev; 3759ff7ddd3SChristoph Hellwig union { 3769ff7ddd3SChristoph Hellwig /* Block mapping */ 377cea9e445SChris Mason u64 physical; 3789ff7ddd3SChristoph Hellwig /* For the endio handler */ 3799ff7ddd3SChristoph Hellwig struct btrfs_io_context *bioc; 3809ff7ddd3SChristoph Hellwig }; 381a4012f06SChristoph Hellwig }; 382a4012f06SChristoph Hellwig 383a4012f06SChristoph Hellwig struct btrfs_discard_stripe { 384a4012f06SChristoph Hellwig struct btrfs_device *dev; 385a4012f06SChristoph Hellwig u64 physical; 386a4012f06SChristoph Hellwig u64 length; 387cea9e445SChris Mason }; 388cea9e445SChris Mason 3894c664611SQu Wenruo /* 3904c664611SQu Wenruo * Context for IO subsmission for device stripe. 3914c664611SQu Wenruo * 3924c664611SQu Wenruo * - Track the unfinished mirrors for mirror based profiles 3934c664611SQu Wenruo * Mirror based profiles are SINGLE/DUP/RAID1/RAID10. 3944c664611SQu Wenruo * 3954c664611SQu Wenruo * - Contain the logical -> physical mapping info 3964c664611SQu Wenruo * Used by submit_stripe_bio() for mapping logical bio 3974c664611SQu Wenruo * into physical device address. 3984c664611SQu Wenruo * 3994c664611SQu Wenruo * - Contain device replace info 4004c664611SQu Wenruo * Used by handle_ops_on_dev_replace() to copy logical bios 4014c664611SQu Wenruo * into the new device. 4024c664611SQu Wenruo * 4034c664611SQu Wenruo * - Contain RAID56 full stripe logical bytenrs 4044c664611SQu Wenruo */ 4054c664611SQu Wenruo struct btrfs_io_context { 406140475aeSElena Reshetova refcount_t refs; 407c404e0dcSMiao Xie struct btrfs_fs_info *fs_info; 40810f11900SZhao Lei u64 map_type; /* get from map_lookup->type */ 4097d2b4daaSChris Mason struct bio *orig_bio; 410a236aed1SChris Mason atomic_t error; 4114ced85f8SQu Wenruo u16 max_errors; 4124ced85f8SQu Wenruo 4134ced85f8SQu Wenruo /* 4144ced85f8SQu Wenruo * The total number of stripes, including the extra duplicated 4154ced85f8SQu Wenruo * stripe for replace. 4164ced85f8SQu Wenruo */ 4174ced85f8SQu Wenruo u16 num_stripes; 4184ced85f8SQu Wenruo 4194ced85f8SQu Wenruo /* 4204ced85f8SQu Wenruo * The mirror_num of this bioc. 4214ced85f8SQu Wenruo * 4224ced85f8SQu Wenruo * This is for reads which use 0 as mirror_num, thus we should return a 4234ced85f8SQu Wenruo * valid mirror_num (>0) for the reader. 4244ced85f8SQu Wenruo */ 4254ced85f8SQu Wenruo u16 mirror_num; 4264ced85f8SQu Wenruo 4274ced85f8SQu Wenruo /* 4284ced85f8SQu Wenruo * The following two members are for dev-replace case only. 4294ced85f8SQu Wenruo * 4301faf3885SQu Wenruo * @replace_nr_stripes: Number of duplicated stripes which need to be 4314ced85f8SQu Wenruo * written to replace target. 4324ced85f8SQu Wenruo * Should be <= 2 (2 for DUP, otherwise <= 1). 4331faf3885SQu Wenruo * @replace_stripe_src: The array indicates where the duplicated stripes 4341faf3885SQu Wenruo * are from. 4354ced85f8SQu Wenruo * 4361faf3885SQu Wenruo * The @replace_stripe_src[] array is mostly for RAID56 cases. 4374ced85f8SQu Wenruo * As non-RAID56 stripes share the same contents of the mapped range, 4384ced85f8SQu Wenruo * thus no need to bother where the duplicated ones are from. 4394ced85f8SQu Wenruo * 4404ced85f8SQu Wenruo * But for RAID56 case, all stripes contain different contents, thus 4414ced85f8SQu Wenruo * we need a way to know the mapping. 4424ced85f8SQu Wenruo * 4434ced85f8SQu Wenruo * There is an example for the two members, using a RAID5 write: 4444ced85f8SQu Wenruo * 4454ced85f8SQu Wenruo * num_stripes: 4 (3 + 1 duplicated write) 4464ced85f8SQu Wenruo * stripes[0]: dev = devid 1, physical = X 4474ced85f8SQu Wenruo * stripes[1]: dev = devid 2, physical = Y 4484ced85f8SQu Wenruo * stripes[2]: dev = devid 3, physical = Z 4494ced85f8SQu Wenruo * stripes[3]: dev = devid 0, physical = Y 4504ced85f8SQu Wenruo * 4511faf3885SQu Wenruo * replace_nr_stripes = 1 4521faf3885SQu Wenruo * replace_stripe_src = 1 <- Means stripes[1] is involved in replace. 4531faf3885SQu Wenruo * The duplicated stripe index would be 4541faf3885SQu Wenruo * (@num_stripes - 1). 4551faf3885SQu Wenruo * 4561faf3885SQu Wenruo * Note, that we can still have cases replace_nr_stripes = 2 for DUP. 4571faf3885SQu Wenruo * In that case, all stripes share the same content, thus we don't 4581faf3885SQu Wenruo * need to bother @replace_stripe_src value at all. 4594ced85f8SQu Wenruo */ 4601faf3885SQu Wenruo u16 replace_nr_stripes; 4611faf3885SQu Wenruo s16 replace_stripe_src; 4628e5cfb55SZhao Lei /* 46318d758a2SQu Wenruo * Logical bytenr of the full stripe start, only for RAID56 cases. 46418d758a2SQu Wenruo * 46518d758a2SQu Wenruo * When this value is set to other than (u64)-1, the stripes[] should 46618d758a2SQu Wenruo * follow this pattern: 46718d758a2SQu Wenruo * 46818d758a2SQu Wenruo * (real_stripes = num_stripes - replace_nr_stripes) 46918d758a2SQu Wenruo * (data_stripes = (is_raid6) ? (real_stripes - 2) : (real_stripes - 1)) 47018d758a2SQu Wenruo * 47118d758a2SQu Wenruo * stripes[0]: The first data stripe 47218d758a2SQu Wenruo * stripes[1]: The second data stripe 47318d758a2SQu Wenruo * ... 47418d758a2SQu Wenruo * stripes[data_stripes - 1]: The last data stripe 47518d758a2SQu Wenruo * stripes[data_stripes]: The P stripe 47618d758a2SQu Wenruo * stripes[data_stripes + 1]: The Q stripe (only for RAID6). 4778e5cfb55SZhao Lei */ 47818d758a2SQu Wenruo u64 full_stripe_logical; 4794c664611SQu Wenruo struct btrfs_io_stripe stripes[]; 480cea9e445SChris Mason }; 481cea9e445SChris Mason 482b2117a39SMiao Xie struct btrfs_device_info { 483b2117a39SMiao Xie struct btrfs_device *dev; 484b2117a39SMiao Xie u64 dev_offset; 485b2117a39SMiao Xie u64 max_avail; 48673c5de00SArne Jansen u64 total_avail; 487b2117a39SMiao Xie }; 488b2117a39SMiao Xie 48931e50229SLiu Bo struct btrfs_raid_attr { 4908c3e3582SDavid Sterba u8 sub_stripes; /* sub_stripes info for map */ 4918c3e3582SDavid Sterba u8 dev_stripes; /* stripes per dev */ 4928c3e3582SDavid Sterba u8 devs_max; /* max devs to use */ 4938c3e3582SDavid Sterba u8 devs_min; /* min devs needed */ 4948c3e3582SDavid Sterba u8 tolerated_failures; /* max tolerated fail devs */ 4958c3e3582SDavid Sterba u8 devs_increment; /* ndevs has to be a multiple of this */ 4968c3e3582SDavid Sterba u8 ncopies; /* how many copies to data has */ 4978c3e3582SDavid Sterba u8 nparity; /* number of stripes worth of bytes to store 498b50836edSHans van Kranenburg * parity information */ 4998c3e3582SDavid Sterba u8 mindev_error; /* error code if min devs requisite is unmet */ 500ed23467bSAnand Jain const char raid_name[8]; /* name of the raid */ 50141a6e891SAnand Jain u64 bg_flag; /* block group flag of the raid */ 50231e50229SLiu Bo }; 50331e50229SLiu Bo 504af902047SZhao Lei extern const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES]; 505af902047SZhao Lei 5061abe9b8aSliubo struct map_lookup { 5071abe9b8aSliubo u64 type; 5081abe9b8aSliubo int io_align; 5091abe9b8aSliubo int io_width; 5101abe9b8aSliubo int num_stripes; 5111abe9b8aSliubo int sub_stripes; 512cf90d884SQu Wenruo int verified_stripes; /* For mount time dev extent verification */ 5134c664611SQu Wenruo struct btrfs_io_stripe stripes[]; 5141abe9b8aSliubo }; 5151abe9b8aSliubo 516a2de733cSArne Jansen #define map_lookup_size(n) (sizeof(struct map_lookup) + \ 5174c664611SQu Wenruo (sizeof(struct btrfs_io_stripe) * (n))) 518a2de733cSArne Jansen 519c9e9f97bSIlya Dryomov struct btrfs_balance_args; 52019a39dceSIlya Dryomov struct btrfs_balance_progress; 521c9e9f97bSIlya Dryomov struct btrfs_balance_control { 522c9e9f97bSIlya Dryomov struct btrfs_balance_args data; 523c9e9f97bSIlya Dryomov struct btrfs_balance_args meta; 524c9e9f97bSIlya Dryomov struct btrfs_balance_args sys; 525c9e9f97bSIlya Dryomov 526c9e9f97bSIlya Dryomov u64 flags; 52719a39dceSIlya Dryomov 52819a39dceSIlya Dryomov struct btrfs_balance_progress stat; 529c9e9f97bSIlya Dryomov }; 530c9e9f97bSIlya Dryomov 531562d7b15SJosef Bacik /* 532562d7b15SJosef Bacik * Search for a given device by the set parameters 533562d7b15SJosef Bacik */ 534562d7b15SJosef Bacik struct btrfs_dev_lookup_args { 535562d7b15SJosef Bacik u64 devid; 536562d7b15SJosef Bacik u8 *uuid; 537562d7b15SJosef Bacik u8 *fsid; 538562d7b15SJosef Bacik bool missing; 539562d7b15SJosef Bacik }; 540562d7b15SJosef Bacik 541562d7b15SJosef Bacik /* We have to initialize to -1 because BTRFS_DEV_REPLACE_DEVID is 0 */ 542562d7b15SJosef Bacik #define BTRFS_DEV_LOOKUP_ARGS_INIT { .devid = (u64)-1 } 543562d7b15SJosef Bacik 544562d7b15SJosef Bacik #define BTRFS_DEV_LOOKUP_ARGS(name) \ 545562d7b15SJosef Bacik struct btrfs_dev_lookup_args name = BTRFS_DEV_LOOKUP_ARGS_INIT 546562d7b15SJosef Bacik 547cf8cddd3SChristoph Hellwig enum btrfs_map_op { 548cf8cddd3SChristoph Hellwig BTRFS_MAP_READ, 549cf8cddd3SChristoph Hellwig BTRFS_MAP_WRITE, 550cf8cddd3SChristoph Hellwig BTRFS_MAP_DISCARD, 551cf8cddd3SChristoph Hellwig BTRFS_MAP_GET_READ_MIRRORS, 552cf8cddd3SChristoph Hellwig }; 553cf8cddd3SChristoph Hellwig 554cf8cddd3SChristoph Hellwig static inline enum btrfs_map_op btrfs_op(struct bio *bio) 555cf8cddd3SChristoph Hellwig { 556cf8cddd3SChristoph Hellwig switch (bio_op(bio)) { 557cf8cddd3SChristoph Hellwig case REQ_OP_DISCARD: 558cf8cddd3SChristoph Hellwig return BTRFS_MAP_DISCARD; 559cf8cddd3SChristoph Hellwig case REQ_OP_WRITE: 560cfe94440SNaohiro Aota case REQ_OP_ZONE_APPEND: 561cf8cddd3SChristoph Hellwig return BTRFS_MAP_WRITE; 562cf8cddd3SChristoph Hellwig default: 563cf8cddd3SChristoph Hellwig WARN_ON_ONCE(1); 564c730ae0cSMarcos Paulo de Souza fallthrough; 565cf8cddd3SChristoph Hellwig case REQ_OP_READ: 566cf8cddd3SChristoph Hellwig return BTRFS_MAP_READ; 567cf8cddd3SChristoph Hellwig } 568cf8cddd3SChristoph Hellwig } 569cf8cddd3SChristoph Hellwig 570b31bed17SJosef Bacik static inline unsigned long btrfs_chunk_item_size(int num_stripes) 571b31bed17SJosef Bacik { 572b31bed17SJosef Bacik ASSERT(num_stripes); 573b31bed17SJosef Bacik return sizeof(struct btrfs_chunk) + 574b31bed17SJosef Bacik sizeof(struct btrfs_stripe) * (num_stripes - 1); 575b31bed17SJosef Bacik } 576b31bed17SJosef Bacik 577*cb091225SQu Wenruo /* 578*cb091225SQu Wenruo * Do the type safe converstion from stripe_nr to offset inside the chunk. 579*cb091225SQu Wenruo * 580*cb091225SQu Wenruo * @stripe_nr is u32, with left shift it can overflow u32 for chunks larger 581*cb091225SQu Wenruo * than 4G. This does the proper type cast to avoid overflow. 582*cb091225SQu Wenruo */ 583*cb091225SQu Wenruo static inline u64 btrfs_stripe_nr_to_offset(u32 stripe_nr) 584*cb091225SQu Wenruo { 585*cb091225SQu Wenruo return (u64)stripe_nr << BTRFS_STRIPE_LEN_SHIFT; 586*cb091225SQu Wenruo } 587*cb091225SQu Wenruo 5884c664611SQu Wenruo void btrfs_get_bioc(struct btrfs_io_context *bioc); 5894c664611SQu Wenruo void btrfs_put_bioc(struct btrfs_io_context *bioc); 590cf8cddd3SChristoph Hellwig int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op, 591cea9e445SChris Mason u64 logical, u64 *length, 5924c664611SQu Wenruo struct btrfs_io_context **bioc_ret, int mirror_num); 593cf8cddd3SChristoph Hellwig int btrfs_map_sblock(struct btrfs_fs_info *fs_info, enum btrfs_map_op op, 594af8e2d1dSMiao Xie u64 logical, u64 *length, 5954c664611SQu Wenruo struct btrfs_io_context **bioc_ret); 596103c1972SChristoph Hellwig int __btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op, 597103c1972SChristoph Hellwig u64 logical, u64 *length, 598103c1972SChristoph Hellwig struct btrfs_io_context **bioc_ret, 599103c1972SChristoph Hellwig struct btrfs_io_stripe *smap, int *mirror_num_ret, 600103c1972SChristoph Hellwig int need_raid_map); 6014886ff7bSQu Wenruo int btrfs_map_repair_block(struct btrfs_fs_info *fs_info, 6024886ff7bSQu Wenruo struct btrfs_io_stripe *smap, u64 logical, 6034886ff7bSQu Wenruo u32 length, int mirror_num); 604a4012f06SChristoph Hellwig struct btrfs_discard_stripe *btrfs_map_discard(struct btrfs_fs_info *fs_info, 605a4012f06SChristoph Hellwig u64 logical, u64 *length_ret, 606a4012f06SChristoph Hellwig u32 *num_stripes); 6076bccf3abSJeff Mahoney int btrfs_read_sys_array(struct btrfs_fs_info *fs_info); 6085b4aacefSJeff Mahoney int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info); 609f6f39f7aSNikolay Borisov struct btrfs_block_group *btrfs_create_chunk(struct btrfs_trans_handle *trans, 61079bd3712SFilipe Manana u64 type); 611c8bf1b67SDavid Sterba void btrfs_mapping_tree_free(struct extent_map_tree *tree); 6128a4b83ccSChris Mason int btrfs_open_devices(struct btrfs_fs_devices *fs_devices, 61397288f2cSChristoph Hellwig fmode_t flags, void *holder); 61436350e95SGu Jinxiang struct btrfs_device *btrfs_scan_one_device(const char *path, 61536350e95SGu Jinxiang fmode_t flags, void *holder); 61616cab91aSAnand Jain int btrfs_forget_devices(dev_t devt); 61754eed6aeSNikolay Borisov void btrfs_close_devices(struct btrfs_fs_devices *fs_devices); 618bacce86aSAnand Jain void btrfs_free_extra_devids(struct btrfs_fs_devices *fs_devices); 619d6507cf1SNikolay Borisov void btrfs_assign_next_active_device(struct btrfs_device *device, 620d6507cf1SNikolay Borisov struct btrfs_device *this_dev); 621a27a94c2SNikolay Borisov struct btrfs_device *btrfs_find_device_by_devspec(struct btrfs_fs_info *fs_info, 622a27a94c2SNikolay Borisov u64 devid, 623a27a94c2SNikolay Borisov const char *devpath); 624faa775c4SJosef Bacik int btrfs_get_dev_args_from_path(struct btrfs_fs_info *fs_info, 625faa775c4SJosef Bacik struct btrfs_dev_lookup_args *args, 626faa775c4SJosef Bacik const char *path); 62712bd2fc0SIlya Dryomov struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info, 628bb21e302SAnand Jain const u64 *devid, const u8 *uuid, 629bb21e302SAnand Jain const char *path); 630faa775c4SJosef Bacik void btrfs_put_dev_args_from_path(struct btrfs_dev_lookup_args *args); 631a425f9d4SDavid Sterba void btrfs_free_device(struct btrfs_device *device); 6322ff7e61eSJeff Mahoney int btrfs_rm_device(struct btrfs_fs_info *fs_info, 6331a15eb72SJosef Bacik struct btrfs_dev_lookup_args *args, 6343fa421deSJosef Bacik struct block_device **bdev, fmode_t *mode); 635ffc5a379SDavid Sterba void __exit btrfs_cleanup_fs_uuids(void); 6365d964051SStefan Behrens int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len); 6378f18cf13SChris Mason int btrfs_grow_device(struct btrfs_trans_handle *trans, 6388f18cf13SChris Mason struct btrfs_device *device, u64 new_size); 639562d7b15SJosef Bacik struct btrfs_device *btrfs_find_device(const struct btrfs_fs_devices *fs_devices, 640562d7b15SJosef Bacik const struct btrfs_dev_lookup_args *args); 6418f18cf13SChris Mason int btrfs_shrink_device(struct btrfs_device *device, u64 new_size); 642da353f6bSDavid Sterba int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *path); 6436fcf6e2bSDavid Sterba int btrfs_balance(struct btrfs_fs_info *fs_info, 6446fcf6e2bSDavid Sterba struct btrfs_balance_control *bctl, 645c9e9f97bSIlya Dryomov struct btrfs_ioctl_balance_args *bargs); 646f89e09cfSAnand Jain void btrfs_describe_block_groups(u64 flags, char *buf, u32 size_buf); 6472b6ba629SIlya Dryomov int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info); 64868310a5eSIlya Dryomov int btrfs_recover_balance(struct btrfs_fs_info *fs_info); 649837d5b6eSIlya Dryomov int btrfs_pause_balance(struct btrfs_fs_info *fs_info); 65018bb8bbfSJohannes Thumshirn int btrfs_relocate_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset); 651a7e99c69SIlya Dryomov int btrfs_cancel_balance(struct btrfs_fs_info *fs_info); 652f7a81ea4SStefan Behrens int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info); 65397f4dd09SNikolay Borisov int btrfs_uuid_scan_kthread(void *data); 654a09f23c3SAnand Jain bool btrfs_chunk_writeable(struct btrfs_fs_info *fs_info, u64 chunk_offset); 65560dfdf25SNikolay Borisov int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes, 656ba1bf481SJosef Bacik u64 *start, u64 *max_avail); 657442a4f63SStefan Behrens void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index); 6582ff7e61eSJeff Mahoney int btrfs_get_dev_stats(struct btrfs_fs_info *fs_info, 659b27f7c0cSDavid Sterba struct btrfs_ioctl_get_dev_stats *stats); 660a8d1b164SJohannes Thumshirn int btrfs_init_devices_late(struct btrfs_fs_info *fs_info); 661733f4fbbSStefan Behrens int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info); 662196c9d8dSDavid Sterba int btrfs_run_dev_stats(struct btrfs_trans_handle *trans); 66368a9db5fSNikolay Borisov void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_device *srcdev); 66465237ee3SDavid Sterba void btrfs_rm_dev_replace_free_srcdev(struct btrfs_device *srcdev); 6654f5ad7bdSNikolay Borisov void btrfs_destroy_dev_replace_tgtdev(struct btrfs_device *tgtdev); 666592d92eeSLiu Bo int btrfs_is_parity_mirror(struct btrfs_fs_info *fs_info, 667e4ff5fb5SNikolay Borisov u64 logical, u64 len); 6682ff7e61eSJeff Mahoney unsigned long btrfs_full_stripe_len(struct btrfs_fs_info *fs_info, 66953b381b3SDavid Woodhouse u64 logical); 670bc88b486SQu Wenruo u64 btrfs_calc_stripe_length(const struct extent_map *em); 6710b30f719SQu Wenruo int btrfs_nr_parity_stripes(u64 type); 67279bd3712SFilipe Manana int btrfs_chunk_alloc_add_chunk_item(struct btrfs_trans_handle *trans, 67379bd3712SFilipe Manana struct btrfs_block_group *bg); 67497aff912SNikolay Borisov int btrfs_remove_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset); 67560ca842eSOmar Sandoval struct extent_map *btrfs_get_chunk_map(struct btrfs_fs_info *fs_info, 67660ca842eSOmar Sandoval u64 logical, u64 length); 6778f32380dSJohannes Thumshirn void btrfs_release_disk_super(struct btrfs_super_block *super); 678addc3fa7SMiao Xie 679442a4f63SStefan Behrens static inline void btrfs_dev_stat_inc(struct btrfs_device *dev, 680442a4f63SStefan Behrens int index) 681442a4f63SStefan Behrens { 682442a4f63SStefan Behrens atomic_inc(dev->dev_stat_values + index); 6839deae968SNikolay Borisov /* 6849deae968SNikolay Borisov * This memory barrier orders stores updating statistics before stores 6859deae968SNikolay Borisov * updating dev_stats_ccnt. 6869deae968SNikolay Borisov * 6879deae968SNikolay Borisov * It pairs with smp_rmb() in btrfs_run_dev_stats(). 6889deae968SNikolay Borisov */ 689addc3fa7SMiao Xie smp_mb__before_atomic(); 690addc3fa7SMiao Xie atomic_inc(&dev->dev_stats_ccnt); 691442a4f63SStefan Behrens } 692442a4f63SStefan Behrens 693442a4f63SStefan Behrens static inline int btrfs_dev_stat_read(struct btrfs_device *dev, 694442a4f63SStefan Behrens int index) 695442a4f63SStefan Behrens { 696442a4f63SStefan Behrens return atomic_read(dev->dev_stat_values + index); 697442a4f63SStefan Behrens } 698442a4f63SStefan Behrens 699442a4f63SStefan Behrens static inline int btrfs_dev_stat_read_and_reset(struct btrfs_device *dev, 700442a4f63SStefan Behrens int index) 701442a4f63SStefan Behrens { 702442a4f63SStefan Behrens int ret; 703442a4f63SStefan Behrens 704442a4f63SStefan Behrens ret = atomic_xchg(dev->dev_stat_values + index, 0); 7054660c49fSNikolay Borisov /* 7064660c49fSNikolay Borisov * atomic_xchg implies a full memory barriers as per atomic_t.txt: 7074660c49fSNikolay Borisov * - RMW operations that have a return value are fully ordered; 7084660c49fSNikolay Borisov * 7094660c49fSNikolay Borisov * This implicit memory barriers is paired with the smp_rmb in 7104660c49fSNikolay Borisov * btrfs_run_dev_stats 7114660c49fSNikolay Borisov */ 712addc3fa7SMiao Xie atomic_inc(&dev->dev_stats_ccnt); 713442a4f63SStefan Behrens return ret; 714442a4f63SStefan Behrens } 715442a4f63SStefan Behrens 716442a4f63SStefan Behrens static inline void btrfs_dev_stat_set(struct btrfs_device *dev, 717442a4f63SStefan Behrens int index, unsigned long val) 718442a4f63SStefan Behrens { 719442a4f63SStefan Behrens atomic_set(dev->dev_stat_values + index, val); 7209deae968SNikolay Borisov /* 7219deae968SNikolay Borisov * This memory barrier orders stores updating statistics before stores 7229deae968SNikolay Borisov * updating dev_stats_ccnt. 7239deae968SNikolay Borisov * 7249deae968SNikolay Borisov * It pairs with smp_rmb() in btrfs_run_dev_stats(). 7259deae968SNikolay Borisov */ 726addc3fa7SMiao Xie smp_mb__before_atomic(); 727addc3fa7SMiao Xie atomic_inc(&dev->dev_stats_ccnt); 728442a4f63SStefan Behrens } 729442a4f63SStefan Behrens 730cb3e217bSQu Wenruo static inline const char *btrfs_dev_name(const struct btrfs_device *device) 731cb3e217bSQu Wenruo { 732cb3e217bSQu Wenruo if (!device || test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) 733cb3e217bSQu Wenruo return "<missing disk>"; 734cb3e217bSQu Wenruo else 735cb3e217bSQu Wenruo return rcu_str_deref(device->name); 736cb3e217bSQu Wenruo } 737cb3e217bSQu Wenruo 738bbbf7243SNikolay Borisov void btrfs_commit_device_sizes(struct btrfs_transaction *trans); 73904216820SFilipe Manana 7404143cb8bSDavid Sterba struct list_head * __attribute_const__ btrfs_get_fs_uuids(void); 7416528b99dSAnand Jain bool btrfs_check_rw_degradable(struct btrfs_fs_info *fs_info, 7426528b99dSAnand Jain struct btrfs_device *failing_dev); 743313b0858SJosef Bacik void btrfs_scratch_superblocks(struct btrfs_fs_info *fs_info, 744313b0858SJosef Bacik struct block_device *bdev, 745313b0858SJosef Bacik const char *device_path); 74621634a19SQu Wenruo 747500a44c9SDavid Sterba enum btrfs_raid_types __attribute_const__ btrfs_bg_flags_to_raid_index(u64 flags); 74846df06b8SDavid Sterba int btrfs_bg_type_to_factor(u64 flags); 749158da513SDavid Sterba const char *btrfs_bg_type_to_raid_name(u64 flags); 750cf90d884SQu Wenruo int btrfs_verify_dev_extents(struct btrfs_fs_info *fs_info); 751554aed7dSJohannes Thumshirn bool btrfs_repair_one_zone(struct btrfs_fs_info *fs_info, u64 logical); 75246df06b8SDavid Sterba 753c2e79e86SJosef Bacik bool btrfs_pinned_by_swapfile(struct btrfs_fs_info *fs_info, void *ptr); 754c2e79e86SJosef Bacik 7550b86a832SChris Mason #endif 756