btrfs.c 4.7 KB

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