]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/sysfs.c
Update bcachefs sources to da7fefde29 bcachefs: shim for userspace raid library
[bcachefs-tools-debian] / libbcachefs / sysfs.c
1 /*
2  * bcache sysfs interfaces
3  *
4  * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com>
5  * Copyright 2012 Google, Inc.
6  */
7
8 #ifndef NO_BCACHEFS_SYSFS
9
10 #include "bcachefs.h"
11 #include "alloc_background.h"
12 #include "sysfs.h"
13 #include "btree_cache.h"
14 #include "btree_io.h"
15 #include "btree_iter.h"
16 #include "btree_update.h"
17 #include "btree_update_interior.h"
18 #include "btree_gc.h"
19 #include "buckets.h"
20 #include "disk_groups.h"
21 #include "ec.h"
22 #include "inode.h"
23 #include "journal.h"
24 #include "keylist.h"
25 #include "move.h"
26 #include "opts.h"
27 #include "rebalance.h"
28 #include "replicas.h"
29 #include "super-io.h"
30 #include "tests.h"
31
32 #include <linux/blkdev.h>
33 #include <linux/sort.h>
34 #include <linux/sched/clock.h>
35
36 #include "util.h"
37
38 #define SYSFS_OPS(type)                                                 \
39 struct sysfs_ops type ## _sysfs_ops = {                                 \
40         .show   = type ## _show,                                        \
41         .store  = type ## _store                                        \
42 }
43
44 #define SHOW(fn)                                                        \
45 static ssize_t fn ## _show(struct kobject *kobj, struct attribute *attr,\
46                            char *buf)                                   \
47
48 #define STORE(fn)                                                       \
49 static ssize_t fn ## _store(struct kobject *kobj, struct attribute *attr,\
50                             const char *buf, size_t size)               \
51
52 #define __sysfs_attribute(_name, _mode)                                 \
53         static struct attribute sysfs_##_name =                         \
54                 { .name = #_name, .mode = _mode }
55
56 #define write_attribute(n)      __sysfs_attribute(n, S_IWUSR)
57 #define read_attribute(n)       __sysfs_attribute(n, S_IRUGO)
58 #define rw_attribute(n)         __sysfs_attribute(n, S_IRUGO|S_IWUSR)
59
60 #define sysfs_printf(file, fmt, ...)                                    \
61 do {                                                                    \
62         if (attr == &sysfs_ ## file)                                    \
63                 return scnprintf(buf, PAGE_SIZE, fmt "\n", __VA_ARGS__);\
64 } while (0)
65
66 #define sysfs_print(file, var)                                          \
67 do {                                                                    \
68         if (attr == &sysfs_ ## file)                                    \
69                 return snprint(buf, PAGE_SIZE, var);                    \
70 } while (0)
71
72 #define sysfs_hprint(file, val)                                         \
73 do {                                                                    \
74         if (attr == &sysfs_ ## file) {                                  \
75                 ssize_t ret = bch2_hprint(buf, val);                    \
76                 strcat(buf, "\n");                                      \
77                 return ret + 1;                                         \
78         }                                                               \
79 } while (0)
80
81 #define var_printf(_var, fmt)   sysfs_printf(_var, fmt, var(_var))
82 #define var_print(_var)         sysfs_print(_var, var(_var))
83 #define var_hprint(_var)        sysfs_hprint(_var, var(_var))
84
85 #define sysfs_strtoul(file, var)                                        \
86 do {                                                                    \
87         if (attr == &sysfs_ ## file)                                    \
88                 return strtoul_safe(buf, var) ?: (ssize_t) size;        \
89 } while (0)
90
91 #define sysfs_strtoul_clamp(file, var, min, max)                        \
92 do {                                                                    \
93         if (attr == &sysfs_ ## file)                                    \
94                 return strtoul_safe_clamp(buf, var, min, max)           \
95                         ?: (ssize_t) size;                              \
96 } while (0)
97
98 #define strtoul_or_return(cp)                                           \
99 ({                                                                      \
100         unsigned long _v;                                               \
101         int _r = kstrtoul(cp, 10, &_v);                                 \
102         if (_r)                                                         \
103                 return _r;                                              \
104         _v;                                                             \
105 })
106
107 #define strtoul_restrict_or_return(cp, min, max)                        \
108 ({                                                                      \
109         unsigned long __v = 0;                                          \
110         int _r = strtoul_safe_restrict(cp, __v, min, max);              \
111         if (_r)                                                         \
112                 return _r;                                              \
113         __v;                                                            \
114 })
115
116 #define strtoi_h_or_return(cp)                                          \
117 ({                                                                      \
118         u64 _v;                                                         \
119         int _r = strtoi_h(cp, &_v);                                     \
120         if (_r)                                                         \
121                 return _r;                                              \
122         _v;                                                             \
123 })
124
125 #define sysfs_hatoi(file, var)                                          \
126 do {                                                                    \
127         if (attr == &sysfs_ ## file)                                    \
128                 return strtoi_h(buf, &var) ?: (ssize_t) size;           \
129 } while (0)
130
131 write_attribute(trigger_journal_flush);
132 write_attribute(trigger_btree_coalesce);
133 write_attribute(trigger_gc);
134 write_attribute(prune_cache);
135 rw_attribute(btree_gc_periodic);
136
137 read_attribute(uuid);
138 read_attribute(minor);
139 read_attribute(bucket_size);
140 read_attribute(block_size);
141 read_attribute(btree_node_size);
142 read_attribute(first_bucket);
143 read_attribute(nbuckets);
144 read_attribute(durability);
145 read_attribute(iodone);
146
147 read_attribute(io_latency_read);
148 read_attribute(io_latency_write);
149 read_attribute(io_latency_stats_read);
150 read_attribute(io_latency_stats_write);
151 read_attribute(congested);
152
153 read_attribute(bucket_quantiles_last_read);
154 read_attribute(bucket_quantiles_last_write);
155 read_attribute(bucket_quantiles_fragmentation);
156 read_attribute(bucket_quantiles_oldest_gen);
157
158 read_attribute(reserve_stats);
159 read_attribute(btree_cache_size);
160 read_attribute(compression_stats);
161 read_attribute(journal_debug);
162 read_attribute(journal_pins);
163 read_attribute(btree_updates);
164 read_attribute(dirty_btree_nodes);
165
166 read_attribute(internal_uuid);
167
168 read_attribute(has_data);
169 read_attribute(alloc_debug);
170 write_attribute(wake_allocator);
171
172 read_attribute(read_realloc_races);
173 read_attribute(extent_migrate_done);
174 read_attribute(extent_migrate_raced);
175
176 rw_attribute(journal_write_delay_ms);
177 rw_attribute(journal_reclaim_delay_ms);
178
179 rw_attribute(discard);
180 rw_attribute(cache_replacement_policy);
181 rw_attribute(label);
182
183 rw_attribute(copy_gc_enabled);
184 sysfs_pd_controller_attribute(copy_gc);
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 rw_attribute(pd_controllers_update_seconds);
194
195 read_attribute(meta_replicas_have);
196 read_attribute(data_replicas_have);
197
198 #ifdef CONFIG_BCACHEFS_TESTS
199 write_attribute(perf_test);
200 #endif /* CONFIG_BCACHEFS_TESTS */
201
202 #define BCH_DEBUG_PARAM(name, description)                              \
203         rw_attribute(name);
204
205         BCH_DEBUG_PARAMS()
206 #undef BCH_DEBUG_PARAM
207
208 #define x(_name)                                                \
209         static struct attribute sysfs_time_stat_##_name =               \
210                 { .name = #_name, .mode = S_IRUGO };
211         BCH_TIME_STATS()
212 #undef x
213
214 static struct attribute sysfs_state_rw = {
215         .name = "state",
216         .mode = S_IRUGO
217 };
218
219 static size_t bch2_btree_cache_size(struct bch_fs *c)
220 {
221         size_t ret = 0;
222         struct btree *b;
223
224         mutex_lock(&c->btree_cache.lock);
225         list_for_each_entry(b, &c->btree_cache.live, list)
226                 ret += btree_bytes(c);
227
228         mutex_unlock(&c->btree_cache.lock);
229         return ret;
230 }
231
232 static ssize_t show_fs_alloc_debug(struct bch_fs *c, char *buf)
233 {
234         struct printbuf out = _PBUF(buf, PAGE_SIZE);
235         struct bch_fs_usage stats = bch2_fs_usage_read(c);
236         unsigned replicas, type;
237
238         pr_buf(&out, "capacity:\t\t%llu\n", c->capacity);
239
240         for (replicas = 0; replicas < ARRAY_SIZE(stats.replicas); replicas++) {
241                 pr_buf(&out, "%u replicas:\n", replicas + 1);
242
243                 for (type = BCH_DATA_SB; type < BCH_DATA_NR; type++)
244                         pr_buf(&out, "\t%s:\t\t%llu\n",
245                                bch2_data_types[type],
246                                stats.replicas[replicas].data[type]);
247                 pr_buf(&out, "\terasure coded:\t%llu\n",
248                        stats.replicas[replicas].ec_data);
249                 pr_buf(&out, "\treserved:\t%llu\n",
250                        stats.replicas[replicas].persistent_reserved);
251         }
252
253         pr_buf(&out, "bucket usage\n");
254
255         for (type = BCH_DATA_SB; type < BCH_DATA_NR; type++)
256                 pr_buf(&out, "\t%s:\t\t%llu\n",
257                        bch2_data_types[type],
258                        stats.buckets[type]);
259
260         pr_buf(&out, "online reserved:\t%llu\n",
261                stats.online_reserved);
262
263         return out.pos - buf;
264 }
265
266 static ssize_t bch2_compression_stats(struct bch_fs *c, char *buf)
267 {
268         struct btree_iter iter;
269         struct bkey_s_c k;
270         u64 nr_uncompressed_extents = 0, uncompressed_sectors = 0,
271             nr_compressed_extents = 0,
272             compressed_sectors_compressed = 0,
273             compressed_sectors_uncompressed = 0;
274
275         if (!bch2_fs_running(c))
276                 return -EPERM;
277
278         for_each_btree_key(&iter, c, BTREE_ID_EXTENTS, POS_MIN, 0, k)
279                 if (k.k->type == BCH_EXTENT) {
280                         struct bkey_s_c_extent e = bkey_s_c_to_extent(k);
281                         const union bch_extent_entry *entry;
282                         struct extent_ptr_decoded p;
283
284                         extent_for_each_ptr_decode(e, p, entry) {
285                                 if (p.crc.compression_type == BCH_COMPRESSION_NONE) {
286                                         nr_uncompressed_extents++;
287                                         uncompressed_sectors += e.k->size;
288                                 } else {
289                                         nr_compressed_extents++;
290                                         compressed_sectors_compressed +=
291                                                 p.crc.compressed_size;
292                                         compressed_sectors_uncompressed +=
293                                                 p.crc.uncompressed_size;
294                                 }
295
296                                 /* only looking at the first ptr */
297                                 break;
298                         }
299                 }
300         bch2_btree_iter_unlock(&iter);
301
302         return scnprintf(buf, PAGE_SIZE,
303                         "uncompressed data:\n"
304                         "       nr extents:                     %llu\n"
305                         "       size (bytes):                   %llu\n"
306                         "compressed data:\n"
307                         "       nr extents:                     %llu\n"
308                         "       compressed size (bytes):        %llu\n"
309                         "       uncompressed size (bytes):      %llu\n",
310                         nr_uncompressed_extents,
311                         uncompressed_sectors << 9,
312                         nr_compressed_extents,
313                         compressed_sectors_compressed << 9,
314                         compressed_sectors_uncompressed << 9);
315 }
316
317 static ssize_t bch2_new_stripes(struct bch_fs *c, char *buf)
318 {
319         char *out = buf, *end = buf + PAGE_SIZE;
320         struct ec_stripe_head *h;
321         struct ec_stripe_new *s;
322
323         mutex_lock(&c->ec_new_stripe_lock);
324         list_for_each_entry(h, &c->ec_new_stripe_list, list) {
325                 out += scnprintf(out, end - out,
326                                  "target %u algo %u redundancy %u:\n",
327                                  h->target, h->algo, h->redundancy);
328
329                 if (h->s)
330                         out += scnprintf(out, end - out,
331                                          "\tpending: blocks %u allocated %u\n",
332                                          h->s->blocks.nr,
333                                          bitmap_weight(h->s->blocks_allocated,
334                                                        h->s->blocks.nr));
335
336                 mutex_lock(&h->lock);
337                 list_for_each_entry(s, &h->stripes, list)
338                         out += scnprintf(out, end - out,
339                                          "\tin flight: blocks %u allocated %u pin %u\n",
340                                          s->blocks.nr,
341                                          bitmap_weight(s->blocks_allocated,
342                                                        s->blocks.nr),
343                                          atomic_read(&s->pin));
344                 mutex_unlock(&h->lock);
345
346         }
347         mutex_unlock(&c->ec_new_stripe_lock);
348
349         return out - buf;
350 }
351
352 SHOW(bch2_fs)
353 {
354         struct bch_fs *c = container_of(kobj, struct bch_fs, kobj);
355
356         sysfs_print(minor,                      c->minor);
357         sysfs_printf(internal_uuid, "%pU",      c->sb.uuid.b);
358
359         sysfs_print(journal_write_delay_ms,     c->journal.write_delay_ms);
360         sysfs_print(journal_reclaim_delay_ms,   c->journal.reclaim_delay_ms);
361
362         sysfs_print(block_size,                 block_bytes(c));
363         sysfs_print(btree_node_size,            btree_bytes(c));
364         sysfs_hprint(btree_cache_size,          bch2_btree_cache_size(c));
365
366         sysfs_print(read_realloc_races,
367                     atomic_long_read(&c->read_realloc_races));
368         sysfs_print(extent_migrate_done,
369                     atomic_long_read(&c->extent_migrate_done));
370         sysfs_print(extent_migrate_raced,
371                     atomic_long_read(&c->extent_migrate_raced));
372
373         sysfs_printf(btree_gc_periodic, "%u",   (int) c->btree_gc_periodic);
374
375         sysfs_printf(copy_gc_enabled, "%i", c->copy_gc_enabled);
376
377         sysfs_print(pd_controllers_update_seconds,
378                     c->pd_controllers_update_seconds);
379
380         sysfs_printf(rebalance_enabled,         "%i", c->rebalance.enabled);
381         sysfs_pd_controller_show(rebalance,     &c->rebalance.pd); /* XXX */
382
383         if (attr == &sysfs_rebalance_work)
384                 return bch2_rebalance_work_show(c, buf);
385
386         sysfs_print(promote_whole_extents,      c->promote_whole_extents);
387
388         sysfs_printf(meta_replicas_have, "%i",  bch2_replicas_online(c, true));
389         sysfs_printf(data_replicas_have, "%i",  bch2_replicas_online(c, false));
390
391         /* Debugging: */
392
393         if (attr == &sysfs_alloc_debug)
394                 return show_fs_alloc_debug(c, buf);
395
396         if (attr == &sysfs_journal_debug)
397                 return bch2_journal_print_debug(&c->journal, buf);
398
399         if (attr == &sysfs_journal_pins)
400                 return bch2_journal_print_pins(&c->journal, buf);
401
402         if (attr == &sysfs_btree_updates)
403                 return bch2_btree_updates_print(c, buf);
404
405         if (attr == &sysfs_dirty_btree_nodes)
406                 return bch2_dirty_btree_nodes_print(c, buf);
407
408         if (attr == &sysfs_compression_stats)
409                 return bch2_compression_stats(c, buf);
410
411         if (attr == &sysfs_new_stripes)
412                 return bch2_new_stripes(c, buf);
413
414 #define BCH_DEBUG_PARAM(name, description) sysfs_print(name, c->name);
415         BCH_DEBUG_PARAMS()
416 #undef BCH_DEBUG_PARAM
417
418         return 0;
419 }
420
421 STORE(__bch2_fs)
422 {
423         struct bch_fs *c = container_of(kobj, struct bch_fs, kobj);
424
425         sysfs_strtoul(journal_write_delay_ms, c->journal.write_delay_ms);
426         sysfs_strtoul(journal_reclaim_delay_ms, c->journal.reclaim_delay_ms);
427
428         if (attr == &sysfs_btree_gc_periodic) {
429                 ssize_t ret = strtoul_safe(buf, c->btree_gc_periodic)
430                         ?: (ssize_t) size;
431
432                 wake_up_process(c->gc_thread);
433                 return ret;
434         }
435
436         if (attr == &sysfs_copy_gc_enabled) {
437                 struct bch_dev *ca;
438                 unsigned i;
439                 ssize_t ret = strtoul_safe(buf, c->copy_gc_enabled)
440                         ?: (ssize_t) size;
441
442                 for_each_member_device(ca, c, i)
443                         if (ca->copygc_thread)
444                                 wake_up_process(ca->copygc_thread);
445                 return ret;
446         }
447
448         if (attr == &sysfs_rebalance_enabled) {
449                 ssize_t ret = strtoul_safe(buf, c->rebalance.enabled)
450                         ?: (ssize_t) size;
451
452                 rebalance_wakeup(c);
453                 return ret;
454         }
455
456         sysfs_strtoul(pd_controllers_update_seconds,
457                       c->pd_controllers_update_seconds);
458         sysfs_pd_controller_store(rebalance,    &c->rebalance.pd);
459
460         sysfs_strtoul(promote_whole_extents,    c->promote_whole_extents);
461
462         /* Debugging: */
463
464 #define BCH_DEBUG_PARAM(name, description) sysfs_strtoul(name, c->name);
465         BCH_DEBUG_PARAMS()
466 #undef BCH_DEBUG_PARAM
467
468         if (!bch2_fs_running(c))
469                 return -EPERM;
470
471         /* Debugging: */
472
473         if (attr == &sysfs_trigger_journal_flush)
474                 bch2_journal_meta_async(&c->journal, NULL);
475
476         if (attr == &sysfs_trigger_btree_coalesce)
477                 bch2_coalesce(c);
478
479         if (attr == &sysfs_trigger_gc)
480                 bch2_gc(c, NULL, false);
481
482         if (attr == &sysfs_prune_cache) {
483                 struct shrink_control sc;
484
485                 sc.gfp_mask = GFP_KERNEL;
486                 sc.nr_to_scan = strtoul_or_return(buf);
487                 c->btree_cache.shrink.scan_objects(&c->btree_cache.shrink, &sc);
488         }
489 #ifdef CONFIG_BCACHEFS_TESTS
490         if (attr == &sysfs_perf_test) {
491                 char *tmp = kstrdup(buf, GFP_KERNEL), *p = tmp;
492                 char *test              = strsep(&p, " \t\n");
493                 char *nr_str            = strsep(&p, " \t\n");
494                 char *threads_str       = strsep(&p, " \t\n");
495                 unsigned threads;
496                 u64 nr;
497                 int ret = -EINVAL;
498
499                 if (threads_str &&
500                     !(ret = kstrtouint(threads_str, 10, &threads)) &&
501                     !(ret = bch2_strtoull_h(nr_str, &nr)))
502                         bch2_btree_perf_test(c, test, nr, threads);
503                 else
504                         size = ret;
505                 kfree(tmp);
506         }
507 #endif
508         return size;
509 }
510
511 STORE(bch2_fs)
512 {
513         struct bch_fs *c = container_of(kobj, struct bch_fs, kobj);
514
515         mutex_lock(&c->state_lock);
516         size = __bch2_fs_store(kobj, attr, buf, size);
517         mutex_unlock(&c->state_lock);
518
519         return size;
520 }
521 SYSFS_OPS(bch2_fs);
522
523 struct attribute *bch2_fs_files[] = {
524         &sysfs_minor,
525         &sysfs_block_size,
526         &sysfs_btree_node_size,
527         &sysfs_btree_cache_size,
528
529         &sysfs_meta_replicas_have,
530         &sysfs_data_replicas_have,
531
532         &sysfs_journal_write_delay_ms,
533         &sysfs_journal_reclaim_delay_ms,
534
535         &sysfs_promote_whole_extents,
536
537         &sysfs_compression_stats,
538
539 #ifdef CONFIG_BCACHEFS_TESTS
540         &sysfs_perf_test,
541 #endif
542         NULL
543 };
544
545 /* internal dir - just a wrapper */
546
547 SHOW(bch2_fs_internal)
548 {
549         struct bch_fs *c = container_of(kobj, struct bch_fs, internal);
550         return bch2_fs_show(&c->kobj, attr, buf);
551 }
552
553 STORE(bch2_fs_internal)
554 {
555         struct bch_fs *c = container_of(kobj, struct bch_fs, internal);
556         return bch2_fs_store(&c->kobj, attr, buf, size);
557 }
558 SYSFS_OPS(bch2_fs_internal);
559
560 struct attribute *bch2_fs_internal_files[] = {
561         &sysfs_alloc_debug,
562         &sysfs_journal_debug,
563         &sysfs_journal_pins,
564         &sysfs_btree_updates,
565         &sysfs_dirty_btree_nodes,
566
567         &sysfs_read_realloc_races,
568         &sysfs_extent_migrate_done,
569         &sysfs_extent_migrate_raced,
570
571         &sysfs_trigger_journal_flush,
572         &sysfs_trigger_btree_coalesce,
573         &sysfs_trigger_gc,
574         &sysfs_prune_cache,
575
576         &sysfs_copy_gc_enabled,
577
578         &sysfs_rebalance_enabled,
579         &sysfs_rebalance_work,
580         sysfs_pd_controller_files(rebalance),
581
582         &sysfs_new_stripes,
583
584         &sysfs_internal_uuid,
585
586 #define BCH_DEBUG_PARAM(name, description) &sysfs_##name,
587         BCH_DEBUG_PARAMS()
588 #undef BCH_DEBUG_PARAM
589
590         NULL
591 };
592
593 /* options */
594
595 SHOW(bch2_fs_opts_dir)
596 {
597         struct printbuf out = _PBUF(buf, PAGE_SIZE);
598         struct bch_fs *c = container_of(kobj, struct bch_fs, opts_dir);
599         const struct bch_option *opt = container_of(attr, struct bch_option, attr);
600         int id = opt - bch2_opt_table;
601         u64 v = bch2_opt_get_by_id(&c->opts, id);
602
603         bch2_opt_to_text(&out, c, opt, v, OPT_SHOW_FULL_LIST);
604         pr_buf(&out, "\n");
605
606         return out.pos - buf;
607 }
608
609 STORE(bch2_fs_opts_dir)
610 {
611         struct bch_fs *c = container_of(kobj, struct bch_fs, opts_dir);
612         const struct bch_option *opt = container_of(attr, struct bch_option, attr);
613         int ret, id = opt - bch2_opt_table;
614         char *tmp;
615         u64 v;
616
617         tmp = kstrdup(buf, GFP_KERNEL);
618         if (!tmp)
619                 return -ENOMEM;
620
621         ret = bch2_opt_parse(c, opt, strim(tmp), &v);
622         kfree(tmp);
623
624         if (ret < 0)
625                 return ret;
626
627         ret = bch2_opt_check_may_set(c, id, v);
628         if (ret < 0)
629                 return ret;
630
631         if (opt->set_sb != SET_NO_SB_OPT) {
632                 mutex_lock(&c->sb_lock);
633                 opt->set_sb(c->disk_sb.sb, v);
634                 bch2_write_super(c);
635                 mutex_unlock(&c->sb_lock);
636         }
637
638         bch2_opt_set_by_id(&c->opts, id, v);
639
640         if ((id == Opt_background_target ||
641              id == Opt_background_compression) && v) {
642                 bch2_rebalance_add_work(c, S64_MAX);
643                 rebalance_wakeup(c);
644         }
645
646         return size;
647 }
648 SYSFS_OPS(bch2_fs_opts_dir);
649
650 struct attribute *bch2_fs_opts_dir_files[] = { NULL };
651
652 int bch2_opts_create_sysfs_files(struct kobject *kobj)
653 {
654         const struct bch_option *i;
655         int ret;
656
657         for (i = bch2_opt_table;
658              i < bch2_opt_table + bch2_opts_nr;
659              i++) {
660                 if (i->mode == OPT_INTERNAL)
661                         continue;
662
663                 ret = sysfs_create_file(kobj, &i->attr);
664                 if (ret)
665                         return ret;
666         }
667
668         return 0;
669 }
670
671 /* time stats */
672
673 SHOW(bch2_fs_time_stats)
674 {
675         struct bch_fs *c = container_of(kobj, struct bch_fs, time_stats);
676
677 #define x(name)                                         \
678         if (attr == &sysfs_time_stat_##name)                            \
679                 return bch2_time_stats_print(&c->times[BCH_TIME_##name],\
680                                              buf, PAGE_SIZE);
681         BCH_TIME_STATS()
682 #undef x
683
684         return 0;
685 }
686
687 STORE(bch2_fs_time_stats)
688 {
689         return size;
690 }
691 SYSFS_OPS(bch2_fs_time_stats);
692
693 struct attribute *bch2_fs_time_stats_files[] = {
694 #define x(name)                                         \
695         &sysfs_time_stat_##name,
696         BCH_TIME_STATS()
697 #undef x
698         NULL
699 };
700
701 typedef unsigned (bucket_map_fn)(struct bch_fs *, struct bch_dev *,
702                                  size_t, void *);
703
704 static unsigned bucket_last_io_fn(struct bch_fs *c, struct bch_dev *ca,
705                                   size_t b, void *private)
706 {
707         int rw = (private ? 1 : 0);
708
709         return bucket_last_io(c, bucket(ca, b), rw);
710 }
711
712 static unsigned bucket_sectors_used_fn(struct bch_fs *c, struct bch_dev *ca,
713                                        size_t b, void *private)
714 {
715         struct bucket *g = bucket(ca, b);
716         return bucket_sectors_used(g->mark);
717 }
718
719 static unsigned bucket_oldest_gen_fn(struct bch_fs *c, struct bch_dev *ca,
720                                      size_t b, void *private)
721 {
722         return bucket_gc_gen(ca, b);
723 }
724
725 static int unsigned_cmp(const void *_l, const void *_r)
726 {
727         unsigned l = *((unsigned *) _l);
728         unsigned r = *((unsigned *) _r);
729
730         return (l > r) - (l < r);
731 }
732
733 static ssize_t show_quantiles(struct bch_fs *c, struct bch_dev *ca,
734                               char *buf, bucket_map_fn *fn, void *private)
735 {
736         size_t i, n;
737         /* Compute 31 quantiles */
738         unsigned q[31], *p;
739         ssize_t ret = 0;
740
741         down_read(&ca->bucket_lock);
742         n = ca->mi.nbuckets;
743
744         p = vzalloc(n * sizeof(unsigned));
745         if (!p) {
746                 up_read(&ca->bucket_lock);
747                 return -ENOMEM;
748         }
749
750         for (i = ca->mi.first_bucket; i < n; i++)
751                 p[i] = fn(c, ca, i, private);
752
753         sort(p, n, sizeof(unsigned), unsigned_cmp, NULL);
754         up_read(&ca->bucket_lock);
755
756         while (n &&
757                !p[n - 1])
758                 --n;
759
760         for (i = 0; i < ARRAY_SIZE(q); i++)
761                 q[i] = p[n * (i + 1) / (ARRAY_SIZE(q) + 1)];
762
763         vfree(p);
764
765         for (i = 0; i < ARRAY_SIZE(q); i++)
766                 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
767                                  "%u ", q[i]);
768         buf[ret - 1] = '\n';
769
770         return ret;
771 }
772
773 static ssize_t show_reserve_stats(struct bch_dev *ca, char *buf)
774 {
775         struct printbuf out = _PBUF(buf, PAGE_SIZE);
776         enum alloc_reserve i;
777
778         spin_lock(&ca->freelist_lock);
779
780         pr_buf(&out, "free_inc:\t%zu\t%zu\n",
781                fifo_used(&ca->free_inc),
782                ca->free_inc.size);
783
784         for (i = 0; i < RESERVE_NR; i++)
785                 pr_buf(&out, "free[%u]:\t%zu\t%zu\n", i,
786                        fifo_used(&ca->free[i]),
787                        ca->free[i].size);
788
789         spin_unlock(&ca->freelist_lock);
790
791         return out.pos - buf;
792 }
793
794 static ssize_t show_dev_alloc_debug(struct bch_dev *ca, char *buf)
795 {
796         struct bch_fs *c = ca->fs;
797         struct bch_dev_usage stats = bch2_dev_usage_read(c, ca);
798
799         return scnprintf(buf, PAGE_SIZE,
800                 "free_inc:               %zu/%zu\n"
801                 "free[RESERVE_BTREE]:    %zu/%zu\n"
802                 "free[RESERVE_MOVINGGC]: %zu/%zu\n"
803                 "free[RESERVE_NONE]:     %zu/%zu\n"
804                 "buckets:\n"
805                 "    capacity:           %llu\n"
806                 "    alloc:              %llu\n"
807                 "    sb:                 %llu\n"
808                 "    journal:            %llu\n"
809                 "    meta:               %llu\n"
810                 "    user:               %llu\n"
811                 "    cached:             %llu\n"
812                 "    erasure coded:      %llu\n"
813                 "    available:          %lli\n"
814                 "sectors:\n"
815                 "    sb:                 %llu\n"
816                 "    journal:            %llu\n"
817                 "    meta:               %llu\n"
818                 "    user:               %llu\n"
819                 "    cached:             %llu\n"
820                 "    fragmented:         %llu\n"
821                 "    copygc threshold:   %llu\n"
822                 "freelist_wait:          %s\n"
823                 "open buckets:           %u/%u (reserved %u)\n"
824                 "open_buckets_wait:      %s\n",
825                 fifo_used(&ca->free_inc),               ca->free_inc.size,
826                 fifo_used(&ca->free[RESERVE_BTREE]),    ca->free[RESERVE_BTREE].size,
827                 fifo_used(&ca->free[RESERVE_MOVINGGC]), ca->free[RESERVE_MOVINGGC].size,
828                 fifo_used(&ca->free[RESERVE_NONE]),     ca->free[RESERVE_NONE].size,
829                 ca->mi.nbuckets - ca->mi.first_bucket,
830                 stats.buckets_alloc,
831                 stats.buckets[BCH_DATA_SB],
832                 stats.buckets[BCH_DATA_JOURNAL],
833                 stats.buckets[BCH_DATA_BTREE],
834                 stats.buckets[BCH_DATA_USER],
835                 stats.buckets[BCH_DATA_CACHED],
836                 stats.buckets_ec,
837                 ca->mi.nbuckets - ca->mi.first_bucket - stats.buckets_unavailable,
838                 stats.sectors[BCH_DATA_SB],
839                 stats.sectors[BCH_DATA_JOURNAL],
840                 stats.sectors[BCH_DATA_BTREE],
841                 stats.sectors[BCH_DATA_USER],
842                 stats.sectors[BCH_DATA_CACHED],
843                 stats.sectors_fragmented,
844                 ca->copygc_threshold,
845                 c->freelist_wait.list.first             ? "waiting" : "empty",
846                 c->open_buckets_nr_free, OPEN_BUCKETS_COUNT, BTREE_NODE_RESERVE,
847                 c->open_buckets_wait.list.first         ? "waiting" : "empty");
848 }
849
850 static const char * const bch2_rw[] = {
851         "read",
852         "write",
853         NULL
854 };
855
856 static ssize_t show_dev_iodone(struct bch_dev *ca, char *buf)
857 {
858         struct printbuf out = _PBUF(buf, PAGE_SIZE);
859         int rw, i, cpu;
860
861         for (rw = 0; rw < 2; rw++) {
862                 pr_buf(&out, "%s:\n", bch2_rw[rw]);
863
864                 for (i = 1; i < BCH_DATA_NR; i++) {
865                         u64 n = 0;
866
867                         for_each_possible_cpu(cpu)
868                                 n += per_cpu_ptr(ca->io_done, cpu)->sectors[rw][i];
869
870                         pr_buf(&out, "%-12s:%12llu\n",
871                                bch2_data_types[i], n << 9);
872                 }
873         }
874
875         return out.pos - buf;
876 }
877
878 SHOW(bch2_dev)
879 {
880         struct bch_dev *ca = container_of(kobj, struct bch_dev, kobj);
881         struct bch_fs *c = ca->fs;
882         struct printbuf out = _PBUF(buf, PAGE_SIZE);
883
884         sysfs_printf(uuid,              "%pU\n", ca->uuid.b);
885
886         sysfs_print(bucket_size,        bucket_bytes(ca));
887         sysfs_print(block_size,         block_bytes(c));
888         sysfs_print(first_bucket,       ca->mi.first_bucket);
889         sysfs_print(nbuckets,           ca->mi.nbuckets);
890         sysfs_print(durability,         ca->mi.durability);
891         sysfs_print(discard,            ca->mi.discard);
892
893         if (attr == &sysfs_label) {
894                 if (ca->mi.group) {
895                         mutex_lock(&c->sb_lock);
896                         bch2_disk_path_to_text(&out, &c->disk_sb,
897                                                ca->mi.group - 1);
898                         mutex_unlock(&c->sb_lock);
899                 } else {
900                         pr_buf(&out, "none");
901                 }
902
903                 pr_buf(&out, "\n");
904                 return out.pos - buf;
905         }
906
907         if (attr == &sysfs_has_data) {
908                 bch2_flags_to_text(&out, bch2_data_types,
909                                    bch2_dev_has_data(c, ca));
910                 pr_buf(&out, "\n");
911                 return out.pos - buf;
912         }
913
914         sysfs_pd_controller_show(copy_gc, &ca->copygc_pd);
915
916         if (attr == &sysfs_cache_replacement_policy) {
917                 bch2_string_opt_to_text(&out,
918                                         bch2_cache_replacement_policies,
919                                         ca->mi.replacement);
920                 pr_buf(&out, "\n");
921                 return out.pos - buf;
922         }
923
924         if (attr == &sysfs_state_rw) {
925                 bch2_string_opt_to_text(&out, bch2_dev_state,
926                                         ca->mi.state);
927                 pr_buf(&out, "\n");
928                 return out.pos - buf;
929         }
930
931         if (attr == &sysfs_iodone)
932                 return show_dev_iodone(ca, buf);
933
934         sysfs_print(io_latency_read,            atomic64_read(&ca->cur_latency[READ]));
935         sysfs_print(io_latency_write,           atomic64_read(&ca->cur_latency[WRITE]));
936
937         if (attr == &sysfs_io_latency_stats_read)
938                 return bch2_time_stats_print(&ca->io_latency[READ], buf, PAGE_SIZE);
939         if (attr == &sysfs_io_latency_stats_write)
940                 return bch2_time_stats_print(&ca->io_latency[WRITE], buf, PAGE_SIZE);
941
942         sysfs_printf(congested,                 "%u%%",
943                      clamp(atomic_read(&ca->congested), 0, CONGESTED_MAX)
944                      * 100 / CONGESTED_MAX);
945
946         if (attr == &sysfs_bucket_quantiles_last_read)
947                 return show_quantiles(c, ca, buf, bucket_last_io_fn, (void *) 0);
948         if (attr == &sysfs_bucket_quantiles_last_write)
949                 return show_quantiles(c, ca, buf, bucket_last_io_fn, (void *) 1);
950         if (attr == &sysfs_bucket_quantiles_fragmentation)
951                 return show_quantiles(c, ca, buf, bucket_sectors_used_fn, NULL);
952         if (attr == &sysfs_bucket_quantiles_oldest_gen)
953                 return show_quantiles(c, ca, buf, bucket_oldest_gen_fn, NULL);
954
955         if (attr == &sysfs_reserve_stats)
956                 return show_reserve_stats(ca, buf);
957         if (attr == &sysfs_alloc_debug)
958                 return show_dev_alloc_debug(ca, buf);
959
960         return 0;
961 }
962
963 STORE(bch2_dev)
964 {
965         struct bch_dev *ca = container_of(kobj, struct bch_dev, kobj);
966         struct bch_fs *c = ca->fs;
967         struct bch_member *mi;
968
969         sysfs_pd_controller_store(copy_gc, &ca->copygc_pd);
970
971         if (attr == &sysfs_discard) {
972                 bool v = strtoul_or_return(buf);
973
974                 mutex_lock(&c->sb_lock);
975                 mi = &bch2_sb_get_members(c->disk_sb.sb)->members[ca->dev_idx];
976
977                 if (v != BCH_MEMBER_DISCARD(mi)) {
978                         SET_BCH_MEMBER_DISCARD(mi, v);
979                         bch2_write_super(c);
980                 }
981                 mutex_unlock(&c->sb_lock);
982         }
983
984         if (attr == &sysfs_cache_replacement_policy) {
985                 ssize_t v = __sysfs_match_string(bch2_cache_replacement_policies, -1, buf);
986
987                 if (v < 0)
988                         return v;
989
990                 mutex_lock(&c->sb_lock);
991                 mi = &bch2_sb_get_members(c->disk_sb.sb)->members[ca->dev_idx];
992
993                 if ((unsigned) v != BCH_MEMBER_REPLACEMENT(mi)) {
994                         SET_BCH_MEMBER_REPLACEMENT(mi, v);
995                         bch2_write_super(c);
996                 }
997                 mutex_unlock(&c->sb_lock);
998         }
999
1000         if (attr == &sysfs_label) {
1001                 char *tmp;
1002                 int ret;
1003
1004                 tmp = kstrdup(buf, GFP_KERNEL);
1005                 if (!tmp)
1006                         return -ENOMEM;
1007
1008                 ret = bch2_dev_group_set(c, ca, strim(tmp));
1009                 kfree(tmp);
1010                 if (ret)
1011                         return ret;
1012         }
1013
1014         if (attr == &sysfs_wake_allocator)
1015                 bch2_wake_allocator(ca);
1016
1017         return size;
1018 }
1019 SYSFS_OPS(bch2_dev);
1020
1021 struct attribute *bch2_dev_files[] = {
1022         &sysfs_uuid,
1023         &sysfs_bucket_size,
1024         &sysfs_block_size,
1025         &sysfs_first_bucket,
1026         &sysfs_nbuckets,
1027         &sysfs_durability,
1028
1029         /* settings: */
1030         &sysfs_discard,
1031         &sysfs_cache_replacement_policy,
1032         &sysfs_state_rw,
1033         &sysfs_label,
1034
1035         &sysfs_has_data,
1036         &sysfs_iodone,
1037
1038         &sysfs_io_latency_read,
1039         &sysfs_io_latency_write,
1040         &sysfs_io_latency_stats_read,
1041         &sysfs_io_latency_stats_write,
1042         &sysfs_congested,
1043
1044         /* alloc info - other stats: */
1045         &sysfs_bucket_quantiles_last_read,
1046         &sysfs_bucket_quantiles_last_write,
1047         &sysfs_bucket_quantiles_fragmentation,
1048         &sysfs_bucket_quantiles_oldest_gen,
1049
1050         &sysfs_reserve_stats,
1051
1052         /* debug: */
1053         &sysfs_alloc_debug,
1054         &sysfs_wake_allocator,
1055
1056         sysfs_pd_controller_files(copy_gc),
1057         NULL
1058 };
1059
1060 #endif  /* _BCACHEFS_SYSFS_H_ */