btrfs-progs pretty/quiet build
[platform/upstream/btrfs-progs.git] / cmds-filesystem.c
index 828ca0c..507239a 100644 (file)
 #include "commands.h"
 #include "btrfslabel.h"
 
-static const char filesystem_cmd_group_usage[] =
-       "btrfs filesystem [<group>] <command> [<args>]";
+static const char * const filesystem_cmd_group_usage[] = {
+       "btrfs filesystem [<group>] <command> [<args>]",
+       NULL
+};
 
 static const char * const cmd_df_usage[] = {
        "btrfs filesystem df <path>",
@@ -45,7 +47,7 @@ static const char * const cmd_df_usage[] = {
 
 static int cmd_df(int argc, char **argv)
 {
-       struct btrfs_ioctl_space_args *sargs;
+       struct btrfs_ioctl_space_args *sargs, *sargs_orig;
        u64 count = 0, i;
        int ret;
        int fd;
@@ -63,7 +65,7 @@ static int cmd_df(int argc, char **argv)
                return 12;
        }
 
-       sargs = malloc(sizeof(struct btrfs_ioctl_space_args));
+       sargs_orig = sargs = malloc(sizeof(struct btrfs_ioctl_space_args));
        if (!sargs)
                return -ENOMEM;
 
@@ -75,18 +77,25 @@ static int cmd_df(int argc, char **argv)
        if (ret) {
                fprintf(stderr, "ERROR: couldn't get space info on '%s' - %s\n",
                        path, strerror(e));
+               close(fd);
                free(sargs);
                return ret;
        }
-       if (!sargs->total_spaces)
+       if (!sargs->total_spaces) {
+               close(fd);
+               free(sargs);
                return 0;
+       }
 
        count = sargs->total_spaces;
 
        sargs = realloc(sargs, sizeof(struct btrfs_ioctl_space_args) +
                        (count * sizeof(struct btrfs_ioctl_space_info)));
-       if (!sargs)
+       if (!sargs) {
+               close(fd);
+               free(sargs_orig);
                return -ENOMEM;
+       }
 
        sargs->space_slots = count;
        sargs->total_spaces = 0;
@@ -146,6 +155,7 @@ static int cmd_df(int argc, char **argv)
                printf("%s: total=%s, used=%s\n", description, total_bytes,
                       used_bytes);
        }
+       close(fd);
        free(sargs);
 
        return 0;
@@ -153,8 +163,15 @@ static int cmd_df(int argc, char **argv)
 
 static int uuid_search(struct btrfs_fs_devices *fs_devices, char *search)
 {
+       char uuidbuf[37];
        struct list_head *cur;
        struct btrfs_device *device;
+       int search_len = strlen(search);
+
+       search_len = min(search_len, 37);
+       uuid_unparse(fs_devices->fsid, uuidbuf);
+       if (!strncmp(uuidbuf, search, search_len))
+               return 1;
 
        list_for_each(cur, &fs_devices->devices) {
                device = list_entry(cur, struct btrfs_device, dev_list);
@@ -294,32 +311,6 @@ static int cmd_sync(int argc, char **argv)
        return 0;
 }
 
-static u64 parse_size(char *s)
-{
-       int len = strlen(s);
-       char c;
-       u64 mult = 1;
-
-       if (!isdigit(s[len - 1])) {
-               c = tolower(s[len - 1]);
-               switch (c) {
-               case 'g':
-                       mult *= 1024;
-               case 'm':
-                       mult *= 1024;
-               case 'k':
-                       mult *= 1024;
-               case 'b':
-                       break;
-               default:
-                       fprintf(stderr, "Unknown size descriptor %c\n", c);
-                       exit(1);
-               }
-               s[len - 1] = '\0';
-       }
-       return atoll(s) * mult;
-}
-
 static int parse_compress_type(char *s)
 {
        if (strcmp(optarg, "zlib") == 0)
@@ -453,47 +444,11 @@ static int cmd_defrag(int argc, char **argv)
        return errors + 20;
 }
 
-static const char * const cmd_balance_usage[] = {
-       "btrfs filesystem balance <path>",
-       "Balance the chunks across the device",
-       NULL
-};
-
-static int cmd_balance(int argc, char **argv)
-{
-       int     fdmnt, ret=0, e;
-       struct btrfs_ioctl_vol_args args;
-       char    *path;
-
-       if (check_argc_exact(argc, 2))
-               usage(cmd_balance_usage);
-
-       path = argv[1];
-
-       fdmnt = open_file_or_dir(path);
-       if (fdmnt < 0) {
-               fprintf(stderr, "ERROR: can't access to '%s'\n", path);
-               return 12;
-       }
-
-       memset(&args, 0, sizeof(args));
-       ret = ioctl(fdmnt, BTRFS_IOC_BALANCE, &args);
-       e = errno;
-       close(fdmnt);
-       if(ret<0){
-               fprintf(stderr, "ERROR: error during balancing '%s' - %s\n", 
-                       path, strerror(e));
-
-               return 19;
-       }
-       return 0;
-}
-
 static const char * const cmd_resize_usage[] = {
-       "btrfs filesystem resize [+/-]<newsize>[gkm]|max <path>",
+       "btrfs filesystem resize [devid:][+/-]<newsize>[gkm]|[devid:]max <path>",
        "Resize a filesystem",
        "If 'max' is passed, the filesystem will occupy all available space",
-       "on the device.",
+       "on the device 'devid'.",
        NULL
 };
 
@@ -523,6 +478,7 @@ static int cmd_resize(int argc, char **argv)
 
        printf("Resize '%s' of '%s'\n", path, amount);
        strncpy(args.name, amount, BTRFS_PATH_NAME_MAX);
+       args.name[BTRFS_PATH_NAME_MAX-1] = 0;
        res = ioctl(fd, BTRFS_IOC_RESIZE, &args);
        e = errno;
        close(fd);
@@ -559,7 +515,7 @@ const struct cmd_group filesystem_cmd_group = {
                { "show", cmd_show, cmd_show_usage, NULL, 0 },
                { "sync", cmd_sync, cmd_sync_usage, NULL, 0 },
                { "defragment", cmd_defrag, cmd_defrag_usage, NULL, 0 },
-               { "balance", cmd_balance, cmd_balance_usage, NULL, 0 },
+               { "balance", cmd_balance, NULL, &balance_cmd_group, 1 },
                { "resize", cmd_resize, cmd_resize_usage, NULL, 0 },
                { "label", cmd_label, cmd_label_usage, NULL, 0 },
                { 0, 0, 0, 0, 0 },