2007-06-12 13:07:11 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2007 Oracle. All rights reserved.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public
|
|
|
|
* License v2 as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public
|
|
|
|
* License along with this program; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 021110-1307, USA.
|
|
|
|
*/
|
|
|
|
|
2007-04-10 13:27:30 +00:00
|
|
|
#ifndef __CHECKER__
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#include "ioctl.h"
|
|
|
|
#endif
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
2007-04-10 18:12:24 +00:00
|
|
|
#include <dirent.h>
|
2008-11-18 16:26:35 +00:00
|
|
|
#include <libgen.h>
|
2009-12-11 21:12:07 +00:00
|
|
|
#include <stdlib.h>
|
2007-04-10 13:27:30 +00:00
|
|
|
#include "kerncompat.h"
|
2008-03-24 19:05:44 +00:00
|
|
|
#include "ctree.h"
|
|
|
|
#include "transaction.h"
|
|
|
|
#include "utils.h"
|
2008-07-24 16:13:30 +00:00
|
|
|
#include "version.h"
|
2007-04-10 13:27:30 +00:00
|
|
|
|
|
|
|
#ifdef __CHECKER__
|
|
|
|
#define BLKGETSIZE64 0
|
|
|
|
#define BTRFS_IOC_SNAP_CREATE 0
|
|
|
|
#define BTRFS_VOL_NAME_MAX 255
|
|
|
|
struct btrfs_ioctl_vol_args { char name[BTRFS_VOL_NAME_MAX]; };
|
|
|
|
static inline int ioctl(int fd, int define, void *arg) { return 0; }
|
|
|
|
#endif
|
|
|
|
|
2009-01-21 18:22:49 +00:00
|
|
|
static void print_usage(void)
|
2007-04-10 13:27:30 +00:00
|
|
|
{
|
2008-07-30 17:29:28 +00:00
|
|
|
printf("usage: btrfsctl [ -d file|dir] [ -s snap_name subvol|tree ]\n");
|
2009-09-21 20:03:57 +00:00
|
|
|
printf(" [-r size] [-A device] [-a] [-c] [-D dir .]\n");
|
2008-07-30 17:29:28 +00:00
|
|
|
printf("\t-d filename: defragments one file\n");
|
|
|
|
printf("\t-d directory: defragments the entire Btree\n");
|
2008-11-18 16:26:35 +00:00
|
|
|
printf("\t-s snap_name dir: creates a new snapshot of dir\n");
|
|
|
|
printf("\t-S subvol_name dir: creates a new subvolume\n");
|
2008-07-30 17:29:28 +00:00
|
|
|
printf("\t-r [+-]size[gkm]: resize the FS by size amount\n");
|
|
|
|
printf("\t-A device: scans the device file for a Btrfs filesystem\n");
|
|
|
|
printf("\t-a: scans all devices for Btrfs filesystems\n");
|
|
|
|
printf("\t-c: forces a single FS sync\n");
|
2009-09-21 20:03:57 +00:00
|
|
|
printf("\t-D: delete snapshot\n");
|
2009-12-11 21:12:07 +00:00
|
|
|
printf("\t-m [tree id] directory: set the default mounted subvolume"
|
|
|
|
" to the [tree id] or the directory\n");
|
2008-07-24 16:13:30 +00:00
|
|
|
printf("%s\n", BTRFS_BUILD_VERSION);
|
2007-04-10 13:27:30 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2009-01-21 18:22:49 +00:00
|
|
|
static int open_file_or_dir(const char *fname)
|
2008-11-18 16:26:35 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct stat st;
|
|
|
|
DIR *dirstream;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
ret = stat(fname, &st);
|
|
|
|
if (ret < 0) {
|
|
|
|
perror("stat:");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (S_ISDIR(st.st_mode)) {
|
|
|
|
dirstream = opendir(fname);
|
|
|
|
if (!dirstream) {
|
|
|
|
perror("opendir");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
fd = dirfd(dirstream);
|
|
|
|
} else {
|
|
|
|
fd = open(fname, O_RDWR);
|
|
|
|
}
|
|
|
|
if (fd < 0) {
|
|
|
|
perror("open");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
return fd;
|
|
|
|
}
|
2007-04-10 13:27:30 +00:00
|
|
|
int main(int ac, char **av)
|
|
|
|
{
|
2008-11-18 16:26:35 +00:00
|
|
|
char *fname = NULL;
|
|
|
|
char *snap_location = NULL;
|
|
|
|
int snap_fd = 0;
|
2007-04-10 13:27:30 +00:00
|
|
|
int fd;
|
|
|
|
int ret;
|
|
|
|
struct btrfs_ioctl_vol_args args;
|
2007-05-22 14:07:10 +00:00
|
|
|
char *name = NULL;
|
2007-04-10 13:27:30 +00:00
|
|
|
int i;
|
2007-05-22 14:07:10 +00:00
|
|
|
unsigned long command = 0;
|
2007-06-12 12:21:28 +00:00
|
|
|
int len;
|
2010-09-09 03:19:25 +00:00
|
|
|
char *pos;
|
2008-11-18 16:26:35 +00:00
|
|
|
char *fullpath;
|
2009-12-11 21:12:07 +00:00
|
|
|
u64 objectid = 0;
|
2007-04-10 13:27:30 +00:00
|
|
|
|
2008-03-24 19:05:44 +00:00
|
|
|
if (ac == 2 && strcmp(av[1], "-a") == 0) {
|
|
|
|
fprintf(stderr, "Scanning for Btrfs filesystems\n");
|
|
|
|
btrfs_scan_one_dir("/dev", 1);
|
|
|
|
exit(0);
|
|
|
|
}
|
2008-07-30 13:17:21 +00:00
|
|
|
for (i = 1; i < ac; i++) {
|
2007-04-10 13:27:30 +00:00
|
|
|
if (strcmp(av[i], "-s") == 0) {
|
|
|
|
if (i + 1 >= ac - 1) {
|
|
|
|
fprintf(stderr, "-s requires an arg");
|
|
|
|
print_usage();
|
|
|
|
}
|
2008-11-18 16:26:35 +00:00
|
|
|
fullpath = av[i + 1];
|
|
|
|
|
|
|
|
snap_location = strdup(fullpath);
|
|
|
|
snap_location = dirname(snap_location);
|
|
|
|
|
|
|
|
snap_fd = open_file_or_dir(snap_location);
|
|
|
|
|
|
|
|
name = strdup(fullpath);
|
|
|
|
name = basename(name);
|
2007-06-12 12:21:28 +00:00
|
|
|
len = strlen(name);
|
2008-11-18 16:26:35 +00:00
|
|
|
|
2007-06-12 12:21:28 +00:00
|
|
|
if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"snapshot name zero length or too long\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (strchr(name, '/')) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"error: / not allowed in names\n");
|
2007-04-10 13:27:30 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2007-04-12 14:51:51 +00:00
|
|
|
command = BTRFS_IOC_SNAP_CREATE;
|
2008-11-18 16:26:35 +00:00
|
|
|
} else if (strcmp(av[i], "-S") == 0) {
|
|
|
|
if (i + 1 >= ac - 1) {
|
|
|
|
fprintf(stderr, "-S requires an arg");
|
|
|
|
print_usage();
|
|
|
|
}
|
|
|
|
name = av[i + 1];
|
|
|
|
len = strlen(name);
|
|
|
|
if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"snapshot name zero length or too long\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (strchr(name, '/')) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"error: / not allowed in names\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
command = BTRFS_IOC_SUBVOL_CREATE;
|
2007-08-07 20:15:59 +00:00
|
|
|
} else if (strcmp(av[i], "-d") == 0) {
|
|
|
|
if (i >= ac - 1) {
|
2007-12-21 21:25:35 +00:00
|
|
|
fprintf(stderr, "-d requires an arg\n");
|
2007-08-07 20:15:59 +00:00
|
|
|
print_usage();
|
|
|
|
}
|
|
|
|
command = BTRFS_IOC_DEFRAG;
|
2009-09-21 20:03:57 +00:00
|
|
|
} else if (strcmp(av[i], "-D") == 0) {
|
|
|
|
if (i >= ac - 1) {
|
|
|
|
fprintf(stderr, "-D requires an arg\n");
|
|
|
|
print_usage();
|
|
|
|
}
|
|
|
|
command = BTRFS_IOC_SNAP_DESTROY;
|
|
|
|
name = av[i + 1];
|
|
|
|
len = strlen(name);
|
2010-09-09 03:19:25 +00:00
|
|
|
pos = strchr(name, '/');
|
|
|
|
if (pos) {
|
|
|
|
if (*(pos + 1) == '\0')
|
|
|
|
*(pos) = '\0';
|
|
|
|
else {
|
|
|
|
fprintf(stderr,
|
|
|
|
"error: / not allowed in names\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
2009-09-21 20:03:57 +00:00
|
|
|
if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
|
|
|
|
fprintf(stderr, "-D size too long\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2008-03-24 19:05:44 +00:00
|
|
|
} else if (strcmp(av[i], "-A") == 0) {
|
2008-03-24 19:04:49 +00:00
|
|
|
if (i >= ac - 1) {
|
2008-03-24 19:05:44 +00:00
|
|
|
fprintf(stderr, "-A requires an arg\n");
|
2008-03-24 19:04:49 +00:00
|
|
|
print_usage();
|
|
|
|
}
|
|
|
|
command = BTRFS_IOC_SCAN_DEV;
|
2007-12-21 21:25:35 +00:00
|
|
|
} else if (strcmp(av[i], "-r") == 0) {
|
|
|
|
if (i >= ac - 1) {
|
|
|
|
fprintf(stderr, "-r requires an arg\n");
|
|
|
|
print_usage();
|
|
|
|
}
|
|
|
|
name = av[i + 1];
|
|
|
|
len = strlen(name);
|
|
|
|
if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
|
|
|
|
fprintf(stderr, "-r size too long\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
command = BTRFS_IOC_RESIZE;
|
2008-06-10 14:09:18 +00:00
|
|
|
} else if (strcmp(av[i], "-c") == 0) {
|
|
|
|
command = BTRFS_IOC_SYNC;
|
2009-12-11 21:12:07 +00:00
|
|
|
} else if (strcmp(av[i], "-m") == 0) {
|
|
|
|
command = BTRFS_IOC_DEFAULT_SUBVOL;
|
|
|
|
if (i == ac - 3) {
|
|
|
|
objectid = (unsigned long long)
|
|
|
|
strtoll(av[i + 1], NULL, 0);
|
|
|
|
if (errno == ERANGE) {
|
|
|
|
fprintf(stderr, "invalid tree id\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
2007-04-12 14:51:51 +00:00
|
|
|
}
|
2007-04-10 13:27:30 +00:00
|
|
|
}
|
2007-05-22 14:07:10 +00:00
|
|
|
if (command == 0) {
|
|
|
|
fprintf(stderr, "no valid commands given\n");
|
2008-06-10 02:23:13 +00:00
|
|
|
print_usage();
|
2007-05-22 14:07:10 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2007-04-10 13:27:30 +00:00
|
|
|
fname = av[ac - 1];
|
2008-11-18 16:26:35 +00:00
|
|
|
|
|
|
|
if (command == BTRFS_IOC_SCAN_DEV) {
|
2008-03-24 19:04:49 +00:00
|
|
|
fd = open("/dev/btrfs-control", O_RDWR);
|
2009-01-21 18:22:49 +00:00
|
|
|
if (fd < 0) {
|
|
|
|
perror("failed to open /dev/btrfs-control");
|
|
|
|
exit(1);
|
|
|
|
}
|
2008-03-24 19:04:49 +00:00
|
|
|
name = fname;
|
2008-11-18 16:26:35 +00:00
|
|
|
} else {
|
|
|
|
fd = open_file_or_dir(fname);
|
|
|
|
}
|
|
|
|
|
2007-08-07 20:15:59 +00:00
|
|
|
if (name)
|
2011-02-07 11:55:04 +00:00
|
|
|
strncpy(args.name, name, BTRFS_PATH_NAME_MAX + 1);
|
2007-08-07 20:15:59 +00:00
|
|
|
else
|
|
|
|
args.name[0] = '\0';
|
2008-11-18 16:26:35 +00:00
|
|
|
|
|
|
|
if (command == BTRFS_IOC_SNAP_CREATE) {
|
|
|
|
args.fd = fd;
|
|
|
|
ret = ioctl(snap_fd, command, &args);
|
2009-12-11 21:12:07 +00:00
|
|
|
} else if (command == BTRFS_IOC_DEFAULT_SUBVOL) {
|
|
|
|
printf("objectid is %llu\n", objectid);
|
|
|
|
ret = ioctl(fd, command, &objectid);
|
2008-11-18 16:26:35 +00:00
|
|
|
} else
|
|
|
|
ret = ioctl(fd, command, &args);
|
2008-06-10 02:23:13 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
perror("ioctl:");
|
|
|
|
exit(1);
|
|
|
|
}
|
2008-07-24 16:13:30 +00:00
|
|
|
if (ret == 0) {
|
|
|
|
printf("operation complete\n");
|
|
|
|
} else {
|
|
|
|
printf("ioctl failed with error %d\n", ret);
|
|
|
|
}
|
|
|
|
printf("%s\n", BTRFS_BUILD_VERSION);
|
|
|
|
if (ret)
|
|
|
|
exit(1);
|
2010-09-09 05:47:32 +00:00
|
|
|
|
|
|
|
return 0;
|
2007-04-10 13:27:30 +00:00
|
|
|
}
|
|
|
|
|