]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - fs/btrfs/volumes.h
Merge tag 'timers_urgent_for_v5.11_rc5' of git://git.kernel.org/pub/scm/linux/kernel...
[mirror_ubuntu-hirsute-kernel.git] / fs / btrfs / volumes.h
CommitLineData
9888c340 1/* SPDX-License-Identifier: GPL-2.0 */
0b86a832
CM
2/*
3 * Copyright (C) 2007 Oracle. All rights reserved.
0b86a832
CM
4 */
5
9888c340
DS
6#ifndef BTRFS_VOLUMES_H
7#define BTRFS_VOLUMES_H
8790d502 8
cea9e445 9#include <linux/bio.h>
b2117a39 10#include <linux/sort.h>
55e301fd 11#include <linux/btrfs.h>
8b712842 12#include "async-thread.h"
cea9e445 13
fce466ea
QW
14#define BTRFS_MAX_DATA_CHUNK_SIZE (10ULL * SZ_1G)
15
67a2c45e
MX
16extern struct mutex uuid_mutex;
17
ee22184b 18#define BTRFS_STRIPE_LEN SZ_64K
b2117a39 19
5f141126
NB
20struct btrfs_io_geometry {
21 /* remaining bytes before crossing a stripe */
22 u64 len;
23 /* offset of logical address in chunk */
24 u64 offset;
25 /* length of single IO stripe */
26 u64 stripe_len;
27 /* number of stripe where address falls */
28 u64 stripe_nr;
29 /* offset of address in stripe */
30 u64 stripe_offset;
31 /* offset of raid56 stripe into the chunk */
32 u64 raid56_stripe_offset;
33};
34
7cc8e58d
MX
35/*
36 * Use sequence counter to get consistent device stat data on
37 * 32-bit processors.
38 */
39#if BITS_PER_LONG==32 && defined(CONFIG_SMP)
40#include <linux/seqlock.h>
41#define __BTRFS_NEED_DEVICE_DATA_ORDERED
d5c82388
DB
42#define btrfs_device_data_ordered_init(device, info) \
43 seqcount_mutex_init(&device->data_seqcount, &info->chunk_mutex)
7cc8e58d 44#else
d5c82388 45#define btrfs_device_data_ordered_init(device, info) do { } while (0)
7cc8e58d
MX
46#endif
47
ebbede42 48#define BTRFS_DEV_STATE_WRITEABLE (0)
e12c9621 49#define BTRFS_DEV_STATE_IN_FS_METADATA (1)
e6e674bd 50#define BTRFS_DEV_STATE_MISSING (2)
401e29c1 51#define BTRFS_DEV_STATE_REPLACE_TGT (3)
1c3063b6 52#define BTRFS_DEV_STATE_FLUSH_SENT (4)
66d204a1 53#define BTRFS_DEV_STATE_NO_READA (5)
ebbede42 54
5b316468
NA
55struct btrfs_zoned_device_info;
56
0b86a832 57struct btrfs_device {
0b6f5d40
NB
58 struct list_head dev_list; /* device_list_mutex */
59 struct list_head dev_alloc_list; /* chunk mutex */
bbbf7243 60 struct list_head post_commit_list; /* chunk mutex */
2b82032c 61 struct btrfs_fs_devices *fs_devices;
fb456252 62 struct btrfs_fs_info *fs_info;
ffbd517d 63
8d1a7aae 64 struct rcu_string __rcu *name;
d5ee37bc
MX
65
66 u64 generation;
67
d5ee37bc
MX
68 struct block_device *bdev;
69
5b316468
NA
70 struct btrfs_zoned_device_info *zone_info;
71
d5ee37bc
MX
72 /* the mode sent to blkdev_get */
73 fmode_t mode;
74
ebbede42 75 unsigned long dev_state;
58efbc9f 76 blk_status_t last_flush_error;
b3075717 77
7cc8e58d 78#ifdef __BTRFS_NEED_DEVICE_DATA_ORDERED
d5c82388
DB
79 /* A seqcount_t with associated chunk_mutex (for lockdep) */
80 seqcount_mutex_t data_seqcount;
7cc8e58d
MX
81#endif
82
0b86a832
CM
83 /* the internal btrfs device id */
84 u64 devid;
85
6ba40b61 86 /* size of the device in memory */
0b86a832
CM
87 u64 total_bytes;
88
6ba40b61 89 /* size of the device on disk */
d6397bae
CB
90 u64 disk_total_bytes;
91
0b86a832
CM
92 /* bytes used */
93 u64 bytes_used;
94
95 /* optimal io alignment for this device */
96 u32 io_align;
97
98 /* optimal io width for this device */
99 u32 io_width;
3c45bfc1
DG
100 /* type and info about this device */
101 u64 type;
0b86a832
CM
102
103 /* minimal io size for this device */
104 u32 sector_size;
105
0b86a832 106 /* physical drive uuid (or lvm uuid) */
e17cade2 107 u8 uuid[BTRFS_UUID_SIZE];
8b712842 108
935e5cc9
MX
109 /*
110 * size of the device on the current transaction
111 *
112 * This variant is update when committing the transaction,
bbbf7243 113 * and protected by chunk mutex
935e5cc9
MX
114 */
115 u64 commit_total_bytes;
116
ce7213c7
MX
117 /* bytes used on the current transaction */
118 u64 commit_bytes_used;
935e5cc9 119
3c45bfc1 120 /* for sending down flush barriers */
3c45bfc1
DG
121 struct bio *flush_bio;
122 struct completion flush_wait;
123
a2de733c 124 /* per-device scrub information */
cadbc0a0 125 struct scrub_ctx *scrub_ctx;
a2de733c 126
90519d66 127 /* readahead state */
90519d66
AJ
128 atomic_t reada_in_flight;
129 u64 reada_next;
130 struct reada_zone *reada_curr_zone;
131 struct radix_tree_root reada_zones;
132 struct radix_tree_root reada_extents;
387125fc 133
442a4f63
SB
134 /* disk I/O failure stats. For detailed description refer to
135 * enum btrfs_dev_stat_values in ioctl.h */
733f4fbb 136 int dev_stats_valid;
addc3fa7
MX
137
138 /* Counter to record the change of device stats */
139 atomic_t dev_stats_ccnt;
442a4f63 140 atomic_t dev_stat_values[BTRFS_DEV_STAT_VALUES_MAX];
1c11b63e
JM
141
142 struct extent_io_tree alloc_state;
668e48af
AJ
143
144 struct completion kobj_unregister;
145 /* For sysfs/FSID/devinfo/devid/ */
146 struct kobject devid_kobj;
0b86a832
CM
147};
148
7cc8e58d
MX
149/*
150 * If we read those variants at the context of their own lock, we needn't
151 * use the following helpers, reading them directly is safe.
152 */
153#if BITS_PER_LONG==32 && defined(CONFIG_SMP)
154#define BTRFS_DEVICE_GETSET_FUNCS(name) \
155static inline u64 \
156btrfs_device_get_##name(const struct btrfs_device *dev) \
157{ \
158 u64 size; \
159 unsigned int seq; \
160 \
161 do { \
162 seq = read_seqcount_begin(&dev->data_seqcount); \
163 size = dev->name; \
164 } while (read_seqcount_retry(&dev->data_seqcount, seq)); \
165 return size; \
166} \
167 \
168static inline void \
169btrfs_device_set_##name(struct btrfs_device *dev, u64 size) \
170{ \
7cc8e58d
MX
171 write_seqcount_begin(&dev->data_seqcount); \
172 dev->name = size; \
173 write_seqcount_end(&dev->data_seqcount); \
7cc8e58d 174}
94545870 175#elif BITS_PER_LONG==32 && defined(CONFIG_PREEMPTION)
7cc8e58d
MX
176#define BTRFS_DEVICE_GETSET_FUNCS(name) \
177static inline u64 \
178btrfs_device_get_##name(const struct btrfs_device *dev) \
179{ \
180 u64 size; \
181 \
182 preempt_disable(); \
183 size = dev->name; \
184 preempt_enable(); \
185 return size; \
186} \
187 \
188static inline void \
189btrfs_device_set_##name(struct btrfs_device *dev, u64 size) \
190{ \
191 preempt_disable(); \
192 dev->name = size; \
193 preempt_enable(); \
194}
195#else
196#define BTRFS_DEVICE_GETSET_FUNCS(name) \
197static inline u64 \
198btrfs_device_get_##name(const struct btrfs_device *dev) \
199{ \
200 return dev->name; \
201} \
202 \
203static inline void \
204btrfs_device_set_##name(struct btrfs_device *dev, u64 size) \
205{ \
206 dev->name = size; \
207}
208#endif
209
210BTRFS_DEVICE_GETSET_FUNCS(total_bytes);
211BTRFS_DEVICE_GETSET_FUNCS(disk_total_bytes);
212BTRFS_DEVICE_GETSET_FUNCS(bytes_used);
213
c4a816c6
NA
214enum btrfs_chunk_allocation_policy {
215 BTRFS_CHUNK_ALLOC_REGULAR,
216};
217
33fd2f71
AJ
218/*
219 * Read policies for mirrored block group profiles, read picks the stripe based
220 * on these policies.
221 */
222enum btrfs_read_policy {
223 /* Use process PID to choose the stripe */
224 BTRFS_READ_POLICY_PID,
225 BTRFS_NR_READ_POLICY,
226};
227
8a4b83cc
CM
228struct btrfs_fs_devices {
229 u8 fsid[BTRFS_FSID_SIZE]; /* FS specific uuid */
7239ff4b 230 u8 metadata_uuid[BTRFS_FSID_SIZE];
d1a63002 231 bool fsid_change;
c4babc5e 232 struct list_head fs_list;
8a4b83cc 233
8a4b83cc 234 u64 num_devices;
a0af469b 235 u64 open_devices;
2b82032c 236 u64 rw_devices;
cd02dca5 237 u64 missing_devices;
2b82032c 238 u64 total_rw_bytes;
02db0844 239 u64 total_devices;
d1a63002
NB
240
241 /* Highest generation number of seen devices */
242 u64 latest_generation;
243
8a4b83cc 244 struct block_device *latest_bdev;
e5e9a520
CM
245
246 /* all of the devices in the FS, protected by a mutex
247 * so we can safely walk it to write out the supers without
9b011adf
WS
248 * worrying about add/remove by the multi-device code.
249 * Scrubbing super can kick off supers writing by holding
250 * this mutex lock.
e5e9a520
CM
251 */
252 struct mutex device_list_mutex;
0b6f5d40
NB
253
254 /* List of all devices, protected by device_list_mutex */
8a4b83cc 255 struct list_head devices;
b3075717 256
0b6f5d40
NB
257 /*
258 * Devices which can satisfy space allocation. Protected by
259 * chunk_mutex
260 */
b3075717 261 struct list_head alloc_list;
2b82032c 262
944d3f9f 263 struct list_head seed_list;
0395d84f 264 bool seeding;
2b82032c
YZ
265
266 int opened;
c289811c
CM
267
268 /* set when we find or add a device that doesn't have the
269 * nonrot flag set
270 */
7f0432d0 271 bool rotating;
2e7910d6 272
5a13f430 273 struct btrfs_fs_info *fs_info;
2e7910d6 274 /* sysfs kobjects */
c1b7e474 275 struct kobject fsid_kobj;
b5501504 276 struct kobject *devices_kobj;
a013d141 277 struct kobject *devinfo_kobj;
2e7910d6 278 struct completion kobj_unregister;
c4a816c6
NA
279
280 enum btrfs_chunk_allocation_policy chunk_alloc_policy;
33fd2f71
AJ
281
282 /* Policy used to read the mirrored stripes */
283 enum btrfs_read_policy read_policy;
8a4b83cc
CM
284};
285
facc8a22
MX
286#define BTRFS_BIO_INLINE_CSUM_SIZE 64
287
ab4ba2e1
QW
288#define BTRFS_MAX_DEVS(info) ((BTRFS_MAX_ITEM_SIZE(info) \
289 - sizeof(struct btrfs_chunk)) \
290 / sizeof(struct btrfs_stripe) + 1)
291
292#define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \
293 - 2 * sizeof(struct btrfs_disk_key) \
294 - 2 * sizeof(struct btrfs_chunk)) \
295 / sizeof(struct btrfs_stripe) + 1)
296
9be3395b
CM
297/*
298 * we need the mirror number and stripe index to be passed around
299 * the call chain while we are processing end_io (especially errors).
300 * Really, what we need is a btrfs_bio structure that has this info
301 * and is properly sized with its stripe array, but we're not there
302 * quite yet. We have our own btrfs bioset, and all of the bios
303 * we allocate are actually btrfs_io_bios. We'll cram as much of
304 * struct btrfs_bio as we can into this over time.
305 */
306struct btrfs_io_bio {
c1dc0896 307 unsigned int mirror_num;
c31efbdf 308 struct btrfs_device *device;
c1dc0896 309 u64 logical;
facc8a22
MX
310 u8 *csum;
311 u8 csum_inline[BTRFS_BIO_INLINE_CSUM_SIZE];
17347cec 312 struct bvec_iter iter;
fa1bcbe0
DS
313 /*
314 * This member must come last, bio_alloc_bioset will allocate enough
315 * bytes for entire btrfs_io_bio but relies on bio being last.
316 */
9be3395b
CM
317 struct bio bio;
318};
319
320static inline struct btrfs_io_bio *btrfs_io_bio(struct bio *bio)
321{
322 return container_of(bio, struct btrfs_io_bio, bio);
323}
324
b3a0dd50
DS
325static inline void btrfs_io_bio_free_csum(struct btrfs_io_bio *io_bio)
326{
327 if (io_bio->csum != io_bio->csum_inline) {
328 kfree(io_bio->csum);
329 io_bio->csum = NULL;
330 }
331}
332
cea9e445
CM
333struct btrfs_bio_stripe {
334 struct btrfs_device *dev;
335 u64 physical;
fce3bb9a 336 u64 length; /* only used for discard mappings */
cea9e445
CM
337};
338
a1d3c478 339struct btrfs_bio {
140475ae 340 refcount_t refs;
cea9e445 341 atomic_t stripes_pending;
c404e0dc 342 struct btrfs_fs_info *fs_info;
10f11900 343 u64 map_type; /* get from map_lookup->type */
cea9e445 344 bio_end_io_t *end_io;
7d2b4daa 345 struct bio *orig_bio;
cea9e445 346 void *private;
a236aed1
CM
347 atomic_t error;
348 int max_errors;
cea9e445 349 int num_stripes;
a1d3c478 350 int mirror_num;
2c8cdd6e
MX
351 int num_tgtdevs;
352 int *tgtdev_map;
8e5cfb55
ZL
353 /*
354 * logical block numbers for the start of each stripe
355 * The last one or two are p/q. These are sorted,
356 * so raid_map[0] is the start of our full stripe
357 */
358 u64 *raid_map;
cea9e445
CM
359 struct btrfs_bio_stripe stripes[];
360};
361
b2117a39
MX
362struct btrfs_device_info {
363 struct btrfs_device *dev;
364 u64 dev_offset;
365 u64 max_avail;
73c5de00 366 u64 total_avail;
b2117a39
MX
367};
368
31e50229 369struct btrfs_raid_attr {
8c3e3582
DS
370 u8 sub_stripes; /* sub_stripes info for map */
371 u8 dev_stripes; /* stripes per dev */
372 u8 devs_max; /* max devs to use */
373 u8 devs_min; /* min devs needed */
374 u8 tolerated_failures; /* max tolerated fail devs */
375 u8 devs_increment; /* ndevs has to be a multiple of this */
376 u8 ncopies; /* how many copies to data has */
377 u8 nparity; /* number of stripes worth of bytes to store
b50836ed 378 * parity information */
8c3e3582 379 u8 mindev_error; /* error code if min devs requisite is unmet */
ed23467b 380 const char raid_name[8]; /* name of the raid */
41a6e891 381 u64 bg_flag; /* block group flag of the raid */
31e50229
LB
382};
383
af902047 384extern const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES];
af902047 385
1abe9b8a 386struct map_lookup {
387 u64 type;
388 int io_align;
389 int io_width;
3d8da678 390 u64 stripe_len;
1abe9b8a 391 int num_stripes;
392 int sub_stripes;
cf90d884 393 int verified_stripes; /* For mount time dev extent verification */
1abe9b8a 394 struct btrfs_bio_stripe stripes[];
395};
396
a2de733c
AJ
397#define map_lookup_size(n) (sizeof(struct map_lookup) + \
398 (sizeof(struct btrfs_bio_stripe) * (n)))
399
c9e9f97b 400struct btrfs_balance_args;
19a39dce 401struct btrfs_balance_progress;
c9e9f97b 402struct btrfs_balance_control {
c9e9f97b
ID
403 struct btrfs_balance_args data;
404 struct btrfs_balance_args meta;
405 struct btrfs_balance_args sys;
406
407 u64 flags;
19a39dce
ID
408
409 struct btrfs_balance_progress stat;
c9e9f97b
ID
410};
411
cf8cddd3
CH
412enum btrfs_map_op {
413 BTRFS_MAP_READ,
414 BTRFS_MAP_WRITE,
415 BTRFS_MAP_DISCARD,
416 BTRFS_MAP_GET_READ_MIRRORS,
417};
418
419static inline enum btrfs_map_op btrfs_op(struct bio *bio)
420{
421 switch (bio_op(bio)) {
422 case REQ_OP_DISCARD:
423 return BTRFS_MAP_DISCARD;
424 case REQ_OP_WRITE:
425 return BTRFS_MAP_WRITE;
426 default:
427 WARN_ON_ONCE(1);
c730ae0c 428 fallthrough;
cf8cddd3
CH
429 case REQ_OP_READ:
430 return BTRFS_MAP_READ;
431 }
432}
433
6e9606d2
ZL
434void btrfs_get_bbio(struct btrfs_bio *bbio);
435void btrfs_put_bbio(struct btrfs_bio *bbio);
cf8cddd3 436int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
cea9e445 437 u64 logical, u64 *length,
a1d3c478 438 struct btrfs_bio **bbio_ret, int mirror_num);
cf8cddd3 439int btrfs_map_sblock(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
af8e2d1d 440 u64 logical, u64 *length,
825ad4c9 441 struct btrfs_bio **bbio_ret);
5f141126 442int btrfs_get_io_geometry(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
89b798ad 443 u64 logical, u64 len, struct btrfs_io_geometry *io_geom);
6bccf3ab 444int btrfs_read_sys_array(struct btrfs_fs_info *fs_info);
5b4aacef 445int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info);
c216b203 446int btrfs_alloc_chunk(struct btrfs_trans_handle *trans, u64 type);
c8bf1b67 447void btrfs_mapping_tree_free(struct extent_map_tree *tree);
58efbc9f 448blk_status_t btrfs_map_bio(struct btrfs_fs_info *fs_info, struct bio *bio,
08635bae 449 int mirror_num);
8a4b83cc 450int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
97288f2c 451 fmode_t flags, void *holder);
36350e95
GJ
452struct btrfs_device *btrfs_scan_one_device(const char *path,
453 fmode_t flags, void *holder);
228a73ab 454int btrfs_forget_devices(const char *path);
54eed6ae 455void btrfs_close_devices(struct btrfs_fs_devices *fs_devices);
bacce86a 456void btrfs_free_extra_devids(struct btrfs_fs_devices *fs_devices);
d6507cf1
NB
457void btrfs_assign_next_active_device(struct btrfs_device *device,
458 struct btrfs_device *this_dev);
a27a94c2
NB
459struct btrfs_device *btrfs_find_device_by_devspec(struct btrfs_fs_info *fs_info,
460 u64 devid,
461 const char *devpath);
12bd2fc0
ID
462struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
463 const u64 *devid,
464 const u8 *uuid);
a425f9d4 465void btrfs_free_device(struct btrfs_device *device);
2ff7e61e 466int btrfs_rm_device(struct btrfs_fs_info *fs_info,
da353f6b 467 const char *device_path, u64 devid);
ffc5a379 468void __exit btrfs_cleanup_fs_uuids(void);
5d964051 469int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len);
8f18cf13
CM
470int btrfs_grow_device(struct btrfs_trans_handle *trans,
471 struct btrfs_device *device, u64 new_size);
e4319cd9 472struct btrfs_device *btrfs_find_device(struct btrfs_fs_devices *fs_devices,
b2598edf 473 u64 devid, u8 *uuid, u8 *fsid);
8f18cf13 474int btrfs_shrink_device(struct btrfs_device *device, u64 new_size);
da353f6b 475int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *path);
6fcf6e2b
DS
476int btrfs_balance(struct btrfs_fs_info *fs_info,
477 struct btrfs_balance_control *bctl,
c9e9f97b 478 struct btrfs_ioctl_balance_args *bargs);
f89e09cf 479void btrfs_describe_block_groups(u64 flags, char *buf, u32 size_buf);
2b6ba629 480int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info);
68310a5e 481int btrfs_recover_balance(struct btrfs_fs_info *fs_info);
837d5b6e 482int btrfs_pause_balance(struct btrfs_fs_info *fs_info);
a7e99c69 483int btrfs_cancel_balance(struct btrfs_fs_info *fs_info);
f7a81ea4 484int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info);
97f4dd09 485int btrfs_uuid_scan_kthread(void *data);
2ff7e61e 486int btrfs_chunk_readonly(struct btrfs_fs_info *fs_info, u64 chunk_offset);
60dfdf25 487int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes,
ba1bf481 488 u64 *start, u64 *max_avail);
442a4f63 489void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index);
2ff7e61e 490int btrfs_get_dev_stats(struct btrfs_fs_info *fs_info,
b27f7c0c 491 struct btrfs_ioctl_get_dev_stats *stats);
cb517eab 492void btrfs_init_devices_late(struct btrfs_fs_info *fs_info);
733f4fbb 493int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info);
196c9d8d 494int btrfs_run_dev_stats(struct btrfs_trans_handle *trans);
68a9db5f 495void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_device *srcdev);
65237ee3 496void btrfs_rm_dev_replace_free_srcdev(struct btrfs_device *srcdev);
4f5ad7bd 497void btrfs_destroy_dev_replace_tgtdev(struct btrfs_device *tgtdev);
592d92ee 498int btrfs_is_parity_mirror(struct btrfs_fs_info *fs_info,
e4ff5fb5 499 u64 logical, u64 len);
2ff7e61e 500unsigned long btrfs_full_stripe_len(struct btrfs_fs_info *fs_info,
53b381b3 501 u64 logical);
6df9a95e 502int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
97aff912
NB
503 u64 chunk_offset, u64 chunk_size);
504int btrfs_remove_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset);
60ca842e
OS
505struct extent_map *btrfs_get_chunk_map(struct btrfs_fs_info *fs_info,
506 u64 logical, u64 length);
8f32380d 507void btrfs_release_disk_super(struct btrfs_super_block *super);
addc3fa7 508
442a4f63
SB
509static inline void btrfs_dev_stat_inc(struct btrfs_device *dev,
510 int index)
511{
512 atomic_inc(dev->dev_stat_values + index);
9deae968
NB
513 /*
514 * This memory barrier orders stores updating statistics before stores
515 * updating dev_stats_ccnt.
516 *
517 * It pairs with smp_rmb() in btrfs_run_dev_stats().
518 */
addc3fa7
MX
519 smp_mb__before_atomic();
520 atomic_inc(&dev->dev_stats_ccnt);
442a4f63
SB
521}
522
523static inline int btrfs_dev_stat_read(struct btrfs_device *dev,
524 int index)
525{
526 return atomic_read(dev->dev_stat_values + index);
527}
528
529static inline int btrfs_dev_stat_read_and_reset(struct btrfs_device *dev,
530 int index)
531{
532 int ret;
533
534 ret = atomic_xchg(dev->dev_stat_values + index, 0);
4660c49f
NB
535 /*
536 * atomic_xchg implies a full memory barriers as per atomic_t.txt:
537 * - RMW operations that have a return value are fully ordered;
538 *
539 * This implicit memory barriers is paired with the smp_rmb in
540 * btrfs_run_dev_stats
541 */
addc3fa7 542 atomic_inc(&dev->dev_stats_ccnt);
442a4f63
SB
543 return ret;
544}
545
546static inline void btrfs_dev_stat_set(struct btrfs_device *dev,
547 int index, unsigned long val)
548{
549 atomic_set(dev->dev_stat_values + index, val);
9deae968
NB
550 /*
551 * This memory barrier orders stores updating statistics before stores
552 * updating dev_stats_ccnt.
553 *
554 * It pairs with smp_rmb() in btrfs_run_dev_stats().
555 */
addc3fa7
MX
556 smp_mb__before_atomic();
557 atomic_inc(&dev->dev_stats_ccnt);
442a4f63
SB
558}
559
3e72ee88
QW
560/*
561 * Convert block group flags (BTRFS_BLOCK_GROUP_*) to btrfs_raid_types, which
562 * can be used as index to access btrfs_raid_array[].
563 */
564static inline enum btrfs_raid_types btrfs_bg_flags_to_raid_index(u64 flags)
565{
566 if (flags & BTRFS_BLOCK_GROUP_RAID10)
567 return BTRFS_RAID_RAID10;
568 else if (flags & BTRFS_BLOCK_GROUP_RAID1)
569 return BTRFS_RAID_RAID1;
47e6f742
DS
570 else if (flags & BTRFS_BLOCK_GROUP_RAID1C3)
571 return BTRFS_RAID_RAID1C3;
8d6fac00
DS
572 else if (flags & BTRFS_BLOCK_GROUP_RAID1C4)
573 return BTRFS_RAID_RAID1C4;
3e72ee88
QW
574 else if (flags & BTRFS_BLOCK_GROUP_DUP)
575 return BTRFS_RAID_DUP;
576 else if (flags & BTRFS_BLOCK_GROUP_RAID0)
577 return BTRFS_RAID_RAID0;
578 else if (flags & BTRFS_BLOCK_GROUP_RAID5)
579 return BTRFS_RAID_RAID5;
580 else if (flags & BTRFS_BLOCK_GROUP_RAID6)
581 return BTRFS_RAID_RAID6;
582
583 return BTRFS_RAID_SINGLE; /* BTRFS_BLOCK_GROUP_SINGLE */
584}
585
bbbf7243 586void btrfs_commit_device_sizes(struct btrfs_transaction *trans);
04216820 587
4143cb8b 588struct list_head * __attribute_const__ btrfs_get_fs_uuids(void);
6528b99d
AJ
589bool btrfs_check_rw_degradable(struct btrfs_fs_info *fs_info,
590 struct btrfs_device *failing_dev);
313b0858
JB
591void btrfs_scratch_superblocks(struct btrfs_fs_info *fs_info,
592 struct block_device *bdev,
593 const char *device_path);
21634a19 594
46df06b8 595int btrfs_bg_type_to_factor(u64 flags);
158da513 596const char *btrfs_bg_type_to_raid_name(u64 flags);
cf90d884 597int btrfs_verify_dev_extents(struct btrfs_fs_info *fs_info);
46df06b8 598
0b86a832 599#endif