]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/sysfs.c
Update bcachefs sources to 31718a2: bcachefs: Don't spin in journal reclaim
[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 "alloc_foreground.h"
14 #include "sysfs.h"
15 #include "btree_cache.h"
16 #include "btree_io.h"
17 #include "btree_iter.h"
18 #include "btree_key_cache.h"
19 #include "btree_update.h"
20 #include "btree_update_interior.h"
21 #include "btree_gc.h"
22 #include "buckets.h"
23 #include "clock.h"
24 #include "disk_groups.h"
25 #include "ec.h"
26 #include "inode.h"
27 #include "journal.h"
28 #include "keylist.h"
29 #include "move.h"
30 #include "opts.h"
31 #include "rebalance.h"
32 #include "replicas.h"
33 #include "super-io.h"
34 #include "tests.h"
35
36 #include <linux/blkdev.h>
37 #include <linux/sort.h>
38 #include <linux/sched/clock.h>
39
40 #include "util.h"
41
42 #define SYSFS_OPS(type)                                                 \
43 struct sysfs_ops type ## _sysfs_ops = {                                 \
44         .show   = type ## _show,                                        \
45         .store  = type ## _store                                        \
46 }
47
48 #define SHOW(fn)                                                        \
49 static ssize_t fn ## _to_text(struct printbuf *,                        \
50                               struct kobject *, struct attribute *);\
51                                                                         \
52 static ssize_t fn ## _show(struct kobject *kobj, struct attribute *attr,\
53                            char *buf)                                   \
54 {                                                                       \
55         struct printbuf out = PRINTBUF;                                 \
56         ssize_t ret = fn ## _to_text(&out, kobj, attr);                 \
57                                                                         \
58         if (!ret && out.allocation_failure)                             \
59                 ret = -ENOMEM;                                          \
60                                                                         \
61         if (!ret) {                                                     \
62                 ret = min_t(size_t, out.pos, PAGE_SIZE - 1);            \
63                 memcpy(buf, out.buf, ret);                              \
64         }                                                               \
65         printbuf_exit(&out);                                            \
66         return ret;                                                     \
67 }                                                                       \
68                                                                         \
69 static ssize_t fn ## _to_text(struct printbuf *out, struct kobject *kobj,\
70                               struct attribute *attr)
71
72 #define STORE(fn)                                                       \
73 static ssize_t fn ## _store(struct kobject *kobj, struct attribute *attr,\
74                             const char *buf, size_t size)               \
75
76 #define __sysfs_attribute(_name, _mode)                                 \
77         static struct attribute sysfs_##_name =                         \
78                 { .name = #_name, .mode = _mode }
79
80 #define write_attribute(n)      __sysfs_attribute(n, S_IWUSR)
81 #define read_attribute(n)       __sysfs_attribute(n, S_IRUGO)
82 #define rw_attribute(n)         __sysfs_attribute(n, S_IRUGO|S_IWUSR)
83
84 #define sysfs_printf(file, fmt, ...)                                    \
85 do {                                                                    \
86         if (attr == &sysfs_ ## file)                                    \
87                 pr_buf(out, fmt "\n", __VA_ARGS__);                     \
88 } while (0)
89
90 #define sysfs_print(file, var)                                          \
91 do {                                                                    \
92         if (attr == &sysfs_ ## file)                                    \
93                 snprint(out, var);                                      \
94 } while (0)
95
96 #define sysfs_hprint(file, val)                                         \
97 do {                                                                    \
98         if (attr == &sysfs_ ## file)                                    \
99                 bch2_hprint(out, val);                                  \
100 } while (0)
101
102 #define var_printf(_var, fmt)   sysfs_printf(_var, fmt, var(_var))
103 #define var_print(_var)         sysfs_print(_var, var(_var))
104 #define var_hprint(_var)        sysfs_hprint(_var, var(_var))
105
106 #define sysfs_strtoul(file, var)                                        \
107 do {                                                                    \
108         if (attr == &sysfs_ ## file)                                    \
109                 return strtoul_safe(buf, var) ?: (ssize_t) size;        \
110 } while (0)
111
112 #define sysfs_strtoul_clamp(file, var, min, max)                        \
113 do {                                                                    \
114         if (attr == &sysfs_ ## file)                                    \
115                 return strtoul_safe_clamp(buf, var, min, max)           \
116                         ?: (ssize_t) size;                              \
117 } while (0)
118
119 #define strtoul_or_return(cp)                                           \
120 ({                                                                      \
121         unsigned long _v;                                               \
122         int _r = kstrtoul(cp, 10, &_v);                                 \
123         if (_r)                                                         \
124                 return _r;                                              \
125         _v;                                                             \
126 })
127
128 #define strtoul_restrict_or_return(cp, min, max)                        \
129 ({                                                                      \
130         unsigned long __v = 0;                                          \
131         int _r = strtoul_safe_restrict(cp, __v, min, max);              \
132         if (_r)                                                         \
133                 return _r;                                              \
134         __v;                                                            \
135 })
136
137 #define strtoi_h_or_return(cp)                                          \
138 ({                                                                      \
139         u64 _v;                                                         \
140         int _r = strtoi_h(cp, &_v);                                     \
141         if (_r)                                                         \
142                 return _r;                                              \
143         _v;                                                             \
144 })
145
146 #define sysfs_hatoi(file, var)                                          \
147 do {                                                                    \
148         if (attr == &sysfs_ ## file)                                    \
149                 return strtoi_h(buf, &var) ?: (ssize_t) size;           \
150 } while (0)
151
152 write_attribute(trigger_gc);
153 write_attribute(prune_cache);
154 rw_attribute(btree_gc_periodic);
155 rw_attribute(gc_gens_pos);
156
157 read_attribute(uuid);
158 read_attribute(minor);
159 read_attribute(bucket_size);
160 read_attribute(first_bucket);
161 read_attribute(nbuckets);
162 read_attribute(durability);
163 read_attribute(iodone);
164
165 read_attribute(io_latency_read);
166 read_attribute(io_latency_write);
167 read_attribute(io_latency_stats_read);
168 read_attribute(io_latency_stats_write);
169 read_attribute(congested);
170
171 read_attribute(btree_avg_write_size);
172
173 read_attribute(reserve_stats);
174 read_attribute(btree_cache_size);
175 read_attribute(compression_stats);
176 read_attribute(journal_debug);
177 read_attribute(journal_pins);
178 read_attribute(btree_updates);
179 read_attribute(dirty_btree_nodes);
180 read_attribute(btree_cache);
181 read_attribute(btree_key_cache);
182 read_attribute(btree_transactions);
183 read_attribute(stripes_heap);
184 read_attribute(open_buckets);
185
186 read_attribute(internal_uuid);
187
188 read_attribute(has_data);
189 read_attribute(alloc_debug);
190 write_attribute(wake_allocator);
191
192 read_attribute(read_realloc_races);
193 read_attribute(extent_migrate_done);
194 read_attribute(extent_migrate_raced);
195
196 rw_attribute(discard);
197 rw_attribute(label);
198
199 rw_attribute(copy_gc_enabled);
200 read_attribute(copy_gc_wait);
201
202 rw_attribute(rebalance_enabled);
203 sysfs_pd_controller_attribute(rebalance);
204 read_attribute(rebalance_work);
205 rw_attribute(promote_whole_extents);
206
207 read_attribute(new_stripes);
208
209 read_attribute(io_timers_read);
210 read_attribute(io_timers_write);
211
212 read_attribute(data_jobs);
213
214 #ifdef CONFIG_BCACHEFS_TESTS
215 write_attribute(perf_test);
216 #endif /* CONFIG_BCACHEFS_TESTS */
217
218 #define x(_name)                                                \
219         static struct attribute sysfs_time_stat_##_name =               \
220                 { .name = #_name, .mode = S_IRUGO };
221         BCH_TIME_STATS()
222 #undef x
223
224 static struct attribute sysfs_state_rw = {
225         .name = "state",
226         .mode = S_IRUGO
227 };
228
229 static size_t bch2_btree_cache_size(struct bch_fs *c)
230 {
231         size_t ret = 0;
232         struct btree *b;
233
234         mutex_lock(&c->btree_cache.lock);
235         list_for_each_entry(b, &c->btree_cache.live, list)
236                 ret += btree_bytes(c);
237
238         mutex_unlock(&c->btree_cache.lock);
239         return ret;
240 }
241
242 static size_t bch2_btree_avg_write_size(struct bch_fs *c)
243 {
244         u64 nr = atomic64_read(&c->btree_writes_nr);
245         u64 sectors = atomic64_read(&c->btree_writes_sectors);
246
247         return nr ? div64_u64(sectors, nr) : 0;
248 }
249
250 static long data_progress_to_text(struct printbuf *out, struct bch_fs *c)
251 {
252         long ret = 0;
253         struct bch_move_stats *stats;
254
255         mutex_lock(&c->data_progress_lock);
256         list_for_each_entry(stats, &c->data_progress_list, list) {
257                 pr_buf(out, "%s: data type %s btree_id %s position: ",
258                        stats->name,
259                        bch2_data_types[stats->data_type],
260                        bch2_btree_ids[stats->btree_id]);
261                 bch2_bpos_to_text(out, stats->pos);
262                 pr_buf(out, "%s", "\n");
263         }
264
265         mutex_unlock(&c->data_progress_lock);
266         return ret;
267 }
268
269 static int bch2_compression_stats_to_text(struct printbuf *out, struct bch_fs *c)
270 {
271         struct btree_trans trans;
272         struct btree_iter iter;
273         struct bkey_s_c k;
274         enum btree_id id;
275         u64 nr_uncompressed_extents = 0,
276             nr_compressed_extents = 0,
277             nr_incompressible_extents = 0,
278             uncompressed_sectors = 0,
279             incompressible_sectors = 0,
280             compressed_sectors_compressed = 0,
281             compressed_sectors_uncompressed = 0;
282         int ret;
283
284         if (!test_bit(BCH_FS_STARTED, &c->flags))
285                 return -EPERM;
286
287         bch2_trans_init(&trans, c, 0, 0);
288
289         for (id = 0; id < BTREE_ID_NR; id++) {
290                 if (!((1U << id) & BTREE_ID_HAS_PTRS))
291                         continue;
292
293                 for_each_btree_key(&trans, iter, id, POS_MIN,
294                                    BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
295                         struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
296                         const union bch_extent_entry *entry;
297                         struct extent_ptr_decoded p;
298                         bool compressed = false, uncompressed = false, incompressible = false;
299
300                         bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
301                                 switch (p.crc.compression_type) {
302                                 case BCH_COMPRESSION_TYPE_none:
303                                         uncompressed = true;
304                                         uncompressed_sectors += k.k->size;
305                                         break;
306                                 case BCH_COMPRESSION_TYPE_incompressible:
307                                         incompressible = true;
308                                         incompressible_sectors += k.k->size;
309                                         break;
310                                 default:
311                                         compressed_sectors_compressed +=
312                                                 p.crc.compressed_size;
313                                         compressed_sectors_uncompressed +=
314                                                 p.crc.uncompressed_size;
315                                         compressed = true;
316                                         break;
317                                 }
318                         }
319
320                         if (incompressible)
321                                 nr_incompressible_extents++;
322                         else if (uncompressed)
323                                 nr_uncompressed_extents++;
324                         else if (compressed)
325                                 nr_compressed_extents++;
326                 }
327                 bch2_trans_iter_exit(&trans, &iter);
328         }
329
330         bch2_trans_exit(&trans);
331
332         if (ret)
333                 return ret;
334
335         pr_buf(out, "uncompressed:\n");
336         pr_buf(out, "   nr extents:             %llu\n", nr_uncompressed_extents);
337         pr_buf(out, "   size:                   ");
338         bch2_hprint(out, uncompressed_sectors << 9);
339         pr_buf(out, "\n");
340
341         pr_buf(out, "compressed:\n");
342         pr_buf(out, "   nr extents:             %llu\n", nr_compressed_extents);
343         pr_buf(out, "   compressed size:        ");
344         bch2_hprint(out, compressed_sectors_compressed << 9);
345         pr_buf(out, "\n");
346         pr_buf(out, "   uncompressed size:      ");
347         bch2_hprint(out, compressed_sectors_uncompressed << 9);
348         pr_buf(out, "\n");
349
350         pr_buf(out, "incompressible:\n");
351         pr_buf(out, "   nr extents:             %llu\n", nr_incompressible_extents);
352         pr_buf(out, "   size:                   ");
353         bch2_hprint(out, incompressible_sectors << 9);
354         pr_buf(out, "\n");
355         return 0;
356 }
357
358 static void bch2_gc_gens_pos_to_text(struct printbuf *out, struct bch_fs *c)
359 {
360         pr_buf(out, "%s: ", bch2_btree_ids[c->gc_gens_btree]);
361         bch2_bpos_to_text(out, c->gc_gens_pos);
362         pr_buf(out, "\n");
363 }
364
365 SHOW(bch2_fs)
366 {
367         struct bch_fs *c = container_of(kobj, struct bch_fs, kobj);
368
369         sysfs_print(minor,                      c->minor);
370         sysfs_printf(internal_uuid, "%pU",      c->sb.uuid.b);
371
372         sysfs_hprint(btree_cache_size,          bch2_btree_cache_size(c));
373         sysfs_hprint(btree_avg_write_size,      bch2_btree_avg_write_size(c));
374
375         sysfs_print(read_realloc_races,
376                     atomic_long_read(&c->read_realloc_races));
377         sysfs_print(extent_migrate_done,
378                     atomic_long_read(&c->extent_migrate_done));
379         sysfs_print(extent_migrate_raced,
380                     atomic_long_read(&c->extent_migrate_raced));
381
382         sysfs_printf(btree_gc_periodic, "%u",   (int) c->btree_gc_periodic);
383
384         if (attr == &sysfs_gc_gens_pos)
385                 bch2_gc_gens_pos_to_text(out, c);
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
398         sysfs_print(promote_whole_extents,      c->promote_whole_extents);
399
400         /* Debugging: */
401
402         if (attr == &sysfs_journal_debug)
403                 bch2_journal_debug_to_text(out, &c->journal);
404
405         if (attr == &sysfs_journal_pins)
406                 bch2_journal_pins_to_text(out, &c->journal);
407
408         if (attr == &sysfs_btree_updates)
409                 bch2_btree_updates_to_text(out, c);
410
411         if (attr == &sysfs_dirty_btree_nodes)
412                 bch2_dirty_btree_nodes_to_text(out, c);
413
414         if (attr == &sysfs_btree_cache)
415                 bch2_btree_cache_to_text(out, c);
416
417         if (attr == &sysfs_btree_key_cache)
418                 bch2_btree_key_cache_to_text(out, &c->btree_key_cache);
419
420         if (attr == &sysfs_btree_transactions)
421                 bch2_btree_trans_to_text(out, c);
422
423         if (attr == &sysfs_stripes_heap)
424                 bch2_stripes_heap_to_text(out, c);
425
426         if (attr == &sysfs_open_buckets)
427                 bch2_open_buckets_to_text(out, c);
428
429         if (attr == &sysfs_compression_stats)
430                 bch2_compression_stats_to_text(out, c);
431
432         if (attr == &sysfs_new_stripes)
433                 bch2_new_stripes_to_text(out, c);
434
435         if (attr == &sysfs_io_timers_read)
436                 bch2_io_timers_to_text(out, &c->io_clock[READ]);
437
438         if (attr == &sysfs_io_timers_write)
439                 bch2_io_timers_to_text(out, &c->io_clock[WRITE]);
440
441         if (attr == &sysfs_data_jobs)
442                 data_progress_to_text(out, c);
443
444         return 0;
445 }
446
447 STORE(bch2_fs)
448 {
449         struct bch_fs *c = container_of(kobj, struct bch_fs, kobj);
450
451         if (attr == &sysfs_btree_gc_periodic) {
452                 ssize_t ret = strtoul_safe(buf, c->btree_gc_periodic)
453                         ?: (ssize_t) size;
454
455                 wake_up_process(c->gc_thread);
456                 return ret;
457         }
458
459         if (attr == &sysfs_copy_gc_enabled) {
460                 ssize_t ret = strtoul_safe(buf, c->copy_gc_enabled)
461                         ?: (ssize_t) size;
462
463                 if (c->copygc_thread)
464                         wake_up_process(c->copygc_thread);
465                 return ret;
466         }
467
468         if (attr == &sysfs_rebalance_enabled) {
469                 ssize_t ret = strtoul_safe(buf, c->rebalance.enabled)
470                         ?: (ssize_t) size;
471
472                 rebalance_wakeup(c);
473                 return ret;
474         }
475
476         sysfs_pd_controller_store(rebalance,    &c->rebalance.pd);
477
478         sysfs_strtoul(promote_whole_extents,    c->promote_whole_extents);
479
480         /* Debugging: */
481
482         if (!test_bit(BCH_FS_STARTED, &c->flags))
483                 return -EPERM;
484
485         /* Debugging: */
486
487         if (!test_bit(BCH_FS_RW, &c->flags))
488                 return -EROFS;
489
490         if (attr == &sysfs_prune_cache) {
491                 struct shrink_control sc;
492
493                 sc.gfp_mask = GFP_KERNEL;
494                 sc.nr_to_scan = strtoul_or_return(buf);
495                 c->btree_cache.shrink.scan_objects(&c->btree_cache.shrink, &sc);
496         }
497
498         if (attr == &sysfs_trigger_gc) {
499                 /*
500                  * Full gc is currently incompatible with btree key cache:
501                  */
502 #if 0
503                 down_read(&c->state_lock);
504                 bch2_gc(c, false, false);
505                 up_read(&c->state_lock);
506 #else
507                 bch2_gc_gens(c);
508 #endif
509         }
510
511 #ifdef CONFIG_BCACHEFS_TESTS
512         if (attr == &sysfs_perf_test) {
513                 char *tmp = kstrdup(buf, GFP_KERNEL), *p = tmp;
514                 char *test              = strsep(&p, " \t\n");
515                 char *nr_str            = strsep(&p, " \t\n");
516                 char *threads_str       = strsep(&p, " \t\n");
517                 unsigned threads;
518                 u64 nr;
519                 int ret = -EINVAL;
520
521                 if (threads_str &&
522                     !(ret = kstrtouint(threads_str, 10, &threads)) &&
523                     !(ret = bch2_strtoull_h(nr_str, &nr)))
524                         ret = bch2_btree_perf_test(c, test, nr, threads);
525                 kfree(tmp);
526
527                 if (ret)
528                         size = ret;
529         }
530 #endif
531         return size;
532 }
533 SYSFS_OPS(bch2_fs);
534
535 struct attribute *bch2_fs_files[] = {
536         &sysfs_minor,
537         &sysfs_btree_cache_size,
538         &sysfs_btree_avg_write_size,
539
540         &sysfs_promote_whole_extents,
541
542         &sysfs_compression_stats,
543
544 #ifdef CONFIG_BCACHEFS_TESTS
545         &sysfs_perf_test,
546 #endif
547         NULL
548 };
549
550 /* internal dir - just a wrapper */
551
552 SHOW(bch2_fs_internal)
553 {
554         struct bch_fs *c = container_of(kobj, struct bch_fs, internal);
555         return bch2_fs_to_text(out, &c->kobj, attr);
556 }
557
558 STORE(bch2_fs_internal)
559 {
560         struct bch_fs *c = container_of(kobj, struct bch_fs, internal);
561         return bch2_fs_store(&c->kobj, attr, buf, size);
562 }
563 SYSFS_OPS(bch2_fs_internal);
564
565 struct attribute *bch2_fs_internal_files[] = {
566         &sysfs_journal_debug,
567         &sysfs_journal_pins,
568         &sysfs_btree_updates,
569         &sysfs_dirty_btree_nodes,
570         &sysfs_btree_cache,
571         &sysfs_btree_key_cache,
572         &sysfs_btree_transactions,
573         &sysfs_new_stripes,
574         &sysfs_stripes_heap,
575         &sysfs_open_buckets,
576         &sysfs_io_timers_read,
577         &sysfs_io_timers_write,
578
579         &sysfs_trigger_gc,
580         &sysfs_prune_cache,
581
582         &sysfs_read_realloc_races,
583         &sysfs_extent_migrate_done,
584         &sysfs_extent_migrate_raced,
585
586         &sysfs_gc_gens_pos,
587
588         &sysfs_copy_gc_enabled,
589         &sysfs_copy_gc_wait,
590
591         &sysfs_rebalance_enabled,
592         &sysfs_rebalance_work,
593         sysfs_pd_controller_files(rebalance),
594
595         &sysfs_data_jobs,
596
597         &sysfs_internal_uuid,
598         NULL
599 };
600
601 /* options */
602
603 SHOW(bch2_fs_opts_dir)
604 {
605         struct bch_fs *c = container_of(kobj, struct bch_fs, opts_dir);
606         const struct bch_option *opt = container_of(attr, struct bch_option, attr);
607         int id = opt - bch2_opt_table;
608         u64 v = bch2_opt_get_by_id(&c->opts, id);
609
610         bch2_opt_to_text(out, c, opt, v, OPT_SHOW_FULL_LIST);
611         pr_char(out, '\n');
612
613         return 0;
614 }
615
616 STORE(bch2_fs_opts_dir)
617 {
618         struct bch_fs *c = container_of(kobj, struct bch_fs, opts_dir);
619         const struct bch_option *opt = container_of(attr, struct bch_option, attr);
620         int ret, id = opt - bch2_opt_table;
621         char *tmp;
622         u64 v;
623
624         tmp = kstrdup(buf, GFP_KERNEL);
625         if (!tmp)
626                 return -ENOMEM;
627
628         ret = bch2_opt_parse(c, NULL, opt, strim(tmp), &v);
629         kfree(tmp);
630
631         if (ret < 0)
632                 return ret;
633
634         ret = bch2_opt_check_may_set(c, id, v);
635         if (ret < 0)
636                 return ret;
637
638         bch2_opt_set_sb(c, opt, v);
639         bch2_opt_set_by_id(&c->opts, id, v);
640
641         if ((id == Opt_background_target ||
642              id == Opt_background_compression) && v) {
643                 bch2_rebalance_add_work(c, S64_MAX);
644                 rebalance_wakeup(c);
645         }
646
647         return size;
648 }
649 SYSFS_OPS(bch2_fs_opts_dir);
650
651 struct attribute *bch2_fs_opts_dir_files[] = { NULL };
652
653 int bch2_opts_create_sysfs_files(struct kobject *kobj)
654 {
655         const struct bch_option *i;
656         int ret;
657
658         for (i = bch2_opt_table;
659              i < bch2_opt_table + bch2_opts_nr;
660              i++) {
661                 if (!(i->flags & OPT_FS))
662                         continue;
663
664                 ret = sysfs_create_file(kobj, &i->attr);
665                 if (ret)
666                         return ret;
667         }
668
669         return 0;
670 }
671
672 /* time stats */
673
674 SHOW(bch2_fs_time_stats)
675 {
676         struct bch_fs *c = container_of(kobj, struct bch_fs, time_stats);
677
678 #define x(name)                                                         \
679         if (attr == &sysfs_time_stat_##name)                            \
680                 bch2_time_stats_to_text(out, &c->times[BCH_TIME_##name]);
681         BCH_TIME_STATS()
682 #undef x
683
684         return 0;
685 }
686
687 STORE(bch2_fs_time_stats)
688 {
689         return size;
690 }
691 SYSFS_OPS(bch2_fs_time_stats);
692
693 struct attribute *bch2_fs_time_stats_files[] = {
694 #define x(name)                                         \
695         &sysfs_time_stat_##name,
696         BCH_TIME_STATS()
697 #undef x
698         NULL
699 };
700
701 static void reserve_stats_to_text(struct printbuf *out, struct bch_dev *ca)
702 {
703         enum alloc_reserve i;
704
705         spin_lock(&ca->fs->freelist_lock);
706
707         pr_buf(out, "free_inc:\t%zu\t%zu\n",
708                fifo_used(&ca->free_inc),
709                ca->free_inc.size);
710
711         for (i = 0; i < RESERVE_NR; i++)
712                 pr_buf(out, "free[%u]:\t%zu\t%zu\n", i,
713                        fifo_used(&ca->free[i]),
714                        ca->free[i].size);
715
716         spin_unlock(&ca->fs->freelist_lock);
717 }
718
719 static void dev_alloc_debug_to_text(struct printbuf *out, struct bch_dev *ca)
720 {
721         struct bch_fs *c = ca->fs;
722         struct bch_dev_usage stats = bch2_dev_usage_read(ca);
723         unsigned i, nr[BCH_DATA_NR];
724
725         memset(nr, 0, sizeof(nr));
726
727         for (i = 0; i < ARRAY_SIZE(c->open_buckets); i++)
728                 nr[c->open_buckets[i].data_type]++;
729
730         pr_buf(out,
731                "\t\t buckets\t sectors      fragmented\n"
732                "capacity%16llu\n",
733                ca->mi.nbuckets - ca->mi.first_bucket);
734
735         for (i = 1; i < BCH_DATA_NR; i++)
736                 pr_buf(out, "%-8s%16llu%16llu%16llu\n",
737                        bch2_data_types[i], stats.d[i].buckets,
738                        stats.d[i].sectors, stats.d[i].fragmented);
739
740         pr_buf(out,
741                "ec\t%16llu\n"
742                "available%15llu\n"
743                "\n"
744                "free_inc\t\t%zu/%zu\n"
745                "free[RESERVE_MOVINGGC]\t%zu/%zu\n"
746                "free[RESERVE_NONE]\t%zu/%zu\n"
747                "freelist_wait\t\t%s\n"
748                "open buckets allocated\t%u\n"
749                "open buckets this dev\t%u\n"
750                "open buckets total\t%u\n"
751                "open_buckets_wait\t%s\n"
752                "open_buckets_btree\t%u\n"
753                "open_buckets_user\t%u\n"
754                "btree reserve cache\t%u\n"
755                "thread state:\t\t%s\n",
756                stats.buckets_ec,
757                __dev_buckets_available(ca, stats),
758                fifo_used(&ca->free_inc),                ca->free_inc.size,
759                fifo_used(&ca->free[RESERVE_MOVINGGC]),  ca->free[RESERVE_MOVINGGC].size,
760                fifo_used(&ca->free[RESERVE_NONE]),      ca->free[RESERVE_NONE].size,
761                c->freelist_wait.list.first              ? "waiting" : "empty",
762                OPEN_BUCKETS_COUNT - c->open_buckets_nr_free,
763                ca->nr_open_buckets,
764                OPEN_BUCKETS_COUNT,
765                c->open_buckets_wait.list.first          ? "waiting" : "empty",
766                nr[BCH_DATA_btree],
767                nr[BCH_DATA_user],
768                c->btree_reserve_cache_nr,
769                bch2_allocator_states[ca->allocator_state]);
770 }
771
772 static const char * const bch2_rw[] = {
773         "read",
774         "write",
775         NULL
776 };
777
778 static void dev_iodone_to_text(struct printbuf *out, struct bch_dev *ca)
779 {
780         int rw, i;
781
782         for (rw = 0; rw < 2; rw++) {
783                 pr_buf(out, "%s:\n", bch2_rw[rw]);
784
785                 for (i = 1; i < BCH_DATA_NR; i++)
786                         pr_buf(out, "%-12s:%12llu\n",
787                                bch2_data_types[i],
788                                percpu_u64_get(&ca->io_done->sectors[rw][i]) << 9);
789         }
790 }
791
792 SHOW(bch2_dev)
793 {
794         struct bch_dev *ca = container_of(kobj, struct bch_dev, kobj);
795         struct bch_fs *c = ca->fs;
796
797         sysfs_printf(uuid,              "%pU\n", ca->uuid.b);
798
799         sysfs_print(bucket_size,        bucket_bytes(ca));
800         sysfs_print(first_bucket,       ca->mi.first_bucket);
801         sysfs_print(nbuckets,           ca->mi.nbuckets);
802         sysfs_print(durability,         ca->mi.durability);
803         sysfs_print(discard,            ca->mi.discard);
804
805         if (attr == &sysfs_label) {
806                 if (ca->mi.group) {
807                         mutex_lock(&c->sb_lock);
808                         bch2_disk_path_to_text(out, c->disk_sb.sb,
809                                                ca->mi.group - 1);
810                         mutex_unlock(&c->sb_lock);
811                 }
812
813                 pr_char(out, '\n');
814         }
815
816         if (attr == &sysfs_has_data) {
817                 bch2_flags_to_text(out, bch2_data_types,
818                                    bch2_dev_has_data(c, ca));
819                 pr_char(out, '\n');
820         }
821
822         if (attr == &sysfs_state_rw) {
823                 bch2_string_opt_to_text(out, bch2_member_states,
824                                         ca->mi.state);
825                 pr_char(out, '\n');
826         }
827
828         if (attr == &sysfs_iodone)
829                 dev_iodone_to_text(out, ca);
830
831         sysfs_print(io_latency_read,            atomic64_read(&ca->cur_latency[READ]));
832         sysfs_print(io_latency_write,           atomic64_read(&ca->cur_latency[WRITE]));
833
834         if (attr == &sysfs_io_latency_stats_read)
835                 bch2_time_stats_to_text(out, &ca->io_latency[READ]);
836
837         if (attr == &sysfs_io_latency_stats_write)
838                 bch2_time_stats_to_text(out, &ca->io_latency[WRITE]);
839
840         sysfs_printf(congested,                 "%u%%",
841                      clamp(atomic_read(&ca->congested), 0, CONGESTED_MAX)
842                      * 100 / CONGESTED_MAX);
843
844         if (attr == &sysfs_reserve_stats)
845                 reserve_stats_to_text(out, ca);
846
847         if (attr == &sysfs_alloc_debug)
848                 dev_alloc_debug_to_text(out, ca);
849
850         return 0;
851 }
852
853 STORE(bch2_dev)
854 {
855         struct bch_dev *ca = container_of(kobj, struct bch_dev, kobj);
856         struct bch_fs *c = ca->fs;
857         struct bch_member *mi;
858
859         if (attr == &sysfs_discard) {
860                 bool v = strtoul_or_return(buf);
861
862                 mutex_lock(&c->sb_lock);
863                 mi = &bch2_sb_get_members(c->disk_sb.sb)->members[ca->dev_idx];
864
865                 if (v != BCH_MEMBER_DISCARD(mi)) {
866                         SET_BCH_MEMBER_DISCARD(mi, v);
867                         bch2_write_super(c);
868                 }
869                 mutex_unlock(&c->sb_lock);
870         }
871
872         if (attr == &sysfs_label) {
873                 char *tmp;
874                 int ret;
875
876                 tmp = kstrdup(buf, GFP_KERNEL);
877                 if (!tmp)
878                         return -ENOMEM;
879
880                 ret = bch2_dev_group_set(c, ca, strim(tmp));
881                 kfree(tmp);
882                 if (ret)
883                         return ret;
884         }
885
886         if (attr == &sysfs_wake_allocator)
887                 bch2_wake_allocator(ca);
888
889         return size;
890 }
891 SYSFS_OPS(bch2_dev);
892
893 struct attribute *bch2_dev_files[] = {
894         &sysfs_uuid,
895         &sysfs_bucket_size,
896         &sysfs_first_bucket,
897         &sysfs_nbuckets,
898         &sysfs_durability,
899
900         /* settings: */
901         &sysfs_discard,
902         &sysfs_state_rw,
903         &sysfs_label,
904
905         &sysfs_has_data,
906         &sysfs_iodone,
907
908         &sysfs_io_latency_read,
909         &sysfs_io_latency_write,
910         &sysfs_io_latency_stats_read,
911         &sysfs_io_latency_stats_write,
912         &sysfs_congested,
913
914         &sysfs_reserve_stats,
915
916         /* debug: */
917         &sysfs_alloc_debug,
918         &sysfs_wake_allocator,
919         NULL
920 };
921
922 #endif  /* _BCACHEFS_SYSFS_H_ */