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