blob: dc3481014f16f17d45dab7a6944bdb307f37ee8c [file] [log] [blame]
David Sterbac1d7c512018-04-03 19:23:33 +02001// SPDX-License-Identifier: GPL-2.0
Stefan Behrense93c89c2012-11-05 17:33:06 +01002/*
3 * Copyright (C) STRATO AG 2012. All rights reserved.
Stefan Behrense93c89c2012-11-05 17:33:06 +01004 */
David Sterbac1d7c512018-04-03 19:23:33 +02005
Stefan Behrense93c89c2012-11-05 17:33:06 +01006#include <linux/sched.h>
7#include <linux/bio.h>
8#include <linux/slab.h>
Stefan Behrense93c89c2012-11-05 17:33:06 +01009#include <linux/blkdev.h>
Stefan Behrense93c89c2012-11-05 17:33:06 +010010#include <linux/kthread.h>
11#include <linux/math64.h>
David Sterba602cbe92019-08-21 18:48:25 +020012#include "misc.h"
Stefan Behrense93c89c2012-11-05 17:33:06 +010013#include "ctree.h"
14#include "extent_map.h"
15#include "disk-io.h"
16#include "transaction.h"
17#include "print-tree.h"
18#include "volumes.h"
19#include "async-thread.h"
20#include "check-integrity.h"
21#include "rcu-string.h"
22#include "dev-replace.h"
Anand Jain49c6f732014-06-03 11:36:02 +080023#include "sysfs.h"
Stefan Behrense93c89c2012-11-05 17:33:06 +010024
Qu Wenruo30b36882020-01-23 15:44:50 +080025/*
26 * Device replace overview
27 *
28 * [Objective]
29 * To copy all extents (both new and on-disk) from source device to target
30 * device, while still keeping the filesystem read-write.
31 *
32 * [Method]
33 * There are two main methods involved:
34 *
35 * - Write duplication
36 *
37 * All new writes will be written to both target and source devices, so even
38 * if replace gets canceled, sources device still contans up-to-date data.
39 *
40 * Location: handle_ops_on_dev_replace() from __btrfs_map_block()
41 * Start: btrfs_dev_replace_start()
42 * End: btrfs_dev_replace_finishing()
43 * Content: Latest data/metadata
44 *
45 * - Copy existing extents
46 *
47 * This happens by re-using scrub facility, as scrub also iterates through
48 * existing extents from commit root.
49 *
50 * Location: scrub_write_block_to_dev_replace() from
51 * scrub_block_complete()
52 * Content: Data/meta from commit root.
53 *
54 * Due to the content difference, we need to avoid nocow write when dev-replace
55 * is happening. This is done by marking the block group read-only and waiting
56 * for NOCOW writes.
57 *
58 * After replace is done, the finishing part is done by swapping the target and
59 * source devices.
60 *
61 * Location: btrfs_dev_replace_update_device_in_mapping_tree() from
62 * btrfs_dev_replace_finishing()
63 */
64
Stefan Behrense93c89c2012-11-05 17:33:06 +010065static int btrfs_dev_replace_finishing(struct btrfs_fs_info *fs_info,
66 int scrub_ret);
Stefan Behrense93c89c2012-11-05 17:33:06 +010067static int btrfs_dev_replace_kthread(void *data);
Stefan Behrense93c89c2012-11-05 17:33:06 +010068
69int btrfs_init_dev_replace(struct btrfs_fs_info *fs_info)
70{
71 struct btrfs_key key;
72 struct btrfs_root *dev_root = fs_info->dev_root;
73 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
74 struct extent_buffer *eb;
75 int slot;
76 int ret = 0;
77 struct btrfs_path *path = NULL;
78 int item_size;
79 struct btrfs_dev_replace_item *ptr;
80 u64 src_devid;
81
82 path = btrfs_alloc_path();
83 if (!path) {
84 ret = -ENOMEM;
85 goto out;
86 }
87
88 key.objectid = 0;
89 key.type = BTRFS_DEV_REPLACE_KEY;
90 key.offset = 0;
91 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
92 if (ret) {
93no_valid_dev_replace_entry_found:
Anand Jaincf89af12020-10-30 06:53:56 +080094 /*
95 * We don't have a replace item or it's corrupted. If there is
96 * a replace target, fail the mount.
97 */
98 if (btrfs_find_device(fs_info->fs_devices,
Anand Jainb2598ed2020-11-03 13:49:43 +080099 BTRFS_DEV_REPLACE_DEVID, NULL, NULL)) {
Anand Jaincf89af12020-10-30 06:53:56 +0800100 btrfs_err(fs_info,
101 "found replace target device without a valid replace item");
102 ret = -EUCLEAN;
103 goto out;
104 }
Stefan Behrense93c89c2012-11-05 17:33:06 +0100105 ret = 0;
106 dev_replace->replace_state =
Anand Jain27e022a2019-08-08 12:32:44 +0800107 BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED;
Stefan Behrense93c89c2012-11-05 17:33:06 +0100108 dev_replace->cont_reading_from_srcdev_mode =
109 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_ALWAYS;
Stefan Behrense93c89c2012-11-05 17:33:06 +0100110 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 goto out;
123 }
124 slot = path->slots[0];
125 eb = path->nodes[0];
126 item_size = btrfs_item_size_nr(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)) {
Frank Holtonefe120a2013-12-20 11:37:06 -0500130 btrfs_warn(fs_info,
131 "dev_replace entry found has unexpected size, ignore entry");
Stefan Behrense93c89c2012-11-05 17:33:06 +0100132 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:
Anand Jaincf89af12020-10-30 06:53:56 +0800157 /*
158 * We don't have an active replace item but if there is a
159 * replace target, fail the mount.
160 */
161 if (btrfs_find_device(fs_info->fs_devices,
Anand Jainb2598ed2020-11-03 13:49:43 +0800162 BTRFS_DEV_REPLACE_DEVID, NULL, NULL)) {
Anand Jaincf89af12020-10-30 06:53:56 +0800163 btrfs_err(fs_info,
164 "replace devid present without an active replace item");
165 ret = -EUCLEAN;
166 } else {
167 dev_replace->srcdev = NULL;
168 dev_replace->tgtdev = NULL;
169 }
Stefan Behrense93c89c2012-11-05 17:33:06 +0100170 break;
171 case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
172 case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
Anand Jaine4319cd2019-01-17 23:32:31 +0800173 dev_replace->srcdev = btrfs_find_device(fs_info->fs_devices,
Anand Jainb2598ed2020-11-03 13:49:43 +0800174 src_devid, NULL, NULL);
Anand Jaine4319cd2019-01-17 23:32:31 +0800175 dev_replace->tgtdev = btrfs_find_device(fs_info->fs_devices,
Stefan Behrense93c89c2012-11-05 17:33:06 +0100176 BTRFS_DEV_REPLACE_DEVID,
Anand Jainb2598ed2020-11-03 13:49:43 +0800177 NULL, NULL);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100178 /*
179 * allow 'btrfs dev replace_cancel' if src/tgt device is
180 * missing
181 */
182 if (!dev_replace->srcdev &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400183 !btrfs_test_opt(fs_info, DEGRADED)) {
Stefan Behrense93c89c2012-11-05 17:33:06 +0100184 ret = -EIO;
Frank Holtonefe120a2013-12-20 11:37:06 -0500185 btrfs_warn(fs_info,
186 "cannot mount because device replace operation is ongoing and");
187 btrfs_warn(fs_info,
188 "srcdev (devid %llu) is missing, need to run 'btrfs dev scan'?",
189 src_devid);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100190 }
191 if (!dev_replace->tgtdev &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400192 !btrfs_test_opt(fs_info, DEGRADED)) {
Stefan Behrense93c89c2012-11-05 17:33:06 +0100193 ret = -EIO;
Frank Holtonefe120a2013-12-20 11:37:06 -0500194 btrfs_warn(fs_info,
195 "cannot mount because device replace operation is ongoing and");
196 btrfs_warn(fs_info,
197 "tgtdev (devid %llu) is missing, need to run 'btrfs dev scan'?",
Geert Uytterhoeven6e71c472013-08-20 13:20:08 +0200198 BTRFS_DEV_REPLACE_DEVID);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100199 }
200 if (dev_replace->tgtdev) {
201 if (dev_replace->srcdev) {
202 dev_replace->tgtdev->total_bytes =
203 dev_replace->srcdev->total_bytes;
204 dev_replace->tgtdev->disk_total_bytes =
205 dev_replace->srcdev->disk_total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +0800206 dev_replace->tgtdev->commit_total_bytes =
207 dev_replace->srcdev->commit_total_bytes;
Stefan Behrense93c89c2012-11-05 17:33:06 +0100208 dev_replace->tgtdev->bytes_used =
209 dev_replace->srcdev->bytes_used;
Miao Xiece7213c2014-09-03 21:35:34 +0800210 dev_replace->tgtdev->commit_bytes_used =
211 dev_replace->srcdev->commit_bytes_used;
Stefan Behrense93c89c2012-11-05 17:33:06 +0100212 }
Anand Jain401e29c2017-12-04 12:54:55 +0800213 set_bit(BTRFS_DEV_STATE_REPLACE_TGT,
214 &dev_replace->tgtdev->dev_state);
Anand Jain15fc1282018-02-12 23:36:25 +0800215
216 WARN_ON(fs_info->fs_devices->rw_devices == 0);
217 dev_replace->tgtdev->io_width = fs_info->sectorsize;
218 dev_replace->tgtdev->io_align = fs_info->sectorsize;
219 dev_replace->tgtdev->sector_size = fs_info->sectorsize;
220 dev_replace->tgtdev->fs_info = fs_info;
221 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
222 &dev_replace->tgtdev->dev_state);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100223 }
224 break;
225 }
226
227out:
Tsutomu Itoh527afb42015-08-19 14:55:00 +0900228 btrfs_free_path(path);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100229 return ret;
230}
231
232/*
David Sterbad48f39d2018-03-20 16:09:48 +0100233 * Initialize a new device for device replace target from a given source dev
234 * and path.
235 *
236 * Return 0 and new device in @device_out, otherwise return < 0
237 */
238static int btrfs_init_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
239 const char *device_path,
240 struct btrfs_device *srcdev,
241 struct btrfs_device **device_out)
242{
243 struct btrfs_device *device;
244 struct block_device *bdev;
David Sterbad48f39d2018-03-20 16:09:48 +0100245 struct rcu_string *name;
246 u64 devid = BTRFS_DEV_REPLACE_DEVID;
247 int ret = 0;
248
249 *device_out = NULL;
Anand Jainc6a5d952020-09-05 01:34:22 +0800250 if (srcdev->fs_devices->seeding) {
David Sterbad48f39d2018-03-20 16:09:48 +0100251 btrfs_err(fs_info, "the filesystem is a seed filesystem!");
252 return -EINVAL;
253 }
254
255 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
256 fs_info->bdev_holder);
257 if (IS_ERR(bdev)) {
258 btrfs_err(fs_info, "target device %s is invalid!", device_path);
259 return PTR_ERR(bdev);
260 }
261
Nikolay Borisovddb93782019-05-14 13:54:38 +0300262 sync_blockdev(bdev);
David Sterbad48f39d2018-03-20 16:09:48 +0100263
Anand Jain18887092020-09-05 01:34:32 +0800264 list_for_each_entry(device, &fs_info->fs_devices->devices, dev_list) {
David Sterbad48f39d2018-03-20 16:09:48 +0100265 if (device->bdev == bdev) {
266 btrfs_err(fs_info,
267 "target device is in the filesystem!");
268 ret = -EEXIST;
269 goto error;
270 }
271 }
272
273
274 if (i_size_read(bdev->bd_inode) <
275 btrfs_device_get_total_bytes(srcdev)) {
276 btrfs_err(fs_info,
277 "target device is smaller than source device!");
278 ret = -EINVAL;
279 goto error;
280 }
281
282
283 device = btrfs_alloc_device(NULL, &devid, NULL);
284 if (IS_ERR(device)) {
285 ret = PTR_ERR(device);
286 goto error;
287 }
288
289 name = rcu_string_strdup(device_path, GFP_KERNEL);
290 if (!name) {
291 btrfs_free_device(device);
292 ret = -ENOMEM;
293 goto error;
294 }
295 rcu_assign_pointer(device->name, name);
296
David Sterbad48f39d2018-03-20 16:09:48 +0100297 set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
298 device->generation = 0;
299 device->io_width = fs_info->sectorsize;
300 device->io_align = fs_info->sectorsize;
301 device->sector_size = fs_info->sectorsize;
302 device->total_bytes = btrfs_device_get_total_bytes(srcdev);
303 device->disk_total_bytes = btrfs_device_get_disk_total_bytes(srcdev);
304 device->bytes_used = btrfs_device_get_bytes_used(srcdev);
305 device->commit_total_bytes = srcdev->commit_total_bytes;
306 device->commit_bytes_used = device->bytes_used;
307 device->fs_info = fs_info;
308 device->bdev = bdev;
309 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
310 set_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
311 device->mode = FMODE_EXCL;
312 device->dev_stats_valid = 1;
313 set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE);
314 device->fs_devices = fs_info->fs_devices;
Nikolay Borisovb0d9e1e2019-05-14 13:54:39 +0300315
316 mutex_lock(&fs_info->fs_devices->device_list_mutex);
David Sterbad48f39d2018-03-20 16:09:48 +0100317 list_add(&device->dev_list, &fs_info->fs_devices->devices);
318 fs_info->fs_devices->num_devices++;
319 fs_info->fs_devices->open_devices++;
320 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
321
322 *device_out = device;
323 return 0;
324
325error:
326 blkdev_put(bdev, FMODE_EXCL);
327 return ret;
328}
329
330/*
Stefan Behrense93c89c2012-11-05 17:33:06 +0100331 * called from commit_transaction. Writes changed device replace state to
332 * disk.
333 */
David Sterba2b584c62019-03-20 16:51:44 +0100334int btrfs_run_dev_replace(struct btrfs_trans_handle *trans)
Stefan Behrense93c89c2012-11-05 17:33:06 +0100335{
David Sterba2b584c62019-03-20 16:51:44 +0100336 struct btrfs_fs_info *fs_info = trans->fs_info;
Stefan Behrense93c89c2012-11-05 17:33:06 +0100337 int ret;
338 struct btrfs_root *dev_root = fs_info->dev_root;
339 struct btrfs_path *path;
340 struct btrfs_key key;
341 struct extent_buffer *eb;
342 struct btrfs_dev_replace_item *ptr;
343 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
344
David Sterbacb5583d2018-09-07 16:11:23 +0200345 down_read(&dev_replace->rwsem);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100346 if (!dev_replace->is_valid ||
347 !dev_replace->item_needs_writeback) {
David Sterbacb5583d2018-09-07 16:11:23 +0200348 up_read(&dev_replace->rwsem);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100349 return 0;
350 }
David Sterbacb5583d2018-09-07 16:11:23 +0200351 up_read(&dev_replace->rwsem);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100352
353 key.objectid = 0;
354 key.type = BTRFS_DEV_REPLACE_KEY;
355 key.offset = 0;
356
357 path = btrfs_alloc_path();
358 if (!path) {
359 ret = -ENOMEM;
360 goto out;
361 }
362 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
363 if (ret < 0) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400364 btrfs_warn(fs_info,
365 "error %d while searching for dev_replace item!",
366 ret);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100367 goto out;
368 }
369
370 if (ret == 0 &&
371 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
372 /*
373 * need to delete old one and insert a new one.
374 * Since no attempt is made to recover any old state, if the
375 * dev_replace state is 'running', the data on the target
376 * drive is lost.
377 * It would be possible to recover the state: just make sure
378 * that the beginning of the item is never changed and always
379 * contains all the essential information. Then read this
380 * minimal set of information and use it as a base for the
381 * new state.
382 */
383 ret = btrfs_del_item(trans, dev_root, path);
384 if (ret != 0) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400385 btrfs_warn(fs_info,
386 "delete too small dev_replace item failed %d!",
387 ret);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100388 goto out;
389 }
390 ret = 1;
391 }
392
393 if (ret == 1) {
394 /* need to insert a new item */
395 btrfs_release_path(path);
396 ret = btrfs_insert_empty_item(trans, dev_root, path,
397 &key, sizeof(*ptr));
398 if (ret < 0) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400399 btrfs_warn(fs_info,
400 "insert dev_replace item failed %d!", ret);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100401 goto out;
402 }
403 }
404
405 eb = path->nodes[0];
406 ptr = btrfs_item_ptr(eb, path->slots[0],
407 struct btrfs_dev_replace_item);
408
David Sterbacb5583d2018-09-07 16:11:23 +0200409 down_write(&dev_replace->rwsem);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100410 if (dev_replace->srcdev)
411 btrfs_set_dev_replace_src_devid(eb, ptr,
412 dev_replace->srcdev->devid);
413 else
414 btrfs_set_dev_replace_src_devid(eb, ptr, (u64)-1);
415 btrfs_set_dev_replace_cont_reading_from_srcdev_mode(eb, ptr,
416 dev_replace->cont_reading_from_srcdev_mode);
417 btrfs_set_dev_replace_replace_state(eb, ptr,
418 dev_replace->replace_state);
419 btrfs_set_dev_replace_time_started(eb, ptr, dev_replace->time_started);
420 btrfs_set_dev_replace_time_stopped(eb, ptr, dev_replace->time_stopped);
421 btrfs_set_dev_replace_num_write_errors(eb, ptr,
422 atomic64_read(&dev_replace->num_write_errors));
423 btrfs_set_dev_replace_num_uncorrectable_read_errors(eb, ptr,
424 atomic64_read(&dev_replace->num_uncorrectable_read_errors));
425 dev_replace->cursor_left_last_write_of_item =
426 dev_replace->cursor_left;
427 btrfs_set_dev_replace_cursor_left(eb, ptr,
428 dev_replace->cursor_left_last_write_of_item);
429 btrfs_set_dev_replace_cursor_right(eb, ptr,
430 dev_replace->cursor_right);
431 dev_replace->item_needs_writeback = 0;
David Sterbacb5583d2018-09-07 16:11:23 +0200432 up_write(&dev_replace->rwsem);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100433
434 btrfs_mark_buffer_dirty(eb);
435
436out:
437 btrfs_free_path(path);
438
439 return ret;
440}
441
Anand Jain3c958bd2017-11-28 10:43:10 +0800442static char* btrfs_dev_name(struct btrfs_device *device)
443{
Anand Jainacf18c52018-02-24 19:43:56 +0800444 if (!device || test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
Anand Jain3c958bd2017-11-28 10:43:10 +0800445 return "<missing disk>";
446 else
447 return rcu_str_deref(device->name);
448}
449
Anand Jain54862d62018-11-11 22:22:16 +0800450static int btrfs_dev_replace_start(struct btrfs_fs_info *fs_info,
David Sterbada353f62017-02-14 17:55:53 +0100451 const char *tgtdev_name, u64 srcdevid, const char *srcdev_name,
452 int read_src)
Stefan Behrense93c89c2012-11-05 17:33:06 +0100453{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400454 struct btrfs_root *root = fs_info->dev_root;
Stefan Behrense93c89c2012-11-05 17:33:06 +0100455 struct btrfs_trans_handle *trans;
Stefan Behrense93c89c2012-11-05 17:33:06 +0100456 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
457 int ret;
458 struct btrfs_device *tgt_device = NULL;
459 struct btrfs_device *src_device = NULL;
460
Nikolay Borisova27a94c2018-09-03 12:46:14 +0300461 src_device = btrfs_find_device_by_devspec(fs_info, srcdevid,
462 srcdev_name);
463 if (IS_ERR(src_device))
464 return PTR_ERR(src_device);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100465
Omar Sandovaleede2bf2016-11-03 10:28:12 -0700466 if (btrfs_pinned_by_swapfile(fs_info, src_device)) {
467 btrfs_warn_in_rcu(fs_info,
468 "cannot replace device %s (devid %llu) due to active swapfile",
469 btrfs_dev_name(src_device), src_device->devid);
470 return -ETXTBSY;
471 }
472
Anand Jain9e271ae2015-08-14 18:33:02 +0800473 /*
474 * Here we commit the transaction to make sure commit_total_bytes
475 * of all the devices are updated.
476 */
477 trans = btrfs_attach_transaction(root);
478 if (!IS_ERR(trans)) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400479 ret = btrfs_commit_transaction(trans);
Anand Jain9e271ae2015-08-14 18:33:02 +0800480 if (ret)
481 return ret;
482 } else if (PTR_ERR(trans) != -ENOENT) {
483 return PTR_ERR(trans);
484 }
485
Nikolay Borisove1e0eb42019-05-14 13:54:41 +0300486 ret = btrfs_init_dev_replace_tgtdev(fs_info, tgtdev_name,
487 src_device, &tgt_device);
488 if (ret)
489 return ret;
490
David Sterbacb5583d2018-09-07 16:11:23 +0200491 down_write(&dev_replace->rwsem);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100492 switch (dev_replace->replace_state) {
493 case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
494 case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
495 case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
496 break;
497 case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
498 case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
Jeff Mahoney5c061472018-09-06 15:52:17 -0400499 ASSERT(0);
Anand Jainb5255452016-03-24 18:48:14 +0800500 ret = BTRFS_IOCTL_DEV_REPLACE_RESULT_ALREADY_STARTED;
Nikolay Borisovfa194522019-05-14 13:54:42 +0300501 up_write(&dev_replace->rwsem);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100502 goto leave;
503 }
504
Anand Jainb5255452016-03-24 18:48:14 +0800505 dev_replace->cont_reading_from_srcdev_mode = read_src;
Stefan Behrense93c89c2012-11-05 17:33:06 +0100506 dev_replace->srcdev = src_device;
Stefan Behrense93c89c2012-11-05 17:33:06 +0100507 dev_replace->tgtdev = tgt_device;
508
Anand Jainfc23c242016-03-24 18:48:12 +0800509 btrfs_info_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +0200510 "dev_replace from %s (devid %llu) to %s started",
Anand Jain3c958bd2017-11-28 10:43:10 +0800511 btrfs_dev_name(src_device),
Stefan Behrense93c89c2012-11-05 17:33:06 +0100512 src_device->devid,
513 rcu_str_deref(tgt_device->name));
514
Stefan Behrense93c89c2012-11-05 17:33:06 +0100515 /*
516 * from now on, the writes to the srcdev are all duplicated to
517 * go to the tgtdev as well (refer to btrfs_map_block()).
518 */
519 dev_replace->replace_state = BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED;
Allen Paisa9444422018-06-12 17:18:25 +0530520 dev_replace->time_started = ktime_get_real_seconds();
Stefan Behrense93c89c2012-11-05 17:33:06 +0100521 dev_replace->cursor_left = 0;
522 dev_replace->committed_cursor_left = 0;
523 dev_replace->cursor_left_last_write_of_item = 0;
524 dev_replace->cursor_right = 0;
525 dev_replace->is_valid = 1;
526 dev_replace->item_needs_writeback = 1;
Yauhen Kharuzhy7ccefb92016-03-29 14:17:48 -0700527 atomic64_set(&dev_replace->num_write_errors, 0);
528 atomic64_set(&dev_replace->num_uncorrectable_read_errors, 0);
David Sterbacb5583d2018-09-07 16:11:23 +0200529 up_write(&dev_replace->rwsem);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100530
Anand Jaincd36da22020-09-05 01:34:26 +0800531 ret = btrfs_sysfs_add_device(tgt_device);
Liu Bo73416da2015-08-14 18:33:07 +0800532 if (ret)
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -0400533 btrfs_err(fs_info, "kobj add dev failed %d", ret);
Liu Bo73416da2015-08-14 18:33:07 +0800534
Chris Mason6374e57a2017-06-23 09:48:21 -0700535 btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100536
Nikolay Borisovf232ab02019-05-14 13:54:43 +0300537 /* Commit dev_replace state and reserve 1 item for it. */
538 trans = btrfs_start_transaction(root, 1);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100539 if (IS_ERR(trans)) {
540 ret = PTR_ERR(trans);
David Sterbacb5583d2018-09-07 16:11:23 +0200541 down_write(&dev_replace->rwsem);
Jeff Mahoney5c061472018-09-06 15:52:17 -0400542 dev_replace->replace_state =
543 BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED;
544 dev_replace->srcdev = NULL;
545 dev_replace->tgtdev = NULL;
Nikolay Borisovfa194522019-05-14 13:54:42 +0300546 up_write(&dev_replace->rwsem);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100547 goto leave;
548 }
549
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400550 ret = btrfs_commit_transaction(trans);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100551 WARN_ON(ret);
552
553 /* the disk copy procedure reuses the scrub code */
554 ret = btrfs_scrub_dev(fs_info, src_device->devid, 0,
Miao Xie7cc8e582014-09-03 21:35:38 +0800555 btrfs_device_get_total_bytes(src_device),
Stefan Behrense93c89c2012-11-05 17:33:06 +0100556 &dev_replace->scrub_progress, 0, 1);
557
Anand Jainfc23c242016-03-24 18:48:12 +0800558 ret = btrfs_dev_replace_finishing(fs_info, ret);
David Sterba4cea9032020-01-25 12:35:38 +0100559 if (ret == -EINPROGRESS)
Anand Jainb5255452016-03-24 18:48:14 +0800560 ret = BTRFS_IOCTL_DEV_REPLACE_RESULT_SCRUB_INPROGRESS;
Stefan Behrense93c89c2012-11-05 17:33:06 +0100561
Eryu Guan2fc9f6b2014-10-13 12:42:12 +0800562 return ret;
Stefan Behrense93c89c2012-11-05 17:33:06 +0100563
564leave:
Nikolay Borisov4f5ad7b2018-07-20 19:37:51 +0300565 btrfs_destroy_dev_replace_tgtdev(tgt_device);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100566 return ret;
567}
568
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400569int btrfs_dev_replace_by_ioctl(struct btrfs_fs_info *fs_info,
Anand Jainb5255452016-03-24 18:48:14 +0800570 struct btrfs_ioctl_dev_replace_args *args)
571{
572 int ret;
573
574 switch (args->start.cont_reading_from_srcdev_mode) {
575 case BTRFS_IOCTL_DEV_REPLACE_CONT_READING_FROM_SRCDEV_MODE_ALWAYS:
576 case BTRFS_IOCTL_DEV_REPLACE_CONT_READING_FROM_SRCDEV_MODE_AVOID:
577 break;
578 default:
579 return -EINVAL;
580 }
581
582 if ((args->start.srcdevid == 0 && args->start.srcdev_name[0] == '\0') ||
583 args->start.tgtdev_name[0] == '\0')
584 return -EINVAL;
585
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400586 ret = btrfs_dev_replace_start(fs_info, args->start.tgtdev_name,
Anand Jainb5255452016-03-24 18:48:14 +0800587 args->start.srcdevid,
588 args->start.srcdev_name,
589 args->start.cont_reading_from_srcdev_mode);
590 args->result = ret;
591 /* don't warn if EINPROGRESS, someone else might be running scrub */
Anand Jain53e62fb2018-11-11 22:22:24 +0800592 if (ret == BTRFS_IOCTL_DEV_REPLACE_RESULT_SCRUB_INPROGRESS ||
593 ret == BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR)
594 return 0;
Anand Jainb5255452016-03-24 18:48:14 +0800595
596 return ret;
597}
598
Miao Xiec404e0d2014-01-30 16:46:55 +0800599/*
Nicholas D Steeves01327612016-05-19 21:18:45 -0400600 * blocked until all in-flight bios operations are finished.
Miao Xiec404e0d2014-01-30 16:46:55 +0800601 */
602static void btrfs_rm_dev_replace_blocked(struct btrfs_fs_info *fs_info)
603{
Miao Xiec404e0d2014-01-30 16:46:55 +0800604 set_bit(BTRFS_FS_STATE_DEV_REPLACING, &fs_info->fs_state);
David Sterba7f8d2362018-04-05 01:04:49 +0200605 wait_event(fs_info->dev_replace.replace_wait, !percpu_counter_sum(
606 &fs_info->dev_replace.bio_counter));
Miao Xiec404e0d2014-01-30 16:46:55 +0800607}
608
609/*
610 * we have removed target device, it is safe to allow new bios request.
611 */
612static void btrfs_rm_dev_replace_unblocked(struct btrfs_fs_info *fs_info)
613{
614 clear_bit(BTRFS_FS_STATE_DEV_REPLACING, &fs_info->fs_state);
David Sterba7f8d2362018-04-05 01:04:49 +0200615 wake_up(&fs_info->dev_replace.replace_wait);
Miao Xiec404e0d2014-01-30 16:46:55 +0800616}
617
Filipe Manana4c8f3532020-09-23 15:30:16 +0100618/*
619 * When finishing the device replace, before swapping the source device with the
620 * target device we must update the chunk allocation state in the target device,
621 * as it is empty because replace works by directly copying the chunks and not
622 * through the normal chunk allocation path.
623 */
624static int btrfs_set_target_alloc_state(struct btrfs_device *srcdev,
625 struct btrfs_device *tgtdev)
626{
627 struct extent_state *cached_state = NULL;
628 u64 start = 0;
629 u64 found_start;
630 u64 found_end;
631 int ret = 0;
632
633 lockdep_assert_held(&srcdev->fs_info->chunk_mutex);
634
635 while (!find_first_extent_bit(&srcdev->alloc_state, start,
636 &found_start, &found_end,
637 CHUNK_ALLOCATED, &cached_state)) {
638 ret = set_extent_bits(&tgtdev->alloc_state, found_start,
639 found_end, CHUNK_ALLOCATED);
640 if (ret)
641 break;
642 start = found_end + 1;
643 }
644
645 free_extent_state(cached_state);
646 return ret;
647}
648
Anand Jain0725c0c2020-09-05 01:34:36 +0800649static void btrfs_dev_replace_update_device_in_mapping_tree(
650 struct btrfs_fs_info *fs_info,
651 struct btrfs_device *srcdev,
652 struct btrfs_device *tgtdev)
653{
654 struct extent_map_tree *em_tree = &fs_info->mapping_tree;
655 struct extent_map *em;
656 struct map_lookup *map;
657 u64 start = 0;
658 int i;
659
660 write_lock(&em_tree->lock);
661 do {
662 em = lookup_extent_mapping(em_tree, start, (u64)-1);
663 if (!em)
664 break;
665 map = em->map_lookup;
666 for (i = 0; i < map->num_stripes; i++)
667 if (srcdev == map->stripes[i].dev)
668 map->stripes[i].dev = tgtdev;
669 start = em->start + em->len;
670 free_extent_map(em);
671 } while (start);
672 write_unlock(&em_tree->lock);
673}
674
Stefan Behrense93c89c2012-11-05 17:33:06 +0100675static int btrfs_dev_replace_finishing(struct btrfs_fs_info *fs_info,
676 int scrub_ret)
677{
678 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
679 struct btrfs_device *tgt_device;
680 struct btrfs_device *src_device;
681 struct btrfs_root *root = fs_info->tree_root;
682 u8 uuid_tmp[BTRFS_UUID_SIZE];
683 struct btrfs_trans_handle *trans;
684 int ret = 0;
685
686 /* don't allow cancel or unmount to disturb the finishing procedure */
687 mutex_lock(&dev_replace->lock_finishing_cancel_unmount);
688
David Sterbacb5583d2018-09-07 16:11:23 +0200689 down_read(&dev_replace->rwsem);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100690 /* was the operation canceled, or is it finished? */
691 if (dev_replace->replace_state !=
692 BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED) {
David Sterbacb5583d2018-09-07 16:11:23 +0200693 up_read(&dev_replace->rwsem);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100694 mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
695 return 0;
696 }
697
698 tgt_device = dev_replace->tgtdev;
699 src_device = dev_replace->srcdev;
David Sterbacb5583d2018-09-07 16:11:23 +0200700 up_read(&dev_replace->rwsem);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100701
Stefan Behrense93c89c2012-11-05 17:33:06 +0100702 /*
703 * flush all outstanding I/O and inode extent mappings before the
704 * copy operation is declared as being finished
705 */
Josef Bacikb4912132020-07-21 10:22:12 -0400706 ret = btrfs_start_delalloc_roots(fs_info, U64_MAX);
Miao Xie3edb2a62013-01-22 10:49:33 +0000707 if (ret) {
708 mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
709 return ret;
710 }
Chris Mason6374e57a2017-06-23 09:48:21 -0700711 btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100712
Filipe Manana66d204a2020-10-12 11:55:24 +0100713 if (!scrub_ret)
714 btrfs_reada_remove_dev(src_device);
715
Nikolay Borisovdebd1c02019-05-17 10:44:25 +0300716 /*
717 * We have to use this loop approach because at this point src_device
718 * has to be available for transaction commit to complete, yet new
719 * chunks shouldn't be allocated on the device.
720 */
721 while (1) {
722 trans = btrfs_start_transaction(root, 0);
723 if (IS_ERR(trans)) {
Filipe Manana66d204a2020-10-12 11:55:24 +0100724 btrfs_reada_undo_remove_dev(src_device);
Nikolay Borisovdebd1c02019-05-17 10:44:25 +0300725 mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
726 return PTR_ERR(trans);
727 }
728 ret = btrfs_commit_transaction(trans);
729 WARN_ON(ret);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100730
Nikolay Borisovdebd1c02019-05-17 10:44:25 +0300731 /* Prevent write_all_supers() during the finishing procedure */
732 mutex_lock(&fs_info->fs_devices->device_list_mutex);
733 /* Prevent new chunks being allocated on the source device */
734 mutex_lock(&fs_info->chunk_mutex);
735
736 if (!list_empty(&src_device->post_commit_list)) {
737 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
738 mutex_unlock(&fs_info->chunk_mutex);
739 } else {
740 break;
741 }
742 }
743
David Sterbacb5583d2018-09-07 16:11:23 +0200744 down_write(&dev_replace->rwsem);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100745 dev_replace->replace_state =
746 scrub_ret ? BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED
747 : BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED;
748 dev_replace->tgtdev = NULL;
749 dev_replace->srcdev = NULL;
Allen Paisa9444422018-06-12 17:18:25 +0530750 dev_replace->time_stopped = ktime_get_real_seconds();
Stefan Behrense93c89c2012-11-05 17:33:06 +0100751 dev_replace->item_needs_writeback = 1;
752
Filipe Manana4c8f3532020-09-23 15:30:16 +0100753 /*
754 * Update allocation state in the new device and replace the old device
755 * with the new one in the mapping tree.
756 */
Miao Xiec404e0d2014-01-30 16:46:55 +0800757 if (!scrub_ret) {
Filipe Manana4c8f3532020-09-23 15:30:16 +0100758 scrub_ret = btrfs_set_target_alloc_state(src_device, tgt_device);
759 if (scrub_ret)
760 goto error;
Miao Xiec404e0d2014-01-30 16:46:55 +0800761 btrfs_dev_replace_update_device_in_mapping_tree(fs_info,
762 src_device,
763 tgt_device);
764 } else {
Anand Jainf9085ab2018-11-20 19:56:16 +0800765 if (scrub_ret != -ECANCELED)
766 btrfs_err_in_rcu(fs_info,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400767 "btrfs_scrub_dev(%s, %llu, %s) failed %d",
Anand Jain3c958bd2017-11-28 10:43:10 +0800768 btrfs_dev_name(src_device),
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400769 src_device->devid,
770 rcu_str_deref(tgt_device->name), scrub_ret);
Filipe Manana4c8f3532020-09-23 15:30:16 +0100771error:
David Sterbacb5583d2018-09-07 16:11:23 +0200772 up_write(&dev_replace->rwsem);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400773 mutex_unlock(&fs_info->chunk_mutex);
774 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Filipe Manana66d204a2020-10-12 11:55:24 +0100775 btrfs_reada_undo_remove_dev(src_device);
Qu Wenruoae6529c2017-03-29 09:33:21 +0800776 btrfs_rm_dev_replace_blocked(fs_info);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100777 if (tgt_device)
Nikolay Borisov4f5ad7b2018-07-20 19:37:51 +0300778 btrfs_destroy_dev_replace_tgtdev(tgt_device);
Qu Wenruoae6529c2017-03-29 09:33:21 +0800779 btrfs_rm_dev_replace_unblocked(fs_info);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100780 mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
781
Eryu Guan2fc9f6b2014-10-13 12:42:12 +0800782 return scrub_ret;
Stefan Behrense93c89c2012-11-05 17:33:06 +0100783 }
784
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400785 btrfs_info_in_rcu(fs_info,
786 "dev_replace from %s (devid %llu) to %s finished",
Anand Jain3c958bd2017-11-28 10:43:10 +0800787 btrfs_dev_name(src_device),
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400788 src_device->devid,
789 rcu_str_deref(tgt_device->name));
Anand Jain401e29c2017-12-04 12:54:55 +0800790 clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &tgt_device->dev_state);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100791 tgt_device->devid = src_device->devid;
792 src_device->devid = BTRFS_DEV_REPLACE_DEVID;
Stefan Behrense93c89c2012-11-05 17:33:06 +0100793 memcpy(uuid_tmp, tgt_device->uuid, sizeof(uuid_tmp));
794 memcpy(tgt_device->uuid, src_device->uuid, sizeof(tgt_device->uuid));
795 memcpy(src_device->uuid, uuid_tmp, sizeof(src_device->uuid));
Miao Xie7cc8e582014-09-03 21:35:38 +0800796 btrfs_device_set_total_bytes(tgt_device, src_device->total_bytes);
797 btrfs_device_set_disk_total_bytes(tgt_device,
798 src_device->disk_total_bytes);
799 btrfs_device_set_bytes_used(tgt_device, src_device->bytes_used);
Miao Xiece7213c2014-09-03 21:35:34 +0800800 tgt_device->commit_bytes_used = src_device->bytes_used;
Anand Jain88acff62016-05-03 17:44:43 +0800801
Nikolay Borisovd6507cf2018-07-20 19:37:50 +0300802 btrfs_assign_next_active_device(src_device, tgt_device);
Anand Jain88acff62016-05-03 17:44:43 +0800803
Stefan Behrense93c89c2012-11-05 17:33:06 +0100804 list_add(&tgt_device->dev_alloc_list, &fs_info->fs_devices->alloc_list);
Miao Xie82372bc2014-09-03 21:35:44 +0800805 fs_info->fs_devices->rw_devices++;
Stefan Behrense93c89c2012-11-05 17:33:06 +0100806
David Sterbacb5583d2018-09-07 16:11:23 +0200807 up_write(&dev_replace->rwsem);
Miao Xiec404e0d2014-01-30 16:46:55 +0800808 btrfs_rm_dev_replace_blocked(fs_info);
809
Nikolay Borisov68a9db52018-07-20 19:37:48 +0300810 btrfs_rm_dev_replace_remove_srcdev(src_device);
Ilya Dryomov13572722013-10-02 20:41:01 +0300811
Miao Xiec404e0d2014-01-30 16:46:55 +0800812 btrfs_rm_dev_replace_unblocked(fs_info);
813
Stefan Behrense93c89c2012-11-05 17:33:06 +0100814 /*
Misono Tomohiro1e7e1f92018-07-31 16:20:21 +0900815 * Increment dev_stats_ccnt so that btrfs_run_dev_stats() will
816 * update on-disk dev stats value during commit transaction
817 */
818 atomic_inc(&tgt_device->dev_stats_ccnt);
819
820 /*
Stefan Behrense93c89c2012-11-05 17:33:06 +0100821 * this is again a consistent state where no dev_replace procedure
822 * is running, the target device is part of the filesystem, the
823 * source device is not part of the filesystem anymore and its 1st
824 * superblock is scratched out so that it is no longer marked to
825 * belong to this filesystem.
826 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400827 mutex_unlock(&fs_info->chunk_mutex);
828 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100829
Qu Wenruo084b6e7c2014-10-30 16:52:31 +0800830 /* replace the sysfs entry */
Anand Jain53f8a742020-09-05 01:34:27 +0800831 btrfs_sysfs_remove_device(src_device);
Anand Jain668e48af2020-01-06 19:38:31 +0800832 btrfs_sysfs_update_devid(tgt_device);
Josef Bacik313b0852020-08-20 11:18:26 -0400833 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &src_device->dev_state))
834 btrfs_scratch_superblocks(fs_info, src_device->bdev,
835 src_device->name->str);
Qu Wenruo084b6e7c2014-10-30 16:52:31 +0800836
Stefan Behrense93c89c2012-11-05 17:33:06 +0100837 /* write back the superblocks */
838 trans = btrfs_start_transaction(root, 0);
839 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400840 btrfs_commit_transaction(trans);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100841
842 mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
843
Josef Bacika466c852020-08-20 11:18:27 -0400844 btrfs_rm_dev_replace_free_srcdev(src_device);
845
Stefan Behrense93c89c2012-11-05 17:33:06 +0100846 return 0;
847}
848
David Sterba74b595f2017-06-14 16:24:56 +0200849/*
850 * Read progress of device replace status according to the state and last
851 * stored position. The value format is the same as for
852 * btrfs_dev_replace::progress_1000
853 */
854static u64 btrfs_dev_replace_progress(struct btrfs_fs_info *fs_info)
855{
856 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
857 u64 ret = 0;
858
859 switch (dev_replace->replace_state) {
860 case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
861 case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
862 ret = 0;
863 break;
864 case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
865 ret = 1000;
866 break;
867 case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
868 case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
869 ret = div64_u64(dev_replace->cursor_left,
870 div_u64(btrfs_device_get_total_bytes(
871 dev_replace->srcdev), 1000));
872 break;
873 }
874
875 return ret;
876}
877
Stefan Behrense93c89c2012-11-05 17:33:06 +0100878void btrfs_dev_replace_status(struct btrfs_fs_info *fs_info,
879 struct btrfs_ioctl_dev_replace_args *args)
880{
881 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
882
David Sterbacb5583d2018-09-07 16:11:23 +0200883 down_read(&dev_replace->rwsem);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100884 /* even if !dev_replace_is_valid, the values are good enough for
885 * the replace_status ioctl */
886 args->result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR;
887 args->status.replace_state = dev_replace->replace_state;
888 args->status.time_started = dev_replace->time_started;
889 args->status.time_stopped = dev_replace->time_stopped;
890 args->status.num_write_errors =
891 atomic64_read(&dev_replace->num_write_errors);
892 args->status.num_uncorrectable_read_errors =
893 atomic64_read(&dev_replace->num_uncorrectable_read_errors);
David Sterba74b595f2017-06-14 16:24:56 +0200894 args->status.progress_1000 = btrfs_dev_replace_progress(fs_info);
David Sterbacb5583d2018-09-07 16:11:23 +0200895 up_read(&dev_replace->rwsem);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100896}
897
Anand Jain18e67c72018-02-12 23:33:31 +0800898int btrfs_dev_replace_cancel(struct btrfs_fs_info *fs_info)
Stefan Behrense93c89c2012-11-05 17:33:06 +0100899{
900 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
901 struct btrfs_device *tgt_device = NULL;
Anand Jain8f2ceaa2018-02-13 11:53:43 +0800902 struct btrfs_device *src_device = NULL;
Stefan Behrense93c89c2012-11-05 17:33:06 +0100903 struct btrfs_trans_handle *trans;
904 struct btrfs_root *root = fs_info->tree_root;
Anand Jain18e67c72018-02-12 23:33:31 +0800905 int result;
Stefan Behrense93c89c2012-11-05 17:33:06 +0100906 int ret;
907
David Howellsbc98a422017-07-17 08:45:34 +0100908 if (sb_rdonly(fs_info->sb))
Ilya Dryomove649e582013-10-10 20:40:21 +0300909 return -EROFS;
910
Stefan Behrense93c89c2012-11-05 17:33:06 +0100911 mutex_lock(&dev_replace->lock_finishing_cancel_unmount);
David Sterbacb5583d2018-09-07 16:11:23 +0200912 down_write(&dev_replace->rwsem);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100913 switch (dev_replace->replace_state) {
914 case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
915 case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
916 case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
917 result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NOT_STARTED;
David Sterbacb5583d2018-09-07 16:11:23 +0200918 up_write(&dev_replace->rwsem);
Anand Jaind189dd72018-11-14 13:50:26 +0800919 break;
Stefan Behrense93c89c2012-11-05 17:33:06 +0100920 case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
Anand Jaind189dd72018-11-14 13:50:26 +0800921 tgt_device = dev_replace->tgtdev;
922 src_device = dev_replace->srcdev;
David Sterbacb5583d2018-09-07 16:11:23 +0200923 up_write(&dev_replace->rwsem);
Anand Jainb47dda22018-11-11 22:22:20 +0800924 ret = btrfs_scrub_cancel(fs_info);
925 if (ret < 0) {
926 result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NOT_STARTED;
927 } else {
928 result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR;
929 /*
930 * btrfs_dev_replace_finishing() will handle the
931 * cleanup part
932 */
933 btrfs_info_in_rcu(fs_info,
934 "dev_replace from %s (devid %llu) to %s canceled",
935 btrfs_dev_name(src_device), src_device->devid,
936 btrfs_dev_name(tgt_device));
937 }
Anand Jaind189dd72018-11-14 13:50:26 +0800938 break;
Stefan Behrense93c89c2012-11-05 17:33:06 +0100939 case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
Anand Jaind189dd72018-11-14 13:50:26 +0800940 /*
941 * Scrub doing the replace isn't running so we need to do the
942 * cleanup step of btrfs_dev_replace_finishing() here
943 */
Stefan Behrense93c89c2012-11-05 17:33:06 +0100944 result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR;
945 tgt_device = dev_replace->tgtdev;
Anand Jain8f2ceaa2018-02-13 11:53:43 +0800946 src_device = dev_replace->srcdev;
Stefan Behrense93c89c2012-11-05 17:33:06 +0100947 dev_replace->tgtdev = NULL;
948 dev_replace->srcdev = NULL;
Anand Jaind189dd72018-11-14 13:50:26 +0800949 dev_replace->replace_state =
950 BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED;
951 dev_replace->time_stopped = ktime_get_real_seconds();
952 dev_replace->item_needs_writeback = 1;
953
David Sterbacb5583d2018-09-07 16:11:23 +0200954 up_write(&dev_replace->rwsem);
Anand Jaind189dd72018-11-14 13:50:26 +0800955
Anand Jainfe97e2e2018-11-11 22:22:21 +0800956 /* Scrub for replace must not be running in suspended state */
957 ret = btrfs_scrub_cancel(fs_info);
958 ASSERT(ret != -ENOTCONN);
Anand Jaind189dd72018-11-14 13:50:26 +0800959
960 trans = btrfs_start_transaction(root, 0);
961 if (IS_ERR(trans)) {
962 mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
963 return PTR_ERR(trans);
964 }
965 ret = btrfs_commit_transaction(trans);
966 WARN_ON(ret);
967
968 btrfs_info_in_rcu(fs_info,
969 "suspended dev_replace from %s (devid %llu) to %s canceled",
970 btrfs_dev_name(src_device), src_device->devid,
971 btrfs_dev_name(tgt_device));
972
973 if (tgt_device)
974 btrfs_destroy_dev_replace_tgtdev(tgt_device);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100975 break;
Anand Jaind189dd72018-11-14 13:50:26 +0800976 default:
Dan Carpenter669e8592019-02-11 21:32:10 +0300977 up_write(&dev_replace->rwsem);
Anand Jaind189dd72018-11-14 13:50:26 +0800978 result = -EINVAL;
Stefan Behrense93c89c2012-11-05 17:33:06 +0100979 }
Stefan Behrense93c89c2012-11-05 17:33:06 +0100980
Stefan Behrense93c89c2012-11-05 17:33:06 +0100981 mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
982 return result;
983}
984
985void btrfs_dev_replace_suspend_for_unmount(struct btrfs_fs_info *fs_info)
986{
987 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
988
989 mutex_lock(&dev_replace->lock_finishing_cancel_unmount);
David Sterbacb5583d2018-09-07 16:11:23 +0200990 down_write(&dev_replace->rwsem);
991
Stefan Behrense93c89c2012-11-05 17:33:06 +0100992 switch (dev_replace->replace_state) {
993 case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
994 case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
995 case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
996 case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
997 break;
998 case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
999 dev_replace->replace_state =
1000 BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED;
Allen Paisa9444422018-06-12 17:18:25 +05301001 dev_replace->time_stopped = ktime_get_real_seconds();
Stefan Behrense93c89c2012-11-05 17:33:06 +01001002 dev_replace->item_needs_writeback = 1;
Frank Holtonefe120a2013-12-20 11:37:06 -05001003 btrfs_info(fs_info, "suspending dev_replace for unmount");
Stefan Behrense93c89c2012-11-05 17:33:06 +01001004 break;
1005 }
1006
David Sterbacb5583d2018-09-07 16:11:23 +02001007 up_write(&dev_replace->rwsem);
Stefan Behrense93c89c2012-11-05 17:33:06 +01001008 mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
1009}
1010
1011/* resume dev_replace procedure that was interrupted by unmount */
1012int btrfs_resume_dev_replace_async(struct btrfs_fs_info *fs_info)
1013{
1014 struct task_struct *task;
1015 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
1016
David Sterbacb5583d2018-09-07 16:11:23 +02001017 down_write(&dev_replace->rwsem);
1018
Stefan Behrense93c89c2012-11-05 17:33:06 +01001019 switch (dev_replace->replace_state) {
1020 case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
1021 case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
1022 case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
David Sterbacb5583d2018-09-07 16:11:23 +02001023 up_write(&dev_replace->rwsem);
Stefan Behrense93c89c2012-11-05 17:33:06 +01001024 return 0;
1025 case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
1026 break;
1027 case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
1028 dev_replace->replace_state =
1029 BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED;
1030 break;
1031 }
1032 if (!dev_replace->tgtdev || !dev_replace->tgtdev->bdev) {
Frank Holtonefe120a2013-12-20 11:37:06 -05001033 btrfs_info(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04001034 "cannot continue dev_replace, tgtdev is missing");
1035 btrfs_info(fs_info,
1036 "you may cancel the operation after 'mount -o degraded'");
Anand Jain0d228ec2018-11-11 22:22:17 +08001037 dev_replace->replace_state =
1038 BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED;
David Sterbacb5583d2018-09-07 16:11:23 +02001039 up_write(&dev_replace->rwsem);
Stefan Behrense93c89c2012-11-05 17:33:06 +01001040 return 0;
1041 }
David Sterbacb5583d2018-09-07 16:11:23 +02001042 up_write(&dev_replace->rwsem);
Stefan Behrense93c89c2012-11-05 17:33:06 +01001043
David Sterba010a47b2018-03-20 19:51:04 +01001044 /*
1045 * This could collide with a paused balance, but the exclusive op logic
1046 * should never allow both to start and pause. We don't want to allow
1047 * dev-replace to start anyway.
1048 */
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -05001049 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_DEV_REPLACE)) {
David Sterbacb5583d2018-09-07 16:11:23 +02001050 down_write(&dev_replace->rwsem);
Anand Jain05c49e62018-11-11 22:22:18 +08001051 dev_replace->replace_state =
1052 BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED;
David Sterbacb5583d2018-09-07 16:11:23 +02001053 up_write(&dev_replace->rwsem);
David Sterba010a47b2018-03-20 19:51:04 +01001054 btrfs_info(fs_info,
1055 "cannot resume dev-replace, other exclusive operation running");
1056 return 0;
1057 }
1058
Stefan Behrense93c89c2012-11-05 17:33:06 +01001059 task = kthread_run(btrfs_dev_replace_kthread, fs_info, "btrfs-devrepl");
Rusty Russell8c6ffba2013-07-15 11:20:32 +09301060 return PTR_ERR_OR_ZERO(task);
Stefan Behrense93c89c2012-11-05 17:33:06 +01001061}
1062
1063static int btrfs_dev_replace_kthread(void *data)
1064{
1065 struct btrfs_fs_info *fs_info = data;
1066 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
Stefan Behrense93c89c2012-11-05 17:33:06 +01001067 u64 progress;
David Sterba00251a52018-03-20 15:35:50 +01001068 int ret;
Stefan Behrense93c89c2012-11-05 17:33:06 +01001069
David Sterbaf1b8a1e2017-06-14 16:28:42 +02001070 progress = btrfs_dev_replace_progress(fs_info);
1071 progress = div_u64(progress, 10);
1072 btrfs_info_in_rcu(fs_info,
Anand Jain3c958bd2017-11-28 10:43:10 +08001073 "continuing dev_replace from %s (devid %llu) to target %s @%u%%",
1074 btrfs_dev_name(dev_replace->srcdev),
David Sterbaf1b8a1e2017-06-14 16:28:42 +02001075 dev_replace->srcdev->devid,
Anand Jain3c958bd2017-11-28 10:43:10 +08001076 btrfs_dev_name(dev_replace->tgtdev),
David Sterbaf1b8a1e2017-06-14 16:28:42 +02001077 (unsigned int)progress);
1078
Stefan Behrense93c89c2012-11-05 17:33:06 +01001079 ret = btrfs_scrub_dev(fs_info, dev_replace->srcdev->devid,
1080 dev_replace->committed_cursor_left,
Miao Xie7cc8e582014-09-03 21:35:38 +08001081 btrfs_device_get_total_bytes(dev_replace->srcdev),
Stefan Behrense93c89c2012-11-05 17:33:06 +01001082 &dev_replace->scrub_progress, 0, 1);
1083 ret = btrfs_dev_replace_finishing(fs_info, ret);
Anand Jain49365e62018-11-20 19:56:15 +08001084 WARN_ON(ret && ret != -ECANCELED);
David Sterba00251a52018-03-20 15:35:50 +01001085
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -05001086 btrfs_exclop_finish(fs_info);
Stefan Behrense93c89c2012-11-05 17:33:06 +01001087 return 0;
1088}
1089
David Sterbae1f60a62019-10-01 19:57:39 +02001090int __pure btrfs_dev_replace_is_ongoing(struct btrfs_dev_replace *dev_replace)
Stefan Behrense93c89c2012-11-05 17:33:06 +01001091{
1092 if (!dev_replace->is_valid)
1093 return 0;
1094
1095 switch (dev_replace->replace_state) {
1096 case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
1097 case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
1098 case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
1099 return 0;
1100 case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
1101 case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
1102 /*
1103 * return true even if tgtdev is missing (this is
1104 * something that can happen if the dev_replace
1105 * procedure is suspended by an umount and then
1106 * the tgtdev is missing (or "btrfs dev scan") was
Andrea Gelmini52042d82018-11-28 12:05:13 +01001107 * not called and the filesystem is remounted
Stefan Behrense93c89c2012-11-05 17:33:06 +01001108 * in degraded state. This does not stop the
1109 * dev_replace procedure. It needs to be canceled
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -08001110 * manually if the cancellation is wanted.
Stefan Behrense93c89c2012-11-05 17:33:06 +01001111 */
1112 break;
1113 }
1114 return 1;
1115}
1116
Miao Xiec404e0d2014-01-30 16:46:55 +08001117void btrfs_bio_counter_inc_noblocked(struct btrfs_fs_info *fs_info)
1118{
David Sterba7f8d2362018-04-05 01:04:49 +02001119 percpu_counter_inc(&fs_info->dev_replace.bio_counter);
Miao Xiec404e0d2014-01-30 16:46:55 +08001120}
1121
Miao Xie42452152014-11-25 16:39:28 +08001122void btrfs_bio_counter_sub(struct btrfs_fs_info *fs_info, s64 amount)
Miao Xiec404e0d2014-01-30 16:46:55 +08001123{
David Sterba7f8d2362018-04-05 01:04:49 +02001124 percpu_counter_sub(&fs_info->dev_replace.bio_counter, amount);
1125 cond_wake_up_nomb(&fs_info->dev_replace.replace_wait);
Miao Xiec404e0d2014-01-30 16:46:55 +08001126}
1127
1128void btrfs_bio_counter_inc_blocked(struct btrfs_fs_info *fs_info)
1129{
Zhao Lei09dd7a02015-01-20 15:11:37 +08001130 while (1) {
David Sterba7f8d2362018-04-05 01:04:49 +02001131 percpu_counter_inc(&fs_info->dev_replace.bio_counter);
Zhao Lei09dd7a02015-01-20 15:11:37 +08001132 if (likely(!test_bit(BTRFS_FS_STATE_DEV_REPLACING,
1133 &fs_info->fs_state)))
1134 break;
1135
Miao Xiec404e0d2014-01-30 16:46:55 +08001136 btrfs_bio_counter_dec(fs_info);
David Sterba7f8d2362018-04-05 01:04:49 +02001137 wait_event(fs_info->dev_replace.replace_wait,
Miao Xiec404e0d2014-01-30 16:46:55 +08001138 !test_bit(BTRFS_FS_STATE_DEV_REPLACING,
1139 &fs_info->fs_state));
Miao Xiec404e0d2014-01-30 16:46:55 +08001140 }
Miao Xiec404e0d2014-01-30 16:46:55 +08001141}