Home | History | Annotate | Line # | Download | only in quotacheck
quotacheck.c revision 1.35
      1 /*	$NetBSD: quotacheck.c,v 1.35 2004/03/27 13:11:47 dsl 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\n\
     38 	The Regents of the University of California.  All rights reserved.\n");
     39 #endif /* not lint */
     40 
     41 #ifndef lint
     42 #if 0
     43 static char sccsid[] = "@(#)quotacheck.c	8.6 (Berkeley) 4/28/95";
     44 #else
     45 __RCSID("$NetBSD: quotacheck.c,v 1.35 2004/03/27 13:11:47 dsl Exp $");
     46 #endif
     47 #endif /* not lint */
     48 
     49 /*
     50  * Fix up / report on disk quotas & usage
     51  */
     52 #include <sys/param.h>
     53 #include <sys/stat.h>
     54 #include <sys/queue.h>
     55 
     56 #include <ufs/ufs/dinode.h>
     57 #include <ufs/ufs/quota.h>
     58 #include <ufs/ufs/ufs_bswap.h>
     59 #include <ufs/ffs/fs.h>
     60 #include <ufs/ffs/ffs_extern.h>
     61 
     62 #include <err.h>
     63 #include <fcntl.h>
     64 #include <fstab.h>
     65 #include <pwd.h>
     66 #include <grp.h>
     67 #include <errno.h>
     68 #include <unistd.h>
     69 #include <stdio.h>
     70 #include <stdlib.h>
     71 #include <string.h>
     72 
     73 #include "fsutil.h"
     74 
     75 static char *qfname = QUOTAFILENAME;
     76 static char *qfextension[] = INITQFNAMES;
     77 static char *quotagroup = QUOTAGROUP;
     78 
     79 static union {
     80 	struct	fs	sblk;
     81 	char	dummy[MAXBSIZE];
     82 } un;
     83 #define	sblock	un.sblk
     84 static long dev_bsize;
     85 static long maxino;
     86 
     87 struct quotaname {
     88 	long	flags;
     89 	char	grpqfname[MAXPATHLEN + 1];
     90 	char	usrqfname[MAXPATHLEN + 1];
     91 };
     92 #define	HASUSR	1
     93 #define	HASGRP	2
     94 
     95 struct fileusage {
     96 	struct	fileusage *fu_next;
     97 	u_long	fu_curinodes;
     98 	u_long	fu_curblocks;
     99 	u_long	fu_id;
    100 	char	fu_name[1];
    101 	/* actually bigger */
    102 };
    103 #define FUHASH 1024	/* must be power of two */
    104 static struct fileusage *fuhead[MAXQUOTAS][FUHASH];
    105 
    106 
    107 union dinode {
    108 	struct ufs1_dinode dp1;
    109 	struct ufs2_dinode dp2;
    110 };
    111 #define DIP(dp, field) \
    112 	(is_ufs2 ? (dp)->dp2.di_##field : (dp)->dp1.di_##field)
    113 
    114 
    115 static int	aflag;		/* all file systems */
    116 static int	gflag;		/* check group quotas */
    117 static int	uflag;		/* check user quotas */
    118 static int	vflag;		/* verbose */
    119 static int	qflag;		/* quick but untidy mode */
    120 static int	fi;		/* open disk file descriptor */
    121 static u_long	highid[MAXQUOTAS];/* highest addid()'ed identifier per type */
    122 static int needswap;	/* FS is in swapped order */
    123 static int got_siginfo = 0; /* got a siginfo signal */
    124 static int is_ufs2;
    125 
    126 
    127 int main __P((int, char *[]));
    128 static void usage __P((void));
    129 static void *needchk __P((struct fstab *));
    130 static int chkquota __P((const char *, const char *, const char *, void *,
    131     pid_t *));
    132 static int update __P((const char *, const char *, int));
    133 static u_long skipforward __P((u_long, u_long, FILE *));
    134 static int oneof __P((const char *, char *[], int));
    135 static int getquotagid __P((void));
    136 static int hasquota __P((struct fstab *, int, char **));
    137 static struct fileusage *lookup __P((u_long, int));
    138 static struct fileusage *addid __P((u_long, int, const char *));
    139 static u_long subsequent __P((u_long, int));
    140 static union dinode *getnextinode __P((ino_t));
    141 static void setinodebuf __P((ino_t));
    142 static void freeinodebuf __P((void));
    143 static void bread __P((daddr_t, char *, long));
    144 static void infohandler __P((int sig));
    145 static void swap_dinode1(union dinode *, int);
    146 static void swap_dinode2(union dinode *, int);
    147 
    148 int
    149 main(argc, argv)
    150 	int argc;
    151 	char *argv[];
    152 {
    153 	struct fstab *fs;
    154 	struct passwd *pw;
    155 	struct group *gr;
    156 	struct quotaname *auxdata;
    157 	int i, argnum, maxrun, errs;
    158 	long done = 0;
    159 	int flags = CHECK_PREEN;
    160 	const char *name;
    161 	int ch;
    162 
    163 	errs = maxrun = 0;
    164 	while ((ch = getopt(argc, argv, "aguvqdl:")) != -1) {
    165 		switch(ch) {
    166 		case 'a':
    167 			aflag++;
    168 			break;
    169 		case 'd':
    170 			flags |= CHECK_DEBUG;
    171 			break;
    172 		case 'g':
    173 			gflag++;
    174 			break;
    175 		case 'u':
    176 			uflag++;
    177 			break;
    178 		case 'q':
    179 			qflag++;
    180 			break;
    181 		case 'v':
    182 			vflag++;
    183 			break;
    184 		case 'l':
    185 			maxrun = atoi(optarg);
    186 			break;
    187 		default:
    188 			usage();
    189 		}
    190 	}
    191 	argc -= optind;
    192 	argv += optind;
    193 	if ((argc == 0 && !aflag) || (argc > 0 && aflag) || (!aflag && maxrun))
    194 		usage();
    195 	if (!gflag && !uflag) {
    196 		gflag++;
    197 		uflag++;
    198 	}
    199 
    200 	/* If -a, we do not want to pay the cost of processing every
    201 	 * group and password entry if there are no filesystems with quotas
    202 	 */
    203 	if (aflag) {
    204 		i = 0;
    205 		while ((fs = getfsent()) != NULL) {
    206 			if (needchk(fs))
    207 				i=1;
    208 		}
    209 		endfsent();
    210 		if (!i)	/* No filesystems with quotas */
    211 			exit(0);
    212 	}
    213 
    214 	if (gflag) {
    215 		setgrent();
    216 		while ((gr = getgrent()) != 0)
    217 			(void) addid((u_long)gr->gr_gid, GRPQUOTA, gr->gr_name);
    218 		endgrent();
    219 	}
    220 	if (uflag) {
    221 		setpwent();
    222 		while ((pw = getpwent()) != 0)
    223 			(void) addid((u_long)pw->pw_uid, USRQUOTA, pw->pw_name);
    224 		endpwent();
    225 	}
    226 	if (aflag)
    227 		exit(checkfstab(flags, maxrun, needchk, chkquota));
    228 	if (setfsent() == 0)
    229 		err(1, "%s: can't open", FSTAB);
    230 	while ((fs = getfsent()) != NULL) {
    231 		if (((argnum = oneof(fs->fs_file, argv, argc)) >= 0 ||
    232 		    (argnum = oneof(fs->fs_spec, argv, argc)) >= 0) &&
    233 		    (auxdata = needchk(fs)) &&
    234 		    (name = blockcheck(fs->fs_spec))) {
    235 			done |= 1 << argnum;
    236 			errs += chkquota(fs->fs_type, name, fs->fs_file,
    237 			    auxdata, NULL);
    238 		}
    239 	}
    240 	endfsent();
    241 	for (i = 0; i < argc; i++)
    242 		if ((done & (1 << i)) == 0)
    243 			fprintf(stderr, "%s not found in %s\n",
    244 				argv[i], FSTAB);
    245 	exit(errs);
    246 }
    247 
    248 static void
    249 usage()
    250 {
    251 
    252 	(void)fprintf(stderr,
    253 	    "usage:\t%s -a [-gquv] [-l maxparallel]\n\t%s [-gquv] filesys ...\n", getprogname(),
    254 	    getprogname());
    255 	exit(1);
    256 }
    257 
    258 static void *
    259 needchk(fs)
    260 	struct fstab *fs;
    261 {
    262 	struct quotaname *qnp;
    263 	char *qfnp;
    264 
    265 	if (strcmp(fs->fs_vfstype, "ffs") ||
    266 	    strcmp(fs->fs_type, FSTAB_RW))
    267 		return (NULL);
    268 	if ((qnp = malloc(sizeof(*qnp))) == NULL)
    269 		err(1, "%s", strerror(errno));
    270 	qnp->flags = 0;
    271 	if (gflag && hasquota(fs, GRPQUOTA, &qfnp)) {
    272 		strlcpy(qnp->grpqfname, qfnp, sizeof(qnp->grpqfname));
    273 		qnp->flags |= HASGRP;
    274 	}
    275 	if (uflag && hasquota(fs, USRQUOTA, &qfnp)) {
    276 		strlcpy(qnp->usrqfname, qfnp, sizeof(qnp->usrqfname));
    277 		qnp->flags |= HASUSR;
    278 	}
    279 	if (qnp->flags)
    280 		return (qnp);
    281 	free(qnp);
    282 	return (NULL);
    283 }
    284 
    285 off_t sblock_try[] = SBLOCKSEARCH;
    286 
    287 /*
    288  * Scan the specified filesystem to check quota(s) present on it.
    289  */
    290 static int
    291 chkquota(type, fsname, mntpt, v, pid)
    292 	const char *type, *fsname, *mntpt;
    293 	void *v;
    294 	pid_t *pid;
    295 {
    296 	struct quotaname *qnp = v;
    297 	struct fileusage *fup;
    298 	union dinode *dp;
    299 	int cg, i, mode, errs = 0, inosused;
    300 	ino_t ino;
    301 	struct cg *cgp;
    302 	char msgbuf[4096];
    303 
    304 	if (pid != NULL) {
    305 		fflush(stdout);
    306 		switch ((*pid = fork())) {
    307 		default:
    308 			return 0;
    309 		case 0:
    310 			break;
    311 		case -1:
    312 			err(1, "Cannot fork");
    313 		}
    314 		setvbuf(stdout, msgbuf, _IOFBF, sizeof msgbuf);
    315 	}
    316 
    317 	if ((fi = open(fsname, O_RDONLY, 0)) < 0) {
    318 		warn("Cannot open %s", fsname);
    319 		if (pid != NULL)
    320 			exit(1);
    321 		return 1;
    322 	}
    323 	if (vflag) {
    324 		(void)printf("*** Checking ");
    325 		if (qnp->flags & HASUSR)
    326 			(void)printf("%s%s", qfextension[USRQUOTA],
    327 			    (qnp->flags & HASGRP) ? " and " : "");
    328 		if (qnp->flags & HASGRP)
    329 			(void)printf("%s", qfextension[GRPQUOTA]);
    330 		(void)printf(" quotas for %s (%s)\n", fsname, mntpt);
    331 		fflush(stdout);
    332 	}
    333 	signal(SIGINFO, infohandler);
    334 	sync();
    335 	dev_bsize = 1;
    336 
    337 	cgp = malloc(sblock.fs_cgsize);
    338 	if (cgp == NULL) {
    339 		warn("%s: can't allocate %d bytes of cg space", fsname,
    340 		    sblock.fs_cgsize);
    341 		if (pid != NULL)
    342 			exit(1);
    343 		return 1;
    344 	}
    345 
    346 	for (i = 0; ; i++) {
    347 		if (sblock_try[i] == -1) {
    348 			warnx("%s: superblock not found", fsname);
    349 			free(cgp);
    350 			if (pid != NULL)
    351 				exit(1);
    352 			return 1;
    353 		}
    354 		bread(sblock_try[i], (char *)&sblock, SBLOCKSIZE);
    355 		switch (sblock.fs_magic) {
    356 		case FS_UFS2_MAGIC:
    357 			is_ufs2 = 1;
    358 			/*FALLTHROUGH*/
    359 		case FS_UFS1_MAGIC:
    360 			break;
    361 		case FS_UFS2_MAGIC_SWAPPED:
    362 			is_ufs2 = 1;
    363 			/*FALLTHROUGH*/
    364 		case FS_UFS1_MAGIC_SWAPPED:
    365 			needswap = 1;
    366 			ffs_sb_swap(&sblock, &sblock);
    367 			break;
    368 		default:
    369 			continue;
    370 		}
    371 
    372 		if (is_ufs2 || sblock.fs_old_flags & FS_FLAGS_UPDATED) {
    373 			if (sblock.fs_sblockloc != sblock_try[i])
    374 				continue;
    375 		} else {
    376 			if (sblock_try[i] == SBLOCK_UFS2)
    377 				continue;
    378 		}
    379 		break;
    380 	}
    381 
    382 	dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1);
    383 	maxino = sblock.fs_ncg * sblock.fs_ipg;
    384 	for (ino = 0, cg = 0; cg < sblock.fs_ncg; cg++) {
    385 		setinodebuf(cg * sblock.fs_ipg);
    386 		if (sblock.fs_magic == FS_UFS2_MAGIC) {
    387 			bread(fsbtodb(&sblock, cgtod(&sblock, cg)), (char *)cgp,
    388 			    sblock.fs_cgsize);
    389 			if (needswap)
    390 				ffs_cg_swap(cgp, cgp, &sblock);
    391 			inosused = cgp->cg_initediblk;
    392 		} else
    393 			inosused = sblock.fs_ipg;
    394 		for (i = 0; i < inosused; i++, ino++) {
    395 			if (got_siginfo) {
    396 				fprintf(stderr,
    397 				    "%s: cyl group %d of %d (%d%%)\n",
    398 				    fsname, cg, sblock.fs_ncg,
    399 				    cg * 100 / sblock.fs_ncg);
    400 				got_siginfo = 0;
    401 			}
    402 			if (ino < ROOTINO)
    403 				continue;
    404 			if ((dp = getnextinode(ino)) == NULL)
    405 				continue;
    406 			if ((mode = DIP(dp, mode) & IFMT) == 0)
    407 				continue;
    408 			if (qnp->flags & HASGRP) {
    409 				fup = addid((u_long)DIP(dp, gid), GRPQUOTA,
    410 				    (char *)0);
    411 				fup->fu_curinodes++;
    412 				if (mode == IFREG || mode == IFDIR ||
    413 				    mode == IFLNK)
    414 					fup->fu_curblocks += DIP(dp, blocks);
    415 			}
    416 			if (qnp->flags & HASUSR) {
    417 				fup = addid((u_long)DIP(dp, uid), USRQUOTA,
    418 				    (char *)0);
    419 				fup->fu_curinodes++;
    420 				if (mode == IFREG || mode == IFDIR ||
    421 				    mode == IFLNK)
    422 					fup->fu_curblocks += DIP(dp, blocks);
    423 			}
    424 		}
    425 	}
    426 	freeinodebuf();
    427 	free(cgp);
    428 	if (qnp->flags & HASUSR)
    429 		errs += update(mntpt, qnp->usrqfname, USRQUOTA);
    430 	if (qnp->flags & HASGRP)
    431 		errs += update(mntpt, qnp->grpqfname, GRPQUOTA);
    432 	close(fi);
    433 	if (pid != NULL)
    434 		exit(errs);
    435 	return errs;
    436 }
    437 
    438 /*
    439  * Update a specified quota file.
    440  */
    441 static int
    442 update(fsname, quotafile, type)
    443 	const char *fsname, *quotafile;
    444 	int type;
    445 {
    446 	struct fileusage *fup;
    447 	FILE *qfi, *qfo;
    448 	u_long id, lastid, nextid;
    449 	int need_seek;
    450 	struct dqblk dqbuf;
    451 	static int warned = 0;
    452 	static struct dqblk zerodqbuf;
    453 	static struct fileusage zerofileusage;
    454 
    455 	if ((qfo = fopen(quotafile, "r+")) == NULL) {
    456 		if (errno == ENOENT)
    457 			qfo = fopen(quotafile, "w+");
    458 		if (qfo) {
    459 			(void) fprintf(stderr,
    460 			    "quotacheck: creating quota file %s\n", quotafile);
    461 #define	MODE	(S_IRUSR|S_IWUSR|S_IRGRP)
    462 			(void) fchown(fileno(qfo), getuid(), getquotagid());
    463 			(void) fchmod(fileno(qfo), MODE);
    464 		} else {
    465 			(void) fprintf(stderr,
    466 			    "quotacheck: %s: %s\n", quotafile, strerror(errno));
    467 			return (1);
    468 		}
    469 	}
    470 	if ((qfi = fopen(quotafile, "r")) == NULL) {
    471 		(void) fprintf(stderr,
    472 		    "quotacheck: %s: %s\n", quotafile, strerror(errno));
    473 		(void) fclose(qfo);
    474 		return (1);
    475 	}
    476 	if (quotactl(fsname, QCMD(Q_SYNC, type), (u_long)0, (caddr_t)0) < 0 &&
    477 	    errno == EOPNOTSUPP && !warned && vflag) {
    478 		warned++;
    479 		(void)printf("*** Warning: %s\n",
    480 		    "Quotas are not compiled into this kernel");
    481 	}
    482 	need_seek = 1;
    483 	for (lastid = highid[type], id = 0; id <= lastid; id = nextid) {
    484 		if (fread((char *)&dqbuf, sizeof(struct dqblk), 1, qfi) == 0)
    485 			dqbuf = zerodqbuf;
    486 		if ((fup = lookup(id, type)) == 0)
    487 			fup = &zerofileusage;
    488 
    489 		nextid = subsequent(id, type);
    490 		if (nextid != id + 1)
    491 			nextid = skipforward(id, nextid, qfi);
    492 
    493 		if (got_siginfo) {
    494 			fprintf(stderr,
    495 			    "%s: updating %s quotas for id=%ld (%s)\n", fsname,
    496 			    qfextension[type < MAXQUOTAS ? type : MAXQUOTAS],
    497 			    id, fup->fu_name);
    498 			got_siginfo = 0;
    499 		}
    500 		if (dqbuf.dqb_curinodes == fup->fu_curinodes &&
    501 		    dqbuf.dqb_curblocks == fup->fu_curblocks) {
    502 			fup->fu_curinodes = 0;	/* reset usage  */
    503 			fup->fu_curblocks = 0;	/* for next filesystem */
    504 
    505 			need_seek = 1;
    506 
    507 			if (id == ULONG_MAX)	/* ++id == 0, infinite loop */
    508 				break;
    509 			continue;
    510 		}
    511 		if (vflag) {
    512 			if (aflag)
    513 				printf("%s: ", fsname);
    514 			printf("%-8s fixed:", fup->fu_name);
    515 			if (dqbuf.dqb_curinodes != fup->fu_curinodes)
    516 				(void)printf("\tinodes %d -> %ld",
    517 					dqbuf.dqb_curinodes, fup->fu_curinodes);
    518 			if (dqbuf.dqb_curblocks != fup->fu_curblocks)
    519 				(void)printf("\tblocks %d -> %ld",
    520 					dqbuf.dqb_curblocks, fup->fu_curblocks);
    521 			(void)printf("\n");
    522 		}
    523 		/*
    524 		 * Reset time limit if have a soft limit and were
    525 		 * previously under it, but are now over it.
    526 		 */
    527 		if (dqbuf.dqb_bsoftlimit &&
    528 		    dqbuf.dqb_curblocks < dqbuf.dqb_bsoftlimit &&
    529 		    fup->fu_curblocks >= dqbuf.dqb_bsoftlimit)
    530 			dqbuf.dqb_btime = 0;
    531 		if (dqbuf.dqb_isoftlimit &&
    532 		    dqbuf.dqb_curblocks < dqbuf.dqb_isoftlimit &&
    533 		    fup->fu_curblocks >= dqbuf.dqb_isoftlimit)
    534 			dqbuf.dqb_itime = 0;
    535 		dqbuf.dqb_curinodes = fup->fu_curinodes;
    536 		dqbuf.dqb_curblocks = fup->fu_curblocks;
    537 
    538 		if (need_seek) {
    539 			(void) fseeko(qfo, (off_t)id * sizeof(struct dqblk),
    540 			    SEEK_SET);
    541 			need_seek = nextid != id + 1;
    542 		}
    543 		(void) fwrite((char *)&dqbuf, sizeof(struct dqblk), 1, qfo);
    544 
    545 		if (!warned)
    546 			(void) quotactl(fsname, QCMD(Q_SETUSE, type), id,
    547 			    (caddr_t)&dqbuf);
    548 
    549 		fup->fu_curinodes = 0;
    550 		fup->fu_curblocks = 0;
    551 		if (id == ULONG_MAX)
    552 			break;
    553 	}
    554 	(void) fclose(qfi);
    555 	(void) fflush(qfo);
    556 	if (highid[type] != ULONG_MAX)
    557 		(void) ftruncate(fileno(qfo),
    558 		    (off_t)((highid[type] + 1) * sizeof(struct dqblk)));
    559 	(void) fclose(qfo);
    560 	return (0);
    561 }
    562 
    563 u_long
    564 skipforward(cur, to, qfi)
    565 	u_long cur, to;
    566 	FILE *qfi;
    567 {
    568 	struct dqblk dqbuf;
    569 
    570 	if (qflag) {
    571 		(void) fseeko(qfi, (off_t)to * sizeof(struct dqblk), SEEK_SET);
    572 		return (to);
    573 	}
    574 
    575 	while (++cur < to) {
    576 		/*
    577 		 * if EOF occurs, nothing left to read, we're done
    578 		 */
    579 		if (fread((char *)&dqbuf, sizeof(struct dqblk), 1, qfi) == 0)
    580 			return (to);
    581 
    582 		/*
    583 		 * If we find an entry that shows usage, before the next
    584 		 * id that has actual usage, we have to stop here, so the
    585 		 * incorrect entry can be corrected in the file
    586 		 */
    587 		if (dqbuf.dqb_curinodes != 0 || dqbuf.dqb_curblocks != 0) {
    588 			(void)fseek(qfi, -(long)sizeof(struct dqblk), SEEK_CUR);
    589 			return (cur);
    590 		}
    591 	}
    592 	return (to);
    593 }
    594 
    595 /*
    596  * Check to see if target appears in list of size cnt.
    597  */
    598 static int
    599 oneof(target, list, cnt)
    600 	const char *target;
    601 	char *list[];
    602 	int cnt;
    603 {
    604 	int i;
    605 
    606 	for (i = 0; i < cnt; i++)
    607 		if (strcmp(target, list[i]) == 0)
    608 			return (i);
    609 	return (-1);
    610 }
    611 
    612 /*
    613  * Determine the group identifier for quota files.
    614  */
    615 static int
    616 getquotagid()
    617 {
    618 	struct group *gr;
    619 
    620 	if ((gr = getgrnam(quotagroup)) != NULL)
    621 		return (gr->gr_gid);
    622 	return (-1);
    623 }
    624 
    625 /*
    626  * Check to see if a particular quota is to be enabled.
    627  */
    628 static int
    629 hasquota(fs, type, qfnamep)
    630 	struct fstab *fs;
    631 	int type;
    632 	char **qfnamep;
    633 {
    634 	char *opt;
    635 	char *cp = NULL;
    636 	static char initname, usrname[100], grpname[100];
    637 	static char buf[BUFSIZ];
    638 
    639 	if (!initname) {
    640 		(void)snprintf(usrname, sizeof(usrname),
    641 		    "%s%s", qfextension[USRQUOTA], qfname);
    642 		(void)snprintf(grpname, sizeof(grpname),
    643 		    "%s%s", qfextension[GRPQUOTA], qfname);
    644 		initname = 1;
    645 	}
    646 	(void) strlcpy(buf, fs->fs_mntops, sizeof(buf));
    647 	for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
    648 		if ((cp = strchr(opt, '=')) != NULL)
    649 			*cp++ = '\0';
    650 		if (type == USRQUOTA && strcmp(opt, usrname) == 0)
    651 			break;
    652 		if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
    653 			break;
    654 	}
    655 	if (!opt)
    656 		return (0);
    657 	if (cp)
    658 		*qfnamep = cp;
    659 	else {
    660 		(void)snprintf(buf, sizeof(buf),
    661 		    "%s/%s.%s", fs->fs_file, qfname, qfextension[type]);
    662 		*qfnamep = buf;
    663 	}
    664 	return (1);
    665 }
    666 
    667 /*
    668  * Routines to manage the file usage table.
    669  *
    670  * Lookup an id of a specific type.
    671  */
    672 static struct fileusage *
    673 lookup(id, type)
    674 	u_long id;
    675 	int type;
    676 {
    677 	struct fileusage *fup;
    678 
    679 	for (fup = fuhead[type][id & (FUHASH-1)]; fup != 0; fup = fup->fu_next)
    680 		if (fup->fu_id == id)
    681 			return (fup);
    682 	return (NULL);
    683 }
    684 
    685 /*
    686  * Add a new file usage id if it does not already exist.
    687  */
    688 static struct fileusage *
    689 addid(id, type, name)
    690 	u_long id;
    691 	int type;
    692 	const char *name;
    693 {
    694 	struct fileusage *fup, **fhp;
    695 	int len;
    696 
    697 	if ((fup = lookup(id, type)) != NULL)
    698 		return (fup);
    699 	if (name)
    700 		len = strlen(name);
    701 	else
    702 		len = 10;
    703 	if ((fup = calloc(1, sizeof(*fup) + len)) == NULL)
    704 		err(1, "%s", strerror(errno));
    705 	fhp = &fuhead[type][id & (FUHASH - 1)];
    706 	fup->fu_next = *fhp;
    707 	*fhp = fup;
    708 	fup->fu_id = id;
    709 	if (id > highid[type])
    710 		highid[type] = id;
    711 	if (name)
    712 		memmove(fup->fu_name, name, len + 1);
    713 	else
    714 		(void)sprintf(fup->fu_name, "%lu", id);
    715 	return (fup);
    716 }
    717 
    718 static u_long
    719 subsequent(id, type)
    720 	u_long id;
    721 	int type;
    722 {
    723 	struct fileusage *fup, **iup, **cup;
    724 	u_long next, offset;
    725 
    726 	next = highid[type] + 1;
    727 	offset = 0;
    728 	cup = iup = &fuhead[type][id & (FUHASH-1)];
    729 	do {
    730 		++offset;
    731 		if (++cup >= &fuhead[type][FUHASH])
    732 			cup = &fuhead[type][0];
    733 		for (fup = *cup; fup != 0; fup = fup->fu_next) {
    734 			if (fup->fu_id > id && fup->fu_id <= id + offset)
    735 				return (fup->fu_id);
    736 			if (fup->fu_id > id && fup->fu_id < next)
    737 				next = fup->fu_id;
    738 		}
    739 	} while (cup != iup);
    740 
    741 	return next;
    742 }
    743 
    744 /*
    745  * Special purpose version of ginode used to optimize first pass
    746  * over all the inodes in numerical order.
    747  */
    748 static ino_t nextino, lastinum, lastvalidinum;
    749 static long readcnt, readpercg, fullcnt, inobufsize, partialcnt, partialsize;
    750 static union dinode *inodebuf;
    751 #define INOBUFSIZE	56*1024	/* size of buffer to read inodes */
    752 
    753 union dinode *
    754 getnextinode(inumber)
    755 	ino_t inumber;
    756 {
    757 	long size;
    758 	daddr_t dblk;
    759 	static union dinode *dp;
    760 	union dinode *ret;
    761 
    762 	if (inumber != nextino++ || inumber > lastvalidinum) {
    763 		errx(1, "bad inode number %d to nextinode", inumber);
    764 	}
    765 
    766 	if (inumber >= lastinum) {
    767 		readcnt++;
    768 		dblk = fsbtodb(&sblock, ino_to_fsba(&sblock, lastinum));
    769 		if (readcnt % readpercg == 0) {
    770 			size = partialsize;
    771 			lastinum += partialcnt;
    772 		} else {
    773 			size = inobufsize;
    774 			lastinum += fullcnt;
    775 		}
    776 		(void)bread(dblk, (caddr_t)inodebuf, size);
    777 		if (needswap) {
    778 			if (is_ufs2)
    779 				swap_dinode2(inodebuf, lastinum - inumber);
    780 			else
    781 				swap_dinode1(inodebuf, lastinum - inumber);
    782 		}
    783 		dp = (union dinode *)inodebuf;
    784 	}
    785 	ret = dp;
    786 	dp = (union dinode *)
    787 	    ((char *)dp + (is_ufs2 ? DINODE2_SIZE : DINODE1_SIZE));
    788 	return ret;
    789 }
    790 
    791 void
    792 setinodebuf(inum)
    793 	ino_t inum;
    794 {
    795 
    796 	if (inum % sblock.fs_ipg != 0)
    797 		errx(1, "bad inode number %d to setinodebuf", inum);
    798 
    799 	lastvalidinum = inum + sblock.fs_ipg - 1;
    800 	nextino = inum;
    801 	lastinum = inum;
    802 	readcnt = 0;
    803 	if (inodebuf != NULL)
    804 		return;
    805 	inobufsize = blkroundup(&sblock, INOBUFSIZE);
    806 	fullcnt = inobufsize / (is_ufs2 ? DINODE2_SIZE : DINODE1_SIZE);
    807 	readpercg = sblock.fs_ipg / fullcnt;
    808 	partialcnt = sblock.fs_ipg % fullcnt;
    809 	partialsize = partialcnt * (is_ufs2 ? DINODE2_SIZE : DINODE1_SIZE);
    810 	if (partialcnt != 0) {
    811 		readpercg++;
    812 	} else {
    813 		partialcnt = fullcnt;
    814 		partialsize = inobufsize;
    815 	}
    816 	if (inodebuf == NULL &&
    817 	    (inodebuf = malloc((unsigned)inobufsize)) == NULL)
    818 		errx(1, "Cannot allocate space for inode buffer");
    819 	while (nextino < ROOTINO)
    820 		getnextinode(nextino);
    821 }
    822 
    823 void
    824 freeinodebuf()
    825 {
    826 
    827 	if (inodebuf != NULL)
    828 		free((char *)inodebuf);
    829 	inodebuf = NULL;
    830 }
    831 
    832 
    833 static void
    834 swap_dinode1(union dinode *dp, int n)
    835 {
    836 	int i;
    837 	struct ufs1_dinode *dp1;
    838 
    839 	dp1 = (struct ufs1_dinode *)&dp->dp1;
    840 	for (i = 0; i < n; i++, dp1++)
    841 		ffs_dinode1_swap(dp1, dp1);
    842 }
    843 
    844 static void
    845 swap_dinode2(union dinode *dp, int n)
    846 {
    847 	int i;
    848 	struct ufs2_dinode *dp2;
    849 
    850 	dp2 = (struct ufs2_dinode *)&dp->dp2;
    851 	for (i = 0; i < n; i++, dp2++)
    852 		ffs_dinode2_swap(dp2, dp2);
    853 }
    854 
    855 /*
    856  * Read specified disk blocks.
    857  */
    858 static void
    859 bread(bno, buf, cnt)
    860 	daddr_t bno;
    861 	char *buf;
    862 	long cnt;
    863 {
    864 
    865 	if (lseek(fi, (off_t)bno * dev_bsize, SEEK_SET) < 0 ||
    866 	    read(fi, buf, cnt) != cnt)
    867 		err(1, "block %lld", (long long)bno);
    868 }
    869 
    870 void
    871 infohandler(int sig)
    872 {
    873 	got_siginfo = 1;
    874 }
    875