dev-replace.c 30 KB

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