subvolume.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * BTRFS filesystem implementation for U-Boot
  3. *
  4. * 2017 Marek Behun, CZ.NIC, marek.behun@nic.cz
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #include "btrfs.h"
  9. #include <malloc.h>
  10. static int get_subvol_name(u64 subvolid, char *name, int max_len)
  11. {
  12. struct btrfs_root_ref rref;
  13. struct btrfs_inode_ref iref;
  14. struct btrfs_root root;
  15. u64 dir;
  16. char tmp[max(BTRFS_VOL_NAME_MAX, BTRFS_NAME_MAX)];
  17. char *ptr;
  18. ptr = name + max_len - 1;
  19. *ptr = '\0';
  20. while (subvolid != BTRFS_FS_TREE_OBJECTID) {
  21. subvolid = btrfs_lookup_root_ref(subvolid, &rref, tmp);
  22. if (subvolid == -1ULL)
  23. return -1;
  24. ptr -= rref.name_len + 1;
  25. if (ptr < name)
  26. goto too_long;
  27. memcpy(ptr + 1, tmp, rref.name_len);
  28. *ptr = '/';
  29. if (btrfs_find_root(subvolid, &root, NULL))
  30. return -1;
  31. dir = rref.dirid;
  32. while (dir != BTRFS_FIRST_FREE_OBJECTID) {
  33. dir = btrfs_lookup_inode_ref(&root, dir, &iref, tmp);
  34. if (dir == -1ULL)
  35. return -1;
  36. ptr -= iref.name_len + 1;
  37. if (ptr < name)
  38. goto too_long;
  39. memcpy(ptr + 1, tmp, iref.name_len);
  40. *ptr = '/';
  41. }
  42. }
  43. if (ptr == name + max_len - 1) {
  44. name[0] = '/';
  45. name[1] = '\0';
  46. } else {
  47. memmove(name, ptr, name + max_len - ptr);
  48. }
  49. return 0;
  50. too_long:
  51. printf("%s: subvolume name too long\n", __func__);
  52. return -1;
  53. }
  54. u64 btrfs_get_default_subvol_objectid(void)
  55. {
  56. struct btrfs_dir_item item;
  57. if (btrfs_lookup_dir_item(&btrfs_info.tree_root,
  58. btrfs_info.sb.root_dir_objectid, "default", 7,
  59. &item))
  60. return BTRFS_FS_TREE_OBJECTID;
  61. return item.location.objectid;
  62. }
  63. static void list_subvols(u64 tree, char *nameptr, int max_name_len, int level)
  64. {
  65. struct btrfs_key key, *found_key;
  66. struct btrfs_path path;
  67. struct btrfs_root_ref *ref;
  68. int res;
  69. key.objectid = tree;
  70. key.type = BTRFS_ROOT_REF_KEY;
  71. key.offset = 0;
  72. if (btrfs_search_tree(&btrfs_info.tree_root, &key, &path))
  73. return;
  74. do {
  75. found_key = btrfs_path_leaf_key(&path);
  76. if (btrfs_comp_keys_type(&key, found_key))
  77. break;
  78. ref = btrfs_path_item_ptr(&path, struct btrfs_root_ref);
  79. btrfs_root_ref_to_cpu(ref);
  80. printf("ID %llu parent %llu name ", found_key->offset, tree);
  81. if (nameptr && !get_subvol_name(found_key->offset, nameptr,
  82. max_name_len))
  83. printf("%s\n", nameptr);
  84. else
  85. printf("%.*s\n", (int) ref->name_len,
  86. (const char *) (ref + 1));
  87. if (level > 0)
  88. list_subvols(found_key->offset, nameptr, max_name_len,
  89. level - 1);
  90. else
  91. printf("%s: Too much recursion, maybe skipping some "
  92. "subvolumes\n", __func__);
  93. } while (!(res = btrfs_next_slot(&path)));
  94. btrfs_free_path(&path);
  95. }
  96. void btrfs_list_subvols(void)
  97. {
  98. char *nameptr = malloc(4096);
  99. list_subvols(BTRFS_FS_TREE_OBJECTID, nameptr, nameptr ? 4096 : 0, 40);
  100. if (nameptr)
  101. free(nameptr);
  102. }