1 // SPDX-License-Identifier: GPL-2.0
3 * bcache sysfs interfaces
5 * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com>
6 * Copyright 2012 Google, Inc.
9 #ifndef NO_BCACHEFS_SYSFS
12 #include "alloc_background.h"
14 #include "btree_cache.h"
16 #include "btree_iter.h"
17 #include "btree_key_cache.h"
18 #include "btree_update.h"
19 #include "btree_update_interior.h"
23 #include "disk_groups.h"
30 #include "rebalance.h"
35 #include <linux/blkdev.h>
36 #include <linux/sort.h>
37 #include <linux/sched/clock.h>
41 #define SYSFS_OPS(type) \
42 struct sysfs_ops type ## _sysfs_ops = { \
43 .show = type ## _show, \
44 .store = type ## _store \
48 static ssize_t fn ## _show(struct kobject *kobj, struct attribute *attr,\
52 static ssize_t fn ## _store(struct kobject *kobj, struct attribute *attr,\
53 const char *buf, size_t size) \
55 #define __sysfs_attribute(_name, _mode) \
56 static struct attribute sysfs_##_name = \
57 { .name = #_name, .mode = _mode }
59 #define write_attribute(n) __sysfs_attribute(n, S_IWUSR)
60 #define read_attribute(n) __sysfs_attribute(n, S_IRUGO)
61 #define rw_attribute(n) __sysfs_attribute(n, S_IRUGO|S_IWUSR)
63 #define sysfs_printf(file, fmt, ...) \
65 if (attr == &sysfs_ ## file) \
66 return scnprintf(buf, PAGE_SIZE, fmt "\n", __VA_ARGS__);\
69 #define sysfs_print(file, var) \
71 if (attr == &sysfs_ ## file) \
72 return snprint(buf, PAGE_SIZE, var); \
75 #define sysfs_hprint(file, val) \
77 if (attr == &sysfs_ ## file) { \
78 bch2_hprint(&out, val); \
80 return out.pos - buf; \
84 #define var_printf(_var, fmt) sysfs_printf(_var, fmt, var(_var))
85 #define var_print(_var) sysfs_print(_var, var(_var))
86 #define var_hprint(_var) sysfs_hprint(_var, var(_var))
88 #define sysfs_strtoul(file, var) \
90 if (attr == &sysfs_ ## file) \
91 return strtoul_safe(buf, var) ?: (ssize_t) size; \
94 #define sysfs_strtoul_clamp(file, var, min, max) \
96 if (attr == &sysfs_ ## file) \
97 return strtoul_safe_clamp(buf, var, min, max) \
101 #define strtoul_or_return(cp) \
104 int _r = kstrtoul(cp, 10, &_v); \
110 #define strtoul_restrict_or_return(cp, min, max) \
112 unsigned long __v = 0; \
113 int _r = strtoul_safe_restrict(cp, __v, min, max); \
119 #define strtoi_h_or_return(cp) \
122 int _r = strtoi_h(cp, &_v); \
128 #define sysfs_hatoi(file, var) \
130 if (attr == &sysfs_ ## file) \
131 return strtoi_h(buf, &var) ?: (ssize_t) size; \
134 write_attribute(trigger_journal_flush);
135 write_attribute(trigger_gc);
136 write_attribute(prune_cache);
137 rw_attribute(btree_gc_periodic);
138 rw_attribute(gc_gens_pos);
140 read_attribute(uuid);
141 read_attribute(minor);
142 read_attribute(bucket_size);
143 read_attribute(block_size);
144 read_attribute(btree_node_size);
145 read_attribute(first_bucket);
146 read_attribute(nbuckets);
147 read_attribute(durability);
148 read_attribute(iodone);
150 read_attribute(io_latency_read);
151 read_attribute(io_latency_write);
152 read_attribute(io_latency_stats_read);
153 read_attribute(io_latency_stats_write);
154 read_attribute(congested);
156 read_attribute(btree_avg_write_size);
158 read_attribute(bucket_quantiles_last_read);
159 read_attribute(bucket_quantiles_last_write);
160 read_attribute(bucket_quantiles_fragmentation);
161 read_attribute(bucket_quantiles_oldest_gen);
163 read_attribute(reserve_stats);
164 read_attribute(btree_cache_size);
165 read_attribute(compression_stats);
166 read_attribute(journal_debug);
167 read_attribute(journal_pins);
168 read_attribute(btree_updates);
169 read_attribute(dirty_btree_nodes);
170 read_attribute(btree_cache);
171 read_attribute(btree_key_cache);
172 read_attribute(btree_transactions);
173 read_attribute(stripes_heap);
174 read_attribute(open_buckets);
176 read_attribute(internal_uuid);
178 read_attribute(has_data);
179 read_attribute(alloc_debug);
180 write_attribute(wake_allocator);
182 read_attribute(read_realloc_races);
183 read_attribute(extent_migrate_done);
184 read_attribute(extent_migrate_raced);
186 rw_attribute(journal_write_delay_ms);
187 rw_attribute(journal_reclaim_delay_ms);
189 rw_attribute(discard);
190 rw_attribute(cache_replacement_policy);
193 rw_attribute(copy_gc_enabled);
194 read_attribute(copy_gc_wait);
196 rw_attribute(rebalance_enabled);
197 sysfs_pd_controller_attribute(rebalance);
198 read_attribute(rebalance_work);
199 rw_attribute(promote_whole_extents);
201 read_attribute(new_stripes);
203 read_attribute(io_timers_read);
204 read_attribute(io_timers_write);
206 read_attribute(data_op_data_progress);
208 #ifdef CONFIG_BCACHEFS_TESTS
209 write_attribute(perf_test);
210 #endif /* CONFIG_BCACHEFS_TESTS */
213 static struct attribute sysfs_time_stat_##_name = \
214 { .name = #_name, .mode = S_IRUGO };
218 static struct attribute sysfs_state_rw = {
223 static size_t bch2_btree_cache_size(struct bch_fs *c)
228 mutex_lock(&c->btree_cache.lock);
229 list_for_each_entry(b, &c->btree_cache.live, list)
230 ret += btree_bytes(c);
232 mutex_unlock(&c->btree_cache.lock);
236 static size_t bch2_btree_avg_write_size(struct bch_fs *c)
238 u64 nr = atomic64_read(&c->btree_writes_nr);
239 u64 sectors = atomic64_read(&c->btree_writes_sectors);
241 return nr ? div64_u64(sectors, nr) : 0;
244 static long stats_to_text(struct printbuf *out, struct bch_fs *c,
245 struct bch_move_stats *stats)
247 pr_buf(out, "%s: data type %s btree_id %s position: ",
249 bch2_data_types[stats->data_type],
250 bch2_btree_ids[stats->btree_id]);
251 bch2_bpos_to_text(out, stats->pos);
252 pr_buf(out, "%s", "\n");
257 static long data_progress_to_text(struct printbuf *out, struct bch_fs *c)
260 struct bch_move_stats *iter;
262 mutex_lock(&c->data_progress_lock);
264 if (list_empty(&c->data_progress_list))
265 pr_buf(out, "%s", "no progress to report\n");
267 list_for_each_entry(iter, &c->data_progress_list, list) {
268 stats_to_text(out, c, iter);
271 mutex_unlock(&c->data_progress_lock);
275 static int fs_alloc_debug_to_text(struct printbuf *out, struct bch_fs *c)
277 struct bch_fs_usage_online *fs_usage = bch2_fs_usage_read(c);
282 bch2_fs_usage_to_text(out, c, fs_usage);
284 percpu_up_read(&c->mark_lock);
290 static int bch2_compression_stats_to_text(struct printbuf *out, struct bch_fs *c)
292 struct btree_trans trans;
293 struct btree_iter iter;
295 u64 nr_uncompressed_extents = 0, uncompressed_sectors = 0,
296 nr_compressed_extents = 0,
297 compressed_sectors_compressed = 0,
298 compressed_sectors_uncompressed = 0;
301 if (!test_bit(BCH_FS_STARTED, &c->flags))
304 bch2_trans_init(&trans, c, 0, 0);
306 for_each_btree_key(&trans, iter, BTREE_ID_extents, POS_MIN, 0, k, ret)
307 if (k.k->type == KEY_TYPE_extent) {
308 struct bkey_s_c_extent e = bkey_s_c_to_extent(k);
309 const union bch_extent_entry *entry;
310 struct extent_ptr_decoded p;
312 extent_for_each_ptr_decode(e, p, entry) {
313 if (!crc_is_compressed(p.crc)) {
314 nr_uncompressed_extents++;
315 uncompressed_sectors += e.k->size;
317 nr_compressed_extents++;
318 compressed_sectors_compressed +=
319 p.crc.compressed_size;
320 compressed_sectors_uncompressed +=
321 p.crc.uncompressed_size;
324 /* only looking at the first ptr */
328 bch2_trans_iter_exit(&trans, &iter);
330 bch2_trans_exit(&trans);
335 "uncompressed data:\n"
336 " nr extents: %llu\n"
337 " size (bytes): %llu\n"
339 " nr extents: %llu\n"
340 " compressed size (bytes): %llu\n"
341 " uncompressed size (bytes): %llu\n",
342 nr_uncompressed_extents,
343 uncompressed_sectors << 9,
344 nr_compressed_extents,
345 compressed_sectors_compressed << 9,
346 compressed_sectors_uncompressed << 9);
350 static void bch2_gc_gens_pos_to_text(struct printbuf *out, struct bch_fs *c)
352 pr_buf(out, "%s: ", bch2_btree_ids[c->gc_gens_btree]);
353 bch2_bpos_to_text(out, c->gc_gens_pos);
359 struct bch_fs *c = container_of(kobj, struct bch_fs, kobj);
360 struct printbuf out = _PBUF(buf, PAGE_SIZE);
362 sysfs_print(minor, c->minor);
363 sysfs_printf(internal_uuid, "%pU", c->sb.uuid.b);
365 sysfs_print(journal_write_delay_ms, c->journal.write_delay_ms);
366 sysfs_print(journal_reclaim_delay_ms, c->journal.reclaim_delay_ms);
368 sysfs_print(block_size, block_bytes(c));
369 sysfs_print(btree_node_size, btree_bytes(c));
370 sysfs_hprint(btree_cache_size, bch2_btree_cache_size(c));
371 sysfs_hprint(btree_avg_write_size, bch2_btree_avg_write_size(c));
373 sysfs_print(read_realloc_races,
374 atomic_long_read(&c->read_realloc_races));
375 sysfs_print(extent_migrate_done,
376 atomic_long_read(&c->extent_migrate_done));
377 sysfs_print(extent_migrate_raced,
378 atomic_long_read(&c->extent_migrate_raced));
380 sysfs_printf(btree_gc_periodic, "%u", (int) c->btree_gc_periodic);
382 if (attr == &sysfs_gc_gens_pos) {
383 bch2_gc_gens_pos_to_text(&out, c);
384 return out.pos - buf;
387 sysfs_printf(copy_gc_enabled, "%i", c->copy_gc_enabled);
389 sysfs_printf(rebalance_enabled, "%i", c->rebalance.enabled);
390 sysfs_pd_controller_show(rebalance, &c->rebalance.pd); /* XXX */
391 sysfs_hprint(copy_gc_wait,
392 max(0LL, c->copygc_wait -
393 atomic64_read(&c->io_clock[WRITE].now)) << 9);
395 if (attr == &sysfs_rebalance_work) {
396 bch2_rebalance_work_to_text(&out, c);
397 return out.pos - buf;
400 sysfs_print(promote_whole_extents, c->promote_whole_extents);
404 if (attr == &sysfs_alloc_debug)
405 return fs_alloc_debug_to_text(&out, c) ?: out.pos - buf;
407 if (attr == &sysfs_journal_debug) {
408 bch2_journal_debug_to_text(&out, &c->journal);
409 return out.pos - buf;
412 if (attr == &sysfs_journal_pins) {
413 bch2_journal_pins_to_text(&out, &c->journal);
414 return out.pos - buf;
417 if (attr == &sysfs_btree_updates) {
418 bch2_btree_updates_to_text(&out, c);
419 return out.pos - buf;
422 if (attr == &sysfs_dirty_btree_nodes) {
423 bch2_dirty_btree_nodes_to_text(&out, c);
424 return out.pos - buf;
427 if (attr == &sysfs_btree_cache) {
428 bch2_btree_cache_to_text(&out, c);
429 return out.pos - buf;
432 if (attr == &sysfs_btree_key_cache) {
433 bch2_btree_key_cache_to_text(&out, &c->btree_key_cache);
434 return out.pos - buf;
437 if (attr == &sysfs_btree_transactions) {
438 bch2_btree_trans_to_text(&out, c);
439 return out.pos - buf;
442 if (attr == &sysfs_stripes_heap) {
443 bch2_stripes_heap_to_text(&out, c);
444 return out.pos - buf;
447 if (attr == &sysfs_open_buckets) {
448 bch2_open_buckets_to_text(&out, c);
449 return out.pos - buf;
452 if (attr == &sysfs_compression_stats) {
453 bch2_compression_stats_to_text(&out, c);
454 return out.pos - buf;
457 if (attr == &sysfs_new_stripes) {
458 bch2_new_stripes_to_text(&out, c);
459 return out.pos - buf;
462 if (attr == &sysfs_io_timers_read) {
463 bch2_io_timers_to_text(&out, &c->io_clock[READ]);
464 return out.pos - buf;
466 if (attr == &sysfs_io_timers_write) {
467 bch2_io_timers_to_text(&out, &c->io_clock[WRITE]);
468 return out.pos - buf;
471 if (attr == &sysfs_data_op_data_progress) {
472 data_progress_to_text(&out, c);
473 return out.pos - buf;
481 struct bch_fs *c = container_of(kobj, struct bch_fs, kobj);
483 sysfs_strtoul(journal_write_delay_ms, c->journal.write_delay_ms);
484 sysfs_strtoul(journal_reclaim_delay_ms, c->journal.reclaim_delay_ms);
486 if (attr == &sysfs_btree_gc_periodic) {
487 ssize_t ret = strtoul_safe(buf, c->btree_gc_periodic)
490 wake_up_process(c->gc_thread);
494 if (attr == &sysfs_copy_gc_enabled) {
495 ssize_t ret = strtoul_safe(buf, c->copy_gc_enabled)
498 if (c->copygc_thread)
499 wake_up_process(c->copygc_thread);
503 if (attr == &sysfs_rebalance_enabled) {
504 ssize_t ret = strtoul_safe(buf, c->rebalance.enabled)
511 sysfs_pd_controller_store(rebalance, &c->rebalance.pd);
513 sysfs_strtoul(promote_whole_extents, c->promote_whole_extents);
517 if (!test_bit(BCH_FS_STARTED, &c->flags))
522 if (attr == &sysfs_trigger_journal_flush)
523 bch2_journal_meta(&c->journal);
525 if (attr == &sysfs_trigger_gc) {
527 * Full gc is currently incompatible with btree key cache:
530 down_read(&c->state_lock);
531 bch2_gc(c, false, false);
532 up_read(&c->state_lock);
538 if (attr == &sysfs_prune_cache) {
539 struct shrink_control sc;
541 sc.gfp_mask = GFP_KERNEL;
542 sc.nr_to_scan = strtoul_or_return(buf);
543 c->btree_cache.shrink.scan_objects(&c->btree_cache.shrink, &sc);
546 #ifdef CONFIG_BCACHEFS_TESTS
547 if (attr == &sysfs_perf_test) {
548 char *tmp = kstrdup(buf, GFP_KERNEL), *p = tmp;
549 char *test = strsep(&p, " \t\n");
550 char *nr_str = strsep(&p, " \t\n");
551 char *threads_str = strsep(&p, " \t\n");
557 !(ret = kstrtouint(threads_str, 10, &threads)) &&
558 !(ret = bch2_strtoull_h(nr_str, &nr)))
559 ret = bch2_btree_perf_test(c, test, nr, threads);
570 struct attribute *bch2_fs_files[] = {
573 &sysfs_btree_node_size,
574 &sysfs_btree_cache_size,
575 &sysfs_btree_avg_write_size,
577 &sysfs_journal_write_delay_ms,
578 &sysfs_journal_reclaim_delay_ms,
580 &sysfs_promote_whole_extents,
582 &sysfs_compression_stats,
584 #ifdef CONFIG_BCACHEFS_TESTS
590 /* internal dir - just a wrapper */
592 SHOW(bch2_fs_internal)
594 struct bch_fs *c = container_of(kobj, struct bch_fs, internal);
595 return bch2_fs_show(&c->kobj, attr, buf);
598 STORE(bch2_fs_internal)
600 struct bch_fs *c = container_of(kobj, struct bch_fs, internal);
601 return bch2_fs_store(&c->kobj, attr, buf, size);
603 SYSFS_OPS(bch2_fs_internal);
605 struct attribute *bch2_fs_internal_files[] = {
607 &sysfs_journal_debug,
609 &sysfs_btree_updates,
610 &sysfs_dirty_btree_nodes,
612 &sysfs_btree_key_cache,
613 &sysfs_btree_transactions,
617 &sysfs_read_realloc_races,
618 &sysfs_extent_migrate_done,
619 &sysfs_extent_migrate_raced,
621 &sysfs_trigger_journal_flush,
626 &sysfs_copy_gc_enabled,
629 &sysfs_rebalance_enabled,
630 &sysfs_rebalance_work,
631 sysfs_pd_controller_files(rebalance),
635 &sysfs_io_timers_read,
636 &sysfs_io_timers_write,
638 &sysfs_data_op_data_progress,
640 &sysfs_internal_uuid,
646 SHOW(bch2_fs_opts_dir)
648 struct printbuf out = _PBUF(buf, PAGE_SIZE);
649 struct bch_fs *c = container_of(kobj, struct bch_fs, opts_dir);
650 const struct bch_option *opt = container_of(attr, struct bch_option, attr);
651 int id = opt - bch2_opt_table;
652 u64 v = bch2_opt_get_by_id(&c->opts, id);
654 bch2_opt_to_text(&out, c, opt, v, OPT_SHOW_FULL_LIST);
657 return out.pos - buf;
660 STORE(bch2_fs_opts_dir)
662 struct bch_fs *c = container_of(kobj, struct bch_fs, opts_dir);
663 const struct bch_option *opt = container_of(attr, struct bch_option, attr);
664 int ret, id = opt - bch2_opt_table;
668 tmp = kstrdup(buf, GFP_KERNEL);
672 ret = bch2_opt_parse(c, opt, strim(tmp), &v);
678 ret = bch2_opt_check_may_set(c, id, v);
682 if (opt->set_sb != SET_NO_SB_OPT) {
683 mutex_lock(&c->sb_lock);
684 opt->set_sb(c->disk_sb.sb, v);
686 mutex_unlock(&c->sb_lock);
689 bch2_opt_set_by_id(&c->opts, id, v);
691 if ((id == Opt_background_target ||
692 id == Opt_background_compression) && v) {
693 bch2_rebalance_add_work(c, S64_MAX);
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->mode & (OPT_FORMAT|OPT_MOUNT|OPT_RUNTIME)))
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);
727 struct printbuf out = _PBUF(buf, PAGE_SIZE);
730 if (attr == &sysfs_time_stat_##name) { \
731 bch2_time_stats_to_text(&out, &c->times[BCH_TIME_##name]);\
732 return out.pos - buf; \
740 STORE(bch2_fs_time_stats)
744 SYSFS_OPS(bch2_fs_time_stats);
746 struct attribute *bch2_fs_time_stats_files[] = {
748 &sysfs_time_stat_##name,
754 typedef unsigned (bucket_map_fn)(struct bch_fs *, struct bch_dev *,
757 static unsigned bucket_last_io_fn(struct bch_fs *c, struct bch_dev *ca,
758 size_t b, void *private)
760 int rw = (private ? 1 : 0);
762 return atomic64_read(&c->io_clock[rw].now) - bucket(ca, b)->io_time[rw];
765 static unsigned bucket_sectors_used_fn(struct bch_fs *c, struct bch_dev *ca,
766 size_t b, void *private)
768 struct bucket *g = bucket(ca, b);
769 return bucket_sectors_used(g->mark);
772 static unsigned bucket_oldest_gen_fn(struct bch_fs *c, struct bch_dev *ca,
773 size_t b, void *private)
775 return bucket_gc_gen(bucket(ca, b));
778 static int unsigned_cmp(const void *_l, const void *_r)
780 const unsigned *l = _l;
781 const unsigned *r = _r;
783 return cmp_int(*l, *r);
786 static int quantiles_to_text(struct printbuf *out,
787 struct bch_fs *c, struct bch_dev *ca,
788 bucket_map_fn *fn, void *private)
791 /* Compute 31 quantiles */
794 down_read(&ca->bucket_lock);
797 p = vzalloc(n * sizeof(unsigned));
799 up_read(&ca->bucket_lock);
803 for (i = ca->mi.first_bucket; i < n; i++)
804 p[i] = fn(c, ca, i, private);
806 sort(p, n, sizeof(unsigned), unsigned_cmp, NULL);
807 up_read(&ca->bucket_lock);
813 for (i = 0; i < ARRAY_SIZE(q); i++)
814 q[i] = p[n * (i + 1) / (ARRAY_SIZE(q) + 1)];
818 for (i = 0; i < ARRAY_SIZE(q); i++)
819 pr_buf(out, "%u ", q[i]);
824 static void reserve_stats_to_text(struct printbuf *out, struct bch_dev *ca)
826 enum alloc_reserve i;
828 spin_lock(&ca->fs->freelist_lock);
830 pr_buf(out, "free_inc:\t%zu\t%zu\n",
831 fifo_used(&ca->free_inc),
834 for (i = 0; i < RESERVE_NR; i++)
835 pr_buf(out, "free[%u]:\t%zu\t%zu\n", i,
836 fifo_used(&ca->free[i]),
839 spin_unlock(&ca->fs->freelist_lock);
842 static void dev_alloc_debug_to_text(struct printbuf *out, struct bch_dev *ca)
844 struct bch_fs *c = ca->fs;
845 struct bch_dev_usage stats = bch2_dev_usage_read(ca);
846 unsigned i, nr[BCH_DATA_NR];
848 memset(nr, 0, sizeof(nr));
850 for (i = 0; i < ARRAY_SIZE(c->open_buckets); i++)
851 nr[c->open_buckets[i].type]++;
854 "\t\t buckets\t sectors fragmented\n"
856 ca->mi.nbuckets - ca->mi.first_bucket);
858 for (i = 1; i < BCH_DATA_NR; i++)
859 pr_buf(out, "%-8s%16llu%16llu%16llu\n",
860 bch2_data_types[i], stats.d[i].buckets,
861 stats.d[i].sectors, stats.d[i].fragmented);
867 "free_inc\t\t%zu/%zu\n"
868 "free[RESERVE_MOVINGGC]\t%zu/%zu\n"
869 "free[RESERVE_NONE]\t%zu/%zu\n"
870 "freelist_wait\t\t%s\n"
871 "open buckets allocated\t%u\n"
872 "open buckets this dev\t%u\n"
873 "open buckets total\t%u\n"
874 "open_buckets_wait\t%s\n"
875 "open_buckets_btree\t%u\n"
876 "open_buckets_user\t%u\n"
877 "btree reserve cache\t%u\n"
878 "thread state:\t\t%s\n",
880 __dev_buckets_available(ca, stats),
881 fifo_used(&ca->free_inc), ca->free_inc.size,
882 fifo_used(&ca->free[RESERVE_MOVINGGC]), ca->free[RESERVE_MOVINGGC].size,
883 fifo_used(&ca->free[RESERVE_NONE]), ca->free[RESERVE_NONE].size,
884 c->freelist_wait.list.first ? "waiting" : "empty",
885 OPEN_BUCKETS_COUNT - c->open_buckets_nr_free,
888 c->open_buckets_wait.list.first ? "waiting" : "empty",
891 c->btree_reserve_cache_nr,
892 bch2_allocator_states[ca->allocator_state]);
895 static const char * const bch2_rw[] = {
901 static void dev_iodone_to_text(struct printbuf *out, struct bch_dev *ca)
905 for (rw = 0; rw < 2; rw++) {
906 pr_buf(out, "%s:\n", bch2_rw[rw]);
908 for (i = 1; i < BCH_DATA_NR; i++)
909 pr_buf(out, "%-12s:%12llu\n",
911 percpu_u64_get(&ca->io_done->sectors[rw][i]) << 9);
917 struct bch_dev *ca = container_of(kobj, struct bch_dev, kobj);
918 struct bch_fs *c = ca->fs;
919 struct printbuf out = _PBUF(buf, PAGE_SIZE);
921 sysfs_printf(uuid, "%pU\n", ca->uuid.b);
923 sysfs_print(bucket_size, bucket_bytes(ca));
924 sysfs_print(block_size, block_bytes(c));
925 sysfs_print(first_bucket, ca->mi.first_bucket);
926 sysfs_print(nbuckets, ca->mi.nbuckets);
927 sysfs_print(durability, ca->mi.durability);
928 sysfs_print(discard, ca->mi.discard);
930 if (attr == &sysfs_label) {
932 mutex_lock(&c->sb_lock);
933 bch2_disk_path_to_text(&out, &c->disk_sb,
935 mutex_unlock(&c->sb_lock);
939 return out.pos - buf;
942 if (attr == &sysfs_has_data) {
943 bch2_flags_to_text(&out, bch2_data_types,
944 bch2_dev_has_data(c, ca));
946 return out.pos - buf;
949 if (attr == &sysfs_cache_replacement_policy) {
950 bch2_string_opt_to_text(&out,
951 bch2_cache_replacement_policies,
954 return out.pos - buf;
957 if (attr == &sysfs_state_rw) {
958 bch2_string_opt_to_text(&out, bch2_member_states,
961 return out.pos - buf;
964 if (attr == &sysfs_iodone) {
965 dev_iodone_to_text(&out, ca);
966 return out.pos - buf;
969 sysfs_print(io_latency_read, atomic64_read(&ca->cur_latency[READ]));
970 sysfs_print(io_latency_write, atomic64_read(&ca->cur_latency[WRITE]));
972 if (attr == &sysfs_io_latency_stats_read) {
973 bch2_time_stats_to_text(&out, &ca->io_latency[READ]);
974 return out.pos - buf;
976 if (attr == &sysfs_io_latency_stats_write) {
977 bch2_time_stats_to_text(&out, &ca->io_latency[WRITE]);
978 return out.pos - buf;
981 sysfs_printf(congested, "%u%%",
982 clamp(atomic_read(&ca->congested), 0, CONGESTED_MAX)
983 * 100 / CONGESTED_MAX);
985 if (attr == &sysfs_bucket_quantiles_last_read)
986 return quantiles_to_text(&out, c, ca, bucket_last_io_fn, (void *) 0) ?: out.pos - buf;
987 if (attr == &sysfs_bucket_quantiles_last_write)
988 return quantiles_to_text(&out, c, ca, bucket_last_io_fn, (void *) 1) ?: out.pos - buf;
989 if (attr == &sysfs_bucket_quantiles_fragmentation)
990 return quantiles_to_text(&out, c, ca, bucket_sectors_used_fn, NULL) ?: out.pos - buf;
991 if (attr == &sysfs_bucket_quantiles_oldest_gen)
992 return quantiles_to_text(&out, c, ca, bucket_oldest_gen_fn, NULL) ?: out.pos - buf;
994 if (attr == &sysfs_reserve_stats) {
995 reserve_stats_to_text(&out, ca);
996 return out.pos - buf;
998 if (attr == &sysfs_alloc_debug) {
999 dev_alloc_debug_to_text(&out, ca);
1000 return out.pos - buf;
1008 struct bch_dev *ca = container_of(kobj, struct bch_dev, kobj);
1009 struct bch_fs *c = ca->fs;
1010 struct bch_member *mi;
1012 if (attr == &sysfs_discard) {
1013 bool v = strtoul_or_return(buf);
1015 mutex_lock(&c->sb_lock);
1016 mi = &bch2_sb_get_members(c->disk_sb.sb)->members[ca->dev_idx];
1018 if (v != BCH_MEMBER_DISCARD(mi)) {
1019 SET_BCH_MEMBER_DISCARD(mi, v);
1020 bch2_write_super(c);
1022 mutex_unlock(&c->sb_lock);
1025 if (attr == &sysfs_cache_replacement_policy) {
1026 ssize_t v = __sysfs_match_string(bch2_cache_replacement_policies, -1, buf);
1031 mutex_lock(&c->sb_lock);
1032 mi = &bch2_sb_get_members(c->disk_sb.sb)->members[ca->dev_idx];
1034 if ((unsigned) v != BCH_MEMBER_REPLACEMENT(mi)) {
1035 SET_BCH_MEMBER_REPLACEMENT(mi, v);
1036 bch2_write_super(c);
1038 mutex_unlock(&c->sb_lock);
1041 if (attr == &sysfs_label) {
1045 tmp = kstrdup(buf, GFP_KERNEL);
1049 ret = bch2_dev_group_set(c, ca, strim(tmp));
1055 if (attr == &sysfs_wake_allocator)
1056 bch2_wake_allocator(ca);
1060 SYSFS_OPS(bch2_dev);
1062 struct attribute *bch2_dev_files[] = {
1066 &sysfs_first_bucket,
1072 &sysfs_cache_replacement_policy,
1079 &sysfs_io_latency_read,
1080 &sysfs_io_latency_write,
1081 &sysfs_io_latency_stats_read,
1082 &sysfs_io_latency_stats_write,
1085 /* alloc info - other stats: */
1086 &sysfs_bucket_quantiles_last_read,
1087 &sysfs_bucket_quantiles_last_write,
1088 &sysfs_bucket_quantiles_fragmentation,
1089 &sysfs_bucket_quantiles_oldest_gen,
1091 &sysfs_reserve_stats,
1095 &sysfs_wake_allocator,
1099 #endif /* _BCACHEFS_SYSFS_H_ */