dev-replace.c 39 KB

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