]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/sysfs.c
Update bcachefs sources to ff3a76e1af bcachefs: Change need_whiteout_for_snapshot...
[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(first_bucket);
144 read_attribute(nbuckets);
145 read_attribute(durability);
146 read_attribute(iodone);
147
148 read_attribute(io_latency_read);
149 read_attribute(io_latency_write);
150 read_attribute(io_latency_stats_read);
151 read_attribute(io_latency_stats_write);
152 read_attribute(congested);
153
154 read_attribute(btree_avg_write_size);
155
156 read_attribute(reserve_stats);
157 read_attribute(btree_cache_size);
158 read_attribute(compression_stats);
159 read_attribute(journal_debug);
160 read_attribute(journal_pins);
161 read_attribute(btree_updates);
162 read_attribute(dirty_btree_nodes);
163 read_attribute(btree_cache);
164 read_attribute(btree_key_cache);
165 read_attribute(btree_transactions);
166 read_attribute(stripes_heap);
167 read_attribute(open_buckets);
168
169 read_attribute(internal_uuid);
170
171 read_attribute(has_data);
172 read_attribute(alloc_debug);
173 write_attribute(wake_allocator);
174
175 read_attribute(read_realloc_races);
176 read_attribute(extent_migrate_done);
177 read_attribute(extent_migrate_raced);
178
179 rw_attribute(discard);
180 rw_attribute(cache_replacement_policy);
181 rw_attribute(label);
182
183 rw_attribute(copy_gc_enabled);
184 read_attribute(copy_gc_wait);
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 read_attribute(new_stripes);
192
193 read_attribute(io_timers_read);
194 read_attribute(io_timers_write);
195
196 read_attribute(data_op_data_progress);
197
198 #ifdef CONFIG_BCACHEFS_TESTS
199 write_attribute(perf_test);
200 #endif /* CONFIG_BCACHEFS_TESTS */
201
202 #define x(_name)                                                \
203         static struct attribute sysfs_time_stat_##_name =               \
204                 { .name = #_name, .mode = S_IRUGO };
205         BCH_TIME_STATS()
206 #undef x
207
208 static struct attribute sysfs_state_rw = {
209         .name = "state",
210         .mode = S_IRUGO
211 };
212
213 static size_t bch2_btree_cache_size(struct bch_fs *c)
214 {
215         size_t ret = 0;
216         struct btree *b;
217
218         mutex_lock(&c->btree_cache.lock);
219         list_for_each_entry(b, &c->btree_cache.live, list)
220                 ret += btree_bytes(c);
221
222         mutex_unlock(&c->btree_cache.lock);
223         return ret;
224 }
225
226 static size_t bch2_btree_avg_write_size(struct bch_fs *c)
227 {
228         u64 nr = atomic64_read(&c->btree_writes_nr);
229         u64 sectors = atomic64_read(&c->btree_writes_sectors);
230
231         return nr ? div64_u64(sectors, nr) : 0;
232 }
233
234 static long stats_to_text(struct printbuf *out, struct bch_fs *c,
235                           struct bch_move_stats *stats)
236 {
237         pr_buf(out, "%s: data type %s btree_id %s position: ",
238                 stats->name,
239                 bch2_data_types[stats->data_type],
240                 bch2_btree_ids[stats->btree_id]);
241         bch2_bpos_to_text(out, stats->pos);
242         pr_buf(out, "%s", "\n");
243
244         return 0;
245 }
246
247 static long data_progress_to_text(struct printbuf *out, struct bch_fs *c)
248 {
249         long ret = 0;
250         struct bch_move_stats *iter;
251
252         mutex_lock(&c->data_progress_lock);
253
254         if (list_empty(&c->data_progress_list))
255                 pr_buf(out, "%s", "no progress to report\n");
256         else
257                 list_for_each_entry(iter, &c->data_progress_list, list) {
258                         stats_to_text(out, c, iter);
259                 }
260
261         mutex_unlock(&c->data_progress_lock);
262         return ret;
263 }
264
265 static int bch2_compression_stats_to_text(struct printbuf *out, struct bch_fs *c)
266 {
267         struct btree_trans trans;
268         struct btree_iter iter;
269         struct bkey_s_c k;
270         u64 nr_uncompressed_extents = 0, uncompressed_sectors = 0,
271             nr_compressed_extents = 0,
272             compressed_sectors_compressed = 0,
273             compressed_sectors_uncompressed = 0;
274         int ret;
275
276         if (!test_bit(BCH_FS_STARTED, &c->flags))
277                 return -EPERM;
278
279         bch2_trans_init(&trans, c, 0, 0);
280
281         for_each_btree_key(&trans, iter, BTREE_ID_extents, POS_MIN, 0, k, ret)
282                 if (k.k->type == KEY_TYPE_extent) {
283                         struct bkey_s_c_extent e = bkey_s_c_to_extent(k);
284                         const union bch_extent_entry *entry;
285                         struct extent_ptr_decoded p;
286
287                         extent_for_each_ptr_decode(e, p, entry) {
288                                 if (!crc_is_compressed(p.crc)) {
289                                         nr_uncompressed_extents++;
290                                         uncompressed_sectors += e.k->size;
291                                 } else {
292                                         nr_compressed_extents++;
293                                         compressed_sectors_compressed +=
294                                                 p.crc.compressed_size;
295                                         compressed_sectors_uncompressed +=
296                                                 p.crc.uncompressed_size;
297                                 }
298
299                                 /* only looking at the first ptr */
300                                 break;
301                         }
302                 }
303         bch2_trans_iter_exit(&trans, &iter);
304
305         bch2_trans_exit(&trans);
306         if (ret)
307                 return ret;
308
309         pr_buf(out,
310                "uncompressed data:\n"
311                "        nr extents:                     %llu\n"
312                "        size (bytes):                   %llu\n"
313                "compressed data:\n"
314                "        nr extents:                     %llu\n"
315                "        compressed size (bytes):        %llu\n"
316                "        uncompressed size (bytes):      %llu\n",
317                nr_uncompressed_extents,
318                uncompressed_sectors << 9,
319                nr_compressed_extents,
320                compressed_sectors_compressed << 9,
321                compressed_sectors_uncompressed << 9);
322         return 0;
323 }
324
325 static void bch2_gc_gens_pos_to_text(struct printbuf *out, struct bch_fs *c)
326 {
327         pr_buf(out, "%s: ", bch2_btree_ids[c->gc_gens_btree]);
328         bch2_bpos_to_text(out, c->gc_gens_pos);
329         pr_buf(out, "\n");
330 }
331
332 SHOW(bch2_fs)
333 {
334         struct bch_fs *c = container_of(kobj, struct bch_fs, kobj);
335         struct printbuf out = _PBUF(buf, PAGE_SIZE);
336
337         sysfs_print(minor,                      c->minor);
338         sysfs_printf(internal_uuid, "%pU",      c->sb.uuid.b);
339
340         sysfs_hprint(btree_cache_size,          bch2_btree_cache_size(c));
341         sysfs_hprint(btree_avg_write_size,      bch2_btree_avg_write_size(c));
342
343         sysfs_print(read_realloc_races,
344                     atomic_long_read(&c->read_realloc_races));
345         sysfs_print(extent_migrate_done,
346                     atomic_long_read(&c->extent_migrate_done));
347         sysfs_print(extent_migrate_raced,
348                     atomic_long_read(&c->extent_migrate_raced));
349
350         sysfs_printf(btree_gc_periodic, "%u",   (int) c->btree_gc_periodic);
351
352         if (attr == &sysfs_gc_gens_pos) {
353                 bch2_gc_gens_pos_to_text(&out, c);
354                 return out.pos - buf;
355         }
356
357         sysfs_printf(copy_gc_enabled, "%i", c->copy_gc_enabled);
358
359         sysfs_printf(rebalance_enabled,         "%i", c->rebalance.enabled);
360         sysfs_pd_controller_show(rebalance,     &c->rebalance.pd); /* XXX */
361         sysfs_hprint(copy_gc_wait,
362                      max(0LL, c->copygc_wait -
363                          atomic64_read(&c->io_clock[WRITE].now)) << 9);
364
365         if (attr == &sysfs_rebalance_work) {
366                 bch2_rebalance_work_to_text(&out, c);
367                 return out.pos - buf;
368         }
369
370         sysfs_print(promote_whole_extents,      c->promote_whole_extents);
371
372         /* Debugging: */
373
374         if (attr == &sysfs_journal_debug) {
375                 bch2_journal_debug_to_text(&out, &c->journal);
376                 return out.pos - buf;
377         }
378
379         if (attr == &sysfs_journal_pins) {
380                 bch2_journal_pins_to_text(&out, &c->journal);
381                 return out.pos - buf;
382         }
383
384         if (attr == &sysfs_btree_updates) {
385                 bch2_btree_updates_to_text(&out, c);
386                 return out.pos - buf;
387         }
388
389         if (attr == &sysfs_dirty_btree_nodes) {
390                 bch2_dirty_btree_nodes_to_text(&out, c);
391                 return out.pos - buf;
392         }
393
394         if (attr == &sysfs_btree_cache) {
395                 bch2_btree_cache_to_text(&out, c);
396                 return out.pos - buf;
397         }
398
399         if (attr == &sysfs_btree_key_cache) {
400                 bch2_btree_key_cache_to_text(&out, &c->btree_key_cache);
401                 return out.pos - buf;
402         }
403
404         if (attr == &sysfs_btree_transactions) {
405                 bch2_btree_trans_to_text(&out, c);
406                 return out.pos - buf;
407         }
408
409         if (attr == &sysfs_stripes_heap) {
410                 bch2_stripes_heap_to_text(&out, c);
411                 return out.pos - buf;
412         }
413
414         if (attr == &sysfs_open_buckets) {
415                 bch2_open_buckets_to_text(&out, c);
416                 return out.pos - buf;
417         }
418
419         if (attr == &sysfs_compression_stats) {
420                 bch2_compression_stats_to_text(&out, c);
421                 return out.pos - buf;
422         }
423
424         if (attr == &sysfs_new_stripes) {
425                 bch2_new_stripes_to_text(&out, c);
426                 return out.pos - buf;
427         }
428
429         if (attr == &sysfs_io_timers_read) {
430                 bch2_io_timers_to_text(&out, &c->io_clock[READ]);
431                 return out.pos - buf;
432         }
433         if (attr == &sysfs_io_timers_write) {
434                 bch2_io_timers_to_text(&out, &c->io_clock[WRITE]);
435                 return out.pos - buf;
436         }
437
438         if (attr == &sysfs_data_op_data_progress) {
439                 data_progress_to_text(&out, c);
440                 return out.pos - buf;
441         }
442
443         return 0;
444 }
445
446 STORE(bch2_fs)
447 {
448         struct bch_fs *c = container_of(kobj, struct bch_fs, kobj);
449
450         if (attr == &sysfs_btree_gc_periodic) {
451                 ssize_t ret = strtoul_safe(buf, c->btree_gc_periodic)
452                         ?: (ssize_t) size;
453
454                 wake_up_process(c->gc_thread);
455                 return ret;
456         }
457
458         if (attr == &sysfs_copy_gc_enabled) {
459                 ssize_t ret = strtoul_safe(buf, c->copy_gc_enabled)
460                         ?: (ssize_t) size;
461
462                 if (c->copygc_thread)
463                         wake_up_process(c->copygc_thread);
464                 return ret;
465         }
466
467         if (attr == &sysfs_rebalance_enabled) {
468                 ssize_t ret = strtoul_safe(buf, c->rebalance.enabled)
469                         ?: (ssize_t) size;
470
471                 rebalance_wakeup(c);
472                 return ret;
473         }
474
475         sysfs_pd_controller_store(rebalance,    &c->rebalance.pd);
476
477         sysfs_strtoul(promote_whole_extents,    c->promote_whole_extents);
478
479         /* Debugging: */
480
481         if (!test_bit(BCH_FS_STARTED, &c->flags))
482                 return -EPERM;
483
484         /* Debugging: */
485
486         if (attr == &sysfs_trigger_journal_flush)
487                 bch2_journal_meta(&c->journal);
488
489         if (attr == &sysfs_trigger_gc) {
490                 /*
491                  * Full gc is currently incompatible with btree key cache:
492                  */
493 #if 0
494                 down_read(&c->state_lock);
495                 bch2_gc(c, false, false);
496                 up_read(&c->state_lock);
497 #else
498                 bch2_gc_gens(c);
499 #endif
500         }
501
502         if (attr == &sysfs_prune_cache) {
503                 struct shrink_control sc;
504
505                 sc.gfp_mask = GFP_KERNEL;
506                 sc.nr_to_scan = strtoul_or_return(buf);
507                 c->btree_cache.shrink.scan_objects(&c->btree_cache.shrink, &sc);
508         }
509
510 #ifdef CONFIG_BCACHEFS_TESTS
511         if (attr == &sysfs_perf_test) {
512                 char *tmp = kstrdup(buf, GFP_KERNEL), *p = tmp;
513                 char *test              = strsep(&p, " \t\n");
514                 char *nr_str            = strsep(&p, " \t\n");
515                 char *threads_str       = strsep(&p, " \t\n");
516                 unsigned threads;
517                 u64 nr;
518                 int ret = -EINVAL;
519
520                 if (threads_str &&
521                     !(ret = kstrtouint(threads_str, 10, &threads)) &&
522                     !(ret = bch2_strtoull_h(nr_str, &nr)))
523                         ret = bch2_btree_perf_test(c, test, nr, threads);
524                 kfree(tmp);
525
526                 if (ret)
527                         size = ret;
528         }
529 #endif
530         return size;
531 }
532 SYSFS_OPS(bch2_fs);
533
534 struct attribute *bch2_fs_files[] = {
535         &sysfs_minor,
536         &sysfs_btree_cache_size,
537         &sysfs_btree_avg_write_size,
538
539         &sysfs_promote_whole_extents,
540
541         &sysfs_compression_stats,
542
543 #ifdef CONFIG_BCACHEFS_TESTS
544         &sysfs_perf_test,
545 #endif
546         NULL
547 };
548
549 /* internal dir - just a wrapper */
550
551 SHOW(bch2_fs_internal)
552 {
553         struct bch_fs *c = container_of(kobj, struct bch_fs, internal);
554         return bch2_fs_show(&c->kobj, attr, buf);
555 }
556
557 STORE(bch2_fs_internal)
558 {
559         struct bch_fs *c = container_of(kobj, struct bch_fs, internal);
560         return bch2_fs_store(&c->kobj, attr, buf, size);
561 }
562 SYSFS_OPS(bch2_fs_internal);
563
564 struct attribute *bch2_fs_internal_files[] = {
565         &sysfs_journal_debug,
566         &sysfs_journal_pins,
567         &sysfs_btree_updates,
568         &sysfs_dirty_btree_nodes,
569         &sysfs_btree_cache,
570         &sysfs_btree_key_cache,
571         &sysfs_btree_transactions,
572         &sysfs_new_stripes,
573         &sysfs_stripes_heap,
574         &sysfs_open_buckets,
575         &sysfs_io_timers_read,
576         &sysfs_io_timers_write,
577
578         &sysfs_trigger_journal_flush,
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_op_data_progress,
596
597         &sysfs_internal_uuid,
598         NULL
599 };
600
601 /* options */
602
603 SHOW(bch2_fs_opts_dir)
604 {
605         struct printbuf out = _PBUF(buf, PAGE_SIZE);
606         struct bch_fs *c = container_of(kobj, struct bch_fs, opts_dir);
607         const struct bch_option *opt = container_of(attr, struct bch_option, attr);
608         int id = opt - bch2_opt_table;
609         u64 v = bch2_opt_get_by_id(&c->opts, id);
610
611         bch2_opt_to_text(&out, c, opt, v, OPT_SHOW_FULL_LIST);
612         pr_buf(&out, "\n");
613
614         return out.pos - buf;
615 }
616
617 STORE(bch2_fs_opts_dir)
618 {
619         struct bch_fs *c = container_of(kobj, struct bch_fs, opts_dir);
620         const struct bch_option *opt = container_of(attr, struct bch_option, attr);
621         int ret, id = opt - bch2_opt_table;
622         char *tmp;
623         u64 v;
624
625         tmp = kstrdup(buf, GFP_KERNEL);
626         if (!tmp)
627                 return -ENOMEM;
628
629         ret = bch2_opt_parse(c, opt, strim(tmp), &v);
630         kfree(tmp);
631
632         if (ret < 0)
633                 return ret;
634
635         ret = bch2_opt_check_may_set(c, id, v);
636         if (ret < 0)
637                 return ret;
638
639         if (opt->set_sb != SET_NO_SB_OPT) {
640                 mutex_lock(&c->sb_lock);
641                 opt->set_sb(c->disk_sb.sb, v);
642                 bch2_write_super(c);
643                 mutex_unlock(&c->sb_lock);
644         }
645
646         bch2_opt_set_by_id(&c->opts, id, v);
647
648         if ((id == Opt_background_target ||
649              id == Opt_background_compression) && v) {
650                 bch2_rebalance_add_work(c, S64_MAX);
651                 rebalance_wakeup(c);
652         }
653
654         return size;
655 }
656 SYSFS_OPS(bch2_fs_opts_dir);
657
658 struct attribute *bch2_fs_opts_dir_files[] = { NULL };
659
660 int bch2_opts_create_sysfs_files(struct kobject *kobj)
661 {
662         const struct bch_option *i;
663         int ret;
664
665         for (i = bch2_opt_table;
666              i < bch2_opt_table + bch2_opts_nr;
667              i++) {
668                 if (!(i->mode & OPT_FS))
669                         continue;
670
671                 ret = sysfs_create_file(kobj, &i->attr);
672                 if (ret)
673                         return ret;
674         }
675
676         return 0;
677 }
678
679 /* time stats */
680
681 SHOW(bch2_fs_time_stats)
682 {
683         struct bch_fs *c = container_of(kobj, struct bch_fs, time_stats);
684         struct printbuf out = _PBUF(buf, PAGE_SIZE);
685
686 #define x(name)                                                         \
687         if (attr == &sysfs_time_stat_##name) {                          \
688                 bch2_time_stats_to_text(&out, &c->times[BCH_TIME_##name]);\
689                 return out.pos - buf;                                   \
690         }
691         BCH_TIME_STATS()
692 #undef x
693
694         return 0;
695 }
696
697 STORE(bch2_fs_time_stats)
698 {
699         return size;
700 }
701 SYSFS_OPS(bch2_fs_time_stats);
702
703 struct attribute *bch2_fs_time_stats_files[] = {
704 #define x(name)                                         \
705         &sysfs_time_stat_##name,
706         BCH_TIME_STATS()
707 #undef x
708         NULL
709 };
710
711 static void reserve_stats_to_text(struct printbuf *out, struct bch_dev *ca)
712 {
713         enum alloc_reserve i;
714
715         spin_lock(&ca->fs->freelist_lock);
716
717         pr_buf(out, "free_inc:\t%zu\t%zu\n",
718                fifo_used(&ca->free_inc),
719                ca->free_inc.size);
720
721         for (i = 0; i < RESERVE_NR; i++)
722                 pr_buf(out, "free[%u]:\t%zu\t%zu\n", i,
723                        fifo_used(&ca->free[i]),
724                        ca->free[i].size);
725
726         spin_unlock(&ca->fs->freelist_lock);
727 }
728
729 static void dev_alloc_debug_to_text(struct printbuf *out, struct bch_dev *ca)
730 {
731         struct bch_fs *c = ca->fs;
732         struct bch_dev_usage stats = bch2_dev_usage_read(ca);
733         unsigned i, nr[BCH_DATA_NR];
734
735         memset(nr, 0, sizeof(nr));
736
737         for (i = 0; i < ARRAY_SIZE(c->open_buckets); i++)
738                 nr[c->open_buckets[i].type]++;
739
740         pr_buf(out,
741                "\t\t buckets\t sectors      fragmented\n"
742                "capacity%16llu\n",
743                ca->mi.nbuckets - ca->mi.first_bucket);
744
745         for (i = 1; i < BCH_DATA_NR; i++)
746                 pr_buf(out, "%-8s%16llu%16llu%16llu\n",
747                        bch2_data_types[i], stats.d[i].buckets,
748                        stats.d[i].sectors, stats.d[i].fragmented);
749
750         pr_buf(out,
751                "ec\t%16llu\n"
752                "available%15llu\n"
753                "\n"
754                "free_inc\t\t%zu/%zu\n"
755                "free[RESERVE_MOVINGGC]\t%zu/%zu\n"
756                "free[RESERVE_NONE]\t%zu/%zu\n"
757                "freelist_wait\t\t%s\n"
758                "open buckets allocated\t%u\n"
759                "open buckets this dev\t%u\n"
760                "open buckets total\t%u\n"
761                "open_buckets_wait\t%s\n"
762                "open_buckets_btree\t%u\n"
763                "open_buckets_user\t%u\n"
764                "btree reserve cache\t%u\n"
765                "thread state:\t\t%s\n",
766                stats.buckets_ec,
767                __dev_buckets_available(ca, stats),
768                fifo_used(&ca->free_inc),                ca->free_inc.size,
769                fifo_used(&ca->free[RESERVE_MOVINGGC]),  ca->free[RESERVE_MOVINGGC].size,
770                fifo_used(&ca->free[RESERVE_NONE]),      ca->free[RESERVE_NONE].size,
771                c->freelist_wait.list.first              ? "waiting" : "empty",
772                OPEN_BUCKETS_COUNT - c->open_buckets_nr_free,
773                ca->nr_open_buckets,
774                OPEN_BUCKETS_COUNT,
775                c->open_buckets_wait.list.first          ? "waiting" : "empty",
776                nr[BCH_DATA_btree],
777                nr[BCH_DATA_user],
778                c->btree_reserve_cache_nr,
779                bch2_allocator_states[ca->allocator_state]);
780 }
781
782 static const char * const bch2_rw[] = {
783         "read",
784         "write",
785         NULL
786 };
787
788 static void dev_iodone_to_text(struct printbuf *out, struct bch_dev *ca)
789 {
790         int rw, i;
791
792         for (rw = 0; rw < 2; rw++) {
793                 pr_buf(out, "%s:\n", bch2_rw[rw]);
794
795                 for (i = 1; i < BCH_DATA_NR; i++)
796                         pr_buf(out, "%-12s:%12llu\n",
797                                bch2_data_types[i],
798                                percpu_u64_get(&ca->io_done->sectors[rw][i]) << 9);
799         }
800 }
801
802 SHOW(bch2_dev)
803 {
804         struct bch_dev *ca = container_of(kobj, struct bch_dev, kobj);
805         struct bch_fs *c = ca->fs;
806         struct printbuf out = _PBUF(buf, PAGE_SIZE);
807
808         sysfs_printf(uuid,              "%pU\n", ca->uuid.b);
809
810         sysfs_print(bucket_size,        bucket_bytes(ca));
811         sysfs_print(first_bucket,       ca->mi.first_bucket);
812         sysfs_print(nbuckets,           ca->mi.nbuckets);
813         sysfs_print(durability,         ca->mi.durability);
814         sysfs_print(discard,            ca->mi.discard);
815
816         if (attr == &sysfs_label) {
817                 if (ca->mi.group) {
818                         mutex_lock(&c->sb_lock);
819                         bch2_disk_path_to_text(&out, &c->disk_sb,
820                                                ca->mi.group - 1);
821                         mutex_unlock(&c->sb_lock);
822                 }
823
824                 pr_buf(&out, "\n");
825                 return out.pos - buf;
826         }
827
828         if (attr == &sysfs_has_data) {
829                 bch2_flags_to_text(&out, bch2_data_types,
830                                    bch2_dev_has_data(c, ca));
831                 pr_buf(&out, "\n");
832                 return out.pos - buf;
833         }
834
835         if (attr == &sysfs_cache_replacement_policy) {
836                 bch2_string_opt_to_text(&out,
837                                         bch2_cache_replacement_policies,
838                                         ca->mi.replacement);
839                 pr_buf(&out, "\n");
840                 return out.pos - buf;
841         }
842
843         if (attr == &sysfs_state_rw) {
844                 bch2_string_opt_to_text(&out, bch2_member_states,
845                                         ca->mi.state);
846                 pr_buf(&out, "\n");
847                 return out.pos - buf;
848         }
849
850         if (attr == &sysfs_iodone) {
851                 dev_iodone_to_text(&out, ca);
852                 return out.pos - buf;
853         }
854
855         sysfs_print(io_latency_read,            atomic64_read(&ca->cur_latency[READ]));
856         sysfs_print(io_latency_write,           atomic64_read(&ca->cur_latency[WRITE]));
857
858         if (attr == &sysfs_io_latency_stats_read) {
859                 bch2_time_stats_to_text(&out, &ca->io_latency[READ]);
860                 return out.pos - buf;
861         }
862         if (attr == &sysfs_io_latency_stats_write) {
863                 bch2_time_stats_to_text(&out, &ca->io_latency[WRITE]);
864                 return out.pos - buf;
865         }
866
867         sysfs_printf(congested,                 "%u%%",
868                      clamp(atomic_read(&ca->congested), 0, CONGESTED_MAX)
869                      * 100 / CONGESTED_MAX);
870
871         if (attr == &sysfs_reserve_stats) {
872                 reserve_stats_to_text(&out, ca);
873                 return out.pos - buf;
874         }
875         if (attr == &sysfs_alloc_debug) {
876                 dev_alloc_debug_to_text(&out, ca);
877                 return out.pos - buf;
878         }
879
880         return 0;
881 }
882
883 STORE(bch2_dev)
884 {
885         struct bch_dev *ca = container_of(kobj, struct bch_dev, kobj);
886         struct bch_fs *c = ca->fs;
887         struct bch_member *mi;
888
889         if (attr == &sysfs_discard) {
890                 bool v = strtoul_or_return(buf);
891
892                 mutex_lock(&c->sb_lock);
893                 mi = &bch2_sb_get_members(c->disk_sb.sb)->members[ca->dev_idx];
894
895                 if (v != BCH_MEMBER_DISCARD(mi)) {
896                         SET_BCH_MEMBER_DISCARD(mi, v);
897                         bch2_write_super(c);
898                 }
899                 mutex_unlock(&c->sb_lock);
900         }
901
902         if (attr == &sysfs_cache_replacement_policy) {
903                 ssize_t v = __sysfs_match_string(bch2_cache_replacement_policies, -1, buf);
904
905                 if (v < 0)
906                         return v;
907
908                 mutex_lock(&c->sb_lock);
909                 mi = &bch2_sb_get_members(c->disk_sb.sb)->members[ca->dev_idx];
910
911                 if ((unsigned) v != BCH_MEMBER_REPLACEMENT(mi)) {
912                         SET_BCH_MEMBER_REPLACEMENT(mi, v);
913                         bch2_write_super(c);
914                 }
915                 mutex_unlock(&c->sb_lock);
916         }
917
918         if (attr == &sysfs_label) {
919                 char *tmp;
920                 int ret;
921
922                 tmp = kstrdup(buf, GFP_KERNEL);
923                 if (!tmp)
924                         return -ENOMEM;
925
926                 ret = bch2_dev_group_set(c, ca, strim(tmp));
927                 kfree(tmp);
928                 if (ret)
929                         return ret;
930         }
931
932         if (attr == &sysfs_wake_allocator)
933                 bch2_wake_allocator(ca);
934
935         return size;
936 }
937 SYSFS_OPS(bch2_dev);
938
939 struct attribute *bch2_dev_files[] = {
940         &sysfs_uuid,
941         &sysfs_bucket_size,
942         &sysfs_first_bucket,
943         &sysfs_nbuckets,
944         &sysfs_durability,
945
946         /* settings: */
947         &sysfs_discard,
948         &sysfs_cache_replacement_policy,
949         &sysfs_state_rw,
950         &sysfs_label,
951
952         &sysfs_has_data,
953         &sysfs_iodone,
954
955         &sysfs_io_latency_read,
956         &sysfs_io_latency_write,
957         &sysfs_io_latency_stats_read,
958         &sysfs_io_latency_stats_write,
959         &sysfs_congested,
960
961         &sysfs_reserve_stats,
962
963         /* debug: */
964         &sysfs_alloc_debug,
965         &sysfs_wake_allocator,
966         NULL
967 };
968
969 #endif  /* _BCACHEFS_SYSFS_H_ */