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