dev-replace.c 39 KB

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