bcache.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright (C) 2013 Rolf Fokkens <rolf@fokkens.nl>
  3. *
  4. * This file may be redistributed under the terms of the
  5. * GNU Lesser General Public License.
  6. *
  7. * Based on code fragments from bcache-tools by Kent Overstreet:
  8. * http://evilpiepirate.org/git/bcache-tools.git
  9. */
  10. //kbuild:lib-$(CONFIG_FEATURE_VOLUMEID_BCACHE) += bcache.o
  11. //config:
  12. //config:config FEATURE_VOLUMEID_BCACHE
  13. //config: bool "bcache filesystem"
  14. //config: default y
  15. //config: depends on VOLUMEID
  16. //config: help
  17. //config: TODO
  18. //config:
  19. #include "volume_id_internal.h"
  20. #define SB_LABEL_SIZE 32
  21. #define SB_JOURNAL_BUCKETS 256U
  22. static const char bcache_magic[] ALIGN1 = {
  23. 0xc6, 0x85, 0x73, 0xf6, 0x4e, 0x1a, 0x45, 0xca,
  24. 0x82, 0x65, 0xf5, 0x7f, 0x48, 0xba, 0x6d, 0x81
  25. };
  26. struct bcache_super_block {
  27. uint64_t csum;
  28. uint64_t offset; /* sector where this sb was written */
  29. uint64_t version;
  30. uint8_t magic[16];
  31. uint8_t uuid[16];
  32. union {
  33. uint8_t set_uuid[16];
  34. uint64_t set_magic;
  35. };
  36. uint8_t label[SB_LABEL_SIZE];
  37. uint64_t flags;
  38. uint64_t seq;
  39. uint64_t pad[8];
  40. union {
  41. struct {
  42. /* Cache devices */
  43. uint64_t nbuckets; /* device size */
  44. uint16_t block_size; /* sectors */
  45. uint16_t bucket_size; /* sectors */
  46. uint16_t nr_in_set;
  47. uint16_t nr_this_dev;
  48. };
  49. struct {
  50. /* Backing devices */
  51. uint64_t data_offset;
  52. /*
  53. * block_size from the cache device section is still used by
  54. * backing devices, so don't add anything here until we fix
  55. * things to not need it for backing devices anymore
  56. */
  57. };
  58. };
  59. uint32_t last_mount; /* time_t */
  60. uint16_t first_bucket;
  61. union {
  62. uint16_t njournal_buckets;
  63. uint16_t keys;
  64. };
  65. uint64_t d[SB_JOURNAL_BUCKETS]; /* journal buckets */
  66. };
  67. /* magic string */
  68. #define BCACHE_SB_MAGIC bcache_magic
  69. /* magic string len */
  70. #define BCACHE_SB_MAGIC_LEN sizeof (bcache_magic)
  71. /* super block offset */
  72. #define BCACHE_SB_OFF 0x1000
  73. /* supper block offset in kB */
  74. #define BCACHE_SB_KBOFF (BCACHE_SB_OFF >> 10)
  75. /* magic string offset within super block */
  76. #define BCACHE_SB_MAGIC_OFF offsetof (struct bcache_super_block, magic)
  77. int FAST_FUNC volume_id_probe_bcache(struct volume_id *id /*,uint64_t off*/)
  78. {
  79. struct bcache_super_block *sb;
  80. sb = volume_id_get_buffer(id, BCACHE_SB_OFF, sizeof(*sb));
  81. if (sb == NULL)
  82. return -1;
  83. if (memcmp(sb->magic, BCACHE_SB_MAGIC, BCACHE_SB_MAGIC_LEN) != 0)
  84. return -1;
  85. volume_id_set_label_string(id, sb->label, SB_LABEL_SIZE);
  86. volume_id_set_uuid(id, sb->uuid, UUID_DCE);
  87. IF_FEATURE_BLKID_TYPE(id->type = "bcache";)
  88. return 0;
  89. }