]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - cmd_device.c
Update bcachefs sources to 717b356d1d bcachefs: Convert journal validation to bkey_in...
[bcachefs-tools-debian] / cmd_device.c
index b18bdd8c6f2e0a89bffb5e0e74fb0352e2a918f2..1914629bbc234fafafe35e11e4a6cd50b01dcb5d 100644 (file)
 
 #include "libbcachefs/bcachefs.h"
 #include "libbcachefs/bcachefs_ioctl.h"
+#include "libbcachefs/errcode.h"
 #include "libbcachefs/journal.h"
+#include "libbcachefs/sb-members.h"
 #include "libbcachefs/super-io.h"
 #include "cmds.h"
 #include "libbcachefs.h"
 #include "libbcachefs/opts.h"
 #include "tools-util.h"
 
+int device_usage(void)
+{
+       puts("bcachefs device - manage devices within a running filesystem\n"
+            "Usage: bcachefs device <CMD> [OPTION]\n"
+            "\n"
+            "Commands:\n"
+            "  add                     add a new device to an existing filesystem\n"
+            "  remove                  remove a device from an existing filesystem\n"
+            "  online                  re-add an existing member to a filesystem\n"
+            "  offline                 take a device offline, without removing it\n"
+            "  evacuate                migrate data off a specific device\n"
+            "  set-state               mark a device as failed\n"
+            "  resize                  resize filesystem on a device\n"
+            "  resize-journal          resize journal on a device\n"
+            "\n"
+            "Report bugs to <linux-bcachefs@vger.kernel.org>");
+       return 0;
+}
+
 static void device_add_usage(void)
 {
        puts("bcachefs device add - add a device to an existing filesystem\n"
@@ -30,11 +51,11 @@ static void device_add_usage(void)
             "  -S, --fs_size=size          Size of filesystem on device\n"
             "  -B, --bucket=size           Bucket size\n"
             "  -D, --discard               Enable discards\n"
-            "  -g, --group=group           Disk group\n"
+            "  -l, --label=label           Disk label\n"
             "  -f, --force                 Use device even if it appears to already be formatted\n"
             "  -h, --help                  Display this help and exit\n"
             "\n"
-            "Report bugs to <linux-bcache@vger.kernel.org>");
+            "Report bugs to <linux-bcachefs@vger.kernel.org>");
 }
 
 int cmd_device_add(int argc, char *argv[])
@@ -43,7 +64,7 @@ int cmd_device_add(int argc, char *argv[])
                { "fs_size",            required_argument,      NULL, 'S' },
                { "bucket",             required_argument,      NULL, 'B' },
                { "discard",            no_argument,            NULL, 'D' },
-               { "group",              required_argument,      NULL, 'g' },
+               { "label",              required_argument,      NULL, 'l' },
                { "force",              no_argument,            NULL, 'f' },
                { "help",               no_argument,            NULL, 'h' },
                { NULL }
@@ -59,18 +80,16 @@ int cmd_device_add(int argc, char *argv[])
                case 'S':
                        if (bch2_strtoull_h(optarg, &dev_opts.size))
                                die("invalid filesystem size");
-
-                       dev_opts.size >>= 9;
                        break;
                case 'B':
-                       dev_opts.bucket_size =
-                               hatoi_validate(optarg, "bucket size");
+                       if (bch2_strtoull_h(optarg, &dev_opts.bucket_size))
+                               die("bad bucket_size %s", optarg);
                        break;
                case 'D':
                        dev_opts.discard = true;
                        break;
-               case 'g':
-                       dev_opts.group = strdup(optarg);
+               case 'l':
+                       dev_opts.label = strdup(optarg);
                        break;
                case 'f':
                        force = true;
@@ -85,8 +104,8 @@ int cmd_device_add(int argc, char *argv[])
        if (!fs_path)
                die("Please supply a filesystem");
 
-       char *dev_path = arg_pop();
-       if (!dev_path)
+       dev_opts.path = arg_pop();
+       if (!dev_opts.path)
                die("Please supply a device");
 
        if (argc)
@@ -94,7 +113,6 @@ int cmd_device_add(int argc, char *argv[])
 
        struct bchfs_handle fs = bcache_fs_open(fs_path);
 
-       dev_opts.path = dev_path;
        dev_opts.fd = open_for_format(dev_opts.path, force);
 
        struct bch_opt_strs fs_opt_strs;
@@ -103,9 +121,9 @@ int cmd_device_add(int argc, char *argv[])
        struct bch_opts fs_opts = bch2_parse_opts(fs_opt_strs);
 
        opt_set(fs_opts, block_size,
-               read_file_u64(fs.sysfs_fd, "block_size") >> 9);
+               read_file_u64(fs.sysfs_fd, "options/block_size"));
        opt_set(fs_opts, btree_node_size,
-               read_file_u64(fs.sysfs_fd, "btree_node_size") >> 9);
+               read_file_u64(fs.sysfs_fd, "options/btree_node_size"));
 
        struct bch_sb *sb = bch2_format(fs_opt_strs,
                                        fs_opts,
@@ -131,7 +149,7 @@ static void device_remove_usage(void)
             "  -F, --force-metadata        Force removal, even if some metadata\n"
             "                              couldn't be migrated\n"
             "  -h, --help                  display this help and exit\n"
-            "Report bugs to <linux-bcache@vger.kernel.org>");
+            "Report bugs to <linux-bcachefs@vger.kernel.org>");
        exit(EXIT_SUCCESS);
 }
 
@@ -198,7 +216,7 @@ static void device_online_usage(void)
             "Options:\n"
             "  -h, --help                  Display this help and exit\n"
             "\n"
