dev-replace.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950
  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. if (path)
  178. btrfs_free_path(path);
  179. return ret;
  180. }
  181. /*
  182. * called from commit_transaction. Writes changed device replace state to
  183. * disk.
  184. */
  185. int btrfs_run_dev_replace(struct btrfs_trans_handle *trans,
  186. struct btrfs_fs_info *fs_info)
  187. {
  188. int ret;
  189. struct btrfs_root *dev_root = fs_info->dev_root;
  190. struct btrfs_path *path;
  191. struct btrfs_key key;
  192. struct extent_buffer *eb;
  193. struct btrfs_dev_replace_item *ptr;
  194. struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
  195. btrfs_dev_replace_lock(dev_replace);
  196. if (!dev_replace->is_valid ||
  197. !dev_replace->item_needs_writeback) {
  198. btrfs_dev_replace_unlock(dev_replace);
  199. return 0;
  200. }
  201. btrfs_dev_replace_unlock(dev_replace);
  202. key.objectid = 0;
  203. key.type = BTRFS_DEV_REPLACE_KEY;
  204. key.offset = 0;
  205. path = btrfs_alloc_path();
  206. if (!path) {
  207. ret = -ENOMEM;
  208. goto out;
  209. }
  210. ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
  211. if (ret < 0) {
  212. btrfs_warn(fs_info, "error %d while searching for dev_replace item!",
  213. ret);
  214. goto out;
  215. }
  216. if (ret == 0 &&
  217. btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
  218. /*
  219. * need to delete old one and insert a new one.
  220. * Since no attempt is made to recover any old state, if the
  221. * dev_replace state is 'running', the data on the target
  222. * drive is lost.
  223. * It would be possible to recover the state: just make sure
  224. * that the beginning of the item is never changed and always
  225. * contains all the essential information. Then read this
  226. * minimal set of information and use it as a base for the
  227. * new state.
  228. */
  229. ret = btrfs_del_item(trans, dev_root, path);
  230. if (ret != 0) {
  231. btrfs_warn(fs_info, "delete too small dev_replace item failed %d!",
  232. ret);
  233. goto out;
  234. }
  235. ret = 1;
  236. }
  237. if (ret == 1) {
  238. /* need to insert a new item */
  239. btrfs_release_path(path);
  240. ret = btrfs_insert_empty_item(trans, dev_root, path,
  241. &key, sizeof(*ptr));
  242. if (ret < 0) {
  243. btrfs_warn(fs_info, "insert dev_replace item failed %d!",
  244. ret);
  245. goto out;
  246. }
  247. }
  248. eb = path->nodes[0];
  249. ptr = btrfs_item_ptr(eb, path->slots[0],
  250. struct btrfs_dev_replace_item);
  251. btrfs_dev_replace_lock(dev_replace);
  252. if (dev_replace->srcdev)
  253. btrfs_set_dev_replace_src_devid(eb, ptr,
  254. dev_replace->srcdev->devid);
  255. else
  256. btrfs_set_dev_replace_src_devid(eb, ptr, (u64)-1);
  257. btrfs_set_dev_replace_cont_reading_from_srcdev_mode(eb, ptr,
  258. dev_replace->cont_reading_from_srcdev_mode);
  259. btrfs_set_dev_replace_replace_state(eb, ptr,
  260. dev_replace->replace_state);
  261. btrfs_set_dev_replace_time_started(eb, ptr, dev_replace->time_started);
  262. btrfs_set_dev_replace_time_stopped(eb, ptr, dev_replace->time_stopped);
  263. btrfs_set_dev_replace_num_write_errors(eb, ptr,
  264. atomic64_read(&dev_replace->num_write_errors));
  265. btrfs_set_dev_replace_num_uncorrectable_read_errors(eb, ptr,
  266. atomic64_read(&dev_replace->num_uncorrectable_read_errors));
  267. dev_replace->cursor_left_last_write_of_item =
  268. dev_replace->cursor_left;
  269. btrfs_set_dev_replace_cursor_left(eb, ptr,
  270. dev_replace->cursor_left_last_write_of_item);
  271. btrfs_set_dev_replace_cursor_right(eb, ptr,
  272. dev_replace->cursor_right);
  273. dev_replace->item_needs_writeback = 0;
  274. btrfs_dev_replace_unlock(dev_replace);
  275. btrfs_mark_buffer_dirty(eb);
  276. out:
  277. btrfs_free_path(path);
  278. return ret;
  279. }
  280. void btrfs_after_dev_replace_commit(struct btrfs_fs_info *fs_info)
  281. {
  282. struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
  283. dev_replace->committed_cursor_left =
  284. dev_replace->cursor_left_last_write_of_item;
  285. }
  286. int btrfs_dev_replace_start(struct btrfs_root *root,
  287. struct btrfs_ioctl_dev_replace_args *args)
  288. {
  289. struct btrfs_trans_handle *trans;
  290. struct btrfs_fs_info *fs_info = root->fs_info;
  291. struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
  292. int ret;
  293. struct btrfs_device *tgt_device = NULL;
  294. struct btrfs_device *src_device = NULL;
  295. if (btrfs_fs_incompat(fs_info, RAID56)) {
  296. btrfs_warn(fs_info, "dev_replace cannot yet handle RAID5/RAID6");
  297. return -EOPNOTSUPP;
  298. }
  299. switch (args->start.cont_reading_from_srcdev_mode) {
  300. case BTRFS_IOCTL_DEV_REPLACE_CONT_READING_FROM_SRCDEV_MODE_ALWAYS:
  301. case BTRFS_IOCTL_DEV_REPLACE_CONT_READING_FROM_SRCDEV_MODE_AVOID:
  302. break;
  303. default:
  304. return -EINVAL;
  305. }
  306. if ((args->start.srcdevid == 0 && args->start.srcdev_name[0] == '\0') ||
  307. args->start.tgtdev_name[0] == '\0')
  308. return -EINVAL;
  309. /*
  310. * Here we commit the transaction to make sure commit_total_bytes
  311. * of all the devices are updated.
  312. */
  313. trans = btrfs_attach_transaction(root);
  314. if (!IS_ERR(trans)) {
  315. ret = btrfs_commit_transaction(trans, root);
  316. if (ret)
  317. return ret;
  318. } else if (PTR_ERR(trans) != -ENOENT) {
  319. return PTR_ERR(trans);
  320. }
  321. /* the disk copy procedure reuses the scrub code */
  322. mutex_lock(&fs_info->volume_mutex);
  323. ret = btrfs_dev_replace_find_srcdev(root, args->start.srcdevid,
  324. args->start.srcdev_name,
  325. &src_device);
  326. if (ret) {
  327. mutex_unlock(&fs_info->volume_mutex);
  328. return ret;
  329. }
  330. ret = btrfs_init_dev_replace_tgtdev(root, args->start.tgtdev_name,
  331. src_device, &tgt_device);
  332. mutex_unlock(&fs_info->volume_mutex);
  333. if (ret)
  334. return ret;
  335. btrfs_dev_replace_lock(dev_replace);
  336. switch (dev_replace->replace_state) {
  337. case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
  338. case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
  339. case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
  340. break;
  341. case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
  342. case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
  343. args->result = BTRFS_IOCTL_DEV_REPLACE_RESULT_ALREADY_STARTED;
  344. goto leave;
  345. }
  346. dev_replace->cont_reading_from_srcdev_mode =
  347. args->start.cont_reading_from_srcdev_mode;
  348. WARN_ON(!src_device);
  349. dev_replace->srcdev = src_device;
  350. WARN_ON(!tgt_device);
  351. dev_replace->tgtdev = tgt_device;
  352. printk_in_rcu(KERN_INFO
  353. "BTRFS: dev_replace from %s (devid %llu) to %s started\n",
  354. src_device->missing ? "<missing disk>" :
  355. rcu_str_deref(src_device->name),
  356. src_device->devid,
  357. rcu_str_deref(tgt_device->name));
  358. /*
  359. * from now on, the writes to the srcdev are all duplicated to
  360. * go to the tgtdev as well (refer to btrfs_map_block()).
  361. */
  362. dev_replace->replace_state = BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED;
  363. dev_replace->time_started = get_seconds();
  364. dev_replace->cursor_left = 0;
  365. dev_replace->committed_cursor_left = 0;
  366. dev_replace->cursor_left_last_write_of_item = 0;
  367. dev_replace->cursor_right = 0;
  368. dev_replace->is_valid = 1;
  369. dev_replace->item_needs_writeback = 1;
  370. args->result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR;
  371. btrfs_dev_replace_unlock(dev_replace);
  372. btrfs_wait_ordered_roots(root->fs_info, -1);
  373. /* force writing the updated state information to disk */
  374. trans = btrfs_start_transaction(root, 0);
  375. if (IS_ERR(trans)) {
  376. ret = PTR_ERR(trans);
  377. btrfs_dev_replace_lock(dev_replace);
  378. goto leave;
  379. }
  380. ret = btrfs_commit_transaction(trans, root);
  381. WARN_ON(ret);
  382. /* the disk copy procedure reuses the scrub code */
  383. ret = btrfs_scrub_dev(fs_info, src_device->devid, 0,
  384. btrfs_device_get_total_bytes(src_device),
  385. &dev_replace->scrub_progress, 0, 1);
  386. ret = btrfs_dev_replace_finishing(root->fs_info, ret);
  387. /* don't warn if EINPROGRESS, someone else might be running scrub */
  388. if (ret == -EINPROGRESS) {
  389. args->result = BTRFS_IOCTL_DEV_REPLACE_RESULT_SCRUB_INPROGRESS;
  390. ret = 0;
  391. } else {
  392. WARN_ON(ret);
  393. }
  394. return ret;
  395. leave:
  396. dev_replace->srcdev = NULL;
  397. dev_replace->tgtdev = NULL;
  398. btrfs_dev_replace_unlock(dev_replace);
  399. btrfs_destroy_dev_replace_tgtdev(fs_info, tgt_device);
  400. return ret;
  401. }
  402. /*
  403. * blocked until all flighting bios are finished.
  404. */
  405. static void btrfs_rm_dev_replace_blocked(struct btrfs_fs_info *fs_info)
  406. {
  407. s64 writers;
  408. DEFINE_WAIT(wait);
  409. set_bit(BTRFS_FS_STATE_DEV_REPLACING, &fs_info->fs_state);
  410. do {
  411. prepare_to_wait(&fs_info->replace_wait, &wait,
  412. TASK_UNINTERRUPTIBLE);
  413. writers = percpu_counter_sum(&fs_info->bio_counter);
  414. if (writers)
  415. schedule();
  416. finish_wait(&fs_info->replace_wait, &wait);
  417. } while (writers);
  418. }
  419. /*
  420. * we have removed target device, it is safe to allow new bios request.
  421. */
  422. static void btrfs_rm_dev_replace_unblocked(struct btrfs_fs_info *fs_info)
  423. {
  424. clear_bit(BTRFS_FS_STATE_DEV_REPLACING, &fs_info->fs_state);
  425. if (waitqueue_active(&fs_info->replace_wait))
  426. wake_up(&fs_info->replace_wait);
  427. }
  428. static int btrfs_dev_replace_finishing(struct btrfs_fs_info *fs_info,
  429. int scrub_ret)
  430. {
  431. struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
  432. struct btrfs_device *tgt_device;
  433. struct btrfs_device *src_device;
  434. struct btrfs_root *root = fs_info->tree_root;
  435. u8 uuid_tmp[BTRFS_UUID_SIZE];
  436. struct btrfs_trans_handle *trans;
  437. int ret = 0;
  438. /* don't allow cancel or unmount to disturb the finishing procedure */
  439. mutex_lock(&dev_replace->lock_finishing_cancel_unmount);
  440. btrfs_dev_replace_lock(dev_replace);
  441. /* was the operation canceled, or is it finished? */
  442. if (dev_replace->replace_state !=
  443. BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED) {
  444. btrfs_dev_replace_unlock(dev_replace);
  445. mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
  446. return 0;
  447. }
  448. tgt_device = dev_replace->tgtdev;
  449. src_device = dev_replace->srcdev;
  450. btrfs_dev_replace_unlock(dev_replace);
  451. /*
  452. * flush all outstanding I/O and inode extent mappings before the
  453. * copy operation is declared as being finished
  454. */
  455. ret = btrfs_start_delalloc_roots(root->fs_info, 0, -1);
  456. if (ret) {
  457. mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
  458. return ret;
  459. }
  460. btrfs_wait_ordered_roots(root->fs_info, -1);
  461. trans = btrfs_start_transaction(root, 0);
  462. if (IS_ERR(trans)) {
  463. mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
  464. return PTR_ERR(trans);
  465. }
  466. ret = btrfs_commit_transaction(trans, root);
  467. WARN_ON(ret);
  468. mutex_lock(&uuid_mutex);
  469. /* keep away write_all_supers() during the finishing procedure */
  470. mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
  471. mutex_lock(&root->fs_info->chunk_mutex);
  472. btrfs_dev_replace_lock(dev_replace);
  473. dev_replace->replace_state =
  474. scrub_ret ? BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED
  475. : BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED;
  476. dev_replace->tgtdev = NULL;
  477. dev_replace->srcdev = NULL;
  478. dev_replace->time_stopped = get_seconds();
  479. dev_replace->item_needs_writeback = 1;
  480. /* replace old device with new one in mapping tree */
  481. if (!scrub_ret) {
  482. btrfs_dev_replace_update_device_in_mapping_tree(fs_info,
  483. src_device,
  484. tgt_device);
  485. } else {
  486. printk_in_rcu(KERN_ERR
  487. "BTRFS: btrfs_scrub_dev(%s, %llu, %s) failed %d\n",
  488. src_device->missing ? "<missing disk>" :
  489. rcu_str_deref(src_device->name),
  490. src_device->devid,
  491. rcu_str_deref(tgt_device->name), scrub_ret);
  492. btrfs_dev_replace_unlock(dev_replace);
  493. mutex_unlock(&root->fs_info->chunk_mutex);
  494. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  495. mutex_unlock(&uuid_mutex);
  496. if (tgt_device)
  497. btrfs_destroy_dev_replace_tgtdev(fs_info, tgt_device);
  498. mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
  499. return scrub_ret;
  500. }
  501. printk_in_rcu(KERN_INFO
  502. "BTRFS: dev_replace from %s (devid %llu) to %s finished\n",
  503. src_device->missing ? "<missing disk>" :
  504. rcu_str_deref(src_device->name),
  505. src_device->devid,
  506. rcu_str_deref(tgt_device->name));
  507. tgt_device->is_tgtdev_for_dev_replace = 0;
  508. tgt_device->devid = src_device->devid;
  509. src_device->devid = BTRFS_DEV_REPLACE_DEVID;
  510. memcpy(uuid_tmp, tgt_device->uuid, sizeof(uuid_tmp));
  511. memcpy(tgt_device->uuid, src_device->uuid, sizeof(tgt_device->uuid));
  512. memcpy(src_device->uuid, uuid_tmp, sizeof(src_device->uuid));
  513. btrfs_device_set_total_bytes(tgt_device, src_device->total_bytes);
  514. btrfs_device_set_disk_total_bytes(tgt_device,
  515. src_device->disk_total_bytes);
  516. btrfs_device_set_bytes_used(tgt_device, src_device->bytes_used);
  517. ASSERT(list_empty(&src_device->resized_list));
  518. tgt_device->commit_total_bytes = src_device->commit_total_bytes;
  519. tgt_device->commit_bytes_used = src_device->bytes_used;
  520. if (fs_info->sb->s_bdev == src_device->bdev)
  521. fs_info->sb->s_bdev = tgt_device->bdev;
  522. if (fs_info->fs_devices->latest_bdev == src_device->bdev)
  523. fs_info->fs_devices->latest_bdev = tgt_device->bdev;
  524. list_add(&tgt_device->dev_alloc_list, &fs_info->fs_devices->alloc_list);
  525. fs_info->fs_devices->rw_devices++;
  526. /* replace the sysfs entry */
  527. btrfs_kobj_rm_device(fs_info, src_device);
  528. btrfs_kobj_add_device(fs_info, tgt_device);
  529. btrfs_dev_replace_unlock(dev_replace);
  530. btrfs_rm_dev_replace_blocked(fs_info);
  531. btrfs_rm_dev_replace_srcdev(fs_info, src_device);
  532. btrfs_rm_dev_replace_unblocked(fs_info);
  533. /*
  534. * this is again a consistent state where no dev_replace procedure
  535. * is running, the target device is part of the filesystem, the
  536. * source device is not part of the filesystem anymore and its 1st
  537. * superblock is scratched out so that it is no longer marked to
  538. * belong to this filesystem.
  539. */
  540. mutex_unlock(&root->fs_info->chunk_mutex);
  541. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  542. mutex_unlock(&uuid_mutex);
  543. /* write back the superblocks */
  544. trans = btrfs_start_transaction(root, 0);
  545. if (!IS_ERR(trans))
  546. btrfs_commit_transaction(trans, root);
  547. mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
  548. return 0;
  549. }
  550. static void btrfs_dev_replace_update_device_in_mapping_tree(
  551. struct btrfs_fs_info *fs_info,
  552. struct btrfs_device *srcdev,
  553. struct btrfs_device *tgtdev)
  554. {
  555. struct extent_map_tree *em_tree = &fs_info->mapping_tree.map_tree;
  556. struct extent_map *em;
  557. struct map_lookup *map;
  558. u64 start = 0;
  559. int i;
  560. write_lock(&em_tree->lock);
  561. do {
  562. em = lookup_extent_mapping(em_tree, start, (u64)-1);
  563. if (!em)
  564. break;
  565. map = (struct map_lookup *)em->bdev;
  566. for (i = 0; i < map->num_stripes; i++)
  567. if (srcdev == map->stripes[i].dev)
  568. map->stripes[i].dev = tgtdev;
  569. start = em->start + em->len;
  570. free_extent_map(em);
  571. } while (start);
  572. write_unlock(&em_tree->lock);
  573. }
  574. static int btrfs_dev_replace_find_srcdev(struct btrfs_root *root, u64 srcdevid,
  575. char *srcdev_name,
  576. struct btrfs_device **device)
  577. {
  578. int ret;
  579. if (srcdevid) {
  580. ret = 0;
  581. *device = btrfs_find_device(root->fs_info, srcdevid, NULL,
  582. NULL);
  583. if (!*device)
  584. ret = -ENOENT;
  585. } else {
  586. ret = btrfs_find_device_missing_or_by_path(root, srcdev_name,
  587. device);
  588. }
  589. return ret;
  590. }
  591. void btrfs_dev_replace_status(struct btrfs_fs_info *fs_info,
  592. struct btrfs_ioctl_dev_replace_args *args)
  593. {
  594. struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
  595. struct btrfs_device *srcdev;
  596. btrfs_dev_replace_lock(dev_replace);
  597. /* even if !dev_replace_is_valid, the values are good enough for
  598. * the replace_status ioctl */
  599. args->result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR;
  600. args->status.replace_state = dev_replace->replace_state;
  601. args->status.time_started = dev_replace->time_started;
  602. args->status.time_stopped = dev_replace->time_stopped;
  603. args->status.num_write_errors =
  604. atomic64_read(&dev_replace->num_write_errors);
  605. args->status.num_uncorrectable_read_errors =
  606. atomic64_read(&dev_replace->num_uncorrectable_read_errors);
  607. switch (dev_replace->replace_state) {
  608. case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
  609. case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
  610. args->status.progress_1000 = 0;
  611. break;
  612. case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
  613. args->status.progress_1000 = 1000;
  614. break;
  615. case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
  616. case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
  617. srcdev = dev_replace->srcdev;
  618. args->status.progress_1000 = div64_u64(dev_replace->cursor_left,
  619. div64_u64(btrfs_device_get_total_bytes(srcdev), 1000));
  620. break;
  621. }
  622. btrfs_dev_replace_unlock(dev_replace);
  623. }
  624. int btrfs_dev_replace_cancel(struct btrfs_fs_info *fs_info,
  625. struct btrfs_ioctl_dev_replace_args *args)
  626. {
  627. args->result = __btrfs_dev_replace_cancel(fs_info);
  628. return 0;
  629. }
  630. static u64 __btrfs_dev_replace_cancel(struct btrfs_fs_info *fs_info)
  631. {
  632. struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
  633. struct btrfs_device *tgt_device = NULL;
  634. struct btrfs_trans_handle *trans;
  635. struct btrfs_root *root = fs_info->tree_root;
  636. u64 result;
  637. int ret;
  638. if (fs_info->sb->s_flags & MS_RDONLY)
  639. return -EROFS;
  640. mutex_lock(&dev_replace->lock_finishing_cancel_unmount);
  641. btrfs_dev_replace_lock(dev_replace);
  642. switch (dev_replace->replace_state) {
  643. case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
  644. case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
  645. case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
  646. result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NOT_STARTED;
  647. btrfs_dev_replace_unlock(dev_replace);
  648. goto leave;
  649. case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
  650. case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
  651. result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR;
  652. tgt_device = dev_replace->tgtdev;
  653. dev_replace->tgtdev = NULL;
  654. dev_replace->srcdev = NULL;
  655. break;
  656. }
  657. dev_replace->replace_state = BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED;
  658. dev_replace->time_stopped = get_seconds();
  659. dev_replace->item_needs_writeback = 1;
  660. btrfs_dev_replace_unlock(dev_replace);
  661. btrfs_scrub_cancel(fs_info);
  662. trans = btrfs_start_transaction(root, 0);
  663. if (IS_ERR(trans)) {
  664. mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
  665. return PTR_ERR(trans);
  666. }
  667. ret = btrfs_commit_transaction(trans, root);
  668. WARN_ON(ret);
  669. if (tgt_device)
  670. btrfs_destroy_dev_replace_tgtdev(fs_info, tgt_device);
  671. leave:
  672. mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
  673. return result;
  674. }
  675. void btrfs_dev_replace_suspend_for_unmount(struct btrfs_fs_info *fs_info)
  676. {
  677. struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
  678. mutex_lock(&dev_replace->lock_finishing_cancel_unmount);
  679. btrfs_dev_replace_lock(dev_replace);
  680. switch (dev_replace->replace_state) {
  681. case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
  682. case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
  683. case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
  684. case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
  685. break;
  686. case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
  687. dev_replace->replace_state =
  688. BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED;
  689. dev_replace->time_stopped = get_seconds();
  690. dev_replace->item_needs_writeback = 1;
  691. btrfs_info(fs_info, "suspending dev_replace for unmount");
  692. break;
  693. }
  694. btrfs_dev_replace_unlock(dev_replace);
  695. mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
  696. }
  697. /* resume dev_replace procedure that was interrupted by unmount */
  698. int btrfs_resume_dev_replace_async(struct btrfs_fs_info *fs_info)
  699. {
  700. struct task_struct *task;
  701. struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
  702. btrfs_dev_replace_lock(dev_replace);
  703. switch (dev_replace->replace_state) {
  704. case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
  705. case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
  706. case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
  707. btrfs_dev_replace_unlock(dev_replace);
  708. return 0;
  709. case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
  710. break;
  711. case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
  712. dev_replace->replace_state =
  713. BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED;
  714. break;
  715. }
  716. if (!dev_replace->tgtdev || !dev_replace->tgtdev->bdev) {
  717. btrfs_info(fs_info, "cannot continue dev_replace, tgtdev is missing");
  718. btrfs_info(fs_info,
  719. "you may cancel the operation after 'mount -o degraded'");
  720. btrfs_dev_replace_unlock(dev_replace);
  721. return 0;
  722. }
  723. btrfs_dev_replace_unlock(dev_replace);
  724. WARN_ON(atomic_xchg(
  725. &fs_info->mutually_exclusive_operation_running, 1));
  726. task = kthread_run(btrfs_dev_replace_kthread, fs_info, "btrfs-devrepl");
  727. return PTR_ERR_OR_ZERO(task);
  728. }
  729. static int btrfs_dev_replace_kthread(void *data)
  730. {
  731. struct btrfs_fs_info *fs_info = data;
  732. struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
  733. struct btrfs_ioctl_dev_replace_args *status_args;
  734. u64 progress;
  735. status_args = kzalloc(sizeof(*status_args), GFP_NOFS);
  736. if (status_args) {
  737. btrfs_dev_replace_status(fs_info, status_args);
  738. progress = status_args->status.progress_1000;
  739. kfree(status_args);
  740. do_div(progress, 10);
  741. printk_in_rcu(KERN_INFO
  742. "BTRFS: continuing dev_replace from %s (devid %llu) to %s @%u%%\n",
  743. dev_replace->srcdev->missing ? "<missing disk>" :
  744. rcu_str_deref(dev_replace->srcdev->name),
  745. dev_replace->srcdev->devid,
  746. dev_replace->tgtdev ?
  747. rcu_str_deref(dev_replace->tgtdev->name) :
  748. "<missing target disk>",
  749. (unsigned int)progress);
  750. }
  751. btrfs_dev_replace_continue_on_mount(fs_info);
  752. atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
  753. return 0;
  754. }
  755. static int btrfs_dev_replace_continue_on_mount(struct btrfs_fs_info *fs_info)
  756. {
  757. struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
  758. int ret;
  759. ret = btrfs_scrub_dev(fs_info, dev_replace->srcdev->devid,
  760. dev_replace->committed_cursor_left,
  761. btrfs_device_get_total_bytes(dev_replace->srcdev),
  762. &dev_replace->scrub_progress, 0, 1);
  763. ret = btrfs_dev_replace_finishing(fs_info, ret);
  764. WARN_ON(ret);
  765. return 0;
  766. }
  767. int btrfs_dev_replace_is_ongoing(struct btrfs_dev_replace *dev_replace)
  768. {
  769. if (!dev_replace->is_valid)
  770. return 0;
  771. switch (dev_replace->replace_state) {
  772. case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
  773. case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
  774. case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
  775. return 0;
  776. case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
  777. case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
  778. /*
  779. * return true even if tgtdev is missing (this is
  780. * something that can happen if the dev_replace
  781. * procedure is suspended by an umount and then
  782. * the tgtdev is missing (or "btrfs dev scan") was
  783. * not called and the the filesystem is remounted
  784. * in degraded state. This does not stop the
  785. * dev_replace procedure. It needs to be canceled
  786. * manually if the cancelation is wanted.
  787. */
  788. break;
  789. }
  790. return 1;
  791. }
  792. void btrfs_dev_replace_lock(struct btrfs_dev_replace *dev_replace)
  793. {
  794. /* the beginning is just an optimization for the typical case */
  795. if (atomic_read(&dev_replace->nesting_level) == 0) {
  796. acquire_lock:
  797. /* this is not a nested case where the same thread
  798. * is trying to acqurire the same lock twice */
  799. mutex_lock(&dev_replace->lock);
  800. mutex_lock(&dev_replace->lock_management_lock);
  801. dev_replace->lock_owner = current->pid;
  802. atomic_inc(&dev_replace->nesting_level);
  803. mutex_unlock(&dev_replace->lock_management_lock);
  804. return;
  805. }
  806. mutex_lock(&dev_replace->lock_management_lock);
  807. if (atomic_read(&dev_replace->nesting_level) > 0 &&
  808. dev_replace->lock_owner == current->pid) {
  809. WARN_ON(!mutex_is_locked(&dev_replace->lock));
  810. atomic_inc(&dev_replace->nesting_level);
  811. mutex_unlock(&dev_replace->lock_management_lock);
  812. return;
  813. }
  814. mutex_unlock(&dev_replace->lock_management_lock);
  815. goto acquire_lock;
  816. }
  817. void btrfs_dev_replace_unlock(struct btrfs_dev_replace *dev_replace)
  818. {
  819. WARN_ON(!mutex_is_locked(&dev_replace->lock));
  820. mutex_lock(&dev_replace->lock_management_lock);
  821. WARN_ON(atomic_read(&dev_replace->nesting_level) < 1);
  822. WARN_ON(dev_replace->lock_owner != current->pid);
  823. atomic_dec(&dev_replace->nesting_level);
  824. if (atomic_read(&dev_replace->nesting_level) == 0) {
  825. dev_replace->lock_owner = 0;
  826. mutex_unlock(&dev_replace->lock_management_lock);
  827. mutex_unlock(&dev_replace->lock);
  828. } else {
  829. mutex_unlock(&dev_replace->lock_management_lock);
  830. }
  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_dec(struct btrfs_fs_info *fs_info)
  837. {
  838. percpu_counter_dec(&fs_info->bio_counter);
  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. DEFINE_WAIT(wait);
  845. again:
  846. percpu_counter_inc(&fs_info->bio_counter);
  847. if (test_bit(BTRFS_FS_STATE_DEV_REPLACING, &fs_info->fs_state)) {
  848. btrfs_bio_counter_dec(fs_info);
  849. wait_event(fs_info->replace_wait,
  850. !test_bit(BTRFS_FS_STATE_DEV_REPLACING,
  851. &fs_info->fs_state));
  852. goto again;
  853. }
  854. }