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