quota.c revision 1.43 1 1.43 dholland /* $NetBSD: quota.c,v 1.43 2011/11/30 16:12:32 dholland Exp $ */
2 1.12 tls
3 1.1 cgd /*
4 1.5 mycroft * Copyright (c) 1980, 1990, 1993
5 1.5 mycroft * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * This code is derived from software contributed to Berkeley by
8 1.1 cgd * Robert Elz at The University of Melbourne.
9 1.1 cgd *
10 1.1 cgd * Redistribution and use in source and binary forms, with or without
11 1.1 cgd * modification, are permitted provided that the following conditions
12 1.1 cgd * are met:
13 1.1 cgd * 1. Redistributions of source code must retain the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer.
15 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 cgd * notice, this list of conditions and the following disclaimer in the
17 1.1 cgd * documentation and/or other materials provided with the distribution.
18 1.26 agc * 3. Neither the name of the University nor the names of its contributors
19 1.1 cgd * may be used to endorse or promote products derived from this software
20 1.1 cgd * without specific prior written permission.
21 1.1 cgd *
22 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 cgd * SUCH DAMAGE.
33 1.1 cgd */
34 1.1 cgd
35 1.16 lukem #include <sys/cdefs.h>
36 1.1 cgd #ifndef lint
37 1.32 lukem __COPYRIGHT("@(#) Copyright (c) 1980, 1990, 1993\
38 1.32 lukem The Regents of the University of California. All rights reserved.");
39 1.1 cgd #endif /* not lint */
40 1.1 cgd
41 1.1 cgd #ifndef lint
42 1.15 mrg #if 0
43 1.15 mrg static char sccsid[] = "@(#)quota.c 8.4 (Berkeley) 4/28/95";
44 1.15 mrg #else
45 1.43 dholland __RCSID("$NetBSD: quota.c,v 1.43 2011/11/30 16:12:32 dholland Exp $");
46 1.15 mrg #endif
47 1.1 cgd #endif /* not lint */
48 1.1 cgd
49 1.1 cgd /*
50 1.1 cgd * Disk quota reporting program.
51 1.1 cgd */
52 1.1 cgd #include <sys/param.h>
53 1.6 deraadt #include <sys/types.h>
54 1.1 cgd #include <sys/file.h>
55 1.1 cgd #include <sys/stat.h>
56 1.6 deraadt #include <sys/mount.h>
57 1.6 deraadt #include <sys/socket.h>
58 1.15 mrg
59 1.42 dholland #include <assert.h>
60 1.15 mrg #include <ctype.h>
61 1.16 lukem #include <err.h>
62 1.15 mrg #include <errno.h>
63 1.15 mrg #include <fstab.h>
64 1.15 mrg #include <grp.h>
65 1.15 mrg #include <netdb.h>
66 1.15 mrg #include <pwd.h>
67 1.1 cgd #include <stdio.h>
68 1.7 cgd #include <stdlib.h>
69 1.16 lukem #include <string.h>
70 1.18 kleink #include <time.h>
71 1.16 lukem #include <unistd.h>
72 1.1 cgd
73 1.37 bouyer #include <quota/quotaprop.h>
74 1.37 bouyer #include <quota/quota.h>
75 1.6 deraadt
76 1.36 christos #include "printquota.h"
77 1.36 christos #include "getvfsquota.h"
78 1.1 cgd
79 1.1 cgd struct quotause {
80 1.1 cgd struct quotause *next;
81 1.1 cgd long flags;
82 1.37 bouyer uid_t id;
83 1.43 dholland struct quotaval *qvs;
84 1.43 dholland unsigned numqvs;
85 1.1 cgd char fsname[MAXPATHLEN + 1];
86 1.6 deraadt };
87 1.1 cgd #define FOUND 0x01
88 1.34 bouyer #define QUOTA2 0x02
89 1.1 cgd
90 1.42 dholland static int anyusage(struct quotaval *, unsigned);
91 1.42 dholland static int anyover(struct quotaval *, unsigned, time_t);
92 1.42 dholland static const char *getovermsg(struct quotaval *, const char *, time_t);
93 1.39 dholland static struct quotause *getprivs(id_t, int);
94 1.39 dholland static void heading(int, id_t, const char *, const char *);
95 1.42 dholland static int isover(struct quotaval *qv, time_t now);
96 1.42 dholland static void printqv(struct quotaval *, int, int, time_t);
97 1.35 christos static void showgid(gid_t);
98 1.35 christos static void showgrpname(const char *);
99 1.41 dholland static void showonequota(int, id_t, const char *, struct quotause *);
100 1.39 dholland static void showquotas(int, id_t, const char *);
101 1.35 christos static void showuid(uid_t);
102 1.35 christos static void showusrname(const char *);
103 1.42 dholland static int unlimited(struct quotaval *qvs, unsigned numqvs);
104 1.40 dholland static void usage(void) __dead;
105 1.35 christos
106 1.35 christos static int qflag = 0;
107 1.35 christos static int vflag = 0;
108 1.35 christos static int hflag = 0;
109 1.35 christos static int dflag = 0;
110 1.35 christos static int Dflag = 0;
111 1.35 christos static uid_t myuid;
112 1.41 dholland static int needheading;
113 1.1 cgd
114 1.16 lukem int
115 1.35 christos main(int argc, char *argv[])
116 1.1 cgd {
117 1.3 jtc int ngroups;
118 1.8 mycroft gid_t mygid, gidset[NGROUPS];
119 1.1 cgd int i, gflag = 0, uflag = 0;
120 1.11 mark int ch;
121 1.1 cgd
122 1.19 mrg myuid = getuid();
123 1.34 bouyer while ((ch = getopt(argc, argv, "Ddhugvq")) != -1) {
124 1.1 cgd switch(ch) {
125 1.1 cgd case 'g':
126 1.1 cgd gflag++;
127 1.1 cgd break;
128 1.1 cgd case 'u':
129 1.1 cgd uflag++;
130 1.1 cgd break;
131 1.1 cgd case 'v':
132 1.1 cgd vflag++;
133 1.1 cgd break;
134 1.1 cgd case 'q':
135 1.1 cgd qflag++;
136 1.1 cgd break;
137 1.34 bouyer case 'h':
138 1.34 bouyer hflag++;
139 1.34 bouyer break;
140 1.34 bouyer case 'd':
141 1.34 bouyer dflag++;
142 1.34 bouyer break;
143 1.34 bouyer case 'D':
144 1.34 bouyer Dflag++;
145 1.34 bouyer break;
146 1.1 cgd default:
147 1.1 cgd usage();
148 1.1 cgd }
149 1.1 cgd }
150 1.1 cgd argc -= optind;
151 1.1 cgd argv += optind;
152 1.1 cgd if (!uflag && !gflag)
153 1.1 cgd uflag++;
154 1.34 bouyer if (dflag) {
155 1.34 bouyer #if 0
156 1.35 christos if (myuid != 0)
157 1.35 christos errx(1, "-d: permission denied");
158 1.34 bouyer #endif
159 1.34 bouyer if (uflag)
160 1.37 bouyer showquotas(QUOTA_CLASS_USER, 0, "");
161 1.34 bouyer if (gflag)
162 1.37 bouyer showquotas(QUOTA_CLASS_GROUP, 0, "");
163 1.35 christos return 0;
164 1.34 bouyer }
165 1.1 cgd if (argc == 0) {
166 1.1 cgd if (uflag)
167 1.19 mrg showuid(myuid);
168 1.1 cgd if (gflag) {
169 1.34 bouyer if (dflag)
170 1.34 bouyer showgid(0);
171 1.34 bouyer else {
172 1.34 bouyer mygid = getgid();
173 1.34 bouyer ngroups = getgroups(NGROUPS, gidset);
174 1.34 bouyer if (ngroups < 0)
175 1.34 bouyer err(1, "getgroups");
176 1.34 bouyer showgid(mygid);
177 1.34 bouyer for (i = 0; i < ngroups; i++)
178 1.34 bouyer if (gidset[i] != mygid)
179 1.34 bouyer showgid(gidset[i]);
180 1.34 bouyer }
181 1.1 cgd }
182 1.35 christos return 0;
183 1.1 cgd }
184 1.1 cgd if (uflag && gflag)
185 1.1 cgd usage();
186 1.1 cgd if (uflag) {
187 1.1 cgd for (; argc > 0; argc--, argv++) {
188 1.1 cgd if (alldigits(*argv))
189 1.35 christos showuid((uid_t)atoi(*argv));
190 1.1 cgd else
191 1.1 cgd showusrname(*argv);
192 1.1 cgd }
193 1.35 christos return 0;
194 1.1 cgd }
195 1.1 cgd if (gflag) {
196 1.1 cgd for (; argc > 0; argc--, argv++) {
197 1.1 cgd if (alldigits(*argv))
198 1.35 christos showgid((gid_t)atoi(*argv));
199 1.1 cgd else
200 1.1 cgd showgrpname(*argv);
201 1.1 cgd }
202 1.35 christos return 0;
203 1.1 cgd }
204 1.16 lukem /* NOTREACHED */
205 1.35 christos return 0;
206 1.1 cgd }
207 1.1 cgd
208 1.35 christos static void
209 1.35 christos usage(void)
210 1.1 cgd {
211 1.35 christos const char *p = getprogname();
212 1.35 christos fprintf(stderr, "Usage: %s [-Dhguqv]\n"
213 1.35 christos "\t%s [-Dhqv] -u username ...\n"
214 1.35 christos "\t%s [-Dhqv] -g groupname ...\n"
215 1.35 christos "\t%s -d [-Dhguqv]\n", p, p, p, p);
216 1.1 cgd exit(1);
217 1.1 cgd }
218 1.1 cgd
219 1.1 cgd /*
220 1.1 cgd * Print out quotas for a specified user identifier.
221 1.1 cgd */
222 1.35 christos static void
223 1.35 christos showuid(uid_t uid)
224 1.1 cgd {
225 1.1 cgd struct passwd *pwd = getpwuid(uid);
226 1.20 mycroft const char *name;
227 1.1 cgd
228 1.1 cgd if (pwd == NULL)
229 1.1 cgd name = "(no account)";
230 1.1 cgd else
231 1.1 cgd name = pwd->pw_name;
232 1.1 cgd if (uid != myuid && myuid != 0) {
233 1.35 christos warnx("%s (uid %d): permission denied", name, uid);
234 1.1 cgd return;
235 1.1 cgd }
236 1.37 bouyer showquotas(QUOTA_CLASS_USER, uid, name);
237 1.1 cgd }
238 1.1 cgd
239 1.1 cgd /*
240 1.24 wiz * Print out quotas for a specified user name.
241 1.1 cgd */
242 1.35 christos static void
243 1.35 christos showusrname(const char *name)
244 1.1 cgd {
245 1.1 cgd struct passwd *pwd = getpwnam(name);
246 1.1 cgd
247 1.1 cgd if (pwd == NULL) {
248 1.16 lukem warnx("%s: unknown user", name);
249 1.1 cgd return;
250 1.1 cgd }
251 1.1 cgd if (pwd->pw_uid != myuid && myuid != 0) {
252 1.16 lukem warnx("%s (uid %d): permission denied", name, pwd->pw_uid);
253 1.1 cgd return;
254 1.1 cgd }
255 1.37 bouyer showquotas(QUOTA_CLASS_USER, pwd->pw_uid, name);
256 1.1 cgd }
257 1.1 cgd
258 1.1 cgd /*
259 1.1 cgd * Print out quotas for a specified group identifier.
260 1.1 cgd */
261 1.35 christos static void
262 1.35 christos showgid(gid_t gid)
263 1.1 cgd {
264 1.1 cgd struct group *grp = getgrgid(gid);
265 1.3 jtc int ngroups;
266 1.8 mycroft gid_t mygid, gidset[NGROUPS];
267 1.16 lukem int i;
268 1.20 mycroft const char *name;
269 1.1 cgd
270 1.1 cgd if (grp == NULL)
271 1.1 cgd name = "(no entry)";
272 1.1 cgd else
273 1.1 cgd name = grp->gr_name;
274 1.8 mycroft mygid = getgid();
275 1.1 cgd ngroups = getgroups(NGROUPS, gidset);
276 1.1 cgd if (ngroups < 0) {
277 1.16 lukem warn("getgroups");
278 1.1 cgd return;
279 1.1 cgd }
280 1.8 mycroft if (gid != mygid) {
281 1.8 mycroft for (i = 0; i < ngroups; i++)
282 1.8 mycroft if (gid == gidset[i])
283 1.8 mycroft break;
284 1.19 mrg if (i >= ngroups && myuid != 0) {
285 1.16 lukem warnx("%s (gid %d): permission denied", name, gid);
286 1.8 mycroft return;
287 1.8 mycroft }
288 1.1 cgd }
289 1.37 bouyer showquotas(QUOTA_CLASS_GROUP, gid, name);
290 1.1 cgd }
291 1.1 cgd
292 1.1 cgd /*
293 1.24 wiz * Print out quotas for a specified group name.
294 1.1 cgd */
295 1.35 christos static void
296 1.35 christos showgrpname(const char *name)
297 1.1 cgd {
298 1.1 cgd struct group *grp = getgrnam(name);
299 1.3 jtc int ngroups;
300 1.8 mycroft gid_t mygid, gidset[NGROUPS];
301 1.16 lukem int i;
302 1.1 cgd
303 1.1 cgd if (grp == NULL) {
304 1.16 lukem warnx("%s: unknown group", name);
305 1.1 cgd return;
306 1.1 cgd }
307 1.8 mycroft mygid = getgid();
308 1.1 cgd ngroups = getgroups(NGROUPS, gidset);
309 1.1 cgd if (ngroups < 0) {
310 1.16 lukem warn("getgroups");
311 1.1 cgd return;
312 1.1 cgd }
313 1.8 mycroft if (grp->gr_gid != mygid) {
314 1.8 mycroft for (i = 0; i < ngroups; i++)
315 1.8 mycroft if (grp->gr_gid == gidset[i])
316 1.8 mycroft break;
317 1.19 mrg if (i >= ngroups && myuid != 0) {
318 1.16 lukem warnx("%s (gid %d): permission denied",
319 1.8 mycroft name, grp->gr_gid);
320 1.8 mycroft return;
321 1.8 mycroft }
322 1.1 cgd }
323 1.37 bouyer showquotas(QUOTA_CLASS_GROUP, grp->gr_gid, name);
324 1.1 cgd }
325 1.1 cgd
326 1.35 christos static void
327 1.42 dholland showquotas(int type, id_t id, const char *idname)
328 1.1 cgd {
329 1.16 lukem struct quotause *qup;
330 1.6 deraadt struct quotause *quplist;
331 1.41 dholland
332 1.41 dholland needheading = 1;
333 1.41 dholland
334 1.41 dholland quplist = getprivs(id, type);
335 1.41 dholland for (qup = quplist; qup; qup = qup->next) {
336 1.42 dholland showonequota(type, id, idname, qup);
337 1.41 dholland }
338 1.41 dholland if (!qflag) {
339 1.41 dholland /* In case nothing printed, issue a header saying "none" */
340 1.42 dholland heading(type, id, idname, "none");
341 1.41 dholland }
342 1.41 dholland }
343 1.41 dholland
344 1.41 dholland static void
345 1.42 dholland showonequota(int type, id_t id, const char *idname, struct quotause *qup)
346 1.41 dholland {
347 1.42 dholland static const int isbytes[QUOTA_NLIMITS] = {
348 1.42 dholland [QUOTA_LIMIT_BLOCK] = 1,
349 1.42 dholland [QUOTA_LIMIT_FILE] = 0,
350 1.42 dholland };
351 1.1 cgd static time_t now;
352 1.42 dholland struct quotaval *qvs;
353 1.42 dholland unsigned numqvs, i;
354 1.42 dholland const char *msg;
355 1.42 dholland int isquota2;
356 1.42 dholland
357 1.43 dholland qvs = qup->qvs;
358 1.43 dholland numqvs = qup->numqvs;
359 1.1 cgd
360 1.41 dholland if (now == 0) {
361 1.1 cgd time(&now);
362 1.41 dholland }
363 1.41 dholland
364 1.42 dholland if (!vflag && unlimited(qvs, numqvs)) {
365 1.41 dholland return;
366 1.41 dholland }
367 1.42 dholland
368 1.41 dholland if (qflag) {
369 1.42 dholland for (i=0; i<numqvs; i++) {
370 1.42 dholland msg = getovermsg(&qvs[i], ufs_quota_limit_names[i],
371 1.42 dholland now);
372 1.42 dholland if (msg != NULL) {
373 1.42 dholland heading(type, id, idname, "");
374 1.42 dholland printf("\t%s %s\n", msg, qup->fsname);
375 1.42 dholland }
376 1.21 ross }
377 1.41 dholland return;
378 1.41 dholland }
379 1.42 dholland
380 1.42 dholland /*
381 1.42 dholland * XXX: anyover can in fact be true if anyusage is not true,
382 1.42 dholland * if there's a quota of zero set on some volume. This is
383 1.42 dholland * because the check we do checks if adding one more thing
384 1.42 dholland * will go over. That is reasonable, I suppose, but Arguably
385 1.42 dholland * the resulting behavior with usage 0 is a bug. (Also, what
386 1.42 dholland * reason do we have to believe that the reported grace expire
387 1.42 dholland * time is valid if we aren't in fact over yet?)
388 1.42 dholland */
389 1.42 dholland
390 1.42 dholland if (vflag || dflag || anyover(qvs, numqvs, now) ||
391 1.42 dholland anyusage(qvs, numqvs)) {
392 1.42 dholland heading(type, id, idname, "");
393 1.41 dholland if (strlen(qup->fsname) > 4) {
394 1.41 dholland printf("%s\n", qup->fsname);
395 1.42 dholland printf("%12s", "");
396 1.42 dholland } else {
397 1.42 dholland printf("%12s", qup->fsname);
398 1.42 dholland }
399 1.42 dholland
400 1.42 dholland isquota2 = (qup->flags & QUOTA2) != 0;
401 1.42 dholland
402 1.42 dholland for (i=0; i<numqvs; i++) {
403 1.43 dholland printqv(&qvs[i], isquota2,
404 1.43 dholland i >= QUOTA_NLIMITS ? 0 : isbytes[i], now);
405 1.42 dholland }
406 1.42 dholland printf("\n");
407 1.1 cgd }
408 1.1 cgd }
409 1.1 cgd
410 1.35 christos static void
411 1.42 dholland heading(int type, id_t id, const char *idname, const char *tag)
412 1.1 cgd {
413 1.41 dholland if (needheading == 0)
414 1.41 dholland return;
415 1.41 dholland needheading = 0;
416 1.41 dholland
417 1.34 bouyer if (dflag)
418 1.34 bouyer printf("Default %s disk quotas: %s\n",
419 1.37 bouyer ufs_quota_class_names[type], tag);
420 1.34 bouyer else
421 1.35 christos printf("Disk quotas for %s %s (%cid %u): %s\n",
422 1.42 dholland ufs_quota_class_names[type], idname,
423 1.37 bouyer *ufs_quota_class_names[type], id, tag);
424 1.1 cgd
425 1.1 cgd if (!qflag && tag[0] == '\0') {
426 1.22 bouyer printf("%12s%9s %8s%9s%8s%8s %7s%8s%8s\n"
427 1.22 bouyer , "Filesystem"
428 1.22 bouyer , "blocks"
429 1.22 bouyer , "quota"
430 1.22 bouyer , "limit"
431 1.22 bouyer , "grace"
432 1.22 bouyer , "files"
433 1.22 bouyer , "quota"
434 1.22 bouyer , "limit"
435 1.22 bouyer , "grace"
436 1.1 cgd );
437 1.1 cgd }
438 1.1 cgd }
439 1.1 cgd
440 1.42 dholland static void
441 1.42 dholland printqv(struct quotaval *qv, int isquota2, int isbytes, time_t now)
442 1.42 dholland {
443 1.42 dholland char buf[20];
444 1.42 dholland const char *str;
445 1.42 dholland int intprtflags, over, width;
446 1.42 dholland
447 1.42 dholland /*
448 1.42 dholland * The assorted finagling of width is to match the previous
449 1.42 dholland * open-coded formatting for exactly two quota object types,
450 1.42 dholland * which was chosen to make the default report fit in 80
451 1.42 dholland * columns.
452 1.42 dholland */
453 1.42 dholland
454 1.42 dholland width = isbytes ? 9 : 8;
455 1.42 dholland intprtflags = isbytes ? HN_B : 0;
456 1.42 dholland over = isover(qv, now);
457 1.42 dholland
458 1.42 dholland str = intprt(buf, width, qv->qv_usage, intprtflags, hflag);
459 1.42 dholland printf("%*s", width, str);
460 1.42 dholland
461 1.42 dholland printf("%c", over ? '*' : ' ');
462 1.42 dholland
463 1.42 dholland str = intprt(buf, width, qv->qv_softlimit, intprtflags, hflag);
464 1.42 dholland printf("%*s", width-1, str);
465 1.42 dholland
466 1.42 dholland str = intprt(buf, width, qv->qv_hardlimit, intprtflags, hflag);
467 1.42 dholland printf("%*s", width, str);
468 1.42 dholland
469 1.42 dholland if (over) {
470 1.42 dholland str = timeprt(buf, 9, now, qv->qv_expiretime);
471 1.42 dholland } else if (isquota2 && vflag) {
472 1.42 dholland str = timeprt(buf, 9, 0, qv->qv_grace);
473 1.42 dholland } else {
474 1.42 dholland str = "";
475 1.42 dholland }
476 1.42 dholland printf("%8s", str);
477 1.42 dholland }
478 1.42 dholland
479 1.1 cgd /*
480 1.1 cgd * Collect the requested quota information.
481 1.1 cgd */
482 1.35 christos static struct quotause *
483 1.39 dholland getprivs(id_t id, int quotatype)
484 1.1 cgd {
485 1.16 lukem struct quotause *qup, *quptail;
486 1.1 cgd struct quotause *quphead;
487 1.29 christos struct statvfs *fst;
488 1.6 deraadt int nfst, i;
489 1.34 bouyer int8_t version;
490 1.6 deraadt
491 1.16 lukem qup = quphead = quptail = NULL;
492 1.1 cgd
493 1.6 deraadt nfst = getmntinfo(&fst, MNT_WAIT);
494 1.16 lukem if (nfst == 0)
495 1.16 lukem errx(2, "no filesystems mounted!");
496 1.19 mrg for (i = 0; i < nfst; i++) {
497 1.6 deraadt if (qup == NULL) {
498 1.35 christos if ((qup = malloc(sizeof *qup)) == NULL)
499 1.43 dholland err(1, "Out of memory");
500 1.1 cgd }
501 1.31 christos if (strncmp(fst[i].f_fstypename, "nfs",
502 1.31 christos sizeof(fst[i].f_fstypename)) == 0) {
503 1.34 bouyer version = 0;
504 1.43 dholland qup->numqvs = QUOTA_NLIMITS;
505 1.43 dholland qup->qvs = malloc(qup->numqvs * sizeof(qup->qvs[0]));
506 1.43 dholland if (qup->qvs == NULL) {
507 1.43 dholland err(1, "Out of memory");
508 1.43 dholland }
509 1.37 bouyer if (getnfsquota(fst[i].f_mntfromname,
510 1.43 dholland qup->qvs, id, ufs_quota_class_names[quotatype]) != 1)
511 1.6 deraadt continue;
512 1.34 bouyer } else if ((fst[i].f_flag & ST_QUOTA) != 0) {
513 1.43 dholland qup->numqvs = QUOTA_NLIMITS;
514 1.43 dholland qup->qvs = malloc(qup->numqvs * sizeof(qup->qvs[0]));
515 1.43 dholland if (qup->qvs == NULL) {
516 1.43 dholland err(1, "Out of memory");
517 1.43 dholland }
518 1.43 dholland if (getvfsquota(fst[i].f_mntonname, qup->qvs, &version,
519 1.37 bouyer id, quotatype, dflag, Dflag) != 1)
520 1.1 cgd continue;
521 1.6 deraadt } else
522 1.6 deraadt continue;
523 1.13 mrg (void)strncpy(qup->fsname, fst[i].f_mntonname,
524 1.13 mrg sizeof(qup->fsname) - 1);
525 1.34 bouyer if (version == 2)
526 1.34 bouyer qup->flags |= QUOTA2;
527 1.1 cgd if (quphead == NULL)
528 1.1 cgd quphead = qup;
529 1.1 cgd else
530 1.1 cgd quptail->next = qup;
531 1.1 cgd quptail = qup;
532 1.6 deraadt quptail->next = 0;
533 1.6 deraadt qup = NULL;
534 1.1 cgd }
535 1.35 christos free(qup);
536 1.35 christos return quphead;
537 1.1 cgd }
538 1.42 dholland
539 1.42 dholland static int
540 1.42 dholland unlimited(struct quotaval *qvs, unsigned numqvs)
541 1.42 dholland {
542 1.42 dholland unsigned i;
543 1.42 dholland
544 1.42 dholland for (i=0; i<numqvs; i++) {
545 1.42 dholland if (qvs[i].qv_softlimit != UQUAD_MAX ||
546 1.42 dholland qvs[i].qv_hardlimit != UQUAD_MAX) {
547 1.42 dholland return 0;
548 1.42 dholland }
549 1.42 dholland }
550 1.42 dholland return 1;
551 1.42 dholland }
552 1.42 dholland
553 1.42 dholland static int
554 1.42 dholland anyusage(struct quotaval *qvs, unsigned numqvs)
555 1.42 dholland {
556 1.42 dholland unsigned i;
557 1.42 dholland
558 1.42 dholland for (i=0; i<numqvs; i++) {
559 1.42 dholland if (qvs[i].qv_usage > 0) {
560 1.42 dholland return 1;
561 1.42 dholland }
562 1.42 dholland }
563 1.42 dholland return 0;
564 1.42 dholland }
565 1.42 dholland
566 1.42 dholland static int
567 1.42 dholland anyover(struct quotaval *qvs, unsigned numqvs, time_t now)
568 1.42 dholland {
569 1.42 dholland unsigned i;
570 1.42 dholland
571 1.42 dholland for (i=0; i<numqvs; i++) {
572 1.42 dholland if (isover(&qvs[i], now)) {
573 1.42 dholland return 1;
574 1.42 dholland }
575 1.42 dholland }
576 1.42 dholland return 0;
577 1.42 dholland }
578 1.42 dholland
579 1.42 dholland static int
580 1.42 dholland isover(struct quotaval *qv, time_t now)
581 1.42 dholland {
582 1.42 dholland int ql_stat;
583 1.42 dholland
584 1.42 dholland ql_stat = quota_check_limit(qv->qv_usage, 1,
585 1.42 dholland qv->qv_softlimit,
586 1.42 dholland qv->qv_hardlimit,
587 1.42 dholland qv->qv_expiretime, now);
588 1.42 dholland switch(QL_STATUS(ql_stat)) {
589 1.42 dholland case QL_S_DENY_HARD:
590 1.42 dholland case QL_S_DENY_GRACE:
591 1.42 dholland case QL_S_ALLOW_SOFT:
592 1.42 dholland return 1;
593 1.42 dholland default:
594 1.42 dholland break;
595 1.42 dholland }
596 1.42 dholland return 0;
597 1.42 dholland }
598 1.42 dholland
599 1.42 dholland static const char *
600 1.42 dholland getovermsg(struct quotaval *qv, const char *what, time_t now)
601 1.42 dholland {
602 1.42 dholland static char buf[64];
603 1.42 dholland int ql_stat;
604 1.42 dholland
605 1.42 dholland ql_stat = quota_check_limit(qv->qv_usage, 1,
606 1.42 dholland qv->qv_softlimit,
607 1.42 dholland qv->qv_hardlimit,
608 1.42 dholland qv->qv_expiretime, now);
609 1.42 dholland switch(QL_STATUS(ql_stat)) {
610 1.42 dholland case QL_S_DENY_HARD:
611 1.42 dholland snprintf(buf, sizeof(buf), "%c%s limit reached on",
612 1.42 dholland toupper((unsigned char)what[0]), what+1);
613 1.42 dholland break;
614 1.42 dholland case QL_S_DENY_GRACE:
615 1.42 dholland snprintf(buf, sizeof(buf), "Over %s quota on", what);
616 1.42 dholland break;
617 1.42 dholland case QL_S_ALLOW_SOFT:
618 1.42 dholland snprintf(buf, sizeof(buf), "In %s grace period on", what);
619 1.42 dholland break;
620 1.42 dholland default:
621 1.42 dholland return NULL;
622 1.42 dholland }
623 1.42 dholland return buf;
624 1.42 dholland }
625