inode.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. u64 btrfs_lookup_inode_ref(struct btrfs_root *root, u64 inr,
  10. struct btrfs_inode_ref *refp, char *name)
  11. {
  12. struct btrfs_path path;
  13. struct btrfs_key *key;
  14. struct btrfs_inode_ref *ref;
  15. u64 res = -1ULL;
  16. key = btrfs_search_tree_key_type(root, inr, BTRFS_INODE_REF_KEY,
  17. &path);
  18. if (!key)
  19. return -1ULL;
  20. ref = btrfs_path_item_ptr(&path, struct btrfs_inode_ref);
  21. btrfs_inode_ref_to_cpu(ref);
  22. if (refp)
  23. *refp = *ref;
  24. if (name) {
  25. if (ref->name_len > BTRFS_NAME_MAX) {
  26. printf("%s: inode name too long: %u\n", __func__,
  27. ref->name_len);
  28. goto out;
  29. }
  30. memcpy(name, ref + 1, ref->name_len);
  31. }
  32. res = key->offset;
  33. out:
  34. btrfs_free_path(&path);
  35. return res;
  36. }
  37. int btrfs_lookup_inode(const struct btrfs_root *root,
  38. struct btrfs_key *location,
  39. struct btrfs_inode_item *item,
  40. struct btrfs_root *new_root)
  41. {
  42. struct btrfs_root tmp_root = *root;
  43. struct btrfs_path path;
  44. int res = -1;
  45. if (location->type == BTRFS_ROOT_ITEM_KEY) {
  46. if (btrfs_find_root(location->objectid, &tmp_root, NULL))
  47. return -1;
  48. location->objectid = tmp_root.root_dirid;
  49. location->type = BTRFS_INODE_ITEM_KEY;
  50. location->offset = 0;
  51. }
  52. if (btrfs_search_tree(&tmp_root, location, &path))
  53. return res;
  54. if (btrfs_comp_keys(location, btrfs_path_leaf_key(&path)))
  55. goto out;
  56. if (item) {
  57. *item = *btrfs_path_item_ptr(&path, struct btrfs_inode_item);
  58. btrfs_inode_item_to_cpu(item);
  59. }
  60. if (new_root)
  61. *new_root = tmp_root;
  62. res = 0;
  63. out:
  64. btrfs_free_path(&path);
  65. return res;
  66. }
  67. int btrfs_readlink(const struct btrfs_root *root, u64 inr, char *target)
  68. {
  69. struct btrfs_path path;
  70. struct btrfs_key key;
  71. struct btrfs_file_extent_item *extent;
  72. const char *data_ptr;
  73. int res = -1;
  74. key.objectid = inr;
  75. key.type = BTRFS_EXTENT_DATA_KEY;
  76. key.offset = 0;
  77. if (btrfs_search_tree(root, &key, &path))
  78. return -1;
  79. if (btrfs_comp_keys(&key, btrfs_path_leaf_key(&path)))
  80. goto out;
  81. extent = btrfs_path_item_ptr(&path, struct btrfs_file_extent_item);
  82. if (extent->type != BTRFS_FILE_EXTENT_INLINE) {
  83. printf("%s: Extent for symlink %llu not of INLINE type\n",
  84. __func__, inr);
  85. goto out;
  86. }
  87. btrfs_file_extent_item_to_cpu_inl(extent);
  88. if (extent->compression != BTRFS_COMPRESS_NONE) {
  89. printf("%s: Symlink %llu extent data compressed!\n", __func__,
  90. inr);
  91. goto out;
  92. } else if (extent->encryption != 0) {
  93. printf("%s: Symlink %llu extent data encrypted!\n", __func__,
  94. inr);
  95. goto out;
  96. } else if (extent->ram_bytes >= btrfs_info.sb.sectorsize) {
  97. printf("%s: Symlink %llu extent data too long (%llu)!\n",
  98. __func__, inr, extent->ram_bytes);
  99. goto out;
  100. }
  101. data_ptr = (const char *) extent
  102. + offsetof(struct btrfs_file_extent_item, disk_bytenr);
  103. memcpy(target, data_ptr, extent->ram_bytes);
  104. target[extent->ram_bytes] = '\0';
  105. res = 0;
  106. out:
  107. btrfs_free_path(&path);
  108. return res;
  109. }
  110. /* inr must be a directory (for regular files with multiple hard links this
  111. function returns only one of the parents of the file) */
  112. static u64 get_parent_inode(struct btrfs_root *root, u64 inr,
  113. struct btrfs_inode_item *inode_item)
  114. {
  115. struct btrfs_key key;
  116. u64 res;
  117. if (inr == BTRFS_FIRST_FREE_OBJECTID) {
  118. if (root->objectid != btrfs_info.fs_root.objectid) {
  119. u64 parent;
  120. struct btrfs_root_ref ref;
  121. parent = btrfs_lookup_root_ref(root->objectid, &ref,
  122. NULL);
  123. if (parent == -1ULL)
  124. return -1ULL;
  125. if (btrfs_find_root(parent, root, NULL))
  126. return -1ULL;
  127. inr = ref.dirid;
  128. }
  129. if (inode_item) {
  130. key.objectid = inr;
  131. key.type = BTRFS_INODE_ITEM_KEY;
  132. key.offset = 0;
  133. if (btrfs_lookup_inode(root, &key, inode_item, NULL))
  134. return -1ULL;
  135. }
  136. return inr;
  137. }
  138. res = btrfs_lookup_inode_ref(root, inr, NULL, NULL);
  139. if (res == -1ULL)
  140. return -1ULL;
  141. if (inode_item) {
  142. key.objectid = res;
  143. key.type = BTRFS_INODE_ITEM_KEY;
  144. key.offset = 0;
  145. if (btrfs_lookup_inode(root, &key, inode_item, NULL))
  146. return -1ULL;
  147. }
  148. return res;
  149. }
  150. static inline int next_length(const char *path)
  151. {
  152. int res = 0;
  153. while (*path != '\0' && *path != '/' && res <= BTRFS_NAME_LEN)
  154. ++res, ++path;
  155. return res;
  156. }
  157. static inline const char *skip_current_directories(const char *cur)
  158. {
  159. while (1) {
  160. if (cur[0] == '/')
  161. ++cur;
  162. else if (cur[0] == '.' && cur[1] == '/')
  163. cur += 2;
  164. else
  165. break;
  166. }
  167. return cur;
  168. }
  169. u64 btrfs_lookup_path(struct btrfs_root *root, u64 inr, const char *path,
  170. u8 *type_p, struct btrfs_inode_item *inode_item_p,
  171. int symlink_limit)
  172. {
  173. struct btrfs_dir_item item;
  174. struct btrfs_inode_item inode_item;
  175. u8 type = BTRFS_FT_DIR;
  176. int len, have_inode = 0;
  177. const char *cur = path;
  178. if (*cur == '/') {
  179. ++cur;
  180. inr = root->root_dirid;
  181. }
  182. do {
  183. cur = skip_current_directories(cur);
  184. len = next_length(cur);
  185. if (len > BTRFS_NAME_LEN) {
  186. printf("%s: Name too long at \"%.*s\"\n", __func__,
  187. BTRFS_NAME_LEN, cur);
  188. return -1ULL;
  189. }
  190. if (len == 1 && cur[0] == '.')
  191. break;
  192. if (len == 2 && cur[0] == '.' && cur[1] == '.') {
  193. cur += 2;
  194. inr = get_parent_inode(root, inr, &inode_item);
  195. if (inr == -1ULL)
  196. return -1ULL;
  197. type = BTRFS_FT_DIR;
  198. continue;
  199. }
  200. if (!*cur)
  201. break;
  202. if (btrfs_lookup_dir_item(root, inr, cur, len, &item))
  203. return -1ULL;
  204. type = item.type;
  205. have_inode = 1;
  206. if (btrfs_lookup_inode(root, &item.location, &inode_item, root))
  207. return -1ULL;
  208. if (item.type == BTRFS_FT_SYMLINK && symlink_limit >= 0) {
  209. char *target;
  210. if (!symlink_limit) {
  211. printf("%s: Too much symlinks!\n", __func__);
  212. return -1ULL;
  213. }
  214. target = malloc(min(inode_item.size + 1,
  215. (u64) btrfs_info.sb.sectorsize));
  216. if (!target)
  217. return -1ULL;
  218. if (btrfs_readlink(root, item.location.objectid,
  219. target)) {
  220. free(target);
  221. return -1ULL;
  222. }
  223. inr = btrfs_lookup_path(root, inr, target, &type,
  224. &inode_item, symlink_limit - 1);
  225. free(target);
  226. if (inr == -1ULL)
  227. return -1ULL;
  228. } else if (item.type != BTRFS_FT_DIR && cur[len]) {
  229. printf("%s: \"%.*s\" not a directory\n", __func__,
  230. (int) (cur - path + len), path);
  231. return -1ULL;
  232. } else {
  233. inr = item.location.objectid;
  234. }
  235. cur += len;
  236. } while (*cur);
  237. if (type_p)
  238. *type_p = type;
  239. if (inode_item_p) {
  240. if (!have_inode) {
  241. struct btrfs_key key;
  242. key.objectid = inr;
  243. key.type = BTRFS_INODE_ITEM_KEY;
  244. key.offset = 0;
  245. if (btrfs_lookup_inode(root, &key, &inode_item, NULL))
  246. return -1ULL;
  247. }
  248. *inode_item_p = inode_item;
  249. }
  250. return inr;
  251. }
  252. u64 btrfs_file_read(const struct btrfs_root *root, u64 inr, u64 offset,
  253. u64 size, char *buf)
  254. {
  255. struct btrfs_path path;
  256. struct btrfs_key key;
  257. struct btrfs_file_extent_item *extent;
  258. int res = 0;
  259. u64 rd, rd_all = -1ULL;
  260. key.objectid = inr;
  261. key.type = BTRFS_EXTENT_DATA_KEY;
  262. key.offset = offset;
  263. if (btrfs_search_tree(root, &key, &path))
  264. return -1ULL;
  265. if (btrfs_comp_keys(&key, btrfs_path_leaf_key(&path)) < 0) {
  266. if (btrfs_prev_slot(&path))
  267. goto out;
  268. if (btrfs_comp_keys_type(&key, btrfs_path_leaf_key(&path)))
  269. goto out;
  270. }
  271. rd_all = 0;
  272. do {
  273. if (btrfs_comp_keys_type(&key, btrfs_path_leaf_key(&path)))
  274. break;
  275. extent = btrfs_path_item_ptr(&path,
  276. struct btrfs_file_extent_item);
  277. if (extent->type == BTRFS_FILE_EXTENT_INLINE) {
  278. btrfs_file_extent_item_to_cpu_inl(extent);
  279. rd = btrfs_read_extent_inline(&path, extent, offset,
  280. size, buf);
  281. } else {
  282. btrfs_file_extent_item_to_cpu(extent);
  283. rd = btrfs_read_extent_reg(&path, extent, offset, size,
  284. buf);
  285. }
  286. if (rd == -1ULL) {
  287. printf("%s: Error reading extent\n", __func__);
  288. rd_all = -1;
  289. goto out;
  290. }
  291. offset = 0;
  292. buf += rd;
  293. rd_all += rd;
  294. size -= rd;
  295. if (!size)
  296. break;
  297. } while (!(res = btrfs_next_slot(&path)));
  298. if (res)
  299. return -1ULL;
  300. out:
  301. btrfs_free_path(&path);
  302. return rd_all;
  303. }