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