David Sterba | c1d7c51 | 2018-04-03 19:23:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) STRATO AG 2012. All rights reserved. |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 4 | */ |
David Sterba | c1d7c51 | 2018-04-03 19:23:33 +0200 | [diff] [blame] | 5 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 6 | #include <linux/sched.h> |
| 7 | #include <linux/bio.h> |
| 8 | #include <linux/slab.h> |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 9 | #include <linux/blkdev.h> |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 10 | #include <linux/kthread.h> |
| 11 | #include <linux/math64.h> |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 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" |
Anand Jain | 49c6f73 | 2014-06-03 11:36:02 +0800 | [diff] [blame] | 22 | #include "sysfs.h" |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 23 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 24 | static int btrfs_dev_replace_finishing(struct btrfs_fs_info *fs_info, |
| 25 | int scrub_ret); |
| 26 | static 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 Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 30 | static int btrfs_dev_replace_kthread(void *data); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 31 | |
| 32 | int 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) { |
| 56 | no_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 Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 62 | 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 Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 82 | btrfs_warn(fs_info, |
| 83 | "dev_replace entry found has unexpected size, ignore entry"); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 84 | 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: |
| 114 | dev_replace->srcdev = btrfs_find_device(fs_info, src_devid, |
| 115 | NULL, NULL); |
| 116 | dev_replace->tgtdev = btrfs_find_device(fs_info, |
| 117 | BTRFS_DEV_REPLACE_DEVID, |
| 118 | NULL, NULL); |
| 119 | /* |
| 120 | * allow 'btrfs dev replace_cancel' if src/tgt device is |
| 121 | * missing |
| 122 | */ |
| 123 | if (!dev_replace->srcdev && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 124 | !btrfs_test_opt(fs_info, DEGRADED)) { |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 125 | ret = -EIO; |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 126 | 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 Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 131 | } |
| 132 | if (!dev_replace->tgtdev && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 133 | !btrfs_test_opt(fs_info, DEGRADED)) { |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 134 | ret = -EIO; |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 135 | 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 Uytterhoeven | 6e71c47 | 2013-08-20 13:20:08 +0200 | [diff] [blame] | 139 | BTRFS_DEV_REPLACE_DEVID); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 140 | } |
| 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 Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 147 | dev_replace->tgtdev->commit_total_bytes = |
| 148 | dev_replace->srcdev->commit_total_bytes; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 149 | dev_replace->tgtdev->bytes_used = |
| 150 | dev_replace->srcdev->bytes_used; |
Miao Xie | ce7213c | 2014-09-03 21:35:34 +0800 | [diff] [blame] | 151 | dev_replace->tgtdev->commit_bytes_used = |
| 152 | dev_replace->srcdev->commit_bytes_used; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 153 | } |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 154 | set_bit(BTRFS_DEV_STATE_REPLACE_TGT, |
| 155 | &dev_replace->tgtdev->dev_state); |
Anand Jain | 15fc128 | 2018-02-12 23:36:25 +0800 | [diff] [blame] | 156 | |
| 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 Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 164 | } |
| 165 | break; |
| 166 | } |
| 167 | |
| 168 | out: |
Tsutomu Itoh | 527afb4 | 2015-08-19 14:55:00 +0900 | [diff] [blame] | 169 | btrfs_free_path(path); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 170 | return ret; |
| 171 | } |
| 172 | |
| 173 | /* |
David Sterba | d48f39d | 2018-03-20 16:09:48 +0100 | [diff] [blame] | 174 | * 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 | */ |
| 179 | static 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 | |
| 204 | filemap_write_and_wait(bdev->bd_inode->i_mapping); |
| 205 | |
| 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 | |
| 240 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
| 241 | set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); |
| 242 | device->generation = 0; |
| 243 | device->io_width = fs_info->sectorsize; |
| 244 | device->io_align = fs_info->sectorsize; |
| 245 | device->sector_size = fs_info->sectorsize; |
| 246 | device->total_bytes = btrfs_device_get_total_bytes(srcdev); |
| 247 | device->disk_total_bytes = btrfs_device_get_disk_total_bytes(srcdev); |
| 248 | device->bytes_used = btrfs_device_get_bytes_used(srcdev); |
| 249 | device->commit_total_bytes = srcdev->commit_total_bytes; |
| 250 | device->commit_bytes_used = device->bytes_used; |
| 251 | device->fs_info = fs_info; |
| 252 | device->bdev = bdev; |
| 253 | set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state); |
| 254 | set_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state); |
| 255 | device->mode = FMODE_EXCL; |
| 256 | device->dev_stats_valid = 1; |
| 257 | set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE); |
| 258 | device->fs_devices = fs_info->fs_devices; |
| 259 | list_add(&device->dev_list, &fs_info->fs_devices->devices); |
| 260 | fs_info->fs_devices->num_devices++; |
| 261 | fs_info->fs_devices->open_devices++; |
| 262 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
| 263 | |
| 264 | *device_out = device; |
| 265 | return 0; |
| 266 | |
| 267 | error: |
| 268 | blkdev_put(bdev, FMODE_EXCL); |
| 269 | return ret; |
| 270 | } |
| 271 | |
| 272 | /* |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 273 | * called from commit_transaction. Writes changed device replace state to |
| 274 | * disk. |
| 275 | */ |
| 276 | int btrfs_run_dev_replace(struct btrfs_trans_handle *trans, |
| 277 | struct btrfs_fs_info *fs_info) |
| 278 | { |
| 279 | int ret; |
| 280 | struct btrfs_root *dev_root = fs_info->dev_root; |
| 281 | struct btrfs_path *path; |
| 282 | struct btrfs_key key; |
| 283 | struct extent_buffer *eb; |
| 284 | struct btrfs_dev_replace_item *ptr; |
| 285 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; |
| 286 | |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame^] | 287 | down_read(&dev_replace->rwsem); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 288 | if (!dev_replace->is_valid || |
| 289 | !dev_replace->item_needs_writeback) { |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame^] | 290 | up_read(&dev_replace->rwsem); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 291 | return 0; |
| 292 | } |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame^] | 293 | up_read(&dev_replace->rwsem); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 294 | |
| 295 | key.objectid = 0; |
| 296 | key.type = BTRFS_DEV_REPLACE_KEY; |
| 297 | key.offset = 0; |
| 298 | |
| 299 | path = btrfs_alloc_path(); |
| 300 | if (!path) { |
| 301 | ret = -ENOMEM; |
| 302 | goto out; |
| 303 | } |
| 304 | ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1); |
| 305 | if (ret < 0) { |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 306 | btrfs_warn(fs_info, |
| 307 | "error %d while searching for dev_replace item!", |
| 308 | ret); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 309 | goto out; |
| 310 | } |
| 311 | |
| 312 | if (ret == 0 && |
| 313 | btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) { |
| 314 | /* |
| 315 | * need to delete old one and insert a new one. |
| 316 | * Since no attempt is made to recover any old state, if the |
| 317 | * dev_replace state is 'running', the data on the target |
| 318 | * drive is lost. |
| 319 | * It would be possible to recover the state: just make sure |
| 320 | * that the beginning of the item is never changed and always |
| 321 | * contains all the essential information. Then read this |
| 322 | * minimal set of information and use it as a base for the |
| 323 | * new state. |
| 324 | */ |
| 325 | ret = btrfs_del_item(trans, dev_root, path); |
| 326 | if (ret != 0) { |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 327 | btrfs_warn(fs_info, |
| 328 | "delete too small dev_replace item failed %d!", |
| 329 | ret); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 330 | goto out; |
| 331 | } |
| 332 | ret = 1; |
| 333 | } |
| 334 | |
| 335 | if (ret == 1) { |
| 336 | /* need to insert a new item */ |
| 337 | btrfs_release_path(path); |
| 338 | ret = btrfs_insert_empty_item(trans, dev_root, path, |
| 339 | &key, sizeof(*ptr)); |
| 340 | if (ret < 0) { |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 341 | btrfs_warn(fs_info, |
| 342 | "insert dev_replace item failed %d!", ret); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 343 | goto out; |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | eb = path->nodes[0]; |
| 348 | ptr = btrfs_item_ptr(eb, path->slots[0], |
| 349 | struct btrfs_dev_replace_item); |
| 350 | |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame^] | 351 | down_write(&dev_replace->rwsem); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 352 | if (dev_replace->srcdev) |
| 353 | btrfs_set_dev_replace_src_devid(eb, ptr, |
| 354 | dev_replace->srcdev->devid); |
| 355 | else |
| 356 | btrfs_set_dev_replace_src_devid(eb, ptr, (u64)-1); |
| 357 | btrfs_set_dev_replace_cont_reading_from_srcdev_mode(eb, ptr, |
| 358 | dev_replace->cont_reading_from_srcdev_mode); |
| 359 | btrfs_set_dev_replace_replace_state(eb, ptr, |
| 360 | dev_replace->replace_state); |
| 361 | btrfs_set_dev_replace_time_started(eb, ptr, dev_replace->time_started); |
| 362 | btrfs_set_dev_replace_time_stopped(eb, ptr, dev_replace->time_stopped); |
| 363 | btrfs_set_dev_replace_num_write_errors(eb, ptr, |
| 364 | atomic64_read(&dev_replace->num_write_errors)); |
| 365 | btrfs_set_dev_replace_num_uncorrectable_read_errors(eb, ptr, |
| 366 | atomic64_read(&dev_replace->num_uncorrectable_read_errors)); |
| 367 | dev_replace->cursor_left_last_write_of_item = |
| 368 | dev_replace->cursor_left; |
| 369 | btrfs_set_dev_replace_cursor_left(eb, ptr, |
| 370 | dev_replace->cursor_left_last_write_of_item); |
| 371 | btrfs_set_dev_replace_cursor_right(eb, ptr, |
| 372 | dev_replace->cursor_right); |
| 373 | dev_replace->item_needs_writeback = 0; |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame^] | 374 | up_write(&dev_replace->rwsem); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 375 | |
| 376 | btrfs_mark_buffer_dirty(eb); |
| 377 | |
| 378 | out: |
| 379 | btrfs_free_path(path); |
| 380 | |
| 381 | return ret; |
| 382 | } |
| 383 | |
Anand Jain | 3c958bd | 2017-11-28 10:43:10 +0800 | [diff] [blame] | 384 | static char* btrfs_dev_name(struct btrfs_device *device) |
| 385 | { |
Anand Jain | acf18c5 | 2018-02-24 19:43:56 +0800 | [diff] [blame] | 386 | if (!device || test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) |
Anand Jain | 3c958bd | 2017-11-28 10:43:10 +0800 | [diff] [blame] | 387 | return "<missing disk>"; |
| 388 | else |
| 389 | return rcu_str_deref(device->name); |
| 390 | } |
| 391 | |
Anand Jain | 54862d6 | 2018-11-11 22:22:16 +0800 | [diff] [blame] | 392 | static int btrfs_dev_replace_start(struct btrfs_fs_info *fs_info, |
David Sterba | da353f6 | 2017-02-14 17:55:53 +0100 | [diff] [blame] | 393 | const char *tgtdev_name, u64 srcdevid, const char *srcdev_name, |
| 394 | int read_src) |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 395 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 396 | struct btrfs_root *root = fs_info->dev_root; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 397 | struct btrfs_trans_handle *trans; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 398 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; |
| 399 | int ret; |
| 400 | struct btrfs_device *tgt_device = NULL; |
| 401 | struct btrfs_device *src_device = NULL; |
David Sterba | aa144bf | 2018-08-24 17:44:05 +0200 | [diff] [blame] | 402 | bool need_unlock; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 403 | |
Nikolay Borisov | a27a94c | 2018-09-03 12:46:14 +0300 | [diff] [blame] | 404 | 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 Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 408 | |
Omar Sandoval | eede2bf | 2016-11-03 10:28:12 -0700 | [diff] [blame] | 409 | 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 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 416 | ret = btrfs_init_dev_replace_tgtdev(fs_info, tgtdev_name, |
Miao Xie | 1c43366 | 2014-09-03 21:35:32 +0800 | [diff] [blame] | 417 | src_device, &tgt_device); |
Miao Xie | 1c43366 | 2014-09-03 21:35:32 +0800 | [diff] [blame] | 418 | if (ret) |
| 419 | return ret; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 420 | |
Anand Jain | 9e271ae | 2015-08-14 18:33:02 +0800 | [diff] [blame] | 421 | /* |
| 422 | * Here we commit the transaction to make sure commit_total_bytes |
| 423 | * of all the devices are updated. |
| 424 | */ |
| 425 | trans = btrfs_attach_transaction(root); |
| 426 | if (!IS_ERR(trans)) { |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 427 | ret = btrfs_commit_transaction(trans); |
Anand Jain | 9e271ae | 2015-08-14 18:33:02 +0800 | [diff] [blame] | 428 | if (ret) |
| 429 | return ret; |
| 430 | } else if (PTR_ERR(trans) != -ENOENT) { |
| 431 | return PTR_ERR(trans); |
| 432 | } |
| 433 | |
David Sterba | aa144bf | 2018-08-24 17:44:05 +0200 | [diff] [blame] | 434 | need_unlock = true; |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame^] | 435 | down_write(&dev_replace->rwsem); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 436 | switch (dev_replace->replace_state) { |
| 437 | case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED: |
| 438 | case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED: |
| 439 | case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED: |
| 440 | break; |
| 441 | case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED: |
| 442 | case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED: |
Jeff Mahoney | 5c06147 | 2018-09-06 15:52:17 -0400 | [diff] [blame] | 443 | ASSERT(0); |
Anand Jain | b525545 | 2016-03-24 18:48:14 +0800 | [diff] [blame] | 444 | ret = BTRFS_IOCTL_DEV_REPLACE_RESULT_ALREADY_STARTED; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 445 | goto leave; |
| 446 | } |
| 447 | |
Anand Jain | b525545 | 2016-03-24 18:48:14 +0800 | [diff] [blame] | 448 | dev_replace->cont_reading_from_srcdev_mode = read_src; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 449 | WARN_ON(!src_device); |
| 450 | dev_replace->srcdev = src_device; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 451 | dev_replace->tgtdev = tgt_device; |
| 452 | |
Anand Jain | fc23c24 | 2016-03-24 18:48:12 +0800 | [diff] [blame] | 453 | btrfs_info_in_rcu(fs_info, |
David Sterba | ecaeb14 | 2015-10-08 09:01:03 +0200 | [diff] [blame] | 454 | "dev_replace from %s (devid %llu) to %s started", |
Anand Jain | 3c958bd | 2017-11-28 10:43:10 +0800 | [diff] [blame] | 455 | btrfs_dev_name(src_device), |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 456 | src_device->devid, |
| 457 | rcu_str_deref(tgt_device->name)); |
| 458 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 459 | /* |
| 460 | * from now on, the writes to the srcdev are all duplicated to |
| 461 | * go to the tgtdev as well (refer to btrfs_map_block()). |
| 462 | */ |
| 463 | dev_replace->replace_state = BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED; |
Allen Pais | a944442 | 2018-06-12 17:18:25 +0530 | [diff] [blame] | 464 | dev_replace->time_started = ktime_get_real_seconds(); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 465 | dev_replace->cursor_left = 0; |
| 466 | dev_replace->committed_cursor_left = 0; |
| 467 | dev_replace->cursor_left_last_write_of_item = 0; |
| 468 | dev_replace->cursor_right = 0; |
| 469 | dev_replace->is_valid = 1; |
| 470 | dev_replace->item_needs_writeback = 1; |
Yauhen Kharuzhy | 7ccefb9 | 2016-03-29 14:17:48 -0700 | [diff] [blame] | 471 | atomic64_set(&dev_replace->num_write_errors, 0); |
| 472 | atomic64_set(&dev_replace->num_uncorrectable_read_errors, 0); |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame^] | 473 | up_write(&dev_replace->rwsem); |
David Sterba | aa144bf | 2018-08-24 17:44:05 +0200 | [diff] [blame] | 474 | need_unlock = false; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 475 | |
Liu Bo | 73416da | 2015-08-14 18:33:07 +0800 | [diff] [blame] | 476 | ret = btrfs_sysfs_add_device_link(tgt_device->fs_devices, tgt_device); |
| 477 | if (ret) |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 478 | btrfs_err(fs_info, "kobj add dev failed %d", ret); |
Liu Bo | 73416da | 2015-08-14 18:33:07 +0800 | [diff] [blame] | 479 | |
Chris Mason | 6374e57a | 2017-06-23 09:48:21 -0700 | [diff] [blame] | 480 | btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 481 | |
| 482 | /* force writing the updated state information to disk */ |
| 483 | trans = btrfs_start_transaction(root, 0); |
| 484 | if (IS_ERR(trans)) { |
| 485 | ret = PTR_ERR(trans); |
David Sterba | aa144bf | 2018-08-24 17:44:05 +0200 | [diff] [blame] | 486 | need_unlock = true; |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame^] | 487 | down_write(&dev_replace->rwsem); |
Jeff Mahoney | 5c06147 | 2018-09-06 15:52:17 -0400 | [diff] [blame] | 488 | dev_replace->replace_state = |
| 489 | BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED; |
| 490 | dev_replace->srcdev = NULL; |
| 491 | dev_replace->tgtdev = NULL; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 492 | goto leave; |
| 493 | } |
| 494 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 495 | ret = btrfs_commit_transaction(trans); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 496 | WARN_ON(ret); |
| 497 | |
| 498 | /* the disk copy procedure reuses the scrub code */ |
| 499 | ret = btrfs_scrub_dev(fs_info, src_device->devid, 0, |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 500 | btrfs_device_get_total_bytes(src_device), |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 501 | &dev_replace->scrub_progress, 0, 1); |
| 502 | |
Anand Jain | fc23c24 | 2016-03-24 18:48:12 +0800 | [diff] [blame] | 503 | ret = btrfs_dev_replace_finishing(fs_info, ret); |
Eryu Guan | 2fc9f6b | 2014-10-13 12:42:12 +0800 | [diff] [blame] | 504 | if (ret == -EINPROGRESS) { |
Anand Jain | b525545 | 2016-03-24 18:48:14 +0800 | [diff] [blame] | 505 | ret = BTRFS_IOCTL_DEV_REPLACE_RESULT_SCRUB_INPROGRESS; |
Anand Jain | 49365e6 | 2018-11-20 19:56:15 +0800 | [diff] [blame] | 506 | } else if (ret != -ECANCELED) { |
Eryu Guan | 2fc9f6b | 2014-10-13 12:42:12 +0800 | [diff] [blame] | 507 | WARN_ON(ret); |
| 508 | } |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 509 | |
Eryu Guan | 2fc9f6b | 2014-10-13 12:42:12 +0800 | [diff] [blame] | 510 | return ret; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 511 | |
| 512 | leave: |
David Sterba | aa144bf | 2018-08-24 17:44:05 +0200 | [diff] [blame] | 513 | if (need_unlock) |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame^] | 514 | up_write(&dev_replace->rwsem); |
Nikolay Borisov | 4f5ad7b | 2018-07-20 19:37:51 +0300 | [diff] [blame] | 515 | btrfs_destroy_dev_replace_tgtdev(tgt_device); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 516 | return ret; |
| 517 | } |
| 518 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 519 | int btrfs_dev_replace_by_ioctl(struct btrfs_fs_info *fs_info, |
Anand Jain | b525545 | 2016-03-24 18:48:14 +0800 | [diff] [blame] | 520 | struct btrfs_ioctl_dev_replace_args *args) |
| 521 | { |
| 522 | int ret; |
| 523 | |
| 524 | switch (args->start.cont_reading_from_srcdev_mode) { |
| 525 | case BTRFS_IOCTL_DEV_REPLACE_CONT_READING_FROM_SRCDEV_MODE_ALWAYS: |
| 526 | case BTRFS_IOCTL_DEV_REPLACE_CONT_READING_FROM_SRCDEV_MODE_AVOID: |
| 527 | break; |
| 528 | default: |
| 529 | return -EINVAL; |
| 530 | } |
| 531 | |
| 532 | if ((args->start.srcdevid == 0 && args->start.srcdev_name[0] == '\0') || |
| 533 | args->start.tgtdev_name[0] == '\0') |
| 534 | return -EINVAL; |
| 535 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 536 | ret = btrfs_dev_replace_start(fs_info, args->start.tgtdev_name, |
Anand Jain | b525545 | 2016-03-24 18:48:14 +0800 | [diff] [blame] | 537 | args->start.srcdevid, |
| 538 | args->start.srcdev_name, |
| 539 | args->start.cont_reading_from_srcdev_mode); |
| 540 | args->result = ret; |
| 541 | /* don't warn if EINPROGRESS, someone else might be running scrub */ |
Anand Jain | 53e62fb | 2018-11-11 22:22:24 +0800 | [diff] [blame] | 542 | if (ret == BTRFS_IOCTL_DEV_REPLACE_RESULT_SCRUB_INPROGRESS || |
| 543 | ret == BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR) |
| 544 | return 0; |
Anand Jain | b525545 | 2016-03-24 18:48:14 +0800 | [diff] [blame] | 545 | |
| 546 | return ret; |
| 547 | } |
| 548 | |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 549 | /* |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 550 | * blocked until all in-flight bios operations are finished. |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 551 | */ |
| 552 | static void btrfs_rm_dev_replace_blocked(struct btrfs_fs_info *fs_info) |
| 553 | { |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 554 | set_bit(BTRFS_FS_STATE_DEV_REPLACING, &fs_info->fs_state); |
David Sterba | 7f8d236 | 2018-04-05 01:04:49 +0200 | [diff] [blame] | 555 | wait_event(fs_info->dev_replace.replace_wait, !percpu_counter_sum( |
| 556 | &fs_info->dev_replace.bio_counter)); |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | /* |
| 560 | * we have removed target device, it is safe to allow new bios request. |
| 561 | */ |
| 562 | static void btrfs_rm_dev_replace_unblocked(struct btrfs_fs_info *fs_info) |
| 563 | { |
| 564 | clear_bit(BTRFS_FS_STATE_DEV_REPLACING, &fs_info->fs_state); |
David Sterba | 7f8d236 | 2018-04-05 01:04:49 +0200 | [diff] [blame] | 565 | wake_up(&fs_info->dev_replace.replace_wait); |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 566 | } |
| 567 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 568 | static int btrfs_dev_replace_finishing(struct btrfs_fs_info *fs_info, |
| 569 | int scrub_ret) |
| 570 | { |
| 571 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; |
| 572 | struct btrfs_device *tgt_device; |
| 573 | struct btrfs_device *src_device; |
| 574 | struct btrfs_root *root = fs_info->tree_root; |
| 575 | u8 uuid_tmp[BTRFS_UUID_SIZE]; |
| 576 | struct btrfs_trans_handle *trans; |
| 577 | int ret = 0; |
| 578 | |
| 579 | /* don't allow cancel or unmount to disturb the finishing procedure */ |
| 580 | mutex_lock(&dev_replace->lock_finishing_cancel_unmount); |
| 581 | |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame^] | 582 | down_read(&dev_replace->rwsem); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 583 | /* was the operation canceled, or is it finished? */ |
| 584 | if (dev_replace->replace_state != |
| 585 | BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED) { |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame^] | 586 | up_read(&dev_replace->rwsem); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 587 | mutex_unlock(&dev_replace->lock_finishing_cancel_unmount); |
| 588 | return 0; |
| 589 | } |
| 590 | |
| 591 | tgt_device = dev_replace->tgtdev; |
| 592 | src_device = dev_replace->srcdev; |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame^] | 593 | up_read(&dev_replace->rwsem); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 594 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 595 | /* |
| 596 | * flush all outstanding I/O and inode extent mappings before the |
| 597 | * copy operation is declared as being finished |
| 598 | */ |
Nikolay Borisov | 82b3e53 | 2018-04-23 10:54:13 +0300 | [diff] [blame] | 599 | ret = btrfs_start_delalloc_roots(fs_info, -1); |
Miao Xie | 3edb2a6 | 2013-01-22 10:49:33 +0000 | [diff] [blame] | 600 | if (ret) { |
| 601 | mutex_unlock(&dev_replace->lock_finishing_cancel_unmount); |
| 602 | return ret; |
| 603 | } |
Chris Mason | 6374e57a | 2017-06-23 09:48:21 -0700 | [diff] [blame] | 604 | btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 605 | |
| 606 | trans = btrfs_start_transaction(root, 0); |
| 607 | if (IS_ERR(trans)) { |
| 608 | mutex_unlock(&dev_replace->lock_finishing_cancel_unmount); |
| 609 | return PTR_ERR(trans); |
| 610 | } |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 611 | ret = btrfs_commit_transaction(trans); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 612 | WARN_ON(ret); |
| 613 | |
| 614 | /* keep away write_all_supers() during the finishing procedure */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 615 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
| 616 | mutex_lock(&fs_info->chunk_mutex); |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame^] | 617 | down_write(&dev_replace->rwsem); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 618 | dev_replace->replace_state = |
| 619 | scrub_ret ? BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED |
| 620 | : BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED; |
| 621 | dev_replace->tgtdev = NULL; |
| 622 | dev_replace->srcdev = NULL; |
Allen Pais | a944442 | 2018-06-12 17:18:25 +0530 | [diff] [blame] | 623 | dev_replace->time_stopped = ktime_get_real_seconds(); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 624 | dev_replace->item_needs_writeback = 1; |
| 625 | |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 626 | /* replace old device with new one in mapping tree */ |
| 627 | if (!scrub_ret) { |
| 628 | btrfs_dev_replace_update_device_in_mapping_tree(fs_info, |
| 629 | src_device, |
| 630 | tgt_device); |
| 631 | } else { |
Anand Jain | f9085ab | 2018-11-20 19:56:16 +0800 | [diff] [blame] | 632 | if (scrub_ret != -ECANCELED) |
| 633 | btrfs_err_in_rcu(fs_info, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 634 | "btrfs_scrub_dev(%s, %llu, %s) failed %d", |
Anand Jain | 3c958bd | 2017-11-28 10:43:10 +0800 | [diff] [blame] | 635 | btrfs_dev_name(src_device), |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 636 | src_device->devid, |
| 637 | rcu_str_deref(tgt_device->name), scrub_ret); |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame^] | 638 | up_write(&dev_replace->rwsem); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 639 | mutex_unlock(&fs_info->chunk_mutex); |
| 640 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
Qu Wenruo | ae6529c | 2017-03-29 09:33:21 +0800 | [diff] [blame] | 641 | btrfs_rm_dev_replace_blocked(fs_info); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 642 | if (tgt_device) |
Nikolay Borisov | 4f5ad7b | 2018-07-20 19:37:51 +0300 | [diff] [blame] | 643 | btrfs_destroy_dev_replace_tgtdev(tgt_device); |
Qu Wenruo | ae6529c | 2017-03-29 09:33:21 +0800 | [diff] [blame] | 644 | btrfs_rm_dev_replace_unblocked(fs_info); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 645 | mutex_unlock(&dev_replace->lock_finishing_cancel_unmount); |
| 646 | |
Eryu Guan | 2fc9f6b | 2014-10-13 12:42:12 +0800 | [diff] [blame] | 647 | return scrub_ret; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 648 | } |
| 649 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 650 | btrfs_info_in_rcu(fs_info, |
| 651 | "dev_replace from %s (devid %llu) to %s finished", |
Anand Jain | 3c958bd | 2017-11-28 10:43:10 +0800 | [diff] [blame] | 652 | btrfs_dev_name(src_device), |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 653 | src_device->devid, |
| 654 | rcu_str_deref(tgt_device->name)); |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 655 | clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &tgt_device->dev_state); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 656 | tgt_device->devid = src_device->devid; |
| 657 | src_device->devid = BTRFS_DEV_REPLACE_DEVID; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 658 | memcpy(uuid_tmp, tgt_device->uuid, sizeof(uuid_tmp)); |
| 659 | memcpy(tgt_device->uuid, src_device->uuid, sizeof(tgt_device->uuid)); |
| 660 | memcpy(src_device->uuid, uuid_tmp, sizeof(src_device->uuid)); |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 661 | btrfs_device_set_total_bytes(tgt_device, src_device->total_bytes); |
| 662 | btrfs_device_set_disk_total_bytes(tgt_device, |
| 663 | src_device->disk_total_bytes); |
| 664 | btrfs_device_set_bytes_used(tgt_device, src_device->bytes_used); |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 665 | ASSERT(list_empty(&src_device->resized_list)); |
| 666 | tgt_device->commit_total_bytes = src_device->commit_total_bytes; |
Miao Xie | ce7213c | 2014-09-03 21:35:34 +0800 | [diff] [blame] | 667 | tgt_device->commit_bytes_used = src_device->bytes_used; |
Anand Jain | 88acff6 | 2016-05-03 17:44:43 +0800 | [diff] [blame] | 668 | |
Nikolay Borisov | d6507cf | 2018-07-20 19:37:50 +0300 | [diff] [blame] | 669 | btrfs_assign_next_active_device(src_device, tgt_device); |
Anand Jain | 88acff6 | 2016-05-03 17:44:43 +0800 | [diff] [blame] | 670 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 671 | list_add(&tgt_device->dev_alloc_list, &fs_info->fs_devices->alloc_list); |
Miao Xie | 82372bc | 2014-09-03 21:35:44 +0800 | [diff] [blame] | 672 | fs_info->fs_devices->rw_devices++; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 673 | |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame^] | 674 | up_write(&dev_replace->rwsem); |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 675 | btrfs_rm_dev_replace_blocked(fs_info); |
| 676 | |
Nikolay Borisov | 68a9db5 | 2018-07-20 19:37:48 +0300 | [diff] [blame] | 677 | btrfs_rm_dev_replace_remove_srcdev(src_device); |
Ilya Dryomov | 1357272 | 2013-10-02 20:41:01 +0300 | [diff] [blame] | 678 | |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 679 | btrfs_rm_dev_replace_unblocked(fs_info); |
| 680 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 681 | /* |
Misono Tomohiro | 1e7e1f9 | 2018-07-31 16:20:21 +0900 | [diff] [blame] | 682 | * Increment dev_stats_ccnt so that btrfs_run_dev_stats() will |
| 683 | * update on-disk dev stats value during commit transaction |
| 684 | */ |
| 685 | atomic_inc(&tgt_device->dev_stats_ccnt); |
| 686 | |
| 687 | /* |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 688 | * this is again a consistent state where no dev_replace procedure |
| 689 | * is running, the target device is part of the filesystem, the |
| 690 | * source device is not part of the filesystem anymore and its 1st |
| 691 | * superblock is scratched out so that it is no longer marked to |
| 692 | * belong to this filesystem. |
| 693 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 694 | mutex_unlock(&fs_info->chunk_mutex); |
| 695 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 696 | |
Qu Wenruo | 084b6e7c | 2014-10-30 16:52:31 +0800 | [diff] [blame] | 697 | /* replace the sysfs entry */ |
Anand Jain | 3257604 | 2015-08-14 18:32:49 +0800 | [diff] [blame] | 698 | btrfs_sysfs_rm_device_link(fs_info->fs_devices, src_device); |
Qu Wenruo | 084b6e7c | 2014-10-30 16:52:31 +0800 | [diff] [blame] | 699 | btrfs_rm_dev_replace_free_srcdev(fs_info, src_device); |
| 700 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 701 | /* write back the superblocks */ |
| 702 | trans = btrfs_start_transaction(root, 0); |
| 703 | if (!IS_ERR(trans)) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 704 | btrfs_commit_transaction(trans); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 705 | |
| 706 | mutex_unlock(&dev_replace->lock_finishing_cancel_unmount); |
| 707 | |
| 708 | return 0; |
| 709 | } |
| 710 | |
| 711 | static void btrfs_dev_replace_update_device_in_mapping_tree( |
| 712 | struct btrfs_fs_info *fs_info, |
| 713 | struct btrfs_device *srcdev, |
| 714 | struct btrfs_device *tgtdev) |
| 715 | { |
| 716 | struct extent_map_tree *em_tree = &fs_info->mapping_tree.map_tree; |
| 717 | struct extent_map *em; |
| 718 | struct map_lookup *map; |
| 719 | u64 start = 0; |
| 720 | int i; |
| 721 | |
| 722 | write_lock(&em_tree->lock); |
| 723 | do { |
| 724 | em = lookup_extent_mapping(em_tree, start, (u64)-1); |
| 725 | if (!em) |
| 726 | break; |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 727 | map = em->map_lookup; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 728 | for (i = 0; i < map->num_stripes; i++) |
| 729 | if (srcdev == map->stripes[i].dev) |
| 730 | map->stripes[i].dev = tgtdev; |
| 731 | start = em->start + em->len; |
| 732 | free_extent_map(em); |
| 733 | } while (start); |
| 734 | write_unlock(&em_tree->lock); |
| 735 | } |
| 736 | |
David Sterba | 74b595f | 2017-06-14 16:24:56 +0200 | [diff] [blame] | 737 | /* |
| 738 | * Read progress of device replace status according to the state and last |
| 739 | * stored position. The value format is the same as for |
| 740 | * btrfs_dev_replace::progress_1000 |
| 741 | */ |
| 742 | static u64 btrfs_dev_replace_progress(struct btrfs_fs_info *fs_info) |
| 743 | { |
| 744 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; |
| 745 | u64 ret = 0; |
| 746 | |
| 747 | switch (dev_replace->replace_state) { |
| 748 | case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED: |
| 749 | case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED: |
| 750 | ret = 0; |
| 751 | break; |
| 752 | case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED: |
| 753 | ret = 1000; |
| 754 | break; |
| 755 | case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED: |
| 756 | case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED: |
| 757 | ret = div64_u64(dev_replace->cursor_left, |
| 758 | div_u64(btrfs_device_get_total_bytes( |
| 759 | dev_replace->srcdev), 1000)); |
| 760 | break; |
| 761 | } |
| 762 | |
| 763 | return ret; |
| 764 | } |
| 765 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 766 | void btrfs_dev_replace_status(struct btrfs_fs_info *fs_info, |
| 767 | struct btrfs_ioctl_dev_replace_args *args) |
| 768 | { |
| 769 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; |
| 770 | |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame^] | 771 | down_read(&dev_replace->rwsem); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 772 | /* even if !dev_replace_is_valid, the values are good enough for |
| 773 | * the replace_status ioctl */ |
| 774 | args->result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR; |
| 775 | args->status.replace_state = dev_replace->replace_state; |
| 776 | args->status.time_started = dev_replace->time_started; |
| 777 | args->status.time_stopped = dev_replace->time_stopped; |
| 778 | args->status.num_write_errors = |
| 779 | atomic64_read(&dev_replace->num_write_errors); |
| 780 | args->status.num_uncorrectable_read_errors = |
| 781 | atomic64_read(&dev_replace->num_uncorrectable_read_errors); |
David Sterba | 74b595f | 2017-06-14 16:24:56 +0200 | [diff] [blame] | 782 | args->status.progress_1000 = btrfs_dev_replace_progress(fs_info); |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame^] | 783 | up_read(&dev_replace->rwsem); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 784 | } |
| 785 | |
Anand Jain | 18e67c7 | 2018-02-12 23:33:31 +0800 | [diff] [blame] | 786 | int btrfs_dev_replace_cancel(struct btrfs_fs_info *fs_info) |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 787 | { |
| 788 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; |
| 789 | struct btrfs_device *tgt_device = NULL; |
Anand Jain | 8f2ceaa | 2018-02-13 11:53:43 +0800 | [diff] [blame] | 790 | struct btrfs_device *src_device = NULL; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 791 | struct btrfs_trans_handle *trans; |
| 792 | struct btrfs_root *root = fs_info->tree_root; |
Anand Jain | 18e67c7 | 2018-02-12 23:33:31 +0800 | [diff] [blame] | 793 | int result; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 794 | int ret; |
| 795 | |
David Howells | bc98a42 | 2017-07-17 08:45:34 +0100 | [diff] [blame] | 796 | if (sb_rdonly(fs_info->sb)) |
Ilya Dryomov | e649e58 | 2013-10-10 20:40:21 +0300 | [diff] [blame] | 797 | return -EROFS; |
| 798 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 799 | mutex_lock(&dev_replace->lock_finishing_cancel_unmount); |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame^] | 800 | down_write(&dev_replace->rwsem); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 801 | switch (dev_replace->replace_state) { |
| 802 | case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED: |
| 803 | case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED: |
| 804 | case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED: |
| 805 | result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NOT_STARTED; |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame^] | 806 | up_write(&dev_replace->rwsem); |
Anand Jain | d189dd7 | 2018-11-14 13:50:26 +0800 | [diff] [blame] | 807 | break; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 808 | case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED: |
Anand Jain | d189dd7 | 2018-11-14 13:50:26 +0800 | [diff] [blame] | 809 | tgt_device = dev_replace->tgtdev; |
| 810 | src_device = dev_replace->srcdev; |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame^] | 811 | up_write(&dev_replace->rwsem); |
Anand Jain | b47dda2 | 2018-11-11 22:22:20 +0800 | [diff] [blame] | 812 | ret = btrfs_scrub_cancel(fs_info); |
| 813 | if (ret < 0) { |
| 814 | result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NOT_STARTED; |
| 815 | } else { |
| 816 | result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR; |
| 817 | /* |
| 818 | * btrfs_dev_replace_finishing() will handle the |
| 819 | * cleanup part |
| 820 | */ |
| 821 | btrfs_info_in_rcu(fs_info, |
| 822 | "dev_replace from %s (devid %llu) to %s canceled", |
| 823 | btrfs_dev_name(src_device), src_device->devid, |
| 824 | btrfs_dev_name(tgt_device)); |
| 825 | } |
Anand Jain | d189dd7 | 2018-11-14 13:50:26 +0800 | [diff] [blame] | 826 | break; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 827 | case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED: |
Anand Jain | d189dd7 | 2018-11-14 13:50:26 +0800 | [diff] [blame] | 828 | /* |
| 829 | * Scrub doing the replace isn't running so we need to do the |
| 830 | * cleanup step of btrfs_dev_replace_finishing() here |
| 831 | */ |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 832 | result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR; |
| 833 | tgt_device = dev_replace->tgtdev; |
Anand Jain | 8f2ceaa | 2018-02-13 11:53:43 +0800 | [diff] [blame] | 834 | src_device = dev_replace->srcdev; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 835 | dev_replace->tgtdev = NULL; |
| 836 | dev_replace->srcdev = NULL; |
Anand Jain | d189dd7 | 2018-11-14 13:50:26 +0800 | [diff] [blame] | 837 | dev_replace->replace_state = |
| 838 | BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED; |
| 839 | dev_replace->time_stopped = ktime_get_real_seconds(); |
| 840 | dev_replace->item_needs_writeback = 1; |
| 841 | |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame^] | 842 | up_write(&dev_replace->rwsem); |
Anand Jain | d189dd7 | 2018-11-14 13:50:26 +0800 | [diff] [blame] | 843 | |
Anand Jain | fe97e2e | 2018-11-11 22:22:21 +0800 | [diff] [blame] | 844 | /* Scrub for replace must not be running in suspended state */ |
| 845 | ret = btrfs_scrub_cancel(fs_info); |
| 846 | ASSERT(ret != -ENOTCONN); |
Anand Jain | d189dd7 | 2018-11-14 13:50:26 +0800 | [diff] [blame] | 847 | |
| 848 | trans = btrfs_start_transaction(root, 0); |
| 849 | if (IS_ERR(trans)) { |
| 850 | mutex_unlock(&dev_replace->lock_finishing_cancel_unmount); |
| 851 | return PTR_ERR(trans); |
| 852 | } |
| 853 | ret = btrfs_commit_transaction(trans); |
| 854 | WARN_ON(ret); |
| 855 | |
| 856 | btrfs_info_in_rcu(fs_info, |
| 857 | "suspended dev_replace from %s (devid %llu) to %s canceled", |
| 858 | btrfs_dev_name(src_device), src_device->devid, |
| 859 | btrfs_dev_name(tgt_device)); |
| 860 | |
| 861 | if (tgt_device) |
| 862 | btrfs_destroy_dev_replace_tgtdev(tgt_device); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 863 | break; |
Anand Jain | d189dd7 | 2018-11-14 13:50:26 +0800 | [diff] [blame] | 864 | default: |
| 865 | result = -EINVAL; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 866 | } |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 867 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 868 | mutex_unlock(&dev_replace->lock_finishing_cancel_unmount); |
| 869 | return result; |
| 870 | } |
| 871 | |
| 872 | void btrfs_dev_replace_suspend_for_unmount(struct btrfs_fs_info *fs_info) |
| 873 | { |
| 874 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; |
| 875 | |
| 876 | mutex_lock(&dev_replace->lock_finishing_cancel_unmount); |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame^] | 877 | down_write(&dev_replace->rwsem); |
| 878 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 879 | switch (dev_replace->replace_state) { |
| 880 | case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED: |
| 881 | case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED: |
| 882 | case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED: |
| 883 | case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED: |
| 884 | break; |
| 885 | case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED: |
| 886 | dev_replace->replace_state = |
| 887 | BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED; |
Allen Pais | a944442 | 2018-06-12 17:18:25 +0530 | [diff] [blame] | 888 | dev_replace->time_stopped = ktime_get_real_seconds(); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 889 | dev_replace->item_needs_writeback = 1; |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 890 | btrfs_info(fs_info, "suspending dev_replace for unmount"); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 891 | break; |
| 892 | } |
| 893 | |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame^] | 894 | up_write(&dev_replace->rwsem); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 895 | mutex_unlock(&dev_replace->lock_finishing_cancel_unmount); |
| 896 | } |
| 897 | |
| 898 | /* resume dev_replace procedure that was interrupted by unmount */ |
| 899 | int btrfs_resume_dev_replace_async(struct btrfs_fs_info *fs_info) |
| 900 | { |
| 901 | struct task_struct *task; |
| 902 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; |
| 903 | |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame^] | 904 | down_write(&dev_replace->rwsem); |
| 905 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 906 | switch (dev_replace->replace_state) { |
| 907 | case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED: |
| 908 | case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED: |
| 909 | case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED: |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame^] | 910 | up_write(&dev_replace->rwsem); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 911 | return 0; |
| 912 | case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED: |
| 913 | break; |
| 914 | case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED: |
| 915 | dev_replace->replace_state = |
| 916 | BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED; |
| 917 | break; |
| 918 | } |
| 919 | if (!dev_replace->tgtdev || !dev_replace->tgtdev->bdev) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 920 | btrfs_info(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 921 | "cannot continue dev_replace, tgtdev is missing"); |
| 922 | btrfs_info(fs_info, |
| 923 | "you may cancel the operation after 'mount -o degraded'"); |
Anand Jain | 0d228ec | 2018-11-11 22:22:17 +0800 | [diff] [blame] | 924 | dev_replace->replace_state = |
| 925 | BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED; |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame^] | 926 | up_write(&dev_replace->rwsem); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 927 | return 0; |
| 928 | } |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame^] | 929 | up_write(&dev_replace->rwsem); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 930 | |
David Sterba | 010a47b | 2018-03-20 19:51:04 +0100 | [diff] [blame] | 931 | /* |
| 932 | * This could collide with a paused balance, but the exclusive op logic |
| 933 | * should never allow both to start and pause. We don't want to allow |
| 934 | * dev-replace to start anyway. |
| 935 | */ |
| 936 | if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) { |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame^] | 937 | down_write(&dev_replace->rwsem); |
Anand Jain | 05c49e6 | 2018-11-11 22:22:18 +0800 | [diff] [blame] | 938 | dev_replace->replace_state = |
| 939 | BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED; |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame^] | 940 | up_write(&dev_replace->rwsem); |
David Sterba | 010a47b | 2018-03-20 19:51:04 +0100 | [diff] [blame] | 941 | btrfs_info(fs_info, |
| 942 | "cannot resume dev-replace, other exclusive operation running"); |
| 943 | return 0; |
| 944 | } |
| 945 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 946 | task = kthread_run(btrfs_dev_replace_kthread, fs_info, "btrfs-devrepl"); |
Rusty Russell | 8c6ffba | 2013-07-15 11:20:32 +0930 | [diff] [blame] | 947 | return PTR_ERR_OR_ZERO(task); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | static int btrfs_dev_replace_kthread(void *data) |
| 951 | { |
| 952 | struct btrfs_fs_info *fs_info = data; |
| 953 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 954 | u64 progress; |
David Sterba | 00251a5 | 2018-03-20 15:35:50 +0100 | [diff] [blame] | 955 | int ret; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 956 | |
David Sterba | f1b8a1e | 2017-06-14 16:28:42 +0200 | [diff] [blame] | 957 | progress = btrfs_dev_replace_progress(fs_info); |
| 958 | progress = div_u64(progress, 10); |
| 959 | btrfs_info_in_rcu(fs_info, |
Anand Jain | 3c958bd | 2017-11-28 10:43:10 +0800 | [diff] [blame] | 960 | "continuing dev_replace from %s (devid %llu) to target %s @%u%%", |
| 961 | btrfs_dev_name(dev_replace->srcdev), |
David Sterba | f1b8a1e | 2017-06-14 16:28:42 +0200 | [diff] [blame] | 962 | dev_replace->srcdev->devid, |
Anand Jain | 3c958bd | 2017-11-28 10:43:10 +0800 | [diff] [blame] | 963 | btrfs_dev_name(dev_replace->tgtdev), |
David Sterba | f1b8a1e | 2017-06-14 16:28:42 +0200 | [diff] [blame] | 964 | (unsigned int)progress); |
| 965 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 966 | ret = btrfs_scrub_dev(fs_info, dev_replace->srcdev->devid, |
| 967 | dev_replace->committed_cursor_left, |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 968 | btrfs_device_get_total_bytes(dev_replace->srcdev), |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 969 | &dev_replace->scrub_progress, 0, 1); |
| 970 | ret = btrfs_dev_replace_finishing(fs_info, ret); |
Anand Jain | 49365e6 | 2018-11-20 19:56:15 +0800 | [diff] [blame] | 971 | WARN_ON(ret && ret != -ECANCELED); |
David Sterba | 00251a5 | 2018-03-20 15:35:50 +0100 | [diff] [blame] | 972 | |
| 973 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 974 | return 0; |
| 975 | } |
| 976 | |
| 977 | int btrfs_dev_replace_is_ongoing(struct btrfs_dev_replace *dev_replace) |
| 978 | { |
| 979 | if (!dev_replace->is_valid) |
| 980 | return 0; |
| 981 | |
| 982 | switch (dev_replace->replace_state) { |
| 983 | case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED: |
| 984 | case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED: |
| 985 | case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED: |
| 986 | return 0; |
| 987 | case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED: |
| 988 | case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED: |
| 989 | /* |
| 990 | * return true even if tgtdev is missing (this is |
| 991 | * something that can happen if the dev_replace |
| 992 | * procedure is suspended by an umount and then |
| 993 | * the tgtdev is missing (or "btrfs dev scan") was |
| 994 | * not called and the the filesystem is remounted |
| 995 | * in degraded state. This does not stop the |
| 996 | * dev_replace procedure. It needs to be canceled |
Adam Buchbinder | bb7ab3b | 2016-03-04 11:23:12 -0800 | [diff] [blame] | 997 | * manually if the cancellation is wanted. |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 998 | */ |
| 999 | break; |
| 1000 | } |
| 1001 | return 1; |
| 1002 | } |
| 1003 | |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 1004 | void btrfs_bio_counter_inc_noblocked(struct btrfs_fs_info *fs_info) |
| 1005 | { |
David Sterba | 7f8d236 | 2018-04-05 01:04:49 +0200 | [diff] [blame] | 1006 | percpu_counter_inc(&fs_info->dev_replace.bio_counter); |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 1007 | } |
| 1008 | |
Miao Xie | 4245215 | 2014-11-25 16:39:28 +0800 | [diff] [blame] | 1009 | void btrfs_bio_counter_sub(struct btrfs_fs_info *fs_info, s64 amount) |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 1010 | { |
David Sterba | 7f8d236 | 2018-04-05 01:04:49 +0200 | [diff] [blame] | 1011 | percpu_counter_sub(&fs_info->dev_replace.bio_counter, amount); |
| 1012 | cond_wake_up_nomb(&fs_info->dev_replace.replace_wait); |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 1013 | } |
| 1014 | |
| 1015 | void btrfs_bio_counter_inc_blocked(struct btrfs_fs_info *fs_info) |
| 1016 | { |
Zhao Lei | 09dd7a0 | 2015-01-20 15:11:37 +0800 | [diff] [blame] | 1017 | while (1) { |
David Sterba | 7f8d236 | 2018-04-05 01:04:49 +0200 | [diff] [blame] | 1018 | percpu_counter_inc(&fs_info->dev_replace.bio_counter); |
Zhao Lei | 09dd7a0 | 2015-01-20 15:11:37 +0800 | [diff] [blame] | 1019 | if (likely(!test_bit(BTRFS_FS_STATE_DEV_REPLACING, |
| 1020 | &fs_info->fs_state))) |
| 1021 | break; |
| 1022 | |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 1023 | btrfs_bio_counter_dec(fs_info); |
David Sterba | 7f8d236 | 2018-04-05 01:04:49 +0200 | [diff] [blame] | 1024 | wait_event(fs_info->dev_replace.replace_wait, |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 1025 | !test_bit(BTRFS_FS_STATE_DEV_REPLACING, |
| 1026 | &fs_info->fs_state)); |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 1027 | } |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 1028 | } |