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> |
David Sterba | 602cbe9 | 2019-08-21 18:48:25 +0200 | [diff] [blame] | 12 | #include "misc.h" |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 13 | #include "ctree.h" |
| 14 | #include "extent_map.h" |
| 15 | #include "disk-io.h" |
| 16 | #include "transaction.h" |
| 17 | #include "print-tree.h" |
| 18 | #include "volumes.h" |
| 19 | #include "async-thread.h" |
| 20 | #include "check-integrity.h" |
| 21 | #include "rcu-string.h" |
| 22 | #include "dev-replace.h" |
Anand Jain | 49c6f73 | 2014-06-03 11:36:02 +0800 | [diff] [blame] | 23 | #include "sysfs.h" |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 24 | |
Qu Wenruo | 30b3688 | 2020-01-23 15:44:50 +0800 | [diff] [blame] | 25 | /* |
| 26 | * Device replace overview |
| 27 | * |
| 28 | * [Objective] |
| 29 | * To copy all extents (both new and on-disk) from source device to target |
| 30 | * device, while still keeping the filesystem read-write. |
| 31 | * |
| 32 | * [Method] |
| 33 | * There are two main methods involved: |
| 34 | * |
| 35 | * - Write duplication |
| 36 | * |
| 37 | * All new writes will be written to both target and source devices, so even |
| 38 | * if replace gets canceled, sources device still contans up-to-date data. |
| 39 | * |
| 40 | * Location: handle_ops_on_dev_replace() from __btrfs_map_block() |
| 41 | * Start: btrfs_dev_replace_start() |
| 42 | * End: btrfs_dev_replace_finishing() |
| 43 | * Content: Latest data/metadata |
| 44 | * |
| 45 | * - Copy existing extents |
| 46 | * |
| 47 | * This happens by re-using scrub facility, as scrub also iterates through |
| 48 | * existing extents from commit root. |
| 49 | * |
| 50 | * Location: scrub_write_block_to_dev_replace() from |
| 51 | * scrub_block_complete() |
| 52 | * Content: Data/meta from commit root. |
| 53 | * |
| 54 | * Due to the content difference, we need to avoid nocow write when dev-replace |
| 55 | * is happening. This is done by marking the block group read-only and waiting |
| 56 | * for NOCOW writes. |
| 57 | * |
| 58 | * After replace is done, the finishing part is done by swapping the target and |
| 59 | * source devices. |
| 60 | * |
| 61 | * Location: btrfs_dev_replace_update_device_in_mapping_tree() from |
| 62 | * btrfs_dev_replace_finishing() |
| 63 | */ |
| 64 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 65 | static int btrfs_dev_replace_finishing(struct btrfs_fs_info *fs_info, |
| 66 | int scrub_ret); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 67 | static int btrfs_dev_replace_kthread(void *data); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 68 | |
| 69 | int btrfs_init_dev_replace(struct btrfs_fs_info *fs_info) |
| 70 | { |
| 71 | struct btrfs_key key; |
| 72 | struct btrfs_root *dev_root = fs_info->dev_root; |
| 73 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; |
| 74 | struct extent_buffer *eb; |
| 75 | int slot; |
| 76 | int ret = 0; |
| 77 | struct btrfs_path *path = NULL; |
| 78 | int item_size; |
| 79 | struct btrfs_dev_replace_item *ptr; |
| 80 | u64 src_devid; |
| 81 | |
| 82 | path = btrfs_alloc_path(); |
| 83 | if (!path) { |
| 84 | ret = -ENOMEM; |
| 85 | goto out; |
| 86 | } |
| 87 | |
| 88 | key.objectid = 0; |
| 89 | key.type = BTRFS_DEV_REPLACE_KEY; |
| 90 | key.offset = 0; |
| 91 | ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0); |
| 92 | if (ret) { |
| 93 | no_valid_dev_replace_entry_found: |
Anand Jain | cf89af1 | 2020-10-30 06:53:56 +0800 | [diff] [blame] | 94 | /* |
| 95 | * We don't have a replace item or it's corrupted. If there is |
| 96 | * a replace target, fail the mount. |
| 97 | */ |
| 98 | if (btrfs_find_device(fs_info->fs_devices, |
Anand Jain | b2598ed | 2020-11-03 13:49:43 +0800 | [diff] [blame^] | 99 | BTRFS_DEV_REPLACE_DEVID, NULL, NULL)) { |
Anand Jain | cf89af1 | 2020-10-30 06:53:56 +0800 | [diff] [blame] | 100 | btrfs_err(fs_info, |
| 101 | "found replace target device without a valid replace item"); |
| 102 | ret = -EUCLEAN; |
| 103 | goto out; |
| 104 | } |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 105 | ret = 0; |
| 106 | dev_replace->replace_state = |
Anand Jain | 27e022a | 2019-08-08 12:32:44 +0800 | [diff] [blame] | 107 | BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 108 | dev_replace->cont_reading_from_srcdev_mode = |
| 109 | BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_ALWAYS; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 110 | dev_replace->time_started = 0; |
| 111 | dev_replace->time_stopped = 0; |
| 112 | atomic64_set(&dev_replace->num_write_errors, 0); |
| 113 | atomic64_set(&dev_replace->num_uncorrectable_read_errors, 0); |
| 114 | dev_replace->cursor_left = 0; |
| 115 | dev_replace->committed_cursor_left = 0; |
| 116 | dev_replace->cursor_left_last_write_of_item = 0; |
| 117 | dev_replace->cursor_right = 0; |
| 118 | dev_replace->srcdev = NULL; |
| 119 | dev_replace->tgtdev = NULL; |
| 120 | dev_replace->is_valid = 0; |
| 121 | dev_replace->item_needs_writeback = 0; |
| 122 | goto out; |
| 123 | } |
| 124 | slot = path->slots[0]; |
| 125 | eb = path->nodes[0]; |
| 126 | item_size = btrfs_item_size_nr(eb, slot); |
| 127 | ptr = btrfs_item_ptr(eb, slot, struct btrfs_dev_replace_item); |
| 128 | |
| 129 | if (item_size != sizeof(struct btrfs_dev_replace_item)) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 130 | btrfs_warn(fs_info, |
| 131 | "dev_replace entry found has unexpected size, ignore entry"); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 132 | goto no_valid_dev_replace_entry_found; |
| 133 | } |
| 134 | |
| 135 | src_devid = btrfs_dev_replace_src_devid(eb, ptr); |
| 136 | dev_replace->cont_reading_from_srcdev_mode = |
| 137 | btrfs_dev_replace_cont_reading_from_srcdev_mode(eb, ptr); |
| 138 | dev_replace->replace_state = btrfs_dev_replace_replace_state(eb, ptr); |
| 139 | dev_replace->time_started = btrfs_dev_replace_time_started(eb, ptr); |
| 140 | dev_replace->time_stopped = |
| 141 | btrfs_dev_replace_time_stopped(eb, ptr); |
| 142 | atomic64_set(&dev_replace->num_write_errors, |
| 143 | btrfs_dev_replace_num_write_errors(eb, ptr)); |
| 144 | atomic64_set(&dev_replace->num_uncorrectable_read_errors, |
| 145 | btrfs_dev_replace_num_uncorrectable_read_errors(eb, ptr)); |
| 146 | dev_replace->cursor_left = btrfs_dev_replace_cursor_left(eb, ptr); |
| 147 | dev_replace->committed_cursor_left = dev_replace->cursor_left; |
| 148 | dev_replace->cursor_left_last_write_of_item = dev_replace->cursor_left; |
| 149 | dev_replace->cursor_right = btrfs_dev_replace_cursor_right(eb, ptr); |
| 150 | dev_replace->is_valid = 1; |
| 151 | |
| 152 | dev_replace->item_needs_writeback = 0; |
| 153 | switch (dev_replace->replace_state) { |
| 154 | case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED: |
| 155 | case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED: |
| 156 | case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED: |
Anand Jain | cf89af1 | 2020-10-30 06:53:56 +0800 | [diff] [blame] | 157 | /* |
| 158 | * We don't have an active replace item but if there is a |
| 159 | * replace target, fail the mount. |
| 160 | */ |
| 161 | if (btrfs_find_device(fs_info->fs_devices, |
Anand Jain | b2598ed | 2020-11-03 13:49:43 +0800 | [diff] [blame^] | 162 | BTRFS_DEV_REPLACE_DEVID, NULL, NULL)) { |
Anand Jain | cf89af1 | 2020-10-30 06:53:56 +0800 | [diff] [blame] | 163 | btrfs_err(fs_info, |
| 164 | "replace devid present without an active replace item"); |
| 165 | ret = -EUCLEAN; |
| 166 | } else { |
| 167 | dev_replace->srcdev = NULL; |
| 168 | dev_replace->tgtdev = NULL; |
| 169 | } |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 170 | break; |
| 171 | case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED: |
| 172 | case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED: |
Anand Jain | e4319cd | 2019-01-17 23:32:31 +0800 | [diff] [blame] | 173 | dev_replace->srcdev = btrfs_find_device(fs_info->fs_devices, |
Anand Jain | b2598ed | 2020-11-03 13:49:43 +0800 | [diff] [blame^] | 174 | src_devid, NULL, NULL); |
Anand Jain | e4319cd | 2019-01-17 23:32:31 +0800 | [diff] [blame] | 175 | dev_replace->tgtdev = btrfs_find_device(fs_info->fs_devices, |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 176 | BTRFS_DEV_REPLACE_DEVID, |
Anand Jain | b2598ed | 2020-11-03 13:49:43 +0800 | [diff] [blame^] | 177 | NULL, NULL); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 178 | /* |
| 179 | * allow 'btrfs dev replace_cancel' if src/tgt device is |
| 180 | * missing |
| 181 | */ |
| 182 | if (!dev_replace->srcdev && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 183 | !btrfs_test_opt(fs_info, DEGRADED)) { |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 184 | ret = -EIO; |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 185 | btrfs_warn(fs_info, |
| 186 | "cannot mount because device replace operation is ongoing and"); |
| 187 | btrfs_warn(fs_info, |
| 188 | "srcdev (devid %llu) is missing, need to run 'btrfs dev scan'?", |
| 189 | src_devid); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 190 | } |
| 191 | if (!dev_replace->tgtdev && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 192 | !btrfs_test_opt(fs_info, DEGRADED)) { |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 193 | ret = -EIO; |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 194 | btrfs_warn(fs_info, |
| 195 | "cannot mount because device replace operation is ongoing and"); |
| 196 | btrfs_warn(fs_info, |
| 197 | "tgtdev (devid %llu) is missing, need to run 'btrfs dev scan'?", |
Geert Uytterhoeven | 6e71c47 | 2013-08-20 13:20:08 +0200 | [diff] [blame] | 198 | BTRFS_DEV_REPLACE_DEVID); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 199 | } |
| 200 | if (dev_replace->tgtdev) { |
| 201 | if (dev_replace->srcdev) { |
| 202 | dev_replace->tgtdev->total_bytes = |
| 203 | dev_replace->srcdev->total_bytes; |
| 204 | dev_replace->tgtdev->disk_total_bytes = |
| 205 | dev_replace->srcdev->disk_total_bytes; |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 206 | dev_replace->tgtdev->commit_total_bytes = |
| 207 | dev_replace->srcdev->commit_total_bytes; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 208 | dev_replace->tgtdev->bytes_used = |
| 209 | dev_replace->srcdev->bytes_used; |
Miao Xie | ce7213c | 2014-09-03 21:35:34 +0800 | [diff] [blame] | 210 | dev_replace->tgtdev->commit_bytes_used = |
| 211 | dev_replace->srcdev->commit_bytes_used; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 212 | } |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 213 | set_bit(BTRFS_DEV_STATE_REPLACE_TGT, |
| 214 | &dev_replace->tgtdev->dev_state); |
Anand Jain | 15fc128 | 2018-02-12 23:36:25 +0800 | [diff] [blame] | 215 | |
| 216 | WARN_ON(fs_info->fs_devices->rw_devices == 0); |
| 217 | dev_replace->tgtdev->io_width = fs_info->sectorsize; |
| 218 | dev_replace->tgtdev->io_align = fs_info->sectorsize; |
| 219 | dev_replace->tgtdev->sector_size = fs_info->sectorsize; |
| 220 | dev_replace->tgtdev->fs_info = fs_info; |
| 221 | set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, |
| 222 | &dev_replace->tgtdev->dev_state); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 223 | } |
| 224 | break; |
| 225 | } |
| 226 | |
| 227 | out: |
Tsutomu Itoh | 527afb4 | 2015-08-19 14:55:00 +0900 | [diff] [blame] | 228 | btrfs_free_path(path); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 229 | return ret; |
| 230 | } |
| 231 | |
| 232 | /* |
David Sterba | d48f39d | 2018-03-20 16:09:48 +0100 | [diff] [blame] | 233 | * Initialize a new device for device replace target from a given source dev |
| 234 | * and path. |
| 235 | * |
| 236 | * Return 0 and new device in @device_out, otherwise return < 0 |
| 237 | */ |
| 238 | static int btrfs_init_dev_replace_tgtdev(struct btrfs_fs_info *fs_info, |
| 239 | const char *device_path, |
| 240 | struct btrfs_device *srcdev, |
| 241 | struct btrfs_device **device_out) |
| 242 | { |
| 243 | struct btrfs_device *device; |
| 244 | struct block_device *bdev; |
David Sterba | d48f39d | 2018-03-20 16:09:48 +0100 | [diff] [blame] | 245 | struct rcu_string *name; |
| 246 | u64 devid = BTRFS_DEV_REPLACE_DEVID; |
| 247 | int ret = 0; |
| 248 | |
| 249 | *device_out = NULL; |
Anand Jain | c6a5d95 | 2020-09-05 01:34:22 +0800 | [diff] [blame] | 250 | if (srcdev->fs_devices->seeding) { |
David Sterba | d48f39d | 2018-03-20 16:09:48 +0100 | [diff] [blame] | 251 | btrfs_err(fs_info, "the filesystem is a seed filesystem!"); |
| 252 | return -EINVAL; |
| 253 | } |
| 254 | |
| 255 | bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL, |
| 256 | fs_info->bdev_holder); |
| 257 | if (IS_ERR(bdev)) { |
| 258 | btrfs_err(fs_info, "target device %s is invalid!", device_path); |
| 259 | return PTR_ERR(bdev); |
| 260 | } |
| 261 | |
Nikolay Borisov | ddb9378 | 2019-05-14 13:54:38 +0300 | [diff] [blame] | 262 | sync_blockdev(bdev); |
David Sterba | d48f39d | 2018-03-20 16:09:48 +0100 | [diff] [blame] | 263 | |
Anand Jain | 1888709 | 2020-09-05 01:34:32 +0800 | [diff] [blame] | 264 | list_for_each_entry(device, &fs_info->fs_devices->devices, dev_list) { |
David Sterba | d48f39d | 2018-03-20 16:09:48 +0100 | [diff] [blame] | 265 | if (device->bdev == bdev) { |
| 266 | btrfs_err(fs_info, |
| 267 | "target device is in the filesystem!"); |
| 268 | ret = -EEXIST; |
| 269 | goto error; |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | |
| 274 | if (i_size_read(bdev->bd_inode) < |
| 275 | btrfs_device_get_total_bytes(srcdev)) { |
| 276 | btrfs_err(fs_info, |
| 277 | "target device is smaller than source device!"); |
| 278 | ret = -EINVAL; |
| 279 | goto error; |
| 280 | } |
| 281 | |
| 282 | |
| 283 | device = btrfs_alloc_device(NULL, &devid, NULL); |
| 284 | if (IS_ERR(device)) { |
| 285 | ret = PTR_ERR(device); |
| 286 | goto error; |
| 287 | } |
| 288 | |
| 289 | name = rcu_string_strdup(device_path, GFP_KERNEL); |
| 290 | if (!name) { |
| 291 | btrfs_free_device(device); |
| 292 | ret = -ENOMEM; |
| 293 | goto error; |
| 294 | } |
| 295 | rcu_assign_pointer(device->name, name); |
| 296 | |
David Sterba | d48f39d | 2018-03-20 16:09:48 +0100 | [diff] [blame] | 297 | set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); |
| 298 | device->generation = 0; |
| 299 | device->io_width = fs_info->sectorsize; |
| 300 | device->io_align = fs_info->sectorsize; |
| 301 | device->sector_size = fs_info->sectorsize; |
| 302 | device->total_bytes = btrfs_device_get_total_bytes(srcdev); |
| 303 | device->disk_total_bytes = btrfs_device_get_disk_total_bytes(srcdev); |
| 304 | device->bytes_used = btrfs_device_get_bytes_used(srcdev); |
| 305 | device->commit_total_bytes = srcdev->commit_total_bytes; |
| 306 | device->commit_bytes_used = device->bytes_used; |
| 307 | device->fs_info = fs_info; |
| 308 | device->bdev = bdev; |
| 309 | set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state); |
| 310 | set_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state); |
| 311 | device->mode = FMODE_EXCL; |
| 312 | device->dev_stats_valid = 1; |
| 313 | set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE); |
| 314 | device->fs_devices = fs_info->fs_devices; |
Nikolay Borisov | b0d9e1e | 2019-05-14 13:54:39 +0300 | [diff] [blame] | 315 | |
| 316 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
David Sterba | d48f39d | 2018-03-20 16:09:48 +0100 | [diff] [blame] | 317 | list_add(&device->dev_list, &fs_info->fs_devices->devices); |
| 318 | fs_info->fs_devices->num_devices++; |
| 319 | fs_info->fs_devices->open_devices++; |
| 320 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
| 321 | |
| 322 | *device_out = device; |
| 323 | return 0; |
| 324 | |
| 325 | error: |
| 326 | blkdev_put(bdev, FMODE_EXCL); |
| 327 | return ret; |
| 328 | } |
| 329 | |
| 330 | /* |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 331 | * called from commit_transaction. Writes changed device replace state to |
| 332 | * disk. |
| 333 | */ |
David Sterba | 2b584c6 | 2019-03-20 16:51:44 +0100 | [diff] [blame] | 334 | int btrfs_run_dev_replace(struct btrfs_trans_handle *trans) |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 335 | { |
David Sterba | 2b584c6 | 2019-03-20 16:51:44 +0100 | [diff] [blame] | 336 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 337 | int ret; |
| 338 | struct btrfs_root *dev_root = fs_info->dev_root; |
| 339 | struct btrfs_path *path; |
| 340 | struct btrfs_key key; |
| 341 | struct extent_buffer *eb; |
| 342 | struct btrfs_dev_replace_item *ptr; |
| 343 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; |
| 344 | |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 345 | down_read(&dev_replace->rwsem); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 346 | if (!dev_replace->is_valid || |
| 347 | !dev_replace->item_needs_writeback) { |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 348 | up_read(&dev_replace->rwsem); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 349 | return 0; |
| 350 | } |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 351 | up_read(&dev_replace->rwsem); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 352 | |
| 353 | key.objectid = 0; |
| 354 | key.type = BTRFS_DEV_REPLACE_KEY; |
| 355 | key.offset = 0; |
| 356 | |
| 357 | path = btrfs_alloc_path(); |
| 358 | if (!path) { |
| 359 | ret = -ENOMEM; |
| 360 | goto out; |
| 361 | } |
| 362 | ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1); |
| 363 | if (ret < 0) { |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 364 | btrfs_warn(fs_info, |
| 365 | "error %d while searching for dev_replace item!", |
| 366 | ret); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 367 | goto out; |
| 368 | } |
| 369 | |
| 370 | if (ret == 0 && |
| 371 | btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) { |
| 372 | /* |
| 373 | * need to delete old one and insert a new one. |
| 374 | * Since no attempt is made to recover any old state, if the |
| 375 | * dev_replace state is 'running', the data on the target |
| 376 | * drive is lost. |
| 377 | * It would be possible to recover the state: just make sure |
| 378 | * that the beginning of the item is never changed and always |
| 379 | * contains all the essential information. Then read this |
| 380 | * minimal set of information and use it as a base for the |
| 381 | * new state. |
| 382 | */ |
| 383 | ret = btrfs_del_item(trans, dev_root, path); |
| 384 | if (ret != 0) { |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 385 | btrfs_warn(fs_info, |
| 386 | "delete too small dev_replace item failed %d!", |
| 387 | ret); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 388 | goto out; |
| 389 | } |
| 390 | ret = 1; |
| 391 | } |
| 392 | |
| 393 | if (ret == 1) { |
| 394 | /* need to insert a new item */ |
| 395 | btrfs_release_path(path); |
| 396 | ret = btrfs_insert_empty_item(trans, dev_root, path, |
| 397 | &key, sizeof(*ptr)); |
| 398 | if (ret < 0) { |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 399 | btrfs_warn(fs_info, |
| 400 | "insert dev_replace item failed %d!", ret); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 401 | goto out; |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | eb = path->nodes[0]; |
| 406 | ptr = btrfs_item_ptr(eb, path->slots[0], |
| 407 | struct btrfs_dev_replace_item); |
| 408 | |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 409 | down_write(&dev_replace->rwsem); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 410 | if (dev_replace->srcdev) |
| 411 | btrfs_set_dev_replace_src_devid(eb, ptr, |
| 412 | dev_replace->srcdev->devid); |
| 413 | else |
| 414 | btrfs_set_dev_replace_src_devid(eb, ptr, (u64)-1); |
| 415 | btrfs_set_dev_replace_cont_reading_from_srcdev_mode(eb, ptr, |
| 416 | dev_replace->cont_reading_from_srcdev_mode); |
| 417 | btrfs_set_dev_replace_replace_state(eb, ptr, |
| 418 | dev_replace->replace_state); |
| 419 | btrfs_set_dev_replace_time_started(eb, ptr, dev_replace->time_started); |
| 420 | btrfs_set_dev_replace_time_stopped(eb, ptr, dev_replace->time_stopped); |
| 421 | btrfs_set_dev_replace_num_write_errors(eb, ptr, |
| 422 | atomic64_read(&dev_replace->num_write_errors)); |
| 423 | btrfs_set_dev_replace_num_uncorrectable_read_errors(eb, ptr, |
| 424 | atomic64_read(&dev_replace->num_uncorrectable_read_errors)); |
| 425 | dev_replace->cursor_left_last_write_of_item = |
| 426 | dev_replace->cursor_left; |
| 427 | btrfs_set_dev_replace_cursor_left(eb, ptr, |
| 428 | dev_replace->cursor_left_last_write_of_item); |
| 429 | btrfs_set_dev_replace_cursor_right(eb, ptr, |
| 430 | dev_replace->cursor_right); |
| 431 | dev_replace->item_needs_writeback = 0; |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 432 | up_write(&dev_replace->rwsem); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 433 | |
| 434 | btrfs_mark_buffer_dirty(eb); |
| 435 | |
| 436 | out: |
| 437 | btrfs_free_path(path); |
| 438 | |
| 439 | return ret; |
| 440 | } |
| 441 | |
Anand Jain | 3c958bd | 2017-11-28 10:43:10 +0800 | [diff] [blame] | 442 | static char* btrfs_dev_name(struct btrfs_device *device) |
| 443 | { |
Anand Jain | acf18c5 | 2018-02-24 19:43:56 +0800 | [diff] [blame] | 444 | if (!device || test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) |
Anand Jain | 3c958bd | 2017-11-28 10:43:10 +0800 | [diff] [blame] | 445 | return "<missing disk>"; |
| 446 | else |
| 447 | return rcu_str_deref(device->name); |
| 448 | } |
| 449 | |
Anand Jain | 54862d6 | 2018-11-11 22:22:16 +0800 | [diff] [blame] | 450 | static int btrfs_dev_replace_start(struct btrfs_fs_info *fs_info, |
David Sterba | da353f6 | 2017-02-14 17:55:53 +0100 | [diff] [blame] | 451 | const char *tgtdev_name, u64 srcdevid, const char *srcdev_name, |
| 452 | int read_src) |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 453 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 454 | struct btrfs_root *root = fs_info->dev_root; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 455 | struct btrfs_trans_handle *trans; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 456 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; |
| 457 | int ret; |
| 458 | struct btrfs_device *tgt_device = NULL; |
| 459 | struct btrfs_device *src_device = NULL; |
| 460 | |
Nikolay Borisov | a27a94c | 2018-09-03 12:46:14 +0300 | [diff] [blame] | 461 | src_device = btrfs_find_device_by_devspec(fs_info, srcdevid, |
| 462 | srcdev_name); |
| 463 | if (IS_ERR(src_device)) |
| 464 | return PTR_ERR(src_device); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 465 | |
Omar Sandoval | eede2bf | 2016-11-03 10:28:12 -0700 | [diff] [blame] | 466 | if (btrfs_pinned_by_swapfile(fs_info, src_device)) { |
| 467 | btrfs_warn_in_rcu(fs_info, |
| 468 | "cannot replace device %s (devid %llu) due to active swapfile", |
| 469 | btrfs_dev_name(src_device), src_device->devid); |
| 470 | return -ETXTBSY; |
| 471 | } |
| 472 | |
Anand Jain | 9e271ae | 2015-08-14 18:33:02 +0800 | [diff] [blame] | 473 | /* |
| 474 | * Here we commit the transaction to make sure commit_total_bytes |
| 475 | * of all the devices are updated. |
| 476 | */ |
| 477 | trans = btrfs_attach_transaction(root); |
| 478 | if (!IS_ERR(trans)) { |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 479 | ret = btrfs_commit_transaction(trans); |
Anand Jain | 9e271ae | 2015-08-14 18:33:02 +0800 | [diff] [blame] | 480 | if (ret) |
| 481 | return ret; |
| 482 | } else if (PTR_ERR(trans) != -ENOENT) { |
| 483 | return PTR_ERR(trans); |
| 484 | } |
| 485 | |
Nikolay Borisov | e1e0eb4 | 2019-05-14 13:54:41 +0300 | [diff] [blame] | 486 | ret = btrfs_init_dev_replace_tgtdev(fs_info, tgtdev_name, |
| 487 | src_device, &tgt_device); |
| 488 | if (ret) |
| 489 | return ret; |
| 490 | |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 491 | down_write(&dev_replace->rwsem); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 492 | switch (dev_replace->replace_state) { |
| 493 | case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED: |
| 494 | case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED: |
| 495 | case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED: |
| 496 | break; |
| 497 | case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED: |
| 498 | case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED: |
Jeff Mahoney | 5c06147 | 2018-09-06 15:52:17 -0400 | [diff] [blame] | 499 | ASSERT(0); |
Anand Jain | b525545 | 2016-03-24 18:48:14 +0800 | [diff] [blame] | 500 | ret = BTRFS_IOCTL_DEV_REPLACE_RESULT_ALREADY_STARTED; |
Nikolay Borisov | fa19452 | 2019-05-14 13:54:42 +0300 | [diff] [blame] | 501 | up_write(&dev_replace->rwsem); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 502 | goto leave; |
| 503 | } |
| 504 | |
Anand Jain | b525545 | 2016-03-24 18:48:14 +0800 | [diff] [blame] | 505 | dev_replace->cont_reading_from_srcdev_mode = read_src; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 506 | dev_replace->srcdev = src_device; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 507 | dev_replace->tgtdev = tgt_device; |
| 508 | |
Anand Jain | fc23c24 | 2016-03-24 18:48:12 +0800 | [diff] [blame] | 509 | btrfs_info_in_rcu(fs_info, |
David Sterba | ecaeb14 | 2015-10-08 09:01:03 +0200 | [diff] [blame] | 510 | "dev_replace from %s (devid %llu) to %s started", |
Anand Jain | 3c958bd | 2017-11-28 10:43:10 +0800 | [diff] [blame] | 511 | btrfs_dev_name(src_device), |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 512 | src_device->devid, |
| 513 | rcu_str_deref(tgt_device->name)); |
| 514 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 515 | /* |
| 516 | * from now on, the writes to the srcdev are all duplicated to |
| 517 | * go to the tgtdev as well (refer to btrfs_map_block()). |
| 518 | */ |
| 519 | dev_replace->replace_state = BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED; |
Allen Pais | a944442 | 2018-06-12 17:18:25 +0530 | [diff] [blame] | 520 | dev_replace->time_started = ktime_get_real_seconds(); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 521 | dev_replace->cursor_left = 0; |
| 522 | dev_replace->committed_cursor_left = 0; |
| 523 | dev_replace->cursor_left_last_write_of_item = 0; |
| 524 | dev_replace->cursor_right = 0; |
| 525 | dev_replace->is_valid = 1; |
| 526 | dev_replace->item_needs_writeback = 1; |
Yauhen Kharuzhy | 7ccefb9 | 2016-03-29 14:17:48 -0700 | [diff] [blame] | 527 | atomic64_set(&dev_replace->num_write_errors, 0); |
| 528 | atomic64_set(&dev_replace->num_uncorrectable_read_errors, 0); |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 529 | up_write(&dev_replace->rwsem); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 530 | |
Anand Jain | cd36da2 | 2020-09-05 01:34:26 +0800 | [diff] [blame] | 531 | ret = btrfs_sysfs_add_device(tgt_device); |
Liu Bo | 73416da | 2015-08-14 18:33:07 +0800 | [diff] [blame] | 532 | if (ret) |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 533 | btrfs_err(fs_info, "kobj add dev failed %d", ret); |
Liu Bo | 73416da | 2015-08-14 18:33:07 +0800 | [diff] [blame] | 534 | |
Chris Mason | 6374e57a | 2017-06-23 09:48:21 -0700 | [diff] [blame] | 535 | btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 536 | |
Nikolay Borisov | f232ab0 | 2019-05-14 13:54:43 +0300 | [diff] [blame] | 537 | /* Commit dev_replace state and reserve 1 item for it. */ |
| 538 | trans = btrfs_start_transaction(root, 1); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 539 | if (IS_ERR(trans)) { |
| 540 | ret = PTR_ERR(trans); |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 541 | down_write(&dev_replace->rwsem); |
Jeff Mahoney | 5c06147 | 2018-09-06 15:52:17 -0400 | [diff] [blame] | 542 | dev_replace->replace_state = |
| 543 | BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED; |
| 544 | dev_replace->srcdev = NULL; |
| 545 | dev_replace->tgtdev = NULL; |
Nikolay Borisov | fa19452 | 2019-05-14 13:54:42 +0300 | [diff] [blame] | 546 | up_write(&dev_replace->rwsem); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 547 | goto leave; |
| 548 | } |
| 549 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 550 | ret = btrfs_commit_transaction(trans); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 551 | WARN_ON(ret); |
| 552 | |
| 553 | /* the disk copy procedure reuses the scrub code */ |
| 554 | ret = btrfs_scrub_dev(fs_info, src_device->devid, 0, |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 555 | btrfs_device_get_total_bytes(src_device), |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 556 | &dev_replace->scrub_progress, 0, 1); |
| 557 | |
Anand Jain | fc23c24 | 2016-03-24 18:48:12 +0800 | [diff] [blame] | 558 | ret = btrfs_dev_replace_finishing(fs_info, ret); |
David Sterba | 4cea903 | 2020-01-25 12:35:38 +0100 | [diff] [blame] | 559 | if (ret == -EINPROGRESS) |
Anand Jain | b525545 | 2016-03-24 18:48:14 +0800 | [diff] [blame] | 560 | ret = BTRFS_IOCTL_DEV_REPLACE_RESULT_SCRUB_INPROGRESS; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 561 | |
Eryu Guan | 2fc9f6b | 2014-10-13 12:42:12 +0800 | [diff] [blame] | 562 | return ret; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 563 | |
| 564 | leave: |
Nikolay Borisov | 4f5ad7b | 2018-07-20 19:37:51 +0300 | [diff] [blame] | 565 | btrfs_destroy_dev_replace_tgtdev(tgt_device); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 566 | return ret; |
| 567 | } |
| 568 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 569 | int btrfs_dev_replace_by_ioctl(struct btrfs_fs_info *fs_info, |
Anand Jain | b525545 | 2016-03-24 18:48:14 +0800 | [diff] [blame] | 570 | struct btrfs_ioctl_dev_replace_args *args) |
| 571 | { |
| 572 | int ret; |
| 573 | |
| 574 | switch (args->start.cont_reading_from_srcdev_mode) { |
| 575 | case BTRFS_IOCTL_DEV_REPLACE_CONT_READING_FROM_SRCDEV_MODE_ALWAYS: |
| 576 | case BTRFS_IOCTL_DEV_REPLACE_CONT_READING_FROM_SRCDEV_MODE_AVOID: |
| 577 | break; |
| 578 | default: |
| 579 | return -EINVAL; |
| 580 | } |
| 581 | |
| 582 | if ((args->start.srcdevid == 0 && args->start.srcdev_name[0] == '\0') || |
| 583 | args->start.tgtdev_name[0] == '\0') |
| 584 | return -EINVAL; |
| 585 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 586 | ret = btrfs_dev_replace_start(fs_info, args->start.tgtdev_name, |
Anand Jain | b525545 | 2016-03-24 18:48:14 +0800 | [diff] [blame] | 587 | args->start.srcdevid, |
| 588 | args->start.srcdev_name, |
| 589 | args->start.cont_reading_from_srcdev_mode); |
| 590 | args->result = ret; |
| 591 | /* don't warn if EINPROGRESS, someone else might be running scrub */ |
Anand Jain | 53e62fb | 2018-11-11 22:22:24 +0800 | [diff] [blame] | 592 | if (ret == BTRFS_IOCTL_DEV_REPLACE_RESULT_SCRUB_INPROGRESS || |
| 593 | ret == BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR) |
| 594 | return 0; |
Anand Jain | b525545 | 2016-03-24 18:48:14 +0800 | [diff] [blame] | 595 | |
| 596 | return ret; |
| 597 | } |
| 598 | |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 599 | /* |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 600 | * blocked until all in-flight bios operations are finished. |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 601 | */ |
| 602 | static void btrfs_rm_dev_replace_blocked(struct btrfs_fs_info *fs_info) |
| 603 | { |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 604 | set_bit(BTRFS_FS_STATE_DEV_REPLACING, &fs_info->fs_state); |
David Sterba | 7f8d236 | 2018-04-05 01:04:49 +0200 | [diff] [blame] | 605 | wait_event(fs_info->dev_replace.replace_wait, !percpu_counter_sum( |
| 606 | &fs_info->dev_replace.bio_counter)); |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | /* |
| 610 | * we have removed target device, it is safe to allow new bios request. |
| 611 | */ |
| 612 | static void btrfs_rm_dev_replace_unblocked(struct btrfs_fs_info *fs_info) |
| 613 | { |
| 614 | clear_bit(BTRFS_FS_STATE_DEV_REPLACING, &fs_info->fs_state); |
David Sterba | 7f8d236 | 2018-04-05 01:04:49 +0200 | [diff] [blame] | 615 | wake_up(&fs_info->dev_replace.replace_wait); |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 616 | } |
| 617 | |
Filipe Manana | 4c8f353 | 2020-09-23 15:30:16 +0100 | [diff] [blame] | 618 | /* |
| 619 | * When finishing the device replace, before swapping the source device with the |
| 620 | * target device we must update the chunk allocation state in the target device, |
| 621 | * as it is empty because replace works by directly copying the chunks and not |
| 622 | * through the normal chunk allocation path. |
| 623 | */ |
| 624 | static int btrfs_set_target_alloc_state(struct btrfs_device *srcdev, |
| 625 | struct btrfs_device *tgtdev) |
| 626 | { |
| 627 | struct extent_state *cached_state = NULL; |
| 628 | u64 start = 0; |
| 629 | u64 found_start; |
| 630 | u64 found_end; |
| 631 | int ret = 0; |
| 632 | |
| 633 | lockdep_assert_held(&srcdev->fs_info->chunk_mutex); |
| 634 | |
| 635 | while (!find_first_extent_bit(&srcdev->alloc_state, start, |
| 636 | &found_start, &found_end, |
| 637 | CHUNK_ALLOCATED, &cached_state)) { |
| 638 | ret = set_extent_bits(&tgtdev->alloc_state, found_start, |
| 639 | found_end, CHUNK_ALLOCATED); |
| 640 | if (ret) |
| 641 | break; |
| 642 | start = found_end + 1; |
| 643 | } |
| 644 | |
| 645 | free_extent_state(cached_state); |
| 646 | return ret; |
| 647 | } |
| 648 | |
Anand Jain | 0725c0c | 2020-09-05 01:34:36 +0800 | [diff] [blame] | 649 | static void btrfs_dev_replace_update_device_in_mapping_tree( |
| 650 | struct btrfs_fs_info *fs_info, |
| 651 | struct btrfs_device *srcdev, |
| 652 | struct btrfs_device *tgtdev) |
| 653 | { |
| 654 | struct extent_map_tree *em_tree = &fs_info->mapping_tree; |
| 655 | struct extent_map *em; |
| 656 | struct map_lookup *map; |
| 657 | u64 start = 0; |
| 658 | int i; |
| 659 | |
| 660 | write_lock(&em_tree->lock); |
| 661 | do { |
| 662 | em = lookup_extent_mapping(em_tree, start, (u64)-1); |
| 663 | if (!em) |
| 664 | break; |
| 665 | map = em->map_lookup; |
| 666 | for (i = 0; i < map->num_stripes; i++) |
| 667 | if (srcdev == map->stripes[i].dev) |
| 668 | map->stripes[i].dev = tgtdev; |
| 669 | start = em->start + em->len; |
| 670 | free_extent_map(em); |
| 671 | } while (start); |
| 672 | write_unlock(&em_tree->lock); |
| 673 | } |
| 674 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 675 | static int btrfs_dev_replace_finishing(struct btrfs_fs_info *fs_info, |
| 676 | int scrub_ret) |
| 677 | { |
| 678 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; |
| 679 | struct btrfs_device *tgt_device; |
| 680 | struct btrfs_device *src_device; |
| 681 | struct btrfs_root *root = fs_info->tree_root; |
| 682 | u8 uuid_tmp[BTRFS_UUID_SIZE]; |
| 683 | struct btrfs_trans_handle *trans; |
| 684 | int ret = 0; |
| 685 | |
| 686 | /* don't allow cancel or unmount to disturb the finishing procedure */ |
| 687 | mutex_lock(&dev_replace->lock_finishing_cancel_unmount); |
| 688 | |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 689 | down_read(&dev_replace->rwsem); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 690 | /* was the operation canceled, or is it finished? */ |
| 691 | if (dev_replace->replace_state != |
| 692 | BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED) { |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 693 | up_read(&dev_replace->rwsem); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 694 | mutex_unlock(&dev_replace->lock_finishing_cancel_unmount); |
| 695 | return 0; |
| 696 | } |
| 697 | |
| 698 | tgt_device = dev_replace->tgtdev; |
| 699 | src_device = dev_replace->srcdev; |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 700 | up_read(&dev_replace->rwsem); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 701 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 702 | /* |
| 703 | * flush all outstanding I/O and inode extent mappings before the |
| 704 | * copy operation is declared as being finished |
| 705 | */ |
Josef Bacik | b491213 | 2020-07-21 10:22:12 -0400 | [diff] [blame] | 706 | ret = btrfs_start_delalloc_roots(fs_info, U64_MAX); |
Miao Xie | 3edb2a6 | 2013-01-22 10:49:33 +0000 | [diff] [blame] | 707 | if (ret) { |
| 708 | mutex_unlock(&dev_replace->lock_finishing_cancel_unmount); |
| 709 | return ret; |
| 710 | } |
Chris Mason | 6374e57a | 2017-06-23 09:48:21 -0700 | [diff] [blame] | 711 | btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 712 | |
Filipe Manana | 66d204a | 2020-10-12 11:55:24 +0100 | [diff] [blame] | 713 | if (!scrub_ret) |
| 714 | btrfs_reada_remove_dev(src_device); |
| 715 | |
Nikolay Borisov | debd1c0 | 2019-05-17 10:44:25 +0300 | [diff] [blame] | 716 | /* |
| 717 | * We have to use this loop approach because at this point src_device |
| 718 | * has to be available for transaction commit to complete, yet new |
| 719 | * chunks shouldn't be allocated on the device. |
| 720 | */ |
| 721 | while (1) { |
| 722 | trans = btrfs_start_transaction(root, 0); |
| 723 | if (IS_ERR(trans)) { |
Filipe Manana | 66d204a | 2020-10-12 11:55:24 +0100 | [diff] [blame] | 724 | btrfs_reada_undo_remove_dev(src_device); |
Nikolay Borisov | debd1c0 | 2019-05-17 10:44:25 +0300 | [diff] [blame] | 725 | mutex_unlock(&dev_replace->lock_finishing_cancel_unmount); |
| 726 | return PTR_ERR(trans); |
| 727 | } |
| 728 | ret = btrfs_commit_transaction(trans); |
| 729 | WARN_ON(ret); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 730 | |
Nikolay Borisov | debd1c0 | 2019-05-17 10:44:25 +0300 | [diff] [blame] | 731 | /* Prevent write_all_supers() during the finishing procedure */ |
| 732 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
| 733 | /* Prevent new chunks being allocated on the source device */ |
| 734 | mutex_lock(&fs_info->chunk_mutex); |
| 735 | |
| 736 | if (!list_empty(&src_device->post_commit_list)) { |
| 737 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
| 738 | mutex_unlock(&fs_info->chunk_mutex); |
| 739 | } else { |
| 740 | break; |
| 741 | } |
| 742 | } |
| 743 | |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 744 | down_write(&dev_replace->rwsem); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 745 | dev_replace->replace_state = |
| 746 | scrub_ret ? BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED |
| 747 | : BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED; |
| 748 | dev_replace->tgtdev = NULL; |
| 749 | dev_replace->srcdev = NULL; |
Allen Pais | a944442 | 2018-06-12 17:18:25 +0530 | [diff] [blame] | 750 | dev_replace->time_stopped = ktime_get_real_seconds(); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 751 | dev_replace->item_needs_writeback = 1; |
| 752 | |
Filipe Manana | 4c8f353 | 2020-09-23 15:30:16 +0100 | [diff] [blame] | 753 | /* |
| 754 | * Update allocation state in the new device and replace the old device |
| 755 | * with the new one in the mapping tree. |
| 756 | */ |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 757 | if (!scrub_ret) { |
Filipe Manana | 4c8f353 | 2020-09-23 15:30:16 +0100 | [diff] [blame] | 758 | scrub_ret = btrfs_set_target_alloc_state(src_device, tgt_device); |
| 759 | if (scrub_ret) |
| 760 | goto error; |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 761 | btrfs_dev_replace_update_device_in_mapping_tree(fs_info, |
| 762 | src_device, |
| 763 | tgt_device); |
| 764 | } else { |
Anand Jain | f9085ab | 2018-11-20 19:56:16 +0800 | [diff] [blame] | 765 | if (scrub_ret != -ECANCELED) |
| 766 | btrfs_err_in_rcu(fs_info, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 767 | "btrfs_scrub_dev(%s, %llu, %s) failed %d", |
Anand Jain | 3c958bd | 2017-11-28 10:43:10 +0800 | [diff] [blame] | 768 | btrfs_dev_name(src_device), |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 769 | src_device->devid, |
| 770 | rcu_str_deref(tgt_device->name), scrub_ret); |
Filipe Manana | 4c8f353 | 2020-09-23 15:30:16 +0100 | [diff] [blame] | 771 | error: |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 772 | up_write(&dev_replace->rwsem); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 773 | mutex_unlock(&fs_info->chunk_mutex); |
| 774 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
Filipe Manana | 66d204a | 2020-10-12 11:55:24 +0100 | [diff] [blame] | 775 | btrfs_reada_undo_remove_dev(src_device); |
Qu Wenruo | ae6529c | 2017-03-29 09:33:21 +0800 | [diff] [blame] | 776 | btrfs_rm_dev_replace_blocked(fs_info); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 777 | if (tgt_device) |
Nikolay Borisov | 4f5ad7b | 2018-07-20 19:37:51 +0300 | [diff] [blame] | 778 | btrfs_destroy_dev_replace_tgtdev(tgt_device); |
Qu Wenruo | ae6529c | 2017-03-29 09:33:21 +0800 | [diff] [blame] | 779 | btrfs_rm_dev_replace_unblocked(fs_info); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 780 | mutex_unlock(&dev_replace->lock_finishing_cancel_unmount); |
| 781 | |
Eryu Guan | 2fc9f6b | 2014-10-13 12:42:12 +0800 | [diff] [blame] | 782 | return scrub_ret; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 783 | } |
| 784 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 785 | btrfs_info_in_rcu(fs_info, |
| 786 | "dev_replace from %s (devid %llu) to %s finished", |
Anand Jain | 3c958bd | 2017-11-28 10:43:10 +0800 | [diff] [blame] | 787 | btrfs_dev_name(src_device), |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 788 | src_device->devid, |
| 789 | rcu_str_deref(tgt_device->name)); |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 790 | clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &tgt_device->dev_state); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 791 | tgt_device->devid = src_device->devid; |
| 792 | src_device->devid = BTRFS_DEV_REPLACE_DEVID; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 793 | memcpy(uuid_tmp, tgt_device->uuid, sizeof(uuid_tmp)); |
| 794 | memcpy(tgt_device->uuid, src_device->uuid, sizeof(tgt_device->uuid)); |
| 795 | memcpy(src_device->uuid, uuid_tmp, sizeof(src_device->uuid)); |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 796 | btrfs_device_set_total_bytes(tgt_device, src_device->total_bytes); |
| 797 | btrfs_device_set_disk_total_bytes(tgt_device, |
| 798 | src_device->disk_total_bytes); |
| 799 | btrfs_device_set_bytes_used(tgt_device, src_device->bytes_used); |
Miao Xie | ce7213c | 2014-09-03 21:35:34 +0800 | [diff] [blame] | 800 | tgt_device->commit_bytes_used = src_device->bytes_used; |
Anand Jain | 88acff6 | 2016-05-03 17:44:43 +0800 | [diff] [blame] | 801 | |
Nikolay Borisov | d6507cf | 2018-07-20 19:37:50 +0300 | [diff] [blame] | 802 | btrfs_assign_next_active_device(src_device, tgt_device); |
Anand Jain | 88acff6 | 2016-05-03 17:44:43 +0800 | [diff] [blame] | 803 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 804 | 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] | 805 | fs_info->fs_devices->rw_devices++; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 806 | |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 807 | up_write(&dev_replace->rwsem); |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 808 | btrfs_rm_dev_replace_blocked(fs_info); |
| 809 | |
Nikolay Borisov | 68a9db5 | 2018-07-20 19:37:48 +0300 | [diff] [blame] | 810 | btrfs_rm_dev_replace_remove_srcdev(src_device); |
Ilya Dryomov | 1357272 | 2013-10-02 20:41:01 +0300 | [diff] [blame] | 811 | |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 812 | btrfs_rm_dev_replace_unblocked(fs_info); |
| 813 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 814 | /* |
Misono Tomohiro | 1e7e1f9 | 2018-07-31 16:20:21 +0900 | [diff] [blame] | 815 | * Increment dev_stats_ccnt so that btrfs_run_dev_stats() will |
| 816 | * update on-disk dev stats value during commit transaction |
| 817 | */ |
| 818 | atomic_inc(&tgt_device->dev_stats_ccnt); |
| 819 | |
| 820 | /* |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 821 | * this is again a consistent state where no dev_replace procedure |
| 822 | * is running, the target device is part of the filesystem, the |
| 823 | * source device is not part of the filesystem anymore and its 1st |
| 824 | * superblock is scratched out so that it is no longer marked to |
| 825 | * belong to this filesystem. |
| 826 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 827 | mutex_unlock(&fs_info->chunk_mutex); |
| 828 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 829 | |
Qu Wenruo | 084b6e7c | 2014-10-30 16:52:31 +0800 | [diff] [blame] | 830 | /* replace the sysfs entry */ |
Anand Jain | 53f8a74 | 2020-09-05 01:34:27 +0800 | [diff] [blame] | 831 | btrfs_sysfs_remove_device(src_device); |
Anand Jain | 668e48af | 2020-01-06 19:38:31 +0800 | [diff] [blame] | 832 | btrfs_sysfs_update_devid(tgt_device); |
Josef Bacik | 313b085 | 2020-08-20 11:18:26 -0400 | [diff] [blame] | 833 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &src_device->dev_state)) |
| 834 | btrfs_scratch_superblocks(fs_info, src_device->bdev, |
| 835 | src_device->name->str); |
Qu Wenruo | 084b6e7c | 2014-10-30 16:52:31 +0800 | [diff] [blame] | 836 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 837 | /* write back the superblocks */ |
| 838 | trans = btrfs_start_transaction(root, 0); |
| 839 | if (!IS_ERR(trans)) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 840 | btrfs_commit_transaction(trans); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 841 | |
| 842 | mutex_unlock(&dev_replace->lock_finishing_cancel_unmount); |
| 843 | |
Josef Bacik | a466c85 | 2020-08-20 11:18:27 -0400 | [diff] [blame] | 844 | btrfs_rm_dev_replace_free_srcdev(src_device); |
| 845 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 846 | return 0; |
| 847 | } |
| 848 | |
David Sterba | 74b595f | 2017-06-14 16:24:56 +0200 | [diff] [blame] | 849 | /* |
| 850 | * Read progress of device replace status according to the state and last |
| 851 | * stored position. The value format is the same as for |
| 852 | * btrfs_dev_replace::progress_1000 |
| 853 | */ |
| 854 | static u64 btrfs_dev_replace_progress(struct btrfs_fs_info *fs_info) |
| 855 | { |
| 856 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; |
| 857 | u64 ret = 0; |
| 858 | |
| 859 | switch (dev_replace->replace_state) { |
| 860 | case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED: |
| 861 | case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED: |
| 862 | ret = 0; |
| 863 | break; |
| 864 | case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED: |
| 865 | ret = 1000; |
| 866 | break; |
| 867 | case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED: |
| 868 | case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED: |
| 869 | ret = div64_u64(dev_replace->cursor_left, |
| 870 | div_u64(btrfs_device_get_total_bytes( |
| 871 | dev_replace->srcdev), 1000)); |
| 872 | break; |
| 873 | } |
| 874 | |
| 875 | return ret; |
| 876 | } |
| 877 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 878 | void btrfs_dev_replace_status(struct btrfs_fs_info *fs_info, |
| 879 | struct btrfs_ioctl_dev_replace_args *args) |
| 880 | { |
| 881 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; |
| 882 | |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 883 | down_read(&dev_replace->rwsem); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 884 | /* even if !dev_replace_is_valid, the values are good enough for |
| 885 | * the replace_status ioctl */ |
| 886 | args->result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR; |
| 887 | args->status.replace_state = dev_replace->replace_state; |
| 888 | args->status.time_started = dev_replace->time_started; |
| 889 | args->status.time_stopped = dev_replace->time_stopped; |
| 890 | args->status.num_write_errors = |
| 891 | atomic64_read(&dev_replace->num_write_errors); |
| 892 | args->status.num_uncorrectable_read_errors = |
| 893 | atomic64_read(&dev_replace->num_uncorrectable_read_errors); |
David Sterba | 74b595f | 2017-06-14 16:24:56 +0200 | [diff] [blame] | 894 | args->status.progress_1000 = btrfs_dev_replace_progress(fs_info); |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 895 | up_read(&dev_replace->rwsem); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 896 | } |
| 897 | |
Anand Jain | 18e67c7 | 2018-02-12 23:33:31 +0800 | [diff] [blame] | 898 | int btrfs_dev_replace_cancel(struct btrfs_fs_info *fs_info) |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 899 | { |
| 900 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; |
| 901 | struct btrfs_device *tgt_device = NULL; |
Anand Jain | 8f2ceaa | 2018-02-13 11:53:43 +0800 | [diff] [blame] | 902 | struct btrfs_device *src_device = NULL; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 903 | struct btrfs_trans_handle *trans; |
| 904 | struct btrfs_root *root = fs_info->tree_root; |
Anand Jain | 18e67c7 | 2018-02-12 23:33:31 +0800 | [diff] [blame] | 905 | int result; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 906 | int ret; |
| 907 | |
David Howells | bc98a42 | 2017-07-17 08:45:34 +0100 | [diff] [blame] | 908 | if (sb_rdonly(fs_info->sb)) |
Ilya Dryomov | e649e58 | 2013-10-10 20:40:21 +0300 | [diff] [blame] | 909 | return -EROFS; |
| 910 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 911 | mutex_lock(&dev_replace->lock_finishing_cancel_unmount); |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 912 | down_write(&dev_replace->rwsem); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 913 | switch (dev_replace->replace_state) { |
| 914 | case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED: |
| 915 | case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED: |
| 916 | case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED: |
| 917 | result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NOT_STARTED; |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 918 | up_write(&dev_replace->rwsem); |
Anand Jain | d189dd7 | 2018-11-14 13:50:26 +0800 | [diff] [blame] | 919 | break; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 920 | case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED: |
Anand Jain | d189dd7 | 2018-11-14 13:50:26 +0800 | [diff] [blame] | 921 | tgt_device = dev_replace->tgtdev; |
| 922 | src_device = dev_replace->srcdev; |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 923 | up_write(&dev_replace->rwsem); |
Anand Jain | b47dda2 | 2018-11-11 22:22:20 +0800 | [diff] [blame] | 924 | ret = btrfs_scrub_cancel(fs_info); |
| 925 | if (ret < 0) { |
| 926 | result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NOT_STARTED; |
| 927 | } else { |
| 928 | result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR; |
| 929 | /* |
| 930 | * btrfs_dev_replace_finishing() will handle the |
| 931 | * cleanup part |
| 932 | */ |
| 933 | btrfs_info_in_rcu(fs_info, |
| 934 | "dev_replace from %s (devid %llu) to %s canceled", |
| 935 | btrfs_dev_name(src_device), src_device->devid, |
| 936 | btrfs_dev_name(tgt_device)); |
| 937 | } |
Anand Jain | d189dd7 | 2018-11-14 13:50:26 +0800 | [diff] [blame] | 938 | break; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 939 | case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED: |
Anand Jain | d189dd7 | 2018-11-14 13:50:26 +0800 | [diff] [blame] | 940 | /* |
| 941 | * Scrub doing the replace isn't running so we need to do the |
| 942 | * cleanup step of btrfs_dev_replace_finishing() here |
| 943 | */ |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 944 | result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR; |
| 945 | tgt_device = dev_replace->tgtdev; |
Anand Jain | 8f2ceaa | 2018-02-13 11:53:43 +0800 | [diff] [blame] | 946 | src_device = dev_replace->srcdev; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 947 | dev_replace->tgtdev = NULL; |
| 948 | dev_replace->srcdev = NULL; |
Anand Jain | d189dd7 | 2018-11-14 13:50:26 +0800 | [diff] [blame] | 949 | dev_replace->replace_state = |
| 950 | BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED; |
| 951 | dev_replace->time_stopped = ktime_get_real_seconds(); |
| 952 | dev_replace->item_needs_writeback = 1; |
| 953 | |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 954 | up_write(&dev_replace->rwsem); |
Anand Jain | d189dd7 | 2018-11-14 13:50:26 +0800 | [diff] [blame] | 955 | |
Anand Jain | fe97e2e | 2018-11-11 22:22:21 +0800 | [diff] [blame] | 956 | /* Scrub for replace must not be running in suspended state */ |
| 957 | ret = btrfs_scrub_cancel(fs_info); |
| 958 | ASSERT(ret != -ENOTCONN); |
Anand Jain | d189dd7 | 2018-11-14 13:50:26 +0800 | [diff] [blame] | 959 | |
| 960 | trans = btrfs_start_transaction(root, 0); |
| 961 | if (IS_ERR(trans)) { |
| 962 | mutex_unlock(&dev_replace->lock_finishing_cancel_unmount); |
| 963 | return PTR_ERR(trans); |
| 964 | } |
| 965 | ret = btrfs_commit_transaction(trans); |
| 966 | WARN_ON(ret); |
| 967 | |
| 968 | btrfs_info_in_rcu(fs_info, |
| 969 | "suspended dev_replace from %s (devid %llu) to %s canceled", |
| 970 | btrfs_dev_name(src_device), src_device->devid, |
| 971 | btrfs_dev_name(tgt_device)); |
| 972 | |
| 973 | if (tgt_device) |
| 974 | btrfs_destroy_dev_replace_tgtdev(tgt_device); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 975 | break; |
Anand Jain | d189dd7 | 2018-11-14 13:50:26 +0800 | [diff] [blame] | 976 | default: |
Dan Carpenter | 669e859 | 2019-02-11 21:32:10 +0300 | [diff] [blame] | 977 | up_write(&dev_replace->rwsem); |
Anand Jain | d189dd7 | 2018-11-14 13:50:26 +0800 | [diff] [blame] | 978 | result = -EINVAL; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 979 | } |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 980 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 981 | mutex_unlock(&dev_replace->lock_finishing_cancel_unmount); |
| 982 | return result; |
| 983 | } |
| 984 | |
| 985 | void btrfs_dev_replace_suspend_for_unmount(struct btrfs_fs_info *fs_info) |
| 986 | { |
| 987 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; |
| 988 | |
| 989 | mutex_lock(&dev_replace->lock_finishing_cancel_unmount); |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 990 | down_write(&dev_replace->rwsem); |
| 991 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 992 | switch (dev_replace->replace_state) { |
| 993 | case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED: |
| 994 | case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED: |
| 995 | case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED: |
| 996 | case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED: |
| 997 | break; |
| 998 | case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED: |
| 999 | dev_replace->replace_state = |
| 1000 | BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED; |
Allen Pais | a944442 | 2018-06-12 17:18:25 +0530 | [diff] [blame] | 1001 | dev_replace->time_stopped = ktime_get_real_seconds(); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1002 | dev_replace->item_needs_writeback = 1; |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 1003 | btrfs_info(fs_info, "suspending dev_replace for unmount"); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1004 | break; |
| 1005 | } |
| 1006 | |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 1007 | up_write(&dev_replace->rwsem); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1008 | mutex_unlock(&dev_replace->lock_finishing_cancel_unmount); |
| 1009 | } |
| 1010 | |
| 1011 | /* resume dev_replace procedure that was interrupted by unmount */ |
| 1012 | int btrfs_resume_dev_replace_async(struct btrfs_fs_info *fs_info) |
| 1013 | { |
| 1014 | struct task_struct *task; |
| 1015 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; |
| 1016 | |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 1017 | down_write(&dev_replace->rwsem); |
| 1018 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1019 | switch (dev_replace->replace_state) { |
| 1020 | case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED: |
| 1021 | case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED: |
| 1022 | case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED: |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 1023 | up_write(&dev_replace->rwsem); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1024 | return 0; |
| 1025 | case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED: |
| 1026 | break; |
| 1027 | case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED: |
| 1028 | dev_replace->replace_state = |
| 1029 | BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED; |
| 1030 | break; |
| 1031 | } |
| 1032 | if (!dev_replace->tgtdev || !dev_replace->tgtdev->bdev) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 1033 | btrfs_info(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 1034 | "cannot continue dev_replace, tgtdev is missing"); |
| 1035 | btrfs_info(fs_info, |
| 1036 | "you may cancel the operation after 'mount -o degraded'"); |
Anand Jain | 0d228ec | 2018-11-11 22:22:17 +0800 | [diff] [blame] | 1037 | dev_replace->replace_state = |
| 1038 | BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED; |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 1039 | up_write(&dev_replace->rwsem); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1040 | return 0; |
| 1041 | } |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 1042 | up_write(&dev_replace->rwsem); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1043 | |
David Sterba | 010a47b | 2018-03-20 19:51:04 +0100 | [diff] [blame] | 1044 | /* |
| 1045 | * This could collide with a paused balance, but the exclusive op logic |
| 1046 | * should never allow both to start and pause. We don't want to allow |
| 1047 | * dev-replace to start anyway. |
| 1048 | */ |
Goldwyn Rodrigues | c3e1f96 | 2020-08-25 10:02:32 -0500 | [diff] [blame] | 1049 | if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_DEV_REPLACE)) { |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 1050 | down_write(&dev_replace->rwsem); |
Anand Jain | 05c49e6 | 2018-11-11 22:22:18 +0800 | [diff] [blame] | 1051 | dev_replace->replace_state = |
| 1052 | BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED; |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 1053 | up_write(&dev_replace->rwsem); |
David Sterba | 010a47b | 2018-03-20 19:51:04 +0100 | [diff] [blame] | 1054 | btrfs_info(fs_info, |
| 1055 | "cannot resume dev-replace, other exclusive operation running"); |
| 1056 | return 0; |
| 1057 | } |
| 1058 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1059 | task = kthread_run(btrfs_dev_replace_kthread, fs_info, "btrfs-devrepl"); |
Rusty Russell | 8c6ffba | 2013-07-15 11:20:32 +0930 | [diff] [blame] | 1060 | return PTR_ERR_OR_ZERO(task); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1061 | } |
| 1062 | |
| 1063 | static int btrfs_dev_replace_kthread(void *data) |
| 1064 | { |
| 1065 | struct btrfs_fs_info *fs_info = data; |
| 1066 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1067 | u64 progress; |
David Sterba | 00251a5 | 2018-03-20 15:35:50 +0100 | [diff] [blame] | 1068 | int ret; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1069 | |
David Sterba | f1b8a1e | 2017-06-14 16:28:42 +0200 | [diff] [blame] | 1070 | progress = btrfs_dev_replace_progress(fs_info); |
| 1071 | progress = div_u64(progress, 10); |
| 1072 | btrfs_info_in_rcu(fs_info, |
Anand Jain | 3c958bd | 2017-11-28 10:43:10 +0800 | [diff] [blame] | 1073 | "continuing dev_replace from %s (devid %llu) to target %s @%u%%", |
| 1074 | btrfs_dev_name(dev_replace->srcdev), |
David Sterba | f1b8a1e | 2017-06-14 16:28:42 +0200 | [diff] [blame] | 1075 | dev_replace->srcdev->devid, |
Anand Jain | 3c958bd | 2017-11-28 10:43:10 +0800 | [diff] [blame] | 1076 | btrfs_dev_name(dev_replace->tgtdev), |
David Sterba | f1b8a1e | 2017-06-14 16:28:42 +0200 | [diff] [blame] | 1077 | (unsigned int)progress); |
| 1078 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1079 | ret = btrfs_scrub_dev(fs_info, dev_replace->srcdev->devid, |
| 1080 | dev_replace->committed_cursor_left, |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 1081 | btrfs_device_get_total_bytes(dev_replace->srcdev), |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1082 | &dev_replace->scrub_progress, 0, 1); |
| 1083 | ret = btrfs_dev_replace_finishing(fs_info, ret); |
Anand Jain | 49365e6 | 2018-11-20 19:56:15 +0800 | [diff] [blame] | 1084 | WARN_ON(ret && ret != -ECANCELED); |
David Sterba | 00251a5 | 2018-03-20 15:35:50 +0100 | [diff] [blame] | 1085 | |
Goldwyn Rodrigues | c3e1f96 | 2020-08-25 10:02:32 -0500 | [diff] [blame] | 1086 | btrfs_exclop_finish(fs_info); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1087 | return 0; |
| 1088 | } |
| 1089 | |
David Sterba | e1f60a6 | 2019-10-01 19:57:39 +0200 | [diff] [blame] | 1090 | int __pure btrfs_dev_replace_is_ongoing(struct btrfs_dev_replace *dev_replace) |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1091 | { |
| 1092 | if (!dev_replace->is_valid) |
| 1093 | return 0; |
| 1094 | |
| 1095 | switch (dev_replace->replace_state) { |
| 1096 | case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED: |
| 1097 | case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED: |
| 1098 | case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED: |
| 1099 | return 0; |
| 1100 | case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED: |
| 1101 | case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED: |
| 1102 | /* |
| 1103 | * return true even if tgtdev is missing (this is |
| 1104 | * something that can happen if the dev_replace |
| 1105 | * procedure is suspended by an umount and then |
| 1106 | * the tgtdev is missing (or "btrfs dev scan") was |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 1107 | * not called and the filesystem is remounted |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1108 | * in degraded state. This does not stop the |
| 1109 | * dev_replace procedure. It needs to be canceled |
Adam Buchbinder | bb7ab3b | 2016-03-04 11:23:12 -0800 | [diff] [blame] | 1110 | * manually if the cancellation is wanted. |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1111 | */ |
| 1112 | break; |
| 1113 | } |
| 1114 | return 1; |
| 1115 | } |
| 1116 | |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 1117 | void btrfs_bio_counter_inc_noblocked(struct btrfs_fs_info *fs_info) |
| 1118 | { |
David Sterba | 7f8d236 | 2018-04-05 01:04:49 +0200 | [diff] [blame] | 1119 | percpu_counter_inc(&fs_info->dev_replace.bio_counter); |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 1120 | } |
| 1121 | |
Miao Xie | 4245215 | 2014-11-25 16:39:28 +0800 | [diff] [blame] | 1122 | 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] | 1123 | { |
David Sterba | 7f8d236 | 2018-04-05 01:04:49 +0200 | [diff] [blame] | 1124 | percpu_counter_sub(&fs_info->dev_replace.bio_counter, amount); |
| 1125 | cond_wake_up_nomb(&fs_info->dev_replace.replace_wait); |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 1126 | } |
| 1127 | |
| 1128 | void btrfs_bio_counter_inc_blocked(struct btrfs_fs_info *fs_info) |
| 1129 | { |
Zhao Lei | 09dd7a0 | 2015-01-20 15:11:37 +0800 | [diff] [blame] | 1130 | while (1) { |
David Sterba | 7f8d236 | 2018-04-05 01:04:49 +0200 | [diff] [blame] | 1131 | percpu_counter_inc(&fs_info->dev_replace.bio_counter); |
Zhao Lei | 09dd7a0 | 2015-01-20 15:11:37 +0800 | [diff] [blame] | 1132 | if (likely(!test_bit(BTRFS_FS_STATE_DEV_REPLACING, |
| 1133 | &fs_info->fs_state))) |
| 1134 | break; |
| 1135 | |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 1136 | btrfs_bio_counter_dec(fs_info); |
David Sterba | 7f8d236 | 2018-04-05 01:04:49 +0200 | [diff] [blame] | 1137 | wait_event(fs_info->dev_replace.replace_wait, |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 1138 | !test_bit(BTRFS_FS_STATE_DEV_REPLACING, |
| 1139 | &fs_info->fs_state)); |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 1140 | } |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 1141 | } |