uuid-tree.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) STRATO AG 2013. All rights reserved.
  4. */
  5. #include <linux/kthread.h>
  6. #include <linux/uuid.h>
  7. #include <linux/unaligned.h>
  8. #include "messages.h"
  9. #include "ctree.h"
  10. #include "transaction.h"
  11. #include "disk-io.h"
  12. #include "fs.h"
  13. #include "accessors.h"
  14. #include "uuid-tree.h"
  15. #include "ioctl.h"
  16. static void btrfs_uuid_to_key(const u8 *uuid, u8 type, struct btrfs_key *key)
  17. {
  18. key->type = type;
  19. key->objectid = get_unaligned_le64(uuid);
  20. key->offset = get_unaligned_le64(uuid + sizeof(u64));
  21. }
  22. /* return -ENOENT for !found, < 0 for errors, or 0 if an item was found */
  23. static int btrfs_uuid_tree_lookup(struct btrfs_root *uuid_root, const u8 *uuid,
  24. u8 type, u64 subid)
  25. {
  26. int ret;
  27. struct btrfs_path *path = NULL;
  28. struct extent_buffer *eb;
  29. int slot;
  30. u32 item_size;
  31. unsigned long offset;
  32. struct btrfs_key key;
  33. if (WARN_ON_ONCE(!uuid_root)) {
  34. ret = -ENOENT;
  35. goto out;
  36. }
  37. path = btrfs_alloc_path();
  38. if (!path) {
  39. ret = -ENOMEM;
  40. goto out;
  41. }
  42. btrfs_uuid_to_key(uuid, type, &key);
  43. ret = btrfs_search_slot(NULL, uuid_root, &key, path, 0, 0);
  44. if (ret < 0) {
  45. goto out;
  46. } else if (ret > 0) {
  47. ret = -ENOENT;
  48. goto out;
  49. }
  50. eb = path->nodes[0];
  51. slot = path->slots[0];
  52. item_size = btrfs_item_size(eb, slot);
  53. offset = btrfs_item_ptr_offset(eb, slot);
  54. ret = -ENOENT;
  55. if (!IS_ALIGNED(item_size, sizeof(u64))) {
  56. btrfs_warn(uuid_root->fs_info,
  57. "uuid item with illegal size %lu!",
  58. (unsigned long)item_size);
  59. goto out;
  60. }
  61. while (item_size) {
  62. __le64 data;
  63. read_extent_buffer(eb, &data, offset, sizeof(data));
  64. if (le64_to_cpu(data) == subid) {
  65. ret = 0;
  66. break;
  67. }
  68. offset += sizeof(data);
  69. item_size -= sizeof(data);
  70. }
  71. out:
  72. btrfs_free_path(path);
  73. return ret;
  74. }
  75. int btrfs_uuid_tree_add(struct btrfs_trans_handle *trans, const u8 *uuid, u8 type,
  76. u64 subid_cpu)
  77. {
  78. struct btrfs_fs_info *fs_info = trans->fs_info;
  79. struct btrfs_root *uuid_root = fs_info->uuid_root;
  80. int ret;
  81. struct btrfs_path *path = NULL;
  82. struct btrfs_key key;
  83. struct extent_buffer *eb;
  84. int slot;
  85. unsigned long offset;
  86. __le64 subid_le;
  87. ret = btrfs_uuid_tree_lookup(uuid_root, uuid, type, subid_cpu);
  88. if (ret != -ENOENT)
  89. return ret;
  90. if (WARN_ON_ONCE(!uuid_root)) {
  91. ret = -EINVAL;
  92. goto out;
  93. }
  94. btrfs_uuid_to_key(uuid, type, &key);
  95. path = btrfs_alloc_path();
  96. if (!path) {
  97. ret = -ENOMEM;
  98. goto out;
  99. }
  100. ret = btrfs_insert_empty_item(trans, uuid_root, path, &key,
  101. sizeof(subid_le));
  102. if (ret == 0) {
  103. /* Add an item for the type for the first time */
  104. eb = path->nodes[0];
  105. slot = path->slots[0];
  106. offset = btrfs_item_ptr_offset(eb, slot);
  107. } else if (ret == -EEXIST) {
  108. /*
  109. * An item with that type already exists.
  110. * Extend the item and store the new subid at the end.
  111. */
  112. btrfs_extend_item(trans, path, sizeof(subid_le));
  113. eb = path->nodes[0];
  114. slot = path->slots[0];
  115. offset = btrfs_item_ptr_offset(eb, slot);
  116. offset += btrfs_item_size(eb, slot) - sizeof(subid_le);
  117. } else {
  118. btrfs_warn(fs_info,
  119. "insert uuid item failed %d (0x%016llx, 0x%016llx) type %u!",
  120. ret, key.objectid, key.offset, type);
  121. goto out;
  122. }
  123. ret = 0;
  124. subid_le = cpu_to_le64(subid_cpu);
  125. write_extent_buffer(eb, &subid_le, offset, sizeof(subid_le));
  126. btrfs_mark_buffer_dirty(trans, eb);
  127. out:
  128. btrfs_free_path(path);
  129. return ret;
  130. }
  131. int btrfs_uuid_tree_remove(struct btrfs_trans_handle *trans, const u8 *uuid, u8 type,
  132. u64 subid)
  133. {
  134. struct btrfs_fs_info *fs_info = trans->fs_info;
  135. struct btrfs_root *uuid_root = fs_info->uuid_root;
  136. int ret;
  137. struct btrfs_path *path = NULL;
  138. struct btrfs_key key;
  139. struct extent_buffer *eb;
  140. int slot;
  141. unsigned long offset;
  142. u32 item_size;
  143. unsigned long move_dst;
  144. unsigned long move_src;
  145. unsigned long move_len;
  146. if (WARN_ON_ONCE(!uuid_root)) {
  147. ret = -EINVAL;
  148. goto out;
  149. }
  150. btrfs_uuid_to_key(uuid, type, &key);
  151. path = btrfs_alloc_path();
  152. if (!path) {
  153. ret = -ENOMEM;
  154. goto out;
  155. }
  156. ret = btrfs_search_slot(trans, uuid_root, &key, path, -1, 1);
  157. if (ret < 0) {
  158. btrfs_warn(fs_info, "error %d while searching for uuid item!",
  159. ret);
  160. goto out;
  161. }
  162. if (ret > 0) {
  163. ret = -ENOENT;
  164. goto out;
  165. }
  166. eb = path->nodes[0];
  167. slot = path->slots[0];
  168. offset = btrfs_item_ptr_offset(eb, slot);
  169. item_size = btrfs_item_size(eb, slot);
  170. if (!IS_ALIGNED(item_size, sizeof(u64))) {
  171. btrfs_warn(fs_info, "uuid item with illegal size %lu!",
  172. (unsigned long)item_size);
  173. ret = -ENOENT;
  174. goto out;
  175. }
  176. while (item_size) {
  177. __le64 read_subid;
  178. read_extent_buffer(eb, &read_subid, offset, sizeof(read_subid));
  179. if (le64_to_cpu(read_subid) == subid)
  180. break;
  181. offset += sizeof(read_subid);
  182. item_size -= sizeof(read_subid);
  183. }
  184. if (!item_size) {
  185. ret = -ENOENT;
  186. goto out;
  187. }
  188. item_size = btrfs_item_size(eb, slot);
  189. if (item_size == sizeof(subid)) {
  190. ret = btrfs_del_item(trans, uuid_root, path);
  191. goto out;
  192. }
  193. move_dst = offset;
  194. move_src = offset + sizeof(subid);
  195. move_len = item_size - (move_src - btrfs_item_ptr_offset(eb, slot));
  196. memmove_extent_buffer(eb, move_dst, move_src, move_len);
  197. btrfs_truncate_item(trans, path, item_size - sizeof(subid), 1);
  198. out:
  199. btrfs_free_path(path);
  200. return ret;
  201. }
  202. static int btrfs_uuid_iter_rem(struct btrfs_root *uuid_root, u8 *uuid, u8 type,
  203. u64 subid)
  204. {
  205. struct btrfs_trans_handle *trans;
  206. int ret;
  207. /* 1 - for the uuid item */
  208. trans = btrfs_start_transaction(uuid_root, 1);
  209. if (IS_ERR(trans)) {
  210. ret = PTR_ERR(trans);
  211. goto out;
  212. }
  213. ret = btrfs_uuid_tree_remove(trans, uuid, type, subid);
  214. btrfs_end_transaction(trans);
  215. out:
  216. return ret;
  217. }
  218. /*
  219. * Check if there's an matching subvolume for given UUID
  220. *
  221. * Return:
  222. * 0 check succeeded, the entry is not outdated
  223. * > 0 if the check failed, the caller should remove the entry
  224. * < 0 if an error occurred
  225. */
  226. static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info,
  227. const u8 *uuid, u8 type, u64 subvolid)
  228. {
  229. int ret = 0;
  230. struct btrfs_root *subvol_root;
  231. if (type != BTRFS_UUID_KEY_SUBVOL &&
  232. type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
  233. goto out;
  234. subvol_root = btrfs_get_fs_root(fs_info, subvolid, true);
  235. if (IS_ERR(subvol_root)) {
  236. ret = PTR_ERR(subvol_root);
  237. if (ret == -ENOENT)
  238. ret = 1;
  239. goto out;
  240. }
  241. switch (type) {
  242. case BTRFS_UUID_KEY_SUBVOL:
  243. if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE))
  244. ret = 1;
  245. break;
  246. case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
  247. if (memcmp(uuid, subvol_root->root_item.received_uuid,
  248. BTRFS_UUID_SIZE))
  249. ret = 1;
  250. break;
  251. }
  252. btrfs_put_root(subvol_root);
  253. out:
  254. return ret;
  255. }
  256. int btrfs_uuid_tree_iterate(struct btrfs_fs_info *fs_info)
  257. {
  258. struct btrfs_root *root = fs_info->uuid_root;
  259. struct btrfs_key key;
  260. struct btrfs_path *path;
  261. int ret = 0;
  262. struct extent_buffer *leaf;
  263. int slot;
  264. u32 item_size;
  265. unsigned long offset;
  266. path = btrfs_alloc_path();
  267. if (!path) {
  268. ret = -ENOMEM;
  269. goto out;
  270. }
  271. key.objectid = 0;
  272. key.type = 0;
  273. key.offset = 0;
  274. again_search_slot:
  275. ret = btrfs_search_forward(root, &key, path, BTRFS_OLDEST_GENERATION);
  276. if (ret) {
  277. if (ret > 0)
  278. ret = 0;
  279. goto out;
  280. }
  281. while (1) {
  282. if (btrfs_fs_closing(fs_info)) {
  283. ret = -EINTR;
  284. goto out;
  285. }
  286. cond_resched();
  287. leaf = path->nodes[0];
  288. slot = path->slots[0];
  289. btrfs_item_key_to_cpu(leaf, &key, slot);
  290. if (key.type != BTRFS_UUID_KEY_SUBVOL &&
  291. key.type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
  292. goto skip;
  293. offset = btrfs_item_ptr_offset(leaf, slot);
  294. item_size = btrfs_item_size(leaf, slot);
  295. if (!IS_ALIGNED(item_size, sizeof(u64))) {
  296. btrfs_warn(fs_info,
  297. "uuid item with illegal size %lu!",
  298. (unsigned long)item_size);
  299. goto skip;
  300. }
  301. while (item_size) {
  302. u8 uuid[BTRFS_UUID_SIZE];
  303. __le64 subid_le;
  304. u64 subid_cpu;
  305. put_unaligned_le64(key.objectid, uuid);
  306. put_unaligned_le64(key.offset, uuid + sizeof(u64));
  307. read_extent_buffer(leaf, &subid_le, offset,
  308. sizeof(subid_le));
  309. subid_cpu = le64_to_cpu(subid_le);
  310. ret = btrfs_check_uuid_tree_entry(fs_info, uuid,
  311. key.type, subid_cpu);
  312. if (ret < 0)
  313. goto out;
  314. if (ret > 0) {
  315. btrfs_release_path(path);
  316. ret = btrfs_uuid_iter_rem(root, uuid, key.type,
  317. subid_cpu);
  318. if (ret == 0) {
  319. /*
  320. * this might look inefficient, but the
  321. * justification is that it is an
  322. * exception that check_func returns 1,
  323. * and that in the regular case only one
  324. * entry per UUID exists.
  325. */
  326. goto again_search_slot;
  327. }
  328. if (ret < 0 && ret != -ENOENT)
  329. goto out;
  330. key.offset++;
  331. goto again_search_slot;
  332. }
  333. item_size -= sizeof(subid_le);
  334. offset += sizeof(subid_le);
  335. }
  336. skip:
  337. ret = btrfs_next_item(root, path);
  338. if (ret == 0)
  339. continue;
  340. else if (ret > 0)
  341. ret = 0;
  342. break;
  343. }
  344. out:
  345. btrfs_free_path(path);
  346. return ret;
  347. }
  348. int btrfs_uuid_scan_kthread(void *data)
  349. {
  350. struct btrfs_fs_info *fs_info = data;
  351. struct btrfs_root *root = fs_info->tree_root;
  352. struct btrfs_key key;
  353. struct btrfs_path *path = NULL;
  354. int ret = 0;
  355. struct extent_buffer *eb;
  356. int slot;
  357. struct btrfs_root_item root_item;
  358. u32 item_size;
  359. struct btrfs_trans_handle *trans = NULL;
  360. bool closing = false;
  361. path = btrfs_alloc_path();
  362. if (!path) {
  363. ret = -ENOMEM;
  364. goto out;
  365. }
  366. key.objectid = 0;
  367. key.type = BTRFS_ROOT_ITEM_KEY;
  368. key.offset = 0;
  369. while (1) {
  370. if (btrfs_fs_closing(fs_info)) {
  371. closing = true;
  372. break;
  373. }
  374. ret = btrfs_search_forward(root, &key, path,
  375. BTRFS_OLDEST_GENERATION);
  376. if (ret) {
  377. if (ret > 0)
  378. ret = 0;
  379. break;
  380. }
  381. if (key.type != BTRFS_ROOT_ITEM_KEY ||
  382. (key.objectid < BTRFS_FIRST_FREE_OBJECTID &&
  383. key.objectid != BTRFS_FS_TREE_OBJECTID) ||
  384. key.objectid > BTRFS_LAST_FREE_OBJECTID)
  385. goto skip;
  386. eb = path->nodes[0];
  387. slot = path->slots[0];
  388. item_size = btrfs_item_size(eb, slot);
  389. if (item_size < sizeof(root_item))
  390. goto skip;
  391. read_extent_buffer(eb, &root_item,
  392. btrfs_item_ptr_offset(eb, slot),
  393. (int)sizeof(root_item));
  394. if (btrfs_root_refs(&root_item) == 0)
  395. goto skip;
  396. if (!btrfs_is_empty_uuid(root_item.uuid) ||
  397. !btrfs_is_empty_uuid(root_item.received_uuid)) {
  398. if (trans)
  399. goto update_tree;
  400. btrfs_release_path(path);
  401. /*
  402. * 1 - subvol uuid item
  403. * 1 - received_subvol uuid item
  404. */
  405. trans = btrfs_start_transaction(fs_info->uuid_root, 2);
  406. if (IS_ERR(trans)) {
  407. ret = PTR_ERR(trans);
  408. break;
  409. }
  410. continue;
  411. } else {
  412. goto skip;
  413. }
  414. update_tree:
  415. btrfs_release_path(path);
  416. if (!btrfs_is_empty_uuid(root_item.uuid)) {
  417. ret = btrfs_uuid_tree_add(trans, root_item.uuid,
  418. BTRFS_UUID_KEY_SUBVOL,
  419. key.objectid);
  420. if (ret < 0) {
  421. btrfs_warn(fs_info, "uuid_tree_add failed %d",
  422. ret);
  423. break;
  424. }
  425. }
  426. if (!btrfs_is_empty_uuid(root_item.received_uuid)) {
  427. ret = btrfs_uuid_tree_add(trans,
  428. root_item.received_uuid,
  429. BTRFS_UUID_KEY_RECEIVED_SUBVOL,
  430. key.objectid);
  431. if (ret < 0) {
  432. btrfs_warn(fs_info, "uuid_tree_add failed %d",
  433. ret);
  434. break;
  435. }
  436. }
  437. skip:
  438. btrfs_release_path(path);
  439. if (trans) {
  440. ret = btrfs_end_transaction(trans);
  441. trans = NULL;
  442. if (ret)
  443. break;
  444. }
  445. if (key.offset < (u64)-1) {
  446. key.offset++;
  447. } else if (key.type < BTRFS_ROOT_ITEM_KEY) {
  448. key.offset = 0;
  449. key.type = BTRFS_ROOT_ITEM_KEY;
  450. } else if (key.objectid < (u64)-1) {
  451. key.offset = 0;
  452. key.type = BTRFS_ROOT_ITEM_KEY;
  453. key.objectid++;
  454. } else {
  455. break;
  456. }
  457. cond_resched();
  458. }
  459. out:
  460. btrfs_free_path(path);
  461. if (trans && !IS_ERR(trans))
  462. btrfs_end_transaction(trans);
  463. if (ret)
  464. btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret);
  465. else if (!closing)
  466. set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags);
  467. up(&fs_info->uuid_tree_rescan_sem);
  468. return 0;
  469. }
  470. int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info)
  471. {
  472. struct btrfs_trans_handle *trans;
  473. struct btrfs_root *tree_root = fs_info->tree_root;
  474. struct btrfs_root *uuid_root;
  475. struct task_struct *task;
  476. int ret;
  477. /*
  478. * 1 - root node
  479. * 1 - root item
  480. */
  481. trans = btrfs_start_transaction(tree_root, 2);
  482. if (IS_ERR(trans))
  483. return PTR_ERR(trans);
  484. uuid_root = btrfs_create_tree(trans, BTRFS_UUID_TREE_OBJECTID);
  485. if (IS_ERR(uuid_root)) {
  486. ret = PTR_ERR(uuid_root);
  487. btrfs_abort_transaction(trans, ret);
  488. btrfs_end_transaction(trans);
  489. return ret;
  490. }
  491. fs_info->uuid_root = uuid_root;
  492. ret = btrfs_commit_transaction(trans);
  493. if (ret)
  494. return ret;
  495. down(&fs_info->uuid_tree_rescan_sem);
  496. task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid");
  497. if (IS_ERR(task)) {
  498. /* fs_info->update_uuid_tree_gen remains 0 in all error case */
  499. btrfs_warn(fs_info, "failed to start uuid_scan task");
  500. up(&fs_info->uuid_tree_rescan_sem);
  501. return PTR_ERR(task);
  502. }
  503. return 0;
  504. }