xref: /netbsd-src/usr.sbin/quotaon/quotaon.c (revision ead2c0eee3abe6bcf08c63bfc78eb8a93a579b2b)
1 /*	$NetBSD: quotaon.c,v 1.29 2012/01/30 16:45:13 dholland Exp $	*/
2 
3 /*
4  * Copyright (c) 1980, 1990, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Robert Elz at The University of Melbourne.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #include <sys/cdefs.h>
36 #ifndef lint
37 __COPYRIGHT("@(#) Copyright (c) 1980, 1990, 1993\
38  The Regents of the University of California.  All rights reserved.");
39 #endif /* not lint */
40 
41 #ifndef lint
42 #if 0
43 static char sccsid[] = "@(#)quotaon.c	8.1 (Berkeley) 6/6/93";
44 #else
45 __RCSID("$NetBSD: quotaon.c,v 1.29 2012/01/30 16:45:13 dholland Exp $");
46 #endif
47 #endif /* not lint */
48 
49 /*
50  * Turn quota on/off for a filesystem.
51  */
52 #include <sys/param.h>
53 #include <sys/file.h>
54 #include <sys/mount.h>
55 
56 #include <quota.h>
57 #include <ufs/ufs/quota1.h>
58 
59 #include <err.h>
60 #include <fstab.h>
61 #include <stdio.h>
62 #include <errno.h>
63 #include <stdlib.h>
64 #include <string.h>
65 #include <unistd.h>
66 
67 
68 static int	vflag;		/* verbose */
69 
70 static void usage(void) __dead;
71 static int quotaonoff(struct fstab *, struct quotahandle *, int, int, int);
72 static int readonly(struct fstab *);
73 static int oneof(const char *target, char *list[], int cnt);
74 
75 int
76 main(int argc, char *argv[])
77 {
78 	struct fstab *fs;
79 	struct quotahandle *qh;
80 	long argnum, done = 0;
81 	int i, offmode = 0, errs = 0;
82 	unsigned restrictions;
83 	int ch;
84 
85 	int aflag = 0;		/* all file systems */
86 	int gflag = 0;		/* operate on group quotas */
87 	int uflag = 0;		/* operate on user quotas */
88 	int noguflag = 0;	/* operate on both (by default) */
89 
90 	if (strcmp(getprogname(), "quotaoff") == 0)
91 		offmode++;
92 	else if (strcmp(getprogname(), "quotaon") != 0)
93 		errx(1, "Name must be quotaon or quotaoff");
94 
95 	while ((ch = getopt(argc, argv, "avug")) != -1) {
96 		switch(ch) {
97 		case 'a':
98 			aflag++;
99 			break;
100 		case 'g':
101 			gflag++;
102 			break;
103 		case 'u':
104 			uflag++;
105 			break;
106 		case 'v':
107 			vflag++;
108 			break;
109 		default:
110 			usage();
111 			break;
112 		}
113 	}
114 	argc -= optind;
115 	argv += optind;
116 
117 	if (argc <= 0 && !aflag)
118 		usage();
119 
120 	if (!gflag && !uflag) {
121 		noguflag = 1;
122 	}
123 
124 	/*
125 	 * XXX at the moment quota_open also uses getfsent(), but it
126 	 * uses it only up front. To avoid conflicting with it, let it
127 	 * initialize first.
128 	 */
129 	qh = quota_open("/");
130 	if (qh != NULL) {
131 		quota_close(qh);
132 	}
133 
134 	setfsent();
135 	while ((fs = getfsent()) != NULL) {
136 		if ((strcmp(fs->fs_vfstype, "ffs") &&
137 		     strcmp(fs->fs_vfstype, "lfs")) ||
138 		    strcmp(fs->fs_type, FSTAB_RW))
139 			continue;
140 
141 		if (!aflag) {
142 			if ((argnum = oneof(fs->fs_file, argv, argc)) < 0 &&
143 			    (argnum = oneof(fs->fs_spec, argv, argc)) < 0) {
144 				continue;
145 			}
146 			done |= 1U << argnum;
147 		}
148 
149 		qh = quota_open(fs->fs_file);
150 		if (qh == NULL) {
151 			if (!aflag) {
152 				warn("quota_open");
153 				errs++;
154 			}
155 			continue;
156 		}
157 
158 		restrictions = quota_getrestrictions(qh);
159 		if ((restrictions & QUOTA_RESTRICT_NEEDSQUOTACHECK) == 0) {
160 			/* Not a quota v1 volume, skip it */
161 			if (!aflag) {
162 				errno = EBUSY;
163 				warn("%s", fs->fs_file);
164 				errs++;
165 			}
166 			quota_close(qh);
167 			continue;
168 		}
169 
170 		/*
171 		 * The idea here is to warn if someone explicitly
172 		 * tries to turn on group quotas and there are no
173 		 * group quotas, and likewise for user quotas, but not
174 		 * to warn if just doing the default thing and one of
175 		 * the quota types isn't configured.
176 		 */
177 
178 		if (noguflag) {
179 			errs += quotaonoff(fs, qh, offmode, GRPQUOTA, 0);
180 			errs += quotaonoff(fs, qh, offmode, USRQUOTA, 0);
181 		}
182 		if (gflag) {
183 			errs += quotaonoff(fs, qh, offmode, GRPQUOTA, 1);
184 		}
185 		if (uflag) {
186 			errs += quotaonoff(fs, qh, offmode, USRQUOTA, 1);
187 		}
188 		quota_close(qh);
189 	}
190 	endfsent();
191 	for (i = 0; i < argc; i++)
192 		if ((done & (1U << i)) == 0)
193 			warnx("%s not found in fstab", argv[i]);
194 	return errs;
195 }
196 
197 static void
198 usage(void)
199 {
200 	const char *p = getprogname();
201 	(void) fprintf(stderr, "Usage: %s [-g] [-u] [-v] -a\n"
202 	    "\t%s [-g] [-u] [-v] filesys ...\n", p, p);
203 	exit(1);
204 }
205 
206 static int
207 quotaonoff(struct fstab *fs, struct quotahandle *qh, int offmode, int idtype,
208 	   int warn_on_enxio)
209 {
210 	const char *mode = (offmode == 1) ? "off" : "on";
211 
212 	if (strcmp(fs->fs_file, "/") && readonly(fs)) {
213 		return 1;
214 	}
215 
216 	if (offmode) {
217 		if (quota_quotaoff(qh, idtype)) {
218 			if (warn_on_enxio || errno != ENXIO) {
219 				warn("quota%s for %s", mode, fs->fs_file);
220 			}
221 			return 1;
222 		}
223 	} else {
224 		if (quota_quotaon(qh, idtype)) {
225 			if (warn_on_enxio || errno != ENXIO) {
226 				warn("quota%s for %s", mode, fs->fs_file);
227 			}
228 			return 1;
229 		}
230 	}
231 
232 	if (vflag) {
233 		printf("%s: %s quotas turned %s\n",
234 		    fs->fs_file, quota_idtype_getname(qh, idtype), mode);
235 	}
236 	return 0;
237 }
238 
239 /*
240  * Verify file system is mounted and not readonly.
241  */
242 static int
243 readonly(struct fstab *fs)
244 {
245 	struct statvfs fsbuf;
246 
247 	if (statvfs(fs->fs_file, &fsbuf) < 0 ||
248 	    strcmp(fsbuf.f_mntonname, fs->fs_file) ||
249 	    strcmp(fsbuf.f_mntfromname, fs->fs_spec)) {
250 		printf("%s: not mounted\n", fs->fs_file);
251 		return 1;
252 	}
253 	if (fsbuf.f_flag & MNT_RDONLY) {
254 		printf("%s: mounted read-only\n", fs->fs_file);
255 		return 1;
256 	}
257 	return 0;
258 }
259 
260 /*
261  * Check to see if target appears in list of size cnt.
262  */
263 static int
264 oneof(const char *target, char *list[], int cnt)
265 {
266 	int i;
267 
268 	for (i = 0; i < cnt; i++)
269 		if (strcmp(target, list[i]) == 0)
270 			return i;
271 	return -1;
272 }
273