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 "ctree.h"
13 #include "extent_map.h"
14 #include "disk-io.h"
15 #include "transaction.h"
16 #include "print-tree.h"
17 #include "volumes.h"
18 #include "async-thread.h"
19 #include "check-integrity.h"
20 #include "rcu-string.h"
21 #include "dev-replace.h"
22 #include "sysfs.h"
23 
24 static int btrfs_dev_replace_finishing(struct btrfs_fs_info *fs_info,
25 				       int scrub_ret);
26 static void btrfs_dev_replace_update_device_in_mapping_tree(
27 						struct btrfs_fs_info *fs_info,
28 						struct btrfs_device *srcdev,
29 						struct btrfs_device *tgtdev);
30 static int btrfs_dev_replace_kthread(void *data);
31 
btrfs_init_dev_replace(struct btrfs_fs_info * fs_info)32 int btrfs_init_dev_replace(struct btrfs_fs_info *fs_info)
33 {
34 	struct btrfs_key key;
35 	struct btrfs_root *dev_root = fs_info->dev_root;
36 	struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
37 	struct extent_buffer *eb;
38 	int slot;
39 	int ret = 0;
40 	struct btrfs_path *path = NULL;
41 	int item_size;
42 	struct btrfs_dev_replace_item *ptr;
43 	u64 src_devid;
44 
45 	path = btrfs_alloc_path();
46 	if (!path) {
47 		ret = -ENOMEM;
48 		goto out;
49 	}
50 
51 	key.objectid = 0;
52 	key.type = BTRFS_DEV_REPLACE_KEY;
53 	key.offset = 0;
54 	ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
55 	if (ret) {
56 no_valid_dev_replace_entry_found:
57 		/*
58 		 * We don't have a replace item or it's corrupted.  If there is
59 		 * a replace target, fail the mount.
60 		 */
61 		if (btrfs_find_device(fs_info->fs_devices,
62 				      BTRFS_DEV_REPLACE_DEVID, NULL, NULL, false)) {
63 			btrfs_err(fs_info,
64 			"found replace target device without a valid replace item");
65 			ret = -EUCLEAN;
66 			goto out;
67 		}
68 		ret = 0;
69 		dev_replace->replace_state =
70 			BTRFS_DEV_REPLACE_ITEM_STATE_NEVER_STARTED;
71 		dev_replace->cont_reading_from_srcdev_mode =
72 		    BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_ALWAYS;
73 		dev_replace->replace_state = 0;
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 devid present without an active replace item");
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 	filemap_write_and_wait(bdev->bd_inode->i_mapping);
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 	mutex_lock(&fs_info->fs_devices->device_list_mutex);
264 	set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
265 	device->generation = 0;
266 	device->io_width = fs_info->sectorsize;
267 	device->io_align = fs_info->sectorsize;
268 	device->sector_size = fs_info->sectorsize;
269 	device->total_bytes = btrfs_device_get_total_bytes(srcdev);
270 	device->disk_total_bytes = btrfs_device_get_disk_total_bytes(srcdev);
271 	device->bytes_used = btrfs_device_get_bytes_used(srcdev);
272 	device->commit_total_bytes = srcdev->commit_total_bytes;
273 	device->commit_bytes_used = device->bytes_used;
274 	device->fs_info = fs_info;
275 	device->bdev = bdev;
276 	set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
277 	set_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
278 	device->mode = FMODE_EXCL;
279 	device->dev_stats_valid = 1;
280 	set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE);
281 	device->fs_devices = fs_info->fs_devices;
282 	list_add(&device->dev_list, &fs_info->fs_devices->devices);
283 	fs_info->fs_devices->num_devices++;
284 	fs_info->fs_devices->open_devices++;
285 	mutex_unlock(&fs_info->fs_devices->device_list_mutex);
286 
287 	*device_out = device;
288 	return 0;
289 
290 error:
291 	blkdev_put(bdev, FMODE_EXCL);
292 	return ret;
293 }
294 
295 /*
296  * called from commit_transaction. Writes changed device replace state to
297  * disk.
298  */
btrfs_run_dev_replace(struct btrfs_trans_handle * trans,struct btrfs_fs_info * fs_info)299 int btrfs_run_dev_replace(struct btrfs_trans_handle *trans,
300 			  struct btrfs_fs_info *fs_info)
301 {
302 	int ret;
303 	struct btrfs_root *dev_root = fs_info->dev_root;
304 	struct btrfs_path *path;
305 	struct btrfs_key key;
306 	struct extent_buffer *eb;
307 	struct btrfs_dev_replace_item *ptr;
308 	struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
309 
310 	btrfs_dev_replace_read_lock(dev_replace);
311 	if (!dev_replace->is_valid ||
312 	    !dev_replace->item_needs_writeback) {
313 		btrfs_dev_replace_read_unlock(dev_replace);
314 		return 0;
315 	}
316 	btrfs_dev_replace_read_unlock(dev_replace);
317 
318 	key.objectid = 0;
319 	key.type = BTRFS_DEV_REPLACE_KEY;
320 	key.offset = 0;
321 
322 	path = btrfs_alloc_path();
323 	if (!path) {
324 		ret = -ENOMEM;
325 		goto out;
326 	}
327 	ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
328 	if (ret < 0) {
329 		btrfs_warn(fs_info,
330 			   "error %d while searching for dev_replace item!",
331 			   ret);
332 		goto out;
333 	}
334 
335 	if (ret == 0 &&
336 	    btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
337 		/*
338 		 * need to delete old one and insert a new one.
339 		 * Since no attempt is made to recover any old state, if the
340 		 * dev_replace state is 'running', the data on the target
341 		 * drive is lost.
342 		 * It would be possible to recover the state: just make sure
343 		 * that the beginning of the item is never changed and always
344 		 * contains all the essential information. Then read this
345 		 * minimal set of information and use it as a base for the
346 		 * new state.
347 		 */
348 		ret = btrfs_del_item(trans, dev_root, path);
349 		if (ret != 0) {
350 			btrfs_warn(fs_info,
351 				   "delete too small dev_replace item failed %d!",
352 				   ret);
353 			goto out;
354 		}
355 		ret = 1;
356 	}
357 
358 	if (ret == 1) {
359 		/* need to insert a new item */
360 		btrfs_release_path(path);
361 		ret = btrfs_insert_empty_item(trans, dev_root, path,
362 					      &key, sizeof(*ptr));
363 		if (ret < 0) {
364 			btrfs_warn(fs_info,
365 				   "insert dev_replace item failed %d!", ret);
366 			goto out;
367 		}
368 	}
369 
370 	eb = path->nodes[0];
371 	ptr = btrfs_item_ptr(eb, path->slots[0],
372 			     struct btrfs_dev_replace_item);
373 
374 	btrfs_dev_replace_write_lock(dev_replace);
375 	if (dev_replace->srcdev)
376 		btrfs_set_dev_replace_src_devid(eb, ptr,
377 			dev_replace->srcdev->devid);
378 	else
379 		btrfs_set_dev_replace_src_devid(eb, ptr, (u64)-1);
380 	btrfs_set_dev_replace_cont_reading_from_srcdev_mode(eb, ptr,
381 		dev_replace->cont_reading_from_srcdev_mode);
382 	btrfs_set_dev_replace_replace_state(eb, ptr,
383 		dev_replace->replace_state);
384 	btrfs_set_dev_replace_time_started(eb, ptr, dev_replace->time_started);
385 	btrfs_set_dev_replace_time_stopped(eb, ptr, dev_replace->time_stopped);
386 	btrfs_set_dev_replace_num_write_errors(eb, ptr,
387 		atomic64_read(&dev_replace->num_write_errors));
388 	btrfs_set_dev_replace_num_uncorrectable_read_errors(eb, ptr,
389 		atomic64_read(&dev_replace->num_uncorrectable_read_errors));
390 	dev_replace->cursor_left_last_write_of_item =
391 		dev_replace->cursor_left;
392 	btrfs_set_dev_replace_cursor_left(eb, ptr,
393 		dev_replace->cursor_left_last_write_of_item);
394 	btrfs_set_dev_replace_cursor_right(eb, ptr,
395 		dev_replace->cursor_right);
396 	dev_replace->item_needs_writeback = 0;
397 	btrfs_dev_replace_write_unlock(dev_replace);
398 
399 	btrfs_mark_buffer_dirty(eb);
400 
401 out:
402 	btrfs_free_path(path);
403 
404 	return ret;
405 }
406 
btrfs_after_dev_replace_commit(struct btrfs_fs_info * fs_info)407 void btrfs_after_dev_replace_commit(struct btrfs_fs_info *fs_info)
408 {
409 	struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
410 
411 	dev_replace->committed_cursor_left =
412 		dev_replace->cursor_left_last_write_of_item;
413 }
414 
btrfs_dev_name(struct btrfs_device * device)415 static char* btrfs_dev_name(struct btrfs_device *device)
416 {
417 	if (!device || test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
418 		return "<missing disk>";
419 	else
420 		return rcu_str_deref(device->name);
421 }
422 
btrfs_dev_replace_start(struct btrfs_fs_info * fs_info,const char * tgtdev_name,u64 srcdevid,const char * srcdev_name,int read_src)423 int btrfs_dev_replace_start(struct btrfs_fs_info *fs_info,
424 		const char *tgtdev_name, u64 srcdevid, const char *srcdev_name,
425 		int read_src)
426 {
427 	struct btrfs_root *root = fs_info->dev_root;
428 	struct btrfs_trans_handle *trans;
429 	struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
430 	int ret;
431 	struct btrfs_device *tgt_device = NULL;
432 	struct btrfs_device *src_device = NULL;
433 
434 	ret = btrfs_find_device_by_devspec(fs_info, srcdevid,
435 					    srcdev_name, &src_device);
436 	if (ret)
437 		return ret;
438 
439 	ret = btrfs_init_dev_replace_tgtdev(fs_info, tgtdev_name,
440 					    src_device, &tgt_device);
441 	if (ret)
442 		return ret;
443 
444 	/*
445 	 * Here we commit the transaction to make sure commit_total_bytes
446 	 * of all the devices are updated.
447 	 */
448 	trans = btrfs_attach_transaction(root);
449 	if (!IS_ERR(trans)) {
450 		ret = btrfs_commit_transaction(trans);
451 		if (ret)
452 			return ret;
453 	} else if (PTR_ERR(trans) != -ENOENT) {
454 		return PTR_ERR(trans);
455 	}
456 
457 	btrfs_dev_replace_write_lock(dev_replace);
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 		goto leave;
468 	}
469 
470 	dev_replace->cont_reading_from_srcdev_mode = read_src;
471 	WARN_ON(!src_device);
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 	btrfs_dev_replace_write_unlock(dev_replace);
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 	/* force writing the updated state information to disk */
504 	trans = btrfs_start_transaction(root, 0);
505 	if (IS_ERR(trans)) {
506 		ret = PTR_ERR(trans);
507 		btrfs_dev_replace_write_lock(dev_replace);
508 		dev_replace->replace_state =
509 			BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED;
510 		dev_replace->srcdev = NULL;
511 		dev_replace->tgtdev = NULL;
512 		goto leave;
513 	}
514 
515 	ret = btrfs_commit_transaction(trans);
516 	WARN_ON(ret);
517 
518 	/* the disk copy procedure reuses the scrub code */
519 	ret = btrfs_scrub_dev(fs_info, src_device->devid, 0,
520 			      btrfs_device_get_total_bytes(src_device),
521 			      &dev_replace->scrub_progress, 0, 1);
522 
523 	ret = btrfs_dev_replace_finishing(fs_info, ret);
524 	if (ret == -EINPROGRESS) {
525 		ret = BTRFS_IOCTL_DEV_REPLACE_RESULT_SCRUB_INPROGRESS;
526 	} else {
527 		WARN_ON(ret);
528 	}
529 
530 	return ret;
531 
532 leave:
533 	btrfs_dev_replace_write_unlock(dev_replace);
534 	btrfs_destroy_dev_replace_tgtdev(tgt_device);
535 	return ret;
536 }
537 
btrfs_dev_replace_by_ioctl(struct btrfs_fs_info * fs_info,struct btrfs_ioctl_dev_replace_args * args)538 int btrfs_dev_replace_by_ioctl(struct btrfs_fs_info *fs_info,
539 			    struct btrfs_ioctl_dev_replace_args *args)
540 {
541 	int ret;
542 
543 	switch (args->start.cont_reading_from_srcdev_mode) {
544 	case BTRFS_IOCTL_DEV_REPLACE_CONT_READING_FROM_SRCDEV_MODE_ALWAYS:
545 	case BTRFS_IOCTL_DEV_REPLACE_CONT_READING_FROM_SRCDEV_MODE_AVOID:
546 		break;
547 	default:
548 		return -EINVAL;
549 	}
550 
551 	if ((args->start.srcdevid == 0 && args->start.srcdev_name[0] == '\0') ||
552 	    args->start.tgtdev_name[0] == '\0')
553 		return -EINVAL;
554 
555 	ret = btrfs_dev_replace_start(fs_info, args->start.tgtdev_name,
556 					args->start.srcdevid,
557 					args->start.srcdev_name,
558 					args->start.cont_reading_from_srcdev_mode);
559 	args->result = ret;
560 	/* don't warn if EINPROGRESS, someone else might be running scrub */
561 	if (ret == BTRFS_IOCTL_DEV_REPLACE_RESULT_SCRUB_INPROGRESS)
562 		ret = 0;
563 
564 	return ret;
565 }
566 
567 /*
568  * blocked until all in-flight bios operations are finished.
569  */
btrfs_rm_dev_replace_blocked(struct btrfs_fs_info * fs_info)570 static void btrfs_rm_dev_replace_blocked(struct btrfs_fs_info *fs_info)
571 {
572 	set_bit(BTRFS_FS_STATE_DEV_REPLACING, &fs_info->fs_state);
573 	wait_event(fs_info->replace_wait, !percpu_counter_sum(
574 		   &fs_info->bio_counter));
575 }
576 
577 /*
578  * we have removed target device, it is safe to allow new bios request.
579  */
btrfs_rm_dev_replace_unblocked(struct btrfs_fs_info * fs_info)580 static void btrfs_rm_dev_replace_unblocked(struct btrfs_fs_info *fs_info)
581 {
582 	clear_bit(BTRFS_FS_STATE_DEV_REPLACING, &fs_info->fs_state);
583 	wake_up(&fs_info->replace_wait);
584 }
585 
btrfs_dev_replace_finishing(struct btrfs_fs_info * fs_info,int scrub_ret)586 static int btrfs_dev_replace_finishing(struct btrfs_fs_info *fs_info,
587 				       int scrub_ret)
588 {
589 	struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
590 	struct btrfs_device *tgt_device;
591 	struct btrfs_device *src_device;
592 	struct btrfs_root *root = fs_info->tree_root;
593 	u8 uuid_tmp[BTRFS_UUID_SIZE];
594 	struct btrfs_trans_handle *trans;
595 	int ret = 0;
596 
597 	/* don't allow cancel or unmount to disturb the finishing procedure */
598 	mutex_lock(&dev_replace->lock_finishing_cancel_unmount);
599 
600 	btrfs_dev_replace_read_lock(dev_replace);
601 	/* was the operation canceled, or is it finished? */
602 	if (dev_replace->replace_state !=
603 	    BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED) {
604 		btrfs_dev_replace_read_unlock(dev_replace);
605 		mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
606 		return 0;
607 	}
608 
609 	tgt_device = dev_replace->tgtdev;
610 	src_device = dev_replace->srcdev;
611 	btrfs_dev_replace_read_unlock(dev_replace);
612 
613 	/*
614 	 * flush all outstanding I/O and inode extent mappings before the
615 	 * copy operation is declared as being finished
616 	 */
617 	ret = btrfs_start_delalloc_roots(fs_info, -1);
618 	if (ret) {
619 		mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
620 		return ret;
621 	}
622 	btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1);
623 
624 	while (1) {
625 		trans = btrfs_start_transaction(root, 0);
626 		if (IS_ERR(trans)) {
627 			mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
628 			return PTR_ERR(trans);
629 		}
630 		ret = btrfs_commit_transaction(trans);
631 		WARN_ON(ret);
632 		/* keep away write_all_supers() during the finishing procedure */
633 		mutex_lock(&fs_info->fs_devices->device_list_mutex);
634 		mutex_lock(&fs_info->chunk_mutex);
635 		if (src_device->has_pending_chunks) {
636 			mutex_unlock(&root->fs_info->chunk_mutex);
637 			mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
638 		} else {
639 			break;
640 		}
641 	}
642 
643 	btrfs_dev_replace_write_lock(dev_replace);
644 	dev_replace->replace_state =
645 		scrub_ret ? BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED
646 			  : BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED;
647 	dev_replace->tgtdev = NULL;
648 	dev_replace->srcdev = NULL;
649 	dev_replace->time_stopped = ktime_get_real_seconds();
650 	dev_replace->item_needs_writeback = 1;
651 
652 	/* replace old device with new one in mapping tree */
653 	if (!scrub_ret) {
654 		btrfs_dev_replace_update_device_in_mapping_tree(fs_info,
655 								src_device,
656 								tgt_device);
657 	} else {
658 		btrfs_err_in_rcu(fs_info,
659 				 "btrfs_scrub_dev(%s, %llu, %s) failed %d",
660 				 btrfs_dev_name(src_device),
661 				 src_device->devid,
662 				 rcu_str_deref(tgt_device->name), scrub_ret);
663 		btrfs_dev_replace_write_unlock(dev_replace);
664 		mutex_unlock(&fs_info->chunk_mutex);
665 		mutex_unlock(&fs_info->fs_devices->device_list_mutex);
666 		btrfs_rm_dev_replace_blocked(fs_info);
667 		if (tgt_device)
668 			btrfs_destroy_dev_replace_tgtdev(tgt_device);
669 		btrfs_rm_dev_replace_unblocked(fs_info);
670 		mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
671 
672 		return scrub_ret;
673 	}
674 
675 	btrfs_info_in_rcu(fs_info,
676 			  "dev_replace from %s (devid %llu) to %s finished",
677 			  btrfs_dev_name(src_device),
678 			  src_device->devid,
679 			  rcu_str_deref(tgt_device->name));
680 	clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &tgt_device->dev_state);
681 	tgt_device->devid = src_device->devid;
682 	src_device->devid = BTRFS_DEV_REPLACE_DEVID;
683 	memcpy(uuid_tmp, tgt_device->uuid, sizeof(uuid_tmp));
684 	memcpy(tgt_device->uuid, src_device->uuid, sizeof(tgt_device->uuid));
685 	memcpy(src_device->uuid, uuid_tmp, sizeof(src_device->uuid));
686 	btrfs_device_set_total_bytes(tgt_device, src_device->total_bytes);
687 	btrfs_device_set_disk_total_bytes(tgt_device,
688 					  src_device->disk_total_bytes);
689 	btrfs_device_set_bytes_used(tgt_device, src_device->bytes_used);
690 	ASSERT(list_empty(&src_device->resized_list));
691 	tgt_device->commit_total_bytes = src_device->commit_total_bytes;
692 	tgt_device->commit_bytes_used = src_device->bytes_used;
693 
694 	btrfs_assign_next_active_device(src_device, tgt_device);
695 
696 	list_add(&tgt_device->dev_alloc_list, &fs_info->fs_devices->alloc_list);
697 	fs_info->fs_devices->rw_devices++;
698 
699 	btrfs_dev_replace_write_unlock(dev_replace);
700 
701 	btrfs_rm_dev_replace_blocked(fs_info);
702 
703 	btrfs_rm_dev_replace_remove_srcdev(src_device);
704 
705 	btrfs_rm_dev_replace_unblocked(fs_info);
706 
707 	/*
708 	 * Increment dev_stats_ccnt so that btrfs_run_dev_stats() will
709 	 * update on-disk dev stats value during commit transaction
710 	 */
711 	atomic_inc(&tgt_device->dev_stats_ccnt);
712 
713 	/*
714 	 * this is again a consistent state where no dev_replace procedure
715 	 * is running, the target device is part of the filesystem, the
716 	 * source device is not part of the filesystem anymore and its 1st
717 	 * superblock is scratched out so that it is no longer marked to
718 	 * belong to this filesystem.
719 	 */
720 	mutex_unlock(&fs_info->chunk_mutex);
721 	mutex_unlock(&fs_info->fs_devices->device_list_mutex);
722 
723 	/* replace the sysfs entry */
724 	btrfs_sysfs_rm_device_link(fs_info->fs_devices, src_device);
725 	btrfs_rm_dev_replace_free_srcdev(fs_info, src_device);
726 
727 	/* write back the superblocks */
728 	trans = btrfs_start_transaction(root, 0);
729 	if (!IS_ERR(trans))
730 		btrfs_commit_transaction(trans);
731 
732 	mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
733 
734 	return 0;
735 }
736 
btrfs_dev_replace_update_device_in_mapping_tree(struct btrfs_fs_info * fs_info,struct btrfs_device * srcdev,struct btrfs_device * tgtdev)737 static void btrfs_dev_replace_update_device_in_mapping_tree(
738 						struct btrfs_fs_info *fs_info,
739 						struct btrfs_device *srcdev,
740 						struct btrfs_device *tgtdev)
741 {
742 	struct extent_map_tree *em_tree = &fs_info->mapping_tree.map_tree;
743 	struct extent_map *em;
744 	struct map_lookup *map;
745 	u64 start = 0;
746 	int i;
747 
748 	write_lock(&em_tree->lock);
749 	do {
750 		em = lookup_extent_mapping(em_tree, start, (u64)-1);
751 		if (!em)
752 			break;
753 		map = em->map_lookup;
754 		for (i = 0; i < map->num_stripes; i++)
755 			if (srcdev == map->stripes[i].dev)
756 				map->stripes[i].dev = tgtdev;
757 		start = em->start + em->len;
758 		free_extent_map(em);
759 	} while (start);
760 	write_unlock(&em_tree->lock);
761 }
762 
763 /*
764  * Read progress of device replace status according to the state and last
765  * stored position. The value format is the same as for
766  * btrfs_dev_replace::progress_1000
767  */
btrfs_dev_replace_progress(struct btrfs_fs_info * fs_info)768 static u64 btrfs_dev_replace_progress(struct btrfs_fs_info *fs_info)
769 {
770 	struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
771 	u64 ret = 0;
772 
773 	switch (dev_replace->replace_state) {
774 	case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
775 	case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
776 		ret = 0;
777 		break;
778 	case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
779 		ret = 1000;
780 		break;
781 	case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
782 	case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
783 		ret = div64_u64(dev_replace->cursor_left,
784 				div_u64(btrfs_device_get_total_bytes(
785 						dev_replace->srcdev), 1000));
786 		break;
787 	}
788 
789 	return ret;
790 }
791 
btrfs_dev_replace_status(struct btrfs_fs_info * fs_info,struct btrfs_ioctl_dev_replace_args * args)792 void btrfs_dev_replace_status(struct btrfs_fs_info *fs_info,
793 			      struct btrfs_ioctl_dev_replace_args *args)
794 {
795 	struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
796 
797 	btrfs_dev_replace_read_lock(dev_replace);
798 	/* even if !dev_replace_is_valid, the values are good enough for
799 	 * the replace_status ioctl */
800 	args->result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR;
801 	args->status.replace_state = dev_replace->replace_state;
802 	args->status.time_started = dev_replace->time_started;
803 	args->status.time_stopped = dev_replace->time_stopped;
804 	args->status.num_write_errors =
805 		atomic64_read(&dev_replace->num_write_errors);
806 	args->status.num_uncorrectable_read_errors =
807 		atomic64_read(&dev_replace->num_uncorrectable_read_errors);
808 	args->status.progress_1000 = btrfs_dev_replace_progress(fs_info);
809 	btrfs_dev_replace_read_unlock(dev_replace);
810 }
811 
btrfs_dev_replace_cancel(struct btrfs_fs_info * fs_info)812 int btrfs_dev_replace_cancel(struct btrfs_fs_info *fs_info)
813 {
814 	struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
815 	struct btrfs_device *tgt_device = NULL;
816 	struct btrfs_device *src_device = NULL;
817 	struct btrfs_trans_handle *trans;
818 	struct btrfs_root *root = fs_info->tree_root;
819 	int result;
820 	int ret;
821 
822 	if (sb_rdonly(fs_info->sb))
823 		return -EROFS;
824 
825 	mutex_lock(&dev_replace->lock_finishing_cancel_unmount);
826 	btrfs_dev_replace_write_lock(dev_replace);
827 	switch (dev_replace->replace_state) {
828 	case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
829 	case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
830 	case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
831 		result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NOT_STARTED;
832 		btrfs_dev_replace_write_unlock(dev_replace);
833 		break;
834 	case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
835 		tgt_device = dev_replace->tgtdev;
836 		src_device = dev_replace->srcdev;
837 		btrfs_dev_replace_write_unlock(dev_replace);
838 		ret = btrfs_scrub_cancel(fs_info);
839 		if (ret < 0) {
840 			result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NOT_STARTED;
841 		} else {
842 			result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR;
843 			/*
844 			 * btrfs_dev_replace_finishing() will handle the
845 			 * cleanup part
846 			 */
847 			btrfs_info_in_rcu(fs_info,
848 				"dev_replace from %s (devid %llu) to %s canceled",
849 				btrfs_dev_name(src_device), src_device->devid,
850 				btrfs_dev_name(tgt_device));
851 		}
852 		break;
853 	case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
854 		/*
855 		 * Scrub doing the replace isn't running so we need to do the
856 		 * cleanup step of btrfs_dev_replace_finishing() here
857 		 */
858 		result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR;
859 		tgt_device = dev_replace->tgtdev;
860 		src_device = dev_replace->srcdev;
861 		dev_replace->tgtdev = NULL;
862 		dev_replace->srcdev = NULL;
863 		dev_replace->replace_state =
864 				BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED;
865 		dev_replace->time_stopped = ktime_get_real_seconds();
866 		dev_replace->item_needs_writeback = 1;
867 
868 		btrfs_dev_replace_write_unlock(dev_replace);
869 
870 		btrfs_scrub_cancel(fs_info);
871 
872 		trans = btrfs_start_transaction(root, 0);
873 		if (IS_ERR(trans)) {
874 			mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
875 			return PTR_ERR(trans);
876 		}
877 		ret = btrfs_commit_transaction(trans);
878 		WARN_ON(ret);
879 
880 		btrfs_info_in_rcu(fs_info,
881 		"suspended dev_replace from %s (devid %llu) to %s canceled",
882 			btrfs_dev_name(src_device), src_device->devid,
883 			btrfs_dev_name(tgt_device));
884 
885 		if (tgt_device)
886 			btrfs_destroy_dev_replace_tgtdev(tgt_device);
887 		break;
888 	default:
889 		result = -EINVAL;
890 	}
891 
892 	mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
893 	return result;
894 }
895 
btrfs_dev_replace_suspend_for_unmount(struct btrfs_fs_info * fs_info)896 void btrfs_dev_replace_suspend_for_unmount(struct btrfs_fs_info *fs_info)
897 {
898 	struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
899 
900 	mutex_lock(&dev_replace->lock_finishing_cancel_unmount);
901 	btrfs_dev_replace_write_lock(dev_replace);
902 	switch (dev_replace->replace_state) {
903 	case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
904 	case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
905 	case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
906 	case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
907 		break;
908 	case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
909 		dev_replace->replace_state =
910 			BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED;
911 		dev_replace->time_stopped = ktime_get_real_seconds();
912 		dev_replace->item_needs_writeback = 1;
913 		btrfs_info(fs_info, "suspending dev_replace for unmount");
914 		break;
915 	}
916 
917 	btrfs_dev_replace_write_unlock(dev_replace);
918 	mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
919 }
920 
921 /* resume dev_replace procedure that was interrupted by unmount */
btrfs_resume_dev_replace_async(struct btrfs_fs_info * fs_info)922 int btrfs_resume_dev_replace_async(struct btrfs_fs_info *fs_info)
923 {
924 	struct task_struct *task;
925 	struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
926 
927 	btrfs_dev_replace_write_lock(dev_replace);
928 	switch (dev_replace->replace_state) {
929 	case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
930 	case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
931 	case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
932 		btrfs_dev_replace_write_unlock(dev_replace);
933 		return 0;
934 	case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
935 		break;
936 	case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
937 		dev_replace->replace_state =
938 			BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED;
939 		break;
940 	}
941 	if (!dev_replace->tgtdev || !dev_replace->tgtdev->bdev) {
942 		btrfs_info(fs_info,
943 			   "cannot continue dev_replace, tgtdev is missing");
944 		btrfs_info(fs_info,
945 			   "you may cancel the operation after 'mount -o degraded'");
946 		dev_replace->replace_state =
947 					BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED;
948 		btrfs_dev_replace_write_unlock(dev_replace);
949 		return 0;
950 	}
951 	btrfs_dev_replace_write_unlock(dev_replace);
952 
953 	/*
954 	 * This could collide with a paused balance, but the exclusive op logic
955 	 * should never allow both to start and pause. We don't want to allow
956 	 * dev-replace to start anyway.
957 	 */
958 	if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
959 		btrfs_dev_replace_write_lock(dev_replace);
960 		dev_replace->replace_state =
961 					BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED;
962 		btrfs_dev_replace_write_unlock(dev_replace);
963 		btrfs_info(fs_info,
964 		"cannot resume dev-replace, other exclusive operation running");
965 		return 0;
966 	}
967 
968 	task = kthread_run(btrfs_dev_replace_kthread, fs_info, "btrfs-devrepl");
969 	return PTR_ERR_OR_ZERO(task);
970 }
971 
btrfs_dev_replace_kthread(void * data)972 static int btrfs_dev_replace_kthread(void *data)
973 {
974 	struct btrfs_fs_info *fs_info = data;
975 	struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
976 	u64 progress;
977 	int ret;
978 
979 	progress = btrfs_dev_replace_progress(fs_info);
980 	progress = div_u64(progress, 10);
981 	btrfs_info_in_rcu(fs_info,
982 		"continuing dev_replace from %s (devid %llu) to target %s @%u%%",
983 		btrfs_dev_name(dev_replace->srcdev),
984 		dev_replace->srcdev->devid,
985 		btrfs_dev_name(dev_replace->tgtdev),
986 		(unsigned int)progress);
987 
988 	ret = btrfs_scrub_dev(fs_info, dev_replace->srcdev->devid,
989 			      dev_replace->committed_cursor_left,
990 			      btrfs_device_get_total_bytes(dev_replace->srcdev),
991 			      &dev_replace->scrub_progress, 0, 1);
992 	ret = btrfs_dev_replace_finishing(fs_info, ret);
993 	WARN_ON(ret);
994 
995 	clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
996 	return 0;
997 }
998 
btrfs_dev_replace_is_ongoing(struct btrfs_dev_replace * dev_replace)999 int btrfs_dev_replace_is_ongoing(struct btrfs_dev_replace *dev_replace)
1000 {
1001 	if (!dev_replace->is_valid)
1002 		return 0;
1003 
1004 	switch (dev_replace->replace_state) {
1005 	case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
1006 	case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
1007 	case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
1008 		return 0;
1009 	case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
1010 	case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
1011 		/*
1012 		 * return true even if tgtdev is missing (this is
1013 		 * something that can happen if the dev_replace
1014 		 * procedure is suspended by an umount and then
1015 		 * the tgtdev is missing (or "btrfs dev scan") was
1016 		 * not called and the the filesystem is remounted
1017 		 * in degraded state. This does not stop the
1018 		 * dev_replace procedure. It needs to be canceled
1019 		 * manually if the cancellation is wanted.
1020 		 */
1021 		break;
1022 	}
1023 	return 1;
1024 }
1025 
btrfs_dev_replace_read_lock(struct btrfs_dev_replace * dev_replace)1026 void btrfs_dev_replace_read_lock(struct btrfs_dev_replace *dev_replace)
1027 {
1028 	read_lock(&dev_replace->lock);
1029 	atomic_inc(&dev_replace->read_locks);
1030 }
1031 
btrfs_dev_replace_read_unlock(struct btrfs_dev_replace * dev_replace)1032 void btrfs_dev_replace_read_unlock(struct btrfs_dev_replace *dev_replace)
1033 {
1034 	ASSERT(atomic_read(&dev_replace->read_locks) > 0);
1035 	atomic_dec(&dev_replace->read_locks);
1036 	read_unlock(&dev_replace->lock);
1037 }
1038 
btrfs_dev_replace_write_lock(struct btrfs_dev_replace * dev_replace)1039 void btrfs_dev_replace_write_lock(struct btrfs_dev_replace *dev_replace)
1040 {
1041 again:
1042 	wait_event(dev_replace->read_lock_wq,
1043 		   atomic_read(&dev_replace->blocking_readers) == 0);
1044 	write_lock(&dev_replace->lock);
1045 	if (atomic_read(&dev_replace->blocking_readers)) {
1046 		write_unlock(&dev_replace->lock);
1047 		goto again;
1048 	}
1049 }
1050 
btrfs_dev_replace_write_unlock(struct btrfs_dev_replace * dev_replace)1051 void btrfs_dev_replace_write_unlock(struct btrfs_dev_replace *dev_replace)
1052 {
1053 	ASSERT(atomic_read(&dev_replace->blocking_readers) == 0);
1054 	write_unlock(&dev_replace->lock);
1055 }
1056 
1057 /* inc blocking cnt and release read lock */
btrfs_dev_replace_set_lock_blocking(struct btrfs_dev_replace * dev_replace)1058 void btrfs_dev_replace_set_lock_blocking(
1059 					struct btrfs_dev_replace *dev_replace)
1060 {
1061 	/* only set blocking for read lock */
1062 	ASSERT(atomic_read(&dev_replace->read_locks) > 0);
1063 	atomic_inc(&dev_replace->blocking_readers);
1064 	read_unlock(&dev_replace->lock);
1065 }
1066 
1067 /* acquire read lock and dec blocking cnt */
btrfs_dev_replace_clear_lock_blocking(struct btrfs_dev_replace * dev_replace)1068 void btrfs_dev_replace_clear_lock_blocking(
1069 					struct btrfs_dev_replace *dev_replace)
1070 {
1071 	/* only set blocking for read lock */
1072 	ASSERT(atomic_read(&dev_replace->read_locks) > 0);
1073 	ASSERT(atomic_read(&dev_replace->blocking_readers) > 0);
1074 	read_lock(&dev_replace->lock);
1075 	/* Barrier implied by atomic_dec_and_test */
1076 	if (atomic_dec_and_test(&dev_replace->blocking_readers))
1077 		cond_wake_up_nomb(&dev_replace->read_lock_wq);
1078 }
1079 
btrfs_bio_counter_inc_noblocked(struct btrfs_fs_info * fs_info)1080 void btrfs_bio_counter_inc_noblocked(struct btrfs_fs_info *fs_info)
1081 {
1082 	percpu_counter_inc(&fs_info->bio_counter);
1083 }
1084 
btrfs_bio_counter_sub(struct btrfs_fs_info * fs_info,s64 amount)1085 void btrfs_bio_counter_sub(struct btrfs_fs_info *fs_info, s64 amount)
1086 {
1087 	percpu_counter_sub(&fs_info->bio_counter, amount);
1088 	cond_wake_up_nomb(&fs_info->replace_wait);
1089 }
1090 
btrfs_bio_counter_inc_blocked(struct btrfs_fs_info * fs_info)1091 void btrfs_bio_counter_inc_blocked(struct btrfs_fs_info *fs_info)
1092 {
1093 	while (1) {
1094 		percpu_counter_inc(&fs_info->bio_counter);
1095 		if (likely(!test_bit(BTRFS_FS_STATE_DEV_REPLACING,
1096 				     &fs_info->fs_state)))
1097 			break;
1098 
1099 		btrfs_bio_counter_dec(fs_info);
1100 		wait_event(fs_info->replace_wait,
1101 			   !test_bit(BTRFS_FS_STATE_DEV_REPLACING,
1102 				     &fs_info->fs_state));
1103 	}
1104 }
1105