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 device_cmd_group_usage
[] =
43 "btrfs device <command> [<args>]";
45 static const char * const cmd_add_dev_usage
[] = {
46 "btrfs device add <device> [<device>...] <path>",
47 "Add a device to a filesystem",
51 static int cmd_add_dev(int argc
, char **argv
)
54 int i
, fdmnt
, ret
=0, e
;
56 if (check_argc_min(argc
, 3))
57 usage(cmd_add_dev_usage
);
59 mntpnt
= argv
[argc
- 1];
61 fdmnt
= open_file_or_dir(mntpnt
);
63 fprintf(stderr
, "ERROR: can't access to '%s'\n", mntpnt
);
67 for (i
= 1; i
< argc
- 1; i
++ ){
68 struct btrfs_ioctl_vol_args ioctl_args
;
70 u64 dev_block_count
= 0;
74 res
= check_mounted(argv
[i
]);
76 fprintf(stderr
, "error checking %s mount status\n",
82 fprintf(stderr
, "%s is mounted\n", argv
[i
]);
87 devfd
= open(argv
[i
], O_RDWR
);
89 fprintf(stderr
, "ERROR: Unable to open device '%s'\n", argv
[i
]);
94 res
= fstat(devfd
, &st
);
96 fprintf(stderr
, "ERROR: Unable to stat '%s'\n", argv
[i
]);
101 if (!S_ISBLK(st
.st_mode
)) {
102 fprintf(stderr
, "ERROR: '%s' is not a block device\n", argv
[i
]);
108 res
= btrfs_prepare_device(devfd
, argv
[i
], 1, &dev_block_count
, &mixed
);
110 fprintf(stderr
, "ERROR: Unable to init '%s'\n", argv
[i
]);
117 strncpy(ioctl_args
.name
, argv
[i
], BTRFS_PATH_NAME_MAX
);
118 res
= ioctl(fdmnt
, BTRFS_IOC_ADD_DEV
, &ioctl_args
);
121 fprintf(stderr
, "ERROR: error adding the device '%s' - %s\n",
122 argv
[i
], strerror(e
));
135 static const char * const cmd_rm_dev_usage
[] = {
136 "btrfs device delete <device> [<device>...] <path>",
137 "Remove a device from a filesystem",
141 static int cmd_rm_dev(int argc
, char **argv
)
144 int i
, fdmnt
, ret
=0, e
;
146 if (check_argc_min(argc
, 3))
147 usage(cmd_rm_dev_usage
);
149 mntpnt
= argv
[argc
- 1];
151 fdmnt
= open_file_or_dir(mntpnt
);
153 fprintf(stderr
, "ERROR: can't access to '%s'\n", mntpnt
);
157 for(i
=1 ; i
< argc
- 1; i
++ ){
158 struct btrfs_ioctl_vol_args arg
;
161 strncpy(arg
.name
, argv
[i
], BTRFS_PATH_NAME_MAX
);
162 res
= ioctl(fdmnt
, BTRFS_IOC_RM_DEV
, &arg
);
165 fprintf(stderr
, "ERROR: error removing the device '%s' - %s\n",
166 argv
[i
], strerror(e
));
178 static const char * const cmd_scan_dev_usage
[] = {
179 "btrfs device scan [<device>...]",
180 "Scan devices for a btrfs filesystem",
184 static int cmd_scan_dev(int argc
, char **argv
)
190 if( argc
> 1 && !strcmp(argv
[1],"--all-devices")){
191 if (check_argc_max(argc
, 2))
192 usage(cmd_scan_dev_usage
);
202 printf("Scanning for Btrfs filesystems\n");
204 ret
= btrfs_scan_block_devices(1);
206 ret
= btrfs_scan_one_dir("/dev", 1);
208 fprintf(stderr
, "ERROR: error %d while scanning\n", ret
);
214 fd
= open("/dev/btrfs-control", O_RDWR
);
216 perror("failed to open /dev/btrfs-control");
220 for( i
= devstart
; i
< argc
; i
++ ){
221 struct btrfs_ioctl_vol_args args
;
224 printf("Scanning for Btrfs filesystems in '%s'\n", argv
[i
]);
226 strncpy(args
.name
, argv
[i
], BTRFS_PATH_NAME_MAX
);
228 * FIXME: which are the error code returned by this ioctl ?
229 * it seems that is impossible to understand if there no is
230 * a btrfs filesystem from an I/O error !!!
232 ret
= ioctl(fd
, BTRFS_IOC_SCAN_DEV
, &args
);
237 fprintf(stderr
, "ERROR: unable to scan the device '%s' - %s\n",
238 argv
[i
], strerror(e
));
247 const struct cmd_group device_cmd_group
= {
248 device_cmd_group_usage
, NULL
, {
249 { "add", cmd_add_dev
, cmd_add_dev_usage
, NULL
, 0 },
250 { "delete", cmd_rm_dev
, cmd_rm_dev_usage
, NULL
, 0 },
251 { "scan", cmd_scan_dev
, cmd_scan_dev_usage
, NULL
, 0 },
256 int cmd_device(int argc
, char **argv
)
258 return handle_command_group(&device_cmd_group
, argc
, argv
);