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