btrfs.c 4.7 KB

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