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