xref: /freebsd/usr.sbin/quotaon/quotaon.c (revision 0b8224d1cc9dc6c9778ba04a75b2c8d47e5d7481)
18a16b7a1SPedro F. Giffuni /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
4dea673e9SRodney W. Grimes  * Copyright (c) 1980, 1990, 1993
5dea673e9SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
6dea673e9SRodney W. Grimes  *
7dea673e9SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
8dea673e9SRodney W. Grimes  * Robert Elz at The University of Melbourne.
9dea673e9SRodney W. Grimes  *
10dea673e9SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
11dea673e9SRodney W. Grimes  * modification, are permitted provided that the following conditions
12dea673e9SRodney W. Grimes  * are met:
13dea673e9SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
14dea673e9SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
15dea673e9SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
16dea673e9SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
17dea673e9SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
19dea673e9SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
20dea673e9SRodney W. Grimes  *    without specific prior written permission.
21dea673e9SRodney W. Grimes  *
22dea673e9SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23dea673e9SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24dea673e9SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25dea673e9SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26dea673e9SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27dea673e9SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28dea673e9SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29dea673e9SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30dea673e9SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31dea673e9SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32dea673e9SRodney W. Grimes  * SUCH DAMAGE.
33dea673e9SRodney W. Grimes  */
34dea673e9SRodney W. Grimes 
35dea673e9SRodney W. Grimes /*
36dea673e9SRodney W. Grimes  * Turn quota on/off for a filesystem.
37dea673e9SRodney W. Grimes  */
38dea673e9SRodney W. Grimes #include <sys/param.h>
39dea673e9SRodney W. Grimes #include <sys/file.h>
40dea673e9SRodney W. Grimes #include <sys/mount.h>
41dea673e9SRodney W. Grimes #include <ufs/ufs/quota.h>
42607b525cSPhilippe Charnier #include <err.h>
43dea673e9SRodney W. Grimes #include <fstab.h>
44708a2897SKirk McKusick #include <libutil.h>
45607b525cSPhilippe Charnier #include <stdio.h>
462694efd4SDima Dorfman #include <stdlib.h>
47607b525cSPhilippe Charnier #include <string.h>
48607b525cSPhilippe Charnier #include <unistd.h>
49dea673e9SRodney W. Grimes 
50bf70beceSEd Schouten static const char *qfextension[] = INITQFNAMES;
51dea673e9SRodney W. Grimes 
52bf70beceSEd Schouten static int	aflag;		/* all filesystems */
53bf70beceSEd Schouten static int	gflag;		/* operate on group quotas */
54bf70beceSEd Schouten static int	uflag;		/* operate on user quotas */
55bf70beceSEd Schouten static int	vflag;		/* verbose */
56dea673e9SRodney W. Grimes 
57bf70beceSEd Schouten static int oneof(char *, char *[], int);
58bf70beceSEd Schouten static int quotaonoff(struct fstab *fs, int, int);
59*a2cc93ecSAlfonso Gregory static void usage(void) __dead2;
60607b525cSPhilippe Charnier 
61607b525cSPhilippe Charnier int
main(int argc,char ** argv)62d8c96595SAlfred Perlstein main(int argc, char **argv)
63dea673e9SRodney W. Grimes {
648f518424SDag-Erling Smørgrav 	struct fstab *fs;
65c70a327bSEd Schouten 	const char *whoami;
66dea673e9SRodney W. Grimes 	long argnum, done = 0;
675cfe0423SPeter Grehan 	int ch, i, offmode = 0, errs = 0;
68dea673e9SRodney W. Grimes 
69c70a327bSEd Schouten 	whoami = getprogname();
70dea673e9SRodney W. Grimes 	if (strcmp(whoami, "quotaoff") == 0)
71dea673e9SRodney W. Grimes 		offmode++;
72607b525cSPhilippe Charnier 	else if (strcmp(whoami, "quotaon") != 0)
73607b525cSPhilippe Charnier 		errx(1, "name must be quotaon or quotaoff");
746c3f552aSWarner Losh 	while ((ch = getopt(argc, argv, "avug")) != -1) {
75dea673e9SRodney W. Grimes 		switch(ch) {
76dea673e9SRodney W. Grimes 		case 'a':
77dea673e9SRodney W. Grimes 			aflag++;
78dea673e9SRodney W. Grimes 			break;
79dea673e9SRodney W. Grimes 		case 'g':
80dea673e9SRodney W. Grimes 			gflag++;
81dea673e9SRodney W. Grimes 			break;
82dea673e9SRodney W. Grimes 		case 'u':
83dea673e9SRodney W. Grimes 			uflag++;
84dea673e9SRodney W. Grimes 			break;
85dea673e9SRodney W. Grimes 		case 'v':
86dea673e9SRodney W. Grimes 			vflag++;
87dea673e9SRodney W. Grimes 			break;
88dea673e9SRodney W. Grimes 		default:
89607b525cSPhilippe Charnier 			usage();
90dea673e9SRodney W. Grimes 		}
91dea673e9SRodney W. Grimes 	}
92dea673e9SRodney W. Grimes 	argc -= optind;
93dea673e9SRodney W. Grimes 	argv += optind;
94dea673e9SRodney W. Grimes 	if (argc <= 0 && !aflag)
95607b525cSPhilippe Charnier 		usage();
96dea673e9SRodney W. Grimes 	if (!gflag && !uflag) {
97dea673e9SRodney W. Grimes 		gflag++;
98dea673e9SRodney W. Grimes 		uflag++;
99dea673e9SRodney W. Grimes 	}
100dea673e9SRodney W. Grimes 	setfsent();
101dea673e9SRodney W. Grimes 	while ((fs = getfsent()) != NULL) {
102dea673e9SRodney W. Grimes 		if (strcmp(fs->fs_vfstype, "ufs") ||
103dea673e9SRodney W. Grimes 		    strcmp(fs->fs_type, FSTAB_RW))
104dea673e9SRodney W. Grimes 			continue;
105dea673e9SRodney W. Grimes 		if (aflag) {
106708a2897SKirk McKusick 			if (gflag)
107708a2897SKirk McKusick 				errs += quotaonoff(fs, offmode, GRPQUOTA);
108708a2897SKirk McKusick 			if (uflag)
109708a2897SKirk McKusick 				errs += quotaonoff(fs, offmode, USRQUOTA);
110dea673e9SRodney W. Grimes 			continue;
111dea673e9SRodney W. Grimes 		}
112dea673e9SRodney W. Grimes 		if ((argnum = oneof(fs->fs_file, argv, argc)) >= 0 ||
113dea673e9SRodney W. Grimes 		    (argnum = oneof(fs->fs_spec, argv, argc)) >= 0) {
114dea673e9SRodney W. Grimes 			done |= 1 << argnum;
115708a2897SKirk McKusick 			if (gflag)
116708a2897SKirk McKusick 				errs += quotaonoff(fs, offmode, GRPQUOTA);
117708a2897SKirk McKusick 			if (uflag)
118708a2897SKirk McKusick 				errs += quotaonoff(fs, offmode, USRQUOTA);
119dea673e9SRodney W. Grimes 		}
120dea673e9SRodney W. Grimes 	}
121dea673e9SRodney W. Grimes 	endfsent();
122dea673e9SRodney W. Grimes 	for (i = 0; i < argc; i++)
123dea673e9SRodney W. Grimes 		if ((done & (1 << i)) == 0)
124607b525cSPhilippe Charnier 			warnx("%s not found in fstab", argv[i]);
125dea673e9SRodney W. Grimes 	exit(errs);
126dea673e9SRodney W. Grimes }
127dea673e9SRodney W. Grimes 
128607b525cSPhilippe Charnier static void
usage(void)1298f518424SDag-Erling Smørgrav usage(void)
130dea673e9SRodney W. Grimes {
131dea673e9SRodney W. Grimes 
132607b525cSPhilippe Charnier 	fprintf(stderr, "%s\n%s\n%s\n%s\n",
133607b525cSPhilippe Charnier 		"usage: quotaon [-g] [-u] [-v] -a",
134607b525cSPhilippe Charnier 		"       quotaon [-g] [-u] [-v] filesystem ...",
135607b525cSPhilippe Charnier 		"       quotaoff [-g] [-u] [-v] -a",
136607b525cSPhilippe Charnier 		"       quotaoff [-g] [-u] [-v] filesystem ...");
137dea673e9SRodney W. Grimes 	exit(1);
138dea673e9SRodney W. Grimes }
139dea673e9SRodney W. Grimes 
140bf70beceSEd Schouten static int
quotaonoff(struct fstab * fs,int offmode,int type)1411a0fda2bSDag-Erling Smørgrav quotaonoff(struct fstab *fs, int offmode, int type)
142dea673e9SRodney W. Grimes {
143708a2897SKirk McKusick 	struct quotafile *qf;
144dea673e9SRodney W. Grimes 
145708a2897SKirk McKusick 	if ((qf = quota_open(fs, type, O_RDONLY)) == NULL)
146708a2897SKirk McKusick 		return (0);
147dea673e9SRodney W. Grimes 	if (offmode) {
148708a2897SKirk McKusick 		if (quota_off(qf) != 0) {
149708a2897SKirk McKusick 			warn("%s", quota_fsname(qf));
150dea673e9SRodney W. Grimes 			return (1);
151dea673e9SRodney W. Grimes 		}
152dea673e9SRodney W. Grimes 		if (vflag)
153708a2897SKirk McKusick 			printf("%s: quotas turned off\n", quota_fsname(qf));
154708a2897SKirk McKusick 		quota_close(qf);
155dea673e9SRodney W. Grimes 		return(0);
156dea673e9SRodney W. Grimes 	}
157708a2897SKirk McKusick 	if (quota_on(qf) != 0) {
158708a2897SKirk McKusick 		warn("using %s on %s", quota_qfname(qf), quota_fsname(qf));
159dea673e9SRodney W. Grimes 		return (1);
160dea673e9SRodney W. Grimes 	}
161dea673e9SRodney W. Grimes 	if (vflag)
162fd7b7762SMike Pritchard 		printf("%s: %s quotas turned on with data file %s\n",
163708a2897SKirk McKusick 		    quota_fsname(qf), qfextension[type], quota_qfname(qf));
164708a2897SKirk McKusick 	quota_close(qf);
165dea673e9SRodney W. Grimes 	return(0);
166dea673e9SRodney W. Grimes }
167dea673e9SRodney W. Grimes 
168dea673e9SRodney W. Grimes /*
169dea673e9SRodney W. Grimes  * Check to see if target appears in list of size cnt.
170dea673e9SRodney W. Grimes  */
171bf70beceSEd Schouten static int
oneof(char * target,char * list[],int cnt)1728f518424SDag-Erling Smørgrav oneof(char *target, char *list[], int cnt)
173dea673e9SRodney W. Grimes {
1748f518424SDag-Erling Smørgrav 	int i;
175dea673e9SRodney W. Grimes 
176dea673e9SRodney W. Grimes 	for (i = 0; i < cnt; i++)
177dea673e9SRodney W. Grimes 		if (strcmp(target, list[i]) == 0)
178dea673e9SRodney W. Grimes 			return (i);
179dea673e9SRodney W. Grimes 	return (-1);
180dea673e9SRodney W. Grimes }
181