]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/sysfs.c
Update bcachefs sources to 070ec8d07b bcachefs: Snapshot depth, skiplist fields
[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 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(open_buckets_partial);
199 read_attribute(write_points);
200 read_attribute(nocow_lock_table);
201
202 #ifdef BCH_WRITE_REF_DEBUG
203 read_attribute(write_refs);
204
205 static const char * const bch2_write_refs[] = {
206 #define x(n)    #n,
207         BCH_WRITE_REFS()
208 #undef x
209         NULL
210 };
211
212 static void bch2_write_refs_to_text(struct printbuf *out, struct bch_fs *c)
213 {
214         bch2_printbuf_tabstop_push(out, 24);
215
216         for (unsigned i = 0; i < ARRAY_SIZE(c->writes); i++) {
217                 prt_str(out, bch2_write_refs[i]);
218                 prt_tab(out);
219                 prt_printf(out, "%li", atomic_long_read(&c->writes[i]));
220                 prt_newline(out);
221         }
222 }
223 #endif
224
225 read_attribute(internal_uuid);
226 read_attribute(disk_groups);
227
228 read_attribute(has_data);
229 read_attribute(alloc_debug);
230
231 #define x(t, n, ...) read_attribute(t);
232 BCH_PERSISTENT_COUNTERS()
233 #undef x
234
235 rw_attribute(discard);
236 rw_attribute(label);
237
238 rw_attribute(copy_gc_enabled);
239 read_attribute(copy_gc_wait);
240
241 rw_attribute(rebalance_enabled);
242 sysfs_pd_controller_attribute(rebalance);
243 read_attribute(rebalance_work);
244 rw_attribute(promote_whole_extents);
245
246 read_attribute(new_stripes);
247
248 read_attribute(io_timers_read);
249 read_attribute(io_timers_write);
250
251 read_attribute(data_jobs);
252 read_attribute(moving_ctxts);
253
254 #ifdef CONFIG_BCACHEFS_TESTS
255 write_attribute(perf_test);
256 #endif /* CONFIG_BCACHEFS_TESTS */
257
258 #define x(_name)                                                \
259         static struct attribute sysfs_time_stat_##_name =               \
260                 { .name = #_name, .mode = 0444 };
261         BCH_TIME_STATS()
262 #undef x
263
264 static struct attribute sysfs_state_rw = {
265         .name = "state",
266         .mode =  0444,
267 };
268
269 static size_t bch2_btree_cache_size(struct bch_fs *c)
270 {
271         size_t ret = 0;
272         struct btree *b;
273
274         mutex_lock(&c->btree_cache.lock);
275         list_for_each_entry(b, &c->btree_cache.live, list)
276                 ret += btree_bytes(c);
277
278         mutex_unlock(&c->btree_cache.lock);
279         return ret;
280 }
281
282 static int bch2_compression_stats_to_text(struct printbuf *out, struct bch_fs *c)
283 {
284         struct btree_trans trans;
285         struct btree_iter iter;
286         struct bkey_s_c k;
287         enum btree_id id;
288         u64 nr_uncompressed_extents = 0,
289             nr_compressed_extents = 0,
290             nr_incompressible_extents = 0,
291             uncompressed_sectors = 0,
292             incompressible_sectors = 0,
293             compressed_sectors_compressed = 0,
294             compressed_sectors_uncompressed = 0;
295         int ret;
296
297         if (!test_bit(BCH_FS_STARTED, &c->flags))
298                 return -EPERM;
299
300         bch2_trans_init(&trans, c, 0, 0);
301
302         for (id = 0; id < BTREE_ID_NR; id++) {
303                 if (!btree_type_has_ptrs(id))
304                         continue;
305
306                 for_each_btree_key(&trans, iter, id, POS_MIN,
307                                    BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
308                         struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
309                         const union bch_extent_entry *entry;
310                         struct extent_ptr_decoded p;
311                         bool compressed = false, uncompressed = false, incompressible = false;
312
313                         bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
314                                 switch (p.crc.compression_type) {
315                                 case BCH_COMPRESSION_TYPE_none:
316                                         uncompressed = true;
317                                         uncompressed_sectors += k.k->size;
318                                         break;
319                                 case BCH_COMPRESSION_TYPE_incompressible:
320                                         incompressible = true;
321                                         incompressible_sectors += k.k->size;
322                                         break;
323                                 default:
324                                         compressed_sectors_compressed +=
325                                                 p.crc.compressed_size;
326                                         compressed_sectors_uncompressed +=
327                                                 p.crc.uncompressed_size;
328                                         compressed = true;
329                                         break;
330                                 }
331                         }
332
333                         if (incompressible)
334                                 nr_incompressible_extents++;
335                         else if (uncompressed)
336                                 nr_uncompressed_extents++;
337                         else if (compressed)
338                                 nr_compressed_extents++;
339                 }
340                 bch2_trans_iter_exit(&trans, &iter);
341         }
342
343         bch2_trans_exit(&trans);
344
345         if (ret)
346                 return ret;
347
348         prt_printf(out, "uncompressed:\n");
349         prt_printf(out, "       nr extents:             %llu\n", nr_uncompressed_extents);
350         prt_printf(out, "       size:                   ");
351         prt_human_readable_u64(out, uncompressed_sectors << 9);
352         prt_printf(out, "\n");
353
354         prt_printf(out, "compressed:\n");
355         prt_printf(out, "       nr extents:             %llu\n", nr_compressed_extents);
356         prt_printf(out, "       compressed size:        ");
357         prt_human_readable_u64(out, compressed_sectors_compressed << 9);
358         prt_printf(out, "\n");
359         prt_printf(out, "       uncompressed size:      ");
360         prt_human_readable_u64(out, compressed_sectors_uncompressed << 9);
361         prt_printf(out, "\n");
362
363         prt_printf(out, "incompressible:\n");
364         prt_printf(out, "       nr extents:             %llu\n", nr_incompressible_extents);
365         prt_printf(out, "       size:                   ");
366         prt_human_readable_u64(out, incompressible_sectors << 9);
367         prt_printf(out, "\n");
368         return 0;
369 }
370
371 static void bch2_gc_gens_pos_to_text(struct printbuf *out, struct bch_fs *c)
372 {
373         prt_printf(out, "%s: ", bch2_btree_ids[c->gc_gens_btree]);
374         bch2_bpos_to_text(out, c->gc_gens_pos);
375         prt_printf(out, "\n");
376 }
377
378 static void bch2_btree_wakeup_all(struct bch_fs *c)
379 {
380         struct btree_trans *trans;
381
382         seqmutex_lock(&c->btree_trans_lock);
383         list_for_each_entry(trans, &c->btree_trans_list, list) {
384                 struct btree_bkey_cached_common *b = READ_ONCE(trans->locking);
385
386                 if (b)
387                         six_lock_wakeup_all(&b->lock);
388
389         }
390         seqmutex_unlock(&c->btree_trans_lock);
391 }
392
393 SHOW(bch2_fs)
394 {
395         struct bch_fs *c = container_of(kobj, struct bch_fs, kobj);
396
397         sysfs_print(minor,                      c->minor);
398         sysfs_printf(internal_uuid, "%pU",      c->sb.uuid.b);
399
400         sysfs_hprint(btree_cache_size,          bch2_btree_cache_size(c));
401
402         if (attr == &sysfs_btree_write_stats)
403                 bch2_btree_write_stats_to_text(out, c);
404
405         sysfs_printf(btree_gc_periodic, "%u",   (int) c->btree_gc_periodic);
406
407         if (attr == &sysfs_gc_gens_pos)
408                 bch2_gc_gens_pos_to_text(out, c);
409
410         sysfs_printf(copy_gc_enabled, "%i", c->copy_gc_enabled);
411
412         sysfs_printf(rebalance_enabled,         "%i", c->rebalance.enabled);
413         sysfs_pd_controller_show(rebalance,     &c->rebalance.pd); /* XXX */
414
415         if (attr == &sysfs_copy_gc_wait)
416                 bch2_copygc_wait_to_text(out, c);
417
418         if (attr == &sysfs_rebalance_work)
419                 bch2_rebalance_work_to_text(out, c);
420
421         sysfs_print(promote_whole_extents,      c->promote_whole_extents);
422
423         /* Debugging: */
424
425         if (attr == &sysfs_journal_debug)
426                 bch2_journal_debug_to_text(out, &c->journal);
427
428         if (attr == &sysfs_btree_updates)
429                 bch2_btree_updates_to_text(out, c);
430
431         if (attr == &sysfs_btree_cache)
432                 bch2_btree_cache_to_text(out, &c->btree_cache);
433
434         if (attr == &sysfs_btree_key_cache)
435                 bch2_btree_key_cache_to_text(out, &c->btree_key_cache);
436
437         if (attr == &sysfs_stripes_heap)
438                 bch2_stripes_heap_to_text(out, c);
439
440         if (attr == &sysfs_open_buckets)
441                 bch2_open_buckets_to_text(out, c);
442
443         if (attr == &sysfs_open_buckets_partial)
444                 bch2_open_buckets_partial_to_text(out, c);
445
446         if (attr == &sysfs_write_points)
447                 bch2_write_points_to_text(out, c);
448
449         if (attr == &sysfs_compression_stats)
450                 bch2_compression_stats_to_text(out, c);
451
452         if (attr == &sysfs_new_stripes)
453                 bch2_new_stripes_to_text(out, c);
454
455         if (attr == &sysfs_io_timers_read)
456                 bch2_io_timers_to_text(out, &c->io_clock[READ]);
457
458         if (attr == &sysfs_io_timers_write)
459                 bch2_io_timers_to_text(out, &c->io_clock[WRITE]);
460
461         if (attr == &sysfs_data_jobs)
462                 bch2_data_jobs_to_text(out, c);
463
464         if (attr == &sysfs_moving_ctxts)
465                 bch2_fs_moving_ctxts_to_text(out, c);
466
467 #ifdef BCH_WRITE_REF_DEBUG
468         if (attr == &sysfs_write_refs)
469                 bch2_write_refs_to_text(out, c);
470 #endif
471
472         if (attr == &sysfs_nocow_lock_table)
473                 bch2_nocow_locks_to_text(out, &c->nocow_locks);
474
475         if (attr == &sysfs_disk_groups)
476                 bch2_disk_groups_to_text(out, c);
477
478         return 0;
479 }
480
481 STORE(bch2_fs)
482 {
483         struct bch_fs *c = container_of(kobj, struct bch_fs, kobj);
484
485         if (attr == &sysfs_btree_gc_periodic) {
486                 ssize_t ret = strtoul_safe(buf, c->btree_gc_periodic)
487                         ?: (ssize_t) size;
488
489                 wake_up_process(c->gc_thread);
490                 return ret;
491         }
492
493         if (attr == &sysfs_copy_gc_enabled) {
494                 ssize_t ret = strtoul_safe(buf, c->copy_gc_enabled)
495                         ?: (ssize_t) size;
496
497                 if (c->copygc_thread)
498                         wake_up_process(c->copygc_thread);
499                 return ret;
500         }
501
502         if (attr == &sysfs_rebalance_enabled) {
503                 ssize_t ret = strtoul_safe(buf, c->rebalance.enabled)
504                         ?: (ssize_t) size;
505
506                 rebalance_wakeup(c);
507                 return ret;
508         }
509
510         sysfs_pd_controller_store(rebalance,    &c->rebalance.pd);
511
512         sysfs_strtoul(promote_whole_extents,    c->promote_whole_extents);
513
514         /* Debugging: */
515
516         if (!test_bit(BCH_FS_STARTED, &c->flags))
517                 return -EPERM;
518
519         /* Debugging: */
520
521         if (!test_bit(BCH_FS_RW, &c->flags))
522                 return -EROFS;
523
524         if (attr == &sysfs_prune_cache) {
525                 struct shrink_control sc;
526
527                 sc.gfp_mask = GFP_KERNEL;
528                 sc.nr_to_scan = strtoul_or_return(buf);
529                 c->btree_cache.shrink.scan_objects(&c->btree_cache.shrink, &sc);
530         }
531
532         if (attr == &sysfs_btree_wakeup)
533                 bch2_btree_wakeup_all(c);
534
535         if (attr == &sysfs_trigger_gc) {
536                 /*
537                  * Full gc is currently incompatible with btree key cache:
538                  */
539 #if 0
540                 down_read(&c->state_lock);
541                 bch2_gc(c, false, false);
542                 up_read(&c->state_lock);
543 #else
544                 bch2_gc_gens(c);
545 #endif
546         }
547
548         if (attr == &sysfs_trigger_discards)
549                 bch2_do_discards(c);
550
551         if (attr == &sysfs_trigger_invalidates)
552                 bch2_do_invalidates(c);
553
554 #ifdef CONFIG_BCACHEFS_TESTS
555         if (attr == &sysfs_perf_test) {
556                 char *tmp = kstrdup(buf, GFP_KERNEL), *p = tmp;
557                 char *test              = strsep(&p, " \t\n");
558                 char *nr_str            = strsep(&p, " \t\n");
559                 char *threads_str       = strsep(&p, " \t\n");
560                 unsigned threads;
561                 u64 nr;
562                 int ret = -EINVAL;
563
564                 if (threads_str &&
565                     !(ret = kstrtouint(threads_str, 10, &threads)) &&
566                     !(ret = bch2_strtoull_h(nr_str, &nr)))
567                         ret = bch2_btree_perf_test(c, test, nr, threads);
568                 kfree(tmp);
569
570                 if (ret)
571                         size = ret;
572         }
573 #endif
574         return size;
575 }
576 SYSFS_OPS(bch2_fs);
577
578 struct attribute *bch2_fs_files[] = {
579         &sysfs_minor,
580         &sysfs_btree_cache_size,
581         &sysfs_btree_write_stats,
582
583         &sysfs_promote_whole_extents,
584
585         &sysfs_compression_stats,
586
587 #ifdef CONFIG_BCACHEFS_TESTS
588         &sysfs_perf_test,
589 #endif
590         NULL
591 };
592
593 /* counters dir */
594
595 SHOW(bch2_fs_counters)
596 {
597         struct bch_fs *c = container_of(kobj, struct bch_fs, counters_kobj);
598         u64 counter = 0;
599         u64 counter_since_mount = 0;
600
601         printbuf_tabstop_push(out, 32);
602
603         #define x(t, ...) \
604                 if (attr == &sysfs_##t) {                                       \
605                         counter             = percpu_u64_get(&c->counters[BCH_COUNTER_##t]);\
606                         counter_since_mount = counter - c->counters_on_mount[BCH_COUNTER_##t];\
607                         prt_printf(out, "since mount:");                                \
608                         prt_tab(out);                                           \
609                         prt_human_readable_u64(out, counter_since_mount);       \
610                         prt_newline(out);                                       \
611                                                                                 \
612                         prt_printf(out, "since filesystem creation:");          \
613                         prt_tab(out);                                           \
614                         prt_human_readable_u64(out, counter);                   \
615                         prt_newline(out);                                       \
616                 }
617         BCH_PERSISTENT_COUNTERS()
618         #undef x
619         return 0;
620 }
621
622 STORE(bch2_fs_counters) {
623         return 0;
624 }
625
626 SYSFS_OPS(bch2_fs_counters);
627
628 struct attribute *bch2_fs_counters_files[] = {
629 #define x(t, ...) \
630         &sysfs_##t,
631         BCH_PERSISTENT_COUNTERS()
632 #undef x
633         NULL
634 };
635 /* internal dir - just a wrapper */
636
637 SHOW(bch2_fs_internal)
638 {
639         struct bch_fs *c = container_of(kobj, struct bch_fs, internal);
640
641         return bch2_fs_to_text(out, &c->kobj, attr);
642 }
643
644 STORE(bch2_fs_internal)
645 {
646         struct bch_fs *c = container_of(kobj, struct bch_fs, internal);
647
648         return bch2_fs_store(&c->kobj, attr, buf, size);
649 }
650 SYSFS_OPS(bch2_fs_internal);
651
652 struct attribute *bch2_fs_internal_files[] = {
653         &sysfs_journal_debug,
654         &sysfs_btree_updates,
655         &sysfs_btree_cache,
656         &sysfs_btree_key_cache,
657         &sysfs_new_stripes,
658         &sysfs_stripes_heap,
659         &sysfs_open_buckets,
660         &sysfs_open_buckets_partial,
661         &sysfs_write_points,
662 #ifdef BCH_WRITE_REF_DEBUG
663         &sysfs_write_refs,
664 #endif
665         &sysfs_nocow_lock_table,
666         &sysfs_io_timers_read,
667         &sysfs_io_timers_write,
668
669         &sysfs_trigger_gc,
670         &sysfs_trigger_discards,
671         &sysfs_trigger_invalidates,
672         &sysfs_prune_cache,
673         &sysfs_btree_wakeup,
674
675         &sysfs_gc_gens_pos,
676
677         &sysfs_copy_gc_enabled,
678         &sysfs_copy_gc_wait,
679
680         &sysfs_rebalance_enabled,
681         &sysfs_rebalance_work,
682         sysfs_pd_controller_files(rebalance),
683
684         &sysfs_data_jobs,
685         &sysfs_moving_ctxts,
686
687         &sysfs_internal_uuid,
688
689         &sysfs_disk_groups,
690         NULL
691 };
692
693 /* options */
694
695 SHOW(bch2_fs_opts_dir)
696 {
697         struct bch_fs *c = container_of(kobj, struct bch_fs, opts_dir);
698         const struct bch_option *opt = container_of(attr, struct bch_option, attr);
699         int id = opt - bch2_opt_table;
700         u64 v = bch2_opt_get_by_id(&c->opts, id);
701
702         bch2_opt_to_text(out, c, c->disk_sb.sb, opt, v, OPT_SHOW_FULL_LIST);
703         prt_char(out, '\n');
704
705         return 0;
706 }
707
708 STORE(bch2_fs_opts_dir)
709 {
710         struct bch_fs *c = container_of(kobj, struct bch_fs, opts_dir);
711         const struct bch_option *opt = container_of(attr, struct bch_option, attr);
712         int ret, id = opt - bch2_opt_table;
713         char *tmp;
714         u64 v;
715
716         /*
717          * We don't need to take c->writes for correctness, but it eliminates an
718          * unsightly error message in the dmesg log when we're RO:
719          */
720         if (unlikely(!bch2_write_ref_tryget(c, BCH_WRITE_REF_sysfs)))
721                 return -EROFS;
722
723         tmp = kstrdup(buf, GFP_KERNEL);
724         if (!tmp) {
725                 ret = -ENOMEM;
726                 goto err;
727         }
728
729         ret = bch2_opt_parse(c, opt, strim(tmp), &v, NULL);
730         kfree(tmp);
731
732         if (ret < 0)
733                 goto err;
734
735         ret = bch2_opt_check_may_set(c, id, v);
736         if (ret < 0)
737                 goto err;
738
739         bch2_opt_set_sb(c, opt, v);
740         bch2_opt_set_by_id(&c->opts, id, v);
741
742         if ((id == Opt_background_target ||
743              id == Opt_background_compression) && v) {
744                 bch2_rebalance_add_work(c, S64_MAX);
745                 rebalance_wakeup(c);
746         }
747
748         ret = size;
749 err:
750         bch2_write_ref_put(c, BCH_WRITE_REF_sysfs);
751         return ret;
752 }
753 SYSFS_OPS(bch2_fs_opts_dir);
754
755 struct attribute *bch2_fs_opts_dir_files[] = { NULL };
756
757 int bch2_opts_create_sysfs_files(struct kobject *kobj)
758 {
759         const struct bch_option *i;
760         int ret;
761
762         for (i = bch2_opt_table;
763              i < bch2_opt_table + bch2_opts_nr;
764              i++) {
765                 if (!(i->flags & OPT_FS))
766                         continue;
767
768                 ret = sysfs_create_file(kobj, &i->attr);
769                 if (ret)
770                         return ret;
771         }
772
773         return 0;
774 }
775
776 /* time stats */
777
778 SHOW(bch2_fs_time_stats)
779 {
780         struct bch_fs *c = container_of(kobj, struct bch_fs, time_stats);
781
782 #define x(name)                                                         \
783         if (attr == &sysfs_time_stat_##name)                            \
784                 bch2_time_stats_to_text(out, &c->times[BCH_TIME_##name]);
785         BCH_TIME_STATS()
786 #undef x
787
788         return 0;
789 }
790
791 STORE(bch2_fs_time_stats)
792 {
793         return size;
794 }
795 SYSFS_OPS(bch2_fs_time_stats);
796
797 struct attribute *bch2_fs_time_stats_files[] = {
798 #define x(name)                                         \
799         &sysfs_time_stat_##name,
800         BCH_TIME_STATS()
801 #undef x
802         NULL
803 };
804
805 static void dev_alloc_debug_to_text(struct printbuf *out, struct bch_dev *ca)
806 {
807         struct bch_fs *c = ca->fs;
808         struct bch_dev_usage stats = bch2_dev_usage_read(ca);
809         unsigned i, nr[BCH_DATA_NR];
810
811         memset(nr, 0, sizeof(nr));
812
813         for (i = 0; i < ARRAY_SIZE(c->open_buckets); i++)
814                 nr[c->open_buckets[i].data_type]++;
815
816         printbuf_tabstop_push(out, 8);
817         printbuf_tabstop_push(out, 16);
818         printbuf_tabstop_push(out, 16);
819         printbuf_tabstop_push(out, 16);
820         printbuf_tabstop_push(out, 16);
821
822         prt_tab(out);
823         prt_str(out, "buckets");
824         prt_tab_rjust(out);
825         prt_str(out, "sectors");
826         prt_tab_rjust(out);
827         prt_str(out, "fragmented");
828         prt_tab_rjust(out);
829         prt_newline(out);
830
831         for (i = 0; i < BCH_DATA_NR; i++) {
832                 prt_str(out, bch2_data_types[i]);
833                 prt_tab(out);
834                 prt_u64(out, stats.d[i].buckets);
835                 prt_tab_rjust(out);
836                 prt_u64(out, stats.d[i].sectors);
837                 prt_tab_rjust(out);
838                 prt_u64(out, stats.d[i].fragmented);
839                 prt_tab_rjust(out);
840                 prt_newline(out);
841         }
842
843         prt_str(out, "ec");
844         prt_tab(out);
845         prt_u64(out, stats.buckets_ec);
846         prt_tab_rjust(out);
847         prt_newline(out);
848
849         prt_newline(out);
850
851         prt_printf(out, "reserves:");
852         prt_newline(out);
853         for (i = 0; i < BCH_WATERMARK_NR; i++) {
854                 prt_str(out, bch2_watermarks[i]);
855                 prt_tab(out);
856                 prt_u64(out, bch2_dev_buckets_reserved(ca, i));
857                 prt_tab_rjust(out);
858                 prt_newline(out);
859         }
860
861         prt_newline(out);
862
863         printbuf_tabstops_reset(out);
864         printbuf_tabstop_push(out, 24);
865
866         prt_str(out, "freelist_wait");
867         prt_tab(out);
868         prt_str(out, c->freelist_wait.list.first ? "waiting" : "empty");
869         prt_newline(out);
870
871         prt_str(out, "open buckets allocated");
872         prt_tab(out);
873         prt_u64(out, OPEN_BUCKETS_COUNT - c->open_buckets_nr_free);
874         prt_newline(out);
875
876         prt_str(out, "open buckets this dev");
877         prt_tab(out);
878         prt_u64(out, ca->nr_open_buckets);
879         prt_newline(out);
880
881         prt_str(out, "open buckets total");
882         prt_tab(out);
883         prt_u64(out, OPEN_BUCKETS_COUNT);
884         prt_newline(out);
885
886         prt_str(out, "open_buckets_wait");
887         prt_tab(out);
888         prt_str(out, c->open_buckets_wait.list.first ? "waiting" : "empty");
889         prt_newline(out);
890
891         prt_str(out, "open_buckets_btree");
892         prt_tab(out);
893         prt_u64(out, nr[BCH_DATA_btree]);
894         prt_newline(out);
895
896         prt_str(out, "open_buckets_user");
897         prt_tab(out);
898         prt_u64(out, nr[BCH_DATA_user]);
899         prt_newline(out);
900
901         prt_str(out, "buckets_to_invalidate");
902         prt_tab(out);
903         prt_u64(out, should_invalidate_buckets(ca, stats));
904         prt_newline(out);
905
906         prt_str(out, "btree reserve cache");
907         prt_tab(out);
908         prt_u64(out, c->btree_reserve_cache_nr);
909         prt_newline(out);
910 }
911
912 static const char * const bch2_rw[] = {
913         "read",
914         "write",
915         NULL
916 };
917
918 static void dev_iodone_to_text(struct printbuf *out, struct bch_dev *ca)
919 {
920         int rw, i;
921
922         for (rw = 0; rw < 2; rw++) {
923                 prt_printf(out, "%s:\n", bch2_rw[rw]);
924
925                 for (i = 1; i < BCH_DATA_NR; i++)
926                         prt_printf(out, "%-12s:%12llu\n",
927                                bch2_data_types[i],
928                                percpu_u64_get(&ca->io_done->sectors[rw][i]) << 9);
929         }
930 }
931
932 SHOW(bch2_dev)
933 {
934         struct bch_dev *ca = container_of(kobj, struct bch_dev, kobj);
935         struct bch_fs *c = ca->fs;
936
937         sysfs_printf(uuid,              "%pU\n", ca->uuid.b);
938
939         sysfs_print(bucket_size,        bucket_bytes(ca));
940         sysfs_print(first_bucket,       ca->mi.first_bucket);
941         sysfs_print(nbuckets,           ca->mi.nbuckets);
942         sysfs_print(durability,         ca->mi.durability);
943         sysfs_print(discard,            ca->mi.discard);
944
945         if (attr == &sysfs_label) {
946                 if (ca->mi.group) {
947                         mutex_lock(&c->sb_lock);
948                         bch2_disk_path_to_text(out, c->disk_sb.sb,
949                                                ca->mi.group - 1);
950                         mutex_unlock(&c->sb_lock);
951                 }
952
953                 prt_char(out, '\n');
954         }
955
956         if (attr == &sysfs_has_data) {
957                 prt_bitflags(out, bch2_data_types, bch2_dev_has_data(c, ca));
958                 prt_char(out, '\n');
959         }
960
961         if (attr == &sysfs_state_rw) {
962                 prt_string_option(out, bch2_member_states, ca->mi.state);
963                 prt_char(out, '\n');
964         }
965
966         if (attr == &sysfs_iodone)
967                 dev_iodone_to_text(out, ca);
968
969         sysfs_print(io_latency_read,            atomic64_read(&ca->cur_latency[READ]));
970         sysfs_print(io_latency_write,           atomic64_read(&ca->cur_latency[WRITE]));
971
972         if (attr == &sysfs_io_latency_stats_read)
973                 bch2_time_stats_to_text(out, &ca->io_latency[READ]);
974
975         if (attr == &sysfs_io_latency_stats_write)
976                 bch2_time_stats_to_text(out, &ca->io_latency[WRITE]);
977
978         sysfs_printf(congested,                 "%u%%",
979                      clamp(atomic_read(&ca->congested), 0, CONGESTED_MAX)
980                      * 100 / CONGESTED_MAX);
981
982         if (attr == &sysfs_alloc_debug)
983                 dev_alloc_debug_to_text(out, ca);
984
985         return 0;
986 }
987
988 STORE(bch2_dev)
989 {
990         struct bch_dev *ca = container_of(kobj, struct bch_dev, kobj);
991         struct bch_fs *c = ca->fs;
992         struct bch_member *mi;
993
994         if (attr == &sysfs_discard) {
995                 bool v = strtoul_or_return(buf);
996
997                 mutex_lock(&c->sb_lock);
998                 mi = &bch2_sb_get_members(c->disk_sb.sb)->members[ca->dev_idx];
999
1000                 if (v != BCH_MEMBER_DISCARD(mi)) {
1001                         SET_BCH_MEMBER_DISCARD(mi, v);
1002                         bch2_write_super(c);
1003                 }
1004                 mutex_unlock(&c->sb_lock);
1005         }
1006
1007         if (attr == &sysfs_durability) {
1008                 u64 v = strtoul_or_return(buf);
1009
1010                 mutex_lock(&c->sb_lock);
1011                 mi = &bch2_sb_get_members(c->disk_sb.sb)->members[ca->dev_idx];
1012
1013                 if (v != BCH_MEMBER_DURABILITY(mi)) {
1014                         SET_BCH_MEMBER_DURABILITY(mi, v + 1);
1015                         bch2_write_super(c);
1016                 }
1017                 mutex_unlock(&c->sb_lock);
1018         }
1019
1020         if (attr == &sysfs_label) {
1021                 char *tmp;
1022                 int ret;
1023
1024                 tmp = kstrdup(buf, GFP_KERNEL);
1025                 if (!tmp)
1026                         return -ENOMEM;
1027
1028                 ret = bch2_dev_group_set(c, ca, strim(tmp));
1029                 kfree(tmp);
1030                 if (ret)
1031                         return ret;
1032         }
1033
1034         return size;
1035 }
1036 SYSFS_OPS(bch2_dev);
1037
1038 struct attribute *bch2_dev_files[] = {
1039         &sysfs_uuid,
1040         &sysfs_bucket_size,
1041         &sysfs_first_bucket,
1042         &sysfs_nbuckets,
1043         &sysfs_durability,
1044
1045         /* settings: */
1046         &sysfs_discard,
1047         &sysfs_state_rw,
1048         &sysfs_label,
1049
1050         &sysfs_has_data,
1051         &sysfs_iodone,
1052
1053         &sysfs_io_latency_read,
1054         &sysfs_io_latency_write,
1055         &sysfs_io_latency_stats_read,
1056         &sysfs_io_latency_stats_write,
1057         &sysfs_congested,
1058
1059         /* debug: */
1060         &sysfs_alloc_debug,
1061         NULL
1062 };
1063
1064 #endif  /* _BCACHEFS_SYSFS_H_ */