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.
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.
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.
22 #include <sys/ioctl.h>
26 #include "kerncompat.h"
33 /* FIXME - imported cruft, fix sparse errors and warnings */
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; }
42 static const char * const device_cmd_group_usage
[] = {
43 "btrfs device <command> [<args>]",
47 static const char * const cmd_add_dev_usage
[] = {
48 "btrfs device add <device> [<device>...] <path>",
49 "Add a device to a filesystem",
53 static int cmd_add_dev(int argc
, char **argv
)
56 int i
, fdmnt
, ret
=0, e
;
58 if (check_argc_min(argc
, 3))
59 usage(cmd_add_dev_usage
);
61 mntpnt
= argv
[argc
- 1];
63 fdmnt
= open_file_or_dir(mntpnt
);
65 fprintf(stderr
, "ERROR: can't access to '%s'\n", mntpnt
);
69 for (i
= 1; i
< argc
- 1; i
++ ){
70 struct btrfs_ioctl_vol_args ioctl_args
;
72 u64 dev_block_count
= 0;
76 res
= check_mounted(argv
[i
]);
78 fprintf(stderr
, "error checking %s mount status\n",
84 fprintf(stderr
, "%s is mounted\n", argv
[i
]);
89 devfd
= open(argv
[i
], O_RDWR
);
91 fprintf(stderr
, "ERROR: Unable to open device '%s'\n", argv
[i
]);
96 res
= fstat(devfd
, &st
);
98 fprintf(stderr
, "ERROR: Unable to stat '%s'\n", argv
[i
]);
103 if (!S_ISBLK(st
.st_mode
)) {
104 fprintf(stderr
, "ERROR: '%s' is not a block device\n", argv
[i
]);
110 res
= btrfs_prepare_device(devfd
, argv
[i
], 1, &dev_block_count
,
113 fprintf(stderr
, "ERROR: Unable to init '%s'\n", argv
[i
]);
120 strncpy(ioctl_args
.name
, argv
[i
], BTRFS_PATH_NAME_MAX
);
121 ioctl_args
.name
[BTRFS_PATH_NAME_MAX
-1] = 0;
122 res
= ioctl(fdmnt
, BTRFS_IOC_ADD_DEV
, &ioctl_args
);
125 fprintf(stderr
, "ERROR: error adding the device '%s' - %s\n",
126 argv
[i
], strerror(e
));
139 static const char * const cmd_rm_dev_usage
[] = {
140 "btrfs device delete <device> [<device>...] <path>",
141 "Remove a device from a filesystem",
145 static int cmd_rm_dev(int argc
, char **argv
)
148 int i
, fdmnt
, ret
=0, e
;
150 if (check_argc_min(argc
, 3))
151 usage(cmd_rm_dev_usage
);
153 mntpnt
= argv
[argc
- 1];
155 fdmnt
= open_file_or_dir(mntpnt
);
157 fprintf(stderr
, "ERROR: can't access to '%s'\n", mntpnt
);
161 for(i
=1 ; i
< argc
- 1; i
++ ){
162 struct btrfs_ioctl_vol_args arg
;
165 strncpy(arg
.name
, argv
[i
], BTRFS_PATH_NAME_MAX
);
166 arg
.name
[BTRFS_PATH_NAME_MAX
-1] = 0;
167 res
= ioctl(fdmnt
, BTRFS_IOC_RM_DEV
, &arg
);
170 fprintf(stderr
, "ERROR: error removing the device '%s' - %s\n",
171 argv
[i
], strerror(e
));
183 static const char * const cmd_scan_dev_usage
[] = {
184 "btrfs device scan [<device>...]",
185 "Scan devices for a btrfs filesystem",
189 static int cmd_scan_dev(int argc
, char **argv
)
195 if( argc
> 1 && !strcmp(argv
[1],"--all-devices")){
196 if (check_argc_max(argc
, 2))
197 usage(cmd_scan_dev_usage
);
207 printf("Scanning for Btrfs filesystems\n");
209 ret
= btrfs_scan_block_devices(1);
211 ret
= btrfs_scan_one_dir("/dev", 1);
213 fprintf(stderr
, "ERROR: error %d while scanning\n", ret
);
219 fd
= open("/dev/btrfs-control", O_RDWR
);
221 perror("failed to open /dev/btrfs-control");
225 for( i
= devstart
; i
< argc
; i
++ ){
226 struct btrfs_ioctl_vol_args args
;
229 printf("Scanning for Btrfs filesystems in '%s'\n", argv
[i
]);
231 strncpy(args
.name
, argv
[i
], BTRFS_PATH_NAME_MAX
);
232 args
.name
[BTRFS_PATH_NAME_MAX
-1] = 0;
234 * FIXME: which are the error code returned by this ioctl ?
235 * it seems that is impossible to understand if there no is
236 * a btrfs filesystem from an I/O error !!!
238 ret
= ioctl(fd
, BTRFS_IOC_SCAN_DEV
, &args
);
243 fprintf(stderr
, "ERROR: unable to scan the device '%s' - %s\n",
244 argv
[i
], strerror(e
));
253 const struct cmd_group device_cmd_group
= {
254 device_cmd_group_usage
, NULL
, {
255 { "add", cmd_add_dev
, cmd_add_dev_usage
, NULL
, 0 },
256 { "delete", cmd_rm_dev
, cmd_rm_dev_usage
, NULL
, 0 },
257 { "scan", cmd_scan_dev
, cmd_scan_dev_usage
, NULL
, 0 },
262 int cmd_device(int argc
, char **argv
)
264 return handle_command_group(&device_cmd_group
, argc
, argv
);