]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/sysfs.c
Update bcachefs sources to da037866e6
[bcachefs-tools-debian] / libbcachefs / sysfs.c
1 /*
2  * bcache sysfs interfaces
3  *
4  * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com>
5  * Copyright 2012 Google, Inc.
6  */
7
8 #include "bcachefs.h"
9 #include "alloc.h"
10 #include "compress.h"
11 #include "sysfs.h"
12 #include "btree_cache.h"
13 #include "btree_iter.h"
14 #include "btree_update.h"
15 #include "btree_gc.h"
16 #include "buckets.h"
17 #include "inode.h"
18 #include "journal.h"
19 #include "keylist.h"
20 #include "move.h"
21 #include "opts.h"
22 #include "super-io.h"
23 #include "tier.h"
24
25 #include <linux/blkdev.h>
26 #include <linux/sort.h>
27
28 #include "util.h"
29
30 #define SYSFS_OPS(type)                                                 \
31 struct sysfs_ops type ## _sysfs_ops = {                                 \
32         .show   = type ## _show,                                        \
33         .store  = type ## _store                                        \
34 }
35
36 #define SHOW(fn)                                                        \
37 static ssize_t fn ## _show(struct kobject *kobj, struct attribute *attr,\
38                            char *buf)                                   \
39
40 #define STORE(fn)                                                       \
41 static ssize_t fn ## _store(struct kobject *kobj, struct attribute *attr,\
42                             const char *buf, size_t size)               \
43
44 #define __sysfs_attribute(_name, _mode)                                 \
45         static struct attribute sysfs_##_name =                         \
46                 { .name = #_name, .mode = _mode }
47
48 #define write_attribute(n)      __sysfs_attribute(n, S_IWUSR)
49 #define read_attribute(n)       __sysfs_attribute(n, S_IRUGO)
50 #define rw_attribute(n)         __sysfs_attribute(n, S_IRUGO|S_IWUSR)
51
52 #define sysfs_printf(file, fmt, ...)                                    \
53 do {                                                                    \
54         if (attr == &sysfs_ ## file)                                    \
55                 return snprintf(buf, PAGE_SIZE, fmt "\n", __VA_ARGS__); \
56 } while (0)
57
58 #define sysfs_print(file, var)                                          \
59 do {                                                                    \
60         if (attr == &sysfs_ ## file)                                    \
61                 return snprint(buf, PAGE_SIZE, var);                    \
62 } while (0)
63
64 #define sysfs_hprint(file, val)                                         \
65 do {                                                                    \
66         if (attr == &sysfs_ ## file) {                                  \
67                 ssize_t ret = bch2_hprint(buf, val);                    \
68                 strcat(buf, "\n");                                      \
69                 return ret + 1;                                         \
70         }                                                               \
71 } while (0)
72
73 #define var_printf(_var, fmt)   sysfs_printf(_var, fmt, var(_var))
74 #define var_print(_var)         sysfs_print(_var, var(_var))
75 #define var_hprint(_var)        sysfs_hprint(_var, var(_var))
76
77 #define sysfs_strtoul(file, var)                                        \
78 do {                                                                    \
79         if (attr == &sysfs_ ## file)                                    \
80                 return strtoul_safe(buf, var) ?: (ssize_t) size;        \
81 } while (0)
82
83 #define sysfs_strtoul_clamp(file, var, min, max)                        \
84 do {                                                                    \
85         if (attr == &sysfs_ ## file)                                    \
86                 return strtoul_safe_clamp(buf, var, min, max)           \
87                         ?: (ssize_t) size;                              \
88 } while (0)
89
90 #define strtoul_or_return(cp)                                           \
91 ({                                                                      \
92         unsigned long _v;                                               \
93         int _r = kstrtoul(cp, 10, &_v);                                 \
94         if (_r)                                                         \
95                 return _r;                                              \
96         _v;                                                             \
97 })
98
99 #define strtoul_restrict_or_return(cp, min, max)                        \
100 ({                                                                      \
101         unsigned long __v = 0;                                          \
102         int _r = strtoul_safe_restrict(cp, __v, min, max);              \
103         if (_r)                                                         \
104                 return _r;                                              \
105         __v;                                                            \
106 })
107
108 #define strtoi_h_or_return(cp)                                          \
109 ({                                                                      \
110         u64 _v;                                                         \
111         int _r = strtoi_h(cp, &_v);                                     \
112         if (_r)                                                         \
113                 return _r;                                              \
114         _v;                                                             \
115 })
116
117 #define sysfs_hatoi(file, var)                                          \
118 do {                                                                    \
119         if (attr == &sysfs_ ## file)                                    \
120                 return strtoi_h(buf, &var) ?: (ssize_t) size;           \
121 } while (0)
122
123 write_attribute(trigger_btree_coalesce);
124 write_attribute(trigger_gc);
125 write_attribute(prune_cache);
126
127 read_attribute(uuid);
128 read_attribute(minor);
129 read_attribute(bucket_size);
130 read_attribute(bucket_size_bytes);
131 read_attribute(block_size);
132 read_attribute(block_size_bytes);
133 read_attribute(btree_node_size);
134 read_attribute(btree_node_size_bytes);
135 read_attribute(first_bucket);
136 read_attribute(nbuckets);
137 read_attribute(tree_depth);
138 read_attribute(root_usage_percent);
139 read_attribute(read_priority_stats);
140 read_attribute(write_priority_stats);
141 read_attribute(fragmentation_stats);
142 read_attribute(oldest_gen_stats);
143 read_attribute(reserve_stats);
144 read_attribute(btree_cache_size);
145 read_attribute(cache_available_percent);
146 read_attribute(compression_stats);
147 read_attribute(written);
148 read_attribute(btree_written);
149 read_attribute(metadata_written);
150 read_attribute(journal_debug);
151 write_attribute(journal_flush);
152 read_attribute(internal_uuid);
153
154 read_attribute(btree_gc_running);
155
156 read_attribute(btree_nodes);
157 read_attribute(btree_used_percent);
158 read_attribute(average_key_size);
159 read_attribute(available_buckets);
160 read_attribute(free_buckets);
161 read_attribute(dirty_data);
162 read_attribute(dirty_bytes);
163 read_attribute(dirty_buckets);
164 read_attribute(cached_data);
165 read_attribute(cached_bytes);
166 read_attribute(cached_buckets);
167 read_attribute(meta_buckets);
168 read_attribute(alloc_buckets);
169 read_attribute(has_data);
170 read_attribute(has_metadata);
171 read_attribute(bset_tree_stats);
172 read_attribute(alloc_debug);
173
174 read_attribute(cache_read_races);
175
176 rw_attribute(journal_write_delay_ms);
177 rw_attribute(journal_reclaim_delay_ms);
178
179 rw_attribute(discard);
180 rw_attribute(cache_replacement_policy);
181
182 rw_attribute(foreground_write_ratelimit_enabled);
183 rw_attribute(copy_gc_enabled);
184 sysfs_pd_controller_attribute(copy_gc);
185
186 rw_attribute(tier);
187 rw_attribute(tiering_enabled);
188 rw_attribute(tiering_percent);
189 sysfs_pd_controller_attribute(tiering);
190
191 sysfs_pd_controller_attribute(foreground_write);
192
193 rw_attribute(pd_controllers_update_seconds);
194
195 rw_attribute(foreground_target_percent);
196
197 read_attribute(meta_replicas_have);
198 read_attribute(data_replicas_have);
199
200 #define BCH_DEBUG_PARAM(name, description)                              \
201         rw_attribute(name);
202
203         BCH_DEBUG_PARAMS()
204 #undef BCH_DEBUG_PARAM
205
206 #define BCH_OPT(_name, _mode, ...)                                      \
207         static struct attribute sysfs_opt_##_name = {                   \
208                 .name = #_name, .mode = _mode,                          \
209         };
210
211         BCH_VISIBLE_OPTS()
212 #undef BCH_OPT
213
214 #define BCH_TIME_STAT(name, frequency_units, duration_units)            \
215         sysfs_time_stats_attribute(name, frequency_units, duration_units);
216         BCH_TIME_STATS()
217 #undef BCH_TIME_STAT
218
219 static struct attribute sysfs_state_rw = {
220         .name = "state",
221         .mode = S_IRUGO
222 };
223
224 static int bch2_bset_print_stats(struct bch_fs *c, char *buf)
225 {
226         struct bset_stats stats;
227         size_t nodes = 0;
228         struct btree *b;
229         struct bucket_table *tbl;
230         struct rhash_head *pos;
231         unsigned iter;
232
233         memset(&stats, 0, sizeof(stats));
234
235         rcu_read_lock();
236         for_each_cached_btree(b, c, tbl, iter, pos) {
237                 bch2_btree_keys_stats(b, &stats);
238                 nodes++;
239         }
240         rcu_read_unlock();
241
242         return snprintf(buf, PAGE_SIZE,
243                         "btree nodes:           %zu\n"
244                         "written sets:          %zu\n"
245                         "written key bytes:     %zu\n"
246                         "unwritten sets:                %zu\n"
247                         "unwritten key bytes:   %zu\n"
248                         "no table sets:         %zu\n"
249                         "no table key bytes:    %zu\n"
250                         "floats:                        %zu\n"
251                         "failed unpacked:       %zu\n"
252                         "failed prev:           %zu\n"
253                         "failed overflow:       %zu\n",
254                         nodes,
255                         stats.sets[BSET_RO_AUX_TREE].nr,
256                         stats.sets[BSET_RO_AUX_TREE].bytes,
257                         stats.sets[BSET_RW_AUX_TREE].nr,
258                         stats.sets[BSET_RW_AUX_TREE].bytes,
259                         stats.sets[BSET_NO_AUX_TREE].nr,
260                         stats.sets[BSET_NO_AUX_TREE].bytes,
261                         stats.floats,
262                         stats.failed_unpacked,
263                         stats.failed_prev,
264                         stats.failed_overflow);
265 }
266
267 static unsigned bch2_root_usage(struct bch_fs *c)
268 {
269         unsigned bytes = 0;
270         struct bkey_packed *k;
271         struct btree *b;
272         struct btree_node_iter iter;
273
274         goto lock_root;
275
276         do {
277                 six_unlock_read(&b->lock);
278 lock_root:
279                 b = c->btree_roots[BTREE_ID_EXTENTS].b;
280                 six_lock_read(&b->lock);
281         } while (b != c->btree_roots[BTREE_ID_EXTENTS].b);
282
283         for_each_btree_node_key(b, k, &iter, btree_node_is_extents(b))
284                 bytes += bkey_bytes(k);
285
286         six_unlock_read(&b->lock);
287
288         return (bytes * 100) / btree_bytes(c);
289 }
290
291 static size_t bch2_btree_cache_size(struct bch_fs *c)
292 {
293         size_t ret = 0;
294         struct btree *b;
295
296         mutex_lock(&c->btree_cache_lock);
297         list_for_each_entry(b, &c->btree_cache, list)
298                 ret += btree_bytes(c);
299
300         mutex_unlock(&c->btree_cache_lock);
301         return ret;
302 }
303
304 static unsigned bch2_fs_available_percent(struct bch_fs *c)
305 {
306         return div64_u64((u64) sectors_available(c) * 100,
307                          c->capacity ?: 1);
308 }
309
310 #if 0
311 static unsigned bch2_btree_used(struct bch_fs *c)
312 {
313         return div64_u64(c->gc_stats.key_bytes * 100,
314                          (c->gc_stats.nodes ?: 1) * btree_bytes(c));
315 }
316
317 static unsigned bch2_average_key_size(struct bch_fs *c)
318 {
319         return c->gc_stats.nkeys
320                 ? div64_u64(c->gc_stats.data, c->gc_stats.nkeys)
321                 : 0;
322 }
323 #endif
324
325 static ssize_t show_fs_alloc_debug(struct bch_fs *c, char *buf)
326 {
327         struct bch_fs_usage stats = bch2_fs_usage_read(c);
328
329         return scnprintf(buf, PAGE_SIZE,
330                          "capacity:\t\t%llu\n"
331                          "compressed:\n"
332                          "\tmeta:\t\t%llu\n"
333                          "\tdirty:\t\t%llu\n"
334                          "\tcached:\t\t%llu\n"
335                          "uncompressed:\n"
336                          "\tmeta:\t\t%llu\n"
337                          "\tdirty:\t\t%llu\n"
338                          "\tcached:\t\t%llu\n"
339                          "persistent reserved sectors:\t%llu\n"
340                          "online reserved sectors:\t%llu\n",
341                          c->capacity,
342                          stats.s[S_COMPRESSED][S_META],
343                          stats.s[S_COMPRESSED][S_DIRTY],
344                          stats.s[S_COMPRESSED][S_CACHED],
345                          stats.s[S_UNCOMPRESSED][S_META],
346                          stats.s[S_UNCOMPRESSED][S_DIRTY],
347                          stats.s[S_UNCOMPRESSED][S_CACHED],
348                          stats.persistent_reserved,
349                          stats.online_reserved);
350 }
351
352 static ssize_t bch2_compression_stats(struct bch_fs *c, char *buf)
353 {
354         struct btree_iter iter;
355         struct bkey_s_c k;
356         u64 nr_uncompressed_extents = 0, uncompressed_sectors = 0,
357             nr_compressed_extents = 0,
358             compressed_sectors_compressed = 0,
359             compressed_sectors_uncompressed = 0;
360
361         for_each_btree_key(&iter, c, BTREE_ID_EXTENTS, POS_MIN, k)
362                 if (k.k->type == BCH_EXTENT) {
363                         struct bkey_s_c_extent e = bkey_s_c_to_extent(k);
364                         const struct bch_extent_ptr *ptr;
365                         const union bch_extent_crc *crc;
366
367                         extent_for_each_ptr_crc(e, ptr, crc) {
368                                 if (crc_compression_type(crc) == BCH_COMPRESSION_NONE) {
369                                         nr_uncompressed_extents++;
370                                         uncompressed_sectors += e.k->size;
371                                 } else {
372                                         nr_compressed_extents++;
373                                         compressed_sectors_compressed +=
374                                                 crc_compressed_size(e.k, crc);
375                                         compressed_sectors_uncompressed +=
376                                                 crc_uncompressed_size(e.k, crc);
377                                 }
378
379                                 /* only looking at the first ptr */
380                                 break;
381                         }
382                 }
383         bch2_btree_iter_unlock(&iter);
384
385         return snprintf(buf, PAGE_SIZE,
386                         "uncompressed data:\n"
387                         "       nr extents:                     %llu\n"
388                         "       size (bytes):                   %llu\n"
389                         "compressed data:\n"
390                         "       nr extents:                     %llu\n"
391                         "       compressed size (bytes):        %llu\n"
392                         "       uncompressed size (bytes):      %llu\n",
393                         nr_uncompressed_extents,
394                         uncompressed_sectors << 9,
395                         nr_compressed_extents,
396                         compressed_sectors_compressed << 9,
397                         compressed_sectors_uncompressed << 9);
398 }
399
400 SHOW(bch2_fs)
401 {
402         struct bch_fs *c = container_of(kobj, struct bch_fs, kobj);
403
404         sysfs_print(minor,                      c->minor);
405
406         sysfs_print(journal_write_delay_ms,     c->journal.write_delay_ms);
407         sysfs_print(journal_reclaim_delay_ms,   c->journal.reclaim_delay_ms);
408
409         sysfs_hprint(block_size,                block_bytes(c));
410         sysfs_print(block_size_bytes,           block_bytes(c));
411         sysfs_hprint(btree_node_size,           c->sb.btree_node_size << 9);
412         sysfs_print(btree_node_size_bytes,      c->sb.btree_node_size << 9);
413
414         sysfs_hprint(btree_cache_size,          bch2_btree_cache_size(c));
415         sysfs_print(cache_available_percent,    bch2_fs_available_percent(c));
416
417         sysfs_print(btree_gc_running,           c->gc_pos.phase != GC_PHASE_DONE);
418
419 #if 0
420         /* XXX: reimplement */
421         sysfs_print(btree_used_percent, bch2_btree_used(c));
422         sysfs_print(btree_nodes,        c->gc_stats.nodes);
423         sysfs_hprint(average_key_size,  bch2_average_key_size(c));
424 #endif
425
426         sysfs_print(cache_read_races,
427                     atomic_long_read(&c->cache_read_races));
428
429         sysfs_printf(foreground_write_ratelimit_enabled, "%i",
430                      c->foreground_write_ratelimit_enabled);
431         sysfs_printf(copy_gc_enabled, "%i", c->copy_gc_enabled);
432         sysfs_pd_controller_show(foreground_write, &c->foreground_write_pd);
433
434         sysfs_print(pd_controllers_update_seconds,
435                     c->pd_controllers_update_seconds);
436         sysfs_print(foreground_target_percent, c->foreground_target_percent);
437
438         sysfs_printf(tiering_enabled,           "%i", c->tiering_enabled);
439         sysfs_print(tiering_percent,            c->tiering_percent);
440
441         sysfs_pd_controller_show(tiering,       &c->tiers[1].pd); /* XXX */
442
443         sysfs_printf(meta_replicas_have, "%u",  c->sb.meta_replicas_have);
444         sysfs_printf(data_replicas_have, "%u",  c->sb.data_replicas_have);
445
446         /* Debugging: */
447
448         if (attr == &sysfs_journal_debug)
449                 return bch2_journal_print_debug(&c->journal, buf);
450
451 #define BCH_DEBUG_PARAM(name, description) sysfs_print(name, c->name);
452         BCH_DEBUG_PARAMS()
453 #undef BCH_DEBUG_PARAM
454
455         if (!bch2_fs_running(c))
456                 return -EPERM;
457
458         if (attr == &sysfs_bset_tree_stats)
459                 return bch2_bset_print_stats(c, buf);
460         if (attr == &sysfs_alloc_debug)
461                 return show_fs_alloc_debug(c, buf);
462
463         sysfs_print(tree_depth, c->btree_roots[BTREE_ID_EXTENTS].b->level);
464         sysfs_print(root_usage_percent,         bch2_root_usage(c));
465
466         if (attr == &sysfs_compression_stats)
467                 return bch2_compression_stats(c, buf);
468
469         sysfs_printf(internal_uuid, "%pU", c->sb.uuid.b);
470
471         return 0;
472 }
473
474 STORE(__bch2_fs)
475 {
476         struct bch_fs *c = container_of(kobj, struct bch_fs, kobj);
477
478         sysfs_strtoul(journal_write_delay_ms, c->journal.write_delay_ms);
479         sysfs_strtoul(journal_reclaim_delay_ms, c->journal.reclaim_delay_ms);
480
481         sysfs_strtoul(foreground_write_ratelimit_enabled,
482                       c->foreground_write_ratelimit_enabled);
483
484         if (attr == &sysfs_copy_gc_enabled) {
485                 struct bch_dev *ca;
486                 unsigned i;
487                 ssize_t ret = strtoul_safe(buf, c->copy_gc_enabled)
488                         ?: (ssize_t) size;
489
490                 for_each_member_device(ca, c, i)
491                         if (ca->moving_gc_read)
492                                 wake_up_process(ca->moving_gc_read);
493                 return ret;
494         }
495
496         if (attr == &sysfs_tiering_enabled) {
497                 ssize_t ret = strtoul_safe(buf, c->tiering_enabled)
498                         ?: (ssize_t) size;
499
500                 bch2_tiering_start(c); /* issue wakeups */
501                 return ret;
502         }
503
504         sysfs_pd_controller_store(foreground_write, &c->foreground_write_pd);
505
506         sysfs_strtoul(pd_controllers_update_seconds,
507                       c->pd_controllers_update_seconds);
508         sysfs_strtoul(foreground_target_percent, c->foreground_target_percent);
509
510         sysfs_strtoul(tiering_percent,          c->tiering_percent);
511         sysfs_pd_controller_store(tiering,      &c->tiers[1].pd); /* XXX */
512
513         /* Debugging: */
514
515 #define BCH_DEBUG_PARAM(name, description) sysfs_strtoul(name, c->name);
516         BCH_DEBUG_PARAMS()
517 #undef BCH_DEBUG_PARAM
518
519         if (!bch2_fs_running(c))
520                 return -EPERM;
521
522         if (attr == &sysfs_journal_flush) {
523                 bch2_journal_meta_async(&c->journal, NULL);
524
525                 return size;
526         }
527
528         if (attr == &sysfs_trigger_btree_coalesce)
529                 bch2_coalesce(c);
530
531         /* Debugging: */
532
533         if (attr == &sysfs_trigger_gc)
534                 bch2_gc(c);
535
536         if (attr == &sysfs_prune_cache) {
537                 struct shrink_control sc;
538
539                 sc.gfp_mask = GFP_KERNEL;
540                 sc.nr_to_scan = strtoul_or_return(buf);
541                 c->btree_cache_shrink.scan_objects(&c->btree_cache_shrink, &sc);
542         }
543
544         return size;
545 }
546
547 STORE(bch2_fs)
548 {
549         struct bch_fs *c = container_of(kobj, struct bch_fs, kobj);
550
551         mutex_lock(&c->state_lock);
552         size = __bch2_fs_store(kobj, attr, buf, size);
553         mutex_unlock(&c->state_lock);
554
555         return size;
556 }
557 SYSFS_OPS(bch2_fs);
558
559 struct attribute *bch2_fs_files[] = {
560         &sysfs_journal_write_delay_ms,
561         &sysfs_journal_reclaim_delay_ms,
562
563         &sysfs_block_size,
564         &sysfs_block_size_bytes,
565         &sysfs_btree_node_size,
566         &sysfs_btree_node_size_bytes,
567         &sysfs_tree_depth,
568         &sysfs_root_usage_percent,
569         &sysfs_btree_cache_size,
570         &sysfs_cache_available_percent,
571         &sysfs_compression_stats,
572
573         &sysfs_average_key_size,
574
575         &sysfs_meta_replicas_have,
576         &sysfs_data_replicas_have,
577
578         &sysfs_foreground_target_percent,
579         &sysfs_tiering_percent,
580
581         &sysfs_journal_flush,
582         NULL
583 };
584
585 /* internal dir - just a wrapper */
586
587 SHOW(bch2_fs_internal)
588 {
589         struct bch_fs *c = container_of(kobj, struct bch_fs, internal);
590         return bch2_fs_show(&c->kobj, attr, buf);
591 }
592
593 STORE(bch2_fs_internal)
594 {
595         struct bch_fs *c = container_of(kobj, struct bch_fs, internal);
596         return bch2_fs_store(&c->kobj, attr, buf, size);
597 }
598 SYSFS_OPS(bch2_fs_internal);
599
600 struct attribute *bch2_fs_internal_files[] = {
601         &sysfs_journal_debug,
602
603         &sysfs_alloc_debug,
604
605         &sysfs_btree_gc_running,
606
607         &sysfs_btree_nodes,
608         &sysfs_btree_used_percent,
609
610         &sysfs_bset_tree_stats,
611         &sysfs_cache_read_races,
612
613         &sysfs_trigger_btree_coalesce,
614         &sysfs_trigger_gc,
615         &sysfs_prune_cache,
616         &sysfs_foreground_write_ratelimit_enabled,
617         &sysfs_copy_gc_enabled,
618         &sysfs_tiering_enabled,
619         sysfs_pd_controller_files(tiering),
620         sysfs_pd_controller_files(foreground_write),
621         &sysfs_internal_uuid,
622
623 #define BCH_DEBUG_PARAM(name, description) &sysfs_##name,
624         BCH_DEBUG_PARAMS()
625 #undef BCH_DEBUG_PARAM
626
627         NULL
628 };
629
630 /* options */
631
632 SHOW(bch2_fs_opts_dir)
633 {
634         struct bch_fs *c = container_of(kobj, struct bch_fs, opts_dir);
635
636         return bch2_opt_show(&c->opts, attr->name, buf, PAGE_SIZE);
637 }
638
639 STORE(bch2_fs_opts_dir)
640 {
641         struct bch_fs *c = container_of(kobj, struct bch_fs, opts_dir);
642         const struct bch_option *opt;
643         enum bch_opt_id id;
644         u64 v;
645
646         id = bch2_parse_sysfs_opt(attr->name, buf, &v);
647         if (id < 0)
648                 return id;
649
650         opt = &bch2_opt_table[id];
651
652         mutex_lock(&c->sb_lock);
653
654         if (id == Opt_compression) {
655                 int ret = bch2_check_set_has_compressed_data(c, v);
656                 if (ret) {
657                         mutex_unlock(&c->sb_lock);
658                         return ret;
659                 }
660         }
661
662         if (opt->set_sb != SET_NO_SB_OPT) {
663                 opt->set_sb(c->disk_sb, v);
664                 bch2_write_super(c);
665         }
666
667         bch2_opt_set(&c->opts, id, v);
668
669         mutex_unlock(&c->sb_lock);
670
671         return size;
672 }
673 SYSFS_OPS(bch2_fs_opts_dir);
674
675 struct attribute *bch2_fs_opts_dir_files[] = {
676 #define BCH_OPT(_name, ...)                                             \
677         &sysfs_opt_##_name,
678
679         BCH_VISIBLE_OPTS()
680 #undef BCH_OPT
681
682         NULL
683 };
684
685 /* time stats */
686
687 SHOW(bch2_fs_time_stats)
688 {
689         struct bch_fs *c = container_of(kobj, struct bch_fs, time_stats);
690
691 #define BCH_TIME_STAT(name, frequency_units, duration_units)            \
692         sysfs_print_time_stats(&c->name##_time, name,                   \
693                                frequency_units, duration_units);
694         BCH_TIME_STATS()
695 #undef BCH_TIME_STAT
696
697         return 0;
698 }
699
700 STORE(bch2_fs_time_stats)
701 {
702         struct bch_fs *c = container_of(kobj, struct bch_fs, time_stats);
703
704 #define BCH_TIME_STAT(name, frequency_units, duration_units)            \
705         sysfs_clear_time_stats(&c->name##_time, name);
706         BCH_TIME_STATS()
707 #undef BCH_TIME_STAT
708
709         return size;
710 }
711 SYSFS_OPS(bch2_fs_time_stats);
712
713 struct attribute *bch2_fs_time_stats_files[] = {
714 #define BCH_TIME_STAT(name, frequency_units, duration_units)            \
715         sysfs_time_stats_attribute_list(name, frequency_units, duration_units)
716         BCH_TIME_STATS()
717 #undef BCH_TIME_STAT
718
719         NULL
720 };
721
722 typedef unsigned (bucket_map_fn)(struct bch_dev *, struct bucket *, void *);
723
724 static unsigned bucket_priority_fn(struct bch_dev *ca, struct bucket *g,
725                                    void *private)
726 {
727         int rw = (private ? 1 : 0);
728
729         return ca->fs->prio_clock[rw].hand - g->prio[rw];
730 }
731
732 static unsigned bucket_sectors_used_fn(struct bch_dev *ca, struct bucket *g,
733                                        void *private)
734 {
735         return bucket_sectors_used(g);
736 }
737
738 static unsigned bucket_oldest_gen_fn(struct bch_dev *ca, struct bucket *g,
739                                      void *private)
740 {
741         return bucket_gc_gen(ca, g);
742 }
743
744 static ssize_t show_quantiles(struct bch_dev *ca, char *buf,
745                               bucket_map_fn *fn, void *private)
746 {
747         int cmp(const void *l, const void *r)
748         {       return *((unsigned *) r) - *((unsigned *) l); }
749
750         size_t n = ca->mi.nbuckets, i;
751         /* Compute 31 quantiles */
752         unsigned q[31], *p;
753         ssize_t ret = 0;
754
755         p = vzalloc(ca->mi.nbuckets * sizeof(unsigned));
756         if (!p)
757                 return -ENOMEM;
758
759         for (i = ca->mi.first_bucket; i < n; i++)
760                 p[i] = fn(ca, &ca->buckets[i], private);
761
762         sort(p, n, sizeof(unsigned), cmp, NULL);
763
764         while (n &&
765                !p[n - 1])
766                 --n;
767
768         for (i = 0; i < ARRAY_SIZE(q); i++)
769                 q[i] = p[n * (i + 1) / (ARRAY_SIZE(q) + 1)];
770
771         vfree(p);
772
773         for (i = 0; i < ARRAY_SIZE(q); i++)
774                 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
775                                  "%u ", q[i]);
776         buf[ret - 1] = '\n';
777
778         return ret;
779
780 }
781
782 static ssize_t show_reserve_stats(struct bch_dev *ca, char *buf)
783 {
784         enum alloc_reserve i;
785         ssize_t ret;
786
787         spin_lock(&ca->freelist_lock);
788
789         ret = scnprintf(buf, PAGE_SIZE,
790                         "free_inc:\t%zu\t%zu\n",
791                         fifo_used(&ca->free_inc),
792                         ca->free_inc.size);
793
794         for (i = 0; i < RESERVE_NR; i++)
795                 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
796                                  "free[%u]:\t%zu\t%zu\n", i,
797                                  fifo_used(&ca->free[i]),
798                                  ca->free[i].size);
799
800         spin_unlock(&ca->freelist_lock);
801
802         return ret;
803 }
804
805 static ssize_t show_dev_alloc_debug(struct bch_dev *ca, char *buf)
806 {
807         struct bch_fs *c = ca->fs;
808         struct bch_dev_usage stats = bch2_dev_usage_read(ca);
809
810         return scnprintf(buf, PAGE_SIZE,
811                 "free_inc:               %zu/%zu\n"
812                 "free[RESERVE_PRIO]:     %zu/%zu\n"
813                 "free[RESERVE_BTREE]:    %zu/%zu\n"
814                 "free[RESERVE_MOVINGGC]: %zu/%zu\n"
815                 "free[RESERVE_NONE]:     %zu/%zu\n"
816                 "alloc:                  %llu/%llu\n"
817                 "meta:                   %llu/%llu\n"
818                 "dirty:                  %llu/%llu\n"
819                 "available:              %llu/%llu\n"
820                 "freelist_wait:          %s\n"
821                 "open buckets:           %u/%u (reserved %u)\n"
822                 "open_buckets_wait:      %s\n",
823                 fifo_used(&ca->free_inc),               ca->free_inc.size,
824                 fifo_used(&ca->free[RESERVE_PRIO]),     ca->free[RESERVE_PRIO].size,
825                 fifo_used(&ca->free[RESERVE_BTREE]),    ca->free[RESERVE_BTREE].size,
826                 fifo_used(&ca->free[RESERVE_MOVINGGC]), ca->free[RESERVE_MOVINGGC].size,
827                 fifo_used(&ca->free[RESERVE_NONE]),     ca->free[RESERVE_NONE].size,
828                 stats.buckets_alloc,                    ca->mi.nbuckets - ca->mi.first_bucket,
829                 stats.buckets_meta,                     ca->mi.nbuckets - ca->mi.first_bucket,
830                 stats.buckets_dirty,                    ca->mi.nbuckets - ca->mi.first_bucket,
831                 __dev_buckets_available(ca, stats),     ca->mi.nbuckets - ca->mi.first_bucket,
832                 c->freelist_wait.list.first             ? "waiting" : "empty",
833                 c->open_buckets_nr_free, OPEN_BUCKETS_COUNT, BTREE_NODE_RESERVE,
834                 c->open_buckets_wait.list.first         ? "waiting" : "empty");
835 }
836
837 static u64 sectors_written(struct bch_dev *ca)
838 {
839         u64 ret = 0;
840         int cpu;
841
842         for_each_possible_cpu(cpu)
843                 ret += *per_cpu_ptr(ca->sectors_written, cpu);
844
845         return ret;
846 }
847
848 SHOW(bch2_dev)
849 {
850         struct bch_dev *ca = container_of(kobj, struct bch_dev, kobj);
851         struct bch_fs *c = ca->fs;
852         struct bch_dev_usage stats = bch2_dev_usage_read(ca);
853
854         sysfs_printf(uuid,              "%pU\n", ca->uuid.b);
855
856         sysfs_hprint(bucket_size,       bucket_bytes(ca));
857         sysfs_print(bucket_size_bytes,  bucket_bytes(ca));
858         sysfs_hprint(block_size,        block_bytes(c));
859         sysfs_print(block_size_bytes,   block_bytes(c));
860         sysfs_print(first_bucket,       ca->mi.first_bucket);
861         sysfs_print(nbuckets,           ca->mi.nbuckets);
862         sysfs_print(discard,            ca->mi.discard);
863         sysfs_hprint(written, sectors_written(ca) << 9);
864         sysfs_hprint(btree_written,
865                      atomic64_read(&ca->btree_sectors_written) << 9);
866         sysfs_hprint(metadata_written,
867                      (atomic64_read(&ca->meta_sectors_written) +
868                       atomic64_read(&ca->btree_sectors_written)) << 9);
869
870         sysfs_hprint(dirty_data,        stats.sectors[S_DIRTY] << 9);
871         sysfs_print(dirty_bytes,        stats.sectors[S_DIRTY] << 9);
872         sysfs_print(dirty_buckets,      stats.buckets_dirty);
873         sysfs_hprint(cached_data,       stats.sectors[S_CACHED] << 9);
874         sysfs_print(cached_bytes,       stats.sectors[S_CACHED] << 9);
875         sysfs_print(cached_buckets,     stats.buckets_cached);
876         sysfs_print(meta_buckets,       stats.buckets_meta);
877         sysfs_print(alloc_buckets,      stats.buckets_alloc);
878         sysfs_print(available_buckets,  dev_buckets_available(ca));
879         sysfs_print(free_buckets,       dev_buckets_free(ca));
880         sysfs_print(has_data,           ca->mi.has_data);
881         sysfs_print(has_metadata,       ca->mi.has_metadata);
882
883         sysfs_pd_controller_show(copy_gc, &ca->moving_gc_pd);
884
885         if (attr == &sysfs_cache_replacement_policy)
886                 return bch2_snprint_string_list(buf, PAGE_SIZE,
887                                                 bch2_cache_replacement_policies,
888                                                 ca->mi.replacement);
889
890         sysfs_print(tier,               ca->mi.tier);
891
892         if (attr == &sysfs_state_rw)
893                 return bch2_snprint_string_list(buf, PAGE_SIZE,
894                                                 bch2_dev_state,
895                                                 ca->mi.state);
896
897         if (attr == &sysfs_read_priority_stats)
898                 return show_quantiles(ca, buf, bucket_priority_fn, (void *) 0);
899         if (attr == &sysfs_write_priority_stats)
900                 return show_quantiles(ca, buf, bucket_priority_fn, (void *) 1);
901         if (attr == &sysfs_fragmentation_stats)
902                 return show_quantiles(ca, buf, bucket_sectors_used_fn, NULL);
903         if (attr == &sysfs_oldest_gen_stats)
904                 return show_quantiles(ca, buf, bucket_oldest_gen_fn, NULL);
905         if (attr == &sysfs_reserve_stats)
906                 return show_reserve_stats(ca, buf);
907         if (attr == &sysfs_alloc_debug)
908                 return show_dev_alloc_debug(ca, buf);
909
910         return 0;
911 }
912
913 STORE(bch2_dev)
914 {
915         struct bch_dev *ca = container_of(kobj, struct bch_dev, kobj);
916         struct bch_fs *c = ca->fs;
917         struct bch_member *mi;
918
919         sysfs_pd_controller_store(copy_gc, &ca->moving_gc_pd);
920
921         if (attr == &sysfs_discard) {
922                 bool v = strtoul_or_return(buf);
923
924                 mutex_lock(&c->sb_lock);
925                 mi = &bch2_sb_get_members(c->disk_sb)->members[ca->dev_idx];
926
927                 if (v != BCH_MEMBER_DISCARD(mi)) {
928                         SET_BCH_MEMBER_DISCARD(mi, v);
929                         bch2_write_super(c);
930                 }
931                 mutex_unlock(&c->sb_lock);
932         }
933
934         if (attr == &sysfs_cache_replacement_policy) {
935                 ssize_t v = bch2_read_string_list(buf, bch2_cache_replacement_policies);
936
937                 if (v < 0)
938                         return v;
939
940                 mutex_lock(&c->sb_lock);
941                 mi = &bch2_sb_get_members(c->disk_sb)->members[ca->dev_idx];
942
943                 if ((unsigned) v != BCH_MEMBER_REPLACEMENT(mi)) {
944                         SET_BCH_MEMBER_REPLACEMENT(mi, v);
945                         bch2_write_super(c);
946                 }
947                 mutex_unlock(&c->sb_lock);
948         }
949
950         if (attr == &sysfs_tier) {
951                 unsigned prev_tier;
952                 unsigned v = strtoul_restrict_or_return(buf,
953                                         0, BCH_TIER_MAX - 1);
954
955                 mutex_lock(&c->sb_lock);
956                 prev_tier = ca->mi.tier;
957
958                 if (v == ca->mi.tier) {
959                         mutex_unlock(&c->sb_lock);
960                         return size;
961                 }
962
963                 mi = &bch2_sb_get_members(c->disk_sb)->members[ca->dev_idx];
964                 SET_BCH_MEMBER_TIER(mi, v);
965                 bch2_write_super(c);
966
967                 bch2_dev_group_remove(&c->tiers[prev_tier].devs, ca);
968                 bch2_dev_group_add(&c->tiers[ca->mi.tier].devs, ca);
969                 mutex_unlock(&c->sb_lock);
970
971                 bch2_recalc_capacity(c);
972                 bch2_tiering_start(c);
973         }
974
975         return size;
976 }
977 SYSFS_OPS(bch2_dev);
978
979 struct attribute *bch2_dev_files[] = {
980         &sysfs_uuid,
981         &sysfs_bucket_size,
982         &sysfs_bucket_size_bytes,
983         &sysfs_block_size,
984         &sysfs_block_size_bytes,
985         &sysfs_first_bucket,
986         &sysfs_nbuckets,
987         &sysfs_read_priority_stats,
988         &sysfs_write_priority_stats,
989         &sysfs_fragmentation_stats,
990         &sysfs_oldest_gen_stats,
991         &sysfs_reserve_stats,
992         &sysfs_available_buckets,
993         &sysfs_free_buckets,
994         &sysfs_dirty_data,
995         &sysfs_dirty_bytes,
996         &sysfs_dirty_buckets,
997         &sysfs_cached_data,
998         &sysfs_cached_bytes,
999         &sysfs_cached_buckets,
1000         &sysfs_meta_buckets,
1001         &sysfs_alloc_buckets,
1002         &sysfs_has_data,
1003         &sysfs_has_metadata,
1004         &sysfs_discard,
1005         &sysfs_written,
1006         &sysfs_btree_written,
1007         &sysfs_metadata_written,
1008         &sysfs_cache_replacement_policy,
1009         &sysfs_tier,
1010         &sysfs_state_rw,
1011         &sysfs_alloc_debug,
1012
1013         sysfs_pd_controller_files(copy_gc),
1014         NULL
1015 };