Home | History | Annotate | Line # | Download | only in repquota
repquota.c revision 1.35
      1 /*	$NetBSD: repquota.c,v 1.35 2012/01/09 15:38:59 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[] = "@(#)repquota.c	8.2 (Berkeley) 11/22/94";
     44 #else
     45 __RCSID("$NetBSD: repquota.c,v 1.35 2012/01/09 15:38:59 dholland Exp $");
     46 #endif
     47 #endif /* not lint */
     48 
     49 /*
     50  * Quota report
     51  */
     52 #include <sys/param.h>
     53 #include <sys/stat.h>
     54 #include <sys/types.h>
     55 #include <sys/statvfs.h>
     56 
     57 #include <errno.h>
     58 #include <err.h>
     59 #include <fstab.h>
     60 #include <grp.h>
     61 #include <pwd.h>
     62 #include <stdio.h>
     63 #include <stdlib.h>
     64 #include <string.h>
     65 #include <unistd.h>
     66 
     67 #include <quota/quota.h>
     68 #include <ufs/ufs/quota1.h>
     69 #include <quota.h>
     70 
     71 #include "printquota.h"
     72 #include "quotautil.h"
     73 
     74 struct fileusage {
     75 	struct	fileusage *fu_next;
     76 	struct	quotaval fu_qv[QUOTA_NLIMITS];
     77 	uint32_t	fu_id;
     78 	char	fu_name[1];
     79 	/* actually bigger */
     80 };
     81 #define FUHASH 1024	/* must be power of two */
     82 static struct fileusage *fuhead[QUOTA_NCLASS][FUHASH];
     83 static uint32_t highid[QUOTA_NCLASS];	/* highest addid()'ed identifier per class */
     84 int valid[QUOTA_NCLASS];
     85 static struct quotaval defaultqv[QUOTA_NCLASS][QUOTA_NLIMITS];
     86 
     87 static int	vflag = 0;		/* verbose */
     88 static int	aflag = 0;		/* all file systems */
     89 static int	Dflag = 0;		/* debug */
     90 static int	hflag = 0;		/* humanize */
     91 static int	xflag = 0;		/* export */
     92 
     93 
     94 static struct fileusage *addid(uint32_t, int, const char *);
     95 static struct fileusage *lookup(uint32_t, int);
     96 static struct fileusage *qremove(uint32_t, int);
     97 static int	repquota(struct quotahandle *, int);
     98 static int	repquota2(struct quotahandle *, int);
     99 static int	repquota1(struct quotahandle *, int);
    100 static void	usage(void) __attribute__((__noreturn__));
    101 static void	printquotas(int, struct quotahandle *);
    102 static void	exportquotas(void);
    103 
    104 int
    105 main(int argc, char **argv)
    106 {
    107 	int gflag = 0, uflag = 0, errs = 0;
    108 	long i, argnum, done = 0;
    109 	int ch;
    110 	struct statvfs *fst;
    111 	int nfst;
    112 	struct quotahandle *qh;
    113 
    114 	while ((ch = getopt(argc, argv, "Daguhvx")) != -1) {
    115 		switch(ch) {
    116 		case 'a':
    117 			aflag++;
    118 			break;
    119 		case 'g':
    120 			gflag++;
    121 			break;
    122 		case 'u':
    123 			uflag++;
    124 			break;
    125 		case 'h':
    126 			hflag++;
    127 			break;
    128 		case 'v':
    129 			vflag++;
    130 			break;
    131 		case 'D':
    132 			Dflag++;
    133 			break;
    134 		case 'x':
    135 			xflag++;
    136 			break;
    137 		default:
    138 			usage();
    139 		}
    140 	}
    141 	argc -= optind;
    142 	argv += optind;
    143 	if (xflag && (argc != 1 || aflag))
    144 		usage();
    145 	if (argc == 0 && !aflag)
    146 		usage();
    147 	if (!gflag && !uflag) {
    148 		if (aflag)
    149 			gflag++;
    150 		uflag++;
    151 	}
    152 
    153 	nfst = getmntinfo(&fst, MNT_WAIT);
    154 	if (nfst == 0)
    155 		errx(1, "no filesystems mounted!");
    156 	for (i = 0; i < nfst; i++) {
    157 		if ((fst[i].f_flag & ST_QUOTA) == 0)
    158 			continue;
    159 		/* check if we want this volume */
    160 		if (!aflag) {
    161 			argnum = oneof(fst[i].f_mntonname, argv, argc);
    162 			if (argnum < 0) {
    163 				argnum = oneof(fst[i].f_mntfromname,
    164 					       argv, argc);
    165 			}
    166 			if (argnum < 0) {
    167 				continue;
    168 			}
    169 			done |= 1U << argnum;
    170 		}
    171 
    172 		qh = quota_open(fst[i].f_mntonname);
    173 		if (qh == NULL) {
    174 			/* XXX: check this errno */
    175 			if (errno == EOPNOTSUPP || errno == ENXIO) {
    176 				continue;
    177 			}
    178 			warn("%s: quota_open", fst[i].f_mntonname);
    179 			continue;
    180 		}
    181 
    182 		if (gflag)
    183 			errs += repquota(qh, QUOTA_IDTYPE_GROUP);
    184 		if (uflag)
    185 			errs += repquota(qh, QUOTA_IDTYPE_USER);
    186 
    187 		quota_close(qh);
    188 	}
    189 	if (xflag)
    190 		exportquotas();
    191 	for (i = 0; i < argc; i++)
    192 		if ((done & (1U << i)) == 0)
    193 			warnx("%s not mounted", argv[i]);
    194 	return errs;
    195 }
    196 
    197 static void
    198 usage(void)
    199 {
    200 	const char *p = getprogname();
    201 	fprintf(stderr, "usage: %s [-D] [-v] [-g] [-u] -a\n"
    202 		"\t%s [-D] [-v] [-g] [-u] filesys ...\n"
    203 		"\t%s -x [-D] [-g] [-u] filesys\n", p, p, p);
    204 	exit(1);
    205 }
    206 
    207 static int
    208 repquota(struct quotahandle *qh, int idtype)
    209 {
    210 	if (repquota2(qh, idtype) != 0)
    211 		return repquota1(qh, idtype);
    212 	return 0;
    213 }
    214 
    215 static int
    216 repquota2_getstuff(struct quotahandle *qh, int idtype, prop_array_t *ret)
    217 {
    218 	prop_dictionary_t dict, cmd;
    219 	prop_array_t cmds, datas;
    220 	struct plistref pref;
    221 	int8_t error8;
    222 
    223 	dict = quota_prop_create();
    224 	cmds = prop_array_create();
    225 	datas = prop_array_create();
    226 
    227 	if (dict == NULL || cmds == NULL || datas == NULL)
    228 		errx(1, "can't allocate proplist");
    229 	if (!quota_prop_add_command(cmds, "getall",
    230 	    ufs_quota_class_names[idtype], datas))
    231 		err(1, "prop_add_command");
    232 	if (!prop_dictionary_set(dict, "commands", cmds))
    233 		err(1, "prop_dictionary_set(command)");
    234 	if (Dflag)
    235 		printf("message to kernel:\n%s\n",
    236 		    prop_dictionary_externalize(dict));
    237 	if (prop_dictionary_send_syscall(dict, &pref) != 0)
    238 		err(1, "prop_dictionary_send_syscall");
    239 	prop_object_release(dict);
    240 
    241 	if (quotactl(quota_getmountpoint(qh), &pref) != 0)
    242 		err(1, "quotactl");
    243 
    244 	if (prop_dictionary_recv_syscall(&pref, &dict) != 0) {
    245 		err(1, "prop_dictionary_recv_syscall");
    246 	}
    247 	if (Dflag)
    248 		printf("reply from kernel:\n%s\n",
    249 		    prop_dictionary_externalize(dict));
    250 	if ((errno = quota_get_cmds(dict, &cmds)) != 0) {
    251 		err(1, "quota_get_cmds");
    252 	}
    253 
    254 	cmd = prop_array_get(cmds, 0);
    255 	if (cmd == NULL) {
    256 		err(1, "prop_array_get(cmds)");
    257 	}
    258 
    259 	const char *cmdstr;
    260 	if (!prop_dictionary_get_cstring_nocopy(cmd, "command",
    261 	    &cmdstr))
    262 		err(1, "prop_get(command)");
    263 
    264 	if (!prop_dictionary_get_int8(cmd, "return", &error8))
    265 		err(1, "prop_get(return)");
    266 
    267 	if (error8) {
    268 		prop_object_release(dict);
    269 		if (error8 != EOPNOTSUPP) {
    270 			errno = error8;
    271 			warn("get %s quotas",
    272 			    ufs_quota_class_names[idtype]);
    273 		}
    274 		return 1;
    275 	}
    276 	datas = prop_dictionary_get(cmd, "data");
    277 	if (datas == NULL)
    278 		err(1, "prop_dict_get(datas)");
    279 
    280 	prop_object_retain(datas);
    281 	prop_object_release(dict);
    282 
    283 	*ret = datas;
    284 	return 0;
    285 }
    286 
    287 static int
    288 repquota2(struct quotahandle *qh, int idtype)
    289 {
    290 	prop_dictionary_t data;
    291 	prop_array_t datas;
    292 	prop_object_iterator_t dataiter;
    293 	struct quotaval *qvp;
    294 	struct fileusage *fup;
    295 	const char *strid;
    296 	uint32_t id;
    297 	uint64_t *values[QUOTA_NLIMITS];
    298 
    299 	if (repquota2_getstuff(qh, idtype, &datas)) {
    300 		return 1;
    301 	}
    302 
    303 		dataiter = prop_array_iterator(datas);
    304 		if (dataiter == NULL)
    305 			err(1, "prop_array_iterator");
    306 
    307 		valid[idtype] = 0;
    308 		while ((data = prop_object_iterator_next(dataiter)) != NULL) {
    309 			valid[idtype] = 1;
    310 			strid = NULL;
    311 			if (!prop_dictionary_get_uint32(data, "id", &id)) {
    312 				if (!prop_dictionary_get_cstring_nocopy(data,
    313 				    "id", &strid))
    314 					errx(1, "can't find id in quota entry");
    315 				if (strcmp(strid, "default") != 0) {
    316 					errx(1,
    317 					    "wrong id string %s in quota entry",
    318 					    strid);
    319 				}
    320 				qvp = defaultqv[idtype];
    321 			} else {
    322 				if ((fup = lookup(id, idtype)) == 0)
    323 					fup = addid(id, idtype, (char *)0);
    324 				qvp = fup->fu_qv;
    325 			}
    326 			values[QUOTA_LIMIT_BLOCK] =
    327 			    &qvp[QUOTA_LIMIT_BLOCK].qv_hardlimit;
    328 			values[QUOTA_LIMIT_FILE] =
    329 			    &qvp[QUOTA_LIMIT_FILE].qv_hardlimit;
    330 
    331 			errno = proptoquota64(data, values,
    332 			    ufs_quota_entry_names, UFS_QUOTA_NENTRIES,
    333 			    ufs_quota_limit_names, QUOTA_NLIMITS);
    334 			if (errno)
    335 				err(1, "proptoquota64");
    336 		}
    337 		prop_object_iterator_release(dataiter);
    338 
    339 	if (xflag == 0 && valid[idtype])
    340 		printquotas(idtype, qh);
    341 
    342 	prop_object_release(datas);
    343 
    344 	return 0;
    345 }
    346 
    347 static int
    348 repquota1(struct quotahandle *qh, int idtype)
    349 {
    350 	char qfpathname[MAXPATHLEN];
    351 	struct fstab *fs;
    352 	struct fileusage *fup;
    353 	FILE *qf;
    354 	uint32_t id;
    355 	struct dqblk dqbuf;
    356 	time_t bgrace = MAX_DQ_TIME, igrace = MAX_DQ_TIME;
    357 	int type = ufsclass2qtype(idtype);
    358 	const char *mountpoint;
    359 
    360 	mountpoint = quota_getmountpoint(qh);
    361 
    362 	setfsent();
    363 	while ((fs = getfsent()) != NULL) {
    364 		if (strcmp(fs->fs_vfstype, "ffs") == 0 &&
    365 		   strcmp(fs->fs_file, mountpoint) == 0)
    366 			break;
    367 	}
    368 	endfsent();
    369 	if (fs == NULL) {
    370 		warnx("%s not found in fstab", mountpoint);
    371 		return 1;
    372 	}
    373 	if (!hasquota(qfpathname, sizeof(qfpathname), fs, type))
    374 		return 0;
    375 
    376 	if ((qf = fopen(qfpathname, "r")) == NULL) {
    377 		warn("Cannot open `%s'", qfpathname);
    378 		return 1;
    379 	}
    380 	for (id = 0; ; id++) {
    381 		fread(&dqbuf, sizeof(struct dqblk), 1, qf);
    382 		if (feof(qf))
    383 			break;
    384 		if (id == 0) {
    385 			if (dqbuf.dqb_btime > 0)
    386 				bgrace = dqbuf.dqb_btime;
    387 			if (dqbuf.dqb_itime > 0)
    388 				igrace = dqbuf.dqb_itime;
    389 		}
    390 		if (dqbuf.dqb_curinodes == 0 && dqbuf.dqb_curblocks == 0 &&
    391 		    dqbuf.dqb_bsoftlimit == 0 && dqbuf.dqb_bhardlimit == 0 &&
    392 		    dqbuf.dqb_isoftlimit == 0 && dqbuf.dqb_ihardlimit == 0)
    393 			continue;
    394 		if ((fup = lookup(id, idtype)) == 0)
    395 			fup = addid(id, idtype, (char *)0);
    396 		dqblk_to_quotaval(&dqbuf, fup->fu_qv);
    397 		fup->fu_qv[QUOTA_LIMIT_BLOCK].qv_grace = bgrace;
    398 		fup->fu_qv[QUOTA_LIMIT_FILE].qv_grace = igrace;
    399 	}
    400 	defaultqv[idtype][QUOTA_LIMIT_BLOCK].qv_grace = bgrace;
    401 	defaultqv[idtype][QUOTA_LIMIT_FILE].qv_grace = igrace;
    402 	defaultqv[idtype][QUOTA_LIMIT_BLOCK].qv_softlimit =
    403 	    defaultqv[idtype][QUOTA_LIMIT_BLOCK].qv_hardlimit =
    404 	    defaultqv[idtype][QUOTA_LIMIT_FILE].qv_softlimit =
    405 	    defaultqv[idtype][QUOTA_LIMIT_FILE].qv_hardlimit = QUOTA_NOLIMIT;
    406 	fclose(qf);
    407 	valid[idtype] = 1;
    408 	if (xflag == 0)
    409 		printquotas(idtype, qh);
    410 	return 0;
    411 }
    412 
    413 static void
    414 printquotas(int idtype, struct quotahandle *qh)
    415 {
    416 	static int multiple = 0;
    417 	uint32_t id;
    418 	int i;
    419 	struct fileusage *fup;
    420 	struct quotaval *q;
    421 	const char *timemsg[QUOTA_NLIMITS];
    422 	char overchar[QUOTA_NLIMITS];
    423 	time_t now;
    424 	char b0[2][20], b1[20], b2[20], b3[20];
    425 
    426 	switch (idtype) {
    427 	case QUOTA_IDTYPE_GROUP:
    428 		{
    429 		struct group *gr;
    430 		setgrent();
    431 		while ((gr = getgrent()) != 0)
    432 			(void)addid(gr->gr_gid, idtype, gr->gr_name);
    433 		endgrent();
    434 		break;
    435 		}
    436 	case QUOTA_IDTYPE_USER:
    437 		{
    438 		struct passwd *pw;
    439 		setpwent();
    440 		while ((pw = getpwent()) != 0)
    441 			(void)addid(pw->pw_uid, idtype, pw->pw_name);
    442 		endpwent();
    443 		break;
    444 		}
    445 	default:
    446 		errx(1, "Unknown quota ID type %d", idtype);
    447 	}
    448 
    449 	time(&now);
    450 
    451 	if (multiple++)
    452 		printf("\n");
    453 	if (vflag)
    454 		printf("*** Report for %s quotas on %s (%s: %s)\n",
    455 		    ufs_quota_class_names[idtype], quota_getmountpoint(qh),
    456 		    quota_getmountdevice(qh), quota_getimplname(qh));
    457 	printf("                        Block limits               "
    458 	    "File limits\n");
    459 	printf(idtype == QUOTA_IDTYPE_USER ? "User " : "Group");
    460 	printf("            used     soft     hard  grace      used"
    461 	    "soft    hard  grace\n");
    462 	for (id = 0; id <= highid[idtype]; id++) {
    463 		fup = qremove(id, idtype);
    464 		q = fup->fu_qv;
    465 		if (fup == 0)
    466 			continue;
    467 		for (i = 0; i < QUOTA_NLIMITS; i++) {
    468 			switch (QL_STATUS(quota_check_limit(q[i].qv_usage, 1,
    469 			    q[i].qv_softlimit, q[i].qv_hardlimit,
    470 			    q[i].qv_expiretime, now))) {
    471 			case QL_S_DENY_HARD:
    472 			case QL_S_DENY_GRACE:
    473 			case QL_S_ALLOW_SOFT:
    474 				timemsg[i] = timeprt(b0[i], 8, now,
    475 				    q[i].qv_expiretime);
    476 				overchar[i] = '+';
    477 				break;
    478 			default:
    479 				if (vflag && q[i].qv_grace != QUOTA_NOTIME) {
    480 					timemsg[i] = timeprt(b0[i], 8, 0,
    481 							     q[i].qv_grace);
    482 				} else {
    483 					timemsg[i] = "";
    484 				}
    485 				overchar[i] = '-';
    486 				break;
    487 			}
    488 		}
    489 
    490 		if (q[QUOTA_LIMIT_BLOCK].qv_usage == 0 &&
    491 		    q[QUOTA_LIMIT_FILE].qv_usage == 0 && vflag == 0 &&
    492 		    overchar[QUOTA_LIMIT_BLOCK] == '-' &&
    493 		    overchar[QUOTA_LIMIT_FILE] == '-')
    494 			continue;
    495 		if (strlen(fup->fu_name) > 9)
    496 			printf("%s ", fup->fu_name);
    497 		else
    498 			printf("%-10s", fup->fu_name);
    499 		printf("%c%c%9s%9s%9s%7s",
    500 			overchar[QUOTA_LIMIT_BLOCK], overchar[QUOTA_LIMIT_FILE],
    501 			intprt(b1, 10, q[QUOTA_LIMIT_BLOCK].qv_usage,
    502 			  HN_B, hflag),
    503 			intprt(b2, 10, q[QUOTA_LIMIT_BLOCK].qv_softlimit,
    504 			  HN_B, hflag),
    505 			intprt(b3, 10, q[QUOTA_LIMIT_BLOCK].qv_hardlimit,
    506 			  HN_B, hflag),
    507 			timemsg[QUOTA_LIMIT_BLOCK]);
    508 		printf("  %8s%8s%8s%7s\n",
    509 			intprt(b1, 9, q[QUOTA_LIMIT_FILE].qv_usage, 0, hflag),
    510 			intprt(b2, 9, q[QUOTA_LIMIT_FILE].qv_softlimit,
    511 			  0, hflag),
    512 			intprt(b3, 9, q[QUOTA_LIMIT_FILE].qv_hardlimit,
    513 			  0, hflag),
    514 			timemsg[QUOTA_LIMIT_FILE]);
    515 		free(fup);
    516 	}
    517 }
    518 
    519 static void
    520 exportquotas(void)
    521 {
    522 	uint32_t id;
    523 	struct fileusage *fup;
    524 	prop_dictionary_t dict, data;
    525 	prop_array_t cmds, datas;
    526 	int idtype;
    527 	uint64_t *valuesp[QUOTA_NLIMITS];
    528 
    529 	dict = quota_prop_create();
    530 	cmds = prop_array_create();
    531 
    532 	if (dict == NULL || cmds == NULL) {
    533 		errx(1, "can't allocate proplist");
    534 	}
    535 
    536 
    537 	for (idtype = 0; idtype < QUOTA_NCLASS; idtype++) {
    538 		if (valid[idtype] == 0)
    539 			continue;
    540 		datas = prop_array_create();
    541 		if (datas == NULL)
    542 			errx(1, "can't allocate proplist");
    543 		valuesp[QUOTA_LIMIT_BLOCK] =
    544 		    &defaultqv[idtype][QUOTA_LIMIT_BLOCK].qv_hardlimit;
    545 		valuesp[QUOTA_LIMIT_FILE] =
    546 		    &defaultqv[idtype][QUOTA_LIMIT_FILE].qv_hardlimit;
    547 		data = quota64toprop(0, 1, valuesp,
    548 		    ufs_quota_entry_names, UFS_QUOTA_NENTRIES,
    549 		    ufs_quota_limit_names, QUOTA_NLIMITS);
    550 		if (data == NULL)
    551 			err(1, "quota64toprop(default)");
    552 		if (!prop_array_add_and_rel(datas, data))
    553 			err(1, "prop_array_add(data)");
    554 
    555 		for (id = 0; id <= highid[idtype]; id++) {
    556 			fup = qremove(id, idtype);
    557 			if (fup == 0)
    558 				continue;
    559 			valuesp[QUOTA_LIMIT_BLOCK] =
    560 			    &fup->fu_qv[QUOTA_LIMIT_BLOCK].qv_hardlimit;
    561 			valuesp[QUOTA_LIMIT_FILE] =
    562 			    &fup->fu_qv[QUOTA_LIMIT_FILE].qv_hardlimit;
    563 			data = quota64toprop(id, 0, valuesp,
    564 			    ufs_quota_entry_names, UFS_QUOTA_NENTRIES,
    565 			    ufs_quota_limit_names, QUOTA_NLIMITS);
    566 			if (data == NULL)
    567 				err(1, "quota64toprop(id)");
    568 			if (!prop_array_add_and_rel(datas, data))
    569 				err(1, "prop_array_add(data)");
    570 			free(fup);
    571 		}
    572 
    573 		if (!quota_prop_add_command(cmds, "set",
    574 		    ufs_quota_class_names[idtype], datas))
    575 			err(1, "prop_add_command");
    576 	}
    577 
    578 	if (!prop_dictionary_set(dict, "commands", cmds))
    579 		err(1, "prop_dictionary_set(command)");
    580 
    581 	printf("%s\n", prop_dictionary_externalize(dict));
    582 	return;
    583 }
    584 
    585 /*
    586  * Routines to manage the file usage table.
    587  *
    588  * Lookup an id of a specific id type.
    589  */
    590 struct fileusage *
    591 lookup(uint32_t id, int idtype)
    592 {
    593 	struct fileusage *fup;
    594 
    595 	for (fup = fuhead[idtype][id & (FUHASH-1)]; fup != 0; fup = fup->fu_next)
    596 		if (fup->fu_id == id)
    597 			return fup;
    598 	return NULL;
    599 }
    600 /*
    601  * Lookup and remove an id of a specific id type.
    602  */
    603 static struct fileusage *
    604 qremove(uint32_t id, int idtype)
    605 {
    606 	struct fileusage *fup, **fupp;
    607 
    608 	for (fupp = &fuhead[idtype][id & (FUHASH-1)]; *fupp != 0;) {
    609 		fup = *fupp;
    610 		if (fup->fu_id == id) {
    611 			*fupp = fup->fu_next;
    612 			return fup;
    613 		}
    614 		fupp = &fup->fu_next;
    615 	}
    616 	return NULL;
    617 }
    618 
    619 /*
    620  * Add a new file usage id if it does not already exist.
    621  */
    622 static struct fileusage *
    623 addid(uint32_t id, int idtype, const char *name)
    624 {
    625 	struct fileusage *fup, **fhp;
    626 	struct group *gr = NULL;
    627 	struct passwd *pw = NULL;
    628 	size_t len;
    629 
    630 	if ((fup = lookup(id, idtype)) != NULL) {
    631 		return fup;
    632 	}
    633 	if (name == NULL) {
    634 		switch(idtype) {
    635 		case  QUOTA_IDTYPE_GROUP:
    636 			gr = getgrgid(id);
    637 
    638 			if (gr != NULL)
    639 				name = gr->gr_name;
    640 			break;
    641 		case QUOTA_IDTYPE_USER:
    642 			pw = getpwuid(id);
    643 			if (pw)
    644 				name = pw->pw_name;
    645 			break;
    646 		default:
    647 			errx(1, "Unknown quota ID type %d\n", idtype);
    648 		}
    649 	}
    650 
    651 	if (name)
    652 		len = strlen(name);
    653 	else
    654 		len = 10;
    655 	if ((fup = calloc(1, sizeof(*fup) + len)) == NULL)
    656 		err(1, "out of memory for fileusage structures");
    657 	fhp = &fuhead[idtype][id & (FUHASH - 1)];
    658 	fup->fu_next = *fhp;
    659 	*fhp = fup;
    660 	fup->fu_id = id;
    661 	if (id > highid[idtype])
    662 		highid[idtype] = id;
    663 	if (name) {
    664 		memmove(fup->fu_name, name, len + 1);
    665 	} else {
    666 		snprintf(fup->fu_name, len + 1, "%u", id);
    667 	}
    668 	fup->fu_qv[QUOTA_LIMIT_BLOCK] = defaultqv[idtype][QUOTA_LIMIT_BLOCK];
    669 	fup->fu_qv[QUOTA_LIMIT_FILE] = defaultqv[idtype][QUOTA_LIMIT_FILE];
    670 	return fup;
    671 }
    672