]> git.sesse.net Git - bcachefs-tools-debian/commitdiff
Add a subcommand for resizing the journal
authorKent Overstreet <kent.overstreet@gmail.com>
Tue, 17 Nov 2020 15:52:29 +0000 (10:52 -0500)
committerKent Overstreet <kent.overstreet@gmail.com>
Tue, 17 Nov 2020 15:52:39 +0000 (10:52 -0500)
Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
bcachefs.c
cmd_device.c
cmds.h
libbcachefs.h

index b4958f13c8a2e20436c08974b08395a9a208b959..3a84c38ac699dd9085571ce3031f9050e9e6c53a 100644 (file)
@@ -128,6 +128,8 @@ static int device_cmds(int argc, char *argv[])
                return cmd_device_set_state(argc, argv);
        if (!strcmp(cmd, "resize"))
                return cmd_device_resize(argc, argv);
+       if (!strcmp(cmd, "resize-journal"))
+               return cmd_device_resize_journal(argc, argv);
 
        usage();
        return 0;
index c311324a75dc17c2749bfadaf4c4e51a86cc45b9..a7ff36154ce6794163af309559cadc4c27ee5ba2 100644 (file)
@@ -12,7 +12,9 @@
 #include <sys/types.h>
 #include <unistd.h>
 
+#include "libbcachefs/bcachefs.h"
 #include "libbcachefs/bcachefs_ioctl.h"
+#include "libbcachefs/journal.h"
 #include "libbcachefs/super-io.h"
 #include "cmds.h"
 #include "libbcachefs.h"
@@ -496,3 +498,103 @@ int cmd_device_resize(int argc, char *argv[])
        }
        return 0;
 }
+
+static void device_resize_journal_usage(void)
+{
+       puts("bcachefs device resize-journal \n"
+            "Usage: bcachefs device resize-journal device [ size ]\n"
+            "\n"
+            "Options:\n"
+            "  -h, --help                  display this help and exit\n"
+            "Report bugs to <linux-bcache@vger.kernel.org>");
+       exit(EXIT_SUCCESS);
+}
+
+int cmd_device_resize_journal(int argc, char *argv[])
+{
+       static const struct option longopts[] = {
+               { "help",                       0, NULL, 'h' },
+               { NULL }
+       };
+       u64 size;
+       int opt;
+
+       while ((opt = getopt_long(argc, argv, "h", longopts, NULL)) != -1)
+               switch (opt) {
+               case 'h':
+                       device_resize_journal_usage();
+               }
+       args_shift(optind);
+
+       char *dev = arg_pop();
+       if (!dev)
+               die("Please supply a device");
+
+       int dev_fd = xopen(dev, O_RDONLY);
+
+       char *size_arg = arg_pop();
+       if (!size_arg)
+               size = get_size(dev, dev_fd);
+       else if (bch2_strtoull_h(size_arg, &size))
+               die("invalid size");
+
+       size >>= 9;
+
+       if (argc)
+               die("Too many arguments");
+
+       struct stat dev_stat = xfstat(dev_fd);
+
+       struct mntent *mount = dev_to_mount(dev);
+       if (mount) {
+               if (!S_ISBLK(dev_stat.st_mode))
+                       die("%s is mounted but isn't a block device?!", dev);
+
+               struct bchfs_handle fs = bcache_fs_open(mount->mnt_dir);
+
+               unsigned idx = bchu_disk_get_idx(fs, dev_stat.st_rdev);
+
+               struct bch_sb *sb = bchu_read_super(fs, -1);
+               if (idx >= sb->nr_devices)
+                       die("error reading superblock: dev idx >= sb->nr_devices");
+
+               struct bch_sb_field_members *mi = bch2_sb_get_members(sb);
+               if (!mi)
+                       die("error reading superblock: no member info");
+
+               /* could also just read this out of sysfs... meh */
+               struct bch_member *m = mi->members + idx;
+
+               u64 nbuckets = size / le16_to_cpu(m->bucket_size);
+
+               printf("resizing journal on %s to %llu buckets\n", dev, nbuckets);
+               bchu_disk_resize_journal(fs, idx, nbuckets);
+       } else {
+               printf("%s is offline - starting:\n", dev);
+
+               struct bch_fs *c = bch2_fs_open(&dev, 1, bch2_opts_empty());
+               if (IS_ERR(c))
+                       die("error opening %s: %s", dev, strerror(-PTR_ERR(c)));
+
+               struct bch_dev *ca, *resize = NULL;
+               unsigned i;
+
+               for_each_online_member(ca, c, i) {
+                       if (resize)
+                               die("confused: more than one online device?");
+                       resize = ca;
+                       percpu_ref_get(&resize->io_ref);
+               }
+
+               u64 nbuckets = size / le16_to_cpu(resize->mi.bucket_size);
+
+               printf("resizing journal on %s to %llu buckets\n", dev, nbuckets);
+               int ret = bch2_set_nr_journal_buckets(c, resize, nbuckets);
+               if (ret)
+                       fprintf(stderr, "resize error: %s\n", strerror(-ret));
+
+               percpu_ref_put(&resize->io_ref);
+               bch2_fs_stop(c);
+       }
+       return 0;
+}
diff --git a/cmds.h b/cmds.h
index bcd27adc5350a5474f21d9a4d77cfa1f52e1fe54..a05c98b9b1ab38acbda86599cd0dbdfc459c17de 100644 (file)
--- a/cmds.h
+++ b/cmds.h
@@ -28,6 +28,7 @@ int cmd_device_offline(int argc, char *argv[]);
 int cmd_device_evacuate(int argc, char *argv[]);
 int cmd_device_set_state(int argc, char *argv[]);
 int cmd_device_resize(int argc, char *argv[]);
+int cmd_device_resize_journal(int argc, char *argv[]);
 
 int cmd_data_rereplicate(int argc, char *argv[]);
 
index 30add92c518c4ceae328048b5318b96220f0278a..6ae24e9f865f89139fa917db9fc2bc1a55cb3f66 100644 (file)
@@ -214,6 +214,19 @@ static inline void bchu_disk_resize(struct bchfs_handle fs,
        xioctl(fs.ioctl_fd, BCH_IOCTL_DISK_RESIZE, &i);
 }
 
+static inline void bchu_disk_resize_journal(struct bchfs_handle fs,
+                                           unsigned idx,
+                                           u64 nbuckets)
+{
+       struct bch_ioctl_disk_resize i = {
+               .flags  = BCH_BY_INDEX,
+               .dev    = idx,
+               .nbuckets = nbuckets,
+       };
+
+       xioctl(fs.ioctl_fd, BCH_IOCTL_DISK_RESIZE_JOURNAL, &i);
+}
+
 int bchu_data(struct bchfs_handle, struct bch_ioctl_data);
 
 struct dev_name {