Add sync and transaction ioctl defs
[btrfs-progs-unstable.git] / btrfsctl.c
blob7e7e79b5d2c9d8442edfa9d0322d4d4cd64e6731
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"
32 #include "ctree.h"
33 #include "transaction.h"
34 #include "utils.h"
36 #ifdef __CHECKER__
37 #define BLKGETSIZE64 0
38 #define BTRFS_IOC_SNAP_CREATE 0
39 #define BTRFS_VOL_NAME_MAX 255
40 struct btrfs_ioctl_vol_args { char name[BTRFS_VOL_NAME_MAX]; };
41 static inline int ioctl(int fd, int define, void *arg) { return 0; }
42 #endif
44 void print_usage(void)
46 printf("usage: btrfsctl [ -s name ] [-d] [-r size] file_or_dir\n");
47 printf("\t-d filename defragments one file\n");
48 printf("\t-d directory defragments the entire Btree\n");
49 printf("\t-s snap_name existing_subvol creates a new snapshot\n");
50 printf("\t-s snap_name tree_root creates a new subvolume\n");
51 printf("\t-r [+-]size[gkm] resize the FS\n");
52 printf("\t-A device scans the device for a Btrfs filesystem\n");
53 printf("\t-a scans all devices for Btrfs filesystems\n");
54 printf("\t-c forces a single FS sync\n");
55 exit(1);
58 int main(int ac, char **av)
60 char *fname;
61 int fd;
62 int ret;
63 struct btrfs_ioctl_vol_args args;
64 char *name = NULL;
65 int i;
66 struct stat st;
67 DIR *dirstream;
68 unsigned long command = 0;
69 int len;
71 if (ac == 2 && strcmp(av[1], "-a") == 0) {
72 fprintf(stderr, "Scanning for Btrfs filesystems\n");
73 btrfs_scan_one_dir("/dev", 1);
74 exit(0);
76 for (i = 1; i < ac - 1; i++) {
77 if (strcmp(av[i], "-s") == 0) {
78 if (i + 1 >= ac - 1) {
79 fprintf(stderr, "-s requires an arg");
80 print_usage();
82 name = av[i + 1];
83 len = strlen(name);
84 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
85 fprintf(stderr,
86 "snapshot name zero length or too long\n");
87 exit(1);
89 if (strchr(name, '/')) {
90 fprintf(stderr,
91 "error: / not allowed in names\n");
92 exit(1);
94 command = BTRFS_IOC_SNAP_CREATE;
95 } else if (strcmp(av[i], "-d") == 0) {
96 if (i >= ac - 1) {
97 fprintf(stderr, "-d requires an arg\n");
98 print_usage();
100 command = BTRFS_IOC_DEFRAG;
101 } else if (strcmp(av[i], "-A") == 0) {
102 if (i >= ac - 1) {
103 fprintf(stderr, "-A requires an arg\n");
104 print_usage();
106 command = BTRFS_IOC_SCAN_DEV;
107 } else if (strcmp(av[i], "-r") == 0) {
108 if (i >= ac - 1) {
109 fprintf(stderr, "-r requires an arg\n");
110 print_usage();
112 name = av[i + 1];
113 len = strlen(name);
114 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
115 fprintf(stderr, "-r size too long\n");
116 exit(1);
118 command = BTRFS_IOC_RESIZE;
119 } else if (strcmp(av[i], "-c") == 0) {
120 command = BTRFS_IOC_SYNC;
123 if (command == 0) {
124 fprintf(stderr, "no valid commands given\n");
125 print_usage();
126 exit(1);
128 fname = av[ac - 1];
129 ret = stat(fname, &st);
130 if (ret < 0) {
131 perror("stat:");
132 exit(1);
134 if (S_ISDIR(st.st_mode)) {
135 dirstream = opendir(fname);
136 if (!dirstream) {
137 perror("opendir");
138 exit(1);
140 fd = dirfd(dirstream);
141 } else if (command == BTRFS_IOC_SCAN_DEV) {
142 fd = open("/dev/btrfs-control", O_RDWR);
143 name = fname;
144 } else {
145 fd = open(fname, O_RDWR);
147 if (fd < 0) {
148 perror("open");
149 exit(1);
151 if (name)
152 strcpy(args.name, name);
153 else
154 args.name[0] = '\0';
155 ret = ioctl(fd, command, &args);
156 if (ret < 0) {
157 perror("ioctl:");
158 exit(1);
160 printf("ioctl returns %d\n", ret);
161 return 0;