dev-replace.c 29 KB

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