Add btrfs-vol command to balance, add and (eventually) remove devices
authorChris Mason <chris.mason@oracle.com>
Mon, 28 Apr 2008 20:44:22 +0000 (16:44 -0400)
committerDavid Woodhouse <dwmw2@hera.kernel.org>
Mon, 28 Apr 2008 20:44:22 +0000 (16:44 -0400)
Makefile
btrfs-vol.c [new file with mode: 0644]
btrfsctl.c
ioctl.h
mkfs.c

index 58dfa5f..7100852 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -15,7 +15,7 @@ prefix ?= /usr/local
 bindir = $(prefix)/bin
 LIBS=-luuid
 
-progs = btrfsctl btrfsck mkfs.btrfs debug-tree btrfs-show
+progs = btrfsctl btrfsck mkfs.btrfs debug-tree btrfs-show btrfs-vol
 
 # make C=1 to enable sparse
 ifdef C
@@ -34,6 +34,9 @@ all: $(progs)
 btrfsctl: $(objects) btrfsctl.o
        gcc $(CFLAGS) -o btrfsctl btrfsctl.o $(objects) $(LDFLAGS) $(LIBS)
 
+btrfs-vol: $(objects) btrfs-vol.o
+       gcc $(CFLAGS) -o btrfs-vol btrfs-vol.o $(objects) $(LDFLAGS) $(LIBS)
+
 btrfs-show: $(objects) btrfs-show.o
        gcc $(CFLAGS) -o btrfs-show btrfs-show.o $(objects) $(LDFLAGS) $(LIBS)
 
diff --git a/btrfs-vol.c b/btrfs-vol.c
new file mode 100644 (file)
index 0000000..0cc2e97
--- /dev/null
@@ -0,0 +1,145 @@
+/*
+ * Copyright (C) 2007 Oracle.  All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License v2 as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this program; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 021110-1307, USA.
+ */
+
+#define _GNU_SOURCE
+#ifndef __CHECKER__
+#include <sys/ioctl.h>
+#include <sys/mount.h>
+#include "ioctl.h"
+#endif
+#include <stdio.h>
+#include <stdlib.h>
+#include <getopt.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <dirent.h>
+#include <uuid/uuid.h>
+#include "kerncompat.h"
+#include "ctree.h"
+#include "transaction.h"
+#include "utils.h"
+#include "volumes.h"
+
+#ifdef __CHECKER__
+#define BLKGETSIZE64 0
+#define BTRFS_IOC_SNAP_CREATE 0
+#define BTRFS_IOC_ADD_DEV 0
+#define BTRFS_IOC_RM_DEV 0
+#define BTRFS_VOL_NAME_MAX 255
+struct btrfs_ioctl_vol_args { char name[BTRFS_VOL_NAME_MAX]; };
+static inline int ioctl(int fd, int define, void *arg) { return 0; }
+#endif
+
+static void print_usage(void)
+{
+       fprintf(stderr, "usage: btrfs-vol [options] mount_point\n");
+       fprintf(stderr, "\t-a device add one device\n");
+       fprintf(stderr, "\t-b balance chunks across all devices\n");
+       fprintf(stderr, "\t-r device remove one device\n");
+       exit(1);
+}
+
+static struct option long_options[] = {
+       /* { "byte-count", 1, NULL, 'b' }, */
+       { "add", 1, NULL, 'a' },
+       { "balance", 0, NULL, 'b' },
+       { "remove", 1, NULL, 'r' },
+       { 0, 0, 0, 0}
+};
+
+int main(int ac, char **av)
+{
+       struct stat st;
+       char *device = NULL;
+       char *mnt = NULL;
+       int ret;
+       int option_index = 0;
+       int cmd;
+       int fd;
+       int devfd;
+       DIR *dirstream;
+       struct btrfs_ioctl_vol_args args;
+       u64 dev_block_count = 0;
+
+       while(1) {
+               int c;
+               c = getopt_long(ac, av, "a:br:", long_options,
+                               &option_index);
+               if (c < 0)
+                       break;
+               switch(c) {
+                       case 'a':
+                               device = strdup(optarg);
+                               cmd = BTRFS_IOC_ADD_DEV;
+                               break;
+                       case 'b':
+                               cmd = BTRFS_IOC_BALANCE;
+                               break;
+                       case 'r':
+                               device = strdup(optarg);
+                               cmd = BTRFS_IOC_RM_DEV;
+                               break;
+                       default:
+                               print_usage();
+               }
+       }
+       ac = ac - optind;
+       if (ac == 0)
+               print_usage();
+       mnt = av[optind];
+
+       if (device) {
+               devfd = open(device, O_RDWR);
+               if (!devfd) {
+                       fprintf(stderr, "Unable to open device %s\n", device);
+               }
+               ret = fstat(devfd, &st);
+               if (ret) {
+                       fprintf(stderr, "Unable to stat %s\n", device);
+                       exit(1);
+               }
+               if (!S_ISBLK(st.st_mode)) {
+                       fprintf(stderr, "%s is not a block device\n", device);
+                       exit(1);
+               }
+       }
+       dirstream = opendir(mnt);
+       if (!dirstream) {
+               fprintf(stderr, "Unable to open directory %s\n", mnt);
+               exit(1);
+       }
+       if (cmd == BTRFS_IOC_ADD_DEV) {
+               ret = btrfs_prepare_device(devfd, device, 1, &dev_block_count);
+               if (ret) {
+                       fprintf(stderr, "Unable to init %s\n", device);
+                       exit(1);
+               }
+       }
+       fd = dirfd(dirstream);
+       if (device)
+               strcpy(args.name, device);
+       else
+               args.name[0] = '\0';
+
+       ret = ioctl(fd, cmd, &args);
+       printf("ioctl returns %d\n", ret);
+       return 0;
+}
+
index 8e8cefa..53b43b6 100644 (file)
@@ -36,7 +36,6 @@
 #ifdef __CHECKER__
 #define BLKGETSIZE64 0
 #define BTRFS_IOC_SNAP_CREATE 0
