4dfc42bd6406fd8b854606213d5803007a76d32e
[platform/upstream/btrfs-progs.git] / btrfsctl.c
1 /*
2  * Copyright (C) 2007 Oracle.  All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public
6  * License v2 as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public
14  * License along with this program; if not, write to the
15  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16  * Boston, MA 021110-1307, USA.
17  */
18
19 #ifndef __CHECKER__
20 #include <sys/ioctl.h>
21 #include <sys/mount.h>
22 #include "ioctl.h"
23 #endif
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <fcntl.h>
29 #include <unistd.h>
30 #include <dirent.h>
31 #include "kerncompat.h"
32
33 #ifdef __CHECKER__
34 #define BLKGETSIZE64 0
35 #define BTRFS_IOC_SNAP_CREATE 0
36 #define BTRFS_IOC_ADD_DISK 0
37 #define BTRFS_VOL_NAME_MAX 255
38 struct btrfs_ioctl_vol_args { char name[BTRFS_VOL_NAME_MAX]; };
39 static inline int ioctl(int fd, int define, void *arg) { return 0; }
40 #endif
41
42 void print_usage(void)
43 {
44         printf("usage: btrfsctl [ -s snapshot_name ] dir\n");
45         exit(1);
46 }
47
48 int main(int ac, char **av)
49 {
50         char *fname;
51         int fd;
52         int ret;
53         struct btrfs_ioctl_vol_args args;
54         char *name = NULL;
55         int i;
56         struct stat st;
57         DIR *dirstream;
58         unsigned long command = 0;
59         int len;
60
61         for (i = 1; i < ac - 1; i++) {
62                 if (strcmp(av[i], "-s") == 0) {
63                         if (i + 1 >= ac - 1) {
64                                 fprintf(stderr, "-s requires an arg");
65                                 print_usage();
66                         }
67                         name = av[i + 1];
68                         len = strlen(name);
69                         if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
70                                 fprintf(stderr,
71                                      "snapshot name zero length or too long\n");
72                                 exit(1);
73                         }
74                         if (strchr(name, '/')) {
75                                 fprintf(stderr,
76                                         "error: / not allowed in names\n");
77                                 exit(1);
78                         }
79                         command = BTRFS_IOC_SNAP_CREATE;
80                 } else if (strcmp(av[i], "-d") == 0) {
81                         if (i >= ac - 1) {
82                                 fprintf(stderr, "-d requires an arg");
83                                 print_usage();
84                         }
85                         command = BTRFS_IOC_DEFRAG;
86                 }
87         }
88         if (command == 0) {
89                 fprintf(stderr, "no valid commands given\n");
90                 exit(1);
91         }
92         fname = av[ac - 1];
93         ret = stat(fname, &st);
94         if (ret < 0) {
95                 perror("stat:");
96                 exit(1);
97         }
98         if (S_ISDIR(st.st_mode)) {
99                 dirstream = opendir(fname);
100                 if (!dirstream) {
101                         perror("opendir");
102                         exit(1);
103                 }
104                 fd = dirfd(dirstream);
105         } else {
106                 fd = open(fname, O_RDWR);
107         } if (fd < 0) {
108                 perror("open");
109                 exit(1);
110         }
111         if (name)
112                 strcpy(args.name, name);
113         else
114                 args.name[0] = '\0';
115         ret = ioctl(fd, command, &args);
116         printf("ioctl returns %d\n", ret);
117         return 0;
118 }
119