2 * bcache sysfs interfaces
4 * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com>
5 * Copyright 2012 Google, Inc.
12 #include "btree_cache.h"
13 #include "btree_iter.h"
14 #include "btree_update.h"
25 #include <linux/blkdev.h>
26 #include <linux/sort.h>
30 #define SYSFS_OPS(type) \
31 struct sysfs_ops type ## _sysfs_ops = { \
32 .show = type ## _show, \
33 .store = type ## _store \
37 static ssize_t fn ## _show(struct kobject *kobj, struct attribute *attr,\
41 static ssize_t fn ## _store(struct kobject *kobj, struct attribute *attr,\
42 const char *buf, size_t size) \
44 #define __sysfs_attribute(_name, _mode) \
45 static struct attribute sysfs_##_name = \
46 { .name = #_name, .mode = _mode }
48 #define write_attribute(n) __sysfs_attribute(n, S_IWUSR)
49 #define read_attribute(n) __sysfs_attribute(n, S_IRUGO)
50 #define rw_attribute(n) __sysfs_attribute(n, S_IRUGO|S_IWUSR)
52 #define sysfs_printf(file, fmt, ...) \
54 if (attr == &sysfs_ ## file) \
55 return snprintf(buf, PAGE_SIZE, fmt "\n", __VA_ARGS__); \
58 #define sysfs_print(file, var) \
60 if (attr == &sysfs_ ## file) \
61 return snprint(buf, PAGE_SIZE, var); \
64 #define sysfs_hprint(file, val) \
66 if (attr == &sysfs_ ## file) { \
67 ssize_t ret = bch2_hprint(buf, val); \
73 #define var_printf(_var, fmt) sysfs_printf(_var, fmt, var(_var))
74 #define var_print(_var) sysfs_print(_var, var(_var))
75 #define var_hprint(_var) sysfs_hprint(_var, var(_var))
77 #define sysfs_strtoul(file, var) \
79 if (attr == &sysfs_ ## file) \
80 return strtoul_safe(buf, var) ?: (ssize_t) size; \
83 #define sysfs_strtoul_clamp(file, var, min, max) \
85 if (attr == &sysfs_ ## file) \
86 return strtoul_safe_clamp(buf, var, min, max) \
90 #define strtoul_or_return(cp) \
93 int _r = kstrtoul(cp, 10, &_v); \
99 #define strtoul_restrict_or_return(cp, min, max) \
101 unsigned long __v = 0; \
102 int _r = strtoul_safe_restrict(cp, __v, min, max); \
108 #define strtoi_h_or_return(cp) \
111 int _r = strtoi_h(cp, &_v); \
117 #define sysfs_hatoi(file, var) \
119 if (attr == &sysfs_ ## file) \
120 return strtoi_h(buf, &var) ?: (ssize_t) size; \
123 write_attribute(trigger_btree_coalesce);
124 write_attribute(trigger_gc);
125 write_attribute(prune_cache);
127 read_attribute(uuid);
128 read_attribute(minor);
129 read_attribute(bucket_size);
130 read_attribute(bucket_size_bytes);
131 read_attribute(block_size);
132 read_attribute(block_size_bytes);
133 read_attribute(btree_node_size);
134 read_attribute(btree_node_size_bytes);
135 read_attribute(first_bucket);
136 read_attribute(nbuckets);
137 read_attribute(tree_depth);
138 read_attribute(root_usage_percent);
139 read_attribute(read_priority_stats);
140 read_attribute(write_priority_stats);
141 read_attribute(fragmentation_stats);
142 read_attribute(oldest_gen_stats);
143 read_attribute(reserve_stats);
144 read_attribute(btree_cache_size);
145 read_attribute(cache_available_percent);
146 read_attribute(compression_stats);
147 read_attribute(written);
148 read_attribute(btree_written);
149 read_attribute(metadata_written);
150 read_attribute(journal_debug);
151 write_attribute(journal_flush);
152 read_attribute(internal_uuid);
154 read_attribute(btree_gc_running);
156 read_attribute(btree_nodes);
157 read_attribute(btree_used_percent);
158 read_attribute(average_key_size);
159 read_attribute(available_buckets);
160 read_attribute(free_buckets);
161 read_attribute(dirty_data);
162 read_attribute(dirty_bytes);
163 read_attribute(dirty_buckets);
164 read_attribute(cached_data);
165 read_attribute(cached_bytes);
166 read_attribute(cached_buckets);
167 read_attribute(meta_buckets);
168 read_attribute(alloc_buckets);
169 read_attribute(has_data);
170 read_attribute(has_metadata);
171 read_attribute(bset_tree_stats);
172 read_attribute(alloc_debug);
174 read_attribute(cache_read_races);
176 rw_attribute(journal_write_delay_ms);
177 rw_attribute(journal_reclaim_delay_ms);
179 rw_attribute(discard);
180 rw_attribute(cache_replacement_policy);
182 rw_attribute(foreground_write_ratelimit_enabled);
183 rw_attribute(copy_gc_enabled);
184 sysfs_pd_controller_attribute(copy_gc);
187 rw_attribute(tiering_enabled);
188 rw_attribute(tiering_percent);
189 sysfs_pd_controller_attribute(tiering);
191 sysfs_pd_controller_attribute(foreground_write);
193 rw_attribute(pd_controllers_update_seconds);
195 rw_attribute(foreground_target_percent);
197 read_attribute(meta_replicas_have);
198 read_attribute(data_replicas_have);
200 #define BCH_DEBUG_PARAM(name, description) \
204 #undef BCH_DEBUG_PARAM
206 #define BCH_OPT(_name, _mode, ...) \
207 static struct attribute sysfs_opt_##_name = { \
208 .name = #_name, .mode = _mode, \
214 #define BCH_TIME_STAT(name, frequency_units, duration_units) \
215 sysfs_time_stats_attribute(name, frequency_units, duration_units);
219 static struct attribute sysfs_state_rw = {
224 static int bch2_bset_print_stats(struct bch_fs *c, char *buf)
226 struct bset_stats stats;
229 struct bucket_table *tbl;
230 struct rhash_head *pos;
233 memset(&stats, 0, sizeof(stats));
236 for_each_cached_btree(b, c, tbl, iter, pos) {
237 bch2_btree_keys_stats(b, &stats);
242 return snprintf(buf, PAGE_SIZE,
244 "written sets: %zu\n"
245 "written key bytes: %zu\n"
246 "unwritten sets: %zu\n"
247 "unwritten key bytes: %zu\n"
248 "no table sets: %zu\n"
249 "no table key bytes: %zu\n"
251 "failed unpacked: %zu\n"
253 "failed overflow: %zu\n",
255 stats.sets[BSET_RO_AUX_TREE].nr,
256 stats.sets[BSET_RO_AUX_TREE].bytes,
257 stats.sets[BSET_RW_AUX_TREE].nr,
258 stats.sets[BSET_RW_AUX_TREE].bytes,
259 stats.sets[BSET_NO_AUX_TREE].nr,
260 stats.sets[BSET_NO_AUX_TREE].bytes,
262 stats.failed_unpacked,
264 stats.failed_overflow);
267 static unsigned bch2_root_usage(struct bch_fs *c)
270 struct bkey_packed *k;
272 struct btree_node_iter iter;
277 six_unlock_read(&b->lock);
279 b = c->btree_roots[BTREE_ID_EXTENTS].b;
280 six_lock_read(&b->lock);
281 } while (b != c->btree_roots[BTREE_ID_EXTENTS].b);
283 for_each_btree_node_key(b, k, &iter, btree_node_is_extents(b))
284 bytes += bkey_bytes(k);
286 six_unlock_read(&b->lock);
288 return (bytes * 100) / btree_bytes(c);
291 static size_t bch2_btree_cache_size(struct bch_fs *c)
296 mutex_lock(&c->btree_cache_lock);
297 list_for_each_entry(b, &c->btree_cache, list)
298 ret += btree_bytes(c);
300 mutex_unlock(&c->btree_cache_lock);
304 static unsigned bch2_fs_available_percent(struct bch_fs *c)
306 return div64_u64((u64) sectors_available(c) * 100,
311 static unsigned bch2_btree_used(struct bch_fs *c)
313 return div64_u64(c->gc_stats.key_bytes * 100,
314 (c->gc_stats.nodes ?: 1) * btree_bytes(c));
317 static unsigned bch2_average_key_size(struct bch_fs *c)
319 return c->gc_stats.nkeys
320 ? div64_u64(c->gc_stats.data, c->gc_stats.nkeys)
325 static ssize_t show_fs_alloc_debug(struct bch_fs *c, char *buf)
327 struct bch_fs_usage stats = bch2_fs_usage_read(c);
329 return scnprintf(buf, PAGE_SIZE,
330 "capacity:\t\t%llu\n"
334 "\tcached:\t\t%llu\n"
338 "\tcached:\t\t%llu\n"
339 "persistent reserved sectors:\t%llu\n"
340 "online reserved sectors:\t%llu\n",
342 stats.s[S_COMPRESSED][S_META],
343 stats.s[S_COMPRESSED][S_DIRTY],
344 stats.s[S_COMPRESSED][S_CACHED],
345 stats.s[S_UNCOMPRESSED][S_META],
346 stats.s[S_UNCOMPRESSED][S_DIRTY],
347 stats.s[S_UNCOMPRESSED][S_CACHED],
348 stats.persistent_reserved,
349 stats.online_reserved);
352 static ssize_t bch2_compression_stats(struct bch_fs *c, char *buf)
354 struct btree_iter iter;
356 u64 nr_uncompressed_extents = 0, uncompressed_sectors = 0,
357 nr_compressed_extents = 0,
358 compressed_sectors_compressed = 0,
359 compressed_sectors_uncompressed = 0;
361 for_each_btree_key(&iter, c, BTREE_ID_EXTENTS, POS_MIN, k)
362 if (k.k->type == BCH_EXTENT) {
363 struct bkey_s_c_extent e = bkey_s_c_to_extent(k);
364 const struct bch_extent_ptr *ptr;
365 const union bch_extent_crc *crc;
367 extent_for_each_ptr_crc(e, ptr, crc) {
368 if (crc_compression_type(crc) == BCH_COMPRESSION_NONE) {
369 nr_uncompressed_extents++;
370 uncompressed_sectors += e.k->size;
372 nr_compressed_extents++;
373 compressed_sectors_compressed +=
374 crc_compressed_size(e.k, crc);
375 compressed_sectors_uncompressed +=
376 crc_uncompressed_size(e.k, crc);
379 /* only looking at the first ptr */
383 bch2_btree_iter_unlock(&iter);
385 return snprintf(buf, PAGE_SIZE,
386 "uncompressed data:\n"
387 " nr extents: %llu\n"
388 " size (bytes): %llu\n"
390 " nr extents: %llu\n"
391 " compressed size (bytes): %llu\n"
392 " uncompressed size (bytes): %llu\n",
393 nr_uncompressed_extents,
394 uncompressed_sectors << 9,
395 nr_compressed_extents,
396 compressed_sectors_compressed << 9,
397 compressed_sectors_uncompressed << 9);
402 struct bch_fs *c = container_of(kobj, struct bch_fs, kobj);
404 sysfs_print(minor, c->minor);
406 sysfs_print(journal_write_delay_ms, c->journal.write_delay_ms);
407 sysfs_print(journal_reclaim_delay_ms, c->journal.reclaim_delay_ms);
409 sysfs_hprint(block_size, block_bytes(c));
410 sysfs_print(block_size_bytes, block_bytes(c));
411 sysfs_hprint(btree_node_size, c->sb.btree_node_size << 9);
412 sysfs_print(btree_node_size_bytes, c->sb.btree_node_size << 9);
414 sysfs_hprint(btree_cache_size, bch2_btree_cache_size(c));
415 sysfs_print(cache_available_percent, bch2_fs_available_percent(c));
417 sysfs_print(btree_gc_running, c->gc_pos.phase != GC_PHASE_DONE);
420 /* XXX: reimplement */
421 sysfs_print(btree_used_percent, bch2_btree_used(c));
422 sysfs_print(btree_nodes, c->gc_stats.nodes);
423 sysfs_hprint(average_key_size, bch2_average_key_size(c));
426 sysfs_print(cache_read_races,
427 atomic_long_read(&c->cache_read_races));
429 sysfs_printf(foreground_write_ratelimit_enabled, "%i",
430 c->foreground_write_ratelimit_enabled);
431 sysfs_printf(copy_gc_enabled, "%i", c->copy_gc_enabled);
432 sysfs_pd_controller_show(foreground_write, &c->foreground_write_pd);
434 sysfs_print(pd_controllers_update_seconds,
435 c->pd_controllers_update_seconds);
436 sysfs_print(foreground_target_percent, c->foreground_target_percent);
438 sysfs_printf(tiering_enabled, "%i", c->tiering_enabled);
439 sysfs_print(tiering_percent, c->tiering_percent);
441 sysfs_pd_controller_show(tiering, &c->tiers[1].pd); /* XXX */
443 sysfs_printf(meta_replicas_have, "%u", c->sb.meta_replicas_have);
444 sysfs_printf(data_replicas_have, "%u", c->sb.data_replicas_have);
448 if (attr == &sysfs_journal_debug)
449 return bch2_journal_print_debug(&c->journal, buf);
451 #define BCH_DEBUG_PARAM(name, description) sysfs_print(name, c->name);
453 #undef BCH_DEBUG_PARAM
455 if (!bch2_fs_running(c))
458 if (attr == &sysfs_bset_tree_stats)
459 return bch2_bset_print_stats(c, buf);
460 if (attr == &sysfs_alloc_debug)
461 return show_fs_alloc_debug(c, buf);
463 sysfs_print(tree_depth, c->btree_roots[BTREE_ID_EXTENTS].b->level);
464 sysfs_print(root_usage_percent, bch2_root_usage(c));
466 if (attr == &sysfs_compression_stats)
467 return bch2_compression_stats(c, buf);
469 sysfs_printf(internal_uuid, "%pU", c->sb.uuid.b);
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 sysfs_strtoul(foreground_write_ratelimit_enabled,
482 c->foreground_write_ratelimit_enabled);
484 if (attr == &sysfs_copy_gc_enabled) {
487 ssize_t ret = strtoul_safe(buf, c->copy_gc_enabled)
490 for_each_member_device(ca, c, i)
491 if (ca->moving_gc_read)
492 wake_up_process(ca->moving_gc_read);
496 if (attr == &sysfs_tiering_enabled) {
497 ssize_t ret = strtoul_safe(buf, c->tiering_enabled)
500 bch2_tiering_start(c); /* issue wakeups */
504 sysfs_pd_controller_store(foreground_write, &c->foreground_write_pd);
506 sysfs_strtoul(pd_controllers_update_seconds,
507 c->pd_controllers_update_seconds);
508 sysfs_strtoul(foreground_target_percent, c->foreground_target_percent);
510 sysfs_strtoul(tiering_percent, c->tiering_percent);
511 sysfs_pd_controller_store(tiering, &c->tiers[1].pd); /* XXX */
515 #define BCH_DEBUG_PARAM(name, description) sysfs_strtoul(name, c->name);
517 #undef BCH_DEBUG_PARAM
519 if (!bch2_fs_running(c))
522 if (attr == &sysfs_journal_flush) {
523 bch2_journal_meta_async(&c->journal, NULL);
528 if (attr == &sysfs_trigger_btree_coalesce)
533 if (attr == &sysfs_trigger_gc)
536 if (attr == &sysfs_prune_cache) {
537 struct shrink_control sc;
539 sc.gfp_mask = GFP_KERNEL;
540 sc.nr_to_scan = strtoul_or_return(buf);
541 c->btree_cache_shrink.scan_objects(&c->btree_cache_shrink, &sc);
549 struct bch_fs *c = container_of(kobj, struct bch_fs, kobj);
551 mutex_lock(&c->state_lock);
552 size = __bch2_fs_store(kobj, attr, buf, size);
553 mutex_unlock(&c->state_lock);
559 struct attribute *bch2_fs_files[] = {
560 &sysfs_journal_write_delay_ms,
561 &sysfs_journal_reclaim_delay_ms,
564 &sysfs_block_size_bytes,
565 &sysfs_btree_node_size,
566 &sysfs_btree_node_size_bytes,
568 &sysfs_root_usage_percent,
569 &sysfs_btree_cache_size,
570 &sysfs_cache_available_percent,
571 &sysfs_compression_stats,
573 &sysfs_average_key_size,
575 &sysfs_meta_replicas_have,
576 &sysfs_data_replicas_have,
578 &sysfs_foreground_target_percent,
579 &sysfs_tiering_percent,
581 &sysfs_journal_flush,
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[] = {
601 &sysfs_journal_debug,
605 &sysfs_btree_gc_running,
608 &sysfs_btree_used_percent,
610 &sysfs_bset_tree_stats,
611 &sysfs_cache_read_races,
613 &sysfs_trigger_btree_coalesce,
616 &sysfs_foreground_write_ratelimit_enabled,
617 &sysfs_copy_gc_enabled,
618 &sysfs_tiering_enabled,
619 sysfs_pd_controller_files(tiering),
620 sysfs_pd_controller_files(foreground_write),
621 &sysfs_internal_uuid,
623 #define BCH_DEBUG_PARAM(name, description) &sysfs_##name,
625 #undef BCH_DEBUG_PARAM
632 SHOW(bch2_fs_opts_dir)
634 struct bch_fs *c = container_of(kobj, struct bch_fs, opts_dir);
636 return bch2_opt_show(&c->opts, attr->name, buf, PAGE_SIZE);
639 STORE(bch2_fs_opts_dir)
641 struct bch_fs *c = container_of(kobj, struct bch_fs, opts_dir);
642 const struct bch_option *opt;
646 id = bch2_parse_sysfs_opt(attr->name, buf, &v);
650 opt = &bch2_opt_table[id];
652 mutex_lock(&c->sb_lock);
654 if (id == Opt_compression) {
655 int ret = bch2_check_set_has_compressed_data(c, v);
657 mutex_unlock(&c->sb_lock);
662 if (opt->set_sb != SET_NO_SB_OPT) {
663 opt->set_sb(c->disk_sb, v);
667 bch2_opt_set(&c->opts, id, v);
669 mutex_unlock(&c->sb_lock);
673 SYSFS_OPS(bch2_fs_opts_dir);
675 struct attribute *bch2_fs_opts_dir_files[] = {
676 #define BCH_OPT(_name, ...) \
687 SHOW(bch2_fs_time_stats)
689 struct bch_fs *c = container_of(kobj, struct bch_fs, time_stats);
691 #define BCH_TIME_STAT(name, frequency_units, duration_units) \
692 sysfs_print_time_stats(&c->name##_time, name, \
693 frequency_units, duration_units);
700 STORE(bch2_fs_time_stats)
702 struct bch_fs *c = container_of(kobj, struct bch_fs, time_stats);
704 #define BCH_TIME_STAT(name, frequency_units, duration_units) \
705 sysfs_clear_time_stats(&c->name##_time, name);
711 SYSFS_OPS(bch2_fs_time_stats);
713 struct attribute *bch2_fs_time_stats_files[] = {
714 #define BCH_TIME_STAT(name, frequency_units, duration_units) \
715 sysfs_time_stats_attribute_list(name, frequency_units, duration_units)
722 typedef unsigned (bucket_map_fn)(struct bch_dev *, struct bucket *, void *);
724 static unsigned bucket_priority_fn(struct bch_dev *ca, struct bucket *g,
727 int rw = (private ? 1 : 0);
729 return ca->fs->prio_clock[rw].hand - g->prio[rw];
732 static unsigned bucket_sectors_used_fn(struct bch_dev *ca, struct bucket *g,
735 return bucket_sectors_used(g);
738 static unsigned bucket_oldest_gen_fn(struct bch_dev *ca, struct bucket *g,
741 return bucket_gc_gen(ca, g);
744 static ssize_t show_quantiles(struct bch_dev *ca, char *buf,
745 bucket_map_fn *fn, void *private)
747 int cmp(const void *l, const void *r)
748 { return *((unsigned *) r) - *((unsigned *) l); }
750 size_t n = ca->mi.nbuckets, i;
751 /* Compute 31 quantiles */
755 p = vzalloc(ca->mi.nbuckets * sizeof(unsigned));
759 for (i = ca->mi.first_bucket; i < n; i++)
760 p[i] = fn(ca, &ca->buckets[i], private);
762 sort(p, n, sizeof(unsigned), cmp, NULL);
768 for (i = 0; i < ARRAY_SIZE(q); i++)
769 q[i] = p[n * (i + 1) / (ARRAY_SIZE(q) + 1)];
773 for (i = 0; i < ARRAY_SIZE(q); i++)
774 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
782 static ssize_t show_reserve_stats(struct bch_dev *ca, char *buf)
784 enum alloc_reserve i;
787 spin_lock(&ca->freelist_lock);
789 ret = scnprintf(buf, PAGE_SIZE,
790 "free_inc:\t%zu\t%zu\n",
791 fifo_used(&ca->free_inc),
794 for (i = 0; i < RESERVE_NR; i++)
795 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
796 "free[%u]:\t%zu\t%zu\n", i,
797 fifo_used(&ca->free[i]),
800 spin_unlock(&ca->freelist_lock);
805 static ssize_t show_dev_alloc_debug(struct bch_dev *ca, char *buf)
807 struct bch_fs *c = ca->fs;
808 struct bch_dev_usage stats = bch2_dev_usage_read(ca);
810 return scnprintf(buf, PAGE_SIZE,
811 "free_inc: %zu/%zu\n"
812 "free[RESERVE_PRIO]: %zu/%zu\n"
813 "free[RESERVE_BTREE]: %zu/%zu\n"
814 "free[RESERVE_MOVINGGC]: %zu/%zu\n"
815 "free[RESERVE_NONE]: %zu/%zu\n"
819 "available: %llu/%llu\n"
820 "freelist_wait: %s\n"
821 "open buckets: %u/%u (reserved %u)\n"
822 "open_buckets_wait: %s\n",
823 fifo_used(&ca->free_inc), ca->free_inc.size,
824 fifo_used(&ca->free[RESERVE_PRIO]), ca->free[RESERVE_PRIO].size,
825 fifo_used(&ca->free[RESERVE_BTREE]), ca->free[RESERVE_BTREE].size,
826 fifo_used(&ca->free[RESERVE_MOVINGGC]), ca->free[RESERVE_MOVINGGC].size,
827 fifo_used(&ca->free[RESERVE_NONE]), ca->free[RESERVE_NONE].size,
828 stats.buckets_alloc, ca->mi.nbuckets - ca->mi.first_bucket,
829 stats.buckets_meta, ca->mi.nbuckets - ca->mi.first_bucket,
830 stats.buckets_dirty, ca->mi.nbuckets - ca->mi.first_bucket,
831 __dev_buckets_available(ca, stats), ca->mi.nbuckets - ca->mi.first_bucket,
832 c->freelist_wait.list.first ? "waiting" : "empty",
833 c->open_buckets_nr_free, OPEN_BUCKETS_COUNT, BTREE_NODE_RESERVE,
834 c->open_buckets_wait.list.first ? "waiting" : "empty");
837 static u64 sectors_written(struct bch_dev *ca)
842 for_each_possible_cpu(cpu)
843 ret += *per_cpu_ptr(ca->sectors_written, cpu);
850 struct bch_dev *ca = container_of(kobj, struct bch_dev, kobj);
851 struct bch_fs *c = ca->fs;
852 struct bch_dev_usage stats = bch2_dev_usage_read(ca);
854 sysfs_printf(uuid, "%pU\n", ca->uuid.b);
856 sysfs_hprint(bucket_size, bucket_bytes(ca));
857 sysfs_print(bucket_size_bytes, bucket_bytes(ca));
858 sysfs_hprint(block_size, block_bytes(c));
859 sysfs_print(block_size_bytes, block_bytes(c));
860 sysfs_print(first_bucket, ca->mi.first_bucket);
861 sysfs_print(nbuckets, ca->mi.nbuckets);
862 sysfs_print(discard, ca->mi.discard);
863 sysfs_hprint(written, sectors_written(ca) << 9);
864 sysfs_hprint(btree_written,
865 atomic64_read(&ca->btree_sectors_written) << 9);
866 sysfs_hprint(metadata_written,
867 (atomic64_read(&ca->meta_sectors_written) +
868 atomic64_read(&ca->btree_sectors_written)) << 9);
870 sysfs_hprint(dirty_data, stats.sectors[S_DIRTY] << 9);
871 sysfs_print(dirty_bytes, stats.sectors[S_DIRTY] << 9);
872 sysfs_print(dirty_buckets, stats.buckets_dirty);
873 sysfs_hprint(cached_data, stats.sectors[S_CACHED] << 9);
874 sysfs_print(cached_bytes, stats.sectors[S_CACHED] << 9);
875 sysfs_print(cached_buckets, stats.buckets_cached);
876 sysfs_print(meta_buckets, stats.buckets_meta);
877 sysfs_print(alloc_buckets, stats.buckets_alloc);
878 sysfs_print(available_buckets, dev_buckets_available(ca));
879 sysfs_print(free_buckets, dev_buckets_free(ca));
880 sysfs_print(has_data, ca->mi.has_data);
881 sysfs_print(has_metadata, ca->mi.has_metadata);
883 sysfs_pd_controller_show(copy_gc, &ca->moving_gc_pd);
885 if (attr == &sysfs_cache_replacement_policy)
886 return bch2_snprint_string_list(buf, PAGE_SIZE,
887 bch2_cache_replacement_policies,
890 sysfs_print(tier, ca->mi.tier);
892 if (attr == &sysfs_state_rw)
893 return bch2_snprint_string_list(buf, PAGE_SIZE,
897 if (attr == &sysfs_read_priority_stats)
898 return show_quantiles(ca, buf, bucket_priority_fn, (void *) 0);
899 if (attr == &sysfs_write_priority_stats)
900 return show_quantiles(ca, buf, bucket_priority_fn, (void *) 1);
901 if (attr == &sysfs_fragmentation_stats)
902 return show_quantiles(ca, buf, bucket_sectors_used_fn, NULL);
903 if (attr == &sysfs_oldest_gen_stats)
904 return show_quantiles(ca, buf, bucket_oldest_gen_fn, NULL);
905 if (attr == &sysfs_reserve_stats)
906 return show_reserve_stats(ca, buf);
907 if (attr == &sysfs_alloc_debug)
908 return show_dev_alloc_debug(ca, buf);
915 struct bch_dev *ca = container_of(kobj, struct bch_dev, kobj);
916 struct bch_fs *c = ca->fs;
917 struct bch_member *mi;
919 sysfs_pd_controller_store(copy_gc, &ca->moving_gc_pd);
921 if (attr == &sysfs_discard) {
922 bool v = strtoul_or_return(buf);
924 mutex_lock(&c->sb_lock);
925 mi = &bch2_sb_get_members(c->disk_sb)->members[ca->dev_idx];
927 if (v != BCH_MEMBER_DISCARD(mi)) {
928 SET_BCH_MEMBER_DISCARD(mi, v);
931 mutex_unlock(&c->sb_lock);
934 if (attr == &sysfs_cache_replacement_policy) {
935 ssize_t v = bch2_read_string_list(buf, bch2_cache_replacement_policies);
940 mutex_lock(&c->sb_lock);
941 mi = &bch2_sb_get_members(c->disk_sb)->members[ca->dev_idx];
943 if ((unsigned) v != BCH_MEMBER_REPLACEMENT(mi)) {
944 SET_BCH_MEMBER_REPLACEMENT(mi, v);
947 mutex_unlock(&c->sb_lock);
950 if (attr == &sysfs_tier) {
952 unsigned v = strtoul_restrict_or_return(buf,
953 0, BCH_TIER_MAX - 1);
955 mutex_lock(&c->sb_lock);
956 prev_tier = ca->mi.tier;
958 if (v == ca->mi.tier) {
959 mutex_unlock(&c->sb_lock);
963 mi = &bch2_sb_get_members(c->disk_sb)->members[ca->dev_idx];
964 SET_BCH_MEMBER_TIER(mi, v);
967 bch2_dev_group_remove(&c->tiers[prev_tier].devs, ca);
968 bch2_dev_group_add(&c->tiers[ca->mi.tier].devs, ca);
969 mutex_unlock(&c->sb_lock);
971 bch2_recalc_capacity(c);
972 bch2_tiering_start(c);
979 struct attribute *bch2_dev_files[] = {
982 &sysfs_bucket_size_bytes,
984 &sysfs_block_size_bytes,
987 &sysfs_read_priority_stats,
988 &sysfs_write_priority_stats,
989 &sysfs_fragmentation_stats,
990 &sysfs_oldest_gen_stats,
991 &sysfs_reserve_stats,
992 &sysfs_available_buckets,
996 &sysfs_dirty_buckets,
999 &sysfs_cached_buckets,
1000 &sysfs_meta_buckets,
1001 &sysfs_alloc_buckets,
1003 &sysfs_has_metadata,
1006 &sysfs_btree_written,
1007 &sysfs_metadata_written,
1008 &sysfs_cache_replacement_policy,
1013 sysfs_pd_controller_files(copy_gc),