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_update.h"
18 #include "btree_update_interior.h"
21 #include "disk_groups.h"
28 #include "rebalance.h"
33 #include <linux/blkdev.h>
34 #include <linux/sort.h>
35 #include <linux/sched/clock.h>
39 #define SYSFS_OPS(type) \
40 struct sysfs_ops type ## _sysfs_ops = { \
41 .show = type ## _show, \
42 .store = type ## _store \
46 static ssize_t fn ## _show(struct kobject *kobj, struct attribute *attr,\
50 static ssize_t fn ## _store(struct kobject *kobj, struct attribute *attr,\
51 const char *buf, size_t size) \
53 #define __sysfs_attribute(_name, _mode) \
54 static struct attribute sysfs_##_name = \
55 { .name = #_name, .mode = _mode }
57 #define write_attribute(n) __sysfs_attribute(n, S_IWUSR)
58 #define read_attribute(n) __sysfs_attribute(n, S_IRUGO)
59 #define rw_attribute(n) __sysfs_attribute(n, S_IRUGO|S_IWUSR)
61 #define sysfs_printf(file, fmt, ...) \
63 if (attr == &sysfs_ ## file) \
64 return scnprintf(buf, PAGE_SIZE, fmt "\n", __VA_ARGS__);\
67 #define sysfs_print(file, var) \
69 if (attr == &sysfs_ ## file) \
70 return snprint(buf, PAGE_SIZE, var); \
73 #define sysfs_hprint(file, val) \
75 if (attr == &sysfs_ ## file) { \
76 struct printbuf out = _PBUF(buf, PAGE_SIZE); \
77 bch2_hprint(&out, val); \
79 return out.pos - buf; \
83 #define var_printf(_var, fmt) sysfs_printf(_var, fmt, var(_var))
84 #define var_print(_var) sysfs_print(_var, var(_var))
85 #define var_hprint(_var) sysfs_hprint(_var, var(_var))
87 #define sysfs_strtoul(file, var) \
89 if (attr == &sysfs_ ## file) \
90 return strtoul_safe(buf, var) ?: (ssize_t) size; \
93 #define sysfs_strtoul_clamp(file, var, min, max) \
95 if (attr == &sysfs_ ## file) \
96 return strtoul_safe_clamp(buf, var, min, max) \
100 #define strtoul_or_return(cp) \
103 int _r = kstrtoul(cp, 10, &_v); \
109 #define strtoul_restrict_or_return(cp, min, max) \
111 unsigned long __v = 0; \
112 int _r = strtoul_safe_restrict(cp, __v, min, max); \
118 #define strtoi_h_or_return(cp) \
121 int _r = strtoi_h(cp, &_v); \
127 #define sysfs_hatoi(file, var) \
129 if (attr == &sysfs_ ## file) \
130 return strtoi_h(buf, &var) ?: (ssize_t) size; \
133 write_attribute(trigger_journal_flush);
134 write_attribute(trigger_btree_coalesce);
135 write_attribute(trigger_gc);
136 write_attribute(trigger_alloc_write);
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);
169 read_attribute(internal_uuid);
171 read_attribute(has_data);
172 read_attribute(alloc_debug);
173 write_attribute(wake_allocator);
175 read_attribute(read_realloc_races);
176 read_attribute(extent_migrate_done);
177 read_attribute(extent_migrate_raced);
179 rw_attribute(journal_write_delay_ms);
180 rw_attribute(journal_reclaim_delay_ms);
182 rw_attribute(discard);
183 rw_attribute(cache_replacement_policy);
186 rw_attribute(copy_gc_enabled);
187 sysfs_pd_controller_attribute(copy_gc);
189 rw_attribute(rebalance_enabled);
190 sysfs_pd_controller_attribute(rebalance);
191 read_attribute(rebalance_work);
192 rw_attribute(promote_whole_extents);
194 read_attribute(new_stripes);
196 rw_attribute(pd_controllers_update_seconds);
198 read_attribute(meta_replicas_have);
199 read_attribute(data_replicas_have);
201 #ifdef CONFIG_BCACHEFS_TESTS
202 write_attribute(perf_test);
203 #endif /* CONFIG_BCACHEFS_TESTS */
205 #define BCH_DEBUG_PARAM(name, description) \
209 #undef BCH_DEBUG_PARAM
212 static struct attribute sysfs_time_stat_##_name = \
213 { .name = #_name, .mode = S_IRUGO };
217 static struct attribute sysfs_state_rw = {
222 static size_t bch2_btree_cache_size(struct bch_fs *c)
227 mutex_lock(&c->btree_cache.lock);
228 list_for_each_entry(b, &c->btree_cache.live, list)
229 ret += btree_bytes(c);
231 mutex_unlock(&c->btree_cache.lock);
235 static ssize_t show_fs_alloc_debug(struct bch_fs *c, char *buf)
237 struct printbuf out = _PBUF(buf, PAGE_SIZE);
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);
249 return out.pos - buf;
252 static ssize_t bch2_compression_stats(struct bch_fs *c, char *buf)
254 struct btree_trans trans;
255 struct btree_iter *iter;
257 u64 nr_uncompressed_extents = 0, uncompressed_sectors = 0,
258 nr_compressed_extents = 0,
259 compressed_sectors_compressed = 0,
260 compressed_sectors_uncompressed = 0;
263 if (!test_bit(BCH_FS_STARTED, &c->flags))
266 bch2_trans_init(&trans, c, 0, 0);
268 for_each_btree_key(&trans, iter, BTREE_ID_EXTENTS, POS_MIN, 0, k, ret)
269 if (k.k->type == KEY_TYPE_extent) {
270 struct bkey_s_c_extent e = bkey_s_c_to_extent(k);
271 const union bch_extent_entry *entry;
272 struct extent_ptr_decoded p;
274 extent_for_each_ptr_decode(e, p, entry) {
275 if (p.crc.compression_type == BCH_COMPRESSION_NONE) {
276 nr_uncompressed_extents++;
277 uncompressed_sectors += e.k->size;
279 nr_compressed_extents++;
280 compressed_sectors_compressed +=
281 p.crc.compressed_size;
282 compressed_sectors_uncompressed +=
283 p.crc.uncompressed_size;
286 /* only looking at the first ptr */
291 ret = bch2_trans_exit(&trans) ?: ret;
295 return scnprintf(buf, PAGE_SIZE,
296 "uncompressed data:\n"
297 " nr extents: %llu\n"
298 " size (bytes): %llu\n"
300 " nr extents: %llu\n"
301 " compressed size (bytes): %llu\n"
302 " uncompressed size (bytes): %llu\n",
303 nr_uncompressed_extents,
304 uncompressed_sectors << 9,
305 nr_compressed_extents,
306 compressed_sectors_compressed << 9,
307 compressed_sectors_uncompressed << 9);
310 static ssize_t bch2_new_stripes(struct bch_fs *c, char *buf)
312 char *out = buf, *end = buf + PAGE_SIZE;
313 struct ec_stripe_head *h;
314 struct ec_stripe_new *s;
316 mutex_lock(&c->ec_new_stripe_lock);
317 list_for_each_entry(h, &c->ec_new_stripe_list, list) {
318 out += scnprintf(out, end - out,
319 "target %u algo %u redundancy %u:\n",
320 h->target, h->algo, h->redundancy);
323 out += scnprintf(out, end - out,
324 "\tpending: blocks %u allocated %u\n",
326 bitmap_weight(h->s->blocks_allocated,
329 mutex_lock(&h->lock);
330 list_for_each_entry(s, &h->stripes, list)
331 out += scnprintf(out, end - out,
332 "\tin flight: blocks %u allocated %u pin %u\n",
334 bitmap_weight(s->blocks_allocated,
336 atomic_read(&s->pin));
337 mutex_unlock(&h->lock);
340 mutex_unlock(&c->ec_new_stripe_lock);
347 struct bch_fs *c = container_of(kobj, struct bch_fs, kobj);
349 sysfs_print(minor, c->minor);
350 sysfs_printf(internal_uuid, "%pU", c->sb.uuid.b);
352 sysfs_print(journal_write_delay_ms, c->journal.write_delay_ms);
353 sysfs_print(journal_reclaim_delay_ms, c->journal.reclaim_delay_ms);
355 sysfs_print(block_size, block_bytes(c));
356 sysfs_print(btree_node_size, btree_bytes(c));
357 sysfs_hprint(btree_cache_size, bch2_btree_cache_size(c));
359 sysfs_print(read_realloc_races,
360 atomic_long_read(&c->read_realloc_races));
361 sysfs_print(extent_migrate_done,
362 atomic_long_read(&c->extent_migrate_done));
363 sysfs_print(extent_migrate_raced,
364 atomic_long_read(&c->extent_migrate_raced));
366 sysfs_printf(btree_gc_periodic, "%u", (int) c->btree_gc_periodic);
368 sysfs_printf(copy_gc_enabled, "%i", c->copy_gc_enabled);
370 sysfs_print(pd_controllers_update_seconds,
371 c->pd_controllers_update_seconds);
373 sysfs_printf(rebalance_enabled, "%i", c->rebalance.enabled);
374 sysfs_pd_controller_show(rebalance, &c->rebalance.pd); /* XXX */
376 if (attr == &sysfs_rebalance_work)
377 return bch2_rebalance_work_show(c, buf);
379 sysfs_print(promote_whole_extents, c->promote_whole_extents);
381 sysfs_printf(meta_replicas_have, "%i", bch2_replicas_online(c, true));
382 sysfs_printf(data_replicas_have, "%i", bch2_replicas_online(c, false));
386 if (attr == &sysfs_alloc_debug)
387 return show_fs_alloc_debug(c, buf);
389 if (attr == &sysfs_journal_debug)
390 return bch2_journal_print_debug(&c->journal, buf);
392 if (attr == &sysfs_journal_pins)
393 return bch2_journal_print_pins(&c->journal, buf);
395 if (attr == &sysfs_btree_updates)
396 return bch2_btree_updates_print(c, buf);
398 if (attr == &sysfs_dirty_btree_nodes)
399 return bch2_dirty_btree_nodes_print(c, buf);
401 if (attr == &sysfs_compression_stats)
402 return bch2_compression_stats(c, buf);
404 if (attr == &sysfs_new_stripes)
405 return bch2_new_stripes(c, buf);
407 #define BCH_DEBUG_PARAM(name, description) sysfs_print(name, c->name);
409 #undef BCH_DEBUG_PARAM
416 struct bch_fs *c = container_of(kobj, struct bch_fs, kobj);
418 sysfs_strtoul(journal_write_delay_ms, c->journal.write_delay_ms);
419 sysfs_strtoul(journal_reclaim_delay_ms, c->journal.reclaim_delay_ms);
421 if (attr == &sysfs_btree_gc_periodic) {
422 ssize_t ret = strtoul_safe(buf, c->btree_gc_periodic)
425 wake_up_process(c->gc_thread);
429 if (attr == &sysfs_copy_gc_enabled) {
432 ssize_t ret = strtoul_safe(buf, c->copy_gc_enabled)
435 for_each_member_device(ca, c, i)
436 if (ca->copygc_thread)
437 wake_up_process(ca->copygc_thread);
441 if (attr == &sysfs_rebalance_enabled) {
442 ssize_t ret = strtoul_safe(buf, c->rebalance.enabled)
449 sysfs_strtoul(pd_controllers_update_seconds,
450 c->pd_controllers_update_seconds);
451 sysfs_pd_controller_store(rebalance, &c->rebalance.pd);
453 sysfs_strtoul(promote_whole_extents, c->promote_whole_extents);
457 #define BCH_DEBUG_PARAM(name, description) sysfs_strtoul(name, c->name);
459 #undef BCH_DEBUG_PARAM
461 if (!test_bit(BCH_FS_STARTED, &c->flags))
466 if (attr == &sysfs_trigger_journal_flush)
467 bch2_journal_meta_async(&c->journal, NULL);
469 if (attr == &sysfs_trigger_btree_coalesce)
472 if (attr == &sysfs_trigger_gc)
473 bch2_gc(c, NULL, false, false);
475 if (attr == &sysfs_trigger_alloc_write) {
478 bch2_alloc_write(c, 0, &wrote);
481 if (attr == &sysfs_prune_cache) {
482 struct shrink_control sc;
484 sc.gfp_mask = GFP_KERNEL;
485 sc.nr_to_scan = strtoul_or_return(buf);
486 c->btree_cache.shrink.scan_objects(&c->btree_cache.shrink, &sc);
488 #ifdef CONFIG_BCACHEFS_TESTS
489 if (attr == &sysfs_perf_test) {
490 char *tmp = kstrdup(buf, GFP_KERNEL), *p = tmp;
491 char *test = strsep(&p, " \t\n");
492 char *nr_str = strsep(&p, " \t\n");
493 char *threads_str = strsep(&p, " \t\n");
499 !(ret = kstrtouint(threads_str, 10, &threads)) &&
500 !(ret = bch2_strtoull_h(nr_str, &nr)))
501 bch2_btree_perf_test(c, test, nr, threads);
512 struct bch_fs *c = container_of(kobj, struct bch_fs, kobj);
514 mutex_lock(&c->state_lock);
515 size = __bch2_fs_store(kobj, attr, buf, size);
516 mutex_unlock(&c->state_lock);
522 struct attribute *bch2_fs_files[] = {
525 &sysfs_btree_node_size,
526 &sysfs_btree_cache_size,
528 &sysfs_meta_replicas_have,
529 &sysfs_data_replicas_have,
531 &sysfs_journal_write_delay_ms,
532 &sysfs_journal_reclaim_delay_ms,
534 &sysfs_promote_whole_extents,
536 &sysfs_compression_stats,
538 #ifdef CONFIG_BCACHEFS_TESTS
544 /* internal dir - just a wrapper */
546 SHOW(bch2_fs_internal)
548 struct bch_fs *c = container_of(kobj, struct bch_fs, internal);
549 return bch2_fs_show(&c->kobj, attr, buf);
552 STORE(bch2_fs_internal)
554 struct bch_fs *c = container_of(kobj, struct bch_fs, internal);
555 return bch2_fs_store(&c->kobj, attr, buf, size);
557 SYSFS_OPS(bch2_fs_internal);
559 struct attribute *bch2_fs_internal_files[] = {
561 &sysfs_journal_debug,
563 &sysfs_btree_updates,
564 &sysfs_dirty_btree_nodes,
566 &sysfs_read_realloc_races,
567 &sysfs_extent_migrate_done,
568 &sysfs_extent_migrate_raced,
570 &sysfs_trigger_journal_flush,
571 &sysfs_trigger_btree_coalesce,
573 &sysfs_trigger_alloc_write,
576 &sysfs_copy_gc_enabled,
578 &sysfs_rebalance_enabled,
579 &sysfs_rebalance_work,
580 sysfs_pd_controller_files(rebalance),
584 &sysfs_internal_uuid,
586 #define BCH_DEBUG_PARAM(name, description) &sysfs_##name,
588 #undef BCH_DEBUG_PARAM
595 SHOW(bch2_fs_opts_dir)
597 struct printbuf out = _PBUF(buf, PAGE_SIZE);
598 struct bch_fs *c = container_of(kobj, struct bch_fs, opts_dir);
599 const struct bch_option *opt = container_of(attr, struct bch_option, attr);
600 int id = opt - bch2_opt_table;
601 u64 v = bch2_opt_get_by_id(&c->opts, id);
603 bch2_opt_to_text(&out, c, opt, v, OPT_SHOW_FULL_LIST);
606 return out.pos - buf;
609 STORE(bch2_fs_opts_dir)
611 struct bch_fs *c = container_of(kobj, struct bch_fs, opts_dir);
612 const struct bch_option *opt = container_of(attr, struct bch_option, attr);
613 int ret, id = opt - bch2_opt_table;
617 tmp = kstrdup(buf, GFP_KERNEL);
621 ret = bch2_opt_parse(c, opt, strim(tmp), &v);
627 ret = bch2_opt_check_may_set(c, id, v);
631 if (opt->set_sb != SET_NO_SB_OPT) {
632 mutex_lock(&c->sb_lock);
633 opt->set_sb(c->disk_sb.sb, v);
635 mutex_unlock(&c->sb_lock);
638 bch2_opt_set_by_id(&c->opts, id, v);
640 if ((id == Opt_background_target ||
641 id == Opt_background_compression) && v) {
642 bch2_rebalance_add_work(c, S64_MAX);
648 SYSFS_OPS(bch2_fs_opts_dir);
650 struct attribute *bch2_fs_opts_dir_files[] = { NULL };
652 int bch2_opts_create_sysfs_files(struct kobject *kobj)
654 const struct bch_option *i;
657 for (i = bch2_opt_table;
658 i < bch2_opt_table + bch2_opts_nr;
660 if (!(i->mode & (OPT_FORMAT|OPT_MOUNT|OPT_RUNTIME)))
663 ret = sysfs_create_file(kobj, &i->attr);
673 SHOW(bch2_fs_time_stats)
675 struct bch_fs *c = container_of(kobj, struct bch_fs, time_stats);
678 if (attr == &sysfs_time_stat_##name) \
679 return bch2_time_stats_print(&c->times[BCH_TIME_##name],\
687 STORE(bch2_fs_time_stats)
691 SYSFS_OPS(bch2_fs_time_stats);
693 struct attribute *bch2_fs_time_stats_files[] = {
695 &sysfs_time_stat_##name,
701 typedef unsigned (bucket_map_fn)(struct bch_fs *, struct bch_dev *,
704 static unsigned bucket_last_io_fn(struct bch_fs *c, struct bch_dev *ca,
705 size_t b, void *private)
707 int rw = (private ? 1 : 0);
709 return bucket_last_io(c, bucket(ca, b), rw);
712 static unsigned bucket_sectors_used_fn(struct bch_fs *c, struct bch_dev *ca,
713 size_t b, void *private)
715 struct bucket *g = bucket(ca, b);
716 return bucket_sectors_used(g->mark);
719 static unsigned bucket_oldest_gen_fn(struct bch_fs *c, struct bch_dev *ca,
720 size_t b, void *private)
722 return bucket_gc_gen(ca, b);
725 static int unsigned_cmp(const void *_l, const void *_r)
727 const unsigned *l = _l;
728 const unsigned *r = _r;
730 return cmp_int(*l, *r);
733 static ssize_t show_quantiles(struct bch_fs *c, struct bch_dev *ca,
734 char *buf, bucket_map_fn *fn, void *private)
737 /* Compute 31 quantiles */
741 down_read(&ca->bucket_lock);
744 p = vzalloc(n * sizeof(unsigned));
746 up_read(&ca->bucket_lock);
750 for (i = ca->mi.first_bucket; i < n; i++)
751 p[i] = fn(c, ca, i, private);
753 sort(p, n, sizeof(unsigned), unsigned_cmp, NULL);
754 up_read(&ca->bucket_lock);
760 for (i = 0; i < ARRAY_SIZE(q); i++)
761 q[i] = p[n * (i + 1) / (ARRAY_SIZE(q) + 1)];
765 for (i = 0; i < ARRAY_SIZE(q); i++)
766 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
773 static ssize_t show_reserve_stats(struct bch_dev *ca, char *buf)
775 struct printbuf out = _PBUF(buf, PAGE_SIZE);
776 enum alloc_reserve i;
778 spin_lock(&ca->fs->freelist_lock);
780 pr_buf(&out, "free_inc:\t%zu\t%zu\n",
781 fifo_used(&ca->free_inc),
784 for (i = 0; i < RESERVE_NR; i++)
785 pr_buf(&out, "free[%u]:\t%zu\t%zu\n", i,
786 fifo_used(&ca->free[i]),
789 spin_unlock(&ca->fs->freelist_lock);
791 return out.pos - buf;
794 static ssize_t show_dev_alloc_debug(struct bch_dev *ca, char *buf)
796 struct bch_fs *c = ca->fs;
797 struct bch_dev_usage stats = bch2_dev_usage_read(c, ca);
798 unsigned i, nr[BCH_DATA_NR];
800 memset(nr, 0, sizeof(nr));
802 for (i = 0; i < ARRAY_SIZE(c->open_buckets); i++)
803 nr[c->open_buckets[i].type]++;
805 return scnprintf(buf, PAGE_SIZE,
806 "free_inc: %zu/%zu\n"
807 "free[RESERVE_BTREE]: %zu/%zu\n"
808 "free[RESERVE_MOVINGGC]: %zu/%zu\n"
809 "free[RESERVE_NONE]: %zu/%zu\n"
818 " erasure coded: %llu\n"
826 " fragmented: %llu\n"
827 " copygc threshold: %llu\n"
828 "freelist_wait: %s\n"
829 "open buckets: %u/%u (reserved %u)\n"
830 "open_buckets_wait: %s\n"
831 "open_buckets_btree: %u\n"
832 "open_buckets_user: %u\n"
833 "btree reserve cache: %u\n",
834 fifo_used(&ca->free_inc), ca->free_inc.size,
835 fifo_used(&ca->free[RESERVE_BTREE]), ca->free[RESERVE_BTREE].size,
836 fifo_used(&ca->free[RESERVE_MOVINGGC]), ca->free[RESERVE_MOVINGGC].size,
837 fifo_used(&ca->free[RESERVE_NONE]), ca->free[RESERVE_NONE].size,
838 ca->mi.nbuckets - ca->mi.first_bucket,
840 stats.buckets[BCH_DATA_SB],
841 stats.buckets[BCH_DATA_JOURNAL],
842 stats.buckets[BCH_DATA_BTREE],
843 stats.buckets[BCH_DATA_USER],
844 stats.buckets[BCH_DATA_CACHED],
846 ca->mi.nbuckets - ca->mi.first_bucket - stats.buckets_unavailable,
847 stats.sectors[BCH_DATA_SB],
848 stats.sectors[BCH_DATA_JOURNAL],
849 stats.sectors[BCH_DATA_BTREE],
850 stats.sectors[BCH_DATA_USER],
851 stats.sectors[BCH_DATA_CACHED],
852 stats.sectors_fragmented,
853 ca->copygc_threshold,
854 c->freelist_wait.list.first ? "waiting" : "empty",
855 c->open_buckets_nr_free, OPEN_BUCKETS_COUNT,
856 BTREE_NODE_OPEN_BUCKET_RESERVE,
857 c->open_buckets_wait.list.first ? "waiting" : "empty",
860 c->btree_reserve_cache_nr);
863 static const char * const bch2_rw[] = {
869 static ssize_t show_dev_iodone(struct bch_dev *ca, char *buf)
871 struct printbuf out = _PBUF(buf, PAGE_SIZE);
874 for (rw = 0; rw < 2; rw++) {
875 pr_buf(&out, "%s:\n", bch2_rw[rw]);
877 for (i = 1; i < BCH_DATA_NR; i++)
878 pr_buf(&out, "%-12s:%12llu\n",
880 percpu_u64_get(&ca->io_done->sectors[rw][i]) << 9);
883 return out.pos - buf;
888 struct bch_dev *ca = container_of(kobj, struct bch_dev, kobj);
889 struct bch_fs *c = ca->fs;
890 struct printbuf out = _PBUF(buf, PAGE_SIZE);
892 sysfs_printf(uuid, "%pU\n", ca->uuid.b);
894 sysfs_print(bucket_size, bucket_bytes(ca));
895 sysfs_print(block_size, block_bytes(c));
896 sysfs_print(first_bucket, ca->mi.first_bucket);
897 sysfs_print(nbuckets, ca->mi.nbuckets);
898 sysfs_print(durability, ca->mi.durability);
899 sysfs_print(discard, ca->mi.discard);
901 if (attr == &sysfs_label) {
903 mutex_lock(&c->sb_lock);
904 bch2_disk_path_to_text(&out, &c->disk_sb,
906 mutex_unlock(&c->sb_lock);
908 pr_buf(&out, "none");
912 return out.pos - buf;
915 if (attr == &sysfs_has_data) {
916 bch2_flags_to_text(&out, bch2_data_types,
917 bch2_dev_has_data(c, ca));
919 return out.pos - buf;
922 sysfs_pd_controller_show(copy_gc, &ca->copygc_pd);
924 if (attr == &sysfs_cache_replacement_policy) {
925 bch2_string_opt_to_text(&out,
926 bch2_cache_replacement_policies,
929 return out.pos - buf;
932 if (attr == &sysfs_state_rw) {
933 bch2_string_opt_to_text(&out, bch2_dev_state,
936 return out.pos - buf;
939 if (attr == &sysfs_iodone)
940 return show_dev_iodone(ca, buf);
942 sysfs_print(io_latency_read, atomic64_read(&ca->cur_latency[READ]));
943 sysfs_print(io_latency_write, atomic64_read(&ca->cur_latency[WRITE]));
945 if (attr == &sysfs_io_latency_stats_read)
946 return bch2_time_stats_print(&ca->io_latency[READ], buf, PAGE_SIZE);
947 if (attr == &sysfs_io_latency_stats_write)
948 return bch2_time_stats_print(&ca->io_latency[WRITE], buf, PAGE_SIZE);
950 sysfs_printf(congested, "%u%%",
951 clamp(atomic_read(&ca->congested), 0, CONGESTED_MAX)
952 * 100 / CONGESTED_MAX);
954 if (attr == &sysfs_bucket_quantiles_last_read)
955 return show_quantiles(c, ca, buf, bucket_last_io_fn, (void *) 0);
956 if (attr == &sysfs_bucket_quantiles_last_write)
957 return show_quantiles(c, ca, buf, bucket_last_io_fn, (void *) 1);
958 if (attr == &sysfs_bucket_quantiles_fragmentation)
959 return show_quantiles(c, ca, buf, bucket_sectors_used_fn, NULL);
960 if (attr == &sysfs_bucket_quantiles_oldest_gen)
961 return show_quantiles(c, ca, buf, bucket_oldest_gen_fn, NULL);
963 if (attr == &sysfs_reserve_stats)
964 return show_reserve_stats(ca, buf);
965 if (attr == &sysfs_alloc_debug)
966 return show_dev_alloc_debug(ca, buf);
973 struct bch_dev *ca = container_of(kobj, struct bch_dev, kobj);
974 struct bch_fs *c = ca->fs;
975 struct bch_member *mi;
977 sysfs_pd_controller_store(copy_gc, &ca->copygc_pd);
979 if (attr == &sysfs_discard) {
980 bool v = strtoul_or_return(buf);
982 mutex_lock(&c->sb_lock);
983 mi = &bch2_sb_get_members(c->disk_sb.sb)->members[ca->dev_idx];
985 if (v != BCH_MEMBER_DISCARD(mi)) {
986 SET_BCH_MEMBER_DISCARD(mi, v);
989 mutex_unlock(&c->sb_lock);
992 if (attr == &sysfs_cache_replacement_policy) {
993 ssize_t v = __sysfs_match_string(bch2_cache_replacement_policies, -1, buf);
998 mutex_lock(&c->sb_lock);
999 mi = &bch2_sb_get_members(c->disk_sb.sb)->members[ca->dev_idx];
1001 if ((unsigned) v != BCH_MEMBER_REPLACEMENT(mi)) {
1002 SET_BCH_MEMBER_REPLACEMENT(mi, v);
1003 bch2_write_super(c);
1005 mutex_unlock(&c->sb_lock);
1008 if (attr == &sysfs_label) {
1012 tmp = kstrdup(buf, GFP_KERNEL);
1016 ret = bch2_dev_group_set(c, ca, strim(tmp));
1022 if (attr == &sysfs_wake_allocator)
1023 bch2_wake_allocator(ca);
1027 SYSFS_OPS(bch2_dev);
1029 struct attribute *bch2_dev_files[] = {
1033 &sysfs_first_bucket,
1039 &sysfs_cache_replacement_policy,
1046 &sysfs_io_latency_read,
1047 &sysfs_io_latency_write,
1048 &sysfs_io_latency_stats_read,
1049 &sysfs_io_latency_stats_write,
1052 /* alloc info - other stats: */
1053 &sysfs_bucket_quantiles_last_read,
1054 &sysfs_bucket_quantiles_last_write,
1055 &sysfs_bucket_quantiles_fragmentation,
1056 &sysfs_bucket_quantiles_oldest_gen,
1058 &sysfs_reserve_stats,
1062 &sysfs_wake_allocator,
1064 sysfs_pd_controller_files(copy_gc),
1068 #endif /* _BCACHEFS_SYSFS_H_ */