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_btree_coalesce);
136 write_attribute(trigger_gc);
137 write_attribute(prune_cache);
138 rw_attribute(btree_gc_periodic);
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(bucket_quantiles_last_read);
157 read_attribute(bucket_quantiles_last_write);
158 read_attribute(bucket_quantiles_fragmentation);
159 read_attribute(bucket_quantiles_oldest_gen);
161 read_attribute(reserve_stats);
162 read_attribute(btree_cache_size);
163 read_attribute(compression_stats);
164 read_attribute(journal_debug);
165 read_attribute(journal_pins);
166 read_attribute(btree_updates);
167 read_attribute(dirty_btree_nodes);
168 read_attribute(btree_cache);
169 read_attribute(btree_key_cache);
170 read_attribute(btree_transactions);
171 read_attribute(stripes_heap);
173 read_attribute(internal_uuid);
175 read_attribute(has_data);
176 read_attribute(alloc_debug);
177 write_attribute(wake_allocator);
179 read_attribute(read_realloc_races);
180 read_attribute(extent_migrate_done);
181 read_attribute(extent_migrate_raced);
183 rw_attribute(journal_write_delay_ms);
184 rw_attribute(journal_reclaim_delay_ms);
186 rw_attribute(discard);
187 rw_attribute(cache_replacement_policy);
190 rw_attribute(copy_gc_enabled);
191 sysfs_pd_controller_attribute(copy_gc);
193 rw_attribute(rebalance_enabled);
194 sysfs_pd_controller_attribute(rebalance);
195 read_attribute(rebalance_work);
196 rw_attribute(promote_whole_extents);
198 read_attribute(new_stripes);
200 rw_attribute(pd_controllers_update_seconds);
202 read_attribute(meta_replicas_have);
203 read_attribute(data_replicas_have);
205 read_attribute(io_timers_read);
206 read_attribute(io_timers_write);
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 int fs_alloc_debug_to_text(struct printbuf *out, struct bch_fs *c)
238 struct bch_fs_usage *fs_usage = bch2_fs_usage_read(c);
243 bch2_fs_usage_to_text(out, c, fs_usage);
245 percpu_up_read(&c->mark_lock);
251 static int bch2_compression_stats_to_text(struct printbuf *out, struct bch_fs *c)
253 struct btree_trans trans;
254 struct btree_iter *iter;
256 u64 nr_uncompressed_extents = 0, uncompressed_sectors = 0,
257 nr_compressed_extents = 0,
258 compressed_sectors_compressed = 0,
259 compressed_sectors_uncompressed = 0;
262 if (!test_bit(BCH_FS_STARTED, &c->flags))
265 bch2_trans_init(&trans, c, 0, 0);
267 for_each_btree_key(&trans, iter, BTREE_ID_EXTENTS, POS_MIN, 0, k, ret)
268 if (k.k->type == KEY_TYPE_extent) {
269 struct bkey_s_c_extent e = bkey_s_c_to_extent(k);
270 const union bch_extent_entry *entry;
271 struct extent_ptr_decoded p;
273 extent_for_each_ptr_decode(e, p, entry) {
274 if (!crc_is_compressed(p.crc)) {
275 nr_uncompressed_extents++;
276 uncompressed_sectors += e.k->size;
278 nr_compressed_extents++;
279 compressed_sectors_compressed +=
280 p.crc.compressed_size;
281 compressed_sectors_uncompressed +=
282 p.crc.uncompressed_size;
285 /* only looking at the first ptr */
290 ret = bch2_trans_exit(&trans) ?: ret;
295 "uncompressed data:\n"
296 " nr extents: %llu\n"
297 " size (bytes): %llu\n"
299 " nr extents: %llu\n"
300 " compressed size (bytes): %llu\n"
301 " uncompressed size (bytes): %llu\n",
302 nr_uncompressed_extents,
303 uncompressed_sectors << 9,
304 nr_compressed_extents,
305 compressed_sectors_compressed << 9,
306 compressed_sectors_uncompressed << 9);
312 struct bch_fs *c = container_of(kobj, struct bch_fs, kobj);
313 struct printbuf out = _PBUF(buf, PAGE_SIZE);
315 sysfs_print(minor, c->minor);
316 sysfs_printf(internal_uuid, "%pU", c->sb.uuid.b);
318 sysfs_print(journal_write_delay_ms, c->journal.write_delay_ms);
319 sysfs_print(journal_reclaim_delay_ms, c->journal.reclaim_delay_ms);
321 sysfs_print(block_size, block_bytes(c));
322 sysfs_print(btree_node_size, btree_bytes(c));
323 sysfs_hprint(btree_cache_size, bch2_btree_cache_size(c));
325 sysfs_print(read_realloc_races,
326 atomic_long_read(&c->read_realloc_races));
327 sysfs_print(extent_migrate_done,
328 atomic_long_read(&c->extent_migrate_done));
329 sysfs_print(extent_migrate_raced,
330 atomic_long_read(&c->extent_migrate_raced));
332 sysfs_printf(btree_gc_periodic, "%u", (int) c->btree_gc_periodic);
334 sysfs_printf(copy_gc_enabled, "%i", c->copy_gc_enabled);
336 sysfs_print(pd_controllers_update_seconds,
337 c->pd_controllers_update_seconds);
339 sysfs_printf(rebalance_enabled, "%i", c->rebalance.enabled);
340 sysfs_pd_controller_show(rebalance, &c->rebalance.pd); /* XXX */
341 sysfs_pd_controller_show(copy_gc, &c->copygc_pd);
343 if (attr == &sysfs_rebalance_work) {
344 bch2_rebalance_work_to_text(&out, c);
345 return out.pos - buf;
348 sysfs_print(promote_whole_extents, c->promote_whole_extents);
350 sysfs_printf(meta_replicas_have, "%i", bch2_replicas_online(c, true));
351 sysfs_printf(data_replicas_have, "%i", bch2_replicas_online(c, false));
355 if (attr == &sysfs_alloc_debug)
356 return fs_alloc_debug_to_text(&out, c) ?: out.pos - buf;
358 if (attr == &sysfs_journal_debug) {
359 bch2_journal_debug_to_text(&out, &c->journal);
360 return out.pos - buf;
363 if (attr == &sysfs_journal_pins) {
364 bch2_journal_pins_to_text(&out, &c->journal);
365 return out.pos - buf;
368 if (attr == &sysfs_btree_updates) {
369 bch2_btree_updates_to_text(&out, c);
370 return out.pos - buf;
373 if (attr == &sysfs_dirty_btree_nodes) {
374 bch2_dirty_btree_nodes_to_text(&out, c);
375 return out.pos - buf;
378 if (attr == &sysfs_btree_cache) {
379 bch2_btree_cache_to_text(&out, c);
380 return out.pos - buf;
383 if (attr == &sysfs_btree_key_cache) {
384 bch2_btree_key_cache_to_text(&out, &c->btree_key_cache);
385 return out.pos - buf;
388 if (attr == &sysfs_btree_transactions) {
389 bch2_btree_trans_to_text(&out, c);
390 return out.pos - buf;
393 if (attr == &sysfs_stripes_heap) {
394 bch2_stripes_heap_to_text(&out, c);
395 return out.pos - buf;
398 if (attr == &sysfs_compression_stats) {
399 bch2_compression_stats_to_text(&out, c);
400 return out.pos - buf;
403 if (attr == &sysfs_new_stripes) {
404 bch2_new_stripes_to_text(&out, c);
405 return out.pos - buf;
408 if (attr == &sysfs_io_timers_read) {
409 bch2_io_timers_to_text(&out, &c->io_clock[READ]);
410 return out.pos - buf;
412 if (attr == &sysfs_io_timers_write) {
413 bch2_io_timers_to_text(&out, &c->io_clock[WRITE]);
414 return out.pos - buf;
422 struct bch_fs *c = container_of(kobj, struct bch_fs, kobj);
424 sysfs_strtoul(journal_write_delay_ms, c->journal.write_delay_ms);
425 sysfs_strtoul(journal_reclaim_delay_ms, c->journal.reclaim_delay_ms);
427 if (attr == &sysfs_btree_gc_periodic) {
428 ssize_t ret = strtoul_safe(buf, c->btree_gc_periodic)
431 wake_up_process(c->gc_thread);
435 if (attr == &sysfs_copy_gc_enabled) {
436 ssize_t ret = strtoul_safe(buf, c->copy_gc_enabled)
439 if (c->copygc_thread)
440 wake_up_process(c->copygc_thread);
444 if (attr == &sysfs_rebalance_enabled) {
445 ssize_t ret = strtoul_safe(buf, c->rebalance.enabled)
452 sysfs_strtoul(pd_controllers_update_seconds,
453 c->pd_controllers_update_seconds);
454 sysfs_pd_controller_store(rebalance, &c->rebalance.pd);
455 sysfs_pd_controller_store(copy_gc, &c->copygc_pd);
457 sysfs_strtoul(promote_whole_extents, c->promote_whole_extents);
461 if (!test_bit(BCH_FS_STARTED, &c->flags))
466 if (attr == &sysfs_trigger_journal_flush)
467 bch2_journal_meta(&c->journal);
469 if (attr == &sysfs_trigger_btree_coalesce)
472 if (attr == &sysfs_trigger_gc) {
474 * Full gc is currently incompatible with btree key cache:
477 down_read(&c->state_lock);
478 bch2_gc(c, NULL, false, false);
479 up_read(&c->state_lock);
485 if (attr == &sysfs_prune_cache) {
486 struct shrink_control sc;
488 sc.gfp_mask = GFP_KERNEL;
489 sc.nr_to_scan = strtoul_or_return(buf);
490 c->btree_cache.shrink.scan_objects(&c->btree_cache.shrink, &sc);
493 #ifdef CONFIG_BCACHEFS_TESTS
494 if (attr == &sysfs_perf_test) {
495 char *tmp = kstrdup(buf, GFP_KERNEL), *p = tmp;
496 char *test = strsep(&p, " \t\n");
497 char *nr_str = strsep(&p, " \t\n");
498 char *threads_str = strsep(&p, " \t\n");
504 !(ret = kstrtouint(threads_str, 10, &threads)) &&
505 !(ret = bch2_strtoull_h(nr_str, &nr)))
506 ret = bch2_btree_perf_test(c, test, nr, threads);
517 struct attribute *bch2_fs_files[] = {
520 &sysfs_btree_node_size,
521 &sysfs_btree_cache_size,
523 &sysfs_meta_replicas_have,
524 &sysfs_data_replicas_have,
526 &sysfs_journal_write_delay_ms,
527 &sysfs_journal_reclaim_delay_ms,
529 &sysfs_promote_whole_extents,
531 &sysfs_compression_stats,
533 #ifdef CONFIG_BCACHEFS_TESTS
539 /* internal dir - just a wrapper */
541 SHOW(bch2_fs_internal)
543 struct bch_fs *c = container_of(kobj, struct bch_fs, internal);
544 return bch2_fs_show(&c->kobj, attr, buf);
547 STORE(bch2_fs_internal)
549 struct bch_fs *c = container_of(kobj, struct bch_fs, internal);
550 return bch2_fs_store(&c->kobj, attr, buf, size);
552 SYSFS_OPS(bch2_fs_internal);
554 struct attribute *bch2_fs_internal_files[] = {
556 &sysfs_journal_debug,
558 &sysfs_btree_updates,
559 &sysfs_dirty_btree_nodes,
561 &sysfs_btree_key_cache,
562 &sysfs_btree_transactions,
565 &sysfs_read_realloc_races,
566 &sysfs_extent_migrate_done,
567 &sysfs_extent_migrate_raced,
569 &sysfs_trigger_journal_flush,
570 &sysfs_trigger_btree_coalesce,
574 &sysfs_copy_gc_enabled,
576 &sysfs_rebalance_enabled,
577 &sysfs_rebalance_work,
578 sysfs_pd_controller_files(rebalance),
579 sysfs_pd_controller_files(copy_gc),
583 &sysfs_io_timers_read,
584 &sysfs_io_timers_write,
586 &sysfs_internal_uuid,
592 SHOW(bch2_fs_opts_dir)
594 struct printbuf out = _PBUF(buf, PAGE_SIZE);
595 struct bch_fs *c = container_of(kobj, struct bch_fs, opts_dir);
596 const struct bch_option *opt = container_of(attr, struct bch_option, attr);
597 int id = opt - bch2_opt_table;
598 u64 v = bch2_opt_get_by_id(&c->opts, id);
600 bch2_opt_to_text(&out, c, opt, v, OPT_SHOW_FULL_LIST);
603 return out.pos - buf;
606 STORE(bch2_fs_opts_dir)
608 struct bch_fs *c = container_of(kobj, struct bch_fs, opts_dir);
609 const struct bch_option *opt = container_of(attr, struct bch_option, attr);
610 int ret, id = opt - bch2_opt_table;
614 tmp = kstrdup(buf, GFP_KERNEL);
618 ret = bch2_opt_parse(c, opt, strim(tmp), &v);
624 ret = bch2_opt_check_may_set(c, id, v);
628 if (opt->set_sb != SET_NO_SB_OPT) {
629 mutex_lock(&c->sb_lock);
630 opt->set_sb(c->disk_sb.sb, v);
632 mutex_unlock(&c->sb_lock);
635 bch2_opt_set_by_id(&c->opts, id, v);
637 if ((id == Opt_background_target ||
638 id == Opt_background_compression) && v) {
639 bch2_rebalance_add_work(c, S64_MAX);
645 SYSFS_OPS(bch2_fs_opts_dir);
647 struct attribute *bch2_fs_opts_dir_files[] = { NULL };
649 int bch2_opts_create_sysfs_files(struct kobject *kobj)
651 const struct bch_option *i;
654 for (i = bch2_opt_table;
655 i < bch2_opt_table + bch2_opts_nr;
657 if (!(i->mode & (OPT_FORMAT|OPT_MOUNT|OPT_RUNTIME)))
660 ret = sysfs_create_file(kobj, &i->attr);
670 SHOW(bch2_fs_time_stats)
672 struct bch_fs *c = container_of(kobj, struct bch_fs, time_stats);
673 struct printbuf out = _PBUF(buf, PAGE_SIZE);
676 if (attr == &sysfs_time_stat_##name) { \
677 bch2_time_stats_to_text(&out, &c->times[BCH_TIME_##name]);\
678 return out.pos - buf; \
686 STORE(bch2_fs_time_stats)
690 SYSFS_OPS(bch2_fs_time_stats);
692 struct attribute *bch2_fs_time_stats_files[] = {
694 &sysfs_time_stat_##name,
700 typedef unsigned (bucket_map_fn)(struct bch_fs *, struct bch_dev *,
703 static unsigned bucket_last_io_fn(struct bch_fs *c, struct bch_dev *ca,
704 size_t b, void *private)
706 int rw = (private ? 1 : 0);
708 return bucket_last_io(c, bucket(ca, b), rw);
711 static unsigned bucket_sectors_used_fn(struct bch_fs *c, struct bch_dev *ca,
712 size_t b, void *private)
714 struct bucket *g = bucket(ca, b);
715 return bucket_sectors_used(g->mark);
718 static unsigned bucket_oldest_gen_fn(struct bch_fs *c, struct bch_dev *ca,
719 size_t b, void *private)
721 return bucket_gc_gen(ca, b);
724 static int unsigned_cmp(const void *_l, const void *_r)
726 const unsigned *l = _l;
727 const unsigned *r = _r;
729 return cmp_int(*l, *r);
732 static int quantiles_to_text(struct printbuf *out,
733 struct bch_fs *c, struct bch_dev *ca,
734 bucket_map_fn *fn, void *private)
737 /* Compute 31 quantiles */
740 down_read(&ca->bucket_lock);
743 p = vzalloc(n * sizeof(unsigned));
745 up_read(&ca->bucket_lock);
749 for (i = ca->mi.first_bucket; i < n; i++)
750 p[i] = fn(c, ca, i, private);
752 sort(p, n, sizeof(unsigned), unsigned_cmp, NULL);
753 up_read(&ca->bucket_lock);
759 for (i = 0; i < ARRAY_SIZE(q); i++)
760 q[i] = p[n * (i + 1) / (ARRAY_SIZE(q) + 1)];
764 for (i = 0; i < ARRAY_SIZE(q); i++)
765 pr_buf(out, "%u ", q[i]);
770 static void reserve_stats_to_text(struct printbuf *out, struct bch_dev *ca)
772 enum alloc_reserve i;
774 spin_lock(&ca->fs->freelist_lock);
776 pr_buf(out, "free_inc:\t%zu\t%zu\n",
777 fifo_used(&ca->free_inc),
780 for (i = 0; i < RESERVE_NR; i++)
781 pr_buf(out, "free[%u]:\t%zu\t%zu\n", i,
782 fifo_used(&ca->free[i]),
785 spin_unlock(&ca->fs->freelist_lock);
788 static void dev_alloc_debug_to_text(struct printbuf *out, struct bch_dev *ca)
790 struct bch_fs *c = ca->fs;
791 struct bch_dev_usage stats = bch2_dev_usage_read(ca);
792 unsigned i, nr[BCH_DATA_NR];
794 memset(nr, 0, sizeof(nr));
796 for (i = 0; i < ARRAY_SIZE(c->open_buckets); i++)
797 nr[c->open_buckets[i].type]++;
800 "free_inc: %zu/%zu\n"
801 "free[RESERVE_MOVINGGC]: %zu/%zu\n"
802 "free[RESERVE_NONE]: %zu/%zu\n"
811 " erasure coded: %llu\n"
819 " erasure coded: %llu\n"
820 " fragmented: %llu\n"
821 " copygc threshold: %llu\n"
822 "freelist_wait: %s\n"
823 "open buckets: %u/%u (reserved %u)\n"
824 "open_buckets_wait: %s\n"
825 "open_buckets_btree: %u\n"
826 "open_buckets_user: %u\n"
827 "btree reserve cache: %u\n",
828 fifo_used(&ca->free_inc), ca->free_inc.size,
829 fifo_used(&ca->free[RESERVE_MOVINGGC]), ca->free[RESERVE_MOVINGGC].size,
830 fifo_used(&ca->free[RESERVE_NONE]), ca->free[RESERVE_NONE].size,
831 ca->mi.nbuckets - ca->mi.first_bucket,
833 stats.buckets[BCH_DATA_sb],
834 stats.buckets[BCH_DATA_journal],
835 stats.buckets[BCH_DATA_btree],
836 stats.buckets[BCH_DATA_user],
837 stats.buckets[BCH_DATA_cached],
839 __dev_buckets_available(ca, stats),
840 stats.sectors[BCH_DATA_sb],
841 stats.sectors[BCH_DATA_journal],
842 stats.sectors[BCH_DATA_btree],
843 stats.sectors[BCH_DATA_user],
844 stats.sectors[BCH_DATA_cached],
846 stats.sectors_fragmented,
848 c->freelist_wait.list.first ? "waiting" : "empty",
849 c->open_buckets_nr_free, OPEN_BUCKETS_COUNT,
850 BTREE_NODE_OPEN_BUCKET_RESERVE,
851 c->open_buckets_wait.list.first ? "waiting" : "empty",
854 c->btree_reserve_cache_nr);
857 static const char * const bch2_rw[] = {
863 static void dev_iodone_to_text(struct printbuf *out, struct bch_dev *ca)
867 for (rw = 0; rw < 2; rw++) {
868 pr_buf(out, "%s:\n", bch2_rw[rw]);
870 for (i = 1; i < BCH_DATA_NR; i++)
871 pr_buf(out, "%-12s:%12llu\n",
873 percpu_u64_get(&ca->io_done->sectors[rw][i]) << 9);
879 struct bch_dev *ca = container_of(kobj, struct bch_dev, kobj);
880 struct bch_fs *c = ca->fs;
881 struct printbuf out = _PBUF(buf, PAGE_SIZE);
883 sysfs_printf(uuid, "%pU\n", ca->uuid.b);
885 sysfs_print(bucket_size, bucket_bytes(ca));
886 sysfs_print(block_size, block_bytes(c));
887 sysfs_print(first_bucket, ca->mi.first_bucket);
888 sysfs_print(nbuckets, ca->mi.nbuckets);
889 sysfs_print(durability, ca->mi.durability);
890 sysfs_print(discard, ca->mi.discard);
892 if (attr == &sysfs_label) {
894 mutex_lock(&c->sb_lock);
895 bch2_disk_path_to_text(&out, &c->disk_sb,
897 mutex_unlock(&c->sb_lock);
901 return out.pos - buf;
904 if (attr == &sysfs_has_data) {
905 bch2_flags_to_text(&out, bch2_data_types,
906 bch2_dev_has_data(c, ca));
908 return out.pos - buf;
911 if (attr == &sysfs_cache_replacement_policy) {
912 bch2_string_opt_to_text(&out,
913 bch2_cache_replacement_policies,
916 return out.pos - buf;
919 if (attr == &sysfs_state_rw) {
920 bch2_string_opt_to_text(&out, bch2_dev_state,
923 return out.pos - buf;
926 if (attr == &sysfs_iodone) {
927 dev_iodone_to_text(&out, ca);
928 return out.pos - buf;
931 sysfs_print(io_latency_read, atomic64_read(&ca->cur_latency[READ]));
932 sysfs_print(io_latency_write, atomic64_read(&ca->cur_latency[WRITE]));
934 if (attr == &sysfs_io_latency_stats_read) {
935 bch2_time_stats_to_text(&out, &ca->io_latency[READ]);
936 return out.pos - buf;
938 if (attr == &sysfs_io_latency_stats_write) {
939 bch2_time_stats_to_text(&out, &ca->io_latency[WRITE]);
940 return out.pos - buf;
943 sysfs_printf(congested, "%u%%",
944 clamp(atomic_read(&ca->congested), 0, CONGESTED_MAX)
945 * 100 / CONGESTED_MAX);
947 if (attr == &sysfs_bucket_quantiles_last_read)
948 return quantiles_to_text(&out, c, ca, bucket_last_io_fn, (void *) 0) ?: out.pos - buf;
949 if (attr == &sysfs_bucket_quantiles_last_write)
950 return quantiles_to_text(&out, c, ca, bucket_last_io_fn, (void *) 1) ?: out.pos - buf;
951 if (attr == &sysfs_bucket_quantiles_fragmentation)
952 return quantiles_to_text(&out, c, ca, bucket_sectors_used_fn, NULL) ?: out.pos - buf;
953 if (attr == &sysfs_bucket_quantiles_oldest_gen)
954 return quantiles_to_text(&out, c, ca, bucket_oldest_gen_fn, NULL) ?: out.pos - buf;
956 if (attr == &sysfs_reserve_stats) {
957 reserve_stats_to_text(&out, ca);
958 return out.pos - buf;
960 if (attr == &sysfs_alloc_debug) {
961 dev_alloc_debug_to_text(&out, ca);
962 return out.pos - buf;
970 struct bch_dev *ca = container_of(kobj, struct bch_dev, kobj);
971 struct bch_fs *c = ca->fs;
972 struct bch_member *mi;
974 if (attr == &sysfs_discard) {
975 bool v = strtoul_or_return(buf);
977 mutex_lock(&c->sb_lock);
978 mi = &bch2_sb_get_members(c->disk_sb.sb)->members[ca->dev_idx];
980 if (v != BCH_MEMBER_DISCARD(mi)) {
981 SET_BCH_MEMBER_DISCARD(mi, v);
984 mutex_unlock(&c->sb_lock);
987 if (attr == &sysfs_cache_replacement_policy) {
988 ssize_t v = __sysfs_match_string(bch2_cache_replacement_policies, -1, buf);
993 mutex_lock(&c->sb_lock);
994 mi = &bch2_sb_get_members(c->disk_sb.sb)->members[ca->dev_idx];
996 if ((unsigned) v != BCH_MEMBER_REPLACEMENT(mi)) {
997 SET_BCH_MEMBER_REPLACEMENT(mi, v);
1000 mutex_unlock(&c->sb_lock);
1003 if (attr == &sysfs_label) {
1007 tmp = kstrdup(buf, GFP_KERNEL);
1011 ret = bch2_dev_group_set(c, ca, strim(tmp));
1017 if (attr == &sysfs_wake_allocator)
1018 bch2_wake_allocator(ca);
1022 SYSFS_OPS(bch2_dev);
1024 struct attribute *bch2_dev_files[] = {
1028 &sysfs_first_bucket,
1034 &sysfs_cache_replacement_policy,
1041 &sysfs_io_latency_read,
1042 &sysfs_io_latency_write,
1043 &sysfs_io_latency_stats_read,
1044 &sysfs_io_latency_stats_write,
1047 /* alloc info - other stats: */
1048 &sysfs_bucket_quantiles_last_read,
1049 &sysfs_bucket_quantiles_last_write,
1050 &sysfs_bucket_quantiles_fragmentation,
1051 &sysfs_bucket_quantiles_oldest_gen,
1053 &sysfs_reserve_stats,
1057 &sysfs_wake_allocator,
1061 #endif /* _BCACHEFS_SYSFS_H_ */