File: quota.c

package info (click to toggle)
btrfs-progs 6.17.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,612 kB
  • sloc: ansic: 127,282; sh: 7,915; python: 1,384; makefile: 900; asm: 296
file content (426 lines) | stat: -rw-r--r-- 9,960 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/*
 * Copyright (C) 2012 STRATO.  All rights reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License v2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 021110-1307, USA.
 */

#include "kerncompat.h"
#include <sys/ioctl.h>
#include <dirent.h>
#include <errno.h>
#include <ctype.h>
#include <getopt.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <unistd.h>
#include "kernel-shared/uapi/btrfs.h"
#include "kernel-shared/uapi/btrfs_tree.h"
#include "common/help.h"
#include "common/open-utils.h"
#include "common/messages.h"
#include "common/sysfs-utils.h"
#include "common/parse-utils.h"
#include "common/string-utils.h"
#include "cmds/commands.h"

static const char * const quota_cmd_group_usage[] = {
	"btrfs quota <command> [options] <path>",
	NULL
};

static int quota_ctl(int cmd, char *path)
{
	int ret = 0;
	int fd;
	struct btrfs_ioctl_quota_ctl_args args;

	memset(&args, 0, sizeof(args));
	args.cmd = cmd;

	fd = btrfs_open_dir(path);
	if (fd < 0)
		return 1;

	ret = ioctl(fd, BTRFS_IOC_QUOTA_CTL, &args);
	close(fd);
	if (ret < 0) {
		error("quota command failed: %m");
		return 1;
	}
	return 0;
}

static const char * const cmd_quota_enable_usage[] = {
	"btrfs quota enable <path>",
	"Enable subvolume quota support for a filesystem.",
	"Any data already present on the filesystem will not count towards",
	"the space usage numbers. It is recommended to enable quota for a",
	"filesystem before writing any data to it.",
	"",
	"-s|--simple	simple qgroups account ownership by extent lifetime rather than backref walks",
	NULL
};

static int cmd_quota_enable(const struct cmd_struct *cmd, int argc, char **argv)
{
	int ret;
	int ctl_cmd = BTRFS_QUOTA_CTL_ENABLE;

	optind = 0;
	while (1) {
		static const struct option long_options[] = {
			{"simple", no_argument, NULL, 's'},
			{NULL, 0, NULL, 0}
		};
		int c;

		c = getopt_long(argc, argv, "s", long_options, NULL);
		if (c < 0)
			break;

		switch (c) {
		case 's':
			ctl_cmd = BTRFS_QUOTA_CTL_ENABLE_SIMPLE_QUOTA;
			break;
		default:
			usage_unknown_option(cmd, argv);
		}
	}
	if (check_argc_exact(argc - optind, 1))
		return -1;

	ret = quota_ctl(ctl_cmd, argv[optind]);
	if (ret < 0)
		usage(cmd, 1);
	return ret;
}
static DEFINE_SIMPLE_COMMAND(quota_enable, "enable");

static const char * const cmd_quota_disable_usage[] = {
	"btrfs quota disable <path>",
	"Disable subvolume quota support for a filesystem.",
	NULL
};

static int cmd_quota_disable(const struct cmd_struct *cmd,
			     int argc, char **argv)
{
	int ret;

	clean_args_no_options(cmd, argc, argv);

	if (check_argc_exact(argc, 2))
		return -1;

	ret = quota_ctl(BTRFS_QUOTA_CTL_DISABLE, argv[1]);
	if (ret < 0)
		usage(cmd, 1);
	return ret;
}
static DEFINE_SIMPLE_COMMAND(quota_disable, "disable");

static const char * const cmd_quota_rescan_usage[] = {
	"btrfs quota rescan [-sw] <path>",
	"Trash all qgroup numbers and scan the metadata again with the current config.",
	"",
	OPTLINE("-s|--status", "show status of a running rescan operation"),
	OPTLINE("-w|--wait", "start rescan and wait for it to finish (can be already in progress)"),
	OPTLINE("-W|--wait-norescan", "wait for rescan to finish without starting it"),
	HELPINFO_INSERT_GLOBALS,
	HELPINFO_INSERT_QUIET,
	NULL
};

