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