]> git.sesse.net Git - bcachefs-tools-debian/commitdiff
cmd_reset_counters
authorKent Overstreet <kent.overstreet@linux.dev>
Fri, 5 Jan 2024 00:45:54 +0000 (19:45 -0500)
committerKent Overstreet <kent.overstreet@linux.dev>
Fri, 5 Jan 2024 00:56:14 +0000 (19:56 -0500)
Add a subcommand for resetting superblock counters - for automated tests

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
bcachefs.c
cmd_counters.c [new file with mode: 0644]
cmds.h

index 4efe29edaad25c0b3fb96865e0a5f4907b3f1d55..e132d916fa73967f2e289b0a42e725f1e7a4e822 100644 (file)
@@ -34,6 +34,7 @@ static void usage(void)
             "  format                   Format a new filesystem\n"
             "  show-super               Dump superblock information to stdout\n"
             "  set-option               Set a filesystem option\n"
+            "  reset-counters           Reset all counters on an unmounted device\n"
             "\n"
 #ifndef BCACHEFS_NO_RUST
             "Mount:\n"
@@ -236,6 +237,8 @@ int main(int argc, char *argv[])
                return cmd_show_super(argc, argv);
        if (!strcmp(cmd, "set-option"))
                return cmd_set_option(argc, argv);
+       if (!strcmp(cmd, "reset-counters"))
+               return cmd_reset_counters(argc, argv);
 
 #if 0
        if (!strcmp(cmd, "assemble"))
diff --git a/cmd_counters.c b/cmd_counters.c
new file mode 100644 (file)
index 0000000..7605e2d
--- /dev/null
@@ -0,0 +1,51 @@
+#include <getopt.h>
+
+#include "cmds.h"
+#include "libbcachefs.h"
+#include "libbcachefs/super-io.h"
+
+static void reset_counters_usage(void)
+{
+       puts("bcachefs reset-counters \n"
+            "Usage: bcachefs reset-counters device\n"
+            "\n"
+            "Options:\n"
+            "  -h, --help                  display this help and exit\n"
+            "Report bugs to <linux-bcachefs@vger.kernel.org>");
+       exit(EXIT_SUCCESS);
+}
+
+int cmd_reset_counters(int argc, char *argv[])
+{
+       static const struct option longopts[] = {
+               { "help",                       0, NULL, 'h' },
+               { NULL }
+       };
+       int opt;
+
+       while ((opt = getopt_long(argc, argv, "h", longopts, NULL)) != -1)
+               switch (opt) {
+               case 'h':
+                       reset_counters_usage();
+                       break;
+               }
+       args_shift(optind);
+
+       char *dev = arg_pop();
+       if (!dev)
+               die("please supply a device");
+       if (argc)
+               die("too many arguments");
+
+       struct bch_opts opts = bch2_opts_empty();
+       struct bch_sb_handle sb;
+       int ret = bch2_read_super(dev, &opts, &sb);
+       if (ret)
+               die("Error opening %s: %s", dev, bch2_err_str(ret));
+
+       bch2_sb_field_resize(&sb, counters, 0);
+
+       bch2_super_write(sb.bdev->bd_buffered_fd, sb.sb);
+       bch2_free_super(&sb);
+       return 0;
+}
diff --git a/cmds.h b/cmds.h
index 5b3f5f55d8b9717085d4b1078e48ab40d9a8b8bb..76a7613500db95087a342a926ced55e201a2a8c3 100644 (file)
--- a/cmds.h
+++ b/cmds.h
@@ -11,6 +11,7 @@
 
 int cmd_format(int argc, char *argv[]);
 int cmd_show_super(int argc, char *argv[]);
+int cmd_reset_counters(int argc, char *argv[]);
 int cmd_set_option(int argc, char *argv[]);
 
 int cmd_fs_usage(int argc, char *argv[]);