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