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