btrfs.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * volume_id - reads filesystem label and uuid
  3. *
  4. * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
  5. * Copyright (C) 2009 Vladimir Dronnikov <dronnikov@gmail.com>
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. //kbuild:lib-$(CONFIG_FEATURE_VOLUMEID_BTRFS) += btrfs.o
  22. //config:
  23. //config:config FEATURE_VOLUMEID_BTRFS
  24. //config: bool "btrfs filesystem"
  25. //config: default y
  26. //config: depends on VOLUMEID
  27. //config: help
  28. //config: TODO
  29. //config:
  30. #include "volume_id_internal.h"
  31. #define BTRFS_UUID_SIZE 16
  32. #define BTRFS_LABEL_SIZE 256
  33. #define BTRFS_CSUM_SIZE 32
  34. #define BTRFS_FSID_SIZE 16
  35. #define BTRFS_MAGIC "_BHRfS_M"
  36. struct btrfs_dev_item {
  37. uint64_t devid;
  38. uint64_t total_bytes;
  39. uint64_t bytes_used;
  40. uint32_t io_align;
  41. uint32_t io_width;
  42. uint32_t sector_size;
  43. uint64_t type;
  44. uint64_t generation;
  45. uint64_t start_offset;
  46. uint32_t dev_group;
  47. uint8_t seek_speed;
  48. uint8_t bandwidth;
  49. uint8_t uuid[BTRFS_UUID_SIZE];
  50. uint8_t fsid[BTRFS_UUID_SIZE];
  51. } PACKED;
  52. struct btrfs_super_block {
  53. uint8_t csum[BTRFS_CSUM_SIZE];
  54. uint8_t fsid[BTRFS_FSID_SIZE]; // UUID
  55. uint64_t bytenr;
  56. uint64_t flags;
  57. uint8_t magic[8];
  58. uint64_t generation;
  59. uint64_t root;
  60. uint64_t chunk_root;
  61. uint64_t log_root;
  62. uint64_t log_root_transid;
  63. uint64_t total_bytes;
  64. uint64_t bytes_used;
  65. uint64_t root_dir_objectid;
  66. uint64_t num_devices;
  67. uint32_t sectorsize;
  68. uint32_t nodesize;
  69. uint32_t leafsize;
  70. uint32_t stripesize;
  71. uint32_t sys_chunk_array_size;
  72. uint64_t chunk_root_generation;
  73. uint64_t compat_flags;
  74. uint64_t compat_ro_flags;
  75. uint64_t incompat_flags;
  76. uint16_t csum_type;
  77. uint8_t root_level;
  78. uint8_t chunk_root_level;
  79. uint8_t log_root_level;
  80. struct btrfs_dev_item dev_item;
  81. uint8_t label[BTRFS_LABEL_SIZE]; // LABEL
  82. // ...
  83. } PACKED;
  84. int FAST_FUNC volume_id_probe_btrfs(struct volume_id *id /*,uint64_t off*/)
  85. {
  86. // btrfs has superblocks at 64K, 64M and 256G
  87. // minimum btrfs size is 256M
  88. // so we never step out the device if we analyze
  89. // the first and the second superblocks
  90. struct btrfs_super_block *sb;
  91. unsigned off = 64;
  92. while (off < 64*1024*1024) {
  93. off *= 1024;
  94. dbg("btrfs: probing at offset 0x%x", off);
  95. sb = volume_id_get_buffer(id, off, sizeof(*sb));
  96. if (sb == NULL)
  97. return -1;
  98. if (memcmp(sb->magic, BTRFS_MAGIC, 8) != 0)
  99. return -1;
  100. }
  101. // N.B.: btrfs natively supports 256 (>VOLUME_ID_LABEL_SIZE) size labels
  102. volume_id_set_label_string(id, sb->label, VOLUME_ID_LABEL_SIZE);
  103. volume_id_set_uuid(id, sb->fsid, UUID_DCE);
  104. IF_FEATURE_BLKID_TYPE(id->type = "btrfs";)
  105. return 0;
  106. }