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