Btrfs-progs: rearrange files in the repo
[platform/upstream/btrfs-progs.git] / cmds-device.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public
4  * License v2 as published by the Free Software Foundation.
5  *
6  * This program is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
9  * General Public License for more details.
10  *
11  * You should have received a copy of the GNU General Public
12  * License along with this program; if not, write to the
13  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
14  * Boston, MA 021110-1307, USA.
15  */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <unistd.h>
21 #include <fcntl.h>
22 #include <sys/ioctl.h>
23 #include <errno.h>
24 #include <sys/stat.h>
25
26 #include "kerncompat.h"
27 #include "ctree.h"
28 #include "ioctl.h"
29 #include "utils.h"
30
31 #include "btrfs_cmds.h"
32
33 /* FIXME - imported cruft, fix sparse errors and warnings */
34 #ifdef __CHECKER__
35 #define BLKGETSIZE64 0
36 #define BTRFS_IOC_SNAP_CREATE_V2 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 int do_add_volume(int nargs, char **args)
43 {
44
45         char    *mntpnt = args[nargs-1];
46         int     i, fdmnt, ret=0, e;
47
48
49         fdmnt = open_file_or_dir(mntpnt);
50         if (fdmnt < 0) {
51                 fprintf(stderr, "ERROR: can't access to '%s'\n", mntpnt);
52                 return 12;
53         }
54
55         for (i = 1; i < (nargs-1); i++ ){
56                 struct btrfs_ioctl_vol_args ioctl_args;
57                 int     devfd, res;
58                 u64 dev_block_count = 0;
59                 struct stat st;
60                 int mixed = 0;
61
62                 res = check_mounted(args[i]);
63                 if (res < 0) {
64                         fprintf(stderr, "error checking %s mount status\n",
65                                 args[i]);
66                         ret++;
67                         continue;
68                 }
69                 if (res == 1) {
70                         fprintf(stderr, "%s is mounted\n", args[i]);
71                         ret++;
72                         continue;
73                 }
74
75                 devfd = open(args[i], O_RDWR);
76                 if (!devfd) {
77                         fprintf(stderr, "ERROR: Unable to open device '%s'\n", args[i]);
78                         close(devfd);
79                         ret++;
80                         continue;
81                 }
82                 res = fstat(devfd, &st);
83                 if (res) {
84                         fprintf(stderr, "ERROR: Unable to stat '%s'\n", args[i]);
85                         close(devfd);
86                         ret++;
87                         continue;
88                 }
89                 if (!S_ISBLK(st.st_mode)) {
90                         fprintf(stderr, "ERROR: '%s' is not a block device\n", args[i]);
91                         close(devfd);
92                         ret++;
93                         continue;
94                 }
95
96                 res = btrfs_prepare_device(devfd, args[i], 1, &dev_block_count, &mixed);
97                 if (res) {
98                         fprintf(stderr, "ERROR: Unable to init '%s'\n", args[i]);
99                         close(devfd);
100                         ret++;
101                         continue;
102                 }
103                 close(devfd);
104
105                 strncpy(ioctl_args.name, args[i], BTRFS_PATH_NAME_MAX);
106                 res = ioctl(fdmnt, BTRFS_IOC_ADD_DEV, &ioctl_args);
107                 e = errno;
108                 if(res<0){
109                         fprintf(stderr, "ERROR: error adding the device '%s' - %s\n", 
110                                 args[i], strerror(e));
111                         ret++;
112                 }
113
114         }
115
116         close(fdmnt);
117         if (ret)
118                 return ret+20;
119         else
120                 return 0;
121
122 }
123
124 int do_remove_volume(int nargs, char **args)
125 {
126
127         char    *mntpnt = args[nargs-1];
128         int     i, fdmnt, ret=0, e;
129
130         fdmnt = open_file_or_dir(mntpnt);
131         if (fdmnt < 0) {
132                 fprintf(stderr, "ERROR: can't access to '%s'\n", mntpnt);
133                 return 12;
134         }
135
136         for(i=1 ; i < (nargs-1) ; i++ ){
137                 struct  btrfs_ioctl_vol_args arg;
138                 int     res;
139
140                 strncpy(arg.name, args[i], BTRFS_PATH_NAME_MAX);
141                 res = ioctl(fdmnt, BTRFS_IOC_RM_DEV, &arg);
142                 e = errno;
143                 if(res<0){
144                         fprintf(stderr, "ERROR: error removing the device '%s' - %s\n", 
145                                 args[i], strerror(e));
146                         ret++;
147                 }
148         }
149
150         close(fdmnt);
151         if( ret)
152                 return ret+20;
153         else
154                 return 0;
155 }
156
157 int do_scan(int argc, char **argv)
158 {
159         int     i, fd, e;
160         int     checklist = 1;
161         int     devstart = 1;
162
163         if( argc >= 2 && !strcmp(argv[1],"--all-devices")){
164
165                 if( argc >2 ){
166                         fprintf(stderr, "ERROR: too may arguments\n");
167                         return 22;
168                 }
169
170                 checklist = 0;
171                 devstart += 1;
172         }
173
174         if(argc<=devstart){
175
176                 int ret;
177
178                 printf("Scanning for Btrfs filesystems\n");
179                 if(checklist)
180                         ret = btrfs_scan_block_devices(1);
181                 else
182                         ret = btrfs_scan_one_dir("/dev", 1);
183                 if (ret){
184                         fprintf(stderr, "ERROR: error %d while scanning\n", ret);
185                         return 18;
186                 }
187                 return 0;
188         }
189
190         fd = open("/dev/btrfs-control", O_RDWR);
191         if (fd < 0) {
192                 perror("failed to open /dev/btrfs-control");
193                 return 10;
194         }
195
196         for( i = devstart ; i < argc ; i++ ){
197                 struct btrfs_ioctl_vol_args args;
198                 int ret;
199
200                 printf("Scanning for Btrfs filesystems in '%s'\n", argv[i]);
201
202                 strncpy(args.name, argv[i], BTRFS_PATH_NAME_MAX);
203                 /*
204                  * FIXME: which are the error code returned by this ioctl ?
205                  * it seems that is impossible to understand if there no is
206                  * a btrfs filesystem from an I/O error !!!
207                  */
208                 ret = ioctl(fd, BTRFS_IOC_SCAN_DEV, &args);
209                 e = errno;
210
211                 if( ret < 0 ){
212                         close(fd);
213                         fprintf(stderr, "ERROR: unable to scan the device '%s' - %s\n", 
214                                 argv[i], strerror(e));
215                         return 11;
216                 }
217         }
218
219         close(fd);
220         return 0;
221
222 }
223