Btrfs-progs: fixup: add flags to struct btrfs_ioctl_quota_rescan_args
[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 #include "utils.h"
27
28 static const char * const quota_cmd_group_usage[] = {
29         "btrfs quota <command> [options] <path>",
30         NULL
31 };
32
33 int quota_ctl(int cmd, int argc, char **argv)
34 {
35         int ret = 0;
36         int fd;
37         int e;
38         char *path = argv[1];
39         struct btrfs_ioctl_quota_ctl_args args;
40
41         if (check_argc_exact(argc, 2))
42                 return -1;
43
44         memset(&args, 0, sizeof(args));
45         args.cmd = cmd;
46
47         fd = open_file_or_dir(path);
48         if (fd < 0) {
49                 fprintf(stderr, "ERROR: can't access '%s'\n", path);
50                 return 12;
51         }
52
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;
60         }
61         return 0;
62 }
63
64 static const char * const cmd_quota_enable_usage[] = {
65         "btrfs quota enable <path>",
66         "Enable subvolume quota support for a filesystem.",
67         "Any data already present on the filesystem will not count towards",
68         "the space usage numbers. It is recommended to enable quota for a",
69         "filesystem before writing any data to it.",
70         NULL
71 };
72
73 static int cmd_quota_enable(int argc, char **argv)
74 {
75         int ret = quota_ctl(BTRFS_QUOTA_CTL_ENABLE, argc, argv);
76         if (ret < 0)
77                 usage(cmd_quota_enable_usage);
78         return ret;
79 }
80
81 static const char * const cmd_quota_disable_usage[] = {
82         "btrfs quota disable <path>",
83         "Disable subvolume quota support for a filesystem.",
84         NULL
85 };
86
87 static int cmd_quota_disable(int argc, char **argv)
88 {
89         int ret = quota_ctl(BTRFS_QUOTA_CTL_DISABLE, argc, argv);
90         if (ret < 0)
91                 usage(cmd_quota_disable_usage);
92         return ret;
93 }
94
95 static const char * const cmd_quota_rescan_usage[] = {
96         "btrfs quota rescan [-s] <path>",
97         "Trash all qgroup numbers and scan the metadata again with the current config.",
98         "",
99         "-s   show status of a running rescan operation",
100         NULL
101 };
102
103 static int cmd_quota_rescan(int argc, char **argv)
104 {
105         int ret = 0;
106         int fd;
107         int e;
108         char *path = NULL;
109         struct btrfs_ioctl_quota_rescan_args args;
110         int ioctlnum = BTRFS_IOC_QUOTA_RESCAN;
111
112         optind = 1;
113         while (1) {
114                 int c = getopt(argc, argv, "s");
115                 if (c < 0)
116                         break;
117                 switch (c) {
118                 case 's':
119                         ioctlnum = BTRFS_IOC_QUOTA_RESCAN_STATUS;
120                         break;
121                 default:
122                         usage(cmd_quota_rescan_usage);
123                 }
124         }
125
126         if (check_argc_exact(argc - optind, 1))
127                 usage(cmd_quota_rescan_usage);
128
129         memset(&args, 0, sizeof(args));
130
131         path = argv[optind];
132         fd = open_file_or_dir(path);
133         if (fd < 0) {
134                 fprintf(stderr, "ERROR: can't access '%s'\n", path);
135                 return 12;
136         }
137
138         ret = ioctl(fd, ioctlnum, &args);
139         e = errno;
140         close(fd);
141
142         if (ioctlnum == BTRFS_IOC_QUOTA_RESCAN) {
143                 if (ret < 0) {
144                         fprintf(stderr, "ERROR: quota rescan failed: "
145                                 "%s\n", strerror(e));
146                         return 30;
147                 }  else {
148                         printf("quota rescan started\n");
149                 }
150         } else {
151                 if (!args.flags) {
152                         printf("no rescan operation in progress\n");
153                 } else {
154                         printf("rescan operation running (current key %lld)\n",
155                                 args.progress);
156                 }
157         }
158
159         return 0;
160 }
161
162 const struct cmd_group quota_cmd_group = {
163         quota_cmd_group_usage, NULL, {
164                 { "enable", cmd_quota_enable, cmd_quota_enable_usage, NULL, 0 },
165                 { "disable", cmd_quota_disable, cmd_quota_disable_usage, 0, 0 },
166                 { "rescan", cmd_quota_rescan, cmd_quota_rescan_usage, NULL, 0 },
167                 { 0, 0, 0, 0, 0 }
168         }
169 };
170
171 int cmd_quota(int argc, char **argv)
172 {
173         return handle_command_group(&quota_cmd_group, argc, argv);
174 }