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 ssize_t ret = bch2_hprint(buf, val); \
82 #define var_printf(_var, fmt) sysfs_printf(_var, fmt, var(_var))
83 #define var_print(_var) sysfs_print(_var, var(_var))
84 #define var_hprint(_var) sysfs_hprint(_var, var(_var))
86 #define sysfs_strtoul(file, var) \
88 if (attr == &sysfs_ ## file) \
89 return strtoul_safe(buf, var) ?: (ssize_t) size; \
92 #define sysfs_strtoul_clamp(file, var, min, max) \
94 if (attr == &sysfs_ ## file) \
95 return strtoul_safe_clamp(buf, var, min, max) \
99 #define strtoul_or_return(cp) \
102 int _r = kstrtoul(cp, 10, &_v); \
108 #define strtoul_restrict_or_return(cp, min, max) \
110 unsigned long __v = 0; \
111 int _r = strtoul_safe_restrict(cp, __v, min, max); \
117 #define strtoi_h_or_return(cp) \
120 int _r = strtoi_h(cp, &_v); \
126 #define sysfs_hatoi(file, var) \
128 if (attr == &sysfs_ ## file) \
129 return strtoi_h(buf, &var) ?: (ssize_t) size; \
132 write_attribute(trigger_journal_flush);
133 write_attribute(trigger_btree_coalesce);
134 write_attribute(trigger_gc);
135 write_attribute(prune_cache);
136 rw_attribute(btree_gc_periodic);
138 read_attribute(uuid);
139 read_attribute(minor);
140 read_attribute(bucket_size);
141 read_attribute(block_size);
142 read_attribute(btree_node_size);
143 read_attribute(first_bucket);
144 read_attribute(nbuckets);
145 read_attribute(durability);
146 read_attribute(iodone);
148 read_attribute(io_latency_read);
149 read_attribute(io_latency_write);
150 read_attribute(io_latency_stats_read);
151 read_attribute(io_latency_stats_write);
152 read_attribute(congested);
154 read_attribute(bucket_quantiles_last_read);
155 read_attribute(bucket_quantiles_last_write);
156 read_attribute(bucket_quantiles_fragmentation);
157 read_attribute(bucket_quantiles_oldest_gen);
159 read_attribute(reserve_stats);
160 read_attribute(btree_cache_size);
161 read_attribute(compression_stats);
162 read_attribute(journal_debug);
163 read_attribute(journal_pins);
164 read_attribute(btree_updates);
165 read_attribute(dirty_btree_nodes);
167 read_attribute(internal_uuid);
169 read_attribute(has_data);
170 read_attribute(alloc_debug);
171 write_attribute(wake_allocator);
173 read_attribute(read_realloc_races);
174 read_attribute(extent_migrate_done);
175 read_attribute(extent_migrate_raced);
177 rw_attribute(journal_write_delay_ms);
178 rw_attribute(journal_reclaim_delay_ms);
180 rw_attribute(discard);
181 rw_attribute(cache_replacement_policy);
184 rw_attribute(copy_gc_enabled);
185 sysfs_pd_controller_attribute(copy_gc);
187 rw_attribute(rebalance_enabled);
188 sysfs_pd_controller_attribute(rebalance);
189 read_attribute(rebalance_work);
190 rw_attribute(promote_whole_extents);
192 read_attribute(new_stripes);
194 rw_attribute(pd_controllers_update_seconds);
196 read_attribute(meta_replicas_have);
197 read_attribute(data_replicas_have);
199 #ifdef CONFIG_BCACHEFS_TESTS
200 write_attribute(perf_test);
201 #endif /* CONFIG_BCACHEFS_TESTS */
203 #define BCH_DEBUG_PARAM(name, description) \
207 #undef BCH_DEBUG_PARAM
210 static struct attribute sysfs_time_stat_##_name = \
211 { .name = #_name, .mode = S_IRUGO };
215 static struct attribute sysfs_state_rw = {
220 static size_t bch2_btree_cache_size(struct bch_fs *c)
225 mutex_lock(&c->btree_cache.lock);
226 list_for_each_entry(b, &c->btree_cache.live, list)
227 ret += btree_bytes(c);
229 mutex_unlock(&c->btree_cache.lock);
233 static ssize_t show_fs_alloc_debug(struct bch_fs *c, char *buf)
235 struct printbuf out = _PBUF(buf, PAGE_SIZE);
236 struct bch_fs_usage stats = bch2_fs_usage_read(c);
237 unsigned replicas, type;
239 pr_buf(&out, "capacity:\t\t%llu\n", c->capacity);
241 for (replicas = 0; replicas < ARRAY_SIZE(stats.replicas); replicas++) {
242 pr_buf(&out, "%u replicas:\n", replicas + 1);
244 for (type = BCH_DATA_SB; type < BCH_DATA_NR; type++)
245 pr_buf(&out, "\t%s:\t\t%llu\n",
246 bch2_data_types[type],
247 stats.replicas[replicas].data[type]);
248 pr_buf(&out, "\terasure coded:\t%llu\n",
249 stats.replicas[replicas].ec_data);
250 pr_buf(&out, "\treserved:\t%llu\n",
251 stats.replicas[replicas].persistent_reserved);
254 pr_buf(&out, "bucket usage\n");
256 for (type = BCH_DATA_SB; type < BCH_DATA_NR; type++)
257 pr_buf(&out, "\t%s:\t\t%llu\n",
258 bch2_data_types[type],
259 stats.buckets[type]);
261 pr_buf(&out, "online reserved:\t%llu\n",
262 stats.online_reserved);
264 return out.pos - buf;
267 static ssize_t bch2_compression_stats(struct bch_fs *c, char *buf)
269 struct btree_iter iter;
271 u64 nr_uncompressed_extents = 0, uncompressed_sectors = 0,
272 nr_compressed_extents = 0,
273 compressed_sectors_compressed = 0,
274 compressed_sectors_uncompressed = 0;
276 if (!bch2_fs_running(c))
279 for_each_btree_key(&iter, c, BTREE_ID_EXTENTS, POS_MIN, 0, k)
280 if (k.k->type == KEY_TYPE_extent) {
281 struct bkey_s_c_extent e = bkey_s_c_to_extent(k);
282 const union bch_extent_entry *entry;
283 struct extent_ptr_decoded p;
285 extent_for_each_ptr_decode(e, p, entry) {
286 if (p.crc.compression_type == BCH_COMPRESSION_NONE) {
287 nr_uncompressed_extents++;
288 uncompressed_sectors += e.k->size;
290 nr_compressed_extents++;
291 compressed_sectors_compressed +=
292 p.crc.compressed_size;
293 compressed_sectors_uncompressed +=
294 p.crc.uncompressed_size;
297 /* only looking at the first ptr */
301 bch2_btree_iter_unlock(&iter);
303 return scnprintf(buf, PAGE_SIZE,
304 "uncompressed data:\n"
305 " nr extents: %llu\n"
306 " size (bytes): %llu\n"
308 " nr extents: %llu\n"
309 " compressed size (bytes): %llu\n"
310 " uncompressed size (bytes): %llu\n",
311 nr_uncompressed_extents,
312 uncompressed_sectors << 9,
313 nr_compressed_extents,
314 compressed_sectors_compressed << 9,
315 compressed_sectors_uncompressed << 9);
318 static ssize_t bch2_new_stripes(struct bch_fs *c, char *buf)
320 char *out = buf, *end = buf + PAGE_SIZE;
321 struct ec_stripe_head *h;
322 struct ec_stripe_new *s;
324 mutex_lock(&c->ec_new_stripe_lock);
325 list_for_each_entry(h, &c->ec_new_stripe_list, list) {
326 out += scnprintf(out, end - out,
327 "target %u algo %u redundancy %u:\n",
328 h->target, h->algo, h->redundancy);
331 out += scnprintf(out, end - out,
332 "\tpending: blocks %u allocated %u\n",
334 bitmap_weight(h->s->blocks_allocated,
337 mutex_lock(&h->lock);
338 list_for_each_entry(s, &h->stripes, list)
339 out += scnprintf(out, end - out,
340 "\tin flight: blocks %u allocated %u pin %u\n",
342 bitmap_weight(s->blocks_allocated,
344 atomic_read(&s->pin));
345 mutex_unlock(&h->lock);
348 mutex_unlock(&c->ec_new_stripe_lock);
355 struct bch_fs *c = container_of(kobj, struct bch_fs, kobj);
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));
367 sysfs_print(read_realloc_races,
368 atomic_long_read(&c->read_realloc_races));
369 sysfs_print(extent_migrate_done,
370 atomic_long_read(&c->extent_migrate_done));
371 sysfs_print(extent_migrate_raced,
372 atomic_long_read(&c->extent_migrate_raced));
374 sysfs_printf(btree_gc_periodic, "%u", (int) c->btree_gc_periodic);
376 sysfs_printf(copy_gc_enabled, "%i", c->copy_gc_enabled);
378 sysfs_print(pd_controllers_update_seconds,
379 c->pd_controllers_update_seconds);
381 sysfs_printf(rebalance_enabled, "%i", c->rebalance.enabled);
382 sysfs_pd_controller_show(rebalance, &c->rebalance.pd); /* XXX */
384 if (attr == &sysfs_rebalance_work)
385 return bch2_rebalance_work_show(c, buf);
387 sysfs_print(promote_whole_extents, c->promote_whole_extents);
389 sysfs_printf(meta_replicas_have, "%i", bch2_replicas_online(c, true));
390 sysfs_printf(data_replicas_have, "%i", bch2_replicas_online(c, false));
394 if (attr == &sysfs_alloc_debug)
395 return show_fs_alloc_debug(c, buf);
397 if (attr == &sysfs_journal_debug)
398 return bch2_journal_print_debug(&c->journal, buf);
400 if (attr == &sysfs_journal_pins)
401 return bch2_journal_print_pins(&c->journal, buf);
403 if (attr == &sysfs_btree_updates)
404 return bch2_btree_updates_print(c, buf);
406 if (attr == &sysfs_dirty_btree_nodes)
407 return bch2_dirty_btree_nodes_print(c, buf);
409 if (attr == &sysfs_compression_stats)
410 return bch2_compression_stats(c, buf);
412 if (attr == &sysfs_new_stripes)
413 return bch2_new_stripes(c, buf);
415 #define BCH_DEBUG_PARAM(name, description) sysfs_print(name, c->name);
417 #undef BCH_DEBUG_PARAM
424 struct bch_fs *c = container_of(kobj, struct bch_fs, kobj);
426 sysfs_strtoul(journal_write_delay_ms, c->journal.write_delay_ms);
427 sysfs_strtoul(journal_reclaim_delay_ms, c->journal.reclaim_delay_ms);
429 if (attr == &sysfs_btree_gc_periodic) {
430 ssize_t ret = strtoul_safe(buf, c->btree_gc_periodic)
433 wake_up_process(c->gc_thread);
437 if (attr == &sysfs_copy_gc_enabled) {
440 ssize_t ret = strtoul_safe(buf, c->copy_gc_enabled)
443 for_each_member_device(ca, c, i)
444 if (ca->copygc_thread)
445 wake_up_process(ca->copygc_thread);
449 if (attr == &sysfs_rebalance_enabled) {
450 ssize_t ret = strtoul_safe(buf, c->rebalance.enabled)
457 sysfs_strtoul(pd_controllers_update_seconds,
458 c->pd_controllers_update_seconds);
459 sysfs_pd_controller_store(rebalance, &c->rebalance.pd);
461 sysfs_strtoul(promote_whole_extents, c->promote_whole_extents);
465 #define BCH_DEBUG_PARAM(name, description) sysfs_strtoul(name, c->name);
467 #undef BCH_DEBUG_PARAM
469 if (!bch2_fs_running(c))
474 if (attr == &sysfs_trigger_journal_flush)
475 bch2_journal_meta_async(&c->journal, NULL);
477 if (attr == &sysfs_trigger_btree_coalesce)
480 if (attr == &sysfs_trigger_gc)
481 bch2_gc(c, NULL, false);
483 if (attr == &sysfs_prune_cache) {
484 struct shrink_control sc;
486 sc.gfp_mask = GFP_KERNEL;
487 sc.nr_to_scan = strtoul_or_return(buf);
488 c->btree_cache.shrink.scan_objects(&c->btree_cache.shrink, &sc);
490 #ifdef CONFIG_BCACHEFS_TESTS
491 if (attr == &sysfs_perf_test) {
492 char *tmp = kstrdup(buf, GFP_KERNEL), *p = tmp;
493 char *test = strsep(&p, " \t\n");
494 char *nr_str = strsep(&p, " \t\n");
495 char *threads_str = strsep(&p, " \t\n");
501 !(ret = kstrtouint(threads_str, 10, &threads)) &&
502 !(ret = bch2_strtoull_h(nr_str, &nr)))
503 bch2_btree_perf_test(c, test, nr, threads);
514 struct bch_fs *c = container_of(kobj, struct bch_fs, kobj);
516 mutex_lock(&c->state_lock);
517 size = __bch2_fs_store(kobj, attr, buf, size);
518 mutex_unlock(&c->state_lock);
524 struct attribute *bch2_fs_files[] = {
527 &sysfs_btree_node_size,
528 &sysfs_btree_cache_size,
530 &sysfs_meta_replicas_have,
531 &sysfs_data_replicas_have,
533 &sysfs_journal_write_delay_ms,
534 &sysfs_journal_reclaim_delay_ms,
536 &sysfs_promote_whole_extents,
538 &sysfs_compression_stats,
540 #ifdef CONFIG_BCACHEFS_TESTS
546 /* internal dir - just a wrapper */
548 SHOW(bch2_fs_internal)
550 struct bch_fs *c = container_of(kobj, struct bch_fs, internal);
551 return bch2_fs_show(&c->kobj, attr, buf);
554 STORE(bch2_fs_internal)
556 struct bch_fs *c = container_of(kobj, struct bch_fs, internal);
557 return bch2_fs_store(&c->kobj, attr, buf, size);
559 SYSFS_OPS(bch2_fs_internal);
561 struct attribute *bch2_fs_internal_files[] = {
563 &sysfs_journal_debug,
565 &sysfs_btree_updates,
566 &sysfs_dirty_btree_nodes,
568 &sysfs_read_realloc_races,
569 &sysfs_extent_migrate_done,
570 &sysfs_extent_migrate_raced,
572 &sysfs_trigger_journal_flush,
573 &sysfs_trigger_btree_coalesce,
577 &sysfs_copy_gc_enabled,
579 &sysfs_rebalance_enabled,
580 &sysfs_rebalance_work,
581 sysfs_pd_controller_files(rebalance),
585 &sysfs_internal_uuid,
587 #define BCH_DEBUG_PARAM(name, description) &sysfs_##name,
589 #undef BCH_DEBUG_PARAM
596 SHOW(bch2_fs_opts_dir)
598 struct printbuf out = _PBUF(buf, PAGE_SIZE);
599 struct bch_fs *c = container_of(kobj, struct bch_fs, opts_dir);
600 const struct bch_option *opt = container_of(attr, struct bch_option, attr);
601 int id = opt - bch2_opt_table;
602 u64 v = bch2_opt_get_by_id(&c->opts, id);
604 bch2_opt_to_text(&out, c, opt, v, OPT_SHOW_FULL_LIST);
607 return out.pos - buf;
610 STORE(bch2_fs_opts_dir)
612 struct bch_fs *c = container_of(kobj, struct bch_fs, opts_dir);
613 const struct bch_option *opt = container_of(attr, struct bch_option, attr);
614 int ret, id = opt - bch2_opt_table;
618 tmp = kstrdup(buf, GFP_KERNEL);
622 ret = bch2_opt_parse(c, opt, strim(tmp), &v);
628 ret = bch2_opt_check_may_set(c, id, v);
632 if (opt->set_sb != SET_NO_SB_OPT) {
633 mutex_lock(&c->sb_lock);
634 opt->set_sb(c->disk_sb.sb, v);
636 mutex_unlock(&c->sb_lock);
639 bch2_opt_set_by_id(&c->opts, id, v);
641 if ((id == Opt_background_target ||
642 id == Opt_background_compression) && v) {
643 bch2_rebalance_add_work(c, S64_MAX);
649 SYSFS_OPS(bch2_fs_opts_dir);
651 struct attribute *bch2_fs_opts_dir_files[] = { NULL };
653 int bch2_opts_create_sysfs_files(struct kobject *kobj)
655 const struct bch_option *i;
658 for (i = bch2_opt_table;
659 i < bch2_opt_table + bch2_opts_nr;
661 if (i->mode == OPT_INTERNAL)
664 ret = sysfs_create_file(kobj, &i->attr);
674 SHOW(bch2_fs_time_stats)
676 struct bch_fs *c = container_of(kobj, struct bch_fs, time_stats);
679 if (attr == &sysfs_time_stat_##name) \
680 return bch2_time_stats_print(&c->times[BCH_TIME_##name],\
688 STORE(bch2_fs_time_stats)
692 SYSFS_OPS(bch2_fs_time_stats);
694 struct attribute *bch2_fs_time_stats_files[] = {
696 &sysfs_time_stat_##name,
702 typedef unsigned (bucket_map_fn)(struct bch_fs *, struct bch_dev *,
705 static unsigned bucket_last_io_fn(struct bch_fs *c, struct bch_dev *ca,
706 size_t b, void *private)
708 int rw = (private ? 1 : 0);
710 return bucket_last_io(c, bucket(ca, b), rw);
713 static unsigned bucket_sectors_used_fn(struct bch_fs *c, struct bch_dev *ca,
714 size_t b, void *private)
716 struct bucket *g = bucket(ca, b);
717 return bucket_sectors_used(g->mark);
720 static unsigned bucket_oldest_gen_fn(struct bch_fs *c, struct bch_dev *ca,
721 size_t b, void *private)
723 return bucket_gc_gen(ca, b);
726 static int unsigned_cmp(const void *_l, const void *_r)
728 unsigned l = *((unsigned *) _l);
729 unsigned r = *((unsigned *) _r);
731 return (l > r) - (l < r);
734 static ssize_t show_quantiles(struct bch_fs *c, struct bch_dev *ca,
735 char *buf, bucket_map_fn *fn, void *private)
738 /* Compute 31 quantiles */
742 down_read(&ca->bucket_lock);
745 p = vzalloc(n * sizeof(unsigned));
747 up_read(&ca->bucket_lock);
751 for (i = ca->mi.first_bucket; i < n; i++)
752 p[i] = fn(c, ca, i, private);
754 sort(p, n, sizeof(unsigned), unsigned_cmp, NULL);
755 up_read(&ca->bucket_lock);
761 for (i = 0; i < ARRAY_SIZE(q); i++)
762 q[i] = p[n * (i + 1) / (ARRAY_SIZE(q) + 1)];
766 for (i = 0; i < ARRAY_SIZE(q); i++)
767 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
774 static ssize_t show_reserve_stats(struct bch_dev *ca, char *buf)
776 struct printbuf out = _PBUF(buf, PAGE_SIZE);
777 enum alloc_reserve i;
779 spin_lock(&ca->freelist_lock);
781 pr_buf(&out, "free_inc:\t%zu\t%zu\n",
782 fifo_used(&ca->free_inc),
785 for (i = 0; i < RESERVE_NR; i++)
786 pr_buf(&out, "free[%u]:\t%zu\t%zu\n", i,
787 fifo_used(&ca->free[i]),
790 spin_unlock(&ca->freelist_lock);
792 return out.pos - buf;
795 static ssize_t show_dev_alloc_debug(struct bch_dev *ca, char *buf)
797 struct bch_fs *c = ca->fs;
798 struct bch_dev_usage stats = bch2_dev_usage_read(c, ca);
800 return scnprintf(buf, PAGE_SIZE,
801 "free_inc: %zu/%zu\n"
802 "free[RESERVE_BTREE]: %zu/%zu\n"
803 "free[RESERVE_MOVINGGC]: %zu/%zu\n"
804 "free[RESERVE_NONE]: %zu/%zu\n"
813 " erasure coded: %llu\n"
821 " fragmented: %llu\n"
822 " copygc threshold: %llu\n"
823 "freelist_wait: %s\n"
824 "open buckets: %u/%u (reserved %u)\n"
825 "open_buckets_wait: %s\n",
826 fifo_used(&ca->free_inc), ca->free_inc.size,
827 fifo_used(&ca->free[RESERVE_BTREE]), ca->free[RESERVE_BTREE].size,
828 fifo_used(&ca->free[RESERVE_MOVINGGC]), ca->free[RESERVE_MOVINGGC].size,
829 fifo_used(&ca->free[RESERVE_NONE]), ca->free[RESERVE_NONE].size,
830 ca->mi.nbuckets - ca->mi.first_bucket,
832 stats.buckets[BCH_DATA_SB],
833 stats.buckets[BCH_DATA_JOURNAL],
834 stats.buckets[BCH_DATA_BTREE],
835 stats.buckets[BCH_DATA_USER],
836 stats.buckets[BCH_DATA_CACHED],
838 ca->mi.nbuckets - ca->mi.first_bucket - stats.buckets_unavailable,
839 stats.sectors[BCH_DATA_SB],
840 stats.sectors[BCH_DATA_JOURNAL],
841 stats.sectors[BCH_DATA_BTREE],
842 stats.sectors[BCH_DATA_USER],
843 stats.sectors[BCH_DATA_CACHED],
844 stats.sectors_fragmented,
845 ca->copygc_threshold,
846 c->freelist_wait.list.first ? "waiting" : "empty",
847 c->open_buckets_nr_free, OPEN_BUCKETS_COUNT, BTREE_NODE_RESERVE,
848 c->open_buckets_wait.list.first ? "waiting" : "empty");
851 static const char * const bch2_rw[] = {
857 static ssize_t show_dev_iodone(struct bch_dev *ca, char *buf)
859 struct printbuf out = _PBUF(buf, PAGE_SIZE);
862 for (rw = 0; rw < 2; rw++) {
863 pr_buf(&out, "%s:\n", bch2_rw[rw]);
865 for (i = 1; i < BCH_DATA_NR; i++) {
868 for_each_possible_cpu(cpu)
869 n += per_cpu_ptr(ca->io_done, cpu)->sectors[rw][i];
871 pr_buf(&out, "%-12s:%12llu\n",
872 bch2_data_types[i], n << 9);
876 return out.pos - buf;
881 struct bch_dev *ca = container_of(kobj, struct bch_dev, kobj);
882 struct bch_fs *c = ca->fs;
883 struct printbuf out = _PBUF(buf, PAGE_SIZE);
885 sysfs_printf(uuid, "%pU\n", ca->uuid.b);
887 sysfs_print(bucket_size, bucket_bytes(ca));
888 sysfs_print(block_size, block_bytes(c));
889 sysfs_print(first_bucket, ca->mi.first_bucket);
890 sysfs_print(nbuckets, ca->mi.nbuckets);
891 sysfs_print(durability, ca->mi.durability);
892 sysfs_print(discard, ca->mi.discard);
894 if (attr == &sysfs_label) {
896 mutex_lock(&c->sb_lock);
897 bch2_disk_path_to_text(&out, &c->disk_sb,
899 mutex_unlock(&c->sb_lock);
901 pr_buf(&out, "none");
905 return out.pos - buf;
908 if (attr == &sysfs_has_data) {
909 bch2_flags_to_text(&out, bch2_data_types,
910 bch2_dev_has_data(c, ca));
912 return out.pos - buf;
915 sysfs_pd_controller_show(copy_gc, &ca->copygc_pd);
917 if (attr == &sysfs_cache_replacement_policy) {
918 bch2_string_opt_to_text(&out,
919 bch2_cache_replacement_policies,
922 return out.pos - buf;
925 if (attr == &sysfs_state_rw) {
926 bch2_string_opt_to_text(&out, bch2_dev_state,
929 return out.pos - buf;
932 if (attr == &sysfs_iodone)
933 return show_dev_iodone(ca, buf);
935 sysfs_print(io_latency_read, atomic64_read(&ca->cur_latency[READ]));
936 sysfs_print(io_latency_write, atomic64_read(&ca->cur_latency[WRITE]));
938 if (attr == &sysfs_io_latency_stats_read)
939 return bch2_time_stats_print(&ca->io_latency[READ], buf, PAGE_SIZE);
940 if (attr == &sysfs_io_latency_stats_write)
941 return bch2_time_stats_print(&ca->io_latency[WRITE], buf, PAGE_SIZE);
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 show_quantiles(c, ca, buf, bucket_last_io_fn, (void *) 0);
949 if (attr == &sysfs_bucket_quantiles_last_write)
950 return show_quantiles(c, ca, buf, bucket_last_io_fn, (void *) 1);
951 if (attr == &sysfs_bucket_quantiles_fragmentation)
952 return show_quantiles(c, ca, buf, bucket_sectors_used_fn, NULL);
953 if (attr == &sysfs_bucket_quantiles_oldest_gen)
954 return show_quantiles(c, ca, buf, bucket_oldest_gen_fn, NULL);
956 if (attr == &sysfs_reserve_stats)
957 return show_reserve_stats(ca, buf);
958 if (attr == &sysfs_alloc_debug)
959 return show_dev_alloc_debug(ca, buf);
966 struct bch_dev *ca = container_of(kobj, struct bch_dev, kobj);
967 struct bch_fs *c = ca->fs;
968 struct bch_member *mi;
970 sysfs_pd_controller_store(copy_gc, &ca->copygc_pd);
972 if (attr == &sysfs_discard) {
973 bool v = strtoul_or_return(buf);
975 mutex_lock(&c->sb_lock);
976 mi = &bch2_sb_get_members(c->disk_sb.sb)->members[ca->dev_idx];
978 if (v != BCH_MEMBER_DISCARD(mi)) {
979 SET_BCH_MEMBER_DISCARD(mi, v);
982 mutex_unlock(&c->sb_lock);
985 if (attr == &sysfs_cache_replacement_policy) {
986 ssize_t v = __sysfs_match_string(bch2_cache_replacement_policies, -1, buf);
991 mutex_lock(&c->sb_lock);
992 mi = &bch2_sb_get_members(c->disk_sb.sb)->members[ca->dev_idx];
994 if ((unsigned) v != BCH_MEMBER_REPLACEMENT(mi)) {
995 SET_BCH_MEMBER_REPLACEMENT(mi, v);
998 mutex_unlock(&c->sb_lock);
1001 if (attr == &sysfs_label) {
1005 tmp = kstrdup(buf, GFP_KERNEL);
1009 ret = bch2_dev_group_set(c, ca, strim(tmp));
1015 if (attr == &sysfs_wake_allocator)
1016 bch2_wake_allocator(ca);
1020 SYSFS_OPS(bch2_dev);
1022 struct attribute *bch2_dev_files[] = {
1026 &sysfs_first_bucket,
1032 &sysfs_cache_replacement_policy,
1039 &sysfs_io_latency_read,
1040 &sysfs_io_latency_write,
1041 &sysfs_io_latency_stats_read,
1042 &sysfs_io_latency_stats_write,
1045 /* alloc info - other stats: */
1046 &sysfs_bucket_quantiles_last_read,
1047 &sysfs_bucket_quantiles_last_write,
1048 &sysfs_bucket_quantiles_fragmentation,
1049 &sysfs_bucket_quantiles_oldest_gen,
1051 &sysfs_reserve_stats,
1055 &sysfs_wake_allocator,
1057 sysfs_pd_controller_files(copy_gc),
1061 #endif /* _BCACHEFS_SYSFS_H_ */