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