Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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 "compress.h"
25#include "disk_accounting.h"
26#include "disk_groups.h"
27#include "ec.h"
28#include "inode.h"
29#include "journal.h"
30#include "journal_reclaim.h"
31#include "keylist.h"
32#include "move.h"
33#include "movinggc.h"
34#include "nocow_locking.h"
35#include "opts.h"
36#include "rebalance.h"
37#include "replicas.h"
38#include "super-io.h"
39#include "tests.h"
40
41#include <linux/blkdev.h>
42#include <linux/sort.h>
43#include <linux/sched/clock.h>
44
45#include "util.h"
46
47#define SYSFS_OPS(type) \
48const struct sysfs_ops type ## _sysfs_ops = { \
49 .show = type ## _show, \
50 .store = type ## _store \
51}
52
53#define SHOW(fn) \
54static ssize_t fn ## _to_text(struct printbuf *, \
55 struct kobject *, struct attribute *); \
56 \
57static ssize_t fn ## _show(struct kobject *kobj, struct attribute *attr,\
58 char *buf) \
59{ \
60 struct printbuf out = PRINTBUF; \
61 ssize_t ret = fn ## _to_text(&out, kobj, attr); \
62 \
63 if (out.pos && out.buf[out.pos - 1] != '\n') \
64 prt_newline(&out); \
65 \
66 if (!ret && out.allocation_failure) \
67 ret = -ENOMEM; \
68 \
69 if (!ret) { \
70 ret = min_t(size_t, out.pos, PAGE_SIZE - 1); \
71 memcpy(buf, out.buf, ret); \
72 } \
73 printbuf_exit(&out); \
74 return bch2_err_class(ret); \
75} \
76 \
77static ssize_t fn ## _to_text(struct printbuf *out, struct kobject *kobj,\
78 struct attribute *attr)
79
80#define STORE(fn) \
81static ssize_t fn ## _store_inner(struct kobject *, struct attribute *,\
82 const char *, size_t); \
83 \
84static ssize_t fn ## _store(struct kobject *kobj, struct attribute *attr,\
85 const char *buf, size_t size) \
86{ \
87 return bch2_err_class(fn##_store_inner(kobj, attr, buf, size)); \
88} \
89 \
90static ssize_t fn ## _store_inner(struct kobject *kobj, struct attribute *attr,\
91 const char *buf, size_t size)
92
93#define __sysfs_attribute(_name, _mode) \
94 static struct attribute sysfs_##_name = \
95 { .name = #_name, .mode = _mode }
96
97#define write_attribute(n) __sysfs_attribute(n, 0200)
98#define read_attribute(n) __sysfs_attribute(n, 0444)
99#define rw_attribute(n) __sysfs_attribute(n, 0644)
100
101#define sysfs_printf(file, fmt, ...) \
102do { \
103 if (attr == &sysfs_ ## file) \
104 prt_printf(out, fmt "\n", __VA_ARGS__); \
105} while (0)
106
107#define sysfs_print(file, var) \
108do { \
109 if (attr == &sysfs_ ## file) \
110 snprint(out, var); \
111} while (0)
112
113#define sysfs_hprint(file, val) \
114do { \
115 if (attr == &sysfs_ ## file) \
116 prt_human_readable_s64(out, val); \
117} while (0)
118
119#define sysfs_strtoul(file, var) \
120do { \
121 if (attr == &sysfs_ ## file) \
122 return strtoul_safe(buf, var) ?: (ssize_t) size; \
123} while (0)
124
125#define sysfs_strtoul_clamp(file, var, min, max) \
126do { \
127 if (attr == &sysfs_ ## file) \
128 return strtoul_safe_clamp(buf, var, min, max) \
129 ?: (ssize_t) size; \
130} while (0)
131
132#define strtoul_or_return(cp) \
133({ \
134 unsigned long _v; \
135 int _r = kstrtoul(cp, 10, &_v); \
136 if (_r) \
137 return _r; \
138 _v; \
139})
140
141write_attribute(trigger_gc);
142write_attribute(trigger_discards);
143write_attribute(trigger_invalidates);
144write_attribute(trigger_journal_flush);
145write_attribute(trigger_journal_writes);
146write_attribute(trigger_btree_cache_shrink);
147write_attribute(trigger_btree_key_cache_shrink);
148write_attribute(trigger_freelist_wakeup);
149read_attribute(gc_gens_pos);
150
151read_attribute(uuid);
152read_attribute(minor);
153read_attribute(flags);
154read_attribute(bucket_size);
155read_attribute(first_bucket);
156read_attribute(nbuckets);
157rw_attribute(durability);
158read_attribute(io_done);
159read_attribute(io_errors);
160write_attribute(io_errors_reset);
161
162read_attribute(io_latency_read);
163read_attribute(io_latency_write);
164read_attribute(io_latency_stats_read);
165read_attribute(io_latency_stats_write);
166read_attribute(congested);
167
168read_attribute(btree_write_stats);
169
170read_attribute(btree_cache_size);
171read_attribute(compression_stats);
172read_attribute(journal_debug);
173read_attribute(btree_cache);
174read_attribute(btree_key_cache);
175read_attribute(btree_reserve_cache);
176read_attribute(stripes_heap);
177read_attribute(open_buckets);
178read_attribute(open_buckets_partial);
179read_attribute(write_points);
180read_attribute(nocow_lock_table);
181
182#ifdef BCH_WRITE_REF_DEBUG
183read_attribute(write_refs);
184
185static const char * const bch2_write_refs[] = {
186#define x(n) #n,
187 BCH_WRITE_REFS()
188#undef x
189 NULL
190};
191
192static void bch2_write_refs_to_text(struct printbuf *out, struct bch_fs *c)
193{
194 bch2_printbuf_tabstop_push(out, 24);
195
196 for (unsigned i = 0; i < ARRAY_SIZE(c->writes); i++)
197 prt_printf(out, "%s\t%li\n", bch2_write_refs[i], atomic_long_read(&c->writes[i]));
198}
199#endif
200
201read_attribute(internal_uuid);
202read_attribute(disk_groups);
203
204read_attribute(has_data);
205read_attribute(alloc_debug);
206read_attribute(usage_base);
207
208#define x(t, n, ...) read_attribute(t);
209BCH_PERSISTENT_COUNTERS()
210#undef x
211
212rw_attribute(discard);
213read_attribute(state);
214rw_attribute(label);
215
216read_attribute(copy_gc_wait);
217
218sysfs_pd_controller_attribute(rebalance);
219read_attribute(rebalance_status);
220
221read_attribute(new_stripes);
222
223read_attribute(io_timers_read);
224read_attribute(io_timers_write);
225
226read_attribute(moving_ctxts);
227
228#ifdef CONFIG_BCACHEFS_TESTS
229write_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 = 0644 };
235 BCH_TIME_STATS()
236#undef x
237
238static size_t bch2_btree_cache_size(struct bch_fs *c)
239{
240 struct btree_cache *bc = &c->btree_cache;
241 size_t ret = 0;
242 struct btree *b;
243
244 mutex_lock(&bc->lock);
245 list_for_each_entry(b, &bc->live[0].list, list)
246 ret += btree_buf_bytes(b);
247 list_for_each_entry(b, &bc->live[1].list, list)
248 ret += btree_buf_bytes(b);
249 list_for_each_entry(b, &bc->freeable, list)
250 ret += btree_buf_bytes(b);
251 mutex_unlock(&bc->lock);
252 return ret;
253}
254
255static int bch2_compression_stats_to_text(struct printbuf *out, struct bch_fs *c)
256{
257 prt_str(out, "type");
258 printbuf_tabstop_push(out, 12);
259 printbuf_tabstop_push(out, 16);
260 printbuf_tabstop_push(out, 16);
261 printbuf_tabstop_push(out, 24);
262 prt_printf(out, "type\tcompressed\runcompressed\raverage extent size\r\n");
263
264 for (unsigned i = 1; i < BCH_COMPRESSION_TYPE_NR; i++) {
265 struct disk_accounting_pos a = {
266 .type = BCH_DISK_ACCOUNTING_compression,
267 .compression.type = i,
268 };
269 struct bpos p = disk_accounting_pos_to_bpos(&a);
270 u64 v[3];
271 bch2_accounting_mem_read(c, p, v, ARRAY_SIZE(v));
272
273 u64 nr_extents = v[0];
274 u64 sectors_uncompressed = v[1];
275 u64 sectors_compressed = v[2];
276
277 bch2_prt_compression_type(out, i);
278 prt_tab(out);
279
280 prt_human_readable_u64(out, sectors_compressed << 9);
281 prt_tab_rjust(out);
282
283 prt_human_readable_u64(out, sectors_uncompressed << 9);
284 prt_tab_rjust(out);
285
286 prt_human_readable_u64(out, nr_extents
287 ? div64_u64(sectors_uncompressed << 9, nr_extents)
288 : 0);
289 prt_tab_rjust(out);
290 prt_newline(out);
291 }
292
293 return 0;
294}
295
296static void bch2_gc_gens_pos_to_text(struct printbuf *out, struct bch_fs *c)
297{
298 bch2_btree_id_to_text(out, c->gc_gens_btree);
299 prt_printf(out, ": ");
300 bch2_bpos_to_text(out, c->gc_gens_pos);
301 prt_printf(out, "\n");
302}
303
304static void bch2_fs_usage_base_to_text(struct printbuf *out, struct bch_fs *c)
305{
306 struct bch_fs_usage_base b = {};
307
308 acc_u64s_percpu(&b.hidden, &c->usage->hidden, sizeof(b) / sizeof(u64));
309
310 prt_printf(out, "hidden:\t\t%llu\n", b.hidden);
311 prt_printf(out, "btree:\t\t%llu\n", b.btree);
312 prt_printf(out, "data:\t\t%llu\n", b.data);
313 prt_printf(out, "cached:\t%llu\n", b.cached);
314 prt_printf(out, "reserved:\t\t%llu\n", b.reserved);
315 prt_printf(out, "nr_inodes:\t%llu\n", b.nr_inodes);
316}
317
318SHOW(bch2_fs)
319{
320 struct bch_fs *c = container_of(kobj, struct bch_fs, kobj);
321
322 sysfs_print(minor, c->minor);
323 sysfs_printf(internal_uuid, "%pU", c->sb.uuid.b);
324
325 if (attr == &sysfs_flags)
326 prt_bitflags(out, bch2_fs_flag_strs, c->flags);
327
328 sysfs_hprint(btree_cache_size, bch2_btree_cache_size(c));
329
330 if (attr == &sysfs_btree_write_stats)
331 bch2_btree_write_stats_to_text(out, c);
332
333 if (attr == &sysfs_gc_gens_pos)
334 bch2_gc_gens_pos_to_text(out, c);
335
336 sysfs_pd_controller_show(rebalance, &c->rebalance.pd); /* XXX */
337
338 if (attr == &sysfs_copy_gc_wait)
339 bch2_copygc_wait_to_text(out, c);
340
341 if (attr == &sysfs_rebalance_status)
342 bch2_rebalance_status_to_text(out, c);
343
344 /* Debugging: */
345
346 if (attr == &sysfs_journal_debug)
347 bch2_journal_debug_to_text(out, &c->journal);
348
349 if (attr == &sysfs_btree_cache)
350 bch2_btree_cache_to_text(out, &c->btree_cache);
351
352 if (attr == &sysfs_btree_key_cache)
353 bch2_btree_key_cache_to_text(out, &c->btree_key_cache);
354
355 if (attr == &sysfs_btree_reserve_cache)
356 bch2_btree_reserve_cache_to_text(out, c);
357
358 if (attr == &sysfs_stripes_heap)
359 bch2_stripes_heap_to_text(out, c);
360
361 if (attr == &sysfs_open_buckets)
362 bch2_open_buckets_to_text(out, c, NULL);
363
364 if (attr == &sysfs_open_buckets_partial)
365 bch2_open_buckets_partial_to_text(out, c);
366
367 if (attr == &sysfs_write_points)
368 bch2_write_points_to_text(out, c);
369
370 if (attr == &sysfs_compression_stats)
371 bch2_compression_stats_to_text(out, c);
372
373 if (attr == &sysfs_new_stripes)
374 bch2_new_stripes_to_text(out, c);
375
376 if (attr == &sysfs_io_timers_read)
377 bch2_io_timers_to_text(out, &c->io_clock[READ]);
378
379 if (attr == &sysfs_io_timers_write)
380 bch2_io_timers_to_text(out, &c->io_clock[WRITE]);
381
382 if (attr == &sysfs_moving_ctxts)
383 bch2_fs_moving_ctxts_to_text(out, c);
384
385#ifdef BCH_WRITE_REF_DEBUG
386 if (attr == &sysfs_write_refs)
387 bch2_write_refs_to_text(out, c);
388#endif
389
390 if (attr == &sysfs_nocow_lock_table)
391 bch2_nocow_locks_to_text(out, &c->nocow_locks);
392
393 if (attr == &sysfs_disk_groups)
394 bch2_disk_groups_to_text(out, c);
395
396 if (attr == &sysfs_alloc_debug)
397 bch2_fs_alloc_debug_to_text(out, c);
398
399 if (attr == &sysfs_usage_base)
400 bch2_fs_usage_base_to_text(out, c);
401
402 return 0;
403}
404
405STORE(bch2_fs)
406{
407 struct bch_fs *c = container_of(kobj, struct bch_fs, kobj);
408
409 sysfs_pd_controller_store(rebalance, &c->rebalance.pd);
410
411 /* Debugging: */
412
413 if (!test_bit(BCH_FS_started, &c->flags))
414 return -EPERM;
415
416 /* Debugging: */
417
418 if (!bch2_write_ref_tryget(c, BCH_WRITE_REF_sysfs))
419 return -EROFS;
420
421 if (attr == &sysfs_trigger_btree_cache_shrink) {
422 struct btree_cache *bc = &c->btree_cache;
423 struct shrink_control sc;
424
425 sc.gfp_mask = GFP_KERNEL;
426 sc.nr_to_scan = strtoul_or_return(buf);
427 bc->live[0].shrink->scan_objects(bc->live[0].shrink, &sc);
428 }
429
430 if (attr == &sysfs_trigger_btree_key_cache_shrink) {
431 struct shrink_control sc;
432
433 sc.gfp_mask = GFP_KERNEL;
434 sc.nr_to_scan = strtoul_or_return(buf);
435 c->btree_key_cache.shrink->scan_objects(c->btree_key_cache.shrink, &sc);
436 }
437
438 if (attr == &sysfs_trigger_gc)
439 bch2_gc_gens(c);
440
441 if (attr == &sysfs_trigger_discards)
442 bch2_do_discards(c);
443
444 if (attr == &sysfs_trigger_invalidates)
445 bch2_do_invalidates(c);
446
447 if (attr == &sysfs_trigger_journal_flush) {
448 bch2_journal_flush_all_pins(&c->journal);
449 bch2_journal_meta(&c->journal);
450 }
451
452 if (attr == &sysfs_trigger_journal_writes)
453 bch2_journal_do_writes(&c->journal);
454
455 if (attr == &sysfs_trigger_freelist_wakeup)
456 closure_wake_up(&c->freelist_wait);
457
458#ifdef CONFIG_BCACHEFS_TESTS
459 if (attr == &sysfs_perf_test) {
460 char *tmp = kstrdup(buf, GFP_KERNEL), *p = tmp;
461 char *test = strsep(&p, " \t\n");
462 char *nr_str = strsep(&p, " \t\n");
463 char *threads_str = strsep(&p, " \t\n");
464 unsigned threads;
465 u64 nr;
466 int ret = -EINVAL;
467
468 if (threads_str &&
469 !(ret = kstrtouint(threads_str, 10, &threads)) &&
470 !(ret = bch2_strtoull_h(nr_str, &nr)))
471 ret = bch2_btree_perf_test(c, test, nr, threads);
472 kfree(tmp);
473
474 if (ret)
475 size = ret;
476 }
477#endif
478 bch2_write_ref_put(c, BCH_WRITE_REF_sysfs);
479 return size;
480}
481SYSFS_OPS(bch2_fs);
482
483struct attribute *bch2_fs_files[] = {
484 &sysfs_minor,
485 &sysfs_btree_cache_size,
486 &sysfs_btree_write_stats,
487
488 &sysfs_rebalance_status,
489
490 &sysfs_compression_stats,
491
492#ifdef CONFIG_BCACHEFS_TESTS
493 &sysfs_perf_test,
494#endif
495 NULL
496};
497
498/* counters dir */
499
500SHOW(bch2_fs_counters)
501{
502 struct bch_fs *c = container_of(kobj, struct bch_fs, counters_kobj);
503 u64 counter = 0;
504 u64 counter_since_mount = 0;
505
506 printbuf_tabstop_push(out, 32);
507
508 #define x(t, n, f, ...) \
509 if (attr == &sysfs_##t) { \
510 counter = percpu_u64_get(&c->counters[BCH_COUNTER_##t]);\
511 counter_since_mount = counter - c->counters_on_mount[BCH_COUNTER_##t];\
512 if (f & TYPE_SECTORS) { \
513 counter <<= 9; \
514 counter_since_mount <<= 9; \
515 } \
516 \
517 prt_printf(out, "since mount:\t"); \
518 (f & TYPE_COUNTER) ? prt_u64(out, counter_since_mount) :\
519 prt_human_readable_u64(out, counter_since_mount); \
520 prt_newline(out); \
521 \
522 prt_printf(out, "since filesystem creation:\t"); \
523 (f & TYPE_COUNTER) ? prt_u64(out, counter) : \
524 prt_human_readable_u64(out, counter); \
525 prt_newline(out); \
526 }
527 BCH_PERSISTENT_COUNTERS()
528 #undef x
529 return 0;
530}
531
532STORE(bch2_fs_counters) {
533 return 0;
534}
535
536SYSFS_OPS(bch2_fs_counters);
537
538struct attribute *bch2_fs_counters_files[] = {
539#define x(t, ...) \
540 &sysfs_##t,
541 BCH_PERSISTENT_COUNTERS()
542#undef x
543 NULL
544};
545/* internal dir - just a wrapper */
546
547SHOW(bch2_fs_internal)
548{
549 struct bch_fs *c = container_of(kobj, struct bch_fs, internal);
550
551 return bch2_fs_to_text(out, &c->kobj, attr);
552}
553
554STORE(bch2_fs_internal)
555{
556 struct bch_fs *c = container_of(kobj, struct bch_fs, internal);
557
558 return bch2_fs_store(&c->kobj, attr, buf, size);
559}
560SYSFS_OPS(bch2_fs_internal);
561
562struct attribute *bch2_fs_internal_files[] = {
563 &sysfs_flags,
564 &sysfs_journal_debug,
565 &sysfs_btree_cache,
566 &sysfs_btree_key_cache,
567 &sysfs_btree_reserve_cache,
568 &sysfs_new_stripes,
569 &sysfs_stripes_heap,
570 &sysfs_open_buckets,
571 &sysfs_open_buckets_partial,
572 &sysfs_write_points,
573#ifdef BCH_WRITE_REF_DEBUG
574 &sysfs_write_refs,
575#endif
576 &sysfs_nocow_lock_table,
577 &sysfs_io_timers_read,
578 &sysfs_io_timers_write,
579
580 &sysfs_trigger_gc,
581 &sysfs_trigger_discards,
582 &sysfs_trigger_invalidates,
583 &sysfs_trigger_journal_flush,
584 &sysfs_trigger_journal_writes,
585 &sysfs_trigger_btree_cache_shrink,
586 &sysfs_trigger_btree_key_cache_shrink,
587 &sysfs_trigger_freelist_wakeup,
588
589 &sysfs_gc_gens_pos,
590
591 &sysfs_copy_gc_wait,
592
593 sysfs_pd_controller_files(rebalance),
594
595 &sysfs_moving_ctxts,
596
597 &sysfs_internal_uuid,
598
599 &sysfs_disk_groups,
600 &sysfs_alloc_debug,
601 &sysfs_usage_base,
602 NULL
603};
604
605/* options */
606
607SHOW(bch2_fs_opts_dir)
608{
609 struct bch_fs *c = container_of(kobj, struct bch_fs, opts_dir);
610 const struct bch_option *opt = container_of(attr, struct bch_option, attr);
611 int id = opt - bch2_opt_table;
612 u64 v = bch2_opt_get_by_id(&c->opts, id);
613
614 bch2_opt_to_text(out, c, c->disk_sb.sb, opt, v, OPT_SHOW_FULL_LIST);
615 prt_char(out, '\n');
616
617 return 0;
618}
619
620STORE(bch2_fs_opts_dir)
621{
622 struct bch_fs *c = container_of(kobj, struct bch_fs, opts_dir);
623 const struct bch_option *opt = container_of(attr, struct bch_option, attr);
624 int ret, id = opt - bch2_opt_table;
625 char *tmp;
626 u64 v;
627
628 /*
629 * We don't need to take c->writes for correctness, but it eliminates an
630 * unsightly error message in the dmesg log when we're RO:
631 */
632 if (unlikely(!bch2_write_ref_tryget(c, BCH_WRITE_REF_sysfs)))
633 return -EROFS;
634
635 tmp = kstrdup(buf, GFP_KERNEL);
636 if (!tmp) {
637 ret = -ENOMEM;
638 goto err;
639 }
640
641 ret = bch2_opt_parse(c, opt, strim(tmp), &v, NULL);
642 kfree(tmp);
643
644 if (ret < 0)
645 goto err;
646
647 ret = bch2_opt_check_may_set(c, id, v);
648 if (ret < 0)
649 goto err;
650
651 bch2_opt_set_sb(c, NULL, opt, v);
652 bch2_opt_set_by_id(&c->opts, id, v);
653
654 if (v &&
655 (id == Opt_background_target ||
656 id == Opt_background_compression ||
657 (id == Opt_compression && !c->opts.background_compression)))
658 bch2_set_rebalance_needs_scan(c, 0);
659
660 if (v && id == Opt_rebalance_enabled)
661 rebalance_wakeup(c);
662
663 if (v && id == Opt_copygc_enabled &&
664 c->copygc_thread)
665 wake_up_process(c->copygc_thread);
666
667 ret = size;
668err:
669 bch2_write_ref_put(c, BCH_WRITE_REF_sysfs);
670 return ret;
671}
672SYSFS_OPS(bch2_fs_opts_dir);
673
674struct attribute *bch2_fs_opts_dir_files[] = { NULL };
675
676int bch2_opts_create_sysfs_files(struct kobject *kobj)
677{
678 const struct bch_option *i;
679 int ret;
680
681 for (i = bch2_opt_table;
682 i < bch2_opt_table + bch2_opts_nr;
683 i++) {
684 if (!(i->flags & OPT_FS))
685 continue;
686
687 ret = sysfs_create_file(kobj, &i->attr);
688 if (ret)
689 return ret;
690 }
691
692 return 0;
693}
694
695/* time stats */
696
697SHOW(bch2_fs_time_stats)
698{
699 struct bch_fs *c = container_of(kobj, struct bch_fs, time_stats);
700
701#define x(name) \
702 if (attr == &sysfs_time_stat_##name) \
703 bch2_time_stats_to_text(out, &c->times[BCH_TIME_##name]);
704 BCH_TIME_STATS()
705#undef x
706
707 return 0;
708}
709
710STORE(bch2_fs_time_stats)
711{
712 struct bch_fs *c = container_of(kobj, struct bch_fs, time_stats);
713
714#define x(name) \
715 if (attr == &sysfs_time_stat_##name) \
716 bch2_time_stats_reset(&c->times[BCH_TIME_##name]);
717 BCH_TIME_STATS()
718#undef x
719 return size;
720}
721SYSFS_OPS(bch2_fs_time_stats);
722
723struct attribute *bch2_fs_time_stats_files[] = {
724#define x(name) \
725 &sysfs_time_stat_##name,
726 BCH_TIME_STATS()
727#undef x
728 NULL
729};
730
731static const char * const bch2_rw[] = {
732 "read",
733 "write",
734 NULL
735};
736
737static void dev_io_done_to_text(struct printbuf *out, struct bch_dev *ca)
738{
739 int rw, i;
740
741 for (rw = 0; rw < 2; rw++) {
742 prt_printf(out, "%s:\n", bch2_rw[rw]);
743
744 for (i = 1; i < BCH_DATA_NR; i++)
745 prt_printf(out, "%-12s:%12llu\n",
746 bch2_data_type_str(i),
747 percpu_u64_get(&ca->io_done->sectors[rw][i]) << 9);
748 }
749}
750
751SHOW(bch2_dev)
752{
753 struct bch_dev *ca = container_of(kobj, struct bch_dev, kobj);
754 struct bch_fs *c = ca->fs;
755
756 sysfs_printf(uuid, "%pU\n", ca->uuid.b);
757
758 sysfs_print(bucket_size, bucket_bytes(ca));
759 sysfs_print(first_bucket, ca->mi.first_bucket);
760 sysfs_print(nbuckets, ca->mi.nbuckets);
761 sysfs_print(durability, ca->mi.durability);
762 sysfs_print(discard, ca->mi.discard);
763
764 if (attr == &sysfs_label) {
765 if (ca->mi.group)
766 bch2_disk_path_to_text(out, c, ca->mi.group - 1);
767 prt_char(out, '\n');
768 }
769
770 if (attr == &sysfs_has_data) {
771 prt_bitflags(out, __bch2_data_types, bch2_dev_has_data(c, ca));
772 prt_char(out, '\n');
773 }
774
775 if (attr == &sysfs_state) {
776 prt_string_option(out, bch2_member_states, ca->mi.state);
777 prt_char(out, '\n');
778 }
779
780 if (attr == &sysfs_io_done)
781 dev_io_done_to_text(out, ca);
782
783 if (attr == &sysfs_io_errors)
784 bch2_dev_io_errors_to_text(out, ca);
785
786 sysfs_print(io_latency_read, atomic64_read(&ca->cur_latency[READ]));
787 sysfs_print(io_latency_write, atomic64_read(&ca->cur_latency[WRITE]));
788
789 if (attr == &sysfs_io_latency_stats_read)
790 bch2_time_stats_to_text(out, &ca->io_latency[READ].stats);
791
792 if (attr == &sysfs_io_latency_stats_write)
793 bch2_time_stats_to_text(out, &ca->io_latency[WRITE].stats);
794
795 sysfs_printf(congested, "%u%%",
796 clamp(atomic_read(&ca->congested), 0, CONGESTED_MAX)
797 * 100 / CONGESTED_MAX);
798
799 if (attr == &sysfs_alloc_debug)
800 bch2_dev_alloc_debug_to_text(out, ca);
801
802 if (attr == &sysfs_open_buckets)
803 bch2_open_buckets_to_text(out, c, ca);
804
805 return 0;
806}
807
808STORE(bch2_dev)
809{
810 struct bch_dev *ca = container_of(kobj, struct bch_dev, kobj);
811 struct bch_fs *c = ca->fs;
812
813 if (attr == &sysfs_discard) {
814 bool v = strtoul_or_return(buf);
815
816 bch2_opt_set_sb(c, ca, bch2_opt_table + Opt_discard, v);
817 }
818
819 if (attr == &sysfs_durability) {
820 u64 v = strtoul_or_return(buf);
821
822 bch2_opt_set_sb(c, ca, bch2_opt_table + Opt_durability, v);
823 }
824
825 if (attr == &sysfs_label) {
826 char *tmp;
827 int ret;
828
829 tmp = kstrdup(buf, GFP_KERNEL);
830 if (!tmp)
831 return -ENOMEM;
832
833 ret = bch2_dev_group_set(c, ca, strim(tmp));
834 kfree(tmp);
835 if (ret)
836 return ret;
837 }
838
839 if (attr == &sysfs_io_errors_reset)
840 bch2_dev_errors_reset(ca);
841
842 return size;
843}
844SYSFS_OPS(bch2_dev);
845
846struct attribute *bch2_dev_files[] = {
847 &sysfs_uuid,
848 &sysfs_bucket_size,
849 &sysfs_first_bucket,
850 &sysfs_nbuckets,
851 &sysfs_durability,
852
853 /* settings: */
854 &sysfs_discard,
855 &sysfs_state,
856 &sysfs_label,
857
858 &sysfs_has_data,
859 &sysfs_io_done,
860 &sysfs_io_errors,
861 &sysfs_io_errors_reset,
862
863 &sysfs_io_latency_read,
864 &sysfs_io_latency_write,
865 &sysfs_io_latency_stats_read,
866 &sysfs_io_latency_stats_write,
867 &sysfs_congested,
868
869 /* debug: */
870 &sysfs_alloc_debug,
871 &sysfs_open_buckets,
872 NULL
873};
874
875#endif /* _BCACHEFS_SYSFS_H_ */