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 path->need_commit_sem = true;
498
499 key.objectid = src_dev->devid;
500 key.type = BTRFS_DEV_EXTENT_KEY;
501 key.offset = 0;
502
503 btrfs_for_each_slot(root, &key, &found_key, path, iter_ret) {
504 struct extent_buffer *leaf = path->nodes[0];
505
506 if (found_key.objectid != src_dev->devid)
507 break;
508
509 if (found_key.type != BTRFS_DEV_EXTENT_KEY)
510 break;
511
512 if (found_key.offset < key.offset)
513 break;
514
515 dev_extent = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_extent);
516
517 chunk_offset = btrfs_dev_extent_chunk_offset(leaf, dev_extent);
518
519 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
520 if (!cache)
521 continue;
522
523 set_bit(BLOCK_GROUP_FLAG_TO_COPY, &cache->runtime_flags);
524 btrfs_put_block_group(cache);
525 }
526 if (iter_ret < 0)
527 ret = iter_ret;
528
529 btrfs_free_path(path);
530 unlock:
531 mutex_unlock(&fs_info->chunk_mutex);
532
533 return ret;
534 }
535
btrfs_finish_block_group_to_copy(struct btrfs_device * srcdev,struct btrfs_block_group * cache,u64 physical)536 bool btrfs_finish_block_group_to_copy(struct btrfs_device *srcdev,
537 struct btrfs_block_group *cache,
538 u64 physical)
539 {
540 struct btrfs_fs_info *fs_info = cache->fs_info;
541 struct btrfs_chunk_map *map;
542 u64 chunk_offset = cache->start;
543 int num_extents, cur_extent;
544 int i;
545
546 /* Do not use "to_copy" on non zoned filesystem for now */
547 if (!btrfs_is_zoned(fs_info))
548 return true;
549
550 spin_lock(&cache->lock);
551 if (test_bit(BLOCK_GROUP_FLAG_REMOVED, &cache->runtime_flags)) {
552 spin_unlock(&cache->lock);
553 return true;
554 }
555 spin_unlock(&cache->lock);
556
557 map = btrfs_get_chunk_map(fs_info, chunk_offset, 1);
558 ASSERT(!IS_ERR(map));
559
560 num_extents = 0;
561 cur_extent = 0;
562 for (i = 0; i < map->num_stripes; i++) {
563 /* We have more device extent to copy */
564 if (srcdev != map->stripes[i].dev)
565 continue;
566
567 num_extents++;
568 if (physical == map->stripes[i].physical)
569 cur_extent = i;
570 }
571
572 btrfs_free_chunk_map(map);
573
574 if (num_extents > 1 && cur_extent < num_extents - 1) {
575 /*
576 * Has more stripes on this device. Keep this block group
577 * readonly until we finish all the stripes.
578 */
579 return false;
580 }
581
582 /* Last stripe on this device */
583 clear_bit(BLOCK_GROUP_FLAG_TO_COPY, &cache->runtime_flags);
584
585 return true;
586 }
587
btrfs_dev_replace_start(struct btrfs_fs_info * fs_info,const char * tgtdev_name,u64 srcdevid,const char * srcdev_name,int read_src)588 static int btrfs_dev_replace_start(struct btrfs_fs_info *fs_info,
589 const char *tgtdev_name, u64 srcdevid, const char *srcdev_name,
590 int read_src)
591 {
592 struct btrfs_root *root = fs_info->dev_root;
593 struct btrfs_trans_handle *trans;
594 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
595 int ret;
596 struct btrfs_device *tgt_device = NULL;
597 struct btrfs_device *src_device = NULL;
598
599 src_device = btrfs_find_device_by_devspec(fs_info, srcdevid,
600 srcdev_name);
601 if (IS_ERR(src_device))
602 return PTR_ERR(src_device);
603
604 if (btrfs_pinned_by_swapfile(fs_info, src_device)) {
605 btrfs_warn(fs_info,
606 "cannot replace device %s (devid %llu) due to active swapfile",
607 btrfs_dev_name(src_device), src_device->devid);
608 return -ETXTBSY;
609 }
610
611 /*
612 * Here we commit the transaction to make sure commit_total_bytes
613 * of all the devices are updated.
614 */
615 trans = btrfs_attach_transaction(root);
616 if (!IS_ERR(trans)) {
617 ret = btrfs_commit_transaction(trans);
618 if (ret)
619 return ret;
620 } else if (PTR_ERR(trans) != -ENOENT) {
621 return PTR_ERR(trans);
622 }
623
624 ret = btrfs_init_dev_replace_tgtdev(fs_info, tgtdev_name,
625 src_device, &tgt_device);
626 if (ret)
627 return ret;
628
629 /* Deny the source before mark, so every 'leave' unwinds both denied. */
630 if (src_device->bdev) {
631 ret = bdev_deny_freeze(src_device->bdev);
632 if (ret) {
633 btrfs_destroy_dev_replace_tgtdev(tgt_device, true);
634 return ret;
635 }
636 }
637
638 ret = mark_block_group_to_copy(fs_info, src_device);
639 if (ret)
640 goto leave;
641
642 down_write(&dev_replace->rwsem);
643 dev_replace->replace_task = current;
644 switch (dev_replace->replace_state) {
645 case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
646 case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
647 case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
648 break;
649 case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
650 case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
651 DEBUG_WARN("unexpected STARTED or SUSPENDED dev-replace state");
652 ret = BTRFS_IOCTL_DEV_REPLACE_RESULT_ALREADY_STARTED;
653 up_write(&dev_replace->rwsem);
654 goto leave;
655 }
656
657 dev_replace->cont_reading_from_srcdev_mode = read_src;
658 dev_replace->srcdev = src_device;
659 dev_replace->tgtdev = tgt_device;
660
661 btrfs_info(fs_info,
662 "dev_replace from %s (devid %llu) to %s started",
663 btrfs_dev_name(src_device),
664 src_device->devid,
665 btrfs_dev_name(tgt_device));
666
667 /*
668 * from now on, the writes to the srcdev are all duplicated to
669 * go to the tgtdev as well (refer to btrfs_map_block()).
670 */
671 dev_replace->replace_state = BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED;
672 dev_replace->time_started = ktime_get_real_seconds();
673 dev_replace->cursor_left = 0;
674 dev_replace->committed_cursor_left = 0;
675 dev_replace->cursor_left_last_write_of_item = 0;
676 dev_replace->cursor_right = 0;
677 dev_replace->is_valid = 1;
678 dev_replace->item_needs_writeback = 1;
679 atomic64_set(&dev_replace->num_write_errors, 0);
680 atomic64_set(&dev_replace->num_uncorrectable_read_errors, 0);
681 up_write(&dev_replace->rwsem);
682
683 ret = btrfs_sysfs_add_device(tgt_device);
684 if (ret)
685 btrfs_err(fs_info, "kobj add dev failed %d", ret);
686
687 btrfs_wait_ordered_roots(fs_info, U64_MAX, NULL);
688
689 /*
690 * Commit dev_replace state and reserve 1 item for it.
691 * This is crucial to ensure we won't miss copying extents for new block
692 * groups that are allocated after we started the device replace, and
693 * must be done after setting up the device replace state.
694 */
695 trans = btrfs_start_transaction(root, 1);
696 if (IS_ERR(trans)) {
697 ret = PTR_ERR(trans);
698 down_write(&dev_replace->rwsem);
699 dev_replace->replace_state =
700 BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED;
701 dev_replace->srcdev = NULL;
702 dev_replace->tgtdev = NULL;
703 up_write(&dev_replace->rwsem);
704 goto leave;
705 }
706
707 ret = btrfs_commit_transaction(trans);
708 WARN_ON(ret);
709
710 /* the disk copy procedure reuses the scrub code */
711 ret = btrfs_scrub_dev(fs_info, src_device->devid, 0,
712 btrfs_device_get_total_bytes(src_device),
713 &dev_replace->scrub_progress, false, true);
714
715 ret = btrfs_dev_replace_finishing(fs_info, ret);
716 if (ret == -EINPROGRESS)
717 ret = BTRFS_IOCTL_DEV_REPLACE_RESULT_SCRUB_INPROGRESS;
718
719 return ret;
720
721 leave:
722 if (src_device->bdev)
723 bdev_allow_freeze(src_device->bdev);
724 btrfs_destroy_dev_replace_tgtdev(tgt_device, true);
725 return ret;
726 }
727
btrfs_check_replace_dev_names(struct btrfs_ioctl_dev_replace_args * args)728 static int btrfs_check_replace_dev_names(struct btrfs_ioctl_dev_replace_args *args)
729 {
730 if (args->start.srcdevid == 0) {
731 if (memchr(args->start.srcdev_name, 0,
732 sizeof(args->start.srcdev_name)) == NULL)
733 return -ENAMETOOLONG;
734 } else {
735 args->start.srcdev_name[0] = 0;
736 }
737
738 if (memchr(args->start.tgtdev_name, 0,
739 sizeof(args->start.tgtdev_name)) == NULL)
740 return -ENAMETOOLONG;
741
742 return 0;
743 }
744
btrfs_dev_replace_by_ioctl(struct btrfs_fs_info * fs_info,struct btrfs_ioctl_dev_replace_args * args)745 int btrfs_dev_replace_by_ioctl(struct btrfs_fs_info *fs_info,
746 struct btrfs_ioctl_dev_replace_args *args)
747 {
748 int ret;
749
750 switch (args->start.cont_reading_from_srcdev_mode) {
751 case BTRFS_IOCTL_DEV_REPLACE_CONT_READING_FROM_SRCDEV_MODE_ALWAYS:
752 case BTRFS_IOCTL_DEV_REPLACE_CONT_READING_FROM_SRCDEV_MODE_AVOID:
753 break;
754 default:
755 return -EINVAL;
756 }
757 ret = btrfs_check_replace_dev_names(args);
758 if (ret < 0)
759 return ret;
760
761 ret = btrfs_dev_replace_start(fs_info, args->start.tgtdev_name,
762 args->start.srcdevid,
763 args->start.srcdev_name,
764 args->start.cont_reading_from_srcdev_mode);
765 args->result = ret;
766 /* don't warn if EINPROGRESS, someone else might be running scrub */
767 if (ret == BTRFS_IOCTL_DEV_REPLACE_RESULT_SCRUB_INPROGRESS ||
768 ret == BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR)
769 return 0;
770
771 return ret;
772 }
773
774 /*
775 * blocked until all in-flight bios operations are finished.
776 */
btrfs_rm_dev_replace_blocked(struct btrfs_fs_info * fs_info)777 static void btrfs_rm_dev_replace_blocked(struct btrfs_fs_info *fs_info)
778 {
779 set_bit(BTRFS_FS_STATE_DEV_REPLACING, &fs_info->fs_state);
780 wait_event(fs_info->dev_replace.replace_wait, !percpu_counter_sum(
781 &fs_info->dev_replace.bio_counter));
782 }
783
784 /*
785 * we have removed target device, it is safe to allow new bios request.
786 */
btrfs_rm_dev_replace_unblocked(struct btrfs_fs_info * fs_info)787 static void btrfs_rm_dev_replace_unblocked(struct btrfs_fs_info *fs_info)
788 {
789 clear_bit(BTRFS_FS_STATE_DEV_REPLACING, &fs_info->fs_state);
790 wake_up(&fs_info->dev_replace.replace_wait);
791 }
792
793 /*
794 * When finishing the device replace, before swapping the source device with the
795 * target device we must update the chunk allocation state in the target device,
796 * as it is empty because replace works by directly copying the chunks and not
797 * through the normal chunk allocation path.
798 */
btrfs_set_target_alloc_state(struct btrfs_device * srcdev,struct btrfs_device * tgtdev)799 static int btrfs_set_target_alloc_state(struct btrfs_device *srcdev,
800 struct btrfs_device *tgtdev)
801 {
802 struct extent_state *cached_state = NULL;
803 u64 start = 0;
804 u64 found_start;
805 u64 found_end;
806 int ret = 0;
807
808 lockdep_assert_held(&srcdev->fs_info->chunk_mutex);
809
810 while (btrfs_find_first_extent_bit(&srcdev->alloc_state, start,
811 &found_start, &found_end,
812 CHUNK_ALLOCATED, &cached_state)) {
813 ret = btrfs_set_extent_bit(&tgtdev->alloc_state, found_start,
814 found_end, CHUNK_ALLOCATED, NULL);
815 if (ret)
816 break;
817 start = found_end + 1;
818 }
819
820 btrfs_free_extent_state(cached_state);
821 return ret;
822 }
823
btrfs_dev_replace_update_device_in_mapping_tree(struct btrfs_fs_info * fs_info,struct btrfs_device * srcdev,struct btrfs_device * tgtdev)824 static void btrfs_dev_replace_update_device_in_mapping_tree(
825 struct btrfs_fs_info *fs_info,
826 struct btrfs_device *srcdev,
827 struct btrfs_device *tgtdev)
828 {
829 struct rb_node *node;
830
831 /*
832 * The chunk mutex must be held so that no new chunks can be created
833 * while we are updating existing chunks. This guarantees we don't miss
834 * any new chunk that gets created for a range that falls before the
835 * range of the last chunk we processed.
836 */
837 lockdep_assert_held(&fs_info->chunk_mutex);
838
839 write_lock(&fs_info->mapping_tree_lock);
840 node = rb_first_cached(&fs_info->mapping_tree);
841 while (node) {
842 struct rb_node *next = rb_next(node);
843 struct btrfs_chunk_map *map;
844 u64 next_start;
845
846 map = rb_entry(node, struct btrfs_chunk_map, rb_node);
847 next_start = map->start + map->chunk_len;
848
849 for (int i = 0; i < map->num_stripes; i++)
850 if (srcdev == map->stripes[i].dev)
851 map->stripes[i].dev = tgtdev;
852
853 if (cond_resched_rwlock_write(&fs_info->mapping_tree_lock)) {
854 map = btrfs_find_chunk_map_nolock(fs_info, next_start, U64_MAX);
855 if (!map)
856 break;
857 node = &map->rb_node;
858 /*
859 * Drop the lookup reference since we are holding the
860 * lock in write mode and no one can remove the chunk
861 * map from the tree and drop its tree reference.
862 */
863 btrfs_free_chunk_map(map);
864 } else {
865 node = next;
866 }
867 }
868 write_unlock(&fs_info->mapping_tree_lock);
869 }
870
btrfs_dev_replace_finishing(struct btrfs_fs_info * fs_info,int scrub_ret)871 static int btrfs_dev_replace_finishing(struct btrfs_fs_info *fs_info,
872 int scrub_ret)
873 {
874 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
875 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
876 struct btrfs_device *tgt_device;
877 struct btrfs_device *src_device;
878 struct btrfs_root *root = fs_info->tree_root;
879 u8 uuid_tmp[BTRFS_UUID_SIZE];
880 struct btrfs_trans_handle *trans;
881 int ret = 0;
882
883 /* don't allow cancel or unmount to disturb the finishing procedure */
884 mutex_lock(&dev_replace->lock_finishing_cancel_unmount);
885
886 down_read(&dev_replace->rwsem);
887 /* was the operation canceled, or is it finished? */
888 if (dev_replace->replace_state !=
889 BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED) {
890 up_read(&dev_replace->rwsem);
891 mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
892 return 0;
893 }
894
895 tgt_device = dev_replace->tgtdev;
896 src_device = dev_replace->srcdev;
897 up_read(&dev_replace->rwsem);
898
899 /*
900 * flush all outstanding I/O and inode extent mappings before the
901 * copy operation is declared as being finished
902 */
903 ret = btrfs_start_delalloc_roots(fs_info, LONG_MAX, false);
904 if (ret) {
905 /* Stays started/resumable; keep both denied. */
906 mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
907 return ret;
908 }
909 btrfs_wait_ordered_roots(fs_info, U64_MAX, NULL);
910
911 /*
912 * We have to use this loop approach because at this point src_device
913 * has to be available for transaction commit to complete, yet new
914 * chunks shouldn't be allocated on the device.
915 */
916 while (1) {
917 trans = btrfs_start_transaction(root, 0);
918 if (IS_ERR(trans)) {
919 /* Stays started/resumable; keep both denied. */
920 mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
921 return PTR_ERR(trans);
922 }
923 ret = btrfs_commit_transaction(trans);
924 WARN_ON(ret);
925
926 /* Prevent write_all_supers() during the finishing procedure */
927 mutex_lock(&fs_devices->device_list_mutex);
928 /* Prevent new chunks being allocated on the source device */
929 mutex_lock(&fs_info->chunk_mutex);
930
931 if (!list_empty(&src_device->post_commit_list)) {
932 mutex_unlock(&fs_devices->device_list_mutex);
933 mutex_unlock(&fs_info->chunk_mutex);
934 } else {
935 break;
936 }
937 }
938
939 down_write(&dev_replace->rwsem);
940 dev_replace->replace_state =
941 scrub_ret ? BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED
942 : BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED;
943 dev_replace->tgtdev = NULL;
944 dev_replace->srcdev = NULL;
945 dev_replace->time_stopped = ktime_get_real_seconds();
946 dev_replace->item_needs_writeback = 1;
947
948 /*
949 * Update allocation state in the new device and replace the old device
950 * with the new one in the mapping tree.
951 */
952 if (!scrub_ret) {
953 scrub_ret = btrfs_set_target_alloc_state(src_device, tgt_device);
954 if (scrub_ret)
955 goto error;
956 btrfs_dev_replace_update_device_in_mapping_tree(fs_info,
957 src_device,
958 tgt_device);
959 } else {
960 if (scrub_ret != -ECANCELED)
961 btrfs_err(fs_info,
962 "btrfs_scrub_dev(%s, %llu, %s) failed %d",
963 btrfs_dev_name(src_device),
964 src_device->devid,
965 btrfs_dev_name(tgt_device), scrub_ret);
966 error:
967 up_write(&dev_replace->rwsem);
968 mutex_unlock(&fs_info->chunk_mutex);
969 mutex_unlock(&fs_devices->device_list_mutex);
970 btrfs_rm_dev_replace_blocked(fs_info);
971 if (tgt_device)
972 btrfs_destroy_dev_replace_tgtdev(tgt_device, true);
973 /* The source stays a member; re-allow freezing it. */
974 if (src_device->bdev)
975 bdev_allow_freeze(src_device->bdev);
976 btrfs_rm_dev_replace_unblocked(fs_info);
977 mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
978
979 return scrub_ret;
980 }
981
982 btrfs_info(fs_info,
983 "dev_replace from %s (devid %llu) to %s finished",
984 btrfs_dev_name(src_device),
985 src_device->devid,
986 btrfs_dev_name(tgt_device));
987 clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &tgt_device->dev_state);
988 tgt_device->devid = src_device->devid;
989 src_device->devid = BTRFS_DEV_REPLACE_DEVID;
990 memcpy(uuid_tmp, tgt_device->uuid, sizeof(uuid_tmp));
991 memcpy(tgt_device->uuid, src_device->uuid, sizeof(tgt_device->uuid));
992 memcpy(src_device->uuid, uuid_tmp, sizeof(src_device->uuid));
993 btrfs_device_set_total_bytes(tgt_device, src_device->total_bytes);
994 btrfs_device_set_disk_total_bytes(tgt_device,
995 src_device->disk_total_bytes);
996 btrfs_device_set_bytes_used(tgt_device, src_device->bytes_used);
997 tgt_device->commit_bytes_used = src_device->bytes_used;
998
999 btrfs_assign_next_active_device(src_device, tgt_device);
1000
1001 list_add(&tgt_device->dev_alloc_list, &fs_devices->alloc_list);
1002 fs_devices->rw_devices++;
1003
1004 dev_replace->replace_task = NULL;
1005 up_write(&dev_replace->rwsem);
1006 btrfs_rm_dev_replace_blocked(fs_info);
1007
1008 btrfs_rm_dev_replace_remove_srcdev(src_device);
1009
1010 btrfs_rm_dev_replace_unblocked(fs_info);
1011
1012 /*
1013 * Increment dev_stats_ccnt so that btrfs_run_dev_stats() will
1014 * update on-disk dev stats value during commit transaction
1015 */
1016 atomic_inc(&tgt_device->dev_stats_ccnt);
1017
1018 /*
1019 * this is again a consistent state where no dev_replace procedure
1020 * is running, the target device is part of the filesystem, the
1021 * source device is not part of the filesystem anymore and its 1st
1022 * superblock is scratched out so that it is no longer marked to
1023 * belong to this filesystem.
1024 */
1025 mutex_unlock(&fs_info->chunk_mutex);
1026 mutex_unlock(&fs_devices->device_list_mutex);
1027
1028 /* replace the sysfs entry */
1029 btrfs_sysfs_remove_device(src_device);
1030 btrfs_sysfs_update_devid(tgt_device);
1031 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &src_device->dev_state))
1032 btrfs_scratch_superblocks(fs_info, src_device);
1033
1034 /* write back the superblocks */
1035 trans = btrfs_start_transaction(root, 0);
1036 if (!IS_ERR(trans)) {
1037 /*
1038 * Ignore any error here, if we failed to remove the DEV_STATS
1039 * item for devid 0, it's not a big deal. We have other ways
1040 * to address it.
1041 */
1042 btrfs_remove_dev_stat_item(trans, BTRFS_DEV_REPLACE_DEVID);
1043 btrfs_commit_transaction(trans);
1044 }
1045
1046 mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
1047
1048 /* The target is now a member; the source is freed (allow + release). */
1049 bdev_allow_freeze(tgt_device->bdev);
1050 btrfs_rm_dev_replace_free_srcdev(src_device);
1051
1052 return 0;
1053 }
1054
1055 /*
1056 * Read progress of device replace status according to the state and last
1057 * stored position. The value format is the same as for
1058 * btrfs_dev_replace::progress_1000
1059 */
btrfs_dev_replace_progress(struct btrfs_fs_info * fs_info)1060 static u64 btrfs_dev_replace_progress(struct btrfs_fs_info *fs_info)
1061 {
1062 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
1063 u64 ret = 0;
1064
1065 switch (dev_replace->replace_state) {
1066 case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
1067 case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
1068 ret = 0;
1069 break;
1070 case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
1071 ret = 1000;
1072 break;
1073 case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
1074 case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
1075 ret = div64_u64(dev_replace->cursor_left,
1076 div_u64(btrfs_device_get_total_bytes(
1077 dev_replace->srcdev), 1000));
1078 break;
1079 }
1080
1081 return ret;
1082 }
1083
btrfs_dev_replace_status(struct btrfs_fs_info * fs_info,struct btrfs_ioctl_dev_replace_args * args)1084 void btrfs_dev_replace_status(struct btrfs_fs_info *fs_info,
1085 struct btrfs_ioctl_dev_replace_args *args)
1086 {
1087 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
1088
1089 down_read(&dev_replace->rwsem);
1090 /* even if !dev_replace_is_valid, the values are good enough for
1091 * the replace_status ioctl */
1092 args->result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR;
1093 args->status.replace_state = dev_replace->replace_state;
1094 args->status.time_started = dev_replace->time_started;
1095 args->status.time_stopped = dev_replace->time_stopped;
1096 args->status.num_write_errors =
1097 atomic64_read(&dev_replace->num_write_errors);
1098 args->status.num_uncorrectable_read_errors =
1099 atomic64_read(&dev_replace->num_uncorrectable_read_errors);
1100 args->status.progress_1000 = btrfs_dev_replace_progress(fs_info);
1101 up_read(&dev_replace->rwsem);
1102 }
1103
btrfs_dev_replace_cancel(struct btrfs_fs_info * fs_info)1104 int btrfs_dev_replace_cancel(struct btrfs_fs_info *fs_info)
1105 {
1106 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
1107 struct btrfs_device *tgt_device = NULL;
1108 struct btrfs_device *src_device = NULL;
1109 struct btrfs_trans_handle *trans;
1110 struct btrfs_root *root = fs_info->tree_root;
1111 int result;
1112 int ret;
1113
1114 if (sb_rdonly(fs_info->sb))
1115 return -EROFS;
1116
1117 mutex_lock(&dev_replace->lock_finishing_cancel_unmount);
1118 down_write(&dev_replace->rwsem);
1119 switch (dev_replace->replace_state) {
1120 case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
1121 case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
1122 case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
1123 result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NOT_STARTED;
1124 up_write(&dev_replace->rwsem);
1125 break;
1126 case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
1127 tgt_device = dev_replace->tgtdev;
1128 src_device = dev_replace->srcdev;
1129 up_write(&dev_replace->rwsem);
1130 ret = btrfs_scrub_cancel(fs_info);
1131 if (ret < 0) {
1132 result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NOT_STARTED;
1133 } else {
1134 result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR;
1135 /*
1136 * btrfs_dev_replace_finishing() will handle the
1137 * cleanup part
1138 */
1139 btrfs_info(fs_info,
1140 "dev_replace from %s (devid %llu) to %s canceled",
1141 btrfs_dev_name(src_device), src_device->devid,
1142 btrfs_dev_name(tgt_device));
1143 }
1144 break;
1145 case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
1146 /*
1147 * Scrub doing the replace isn't running so we need to do the
1148 * cleanup step of btrfs_dev_replace_finishing() here
1149 */
1150 result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR;
1151 tgt_device = dev_replace->tgtdev;
1152 src_device = dev_replace->srcdev;
1153 dev_replace->tgtdev = NULL;
1154 dev_replace->srcdev = NULL;
1155 dev_replace->replace_state =
1156 BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED;
1157 dev_replace->time_stopped = ktime_get_real_seconds();
1158 dev_replace->item_needs_writeback = 1;
1159
1160 up_write(&dev_replace->rwsem);
1161
1162 /* Scrub for replace must not be running in suspended state */
1163 btrfs_scrub_cancel(fs_info);
1164
1165 trans = btrfs_start_transaction(root, 0);
1166 if (IS_ERR(trans)) {
1167 mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
1168 return PTR_ERR(trans);
1169 }
1170 ret = btrfs_commit_transaction(trans);
1171 WARN_ON(ret);
1172
1173 btrfs_info(fs_info,
1174 "suspended dev_replace from %s (devid %llu) to %s canceled",
1175 btrfs_dev_name(src_device), src_device->devid,
1176 btrfs_dev_name(tgt_device));
1177
1178 /* A suspended replace never re-denied freezing; do not allow. */
1179 if (tgt_device)
1180 btrfs_destroy_dev_replace_tgtdev(tgt_device, false);
1181 break;
1182 default:
1183 up_write(&dev_replace->rwsem);
1184 result = -EINVAL;
1185 }
1186
1187 mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
1188 return result;
1189 }
1190
btrfs_dev_replace_suspend_for_unmount(struct btrfs_fs_info * fs_info)1191 void btrfs_dev_replace_suspend_for_unmount(struct btrfs_fs_info *fs_info)
1192 {
1193 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
1194
1195 mutex_lock(&dev_replace->lock_finishing_cancel_unmount);
1196 down_write(&dev_replace->rwsem);
1197
1198 switch (dev_replace->replace_state) {
1199 case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
1200 case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
1201 case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
1202 case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
1203 break;
1204 case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
1205 dev_replace->replace_state =
1206 BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED;
1207 dev_replace->time_stopped = ktime_get_real_seconds();
1208 dev_replace->item_needs_writeback = 1;
1209 btrfs_info(fs_info, "suspending dev_replace for unmount");
1210 /* Reopened freezable next mount; resume re-denies. */
1211 if (dev_replace->srcdev && dev_replace->srcdev->bdev)
1212 bdev_allow_freeze(dev_replace->srcdev->bdev);
1213 if (dev_replace->tgtdev && dev_replace->tgtdev->bdev)
1214 bdev_allow_freeze(dev_replace->tgtdev->bdev);
1215 break;
1216 }
1217
1218 up_write(&dev_replace->rwsem);
1219 mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
1220 }
1221
1222 /* resume dev_replace procedure that was interrupted by unmount */
btrfs_resume_dev_replace_async(struct btrfs_fs_info * fs_info)1223 int btrfs_resume_dev_replace_async(struct btrfs_fs_info *fs_info)
1224 {
1225 struct task_struct *task;
1226 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
1227 int ret = 0;
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 /* Re-deny for the resumed replace; stay suspended if frozen now. */
1272 if (dev_replace->srcdev->bdev &&
1273 bdev_deny_freeze(dev_replace->srcdev->bdev))
1274 goto suspend;
1275 if (bdev_deny_freeze(dev_replace->tgtdev->bdev)) {
1276 if (dev_replace->srcdev->bdev)
1277 bdev_allow_freeze(dev_replace->srcdev->bdev);
1278 goto suspend;
1279 }
1280
1281 task = kthread_run(btrfs_dev_replace_kthread, fs_info, "btrfs-devrepl");
1282 if (IS_ERR(task)) {
1283 bdev_allow_freeze(dev_replace->tgtdev->bdev);
1284 if (dev_replace->srcdev->bdev)
1285 bdev_allow_freeze(dev_replace->srcdev->bdev);
1286 /* Undo the deny and suspend, but still fail the mount. */
1287 ret = PTR_ERR(task);
1288 goto suspend;
1289 }
1290 return 0;
1291
1292 suspend:
1293 btrfs_exclop_finish(fs_info);
1294 down_write(&dev_replace->rwsem);
1295 dev_replace->replace_state = BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED;
1296 up_write(&dev_replace->rwsem);
1297 return ret;
1298 }
1299
btrfs_dev_replace_kthread(void * data)1300 static int btrfs_dev_replace_kthread(void *data)
1301 {
1302 struct btrfs_fs_info *fs_info = data;
1303 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
1304 u64 progress;
1305 int ret;
1306
1307 progress = btrfs_dev_replace_progress(fs_info);
1308 progress = div_u64(progress, 10);
1309 btrfs_info(fs_info,
1310 "continuing dev_replace from %s (devid %llu) to target %s @%u%%",
1311 btrfs_dev_name(dev_replace->srcdev),
1312 dev_replace->srcdev->devid,
1313 btrfs_dev_name(dev_replace->tgtdev),
1314 (unsigned int)progress);
1315
1316 ret = btrfs_scrub_dev(fs_info, dev_replace->srcdev->devid,
1317 dev_replace->committed_cursor_left,
1318 btrfs_device_get_total_bytes(dev_replace->srcdev),
1319 &dev_replace->scrub_progress, false, true);
1320 ret = btrfs_dev_replace_finishing(fs_info, ret);
1321 WARN_ON(ret && ret != -ECANCELED);
1322
1323 btrfs_exclop_finish(fs_info);
1324 return 0;
1325 }
1326
btrfs_dev_replace_is_ongoing(struct btrfs_dev_replace * dev_replace)1327 bool __pure btrfs_dev_replace_is_ongoing(struct btrfs_dev_replace *dev_replace)
1328 {
1329 if (!dev_replace->is_valid)
1330 return false;
1331
1332 switch (dev_replace->replace_state) {
1333 case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
1334 case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
1335 case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
1336 return false;
1337 case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
1338 case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
1339 /*
1340 * return true even if tgtdev is missing (this is
1341 * something that can happen if the dev_replace
1342 * procedure is suspended by an umount and then
1343 * the tgtdev is missing (or "btrfs dev scan") was
1344 * not called and the filesystem is remounted
1345 * in degraded state. This does not stop the
1346 * dev_replace procedure. It needs to be canceled
1347 * manually if the cancellation is wanted.
1348 */
1349 break;
1350 }
1351 return true;
1352 }
1353
btrfs_bio_counter_sub(struct btrfs_fs_info * fs_info,s64 amount)1354 void btrfs_bio_counter_sub(struct btrfs_fs_info *fs_info, s64 amount)
1355 {
1356 percpu_counter_sub(&fs_info->dev_replace.bio_counter, amount);
1357 cond_wake_up_nomb(&fs_info->dev_replace.replace_wait);
1358 }
1359
btrfs_bio_counter_inc_blocked(struct btrfs_fs_info * fs_info)1360 void btrfs_bio_counter_inc_blocked(struct btrfs_fs_info *fs_info)
1361 {
1362 while (1) {
1363 percpu_counter_inc(&fs_info->dev_replace.bio_counter);
1364 if (likely(!test_bit(BTRFS_FS_STATE_DEV_REPLACING,
1365 &fs_info->fs_state)))
1366 break;
1367
1368 btrfs_bio_counter_dec(fs_info);
1369 wait_event(fs_info->dev_replace.replace_wait,
1370 !test_bit(BTRFS_FS_STATE_DEV_REPLACING,
1371 &fs_info->fs_state));
1372 }
1373 }
1374