static int cmd_quota_rescan(const struct cmd_struct *cmd, int argc, char **argv)
{
	int ret = 0;
	int fd;
	int e;
	char *path = NULL;
	struct btrfs_ioctl_quota_rescan_args args;
	unsigned long ioctlnum = BTRFS_IOC_QUOTA_RESCAN;
	bool wait_for_completion = false;

	optind = 0;
	while (1) {
		static const struct option long_options[] = {
			{"status", no_argument, NULL, 's'},
			{"wait", no_argument, NULL, 'w'},
			{"wait-norescan", no_argument, NULL, 'W'},
			{NULL, 0, NULL, 0}
		};
		int c;

		c = getopt_long(argc, argv, "swW", long_options, NULL);
		if (c < 0)
			break;

		switch (c) {
		case 's':
			ioctlnum = BTRFS_IOC_QUOTA_RESCAN_STATUS;
			break;
		case 'w':
			ioctlnum = BTRFS_IOC_QUOTA_RESCAN;
			wait_for_completion = true;
			break;
		case 'W':
			ioctlnum = 0;
			wait_for_completion = 1;
			break;
		default:
			usage_unknown_option(cmd, argv);
		}
	}

	if (ioctlnum == BTRFS_IOC_QUOTA_RESCAN_STATUS && wait_for_completion) {
		error("switch -w cannot be used with -s");
		return 1;
	}

	if (check_argc_exact(argc - optind, 1))
		return 1;

	memset(&args, 0, sizeof(args));

	path = argv[optind];
	fd = btrfs_open_dir(path);
	if (fd < 0)
		return 1;

	if (ioctlnum) {
		ret = ioctl(fd, ioctlnum, &args);
		e = errno;
	}

	if (ioctlnum == BTRFS_IOC_QUOTA_RESCAN_STATUS) {
		close(fd);
		if (ret < 0) {
			error("could not obtain quota rescan status: %m");
			return 1;
		}
		if (!args.flags)
			pr_verbose(LOG_DEFAULT, "no rescan operation in progress\n");
		else
			pr_verbose(LOG_DEFAULT, "rescan operation running (current key %lld)\n",
				args.progress);
		return 0;
	}

	if (ioctlnum == BTRFS_IOC_QUOTA_RESCAN && ret == 0) {
		pr_verbose(LOG_DEFAULT, "quota rescan started\n");
		fflush(stdout);
	} else if (ret < 0 && (!wait_for_completion || e != EINPROGRESS)) {
		error("quota rescan failed: %m");
		close(fd);
		return 1;
	}

	if (wait_for_completion) {
		ret = ioctl(fd, BTRFS_IOC_QUOTA_RESCAN_WAIT, &args);
		e = errno;
		if (ret < 0) {
			error("quota rescan wait failed: %m");
			close(fd);
			return 1;
		}
	}

	close(fd);
	return 0;
}
static DEFINE_SIMPLE_COMMAND(quota_rescan, "rescan");

static const char * const cmd_quota_status_usage[] = {
	"btrfs quota status <path>",
	"Show status information about quota if enabled on the <path>.",
	"",
	OPTLINE("--is-enabled", "only check if quotas are enabled, not not print anything"),
	NULL
};

static bool quota_is_enabled(const char *path)
{
	int fsfd = -1;
	int dirfd = -1;
	bool ret = true;

	fsfd = btrfs_open_dir(path);
	if (fsfd < 0)
		return false;

	dirfd = sysfs_open_fsid_dir(fsfd, "qgroups");
	if (dirfd < 0) {
		ret = false;
		goto out;
	}

out:
	close(fsfd);
	close(dirfd);
	return ret;
}

static const char *describe_mode(const char *mode)
{
	if (strcmp("qgroup", mode) == 0)
		return "full accounting";
	if (strcmp("squota", mode) == 0)
		return "simplified accounting";
	return "unknown mode";
}

