Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
[linux-2.6-block.git] / fs / btrfs / dev-replace.c
CommitLineData
c1d7c514 1// SPDX-License-Identifier: GPL-2.0
e93c89c1
SB
2/*
3 * Copyright (C) STRATO AG 2012. All rights reserved.
e93c89c1 4 */
c1d7c514 5
e93c89c1
SB
6#include <linux/sched.h>
7#include <linux/bio.h>
8#include <linux/slab.h>
e93c89c1 9#include <linux/blkdev.h>
e93c89c1
SB
10#include <linux/kthread.h>
11#include <linux/math64.h>
e93c89c1
SB
12#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"
49c6f736 22#include "sysfs.h"
e93c89c1 23
e93c89c1
SB
24static 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);
e93c89c1 30static int btrfs_dev_replace_kthread(void *data);
e93c89c1
SB
31
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;
62 dev_replace->replace_state = 0;
63 dev_replace->time_started = 0;
64 dev_replace->time_stopped = 0;
65 atomic64_set(&dev_replace->num_write_errors, 0);
66 atomic64_set(&dev_replace->num_uncorrectable_read_errors, 0);
67 dev_replace->cursor_left = 0;
68 dev_replace->committed_cursor_left = 0;
69 dev_replace->cursor_left_last_write_of_item = 0;
70 dev_replace->cursor_right = 0;
71 dev_replace->srcdev = NULL;
72 dev_replace->tgtdev = NULL;
73 dev_replace->is_valid = 0;
74 dev_replace->item_needs_writeback = 0;
75 goto out;
76 }
77 slot = path->slots[0];
78 eb = path->nodes[0];
79 item_size = btrfs_item_size_nr(eb, slot);
80 ptr = btrfs_item_ptr(eb, slot, struct btrfs_dev_replace_item);
81
82 if (item_size != sizeof(struct btrfs_dev_replace_item)) {
efe120a0
FH
83 btrfs_warn(fs_info,
84 "dev_replace entry found has unexpected size, ignore entry");
e93c89c1
SB
85 goto no_valid_dev_replace_entry_found;
86 }
87
88 src_devid = btrfs_dev_replace_src_devid(eb, ptr);
89 dev_replace->cont_reading_from_srcdev_mode =
90 btrfs_dev_replace_cont_reading_from_srcdev_mode(eb, ptr);
91 dev_replace->replace_state = btrfs_dev_replace_replace_state(eb, ptr);
92 dev_replace->time_started = btrfs_dev_replace_time_started(eb, ptr);
93 dev_replace->time_stopped =
94 btrfs_dev_replace_time_stopped(eb, ptr);
95 atomic64_set(&dev_replace->num_write_errors,
96 btrfs_dev_replace_num_write_errors(eb, ptr));
97 atomic64_set(&dev_replace->num_uncorrectable_read_errors,
98 btrfs_dev_replace_num_uncorrectable_read_errors(eb, ptr));
99 dev_replace->cursor_left = btrfs_dev_replace_cursor_left(eb, ptr);
100 dev_replace->committed_cursor_left = dev_replace->cursor_left;
101 dev_replace->cursor_left_last_write_of_item = dev_replace->cursor_left;
102 dev_replace->cursor_right = btrfs_dev_replace_cursor_right(eb, ptr);
103 dev_replace->is_valid = 1;
104
105 dev_replace->item_needs_writeback = 0;
106 switch (dev_replace->replace_state) {
107 case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
108 case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
109 case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
110 dev_replace->srcdev = NULL;
111 dev_replace->tgtdev = NULL;
112 break;
113 case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
114 case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
115 dev_replace->srcdev = btrfs_find_device(fs_info, src_devid,
116 NULL, NULL);
117 dev_replace->tgtdev = btrfs_find_device(fs_info,
118 BTRFS_DEV_REPLACE_DEVID,
119 NULL, NULL);
120 /*
121 * allow 'btrfs dev replace_cancel' if src/tgt device is
122 * missing
123 */
124 if (!dev_replace->srcdev &&
0b246afa 125 !btrfs_test_opt(fs_info, DEGRADED)) {
e93c89c1 126 ret = -EIO;
efe120a0
FH
127 btrfs_warn(fs_info,
128 "cannot mount because device replace operation is ongoing and");
129 btrfs_warn(fs_info,
130 "srcdev (devid %llu) is missing, need to run 'btrfs dev scan'?",
131 src_devid);
e93c89c1
SB
132 }
133 if (!dev_replace->tgtdev &&
0b246afa 134 !btrfs_test_opt(fs_info, DEGRADED)) {
e93c89c1 135 ret = -EIO;
efe120a0
FH
136 btrfs_warn(fs_info,
137 "cannot mount because device replace operation is ongoing and");
138 btrfs_warn(fs_info,
139 "tgtdev (devid %llu) is missing, need to run 'btrfs dev scan'?",
6e71c47a 140 BTRFS_DEV_REPLACE_DEVID);
e93c89c1
SB
141 }
142 if (dev_replace->tgtdev) {
143 if (dev_replace->srcdev) {
144 dev_replace->tgtdev->total_bytes =
145 dev_replace->srcdev->total_bytes;
146 dev_replace->tgtdev->disk_total_bytes =
147 dev_replace->srcdev->disk_total_bytes;
935e5cc9
MX
148 dev_replace->tgtdev->commit_total_bytes =
149 dev_replace->srcdev->commit_total_bytes;
e93c89c1
SB
150 dev_replace->tgtdev->bytes_used =
151 dev_replace->srcdev->bytes_used;
ce7213c7
MX
152 dev_replace->tgtdev->commit_bytes_used =
153 dev_replace->srcdev->commit_bytes_used;
e93c89c1 154 }
401e29c1
AJ
155 set_bit(BTRFS_DEV_STATE_REPLACE_TGT,
156 &dev_replace->tgtdev->dev_state);
15fc1283
AJ
157
158 WARN_ON(fs_info->fs_devices->rw_devices == 0);
159 dev_replace->tgtdev->io_width = fs_info->sectorsize;
160 dev_replace->tgtdev->io_align = fs_info->sectorsize;
161 dev_replace->tgtdev->sector_size = fs_info->sectorsize;
162 dev_replace->tgtdev->fs_info = fs_info;
163 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
164 &dev_replace->tgtdev->dev_state);
e93c89c1
SB
165 }
166 break;
167 }
168
169out:
527afb44 170 btrfs_free_path(path);
e93c89c1
SB
171 return ret;
172}
173
d48f39d5
DS
174/*
175 * Initialize a new device for device replace target from a given source dev
176 * and path.
177 *
178 * Return 0 and new device in @device_out, otherwise return < 0
179 */
180static int btrfs_init_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
181 const char *device_path,
182 struct btrfs_device *srcdev,
183 struct btrfs_device **device_out)
184{
185 struct btrfs_device *device;
186 struct block_device *bdev;
187 struct list_head *devices;
188 struct rcu_string *name;
189 u64 devid = BTRFS_DEV_REPLACE_DEVID;
190 int ret = 0;
191
192 *device_out = NULL;
193 if (fs_info->fs_devices->seeding) {
194 btrfs_err(fs_info, "the filesystem is a seed filesystem!");
195 return -EINVAL;
196 }
197
198 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
199 fs_info->bdev_holder);
200 if (IS_ERR(bdev)) {
201 btrfs_err(fs_info, "target device %s is invalid!", device_path);
202 return PTR_ERR(bdev);
203 }
204
205 filemap_write_and_wait(bdev->bd_inode->i_mapping);
206
207 devices = &fs_info->fs_devices->devices;
208 list_for_each_entry(device, devices, dev_list) {
209 if (device->bdev == bdev) {
210 btrfs_err(fs_info,
211 "target device is in the filesystem!");
212 ret = -EEXIST;
213 goto error;
214 }
215 }
216
217
218 if (i_size_read(bdev->bd_inode) <
219 btrfs_device_get_total_bytes(srcdev)) {
220 btrfs_err(fs_info,
221 "target device is smaller than source device!");
222 ret = -EINVAL;
223 goto error;
224 }
225
226
227 device = btrfs_alloc_device(NULL, &devid, NULL);
228 if (IS_ERR(device)) {
229 ret = PTR_ERR(device);
230 goto error;
231 }
232
233 name = rcu_string_strdup(device_path, GFP_KERNEL);
234 if (!name) {
235 btrfs_free_device(device);
236 ret = -ENOMEM;
237 goto error;
238 }
239 rcu_assign_pointer(device->name, name);
240
241 mutex_lock(&fs_info->fs_devices->device_list_mutex);
242 set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
243 device->generation = 0;
244 device->io_width = fs_info->sectorsize;
245 device->io_align = fs_info->sectorsize;
246 device->sector_size = fs_info->sectorsize;
247 device->total_bytes = btrfs_device_get_total_bytes(srcdev);
248 device->disk_total_bytes = btrfs_device_get_disk_total_bytes(srcdev);
249 device->bytes_used = btrfs_device_get_bytes_used(srcdev);
250 device->commit_total_bytes = srcdev->commit_total_bytes;
251 device->commit_bytes_used = device->bytes_used;
252 device->fs_info = fs_info;
253 device->bdev = bdev;
254 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
255 set_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
256 device->mode = FMODE_EXCL;
257 device->dev_stats_valid = 1;
258 set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE);
259 device->fs_devices = fs_info->fs_devices;
260 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
e93c89c1
SB
273/*
274 * called from commit_transaction. Writes changed device replace state to
275 * disk.
276 */
277int btrfs_run_dev_replace(struct btrfs_trans_handle *trans,
278 struct btrfs_fs_info *fs_info)
279{
280 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
7e79cb86 288 btrfs_dev_replace_read_lock(dev_replace);
e93c89c1
SB
289 if (!dev_replace->is_valid ||
290 !dev_replace->item_needs_writeback) {
7e79cb86 291 btrfs_dev_replace_read_unlock(dev_replace);
e93c89c1
SB
292 return 0;
293 }
7e79cb86 294 btrfs_dev_replace_read_unlock(dev_replace);
e93c89c1
SB
295
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) {
5d163e0e
JM
307 btrfs_warn(fs_info,
308 "error %d while searching for dev_replace item!",
309 ret);
e93c89c1
SB
310 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) {
5d163e0e
JM
328 btrfs_warn(fs_info,
329 "delete too small dev_replace item failed %d!",
330 ret);
e93c89c1
SB
331 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) {
5d163e0e
JM
342 btrfs_warn(fs_info,
343 "insert dev_replace item failed %d!", ret);
e93c89c1
SB
344 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
7e79cb86 352 btrfs_dev_replace_write_lock(dev_replace);
e93c89c1
SB
353 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;
7e79cb86 375 btrfs_dev_replace_write_unlock(dev_replace);
e93c89c1
SB
376
377 btrfs_mark_buffer_dirty(eb);
378
379out:
380 btrfs_free_path(path);
381
382 return ret;
383}
384
3c958bd2
AJ
385static char* btrfs_dev_name(struct btrfs_device *device)
386{
acf18c56 387 if (!device || test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
3c958bd2
AJ
388 return "<missing disk>";
389 else
390 return rcu_str_deref(device->name);
391}
392
da353f6b
DS
393int btrfs_dev_replace_start(struct btrfs_fs_info *fs_info,
394 const char *tgtdev_name, u64 srcdevid, const char *srcdev_name,
395 int read_src)
e93c89c1 396{
2ff7e61e 397 struct btrfs_root *root = fs_info->dev_root;
e93c89c1 398 struct btrfs_trans_handle *trans;
e93c89c1
SB
399 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;
aa144bfe 403 bool need_unlock;
e93c89c1 404
a27a94c2
NB
405 src_device = btrfs_find_device_by_devspec(fs_info, srcdevid,
406 srcdev_name);
407 if (IS_ERR(src_device))
408 return PTR_ERR(src_device);
e93c89c1 409
2ff7e61e 410 ret = btrfs_init_dev_replace_tgtdev(fs_info, tgtdev_name,
1c43366d 411 src_device, &tgt_device);
1c43366d
MX
412 if (ret)
413 return ret;
e93c89c1 414
9e271ae2
AJ
415 /*
416 * Here we commit the transaction to make sure commit_total_bytes
417 * of all the devices are updated.
418 */
419 trans = btrfs_attach_transaction(root);
420 if (!IS_ERR(trans)) {
3a45bb20 421 ret = btrfs_commit_transaction(trans);
9e271ae2
AJ
422 if (ret)
423 return ret;
424 } else if (PTR_ERR(trans) != -ENOENT) {
425 return PTR_ERR(trans);
426 }
427
aa144bfe 428 need_unlock = true;
7e79cb86 429 btrfs_dev_replace_write_lock(dev_replace);
e93c89c1
SB
430 switch (dev_replace->replace_state) {
431 case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
432 case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
433 case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
434 break;
435 case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
436 case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
5c061471 437 ASSERT(0);
b5255456 438 ret = BTRFS_IOCTL_DEV_REPLACE_RESULT_ALREADY_STARTED;
e93c89c1
SB
439 goto leave;
440 }
441
b5255456 442 dev_replace->cont_reading_from_srcdev_mode = read_src;
e93c89c1
SB
443 WARN_ON(!src_device);
444 dev_replace->srcdev = src_device;
e93c89c1
SB
445 dev_replace->tgtdev = tgt_device;
446
fc23c246 447 btrfs_info_in_rcu(fs_info,
ecaeb14b 448 "dev_replace from %s (devid %llu) to %s started",
3c958bd2 449 btrfs_dev_name(src_device),
e93c89c1
SB
450 src_device->devid,
451 rcu_str_deref(tgt_device->name));
452
e93c89c1
SB
453 /*
454 * from now on, the writes to the srcdev are all duplicated to
455 * go to the tgtdev as well (refer to btrfs_map_block()).
456 */
457 dev_replace->replace_state = BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED;
a944442c 458 dev_replace->time_started = ktime_get_real_seconds();
e93c89c1
SB
459 dev_replace->cursor_left = 0;
460 dev_replace->committed_cursor_left = 0;
461 dev_replace->cursor_left_last_write_of_item = 0;
462 dev_replace->cursor_right = 0;
463 dev_replace->is_valid = 1;
464 dev_replace->item_needs_writeback = 1;
7ccefb98
YK
465 atomic64_set(&dev_replace->num_write_errors, 0);
466 atomic64_set(&dev_replace->num_uncorrectable_read_errors, 0);
7e79cb86 467 btrfs_dev_replace_write_unlock(dev_replace);
aa144bfe 468 need_unlock = false;
e93c89c1 469
73416dab
LB
470 ret = btrfs_sysfs_add_device_link(tgt_device->fs_devices, tgt_device);
471 if (ret)
ab8d0fc4 472 btrfs_err(fs_info, "kobj add dev failed %d", ret);
73416dab 473
6374e57a 474 btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1);
e93c89c1
SB
475
476 /* force writing the updated state information to disk */
477 trans = btrfs_start_transaction(root, 0);
478 if (IS_ERR(trans)) {
479 ret = PTR_ERR(trans);
aa144bfe 480 need_unlock = true;
7e79cb86 481 btrfs_dev_replace_write_lock(dev_replace);
5c061471
JM
482 dev_replace->replace_state =
483 BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED;
484 dev_replace->srcdev = NULL;
485 dev_replace->tgtdev = NULL;
e93c89c1
SB
486 goto leave;
487 }
488
3a45bb20 489 ret = btrfs_commit_transaction(trans);
e93c89c1
SB
490 WARN_ON(ret);
491
492 /* the disk copy procedure reuses the scrub code */
493 ret = btrfs_scrub_dev(fs_info, src_device->devid, 0,
7cc8e58d 494 btrfs_device_get_total_bytes(src_device),
e93c89c1
SB
495 &dev_replace->scrub_progress, 0, 1);
496
fc23c246 497 ret = btrfs_dev_replace_finishing(fs_info, ret);
2fc9f6ba 498 if (ret == -EINPROGRESS) {
b5255456 499 ret = BTRFS_IOCTL_DEV_REPLACE_RESULT_SCRUB_INPROGRESS;
2fc9f6ba
EG
500 } else {
501 WARN_ON(ret);
502 }
e93c89c1 503
2fc9f6ba 504 return ret;
e93c89c1
SB
505
506leave:
aa144bfe
DS
507 if (need_unlock)
508 btrfs_dev_replace_write_unlock(dev_replace);
4f5ad7bd 509 btrfs_destroy_dev_replace_tgtdev(tgt_device);
e93c89c1
SB
510 return ret;
511}
512
2ff7e61e 513int btrfs_dev_replace_by_ioctl(struct btrfs_fs_info *fs_info,
b5255456
AJ
514 struct btrfs_ioctl_dev_replace_args *args)
515{
516 int ret;
517
518 switch (args->start.cont_reading_from_srcdev_mode) {
519 case BTRFS_IOCTL_DEV_REPLACE_CONT_READING_FROM_SRCDEV_MODE_ALWAYS:
520 case BTRFS_IOCTL_DEV_REPLACE_CONT_READING_FROM_SRCDEV_MODE_AVOID:
521 break;
522 default:
523 return -EINVAL;
524 }
525
526 if ((args->start.srcdevid == 0 && args->start.srcdev_name[0] == '\0') ||
527 args->start.tgtdev_name[0] == '\0')
528 return -EINVAL;
529
2ff7e61e 530 ret = btrfs_dev_replace_start(fs_info, args->start.tgtdev_name,
b5255456
AJ
531 args->start.srcdevid,
532 args->start.srcdev_name,
533 args->start.cont_reading_from_srcdev_mode);
534 args->result = ret;
535 /* don't warn if EINPROGRESS, someone else might be running scrub */
536 if (ret == BTRFS_IOCTL_DEV_REPLACE_RESULT_SCRUB_INPROGRESS)
537 ret = 0;
538
539 return ret;
540}
541
c404e0dc 542/*
01327610 543 * blocked until all in-flight bios operations are finished.
c404e0dc
MX
544 */
545static void btrfs_rm_dev_replace_blocked(struct btrfs_fs_info *fs_info)
546{
c404e0dc 547 set_bit(BTRFS_FS_STATE_DEV_REPLACING, &fs_info->fs_state);
7f8d236a
DS
548 wait_event(fs_info->dev_replace.replace_wait, !percpu_counter_sum(
549 &fs_info->dev_replace.bio_counter));
c404e0dc
MX
550}
551
552/*
553 * we have removed target device, it is safe to allow new bios request.
554 */
555static void btrfs_rm_dev_replace_unblocked(struct btrfs_fs_info *fs_info)
556{
557 clear_bit(BTRFS_FS_STATE_DEV_REPLACING, &fs_info->fs_state);
7f8d236a 558 wake_up(&fs_info->dev_replace.replace_wait);
c404e0dc
MX
559}
560
e93c89c1
SB
561static int btrfs_dev_replace_finishing(struct btrfs_fs_info *fs_info,
562 int scrub_ret)
563{
564 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
565 struct btrfs_device *tgt_device;
566 struct btrfs_device *src_device;
567 struct btrfs_root *root = fs_info->tree_root;
568 u8 uuid_tmp[BTRFS_UUID_SIZE];
569 struct btrfs_trans_handle *trans;
570 int ret = 0;
571
572 /* don't allow cancel or unmount to disturb the finishing procedure */
573 mutex_lock(&dev_replace->lock_finishing_cancel_unmount);
574
7e79cb86 575 btrfs_dev_replace_read_lock(dev_replace);
e93c89c1
SB
576 /* was the operation canceled, or is it finished? */
577 if (dev_replace->replace_state !=
578 BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED) {
7e79cb86 579 btrfs_dev_replace_read_unlock(dev_replace);
e93c89c1
SB
580 mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
581 return 0;
582 }
583
584 tgt_device = dev_replace->tgtdev;
585 src_device = dev_replace->srcdev;
7e79cb86 586 btrfs_dev_replace_read_unlock(dev_replace);
e93c89c1 587
e93c89c1
SB
588 /*
589 * flush all outstanding I/O and inode extent mappings before the
590 * copy operation is declared as being finished
591 */
82b3e53b 592 ret = btrfs_start_delalloc_roots(fs_info, -1);
3edb2a68
MX
593 if (ret) {
594 mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
595 return ret;
596 }
6374e57a 597 btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1);
e93c89c1
SB
598
599 trans = btrfs_start_transaction(root, 0);
600 if (IS_ERR(trans)) {
601 mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
602 return PTR_ERR(trans);
603 }
3a45bb20 604 ret = btrfs_commit_transaction(trans);
e93c89c1
SB
605 WARN_ON(ret);
606
607 /* keep away write_all_supers() during the finishing procedure */
0b246afa
JM
608 mutex_lock(&fs_info->fs_devices->device_list_mutex);
609 mutex_lock(&fs_info->chunk_mutex);
7e79cb86 610 btrfs_dev_replace_write_lock(dev_replace);
e93c89c1
SB
611 dev_replace->replace_state =
612 scrub_ret ? BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED
613 : BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED;
614 dev_replace->tgtdev = NULL;
615 dev_replace->srcdev = NULL;
a944442c 616 dev_replace->time_stopped = ktime_get_real_seconds();
e93c89c1
SB
617 dev_replace->item_needs_writeback = 1;
618
c404e0dc
MX
619 /* replace old device with new one in mapping tree */
620 if (!scrub_ret) {
621 btrfs_dev_replace_update_device_in_mapping_tree(fs_info,
622 src_device,
623 tgt_device);
624 } else {
0b246afa
JM
625 btrfs_err_in_rcu(fs_info,
626 "btrfs_scrub_dev(%s, %llu, %s) failed %d",
3c958bd2 627 btrfs_dev_name(src_device),
0b246afa
JM
628 src_device->devid,
629 rcu_str_deref(tgt_device->name), scrub_ret);
7e79cb86 630 btrfs_dev_replace_write_unlock(dev_replace);
0b246afa
JM
631 mutex_unlock(&fs_info->chunk_mutex);
632 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
ae6529c3 633 btrfs_rm_dev_replace_blocked(fs_info);
e93c89c1 634 if (tgt_device)
4f5ad7bd 635 btrfs_destroy_dev_replace_tgtdev(tgt_device);
ae6529c3 636 btrfs_rm_dev_replace_unblocked(fs_info);
e93c89c1
SB
637 mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
638
2fc9f6ba 639 return scrub_ret;
e93c89c1
SB
640 }
641
0b246afa
JM
642 btrfs_info_in_rcu(fs_info,
643 "dev_replace from %s (devid %llu) to %s finished",
3c958bd2 644 btrfs_dev_name(src_device),
0b246afa
JM
645 src_device->devid,
646 rcu_str_deref(tgt_device->name));
401e29c1 647 clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &tgt_device->dev_state);
e93c89c1
SB
648 tgt_device->devid = src_device->devid;
649 src_device->devid = BTRFS_DEV_REPLACE_DEVID;
e93c89c1
SB
650 memcpy(uuid_tmp, tgt_device->uuid, sizeof(uuid_tmp));
651 memcpy(tgt_device->uuid, src_device->uuid, sizeof(tgt_device->uuid));
652 memcpy(src_device->uuid, uuid_tmp, sizeof(src_device->uuid));
7cc8e58d
MX
653 btrfs_device_set_total_bytes(tgt_device, src_device->total_bytes);
654 btrfs_device_set_disk_total_bytes(tgt_device,
655 src_device->disk_total_bytes);
656 btrfs_device_set_bytes_used(tgt_device, src_device->bytes_used);
935e5cc9
MX
657 ASSERT(list_empty(&src_device->resized_list));
658 tgt_device->commit_total_bytes = src_device->commit_total_bytes;
ce7213c7 659 tgt_device->commit_bytes_used = src_device->bytes_used;
88acff64 660
d6507cf1 661 btrfs_assign_next_active_device(src_device, tgt_device);
88acff64 662
e93c89c1 663 list_add(&tgt_device->dev_alloc_list, &fs_info->fs_devices->alloc_list);
82372bc8 664 fs_info->fs_devices->rw_devices++;
e93c89c1 665
7e79cb86 666 btrfs_dev_replace_write_unlock(dev_replace);
12b894cb 667
c404e0dc
MX
668 btrfs_rm_dev_replace_blocked(fs_info);
669
68a9db5f 670 btrfs_rm_dev_replace_remove_srcdev(src_device);
1357272f 671
c404e0dc
MX
672 btrfs_rm_dev_replace_unblocked(fs_info);
673
1e7e1f9e
MT
674 /*
675 * Increment dev_stats_ccnt so that btrfs_run_dev_stats() will
676 * update on-disk dev stats value during commit transaction
677 */
678 atomic_inc(&tgt_device->dev_stats_ccnt);
679
e93c89c1
SB
680 /*
681 * this is again a consistent state where no dev_replace procedure
682 * is running, the target device is part of the filesystem, the
683 * source device is not part of the filesystem anymore and its 1st
684 * superblock is scratched out so that it is no longer marked to
685 * belong to this filesystem.
686 */
0b246afa
JM
687 mutex_unlock(&fs_info->chunk_mutex);
688 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
e93c89c1 689
084b6e7c 690 /* replace the sysfs entry */
32576040 691 btrfs_sysfs_rm_device_link(fs_info->fs_devices, src_device);
084b6e7c
QW
692 btrfs_rm_dev_replace_free_srcdev(fs_info, src_device);
693
e93c89c1
SB
694 /* write back the superblocks */
695 trans = btrfs_start_transaction(root, 0);
696 if (!IS_ERR(trans))
3a45bb20 697 btrfs_commit_transaction(trans);
e93c89c1
SB
698
699 mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
700
701 return 0;
702}
703
704static void btrfs_dev_replace_update_device_in_mapping_tree(
705 struct btrfs_fs_info *fs_info,
706 struct btrfs_device *srcdev,
707 struct btrfs_device *tgtdev)
708{
709 struct extent_map_tree *em_tree = &fs_info->mapping_tree.map_tree;
710 struct extent_map *em;
711 struct map_lookup *map;
712 u64 start = 0;
713 int i;
714
715 write_lock(&em_tree->lock);
716 do {
717 em = lookup_extent_mapping(em_tree, start, (u64)-1);
718 if (!em)
719 break;
95617d69 720 map = em->map_lookup;
e93c89c1
SB
721 for (i = 0; i < map->num_stripes; i++)
722 if (srcdev == map->stripes[i].dev)
723 map->stripes[i].dev = tgtdev;
724 start = em->start + em->len;
725 free_extent_map(em);
726 } while (start);
727 write_unlock(&em_tree->lock);
728}
729
74b595fe
DS
730/*
731 * Read progress of device replace status according to the state and last
732 * stored position. The value format is the same as for
733 * btrfs_dev_replace::progress_1000
734 */
735static u64 btrfs_dev_replace_progress(struct btrfs_fs_info *fs_info)
736{
737 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
738 u64 ret = 0;
739
740 switch (dev_replace->replace_state) {
741 case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
742 case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
743 ret = 0;
744 break;
745 case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
746 ret = 1000;
747 break;
748 case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
749 case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
750 ret = div64_u64(dev_replace->cursor_left,
751 div_u64(btrfs_device_get_total_bytes(
752 dev_replace->srcdev), 1000));
753 break;
754 }
755
756 return ret;
757}
758
e93c89c1
SB
759void btrfs_dev_replace_status(struct btrfs_fs_info *fs_info,
760 struct btrfs_ioctl_dev_replace_args *args)
761{
762 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
763
7e79cb86 764 btrfs_dev_replace_read_lock(dev_replace);
e93c89c1
SB
765 /* even if !dev_replace_is_valid, the values are good enough for
766 * the replace_status ioctl */
767 args->result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR;
768 args->status.replace_state = dev_replace->replace_state;
769 args->status.time_started = dev_replace->time_started;
770 args->status.time_stopped = dev_replace->time_stopped;
771 args->status.num_write_errors =
772 atomic64_read(&dev_replace->num_write_errors);
773 args->status.num_uncorrectable_read_errors =
774 atomic64_read(&dev_replace->num_uncorrectable_read_errors);
74b595fe 775 args->status.progress_1000 = btrfs_dev_replace_progress(fs_info);
7e79cb86 776 btrfs_dev_replace_read_unlock(dev_replace);
e93c89c1
SB
777}
778
18e67c73 779int btrfs_dev_replace_cancel(struct btrfs_fs_info *fs_info)
e93c89c1
SB
780{
781 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
782 struct btrfs_device *tgt_device = NULL;
8f2ceaa7 783 struct btrfs_device *src_device = NULL;
e93c89c1
SB
784 struct btrfs_trans_handle *trans;
785 struct btrfs_root *root = fs_info->tree_root;
18e67c73 786 int result;
e93c89c1
SB
787 int ret;
788
bc98a42c 789 if (sb_rdonly(fs_info->sb))
e649e587
ID
790 return -EROFS;
791
e93c89c1 792 mutex_lock(&dev_replace->lock_finishing_cancel_unmount);
7e79cb86 793 btrfs_dev_replace_write_lock(dev_replace);
e93c89c1
SB
794 switch (dev_replace->replace_state) {
795 case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
796 case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
797 case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
798 result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NOT_STARTED;
7e79cb86 799 btrfs_dev_replace_write_unlock(dev_replace);
e93c89c1
SB
800 goto leave;
801 case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
802 case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
803 result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR;
804 tgt_device = dev_replace->tgtdev;
8f2ceaa7 805 src_device = dev_replace->srcdev;
e93c89c1
SB
806 dev_replace->tgtdev = NULL;
807 dev_replace->srcdev = NULL;
808 break;
809 }
810 dev_replace->replace_state = BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED;
a944442c 811 dev_replace->time_stopped = ktime_get_real_seconds();
e93c89c1 812 dev_replace->item_needs_writeback = 1;
7e79cb86 813 btrfs_dev_replace_write_unlock(dev_replace);
e93c89c1
SB
814 btrfs_scrub_cancel(fs_info);
815
816 trans = btrfs_start_transaction(root, 0);
817 if (IS_ERR(trans)) {
818 mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
819 return PTR_ERR(trans);
820 }
3a45bb20 821 ret = btrfs_commit_transaction(trans);
e93c89c1 822 WARN_ON(ret);
8f2ceaa7
AJ
823
824 btrfs_info_in_rcu(fs_info,
825 "dev_replace from %s (devid %llu) to %s canceled",
826 btrfs_dev_name(src_device), src_device->devid,
827 btrfs_dev_name(tgt_device));
828
e93c89c1 829 if (tgt_device)
4f5ad7bd 830 btrfs_destroy_dev_replace_tgtdev(tgt_device);
e93c89c1
SB
831
832leave:
833 mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
834 return result;
835}
836
837void btrfs_dev_replace_suspend_for_unmount(struct btrfs_fs_info *fs_info)
838{
839 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
840
841 mutex_lock(&dev_replace->lock_finishing_cancel_unmount);
7e79cb86 842 btrfs_dev_replace_write_lock(dev_replace);
e93c89c1
SB
843 switch (dev_replace->replace_state) {
844 case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
845 case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
846 case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
847 case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
848 break;
849 case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
850 dev_replace->replace_state =
851 BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED;
a944442c 852 dev_replace->time_stopped = ktime_get_real_seconds();
e93c89c1 853 dev_replace->item_needs_writeback = 1;
efe120a0 854 btrfs_info(fs_info, "suspending dev_replace for unmount");
e93c89c1
SB
855 break;
856 }
857
7e79cb86 858 btrfs_dev_replace_write_unlock(dev_replace);
e93c89c1
SB
859 mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
860}
861
862/* resume dev_replace procedure that was interrupted by unmount */
863int btrfs_resume_dev_replace_async(struct btrfs_fs_info *fs_info)
864{
865 struct task_struct *task;
866 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
867
7e79cb86 868 btrfs_dev_replace_write_lock(dev_replace);
e93c89c1
SB
869 switch (dev_replace->replace_state) {
870 case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
871 case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
872 case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
7e79cb86 873 btrfs_dev_replace_write_unlock(dev_replace);
e93c89c1
SB
874 return 0;
875 case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
876 break;
877 case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
878 dev_replace->replace_state =
879 BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED;
880 break;
881 }
882 if (!dev_replace->tgtdev || !dev_replace->tgtdev->bdev) {
efe120a0 883 btrfs_info(fs_info,
5d163e0e
JM
884 "cannot continue dev_replace, tgtdev is missing");
885 btrfs_info(fs_info,
886 "you may cancel the operation after 'mount -o degraded'");
7e79cb86 887 btrfs_dev_replace_write_unlock(dev_replace);
e93c89c1
SB
888 return 0;
889 }
7e79cb86 890 btrfs_dev_replace_write_unlock(dev_replace);
e93c89c1 891
010a47bd
DS
892 /*
893 * This could collide with a paused balance, but the exclusive op logic
894 * should never allow both to start and pause. We don't want to allow
895 * dev-replace to start anyway.
896 */
897 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
898 btrfs_info(fs_info,
899 "cannot resume dev-replace, other exclusive operation running");
900 return 0;
901 }
902
e93c89c1 903 task = kthread_run(btrfs_dev_replace_kthread, fs_info, "btrfs-devrepl");
8c6ffba0 904 return PTR_ERR_OR_ZERO(task);
e93c89c1
SB
905}
906
907static int btrfs_dev_replace_kthread(void *data)
908{
909 struct btrfs_fs_info *fs_info = data;
910 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
e93c89c1 911 u64 progress;
00251a52 912 int ret;
e93c89c1 913
f1b8a1e8
DS
914 progress = btrfs_dev_replace_progress(fs_info);
915 progress = div_u64(progress, 10);
916 btrfs_info_in_rcu(fs_info,
3c958bd2
AJ
917 "continuing dev_replace from %s (devid %llu) to target %s @%u%%",
918 btrfs_dev_name(dev_replace->srcdev),
f1b8a1e8 919 dev_replace->srcdev->devid,
3c958bd2 920 btrfs_dev_name(dev_replace->tgtdev),
f1b8a1e8
DS
921 (unsigned int)progress);
922
e93c89c1
SB
923 ret = btrfs_scrub_dev(fs_info, dev_replace->srcdev->devid,
924 dev_replace->committed_cursor_left,
7cc8e58d 925 btrfs_device_get_total_bytes(dev_replace->srcdev),
e93c89c1
SB
926 &dev_replace->scrub_progress, 0, 1);
927 ret = btrfs_dev_replace_finishing(fs_info, ret);
928 WARN_ON(ret);
00251a52
DS
929
930 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
e93c89c1
SB
931 return 0;
932}
933
934int btrfs_dev_replace_is_ongoing(struct btrfs_dev_replace *dev_replace)
935{
936 if (!dev_replace->is_valid)
937 return 0;
938
939 switch (dev_replace->replace_state) {
940 case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
941 case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
942 case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
943 return 0;
944 case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
945 case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
946 /*
947 * return true even if tgtdev is missing (this is
948 * something that can happen if the dev_replace
949 * procedure is suspended by an umount and then
950 * the tgtdev is missing (or "btrfs dev scan") was
951 * not called and the the filesystem is remounted
952 * in degraded state. This does not stop the
953 * dev_replace procedure. It needs to be canceled
bb7ab3b9 954 * manually if the cancellation is wanted.
e93c89c1
SB
955 */
956 break;
957 }
958 return 1;
959}
960
7e79cb86 961void btrfs_dev_replace_read_lock(struct btrfs_dev_replace *dev_replace)
e93c89c1 962{
7e79cb86 963 read_lock(&dev_replace->lock);
7e79cb86
DS
964}
965
966void btrfs_dev_replace_read_unlock(struct btrfs_dev_replace *dev_replace)
967{
7e79cb86 968 read_unlock(&dev_replace->lock);
73beece9 969}
e93c89c1 970
7e79cb86 971void btrfs_dev_replace_write_lock(struct btrfs_dev_replace *dev_replace)
73beece9 972{
7e79cb86
DS
973again:
974 wait_event(dev_replace->read_lock_wq,
975 atomic_read(&dev_replace->blocking_readers) == 0);
976 write_lock(&dev_replace->lock);
977 if (atomic_read(&dev_replace->blocking_readers)) {
73beece9 978 write_unlock(&dev_replace->lock);
7e79cb86 979 goto again;
e93c89c1 980 }
73beece9 981}
e93c89c1 982
7e79cb86
DS
983void btrfs_dev_replace_write_unlock(struct btrfs_dev_replace *dev_replace)
984{
7e79cb86
DS
985 write_unlock(&dev_replace->lock);
986}
987
73beece9
LB
988/* inc blocking cnt and release read lock */
989void btrfs_dev_replace_set_lock_blocking(
990 struct btrfs_dev_replace *dev_replace)
991{
992 /* only set blocking for read lock */
73beece9
LB
993 atomic_inc(&dev_replace->blocking_readers);
994 read_unlock(&dev_replace->lock);
e93c89c1
SB
995}
996
c404e0dc
MX
997void btrfs_bio_counter_inc_noblocked(struct btrfs_fs_info *fs_info)
998{
7f8d236a 999 percpu_counter_inc(&fs_info->dev_replace.bio_counter);
c404e0dc
MX
1000}
1001
4245215d 1002void btrfs_bio_counter_sub(struct btrfs_fs_info *fs_info, s64 amount)
c404e0dc 1003{
7f8d236a
DS
1004 percpu_counter_sub(&fs_info->dev_replace.bio_counter, amount);
1005 cond_wake_up_nomb(&fs_info->dev_replace.replace_wait);
c404e0dc
MX
1006}
1007
1008void btrfs_bio_counter_inc_blocked(struct btrfs_fs_info *fs_info)
1009{
09dd7a01 1010 while (1) {
7f8d236a 1011 percpu_counter_inc(&fs_info->dev_replace.bio_counter);
09dd7a01
ZL
1012 if (likely(!test_bit(BTRFS_FS_STATE_DEV_REPLACING,
1013 &fs_info->fs_state)))
1014 break;
1015
c404e0dc 1016 btrfs_bio_counter_dec(fs_info);
7f8d236a 1017 wait_event(fs_info->dev_replace.replace_wait,
c404e0dc
MX
1018 !test_bit(BTRFS_FS_STATE_DEV_REPLACING,
1019 &fs_info->fs_state));
c404e0dc 1020 }
c404e0dc 1021}