]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/sysfs.c
Update bcachefs sources to f26267fc82 bcachefs: kill bset_tree->max_key
[bcachefs-tools-debian] / libbcachefs / sysfs.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * bcache sysfs interfaces
4  *
5  * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com>
6  * Copyright 2012 Google, Inc.
7  */
8
9 #ifndef NO_BCACHEFS_SYSFS
10
11 #include "bcachefs.h"
12 #include "alloc_background.h"
13 #include "sysfs.h"
14 #include "btree_cache.h"
15 #include "btree_io.h"
16 #include "btree_iter.h"
17 #include "btree_key_cache.h"
18 #include "btree_update.h"
19 #include "btree_update_interior.h"
20 #include "btree_gc.h"
21 #include "buckets.h"
22 #include "clock.h"
23 #include "disk_groups.h"
24 #include "ec.h"
25 #include "inode.h"
26 #include "journal.h"
27 #include "keylist.h"
28 #include "move.h"
29 #include "opts.h"
30 #include "rebalance.h"
31 #include "replicas.h"
32 #include "super-io.h"
33 #include "tests.h"
34
35 #include <linux/blkdev.h>
36 #include <linux/sort.h>
37 #include <linux/sched/clock.h>
38
39 #include "util.h"
40
41 #define SYSFS_OPS(type)                                                 \
42 struct sysfs_ops type ## _sysfs_ops = {                                 \
43         .show   = type ## _show,                                        \
44         .store  = type ## _store                                        \
45 }
46
47 #define SHOW(fn)                                                        \
48 static ssize_t fn ## _show(struct kobject *kobj, struct attribute *attr,\
49                            char *buf)                                   \
50
51 #define STORE(fn)                                                       \
52 static ssize_t fn ## _store(struct kobject *kobj, struct attribute *attr,\
53                             const char *buf, size_t size)               \
54
55 #define __sysfs_attribute(_name, _mode)                                 \
56         static struct attribute sysfs_##_name =                         \
57                 { .name = #_name, .mode = _mode }
58
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)
62
63 #define sysfs_printf(file, fmt, ...)                                    \
64 do {                                                                    \
65         if (attr == &sysfs_ ## file)                                    \
66                 return scnprintf(buf, PAGE_SIZE, fmt "\n", __VA_ARGS__);\
67 } while (0)
68
69 #define sysfs_print(file, var)                                          \
70 do {                                                                    \
71         if (attr == &sysfs_ ## file)                                    \
72                 return snprint(buf, PAGE_SIZE, var);                    \
73 } while (0)
74
75 #define sysfs_hprint(file, val)                                         \
76 do {                                                                    \
77         if (attr == &sysfs_ ## file) {                                  \
78                 bch2_hprint(&out, val);                                 \
79                 pr_buf(&out, "\n");                                     \
80                 return out.pos - buf;                                   \
81         }                                                               \
82 } while (0)
83
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))
87
88 #define sysfs_strtoul(file, var)                                        \
89 do {                                                                    \
90         if (attr == &sysfs_ ## file)                                    \
91                 return strtoul_safe(buf, var) ?: (ssize_t) size;        \
92 } while (0)
93
94 #define sysfs_strtoul_clamp(file, var, min, max)                        \
95 do {                                                                    \
96         if (attr == &sysfs_ ## file)                                    \
97                 return strtoul_safe_clamp(buf, var, min, max)           \
98                         ?: (ssize_t) size;                              \
99 } while (0)
100
101 #define strtoul_or_return(cp)                                           \
102 ({                                                                      \
103         unsigned long _v;                                               \
104         int _r = kstrtoul(cp, 10, &_v);                                 \
105         if (_r)                                                         \
106                 return _r;                                              \
107         _v;                                                             \
108 })
109
110 #define strtoul_restrict_or_return(cp, min, max)                        \
111 ({                                                                      \
112         unsigned long __v = 0;                                          \
113         int _r = strtoul_safe_restrict(cp, __v, min, max);              \
114         if (_r)                                                         \
115                 return _r;                                              \
116         __v;                                                            \
117 })
118
119 #define strtoi_h_or_return(cp)                                          \
120 ({                                                                      \
121         u64 _v;                                                         \
122         int _r = strtoi_h(cp, &_v);                                     \
123         if (_r)                                                         \
124                 return _r;                                              \
125         _v;                                                             \
126 })
127
128 #define sysfs_hatoi(file, var)                                          \
129 do {                                                                    \
130         if (attr == &sysfs_ ## file)                                    \
131                 return strtoi_h(buf, &var) ?: (ssize_t) size;           \
132 } while (0)
133
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);
139
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);
149
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);
155
156 read_attribute(btree_avg_write_size);
157
158 read_attribute(bucket_quantiles_last_read);
159 read_attribute(bucket_quantiles_last_write);
160 read_attribute(bucket_quantiles_fragmentation);
161 read_attribute(bucket_quantiles_oldest_gen);
162
163 read_attribute(reserve_stats);
164 read_attribute(btree_cache_size);
165 read_attribute(compression_stats);
166 read_attribute(journal_debug);
167 read_attribute(journal_pins);
168 read_attribute(btree_updates);
169 read_attribute(dirty_btree_nodes);
170 read_attribute(btree_cache);
171 read_attribute(btree_key_cache);
172 read_attribute(btree_transactions);
173 read_attribute(stripes_heap);
174
175 read_attribute(internal_uuid);
176
177 read_attribute(has_data);
178 read_attribute(alloc_debug);
179 write_attribute(wake_allocator);
180
181 read_attribute(read_realloc_races);
182 read_attribute(extent_migrate_done);
183 read_attribute(extent_migrate_raced);
184
185 rw_attribute(journal_write_delay_ms);
186 rw_attribute(journal_reclaim_delay_ms);
187
188 rw_attribute(discard);
189 rw_attribute(cache_replacement_policy);
190 rw_attribute(label);
191
192 rw_attribute(copy_gc_enabled);
193 sysfs_pd_controller_attribute(copy_gc);
194
195 rw_attribute(rebalance_enabled);
196 sysfs_pd_controller_attribute(rebalance);
197 read_attribute(rebalance_work);
198 rw_attribute(promote_whole_extents);
199
200 read_attribute(new_stripes);
201
202 rw_attribute(pd_controllers_update_seconds);
203
204 read_attribute(io_timers_read);
205 read_attribute(io_timers_write);
206
207 #ifdef CONFIG_BCACHEFS_TESTS
208 write_attribute(perf_test);
209 #endif /* CONFIG_BCACHEFS_TESTS */
210
211 #define x(_name)                                                \
212         static struct attribute sysfs_time_stat_##_name =               \
213                 { .name = #_name, .mode = S_IRUGO };
214         BCH_TIME_STATS()
215 #undef x
216
217 static struct attribute sysfs_state_rw = {
218         .name = "state",
219         .mode = S_IRUGO
220 };
221
222 static size_t bch2_btree_cache_size(struct bch_fs *c)
223 {
224         size_t ret = 0;
225         struct btree *b;
226
227         mutex_lock(&c->btree_cache.lock);
228         list_for_each_entry(b, &c->btree_cache.live, list)
229                 ret += btree_bytes(c);
230
231         mutex_unlock(&c->btree_cache.lock);
232         return ret;
233 }
234
235 static size_t bch2_btree_avg_write_size(struct bch_fs *c)
236 {
237         u64 nr = atomic64_read(&c->btree_writes_nr);
238         u64 sectors = atomic64_read(&c->btree_writes_sectors);
239
240         return nr ? div64_u64(sectors, nr) : 0;
241 }
242
243 static int fs_alloc_debug_to_text(struct printbuf *out, struct bch_fs *c)
244 {
245         struct bch_fs_usage_online *fs_usage = bch2_fs_usage_read(c);
246
247         if (!fs_usage)
248                 return -ENOMEM;
249
250         bch2_fs_usage_to_text(out, c, fs_usage);
251
252         percpu_up_read(&c->mark_lock);
253
254         kfree(fs_usage);
255         return 0;
256 }
257
258 static int bch2_compression_stats_to_text(struct printbuf *out, struct bch_fs *c)
259 {
260         struct btree_trans trans;
261         struct btree_iter *iter;
262         struct bkey_s_c k;
263         u64 nr_uncompressed_extents = 0, uncompressed_sectors = 0,
264             nr_compressed_extents = 0,
265             compressed_sectors_compressed = 0,
266             compressed_sectors_uncompressed = 0;
267         int ret;
268
269         if (!test_bit(BCH_FS_STARTED, &c->flags))
270                 return -EPERM;
271
272         bch2_trans_init(&trans, c, 0, 0);
273
274         for_each_btree_key(&trans, iter, BTREE_ID_extents, POS_MIN, 0, k, ret)
275                 if (k.k->type == KEY_TYPE_extent) {
276                         struct bkey_s_c_extent e = bkey_s_c_to_extent(k);
277                         const union bch_extent_entry *entry;
278                         struct extent_ptr_decoded p;
279
280                         extent_for_each_ptr_decode(e, p, entry) {
281                                 if (!crc_is_compressed(p.crc)) {
282                                         nr_uncompressed_extents++;
283                                         uncompressed_sectors += e.k->size;
284                                 } else {
285                                         nr_compressed_extents++;
286                                         compressed_sectors_compressed +=
287                                                 p.crc.compressed_size;
288                                         compressed_sectors_uncompressed +=
289                                                 p.crc.uncompressed_size;
290                                 }
291
292                                 /* only looking at the first ptr */
293                                 break;
294                         }
295                 }
296
297         ret = bch2_trans_exit(&trans) ?: ret;
298         if (ret)
299                 return ret;
300
301         pr_buf(out,
302                "uncompressed data:\n"
303                "        nr extents:                     %llu\n"
304                "        size (bytes):                   %llu\n"
305                "compressed data:\n"
306                "        nr extents:                     %llu\n"
307                "        compressed size (bytes):        %llu\n"
308                "        uncompressed size (bytes):      %llu\n",
309                nr_uncompressed_extents,
310                uncompressed_sectors << 9,
311                nr_compressed_extents,
312                compressed_sectors_compressed << 9,
313                compressed_sectors_uncompressed << 9);
314         return 0;
315 }
316
317 SHOW(bch2_fs)
318 {
319         struct bch_fs *c = container_of(kobj, struct bch_fs, kobj);
320         struct printbuf out = _PBUF(buf, PAGE_SIZE);
321
322         sysfs_print(minor,                      c->minor);
323         sysfs_printf(internal_uuid, "%pU",      c->sb.uuid.b);
324
325         sysfs_print(journal_write_delay_ms,     c->journal.write_delay_ms);
326         sysfs_print(journal_reclaim_delay_ms,   c->journal.reclaim_delay_ms);
327
328         sysfs_print(block_size,                 block_bytes(c));
329         sysfs_print(btree_node_size,            btree_bytes(c));
330         sysfs_hprint(btree_cache_size,          bch2_btree_cache_size(c));
331         sysfs_hprint(btree_avg_write_size,      bch2_btree_avg_write_size(c));
332
333         sysfs_print(read_realloc_races,
334                     atomic_long_read(&c->read_realloc_races));
335         sysfs_print(extent_migrate_done,
336                     atomic_long_read(&c->extent_migrate_done));
337         sysfs_print(extent_migrate_raced,
338                     atomic_long_read(&c->extent_migrate_raced));
339
340         sysfs_printf(btree_gc_periodic, "%u",   (int) c->btree_gc_periodic);
341
342         sysfs_printf(copy_gc_enabled, "%i", c->copy_gc_enabled);
343
344         sysfs_print(pd_controllers_update_seconds,
345                     c->pd_controllers_update_seconds);
346
347         sysfs_printf(rebalance_enabled,         "%i", c->rebalance.enabled);
348         sysfs_pd_controller_show(rebalance,     &c->rebalance.pd); /* XXX */
349         sysfs_pd_controller_show(copy_gc,       &c->copygc_pd);
350
351         if (attr == &sysfs_rebalance_work) {
352                 bch2_rebalance_work_to_text(&out, c);
353                 return out.pos - buf;
354         }
355
356         sysfs_print(promote_whole_extents,      c->promote_whole_extents);
357
358         /* Debugging: */
359
360         if (attr == &sysfs_alloc_debug)
361                 return fs_alloc_debug_to_text(&out, c) ?: out.pos - buf;
362
363         if (attr == &sysfs_journal_debug) {
364                 bch2_journal_debug_to_text(&out, &c->journal);
365                 return out.pos - buf;
366         }
367
368         if (attr == &sysfs_journal_pins) {
369                 bch2_journal_pins_to_text(&out, &c->journal);
370                 return out.pos - buf;
371         }
372
373         if (attr == &sysfs_btree_updates) {
374                 bch2_btree_updates_to_text(&out, c);
375                 return out.pos - buf;
376         }
377
378         if (attr == &sysfs_dirty_btree_nodes) {
379                 bch2_dirty_btree_nodes_to_text(&out, c);
380                 return out.pos - buf;
381         }
382
383         if (attr == &sysfs_btree_cache) {
384                 bch2_btree_cache_to_text(&out, c);
385                 return out.pos - buf;
386         }
387
388         if (attr == &sysfs_btree_key_cache) {
389                 bch2_btree_key_cache_to_text(&out, &c->btree_key_cache);
390                 return out.pos - buf;
391         }
392
393         if (attr == &sysfs_btree_transactions) {
394                 bch2_btree_trans_to_text(&out, c);
395                 return out.pos - buf;
396         }
397
398         if (attr == &sysfs_stripes_heap) {
399                 bch2_stripes_heap_to_text(&out, c);
400                 return out.pos - buf;
401         }
402
403         if (attr == &sysfs_compression_stats) {
404                 bch2_compression_stats_to_text(&out, c);
405                 return out.pos - buf;
406         }
407
408         if (attr == &sysfs_new_stripes) {
409                 bch2_new_stripes_to_text(&out, c);
410                 return out.pos - buf;
411         }
412
413         if (attr == &sysfs_io_timers_read) {
414                 bch2_io_timers_to_text(&out, &c->io_clock[READ]);
415                 return out.pos - buf;
416         }
417         if (attr == &sysfs_io_timers_write) {
418                 bch2_io_timers_to_text(&out, &c->io_clock[WRITE]);
419                 return out.pos - buf;
420         }
421
422         return 0;
423 }
424
425 STORE(bch2_fs)
426 {
427         struct bch_fs *c = container_of(kobj, struct bch_fs, kobj);
428
429         sysfs_strtoul(journal_write_delay_ms, c->journal.write_delay_ms);
430         sysfs_strtoul(journal_reclaim_delay_ms, c->journal.reclaim_delay_ms);
431
432         if (attr == &sysfs_btree_gc_periodic) {
433                 ssize_t ret = strtoul_safe(buf, c->btree_gc_periodic)
434                         ?: (ssize_t) size;
435
436                 wake_up_process(c->gc_thread);
437                 return ret;
438         }
439
440         if (attr == &sysfs_copy_gc_enabled) {
441                 ssize_t ret = strtoul_safe(buf, c->copy_gc_enabled)
442                         ?: (ssize_t) size;
443
444                 if (c->copygc_thread)
445                         wake_up_process(c->copygc_thread);
446                 return ret;
447         }
448
449         if (attr == &sysfs_rebalance_enabled) {
450                 ssize_t ret = strtoul_safe(buf, c->rebalance.enabled)
451                         ?: (ssize_t) size;
452
453                 rebalance_wakeup(c);
454                 return ret;
455         }
456
457         sysfs_strtoul(pd_controllers_update_seconds,
458                       c->pd_controllers_update_seconds);
459         sysfs_pd_controller_store(rebalance,    &c->rebalance.pd);
460         sysfs_pd_controller_store(copy_gc,      &c->copygc_pd);
461
462         sysfs_strtoul(promote_whole_extents,    c->promote_whole_extents);
463
464         /* Debugging: */
465
466         if (!test_bit(BCH_FS_STARTED, &c->flags))
467                 return -EPERM;
468
469         /* Debugging: */
470
471         if (attr == &sysfs_trigger_journal_flush)
472                 bch2_journal_meta(&c->journal);
473
474         if (attr == &sysfs_trigger_btree_coalesce)
475                 bch2_coalesce(c);
476
477         if (attr == &sysfs_trigger_gc) {
478                 /*
479                  * Full gc is currently incompatible with btree key cache:
480                  */
481 #if 0
482                 down_read(&c->state_lock);
483                 bch2_gc(c, false, false);
484                 up_read(&c->state_lock);
485 #else
486                 bch2_gc_gens(c);
487 #endif
488         }
489
490         if (attr == &sysfs_prune_cache) {
491                 struct shrink_control sc;
492
493                 sc.gfp_mask = GFP_KERNEL;
494                 sc.nr_to_scan = strtoul_or_return(buf);
495                 c->btree_cache.shrink.scan_objects(&c->btree_cache.shrink, &sc);
496         }
497
498 #ifdef CONFIG_BCACHEFS_TESTS
499         if (attr == &sysfs_perf_test) {
500                 char *tmp = kstrdup(buf, GFP_KERNEL), *p = tmp;
501                 char *test              = strsep(&p, " \t\n");
502                 char *nr_str            = strsep(&p, " \t\n");
503                 char *threads_str       = strsep(&p, " \t\n");
504                 unsigned threads;
505                 u64 nr;
506                 int ret = -EINVAL;
507
508                 if (threads_str &&
509                     !(ret = kstrtouint(threads_str, 10, &threads)) &&
510                     !(ret = bch2_strtoull_h(nr_str, &nr)))
511                         ret = bch2_btree_perf_test(c, test, nr, threads);
512                 kfree(tmp);
513
514                 if (ret)
515                         size = ret;
516         }
517 #endif
518         return size;
519 }
520 SYSFS_OPS(bch2_fs);
521
522 struct attribute *bch2_fs_files[] = {
523         &sysfs_minor,
524         &sysfs_block_size,
525         &sysfs_btree_node_size,
526         &sysfs_btree_cache_size,
527         &sysfs_btree_avg_write_size,
528
529         &sysfs_journal_write_delay_ms,
530         &sysfs_journal_reclaim_delay_ms,
531
532         &sysfs_promote_whole_extents,
533
534         &sysfs_compression_stats,
535
536 #ifdef CONFIG_BCACHEFS_TESTS
537         &sysfs_perf_test,
538 #endif
539         NULL
540 };
541
542 /* internal dir - just a wrapper */
543
544 SHOW(bch2_fs_internal)
545 {
546         struct bch_fs *c = container_of(kobj, struct bch_fs, internal);
547         return bch2_fs_show(&c->kobj, attr, buf);
548 }
549
550 STORE(bch2_fs_internal)
551 {
552         struct bch_fs *c = container_of(kobj, struct bch_fs, internal);
553         return bch2_fs_store(&c->kobj, attr, buf, size);
554 }
555 SYSFS_OPS(bch2_fs_internal);
556
557 struct attribute *bch2_fs_internal_files[] = {
558         &sysfs_alloc_debug,
559         &sysfs_journal_debug,
560         &sysfs_journal_pins,
561         &sysfs_btree_updates,
562         &sysfs_dirty_btree_nodes,
563         &sysfs_btree_cache,
564         &sysfs_btree_key_cache,
565         &sysfs_btree_transactions,
566         &sysfs_stripes_heap,
567
568         &sysfs_read_realloc_races,
569         &sysfs_extent_migrate_done,
570         &sysfs_extent_migrate_raced,
571
572         &sysfs_trigger_journal_flush,
573         &sysfs_trigger_btree_coalesce,
574         &sysfs_trigger_gc,
575         &sysfs_prune_cache,
576
577         &sysfs_copy_gc_enabled,
578
579         &sysfs_rebalance_enabled,
580         &sysfs_rebalance_work,
581         sysfs_pd_controller_files(rebalance),
582         sysfs_pd_controller_files(copy_gc),
583
584         &sysfs_new_stripes,
585
586         &sysfs_io_timers_read,
587         &sysfs_io_timers_write,
588
589         &sysfs_internal_uuid,
590         NULL
591 };
592
593 /* options */
594
595 SHOW(bch2_fs_opts_dir)
596 {
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);
602
603         bch2_opt_to_text(&out, c, opt, v, OPT_SHOW_FULL_LIST);
604         pr_buf(&out, "\n");
605
606         return out.pos - buf;
607 }
608
609 STORE(bch2_fs_opts_dir)
610 {
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;
614         char *tmp;
615         u64 v;
616
617         tmp = kstrdup(buf, GFP_KERNEL);
618         if (!tmp)
619                 return -ENOMEM;
620
621         ret = bch2_opt_parse(c, opt, strim(tmp), &v);
622         kfree(tmp);
623
624         if (ret < 0)
625                 return ret;
626
627         ret = bch2_opt_check_may_set(c, id, v);
628         if (ret < 0)
629                 return ret;
630
631         if (opt->set_sb != SET_NO_SB_OPT) {
632                 mutex_lock(&c->sb_lock);
633                 opt->set_sb(c->disk_sb.sb, v);
634                 bch2_write_super(c);
635                 mutex_unlock(&c->sb_lock);
636         }
637
638         bch2_opt_set_by_id(&c->opts, id, v);
639
640         if ((id == Opt_background_target ||
641              id == Opt_background_compression) && v) {
642                 bch2_rebalance_add_work(c, S64_MAX);
643                 rebalance_wakeup(c);
644         }
645
646         return size;
647 }
648 SYSFS_OPS(bch2_fs_opts_dir);
649
650 struct attribute *bch2_fs_opts_dir_files[] = { NULL };
651
652 int bch2_opts_create_sysfs_files(struct kobject *kobj)
653 {
654         const struct bch_option *i;
655         int ret;
656
657         for (i = bch2_opt_table;
658              i < bch2_opt_table + bch2_opts_nr;
659              i++) {
660                 if (!(i->mode & (OPT_FORMAT|OPT_MOUNT|OPT_RUNTIME)))
661                         continue;
662
663                 ret = sysfs_create_file(kobj, &i->attr);
664                 if (ret)
665                         return ret;
666         }
667
668         return 0;
669 }
670
671 /* time stats */
672
673 SHOW(bch2_fs_time_stats)
674 {
675         struct bch_fs *c = container_of(kobj, struct bch_fs, time_stats);
676         struct printbuf out = _PBUF(buf, PAGE_SIZE);
677
678 #define x(name)                                                         \
679         if (attr == &sysfs_time_stat_##name) {                          \
680                 bch2_time_stats_to_text(&out, &c->times[BCH_TIME_##name]);\
681                 return out.pos - buf;                                   \
682         }
683         BCH_TIME_STATS()
684 #undef x
685
686         return 0;
687 }
688
689 STORE(bch2_fs_time_stats)
690 {
691         return size;
692 }
693 SYSFS_OPS(bch2_fs_time_stats);
694
695 struct attribute *bch2_fs_time_stats_files[] = {
696 #define x(name)                                         \
697         &sysfs_time_stat_##name,
698         BCH_TIME_STATS()
699 #undef x
700         NULL
701 };
702
703 typedef unsigned (bucket_map_fn)(struct bch_fs *, struct bch_dev *,
704                                  size_t, void *);
705
706 static unsigned bucket_last_io_fn(struct bch_fs *c, struct bch_dev *ca,
707                                   size_t b, void *private)
708 {
709         int rw = (private ? 1 : 0);
710
711         return atomic64_read(&c->io_clock[rw].now) - bucket(ca, b)->io_time[rw];
712 }
713
714 static unsigned bucket_sectors_used_fn(struct bch_fs *c, struct bch_dev *ca,
715                                        size_t b, void *private)
716 {
717         struct bucket *g = bucket(ca, b);
718         return bucket_sectors_used(g->mark);
719 }
720
721 static unsigned bucket_oldest_gen_fn(struct bch_fs *c, struct bch_dev *ca,
722                                      size_t b, void *private)
723 {
724         return bucket_gc_gen(bucket(ca, b));
725 }
726
727 static int unsigned_cmp(const void *_l, const void *_r)
728 {
729         const unsigned *l = _l;
730         const unsigned *r = _r;
731
732         return cmp_int(*l, *r);
733 }
734
735 static int quantiles_to_text(struct printbuf *out,
736                              struct bch_fs *c, struct bch_dev *ca,
737                              bucket_map_fn *fn, void *private)
738 {
739         size_t i, n;
740         /* Compute 31 quantiles */
741         unsigned q[31], *p;
742
743         down_read(&ca->bucket_lock);
744         n = ca->mi.nbuckets;
745
746         p = vzalloc(n * sizeof(unsigned));
747         if (!p) {
748                 up_read(&ca->bucket_lock);
749                 return -ENOMEM;
750         }
751
752         for (i = ca->mi.first_bucket; i < n; i++)
753                 p[i] = fn(c, ca, i, private);
754
755         sort(p, n, sizeof(unsigned), unsigned_cmp, NULL);
756         up_read(&ca->bucket_lock);
757
758         while (n &&
759                !p[n - 1])
760                 --n;
761
762         for (i = 0; i < ARRAY_SIZE(q); i++)
763                 q[i] = p[n * (i + 1) / (ARRAY_SIZE(q) + 1)];
764
765         vfree(p);
766
767         for (i = 0; i < ARRAY_SIZE(q); i++)
768                 pr_buf(out, "%u ", q[i]);
769         pr_buf(out, "\n");
770         return 0;
771 }
772
773 static void reserve_stats_to_text(struct printbuf *out, struct bch_dev *ca)
774 {
775         enum alloc_reserve i;
776
777         spin_lock(&ca->fs->freelist_lock);
778
779         pr_buf(out, "free_inc:\t%zu\t%zu\n",
780                fifo_used(&ca->free_inc),
781                ca->free_inc.size);
782
783         for (i = 0; i < RESERVE_NR; i++)
784                 pr_buf(out, "free[%u]:\t%zu\t%zu\n", i,
785                        fifo_used(&ca->free[i]),
786                        ca->free[i].size);
787
788         spin_unlock(&ca->fs->freelist_lock);
789 }
790
791 static void dev_alloc_debug_to_text(struct printbuf *out, struct bch_dev *ca)
792 {
793         struct bch_fs *c = ca->fs;
794         struct bch_dev_usage stats = bch2_dev_usage_read(ca);
795         unsigned i, nr[BCH_DATA_NR];
796
797         memset(nr, 0, sizeof(nr));
798
799         for (i = 0; i < ARRAY_SIZE(c->open_buckets); i++)
800                 nr[c->open_buckets[i].type]++;
801
802         pr_buf(out,
803                "\t\t buckets\t sectors      fragmented\n"
804                "capacity%16llu\n",
805                ca->mi.nbuckets - ca->mi.first_bucket);
806
807         for (i = 1; i < BCH_DATA_NR; i++)
808                 pr_buf(out, "%-8s%16llu%16llu%16llu\n",
809                        bch2_data_types[i], stats.d[i].buckets,
810                        stats.d[i].sectors, stats.d[i].fragmented);
811
812         pr_buf(out,
813                "ec\t%16llu\n"
814                "available%15llu\n"
815                "\n"
816                "free_inc\t\t%zu/%zu\n"
817                "free[RESERVE_MOVINGGC]\t%zu/%zu\n"
818                "free[RESERVE_NONE]\t%zu/%zu\n"
819                "freelist_wait\t\t%s\n"
820                "open buckets\t\t%u/%u (reserved %u)\n"
821                "open_buckets_wait\t%s\n"
822                "open_buckets_btree\t%u\n"
823                "open_buckets_user\t%u\n"
824                "btree reserve cache\t%u\n",
825                stats.buckets_ec,
826                __dev_buckets_available(ca, stats),
827                fifo_used(&ca->free_inc),                ca->free_inc.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                c->freelist_wait.list.first              ? "waiting" : "empty",
831                c->open_buckets_nr_free, OPEN_BUCKETS_COUNT,
832                BTREE_NODE_OPEN_BUCKET_RESERVE,
833                c->open_buckets_wait.list.first          ? "waiting" : "empty",
834                nr[BCH_DATA_btree],
835                nr[BCH_DATA_user],
836                c->btree_reserve_cache_nr);
837 }
838
839 static const char * const bch2_rw[] = {
840         "read",
841         "write",
842         NULL
843 };
844
845 static void dev_iodone_to_text(struct printbuf *out, struct bch_dev *ca)
846 {
847         int rw, i;
848
849         for (rw = 0; rw < 2; rw++) {
850                 pr_buf(out, "%s:\n", bch2_rw[rw]);
851
852                 for (i = 1; i < BCH_DATA_NR; i++)
853                         pr_buf(out, "%-12s:%12llu\n",
854                                bch2_data_types[i],
855                                percpu_u64_get(&ca->io_done->sectors[rw][i]) << 9);
856         }
857 }
858
859 SHOW(bch2_dev)
860 {
861         struct bch_dev *ca = container_of(kobj, struct bch_dev, kobj);
862         struct bch_fs *c = ca->fs;
863         struct printbuf out = _PBUF(buf, PAGE_SIZE);
864
865         sysfs_printf(uuid,              "%pU\n", ca->uuid.b);
866
867         sysfs_print(bucket_size,        bucket_bytes(ca));
868         sysfs_print(block_size,         block_bytes(c));
869         sysfs_print(first_bucket,       ca->mi.first_bucket);
870         sysfs_print(nbuckets,           ca->mi.nbuckets);
871         sysfs_print(durability,         ca->mi.durability);
872         sysfs_print(discard,            ca->mi.discard);
873
874         if (attr == &sysfs_label) {
875                 if (ca->mi.group) {
876                         mutex_lock(&c->sb_lock);
877                         bch2_disk_path_to_text(&out, &c->disk_sb,
878                                                ca->mi.group - 1);
879                         mutex_unlock(&c->sb_lock);
880                 }
881
882                 pr_buf(&out, "\n");
883                 return out.pos - buf;
884         }
885
886         if (attr == &sysfs_has_data) {
887                 bch2_flags_to_text(&out, bch2_data_types,
888                                    bch2_dev_has_data(c, ca));
889                 pr_buf(&out, "\n");
890                 return out.pos - buf;
891         }
892
893         if (attr == &sysfs_cache_replacement_policy) {
894                 bch2_string_opt_to_text(&out,
895                                         bch2_cache_replacement_policies,
896                                         ca->mi.replacement);
897                 pr_buf(&out, "\n");
898                 return out.pos - buf;
899         }
900
901         if (attr == &sysfs_state_rw) {
902                 bch2_string_opt_to_text(&out, bch2_member_states,
903                                         ca->mi.state);
904                 pr_buf(&out, "\n");
905                 return out.pos - buf;
906         }
907
908         if (attr == &sysfs_iodone) {
909                 dev_iodone_to_text(&out, ca);
910                 return out.pos - buf;
911         }
912
913         sysfs_print(io_latency_read,            atomic64_read(&ca->cur_latency[READ]));
914         sysfs_print(io_latency_write,           atomic64_read(&ca->cur_latency[WRITE]));
915
916         if (attr == &sysfs_io_latency_stats_read) {
917                 bch2_time_stats_to_text(&out, &ca->io_latency[READ]);
918                 return out.pos - buf;
919         }
920         if (attr == &sysfs_io_latency_stats_write) {
921                 bch2_time_stats_to_text(&out, &ca->io_latency[WRITE]);
922                 return out.pos - buf;
923         }
924
925         sysfs_printf(congested,                 "%u%%",
926                      clamp(atomic_read(&ca->congested), 0, CONGESTED_MAX)
927                      * 100 / CONGESTED_MAX);
928
929         if (attr == &sysfs_bucket_quantiles_last_read)
930                 return quantiles_to_text(&out, c, ca, bucket_last_io_fn, (void *) 0) ?: out.pos - buf;
931         if (attr == &sysfs_bucket_quantiles_last_write)
932                 return quantiles_to_text(&out, c, ca, bucket_last_io_fn, (void *) 1) ?: out.pos - buf;
933         if (attr == &sysfs_bucket_quantiles_fragmentation)
934                 return quantiles_to_text(&out, c, ca, bucket_sectors_used_fn, NULL)  ?: out.pos - buf;
935         if (attr == &sysfs_bucket_quantiles_oldest_gen)
936                 return quantiles_to_text(&out, c, ca, bucket_oldest_gen_fn, NULL)    ?: out.pos - buf;
937
938         if (attr == &sysfs_reserve_stats) {
939                 reserve_stats_to_text(&out, ca);
940                 return out.pos - buf;
941         }
942         if (attr == &sysfs_alloc_debug) {
943                 dev_alloc_debug_to_text(&out, ca);
944                 return out.pos - buf;
945         }
946
947         return 0;
948 }
949
950 STORE(bch2_dev)
951 {
952         struct bch_dev *ca = container_of(kobj, struct bch_dev, kobj);
953         struct bch_fs *c = ca->fs;
954         struct bch_member *mi;
955
956         if (attr == &sysfs_discard) {
957                 bool v = strtoul_or_return(buf);
958
959                 mutex_lock(&c->sb_lock);
960                 mi = &bch2_sb_get_members(c->disk_sb.sb)->members[ca->dev_idx];
961
962                 if (v != BCH_MEMBER_DISCARD(mi)) {
963                         SET_BCH_MEMBER_DISCARD(mi, v);
964                         bch2_write_super(c);
965                 }
966                 mutex_unlock(&c->sb_lock);
967         }
968
969         if (attr == &sysfs_cache_replacement_policy) {
970                 ssize_t v = __sysfs_match_string(bch2_cache_replacement_policies, -1, buf);
971
972                 if (v < 0)
973                         return v;
974
975                 mutex_lock(&c->sb_lock);
976                 mi = &bch2_sb_get_members(c->disk_sb.sb)->members[ca->dev_idx];
977
978                 if ((unsigned) v != BCH_MEMBER_REPLACEMENT(mi)) {
979                         SET_BCH_MEMBER_REPLACEMENT(mi, v);
980                         bch2_write_super(c);
981                 }
982                 mutex_unlock(&c->sb_lock);
983         }
984
985         if (attr == &sysfs_label) {
986                 char *tmp;
987                 int ret;
988
989                 tmp = kstrdup(buf, GFP_KERNEL);
990                 if (!tmp)
991                         return -ENOMEM;
992
993                 ret = bch2_dev_group_set(c, ca, strim(tmp));
994                 kfree(tmp);
995                 if (ret)
996                         return ret;
997         }
998
999         if (attr == &sysfs_wake_allocator)
1000                 bch2_wake_allocator(ca);
1001
1002         return size;
1003 }
1004 SYSFS_OPS(bch2_dev);
1005
1006 struct attribute *bch2_dev_files[] = {
1007         &sysfs_uuid,
1008         &sysfs_bucket_size,
1009         &sysfs_block_size,
1010         &sysfs_first_bucket,
1011         &sysfs_nbuckets,
1012         &sysfs_durability,
1013
1014         /* settings: */
1015         &sysfs_discard,
1016         &sysfs_cache_replacement_policy,
1017         &sysfs_state_rw,
1018         &sysfs_label,
1019
1020         &sysfs_has_data,
1021         &sysfs_iodone,
1022
1023         &sysfs_io_latency_read,
1024         &sysfs_io_latency_write,
1025         &sysfs_io_latency_stats_read,
1026         &sysfs_io_latency_stats_write,
1027         &sysfs_congested,
1028
1029         /* alloc info - other stats: */
1030         &sysfs_bucket_quantiles_last_read,
1031         &sysfs_bucket_quantiles_last_write,
1032         &sysfs_bucket_quantiles_fragmentation,
1033         &sysfs_bucket_quantiles_oldest_gen,
1034
1035         &sysfs_reserve_stats,
1036
1037         /* debug: */
1038         &sysfs_alloc_debug,
1039         &sysfs_wake_allocator,
1040         NULL
1041 };
1042
1043 #endif  /* _BCACHEFS_SYSFS_H_ */