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