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