quotaon.c revision 1.24 1 /* $NetBSD: quotaon.c,v 1.24 2011/03/06 17:08:43 bouyer 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.24 2011/03/06 17:08:43 bouyer 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 <ufs/ufs/quota2_prop.h>
57 #include <ufs/ufs/quota1.h>
58 #include <sys/quota.h>
59
60
61 #include <err.h>
62 #include <fstab.h>
63 #include <stdio.h>
64 #include <stdlib.h>
65 #include <string.h>
66 #include <unistd.h>
67
68 const char *qfname = QUOTAFILENAME;
69 const char *qfextension[] = INITQFNAMES;
70
71 int aflag; /* all file systems */
72 int gflag; /* operate on group quotas */
73 int uflag; /* operate on user quotas */
74 int vflag; /* verbose */
75
76 int main __P((int, char *[]));
77
78 static void usage __P((void));
79 static int quotaonoff __P((struct fstab *, int, int, char *));
80 static int oneof __P((const char *, char *[], int));
81 static int hasquota __P((struct fstab *, int, char **));
82 static int readonly __P((struct fstab *));
83
84 int
85 main(argc, argv)
86 int argc;
87 char *argv[];
88 {
89 struct fstab *fs;
90 char *qfnp;
91 long argnum, done = 0;
92 int i, offmode = 0, errs = 0;
93 int ch;
94
95 if (strcmp(getprogname(), "quotaoff") == 0)
96 offmode++;
97 else if (strcmp(getprogname(), "quotaon") != 0)
98 errx(1, "Name must be quotaon or quotaoff");
99
100 while ((ch = getopt(argc, argv, "avug")) != -1) {
101 switch(ch) {
102 case 'a':
103 aflag++;
104 break;
105 case 'g':
106 gflag++;
107 break;
108 case 'u':
109 uflag++;
110 break;
111 case 'v':
112 vflag++;
113 break;
114 default:
115 usage();
116 break;
117 }
118 }
119 argc -= optind;
120 argv += optind;
121
122 if (argc <= 0 && !aflag)
123 usage();
124
125 if (!gflag && !uflag) {
126 gflag++;
127 uflag++;
128 }
129 setfsent();
130 while ((fs = getfsent()) != NULL) {
131 if ((strcmp(fs->fs_vfstype, "ffs") &&
132 strcmp(fs->fs_vfstype, "lfs")) ||
133 strcmp(fs->fs_type, FSTAB_RW))
134 continue;
135 if (aflag) {
136 if (gflag && hasquota(fs, GRPQUOTA, &qfnp))
137 errs += quotaonoff(fs, offmode, GRPQUOTA, qfnp);
138 if (uflag && hasquota(fs, USRQUOTA, &qfnp))
139 errs += quotaonoff(fs, offmode, USRQUOTA, qfnp);
140 continue;
141 }
142 if ((argnum = oneof(fs->fs_file, argv, argc)) >= 0 ||
143 (argnum = oneof(fs->fs_spec, argv, argc)) >= 0) {
144 done |= 1 << argnum;
145 if (gflag && hasquota(fs, GRPQUOTA, &qfnp))
146 errs += quotaonoff(fs, offmode, GRPQUOTA, qfnp);
147 if (uflag && hasquota(fs, USRQUOTA, &qfnp))
148 errs += quotaonoff(fs, offmode, USRQUOTA, qfnp);
149 }
150 }
151 endfsent();
152 for (i = 0; i < argc; i++)
153 if ((done & (1 << i)) == 0)
154 warnx("%s not found in fstab", argv[i]);
155 exit(errs);
156 }
157
158 static void
159 usage()
160 {
161
162 (void) fprintf(stderr, "usage:\n\t%s [-g] [-u] [-v] -a\n",
163 getprogname());
164 (void) fprintf(stderr, "\t%s [-g] [-u] [-v] filesys ...\n",
165 getprogname());
166 exit(1);
167 }
168
169 static int
170 quotaonoff(fs, offmode, type, qfpathname)
171 struct fstab *fs;
172 int offmode, type;
173 char *qfpathname;
174 {
175 const char *mode = (offmode == 1) ? "off" : "on";
176 prop_dictionary_t dict, data, cmd;
177 prop_array_t cmds, datas;
178 struct plistref pref;
179 int error;
180 int8_t error8;
181
182 dict = quota2_prop_create();
183 cmds = prop_array_create();
184 datas = prop_array_create();
185
186 if (strcmp(fs->fs_file, "/") && readonly(fs))
187 return (1);
188
189 if (dict == NULL || cmds == NULL || datas == NULL)
190 errx(1, "can't allocate proplist");
191
192 if (offmode) {
193 if (!quota2_prop_add_command(cmds, "quotaoff",
194 qfextension[type], datas))
195 err(1, "prop_add_command");
196 } else {
197 data = prop_dictionary_create();
198 if (data == NULL)
199 errx(1, "can't allocate proplist");
200 if (!prop_dictionary_set_cstring(data, "quotafile",
201 qfpathname))
202 err(1, "prop_dictionary_set(quotafile)");
203 if (!prop_array_add_and_rel(datas, data))
204 err(1, "prop_array_add(data)");
205 if (!quota2_prop_add_command(cmds, "quotaon",
206 qfextension[type], datas))
207 err(1, "prop_add_command");
208 }
209 if (!prop_dictionary_set(dict, "commands", cmds))
210 err(1, "prop_dictionary_set(command)");
211
212 if (!prop_dictionary_send_syscall(dict, &pref))
213 err(1, "prop_dictionary_send_syscall");
214 prop_object_release(dict);
215
216 if (quotactl(fs->fs_file, &pref) != 0) {
217 warn("quotactl(%s)", fs->fs_file);
218 return(1);
219 }
220
221 if ((error = prop_dictionary_recv_syscall(&pref, &dict)) != 0) {
222 errx(1, "prop_dictionary_recv_syscall: %s\n",
223 strerror(error));
224 }
225
226 if ((error = quota2_get_cmds(dict, &cmds)) != 0) {
227 errx(1, "quota2_get_cmds: %s\n", strerror(error));
228 }
229 /* only one command, no need to iter */
230 cmd = prop_array_get(cmds, 0);
231 if (cmd == NULL)
232 err(1, "prop_array_get(cmd)");
233
234 if (!prop_dictionary_get_int8(cmd, "return", &error8))
235 err(1, "prop_get(return)");
236
237 if (error8) {
238 warnx("quota%s for %s: %s", mode, fs->fs_file,
239 strerror(error8));
240 return 1;
241 }
242
243 if (vflag) {
244 printf("%s: %s quotas turned %s\n",
245 fs->fs_file, qfextension[type], mode);
246 }
247 return (0);
248 }
249
250 /*
251 * Check to see if target appears in list of size cnt.
252 */
253 static int
254 oneof(target, list, cnt)
255 const char *target;
256 char *list[];
257 int cnt;
258 {
259 int i;
260
261 for (i = 0; i < cnt; i++)
262 if (strcmp(target, list[i]) == 0)
263 return (i);
264 return (-1);
265 }
266
267 /*
268 * Check to see if a particular quota is to be enabled.
269 */
270 static int
271 hasquota(fs, type, qfnamep)
272 struct fstab *fs;
273 int type;
274 char **qfnamep;
275 {
276 char *opt;
277 char *cp = NULL;
278 static char initname, usrname[100], grpname[100];
279 static char buf[BUFSIZ];
280
281 if (!initname) {
282 (void) snprintf(usrname, sizeof(usrname), "%s%s",
283 qfextension[USRQUOTA], qfname);
284 (void) snprintf(grpname, sizeof(grpname), "%s%s",
285 qfextension[GRPQUOTA], qfname);
286 initname = 1;
287 }
288 strcpy(buf, fs->fs_mntops);
289 for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
290 if ((cp = strchr(opt, '=')) != NULL)
291 *cp++ = '\0';
292 if (type == USRQUOTA && strcmp(opt, usrname) == 0)
293 break;
294 if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
295 break;
296 }
297 if (!opt)
298 return (0);
299 if (cp) {
300 *qfnamep = cp;
301 return (1);
302 }
303 (void) snprintf(buf, sizeof(buf), "%s/%s.%s", fs->fs_file, qfname,
304 qfextension[type]);
305 *qfnamep = buf;
306 return (1);
307 }
308
309 /*
310 * Verify file system is mounted and not readonly.
311 */
312 static int
313 readonly(fs)
314 struct fstab *fs;
315 {
316 struct statvfs fsbuf;
317
318 if (statvfs(fs->fs_file, &fsbuf) < 0 ||
319 strcmp(fsbuf.f_mntonname, fs->fs_file) ||
320 strcmp(fsbuf.f_mntfromname, fs->fs_spec)) {
321 printf("%s: not mounted\n", fs->fs_file);
322 return (1);
323 }
324 if (fsbuf.f_flag & MNT_RDONLY) {
325 printf("%s: mounted read-only\n", fs->fs_file);
326 return (1);
327 }
328 return (0);
329 }
330