btrfs.c 4.7 KB

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