subvolume.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. static int get_subvol_name(u64 subvolid, char *name, int max_len)
  205. {
  206. struct btrfs_root_ref rref;
  207. struct btrfs_inode_ref iref;
  208. struct __btrfs_root root;
  209. u64 dir;
  210. char tmp[BTRFS_NAME_LEN];
  211. char *ptr;
  212. ptr = name + max_len - 1;
  213. *ptr = '\0';
  214. while (subvolid != BTRFS_FS_TREE_OBJECTID) {
  215. subvolid = btrfs_lookup_root_ref(subvolid, &rref, tmp);
  216. if (subvolid == -1ULL)
  217. return -1;
  218. ptr -= rref.name_len + 1;
  219. if (ptr < name)
  220. goto too_long;
  221. memcpy(ptr + 1, tmp, rref.name_len);
  222. *ptr = '/';
  223. if (btrfs_find_root(subvolid, &root, NULL))
  224. return -1;
  225. dir = rref.dirid;
  226. while (dir != BTRFS_FIRST_FREE_OBJECTID) {
  227. dir = __btrfs_lookup_inode_ref(&root, dir, &iref, tmp);
  228. if (dir == -1ULL)
  229. return -1;
  230. ptr -= iref.name_len + 1;
  231. if (ptr < name)
  232. goto too_long;
  233. memcpy(ptr + 1, tmp, iref.name_len);
  234. *ptr = '/';
  235. }
  236. }
  237. if (ptr == name + max_len - 1) {
  238. name[0] = '/';
  239. name[1] = '\0';
  240. } else {
  241. memmove(name, ptr, name + max_len - ptr);
  242. }
  243. return 0;
  244. too_long:
  245. printf("%s: subvolume name too long\n", __func__);
  246. return -1;
  247. }
  248. u64 btrfs_get_default_subvol_objectid(void)
  249. {
  250. struct btrfs_dir_item item;
  251. if (__btrfs_lookup_dir_item(&btrfs_info.tree_root,
  252. btrfs_info.sb.root_dir_objectid, "default", 7,
  253. &item))
  254. return BTRFS_FS_TREE_OBJECTID;
  255. return item.location.objectid;
  256. }
  257. static void list_subvols(u64 tree, char *nameptr, int max_name_len, int level)
  258. {
  259. struct btrfs_key key, *found_key;
  260. struct __btrfs_path path;
  261. struct btrfs_root_ref *ref;
  262. int res;
  263. key.objectid = tree;
  264. key.type = BTRFS_ROOT_REF_KEY;
  265. key.offset = 0;
  266. if (btrfs_search_tree(&btrfs_info.tree_root, &key, &path))
  267. return;
  268. do {
  269. found_key = btrfs_path_leaf_key(&path);
  270. if (btrfs_comp_keys_type(&key, found_key))
  271. break;
  272. ref = btrfs_path_item_ptr(&path, struct btrfs_root_ref);
  273. btrfs_root_ref_to_cpu(ref);
  274. printf("ID %llu parent %llu name ", found_key->offset, tree);
  275. if (nameptr && !get_subvol_name(found_key->offset, nameptr,
  276. max_name_len))
  277. printf("%s\n", nameptr);
  278. else
  279. printf("%.*s\n", (int) ref->name_len,
  280. (const char *) (ref + 1));
  281. if (level > 0)
  282. list_subvols(found_key->offset, nameptr, max_name_len,
  283. level - 1);
  284. else
  285. printf("%s: Too much recursion, maybe skipping some "
  286. "subvolumes\n", __func__);
  287. } while (!(res = btrfs_next_slot(&path)));
  288. __btrfs_free_path(&path);
  289. }
  290. void btrfs_list_subvols(void)
  291. {
  292. struct btrfs_fs_info *fs_info = current_fs_info;
  293. int ret;
  294. if (!fs_info)
  295. return;
  296. ret = list_subvolums(fs_info);
  297. if (ret < 0)
  298. error("failed to list subvolume: %d", ret);
  299. }