btrfs: remove unnecessary tmp variable in btrfs_assign_next_active_device()
[linux-block.git] / fs / btrfs / volumes.c
CommitLineData
c1d7c514 1// SPDX-License-Identifier: GPL-2.0
0b86a832
CM
2/*
3 * Copyright (C) 2007 Oracle. All rights reserved.
0b86a832 4 */
c1d7c514 5
0b86a832 6#include <linux/sched.h>
fccc0007 7#include <linux/sched/mm.h>
0b86a832 8#include <linux/bio.h>
5a0e3ad6 9#include <linux/slab.h>
f2d8d74d 10#include <linux/blkdev.h>
442a4f63 11#include <linux/ratelimit.h>
59641015 12#include <linux/kthread.h>
53b381b3 13#include <linux/raid/pq.h>
803b2f54 14#include <linux/semaphore.h>
8da4b8c4 15#include <linux/uuid.h>
f8e10cd3 16#include <linux/list_sort.h>
784352fe 17#include "misc.h"
0b86a832
CM
18#include "ctree.h"
19#include "extent_map.h"
20#include "disk-io.h"
21#include "transaction.h"
22#include "print-tree.h"
23#include "volumes.h"
53b381b3 24#include "raid56.h"
8b712842 25#include "async-thread.h"
21adbd5c 26#include "check-integrity.h"
606686ee 27#include "rcu-string.h"
8dabb742 28#include "dev-replace.h"
99994cde 29#include "sysfs.h"
82fc28fb 30#include "tree-checker.h"
8719aaae 31#include "space-info.h"
aac0023c 32#include "block-group.h"
b0643e59 33#include "discard.h"
0b86a832 34
af902047
ZL
35const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
36 [BTRFS_RAID_RAID10] = {
37 .sub_stripes = 2,
38 .dev_stripes = 1,
39 .devs_max = 0, /* 0 == as many as possible */
40 .devs_min = 4,
8789f4fe 41 .tolerated_failures = 1,
af902047
ZL
42 .devs_increment = 2,
43 .ncopies = 2,
b50836ed 44 .nparity = 0,
ed23467b 45 .raid_name = "raid10",
41a6e891 46 .bg_flag = BTRFS_BLOCK_GROUP_RAID10,
f9fbcaa2 47 .mindev_error = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET,
af902047
ZL
48 },
49 [BTRFS_RAID_RAID1] = {
50 .sub_stripes = 1,
51 .dev_stripes = 1,
52 .devs_max = 2,
53 .devs_min = 2,
8789f4fe 54 .tolerated_failures = 1,
af902047
ZL
55 .devs_increment = 2,
56 .ncopies = 2,
b50836ed 57 .nparity = 0,
ed23467b 58 .raid_name = "raid1",
41a6e891 59 .bg_flag = BTRFS_BLOCK_GROUP_RAID1,
f9fbcaa2 60 .mindev_error = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET,
af902047 61 },
47e6f742
DS
62 [BTRFS_RAID_RAID1C3] = {
63 .sub_stripes = 1,
64 .dev_stripes = 1,
cf93e15e 65 .devs_max = 3,
47e6f742
DS
66 .devs_min = 3,
67 .tolerated_failures = 2,
68 .devs_increment = 3,
69 .ncopies = 3,
db26a024 70 .nparity = 0,
47e6f742
DS
71 .raid_name = "raid1c3",
72 .bg_flag = BTRFS_BLOCK_GROUP_RAID1C3,
73 .mindev_error = BTRFS_ERROR_DEV_RAID1C3_MIN_NOT_MET,
74 },
8d6fac00
DS
75 [BTRFS_RAID_RAID1C4] = {
76 .sub_stripes = 1,
77 .dev_stripes = 1,
cf93e15e 78 .devs_max = 4,
8d6fac00
DS
79 .devs_min = 4,
80 .tolerated_failures = 3,
81 .devs_increment = 4,
82 .ncopies = 4,
db26a024 83 .nparity = 0,
8d6fac00
DS
84 .raid_name = "raid1c4",
85 .bg_flag = BTRFS_BLOCK_GROUP_RAID1C4,
86 .mindev_error = BTRFS_ERROR_DEV_RAID1C4_MIN_NOT_MET,
87 },
af902047
ZL
88 [BTRFS_RAID_DUP] = {
89 .sub_stripes = 1,
90 .dev_stripes = 2,
91 .devs_max = 1,
92 .devs_min = 1,
8789f4fe 93 .tolerated_failures = 0,
af902047
ZL
94 .devs_increment = 1,
95 .ncopies = 2,
b50836ed 96 .nparity = 0,
ed23467b 97 .raid_name = "dup",
41a6e891 98 .bg_flag = BTRFS_BLOCK_GROUP_DUP,
f9fbcaa2 99 .mindev_error = 0,
af902047
ZL
100 },
101 [BTRFS_RAID_RAID0] = {
102 .sub_stripes = 1,
103 .dev_stripes = 1,
104 .devs_max = 0,
105 .devs_min = 2,
8789f4fe 106 .tolerated_failures = 0,
af902047
ZL
107 .devs_increment = 1,
108 .ncopies = 1,
b50836ed 109 .nparity = 0,
ed23467b 110 .raid_name = "raid0",
41a6e891 111 .bg_flag = BTRFS_BLOCK_GROUP_RAID0,
f9fbcaa2 112 .mindev_error = 0,
af902047
ZL
113 },
114 [BTRFS_RAID_SINGLE] = {
115 .sub_stripes = 1,
116 .dev_stripes = 1,
117 .devs_max = 1,
118 .devs_min = 1,
8789f4fe 119 .tolerated_failures = 0,
af902047
ZL
120 .devs_increment = 1,
121 .ncopies = 1,
b50836ed 122 .nparity = 0,
ed23467b 123 .raid_name = "single",
41a6e891 124 .bg_flag = 0,
f9fbcaa2 125 .mindev_error = 0,
af902047
ZL
126 },
127 [BTRFS_RAID_RAID5] = {
128 .sub_stripes = 1,
129 .dev_stripes = 1,
130 .devs_max = 0,
131 .devs_min = 2,
8789f4fe 132 .tolerated_failures = 1,
af902047 133 .devs_increment = 1,
da612e31 134 .ncopies = 1,
b50836ed 135 .nparity = 1,
ed23467b 136 .raid_name = "raid5",
41a6e891 137 .bg_flag = BTRFS_BLOCK_GROUP_RAID5,
f9fbcaa2 138 .mindev_error = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET,
af902047
ZL
139 },
140 [BTRFS_RAID_RAID6] = {
141 .sub_stripes = 1,
142 .dev_stripes = 1,
143 .devs_max = 0,
144 .devs_min = 3,
8789f4fe 145 .tolerated_failures = 2,
af902047 146 .devs_increment = 1,
da612e31 147 .ncopies = 1,
b50836ed 148 .nparity = 2,
ed23467b 149 .raid_name = "raid6",
41a6e891 150 .bg_flag = BTRFS_BLOCK_GROUP_RAID6,
f9fbcaa2 151 .mindev_error = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET,
af902047
ZL
152 },
153};
154
158da513 155const char *btrfs_bg_type_to_raid_name(u64 flags)
ed23467b 156{
158da513
DS
157 const int index = btrfs_bg_flags_to_raid_index(flags);
158
159 if (index >= BTRFS_NR_RAID_TYPES)
ed23467b
AJ
160 return NULL;
161
158da513 162 return btrfs_raid_array[index].raid_name;
ed23467b
AJ
163}
164
f89e09cf
AJ
165/*
166 * Fill @buf with textual description of @bg_flags, no more than @size_buf
167 * bytes including terminating null byte.
168 */
169void btrfs_describe_block_groups(u64 bg_flags, char *buf, u32 size_buf)
170{
171 int i;
172 int ret;
173 char *bp = buf;
174 u64 flags = bg_flags;
175 u32 size_bp = size_buf;
176
177 if (!flags) {
178 strcpy(bp, "NONE");
179 return;
180 }
181
182#define DESCRIBE_FLAG(flag, desc) \
183 do { \
184 if (flags & (flag)) { \
185 ret = snprintf(bp, size_bp, "%s|", (desc)); \
186 if (ret < 0 || ret >= size_bp) \
187 goto out_overflow; \
188 size_bp -= ret; \
189 bp += ret; \
190 flags &= ~(flag); \
191 } \
192 } while (0)
193
194 DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_DATA, "data");
195 DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_SYSTEM, "system");
196 DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_METADATA, "metadata");
197
198 DESCRIBE_FLAG(BTRFS_AVAIL_ALLOC_BIT_SINGLE, "single");
199 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
200 DESCRIBE_FLAG(btrfs_raid_array[i].bg_flag,
201 btrfs_raid_array[i].raid_name);
202#undef DESCRIBE_FLAG
203
204 if (flags) {
205 ret = snprintf(bp, size_bp, "0x%llx|", flags);
206 size_bp -= ret;
207 }
208
209 if (size_bp < size_buf)
210 buf[size_buf - size_bp - 1] = '\0'; /* remove last | */
211
212 /*
213 * The text is trimmed, it's up to the caller to provide sufficiently
214 * large buffer
215 */
216out_overflow:;
217}
218
6f8e0fc7 219static int init_first_rw_device(struct btrfs_trans_handle *trans);
2ff7e61e 220static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info);
48a3b636 221static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev);
733f4fbb 222static void btrfs_dev_stat_print_on_load(struct btrfs_device *device);
5ab56090
LB
223static int __btrfs_map_block(struct btrfs_fs_info *fs_info,
224 enum btrfs_map_op op,
225 u64 logical, u64 *length,
226 struct btrfs_bio **bbio_ret,
227 int mirror_num, int need_raid_map);
2b82032c 228
9c6b1c4d
DS
229/*
230 * Device locking
231 * ==============
232 *
233 * There are several mutexes that protect manipulation of devices and low-level
234 * structures like chunks but not block groups, extents or files
235 *
236 * uuid_mutex (global lock)
237 * ------------------------
238 * protects the fs_uuids list that tracks all per-fs fs_devices, resulting from
239 * the SCAN_DEV ioctl registration or from mount either implicitly (the first
240 * device) or requested by the device= mount option
241 *
242 * the mutex can be very coarse and can cover long-running operations
243 *
244 * protects: updates to fs_devices counters like missing devices, rw devices,
52042d8e 245 * seeding, structure cloning, opening/closing devices at mount/umount time
9c6b1c4d
DS
246 *
247 * global::fs_devs - add, remove, updates to the global list
248 *
18c850fd
JB
249 * does not protect: manipulation of the fs_devices::devices list in general
250 * but in mount context it could be used to exclude list modifications by eg.
251 * scan ioctl
9c6b1c4d
DS
252 *
253 * btrfs_device::name - renames (write side), read is RCU
254 *
255 * fs_devices::device_list_mutex (per-fs, with RCU)
256 * ------------------------------------------------
257 * protects updates to fs_devices::devices, ie. adding and deleting
258 *
259 * simple list traversal with read-only actions can be done with RCU protection
260 *
261 * may be used to exclude some operations from running concurrently without any
262 * modifications to the list (see write_all_supers)
263 *
18c850fd
JB
264 * Is not required at mount and close times, because our device list is
265 * protected by the uuid_mutex at that point.
266 *
9c6b1c4d
DS
267 * balance_mutex
268 * -------------
269 * protects balance structures (status, state) and context accessed from
270 * several places (internally, ioctl)
271 *
272 * chunk_mutex
273 * -----------
274 * protects chunks, adding or removing during allocation, trim or when a new
0b6f5d40
NB
275 * device is added/removed. Additionally it also protects post_commit_list of
276 * individual devices, since they can be added to the transaction's
277 * post_commit_list only with chunk_mutex held.
9c6b1c4d
DS
278 *
279 * cleaner_mutex
280 * -------------
281 * a big lock that is held by the cleaner thread and prevents running subvolume
282 * cleaning together with relocation or delayed iputs
283 *
284 *
285 * Lock nesting
286 * ============
287 *
288 * uuid_mutex
ae3e715f
AJ
289 * device_list_mutex
290 * chunk_mutex
291 * balance_mutex
89595e80
AJ
292 *
293 *
c3e1f96c
GR
294 * Exclusive operations
295 * ====================
89595e80
AJ
296 *
297 * Maintains the exclusivity of the following operations that apply to the
298 * whole filesystem and cannot run in parallel.
299 *
300 * - Balance (*)
301 * - Device add
302 * - Device remove
303 * - Device replace (*)
304 * - Resize
305 *
306 * The device operations (as above) can be in one of the following states:
307 *
308 * - Running state
309 * - Paused state
310 * - Completed state
311 *
312 * Only device operations marked with (*) can go into the Paused state for the
313 * following reasons:
314 *
315 * - ioctl (only Balance can be Paused through ioctl)
316 * - filesystem remounted as read-only
317 * - filesystem unmounted and mounted as read-only
318 * - system power-cycle and filesystem mounted as read-only
319 * - filesystem or device errors leading to forced read-only
320 *
c3e1f96c
GR
321 * The status of exclusive operation is set and cleared atomically.
322 * During the course of Paused state, fs_info::exclusive_operation remains set.
89595e80
AJ
323 * A device operation in Paused or Running state can be canceled or resumed
324 * either by ioctl (Balance only) or when remounted as read-write.
c3e1f96c 325 * The exclusive status is cleared when the device operation is canceled or
89595e80 326 * completed.
9c6b1c4d
DS
327 */
328
67a2c45e 329DEFINE_MUTEX(uuid_mutex);
8a4b83cc 330static LIST_HEAD(fs_uuids);
4143cb8b 331struct list_head * __attribute_const__ btrfs_get_fs_uuids(void)
c73eccf7
AJ
332{
333 return &fs_uuids;
334}
8a4b83cc 335
2dfeca9b
DS
336/*
337 * alloc_fs_devices - allocate struct btrfs_fs_devices
7239ff4b
NB
338 * @fsid: if not NULL, copy the UUID to fs_devices::fsid
339 * @metadata_fsid: if not NULL, copy the UUID to fs_devices::metadata_fsid
2dfeca9b
DS
340 *
341 * Return a pointer to a new struct btrfs_fs_devices on success, or ERR_PTR().
342 * The returned struct is not linked onto any lists and can be destroyed with
343 * kfree() right away.
344 */
7239ff4b
NB
345static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid,
346 const u8 *metadata_fsid)
2208a378
ID
347{
348 struct btrfs_fs_devices *fs_devs;
349
78f2c9e6 350 fs_devs = kzalloc(sizeof(*fs_devs), GFP_KERNEL);
2208a378
ID
351 if (!fs_devs)
352 return ERR_PTR(-ENOMEM);
353
354 mutex_init(&fs_devs->device_list_mutex);
355
356 INIT_LIST_HEAD(&fs_devs->devices);
357 INIT_LIST_HEAD(&fs_devs->alloc_list);
c4babc5e 358 INIT_LIST_HEAD(&fs_devs->fs_list);
944d3f9f 359 INIT_LIST_HEAD(&fs_devs->seed_list);
2208a378
ID
360 if (fsid)
361 memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE);
2208a378 362
7239ff4b
NB
363 if (metadata_fsid)
364 memcpy(fs_devs->metadata_uuid, metadata_fsid, BTRFS_FSID_SIZE);
365 else if (fsid)
366 memcpy(fs_devs->metadata_uuid, fsid, BTRFS_FSID_SIZE);
367
2208a378
ID
368 return fs_devs;
369}
370
a425f9d4 371void btrfs_free_device(struct btrfs_device *device)
48dae9cf 372{
bbbf7243 373 WARN_ON(!list_empty(&device->post_commit_list));
48dae9cf 374 rcu_string_free(device->name);
1c11b63e 375 extent_io_tree_release(&device->alloc_state);
48dae9cf
DS
376 bio_put(device->flush_bio);
377 kfree(device);
378}
379
e4404d6e
YZ
380static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
381{
382 struct btrfs_device *device;
383 WARN_ON(fs_devices->opened);
384 while (!list_empty(&fs_devices->devices)) {
385 device = list_entry(fs_devices->devices.next,
386 struct btrfs_device, dev_list);
387 list_del(&device->dev_list);
a425f9d4 388 btrfs_free_device(device);
e4404d6e
YZ
389 }
390 kfree(fs_devices);
391}
392
ffc5a379 393void __exit btrfs_cleanup_fs_uuids(void)
8a4b83cc
CM
394{
395 struct btrfs_fs_devices *fs_devices;
8a4b83cc 396
2b82032c
YZ
397 while (!list_empty(&fs_uuids)) {
398 fs_devices = list_entry(fs_uuids.next,
c4babc5e
AJ
399 struct btrfs_fs_devices, fs_list);
400 list_del(&fs_devices->fs_list);
e4404d6e 401 free_fs_devices(fs_devices);
8a4b83cc 402 }
8a4b83cc
CM
403}
404
48dae9cf
DS
405/*
406 * Returns a pointer to a new btrfs_device on success; ERR_PTR() on error.
407 * Returned struct is not linked onto any lists and must be destroyed using
a425f9d4 408 * btrfs_free_device.
48dae9cf 409 */
154f7cb8 410static struct btrfs_device *__alloc_device(struct btrfs_fs_info *fs_info)
12bd2fc0
ID
411{
412 struct btrfs_device *dev;
413
78f2c9e6 414 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
12bd2fc0
ID
415 if (!dev)
416 return ERR_PTR(-ENOMEM);
417
e0ae9994
DS
418 /*
419 * Preallocate a bio that's always going to be used for flushing device
420 * barriers and matches the device lifespan
421 */
422 dev->flush_bio = bio_alloc_bioset(GFP_KERNEL, 0, NULL);
423 if (!dev->flush_bio) {
424 kfree(dev);
425 return ERR_PTR(-ENOMEM);
426 }
e0ae9994 427
12bd2fc0
ID
428 INIT_LIST_HEAD(&dev->dev_list);
429 INIT_LIST_HEAD(&dev->dev_alloc_list);
bbbf7243 430 INIT_LIST_HEAD(&dev->post_commit_list);
12bd2fc0 431
12bd2fc0 432 atomic_set(&dev->reada_in_flight, 0);
addc3fa7 433 atomic_set(&dev->dev_stats_ccnt, 0);
546bed63 434 btrfs_device_data_ordered_init(dev);
9bcaaea7 435 INIT_RADIX_TREE(&dev->reada_zones, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
d0164adc 436 INIT_RADIX_TREE(&dev->reada_extents, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
154f7cb8
QW
437 extent_io_tree_init(fs_info, &dev->alloc_state,
438 IO_TREE_DEVICE_ALLOC_STATE, NULL);
12bd2fc0
ID
439
440 return dev;
441}
442
7239ff4b
NB
443static noinline struct btrfs_fs_devices *find_fsid(
444 const u8 *fsid, const u8 *metadata_fsid)
8a4b83cc 445{
8a4b83cc
CM
446 struct btrfs_fs_devices *fs_devices;
447
7239ff4b
NB
448 ASSERT(fsid);
449
7a62d0f0 450 /* Handle non-split brain cases */
c4babc5e 451 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
7239ff4b
NB
452 if (metadata_fsid) {
453 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0
454 && memcmp(metadata_fsid, fs_devices->metadata_uuid,
455 BTRFS_FSID_SIZE) == 0)
456 return fs_devices;
457 } else {
458 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
459 return fs_devices;
460 }
8a4b83cc
CM
461 }
462 return NULL;
463}
464
c6730a0e
SY
465static struct btrfs_fs_devices *find_fsid_with_metadata_uuid(
466 struct btrfs_super_block *disk_super)
467{
468
469 struct btrfs_fs_devices *fs_devices;
470
471 /*
472 * Handle scanned device having completed its fsid change but
473 * belonging to a fs_devices that was created by first scanning
474 * a device which didn't have its fsid/metadata_uuid changed
475 * at all and the CHANGING_FSID_V2 flag set.
476 */
477 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
478 if (fs_devices->fsid_change &&
479 memcmp(disk_super->metadata_uuid, fs_devices->fsid,
480 BTRFS_FSID_SIZE) == 0 &&
481 memcmp(fs_devices->fsid, fs_devices->metadata_uuid,
482 BTRFS_FSID_SIZE) == 0) {
483 return fs_devices;
484 }
485 }
486 /*
487 * Handle scanned device having completed its fsid change but
488 * belonging to a fs_devices that was created by a device that
489 * has an outdated pair of fsid/metadata_uuid and
490 * CHANGING_FSID_V2 flag set.
491 */
492 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
493 if (fs_devices->fsid_change &&
494 memcmp(fs_devices->metadata_uuid,
495 fs_devices->fsid, BTRFS_FSID_SIZE) != 0 &&
496 memcmp(disk_super->metadata_uuid, fs_devices->metadata_uuid,
497 BTRFS_FSID_SIZE) == 0) {
498 return fs_devices;
499 }
500 }
501
502 return find_fsid(disk_super->fsid, disk_super->metadata_uuid);
503}
504
505
beaf8ab3
SB
506static int
507btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder,
508 int flush, struct block_device **bdev,
8f32380d 509 struct btrfs_super_block **disk_super)
beaf8ab3
SB
510{
511 int ret;
512
513 *bdev = blkdev_get_by_path(device_path, flags, holder);
514
515 if (IS_ERR(*bdev)) {
516 ret = PTR_ERR(*bdev);
beaf8ab3
SB
517 goto error;
518 }
519
520 if (flush)
521 filemap_write_and_wait((*bdev)->bd_inode->i_mapping);
9f6d2510 522 ret = set_blocksize(*bdev, BTRFS_BDEV_BLOCKSIZE);
beaf8ab3
SB
523 if (ret) {
524 blkdev_put(*bdev, flags);
525 goto error;
526 }
527 invalidate_bdev(*bdev);
8f32380d
JT
528 *disk_super = btrfs_read_dev_super(*bdev);
529 if (IS_ERR(*disk_super)) {
530 ret = PTR_ERR(*disk_super);
beaf8ab3
SB
531 blkdev_put(*bdev, flags);
532 goto error;
533 }
534
535 return 0;
536
537error:
538 *bdev = NULL;
beaf8ab3
SB
539 return ret;
540}
541
70bc7088
AJ
542static bool device_path_matched(const char *path, struct btrfs_device *device)
543{
544 int found;
545
546 rcu_read_lock();
547 found = strcmp(rcu_str_deref(device->name), path);
548 rcu_read_unlock();
549
550 return found == 0;
551}
552
d8367db3
AJ
553/*
554 * Search and remove all stale (devices which are not mounted) devices.
555 * When both inputs are NULL, it will search and release all stale devices.
556 * path: Optional. When provided will it release all unmounted devices
557 * matching this path only.
558 * skip_dev: Optional. Will skip this device when searching for the stale
559 * devices.
70bc7088
AJ
560 * Return: 0 for success or if @path is NULL.
561 * -EBUSY if @path is a mounted device.
562 * -ENOENT if @path does not match any device in the list.
d8367db3 563 */
70bc7088 564static int btrfs_free_stale_devices(const char *path,
fa6d2ae5 565 struct btrfs_device *skip_device)
4fde46f0 566{
fa6d2ae5
AJ
567 struct btrfs_fs_devices *fs_devices, *tmp_fs_devices;
568 struct btrfs_device *device, *tmp_device;
70bc7088
AJ
569 int ret = 0;
570
571 if (path)
572 ret = -ENOENT;
4fde46f0 573
fa6d2ae5 574 list_for_each_entry_safe(fs_devices, tmp_fs_devices, &fs_uuids, fs_list) {
4fde46f0 575
70bc7088 576 mutex_lock(&fs_devices->device_list_mutex);
fa6d2ae5
AJ
577 list_for_each_entry_safe(device, tmp_device,
578 &fs_devices->devices, dev_list) {
fa6d2ae5 579 if (skip_device && skip_device == device)
d8367db3 580 continue;
fa6d2ae5 581 if (path && !device->name)
4fde46f0 582 continue;
70bc7088 583 if (path && !device_path_matched(path, device))
38cf665d 584 continue;
70bc7088
AJ
585 if (fs_devices->opened) {
586 /* for an already deleted device return 0 */
587 if (path && ret != 0)
588 ret = -EBUSY;
589 break;
590 }
4fde46f0 591
4fde46f0 592 /* delete the stale device */
7bcb8164
AJ
593 fs_devices->num_devices--;
594 list_del(&device->dev_list);
595 btrfs_free_device(device);
596
70bc7088 597 ret = 0;
7bcb8164
AJ
598 }
599 mutex_unlock(&fs_devices->device_list_mutex);
70bc7088 600
7bcb8164
AJ
601 if (fs_devices->num_devices == 0) {
602 btrfs_sysfs_remove_fsid(fs_devices);
603 list_del(&fs_devices->fs_list);
604 free_fs_devices(fs_devices);
4fde46f0
AJ
605 }
606 }
70bc7088
AJ
607
608 return ret;
4fde46f0
AJ
609}
610
18c850fd
JB
611/*
612 * This is only used on mount, and we are protected from competing things
613 * messing with our fs_devices by the uuid_mutex, thus we do not need the
614 * fs_devices->device_list_mutex here.
615 */
0fb08bcc
AJ
616static int btrfs_open_one_device(struct btrfs_fs_devices *fs_devices,
617 struct btrfs_device *device, fmode_t flags,
618 void *holder)
619{
620 struct request_queue *q;
621 struct block_device *bdev;
0fb08bcc
AJ
622 struct btrfs_super_block *disk_super;
623 u64 devid;
624 int ret;
625
626 if (device->bdev)
627 return -EINVAL;
628 if (!device->name)
629 return -EINVAL;
630
631 ret = btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
8f32380d 632 &bdev, &disk_super);
0fb08bcc
AJ
633 if (ret)
634 return ret;
635
0fb08bcc
AJ
636 devid = btrfs_stack_device_id(&disk_super->dev_item);
637 if (devid != device->devid)
8f32380d 638 goto error_free_page;
0fb08bcc
AJ
639
640 if (memcmp(device->uuid, disk_super->dev_item.uuid, BTRFS_UUID_SIZE))
8f32380d 641 goto error_free_page;
0fb08bcc
AJ
642
643 device->generation = btrfs_super_generation(disk_super);
644
645 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
7239ff4b
NB
646 if (btrfs_super_incompat_flags(disk_super) &
647 BTRFS_FEATURE_INCOMPAT_METADATA_UUID) {
648 pr_err(
649 "BTRFS: Invalid seeding and uuid-changed device detected\n");
8f32380d 650 goto error_free_page;
7239ff4b
NB
651 }
652
ebbede42 653 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
0395d84f 654 fs_devices->seeding = true;
0fb08bcc 655 } else {
ebbede42
AJ
656 if (bdev_read_only(bdev))
657 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
658 else
659 set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
0fb08bcc
AJ
660 }
661
662 q = bdev_get_queue(bdev);
0fb08bcc 663 if (!blk_queue_nonrot(q))
7f0432d0 664 fs_devices->rotating = true;
0fb08bcc
AJ
665
666 device->bdev = bdev;
e12c9621 667 clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
0fb08bcc
AJ
668 device->mode = flags;
669
670 fs_devices->open_devices++;
ebbede42
AJ
671 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
672 device->devid != BTRFS_DEV_REPLACE_DEVID) {
0fb08bcc 673 fs_devices->rw_devices++;
b1b8e386 674 list_add_tail(&device->dev_alloc_list, &fs_devices->alloc_list);
0fb08bcc 675 }
8f32380d 676 btrfs_release_disk_super(disk_super);
0fb08bcc
AJ
677
678 return 0;
679
8f32380d
JT
680error_free_page:
681 btrfs_release_disk_super(disk_super);
0fb08bcc
AJ
682 blkdev_put(bdev, flags);
683
684 return -EINVAL;
685}
686
7a62d0f0
NB
687/*
688 * Handle scanned device having its CHANGING_FSID_V2 flag set and the fs_devices
c0d81c7c
SY
689 * being created with a disk that has already completed its fsid change. Such
690 * disk can belong to an fs which has its FSID changed or to one which doesn't.
691 * Handle both cases here.
7a62d0f0
NB
692 */
693static struct btrfs_fs_devices *find_fsid_inprogress(
694 struct btrfs_super_block *disk_super)
695{
696 struct btrfs_fs_devices *fs_devices;
697
698 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
699 if (memcmp(fs_devices->metadata_uuid, fs_devices->fsid,
700 BTRFS_FSID_SIZE) != 0 &&
701 memcmp(fs_devices->metadata_uuid, disk_super->fsid,
702 BTRFS_FSID_SIZE) == 0 && !fs_devices->fsid_change) {
703 return fs_devices;
704 }
705 }
706
c0d81c7c 707 return find_fsid(disk_super->fsid, NULL);
7a62d0f0
NB
708}
709
cc5de4e7
NB
710
711static struct btrfs_fs_devices *find_fsid_changed(
712 struct btrfs_super_block *disk_super)
713{
714 struct btrfs_fs_devices *fs_devices;
715
716 /*
717 * Handles the case where scanned device is part of an fs that had
718 * multiple successful changes of FSID but curently device didn't
05840710
NB
719 * observe it. Meaning our fsid will be different than theirs. We need
720 * to handle two subcases :
721 * 1 - The fs still continues to have different METADATA/FSID uuids.
722 * 2 - The fs is switched back to its original FSID (METADATA/FSID
723 * are equal).
cc5de4e7
NB
724 */
725 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
05840710 726 /* Changed UUIDs */
cc5de4e7
NB
727 if (memcmp(fs_devices->metadata_uuid, fs_devices->fsid,
728 BTRFS_FSID_SIZE) != 0 &&
729 memcmp(fs_devices->metadata_uuid, disk_super->metadata_uuid,
730 BTRFS_FSID_SIZE) == 0 &&
731 memcmp(fs_devices->fsid, disk_super->fsid,
05840710
NB
732 BTRFS_FSID_SIZE) != 0)
733 return fs_devices;
734
735 /* Unchanged UUIDs */
736 if (memcmp(fs_devices->metadata_uuid, fs_devices->fsid,
737 BTRFS_FSID_SIZE) == 0 &&
738 memcmp(fs_devices->fsid, disk_super->metadata_uuid,
739 BTRFS_FSID_SIZE) == 0)
cc5de4e7 740 return fs_devices;
cc5de4e7
NB
741 }
742
743 return NULL;
744}
1362089d
NB
745
746static struct btrfs_fs_devices *find_fsid_reverted_metadata(
747 struct btrfs_super_block *disk_super)
748{
749 struct btrfs_fs_devices *fs_devices;
750
751 /*
752 * Handle the case where the scanned device is part of an fs whose last
753 * metadata UUID change reverted it to the original FSID. At the same
754 * time * fs_devices was first created by another constitutent device
755 * which didn't fully observe the operation. This results in an
756 * btrfs_fs_devices created with metadata/fsid different AND
757 * btrfs_fs_devices::fsid_change set AND the metadata_uuid of the
758 * fs_devices equal to the FSID of the disk.
759 */
760 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
761 if (memcmp(fs_devices->fsid, fs_devices->metadata_uuid,
762 BTRFS_FSID_SIZE) != 0 &&
763 memcmp(fs_devices->metadata_uuid, disk_super->fsid,
764 BTRFS_FSID_SIZE) == 0 &&
765 fs_devices->fsid_change)
766 return fs_devices;
767 }
768
769 return NULL;
770}
60999ca4
DS
771/*
772 * Add new device to list of registered devices
773 *
774 * Returns:
e124ece5
AJ
775 * device pointer which was just added or updated when successful
776 * error pointer when failed
60999ca4 777 */
e124ece5 778static noinline struct btrfs_device *device_list_add(const char *path,
4306a974
AJ
779 struct btrfs_super_block *disk_super,
780 bool *new_device_added)
8a4b83cc
CM
781{
782 struct btrfs_device *device;
7a62d0f0 783 struct btrfs_fs_devices *fs_devices = NULL;
606686ee 784 struct rcu_string *name;
8a4b83cc 785 u64 found_transid = btrfs_super_generation(disk_super);
3acbcbfc 786 u64 devid = btrfs_stack_device_id(&disk_super->dev_item);
7239ff4b
NB
787 bool has_metadata_uuid = (btrfs_super_incompat_flags(disk_super) &
788 BTRFS_FEATURE_INCOMPAT_METADATA_UUID);
d1a63002
NB
789 bool fsid_change_in_progress = (btrfs_super_flags(disk_super) &
790 BTRFS_SUPER_FLAG_CHANGING_FSID_V2);
7239ff4b 791
cc5de4e7 792 if (fsid_change_in_progress) {
c0d81c7c 793 if (!has_metadata_uuid)
cc5de4e7 794 fs_devices = find_fsid_inprogress(disk_super);
c0d81c7c 795 else
cc5de4e7 796 fs_devices = find_fsid_changed(disk_super);
7a62d0f0 797 } else if (has_metadata_uuid) {
c6730a0e 798 fs_devices = find_fsid_with_metadata_uuid(disk_super);
7a62d0f0 799 } else {
1362089d
NB
800 fs_devices = find_fsid_reverted_metadata(disk_super);
801 if (!fs_devices)
802 fs_devices = find_fsid(disk_super->fsid, NULL);
7a62d0f0
NB
803 }
804
8a4b83cc 805
8a4b83cc 806 if (!fs_devices) {
7239ff4b
NB
807 if (has_metadata_uuid)
808 fs_devices = alloc_fs_devices(disk_super->fsid,
809 disk_super->metadata_uuid);
810 else
811 fs_devices = alloc_fs_devices(disk_super->fsid, NULL);
812
2208a378 813 if (IS_ERR(fs_devices))
e124ece5 814 return ERR_CAST(fs_devices);
2208a378 815
92900e51
AV
816 fs_devices->fsid_change = fsid_change_in_progress;
817
9c6d173e 818 mutex_lock(&fs_devices->device_list_mutex);
c4babc5e 819 list_add(&fs_devices->fs_list, &fs_uuids);
2208a378 820
8a4b83cc
CM
821 device = NULL;
822 } else {
9c6d173e 823 mutex_lock(&fs_devices->device_list_mutex);
09ba3bc9
AJ
824 device = btrfs_find_device(fs_devices, devid,
825 disk_super->dev_item.uuid, NULL, false);
7a62d0f0
NB
826
827 /*
828 * If this disk has been pulled into an fs devices created by
829 * a device which had the CHANGING_FSID_V2 flag then replace the
830 * metadata_uuid/fsid values of the fs_devices.
831 */
1362089d 832 if (fs_devices->fsid_change &&
7a62d0f0
NB
833 found_transid > fs_devices->latest_generation) {
834 memcpy(fs_devices->fsid, disk_super->fsid,
835 BTRFS_FSID_SIZE);
1362089d
NB
836
837 if (has_metadata_uuid)
838 memcpy(fs_devices->metadata_uuid,
839 disk_super->metadata_uuid,
840 BTRFS_FSID_SIZE);
841 else
842 memcpy(fs_devices->metadata_uuid,
843 disk_super->fsid, BTRFS_FSID_SIZE);
7a62d0f0
NB
844
845 fs_devices->fsid_change = false;
846 }
8a4b83cc 847 }
443f24fe 848
8a4b83cc 849 if (!device) {
9c6d173e
AJ
850 if (fs_devices->opened) {
851 mutex_unlock(&fs_devices->device_list_mutex);
e124ece5 852 return ERR_PTR(-EBUSY);
9c6d173e 853 }
2b82032c 854
12bd2fc0
ID
855 device = btrfs_alloc_device(NULL, &devid,
856 disk_super->dev_item.uuid);
857 if (IS_ERR(device)) {
9c6d173e 858 mutex_unlock(&fs_devices->device_list_mutex);
8a4b83cc 859 /* we can safely leave the fs_devices entry around */
e124ece5 860 return device;
8a4b83cc 861 }
606686ee
JB
862
863 name = rcu_string_strdup(path, GFP_NOFS);
864 if (!name) {
a425f9d4 865 btrfs_free_device(device);
9c6d173e 866 mutex_unlock(&fs_devices->device_list_mutex);
e124ece5 867 return ERR_PTR(-ENOMEM);
8a4b83cc 868 }
606686ee 869 rcu_assign_pointer(device->name, name);
90519d66 870
1f78160c 871 list_add_rcu(&device->dev_list, &fs_devices->devices);
f7171750 872 fs_devices->num_devices++;
e5e9a520 873
2b82032c 874 device->fs_devices = fs_devices;
4306a974 875 *new_device_added = true;
327f18cc
AJ
876
877 if (disk_super->label[0])
aa6c0df7
AJ
878 pr_info(
879 "BTRFS: device label %s devid %llu transid %llu %s scanned by %s (%d)\n",
880 disk_super->label, devid, found_transid, path,
881 current->comm, task_pid_nr(current));
327f18cc 882 else
aa6c0df7
AJ
883 pr_info(
884 "BTRFS: device fsid %pU devid %llu transid %llu %s scanned by %s (%d)\n",
885 disk_super->fsid, devid, found_transid, path,
886 current->comm, task_pid_nr(current));
327f18cc 887
606686ee 888 } else if (!device->name || strcmp(device->name->str, path)) {
b96de000
AJ
889 /*
890 * When FS is already mounted.
891 * 1. If you are here and if the device->name is NULL that
892 * means this device was missing at time of FS mount.
893 * 2. If you are here and if the device->name is different
894 * from 'path' that means either
895 * a. The same device disappeared and reappeared with
896 * different name. or
897 * b. The missing-disk-which-was-replaced, has
898 * reappeared now.
899 *
900 * We must allow 1 and 2a above. But 2b would be a spurious
901 * and unintentional.
902 *
903 * Further in case of 1 and 2a above, the disk at 'path'
904 * would have missed some transaction when it was away and
905 * in case of 2a the stale bdev has to be updated as well.
906 * 2b must not be allowed at all time.
907 */
908
909 /*
0f23ae74
CM
910 * For now, we do allow update to btrfs_fs_device through the
911 * btrfs dev scan cli after FS has been mounted. We're still
912 * tracking a problem where systems fail mount by subvolume id
913 * when we reject replacement on a mounted FS.
b96de000 914 */
0f23ae74 915 if (!fs_devices->opened && found_transid < device->generation) {
77bdae4d
AJ
916 /*
917 * That is if the FS is _not_ mounted and if you
918 * are here, that means there is more than one
919 * disk with same uuid and devid.We keep the one
920 * with larger generation number or the last-in if
921 * generation are equal.
922 */
9c6d173e 923 mutex_unlock(&fs_devices->device_list_mutex);
e124ece5 924 return ERR_PTR(-EEXIST);
77bdae4d 925 }
b96de000 926
a9261d41
AJ
927 /*
928 * We are going to replace the device path for a given devid,
929 * make sure it's the same device if the device is mounted
930 */
931 if (device->bdev) {
932 struct block_device *path_bdev;
933
934 path_bdev = lookup_bdev(path);
935 if (IS_ERR(path_bdev)) {
936 mutex_unlock(&fs_devices->device_list_mutex);
937 return ERR_CAST(path_bdev);
938 }
939
940 if (device->bdev != path_bdev) {
941 bdput(path_bdev);
942 mutex_unlock(&fs_devices->device_list_mutex);
943 btrfs_warn_in_rcu(device->fs_info,
79dae17d
AJ
944 "duplicate device %s devid %llu generation %llu scanned by %s (%d)",
945 path, devid, found_transid,
946 current->comm,
947 task_pid_nr(current));
a9261d41
AJ
948 return ERR_PTR(-EEXIST);
949 }
950 bdput(path_bdev);
951 btrfs_info_in_rcu(device->fs_info,
79dae17d
AJ
952 "devid %llu device path %s changed to %s scanned by %s (%d)",
953 devid, rcu_str_deref(device->name),
954 path, current->comm,
955 task_pid_nr(current));
a9261d41
AJ
956 }
957
606686ee 958 name = rcu_string_strdup(path, GFP_NOFS);
9c6d173e
AJ
959 if (!name) {
960 mutex_unlock(&fs_devices->device_list_mutex);
e124ece5 961 return ERR_PTR(-ENOMEM);
9c6d173e 962 }
606686ee
JB
963 rcu_string_free(device->name);
964 rcu_assign_pointer(device->name, name);
e6e674bd 965 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) {
cd02dca5 966 fs_devices->missing_devices--;
e6e674bd 967 clear_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
cd02dca5 968 }
8a4b83cc
CM
969 }
970
77bdae4d
AJ
971 /*
972 * Unmount does not free the btrfs_device struct but would zero
973 * generation along with most of the other members. So just update
974 * it back. We need it to pick the disk with largest generation
975 * (as above).
976 */
d1a63002 977 if (!fs_devices->opened) {
77bdae4d 978 device->generation = found_transid;
d1a63002
NB
979 fs_devices->latest_generation = max_t(u64, found_transid,
980 fs_devices->latest_generation);
981 }
77bdae4d 982
f2788d2f
AJ
983 fs_devices->total_devices = btrfs_super_num_devices(disk_super);
984
9c6d173e 985 mutex_unlock(&fs_devices->device_list_mutex);
e124ece5 986 return device;
8a4b83cc
CM
987}
988
e4404d6e
YZ
989static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
990{
991 struct btrfs_fs_devices *fs_devices;
992 struct btrfs_device *device;
993 struct btrfs_device *orig_dev;
d2979aa2 994 int ret = 0;
e4404d6e 995
7239ff4b 996 fs_devices = alloc_fs_devices(orig->fsid, NULL);
2208a378
ID
997 if (IS_ERR(fs_devices))
998 return fs_devices;
e4404d6e 999
adbbb863 1000 mutex_lock(&orig->device_list_mutex);
02db0844 1001 fs_devices->total_devices = orig->total_devices;
e4404d6e
YZ
1002
1003 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
606686ee
JB
1004 struct rcu_string *name;
1005
12bd2fc0
ID
1006 device = btrfs_alloc_device(NULL, &orig_dev->devid,
1007 orig_dev->uuid);
d2979aa2
AJ
1008 if (IS_ERR(device)) {
1009 ret = PTR_ERR(device);
e4404d6e 1010 goto error;
d2979aa2 1011 }
e4404d6e 1012
606686ee
JB
1013 /*
1014 * This is ok to do without rcu read locked because we hold the
1015 * uuid mutex so nothing we touch in here is going to disappear.
1016 */
e755f780 1017 if (orig_dev->name) {
78f2c9e6
DS
1018 name = rcu_string_strdup(orig_dev->name->str,
1019 GFP_KERNEL);
e755f780 1020 if (!name) {
a425f9d4 1021 btrfs_free_device(device);
d2979aa2 1022 ret = -ENOMEM;
e755f780
AJ
1023 goto error;
1024 }
1025 rcu_assign_pointer(device->name, name);
fd2696f3 1026 }
e4404d6e 1027
e4404d6e
YZ
1028 list_add(&device->dev_list, &fs_devices->devices);
1029 device->fs_devices = fs_devices;
1030 fs_devices->num_devices++;
1031 }
adbbb863 1032 mutex_unlock(&orig->device_list_mutex);
e4404d6e
YZ
1033 return fs_devices;
1034error:
adbbb863 1035 mutex_unlock(&orig->device_list_mutex);
e4404d6e 1036 free_fs_devices(fs_devices);
d2979aa2 1037 return ERR_PTR(ret);
e4404d6e
YZ
1038}
1039
3712ccb7
NB
1040static void __btrfs_free_extra_devids(struct btrfs_fs_devices *fs_devices,
1041 int step, struct btrfs_device **latest_dev)
dfe25020 1042{
c6e30871 1043 struct btrfs_device *device, *next;
a6b0d5c8 1044
46224705 1045 /* This is the initialized path, it is safe to release the devices. */
c6e30871 1046 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
3712ccb7 1047 if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state)) {
401e29c1 1048 if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
3712ccb7 1049 &device->dev_state) &&
998a0671
AJ
1050 !test_bit(BTRFS_DEV_STATE_MISSING,
1051 &device->dev_state) &&
3712ccb7
NB
1052 (!*latest_dev ||
1053 device->generation > (*latest_dev)->generation)) {
1054 *latest_dev = device;
a6b0d5c8 1055 }
2b82032c 1056 continue;
a6b0d5c8 1057 }
2b82032c 1058
8dabb742
SB
1059 if (device->devid == BTRFS_DEV_REPLACE_DEVID) {
1060 /*
1061 * In the first step, keep the device which has
1062 * the correct fsid and the devid that is used
1063 * for the dev_replace procedure.
1064 * In the second step, the dev_replace state is
1065 * read from the device tree and it is known
1066 * whether the procedure is really active or
1067 * not, which means whether this device is
1068 * used or whether it should be removed.
1069 */
401e29c1
AJ
1070 if (step == 0 || test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
1071 &device->dev_state)) {
8dabb742
SB
1072 continue;
1073 }
1074 }
2b82032c 1075 if (device->bdev) {
d4d77629 1076 blkdev_put(device->bdev, device->mode);
2b82032c
YZ
1077 device->bdev = NULL;
1078 fs_devices->open_devices--;
1079 }
ebbede42 1080 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
2b82032c 1081 list_del_init(&device->dev_alloc_list);
ebbede42 1082 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
401e29c1
AJ
1083 if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
1084 &device->dev_state))
8dabb742 1085 fs_devices->rw_devices--;
2b82032c 1086 }
e4404d6e
YZ
1087 list_del_init(&device->dev_list);
1088 fs_devices->num_devices--;
a425f9d4 1089 btrfs_free_device(device);
dfe25020 1090 }
2b82032c 1091
3712ccb7
NB
1092}
1093
1094/*
1095 * After we have read the system tree and know devids belonging to this
1096 * filesystem, remove the device which does not belong there.
1097 */
1098void btrfs_free_extra_devids(struct btrfs_fs_devices *fs_devices, int step)
1099{
1100 struct btrfs_device *latest_dev = NULL;
944d3f9f 1101 struct btrfs_fs_devices *seed_dev;
3712ccb7
NB
1102
1103 mutex_lock(&uuid_mutex);
3712ccb7 1104 __btrfs_free_extra_devids(fs_devices, step, &latest_dev);
944d3f9f
NB
1105
1106 list_for_each_entry(seed_dev, &fs_devices->seed_list, seed_list)
1107 __btrfs_free_extra_devids(seed_dev, step, &latest_dev);
2b82032c 1108
443f24fe 1109 fs_devices->latest_bdev = latest_dev->bdev;
a6b0d5c8 1110
dfe25020 1111 mutex_unlock(&uuid_mutex);
dfe25020 1112}
a0af469b 1113
14238819
AJ
1114static void btrfs_close_bdev(struct btrfs_device *device)
1115{
08ffcae8
DS
1116 if (!device->bdev)
1117 return;
1118
ebbede42 1119 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
14238819
AJ
1120 sync_blockdev(device->bdev);
1121 invalidate_bdev(device->bdev);
1122 }
1123
08ffcae8 1124 blkdev_put(device->bdev, device->mode);
14238819
AJ
1125}
1126
959b1c04 1127static void btrfs_close_one_device(struct btrfs_device *device)
f448341a
AJ
1128{
1129 struct btrfs_fs_devices *fs_devices = device->fs_devices;
f448341a 1130
ebbede42 1131 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
f448341a
AJ
1132 device->devid != BTRFS_DEV_REPLACE_DEVID) {
1133 list_del_init(&device->dev_alloc_list);
1134 fs_devices->rw_devices--;
1135 }
1136
e6e674bd 1137 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
f448341a
AJ
1138 fs_devices->missing_devices--;
1139
959b1c04 1140 btrfs_close_bdev(device);
321f69f8 1141 if (device->bdev) {
3fff3975 1142 fs_devices->open_devices--;
321f69f8 1143 device->bdev = NULL;
f448341a 1144 }
321f69f8 1145 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
f448341a 1146
321f69f8
JT
1147 device->fs_info = NULL;
1148 atomic_set(&device->dev_stats_ccnt, 0);
1149 extent_io_tree_release(&device->alloc_state);
959b1c04 1150
321f69f8
JT
1151 /* Verify the device is back in a pristine state */
1152 ASSERT(!test_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state));
1153 ASSERT(!test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state));
1154 ASSERT(list_empty(&device->dev_alloc_list));
1155 ASSERT(list_empty(&device->post_commit_list));
1156 ASSERT(atomic_read(&device->reada_in_flight) == 0);
f448341a
AJ
1157}
1158
54eed6ae 1159static void close_fs_devices(struct btrfs_fs_devices *fs_devices)
8a4b83cc 1160{
2037a093 1161 struct btrfs_device *device, *tmp;
e4404d6e 1162
425c6ed6
JB
1163 lockdep_assert_held(&uuid_mutex);
1164
2b82032c 1165 if (--fs_devices->opened > 0)
54eed6ae 1166 return;
8a4b83cc 1167
425c6ed6 1168 list_for_each_entry_safe(device, tmp, &fs_devices->devices, dev_list)
959b1c04 1169 btrfs_close_one_device(device);
c9513edb 1170
e4404d6e
YZ
1171 WARN_ON(fs_devices->open_devices);
1172 WARN_ON(fs_devices->rw_devices);
2b82032c 1173 fs_devices->opened = 0;
0395d84f 1174 fs_devices->seeding = false;
c4989c2f 1175 fs_devices->fs_info = NULL;
8a4b83cc
CM
1176}
1177
54eed6ae 1178void btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
2b82032c 1179{
944d3f9f
NB
1180 LIST_HEAD(list);
1181 struct btrfs_fs_devices *tmp;
2b82032c
YZ
1182
1183 mutex_lock(&uuid_mutex);
54eed6ae 1184 close_fs_devices(fs_devices);
944d3f9f
NB
1185 if (!fs_devices->opened)
1186 list_splice_init(&fs_devices->seed_list, &list);
e4404d6e 1187
944d3f9f 1188 list_for_each_entry_safe(fs_devices, tmp, &list, seed_list) {
0226e0eb 1189 close_fs_devices(fs_devices);
944d3f9f 1190 list_del(&fs_devices->seed_list);
e4404d6e
YZ
1191 free_fs_devices(fs_devices);
1192 }
425c6ed6 1193 mutex_unlock(&uuid_mutex);
2b82032c
YZ
1194}
1195
897fb573 1196static int open_fs_devices(struct btrfs_fs_devices *fs_devices,
e4404d6e 1197 fmode_t flags, void *holder)
8a4b83cc 1198{
8a4b83cc 1199 struct btrfs_device *device;
443f24fe 1200 struct btrfs_device *latest_dev = NULL;
8a4b83cc 1201
d4d77629
TH
1202 flags |= FMODE_EXCL;
1203
f117e290 1204 list_for_each_entry(device, &fs_devices->devices, dev_list) {
f63e0cca 1205 /* Just open everything we can; ignore failures here */
0fb08bcc 1206 if (btrfs_open_one_device(fs_devices, device, flags, holder))
beaf8ab3 1207 continue;
a0af469b 1208
9f050db4
AJ
1209 if (!latest_dev ||
1210 device->generation > latest_dev->generation)
1211 latest_dev = device;
8a4b83cc 1212 }
1ed802c9
AJ
1213 if (fs_devices->open_devices == 0)
1214 return -EINVAL;
1215
2b82032c 1216 fs_devices->opened = 1;
443f24fe 1217 fs_devices->latest_bdev = latest_dev->bdev;
2b82032c 1218 fs_devices->total_rw_bytes = 0;
c4a816c6 1219 fs_devices->chunk_alloc_policy = BTRFS_CHUNK_ALLOC_REGULAR;
1ed802c9
AJ
1220
1221 return 0;
2b82032c
YZ
1222}
1223
f8e10cd3
AJ
1224static int devid_cmp(void *priv, struct list_head *a, struct list_head *b)
1225{
1226 struct btrfs_device *dev1, *dev2;
1227
1228 dev1 = list_entry(a, struct btrfs_device, dev_list);
1229 dev2 = list_entry(b, struct btrfs_device, dev_list);
1230
1231 if (dev1->devid < dev2->devid)
1232 return -1;
1233 else if (dev1->devid > dev2->devid)
1234 return 1;
1235 return 0;
1236}
1237
2b82032c 1238int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
97288f2c 1239 fmode_t flags, void *holder)
2b82032c
YZ
1240{
1241 int ret;
1242
f5194e34 1243 lockdep_assert_held(&uuid_mutex);
18c850fd
JB
1244 /*
1245 * The device_list_mutex cannot be taken here in case opening the
1246 * underlying device takes further locks like bd_mutex.
1247 *
1248 * We also don't need the lock here as this is called during mount and
1249 * exclusion is provided by uuid_mutex
1250 */
f5194e34 1251
2b82032c 1252 if (fs_devices->opened) {
e4404d6e
YZ
1253 fs_devices->opened++;
1254 ret = 0;
2b82032c 1255 } else {
f8e10cd3 1256 list_sort(NULL, &fs_devices->devices, devid_cmp);
897fb573 1257 ret = open_fs_devices(fs_devices, flags, holder);
2b82032c 1258 }
542c5908 1259
8a4b83cc
CM
1260 return ret;
1261}
1262
8f32380d 1263void btrfs_release_disk_super(struct btrfs_super_block *super)
6cf86a00 1264{
8f32380d
JT
1265 struct page *page = virt_to_page(super);
1266
6cf86a00
AJ
1267 put_page(page);
1268}
1269
b335eab8
NB
1270static struct btrfs_super_block *btrfs_read_disk_super(struct block_device *bdev,
1271 u64 bytenr)
6cf86a00 1272{
b335eab8
NB
1273 struct btrfs_super_block *disk_super;
1274 struct page *page;
6cf86a00
AJ
1275 void *p;
1276 pgoff_t index;
1277
1278 /* make sure our super fits in the device */
1279 if (bytenr + PAGE_SIZE >= i_size_read(bdev->bd_inode))
b335eab8 1280 return ERR_PTR(-EINVAL);
6cf86a00
AJ
1281
1282 /* make sure our super fits in the page */
b335eab8
NB
1283 if (sizeof(*disk_super) > PAGE_SIZE)
1284 return ERR_PTR(-EINVAL);
6cf86a00
AJ
1285
1286 /* make sure our super doesn't straddle pages on disk */
1287 index = bytenr >> PAGE_SHIFT;
b335eab8
NB
1288 if ((bytenr + sizeof(*disk_super) - 1) >> PAGE_SHIFT != index)
1289 return ERR_PTR(-EINVAL);
6cf86a00
AJ
1290
1291 /* pull in the page with our super */
b335eab8 1292 page = read_cache_page_gfp(bdev->bd_inode->i_mapping, index, GFP_KERNEL);
6cf86a00 1293
b335eab8
NB
1294 if (IS_ERR(page))
1295 return ERR_CAST(page);
6cf86a00 1296
b335eab8 1297 p = page_address(page);
6cf86a00
AJ
1298
1299 /* align our pointer to the offset of the super block */
b335eab8 1300 disk_super = p + offset_in_page(bytenr);
6cf86a00 1301
b335eab8
NB
1302 if (btrfs_super_bytenr(disk_super) != bytenr ||
1303 btrfs_super_magic(disk_super) != BTRFS_MAGIC) {
8f32380d 1304 btrfs_release_disk_super(p);
b335eab8 1305 return ERR_PTR(-EINVAL);
6cf86a00
AJ
1306 }
1307
b335eab8
NB
1308 if (disk_super->label[0] && disk_super->label[BTRFS_LABEL_SIZE - 1])
1309 disk_super->label[BTRFS_LABEL_SIZE - 1] = 0;
6cf86a00 1310
b335eab8 1311 return disk_super;
6cf86a00
AJ
1312}
1313
228a73ab
AJ
1314int btrfs_forget_devices(const char *path)
1315{
1316 int ret;
1317
1318 mutex_lock(&uuid_mutex);
1319 ret = btrfs_free_stale_devices(strlen(path) ? path : NULL, NULL);
1320 mutex_unlock(&uuid_mutex);
1321
1322 return ret;
1323}
1324
6f60cbd3
DS
1325/*
1326 * Look for a btrfs signature on a device. This may be called out of the mount path
1327 * and we are not allowed to call set_blocksize during the scan. The superblock
1328 * is read via pagecache
1329 */
36350e95
GJ
1330struct btrfs_device *btrfs_scan_one_device(const char *path, fmode_t flags,
1331 void *holder)
8a4b83cc
CM
1332{
1333 struct btrfs_super_block *disk_super;
4306a974 1334 bool new_device_added = false;
36350e95 1335 struct btrfs_device *device = NULL;
8a4b83cc 1336 struct block_device *bdev;
6f60cbd3 1337 u64 bytenr;
8a4b83cc 1338
899f9307
DS
1339 lockdep_assert_held(&uuid_mutex);
1340
6f60cbd3
DS
1341 /*
1342 * we would like to check all the supers, but that would make
1343 * a btrfs mount succeed after a mkfs from a different FS.
1344 * So, we need to add a special mount option to scan for
1345 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
1346 */
1347 bytenr = btrfs_sb_offset(0);
d4d77629 1348 flags |= FMODE_EXCL;
6f60cbd3
DS
1349
1350 bdev = blkdev_get_by_path(path, flags, holder);
b6ed73bc 1351 if (IS_ERR(bdev))
36350e95 1352 return ERR_CAST(bdev);
6f60cbd3 1353
b335eab8
NB
1354 disk_super = btrfs_read_disk_super(bdev, bytenr);
1355 if (IS_ERR(disk_super)) {
1356 device = ERR_CAST(disk_super);
6f60cbd3 1357 goto error_bdev_put;
05a5c55d 1358 }
6f60cbd3 1359
4306a974 1360 device = device_list_add(path, disk_super, &new_device_added);
36350e95 1361 if (!IS_ERR(device)) {
4306a974
AJ
1362 if (new_device_added)
1363 btrfs_free_stale_devices(path, device);
1364 }
6f60cbd3 1365
8f32380d 1366 btrfs_release_disk_super(disk_super);
6f60cbd3
DS
1367
1368error_bdev_put:
d4d77629 1369 blkdev_put(bdev, flags);
b6ed73bc 1370
36350e95 1371 return device;
8a4b83cc 1372}
0b86a832 1373
1c11b63e
JM
1374/*
1375 * Try to find a chunk that intersects [start, start + len] range and when one
1376 * such is found, record the end of it in *start
1377 */
1c11b63e
JM
1378static bool contains_pending_extent(struct btrfs_device *device, u64 *start,
1379 u64 len)
6df9a95e 1380{
1c11b63e 1381 u64 physical_start, physical_end;
6df9a95e 1382
1c11b63e 1383 lockdep_assert_held(&device->fs_info->chunk_mutex);
6df9a95e 1384
1c11b63e
JM
1385 if (!find_first_extent_bit(&device->alloc_state, *start,
1386 &physical_start, &physical_end,
1387 CHUNK_ALLOCATED, NULL)) {
c152b63e 1388
1c11b63e
JM
1389 if (in_range(physical_start, *start, len) ||
1390 in_range(*start, physical_start,
1391 physical_end - physical_start)) {
1392 *start = physical_end + 1;
1393 return true;
6df9a95e
JB
1394 }
1395 }
1c11b63e 1396 return false;
6df9a95e
JB
1397}
1398
3b4ffa40
NA
1399static u64 dev_extent_search_start(struct btrfs_device *device, u64 start)
1400{
1401 switch (device->fs_devices->chunk_alloc_policy) {
1402 case BTRFS_CHUNK_ALLOC_REGULAR:
1403 /*
1404 * We don't want to overwrite the superblock on the drive nor
1405 * any area used by the boot loader (grub for example), so we
1406 * make sure to start at an offset of at least 1MB.
1407 */
1408 return max_t(u64, start, SZ_1M);
1409 default:
1410 BUG();
1411 }
1412}
1413
1414/**
1415 * dev_extent_hole_check - check if specified hole is suitable for allocation
1416 * @device: the device which we have the hole
1417 * @hole_start: starting position of the hole
1418 * @hole_size: the size of the hole
1419 * @num_bytes: the size of the free space that we need
1420 *
1421 * This function may modify @hole_start and @hole_end to reflect the suitable
1422 * position for allocation. Returns 1 if hole position is updated, 0 otherwise.
1423 */
1424static bool dev_extent_hole_check(struct btrfs_device *device, u64 *hole_start,
1425 u64 *hole_size, u64 num_bytes)
1426{
1427 bool changed = false;
1428 u64 hole_end = *hole_start + *hole_size;
1429
1430 /*
1431 * Check before we set max_hole_start, otherwise we could end up
1432 * sending back this offset anyway.
1433 */
1434 if (contains_pending_extent(device, hole_start, *hole_size)) {
1435 if (hole_end >= *hole_start)
1436 *hole_size = hole_end - *hole_start;
1437 else
1438 *hole_size = 0;
1439 changed = true;
1440 }
1441
1442 switch (device->fs_devices->chunk_alloc_policy) {
1443 case BTRFS_CHUNK_ALLOC_REGULAR:
1444 /* No extra check */
1445 break;
1446 default:
1447 BUG();
1448 }
1449
1450 return changed;
1451}
6df9a95e 1452
0b86a832 1453/*
499f377f
JM
1454 * find_free_dev_extent_start - find free space in the specified device
1455 * @device: the device which we search the free space in
1456 * @num_bytes: the size of the free space that we need
1457 * @search_start: the position from which to begin the search
1458 * @start: store the start of the free space.
1459 * @len: the size of the free space. that we find, or the size
1460 * of the max free space if we don't find suitable free space
7bfc837d 1461 *
0b86a832
CM
1462 * this uses a pretty simple search, the expectation is that it is
1463 * called very infrequently and that a given device has a small number
1464 * of extents
7bfc837d
MX
1465 *
1466 * @start is used to store the start of the free space if we find. But if we
1467 * don't find suitable free space, it will be used to store the start position
1468 * of the max free space.
1469 *
1470 * @len is used to store the size of the free space that we find.
1471 * But if we don't find suitable free space, it is used to store the size of
1472 * the max free space.
135da976
QW
1473 *
1474 * NOTE: This function will search *commit* root of device tree, and does extra
1475 * check to ensure dev extents are not double allocated.
1476 * This makes the function safe to allocate dev extents but may not report
1477 * correct usable device space, as device extent freed in current transaction
1478 * is not reported as avaiable.
0b86a832 1479 */
9e3246a5
QW
1480static int find_free_dev_extent_start(struct btrfs_device *device,
1481 u64 num_bytes, u64 search_start, u64 *start,
1482 u64 *len)
0b86a832 1483{
0b246afa
JM
1484 struct btrfs_fs_info *fs_info = device->fs_info;
1485 struct btrfs_root *root = fs_info->dev_root;
0b86a832 1486 struct btrfs_key key;
7bfc837d 1487 struct btrfs_dev_extent *dev_extent;
2b82032c 1488 struct btrfs_path *path;
7bfc837d
MX
1489 u64 hole_size;
1490 u64 max_hole_start;
1491 u64 max_hole_size;
1492 u64 extent_end;
0b86a832
CM
1493 u64 search_end = device->total_bytes;
1494 int ret;
7bfc837d 1495 int slot;
0b86a832 1496 struct extent_buffer *l;
8cdc7c5b 1497
3b4ffa40 1498 search_start = dev_extent_search_start(device, search_start);
0b86a832 1499
6df9a95e
JB
1500 path = btrfs_alloc_path();
1501 if (!path)
1502 return -ENOMEM;
f2ab7618 1503
7bfc837d
MX
1504 max_hole_start = search_start;
1505 max_hole_size = 0;
1506
f2ab7618 1507again:
401e29c1
AJ
1508 if (search_start >= search_end ||
1509 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
7bfc837d 1510 ret = -ENOSPC;
6df9a95e 1511 goto out;
7bfc837d
MX
1512 }
1513
e4058b54 1514 path->reada = READA_FORWARD;
6df9a95e
JB
1515 path->search_commit_root = 1;
1516 path->skip_locking = 1;
7bfc837d 1517
0b86a832
CM
1518 key.objectid = device->devid;
1519 key.offset = search_start;
1520 key.type = BTRFS_DEV_EXTENT_KEY;
7bfc837d 1521
125ccb0a 1522 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
0b86a832 1523 if (ret < 0)
7bfc837d 1524 goto out;
1fcbac58
YZ
1525 if (ret > 0) {
1526 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1527 if (ret < 0)
7bfc837d 1528 goto out;
1fcbac58 1529 }
7bfc837d 1530
0b86a832
CM
1531 while (1) {
1532 l = path->nodes[0];
1533 slot = path->slots[0];
1534 if (slot >= btrfs_header_nritems(l)) {
1535 ret = btrfs_next_leaf(root, path);
1536 if (ret == 0)
1537 continue;
1538 if (ret < 0)
7bfc837d
MX
1539 goto out;
1540
1541 break;
0b86a832
CM
1542 }
1543 btrfs_item_key_to_cpu(l, &key, slot);
1544
1545 if (key.objectid < device->devid)
1546 goto next;
1547
1548 if (key.objectid > device->devid)
7bfc837d 1549 break;
0b86a832 1550
962a298f 1551 if (key.type != BTRFS_DEV_EXTENT_KEY)
7bfc837d 1552 goto next;
9779b72f 1553
7bfc837d
MX
1554 if (key.offset > search_start) {
1555 hole_size = key.offset - search_start;
3b4ffa40
NA
1556 dev_extent_hole_check(device, &search_start, &hole_size,
1557 num_bytes);
6df9a95e 1558
7bfc837d
MX
1559 if (hole_size > max_hole_size) {
1560 max_hole_start = search_start;
1561 max_hole_size = hole_size;
1562 }
9779b72f 1563
7bfc837d
MX
1564 /*
1565 * If this free space is greater than which we need,
1566 * it must be the max free space that we have found
1567 * until now, so max_hole_start must point to the start
1568 * of this free space and the length of this free space
1569 * is stored in max_hole_size. Thus, we return
1570 * max_hole_start and max_hole_size and go back to the
1571 * caller.
1572 */
1573 if (hole_size >= num_bytes) {
1574 ret = 0;
1575 goto out;
0b86a832
CM
1576 }
1577 }
0b86a832 1578
0b86a832 1579 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
7bfc837d
MX
1580 extent_end = key.offset + btrfs_dev_extent_length(l,
1581 dev_extent);
1582 if (extent_end > search_start)
1583 search_start = extent_end;
0b86a832
CM
1584next:
1585 path->slots[0]++;
1586 cond_resched();
1587 }
0b86a832 1588
38c01b96 1589 /*
1590 * At this point, search_start should be the end of
1591 * allocated dev extents, and when shrinking the device,
1592 * search_end may be smaller than search_start.
1593 */
f2ab7618 1594 if (search_end > search_start) {
38c01b96 1595 hole_size = search_end - search_start;
3b4ffa40
NA
1596 if (dev_extent_hole_check(device, &search_start, &hole_size,
1597 num_bytes)) {
f2ab7618
ZL
1598 btrfs_release_path(path);
1599 goto again;
1600 }
0b86a832 1601
f2ab7618
ZL
1602 if (hole_size > max_hole_size) {
1603 max_hole_start = search_start;
1604 max_hole_size = hole_size;
1605 }
6df9a95e
JB
1606 }
1607
7bfc837d 1608 /* See above. */
f2ab7618 1609 if (max_hole_size < num_bytes)
7bfc837d
MX
1610 ret = -ENOSPC;
1611 else
1612 ret = 0;
1613
1614out:
2b82032c 1615 btrfs_free_path(path);
7bfc837d 1616 *start = max_hole_start;
b2117a39 1617 if (len)
7bfc837d 1618 *len = max_hole_size;
0b86a832
CM
1619 return ret;
1620}
1621
60dfdf25 1622int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes,
499f377f
JM
1623 u64 *start, u64 *len)
1624{
499f377f 1625 /* FIXME use last free of some kind */
60dfdf25 1626 return find_free_dev_extent_start(device, num_bytes, 0, start, len);
499f377f
JM
1627}
1628
b2950863 1629static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
8f18cf13 1630 struct btrfs_device *device,
2196d6e8 1631 u64 start, u64 *dev_extent_len)
8f18cf13 1632{
0b246afa
JM
1633 struct btrfs_fs_info *fs_info = device->fs_info;
1634 struct btrfs_root *root = fs_info->dev_root;
8f18cf13
CM
1635 int ret;
1636 struct btrfs_path *path;
8f18cf13 1637 struct btrfs_key key;
a061fc8d
CM
1638 struct btrfs_key found_key;
1639 struct extent_buffer *leaf = NULL;
1640 struct btrfs_dev_extent *extent = NULL;
8f18cf13
CM
1641
1642 path = btrfs_alloc_path();
1643 if (!path)
1644 return -ENOMEM;
1645
1646 key.objectid = device->devid;
1647 key.offset = start;
1648 key.type = BTRFS_DEV_EXTENT_KEY;
924cd8fb 1649again:
8f18cf13 1650 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
a061fc8d
CM
1651 if (ret > 0) {
1652 ret = btrfs_previous_item(root, path, key.objectid,
1653 BTRFS_DEV_EXTENT_KEY);
b0b802d7
TI
1654 if (ret)
1655 goto out;
a061fc8d
CM
1656 leaf = path->nodes[0];
1657 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1658 extent = btrfs_item_ptr(leaf, path->slots[0],
1659 struct btrfs_dev_extent);
1660 BUG_ON(found_key.offset > start || found_key.offset +
1661 btrfs_dev_extent_length(leaf, extent) < start);
924cd8fb
MX
1662 key = found_key;
1663 btrfs_release_path(path);
1664 goto again;
a061fc8d
CM
1665 } else if (ret == 0) {
1666 leaf = path->nodes[0];
1667 extent = btrfs_item_ptr(leaf, path->slots[0],
1668 struct btrfs_dev_extent);
79787eaa 1669 } else {
0b246afa 1670 btrfs_handle_fs_error(fs_info, ret, "Slot search failed");
79787eaa 1671 goto out;
a061fc8d 1672 }
8f18cf13 1673
2196d6e8
MX
1674 *dev_extent_len = btrfs_dev_extent_length(leaf, extent);
1675
8f18cf13 1676 ret = btrfs_del_item(trans, root, path);
79787eaa 1677 if (ret) {
0b246afa
JM
1678 btrfs_handle_fs_error(fs_info, ret,
1679 "Failed to remove dev extent item");
13212b54 1680 } else {
3204d33c 1681 set_bit(BTRFS_TRANS_HAVE_FREE_BGS, &trans->transaction->flags);
79787eaa 1682 }
b0b802d7 1683out:
8f18cf13
CM
1684 btrfs_free_path(path);
1685 return ret;
1686}
1687
48a3b636
ES
1688static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1689 struct btrfs_device *device,
48a3b636 1690 u64 chunk_offset, u64 start, u64 num_bytes)
0b86a832
CM
1691{
1692 int ret;
1693 struct btrfs_path *path;
0b246afa
JM
1694 struct btrfs_fs_info *fs_info = device->fs_info;
1695 struct btrfs_root *root = fs_info->dev_root;
0b86a832
CM
1696 struct btrfs_dev_extent *extent;
1697 struct extent_buffer *leaf;
1698 struct btrfs_key key;
1699
e12c9621 1700 WARN_ON(!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state));
401e29c1 1701 WARN_ON(test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state));
0b86a832
CM
1702 path = btrfs_alloc_path();
1703 if (!path)
1704 return -ENOMEM;
1705
0b86a832 1706 key.objectid = device->devid;
2b82032c 1707 key.offset = start;
0b86a832
CM
1708 key.type = BTRFS_DEV_EXTENT_KEY;
1709 ret = btrfs_insert_empty_item(trans, root, path, &key,
1710 sizeof(*extent));
2cdcecbc
MF
1711 if (ret)
1712 goto out;
0b86a832
CM
1713
1714 leaf = path->nodes[0];
1715 extent = btrfs_item_ptr(leaf, path->slots[0],
1716 struct btrfs_dev_extent);
b5d9071c
NB
1717 btrfs_set_dev_extent_chunk_tree(leaf, extent,
1718 BTRFS_CHUNK_TREE_OBJECTID);
0ca00afb
NB
1719 btrfs_set_dev_extent_chunk_objectid(leaf, extent,
1720 BTRFS_FIRST_CHUNK_TREE_OBJECTID);
e17cade2
CM
1721 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1722
0b86a832
CM
1723 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1724 btrfs_mark_buffer_dirty(leaf);
2cdcecbc 1725out:
0b86a832
CM
1726 btrfs_free_path(path);
1727 return ret;
1728}
1729
6df9a95e 1730static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
0b86a832 1731{
6df9a95e
JB
1732 struct extent_map_tree *em_tree;
1733 struct extent_map *em;
1734 struct rb_node *n;
1735 u64 ret = 0;
0b86a832 1736
c8bf1b67 1737 em_tree = &fs_info->mapping_tree;
6df9a95e 1738 read_lock(&em_tree->lock);
07e1ce09 1739 n = rb_last(&em_tree->map.rb_root);
6df9a95e
JB
1740 if (n) {
1741 em = rb_entry(n, struct extent_map, rb_node);
1742 ret = em->start + em->len;
0b86a832 1743 }
6df9a95e
JB
1744 read_unlock(&em_tree->lock);
1745
0b86a832
CM
1746 return ret;
1747}
1748
53f10659
ID
1749static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
1750 u64 *devid_ret)
0b86a832
CM
1751{
1752 int ret;
1753 struct btrfs_key key;
1754 struct btrfs_key found_key;
2b82032c
YZ
1755 struct btrfs_path *path;
1756
2b82032c
YZ
1757 path = btrfs_alloc_path();
1758 if (!path)
1759 return -ENOMEM;
0b86a832
CM
1760
1761 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1762 key.type = BTRFS_DEV_ITEM_KEY;
1763 key.offset = (u64)-1;
1764
53f10659 1765 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
0b86a832
CM
1766 if (ret < 0)
1767 goto error;
1768
a06dee4d
AJ
1769 if (ret == 0) {
1770 /* Corruption */
1771 btrfs_err(fs_info, "corrupted chunk tree devid -1 matched");
1772 ret = -EUCLEAN;
1773 goto error;
1774 }
0b86a832 1775
53f10659
ID
1776 ret = btrfs_previous_item(fs_info->chunk_root, path,
1777 BTRFS_DEV_ITEMS_OBJECTID,
0b86a832
CM
1778 BTRFS_DEV_ITEM_KEY);
1779 if (ret) {
53f10659 1780 *devid_ret = 1;
0b86a832
CM
1781 } else {
1782 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1783 path->slots[0]);
53f10659 1784 *devid_ret = found_key.offset + 1;
0b86a832
CM
1785 }
1786 ret = 0;
1787error:
2b82032c 1788 btrfs_free_path(path);
0b86a832
CM
1789 return ret;
1790}
1791
1792/*
1793 * the device information is stored in the chunk root
1794 * the btrfs_device struct should be fully filled in
1795 */
c74a0b02 1796static int btrfs_add_dev_item(struct btrfs_trans_handle *trans,
48a3b636 1797 struct btrfs_device *device)
0b86a832
CM
1798{
1799 int ret;
1800 struct btrfs_path *path;
1801 struct btrfs_dev_item *dev_item;
1802 struct extent_buffer *leaf;
1803 struct btrfs_key key;
1804 unsigned long ptr;
0b86a832 1805
0b86a832
CM
1806 path = btrfs_alloc_path();
1807 if (!path)
1808 return -ENOMEM;
1809
0b86a832
CM
1810 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1811 key.type = BTRFS_DEV_ITEM_KEY;
2b82032c 1812 key.offset = device->devid;
0b86a832 1813
8e87e856
NB
1814 ret = btrfs_insert_empty_item(trans, trans->fs_info->chunk_root, path,
1815 &key, sizeof(*dev_item));
0b86a832
CM
1816 if (ret)
1817 goto out;
1818
1819 leaf = path->nodes[0];
1820 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1821
1822 btrfs_set_device_id(leaf, dev_item, device->devid);
2b82032c 1823 btrfs_set_device_generation(leaf, dev_item, 0);
0b86a832
CM
1824 btrfs_set_device_type(leaf, dev_item, device->type);
1825 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1826 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1827 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
7cc8e58d
MX
1828 btrfs_set_device_total_bytes(leaf, dev_item,
1829 btrfs_device_get_disk_total_bytes(device));
1830 btrfs_set_device_bytes_used(leaf, dev_item,
1831 btrfs_device_get_bytes_used(device));
e17cade2
CM
1832 btrfs_set_device_group(leaf, dev_item, 0);
1833 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1834 btrfs_set_device_bandwidth(leaf, dev_item, 0);
c3027eb5 1835 btrfs_set_device_start_offset(leaf, dev_item, 0);
0b86a832 1836
410ba3a2 1837 ptr = btrfs_device_uuid(dev_item);
e17cade2 1838 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
1473b24e 1839 ptr = btrfs_device_fsid(dev_item);
de37aa51
NB
1840 write_extent_buffer(leaf, trans->fs_info->fs_devices->metadata_uuid,
1841 ptr, BTRFS_FSID_SIZE);
0b86a832 1842 btrfs_mark_buffer_dirty(leaf);
0b86a832 1843
2b82032c 1844 ret = 0;
0b86a832
CM
1845out:
1846 btrfs_free_path(path);
1847 return ret;
1848}
8f18cf13 1849
5a1972bd
QW
1850/*
1851 * Function to update ctime/mtime for a given device path.
1852 * Mainly used for ctime/mtime based probe like libblkid.
1853 */
da353f6b 1854static void update_dev_time(const char *path_name)
5a1972bd
QW
1855{
1856 struct file *filp;
1857
1858 filp = filp_open(path_name, O_RDWR, 0);
98af592f 1859 if (IS_ERR(filp))
5a1972bd
QW
1860 return;
1861 file_update_time(filp);
1862 filp_close(filp, NULL);
5a1972bd
QW
1863}
1864
f331a952 1865static int btrfs_rm_dev_item(struct btrfs_device *device)
a061fc8d 1866{
f331a952 1867 struct btrfs_root *root = device->fs_info->chunk_root;
a061fc8d
CM
1868 int ret;
1869 struct btrfs_path *path;
a061fc8d 1870 struct btrfs_key key;
a061fc8d
CM
1871 struct btrfs_trans_handle *trans;
1872
a061fc8d
CM
1873 path = btrfs_alloc_path();
1874 if (!path)
1875 return -ENOMEM;
1876
a22285a6 1877 trans = btrfs_start_transaction(root, 0);
98d5dc13
TI
1878 if (IS_ERR(trans)) {
1879 btrfs_free_path(path);
1880 return PTR_ERR(trans);
1881 }
a061fc8d
CM
1882 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1883 key.type = BTRFS_DEV_ITEM_KEY;
1884 key.offset = device->devid;
1885
1886 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
5e9f2ad5
NB
1887 if (ret) {
1888 if (ret > 0)
1889 ret = -ENOENT;
1890 btrfs_abort_transaction(trans, ret);
1891 btrfs_end_transaction(trans);
a061fc8d
CM
1892 goto out;
1893 }
1894
1895 ret = btrfs_del_item(trans, root, path);
5e9f2ad5
NB
1896 if (ret) {
1897 btrfs_abort_transaction(trans, ret);
1898 btrfs_end_transaction(trans);
1899 }
1900
a061fc8d
CM
1901out:
1902 btrfs_free_path(path);
5e9f2ad5
NB
1903 if (!ret)
1904 ret = btrfs_commit_transaction(trans);
a061fc8d
CM
1905 return ret;
1906}
1907
3cc31a0d
DS
1908/*
1909 * Verify that @num_devices satisfies the RAID profile constraints in the whole
1910 * filesystem. It's up to the caller to adjust that number regarding eg. device
1911 * replace.
1912 */
1913static int btrfs_check_raid_min_devices(struct btrfs_fs_info *fs_info,
1914 u64 num_devices)
a061fc8d 1915{
a061fc8d 1916 u64 all_avail;
de98ced9 1917 unsigned seq;
418775a2 1918 int i;
a061fc8d 1919
de98ced9 1920 do {
bd45ffbc 1921 seq = read_seqbegin(&fs_info->profiles_lock);
de98ced9 1922
bd45ffbc
AJ
1923 all_avail = fs_info->avail_data_alloc_bits |
1924 fs_info->avail_system_alloc_bits |
1925 fs_info->avail_metadata_alloc_bits;
1926 } while (read_seqretry(&fs_info->profiles_lock, seq));
a061fc8d 1927
418775a2 1928 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
41a6e891 1929 if (!(all_avail & btrfs_raid_array[i].bg_flag))
418775a2 1930 continue;
a061fc8d 1931
418775a2 1932 if (num_devices < btrfs_raid_array[i].devs_min) {
f9fbcaa2 1933 int ret = btrfs_raid_array[i].mindev_error;
bd45ffbc 1934
418775a2
DS
1935 if (ret)
1936 return ret;
1937 }
53b381b3
DW
1938 }
1939
bd45ffbc 1940 return 0;
f1fa7f26
AJ
1941}
1942
c9162bdf
OS
1943static struct btrfs_device * btrfs_find_next_active_device(
1944 struct btrfs_fs_devices *fs_devs, struct btrfs_device *device)
a061fc8d 1945{
2b82032c 1946 struct btrfs_device *next_device;
88acff64
AJ
1947
1948 list_for_each_entry(next_device, &fs_devs->devices, dev_list) {
1949 if (next_device != device &&
e6e674bd
AJ
1950 !test_bit(BTRFS_DEV_STATE_MISSING, &next_device->dev_state)
1951 && next_device->bdev)
88acff64
AJ
1952 return next_device;
1953 }
1954
1955 return NULL;
1956}
1957
1958/*
1959 * Helper function to check if the given device is part of s_bdev / latest_bdev
1960 * and replace it with the provided or the next active device, in the context
1961 * where this function called, there should be always be another device (or
1962 * this_dev) which is active.
1963 */
b105e927 1964void __cold btrfs_assign_next_active_device(struct btrfs_device *device,
e493e8f9 1965 struct btrfs_device *next_device)
88acff64 1966{
d6507cf1 1967 struct btrfs_fs_info *fs_info = device->fs_info;
88acff64 1968
e493e8f9 1969 if (!next_device)
88acff64 1970 next_device = btrfs_find_next_active_device(fs_info->fs_devices,
e493e8f9 1971 device);
88acff64
AJ
1972 ASSERT(next_device);
1973
1974 if (fs_info->sb->s_bdev &&
1975 (fs_info->sb->s_bdev == device->bdev))
1976 fs_info->sb->s_bdev = next_device->bdev;
1977
1978 if (fs_info->fs_devices->latest_bdev == device->bdev)
1979 fs_info->fs_devices->latest_bdev = next_device->bdev;
1980}
1981
1da73967
AJ
1982/*
1983 * Return btrfs_fs_devices::num_devices excluding the device that's being
1984 * currently replaced.
1985 */
1986static u64 btrfs_num_devices(struct btrfs_fs_info *fs_info)
1987{
1988 u64 num_devices = fs_info->fs_devices->num_devices;
1989
cb5583dd 1990 down_read(&fs_info->dev_replace.rwsem);
1da73967
AJ
1991 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
1992 ASSERT(num_devices > 1);
1993 num_devices--;
1994 }
cb5583dd 1995 up_read(&fs_info->dev_replace.rwsem);
1da73967
AJ
1996
1997 return num_devices;
1998}
1999
313b0858
JB
2000void btrfs_scratch_superblocks(struct btrfs_fs_info *fs_info,
2001 struct block_device *bdev,
2002 const char *device_path)
6fbceb9f 2003{
6fbceb9f
JT
2004 struct btrfs_super_block *disk_super;
2005 int copy_num;
2006
2007 if (!bdev)
2008 return;
2009
2010 for (copy_num = 0; copy_num < BTRFS_SUPER_MIRROR_MAX; copy_num++) {
8f32380d
JT
2011 struct page *page;
2012 int ret;
6fbceb9f 2013
8f32380d
JT
2014 disk_super = btrfs_read_dev_one_super(bdev, copy_num);
2015 if (IS_ERR(disk_super))
2016 continue;
6fbceb9f
JT
2017
2018 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
8f32380d
JT
2019
2020 page = virt_to_page(disk_super);
2021 set_page_dirty(page);
2022 lock_page(page);
2023 /* write_on_page() unlocks the page */
2024 ret = write_one_page(page);
2025 if (ret)
2026 btrfs_warn(fs_info,
2027 "error clearing superblock number %d (%d)",
2028 copy_num, ret);
2029 btrfs_release_disk_super(disk_super);
2030
6fbceb9f
JT
2031 }
2032
2033 /* Notify udev that device has changed */
2034 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
2035
2036 /* Update ctime/mtime for device path for libblkid */
2037 update_dev_time(device_path);
2038}
2039
da353f6b 2040int btrfs_rm_device(struct btrfs_fs_info *fs_info, const char *device_path,
53f8a74c 2041 u64 devid)
f1fa7f26
AJ
2042{
2043 struct btrfs_device *device;
1f78160c 2044 struct btrfs_fs_devices *cur_devices;
b5185197 2045 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
2b82032c 2046 u64 num_devices;
a061fc8d
CM
2047 int ret = 0;
2048
a061fc8d
CM
2049 mutex_lock(&uuid_mutex);
2050
1da73967 2051 num_devices = btrfs_num_devices(fs_info);
8dabb742 2052
0b246afa 2053 ret = btrfs_check_raid_min_devices(fs_info, num_devices - 1);
f1fa7f26 2054 if (ret)
a061fc8d 2055 goto out;
a061fc8d 2056
a27a94c2
NB
2057 device = btrfs_find_device_by_devspec(fs_info, devid, device_path);
2058
2059 if (IS_ERR(device)) {
2060 if (PTR_ERR(device) == -ENOENT &&
2061 strcmp(device_path, "missing") == 0)
2062 ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
2063 else
2064 ret = PTR_ERR(device);
53b381b3 2065 goto out;
a27a94c2 2066 }
dfe25020 2067
eede2bf3
OS
2068 if (btrfs_pinned_by_swapfile(fs_info, device)) {
2069 btrfs_warn_in_rcu(fs_info,
2070 "cannot remove device %s (devid %llu) due to active swapfile",
2071 rcu_str_deref(device->name), device->devid);
2072 ret = -ETXTBSY;
2073 goto out;
2074 }
2075
401e29c1 2076 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
183860f6 2077 ret = BTRFS_ERROR_DEV_TGT_REPLACE;
24fc572f 2078 goto out;
63a212ab
SB
2079 }
2080
ebbede42
AJ
2081 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
2082 fs_info->fs_devices->rw_devices == 1) {
183860f6 2083 ret = BTRFS_ERROR_DEV_ONLY_WRITABLE;
24fc572f 2084 goto out;
2b82032c
YZ
2085 }
2086
ebbede42 2087 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
34441361 2088 mutex_lock(&fs_info->chunk_mutex);
2b82032c 2089 list_del_init(&device->dev_alloc_list);
c3929c36 2090 device->fs_devices->rw_devices--;
34441361 2091 mutex_unlock(&fs_info->chunk_mutex);
dfe25020 2092 }
a061fc8d 2093
d7901554 2094 mutex_unlock(&uuid_mutex);
a061fc8d 2095 ret = btrfs_shrink_device(device, 0);
d7901554 2096 mutex_lock(&uuid_mutex);
a061fc8d 2097 if (ret)
9b3517e9 2098 goto error_undo;
a061fc8d 2099
63a212ab
SB
2100 /*
2101 * TODO: the superblock still includes this device in its num_devices
2102 * counter although write_all_supers() is not locked out. This
2103 * could give a filesystem state which requires a degraded mount.
2104 */
f331a952 2105 ret = btrfs_rm_dev_item(device);
a061fc8d 2106 if (ret)
9b3517e9 2107 goto error_undo;
a061fc8d 2108
e12c9621 2109 clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
163e97ee 2110 btrfs_scrub_cancel_dev(device);
e5e9a520
CM
2111
2112 /*
2113 * the device list mutex makes sure that we don't change
2114 * the device list while someone else is writing out all
d7306801
FDBM
2115 * the device supers. Whoever is writing all supers, should
2116 * lock the device list mutex before getting the number of
2117 * devices in the super block (super_copy). Conversely,
2118 * whoever updates the number of devices in the super block
2119 * (super_copy) should hold the device list mutex.
e5e9a520 2120 */
1f78160c 2121
41a52a0f
AJ
2122 /*
2123 * In normal cases the cur_devices == fs_devices. But in case
2124 * of deleting a seed device, the cur_devices should point to
2125 * its own fs_devices listed under the fs_devices->seed.
2126 */
1f78160c 2127 cur_devices = device->fs_devices;
b5185197 2128 mutex_lock(&fs_devices->device_list_mutex);
1f78160c 2129 list_del_rcu(&device->dev_list);
e5e9a520 2130
41a52a0f
AJ
2131 cur_devices->num_devices--;
2132 cur_devices->total_devices--;
b4993e64
AJ
2133 /* Update total_devices of the parent fs_devices if it's seed */
2134 if (cur_devices != fs_devices)
2135 fs_devices->total_devices--;
2b82032c 2136
e6e674bd 2137 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
41a52a0f 2138 cur_devices->missing_devices--;
cd02dca5 2139
d6507cf1 2140 btrfs_assign_next_active_device(device, NULL);
2b82032c 2141
0bfaa9c5 2142 if (device->bdev) {
41a52a0f 2143 cur_devices->open_devices--;
0bfaa9c5 2144 /* remove sysfs entry */
53f8a74c 2145 btrfs_sysfs_remove_device(device);
0bfaa9c5 2146 }
99994cde 2147
0b246afa
JM
2148 num_devices = btrfs_super_num_devices(fs_info->super_copy) - 1;
2149 btrfs_set_super_num_devices(fs_info->super_copy, num_devices);
b5185197 2150 mutex_unlock(&fs_devices->device_list_mutex);
2b82032c 2151
cea67ab9
JM
2152 /*
2153 * at this point, the device is zero sized and detached from
2154 * the devices list. All that's left is to zero out the old
2155 * supers and free the device.
2156 */
ebbede42 2157 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
8f32380d
JT
2158 btrfs_scratch_superblocks(fs_info, device->bdev,
2159 device->name->str);
cea67ab9
JM
2160
2161 btrfs_close_bdev(device);
8e75fd89
NB
2162 synchronize_rcu();
2163 btrfs_free_device(device);
cea67ab9 2164
1f78160c 2165 if (cur_devices->open_devices == 0) {
944d3f9f 2166 list_del_init(&cur_devices->seed_list);
0226e0eb 2167 close_fs_devices(cur_devices);
1f78160c 2168 free_fs_devices(cur_devices);
2b82032c
YZ
2169 }
2170
a061fc8d
CM
2171out:
2172 mutex_unlock(&uuid_mutex);
a061fc8d 2173 return ret;
24fc572f 2174
9b3517e9 2175error_undo:
ebbede42 2176 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
34441361 2177 mutex_lock(&fs_info->chunk_mutex);
9b3517e9 2178 list_add(&device->dev_alloc_list,
b5185197 2179 &fs_devices->alloc_list);
c3929c36 2180 device->fs_devices->rw_devices++;
34441361 2181 mutex_unlock(&fs_info->chunk_mutex);
9b3517e9 2182 }
24fc572f 2183 goto out;
a061fc8d
CM
2184}
2185
68a9db5f 2186void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_device *srcdev)
e93c89c1 2187{
d51908ce
AJ
2188 struct btrfs_fs_devices *fs_devices;
2189
68a9db5f 2190 lockdep_assert_held(&srcdev->fs_info->fs_devices->device_list_mutex);
1357272f 2191
25e8e911
AJ
2192 /*
2193 * in case of fs with no seed, srcdev->fs_devices will point
2194 * to fs_devices of fs_info. However when the dev being replaced is
2195 * a seed dev it will point to the seed's local fs_devices. In short
2196 * srcdev will have its correct fs_devices in both the cases.
2197 */
2198 fs_devices = srcdev->fs_devices;
d51908ce 2199
e93c89c1 2200 list_del_rcu(&srcdev->dev_list);
619c47f3 2201 list_del(&srcdev->dev_alloc_list);
d51908ce 2202 fs_devices->num_devices--;
e6e674bd 2203 if (test_bit(BTRFS_DEV_STATE_MISSING, &srcdev->dev_state))
d51908ce 2204 fs_devices->missing_devices--;
e93c89c1 2205
ebbede42 2206 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state))
82372bc8 2207 fs_devices->rw_devices--;
1357272f 2208
82372bc8 2209 if (srcdev->bdev)
d51908ce 2210 fs_devices->open_devices--;
084b6e7c
QW
2211}
2212
65237ee3 2213void btrfs_rm_dev_replace_free_srcdev(struct btrfs_device *srcdev)
084b6e7c
QW
2214{
2215 struct btrfs_fs_devices *fs_devices = srcdev->fs_devices;
e93c89c1 2216
a466c85e
JB
2217 mutex_lock(&uuid_mutex);
2218
14238819 2219 btrfs_close_bdev(srcdev);
8e75fd89
NB
2220 synchronize_rcu();
2221 btrfs_free_device(srcdev);
94d5f0c2 2222
94d5f0c2
AJ
2223 /* if this is no devs we rather delete the fs_devices */
2224 if (!fs_devices->num_devices) {
6dd38f81
AJ
2225 /*
2226 * On a mounted FS, num_devices can't be zero unless it's a
2227 * seed. In case of a seed device being replaced, the replace
2228 * target added to the sprout FS, so there will be no more
2229 * device left under the seed FS.
2230 */
2231 ASSERT(fs_devices->seeding);
2232
944d3f9f 2233 list_del_init(&fs_devices->seed_list);
0226e0eb 2234 close_fs_devices(fs_devices);
8bef8401 2235 free_fs_devices(fs_devices);
94d5f0c2 2236 }
a466c85e 2237 mutex_unlock(&uuid_mutex);
e93c89c1
SB
2238}
2239
4f5ad7bd 2240void btrfs_destroy_dev_replace_tgtdev(struct btrfs_device *tgtdev)
e93c89c1 2241{
4f5ad7bd 2242 struct btrfs_fs_devices *fs_devices = tgtdev->fs_info->fs_devices;
d9a071f0 2243
d9a071f0 2244 mutex_lock(&fs_devices->device_list_mutex);
d2ff1b20 2245
53f8a74c 2246 btrfs_sysfs_remove_device(tgtdev);
d2ff1b20 2247
779bf3fe 2248 if (tgtdev->bdev)
d9a071f0 2249 fs_devices->open_devices--;
779bf3fe 2250
d9a071f0 2251 fs_devices->num_devices--;
e93c89c1 2252
d6507cf1 2253 btrfs_assign_next_active_device(tgtdev, NULL);
e93c89c1 2254
e93c89c1 2255 list_del_rcu(&tgtdev->dev_list);
e93c89c1 2256
d9a071f0 2257 mutex_unlock(&fs_devices->device_list_mutex);
779bf3fe
AJ
2258
2259 /*
2260 * The update_dev_time() with in btrfs_scratch_superblocks()
2261 * may lead to a call to btrfs_show_devname() which will try
2262 * to hold device_list_mutex. And here this device
2263 * is already out of device list, so we don't have to hold
2264 * the device_list_mutex lock.
2265 */
8f32380d
JT
2266 btrfs_scratch_superblocks(tgtdev->fs_info, tgtdev->bdev,
2267 tgtdev->name->str);
14238819
AJ
2268
2269 btrfs_close_bdev(tgtdev);
8e75fd89
NB
2270 synchronize_rcu();
2271 btrfs_free_device(tgtdev);
e93c89c1
SB
2272}
2273
b444ad46
NB
2274static struct btrfs_device *btrfs_find_device_by_path(
2275 struct btrfs_fs_info *fs_info, const char *device_path)
7ba15b7d
SB
2276{
2277 int ret = 0;
2278 struct btrfs_super_block *disk_super;
2279 u64 devid;
2280 u8 *dev_uuid;
2281 struct block_device *bdev;
b444ad46 2282 struct btrfs_device *device;
7ba15b7d 2283
7ba15b7d 2284 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
8f32380d 2285 fs_info->bdev_holder, 0, &bdev, &disk_super);
7ba15b7d 2286 if (ret)
b444ad46 2287 return ERR_PTR(ret);
8f32380d 2288
7ba15b7d
SB
2289 devid = btrfs_stack_device_id(&disk_super->dev_item);
2290 dev_uuid = disk_super->dev_item.uuid;
7239ff4b 2291 if (btrfs_fs_incompat(fs_info, METADATA_UUID))
e4319cd9 2292 device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
09ba3bc9 2293 disk_super->metadata_uuid, true);
7239ff4b 2294 else
e4319cd9 2295 device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
09ba3bc9 2296 disk_super->fsid, true);
7239ff4b 2297
8f32380d 2298 btrfs_release_disk_super(disk_super);
b444ad46
NB
2299 if (!device)
2300 device = ERR_PTR(-ENOENT);
7ba15b7d 2301 blkdev_put(bdev, FMODE_READ);
b444ad46 2302 return device;
7ba15b7d
SB
2303}
2304
5c5c0df0
DS
2305/*
2306 * Lookup a device given by device id, or the path if the id is 0.
2307 */
a27a94c2 2308struct btrfs_device *btrfs_find_device_by_devspec(
6e927ceb
AJ
2309 struct btrfs_fs_info *fs_info, u64 devid,
2310 const char *device_path)
24e0474b 2311{
a27a94c2 2312 struct btrfs_device *device;
24e0474b 2313
5c5c0df0 2314 if (devid) {
e4319cd9 2315 device = btrfs_find_device(fs_info->fs_devices, devid, NULL,
09ba3bc9 2316 NULL, true);
a27a94c2
NB
2317 if (!device)
2318 return ERR_PTR(-ENOENT);
6e927ceb
AJ
2319 return device;
2320 }
2321
2322 if (!device_path || !device_path[0])
2323 return ERR_PTR(-EINVAL);
2324
2325 if (strcmp(device_path, "missing") == 0) {
2326 /* Find first missing device */
2327 list_for_each_entry(device, &fs_info->fs_devices->devices,
2328 dev_list) {
2329 if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
2330 &device->dev_state) && !device->bdev)
2331 return device;
d95a830c 2332 }
6e927ceb 2333 return ERR_PTR(-ENOENT);
24e0474b 2334 }
6e927ceb
AJ
2335
2336 return btrfs_find_device_by_path(fs_info, device_path);
24e0474b
AJ
2337}
2338
2b82032c
YZ
2339/*
2340 * does all the dirty work required for changing file system's UUID.
2341 */
2ff7e61e 2342static int btrfs_prepare_sprout(struct btrfs_fs_info *fs_info)
2b82032c 2343{
0b246afa 2344 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
2b82032c 2345 struct btrfs_fs_devices *old_devices;
e4404d6e 2346 struct btrfs_fs_devices *seed_devices;
0b246afa 2347 struct btrfs_super_block *disk_super = fs_info->super_copy;
2b82032c
YZ
2348 struct btrfs_device *device;
2349 u64 super_flags;
2350
a32bf9a3 2351 lockdep_assert_held(&uuid_mutex);
e4404d6e 2352 if (!fs_devices->seeding)
2b82032c
YZ
2353 return -EINVAL;
2354
427c8fdd
NB
2355 /*
2356 * Private copy of the seed devices, anchored at
2357 * fs_info->fs_devices->seed_list
2358 */
7239ff4b 2359 seed_devices = alloc_fs_devices(NULL, NULL);
2208a378
ID
2360 if (IS_ERR(seed_devices))
2361 return PTR_ERR(seed_devices);
2b82032c 2362
427c8fdd
NB
2363 /*
2364 * It's necessary to retain a copy of the original seed fs_devices in
2365 * fs_uuids so that filesystems which have been seeded can successfully
2366 * reference the seed device from open_seed_devices. This also supports
2367 * multiple fs seed.
2368 */
e4404d6e
YZ
2369 old_devices = clone_fs_devices(fs_devices);
2370 if (IS_ERR(old_devices)) {
2371 kfree(seed_devices);
2372 return PTR_ERR(old_devices);
2b82032c 2373 }
e4404d6e 2374
c4babc5e 2375 list_add(&old_devices->fs_list, &fs_uuids);
2b82032c 2376
e4404d6e
YZ
2377 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
2378 seed_devices->opened = 1;
2379 INIT_LIST_HEAD(&seed_devices->devices);
2380 INIT_LIST_HEAD(&seed_devices->alloc_list);
e5e9a520 2381 mutex_init(&seed_devices->device_list_mutex);
c9513edb 2382
321a4bf7 2383 mutex_lock(&fs_devices->device_list_mutex);
1f78160c
XG
2384 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
2385 synchronize_rcu);
2196d6e8
MX
2386 list_for_each_entry(device, &seed_devices->devices, dev_list)
2387 device->fs_devices = seed_devices;
c9513edb 2388
0395d84f 2389 fs_devices->seeding = false;
2b82032c
YZ
2390 fs_devices->num_devices = 0;
2391 fs_devices->open_devices = 0;
69611ac8 2392 fs_devices->missing_devices = 0;
7f0432d0 2393 fs_devices->rotating = false;
944d3f9f 2394 list_add(&seed_devices->seed_list, &fs_devices->seed_list);
2b82032c
YZ
2395
2396 generate_random_uuid(fs_devices->fsid);
7239ff4b 2397 memcpy(fs_devices->metadata_uuid, fs_devices->fsid, BTRFS_FSID_SIZE);
2b82032c 2398 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
321a4bf7 2399 mutex_unlock(&fs_devices->device_list_mutex);
f7171750 2400
2b82032c
YZ
2401 super_flags = btrfs_super_flags(disk_super) &
2402 ~BTRFS_SUPER_FLAG_SEEDING;
2403 btrfs_set_super_flags(disk_super, super_flags);
2404
2405 return 0;
2406}
2407
2408/*
01327610 2409 * Store the expected generation for seed devices in device items.
2b82032c 2410 */
5c466629 2411static int btrfs_finish_sprout(struct btrfs_trans_handle *trans)
2b82032c 2412{
5c466629 2413 struct btrfs_fs_info *fs_info = trans->fs_info;
5b4aacef 2414 struct btrfs_root *root = fs_info->chunk_root;
2b82032c
YZ
2415 struct btrfs_path *path;
2416 struct extent_buffer *leaf;
2417 struct btrfs_dev_item *dev_item;
2418 struct btrfs_device *device;
2419 struct btrfs_key key;
44880fdc 2420 u8 fs_uuid[BTRFS_FSID_SIZE];
2b82032c
YZ
2421 u8 dev_uuid[BTRFS_UUID_SIZE];
2422 u64 devid;
2423 int ret;
2424
2425 path = btrfs_alloc_path();
2426 if (!path)
2427 return -ENOMEM;
2428
2b82032c
YZ
2429 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2430 key.offset = 0;
2431 key.type = BTRFS_DEV_ITEM_KEY;
2432
2433 while (1) {
2434 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2435 if (ret < 0)
2436 goto error;
2437
2438 leaf = path->nodes[0];
2439next_slot:
2440 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
2441 ret = btrfs_next_leaf(root, path);
2442 if (ret > 0)
2443 break;
2444 if (ret < 0)
2445 goto error;
2446 leaf = path->nodes[0];
2447 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
b3b4aa74 2448 btrfs_release_path(path);
2b82032c
YZ
2449 continue;
2450 }
2451
2452 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2453 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
2454 key.type != BTRFS_DEV_ITEM_KEY)
2455 break;
2456
2457 dev_item = btrfs_item_ptr(leaf, path->slots[0],
2458 struct btrfs_dev_item);
2459 devid = btrfs_device_id(leaf, dev_item);
410ba3a2 2460 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
2b82032c 2461 BTRFS_UUID_SIZE);
1473b24e 2462 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
44880fdc 2463 BTRFS_FSID_SIZE);
e4319cd9 2464 device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
09ba3bc9 2465 fs_uuid, true);
79787eaa 2466 BUG_ON(!device); /* Logic error */
2b82032c
YZ
2467
2468 if (device->fs_devices->seeding) {
2469 btrfs_set_device_generation(leaf, dev_item,
2470 device->generation);
2471 btrfs_mark_buffer_dirty(leaf);
2472 }
2473
2474 path->slots[0]++;
2475 goto next_slot;
2476 }
2477 ret = 0;
2478error:
2479 btrfs_free_path(path);
2480 return ret;
2481}
2482
da353f6b 2483int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path)
788f20eb 2484{
5112febb 2485 struct btrfs_root *root = fs_info->dev_root;
d5e2003c 2486 struct request_queue *q;
788f20eb
CM
2487 struct btrfs_trans_handle *trans;
2488 struct btrfs_device *device;
2489 struct block_device *bdev;
0b246afa 2490 struct super_block *sb = fs_info->sb;
606686ee 2491 struct rcu_string *name;
5da54bc1 2492 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
39379faa
NA
2493 u64 orig_super_total_bytes;
2494 u64 orig_super_num_devices;
2b82032c 2495 int seeding_dev = 0;
788f20eb 2496 int ret = 0;
44cab9ba 2497 bool locked = false;
788f20eb 2498
5da54bc1 2499 if (sb_rdonly(sb) && !fs_devices->seeding)
f8c5d0b4 2500 return -EROFS;
788f20eb 2501
a5d16333 2502 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
0b246afa 2503 fs_info->bdev_holder);
7f59203a
JB
2504 if (IS_ERR(bdev))
2505 return PTR_ERR(bdev);
a2135011 2506
5da54bc1 2507 if (fs_devices->seeding) {
2b82032c
YZ
2508 seeding_dev = 1;
2509 down_write(&sb->s_umount);
2510 mutex_lock(&uuid_mutex);
44cab9ba 2511 locked = true;
2b82032c
YZ
2512 }
2513
b9ba017f 2514 sync_blockdev(bdev);
a2135011 2515
f4cfa9bd
NB
2516 rcu_read_lock();
2517 list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
788f20eb
CM
2518 if (device->bdev == bdev) {
2519 ret = -EEXIST;
f4cfa9bd 2520 rcu_read_unlock();
2b82032c 2521 goto error;
788f20eb
CM
2522 }
2523 }
f4cfa9bd 2524 rcu_read_unlock();
788f20eb 2525
0b246afa 2526 device = btrfs_alloc_device(fs_info, NULL, NULL);
12bd2fc0 2527 if (IS_ERR(device)) {
788f20eb 2528 /* we can safely leave the fs_devices entry around */
12bd2fc0 2529 ret = PTR_ERR(device);
2b82032c 2530 goto error;
788f20eb
CM
2531 }
2532
78f2c9e6 2533 name = rcu_string_strdup(device_path, GFP_KERNEL);
606686ee 2534 if (!name) {
2b82032c 2535 ret = -ENOMEM;
5c4cf6c9 2536 goto error_free_device;
788f20eb 2537 }
606686ee 2538 rcu_assign_pointer(device->name, name);
2b82032c 2539
a22285a6 2540 trans = btrfs_start_transaction(root, 0);
98d5dc13 2541 if (IS_ERR(trans)) {
98d5dc13 2542 ret = PTR_ERR(trans);
5c4cf6c9 2543 goto error_free_device;
98d5dc13
TI
2544 }
2545
d5e2003c 2546 q = bdev_get_queue(bdev);
ebbede42 2547 set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
2b82032c 2548 device->generation = trans->transid;
0b246afa
JM
2549 device->io_width = fs_info->sectorsize;
2550 device->io_align = fs_info->sectorsize;
2551 device->sector_size = fs_info->sectorsize;
7dfb8be1
NB
2552 device->total_bytes = round_down(i_size_read(bdev->bd_inode),
2553 fs_info->sectorsize);
2cc3c559 2554 device->disk_total_bytes = device->total_bytes;
935e5cc9 2555 device->commit_total_bytes = device->total_bytes;
fb456252 2556 device->fs_info = fs_info;
788f20eb 2557 device->bdev = bdev;
e12c9621 2558 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
401e29c1 2559 clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
fb01aa85 2560 device->mode = FMODE_EXCL;
27087f37 2561 device->dev_stats_valid = 1;
9f6d2510 2562 set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE);
788f20eb 2563
2b82032c 2564 if (seeding_dev) {
1751e8a6 2565 sb->s_flags &= ~SB_RDONLY;
2ff7e61e 2566 ret = btrfs_prepare_sprout(fs_info);
d31c32f6
AJ
2567 if (ret) {
2568 btrfs_abort_transaction(trans, ret);
2569 goto error_trans;
2570 }
2b82032c 2571 }
788f20eb 2572
5da54bc1 2573 device->fs_devices = fs_devices;
e5e9a520 2574
5da54bc1 2575 mutex_lock(&fs_devices->device_list_mutex);
34441361 2576 mutex_lock(&fs_info->chunk_mutex);
5da54bc1
AJ
2577 list_add_rcu(&device->dev_list, &fs_devices->devices);
2578 list_add(&device->dev_alloc_list, &fs_devices->alloc_list);
2579 fs_devices->num_devices++;
2580 fs_devices->open_devices++;
2581 fs_devices->rw_devices++;
2582 fs_devices->total_devices++;
2583 fs_devices->total_rw_bytes += device->total_bytes;
325cd4ba 2584
a5ed45f8 2585 atomic64_add(device->total_bytes, &fs_info->free_chunk_space);
2bf64758 2586
e884f4f0 2587 if (!blk_queue_nonrot(q))
7f0432d0 2588 fs_devices->rotating = true;
c289811c 2589
39379faa 2590 orig_super_total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
0b246afa 2591 btrfs_set_super_total_bytes(fs_info->super_copy,
39379faa
NA
2592 round_down(orig_super_total_bytes + device->total_bytes,
2593 fs_info->sectorsize));
788f20eb 2594
39379faa
NA
2595 orig_super_num_devices = btrfs_super_num_devices(fs_info->super_copy);
2596 btrfs_set_super_num_devices(fs_info->super_copy,
2597 orig_super_num_devices + 1);
0d39376a 2598
2196d6e8
MX
2599 /*
2600 * we've got more storage, clear any full flags on the space
2601 * infos
2602 */
0b246afa 2603 btrfs_clear_space_info_full(fs_info);
2196d6e8 2604
34441361 2605 mutex_unlock(&fs_info->chunk_mutex);
ca10845a
JB
2606
2607 /* Add sysfs device entry */
cd36da2e 2608 btrfs_sysfs_add_device(device);
ca10845a 2609
5da54bc1 2610 mutex_unlock(&fs_devices->device_list_mutex);
788f20eb 2611
2b82032c 2612 if (seeding_dev) {
34441361 2613 mutex_lock(&fs_info->chunk_mutex);
6f8e0fc7 2614 ret = init_first_rw_device(trans);
34441361 2615 mutex_unlock(&fs_info->chunk_mutex);
005d6427 2616 if (ret) {
66642832 2617 btrfs_abort_transaction(trans, ret);
d31c32f6 2618 goto error_sysfs;
005d6427 2619 }
2196d6e8
MX
2620 }
2621
8e87e856 2622 ret = btrfs_add_dev_item(trans, device);
2196d6e8 2623 if (ret) {
66642832 2624 btrfs_abort_transaction(trans, ret);
d31c32f6 2625 goto error_sysfs;
2196d6e8
MX
2626 }
2627
2628 if (seeding_dev) {
5c466629 2629 ret = btrfs_finish_sprout(trans);
005d6427 2630 if (ret) {
66642832 2631 btrfs_abort_transaction(trans, ret);
d31c32f6 2632 goto error_sysfs;
005d6427 2633 }
b2373f25 2634
8e560081
NB
2635 /*
2636 * fs_devices now represents the newly sprouted filesystem and
2637 * its fsid has been changed by btrfs_prepare_sprout
2638 */
2639 btrfs_sysfs_update_sprout_fsid(fs_devices);
2b82032c
YZ
2640 }
2641
3a45bb20 2642 ret = btrfs_commit_transaction(trans);
a2135011 2643
2b82032c
YZ
2644 if (seeding_dev) {
2645 mutex_unlock(&uuid_mutex);
2646 up_write(&sb->s_umount);
44cab9ba 2647 locked = false;
788f20eb 2648
79787eaa
JM
2649 if (ret) /* transaction commit */
2650 return ret;
2651
2ff7e61e 2652 ret = btrfs_relocate_sys_chunks(fs_info);
79787eaa 2653 if (ret < 0)
0b246afa 2654 btrfs_handle_fs_error(fs_info, ret,
5d163e0e 2655 "Failed to relocate sys chunks after device initialization. This can be fixed using the \"btrfs balance\" command.");
671415b7
MX
2656 trans = btrfs_attach_transaction(root);
2657 if (IS_ERR(trans)) {
2658 if (PTR_ERR(trans) == -ENOENT)
2659 return 0;
7132a262
AJ
2660 ret = PTR_ERR(trans);
2661 trans = NULL;
2662 goto error_sysfs;
671415b7 2663 }
3a45bb20 2664 ret = btrfs_commit_transaction(trans);
2b82032c 2665 }
c9e9f97b 2666
7f551d96
AJ
2667 /*
2668 * Now that we have written a new super block to this device, check all
2669 * other fs_devices list if device_path alienates any other scanned
2670 * device.
2671 * We can ignore the return value as it typically returns -EINVAL and
2672 * only succeeds if the device was an alien.
2673 */
2674 btrfs_forget_devices(device_path);
2675
2676 /* Update ctime/mtime for blkid or udev */
5a1972bd 2677 update_dev_time(device_path);
7f551d96 2678
2b82032c 2679 return ret;
79787eaa 2680
d31c32f6 2681error_sysfs:
53f8a74c 2682 btrfs_sysfs_remove_device(device);
39379faa
NA
2683 mutex_lock(&fs_info->fs_devices->device_list_mutex);
2684 mutex_lock(&fs_info->chunk_mutex);
2685 list_del_rcu(&device->dev_list);
2686 list_del(&device->dev_alloc_list);
2687 fs_info->fs_devices->num_devices--;
2688 fs_info->fs_devices->open_devices--;
2689 fs_info->fs_devices->rw_devices--;
2690 fs_info->fs_devices->total_devices--;
2691 fs_info->fs_devices->total_rw_bytes -= device->total_bytes;
2692 atomic64_sub(device->total_bytes, &fs_info->free_chunk_space);
2693 btrfs_set_super_total_bytes(fs_info->super_copy,
2694 orig_super_total_bytes);
2695 btrfs_set_super_num_devices(fs_info->super_copy,
2696 orig_super_num_devices);
2697 mutex_unlock(&fs_info->chunk_mutex);
2698 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
79787eaa 2699error_trans:
0af2c4bf 2700 if (seeding_dev)
1751e8a6 2701 sb->s_flags |= SB_RDONLY;
7132a262
AJ
2702 if (trans)
2703 btrfs_end_transaction(trans);
5c4cf6c9 2704error_free_device:
a425f9d4 2705 btrfs_free_device(device);
2b82032c 2706error:
e525fd89 2707 blkdev_put(bdev, FMODE_EXCL);
44cab9ba 2708 if (locked) {
2b82032c
YZ
2709 mutex_unlock(&uuid_mutex);
2710 up_write(&sb->s_umount);
2711 }
c9e9f97b 2712 return ret;
788f20eb
CM
2713}
2714
d397712b
CM
2715static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2716 struct btrfs_device *device)
0b86a832
CM
2717{
2718 int ret;
2719 struct btrfs_path *path;
0b246afa 2720 struct btrfs_root *root = device->fs_info->chunk_root;
0b86a832
CM
2721 struct btrfs_dev_item *dev_item;
2722 struct extent_buffer *leaf;
2723 struct btrfs_key key;
2724
0b86a832
CM
2725 path = btrfs_alloc_path();
2726 if (!path)
2727 return -ENOMEM;
2728
2729 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2730 key.type = BTRFS_DEV_ITEM_KEY;
2731 key.offset = device->devid;
2732
2733 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2734 if (ret < 0)
2735 goto out;
2736
2737 if (ret > 0) {
2738 ret = -ENOENT;
2739 goto out;
2740 }
2741
2742 leaf = path->nodes[0];
2743 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2744
2745 btrfs_set_device_id(leaf, dev_item, device->devid);
2746 btrfs_set_device_type(leaf, dev_item, device->type);
2747 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2748 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2749 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
7cc8e58d
MX
2750 btrfs_set_device_total_bytes(leaf, dev_item,
2751 btrfs_device_get_disk_total_bytes(device));
2752 btrfs_set_device_bytes_used(leaf, dev_item,
2753 btrfs_device_get_bytes_used(device));
0b86a832
CM
2754 btrfs_mark_buffer_dirty(leaf);
2755
2756out:
2757 btrfs_free_path(path);
2758 return ret;
2759}
2760
2196d6e8 2761int btrfs_grow_device(struct btrfs_trans_handle *trans,
8f18cf13
CM
2762 struct btrfs_device *device, u64 new_size)
2763{
0b246afa
JM
2764 struct btrfs_fs_info *fs_info = device->fs_info;
2765 struct btrfs_super_block *super_copy = fs_info->super_copy;
2196d6e8
MX
2766 u64 old_total;
2767 u64 diff;
8f18cf13 2768
ebbede42 2769 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
2b82032c 2770 return -EACCES;
2196d6e8 2771
7dfb8be1
NB
2772 new_size = round_down(new_size, fs_info->sectorsize);
2773
34441361 2774 mutex_lock(&fs_info->chunk_mutex);
2196d6e8 2775 old_total = btrfs_super_total_bytes(super_copy);
0e4324a4 2776 diff = round_down(new_size - device->total_bytes, fs_info->sectorsize);
2196d6e8 2777
63a212ab 2778 if (new_size <= device->total_bytes ||
401e29c1 2779 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
34441361 2780 mutex_unlock(&fs_info->chunk_mutex);
2b82032c 2781 return -EINVAL;
2196d6e8 2782 }
2b82032c 2783
7dfb8be1
NB
2784 btrfs_set_super_total_bytes(super_copy,
2785 round_down(old_total + diff, fs_info->sectorsize));
2b82032c
YZ
2786 device->fs_devices->total_rw_bytes += diff;
2787
7cc8e58d
MX
2788 btrfs_device_set_total_bytes(device, new_size);
2789 btrfs_device_set_disk_total_bytes(device, new_size);
fb456252 2790 btrfs_clear_space_info_full(device->fs_info);
bbbf7243
NB
2791 if (list_empty(&device->post_commit_list))
2792 list_add_tail(&device->post_commit_list,
2793 &trans->transaction->dev_update_list);
34441361 2794 mutex_unlock(&fs_info->chunk_mutex);
4184ea7f 2795
8f18cf13
CM
2796 return btrfs_update_device(trans, device);
2797}
2798
f4208794 2799static int btrfs_free_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset)
8f18cf13 2800{
f4208794 2801 struct btrfs_fs_info *fs_info = trans->fs_info;
5b4aacef 2802 struct btrfs_root *root = fs_info->chunk_root;
8f18cf13
CM
2803 int ret;
2804 struct btrfs_path *path;
2805 struct btrfs_key key;
2806
8f18cf13
CM
2807 path = btrfs_alloc_path();
2808 if (!path)
2809 return -ENOMEM;
2810
408fbf19 2811 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
8f18cf13
CM
2812 key.offset = chunk_offset;
2813 key.type = BTRFS_CHUNK_ITEM_KEY;
2814
2815 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
79787eaa
JM
2816 if (ret < 0)
2817 goto out;
2818 else if (ret > 0) { /* Logic error or corruption */
0b246afa
JM
2819 btrfs_handle_fs_error(fs_info, -ENOENT,
2820 "Failed lookup while freeing chunk.");
79787eaa
JM
2821 ret = -ENOENT;
2822 goto out;
2823 }
8f18cf13
CM
2824
2825 ret = btrfs_del_item(trans, root, path);
79787eaa 2826 if (ret < 0)
0b246afa
JM
2827 btrfs_handle_fs_error(fs_info, ret,
2828 "Failed to delete chunk item.");
79787eaa 2829out:
8f18cf13 2830 btrfs_free_path(path);
65a246c5 2831 return ret;
8f18cf13
CM
2832}
2833
408fbf19 2834static int btrfs_del_sys_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
8f18cf13 2835{
0b246afa 2836 struct btrfs_super_block *super_copy = fs_info->super_copy;
8f18cf13
CM
2837 struct btrfs_disk_key *disk_key;
2838 struct btrfs_chunk *chunk;
2839 u8 *ptr;
2840 int ret = 0;
2841 u32 num_stripes;
2842 u32 array_size;
2843 u32 len = 0;
2844 u32 cur;
2845 struct btrfs_key key;
2846
34441361 2847 mutex_lock(&fs_info->chunk_mutex);
8f18cf13
CM
2848 array_size = btrfs_super_sys_array_size(super_copy);
2849
2850 ptr = super_copy->sys_chunk_array;
2851 cur = 0;
2852
2853 while (cur < array_size) {
2854 disk_key = (struct btrfs_disk_key *)ptr;
2855 btrfs_disk_key_to_cpu(&key, disk_key);
2856
2857 len = sizeof(*disk_key);
2858
2859 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2860 chunk = (struct btrfs_chunk *)(ptr + len);
2861 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2862 len += btrfs_chunk_item_size(num_stripes);
2863 } else {
2864 ret = -EIO;
2865 break;
2866 }
408fbf19 2867 if (key.objectid == BTRFS_FIRST_CHUNK_TREE_OBJECTID &&
8f18cf13
CM
2868 key.offset == chunk_offset) {
2869 memmove(ptr, ptr + len, array_size - (cur + len));
2870 array_size -= len;
2871 btrfs_set_super_sys_array_size(super_copy, array_size);
2872 } else {
2873 ptr += len;
2874 cur += len;
2875 }
2876 }
34441361 2877 mutex_unlock(&fs_info->chunk_mutex);
8f18cf13
CM
2878 return ret;
2879}
2880
60ca842e
OS
2881/*
2882 * btrfs_get_chunk_map() - Find the mapping containing the given logical extent.
2883 * @logical: Logical block offset in bytes.
2884 * @length: Length of extent in bytes.
2885 *
2886 * Return: Chunk mapping or ERR_PTR.
2887 */
2888struct extent_map *btrfs_get_chunk_map(struct btrfs_fs_info *fs_info,
2889 u64 logical, u64 length)
592d92ee
LB
2890{
2891 struct extent_map_tree *em_tree;
2892 struct extent_map *em;
2893
c8bf1b67 2894 em_tree = &fs_info->mapping_tree;
592d92ee
LB
2895 read_lock(&em_tree->lock);
2896 em = lookup_extent_mapping(em_tree, logical, length);
2897 read_unlock(&em_tree->lock);
2898
2899 if (!em) {
2900 btrfs_crit(fs_info, "unable to find logical %llu length %llu",
2901 logical, length);
2902 return ERR_PTR(-EINVAL);
2903 }
2904
2905 if (em->start > logical || em->start + em->len < logical) {
2906 btrfs_crit(fs_info,
2907 "found a bad mapping, wanted %llu-%llu, found %llu-%llu",
2908 logical, length, em->start, em->start + em->len);
2909 free_extent_map(em);
2910 return ERR_PTR(-EINVAL);
2911 }
2912
2913 /* callers are responsible for dropping em's ref. */
2914 return em;
2915}
2916
97aff912 2917int btrfs_remove_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset)
8f18cf13 2918{
97aff912 2919 struct btrfs_fs_info *fs_info = trans->fs_info;
8f18cf13
CM
2920 struct extent_map *em;
2921 struct map_lookup *map;
2196d6e8 2922 u64 dev_extent_len = 0;
47ab2a6c 2923 int i, ret = 0;
0b246afa 2924 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
8f18cf13 2925
60ca842e 2926 em = btrfs_get_chunk_map(fs_info, chunk_offset, 1);
592d92ee 2927 if (IS_ERR(em)) {
47ab2a6c
JB
2928 /*
2929 * This is a logic error, but we don't want to just rely on the
bb7ab3b9 2930 * user having built with ASSERT enabled, so if ASSERT doesn't
47ab2a6c
JB
2931 * do anything we still error out.
2932 */
2933 ASSERT(0);
592d92ee 2934 return PTR_ERR(em);
47ab2a6c 2935 }
95617d69 2936 map = em->map_lookup;
34441361 2937 mutex_lock(&fs_info->chunk_mutex);
451a2c13 2938 check_system_chunk(trans, map->type);
34441361 2939 mutex_unlock(&fs_info->chunk_mutex);
8f18cf13 2940
57ba4cb8
FM
2941 /*
2942 * Take the device list mutex to prevent races with the final phase of
2943 * a device replace operation that replaces the device object associated
2944 * with map stripes (dev-replace.c:btrfs_dev_replace_finishing()).
2945 */
2946 mutex_lock(&fs_devices->device_list_mutex);
8f18cf13 2947 for (i = 0; i < map->num_stripes; i++) {
47ab2a6c 2948 struct btrfs_device *device = map->stripes[i].dev;
2196d6e8
MX
2949 ret = btrfs_free_dev_extent(trans, device,
2950 map->stripes[i].physical,
2951 &dev_extent_len);
47ab2a6c 2952 if (ret) {
57ba4cb8 2953 mutex_unlock(&fs_devices->device_list_mutex);
66642832 2954 btrfs_abort_transaction(trans, ret);
47ab2a6c
JB
2955 goto out;
2956 }
a061fc8d 2957
2196d6e8 2958 if (device->bytes_used > 0) {
34441361 2959 mutex_lock(&fs_info->chunk_mutex);
2196d6e8
MX
2960 btrfs_device_set_bytes_used(device,
2961 device->bytes_used - dev_extent_len);
a5ed45f8 2962 atomic64_add(dev_extent_len, &fs_info->free_chunk_space);
0b246afa 2963 btrfs_clear_space_info_full(fs_info);
34441361 2964 mutex_unlock(&fs_info->chunk_mutex);
2196d6e8 2965 }
a061fc8d 2966
64bc6c2a
NB
2967 ret = btrfs_update_device(trans, device);
2968 if (ret) {
2969 mutex_unlock(&fs_devices->device_list_mutex);
2970 btrfs_abort_transaction(trans, ret);
2971 goto out;
dfe25020 2972 }
8f18cf13 2973 }
57ba4cb8
FM
2974 mutex_unlock(&fs_devices->device_list_mutex);
2975
f4208794 2976 ret = btrfs_free_chunk(trans, chunk_offset);
47ab2a6c 2977 if (ret) {
66642832 2978 btrfs_abort_transaction(trans, ret);
47ab2a6c
JB
2979 goto out;
2980 }
8f18cf13 2981
6bccf3ab 2982 trace_btrfs_chunk_free(fs_info, map, chunk_offset, em->len);
1abe9b8a 2983
8f18cf13 2984 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
408fbf19 2985 ret = btrfs_del_sys_chunk(fs_info, chunk_offset);
47ab2a6c 2986 if (ret) {
66642832 2987 btrfs_abort_transaction(trans, ret);
47ab2a6c
JB
2988 goto out;
2989 }
8f18cf13
CM
2990 }
2991
5a98ec01 2992 ret = btrfs_remove_block_group(trans, chunk_offset, em);
47ab2a6c 2993 if (ret) {
66642832 2994 btrfs_abort_transaction(trans, ret);
47ab2a6c
JB
2995 goto out;
2996 }
2b82032c 2997
47ab2a6c 2998out:
2b82032c
YZ
2999 /* once for us */
3000 free_extent_map(em);
47ab2a6c
JB
3001 return ret;
3002}
2b82032c 3003
5b4aacef 3004static int btrfs_relocate_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
47ab2a6c 3005{
5b4aacef 3006 struct btrfs_root *root = fs_info->chunk_root;
19c4d2f9 3007 struct btrfs_trans_handle *trans;
b0643e59 3008 struct btrfs_block_group *block_group;
47ab2a6c 3009 int ret;
2b82032c 3010
67c5e7d4
FM
3011 /*
3012 * Prevent races with automatic removal of unused block groups.
3013 * After we relocate and before we remove the chunk with offset
3014 * chunk_offset, automatic removal of the block group can kick in,
3015 * resulting in a failure when calling btrfs_remove_chunk() below.
3016 *
3017 * Make sure to acquire this mutex before doing a tree search (dev
3018 * or chunk trees) to find chunks. Otherwise the cleaner kthread might
3019 * call btrfs_remove_chunk() (through btrfs_delete_unused_bgs()) after
3020 * we release the path used to search the chunk/dev tree and before
3021 * the current task acquires this mutex and calls us.
3022 */
a32bf9a3 3023 lockdep_assert_held(&fs_info->delete_unused_bgs_mutex);
67c5e7d4 3024
47ab2a6c 3025 /* step one, relocate all the extents inside this chunk */
2ff7e61e 3026 btrfs_scrub_pause(fs_info);
0b246afa 3027 ret = btrfs_relocate_block_group(fs_info, chunk_offset);
2ff7e61e 3028 btrfs_scrub_continue(fs_info);
47ab2a6c
JB
3029 if (ret)
3030 return ret;
3031
b0643e59
DZ
3032 block_group = btrfs_lookup_block_group(fs_info, chunk_offset);
3033 if (!block_group)
3034 return -ENOENT;
3035 btrfs_discard_cancel_work(&fs_info->discard_ctl, block_group);
3036 btrfs_put_block_group(block_group);
3037
19c4d2f9
CM
3038 trans = btrfs_start_trans_remove_block_group(root->fs_info,
3039 chunk_offset);
3040 if (IS_ERR(trans)) {
3041 ret = PTR_ERR(trans);
3042 btrfs_handle_fs_error(root->fs_info, ret, NULL);
3043 return ret;
3044 }
3045
47ab2a6c 3046 /*
19c4d2f9
CM
3047 * step two, delete the device extents and the
3048 * chunk tree entries
47ab2a6c 3049 */
97aff912 3050 ret = btrfs_remove_chunk(trans, chunk_offset);
3a45bb20 3051 btrfs_end_transaction(trans);
19c4d2f9 3052 return ret;
2b82032c
YZ
3053}
3054
2ff7e61e 3055static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info)
2b82032c 3056{
0b246afa 3057 struct btrfs_root *chunk_root = fs_info->chunk_root;
2b82032c
YZ
3058 struct btrfs_path *path;
3059 struct extent_buffer *leaf;
3060 struct btrfs_chunk *chunk;
3061 struct btrfs_key key;
3062 struct btrfs_key found_key;
2b82032c 3063 u64 chunk_type;
ba1bf481
JB
3064 bool retried = false;
3065 int failed = 0;
2b82032c
YZ
3066 int ret;
3067
3068 path = btrfs_alloc_path();
3069 if (!path)
3070 return -ENOMEM;
3071
ba1bf481 3072again:
2b82032c
YZ
3073 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3074 key.offset = (u64)-1;
3075 key.type = BTRFS_CHUNK_ITEM_KEY;
3076
3077 while (1) {
0b246afa 3078 mutex_lock(&fs_info->delete_unused_bgs_mutex);
2b82032c 3079 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
67c5e7d4 3080 if (ret < 0) {
0b246afa 3081 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
2b82032c 3082 goto error;
67c5e7d4 3083 }
79787eaa 3084 BUG_ON(ret == 0); /* Corruption */
2b82032c
YZ
3085
3086 ret = btrfs_previous_item(chunk_root, path, key.objectid,
3087 key.type);
67c5e7d4 3088 if (ret)
0b246afa 3089 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
2b82032c
YZ
3090 if (ret < 0)
3091 goto error;
3092 if (ret > 0)
3093 break;
1a40e23b 3094
2b82032c
YZ
3095 leaf = path->nodes[0];
3096 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1a40e23b 3097
2b82032c
YZ
3098 chunk = btrfs_item_ptr(leaf, path->slots[0],
3099 struct btrfs_chunk);
3100 chunk_type = btrfs_chunk_type(leaf, chunk);
b3b4aa74 3101 btrfs_release_path(path);
8f18cf13 3102
2b82032c 3103 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
0b246afa 3104 ret = btrfs_relocate_chunk(fs_info, found_key.offset);
ba1bf481
JB
3105 if (ret == -ENOSPC)
3106 failed++;
14586651
HS
3107 else
3108 BUG_ON(ret);
2b82032c 3109 }
0b246afa 3110 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
8f18cf13 3111
2b82032c
YZ
3112 if (found_key.offset == 0)
3113 break;
3114 key.offset = found_key.offset - 1;
3115 }
3116 ret = 0;
ba1bf481
JB
3117 if (failed && !retried) {
3118 failed = 0;
3119 retried = true;
3120 goto again;
fae7f21c 3121 } else if (WARN_ON(failed && retried)) {
ba1bf481
JB
3122 ret = -ENOSPC;
3123 }
2b82032c
YZ
3124error:
3125 btrfs_free_path(path);
3126 return ret;
8f18cf13
CM
3127}
3128
a6f93c71
LB
3129/*
3130 * return 1 : allocate a data chunk successfully,
3131 * return <0: errors during allocating a data chunk,
3132 * return 0 : no need to allocate a data chunk.
3133 */
3134static int btrfs_may_alloc_data_chunk(struct btrfs_fs_info *fs_info,
3135 u64 chunk_offset)
3136{
32da5386 3137 struct btrfs_block_group *cache;
a6f93c71
LB
3138 u64 bytes_used;
3139 u64 chunk_type;
3140
3141 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3142 ASSERT(cache);
3143 chunk_type = cache->flags;
3144 btrfs_put_block_group(cache);
3145
5ae21692
JT
3146 if (!(chunk_type & BTRFS_BLOCK_GROUP_DATA))
3147 return 0;
3148
3149 spin_lock(&fs_info->data_sinfo->lock);
3150 bytes_used = fs_info->data_sinfo->bytes_used;
3151 spin_unlock(&fs_info->data_sinfo->lock);
3152
3153 if (!bytes_used) {
3154 struct btrfs_trans_handle *trans;
3155 int ret;
3156
3157 trans = btrfs_join_transaction(fs_info->tree_root);
3158 if (IS_ERR(trans))
3159 return PTR_ERR(trans);
3160
3161 ret = btrfs_force_chunk_alloc(trans, BTRFS_BLOCK_GROUP_DATA);
3162 btrfs_end_transaction(trans);
3163 if (ret < 0)
3164 return ret;
3165 return 1;
a6f93c71 3166 }
5ae21692 3167
a6f93c71
LB
3168 return 0;
3169}
3170
6bccf3ab 3171static int insert_balance_item(struct btrfs_fs_info *fs_info,
0940ebf6
ID
3172 struct btrfs_balance_control *bctl)
3173{
6bccf3ab 3174 struct btrfs_root *root = fs_info->tree_root;
0940ebf6
ID
3175 struct btrfs_trans_handle *trans;
3176 struct btrfs_balance_item *item;
3177 struct btrfs_disk_balance_args disk_bargs;
3178 struct btrfs_path *path;
3179 struct extent_buffer *leaf;
3180 struct btrfs_key key;
3181 int ret, err;
3182
3183 path = btrfs_alloc_path();
3184 if (!path)
3185 return -ENOMEM;
3186
3187 trans = btrfs_start_transaction(root, 0);
3188 if (IS_ERR(trans)) {
3189 btrfs_free_path(path);
3190 return PTR_ERR(trans);
3191 }
3192
3193 key.objectid = BTRFS_BALANCE_OBJECTID;
c479cb4f 3194 key.type = BTRFS_TEMPORARY_ITEM_KEY;
0940ebf6
ID
3195 key.offset = 0;
3196
3197 ret = btrfs_insert_empty_item(trans, root, path, &key,
3198 sizeof(*item));
3199 if (ret)
3200 goto out;
3201
3202 leaf = path->nodes[0];
3203 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3204
b159fa28 3205 memzero_extent_buffer(leaf, (unsigned long)item, sizeof(*item));
0940ebf6
ID
3206
3207 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
3208 btrfs_set_balance_data(leaf, item, &disk_bargs);
3209 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
3210 btrfs_set_balance_meta(leaf, item, &disk_bargs);
3211 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
3212 btrfs_set_balance_sys(leaf, item, &disk_bargs);
3213
3214 btrfs_set_balance_flags(leaf, item, bctl->flags);
3215
3216 btrfs_mark_buffer_dirty(leaf);
3217out:
3218 btrfs_free_path(path);
3a45bb20 3219 err = btrfs_commit_transaction(trans);
0940ebf6
ID
3220 if (err && !ret)
3221 ret = err;
3222 return ret;
3223}
3224
6bccf3ab 3225static int del_balance_item(struct btrfs_fs_info *fs_info)
0940ebf6 3226{
6bccf3ab 3227 struct btrfs_root *root = fs_info->tree_root;
0940ebf6
ID
3228 struct btrfs_trans_handle *trans;
3229 struct btrfs_path *path;
3230 struct btrfs_key key;
3231 int ret, err;
3232
3233 path = btrfs_alloc_path();
3234 if (!path)
3235 return -ENOMEM;
3236
3502a8c0 3237 trans = btrfs_start_transaction_fallback_global_rsv(root, 0);
0940ebf6
ID
3238 if (IS_ERR(trans)) {
3239 btrfs_free_path(path);
3240 return PTR_ERR(trans);
3241 }
3242
3243 key.objectid = BTRFS_BALANCE_OBJECTID;
c479cb4f 3244 key.type = BTRFS_TEMPORARY_ITEM_KEY;
0940ebf6
ID
3245 key.offset = 0;
3246
3247 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3248 if (ret < 0)
3249 goto out;
3250 if (ret > 0) {
3251 ret = -ENOENT;
3252 goto out;
3253 }
3254
3255 ret = btrfs_del_item(trans, root, path);
3256out:
3257 btrfs_free_path(path);
3a45bb20 3258 err = btrfs_commit_transaction(trans);
0940ebf6
ID
3259 if (err && !ret)
3260 ret = err;
3261 return ret;
3262}
3263
59641015
ID
3264/*
3265 * This is a heuristic used to reduce the number of chunks balanced on
3266 * resume after balance was interrupted.
3267 */
3268static void update_balance_args(struct btrfs_balance_control *bctl)
3269{
3270 /*
3271 * Turn on soft mode for chunk types that were being converted.
3272 */
3273 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
3274 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
3275 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
3276 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
3277 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
3278 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
3279
3280 /*
3281 * Turn on usage filter if is not already used. The idea is
3282 * that chunks that we have already balanced should be
3283 * reasonably full. Don't do it for chunks that are being
3284 * converted - that will keep us from relocating unconverted
3285 * (albeit full) chunks.
3286 */
3287 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
bc309467 3288 !(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
59641015
ID
3289 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3290 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
3291 bctl->data.usage = 90;
3292 }
3293 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
bc309467 3294 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
59641015
ID
3295 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3296 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
3297 bctl->sys.usage = 90;
3298 }
3299 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
bc309467 3300 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
59641015
ID
3301 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3302 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
3303 bctl->meta.usage = 90;
3304 }
3305}
3306
149196a2
DS
3307/*
3308 * Clear the balance status in fs_info and delete the balance item from disk.
3309 */
3310static void reset_balance_state(struct btrfs_fs_info *fs_info)
c9e9f97b
ID
3311{
3312 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
149196a2 3313 int ret;
c9e9f97b
ID
3314
3315 BUG_ON(!fs_info->balance_ctl);
3316
3317 spin_lock(&fs_info->balance_lock);
3318 fs_info->balance_ctl = NULL;
3319 spin_unlock(&fs_info->balance_lock);
3320
3321 kfree(bctl);
149196a2
DS
3322 ret = del_balance_item(fs_info);
3323 if (ret)
3324 btrfs_handle_fs_error(fs_info, ret, NULL);
c9e9f97b
ID
3325}
3326
ed25e9b2
ID
3327/*
3328 * Balance filters. Return 1 if chunk should be filtered out
3329 * (should not be balanced).
3330 */
899c81ea 3331static int chunk_profiles_filter(u64 chunk_type,
ed25e9b2
ID
3332 struct btrfs_balance_args *bargs)
3333{
899c81ea
ID
3334 chunk_type = chunk_to_extended(chunk_type) &
3335 BTRFS_EXTENDED_PROFILE_MASK;
ed25e9b2 3336
899c81ea 3337 if (bargs->profiles & chunk_type)
ed25e9b2
ID
3338 return 0;
3339
3340 return 1;
3341}
3342
dba72cb3 3343static int chunk_usage_range_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
5ce5b3c0 3344 struct btrfs_balance_args *bargs)
bc309467 3345{
32da5386 3346 struct btrfs_block_group *cache;
bc309467
DS
3347 u64 chunk_used;
3348 u64 user_thresh_min;
3349 u64 user_thresh_max;
3350 int ret = 1;
3351
3352 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
bf38be65 3353 chunk_used = cache->used;
bc309467
DS
3354
3355 if (bargs->usage_min == 0)
3356 user_thresh_min = 0;
3357 else
b3470b5d
DS
3358 user_thresh_min = div_factor_fine(cache->length,
3359 bargs->usage_min);
bc309467
DS
3360
3361 if (bargs->usage_max == 0)
3362 user_thresh_max = 1;
3363 else if (bargs->usage_max > 100)
b3470b5d 3364 user_thresh_max = cache->length;
bc309467 3365 else
b3470b5d
DS
3366 user_thresh_max = div_factor_fine(cache->length,
3367 bargs->usage_max);
bc309467
DS
3368
3369 if (user_thresh_min <= chunk_used && chunk_used < user_thresh_max)
3370 ret = 0;
3371
3372 btrfs_put_block_group(cache);
3373 return ret;
3374}
3375
dba72cb3 3376static int chunk_usage_filter(struct btrfs_fs_info *fs_info,
bc309467 3377 u64 chunk_offset, struct btrfs_balance_args *bargs)
5ce5b3c0 3378{
32da5386 3379 struct btrfs_block_group *cache;
5ce5b3c0
ID
3380 u64 chunk_used, user_thresh;
3381 int ret = 1;
3382
3383 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
bf38be65 3384 chunk_used = cache->used;
5ce5b3c0 3385
bc309467 3386 if (bargs->usage_min == 0)
3e39cea6 3387 user_thresh = 1;
a105bb88 3388 else if (bargs->usage > 100)
b3470b5d 3389 user_thresh = cache->length;
a105bb88 3390 else
b3470b5d 3391 user_thresh = div_factor_fine(cache->length, bargs->usage);
a105bb88 3392
5ce5b3c0
ID
3393 if (chunk_used < user_thresh)
3394 ret = 0;
3395
3396 btrfs_put_block_group(cache);
3397 return ret;
3398}
3399
409d404b
ID
3400static int chunk_devid_filter(struct extent_buffer *leaf,
3401 struct btrfs_chunk *chunk,
3402 struct btrfs_balance_args *bargs)
3403{
3404 struct btrfs_stripe *stripe;
3405 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3406 int i;
3407
3408 for (i = 0; i < num_stripes; i++) {
3409 stripe = btrfs_stripe_nr(chunk, i);
3410 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
3411 return 0;
3412 }
3413
3414 return 1;
3415}
3416
946c9256
DS
3417static u64 calc_data_stripes(u64 type, int num_stripes)
3418{
3419 const int index = btrfs_bg_flags_to_raid_index(type);
3420 const int ncopies = btrfs_raid_array[index].ncopies;
3421 const int nparity = btrfs_raid_array[index].nparity;
3422
3423 if (nparity)
3424 return num_stripes - nparity;
3425 else
3426 return num_stripes / ncopies;
3427}
3428
94e60d5a
ID
3429/* [pstart, pend) */
3430static int chunk_drange_filter(struct extent_buffer *leaf,
3431 struct btrfs_chunk *chunk,
94e60d5a
ID
3432 struct btrfs_balance_args *bargs)
3433{
3434 struct btrfs_stripe *stripe;
3435 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3436 u64 stripe_offset;
3437 u64 stripe_length;
946c9256 3438 u64 type;
94e60d5a
ID
3439 int factor;
3440 int i;
3441
3442 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
3443 return 0;
3444
946c9256
DS
3445 type = btrfs_chunk_type(leaf, chunk);
3446 factor = calc_data_stripes(type, num_stripes);
94e60d5a
ID
3447
3448 for (i = 0; i < num_stripes; i++) {
3449 stripe = btrfs_stripe_nr(chunk, i);
3450 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
3451 continue;
3452
3453 stripe_offset = btrfs_stripe_offset(leaf, stripe);
3454 stripe_length = btrfs_chunk_length(leaf, chunk);
b8b93add 3455 stripe_length = div_u64(stripe_length, factor);
94e60d5a
ID
3456
3457 if (stripe_offset < bargs->pend &&
3458 stripe_offset + stripe_length > bargs->pstart)
3459 return 0;
3460 }
3461
3462 return 1;
3463}
3464
ea67176a
ID
3465/* [vstart, vend) */
3466static int chunk_vrange_filter(struct extent_buffer *leaf,
3467 struct btrfs_chunk *chunk,
3468 u64 chunk_offset,
3469 struct btrfs_balance_args *bargs)
3470{
3471 if (chunk_offset < bargs->vend &&
3472 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
3473 /* at least part of the chunk is inside this vrange */
3474 return 0;
3475
3476 return 1;
3477}
3478
dee32d0a
GAP
3479static int chunk_stripes_range_filter(struct extent_buffer *leaf,
3480 struct btrfs_chunk *chunk,
3481 struct btrfs_balance_args *bargs)
3482{
3483 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3484
3485 if (bargs->stripes_min <= num_stripes
3486 && num_stripes <= bargs->stripes_max)
3487 return 0;
3488
3489 return 1;
3490}
3491
899c81ea 3492static int chunk_soft_convert_filter(u64 chunk_type,
cfa4c961
ID
3493 struct btrfs_balance_args *bargs)
3494{
3495 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
3496 return 0;
3497
899c81ea
ID
3498 chunk_type = chunk_to_extended(chunk_type) &
3499 BTRFS_EXTENDED_PROFILE_MASK;
cfa4c961 3500
899c81ea 3501 if (bargs->target == chunk_type)
cfa4c961
ID
3502 return 1;
3503
3504 return 0;
3505}
3506
6ec0896c 3507static int should_balance_chunk(struct extent_buffer *leaf,
f43ffb60
ID
3508 struct btrfs_chunk *chunk, u64 chunk_offset)
3509{
6ec0896c 3510 struct btrfs_fs_info *fs_info = leaf->fs_info;
0b246afa 3511 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
f43ffb60
ID
3512 struct btrfs_balance_args *bargs = NULL;
3513 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
3514
3515 /* type filter */
3516 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
3517 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
3518 return 0;
3519 }
3520
3521 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3522 bargs = &bctl->data;
3523 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3524 bargs = &bctl->sys;
3525 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3526 bargs = &bctl->meta;
3527
ed25e9b2
ID
3528 /* profiles filter */
3529 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
3530 chunk_profiles_filter(chunk_type, bargs)) {
3531 return 0;
5ce5b3c0
ID
3532 }
3533
3534 /* usage filter */
3535 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
0b246afa 3536 chunk_usage_filter(fs_info, chunk_offset, bargs)) {
5ce5b3c0 3537 return 0;
bc309467 3538 } else if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
0b246afa 3539 chunk_usage_range_filter(fs_info, chunk_offset, bargs)) {
bc309467 3540 return 0;
409d404b
ID
3541 }
3542
3543 /* devid filter */
3544 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
3545 chunk_devid_filter(leaf, chunk, bargs)) {
3546 return 0;
94e60d5a
ID
3547 }
3548
3549 /* drange filter, makes sense only with devid filter */
3550 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
e4ff5fb5 3551 chunk_drange_filter(leaf, chunk, bargs)) {
94e60d5a 3552 return 0;
ea67176a
ID
3553 }
3554
3555 /* vrange filter */
3556 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
3557 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
3558 return 0;
ed25e9b2
ID
3559 }
3560
dee32d0a
GAP
3561 /* stripes filter */
3562 if ((bargs->flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE) &&
3563 chunk_stripes_range_filter(leaf, chunk, bargs)) {
3564 return 0;
3565 }
3566
cfa4c961
ID
3567 /* soft profile changing mode */
3568 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
3569 chunk_soft_convert_filter(chunk_type, bargs)) {
3570 return 0;
3571 }
3572
7d824b6f
DS
3573 /*
3574 * limited by count, must be the last filter
3575 */
3576 if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) {
3577 if (bargs->limit == 0)
3578 return 0;
3579 else
3580 bargs->limit--;
12907fc7
DS
3581 } else if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE)) {
3582 /*
3583 * Same logic as the 'limit' filter; the minimum cannot be
01327610 3584 * determined here because we do not have the global information
12907fc7
DS
3585 * about the count of all chunks that satisfy the filters.
3586 */
3587 if (bargs->limit_max == 0)
3588 return 0;
3589 else
3590 bargs->limit_max--;
7d824b6f
DS
3591 }
3592
f43ffb60
ID
3593 return 1;
3594}
3595
c9e9f97b 3596static int __btrfs_balance(struct btrfs_fs_info *fs_info)
ec44a35c 3597{
19a39dce 3598 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
c9e9f97b 3599 struct btrfs_root *chunk_root = fs_info->chunk_root;
12907fc7 3600 u64 chunk_type;
f43ffb60 3601 struct btrfs_chunk *chunk;
5a488b9d 3602 struct btrfs_path *path = NULL;
ec44a35c 3603 struct btrfs_key key;
ec44a35c 3604 struct btrfs_key found_key;
f43ffb60
ID
3605 struct extent_buffer *leaf;
3606 int slot;
c9e9f97b
ID
3607 int ret;
3608 int enospc_errors = 0;
19a39dce 3609 bool counting = true;
12907fc7 3610 /* The single value limit and min/max limits use the same bytes in the */
7d824b6f
DS
3611 u64 limit_data = bctl->data.limit;
3612 u64 limit_meta = bctl->meta.limit;
3613 u64 limit_sys = bctl->sys.limit;
12907fc7
DS
3614 u32 count_data = 0;
3615 u32 count_meta = 0;
3616 u32 count_sys = 0;
2c9fe835 3617 int chunk_reserved = 0;
ec44a35c 3618
ec44a35c 3619 path = btrfs_alloc_path();
17e9f796
MF
3620 if (!path) {
3621 ret = -ENOMEM;
3622 goto error;
3623 }
19a39dce
ID
3624
3625 /* zero out stat counters */
3626 spin_lock(&fs_info->balance_lock);
3627 memset(&bctl->stat, 0, sizeof(bctl->stat));
3628 spin_unlock(&fs_info->balance_lock);
3629again:
7d824b6f 3630 if (!counting) {
12907fc7
DS
3631 /*
3632 * The single value limit and min/max limits use the same bytes
3633 * in the
3634 */
7d824b6f
DS
3635 bctl->data.limit = limit_data;
3636 bctl->meta.limit = limit_meta;
3637 bctl->sys.limit = limit_sys;
3638 }
ec44a35c
CM
3639 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3640 key.offset = (u64)-1;
3641 key.type = BTRFS_CHUNK_ITEM_KEY;
3642
d397712b 3643 while (1) {
19a39dce 3644 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
a7e99c69 3645 atomic_read(&fs_info->balance_cancel_req)) {
837d5b6e
ID
3646 ret = -ECANCELED;
3647 goto error;
3648 }
3649
67c5e7d4 3650 mutex_lock(&fs_info->delete_unused_bgs_mutex);
ec44a35c 3651 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
67c5e7d4
FM
3652 if (ret < 0) {
3653 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
ec44a35c 3654 goto error;
67c5e7d4 3655 }
ec44a35c
CM
3656
3657 /*
3658 * this shouldn't happen, it means the last relocate
3659 * failed
3660 */
3661 if (ret == 0)
c9e9f97b 3662 BUG(); /* FIXME break ? */
ec44a35c
CM
3663
3664 ret = btrfs_previous_item(chunk_root, path, 0,
3665 BTRFS_CHUNK_ITEM_KEY);
c9e9f97b 3666 if (ret) {
67c5e7d4 3667 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
c9e9f97b 3668 ret = 0;
ec44a35c 3669 break;
c9e9f97b 3670 }
7d9eb12c 3671
f43ffb60
ID
3672 leaf = path->nodes[0];
3673 slot = path->slots[0];
3674 btrfs_item_key_to_cpu(leaf, &found_key, slot);
7d9eb12c 3675
67c5e7d4
FM
3676 if (found_key.objectid != key.objectid) {
3677 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
ec44a35c 3678 break;
67c5e7d4 3679 }
7d9eb12c 3680
f43ffb60 3681 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
12907fc7 3682 chunk_type = btrfs_chunk_type(leaf, chunk);
f43ffb60 3683
19a39dce
ID
3684 if (!counting) {
3685 spin_lock(&fs_info->balance_lock);
3686 bctl->stat.considered++;
3687 spin_unlock(&fs_info->balance_lock);
3688 }
3689
6ec0896c 3690 ret = should_balance_chunk(leaf, chunk, found_key.offset);
2c9fe835 3691
b3b4aa74 3692 btrfs_release_path(path);
67c5e7d4
FM
3693 if (!ret) {
3694 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
f43ffb60 3695 goto loop;
67c5e7d4 3696 }
f43ffb60 3697
19a39dce 3698 if (counting) {
67c5e7d4 3699 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
19a39dce
ID
3700 spin_lock(&fs_info->balance_lock);
3701 bctl->stat.expected++;
3702 spin_unlock(&fs_info->balance_lock);
12907fc7
DS
3703
3704 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3705 count_data++;
3706 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3707 count_sys++;
3708 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3709 count_meta++;
3710
3711 goto loop;
3712 }
3713
3714 /*
3715 * Apply limit_min filter, no need to check if the LIMITS
3716 * filter is used, limit_min is 0 by default
3717 */
3718 if (((chunk_type & BTRFS_BLOCK_GROUP_DATA) &&
3719 count_data < bctl->data.limit_min)
3720 || ((chunk_type & BTRFS_BLOCK_GROUP_METADATA) &&
3721 count_meta < bctl->meta.limit_min)
3722 || ((chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) &&
3723 count_sys < bctl->sys.limit_min)) {
3724 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
19a39dce
ID
3725 goto loop;
3726 }
3727
a6f93c71
LB
3728 if (!chunk_reserved) {
3729 /*
3730 * We may be relocating the only data chunk we have,
3731 * which could potentially end up with losing data's
3732 * raid profile, so lets allocate an empty one in
3733 * advance.
3734 */
3735 ret = btrfs_may_alloc_data_chunk(fs_info,
3736 found_key.offset);
2c9fe835
ZL
3737 if (ret < 0) {
3738 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3739 goto error;
a6f93c71
LB
3740 } else if (ret == 1) {
3741 chunk_reserved = 1;
2c9fe835 3742 }
2c9fe835
ZL
3743 }
3744
5b4aacef 3745 ret = btrfs_relocate_chunk(fs_info, found_key.offset);
67c5e7d4 3746 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
19a39dce 3747 if (ret == -ENOSPC) {
c9e9f97b 3748 enospc_errors++;
eede2bf3
OS
3749 } else if (ret == -ETXTBSY) {
3750 btrfs_info(fs_info,
3751 "skipping relocation of block group %llu due to active swapfile",
3752 found_key.offset);
3753 ret = 0;
3754 } else if (ret) {
3755 goto error;
19a39dce
ID
3756 } else {
3757 spin_lock(&fs_info->balance_lock);
3758 bctl->stat.completed++;
3759 spin_unlock(&fs_info->balance_lock);
3760 }
f43ffb60 3761loop:
795a3321
ID
3762 if (found_key.offset == 0)
3763 break;
ba1bf481 3764 key.offset = found_key.offset - 1;
ec44a35c 3765 }
c9e9f97b 3766
19a39dce
ID
3767 if (counting) {
3768 btrfs_release_path(path);
3769 counting = false;
3770 goto again;
3771 }
ec44a35c
CM
3772error:
3773 btrfs_free_path(path);
c9e9f97b 3774 if (enospc_errors) {
efe120a0 3775 btrfs_info(fs_info, "%d enospc errors during balance",
5d163e0e 3776 enospc_errors);
c9e9f97b
ID
3777 if (!ret)
3778 ret = -ENOSPC;
3779 }
3780
ec44a35c
CM
3781 return ret;
3782}
3783
0c460c0d
ID
3784/**
3785 * alloc_profile_is_valid - see if a given profile is valid and reduced
3786 * @flags: profile to validate
3787 * @extended: if true @flags is treated as an extended profile
3788 */
3789static int alloc_profile_is_valid(u64 flags, int extended)
3790{
3791 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
3792 BTRFS_BLOCK_GROUP_PROFILE_MASK);
3793
3794 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
3795
3796 /* 1) check that all other bits are zeroed */
3797 if (flags & ~mask)
3798 return 0;
3799
3800 /* 2) see if profile is reduced */
3801 if (flags == 0)
3802 return !extended; /* "0" is valid for usual profiles */
3803
c1499166 3804 return has_single_bit_set(flags);
0c460c0d
ID
3805}
3806
837d5b6e
ID
3807static inline int balance_need_close(struct btrfs_fs_info *fs_info)
3808{
a7e99c69
ID
3809 /* cancel requested || normal exit path */
3810 return atomic_read(&fs_info->balance_cancel_req) ||
3811 (atomic_read(&fs_info->balance_pause_req) == 0 &&
3812 atomic_read(&fs_info->balance_cancel_req) == 0);
837d5b6e
ID
3813}
3814
5ba366c3
DS
3815/*
3816 * Validate target profile against allowed profiles and return true if it's OK.
3817 * Otherwise print the error message and return false.
3818 */
3819static inline int validate_convert_profile(struct btrfs_fs_info *fs_info,
3820 const struct btrfs_balance_args *bargs,
3821 u64 allowed, const char *type)
bdcd3c97 3822{
5ba366c3
DS
3823 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
3824 return true;
3825
3826 /* Profile is valid and does not have bits outside of the allowed set */
3827 if (alloc_profile_is_valid(bargs->target, 1) &&
3828 (bargs->target & ~allowed) == 0)
3829 return true;
3830
3831 btrfs_err(fs_info, "balance: invalid convert %s profile %s",
3832 type, btrfs_bg_type_to_raid_name(bargs->target));
3833 return false;
bdcd3c97
AM
3834}
3835
56fc37d9
AJ
3836/*
3837 * Fill @buf with textual description of balance filter flags @bargs, up to
3838 * @size_buf including the terminating null. The output may be trimmed if it
3839 * does not fit into the provided buffer.
3840 */
3841static void describe_balance_args(struct btrfs_balance_args *bargs, char *buf,
3842 u32 size_buf)
3843{
3844 int ret;
3845 u32 size_bp = size_buf;
3846 char *bp = buf;
3847 u64 flags = bargs->flags;
3848 char tmp_buf[128] = {'\0'};
3849
3850 if (!flags)
3851 return;
3852
3853#define CHECK_APPEND_NOARG(a) \
3854 do { \
3855 ret = snprintf(bp, size_bp, (a)); \
3856 if (ret < 0 || ret >= size_bp) \
3857 goto out_overflow; \
3858 size_bp -= ret; \
3859 bp += ret; \
3860 } while (0)
3861
3862#define CHECK_APPEND_1ARG(a, v1) \
3863 do { \
3864 ret = snprintf(bp, size_bp, (a), (v1)); \
3865 if (ret < 0 || ret >= size_bp) \
3866 goto out_overflow; \
3867 size_bp -= ret; \
3868 bp += ret; \
3869 } while (0)
3870
3871#define CHECK_APPEND_2ARG(a, v1, v2) \
3872 do { \
3873 ret = snprintf(bp, size_bp, (a), (v1), (v2)); \
3874 if (ret < 0 || ret >= size_bp) \
3875 goto out_overflow; \
3876 size_bp -= ret; \
3877 bp += ret; \
3878 } while (0)
3879
158da513
DS
3880 if (flags & BTRFS_BALANCE_ARGS_CONVERT)
3881 CHECK_APPEND_1ARG("convert=%s,",
3882 btrfs_bg_type_to_raid_name(bargs->target));
56fc37d9
AJ
3883
3884 if (flags & BTRFS_BALANCE_ARGS_SOFT)
3885 CHECK_APPEND_NOARG("soft,");
3886
3887 if (flags & BTRFS_BALANCE_ARGS_PROFILES) {
3888 btrfs_describe_block_groups(bargs->profiles, tmp_buf,
3889 sizeof(tmp_buf));
3890 CHECK_APPEND_1ARG("profiles=%s,", tmp_buf);
3891 }
3892
3893 if (flags & BTRFS_BALANCE_ARGS_USAGE)
3894 CHECK_APPEND_1ARG("usage=%llu,", bargs->usage);
3895
3896 if (flags & BTRFS_BALANCE_ARGS_USAGE_RANGE)
3897 CHECK_APPEND_2ARG("usage=%u..%u,",
3898 bargs->usage_min, bargs->usage_max);
3899
3900 if (flags & BTRFS_BALANCE_ARGS_DEVID)
3901 CHECK_APPEND_1ARG("devid=%llu,", bargs->devid);
3902
3903 if (flags & BTRFS_BALANCE_ARGS_DRANGE)
3904 CHECK_APPEND_2ARG("drange=%llu..%llu,",
3905 bargs->pstart, bargs->pend);
3906
3907 if (flags & BTRFS_BALANCE_ARGS_VRANGE)
3908 CHECK_APPEND_2ARG("vrange=%llu..%llu,",
3909 bargs->vstart, bargs->vend);
3910
3911 if (flags & BTRFS_BALANCE_ARGS_LIMIT)
3912 CHECK_APPEND_1ARG("limit=%llu,", bargs->limit);
3913
3914 if (flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE)
3915 CHECK_APPEND_2ARG("limit=%u..%u,",
3916 bargs->limit_min, bargs->limit_max);
3917
3918 if (flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE)
3919 CHECK_APPEND_2ARG("stripes=%u..%u,",
3920 bargs->stripes_min, bargs->stripes_max);
3921
3922#undef CHECK_APPEND_2ARG
3923#undef CHECK_APPEND_1ARG
3924#undef CHECK_APPEND_NOARG
3925
3926out_overflow:
3927
3928 if (size_bp < size_buf)
3929 buf[size_buf - size_bp - 1] = '\0'; /* remove last , */
3930 else
3931 buf[0] = '\0';
3932}
3933
3934static void describe_balance_start_or_resume(struct btrfs_fs_info *fs_info)
3935{
3936 u32 size_buf = 1024;
3937 char tmp_buf[192] = {'\0'};
3938 char *buf;
3939 char *bp;
3940 u32 size_bp = size_buf;
3941 int ret;
3942 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3943
3944 buf = kzalloc(size_buf, GFP_KERNEL);
3945 if (!buf)
3946 return;
3947
3948 bp = buf;
3949
3950#define CHECK_APPEND_1ARG(a, v1) \
3951 do { \
3952 ret = snprintf(bp, size_bp, (a), (v1)); \
3953 if (ret < 0 || ret >= size_bp) \
3954 goto out_overflow; \
3955 size_bp -= ret; \
3956 bp += ret; \
3957 } while (0)
3958
3959 if (bctl->flags & BTRFS_BALANCE_FORCE)
3960 CHECK_APPEND_1ARG("%s", "-f ");
3961
3962 if (bctl->flags & BTRFS_BALANCE_DATA) {
3963 describe_balance_args(&bctl->data, tmp_buf, sizeof(tmp_buf));
3964 CHECK_APPEND_1ARG("-d%s ", tmp_buf);
3965 }
3966
3967 if (bctl->flags & BTRFS_BALANCE_METADATA) {
3968 describe_balance_args(&bctl->meta, tmp_buf, sizeof(tmp_buf));
3969 CHECK_APPEND_1ARG("-m%s ", tmp_buf);
3970 }
3971
3972 if (bctl->flags & BTRFS_BALANCE_SYSTEM) {
3973 describe_balance_args(&bctl->sys, tmp_buf, sizeof(tmp_buf));
3974 CHECK_APPEND_1ARG("-s%s ", tmp_buf);
3975 }
3976
3977#undef CHECK_APPEND_1ARG
3978
3979out_overflow:
3980
3981 if (size_bp < size_buf)
3982 buf[size_buf - size_bp - 1] = '\0'; /* remove last " " */
3983 btrfs_info(fs_info, "balance: %s %s",
3984 (bctl->flags & BTRFS_BALANCE_RESUME) ?
3985 "resume" : "start", buf);
3986
3987 kfree(buf);
3988}
3989
c9e9f97b 3990/*
dccdb07b 3991 * Should be called with balance mutexe held
c9e9f97b 3992 */
6fcf6e2b
DS
3993int btrfs_balance(struct btrfs_fs_info *fs_info,
3994 struct btrfs_balance_control *bctl,
c9e9f97b
ID
3995 struct btrfs_ioctl_balance_args *bargs)
3996{
14506127 3997 u64 meta_target, data_target;
f43ffb60 3998 u64 allowed;
e4837f8f 3999 int mixed = 0;
c9e9f97b 4000 int ret;
8dabb742 4001 u64 num_devices;
de98ced9 4002 unsigned seq;
e62869be 4003 bool reducing_redundancy;
081db89b 4004 int i;
c9e9f97b 4005
837d5b6e 4006 if (btrfs_fs_closing(fs_info) ||
a7e99c69 4007 atomic_read(&fs_info->balance_pause_req) ||
726a3421 4008 btrfs_should_cancel_balance(fs_info)) {
c9e9f97b
ID
4009 ret = -EINVAL;
4010 goto out;
4011 }
4012
e4837f8f
ID
4013 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
4014 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
4015 mixed = 1;
4016
f43ffb60
ID
4017 /*
4018 * In case of mixed groups both data and meta should be picked,
4019 * and identical options should be given for both of them.
4020 */
e4837f8f
ID
4021 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
4022 if (mixed && (bctl->flags & allowed)) {
f43ffb60
ID
4023 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
4024 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
4025 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
5d163e0e 4026 btrfs_err(fs_info,
6dac13f8 4027 "balance: mixed groups data and metadata options must be the same");
f43ffb60
ID
4028 ret = -EINVAL;
4029 goto out;
4030 }
4031 }
4032
b35cf1f0
JB
4033 /*
4034 * rw_devices will not change at the moment, device add/delete/replace
c3e1f96c 4035 * are exclusive
b35cf1f0
JB
4036 */
4037 num_devices = fs_info->fs_devices->rw_devices;
fab27359
QW
4038
4039 /*
4040 * SINGLE profile on-disk has no profile bit, but in-memory we have a
4041 * special bit for it, to make it easier to distinguish. Thus we need
4042 * to set it manually, or balance would refuse the profile.
4043 */
4044 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
081db89b
DS
4045 for (i = 0; i < ARRAY_SIZE(btrfs_raid_array); i++)
4046 if (num_devices >= btrfs_raid_array[i].devs_min)
4047 allowed |= btrfs_raid_array[i].bg_flag;
1da73967 4048
5ba366c3
DS
4049 if (!validate_convert_profile(fs_info, &bctl->data, allowed, "data") ||
4050 !validate_convert_profile(fs_info, &bctl->meta, allowed, "metadata") ||
4051 !validate_convert_profile(fs_info, &bctl->sys, allowed, "system")) {
e4d8ec0f
ID
4052 ret = -EINVAL;
4053 goto out;
4054 }
4055
6079e12c
DS
4056 /*
4057 * Allow to reduce metadata or system integrity only if force set for
4058 * profiles with redundancy (copies, parity)
4059 */
4060 allowed = 0;
4061 for (i = 0; i < ARRAY_SIZE(btrfs_raid_array); i++) {
4062 if (btrfs_raid_array[i].ncopies >= 2 ||
4063 btrfs_raid_array[i].tolerated_failures >= 1)
4064 allowed |= btrfs_raid_array[i].bg_flag;
4065 }
de98ced9
MX
4066 do {
4067 seq = read_seqbegin(&fs_info->profiles_lock);
4068
4069 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
4070 (fs_info->avail_system_alloc_bits & allowed) &&
4071 !(bctl->sys.target & allowed)) ||
4072 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
4073 (fs_info->avail_metadata_alloc_bits & allowed) &&
5a8067c0 4074 !(bctl->meta.target & allowed)))
e62869be 4075 reducing_redundancy = true;
5a8067c0 4076 else
e62869be 4077 reducing_redundancy = false;
5a8067c0
FM
4078
4079 /* if we're not converting, the target field is uninitialized */
4080 meta_target = (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) ?
4081 bctl->meta.target : fs_info->avail_metadata_alloc_bits;
4082 data_target = (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) ?
4083 bctl->data.target : fs_info->avail_data_alloc_bits;
de98ced9 4084 } while (read_seqretry(&fs_info->profiles_lock, seq));
e4d8ec0f 4085
e62869be 4086 if (reducing_redundancy) {
5a8067c0
FM
4087 if (bctl->flags & BTRFS_BALANCE_FORCE) {
4088 btrfs_info(fs_info,
e62869be 4089 "balance: force reducing metadata redundancy");
5a8067c0
FM
4090 } else {
4091 btrfs_err(fs_info,
e62869be 4092 "balance: reduces metadata redundancy, use --force if you want this");
5a8067c0
FM
4093 ret = -EINVAL;
4094 goto out;
4095 }
4096 }
4097
14506127
AB
4098 if (btrfs_get_num_tolerated_disk_barrier_failures(meta_target) <
4099 btrfs_get_num_tolerated_disk_barrier_failures(data_target)) {
ee592d07 4100 btrfs_warn(fs_info,
6dac13f8 4101 "balance: metadata profile %s has lower redundancy than data profile %s",
158da513
DS
4102 btrfs_bg_type_to_raid_name(meta_target),
4103 btrfs_bg_type_to_raid_name(data_target));
ee592d07
ST
4104 }
4105
9e967495
FM
4106 if (fs_info->send_in_progress) {
4107 btrfs_warn_rl(fs_info,
4108"cannot run balance while send operations are in progress (%d in progress)",
4109 fs_info->send_in_progress);
4110 ret = -EAGAIN;
4111 goto out;
4112 }
4113
6bccf3ab 4114 ret = insert_balance_item(fs_info, bctl);
59641015 4115 if (ret && ret != -EEXIST)
0940ebf6
ID
4116 goto out;
4117
59641015
ID
4118 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
4119 BUG_ON(ret == -EEXIST);
833aae18
DS
4120 BUG_ON(fs_info->balance_ctl);
4121 spin_lock(&fs_info->balance_lock);
4122 fs_info->balance_ctl = bctl;
4123 spin_unlock(&fs_info->balance_lock);
59641015
ID
4124 } else {
4125 BUG_ON(ret != -EEXIST);
4126 spin_lock(&fs_info->balance_lock);
4127 update_balance_args(bctl);
4128 spin_unlock(&fs_info->balance_lock);
4129 }
c9e9f97b 4130
3009a62f
DS
4131 ASSERT(!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
4132 set_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags);
56fc37d9 4133 describe_balance_start_or_resume(fs_info);
c9e9f97b
ID
4134 mutex_unlock(&fs_info->balance_mutex);
4135
4136 ret = __btrfs_balance(fs_info);
4137
4138 mutex_lock(&fs_info->balance_mutex);
7333bd02
AJ
4139 if (ret == -ECANCELED && atomic_read(&fs_info->balance_pause_req))
4140 btrfs_info(fs_info, "balance: paused");
44d354ab
QW
4141 /*
4142 * Balance can be canceled by:
4143 *
4144 * - Regular cancel request
4145 * Then ret == -ECANCELED and balance_cancel_req > 0
4146 *
4147 * - Fatal signal to "btrfs" process
4148 * Either the signal caught by wait_reserve_ticket() and callers
4149 * got -EINTR, or caught by btrfs_should_cancel_balance() and
4150 * got -ECANCELED.
4151 * Either way, in this case balance_cancel_req = 0, and
4152 * ret == -EINTR or ret == -ECANCELED.
4153 *
4154 * So here we only check the return value to catch canceled balance.
4155 */
4156 else if (ret == -ECANCELED || ret == -EINTR)
7333bd02
AJ
4157 btrfs_info(fs_info, "balance: canceled");
4158 else
4159 btrfs_info(fs_info, "balance: ended with status: %d", ret);
4160
3009a62f 4161 clear_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags);
c9e9f97b
ID
4162
4163 if (bargs) {
4164 memset(bargs, 0, sizeof(*bargs));
008ef096 4165 btrfs_update_ioctl_balance_args(fs_info, bargs);
c9e9f97b
ID
4166 }
4167
3a01aa7a
ID
4168 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
4169 balance_need_close(fs_info)) {
149196a2 4170 reset_balance_state(fs_info);
c3e1f96c 4171 btrfs_exclop_finish(fs_info);
3a01aa7a
ID
4172 }
4173
837d5b6e 4174 wake_up(&fs_info->balance_wait_q);
c9e9f97b
ID
4175
4176 return ret;
4177out:
59641015 4178 if (bctl->flags & BTRFS_BALANCE_RESUME)
149196a2 4179 reset_balance_state(fs_info);
a17c95df 4180 else
59641015 4181 kfree(bctl);
c3e1f96c 4182 btrfs_exclop_finish(fs_info);
a17c95df 4183
59641015
ID
4184 return ret;
4185}
4186
4187static int balance_kthread(void *data)
4188{
2b6ba629 4189 struct btrfs_fs_info *fs_info = data;
9555c6c1 4190 int ret = 0;
59641015 4191
59641015 4192 mutex_lock(&fs_info->balance_mutex);
56fc37d9 4193 if (fs_info->balance_ctl)
6fcf6e2b 4194 ret = btrfs_balance(fs_info, fs_info->balance_ctl, NULL);
59641015 4195 mutex_unlock(&fs_info->balance_mutex);
2b6ba629 4196
59641015
ID
4197 return ret;
4198}
4199
2b6ba629
ID
4200int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
4201{
4202 struct task_struct *tsk;
4203
1354e1a1 4204 mutex_lock(&fs_info->balance_mutex);
2b6ba629 4205 if (!fs_info->balance_ctl) {
1354e1a1 4206 mutex_unlock(&fs_info->balance_mutex);
2b6ba629
ID
4207 return 0;
4208 }
1354e1a1 4209 mutex_unlock(&fs_info->balance_mutex);
2b6ba629 4210
3cdde224 4211 if (btrfs_test_opt(fs_info, SKIP_BALANCE)) {
6dac13f8 4212 btrfs_info(fs_info, "balance: resume skipped");
2b6ba629
ID
4213 return 0;
4214 }
4215
02ee654d
AJ
4216 /*
4217 * A ro->rw remount sequence should continue with the paused balance
4218 * regardless of who pauses it, system or the user as of now, so set
4219 * the resume flag.
4220 */
4221 spin_lock(&fs_info->balance_lock);
4222 fs_info->balance_ctl->flags |= BTRFS_BALANCE_RESUME;
4223 spin_unlock(&fs_info->balance_lock);
4224
2b6ba629 4225 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
cd633972 4226 return PTR_ERR_OR_ZERO(tsk);
2b6ba629
ID
4227}
4228
68310a5e 4229int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
59641015 4230{
59641015
ID
4231 struct btrfs_balance_control *bctl;
4232 struct btrfs_balance_item *item;
4233 struct btrfs_disk_balance_args disk_bargs;
4234 struct btrfs_path *path;
4235 struct extent_buffer *leaf;
4236 struct btrfs_key key;
4237 int ret;
4238
4239 path = btrfs_alloc_path();
4240 if (!path)
4241 return -ENOMEM;
4242
59641015 4243 key.objectid = BTRFS_BALANCE_OBJECTID;
c479cb4f 4244 key.type = BTRFS_TEMPORARY_ITEM_KEY;
59641015
ID
4245 key.offset = 0;
4246
68310a5e 4247 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
59641015 4248 if (ret < 0)
68310a5e 4249 goto out;
59641015
ID
4250 if (ret > 0) { /* ret = -ENOENT; */
4251 ret = 0;
68310a5e
ID
4252 goto out;
4253 }
4254
4255 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
4256 if (!bctl) {
4257 ret = -ENOMEM;
4258 goto out;
59641015
ID
4259 }
4260
4261 leaf = path->nodes[0];
4262 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
4263
68310a5e
ID
4264 bctl->flags = btrfs_balance_flags(leaf, item);
4265 bctl->flags |= BTRFS_BALANCE_RESUME;
59641015
ID
4266
4267 btrfs_balance_data(leaf, item, &disk_bargs);
4268 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
4269 btrfs_balance_meta(leaf, item, &disk_bargs);
4270 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
4271 btrfs_balance_sys(leaf, item, &disk_bargs);
4272 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
4273
eee95e3f
DS
4274 /*
4275 * This should never happen, as the paused balance state is recovered
4276 * during mount without any chance of other exclusive ops to collide.
4277 *
4278 * This gives the exclusive op status to balance and keeps in paused
4279 * state until user intervention (cancel or umount). If the ownership
4280 * cannot be assigned, show a message but do not fail. The balance
4281 * is in a paused state and must have fs_info::balance_ctl properly
4282 * set up.
4283 */
c3e1f96c 4284 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_BALANCE))
eee95e3f 4285 btrfs_warn(fs_info,
6dac13f8 4286 "balance: cannot set exclusive op status, resume manually");
ed0fb78f 4287
68310a5e 4288 mutex_lock(&fs_info->balance_mutex);
833aae18
DS
4289 BUG_ON(fs_info->balance_ctl);
4290 spin_lock(&fs_info->balance_lock);
4291 fs_info->balance_ctl = bctl;
4292 spin_unlock(&fs_info->balance_lock);
68310a5e 4293 mutex_unlock(&fs_info->balance_mutex);
59641015
ID
4294out:
4295 btrfs_free_path(path);
ec44a35c
CM
4296 return ret;
4297}
4298
837d5b6e
ID
4299int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
4300{
4301 int ret = 0;
4302
4303 mutex_lock(&fs_info->balance_mutex);
4304 if (!fs_info->balance_ctl) {
4305 mutex_unlock(&fs_info->balance_mutex);
4306 return -ENOTCONN;
4307 }
4308
3009a62f 4309 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
837d5b6e
ID
4310 atomic_inc(&fs_info->balance_pause_req);
4311 mutex_unlock(&fs_info->balance_mutex);
4312
4313 wait_event(fs_info->balance_wait_q,
3009a62f 4314 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
837d5b6e
ID
4315
4316 mutex_lock(&fs_info->balance_mutex);
4317 /* we are good with balance_ctl ripped off from under us */
3009a62f 4318 BUG_ON(test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
837d5b6e
ID
4319 atomic_dec(&fs_info->balance_pause_req);
4320 } else {
4321 ret = -ENOTCONN;
4322 }
4323
4324 mutex_unlock(&fs_info->balance_mutex);
4325 return ret;
4326}
4327
a7e99c69
ID
4328int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
4329{
4330 mutex_lock(&fs_info->balance_mutex);
4331 if (!fs_info->balance_ctl) {
4332 mutex_unlock(&fs_info->balance_mutex);
4333 return -ENOTCONN;
4334 }
4335
cf7d20f4
DS
4336 /*
4337 * A paused balance with the item stored on disk can be resumed at
4338 * mount time if the mount is read-write. Otherwise it's still paused
4339 * and we must not allow cancelling as it deletes the item.
4340 */
4341 if (sb_rdonly(fs_info->sb)) {
4342 mutex_unlock(&fs_info->balance_mutex);
4343 return -EROFS;
4344 }
4345
a7e99c69
ID
4346 atomic_inc(&fs_info->balance_cancel_req);
4347 /*
4348 * if we are running just wait and return, balance item is
4349 * deleted in btrfs_balance in this case
4350 */
3009a62f 4351 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
a7e99c69
ID
4352 mutex_unlock(&fs_info->balance_mutex);
4353 wait_event(fs_info->balance_wait_q,
3009a62f 4354 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
a7e99c69
ID
4355 mutex_lock(&fs_info->balance_mutex);
4356 } else {
a7e99c69 4357 mutex_unlock(&fs_info->balance_mutex);
dccdb07b
DS
4358 /*
4359 * Lock released to allow other waiters to continue, we'll
4360 * reexamine the status again.
4361 */
a7e99c69
ID
4362 mutex_lock(&fs_info->balance_mutex);
4363
a17c95df 4364 if (fs_info->balance_ctl) {
149196a2 4365 reset_balance_state(fs_info);
c3e1f96c 4366 btrfs_exclop_finish(fs_info);
6dac13f8 4367 btrfs_info(fs_info, "balance: canceled");
a17c95df 4368 }
a7e99c69
ID
4369 }
4370
3009a62f
DS
4371 BUG_ON(fs_info->balance_ctl ||
4372 test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
a7e99c69
ID
4373 atomic_dec(&fs_info->balance_cancel_req);
4374 mutex_unlock(&fs_info->balance_mutex);
4375 return 0;
4376}
4377
97f4dd09 4378int btrfs_uuid_scan_kthread(void *data)
803b2f54
SB
4379{
4380 struct btrfs_fs_info *fs_info = data;
4381 struct btrfs_root *root = fs_info->tree_root;
4382 struct btrfs_key key;
803b2f54
SB
4383 struct btrfs_path *path = NULL;
4384 int ret = 0;
4385 struct extent_buffer *eb;
4386 int slot;
4387 struct btrfs_root_item root_item;
4388 u32 item_size;
f45388f3 4389 struct btrfs_trans_handle *trans = NULL;
c94bec2c 4390 bool closing = false;
803b2f54
SB
4391
4392 path = btrfs_alloc_path();
4393 if (!path) {
4394 ret = -ENOMEM;
4395 goto out;
4396 }
4397
4398 key.objectid = 0;
4399 key.type = BTRFS_ROOT_ITEM_KEY;
4400 key.offset = 0;
4401
803b2f54 4402 while (1) {
c94bec2c
JB
4403 if (btrfs_fs_closing(fs_info)) {
4404 closing = true;
4405 break;
4406 }
7c829b72
AJ
4407 ret = btrfs_search_forward(root, &key, path,
4408 BTRFS_OLDEST_GENERATION);
803b2f54
SB
4409 if (ret) {
4410 if (ret > 0)
4411 ret = 0;
4412 break;
4413 }
4414
4415 if (key.type != BTRFS_ROOT_ITEM_KEY ||
4416 (key.objectid < BTRFS_FIRST_FREE_OBJECTID &&
4417 key.objectid != BTRFS_FS_TREE_OBJECTID) ||
4418 key.objectid > BTRFS_LAST_FREE_OBJECTID)
4419 goto skip;
4420
4421 eb = path->nodes[0];
4422 slot = path->slots[0];
4423 item_size = btrfs_item_size_nr(eb, slot);
4424 if (item_size < sizeof(root_item))
4425 goto skip;
4426
803b2f54
SB
4427 read_extent_buffer(eb, &root_item,
4428 btrfs_item_ptr_offset(eb, slot),
4429 (int)sizeof(root_item));
4430 if (btrfs_root_refs(&root_item) == 0)
4431 goto skip;
f45388f3
FDBM
4432
4433 if (!btrfs_is_empty_uuid(root_item.uuid) ||
4434 !btrfs_is_empty_uuid(root_item.received_uuid)) {
4435 if (trans)
4436 goto update_tree;
4437
4438 btrfs_release_path(path);
803b2f54
SB
4439 /*
4440 * 1 - subvol uuid item
4441 * 1 - received_subvol uuid item
4442 */
4443 trans = btrfs_start_transaction(fs_info->uuid_root, 2);
4444 if (IS_ERR(trans)) {
4445 ret = PTR_ERR(trans);
4446 break;
4447 }
f45388f3
FDBM
4448 continue;
4449 } else {
4450 goto skip;
4451 }
4452update_tree:
9771a5cf 4453 btrfs_release_path(path);
f45388f3 4454 if (!btrfs_is_empty_uuid(root_item.uuid)) {
cdb345a8 4455 ret = btrfs_uuid_tree_add(trans, root_item.uuid,
803b2f54
SB
4456 BTRFS_UUID_KEY_SUBVOL,
4457 key.objectid);
4458 if (ret < 0) {
efe120a0 4459 btrfs_warn(fs_info, "uuid_tree_add failed %d",
803b2f54 4460 ret);
803b2f54
SB
4461 break;
4462 }
4463 }
4464
4465 if (!btrfs_is_empty_uuid(root_item.received_uuid)) {
cdb345a8 4466 ret = btrfs_uuid_tree_add(trans,
803b2f54
SB
4467 root_item.received_uuid,
4468 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4469 key.objectid);
4470 if (ret < 0) {
efe120a0 4471 btrfs_warn(fs_info, "uuid_tree_add failed %d",
803b2f54 4472 ret);
803b2f54
SB
4473 break;
4474 }
4475 }
4476
f45388f3 4477skip:
9771a5cf 4478 btrfs_release_path(path);
803b2f54 4479 if (trans) {
3a45bb20 4480 ret = btrfs_end_transaction(trans);
f45388f3 4481 trans = NULL;
803b2f54
SB
4482 if (ret)
4483 break;
4484 }
4485
803b2f54
SB
4486 if (key.offset < (u64)-1) {
4487 key.offset++;
4488 } else if (key.type < BTRFS_ROOT_ITEM_KEY) {
4489 key.offset = 0;
4490 key.type = BTRFS_ROOT_ITEM_KEY;
4491 } else if (key.objectid < (u64)-1) {
4492 key.offset = 0;
4493 key.type = BTRFS_ROOT_ITEM_KEY;
4494 key.objectid++;
4495 } else {
4496 break;
4497 }
4498 cond_resched();
4499 }
4500
4501out:
4502 btrfs_free_path(path);
f45388f3 4503 if (trans && !IS_ERR(trans))
3a45bb20 4504 btrfs_end_transaction(trans);
803b2f54 4505 if (ret)
efe120a0 4506 btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret);
c94bec2c 4507 else if (!closing)
afcdd129 4508 set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags);
803b2f54
SB
4509 up(&fs_info->uuid_tree_rescan_sem);
4510 return 0;
4511}
4512
f7a81ea4
SB
4513int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info)
4514{
4515 struct btrfs_trans_handle *trans;
4516 struct btrfs_root *tree_root = fs_info->tree_root;
4517 struct btrfs_root *uuid_root;
803b2f54
SB
4518 struct task_struct *task;
4519 int ret;
f7a81ea4
SB
4520
4521 /*
4522 * 1 - root node
4523 * 1 - root item
4524 */
4525 trans = btrfs_start_transaction(tree_root, 2);
4526 if (IS_ERR(trans))
4527 return PTR_ERR(trans);
4528
9b7a2440 4529 uuid_root = btrfs_create_tree(trans, BTRFS_UUID_TREE_OBJECTID);
f7a81ea4 4530 if (IS_ERR(uuid_root)) {
6d13f549 4531 ret = PTR_ERR(uuid_root);
66642832 4532 btrfs_abort_transaction(trans, ret);
3a45bb20 4533 btrfs_end_transaction(trans);
6d13f549 4534 return ret;
f7a81ea4
SB
4535 }
4536
4537 fs_info->uuid_root = uuid_root;
4538
3a45bb20 4539 ret = btrfs_commit_transaction(trans);
803b2f54
SB
4540 if (ret)
4541 return ret;
4542
4543 down(&fs_info->uuid_tree_rescan_sem);
4544 task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid");
4545 if (IS_ERR(task)) {
70f80175 4546 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
efe120a0 4547 btrfs_warn(fs_info, "failed to start uuid_scan task");
803b2f54
SB
4548 up(&fs_info->uuid_tree_rescan_sem);
4549 return PTR_ERR(task);
4550 }
4551
4552 return 0;
f7a81ea4 4553}
803b2f54 4554
8f18cf13
CM
4555/*
4556 * shrinking a device means finding all of the device extents past
4557 * the new size, and then following the back refs to the chunks.
4558 * The chunk relocation code actually frees the device extent
4559 */
4560int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
4561{
0b246afa
JM
4562 struct btrfs_fs_info *fs_info = device->fs_info;
4563 struct btrfs_root *root = fs_info->dev_root;
8f18cf13 4564 struct btrfs_trans_handle *trans;
8f18cf13
CM
4565 struct btrfs_dev_extent *dev_extent = NULL;
4566 struct btrfs_path *path;
4567 u64 length;
8f18cf13
CM
4568 u64 chunk_offset;
4569 int ret;
4570 int slot;
ba1bf481
JB
4571 int failed = 0;
4572 bool retried = false;
8f18cf13
CM
4573 struct extent_buffer *l;
4574 struct btrfs_key key;
0b246afa 4575 struct btrfs_super_block *super_copy = fs_info->super_copy;
8f18cf13 4576 u64 old_total = btrfs_super_total_bytes(super_copy);
7cc8e58d 4577 u64 old_size = btrfs_device_get_total_bytes(device);
7dfb8be1 4578 u64 diff;
61d0d0d2 4579 u64 start;
7dfb8be1
NB
4580
4581 new_size = round_down(new_size, fs_info->sectorsize);
61d0d0d2 4582 start = new_size;
0e4324a4 4583 diff = round_down(old_size - new_size, fs_info->sectorsize);
8f18cf13 4584
401e29c1 4585 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
63a212ab
SB
4586 return -EINVAL;
4587
8f18cf13
CM
4588 path = btrfs_alloc_path();
4589 if (!path)
4590 return -ENOMEM;
4591
0338dff6 4592 path->reada = READA_BACK;
8f18cf13 4593
61d0d0d2
NB
4594 trans = btrfs_start_transaction(root, 0);
4595 if (IS_ERR(trans)) {
4596 btrfs_free_path(path);
4597 return PTR_ERR(trans);
4598 }
4599
34441361 4600 mutex_lock(&fs_info->chunk_mutex);
7d9eb12c 4601
7cc8e58d 4602 btrfs_device_set_total_bytes(device, new_size);
ebbede42 4603 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
2b82032c 4604 device->fs_devices->total_rw_bytes -= diff;
a5ed45f8 4605 atomic64_sub(diff, &fs_info->free_chunk_space);
2bf64758 4606 }
61d0d0d2
NB
4607
4608 /*
4609 * Once the device's size has been set to the new size, ensure all
4610 * in-memory chunks are synced to disk so that the loop below sees them
4611 * and relocates them accordingly.
4612 */
1c11b63e 4613 if (contains_pending_extent(device, &start, diff)) {
61d0d0d2
NB
4614 mutex_unlock(&fs_info->chunk_mutex);
4615 ret = btrfs_commit_transaction(trans);
4616 if (ret)
4617 goto done;
4618 } else {
4619 mutex_unlock(&fs_info->chunk_mutex);
4620 btrfs_end_transaction(trans);
4621 }
8f18cf13 4622
ba1bf481 4623again:
8f18cf13
CM
4624 key.objectid = device->devid;
4625 key.offset = (u64)-1;
4626 key.type = BTRFS_DEV_EXTENT_KEY;
4627
213e64da 4628 do {
0b246afa 4629 mutex_lock(&fs_info->delete_unused_bgs_mutex);
8f18cf13 4630 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
67c5e7d4 4631 if (ret < 0) {
0b246afa 4632 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
8f18cf13 4633 goto done;
67c5e7d4 4634 }
8f18cf13
CM
4635
4636 ret = btrfs_previous_item(root, path, 0, key.type);
67c5e7d4 4637 if (ret)
0b246afa 4638 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
8f18cf13
CM
4639 if (ret < 0)
4640 goto done;
4641 if (ret) {
4642 ret = 0;
b3b4aa74 4643 btrfs_release_path(path);
bf1fb512 4644 break;
8f18cf13
CM
4645 }
4646
4647 l = path->nodes[0];
4648 slot = path->slots[0];
4649 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
4650
ba1bf481 4651 if (key.objectid != device->devid) {
0b246afa 4652 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
b3b4aa74 4653 btrfs_release_path(path);
bf1fb512 4654 break;
ba1bf481 4655 }
8f18cf13
CM
4656
4657 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
4658 length = btrfs_dev_extent_length(l, dev_extent);
4659
ba1bf481 4660 if (key.offset + length <= new_size) {
0b246afa 4661 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
b3b4aa74 4662 btrfs_release_path(path);
d6397bae 4663 break;
ba1bf481 4664 }
8f18cf13 4665
8f18cf13 4666 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
b3b4aa74 4667 btrfs_release_path(path);
8f18cf13 4668
a6f93c71
LB
4669 /*
4670 * We may be relocating the only data chunk we have,
4671 * which could potentially end up with losing data's
4672 * raid profile, so lets allocate an empty one in
4673 * advance.
4674 */
4675 ret = btrfs_may_alloc_data_chunk(fs_info, chunk_offset);
4676 if (ret < 0) {
4677 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
4678 goto done;
4679 }
4680
0b246afa
JM
4681 ret = btrfs_relocate_chunk(fs_info, chunk_offset);
4682 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
eede2bf3 4683 if (ret == -ENOSPC) {
ba1bf481 4684 failed++;
eede2bf3
OS
4685 } else if (ret) {
4686 if (ret == -ETXTBSY) {
4687 btrfs_warn(fs_info,
4688 "could not shrink block group %llu due to active swapfile",
4689 chunk_offset);
4690 }
4691 goto done;
4692 }
213e64da 4693 } while (key.offset-- > 0);
ba1bf481
JB
4694
4695 if (failed && !retried) {
4696 failed = 0;
4697 retried = true;
4698 goto again;
4699 } else if (failed && retried) {
4700 ret = -ENOSPC;
ba1bf481 4701 goto done;
8f18cf13
CM
4702 }
4703
d6397bae 4704 /* Shrinking succeeded, else we would be at "done". */
a22285a6 4705 trans = btrfs_start_transaction(root, 0);
98d5dc13
TI
4706 if (IS_ERR(trans)) {
4707 ret = PTR_ERR(trans);
4708 goto done;
4709 }
4710
34441361 4711 mutex_lock(&fs_info->chunk_mutex);
c57dd1f2
QW
4712 /* Clear all state bits beyond the shrunk device size */
4713 clear_extent_bits(&device->alloc_state, new_size, (u64)-1,
4714 CHUNK_STATE_MASK);
4715
7cc8e58d 4716 btrfs_device_set_disk_total_bytes(device, new_size);
bbbf7243
NB
4717 if (list_empty(&device->post_commit_list))
4718 list_add_tail(&device->post_commit_list,
4719 &trans->transaction->dev_update_list);
d6397bae 4720
d6397bae 4721 WARN_ON(diff > old_total);
7dfb8be1
NB
4722 btrfs_set_super_total_bytes(super_copy,
4723 round_down(old_total - diff, fs_info->sectorsize));
34441361 4724 mutex_unlock(&fs_info->chunk_mutex);
2196d6e8
MX
4725
4726 /* Now btrfs_update_device() will change the on-disk size. */
4727 ret = btrfs_update_device(trans, device);
801660b0
AJ
4728 if (ret < 0) {
4729 btrfs_abort_transaction(trans, ret);
4730 btrfs_end_transaction(trans);
4731 } else {
4732 ret = btrfs_commit_transaction(trans);
4733 }
8f18cf13
CM
4734done:
4735 btrfs_free_path(path);
53e489bc 4736 if (ret) {
34441361 4737 mutex_lock(&fs_info->chunk_mutex);
53e489bc 4738 btrfs_device_set_total_bytes(device, old_size);
ebbede42 4739 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
53e489bc 4740 device->fs_devices->total_rw_bytes += diff;
a5ed45f8 4741 atomic64_add(diff, &fs_info->free_chunk_space);
34441361 4742 mutex_unlock(&fs_info->chunk_mutex);
53e489bc 4743 }
8f18cf13
CM
4744 return ret;
4745}
4746
2ff7e61e 4747static int btrfs_add_system_chunk(struct btrfs_fs_info *fs_info,
0b86a832
CM
4748 struct btrfs_key *key,
4749 struct btrfs_chunk *chunk, int item_size)
4750{
0b246afa 4751 struct btrfs_super_block *super_copy = fs_info->super_copy;
0b86a832
CM
4752 struct btrfs_disk_key disk_key;
4753 u32 array_size;
4754 u8 *ptr;
4755
34441361 4756 mutex_lock(&fs_info->chunk_mutex);
0b86a832 4757 array_size = btrfs_super_sys_array_size(super_copy);
5f43f86e 4758 if (array_size + item_size + sizeof(disk_key)
fe48a5c0 4759 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
34441361 4760 mutex_unlock(&fs_info->chunk_mutex);
0b86a832 4761 return -EFBIG;
fe48a5c0 4762 }
0b86a832
CM
4763
4764 ptr = super_copy->sys_chunk_array + array_size;
4765 btrfs_cpu_key_to_disk(&disk_key, key);
4766 memcpy(ptr, &disk_key, sizeof(disk_key));
4767 ptr += sizeof(disk_key);
4768 memcpy(ptr, chunk, item_size);
4769 item_size += sizeof(disk_key);
4770 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
34441361 4771 mutex_unlock(&fs_info->chunk_mutex);
fe48a5c0 4772
0b86a832
CM
4773 return 0;
4774}
4775
73c5de00
AJ
4776/*
4777 * sort the devices in descending order by max_avail, total_avail
4778 */
4779static int btrfs_cmp_device_info(const void *a, const void *b)
9b3f68b9 4780{
73c5de00
AJ
4781 const struct btrfs_device_info *di_a = a;
4782 const struct btrfs_device_info *di_b = b;
9b3f68b9 4783
73c5de00 4784 if (di_a->max_avail > di_b->max_avail)
b2117a39 4785 return -1;
73c5de00 4786 if (di_a->max_avail < di_b->max_avail)
b2117a39 4787 return 1;
73c5de00
AJ
4788 if (di_a->total_avail > di_b->total_avail)
4789 return -1;
4790 if (di_a->total_avail < di_b->total_avail)
4791 return 1;
4792 return 0;
b2117a39 4793}
0b86a832 4794
53b381b3
DW
4795static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
4796{
ffe2d203 4797 if (!(type & BTRFS_BLOCK_GROUP_RAID56_MASK))
53b381b3
DW
4798 return;
4799
ceda0864 4800 btrfs_set_fs_incompat(info, RAID56);
53b381b3
DW
4801}
4802
cfbb825c
DS
4803static void check_raid1c34_incompat_flag(struct btrfs_fs_info *info, u64 type)
4804{
4805 if (!(type & (BTRFS_BLOCK_GROUP_RAID1C3 | BTRFS_BLOCK_GROUP_RAID1C4)))
4806 return;
4807
4808 btrfs_set_fs_incompat(info, RAID1C34);
4809}
4810
4f2bafe8
NA
4811/*
4812 * Structure used internally for __btrfs_alloc_chunk() function.
4813 * Wraps needed parameters.
4814 */
4815struct alloc_chunk_ctl {
4816 u64 start;
4817 u64 type;
4818 /* Total number of stripes to allocate */
4819 int num_stripes;
4820 /* sub_stripes info for map */
4821 int sub_stripes;
4822 /* Stripes per device */
4823 int dev_stripes;
4824 /* Maximum number of devices to use */
4825 int devs_max;
4826 /* Minimum number of devices to use */
4827 int devs_min;
4828 /* ndevs has to be a multiple of this */
4829 int devs_increment;
4830 /* Number of copies */
4831 int ncopies;
4832 /* Number of stripes worth of bytes to store parity information */
4833 int nparity;
4834 u64 max_stripe_size;
4835 u64 max_chunk_size;
6aafb303 4836 u64 dev_extent_min;
4f2bafe8
NA
4837 u64 stripe_size;
4838 u64 chunk_size;
4839 int ndevs;
4840};
4841
27c314d5
NA
4842static void init_alloc_chunk_ctl_policy_regular(
4843 struct btrfs_fs_devices *fs_devices,
4844 struct alloc_chunk_ctl *ctl)
4845{
4846 u64 type = ctl->type;
4847
4848 if (type & BTRFS_BLOCK_GROUP_DATA) {
4849 ctl->max_stripe_size = SZ_1G;
4850 ctl->max_chunk_size = BTRFS_MAX_DATA_CHUNK_SIZE;
4851 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
4852 /* For larger filesystems, use larger metadata chunks */
4853 if (fs_devices->total_rw_bytes > 50ULL * SZ_1G)
4854 ctl->max_stripe_size = SZ_1G;
4855 else
4856 ctl->max_stripe_size = SZ_256M;
4857 ctl->max_chunk_size = ctl->max_stripe_size;
4858 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
4859 ctl->max_stripe_size = SZ_32M;
4860 ctl->max_chunk_size = 2 * ctl->max_stripe_size;
4861 ctl->devs_max = min_t(int, ctl->devs_max,
4862 BTRFS_MAX_DEVS_SYS_CHUNK);
4863 } else {
4864 BUG();
4865 }
4866
4867 /* We don't want a chunk larger than 10% of writable space */
4868 ctl->max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
4869 ctl->max_chunk_size);
6aafb303 4870 ctl->dev_extent_min = BTRFS_STRIPE_LEN * ctl->dev_stripes;
27c314d5
NA
4871}
4872
4873static void init_alloc_chunk_ctl(struct btrfs_fs_devices *fs_devices,
4874 struct alloc_chunk_ctl *ctl)
4875{
4876 int index = btrfs_bg_flags_to_raid_index(ctl->type);
4877
4878 ctl->sub_stripes = btrfs_raid_array[index].sub_stripes;
4879 ctl->dev_stripes = btrfs_raid_array[index].dev_stripes;
4880 ctl->devs_max = btrfs_raid_array[index].devs_max;
4881 if (!ctl->devs_max)
4882 ctl->devs_max = BTRFS_MAX_DEVS(fs_devices->fs_info);
4883 ctl->devs_min = btrfs_raid_array[index].devs_min;
4884 ctl->devs_increment = btrfs_raid_array[index].devs_increment;
4885 ctl->ncopies = btrfs_raid_array[index].ncopies;
4886 ctl->nparity = btrfs_raid_array[index].nparity;
4887 ctl->ndevs = 0;
4888
4889 switch (fs_devices->chunk_alloc_policy) {
4890 case BTRFS_CHUNK_ALLOC_REGULAR:
4891 init_alloc_chunk_ctl_policy_regular(fs_devices, ctl);
4892 break;
4893 default:
4894 BUG();
4895 }
4896}
4897
560156cb
NA
4898static int gather_device_info(struct btrfs_fs_devices *fs_devices,
4899 struct alloc_chunk_ctl *ctl,
4900 struct btrfs_device_info *devices_info)
b2117a39 4901{
560156cb 4902 struct btrfs_fs_info *info = fs_devices->fs_info;
ebcc9301 4903 struct btrfs_device *device;
73c5de00 4904 u64 total_avail;
560156cb 4905 u64 dev_extent_want = ctl->max_stripe_size * ctl->dev_stripes;
73c5de00 4906 int ret;
560156cb
NA
4907 int ndevs = 0;
4908 u64 max_avail;
4909 u64 dev_offset;
0cad8a11 4910
9f680ce0 4911 /*
73c5de00
AJ
4912 * in the first pass through the devices list, we gather information
4913 * about the available holes on each device.
9f680ce0 4914 */
ebcc9301 4915 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
ebbede42 4916 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
31b1a2bd 4917 WARN(1, KERN_ERR
efe120a0 4918 "BTRFS: read-only device in alloc_list\n");
73c5de00
AJ
4919 continue;
4920 }
b2117a39 4921
e12c9621
AJ
4922 if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
4923 &device->dev_state) ||
401e29c1 4924 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
73c5de00 4925 continue;
b2117a39 4926
73c5de00
AJ
4927 if (device->total_bytes > device->bytes_used)
4928 total_avail = device->total_bytes - device->bytes_used;
4929 else
4930 total_avail = 0;
38c01b96 4931
4932 /* If there is no space on this device, skip it. */
6aafb303 4933 if (total_avail < ctl->dev_extent_min)
38c01b96 4934 continue;
b2117a39 4935
560156cb
NA
4936 ret = find_free_dev_extent(device, dev_extent_want, &dev_offset,
4937 &max_avail);
73c5de00 4938 if (ret && ret != -ENOSPC)
560156cb 4939 return ret;
b2117a39 4940
73c5de00 4941 if (ret == 0)
560156cb 4942 max_avail = dev_extent_want;
b2117a39 4943
6aafb303 4944 if (max_avail < ctl->dev_extent_min) {
4117f207
QW
4945 if (btrfs_test_opt(info, ENOSPC_DEBUG))
4946 btrfs_debug(info,
560156cb 4947 "%s: devid %llu has no free space, have=%llu want=%llu",
4117f207 4948 __func__, device->devid, max_avail,
6aafb303 4949 ctl->dev_extent_min);
73c5de00 4950 continue;
4117f207 4951 }
b2117a39 4952
063d006f
ES
4953 if (ndevs == fs_devices->rw_devices) {
4954 WARN(1, "%s: found more than %llu devices\n",
4955 __func__, fs_devices->rw_devices);
4956 break;
4957 }
73c5de00
AJ
4958 devices_info[ndevs].dev_offset = dev_offset;
4959 devices_info[ndevs].max_avail = max_avail;
4960 devices_info[ndevs].total_avail = total_avail;
4961 devices_info[ndevs].dev = device;
4962 ++ndevs;
4963 }
560156cb 4964 ctl->ndevs = ndevs;
b2117a39 4965
73c5de00
AJ
4966 /*
4967 * now sort the devices by hole size / available space
4968 */
560156cb 4969 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
73c5de00 4970 btrfs_cmp_device_info, NULL);
b2117a39 4971
560156cb
NA
4972 return 0;
4973}
4974
5badf512
NA
4975static int decide_stripe_size_regular(struct alloc_chunk_ctl *ctl,
4976 struct btrfs_device_info *devices_info)
4977{
4978 /* Number of stripes that count for block group size */
4979 int data_stripes;
4980
4981 /*
4982 * The primary goal is to maximize the number of stripes, so use as
4983 * many devices as possible, even if the stripes are not maximum sized.
4984 *
4985 * The DUP profile stores more than one stripe per device, the
4986 * max_avail is the total size so we have to adjust.
4987 */
4988 ctl->stripe_size = div_u64(devices_info[ctl->ndevs - 1].max_avail,
4989 ctl->dev_stripes);
4990 ctl->num_stripes = ctl->ndevs * ctl->dev_stripes;
4991
4992 /* This will have to be fixed for RAID1 and RAID10 over more drives */
4993 data_stripes = (ctl->num_stripes - ctl->nparity) / ctl->ncopies;
4994
4995 /*
4996 * Use the number of data stripes to figure out how big this chunk is
4997 * really going to be in terms of logical address space, and compare
4998 * that answer with the max chunk size. If it's higher, we try to
4999 * reduce stripe_size.
5000 */
5001 if (ctl->stripe_size * data_stripes > ctl->max_chunk_size) {
5002 /*
5003 * Reduce stripe_size, round it up to a 16MB boundary again and
5004 * then use it, unless it ends up being even bigger than the
5005 * previous value we had already.
5006 */
5007 ctl->stripe_size = min(round_up(div_u64(ctl->max_chunk_size,
5008 data_stripes), SZ_16M),
5009 ctl->stripe_size);
5010 }
5011
5012 /* Align to BTRFS_STRIPE_LEN */
5013 ctl->stripe_size = round_down(ctl->stripe_size, BTRFS_STRIPE_LEN);
5014 ctl->chunk_size = ctl->stripe_size * data_stripes;
5015
5016 return 0;
5017}
5018
5019static int decide_stripe_size(struct btrfs_fs_devices *fs_devices,
5020 struct alloc_chunk_ctl *ctl,
5021 struct btrfs_device_info *devices_info)
5022{
5023 struct btrfs_fs_info *info = fs_devices->fs_info;
5024
5025 /*
5026 * Round down to number of usable stripes, devs_increment can be any
5027 * number so we can't use round_down() that requires power of 2, while
5028 * rounddown is safe.
5029 */
5030 ctl->ndevs = rounddown(ctl->ndevs, ctl->devs_increment);
5031
5032 if (ctl->ndevs < ctl->devs_min) {
5033 if (btrfs_test_opt(info, ENOSPC_DEBUG)) {
5034 btrfs_debug(info,
5035 "%s: not enough devices with free space: have=%d minimum required=%d",
5036 __func__, ctl->ndevs, ctl->devs_min);
5037 }
5038 return -ENOSPC;
5039 }
5040
5041 ctl->ndevs = min(ctl->ndevs, ctl->devs_max);
5042
5043 switch (fs_devices->chunk_alloc_policy) {
5044 case BTRFS_CHUNK_ALLOC_REGULAR:
5045 return decide_stripe_size_regular(ctl, devices_info);
5046 default:
5047 BUG();
5048 }
5049}
5050
dce580ca
NA
5051static int create_chunk(struct btrfs_trans_handle *trans,
5052 struct alloc_chunk_ctl *ctl,
5053 struct btrfs_device_info *devices_info)
560156cb
NA
5054{
5055 struct btrfs_fs_info *info = trans->fs_info;
560156cb
NA
5056 struct map_lookup *map = NULL;
5057 struct extent_map_tree *em_tree;
5058 struct extent_map *em;
dce580ca
NA
5059 u64 start = ctl->start;
5060 u64 type = ctl->type;
560156cb
NA
5061 int ret;
5062 int i;
5063 int j;
5064
dce580ca
NA
5065 map = kmalloc(map_lookup_size(ctl->num_stripes), GFP_NOFS);
5066 if (!map)
560156cb 5067 return -ENOMEM;
dce580ca 5068 map->num_stripes = ctl->num_stripes;
560156cb 5069
dce580ca
NA
5070 for (i = 0; i < ctl->ndevs; ++i) {
5071 for (j = 0; j < ctl->dev_stripes; ++j) {
5072 int s = i * ctl->dev_stripes + j;
73c5de00
AJ
5073 map->stripes[s].dev = devices_info[i].dev;
5074 map->stripes[s].physical = devices_info[i].dev_offset +
dce580ca 5075 j * ctl->stripe_size;
6324fbf3 5076 }
6324fbf3 5077 }
500ceed8
NB
5078 map->stripe_len = BTRFS_STRIPE_LEN;
5079 map->io_align = BTRFS_STRIPE_LEN;
5080 map->io_width = BTRFS_STRIPE_LEN;
2b82032c 5081 map->type = type;
dce580ca 5082 map->sub_stripes = ctl->sub_stripes;
0b86a832 5083
dce580ca 5084 trace_btrfs_chunk_alloc(info, map, start, ctl->chunk_size);
1abe9b8a 5085
172ddd60 5086 em = alloc_extent_map();
2b82032c 5087 if (!em) {
298a8f9c 5088 kfree(map);
dce580ca 5089 return -ENOMEM;
593060d7 5090 }
298a8f9c 5091 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
95617d69 5092 em->map_lookup = map;
2b82032c 5093 em->start = start;
dce580ca 5094 em->len = ctl->chunk_size;
2b82032c
YZ
5095 em->block_start = 0;
5096 em->block_len = em->len;
dce580ca 5097 em->orig_block_len = ctl->stripe_size;
593060d7 5098
c8bf1b67 5099 em_tree = &info->mapping_tree;
890871be 5100 write_lock(&em_tree->lock);
09a2a8f9 5101 ret = add_extent_mapping(em_tree, em, 0);
0f5d42b2 5102 if (ret) {
1efb72a3 5103 write_unlock(&em_tree->lock);
0f5d42b2 5104 free_extent_map(em);
dce580ca 5105 return ret;
0f5d42b2 5106 }
1efb72a3
NB
5107 write_unlock(&em_tree->lock);
5108
dce580ca 5109 ret = btrfs_make_block_group(trans, 0, type, start, ctl->chunk_size);
6df9a95e
JB
5110 if (ret)
5111 goto error_del_extent;
2b82032c 5112
bbbf7243
NB
5113 for (i = 0; i < map->num_stripes; i++) {
5114 struct btrfs_device *dev = map->stripes[i].dev;
5115
4f2bafe8 5116 btrfs_device_set_bytes_used(dev,
dce580ca 5117 dev->bytes_used + ctl->stripe_size);
bbbf7243
NB
5118 if (list_empty(&dev->post_commit_list))
5119 list_add_tail(&dev->post_commit_list,
5120 &trans->transaction->dev_update_list);
5121 }
43530c46 5122
dce580ca 5123 atomic64_sub(ctl->stripe_size * map->num_stripes,
4f2bafe8 5124 &info->free_chunk_space);
1c116187 5125
0f5d42b2 5126 free_extent_map(em);
0b246afa 5127 check_raid56_incompat_flag(info, type);
cfbb825c 5128 check_raid1c34_incompat_flag(info, type);
53b381b3 5129
2b82032c 5130 return 0;
b2117a39 5131
6df9a95e 5132error_del_extent:
0f5d42b2
JB
5133 write_lock(&em_tree->lock);
5134 remove_extent_mapping(em_tree, em);
5135 write_unlock(&em_tree->lock);
5136
5137 /* One for our allocation */
5138 free_extent_map(em);
5139 /* One for the tree reference */
5140 free_extent_map(em);
dce580ca
NA
5141
5142 return ret;
5143}
5144
11c67b1a 5145int btrfs_alloc_chunk(struct btrfs_trans_handle *trans, u64 type)
dce580ca
NA
5146{
5147 struct btrfs_fs_info *info = trans->fs_info;
5148 struct btrfs_fs_devices *fs_devices = info->fs_devices;
5149 struct btrfs_device_info *devices_info = NULL;
5150 struct alloc_chunk_ctl ctl;
5151 int ret;
5152
11c67b1a
NB
5153 lockdep_assert_held(&info->chunk_mutex);
5154
dce580ca
NA
5155 if (!alloc_profile_is_valid(type, 0)) {
5156 ASSERT(0);
5157 return -EINVAL;
5158 }
5159
5160 if (list_empty(&fs_devices->alloc_list)) {
5161 if (btrfs_test_opt(info, ENOSPC_DEBUG))
5162 btrfs_debug(info, "%s: no writable device", __func__);
5163 return -ENOSPC;
5164 }
5165
5166 if (!(type & BTRFS_BLOCK_GROUP_TYPE_MASK)) {
5167 btrfs_err(info, "invalid chunk type 0x%llx requested", type);
5168 ASSERT(0);
5169 return -EINVAL;
5170 }
5171
11c67b1a 5172 ctl.start = find_next_chunk(info);
dce580ca
NA
5173 ctl.type = type;
5174 init_alloc_chunk_ctl(fs_devices, &ctl);
5175
5176 devices_info = kcalloc(fs_devices->rw_devices, sizeof(*devices_info),
5177 GFP_NOFS);
5178 if (!devices_info)
5179 return -ENOMEM;
5180
5181 ret = gather_device_info(fs_devices, &ctl, devices_info);
5182 if (ret < 0)
5183 goto out;
5184
5185 ret = decide_stripe_size(fs_devices, &ctl, devices_info);
5186 if (ret < 0)
5187 goto out;
5188
5189 ret = create_chunk(trans, &ctl, devices_info);
5190
5191out:
b2117a39
MX
5192 kfree(devices_info);
5193 return ret;
2b82032c
YZ
5194}
5195
11c67b1a
NB
5196/*
5197 * Chunk allocation falls into two parts. The first part does work
5198 * that makes the new allocated chunk usable, but does not do any operation
5199 * that modifies the chunk tree. The second part does the work that
5200 * requires modifying the chunk tree. This division is important for the
5201 * bootstrap process of adding storage to a seed btrfs.
5202 */
6df9a95e 5203int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
97aff912 5204 u64 chunk_offset, u64 chunk_size)
2b82032c 5205{
97aff912 5206 struct btrfs_fs_info *fs_info = trans->fs_info;
6bccf3ab
JM
5207 struct btrfs_root *extent_root = fs_info->extent_root;
5208 struct btrfs_root *chunk_root = fs_info->chunk_root;
2b82032c 5209 struct btrfs_key key;
2b82032c
YZ
5210 struct btrfs_device *device;
5211 struct btrfs_chunk *chunk;
5212 struct btrfs_stripe *stripe;
6df9a95e
JB
5213 struct extent_map *em;
5214 struct map_lookup *map;
5215 size_t item_size;
5216 u64 dev_offset;
5217 u64 stripe_size;
5218 int i = 0;
140e639f 5219 int ret = 0;
2b82032c 5220
60ca842e 5221 em = btrfs_get_chunk_map(fs_info, chunk_offset, chunk_size);
592d92ee
LB
5222 if (IS_ERR(em))
5223 return PTR_ERR(em);
6df9a95e 5224
95617d69 5225 map = em->map_lookup;
6df9a95e
JB
5226 item_size = btrfs_chunk_item_size(map->num_stripes);
5227 stripe_size = em->orig_block_len;
5228
2b82032c 5229 chunk = kzalloc(item_size, GFP_NOFS);
6df9a95e
JB
5230 if (!chunk) {
5231 ret = -ENOMEM;
5232 goto out;
5233 }
5234
50460e37
FM
5235 /*
5236 * Take the device list mutex to prevent races with the final phase of
5237 * a device replace operation that replaces the device object associated
5238 * with the map's stripes, because the device object's id can change
5239 * at any time during that final phase of the device replace operation
5240 * (dev-replace.c:btrfs_dev_replace_finishing()).
5241 */
0b246afa 5242 mutex_lock(&fs_info->fs_devices->device_list_mutex);
6df9a95e
JB
5243 for (i = 0; i < map->num_stripes; i++) {
5244 device = map->stripes[i].dev;
5245 dev_offset = map->stripes[i].physical;
2b82032c 5246
0b86a832 5247 ret = btrfs_update_device(trans, device);
3acd3953 5248 if (ret)
50460e37 5249 break;
b5d9071c
NB
5250 ret = btrfs_alloc_dev_extent(trans, device, chunk_offset,
5251 dev_offset, stripe_size);
6df9a95e 5252 if (ret)
50460e37
FM
5253 break;
5254 }
5255 if (ret) {
0b246afa 5256 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
50460e37 5257 goto out;
2b82032c
YZ
5258 }
5259
2b82032c 5260 stripe = &chunk->stripe;
6df9a95e
JB
5261 for (i = 0; i < map->num_stripes; i++) {
5262 device = map->stripes[i].dev;
5263 dev_offset = map->stripes[i].physical;
0b86a832 5264
e17cade2
CM
5265 btrfs_set_stack_stripe_devid(stripe, device->devid);
5266 btrfs_set_stack_stripe_offset(stripe, dev_offset);
5267 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
2b82032c 5268 stripe++;
0b86a832 5269 }
0b246afa 5270 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
0b86a832 5271
2b82032c 5272 btrfs_set_stack_chunk_length(chunk, chunk_size);
0b86a832 5273 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
2b82032c
YZ
5274 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
5275 btrfs_set_stack_chunk_type(chunk, map->type);
5276 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
5277 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
5278 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
0b246afa 5279 btrfs_set_stack_chunk_sector_size(chunk, fs_info->sectorsize);
2b82032c 5280 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
0b86a832 5281
2b82032c
YZ
5282 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
5283 key.type = BTRFS_CHUNK_ITEM_KEY;
5284 key.offset = chunk_offset;
0b86a832 5285
2b82032c 5286 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
4ed1d16e
MF
5287 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
5288 /*
5289 * TODO: Cleanup of inserted chunk root in case of
5290 * failure.
5291 */
2ff7e61e 5292 ret = btrfs_add_system_chunk(fs_info, &key, chunk, item_size);
8f18cf13 5293 }
1abe9b8a 5294
6df9a95e 5295out:
0b86a832 5296 kfree(chunk);
6df9a95e 5297 free_extent_map(em);
4ed1d16e 5298 return ret;
2b82032c 5299}
0b86a832 5300
6f8e0fc7 5301static noinline int init_first_rw_device(struct btrfs_trans_handle *trans)
2b82032c 5302{
6f8e0fc7 5303 struct btrfs_fs_info *fs_info = trans->fs_info;
2b82032c 5304 u64 alloc_profile;
2b82032c
YZ
5305 int ret;
5306
1b86826d 5307 alloc_profile = btrfs_metadata_alloc_profile(fs_info);
11c67b1a 5308 ret = btrfs_alloc_chunk(trans, alloc_profile);
79787eaa
JM
5309 if (ret)
5310 return ret;
2b82032c 5311
1b86826d 5312 alloc_profile = btrfs_system_alloc_profile(fs_info);
11c67b1a 5313 ret = btrfs_alloc_chunk(trans, alloc_profile);
79787eaa 5314 return ret;
2b82032c
YZ
5315}
5316
d20983b4
MX
5317static inline int btrfs_chunk_max_errors(struct map_lookup *map)
5318{
fc9a2ac7 5319 const int index = btrfs_bg_flags_to_raid_index(map->type);
2b82032c 5320
fc9a2ac7 5321 return btrfs_raid_array[index].tolerated_failures;
2b82032c
YZ
5322}
5323
2ff7e61e 5324int btrfs_chunk_readonly(struct btrfs_fs_info *fs_info, u64 chunk_offset)
2b82032c
YZ
5325{
5326 struct extent_map *em;
5327 struct map_lookup *map;
2b82032c 5328 int readonly = 0;
d20983b4 5329 int miss_ndevs = 0;
2b82032c
YZ
5330 int i;
5331
60ca842e 5332 em = btrfs_get_chunk_map(fs_info, chunk_offset, 1);
592d92ee 5333 if (IS_ERR(em))
2b82032c
YZ
5334 return 1;
5335
95617d69 5336 map = em->map_lookup;
2b82032c 5337 for (i = 0; i < map->num_stripes; i++) {
e6e674bd
AJ
5338 if (test_bit(BTRFS_DEV_STATE_MISSING,
5339 &map->stripes[i].dev->dev_state)) {
d20983b4
MX
5340 miss_ndevs++;
5341 continue;
5342 }
ebbede42
AJ
5343 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE,
5344 &map->stripes[i].dev->dev_state)) {
2b82032c 5345 readonly = 1;
d20983b4 5346 goto end;
2b82032c
YZ
5347 }
5348 }
d20983b4
MX
5349
5350 /*
5351 * If the number of missing devices is larger than max errors,
5352 * we can not write the data into that chunk successfully, so
5353 * set it readonly.
5354 */
5355 if (miss_ndevs > btrfs_chunk_max_errors(map))
5356 readonly = 1;
5357end:
0b86a832 5358 free_extent_map(em);
2b82032c 5359 return readonly;
0b86a832
CM
5360}
5361
c8bf1b67 5362void btrfs_mapping_tree_free(struct extent_map_tree *tree)
0b86a832
CM
5363{
5364 struct extent_map *em;
5365
d397712b 5366 while (1) {
c8bf1b67
DS
5367 write_lock(&tree->lock);
5368 em = lookup_extent_mapping(tree, 0, (u64)-1);
0b86a832 5369 if (em)
c8bf1b67
DS
5370 remove_extent_mapping(tree, em);
5371 write_unlock(&tree->lock);
0b86a832
CM
5372 if (!em)
5373 break;
0b86a832
CM
5374 /* once for us */
5375 free_extent_map(em);
5376 /* once for the tree */
5377 free_extent_map(em);
5378 }
5379}
5380
5d964051 5381int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
f188591e
CM
5382{
5383 struct extent_map *em;
5384 struct map_lookup *map;
f188591e
CM
5385 int ret;
5386
60ca842e 5387 em = btrfs_get_chunk_map(fs_info, logical, len);
592d92ee
LB
5388 if (IS_ERR(em))
5389 /*
5390 * We could return errors for these cases, but that could get
5391 * ugly and we'd probably do the same thing which is just not do
5392 * anything else and exit, so return 1 so the callers don't try
5393 * to use other copies.
5394 */
fb7669b5 5395 return 1;
fb7669b5 5396
95617d69 5397 map = em->map_lookup;
c7369b3f 5398 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1_MASK))
f188591e 5399 ret = map->num_stripes;
321aecc6
CM
5400 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5401 ret = map->sub_stripes;
53b381b3
DW
5402 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
5403 ret = 2;
5404 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
8810f751
LB
5405 /*
5406 * There could be two corrupted data stripes, we need
5407 * to loop retry in order to rebuild the correct data.
e7e02096 5408 *
8810f751
LB
5409 * Fail a stripe at a time on every retry except the
5410 * stripe under reconstruction.
5411 */
5412 ret = map->num_stripes;
f188591e
CM
5413 else
5414 ret = 1;
5415 free_extent_map(em);
ad6d620e 5416
cb5583dd 5417 down_read(&fs_info->dev_replace.rwsem);
6fad823f
LB
5418 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace) &&
5419 fs_info->dev_replace.tgtdev)
ad6d620e 5420 ret++;
cb5583dd 5421 up_read(&fs_info->dev_replace.rwsem);
ad6d620e 5422
f188591e
CM
5423 return ret;
5424}
5425
2ff7e61e 5426unsigned long btrfs_full_stripe_len(struct btrfs_fs_info *fs_info,
53b381b3
DW
5427 u64 logical)
5428{
5429 struct extent_map *em;
5430 struct map_lookup *map;
0b246afa 5431 unsigned long len = fs_info->sectorsize;
53b381b3 5432
60ca842e 5433 em = btrfs_get_chunk_map(fs_info, logical, len);
53b381b3 5434
69f03f13
NB
5435 if (!WARN_ON(IS_ERR(em))) {
5436 map = em->map_lookup;
5437 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
5438 len = map->stripe_len * nr_data_stripes(map);
5439 free_extent_map(em);
5440 }
53b381b3
DW
5441 return len;
5442}
5443
e4ff5fb5 5444int btrfs_is_parity_mirror(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
53b381b3
DW
5445{
5446 struct extent_map *em;
5447 struct map_lookup *map;
53b381b3
DW
5448 int ret = 0;
5449
60ca842e 5450 em = btrfs_get_chunk_map(fs_info, logical, len);
53b381b3 5451
69f03f13
NB
5452 if(!WARN_ON(IS_ERR(em))) {
5453 map = em->map_lookup;
5454 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
5455 ret = 1;
5456 free_extent_map(em);
5457 }
53b381b3
DW
5458 return ret;
5459}
5460
30d9861f 5461static int find_live_mirror(struct btrfs_fs_info *fs_info,
99f92a7c 5462 struct map_lookup *map, int first,
8ba0ae78 5463 int dev_replace_is_ongoing)
dfe25020
CM
5464{
5465 int i;
99f92a7c 5466 int num_stripes;
8ba0ae78 5467 int preferred_mirror;
30d9861f
SB
5468 int tolerance;
5469 struct btrfs_device *srcdev;
5470
99f92a7c 5471 ASSERT((map->type &
c7369b3f 5472 (BTRFS_BLOCK_GROUP_RAID1_MASK | BTRFS_BLOCK_GROUP_RAID10)));
99f92a7c
AJ
5473
5474 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5475 num_stripes = map->sub_stripes;
5476 else
5477 num_stripes = map->num_stripes;
5478
8ba0ae78
AJ
5479 preferred_mirror = first + current->pid % num_stripes;
5480
30d9861f
SB
5481 if (dev_replace_is_ongoing &&
5482 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
5483 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
5484 srcdev = fs_info->dev_replace.srcdev;
5485 else
5486 srcdev = NULL;
5487
5488 /*
5489 * try to avoid the drive that is the source drive for a
5490 * dev-replace procedure, only choose it if no other non-missing
5491 * mirror is available
5492 */
5493 for (tolerance = 0; tolerance < 2; tolerance++) {
8ba0ae78
AJ
5494 if (map->stripes[preferred_mirror].dev->bdev &&
5495 (tolerance || map->stripes[preferred_mirror].dev != srcdev))
5496 return preferred_mirror;
99f92a7c 5497 for (i = first; i < first + num_stripes; i++) {
30d9861f
SB
5498 if (map->stripes[i].dev->bdev &&
5499 (tolerance || map->stripes[i].dev != srcdev))
5500 return i;
5501 }
dfe25020 5502 }
30d9861f 5503
dfe25020
CM
5504 /* we couldn't find one that doesn't fail. Just return something
5505 * and the io error handling code will clean up eventually
5506 */
8ba0ae78 5507 return preferred_mirror;
dfe25020
CM
5508}
5509
53b381b3 5510/* Bubble-sort the stripe set to put the parity/syndrome stripes last */
8e5cfb55 5511static void sort_parity_stripes(struct btrfs_bio *bbio, int num_stripes)
53b381b3 5512{
53b381b3 5513 int i;
53b381b3
DW
5514 int again = 1;
5515
5516 while (again) {
5517 again = 0;
cc7539ed 5518 for (i = 0; i < num_stripes - 1; i++) {
eeb6f172
DS
5519 /* Swap if parity is on a smaller index */
5520 if (bbio->raid_map[i] > bbio->raid_map[i + 1]) {
5521 swap(bbio->stripes[i], bbio->stripes[i + 1]);
5522 swap(bbio->raid_map[i], bbio->raid_map[i + 1]);
53b381b3
DW
5523 again = 1;
5524 }
5525 }
5526 }
5527}
5528
6e9606d2
ZL
5529static struct btrfs_bio *alloc_btrfs_bio(int total_stripes, int real_stripes)
5530{
5531 struct btrfs_bio *bbio = kzalloc(
e57cf21e 5532 /* the size of the btrfs_bio */
6e9606d2 5533 sizeof(struct btrfs_bio) +
e57cf21e 5534 /* plus the variable array for the stripes */
6e9606d2 5535 sizeof(struct btrfs_bio_stripe) * (total_stripes) +
e57cf21e 5536 /* plus the variable array for the tgt dev */
6e9606d2 5537 sizeof(int) * (real_stripes) +
e57cf21e
CM
5538 /*
5539 * plus the raid_map, which includes both the tgt dev
5540 * and the stripes
5541 */
5542 sizeof(u64) * (total_stripes),
277fb5fc 5543 GFP_NOFS|__GFP_NOFAIL);
6e9606d2
ZL
5544
5545 atomic_set(&bbio->error, 0);
140475ae 5546 refcount_set(&bbio->refs, 1);
6e9606d2 5547
608769a4
NB
5548 bbio->tgtdev_map = (int *)(bbio->stripes + total_stripes);
5549 bbio->raid_map = (u64 *)(bbio->tgtdev_map + real_stripes);
5550
6e9606d2
ZL
5551 return bbio;
5552}
5553
5554void btrfs_get_bbio(struct btrfs_bio *bbio)
5555{
140475ae
ER
5556 WARN_ON(!refcount_read(&bbio->refs));
5557 refcount_inc(&bbio->refs);
6e9606d2
ZL
5558}
5559
5560void btrfs_put_bbio(struct btrfs_bio *bbio)
5561{
5562 if (!bbio)
5563 return;
140475ae 5564 if (refcount_dec_and_test(&bbio->refs))
6e9606d2
ZL
5565 kfree(bbio);
5566}
5567
0b3d4cd3
LB
5568/* can REQ_OP_DISCARD be sent with other REQ like REQ_OP_WRITE? */
5569/*
5570 * Please note that, discard won't be sent to target device of device
5571 * replace.
5572 */
5573static int __btrfs_map_block_for_discard(struct btrfs_fs_info *fs_info,
6b7faadd 5574 u64 logical, u64 *length_ret,
0b3d4cd3
LB
5575 struct btrfs_bio **bbio_ret)
5576{
5577 struct extent_map *em;
5578 struct map_lookup *map;
5579 struct btrfs_bio *bbio;
6b7faadd 5580 u64 length = *length_ret;
0b3d4cd3
LB
5581 u64 offset;
5582 u64 stripe_nr;
5583 u64 stripe_nr_end;
5584 u64 stripe_end_offset;
5585 u64 stripe_cnt;
5586 u64 stripe_len;
5587 u64 stripe_offset;
5588 u64 num_stripes;
5589 u32 stripe_index;
5590 u32 factor = 0;
5591 u32 sub_stripes = 0;
5592 u64 stripes_per_dev = 0;
5593 u32 remaining_stripes = 0;
5594 u32 last_stripe = 0;
5595 int ret = 0;
5596 int i;
5597
5598 /* discard always return a bbio */
5599 ASSERT(bbio_ret);
5600
60ca842e 5601 em = btrfs_get_chunk_map(fs_info, logical, length);
0b3d4cd3
LB
5602 if (IS_ERR(em))
5603 return PTR_ERR(em);
5604
5605 map = em->map_lookup;
5606 /* we don't discard raid56 yet */
5607 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
5608 ret = -EOPNOTSUPP;
5609 goto out;
5610 }
5611
5612 offset = logical - em->start;
2d974619 5613 length = min_t(u64, em->start + em->len - logical, length);
6b7faadd 5614 *length_ret = length;
0b3d4cd3
LB
5615
5616 stripe_len = map->stripe_len;
5617 /*
5618 * stripe_nr counts the total number of stripes we have to stride
5619 * to get to this block
5620 */
5621 stripe_nr = div64_u64(offset, stripe_len);
5622
5623 /* stripe_offset is the offset of this block in its stripe */
5624 stripe_offset = offset - stripe_nr * stripe_len;
5625
5626 stripe_nr_end = round_up(offset + length, map->stripe_len);
42c61ab6 5627 stripe_nr_end = div64_u64(stripe_nr_end, map->stripe_len);
0b3d4cd3
LB
5628 stripe_cnt = stripe_nr_end - stripe_nr;
5629 stripe_end_offset = stripe_nr_end * map->stripe_len -
5630 (offset + length);
5631 /*
5632 * after this, stripe_nr is the number of stripes on this
5633 * device we have to walk to find the data, and stripe_index is
5634 * the number of our device in the stripe array
5635 */
5636 num_stripes = 1;
5637 stripe_index = 0;
5638 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5639 BTRFS_BLOCK_GROUP_RAID10)) {
5640 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5641 sub_stripes = 1;
5642 else
5643 sub_stripes = map->sub_stripes;
5644
5645 factor = map->num_stripes / sub_stripes;
5646 num_stripes = min_t(u64, map->num_stripes,
5647 sub_stripes * stripe_cnt);
5648 stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
5649 stripe_index *= sub_stripes;
5650 stripes_per_dev = div_u64_rem(stripe_cnt, factor,
5651 &remaining_stripes);
5652 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
5653 last_stripe *= sub_stripes;
c7369b3f 5654 } else if (map->type & (BTRFS_BLOCK_GROUP_RAID1_MASK |
0b3d4cd3
LB
5655 BTRFS_BLOCK_GROUP_DUP)) {
5656 num_stripes = map->num_stripes;
5657 } else {
5658 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5659 &stripe_index);
5660 }
5661
5662 bbio = alloc_btrfs_bio(num_stripes, 0);
5663 if (!bbio) {
5664 ret = -ENOMEM;
5665 goto out;
5666 }
5667
5668 for (i = 0; i < num_stripes; i++) {
5669 bbio->stripes[i].physical =
5670 map->stripes[stripe_index].physical +
5671 stripe_offset + stripe_nr * map->stripe_len;
5672 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
5673
5674 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5675 BTRFS_BLOCK_GROUP_RAID10)) {
5676 bbio->stripes[i].length = stripes_per_dev *
5677 map->stripe_len;
5678
5679 if (i / sub_stripes < remaining_stripes)
5680 bbio->stripes[i].length +=
5681 map->stripe_len;
5682
5683 /*
5684 * Special for the first stripe and
5685 * the last stripe:
5686 *
5687 * |-------|...|-------|
5688 * |----------|
5689 * off end_off
5690 */
5691 if (i < sub_stripes)
5692 bbio->stripes[i].length -=
5693 stripe_offset;
5694
5695 if (stripe_index >= last_stripe &&
5696 stripe_index <= (last_stripe +
5697 sub_stripes - 1))
5698 bbio->stripes[i].length -=
5699 stripe_end_offset;
5700
5701 if (i == sub_stripes - 1)
5702 stripe_offset = 0;
5703 } else {
5704 bbio->stripes[i].length = length;
5705 }
5706
5707 stripe_index++;
5708 if (stripe_index == map->num_stripes) {
5709 stripe_index = 0;
5710 stripe_nr++;
5711 }
5712 }
5713
5714 *bbio_ret = bbio;
5715 bbio->map_type = map->type;
5716 bbio->num_stripes = num_stripes;
5717out:
5718 free_extent_map(em);
5719 return ret;
5720}
5721
5ab56090
LB
5722/*
5723 * In dev-replace case, for repair case (that's the only case where the mirror
5724 * is selected explicitly when calling btrfs_map_block), blocks left of the
5725 * left cursor can also be read from the target drive.
5726 *
5727 * For REQ_GET_READ_MIRRORS, the target drive is added as the last one to the
5728 * array of stripes.
5729 * For READ, it also needs to be supported using the same mirror number.
5730 *
5731 * If the requested block is not left of the left cursor, EIO is returned. This
5732 * can happen because btrfs_num_copies() returns one more in the dev-replace
5733 * case.
5734 */
5735static int get_extra_mirror_from_replace(struct btrfs_fs_info *fs_info,
5736 u64 logical, u64 length,
5737 u64 srcdev_devid, int *mirror_num,
5738 u64 *physical)
5739{
5740 struct btrfs_bio *bbio = NULL;
5741 int num_stripes;
5742 int index_srcdev = 0;
5743 int found = 0;
5744 u64 physical_of_found = 0;
5745 int i;
5746 int ret = 0;
5747
5748 ret = __btrfs_map_block(fs_info, BTRFS_MAP_GET_READ_MIRRORS,
5749 logical, &length, &bbio, 0, 0);
5750 if (ret) {
5751 ASSERT(bbio == NULL);
5752 return ret;
5753 }
5754
5755 num_stripes = bbio->num_stripes;
5756 if (*mirror_num > num_stripes) {
5757 /*
5758 * BTRFS_MAP_GET_READ_MIRRORS does not contain this mirror,
5759 * that means that the requested area is not left of the left
5760 * cursor
5761 */
5762 btrfs_put_bbio(bbio);
5763 return -EIO;
5764 }
5765
5766 /*
5767 * process the rest of the function using the mirror_num of the source
5768 * drive. Therefore look it up first. At the end, patch the device
5769 * pointer to the one of the target drive.
5770 */
5771 for (i = 0; i < num_stripes; i++) {
5772 if (bbio->stripes[i].dev->devid != srcdev_devid)
5773 continue;
5774
5775 /*
5776 * In case of DUP, in order to keep it simple, only add the
5777 * mirror with the lowest physical address
5778 */
5779 if (found &&
5780 physical_of_found <= bbio->stripes[i].physical)
5781 continue;
5782
5783 index_srcdev = i;
5784 found = 1;
5785 physical_of_found = bbio->stripes[i].physical;
5786 }
5787
5788 btrfs_put_bbio(bbio);
5789
5790 ASSERT(found);
5791 if (!found)
5792 return -EIO;
5793
5794 *mirror_num = index_srcdev + 1;
5795 *physical = physical_of_found;
5796 return ret;
5797}
5798
73c0f228
LB
5799static void handle_ops_on_dev_replace(enum btrfs_map_op op,
5800 struct btrfs_bio **bbio_ret,
5801 struct btrfs_dev_replace *dev_replace,
5802 int *num_stripes_ret, int *max_errors_ret)
5803{
5804 struct btrfs_bio *bbio = *bbio_ret;
5805 u64 srcdev_devid = dev_replace->srcdev->devid;
5806 int tgtdev_indexes = 0;
5807 int num_stripes = *num_stripes_ret;
5808 int max_errors = *max_errors_ret;
5809 int i;
5810
5811 if (op == BTRFS_MAP_WRITE) {
5812 int index_where_to_add;
5813
5814 /*
5815 * duplicate the write operations while the dev replace
5816 * procedure is running. Since the copying of the old disk to
5817 * the new disk takes place at run time while the filesystem is
5818 * mounted writable, the regular write operations to the old
5819 * disk have to be duplicated to go to the new disk as well.
5820 *
5821 * Note that device->missing is handled by the caller, and that
5822 * the write to the old disk is already set up in the stripes
5823 * array.
5824 */
5825 index_where_to_add = num_stripes;
5826 for (i = 0; i < num_stripes; i++) {
5827 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5828 /* write to new disk, too */
5829 struct btrfs_bio_stripe *new =
5830 bbio->stripes + index_where_to_add;
5831 struct btrfs_bio_stripe *old =
5832 bbio->stripes + i;
5833
5834 new->physical = old->physical;
5835 new->length = old->length;
5836 new->dev = dev_replace->tgtdev;
5837 bbio->tgtdev_map[i] = index_where_to_add;
5838 index_where_to_add++;
5839 max_errors++;
5840 tgtdev_indexes++;
5841 }
5842 }
5843 num_stripes = index_where_to_add;
5844 } else if (op == BTRFS_MAP_GET_READ_MIRRORS) {
5845 int index_srcdev = 0;
5846 int found = 0;
5847 u64 physical_of_found = 0;
5848
5849 /*
5850 * During the dev-replace procedure, the target drive can also
5851 * be used to read data in case it is needed to repair a corrupt
5852 * block elsewhere. This is possible if the requested area is
5853 * left of the left cursor. In this area, the target drive is a
5854 * full copy of the source drive.
5855 */
5856 for (i = 0; i < num_stripes; i++) {
5857 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5858 /*
5859 * In case of DUP, in order to keep it simple,
5860 * only add the mirror with the lowest physical
5861 * address
5862 */
5863 if (found &&
5864 physical_of_found <=
5865 bbio->stripes[i].physical)
5866 continue;
5867 index_srcdev = i;
5868 found = 1;
5869 physical_of_found = bbio->stripes[i].physical;
5870 }
5871 }
5872 if (found) {
5873 struct btrfs_bio_stripe *tgtdev_stripe =
5874 bbio->stripes + num_stripes;
5875
5876 tgtdev_stripe->physical = physical_of_found;
5877 tgtdev_stripe->length =
5878 bbio->stripes[index_srcdev].length;
5879 tgtdev_stripe->dev = dev_replace->tgtdev;
5880 bbio->tgtdev_map[index_srcdev] = num_stripes;
5881
5882 tgtdev_indexes++;
5883 num_stripes++;
5884 }
5885 }
5886
5887 *num_stripes_ret = num_stripes;
5888 *max_errors_ret = max_errors;
5889 bbio->num_tgtdevs = tgtdev_indexes;
5890 *bbio_ret = bbio;
5891}
5892
2b19a1fe
LB
5893static bool need_full_stripe(enum btrfs_map_op op)
5894{
5895 return (op == BTRFS_MAP_WRITE || op == BTRFS_MAP_GET_READ_MIRRORS);
5896}
5897
5f141126
NB
5898/*
5899 * btrfs_get_io_geometry - calculates the geomery of a particular (address, len)
5900 * tuple. This information is used to calculate how big a
5901 * particular bio can get before it straddles a stripe.
5902 *
5903 * @fs_info - the filesystem
5904 * @logical - address that we want to figure out the geometry of
5905 * @len - the length of IO we are going to perform, starting at @logical
5906 * @op - type of operation - write or read
5907 * @io_geom - pointer used to return values
5908 *
5909 * Returns < 0 in case a chunk for the given logical address cannot be found,
5910 * usually shouldn't happen unless @logical is corrupted, 0 otherwise.
5911 */
5912int btrfs_get_io_geometry(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
89b798ad 5913 u64 logical, u64 len, struct btrfs_io_geometry *io_geom)
5f141126
NB
5914{
5915 struct extent_map *em;
5916 struct map_lookup *map;
5917 u64 offset;
5918 u64 stripe_offset;
5919 u64 stripe_nr;
5920 u64 stripe_len;
5921 u64 raid56_full_stripe_start = (u64)-1;
5922 int data_stripes;
373c3b80 5923 int ret = 0;
5f141126
NB
5924
5925 ASSERT(op != BTRFS_MAP_DISCARD);
5926
5927 em = btrfs_get_chunk_map(fs_info, logical, len);
5928 if (IS_ERR(em))
5929 return PTR_ERR(em);
5930
5931 map = em->map_lookup;
5932 /* Offset of this logical address in the chunk */
5933 offset = logical - em->start;
5934 /* Len of a stripe in a chunk */
5935 stripe_len = map->stripe_len;
5936 /* Stripe wher this block falls in */
5937 stripe_nr = div64_u64(offset, stripe_len);
5938 /* Offset of stripe in the chunk */
5939 stripe_offset = stripe_nr * stripe_len;
5940 if (offset < stripe_offset) {
5941 btrfs_crit(fs_info,
5942"stripe math has gone wrong, stripe_offset=%llu offset=%llu start=%llu logical=%llu stripe_len=%llu",
5943 stripe_offset, offset, em->start, logical, stripe_len);
373c3b80
JT
5944 ret = -EINVAL;
5945 goto out;
5f141126
NB
5946 }
5947
5948 /* stripe_offset is the offset of this block in its stripe */
5949 stripe_offset = offset - stripe_offset;
5950 data_stripes = nr_data_stripes(map);
5951
5952 if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
5953 u64 max_len = stripe_len - stripe_offset;
5954
5955 /*
5956 * In case of raid56, we need to know the stripe aligned start
5957 */
5958 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
5959 unsigned long full_stripe_len = stripe_len * data_stripes;
5960 raid56_full_stripe_start = offset;
5961
5962 /*
5963 * Allow a write of a full stripe, but make sure we
5964 * don't allow straddling of stripes
5965 */
5966 raid56_full_stripe_start = div64_u64(raid56_full_stripe_start,
5967 full_stripe_len);
5968 raid56_full_stripe_start *= full_stripe_len;
5969
5970 /*
5971 * For writes to RAID[56], allow a full stripeset across
5972 * all disks. For other RAID types and for RAID[56]
5973 * reads, just allow a single stripe (on a single disk).
5974 */
5975 if (op == BTRFS_MAP_WRITE) {
5976 max_len = stripe_len * data_stripes -
5977 (offset - raid56_full_stripe_start);
5978 }
5979 }
5980 len = min_t(u64, em->len - offset, max_len);
5981 } else {
5982 len = em->len - offset;
5983 }
5984
5985 io_geom->len = len;
5986 io_geom->offset = offset;
5987 io_geom->stripe_len = stripe_len;
5988 io_geom->stripe_nr = stripe_nr;
5989 io_geom->stripe_offset = stripe_offset;
5990 io_geom->raid56_stripe_offset = raid56_full_stripe_start;
5991
373c3b80
JT
5992out:
5993 /* once for us */
5994 free_extent_map(em);
5995 return ret;
5f141126
NB
5996}
5997
cf8cddd3
CH
5998static int __btrfs_map_block(struct btrfs_fs_info *fs_info,
5999 enum btrfs_map_op op,
f2d8d74d 6000 u64 logical, u64 *length,
a1d3c478 6001 struct btrfs_bio **bbio_ret,
8e5cfb55 6002 int mirror_num, int need_raid_map)
0b86a832
CM
6003{
6004 struct extent_map *em;
6005 struct map_lookup *map;
593060d7
CM
6006 u64 stripe_offset;
6007 u64 stripe_nr;
53b381b3 6008 u64 stripe_len;
9d644a62 6009 u32 stripe_index;
cff82672 6010 int data_stripes;
cea9e445 6011 int i;
de11cc12 6012 int ret = 0;
f2d8d74d 6013 int num_stripes;
a236aed1 6014 int max_errors = 0;
2c8cdd6e 6015 int tgtdev_indexes = 0;
a1d3c478 6016 struct btrfs_bio *bbio = NULL;
472262f3
SB
6017 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
6018 int dev_replace_is_ongoing = 0;
6019 int num_alloc_stripes;
ad6d620e
SB
6020 int patch_the_first_stripe_for_dev_replace = 0;
6021 u64 physical_to_patch_in_first_stripe = 0;
53b381b3 6022 u64 raid56_full_stripe_start = (u64)-1;
89b798ad
NB
6023 struct btrfs_io_geometry geom;
6024
6025 ASSERT(bbio_ret);
75fb2e9e 6026 ASSERT(op != BTRFS_MAP_DISCARD);
0b3d4cd3 6027
89b798ad
NB
6028 ret = btrfs_get_io_geometry(fs_info, op, logical, *length, &geom);
6029 if (ret < 0)
6030 return ret;
0b86a832 6031
89b798ad 6032 em = btrfs_get_chunk_map(fs_info, logical, *length);
f1136989 6033 ASSERT(!IS_ERR(em));
95617d69 6034 map = em->map_lookup;
593060d7 6035
89b798ad 6036 *length = geom.len;
89b798ad
NB
6037 stripe_len = geom.stripe_len;
6038 stripe_nr = geom.stripe_nr;
6039 stripe_offset = geom.stripe_offset;
6040 raid56_full_stripe_start = geom.raid56_stripe_offset;
cff82672 6041 data_stripes = nr_data_stripes(map);
593060d7 6042
cb5583dd 6043 down_read(&dev_replace->rwsem);
472262f3 6044 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
53176dde
DS
6045 /*
6046 * Hold the semaphore for read during the whole operation, write is
6047 * requested at commit time but must wait.
6048 */
472262f3 6049 if (!dev_replace_is_ongoing)
cb5583dd 6050 up_read(&dev_replace->rwsem);
472262f3 6051
ad6d620e 6052 if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
2b19a1fe 6053 !need_full_stripe(op) && dev_replace->tgtdev != NULL) {
5ab56090
LB
6054 ret = get_extra_mirror_from_replace(fs_info, logical, *length,
6055 dev_replace->srcdev->devid,
6056 &mirror_num,
6057 &physical_to_patch_in_first_stripe);
6058 if (ret)
ad6d620e 6059 goto out;
5ab56090
LB
6060 else
6061 patch_the_first_stripe_for_dev_replace = 1;
ad6d620e
SB
6062 } else if (mirror_num > map->num_stripes) {
6063 mirror_num = 0;
6064 }
6065
f2d8d74d 6066 num_stripes = 1;
cea9e445 6067 stripe_index = 0;
fce3bb9a 6068 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
47c5713f
DS
6069 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
6070 &stripe_index);
de483734 6071 if (!need_full_stripe(op))
28e1cc7d 6072 mirror_num = 1;
c7369b3f 6073 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1_MASK) {
de483734 6074 if (need_full_stripe(op))
f2d8d74d 6075 num_stripes = map->num_stripes;
2fff734f 6076 else if (mirror_num)
f188591e 6077 stripe_index = mirror_num - 1;
dfe25020 6078 else {
30d9861f 6079 stripe_index = find_live_mirror(fs_info, map, 0,
30d9861f 6080 dev_replace_is_ongoing);
a1d3c478 6081 mirror_num = stripe_index + 1;
dfe25020 6082 }
2fff734f 6083
611f0e00 6084 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
de483734 6085 if (need_full_stripe(op)) {
f2d8d74d 6086 num_stripes = map->num_stripes;
a1d3c478 6087 } else if (mirror_num) {
f188591e 6088 stripe_index = mirror_num - 1;
a1d3c478
JS
6089 } else {
6090 mirror_num = 1;
6091 }
2fff734f 6092
321aecc6 6093 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
9d644a62 6094 u32 factor = map->num_stripes / map->sub_stripes;
321aecc6 6095
47c5713f 6096 stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
321aecc6
CM
6097 stripe_index *= map->sub_stripes;
6098
de483734 6099 if (need_full_stripe(op))
f2d8d74d 6100 num_stripes = map->sub_stripes;
321aecc6
CM
6101 else if (mirror_num)
6102 stripe_index += mirror_num - 1;
dfe25020 6103 else {
3e74317a 6104 int old_stripe_index = stripe_index;
30d9861f
SB
6105 stripe_index = find_live_mirror(fs_info, map,
6106 stripe_index,
30d9861f 6107 dev_replace_is_ongoing);
3e74317a 6108 mirror_num = stripe_index - old_stripe_index + 1;
dfe25020 6109 }
53b381b3 6110
ffe2d203 6111 } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
de483734 6112 if (need_raid_map && (need_full_stripe(op) || mirror_num > 1)) {
53b381b3 6113 /* push stripe_nr back to the start of the full stripe */
42c61ab6 6114 stripe_nr = div64_u64(raid56_full_stripe_start,
cff82672 6115 stripe_len * data_stripes);
53b381b3
DW
6116
6117 /* RAID[56] write or recovery. Return all stripes */
6118 num_stripes = map->num_stripes;
6119 max_errors = nr_parity_stripes(map);
6120
53b381b3
DW
6121 *length = map->stripe_len;
6122 stripe_index = 0;
6123 stripe_offset = 0;
6124 } else {
6125 /*
6126 * Mirror #0 or #1 means the original data block.
6127 * Mirror #2 is RAID5 parity block.
6128 * Mirror #3 is RAID6 Q block.
6129 */
47c5713f 6130 stripe_nr = div_u64_rem(stripe_nr,
cff82672 6131 data_stripes, &stripe_index);
53b381b3 6132 if (mirror_num > 1)
cff82672 6133 stripe_index = data_stripes + mirror_num - 2;
53b381b3
DW
6134
6135 /* We distribute the parity blocks across stripes */
47c5713f
DS
6136 div_u64_rem(stripe_nr + stripe_index, map->num_stripes,
6137 &stripe_index);
de483734 6138 if (!need_full_stripe(op) && mirror_num <= 1)
28e1cc7d 6139 mirror_num = 1;
53b381b3 6140 }
8790d502
CM
6141 } else {
6142 /*
47c5713f
DS
6143 * after this, stripe_nr is the number of stripes on this
6144 * device we have to walk to find the data, and stripe_index is
6145 * the number of our device in the stripe array
8790d502 6146 */
47c5713f
DS
6147 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
6148 &stripe_index);
a1d3c478 6149 mirror_num = stripe_index + 1;
8790d502 6150 }
e042d1ec 6151 if (stripe_index >= map->num_stripes) {
5d163e0e
JM
6152 btrfs_crit(fs_info,
6153 "stripe index math went horribly wrong, got stripe_index=%u, num_stripes=%u",
e042d1ec
JB
6154 stripe_index, map->num_stripes);
6155 ret = -EINVAL;
6156 goto out;
6157 }
cea9e445 6158
472262f3 6159 num_alloc_stripes = num_stripes;
6fad823f 6160 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL) {
0b3d4cd3 6161 if (op == BTRFS_MAP_WRITE)
ad6d620e 6162 num_alloc_stripes <<= 1;
cf8cddd3 6163 if (op == BTRFS_MAP_GET_READ_MIRRORS)
ad6d620e 6164 num_alloc_stripes++;
2c8cdd6e 6165 tgtdev_indexes = num_stripes;
ad6d620e 6166 }
2c8cdd6e 6167
6e9606d2 6168 bbio = alloc_btrfs_bio(num_alloc_stripes, tgtdev_indexes);
de11cc12
LZ
6169 if (!bbio) {
6170 ret = -ENOMEM;
6171 goto out;
6172 }
608769a4
NB
6173
6174 for (i = 0; i < num_stripes; i++) {
6175 bbio->stripes[i].physical = map->stripes[stripe_index].physical +
6176 stripe_offset + stripe_nr * map->stripe_len;
6177 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
6178 stripe_index++;
6179 }
de11cc12 6180
8e5cfb55 6181 /* build raid_map */
2b19a1fe
LB
6182 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK && need_raid_map &&
6183 (need_full_stripe(op) || mirror_num > 1)) {
8e5cfb55 6184 u64 tmp;
9d644a62 6185 unsigned rot;
8e5cfb55 6186
8e5cfb55 6187 /* Work out the disk rotation on this stripe-set */
47c5713f 6188 div_u64_rem(stripe_nr, num_stripes, &rot);
8e5cfb55
ZL
6189
6190 /* Fill in the logical address of each stripe */
cff82672
DS
6191 tmp = stripe_nr * data_stripes;
6192 for (i = 0; i < data_stripes; i++)
8e5cfb55
ZL
6193 bbio->raid_map[(i+rot) % num_stripes] =
6194 em->start + (tmp + i) * map->stripe_len;
6195
6196 bbio->raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
6197 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
6198 bbio->raid_map[(i+rot+1) % num_stripes] =
6199 RAID6_Q_STRIPE;
8e5cfb55 6200
608769a4 6201 sort_parity_stripes(bbio, num_stripes);
593060d7 6202 }
de11cc12 6203
2b19a1fe 6204 if (need_full_stripe(op))
d20983b4 6205 max_errors = btrfs_chunk_max_errors(map);
de11cc12 6206
73c0f228 6207 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL &&
2b19a1fe 6208 need_full_stripe(op)) {
73c0f228
LB
6209 handle_ops_on_dev_replace(op, &bbio, dev_replace, &num_stripes,
6210 &max_errors);
472262f3
SB
6211 }
6212
de11cc12 6213 *bbio_ret = bbio;
10f11900 6214 bbio->map_type = map->type;
de11cc12
LZ
6215 bbio->num_stripes = num_stripes;
6216 bbio->max_errors = max_errors;
6217 bbio->mirror_num = mirror_num;
ad6d620e
SB
6218
6219 /*
6220 * this is the case that REQ_READ && dev_replace_is_ongoing &&
6221 * mirror_num == num_stripes + 1 && dev_replace target drive is
6222 * available as a mirror
6223 */
6224 if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
6225 WARN_ON(num_stripes > 1);
6226 bbio->stripes[0].dev = dev_replace->tgtdev;
6227 bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
6228 bbio->mirror_num = map->num_stripes + 1;
6229 }
cea9e445 6230out:
73beece9 6231 if (dev_replace_is_ongoing) {
53176dde
DS
6232 lockdep_assert_held(&dev_replace->rwsem);
6233 /* Unlock and let waiting writers proceed */
cb5583dd 6234 up_read(&dev_replace->rwsem);
73beece9 6235 }
0b86a832 6236 free_extent_map(em);
de11cc12 6237 return ret;
0b86a832
CM
6238}
6239
cf8cddd3 6240int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
f2d8d74d 6241 u64 logical, u64 *length,
a1d3c478 6242 struct btrfs_bio **bbio_ret, int mirror_num)
f2d8d74d 6243{
75fb2e9e
DS
6244 if (op == BTRFS_MAP_DISCARD)
6245 return __btrfs_map_block_for_discard(fs_info, logical,
6246 length, bbio_ret);
6247
b3d3fa51 6248 return __btrfs_map_block(fs_info, op, logical, length, bbio_ret,
8e5cfb55 6249 mirror_num, 0);
f2d8d74d
CM
6250}
6251
af8e2d1d 6252/* For Scrub/replace */
cf8cddd3 6253int btrfs_map_sblock(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
af8e2d1d 6254 u64 logical, u64 *length,
825ad4c9 6255 struct btrfs_bio **bbio_ret)
af8e2d1d 6256{
825ad4c9 6257 return __btrfs_map_block(fs_info, op, logical, length, bbio_ret, 0, 1);
af8e2d1d
MX
6258}
6259
4246a0b6 6260static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio)
8408c716 6261{
326e1dbb
MS
6262 bio->bi_private = bbio->private;
6263 bio->bi_end_io = bbio->end_io;
4246a0b6 6264 bio_endio(bio);
326e1dbb 6265
6e9606d2 6266 btrfs_put_bbio(bbio);
8408c716
MX
6267}
6268
4246a0b6 6269static void btrfs_end_bio(struct bio *bio)
8790d502 6270{
9be3395b 6271 struct btrfs_bio *bbio = bio->bi_private;
7d2b4daa 6272 int is_orig_bio = 0;
8790d502 6273
4e4cbee9 6274 if (bio->bi_status) {
a1d3c478 6275 atomic_inc(&bbio->error);
4e4cbee9
CH
6276 if (bio->bi_status == BLK_STS_IOERR ||
6277 bio->bi_status == BLK_STS_TARGET) {
c31efbdf 6278 struct btrfs_device *dev = btrfs_io_bio(bio)->device;
442a4f63 6279
3eee86c8
NB
6280 ASSERT(dev->bdev);
6281 if (bio_op(bio) == REQ_OP_WRITE)
6282 btrfs_dev_stat_inc_and_print(dev,
597a60fa 6283 BTRFS_DEV_STAT_WRITE_ERRS);
3eee86c8
NB
6284 else if (!(bio->bi_opf & REQ_RAHEAD))
6285 btrfs_dev_stat_inc_and_print(dev,
597a60fa 6286 BTRFS_DEV_STAT_READ_ERRS);
3eee86c8
NB
6287 if (bio->bi_opf & REQ_PREFLUSH)
6288 btrfs_dev_stat_inc_and_print(dev,
597a60fa 6289 BTRFS_DEV_STAT_FLUSH_ERRS);
442a4f63
SB
6290 }
6291 }
8790d502 6292
a1d3c478 6293 if (bio == bbio->orig_bio)
7d2b4daa
CM
6294 is_orig_bio = 1;
6295
c404e0dc
MX
6296 btrfs_bio_counter_dec(bbio->fs_info);
6297
a1d3c478 6298 if (atomic_dec_and_test(&bbio->stripes_pending)) {
7d2b4daa
CM
6299 if (!is_orig_bio) {
6300 bio_put(bio);
a1d3c478 6301 bio = bbio->orig_bio;
7d2b4daa 6302 }
c7b22bb1 6303
9be3395b 6304 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
a236aed1 6305 /* only send an error to the higher layers if it is
53b381b3 6306 * beyond the tolerance of the btrfs bio
a236aed1 6307 */
a1d3c478 6308 if (atomic_read(&bbio->error) > bbio->max_errors) {
4e4cbee9 6309 bio->bi_status = BLK_STS_IOERR;
5dbc8fca 6310 } else {
1259ab75
CM
6311 /*
6312 * this bio is actually up to date, we didn't
6313 * go over the max number of errors
6314 */
2dbe0c77 6315 bio->bi_status = BLK_STS_OK;
1259ab75 6316 }
c55f1396 6317
4246a0b6 6318 btrfs_end_bbio(bbio, bio);
7d2b4daa 6319 } else if (!is_orig_bio) {
8790d502
CM
6320 bio_put(bio);
6321 }
8790d502
CM
6322}
6323
2ff7e61e 6324static void submit_stripe_bio(struct btrfs_bio *bbio, struct bio *bio,
c31efbdf 6325 u64 physical, struct btrfs_device *dev)
de1ee92a 6326{
2ff7e61e 6327 struct btrfs_fs_info *fs_info = bbio->fs_info;
de1ee92a
JB
6328
6329 bio->bi_private = bbio;
c31efbdf 6330 btrfs_io_bio(bio)->device = dev;
de1ee92a 6331 bio->bi_end_io = btrfs_end_bio;
4f024f37 6332 bio->bi_iter.bi_sector = physical >> 9;
672d5990
MT
6333 btrfs_debug_in_rcu(fs_info,
6334 "btrfs_map_bio: rw %d 0x%x, sector=%llu, dev=%lu (%s id %llu), size=%u",
6335 bio_op(bio), bio->bi_opf, (u64)bio->bi_iter.bi_sector,
1db45a35
DS
6336 (unsigned long)dev->bdev->bd_dev, rcu_str_deref(dev->name),
6337 dev->devid, bio->bi_iter.bi_size);
74d46992 6338 bio_set_dev(bio, dev->bdev);
c404e0dc 6339
2ff7e61e 6340 btrfs_bio_counter_inc_noblocked(fs_info);
c404e0dc 6341
08635bae 6342 btrfsic_submit_bio(bio);
de1ee92a
JB
6343}
6344
de1ee92a
JB
6345static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
6346{
6347 atomic_inc(&bbio->error);
6348 if (atomic_dec_and_test(&bbio->stripes_pending)) {
01327610 6349 /* Should be the original bio. */
8408c716
MX
6350 WARN_ON(bio != bbio->orig_bio);
6351
9be3395b 6352 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
4f024f37 6353 bio->bi_iter.bi_sector = logical >> 9;
102ed2c5
AJ
6354 if (atomic_read(&bbio->error) > bbio->max_errors)
6355 bio->bi_status = BLK_STS_IOERR;
6356 else
6357 bio->bi_status = BLK_STS_OK;
4246a0b6 6358 btrfs_end_bbio(bbio, bio);
de1ee92a
JB
6359 }
6360}
6361
58efbc9f 6362blk_status_t btrfs_map_bio(struct btrfs_fs_info *fs_info, struct bio *bio,
08635bae 6363 int mirror_num)
0b86a832 6364{
0b86a832 6365 struct btrfs_device *dev;
8790d502 6366 struct bio *first_bio = bio;
4f024f37 6367 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
0b86a832
CM
6368 u64 length = 0;
6369 u64 map_length;
0b86a832 6370 int ret;
08da757d
ZL
6371 int dev_nr;
6372 int total_devs;
a1d3c478 6373 struct btrfs_bio *bbio = NULL;
0b86a832 6374
4f024f37 6375 length = bio->bi_iter.bi_size;
0b86a832 6376 map_length = length;
cea9e445 6377
0b246afa 6378 btrfs_bio_counter_inc_blocked(fs_info);
bd7d63c2 6379 ret = __btrfs_map_block(fs_info, btrfs_op(bio), logical,
37226b21 6380 &map_length, &bbio, mirror_num, 1);
c404e0dc 6381 if (ret) {
0b246afa 6382 btrfs_bio_counter_dec(fs_info);
58efbc9f 6383 return errno_to_blk_status(ret);
c404e0dc 6384 }
cea9e445 6385
a1d3c478 6386 total_devs = bbio->num_stripes;
53b381b3
DW
6387 bbio->orig_bio = first_bio;
6388 bbio->private = first_bio->bi_private;
6389 bbio->end_io = first_bio->bi_end_io;
0b246afa 6390 bbio->fs_info = fs_info;
53b381b3
DW
6391 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
6392
ad1ba2a0 6393 if ((bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
37226b21 6394 ((bio_op(bio) == REQ_OP_WRITE) || (mirror_num > 1))) {
53b381b3
DW
6395 /* In this case, map_length has been set to the length of
6396 a single stripe; not the whole write */
37226b21 6397 if (bio_op(bio) == REQ_OP_WRITE) {
2ff7e61e
JM
6398 ret = raid56_parity_write(fs_info, bio, bbio,
6399 map_length);
53b381b3 6400 } else {
2ff7e61e
JM
6401 ret = raid56_parity_recover(fs_info, bio, bbio,
6402 map_length, mirror_num, 1);
53b381b3 6403 }
4245215d 6404
0b246afa 6405 btrfs_bio_counter_dec(fs_info);
58efbc9f 6406 return errno_to_blk_status(ret);
53b381b3
DW
6407 }
6408
cea9e445 6409 if (map_length < length) {
0b246afa 6410 btrfs_crit(fs_info,
5d163e0e
JM
6411 "mapping failed logical %llu bio len %llu len %llu",
6412 logical, length, map_length);
cea9e445
CM
6413 BUG();
6414 }
a1d3c478 6415
08da757d 6416 for (dev_nr = 0; dev_nr < total_devs; dev_nr++) {
de1ee92a 6417 dev = bbio->stripes[dev_nr].dev;
fc8a168a
NB
6418 if (!dev || !dev->bdev || test_bit(BTRFS_DEV_STATE_MISSING,
6419 &dev->dev_state) ||
ebbede42
AJ
6420 (bio_op(first_bio) == REQ_OP_WRITE &&
6421 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))) {
de1ee92a 6422 bbio_error(bbio, first_bio, logical);
de1ee92a
JB
6423 continue;
6424 }
6425
3aa8e074 6426 if (dev_nr < total_devs - 1)
8b6c1d56 6427 bio = btrfs_bio_clone(first_bio);
3aa8e074 6428 else
a1d3c478 6429 bio = first_bio;
de1ee92a 6430
c31efbdf 6431 submit_stripe_bio(bbio, bio, bbio->stripes[dev_nr].physical, dev);
8790d502 6432 }
0b246afa 6433 btrfs_bio_counter_dec(fs_info);
58efbc9f 6434 return BLK_STS_OK;
0b86a832
CM
6435}
6436
09ba3bc9
AJ
6437/*
6438 * Find a device specified by @devid or @uuid in the list of @fs_devices, or
6439 * return NULL.
6440 *
6441 * If devid and uuid are both specified, the match must be exact, otherwise
6442 * only devid is used.
6443 *
6444 * If @seed is true, traverse through the seed devices.
6445 */
e4319cd9 6446struct btrfs_device *btrfs_find_device(struct btrfs_fs_devices *fs_devices,
09ba3bc9
AJ
6447 u64 devid, u8 *uuid, u8 *fsid,
6448 bool seed)
0b86a832 6449{
2b82032c 6450 struct btrfs_device *device;
944d3f9f
NB
6451 struct btrfs_fs_devices *seed_devs;
6452
6453 if (!fsid || !memcmp(fs_devices->metadata_uuid, fsid, BTRFS_FSID_SIZE)) {
6454 list_for_each_entry(device, &fs_devices->devices, dev_list) {
6455 if (device->devid == devid &&
6456 (!uuid || memcmp(device->uuid, uuid,
6457 BTRFS_UUID_SIZE) == 0))
6458 return device;
6459 }
6460 }
2b82032c 6461
944d3f9f 6462 list_for_each_entry(seed_devs, &fs_devices->seed_list, seed_list) {
2b82032c 6463 if (!fsid ||
944d3f9f
NB
6464 !memcmp(seed_devs->metadata_uuid, fsid, BTRFS_FSID_SIZE)) {
6465 list_for_each_entry(device, &seed_devs->devices,
09ba3bc9
AJ
6466 dev_list) {
6467 if (device->devid == devid &&
6468 (!uuid || memcmp(device->uuid, uuid,
6469 BTRFS_UUID_SIZE) == 0))
6470 return device;
6471 }
2b82032c 6472 }
2b82032c 6473 }
944d3f9f 6474
2b82032c 6475 return NULL;
0b86a832
CM
6476}
6477
2ff7e61e 6478static struct btrfs_device *add_missing_dev(struct btrfs_fs_devices *fs_devices,
dfe25020
CM
6479 u64 devid, u8 *dev_uuid)
6480{
6481 struct btrfs_device *device;
fccc0007 6482 unsigned int nofs_flag;
dfe25020 6483
fccc0007
JB
6484 /*
6485 * We call this under the chunk_mutex, so we want to use NOFS for this
6486 * allocation, however we don't want to change btrfs_alloc_device() to
6487 * always do NOFS because we use it in a lot of other GFP_KERNEL safe
6488 * places.
6489 */
6490 nofs_flag = memalloc_nofs_save();
12bd2fc0 6491 device = btrfs_alloc_device(NULL, &devid, dev_uuid);
fccc0007 6492 memalloc_nofs_restore(nofs_flag);
12bd2fc0 6493 if (IS_ERR(device))
adfb69af 6494 return device;
12bd2fc0
ID
6495
6496 list_add(&device->dev_list, &fs_devices->devices);
e4404d6e 6497 device->fs_devices = fs_devices;
dfe25020 6498 fs_devices->num_devices++;
12bd2fc0 6499
e6e674bd 6500 set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
cd02dca5 6501 fs_devices->missing_devices++;
12bd2fc0 6502
dfe25020
CM
6503 return device;
6504}
6505
12bd2fc0
ID
6506/**
6507 * btrfs_alloc_device - allocate struct btrfs_device
6508 * @fs_info: used only for generating a new devid, can be NULL if
6509 * devid is provided (i.e. @devid != NULL).
6510 * @devid: a pointer to devid for this device. If NULL a new devid
6511 * is generated.
6512 * @uuid: a pointer to UUID for this device. If NULL a new UUID
6513 * is generated.
6514 *
6515 * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR()
48dae9cf 6516 * on error. Returned struct is not linked onto any lists and must be
a425f9d4 6517 * destroyed with btrfs_free_device.
12bd2fc0
ID
6518 */
6519struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
6520 const u64 *devid,
6521 const u8 *uuid)
6522{
6523 struct btrfs_device *dev;
6524 u64 tmp;
6525
fae7f21c 6526 if (WARN_ON(!devid && !fs_info))
12bd2fc0 6527 return ERR_PTR(-EINVAL);
12bd2fc0 6528
154f7cb8 6529 dev = __alloc_device(fs_info);
12bd2fc0
ID
6530 if (IS_ERR(dev))
6531 return dev;
6532
6533 if (devid)
6534 tmp = *devid;
6535 else {
6536 int ret;
6537
6538 ret = find_next_devid(fs_info, &tmp);
6539 if (ret) {
a425f9d4 6540 btrfs_free_device(dev);
12bd2fc0
ID
6541 return ERR_PTR(ret);
6542 }
6543 }
6544 dev->devid = tmp;
6545
6546 if (uuid)
6547 memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE);
6548 else
6549 generate_random_uuid(dev->uuid);
6550
12bd2fc0
ID
6551 return dev;
6552}
6553
5a2b8e60 6554static void btrfs_report_missing_device(struct btrfs_fs_info *fs_info,
2b902dfc 6555 u64 devid, u8 *uuid, bool error)
5a2b8e60 6556{
2b902dfc
AJ
6557 if (error)
6558 btrfs_err_rl(fs_info, "devid %llu uuid %pU is missing",
6559 devid, uuid);
6560 else
6561 btrfs_warn_rl(fs_info, "devid %llu uuid %pU is missing",
6562 devid, uuid);
5a2b8e60
AJ
6563}
6564
39e264a4
NB
6565static u64 calc_stripe_length(u64 type, u64 chunk_len, int num_stripes)
6566{
6567 int index = btrfs_bg_flags_to_raid_index(type);
6568 int ncopies = btrfs_raid_array[index].ncopies;
e4f6c6be 6569 const int nparity = btrfs_raid_array[index].nparity;
39e264a4
NB
6570 int data_stripes;
6571
e4f6c6be
DS
6572 if (nparity)
6573 data_stripes = num_stripes - nparity;
6574 else
39e264a4 6575 data_stripes = num_stripes / ncopies;
e4f6c6be 6576
39e264a4
NB
6577 return div_u64(chunk_len, data_stripes);
6578}
6579
9690ac09 6580static int read_one_chunk(struct btrfs_key *key, struct extent_buffer *leaf,
e06cd3dd
LB
6581 struct btrfs_chunk *chunk)
6582{
9690ac09 6583 struct btrfs_fs_info *fs_info = leaf->fs_info;
c8bf1b67 6584 struct extent_map_tree *map_tree = &fs_info->mapping_tree;
e06cd3dd
LB
6585 struct map_lookup *map;
6586 struct extent_map *em;
6587 u64 logical;
6588 u64 length;
e06cd3dd
LB
6589 u64 devid;
6590 u8 uuid[BTRFS_UUID_SIZE];
6591 int num_stripes;
6592 int ret;
6593 int i;
6594
6595 logical = key->offset;
6596 length = btrfs_chunk_length(leaf, chunk);
e06cd3dd
LB
6597 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
6598
075cb3c7
QW
6599 /*
6600 * Only need to verify chunk item if we're reading from sys chunk array,
6601 * as chunk item in tree block is already verified by tree-checker.
6602 */
6603 if (leaf->start == BTRFS_SUPER_INFO_OFFSET) {
ddaf1d5a 6604 ret = btrfs_check_chunk_valid(leaf, chunk, logical);
075cb3c7
QW
6605 if (ret)
6606 return ret;
6607 }
a061fc8d 6608
c8bf1b67
DS
6609 read_lock(&map_tree->lock);
6610 em = lookup_extent_mapping(map_tree, logical, 1);
6611 read_unlock(&map_tree->lock);
0b86a832
CM
6612
6613 /* already mapped? */
6614 if (em && em->start <= logical && em->start + em->len > logical) {
6615 free_extent_map(em);
0b86a832
CM
6616 return 0;
6617 } else if (em) {
6618 free_extent_map(em);
6619 }
0b86a832 6620
172ddd60 6621 em = alloc_extent_map();
0b86a832
CM
6622 if (!em)
6623 return -ENOMEM;
593060d7 6624 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
0b86a832
CM
6625 if (!map) {
6626 free_extent_map(em);
6627 return -ENOMEM;
6628 }
6629
298a8f9c 6630 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
95617d69 6631 em->map_lookup = map;
0b86a832
CM
6632 em->start = logical;
6633 em->len = length;
70c8a91c 6634 em->orig_start = 0;
0b86a832 6635 em->block_start = 0;
c8b97818 6636 em->block_len = em->len;
0b86a832 6637
593060d7
CM
6638 map->num_stripes = num_stripes;
6639 map->io_width = btrfs_chunk_io_width(leaf, chunk);
6640 map->io_align = btrfs_chunk_io_align(leaf, chunk);
593060d7
CM
6641 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
6642 map->type = btrfs_chunk_type(leaf, chunk);
321aecc6 6643 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
cf90d884 6644 map->verified_stripes = 0;
39e264a4
NB
6645 em->orig_block_len = calc_stripe_length(map->type, em->len,
6646 map->num_stripes);
593060d7
CM
6647 for (i = 0; i < num_stripes; i++) {
6648 map->stripes[i].physical =
6649 btrfs_stripe_offset_nr(leaf, chunk, i);
6650 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
a443755f
CM
6651 read_extent_buffer(leaf, uuid, (unsigned long)
6652 btrfs_stripe_dev_uuid_nr(chunk, i),
6653 BTRFS_UUID_SIZE);
e4319cd9 6654 map->stripes[i].dev = btrfs_find_device(fs_info->fs_devices,
09ba3bc9 6655 devid, uuid, NULL, true);
3cdde224 6656 if (!map->stripes[i].dev &&
0b246afa 6657 !btrfs_test_opt(fs_info, DEGRADED)) {
593060d7 6658 free_extent_map(em);
2b902dfc 6659 btrfs_report_missing_device(fs_info, devid, uuid, true);
45dbdbc9 6660 return -ENOENT;
593060d7 6661 }
dfe25020
CM
6662 if (!map->stripes[i].dev) {
6663 map->stripes[i].dev =
2ff7e61e
JM
6664 add_missing_dev(fs_info->fs_devices, devid,
6665 uuid);
adfb69af 6666 if (IS_ERR(map->stripes[i].dev)) {
dfe25020 6667 free_extent_map(em);
adfb69af
AJ
6668 btrfs_err(fs_info,
6669 "failed to init missing dev %llu: %ld",
6670 devid, PTR_ERR(map->stripes[i].dev));
6671 return PTR_ERR(map->stripes[i].dev);
dfe25020 6672 }
2b902dfc 6673 btrfs_report_missing_device(fs_info, devid, uuid, false);
dfe25020 6674 }
e12c9621
AJ
6675 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
6676 &(map->stripes[i].dev->dev_state));
6677
0b86a832
CM
6678 }
6679
c8bf1b67
DS
6680 write_lock(&map_tree->lock);
6681 ret = add_extent_mapping(map_tree, em, 0);
6682 write_unlock(&map_tree->lock);
64f64f43
QW
6683 if (ret < 0) {
6684 btrfs_err(fs_info,
6685 "failed to add chunk map, start=%llu len=%llu: %d",
6686 em->start, em->len, ret);
6687 }
0b86a832
CM
6688 free_extent_map(em);
6689
64f64f43 6690 return ret;
0b86a832
CM
6691}
6692
143bede5 6693static void fill_device_from_item(struct extent_buffer *leaf,
0b86a832
CM
6694 struct btrfs_dev_item *dev_item,
6695 struct btrfs_device *device)
6696{
6697 unsigned long ptr;
0b86a832
CM
6698
6699 device->devid = btrfs_device_id(leaf, dev_item);
d6397bae
CB
6700 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
6701 device->total_bytes = device->disk_total_bytes;
935e5cc9 6702 device->commit_total_bytes = device->disk_total_bytes;
0b86a832 6703 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
ce7213c7 6704 device->commit_bytes_used = device->bytes_used;
0b86a832
CM
6705 device->type = btrfs_device_type(leaf, dev_item);
6706 device->io_align = btrfs_device_io_align(leaf, dev_item);
6707 device->io_width = btrfs_device_io_width(leaf, dev_item);
6708 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
8dabb742 6709 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
401e29c1 6710 clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
0b86a832 6711
410ba3a2 6712 ptr = btrfs_device_uuid(dev_item);
e17cade2 6713 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
0b86a832
CM
6714}
6715
2ff7e61e 6716static struct btrfs_fs_devices *open_seed_devices(struct btrfs_fs_info *fs_info,
5f375835 6717 u8 *fsid)
2b82032c
YZ
6718{
6719 struct btrfs_fs_devices *fs_devices;
6720 int ret;
6721
a32bf9a3 6722 lockdep_assert_held(&uuid_mutex);
2dfeca9b 6723 ASSERT(fsid);
2b82032c 6724
427c8fdd 6725 /* This will match only for multi-device seed fs */
944d3f9f 6726 list_for_each_entry(fs_devices, &fs_info->fs_devices->seed_list, seed_list)
44880fdc 6727 if (!memcmp(fs_devices->fsid, fsid, BTRFS_FSID_SIZE))
5f375835
MX
6728 return fs_devices;
6729
2b82032c 6730
7239ff4b 6731 fs_devices = find_fsid(fsid, NULL);
2b82032c 6732 if (!fs_devices) {
0b246afa 6733 if (!btrfs_test_opt(fs_info, DEGRADED))
5f375835
MX
6734 return ERR_PTR(-ENOENT);
6735
7239ff4b 6736 fs_devices = alloc_fs_devices(fsid, NULL);
5f375835
MX
6737 if (IS_ERR(fs_devices))
6738 return fs_devices;
6739
0395d84f 6740 fs_devices->seeding = true;
5f375835
MX
6741 fs_devices->opened = 1;
6742 return fs_devices;
2b82032c 6743 }
e4404d6e 6744
427c8fdd
NB
6745 /*
6746 * Upon first call for a seed fs fsid, just create a private copy of the
6747 * respective fs_devices and anchor it at fs_info->fs_devices->seed_list
6748 */
e4404d6e 6749 fs_devices = clone_fs_devices(fs_devices);
5f375835
MX
6750 if (IS_ERR(fs_devices))
6751 return fs_devices;
2b82032c 6752
897fb573 6753 ret = open_fs_devices(fs_devices, FMODE_READ, fs_info->bdev_holder);
48d28232
JL
6754 if (ret) {
6755 free_fs_devices(fs_devices);
5f375835 6756 fs_devices = ERR_PTR(ret);
2b82032c 6757 goto out;
48d28232 6758 }
2b82032c
YZ
6759
6760 if (!fs_devices->seeding) {
0226e0eb 6761 close_fs_devices(fs_devices);
e4404d6e 6762 free_fs_devices(fs_devices);
5f375835 6763 fs_devices = ERR_PTR(-EINVAL);
2b82032c
YZ
6764 goto out;
6765 }
6766
944d3f9f 6767 list_add(&fs_devices->seed_list, &fs_info->fs_devices->seed_list);
2b82032c 6768out:
5f375835 6769 return fs_devices;
2b82032c
YZ
6770}
6771
17850759 6772static int read_one_dev(struct extent_buffer *leaf,
0b86a832
CM
6773 struct btrfs_dev_item *dev_item)
6774{
17850759 6775 struct btrfs_fs_info *fs_info = leaf->fs_info;
0b246afa 6776 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
0b86a832
CM
6777 struct btrfs_device *device;
6778 u64 devid;
6779 int ret;
44880fdc 6780 u8 fs_uuid[BTRFS_FSID_SIZE];
a443755f
CM
6781 u8 dev_uuid[BTRFS_UUID_SIZE];
6782
0b86a832 6783 devid = btrfs_device_id(leaf, dev_item);
410ba3a2 6784 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
a443755f 6785 BTRFS_UUID_SIZE);
1473b24e 6786 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
44880fdc 6787 BTRFS_FSID_SIZE);
2b82032c 6788
de37aa51 6789 if (memcmp(fs_uuid, fs_devices->metadata_uuid, BTRFS_FSID_SIZE)) {
2ff7e61e 6790 fs_devices = open_seed_devices(fs_info, fs_uuid);
5f375835
MX
6791 if (IS_ERR(fs_devices))
6792 return PTR_ERR(fs_devices);
2b82032c
YZ
6793 }
6794
e4319cd9 6795 device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
09ba3bc9 6796 fs_uuid, true);
5f375835 6797 if (!device) {
c5502451 6798 if (!btrfs_test_opt(fs_info, DEGRADED)) {
2b902dfc
AJ
6799 btrfs_report_missing_device(fs_info, devid,
6800 dev_uuid, true);
45dbdbc9 6801 return -ENOENT;
c5502451 6802 }
2b82032c 6803
2ff7e61e 6804 device = add_missing_dev(fs_devices, devid, dev_uuid);
adfb69af
AJ
6805 if (IS_ERR(device)) {
6806 btrfs_err(fs_info,
6807 "failed to add missing dev %llu: %ld",
6808 devid, PTR_ERR(device));
6809 return PTR_ERR(device);
6810 }
2b902dfc 6811 btrfs_report_missing_device(fs_info, devid, dev_uuid, false);
5f375835 6812 } else {
c5502451 6813 if (!device->bdev) {
2b902dfc
AJ
6814 if (!btrfs_test_opt(fs_info, DEGRADED)) {
6815 btrfs_report_missing_device(fs_info,
6816 devid, dev_uuid, true);
45dbdbc9 6817 return -ENOENT;
2b902dfc
AJ
6818 }
6819 btrfs_report_missing_device(fs_info, devid,
6820 dev_uuid, false);
c5502451 6821 }
5f375835 6822
e6e674bd
AJ
6823 if (!device->bdev &&
6824 !test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) {
cd02dca5
CM
6825 /*
6826 * this happens when a device that was properly setup
6827 * in the device info lists suddenly goes bad.
6828 * device->bdev is NULL, and so we have to set
6829 * device->missing to one here
6830 */
5f375835 6831 device->fs_devices->missing_devices++;
e6e674bd 6832 set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
2b82032c 6833 }
5f375835
MX
6834
6835 /* Move the device to its own fs_devices */
6836 if (device->fs_devices != fs_devices) {
e6e674bd
AJ
6837 ASSERT(test_bit(BTRFS_DEV_STATE_MISSING,
6838 &device->dev_state));
5f375835
MX
6839
6840 list_move(&device->dev_list, &fs_devices->devices);
6841 device->fs_devices->num_devices--;
6842 fs_devices->num_devices++;
6843
6844 device->fs_devices->missing_devices--;
6845 fs_devices->missing_devices++;
6846
6847 device->fs_devices = fs_devices;
6848 }
2b82032c
YZ
6849 }
6850
0b246afa 6851 if (device->fs_devices != fs_info->fs_devices) {
ebbede42 6852 BUG_ON(test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state));
2b82032c
YZ
6853 if (device->generation !=
6854 btrfs_device_generation(leaf, dev_item))
6855 return -EINVAL;
6324fbf3 6856 }
0b86a832
CM
6857
6858 fill_device_from_item(leaf, dev_item, device);
e12c9621 6859 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
ebbede42 6860 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
401e29c1 6861 !test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
2b82032c 6862 device->fs_devices->total_rw_bytes += device->total_bytes;
a5ed45f8
NB
6863 atomic64_add(device->total_bytes - device->bytes_used,
6864 &fs_info->free_chunk_space);
2bf64758 6865 }
0b86a832 6866 ret = 0;
0b86a832
CM
6867 return ret;
6868}
6869
6bccf3ab 6870int btrfs_read_sys_array(struct btrfs_fs_info *fs_info)
0b86a832 6871{
6bccf3ab 6872 struct btrfs_root *root = fs_info->tree_root;
ab8d0fc4 6873 struct btrfs_super_block *super_copy = fs_info->super_copy;
a061fc8d 6874 struct extent_buffer *sb;
0b86a832 6875 struct btrfs_disk_key *disk_key;
0b86a832 6876 struct btrfs_chunk *chunk;
1ffb22cf
DS
6877 u8 *array_ptr;
6878 unsigned long sb_array_offset;
84eed90f 6879 int ret = 0;
0b86a832
CM
6880 u32 num_stripes;
6881 u32 array_size;
6882 u32 len = 0;
1ffb22cf 6883 u32 cur_offset;
e06cd3dd 6884 u64 type;
84eed90f 6885 struct btrfs_key key;
0b86a832 6886
0b246afa 6887 ASSERT(BTRFS_SUPER_INFO_SIZE <= fs_info->nodesize);
a83fffb7
DS
6888 /*
6889 * This will create extent buffer of nodesize, superblock size is
6890 * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will
6891 * overallocate but we can keep it as-is, only the first page is used.
6892 */
2ff7e61e 6893 sb = btrfs_find_create_tree_block(fs_info, BTRFS_SUPER_INFO_OFFSET);
c871b0f2
LB
6894 if (IS_ERR(sb))
6895 return PTR_ERR(sb);
4db8c528 6896 set_extent_buffer_uptodate(sb);
85d4e461 6897 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
8a334426 6898 /*
01327610 6899 * The sb extent buffer is artificial and just used to read the system array.
4db8c528 6900 * set_extent_buffer_uptodate() call does not properly mark all it's
8a334426
DS
6901 * pages up-to-date when the page is larger: extent does not cover the
6902 * whole page and consequently check_page_uptodate does not find all
6903 * the page's extents up-to-date (the hole beyond sb),
6904 * write_extent_buffer then triggers a WARN_ON.
6905 *
6906 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
6907 * but sb spans only this function. Add an explicit SetPageUptodate call
6908 * to silence the warning eg. on PowerPC 64.
6909 */
09cbfeaf 6910 if (PAGE_SIZE > BTRFS_SUPER_INFO_SIZE)
727011e0 6911 SetPageUptodate(sb->pages[0]);
4008c04a 6912
a061fc8d 6913 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
0b86a832
CM
6914 array_size = btrfs_super_sys_array_size(super_copy);
6915
1ffb22cf
DS
6916 array_ptr = super_copy->sys_chunk_array;
6917 sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array);
6918 cur_offset = 0;
0b86a832 6919
1ffb22cf
DS
6920 while (cur_offset < array_size) {
6921 disk_key = (struct btrfs_disk_key *)array_ptr;
e3540eab
DS
6922 len = sizeof(*disk_key);
6923 if (cur_offset + len > array_size)
6924 goto out_short_read;
6925
0b86a832
CM
6926 btrfs_disk_key_to_cpu(&key, disk_key);
6927
1ffb22cf
DS
6928 array_ptr += len;
6929 sb_array_offset += len;
6930 cur_offset += len;
0b86a832 6931
32ab3d1b
JT
6932 if (key.type != BTRFS_CHUNK_ITEM_KEY) {
6933 btrfs_err(fs_info,
6934 "unexpected item type %u in sys_array at offset %u",
6935 (u32)key.type, cur_offset);
6936 ret = -EIO;
6937 break;
6938 }
f5cdedd7 6939
32ab3d1b
JT
6940 chunk = (struct btrfs_chunk *)sb_array_offset;
6941 /*
6942 * At least one btrfs_chunk with one stripe must be present,
6943 * exact stripe count check comes afterwards
6944 */
6945 len = btrfs_chunk_item_size(1);
6946 if (cur_offset + len > array_size)
6947 goto out_short_read;
e06cd3dd 6948
32ab3d1b
JT
6949 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
6950 if (!num_stripes) {
6951 btrfs_err(fs_info,
6952 "invalid number of stripes %u in sys_array at offset %u",
6953 num_stripes, cur_offset);
6954 ret = -EIO;
6955 break;
6956 }
e3540eab 6957
32ab3d1b
JT
6958 type = btrfs_chunk_type(sb, chunk);
6959 if ((type & BTRFS_BLOCK_GROUP_SYSTEM) == 0) {
ab8d0fc4 6960 btrfs_err(fs_info,
32ab3d1b
JT
6961 "invalid chunk type %llu in sys_array at offset %u",
6962 type, cur_offset);
84eed90f
CM
6963 ret = -EIO;
6964 break;
0b86a832 6965 }
32ab3d1b
JT
6966
6967 len = btrfs_chunk_item_size(num_stripes);
6968 if (cur_offset + len > array_size)
6969 goto out_short_read;
6970
6971 ret = read_one_chunk(&key, sb, chunk);
6972 if (ret)
6973 break;
6974
1ffb22cf
DS
6975 array_ptr += len;
6976 sb_array_offset += len;
6977 cur_offset += len;
0b86a832 6978 }
d865177a 6979 clear_extent_buffer_uptodate(sb);
1c8b5b6e 6980 free_extent_buffer_stale(sb);
84eed90f 6981 return ret;
e3540eab
DS
6982
6983out_short_read:
ab8d0fc4 6984 btrfs_err(fs_info, "sys_array too short to read %u bytes at offset %u",
e3540eab 6985 len, cur_offset);
d865177a 6986 clear_extent_buffer_uptodate(sb);
1c8b5b6e 6987 free_extent_buffer_stale(sb);
e3540eab 6988 return -EIO;
0b86a832
CM
6989}
6990
21634a19
QW
6991/*
6992 * Check if all chunks in the fs are OK for read-write degraded mount
6993 *
6528b99d
AJ
6994 * If the @failing_dev is specified, it's accounted as missing.
6995 *
21634a19
QW
6996 * Return true if all chunks meet the minimal RW mount requirements.
6997 * Return false if any chunk doesn't meet the minimal RW mount requirements.
6998 */
6528b99d
AJ
6999bool btrfs_check_rw_degradable(struct btrfs_fs_info *fs_info,
7000 struct btrfs_device *failing_dev)
21634a19 7001{
c8bf1b67 7002 struct extent_map_tree *map_tree = &fs_info->mapping_tree;
21634a19
QW
7003 struct extent_map *em;
7004 u64 next_start = 0;
7005 bool ret = true;
7006
c8bf1b67
DS
7007 read_lock(&map_tree->lock);
7008 em = lookup_extent_mapping(map_tree, 0, (u64)-1);
7009 read_unlock(&map_tree->lock);
21634a19
QW
7010 /* No chunk at all? Return false anyway */
7011 if (!em) {
7012 ret = false;
7013 goto out;
7014 }
7015 while (em) {
7016 struct map_lookup *map;
7017 int missing = 0;
7018 int max_tolerated;
7019 int i;
7020
7021 map = em->map_lookup;
7022 max_tolerated =
7023 btrfs_get_num_tolerated_disk_barrier_failures(
7024 map->type);
7025 for (i = 0; i < map->num_stripes; i++) {
7026 struct btrfs_device *dev = map->stripes[i].dev;
7027
e6e674bd
AJ
7028 if (!dev || !dev->bdev ||
7029 test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state) ||
21634a19
QW
7030 dev->last_flush_error)
7031 missing++;
6528b99d
AJ
7032 else if (failing_dev && failing_dev == dev)
7033 missing++;
21634a19
QW
7034 }
7035 if (missing > max_tolerated) {
6528b99d
AJ
7036 if (!failing_dev)
7037 btrfs_warn(fs_info,
52042d8e 7038 "chunk %llu missing %d devices, max tolerance is %d for writable mount",
21634a19
QW
7039 em->start, missing, max_tolerated);
7040 free_extent_map(em);
7041 ret = false;
7042 goto out;
7043 }
7044 next_start = extent_map_end(em);
7045 free_extent_map(em);
7046
c8bf1b67
DS
7047 read_lock(&map_tree->lock);
7048 em = lookup_extent_mapping(map_tree, next_start,
21634a19 7049 (u64)(-1) - next_start);
c8bf1b67 7050 read_unlock(&map_tree->lock);
21634a19
QW
7051 }
7052out:
7053 return ret;
7054}
7055
d85327b1
DS
7056static void readahead_tree_node_children(struct extent_buffer *node)
7057{
7058 int i;
7059 const int nr_items = btrfs_header_nritems(node);
7060
7061 for (i = 0; i < nr_items; i++) {
7062 u64 start;
7063
7064 start = btrfs_node_blockptr(node, i);
7065 readahead_tree_block(node->fs_info, start);
7066 }
7067}
7068
5b4aacef 7069int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info)
0b86a832 7070{
5b4aacef 7071 struct btrfs_root *root = fs_info->chunk_root;
0b86a832
CM
7072 struct btrfs_path *path;
7073 struct extent_buffer *leaf;
7074 struct btrfs_key key;
7075 struct btrfs_key found_key;
7076 int ret;
7077 int slot;
99e3ecfc 7078 u64 total_dev = 0;
d85327b1 7079 u64 last_ra_node = 0;
0b86a832 7080
0b86a832
CM
7081 path = btrfs_alloc_path();
7082 if (!path)
7083 return -ENOMEM;
7084
3dd0f7a3
AJ
7085 /*
7086 * uuid_mutex is needed only if we are mounting a sprout FS
7087 * otherwise we don't need it.
7088 */
b367e47f 7089 mutex_lock(&uuid_mutex);
b367e47f 7090
48cfa61b
BB
7091 /*
7092 * It is possible for mount and umount to race in such a way that
7093 * we execute this code path, but open_fs_devices failed to clear
7094 * total_rw_bytes. We certainly want it cleared before reading the
7095 * device items, so clear it here.
7096 */
7097 fs_info->fs_devices->total_rw_bytes = 0;
7098
395927a9
FDBM
7099 /*
7100 * Read all device items, and then all the chunk items. All
7101 * device items are found before any chunk item (their object id
7102 * is smaller than the lowest possible object id for a chunk
7103 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
0b86a832
CM
7104 */
7105 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
7106 key.offset = 0;
7107 key.type = 0;
0b86a832 7108 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
ab59381e
ZL
7109 if (ret < 0)
7110 goto error;
d397712b 7111 while (1) {
d85327b1
DS
7112 struct extent_buffer *node;
7113
0b86a832
CM
7114 leaf = path->nodes[0];
7115 slot = path->slots[0];
7116 if (slot >= btrfs_header_nritems(leaf)) {
7117 ret = btrfs_next_leaf(root, path);
7118 if (ret == 0)
7119 continue;
7120 if (ret < 0)
7121 goto error;
7122 break;
7123 }
d85327b1
DS
7124 /*
7125 * The nodes on level 1 are not locked but we don't need to do
7126 * that during mount time as nothing else can access the tree
7127 */
7128 node = path->nodes[1];
7129 if (node) {
7130 if (last_ra_node != node->start) {
7131 readahead_tree_node_children(node);
7132 last_ra_node = node->start;
7133 }
7134 }
0b86a832 7135 btrfs_item_key_to_cpu(leaf, &found_key, slot);
395927a9
FDBM
7136 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
7137 struct btrfs_dev_item *dev_item;
7138 dev_item = btrfs_item_ptr(leaf, slot,
0b86a832 7139 struct btrfs_dev_item);
17850759 7140 ret = read_one_dev(leaf, dev_item);
395927a9
FDBM
7141 if (ret)
7142 goto error;
99e3ecfc 7143 total_dev++;
0b86a832
CM
7144 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
7145 struct btrfs_chunk *chunk;
7146 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
01d01caf 7147 mutex_lock(&fs_info->chunk_mutex);
9690ac09 7148 ret = read_one_chunk(&found_key, leaf, chunk);
01d01caf 7149 mutex_unlock(&fs_info->chunk_mutex);
2b82032c
YZ
7150 if (ret)
7151 goto error;
0b86a832
CM
7152 }
7153 path->slots[0]++;
7154 }
99e3ecfc
LB
7155
7156 /*
7157 * After loading chunk tree, we've got all device information,
7158 * do another round of validation checks.
7159 */
0b246afa
JM
7160 if (total_dev != fs_info->fs_devices->total_devices) {
7161 btrfs_err(fs_info,
99e3ecfc 7162 "super_num_devices %llu mismatch with num_devices %llu found here",
0b246afa 7163 btrfs_super_num_devices(fs_info->super_copy),
99e3ecfc
LB
7164 total_dev);
7165 ret = -EINVAL;
7166 goto error;
7167 }
0b246afa
JM
7168 if (btrfs_super_total_bytes(fs_info->super_copy) <
7169 fs_info->fs_devices->total_rw_bytes) {
7170 btrfs_err(fs_info,
99e3ecfc 7171 "super_total_bytes %llu mismatch with fs_devices total_rw_bytes %llu",
0b246afa
JM
7172 btrfs_super_total_bytes(fs_info->super_copy),
7173 fs_info->fs_devices->total_rw_bytes);
99e3ecfc
LB
7174 ret = -EINVAL;
7175 goto error;
7176 }
0b86a832
CM
7177 ret = 0;
7178error:
b367e47f
LZ
7179 mutex_unlock(&uuid_mutex);
7180
2b82032c 7181 btrfs_free_path(path);
0b86a832
CM
7182 return ret;
7183}
442a4f63 7184
cb517eab
MX
7185void btrfs_init_devices_late(struct btrfs_fs_info *fs_info)
7186{
944d3f9f 7187 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices, *seed_devs;
cb517eab
MX
7188 struct btrfs_device *device;
7189
944d3f9f
NB
7190 fs_devices->fs_info = fs_info;
7191
7192 mutex_lock(&fs_devices->device_list_mutex);
7193 list_for_each_entry(device, &fs_devices->devices, dev_list)
7194 device->fs_info = fs_info;
944d3f9f
NB
7195
7196 list_for_each_entry(seed_devs, &fs_devices->seed_list, seed_list) {
944d3f9f 7197 list_for_each_entry(device, &seed_devs->devices, dev_list)
fb456252 7198 device->fs_info = fs_info;
29cc83f6 7199
944d3f9f 7200 seed_devs->fs_info = fs_info;
29cc83f6 7201 }
e17125b5 7202 mutex_unlock(&fs_devices->device_list_mutex);
cb517eab
MX
7203}
7204
1dc990df
DS
7205static u64 btrfs_dev_stats_value(const struct extent_buffer *eb,
7206 const struct btrfs_dev_stats_item *ptr,
7207 int index)
7208{
7209 u64 val;
7210
7211 read_extent_buffer(eb, &val,
7212 offsetof(struct btrfs_dev_stats_item, values) +
7213 ((unsigned long)ptr) + (index * sizeof(u64)),
7214 sizeof(val));
7215 return val;
7216}
7217
7218static void btrfs_set_dev_stats_value(struct extent_buffer *eb,
7219 struct btrfs_dev_stats_item *ptr,
7220 int index, u64 val)
7221{
7222 write_extent_buffer(eb, &val,
7223 offsetof(struct btrfs_dev_stats_item, values) +
7224 ((unsigned long)ptr) + (index * sizeof(u64)),
7225 sizeof(val));
7226}
7227
733f4fbb
SB
7228int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
7229{
7230 struct btrfs_key key;
733f4fbb
SB
7231 struct btrfs_root *dev_root = fs_info->dev_root;
7232 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7233 struct extent_buffer *eb;
7234 int slot;
7235 int ret = 0;
7236 struct btrfs_device *device;
7237 struct btrfs_path *path = NULL;
7238 int i;
7239
7240 path = btrfs_alloc_path();
3b80a984
AJ
7241 if (!path)
7242 return -ENOMEM;
733f4fbb
SB
7243
7244 mutex_lock(&fs_devices->device_list_mutex);
7245 list_for_each_entry(device, &fs_devices->devices, dev_list) {
7246 int item_size;
7247 struct btrfs_dev_stats_item *ptr;
7248
242e2956
DS
7249 key.objectid = BTRFS_DEV_STATS_OBJECTID;
7250 key.type = BTRFS_PERSISTENT_ITEM_KEY;
733f4fbb
SB
7251 key.offset = device->devid;
7252 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
7253 if (ret) {
ae4b9b4c
AJ
7254 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7255 btrfs_dev_stat_set(device, i, 0);
733f4fbb
SB
7256 device->dev_stats_valid = 1;
7257 btrfs_release_path(path);
7258 continue;
7259 }
7260 slot = path->slots[0];
7261 eb = path->nodes[0];
733f4fbb
SB
7262 item_size = btrfs_item_size_nr(eb, slot);
7263
7264 ptr = btrfs_item_ptr(eb, slot,
7265 struct btrfs_dev_stats_item);
7266
7267 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
7268 if (item_size >= (1 + i) * sizeof(__le64))
7269 btrfs_dev_stat_set(device, i,
7270 btrfs_dev_stats_value(eb, ptr, i));
7271 else
4e411a7d 7272 btrfs_dev_stat_set(device, i, 0);
733f4fbb
SB
7273 }
7274
7275 device->dev_stats_valid = 1;
7276 btrfs_dev_stat_print_on_load(device);
7277 btrfs_release_path(path);
7278 }
7279 mutex_unlock(&fs_devices->device_list_mutex);
7280
733f4fbb
SB
7281 btrfs_free_path(path);
7282 return ret < 0 ? ret : 0;
7283}
7284
7285static int update_dev_stat_item(struct btrfs_trans_handle *trans,
733f4fbb
SB
7286 struct btrfs_device *device)
7287{
5495f195 7288 struct btrfs_fs_info *fs_info = trans->fs_info;
6bccf3ab 7289 struct btrfs_root *dev_root = fs_info->dev_root;
733f4fbb
SB
7290 struct btrfs_path *path;
7291 struct btrfs_key key;
7292 struct extent_buffer *eb;
7293 struct btrfs_dev_stats_item *ptr;
7294 int ret;
7295 int i;
7296
242e2956
DS
7297 key.objectid = BTRFS_DEV_STATS_OBJECTID;
7298 key.type = BTRFS_PERSISTENT_ITEM_KEY;
733f4fbb
SB
7299 key.offset = device->devid;
7300
7301 path = btrfs_alloc_path();
fa252992
DS
7302 if (!path)
7303 return -ENOMEM;
733f4fbb
SB
7304 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
7305 if (ret < 0) {
0b246afa 7306 btrfs_warn_in_rcu(fs_info,
ecaeb14b 7307 "error %d while searching for dev_stats item for device %s",
606686ee 7308 ret, rcu_str_deref(device->name));
733f4fbb
SB
7309 goto out;
7310 }
7311
7312 if (ret == 0 &&
7313 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
7314 /* need to delete old one and insert a new one */
7315 ret = btrfs_del_item(trans, dev_root, path);
7316 if (ret != 0) {
0b246afa 7317 btrfs_warn_in_rcu(fs_info,
ecaeb14b 7318 "delete too small dev_stats item for device %s failed %d",
606686ee 7319 rcu_str_deref(device->name), ret);
733f4fbb
SB
7320 goto out;
7321 }
7322 ret = 1;
7323 }
7324
7325 if (ret == 1) {
7326 /* need to insert a new item */
7327 btrfs_release_path(path);
7328 ret = btrfs_insert_empty_item(trans, dev_root, path,
7329 &key, sizeof(*ptr));
7330 if (ret < 0) {
0b246afa 7331 btrfs_warn_in_rcu(fs_info,
ecaeb14b
DS
7332 "insert dev_stats item for device %s failed %d",
7333 rcu_str_deref(device->name), ret);
733f4fbb
SB
7334 goto out;
7335 }
7336 }
7337
7338 eb = path->nodes[0];
7339 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
7340 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7341 btrfs_set_dev_stats_value(eb, ptr, i,
7342 btrfs_dev_stat_read(device, i));
7343 btrfs_mark_buffer_dirty(eb);
7344
7345out:
7346 btrfs_free_path(path);
7347 return ret;
7348}
7349
7350/*
7351 * called from commit_transaction. Writes all changed device stats to disk.
7352 */
196c9d8d 7353int btrfs_run_dev_stats(struct btrfs_trans_handle *trans)
733f4fbb 7354{
196c9d8d 7355 struct btrfs_fs_info *fs_info = trans->fs_info;
733f4fbb
SB
7356 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7357 struct btrfs_device *device;
addc3fa7 7358 int stats_cnt;
733f4fbb
SB
7359 int ret = 0;
7360
7361 mutex_lock(&fs_devices->device_list_mutex);
7362 list_for_each_entry(device, &fs_devices->devices, dev_list) {
9deae968
NB
7363 stats_cnt = atomic_read(&device->dev_stats_ccnt);
7364 if (!device->dev_stats_valid || stats_cnt == 0)
733f4fbb
SB
7365 continue;
7366
9deae968
NB
7367
7368 /*
7369 * There is a LOAD-LOAD control dependency between the value of
7370 * dev_stats_ccnt and updating the on-disk values which requires
7371 * reading the in-memory counters. Such control dependencies
7372 * require explicit read memory barriers.
7373 *
7374 * This memory barriers pairs with smp_mb__before_atomic in
7375 * btrfs_dev_stat_inc/btrfs_dev_stat_set and with the full
7376 * barrier implied by atomic_xchg in
7377 * btrfs_dev_stats_read_and_reset
7378 */
7379 smp_rmb();
7380
5495f195 7381 ret = update_dev_stat_item(trans, device);
733f4fbb 7382 if (!ret)
addc3fa7 7383 atomic_sub(stats_cnt, &device->dev_stats_ccnt);
733f4fbb
SB
7384 }
7385 mutex_unlock(&fs_devices->device_list_mutex);
7386
7387 return ret;
7388}
7389
442a4f63
SB
7390void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
7391{
7392 btrfs_dev_stat_inc(dev, index);
7393 btrfs_dev_stat_print_on_error(dev);
7394}
7395
48a3b636 7396static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
442a4f63 7397{
733f4fbb
SB
7398 if (!dev->dev_stats_valid)
7399 return;
fb456252 7400 btrfs_err_rl_in_rcu(dev->fs_info,
b14af3b4 7401 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
606686ee 7402 rcu_str_deref(dev->name),
442a4f63
SB
7403 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
7404 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
7405 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
efe120a0
FH
7406 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
7407 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
442a4f63 7408}
c11d2c23 7409
733f4fbb
SB
7410static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
7411{
a98cdb85
SB
7412 int i;
7413
7414 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7415 if (btrfs_dev_stat_read(dev, i) != 0)
7416 break;
7417 if (i == BTRFS_DEV_STAT_VALUES_MAX)
7418 return; /* all values == 0, suppress message */
7419
fb456252 7420 btrfs_info_in_rcu(dev->fs_info,
ecaeb14b 7421 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
606686ee 7422 rcu_str_deref(dev->name),
733f4fbb
SB
7423 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
7424 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
7425 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
7426 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
7427 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
7428}
7429
2ff7e61e 7430int btrfs_get_dev_stats(struct btrfs_fs_info *fs_info,
b27f7c0c 7431 struct btrfs_ioctl_get_dev_stats *stats)
c11d2c23
SB
7432{
7433 struct btrfs_device *dev;
0b246afa 7434 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
c11d2c23
SB
7435 int i;
7436
7437 mutex_lock(&fs_devices->device_list_mutex);
09ba3bc9
AJ
7438 dev = btrfs_find_device(fs_info->fs_devices, stats->devid, NULL, NULL,
7439 true);
c11d2c23
SB
7440 mutex_unlock(&fs_devices->device_list_mutex);
7441
7442 if (!dev) {
0b246afa 7443 btrfs_warn(fs_info, "get dev_stats failed, device not found");
c11d2c23 7444 return -ENODEV;
733f4fbb 7445 } else if (!dev->dev_stats_valid) {
0b246afa 7446 btrfs_warn(fs_info, "get dev_stats failed, not yet valid");
733f4fbb 7447 return -ENODEV;
b27f7c0c 7448 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
c11d2c23
SB
7449 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
7450 if (stats->nr_items > i)
7451 stats->values[i] =
7452 btrfs_dev_stat_read_and_reset(dev, i);
7453 else
4e411a7d 7454 btrfs_dev_stat_set(dev, i, 0);
c11d2c23 7455 }
a69976bc
AJ
7456 btrfs_info(fs_info, "device stats zeroed by %s (%d)",
7457 current->comm, task_pid_nr(current));
c11d2c23
SB
7458 } else {
7459 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7460 if (stats->nr_items > i)
7461 stats->values[i] = btrfs_dev_stat_read(dev, i);
7462 }
7463 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
7464 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
7465 return 0;
7466}
a8a6dab7 7467
935e5cc9 7468/*
bbbf7243
NB
7469 * Update the size and bytes used for each device where it changed. This is
7470 * delayed since we would otherwise get errors while writing out the
7471 * superblocks.
7472 *
7473 * Must be invoked during transaction commit.
935e5cc9 7474 */
bbbf7243 7475void btrfs_commit_device_sizes(struct btrfs_transaction *trans)
935e5cc9 7476{
935e5cc9
MX
7477 struct btrfs_device *curr, *next;
7478
bbbf7243 7479 ASSERT(trans->state == TRANS_STATE_COMMIT_DOING);
ce7213c7 7480
bbbf7243 7481 if (list_empty(&trans->dev_update_list))
ce7213c7
MX
7482 return;
7483
bbbf7243
NB
7484 /*
7485 * We don't need the device_list_mutex here. This list is owned by the
7486 * transaction and the transaction must complete before the device is
7487 * released.
7488 */
7489 mutex_lock(&trans->fs_info->chunk_mutex);
7490 list_for_each_entry_safe(curr, next, &trans->dev_update_list,
7491 post_commit_list) {
7492 list_del_init(&curr->post_commit_list);
7493 curr->commit_total_bytes = curr->disk_total_bytes;
7494 curr->commit_bytes_used = curr->bytes_used;
ce7213c7 7495 }
bbbf7243 7496 mutex_unlock(&trans->fs_info->chunk_mutex);
ce7213c7 7497}
5a13f430 7498
46df06b8
DS
7499/*
7500 * Multiplicity factor for simple profiles: DUP, RAID1-like and RAID10.
7501 */
7502int btrfs_bg_type_to_factor(u64 flags)
7503{
44b28ada
DS
7504 const int index = btrfs_bg_flags_to_raid_index(flags);
7505
7506 return btrfs_raid_array[index].ncopies;
46df06b8 7507}
cf90d884
QW
7508
7509
cf90d884
QW
7510
7511static int verify_one_dev_extent(struct btrfs_fs_info *fs_info,
7512 u64 chunk_offset, u64 devid,
7513 u64 physical_offset, u64 physical_len)
7514{
c8bf1b67 7515 struct extent_map_tree *em_tree = &fs_info->mapping_tree;
cf90d884
QW
7516 struct extent_map *em;
7517 struct map_lookup *map;
05a37c48 7518 struct btrfs_device *dev;
cf90d884
QW
7519 u64 stripe_len;
7520 bool found = false;
7521 int ret = 0;
7522 int i;
7523
7524 read_lock(&em_tree->lock);
7525 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
7526 read_unlock(&em_tree->lock);
7527
7528 if (!em) {
7529 btrfs_err(fs_info,
7530"dev extent physical offset %llu on devid %llu doesn't have corresponding chunk",
7531 physical_offset, devid);
7532 ret = -EUCLEAN;
7533 goto out;
7534 }
7535
7536 map = em->map_lookup;
7537 stripe_len = calc_stripe_length(map->type, em->len, map->num_stripes);
7538 if (physical_len != stripe_len) {
7539 btrfs_err(fs_info,
7540"dev extent physical offset %llu on devid %llu length doesn't match chunk %llu, have %llu expect %llu",
7541 physical_offset, devid, em->start, physical_len,
7542 stripe_len);
7543 ret = -EUCLEAN;
7544 goto out;
7545 }
7546
7547 for (i = 0; i < map->num_stripes; i++) {
7548 if (map->stripes[i].dev->devid == devid &&
7549 map->stripes[i].physical == physical_offset) {
7550 found = true;
7551 if (map->verified_stripes >= map->num_stripes) {
7552 btrfs_err(fs_info,
7553 "too many dev extents for chunk %llu found",
7554 em->start);
7555 ret = -EUCLEAN;
7556 goto out;
7557 }
7558 map->verified_stripes++;
7559 break;
7560 }
7561 }
7562 if (!found) {
7563 btrfs_err(fs_info,
7564 "dev extent physical offset %llu devid %llu has no corresponding chunk",
7565 physical_offset, devid);
7566 ret = -EUCLEAN;
7567 }
05a37c48
QW
7568
7569 /* Make sure no dev extent is beyond device bondary */
09ba3bc9 7570 dev = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL, true);
05a37c48
QW
7571 if (!dev) {
7572 btrfs_err(fs_info, "failed to find devid %llu", devid);
7573 ret = -EUCLEAN;
7574 goto out;
7575 }
1b3922a8
QW
7576
7577 /* It's possible this device is a dummy for seed device */
7578 if (dev->disk_total_bytes == 0) {
944d3f9f
NB
7579 struct btrfs_fs_devices *devs;
7580
7581 devs = list_first_entry(&fs_info->fs_devices->seed_list,
7582 struct btrfs_fs_devices, seed_list);
7583 dev = btrfs_find_device(devs, devid, NULL, NULL, false);
1b3922a8
QW
7584 if (!dev) {
7585 btrfs_err(fs_info, "failed to find seed devid %llu",
7586 devid);
7587 ret = -EUCLEAN;
7588 goto out;
7589 }
7590 }
7591
05a37c48
QW
7592 if (physical_offset + physical_len > dev->disk_total_bytes) {
7593 btrfs_err(fs_info,
7594"dev extent devid %llu physical offset %llu len %llu is beyond device boundary %llu",
7595 devid, physical_offset, physical_len,
7596 dev->disk_total_bytes);
7597 ret = -EUCLEAN;
7598 goto out;
7599 }
cf90d884
QW
7600out:
7601 free_extent_map(em);
7602 return ret;
7603}
7604
7605static int verify_chunk_dev_extent_mapping(struct btrfs_fs_info *fs_info)
7606{
c8bf1b67 7607 struct extent_map_tree *em_tree = &fs_info->mapping_tree;
cf90d884
QW
7608 struct extent_map *em;
7609 struct rb_node *node;
7610 int ret = 0;
7611
7612 read_lock(&em_tree->lock);
07e1ce09 7613 for (node = rb_first_cached(&em_tree->map); node; node = rb_next(node)) {
cf90d884
QW
7614 em = rb_entry(node, struct extent_map, rb_node);
7615 if (em->map_lookup->num_stripes !=
7616 em->map_lookup->verified_stripes) {
7617 btrfs_err(fs_info,
7618 "chunk %llu has missing dev extent, have %d expect %d",
7619 em->start, em->map_lookup->verified_stripes,
7620 em->map_lookup->num_stripes);
7621 ret = -EUCLEAN;
7622 goto out;
7623 }
7624 }
7625out:
7626 read_unlock(&em_tree->lock);
7627 return ret;
7628}
7629
7630/*
7631 * Ensure that all dev extents are mapped to correct chunk, otherwise
7632 * later chunk allocation/free would cause unexpected behavior.
7633 *
7634 * NOTE: This will iterate through the whole device tree, which should be of
7635 * the same size level as the chunk tree. This slightly increases mount time.
7636 */
7637int btrfs_verify_dev_extents(struct btrfs_fs_info *fs_info)
7638{
7639 struct btrfs_path *path;
7640 struct btrfs_root *root = fs_info->dev_root;
7641 struct btrfs_key key;
5eb19381
QW
7642 u64 prev_devid = 0;
7643 u64 prev_dev_ext_end = 0;
cf90d884
QW
7644 int ret = 0;
7645
7646 key.objectid = 1;
7647 key.type = BTRFS_DEV_EXTENT_KEY;
7648 key.offset = 0;
7649
7650 path = btrfs_alloc_path();
7651 if (!path)
7652 return -ENOMEM;
7653
7654 path->reada = READA_FORWARD;
7655 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
7656 if (ret < 0)
7657 goto out;
7658
7659 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
7660 ret = btrfs_next_item(root, path);
7661 if (ret < 0)
7662 goto out;
7663 /* No dev extents at all? Not good */
7664 if (ret > 0) {
7665 ret = -EUCLEAN;
7666 goto out;
7667 }
7668 }
7669 while (1) {
7670 struct extent_buffer *leaf = path->nodes[0];
7671 struct btrfs_dev_extent *dext;
7672 int slot = path->slots[0];
7673 u64 chunk_offset;
7674 u64 physical_offset;
7675 u64 physical_len;
7676 u64 devid;
7677
7678 btrfs_item_key_to_cpu(leaf, &key, slot);
7679 if (key.type != BTRFS_DEV_EXTENT_KEY)
7680 break;
7681 devid = key.objectid;
7682 physical_offset = key.offset;
7683
7684 dext = btrfs_item_ptr(leaf, slot, struct btrfs_dev_extent);
7685 chunk_offset = btrfs_dev_extent_chunk_offset(leaf, dext);
7686 physical_len = btrfs_dev_extent_length(leaf, dext);
7687
5eb19381
QW
7688 /* Check if this dev extent overlaps with the previous one */
7689 if (devid == prev_devid && physical_offset < prev_dev_ext_end) {
7690 btrfs_err(fs_info,
7691"dev extent devid %llu physical offset %llu overlap with previous dev extent end %llu",
7692 devid, physical_offset, prev_dev_ext_end);
7693 ret = -EUCLEAN;
7694 goto out;
7695 }
7696
cf90d884
QW
7697 ret = verify_one_dev_extent(fs_info, chunk_offset, devid,
7698 physical_offset, physical_len);
7699 if (ret < 0)
7700 goto out;
5eb19381
QW
7701 prev_devid = devid;
7702 prev_dev_ext_end = physical_offset + physical_len;
7703
cf90d884
QW
7704 ret = btrfs_next_item(root, path);
7705 if (ret < 0)
7706 goto out;
7707 if (ret > 0) {
7708 ret = 0;
7709 break;
7710 }
7711 }
7712
7713 /* Ensure all chunks have corresponding dev extents */
7714 ret = verify_chunk_dev_extent_mapping(fs_info);
7715out:
7716 btrfs_free_path(path);
7717 return ret;
7718}
eede2bf3
OS
7719
7720/*
7721 * Check whether the given block group or device is pinned by any inode being
7722 * used as a swapfile.
7723 */
7724bool btrfs_pinned_by_swapfile(struct btrfs_fs_info *fs_info, void *ptr)
7725{
7726 struct btrfs_swapfile_pin *sp;
7727 struct rb_node *node;
7728
7729 spin_lock(&fs_info->swapfile_pins_lock);
7730 node = fs_info->swapfile_pins.rb_node;
7731 while (node) {
7732 sp = rb_entry(node, struct btrfs_swapfile_pin, node);
7733 if (ptr < sp->ptr)
7734 node = node->rb_left;
7735 else if (ptr > sp->ptr)
7736 node = node->rb_right;
7737 else
7738 break;
7739 }
7740 spin_unlock(&fs_info->swapfile_pins_lock);
7741 return node != NULL;
7742}