blob: 554e3afc9b688be1b3415d7fb1fdf407f5c192b0 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Kent Overstreetcafe5632013-03-23 16:11:31 -07002/*
3 * bcache sysfs interfaces
4 *
5 * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com>
6 * Copyright 2012 Google, Inc.
7 */
8
9#include "bcache.h"
10#include "sysfs.h"
11#include "btree.h"
12#include "request.h"
Kent Overstreet279afba2013-06-05 06:21:07 -070013#include "writeback.h"
Coly Li092bd542020-07-25 20:00:36 +080014#include "features.h"
Kent Overstreetcafe5632013-03-23 16:11:31 -070015
Kent Overstreetc37511b2013-04-26 15:39:55 -070016#include <linux/blkdev.h>
Kent Overstreetcafe5632013-03-23 16:11:31 -070017#include <linux/sort.h>
Ingo Molnare6017572017-02-01 16:36:40 +010018#include <linux/sched/clock.h>
Kent Overstreetcafe5632013-03-23 16:11:31 -070019
Coly Lia59ff6c2019-06-28 19:59:46 +080020extern bool bcache_is_reboot;
21
Shenghui Wang4e361e02018-12-13 22:53:49 +080022/* Default is 0 ("writethrough") */
Andy Shevchenko04cbc212018-05-28 15:37:42 +080023static const char * const bch_cache_modes[] = {
Andy Shevchenko04cbc212018-05-28 15:37:42 +080024 "writethrough",
25 "writeback",
26 "writearound",
Coly Li20621fe2019-08-09 14:14:05 +080027 "none",
28 NULL
Andy Shevchenko04cbc212018-05-28 15:37:42 +080029};
30
Coly Li038ba8c2020-02-01 22:42:33 +080031static const char * const bch_reada_cache_policies[] = {
32 "all",
33 "meta-only",
34 NULL
35};
36
Shenghui Wang4e361e02018-12-13 22:53:49 +080037/* Default is 0 ("auto") */
Andy Shevchenko04cbc212018-05-28 15:37:42 +080038static const char * const bch_stop_on_failure_modes[] = {
Andy Shevchenko04cbc212018-05-28 15:37:42 +080039 "auto",
Coly Li20621fe2019-08-09 14:14:05 +080040 "always",
41 NULL
Andy Shevchenko04cbc212018-05-28 15:37:42 +080042};
43
Kent Overstreetcafe5632013-03-23 16:11:31 -070044static const char * const cache_replacement_policies[] = {
45 "lru",
46 "fifo",
Coly Li20621fe2019-08-09 14:14:05 +080047 "random",
48 NULL
Kent Overstreetcafe5632013-03-23 16:11:31 -070049};
50
Kent Overstreet77c320e2013-07-11 19:42:51 -070051static const char * const error_actions[] = {
52 "unregister",
Coly Li20621fe2019-08-09 14:14:05 +080053 "panic",
54 NULL
Kent Overstreet77c320e2013-07-11 19:42:51 -070055};
56
Kent Overstreetcafe5632013-03-23 16:11:31 -070057write_attribute(attach);
58write_attribute(detach);
59write_attribute(unregister);
60write_attribute(stop);
61write_attribute(clear_stats);
62write_attribute(trigger_gc);
63write_attribute(prune_cache);
64write_attribute(flash_vol_create);
65
66read_attribute(bucket_size);
67read_attribute(block_size);
68read_attribute(nbuckets);
69read_attribute(tree_depth);
70read_attribute(root_usage_percent);
71read_attribute(priority_stats);
72read_attribute(btree_cache_size);
73read_attribute(btree_cache_max_chain);
74read_attribute(cache_available_percent);
75read_attribute(written);
76read_attribute(btree_written);
77read_attribute(metadata_written);
78read_attribute(active_journal_entries);
Coly Li926d1942019-02-09 12:52:55 +080079read_attribute(backing_dev_name);
Coly Lid4610452019-02-09 12:52:56 +080080read_attribute(backing_dev_uuid);
Kent Overstreetcafe5632013-03-23 16:11:31 -070081
82sysfs_time_stats_attribute(btree_gc, sec, ms);
83sysfs_time_stats_attribute(btree_split, sec, us);
84sysfs_time_stats_attribute(btree_sort, ms, us);
85sysfs_time_stats_attribute(btree_read, ms, us);
Kent Overstreetcafe5632013-03-23 16:11:31 -070086
87read_attribute(btree_nodes);
88read_attribute(btree_used_percent);
89read_attribute(average_key_size);
90read_attribute(dirty_data);
91read_attribute(bset_tree_stats);
Coly Li092bd542020-07-25 20:00:36 +080092read_attribute(feature_compat);
93read_attribute(feature_ro_compat);
94read_attribute(feature_incompat);
Kent Overstreetcafe5632013-03-23 16:11:31 -070095
96read_attribute(state);
97read_attribute(cache_read_races);
Tang Junhuia728eac2018-02-07 11:41:39 -080098read_attribute(reclaim);
Coly Lidff90d52019-06-28 20:00:00 +080099read_attribute(reclaimed_journal_buckets);
Tang Junhuia728eac2018-02-07 11:41:39 -0800100read_attribute(flush_write);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700101read_attribute(writeback_keys_done);
102read_attribute(writeback_keys_failed);
103read_attribute(io_errors);
104read_attribute(congested);
Coly Li9aaf5162018-12-13 22:53:55 +0800105read_attribute(cutoff_writeback);
106read_attribute(cutoff_writeback_sync);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700107rw_attribute(congested_read_threshold_us);
108rw_attribute(congested_write_threshold_us);
109
110rw_attribute(sequential_cutoff);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700111rw_attribute(data_csum);
112rw_attribute(cache_mode);
Coly Li038ba8c2020-02-01 22:42:33 +0800113rw_attribute(readahead_cache_policy);
Coly Li7e027ca2018-03-18 17:36:18 -0700114rw_attribute(stop_when_cache_set_failed);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700115rw_attribute(writeback_metadata);
116rw_attribute(writeback_running);
117rw_attribute(writeback_percent);
118rw_attribute(writeback_delay);
119rw_attribute(writeback_rate);
120
121rw_attribute(writeback_rate_update_seconds);
Michael Lyle1d316e62017-10-13 16:35:36 -0700122rw_attribute(writeback_rate_i_term_inverse);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700123rw_attribute(writeback_rate_p_term_inverse);
Michael Lyle1d316e62017-10-13 16:35:36 -0700124rw_attribute(writeback_rate_minimum);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700125read_attribute(writeback_rate_debug);
126
Kent Overstreet72c27062013-06-05 06:24:39 -0700127read_attribute(stripe_size);
128read_attribute(partial_stripes_expensive);
129
Kent Overstreetcafe5632013-03-23 16:11:31 -0700130rw_attribute(synchronous);
131rw_attribute(journal_delay_ms);
Coly Li771f3932018-03-18 17:36:17 -0700132rw_attribute(io_disable);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700133rw_attribute(discard);
134rw_attribute(running);
135rw_attribute(label);
136rw_attribute(readahead);
Kent Overstreet77c320e2013-07-11 19:42:51 -0700137rw_attribute(errors);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700138rw_attribute(io_error_limit);
139rw_attribute(io_error_halflife);
140rw_attribute(verify);
Kent Overstreet5ceaaad2013-09-10 14:27:42 -0700141rw_attribute(bypass_torture_test);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700142rw_attribute(key_merging_disabled);
143rw_attribute(gc_always_rewrite);
Kent Overstreet280481d2013-10-24 16:36:03 -0700144rw_attribute(expensive_debug_checks);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700145rw_attribute(cache_replacement_policy);
146rw_attribute(btree_shrinker_disabled);
147rw_attribute(copy_gc_enabled);
Coly Lic5fcded2019-11-13 16:03:23 +0800148rw_attribute(idle_max_writeback_rate);
Coly Li7a671d82018-12-13 22:53:53 +0800149rw_attribute(gc_after_writeback);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700150rw_attribute(size);
151
Coly Lib0d30982018-08-11 13:19:47 +0800152static ssize_t bch_snprint_string_list(char *buf,
153 size_t size,
154 const char * const list[],
155 size_t selected)
Andy Shevchenkoecb37ce2018-05-28 15:37:43 +0800156{
157 char *out = buf;
158 size_t i;
159
160 for (i = 0; list[i]; i++)
Takashi Iwai9876e3862020-03-22 14:03:03 +0800161 out += scnprintf(out, buf + size - out,
Andy Shevchenkoecb37ce2018-05-28 15:37:43 +0800162 i == selected ? "[%s] " : "%s ", list[i]);
163
164 out[-1] = '\n';
165 return out - buf;
166}
167
Kent Overstreetcafe5632013-03-23 16:11:31 -0700168SHOW(__bch_cached_dev)
169{
170 struct cached_dev *dc = container_of(kobj, struct cached_dev,
171 disk.kobj);
Coly Lie1f08f12018-08-11 13:19:56 +0800172 char const *states[] = { "no cache", "clean", "dirty", "inconsistent" };
Coly Lib4cb6ef2018-08-09 15:48:43 +0800173 int wb = dc->writeback_running;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700174
175#define var(stat) (dc->stat)
176
177 if (attr == &sysfs_cache_mode)
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600178 return bch_snprint_string_list(buf, PAGE_SIZE,
Andy Shevchenkoce4c3e12018-05-28 15:37:44 +0800179 bch_cache_modes,
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600180 BDEV_CACHE_MODE(&dc->sb));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700181
Coly Li038ba8c2020-02-01 22:42:33 +0800182 if (attr == &sysfs_readahead_cache_policy)
183 return bch_snprint_string_list(buf, PAGE_SIZE,
184 bch_reada_cache_policies,
185 dc->cache_readahead_policy);
186
Coly Li7e027ca2018-03-18 17:36:18 -0700187 if (attr == &sysfs_stop_when_cache_set_failed)
188 return bch_snprint_string_list(buf, PAGE_SIZE,
Andy Shevchenkoce4c3e12018-05-28 15:37:44 +0800189 bch_stop_on_failure_modes,
Coly Li7e027ca2018-03-18 17:36:18 -0700190 dc->stop_when_cache_set_failed);
191
192
Kent Overstreetcafe5632013-03-23 16:11:31 -0700193 sysfs_printf(data_csum, "%i", dc->disk.data_csum);
194 var_printf(verify, "%i");
Kent Overstreet5ceaaad2013-09-10 14:27:42 -0700195 var_printf(bypass_torture_test, "%i");
Kent Overstreetcafe5632013-03-23 16:11:31 -0700196 var_printf(writeback_metadata, "%i");
197 var_printf(writeback_running, "%i");
198 var_print(writeback_delay);
199 var_print(writeback_percent);
Coly Liea8c53562018-08-09 15:48:49 +0800200 sysfs_hprint(writeback_rate,
201 wb ? atomic_long_read(&dc->writeback_rate.rate) << 9 : 0);
Coly Li546199982019-06-28 19:59:43 +0800202 sysfs_printf(io_errors, "%i", atomic_read(&dc->io_errors));
Coly Lic7b7bd02018-03-18 17:36:25 -0700203 sysfs_printf(io_error_limit, "%i", dc->error_limit);
204 sysfs_printf(io_disable, "%i", dc->io_disable);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700205 var_print(writeback_rate_update_seconds);
Michael Lyle1d316e62017-10-13 16:35:36 -0700206 var_print(writeback_rate_i_term_inverse);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700207 var_print(writeback_rate_p_term_inverse);
Michael Lyle1d316e62017-10-13 16:35:36 -0700208 var_print(writeback_rate_minimum);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700209
210 if (attr == &sysfs_writeback_rate_debug) {
Kent Overstreet16749c22013-11-11 13:58:34 -0800211 char rate[20];
Kent Overstreetcafe5632013-03-23 16:11:31 -0700212 char dirty[20];
Kent Overstreetcafe5632013-03-23 16:11:31 -0700213 char target[20];
Kent Overstreet16749c22013-11-11 13:58:34 -0800214 char proportional[20];
Michael Lyle1d316e62017-10-13 16:35:36 -0700215 char integral[20];
Kent Overstreet16749c22013-11-11 13:58:34 -0800216 char change[20];
217 s64 next_io;
218
Coly Lib4cb6ef2018-08-09 15:48:43 +0800219 /*
220 * Except for dirty and target, other values should
221 * be 0 if writeback is not running.
222 */
Coly Liea8c53562018-08-09 15:48:49 +0800223 bch_hprint(rate,
224 wb ? atomic_long_read(&dc->writeback_rate.rate) << 9
225 : 0);
Coly Lib4cb6ef2018-08-09 15:48:43 +0800226 bch_hprint(dirty, bcache_dev_sectors_dirty(&dc->disk) << 9);
227 bch_hprint(target, dc->writeback_rate_target << 9);
228 bch_hprint(proportional,
229 wb ? dc->writeback_rate_proportional << 9 : 0);
230 bch_hprint(integral,
231 wb ? dc->writeback_rate_integral_scaled << 9 : 0);
232 bch_hprint(change, wb ? dc->writeback_rate_change << 9 : 0);
233 next_io = wb ? div64_s64(dc->writeback_rate.next-local_clock(),
234 NSEC_PER_MSEC) : 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700235
236 return sprintf(buf,
Kent Overstreet16749c22013-11-11 13:58:34 -0800237 "rate:\t\t%s/sec\n"
Kent Overstreetcafe5632013-03-23 16:11:31 -0700238 "dirty:\t\t%s\n"
Kent Overstreet16749c22013-11-11 13:58:34 -0800239 "target:\t\t%s\n"
240 "proportional:\t%s\n"
Michael Lyle1d316e62017-10-13 16:35:36 -0700241 "integral:\t%s\n"
Kent Overstreet16749c22013-11-11 13:58:34 -0800242 "change:\t\t%s/sec\n"
243 "next io:\t%llims\n",
244 rate, dirty, target, proportional,
Michael Lyle1d316e62017-10-13 16:35:36 -0700245 integral, change, next_io);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700246 }
247
248 sysfs_hprint(dirty_data,
Kent Overstreet279afba2013-06-05 06:21:07 -0700249 bcache_dev_sectors_dirty(&dc->disk) << 9);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700250
Tang Junhui688892b2018-03-18 17:36:20 -0700251 sysfs_hprint(stripe_size, ((uint64_t)dc->disk.stripe_size) << 9);
Kent Overstreet72c27062013-06-05 06:24:39 -0700252 var_printf(partial_stripes_expensive, "%u");
253
Kent Overstreetcafe5632013-03-23 16:11:31 -0700254 var_hprint(sequential_cutoff);
255 var_hprint(readahead);
256
257 sysfs_print(running, atomic_read(&dc->running));
258 sysfs_print(state, states[BDEV_STATE(&dc->sb)]);
259
260 if (attr == &sysfs_label) {
261 memcpy(buf, dc->sb.label, SB_LABEL_SIZE);
262 buf[SB_LABEL_SIZE + 1] = '\0';
263 strcat(buf, "\n");
264 return strlen(buf);
265 }
266
Coly Li926d1942019-02-09 12:52:55 +0800267 if (attr == &sysfs_backing_dev_name) {
268 snprintf(buf, BDEVNAME_SIZE + 1, "%s", dc->backing_dev_name);
269 strcat(buf, "\n");
270 return strlen(buf);
271 }
272
Coly Lid4610452019-02-09 12:52:56 +0800273 if (attr == &sysfs_backing_dev_uuid) {
274 /* convert binary uuid into 36-byte string plus '\0' */
275 snprintf(buf, 36+1, "%pU", dc->sb.uuid);
276 strcat(buf, "\n");
277 return strlen(buf);
278 }
279
Kent Overstreetcafe5632013-03-23 16:11:31 -0700280#undef var
281 return 0;
282}
283SHOW_LOCKED(bch_cached_dev)
284
285STORE(__cached_dev)
286{
287 struct cached_dev *dc = container_of(kobj, struct cached_dev,
288 disk.kobj);
Tang Junhui7f4fc932018-02-07 11:41:45 -0800289 ssize_t v;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700290 struct cache_set *c;
Gabriel de Perthuisab9e1402013-06-09 00:54:48 +0200291 struct kobj_uevent_env *env;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700292
Coly Lia59ff6c2019-06-28 19:59:46 +0800293 /* no user space access if system is rebooting */
294 if (bcache_is_reboot)
295 return -EBUSY;
296
Kent Overstreetcafe5632013-03-23 16:11:31 -0700297#define d_strtoul(var) sysfs_strtoul(var, dc->var)
Kent Overstreet16749c22013-11-11 13:58:34 -0800298#define d_strtoul_nonzero(var) sysfs_strtoul_clamp(var, dc->var, 1, INT_MAX)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700299#define d_strtoi_h(var) sysfs_hatoi(var, dc->var)
300
301 sysfs_strtoul(data_csum, dc->disk.data_csum);
302 d_strtoul(verify);
Coly Lif5c0b952019-02-09 12:53:03 +0800303 sysfs_strtoul_bool(bypass_torture_test, dc->bypass_torture_test);
304 sysfs_strtoul_bool(writeback_metadata, dc->writeback_metadata);
305 sysfs_strtoul_bool(writeback_running, dc->writeback_running);
Coly Li369d21a2019-02-09 12:53:04 +0800306 sysfs_strtoul_clamp(writeback_delay, dc->writeback_delay, 0, UINT_MAX);
Kent Overstreet16749c22013-11-11 13:58:34 -0800307
Coly Licc38ca72018-12-13 22:53:56 +0800308 sysfs_strtoul_clamp(writeback_percent, dc->writeback_percent,
309 0, bch_cutoff_writeback);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700310
Coly Liea8c53562018-08-09 15:48:49 +0800311 if (attr == &sysfs_writeback_rate) {
Coly Li46451872018-08-10 23:45:50 +0800312 ssize_t ret;
313 long int v = atomic_long_read(&dc->writeback_rate.rate);
Coly Liea8c53562018-08-09 15:48:49 +0800314
Coly Li46451872018-08-10 23:45:50 +0800315 ret = strtoul_safe_clamp(buf, v, 1, INT_MAX);
316
317 if (!ret) {
318 atomic_long_set(&dc->writeback_rate.rate, v);
319 ret = size;
320 }
321
322 return ret;
Coly Liea8c53562018-08-09 15:48:49 +0800323 }
Kent Overstreet16749c22013-11-11 13:58:34 -0800324
Coly Li7a5e3ec2018-02-07 11:41:44 -0800325 sysfs_strtoul_clamp(writeback_rate_update_seconds,
326 dc->writeback_rate_update_seconds,
327 1, WRITEBACK_RATE_UPDATE_SECS_MAX);
Coly Lic3b75a22019-02-09 12:53:05 +0800328 sysfs_strtoul_clamp(writeback_rate_i_term_inverse,
329 dc->writeback_rate_i_term_inverse,
330 1, UINT_MAX);
Coly Li5b5fd3c2019-02-09 12:53:06 +0800331 sysfs_strtoul_clamp(writeback_rate_p_term_inverse,
332 dc->writeback_rate_p_term_inverse,
333 1, UINT_MAX);
Coly Lidab71b22019-02-09 12:53:07 +0800334 sysfs_strtoul_clamp(writeback_rate_minimum,
335 dc->writeback_rate_minimum,
336 1, UINT_MAX);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700337
Coly Lic7b7bd02018-03-18 17:36:25 -0700338 sysfs_strtoul_clamp(io_error_limit, dc->error_limit, 0, INT_MAX);
339
340 if (attr == &sysfs_io_disable) {
341 int v = strtoul_or_return(buf);
342
343 dc->io_disable = v ? 1 : 0;
344 }
345
Coly Li8c27a392019-02-09 12:53:01 +0800346 sysfs_strtoul_clamp(sequential_cutoff,
347 dc->sequential_cutoff,
348 0, UINT_MAX);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700349 d_strtoi_h(readahead);
350
351 if (attr == &sysfs_clear_stats)
352 bch_cache_accounting_clear(&dc->accounting);
353
354 if (attr == &sysfs_running &&
Coly Li0b13efe2019-06-28 19:59:33 +0800355 strtoul_or_return(buf)) {
356 v = bch_cached_dev_run(dc);
357 if (v)
358 return v;
359 }
Kent Overstreetcafe5632013-03-23 16:11:31 -0700360
361 if (attr == &sysfs_cache_mode) {
Coly Li20621fe2019-08-09 14:14:05 +0800362 v = __sysfs_match_string(bch_cache_modes, -1, buf);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700363 if (v < 0)
364 return v;
365
Coly Li6f10f7d2018-08-11 13:19:44 +0800366 if ((unsigned int) v != BDEV_CACHE_MODE(&dc->sb)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700367 SET_BDEV_CACHE_MODE(&dc->sb, v);
368 bch_write_bdev_super(dc, NULL);
369 }
370 }
371
Coly Li038ba8c2020-02-01 22:42:33 +0800372 if (attr == &sysfs_readahead_cache_policy) {
373 v = __sysfs_match_string(bch_reada_cache_policies, -1, buf);
374 if (v < 0)
375 return v;
376
377 if ((unsigned int) v != dc->cache_readahead_policy)
378 dc->cache_readahead_policy = v;
379 }
380
Coly Li7e027ca2018-03-18 17:36:18 -0700381 if (attr == &sysfs_stop_when_cache_set_failed) {
Coly Li20621fe2019-08-09 14:14:05 +0800382 v = __sysfs_match_string(bch_stop_on_failure_modes, -1, buf);
Coly Li7e027ca2018-03-18 17:36:18 -0700383 if (v < 0)
384 return v;
385
386 dc->stop_when_cache_set_failed = v;
387 }
388
Kent Overstreetcafe5632013-03-23 16:11:31 -0700389 if (attr == &sysfs_label) {
Gabriel de Perthuisaee6f1c2013-09-23 23:17:28 -0700390 if (size > SB_LABEL_SIZE)
391 return -EINVAL;
392 memcpy(dc->sb.label, buf, size);
393 if (size < SB_LABEL_SIZE)
394 dc->sb.label[size] = '\0';
395 if (size && dc->sb.label[size - 1] == '\n')
396 dc->sb.label[size - 1] = '\0';
Kent Overstreetcafe5632013-03-23 16:11:31 -0700397 bch_write_bdev_super(dc, NULL);
398 if (dc->disk.c) {
399 memcpy(dc->disk.c->uuids[dc->disk.id].label,
400 buf, SB_LABEL_SIZE);
401 bch_uuid_write(dc->disk.c);
402 }
Gabriel de Perthuisab9e1402013-06-09 00:54:48 +0200403 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
Dan Carpenterd2a65ce2013-07-05 09:05:46 +0300404 if (!env)
405 return -ENOMEM;
Gabriel de Perthuisab9e1402013-06-09 00:54:48 +0200406 add_uevent_var(env, "DRIVER=bcache");
407 add_uevent_var(env, "CACHED_UUID=%pU", dc->sb.uuid),
408 add_uevent_var(env, "CACHED_LABEL=%s", buf);
Coly Lib0d30982018-08-11 13:19:47 +0800409 kobject_uevent_env(&disk_to_dev(dc->disk.disk)->kobj,
410 KOBJ_CHANGE,
411 env->envp);
Gabriel de Perthuisab9e1402013-06-09 00:54:48 +0200412 kfree(env);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700413 }
414
415 if (attr == &sysfs_attach) {
Tang Junhui73ac1052018-02-07 11:41:46 -0800416 uint8_t set_uuid[16];
417
418 if (bch_parse_uuid(buf, set_uuid) < 16)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700419 return -EINVAL;
420
Tang Junhui7f4fc932018-02-07 11:41:45 -0800421 v = -ENOENT;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700422 list_for_each_entry(c, &bch_cache_sets, list) {
Tang Junhui73ac1052018-02-07 11:41:46 -0800423 v = bch_cached_dev_attach(dc, c, set_uuid);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700424 if (!v)
425 return size;
426 }
Shenghui Wange921efe2018-08-09 15:48:50 +0800427 if (v == -ENOENT)
Joe Perches46f5aa82020-05-27 12:01:52 +0800428 pr_err("Can't attach %s: cache set not found\n", buf);
Tang Junhui7f4fc932018-02-07 11:41:45 -0800429 return v;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700430 }
431
432 if (attr == &sysfs_detach && dc->disk.c)
433 bch_cached_dev_detach(dc);
434
435 if (attr == &sysfs_stop)
436 bcache_device_stop(&dc->disk);
437
438 return size;
439}
440
441STORE(bch_cached_dev)
442{
443 struct cached_dev *dc = container_of(kobj, struct cached_dev,
444 disk.kobj);
445
Coly Lia59ff6c2019-06-28 19:59:46 +0800446 /* no user space access if system is rebooting */
447 if (bcache_is_reboot)
448 return -EBUSY;
449
Kent Overstreetcafe5632013-03-23 16:11:31 -0700450 mutex_lock(&bch_register_lock);
451 size = __cached_dev_store(kobj, attr, buf, size);
452
Shenghui Wangf383ae32018-12-13 22:53:51 +0800453 if (attr == &sysfs_writeback_running) {
454 /* dc->writeback_running changed in __cached_dev_store() */
455 if (IS_ERR_OR_NULL(dc->writeback_thread)) {
456 /*
457 * reject setting it to 1 via sysfs if writeback
458 * kthread is not created yet.
459 */
460 if (dc->writeback_running) {
461 dc->writeback_running = false;
Joe Perches46f5aa82020-05-27 12:01:52 +0800462 pr_err("%s: failed to run non-existent writeback thread\n",
Shenghui Wangf383ae32018-12-13 22:53:51 +0800463 dc->disk.disk->disk_name);
464 }
465 } else
466 /*
467 * writeback kthread will check if dc->writeback_running
468 * is true or false.
469 */
470 bch_writeback_queue(dc);
471 }
Kent Overstreetcafe5632013-03-23 16:11:31 -0700472
Coly Li1f0ffa62019-06-10 06:13:35 +0800473 /*
474 * Only set BCACHE_DEV_WB_RUNNING when cached device attached to
475 * a cache set, otherwise it doesn't make sense.
476 */
Kent Overstreetcafe5632013-03-23 16:11:31 -0700477 if (attr == &sysfs_writeback_percent)
Coly Li1f0ffa62019-06-10 06:13:35 +0800478 if ((dc->disk.c != NULL) &&
479 (!test_and_set_bit(BCACHE_DEV_WB_RUNNING, &dc->disk.flags)))
Coly Li3fd47bf2018-03-18 17:36:16 -0700480 schedule_delayed_work(&dc->writeback_rate_update,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700481 dc->writeback_rate_update_seconds * HZ);
482
483 mutex_unlock(&bch_register_lock);
484 return size;
485}
486
487static struct attribute *bch_cached_dev_files[] = {
488 &sysfs_attach,
489 &sysfs_detach,
490 &sysfs_stop,
491#if 0
492 &sysfs_data_csum,
493#endif
494 &sysfs_cache_mode,
Coly Li038ba8c2020-02-01 22:42:33 +0800495 &sysfs_readahead_cache_policy,
Coly Li7e027ca2018-03-18 17:36:18 -0700496 &sysfs_stop_when_cache_set_failed,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700497 &sysfs_writeback_metadata,
498 &sysfs_writeback_running,
499 &sysfs_writeback_delay,
500 &sysfs_writeback_percent,
501 &sysfs_writeback_rate,
502 &sysfs_writeback_rate_update_seconds,
Michael Lyle1d316e62017-10-13 16:35:36 -0700503 &sysfs_writeback_rate_i_term_inverse,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700504 &sysfs_writeback_rate_p_term_inverse,
Ben Peddell7567c2a2018-10-08 20:41:13 +0800505 &sysfs_writeback_rate_minimum,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700506 &sysfs_writeback_rate_debug,
Coly Li546199982019-06-28 19:59:43 +0800507 &sysfs_io_errors,
Coly Lic7b7bd02018-03-18 17:36:25 -0700508 &sysfs_io_error_limit,
509 &sysfs_io_disable,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700510 &sysfs_dirty_data,
Kent Overstreet72c27062013-06-05 06:24:39 -0700511 &sysfs_stripe_size,
512 &sysfs_partial_stripes_expensive,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700513 &sysfs_sequential_cutoff,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700514 &sysfs_clear_stats,
515 &sysfs_running,
516 &sysfs_state,
517 &sysfs_label,
518 &sysfs_readahead,
519#ifdef CONFIG_BCACHE_DEBUG
520 &sysfs_verify,
Kent Overstreet5ceaaad2013-09-10 14:27:42 -0700521 &sysfs_bypass_torture_test,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700522#endif
Coly Li926d1942019-02-09 12:52:55 +0800523 &sysfs_backing_dev_name,
Coly Lid4610452019-02-09 12:52:56 +0800524 &sysfs_backing_dev_uuid,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700525 NULL
526};
527KTYPE(bch_cached_dev);
528
529SHOW(bch_flash_dev)
530{
531 struct bcache_device *d = container_of(kobj, struct bcache_device,
532 kobj);
533 struct uuid_entry *u = &d->c->uuids[d->id];
534
535 sysfs_printf(data_csum, "%i", d->data_csum);
536 sysfs_hprint(size, u->sectors << 9);
537
538 if (attr == &sysfs_label) {
539 memcpy(buf, u->label, SB_LABEL_SIZE);
540 buf[SB_LABEL_SIZE + 1] = '\0';
541 strcat(buf, "\n");
542 return strlen(buf);
543 }
544
545 return 0;
546}
547
548STORE(__bch_flash_dev)
549{
550 struct bcache_device *d = container_of(kobj, struct bcache_device,
551 kobj);
552 struct uuid_entry *u = &d->c->uuids[d->id];
553
Coly Lia59ff6c2019-06-28 19:59:46 +0800554 /* no user space access if system is rebooting */
555 if (bcache_is_reboot)
556 return -EBUSY;
557
Kent Overstreetcafe5632013-03-23 16:11:31 -0700558 sysfs_strtoul(data_csum, d->data_csum);
559
560 if (attr == &sysfs_size) {
561 uint64_t v;
Coly Li1fae7cf2018-08-11 13:19:45 +0800562
Kent Overstreetcafe5632013-03-23 16:11:31 -0700563 strtoi_h_or_return(buf, v);
564
565 u->sectors = v >> 9;
566 bch_uuid_write(d->c);
567 set_capacity(d->disk, u->sectors);
568 }
569
570 if (attr == &sysfs_label) {
571 memcpy(u->label, buf, SB_LABEL_SIZE);
572 bch_uuid_write(d->c);
573 }
574
575 if (attr == &sysfs_unregister) {
Kent Overstreetc4d951d2013-08-21 17:49:09 -0700576 set_bit(BCACHE_DEV_DETACHING, &d->flags);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700577 bcache_device_stop(d);
578 }
579
580 return size;
581}
582STORE_LOCKED(bch_flash_dev)
583
584static struct attribute *bch_flash_dev_files[] = {
585 &sysfs_unregister,
586#if 0
587 &sysfs_data_csum,
588#endif
589 &sysfs_label,
590 &sysfs_size,
591 NULL
592};
593KTYPE(bch_flash_dev);
594
Kent Overstreetf67342d2013-11-11 19:25:55 -0800595struct bset_stats_op {
596 struct btree_op op;
597 size_t nodes;
598 struct bset_stats stats;
599};
600
John Sheucb851142014-03-17 23:13:56 -0700601static int bch_btree_bset_stats(struct btree_op *b_op, struct btree *b)
Kent Overstreetf67342d2013-11-11 19:25:55 -0800602{
603 struct bset_stats_op *op = container_of(b_op, struct bset_stats_op, op);
604
605 op->nodes++;
606 bch_btree_keys_stats(&b->keys, &op->stats);
607
608 return MAP_CONTINUE;
609}
610
Kent Overstreet35723242014-01-10 18:53:02 -0800611static int bch_bset_print_stats(struct cache_set *c, char *buf)
Kent Overstreetf67342d2013-11-11 19:25:55 -0800612{
613 struct bset_stats_op op;
614 int ret;
615
616 memset(&op, 0, sizeof(op));
617 bch_btree_op_init(&op.op, -1);
618
John Sheucb851142014-03-17 23:13:56 -0700619 ret = bch_btree_map_nodes(&op.op, c, &ZERO_KEY, bch_btree_bset_stats);
Kent Overstreetf67342d2013-11-11 19:25:55 -0800620 if (ret < 0)
621 return ret;
622
623 return snprintf(buf, PAGE_SIZE,
624 "btree nodes: %zu\n"
625 "written sets: %zu\n"
626 "unwritten sets: %zu\n"
627 "written key bytes: %zu\n"
628 "unwritten key bytes: %zu\n"
629 "floats: %zu\n"
630 "failed: %zu\n",
631 op.nodes,
632 op.stats.sets_written, op.stats.sets_unwritten,
633 op.stats.bytes_written, op.stats.bytes_unwritten,
634 op.stats.floats, op.stats.failed);
635}
636
Coly Li6f10f7d2018-08-11 13:19:44 +0800637static unsigned int bch_root_usage(struct cache_set *c)
John Sheucb851142014-03-17 23:13:56 -0700638{
Coly Li6f10f7d2018-08-11 13:19:44 +0800639 unsigned int bytes = 0;
John Sheucb851142014-03-17 23:13:56 -0700640 struct bkey *k;
641 struct btree *b;
642 struct btree_iter iter;
643
644 goto lock_root;
645
646 do {
647 rw_unlock(false, b);
648lock_root:
649 b = c->root;
650 rw_lock(false, b, b->level);
651 } while (b != c->root);
652
653 for_each_key_filter(&b->keys, k, &iter, bch_ptr_bad)
654 bytes += bkey_bytes(k);
655
656 rw_unlock(false, b);
657
658 return (bytes * 100) / btree_bytes(c);
659}
660
661static size_t bch_cache_size(struct cache_set *c)
662{
663 size_t ret = 0;
664 struct btree *b;
665
666 mutex_lock(&c->bucket_lock);
667 list_for_each_entry(b, &c->btree_cache, list)
668 ret += 1 << (b->keys.page_order + PAGE_SHIFT);
669
670 mutex_unlock(&c->bucket_lock);
671 return ret;
672}
673
Coly Li6f10f7d2018-08-11 13:19:44 +0800674static unsigned int bch_cache_max_chain(struct cache_set *c)
John Sheucb851142014-03-17 23:13:56 -0700675{
Coly Li6f10f7d2018-08-11 13:19:44 +0800676 unsigned int ret = 0;
John Sheucb851142014-03-17 23:13:56 -0700677 struct hlist_head *h;
678
679 mutex_lock(&c->bucket_lock);
680
681 for (h = c->bucket_hash;
682 h < c->bucket_hash + (1 << BUCKET_HASH_BITS);
683 h++) {
Coly Li6f10f7d2018-08-11 13:19:44 +0800684 unsigned int i = 0;
John Sheucb851142014-03-17 23:13:56 -0700685 struct hlist_node *p;
686
687 hlist_for_each(p, h)
688 i++;
689
690 ret = max(ret, i);
691 }
692
693 mutex_unlock(&c->bucket_lock);
694 return ret;
695}
696
Coly Li6f10f7d2018-08-11 13:19:44 +0800697static unsigned int bch_btree_used(struct cache_set *c)
John Sheucb851142014-03-17 23:13:56 -0700698{
699 return div64_u64(c->gc_stats.key_bytes * 100,
700 (c->gc_stats.nodes ?: 1) * btree_bytes(c));
701}
702
Coly Li6f10f7d2018-08-11 13:19:44 +0800703static unsigned int bch_average_key_size(struct cache_set *c)
John Sheucb851142014-03-17 23:13:56 -0700704{
705 return c->gc_stats.nkeys
706 ? div64_u64(c->gc_stats.data, c->gc_stats.nkeys)
707 : 0;
708}
709
Kent Overstreetcafe5632013-03-23 16:11:31 -0700710SHOW(__bch_cache_set)
711{
Kent Overstreetcafe5632013-03-23 16:11:31 -0700712 struct cache_set *c = container_of(kobj, struct cache_set, kobj);
713
Coly Li6f9414e2020-10-01 14:50:55 +0800714 sysfs_print(synchronous, CACHE_SYNC(&c->cache->sb));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700715 sysfs_print(journal_delay_ms, c->journal_delay_ms);
Coly Li63a96c02020-10-01 14:50:52 +0800716 sysfs_hprint(bucket_size, bucket_bytes(c->cache));
Coly Li4e1ebae2020-10-01 14:50:49 +0800717 sysfs_hprint(block_size, block_bytes(c->cache));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700718 sysfs_print(tree_depth, c->root->level);
John Sheucb851142014-03-17 23:13:56 -0700719 sysfs_print(root_usage_percent, bch_root_usage(c));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700720
John Sheucb851142014-03-17 23:13:56 -0700721 sysfs_hprint(btree_cache_size, bch_cache_size(c));
722 sysfs_print(btree_cache_max_chain, bch_cache_max_chain(c));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700723 sysfs_print(cache_available_percent, 100 - c->gc_stats.in_use);
724
725 sysfs_print_time_stats(&c->btree_gc_time, btree_gc, sec, ms);
726 sysfs_print_time_stats(&c->btree_split_time, btree_split, sec, us);
Kent Overstreet67539e82013-09-10 22:53:34 -0700727 sysfs_print_time_stats(&c->sort.time, btree_sort, ms, us);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700728 sysfs_print_time_stats(&c->btree_read_time, btree_read, ms, us);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700729
John Sheucb851142014-03-17 23:13:56 -0700730 sysfs_print(btree_used_percent, bch_btree_used(c));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700731 sysfs_print(btree_nodes, c->gc_stats.nodes);
John Sheucb851142014-03-17 23:13:56 -0700732 sysfs_hprint(average_key_size, bch_average_key_size(c));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700733
734 sysfs_print(cache_read_races,
735 atomic_long_read(&c->cache_read_races));
736
Tang Junhuia728eac2018-02-07 11:41:39 -0800737 sysfs_print(reclaim,
738 atomic_long_read(&c->reclaim));
739
Coly Lidff90d52019-06-28 20:00:00 +0800740 sysfs_print(reclaimed_journal_buckets,
741 atomic_long_read(&c->reclaimed_journal_buckets));
742
Tang Junhuia728eac2018-02-07 11:41:39 -0800743 sysfs_print(flush_write,
744 atomic_long_read(&c->flush_write));
745
Kent Overstreetcafe5632013-03-23 16:11:31 -0700746 sysfs_print(writeback_keys_done,
747 atomic_long_read(&c->writeback_keys_done));
748 sysfs_print(writeback_keys_failed,
749 atomic_long_read(&c->writeback_keys_failed));
750
Kent Overstreet77c320e2013-07-11 19:42:51 -0700751 if (attr == &sysfs_errors)
752 return bch_snprint_string_list(buf, PAGE_SIZE, error_actions,
753 c->on_error);
754
Kent Overstreetcafe5632013-03-23 16:11:31 -0700755 /* See count_io_errors for why 88 */
756 sysfs_print(io_error_halflife, c->error_decay * 88);
Coly Li7ba0d832018-02-07 11:41:42 -0800757 sysfs_print(io_error_limit, c->error_limit);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700758
759 sysfs_hprint(congested,
760 ((uint64_t) bch_get_congested(c)) << 9);
761 sysfs_print(congested_read_threshold_us,
762 c->congested_read_threshold_us);
763 sysfs_print(congested_write_threshold_us,
764 c->congested_write_threshold_us);
765
Coly Li9aaf5162018-12-13 22:53:55 +0800766 sysfs_print(cutoff_writeback, bch_cutoff_writeback);
767 sysfs_print(cutoff_writeback_sync, bch_cutoff_writeback_sync);
768
Kent Overstreetcafe5632013-03-23 16:11:31 -0700769 sysfs_print(active_journal_entries, fifo_used(&c->journal.pin));
770 sysfs_printf(verify, "%i", c->verify);
771 sysfs_printf(key_merging_disabled, "%i", c->key_merging_disabled);
Kent Overstreet280481d2013-10-24 16:36:03 -0700772 sysfs_printf(expensive_debug_checks,
773 "%i", c->expensive_debug_checks);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700774 sysfs_printf(gc_always_rewrite, "%i", c->gc_always_rewrite);
775 sysfs_printf(btree_shrinker_disabled, "%i", c->shrinker_disabled);
776 sysfs_printf(copy_gc_enabled, "%i", c->copy_gc_enabled);
Coly Lic5fcded2019-11-13 16:03:23 +0800777 sysfs_printf(idle_max_writeback_rate, "%i",
778 c->idle_max_writeback_rate_enabled);
Coly Li7a671d82018-12-13 22:53:53 +0800779 sysfs_printf(gc_after_writeback, "%i", c->gc_after_writeback);
Coly Li771f3932018-03-18 17:36:17 -0700780 sysfs_printf(io_disable, "%i",
781 test_bit(CACHE_SET_IO_DISABLE, &c->flags));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700782
783 if (attr == &sysfs_bset_tree_stats)
784 return bch_bset_print_stats(c, buf);
785
Coly Li092bd542020-07-25 20:00:36 +0800786 if (attr == &sysfs_feature_compat)
787 return bch_print_cache_set_feature_compat(c, buf, PAGE_SIZE);
788 if (attr == &sysfs_feature_ro_compat)
789 return bch_print_cache_set_feature_ro_compat(c, buf, PAGE_SIZE);
790 if (attr == &sysfs_feature_incompat)
791 return bch_print_cache_set_feature_incompat(c, buf, PAGE_SIZE);
792
Kent Overstreetcafe5632013-03-23 16:11:31 -0700793 return 0;
794}
795SHOW_LOCKED(bch_cache_set)
796
797STORE(__bch_cache_set)
798{
799 struct cache_set *c = container_of(kobj, struct cache_set, kobj);
Andy Shevchenkoce4c3e12018-05-28 15:37:44 +0800800 ssize_t v;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700801
Coly Lia59ff6c2019-06-28 19:59:46 +0800802 /* no user space access if system is rebooting */
803 if (bcache_is_reboot)
804 return -EBUSY;
805
Kent Overstreetcafe5632013-03-23 16:11:31 -0700806 if (attr == &sysfs_unregister)
807 bch_cache_set_unregister(c);
808
809 if (attr == &sysfs_stop)
810 bch_cache_set_stop(c);
811
812 if (attr == &sysfs_synchronous) {
813 bool sync = strtoul_or_return(buf);
814
Coly Li6f9414e2020-10-01 14:50:55 +0800815 if (sync != CACHE_SYNC(&c->cache->sb)) {
816 SET_CACHE_SYNC(&c->cache->sb, sync);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700817 bcache_write_super(c);
818 }
819 }
820
821 if (attr == &sysfs_flash_vol_create) {
822 int r;
823 uint64_t v;
Coly Li1fae7cf2018-08-11 13:19:45 +0800824
Kent Overstreetcafe5632013-03-23 16:11:31 -0700825 strtoi_h_or_return(buf, v);
826
827 r = bch_flash_dev_create(c, v);
828 if (r)
829 return r;
830 }
831
832 if (attr == &sysfs_clear_stats) {
833 atomic_long_set(&c->writeback_keys_done, 0);
834 atomic_long_set(&c->writeback_keys_failed, 0);
835
836 memset(&c->gc_stats, 0, sizeof(struct gc_stat));
837 bch_cache_accounting_clear(&c->accounting);
838 }
839
Coly Licb07ad62018-12-13 22:53:52 +0800840 if (attr == &sysfs_trigger_gc)
841 force_wake_up_gc(c);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700842
843 if (attr == &sysfs_prune_cache) {
844 struct shrink_control sc;
Coly Li1fae7cf2018-08-11 13:19:45 +0800845
Kent Overstreetcafe5632013-03-23 16:11:31 -0700846 sc.gfp_mask = GFP_KERNEL;
847 sc.nr_to_scan = strtoul_or_return(buf);
Dave Chinner7dc19d52013-08-28 10:18:11 +1000848 c->shrink.scan_objects(&c->shrink, &sc);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700849 }
850
Coly Lif54478c2019-02-09 12:53:00 +0800851 sysfs_strtoul_clamp(congested_read_threshold_us,
852 c->congested_read_threshold_us,
853 0, UINT_MAX);
854 sysfs_strtoul_clamp(congested_write_threshold_us,
855 c->congested_write_threshold_us,
856 0, UINT_MAX);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700857
Kent Overstreet77c320e2013-07-11 19:42:51 -0700858 if (attr == &sysfs_errors) {
Coly Li20621fe2019-08-09 14:14:05 +0800859 v = __sysfs_match_string(error_actions, -1, buf);
Kent Overstreet77c320e2013-07-11 19:42:51 -0700860 if (v < 0)
861 return v;
862
863 c->on_error = v;
864 }
865
Coly Lib1500842019-02-09 12:53:09 +0800866 sysfs_strtoul_clamp(io_error_limit, c->error_limit, 0, UINT_MAX);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700867
868 /* See count_io_errors() for why 88 */
Coly Lia91fbda2019-02-09 12:53:10 +0800869 if (attr == &sysfs_io_error_halflife) {
870 unsigned long v = 0;
871 ssize_t ret;
872
873 ret = strtoul_safe_clamp(buf, v, 0, UINT_MAX);
874 if (!ret) {
875 c->error_decay = v / 88;
876 return size;
877 }
878 return ret;
879 }
Kent Overstreetcafe5632013-03-23 16:11:31 -0700880
Coly Li771f3932018-03-18 17:36:17 -0700881 if (attr == &sysfs_io_disable) {
Andy Shevchenkoce4c3e12018-05-28 15:37:44 +0800882 v = strtoul_or_return(buf);
Coly Li771f3932018-03-18 17:36:17 -0700883 if (v) {
884 if (test_and_set_bit(CACHE_SET_IO_DISABLE,
885 &c->flags))
Joe Perches46f5aa82020-05-27 12:01:52 +0800886 pr_warn("CACHE_SET_IO_DISABLE already set\n");
Coly Li771f3932018-03-18 17:36:17 -0700887 } else {
888 if (!test_and_clear_bit(CACHE_SET_IO_DISABLE,
889 &c->flags))
Joe Perches46f5aa82020-05-27 12:01:52 +0800890 pr_warn("CACHE_SET_IO_DISABLE already cleared\n");
Coly Li771f3932018-03-18 17:36:17 -0700891 }
892 }
893
Coly Li453745f2019-02-09 12:53:08 +0800894 sysfs_strtoul_clamp(journal_delay_ms,
895 c->journal_delay_ms,
896 0, USHRT_MAX);
Coly Lif5c0b952019-02-09 12:53:03 +0800897 sysfs_strtoul_bool(verify, c->verify);
898 sysfs_strtoul_bool(key_merging_disabled, c->key_merging_disabled);
Kent Overstreet280481d2013-10-24 16:36:03 -0700899 sysfs_strtoul(expensive_debug_checks, c->expensive_debug_checks);
Coly Lif5c0b952019-02-09 12:53:03 +0800900 sysfs_strtoul_bool(gc_always_rewrite, c->gc_always_rewrite);
901 sysfs_strtoul_bool(btree_shrinker_disabled, c->shrinker_disabled);
902 sysfs_strtoul_bool(copy_gc_enabled, c->copy_gc_enabled);
Coly Lic5fcded2019-11-13 16:03:23 +0800903 sysfs_strtoul_bool(idle_max_writeback_rate,
904 c->idle_max_writeback_rate_enabled);
905
Coly Li7a671d82018-12-13 22:53:53 +0800906 /*
907 * write gc_after_writeback here may overwrite an already set
908 * BCH_DO_AUTO_GC, it doesn't matter because this flag will be
909 * set in next chance.
910 */
911 sysfs_strtoul_clamp(gc_after_writeback, c->gc_after_writeback, 0, 1);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700912
913 return size;
914}
915STORE_LOCKED(bch_cache_set)
916
917SHOW(bch_cache_set_internal)
918{
919 struct cache_set *c = container_of(kobj, struct cache_set, internal);
Coly Li1fae7cf2018-08-11 13:19:45 +0800920
Kent Overstreetcafe5632013-03-23 16:11:31 -0700921 return bch_cache_set_show(&c->kobj, attr, buf);
922}
923
924STORE(bch_cache_set_internal)
925{
926 struct cache_set *c = container_of(kobj, struct cache_set, internal);
Coly Li1fae7cf2018-08-11 13:19:45 +0800927
Coly Lia59ff6c2019-06-28 19:59:46 +0800928 /* no user space access if system is rebooting */
929 if (bcache_is_reboot)
930 return -EBUSY;
931
Kent Overstreetcafe5632013-03-23 16:11:31 -0700932 return bch_cache_set_store(&c->kobj, attr, buf, size);
933}
934
935static void bch_cache_set_internal_release(struct kobject *k)
936{
937}
938
939static struct attribute *bch_cache_set_files[] = {
940 &sysfs_unregister,
941 &sysfs_stop,
942 &sysfs_synchronous,
943 &sysfs_journal_delay_ms,
944 &sysfs_flash_vol_create,
945
946 &sysfs_bucket_size,
947 &sysfs_block_size,
948 &sysfs_tree_depth,
949 &sysfs_root_usage_percent,
950 &sysfs_btree_cache_size,
951 &sysfs_cache_available_percent,
952
953 &sysfs_average_key_size,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700954
Kent Overstreet77c320e2013-07-11 19:42:51 -0700955 &sysfs_errors,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700956 &sysfs_io_error_limit,
957 &sysfs_io_error_halflife,
958 &sysfs_congested,
959 &sysfs_congested_read_threshold_us,
960 &sysfs_congested_write_threshold_us,
961 &sysfs_clear_stats,
962 NULL
963};
964KTYPE(bch_cache_set);
965
966static struct attribute *bch_cache_set_internal_files[] = {
967 &sysfs_active_journal_entries,
968
969 sysfs_time_stats_attribute_list(btree_gc, sec, ms)
970 sysfs_time_stats_attribute_list(btree_split, sec, us)
971 sysfs_time_stats_attribute_list(btree_sort, ms, us)
972 sysfs_time_stats_attribute_list(btree_read, ms, us)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700973
974 &sysfs_btree_nodes,
975 &sysfs_btree_used_percent,
976 &sysfs_btree_cache_max_chain,
977
978 &sysfs_bset_tree_stats,
979 &sysfs_cache_read_races,
Tang Junhuia728eac2018-02-07 11:41:39 -0800980 &sysfs_reclaim,
Coly Lidff90d52019-06-28 20:00:00 +0800981 &sysfs_reclaimed_journal_buckets,
Tang Junhuia728eac2018-02-07 11:41:39 -0800982 &sysfs_flush_write,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700983 &sysfs_writeback_keys_done,
984 &sysfs_writeback_keys_failed,
985
986 &sysfs_trigger_gc,
987 &sysfs_prune_cache,
988#ifdef CONFIG_BCACHE_DEBUG
989 &sysfs_verify,
990 &sysfs_key_merging_disabled,
Kent Overstreet280481d2013-10-24 16:36:03 -0700991 &sysfs_expensive_debug_checks,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700992#endif
993 &sysfs_gc_always_rewrite,
994 &sysfs_btree_shrinker_disabled,
995 &sysfs_copy_gc_enabled,
Coly Lic5fcded2019-11-13 16:03:23 +0800996 &sysfs_idle_max_writeback_rate,
Coly Li7a671d82018-12-13 22:53:53 +0800997 &sysfs_gc_after_writeback,
Coly Li771f3932018-03-18 17:36:17 -0700998 &sysfs_io_disable,
Coly Li9aaf5162018-12-13 22:53:55 +0800999 &sysfs_cutoff_writeback,
1000 &sysfs_cutoff_writeback_sync,
Coly Li092bd542020-07-25 20:00:36 +08001001 &sysfs_feature_compat,
1002 &sysfs_feature_ro_compat,
1003 &sysfs_feature_incompat,
Kent Overstreetcafe5632013-03-23 16:11:31 -07001004 NULL
1005};
1006KTYPE(bch_cache_set_internal);
1007
Peter Foley58f913d2017-10-13 16:35:28 -07001008static int __bch_cache_cmp(const void *l, const void *r)
1009{
Shile Zhangd55a4ae2019-09-03 21:25:43 +08001010 cond_resched();
Peter Foley58f913d2017-10-13 16:35:28 -07001011 return *((uint16_t *)r) - *((uint16_t *)l);
1012}
1013
Kent Overstreetcafe5632013-03-23 16:11:31 -07001014SHOW(__bch_cache)
1015{
1016 struct cache *ca = container_of(kobj, struct cache, kobj);
1017
1018 sysfs_hprint(bucket_size, bucket_bytes(ca));
1019 sysfs_hprint(block_size, block_bytes(ca));
1020 sysfs_print(nbuckets, ca->sb.nbuckets);
1021 sysfs_print(discard, ca->discard);
1022 sysfs_hprint(written, atomic_long_read(&ca->sectors_written) << 9);
1023 sysfs_hprint(btree_written,
1024 atomic_long_read(&ca->btree_sectors_written) << 9);
1025 sysfs_hprint(metadata_written,
1026 (atomic_long_read(&ca->meta_sectors_written) +
1027 atomic_long_read(&ca->btree_sectors_written)) << 9);
1028
1029 sysfs_print(io_errors,
1030 atomic_read(&ca->io_errors) >> IO_ERROR_SHIFT);
1031
Kent Overstreetcafe5632013-03-23 16:11:31 -07001032 if (attr == &sysfs_cache_replacement_policy)
Kent Overstreet169ef1c2013-03-28 12:50:55 -06001033 return bch_snprint_string_list(buf, PAGE_SIZE,
1034 cache_replacement_policies,
1035 CACHE_REPLACEMENT(&ca->sb));
Kent Overstreetcafe5632013-03-23 16:11:31 -07001036
1037 if (attr == &sysfs_priority_stats) {
Kent Overstreet15754022014-02-25 17:34:21 -08001038 struct bucket *b;
1039 size_t n = ca->sb.nbuckets, i;
1040 size_t unused = 0, available = 0, dirty = 0, meta = 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001041 uint64_t sum = 0;
Kent Overstreetbbc77aa2013-05-28 21:53:19 -07001042 /* Compute 31 quantiles */
1043 uint16_t q[31], *p, *cached;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001044 ssize_t ret;
1045
Kees Cook42bc47b2018-06-12 14:27:11 -07001046 cached = p = vmalloc(array_size(sizeof(uint16_t),
1047 ca->sb.nbuckets));
Kent Overstreetcafe5632013-03-23 16:11:31 -07001048 if (!p)
1049 return -ENOMEM;
1050
1051 mutex_lock(&ca->set->bucket_lock);
Kent Overstreet15754022014-02-25 17:34:21 -08001052 for_each_bucket(b, ca) {
1053 if (!GC_SECTORS_USED(b))
1054 unused++;
1055 if (GC_MARK(b) == GC_MARK_RECLAIMABLE)
1056 available++;
1057 if (GC_MARK(b) == GC_MARK_DIRTY)
1058 dirty++;
1059 if (GC_MARK(b) == GC_MARK_METADATA)
1060 meta++;
1061 }
1062
Kent Overstreetcafe5632013-03-23 16:11:31 -07001063 for (i = ca->sb.first_bucket; i < n; i++)
1064 p[i] = ca->buckets[i].prio;
1065 mutex_unlock(&ca->set->bucket_lock);
1066
Peter Foley58f913d2017-10-13 16:35:28 -07001067 sort(p, n, sizeof(uint16_t), __bch_cache_cmp, NULL);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001068
1069 while (n &&
1070 !cached[n - 1])
1071 --n;
1072
Kent Overstreetcafe5632013-03-23 16:11:31 -07001073 while (cached < p + n &&
1074 *cached == BTREE_PRIO)
Kent Overstreet15754022014-02-25 17:34:21 -08001075 cached++, n--;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001076
1077 for (i = 0; i < n; i++)
1078 sum += INITIAL_PRIO - cached[i];
1079
1080 if (n)
1081 do_div(sum, n);
1082
Kent Overstreetbbc77aa2013-05-28 21:53:19 -07001083 for (i = 0; i < ARRAY_SIZE(q); i++)
1084 q[i] = INITIAL_PRIO - cached[n * (i + 1) /
1085 (ARRAY_SIZE(q) + 1)];
Kent Overstreetcafe5632013-03-23 16:11:31 -07001086
1087 vfree(p);
1088
Kent Overstreetbbc77aa2013-05-28 21:53:19 -07001089 ret = scnprintf(buf, PAGE_SIZE,
1090 "Unused: %zu%%\n"
Kent Overstreet15754022014-02-25 17:34:21 -08001091 "Clean: %zu%%\n"
1092 "Dirty: %zu%%\n"
Kent Overstreetbbc77aa2013-05-28 21:53:19 -07001093 "Metadata: %zu%%\n"
1094 "Average: %llu\n"
1095 "Sectors per Q: %zu\n"
1096 "Quantiles: [",
1097 unused * 100 / (size_t) ca->sb.nbuckets,
Kent Overstreet15754022014-02-25 17:34:21 -08001098 available * 100 / (size_t) ca->sb.nbuckets,
1099 dirty * 100 / (size_t) ca->sb.nbuckets,
1100 meta * 100 / (size_t) ca->sb.nbuckets, sum,
Kent Overstreetbbc77aa2013-05-28 21:53:19 -07001101 n * ca->sb.bucket_size / (ARRAY_SIZE(q) + 1));
Kent Overstreetcafe5632013-03-23 16:11:31 -07001102
Kent Overstreetbbc77aa2013-05-28 21:53:19 -07001103 for (i = 0; i < ARRAY_SIZE(q); i++)
1104 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
1105 "%u ", q[i]);
1106 ret--;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001107
Kent Overstreetbbc77aa2013-05-28 21:53:19 -07001108 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "]\n");
1109
Kent Overstreetcafe5632013-03-23 16:11:31 -07001110 return ret;
1111 }
1112
1113 return 0;
1114}
1115SHOW_LOCKED(bch_cache)
1116
1117STORE(__bch_cache)
1118{
1119 struct cache *ca = container_of(kobj, struct cache, kobj);
Andy Shevchenkoce4c3e12018-05-28 15:37:44 +08001120 ssize_t v;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001121
Coly Lia59ff6c2019-06-28 19:59:46 +08001122 /* no user space access if system is rebooting */
1123 if (bcache_is_reboot)
1124 return -EBUSY;
1125
Kent Overstreetcafe5632013-03-23 16:11:31 -07001126 if (attr == &sysfs_discard) {
1127 bool v = strtoul_or_return(buf);
1128
1129 if (blk_queue_discard(bdev_get_queue(ca->bdev)))
1130 ca->discard = v;
1131
1132 if (v != CACHE_DISCARD(&ca->sb)) {
1133 SET_CACHE_DISCARD(&ca->sb, v);
1134 bcache_write_super(ca->set);
1135 }
1136 }
1137
1138 if (attr == &sysfs_cache_replacement_policy) {
Coly Li20621fe2019-08-09 14:14:05 +08001139 v = __sysfs_match_string(cache_replacement_policies, -1, buf);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001140 if (v < 0)
1141 return v;
1142
Coly Li6f10f7d2018-08-11 13:19:44 +08001143 if ((unsigned int) v != CACHE_REPLACEMENT(&ca->sb)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -07001144 mutex_lock(&ca->set->bucket_lock);
1145 SET_CACHE_REPLACEMENT(&ca->sb, v);
1146 mutex_unlock(&ca->set->bucket_lock);
1147
1148 bcache_write_super(ca->set);
1149 }
1150 }
1151
Kent Overstreetcafe5632013-03-23 16:11:31 -07001152 if (attr == &sysfs_clear_stats) {
1153 atomic_long_set(&ca->sectors_written, 0);
1154 atomic_long_set(&ca->btree_sectors_written, 0);
1155 atomic_long_set(&ca->meta_sectors_written, 0);
1156 atomic_set(&ca->io_count, 0);
1157 atomic_set(&ca->io_errors, 0);
1158 }
1159
1160 return size;
1161}
1162STORE_LOCKED(bch_cache)
1163
1164static struct attribute *bch_cache_files[] = {
1165 &sysfs_bucket_size,
1166 &sysfs_block_size,
1167 &sysfs_nbuckets,
1168 &sysfs_priority_stats,
1169 &sysfs_discard,
1170 &sysfs_written,
1171 &sysfs_btree_written,
1172 &sysfs_metadata_written,
1173 &sysfs_io_errors,
1174 &sysfs_clear_stats,
Kent Overstreetcafe5632013-03-23 16:11:31 -07001175 &sysfs_cache_replacement_policy,
1176 NULL
1177};
1178KTYPE(bch_cache);