]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/sysfs.c
Update bcachefs sources to 96b991466a bcachefs: Improve error message in fsck
[bcachefs-tools-debian] / libbcachefs / sysfs.c
index 883165196833ec698ae630aa6362c7bfba5b0308..d78ffcc0e8a457cfef0790908938bda9bce765b0 100644 (file)
@@ -1,3 +1,4 @@
+// SPDX-License-Identifier: GPL-2.0
 /*
  * bcache sysfs interfaces
  *
@@ -17,6 +18,7 @@
 #include "btree_update_interior.h"
 #include "btree_gc.h"
 #include "buckets.h"
+#include "clock.h"
 #include "disk_groups.h"
 #include "ec.h"
 #include "inode.h"
@@ -72,9 +74,10 @@ do {                                                                 \
 #define sysfs_hprint(file, val)                                                \
 do {                                                                   \
        if (attr == &sysfs_ ## file) {                                  \
-               ssize_t ret = bch2_hprint(buf, val);                    \
-               strcat(buf, "\n");                                      \
-               return ret + 1;                                         \
+               struct printbuf out = _PBUF(buf, PAGE_SIZE);            \
+               bch2_hprint(&out, val);                                 \
+               pr_buf(&out, "\n");                                     \
+               return out.pos - buf;                                   \
        }                                                               \
 } while (0)
 
@@ -131,6 +134,7 @@ do {                                                                        \
 write_attribute(trigger_journal_flush);
 write_attribute(trigger_btree_coalesce);
 write_attribute(trigger_gc);
+write_attribute(trigger_alloc_write);
 write_attribute(prune_cache);
 rw_attribute(btree_gc_periodic);
 
@@ -195,6 +199,9 @@ rw_attribute(pd_controllers_update_seconds);
 read_attribute(meta_replicas_have);
 read_attribute(data_replicas_have);
 
+read_attribute(io_timers_read);
+read_attribute(io_timers_write);
+
 #ifdef CONFIG_BCACHEFS_TESTS
 write_attribute(perf_test);
 #endif /* CONFIG_BCACHEFS_TESTS */
