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