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