-            "Report bugs to <linux-bcache@vger.kernel.org>");
+            "Report bugs to <linux-bcachefs@vger.kernel.org>");
 }
 
 int cmd_device_online(int argc, char *argv[])
@@ -235,7 +253,7 @@ static void device_offline_usage(void)
             "  -f, --force                 Force, if data redundancy will be degraded\n"
             "  -h, --help                  Display this help and exit\n"
             "\n"
-            "Report bugs to <linux-bcache@vger.kernel.org>");
+            "Report bugs to <linux-bcachefs@vger.kernel.org>");
 }
 
 int cmd_device_offline(int argc, char *argv[])
@@ -279,7 +297,7 @@ static void device_evacuate_usage(void)
             "Options:\n"
             "  -h, --help                  Display this help and exit\n"
             "\n"
-            "Report bugs to <linux-bcache@vger.kernel.org>");
+            "Report bugs to <linux-bcachefs@vger.kernel.org>");
 }
 
 int cmd_device_evacuate(int argc, char *argv[])
@@ -334,7 +352,7 @@ static void device_set_state_usage(void)
             "      --force-if-data-lost    Force, if data will be lost\n"
             "  -o, --offline               Set state of an offline device\n"
             "  -h, --help                  display this help and exit\n"
-            "Report bugs to <linux-bcache@vger.kernel.org>");
+            "Report bugs to <linux-bcachefs@vger.kernel.org>");
        exit(EXIT_SUCCESS);
 }
 
@@ -394,7 +412,7 @@ int cmd_device_set_state(int argc, char *argv[])
 
                int ret = bch2_read_super(dev_str, &opts, &sb);
                if (ret)
-                       die("error opening %s: %s", dev_str, strerror(-ret));
+                       die("error opening %s: %s", dev_str, bch2_err_str(ret));
 
                struct bch_member *m = bch2_sb_get_members(sb.sb)->members + sb.sb->dev_idx;
 
@@ -402,9 +420,12 @@ int cmd_device_set_state(int argc, char *argv[])
 
                le64_add_cpu(&sb.sb->seq, 1);
 
-               bch2_super_write(sb.bdev->bd_fd, sb.sb);
+               bch2_super_write(sb.bdev->bd_buffered_fd, sb.sb);
+               ret = fsync(sb.bdev->bd_buffered_fd);
+               if (ret)
+                       fprintf(stderr, "error writing superblock: fsync error (%m)");
                bch2_free_super(&sb);
-               return 0;
+               return ret;
        }
 
        char *fs_path = arg_pop();
@@ -435,7 +456,7 @@ static void device_resize_usage(void)
             "\n"
             "Options:\n"
             "  -h, --help                  display this help and exit\n"
-            "Report bugs to <linux-bcache@vger.kernel.org>");
+            "Report bugs to <linux-bcachefs@vger.kernel.org>");
        exit(EXIT_SUCCESS);
 }
 
@@ -498,6 +519,9 @@ int cmd_device_resize(int argc, char *argv[])
 
                u64 nbuckets = size / le16_to_cpu(m->bucket_size);
 
+               if (nbuckets < le64_to_cpu(m->nbuckets))
+                       die("Shrinking not supported yet");
+
                printf("resizing %s to %llu buckets\n", dev, nbuckets);
                bchu_disk_resize(fs, idx, nbuckets);
        } else {
@@ -505,7 +529,7 @@ int cmd_device_resize(int argc, char *argv[])
 
                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)));
+                       die("error opening %s: %s", dev, bch2_err_str(PTR_ERR(c)));
 
                struct bch_dev *ca, *resize = NULL;
                unsigned i;
@@ -519,10 +543,13 @@ int cmd_device_resize(int argc, char *argv[])
 
                u64 nbuckets = size / le16_to_cpu(resize->mi.bucket_size);
 
+               if (nbuckets < le64_to_cpu(resize->mi.nbuckets))
+                       die("Shrinking not supported yet");
+
                printf("resizing %s to %llu buckets\n", dev, nbuckets);
                int ret = bch2_dev_resize(c, resize, nbuckets);
                if (ret)
-                       fprintf(stderr, "resize error: %s\n", strerror(-ret));
+                       fprintf(stderr, "resize error: %s\n", bch2_err_str(ret));
 
                percpu_ref_put(&resize->io_ref);
                bch2_fs_stop(c);
@@ -533,11 +560,11 @@ int cmd_device_resize(int argc, char *argv[])
 static void device_resize_journal_usage(void)
 {
        puts("bcachefs device resize-journal \n"
-            "Usage: bcachefs device resize-journal device [ size ]\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>");
+            "Report bugs to <linux-bcachefs@vger.kernel.org>");
        exit(EXIT_SUCCESS);
 }
 
@@ -565,7 +592,7 @@ int cmd_device_resize_journal(int argc, char *argv[])
 
        char *size_arg = arg_pop();
        if (!size_arg)
-               size = get_size(dev, dev_fd);
+               die("Please supply a journal size");
        else if (bch2_strtoull_h(size_arg, &size))
                die("invalid size");
 
@@ -605,7 +632,7 @@ int cmd_device_resize_journal(int argc, char *argv[])
 
                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)));
+                       die("error opening %s: %s", dev, bch2_err_str(PTR_ERR(c)));
 
                struct bch_dev *ca, *resize = NULL;
                unsigned i;
@@ -622,7 +649,7 @@ int cmd_device_resize_journal(int argc, char *argv[])
                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));
+                       fprintf(stderr, "resize error: %s\n", bch2_err_str(ret));
 
                percpu_ref_put(&resize->io_ref);
                bch2_fs_stop(c);