-#define BTRFS_IOC_ADD_DISK 0
 #define BTRFS_VOL_NAME_MAX 255
 struct btrfs_ioctl_vol_args { char name[BTRFS_VOL_NAME_MAX]; };
 static inline int ioctl(int fd, int define, void *arg) { return 0; }
diff --git a/ioctl.h b/ioctl.h
index 4551e82..8ad35fc 100644 (file)
--- a/ioctl.h
+++ b/ioctl.h
@@ -36,4 +36,10 @@ struct btrfs_ioctl_vol_args {
                                   struct btrfs_ioctl_vol_args)
 #define BTRFS_IOC_SCAN_DEV _IOW(BTRFS_IOCTL_MAGIC, 4, \
                                   struct btrfs_ioctl_vol_args)
+#define BTRFS_IOC_ADD_DEV _IOW(BTRFS_IOCTL_MAGIC, 10, \
+                                  struct btrfs_ioctl_vol_args)
+#define BTRFS_IOC_RM_DEV _IOW(BTRFS_IOCTL_MAGIC, 11, \
+                                  struct btrfs_ioctl_vol_args)
+#define BTRFS_IOC_BALANCE _IOW(BTRFS_IOCTL_MAGIC, 12, \
+                                  struct btrfs_ioctl_vol_args)
 #endif
diff --git a/mkfs.c b/mkfs.c
index ac81ab5..c02627f 100644 (file)
--- a/mkfs.c
+++ b/mkfs.c
 
 #define _XOPEN_SOURCE 500
 #define _GNU_SOURCE
+
+#ifndef __CHECKER__
+#include <sys/ioctl.h>
+#include <sys/mount.h>
+#include "ioctl.h"
+#endif
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <sys/types.h>