dev-replace.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  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 "compat.h"
  30. #include "ctree.h"
  31. #include "extent_map.h"
  32. #include "disk-io.h"
  33. #include "transaction.h"
  34. #include "print-tree.h"
  35. #include "volumes.h"
  36. #include "async-thread.h"
  37. #include "check-integrity.h"
  38. #include "rcu-string.h"
  39. #include "dev-replace.h"
  40. static u64 btrfs_get_seconds_since_1970(void);
  41. static int btrfs_dev_replace_finishing(struct btrfs_fs_info *fs_info,
  42. int scrub_ret);
  43. static void btrfs_dev_replace_update_device_in_mapping_tree(
  44. struct btrfs_fs_info *fs_info,
  45. struct btrfs_device *srcdev,
  46. struct btrfs_device *tgtdev);
  47. static int btrfs_dev_replace_find_srcdev(struct btrfs_root *root, u64 srcdevid,
  48. char *srcdev_name,
  49. struct btrfs_device **device);
  50. static u64 __btrfs_dev_replace_cancel(struct btrfs_fs_info *fs_info);
  51. static int btrfs_dev_replace_kthread(void *data);
  52. static int btrfs_dev_replace_continue_on_mount(struct btrfs_fs_info *fs_info);
  53. int btrfs_init_dev_replace(struct btrfs_fs_info *fs_info)
  54. {
  55. struct btrfs_key key;
  56. struct btrfs_root *dev_root = fs_info->dev_root;
  57. struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
  58. struct extent_buffer *eb;
  59. int slot;
  60. int ret = 0;
  61. struct btrfs_path *path = NULL;
  62. int item_size;
  63. struct btrfs_dev_replace_item *ptr;
  64. u64 src_devid;
  65. path = btrfs_alloc_path();
  66. if (!path) {
  67. ret = -ENOMEM;
  68. goto out;
  69. }
  70. key.objectid = 0;
  71. key.type = BTRFS_DEV_REPLACE_KEY;
  72. key.offset = 0;
  73. ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
  74. if (ret) {
  75. no_valid_dev_replace_entry_found:
  76. ret = 0;
  77. dev_replace->replace_state =
  78. BTRFS_DEV_REPLACE_ITEM_STATE_NEVER_STARTED;
  79. dev_replace->cont_reading_from_srcdev_mode =
  80. BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_ALWAYS;
  81. dev_replace->replace_state = 0;
  82. dev_replace->time_started = 0;
  83. dev_replace->time_stopped = 0;
  84. atomic64_set(&dev_replace->num_write_errors, 0);
  85. atomic64_set(&dev_replace->num_uncorrectable_read_errors, 0);
  86. dev_replace->cursor_left = 0;
  87. dev_replace->committed_cursor_left = 0;
  88. dev_replace->cursor_left_last_write_of_item = 0;
  89. dev_replace->cursor_right = 0;
  90. dev_replace->srcdev = NULL;
  91. dev_replace->tgtdev = NULL;
  92. dev_replace->is_valid = 0;
  93. dev_replace->item_needs_writeback = 0;
  94. goto out;
  95. }
  96. slot = path->slots[0];
  97. eb = path->nodes[0];
  98. item_size = btrfs_item_size_nr(eb, slot);
  99. ptr = btrfs_item_ptr(eb, slot, struct btrfs_dev_replace_item);
  100. if (item_size != sizeof(struct btrfs_dev_replace_item)) {
  101. pr_warn("btrfs: dev_replace entry found has unexpected size, ignore entry\n");
  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. pr_warn("btrfs: cannot mount because device replace operation is ongoing and\n" "srcdev (devid %llu) is missing, need to run 'btrfs dev scan'?\n",
  143. src_devid);
  144. }
  145. if (!dev_replace->tgtdev &&
  146. !btrfs_test_opt(dev_root, DEGRADED)) {
  147. ret = -EIO;
  148. pr_warn("btrfs: cannot mount because device replace operation is ongoing and\n" "tgtdev (devid %llu) is missing, need to run btrfs dev scan?\n",
  149. BTRFS_DEV_REPLACE_DEVID);
  150. }
  151. if (dev_replace->tgtdev) {
  152. if (dev_replace->srcdev) {
  153. dev_replace->tgtdev->total_bytes =
  154. dev_replace->srcdev->total_bytes;
  155. dev_replace->tgtdev->disk_total_bytes =
  156. dev_replace->srcdev->disk_total_bytes;
  157. dev_replace->tgtdev->bytes_used =
  158. dev_replace->srcdev->bytes_used;
  159. }
  160. dev_replace->tgtdev->is_tgtdev_for_dev_replace = 1;
  161. btrfs_init_dev_replace_tgtdev_for_resume(fs_info,
  162. dev_replace->tgtdev);
  163. }
  164. break;
  165. }
  166. out:
  167. if (path)
  168. btrfs_free_path(path);
  169. return ret;
  170. }
  171. /*
  172. * called from commit_transaction. Writes changed device replace state to
  173. * disk.
  174. */
  175. int btrfs_run_dev_replace(struct btrfs_trans_handle *trans,
  176. struct btrfs_fs_info *fs_info)
  177. {
  178. int ret;
  179. struct btrfs_root *dev_root = fs_info->dev_root;
  180. struct btrfs_path *path;
  181. struct btrfs_key key;
  182. struct extent_buffer *eb;
  183. struct btrfs_dev_replace_item *ptr;
  184. struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
  185. btrfs_dev_replace_lock(dev_replace);
  186. if (!dev_replace->is_valid ||
  187. !dev_replace->item_needs_writeback) {
  188. btrfs_dev_replace_unlock(dev_replace);
  189. return 0;
  190. }
  191. btrfs_dev_replace_unlock(dev_replace);
  192. key.objectid = 0;
  193. key.type = BTRFS_DEV_REPLACE_KEY;
  194. key.offset = 0;
  195. path = btrfs_alloc_path();
  196. if (!path) {
  197. ret = -ENOMEM;
  198. goto out;
  199. }
  200. ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
  201. if (ret < 0) {
  202. pr_warn("btrfs: error %d while searching for dev_replace item!\n",
  203. ret);
  204. goto out;
  205. }
  206. if (ret == 0 &&
  207. btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
  208. /*
  209. * need to delete old one and insert a new one.
  210. * Since no attempt is made to recover any old state, if the
  211. * dev_replace state is 'running', the data on the target
  212. * drive is lost.
  213. * It would be possible to recover the state: just make sure
  214. * that the beginning of the item is never changed and always
  215. * contains all the essential information. Then read this
  216. * minimal set of information and use it as a base for the
  217. * new state.
  218. */
  219. ret = btrfs_del_item(trans, dev_root, path);
  220. if (ret != 0) {
  221. pr_warn("btrfs: delete too small dev_replace item failed %d!\n",
  222. ret);
  223. goto out;
  224. }
  225. ret = 1;
  226. }
  227. if (ret == 1) {
  228. /* need to insert a new item */
  229. btrfs_release_path(path);
  230. ret = btrfs_insert_empty_item(trans, dev_root, path,
  231. &key, sizeof(*ptr));
  232. if (ret < 0) {
  233. pr_warn("btrfs: insert dev_replace item failed %d!\n",
  234. ret);
  235. goto out;
  236. }
  237. }
  238. eb = path->nodes[0];
  239. ptr = btrfs_item_ptr(eb, path->slots[0],
  240. struct btrfs_dev_replace_item);
  241. btrfs_dev_replace_lock(dev_replace);
  242. if (dev_replace->srcdev)
  243. btrfs_set_dev_replace_src_devid(eb, ptr,
  244. dev_replace->srcdev->devid);
  245. else
  246. btrfs_set_dev_replace_src_devid(eb, ptr, (u64)-1);
  247. btrfs_set_dev_replace_cont_reading_from_srcdev_mode(eb, ptr,
  248. dev_replace->cont_reading_from_srcdev_mode);
  249. btrfs_set_dev_replace_replace_state(eb, ptr,
  250. dev_replace->replace_state);
  251. btrfs_set_dev_replace_time_started(eb, ptr, dev_replace->time_started);
  252. btrfs_set_dev_replace_time_stopped(eb, ptr, dev_replace->time_stopped);
  253. btrfs_set_dev_replace_num_write_errors(eb, ptr,
  254. atomic64_read(&dev_replace->num_write_errors));
  255. btrfs_set_dev_replace_num_uncorrectable_read_errors(eb, ptr,
  256. atomic64_read(&dev_replace->num_uncorrectable_read_errors));
  257. dev_replace->cursor_left_last_write_of_item =
  258. dev_replace->cursor_left;
  259. btrfs_set_dev_replace_cursor_left(eb, ptr,
  260. dev_replace->cursor_left_last_write_of_item);
  261. btrfs_set_dev_replace_cursor_right(eb, ptr,
  262. dev_replace->cursor_right);
  263. dev_replace->item_needs_writeback = 0;
  264. btrfs_dev_replace_unlock(dev_replace);
  265. btrfs_mark_buffer_dirty(eb);
  266. out:
  267. btrfs_free_path(path);
  268. return ret;
  269. }
  270. void btrfs_after_dev_replace_commit(struct btrfs_fs_info *fs_info)
  271. {
  272. struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
  273. dev_replace->committed_cursor_left =
  274. dev_replace->cursor_left_last_write_of_item;
  275. }
  276. static u64 btrfs_get_seconds_since_1970(void)
  277. {
  278. struct timespec t = CURRENT_TIME_SEC;
  279. return t.tv_sec;
  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. pr_warn("btrfs: dev_replace cannot yet handle RAID5/RAID6\n");
  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. pr_err("btrfs: target device %s is invalid!\n",
  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. pr_err("btrfs: target device is smaller than source device!\n");
  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 = btrfs_get_seconds_since_1970();
  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_all_ordered_extents(root->fs_info);
  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_all_delalloc_inodes(root->fs_info, 0);
  425. if (ret) {
  426. mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
  427. return ret;
  428. }
  429. btrfs_wait_all_ordered_extents(root->fs_info);
  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 = btrfs_get_seconds_since_1970();
  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. if (src_device->bdev) {
  484. /* zero out the old super */
  485. btrfs_scratch_superblock(src_device);
  486. }
  487. /*
  488. * this is again a consistent state where no dev_replace procedure
  489. * is running, the target device is part of the filesystem, the
  490. * source device is not part of the filesystem anymore and its 1st
  491. * superblock is scratched out so that it is no longer marked to
  492. * belong to this filesystem.
  493. */
  494. btrfs_dev_replace_unlock(dev_replace);
  495. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  496. /* write back the superblocks */
  497. trans = btrfs_start_transaction(root, 0);
  498. if (!IS_ERR(trans))
  499. btrfs_commit_transaction(trans, root);
  500. mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
  501. return 0;
  502. }
  503. static void btrfs_dev_replace_update_device_in_mapping_tree(
  504. struct btrfs_fs_info *fs_info,
  505. struct btrfs_device *srcdev,
  506. struct btrfs_device *tgtdev)
  507. {
  508. struct extent_map_tree *em_tree = &fs_info->mapping_tree.map_tree;
  509. struct extent_map *em;
  510. struct map_lookup *map;
  511. u64 start = 0;
  512. int i;
  513. write_lock(&em_tree->lock);
  514. do {
  515. em = lookup_extent_mapping(em_tree, start, (u64)-1);
  516. if (!em)
  517. break;
  518. map = (struct map_lookup *)em->bdev;
  519. for (i = 0; i < map->num_stripes; i++)
  520. if (srcdev == map->stripes[i].dev)
  521. map->stripes[i].dev = tgtdev;
  522. start = em->start + em->len;
  523. free_extent_map(em);
  524. } while (start);
  525. write_unlock(&em_tree->lock);
  526. }
  527. static int btrfs_dev_replace_find_srcdev(struct btrfs_root *root, u64 srcdevid,
  528. char *srcdev_name,
  529. struct btrfs_device **device)
  530. {
  531. int ret;
  532. if (srcdevid) {
  533. ret = 0;
  534. *device = btrfs_find_device(root->fs_info, srcdevid, NULL,
  535. NULL);
  536. if (!*device)
  537. ret = -ENOENT;
  538. } else {
  539. ret = btrfs_find_device_missing_or_by_path(root, srcdev_name,
  540. device);
  541. }
  542. return ret;
  543. }
  544. void btrfs_dev_replace_status(struct btrfs_fs_info *fs_info,
  545. struct btrfs_ioctl_dev_replace_args *args)
  546. {
  547. struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
  548. btrfs_dev_replace_lock(dev_replace);
  549. /* even if !dev_replace_is_valid, the values are good enough for
  550. * the replace_status ioctl */
  551. args->result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR;
  552. args->status.replace_state = dev_replace->replace_state;
  553. args->status.time_started = dev_replace->time_started;
  554. args->status.time_stopped = dev_replace->time_stopped;
  555. args->status.num_write_errors =
  556. atomic64_read(&dev_replace->num_write_errors);
  557. args->status.num_uncorrectable_read_errors =
  558. atomic64_read(&dev_replace->num_uncorrectable_read_errors);
  559. switch (dev_replace->replace_state) {
  560. case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
  561. case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
  562. args->status.progress_1000 = 0;
  563. break;
  564. case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
  565. args->status.progress_1000 = 1000;
  566. break;
  567. case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
  568. case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
  569. args->status.progress_1000 = div64_u64(dev_replace->cursor_left,
  570. div64_u64(dev_replace->srcdev->total_bytes, 1000));
  571. break;
  572. }
  573. btrfs_dev_replace_unlock(dev_replace);
  574. }
  575. int btrfs_dev_replace_cancel(struct btrfs_fs_info *fs_info,
  576. struct btrfs_ioctl_dev_replace_args *args)
  577. {
  578. args->result = __btrfs_dev_replace_cancel(fs_info);
  579. return 0;
  580. }
  581. static u64 __btrfs_dev_replace_cancel(struct btrfs_fs_info *fs_info)
  582. {
  583. struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
  584. struct btrfs_device *tgt_device = NULL;
  585. struct btrfs_trans_handle *trans;
  586. struct btrfs_root *root = fs_info->tree_root;
  587. u64 result;
  588. int ret;
  589. mutex_lock(&dev_replace->lock_finishing_cancel_unmount);
  590. btrfs_dev_replace_lock(dev_replace);
  591. switch (dev_replace->replace_state) {
  592. case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
  593. case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
  594. case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
  595. result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NOT_STARTED;
  596. btrfs_dev_replace_unlock(dev_replace);
  597. goto leave;
  598. case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
  599. case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
  600. result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR;
  601. tgt_device = dev_replace->tgtdev;
  602. dev_replace->tgtdev = NULL;
  603. dev_replace->srcdev = NULL;
  604. break;
  605. }
  606. dev_replace->replace_state = BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED;
  607. dev_replace->time_stopped = btrfs_get_seconds_since_1970();
  608. dev_replace->item_needs_writeback = 1;
  609. btrfs_dev_replace_unlock(dev_replace);
  610. btrfs_scrub_cancel(fs_info);
  611. trans = btrfs_start_transaction(root, 0);
  612. if (IS_ERR(trans)) {
  613. mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
  614. return PTR_ERR(trans);
  615. }
  616. ret = btrfs_commit_transaction(trans, root);
  617. WARN_ON(ret);
  618. if (tgt_device)
  619. btrfs_destroy_dev_replace_tgtdev(fs_info, tgt_device);
  620. leave:
  621. mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
  622. return result;
  623. }
  624. void btrfs_dev_replace_suspend_for_unmount(struct btrfs_fs_info *fs_info)
  625. {
  626. struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
  627. mutex_lock(&dev_replace->lock_finishing_cancel_unmount);
  628. btrfs_dev_replace_lock(dev_replace);
  629. switch (dev_replace->replace_state) {
  630. case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
  631. case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
  632. case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
  633. case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
  634. break;
  635. case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
  636. dev_replace->replace_state =
  637. BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED;
  638. dev_replace->time_stopped = btrfs_get_seconds_since_1970();
  639. dev_replace->item_needs_writeback = 1;
  640. pr_info("btrfs: suspending dev_replace for unmount\n");
  641. break;
  642. }
  643. btrfs_dev_replace_unlock(dev_replace);
  644. mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
  645. }
  646. /* resume dev_replace procedure that was interrupted by unmount */
  647. int btrfs_resume_dev_replace_async(struct btrfs_fs_info *fs_info)
  648. {
  649. struct task_struct *task;
  650. struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
  651. btrfs_dev_replace_lock(dev_replace);
  652. switch (dev_replace->replace_state) {
  653. case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
  654. case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
  655. case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
  656. btrfs_dev_replace_unlock(dev_replace);
  657. return 0;
  658. case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
  659. break;
  660. case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
  661. dev_replace->replace_state =
  662. BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED;
  663. break;
  664. }
  665. if (!dev_replace->tgtdev || !dev_replace->tgtdev->bdev) {
  666. pr_info("btrfs: cannot continue dev_replace, tgtdev is missing\n"
  667. "btrfs: you may cancel the operation after 'mount -o degraded'\n");
  668. btrfs_dev_replace_unlock(dev_replace);
  669. return 0;
  670. }
  671. btrfs_dev_replace_unlock(dev_replace);
  672. WARN_ON(atomic_xchg(
  673. &fs_info->mutually_exclusive_operation_running, 1));
  674. task = kthread_run(btrfs_dev_replace_kthread, fs_info, "btrfs-devrepl");
  675. return PTR_ERR_OR_ZERO(task);
  676. }
  677. static int btrfs_dev_replace_kthread(void *data)
  678. {
  679. struct btrfs_fs_info *fs_info = data;
  680. struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
  681. struct btrfs_ioctl_dev_replace_args *status_args;
  682. u64 progress;
  683. status_args = kzalloc(sizeof(*status_args), GFP_NOFS);
  684. if (status_args) {
  685. btrfs_dev_replace_status(fs_info, status_args);
  686. progress = status_args->status.progress_1000;
  687. kfree(status_args);
  688. do_div(progress, 10);
  689. printk_in_rcu(KERN_INFO
  690. "btrfs: continuing dev_replace from %s (devid %llu) to %s @%u%%\n",
  691. dev_replace->srcdev->missing ? "<missing disk>" :
  692. rcu_str_deref(dev_replace->srcdev->name),
  693. dev_replace->srcdev->devid,
  694. dev_replace->tgtdev ?
  695. rcu_str_deref(dev_replace->tgtdev->name) :
  696. "<missing target disk>",
  697. (unsigned int)progress);
  698. }
  699. btrfs_dev_replace_continue_on_mount(fs_info);
  700. atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
  701. return 0;
  702. }
  703. static int btrfs_dev_replace_continue_on_mount(struct btrfs_fs_info *fs_info)
  704. {
  705. struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
  706. int ret;
  707. ret = btrfs_scrub_dev(fs_info, dev_replace->srcdev->devid,
  708. dev_replace->committed_cursor_left,
  709. dev_replace->srcdev->total_bytes,
  710. &dev_replace->scrub_progress, 0, 1);
  711. ret = btrfs_dev_replace_finishing(fs_info, ret);
  712. WARN_ON(ret);
  713. return 0;
  714. }
  715. int btrfs_dev_replace_is_ongoing(struct btrfs_dev_replace *dev_replace)
  716. {
  717. if (!dev_replace->is_valid)
  718. return 0;
  719. switch (dev_replace->replace_state) {
  720. case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
  721. case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
  722. case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
  723. return 0;
  724. case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
  725. case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
  726. /*
  727. * return true even if tgtdev is missing (this is
  728. * something that can happen if the dev_replace
  729. * procedure is suspended by an umount and then
  730. * the tgtdev is missing (or "btrfs dev scan") was
  731. * not called and the the filesystem is remounted
  732. * in degraded state. This does not stop the
  733. * dev_replace procedure. It needs to be canceled
  734. * manually if the cancelation is wanted.
  735. */
  736. break;
  737. }
  738. return 1;
  739. }
  740. void btrfs_dev_replace_lock(struct btrfs_dev_replace *dev_replace)
  741. {
  742. /* the beginning is just an optimization for the typical case */
  743. if (atomic_read(&dev_replace->nesting_level) == 0) {
  744. acquire_lock:
  745. /* this is not a nested case where the same thread
  746. * is trying to acqurire the same lock twice */
  747. mutex_lock(&dev_replace->lock);
  748. mutex_lock(&dev_replace->lock_management_lock);
  749. dev_replace->lock_owner = current->pid;
  750. atomic_inc(&dev_replace->nesting_level);
  751. mutex_unlock(&dev_replace->lock_management_lock);
  752. return;
  753. }
  754. mutex_lock(&dev_replace->lock_management_lock);
  755. if (atomic_read(&dev_replace->nesting_level) > 0 &&
  756. dev_replace->lock_owner == current->pid) {
  757. WARN_ON(!mutex_is_locked(&dev_replace->lock));
  758. atomic_inc(&dev_replace->nesting_level);
  759. mutex_unlock(&dev_replace->lock_management_lock);
  760. return;
  761. }
  762. mutex_unlock(&dev_replace->lock_management_lock);
  763. goto acquire_lock;
  764. }
  765. void btrfs_dev_replace_unlock(struct btrfs_dev_replace *dev_replace)
  766. {
  767. WARN_ON(!mutex_is_locked(&dev_replace->lock));
  768. mutex_lock(&dev_replace->lock_management_lock);
  769. WARN_ON(atomic_read(&dev_replace->nesting_level) < 1);
  770. WARN_ON(dev_replace->lock_owner != current->pid);
  771. atomic_dec(&dev_replace->nesting_level);
  772. if (atomic_read(&dev_replace->nesting_level) == 0) {
  773. dev_replace->lock_owner = 0;
  774. mutex_unlock(&dev_replace->lock_management_lock);
  775. mutex_unlock(&dev_replace->lock);
  776. } else {
  777. mutex_unlock(&dev_replace->lock_management_lock);
  778. }
  779. }