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