Skip to content

Commit ce4c3e1

Browse files
andy-shevaxboe
authored andcommitted
bcache: Replace bch_read_string_list() by __sysfs_match_string()
Kernel library has a common function to match user input from sysfs against an array of strings. Thus, replace bch_read_string_list() by __sysfs_match_string(). Signed-off-by: Andy Shevchenko <[email protected]> Signed-off-by: Coly Li <[email protected]> Signed-off-by: Jens Axboe <[email protected]>
1 parent ecb37ce commit ce4c3e1

File tree

1 file changed

+9
-35
lines changed

1 file changed

+9
-35
lines changed

drivers/md/bcache/sysfs.c

Lines changed: 9 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
/* Default is -1; we skip past it for struct cached_dev's cache mode */
2020
static const char * const bch_cache_modes[] = {
21-
"default",
2221
"writethrough",
2322
"writeback",
2423
"writearound",
@@ -28,7 +27,6 @@ static const char * const bch_cache_modes[] = {
2827

2928
/* Default is -1; we skip past it for stop_when_cache_set_failed */
3029
static const char * const bch_stop_on_failure_modes[] = {
31-
"default",
3230
"auto",
3331
"always",
3432
NULL
@@ -146,27 +144,6 @@ static ssize_t bch_snprint_string_list(char *buf, size_t size, const char * cons
146144
return out - buf;
147145
}
148146

149-
static ssize_t bch_read_string_list(const char *buf, const char * const list[])
150-
{
151-
size_t i;
152-
char *s, *d = kstrndup(buf, PAGE_SIZE - 1, GFP_KERNEL);
153-
if (!d)
154-
return -ENOMEM;
155-
156-
s = strim(d);
157-
158-
for (i = 0; list[i]; i++)
159-
if (!strcmp(list[i], s))
160-
break;
161-
162-
kfree(d);
163-
164-
if (!list[i])
165-
return -EINVAL;
166-
167-
return i;
168-
}
169-
170147
SHOW(__bch_cached_dev)
171148
{
172149
struct cached_dev *dc = container_of(kobj, struct cached_dev,
@@ -177,12 +154,12 @@ SHOW(__bch_cached_dev)
177154

178155
if (attr == &sysfs_cache_mode)
179156
return bch_snprint_string_list(buf, PAGE_SIZE,
180-
bch_cache_modes + 1,
157+
bch_cache_modes,
181158
BDEV_CACHE_MODE(&dc->sb));
182159

183160
if (attr == &sysfs_stop_when_cache_set_failed)
184161
return bch_snprint_string_list(buf, PAGE_SIZE,
185-
bch_stop_on_failure_modes + 1,
162+
bch_stop_on_failure_modes,
186163
dc->stop_when_cache_set_failed);
187164

188165

@@ -306,8 +283,7 @@ STORE(__cached_dev)
306283
bch_cached_dev_run(dc);
307284

308285
if (attr == &sysfs_cache_mode) {
309-
v = bch_read_string_list(buf, bch_cache_modes + 1);
310-
286+
v = __sysfs_match_string(bch_cache_modes, -1, buf);
311287
if (v < 0)
312288
return v;
313289

@@ -318,8 +294,7 @@ STORE(__cached_dev)
318294
}
319295

320296
if (attr == &sysfs_stop_when_cache_set_failed) {
321-
v = bch_read_string_list(buf, bch_stop_on_failure_modes + 1);
322-
297+
v = __sysfs_match_string(bch_stop_on_failure_modes, -1, buf);
323298
if (v < 0)
324299
return v;
325300

@@ -688,6 +663,7 @@ SHOW_LOCKED(bch_cache_set)
688663
STORE(__bch_cache_set)
689664
{
690665
struct cache_set *c = container_of(kobj, struct cache_set, kobj);
666+
ssize_t v;
691667

692668
if (attr == &sysfs_unregister)
693669
bch_cache_set_unregister(c);
@@ -751,8 +727,7 @@ STORE(__bch_cache_set)
751727
c->congested_write_threshold_us);
752728

753729
if (attr == &sysfs_errors) {
754-
ssize_t v = bch_read_string_list(buf, error_actions);
755-
730+
v = __sysfs_match_string(error_actions, -1, buf);
756731
if (v < 0)
757732
return v;
758733

@@ -767,8 +742,7 @@ STORE(__bch_cache_set)
767742
c->error_decay = strtoul_or_return(buf) / 88;
768743

769744
if (attr == &sysfs_io_disable) {
770-
int v = strtoul_or_return(buf);
771-
745+
v = strtoul_or_return(buf);
772746
if (v) {
773747
if (test_and_set_bit(CACHE_SET_IO_DISABLE,
774748
&c->flags))
@@ -982,6 +956,7 @@ SHOW_LOCKED(bch_cache)
982956
STORE(__bch_cache)
983957
{
984958
struct cache *ca = container_of(kobj, struct cache, kobj);
959+
ssize_t v;
985960

986961
if (attr == &sysfs_discard) {
987962
bool v = strtoul_or_return(buf);
@@ -996,8 +971,7 @@ STORE(__bch_cache)
996971
}
997972

998973
if (attr == &sysfs_cache_replacement_policy) {
999-
ssize_t v = bch_read_string_list(buf, cache_replacement_policies);
1000-
974+
v = __sysfs_match_string(cache_replacement_policies, -1, buf);
1001975
if (v < 0)
1002976
return v;
1003977

0 commit comments

Comments
 (0)