blob: 94f000b0ca792c7dcf28044683f49df060054f52 [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>
Stefan Behrense93c89c2012-11-05 17:33:06 +010012#include "ctree.h"
13#include "extent_map.h"
14#include "disk-io.h"
15#include "transaction.h"
16#include "print-tree.h"
17#include "volumes.h"
18#include "async-thread.h"
19#include "check-integrity.h"
20#include "rcu-string.h"
21#include "dev-replace.h"
Anand Jain49c6f732014-06-03 11:36:02 +080022#include "sysfs.h"
Stefan Behrense93c89c2012-11-05 17:33:06 +010023
Stefan Behrense93c89c2012-11-05 17:33:06 +010024static int btrfs_dev_replace_finishing(struct btrfs_fs_info *fs_info,
25 int scrub_ret);
26static void btrfs_dev_replace_update_device_in_mapping_tree(
27 struct btrfs_fs_info *fs_info,
28 struct btrfs_device *srcdev,
29 struct btrfs_device *tgtdev);
Stefan Behrense93c89c2012-11-05 17:33:06 +010030static int btrfs_dev_replace_kthread(void *data);
Stefan Behrense93c89c2012-11-05 17:33:06 +010031
32int btrfs_init_dev_replace(struct btrfs_fs_info *fs_info)
33{
34 struct btrfs_key key;
35 struct btrfs_root *dev_root = fs_info->dev_root;
36 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
37 struct extent_buffer *eb;
38 int slot;
39 int ret = 0;
40 struct btrfs_path *path = NULL;
41 int item_size;
42 struct btrfs_dev_replace_item *ptr;
43 u64 src_devid;
44
45 path = btrfs_alloc_path();
46 if (!path) {
47 ret = -ENOMEM;
48 goto out;
49 }
50
51 key.objectid = 0;
52 key.type = BTRFS_DEV_REPLACE_KEY;
53 key.offset = 0;
54 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
55 if (ret) {
56no_valid_dev_replace_entry_found:
57 ret = 0;
58 dev_replace->replace_state =
59 BTRFS_DEV_REPLACE_ITEM_STATE_NEVER_STARTED;
60 dev_replace->cont_reading_from_srcdev_mode =
61 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_ALWAYS;
Stefan Behrense93c89c2012-11-05 17:33:06 +010062 dev_replace->time_started = 0;
63 dev_replace->time_stopped = 0;
64 atomic64_set(&dev_replace->num_write_errors, 0);
65 atomic64_set(&dev_replace->num_uncorrectable_read_errors, 0);
66 dev_replace->cursor_left = 0;
67 dev_replace->committed_cursor_left = 0;
68 dev_replace->cursor_left_last_write_of_item = 0;
69 dev_replace->cursor_right = 0;
70 dev_replace->srcdev = NULL;
71 dev_replace->tgtdev = NULL;
72 dev_replace->is_valid = 0;
73 dev_replace->item_needs_writeback = 0;
74 goto out;
75 }
76 slot = path->slots[0];
77 eb = path->nodes[0];
78 item_size = btrfs_item_size_nr(eb, slot);
79 ptr = btrfs_item_ptr(eb, slot, struct btrfs_dev_replace_item);
80
81 if (item_size != sizeof(struct btrfs_dev_replace_item)) {
Frank Holtonefe120a2013-12-20 11:37:06 -050082 btrfs_warn(fs_info,
83 "dev_replace entry found has unexpected size, ignore entry");
Stefan Behrense93c89c2012-11-05 17:33:06 +010084 goto no_valid_dev_replace_entry_found;
85 }
86
87 src_devid = btrfs_dev_replace_src_devid(eb, ptr);
88 dev_replace->cont_reading_from_srcdev_mode =
89 btrfs_dev_replace_cont_reading_from_srcdev_mode(eb, ptr);
90 dev_replace->replace_state = btrfs_dev_replace_replace_state(eb, ptr);
91 dev_replace->time_started = btrfs_dev_replace_time_started(eb, ptr);
92 dev_replace->time_stopped =
93 btrfs_dev_replace_time_stopped(eb, ptr);
94 atomic64_set(&dev_replace->num_write_errors,
95 btrfs_dev_replace_num_write_errors(eb, ptr));
96 atomic64_set(&dev_replace->num_uncorrectable_read_errors,
97 btrfs_dev_replace_num_uncorrectable_read_errors(eb, ptr));
98 dev_replace->cursor_left = btrfs_dev_replace_cursor_left(eb, ptr);
99 dev_replace->committed_cursor_left = dev_replace->cursor_left;
100 dev_replace->cursor_left_last_write_of_item = dev_replace->cursor_left;
101 dev_replace->cursor_right = btrfs_dev_replace_cursor_right(eb, ptr);
102 dev_replace->is_valid = 1;
103
104 dev_replace->item_needs_writeback = 0;
105 switch (dev_replace->replace_state) {
106 case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
107 case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
108 case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
109 dev_replace->srcdev = NULL;
110 dev_replace->tgtdev = NULL;
111 break;
112 case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
113 case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
Anand Jaine4319cd2019-01-17 23:32:31 +0800114 dev_replace->srcdev = btrfs_find_device(fs_info->fs_devices,
Anand Jain09ba3bc2019-01-19 14:48:55 +0800115 src_devid, NULL, NULL, true);
Anand Jaine4319cd2019-01-17 23:32:31 +0800116 dev_replace->tgtdev = btrfs_find_device(fs_info->fs_devices,
Stefan Behrense93c89c2012-11-05 17:33:06 +0100117 BTRFS_DEV_REPLACE_DEVID,
Anand Jain09ba3bc2019-01-19 14:48:55 +0800118 NULL, NULL, true);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100119 /*
120 * allow 'btrfs dev replace_cancel' if src/tgt device is
121 * missing
122 */
123 if (!dev_replace->srcdev &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400124 !btrfs_test_opt(fs_info, DEGRADED)) {
Stefan Behrense93c89c2012-11-05 17:33:06 +0100125 ret = -EIO;
Frank Holtonefe120a2013-12-20 11:37:06 -0500126 btrfs_warn(fs_info,
127 "cannot mount because device replace operation is ongoing and");
128 btrfs_warn(fs_info,
129 "srcdev (devid %llu) is missing, need to run 'btrfs dev scan'?",
130 src_devid);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100131 }
132 if (!dev_replace->tgtdev &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400133 !btrfs_test_opt(fs_info, DEGRADED)) {
Stefan Behrense93c89c2012-11-05 17:33:06 +0100134 ret = -EIO;
Frank Holtonefe120a2013-12-20 11:37:06 -0500135 btrfs_warn(fs_info,
136 "cannot mount because device replace operation is ongoing and");
137 btrfs_warn(fs_info,
138 "tgtdev (devid %llu) is missing, need to run 'btrfs dev scan'?",
Geert Uytterhoeven6e71c472013-08-20 13:20:08 +0200139 BTRFS_DEV_REPLACE_DEVID);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100140 }
141 if (dev_replace->tgtdev) {
142 if (dev_replace->srcdev) {
143 dev_replace->tgtdev->total_bytes =
144 dev_replace->srcdev->total_bytes;
145 dev_replace->tgtdev->disk_total_bytes =
146 dev_replace->srcdev->disk_total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +0800147 dev_replace->tgtdev->commit_total_bytes =
148 dev_replace->srcdev->commit_total_bytes;
Stefan Behrense93c89c2012-11-05 17:33:06 +0100149 dev_replace->tgtdev->bytes_used =
150 dev_replace->srcdev->bytes_used;
Miao Xiece7213c2014-09-03 21:35:34 +0800151 dev_replace->tgtdev->commit_bytes_used =
152 dev_replace->srcdev->commit_bytes_used;
Stefan Behrense93c89c2012-11-05 17:33:06 +0100153 }
Anand Jain401e29c2017-12-04 12:54:55 +0800154 set_bit(BTRFS_DEV_STATE_REPLACE_TGT,
155 &dev_replace->tgtdev->dev_state);
Anand Jain15fc1282018-02-12 23:36:25 +0800156
157 WARN_ON(fs_info->fs_devices->rw_devices == 0);
158 dev_replace->tgtdev->io_width = fs_info->sectorsize;
159 dev_replace->tgtdev->io_align = fs_info->sectorsize;
160 dev_replace->tgtdev->sector_size = fs_info->sectorsize;
161 dev_replace->tgtdev->fs_info = fs_info;
162 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
163 &dev_replace->tgtdev->dev_state);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100164 }
165 break;
166 }
167
168out:
Tsutomu Itoh527afb42015-08-19 14:55:00 +0900169 btrfs_free_path(path);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100170 return ret;
171}
172
173/*
David Sterbad48f39d2018-03-20 16:09:48 +0100174 * Initialize a new device for device replace target from a given source dev
175 * and path.
176 *
177 * Return 0 and new device in @device_out, otherwise return < 0
178 */
179static int btrfs_init_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
180 const char *device_path,
181 struct btrfs_device *srcdev,
182 struct btrfs_device **device_out)
183{
184 struct btrfs_device *device;
185 struct block_device *bdev;
186 struct list_head *devices;
187 struct rcu_string *name;
188 u64 devid = BTRFS_DEV_REPLACE_DEVID;
189 int ret = 0;
190
191 *device_out = NULL;
192 if (fs_info->fs_devices->seeding) {
193 btrfs_err(fs_info, "the filesystem is a seed filesystem!");
194 return -EINVAL;
195 }
196
197 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
198 fs_info->bdev_holder);
199 if (IS_ERR(bdev)) {
200 btrfs_err(fs_info, "target device %s is invalid!", device_path);
201 return PTR_ERR(bdev);
202 }
203
Nikolay Borisovddb93782019-05-14 13:54:38 +0300204 sync_blockdev(bdev);
David Sterbad48f39d2018-03-20 16:09:48 +0100205
206 devices = &fs_info->fs_devices->devices;
207 list_for_each_entry(device, devices, dev_list) {
208 if (device->bdev == bdev) {
209 btrfs_err(fs_info,
210 "target device is in the filesystem!");
211 ret = -EEXIST;
212 goto error;
213 }
214 }
215
216
217 if (i_size_read(bdev->bd_inode) <
218 btrfs_device_get_total_bytes(srcdev)) {
219 btrfs_err(fs_info,
220 "target device is smaller than source device!");
221 ret = -EINVAL;
222 goto error;
223 }
224
225
226 device = btrfs_alloc_device(NULL, &devid, NULL);
227 if (IS_ERR(device)) {
228 ret = PTR_ERR(device);
229 goto error;
230 }
231
232 name = rcu_string_strdup(device_path, GFP_KERNEL);
233 if (!name) {
234 btrfs_free_device(device);
235 ret = -ENOMEM;
236 goto error;
237 }
238 rcu_assign_pointer(device->name, name);
239
David Sterbad48f39d2018-03-20 16:09:48 +0100240 set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
241 device->generation = 0;
242 device->io_width = fs_info->sectorsize;
243 device->io_align = fs_info->sectorsize;
244 device->sector_size = fs_info->sectorsize;
245 device->total_bytes = btrfs_device_get_total_bytes(srcdev);
246 device->disk_total_bytes = btrfs_device_get_disk_total_bytes(srcdev);
247 device->bytes_used = btrfs_device_get_bytes_used(srcdev);
248 device->commit_total_bytes = srcdev->commit_total_bytes;
249 device->commit_bytes_used = device->bytes_used;
250 device->fs_info = fs_info;
251 device->bdev = bdev;
252 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
253 set_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
254 device->mode = FMODE_EXCL;
255 device->dev_stats_valid = 1;
256 set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE);
257 device->fs_devices = fs_info->fs_devices;
Nikolay Borisovb0d9e1e2019-05-14 13:54:39 +0300258
259 mutex_lock(&fs_info->fs_devices->device_list_mutex);
David Sterbad48f39d2018-03-20 16:09:48 +0100260 list_add(&device->dev_list, &fs_info->fs_devices->devices);
261 fs_info->fs_devices->num_devices++;
262 fs_info->fs_devices->open_devices++;
263 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
264
265 *device_out = device;
266 return 0;
267
268error:
269 blkdev_put(bdev, FMODE_EXCL);
270 return ret;
271}
272
273/*
Stefan Behrense93c89c2012-11-05 17:33:06 +0100274 * called from commit_transaction. Writes changed device replace state to
275 * disk.
276 */
David Sterba2b584c62019-03-20 16:51:44 +0100277int btrfs_run_dev_replace(struct btrfs_trans_handle *trans)
Stefan Behrense93c89c2012-11-05 17:33:06 +0100278{
David Sterba2b584c62019-03-20 16:51:44 +0100279 struct btrfs_fs_info *fs_info = trans->fs_info;
Stefan Behrense93c89c2012-11-05 17:33:06 +0100280 int ret;
281 struct btrfs_root *dev_root = fs_info->dev_root;
282 struct btrfs_path *path;
283 struct btrfs_key key;
284 struct extent_buffer *eb;
285 struct btrfs_dev_replace_item *ptr;
286 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
287
David Sterbacb5583d2018-09-07 16:11:23 +0200288 down_read(&dev_replace->rwsem);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100289 if (!dev_replace->is_valid ||
290 !dev_replace->item_needs_writeback) {
David Sterbacb5583d2018-09-07 16:11:23 +0200291 up_read(&dev_replace->rwsem);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100292 return 0;
293 }
David Sterbacb5583d2018-09-07 16:11:23 +0200294 up_read(&dev_replace->rwsem);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100295
296 key.objectid = 0;
297 key.type = BTRFS_DEV_REPLACE_KEY;
298 key.offset = 0;
299
300 path = btrfs_alloc_path();
301 if (!path) {
302 ret = -ENOMEM;
303 goto out;
304 }
305 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
306 if (ret < 0) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400307 btrfs_warn(fs_info,
308 "error %d while searching for dev_replace item!",
309 ret);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100310 goto out;
311 }
312
313 if (ret == 0 &&
314 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
315 /*
316 * need to delete old one and insert a new one.
317 * Since no attempt is made to recover any old state, if the
318 * dev_replace state is 'running', the data on the target
319 * drive is lost.
320 * It would be possible to recover the state: just make sure
321 * that the beginning of the item is never changed and always
322 * contains all the essential information. Then read this
323 * minimal set of information and use it as a base for the
324 * new state.
325 */
326 ret = btrfs_del_item(trans, dev_root, path);
327 if (ret != 0) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400328 btrfs_warn(fs_info,
329 "delete too small dev_replace item failed %d!",
330 ret);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100331 goto out;
332 }
333 ret = 1;
334 }
335
336 if (ret == 1) {
337 /* need to insert a new item */
338 btrfs_release_path(path);
339 ret = btrfs_insert_empty_item(trans, dev_root, path,
340 &key, sizeof(*ptr));
341 if (ret < 0) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400342 btrfs_warn(fs_info,
343 "insert dev_replace item failed %d!", ret);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100344 goto out;
345 }
346 }
347
348 eb = path->nodes[0];
349 ptr = btrfs_item_ptr(eb, path->slots[0],
350 struct btrfs_dev_replace_item);
351
David Sterbacb5583d2018-09-07 16:11:23 +0200352 down_write(&dev_replace->rwsem);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100353 if (dev_replace->srcdev)
354 btrfs_set_dev_replace_src_devid(eb, ptr,
355 dev_replace->srcdev->devid);
356 else
357 btrfs_set_dev_replace_src_devid(eb, ptr, (u64)-1);
358 btrfs_set_dev_replace_cont_reading_from_srcdev_mode(eb, ptr,
359 dev_replace->cont_reading_from_srcdev_mode);
360 btrfs_set_dev_replace_replace_state(eb, ptr,
361 dev_replace->replace_state);
362 btrfs_set_dev_replace_time_started(eb, ptr, dev_replace->time_started);
363 btrfs_set_dev_replace_time_stopped(eb, ptr, dev_replace->time_stopped);
364 btrfs_set_dev_replace_num_write_errors(eb, ptr,
365 atomic64_read(&dev_replace->num_write_errors));
366 btrfs_set_dev_replace_num_uncorrectable_read_errors(eb, ptr,
367 atomic64_read(&dev_replace->num_uncorrectable_read_errors));
368 dev_replace->cursor_left_last_write_of_item =
369 dev_replace->cursor_left;
370 btrfs_set_dev_replace_cursor_left(eb, ptr,
371 dev_replace->cursor_left_last_write_of_item);
372 btrfs_set_dev_replace_cursor_right(eb, ptr,
373 dev_replace->cursor_right);
374 dev_replace->item_needs_writeback = 0;
David Sterbacb5583d2018-09-07 16:11:23 +0200375 up_write(&dev_replace->rwsem);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100376
377 btrfs_mark_buffer_dirty(eb);
378
379out:
380 btrfs_free_path(path);
381
382 return ret;
383}
384
Anand Jain3c958bd2017-11-28 10:43:10 +0800385static char* btrfs_dev_name(struct btrfs_device *device)
386{
Anand Jainacf18c52018-02-24 19:43:56 +0800387 if (!device || test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
Anand Jain3c958bd2017-11-28 10:43:10 +0800388 return "<missing disk>";
389 else
390 return rcu_str_deref(device->name);
391}
392
Anand Jain54862d62018-11-11 22:22:16 +0800393static int btrfs_dev_replace_start(struct btrfs_fs_info *fs_info,
David Sterbada353f62017-02-14 17:55:53 +0100394 const char *tgtdev_name, u64 srcdevid, const char *srcdev_name,
395 int read_src)
Stefan Behrense93c89c2012-11-05 17:33:06 +0100396{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400397 struct btrfs_root *root = fs_info->dev_root;
Stefan Behrense93c89c2012-11-05 17:33:06 +0100398 struct btrfs_trans_handle *trans;
Stefan Behrense93c89c2012-11-05 17:33:06 +0100399 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
400 int ret;
401 struct btrfs_device *tgt_device = NULL;
402 struct btrfs_device *src_device = NULL;
403
Nikolay Borisova27a94c2018-09-03 12:46:14 +0300404 src_device = btrfs_find_device_by_devspec(fs_info, srcdevid,
405 srcdev_name);
406 if (IS_ERR(src_device))
407 return PTR_ERR(src_device);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100408
Omar Sandovaleede2bf2016-11-03 10:28:12 -0700409 if (btrfs_pinned_by_swapfile(fs_info, src_device)) {
410 btrfs_warn_in_rcu(fs_info,
411 "cannot replace device %s (devid %llu) due to active swapfile",
412 btrfs_dev_name(src_device), src_device->devid);
413 return -ETXTBSY;
414 }
415
Anand Jain9e271ae2015-08-14 18:33:02 +0800416 /*
417 * Here we commit the transaction to make sure commit_total_bytes
418 * of all the devices are updated.
419 */
420 trans = btrfs_attach_transaction(root);
421 if (!IS_ERR(trans)) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400422 ret = btrfs_commit_transaction(trans);
Anand Jain9e271ae2015-08-14 18:33:02 +0800423 if (ret)
424 return ret;
425 } else if (PTR_ERR(trans) != -ENOENT) {
426 return PTR_ERR(trans);
427 }
428
Nikolay Borisove1e0eb42019-05-14 13:54:41 +0300429 ret = btrfs_init_dev_replace_tgtdev(fs_info, tgtdev_name,
430 src_device, &tgt_device);
431 if (ret)
432 return ret;
433
David Sterbacb5583d2018-09-07 16:11:23 +0200434 down_write(&dev_replace->rwsem);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100435 switch (dev_replace->replace_state) {
436 case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
437 case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
438 case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
439 break;
440 case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
441 case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
Jeff Mahoney5c061472018-09-06 15:52:17 -0400442 ASSERT(0);
Anand Jainb5255452016-03-24 18:48:14 +0800443 ret = BTRFS_IOCTL_DEV_REPLACE_RESULT_ALREADY_STARTED;
Nikolay Borisovfa194522019-05-14 13:54:42 +0300444 up_write(&dev_replace->rwsem);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100445 goto leave;
446 }
447
Anand Jainb5255452016-03-24 18:48:14 +0800448 dev_replace->cont_reading_from_srcdev_mode = read_src;
Stefan Behrense93c89c2012-11-05 17:33:06 +0100449 dev_replace->srcdev = src_device;
Stefan Behrense93c89c2012-11-05 17:33:06 +0100450 dev_replace->tgtdev = tgt_device;
451
Anand Jainfc23c242016-03-24 18:48:12 +0800452 btrfs_info_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +0200453 "dev_replace from %s (devid %llu) to %s started",
Anand Jain3c958bd2017-11-28 10:43:10 +0800454 btrfs_dev_name(src_device),
Stefan Behrense93c89c2012-11-05 17:33:06 +0100455 src_device->devid,
456 rcu_str_deref(tgt_device->name));
457
Stefan Behrense93c89c2012-11-05 17:33:06 +0100458 /*
459 * from now on, the writes to the srcdev are all duplicated to
460 * go to the tgtdev as well (refer to btrfs_map_block()).
461 */
462 dev_replace->replace_state = BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED;
Allen Paisa9444422018-06-12 17:18:25 +0530463 dev_replace->time_started = ktime_get_real_seconds();
Stefan Behrense93c89c2012-11-05 17:33:06 +0100464 dev_replace->cursor_left = 0;
465 dev_replace->committed_cursor_left = 0;
466 dev_replace->cursor_left_last_write_of_item = 0;
467 dev_replace->cursor_right = 0;
468 dev_replace->is_valid = 1;
469 dev_replace->item_needs_writeback = 1;
Yauhen Kharuzhy7ccefb92016-03-29 14:17:48 -0700470 atomic64_set(&dev_replace->num_write_errors, 0);
471 atomic64_set(&dev_replace->num_uncorrectable_read_errors, 0);
David Sterbacb5583d2018-09-07 16:11:23 +0200472 up_write(&dev_replace->rwsem);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100473
Liu Bo73416da2015-08-14 18:33:07 +0800474 ret = btrfs_sysfs_add_device_link(tgt_device->fs_devices, tgt_device);
475 if (ret)
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -0400476 btrfs_err(fs_info, "kobj add dev failed %d", ret);
Liu Bo73416da2015-08-14 18:33:07 +0800477
Chris Mason6374e57a2017-06-23 09:48:21 -0700478 btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100479
480 /* force writing the updated state information to disk */
481 trans = btrfs_start_transaction(root, 0);
482 if (IS_ERR(trans)) {
483 ret = PTR_ERR(trans);
David Sterbacb5583d2018-09-07 16:11:23 +0200484 down_write(&dev_replace->rwsem);
Jeff Mahoney5c061472018-09-06 15:52:17 -0400485 dev_replace->replace_state =
486 BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED;
487 dev_replace->srcdev = NULL;
488 dev_replace->tgtdev = NULL;
Nikolay Borisovfa194522019-05-14 13:54:42 +0300489 up_write(&dev_replace->rwsem);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100490 goto leave;
491 }
492
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400493 ret = btrfs_commit_transaction(trans);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100494 WARN_ON(ret);
495
496 /* the disk copy procedure reuses the scrub code */
497 ret = btrfs_scrub_dev(fs_info, src_device->devid, 0,
Miao Xie7cc8e582014-09-03 21:35:38 +0800498 btrfs_device_get_total_bytes(src_device),
Stefan Behrense93c89c2012-11-05 17:33:06 +0100499 &dev_replace->scrub_progress, 0, 1);
500
Anand Jainfc23c242016-03-24 18:48:12 +0800501 ret = btrfs_dev_replace_finishing(fs_info, ret);
Eryu Guan2fc9f6b2014-10-13 12:42:12 +0800502 if (ret == -EINPROGRESS) {
Anand Jainb5255452016-03-24 18:48:14 +0800503 ret = BTRFS_IOCTL_DEV_REPLACE_RESULT_SCRUB_INPROGRESS;
Anand Jain49365e62018-11-20 19:56:15 +0800504 } else if (ret != -ECANCELED) {
Eryu Guan2fc9f6b2014-10-13 12:42:12 +0800505 WARN_ON(ret);
506 }
Stefan Behrense93c89c2012-11-05 17:33:06 +0100507
Eryu Guan2fc9f6b2014-10-13 12:42:12 +0800508 return ret;
Stefan Behrense93c89c2012-11-05 17:33:06 +0100509
510leave:
Nikolay Borisov4f5ad7b2018-07-20 19:37:51 +0300511 btrfs_destroy_dev_replace_tgtdev(tgt_device);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100512 return ret;
513}
514
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400515int btrfs_dev_replace_by_ioctl(struct btrfs_fs_info *fs_info,
Anand Jainb5255452016-03-24 18:48:14 +0800516 struct btrfs_ioctl_dev_replace_args *args)
517{
518 int ret;
519
520 switch (args->start.cont_reading_from_srcdev_mode) {
521 case BTRFS_IOCTL_DEV_REPLACE_CONT_READING_FROM_SRCDEV_MODE_ALWAYS:
522 case BTRFS_IOCTL_DEV_REPLACE_CONT_READING_FROM_SRCDEV_MODE_AVOID:
523 break;
524 default:
525 return -EINVAL;
526 }
527
528 if ((args->start.srcdevid == 0 && args->start.srcdev_name[0] == '\0') ||
529 args->start.tgtdev_name[0] == '\0')
530 return -EINVAL;
531
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400532 ret = btrfs_dev_replace_start(fs_info, args->start.tgtdev_name,
Anand Jainb5255452016-03-24 18:48:14 +0800533 args->start.srcdevid,
534 args->start.srcdev_name,
535 args->start.cont_reading_from_srcdev_mode);
536 args->result = ret;
537 /* don't warn if EINPROGRESS, someone else might be running scrub */
Anand Jain53e62fb2018-11-11 22:22:24 +0800538 if (ret == BTRFS_IOCTL_DEV_REPLACE_RESULT_SCRUB_INPROGRESS ||
539 ret == BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR)
540 return 0;
Anand Jainb5255452016-03-24 18:48:14 +0800541
542 return ret;
543}
544
Miao Xiec404e0d2014-01-30 16:46:55 +0800545/*
Nicholas D Steeves01327612016-05-19 21:18:45 -0400546 * blocked until all in-flight bios operations are finished.
Miao Xiec404e0d2014-01-30 16:46:55 +0800547 */
548static void btrfs_rm_dev_replace_blocked(struct btrfs_fs_info *fs_info)
549{
Miao Xiec404e0d2014-01-30 16:46:55 +0800550 set_bit(BTRFS_FS_STATE_DEV_REPLACING, &fs_info->fs_state);
David Sterba7f8d2362018-04-05 01:04:49 +0200551 wait_event(fs_info->dev_replace.replace_wait, !percpu_counter_sum(
552 &fs_info->dev_replace.bio_counter));
Miao Xiec404e0d2014-01-30 16:46:55 +0800553}
554
555/*
556 * we have removed target device, it is safe to allow new bios request.
557 */
558static void btrfs_rm_dev_replace_unblocked(struct btrfs_fs_info *fs_info)
559{
560 clear_bit(BTRFS_FS_STATE_DEV_REPLACING, &fs_info->fs_state);
David Sterba7f8d2362018-04-05 01:04:49 +0200561 wake_up(&fs_info->dev_replace.replace_wait);
Miao Xiec404e0d2014-01-30 16:46:55 +0800562}
563
Stefan Behrense93c89c2012-11-05 17:33:06 +0100564static int btrfs_dev_replace_finishing(struct btrfs_fs_info *fs_info,
565 int scrub_ret)
566{
567 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
568 struct btrfs_device *tgt_device;
569 struct btrfs_device *src_device;
570 struct btrfs_root *root = fs_info->tree_root;
571 u8 uuid_tmp[BTRFS_UUID_SIZE];
572 struct btrfs_trans_handle *trans;
573 int ret = 0;
574
575 /* don't allow cancel or unmount to disturb the finishing procedure */
576 mutex_lock(&dev_replace->lock_finishing_cancel_unmount);
577
David Sterbacb5583d2018-09-07 16:11:23 +0200578 down_read(&dev_replace->rwsem);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100579 /* was the operation canceled, or is it finished? */
580 if (dev_replace->replace_state !=
581 BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED) {
David Sterbacb5583d2018-09-07 16:11:23 +0200582 up_read(&dev_replace->rwsem);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100583 mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
584 return 0;
585 }
586
587 tgt_device = dev_replace->tgtdev;
588 src_device = dev_replace->srcdev;
David Sterbacb5583d2018-09-07 16:11:23 +0200589 up_read(&dev_replace->rwsem);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100590
Stefan Behrense93c89c2012-11-05 17:33:06 +0100591 /*
592 * flush all outstanding I/O and inode extent mappings before the
593 * copy operation is declared as being finished
594 */
Nikolay Borisov82b3e532018-04-23 10:54:13 +0300595 ret = btrfs_start_delalloc_roots(fs_info, -1);
Miao Xie3edb2a62013-01-22 10:49:33 +0000596 if (ret) {
597 mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
598 return ret;
599 }
Chris Mason6374e57a2017-06-23 09:48:21 -0700600 btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100601
Nikolay Borisovdebd1c02019-05-17 10:44:25 +0300602 /*
603 * We have to use this loop approach because at this point src_device
604 * has to be available for transaction commit to complete, yet new
605 * chunks shouldn't be allocated on the device.
606 */
607 while (1) {
608 trans = btrfs_start_transaction(root, 0);
609 if (IS_ERR(trans)) {
610 mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
611 return PTR_ERR(trans);
612 }
613 ret = btrfs_commit_transaction(trans);
614 WARN_ON(ret);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100615
Nikolay Borisovdebd1c02019-05-17 10:44:25 +0300616 /* Prevent write_all_supers() during the finishing procedure */
617 mutex_lock(&fs_info->fs_devices->device_list_mutex);
618 /* Prevent new chunks being allocated on the source device */
619 mutex_lock(&fs_info->chunk_mutex);
620
621 if (!list_empty(&src_device->post_commit_list)) {
622 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
623 mutex_unlock(&fs_info->chunk_mutex);
624 } else {
625 break;
626 }
627 }
628
David Sterbacb5583d2018-09-07 16:11:23 +0200629 down_write(&dev_replace->rwsem);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100630 dev_replace->replace_state =
631 scrub_ret ? BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED
632 : BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED;
633 dev_replace->tgtdev = NULL;
634 dev_replace->srcdev = NULL;
Allen Paisa9444422018-06-12 17:18:25 +0530635 dev_replace->time_stopped = ktime_get_real_seconds();
Stefan Behrense93c89c2012-11-05 17:33:06 +0100636 dev_replace->item_needs_writeback = 1;
637
Miao Xiec404e0d2014-01-30 16:46:55 +0800638 /* replace old device with new one in mapping tree */
639 if (!scrub_ret) {
640 btrfs_dev_replace_update_device_in_mapping_tree(fs_info,
641 src_device,
642 tgt_device);
643 } else {
Anand Jainf9085ab2018-11-20 19:56:16 +0800644 if (scrub_ret != -ECANCELED)
645 btrfs_err_in_rcu(fs_info,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400646 "btrfs_scrub_dev(%s, %llu, %s) failed %d",
Anand Jain3c958bd2017-11-28 10:43:10 +0800647 btrfs_dev_name(src_device),
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400648 src_device->devid,
649 rcu_str_deref(tgt_device->name), scrub_ret);
David Sterbacb5583d2018-09-07 16:11:23 +0200650 up_write(&dev_replace->rwsem);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400651 mutex_unlock(&fs_info->chunk_mutex);
652 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Qu Wenruoae6529c2017-03-29 09:33:21 +0800653 btrfs_rm_dev_replace_blocked(fs_info);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100654 if (tgt_device)
Nikolay Borisov4f5ad7b2018-07-20 19:37:51 +0300655 btrfs_destroy_dev_replace_tgtdev(tgt_device);
Qu Wenruoae6529c2017-03-29 09:33:21 +0800656 btrfs_rm_dev_replace_unblocked(fs_info);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100657 mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
658
Eryu Guan2fc9f6b2014-10-13 12:42:12 +0800659 return scrub_ret;
Stefan Behrense93c89c2012-11-05 17:33:06 +0100660 }
661
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400662 btrfs_info_in_rcu(fs_info,
663 "dev_replace from %s (devid %llu) to %s finished",
Anand Jain3c958bd2017-11-28 10:43:10 +0800664 btrfs_dev_name(src_device),
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400665 src_device->devid,
666 rcu_str_deref(tgt_device->name));
Anand Jain401e29c2017-12-04 12:54:55 +0800667 clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &tgt_device->dev_state);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100668 tgt_device->devid = src_device->devid;
669 src_device->devid = BTRFS_DEV_REPLACE_DEVID;
Stefan Behrense93c89c2012-11-05 17:33:06 +0100670 memcpy(uuid_tmp, tgt_device->uuid, sizeof(uuid_tmp));
671 memcpy(tgt_device->uuid, src_device->uuid, sizeof(tgt_device->uuid));
672 memcpy(src_device->uuid, uuid_tmp, sizeof(src_device->uuid));
Miao Xie7cc8e582014-09-03 21:35:38 +0800673 btrfs_device_set_total_bytes(tgt_device, src_device->total_bytes);
674 btrfs_device_set_disk_total_bytes(tgt_device,
675 src_device->disk_total_bytes);
676 btrfs_device_set_bytes_used(tgt_device, src_device->bytes_used);
Miao Xie935e5cc2014-09-03 21:35:33 +0800677 tgt_device->commit_total_bytes = src_device->commit_total_bytes;
Miao Xiece7213c2014-09-03 21:35:34 +0800678 tgt_device->commit_bytes_used = src_device->bytes_used;
Anand Jain88acff62016-05-03 17:44:43 +0800679
Nikolay Borisovd6507cf2018-07-20 19:37:50 +0300680 btrfs_assign_next_active_device(src_device, tgt_device);
Anand Jain88acff62016-05-03 17:44:43 +0800681
Stefan Behrense93c89c2012-11-05 17:33:06 +0100682 list_add(&tgt_device->dev_alloc_list, &fs_info->fs_devices->alloc_list);
Miao Xie82372bc2014-09-03 21:35:44 +0800683 fs_info->fs_devices->rw_devices++;
Stefan Behrense93c89c2012-11-05 17:33:06 +0100684
David Sterbacb5583d2018-09-07 16:11:23 +0200685 up_write(&dev_replace->rwsem);
Miao Xiec404e0d2014-01-30 16:46:55 +0800686 btrfs_rm_dev_replace_blocked(fs_info);
687
Nikolay Borisov68a9db52018-07-20 19:37:48 +0300688 btrfs_rm_dev_replace_remove_srcdev(src_device);
Ilya Dryomov13572722013-10-02 20:41:01 +0300689
Miao Xiec404e0d2014-01-30 16:46:55 +0800690 btrfs_rm_dev_replace_unblocked(fs_info);
691
Stefan Behrense93c89c2012-11-05 17:33:06 +0100692 /*
Misono Tomohiro1e7e1f92018-07-31 16:20:21 +0900693 * Increment dev_stats_ccnt so that btrfs_run_dev_stats() will
694 * update on-disk dev stats value during commit transaction
695 */
696 atomic_inc(&tgt_device->dev_stats_ccnt);
697
698 /*
Stefan Behrense93c89c2012-11-05 17:33:06 +0100699 * this is again a consistent state where no dev_replace procedure
700 * is running, the target device is part of the filesystem, the
701 * source device is not part of the filesystem anymore and its 1st
702 * superblock is scratched out so that it is no longer marked to
703 * belong to this filesystem.
704 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400705 mutex_unlock(&fs_info->chunk_mutex);
706 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100707
Qu Wenruo084b6e7c2014-10-30 16:52:31 +0800708 /* replace the sysfs entry */
Anand Jain32576042015-08-14 18:32:49 +0800709 btrfs_sysfs_rm_device_link(fs_info->fs_devices, src_device);
David Sterba65237ee2019-03-20 16:34:54 +0100710 btrfs_rm_dev_replace_free_srcdev(src_device);
Qu Wenruo084b6e7c2014-10-30 16:52:31 +0800711
Stefan Behrense93c89c2012-11-05 17:33:06 +0100712 /* write back the superblocks */
713 trans = btrfs_start_transaction(root, 0);
714 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400715 btrfs_commit_transaction(trans);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100716
717 mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
718
719 return 0;
720}
721
722static void btrfs_dev_replace_update_device_in_mapping_tree(
723 struct btrfs_fs_info *fs_info,
724 struct btrfs_device *srcdev,
725 struct btrfs_device *tgtdev)
726{
727 struct extent_map_tree *em_tree = &fs_info->mapping_tree.map_tree;
728 struct extent_map *em;
729 struct map_lookup *map;
730 u64 start = 0;
731 int i;
732
733 write_lock(&em_tree->lock);
734 do {
735 em = lookup_extent_mapping(em_tree, start, (u64)-1);
736 if (!em)
737 break;
Jeff Mahoney95617d62015-06-03 10:55:48 -0400738 map = em->map_lookup;
Stefan Behrense93c89c2012-11-05 17:33:06 +0100739 for (i = 0; i < map->num_stripes; i++)
740 if (srcdev == map->stripes[i].dev)
741 map->stripes[i].dev = tgtdev;
742 start = em->start + em->len;
743 free_extent_map(em);
744 } while (start);
745 write_unlock(&em_tree->lock);
746}
747
David Sterba74b595f2017-06-14 16:24:56 +0200748/*
749 * Read progress of device replace status according to the state and last
750 * stored position. The value format is the same as for
751 * btrfs_dev_replace::progress_1000
752 */
753static u64 btrfs_dev_replace_progress(struct btrfs_fs_info *fs_info)
754{
755 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
756 u64 ret = 0;
757
758 switch (dev_replace->replace_state) {
759 case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
760 case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
761 ret = 0;
762 break;
763 case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
764 ret = 1000;
765 break;
766 case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
767 case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
768 ret = div64_u64(dev_replace->cursor_left,
769 div_u64(btrfs_device_get_total_bytes(
770 dev_replace->srcdev), 1000));
771 break;
772 }
773
774 return ret;
775}
776
Stefan Behrense93c89c2012-11-05 17:33:06 +0100777void btrfs_dev_replace_status(struct btrfs_fs_info *fs_info,
778 struct btrfs_ioctl_dev_replace_args *args)
779{
780 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
781
David Sterbacb5583d2018-09-07 16:11:23 +0200782 down_read(&dev_replace->rwsem);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100783 /* even if !dev_replace_is_valid, the values are good enough for
784 * the replace_status ioctl */
785 args->result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR;
786 args->status.replace_state = dev_replace->replace_state;
787 args->status.time_started = dev_replace->time_started;
788 args->status.time_stopped = dev_replace->time_stopped;
789 args->status.num_write_errors =
790 atomic64_read(&dev_replace->num_write_errors);
791 args->status.num_uncorrectable_read_errors =
792 atomic64_read(&dev_replace->num_uncorrectable_read_errors);
David Sterba74b595f2017-06-14 16:24:56 +0200793 args->status.progress_1000 = btrfs_dev_replace_progress(fs_info);
David Sterbacb5583d2018-09-07 16:11:23 +0200794 up_read(&dev_replace->rwsem);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100795}
796
Anand Jain18e67c72018-02-12 23:33:31 +0800797int btrfs_dev_replace_cancel(struct btrfs_fs_info *fs_info)
Stefan Behrense93c89c2012-11-05 17:33:06 +0100798{
799 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
800 struct btrfs_device *tgt_device = NULL;
Anand Jain8f2ceaa2018-02-13 11:53:43 +0800801 struct btrfs_device *src_device = NULL;
Stefan Behrense93c89c2012-11-05 17:33:06 +0100802 struct btrfs_trans_handle *trans;
803 struct btrfs_root *root = fs_info->tree_root;
Anand Jain18e67c72018-02-12 23:33:31 +0800804 int result;
Stefan Behrense93c89c2012-11-05 17:33:06 +0100805 int ret;
806
David Howellsbc98a422017-07-17 08:45:34 +0100807 if (sb_rdonly(fs_info->sb))
Ilya Dryomove649e582013-10-10 20:40:21 +0300808 return -EROFS;
809
Stefan Behrense93c89c2012-11-05 17:33:06 +0100810 mutex_lock(&dev_replace->lock_finishing_cancel_unmount);
David Sterbacb5583d2018-09-07 16:11:23 +0200811 down_write(&dev_replace->rwsem);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100812 switch (dev_replace->replace_state) {
813 case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
814 case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
815 case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
816 result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NOT_STARTED;
David Sterbacb5583d2018-09-07 16:11:23 +0200817 up_write(&dev_replace->rwsem);
Anand Jaind189dd72018-11-14 13:50:26 +0800818 break;
Stefan Behrense93c89c2012-11-05 17:33:06 +0100819 case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
Anand Jaind189dd72018-11-14 13:50:26 +0800820 tgt_device = dev_replace->tgtdev;
821 src_device = dev_replace->srcdev;
David Sterbacb5583d2018-09-07 16:11:23 +0200822 up_write(&dev_replace->rwsem);
Anand Jainb47dda22018-11-11 22:22:20 +0800823 ret = btrfs_scrub_cancel(fs_info);
824 if (ret < 0) {
825 result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NOT_STARTED;
826 } else {
827 result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR;
828 /*
829 * btrfs_dev_replace_finishing() will handle the
830 * cleanup part
831 */
832 btrfs_info_in_rcu(fs_info,
833 "dev_replace from %s (devid %llu) to %s canceled",
834 btrfs_dev_name(src_device), src_device->devid,
835 btrfs_dev_name(tgt_device));
836 }
Anand Jaind189dd72018-11-14 13:50:26 +0800837 break;
Stefan Behrense93c89c2012-11-05 17:33:06 +0100838 case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
Anand Jaind189dd72018-11-14 13:50:26 +0800839 /*
840 * Scrub doing the replace isn't running so we need to do the
841 * cleanup step of btrfs_dev_replace_finishing() here
842 */
Stefan Behrense93c89c2012-11-05 17:33:06 +0100843 result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR;
844 tgt_device = dev_replace->tgtdev;
Anand Jain8f2ceaa2018-02-13 11:53:43 +0800845 src_device = dev_replace->srcdev;
Stefan Behrense93c89c2012-11-05 17:33:06 +0100846 dev_replace->tgtdev = NULL;
847 dev_replace->srcdev = NULL;
Anand Jaind189dd72018-11-14 13:50:26 +0800848 dev_replace->replace_state =
849 BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED;
850 dev_replace->time_stopped = ktime_get_real_seconds();
851 dev_replace->item_needs_writeback = 1;
852
David Sterbacb5583d2018-09-07 16:11:23 +0200853 up_write(&dev_replace->rwsem);
Anand Jaind189dd72018-11-14 13:50:26 +0800854
Anand Jainfe97e2e2018-11-11 22:22:21 +0800855 /* Scrub for replace must not be running in suspended state */
856 ret = btrfs_scrub_cancel(fs_info);
857 ASSERT(ret != -ENOTCONN);
Anand Jaind189dd72018-11-14 13:50:26 +0800858
859 trans = btrfs_start_transaction(root, 0);
860 if (IS_ERR(trans)) {
861 mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
862 return PTR_ERR(trans);
863 }
864 ret = btrfs_commit_transaction(trans);
865 WARN_ON(ret);
866
867 btrfs_info_in_rcu(fs_info,
868 "suspended dev_replace from %s (devid %llu) to %s canceled",
869 btrfs_dev_name(src_device), src_device->devid,
870 btrfs_dev_name(tgt_device));
871
872 if (tgt_device)
873 btrfs_destroy_dev_replace_tgtdev(tgt_device);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100874 break;
Anand Jaind189dd72018-11-14 13:50:26 +0800875 default:
Dan Carpenter669e8592019-02-11 21:32:10 +0300876 up_write(&dev_replace->rwsem);
Anand Jaind189dd72018-11-14 13:50:26 +0800877 result = -EINVAL;
Stefan Behrense93c89c2012-11-05 17:33:06 +0100878 }
Stefan Behrense93c89c2012-11-05 17:33:06 +0100879
Stefan Behrense93c89c2012-11-05 17:33:06 +0100880 mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
881 return result;
882}
883
884void btrfs_dev_replace_suspend_for_unmount(struct btrfs_fs_info *fs_info)
885{
886 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
887
888 mutex_lock(&dev_replace->lock_finishing_cancel_unmount);
David Sterbacb5583d2018-09-07 16:11:23 +0200889 down_write(&dev_replace->rwsem);
890
Stefan Behrense93c89c2012-11-05 17:33:06 +0100891 switch (dev_replace->replace_state) {
892 case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
893 case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
894 case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
895 case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
896 break;
897 case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
898 dev_replace->replace_state =
899 BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED;
Allen Paisa9444422018-06-12 17:18:25 +0530900 dev_replace->time_stopped = ktime_get_real_seconds();
Stefan Behrense93c89c2012-11-05 17:33:06 +0100901 dev_replace->item_needs_writeback = 1;
Frank Holtonefe120a2013-12-20 11:37:06 -0500902 btrfs_info(fs_info, "suspending dev_replace for unmount");
Stefan Behrense93c89c2012-11-05 17:33:06 +0100903 break;
904 }
905
David Sterbacb5583d2018-09-07 16:11:23 +0200906 up_write(&dev_replace->rwsem);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100907 mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
908}
909
910/* resume dev_replace procedure that was interrupted by unmount */
911int btrfs_resume_dev_replace_async(struct btrfs_fs_info *fs_info)
912{
913 struct task_struct *task;
914 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
915
David Sterbacb5583d2018-09-07 16:11:23 +0200916 down_write(&dev_replace->rwsem);
917
Stefan Behrense93c89c2012-11-05 17:33:06 +0100918 switch (dev_replace->replace_state) {
919 case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
920 case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
921 case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
David Sterbacb5583d2018-09-07 16:11:23 +0200922 up_write(&dev_replace->rwsem);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100923 return 0;
924 case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
925 break;
926 case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
927 dev_replace->replace_state =
928 BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED;
929 break;
930 }
931 if (!dev_replace->tgtdev || !dev_replace->tgtdev->bdev) {
Frank Holtonefe120a2013-12-20 11:37:06 -0500932 btrfs_info(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400933 "cannot continue dev_replace, tgtdev is missing");
934 btrfs_info(fs_info,
935 "you may cancel the operation after 'mount -o degraded'");
Anand Jain0d228ec2018-11-11 22:22:17 +0800936 dev_replace->replace_state =
937 BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED;
David Sterbacb5583d2018-09-07 16:11:23 +0200938 up_write(&dev_replace->rwsem);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100939 return 0;
940 }
David Sterbacb5583d2018-09-07 16:11:23 +0200941 up_write(&dev_replace->rwsem);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100942
David Sterba010a47b2018-03-20 19:51:04 +0100943 /*
944 * This could collide with a paused balance, but the exclusive op logic
945 * should never allow both to start and pause. We don't want to allow
946 * dev-replace to start anyway.
947 */
948 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
David Sterbacb5583d2018-09-07 16:11:23 +0200949 down_write(&dev_replace->rwsem);
Anand Jain05c49e62018-11-11 22:22:18 +0800950 dev_replace->replace_state =
951 BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED;
David Sterbacb5583d2018-09-07 16:11:23 +0200952 up_write(&dev_replace->rwsem);
David Sterba010a47b2018-03-20 19:51:04 +0100953 btrfs_info(fs_info,
954 "cannot resume dev-replace, other exclusive operation running");
955 return 0;
956 }
957
Stefan Behrense93c89c2012-11-05 17:33:06 +0100958 task = kthread_run(btrfs_dev_replace_kthread, fs_info, "btrfs-devrepl");
Rusty Russell8c6ffba2013-07-15 11:20:32 +0930959 return PTR_ERR_OR_ZERO(task);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100960}
961
962static int btrfs_dev_replace_kthread(void *data)
963{
964 struct btrfs_fs_info *fs_info = data;
965 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
Stefan Behrense93c89c2012-11-05 17:33:06 +0100966 u64 progress;
David Sterba00251a52018-03-20 15:35:50 +0100967 int ret;
Stefan Behrense93c89c2012-11-05 17:33:06 +0100968
David Sterbaf1b8a1e2017-06-14 16:28:42 +0200969 progress = btrfs_dev_replace_progress(fs_info);
970 progress = div_u64(progress, 10);
971 btrfs_info_in_rcu(fs_info,
Anand Jain3c958bd2017-11-28 10:43:10 +0800972 "continuing dev_replace from %s (devid %llu) to target %s @%u%%",
973 btrfs_dev_name(dev_replace->srcdev),
David Sterbaf1b8a1e2017-06-14 16:28:42 +0200974 dev_replace->srcdev->devid,
Anand Jain3c958bd2017-11-28 10:43:10 +0800975 btrfs_dev_name(dev_replace->tgtdev),
David Sterbaf1b8a1e2017-06-14 16:28:42 +0200976 (unsigned int)progress);
977
Stefan Behrense93c89c2012-11-05 17:33:06 +0100978 ret = btrfs_scrub_dev(fs_info, dev_replace->srcdev->devid,
979 dev_replace->committed_cursor_left,
Miao Xie7cc8e582014-09-03 21:35:38 +0800980 btrfs_device_get_total_bytes(dev_replace->srcdev),
Stefan Behrense93c89c2012-11-05 17:33:06 +0100981 &dev_replace->scrub_progress, 0, 1);
982 ret = btrfs_dev_replace_finishing(fs_info, ret);
Anand Jain49365e62018-11-20 19:56:15 +0800983 WARN_ON(ret && ret != -ECANCELED);
David Sterba00251a52018-03-20 15:35:50 +0100984
985 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100986 return 0;
987}
988
989int btrfs_dev_replace_is_ongoing(struct btrfs_dev_replace *dev_replace)
990{
991 if (!dev_replace->is_valid)
992 return 0;
993
994 switch (dev_replace->replace_state) {
995 case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
996 case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
997 case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
998 return 0;
999 case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
1000 case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
1001 /*
1002 * return true even if tgtdev is missing (this is
1003 * something that can happen if the dev_replace
1004 * procedure is suspended by an umount and then
1005 * the tgtdev is missing (or "btrfs dev scan") was
Andrea Gelmini52042d82018-11-28 12:05:13 +01001006 * not called and the filesystem is remounted
Stefan Behrense93c89c2012-11-05 17:33:06 +01001007 * in degraded state. This does not stop the
1008 * dev_replace procedure. It needs to be canceled
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -08001009 * manually if the cancellation is wanted.
Stefan Behrense93c89c2012-11-05 17:33:06 +01001010 */
1011 break;
1012 }
1013 return 1;
1014}
1015
Miao Xiec404e0d2014-01-30 16:46:55 +08001016void btrfs_bio_counter_inc_noblocked(struct btrfs_fs_info *fs_info)
1017{
David Sterba7f8d2362018-04-05 01:04:49 +02001018 percpu_counter_inc(&fs_info->dev_replace.bio_counter);
Miao Xiec404e0d2014-01-30 16:46:55 +08001019}
1020
Miao Xie42452152014-11-25 16:39:28 +08001021void btrfs_bio_counter_sub(struct btrfs_fs_info *fs_info, s64 amount)
Miao Xiec404e0d2014-01-30 16:46:55 +08001022{
David Sterba7f8d2362018-04-05 01:04:49 +02001023 percpu_counter_sub(&fs_info->dev_replace.bio_counter, amount);
1024 cond_wake_up_nomb(&fs_info->dev_replace.replace_wait);
Miao Xiec404e0d2014-01-30 16:46:55 +08001025}
1026
1027void btrfs_bio_counter_inc_blocked(struct btrfs_fs_info *fs_info)
1028{
Zhao Lei09dd7a02015-01-20 15:11:37 +08001029 while (1) {
David Sterba7f8d2362018-04-05 01:04:49 +02001030 percpu_counter_inc(&fs_info->dev_replace.bio_counter);
Zhao Lei09dd7a02015-01-20 15:11:37 +08001031 if (likely(!test_bit(BTRFS_FS_STATE_DEV_REPLACING,
1032 &fs_info->fs_state)))
1033 break;
1034
Miao Xiec404e0d2014-01-30 16:46:55 +08001035 btrfs_bio_counter_dec(fs_info);
David Sterba7f8d2362018-04-05 01:04:49 +02001036 wait_event(fs_info->dev_replace.replace_wait,
Miao Xiec404e0d2014-01-30 16:46:55 +08001037 !test_bit(BTRFS_FS_STATE_DEV_REPLACING,
1038 &fs_info->fs_state));
Miao Xiec404e0d2014-01-30 16:46:55 +08001039 }
Miao Xiec404e0d2014-01-30 16:46:55 +08001040}