xref: /openbmc/linux/fs/btrfs/dev-replace.c (revision bef16b52)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) STRATO AG 2012.  All rights reserved.
4  */
5 
6 #include <linux/sched.h>
7 #include <linux/bio.h>
8 #include <linux/slab.h>
9 #include <linux/blkdev.h>
10 #include <linux/kthread.h>
11 #include <linux/math64.h>
12 #include "misc.h"
13 #include "ctree.h"
14 #include "extent_map.h"
15 #include "disk-io.h"
16 #include "transaction.h"
17 #include "print-tree.h"
18 #include "volumes.h"
19 #include "async-thread.h"
20 #include "check-integrity.h"
21 #include "rcu-string.h"
22 #include "dev-replace.h"
23 #include "sysfs.h"
24 #include "zoned.h"
25 #include "block-group.h"
26 
27 /*
28  * Device replace overview
29  *
30  * [Objective]
31  * To copy all extents (both new and on-disk) from source device to target
32  * device, while still keeping the filesystem read-write.
33  *
34  * [Method]
35  * There are two main methods involved:
36  *
37  * - Write duplication
38  *
39  *   All new writes will be written to both target and source devices, so even
40  *   if replace gets canceled, sources device still contains up-to-date data.
41  *
42  *   Location:		handle_ops_on_dev_replace() from __btrfs_map_block()
43  *   Start:		btrfs_dev_replace_start()
44  *   End:		btrfs_dev_replace_finishing()
45  *   Content:		Latest data/metadata
46  *
47  * - Copy existing extents
48  *
49  *   This happens by re-using scrub facility, as scrub also iterates through
50  *   existing extents from commit root.
51  *
52  *   Location:		scrub_write_block_to_dev_replace() from
53  *   			scrub_block_complete()
54  *   Content:		Data/meta from commit root.
55  *
56  * Due to the content difference, we need to avoid nocow write when dev-replace
57  * is happening.  This is done by marking the block group read-only and waiting
58  * for NOCOW writes.
59  *
60  * After replace is done, the finishing part is done by swapping the target and
61  * source devices.
62  *
63  *   Location:		btrfs_dev_replace_update_device_in_mapping_tree() from
64  *   			btrfs_dev_replace_finishing()
65  */
66 
67 static int btrfs_dev_replace_finishing(struct btrfs_fs_info *fs_info,
68 				       int scrub_ret);
69 static int btrfs_dev_replace_kthread(void *data);
70 
71 int btrfs_init_dev_replace(struct btrfs_fs_info *fs_info)
72 {
73 	struct btrfs_dev_lookup_args args = { .devid = BTRFS_DEV_REPLACE_DEVID };
74 	struct btrfs_key key;
75 	struct btrfs_root *dev_root = fs_info->dev_root;
76 	struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
77 	struct extent_buffer *eb;
78 	int slot;
79 	int ret = 0;
80 	struct btrfs_path *path = NULL;
81 	int item_size;
82 	struct btrfs_dev_replace_item *ptr;
83 	u64 src_devid;
84 
85 	if (!dev_root)
86 		return 0;
87 
88 	path = btrfs_alloc_path();
89 	if (!path) {
90 		ret = -ENOMEM;
91 		goto out;
92 	}
93 
94 	key.objectid = 0;
95 	key.type = BTRFS_DEV_REPLACE_KEY;
96 	key.offset = 0;
97 	ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
98 	if (ret) {
99 no_valid_dev_replace_entry_found:
100 		/*
101 		 * We don't have a replace item or it's corrupted.  If there is
102 		 * a replace target, fail the mount.
103 		 */
104 		if (btrfs_find_device(fs_info->fs_devices, &args)) {
105 			btrfs_err(fs_info,
106 			"found replace target device without a valid replace item");
107 			ret = -EUCLEAN;
108 			goto out;
109 		}
110 		ret = 0;
111 		dev_replace->replace_state =
112 			BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED;
113 		dev_replace->cont_reading_from_srcdev_mode =
114 		    BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_ALWAYS;
115 		dev_replace->time_started = 0;
116 		dev_replace->time_stopped = 0;
117 		atomic64_set(&dev_replace->num_write_errors, 0);
118 		atomic64_set(&dev_replace->num_uncorrectable_read_errors, 0);
119 		dev_replace->cursor_left = 0;
120 		dev_replace->committed_cursor_left = 0;
121 		dev_replace->cursor_left_last_write_of_item = 0;
122 		dev_replace->cursor_right = 0;
123 		dev_replace->srcdev = NULL;
124 		dev_replace->tgtdev = NULL;
125 		dev_replace->is_valid = 0;
126 		dev_replace->item_needs_writeback = 0;
127 		goto out;
128 	}
129 	slot = path->slots[0];
130 	eb = path->nodes[0];
131 	item_size = btrfs_item_size(eb, slot);
132 	ptr = btrfs_item_ptr(eb, slot, struct btrfs_dev_replace_item);
133 
134 	if (item_size != sizeof(struct btrfs_dev_replace_item)) {
135 		btrfs_warn(fs_info,
136 			"dev_replace entry found has unexpected size, ignore entry");
137 		goto no_valid_dev_replace_entry_found;
138 	}
139 
140 	src_devid = btrfs_dev_replace_src_devid(eb, ptr);
141 	dev_replace->cont_reading_from_srcdev_mode =
142 		btrfs_dev_replace_cont_reading_from_srcdev_mode(eb, ptr);
143 	dev_replace->replace_state = btrfs_dev_replace_replace_state(eb, ptr);
144 	dev_replace->time_started = btrfs_dev_replace_time_started(eb, ptr);
145 	dev_replace->time_stopped =
146 		btrfs_dev_replace_time_stopped(eb, ptr);
147 	atomic64_set(&dev_replace->num_write_errors,
148 		     btrfs_dev_replace_num_write_errors(eb, ptr));
149 	atomic64_set(&dev_replace->num_uncorrectable_read_errors,
150 		     btrfs_dev_replace_num_uncorrectable_read_errors(eb, ptr));
151 	dev_replace->cursor_left = btrfs_dev_replace_cursor_left(eb, ptr);
152 	dev_replace->committed_cursor_left = dev_replace->cursor_left;
153 	dev_replace->cursor_left_last_write_of_item = dev_replace->cursor_left;
154 	dev_replace->cursor_right = btrfs_dev_replace_cursor_right(eb, ptr);
155 	dev_replace->is_valid = 1;
156 
157 	dev_replace->item_needs_writeback = 0;
158 	switch (dev_replace->replace_state) {
159 	case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
160 	case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
161 	case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
162 		/*
163 		 * We don't have an active replace item but if there is a
164 		 * replace target, fail the mount.
165 		 */
166 		if (btrfs_find_device(fs_info->fs_devices, &args)) {
167 			btrfs_err(fs_info,
168 			"replace devid present without an active replace item");
169 			ret = -EUCLEAN;
170 		} else {
171 			dev_replace->srcdev = NULL;
172 			dev_replace->tgtdev = NULL;
173 		}
174 		break;
175 	case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
176 	case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
177 		dev_replace->tgtdev = btrfs_find_device(fs_info->fs_devices, &args);
178 		args.devid = src_devid;
179 		dev_replace->srcdev = btrfs_find_device(fs_info->fs_devices, &args);
180 
181 		/*
182 		 * allow 'btrfs dev replace_cancel' if src/tgt device is
183 		 * missing
184 		 */
185 		if (!dev_replace->srcdev &&
186 		    !btrfs_test_opt(fs_info, DEGRADED)) {
187 			ret = -EIO;
188 			btrfs_warn(fs_info,
189 			   "cannot mount because device replace operation is ongoing and");
190 			btrfs_warn(fs_info,
191 			   "srcdev (devid %llu) is missing, need to run 'btrfs dev scan'?",
192 			   src_devid);
193 		}
194 		if (!dev_replace->tgtdev &&
195 		    !btrfs_test_opt(fs_info, DEGRADED)) {
196 			ret = -EIO;
197 			btrfs_warn(fs_info,
198 			   "cannot mount because device replace operation is ongoing and");
199 			btrfs_warn(fs_info,
200 			   "tgtdev (devid %llu) is missing, need to run 'btrfs dev scan'?",
201 				BTRFS_DEV_REPLACE_DEVID);
202 		}
203 		if (dev_replace->tgtdev) {
204 			if (dev_replace->srcdev) {
205 				dev_replace->tgtdev->total_bytes =
206 					dev_replace->srcdev->total_bytes;
207 				dev_replace->tgtdev->disk_total_bytes =
208 					dev_replace->srcdev->disk_total_bytes;
209 				dev_replace->tgtdev->commit_total_bytes =
210 					dev_replace->srcdev->commit_total_bytes;
211 				dev_replace->tgtdev->bytes_used =
212 					dev_replace->srcdev->bytes_used;
213 				dev_replace->tgtdev->commit_bytes_used =
214 					dev_replace->srcdev->commit_bytes_used;
215 			}
216 			set_bit(BTRFS_DEV_STATE_REPLACE_TGT,
217 				&dev_replace->tgtdev->dev_state);
218 
219 			WARN_ON(fs_info->fs_devices->rw_devices == 0);
220 			dev_replace->tgtdev->io_width = fs_info->sectorsize;
221 			dev_replace->tgtdev->io_align = fs_info->sectorsize;
222 			dev_replace->tgtdev->sector_size = fs_info->sectorsize;
223 			dev_replace->tgtdev->fs_info = fs_info;
224 			set_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
225 				&dev_replace->tgtdev->dev_state);
226 		}
227 		break;
228 	}
229 
230 out:
231 	btrfs_free_path(path);
232 	return ret;
233 }
234 
235 /*
236  * Initialize a new device for device replace target from a given source dev
237  * and path.
238  *
239  * Return 0 and new device in @device_out, otherwise return < 0
240  */
241 static int btrfs_init_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
242 				  const char *device_path,
243 				  struct btrfs_device *srcdev,
244 				  struct btrfs_device **device_out)
245 {
246 	struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
247 	struct btrfs_device *device;
248 	struct block_device *bdev;
249 	struct rcu_string *name;
250 	u64 devid = BTRFS_DEV_REPLACE_DEVID;
251 	int ret = 0;
252 
253 	*device_out = NULL;
254 	if (srcdev->fs_devices->seeding) {
255 		btrfs_err(fs_info, "the filesystem is a seed filesystem!");
256 		return -EINVAL;
257 	}
258 
259 	bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
260 				  fs_info->bdev_holder);
261 	if (IS_ERR(bdev)) {
262 		btrfs_err(fs_info, "target device %s is invalid!", device_path);
263 		return PTR_ERR(bdev);
264 	}
265 
266 	if (!btrfs_check_device_zone_type(fs_info, bdev)) {
267 		btrfs_err(fs_info,
268 		"dev-replace: zoned type of target device mismatch with filesystem");
269 		ret = -EINVAL;
270 		goto error;
271 	}
272 
273 	sync_blockdev(bdev);
274 
275 	list_for_each_entry(device, &fs_devices->devices, dev_list) {
276 		if (device->bdev == bdev) {
277 			btrfs_err(fs_info,
278 				  "target device is in the filesystem!");
279 			ret = -EEXIST;
280 			goto error;
281 		}
282 	}
283 
284 
285 	if (bdev_nr_bytes(bdev) < btrfs_device_get_total_bytes(srcdev)) {
286 		btrfs_err(fs_info,
287 			  "target device is smaller than source device!");
288 		ret = -EINVAL;
289 		goto error;
290 	}
291 
292 
293 	device = btrfs_alloc_device(NULL, &devid, NULL);
294 	if (IS_ERR(device)) {
295 		ret = PTR_ERR(device);
296 		goto error;
297 	}
298 
299 	name = rcu_string_strdup(device_path, GFP_KERNEL);
300 	if (!name) {
301 		btrfs_free_device(device);
302 		ret = -ENOMEM;
303 		goto error;
304 	}
305 	rcu_assign_pointer(device->name, name);
306 
307 	set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
308 	device->generation = 0;
309 	device->io_width = fs_info->sectorsize;
310 	device->io_align = fs_info->sectorsize;
311 	device->sector_size = fs_info->sectorsize;
312 	device->total_bytes = btrfs_device_get_total_bytes(srcdev);
313 	device->disk_total_bytes = btrfs_device_get_disk_total_bytes(srcdev);
314 	device->bytes_used = btrfs_device_get_bytes_used(srcdev);
315 	device->commit_total_bytes = srcdev->commit_total_bytes;
316 	device->commit_bytes_used = device->bytes_used;
317 	device->fs_info = fs_info;
318 	device->bdev = bdev;
319 	set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
320 	set_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
321 	device->mode = FMODE_EXCL;
322 	device->dev_stats_valid = 1;
323 	set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE);
324 	device->fs_devices = fs_devices;
325 
326 	ret = btrfs_get_dev_zone_info(device, false);
327 	if (ret)
328 		goto error;
329 
330 	mutex_lock(&fs_devices->device_list_mutex);
331 	list_add(&device->dev_list, &fs_devices->devices);
332 	fs_devices->num_devices++;
333 	fs_devices->open_devices++;
334 	mutex_unlock(&fs_devices->device_list_mutex);
335 
336 	*device_out = device;
337 	return 0;
338 
339 error:
340 	blkdev_put(bdev, FMODE_EXCL);
341 	return ret;
342 }
343 
344 /*
345  * called from commit_transaction. Writes changed device replace state to
346  * disk.
347  */
348 int btrfs_run_dev_replace(struct btrfs_trans_handle *trans)
349 {
350 	struct btrfs_fs_info *fs_info = trans->fs_info;
351 	int ret;
352 	struct btrfs_root *dev_root = fs_info->dev_root;
353 	struct btrfs_path *path;
354 	struct btrfs_key key;
355 	struct extent_buffer *eb;
356 	struct btrfs_dev_replace_item *ptr;
357 	struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
358 
359 	down_read(&dev_replace->rwsem);
360 	if (!dev_replace->is_valid ||
361 	    !dev_replace->item_needs_writeback) {
362 		up_read(&dev_replace->rwsem);
363 		return 0;
364 	}
365 	up_read(&dev_replace->rwsem);
366 
367 	key.objectid = 0;
368 	key.type = BTRFS_DEV_REPLACE_KEY;
369 	key.offset = 0;
370 
371 	path = btrfs_alloc_path();
372 	if (!path) {
373 		ret = -ENOMEM;
374 		goto out;
375 	}
376 	ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
377 	if (ret < 0) {
378 		btrfs_warn(fs_info,
379 			   "error %d while searching for dev_replace item!",
380 			   ret);
381 		goto out;
382 	}
383 
384 	if (ret == 0 &&
385 	    btrfs_item_size(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
386 		/*
387 		 * need to delete old one and insert a new one.
388 		 * Since no attempt is made to recover any old state, if the
389 		 * dev_replace state is 'running', the data on the target
390 		 * drive is lost.
391 		 * It would be possible to recover the state: just make sure
392 		 * that the beginning of the item is never changed and always
393 		 * contains all the essential information. Then read this
394 		 * minimal set of information and use it as a base for the
395 		 * new state.
396 		 */
397 		ret = btrfs_del_item(trans, dev_root, path);
398 		if (ret != 0) {
399 			btrfs_warn(fs_info,
400 				   "delete too small dev_replace item failed %d!",
401 				   ret);
402 			goto out;
403 		}
404 		ret = 1;
405 	}
406 
407 	if (ret == 1) {
408 		/* need to insert a new item */
409 		btrfs_release_path(path);
410 		ret = btrfs_insert_empty_item(trans, dev_root, path,
411 					      &key, sizeof(*ptr));
412 		if (ret < 0) {
413 			btrfs_warn(fs_info,
414 				   "insert dev_replace item failed %d!", ret);
415 			goto out;
416 		}
417 	}
418 
419 	eb = path->nodes[0];
420 	ptr = btrfs_item_ptr(eb, path->slots[0],
421 			     struct btrfs_dev_replace_item);
422 
423 	down_write(&dev_replace->rwsem);
424 	if (dev_replace->srcdev)
425 		btrfs_set_dev_replace_src_devid(eb, ptr,
426 			dev_replace->srcdev->devid);
427 	else
428 		btrfs_set_dev_replace_src_devid(eb, ptr, (u64)-1);
429 	btrfs_set_dev_replace_cont_reading_from_srcdev_mode(eb, ptr,
430 		dev_replace->cont_reading_from_srcdev_mode);
431 	btrfs_set_dev_replace_replace_state(eb, ptr,
432 		dev_replace->replace_state);
433 	btrfs_set_dev_replace_time_started(eb, ptr, dev_replace->time_started);
434 	btrfs_set_dev_replace_time_stopped(eb, ptr, dev_replace->time_stopped);
435 	btrfs_set_dev_replace_num_write_errors(eb, ptr,
436 		atomic64_read(&dev_replace->num_write_errors));
437 	btrfs_set_dev_replace_num_uncorrectable_read_errors(eb, ptr,
438 		atomic64_read(&dev_replace->num_uncorrectable_read_errors));
439 	dev_replace->cursor_left_last_write_of_item =
440 		dev_replace->cursor_left;
441 	btrfs_set_dev_replace_cursor_left(eb, ptr,
442 		dev_replace->cursor_left_last_write_of_item);
443 	btrfs_set_dev_replace_cursor_right(eb, ptr,
444 		dev_replace->cursor_right);
445 	dev_replace->item_needs_writeback = 0;
446 	up_write(&dev_replace->rwsem);
447 
448 	btrfs_mark_buffer_dirty(eb);
449 
450 out:
451 	btrfs_free_path(path);
452 
453 	return ret;
454 }
455 
456 static char* btrfs_dev_name(struct btrfs_device *device)
457 {
458 	if (!device || test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
459 		return "<missing disk>";
460 	else
461 		return rcu_str_deref(device->name);
462 }
463 
464 static int mark_block_group_to_copy(struct btrfs_fs_info *fs_info,
465 				    struct btrfs_device *src_dev)
466 {
467 	struct btrfs_path *path;
468 	struct btrfs_key key;
469 	struct btrfs_key found_key;
470 	struct btrfs_root *root = fs_info->dev_root;
471 	struct btrfs_dev_extent *dev_extent = NULL;
472 	struct btrfs_block_group *cache;
473 	struct btrfs_trans_handle *trans;
474 	int ret = 0;
475 	u64 chunk_offset;
476 
477 	/* Do not use "to_copy" on non zoned filesystem for now */
478 	if (!btrfs_is_zoned(fs_info))
479 		return 0;
480 
481 	mutex_lock(&fs_info->chunk_mutex);
482 
483 	/* Ensure we don't have pending new block group */
484 	spin_lock(&fs_info->trans_lock);
485 	while (fs_info->running_transaction &&
486 	       !list_empty(&fs_info->running_transaction->dev_update_list)) {
487 		spin_unlock(&fs_info->trans_lock);
488 		mutex_unlock(&fs_info->chunk_mutex);
489 		trans = btrfs_attach_transaction(root);
490 		if (IS_ERR(trans)) {
491 			ret = PTR_ERR(trans);
492 			mutex_lock(&fs_info->chunk_mutex);
493 			if (ret == -ENOENT) {
494 				spin_lock(&fs_info->trans_lock);
495 				continue;
496 			} else {
497 				goto unlock;
498 			}
499 		}
500 
501 		ret = btrfs_commit_transaction(trans);
502 		mutex_lock(&fs_info->chunk_mutex);
503 		if (ret)
504 			goto unlock;
505 
506 		spin_lock(&fs_info->trans_lock);
507 	}
508 	spin_unlock(&fs_info->trans_lock);
509 
510 	path = btrfs_alloc_path();
511 	if (!path) {
512 		ret = -ENOMEM;
513 		goto unlock;
514 	}
515 
516 	path->reada = READA_FORWARD;
517 	path->search_commit_root = 1;
518 	path->skip_locking = 1;
519 
520 	key.objectid = src_dev->devid;
521 	key.type = BTRFS_DEV_EXTENT_KEY;
522 	key.offset = 0;
523 
524 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
525 	if (ret < 0)
526 		goto free_path;
527 	if (ret > 0) {
528 		if (path->slots[0] >=
529 		    btrfs_header_nritems(path->nodes[0])) {
530 			ret = btrfs_next_leaf(root, path);
531 			if (ret < 0)
532 				goto free_path;
533 			if (ret > 0) {
534 				ret = 0;
535 				goto free_path;
536 			}
537 		} else {
538 			ret = 0;
539 		}
540 	}
541 
542 	while (1) {
543 		struct extent_buffer *leaf = path->nodes[0];
544 		int slot = path->slots[0];
545 
546 		btrfs_item_key_to_cpu(leaf, &found_key, slot);
547 
548 		if (found_key.objectid != src_dev->devid)
549 			break;
550 
551 		if (found_key.type != BTRFS_DEV_EXTENT_KEY)
552 			break;
553 
554 		if (found_key.offset < key.offset)
555 			break;
556 
557 		dev_extent = btrfs_item_ptr(leaf, slot, struct btrfs_dev_extent);
558 
559 		chunk_offset = btrfs_dev_extent_chunk_offset(leaf, dev_extent);
560 
561 		cache = btrfs_lookup_block_group(fs_info, chunk_offset);
562 		if (!cache)
563 			goto skip;
564 
565 		spin_lock(&cache->lock);
566 		cache->to_copy = 1;
567 		spin_unlock(&cache->lock);
568 
569 		btrfs_put_block_group(cache);
570 
571 skip:
572 		ret = btrfs_next_item(root, path);
573 		if (ret != 0) {
574 			if (ret > 0)
575 				ret = 0;
576 			break;
577 		}
578 	}
579 
580 free_path:
581 	btrfs_free_path(path);
582 unlock:
583 	mutex_unlock(&fs_info->chunk_mutex);
584 
585 	return ret;
586 }
587 
588 bool btrfs_finish_block_group_to_copy(struct btrfs_device *srcdev,
589 				      struct btrfs_block_group *cache,
590 				      u64 physical)
591 {
592 	struct btrfs_fs_info *fs_info = cache->fs_info;
593 	struct extent_map *em;
594 	struct map_lookup *map;
595 	u64 chunk_offset = cache->start;
596 	int num_extents, cur_extent;
597 	int i;
598 
599 	/* Do not use "to_copy" on non zoned filesystem for now */
600 	if (!btrfs_is_zoned(fs_info))
601 		return true;
602 
603 	spin_lock(&cache->lock);
604 	if (cache->removed) {
605 		spin_unlock(&cache->lock);
606 		return true;
607 	}
608 	spin_unlock(&cache->lock);
609 
610 	em = btrfs_get_chunk_map(fs_info, chunk_offset, 1);
611 	ASSERT(!IS_ERR(em));
612 	map = em->map_lookup;
613 
614 	num_extents = cur_extent = 0;
615 	for (i = 0; i < map->num_stripes; i++) {
616 		/* We have more device extent to copy */
617 		if (srcdev != map->stripes[i].dev)
618 			continue;
619 
620 		num_extents++;
621 		if (physical == map->stripes[i].physical)
622 			cur_extent = i;
623 	}
624 
625 	free_extent_map(em);
626 
627 	if (num_extents > 1 && cur_extent < num_extents - 1) {
628 		/*
629 		 * Has more stripes on this device. Keep this block group
630 		 * readonly until we finish all the stripes.
631 		 */
632 		return false;
633 	}
634 
635 	/* Last stripe on this device */
636 	spin_lock(&cache->lock);
637 	cache->to_copy = 0;
638 	spin_unlock(&cache->lock);
639 
640 	return true;
641 }
642 
643 static int btrfs_dev_replace_start(struct btrfs_fs_info *fs_info,
644 		const char *tgtdev_name, u64 srcdevid, const char *srcdev_name,
645 		int read_src)
646 {
647 	struct btrfs_root *root = fs_info->dev_root;
648 	struct btrfs_trans_handle *trans;
649 	struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
650 	int ret;
651 	struct btrfs_device *tgt_device = NULL;
652 	struct btrfs_device *src_device = NULL;
653 
654 	src_device = btrfs_find_device_by_devspec(fs_info, srcdevid,
655 						  srcdev_name);
656 	if (IS_ERR(src_device))
657 		return PTR_ERR(src_device);
658 
659 	if (btrfs_pinned_by_swapfile(fs_info, src_device)) {
660 		btrfs_warn_in_rcu(fs_info,
661 	  "cannot replace device %s (devid %llu) due to active swapfile",
662 			btrfs_dev_name(src_device), src_device->devid);
663 		return -ETXTBSY;
664 	}
665 
666 	/*
667 	 * Here we commit the transaction to make sure commit_total_bytes
668 	 * of all the devices are updated.
669 	 */
670 	trans = btrfs_attach_transaction(root);
671 	if (!IS_ERR(trans)) {
672 		ret = btrfs_commit_transaction(trans);
673 		if (ret)
674 			return ret;
675 	} else if (PTR_ERR(trans) != -ENOENT) {
676 		return PTR_ERR(trans);
677 	}
678 
679 	ret = btrfs_init_dev_replace_tgtdev(fs_info, tgtdev_name,
680 					    src_device, &tgt_device);
681 	if (ret)
682 		return ret;
683 
684 	ret = mark_block_group_to_copy(fs_info, src_device);
685 	if (ret)
686 		return ret;
687 
688 	down_write(&dev_replace->rwsem);
689 	switch (dev_replace->replace_state) {
690 	case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
691 	case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
692 	case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
693 		break;
694 	case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
695 	case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
696 		ASSERT(0);
697 		ret = BTRFS_IOCTL_DEV_REPLACE_RESULT_ALREADY_STARTED;
698 		up_write(&dev_replace->rwsem);
699 		goto leave;
700 	}
701 
702 	dev_replace->cont_reading_from_srcdev_mode = read_src;
703 	dev_replace->srcdev = src_device;
704 	dev_replace->tgtdev = tgt_device;
705 
706 	btrfs_info_in_rcu(fs_info,
707 		      "dev_replace from %s (devid %llu) to %s started",
708 		      btrfs_dev_name(src_device),
709 		      src_device->devid,
710 		      rcu_str_deref(tgt_device->name));
711 
712 	/*
713 	 * from now on, the writes to the srcdev are all duplicated to
714 	 * go to the tgtdev as well (refer to btrfs_map_block()).
715 	 */
716 	dev_replace->replace_state = BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED;
717 	dev_replace->time_started = ktime_get_real_seconds();
718 	dev_replace->cursor_left = 0;
719 	dev_replace->committed_cursor_left = 0;
720 	dev_replace->cursor_left_last_write_of_item = 0;
721 	dev_replace->cursor_right = 0;
722 	dev_replace->is_valid = 1;
723 	dev_replace->item_needs_writeback = 1;
724 	atomic64_set(&dev_replace->num_write_errors, 0);
725 	atomic64_set(&dev_replace->num_uncorrectable_read_errors, 0);
726 	up_write(&dev_replace->rwsem);
727 
728 	ret = btrfs_sysfs_add_device(tgt_device);
729 	if (ret)
730 		btrfs_err(fs_info, "kobj add dev failed %d", ret);
731 
732 	btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1);
733 
734 	/* Commit dev_replace state and reserve 1 item for it. */
735 	trans = btrfs_start_transaction(root, 1);
736 	if (IS_ERR(trans)) {
737 		ret = PTR_ERR(trans);
738 		down_write(&dev_replace->rwsem);
739 		dev_replace->replace_state =
740 			BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED;
741 		dev_replace->srcdev = NULL;
742 		dev_replace->tgtdev = NULL;
743 		up_write(&dev_replace->rwsem);
744 		goto leave;
745 	}
746 
747 	ret = btrfs_commit_transaction(trans);
748 	WARN_ON(ret);
749 
750 	/* the disk copy procedure reuses the scrub code */
751 	ret = btrfs_scrub_dev(fs_info, src_device->devid, 0,
752 			      btrfs_device_get_total_bytes(src_device),
753 			      &dev_replace->scrub_progress, 0, 1);
754 
755 	ret = btrfs_dev_replace_finishing(fs_info, ret);
756 	if (ret == -EINPROGRESS)
757 		ret = BTRFS_IOCTL_DEV_REPLACE_RESULT_SCRUB_INPROGRESS;
758 
759 	return ret;
760 
761 leave:
762 	btrfs_destroy_dev_replace_tgtdev(tgt_device);
763 	return ret;
764 }
765 
766 int btrfs_dev_replace_by_ioctl(struct btrfs_fs_info *fs_info,
767 			    struct btrfs_ioctl_dev_replace_args *args)
768 {
769 	int ret;
770 
771 	switch (args->start.cont_reading_from_srcdev_mode) {
772 	case BTRFS_IOCTL_DEV_REPLACE_CONT_READING_FROM_SRCDEV_MODE_ALWAYS:
773 	case BTRFS_IOCTL_DEV_REPLACE_CONT_READING_FROM_SRCDEV_MODE_AVOID:
774 		break;
775 	default:
776 		return -EINVAL;
777 	}
778 
779 	if ((args->start.srcdevid == 0 && args->start.srcdev_name[0] == '\0') ||
780 	    args->start.tgtdev_name[0] == '\0')
781 		return -EINVAL;
782 
783 	ret = btrfs_dev_replace_start(fs_info, args->start.tgtdev_name,
784 					args->start.srcdevid,
785 					args->start.srcdev_name,
786 					args->start.cont_reading_from_srcdev_mode);
787 	args->result = ret;
788 	/* don't warn if EINPROGRESS, someone else might be running scrub */
789 	if (ret == BTRFS_IOCTL_DEV_REPLACE_RESULT_SCRUB_INPROGRESS ||
790 	    ret == BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR)
791 		return 0;
792 
793 	return ret;
794 }
795 
796 /*
797  * blocked until all in-flight bios operations are finished.
798  */
799 static void btrfs_rm_dev_replace_blocked(struct btrfs_fs_info *fs_info)
800 {
801 	set_bit(BTRFS_FS_STATE_DEV_REPLACING, &fs_info->fs_state);
802 	wait_event(fs_info->dev_replace.replace_wait, !percpu_counter_sum(
803 		   &fs_info->dev_replace.bio_counter));
804 }
805 
806 /*
807  * we have removed target device, it is safe to allow new bios request.
808  */
809 static void btrfs_rm_dev_replace_unblocked(struct btrfs_fs_info *fs_info)
810 {
811 	clear_bit(BTRFS_FS_STATE_DEV_REPLACING, &fs_info->fs_state);
812 	wake_up(&fs_info->dev_replace.replace_wait);
813 }
814 
815 /*
816  * When finishing the device replace, before swapping the source device with the
817  * target device we must update the chunk allocation state in the target device,
818  * as it is empty because replace works by directly copying the chunks and not
819  * through the normal chunk allocation path.
820  */
821 static int btrfs_set_target_alloc_state(struct btrfs_device *srcdev,
822 					struct btrfs_device *tgtdev)
823 {
824 	struct extent_state *cached_state = NULL;
825 	u64 start = 0;
826 	u64 found_start;
827 	u64 found_end;
828 	int ret = 0;
829 
830 	lockdep_assert_held(&srcdev->fs_info->chunk_mutex);
831 
832 	while (!find_first_extent_bit(&srcdev->alloc_state, start,
833 				      &found_start, &found_end,
834 				      CHUNK_ALLOCATED, &cached_state)) {
835 		ret = set_extent_bits(&tgtdev->alloc_state, found_start,
836 				      found_end, CHUNK_ALLOCATED);
837 		if (ret)
838 			break;
839 		start = found_end + 1;
840 	}
841 
842 	free_extent_state(cached_state);
843 	return ret;
844 }
845 
846 static void btrfs_dev_replace_update_device_in_mapping_tree(
847 						struct btrfs_fs_info *fs_info,
848 						struct btrfs_device *srcdev,
849 						struct btrfs_device *tgtdev)
850 {
851 	struct extent_map_tree *em_tree = &fs_info->mapping_tree;
852 	struct extent_map *em;
853 	struct map_lookup *map;
854 	u64 start = 0;
855 	int i;
856 
857 	write_lock(&em_tree->lock);
858 	do {
859 		em = lookup_extent_mapping(em_tree, start, (u64)-1);
860 		if (!em)
861 			break;
862 		map = em->map_lookup;
863 		for (i = 0; i < map->num_stripes; i++)
864 			if (srcdev == map->stripes[i].dev)
865 				map->stripes[i].dev = tgtdev;
866 		start = em->start + em->len;
867 		free_extent_map(em);
868 	} while (start);
869 	write_unlock(&em_tree->lock);
870 }
871 
872 static int btrfs_dev_replace_finishing(struct btrfs_fs_info *fs_info,
873 				       int scrub_ret)
874 {
875 	struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
876 	struct btrfs_device *tgt_device;
877 	struct btrfs_device *src_device;
878 	struct btrfs_root *root = fs_info->tree_root;
879 	u8 uuid_tmp[BTRFS_UUID_SIZE];
880 	struct btrfs_trans_handle *trans;
881 	int ret = 0;
882 
883 	/* don't allow cancel or unmount to disturb the finishing procedure */
884 	mutex_lock(&dev_replace->lock_finishing_cancel_unmount);
885 
886 	down_read(&dev_replace->rwsem);
887 	/* was the operation canceled, or is it finished? */
888 	if (dev_replace->replace_state !=
889 	    BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED) {
890 		up_read(&dev_replace->rwsem);
891 		mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
892 		return 0;
893 	}
894 
895 	tgt_device = dev_replace->tgtdev;
896 	src_device = dev_replace->srcdev;
897 	up_read(&dev_replace->rwsem);
898 
899 	/*
900 	 * flush all outstanding I/O and inode extent mappings before the
901 	 * copy operation is declared as being finished
902 	 */
903 	ret = btrfs_start_delalloc_roots(fs_info, LONG_MAX, false);
904 	if (ret) {
905 		mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
906 		return ret;
907 	}
908 	btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1);
909 
910 	/*
911 	 * We have to use this loop approach because at this point src_device
912 	 * has to be available for transaction commit to complete, yet new
913 	 * chunks shouldn't be allocated on the device.
914 	 */
915 	while (1) {
916 		trans = btrfs_start_transaction(root, 0);
917 		if (IS_ERR(trans)) {
918 			mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
919 			return PTR_ERR(trans);
920 		}
921 		ret = btrfs_commit_transaction(trans);
922 		WARN_ON(ret);
923 
924 		/* Prevent write_all_supers() during the finishing procedure */
925 		mutex_lock(&fs_info->fs_devices->device_list_mutex);
926 		/* Prevent new chunks being allocated on the source device */
927 		mutex_lock(&fs_info->chunk_mutex);
928 
929 		if (!list_empty(&src_device->post_commit_list)) {
930 			mutex_unlock(&fs_info->fs_devices->device_list_mutex);
931 			mutex_unlock(&fs_info->chunk_mutex);
932 		} else {
933 			break;
934 		}
935 	}
936 
937 	down_write(&dev_replace->rwsem);
938 	dev_replace->replace_state =
939 		scrub_ret ? BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED
940 			  : BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED;
941 	dev_replace->tgtdev = NULL;
942 	dev_replace->srcdev = NULL;
943 	dev_replace->time_stopped = ktime_get_real_seconds();
944 	dev_replace->item_needs_writeback = 1;
945 
946 	/*
947 	 * Update allocation state in the new device and replace the old device
948 	 * with the new one in the mapping tree.
949 	 */
950 	if (!scrub_ret) {
951 		scrub_ret = btrfs_set_target_alloc_state(src_device, tgt_device);
952 		if (scrub_ret)
953 			goto error;
954 		btrfs_dev_replace_update_device_in_mapping_tree(fs_info,
955 								src_device,
956 								tgt_device);
957 	} else {
958 		if (scrub_ret != -ECANCELED)
959 			btrfs_err_in_rcu(fs_info,
960 				 "btrfs_scrub_dev(%s, %llu, %s) failed %d",
961 				 btrfs_dev_name(src_device),
962 				 src_device->devid,
963 				 rcu_str_deref(tgt_device->name), scrub_ret);
964 error:
965 		up_write(&dev_replace->rwsem);
966 		mutex_unlock(&fs_info->chunk_mutex);
967 		mutex_unlock(&fs_info->fs_devices->device_list_mutex);
968 		btrfs_rm_dev_replace_blocked(fs_info);
969 		if (tgt_device)
970 			btrfs_destroy_dev_replace_tgtdev(tgt_device);
971 		btrfs_rm_dev_replace_unblocked(fs_info);
972 		mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
973 
974 		return scrub_ret;
975 	}
976 
977 	btrfs_info_in_rcu(fs_info,
978 			  "dev_replace from %s (devid %llu) to %s finished",
979 			  btrfs_dev_name(src_device),
980 			  src_device->devid,
981 			  rcu_str_deref(tgt_device->name));
982 	clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &tgt_device->dev_state);
983 	tgt_device->devid = src_device->devid;
984 	src_device->devid = BTRFS_DEV_REPLACE_DEVID;
985 	memcpy(uuid_tmp, tgt_device->uuid, sizeof(uuid_tmp));
986 	memcpy(tgt_device->uuid, src_device->uuid, sizeof(tgt_device->uuid));
987 	memcpy(src_device->uuid, uuid_tmp, sizeof(src_device->uuid));
988 	btrfs_device_set_total_bytes(tgt_device, src_device->total_bytes);
989 	btrfs_device_set_disk_total_bytes(tgt_device,
990 					  src_device->disk_total_bytes);
991 	btrfs_device_set_bytes_used(tgt_device, src_device->bytes_used);
992 	tgt_device->commit_bytes_used = src_device->bytes_used;
993 
994 	btrfs_assign_next_active_device(src_device, tgt_device);
995 
996 	list_add(&tgt_device->dev_alloc_list, &fs_info->fs_devices->alloc_list);
997 	fs_info->fs_devices->rw_devices++;
998 
999 	up_write(&dev_replace->rwsem);
1000 	btrfs_rm_dev_replace_blocked(fs_info);
1001 
1002 	btrfs_rm_dev_replace_remove_srcdev(src_device);
1003 
1004 	btrfs_rm_dev_replace_unblocked(fs_info);
1005 
1006 	/*
1007 	 * Increment dev_stats_ccnt so that btrfs_run_dev_stats() will
1008 	 * update on-disk dev stats value during commit transaction
1009 	 */
1010 	atomic_inc(&tgt_device->dev_stats_ccnt);
1011 
1012 	/*
1013 	 * this is again a consistent state where no dev_replace procedure
1014 	 * is running, the target device is part of the filesystem, the
1015 	 * source device is not part of the filesystem anymore and its 1st
1016 	 * superblock is scratched out so that it is no longer marked to
1017 	 * belong to this filesystem.
1018 	 */
1019 	mutex_unlock(&fs_info->chunk_mutex);
1020 	mutex_unlock(&fs_info->fs_devices->device_list_mutex);
1021 
1022 	/* replace the sysfs entry */
1023 	btrfs_sysfs_remove_device(src_device);
1024 	btrfs_sysfs_update_devid(tgt_device);
1025 	if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &src_device->dev_state))
1026 		btrfs_scratch_superblocks(fs_info, src_device->bdev,
1027 					  src_device->name->str);
1028 
1029 	/* write back the superblocks */
1030 	trans = btrfs_start_transaction(root, 0);
1031 	if (!IS_ERR(trans))
1032 		btrfs_commit_transaction(trans);
1033 
1034 	mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
1035 
1036 	btrfs_rm_dev_replace_free_srcdev(src_device);
1037 
1038 	return 0;
1039 }
1040 
1041 /*
1042  * Read progress of device replace status according to the state and last
1043  * stored position. The value format is the same as for
1044  * btrfs_dev_replace::progress_1000
1045  */
1046 static u64 btrfs_dev_replace_progress(struct btrfs_fs_info *fs_info)
1047 {
1048 	struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
1049 	u64 ret = 0;
1050 
1051 	switch (dev_replace->replace_state) {
1052 	case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
1053 	case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
1054 		ret = 0;
1055 		break;
1056 	case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
1057 		ret = 1000;
1058 		break;
1059 	case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
1060 	case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
1061 		ret = div64_u64(dev_replace->cursor_left,
1062 				div_u64(btrfs_device_get_total_bytes(
1063 						dev_replace->srcdev), 1000));
1064 		break;
1065 	}
1066 
1067 	return ret;
1068 }
1069 
1070 void btrfs_dev_replace_status(struct btrfs_fs_info *fs_info,
1071 			      struct btrfs_ioctl_dev_replace_args *args)
1072 {
1073 	struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
1074 
1075 	down_read(&dev_replace->rwsem);
1076 	/* even if !dev_replace_is_valid, the values are good enough for
1077 	 * the replace_status ioctl */
1078 	args->result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR;
1079 	args->status.replace_state = dev_replace->replace_state;
1080 	args->status.time_started = dev_replace->time_started;
1081 	args->status.time_stopped = dev_replace->time_stopped;
1082 	args->status.num_write_errors =
1083 		atomic64_read(&dev_replace->num_write_errors);
1084 	args->status.num_uncorrectable_read_errors =
1085 		atomic64_read(&dev_replace->num_uncorrectable_read_errors);
1086 	args->status.progress_1000 = btrfs_dev_replace_progress(fs_info);
1087 	up_read(&dev_replace->rwsem);
1088 }
1089 
1090 int btrfs_dev_replace_cancel(struct btrfs_fs_info *fs_info)
1091 {
1092 	struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
1093 	struct btrfs_device *tgt_device = NULL;
1094 	struct btrfs_device *src_device = NULL;
1095 	struct btrfs_trans_handle *trans;
1096 	struct btrfs_root *root = fs_info->tree_root;
1097 	int result;
1098 	int ret;
1099 
1100 	if (sb_rdonly(fs_info->sb))
1101 		return -EROFS;
1102 
1103 	mutex_lock(&dev_replace->lock_finishing_cancel_unmount);
1104 	down_write(&dev_replace->rwsem);
1105 	switch (dev_replace->replace_state) {
1106 	case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
1107 	case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
1108 	case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
1109 		result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NOT_STARTED;
1110 		up_write(&dev_replace->rwsem);
1111 		break;
1112 	case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
1113 		tgt_device = dev_replace->tgtdev;
1114 		src_device = dev_replace->srcdev;
1115 		up_write(&dev_replace->rwsem);
1116 		ret = btrfs_scrub_cancel(fs_info);
1117 		if (ret < 0) {
1118 			result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NOT_STARTED;
1119 		} else {
1120 			result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR;
1121 			/*
1122 			 * btrfs_dev_replace_finishing() will handle the
1123 			 * cleanup part
1124 			 */
1125 			btrfs_info_in_rcu(fs_info,
1126 				"dev_replace from %s (devid %llu) to %s canceled",
1127 				btrfs_dev_name(src_device), src_device->devid,
1128 				btrfs_dev_name(tgt_device));
1129 		}
1130 		break;
1131 	case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
1132 		/*
1133 		 * Scrub doing the replace isn't running so we need to do the
1134 		 * cleanup step of btrfs_dev_replace_finishing() here
1135 		 */
1136 		result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR;
1137 		tgt_device = dev_replace->tgtdev;
1138 		src_device = dev_replace->srcdev;
1139 		dev_replace->tgtdev = NULL;
1140 		dev_replace->srcdev = NULL;
1141 		dev_replace->replace_state =
1142 				BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED;
1143 		dev_replace->time_stopped = ktime_get_real_seconds();
1144 		dev_replace->item_needs_writeback = 1;
1145 
1146 		up_write(&dev_replace->rwsem);
1147 
1148 		/* Scrub for replace must not be running in suspended state */
1149 		ret = btrfs_scrub_cancel(fs_info);
1150 		ASSERT(ret != -ENOTCONN);
1151 
1152 		trans = btrfs_start_transaction(root, 0);
1153 		if (IS_ERR(trans)) {
1154 			mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
1155 			return PTR_ERR(trans);
1156 		}
1157 		ret = btrfs_commit_transaction(trans);
1158 		WARN_ON(ret);
1159 
1160 		btrfs_info_in_rcu(fs_info,
1161 		"suspended dev_replace from %s (devid %llu) to %s canceled",
1162 			btrfs_dev_name(src_device), src_device->devid,
1163 			btrfs_dev_name(tgt_device));
1164 
1165 		if (tgt_device)
1166 			btrfs_destroy_dev_replace_tgtdev(tgt_device);
1167 		break;
1168 	default:
1169 		up_write(&dev_replace->rwsem);
1170 		result = -EINVAL;
1171 	}
1172 
1173 	mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
1174 	return result;
1175 }
1176 
1177 void btrfs_dev_replace_suspend_for_unmount(struct btrfs_fs_info *fs_info)
1178 {
1179 	struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
1180 
1181 	mutex_lock(&dev_replace->lock_finishing_cancel_unmount);
1182 	down_write(&dev_replace->rwsem);
1183 
1184 	switch (dev_replace->replace_state) {
1185 	case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
1186 	case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
1187 	case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
1188 	case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
1189 		break;
1190 	case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
1191 		dev_replace->replace_state =
1192 			BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED;
1193 		dev_replace->time_stopped = ktime_get_real_seconds();
1194 		dev_replace->item_needs_writeback = 1;
1195 		btrfs_info(fs_info, "suspending dev_replace for unmount");
1196 		break;
1197 	}
1198 
1199 	up_write(&dev_replace->rwsem);
1200 	mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
1201 }
1202 
1203 /* resume dev_replace procedure that was interrupted by unmount */
1204 int btrfs_resume_dev_replace_async(struct btrfs_fs_info *fs_info)
1205 {
1206 	struct task_struct *task;
1207 	struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
1208 
1209 	down_write(&dev_replace->rwsem);
1210 
1211 	switch (dev_replace->replace_state) {
1212 	case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
1213 	case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
1214 	case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
1215 		up_write(&dev_replace->rwsem);
1216 		return 0;
1217 	case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
1218 		break;
1219 	case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
1220 		dev_replace->replace_state =
1221 			BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED;
1222 		break;
1223 	}
1224 	if (!dev_replace->tgtdev || !dev_replace->tgtdev->bdev) {
1225 		btrfs_info(fs_info,
1226 			   "cannot continue dev_replace, tgtdev is missing");
1227 		btrfs_info(fs_info,
1228 			   "you may cancel the operation after 'mount -o degraded'");
1229 		dev_replace->replace_state =
1230 					BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED;
1231 		up_write(&dev_replace->rwsem);
1232 		return 0;
1233 	}
1234 	up_write(&dev_replace->rwsem);
1235 
1236 	/*
1237 	 * This could collide with a paused balance, but the exclusive op logic
1238 	 * should never allow both to start and pause. We don't want to allow
1239 	 * dev-replace to start anyway.
1240 	 */
1241 	if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_DEV_REPLACE)) {
1242 		down_write(&dev_replace->rwsem);
1243 		dev_replace->replace_state =
1244 					BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED;
1245 		up_write(&dev_replace->rwsem);
1246 		btrfs_info(fs_info,
1247 		"cannot resume dev-replace, other exclusive operation running");
1248 		return 0;
1249 	}
1250 
1251 	task = kthread_run(btrfs_dev_replace_kthread, fs_info, "btrfs-devrepl");
1252 	return PTR_ERR_OR_ZERO(task);
1253 }
1254 
1255 static int btrfs_dev_replace_kthread(void *data)
1256 {
1257 	struct btrfs_fs_info *fs_info = data;
1258 	struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
1259 	u64 progress;
1260 	int ret;
1261 
1262 	progress = btrfs_dev_replace_progress(fs_info);
1263 	progress = div_u64(progress, 10);
1264 	btrfs_info_in_rcu(fs_info,
1265 		"continuing dev_replace from %s (devid %llu) to target %s @%u%%",
1266 		btrfs_dev_name(dev_replace->srcdev),
1267 		dev_replace->srcdev->devid,
1268 		btrfs_dev_name(dev_replace->tgtdev),
1269 		(unsigned int)progress);
1270 
1271 	ret = btrfs_scrub_dev(fs_info, dev_replace->srcdev->devid,
1272 			      dev_replace->committed_cursor_left,
1273 			      btrfs_device_get_total_bytes(dev_replace->srcdev),
1274 			      &dev_replace->scrub_progress, 0, 1);
1275 	ret = btrfs_dev_replace_finishing(fs_info, ret);
1276 	WARN_ON(ret && ret != -ECANCELED);
1277 
1278 	btrfs_exclop_finish(fs_info);
1279 	return 0;
1280 }
1281 
1282 int __pure btrfs_dev_replace_is_ongoing(struct btrfs_dev_replace *dev_replace)
1283 {
1284 	if (!dev_replace->is_valid)
1285 		return 0;
1286 
1287 	switch (dev_replace->replace_state) {
1288 	case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
1289 	case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
1290 	case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
1291 		return 0;
1292 	case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
1293 	case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
1294 		/*
1295 		 * return true even if tgtdev is missing (this is
1296 		 * something that can happen if the dev_replace
1297 		 * procedure is suspended by an umount and then
1298 		 * the tgtdev is missing (or "btrfs dev scan") was
1299 		 * not called and the filesystem is remounted
1300 		 * in degraded state. This does not stop the
1301 		 * dev_replace procedure. It needs to be canceled
1302 		 * manually if the cancellation is wanted.
1303 		 */
1304 		break;
1305 	}
1306 	return 1;
1307 }
1308 
1309 void btrfs_bio_counter_inc_noblocked(struct btrfs_fs_info *fs_info)
1310 {
1311 	percpu_counter_inc(&fs_info->dev_replace.bio_counter);
1312 }
1313 
1314 void btrfs_bio_counter_sub(struct btrfs_fs_info *fs_info, s64 amount)
1315 {
1316 	percpu_counter_sub(&fs_info->dev_replace.bio_counter, amount);
1317 	cond_wake_up_nomb(&fs_info->dev_replace.replace_wait);
1318 }
1319 
1320 void btrfs_bio_counter_inc_blocked(struct btrfs_fs_info *fs_info)
1321 {
1322 	while (1) {
1323 		percpu_counter_inc(&fs_info->dev_replace.bio_counter);
1324 		if (likely(!test_bit(BTRFS_FS_STATE_DEV_REPLACING,
1325 				     &fs_info->fs_state)))
1326 			break;
1327 
1328 		btrfs_bio_counter_dec(fs_info);
1329 		wait_event(fs_info->dev_replace.replace_wait,
1330 			   !test_bit(BTRFS_FS_STATE_DEV_REPLACING,
1331 				     &fs_info->fs_state));
1332 	}
1333 }
1334