Merge branch 'for-chris' of git://repo.or.cz/btrfs-progs-unstable/devel into raid56
[btrfs-progs-unstable/devel.git] / cmds-quota.c
blob8481514cb2b9449ad13291ada50106741f45074e
1 /*
2 * Copyright (C) 2012 STRATO. 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 #include <sys/ioctl.h>
20 #include <unistd.h>
22 #include "ctree.h"
23 #include "ioctl.h"
25 #include "commands.h"
26 #include "utils.h"
28 static const char * const quota_cmd_group_usage[] = {
29 "btrfs quota <command> [options] <path>",
30 NULL
33 int quota_ctl(int cmd, int argc, char **argv)
35 int ret = 0;
36 int fd;
37 int e;
38 char *path = argv[1];
39 struct btrfs_ioctl_quota_ctl_args args;
41 if (check_argc_exact(argc, 2))
42 return -1;
44 memset(&args, 0, sizeof(args));
45 args.cmd = cmd;
47 fd = open_file_or_dir(path);
48 if (fd < 0) {
49 fprintf(stderr, "ERROR: can't access '%s'\n", path);
50 return 12;
53 ret = ioctl(fd, BTRFS_IOC_QUOTA_CTL, &args);
54 e = errno;
55 close(fd);
56 if (ret < 0) {
57 fprintf(stderr, "ERROR: quota command failed: %s\n",
58 strerror(e));
59 return 30;
61 return 0;
64 static const char * const cmd_quota_enable_usage[] = {
65 "btrfs quota enable <path>",
66 "Enable subvolume quota support for a filesystem.",
67 NULL
70 static int cmd_quota_enable(int argc, char **argv)
72 int ret = quota_ctl(BTRFS_QUOTA_CTL_ENABLE, argc, argv);
73 if (ret < 0)
74 usage(cmd_quota_enable_usage);
75 return ret;
78 static const char * const cmd_quota_disable_usage[] = {
79 "btrfs quota disable <path>",
80 "Disable subvolume quota support for a filesystem.",
81 NULL
84 static int cmd_quota_disable(int argc, char **argv)
86 int ret = quota_ctl(BTRFS_QUOTA_CTL_DISABLE, argc, argv);
87 if (ret < 0)
88 usage(cmd_quota_disable_usage);
89 return ret;
92 static const char * const cmd_quota_rescan_usage[] = {
93 "btrfs quota rescan <path>",
94 "Rescan the subvolume for a changed quota setting.",
95 NULL
98 static int cmd_quota_rescan(int argc, char **argv)
100 int ret = quota_ctl(BTRFS_QUOTA_CTL_RESCAN, argc, argv);
101 if (ret < 0)
102 usage(cmd_quota_rescan_usage);
103 return ret;
106 const struct cmd_group quota_cmd_group = {
107 quota_cmd_group_usage, NULL, {
108 { "enable", cmd_quota_enable, cmd_quota_enable_usage, NULL, 0 },
109 { "disable", cmd_quota_disable, cmd_quota_disable_usage, 0, 0 },
110 { "rescan", cmd_quota_rescan, cmd_quota_rescan_usage, NULL, 0 },
111 { 0, 0, 0, 0, 0 }
115 int cmd_quota(int argc, char **argv)
117 return handle_command_group(&quota_cmd_group, argc, argv);