dev-replace.c 34 KB

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