subvolume.c 2.7 KB

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