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