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);
149rw_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(accounting);
207read_attribute(usage_base);
208
209#define x(t, n, ...) read_attribute(t);
210BCH_PERSISTENT_COUNTERS()
211#undef x
212
213rw_attribute(discard);
214rw_attribute(label);
215
216rw_attribute(copy_gc_enabled);
217read_attribute(copy_gc_wait);
218
219rw_attribute(rebalance_enabled);
220sysfs_pd_controller_attribute(rebalance);
221read_attribute(rebalance_status);
222rw_attribute(promote_whole_extents);
223
224read_attribute(new_stripes);
225
226read_attribute(io_timers_read);
227read_attribute(io_timers_write);
228
229read_attribute(moving_ctxts);
230
231#ifdef CONFIG_BCACHEFS_TESTS
232write_attribute(perf_test);
233#endif /* CONFIG_BCACHEFS_TESTS */
234
235#define x(_name) \
236 static struct attribute sysfs_time_stat_##_name = \
237 { .name = #_name, .mode = 0444 };
238 BCH_TIME_STATS()
239#undef x
240
241static struct attribute sysfs_state_rw = {
242 .name = "state",
243 .mode = 0444,
244};
245
246static size_t bch2_btree_cache_size(struct bch_fs *c)
247{
248 size_t ret = 0;
249 struct btree *b;
250
251 mutex_lock(&c->btree_cache.lock);
252 list_for_each_entry(b, &c->btree_cache.live, list)
253 ret += btree_buf_bytes(b);
254
255 mutex_unlock(&c->btree_cache.lock);
256 return ret;
257}
258
259static int bch2_compression_stats_to_text(struct printbuf *out, struct bch_fs *c)
260{
261 prt_str(out, "type");
262 printbuf_tabstop_push(out, 12);
263 printbuf_tabstop_push(out, 16);
264 printbuf_tabstop_push(out, 16);
265 printbuf_tabstop_push(out, 24);
266 prt_printf(out, "type\tcompressed\runcompressed\raverage extent size\r\n");
267
268 for (unsigned i = 1; i < BCH_COMPRESSION_TYPE_NR; i++) {
269 struct disk_accounting_pos a = {
270 .type = BCH_DISK_ACCOUNTING_compression,
271 .compression.type = i,
272 };
273 struct bpos p = disk_accounting_pos_to_bpos(&a);
274 u64 v[3];
275 bch2_accounting_mem_read(c, p, v, ARRAY_SIZE(v));
276
277 u64 nr_extents = v[0];
278 u64 sectors_uncompressed = v[1];
279 u64 sectors_compressed = v[2];
280
281 bch2_prt_compression_type(out, i);
282 prt_tab(out);
283
284 prt_human_readable_u64(out, sectors_compressed << 9);
285 prt_tab_rjust(out);
286
287 prt_human_readable_u64(out, sectors_uncompressed << 9);
288 prt_tab_rjust(out);
289
290 prt_human_readable_u64(out, nr_extents
291 ? div_u64(sectors_uncompressed << 9, nr_extents)
292 : 0);
293 prt_tab_rjust(out);
294 prt_newline(out);
295 }
296
297 return 0;
298}
299
300static void bch2_gc_gens_pos_to_text(struct printbuf *out, struct bch_fs *c)
301{
302 prt_printf(out, "%s: ", bch2_btree_id_str(c->gc_gens_btree));
303 bch2_bpos_to_text(out, c->gc_gens_pos);
304 prt_printf(out, "\n");
305}
306
307static void bch2_fs_usage_base_to_text(struct printbuf *out, struct bch_fs *c)
308{
309 struct bch_fs_usage_base b = {};
310
311 acc_u64s_percpu(&b.hidden, &c->usage->hidden, sizeof(b) / sizeof(u64));
312
313 prt_printf(out, "hidden:\t\t%llu\n", b.hidden);
314 prt_printf(out, "btree:\t\t%llu\n", b.btree);
315 prt_printf(out, "data:\t\t%llu\n", b.data);
316 prt_printf(out, "cached:\t%llu\n", b.cached);
317 prt_printf(out, "reserved:\t\t%llu\n", b.reserved);
318 prt_printf(out, "nr_inodes:\t%llu\n", b.nr_inodes);
319}
320
321SHOW(bch2_fs)
322{
323 struct bch_fs *c = container_of(kobj, struct bch_fs, kobj);
324
325 sysfs_print(minor, c->minor);
326 sysfs_printf(internal_uuid, "%pU", c->sb.uuid.b);
327
328 if (attr == &sysfs_flags)
329 prt_bitflags(out, bch2_fs_flag_strs, c->flags);
330
331 sysfs_hprint(btree_cache_size, bch2_btree_cache_size(c));
332
333 if (attr == &sysfs_btree_write_stats)
334 bch2_btree_write_stats_to_text(out, c);
335
336 if (attr == &sysfs_gc_gens_pos)
337 bch2_gc_gens_pos_to_text(out, c);
338
339 sysfs_printf(copy_gc_enabled, "%i", c->copy_gc_enabled);
340
341 sysfs_printf(rebalance_enabled, "%i", c->rebalance.enabled);
342 sysfs_pd_controller_show(rebalance, &c->rebalance.pd); /* XXX */
343
344 if (attr == &sysfs_copy_gc_wait)
345 bch2_copygc_wait_to_text(out, c);
346
347 if (attr == &sysfs_rebalance_status)
348 bch2_rebalance_status_to_text(out, c);
349
350 sysfs_print(promote_whole_extents, c->promote_whole_extents);
351
352 /* Debugging: */
353
354 if (attr == &sysfs_journal_debug)
355 bch2_journal_debug_to_text(out, &c->journal);
356
357 if (attr == &sysfs_btree_cache)
358 bch2_btree_cache_to_text(out, &c->btree_cache);
359
360 if (attr == &sysfs_btree_key_cache)
361 bch2_btree_key_cache_to_text(out, &c->btree_key_cache);
362
363 if (attr == &sysfs_btree_reserve_cache)
364 bch2_btree_reserve_cache_to_text(out, c);
365
366 if (attr == &sysfs_stripes_heap)
367 bch2_stripes_heap_to_text(out, c);
368
369 if (attr == &sysfs_open_buckets)
370 bch2_open_buckets_to_text(out, c);
371
372 if (attr == &sysfs_open_buckets_partial)
373 bch2_open_buckets_partial_to_text(out, c);
374
375 if (attr == &sysfs_write_points)
376 bch2_write_points_to_text(out, c);
377
378 if (attr == &sysfs_compression_stats)
379 bch2_compression_stats_to_text(out, c);
380
381 if (attr == &sysfs_new_stripes)
382 bch2_new_stripes_to_text(out, c);
383
384 if (attr == &sysfs_io_timers_read)
385 bch2_io_timers_to_text(out, &c->io_clock[READ]);
386
387 if (attr == &sysfs_io_timers_write)
388 bch2_io_timers_to_text(out, &c->io_clock[WRITE]);
389
390 if (attr == &sysfs_moving_ctxts)
391 bch2_fs_moving_ctxts_to_text(out, c);
392
393#ifdef BCH_WRITE_REF_DEBUG
394 if (attr == &sysfs_write_refs)
395 bch2_write_refs_to_text(out, c);
396#endif
397
398 if (attr == &sysfs_nocow_lock_table)
399 bch2_nocow_locks_to_text(out, &c->nocow_locks);
400
401 if (attr == &sysfs_disk_groups)
402 bch2_disk_groups_to_text(out, c);
403
404 if (attr == &sysfs_alloc_debug)
405 bch2_fs_alloc_debug_to_text(out, c);
406
407 if (attr == &sysfs_accounting)
408 bch2_fs_accounting_to_text(out, c);
409
410 if (attr == &sysfs_usage_base)
411 bch2_fs_usage_base_to_text(out, c);
412
413 return 0;
414}
415
416STORE(bch2_fs)
417{
418 struct bch_fs *c = container_of(kobj, struct bch_fs, kobj);
419
420 if (attr == &sysfs_copy_gc_enabled) {
421 ssize_t ret = strtoul_safe(buf, c->copy_gc_enabled)
422 ?: (ssize_t) size;
423
424 if (c->copygc_thread)
425 wake_up_process(c->copygc_thread);
426 return ret;
427 }
428
429 if (attr == &sysfs_rebalance_enabled) {
430 ssize_t ret = strtoul_safe(buf, c->rebalance.enabled)
431 ?: (ssize_t) size;
432
433 rebalance_wakeup(c);
434 return ret;
435 }
436
437 sysfs_pd_controller_store(rebalance, &c->rebalance.pd);
438
439 sysfs_strtoul(promote_whole_extents, c->promote_whole_extents);
440
441 /* Debugging: */
442
443 if (!test_bit(BCH_FS_started, &c->flags))
444 return -EPERM;
445
446 /* Debugging: */
447
448 if (!bch2_write_ref_tryget(c, BCH_WRITE_REF_sysfs))
449 return -EROFS;
450
451 if (attr == &sysfs_trigger_btree_cache_shrink) {
452 struct shrink_control sc;
453
454 sc.gfp_mask = GFP_KERNEL;
455 sc.nr_to_scan = strtoul_or_return(buf);
456 c->btree_cache.shrink->scan_objects(c->btree_cache.shrink, &sc);
457 }
458
459 if (attr == &sysfs_trigger_btree_key_cache_shrink) {
460 struct shrink_control sc;
461
462 sc.gfp_mask = GFP_KERNEL;
463 sc.nr_to_scan = strtoul_or_return(buf);
464 c->btree_key_cache.shrink->scan_objects(c->btree_cache.shrink, &sc);
465 }
466
467 if (attr == &sysfs_trigger_gc)
468 bch2_gc_gens(c);
469
470 if (attr == &sysfs_trigger_discards)
471 bch2_do_discards(c);
472
473 if (attr == &sysfs_trigger_invalidates)
474 bch2_do_invalidates(c);
475
476 if (attr == &sysfs_trigger_journal_flush) {
477 bch2_journal_flush_all_pins(&c->journal);
478 bch2_journal_meta(&c->journal);
479 }
480
481 if (attr == &sysfs_trigger_journal_writes)
482 bch2_journal_do_writes(&c->journal);
483
484 if (attr == &sysfs_trigger_freelist_wakeup)
485 closure_wake_up(&c->freelist_wait);
486
487#ifdef CONFIG_BCACHEFS_TESTS
488 if (attr == &sysfs_perf_test) {
489 char *tmp = kstrdup(buf, GFP_KERNEL), *p = tmp;
490 char *test = strsep(&p, " \t\n");
491 char *nr_str = strsep(&p, " \t\n");
492 char *threads_str = strsep(&p, " \t\n");
493 unsigned threads;
494 u64 nr;
495 int ret = -EINVAL;
496
497 if (threads_str &&
498 !(ret = kstrtouint(threads_str, 10, &threads)) &&
499 !(ret = bch2_strtoull_h(nr_str, &nr)))
500 ret = bch2_btree_perf_test(c, test, nr, threads);
501 kfree(tmp);
502
503 if (ret)
504 size = ret;
505 }
506#endif
507 bch2_write_ref_put(c, BCH_WRITE_REF_sysfs);
508 return size;
509}
510SYSFS_OPS(bch2_fs);
511
512struct attribute *bch2_fs_files[] = {
513 &sysfs_minor,
514 &sysfs_btree_cache_size,
515 &sysfs_btree_write_stats,
516
517 &sysfs_promote_whole_extents,
518
519 &sysfs_compression_stats,
520
521#ifdef CONFIG_BCACHEFS_TESTS
522 &sysfs_perf_test,
523#endif
524 NULL
525};
526
527/* counters dir */
528
529SHOW(bch2_fs_counters)
530{
531 struct bch_fs *c = container_of(kobj, struct bch_fs, counters_kobj);
532 u64 counter = 0;
533 u64 counter_since_mount = 0;
534
535 printbuf_tabstop_push(out, 32);
536
537 #define x(t, ...) \
538 if (attr == &sysfs_##t) { \
539 counter = percpu_u64_get(&c->counters[BCH_COUNTER_##t]);\
540 counter_since_mount = counter - c->counters_on_mount[BCH_COUNTER_##t];\
541 prt_printf(out, "since mount:\t"); \
542 prt_human_readable_u64(out, counter_since_mount); \
543 prt_newline(out); \
544 \
545 prt_printf(out, "since filesystem creation:\t"); \
546 prt_human_readable_u64(out, counter); \
547 prt_newline(out); \
548 }
549 BCH_PERSISTENT_COUNTERS()
550 #undef x
551 return 0;
552}
553
554STORE(bch2_fs_counters) {
555 return 0;
556}
557
558SYSFS_OPS(bch2_fs_counters);
559
560struct attribute *bch2_fs_counters_files[] = {
561#define x(t, ...) \
562 &sysfs_##t,
563 BCH_PERSISTENT_COUNTERS()
564#undef x
565 NULL
566};
567/* internal dir - just a wrapper */
568
569SHOW(bch2_fs_internal)
570{
571 struct bch_fs *c = container_of(kobj, struct bch_fs, internal);
572
573 return bch2_fs_to_text(out, &c->kobj, attr);
574}
575
576STORE(bch2_fs_internal)
577{
578 struct bch_fs *c = container_of(kobj, struct bch_fs, internal);
579
580 return bch2_fs_store(&c->kobj, attr, buf, size);
581}
582SYSFS_OPS(bch2_fs_internal);
583
584struct attribute *bch2_fs_internal_files[] = {
585 &sysfs_flags,
586 &sysfs_journal_debug,
587 &sysfs_btree_cache,
588 &sysfs_btree_key_cache,
589 &sysfs_btree_reserve_cache,
590 &sysfs_new_stripes,
591 &sysfs_stripes_heap,
592 &sysfs_open_buckets,
593 &sysfs_open_buckets_partial,
594 &sysfs_write_points,
595#ifdef BCH_WRITE_REF_DEBUG
596 &sysfs_write_refs,
597#endif
598 &sysfs_nocow_lock_table,
599 &sysfs_io_timers_read,
600 &sysfs_io_timers_write,
601
602 &sysfs_trigger_gc,
603 &sysfs_trigger_discards,
604 &sysfs_trigger_invalidates,
605 &sysfs_trigger_journal_flush,
606 &sysfs_trigger_journal_writes,
607 &sysfs_trigger_btree_cache_shrink,
608 &sysfs_trigger_btree_key_cache_shrink,
609 &sysfs_trigger_freelist_wakeup,
610
611 &sysfs_gc_gens_pos,
612
613 &sysfs_copy_gc_enabled,
614 &sysfs_copy_gc_wait,
615
616 &sysfs_rebalance_enabled,
617 &sysfs_rebalance_status,
618 sysfs_pd_controller_files(rebalance),
619
620 &sysfs_moving_ctxts,
621
622 &sysfs_internal_uuid,
623
624 &sysfs_disk_groups,
625 &sysfs_alloc_debug,
626 &sysfs_accounting,
627 &sysfs_usage_base,
628 NULL
629};
630
631/* options */
632
633SHOW(bch2_fs_opts_dir)
634{
635 struct bch_fs *c = container_of(kobj, struct bch_fs, opts_dir);
636 const struct bch_option *opt = container_of(attr, struct bch_option, attr);
637 int id = opt - bch2_opt_table;
638 u64 v = bch2_opt_get_by_id(&c->opts, id);
639
640 bch2_opt_to_text(out, c, c->disk_sb.sb, opt, v, OPT_SHOW_FULL_LIST);
641 prt_char(out, '\n');
642
643 return 0;
644}
645
646STORE(bch2_fs_opts_dir)
647{
648 struct bch_fs *c = container_of(kobj, struct bch_fs, opts_dir);
649 const struct bch_option *opt = container_of(attr, struct bch_option, attr);
650 int ret, id = opt - bch2_opt_table;
651 char *tmp;
652 u64 v;
653
654 /*
655 * We don't need to take c->writes for correctness, but it eliminates an
656 * unsightly error message in the dmesg log when we're RO:
657 */
658 if (unlikely(!bch2_write_ref_tryget(c, BCH_WRITE_REF_sysfs)))
659 return -EROFS;
660
661 tmp = kstrdup(buf, GFP_KERNEL);
662 if (!tmp) {
663 ret = -ENOMEM;
664 goto err;
665 }
666
667 ret = bch2_opt_parse(c, opt, strim(tmp), &v, NULL);
668 kfree(tmp);
669
670 if (ret < 0)
671 goto err;
672
673 ret = bch2_opt_check_may_set(c, id, v);
674 if (ret < 0)
675 goto err;
676
677 bch2_opt_set_sb(c, opt, v);
678 bch2_opt_set_by_id(&c->opts, id, v);
679
680 if (v &&
681 (id == Opt_background_target ||
682 id == Opt_background_compression ||
683 (id == Opt_compression && !c->opts.background_compression)))
684 bch2_set_rebalance_needs_scan(c, 0);
685
686 ret = size;
687err:
688 bch2_write_ref_put(c, BCH_WRITE_REF_sysfs);
689 return ret;
690}
691SYSFS_OPS(bch2_fs_opts_dir);
692
693struct attribute *bch2_fs_opts_dir_files[] = { NULL };
694
695int bch2_opts_create_sysfs_files(struct kobject *kobj)
696{
697 const struct bch_option *i;
698 int ret;
699
700 for (i = bch2_opt_table;
701 i < bch2_opt_table + bch2_opts_nr;
702 i++) {
703 if (!(i->flags & OPT_FS))
704 continue;
705
706 ret = sysfs_create_file(kobj, &i->attr);
707 if (ret)
708 return ret;
709 }
710
711 return 0;
712}
713
714/* time stats */
715
716SHOW(bch2_fs_time_stats)
717{
718 struct bch_fs *c = container_of(kobj, struct bch_fs, time_stats);
719
720#define x(name) \
721 if (attr == &sysfs_time_stat_##name) \
722 bch2_time_stats_to_text(out, &c->times[BCH_TIME_##name]);
723 BCH_TIME_STATS()
724#undef x
725
726 return 0;
727}
728
729STORE(bch2_fs_time_stats)
730{
731 return size;
732}
733SYSFS_OPS(bch2_fs_time_stats);
734
735struct attribute *bch2_fs_time_stats_files[] = {
736#define x(name) \
737 &sysfs_time_stat_##name,
738 BCH_TIME_STATS()
739#undef x
740 NULL
741};
742
743static const char * const bch2_rw[] = {
744 "read",
745 "write",
746 NULL
747};
748
749static void dev_io_done_to_text(struct printbuf *out, struct bch_dev *ca)
750{
751 int rw, i;
752
753 for (rw = 0; rw < 2; rw++) {
754 prt_printf(out, "%s:\n", bch2_rw[rw]);
755
756 for (i = 1; i < BCH_DATA_NR; i++)
757 prt_printf(out, "%-12s:%12llu\n",
758 bch2_data_type_str(i),
759 percpu_u64_get(&ca->io_done->sectors[rw][i]) << 9);
760 }
761}
762
763SHOW(bch2_dev)
764{
765 struct bch_dev *ca = container_of(kobj, struct bch_dev, kobj);
766 struct bch_fs *c = ca->fs;
767
768 sysfs_printf(uuid, "%pU\n", ca->uuid.b);
769
770 sysfs_print(bucket_size, bucket_bytes(ca));
771 sysfs_print(first_bucket, ca->mi.first_bucket);
772 sysfs_print(nbuckets, ca->mi.nbuckets);
773 sysfs_print(durability, ca->mi.durability);
774 sysfs_print(discard, ca->mi.discard);
775
776 if (attr == &sysfs_label) {
777 if (ca->mi.group)
778 bch2_disk_path_to_text(out, c, ca->mi.group - 1);
779 prt_char(out, '\n');
780 }
781
782 if (attr == &sysfs_has_data) {
783 prt_bitflags(out, __bch2_data_types, bch2_dev_has_data(c, ca));
784 prt_char(out, '\n');
785 }
786
787 if (attr == &sysfs_state_rw) {
788 prt_string_option(out, bch2_member_states, ca->mi.state);
789 prt_char(out, '\n');
790 }
791
792 if (attr == &sysfs_io_done)
793 dev_io_done_to_text(out, ca);
794
795 if (attr == &sysfs_io_errors)
796 bch2_dev_io_errors_to_text(out, ca);
797
798 sysfs_print(io_latency_read, atomic64_read(&ca->cur_latency[READ]));
799 sysfs_print(io_latency_write, atomic64_read(&ca->cur_latency[WRITE]));
800
801 if (attr == &sysfs_io_latency_stats_read)
802 bch2_time_stats_to_text(out, &ca->io_latency[READ].stats);
803
804 if (attr == &sysfs_io_latency_stats_write)
805 bch2_time_stats_to_text(out, &ca->io_latency[WRITE].stats);
806
807 sysfs_printf(congested, "%u%%",
808 clamp(atomic_read(&ca->congested), 0, CONGESTED_MAX)
809 * 100 / CONGESTED_MAX);
810
811 if (attr == &sysfs_alloc_debug)
812 bch2_dev_alloc_debug_to_text(out, ca);
813
814 return 0;
815}
816
817STORE(bch2_dev)
818{
819 struct bch_dev *ca = container_of(kobj, struct bch_dev, kobj);
820 struct bch_fs *c = ca->fs;
821 struct bch_member *mi;
822
823 if (attr == &sysfs_discard) {
824 bool v = strtoul_or_return(buf);
825
826 mutex_lock(&c->sb_lock);
827 mi = bch2_members_v2_get_mut(c->disk_sb.sb, ca->dev_idx);
828
829 if (v != BCH_MEMBER_DISCARD(mi)) {
830 SET_BCH_MEMBER_DISCARD(mi, v);
831 bch2_write_super(c);
832 }
833 mutex_unlock(&c->sb_lock);
834 }
835
836 if (attr == &sysfs_durability) {
837 u64 v = strtoul_or_return(buf);
838
839 mutex_lock(&c->sb_lock);
840 mi = bch2_members_v2_get_mut(c->disk_sb.sb, ca->dev_idx);
841
842 if (v + 1 != BCH_MEMBER_DURABILITY(mi)) {
843 SET_BCH_MEMBER_DURABILITY(mi, v + 1);
844 bch2_write_super(c);
845 }
846 mutex_unlock(&c->sb_lock);
847 }
848
849 if (attr == &sysfs_label) {
850 char *tmp;
851 int ret;
852
853 tmp = kstrdup(buf, GFP_KERNEL);
854 if (!tmp)
855 return -ENOMEM;
856
857 ret = bch2_dev_group_set(c, ca, strim(tmp));
858 kfree(tmp);
859 if (ret)
860 return ret;
861 }
862
863 if (attr == &sysfs_io_errors_reset)
864 bch2_dev_errors_reset(ca);
865
866 return size;
867}
868SYSFS_OPS(bch2_dev);
869
870struct attribute *bch2_dev_files[] = {
871 &sysfs_uuid,
872 &sysfs_bucket_size,
873 &sysfs_first_bucket,
874 &sysfs_nbuckets,
875 &sysfs_durability,
876
877 /* settings: */
878 &sysfs_discard,
879 &sysfs_state_rw,
880 &sysfs_label,
881
882 &sysfs_has_data,
883 &sysfs_io_done,
884 &sysfs_io_errors,
885 &sysfs_io_errors_reset,
886
887 &sysfs_io_latency_read,
888 &sysfs_io_latency_write,
889 &sysfs_io_latency_stats_read,
890 &sysfs_io_latency_stats_write,
891 &sysfs_congested,
892
893 /* debug: */
894 &sysfs_alloc_debug,
895 NULL
896};
897
898#endif /* _BCACHEFS_SYSFS_H_ */