dev-replace.c 29 KB

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