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