1 // SPDX-License-Identifier: GPL-2.0
3 * bcache sysfs interfaces
6 * Copyright 2012 Google, Inc.
9 #ifndef NO_BCACHEFS_SYSFS
12 #include "alloc_background.h"
13 #include "alloc_foreground.h"
15 #include "btree_cache.h"
17 #include "btree_iter.h"
18 #include "btree_key_cache.h"
19 #include "btree_update.h"
20 #include "btree_update_interior.h"
25 #include "disk_accounting.h"
26 #include "disk_groups.h"
30 #include "journal_reclaim.h"
34 #include "nocow_locking.h"
36 #include "rebalance.h"
41 #include <linux/blkdev.h>
42 #include <linux/sort.h>
43 #include <linux/sched/clock.h>
47 #define SYSFS_OPS(type) \
48 const struct sysfs_ops type ## _sysfs_ops = { \
49 .show = type ## _show, \
50 .store = type ## _store \
54 static ssize_t fn ## _to_text(struct printbuf *, \
55 struct kobject *, struct attribute *); \
57 static ssize_t fn ## _show(struct kobject *kobj, struct attribute *attr,\
60 struct printbuf out = PRINTBUF; \
61 ssize_t ret = fn ## _to_text(&out, kobj, attr); \
63 if (out.pos && out.buf[out.pos - 1] != '\n') \
66 if (!ret && out.allocation_failure) \
70 ret = min_t(size_t, out.pos, PAGE_SIZE - 1); \
71 memcpy(buf, out.buf, ret); \
73 printbuf_exit(&out); \
74 return bch2_err_class(ret); \
77 static ssize_t fn ## _to_text(struct printbuf *out, struct kobject *kobj,\
78 struct attribute *attr)
81 static ssize_t fn ## _store_inner(struct kobject *, struct attribute *,\
82 const char *, size_t); \
84 static ssize_t fn ## _store(struct kobject *kobj, struct attribute *attr,\
85 const char *buf, size_t size) \
87 return bch2_err_class(fn##_store_inner(kobj, attr, buf, size)); \
90 static ssize_t fn ## _store_inner(struct kobject *kobj, struct attribute *attr,\
91 const char *buf, size_t size)
93 #define __sysfs_attribute(_name, _mode) \
94 static struct attribute sysfs_##_name = \
95 { .name = #_name, .mode = _mode }
97 #define write_attribute(n) __sysfs_attribute(n, 0200)
98 #define read_attribute(n) __sysfs_attribute(n, 0444)
99 #define rw_attribute(n) __sysfs_attribute(n, 0644)
101 #define sysfs_printf(file, fmt, ...) \
103 if (attr == &sysfs_ ## file) \
104 prt_printf(out, fmt "\n", __VA_ARGS__); \
107 #define sysfs_print(file, var) \
109 if (attr == &sysfs_ ## file) \
113 #define sysfs_hprint(file, val) \
115 if (attr == &sysfs_ ## file) \
116 prt_human_readable_s64(out, val); \
119 #define sysfs_strtoul(file, var) \
121 if (attr == &sysfs_ ## file) \
122 return strtoul_safe(buf, var) ?: (ssize_t) size; \
125 #define sysfs_strtoul_clamp(file, var, min, max) \
127 if (attr == &sysfs_ ## file) \
128 return strtoul_safe_clamp(buf, var, min, max) \
132 #define strtoul_or_return(cp) \
135 int _r = kstrtoul(cp, 10, &_v); \
141 write_attribute(trigger_gc);
142 write_attribute(trigger_discards);
143 write_attribute(trigger_invalidates);
144 write_attribute(trigger_journal_flush);
145 write_attribute(trigger_journal_writes);
146 write_attribute(trigger_btree_cache_shrink);
147 write_attribute(trigger_btree_key_cache_shrink);
148 write_attribute(trigger_freelist_wakeup);
149 read_attribute(gc_gens_pos);
151 read_attribute(uuid);
152 read_attribute(minor);
153 read_attribute(flags);
154 read_attribute(bucket_size);
155 read_attribute(first_bucket);
156 read_attribute(nbuckets);
157 rw_attribute(durability);
158 read_attribute(io_done);
159 read_attribute(io_errors);
160 write_attribute(io_errors_reset);
162 read_attribute(io_latency_read);
163 read_attribute(io_latency_write);
164 read_attribute(io_latency_stats_read);
165 read_attribute(io_latency_stats_write);
166 read_attribute(congested);
168 read_attribute(btree_write_stats);
170 read_attribute(btree_cache_size);
171 read_attribute(compression_stats);
172 read_attribute(journal_debug);
173 read_attribute(btree_cache);
174 read_attribute(btree_key_cache);
175 read_attribute(btree_reserve_cache);
176 read_attribute(stripes_heap);
177 read_attribute(open_buckets);
178 read_attribute(open_buckets_partial);
179 read_attribute(write_points);
180 read_attribute(nocow_lock_table);
182 #ifdef BCH_WRITE_REF_DEBUG
183 read_attribute(write_refs);
185 static const char * const bch2_write_refs[] = {
192 static void bch2_write_refs_to_text(struct printbuf *out, struct bch_fs *c)
194 bch2_printbuf_tabstop_push(out, 24);
196 for (unsigned i = 0; i < ARRAY_SIZE(c->writes); i++)
197 prt_printf(out, "%s\t%li\n", bch2_write_refs[i], atomic_long_read(&c->writes[i]));
201 read_attribute(internal_uuid);
202 read_attribute(disk_groups);
204 read_attribute(has_data);
205 read_attribute(alloc_debug);
206 read_attribute(usage_base);
208 #define x(t, n, ...) read_attribute(t);
209 BCH_PERSISTENT_COUNTERS()
212 rw_attribute(discard);
213 read_attribute(state);
216 read_attribute(copy_gc_wait);
218 sysfs_pd_controller_attribute(rebalance);
219 read_attribute(rebalance_status);
221 read_attribute(new_stripes);
223 read_attribute(io_timers_read);
224 read_attribute(io_timers_write);
226 read_attribute(moving_ctxts);
228 #ifdef CONFIG_BCACHEFS_TESTS
229 write_attribute(perf_test);
230 #endif /* CONFIG_BCACHEFS_TESTS */
233 static struct attribute sysfs_time_stat_##_name = \
234 { .name = #_name, .mode = 0644 };
238 static size_t bch2_btree_cache_size(struct bch_fs *c)
240 struct btree_cache *bc = &c->btree_cache;
244 mutex_lock(&bc->lock);
245 list_for_each_entry(b, &bc->live[0].list, list)
246 ret += btree_buf_bytes(b);
247 list_for_each_entry(b, &bc->live[1].list, list)
248 ret += btree_buf_bytes(b);
249 list_for_each_entry(b, &bc->freeable, list)
250 ret += btree_buf_bytes(b);
251 mutex_unlock(&bc->lock);
255 static int bch2_compression_stats_to_text(struct printbuf *out, struct bch_fs *c)
257 prt_str(out, "type");
258 printbuf_tabstop_push(out, 12);
259 printbuf_tabstop_push(out, 16);
260 printbuf_tabstop_push(out, 16);
261 printbuf_tabstop_push(out, 24);
262 prt_printf(out, "type\tcompressed\runcompressed\raverage extent size\r\n");
264 for (unsigned i = 1; i < BCH_COMPRESSION_TYPE_NR; i++) {
265 struct disk_accounting_pos a = {
266 .type = BCH_DISK_ACCOUNTING_compression,
267 .compression.type = i,
269 struct bpos p = disk_accounting_pos_to_bpos(&a);
271 bch2_accounting_mem_read(c, p, v, ARRAY_SIZE(v));
273 u64 nr_extents = v[0];
274 u64 sectors_uncompressed = v[1];
275 u64 sectors_compressed = v[2];
277 bch2_prt_compression_type(out, i);
280 prt_human_readable_u64(out, sectors_compressed << 9);
283 prt_human_readable_u64(out, sectors_uncompressed << 9);
286 prt_human_readable_u64(out, nr_extents
287 ? div64_u64(sectors_uncompressed << 9, nr_extents)
296 static void bch2_gc_gens_pos_to_text(struct printbuf *out, struct bch_fs *c)
298 bch2_btree_id_to_text(out, c->gc_gens_btree);
299 prt_printf(out, ": ");
300 bch2_bpos_to_text(out, c->gc_gens_pos);
301 prt_printf(out, "\n");
304 static void bch2_fs_usage_base_to_text(struct printbuf *out, struct bch_fs *c)
306 struct bch_fs_usage_base b = {};
308 acc_u64s_percpu(&b.hidden, &c->usage->hidden, sizeof(b) / sizeof(u64));
310 prt_printf(out, "hidden:\t\t%llu\n", b.hidden);
311 prt_printf(out, "btree:\t\t%llu\n", b.btree);
312 prt_printf(out, "data:\t\t%llu\n", b.data);
313 prt_printf(out, "cached:\t%llu\n", b.cached);
314 prt_printf(out, "reserved:\t\t%llu\n", b.reserved);
315 prt_printf(out, "nr_inodes:\t%llu\n", b.nr_inodes);
320 struct bch_fs *c = container_of(kobj, struct bch_fs, kobj);
322 sysfs_print(minor, c->minor);
323 sysfs_printf(internal_uuid, "%pU", c->sb.uuid.b);
325 if (attr == &sysfs_flags)
326 prt_bitflags(out, bch2_fs_flag_strs, c->flags);
328 sysfs_hprint(btree_cache_size, bch2_btree_cache_size(c));
330 if (attr == &sysfs_btree_write_stats)
331 bch2_btree_write_stats_to_text(out, c);
333 if (attr == &sysfs_gc_gens_pos)
334 bch2_gc_gens_pos_to_text(out, c);
336 sysfs_pd_controller_show(rebalance, &c->rebalance.pd); /* XXX */
338 if (attr == &sysfs_copy_gc_wait)
339 bch2_copygc_wait_to_text(out, c);
341 if (attr == &sysfs_rebalance_status)
342 bch2_rebalance_status_to_text(out, c);
346 if (attr == &sysfs_journal_debug)
347 bch2_journal_debug_to_text(out, &c->journal);
349 if (attr == &sysfs_btree_cache)
350 bch2_btree_cache_to_text(out, &c->btree_cache);
352 if (attr == &sysfs_btree_key_cache)
353 bch2_btree_key_cache_to_text(out, &c->btree_key_cache);
355 if (attr == &sysfs_btree_reserve_cache)
356 bch2_btree_reserve_cache_to_text(out, c);
358 if (attr == &sysfs_stripes_heap)
359 bch2_stripes_heap_to_text(out, c);
361 if (attr == &sysfs_open_buckets)
362 bch2_open_buckets_to_text(out, c, NULL);
364 if (attr == &sysfs_open_buckets_partial)
365 bch2_open_buckets_partial_to_text(out, c);
367 if (attr == &sysfs_write_points)
368 bch2_write_points_to_text(out, c);
370 if (attr == &sysfs_compression_stats)
371 bch2_compression_stats_to_text(out, c);
373 if (attr == &sysfs_new_stripes)
374 bch2_new_stripes_to_text(out, c);
376 if (attr == &sysfs_io_timers_read)
377 bch2_io_timers_to_text(out, &c->io_clock[READ]);
379 if (attr == &sysfs_io_timers_write)
380 bch2_io_timers_to_text(out, &c->io_clock[WRITE]);
382 if (attr == &sysfs_moving_ctxts)
383 bch2_fs_moving_ctxts_to_text(out, c);
385 #ifdef BCH_WRITE_REF_DEBUG
386 if (attr == &sysfs_write_refs)
387 bch2_write_refs_to_text(out, c);
390 if (attr == &sysfs_nocow_lock_table)
391 bch2_nocow_locks_to_text(out, &c->nocow_locks);
393 if (attr == &sysfs_disk_groups)
394 bch2_disk_groups_to_text(out, c);
396 if (attr == &sysfs_alloc_debug)
397 bch2_fs_alloc_debug_to_text(out, c);
399 if (attr == &sysfs_usage_base)
400 bch2_fs_usage_base_to_text(out, c);
407 struct bch_fs *c = container_of(kobj, struct bch_fs, kobj);
409 sysfs_pd_controller_store(rebalance, &c->rebalance.pd);
413 if (!test_bit(BCH_FS_started, &c->flags))
418 if (!bch2_write_ref_tryget(c, BCH_WRITE_REF_sysfs))
421 if (attr == &sysfs_trigger_btree_cache_shrink) {
422 struct btree_cache *bc = &c->btree_cache;
423 struct shrink_control sc;
425 sc.gfp_mask = GFP_KERNEL;
426 sc.nr_to_scan = strtoul_or_return(buf);
427 bc->live[0].shrink->scan_objects(bc->live[0].shrink, &sc);
430 if (attr == &sysfs_trigger_btree_key_cache_shrink) {
431 struct shrink_control sc;
433 sc.gfp_mask = GFP_KERNEL;
434 sc.nr_to_scan = strtoul_or_return(buf);
435 c->btree_key_cache.shrink->scan_objects(c->btree_key_cache.shrink, &sc);
438 if (attr == &sysfs_trigger_gc)
441 if (attr == &sysfs_trigger_discards)
444 if (attr == &sysfs_trigger_invalidates)
445 bch2_do_invalidates(c);
447 if (attr == &sysfs_trigger_journal_flush) {
448 bch2_journal_flush_all_pins(&c->journal);
449 bch2_journal_meta(&c->journal);
452 if (attr == &sysfs_trigger_journal_writes)
453 bch2_journal_do_writes(&c->journal);
455 if (attr == &sysfs_trigger_freelist_wakeup)
456 closure_wake_up(&c->freelist_wait);
458 #ifdef CONFIG_BCACHEFS_TESTS
459 if (attr == &sysfs_perf_test) {
460 char *tmp = kstrdup(buf, GFP_KERNEL), *p = tmp;
461 char *test = strsep(&p, " \t\n");
462 char *nr_str = strsep(&p, " \t\n");
463 char *threads_str = strsep(&p, " \t\n");
469 !(ret = kstrtouint(threads_str, 10, &threads)) &&
470 !(ret = bch2_strtoull_h(nr_str, &nr)))
471 ret = bch2_btree_perf_test(c, test, nr, threads);
478 bch2_write_ref_put(c, BCH_WRITE_REF_sysfs);
483 struct attribute *bch2_fs_files[] = {
485 &sysfs_btree_cache_size,
486 &sysfs_btree_write_stats,
488 &sysfs_rebalance_status,
490 &sysfs_compression_stats,
492 #ifdef CONFIG_BCACHEFS_TESTS
500 SHOW(bch2_fs_counters)
502 struct bch_fs *c = container_of(kobj, struct bch_fs, counters_kobj);
504 u64 counter_since_mount = 0;
506 printbuf_tabstop_push(out, 32);
508 #define x(t, n, f, ...) \
509 if (attr == &sysfs_##t) { \
510 counter = percpu_u64_get(&c->counters[BCH_COUNTER_##t]);\
511 counter_since_mount = counter - c->counters_on_mount[BCH_COUNTER_##t];\
512 if (f & TYPE_SECTORS) { \
514 counter_since_mount <<= 9; \
517 prt_printf(out, "since mount:\t"); \
518 (f & TYPE_COUNTER) ? prt_u64(out, counter_since_mount) :\
519 prt_human_readable_u64(out, counter_since_mount); \
522 prt_printf(out, "since filesystem creation:\t"); \
523 (f & TYPE_COUNTER) ? prt_u64(out, counter) : \
524 prt_human_readable_u64(out, counter); \
527 BCH_PERSISTENT_COUNTERS()
532 STORE(bch2_fs_counters) {
536 SYSFS_OPS(bch2_fs_counters);
538 struct attribute *bch2_fs_counters_files[] = {
541 BCH_PERSISTENT_COUNTERS()
545 /* internal dir - just a wrapper */
547 SHOW(bch2_fs_internal)
549 struct bch_fs *c = container_of(kobj, struct bch_fs, internal);
551 return bch2_fs_to_text(out, &c->kobj, attr);
554 STORE(bch2_fs_internal)
556 struct bch_fs *c = container_of(kobj, struct bch_fs, internal);
558 return bch2_fs_store(&c->kobj, attr, buf, size);
560 SYSFS_OPS(bch2_fs_internal);
562 struct attribute *bch2_fs_internal_files[] = {
564 &sysfs_journal_debug,
566 &sysfs_btree_key_cache,
567 &sysfs_btree_reserve_cache,
571 &sysfs_open_buckets_partial,
573 #ifdef BCH_WRITE_REF_DEBUG
576 &sysfs_nocow_lock_table,
577 &sysfs_io_timers_read,
578 &sysfs_io_timers_write,
581 &sysfs_trigger_discards,
582 &sysfs_trigger_invalidates,
583 &sysfs_trigger_journal_flush,
584 &sysfs_trigger_journal_writes,
585 &sysfs_trigger_btree_cache_shrink,
586 &sysfs_trigger_btree_key_cache_shrink,
587 &sysfs_trigger_freelist_wakeup,
593 sysfs_pd_controller_files(rebalance),
597 &sysfs_internal_uuid,
607 SHOW(bch2_fs_opts_dir)
609 struct bch_fs *c = container_of(kobj, struct bch_fs, opts_dir);
610 const struct bch_option *opt = container_of(attr, struct bch_option, attr);
611 int id = opt - bch2_opt_table;
612 u64 v = bch2_opt_get_by_id(&c->opts, id);
614 bch2_opt_to_text(out, c, c->disk_sb.sb, opt, v, OPT_SHOW_FULL_LIST);
620 STORE(bch2_fs_opts_dir)
622 struct bch_fs *c = container_of(kobj, struct bch_fs, opts_dir);
623 const struct bch_option *opt = container_of(attr, struct bch_option, attr);
624 int ret, id = opt - bch2_opt_table;
629 * We don't need to take c->writes for correctness, but it eliminates an
630 * unsightly error message in the dmesg log when we're RO:
632 if (unlikely(!bch2_write_ref_tryget(c, BCH_WRITE_REF_sysfs)))
635 tmp = kstrdup(buf, GFP_KERNEL);
641 ret = bch2_opt_parse(c, opt, strim(tmp), &v, NULL);
647 ret = bch2_opt_check_may_set(c, id, v);
651 bch2_opt_set_sb(c, NULL, opt, v);
652 bch2_opt_set_by_id(&c->opts, id, v);
655 (id == Opt_background_target ||
656 id == Opt_background_compression ||
657 (id == Opt_compression && !c->opts.background_compression)))
658 bch2_set_rebalance_needs_scan(c, 0);
660 if (v && id == Opt_rebalance_enabled)
663 if (v && id == Opt_copygc_enabled &&
665 wake_up_process(c->copygc_thread);
669 bch2_write_ref_put(c, BCH_WRITE_REF_sysfs);
672 SYSFS_OPS(bch2_fs_opts_dir);
674 struct attribute *bch2_fs_opts_dir_files[] = { NULL };
676 int bch2_opts_create_sysfs_files(struct kobject *kobj)
678 const struct bch_option *i;
681 for (i = bch2_opt_table;
682 i < bch2_opt_table + bch2_opts_nr;
684 if (!(i->flags & OPT_FS))
687 ret = sysfs_create_file(kobj, &i->attr);
697 SHOW(bch2_fs_time_stats)
699 struct bch_fs *c = container_of(kobj, struct bch_fs, time_stats);
702 if (attr == &sysfs_time_stat_##name) \
703 bch2_time_stats_to_text(out, &c->times[BCH_TIME_##name]);
710 STORE(bch2_fs_time_stats)
712 struct bch_fs *c = container_of(kobj, struct bch_fs, time_stats);
715 if (attr == &sysfs_time_stat_##name) \
716 bch2_time_stats_reset(&c->times[BCH_TIME_##name]);
721 SYSFS_OPS(bch2_fs_time_stats);
723 struct attribute *bch2_fs_time_stats_files[] = {
725 &sysfs_time_stat_##name,
731 static const char * const bch2_rw[] = {
737 static void dev_io_done_to_text(struct printbuf *out, struct bch_dev *ca)
741 for (rw = 0; rw < 2; rw++) {
742 prt_printf(out, "%s:\n", bch2_rw[rw]);
744 for (i = 1; i < BCH_DATA_NR; i++)
745 prt_printf(out, "%-12s:%12llu\n",
746 bch2_data_type_str(i),
747 percpu_u64_get(&ca->io_done->sectors[rw][i]) << 9);
753 struct bch_dev *ca = container_of(kobj, struct bch_dev, kobj);
754 struct bch_fs *c = ca->fs;
756 sysfs_printf(uuid, "%pU\n", ca->uuid.b);
758 sysfs_print(bucket_size, bucket_bytes(ca));
759 sysfs_print(first_bucket, ca->mi.first_bucket);
760 sysfs_print(nbuckets, ca->mi.nbuckets);
761 sysfs_print(durability, ca->mi.durability);
762 sysfs_print(discard, ca->mi.discard);
764 if (attr == &sysfs_label) {
766 bch2_disk_path_to_text(out, c, ca->mi.group - 1);
770 if (attr == &sysfs_has_data) {
771 prt_bitflags(out, __bch2_data_types, bch2_dev_has_data(c, ca));
775 if (attr == &sysfs_state) {
776 prt_string_option(out, bch2_member_states, ca->mi.state);
780 if (attr == &sysfs_io_done)
781 dev_io_done_to_text(out, ca);
783 if (attr == &sysfs_io_errors)
784 bch2_dev_io_errors_to_text(out, ca);
786 sysfs_print(io_latency_read, atomic64_read(&ca->cur_latency[READ]));
787 sysfs_print(io_latency_write, atomic64_read(&ca->cur_latency[WRITE]));
789 if (attr == &sysfs_io_latency_stats_read)
790 bch2_time_stats_to_text(out, &ca->io_latency[READ].stats);
792 if (attr == &sysfs_io_latency_stats_write)
793 bch2_time_stats_to_text(out, &ca->io_latency[WRITE].stats);
795 sysfs_printf(congested, "%u%%",
796 clamp(atomic_read(&ca->congested), 0, CONGESTED_MAX)
797 * 100 / CONGESTED_MAX);
799 if (attr == &sysfs_alloc_debug)
800 bch2_dev_alloc_debug_to_text(out, ca);
802 if (attr == &sysfs_open_buckets)
803 bch2_open_buckets_to_text(out, c, ca);
810 struct bch_dev *ca = container_of(kobj, struct bch_dev, kobj);
811 struct bch_fs *c = ca->fs;
813 if (attr == &sysfs_discard) {
814 bool v = strtoul_or_return(buf);
816 bch2_opt_set_sb(c, ca, bch2_opt_table + Opt_discard, v);
819 if (attr == &sysfs_durability) {
820 u64 v = strtoul_or_return(buf);
822 bch2_opt_set_sb(c, ca, bch2_opt_table + Opt_durability, v);
825 if (attr == &sysfs_label) {
829 tmp = kstrdup(buf, GFP_KERNEL);
833 ret = bch2_dev_group_set(c, ca, strim(tmp));
839 if (attr == &sysfs_io_errors_reset)
840 bch2_dev_errors_reset(ca);
846 struct attribute *bch2_dev_files[] = {
861 &sysfs_io_errors_reset,
863 &sysfs_io_latency_read,
864 &sysfs_io_latency_write,
865 &sysfs_io_latency_stats_read,
866 &sysfs_io_latency_stats_write,
875 #endif /* _BCACHEFS_SYSFS_H_ */