btrfs.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 <config.h>
  8. #include <malloc.h>
  9. #include <uuid.h>
  10. #include <linux/time.h>
  11. #include "btrfs.h"
  12. #include "crypto/hash.h"
  13. #include "disk-io.h"
  14. struct btrfs_fs_info *current_fs_info;
  15. static int show_dir(struct btrfs_root *root, struct extent_buffer *eb,
  16. struct btrfs_dir_item *di)
  17. {
  18. struct btrfs_fs_info *fs_info = root->fs_info;
  19. struct btrfs_inode_item ii;
  20. struct btrfs_key key;
  21. static const char* dir_item_str[] = {
  22. [BTRFS_FT_REG_FILE] = "FILE",
  23. [BTRFS_FT_DIR] = "DIR",
  24. [BTRFS_FT_CHRDEV] = "CHRDEV",
  25. [BTRFS_FT_BLKDEV] = "BLKDEV",
  26. [BTRFS_FT_FIFO] = "FIFO",
  27. [BTRFS_FT_SOCK] = "SOCK",
  28. [BTRFS_FT_SYMLINK] = "SYMLINK",
  29. [BTRFS_FT_XATTR] = "XATTR"
  30. };
  31. u8 type = btrfs_dir_type(eb, di);
  32. char namebuf[BTRFS_NAME_LEN];
  33. char *target = NULL;
  34. char filetime[32];
  35. time_t mtime;
  36. int ret;
  37. btrfs_dir_item_key_to_cpu(eb, di, &key);
  38. if (key.type == BTRFS_ROOT_ITEM_KEY) {
  39. struct btrfs_root *subvol;
  40. /* It's a subvolume, get its mtime from root item */
  41. subvol = btrfs_read_fs_root(fs_info, &key);
  42. if (IS_ERR(subvol)) {
  43. ret = PTR_ERR(subvol);
  44. error("Can't find root %llu", key.objectid);
  45. return ret;
  46. }
  47. mtime = btrfs_stack_timespec_sec(&subvol->root_item.otime);
  48. } else {
  49. struct btrfs_path path;
  50. /* It's regular inode, get its mtime from inode item */
  51. btrfs_init_path(&path);
  52. ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
  53. if (ret > 0)
  54. ret = -ENOENT;
  55. if (ret < 0) {
  56. error("Can't find inode %llu", key.objectid);
  57. btrfs_release_path(&path);
  58. return ret;
  59. }
  60. read_extent_buffer(path.nodes[0], &ii,
  61. btrfs_item_ptr_offset(path.nodes[0], path.slots[0]),
  62. sizeof(ii));
  63. btrfs_release_path(&path);
  64. mtime = btrfs_stack_timespec_sec(&ii.mtime);
  65. }
  66. ctime_r(&mtime, filetime);
  67. if (type == BTRFS_FT_SYMLINK) {
  68. target = malloc(fs_info->sectorsize);
  69. if (!target) {
  70. error("Can't alloc memory for symlink %llu",
  71. key.objectid);
  72. return -ENOMEM;
  73. }
  74. ret = btrfs_readlink(root, key.objectid, target);
  75. if (ret < 0) {
  76. error("Failed to read symlink %llu", key.objectid);
  77. goto out;
  78. }
  79. target[ret] = '\0';
  80. }
  81. if (type < ARRAY_SIZE(dir_item_str) && dir_item_str[type])
  82. printf("<%s> ", dir_item_str[type]);
  83. else
  84. printf("DIR_ITEM.%u", type);
  85. if (type == BTRFS_FT_CHRDEV || type == BTRFS_FT_BLKDEV) {
  86. ASSERT(key.type == BTRFS_INODE_ITEM_KEY);
  87. printf("%4llu,%5llu ", btrfs_stack_inode_rdev(&ii) >> 20,
  88. btrfs_stack_inode_rdev(&ii) & 0xfffff);
  89. } else {
  90. if (key.type == BTRFS_INODE_ITEM_KEY)
  91. printf("%10llu ", btrfs_stack_inode_size(&ii));
  92. else
  93. printf("%10llu ", 0ULL);
  94. }
  95. read_extent_buffer(eb, namebuf, (unsigned long)(di + 1),
  96. btrfs_dir_name_len(eb, di));
  97. printf("%24.24s %.*s", filetime, btrfs_dir_name_len(eb, di), namebuf);
  98. if (type == BTRFS_FT_SYMLINK)
  99. printf(" -> %s", target ? target : "?");
  100. printf("\n");
  101. out:
  102. free(target);
  103. return ret;
  104. }
  105. int btrfs_probe(struct blk_desc *fs_dev_desc,
  106. struct disk_partition *fs_partition)
  107. {
  108. struct btrfs_fs_info *fs_info;
  109. int ret = -1;
  110. btrfs_hash_init();
  111. fs_info = open_ctree_fs_info(fs_dev_desc, fs_partition);
  112. if (fs_info) {
  113. current_fs_info = fs_info;
  114. ret = 0;
  115. }
  116. return ret;
  117. }
  118. int btrfs_ls(const char *path)
  119. {
  120. struct btrfs_fs_info *fs_info = current_fs_info;
  121. struct btrfs_root *root = fs_info->fs_root;
  122. u64 ino = BTRFS_FIRST_FREE_OBJECTID;
  123. u8 type;
  124. int ret;
  125. ASSERT(fs_info);
  126. ret = btrfs_lookup_path(fs_info->fs_root, BTRFS_FIRST_FREE_OBJECTID,
  127. path, &root, &ino, &type, 40);
  128. if (ret < 0) {
  129. printf("Cannot lookup path %s\n", path);
  130. return ret;
  131. }
  132. if (type != BTRFS_FT_DIR) {
  133. error("Not a directory: %s", path);
  134. return -ENOENT;
  135. }
  136. ret = btrfs_iter_dir(root, ino, show_dir);
  137. if (ret < 0) {
  138. error("An error occured while listing directory %s", path);
  139. return ret;
  140. }
  141. return 0;
  142. }
  143. int btrfs_exists(const char *file)
  144. {
  145. struct btrfs_fs_info *fs_info = current_fs_info;
  146. struct btrfs_root *root;
  147. u64 ino;
  148. u8 type;
  149. int ret;
  150. ASSERT(fs_info);
  151. ret = btrfs_lookup_path(fs_info->fs_root, BTRFS_FIRST_FREE_OBJECTID,
  152. file, &root, &ino, &type, 40);
  153. if (ret < 0)
  154. return 0;
  155. if (type == BTRFS_FT_REG_FILE)
  156. return 1;
  157. return 0;
  158. }
  159. int btrfs_size(const char *file, loff_t *size)
  160. {
  161. struct btrfs_fs_info *fs_info = current_fs_info;
  162. struct btrfs_inode_item *ii;
  163. struct btrfs_root *root;
  164. struct btrfs_path path;
  165. struct btrfs_key key;
  166. u64 ino;
  167. u8 type;
  168. int ret;
  169. ret = btrfs_lookup_path(fs_info->fs_root, BTRFS_FIRST_FREE_OBJECTID,
  170. file, &root, &ino, &type, 40);
  171. if (ret < 0) {
  172. printf("Cannot lookup file %s\n", file);
  173. return ret;
  174. }
  175. if (type != BTRFS_FT_REG_FILE) {
  176. printf("Not a regular file: %s\n", file);
  177. return -ENOENT;
  178. }
  179. btrfs_init_path(&path);
  180. key.objectid = ino;
  181. key.type = BTRFS_INODE_ITEM_KEY;
  182. key.offset = 0;
  183. ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
  184. if (ret < 0) {
  185. printf("Cannot lookup ino %llu\n", ino);
  186. return ret;
  187. }
  188. if (ret > 0) {
  189. printf("Ino %llu does not exist\n", ino);
  190. ret = -ENOENT;
  191. goto out;
  192. }
  193. ii = btrfs_item_ptr(path.nodes[0], path.slots[0],
  194. struct btrfs_inode_item);
  195. *size = btrfs_inode_size(path.nodes[0], ii);
  196. out:
  197. btrfs_release_path(&path);
  198. return ret;
  199. }
  200. int btrfs_read(const char *file, void *buf, loff_t offset, loff_t len,
  201. loff_t *actread)
  202. {
  203. struct btrfs_fs_info *fs_info = current_fs_info;
  204. struct btrfs_root *root;
  205. loff_t real_size = 0;
  206. u64 ino;
  207. u8 type;
  208. int ret;
  209. ASSERT(fs_info);
  210. ret = btrfs_lookup_path(fs_info->fs_root, BTRFS_FIRST_FREE_OBJECTID,
  211. file, &root, &ino, &type, 40);
  212. if (ret < 0) {
  213. error("Cannot lookup file %s", file);
  214. return ret;
  215. }
  216. if (type != BTRFS_FT_REG_FILE) {
  217. error("Not a regular file: %s", file);
  218. return -EINVAL;
  219. }
  220. if (!len) {
  221. ret = btrfs_size(file, &real_size);
  222. if (ret < 0) {
  223. error("Failed to get inode size: %s", file);
  224. return ret;
  225. }
  226. len = real_size;
  227. }
  228. if (len > real_size - offset)
  229. len = real_size - offset;
  230. ret = btrfs_file_read(root, ino, offset, len, buf);
  231. if (ret < 0) {
  232. error("An error occured while reading file %s", file);
  233. return ret;
  234. }
  235. *actread = len;
  236. return 0;
  237. }
  238. void btrfs_close(void)
  239. {
  240. if (current_fs_info) {
  241. close_ctree_fs_info(current_fs_info);
  242. current_fs_info = NULL;
  243. }
  244. }
  245. int btrfs_uuid(char *uuid_str)
  246. {
  247. #ifdef CONFIG_LIB_UUID
  248. if (current_fs_info)
  249. uuid_bin_to_str(current_fs_info->super_copy->fsid, uuid_str,
  250. UUID_STR_FORMAT_STD);
  251. return 0;
  252. #endif
  253. return -ENOSYS;
  254. }