uuid-tree.c 13 KB

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