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