features.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Feature set bits and string conversion.
  4. * Inspired by ext4's features compat/incompat/ro_compat related code.
  5. *
  6. * Copyright 2020 Coly Li <colyli@suse.de>
  7. *
  8. */
  9. #include "bcache_ondisk.h"
  10. #include "bcache.h"
  11. #include "features.h"
  12. struct feature {
  13. int compat;
  14. unsigned int mask;
  15. const char *string;
  16. };
  17. static struct feature feature_list[] = {
  18. {BCH_FEATURE_INCOMPAT, BCH_FEATURE_INCOMPAT_LOG_LARGE_BUCKET_SIZE,
  19. "large_bucket"},
  20. {0, 0, NULL },
  21. };
  22. #define compose_feature_string(type) \
  23. ({ \
  24. struct feature *f; \
  25. bool first = true; \
  26. \
  27. for (f = &feature_list[0]; f->compat != 0; f++) { \
  28. if (f->compat != BCH_FEATURE_ ## type) \
  29. continue; \
  30. if (BCH_HAS_ ## type ## _FEATURE(&c->cache->sb, f->mask)) { \
  31. if (first) { \
  32. out += snprintf(out, buf + size - out, \
  33. "["); \
  34. } else { \
  35. out += snprintf(out, buf + size - out, \
  36. " ["); \
  37. } \
  38. } else if (!first) { \
  39. out += snprintf(out, buf + size - out, " "); \
  40. } \
  41. \
  42. out += snprintf(out, buf + size - out, "%s", f->string);\
  43. \
  44. if (BCH_HAS_ ## type ## _FEATURE(&c->cache->sb, f->mask)) \
  45. out += snprintf(out, buf + size - out, "]"); \
  46. \
  47. first = false; \
  48. } \
  49. if (!first) \
  50. out += snprintf(out, buf + size - out, "\n"); \
  51. })
  52. int bch_print_cache_set_feature_compat(struct cache_set *c, char *buf, int size)
  53. {
  54. char *out = buf;
  55. compose_feature_string(COMPAT);
  56. return out - buf;
  57. }
  58. int bch_print_cache_set_feature_ro_compat(struct cache_set *c, char *buf, int size)
  59. {
  60. char *out = buf;
  61. compose_feature_string(RO_COMPAT);
  62. return out - buf;
  63. }
  64. int bch_print_cache_set_feature_incompat(struct cache_set *c, char *buf, int size)
  65. {
  66. char *out = buf;
  67. compose_feature_string(INCOMPAT);
  68. return out - buf;
  69. }