]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/sysfs.c
Merge pull request #24 from brendon-boldt/new-install-distros
[bcachefs-tools-debian] / libbcachefs / sysfs.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * bcache sysfs interfaces
4  *
5  * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com>
6  * Copyright 2012 Google, Inc.
7  */
8
9 #ifndef NO_BCACHEFS_SYSFS
10
11 #include "bcachefs.h"
12 #include "alloc_background.h"
13 #include "sysfs.h"
14 #include "btree_cache.h"
15 #include "btree_io.h"
16 #include "btree_iter.h"
17 #include "btree_update.h"
18 #include "btree_update_interior.h"
19 #include "btree_gc.h"
20 #include "buckets.h"
21 #include "disk_groups.h"
22 #include "ec.h"
23 #include "inode.h"
24 #include "journal.h"
25 #include "keylist.h"
26 #include "move.h"
27 #include "opts.h"
28 #include "rebalance.h"
29 #include "replicas.h"
30 #include "super-io.h"
31 #include "tests.h"
32
33 #include <linux/blkdev.h>
34 #include <linux/sort.h>
35 #include <linux/sched/clock.h>
36
37 #include "util.h"
38
39 #define SYSFS_OPS(type)                                                 \
40 struct sysfs_ops type ## _sysfs_ops = {                                 \
41         .show   = type ## _show,                                        \
42         .store  = type ## _store                                        \
43 }
44
45 #define SHOW(fn)                                                        \
46 static ssize_t fn ## _show(struct kobject *kobj, struct attribute *attr,\
47                            char *buf)                                   \
48
49 #define STORE(fn)                                                       \
50 static ssize_t fn ## _store(struct kobject *kobj, struct attribute *attr,\
51                             const char *buf, size_t size)               \
52
53 #define __sysfs_attribute(_name, _mode)                                 \
54         static struct attribute sysfs_##_name =                         \
55                 { .name = #_name, .mode = _mode }
56
57 #define write_attribute(n)      __sysfs_attribute(n, S_IWUSR)
58 #define read_attribute(n)       __sysfs_attribute(n, S_IRUGO)
59 #define rw_attribute(n)         __sysfs_attribute(n, S_IRUGO|S_IWUSR)
60
61 #define sysfs_printf(file, fmt, ...)                                    \
62 do {                                                                    \
63         if (attr == &sysfs_ ## file)                                    \
64                 return scnprintf(buf, PAGE_SIZE, fmt "\n", __VA_ARGS__);\
65 } while (0)
66
67 #define sysfs_print(file, var)                                          \
68 do {                                                                    \
69         if (attr == &sysfs_ ## file)                                    \
70                 return snprint(buf, PAGE_SIZE, var);                    \
71 } while (0)
72
73 #define sysfs_hprint(file, val)                                         \
74 do {                                                                    \
75         if (attr == &sysfs_ ## file) {                                  \
76                 struct printbuf out = _PBUF(buf, PAGE_SIZE);            \
77                 bch2_hprint(&out, val);                                 \
78                 pr_buf(&out, "\n");                                     \
79                 return out.pos - buf;                                   \
80         }                                                               \
81 } while (0)
82
83 #define var_printf(_var, fmt)   sysfs_printf(_var, fmt, var(_var))
84 #define var_print(_var)         sysfs_print(_var, var(_var))
85 #define var_hprint(_var)        sysfs_hprint(_var, var(_var))
86
87 #define sysfs_strtoul(file, var)                                        \
88 do {                                                                    \
89         if (attr == &sysfs_ ## file)                                    \
90                 return strtoul_safe(buf, var) ?: (ssize_t) size;        \
91 } while (0)
92
93 #define sysfs_strtoul_clamp(file, var, min, max)                        \
94 do {                                                                    \
95         if (attr == &sysfs_ ## file)                                    \
96                 return strtoul_safe_clamp(buf, var, min, max)           \
97                         ?: (ssize_t) size;                              \
98 } while (0)
99
100 #define strtoul_or_return(cp)                                           \
101 ({                                                                      \
102         unsigned long _v;                                               \
103         int _r = kstrtoul(cp, 10, &_v);                                 \
104         if (_r)                                                         \
105                 return _r;                                              \
106         _v;                                                             \
107 })
108
109 #define strtoul_restrict_or_return(cp, min, max)                        \
110 ({                                                                      \
111         unsigned long __v = 0;                                          \
112         int _r = strtoul_safe_restrict(cp, __v, min, max);              \
113         if (_r)                                                         \
114                 return _r;                                              \
115         __v;                                                            \
116 })
117
118 #define strtoi_h_or_return(cp)                                          \
119 ({                                                                      \
120         u64 _v;                                                         \
121         int _r = strtoi_h(cp, &_v);                                     \
122         if (_r)                                                         \
123                 return _r;                                              \
124         _v;                                                             \
125 })
126
127 #define sysfs_hatoi(file, var)                                          \
128 do {                                                                    \
129         if (attr == &sysfs_ ## file)                                    \
130                 return strtoi_h(buf, &var) ?: (ssize_t) size;           \
131 } while (0)
132
133 write_attribute(trigger_journal_flush);
134 write_attribute(trigger_btree_coalesce);
135 write_attribute(trigger_gc);
136 write_attribute(trigger_alloc_write);
137 write_attribute(prune_cache);
138 rw_attribute(btree_gc_periodic);
139
140 read_attribute(uuid);
141 read_attribute(minor);
142 read_attribute(bucket_size);
143 read_attribute(block_size);
144 read_attribute(btree_node_size);
145 read_attribute(first_bucket);
146 read_attribute(nbuckets);
147 read_attribute(durability);
148 read_attribute(iodone);
149
150 read_attribute(io_latency_read);
151 read_attribute(io_latency_write);
152 read_attribute(io_latency_stats_read);
153 read_attribute(io_latency_stats_write);
154 read_attribute(congested);
155
156 read_attribute(bucket_quantiles_last_read);
157 read_attribute(bucket_quantiles_last_write);
158 read_attribute(bucket_quantiles_fragmentation);
159 read_attribute(bucket_quantiles_oldest_gen);
160
161 read_attribute(reserve_stats);
162 read_attribute(btree_cache_size);
163 read_attribute(compression_stats);
164 read_attribute(journal_debug);
165 read_attribute(journal_pins);
166 read_attribute(btree_updates);
167 read_attribute(dirty_btree_nodes);
168
169 read_attribute(internal_uuid);
170
171 read_attribute(has_data);
172 read_attribute(alloc_debug);
173 write_attribute(wake_allocator);
174
175 read_attribute(read_realloc_races);
176 read_attribute(extent_migrate_done);
177 read_attribute(extent_migrate_raced);
178
179 rw_attribute(journal_write_delay_ms);
180 rw_attribute(journal_reclaim_delay_ms);
181
182 rw_attribute(discard);
183 rw_attribute(cache_replacement_policy);
184 rw_attribute(label);
185
186 rw_attribute(copy_gc_enabled);
187 sysfs_pd_controller_attribute(copy_gc);
188
189 rw_attribute(rebalance_enabled);
190 sysfs_pd_controller_attribute(rebalance);
191 read_attribute(rebalance_work);
192 rw_attribute(promote_whole_extents);
193
194 read_attribute(new_stripes);
195
196 rw_attribute(pd_controllers_update_seconds);
197
198 read_attribute(meta_replicas_have);
199 read_attribute(data_replicas_have);
200
201 #ifdef CONFIG_BCACHEFS_TESTS
202 write_attribute(perf_test);
203 #endif /* CONFIG_BCACHEFS_TESTS */
204
205 #define BCH_DEBUG_PARAM(name, description)                              \
206         rw_attribute(name);
207
208         BCH_DEBUG_PARAMS()
209 #undef BCH_DEBUG_PARAM
210
211 #define x(_name)                                                \
212         static struct attribute sysfs_time_stat_##_name =               \
213                 { .name = #_name, .mode = S_IRUGO };
214         BCH_TIME_STATS()
215 #undef x
216
217 static struct attribute sysfs_state_rw = {
218         .name = "state",
219         .mode = S_IRUGO
220 };
221
222 static size_t bch2_btree_cache_size(struct bch_fs *c)
223 {
224         size_t ret = 0;
225         struct btree *b;
226
227         mutex_lock(&c->btree_cache.lock);
228         list_for_each_entry(b, &c->btree_cache.live, list)
229                 ret += btree_bytes(c);
230
231         mutex_unlock(&c->btree_cache.lock);
232         return ret;
233 }
234
235 static ssize_t show_fs_alloc_debug(struct bch_fs *c, char *buf)
236 {
237         struct printbuf out = _PBUF(buf, PAGE_SIZE);
238         struct bch_fs_usage *fs_usage = bch2_fs_usage_read(c);
239
240         if (!fs_usage)
241                 return -ENOMEM;
242
243         bch2_fs_usage_to_text(&out, c, fs_usage);
244
245         percpu_up_read(&c->mark_lock);
246
247         kfree(fs_usage);
248
249         return out.pos - buf;
250 }
251
252 static ssize_t bch2_compression_stats(struct bch_fs *c, char *buf)
253 {
254         struct btree_trans trans;
255         struct btree_iter *iter;
256         struct bkey_s_c k;
257         u64 nr_uncompressed_extents = 0, uncompressed_sectors = 0,
258             nr_compressed_extents = 0,
259             compressed_sectors_compressed = 0,
260             compressed_sectors_uncompressed = 0;
261         int ret;
262
263         if (!test_bit(BCH_FS_STARTED, &c->flags))
264                 return -EPERM;
265
266         bch2_trans_init(&trans, c, 0, 0);
267
268         for_each_btree_key(&trans, iter, BTREE_ID_EXTENTS, POS_MIN, 0, k, ret)
269                 if (k.k->type == KEY_TYPE_extent) {
270                         struct bkey_s_c_extent e = bkey_s_c_to_extent(k);
271                         const union bch_extent_entry *entry;
272                         struct extent_ptr_decoded p;
273
274                         extent_for_each_ptr_decode(e, p, entry) {
275                                 if (p.crc.compression_type == BCH_COMPRESSION_NONE) {
276                                         nr_uncompressed_extents++;
277                                         uncompressed_sectors += e.k->size;
278                                 } else {
279                                         nr_compressed_extents++;
280                                         compressed_sectors_compressed +=
281                                                 p.crc.compressed_size;
282                                         compressed_sectors_uncompressed +=
283                                                 p.crc.uncompressed_size;
284                                 }
285
286                                 /* only looking at the first ptr */
287                                 break;
288                         }
289                 }
290
291         ret = bch2_trans_exit(&trans) ?: ret;
292         if (ret)
293                 return ret;
294
295         return scnprintf(buf, PAGE_SIZE,
296                         "uncompressed data:\n"
297                         "       nr extents:                     %llu\n"
298                         "       size (bytes):                   %llu\n"
299                         "compressed data:\n"
300                         "       nr extents:                     %llu\n"
301                         "       compressed size (bytes):        %llu\n"
302                         "       uncompressed size (bytes):      %llu\n",
303                         nr_uncompressed_extents,
304                         uncompressed_sectors << 9,
305                         nr_compressed_extents,
306                         compressed_sectors_compressed << 9,
307                         compressed_sectors_uncompressed << 9);
308 }
309
310 static ssize_t bch2_new_stripes(struct bch_fs *c, char *buf)
311 {
312         char *out = buf, *end = buf + PAGE_SIZE;
313         struct ec_stripe_head *h;
314         struct ec_stripe_new *s;
315
316         mutex_lock(&c->ec_new_stripe_lock);
317         list_for_each_entry(h, &c->ec_new_stripe_list, list) {
318                 out += scnprintf(out, end - out,
319                                  "target %u algo %u redundancy %u:\n",
320                                  h->target, h->algo, h->redundancy);
321
322                 if (h->s)
323                         out += scnprintf(out, end - out,
324                                          "\tpending: blocks %u allocated %u\n",
325                                          h->s->blocks.nr,
326                                          bitmap_weight(h->s->blocks_allocated,
327                                                        h->s->blocks.nr));
328
329                 mutex_lock(&h->lock);
330                 list_for_each_entry(s, &h->stripes, list)
331                         out += scnprintf(out, end - out,
332                                          "\tin flight: blocks %u allocated %u pin %u\n",
333                                          s->blocks.nr,
334                                          bitmap_weight(s->blocks_allocated,
335                                                        s->blocks.nr),
336                                          atomic_read(&s->pin));
337                 mutex_unlock(&h->lock);
338
339         }
340         mutex_unlock(&c->ec_new_stripe_lock);
341
342         return out - buf;
343 }
344
345 SHOW(bch2_fs)
346 {
347         struct bch_fs *c = container_of(kobj, struct bch_fs, kobj);
348
349         sysfs_print(minor,                      c->minor);
350         sysfs_printf(internal_uuid, "%pU",      c->sb.uuid.b);
351
352         sysfs_print(journal_write_delay_ms,     c->journal.write_delay_ms);
353         sysfs_print(journal_reclaim_delay_ms,   c->journal.reclaim_delay_ms);
354
355         sysfs_print(block_size,                 block_bytes(c));
356         sysfs_print(btree_node_size,            btree_bytes(c));
357         sysfs_hprint(btree_cache_size,          bch2_btree_cache_size(c));
358
359         sysfs_print(read_realloc_races,
360                     atomic_long_read(&c->read_realloc_races));
361         sysfs_print(extent_migrate_done,
362                     atomic_long_read(&c->extent_migrate_done));
363         sysfs_print(extent_migrate_raced,
364                     atomic_long_read(&c->extent_migrate_raced));
365
366         sysfs_printf(btree_gc_periodic, "%u",   (int) c->btree_gc_periodic);
367
368         sysfs_printf(copy_gc_enabled, "%i", c->copy_gc_enabled);
369
370         sysfs_print(pd_controllers_update_seconds,
371                     c->pd_controllers_update_seconds);
372
373         sysfs_printf(rebalance_enabled,         "%i", c->rebalance.enabled);
374         sysfs_pd_controller_show(rebalance,     &c->rebalance.pd); /* XXX */
375
376         if (attr == &sysfs_rebalance_work)
377                 return bch2_rebalance_work_show(c, buf);
378
379         sysfs_print(promote_whole_extents,      c->promote_whole_extents);
380
381         sysfs_printf(meta_replicas_have, "%i",  bch2_replicas_online(c, true));
382         sysfs_printf(data_replicas_have, "%i",  bch2_replicas_online(c, false));
383
384         /* Debugging: */
385
386         if (attr == &sysfs_alloc_debug)
387                 return show_fs_alloc_debug(c, buf);
388
389         if (attr == &sysfs_journal_debug)
390                 return bch2_journal_print_debug(&c->journal, buf);
391
392         if (attr == &sysfs_journal_pins)
393                 return bch2_journal_print_pins(&c->journal, buf);
394
395         if (attr == &sysfs_btree_updates)
396                 return bch2_btree_updates_print(c, buf);
397
398         if (attr == &sysfs_dirty_btree_nodes)
399                 return bch2_dirty_btree_nodes_print(c, buf);
400
401         if (attr == &sysfs_compression_stats)
402                 return bch2_compression_stats(c, buf);
403
404         if (attr == &sysfs_new_stripes)
405                 return bch2_new_stripes(c, buf);
406
407 #define BCH_DEBUG_PARAM(name, description) sysfs_print(name, c->name);
408         BCH_DEBUG_PARAMS()
409 #undef BCH_DEBUG_PARAM
410
411         return 0;
412 }
413
414 STORE(__bch2_fs)
415 {
416         struct bch_fs *c = container_of(kobj, struct bch_fs, kobj);
417
418         sysfs_strtoul(journal_write_delay_ms, c->journal.write_delay_ms);
419         sysfs_strtoul(journal_reclaim_delay_ms, c->journal.reclaim_delay_ms);
420
421         if (attr == &sysfs_btree_gc_periodic) {
422                 ssize_t ret = strtoul_safe(buf, c->btree_gc_periodic)
423                         ?: (ssize_t) size;
424
425                 wake_up_process(c->gc_thread);
426                 return ret;
427         }
428
429         if (attr == &sysfs_copy_gc_enabled) {
430                 struct bch_dev *ca;
431                 unsigned i;
432                 ssize_t ret = strtoul_safe(buf, c->copy_gc_enabled)
433                         ?: (ssize_t) size;
434
435                 for_each_member_device(ca, c, i)
436                         if (ca->copygc_thread)
437                                 wake_up_process(ca->copygc_thread);
438                 return ret;
439         }
440
441         if (attr == &sysfs_rebalance_enabled) {
442                 ssize_t ret = strtoul_safe(buf, c->rebalance.enabled)
443                         ?: (ssize_t) size;
444
445                 rebalance_wakeup(c);
446                 return ret;
447         }
448
449         sysfs_strtoul(pd_controllers_update_seconds,
450                       c->pd_controllers_update_seconds);
451         sysfs_pd_controller_store(rebalance,    &c->rebalance.pd);
452
453         sysfs_strtoul(promote_whole_extents,    c->promote_whole_extents);
454
455         /* Debugging: */
456
457 #define BCH_DEBUG_PARAM(name, description) sysfs_strtoul(name, c->name);
458         BCH_DEBUG_PARAMS()
459 #undef BCH_DEBUG_PARAM
460
461         if (!test_bit(BCH_FS_STARTED, &c->flags))
462                 return -EPERM;
463
464         /* Debugging: */
465
466         if (attr == &sysfs_trigger_journal_flush)
467                 bch2_journal_meta_async(&c->journal, NULL);
468
469         if (attr == &sysfs_trigger_btree_coalesce)
470                 bch2_coalesce(c);
471
472         if (attr == &sysfs_trigger_gc)
473                 bch2_gc(c, NULL, false, false);
474
475         if (attr == &sysfs_trigger_alloc_write) {
476                 bool wrote;
477
478                 bch2_alloc_write(c, 0, &wrote);
479         }
480
481         if (attr == &sysfs_prune_cache) {
482                 struct shrink_control sc;
483
484                 sc.gfp_mask = GFP_KERNEL;
485                 sc.nr_to_scan = strtoul_or_return(buf);
486                 c->btree_cache.shrink.scan_objects(&c->btree_cache.shrink, &sc);
487         }
488 #ifdef CONFIG_BCACHEFS_TESTS
489         if (attr == &sysfs_perf_test) {
490                 char *tmp = kstrdup(buf, GFP_KERNEL), *p = tmp;
491                 char *test              = strsep(&p, " \t\n");
492                 char *nr_str            = strsep(&p, " \t\n");
493                 char *threads_str       = strsep(&p, " \t\n");
494                 unsigned threads;
495                 u64 nr;
496                 int ret = -EINVAL;
497
498                 if (threads_str &&
499                     !(ret = kstrtouint(threads_str, 10, &threads)) &&
500                     !(ret = bch2_strtoull_h(nr_str, &nr)))
501                         bch2_btree_perf_test(c, test, nr, threads);
502                 else
503                         size = ret;
504                 kfree(tmp);
505         }
506 #endif
507         return size;
508 }
509
510 STORE(bch2_fs)
511 {
512         struct bch_fs *c = container_of(kobj, struct bch_fs, kobj);
513
514         mutex_lock(&c->state_lock);
515         size = __bch2_fs_store(kobj, attr, buf, size);
516         mutex_unlock(&c->state_lock);
517
518         return size;
519 }
520 SYSFS_OPS(bch2_fs);
521
522 struct attribute *bch2_fs_files[] = {
523         &sysfs_minor,
524         &sysfs_block_size,
525         &sysfs_btree_node_size,
526         &sysfs_btree_cache_size,
527
528         &sysfs_meta_replicas_have,
529         &sysfs_data_replicas_have,
530
531         &sysfs_journal_write_delay_ms,
532         &sysfs_journal_reclaim_delay_ms,
533
534         &sysfs_promote_whole_extents,
535
536         &sysfs_compression_stats,
537
538 #ifdef CONFIG_BCACHEFS_TESTS
539         &sysfs_perf_test,
540 #endif
541         NULL
542 };
543
544 /* internal dir - just a wrapper */
545
546 SHOW(bch2_fs_internal)
547 {
548         struct bch_fs *c = container_of(kobj, struct bch_fs, internal);
549         return bch2_fs_show(&c->kobj, attr, buf);
550 }
551
552 STORE(bch2_fs_internal)
553 {
554         struct bch_fs *c = container_of(kobj, struct bch_fs, internal);
555         return bch2_fs_store(&c->kobj, attr, buf, size);
556 }
557 SYSFS_OPS(bch2_fs_internal);
558
559 struct attribute *bch2_fs_internal_files[] = {
560         &sysfs_alloc_debug,
561         &sysfs_journal_debug,
562         &sysfs_journal_pins,
563         &sysfs_btree_updates,
564         &sysfs_dirty_btree_nodes,
565
566         &sysfs_read_realloc_races,
567         &sysfs_extent_migrate_done,
568         &sysfs_extent_migrate_raced,
569
570         &sysfs_trigger_journal_flush,
571         &sysfs_trigger_btree_coalesce,
572         &sysfs_trigger_gc,
573         &sysfs_trigger_alloc_write,
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_FORMAT|OPT_MOUNT|OPT_RUNTIME)))
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         const unsigned *l = _l;
728         const unsigned *r = _r;
729
730         return cmp_int(*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->fs->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->fs->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         unsigned i, nr[BCH_DATA_NR];
799
800         memset(nr, 0, sizeof(nr));
801
802         for (i = 0; i < ARRAY_SIZE(c->open_buckets); i++)
803                 nr[c->open_buckets[i].type]++;
804
805         return scnprintf(buf, PAGE_SIZE,
806                 "free_inc:               %zu/%zu\n"
807                 "free[RESERVE_BTREE]:    %zu/%zu\n"
808                 "free[RESERVE_MOVINGGC]: %zu/%zu\n"
809                 "free[RESERVE_NONE]:     %zu/%zu\n"
810                 "buckets:\n"
811                 "    capacity:           %llu\n"
812                 "    alloc:              %llu\n"
813                 "    sb:                 %llu\n"
814                 "    journal:            %llu\n"
815                 "    meta:               %llu\n"
816                 "    user:               %llu\n"
817                 "    cached:             %llu\n"
818                 "    erasure coded:      %llu\n"
819                 "    available:          %lli\n"
820                 "sectors:\n"
821                 "    sb:                 %llu\n"
822                 "    journal:            %llu\n"
823                 "    meta:               %llu\n"
824                 "    user:               %llu\n"
825                 "    cached:             %llu\n"
826                 "    fragmented:         %llu\n"
827                 "    copygc threshold:   %llu\n"
828                 "freelist_wait:          %s\n"
829                 "open buckets:           %u/%u (reserved %u)\n"
830                 "open_buckets_wait:      %s\n"
831                 "open_buckets_btree:     %u\n"
832                 "open_buckets_user:      %u\n"
833                 "btree reserve cache:    %u\n",
834                 fifo_used(&ca->free_inc),               ca->free_inc.size,
835                 fifo_used(&ca->free[RESERVE_BTREE]),    ca->free[RESERVE_BTREE].size,
836                 fifo_used(&ca->free[RESERVE_MOVINGGC]), ca->free[RESERVE_MOVINGGC].size,
837                 fifo_used(&ca->free[RESERVE_NONE]),     ca->free[RESERVE_NONE].size,
838                 ca->mi.nbuckets - ca->mi.first_bucket,
839                 stats.buckets_alloc,
840                 stats.buckets[BCH_DATA_SB],
841                 stats.buckets[BCH_DATA_JOURNAL],
842                 stats.buckets[BCH_DATA_BTREE],
843                 stats.buckets[BCH_DATA_USER],
844                 stats.buckets[BCH_DATA_CACHED],
845                 stats.buckets_ec,
846                 ca->mi.nbuckets - ca->mi.first_bucket - stats.buckets_unavailable,
847                 stats.sectors[BCH_DATA_SB],
848                 stats.sectors[BCH_DATA_JOURNAL],
849                 stats.sectors[BCH_DATA_BTREE],
850                 stats.sectors[BCH_DATA_USER],
851                 stats.sectors[BCH_DATA_CACHED],
852                 stats.sectors_fragmented,
853                 ca->copygc_threshold,
854                 c->freelist_wait.list.first             ? "waiting" : "empty",
855                 c->open_buckets_nr_free, OPEN_BUCKETS_COUNT,
856                 BTREE_NODE_OPEN_BUCKET_RESERVE,
857                 c->open_buckets_wait.list.first         ? "waiting" : "empty",
858                 nr[BCH_DATA_BTREE],
859                 nr[BCH_DATA_USER],
860                 c->btree_reserve_cache_nr);
861 }
862
863 static const char * const bch2_rw[] = {
864         "read",
865         "write",
866         NULL
867 };
868
869 static ssize_t show_dev_iodone(struct bch_dev *ca, char *buf)
870 {
871         struct printbuf out = _PBUF(buf, PAGE_SIZE);
872         int rw, i;
873
874         for (rw = 0; rw < 2; rw++) {
875                 pr_buf(&out, "%s:\n", bch2_rw[rw]);
876
877                 for (i = 1; i < BCH_DATA_NR; i++)
878                         pr_buf(&out, "%-12s:%12llu\n",
879                                bch2_data_types[i],
880                                percpu_u64_get(&ca->io_done->sectors[rw][i]) << 9);
881         }
882
883         return out.pos - buf;
884 }
885
886 SHOW(bch2_dev)
887 {
888         struct bch_dev *ca = container_of(kobj, struct bch_dev, kobj);
889         struct bch_fs *c = ca->fs;
890         struct printbuf out = _PBUF(buf, PAGE_SIZE);
891
892         sysfs_printf(uuid,              "%pU\n", ca->uuid.b);
893
894         sysfs_print(bucket_size,        bucket_bytes(ca));
895         sysfs_print(block_size,         block_bytes(c));
896         sysfs_print(first_bucket,       ca->mi.first_bucket);
897         sysfs_print(nbuckets,           ca->mi.nbuckets);
898         sysfs_print(durability,         ca->mi.durability);
899         sysfs_print(discard,            ca->mi.discard);
900
901         if (attr == &sysfs_label) {
902                 if (ca->mi.group) {
903                         mutex_lock(&c->sb_lock);
904                         bch2_disk_path_to_text(&out, &c->disk_sb,
905                                                ca->mi.group - 1);
906                         mutex_unlock(&c->sb_lock);
907                 } else {
908                         pr_buf(&out, "none");
909                 }
910
911                 pr_buf(&out, "\n");
912                 return out.pos - buf;
913         }
914
915         if (attr == &sysfs_has_data) {
916                 bch2_flags_to_text(&out, bch2_data_types,
917                                    bch2_dev_has_data(c, ca));
918                 pr_buf(&out, "\n");
919                 return out.pos - buf;
920         }
921
922         sysfs_pd_controller_show(copy_gc, &ca->copygc_pd);
923
924         if (attr == &sysfs_cache_replacement_policy) {
925                 bch2_string_opt_to_text(&out,
926                                         bch2_cache_replacement_policies,
927                                         ca->mi.replacement);
928                 pr_buf(&out, "\n");
929                 return out.pos - buf;
930         }
931
932         if (attr == &sysfs_state_rw) {
933                 bch2_string_opt_to_text(&out, bch2_dev_state,
934                                         ca->mi.state);
935                 pr_buf(&out, "\n");
936                 return out.pos - buf;
937         }
938
939         if (attr == &sysfs_iodone)
940                 return show_dev_iodone(ca, buf);
941
942         sysfs_print(io_latency_read,            atomic64_read(&ca->cur_latency[READ]));
943         sysfs_print(io_latency_write,           atomic64_read(&ca->cur_latency[WRITE]));
944
945         if (attr == &sysfs_io_latency_stats_read)
946                 return bch2_time_stats_print(&ca->io_latency[READ], buf, PAGE_SIZE);
947         if (attr == &sysfs_io_latency_stats_write)
948                 return bch2_time_stats_print(&ca->io_latency[WRITE], buf, PAGE_SIZE);
949
950         sysfs_printf(congested,                 "%u%%",
951                      clamp(atomic_read(&ca->congested), 0, CONGESTED_MAX)
952                      * 100 / CONGESTED_MAX);
953
954         if (attr == &sysfs_bucket_quantiles_last_read)
955                 return show_quantiles(c, ca, buf, bucket_last_io_fn, (void *) 0);
956         if (attr == &sysfs_bucket_quantiles_last_write)
957                 return show_quantiles(c, ca, buf, bucket_last_io_fn, (void *) 1);
958         if (attr == &sysfs_bucket_quantiles_fragmentation)
959                 return show_quantiles(c, ca, buf, bucket_sectors_used_fn, NULL);
960         if (attr == &sysfs_bucket_quantiles_oldest_gen)
961                 return show_quantiles(c, ca, buf, bucket_oldest_gen_fn, NULL);
962
963         if (attr == &sysfs_reserve_stats)
964                 return show_reserve_stats(ca, buf);
965         if (attr == &sysfs_alloc_debug)
966                 return show_dev_alloc_debug(ca, buf);
967
968         return 0;
969 }
970
971 STORE(bch2_dev)
972 {
973         struct bch_dev *ca = container_of(kobj, struct bch_dev, kobj);
974         struct bch_fs *c = ca->fs;
975         struct bch_member *mi;
976
977         sysfs_pd_controller_store(copy_gc, &ca->copygc_pd);
978
979         if (attr == &sysfs_discard) {
980                 bool v = strtoul_or_return(buf);
981
982                 mutex_lock(&c->sb_lock);
983                 mi = &bch2_sb_get_members(c->disk_sb.sb)->members[ca->dev_idx];
984
985                 if (v != BCH_MEMBER_DISCARD(mi)) {
986                         SET_BCH_MEMBER_DISCARD(mi, v);
987                         bch2_write_super(c);
988                 }
989                 mutex_unlock(&c->sb_lock);
990         }
991
992         if (attr == &sysfs_cache_replacement_policy) {
993                 ssize_t v = __sysfs_match_string(bch2_cache_replacement_policies, -1, buf);
994
995                 if (v < 0)
996                         return v;
997
998                 mutex_lock(&c->sb_lock);
999                 mi = &bch2_sb_get_members(c->disk_sb.sb)->members[ca->dev_idx];
1000
1001                 if ((unsigned) v != BCH_MEMBER_REPLACEMENT(mi)) {
1002                         SET_BCH_MEMBER_REPLACEMENT(mi, v);
1003                         bch2_write_super(c);
1004                 }
1005                 mutex_unlock(&c->sb_lock);
1006         }
1007
1008         if (attr == &sysfs_label) {
1009                 char *tmp;
1010                 int ret;
1011
1012                 tmp = kstrdup(buf, GFP_KERNEL);
1013                 if (!tmp)
1014                         return -ENOMEM;
1015
1016                 ret = bch2_dev_group_set(c, ca, strim(tmp));
1017                 kfree(tmp);
1018                 if (ret)
1019                         return ret;
1020         }
1021
1022         if (attr == &sysfs_wake_allocator)
1023                 bch2_wake_allocator(ca);
1024
1025         return size;
1026 }
1027 SYSFS_OPS(bch2_dev);
1028
1029 struct attribute *bch2_dev_files[] = {
1030         &sysfs_uuid,
1031         &sysfs_bucket_size,
1032         &sysfs_block_size,
1033         &sysfs_first_bucket,
1034         &sysfs_nbuckets,
1035         &sysfs_durability,
1036
1037         /* settings: */
1038         &sysfs_discard,
1039         &sysfs_cache_replacement_policy,
1040         &sysfs_state_rw,
1041         &sysfs_label,
1042
1043         &sysfs_has_data,
1044         &sysfs_iodone,
1045
1046         &sysfs_io_latency_read,
1047         &sysfs_io_latency_write,
1048         &sysfs_io_latency_stats_read,
1049         &sysfs_io_latency_stats_write,
1050         &sysfs_congested,
1051
1052         /* alloc info - other stats: */
1053         &sysfs_bucket_quantiles_last_read,
1054         &sysfs_bucket_quantiles_last_write,
1055         &sysfs_bucket_quantiles_fragmentation,
1056         &sysfs_bucket_quantiles_oldest_gen,
1057
1058         &sysfs_reserve_stats,
1059
1060         /* debug: */
1061         &sysfs_alloc_debug,
1062         &sysfs_wake_allocator,
1063
1064         sysfs_pd_controller_files(copy_gc),
1065         NULL
1066 };
1067
1068 #endif  /* _BCACHEFS_SYSFS_H_ */