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