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