Btrfs-progs: fix wrong leaf when checking the trees relationship
[platform/upstream/btrfs-progs.git] / cmds-quota.c
1 /*
2  * Copyright (C) 2012 STRATO.  All rights reserved.
3  *
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.
7  *
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.
12  *
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.
17  */
18
19 #include <sys/ioctl.h>
20 #include <unistd.h>
21
22 #include "ctree.h"
23 #include "ioctl.h"
24
25 #include "commands.h"
26
27 static const char * const quota_cmd_group_usage[] = {
28         "btrfs quota <command> [options] <path>",
29         NULL
30 };
31
32 int quota_ctl(int cmd, int argc, char **argv)
33 {
34         int ret = 0;
35         int fd;
36         int e;
37         char *path = argv[1];
38         struct btrfs_ioctl_quota_ctl_args args;
39
40         if (check_argc_exact(argc, 2))
41                 return -1;
42
43         memset(&args, 0, sizeof(args));
44         args.cmd = cmd;
45
46         fd = open_file_or_dir(path);
47         if (fd < 0) {
48                 fprintf(stderr, "ERROR: can't access '%s'\n", path);
49                 return 12;
50         }
51
52         ret = ioctl(fd, BTRFS_IOC_QUOTA_CTL, &args);
53         e = errno;
54         close(fd);
55         if (ret < 0) {
56                 fprintf(stderr, "ERROR: quota command failed: %s\n",
57                         strerror(e));
58                 return 30;
59         }
60         return 0;
61 }
62
63 static const char * const cmd_quota_enable_usage[] = {
64         "btrfs quota enable <path>",
65         "Enable subvolume quota support for a filesystem.",
66         NULL
67 };
68
69 static int cmd_quota_enable(int argc, char **argv)
70 {
71         int ret = quota_ctl(BTRFS_QUOTA_CTL_ENABLE, argc, argv);
72         if (ret < 0)
73                 usage(cmd_quota_enable_usage);
74         return ret;
75 }
76
77 static const char * const cmd_quota_disable_usage[] = {
78         "btrfs quota disable <path>",
79         "Disable subvolume quota support for a filesystem.",
80         NULL
81 };
82
83 static int cmd_quota_disable(int argc, char **argv)
84 {
85         int ret = quota_ctl(BTRFS_QUOTA_CTL_DISABLE, argc, argv);
86         if (ret < 0)
87                 usage(cmd_quota_disable_usage);
88         return ret;
89 }
90
91 static const char * const cmd_quota_rescan_usage[] = {
92         "btrfs quota rescan <path>",
93         "Rescan the subvolume for a changed quota setting.",
94         NULL
95 };
96
97 static int cmd_quota_rescan(int argc, char **argv)
98 {
99         int ret = quota_ctl(BTRFS_QUOTA_CTL_RESCAN, argc, argv);
100         if (ret < 0)
101                 usage(cmd_quota_rescan_usage);
102         return ret;
103 }
104
105 const struct cmd_group quota_cmd_group = {
106         quota_cmd_group_usage, NULL, {
107                 { "enable", cmd_quota_enable, cmd_quota_enable_usage, NULL, 0 },
108                 { "disable", cmd_quota_disable, cmd_quota_disable_usage, 0, 0 },
109                 { "rescan", cmd_quota_rescan, cmd_quota_rescan_usage, NULL, 0 },
110                 { 0, 0, 0, 0, 0 }
111         }
112 };
113
114 int cmd_quota(int argc, char **argv)
115 {
116         return handle_command_group(&quota_cmd_group, argc, argv);
117 }