dev-replace.c 29 KB

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