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