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