static int cmd_quota_status(const struct cmd_struct *cmd, int argc, char **argv)
{
	int ret;
	int fsfd = -1;
	int dirfd = -1;
	int fd = -1;
	char buf[4096] = { 0 };
	u64 num, num2;
	DIR *dir = NULL;

	optind = 0;
	while (1) {
		int c;
		enum { GETOPT_VAL_IS_ENABLED = GETOPT_VAL_FIRST };
		static const struct option long_options[] = {
			{ "is-enabled", no_argument, NULL, GETOPT_VAL_IS_ENABLED },
			{ NULL, 0, NULL, 0}
		};

		c = getopt_long(argc, argv, "", long_options, NULL);
		if (c < 0)
			break;
		switch (c) {
		case GETOPT_VAL_IS_ENABLED:
			return quota_is_enabled(argv[optind]) ? 0 : 1;
		default:
			usage_unknown_option(cmd, argv);
		}
	}

	if (check_argc_exact(argc - optind, 1))
		return 1;

	fsfd = btrfs_open_dir(argv[1]);
	if (fsfd < 0)
		return 1;

	dirfd = sysfs_open_fsid_dir(fsfd, "qgroups");
	pr_verbose(LOG_DEFAULT, "Quotas on %s:\n", argv[1]);
	if (dirfd < 0) {
		pr_verbose(LOG_DEFAULT, "  Enabled: no\n");
		goto out;
	}
	pr_verbose(LOG_DEFAULT, "  Enabled:                 %s\n", "yes");

	fd = sysfs_open_fsid_file(fsfd, "qgroups/mode");
	if (fd < 0) {
		error("cannot open file qgroups/mode: %m");
		goto out;
	}
	ret = sysfs_read_file(fd, buf, sizeof(buf));
	if (fd < 0) {
		error("cannot read file qgroups/mode: %m");
		goto out;
	}
	while (isspace(buf[strlen(buf) - 1]))
		buf[strlen(buf) - 1] = 0;
	pr_verbose(LOG_DEFAULT, "  Mode:                    %s (%s)\n", buf, describe_mode(buf));
	close(fd);

	ret = sysfs_read_fsid_file_u64(fsfd, "qgroups/inconsistent", &num);
	if (ret < 0) {
		error("cannot read file qgroups/inconsistent: %m");
		goto out;
	}
	pr_verbose(LOG_DEFAULT, "  Inconsistent:            %s%s\n",
		   (num ? "yes" : "no"), (num ? " (rescan needed)" : ""));

	ret = sysfs_read_fsid_file_u64(fsfd, "quota_override", &num);
	if (ret < 0) {
		error("cannot read file qgroups/quota_override: %m");
		goto out;
	}
	pr_verbose(LOG_DEFAULT, "  Override limits:         %s\n", (num ? "yes" : "no"));

	ret = sysfs_read_fsid_file_u64(fsfd, "qgroups/drop_subtree_threshold", &num);
	if (ret < 0) {
		error("cannot read file qgroups/drop_subtree_threshold");
		goto out;
	}
	pr_verbose(LOG_DEFAULT, "  Drop subtree threshold:  %llu\n", num);

	/* Count */
	dir = fdopendir(dirfd);
	if (!dir) {
		error("cannot open qgroups/ directory: %m");
		goto out;
	}
	num = 0;
	num2 = 0;
	while (1) {
		struct dirent *de;
		u64 qgroupid;
		char *str;

		de = readdir(dir);
		if (!de)
			break;

		str = de->d_name;
		while (*str) {
			if (*str == '_') {
				*str = '/';
				break;
			}
			str++;
		}

		ret = parse_qgroupid(de->d_name, &qgroupid);
		if (ret < 0)
			continue;

		num++;
		if (btrfs_qgroup_level(qgroupid) == 0)
			num2++;
	}
	pr_verbose(LOG_DEFAULT, "  Total count:             %llu\n", num);
	pr_verbose(LOG_DEFAULT, "  Level 0:                 %llu\n", num2);

out:
	if (dir)
		closedir(dir);
	close(dirfd);
	close(fsfd);
	return 0;
}
static DEFINE_SIMPLE_COMMAND(quota_status, "status");

static const char quota_cmd_group_info[] =
"manage filesystem quota settings";

static const struct cmd_group quota_cmd_group = {
	quota_cmd_group_usage, quota_cmd_group_info, {
		&cmd_struct_quota_enable,
		&cmd_struct_quota_disable,
		&cmd_struct_quota_rescan,
		&cmd_struct_quota_status,
		NULL
	}
};

DEFINE_GROUP_COMMAND_TOKEN(quota);