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> |
| 9 | #include <linux/buffer_head.h> |
| 10 | #include <linux/blkdev.h> |
| 11 | #include <linux/random.h> |
| 12 | #include <linux/iocontext.h> |
| 13 | #include <linux/capability.h> |
| 14 | #include <linux/kthread.h> |
| 15 | #include <linux/math64.h> |
| 16 | #include <asm/div64.h> |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 17 | #include "ctree.h" |
| 18 | #include "extent_map.h" |
| 19 | #include "disk-io.h" |
| 20 | #include "transaction.h" |
| 21 | #include "print-tree.h" |
| 22 | #include "volumes.h" |
| 23 | #include "async-thread.h" |
| 24 | #include "check-integrity.h" |
| 25 | #include "rcu-string.h" |
| 26 | #include "dev-replace.h" |
Anand Jain | 49c6f73 | 2014-06-03 11:36:02 +0800 | [diff] [blame] | 27 | #include "sysfs.h" |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 28 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 29 | static int btrfs_dev_replace_finishing(struct btrfs_fs_info *fs_info, |
| 30 | int scrub_ret); |
| 31 | static void btrfs_dev_replace_update_device_in_mapping_tree( |
| 32 | struct btrfs_fs_info *fs_info, |
| 33 | struct btrfs_device *srcdev, |
| 34 | struct btrfs_device *tgtdev); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 35 | static int btrfs_dev_replace_kthread(void *data); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 36 | |
| 37 | int btrfs_init_dev_replace(struct btrfs_fs_info *fs_info) |
| 38 | { |
| 39 | struct btrfs_key key; |
| 40 | struct btrfs_root *dev_root = fs_info->dev_root; |
| 41 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; |
| 42 | struct extent_buffer *eb; |
| 43 | int slot; |
| 44 | int ret = 0; |
| 45 | struct btrfs_path *path = NULL; |
| 46 | int item_size; |
| 47 | struct btrfs_dev_replace_item *ptr; |
| 48 | u64 src_devid; |
| 49 | |
| 50 | path = btrfs_alloc_path(); |
| 51 | if (!path) { |
| 52 | ret = -ENOMEM; |
| 53 | goto out; |
| 54 | } |
| 55 | |
| 56 | key.objectid = 0; |
| 57 | key.type = BTRFS_DEV_REPLACE_KEY; |
| 58 | key.offset = 0; |
| 59 | ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0); |
| 60 | if (ret) { |
| 61 | no_valid_dev_replace_entry_found: |
| 62 | ret = 0; |
| 63 | dev_replace->replace_state = |
| 64 | BTRFS_DEV_REPLACE_ITEM_STATE_NEVER_STARTED; |
| 65 | dev_replace->cont_reading_from_srcdev_mode = |
| 66 | BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_ALWAYS; |
| 67 | dev_replace->replace_state = 0; |
| 68 | dev_replace->time_started = 0; |
| 69 | dev_replace->time_stopped = 0; |
| 70 | atomic64_set(&dev_replace->num_write_errors, 0); |
| 71 | atomic64_set(&dev_replace->num_uncorrectable_read_errors, 0); |
| 72 | dev_replace->cursor_left = 0; |
| 73 | dev_replace->committed_cursor_left = 0; |
| 74 | dev_replace->cursor_left_last_write_of_item = 0; |
| 75 | dev_replace->cursor_right = 0; |
| 76 | dev_replace->srcdev = NULL; |
| 77 | dev_replace->tgtdev = NULL; |
| 78 | dev_replace->is_valid = 0; |
| 79 | dev_replace->item_needs_writeback = 0; |
| 80 | goto out; |
| 81 | } |
| 82 | slot = path->slots[0]; |
| 83 | eb = path->nodes[0]; |
| 84 | item_size = btrfs_item_size_nr(eb, slot); |
| 85 | ptr = btrfs_item_ptr(eb, slot, struct btrfs_dev_replace_item); |
| 86 | |
| 87 | if (item_size != sizeof(struct btrfs_dev_replace_item)) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 88 | btrfs_warn(fs_info, |
| 89 | "dev_replace entry found has unexpected size, ignore entry"); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 90 | goto no_valid_dev_replace_entry_found; |
| 91 | } |
| 92 | |
| 93 | src_devid = btrfs_dev_replace_src_devid(eb, ptr); |
| 94 | dev_replace->cont_reading_from_srcdev_mode = |
| 95 | btrfs_dev_replace_cont_reading_from_srcdev_mode(eb, ptr); |
| 96 | dev_replace->replace_state = btrfs_dev_replace_replace_state(eb, ptr); |
| 97 | dev_replace->time_started = btrfs_dev_replace_time_started(eb, ptr); |
| 98 | dev_replace->time_stopped = |
| 99 | btrfs_dev_replace_time_stopped(eb, ptr); |
| 100 | atomic64_set(&dev_replace->num_write_errors, |
| 101 | btrfs_dev_replace_num_write_errors(eb, ptr)); |
| 102 | atomic64_set(&dev_replace->num_uncorrectable_read_errors, |
| 103 | btrfs_dev_replace_num_uncorrectable_read_errors(eb, ptr)); |
| 104 | dev_replace->cursor_left = btrfs_dev_replace_cursor_left(eb, ptr); |
| 105 | dev_replace->committed_cursor_left = dev_replace->cursor_left; |
| 106 | dev_replace->cursor_left_last_write_of_item = dev_replace->cursor_left; |
| 107 | dev_replace->cursor_right = btrfs_dev_replace_cursor_right(eb, ptr); |
| 108 | dev_replace->is_valid = 1; |
| 109 | |
| 110 | dev_replace->item_needs_writeback = 0; |
| 111 | switch (dev_replace->replace_state) { |
| 112 | case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED: |
| 113 | case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED: |
| 114 | case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED: |
| 115 | dev_replace->srcdev = NULL; |
| 116 | dev_replace->tgtdev = NULL; |
| 117 | break; |
| 118 | case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED: |
| 119 | case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED: |
| 120 | dev_replace->srcdev = btrfs_find_device(fs_info, src_devid, |
| 121 | NULL, NULL); |
| 122 | dev_replace->tgtdev = btrfs_find_device(fs_info, |
| 123 | BTRFS_DEV_REPLACE_DEVID, |
| 124 | NULL, NULL); |
| 125 | /* |
| 126 | * allow 'btrfs dev replace_cancel' if src/tgt device is |
| 127 | * missing |
| 128 | */ |
| 129 | if (!dev_replace->srcdev && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 130 | !btrfs_test_opt(fs_info, DEGRADED)) { |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 131 | ret = -EIO; |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 132 | btrfs_warn(fs_info, |
| 133 | "cannot mount because device replace operation is ongoing and"); |
| 134 | btrfs_warn(fs_info, |
| 135 | "srcdev (devid %llu) is missing, need to run 'btrfs dev scan'?", |
| 136 | src_devid); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 137 | } |
| 138 | if (!dev_replace->tgtdev && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 139 | !btrfs_test_opt(fs_info, DEGRADED)) { |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 140 | ret = -EIO; |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 141 | btrfs_warn(fs_info, |
| 142 | "cannot mount because device replace operation is ongoing and"); |
| 143 | btrfs_warn(fs_info, |
| 144 | "tgtdev (devid %llu) is missing, need to run 'btrfs dev scan'?", |
Geert Uytterhoeven | 6e71c47 | 2013-08-20 13:20:08 +0200 | [diff] [blame] | 145 | BTRFS_DEV_REPLACE_DEVID); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 146 | } |
| 147 | if (dev_replace->tgtdev) { |
| 148 | if (dev_replace->srcdev) { |
| 149 | dev_replace->tgtdev->total_bytes = |
| 150 | dev_replace->srcdev->total_bytes; |
| 151 | dev_replace->tgtdev->disk_total_bytes = |
| 152 | dev_replace->srcdev->disk_total_bytes; |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 153 | dev_replace->tgtdev->commit_total_bytes = |
| 154 | dev_replace->srcdev->commit_total_bytes; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 155 | dev_replace->tgtdev->bytes_used = |
| 156 | dev_replace->srcdev->bytes_used; |
Miao Xie | ce7213c | 2014-09-03 21:35:34 +0800 | [diff] [blame] | 157 | dev_replace->tgtdev->commit_bytes_used = |
| 158 | dev_replace->srcdev->commit_bytes_used; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 159 | } |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 160 | set_bit(BTRFS_DEV_STATE_REPLACE_TGT, |
| 161 | &dev_replace->tgtdev->dev_state); |
Anand Jain | 15fc128 | 2018-02-12 23:36:25 +0800 | [diff] [blame] | 162 | |
| 163 | WARN_ON(fs_info->fs_devices->rw_devices == 0); |
| 164 | dev_replace->tgtdev->io_width = fs_info->sectorsize; |
| 165 | dev_replace->tgtdev->io_align = fs_info->sectorsize; |
| 166 | dev_replace->tgtdev->sector_size = fs_info->sectorsize; |
| 167 | dev_replace->tgtdev->fs_info = fs_info; |
| 168 | set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, |
| 169 | &dev_replace->tgtdev->dev_state); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 170 | } |
| 171 | break; |
| 172 | } |
| 173 | |
| 174 | out: |
Tsutomu Itoh | 527afb4 | 2015-08-19 14:55:00 +0900 | [diff] [blame] | 175 | btrfs_free_path(path); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 176 | return ret; |
| 177 | } |
| 178 | |
| 179 | /* |
| 180 | * called from commit_transaction. Writes changed device replace state to |
| 181 | * disk. |
| 182 | */ |
| 183 | int btrfs_run_dev_replace(struct btrfs_trans_handle *trans, |
| 184 | struct btrfs_fs_info *fs_info) |
| 185 | { |
| 186 | int ret; |
| 187 | struct btrfs_root *dev_root = fs_info->dev_root; |
| 188 | struct btrfs_path *path; |
| 189 | struct btrfs_key key; |
| 190 | struct extent_buffer *eb; |
| 191 | struct btrfs_dev_replace_item *ptr; |
| 192 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; |
| 193 | |
David Sterba | 7e79cb8 | 2018-03-24 02:11:38 +0100 | [diff] [blame] | 194 | btrfs_dev_replace_read_lock(dev_replace); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 195 | if (!dev_replace->is_valid || |
| 196 | !dev_replace->item_needs_writeback) { |
David Sterba | 7e79cb8 | 2018-03-24 02:11:38 +0100 | [diff] [blame] | 197 | btrfs_dev_replace_read_unlock(dev_replace); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 198 | return 0; |
| 199 | } |
David Sterba | 7e79cb8 | 2018-03-24 02:11:38 +0100 | [diff] [blame] | 200 | btrfs_dev_replace_read_unlock(dev_replace); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 201 | |
| 202 | key.objectid = 0; |
| 203 | key.type = BTRFS_DEV_REPLACE_KEY; |
| 204 | key.offset = 0; |
| 205 | |
| 206 | path = btrfs_alloc_path(); |
| 207 | if (!path) { |
| 208 | ret = -ENOMEM; |
| 209 | goto out; |
| 210 | } |
| 211 | ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1); |
| 212 | if (ret < 0) { |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 213 | btrfs_warn(fs_info, |
| 214 | "error %d while searching for dev_replace item!", |
| 215 | ret); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 216 | goto out; |
| 217 | } |
| 218 | |
| 219 | if (ret == 0 && |
| 220 | btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) { |
| 221 | /* |
| 222 | * need to delete old one and insert a new one. |
| 223 | * Since no attempt is made to recover any old state, if the |
| 224 | * dev_replace state is 'running', the data on the target |
| 225 | * drive is lost. |
| 226 | * It would be possible to recover the state: just make sure |
| 227 | * that the beginning of the item is never changed and always |
| 228 | * contains all the essential information. Then read this |
| 229 | * minimal set of information and use it as a base for the |
| 230 | * new state. |
| 231 | */ |
| 232 | ret = btrfs_del_item(trans, dev_root, path); |
| 233 | if (ret != 0) { |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 234 | btrfs_warn(fs_info, |
| 235 | "delete too small dev_replace item failed %d!", |
| 236 | ret); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 237 | goto out; |
| 238 | } |
| 239 | ret = 1; |
| 240 | } |
| 241 | |
| 242 | if (ret == 1) { |
| 243 | /* need to insert a new item */ |
| 244 | btrfs_release_path(path); |
| 245 | ret = btrfs_insert_empty_item(trans, dev_root, path, |
| 246 | &key, sizeof(*ptr)); |
| 247 | if (ret < 0) { |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 248 | btrfs_warn(fs_info, |
| 249 | "insert dev_replace item failed %d!", ret); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 250 | goto out; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | eb = path->nodes[0]; |
| 255 | ptr = btrfs_item_ptr(eb, path->slots[0], |
| 256 | struct btrfs_dev_replace_item); |
| 257 | |
David Sterba | 7e79cb8 | 2018-03-24 02:11:38 +0100 | [diff] [blame] | 258 | btrfs_dev_replace_write_lock(dev_replace); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 259 | if (dev_replace->srcdev) |
| 260 | btrfs_set_dev_replace_src_devid(eb, ptr, |
| 261 | dev_replace->srcdev->devid); |
| 262 | else |
| 263 | btrfs_set_dev_replace_src_devid(eb, ptr, (u64)-1); |
| 264 | btrfs_set_dev_replace_cont_reading_from_srcdev_mode(eb, ptr, |
| 265 | dev_replace->cont_reading_from_srcdev_mode); |
| 266 | btrfs_set_dev_replace_replace_state(eb, ptr, |
| 267 | dev_replace->replace_state); |
| 268 | btrfs_set_dev_replace_time_started(eb, ptr, dev_replace->time_started); |
| 269 | btrfs_set_dev_replace_time_stopped(eb, ptr, dev_replace->time_stopped); |
| 270 | btrfs_set_dev_replace_num_write_errors(eb, ptr, |
| 271 | atomic64_read(&dev_replace->num_write_errors)); |
| 272 | btrfs_set_dev_replace_num_uncorrectable_read_errors(eb, ptr, |
| 273 | atomic64_read(&dev_replace->num_uncorrectable_read_errors)); |
| 274 | dev_replace->cursor_left_last_write_of_item = |
| 275 | dev_replace->cursor_left; |
| 276 | btrfs_set_dev_replace_cursor_left(eb, ptr, |
| 277 | dev_replace->cursor_left_last_write_of_item); |
| 278 | btrfs_set_dev_replace_cursor_right(eb, ptr, |
| 279 | dev_replace->cursor_right); |
| 280 | dev_replace->item_needs_writeback = 0; |
David Sterba | 7e79cb8 | 2018-03-24 02:11:38 +0100 | [diff] [blame] | 281 | btrfs_dev_replace_write_unlock(dev_replace); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 282 | |
| 283 | btrfs_mark_buffer_dirty(eb); |
| 284 | |
| 285 | out: |
| 286 | btrfs_free_path(path); |
| 287 | |
| 288 | return ret; |
| 289 | } |
| 290 | |
| 291 | void btrfs_after_dev_replace_commit(struct btrfs_fs_info *fs_info) |
| 292 | { |
| 293 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; |
| 294 | |
| 295 | dev_replace->committed_cursor_left = |
| 296 | dev_replace->cursor_left_last_write_of_item; |
| 297 | } |
| 298 | |
Anand Jain | 3c958bd | 2017-11-28 10:43:10 +0800 | [diff] [blame] | 299 | static char* btrfs_dev_name(struct btrfs_device *device) |
| 300 | { |
Anand Jain | acf18c5 | 2018-02-24 19:43:56 +0800 | [diff] [blame] | 301 | if (!device || test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) |
Anand Jain | 3c958bd | 2017-11-28 10:43:10 +0800 | [diff] [blame] | 302 | return "<missing disk>"; |
| 303 | else |
| 304 | return rcu_str_deref(device->name); |
| 305 | } |
| 306 | |
David Sterba | da353f6 | 2017-02-14 17:55:53 +0100 | [diff] [blame] | 307 | int btrfs_dev_replace_start(struct btrfs_fs_info *fs_info, |
| 308 | const char *tgtdev_name, u64 srcdevid, const char *srcdev_name, |
| 309 | int read_src) |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 310 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 311 | struct btrfs_root *root = fs_info->dev_root; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 312 | struct btrfs_trans_handle *trans; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 313 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; |
| 314 | int ret; |
| 315 | struct btrfs_device *tgt_device = NULL; |
| 316 | struct btrfs_device *src_device = NULL; |
| 317 | |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 318 | /* the disk copy procedure reuses the scrub code */ |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 319 | mutex_lock(&fs_info->volume_mutex); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 320 | ret = btrfs_find_device_by_devspec(fs_info, srcdevid, |
Anand Jain | b525545 | 2016-03-24 18:48:14 +0800 | [diff] [blame] | 321 | srcdev_name, &src_device); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 322 | if (ret) { |
Miao Xie | 1c43366 | 2014-09-03 21:35:32 +0800 | [diff] [blame] | 323 | mutex_unlock(&fs_info->volume_mutex); |
| 324 | return ret; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 325 | } |
| 326 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 327 | ret = btrfs_init_dev_replace_tgtdev(fs_info, tgtdev_name, |
Miao Xie | 1c43366 | 2014-09-03 21:35:32 +0800 | [diff] [blame] | 328 | src_device, &tgt_device); |
| 329 | mutex_unlock(&fs_info->volume_mutex); |
| 330 | if (ret) |
| 331 | return ret; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 332 | |
Anand Jain | 9e271ae | 2015-08-14 18:33:02 +0800 | [diff] [blame] | 333 | /* |
| 334 | * Here we commit the transaction to make sure commit_total_bytes |
| 335 | * of all the devices are updated. |
| 336 | */ |
| 337 | trans = btrfs_attach_transaction(root); |
| 338 | if (!IS_ERR(trans)) { |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 339 | ret = btrfs_commit_transaction(trans); |
Anand Jain | 9e271ae | 2015-08-14 18:33:02 +0800 | [diff] [blame] | 340 | if (ret) |
| 341 | return ret; |
| 342 | } else if (PTR_ERR(trans) != -ENOENT) { |
| 343 | return PTR_ERR(trans); |
| 344 | } |
| 345 | |
David Sterba | 7e79cb8 | 2018-03-24 02:11:38 +0100 | [diff] [blame] | 346 | btrfs_dev_replace_write_lock(dev_replace); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 347 | switch (dev_replace->replace_state) { |
| 348 | case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED: |
| 349 | case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED: |
| 350 | case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED: |
| 351 | break; |
| 352 | case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED: |
| 353 | case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED: |
Anand Jain | b525545 | 2016-03-24 18:48:14 +0800 | [diff] [blame] | 354 | ret = BTRFS_IOCTL_DEV_REPLACE_RESULT_ALREADY_STARTED; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 355 | goto leave; |
| 356 | } |
| 357 | |
Anand Jain | b525545 | 2016-03-24 18:48:14 +0800 | [diff] [blame] | 358 | dev_replace->cont_reading_from_srcdev_mode = read_src; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 359 | WARN_ON(!src_device); |
| 360 | dev_replace->srcdev = src_device; |
| 361 | WARN_ON(!tgt_device); |
| 362 | dev_replace->tgtdev = tgt_device; |
| 363 | |
Anand Jain | fc23c24 | 2016-03-24 18:48:12 +0800 | [diff] [blame] | 364 | btrfs_info_in_rcu(fs_info, |
David Sterba | ecaeb14 | 2015-10-08 09:01:03 +0200 | [diff] [blame] | 365 | "dev_replace from %s (devid %llu) to %s started", |
Anand Jain | 3c958bd | 2017-11-28 10:43:10 +0800 | [diff] [blame] | 366 | btrfs_dev_name(src_device), |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 367 | src_device->devid, |
| 368 | rcu_str_deref(tgt_device->name)); |
| 369 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 370 | /* |
| 371 | * from now on, the writes to the srcdev are all duplicated to |
| 372 | * go to the tgtdev as well (refer to btrfs_map_block()). |
| 373 | */ |
| 374 | dev_replace->replace_state = BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED; |
Zach Brown | 4546bca | 2013-10-16 12:10:32 -0700 | [diff] [blame] | 375 | dev_replace->time_started = get_seconds(); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 376 | dev_replace->cursor_left = 0; |
| 377 | dev_replace->committed_cursor_left = 0; |
| 378 | dev_replace->cursor_left_last_write_of_item = 0; |
| 379 | dev_replace->cursor_right = 0; |
| 380 | dev_replace->is_valid = 1; |
| 381 | dev_replace->item_needs_writeback = 1; |
Yauhen Kharuzhy | 7ccefb9 | 2016-03-29 14:17:48 -0700 | [diff] [blame] | 382 | atomic64_set(&dev_replace->num_write_errors, 0); |
| 383 | atomic64_set(&dev_replace->num_uncorrectable_read_errors, 0); |
David Sterba | 7e79cb8 | 2018-03-24 02:11:38 +0100 | [diff] [blame] | 384 | btrfs_dev_replace_write_unlock(dev_replace); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 385 | |
Liu Bo | 73416da | 2015-08-14 18:33:07 +0800 | [diff] [blame] | 386 | ret = btrfs_sysfs_add_device_link(tgt_device->fs_devices, tgt_device); |
| 387 | if (ret) |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 388 | btrfs_err(fs_info, "kobj add dev failed %d", ret); |
Liu Bo | 73416da | 2015-08-14 18:33:07 +0800 | [diff] [blame] | 389 | |
Chris Mason | 6374e57a | 2017-06-23 09:48:21 -0700 | [diff] [blame] | 390 | btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 391 | |
| 392 | /* force writing the updated state information to disk */ |
| 393 | trans = btrfs_start_transaction(root, 0); |
| 394 | if (IS_ERR(trans)) { |
| 395 | ret = PTR_ERR(trans); |
David Sterba | 7e79cb8 | 2018-03-24 02:11:38 +0100 | [diff] [blame] | 396 | btrfs_dev_replace_write_lock(dev_replace); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 397 | goto leave; |
| 398 | } |
| 399 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 400 | ret = btrfs_commit_transaction(trans); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 401 | WARN_ON(ret); |
| 402 | |
| 403 | /* the disk copy procedure reuses the scrub code */ |
| 404 | ret = btrfs_scrub_dev(fs_info, src_device->devid, 0, |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 405 | btrfs_device_get_total_bytes(src_device), |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 406 | &dev_replace->scrub_progress, 0, 1); |
| 407 | |
Anand Jain | fc23c24 | 2016-03-24 18:48:12 +0800 | [diff] [blame] | 408 | ret = btrfs_dev_replace_finishing(fs_info, ret); |
Eryu Guan | 2fc9f6b | 2014-10-13 12:42:12 +0800 | [diff] [blame] | 409 | if (ret == -EINPROGRESS) { |
Anand Jain | b525545 | 2016-03-24 18:48:14 +0800 | [diff] [blame] | 410 | ret = BTRFS_IOCTL_DEV_REPLACE_RESULT_SCRUB_INPROGRESS; |
Eryu Guan | 2fc9f6b | 2014-10-13 12:42:12 +0800 | [diff] [blame] | 411 | } else { |
| 412 | WARN_ON(ret); |
| 413 | } |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 414 | |
Eryu Guan | 2fc9f6b | 2014-10-13 12:42:12 +0800 | [diff] [blame] | 415 | return ret; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 416 | |
| 417 | leave: |
| 418 | dev_replace->srcdev = NULL; |
| 419 | dev_replace->tgtdev = NULL; |
David Sterba | 7e79cb8 | 2018-03-24 02:11:38 +0100 | [diff] [blame] | 420 | btrfs_dev_replace_write_unlock(dev_replace); |
Miao Xie | 1c43366 | 2014-09-03 21:35:32 +0800 | [diff] [blame] | 421 | btrfs_destroy_dev_replace_tgtdev(fs_info, tgt_device); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 422 | return ret; |
| 423 | } |
| 424 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 425 | int btrfs_dev_replace_by_ioctl(struct btrfs_fs_info *fs_info, |
Anand Jain | b525545 | 2016-03-24 18:48:14 +0800 | [diff] [blame] | 426 | struct btrfs_ioctl_dev_replace_args *args) |
| 427 | { |
| 428 | int ret; |
| 429 | |
| 430 | switch (args->start.cont_reading_from_srcdev_mode) { |
| 431 | case BTRFS_IOCTL_DEV_REPLACE_CONT_READING_FROM_SRCDEV_MODE_ALWAYS: |
| 432 | case BTRFS_IOCTL_DEV_REPLACE_CONT_READING_FROM_SRCDEV_MODE_AVOID: |
| 433 | break; |
| 434 | default: |
| 435 | return -EINVAL; |
| 436 | } |
| 437 | |
| 438 | if ((args->start.srcdevid == 0 && args->start.srcdev_name[0] == '\0') || |
| 439 | args->start.tgtdev_name[0] == '\0') |
| 440 | return -EINVAL; |
| 441 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 442 | ret = btrfs_dev_replace_start(fs_info, args->start.tgtdev_name, |
Anand Jain | b525545 | 2016-03-24 18:48:14 +0800 | [diff] [blame] | 443 | args->start.srcdevid, |
| 444 | args->start.srcdev_name, |
| 445 | args->start.cont_reading_from_srcdev_mode); |
| 446 | args->result = ret; |
| 447 | /* don't warn if EINPROGRESS, someone else might be running scrub */ |
| 448 | if (ret == BTRFS_IOCTL_DEV_REPLACE_RESULT_SCRUB_INPROGRESS) |
| 449 | ret = 0; |
| 450 | |
| 451 | return ret; |
| 452 | } |
| 453 | |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 454 | /* |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 455 | * blocked until all in-flight bios operations are finished. |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 456 | */ |
| 457 | static void btrfs_rm_dev_replace_blocked(struct btrfs_fs_info *fs_info) |
| 458 | { |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 459 | set_bit(BTRFS_FS_STATE_DEV_REPLACING, &fs_info->fs_state); |
Zhao Lei | 7653947 | 2015-01-20 15:11:38 +0800 | [diff] [blame] | 460 | wait_event(fs_info->replace_wait, !percpu_counter_sum( |
| 461 | &fs_info->bio_counter)); |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | /* |
| 465 | * we have removed target device, it is safe to allow new bios request. |
| 466 | */ |
| 467 | static void btrfs_rm_dev_replace_unblocked(struct btrfs_fs_info *fs_info) |
| 468 | { |
| 469 | clear_bit(BTRFS_FS_STATE_DEV_REPLACING, &fs_info->fs_state); |
David Sterba | 730d9ec | 2015-10-10 18:16:38 +0200 | [diff] [blame] | 470 | wake_up(&fs_info->replace_wait); |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 471 | } |
| 472 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 473 | static int btrfs_dev_replace_finishing(struct btrfs_fs_info *fs_info, |
| 474 | int scrub_ret) |
| 475 | { |
| 476 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; |
| 477 | struct btrfs_device *tgt_device; |
| 478 | struct btrfs_device *src_device; |
| 479 | struct btrfs_root *root = fs_info->tree_root; |
| 480 | u8 uuid_tmp[BTRFS_UUID_SIZE]; |
| 481 | struct btrfs_trans_handle *trans; |
| 482 | int ret = 0; |
| 483 | |
| 484 | /* don't allow cancel or unmount to disturb the finishing procedure */ |
| 485 | mutex_lock(&dev_replace->lock_finishing_cancel_unmount); |
| 486 | |
David Sterba | 7e79cb8 | 2018-03-24 02:11:38 +0100 | [diff] [blame] | 487 | btrfs_dev_replace_read_lock(dev_replace); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 488 | /* was the operation canceled, or is it finished? */ |
| 489 | if (dev_replace->replace_state != |
| 490 | BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED) { |
David Sterba | 7e79cb8 | 2018-03-24 02:11:38 +0100 | [diff] [blame] | 491 | btrfs_dev_replace_read_unlock(dev_replace); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 492 | mutex_unlock(&dev_replace->lock_finishing_cancel_unmount); |
| 493 | return 0; |
| 494 | } |
| 495 | |
| 496 | tgt_device = dev_replace->tgtdev; |
| 497 | src_device = dev_replace->srcdev; |
David Sterba | 7e79cb8 | 2018-03-24 02:11:38 +0100 | [diff] [blame] | 498 | btrfs_dev_replace_read_unlock(dev_replace); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 499 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 500 | /* |
| 501 | * flush all outstanding I/O and inode extent mappings before the |
| 502 | * copy operation is declared as being finished |
| 503 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 504 | ret = btrfs_start_delalloc_roots(fs_info, 0, -1); |
Miao Xie | 3edb2a6 | 2013-01-22 10:49:33 +0000 | [diff] [blame] | 505 | if (ret) { |
| 506 | mutex_unlock(&dev_replace->lock_finishing_cancel_unmount); |
| 507 | return ret; |
| 508 | } |
Chris Mason | 6374e57a | 2017-06-23 09:48:21 -0700 | [diff] [blame] | 509 | btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 510 | |
| 511 | trans = btrfs_start_transaction(root, 0); |
| 512 | if (IS_ERR(trans)) { |
| 513 | mutex_unlock(&dev_replace->lock_finishing_cancel_unmount); |
| 514 | return PTR_ERR(trans); |
| 515 | } |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 516 | ret = btrfs_commit_transaction(trans); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 517 | WARN_ON(ret); |
| 518 | |
Miao Xie | 67a2c45 | 2014-09-03 21:35:43 +0800 | [diff] [blame] | 519 | mutex_lock(&uuid_mutex); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 520 | /* keep away write_all_supers() during the finishing procedure */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 521 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
| 522 | mutex_lock(&fs_info->chunk_mutex); |
David Sterba | 7e79cb8 | 2018-03-24 02:11:38 +0100 | [diff] [blame] | 523 | btrfs_dev_replace_write_lock(dev_replace); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 524 | dev_replace->replace_state = |
| 525 | scrub_ret ? BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED |
| 526 | : BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED; |
| 527 | dev_replace->tgtdev = NULL; |
| 528 | dev_replace->srcdev = NULL; |
Zach Brown | 4546bca | 2013-10-16 12:10:32 -0700 | [diff] [blame] | 529 | dev_replace->time_stopped = get_seconds(); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 530 | dev_replace->item_needs_writeback = 1; |
| 531 | |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 532 | /* replace old device with new one in mapping tree */ |
| 533 | if (!scrub_ret) { |
| 534 | btrfs_dev_replace_update_device_in_mapping_tree(fs_info, |
| 535 | src_device, |
| 536 | tgt_device); |
| 537 | } else { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 538 | btrfs_err_in_rcu(fs_info, |
| 539 | "btrfs_scrub_dev(%s, %llu, %s) failed %d", |
Anand Jain | 3c958bd | 2017-11-28 10:43:10 +0800 | [diff] [blame] | 540 | btrfs_dev_name(src_device), |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 541 | src_device->devid, |
| 542 | rcu_str_deref(tgt_device->name), scrub_ret); |
David Sterba | 7e79cb8 | 2018-03-24 02:11:38 +0100 | [diff] [blame] | 543 | btrfs_dev_replace_write_unlock(dev_replace); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 544 | mutex_unlock(&fs_info->chunk_mutex); |
| 545 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
Miao Xie | 67a2c45 | 2014-09-03 21:35:43 +0800 | [diff] [blame] | 546 | mutex_unlock(&uuid_mutex); |
Qu Wenruo | ae6529c | 2017-03-29 09:33:21 +0800 | [diff] [blame] | 547 | btrfs_rm_dev_replace_blocked(fs_info); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 548 | if (tgt_device) |
| 549 | btrfs_destroy_dev_replace_tgtdev(fs_info, tgt_device); |
Qu Wenruo | ae6529c | 2017-03-29 09:33:21 +0800 | [diff] [blame] | 550 | btrfs_rm_dev_replace_unblocked(fs_info); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 551 | mutex_unlock(&dev_replace->lock_finishing_cancel_unmount); |
| 552 | |
Eryu Guan | 2fc9f6b | 2014-10-13 12:42:12 +0800 | [diff] [blame] | 553 | return scrub_ret; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 554 | } |
| 555 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 556 | btrfs_info_in_rcu(fs_info, |
| 557 | "dev_replace from %s (devid %llu) to %s finished", |
Anand Jain | 3c958bd | 2017-11-28 10:43:10 +0800 | [diff] [blame] | 558 | btrfs_dev_name(src_device), |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 559 | src_device->devid, |
| 560 | rcu_str_deref(tgt_device->name)); |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 561 | clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &tgt_device->dev_state); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 562 | tgt_device->devid = src_device->devid; |
| 563 | src_device->devid = BTRFS_DEV_REPLACE_DEVID; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 564 | memcpy(uuid_tmp, tgt_device->uuid, sizeof(uuid_tmp)); |
| 565 | memcpy(tgt_device->uuid, src_device->uuid, sizeof(tgt_device->uuid)); |
| 566 | memcpy(src_device->uuid, uuid_tmp, sizeof(src_device->uuid)); |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 567 | btrfs_device_set_total_bytes(tgt_device, src_device->total_bytes); |
| 568 | btrfs_device_set_disk_total_bytes(tgt_device, |
| 569 | src_device->disk_total_bytes); |
| 570 | btrfs_device_set_bytes_used(tgt_device, src_device->bytes_used); |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 571 | ASSERT(list_empty(&src_device->resized_list)); |
| 572 | tgt_device->commit_total_bytes = src_device->commit_total_bytes; |
Miao Xie | ce7213c | 2014-09-03 21:35:34 +0800 | [diff] [blame] | 573 | tgt_device->commit_bytes_used = src_device->bytes_used; |
Anand Jain | 88acff6 | 2016-05-03 17:44:43 +0800 | [diff] [blame] | 574 | |
| 575 | btrfs_assign_next_active_device(fs_info, src_device, tgt_device); |
| 576 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 577 | 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] | 578 | fs_info->fs_devices->rw_devices++; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 579 | |
David Sterba | 7e79cb8 | 2018-03-24 02:11:38 +0100 | [diff] [blame] | 580 | btrfs_dev_replace_write_unlock(dev_replace); |
Qu Wenruo | 12b894c | 2014-08-20 16:10:15 +0800 | [diff] [blame] | 581 | |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 582 | btrfs_rm_dev_replace_blocked(fs_info); |
| 583 | |
Qu Wenruo | 084b6e7c | 2014-10-30 16:52:31 +0800 | [diff] [blame] | 584 | btrfs_rm_dev_replace_remove_srcdev(fs_info, src_device); |
Ilya Dryomov | 1357272 | 2013-10-02 20:41:01 +0300 | [diff] [blame] | 585 | |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 586 | btrfs_rm_dev_replace_unblocked(fs_info); |
| 587 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 588 | /* |
| 589 | * this is again a consistent state where no dev_replace procedure |
| 590 | * is running, the target device is part of the filesystem, the |
| 591 | * source device is not part of the filesystem anymore and its 1st |
| 592 | * superblock is scratched out so that it is no longer marked to |
| 593 | * belong to this filesystem. |
| 594 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 595 | mutex_unlock(&fs_info->chunk_mutex); |
| 596 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
Miao Xie | 67a2c45 | 2014-09-03 21:35:43 +0800 | [diff] [blame] | 597 | mutex_unlock(&uuid_mutex); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 598 | |
Qu Wenruo | 084b6e7c | 2014-10-30 16:52:31 +0800 | [diff] [blame] | 599 | /* replace the sysfs entry */ |
Anand Jain | 3257604 | 2015-08-14 18:32:49 +0800 | [diff] [blame] | 600 | btrfs_sysfs_rm_device_link(fs_info->fs_devices, src_device); |
Qu Wenruo | 084b6e7c | 2014-10-30 16:52:31 +0800 | [diff] [blame] | 601 | btrfs_rm_dev_replace_free_srcdev(fs_info, src_device); |
| 602 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 603 | /* write back the superblocks */ |
| 604 | trans = btrfs_start_transaction(root, 0); |
| 605 | if (!IS_ERR(trans)) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 606 | btrfs_commit_transaction(trans); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 607 | |
| 608 | mutex_unlock(&dev_replace->lock_finishing_cancel_unmount); |
| 609 | |
| 610 | return 0; |
| 611 | } |
| 612 | |
| 613 | static void btrfs_dev_replace_update_device_in_mapping_tree( |
| 614 | struct btrfs_fs_info *fs_info, |
| 615 | struct btrfs_device *srcdev, |
| 616 | struct btrfs_device *tgtdev) |
| 617 | { |
| 618 | struct extent_map_tree *em_tree = &fs_info->mapping_tree.map_tree; |
| 619 | struct extent_map *em; |
| 620 | struct map_lookup *map; |
| 621 | u64 start = 0; |
| 622 | int i; |
| 623 | |
| 624 | write_lock(&em_tree->lock); |
| 625 | do { |
| 626 | em = lookup_extent_mapping(em_tree, start, (u64)-1); |
| 627 | if (!em) |
| 628 | break; |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 629 | map = em->map_lookup; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 630 | for (i = 0; i < map->num_stripes; i++) |
| 631 | if (srcdev == map->stripes[i].dev) |
| 632 | map->stripes[i].dev = tgtdev; |
| 633 | start = em->start + em->len; |
| 634 | free_extent_map(em); |
| 635 | } while (start); |
| 636 | write_unlock(&em_tree->lock); |
| 637 | } |
| 638 | |
David Sterba | 74b595f | 2017-06-14 16:24:56 +0200 | [diff] [blame] | 639 | /* |
| 640 | * Read progress of device replace status according to the state and last |
| 641 | * stored position. The value format is the same as for |
| 642 | * btrfs_dev_replace::progress_1000 |
| 643 | */ |
| 644 | static u64 btrfs_dev_replace_progress(struct btrfs_fs_info *fs_info) |
| 645 | { |
| 646 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; |
| 647 | u64 ret = 0; |
| 648 | |
| 649 | switch (dev_replace->replace_state) { |
| 650 | case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED: |
| 651 | case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED: |
| 652 | ret = 0; |
| 653 | break; |
| 654 | case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED: |
| 655 | ret = 1000; |
| 656 | break; |
| 657 | case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED: |
| 658 | case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED: |
| 659 | ret = div64_u64(dev_replace->cursor_left, |
| 660 | div_u64(btrfs_device_get_total_bytes( |
| 661 | dev_replace->srcdev), 1000)); |
| 662 | break; |
| 663 | } |
| 664 | |
| 665 | return ret; |
| 666 | } |
| 667 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 668 | void btrfs_dev_replace_status(struct btrfs_fs_info *fs_info, |
| 669 | struct btrfs_ioctl_dev_replace_args *args) |
| 670 | { |
| 671 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; |
| 672 | |
David Sterba | 7e79cb8 | 2018-03-24 02:11:38 +0100 | [diff] [blame] | 673 | btrfs_dev_replace_read_lock(dev_replace); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 674 | /* even if !dev_replace_is_valid, the values are good enough for |
| 675 | * the replace_status ioctl */ |
| 676 | args->result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR; |
| 677 | args->status.replace_state = dev_replace->replace_state; |
| 678 | args->status.time_started = dev_replace->time_started; |
| 679 | args->status.time_stopped = dev_replace->time_stopped; |
| 680 | args->status.num_write_errors = |
| 681 | atomic64_read(&dev_replace->num_write_errors); |
| 682 | args->status.num_uncorrectable_read_errors = |
| 683 | atomic64_read(&dev_replace->num_uncorrectable_read_errors); |
David Sterba | 74b595f | 2017-06-14 16:24:56 +0200 | [diff] [blame] | 684 | args->status.progress_1000 = btrfs_dev_replace_progress(fs_info); |
David Sterba | 7e79cb8 | 2018-03-24 02:11:38 +0100 | [diff] [blame] | 685 | btrfs_dev_replace_read_unlock(dev_replace); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 686 | } |
| 687 | |
Anand Jain | 18e67c7 | 2018-02-12 23:33:31 +0800 | [diff] [blame] | 688 | int btrfs_dev_replace_cancel(struct btrfs_fs_info *fs_info) |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 689 | { |
| 690 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; |
| 691 | struct btrfs_device *tgt_device = NULL; |
Anand Jain | 8f2ceaa | 2018-02-13 11:53:43 +0800 | [diff] [blame] | 692 | struct btrfs_device *src_device = NULL; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 693 | struct btrfs_trans_handle *trans; |
| 694 | struct btrfs_root *root = fs_info->tree_root; |
Anand Jain | 18e67c7 | 2018-02-12 23:33:31 +0800 | [diff] [blame] | 695 | int result; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 696 | int ret; |
| 697 | |
David Howells | bc98a42 | 2017-07-17 08:45:34 +0100 | [diff] [blame] | 698 | if (sb_rdonly(fs_info->sb)) |
Ilya Dryomov | e649e58 | 2013-10-10 20:40:21 +0300 | [diff] [blame] | 699 | return -EROFS; |
| 700 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 701 | mutex_lock(&dev_replace->lock_finishing_cancel_unmount); |
David Sterba | 7e79cb8 | 2018-03-24 02:11:38 +0100 | [diff] [blame] | 702 | btrfs_dev_replace_write_lock(dev_replace); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 703 | switch (dev_replace->replace_state) { |
| 704 | case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED: |
| 705 | case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED: |
| 706 | case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED: |
| 707 | result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NOT_STARTED; |
David Sterba | 7e79cb8 | 2018-03-24 02:11:38 +0100 | [diff] [blame] | 708 | btrfs_dev_replace_write_unlock(dev_replace); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 709 | goto leave; |
| 710 | case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED: |
| 711 | case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED: |
| 712 | result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR; |
| 713 | tgt_device = dev_replace->tgtdev; |
Anand Jain | 8f2ceaa | 2018-02-13 11:53:43 +0800 | [diff] [blame] | 714 | src_device = dev_replace->srcdev; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 715 | dev_replace->tgtdev = NULL; |
| 716 | dev_replace->srcdev = NULL; |
| 717 | break; |
| 718 | } |
| 719 | dev_replace->replace_state = BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED; |
Zach Brown | 4546bca | 2013-10-16 12:10:32 -0700 | [diff] [blame] | 720 | dev_replace->time_stopped = get_seconds(); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 721 | dev_replace->item_needs_writeback = 1; |
David Sterba | 7e79cb8 | 2018-03-24 02:11:38 +0100 | [diff] [blame] | 722 | btrfs_dev_replace_write_unlock(dev_replace); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 723 | btrfs_scrub_cancel(fs_info); |
| 724 | |
| 725 | trans = btrfs_start_transaction(root, 0); |
| 726 | if (IS_ERR(trans)) { |
| 727 | mutex_unlock(&dev_replace->lock_finishing_cancel_unmount); |
| 728 | return PTR_ERR(trans); |
| 729 | } |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 730 | ret = btrfs_commit_transaction(trans); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 731 | WARN_ON(ret); |
Anand Jain | 8f2ceaa | 2018-02-13 11:53:43 +0800 | [diff] [blame] | 732 | |
| 733 | btrfs_info_in_rcu(fs_info, |
| 734 | "dev_replace from %s (devid %llu) to %s canceled", |
| 735 | btrfs_dev_name(src_device), src_device->devid, |
| 736 | btrfs_dev_name(tgt_device)); |
| 737 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 738 | if (tgt_device) |
| 739 | btrfs_destroy_dev_replace_tgtdev(fs_info, tgt_device); |
| 740 | |
| 741 | leave: |
| 742 | mutex_unlock(&dev_replace->lock_finishing_cancel_unmount); |
| 743 | return result; |
| 744 | } |
| 745 | |
| 746 | void btrfs_dev_replace_suspend_for_unmount(struct btrfs_fs_info *fs_info) |
| 747 | { |
| 748 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; |
| 749 | |
| 750 | mutex_lock(&dev_replace->lock_finishing_cancel_unmount); |
David Sterba | 7e79cb8 | 2018-03-24 02:11:38 +0100 | [diff] [blame] | 751 | btrfs_dev_replace_write_lock(dev_replace); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 752 | switch (dev_replace->replace_state) { |
| 753 | case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED: |
| 754 | case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED: |
| 755 | case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED: |
| 756 | case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED: |
| 757 | break; |
| 758 | case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED: |
| 759 | dev_replace->replace_state = |
| 760 | BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED; |
Zach Brown | 4546bca | 2013-10-16 12:10:32 -0700 | [diff] [blame] | 761 | dev_replace->time_stopped = get_seconds(); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 762 | dev_replace->item_needs_writeback = 1; |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 763 | btrfs_info(fs_info, "suspending dev_replace for unmount"); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 764 | break; |
| 765 | } |
| 766 | |
David Sterba | 7e79cb8 | 2018-03-24 02:11:38 +0100 | [diff] [blame] | 767 | btrfs_dev_replace_write_unlock(dev_replace); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 768 | mutex_unlock(&dev_replace->lock_finishing_cancel_unmount); |
| 769 | } |
| 770 | |
| 771 | /* resume dev_replace procedure that was interrupted by unmount */ |
| 772 | int btrfs_resume_dev_replace_async(struct btrfs_fs_info *fs_info) |
| 773 | { |
| 774 | struct task_struct *task; |
| 775 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; |
| 776 | |
David Sterba | 7e79cb8 | 2018-03-24 02:11:38 +0100 | [diff] [blame] | 777 | btrfs_dev_replace_write_lock(dev_replace); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 778 | switch (dev_replace->replace_state) { |
| 779 | case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED: |
| 780 | case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED: |
| 781 | case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED: |
David Sterba | 7e79cb8 | 2018-03-24 02:11:38 +0100 | [diff] [blame] | 782 | btrfs_dev_replace_write_unlock(dev_replace); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 783 | return 0; |
| 784 | case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED: |
| 785 | break; |
| 786 | case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED: |
| 787 | dev_replace->replace_state = |
| 788 | BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED; |
| 789 | break; |
| 790 | } |
| 791 | if (!dev_replace->tgtdev || !dev_replace->tgtdev->bdev) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 792 | btrfs_info(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 793 | "cannot continue dev_replace, tgtdev is missing"); |
| 794 | btrfs_info(fs_info, |
| 795 | "you may cancel the operation after 'mount -o degraded'"); |
David Sterba | 7e79cb8 | 2018-03-24 02:11:38 +0100 | [diff] [blame] | 796 | btrfs_dev_replace_write_unlock(dev_replace); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 797 | return 0; |
| 798 | } |
David Sterba | 7e79cb8 | 2018-03-24 02:11:38 +0100 | [diff] [blame] | 799 | btrfs_dev_replace_write_unlock(dev_replace); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 800 | |
David Sterba | 171938e | 2017-03-28 14:44:21 +0200 | [diff] [blame] | 801 | WARN_ON(test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 802 | task = kthread_run(btrfs_dev_replace_kthread, fs_info, "btrfs-devrepl"); |
Rusty Russell | 8c6ffba | 2013-07-15 11:20:32 +0930 | [diff] [blame] | 803 | return PTR_ERR_OR_ZERO(task); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | static int btrfs_dev_replace_kthread(void *data) |
| 807 | { |
| 808 | struct btrfs_fs_info *fs_info = data; |
| 809 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 810 | u64 progress; |
David Sterba | 00251a5 | 2018-03-20 15:35:50 +0100 | [diff] [blame^] | 811 | int ret; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 812 | |
David Sterba | f1b8a1e | 2017-06-14 16:28:42 +0200 | [diff] [blame] | 813 | progress = btrfs_dev_replace_progress(fs_info); |
| 814 | progress = div_u64(progress, 10); |
| 815 | btrfs_info_in_rcu(fs_info, |
Anand Jain | 3c958bd | 2017-11-28 10:43:10 +0800 | [diff] [blame] | 816 | "continuing dev_replace from %s (devid %llu) to target %s @%u%%", |
| 817 | btrfs_dev_name(dev_replace->srcdev), |
David Sterba | f1b8a1e | 2017-06-14 16:28:42 +0200 | [diff] [blame] | 818 | dev_replace->srcdev->devid, |
Anand Jain | 3c958bd | 2017-11-28 10:43:10 +0800 | [diff] [blame] | 819 | btrfs_dev_name(dev_replace->tgtdev), |
David Sterba | f1b8a1e | 2017-06-14 16:28:42 +0200 | [diff] [blame] | 820 | (unsigned int)progress); |
| 821 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 822 | ret = btrfs_scrub_dev(fs_info, dev_replace->srcdev->devid, |
| 823 | dev_replace->committed_cursor_left, |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 824 | btrfs_device_get_total_bytes(dev_replace->srcdev), |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 825 | &dev_replace->scrub_progress, 0, 1); |
| 826 | ret = btrfs_dev_replace_finishing(fs_info, ret); |
| 827 | WARN_ON(ret); |
David Sterba | 00251a5 | 2018-03-20 15:35:50 +0100 | [diff] [blame^] | 828 | |
| 829 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 830 | return 0; |
| 831 | } |
| 832 | |
| 833 | int btrfs_dev_replace_is_ongoing(struct btrfs_dev_replace *dev_replace) |
| 834 | { |
| 835 | if (!dev_replace->is_valid) |
| 836 | return 0; |
| 837 | |
| 838 | switch (dev_replace->replace_state) { |
| 839 | case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED: |
| 840 | case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED: |
| 841 | case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED: |
| 842 | return 0; |
| 843 | case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED: |
| 844 | case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED: |
| 845 | /* |
| 846 | * return true even if tgtdev is missing (this is |
| 847 | * something that can happen if the dev_replace |
| 848 | * procedure is suspended by an umount and then |
| 849 | * the tgtdev is missing (or "btrfs dev scan") was |
| 850 | * not called and the the filesystem is remounted |
| 851 | * in degraded state. This does not stop the |
| 852 | * dev_replace procedure. It needs to be canceled |
Adam Buchbinder | bb7ab3b | 2016-03-04 11:23:12 -0800 | [diff] [blame] | 853 | * manually if the cancellation is wanted. |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 854 | */ |
| 855 | break; |
| 856 | } |
| 857 | return 1; |
| 858 | } |
| 859 | |
David Sterba | 7e79cb8 | 2018-03-24 02:11:38 +0100 | [diff] [blame] | 860 | void btrfs_dev_replace_read_lock(struct btrfs_dev_replace *dev_replace) |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 861 | { |
David Sterba | 7e79cb8 | 2018-03-24 02:11:38 +0100 | [diff] [blame] | 862 | read_lock(&dev_replace->lock); |
| 863 | atomic_inc(&dev_replace->read_locks); |
| 864 | } |
| 865 | |
| 866 | void btrfs_dev_replace_read_unlock(struct btrfs_dev_replace *dev_replace) |
| 867 | { |
| 868 | ASSERT(atomic_read(&dev_replace->read_locks) > 0); |
| 869 | atomic_dec(&dev_replace->read_locks); |
| 870 | read_unlock(&dev_replace->lock); |
| 871 | } |
| 872 | |
| 873 | void btrfs_dev_replace_write_lock(struct btrfs_dev_replace *dev_replace) |
| 874 | { |
Liu Bo | 73beece | 2015-07-17 16:49:19 +0800 | [diff] [blame] | 875 | again: |
David Sterba | 7e79cb8 | 2018-03-24 02:11:38 +0100 | [diff] [blame] | 876 | wait_event(dev_replace->read_lock_wq, |
| 877 | atomic_read(&dev_replace->blocking_readers) == 0); |
| 878 | write_lock(&dev_replace->lock); |
| 879 | if (atomic_read(&dev_replace->blocking_readers)) { |
| 880 | write_unlock(&dev_replace->lock); |
| 881 | goto again; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 882 | } |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 883 | } |
| 884 | |
David Sterba | 7e79cb8 | 2018-03-24 02:11:38 +0100 | [diff] [blame] | 885 | void btrfs_dev_replace_write_unlock(struct btrfs_dev_replace *dev_replace) |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 886 | { |
David Sterba | 7e79cb8 | 2018-03-24 02:11:38 +0100 | [diff] [blame] | 887 | ASSERT(atomic_read(&dev_replace->blocking_readers) == 0); |
| 888 | write_unlock(&dev_replace->lock); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 889 | } |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 890 | |
Liu Bo | 73beece | 2015-07-17 16:49:19 +0800 | [diff] [blame] | 891 | /* inc blocking cnt and release read lock */ |
| 892 | void btrfs_dev_replace_set_lock_blocking( |
| 893 | struct btrfs_dev_replace *dev_replace) |
| 894 | { |
| 895 | /* only set blocking for read lock */ |
| 896 | ASSERT(atomic_read(&dev_replace->read_locks) > 0); |
| 897 | atomic_inc(&dev_replace->blocking_readers); |
| 898 | read_unlock(&dev_replace->lock); |
| 899 | } |
| 900 | |
| 901 | /* acquire read lock and dec blocking cnt */ |
| 902 | void btrfs_dev_replace_clear_lock_blocking( |
| 903 | struct btrfs_dev_replace *dev_replace) |
| 904 | { |
| 905 | /* only set blocking for read lock */ |
| 906 | ASSERT(atomic_read(&dev_replace->read_locks) > 0); |
| 907 | ASSERT(atomic_read(&dev_replace->blocking_readers) > 0); |
| 908 | read_lock(&dev_replace->lock); |
| 909 | if (atomic_dec_and_test(&dev_replace->blocking_readers) && |
| 910 | waitqueue_active(&dev_replace->read_lock_wq)) |
| 911 | wake_up(&dev_replace->read_lock_wq); |
| 912 | } |
| 913 | |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 914 | void btrfs_bio_counter_inc_noblocked(struct btrfs_fs_info *fs_info) |
| 915 | { |
| 916 | percpu_counter_inc(&fs_info->bio_counter); |
| 917 | } |
| 918 | |
Miao Xie | 4245215 | 2014-11-25 16:39:28 +0800 | [diff] [blame] | 919 | 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] | 920 | { |
Miao Xie | 4245215 | 2014-11-25 16:39:28 +0800 | [diff] [blame] | 921 | percpu_counter_sub(&fs_info->bio_counter, amount); |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 922 | |
| 923 | if (waitqueue_active(&fs_info->replace_wait)) |
| 924 | wake_up(&fs_info->replace_wait); |
| 925 | } |
| 926 | |
| 927 | void btrfs_bio_counter_inc_blocked(struct btrfs_fs_info *fs_info) |
| 928 | { |
Zhao Lei | 09dd7a0 | 2015-01-20 15:11:37 +0800 | [diff] [blame] | 929 | while (1) { |
| 930 | percpu_counter_inc(&fs_info->bio_counter); |
| 931 | if (likely(!test_bit(BTRFS_FS_STATE_DEV_REPLACING, |
| 932 | &fs_info->fs_state))) |
| 933 | break; |
| 934 | |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 935 | btrfs_bio_counter_dec(fs_info); |
| 936 | wait_event(fs_info->replace_wait, |
| 937 | !test_bit(BTRFS_FS_STATE_DEV_REPLACING, |
| 938 | &fs_info->fs_state)); |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 939 | } |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 940 | } |