| 1 | |
| 2 | |
| 3 | * Copyright (C) STRATO AG 2012. All rights reserved.
|
| 4 |
|
| 5 |
|
| 6 | #include <linux/sched.h>
|
| 7 | #include <linux/bio.h>
|
| 8 | #include <linux/slab.h>
|
| 9 | #include <linux/blkdev.h>
|
| 10 | #include <linux/kthread.h>
|
| 11 | #include <linux/math64.h>
|
| 12 | #include "misc.h"
|
| 13 | #include "ctree.h"
|
| 14 | #include "disk-io.h"
|
| 15 | #include "transaction.h"
|
| 16 | #include "volumes.h"
|
| 17 | #include "async-thread.h"
|
| 18 | #include "dev-replace.h"
|
| 19 | #include "sysfs.h"
|
| 20 | #include "zoned.h"
|
| 21 | #include "block-group.h"
|
| 22 | #include "fs.h"
|
| 23 | #include "accessors.h"
|
| 24 | #include "scrub.h"
|
| 25 |
|
| 26 | |
| 27 | * Device replace overview
|
| 28 | *
|
| 29 | * [Objective]
|
| 30 | * To copy all extents (both new and on-disk) from source device to target
|
| 31 | * device, while still keeping the filesystem read-write.
|
| 32 | *
|
| 33 | * [Method]
|
| 34 | * There are two main methods involved:
|
| 35 | *
|
| 36 | * - Write duplication
|
| 37 | *
|
| 38 | * All new writes will be written to both target and source devices, so even
|
| 39 | * if replace gets canceled, sources device still contains up-to-date data.
|
| 40 | *
|
| 41 | * Location: handle_ops_on_dev_replace() from btrfs_map_block()
|
| 42 | * Start: btrfs_dev_replace_start()
|
| 43 | * End: btrfs_dev_replace_finishing()
|
| 44 | * Content: Latest data/metadata
|
| 45 | *
|
| 46 | * - Copy existing extents
|
| 47 | *
|
| 48 | * This happens by reusing scrub facility, as scrub also iterates through
|
| 49 | * existing extents from commit root.
|
| 50 | *
|
| 51 | * Location: scrub_write_block_to_dev_replace() from
|
| 52 | * scrub_block_complete()
|
| 53 | * Content: Data/meta from commit root.
|
| 54 | *
|
| 55 | * Due to the content difference, we need to avoid nocow write when dev-replace
|
| 56 | * is happening. This is done by marking the block group read-only and waiting
|
| 57 | * for NOCOW writes.
|
| 58 | *
|
| 59 | * After replace is done, the finishing part is done by swapping the target and
|
| 60 | * source devices.
|
| 61 | *
|
| 62 | * Location: btrfs_dev_replace_update_device_in_mapping_tree() from
|
| 63 | * btrfs_dev_replace_finishing()
|
| 64 |
|
| 65 |
|
| 66 | static int btrfs_dev_replace_finishing(struct btrfs_fs_info *fs_info,
|
| 67 | int scrub_ret);
|
| 68 | static int btrfs_dev_replace_kthread(void *data);
|
| 69 |
|
| 70 | int btrfs_init_dev_replace(struct btrfs_fs_info *fs_info)
|
| 71 | {
|
| 72 | struct btrfs_dev_lookup_args args = { .devid = BTRFS_DEV_REPLACE_DEVID };
|
| 73 | struct btrfs_key key;
|
| 74 | struct btrfs_root *dev_root = fs_info->dev_root;
|
| 75 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
|
| 76 | struct extent_buffer *eb;
|
| 77 | int slot;
|
| 78 | int ret = 0;
|
| 79 | BTRFS_PATH_AUTO_FREE(path);
|
| 80 | int item_size;
|
| 81 | struct btrfs_dev_replace_item *ptr;
|
| 82 | u64 src_devid;
|
| 83 |
|
| 84 | if (!dev_root)
|
| 85 | return 0;
|
| 86 |
|
| 87 | path = btrfs_alloc_path();
|
| 88 | if (!path)
|
| 89 | return -ENOMEM;
|
| 90 |
|
| 91 | key.objectid = 0;
|
| 92 | key.type = BTRFS_DEV_REPLACE_KEY;
|
| 93 | key.offset = 0;
|
| 94 | ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
|
| 95 | if (ret) {
|
| 96 | no_valid_dev_replace_entry_found:
|
| 97 | |
| 98 | * We don't have a replace item or it's corrupted. If there is
|
| 99 | * a replace target, fail the mount.
|
| 100 |
|
| 101 | if (unlikely(btrfs_find_device(fs_info->fs_devices, &args))) {
|
| 102 | btrfs_err(fs_info,
|
| 103 | "found replace target device without a valid replace item");
|
| 104 | return -EUCLEAN;
|
| 105 | }
|
| 106 | dev_replace->replace_state =
|
| 107 | BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED;
|
| 108 | dev_replace->cont_reading_from_srcdev_mode =
|
| 109 | BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_ALWAYS;
|
| 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 | return 0;
|
| 123 | }
|
| 124 | slot = path->slots[0];
|
| 125 | eb = path->nodes[0];
|
| 126 | item_size = btrfs_item_size(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)) {
|
| 130 | btrfs_warn(fs_info,
|
| 131 | "dev_replace entry found has unexpected size, ignore entry");
|
| 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:
|
| 157 | |
| 158 | * We don't have an active replace item but if there is a
|
| 159 | * replace target, fail the mount.
|
| 160 |
|
| 161 | if (unlikely(btrfs_find_device(fs_info->fs_devices, &args))) {
|
| 162 | btrfs_err(fs_info,
|
| 163 | "replace without active item, run 'device scan --forget' on the target device");
|
| 164 | ret = -EUCLEAN;
|
| 165 | } else {
|
| 166 | dev_replace->srcdev = NULL;
|
| 167 | dev_replace->tgtdev = NULL;
|
| 168 | }
|
| 169 | break;
|
| 170 | case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
|
| 171 | case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
|
| 172 | dev_replace->tgtdev = btrfs_find_device(fs_info->fs_devices, &args);
|
| 173 | args.devid = src_devid;
|
| 174 | dev_replace->srcdev = btrfs_find_device(fs_info->fs_devices, &args);
|
| 175 |
|
| 176 | |
| 177 | * allow 'btrfs dev replace_cancel' if src/tgt device is
|
| 178 | * missing
|
| 179 |
|
| 180 | if (unlikely(!dev_replace->srcdev && !btrfs_test_opt(fs_info, DEGRADED))) {
|
| 181 | ret = -EIO;
|
| 182 | btrfs_warn(fs_info,
|
| 183 | "cannot mount because device replace operation is ongoing and");
|
| 184 | btrfs_warn(fs_info,
|
| 185 | "srcdev (devid %llu) is missing, need to run 'btrfs dev scan'?",
|
| 186 | src_devid);
|
| 187 | }
|
| 188 | if (unlikely(!dev_replace->tgtdev && !btrfs_test_opt(fs_info, DEGRADED))) {
|
| 189 | ret = -EIO;
|
| 190 | btrfs_warn(fs_info,
|
| 191 | "cannot mount because device replace operation is ongoing and");
|
| 192 | btrfs_warn(fs_info,
|
| 193 | "tgtdev (devid %llu) is missing, need to run 'btrfs dev scan'?",
|
| 194 | BTRFS_DEV_REPLACE_DEVID);
|
| 195 | }
|
| 196 | if (dev_replace->tgtdev) {
|
| 197 | if (dev_replace->srcdev) {
|
| 198 | dev_replace->tgtdev->total_bytes =
|
| 199 | dev_replace->srcdev->total_bytes;
|
| 200 | dev_replace->tgtdev->disk_total_bytes =
|
| 201 | dev_replace->srcdev->disk_total_bytes;
|
| 202 | dev_replace->tgtdev->commit_total_bytes =
|
| 203 | dev_replace->srcdev->commit_total_bytes;
|
| 204 | dev_replace->tgtdev->bytes_used =
|
| 205 | dev_replace->srcdev->bytes_used;
|
| 206 | dev_replace->tgtdev->commit_bytes_used =
|
| 207 | dev_replace->srcdev->commit_bytes_used;
|
| 208 | }
|
| 209 | set_bit(BTRFS_DEV_STATE_REPLACE_TGT,
|
| 210 | &dev_replace->tgtdev->dev_state);
|
| 211 |
|
| 212 | WARN_ON(fs_info->fs_devices->rw_devices == 0);
|
| 213 | dev_replace->tgtdev->io_width = fs_info->sectorsize;
|
| 214 | dev_replace->tgtdev->io_align = fs_info->sectorsize;
|
| 215 | dev_replace->tgtdev->sector_size = fs_info->sectorsize;
|
| 216 | dev_replace->tgtdev->fs_info = fs_info;
|
| 217 | set_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
|
| 218 | &dev_replace->tgtdev->dev_state);
|
| 219 | }
|
| 220 | break;
|
| 221 | }
|
| 222 |
|
| 223 | return ret;
|
| 224 | }
|
| 225 |
|
| 226 | |
| 227 | * Initialize a new device for device replace target from a given source dev
|
| 228 | * and path.
|
| 229 | *
|
| 230 | * Return 0 and new device in @device_out, otherwise return < 0
|
| 231 |
|
| 232 | static int btrfs_init_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
|
| 233 | const char *device_path,
|
| 234 | struct btrfs_device *srcdev,
|
| 235 | struct btrfs_device **device_out)
|
| 236 | {
|
| 237 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
|
| 238 | struct btrfs_device *device = NULL;
|
| 239 | struct btrfs_device *tmp_device;
|
| 240 | struct file *bdev_file;
|
| 241 | struct block_device *bdev;
|
| 242 | u64 devid = BTRFS_DEV_REPLACE_DEVID;
|
| 243 | int ret = 0;
|
| 244 |
|
| 245 | *device_out = NULL;
|
| 246 | if (srcdev->fs_devices->seeding) {
|
| 247 | btrfs_err(fs_info, "the filesystem is a seed filesystem!");
|
| 248 | return -EINVAL;
|
| 249 | }
|
| 250 |
|
| 251 |
|
| 252 | bdev_file = btrfs_open_device_deny_freeze(device_path, fs_info->sb);
|
| 253 | if (IS_ERR(bdev_file)) {
|
| 254 | btrfs_err(fs_info, "target device %s is invalid!", device_path);
|
| 255 | return PTR_ERR(bdev_file);
|
| 256 | }
|
| 257 | bdev = file_bdev(bdev_file);
|
| 258 |
|
| 259 | if (!btrfs_check_device_zone_type(fs_info, bdev)) {
|
| 260 | btrfs_err(fs_info,
|
| 261 | "dev-replace: zoned type of target device mismatch with filesystem");
|
| 262 | ret = -EINVAL;
|
| 263 | goto error;
|
| 264 | }
|
| 265 |
|
| 266 | sync_blockdev(bdev);
|
| 267 |
|
| 268 | list_for_each_entry(tmp_device, &fs_devices->devices, dev_list) {
|
| 269 | if (tmp_device->bdev == bdev) {
|
| 270 | btrfs_err(fs_info,
|
| 271 | "target device is in the filesystem!");
|
| 272 | ret = -EEXIST;
|
| 273 | goto error;
|
| 274 | }
|
| 275 | }
|
| 276 |
|
| 277 |
|
| 278 | if (bdev_nr_bytes(bdev) < btrfs_device_get_total_bytes(srcdev)) {
|
| 279 | btrfs_err(fs_info,
|
| 280 | "target device is smaller than source device!");
|
| 281 | ret = -EINVAL;
|
| 282 | goto error;
|
| 283 | }
|
| 284 |
|
| 285 |
|
| 286 | device = btrfs_alloc_device(NULL, &devid, NULL, device_path);
|
| 287 | if (IS_ERR(device)) {
|
| 288 | ret = PTR_ERR(device);
|
| 289 | device = NULL;
|
| 290 | goto error;
|
| 291 | }
|
| 292 |
|
| 293 | ret = lookup_bdev(device_path, &device->devt);
|
| 294 | if (ret)
|
| 295 | goto error;
|
| 296 |
|
| 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 | device->bdev_file = bdev_file;
|
| 310 | set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
|
| 311 | set_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
|
| 312 |
|
| 313 | atomic_inc(&device->dev_stats_ccnt);
|
| 314 | device->dev_stats_valid = 1;
|
| 315 | set_blocksize(bdev_file, BTRFS_BDEV_BLOCKSIZE);
|
| 316 | device->fs_devices = fs_devices;
|
| 317 |
|
| 318 | ret = btrfs_get_dev_zone_info(device, false);
|
| 319 | if (ret)
|
| 320 | goto error;
|
| 321 |
|
| 322 | mutex_lock(&fs_devices->device_list_mutex);
|
| 323 | list_add(&device->dev_list, &fs_devices->devices);
|
| 324 | fs_devices->num_devices++;
|
| 325 | fs_devices->open_devices++;
|
| 326 | mutex_unlock(&fs_devices->device_list_mutex);
|
| 327 |
|
| 328 | *device_out = device;
|
| 329 | return 0;
|
| 330 |
|
| 331 | error:
|
| 332 |
|
| 333 | if (device)
|
| 334 | btrfs_free_device(device);
|
| 335 | btrfs_release_device_allow_freeze(bdev_file);
|
| 336 | return ret;
|
| 337 | }
|
| 338 |
|
| 339 | |
| 340 | * called from commit_transaction. Writes changed device replace state to
|
| 341 | * disk.
|
| 342 |
|
| 343 | int btrfs_run_dev_replace(struct btrfs_trans_handle *trans)
|
| 344 | {
|
| 345 | struct btrfs_fs_info *fs_info = trans->fs_info;
|
| 346 | int ret;
|
| 347 | struct btrfs_root *dev_root = fs_info->dev_root;
|
| 348 | BTRFS_PATH_AUTO_FREE(path);
|
| 349 | struct btrfs_key key;
|
| 350 | struct extent_buffer *eb;
|
| 351 | struct btrfs_dev_replace_item *ptr;
|
| 352 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
|
| 353 |
|
| 354 | down_read(&dev_replace->rwsem);
|
| 355 | if (!dev_replace->is_valid ||
|
| 356 | !dev_replace->item_needs_writeback) {
|
| 357 | up_read(&dev_replace->rwsem);
|
| 358 | return 0;
|
| 359 | }
|
| 360 | up_read(&dev_replace->rwsem);
|
| 361 |
|
| 362 | key.objectid = 0;
|
| 363 | key.type = BTRFS_DEV_REPLACE_KEY;
|
| 364 | key.offset = 0;
|
| 365 |
|
| 366 | path = btrfs_alloc_path();
|
| 367 | if (!path)
|
| 368 | return -ENOMEM;
|
| 369 |
|
| 370 | ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
|
| 371 | if (ret < 0) {
|
| 372 | btrfs_warn(fs_info,
|
| 373 | "error %d while searching for dev_replace item!",
|
| 374 | ret);
|
| 375 | return ret;
|
| 376 | }
|
| 377 |
|
| 378 | if (ret == 0 &&
|
| 379 | btrfs_item_size(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
|
| 380 | |
| 381 | * need to delete old one and insert a new one.
|
| 382 | * Since no attempt is made to recover any old state, if the
|
| 383 | * dev_replace state is 'running', the data on the target
|
| 384 | * drive is lost.
|
| 385 | * It would be possible to recover the state: just make sure
|
| 386 | * that the beginning of the item is never changed and always
|
| 387 | * contains all the essential information. Then read this
|
| 388 | * minimal set of information and use it as a base for the
|
| 389 | * new state.
|
| 390 |
|
| 391 | ret = btrfs_del_item(trans, dev_root, path);
|
| 392 | if (ret != 0) {
|
| 393 | btrfs_warn(fs_info,
|
| 394 | "delete too small dev_replace item failed %d!",
|
| 395 | ret);
|
| 396 | return ret;
|
| 397 | }
|
| 398 | ret = 1;
|
| 399 | }
|
| 400 |
|
| 401 | if (ret == 1) {
|
| 402 |
|
| 403 | btrfs_release_path(path);
|
| 404 | ret = btrfs_insert_empty_item(trans, dev_root, path,
|
| 405 | &key, sizeof(*ptr));
|
| 406 | if (ret < 0) {
|
| 407 | btrfs_warn(fs_info,
|
| 408 | "insert dev_replace item failed %d!", ret);
|
| 409 | return ret;
|
| 410 | }
|
| 411 | }
|
| 412 |
|
| 413 | eb = path->nodes[0];
|
| 414 | ptr = btrfs_item_ptr(eb, path->slots[0],
|
| 415 | struct btrfs_dev_replace_item);
|
| 416 |
|
| 417 | down_write(&dev_replace->rwsem);
|
| 418 | if (dev_replace->srcdev)
|
| 419 | btrfs_set_dev_replace_src_devid(eb, ptr,
|
| 420 | dev_replace->srcdev->devid);
|
| 421 | else
|
| 422 | btrfs_set_dev_replace_src_devid(eb, ptr, (u64)-1);
|
| 423 | btrfs_set_dev_replace_cont_reading_from_srcdev_mode(eb, ptr,
|
| 424 | dev_replace->cont_reading_from_srcdev_mode);
|
| 425 | btrfs_set_dev_replace_replace_state(eb, ptr,
|
| 426 | dev_replace->replace_state);
|
| 427 | btrfs_set_dev_replace_time_started(eb, ptr, dev_replace->time_started);
|
| 428 | btrfs_set_dev_replace_time_stopped(eb, ptr, dev_replace->time_stopped);
|
| 429 | btrfs_set_dev_replace_num_write_errors(eb, ptr,
|
| 430 | atomic64_read(&dev_replace->num_write_errors));
|
| 431 | btrfs_set_dev_replace_num_uncorrectable_read_errors(eb, ptr,
|
| 432 | atomic64_read(&dev_replace->num_uncorrectable_read_errors));
|
| 433 | dev_replace->cursor_left_last_write_of_item =
|
| 434 | dev_replace->cursor_left;
|
| 435 | btrfs_set_dev_replace_cursor_left(eb, ptr,
|
| 436 | dev_replace->cursor_left_last_write_of_item);
|
| 437 | btrfs_set_dev_replace_cursor_right(eb, ptr,
|
| 438 | dev_replace->cursor_right);
|
| 439 | dev_replace->item_needs_writeback = 0;
|
| 440 | up_write(&dev_replace->rwsem);
|
| 441 |
|
| 442 | return ret;
|
| 443 | }
|
| 444 |
|
| 445 | static int mark_block_group_to_copy(struct btrfs_fs_info *fs_info,
|
| 446 | struct btrfs_device *src_dev)
|
| 447 | {
|
| 448 | struct btrfs_path *path;
|
| 449 | struct btrfs_key key;
|
| 450 | struct btrfs_key found_key;
|
| 451 | struct btrfs_root *root = fs_info->dev_root;
|
| 452 | struct btrfs_dev_extent *dev_extent = NULL;
|
| 453 | struct btrfs_block_group *cache;
|
| 454 | struct btrfs_trans_handle *trans;
|
| 455 | int iter_ret = 0;
|
| 456 | int ret = 0;
|
| 457 | u64 chunk_offset;
|
| 458 |
|
| 459 |
|
| 460 | if (!btrfs_is_zoned(fs_info))
|
| 461 | return 0;
|
| 462 |
|
| 463 | mutex_lock(&fs_info->chunk_mutex);
|
| 464 |
|
| 465 |
|
| 466 | spin_lock(&fs_info->trans_lock);
|
| 467 | while (fs_info->running_transaction &&
|
| 468 | !list_empty(&fs_info->running_transaction->dev_update_list)) {
|
| 469 | spin_unlock(&fs_info->trans_lock);
|
| 470 | mutex_unlock(&fs_info->chunk_mutex);
|
| 471 | trans = btrfs_attach_transaction(root);
|
| 472 | if (IS_ERR(trans)) {
|
| 473 | ret = PTR_ERR(trans);
|
| 474 | mutex_lock(&fs_info->chunk_mutex);
|
| 475 | if (ret == -ENOENT) {
|
| 476 | spin_lock(&fs_info->trans_lock);
|
| 477 | continue;
|
| 478 | } else {
|
| 479 | goto unlock;
|
| 480 | }
|
| 481 | }
|
| 482 |
|
| 483 | ret = btrfs_commit_transaction(trans);
|
| 484 | mutex_lock(&fs_info->chunk_mutex);
|
| 485 | if (ret)
|
| 486 | goto unlock;
|
| 487 |
|
| 488 | spin_lock(&fs_info->trans_lock);
|
| 489 | }
|
| 490 | spin_unlock(&fs_info->trans_lock);
|
| 491 |
|
| 492 | path = btrfs_alloc_path();
|
| 493 | if (!path) {
|
| 494 | ret = -ENOMEM;
|
| 495 | goto unlock;
|
| 496 | }
|
| 497 |
|
| 498 | path->reada = READA_FORWARD;
|
| 499 | path->search_commit_root = true;
|
| 500 | path->skip_locking = true;
|
| 501 | path->need_commit_sem = true;
|
| 502 |
|
| 503 | key.objectid = src_dev->devid;
|
| 504 | key.type = BTRFS_DEV_EXTENT_KEY;
|
| 505 | key.offset = 0;
|
| 506 |
|
| 507 | btrfs_for_each_slot(root, &key, &found_key, path, iter_ret) {
|
| 508 | struct extent_buffer *leaf = path->nodes[0];
|
| 509 |
|
| 510 | if (found_key.objectid != src_dev->devid)
|
| 511 | break;
|
| 512 |
|
| 513 | if (found_key.type != BTRFS_DEV_EXTENT_KEY)
|
| 514 | break;
|
| 515 |
|
| 516 | if (found_key.offset < key.offset)
|
| 517 | break;
|
| 518 |
|
| 519 | dev_extent = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_extent);
|
| 520 |
|
| 521 | chunk_offset = btrfs_dev_extent_chunk_offset(leaf, dev_extent);
|
| 522 |
|
| 523 | cache = btrfs_lookup_block_group(fs_info, chunk_offset);
|
| 524 | if (!cache)
|
| 525 | continue;
|
| 526 |
|
| 527 | set_bit(BLOCK_GROUP_FLAG_TO_COPY, &cache->runtime_flags);
|
| 528 | btrfs_put_block_group(cache);
|
| 529 | }
|
| 530 | if (iter_ret < 0)
|
| 531 | ret = iter_ret;
|
| 532 |
|
| 533 | btrfs_free_path(path);
|
| 534 | unlock:
|
| 535 | mutex_unlock(&fs_info->chunk_mutex);
|
| 536 |
|
| 537 | return ret;
|
| 538 | }
|
| 539 |
|
| 540 | bool btrfs_finish_block_group_to_copy(struct btrfs_device *srcdev,
|
| 541 | struct btrfs_block_group *cache,
|
| 542 | u64 physical)
|
| 543 | {
|
| 544 | struct btrfs_fs_info *fs_info = cache->fs_info;
|
| 545 | struct btrfs_chunk_map *map;
|
| 546 | u64 chunk_offset = cache->start;
|
| 547 | int num_extents, cur_extent;
|
| 548 | int i;
|
| 549 |
|
| 550 |
|
| 551 | if (!btrfs_is_zoned(fs_info))
|
| 552 | return true;
|
| 553 |
|
| 554 | spin_lock(&cache->lock);
|
| 555 | if (test_bit(BLOCK_GROUP_FLAG_REMOVED, &cache->runtime_flags)) {
|
| 556 | spin_unlock(&cache->lock);
|
| 557 | return true;
|
| 558 | }
|
| 559 | spin_unlock(&cache->lock);
|
| 560 |
|
| 561 | map = btrfs_get_chunk_map(fs_info, chunk_offset, 1);
|
| 562 | ASSERT(!IS_ERR(map));
|
| 563 |
|
| 564 | num_extents = 0;
|
| 565 | cur_extent = 0;
|
| 566 | for (i = 0; i < map->num_stripes; i++) {
|
| 567 |
|
| 568 | if (srcdev != map->stripes[i].dev)
|
| 569 | continue;
|
| 570 |
|
| 571 | num_extents++;
|
| 572 | if (physical == map->stripes[i].physical)
|
| 573 | cur_extent = i;
|
| 574 | }
|
| 575 |
|
| 576 | btrfs_free_chunk_map(map);
|
| 577 |
|
| 578 | if (num_extents > 1 && cur_extent < num_extents - 1) {
|
| 579 | |
| 580 | * Has more stripes on this device. Keep this block group
|
| 581 | * readonly until we finish all the stripes.
|
| 582 |
|
| 583 | return false;
|
| 584 | }
|
| 585 |
|
| 586 |
|
| 587 | clear_bit(BLOCK_GROUP_FLAG_TO_COPY, &cache->runtime_flags);
|
| 588 |
|
| 589 | return true;
|
| 590 | }
|
| 591 |
|
| 592 | static int btrfs_dev_replace_start(struct btrfs_fs_info *fs_info,
|
| 593 | const char *tgtdev_name, u64 srcdevid, const char *srcdev_name,
|
| 594 | int read_src)
|
| 595 | {
|
| 596 | struct btrfs_root *root = fs_info->dev_root;
|
| 597 | struct btrfs_trans_handle *trans;
|
| 598 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
|
| 599 | int ret;
|
| 600 | struct btrfs_device *tgt_device = NULL;
|
| 601 | struct btrfs_device *src_device = NULL;
|
| 602 |
|
| 603 | src_device = btrfs_find_device_by_devspec(fs_info, srcdevid,
|
| 604 | srcdev_name);
|
| 605 | if (IS_ERR(src_device))
|
| 606 | return PTR_ERR(src_device);
|
| 607 |
|
| 608 | if (btrfs_pinned_by_swapfile(fs_info, src_device)) {
|
| 609 | btrfs_warn(fs_info,
|
| 610 | "cannot replace device %s (devid %llu) due to active swapfile",
|
| 611 | btrfs_dev_name(src_device), src_device->devid);
|
| 612 | return -ETXTBSY;
|
| 613 | }
|
| 614 |
|
| 615 | |
| 616 | * Here we commit the transaction to make sure commit_total_bytes
|
| 617 | * of all the devices are updated.
|
| 618 |
|
| 619 | trans = btrfs_attach_transaction(root);
|
| 620 | if (!IS_ERR(trans)) {
|
| 621 | ret = btrfs_commit_transaction(trans);
|
| 622 | if (ret)
|
| 623 | return ret;
|
| 624 | } else if (PTR_ERR(trans) != -ENOENT) {
|
| 625 | return PTR_ERR(trans);
|
| 626 | }
|
| 627 |
|
| 628 | ret = btrfs_init_dev_replace_tgtdev(fs_info, tgtdev_name,
|
| 629 | src_device, &tgt_device);
|
| 630 | if (ret)
|
| 631 | return ret;
|
| 632 |
|
| 633 |
|
| 634 | if (src_device->bdev) {
|
| 635 | ret = bdev_deny_freeze(src_device->bdev);
|
| 636 | if (ret) {
|
| 637 | btrfs_destroy_dev_replace_tgtdev(tgt_device, true);
|
| 638 | return ret;
|
| 639 | }
|
| 640 | }
|
| 641 |
|
| 642 | ret = mark_block_group_to_copy(fs_info, src_device);
|
| 643 | if (ret)
|
| 644 | goto leave;
|
| 645 |
|
| 646 | down_write(&dev_replace->rwsem);
|
| 647 | dev_replace->replace_task = current;
|
| 648 | switch (dev_replace->replace_state) {
|
| 649 | case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
|
| 650 | case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
|
| 651 | case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
|
| 652 | break;
|
| 653 | case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
|
| 654 | case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
|
| 655 | DEBUG_WARN("unexpected STARTED or SUSPENDED dev-replace state");
|
| 656 | ret = BTRFS_IOCTL_DEV_REPLACE_RESULT_ALREADY_STARTED;
|
| 657 | up_write(&dev_replace->rwsem);
|
| 658 | goto leave;
|
| 659 | }
|
| 660 |
|
| 661 | dev_replace->cont_reading_from_srcdev_mode = read_src;
|
| 662 | dev_replace->srcdev = src_device;
|
| 663 | dev_replace->tgtdev = tgt_device;
|
| 664 |
|
| 665 | btrfs_info(fs_info,
|
| 666 | "dev_replace from %s (devid %llu) to %s started",
|
| 667 | btrfs_dev_name(src_device),
|
| 668 | src_device->devid,
|
| 669 | btrfs_dev_name(tgt_device));
|
| 670 |
|
| 671 | |
| 672 | * from now on, the writes to the srcdev are all duplicated to
|
| 673 | * go to the tgtdev as well (refer to btrfs_map_block()).
|
| 674 |
|
| 675 | dev_replace->replace_state = BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED;
|
| 676 | dev_replace->time_started = ktime_get_real_seconds();
|
| 677 | dev_replace->cursor_left = 0;
|
| 678 | dev_replace->committed_cursor_left = 0;
|
| 679 | dev_replace->cursor_left_last_write_of_item = 0;
|
| 680 | dev_replace->cursor_right = 0;
|
| 681 | dev_replace->is_valid = 1;
|
| 682 | dev_replace->item_needs_writeback = 1;
|
| 683 | atomic64_set(&dev_replace->num_write_errors, 0);
|
| 684 | atomic64_set(&dev_replace->num_uncorrectable_read_errors, 0);
|
| 685 | up_write(&dev_replace->rwsem);
|
| 686 |
|
| 687 | ret = btrfs_sysfs_add_device(tgt_device);
|
| 688 | if (ret)
|
| 689 | btrfs_err(fs_info, "kobj add dev failed %d", ret);
|
| 690 |
|
| 691 | btrfs_wait_ordered_roots(fs_info, U64_MAX, NULL);
|
| 692 |
|
| 693 | |
| 694 | * Commit dev_replace state and reserve 1 item for it.
|
| 695 | * This is crucial to ensure we won't miss copying extents for new block
|
| 696 | * groups that are allocated after we started the device replace, and
|
| 697 | * must be done after setting up the device replace state.
|
| 698 |
|
| 699 | trans = btrfs_start_transaction(root, 1);
|
| 700 | if (IS_ERR(trans)) {
|
| 701 | ret = PTR_ERR(trans);
|
| 702 | down_write(&dev_replace->rwsem);
|
| 703 | dev_replace->replace_state =
|
| 704 | BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED;
|
| 705 | dev_replace->srcdev = NULL;
|
| 706 | dev_replace->tgtdev = NULL;
|
| 707 | up_write(&dev_replace->rwsem);
|
| 708 | goto leave;
|
| 709 | }
|
| 710 |
|
| 711 | ret = btrfs_commit_transaction(trans);
|
| 712 | WARN_ON(ret);
|
| 713 |
|
| 714 |
|
| 715 | ret = btrfs_scrub_dev(fs_info, src_device->devid, 0,
|
| 716 | btrfs_device_get_total_bytes(src_device),
|
| 717 | &dev_replace->scrub_progress, false, true);
|
| 718 |
|
| 719 | ret = btrfs_dev_replace_finishing(fs_info, ret);
|
| 720 | if (ret == -EINPROGRESS)
|
| 721 | ret = BTRFS_IOCTL_DEV_REPLACE_RESULT_SCRUB_INPROGRESS;
|
| 722 |
|
| 723 | return ret;
|
| 724 |
|
| 725 | leave:
|
| 726 | if (src_device->bdev)
|
| 727 | bdev_allow_freeze(src_device->bdev);
|
| 728 | btrfs_destroy_dev_replace_tgtdev(tgt_device, true);
|
| 729 | return ret;
|
| 730 | }
|
| 731 |
|
| 732 | static int btrfs_check_replace_dev_names(struct btrfs_ioctl_dev_replace_args *args)
|
| 733 | {
|
| 734 | if (args->start.srcdevid == 0) {
|
| 735 | if (memchr(args->start.srcdev_name, 0,
|
| 736 | sizeof(args->start.srcdev_name)) == NULL)
|
| 737 | return -ENAMETOOLONG;
|
| 738 | } else {
|
| 739 | args->start.srcdev_name[0] = 0;
|
| 740 | }
|
| 741 |
|
| 742 | if (memchr(args->start.tgtdev_name, 0,
|
| 743 | sizeof(args->start.tgtdev_name)) == NULL)
|
| 744 | return -ENAMETOOLONG;
|
| 745 |
|
| 746 | return 0;
|
| 747 | }
|
| 748 |
|
| 749 | int btrfs_dev_replace_by_ioctl(struct btrfs_fs_info *fs_info,
|
| 750 | struct btrfs_ioctl_dev_replace_args *args)
|
| 751 | {
|
| 752 | int ret;
|
| 753 |
|
| 754 | switch (args->start.cont_reading_from_srcdev_mode) {
|
| 755 | case BTRFS_IOCTL_DEV_REPLACE_CONT_READING_FROM_SRCDEV_MODE_ALWAYS:
|
| 756 | case BTRFS_IOCTL_DEV_REPLACE_CONT_READING_FROM_SRCDEV_MODE_AVOID:
|
| 757 | break;
|
| 758 | default:
|
| 759 | return -EINVAL;
|
| 760 | }
|
| 761 | ret = btrfs_check_replace_dev_names(args);
|
| 762 | if (ret < 0)
|
| 763 | return ret;
|
| 764 |
|
| 765 | ret = btrfs_dev_replace_start(fs_info, args->start.tgtdev_name,
|
| 766 | args->start.srcdevid,
|
| 767 | args->start.srcdev_name,
|
| 768 | args->start.cont_reading_from_srcdev_mode);
|
| 769 | args->result = ret;
|
| 770 |
|
| 771 | if (ret == BTRFS_IOCTL_DEV_REPLACE_RESULT_SCRUB_INPROGRESS ||
|
| 772 | ret == BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR)
|
| 773 | return 0;
|
| 774 |
|
| 775 | return ret;
|
| 776 | }
|
| 777 |
|
| 778 | |
| 779 | * blocked until all in-flight bios operations are finished.
|
| 780 |
|
| 781 | static void btrfs_rm_dev_replace_blocked(struct btrfs_fs_info *fs_info)
|
| 782 | {
|
| 783 | set_bit(BTRFS_FS_STATE_DEV_REPLACING, &fs_info->fs_state);
|
| 784 | wait_event(fs_info->dev_replace.replace_wait, !percpu_counter_sum(
|
| 785 | &fs_info->dev_replace.bio_counter));
|
| 786 | }
|
| 787 |
|
| 788 | |
| 789 | * we have removed target device, it is safe to allow new bios request.
|
| 790 |
|
| 791 | static void btrfs_rm_dev_replace_unblocked(struct btrfs_fs_info *fs_info)
|
| 792 | {
|
| 793 | clear_bit(BTRFS_FS_STATE_DEV_REPLACING, &fs_info->fs_state);
|
| 794 | wake_up(&fs_info->dev_replace.replace_wait);
|
| 795 | }
|
| 796 |
|
| 797 | |
| 798 | * When finishing the device replace, before swapping the source device with the
|
| 799 | * target device we must update the chunk allocation state in the target device,
|
| 800 | * as it is empty because replace works by directly copying the chunks and not
|
| 801 | * through the normal chunk allocation path.
|
| 802 |
|
| 803 | static int btrfs_set_target_alloc_state(struct btrfs_device *srcdev,
|
| 804 | struct btrfs_device *tgtdev)
|
| 805 | {
|
| 806 | struct extent_state *cached_state = NULL;
|
| 807 | u64 start = 0;
|
| 808 | u64 found_start;
|
| 809 | u64 found_end;
|
| 810 | int ret = 0;
|
| 811 |
|
| 812 | lockdep_assert_held(&srcdev->fs_info->chunk_mutex);
|
| 813 |
|
| 814 | while (btrfs_find_first_extent_bit(&srcdev->alloc_state, start,
|
| 815 | &found_start, &found_end,
|
| 816 | CHUNK_ALLOCATED, &cached_state)) {
|
| 817 | ret = btrfs_set_extent_bit(&tgtdev->alloc_state, found_start,
|
| 818 | found_end, CHUNK_ALLOCATED, NULL);
|
| 819 | if (ret)
|
| 820 | break;
|
| 821 | start = found_end + 1;
|
| 822 | }
|
| 823 |
|
| 824 | btrfs_free_extent_state(cached_state);
|
| 825 | return ret;
|
| 826 | }
|
| 827 |
|
| 828 | static void btrfs_dev_replace_update_device_in_mapping_tree(
|
| 829 | struct btrfs_fs_info *fs_info,
|
| 830 | struct btrfs_device *srcdev,
|
| 831 | struct btrfs_device *tgtdev)
|
| 832 | {
|
| 833 | struct rb_node *node;
|
| 834 |
|
| 835 | |
| 836 | * The chunk mutex must be held so that no new chunks can be created
|
| 837 | * while we are updating existing chunks. This guarantees we don't miss
|
| 838 | * any new chunk that gets created for a range that falls before the
|
| 839 | * range of the last chunk we processed.
|
| 840 |
|
| 841 | lockdep_assert_held(&fs_info->chunk_mutex);
|
| 842 |
|
| 843 | write_lock(&fs_info->mapping_tree_lock);
|
| 844 | node = rb_first_cached(&fs_info->mapping_tree);
|
| 845 | while (node) {
|
| 846 | struct rb_node *next = rb_next(node);
|
| 847 | struct btrfs_chunk_map *map;
|
| 848 | u64 next_start;
|
| 849 |
|
| 850 | map = rb_entry(node, struct btrfs_chunk_map, rb_node);
|
| 851 | next_start = map->start + map->chunk_len;
|
| 852 |
|
| 853 | for (int i = 0; i < map->num_stripes; i++)
|
| 854 | if (srcdev == map->stripes[i].dev)
|
| 855 | map->stripes[i].dev = tgtdev;
|
| 856 |
|
| 857 | if (cond_resched_rwlock_write(&fs_info->mapping_tree_lock)) {
|
| 858 | map = btrfs_find_chunk_map_nolock(fs_info, next_start, U64_MAX);
|
| 859 | if (!map)
|
| 860 | break;
|
| 861 | node = &map->rb_node;
|
| 862 | |
| 863 | * Drop the lookup reference since we are holding the
|
| 864 | * lock in write mode and no one can remove the chunk
|
| 865 | * map from the tree and drop its tree reference.
|
| 866 |
|
| 867 | btrfs_free_chunk_map(map);
|
| 868 | } else {
|
| 869 | node = next;
|
| 870 | }
|
| 871 | }
|
| 872 | write_unlock(&fs_info->mapping_tree_lock);
|
| 873 | }
|
| 874 |
|
| 875 | static int btrfs_dev_replace_finishing(struct btrfs_fs_info *fs_info,
|
| 876 | int scrub_ret)
|
| 877 | {
|
| 878 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
|
| 879 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
|
| 880 | struct btrfs_device *tgt_device;
|
| 881 | struct btrfs_device *src_device;
|
| 882 | struct btrfs_root *root = fs_info->tree_root;
|
| 883 | u8 uuid_tmp[BTRFS_UUID_SIZE];
|
| 884 | struct btrfs_trans_handle *trans;
|
| 885 | int ret = 0;
|
| 886 |
|
| 887 |
|
| 888 | mutex_lock(&dev_replace->lock_finishing_cancel_unmount);
|
| 889 |
|
| 890 | down_read(&dev_replace->rwsem);
|
| 891 |
|
| 892 | if (dev_replace->replace_state !=
|
| 893 | BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED) {
|
| 894 | up_read(&dev_replace->rwsem);
|
| 895 | mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
|
| 896 | return 0;
|
| 897 | }
|
| 898 |
|
| 899 | tgt_device = dev_replace->tgtdev;
|
| 900 | src_device = dev_replace->srcdev;
|
| 901 | up_read(&dev_replace->rwsem);
|
| 902 |
|
| 903 | |
| 904 | * flush all outstanding I/O and inode extent mappings before the
|
| 905 | * copy operation is declared as being finished
|
| 906 |
|
| 907 | ret = btrfs_start_delalloc_roots(fs_info, LONG_MAX, false);
|
| 908 | if (ret) {
|
| 909 |
|
| 910 | mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
|
| 911 | return ret;
|
| 912 | }
|
| 913 | btrfs_wait_ordered_roots(fs_info, U64_MAX, NULL);
|
| 914 |
|
| 915 | |
| 916 | * We have to use this loop approach because at this point src_device
|
| 917 | * has to be available for transaction commit to complete, yet new
|
| 918 | * chunks shouldn't be allocated on the device.
|
| 919 |
|
| 920 | while (1) {
|
| 921 | trans = btrfs_start_transaction(root, 0);
|
| 922 | if (IS_ERR(trans)) {
|
| 923 |
|
| 924 | mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
|
| 925 | return PTR_ERR(trans);
|
| 926 | }
|
| 927 | ret = btrfs_commit_transaction(trans);
|
| 928 | WARN_ON(ret);
|
| 929 |
|
| 930 |
|
| 931 | mutex_lock(&fs_devices->device_list_mutex);
|
| 932 |
|
| 933 | mutex_lock(&fs_info->chunk_mutex);
|
| 934 |
|
| 935 | if (!list_empty(&src_device->post_commit_list)) {
|
| 936 | mutex_unlock(&fs_devices->device_list_mutex);
|
| 937 | mutex_unlock(&fs_info->chunk_mutex);
|
| 938 | } else {
|
| 939 | break;
|
| 940 | }
|
| 941 | }
|
| 942 |
|
| 943 | down_write(&dev_replace->rwsem);
|
| 944 | dev_replace->replace_state =
|
| 945 | scrub_ret ? BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED
|
| 946 | : BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED;
|
| 947 | dev_replace->tgtdev = NULL;
|
| 948 | dev_replace->srcdev = NULL;
|
| 949 | dev_replace->time_stopped = ktime_get_real_seconds();
|
| 950 | dev_replace->item_needs_writeback = 1;
|
| 951 |
|
| 952 | |
| 953 | * Update allocation state in the new device and replace the old device
|
| 954 | * with the new one in the mapping tree.
|
| 955 |
|
| 956 | if (!scrub_ret) {
|
| 957 | scrub_ret = btrfs_set_target_alloc_state(src_device, tgt_device);
|
| 958 | if (scrub_ret)
|
| 959 | goto error;
|
| 960 | btrfs_dev_replace_update_device_in_mapping_tree(fs_info,
|
| 961 | src_device,
|
| 962 | tgt_device);
|
| 963 | } else {
|
| 964 | if (scrub_ret != -ECANCELED)
|
| 965 | btrfs_err(fs_info,
|
| 966 | "btrfs_scrub_dev(%s, %llu, %s) failed %d",
|
| 967 | btrfs_dev_name(src_device),
|
| 968 | src_device->devid,
|
| 969 | btrfs_dev_name(tgt_device), scrub_ret);
|
| 970 | error:
|
| 971 | up_write(&dev_replace->rwsem);
|
| 972 | mutex_unlock(&fs_info->chunk_mutex);
|
| 973 | mutex_unlock(&fs_devices->device_list_mutex);
|
| 974 | btrfs_rm_dev_replace_blocked(fs_info);
|
| 975 | if (tgt_device)
|
| 976 | btrfs_destroy_dev_replace_tgtdev(tgt_device, true);
|
| 977 |
|
| 978 | if (src_device->bdev)
|
| 979 | bdev_allow_freeze(src_device->bdev);
|
| 980 | btrfs_rm_dev_replace_unblocked(fs_info);
|
| 981 | mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
|
| 982 |
|
| 983 | return scrub_ret;
|
| 984 | }
|
| 985 |
|
| 986 | btrfs_info(fs_info,
|
| 987 | "dev_replace from %s (devid %llu) to %s finished",
|
| 988 | btrfs_dev_name(src_device),
|
| 989 | src_device->devid,
|
| 990 | btrfs_dev_name(tgt_device));
|
| 991 | clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &tgt_device->dev_state);
|
| 992 | tgt_device->devid = src_device->devid;
|
| 993 | src_device->devid = BTRFS_DEV_REPLACE_DEVID;
|
| 994 | memcpy(uuid_tmp, tgt_device->uuid, sizeof(uuid_tmp));
|
| 995 | memcpy(tgt_device->uuid, src_device->uuid, sizeof(tgt_device->uuid));
|
| 996 | memcpy(src_device->uuid, uuid_tmp, sizeof(src_device->uuid));
|
| 997 | btrfs_device_set_total_bytes(tgt_device, src_device->total_bytes);
|
| 998 | btrfs_device_set_disk_total_bytes(tgt_device,
|
| 999 | src_device->disk_total_bytes);
|
| 1000 | btrfs_device_set_bytes_used(tgt_device, src_device->bytes_used);
|
| 1001 | tgt_device->commit_bytes_used = src_device->bytes_used;
|
| 1002 |
|
| 1003 | btrfs_assign_next_active_device(src_device, tgt_device);
|
| 1004 |
|
| 1005 | list_add(&tgt_device->dev_alloc_list, &fs_devices->alloc_list);
|
| 1006 | fs_devices->rw_devices++;
|
| 1007 |
|
| 1008 | dev_replace->replace_task = NULL;
|
| 1009 | up_write(&dev_replace->rwsem);
|
| 1010 | btrfs_rm_dev_replace_blocked(fs_info);
|
| 1011 |
|
| 1012 | btrfs_rm_dev_replace_remove_srcdev(src_device);
|
| 1013 |
|
| 1014 | btrfs_rm_dev_replace_unblocked(fs_info);
|
| 1015 |
|
| 1016 | |
| 1017 | * Increment dev_stats_ccnt so that btrfs_run_dev_stats() will
|
| 1018 | * update on-disk dev stats value during commit transaction
|
| 1019 |
|
| 1020 | atomic_inc(&tgt_device->dev_stats_ccnt);
|
| 1021 |
|
| 1022 | |
| 1023 | * this is again a consistent state where no dev_replace procedure
|
| 1024 | * is running, the target device is part of the filesystem, the
|
| 1025 | * source device is not part of the filesystem anymore and its 1st
|
| 1026 | * superblock is scratched out so that it is no longer marked to
|
| 1027 | * belong to this filesystem.
|
| 1028 |
|
| 1029 | mutex_unlock(&fs_info->chunk_mutex);
|
| 1030 | mutex_unlock(&fs_devices->device_list_mutex);
|
| 1031 |
|
| 1032 |
|
| 1033 | btrfs_sysfs_remove_device(src_device);
|
| 1034 | btrfs_sysfs_update_devid(tgt_device);
|
| 1035 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &src_device->dev_state))
|
| 1036 | btrfs_scratch_superblocks(fs_info, src_device);
|
| 1037 |
|
| 1038 |
|
| 1039 | trans = btrfs_start_transaction(root, 0);
|
| 1040 | if (!IS_ERR(trans)) {
|
| 1041 | |
| 1042 | * Ignore any error here, if we failed to remove the DEV_STATS
|
| 1043 | * item for devid 0, it's not a big deal. We have other ways
|
| 1044 | * to address it.
|
| 1045 |
|
| 1046 | btrfs_remove_dev_stat_item(trans, BTRFS_DEV_REPLACE_DEVID);
|
| 1047 | btrfs_commit_transaction(trans);
|
| 1048 | }
|
| 1049 |
|
| 1050 | mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
|
| 1051 |
|
| 1052 |
|
| 1053 | bdev_allow_freeze(tgt_device->bdev);
|
| 1054 | btrfs_rm_dev_replace_free_srcdev(src_device);
|
| 1055 |
|
| 1056 | return 0;
|
| 1057 | }
|
| 1058 |
|
| 1059 | |
| 1060 | * Read progress of device replace status according to the state and last
|
| 1061 | * stored position. The value format is the same as for
|
| 1062 | * btrfs_dev_replace::progress_1000
|
| 1063 |
|
| 1064 | static u64 btrfs_dev_replace_progress(struct btrfs_fs_info *fs_info)
|
| 1065 | {
|
| 1066 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
|
| 1067 | u64 ret = 0;
|
| 1068 |
|
| 1069 | switch (dev_replace->replace_state) {
|
| 1070 | case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
|
| 1071 | case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
|
| 1072 | ret = 0;
|
| 1073 | break;
|
| 1074 | case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
|
| 1075 | ret = 1000;
|
| 1076 | break;
|
| 1077 | case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
|
| 1078 | case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
|
| 1079 | ret = div64_u64(dev_replace->cursor_left,
|
| 1080 | div_u64(btrfs_device_get_total_bytes(
|
| 1081 | dev_replace->srcdev), 1000));
|
| 1082 | break;
|
| 1083 | }
|
| 1084 |
|
| 1085 | return ret;
|
| 1086 | }
|
| 1087 |
|
| 1088 | void btrfs_dev_replace_status(struct btrfs_fs_info *fs_info,
|
| 1089 | struct btrfs_ioctl_dev_replace_args *args)
|
| 1090 | {
|
| 1091 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
|
| 1092 |
|
| 1093 | down_read(&dev_replace->rwsem);
|
| 1094 | |
| 1095 | * the replace_status ioctl
|
| 1096 | args->result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR;
|
| 1097 | args->status.replace_state = dev_replace->replace_state;
|
| 1098 | args->status.time_started = dev_replace->time_started;
|
| 1099 | args->status.time_stopped = dev_replace->time_stopped;
|
| 1100 | args->status.num_write_errors =
|
| 1101 | atomic64_read(&dev_replace->num_write_errors);
|
| 1102 | args->status.num_uncorrectable_read_errors =
|
| 1103 | atomic64_read(&dev_replace->num_uncorrectable_read_errors);
|
| 1104 | args->status.progress_1000 = btrfs_dev_replace_progress(fs_info);
|
| 1105 | up_read(&dev_replace->rwsem);
|
| 1106 | }
|
| 1107 |
|
| 1108 | int btrfs_dev_replace_cancel(struct btrfs_fs_info *fs_info)
|
| 1109 | {
|
| 1110 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
|
| 1111 | struct btrfs_device *tgt_device = NULL;
|
| 1112 | struct btrfs_device *src_device = NULL;
|
| 1113 | struct btrfs_trans_handle *trans;
|
| 1114 | struct btrfs_root *root = fs_info->tree_root;
|
| 1115 | int result;
|
| 1116 | int ret;
|
| 1117 |
|
| 1118 | if (sb_rdonly(fs_info->sb))
|
| 1119 | return -EROFS;
|
| 1120 |
|
| 1121 | mutex_lock(&dev_replace->lock_finishing_cancel_unmount);
|
| 1122 | down_write(&dev_replace->rwsem);
|
| 1123 | switch (dev_replace->replace_state) {
|
| 1124 | case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
|
| 1125 | case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
|
| 1126 | case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
|
| 1127 | result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NOT_STARTED;
|
| 1128 | up_write(&dev_replace->rwsem);
|
| 1129 | break;
|
| 1130 | case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
|
| 1131 | tgt_device = dev_replace->tgtdev;
|
| 1132 | src_device = dev_replace->srcdev;
|
| 1133 | up_write(&dev_replace->rwsem);
|
| 1134 | ret = btrfs_scrub_cancel(fs_info);
|
| 1135 | if (ret < 0) {
|
| 1136 | result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NOT_STARTED;
|
| 1137 | } else {
|
| 1138 | result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR;
|
| 1139 | |
| 1140 | * btrfs_dev_replace_finishing() will handle the
|
| 1141 | * cleanup part
|
| 1142 |
|
| 1143 | btrfs_info(fs_info,
|
| 1144 | "dev_replace from %s (devid %llu) to %s canceled",
|
| 1145 | btrfs_dev_name(src_device), src_device->devid,
|
| 1146 | btrfs_dev_name(tgt_device));
|
| 1147 | }
|
| 1148 | break;
|
| 1149 | case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
|
| 1150 | |
| 1151 | * Scrub doing the replace isn't running so we need to do the
|
| 1152 | * cleanup step of btrfs_dev_replace_finishing() here
|
| 1153 |
|
| 1154 | result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR;
|
| 1155 | tgt_device = dev_replace->tgtdev;
|
| 1156 | src_device = dev_replace->srcdev;
|
| 1157 | dev_replace->tgtdev = NULL;
|
| 1158 | dev_replace->srcdev = NULL;
|
| 1159 | dev_replace->replace_state =
|
| 1160 | BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED;
|
| 1161 | dev_replace->time_stopped = ktime_get_real_seconds();
|
| 1162 | dev_replace->item_needs_writeback = 1;
|
| 1163 |
|
| 1164 | up_write(&dev_replace->rwsem);
|
| 1165 |
|
| 1166 |
|
| 1167 | btrfs_scrub_cancel(fs_info);
|
| 1168 |
|
| 1169 | trans = btrfs_start_transaction(root, 0);
|
| 1170 | if (IS_ERR(trans)) {
|
| 1171 | mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
|
| 1172 | return PTR_ERR(trans);
|
| 1173 | }
|
| 1174 | ret = btrfs_commit_transaction(trans);
|
| 1175 | WARN_ON(ret);
|
| 1176 |
|
| 1177 | btrfs_info(fs_info,
|
| 1178 | "suspended dev_replace from %s (devid %llu) to %s canceled",
|
| 1179 | btrfs_dev_name(src_device), src_device->devid,
|
| 1180 | btrfs_dev_name(tgt_device));
|
| 1181 |
|
| 1182 |
|
| 1183 | if (tgt_device)
|
| 1184 | btrfs_destroy_dev_replace_tgtdev(tgt_device, false);
|
| 1185 | break;
|
| 1186 | default:
|
| 1187 | up_write(&dev_replace->rwsem);
|
| 1188 | result = -EINVAL;
|
| 1189 | }
|
| 1190 |
|
| 1191 | mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
|
| 1192 | return result;
|
| 1193 | }
|
| 1194 |
|
| 1195 | void btrfs_dev_replace_suspend_for_unmount(struct btrfs_fs_info *fs_info)
|
| 1196 | {
|
| 1197 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
|
| 1198 |
|
| 1199 | mutex_lock(&dev_replace->lock_finishing_cancel_unmount);
|
| 1200 | down_write(&dev_replace->rwsem);
|
| 1201 |
|
| 1202 | switch (dev_replace->replace_state) {
|
| 1203 | case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
|
| 1204 | case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
|
| 1205 | case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
|
| 1206 | case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
|
| 1207 | break;
|
| 1208 | case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
|
| 1209 | dev_replace->replace_state =
|
| 1210 | BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED;
|
| 1211 | dev_replace->time_stopped = ktime_get_real_seconds();
|
| 1212 | dev_replace->item_needs_writeback = 1;
|
| 1213 | btrfs_info(fs_info, "suspending dev_replace for unmount");
|
| 1214 |
|
| 1215 | if (dev_replace->srcdev && dev_replace->srcdev->bdev)
|
| 1216 | bdev_allow_freeze(dev_replace->srcdev->bdev);
|
| 1217 | if (dev_replace->tgtdev && dev_replace->tgtdev->bdev)
|
| 1218 | bdev_allow_freeze(dev_replace->tgtdev->bdev);
|
| 1219 | break;
|
| 1220 | }
|
| 1221 |
|
| 1222 | up_write(&dev_replace->rwsem);
|
| 1223 | mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
|
| 1224 | }
|
| 1225 |
|
| 1226 |
|
| 1227 | int btrfs_resume_dev_replace_async(struct btrfs_fs_info *fs_info)
|
| 1228 | {
|
| 1229 | struct task_struct *task;
|
| 1230 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
|
| 1231 | int ret = 0;
|
| 1232 |
|
| 1233 | down_write(&dev_replace->rwsem);
|
| 1234 |
|
| 1235 | switch (dev_replace->replace_state) {
|
| 1236 | case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
|
| 1237 | case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
|
| 1238 | case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
|
| 1239 | up_write(&dev_replace->rwsem);
|
| 1240 | return 0;
|
| 1241 | case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
|
| 1242 | break;
|
| 1243 | case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
|
| 1244 | dev_replace->replace_state =
|
| 1245 | BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED;
|
| 1246 | break;
|
| 1247 | }
|
| 1248 | if (!dev_replace->tgtdev || !dev_replace->tgtdev->bdev) {
|
| 1249 | btrfs_info(fs_info,
|
| 1250 | "cannot continue dev_replace, tgtdev is missing");
|
| 1251 | btrfs_info(fs_info,
|
| 1252 | "you may cancel the operation after 'mount -o degraded'");
|
| 1253 | dev_replace->replace_state =
|
| 1254 | BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED;
|
| 1255 | up_write(&dev_replace->rwsem);
|
| 1256 | return 0;
|
| 1257 | }
|
| 1258 | up_write(&dev_replace->rwsem);
|
| 1259 |
|
| 1260 | |
| 1261 | * This could collide with a paused balance, but the exclusive op logic
|
| 1262 | * should never allow both to start and pause. We don't want to allow
|
| 1263 | * dev-replace to start anyway.
|
| 1264 |
|
| 1265 | if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_DEV_REPLACE)) {
|
| 1266 | down_write(&dev_replace->rwsem);
|
| 1267 | dev_replace->replace_state =
|
| 1268 | BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED;
|
| 1269 | up_write(&dev_replace->rwsem);
|
| 1270 | btrfs_info(fs_info,
|
| 1271 | "cannot resume dev-replace, other exclusive operation running");
|
| 1272 | return 0;
|
| 1273 | }
|
| 1274 |
|
| 1275 |
|
| 1276 | if (dev_replace->srcdev->bdev &&
|
| 1277 | bdev_deny_freeze(dev_replace->srcdev->bdev))
|
| 1278 | goto suspend;
|
| 1279 | if (bdev_deny_freeze(dev_replace->tgtdev->bdev)) {
|
| 1280 | if (dev_replace->srcdev->bdev)
|
| 1281 | bdev_allow_freeze(dev_replace->srcdev->bdev);
|
| 1282 | goto suspend;
|
| 1283 | }
|
| 1284 |
|
| 1285 | task = kthread_run(btrfs_dev_replace_kthread, fs_info, "btrfs-devrepl");
|
| 1286 | if (IS_ERR(task)) {
|
| 1287 | bdev_allow_freeze(dev_replace->tgtdev->bdev);
|
| 1288 | if (dev_replace->srcdev->bdev)
|
| 1289 | bdev_allow_freeze(dev_replace->srcdev->bdev);
|
| 1290 |
|
| 1291 | ret = PTR_ERR(task);
|
| 1292 | goto suspend;
|
| 1293 | }
|
| 1294 | return 0;
|
| 1295 |
|
| 1296 | suspend:
|
| 1297 | btrfs_exclop_finish(fs_info);
|
| 1298 | down_write(&dev_replace->rwsem);
|
| 1299 | dev_replace->replace_state = BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED;
|
| 1300 | up_write(&dev_replace->rwsem);
|
| 1301 | return ret;
|
| 1302 | }
|
| 1303 |
|
| 1304 | static int btrfs_dev_replace_kthread(void *data)
|
| 1305 | {
|
| 1306 | struct btrfs_fs_info *fs_info = data;
|
| 1307 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
|
| 1308 | u64 progress;
|
| 1309 | int ret;
|
| 1310 |
|
| 1311 | progress = btrfs_dev_replace_progress(fs_info);
|
| 1312 | progress = div_u64(progress, 10);
|
| 1313 | btrfs_info(fs_info,
|
| 1314 | "continuing dev_replace from %s (devid %llu) to target %s @%u%%",
|
| 1315 | btrfs_dev_name(dev_replace->srcdev),
|
| 1316 | dev_replace->srcdev->devid,
|
| 1317 | btrfs_dev_name(dev_replace->tgtdev),
|
| 1318 | (unsigned int)progress);
|
| 1319 |
|
| 1320 | ret = btrfs_scrub_dev(fs_info, dev_replace->srcdev->devid,
|
| 1321 | dev_replace->committed_cursor_left,
|
| 1322 | btrfs_device_get_total_bytes(dev_replace->srcdev),
|
| 1323 | &dev_replace->scrub_progress, false, true);
|
| 1324 | ret = btrfs_dev_replace_finishing(fs_info, ret);
|
| 1325 | WARN_ON(ret && ret != -ECANCELED);
|
| 1326 |
|
| 1327 | btrfs_exclop_finish(fs_info);
|
| 1328 | return 0;
|
| 1329 | }
|
| 1330 |
|
| 1331 | bool __pure btrfs_dev_replace_is_ongoing(struct btrfs_dev_replace *dev_replace)
|
| 1332 | {
|
| 1333 | if (!dev_replace->is_valid)
|
| 1334 | return false;
|
| 1335 |
|
| 1336 | switch (dev_replace->replace_state) {
|
| 1337 | case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
|
| 1338 | case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
|
| 1339 | case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
|
| 1340 | return false;
|
| 1341 | case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
|
| 1342 | case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
|
| 1343 | |
| 1344 | * return true even if tgtdev is missing (this is
|
| 1345 | * something that can happen if the dev_replace
|
| 1346 | * procedure is suspended by an umount and then
|
| 1347 | * the tgtdev is missing (or "btrfs dev scan") was
|
| 1348 | * not called and the filesystem is remounted
|
| 1349 | * in degraded state. This does not stop the
|
| 1350 | * dev_replace procedure. It needs to be canceled
|
| 1351 | * manually if the cancellation is wanted.
|
| 1352 |
|
| 1353 | break;
|
| 1354 | }
|
| 1355 | return true;
|
| 1356 | }
|
| 1357 |
|
| 1358 | void btrfs_bio_counter_sub(struct btrfs_fs_info *fs_info, s64 amount)
|
| 1359 | {
|
| 1360 | percpu_counter_sub(&fs_info->dev_replace.bio_counter, amount);
|
| 1361 | cond_wake_up_nomb(&fs_info->dev_replace.replace_wait);
|
| 1362 | }
|
| 1363 |
|
| 1364 | void btrfs_bio_counter_inc_blocked(struct btrfs_fs_info *fs_info)
|
| 1365 | {
|
| 1366 | while (1) {
|
| 1367 | percpu_counter_inc(&fs_info->dev_replace.bio_counter);
|
| 1368 | if (likely(!test_bit(BTRFS_FS_STATE_DEV_REPLACING,
|
| 1369 | &fs_info->fs_state)))
|
| 1370 | break;
|
| 1371 |
|
| 1372 | btrfs_bio_counter_dec(fs_info);
|
| 1373 | wait_event(fs_info->dev_replace.replace_wait,
|
| 1374 | !test_bit(BTRFS_FS_STATE_DEV_REPLACING,
|
| 1375 | &fs_info->fs_state));
|
| 1376 | }
|
| 1377 | }
|