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