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
|
/*
* Utility for turning quotas on and off and reporting their state.
*/
#include "config.h"
/*
* Turn quota on/off for a filesystem.
*/
#include <stdio.h>
#include <errno.h>
#include <getopt.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include "quotaon.h"
#include "quota.h"
#include "quotasys.h"
#define FL_USER 1
#define FL_GROUP 2
#define FL_VERBOSE 4
#define FL_ALL 8
#define FL_STAT 16
#define FL_OFF 32
#define FL_PROJECT 64
static int flags, fmt = -1;
char *progname;
static char **mntpoints;
static int mntcnt;
static char *xarg = NULL;
static void usage(void)
{
errstr(_("Usage:\n\t%s [-guPvp] [-F quotaformat] [-x state] -a\n\
\t%s [-guPvp] [-F quotaformat] [-x state] filesys ...\n\n\
-a, --all %s\n\
-f, --off turn quotas off\n\
-u, --user operate on user quotas\n\
-g, --group operate on group quotas\n\
-P, --project operate on project quotas\n\
-p, --print-state print whether quotas are on or off\n\
-x, --xfs-command=cmd perform XFS quota command\n\
-F, --format=formatname operate on specific quota format\n\
-v, --verbose print more messages\n\
-h, --help display this help text and exit\n\
-V, --version display version information and exit\n"),
progname, progname,
strcmp(progname, "quotaon") ? _("turn quotas off for all filesystems") :
_("turn quotas on for all filesystems"));
exit(1);
}
static void parse_options(int argcnt, char **argstr)
{
int c;
struct option long_opts[] = {
{ "all", 0, NULL, 'a' },
{ "off", 0, NULL, 'f' },
{ "verbose", 0, NULL, 'v' },
{ "user", 0, NULL, 'u' },
{ "group", 0, NULL, 'g' },
{ "project", 0, NULL, 'P' },
{ "print-state", 0, NULL, 'p' },
{ "xfs-command", 1, NULL, 'x' },
{ "format", 1, NULL, 'F' },
{ "version", 0, NULL, 'V' },
{ "help", 0, NULL, 'h' },
{ NULL, 0, NULL, 0 }
};
while ((c = getopt_long(argcnt, argstr, "afvugpPx:VF:h", long_opts, NULL)) != -1) {
switch (c) {
case 'a':
flags |= FL_ALL;
break;
case 'f':
flags |= FL_OFF;
break;
case 'g':
flags |= FL_GROUP;
break;
case 'u':
flags |= FL_USER;
break;
case 'P':
flags |= FL_PROJECT;
break;
case 'v':
flags |= FL_VERBOSE;
break;
case 'x':
xarg = optarg;
break;
case 'p':
flags |= FL_STAT;
break;
case 'F':
if ((fmt = name2fmt(optarg)) == QF_ERROR)
exit(1);
break;
case 'V':
version();
exit(0);
case 'h':
default:
usage();
}
}
if ((flags & FL_ALL && optind != argcnt) || (!(flags & FL_ALL) && optind == argcnt)) {
fputs(_("Bad number of arguments.\n"), stderr);
usage();
}
if (fmt == QF_RPC) {
fputs(_("Cannot turn on/off quotas via RPC.\n"), stderr);
exit(1);
}
if (!(flags & (FL_USER | FL_GROUP | FL_PROJECT)))
flags |= FL_USER | FL_GROUP | FL_PROJECT;
if (!(flags & FL_ALL)) {
mntpoints = argstr + optind;
mntcnt = argcnt - optind;
}
}
int pinfo(char *fmt, ...)
{
va_list arg;
int ret;
if (!(flags & FL_VERBOSE))
return 0;
va_start(arg, fmt);
ret = vprintf(fmt, arg);
va_end(arg);
return ret;
}
/*
* Enable/disable rsquash on given filesystem
*/
static int quotarsquashonoff(const char *quotadev, int type, int flags)
{
#if defined(MNTOPT_RSQUASH)
int ret;
if (kernel_iface == IFACE_GENERIC) {
int qcmd = QCMD(Q_SETINFO, type);
struct if_dqinfo info;
info.dqi_flags = V1_DQF_RSQUASH;
info.dqi_valid = IIF_FLAGS;
ret = quotactl(qcmd, quotadev, 0, (void *)&info);
}
else {
int mode = (flags & STATEFLAG_OFF) ? 0 : 1;
int qcmd = QCMD(Q_V1_RSQUASH, type);
ret = quotactl(qcmd, quotadev, 0, (void *)&mode);
}
if (ret < 0) {
errstr(_("set root_squash on %s: %s\n"), quotadev, strerror(errno));
return 1;
}
if (flags & STATEFLAG_OFF)
pinfo(_("%s: %s root_squash turned off\n"), quotadev, type2name(type));
else if (flags & STATEFLAG_ON)
pinfo(_("%s: %s root_squash turned on\n"), quotadev, type2name(type));
#endif
return 0;
}
/*
* Enable/disable VFS quota on given filesystem
*/
static int quotaonoff(const char *quotadev, const char *quotadir, char *quotafile, int type, int fmt, int flags)
{
int qcmd, kqf;
if (flags & STATEFLAG_OFF) {
if (kernel_iface == IFACE_GENERIC)
qcmd = QCMD(Q_QUOTAOFF, type);
else
qcmd = QCMD(Q_6_5_QUOTAOFF, type);
if (quotactl(qcmd, quotadev, 0, NULL) < 0) {
errstr(_("quotactl on %s [%s]: %s\n"), quotadev, quotadir, strerror(errno));
return 1;
}
pinfo(_("%s [%s]: %s quotas turned off\n"), quotadev, quotadir, _(type2name(type)));
return 0;
}
if (kernel_iface == IFACE_GENERIC) {
qcmd = QCMD(Q_QUOTAON, type);
kqf = util2kernfmt(fmt);
}
else {
qcmd = QCMD(Q_6_5_QUOTAON, type);
kqf = 0;
}
if (quotactl(qcmd, quotadev, kqf, (void *)quotafile) < 0) {
if (errno == ENOENT)
errstr(_("cannot find %s on %s [%s]\n"), quotafile, quotadev, quotadir);
else
errstr(_("using %s on %s [%s]: %s\n"), quotafile, quotadev, quotadir, strerror(errno));
if (errno == EINVAL)
errstr(_("Maybe create new quota files with quotacheck(8)?\n"));
else if (errno == ESRCH)
errstr(_("Quota format not supported in kernel.\n"));
return 1;
}
pinfo(_("%s [%s]: %s quotas turned on\n"), quotadev, quotadir, _(type2name(type)));
return 0;
}
/*
* Enable/disable quota/rootsquash on given filesystem (version 1)
*/
static int v1_newstate(struct mount_entry *mnt, int type, char *file, int flags, int fmt)
{
int errs = 0;
if ((flags & STATEFLAG_OFF) && str_hasmntopt(mnt->me_opts, MNTOPT_RSQUASH))
errs += quotarsquashonoff(mnt->me_devname, type, flags);
errs += quotaonoff(mnt->me_devname, mnt->me_dir, file, type, QF_VFSOLD, flags);
if ((flags & STATEFLAG_ON) && str_hasmntopt(mnt->me_opts, MNTOPT_RSQUASH))
errs += quotarsquashonoff(mnt->me_devname, type, flags);
return errs;
}
/*
* Enable/disable quota on given filesystem (generic VFS quota)
*/
static int v2_newstate(struct mount_entry *mnt, int type, char *file, int flags, int fmt)
{
return quotaonoff(mnt->me_devname, mnt->me_dir, file, type, fmt, flags);
}
/*
* For both VFS quota formats, need to pass in the quota file;
* for XFS quota manager, pass on the -x command line option.
*/
static int newstate(struct mount_entry *mnt, int type, char *extra)
{
static int warned;
int sflags, ret = 0;
sflags = flags & FL_OFF ? STATEFLAG_OFF : STATEFLAG_ON;
if (flags & FL_ALL)
sflags |= STATEFLAG_ALL;
if (!strcmp(mnt->me_type, MNTTYPE_GFS2)) {
errstr(_("Cannot change state of GFS2 quota.\n"));
return 1;
} else if (!strcmp(mnt->me_type, MNTTYPE_XFS) ||
!strcmp(mnt->me_type, MNTTYPE_EXFS)) { /* XFS filesystem has special handling... */
if (!kern_qfmt_supp(QF_XFS)) {
errstr(_("Cannot change state of XFS quota. It's not compiled in kernel.\n"));
return 1;
}
ret = xfs_newstate(mnt, type, extra, sflags);
}
else if (mnt->me_qfmt[type] == QF_META) {
/* Must be non-empty because empty path is always invalid. */
ret = v2_newstate(mnt, type, ".", sflags, QF_VFSV0);
}
else {
int usefmt;
if (!me_hasquota(mnt, type))
return 0;
if (!warned && !strcmp(mnt->me_type, MNTTYPE_EXT4) &&
ext4_supports_quota_feature()) {
warned = 1;
errstr(_("Your kernel probably supports ext4 quota "
"feature but you are using external quota "
"files. Please switch your filesystem to use "
"ext4 quota feature as external quota files "
"on ext4 are deprecated. You can enable the "
"feature by unmounting the file system and "
"running 'tune2fs -O quota <device>'.\n"));
}
if (fmt == -1) {
if (get_qf_name(mnt, type, QF_VFSV0,
NF_FORMAT, &extra) >= 0)
usefmt = QF_VFSV0;
else if (get_qf_name(mnt, type, QF_VFSV1,
NF_FORMAT, &extra) >= 0)
usefmt = QF_VFSV1;
else if (get_qf_name(mnt, type, QF_VFSOLD,
NF_FORMAT, &extra) >= 0)
usefmt = QF_VFSOLD;
else {
errstr(_("Cannot find quota file on %s [%s] to turn quotas on/off.\n"), mnt->me_dir, mnt->me_devname);
return 1;
}
} else {
if (get_qf_name(mnt, type, fmt, NF_FORMAT, &extra) < 0) {
errstr(_("Quota file on %s [%s] does not exist or has wrong format.\n"), mnt->me_dir, mnt->me_devname);
return 1;
}
usefmt = fmt;
}
if (is_tree_qfmt(usefmt))
ret = v2_newstate(mnt, type, extra, sflags, usefmt);
else
ret = v1_newstate(mnt, type, extra, sflags, QF_VFSOLD);
free(extra);
}
return ret;
}
/* Print state of quota (on/off) */
static int print_state(struct mount_entry *mnt, int type)
{
int on = 0;
char *state;
if (kern_qfmt_supp(QF_XFS)) {
on = kern_quota_state_xfs(mnt->me_devname, type);
if (!strcmp(mnt->me_type, MNTTYPE_XFS) ||
!strcmp(mnt->me_type, MNTTYPE_GFS2) || on >= 0 ||
!strcmp(mnt->me_type, MNTTYPE_EXFS)) {
if (on < 0)
on = 0;
if (!(flags & FL_VERBOSE))
goto print_state;
if (on == 0)
state = _("off");
else if (on == 1)
state = _("on (accounting)");
else
state = _("on (enforced)");
goto print;
}
}
if (kernel_iface == IFACE_GENERIC)
on = kern_quota_on(mnt, type, -1) != -1;
else if (kern_qfmt_supp(QF_VFSV0))
on = kern_quota_on(mnt, type, QF_VFSV0) != -1;
else if (kern_qfmt_supp(QF_VFSOLD))
on = kern_quota_on(mnt, type, QF_VFSOLD) != -1;
print_state:
state = on ? _("on") : _("off");
print:
printf(_("%s quota on %s (%s) is %s\n"), _(type2name(type)),
mnt->me_dir, mnt->me_devname, state);
return on > 0;
}
int main(int argc, char **argv)
{
struct mount_entry *mnt;
int errs = 0;
gettexton();
progname = basename(argv[0]);
if (strcmp(progname, "quotaoff") == 0)
flags |= FL_OFF;
else if (strcmp(progname, "quotaon") != 0)
die(1, _("Name must be quotaon or quotaoff not %s\n"), progname);
parse_options(argc, argv);
init_kernel_interface();
if (fmt != -1 && !kern_qfmt_supp(fmt))
die(1, _("Required format %s not supported by kernel.\n"), fmt2name(fmt));
else if (!kern_qfmt_supp(-1))
errstr(_("Warning: No quota format detected in the kernel.\n"));
if (init_mounts_scan(mntcnt, mntpoints, MS_XFS_DISABLED | MS_LOCALONLY) < 0)
return 1;
while ((mnt = get_next_mount())) {
if (nfs_fstype(mnt->me_type)) {
if (!(flags & FL_ALL))
errstr(_("%s: Quota cannot be turned on on NFS filesystem\n"), mnt->me_devname);
continue;
}
if (!(flags & FL_STAT)) {
if (flags & FL_GROUP)
errs += newstate(mnt, GRPQUOTA, xarg);
if (flags & FL_USER)
errs += newstate(mnt, USRQUOTA, xarg);
if (flags & FL_PROJECT)
errs += newstate(mnt, PRJQUOTA, xarg);
}
else {
if (flags & FL_GROUP)
errs += print_state(mnt, GRPQUOTA);
if (flags & FL_USER)
errs += print_state(mnt, USRQUOTA);
if (flags & FL_PROJECT)
errs += print_state(mnt, PRJQUOTA);
}
}
end_mounts_scan();
return errs;
}
|