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