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"
24 #include "disk_groups.h"
28 #include "journal_reclaim.h"
32 #include "nocow_locking.h"
34 #include "rebalance.h"
39 #include <linux/blkdev.h>
40 #include <linux/sort.h>
41 #include <linux/sched/clock.h>
45 #define SYSFS_OPS(type) \
46 const struct sysfs_ops type ## _sysfs_ops = { \
47 .show = type ## _show, \
48 .store = type ## _store \
52 static ssize_t fn ## _to_text(struct printbuf *, \
53 struct kobject *, struct attribute *); \
55 static ssize_t fn ## _show(struct kobject *kobj, struct attribute *attr,\
58 struct printbuf out = PRINTBUF; \
59 ssize_t ret = fn ## _to_text(&out, kobj, attr); \
61 if (out.pos && out.buf[out.pos - 1] != '\n') \
64 if (!ret && out.allocation_failure) \
68 ret = min_t(size_t, out.pos, PAGE_SIZE - 1); \
69 memcpy(buf, out.buf, ret); \
71 printbuf_exit(&out); \
72 return bch2_err_class(ret); \
75 static ssize_t fn ## _to_text(struct printbuf *out, struct kobject *kobj,\
76 struct attribute *attr)
79 static ssize_t fn ## _store_inner(struct kobject *, struct attribute *,\
80 const char *, size_t); \
82 static ssize_t fn ## _store(struct kobject *kobj, struct attribute *attr,\
83 const char *buf, size_t size) \
85 return bch2_err_class(fn##_store_inner(kobj, attr, buf, size)); \
88 static ssize_t fn ## _store_inner(struct kobject *kobj, struct attribute *attr,\
89 const char *buf, size_t size)
91 #define __sysfs_attribute(_name, _mode) \
92 static struct attribute sysfs_##_name = \
93 { .name = #_name, .mode = _mode }
95 #define write_attribute(n) __sysfs_attribute(n, 0200)
96 #define read_attribute(n) __sysfs_attribute(n, 0444)
97 #define rw_attribute(n) __sysfs_attribute(n, 0644)
99 #define sysfs_printf(file, fmt, ...) \
101 if (attr == &sysfs_ ## file) \
102 prt_printf(out, fmt "\n", __VA_ARGS__); \
105 #define sysfs_print(file, var) \
107 if (attr == &sysfs_ ## file) \
111 #define sysfs_hprint(file, val) \
113 if (attr == &sysfs_ ## file) \
114 prt_human_readable_s64(out, val); \
117 #define sysfs_strtoul(file, var) \
119 if (attr == &sysfs_ ## file) \
120 return strtoul_safe(buf, var) ?: (ssize_t) size; \
123 #define sysfs_strtoul_clamp(file, var, min, max) \
125 if (attr == &sysfs_ ## file) \
126 return strtoul_safe_clamp(buf, var, min, max) \
130 #define strtoul_or_return(cp) \
133 int _r = kstrtoul(cp, 10, &_v); \
139 write_attribute(trigger_gc);
140 write_attribute(trigger_discards);
141 write_attribute(trigger_invalidates);
142 write_attribute(trigger_journal_flush);
143 write_attribute(trigger_btree_cache_shrink);
144 write_attribute(trigger_btree_key_cache_shrink);
145 rw_attribute(gc_gens_pos);
147 read_attribute(uuid);
148 read_attribute(minor);
149 read_attribute(flags);
150 read_attribute(bucket_size);
151 read_attribute(first_bucket);
152 read_attribute(nbuckets);
153 rw_attribute(durability);
154 read_attribute(io_done);
155 read_attribute(io_errors);
156 write_attribute(io_errors_reset);
158 read_attribute(io_latency_read);
159 read_attribute(io_latency_write);
160 read_attribute(io_latency_stats_read);
161 read_attribute(io_latency_stats_write);
162 read_attribute(congested);
164 read_attribute(btree_write_stats);
166 read_attribute(btree_cache_size);
167 read_attribute(compression_stats);
168 read_attribute(journal_debug);
169 read_attribute(btree_cache);
170 read_attribute(btree_key_cache);
171 read_attribute(stripes_heap);
172 read_attribute(open_buckets);
173 read_attribute(open_buckets_partial);
174 read_attribute(write_points);
175 read_attribute(nocow_lock_table);
177 #ifdef BCH_WRITE_REF_DEBUG
178 read_attribute(write_refs);
180 static const char * const bch2_write_refs[] = {
187 static void bch2_write_refs_to_text(struct printbuf *out, struct bch_fs *c)
189 bch2_printbuf_tabstop_push(out, 24);
191 for (unsigned i = 0; i < ARRAY_SIZE(c->writes); i++)
192 prt_printf(out, "%s\t%li\n", bch2_write_refs[i], atomic_long_read(&c->writes[i]));
196 read_attribute(internal_uuid);
197 read_attribute(disk_groups);
199 read_attribute(has_data);
200 read_attribute(alloc_debug);
202 #define x(t, n, ...) read_attribute(t);
203 BCH_PERSISTENT_COUNTERS()
206 rw_attribute(discard);
209 rw_attribute(copy_gc_enabled);
210 read_attribute(copy_gc_wait);
212 rw_attribute(rebalance_enabled);
213 sysfs_pd_controller_attribute(rebalance);
214 read_attribute(rebalance_status);
215 rw_attribute(promote_whole_extents);
217 read_attribute(new_stripes);
219 read_attribute(io_timers_read);
220 read_attribute(io_timers_write);
222 read_attribute(moving_ctxts);
224 #ifdef CONFIG_BCACHEFS_TESTS
225 write_attribute(perf_test);
226 #endif /* CONFIG_BCACHEFS_TESTS */
229 static struct attribute sysfs_time_stat_##_name = \
230 { .name = #_name, .mode = 0444 };
234 static struct attribute sysfs_state_rw = {
239 static size_t bch2_btree_cache_size(struct bch_fs *c)
244 mutex_lock(&c->btree_cache.lock);
245 list_for_each_entry(b, &c->btree_cache.live, list)
246 ret += btree_buf_bytes(b);
248 mutex_unlock(&c->btree_cache.lock);
252 static int bch2_compression_stats_to_text(struct printbuf *out, struct bch_fs *c)
254 struct btree_trans *trans;
256 struct compression_type_stats {
258 u64 sectors_compressed;
259 u64 sectors_uncompressed;
260 } s[BCH_COMPRESSION_TYPE_NR];
261 u64 compressed_incompressible = 0;
264 memset(s, 0, sizeof(s));
266 if (!test_bit(BCH_FS_started, &c->flags))
269 trans = bch2_trans_get(c);
271 for (id = 0; id < BTREE_ID_NR; id++) {
272 if (!btree_type_has_ptrs(id))
275 ret = for_each_btree_key(trans, iter, id, POS_MIN,
276 BTREE_ITER_all_snapshots, k, ({
277 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
278 struct bch_extent_crc_unpacked crc;
279 const union bch_extent_entry *entry;
280 bool compressed = false, incompressible = false;
282 bkey_for_each_crc(k.k, ptrs, crc, entry) {
283 incompressible |= crc.compression_type == BCH_COMPRESSION_TYPE_incompressible;
284 compressed |= crc_is_compressed(crc);
286 if (crc_is_compressed(crc)) {
287 s[crc.compression_type].nr_extents++;
288 s[crc.compression_type].sectors_compressed += crc.compressed_size;
289 s[crc.compression_type].sectors_uncompressed += crc.uncompressed_size;
293 compressed_incompressible += compressed && incompressible;
296 unsigned t = incompressible ? BCH_COMPRESSION_TYPE_incompressible : 0;
299 s[t].sectors_compressed += k.k->size;
300 s[t].sectors_uncompressed += k.k->size;
306 bch2_trans_put(trans);
311 printbuf_tabstop_push(out, 12);
312 printbuf_tabstop_push(out, 16);
313 printbuf_tabstop_push(out, 16);
314 printbuf_tabstop_push(out, 24);
315 prt_printf(out, "type\tcompressed\runcompressed\raverage extent size\r\n");
317 for (unsigned i = 0; i < ARRAY_SIZE(s); i++) {
318 bch2_prt_compression_type(out, i);
321 prt_human_readable_u64(out, s[i].sectors_compressed << 9);
324 prt_human_readable_u64(out, s[i].sectors_uncompressed << 9);
327 prt_human_readable_u64(out, s[i].nr_extents
328 ? div_u64(s[i].sectors_uncompressed << 9, s[i].nr_extents)
334 if (compressed_incompressible) {
335 prt_printf(out, "%llu compressed & incompressible extents", compressed_incompressible);
342 static void bch2_gc_gens_pos_to_text(struct printbuf *out, struct bch_fs *c)
344 prt_printf(out, "%s: ", bch2_btree_id_str(c->gc_gens_btree));
345 bch2_bpos_to_text(out, c->gc_gens_pos);
346 prt_printf(out, "\n");
351 struct bch_fs *c = container_of(kobj, struct bch_fs, kobj);
353 sysfs_print(minor, c->minor);
354 sysfs_printf(internal_uuid, "%pU", c->sb.uuid.b);
356 if (attr == &sysfs_flags)
357 prt_bitflags(out, bch2_fs_flag_strs, c->flags);
359 sysfs_hprint(btree_cache_size, bch2_btree_cache_size(c));
361 if (attr == &sysfs_btree_write_stats)
362 bch2_btree_write_stats_to_text(out, c);
364 if (attr == &sysfs_gc_gens_pos)
365 bch2_gc_gens_pos_to_text(out, c);
367 sysfs_printf(copy_gc_enabled, "%i", c->copy_gc_enabled);
369 sysfs_printf(rebalance_enabled, "%i", c->rebalance.enabled);
370 sysfs_pd_controller_show(rebalance, &c->rebalance.pd); /* XXX */
372 if (attr == &sysfs_copy_gc_wait)
373 bch2_copygc_wait_to_text(out, c);
375 if (attr == &sysfs_rebalance_status)
376 bch2_rebalance_status_to_text(out, c);
378 sysfs_print(promote_whole_extents, c->promote_whole_extents);
382 if (attr == &sysfs_journal_debug)
383 bch2_journal_debug_to_text(out, &c->journal);
385 if (attr == &sysfs_btree_cache)
386 bch2_btree_cache_to_text(out, &c->btree_cache);
388 if (attr == &sysfs_btree_key_cache)
389 bch2_btree_key_cache_to_text(out, &c->btree_key_cache);
391 if (attr == &sysfs_stripes_heap)
392 bch2_stripes_heap_to_text(out, c);
394 if (attr == &sysfs_open_buckets)
395 bch2_open_buckets_to_text(out, c);
397 if (attr == &sysfs_open_buckets_partial)
398 bch2_open_buckets_partial_to_text(out, c);
400 if (attr == &sysfs_write_points)
401 bch2_write_points_to_text(out, c);
403 if (attr == &sysfs_compression_stats)
404 bch2_compression_stats_to_text(out, c);
406 if (attr == &sysfs_new_stripes)
407 bch2_new_stripes_to_text(out, c);
409 if (attr == &sysfs_io_timers_read)
410 bch2_io_timers_to_text(out, &c->io_clock[READ]);
412 if (attr == &sysfs_io_timers_write)
413 bch2_io_timers_to_text(out, &c->io_clock[WRITE]);
415 if (attr == &sysfs_moving_ctxts)
416 bch2_fs_moving_ctxts_to_text(out, c);
418 #ifdef BCH_WRITE_REF_DEBUG
419 if (attr == &sysfs_write_refs)
420 bch2_write_refs_to_text(out, c);
423 if (attr == &sysfs_nocow_lock_table)
424 bch2_nocow_locks_to_text(out, &c->nocow_locks);
426 if (attr == &sysfs_disk_groups)
427 bch2_disk_groups_to_text(out, c);
429 if (attr == &sysfs_alloc_debug)
430 bch2_fs_alloc_debug_to_text(out, c);
437 struct bch_fs *c = container_of(kobj, struct bch_fs, kobj);
439 if (attr == &sysfs_copy_gc_enabled) {
440 ssize_t ret = strtoul_safe(buf, c->copy_gc_enabled)
443 if (c->copygc_thread)
444 wake_up_process(c->copygc_thread);
448 if (attr == &sysfs_rebalance_enabled) {
449 ssize_t ret = strtoul_safe(buf, c->rebalance.enabled)
456 sysfs_pd_controller_store(rebalance, &c->rebalance.pd);
458 sysfs_strtoul(promote_whole_extents, c->promote_whole_extents);
462 if (!test_bit(BCH_FS_started, &c->flags))
467 if (!bch2_write_ref_tryget(c, BCH_WRITE_REF_sysfs))
470 if (attr == &sysfs_trigger_btree_cache_shrink) {
471 struct shrink_control sc;
473 sc.gfp_mask = GFP_KERNEL;
474 sc.nr_to_scan = strtoul_or_return(buf);
475 c->btree_cache.shrink->scan_objects(c->btree_cache.shrink, &sc);
478 if (attr == &sysfs_trigger_btree_key_cache_shrink) {
479 struct shrink_control sc;
481 sc.gfp_mask = GFP_KERNEL;
482 sc.nr_to_scan = strtoul_or_return(buf);
483 c->btree_key_cache.shrink->scan_objects(c->btree_cache.shrink, &sc);
486 if (attr == &sysfs_trigger_gc)
489 if (attr == &sysfs_trigger_discards)
492 if (attr == &sysfs_trigger_invalidates)
493 bch2_do_invalidates(c);
495 if (attr == &sysfs_trigger_journal_flush) {
496 bch2_journal_flush_all_pins(&c->journal);
497 bch2_journal_meta(&c->journal);
500 #ifdef CONFIG_BCACHEFS_TESTS
501 if (attr == &sysfs_perf_test) {
502 char *tmp = kstrdup(buf, GFP_KERNEL), *p = tmp;
503 char *test = strsep(&p, " \t\n");
504 char *nr_str = strsep(&p, " \t\n");
505 char *threads_str = strsep(&p, " \t\n");
511 !(ret = kstrtouint(threads_str, 10, &threads)) &&
512 !(ret = bch2_strtoull_h(nr_str, &nr)))
513 ret = bch2_btree_perf_test(c, test, nr, threads);
520 bch2_write_ref_put(c, BCH_WRITE_REF_sysfs);
525 struct attribute *bch2_fs_files[] = {
527 &sysfs_btree_cache_size,
528 &sysfs_btree_write_stats,
530 &sysfs_promote_whole_extents,
532 &sysfs_compression_stats,
534 #ifdef CONFIG_BCACHEFS_TESTS
542 SHOW(bch2_fs_counters)
544 struct bch_fs *c = container_of(kobj, struct bch_fs, counters_kobj);
546 u64 counter_since_mount = 0;
548 printbuf_tabstop_push(out, 32);
551 if (attr == &sysfs_##t) { \
552 counter = percpu_u64_get(&c->counters[BCH_COUNTER_##t]);\
553 counter_since_mount = counter - c->counters_on_mount[BCH_COUNTER_##t];\
554 prt_printf(out, "since mount:\t"); \
555 prt_human_readable_u64(out, counter_since_mount); \
558 prt_printf(out, "since filesystem creation:\t"); \
559 prt_human_readable_u64(out, counter); \
562 BCH_PERSISTENT_COUNTERS()
567 STORE(bch2_fs_counters) {
571 SYSFS_OPS(bch2_fs_counters);
573 struct attribute *bch2_fs_counters_files[] = {
576 BCH_PERSISTENT_COUNTERS()
580 /* internal dir - just a wrapper */
582 SHOW(bch2_fs_internal)
584 struct bch_fs *c = container_of(kobj, struct bch_fs, internal);
586 return bch2_fs_to_text(out, &c->kobj, attr);
589 STORE(bch2_fs_internal)
591 struct bch_fs *c = container_of(kobj, struct bch_fs, internal);
593 return bch2_fs_store(&c->kobj, attr, buf, size);
595 SYSFS_OPS(bch2_fs_internal);
597 struct attribute *bch2_fs_internal_files[] = {
599 &sysfs_journal_debug,
601 &sysfs_btree_key_cache,
605 &sysfs_open_buckets_partial,
607 #ifdef BCH_WRITE_REF_DEBUG
610 &sysfs_nocow_lock_table,
611 &sysfs_io_timers_read,
612 &sysfs_io_timers_write,
615 &sysfs_trigger_discards,
616 &sysfs_trigger_invalidates,
617 &sysfs_trigger_journal_flush,
618 &sysfs_trigger_btree_cache_shrink,
619 &sysfs_trigger_btree_key_cache_shrink,
623 &sysfs_copy_gc_enabled,
626 &sysfs_rebalance_enabled,
627 &sysfs_rebalance_status,
628 sysfs_pd_controller_files(rebalance),
632 &sysfs_internal_uuid,
641 SHOW(bch2_fs_opts_dir)
643 struct bch_fs *c = container_of(kobj, struct bch_fs, opts_dir);
644 const struct bch_option *opt = container_of(attr, struct bch_option, attr);
645 int id = opt - bch2_opt_table;
646 u64 v = bch2_opt_get_by_id(&c->opts, id);
648 bch2_opt_to_text(out, c, c->disk_sb.sb, opt, v, OPT_SHOW_FULL_LIST);
654 STORE(bch2_fs_opts_dir)
656 struct bch_fs *c = container_of(kobj, struct bch_fs, opts_dir);
657 const struct bch_option *opt = container_of(attr, struct bch_option, attr);
658 int ret, id = opt - bch2_opt_table;
663 * We don't need to take c->writes for correctness, but it eliminates an
664 * unsightly error message in the dmesg log when we're RO:
666 if (unlikely(!bch2_write_ref_tryget(c, BCH_WRITE_REF_sysfs)))
669 tmp = kstrdup(buf, GFP_KERNEL);
675 ret = bch2_opt_parse(c, opt, strim(tmp), &v, NULL);
681 ret = bch2_opt_check_may_set(c, id, v);
685 bch2_opt_set_sb(c, opt, v);
686 bch2_opt_set_by_id(&c->opts, id, v);
689 (id == Opt_background_target ||
690 id == Opt_background_compression ||
691 (id == Opt_compression && !c->opts.background_compression)))
692 bch2_set_rebalance_needs_scan(c, 0);
696 bch2_write_ref_put(c, BCH_WRITE_REF_sysfs);
699 SYSFS_OPS(bch2_fs_opts_dir);
701 struct attribute *bch2_fs_opts_dir_files[] = { NULL };
703 int bch2_opts_create_sysfs_files(struct kobject *kobj)
705 const struct bch_option *i;
708 for (i = bch2_opt_table;
709 i < bch2_opt_table + bch2_opts_nr;
711 if (!(i->flags & OPT_FS))
714 ret = sysfs_create_file(kobj, &i->attr);
724 SHOW(bch2_fs_time_stats)
726 struct bch_fs *c = container_of(kobj, struct bch_fs, time_stats);
729 if (attr == &sysfs_time_stat_##name) \
730 bch2_time_stats_to_text(out, &c->times[BCH_TIME_##name]);
737 STORE(bch2_fs_time_stats)
741 SYSFS_OPS(bch2_fs_time_stats);
743 struct attribute *bch2_fs_time_stats_files[] = {
745 &sysfs_time_stat_##name,
751 static const char * const bch2_rw[] = {
757 static void dev_io_done_to_text(struct printbuf *out, struct bch_dev *ca)
761 for (rw = 0; rw < 2; rw++) {
762 prt_printf(out, "%s:\n", bch2_rw[rw]);
764 for (i = 1; i < BCH_DATA_NR; i++)
765 prt_printf(out, "%-12s:%12llu\n",
766 bch2_data_type_str(i),
767 percpu_u64_get(&ca->io_done->sectors[rw][i]) << 9);
773 struct bch_dev *ca = container_of(kobj, struct bch_dev, kobj);
774 struct bch_fs *c = ca->fs;
776 sysfs_printf(uuid, "%pU\n", ca->uuid.b);
778 sysfs_print(bucket_size, bucket_bytes(ca));
779 sysfs_print(first_bucket, ca->mi.first_bucket);
780 sysfs_print(nbuckets, ca->mi.nbuckets);
781 sysfs_print(durability, ca->mi.durability);
782 sysfs_print(discard, ca->mi.discard);
784 if (attr == &sysfs_label) {
786 bch2_disk_path_to_text(out, c, ca->mi.group - 1);
790 if (attr == &sysfs_has_data) {
791 prt_bitflags(out, __bch2_data_types, bch2_dev_has_data(c, ca));
795 if (attr == &sysfs_state_rw) {
796 prt_string_option(out, bch2_member_states, ca->mi.state);
800 if (attr == &sysfs_io_done)
801 dev_io_done_to_text(out, ca);
803 if (attr == &sysfs_io_errors)
804 bch2_dev_io_errors_to_text(out, ca);
806 sysfs_print(io_latency_read, atomic64_read(&ca->cur_latency[READ]));
807 sysfs_print(io_latency_write, atomic64_read(&ca->cur_latency[WRITE]));
809 if (attr == &sysfs_io_latency_stats_read)
810 bch2_time_stats_to_text(out, &ca->io_latency[READ].stats);
812 if (attr == &sysfs_io_latency_stats_write)
813 bch2_time_stats_to_text(out, &ca->io_latency[WRITE].stats);
815 sysfs_printf(congested, "%u%%",
816 clamp(atomic_read(&ca->congested), 0, CONGESTED_MAX)
817 * 100 / CONGESTED_MAX);
819 if (attr == &sysfs_alloc_debug)
820 bch2_dev_alloc_debug_to_text(out, ca);
827 struct bch_dev *ca = container_of(kobj, struct bch_dev, kobj);
828 struct bch_fs *c = ca->fs;
829 struct bch_member *mi;
831 if (attr == &sysfs_discard) {
832 bool v = strtoul_or_return(buf);
834 mutex_lock(&c->sb_lock);
835 mi = bch2_members_v2_get_mut(c->disk_sb.sb, ca->dev_idx);
837 if (v != BCH_MEMBER_DISCARD(mi)) {
838 SET_BCH_MEMBER_DISCARD(mi, v);
841 mutex_unlock(&c->sb_lock);
844 if (attr == &sysfs_durability) {
845 u64 v = strtoul_or_return(buf);
847 mutex_lock(&c->sb_lock);
848 mi = bch2_members_v2_get_mut(c->disk_sb.sb, ca->dev_idx);
850 if (v + 1 != BCH_MEMBER_DURABILITY(mi)) {
851 SET_BCH_MEMBER_DURABILITY(mi, v + 1);
854 mutex_unlock(&c->sb_lock);
857 if (attr == &sysfs_label) {
861 tmp = kstrdup(buf, GFP_KERNEL);
865 ret = bch2_dev_group_set(c, ca, strim(tmp));
871 if (attr == &sysfs_io_errors_reset)
872 bch2_dev_errors_reset(ca);
878 struct attribute *bch2_dev_files[] = {
893 &sysfs_io_errors_reset,
895 &sysfs_io_latency_read,
896 &sysfs_io_latency_write,
897 &sysfs_io_latency_stats_read,
898 &sysfs_io_latency_stats_write,
906 #endif /* _BCACHEFS_SYSFS_H_ */