blob: db4d08c651311581a7f2c739767128f024b46aee [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>
9#include <linux/buffer_head.h>
10#include <linux/blkdev.h>
11#include <linux/random.h>
12#include <linux/iocontext.h>
13#include <linux/capability.h>
14#include <linux/kthread.h>
15#include <linux/math64.h>
16#include <asm/div64.h>
Stefan Behrense93c89c2012-11-05 17:33:06 +010017#include "ctree.h"
18#include "extent_map.h"
19#include "disk-io.h"
20#include "transaction.h"
21#include "print-tree.h"
22#include "volumes.h"
23#include "async-thread.h"
24#include "check-integrity.h"
25#include "rcu-string.h"
26#include "dev-replace.h"
Anand Jain49c6f732014-06-03 11:36:02 +080027#include "sysfs.h"
Stefan Behrense93c89c2012-11-05 17:33:06 +010028
Stefan Behrense93c89c2012-11-05 17:33:06 +010029static int btrfs_dev_replace_finishing(struct btrfs_fs_info *fs_info,
30 int scrub_ret);
31static void btrfs_dev_replace_update_device_in_mapping_tree(
32 struct btrfs_fs_info *fs_info,
33 struct btrfs_device *srcdev,
34 struct btrfs_device *tgtdev);
Stefan Behrense93c89c2012-11-05 17:33:06 +010035static int btrfs_dev_replace_kthread(void *data);
Stefan Behrense93c89c2012-11-05 17:33:06 +010036
37int btrfs_init_dev_replace(struct btrfs_fs_info *fs_info)
38{
39 struct btrfs_key key;
40 struct btrfs_root *dev_root = fs_info->dev_root;
41 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
42 struct extent_buffer *eb;
43 int slot;
44 int ret = 0;
45 struct btrfs_path *path = NULL;
46 int item_size;
47 struct btrfs_dev_replace_item *ptr;
48 u64 src_devid;
49
50 path = btrfs_alloc_path();
51 if (!path) {
52 ret = -ENOMEM;
53 goto out;
54 }
55
56 key.objectid = 0;
57 key.type = BTRFS_DEV_REPLACE_KEY;
58 key.offset = 0;
59 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
60 if (ret) {
61no_valid_dev_replace_entry_found:
62 ret = 0;
63 dev_replace->replace_state =
64 BTRFS_DEV_REPLACE_ITEM_STATE_NEVER_STARTED;
65 dev_replace->cont_reading_from_srcdev_mode =
66 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_ALWAYS;
67 dev_replace->replace_state = 0;
68 dev_replace->time_started = 0;
69 dev_replace->time_stopped = 0;
70 atomic64_set(&dev_replace->num_write_errors, 0);
71 atomic64_set(&dev_replace->num_uncorrectable_read_errors, 0);
72 dev_replace->cursor_left = 0;
73 dev_replace->committed_cursor_left = 0;
74 dev_replace->cursor_left_last_write_of_item = 0;
75 dev_replace->cursor_right = 0;
76 dev_replace->srcdev = NULL;
77 dev_replace->tgtdev = NULL;
78 dev_replace->is_valid = 0;
79 dev_replace->item_needs_writeback = 0;
80 goto out;
81 }
82 slot = path->slots[0];
83 eb = path->nodes[0];
84 item_size = btrfs_item_size_nr(eb, slot);
85 ptr = btrfs_item_ptr(eb, slot, struct btrfs_dev_replace_item);
86
87 if (item_size != sizeof(struct btrfs_dev_replace_item)) {
Frank Holtonefe120a2013-12-20 11:37:06 -050088 btrfs_warn(fs_info,
89 "dev_replace entry found has unexpected size, ignore entry");
Stefan Behrense93c89c2012-11-05 17:33:06 +010090 goto no_valid_dev_replace_entry_found;
91 }
92
93 src_devid = btrfs_dev_replace_src_devid(eb, ptr);
94 dev_replace->cont_reading_from_srcdev_mode =
95 btrfs_dev_replace_cont_reading_from_srcdev_mode(eb, ptr);
96 dev_replace->replace_state = btrfs_dev_replace_replace_state(eb, ptr);
97 dev_replace->time_started = btrfs_dev_replace_time_started(eb, ptr);
98 dev_replace->time_stopped =
99 btrfs_dev_replace_time_stopped(eb, ptr);
100 atomic64_set(&dev_replace->num_write_errors,
101 btrfs_dev_replace_num_write_errors(eb, ptr));
102 atomic64_set(&dev_replace->num_uncorrectable_read_errors,
103 btrfs_dev_replace_num_uncorrectable_read_errors(eb, ptr));
104 dev_replace->cursor_left = btrfs_dev_replace_cursor_left(eb, ptr);
105 dev_replace->committed_cursor_left = dev_replace->cursor_left;
106 dev_replace->cursor_left_last_write_of_item = dev_replace->cursor_left;
107 dev_replace->cursor_right = btrfs_dev_replace_cursor_right(eb, ptr);
108 dev_replace->is_valid = 1;
109
110 dev_replace->item_needs_writeback = 0;
111 switch (dev_replace->replace_state) {
112 case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
113 case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
114 case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
115 dev_replace->srcdev = NULL;
116 dev_replace->tgtdev = NULL;
117 break;
118 case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
119 case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
120 dev_replace->srcdev = btrfs_find_device(fs_info, src_devid,
121 NULL, NULL);
122 dev_replace->tgtdev = btrfs_find_device(fs_info,
123 BTRFS_DEV_REPLACE_DEVID,
124 NULL, NULL);
125 /*
126 * allow 'btrfs dev replace_cancel' if src/tgt device is
127 * missing
128 */
129 if (!dev_replace->srcdev &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400130 !btrfs_test_opt(fs_info, DEGRADED)) {
Stefan Behrense93c89c2012-11-05 17:33:06 +0100131 ret = -EIO;
Frank Holtonefe120a2013-12-20 11:37:06 -0500132 btrfs_warn(fs_info,
133 "cannot mount because device replace operation is ongoing and");
134 btrfs_warn(fs_info,
135 "srcdev (devid %llu) is missing, need to run 'btrfs dev scan'?",
136 src_devid);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100137 }
138 if (!dev_replace->tgtdev &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400139 !btrfs_test_opt(fs_info, DEGRADED)) {
Stefan Behrense93c89c2012-11-05 17:33:06 +0100140 ret = -EIO;
Frank Holtonefe120a2013-12-20 11:37:06 -0500141 btrfs_warn(fs_info,
142 "cannot mount because device replace operation is ongoing and");
143 btrfs_warn(fs_info,
144 "tgtdev (devid %llu) is missing, need to run 'btrfs dev scan'?",
Geert Uytterhoeven6e71c472013-08-20 13:20:08 +0200145 BTRFS_DEV_REPLACE_DEVID);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100146 }
147 if (dev_replace->tgtdev) {
148 if (dev_replace->srcdev) {
149 dev_replace->tgtdev->total_bytes =
150 dev_replace->srcdev->total_bytes;
151 dev_replace->tgtdev->disk_total_bytes =
152 dev_replace->srcdev->disk_total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +0800153 dev_replace->tgtdev->commit_total_bytes =
154 dev_replace->srcdev->commit_total_bytes;
Stefan Behrense93c89c2012-11-05 17:33:06 +0100155 dev_replace->tgtdev->bytes_used =
156 dev_replace->srcdev->bytes_used;
Miao Xiece7213c2014-09-03 21:35:34 +0800157 dev_replace->tgtdev->commit_bytes_used =
158 dev_replace->srcdev->commit_bytes_used;
Stefan Behrense93c89c2012-11-05 17:33:06 +0100159 }
Anand Jain401e29c2017-12-04 12:54:55 +0800160 set_bit(BTRFS_DEV_STATE_REPLACE_TGT,
161 &dev_replace->tgtdev->dev_state);
Anand Jain15fc1282018-02-12 23:36:25 +0800162
163 WARN_ON(fs_info->fs_devices->rw_devices == 0);
164 dev_replace->tgtdev->io_width = fs_info->sectorsize;
165 dev_replace->tgtdev->io_align = fs_info->sectorsize;
166 dev_replace->tgtdev->sector_size = fs_info->sectorsize;
167 dev_replace->tgtdev->fs_info = fs_info;
168 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
169 &dev_replace->tgtdev->dev_state);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100170 }
171 break;
172 }
173
174out:
Tsutomu Itoh527afb42015-08-19 14:55:00 +0900175 btrfs_free_path(path);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100176 return ret;
177}
178
179/*
180 * called from commit_transaction. Writes changed device replace state to
181 * disk.
182 */
183int btrfs_run_dev_replace(struct btrfs_trans_handle *trans,
184 struct btrfs_fs_info *fs_info)
185{
186 int ret;
187 struct btrfs_root *dev_root = fs_info->dev_root;
188 struct btrfs_path *path;
189 struct btrfs_key key;
190 struct extent_buffer *eb;
191 struct btrfs_dev_replace_item *ptr;
192 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
193
David Sterba7e79cb82018-03-24 02:11:38 +0100194 btrfs_dev_replace_read_lock(dev_replace);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100195 if (!dev_replace->is_valid ||
196 !dev_replace->item_needs_writeback) {
David Sterba7e79cb82018-03-24 02:11:38 +0100197 btrfs_dev_replace_read_unlock(dev_replace);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100198 return 0;
199 }
David Sterba7e79cb82018-03-24 02:11:38 +0100200 btrfs_dev_replace_read_unlock(dev_replace);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100201
202 key.objectid = 0;
203 key.type = BTRFS_DEV_REPLACE_KEY;
204 key.offset = 0;
205
206 path = btrfs_alloc_path();
207 if (!path) {
208 ret = -ENOMEM;
209 goto out;
210 }
211 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
212 if (ret < 0) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400213 btrfs_warn(fs_info,
214 "error %d while searching for dev_replace item!",
215 ret);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100216 goto out;
217 }
218
219 if (ret == 0 &&
220 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
221 /*
222 * need to delete old one and insert a new one.
223 * Since no attempt is made to recover any old state, if the
224 * dev_replace state is 'running', the data on the target
225 * drive is lost.
226 * It would be possible to recover the state: just make sure
227 * that the beginning of the item is never changed and always
228 * contains all the essential information. Then read this
229 * minimal set of information and use it as a base for the
230 * new state.
231 */
232 ret = btrfs_del_item(trans, dev_root, path);
233 if (ret != 0) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400234 btrfs_warn(fs_info,
235 "delete too small dev_replace item failed %d!",
236 ret);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100237 goto out;
238 }
239 ret = 1;
240 }
241
242 if (ret == 1) {
243 /* need to insert a new item */
244 btrfs_release_path(path);
245 ret = btrfs_insert_empty_item(trans, dev_root, path,
246 &key, sizeof(*ptr));
247 if (ret < 0) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400248 btrfs_warn(fs_info,
249 "insert dev_replace item failed %d!", ret);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100250 goto out;
251 }
252 }
253
254 eb = path->nodes[0];
255 ptr = btrfs_item_ptr(eb, path->slots[0],
256 struct btrfs_dev_replace_item);
257
David Sterba7e79cb82018-03-24 02:11:38 +0100258 btrfs_dev_replace_write_lock(dev_replace);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100259 if (dev_replace->srcdev)
260 btrfs_set_dev_replace_src_devid(eb, ptr,
261 dev_replace->srcdev->devid);
262 else
263 btrfs_set_dev_replace_src_devid(eb, ptr, (u64)-1);
264 btrfs_set_dev_replace_cont_reading_from_srcdev_mode(eb, ptr,
265 dev_replace->cont_reading_from_srcdev_mode);
266 btrfs_set_dev_replace_replace_state(eb, ptr,
267 dev_replace->replace_state);
268 btrfs_set_dev_replace_time_started(eb, ptr, dev_replace->time_started);
269 btrfs_set_dev_replace_time_stopped(eb, ptr, dev_replace->time_stopped);
270 btrfs_set_dev_replace_num_write_errors(eb, ptr,
271 atomic64_read(&dev_replace->num_write_errors));
272 btrfs_set_dev_replace_num_uncorrectable_read_errors(eb, ptr,
273 atomic64_read(&dev_replace->num_uncorrectable_read_errors));
274 dev_replace->cursor_left_last_write_of_item =
275 dev_replace->cursor_left;
276 btrfs_set_dev_replace_cursor_left(eb, ptr,
277 dev_replace->cursor_left_last_write_of_item);
278 btrfs_set_dev_replace_cursor_right(eb, ptr,
279 dev_replace->cursor_right);
280 dev_replace->item_needs_writeback = 0;
David Sterba7e79cb82018-03-24 02:11:38 +0100281 btrfs_dev_replace_write_unlock(dev_replace);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100282
283 btrfs_mark_buffer_dirty(eb);
284
285out:
286 btrfs_free_path(path);
287
288 return ret;
289}
290
291void btrfs_after_dev_replace_commit(struct btrfs_fs_info *fs_info)
292{
293 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
294
295 dev_replace->committed_cursor_left =
296 dev_replace->cursor_left_last_write_of_item;
297}
298
Anand Jain3c958bd2017-11-28 10:43:10 +0800299static char* btrfs_dev_name(struct btrfs_device *device)
300{
Anand Jainacf18c52018-02-24 19:43:56 +0800301 if (!device || test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
Anand Jain3c958bd2017-11-28 10:43:10 +0800302 return "<missing disk>";
303 else
304 return rcu_str_deref(device->name);
305}
306
David Sterbada353f62017-02-14 17:55:53 +0100307int btrfs_dev_replace_start(struct btrfs_fs_info *fs_info,
308 const char *tgtdev_name, u64 srcdevid, const char *srcdev_name,
309 int read_src)
Stefan Behrense93c89c2012-11-05 17:33:06 +0100310{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400311 struct btrfs_root *root = fs_info->dev_root;
Stefan Behrense93c89c2012-11-05 17:33:06 +0100312 struct btrfs_trans_handle *trans;
Stefan Behrense93c89c2012-11-05 17:33:06 +0100313 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
314 int ret;
315 struct btrfs_device *tgt_device = NULL;
316 struct btrfs_device *src_device = NULL;
317
Miao Xie935e5cc2014-09-03 21:35:33 +0800318 /* the disk copy procedure reuses the scrub code */
Stefan Behrense93c89c2012-11-05 17:33:06 +0100319 mutex_lock(&fs_info->volume_mutex);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400320 ret = btrfs_find_device_by_devspec(fs_info, srcdevid,
Anand Jainb5255452016-03-24 18:48:14 +0800321 srcdev_name, &src_device);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100322 if (ret) {
Miao Xie1c433662014-09-03 21:35:32 +0800323 mutex_unlock(&fs_info->volume_mutex);
324 return ret;
Stefan Behrense93c89c2012-11-05 17:33:06 +0100325 }
326
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400327 ret = btrfs_init_dev_replace_tgtdev(fs_info, tgtdev_name,
Miao Xie1c433662014-09-03 21:35:32 +0800328 src_device, &tgt_device);
329 mutex_unlock(&fs_info->volume_mutex);
330 if (ret)
331 return ret;
Stefan Behrense93c89c2012-11-05 17:33:06 +0100332
Anand Jain9e271ae2015-08-14 18:33:02 +0800333 /*
334 * Here we commit the transaction to make sure commit_total_bytes
335 * of all the devices are updated.
336 */
337 trans = btrfs_attach_transaction(root);
338 if (!IS_ERR(trans)) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400339 ret = btrfs_commit_transaction(trans);
Anand Jain9e271ae2015-08-14 18:33:02 +0800340 if (ret)
341 return ret;
342 } else if (PTR_ERR(trans) != -ENOENT) {
343 return PTR_ERR(trans);
344 }
345
David Sterba7e79cb82018-03-24 02:11:38 +0100346 btrfs_dev_replace_write_lock(dev_replace);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100347 switch (dev_replace->replace_state) {
348 case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
349 case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
350 case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
351 break;
352 case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
353 case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
Anand Jainb5255452016-03-24 18:48:14 +0800354 ret = BTRFS_IOCTL_DEV_REPLACE_RESULT_ALREADY_STARTED;
Stefan Behrense93c89c2012-11-05 17:33:06 +0100355 goto leave;
356 }
357
Anand Jainb5255452016-03-24 18:48:14 +0800358 dev_replace->cont_reading_from_srcdev_mode = read_src;
Stefan Behrense93c89c2012-11-05 17:33:06 +0100359 WARN_ON(!src_device);
360 dev_replace->srcdev = src_device;
361 WARN_ON(!tgt_device);
362 dev_replace->tgtdev = tgt_device;
363
Anand Jainfc23c242016-03-24 18:48:12 +0800364 btrfs_info_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +0200365 "dev_replace from %s (devid %llu) to %s started",
Anand Jain3c958bd2017-11-28 10:43:10 +0800366 btrfs_dev_name(src_device),
Stefan Behrense93c89c2012-11-05 17:33:06 +0100367 src_device->devid,
368 rcu_str_deref(tgt_device->name));
369
Stefan Behrense93c89c2012-11-05 17:33:06 +0100370 /*
371 * from now on, the writes to the srcdev are all duplicated to
372 * go to the tgtdev as well (refer to btrfs_map_block()).
373 */
374 dev_replace->replace_state = BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED;
Zach Brown4546bca2013-10-16 12:10:32 -0700375 dev_replace->time_started = get_seconds();
Stefan Behrense93c89c2012-11-05 17:33:06 +0100376 dev_replace->cursor_left = 0;
377 dev_replace->committed_cursor_left = 0;
378 dev_replace->cursor_left_last_write_of_item = 0;
379 dev_replace->cursor_right = 0;
380 dev_replace->is_valid = 1;
381 dev_replace->item_needs_writeback = 1;
Yauhen Kharuzhy7ccefb92016-03-29 14:17:48 -0700382 atomic64_set(&dev_replace->num_write_errors, 0);
383 atomic64_set(&dev_replace->num_uncorrectable_read_errors, 0);
David Sterba7e79cb82018-03-24 02:11:38 +0100384 btrfs_dev_replace_write_unlock(dev_replace);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100385
Liu Bo73416da2015-08-14 18:33:07 +0800386 ret = btrfs_sysfs_add_device_link(tgt_device->fs_devices, tgt_device);
387 if (ret)
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -0400388 btrfs_err(fs_info, "kobj add dev failed %d", ret);
Liu Bo73416da2015-08-14 18:33:07 +0800389
Chris Mason6374e57a2017-06-23 09:48:21 -0700390 btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100391
392 /* force writing the updated state information to disk */
393 trans = btrfs_start_transaction(root, 0);
394 if (IS_ERR(trans)) {
395 ret = PTR_ERR(trans);
David Sterba7e79cb82018-03-24 02:11:38 +0100396 btrfs_dev_replace_write_lock(dev_replace);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100397 goto leave;
398 }
399
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400400 ret = btrfs_commit_transaction(trans);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100401 WARN_ON(ret);
402
403 /* the disk copy procedure reuses the scrub code */
404 ret = btrfs_scrub_dev(fs_info, src_device->devid, 0,
Miao Xie7cc8e582014-09-03 21:35:38 +0800405 btrfs_device_get_total_bytes(src_device),
Stefan Behrense93c89c2012-11-05 17:33:06 +0100406 &dev_replace->scrub_progress, 0, 1);
407
Anand Jainfc23c242016-03-24 18:48:12 +0800408 ret = btrfs_dev_replace_finishing(fs_info, ret);
Eryu Guan2fc9f6b2014-10-13 12:42:12 +0800409 if (ret == -EINPROGRESS) {
Anand Jainb5255452016-03-24 18:48:14 +0800410 ret = BTRFS_IOCTL_DEV_REPLACE_RESULT_SCRUB_INPROGRESS;
Eryu Guan2fc9f6b2014-10-13 12:42:12 +0800411 } else {
412 WARN_ON(ret);
413 }
Stefan Behrense93c89c2012-11-05 17:33:06 +0100414
Eryu Guan2fc9f6b2014-10-13 12:42:12 +0800415 return ret;
Stefan Behrense93c89c2012-11-05 17:33:06 +0100416
417leave:
418 dev_replace->srcdev = NULL;
419 dev_replace->tgtdev = NULL;
David Sterba7e79cb82018-03-24 02:11:38 +0100420 btrfs_dev_replace_write_unlock(dev_replace);
Miao Xie1c433662014-09-03 21:35:32 +0800421 btrfs_destroy_dev_replace_tgtdev(fs_info, tgt_device);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100422 return ret;
423}
424
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400425int btrfs_dev_replace_by_ioctl(struct btrfs_fs_info *fs_info,
Anand Jainb5255452016-03-24 18:48:14 +0800426 struct btrfs_ioctl_dev_replace_args *args)
427{
428 int ret;
429
430 switch (args->start.cont_reading_from_srcdev_mode) {
431 case BTRFS_IOCTL_DEV_REPLACE_CONT_READING_FROM_SRCDEV_MODE_ALWAYS:
432 case BTRFS_IOCTL_DEV_REPLACE_CONT_READING_FROM_SRCDEV_MODE_AVOID:
433 break;
434 default:
435 return -EINVAL;
436 }
437
438 if ((args->start.srcdevid == 0 && args->start.srcdev_name[0] == '\0') ||
439 args->start.tgtdev_name[0] == '\0')
440 return -EINVAL;
441
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400442 ret = btrfs_dev_replace_start(fs_info, args->start.tgtdev_name,
Anand Jainb5255452016-03-24 18:48:14 +0800443 args->start.srcdevid,
444 args->start.srcdev_name,
445 args->start.cont_reading_from_srcdev_mode);
446 args->result = ret;
447 /* don't warn if EINPROGRESS, someone else might be running scrub */
448 if (ret == BTRFS_IOCTL_DEV_REPLACE_RESULT_SCRUB_INPROGRESS)
449 ret = 0;
450
451 return ret;
452}
453
Miao Xiec404e0d2014-01-30 16:46:55 +0800454/*
Nicholas D Steeves01327612016-05-19 21:18:45 -0400455 * blocked until all in-flight bios operations are finished.
Miao Xiec404e0d2014-01-30 16:46:55 +0800456 */
457static void btrfs_rm_dev_replace_blocked(struct btrfs_fs_info *fs_info)
458{
Miao Xiec404e0d2014-01-30 16:46:55 +0800459 set_bit(BTRFS_FS_STATE_DEV_REPLACING, &fs_info->fs_state);
Zhao Lei76539472015-01-20 15:11:38 +0800460 wait_event(fs_info->replace_wait, !percpu_counter_sum(
461 &fs_info->bio_counter));
Miao Xiec404e0d2014-01-30 16:46:55 +0800462}
463
464/*
465 * we have removed target device, it is safe to allow new bios request.
466 */
467static void btrfs_rm_dev_replace_unblocked(struct btrfs_fs_info *fs_info)
468{
469 clear_bit(BTRFS_FS_STATE_DEV_REPLACING, &fs_info->fs_state);
David Sterba730d9ec2015-10-10 18:16:38 +0200470 wake_up(&fs_info->replace_wait);
Miao Xiec404e0d2014-01-30 16:46:55 +0800471}
472
Stefan Behrense93c89c2012-11-05 17:33:06 +0100473static int btrfs_dev_replace_finishing(struct btrfs_fs_info *fs_info,
474 int scrub_ret)
475{
476 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
477 struct btrfs_device *tgt_device;
478 struct btrfs_device *src_device;
479 struct btrfs_root *root = fs_info->tree_root;
480 u8 uuid_tmp[BTRFS_UUID_SIZE];
481 struct btrfs_trans_handle *trans;
482 int ret = 0;
483
484 /* don't allow cancel or unmount to disturb the finishing procedure */
485 mutex_lock(&dev_replace->lock_finishing_cancel_unmount);
486
David Sterba7e79cb82018-03-24 02:11:38 +0100487 btrfs_dev_replace_read_lock(dev_replace);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100488 /* was the operation canceled, or is it finished? */
489 if (dev_replace->replace_state !=
490 BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED) {
David Sterba7e79cb82018-03-24 02:11:38 +0100491 btrfs_dev_replace_read_unlock(dev_replace);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100492 mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
493 return 0;
494 }
495
496 tgt_device = dev_replace->tgtdev;
497 src_device = dev_replace->srcdev;
David Sterba7e79cb82018-03-24 02:11:38 +0100498 btrfs_dev_replace_read_unlock(dev_replace);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100499
Stefan Behrense93c89c2012-11-05 17:33:06 +0100500 /*
501 * flush all outstanding I/O and inode extent mappings before the
502 * copy operation is declared as being finished
503 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400504 ret = btrfs_start_delalloc_roots(fs_info, 0, -1);
Miao Xie3edb2a62013-01-22 10:49:33 +0000505 if (ret) {
506 mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
507 return ret;
508 }
Chris Mason6374e57a2017-06-23 09:48:21 -0700509 btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100510
511 trans = btrfs_start_transaction(root, 0);
512 if (IS_ERR(trans)) {
513 mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
514 return PTR_ERR(trans);
515 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400516 ret = btrfs_commit_transaction(trans);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100517 WARN_ON(ret);
518
Miao Xie67a2c452014-09-03 21:35:43 +0800519 mutex_lock(&uuid_mutex);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100520 /* keep away write_all_supers() during the finishing procedure */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400521 mutex_lock(&fs_info->fs_devices->device_list_mutex);
522 mutex_lock(&fs_info->chunk_mutex);
David Sterba7e79cb82018-03-24 02:11:38 +0100523 btrfs_dev_replace_write_lock(dev_replace);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100524 dev_replace->replace_state =
525 scrub_ret ? BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED
526 : BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED;
527 dev_replace->tgtdev = NULL;
528 dev_replace->srcdev = NULL;
Zach Brown4546bca2013-10-16 12:10:32 -0700529 dev_replace->time_stopped = get_seconds();
Stefan Behrense93c89c2012-11-05 17:33:06 +0100530 dev_replace->item_needs_writeback = 1;
531
Miao Xiec404e0d2014-01-30 16:46:55 +0800532 /* replace old device with new one in mapping tree */
533 if (!scrub_ret) {
534 btrfs_dev_replace_update_device_in_mapping_tree(fs_info,
535 src_device,
536 tgt_device);
537 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400538 btrfs_err_in_rcu(fs_info,
539 "btrfs_scrub_dev(%s, %llu, %s) failed %d",
Anand Jain3c958bd2017-11-28 10:43:10 +0800540 btrfs_dev_name(src_device),
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400541 src_device->devid,
542 rcu_str_deref(tgt_device->name), scrub_ret);
David Sterba7e79cb82018-03-24 02:11:38 +0100543 btrfs_dev_replace_write_unlock(dev_replace);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400544 mutex_unlock(&fs_info->chunk_mutex);
545 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Miao Xie67a2c452014-09-03 21:35:43 +0800546 mutex_unlock(&uuid_mutex);
Qu Wenruoae6529c2017-03-29 09:33:21 +0800547 btrfs_rm_dev_replace_blocked(fs_info);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100548 if (tgt_device)
549 btrfs_destroy_dev_replace_tgtdev(fs_info, tgt_device);
Qu Wenruoae6529c2017-03-29 09:33:21 +0800550 btrfs_rm_dev_replace_unblocked(fs_info);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100551 mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
552
Eryu Guan2fc9f6b2014-10-13 12:42:12 +0800553 return scrub_ret;
Stefan Behrense93c89c2012-11-05 17:33:06 +0100554 }
555
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400556 btrfs_info_in_rcu(fs_info,
557 "dev_replace from %s (devid %llu) to %s finished",
Anand Jain3c958bd2017-11-28 10:43:10 +0800558 btrfs_dev_name(src_device),
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400559 src_device->devid,
560 rcu_str_deref(tgt_device->name));
Anand Jain401e29c2017-12-04 12:54:55 +0800561 clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &tgt_device->dev_state);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100562 tgt_device->devid = src_device->devid;
563 src_device->devid = BTRFS_DEV_REPLACE_DEVID;
Stefan Behrense93c89c2012-11-05 17:33:06 +0100564 memcpy(uuid_tmp, tgt_device->uuid, sizeof(uuid_tmp));
565 memcpy(tgt_device->uuid, src_device->uuid, sizeof(tgt_device->uuid));
566 memcpy(src_device->uuid, uuid_tmp, sizeof(src_device->uuid));
Miao Xie7cc8e582014-09-03 21:35:38 +0800567 btrfs_device_set_total_bytes(tgt_device, src_device->total_bytes);
568 btrfs_device_set_disk_total_bytes(tgt_device,
569 src_device->disk_total_bytes);
570 btrfs_device_set_bytes_used(tgt_device, src_device->bytes_used);
Miao Xie935e5cc2014-09-03 21:35:33 +0800571 ASSERT(list_empty(&src_device->resized_list));
572 tgt_device->commit_total_bytes = src_device->commit_total_bytes;
Miao Xiece7213c2014-09-03 21:35:34 +0800573 tgt_device->commit_bytes_used = src_device->bytes_used;
Anand Jain88acff62016-05-03 17:44:43 +0800574
575 btrfs_assign_next_active_device(fs_info, src_device, tgt_device);
576
Stefan Behrense93c89c2012-11-05 17:33:06 +0100577 list_add(&tgt_device->dev_alloc_list, &fs_info->fs_devices->alloc_list);
Miao Xie82372bc2014-09-03 21:35:44 +0800578 fs_info->fs_devices->rw_devices++;
Stefan Behrense93c89c2012-11-05 17:33:06 +0100579
David Sterba7e79cb82018-03-24 02:11:38 +0100580 btrfs_dev_replace_write_unlock(dev_replace);
Qu Wenruo12b894c2014-08-20 16:10:15 +0800581
Miao Xiec404e0d2014-01-30 16:46:55 +0800582 btrfs_rm_dev_replace_blocked(fs_info);
583
Qu Wenruo084b6e7c2014-10-30 16:52:31 +0800584 btrfs_rm_dev_replace_remove_srcdev(fs_info, src_device);
Ilya Dryomov13572722013-10-02 20:41:01 +0300585
Miao Xiec404e0d2014-01-30 16:46:55 +0800586 btrfs_rm_dev_replace_unblocked(fs_info);
587
Stefan Behrense93c89c2012-11-05 17:33:06 +0100588 /*
589 * this is again a consistent state where no dev_replace procedure
590 * is running, the target device is part of the filesystem, the
591 * source device is not part of the filesystem anymore and its 1st
592 * superblock is scratched out so that it is no longer marked to
593 * belong to this filesystem.
594 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400595 mutex_unlock(&fs_info->chunk_mutex);
596 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Miao Xie67a2c452014-09-03 21:35:43 +0800597 mutex_unlock(&uuid_mutex);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100598
Qu Wenruo084b6e7c2014-10-30 16:52:31 +0800599 /* replace the sysfs entry */
Anand Jain32576042015-08-14 18:32:49 +0800600 btrfs_sysfs_rm_device_link(fs_info->fs_devices, src_device);
Qu Wenruo084b6e7c2014-10-30 16:52:31 +0800601 btrfs_rm_dev_replace_free_srcdev(fs_info, src_device);
602
Stefan Behrense93c89c2012-11-05 17:33:06 +0100603 /* write back the superblocks */
604 trans = btrfs_start_transaction(root, 0);
605 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400606 btrfs_commit_transaction(trans);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100607
608 mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
609
610 return 0;
611}
612
613static void btrfs_dev_replace_update_device_in_mapping_tree(
614 struct btrfs_fs_info *fs_info,
615 struct btrfs_device *srcdev,
616 struct btrfs_device *tgtdev)
617{
618 struct extent_map_tree *em_tree = &fs_info->mapping_tree.map_tree;
619 struct extent_map *em;
620 struct map_lookup *map;
621 u64 start = 0;
622 int i;
623
624 write_lock(&em_tree->lock);
625 do {
626 em = lookup_extent_mapping(em_tree, start, (u64)-1);
627 if (!em)
628 break;
Jeff Mahoney95617d62015-06-03 10:55:48 -0400629 map = em->map_lookup;
Stefan Behrense93c89c2012-11-05 17:33:06 +0100630 for (i = 0; i < map->num_stripes; i++)
631 if (srcdev == map->stripes[i].dev)
632 map->stripes[i].dev = tgtdev;
633 start = em->start + em->len;
634 free_extent_map(em);
635 } while (start);
636 write_unlock(&em_tree->lock);
637}
638
David Sterba74b595f2017-06-14 16:24:56 +0200639/*
640 * Read progress of device replace status according to the state and last
641 * stored position. The value format is the same as for
642 * btrfs_dev_replace::progress_1000
643 */
644static u64 btrfs_dev_replace_progress(struct btrfs_fs_info *fs_info)
645{
646 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
647 u64 ret = 0;
648
649 switch (dev_replace->replace_state) {
650 case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
651 case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
652 ret = 0;
653 break;
654 case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
655 ret = 1000;
656 break;
657 case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
658 case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
659 ret = div64_u64(dev_replace->cursor_left,
660 div_u64(btrfs_device_get_total_bytes(
661 dev_replace->srcdev), 1000));
662 break;
663 }
664
665 return ret;
666}
667
Stefan Behrense93c89c2012-11-05 17:33:06 +0100668void btrfs_dev_replace_status(struct btrfs_fs_info *fs_info,
669 struct btrfs_ioctl_dev_replace_args *args)
670{
671 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
672
David Sterba7e79cb82018-03-24 02:11:38 +0100673 btrfs_dev_replace_read_lock(dev_replace);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100674 /* even if !dev_replace_is_valid, the values are good enough for
675 * the replace_status ioctl */
676 args->result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR;
677 args->status.replace_state = dev_replace->replace_state;
678 args->status.time_started = dev_replace->time_started;
679 args->status.time_stopped = dev_replace->time_stopped;
680 args->status.num_write_errors =
681 atomic64_read(&dev_replace->num_write_errors);
682 args->status.num_uncorrectable_read_errors =
683 atomic64_read(&dev_replace->num_uncorrectable_read_errors);
David Sterba74b595f2017-06-14 16:24:56 +0200684 args->status.progress_1000 = btrfs_dev_replace_progress(fs_info);
David Sterba7e79cb82018-03-24 02:11:38 +0100685 btrfs_dev_replace_read_unlock(dev_replace);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100686}
687
Anand Jain18e67c72018-02-12 23:33:31 +0800688int btrfs_dev_replace_cancel(struct btrfs_fs_info *fs_info)
Stefan Behrense93c89c2012-11-05 17:33:06 +0100689{
690 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
691 struct btrfs_device *tgt_device = NULL;
Anand Jain8f2ceaa2018-02-13 11:53:43 +0800692 struct btrfs_device *src_device = NULL;
Stefan Behrense93c89c2012-11-05 17:33:06 +0100693 struct btrfs_trans_handle *trans;
694 struct btrfs_root *root = fs_info->tree_root;
Anand Jain18e67c72018-02-12 23:33:31 +0800695 int result;
Stefan Behrense93c89c2012-11-05 17:33:06 +0100696 int ret;
697
David Howellsbc98a422017-07-17 08:45:34 +0100698 if (sb_rdonly(fs_info->sb))
Ilya Dryomove649e582013-10-10 20:40:21 +0300699 return -EROFS;
700
Stefan Behrense93c89c2012-11-05 17:33:06 +0100701 mutex_lock(&dev_replace->lock_finishing_cancel_unmount);
David Sterba7e79cb82018-03-24 02:11:38 +0100702 btrfs_dev_replace_write_lock(dev_replace);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100703 switch (dev_replace->replace_state) {
704 case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
705 case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
706 case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
707 result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NOT_STARTED;
David Sterba7e79cb82018-03-24 02:11:38 +0100708 btrfs_dev_replace_write_unlock(dev_replace);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100709 goto leave;
710 case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
711 case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
712 result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR;
713 tgt_device = dev_replace->tgtdev;
Anand Jain8f2ceaa2018-02-13 11:53:43 +0800714 src_device = dev_replace->srcdev;
Stefan Behrense93c89c2012-11-05 17:33:06 +0100715 dev_replace->tgtdev = NULL;
716 dev_replace->srcdev = NULL;
717 break;
718 }
719 dev_replace->replace_state = BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED;
Zach Brown4546bca2013-10-16 12:10:32 -0700720 dev_replace->time_stopped = get_seconds();
Stefan Behrense93c89c2012-11-05 17:33:06 +0100721 dev_replace->item_needs_writeback = 1;
David Sterba7e79cb82018-03-24 02:11:38 +0100722 btrfs_dev_replace_write_unlock(dev_replace);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100723 btrfs_scrub_cancel(fs_info);
724
725 trans = btrfs_start_transaction(root, 0);
726 if (IS_ERR(trans)) {
727 mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
728 return PTR_ERR(trans);
729 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400730 ret = btrfs_commit_transaction(trans);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100731 WARN_ON(ret);
Anand Jain8f2ceaa2018-02-13 11:53:43 +0800732
733 btrfs_info_in_rcu(fs_info,
734 "dev_replace from %s (devid %llu) to %s canceled",
735 btrfs_dev_name(src_device), src_device->devid,
736 btrfs_dev_name(tgt_device));
737
Stefan Behrense93c89c2012-11-05 17:33:06 +0100738 if (tgt_device)
739 btrfs_destroy_dev_replace_tgtdev(fs_info, tgt_device);
740
741leave:
742 mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
743 return result;
744}
745
746void btrfs_dev_replace_suspend_for_unmount(struct btrfs_fs_info *fs_info)
747{
748 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
749
750 mutex_lock(&dev_replace->lock_finishing_cancel_unmount);
David Sterba7e79cb82018-03-24 02:11:38 +0100751 btrfs_dev_replace_write_lock(dev_replace);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100752 switch (dev_replace->replace_state) {
753 case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
754 case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
755 case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
756 case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
757 break;
758 case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
759 dev_replace->replace_state =
760 BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED;
Zach Brown4546bca2013-10-16 12:10:32 -0700761 dev_replace->time_stopped = get_seconds();
Stefan Behrense93c89c2012-11-05 17:33:06 +0100762 dev_replace->item_needs_writeback = 1;
Frank Holtonefe120a2013-12-20 11:37:06 -0500763 btrfs_info(fs_info, "suspending dev_replace for unmount");
Stefan Behrense93c89c2012-11-05 17:33:06 +0100764 break;
765 }
766
David Sterba7e79cb82018-03-24 02:11:38 +0100767 btrfs_dev_replace_write_unlock(dev_replace);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100768 mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
769}
770
771/* resume dev_replace procedure that was interrupted by unmount */
772int btrfs_resume_dev_replace_async(struct btrfs_fs_info *fs_info)
773{
774 struct task_struct *task;
775 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
776
David Sterba7e79cb82018-03-24 02:11:38 +0100777 btrfs_dev_replace_write_lock(dev_replace);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100778 switch (dev_replace->replace_state) {
779 case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
780 case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
781 case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
David Sterba7e79cb82018-03-24 02:11:38 +0100782 btrfs_dev_replace_write_unlock(dev_replace);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100783 return 0;
784 case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
785 break;
786 case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
787 dev_replace->replace_state =
788 BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED;
789 break;
790 }
791 if (!dev_replace->tgtdev || !dev_replace->tgtdev->bdev) {
Frank Holtonefe120a2013-12-20 11:37:06 -0500792 btrfs_info(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400793 "cannot continue dev_replace, tgtdev is missing");
794 btrfs_info(fs_info,
795 "you may cancel the operation after 'mount -o degraded'");
David Sterba7e79cb82018-03-24 02:11:38 +0100796 btrfs_dev_replace_write_unlock(dev_replace);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100797 return 0;
798 }
David Sterba7e79cb82018-03-24 02:11:38 +0100799 btrfs_dev_replace_write_unlock(dev_replace);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100800
David Sterba171938e2017-03-28 14:44:21 +0200801 WARN_ON(test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags));
Stefan Behrense93c89c2012-11-05 17:33:06 +0100802 task = kthread_run(btrfs_dev_replace_kthread, fs_info, "btrfs-devrepl");
Rusty Russell8c6ffba2013-07-15 11:20:32 +0930803 return PTR_ERR_OR_ZERO(task);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100804}
805
806static int btrfs_dev_replace_kthread(void *data)
807{
808 struct btrfs_fs_info *fs_info = data;
809 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
Stefan Behrense93c89c2012-11-05 17:33:06 +0100810 u64 progress;
David Sterba00251a52018-03-20 15:35:50 +0100811 int ret;
Stefan Behrense93c89c2012-11-05 17:33:06 +0100812
David Sterbaf1b8a1e2017-06-14 16:28:42 +0200813 progress = btrfs_dev_replace_progress(fs_info);
814 progress = div_u64(progress, 10);
815 btrfs_info_in_rcu(fs_info,
Anand Jain3c958bd2017-11-28 10:43:10 +0800816 "continuing dev_replace from %s (devid %llu) to target %s @%u%%",
817 btrfs_dev_name(dev_replace->srcdev),
David Sterbaf1b8a1e2017-06-14 16:28:42 +0200818 dev_replace->srcdev->devid,
Anand Jain3c958bd2017-11-28 10:43:10 +0800819 btrfs_dev_name(dev_replace->tgtdev),
David Sterbaf1b8a1e2017-06-14 16:28:42 +0200820 (unsigned int)progress);
821
Stefan Behrense93c89c2012-11-05 17:33:06 +0100822 ret = btrfs_scrub_dev(fs_info, dev_replace->srcdev->devid,
823 dev_replace->committed_cursor_left,
Miao Xie7cc8e582014-09-03 21:35:38 +0800824 btrfs_device_get_total_bytes(dev_replace->srcdev),
Stefan Behrense93c89c2012-11-05 17:33:06 +0100825 &dev_replace->scrub_progress, 0, 1);
826 ret = btrfs_dev_replace_finishing(fs_info, ret);
827 WARN_ON(ret);
David Sterba00251a52018-03-20 15:35:50 +0100828
829 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100830 return 0;
831}
832
833int btrfs_dev_replace_is_ongoing(struct btrfs_dev_replace *dev_replace)
834{
835 if (!dev_replace->is_valid)
836 return 0;
837
838 switch (dev_replace->replace_state) {
839 case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
840 case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
841 case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
842 return 0;
843 case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
844 case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
845 /*
846 * return true even if tgtdev is missing (this is
847 * something that can happen if the dev_replace
848 * procedure is suspended by an umount and then
849 * the tgtdev is missing (or "btrfs dev scan") was
850 * not called and the the filesystem is remounted
851 * in degraded state. This does not stop the
852 * dev_replace procedure. It needs to be canceled
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -0800853 * manually if the cancellation is wanted.
Stefan Behrense93c89c2012-11-05 17:33:06 +0100854 */
855 break;
856 }
857 return 1;
858}
859
David Sterba7e79cb82018-03-24 02:11:38 +0100860void btrfs_dev_replace_read_lock(struct btrfs_dev_replace *dev_replace)
Stefan Behrense93c89c2012-11-05 17:33:06 +0100861{
David Sterba7e79cb82018-03-24 02:11:38 +0100862 read_lock(&dev_replace->lock);
863 atomic_inc(&dev_replace->read_locks);
864}
865
866void btrfs_dev_replace_read_unlock(struct btrfs_dev_replace *dev_replace)
867{
868 ASSERT(atomic_read(&dev_replace->read_locks) > 0);
869 atomic_dec(&dev_replace->read_locks);
870 read_unlock(&dev_replace->lock);
871}
872
873void btrfs_dev_replace_write_lock(struct btrfs_dev_replace *dev_replace)
874{
Liu Bo73beece2015-07-17 16:49:19 +0800875again:
David Sterba7e79cb82018-03-24 02:11:38 +0100876 wait_event(dev_replace->read_lock_wq,
877 atomic_read(&dev_replace->blocking_readers) == 0);
878 write_lock(&dev_replace->lock);
879 if (atomic_read(&dev_replace->blocking_readers)) {
880 write_unlock(&dev_replace->lock);
881 goto again;
Stefan Behrense93c89c2012-11-05 17:33:06 +0100882 }
Stefan Behrense93c89c2012-11-05 17:33:06 +0100883}
884
David Sterba7e79cb82018-03-24 02:11:38 +0100885void btrfs_dev_replace_write_unlock(struct btrfs_dev_replace *dev_replace)
Stefan Behrense93c89c2012-11-05 17:33:06 +0100886{
David Sterba7e79cb82018-03-24 02:11:38 +0100887 ASSERT(atomic_read(&dev_replace->blocking_readers) == 0);
888 write_unlock(&dev_replace->lock);
Stefan Behrense93c89c2012-11-05 17:33:06 +0100889}
Miao Xiec404e0d2014-01-30 16:46:55 +0800890
Liu Bo73beece2015-07-17 16:49:19 +0800891/* inc blocking cnt and release read lock */
892void btrfs_dev_replace_set_lock_blocking(
893 struct btrfs_dev_replace *dev_replace)
894{
895 /* only set blocking for read lock */
896 ASSERT(atomic_read(&dev_replace->read_locks) > 0);
897 atomic_inc(&dev_replace->blocking_readers);
898 read_unlock(&dev_replace->lock);
899}
900
901/* acquire read lock and dec blocking cnt */
902void btrfs_dev_replace_clear_lock_blocking(
903 struct btrfs_dev_replace *dev_replace)
904{
905 /* only set blocking for read lock */
906 ASSERT(atomic_read(&dev_replace->read_locks) > 0);
907 ASSERT(atomic_read(&dev_replace->blocking_readers) > 0);
908 read_lock(&dev_replace->lock);
909 if (atomic_dec_and_test(&dev_replace->blocking_readers) &&
910 waitqueue_active(&dev_replace->read_lock_wq))
911 wake_up(&dev_replace->read_lock_wq);
912}
913
Miao Xiec404e0d2014-01-30 16:46:55 +0800914void btrfs_bio_counter_inc_noblocked(struct btrfs_fs_info *fs_info)
915{
916 percpu_counter_inc(&fs_info->bio_counter);
917}
918
Miao Xie42452152014-11-25 16:39:28 +0800919void btrfs_bio_counter_sub(struct btrfs_fs_info *fs_info, s64 amount)
Miao Xiec404e0d2014-01-30 16:46:55 +0800920{
Miao Xie42452152014-11-25 16:39:28 +0800921 percpu_counter_sub(&fs_info->bio_counter, amount);
Miao Xiec404e0d2014-01-30 16:46:55 +0800922
923 if (waitqueue_active(&fs_info->replace_wait))
924 wake_up(&fs_info->replace_wait);
925}
926
927void btrfs_bio_counter_inc_blocked(struct btrfs_fs_info *fs_info)
928{
Zhao Lei09dd7a02015-01-20 15:11:37 +0800929 while (1) {
930 percpu_counter_inc(&fs_info->bio_counter);
931 if (likely(!test_bit(BTRFS_FS_STATE_DEV_REPLACING,
932 &fs_info->fs_state)))
933 break;
934
Miao Xiec404e0d2014-01-30 16:46:55 +0800935 btrfs_bio_counter_dec(fs_info);
936 wait_event(fs_info->replace_wait,
937 !test_bit(BTRFS_FS_STATE_DEV_REPLACING,
938 &fs_info->fs_state));
Miao Xiec404e0d2014-01-30 16:46:55 +0800939 }
Miao Xiec404e0d2014-01-30 16:46:55 +0800940}