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