]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/sysfs.c
Merge pull request #38 from jnsaff/patch-1
[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_key_cache);
169 read_attribute(btree_transactions);
170 read_attribute(stripes_heap);
171
172 read_attribute(internal_uuid);
173
174 read_attribute(has_data);
175 read_attribute(alloc_debug);
176 write_attribute(wake_allocator);
177
178 read_attribute(read_realloc_races);
179 read_attribute(extent_migrate_done);
180 read_attribute(extent_migrate_raced);
181
182 rw_attribute(journal_write_delay_ms);
183 rw_attribute(journal_reclaim_delay_ms);
184
185 rw_attribute(discard);
186 rw_attribute(cache_replacement_policy);
187 rw_attribute(label);
188
189 rw_attribute(copy_gc_enabled);
190 sysfs_pd_controller_attribute(copy_gc);
191
192 rw_attribute(rebalance_enabled);
193 sysfs_pd_controller_attribute(rebalance);
194 read_attribute(rebalance_work);
195 rw_attribute(promote_whole_extents);
196
197 read_attribute(new_stripes);
198
199 rw_attribute(pd_controllers_update_seconds);
200
201 read_attribute(meta_replicas_have);
202 read_attribute(data_replicas_have);
203
204 read_attribute(io_timers_read);
205 read_attribute(io_timers_write);
206
207 #ifdef CONFIG_BCACHEFS_TESTS
208 write_attribute(perf_test);
209 #endif /* CONFIG_BCACHEFS_TESTS */
210
211 #define BCH_DEBUG_PARAM(name, description)                              \
212         rw_attribute(name);
213
214         BCH_DEBUG_PARAMS()
215 #undef BCH_DEBUG_PARAM
216
217 #define x(_name)                                                \
218         static struct attribute sysfs_time_stat_##_name =               \
219                 { .name = #_name, .mode = S_IRUGO };
220         BCH_TIME_STATS()
221 #undef x
222
223 static struct attribute sysfs_state_rw = {
224         .name = "state",
225         .mode = S_IRUGO
226 };
227
228 static size_t bch2_btree_cache_size(struct bch_fs *c)
229 {
230         size_t ret = 0;
231         struct btree *b;
232
233         mutex_lock(&c->btree_cache.lock);
234         list_for_each_entry(b, &c->btree_cache.live, list)
235                 ret += btree_bytes(c);
236
237         mutex_unlock(&c->btree_cache.lock);
238         return ret;
239 }
240
241 static int fs_alloc_debug_to_text(struct printbuf *out, struct bch_fs *c)
242 {
243         struct bch_fs_usage *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 SHOW(bch2_fs)
316 {
317         struct bch_fs *c = container_of(kobj, struct bch_fs, kobj);
318         struct printbuf out = _PBUF(buf, PAGE_SIZE);
319
320         sysfs_print(minor,                      c->minor);
321         sysfs_printf(internal_uuid, "%pU",      c->sb.uuid.b);
322
323         sysfs_print(journal_write_delay_ms,     c->journal.write_delay_ms);
324         sysfs_print(journal_reclaim_delay_ms,   c->journal.reclaim_delay_ms);
325
326         sysfs_print(block_size,                 block_bytes(c));
327         sysfs_print(btree_node_size,            btree_bytes(c));
328         sysfs_hprint(btree_cache_size,          bch2_btree_cache_size(c));
329
330         sysfs_print(read_realloc_races,
331                     atomic_long_read(&c->read_realloc_races));
332         sysfs_print(extent_migrate_done,
333                     atomic_long_read(&c->extent_migrate_done));
334         sysfs_print(extent_migrate_raced,
335                     atomic_long_read(&c->extent_migrate_raced));
336
337         sysfs_printf(btree_gc_periodic, "%u",   (int) c->btree_gc_periodic);
338
339         sysfs_printf(copy_gc_enabled, "%i", c->copy_gc_enabled);
340
341         sysfs_print(pd_controllers_update_seconds,
342                     c->pd_controllers_update_seconds);
343
344         sysfs_printf(rebalance_enabled,         "%i", c->rebalance.enabled);
345         sysfs_pd_controller_show(rebalance,     &c->rebalance.pd); /* XXX */
346         sysfs_pd_controller_show(copy_gc,       &c->copygc_pd);
347
348         if (attr == &sysfs_rebalance_work) {
349                 bch2_rebalance_work_to_text(&out, c);
350                 return out.pos - buf;
351         }
352
353         sysfs_print(promote_whole_extents,      c->promote_whole_extents);
354
355         sysfs_printf(meta_replicas_have, "%i",  bch2_replicas_online(c, true));
356         sysfs_printf(data_replicas_have, "%i",  bch2_replicas_online(c, false));
357
358         /* Debugging: */
359
360         if (attr == &sysfs_alloc_debug)
361                 return fs_alloc_debug_to_text(&out, c) ?: out.pos - buf;
362
363         if (attr == &sysfs_journal_debug) {
364                 bch2_journal_debug_to_text(&out, &c->journal);
365                 return out.pos - buf;
366         }
367
368         if (attr == &sysfs_journal_pins) {
369                 bch2_journal_pins_to_text(&out, &c->journal);
370                 return out.pos - buf;
371         }
372
373         if (attr == &sysfs_btree_updates) {
374                 bch2_btree_updates_to_text(&out, c);
375                 return out.pos - buf;
376         }
377
378         if (attr == &sysfs_dirty_btree_nodes) {
379                 bch2_dirty_btree_nodes_to_text(&out, c);
380                 return out.pos - buf;
381         }
382
383         if (attr == &sysfs_btree_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 #define BCH_DEBUG_PARAM(name, description) sysfs_print(name, c->name);
418         BCH_DEBUG_PARAMS()
419 #undef BCH_DEBUG_PARAM
420
421         return 0;
422 }
423
424 STORE(bch2_fs)
425 {
426         struct bch_fs *c = container_of(kobj, struct bch_fs, kobj);
427
428         sysfs_strtoul(journal_write_delay_ms, c->journal.write_delay_ms);
429         sysfs_strtoul(journal_reclaim_delay_ms, c->journal.reclaim_delay_ms);
430
431         if (attr == &sysfs_btree_gc_periodic) {
432                 ssize_t ret = strtoul_safe(buf, c->btree_gc_periodic)
433                         ?: (ssize_t) size;
434
435                 wake_up_process(c->gc_thread);
436                 return ret;
437         }
438
439         if (attr == &sysfs_copy_gc_enabled) {
440                 ssize_t ret = strtoul_safe(buf, c->copy_gc_enabled)
441                         ?: (ssize_t) size;
442
443                 if (c->copygc_thread)
444                         wake_up_process(c->copygc_thread);
445                 return ret;
446         }
447
448         if (attr == &sysfs_rebalance_enabled) {
449                 ssize_t ret = strtoul_safe(buf, c->rebalance.enabled)
450                         ?: (ssize_t) size;
451
452                 rebalance_wakeup(c);
453                 return ret;
454         }
455
456         sysfs_strtoul(pd_controllers_update_seconds,
457                       c->pd_controllers_update_seconds);
458         sysfs_pd_controller_store(rebalance,    &c->rebalance.pd);
459         sysfs_pd_controller_store(copy_gc,      &c->copygc_pd);
460
461         sysfs_strtoul(promote_whole_extents,    c->promote_whole_extents);
462
463         /* Debugging: */
464
465 #define BCH_DEBUG_PARAM(name, description) sysfs_strtoul(name, c->name);
466         BCH_DEBUG_PARAMS()
467 #undef BCH_DEBUG_PARAM
468
469         if (!test_bit(BCH_FS_STARTED, &c->flags))
470                 return -EPERM;
471
472         /* Debugging: */
473
474         if (attr == &sysfs_trigger_journal_flush)
475                 bch2_journal_meta_async(&c->journal, NULL);
476
477         if (attr == &sysfs_trigger_btree_coalesce)
478                 bch2_coalesce(c);
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, NULL, 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                         bch2_btree_perf_test(c, test, nr, threads);
515                 else
516                         size = ret;
517                 kfree(tmp);
518         }
519 #endif
520         return size;
521 }
522 SYSFS_OPS(bch2_fs);
523
524 struct attribute *bch2_fs_files[] = {
525         &sysfs_minor,
526         &sysfs_block_size,
527         &sysfs_btree_node_size,
528         &sysfs_btree_cache_size,
529
530         &sysfs_meta_replicas_have,
531         &sysfs_data_replicas_have,
532
533         &sysfs_journal_write_delay_ms,
534         &sysfs_journal_reclaim_delay_ms,
535
536         &sysfs_promote_whole_extents,
537
538         &sysfs_compression_stats,
539
540 #ifdef CONFIG_BCACHEFS_TESTS
541         &sysfs_perf_test,
542 #endif
543         NULL
544 };
545
546 /* internal dir - just a wrapper */
547
548 SHOW(bch2_fs_internal)
549 {
550         struct bch_fs *c = container_of(kobj, struct bch_fs, internal);
551         return bch2_fs_show(&c->kobj, attr, buf);
552 }
553
554 STORE(bch2_fs_internal)
555 {
556         struct bch_fs *c = container_of(kobj, struct bch_fs, internal);
557         return bch2_fs_store(&c->kobj, attr, buf, size);
558 }
559 SYSFS_OPS(bch2_fs_internal);
560
561 struct attribute *bch2_fs_internal_files[] = {
562         &sysfs_alloc_debug,
563         &sysfs_journal_debug,
564         &sysfs_journal_pins,
565         &sysfs_btree_updates,
566         &sysfs_dirty_btree_nodes,
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_btree_coalesce,
577         &sysfs_trigger_gc,
578         &sysfs_prune_cache,
579
580         &sysfs_copy_gc_enabled,
581
582         &sysfs_rebalance_enabled,
583         &sysfs_rebalance_work,
584         sysfs_pd_controller_files(rebalance),
585         sysfs_pd_controller_files(copy_gc),
586
587         &sysfs_new_stripes,
588
589         &sysfs_io_timers_read,
590         &sysfs_io_timers_write,
591
592         &sysfs_internal_uuid,
593
594 #define BCH_DEBUG_PARAM(name, description) &sysfs_##name,
595         BCH_DEBUG_PARAMS()
596 #undef BCH_DEBUG_PARAM
597
598         NULL
599 };
600
601 /* options */
602
603 SHOW(bch2_fs_opts_dir)
604 {
605         struct printbuf out = _PBUF(buf, PAGE_SIZE);
606         struct bch_fs *c = container_of(kobj, struct bch_fs, opts_dir);
607         const struct bch_option *opt = container_of(attr, struct bch_option, attr);
608         int id = opt - bch2_opt_table;
609         u64 v = bch2_opt_get_by_id(&c->opts, id);
610
611         bch2_opt_to_text(&out, c, opt, v, OPT_SHOW_FULL_LIST);
612         pr_buf(&out, "\n");
613
614         return out.pos - buf;
615 }
616
617 STORE(bch2_fs_opts_dir)
618 {
619         struct bch_fs *c = container_of(kobj, struct bch_fs, opts_dir);
620         const struct bch_option *opt = container_of(attr, struct bch_option, attr);
621         int ret, id = opt - bch2_opt_table;
622         char *tmp;
623         u64 v;
624
625         tmp = kstrdup(buf, GFP_KERNEL);
626         if (!tmp)
627                 return -ENOMEM;
628
629         ret = bch2_opt_parse(c, opt, strim(tmp), &v);
630         kfree(tmp);
631
632         if (ret < 0)
633                 return ret;
634
635         ret = bch2_opt_check_may_set(c, id, v);
636         if (ret < 0)
637                 return ret;
638
639         if (opt->set_sb != SET_NO_SB_OPT) {
640                 mutex_lock(&c->sb_lock);
641                 opt->set_sb(c->disk_sb.sb, v);
642                 bch2_write_super(c);
643                 mutex_unlock(&c->sb_lock);
644         }
645
646         bch2_opt_set_by_id(&c->opts, id, v);
647
648         if ((id == Opt_background_target ||
649              id == Opt_background_compression) && v) {
650                 bch2_rebalance_add_work(c, S64_MAX);
651                 rebalance_wakeup(c);
652         }
653
654         return size;
655 }
656 SYSFS_OPS(bch2_fs_opts_dir);
657
658 struct attribute *bch2_fs_opts_dir_files[] = { NULL };
659
660 int bch2_opts_create_sysfs_files(struct kobject *kobj)
661 {
662         const struct bch_option *i;
663         int ret;
664
665         for (i = bch2_opt_table;
666              i < bch2_opt_table + bch2_opts_nr;
667              i++) {
668                 if (!(i->mode & (OPT_FORMAT|OPT_MOUNT|OPT_RUNTIME)))
669                         continue;
670
671                 ret = sysfs_create_file(kobj, &i->attr);
672                 if (ret)
673                         return ret;
674         }
675
676         return 0;
677 }
678
679 /* time stats */
680
681 SHOW(bch2_fs_time_stats)
682 {
683         struct bch_fs *c = container_of(kobj, struct bch_fs, time_stats);
684         struct printbuf out = _PBUF(buf, PAGE_SIZE);
685
686 #define x(name)                                                         \
687         if (attr == &sysfs_time_stat_##name) {                          \
688                 bch2_time_stats_to_text(&out, &c->times[BCH_TIME_##name]);\
689                 return out.pos - buf;                                   \
690         }
691         BCH_TIME_STATS()
692 #undef x
693
694         return 0;
695 }
696
697 STORE(bch2_fs_time_stats)
698 {
699         return size;
700 }
701 SYSFS_OPS(bch2_fs_time_stats);
702
703 struct attribute *bch2_fs_time_stats_files[] = {
704 #define x(name)                                         \
705         &sysfs_time_stat_##name,
706         BCH_TIME_STATS()
707 #undef x
708         NULL
709 };
710
711 typedef unsigned (bucket_map_fn)(struct bch_fs *, struct bch_dev *,
712                                  size_t, void *);
713
714 static unsigned bucket_last_io_fn(struct bch_fs *c, struct bch_dev *ca,
715                                   size_t b, void *private)
716 {
717         int rw = (private ? 1 : 0);
718
719         return bucket_last_io(c, bucket(ca, b), rw);
720 }
721
722 static unsigned bucket_sectors_used_fn(struct bch_fs *c, struct bch_dev *ca,
723                                        size_t b, void *private)
724 {
725         struct bucket *g = bucket(ca, b);
726         return bucket_sectors_used(g->mark);
727 }
728
729 static unsigned bucket_oldest_gen_fn(struct bch_fs *c, struct bch_dev *ca,
730                                      size_t b, void *private)
731 {
732         return bucket_gc_gen(ca, b);
733 }
734
735 static int unsigned_cmp(const void *_l, const void *_r)
736 {
737         const unsigned *l = _l;
738         const unsigned *r = _r;
739
740         return cmp_int(*l, *r);
741 }
742
743 static int quantiles_to_text(struct printbuf *out,
744                              struct bch_fs *c, struct bch_dev *ca,
745                              bucket_map_fn *fn, void *private)
746 {
747         size_t i, n;
748         /* Compute 31 quantiles */
749         unsigned q[31], *p;
750
751         down_read(&ca->bucket_lock);
752         n = ca->mi.nbuckets;
753
754         p = vzalloc(n * sizeof(unsigned));
755         if (!p) {
756                 up_read(&ca->bucket_lock);
757                 return -ENOMEM;
758         }
759
760         for (i = ca->mi.first_bucket; i < n; i++)
761                 p[i] = fn(c, ca, i, private);
762
763         sort(p, n, sizeof(unsigned), unsigned_cmp, NULL);
764         up_read(&ca->bucket_lock);
765
766         while (n &&
767                !p[n - 1])
768                 --n;
769
770         for (i = 0; i < ARRAY_SIZE(q); i++)
771                 q[i] = p[n * (i + 1) / (ARRAY_SIZE(q) + 1)];
772
773         vfree(p);
774
775         for (i = 0; i < ARRAY_SIZE(q); i++)
776                 pr_buf(out, "%u ", q[i]);
777         pr_buf(out, "\n");
778         return 0;
779 }
780
781 static void reserve_stats_to_text(struct printbuf *out, struct bch_dev *ca)
782 {
783         enum alloc_reserve i;
784
785         spin_lock(&ca->fs->freelist_lock);
786
787         pr_buf(out, "free_inc:\t%zu\t%zu\n",
788                fifo_used(&ca->free_inc),
789                ca->free_inc.size);
790
791         for (i = 0; i < RESERVE_NR; i++)
792                 pr_buf(out, "free[%u]:\t%zu\t%zu\n", i,
793                        fifo_used(&ca->free[i]),
794                        ca->free[i].size);
795
796         spin_unlock(&ca->fs->freelist_lock);
797 }
798
799 static void dev_alloc_debug_to_text(struct printbuf *out, struct bch_dev *ca)
800 {
801         struct bch_fs *c = ca->fs;
802         struct bch_dev_usage stats = bch2_dev_usage_read(ca);
803         unsigned i, nr[BCH_DATA_NR];
804
805         memset(nr, 0, sizeof(nr));
806
807         for (i = 0; i < ARRAY_SIZE(c->open_buckets); i++)
808                 nr[c->open_buckets[i].type]++;
809
810         pr_buf(out,
811                 "free_inc:               %zu/%zu\n"
812                 "free[RESERVE_BTREE]:    %zu/%zu\n"
813                 "free[RESERVE_MOVINGGC]: %zu/%zu\n"
814                 "free[RESERVE_NONE]:     %zu/%zu\n"
815                 "buckets:\n"
816                 "    capacity:           %llu\n"
817                 "    alloc:              %llu\n"
818                 "    sb:                 %llu\n"
819                 "    journal:            %llu\n"
820                 "    meta:               %llu\n"
821                 "    user:               %llu\n"
822                 "    cached:             %llu\n"
823                 "    erasure coded:      %llu\n"
824                 "    available:          %lli\n"
825                 "sectors:\n"
826                 "    sb:                 %llu\n"
827                 "    journal:            %llu\n"
828                 "    meta:               %llu\n"
829                 "    user:               %llu\n"
830                 "    cached:             %llu\n"
831                 "    erasure coded:      %llu\n"
832                 "    fragmented:         %llu\n"
833                 "    copygc threshold:   %llu\n"
834                 "freelist_wait:          %s\n"
835                 "open buckets:           %u/%u (reserved %u)\n"
836                 "open_buckets_wait:      %s\n"
837                 "open_buckets_btree:     %u\n"
838                 "open_buckets_user:      %u\n"
839                 "btree reserve cache:    %u\n",
840                 fifo_used(&ca->free_inc),               ca->free_inc.size,
841                 fifo_used(&ca->free[RESERVE_BTREE]),    ca->free[RESERVE_BTREE].size,
842                 fifo_used(&ca->free[RESERVE_MOVINGGC]), ca->free[RESERVE_MOVINGGC].size,
843                 fifo_used(&ca->free[RESERVE_NONE]),     ca->free[RESERVE_NONE].size,
844                 ca->mi.nbuckets - ca->mi.first_bucket,
845                 stats.buckets_alloc,
846                 stats.buckets[BCH_DATA_sb],
847                 stats.buckets[BCH_DATA_journal],
848                 stats.buckets[BCH_DATA_btree],
849                 stats.buckets[BCH_DATA_user],
850                 stats.buckets[BCH_DATA_cached],
851                 stats.buckets_ec,
852                 __dev_buckets_available(ca, stats),
853                 stats.sectors[BCH_DATA_sb],
854                 stats.sectors[BCH_DATA_journal],
855                 stats.sectors[BCH_DATA_btree],
856                 stats.sectors[BCH_DATA_user],
857                 stats.sectors[BCH_DATA_cached],
858                 stats.sectors_ec,
859                 stats.sectors_fragmented,
860                 c->copygc_threshold,
861                 c->freelist_wait.list.first             ? "waiting" : "empty",
862                 c->open_buckets_nr_free, OPEN_BUCKETS_COUNT,
863                 BTREE_NODE_OPEN_BUCKET_RESERVE,
864                 c->open_buckets_wait.list.first         ? "waiting" : "empty",
865                 nr[BCH_DATA_btree],
866                 nr[BCH_DATA_user],
867                 c->btree_reserve_cache_nr);
868 }
869
870 static const char * const bch2_rw[] = {
871         "read",
872         "write",
873         NULL
874 };
875
876 static void dev_iodone_to_text(struct printbuf *out, struct bch_dev *ca)
877 {
878         int rw, i;
879
880         for (rw = 0; rw < 2; rw++) {
881                 pr_buf(out, "%s:\n", bch2_rw[rw]);
882
883                 for (i = 1; i < BCH_DATA_NR; i++)
884                         pr_buf(out, "%-12s:%12llu\n",
885                                bch2_data_types[i],
886                                percpu_u64_get(&ca->io_done->sectors[rw][i]) << 9);
887         }
888 }
889
890 SHOW(bch2_dev)
891 {
892         struct bch_dev *ca = container_of(kobj, struct bch_dev, kobj);
893         struct bch_fs *c = ca->fs;
894         struct printbuf out = _PBUF(buf, PAGE_SIZE);
895
896         sysfs_printf(uuid,              "%pU\n", ca->uuid.b);
897
898         sysfs_print(bucket_size,        bucket_bytes(ca));
899         sysfs_print(block_size,         block_bytes(c));
900         sysfs_print(first_bucket,       ca->mi.first_bucket);
901         sysfs_print(nbuckets,           ca->mi.nbuckets);
902         sysfs_print(durability,         ca->mi.durability);
903         sysfs_print(discard,            ca->mi.discard);
904
905         if (attr == &sysfs_label) {
906                 if (ca->mi.group) {
907                         mutex_lock(&c->sb_lock);
908                         bch2_disk_path_to_text(&out, &c->disk_sb,
909                                                ca->mi.group - 1);
910                         mutex_unlock(&c->sb_lock);
911                 }
912
913                 pr_buf(&out, "\n");
914                 return out.pos - buf;
915         }
916
917         if (attr == &sysfs_has_data) {
918                 bch2_flags_to_text(&out, bch2_data_types,
919                                    bch2_dev_has_data(c, ca));
920                 pr_buf(&out, "\n");
921                 return out.pos - buf;
922         }
923
924         if (attr == &sysfs_cache_replacement_policy) {
925                 bch2_string_opt_to_text(&out,
926                                         bch2_cache_replacement_policies,
927                                         ca->mi.replacement);
928                 pr_buf(&out, "\n");
929                 return out.pos - buf;
930         }
931
932         if (attr == &sysfs_state_rw) {
933                 bch2_string_opt_to_text(&out, bch2_dev_state,
934                                         ca->mi.state);
935                 pr_buf(&out, "\n");
936                 return out.pos - buf;
937         }
938
939         if (attr == &sysfs_iodone) {
940                 dev_iodone_to_text(&out, ca);
941                 return out.pos - buf;
942         }
943
944         sysfs_print(io_latency_read,            atomic64_read(&ca->cur_latency[READ]));
945         sysfs_print(io_latency_write,           atomic64_read(&ca->cur_latency[WRITE]));
946
947         if (attr == &sysfs_io_latency_stats_read) {
948                 bch2_time_stats_to_text(&out, &ca->io_latency[READ]);
949                 return out.pos - buf;
950         }
951         if (attr == &sysfs_io_latency_stats_write) {
952                 bch2_time_stats_to_text(&out, &ca->io_latency[WRITE]);
953                 return out.pos - buf;
954         }
955
956         sysfs_printf(congested,                 "%u%%",
957                      clamp(atomic_read(&ca->congested), 0, CONGESTED_MAX)
958                      * 100 / CONGESTED_MAX);
959
960         if (attr == &sysfs_bucket_quantiles_last_read)
961                 return quantiles_to_text(&out, c, ca, bucket_last_io_fn, (void *) 0) ?: out.pos - buf;
962         if (attr == &sysfs_bucket_quantiles_last_write)
963                 return quantiles_to_text(&out, c, ca, bucket_last_io_fn, (void *) 1) ?: out.pos - buf;
964         if (attr == &sysfs_bucket_quantiles_fragmentation)
965                 return quantiles_to_text(&out, c, ca, bucket_sectors_used_fn, NULL)  ?: out.pos - buf;
966         if (attr == &sysfs_bucket_quantiles_oldest_gen)
967                 return quantiles_to_text(&out, c, ca, bucket_oldest_gen_fn, NULL)    ?: out.pos - buf;
968
969         if (attr == &sysfs_reserve_stats) {
970                 reserve_stats_to_text(&out, ca);
971                 return out.pos - buf;
972         }
973         if (attr == &sysfs_alloc_debug) {
974                 dev_alloc_debug_to_text(&out, ca);
975                 return out.pos - buf;
976         }
977
978         return 0;
979 }
980
981 STORE(bch2_dev)
982 {
983         struct bch_dev *ca = container_of(kobj, struct bch_dev, kobj);
984         struct bch_fs *c = ca->fs;
985         struct bch_member *mi;
986
987         if (attr == &sysfs_discard) {
988                 bool v = strtoul_or_return(buf);
989
990                 mutex_lock(&c->sb_lock);
991                 mi = &bch2_sb_get_members(c->disk_sb.sb)->members[ca->dev_idx];
992
993                 if (v != BCH_MEMBER_DISCARD(mi)) {
994                         SET_BCH_MEMBER_DISCARD(mi, v);
995                         bch2_write_super(c);
996                 }
997                 mutex_unlock(&c->sb_lock);
998         }
999
1000         if (attr == &sysfs_cache_replacement_policy) {
1001                 ssize_t v = __sysfs_match_string(bch2_cache_replacement_policies, -1, buf);
1002
1003                 if (v < 0)
1004                         return v;
1005
1006                 mutex_lock(&c->sb_lock);
1007                 mi = &bch2_sb_get_members(c->disk_sb.sb)->members[ca->dev_idx];
1008
1009                 if ((unsigned) v != BCH_MEMBER_REPLACEMENT(mi)) {
1010                         SET_BCH_MEMBER_REPLACEMENT(mi, v);
1011                         bch2_write_super(c);
1012                 }
1013                 mutex_unlock(&c->sb_lock);
1014         }
1015
1016         if (attr == &sysfs_label) {
1017                 char *tmp;
1018                 int ret;
1019
1020                 tmp = kstrdup(buf, GFP_KERNEL);
1021                 if (!tmp)
1022                         return -ENOMEM;
1023
1024                 ret = bch2_dev_group_set(c, ca, strim(tmp));
1025                 kfree(tmp);
1026                 if (ret)
1027                         return ret;
1028         }
1029
1030         if (attr == &sysfs_wake_allocator)
1031                 bch2_wake_allocator(ca);
1032
1033         return size;
1034 }
1035 SYSFS_OPS(bch2_dev);
1036
1037 struct attribute *bch2_dev_files[] = {
1038         &sysfs_uuid,
1039         &sysfs_bucket_size,
1040         &sysfs_block_size,
1041         &sysfs_first_bucket,
1042         &sysfs_nbuckets,
1043         &sysfs_durability,
1044
1045         /* settings: */
1046         &sysfs_discard,
1047         &sysfs_cache_replacement_policy,
1048         &sysfs_state_rw,
1049         &sysfs_label,
1050
1051         &sysfs_has_data,
1052         &sysfs_iodone,
1053
1054         &sysfs_io_latency_read,
1055         &sysfs_io_latency_write,
1056         &sysfs_io_latency_stats_read,
1057         &sysfs_io_latency_stats_write,
1058         &sysfs_congested,
1059
1060         /* alloc info - other stats: */
1061         &sysfs_bucket_quantiles_last_read,
1062         &sysfs_bucket_quantiles_last_write,
1063         &sysfs_bucket_quantiles_fragmentation,
1064         &sysfs_bucket_quantiles_oldest_gen,
1065
1066         &sysfs_reserve_stats,
1067
1068         /* debug: */
1069         &sysfs_alloc_debug,
1070         &sysfs_wake_allocator,
1071         NULL
1072 };
1073
1074 #endif  /* _BCACHEFS_SYSFS_H_ */