@@ -232,57 +239,44 @@ static size_t bch2_btree_cache_size(struct bch_fs *c)
 static ssize_t show_fs_alloc_debug(struct bch_fs *c, char *buf)
 {
        struct printbuf out = _PBUF(buf, PAGE_SIZE);
-       struct bch_fs_usage stats = bch2_fs_usage_read(c);
-       unsigned replicas, type;
-
-       pr_buf(&out, "capacity:\t\t%llu\n", c->capacity);
-
-       for (replicas = 0; replicas < ARRAY_SIZE(stats.replicas); replicas++) {
-               pr_buf(&out, "%u replicas:\n", replicas + 1);
-
-               for (type = BCH_DATA_SB; type < BCH_DATA_NR; type++)
-                       pr_buf(&out, "\t%s:\t\t%llu\n",
-                              bch2_data_types[type],
-                              stats.replicas[replicas].data[type]);
-               pr_buf(&out, "\terasure coded:\t%llu\n",
-                      stats.replicas[replicas].ec_data);
-               pr_buf(&out, "\treserved:\t%llu\n",
-                      stats.replicas[replicas].persistent_reserved);
-       }
+       struct bch_fs_usage *fs_usage = bch2_fs_usage_read(c);
 
-       pr_buf(&out, "bucket usage\n");
+       if (!fs_usage)
+               return -ENOMEM;
 
-       for (type = BCH_DATA_SB; type < BCH_DATA_NR; type++)
-               pr_buf(&out, "\t%s:\t\t%llu\n",
-                      bch2_data_types[type],
-                      stats.buckets[type]);
+       bch2_fs_usage_to_text(&out, c, fs_usage);
 
-       pr_buf(&out, "online reserved:\t%llu\n",
-              stats.s.online_reserved);
+       percpu_up_read(&c->mark_lock);
+
+       kfree(fs_usage);
 
        return out.pos - buf;
 }
 
 static ssize_t bch2_compression_stats(struct bch_fs *c, char *buf)
 {
-       struct btree_iter iter;
+       struct btree_trans trans;
+       struct btree_iter *iter;
        struct bkey_s_c k;
        u64 nr_uncompressed_extents = 0, uncompressed_sectors = 0,
            nr_compressed_extents = 0,
            compressed_sectors_compressed = 0,
            compressed_sectors_uncompressed = 0;
+       int ret;
 
-       if (!bch2_fs_running(c))
+       if (!test_bit(BCH_FS_STARTED, &c->flags))
                return -EPERM;
 
-       for_each_btree_key(&iter, c, BTREE_ID_EXTENTS, POS_MIN, 0, k)
+       bch2_trans_init(&trans, c, 0, 0);
+
+       for_each_btree_key(&trans, iter, BTREE_ID_EXTENTS, POS_MIN, 0, k, ret)
                if (k.k->type == KEY_TYPE_extent) {
                        struct bkey_s_c_extent e = bkey_s_c_to_extent(k);
                        const union bch_extent_entry *entry;
                        struct extent_ptr_decoded p;
 
                        extent_for_each_ptr_decode(e, p, entry) {
-                               if (p.crc.compression_type == BCH_COMPRESSION_NONE) {
+                               if (!crc_is_compressed(p.crc)) {
                                        nr_uncompressed_extents++;
                                        uncompressed_sectors += e.k->size;
                                } else {
@@ -297,7 +291,10 @@ static ssize_t bch2_compression_stats(struct bch_fs *c, char *buf)
                                break;
                        }
                }
-       bch2_btree_iter_unlock(&iter);
+
+       ret = bch2_trans_exit(&trans) ?: ret;
+       if (ret)
+               return ret;
 
        return scnprintf(buf, PAGE_SIZE,
                        "uncompressed data:\n"
@@ -411,6 +408,11 @@ SHOW(bch2_fs)
        if (attr == &sysfs_new_stripes)
                return bch2_new_stripes(c, buf);
 
+       if (attr == &sysfs_io_timers_read)
+               return bch2_io_timers_show(&c->io_clock[READ], buf);
+       if (attr == &sysfs_io_timers_write)
+               return bch2_io_timers_show(&c->io_clock[WRITE], buf);
+
 #define BCH_DEBUG_PARAM(name, description) sysfs_print(name, c->name);
        BCH_DEBUG_PARAMS()
 #undef BCH_DEBUG_PARAM
@@ -465,7 +467,7 @@ STORE(__bch2_fs)
        BCH_DEBUG_PARAMS()
 #undef BCH_DEBUG_PARAM
 
-       if (!bch2_fs_running(c))
+       if (!test_bit(BCH_FS_STARTED, &c->flags))
                return -EPERM;
 
        /* Debugging: */
@@ -477,7 +479,13 @@ STORE(__bch2_fs)
                bch2_coalesce(c);
 
        if (attr == &sysfs_trigger_gc)
-               bch2_gc(c, NULL, false);
+               bch2_gc(c, NULL, false, false);
+
+       if (attr == &sysfs_trigger_alloc_write) {
+               bool wrote;
+
+               bch2_alloc_write(c, 0, &wrote);
+       }
 
        if (attr == &sysfs_prune_cache) {
                struct shrink_control sc;
@@ -571,6 +579,7 @@ struct attribute *bch2_fs_internal_files[] = {
        &sysfs_trigger_journal_flush,
        &sysfs_trigger_btree_coalesce,
        &sysfs_trigger_gc,
+       &sysfs_trigger_alloc_write,
        &sysfs_prune_cache,
 
        &sysfs_copy_gc_enabled,
@@ -581,6 +590,9 @@ struct attribute *bch2_fs_internal_files[] = {
 
        &sysfs_new_stripes,
 
+       &sysfs_io_timers_read,
+       &sysfs_io_timers_write,
+
        &sysfs_internal_uuid,
 
 #define BCH_DEBUG_PARAM(name, description) &sysfs_##name,
@@ -657,7 +669,7 @@ int bch2_opts_create_sysfs_files(struct kobject *kobj)
        for (i = bch2_opt_table;
             i < bch2_opt_table + bch2_opts_nr;
             i++) {
-               if (i->mode == OPT_INTERNAL)
+               if (!(i->mode & (OPT_FORMAT|OPT_MOUNT|OPT_RUNTIME)))
                        continue;
 
                ret = sysfs_create_file(kobj, &i->attr);
@@ -724,10 +736,10 @@ static unsigned bucket_oldest_gen_fn(struct bch_fs *c, struct bch_dev *ca,
 
 static int unsigned_cmp(const void *_l, const void *_r)
 {
-       unsigned l = *((unsigned *) _l);
-       unsigned r = *((unsigned *) _r);
+       const unsigned *l = _l;
+       const unsigned *r = _r;
 
-       return (l > r) - (l < r);
+       return cmp_int(*l, *r);
 }
 
 static ssize_t show_quantiles(struct bch_fs *c, struct bch_dev *ca,
@@ -775,7 +787,7 @@ static ssize_t show_reserve_stats(struct bch_dev *ca, char *buf)
        struct printbuf out = _PBUF(buf, PAGE_SIZE);
        enum alloc_reserve i;
 
-       spin_lock(&ca->freelist_lock);
+       spin_lock(&ca->fs->freelist_lock);
 
        pr_buf(&out, "free_inc:\t%zu\t%zu\n",
               fifo_used(&ca->free_inc),
@@ -786,7 +798,7 @@ static ssize_t show_reserve_stats(struct bch_dev *ca, char *buf)
                       fifo_used(&ca->free[i]),
                       ca->free[i].size);
 
-       spin_unlock(&ca->freelist_lock);
+       spin_unlock(&ca->fs->freelist_lock);
 
        return out.pos - buf;
 }
@@ -795,6 +807,12 @@ static ssize_t show_dev_alloc_debug(struct bch_dev *ca, char *buf)
 {
        struct bch_fs *c = ca->fs;
        struct bch_dev_usage stats = bch2_dev_usage_read(c, ca);
+       unsigned i, nr[BCH_DATA_NR];
+
+       memset(nr, 0, sizeof(nr));
+
+       for (i = 0; i < ARRAY_SIZE(c->open_buckets); i++)
+               nr[c->open_buckets[i].type]++;
 
        return scnprintf(buf, PAGE_SIZE,
                "free_inc:               %zu/%zu\n"
@@ -821,7 +839,10 @@ static ssize_t show_dev_alloc_debug(struct bch_dev *ca, char *buf)
                "    copygc threshold:   %llu\n"
                "freelist_wait:          %s\n"
                "open buckets:           %u/%u (reserved %u)\n"
-               "open_buckets_wait:      %s\n",
+               "open_buckets_wait:      %s\n"
+               "open_buckets_btree:     %u\n"
+               "open_buckets_user:      %u\n"
+               "btree reserve cache:    %u\n",
                fifo_used(&ca->free_inc),               ca->free_inc.size,
                fifo_used(&ca->free[RESERVE_BTREE]),    ca->free[RESERVE_BTREE].size,
                fifo_used(&ca->free[RESERVE_MOVINGGC]), ca->free[RESERVE_MOVINGGC].size,
@@ -843,8 +864,12 @@ static ssize_t show_dev_alloc_debug(struct bch_dev *ca, char *buf)
                stats.sectors_fragmented,
                ca->copygc_threshold,
                c->freelist_wait.list.first             ? "waiting" : "empty",
-               c->open_buckets_nr_free, OPEN_BUCKETS_COUNT, BTREE_NODE_RESERVE,
-               c->open_buckets_wait.list.first         ? "waiting" : "empty");
+               c->open_buckets_nr_free, OPEN_BUCKETS_COUNT,
+               BTREE_NODE_OPEN_BUCKET_RESERVE,
+               c->open_buckets_wait.list.first         ? "waiting" : "empty",
+               nr[BCH_DATA_BTREE],
+               nr[BCH_DATA_USER],
+               c->btree_reserve_cache_nr);
 }
 
 static const char * const bch2_rw[] = {
@@ -856,20 +881,15 @@ static const char * const bch2_rw[] = {
 static ssize_t show_dev_iodone(struct bch_dev *ca, char *buf)
 {
        struct printbuf out = _PBUF(buf, PAGE_SIZE);
-       int rw, i, cpu;
+       int rw, i;
 
        for (rw = 0; rw < 2; rw++) {
                pr_buf(&out, "%s:\n", bch2_rw[rw]);
 
-               for (i = 1; i < BCH_DATA_NR; i++) {
-                       u64 n = 0;
-
-                       for_each_possible_cpu(cpu)
-                               n += per_cpu_ptr(ca->io_done, cpu)->sectors[rw][i];
-
+               for (i = 1; i < BCH_DATA_NR; i++)
                        pr_buf(&out, "%-12s:%12llu\n",
-                              bch2_data_types[i], n << 9);
-               }
+                              bch2_data_types[i],
+                              percpu_u64_get(&ca->io_done->sectors[rw][i]) << 9);
        }
 
        return out.pos - buf;
@@ -896,8 +916,6 @@ SHOW(bch2_dev)
                        bch2_disk_path_to_text(&out, &c->disk_sb,
                                               ca->mi.group - 1);
                        mutex_unlock(&c->sb_lock);
-               } else {
-                       pr_buf(&out, "none");
                }
 
                pr_buf(&out, "\n");