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