ioctls to scan for btrfs filesystems
[btrfs-progs-unstable.git] / btrfsctl.c
blob1518a13e0372d76745140ceec4528bab4a00fb93
1 /*
2 * Copyright (C) 2007 Oracle. All rights reserved.
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.
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.
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.
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"
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
42 void print_usage(void)
44 printf("usage: btrfsctl [ -s name ] [-d] [-r size] file_or_dir\n");
45 printf("\t-d filename defragments one file\n");
46 printf("\t-d directory defragments the entire Btree\n");
47 printf("\t-s snap_name existing_subvol creates a new snapshot\n");
48 printf("\t-s snap_name tree_root creates a new subvolume\n");
49 printf("\t-r [+-]size[gkm] resize the FS\n");
50 printf("\t-a device scans the device for a Btrfs filesystem\n");
51 exit(1);
54 int main(int ac, char **av)
56 char *fname;
57 int fd;
58 int ret;
59 struct btrfs_ioctl_vol_args args;
60 char *name = NULL;
61 int i;
62 struct stat st;
63 DIR *dirstream;
64 unsigned long command = 0;
65 int len;
67 for (i = 1; i < ac - 1; i++) {
68 if (strcmp(av[i], "-s") == 0) {
69 if (i + 1 >= ac - 1) {
70 fprintf(stderr, "-s requires an arg");
71 print_usage();
73 name = av[i + 1];
74 len = strlen(name);
75 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
76 fprintf(stderr,
77 "snapshot name zero length or too long\n");
78 exit(1);
80 if (strchr(name, '/')) {
81 fprintf(stderr,
82 "error: / not allowed in names\n");
83 exit(1);
85 command = BTRFS_IOC_SNAP_CREATE;
86 } else if (strcmp(av[i], "-d") == 0) {
87 if (i >= ac - 1) {
88 fprintf(stderr, "-d requires an arg\n");
89 print_usage();
91 command = BTRFS_IOC_DEFRAG;
92 } else if (strcmp(av[i], "-a") == 0) {
93 if (i >= ac - 1) {
94 fprintf(stderr, "-a requires an arg\n");
95 print_usage();
97 command = BTRFS_IOC_SCAN_DEV;
98 } else if (strcmp(av[i], "-r") == 0) {
99 if (i >= ac - 1) {
100 fprintf(stderr, "-r requires an arg\n");
101 print_usage();
103 name = av[i + 1];
104 len = strlen(name);
105 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
106 fprintf(stderr, "-r size too long\n");
107 exit(1);
109 command = BTRFS_IOC_RESIZE;
112 if (command == 0) {
113 fprintf(stderr, "no valid commands given\n");
114 exit(1);
116 fname = av[ac - 1];
117 ret = stat(fname, &st);
118 if (ret < 0) {
119 perror("stat:");
120 exit(1);
122 if (S_ISDIR(st.st_mode)) {
123 dirstream = opendir(fname);
124 if (!dirstream) {
125 perror("opendir");
126 exit(1);
128 fd = dirfd(dirstream);
129 } else if (command == BTRFS_IOC_SCAN_DEV) {
130 fd = open("/dev/btrfs-control", O_RDWR);
131 printf("scanning %s command %lu\n", fname, BTRFS_IOC_SCAN_DEV);
132 name = fname;
133 } else {
134 fd = open(fname, O_RDWR);
136 if (fd < 0) {
137 perror("open");
138 exit(1);
140 if (name)
141 strcpy(args.name, name);
142 else
143 args.name[0] = '\0';
144 ret = ioctl(fd, command, &args);
145 printf("ioctl returns %d\n", ret);
146 return 0;