dev-replace.c 27 KB

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