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