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(reserve_stats);
159 read_attribute(btree_cache_size);
160 read_attribute(compression_stats);
161 read_attribute(journal_debug);
162 read_attribute(journal_pins);
163 read_attribute(btree_updates);
164 read_attribute(dirty_btree_nodes);
165 read_attribute(btree_cache);
166 read_attribute(btree_key_cache);
167 read_attribute(btree_transactions);
168 read_attribute(stripes_heap);
169 read_attribute(open_buckets);
171 read_attribute(internal_uuid);
173 read_attribute(has_data);
174 read_attribute(alloc_debug);
175 write_attribute(wake_allocator);
177 read_attribute(read_realloc_races);
178 read_attribute(extent_migrate_done);
179 read_attribute(extent_migrate_raced);
181 rw_attribute(journal_write_delay_ms);
182 rw_attribute(journal_reclaim_delay_ms);
184 rw_attribute(discard);
185 rw_attribute(cache_replacement_policy);
188 rw_attribute(copy_gc_enabled);
189 read_attribute(copy_gc_wait);
191 rw_attribute(rebalance_enabled);
192 sysfs_pd_controller_attribute(rebalance);
193 read_attribute(rebalance_work);
194 rw_attribute(promote_whole_extents);
196 read_attribute(new_stripes);
198 read_attribute(io_timers_read);
199 read_attribute(io_timers_write);
201 read_attribute(data_op_data_progress);
203 #ifdef CONFIG_BCACHEFS_TESTS
204 write_attribute(perf_test);
205 #endif /* CONFIG_BCACHEFS_TESTS */
208 static struct attribute sysfs_time_stat_##_name = \
209 { .name = #_name, .mode = S_IRUGO };
213 static struct attribute sysfs_state_rw = {
218 static size_t bch2_btree_cache_size(struct bch_fs *c)
223 mutex_lock(&c->btree_cache.lock);
224 list_for_each_entry(b, &c->btree_cache.live, list)
225 ret += btree_bytes(c);
227 mutex_unlock(&c->btree_cache.lock);
231 static size_t bch2_btree_avg_write_size(struct bch_fs *c)
233 u64 nr = atomic64_read(&c->btree_writes_nr);
234 u64 sectors = atomic64_read(&c->btree_writes_sectors);
236 return nr ? div64_u64(sectors, nr) : 0;
239 static long stats_to_text(struct printbuf *out, struct bch_fs *c,
240 struct bch_move_stats *stats)
242 pr_buf(out, "%s: data type %s btree_id %s position: ",
244 bch2_data_types[stats->data_type],
245 bch2_btree_ids[stats->btree_id]);
246 bch2_bpos_to_text(out, stats->pos);
247 pr_buf(out, "%s", "\n");
252 static long data_progress_to_text(struct printbuf *out, struct bch_fs *c)
255 struct bch_move_stats *iter;
257 mutex_lock(&c->data_progress_lock);
259 if (list_empty(&c->data_progress_list))
260 pr_buf(out, "%s", "no progress to report\n");
262 list_for_each_entry(iter, &c->data_progress_list, list) {
263 stats_to_text(out, c, iter);
266 mutex_unlock(&c->data_progress_lock);
270 static int fs_alloc_debug_to_text(struct printbuf *out, struct bch_fs *c)
272 struct bch_fs_usage_online *fs_usage = bch2_fs_usage_read(c);
277 bch2_fs_usage_to_text(out, c, fs_usage);
279 percpu_up_read(&c->mark_lock);
285 static int bch2_compression_stats_to_text(struct printbuf *out, struct bch_fs *c)
287 struct btree_trans trans;
288 struct btree_iter iter;
290 u64 nr_uncompressed_extents = 0, uncompressed_sectors = 0,
291 nr_compressed_extents = 0,
292 compressed_sectors_compressed = 0,
293 compressed_sectors_uncompressed = 0;
296 if (!test_bit(BCH_FS_STARTED, &c->flags))
299 bch2_trans_init(&trans, c, 0, 0);
301 for_each_btree_key(&trans, iter, BTREE_ID_extents, POS_MIN, 0, k, ret)
302 if (k.k->type == KEY_TYPE_extent) {
303 struct bkey_s_c_extent e = bkey_s_c_to_extent(k);
304 const union bch_extent_entry *entry;
305 struct extent_ptr_decoded p;
307 extent_for_each_ptr_decode(e, p, entry) {
308 if (!crc_is_compressed(p.crc)) {
309 nr_uncompressed_extents++;
310 uncompressed_sectors += e.k->size;
312 nr_compressed_extents++;
313 compressed_sectors_compressed +=
314 p.crc.compressed_size;
315 compressed_sectors_uncompressed +=
316 p.crc.uncompressed_size;
319 /* only looking at the first ptr */
323 bch2_trans_iter_exit(&trans, &iter);
325 bch2_trans_exit(&trans);
330 "uncompressed data:\n"
331 " nr extents: %llu\n"
332 " size (bytes): %llu\n"
334 " nr extents: %llu\n"
335 " compressed size (bytes): %llu\n"
336 " uncompressed size (bytes): %llu\n",
337 nr_uncompressed_extents,
338 uncompressed_sectors << 9,
339 nr_compressed_extents,
340 compressed_sectors_compressed << 9,
341 compressed_sectors_uncompressed << 9);
345 static void bch2_gc_gens_pos_to_text(struct printbuf *out, struct bch_fs *c)
347 pr_buf(out, "%s: ", bch2_btree_ids[c->gc_gens_btree]);
348 bch2_bpos_to_text(out, c->gc_gens_pos);
354 struct bch_fs *c = container_of(kobj, struct bch_fs, kobj);
355 struct printbuf out = _PBUF(buf, PAGE_SIZE);
357 sysfs_print(minor, c->minor);
358 sysfs_printf(internal_uuid, "%pU", c->sb.uuid.b);
360 sysfs_print(journal_write_delay_ms, c->journal.write_delay_ms);
361 sysfs_print(journal_reclaim_delay_ms, c->journal.reclaim_delay_ms);
363 sysfs_print(block_size, block_bytes(c));
364 sysfs_print(btree_node_size, btree_bytes(c));
365 sysfs_hprint(btree_cache_size, bch2_btree_cache_size(c));
366 sysfs_hprint(btree_avg_write_size, bch2_btree_avg_write_size(c));
368 sysfs_print(read_realloc_races,
369 atomic_long_read(&c->read_realloc_races));
370 sysfs_print(extent_migrate_done,
371 atomic_long_read(&c->extent_migrate_done));
372 sysfs_print(extent_migrate_raced,
373 atomic_long_read(&c->extent_migrate_raced));
375 sysfs_printf(btree_gc_periodic, "%u", (int) c->btree_gc_periodic);
377 if (attr == &sysfs_gc_gens_pos) {
378 bch2_gc_gens_pos_to_text(&out, c);
379 return out.pos - buf;
382 sysfs_printf(copy_gc_enabled, "%i", c->copy_gc_enabled);
384 sysfs_printf(rebalance_enabled, "%i", c->rebalance.enabled);
385 sysfs_pd_controller_show(rebalance, &c->rebalance.pd); /* XXX */
386 sysfs_hprint(copy_gc_wait,
387 max(0LL, c->copygc_wait -
388 atomic64_read(&c->io_clock[WRITE].now)) << 9);
390 if (attr == &sysfs_rebalance_work) {
391 bch2_rebalance_work_to_text(&out, c);
392 return out.pos - buf;
395 sysfs_print(promote_whole_extents, c->promote_whole_extents);
399 if (attr == &sysfs_alloc_debug)
400 return fs_alloc_debug_to_text(&out, c) ?: out.pos - buf;
402 if (attr == &sysfs_journal_debug) {
403 bch2_journal_debug_to_text(&out, &c->journal);
404 return out.pos - buf;
407 if (attr == &sysfs_journal_pins) {
408 bch2_journal_pins_to_text(&out, &c->journal);
409 return out.pos - buf;
412 if (attr == &sysfs_btree_updates) {
413 bch2_btree_updates_to_text(&out, c);
414 return out.pos - buf;
417 if (attr == &sysfs_dirty_btree_nodes) {
418 bch2_dirty_btree_nodes_to_text(&out, c);
419 return out.pos - buf;
422 if (attr == &sysfs_btree_cache) {
423 bch2_btree_cache_to_text(&out, c);
424 return out.pos - buf;
427 if (attr == &sysfs_btree_key_cache) {
428 bch2_btree_key_cache_to_text(&out, &c->btree_key_cache);
429 return out.pos - buf;
432 if (attr == &sysfs_btree_transactions) {
433 bch2_btree_trans_to_text(&out, c);
434 return out.pos - buf;
437 if (attr == &sysfs_stripes_heap) {
438 bch2_stripes_heap_to_text(&out, c);
439 return out.pos - buf;
442 if (attr == &sysfs_open_buckets) {
443 bch2_open_buckets_to_text(&out, c);
444 return out.pos - buf;
447 if (attr == &sysfs_compression_stats) {
448 bch2_compression_stats_to_text(&out, c);
449 return out.pos - buf;
452 if (attr == &sysfs_new_stripes) {
453 bch2_new_stripes_to_text(&out, c);
454 return out.pos - buf;
457 if (attr == &sysfs_io_timers_read) {
458 bch2_io_timers_to_text(&out, &c->io_clock[READ]);
459 return out.pos - buf;
461 if (attr == &sysfs_io_timers_write) {
462 bch2_io_timers_to_text(&out, &c->io_clock[WRITE]);
463 return out.pos - buf;
466 if (attr == &sysfs_data_op_data_progress) {
467 data_progress_to_text(&out, c);
468 return out.pos - buf;
476 struct bch_fs *c = container_of(kobj, struct bch_fs, kobj);
478 sysfs_strtoul(journal_write_delay_ms, c->journal.write_delay_ms);
479 sysfs_strtoul(journal_reclaim_delay_ms, c->journal.reclaim_delay_ms);
481 if (attr == &sysfs_btree_gc_periodic) {
482 ssize_t ret = strtoul_safe(buf, c->btree_gc_periodic)
485 wake_up_process(c->gc_thread);
489 if (attr == &sysfs_copy_gc_enabled) {
490 ssize_t ret = strtoul_safe(buf, c->copy_gc_enabled)
493 if (c->copygc_thread)
494 wake_up_process(c->copygc_thread);
498 if (attr == &sysfs_rebalance_enabled) {
499 ssize_t ret = strtoul_safe(buf, c->rebalance.enabled)
506 sysfs_pd_controller_store(rebalance, &c->rebalance.pd);
508 sysfs_strtoul(promote_whole_extents, c->promote_whole_extents);
512 if (!test_bit(BCH_FS_STARTED, &c->flags))
517 if (attr == &sysfs_trigger_journal_flush)
518 bch2_journal_meta(&c->journal);
520 if (attr == &sysfs_trigger_gc) {
522 * Full gc is currently incompatible with btree key cache:
525 down_read(&c->state_lock);
526 bch2_gc(c, false, false);
527 up_read(&c->state_lock);
533 if (attr == &sysfs_prune_cache) {
534 struct shrink_control sc;
536 sc.gfp_mask = GFP_KERNEL;
537 sc.nr_to_scan = strtoul_or_return(buf);
538 c->btree_cache.shrink.scan_objects(&c->btree_cache.shrink, &sc);
541 #ifdef CONFIG_BCACHEFS_TESTS
542 if (attr == &sysfs_perf_test) {
543 char *tmp = kstrdup(buf, GFP_KERNEL), *p = tmp;
544 char *test = strsep(&p, " \t\n");
545 char *nr_str = strsep(&p, " \t\n");
546 char *threads_str = strsep(&p, " \t\n");
552 !(ret = kstrtouint(threads_str, 10, &threads)) &&
553 !(ret = bch2_strtoull_h(nr_str, &nr)))
554 ret = bch2_btree_perf_test(c, test, nr, threads);
565 struct attribute *bch2_fs_files[] = {
568 &sysfs_btree_node_size,
569 &sysfs_btree_cache_size,
570 &sysfs_btree_avg_write_size,
572 &sysfs_journal_write_delay_ms,
573 &sysfs_journal_reclaim_delay_ms,
575 &sysfs_promote_whole_extents,
577 &sysfs_compression_stats,
579 #ifdef CONFIG_BCACHEFS_TESTS
585 /* internal dir - just a wrapper */
587 SHOW(bch2_fs_internal)
589 struct bch_fs *c = container_of(kobj, struct bch_fs, internal);
590 return bch2_fs_show(&c->kobj, attr, buf);
593 STORE(bch2_fs_internal)
595 struct bch_fs *c = container_of(kobj, struct bch_fs, internal);
596 return bch2_fs_store(&c->kobj, attr, buf, size);
598 SYSFS_OPS(bch2_fs_internal);
600 struct attribute *bch2_fs_internal_files[] = {
602 &sysfs_journal_debug,
604 &sysfs_btree_updates,
605 &sysfs_dirty_btree_nodes,
607 &sysfs_btree_key_cache,
608 &sysfs_btree_transactions,
612 &sysfs_read_realloc_races,
613 &sysfs_extent_migrate_done,
614 &sysfs_extent_migrate_raced,
616 &sysfs_trigger_journal_flush,
621 &sysfs_copy_gc_enabled,
624 &sysfs_rebalance_enabled,
625 &sysfs_rebalance_work,
626 sysfs_pd_controller_files(rebalance),
630 &sysfs_io_timers_read,
631 &sysfs_io_timers_write,
633 &sysfs_data_op_data_progress,
635 &sysfs_internal_uuid,
641 SHOW(bch2_fs_opts_dir)
643 struct printbuf out = _PBUF(buf, PAGE_SIZE);
644 struct bch_fs *c = container_of(kobj, struct bch_fs, opts_dir);
645 const struct bch_option *opt = container_of(attr, struct bch_option, attr);
646 int id = opt - bch2_opt_table;
647 u64 v = bch2_opt_get_by_id(&c->opts, id);
649 bch2_opt_to_text(&out, c, opt, v, OPT_SHOW_FULL_LIST);
652 return out.pos - buf;
655 STORE(bch2_fs_opts_dir)
657 struct bch_fs *c = container_of(kobj, struct bch_fs, opts_dir);
658 const struct bch_option *opt = container_of(attr, struct bch_option, attr);
659 int ret, id = opt - bch2_opt_table;
663 tmp = kstrdup(buf, GFP_KERNEL);
667 ret = bch2_opt_parse(c, opt, strim(tmp), &v);
673 ret = bch2_opt_check_may_set(c, id, v);
677 if (opt->set_sb != SET_NO_SB_OPT) {
678 mutex_lock(&c->sb_lock);
679 opt->set_sb(c->disk_sb.sb, v);
681 mutex_unlock(&c->sb_lock);
684 bch2_opt_set_by_id(&c->opts, id, v);
686 if ((id == Opt_background_target ||
687 id == Opt_background_compression) && v) {
688 bch2_rebalance_add_work(c, S64_MAX);
694 SYSFS_OPS(bch2_fs_opts_dir);
696 struct attribute *bch2_fs_opts_dir_files[] = { NULL };
698 int bch2_opts_create_sysfs_files(struct kobject *kobj)
700 const struct bch_option *i;
703 for (i = bch2_opt_table;
704 i < bch2_opt_table + bch2_opts_nr;
706 if (!(i->mode & (OPT_FORMAT|OPT_MOUNT|OPT_RUNTIME)))
709 ret = sysfs_create_file(kobj, &i->attr);
719 SHOW(bch2_fs_time_stats)
721 struct bch_fs *c = container_of(kobj, struct bch_fs, time_stats);
722 struct printbuf out = _PBUF(buf, PAGE_SIZE);
725 if (attr == &sysfs_time_stat_##name) { \
726 bch2_time_stats_to_text(&out, &c->times[BCH_TIME_##name]);\
727 return out.pos - buf; \
735 STORE(bch2_fs_time_stats)
739 SYSFS_OPS(bch2_fs_time_stats);
741 struct attribute *bch2_fs_time_stats_files[] = {
743 &sysfs_time_stat_##name,
749 static void reserve_stats_to_text(struct printbuf *out, struct bch_dev *ca)
751 enum alloc_reserve i;
753 spin_lock(&ca->fs->freelist_lock);
755 pr_buf(out, "free_inc:\t%zu\t%zu\n",
756 fifo_used(&ca->free_inc),
759 for (i = 0; i < RESERVE_NR; i++)
760 pr_buf(out, "free[%u]:\t%zu\t%zu\n", i,
761 fifo_used(&ca->free[i]),
764 spin_unlock(&ca->fs->freelist_lock);
767 static void dev_alloc_debug_to_text(struct printbuf *out, struct bch_dev *ca)
769 struct bch_fs *c = ca->fs;
770 struct bch_dev_usage stats = bch2_dev_usage_read(ca);
771 unsigned i, nr[BCH_DATA_NR];
773 memset(nr, 0, sizeof(nr));
775 for (i = 0; i < ARRAY_SIZE(c->open_buckets); i++)
776 nr[c->open_buckets[i].type]++;
779 "\t\t buckets\t sectors fragmented\n"
781 ca->mi.nbuckets - ca->mi.first_bucket);
783 for (i = 1; i < BCH_DATA_NR; i++)
784 pr_buf(out, "%-8s%16llu%16llu%16llu\n",
785 bch2_data_types[i], stats.d[i].buckets,
786 stats.d[i].sectors, stats.d[i].fragmented);
792 "free_inc\t\t%zu/%zu\n"
793 "free[RESERVE_MOVINGGC]\t%zu/%zu\n"
794 "free[RESERVE_NONE]\t%zu/%zu\n"
795 "freelist_wait\t\t%s\n"
796 "open buckets allocated\t%u\n"
797 "open buckets this dev\t%u\n"
798 "open buckets total\t%u\n"
799 "open_buckets_wait\t%s\n"
800 "open_buckets_btree\t%u\n"
801 "open_buckets_user\t%u\n"
802 "btree reserve cache\t%u\n"
803 "thread state:\t\t%s\n",
805 __dev_buckets_available(ca, stats),
806 fifo_used(&ca->free_inc), ca->free_inc.size,
807 fifo_used(&ca->free[RESERVE_MOVINGGC]), ca->free[RESERVE_MOVINGGC].size,
808 fifo_used(&ca->free[RESERVE_NONE]), ca->free[RESERVE_NONE].size,
809 c->freelist_wait.list.first ? "waiting" : "empty",
810 OPEN_BUCKETS_COUNT - c->open_buckets_nr_free,
813 c->open_buckets_wait.list.first ? "waiting" : "empty",
816 c->btree_reserve_cache_nr,
817 bch2_allocator_states[ca->allocator_state]);
820 static const char * const bch2_rw[] = {
826 static void dev_iodone_to_text(struct printbuf *out, struct bch_dev *ca)
830 for (rw = 0; rw < 2; rw++) {
831 pr_buf(out, "%s:\n", bch2_rw[rw]);
833 for (i = 1; i < BCH_DATA_NR; i++)
834 pr_buf(out, "%-12s:%12llu\n",
836 percpu_u64_get(&ca->io_done->sectors[rw][i]) << 9);
842 struct bch_dev *ca = container_of(kobj, struct bch_dev, kobj);
843 struct bch_fs *c = ca->fs;
844 struct printbuf out = _PBUF(buf, PAGE_SIZE);
846 sysfs_printf(uuid, "%pU\n", ca->uuid.b);
848 sysfs_print(bucket_size, bucket_bytes(ca));
849 sysfs_print(block_size, block_bytes(c));
850 sysfs_print(first_bucket, ca->mi.first_bucket);
851 sysfs_print(nbuckets, ca->mi.nbuckets);
852 sysfs_print(durability, ca->mi.durability);
853 sysfs_print(discard, ca->mi.discard);
855 if (attr == &sysfs_label) {
857 mutex_lock(&c->sb_lock);
858 bch2_disk_path_to_text(&out, &c->disk_sb,
860 mutex_unlock(&c->sb_lock);
864 return out.pos - buf;
867 if (attr == &sysfs_has_data) {
868 bch2_flags_to_text(&out, bch2_data_types,
869 bch2_dev_has_data(c, ca));
871 return out.pos - buf;
874 if (attr == &sysfs_cache_replacement_policy) {
875 bch2_string_opt_to_text(&out,
876 bch2_cache_replacement_policies,
879 return out.pos - buf;
882 if (attr == &sysfs_state_rw) {
883 bch2_string_opt_to_text(&out, bch2_member_states,
886 return out.pos - buf;
889 if (attr == &sysfs_iodone) {
890 dev_iodone_to_text(&out, ca);
891 return out.pos - buf;
894 sysfs_print(io_latency_read, atomic64_read(&ca->cur_latency[READ]));
895 sysfs_print(io_latency_write, atomic64_read(&ca->cur_latency[WRITE]));
897 if (attr == &sysfs_io_latency_stats_read) {
898 bch2_time_stats_to_text(&out, &ca->io_latency[READ]);
899 return out.pos - buf;
901 if (attr == &sysfs_io_latency_stats_write) {
902 bch2_time_stats_to_text(&out, &ca->io_latency[WRITE]);
903 return out.pos - buf;
906 sysfs_printf(congested, "%u%%",
907 clamp(atomic_read(&ca->congested), 0, CONGESTED_MAX)
908 * 100 / CONGESTED_MAX);
910 if (attr == &sysfs_reserve_stats) {
911 reserve_stats_to_text(&out, ca);
912 return out.pos - buf;
914 if (attr == &sysfs_alloc_debug) {
915 dev_alloc_debug_to_text(&out, ca);
916 return out.pos - buf;
924 struct bch_dev *ca = container_of(kobj, struct bch_dev, kobj);
925 struct bch_fs *c = ca->fs;
926 struct bch_member *mi;
928 if (attr == &sysfs_discard) {
929 bool v = strtoul_or_return(buf);
931 mutex_lock(&c->sb_lock);
932 mi = &bch2_sb_get_members(c->disk_sb.sb)->members[ca->dev_idx];
934 if (v != BCH_MEMBER_DISCARD(mi)) {
935 SET_BCH_MEMBER_DISCARD(mi, v);
938 mutex_unlock(&c->sb_lock);
941 if (attr == &sysfs_cache_replacement_policy) {
942 ssize_t v = __sysfs_match_string(bch2_cache_replacement_policies, -1, buf);
947 mutex_lock(&c->sb_lock);
948 mi = &bch2_sb_get_members(c->disk_sb.sb)->members[ca->dev_idx];
950 if ((unsigned) v != BCH_MEMBER_REPLACEMENT(mi)) {
951 SET_BCH_MEMBER_REPLACEMENT(mi, v);
954 mutex_unlock(&c->sb_lock);
957 if (attr == &sysfs_label) {
961 tmp = kstrdup(buf, GFP_KERNEL);
965 ret = bch2_dev_group_set(c, ca, strim(tmp));
971 if (attr == &sysfs_wake_allocator)
972 bch2_wake_allocator(ca);
978 struct attribute *bch2_dev_files[] = {
988 &sysfs_cache_replacement_policy,
995 &sysfs_io_latency_read,
996 &sysfs_io_latency_write,
997 &sysfs_io_latency_stats_read,
998 &sysfs_io_latency_stats_write,
1001 &sysfs_reserve_stats,
1005 &sysfs_wake_allocator,
1009 #endif /* _BCACHEFS_SYSFS_H_ */