xref: /openbmc/linux/fs/btrfs/dev-replace.c (revision 401e29c1)
1 /*
2  * Copyright (C) STRATO AG 2012.  All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public
6  * License v2 as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public
14  * License along with this program; if not, write to the
15  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16  * Boston, MA 021110-1307, USA.
17  */
18 #include <linux/sched.h>
19 #include <linux/bio.h>
20 #include <linux/slab.h>
21 #include <linux/buffer_head.h>
22 #include <linux/blkdev.h>
23 #include <linux/random.h>
24 #include <linux/iocontext.h>
25 #include <linux/capability.h>
26 #include <linux/kthread.h>
27 #include <linux/math64.h>
28 #include <asm/div64.h>
29 #include "ctree.h"
30 #include "extent_map.h"
31 #include "disk-io.h"
32 #include "transaction.h"
33 #include "print-tree.h"
34 #include "volumes.h"
35 #include "async-thread.h"
36 #include "check-integrity.h"
37 #include "rcu-string.h"
38 #include "dev-replace.h"
39 #include "sysfs.h"
40 
41 static int btrfs_dev_replace_finishing(struct btrfs_fs_info *fs_info,
42 				       int scrub_ret);
43 static void btrfs_dev_replace_update_device_in_mapping_tree(
44 						struct btrfs_fs_info *fs_info,
45 						struct btrfs_device *srcdev,
46 						struct btrfs_device *tgtdev);
47 static u64 __btrfs_dev_replace_cancel(struct btrfs_fs_info *fs_info);
48 static int btrfs_dev_replace_kthread(void *data);
49 static int btrfs_dev_replace_continue_on_mount(struct btrfs_fs_info *fs_info);
50 
51 
52 int btrfs_init_dev_replace(struct btrfs_fs_info *fs_info)
53 {
54 	struct btrfs_key key;
55 	struct btrfs_root *dev_root = fs_info->dev_root;
56 	struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
57 	struct extent_buffer *eb;
58 	int slot;
59 	int ret = 0;
60 	struct btrfs_path *path = NULL;
61 	int item_size;
62 	struct btrfs_dev_replace_item *ptr;
63 	u64 src_devid;
64 
65 	path = btrfs_alloc_path();
66 	if (!path) {
67 		ret = -ENOMEM;
68 		goto out;
69 	}
70 
71 	key.objectid = 0;
72 	key.type = BTRFS_DEV_REPLACE_KEY;
73 	key.offset = 0;
74 	ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
75 	if (ret) {
76 no_valid_dev_replace_entry_found:
77 		ret = 0;
78 		dev_replace->replace_state =
79 			BTRFS_DEV_REPLACE_ITEM_STATE_NEVER_STARTED;
80 		dev_replace->cont_reading_from_srcdev_mode =
81 		    BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_ALWAYS;
82 		dev_replace->replace_state = 0;
83 		dev_replace->time_started = 0;
84 		dev_replace->time_stopped = 0;
85 		atomic64_set(&dev_replace->num_write_errors, 0);
86 		atomic64_set(&dev_replace->num_uncorrectable_read_errors, 0);
87 		dev_replace->cursor_left = 0;
88 		dev_replace->committed_cursor_left = 0;
89 		dev_replace->cursor_left_last_write_of_item = 0;
90 		dev_replace->cursor_right = 0;
91 		dev_replace->srcdev = NULL;
92 		dev_replace->tgtdev = NULL;
93 		dev_replace->is_valid = 0;
94 		dev_replace->item_needs_writeback = 0;
95 		goto out;
96 	}
97 	slot = path->slots[0];
98 	eb = path->nodes[0];
99 	item_size = btrfs_item_size_nr(eb, slot);
100 	ptr = btrfs_item_ptr(eb, slot, struct btrfs_dev_replace_item);
101 
102 	if (item_size != sizeof(struct btrfs_dev_replace_item)) {
103 		btrfs_warn(fs_info,
104 			"dev_replace entry found has unexpected size, ignore entry");
105 		goto no_valid_dev_replace_entry_found;
106 	}
107 
108 	src_devid = btrfs_dev_replace_src_devid(eb, ptr);
109 	dev_replace->cont_reading_from_srcdev_mode =
110 		btrfs_dev_replace_cont_reading_from_srcdev_mode(eb, ptr);
111 	dev_replace->replace_state = btrfs_dev_replace_replace_state(eb, ptr);
112 	dev_replace->time_started = btrfs_dev_replace_time_started(eb, ptr);
113 	dev_replace->time_stopped =
114 		btrfs_dev_replace_time_stopped(eb, ptr);
115 	atomic64_set(&dev_replace->num_write_errors,
116 		     btrfs_dev_replace_num_write_errors(eb, ptr));
117 	atomic64_set(&dev_replace->num_uncorrectable_read_errors,
118 		     btrfs_dev_replace_num_uncorrectable_read_errors(eb, ptr));
119 	dev_replace->cursor_left = btrfs_dev_replace_cursor_left(eb, ptr);
120 	dev_replace->committed_cursor_left = dev_replace->cursor_left;
121 	dev_replace->cursor_left_last_write_of_item = dev_replace->cursor_left;
122 	dev_replace->cursor_right = btrfs_dev_replace_cursor_right(eb, ptr);
123 	dev_replace->is_valid = 1;
124 
125 	dev_replace->item_needs_writeback = 0;
126 	switch (dev_replace->replace_state) {
127 	case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
128 	case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
129 	case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
130 		dev_replace->srcdev = NULL;
131 		dev_replace->tgtdev = NULL;
132 		break;
133 	case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
134 	case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
135 		dev_replace->srcdev = btrfs_find_device(fs_info, src_devid,
136 							NULL, NULL);
137 		dev_replace->tgtdev = btrfs_find_device(fs_info,
138 							BTRFS_DEV_REPLACE_DEVID,
139 							NULL, NULL);
140 		/*
141 		 * allow 'btrfs dev replace_cancel' if src/tgt device is
142 		 * missing
143 		 */
144 		if (!dev_replace->srcdev &&
145 		    !btrfs_test_opt(fs_info, DEGRADED)) {
146 			ret = -EIO;
147 			btrfs_warn(fs_info,
148 			   "cannot mount because device replace operation is ongoing and");
149 			btrfs_warn(fs_info,
150 			   "srcdev (devid %llu) is missing, need to run 'btrfs dev scan'?",
151 			   src_devid);
152 		}
153 		if (!dev_replace->tgtdev &&
154 		    !btrfs_test_opt(fs_info, DEGRADED)) {
155 			ret = -EIO;
156 			btrfs_warn(fs_info,
157 			   "cannot mount because device replace operation is ongoing and");
158 			btrfs_warn(fs_info,
159 			   "tgtdev (devid %llu) is missing, need to run 'btrfs dev scan'?",
160 				BTRFS_DEV_REPLACE_DEVID);
161 		}
162 		if (dev_replace->tgtdev) {
163 			if (dev_replace->srcdev) {
164 				dev_replace->tgtdev->total_bytes =
165 					dev_replace->srcdev->total_bytes;
166 				dev_replace->tgtdev->disk_total_bytes =
167 					dev_replace->srcdev->disk_total_bytes;
168 				dev_replace->tgtdev->commit_total_bytes =
169 					dev_replace->srcdev->commit_total_bytes;
170 				dev_replace->tgtdev->bytes_used =
171 					dev_replace->srcdev->bytes_used;
172 				dev_replace->tgtdev->commit_bytes_used =
173 					dev_replace->srcdev->commit_bytes_used;
174 			}
175 			set_bit(BTRFS_DEV_STATE_REPLACE_TGT,
176 				&dev_replace->tgtdev->dev_state);
177 			btrfs_init_dev_replace_tgtdev_for_resume(fs_info,
178 				dev_replace->tgtdev);
179 		}
180 		break;
181 	}
182 
183 out:
184 	btrfs_free_path(path);
185 	return ret;
186 }
187 
188 /*
189  * called from commit_transaction. Writes changed device replace state to
190  * disk.
191  */
192 int btrfs_run_dev_replace(struct btrfs_trans_handle *trans,
193 			  struct btrfs_fs_info *fs_info)
194 {
195 	int ret;
196 	struct btrfs_root *dev_root = fs_info->dev_root;
197 	struct btrfs_path *path;
198 	struct btrfs_key key;
199 	struct extent_buffer *eb;
200 	struct btrfs_dev_replace_item *ptr;
201 	struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
202 
203 	btrfs_dev_replace_lock(dev_replace, 0);
204 	if (!dev_replace->is_valid ||
205 	    !dev_replace->item_needs_writeback) {
206 		btrfs_dev_replace_unlock(dev_replace, 0);
207 		return 0;
208 	}
209 	btrfs_dev_replace_unlock(dev_replace, 0);
210 
211 	key.objectid = 0;
212 	key.type = BTRFS_DEV_REPLACE_KEY;
213 	key.offset = 0;
214 
215 	path = btrfs_alloc_path();
216 	if (!path) {
217 		ret = -ENOMEM;
218 		goto out;
219 	}
220 	ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
221 	if (ret < 0) {
222 		btrfs_warn(fs_info,
223 			   "error %d while searching for dev_replace item!",
224 			   ret);
225 		goto out;
226 	}
227 
228 	if (ret == 0 &&
229 	    btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
230 		/*
231 		 * need to delete old one and insert a new one.
232 		 * Since no attempt is made to recover any old state, if the
233 		 * dev_replace state is 'running', the data on the target
234 		 * drive is lost.
235 		 * It would be possible to recover the state: just make sure
236 		 * that the beginning of the item is never changed and always
237 		 * contains all the essential information. Then read this
238 		 * minimal set of information and use it as a base for the
239 		 * new state.
240 		 */
241 		ret = btrfs_del_item(trans, dev_root, path);
242 		if (ret != 0) {
243 			btrfs_warn(fs_info,
244 				   "delete too small dev_replace item failed %d!",
245 				   ret);
246 			goto out;
247 		}
248 		ret = 1;
249 	}
250 
251 	if (ret == 1) {
252 		/* need to insert a new item */
253 		btrfs_release_path(path);
254 		ret = btrfs_insert_empty_item(trans, dev_root, path,
255 					      &key, sizeof(*ptr));
256 		if (ret < 0) {
257 			btrfs_warn(fs_info,
258 				   "insert dev_replace item failed %d!", ret);
259 			goto out;
260 		}
261 	}
262 
263 	eb = path->nodes[0];
264 	ptr = btrfs_item_ptr(eb, path->slots[0],
265 			     struct btrfs_dev_replace_item);
266 
267 	btrfs_dev_replace_lock(dev_replace, 1);
268 	if (dev_replace->srcdev)
269 		btrfs_set_dev_replace_src_devid(eb, ptr,
270 			dev_replace->srcdev->devid);
271 	else
272 		btrfs_set_dev_replace_src_devid(eb, ptr, (u64)-1);
273 	btrfs_set_dev_replace_cont_reading_from_srcdev_mode(eb, ptr,
274 		dev_replace->cont_reading_from_srcdev_mode);
275 	btrfs_set_dev_replace_replace_state(eb, ptr,
276 		dev_replace->replace_state);
277 	btrfs_set_dev_replace_time_started(eb, ptr, dev_replace->time_started);
278 	btrfs_set_dev_replace_time_stopped(eb, ptr, dev_replace->time_stopped);
279 	btrfs_set_dev_replace_num_write_errors(eb, ptr,
280 		atomic64_read(&dev_replace->num_write_errors));
281 	btrfs_set_dev_replace_num_uncorrectable_read_errors(eb, ptr,
282 		atomic64_read(&dev_replace->num_uncorrectable_read_errors));
283 	dev_replace->cursor_left_last_write_of_item =
284 		dev_replace->cursor_left;
285 	btrfs_set_dev_replace_cursor_left(eb, ptr,
286 		dev_replace->cursor_left_last_write_of_item);
287 	btrfs_set_dev_replace_cursor_right(eb, ptr,
288 		dev_replace->cursor_right);
289 	dev_replace->item_needs_writeback = 0;
290 	btrfs_dev_replace_unlock(dev_replace, 1);
291 
292 	btrfs_mark_buffer_dirty(eb);
293 
294 out:
295 	btrfs_free_path(path);
296 
297 	return ret;
298 }
299 
300 void btrfs_after_dev_replace_commit(struct btrfs_fs_info *fs_info)
301 {
302 	struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
303 
304 	dev_replace->committed_cursor_left =
305 		dev_replace->cursor_left_last_write_of_item;
306 }
307 
308 static char* btrfs_dev_name(struct btrfs_device *device)
309 {
310 	if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
311 		return "<missing disk>";
312 	else
313 		return rcu_str_deref(device->name);
314 }
315 
316 int btrfs_dev_replace_start(struct btrfs_fs_info *fs_info,
317 		const char *tgtdev_name, u64 srcdevid, const char *srcdev_name,
318 		int read_src)
319 {
320 	struct btrfs_root *root = fs_info->dev_root;
321 	struct btrfs_trans_handle *trans;
322 	struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
323 	int ret;
324 	struct btrfs_device *tgt_device = NULL;
325 	struct btrfs_device *src_device = NULL;
326 
327 	/* the disk copy procedure reuses the scrub code */
328 	mutex_lock(&fs_info->volume_mutex);
329 	ret = btrfs_find_device_by_devspec(fs_info, srcdevid,
330 					    srcdev_name, &src_device);
331 	if (ret) {
332 		mutex_unlock(&fs_info->volume_mutex);
333 		return ret;
334 	}
335 
336 	ret = btrfs_init_dev_replace_tgtdev(fs_info, tgtdev_name,
337 					    src_device, &tgt_device);
338 	mutex_unlock(&fs_info->volume_mutex);
339 	if (ret)
340 		return ret;
341 
342 	/*
343 	 * Here we commit the transaction to make sure commit_total_bytes
344 	 * of all the devices are updated.
345 	 */
346 	trans = btrfs_attach_transaction(root);
347 	if (!IS_ERR(trans)) {
348 		ret = btrfs_commit_transaction(trans);
349 		if (ret)
350 			return ret;
351 	} else if (PTR_ERR(trans) != -ENOENT) {
352 		return PTR_ERR(trans);
353 	}
354 
355 	btrfs_dev_replace_lock(dev_replace, 1);
356 	switch (dev_replace->replace_state) {
357 	case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
358 	case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
359 	case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
360 		break;
361 	case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
362 	case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
363 		ret = BTRFS_IOCTL_DEV_REPLACE_RESULT_ALREADY_STARTED;
364 		goto leave;
365 	}
366 
367 	dev_replace->cont_reading_from_srcdev_mode = read_src;
368 	WARN_ON(!src_device);
369 	dev_replace->srcdev = src_device;
370 	WARN_ON(!tgt_device);
371 	dev_replace->tgtdev = tgt_device;
372 
373 	btrfs_info_in_rcu(fs_info,
374 		      "dev_replace from %s (devid %llu) to %s started",
375 		      btrfs_dev_name(src_device),
376 		      src_device->devid,
377 		      rcu_str_deref(tgt_device->name));
378 
379 	/*
380 	 * from now on, the writes to the srcdev are all duplicated to
381 	 * go to the tgtdev as well (refer to btrfs_map_block()).
382 	 */
383 	dev_replace->replace_state = BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED;
384 	dev_replace->time_started = get_seconds();
385 	dev_replace->cursor_left = 0;
386 	dev_replace->committed_cursor_left = 0;
387 	dev_replace->cursor_left_last_write_of_item = 0;
388 	dev_replace->cursor_right = 0;
389 	dev_replace->is_valid = 1;
390 	dev_replace->item_needs_writeback = 1;
391 	atomic64_set(&dev_replace->num_write_errors, 0);
392 	atomic64_set(&dev_replace->num_uncorrectable_read_errors, 0);
393 	btrfs_dev_replace_unlock(dev_replace, 1);
394 
395 	ret = btrfs_sysfs_add_device_link(tgt_device->fs_devices, tgt_device);
396 	if (ret)
397 		btrfs_err(fs_info, "kobj add dev failed %d", ret);
398 
399 	btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1);
400 
401 	/* force writing the updated state information to disk */
402 	trans = btrfs_start_transaction(root, 0);
403 	if (IS_ERR(trans)) {
404 		ret = PTR_ERR(trans);
405 		btrfs_dev_replace_lock(dev_replace, 1);
406 		goto leave;
407 	}
408 
409 	ret = btrfs_commit_transaction(trans);
410 	WARN_ON(ret);
411 
412 	/* the disk copy procedure reuses the scrub code */
413 	ret = btrfs_scrub_dev(fs_info, src_device->devid, 0,
414 			      btrfs_device_get_total_bytes(src_device),
415 			      &dev_replace->scrub_progress, 0, 1);
416 
417 	ret = btrfs_dev_replace_finishing(fs_info, ret);
418 	if (ret == -EINPROGRESS) {
419 		ret = BTRFS_IOCTL_DEV_REPLACE_RESULT_SCRUB_INPROGRESS;
420 	} else {
421 		WARN_ON(ret);
422 	}
423 
424 	return ret;
425 
426 leave:
427 	dev_replace->srcdev = NULL;
428 	dev_replace->tgtdev = NULL;
429 	btrfs_dev_replace_unlock(dev_replace, 1);
430 	btrfs_destroy_dev_replace_tgtdev(fs_info, tgt_device);
431 	return ret;
432 }
433 
434 int btrfs_dev_replace_by_ioctl(struct btrfs_fs_info *fs_info,
435 			    struct btrfs_ioctl_dev_replace_args *args)
436 {
437 	int ret;
438 
439 	switch (args->start.cont_reading_from_srcdev_mode) {
440 	case BTRFS_IOCTL_DEV_REPLACE_CONT_READING_FROM_SRCDEV_MODE_ALWAYS:
441 	case BTRFS_IOCTL_DEV_REPLACE_CONT_READING_FROM_SRCDEV_MODE_AVOID:
442 		break;
443 	default:
444 		return -EINVAL;
445 	}
446 
447 	if ((args->start.srcdevid == 0 && args->start.srcdev_name[0] == '\0') ||
448 	    args->start.tgtdev_name[0] == '\0')
449 		return -EINVAL;
450 
451 	ret = btrfs_dev_replace_start(fs_info, args->start.tgtdev_name,
452 					args->start.srcdevid,
453 					args->start.srcdev_name,
454 					args->start.cont_reading_from_srcdev_mode);
455 	args->result = ret;
456 	/* don't warn if EINPROGRESS, someone else might be running scrub */
457 	if (ret == BTRFS_IOCTL_DEV_REPLACE_RESULT_SCRUB_INPROGRESS)
458 		ret = 0;
459 
460 	return ret;
461 }
462 
463 /*
464  * blocked until all in-flight bios operations are finished.
465  */
466 static void btrfs_rm_dev_replace_blocked(struct btrfs_fs_info *fs_info)
467 {
468 	set_bit(BTRFS_FS_STATE_DEV_REPLACING, &fs_info->fs_state);
469 	wait_event(fs_info->replace_wait, !percpu_counter_sum(
470 		   &fs_info->bio_counter));
471 }
472 
473 /*
474  * we have removed target device, it is safe to allow new bios request.
475  */
476 static void btrfs_rm_dev_replace_unblocked(struct btrfs_fs_info *fs_info)
477 {
478 	clear_bit(BTRFS_FS_STATE_DEV_REPLACING, &fs_info->fs_state);
479 	wake_up(&fs_info->replace_wait);
480 }
481 
482 static int btrfs_dev_replace_finishing(struct btrfs_fs_info *fs_info,
483 				       int scrub_ret)
484 {
485 	struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
486 	struct btrfs_device *tgt_device;
487 	struct btrfs_device *src_device;
488 	struct btrfs_root *root = fs_info->tree_root;
489 	u8 uuid_tmp[BTRFS_UUID_SIZE];
490 	struct btrfs_trans_handle *trans;
491 	int ret = 0;
492 
493 	/* don't allow cancel or unmount to disturb the finishing procedure */
494 	mutex_lock(&dev_replace->lock_finishing_cancel_unmount);
495 
496 	btrfs_dev_replace_lock(dev_replace, 0);
497 	/* was the operation canceled, or is it finished? */
498 	if (dev_replace->replace_state !=
499 	    BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED) {
500 		btrfs_dev_replace_unlock(dev_replace, 0);
501 		mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
502 		return 0;
503 	}
504 
505 	tgt_device = dev_replace->tgtdev;
506 	src_device = dev_replace->srcdev;
507 	btrfs_dev_replace_unlock(dev_replace, 0);
508 
509 	/*
510 	 * flush all outstanding I/O and inode extent mappings before the
511 	 * copy operation is declared as being finished
512 	 */
513 	ret = btrfs_start_delalloc_roots(fs_info, 0, -1);
514 	if (ret) {
515 		mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
516 		return ret;
517 	}
518 	btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1);
519 
520 	trans = btrfs_start_transaction(root, 0);
521 	if (IS_ERR(trans)) {
522 		mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
523 		return PTR_ERR(trans);
524 	}
525 	ret = btrfs_commit_transaction(trans);
526 	WARN_ON(ret);
527 
528 	mutex_lock(&uuid_mutex);
529 	/* keep away write_all_supers() during the finishing procedure */
530 	mutex_lock(&fs_info->fs_devices->device_list_mutex);
531 	mutex_lock(&fs_info->chunk_mutex);
532 	btrfs_dev_replace_lock(dev_replace, 1);
533 	dev_replace->replace_state =
534 		scrub_ret ? BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED
535 			  : BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED;
536 	dev_replace->tgtdev = NULL;
537 	dev_replace->srcdev = NULL;
538 	dev_replace->time_stopped = get_seconds();
539 	dev_replace->item_needs_writeback = 1;
540 
541 	/* replace old device with new one in mapping tree */
542 	if (!scrub_ret) {
543 		btrfs_dev_replace_update_device_in_mapping_tree(fs_info,
544 								src_device,
545 								tgt_device);
546 	} else {
547 		btrfs_err_in_rcu(fs_info,
548 				 "btrfs_scrub_dev(%s, %llu, %s) failed %d",
549 				 btrfs_dev_name(src_device),
550 				 src_device->devid,
551 				 rcu_str_deref(tgt_device->name), scrub_ret);
552 		btrfs_dev_replace_unlock(dev_replace, 1);
553 		mutex_unlock(&fs_info->chunk_mutex);
554 		mutex_unlock(&fs_info->fs_devices->device_list_mutex);
555 		mutex_unlock(&uuid_mutex);
556 		btrfs_rm_dev_replace_blocked(fs_info);
557 		if (tgt_device)
558 			btrfs_destroy_dev_replace_tgtdev(fs_info, tgt_device);
559 		btrfs_rm_dev_replace_unblocked(fs_info);
560 		mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
561 
562 		return scrub_ret;
563 	}
564 
565 	btrfs_info_in_rcu(fs_info,
566 			  "dev_replace from %s (devid %llu) to %s finished",
567 			  btrfs_dev_name(src_device),
568 			  src_device->devid,
569 			  rcu_str_deref(tgt_device->name));
570 	clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &tgt_device->dev_state);
571 	tgt_device->devid = src_device->devid;
572 	src_device->devid = BTRFS_DEV_REPLACE_DEVID;
573 	memcpy(uuid_tmp, tgt_device->uuid, sizeof(uuid_tmp));
574 	memcpy(tgt_device->uuid, src_device->uuid, sizeof(tgt_device->uuid));
575 	memcpy(src_device->uuid, uuid_tmp, sizeof(src_device->uuid));
576 	btrfs_device_set_total_bytes(tgt_device, src_device->total_bytes);
577 	btrfs_device_set_disk_total_bytes(tgt_device,
578 					  src_device->disk_total_bytes);
579 	btrfs_device_set_bytes_used(tgt_device, src_device->bytes_used);
580 	ASSERT(list_empty(&src_device->resized_list));
581 	tgt_device->commit_total_bytes = src_device->commit_total_bytes;
582 	tgt_device->commit_bytes_used = src_device->bytes_used;
583 
584 	btrfs_assign_next_active_device(fs_info, src_device, tgt_device);
585 
586 	list_add(&tgt_device->dev_alloc_list, &fs_info->fs_devices->alloc_list);
587 	fs_info->fs_devices->rw_devices++;
588 
589 	btrfs_dev_replace_unlock(dev_replace, 1);
590 
591 	btrfs_rm_dev_replace_blocked(fs_info);
592 
593 	btrfs_rm_dev_replace_remove_srcdev(fs_info, src_device);
594 
595 	btrfs_rm_dev_replace_unblocked(fs_info);
596 
597 	/*
598 	 * this is again a consistent state where no dev_replace procedure
599 	 * is running, the target device is part of the filesystem, the
600 	 * source device is not part of the filesystem anymore and its 1st
601 	 * superblock is scratched out so that it is no longer marked to
602 	 * belong to this filesystem.
603 	 */
604 	mutex_unlock(&fs_info->chunk_mutex);
605 	mutex_unlock(&fs_info->fs_devices->device_list_mutex);
606 	mutex_unlock(&uuid_mutex);
607 
608 	/* replace the sysfs entry */
609 	btrfs_sysfs_rm_device_link(fs_info->fs_devices, src_device);
610 	btrfs_rm_dev_replace_free_srcdev(fs_info, src_device);
611 
612 	/* write back the superblocks */
613 	trans = btrfs_start_transaction(root, 0);
614 	if (!IS_ERR(trans))
615 		btrfs_commit_transaction(trans);
616 
617 	mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
618 
619 	return 0;
620 }
621 
622 static void btrfs_dev_replace_update_device_in_mapping_tree(
623 						struct btrfs_fs_info *fs_info,
624 						struct btrfs_device *srcdev,
625 						struct btrfs_device *tgtdev)
626 {
627 	struct extent_map_tree *em_tree = &fs_info->mapping_tree.map_tree;
628 	struct extent_map *em;
629 	struct map_lookup *map;
630 	u64 start = 0;
631 	int i;
632 
633 	write_lock(&em_tree->lock);
634 	do {
635 		em = lookup_extent_mapping(em_tree, start, (u64)-1);
636 		if (!em)
637 			break;
638 		map = em->map_lookup;
639 		for (i = 0; i < map->num_stripes; i++)
640 			if (srcdev == map->stripes[i].dev)
641 				map->stripes[i].dev = tgtdev;
642 		start = em->start + em->len;
643 		free_extent_map(em);
644 	} while (start);
645 	write_unlock(&em_tree->lock);
646 }
647 
648 /*
649  * Read progress of device replace status according to the state and last
650  * stored position. The value format is the same as for
651  * btrfs_dev_replace::progress_1000
652  */
653 static u64 btrfs_dev_replace_progress(struct btrfs_fs_info *fs_info)
654 {
655 	struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
656 	u64 ret = 0;
657 
658 	switch (dev_replace->replace_state) {
659 	case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
660 	case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
661 		ret = 0;
662 		break;
663 	case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
664 		ret = 1000;
665 		break;
666 	case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
667 	case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
668 		ret = div64_u64(dev_replace->cursor_left,
669 				div_u64(btrfs_device_get_total_bytes(
670 						dev_replace->srcdev), 1000));
671 		break;
672 	}
673 
674 	return ret;
675 }
676 
677 void btrfs_dev_replace_status(struct btrfs_fs_info *fs_info,
678 			      struct btrfs_ioctl_dev_replace_args *args)
679 {
680 	struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
681 
682 	btrfs_dev_replace_lock(dev_replace, 0);
683 	/* even if !dev_replace_is_valid, the values are good enough for
684 	 * the replace_status ioctl */
685 	args->result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR;
686 	args->status.replace_state = dev_replace->replace_state;
687 	args->status.time_started = dev_replace->time_started;
688 	args->status.time_stopped = dev_replace->time_stopped;
689 	args->status.num_write_errors =
690 		atomic64_read(&dev_replace->num_write_errors);
691 	args->status.num_uncorrectable_read_errors =
692 		atomic64_read(&dev_replace->num_uncorrectable_read_errors);
693 	args->status.progress_1000 = btrfs_dev_replace_progress(fs_info);
694 	btrfs_dev_replace_unlock(dev_replace, 0);
695 }
696 
697 int btrfs_dev_replace_cancel(struct btrfs_fs_info *fs_info,
698 			     struct btrfs_ioctl_dev_replace_args *args)
699 {
700 	args->result = __btrfs_dev_replace_cancel(fs_info);
701 	return 0;
702 }
703 
704 static u64 __btrfs_dev_replace_cancel(struct btrfs_fs_info *fs_info)
705 {
706 	struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
707 	struct btrfs_device *tgt_device = NULL;
708 	struct btrfs_trans_handle *trans;
709 	struct btrfs_root *root = fs_info->tree_root;
710 	u64 result;
711 	int ret;
712 
713 	if (sb_rdonly(fs_info->sb))
714 		return -EROFS;
715 
716 	mutex_lock(&dev_replace->lock_finishing_cancel_unmount);
717 	btrfs_dev_replace_lock(dev_replace, 1);
718 	switch (dev_replace->replace_state) {
719 	case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
720 	case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
721 	case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
722 		result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NOT_STARTED;
723 		btrfs_dev_replace_unlock(dev_replace, 1);
724 		goto leave;
725 	case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
726 	case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
727 		result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR;
728 		tgt_device = dev_replace->tgtdev;
729 		dev_replace->tgtdev = NULL;
730 		dev_replace->srcdev = NULL;
731 		break;
732 	}
733 	dev_replace->replace_state = BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED;
734 	dev_replace->time_stopped = get_seconds();
735 	dev_replace->item_needs_writeback = 1;
736 	btrfs_dev_replace_unlock(dev_replace, 1);
737 	btrfs_scrub_cancel(fs_info);
738 
739 	trans = btrfs_start_transaction(root, 0);
740 	if (IS_ERR(trans)) {
741 		mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
742 		return PTR_ERR(trans);
743 	}
744 	ret = btrfs_commit_transaction(trans);
745 	WARN_ON(ret);
746 	if (tgt_device)
747 		btrfs_destroy_dev_replace_tgtdev(fs_info, tgt_device);
748 
749 leave:
750 	mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
751 	return result;
752 }
753 
754 void btrfs_dev_replace_suspend_for_unmount(struct btrfs_fs_info *fs_info)
755 {
756 	struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
757 
758 	mutex_lock(&dev_replace->lock_finishing_cancel_unmount);
759 	btrfs_dev_replace_lock(dev_replace, 1);
760 	switch (dev_replace->replace_state) {
761 	case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
762 	case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
763 	case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
764 	case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
765 		break;
766 	case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
767 		dev_replace->replace_state =
768 			BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED;
769 		dev_replace->time_stopped = get_seconds();
770 		dev_replace->item_needs_writeback = 1;
771 		btrfs_info(fs_info, "suspending dev_replace for unmount");
772 		break;
773 	}
774 
775 	btrfs_dev_replace_unlock(dev_replace, 1);
776 	mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
777 }
778 
779 /* resume dev_replace procedure that was interrupted by unmount */
780 int btrfs_resume_dev_replace_async(struct btrfs_fs_info *fs_info)
781 {
782 	struct task_struct *task;
783 	struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
784 
785 	btrfs_dev_replace_lock(dev_replace, 1);
786 	switch (dev_replace->replace_state) {
787 	case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
788 	case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
789 	case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
790 		btrfs_dev_replace_unlock(dev_replace, 1);
791 		return 0;
792 	case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
793 		break;
794 	case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
795 		dev_replace->replace_state =
796 			BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED;
797 		break;
798 	}
799 	if (!dev_replace->tgtdev || !dev_replace->tgtdev->bdev) {
800 		btrfs_info(fs_info,
801 			   "cannot continue dev_replace, tgtdev is missing");
802 		btrfs_info(fs_info,
803 			   "you may cancel the operation after 'mount -o degraded'");
804 		btrfs_dev_replace_unlock(dev_replace, 1);
805 		return 0;
806 	}
807 	btrfs_dev_replace_unlock(dev_replace, 1);
808 
809 	WARN_ON(test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags));
810 	task = kthread_run(btrfs_dev_replace_kthread, fs_info, "btrfs-devrepl");
811 	return PTR_ERR_OR_ZERO(task);
812 }
813 
814 static int btrfs_dev_replace_kthread(void *data)
815 {
816 	struct btrfs_fs_info *fs_info = data;
817 	struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
818 	u64 progress;
819 
820 	progress = btrfs_dev_replace_progress(fs_info);
821 	progress = div_u64(progress, 10);
822 	btrfs_info_in_rcu(fs_info,
823 		"continuing dev_replace from %s (devid %llu) to target %s @%u%%",
824 		btrfs_dev_name(dev_replace->srcdev),
825 		dev_replace->srcdev->devid,
826 		btrfs_dev_name(dev_replace->tgtdev),
827 		(unsigned int)progress);
828 
829 	btrfs_dev_replace_continue_on_mount(fs_info);
830 	clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
831 
832 	return 0;
833 }
834 
835 static int btrfs_dev_replace_continue_on_mount(struct btrfs_fs_info *fs_info)
836 {
837 	struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
838 	int ret;
839 
840 	ret = btrfs_scrub_dev(fs_info, dev_replace->srcdev->devid,
841 			      dev_replace->committed_cursor_left,
842 			      btrfs_device_get_total_bytes(dev_replace->srcdev),
843 			      &dev_replace->scrub_progress, 0, 1);
844 	ret = btrfs_dev_replace_finishing(fs_info, ret);
845 	WARN_ON(ret);
846 	return 0;
847 }
848 
849 int btrfs_dev_replace_is_ongoing(struct btrfs_dev_replace *dev_replace)
850 {
851 	if (!dev_replace->is_valid)
852 		return 0;
853 
854 	switch (dev_replace->replace_state) {
855 	case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
856 	case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
857 	case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
858 		return 0;
859 	case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
860 	case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
861 		/*
862 		 * return true even if tgtdev is missing (this is
863 		 * something that can happen if the dev_replace
864 		 * procedure is suspended by an umount and then
865 		 * the tgtdev is missing (or "btrfs dev scan") was
866 		 * not called and the the filesystem is remounted
867 		 * in degraded state. This does not stop the
868 		 * dev_replace procedure. It needs to be canceled
869 		 * manually if the cancellation is wanted.
870 		 */
871 		break;
872 	}
873 	return 1;
874 }
875 
876 void btrfs_dev_replace_lock(struct btrfs_dev_replace *dev_replace, int rw)
877 {
878 	if (rw == 1) {
879 		/* write */
880 again:
881 		wait_event(dev_replace->read_lock_wq,
882 			   atomic_read(&dev_replace->blocking_readers) == 0);
883 		write_lock(&dev_replace->lock);
884 		if (atomic_read(&dev_replace->blocking_readers)) {
885 			write_unlock(&dev_replace->lock);
886 			goto again;
887 		}
888 	} else {
889 		read_lock(&dev_replace->lock);
890 		atomic_inc(&dev_replace->read_locks);
891 	}
892 }
893 
894 void btrfs_dev_replace_unlock(struct btrfs_dev_replace *dev_replace, int rw)
895 {
896 	if (rw == 1) {
897 		/* write */
898 		ASSERT(atomic_read(&dev_replace->blocking_readers) == 0);
899 		write_unlock(&dev_replace->lock);
900 	} else {
901 		ASSERT(atomic_read(&dev_replace->read_locks) > 0);
902 		atomic_dec(&dev_replace->read_locks);
903 		read_unlock(&dev_replace->lock);
904 	}
905 }
906 
907 /* inc blocking cnt and release read lock */
908 void btrfs_dev_replace_set_lock_blocking(
909 					struct btrfs_dev_replace *dev_replace)
910 {
911 	/* only set blocking for read lock */
912 	ASSERT(atomic_read(&dev_replace->read_locks) > 0);
913 	atomic_inc(&dev_replace->blocking_readers);
914 	read_unlock(&dev_replace->lock);
915 }
916 
917 /* acquire read lock and dec blocking cnt */
918 void btrfs_dev_replace_clear_lock_blocking(
919 					struct btrfs_dev_replace *dev_replace)
920 {
921 	/* only set blocking for read lock */
922 	ASSERT(atomic_read(&dev_replace->read_locks) > 0);
923 	ASSERT(atomic_read(&dev_replace->blocking_readers) > 0);
924 	read_lock(&dev_replace->lock);
925 	if (atomic_dec_and_test(&dev_replace->blocking_readers) &&
926 	    waitqueue_active(&dev_replace->read_lock_wq))
927 		wake_up(&dev_replace->read_lock_wq);
928 }
929 
930 void btrfs_bio_counter_inc_noblocked(struct btrfs_fs_info *fs_info)
931 {
932 	percpu_counter_inc(&fs_info->bio_counter);
933 }
934 
935 void btrfs_bio_counter_sub(struct btrfs_fs_info *fs_info, s64 amount)
936 {
937 	percpu_counter_sub(&fs_info->bio_counter, amount);
938 
939 	if (waitqueue_active(&fs_info->replace_wait))
940 		wake_up(&fs_info->replace_wait);
941 }
942 
943 void btrfs_bio_counter_inc_blocked(struct btrfs_fs_info *fs_info)
944 {
945 	while (1) {
946 		percpu_counter_inc(&fs_info->bio_counter);
947 		if (likely(!test_bit(BTRFS_FS_STATE_DEV_REPLACING,
948 				     &fs_info->fs_state)))
949 			break;
950 
951 		btrfs_bio_counter_dec(fs_info);
952 		wait_event(fs_info->replace_wait,
953 			   !test_bit(BTRFS_FS_STATE_DEV_REPLACING,
954 				     &fs_info->fs_state));
955 	}
956 }
957