btrfs-progs: cmd quota: switch to common error message wrapper
[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 static 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         DIR *dirstream = NULL;
41
42         if (check_argc_exact(argc, 2))
43                 return -1;
44
45         memset(&args, 0, sizeof(args));
46         args.cmd = cmd;
47
48         fd = btrfs_open_dir(path, &dirstream, 1);
49         if (fd < 0)
50                 return 1;
51
52         ret = ioctl(fd, BTRFS_IOC_QUOTA_CTL, &args);
53         e = errno;
54         close_file_or_dir(fd, dirstream);
55         if (ret < 0) {
56                 error("quota command failed: %s", strerror(e));
57                 return 1;
58         }
59         return 0;
60 }
61
62 static const char * const cmd_quota_enable_usage[] = {
63         "btrfs quota enable <path>",
64         "Enable subvolume quota support for a filesystem.",
65         "Any data already present on the filesystem will not count towards",
66         "the space usage numbers. It is recommended to enable quota for a",
67         "filesystem before writing any data to it.",
68         NULL
69 };
70
71 static int cmd_quota_enable(int argc, char **argv)
72 {
73         int ret = quota_ctl(BTRFS_QUOTA_CTL_ENABLE, argc, argv);
74         if (ret < 0)
75                 usage(cmd_quota_enable_usage);
76         return ret;
77 }
78
79 static const char * const cmd_quota_disable_usage[] = {
80         "btrfs quota disable <path>",
81         "Disable subvolume quota support for a filesystem.",
82         NULL
83 };
84
85 static int cmd_quota_disable(int argc, char **argv)
86 {
87         int ret = quota_ctl(BTRFS_QUOTA_CTL_DISABLE, argc, argv);
88         if (ret < 0)
89                 usage(cmd_quota_disable_usage);
90         return ret;
91 }
92
93 static const char * const cmd_quota_rescan_usage[] = {
94         "btrfs quota rescan [-sw] <path>",
95         "Trash all qgroup numbers and scan the metadata again with the current config.",
96         "",
97         "-s   show status of a running rescan operation",
98         "-w   wait for rescan operation to finish (can be already in progress)",
99         NULL
100 };
101
102 static int cmd_quota_rescan(int argc, char **argv)
103 {
104         int ret = 0;
105         int fd;
106         int e;
107         char *path = NULL;
108         struct btrfs_ioctl_quota_rescan_args args;
109         unsigned long ioctlnum = BTRFS_IOC_QUOTA_RESCAN;
110         DIR *dirstream = NULL;
111         int wait_for_completion = 0;
112
113         optind = 1;
114         while (1) {
115                 int c = getopt(argc, argv, "sw");
116                 if (c < 0)
117                         break;
118                 switch (c) {
119                 case 's':
120                         ioctlnum = BTRFS_IOC_QUOTA_RESCAN_STATUS;
121                         break;
122                 case 'w':
123                         wait_for_completion = 1;
124                         break;
125                 default:
126                         usage(cmd_quota_rescan_usage);
127                 }
128         }
129
130         if (ioctlnum != BTRFS_IOC_QUOTA_RESCAN && wait_for_completion) {
131                 error("switch -w cannot be used with -s");
132                 return 1;
133         }
134
135         if (check_argc_exact(argc - optind, 1))
136                 usage(cmd_quota_rescan_usage);
137
138         memset(&args, 0, sizeof(args));
139
140         path = argv[optind];
141         fd = btrfs_open_dir(path, &dirstream, 1);
142         if (fd < 0)
143                 return 1;
144
145         ret = ioctl(fd, ioctlnum, &args);
146         e = errno;
147
148         if (wait_for_completion && (ret == 0 || e == EINPROGRESS)) {
149                 ret = ioctl(fd, BTRFS_IOC_QUOTA_RESCAN_WAIT, &args);
150                 e = errno;
151         }
152         close_file_or_dir(fd, dirstream);
153
154         if (ioctlnum == BTRFS_IOC_QUOTA_RESCAN) {
155                 if (ret < 0) {
156                         error("quota rescan failed: %s", strerror(e));
157                         return 1;
158                 }  else {
159                         printf("quota rescan started\n");
160                 }
161         } else {
162                 if (!args.flags) {
163                         printf("no rescan operation in progress\n");
164                 } else {
165                         printf("rescan operation running (current key %lld)\n",
166                                 args.progress);
167                 }
168         }
169
170         return 0;
171 }
172
173 static const char quota_cmd_group_info[] =
174 "manage filesystem quota settings";
175
176 const struct cmd_group quota_cmd_group = {
177         quota_cmd_group_usage, quota_cmd_group_info, {
178                 { "enable", cmd_quota_enable, cmd_quota_enable_usage, NULL, 0 },
179                 { "disable", cmd_quota_disable, cmd_quota_disable_usage,
180                    NULL, 0 },
181                 { "rescan", cmd_quota_rescan, cmd_quota_rescan_usage, NULL, 0 },
182                 NULL_CMD_STRUCT
183         }
184 };
185
186 int cmd_quota(int argc, char **argv)
187 {
188         return handle_command_group(&quota_cmd_group, argc, argv);
189 }