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