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 rw_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(accounting);
207 read_attribute(usage_base);
209 #define x(t, n, ...) read_attribute(t);
210 BCH_PERSISTENT_COUNTERS()
213 rw_attribute(discard);
216 rw_attribute(copy_gc_enabled);
217 read_attribute(copy_gc_wait);
219 rw_attribute(rebalance_enabled);
220 sysfs_pd_controller_attribute(rebalance);
221 read_attribute(rebalance_status);
223 read_attribute(new_stripes);
225 read_attribute(io_timers_read);
226 read_attribute(io_timers_write);
228 read_attribute(moving_ctxts);
230 #ifdef CONFIG_BCACHEFS_TESTS
231 write_attribute(perf_test);
232 #endif /* CONFIG_BCACHEFS_TESTS */
235 static struct attribute sysfs_time_stat_##_name = \
236 { .name = #_name, .mode = 0644 };
240 static struct attribute sysfs_state_rw = {
245 static size_t bch2_btree_cache_size(struct bch_fs *c)
247 struct btree_cache *bc = &c->btree_cache;
251 mutex_lock(&bc->lock);
252 list_for_each_entry(b, &bc->live[0].list, list)
253 ret += btree_buf_bytes(b);
254 list_for_each_entry(b, &bc->live[1].list, list)
255 ret += btree_buf_bytes(b);
256 list_for_each_entry(b, &bc->freeable, list)
257 ret += btree_buf_bytes(b);
258 mutex_unlock(&bc->lock);
262 static int bch2_compression_stats_to_text(struct printbuf *out, struct bch_fs *c)
264 prt_str(out, "type");
265 printbuf_tabstop_push(out, 12);
266 printbuf_tabstop_push(out, 16);
267 printbuf_tabstop_push(out, 16);
268 printbuf_tabstop_push(out, 24);
269 prt_printf(out, "type\tcompressed\runcompressed\raverage extent size\r\n");
271 for (unsigned i = 1; i < BCH_COMPRESSION_TYPE_NR; i++) {
272 struct disk_accounting_pos a = {
273 .type = BCH_DISK_ACCOUNTING_compression,
274 .compression.type = i,
276 struct bpos p = disk_accounting_pos_to_bpos(&a);
278 bch2_accounting_mem_read(c, p, v, ARRAY_SIZE(v));
280 u64 nr_extents = v[0];
281 u64 sectors_uncompressed = v[1];
282 u64 sectors_compressed = v[2];
284 bch2_prt_compression_type(out, i);
287 prt_human_readable_u64(out, sectors_compressed << 9);
290 prt_human_readable_u64(out, sectors_uncompressed << 9);
293 prt_human_readable_u64(out, nr_extents
294 ? div64_u64(sectors_uncompressed << 9, nr_extents)
303 static void bch2_gc_gens_pos_to_text(struct printbuf *out, struct bch_fs *c)
305 prt_printf(out, "%s: ", bch2_btree_id_str(c->gc_gens_btree));
306 bch2_bpos_to_text(out, c->gc_gens_pos);
307 prt_printf(out, "\n");
310 static void bch2_fs_usage_base_to_text(struct printbuf *out, struct bch_fs *c)
312 struct bch_fs_usage_base b = {};
314 acc_u64s_percpu(&b.hidden, &c->usage->hidden, sizeof(b) / sizeof(u64));
316 prt_printf(out, "hidden:\t\t%llu\n", b.hidden);
317 prt_printf(out, "btree:\t\t%llu\n", b.btree);
318 prt_printf(out, "data:\t\t%llu\n", b.data);
319 prt_printf(out, "cached:\t%llu\n", b.cached);
320 prt_printf(out, "reserved:\t\t%llu\n", b.reserved);
321 prt_printf(out, "nr_inodes:\t%llu\n", b.nr_inodes);
326 struct bch_fs *c = container_of(kobj, struct bch_fs, kobj);
328 sysfs_print(minor, c->minor);
329 sysfs_printf(internal_uuid, "%pU", c->sb.uuid.b);
331 if (attr == &sysfs_flags)
332 prt_bitflags(out, bch2_fs_flag_strs, c->flags);
334 sysfs_hprint(btree_cache_size, bch2_btree_cache_size(c));
336 if (attr == &sysfs_btree_write_stats)
337 bch2_btree_write_stats_to_text(out, c);
339 if (attr == &sysfs_gc_gens_pos)
340 bch2_gc_gens_pos_to_text(out, c);
342 sysfs_printf(copy_gc_enabled, "%i", c->copy_gc_enabled);
344 sysfs_printf(rebalance_enabled, "%i", c->rebalance.enabled);
345 sysfs_pd_controller_show(rebalance, &c->rebalance.pd); /* XXX */
347 if (attr == &sysfs_copy_gc_wait)
348 bch2_copygc_wait_to_text(out, c);
350 if (attr == &sysfs_rebalance_status)
351 bch2_rebalance_status_to_text(out, c);
355 if (attr == &sysfs_journal_debug)
356 bch2_journal_debug_to_text(out, &c->journal);
358 if (attr == &sysfs_btree_cache)
359 bch2_btree_cache_to_text(out, &c->btree_cache);
361 if (attr == &sysfs_btree_key_cache)
362 bch2_btree_key_cache_to_text(out, &c->btree_key_cache);
364 if (attr == &sysfs_btree_reserve_cache)
365 bch2_btree_reserve_cache_to_text(out, c);
367 if (attr == &sysfs_stripes_heap)
368 bch2_stripes_heap_to_text(out, c);
370 if (attr == &sysfs_open_buckets)
371 bch2_open_buckets_to_text(out, c, NULL);
373 if (attr == &sysfs_open_buckets_partial)
374 bch2_open_buckets_partial_to_text(out, c);
376 if (attr == &sysfs_write_points)
377 bch2_write_points_to_text(out, c);
379 if (attr == &sysfs_compression_stats)
380 bch2_compression_stats_to_text(out, c);
382 if (attr == &sysfs_new_stripes)
383 bch2_new_stripes_to_text(out, c);
385 if (attr == &sysfs_io_timers_read)
386 bch2_io_timers_to_text(out, &c->io_clock[READ]);
388 if (attr == &sysfs_io_timers_write)
389 bch2_io_timers_to_text(out, &c->io_clock[WRITE]);
391 if (attr == &sysfs_moving_ctxts)
392 bch2_fs_moving_ctxts_to_text(out, c);
394 #ifdef BCH_WRITE_REF_DEBUG
395 if (attr == &sysfs_write_refs)
396 bch2_write_refs_to_text(out, c);
399 if (attr == &sysfs_nocow_lock_table)
400 bch2_nocow_locks_to_text(out, &c->nocow_locks);
402 if (attr == &sysfs_disk_groups)
403 bch2_disk_groups_to_text(out, c);
405 if (attr == &sysfs_alloc_debug)
406 bch2_fs_alloc_debug_to_text(out, c);
408 if (attr == &sysfs_accounting)
409 bch2_fs_accounting_to_text(out, c);
411 if (attr == &sysfs_usage_base)
412 bch2_fs_usage_base_to_text(out, c);
419 struct bch_fs *c = container_of(kobj, struct bch_fs, kobj);
421 if (attr == &sysfs_copy_gc_enabled) {
422 ssize_t ret = strtoul_safe(buf, c->copy_gc_enabled)
425 if (c->copygc_thread)
426 wake_up_process(c->copygc_thread);
430 if (attr == &sysfs_rebalance_enabled) {
431 ssize_t ret = strtoul_safe(buf, c->rebalance.enabled)
438 sysfs_pd_controller_store(rebalance, &c->rebalance.pd);
442 if (!test_bit(BCH_FS_started, &c->flags))
447 if (!bch2_write_ref_tryget(c, BCH_WRITE_REF_sysfs))
450 if (attr == &sysfs_trigger_btree_cache_shrink) {
451 struct btree_cache *bc = &c->btree_cache;
452 struct shrink_control sc;
454 sc.gfp_mask = GFP_KERNEL;
455 sc.nr_to_scan = strtoul_or_return(buf);
456 bc->live[0].shrink->scan_objects(bc->live[0].shrink, &sc);
459 if (attr == &sysfs_trigger_btree_key_cache_shrink) {
460 struct shrink_control sc;
462 sc.gfp_mask = GFP_KERNEL;
463 sc.nr_to_scan = strtoul_or_return(buf);
464 c->btree_key_cache.shrink->scan_objects(c->btree_key_cache.shrink, &sc);
467 if (attr == &sysfs_trigger_gc)
470 if (attr == &sysfs_trigger_discards)
473 if (attr == &sysfs_trigger_invalidates)
474 bch2_do_invalidates(c);
476 if (attr == &sysfs_trigger_journal_flush) {
477 bch2_journal_flush_all_pins(&c->journal);
478 bch2_journal_meta(&c->journal);
481 if (attr == &sysfs_trigger_journal_writes)
482 bch2_journal_do_writes(&c->journal);
484 if (attr == &sysfs_trigger_freelist_wakeup)
485 closure_wake_up(&c->freelist_wait);
487 #ifdef CONFIG_BCACHEFS_TESTS
488 if (attr == &sysfs_perf_test) {
489 char *tmp = kstrdup(buf, GFP_KERNEL), *p = tmp;
490 char *test = strsep(&p, " \t\n");
491 char *nr_str = strsep(&p, " \t\n");
492 char *threads_str = strsep(&p, " \t\n");
498 !(ret = kstrtouint(threads_str, 10, &threads)) &&
499 !(ret = bch2_strtoull_h(nr_str, &nr)))
500 ret = bch2_btree_perf_test(c, test, nr, threads);
507 bch2_write_ref_put(c, BCH_WRITE_REF_sysfs);
512 struct attribute *bch2_fs_files[] = {
514 &sysfs_btree_cache_size,
515 &sysfs_btree_write_stats,
517 &sysfs_rebalance_status,
519 &sysfs_compression_stats,
521 #ifdef CONFIG_BCACHEFS_TESTS
529 SHOW(bch2_fs_counters)
531 struct bch_fs *c = container_of(kobj, struct bch_fs, counters_kobj);
533 u64 counter_since_mount = 0;
535 printbuf_tabstop_push(out, 32);
538 if (attr == &sysfs_##t) { \
539 counter = percpu_u64_get(&c->counters[BCH_COUNTER_##t]);\
540 counter_since_mount = counter - c->counters_on_mount[BCH_COUNTER_##t];\
541 prt_printf(out, "since mount:\t"); \
542 prt_human_readable_u64(out, counter_since_mount); \
545 prt_printf(out, "since filesystem creation:\t"); \
546 prt_human_readable_u64(out, counter); \
549 BCH_PERSISTENT_COUNTERS()
554 STORE(bch2_fs_counters) {
558 SYSFS_OPS(bch2_fs_counters);
560 struct attribute *bch2_fs_counters_files[] = {
563 BCH_PERSISTENT_COUNTERS()
567 /* internal dir - just a wrapper */
569 SHOW(bch2_fs_internal)
571 struct bch_fs *c = container_of(kobj, struct bch_fs, internal);
573 return bch2_fs_to_text(out, &c->kobj, attr);
576 STORE(bch2_fs_internal)
578 struct bch_fs *c = container_of(kobj, struct bch_fs, internal);
580 return bch2_fs_store(&c->kobj, attr, buf, size);
582 SYSFS_OPS(bch2_fs_internal);
584 struct attribute *bch2_fs_internal_files[] = {
586 &sysfs_journal_debug,
588 &sysfs_btree_key_cache,
589 &sysfs_btree_reserve_cache,
593 &sysfs_open_buckets_partial,
595 #ifdef BCH_WRITE_REF_DEBUG
598 &sysfs_nocow_lock_table,
599 &sysfs_io_timers_read,
600 &sysfs_io_timers_write,
603 &sysfs_trigger_discards,
604 &sysfs_trigger_invalidates,
605 &sysfs_trigger_journal_flush,
606 &sysfs_trigger_journal_writes,
607 &sysfs_trigger_btree_cache_shrink,
608 &sysfs_trigger_btree_key_cache_shrink,
609 &sysfs_trigger_freelist_wakeup,
613 &sysfs_copy_gc_enabled,
616 &sysfs_rebalance_enabled,
617 sysfs_pd_controller_files(rebalance),
621 &sysfs_internal_uuid,
632 SHOW(bch2_fs_opts_dir)
634 struct bch_fs *c = container_of(kobj, struct bch_fs, opts_dir);
635 const struct bch_option *opt = container_of(attr, struct bch_option, attr);
636 int id = opt - bch2_opt_table;
637 u64 v = bch2_opt_get_by_id(&c->opts, id);
639 bch2_opt_to_text(out, c, c->disk_sb.sb, opt, v, OPT_SHOW_FULL_LIST);
645 STORE(bch2_fs_opts_dir)
647 struct bch_fs *c = container_of(kobj, struct bch_fs, opts_dir);
648 const struct bch_option *opt = container_of(attr, struct bch_option, attr);
649 int ret, id = opt - bch2_opt_table;
654 * We don't need to take c->writes for correctness, but it eliminates an
655 * unsightly error message in the dmesg log when we're RO:
657 if (unlikely(!bch2_write_ref_tryget(c, BCH_WRITE_REF_sysfs)))
660 tmp = kstrdup(buf, GFP_KERNEL);
666 ret = bch2_opt_parse(c, opt, strim(tmp), &v, NULL);
672 ret = bch2_opt_check_may_set(c, id, v);
676 bch2_opt_set_sb(c, NULL, opt, v);
677 bch2_opt_set_by_id(&c->opts, id, v);
680 (id == Opt_background_target ||
681 id == Opt_background_compression ||
682 (id == Opt_compression && !c->opts.background_compression)))
683 bch2_set_rebalance_needs_scan(c, 0);
687 bch2_write_ref_put(c, BCH_WRITE_REF_sysfs);
690 SYSFS_OPS(bch2_fs_opts_dir);
692 struct attribute *bch2_fs_opts_dir_files[] = { NULL };
694 int bch2_opts_create_sysfs_files(struct kobject *kobj)
696 const struct bch_option *i;
699 for (i = bch2_opt_table;
700 i < bch2_opt_table + bch2_opts_nr;
702 if (!(i->flags & OPT_FS))
705 ret = sysfs_create_file(kobj, &i->attr);
715 SHOW(bch2_fs_time_stats)
717 struct bch_fs *c = container_of(kobj, struct bch_fs, time_stats);
720 if (attr == &sysfs_time_stat_##name) \
721 bch2_time_stats_to_text(out, &c->times[BCH_TIME_##name]);
728 STORE(bch2_fs_time_stats)
730 struct bch_fs *c = container_of(kobj, struct bch_fs, time_stats);
733 if (attr == &sysfs_time_stat_##name) \
734 bch2_time_stats_reset(&c->times[BCH_TIME_##name]);
739 SYSFS_OPS(bch2_fs_time_stats);
741 struct attribute *bch2_fs_time_stats_files[] = {
743 &sysfs_time_stat_##name,
749 static const char * const bch2_rw[] = {
755 static void dev_io_done_to_text(struct printbuf *out, struct bch_dev *ca)
759 for (rw = 0; rw < 2; rw++) {
760 prt_printf(out, "%s:\n", bch2_rw[rw]);
762 for (i = 1; i < BCH_DATA_NR; i++)
763 prt_printf(out, "%-12s:%12llu\n",
764 bch2_data_type_str(i),
765 percpu_u64_get(&ca->io_done->sectors[rw][i]) << 9);
771 struct bch_dev *ca = container_of(kobj, struct bch_dev, kobj);
772 struct bch_fs *c = ca->fs;
774 sysfs_printf(uuid, "%pU\n", ca->uuid.b);
776 sysfs_print(bucket_size, bucket_bytes(ca));
777 sysfs_print(first_bucket, ca->mi.first_bucket);
778 sysfs_print(nbuckets, ca->mi.nbuckets);
779 sysfs_print(durability, ca->mi.durability);
780 sysfs_print(discard, ca->mi.discard);
782 if (attr == &sysfs_label) {
784 bch2_disk_path_to_text(out, c, ca->mi.group - 1);
788 if (attr == &sysfs_has_data) {
789 prt_bitflags(out, __bch2_data_types, bch2_dev_has_data(c, ca));
793 if (attr == &sysfs_state_rw) {
794 prt_string_option(out, bch2_member_states, ca->mi.state);
798 if (attr == &sysfs_io_done)
799 dev_io_done_to_text(out, ca);
801 if (attr == &sysfs_io_errors)
802 bch2_dev_io_errors_to_text(out, ca);
804 sysfs_print(io_latency_read, atomic64_read(&ca->cur_latency[READ]));
805 sysfs_print(io_latency_write, atomic64_read(&ca->cur_latency[WRITE]));
807 if (attr == &sysfs_io_latency_stats_read)
808 bch2_time_stats_to_text(out, &ca->io_latency[READ].stats);
810 if (attr == &sysfs_io_latency_stats_write)
811 bch2_time_stats_to_text(out, &ca->io_latency[WRITE].stats);
813 sysfs_printf(congested, "%u%%",
814 clamp(atomic_read(&ca->congested), 0, CONGESTED_MAX)
815 * 100 / CONGESTED_MAX);
817 if (attr == &sysfs_alloc_debug)
818 bch2_dev_alloc_debug_to_text(out, ca);
820 if (attr == &sysfs_open_buckets)
821 bch2_open_buckets_to_text(out, c, ca);
828 struct bch_dev *ca = container_of(kobj, struct bch_dev, kobj);
829 struct bch_fs *c = ca->fs;
831 if (attr == &sysfs_discard) {
832 bool v = strtoul_or_return(buf);
834 bch2_opt_set_sb(c, ca, bch2_opt_table + Opt_discard, v);
837 if (attr == &sysfs_durability) {
838 u64 v = strtoul_or_return(buf);
840 bch2_opt_set_sb(c, ca, bch2_opt_table + Opt_durability, v);
843 if (attr == &sysfs_label) {
847 tmp = kstrdup(buf, GFP_KERNEL);
851 ret = bch2_dev_group_set(c, ca, strim(tmp));
857 if (attr == &sysfs_io_errors_reset)
858 bch2_dev_errors_reset(ca);
864 struct attribute *bch2_dev_files[] = {
879 &sysfs_io_errors_reset,
881 &sysfs_io_latency_read,
882 &sysfs_io_latency_write,
883 &sysfs_io_latency_stats_read,
884 &sysfs_io_latency_stats_write,
893 #endif /* _BCACHEFS_SYSFS_H_ */