btrfs.c 4.7 KB

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