repquota.c revision 1.41 1 1.41 dholland /* $NetBSD: repquota.c,v 1.41 2012/02/01 17:48:10 dholland Exp $ */
2 1.27 christos
3 1.1 cgd /*
4 1.4 mycroft * Copyright (c) 1980, 1990, 1993
5 1.4 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.20 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.10 mrg #include <sys/cdefs.h>
36 1.1 cgd #ifndef lint
37 1.23 lukem __COPYRIGHT("@(#) Copyright (c) 1980, 1990, 1993\
38 1.23 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.10 mrg #if 0
43 1.10 mrg static char sccsid[] = "@(#)repquota.c 8.2 (Berkeley) 11/22/94";
44 1.10 mrg #else
45 1.41 dholland __RCSID("$NetBSD: repquota.c,v 1.41 2012/02/01 17:48:10 dholland Exp $");
46 1.10 mrg #endif
47 1.1 cgd #endif /* not lint */
48 1.1 cgd
49 1.1 cgd /*
50 1.1 cgd * Quota report
51 1.1 cgd */
52 1.1 cgd #include <sys/param.h>
53 1.1 cgd #include <sys/stat.h>
54 1.26 bouyer #include <sys/types.h>
55 1.26 bouyer #include <sys/statvfs.h>
56 1.26 bouyer
57 1.11 lukem #include <errno.h>
58 1.26 bouyer #include <err.h>
59 1.1 cgd #include <fstab.h>
60 1.11 lukem #include <grp.h>
61 1.1 cgd #include <pwd.h>
62 1.1 cgd #include <stdio.h>
63 1.11 lukem #include <stdlib.h>
64 1.6 cgd #include <string.h>
65 1.11 lukem #include <unistd.h>
66 1.1 cgd
67 1.30 bouyer #include <quota/quota.h>
68 1.34 dholland #include <quota.h>
69 1.26 bouyer
70 1.27 christos #include "printquota.h"
71 1.1 cgd
72 1.38 dholland /*
73 1.38 dholland * XXX. Ideally we shouldn't compile either of these in, but it's a
74 1.38 dholland * nontrivial rework to avoid it and it'll work ok for now.
75 1.38 dholland */
76 1.38 dholland #define REPQUOTA_NUMIDTYPES 2
77 1.38 dholland #define REPQUOTA_NUMOBJTYPES 2
78 1.38 dholland
79 1.1 cgd struct fileusage {
80 1.1 cgd struct fileusage *fu_next;
81 1.38 dholland struct quotaval fu_qv[REPQUOTA_NUMOBJTYPES];
82 1.27 christos uint32_t fu_id;
83 1.1 cgd char fu_name[1];
84 1.1 cgd /* actually bigger */
85 1.1 cgd };
86 1.38 dholland
87 1.1 cgd #define FUHASH 1024 /* must be power of two */
88 1.38 dholland static struct fileusage *fuhead[REPQUOTA_NUMIDTYPES][FUHASH];
89 1.38 dholland
90 1.38 dholland /* highest addid()'ed identifier per idtype */
91 1.38 dholland static uint32_t highid[REPQUOTA_NUMIDTYPES];
92 1.38 dholland
93 1.38 dholland int valid[REPQUOTA_NUMIDTYPES];
94 1.38 dholland
95 1.38 dholland static struct quotaval defaultqv[REPQUOTA_NUMIDTYPES][REPQUOTA_NUMOBJTYPES];
96 1.1 cgd
97 1.27 christos static int vflag = 0; /* verbose */
98 1.27 christos static int aflag = 0; /* all file systems */
99 1.27 christos static int Dflag = 0; /* debug */
100 1.27 christos static int hflag = 0; /* humanize */
101 1.27 christos static int xflag = 0; /* export */
102 1.27 christos
103 1.38 dholland /*
104 1.38 dholland * XXX this should go away and be replaced with a call to
105 1.38 dholland * quota_idtype_getname(), but that needs a quotahandle and requires
106 1.38 dholland * the same nontrivial rework as getting rid of REPQUOTA_NUMIDTYPES.
107 1.38 dholland */
108 1.38 dholland static const char *const repquota_idtype_names[REPQUOTA_NUMIDTYPES] = {
109 1.38 dholland "user",
110 1.38 dholland "group",
111 1.38 dholland };
112 1.27 christos
113 1.27 christos static struct fileusage *addid(uint32_t, int, const char *);
114 1.27 christos static struct fileusage *lookup(uint32_t, int);
115 1.27 christos static struct fileusage *qremove(uint32_t, int);
116 1.35 dholland static int repquota(struct quotahandle *, int);
117 1.27 christos static void usage(void) __attribute__((__noreturn__));
118 1.35 dholland static void printquotas(int, struct quotahandle *);
119 1.27 christos static void exportquotas(void);
120 1.37 dholland static int oneof(const char *, char *[], int cnt);
121 1.41 dholland static int isover(struct quotaval *qv, time_t now);
122 1.11 lukem
123 1.11 lukem int
124 1.27 christos main(int argc, char **argv)
125 1.1 cgd {
126 1.1 cgd int gflag = 0, uflag = 0, errs = 0;
127 1.1 cgd long i, argnum, done = 0;
128 1.9 mark int ch;
129 1.26 bouyer struct statvfs *fst;
130 1.26 bouyer int nfst;
131 1.35 dholland struct quotahandle *qh;
132 1.1 cgd
133 1.26 bouyer while ((ch = getopt(argc, argv, "Daguhvx")) != -1) {
134 1.1 cgd switch(ch) {
135 1.1 cgd case 'a':
136 1.1 cgd aflag++;
137 1.1 cgd break;
138 1.1 cgd case 'g':
139 1.1 cgd gflag++;
140 1.1 cgd break;
141 1.1 cgd case 'u':
142 1.1 cgd uflag++;
143 1.1 cgd break;
144 1.26 bouyer case 'h':
145 1.26 bouyer hflag++;
146 1.26 bouyer break;
147 1.1 cgd case 'v':
148 1.1 cgd vflag++;
149 1.1 cgd break;
150 1.26 bouyer case 'D':
151 1.26 bouyer Dflag++;
152 1.26 bouyer break;
153 1.26 bouyer case 'x':
154 1.26 bouyer xflag++;
155 1.26 bouyer break;
156 1.1 cgd default:
157 1.1 cgd usage();
158 1.1 cgd }
159 1.1 cgd }
160 1.1 cgd argc -= optind;
161 1.1 cgd argv += optind;
162 1.26 bouyer if (xflag && (argc != 1 || aflag))
163 1.26 bouyer usage();
164 1.1 cgd if (argc == 0 && !aflag)
165 1.1 cgd usage();
166 1.1 cgd if (!gflag && !uflag) {
167 1.1 cgd if (aflag)
168 1.1 cgd gflag++;
169 1.1 cgd uflag++;
170 1.1 cgd }
171 1.26 bouyer
172 1.26 bouyer nfst = getmntinfo(&fst, MNT_WAIT);
173 1.26 bouyer if (nfst == 0)
174 1.27 christos errx(1, "no filesystems mounted!");
175 1.26 bouyer for (i = 0; i < nfst; i++) {
176 1.26 bouyer if ((fst[i].f_flag & ST_QUOTA) == 0)
177 1.1 cgd continue;
178 1.35 dholland /* check if we want this volume */
179 1.35 dholland if (!aflag) {
180 1.35 dholland argnum = oneof(fst[i].f_mntonname, argv, argc);
181 1.35 dholland if (argnum < 0) {
182 1.35 dholland argnum = oneof(fst[i].f_mntfromname,
183 1.35 dholland argv, argc);
184 1.35 dholland }
185 1.35 dholland if (argnum < 0) {
186 1.35 dholland continue;
187 1.35 dholland }
188 1.35 dholland done |= 1U << argnum;
189 1.35 dholland }
190 1.35 dholland
191 1.35 dholland qh = quota_open(fst[i].f_mntonname);
192 1.35 dholland if (qh == NULL) {
193 1.35 dholland /* XXX: check this errno */
194 1.35 dholland if (errno == EOPNOTSUPP || errno == ENXIO) {
195 1.35 dholland continue;
196 1.35 dholland }
197 1.35 dholland warn("%s: quota_open", fst[i].f_mntonname);
198 1.1 cgd continue;
199 1.1 cgd }
200 1.35 dholland
201 1.35 dholland if (gflag)
202 1.35 dholland errs += repquota(qh, QUOTA_IDTYPE_GROUP);
203 1.35 dholland if (uflag)
204 1.35 dholland errs += repquota(qh, QUOTA_IDTYPE_USER);
205 1.35 dholland
206 1.35 dholland quota_close(qh);
207 1.1 cgd }
208 1.26 bouyer if (xflag)
209 1.26 bouyer exportquotas();
210 1.1 cgd for (i = 0; i < argc; i++)
211 1.27 christos if ((done & (1U << i)) == 0)
212 1.27 christos warnx("%s not mounted", argv[i]);
213 1.27 christos return errs;
214 1.1 cgd }
215 1.1 cgd
216 1.27 christos static void
217 1.27 christos usage(void)
218 1.27 christos {
219 1.27 christos const char *p = getprogname();
220 1.27 christos fprintf(stderr, "usage: %s [-D] [-v] [-g] [-u] -a\n"
221 1.27 christos "\t%s [-D] [-v] [-g] [-u] filesys ...\n"
222 1.27 christos "\t%s -x [-D] [-g] [-u] filesys\n", p, p, p);
223 1.1 cgd exit(1);
224 1.1 cgd }
225 1.1 cgd
226 1.27 christos static int
227 1.35 dholland repquota(struct quotahandle *qh, int idtype)
228 1.26 bouyer {
229 1.36 dholland struct quotacursor *qc;
230 1.36 dholland struct quotakey qk;
231 1.36 dholland struct quotaval qv;
232 1.35 dholland struct quotaval *qvp;
233 1.35 dholland struct fileusage *fup;
234 1.35 dholland
235 1.36 dholland qc = quota_opencursor(qh);
236 1.36 dholland if (qc == NULL) {
237 1.35 dholland return 1;
238 1.35 dholland }
239 1.26 bouyer
240 1.36 dholland if (idtype == QUOTA_IDTYPE_USER) {
241 1.36 dholland quotacursor_skipidtype(qc, QUOTA_IDTYPE_GROUP);
242 1.36 dholland }
243 1.36 dholland if (idtype == QUOTA_IDTYPE_GROUP) {
244 1.36 dholland quotacursor_skipidtype(qc, QUOTA_IDTYPE_USER);
245 1.36 dholland }
246 1.36 dholland
247 1.36 dholland valid[idtype] = 0;
248 1.36 dholland while (!quotacursor_atend(qc)) {
249 1.36 dholland if (quotacursor_get(qc, &qk, &qv)) {
250 1.36 dholland err(1, "%s: quotacursor_get", quota_getmountpoint(qh));
251 1.36 dholland }
252 1.36 dholland if (qk.qk_idtype != idtype) {
253 1.36 dholland continue;
254 1.36 dholland }
255 1.36 dholland
256 1.36 dholland valid[idtype] = 1;
257 1.36 dholland if (qk.qk_id == QUOTA_DEFAULTID) {
258 1.36 dholland qvp = defaultqv[idtype];
259 1.36 dholland } else {
260 1.36 dholland if ((fup = lookup(qk.qk_id, idtype)) == 0)
261 1.36 dholland fup = addid(qk.qk_id, idtype, (char *)0);
262 1.36 dholland qvp = fup->fu_qv;
263 1.36 dholland }
264 1.36 dholland if (qk.qk_objtype == QUOTA_OBJTYPE_BLOCKS) {
265 1.36 dholland qvp[QUOTA_OBJTYPE_BLOCKS] = qv;
266 1.36 dholland } else if (qk.qk_objtype == QUOTA_OBJTYPE_FILES) {
267 1.36 dholland qvp[QUOTA_OBJTYPE_FILES] = qv;
268 1.26 bouyer }
269 1.36 dholland }
270 1.35 dholland
271 1.34 dholland if (xflag == 0 && valid[idtype])
272 1.35 dholland printquotas(idtype, qh);
273 1.35 dholland
274 1.27 christos return 0;
275 1.26 bouyer }
276 1.26 bouyer
277 1.27 christos static void
278 1.35 dholland printquotas(int idtype, struct quotahandle *qh)
279 1.26 bouyer {
280 1.26 bouyer static int multiple = 0;
281 1.27 christos uint32_t id;
282 1.26 bouyer int i;
283 1.26 bouyer struct fileusage *fup;
284 1.33 dholland struct quotaval *q;
285 1.38 dholland const char *timemsg[REPQUOTA_NUMOBJTYPES];
286 1.38 dholland char overchar[REPQUOTA_NUMOBJTYPES];
287 1.27 christos time_t now;
288 1.29 bouyer char b0[2][20], b1[20], b2[20], b3[20];
289 1.38 dholland int ok, objtype;
290 1.38 dholland int isbytes, width;
291 1.26 bouyer
292 1.34 dholland switch (idtype) {
293 1.34 dholland case QUOTA_IDTYPE_GROUP:
294 1.26 bouyer {
295 1.26 bouyer struct group *gr;
296 1.26 bouyer setgrent();
297 1.26 bouyer while ((gr = getgrent()) != 0)
298 1.34 dholland (void)addid(gr->gr_gid, idtype, gr->gr_name);
299 1.26 bouyer endgrent();
300 1.26 bouyer break;
301 1.26 bouyer }
302 1.34 dholland case QUOTA_IDTYPE_USER:
303 1.26 bouyer {
304 1.26 bouyer struct passwd *pw;
305 1.26 bouyer setpwent();
306 1.26 bouyer while ((pw = getpwent()) != 0)
307 1.34 dholland (void)addid(pw->pw_uid, idtype, pw->pw_name);
308 1.26 bouyer endpwent();
309 1.26 bouyer break;
310 1.26 bouyer }
311 1.26 bouyer default:
312 1.34 dholland errx(1, "Unknown quota ID type %d", idtype);
313 1.1 cgd }
314 1.26 bouyer
315 1.27 christos time(&now);
316 1.26 bouyer
317 1.26 bouyer if (multiple++)
318 1.26 bouyer printf("\n");
319 1.26 bouyer if (vflag)
320 1.35 dholland printf("*** Report for %s quotas on %s (%s: %s)\n",
321 1.38 dholland repquota_idtype_names[idtype], quota_getmountpoint(qh),
322 1.35 dholland quota_getmountdevice(qh), quota_getimplname(qh));
323 1.27 christos printf(" Block limits "
324 1.27 christos "File limits\n");
325 1.34 dholland printf(idtype == QUOTA_IDTYPE_USER ? "User " : "Group");
326 1.27 christos printf(" used soft hard grace used"
327 1.39 dholland " soft hard grace\n");
328 1.34 dholland for (id = 0; id <= highid[idtype]; id++) {
329 1.34 dholland fup = qremove(id, idtype);
330 1.33 dholland q = fup->fu_qv;
331 1.1 cgd if (fup == 0)
332 1.1 cgd continue;
333 1.38 dholland for (i = 0; i < REPQUOTA_NUMOBJTYPES; i++) {
334 1.41 dholland if (isover(&q[i], now)) {
335 1.29 bouyer timemsg[i] = timeprt(b0[i], 8, now,
336 1.33 dholland q[i].qv_expiretime);
337 1.26 bouyer overchar[i] = '+';
338 1.41 dholland } else {
339 1.35 dholland if (vflag && q[i].qv_grace != QUOTA_NOTIME) {
340 1.35 dholland timemsg[i] = timeprt(b0[i], 8, 0,
341 1.35 dholland q[i].qv_grace);
342 1.35 dholland } else {
343 1.35 dholland timemsg[i] = "";
344 1.35 dholland }
345 1.26 bouyer overchar[i] = '-';
346 1.26 bouyer }
347 1.26 bouyer }
348 1.26 bouyer
349 1.38 dholland ok = 1;
350 1.38 dholland for (objtype = 0; objtype < REPQUOTA_NUMOBJTYPES; objtype++) {
351 1.38 dholland if (q[objtype].qv_usage != 0 ||
352 1.38 dholland overchar[objtype] != '-') {
353 1.38 dholland ok = 0;
354 1.38 dholland }
355 1.38 dholland }
356 1.38 dholland if (ok && vflag == 0)
357 1.1 cgd continue;
358 1.22 jdolecek if (strlen(fup->fu_name) > 9)
359 1.22 jdolecek printf("%s ", fup->fu_name);
360 1.22 jdolecek else
361 1.22 jdolecek printf("%-10s", fup->fu_name);
362 1.38 dholland for (objtype = 0; objtype < REPQUOTA_NUMOBJTYPES; objtype++) {
363 1.38 dholland printf("%c", overchar[objtype]);
364 1.38 dholland }
365 1.38 dholland for (objtype = 0; objtype < REPQUOTA_NUMOBJTYPES; objtype++) {
366 1.38 dholland isbytes = quota_objtype_isbytes(qh, objtype);
367 1.38 dholland width = isbytes ? 9 : 8;
368 1.38 dholland printf("%*s%*s%*s%7s",
369 1.38 dholland width,
370 1.38 dholland intprt(b1, width+1, q[objtype].qv_usage,
371 1.38 dholland isbytes ? HN_B : 0, hflag),
372 1.38 dholland width,
373 1.38 dholland intprt(b2, width+1, q[objtype].qv_softlimit,
374 1.38 dholland isbytes ? HN_B : 0, hflag),
375 1.38 dholland width,
376 1.38 dholland intprt(b3, width+1, q[objtype].qv_hardlimit,
377 1.38 dholland isbytes ? HN_B : 0, hflag),
378 1.38 dholland timemsg[objtype]);
379 1.38 dholland
380 1.38 dholland if (objtype + 1 < REPQUOTA_NUMOBJTYPES) {
381 1.38 dholland printf(" ");
382 1.38 dholland } else {
383 1.38 dholland printf("\n");
384 1.38 dholland }
385 1.38 dholland }
386 1.26 bouyer free(fup);
387 1.26 bouyer }
388 1.26 bouyer }
389 1.26 bouyer
390 1.27 christos static void
391 1.40 dholland exportquotaval(const struct quotaval *qv)
392 1.26 bouyer {
393 1.40 dholland if (qv->qv_hardlimit == QUOTA_NOLIMIT) {
394 1.40 dholland printf(" -");
395 1.40 dholland } else {
396 1.40 dholland printf(" %llu", (unsigned long long)qv->qv_hardlimit);
397 1.40 dholland }
398 1.40 dholland
399 1.40 dholland if (qv->qv_softlimit == QUOTA_NOLIMIT) {
400 1.40 dholland printf(" -");
401 1.40 dholland } else {
402 1.40 dholland printf(" %llu", (unsigned long long)qv->qv_softlimit);
403 1.40 dholland }
404 1.26 bouyer
405 1.40 dholland printf(" %llu", (unsigned long long)qv->qv_usage);
406 1.26 bouyer
407 1.40 dholland if (qv->qv_expiretime == QUOTA_NOTIME) {
408 1.40 dholland printf(" -");
409 1.40 dholland } else {
410 1.40 dholland printf(" %lld", (long long)qv->qv_expiretime);
411 1.26 bouyer }
412 1.26 bouyer
413 1.40 dholland if (qv->qv_grace == QUOTA_NOTIME) {
414 1.40 dholland printf(" -");
415 1.40 dholland } else {
416 1.40 dholland printf(" %lld", (long long)qv->qv_grace);
417 1.40 dholland }
418 1.40 dholland }
419 1.40 dholland
420 1.40 dholland static void
421 1.40 dholland exportquotas(void)
422 1.40 dholland {
423 1.40 dholland int idtype;
424 1.40 dholland id_t id;
425 1.40 dholland struct fileusage *fup;
426 1.40 dholland
427 1.40 dholland /* header */
428 1.40 dholland printf("@format netbsd-quota-dump v1\n");
429 1.40 dholland printf("# idtype id objtype hard soft usage expire grace\n");
430 1.26 bouyer
431 1.38 dholland for (idtype = 0; idtype < REPQUOTA_NUMIDTYPES; idtype++) {
432 1.34 dholland if (valid[idtype] == 0)
433 1.26 bouyer continue;
434 1.40 dholland
435 1.40 dholland printf("%s default blocks ", repquota_idtype_names[idtype]);
436 1.40 dholland exportquotaval(&defaultqv[idtype][QUOTA_OBJTYPE_BLOCKS]);
437 1.40 dholland printf("\n");
438 1.40 dholland
439 1.40 dholland printf("%s default files ", repquota_idtype_names[idtype]);
440 1.40 dholland exportquotaval(&defaultqv[idtype][QUOTA_OBJTYPE_FILES]);
441 1.40 dholland printf("\n");
442 1.26 bouyer
443 1.34 dholland for (id = 0; id <= highid[idtype]; id++) {
444 1.34 dholland fup = qremove(id, idtype);
445 1.26 bouyer if (fup == 0)
446 1.26 bouyer continue;
447 1.40 dholland
448 1.40 dholland printf("%s %u blocks ", repquota_idtype_names[idtype],
449 1.40 dholland id);
450 1.40 dholland exportquotaval(&fup->fu_qv[QUOTA_OBJTYPE_BLOCKS]);
451 1.40 dholland printf("\n");
452 1.40 dholland
453 1.40 dholland printf("%s %u files ", repquota_idtype_names[idtype],
454 1.40 dholland id);
455 1.40 dholland exportquotaval(&fup->fu_qv[QUOTA_OBJTYPE_FILES]);
456 1.40 dholland printf("\n");
457 1.40 dholland
458 1.26 bouyer free(fup);
459 1.26 bouyer }
460 1.1 cgd }
461 1.40 dholland printf("@end\n");
462 1.1 cgd }
463 1.1 cgd
464 1.1 cgd /*
465 1.1 cgd * Routines to manage the file usage table.
466 1.1 cgd *
467 1.34 dholland * Lookup an id of a specific id type.
468 1.1 cgd */
469 1.1 cgd struct fileusage *
470 1.34 dholland lookup(uint32_t id, int idtype)
471 1.1 cgd {
472 1.11 lukem struct fileusage *fup;
473 1.1 cgd
474 1.34 dholland for (fup = fuhead[idtype][id & (FUHASH-1)]; fup != 0; fup = fup->fu_next)
475 1.1 cgd if (fup->fu_id == id)
476 1.27 christos return fup;
477 1.27 christos return NULL;
478 1.1 cgd }
479 1.26 bouyer /*
480 1.34 dholland * Lookup and remove an id of a specific id type.
481 1.26 bouyer */
482 1.27 christos static struct fileusage *
483 1.34 dholland qremove(uint32_t id, int idtype)
484 1.26 bouyer {
485 1.26 bouyer struct fileusage *fup, **fupp;
486 1.26 bouyer
487 1.34 dholland for (fupp = &fuhead[idtype][id & (FUHASH-1)]; *fupp != 0;) {
488 1.26 bouyer fup = *fupp;
489 1.26 bouyer if (fup->fu_id == id) {
490 1.26 bouyer *fupp = fup->fu_next;
491 1.27 christos return fup;
492 1.26 bouyer }
493 1.26 bouyer fupp = &fup->fu_next;
494 1.26 bouyer }
495 1.27 christos return NULL;
496 1.26 bouyer }
497 1.1 cgd
498 1.1 cgd /*
499 1.1 cgd * Add a new file usage id if it does not already exist.
500 1.1 cgd */
501 1.27 christos static struct fileusage *
502 1.34 dholland addid(uint32_t id, int idtype, const char *name)
503 1.1 cgd {
504 1.1 cgd struct fileusage *fup, **fhp;
505 1.26 bouyer struct group *gr = NULL;
506 1.26 bouyer struct passwd *pw = NULL;
507 1.27 christos size_t len;
508 1.1 cgd
509 1.34 dholland if ((fup = lookup(id, idtype)) != NULL) {
510 1.27 christos return fup;
511 1.26 bouyer }
512 1.26 bouyer if (name == NULL) {
513 1.34 dholland switch(idtype) {
514 1.34 dholland case QUOTA_IDTYPE_GROUP:
515 1.26 bouyer gr = getgrgid(id);
516 1.26 bouyer
517 1.26 bouyer if (gr != NULL)
518 1.26 bouyer name = gr->gr_name;
519 1.26 bouyer break;
520 1.34 dholland case QUOTA_IDTYPE_USER:
521 1.26 bouyer pw = getpwuid(id);
522 1.26 bouyer if (pw)
523 1.26 bouyer name = pw->pw_name;
524 1.26 bouyer break;
525 1.26 bouyer default:
526 1.34 dholland errx(1, "Unknown quota ID type %d\n", idtype);
527 1.26 bouyer }
528 1.26 bouyer }
529 1.26 bouyer
530 1.1 cgd if (name)
531 1.1 cgd len = strlen(name);
532 1.1 cgd else
533 1.1 cgd len = 10;
534 1.27 christos if ((fup = calloc(1, sizeof(*fup) + len)) == NULL)
535 1.27 christos err(1, "out of memory for fileusage structures");
536 1.34 dholland fhp = &fuhead[idtype][id & (FUHASH - 1)];
537 1.1 cgd fup->fu_next = *fhp;
538 1.1 cgd *fhp = fup;
539 1.1 cgd fup->fu_id = id;
540 1.34 dholland if (id > highid[idtype])
541 1.34 dholland highid[idtype] = id;
542 1.1 cgd if (name) {
543 1.12 lukem memmove(fup->fu_name, name, len + 1);
544 1.1 cgd } else {
545 1.28 christos snprintf(fup->fu_name, len + 1, "%u", id);
546 1.1 cgd }
547 1.36 dholland /*
548 1.36 dholland * XXX nothing guarantees the default limits have been loaded yet
549 1.36 dholland */
550 1.38 dholland fup->fu_qv[QUOTA_OBJTYPE_BLOCKS] = defaultqv[idtype][QUOTA_OBJTYPE_BLOCKS];
551 1.38 dholland fup->fu_qv[QUOTA_OBJTYPE_FILES] = defaultqv[idtype][QUOTA_OBJTYPE_FILES];
552 1.27 christos return fup;
553 1.1 cgd }
554 1.37 dholland
555 1.37 dholland /*
556 1.37 dholland * Check to see if target appears in list of size cnt.
557 1.37 dholland */
558 1.37 dholland static int
559 1.37 dholland oneof(const char *target, char *list[], int cnt)
560 1.37 dholland {
561 1.37 dholland int i;
562 1.37 dholland
563 1.37 dholland for (i = 0; i < cnt; i++)
564 1.37 dholland if (strcmp(target, list[i]) == 0)
565 1.37 dholland return i;
566 1.37 dholland return -1;
567 1.37 dholland }
568 1.41 dholland
569 1.41 dholland static int
570 1.41 dholland isover(struct quotaval *qv, time_t now)
571 1.41 dholland {
572 1.41 dholland return (qv->qv_usage >= qv->qv_hardlimit ||
573 1.41 dholland qv->qv_usage >= qv->qv_softlimit);
574 1.41 dholland }
575