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