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