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