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