]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - cmd_device.c
Fix build against musl libc.
[bcachefs-tools-debian] / cmd_device.c
index f9e975abc2b93916e0ea820aac1d3845e241a416..e3c5d513bf3026111ca5c49d68a1744041326172 100644 (file)
 #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 +49,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 +62,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 +78,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 +102,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 +111,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 +119,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 +147,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 +214,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 +251,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 +295,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[])
@@ -331,9 +347,10 @@ static void device_set_state_usage(void)
             "\n"
             "Options:\n"
             "  -f, --force                 Force, if data redundancy will be degraded\n"
+            "      --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);
 }
 
@@ -341,6 +358,7 @@ int cmd_device_set_state(int argc, char *argv[])
 {
        static const struct option longopts[] = {
                { "force",                      0, NULL, 'f' },
+               { "force-if-data-lost",         0, NULL, 'F' },
                { "offline",                    0, NULL, 'o' },
                { "help",                       0, NULL, 'h' },
                { NULL }
@@ -355,6 +373,10 @@ int cmd_device_set_state(int argc, char *argv[])
                case 'f':
                        flags |= BCH_FORCE_IF_DEGRADED;
                        break;
+               case 'F':
+                       flags |= BCH_FORCE_IF_DEGRADED;
+                       flags |= BCH_FORCE_IF_LOST;
+                       break;
                case 'o':
                        offline = true;
                        break;
@@ -396,9 +418,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();
@@ -429,7 +454,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);
 }
 
@@ -492,6 +517,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 {
@@ -513,6 +541,9 @@ 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)
@@ -527,11 +558,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);
 }
 
@@ -559,7 +590,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");