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