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