xref: /dragonfly/usr.sbin/quotaon/quotaon.c (revision dadd6466)
1 /*
2  * Copyright (c) 1980, 1990, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Robert Elz at The University of Melbourne.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * @(#) Copyright (c) 1980, 1990, 1993 The Regents of the University of California.  All rights reserved.
33  * @(#)quotaon.c	8.1 (Berkeley) 6/6/93
34  * $FreeBSD: src/usr.sbin/quotaon/quotaon.c,v 1.4.2.1 2001/07/19 05:17:06 kris Exp $
35  * $DragonFly: src/usr.sbin/quotaon/quotaon.c,v 1.8 2005/03/18 00:52:49 joerg Exp $
36  */
37 
38 /*
39  * Turn quota on/off for a filesystem.
40  */
41 #include <sys/param.h>
42 #include <sys/file.h>
43 #include <sys/mount.h>
44 #include <vfs/ufs/quota.h>
45 #include <err.h>
46 #include <fstab.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <unistd.h>
51 
52 const char *qfname = QUOTAFILENAME;
53 const char *qfextension[] = INITQFNAMES;
54 
55 int	aflag;		/* all file systems */
56 int	gflag;		/* operate on group quotas */
57 int	uflag;		/* operate on user quotas */
58 int	vflag;		/* verbose */
59 
60 int hasquota(struct fstab *, int, char **);
61 int oneof(char *, char *[], int);
62 int quotaonoff(struct fstab *fs, int, int, char *);
63 int readonly(struct fstab *);
64 static void usage(void);
65 
66 int
67 main(int argc, char **argv)
68 {
69 	struct fstab *fs;
70 	char ch, *qfnp, *whoami;
71 	long argnum, done = 0;
72 	int i, offmode = 0, errs = 0;
73 
74 	whoami = strrchr(*argv, '/') + 1;
75 	if (whoami == (char *)1)
76 		whoami = *argv;
77 	if (strcmp(whoami, "quotaoff") == 0)
78 		offmode++;
79 	else if (strcmp(whoami, "quotaon") != 0)
80 		errx(1, "name must be quotaon or quotaoff");
81 	while ((ch = getopt(argc, argv, "avug")) != -1) {
82 		switch(ch) {
83 		case 'a':
84 			aflag++;
85 			break;
86 		case 'g':
87 			gflag++;
88 			break;
89 		case 'u':
90 			uflag++;
91 			break;
92 		case 'v':
93 			vflag++;
94 			break;
95 		default:
96 			usage();
97 		}
98 	}
99 	argc -= optind;
100 	argv += optind;
101 	if (argc <= 0 && !aflag)
102 		usage();
103 	if (!gflag && !uflag) {
104 		gflag++;
105 		uflag++;
106 	}
107 	setfsent();
108 	while ((fs = getfsent()) != NULL) {
109 		if (strcmp(fs->fs_vfstype, "ufs") ||
110 		    strcmp(fs->fs_type, FSTAB_RW))
111 			continue;
112 		if (aflag) {
113 			if (gflag && hasquota(fs, GRPQUOTA, &qfnp))
114 				errs += quotaonoff(fs, offmode, GRPQUOTA, qfnp);
115 			if (uflag && hasquota(fs, USRQUOTA, &qfnp))
116 				errs += quotaonoff(fs, offmode, USRQUOTA, qfnp);
117 			continue;
118 		}
119 		if ((argnum = oneof(fs->fs_file, argv, argc)) >= 0 ||
120 		    (argnum = oneof(fs->fs_spec, argv, argc)) >= 0) {
121 			done |= 1 << argnum;
122 			if (gflag && hasquota(fs, GRPQUOTA, &qfnp))
123 				errs += quotaonoff(fs, offmode, GRPQUOTA, qfnp);
124 			if (uflag && hasquota(fs, USRQUOTA, &qfnp))
125 				errs += quotaonoff(fs, offmode, USRQUOTA, qfnp);
126 		}
127 	}
128 	endfsent();
129 	for (i = 0; i < argc; i++)
130 		if ((done & (1 << i)) == 0)
131 			warnx("%s not found in fstab", argv[i]);
132 	exit(errs);
133 }
134 
135 static void
136 usage(void)
137 {
138 	fprintf(stderr, "%s\n%s\n%s\n%s\n",
139 		"usage: quotaon [-g] [-u] [-v] -a",
140 		"       quotaon [-g] [-u] [-v] filesystem ...",
141 		"       quotaoff [-g] [-u] [-v] -a",
142 		"       quotaoff [-g] [-u] [-v] filesystem ...");
143 	exit(1);
144 }
145 
146 int
147 quotaonoff(struct fstab *fs, int offmode, int type, char *qfpathname)
148 {
149 	if (strcmp(fs->fs_file, "/") && readonly(fs))
150 		return (1);
151 	if (offmode) {
152 		if (quotactl(fs->fs_file, QCMD(Q_QUOTAOFF, type), 0, 0) < 0) {
153 			warn("%s", fs->fs_file);
154 			return (1);
155 		}
156 		if (vflag)
157 			printf("%s: quotas turned off\n", fs->fs_file);
158 		return (0);
159 	}
160 	if (quotactl(fs->fs_file, QCMD(Q_QUOTAON, type), 0, qfpathname) < 0) {
161 		warnx("using %s on", qfpathname);
162 		warn("%s", fs->fs_file);
163 		return (1);
164 	}
165 	if (vflag)
166 		printf("%s: %s quotas turned on\n", fs->fs_file,
167 		    qfextension[type]);
168 	return (0);
169 }
170 
171 /*
172  * Check to see if target appears in list of size cnt.
173  */
174 int
175 oneof(char *target, char *list[], int cnt)
176 {
177 	int i;
178 
179 	for (i = 0; i < cnt; i++)
180 		if (strcmp(target, list[i]) == 0)
181 			return (i);
182 	return (-1);
183 }
184 
185 /*
186  * Check to see if a particular quota is to be enabled.
187  */
188 int
189 hasquota(struct fstab *fs, int type, char **qfnamep)
190 {
191 	char *opt;
192 	char *cp;
193 	static char initname, usrname[100], grpname[100];
194 	static char buf[BUFSIZ];
195 
196 	if (!initname) {
197 		sprintf(usrname, "%s%s", qfextension[USRQUOTA], qfname);
198 		sprintf(grpname, "%s%s", qfextension[GRPQUOTA], qfname);
199 		initname = 1;
200 	}
201 	strcpy(buf, fs->fs_mntops);
202 	for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
203 		if ((cp = strchr(opt, '=')))
204 			*cp++ = '\0';
205 		if (type == USRQUOTA && strcmp(opt, usrname) == 0)
206 			break;
207 		if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
208 			break;
209 	}
210 	if (!opt)
211 		return (0);
212 	if (cp) {
213 		*qfnamep = cp;
214 		return (1);
215 	}
216 	sprintf(buf, "%s/%s.%s", fs->fs_file, qfname, qfextension[type]);
217 	*qfnamep = buf;
218 	return (1);
219 }
220 
221 /*
222  * Verify file system is mounted and not readonly.
223  */
224 int
225 readonly(struct fstab *fs)
226 {
227 	struct statfs fsbuf;
228 
229 	if (statfs(fs->fs_file, &fsbuf) < 0 ||
230 	    strcmp(fsbuf.f_mntonname, fs->fs_file) ||
231 	    strcmp(fsbuf.f_mntfromname, fs->fs_spec)) {
232 		printf("%s: not mounted\n", fs->fs_file);
233 		return (1);
234 	}
235 	if (fsbuf.f_flags & MNT_RDONLY) {
236 		printf("%s: mounted read-only\n", fs->fs_file);
237 		return (1);
238 	}
239 	return (0);
240 }
241