subvolume.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 <malloc.h>
  8. #include "ctree.h"
  9. #include "btrfs.h"
  10. #include "disk-io.h"
  11. /*
  12. * Resolve the path of ino inside subvolume @root into @path_ret.
  13. *
  14. * @path_ret must be at least PATH_MAX size.
  15. */
  16. static int get_path_in_subvol(struct btrfs_root *root, u64 ino, char *path_ret)
  17. {
  18. struct btrfs_path path;
  19. struct btrfs_key key;
  20. char *tmp;
  21. u64 cur = ino;
  22. int ret = 0;
  23. tmp = malloc(PATH_MAX);
  24. if (!tmp)
  25. return -ENOMEM;
  26. tmp[0] = '\0';
  27. btrfs_init_path(&path);
  28. while (cur != BTRFS_FIRST_FREE_OBJECTID) {
  29. struct btrfs_inode_ref *iref;
  30. int name_len;
  31. btrfs_release_path(&path);
  32. key.objectid = cur;
  33. key.type = BTRFS_INODE_REF_KEY;
  34. key.offset = (u64)-1;
  35. ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
  36. /* Impossible */
  37. if (ret == 0)
  38. ret = -EUCLEAN;
  39. if (ret < 0)
  40. goto out;
  41. ret = btrfs_previous_item(root, &path, cur,
  42. BTRFS_INODE_REF_KEY);
  43. if (ret > 0)
  44. ret = -ENOENT;
  45. if (ret < 0)
  46. goto out;
  47. strncpy(tmp, path_ret, PATH_MAX);
  48. iref = btrfs_item_ptr(path.nodes[0], path.slots[0],
  49. struct btrfs_inode_ref);
  50. name_len = btrfs_inode_ref_name_len(path.nodes[0],
  51. iref);
  52. if (name_len > BTRFS_NAME_LEN) {
  53. ret = -ENAMETOOLONG;
  54. goto out;
  55. }
  56. read_extent_buffer(path.nodes[0], path_ret,
  57. (unsigned long)(iref + 1), name_len);
  58. path_ret[name_len] = '/';
  59. path_ret[name_len + 1] = '\0';
  60. strncat(path_ret, tmp, PATH_MAX);
  61. btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
  62. cur = key.offset;
  63. }
  64. out:
  65. btrfs_release_path(&path);
  66. free(tmp);
  67. return ret;
  68. }
  69. static int list_one_subvol(struct btrfs_root *root, char *path_ret)
  70. {
  71. struct btrfs_fs_info *fs_info = root->fs_info;
  72. struct btrfs_root *tree_root = fs_info->tree_root;
  73. struct btrfs_path path;
  74. struct btrfs_key key;
  75. char *tmp;
  76. u64 cur = root->root_key.objectid;
  77. int ret = 0;
  78. tmp = malloc(PATH_MAX);
  79. if (!tmp)
  80. return -ENOMEM;
  81. tmp[0] = '\0';
  82. path_ret[0] = '\0';
  83. btrfs_init_path(&path);
  84. while (cur != BTRFS_FS_TREE_OBJECTID) {
  85. struct btrfs_root_ref *rr;
  86. struct btrfs_key location;
  87. int name_len;
  88. u64 ino;
  89. key.objectid = cur;
  90. key.type = BTRFS_ROOT_BACKREF_KEY;
  91. key.offset = (u64)-1;
  92. btrfs_release_path(&path);
  93. ret = btrfs_search_slot(NULL, tree_root, &key, &path, 0, 0);
  94. if (ret == 0)
  95. ret = -EUCLEAN;
  96. if (ret < 0)
  97. goto out;
  98. ret = btrfs_previous_item(tree_root, &path, cur,
  99. BTRFS_ROOT_BACKREF_KEY);
  100. if (ret > 0)
  101. ret = -ENOENT;
  102. if (ret < 0)
  103. goto out;
  104. /* Get the subvolume name */
  105. rr = btrfs_item_ptr(path.nodes[0], path.slots[0],
  106. struct btrfs_root_ref);
  107. strncpy(tmp, path_ret, PATH_MAX);
  108. name_len = btrfs_root_ref_name_len(path.nodes[0], rr);
  109. if (name_len > BTRFS_NAME_LEN) {
  110. ret = -ENAMETOOLONG;
  111. goto out;
  112. }
  113. ino = btrfs_root_ref_dirid(path.nodes[0], rr);
  114. read_extent_buffer(path.nodes[0], path_ret,
  115. (unsigned long)(rr + 1), name_len);
  116. path_ret[name_len] = '/';
  117. path_ret[name_len + 1] = '\0';
  118. strncat(path_ret, tmp, PATH_MAX);
  119. /* Get the path inside the parent subvolume */
  120. btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
  121. location.objectid = key.offset;
  122. location.type = BTRFS_ROOT_ITEM_KEY;
  123. location.offset = (u64)-1;
  124. root = btrfs_read_fs_root(fs_info, &location);
  125. if (IS_ERR(root)) {
  126. ret = PTR_ERR(root);
  127. goto out;
  128. }
  129. ret = get_path_in_subvol(root, ino, path_ret);
  130. if (ret < 0)
  131. goto out;
  132. cur = key.offset;
  133. }
  134. /* Add the leading '/' */
  135. strncpy(tmp, path_ret, PATH_MAX);
  136. strncpy(path_ret, "/", PATH_MAX);
  137. strncat(path_ret, tmp, PATH_MAX);
  138. out:
  139. btrfs_release_path(&path);
  140. free(tmp);
  141. return ret;
  142. }
  143. static int list_subvolums(struct btrfs_fs_info *fs_info)
  144. {
  145. struct btrfs_root *tree_root = fs_info->tree_root;
  146. struct btrfs_root *root;
  147. struct btrfs_path path;
  148. struct btrfs_key key;
  149. char *result;
  150. int ret = 0;
  151. result = malloc(PATH_MAX);
  152. if (!result)
  153. return -ENOMEM;
  154. ret = list_one_subvol(fs_info->fs_root, result);
  155. if (ret < 0)
  156. goto out;
  157. root = fs_info->fs_root;
  158. printf("ID %llu gen %llu path %.*s\n",
  159. root->root_key.objectid, btrfs_root_generation(&root->root_item),
  160. PATH_MAX, result);
  161. key.objectid = BTRFS_FIRST_FREE_OBJECTID;
  162. key.type = BTRFS_ROOT_ITEM_KEY;
  163. key.offset = 0;
  164. btrfs_init_path(&path);
  165. ret = btrfs_search_slot(NULL, tree_root, &key, &path, 0, 0);
  166. if (ret < 0)
  167. goto out;
  168. while (1) {
  169. if (path.slots[0] >= btrfs_header_nritems(path.nodes[0]))
  170. goto next;
  171. btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
  172. if (key.objectid > BTRFS_LAST_FREE_OBJECTID)
  173. break;
  174. if (key.objectid < BTRFS_FIRST_FREE_OBJECTID ||
  175. key.type != BTRFS_ROOT_ITEM_KEY)
  176. goto next;
  177. key.offset = (u64)-1;
  178. root = btrfs_read_fs_root(fs_info, &key);
  179. if (IS_ERR(root)) {
  180. ret = PTR_ERR(root);
  181. if (ret == -ENOENT)
  182. goto next;
  183. }
  184. ret = list_one_subvol(root, result);
  185. if (ret < 0)
  186. goto out;
  187. printf("ID %llu gen %llu path %.*s\n",
  188. root->root_key.objectid,
  189. btrfs_root_generation(&root->root_item),
  190. PATH_MAX, result);
  191. next:
  192. ret = btrfs_next_item(tree_root, &path);
  193. if (ret < 0)
  194. goto out;
  195. if (ret > 0) {
  196. ret = 0;
  197. break;
  198. }
  199. }
  200. out:
  201. free(result);
  202. return ret;
  203. }
  204. void btrfs_list_subvols(void)
  205. {
  206. struct btrfs_fs_info *fs_info = current_fs_info;
  207. int ret;
  208. if (!fs_info)
  209. return;
  210. ret = list_subvolums(fs_info);
  211. if (ret < 0)
  212. error("failed to list subvolume: %d", ret);
  213. }