btrfs.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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_info btrfs_info;
  15. static int readdir_callback(const struct __btrfs_root *root,
  16. struct btrfs_dir_item *item)
  17. {
  18. static const char typestr[BTRFS_FT_MAX][4] = {
  19. [BTRFS_FT_UNKNOWN] = " ? ",
  20. [BTRFS_FT_REG_FILE] = " ",
  21. [BTRFS_FT_DIR] = "DIR",
  22. [BTRFS_FT_CHRDEV] = "CHR",
  23. [BTRFS_FT_BLKDEV] = "BLK",
  24. [BTRFS_FT_FIFO] = "FIF",
  25. [BTRFS_FT_SOCK] = "SCK",
  26. [BTRFS_FT_SYMLINK] = "SYM",
  27. [BTRFS_FT_XATTR] = " ? ",
  28. };
  29. struct btrfs_inode_item inode;
  30. const char *name = (const char *) (item + 1);
  31. char filetime[32], *target = NULL;
  32. time_t mtime;
  33. if (btrfs_lookup_inode(root, (struct btrfs_key *)&item->location,
  34. &inode, NULL)) {
  35. printf("%s: Cannot find inode item for directory entry %.*s!\n",
  36. __func__, item->name_len, name);
  37. return 0;
  38. }
  39. mtime = inode.mtime.sec;
  40. ctime_r(&mtime, filetime);
  41. if (item->type == BTRFS_FT_SYMLINK) {
  42. target = malloc(min(inode.size + 1,
  43. (u64) btrfs_info.sb.sectorsize));
  44. if (target && btrfs_readlink(root, item->location.objectid,
  45. target)) {
  46. free(target);
  47. target = NULL;
  48. }
  49. if (!target)
  50. printf("%s: Cannot read symlink target!\n", __func__);
  51. }
  52. printf("<%s> ", typestr[item->type]);
  53. if (item->type == BTRFS_FT_CHRDEV || item->type == BTRFS_FT_BLKDEV)
  54. printf("%4u,%5u ", (unsigned int) (inode.rdev >> 20),
  55. (unsigned int) (inode.rdev & 0xfffff));
  56. else
  57. printf("%10llu ", inode.size);
  58. printf("%24.24s %.*s", filetime, item->name_len, name);
  59. if (item->type == BTRFS_FT_SYMLINK) {
  60. printf(" -> %s", target ? target : "?");
  61. if (target)
  62. free(target);
  63. }
  64. printf("\n");
  65. return 0;
  66. }
  67. int btrfs_probe(struct blk_desc *fs_dev_desc,
  68. struct disk_partition *fs_partition)
  69. {
  70. btrfs_blk_desc = fs_dev_desc;
  71. btrfs_part_info = fs_partition;
  72. memset(&btrfs_info, 0, sizeof(btrfs_info));
  73. btrfs_hash_init();
  74. if (btrfs_read_superblock())
  75. return -1;
  76. if (btrfs_chunk_map_init()) {
  77. printf("%s: failed to init chunk map\n", __func__);
  78. return -1;
  79. }
  80. btrfs_info.tree_root.objectid = 0;
  81. btrfs_info.tree_root.bytenr = btrfs_info.sb.root;
  82. btrfs_info.chunk_root.objectid = 0;
  83. btrfs_info.chunk_root.bytenr = btrfs_info.sb.chunk_root;
  84. if (__btrfs_read_chunk_tree()) {
  85. printf("%s: failed to read chunk tree\n", __func__);
  86. return -1;
  87. }
  88. if (btrfs_find_root(btrfs_get_default_subvol_objectid(),
  89. &btrfs_info.fs_root, NULL)) {
  90. printf("%s: failed to find default subvolume\n", __func__);
  91. return -1;
  92. }
  93. return 0;
  94. }
  95. int btrfs_ls(const char *path)
  96. {
  97. struct __btrfs_root root = btrfs_info.fs_root;
  98. u64 inr;
  99. u8 type;
  100. inr = btrfs_lookup_path(&root, root.root_dirid, path, &type, NULL, 40);
  101. if (inr == -1ULL) {
  102. printf("Cannot lookup path %s\n", path);
  103. return -1;
  104. }
  105. if (type != BTRFS_FT_DIR) {
  106. printf("Not a directory: %s\n", path);
  107. return -1;
  108. }
  109. if (btrfs_readdir(&root, inr, readdir_callback)) {
  110. printf("An error occured while listing directory %s\n", path);
  111. return -1;
  112. }
  113. return 0;
  114. }
  115. int btrfs_exists(const char *file)
  116. {
  117. struct __btrfs_root root = btrfs_info.fs_root;
  118. u64 inr;
  119. u8 type;
  120. inr = btrfs_lookup_path(&root, root.root_dirid, file, &type, NULL, 40);
  121. return (inr != -1ULL && type == BTRFS_FT_REG_FILE);
  122. }
  123. int btrfs_size(const char *file, loff_t *size)
  124. {
  125. struct __btrfs_root root = btrfs_info.fs_root;
  126. struct btrfs_inode_item inode;
  127. u64 inr;
  128. u8 type;
  129. inr = btrfs_lookup_path(&root, root.root_dirid, file, &type, &inode,
  130. 40);
  131. if (inr == -1ULL) {
  132. printf("Cannot lookup file %s\n", file);
  133. return -1;
  134. }
  135. if (type != BTRFS_FT_REG_FILE) {
  136. printf("Not a regular file: %s\n", file);
  137. return -1;
  138. }
  139. *size = inode.size;
  140. return 0;
  141. }
  142. int btrfs_read(const char *file, void *buf, loff_t offset, loff_t len,
  143. loff_t *actread)
  144. {
  145. struct __btrfs_root root = btrfs_info.fs_root;
  146. struct btrfs_inode_item inode;
  147. u64 inr, rd;
  148. u8 type;
  149. inr = btrfs_lookup_path(&root, root.root_dirid, file, &type, &inode,
  150. 40);
  151. if (inr == -1ULL) {
  152. printf("Cannot lookup file %s\n", file);
  153. return -1;
  154. }
  155. if (type != BTRFS_FT_REG_FILE) {
  156. printf("Not a regular file: %s\n", file);
  157. return -1;
  158. }
  159. if (!len)
  160. len = inode.size;
  161. if (len > inode.size - offset)
  162. len = inode.size - offset;
  163. rd = btrfs_file_read(&root, inr, offset, len, buf);
  164. if (rd == -1ULL) {
  165. printf("An error occured while reading file %s\n", file);
  166. return -1;
  167. }
  168. *actread = rd;
  169. return 0;
  170. }
  171. void btrfs_close(void)
  172. {
  173. btrfs_chunk_map_exit();
  174. }
  175. int btrfs_uuid(char *uuid_str)
  176. {
  177. #ifdef CONFIG_LIB_UUID
  178. uuid_bin_to_str(btrfs_info.sb.fsid, uuid_str, UUID_STR_FORMAT_STD);
  179. return 0;
  180. #endif
  181. return -ENOSYS;
  182. }