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