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