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